1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2012, 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 function Expand_Composite_Equality
148 Bodies : List_Id) return Node_Id;
149 -- Local recursive function used to expand equality for nested composite
150 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
151 -- to attach bodies of local functions that are created in the process.
152 -- This is the responsibility of the caller to insert those bodies at the
153 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
154 -- are the left and right sides for the comparison, and Typ is the type of
155 -- the arrays to compare.
157 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
158 -- Routine to expand concatenation of a sequence of two or more operands
159 -- (in the list Operands) and replace node Cnode with the result of the
160 -- concatenation. The operands can be of any appropriate type, and can
161 -- include both arrays and singleton elements.
163 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
164 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
165 -- fixed. We do not have such a type at runtime, so the purpose of this
166 -- routine is to find the real type by looking up the tree. We also
167 -- determine if the operation must be rounded.
169 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
170 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
171 -- discriminants if it has a constrained nominal type, unless the object
172 -- is a component of an enclosing Unchecked_Union object that is subject
173 -- to a per-object constraint and the enclosing object lacks inferable
176 -- An expression of an Unchecked_Union type has inferable discriminants
177 -- if it is either a name of an object with inferable discriminants or a
178 -- qualified expression whose subtype mark denotes a constrained subtype.
180 procedure Insert_Dereference_Action (N : Node_Id);
181 -- N is an expression whose type is an access. When the type of the
182 -- associated storage pool is derived from Checked_Pool, generate a
183 -- call to the 'Dereference' primitive operation.
185 function Make_Array_Comparison_Op
187 Nod : Node_Id) return Node_Id;
188 -- Comparisons between arrays are expanded in line. This function produces
189 -- the body of the implementation of (a > b), where a and b are one-
190 -- dimensional arrays of some discrete type. The original node is then
191 -- expanded into the appropriate call to this function. Nod provides the
192 -- Sloc value for the generated code.
194 function Make_Boolean_Array_Op
196 N : Node_Id) return Node_Id;
197 -- Boolean operations on boolean arrays are expanded in line. This function
198 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
199 -- b). It is used only the normal case and not the packed case. The type
200 -- involved, Typ, is the Boolean array type, and the logical operations in
201 -- the body are simple boolean operations. Note that Typ is always a
202 -- constrained type (the caller has ensured this by using
203 -- Convert_To_Actual_Subtype if necessary).
205 procedure Optimize_Length_Comparison (N : Node_Id);
206 -- Given an expression, if it is of the form X'Length op N (or the other
207 -- way round), where N is known at compile time to be 0 or 1, and X is a
208 -- simple entity, and op is a comparison operator, optimizes it into a
209 -- comparison of First and Last.
211 procedure Rewrite_Comparison (N : Node_Id);
212 -- If N is the node for a comparison whose outcome can be determined at
213 -- compile time, then the node N can be rewritten with True or False. If
214 -- the outcome cannot be determined at compile time, the call has no
215 -- effect. If N is a type conversion, then this processing is applied to
216 -- its expression. If N is neither comparison nor a type conversion, the
217 -- call has no effect.
219 procedure Tagged_Membership
221 SCIL_Node : out Node_Id;
222 Result : out Node_Id);
223 -- Construct the expression corresponding to the tagged membership test.
224 -- Deals with a second operand being (or not) a class-wide type.
226 function Safe_In_Place_Array_Op
229 Op2 : Node_Id) return Boolean;
230 -- In the context of an assignment, where the right-hand side is a boolean
231 -- operation on arrays, check whether operation can be performed in place.
233 procedure Unary_Op_Validity_Checks (N : Node_Id);
234 pragma Inline (Unary_Op_Validity_Checks);
235 -- Performs validity checks for a unary operator
237 -------------------------------
238 -- Binary_Op_Validity_Checks --
239 -------------------------------
241 procedure Binary_Op_Validity_Checks (N : Node_Id) is
243 if Validity_Checks_On and Validity_Check_Operands then
244 Ensure_Valid (Left_Opnd (N));
245 Ensure_Valid (Right_Opnd (N));
247 end Binary_Op_Validity_Checks;
249 ------------------------------------
250 -- Build_Boolean_Array_Proc_Call --
251 ------------------------------------
253 procedure Build_Boolean_Array_Proc_Call
258 Loc : constant Source_Ptr := Sloc (N);
259 Kind : constant Node_Kind := Nkind (Expression (N));
260 Target : constant Node_Id :=
261 Make_Attribute_Reference (Loc,
263 Attribute_Name => Name_Address);
265 Arg1 : Node_Id := Op1;
266 Arg2 : Node_Id := Op2;
268 Proc_Name : Entity_Id;
271 if Kind = N_Op_Not then
272 if Nkind (Op1) in N_Binary_Op then
274 -- Use negated version of the binary operators
276 if Nkind (Op1) = N_Op_And then
277 Proc_Name := RTE (RE_Vector_Nand);
279 elsif Nkind (Op1) = N_Op_Or then
280 Proc_Name := RTE (RE_Vector_Nor);
282 else pragma Assert (Nkind (Op1) = N_Op_Xor);
283 Proc_Name := RTE (RE_Vector_Xor);
287 Make_Procedure_Call_Statement (Loc,
288 Name => New_Occurrence_Of (Proc_Name, Loc),
290 Parameter_Associations => New_List (
292 Make_Attribute_Reference (Loc,
293 Prefix => Left_Opnd (Op1),
294 Attribute_Name => Name_Address),
296 Make_Attribute_Reference (Loc,
297 Prefix => Right_Opnd (Op1),
298 Attribute_Name => Name_Address),
300 Make_Attribute_Reference (Loc,
301 Prefix => Left_Opnd (Op1),
302 Attribute_Name => Name_Length)));
305 Proc_Name := RTE (RE_Vector_Not);
308 Make_Procedure_Call_Statement (Loc,
309 Name => New_Occurrence_Of (Proc_Name, Loc),
310 Parameter_Associations => New_List (
313 Make_Attribute_Reference (Loc,
315 Attribute_Name => Name_Address),
317 Make_Attribute_Reference (Loc,
319 Attribute_Name => Name_Length)));
323 -- We use the following equivalences:
325 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
326 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
327 -- (not X) xor (not Y) = X xor Y
328 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
330 if Nkind (Op1) = N_Op_Not then
331 Arg1 := Right_Opnd (Op1);
332 Arg2 := Right_Opnd (Op2);
333 if Kind = N_Op_And then
334 Proc_Name := RTE (RE_Vector_Nor);
335 elsif Kind = N_Op_Or then
336 Proc_Name := RTE (RE_Vector_Nand);
338 Proc_Name := RTE (RE_Vector_Xor);
342 if Kind = N_Op_And then
343 Proc_Name := RTE (RE_Vector_And);
344 elsif Kind = N_Op_Or then
345 Proc_Name := RTE (RE_Vector_Or);
346 elsif Nkind (Op2) = N_Op_Not then
347 Proc_Name := RTE (RE_Vector_Nxor);
348 Arg2 := Right_Opnd (Op2);
350 Proc_Name := RTE (RE_Vector_Xor);
355 Make_Procedure_Call_Statement (Loc,
356 Name => New_Occurrence_Of (Proc_Name, Loc),
357 Parameter_Associations => New_List (
359 Make_Attribute_Reference (Loc,
361 Attribute_Name => Name_Address),
362 Make_Attribute_Reference (Loc,
364 Attribute_Name => Name_Address),
365 Make_Attribute_Reference (Loc,
367 Attribute_Name => Name_Length)));
370 Rewrite (N, Call_Node);
374 when RE_Not_Available =>
376 end Build_Boolean_Array_Proc_Call;
378 ------------------------------
379 -- Current_Anonymous_Master --
380 ------------------------------
382 function Current_Anonymous_Master return Entity_Id is
390 Unit_Id := Cunit_Entity (Current_Sem_Unit);
392 -- Find the entity of the current unit
394 if Ekind (Unit_Id) = E_Subprogram_Body then
396 -- When processing subprogram bodies, the proper scope is always that
399 Subp_Body := Unit_Id;
400 while Present (Subp_Body)
401 and then Nkind (Subp_Body) /= N_Subprogram_Body
403 Subp_Body := Parent (Subp_Body);
406 Unit_Id := Corresponding_Spec (Subp_Body);
409 Loc := Sloc (Unit_Id);
410 Unit_Decl := Unit (Cunit (Current_Sem_Unit));
412 -- Find the declarations list of the current unit
414 if Nkind (Unit_Decl) = N_Package_Declaration then
415 Unit_Decl := Specification (Unit_Decl);
416 Decls := Visible_Declarations (Unit_Decl);
419 Decls := New_List (Make_Null_Statement (Loc));
420 Set_Visible_Declarations (Unit_Decl, Decls);
422 elsif Is_Empty_List (Decls) then
423 Append_To (Decls, Make_Null_Statement (Loc));
427 Decls := Declarations (Unit_Decl);
430 Decls := New_List (Make_Null_Statement (Loc));
431 Set_Declarations (Unit_Decl, Decls);
433 elsif Is_Empty_List (Decls) then
434 Append_To (Decls, Make_Null_Statement (Loc));
438 -- The current unit has an existing anonymous master, traverse its
439 -- declarations and locate the entity.
441 if Has_Anonymous_Master (Unit_Id) then
444 Fin_Mas_Id : Entity_Id;
447 Decl := First (Decls);
448 while Present (Decl) loop
450 -- Look for the first variable in the declarations whole type
451 -- is Finalization_Master.
453 if Nkind (Decl) = N_Object_Declaration then
454 Fin_Mas_Id := Defining_Identifier (Decl);
456 if Ekind (Fin_Mas_Id) = E_Variable
457 and then Etype (Fin_Mas_Id) = RTE (RE_Finalization_Master)
466 -- The master was not found even though the unit was labeled as
472 -- Create a new anonymous master
476 First_Decl : constant Node_Id := First (Decls);
478 Fin_Mas_Id : Entity_Id;
481 -- Since the master and its associated initialization is inserted
482 -- at top level, use the scope of the unit when analyzing.
484 Push_Scope (Unit_Id);
486 -- Create the finalization master
489 Make_Defining_Identifier (Loc,
490 Chars => New_External_Name (Chars (Unit_Id), "AM"));
493 -- <Fin_Mas_Id> : Finalization_Master;
496 Make_Object_Declaration (Loc,
497 Defining_Identifier => Fin_Mas_Id,
499 New_Reference_To (RTE (RE_Finalization_Master), Loc));
501 Insert_Before_And_Analyze (First_Decl, Action);
503 -- Mark the unit to prevent the generation of multiple masters
505 Set_Has_Anonymous_Master (Unit_Id);
507 -- Do not set the base pool and mode of operation on .NET/JVM
508 -- since those targets do not support pools and all VM masters
509 -- are heterogeneous by default.
511 if VM_Target = No_VM then
515 -- (<Fin_Mas_Id>, Global_Pool_Object'Unrestricted_Access);
518 Make_Procedure_Call_Statement (Loc,
520 New_Reference_To (RTE (RE_Set_Base_Pool), Loc),
522 Parameter_Associations => New_List (
523 New_Reference_To (Fin_Mas_Id, Loc),
524 Make_Attribute_Reference (Loc,
526 New_Reference_To (RTE (RE_Global_Pool_Object), Loc),
527 Attribute_Name => Name_Unrestricted_Access)));
529 Insert_Before_And_Analyze (First_Decl, Action);
532 -- Set_Is_Heterogeneous (<Fin_Mas_Id>);
535 Make_Procedure_Call_Statement (Loc,
537 New_Reference_To (RTE (RE_Set_Is_Heterogeneous), Loc),
538 Parameter_Associations => New_List (
539 New_Reference_To (Fin_Mas_Id, Loc)));
541 Insert_Before_And_Analyze (First_Decl, Action);
544 -- Restore the original state of the scope stack
551 end Current_Anonymous_Master;
553 --------------------------------
554 -- Displace_Allocator_Pointer --
555 --------------------------------
557 procedure Displace_Allocator_Pointer (N : Node_Id) is
558 Loc : constant Source_Ptr := Sloc (N);
559 Orig_Node : constant Node_Id := Original_Node (N);
565 -- Do nothing in case of VM targets: the virtual machine will handle
566 -- interfaces directly.
568 if not Tagged_Type_Expansion then
572 pragma Assert (Nkind (N) = N_Identifier
573 and then Nkind (Orig_Node) = N_Allocator);
575 PtrT := Etype (Orig_Node);
576 Dtyp := Available_View (Designated_Type (PtrT));
577 Etyp := Etype (Expression (Orig_Node));
579 if Is_Class_Wide_Type (Dtyp)
580 and then Is_Interface (Dtyp)
582 -- If the type of the allocator expression is not an interface type
583 -- we can generate code to reference the record component containing
584 -- the pointer to the secondary dispatch table.
586 if not Is_Interface (Etyp) then
588 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
591 -- 1) Get access to the allocated object
594 Make_Explicit_Dereference (Loc, Relocate_Node (N)));
598 -- 2) Add the conversion to displace the pointer to reference
599 -- the secondary dispatch table.
601 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
602 Analyze_And_Resolve (N, Dtyp);
604 -- 3) The 'access to the secondary dispatch table will be used
605 -- as the value returned by the allocator.
608 Make_Attribute_Reference (Loc,
609 Prefix => Relocate_Node (N),
610 Attribute_Name => Name_Access));
611 Set_Etype (N, Saved_Typ);
615 -- If the type of the allocator expression is an interface type we
616 -- generate a run-time call to displace "this" to reference the
617 -- component containing the pointer to the secondary dispatch table
618 -- or else raise Constraint_Error if the actual object does not
619 -- implement the target interface. This case corresponds with the
620 -- following example:
622 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
624 -- return new Iface_2'Class'(Obj);
629 Unchecked_Convert_To (PtrT,
630 Make_Function_Call (Loc,
631 Name => New_Reference_To (RTE (RE_Displace), Loc),
632 Parameter_Associations => New_List (
633 Unchecked_Convert_To (RTE (RE_Address),
639 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
641 Analyze_And_Resolve (N, PtrT);
644 end Displace_Allocator_Pointer;
646 ---------------------------------
647 -- Expand_Allocator_Expression --
648 ---------------------------------
650 procedure Expand_Allocator_Expression (N : Node_Id) is
651 Loc : constant Source_Ptr := Sloc (N);
652 Exp : constant Node_Id := Expression (Expression (N));
653 PtrT : constant Entity_Id := Etype (N);
654 DesigT : constant Entity_Id := Designated_Type (PtrT);
656 procedure Apply_Accessibility_Check
658 Built_In_Place : Boolean := False);
659 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
660 -- type, generate an accessibility check to verify that the level of the
661 -- type of the created object is not deeper than the level of the access
662 -- type. If the type of the qualified expression is class- wide, then
663 -- always generate the check (except in the case where it is known to be
664 -- unnecessary, see comment below). Otherwise, only generate the check
665 -- if the level of the qualified expression type is statically deeper
666 -- than the access type.
668 -- Although the static accessibility will generally have been performed
669 -- as a legality check, it won't have been done in cases where the
670 -- allocator appears in generic body, so a run-time check is needed in
671 -- general. One special case is when the access type is declared in the
672 -- same scope as the class-wide allocator, in which case the check can
673 -- never fail, so it need not be generated.
675 -- As an open issue, there seem to be cases where the static level
676 -- associated with the class-wide object's underlying type is not
677 -- sufficient to perform the proper accessibility check, such as for
678 -- allocators in nested subprograms or accept statements initialized by
679 -- class-wide formals when the actual originates outside at a deeper
680 -- static level. The nested subprogram case might require passing
681 -- accessibility levels along with class-wide parameters, and the task
682 -- case seems to be an actual gap in the language rules that needs to
683 -- be fixed by the ARG. ???
685 -------------------------------
686 -- Apply_Accessibility_Check --
687 -------------------------------
689 procedure Apply_Accessibility_Check
691 Built_In_Place : Boolean := False)
696 if Ada_Version >= Ada_2005
697 and then Is_Class_Wide_Type (DesigT)
698 and then not Scope_Suppress (Accessibility_Check)
700 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
702 (Is_Class_Wide_Type (Etype (Exp))
703 and then Scope (PtrT) /= Current_Scope))
705 -- If the allocator was built in place Ref is already a reference
706 -- to the access object initialized to the result of the allocator
707 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise
708 -- it is the entity associated with the object containing the
709 -- address of the allocated object.
711 if Built_In_Place then
712 New_Node := New_Copy (Ref);
714 New_Node := New_Reference_To (Ref, Loc);
718 Make_Attribute_Reference (Loc,
720 Attribute_Name => Name_Tag);
722 if Tagged_Type_Expansion then
723 New_Node := Build_Get_Access_Level (Loc, New_Node);
725 elsif VM_Target /= No_VM then
727 Make_Function_Call (Loc,
728 Name => New_Reference_To (RTE (RE_Get_Access_Level), Loc),
729 Parameter_Associations => New_List (New_Node));
731 -- Cannot generate the runtime check
738 Make_Raise_Program_Error (Loc,
741 Left_Opnd => New_Node,
743 Make_Integer_Literal (Loc, Type_Access_Level (PtrT))),
744 Reason => PE_Accessibility_Check_Failed));
746 end Apply_Accessibility_Check;
750 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
751 Indic : constant Node_Id := Subtype_Mark (Expression (N));
752 T : constant Entity_Id := Entity (Indic);
754 Tag_Assign : Node_Id;
758 TagT : Entity_Id := Empty;
759 -- Type used as source for tag assignment
761 TagR : Node_Id := Empty;
762 -- Target reference for tag assignment
764 -- Start of processing for Expand_Allocator_Expression
767 -- In the case of an Ada 2012 allocator whose initial value comes from a
768 -- function call, pass "the accessibility level determined by the point
769 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
770 -- Expand_Call but it couldn't be done there (because the Etype of the
771 -- allocator wasn't set then) so we generate the parameter here. See
772 -- the Boolean variable Defer in (a block within) Expand_Call.
774 if Ada_Version >= Ada_2012 and then Nkind (Exp) = N_Function_Call then
779 if Nkind (Name (Exp)) = N_Explicit_Dereference then
780 Subp := Designated_Type (Etype (Prefix (Name (Exp))));
782 Subp := Entity (Name (Exp));
785 Subp := Ultimate_Alias (Subp);
787 if Present (Extra_Accessibility_Of_Result (Subp)) then
788 Add_Extra_Actual_To_Call
789 (Subprogram_Call => Exp,
790 Extra_Formal => Extra_Accessibility_Of_Result (Subp),
791 Extra_Actual => Dynamic_Accessibility_Level (PtrT));
796 -- Would be nice to comment the branches of this very long if ???
798 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
799 if Is_CPP_Constructor_Call (Exp) then
802 -- Pnnn : constant ptr_T := new (T);
803 -- Init (Pnnn.all,...);
805 -- Allocate the object without an expression
807 Node := Relocate_Node (N);
808 Set_Expression (Node, New_Reference_To (Etype (Exp), Loc));
810 -- Avoid its expansion to avoid generating a call to the default
815 Temp := Make_Temporary (Loc, 'P', N);
818 Make_Object_Declaration (Loc,
819 Defining_Identifier => Temp,
820 Constant_Present => True,
821 Object_Definition => New_Reference_To (PtrT, Loc),
823 Insert_Action (N, Temp_Decl);
825 Apply_Accessibility_Check (Temp);
827 -- Locate the enclosing list and insert the C++ constructor call
834 while not Is_List_Member (P) loop
838 Insert_List_After_And_Analyze (P,
839 Build_Initialization_Call (Loc,
841 Make_Explicit_Dereference (Loc,
842 Prefix => New_Reference_To (Temp, Loc)),
844 Constructor_Ref => Exp));
847 Rewrite (N, New_Reference_To (Temp, Loc));
848 Analyze_And_Resolve (N, PtrT);
852 -- Ada 2005 (AI-318-02): If the initialization expression is a call
853 -- to a build-in-place function, then access to the allocated object
854 -- must be passed to the function. Currently we limit such functions
855 -- to those with constrained limited result subtypes, but eventually
856 -- we plan to expand the allowed forms of functions that are treated
857 -- as build-in-place.
859 if Ada_Version >= Ada_2005
860 and then Is_Build_In_Place_Function_Call (Exp)
862 Make_Build_In_Place_Call_In_Allocator (N, Exp);
863 Apply_Accessibility_Check (N, Built_In_Place => True);
867 -- Actions inserted before:
868 -- Temp : constant ptr_T := new T'(Expression);
869 -- Temp._tag = T'tag; -- when not class-wide
870 -- [Deep_]Adjust (Temp.all);
872 -- We analyze by hand the new internal allocator to avoid any
873 -- recursion and inappropriate call to Initialize
875 -- We don't want to remove side effects when the expression must be
876 -- built in place. In the case of a build-in-place function call,
877 -- that could lead to a duplication of the call, which was already
878 -- substituted for the allocator.
880 if not Aggr_In_Place then
881 Remove_Side_Effects (Exp);
884 Temp := Make_Temporary (Loc, 'P', N);
886 -- For a class wide allocation generate the following code:
888 -- type Equiv_Record is record ... end record;
889 -- implicit subtype CW is <Class_Wide_Subytpe>;
890 -- temp : PtrT := new CW'(CW!(expr));
892 if Is_Class_Wide_Type (T) then
893 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
895 -- Ada 2005 (AI-251): If the expression is a class-wide interface
896 -- object we generate code to move up "this" to reference the
897 -- base of the object before allocating the new object.
899 -- Note that Exp'Address is recursively expanded into a call
900 -- to Base_Address (Exp.Tag)
902 if Is_Class_Wide_Type (Etype (Exp))
903 and then Is_Interface (Etype (Exp))
904 and then Tagged_Type_Expansion
908 Unchecked_Convert_To (Entity (Indic),
909 Make_Explicit_Dereference (Loc,
910 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
911 Make_Attribute_Reference (Loc,
913 Attribute_Name => Name_Address)))));
917 Unchecked_Convert_To (Entity (Indic), Exp));
920 Analyze_And_Resolve (Expression (N), Entity (Indic));
923 -- Processing for allocators returning non-interface types
925 if not Is_Interface (Directly_Designated_Type (PtrT)) then
926 if Aggr_In_Place then
928 Make_Object_Declaration (Loc,
929 Defining_Identifier => Temp,
930 Object_Definition => New_Reference_To (PtrT, Loc),
934 New_Reference_To (Etype (Exp), Loc)));
936 -- Copy the Comes_From_Source flag for the allocator we just
937 -- built, since logically this allocator is a replacement of
938 -- the original allocator node. This is for proper handling of
939 -- restriction No_Implicit_Heap_Allocations.
941 Set_Comes_From_Source
942 (Expression (Temp_Decl), Comes_From_Source (N));
944 Set_No_Initialization (Expression (Temp_Decl));
945 Insert_Action (N, Temp_Decl);
947 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
948 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
950 -- Attach the object to the associated finalization master.
951 -- This is done manually on .NET/JVM since those compilers do
952 -- no support pools and can't benefit from internally generated
953 -- Allocate / Deallocate procedures.
955 if VM_Target /= No_VM
956 and then Is_Controlled (DesigT)
957 and then Present (Finalization_Master (PtrT))
962 New_Reference_To (Temp, Loc),
967 Node := Relocate_Node (N);
971 Make_Object_Declaration (Loc,
972 Defining_Identifier => Temp,
973 Constant_Present => True,
974 Object_Definition => New_Reference_To (PtrT, Loc),
977 Insert_Action (N, Temp_Decl);
978 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
980 -- Attach the object to the associated finalization master.
981 -- This is done manually on .NET/JVM since those compilers do
982 -- no support pools and can't benefit from internally generated
983 -- Allocate / Deallocate procedures.
985 if VM_Target /= No_VM
986 and then Is_Controlled (DesigT)
987 and then Present (Finalization_Master (PtrT))
992 New_Reference_To (Temp, Loc),
997 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
998 -- interface type. In this case we use the type of the qualified
999 -- expression to allocate the object.
1003 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
1008 Make_Full_Type_Declaration (Loc,
1009 Defining_Identifier => Def_Id,
1011 Make_Access_To_Object_Definition (Loc,
1012 All_Present => True,
1013 Null_Exclusion_Present => False,
1014 Constant_Present => False,
1015 Subtype_Indication =>
1016 New_Reference_To (Etype (Exp), Loc)));
1018 Insert_Action (N, New_Decl);
1020 -- Inherit the allocation-related attributes from the original
1023 Set_Finalization_Master (Def_Id, Finalization_Master (PtrT));
1025 Set_Associated_Storage_Pool (Def_Id,
1026 Associated_Storage_Pool (PtrT));
1028 -- Declare the object using the previous type declaration
1030 if Aggr_In_Place then
1032 Make_Object_Declaration (Loc,
1033 Defining_Identifier => Temp,
1034 Object_Definition => New_Reference_To (Def_Id, Loc),
1036 Make_Allocator (Loc,
1037 New_Reference_To (Etype (Exp), Loc)));
1039 -- Copy the Comes_From_Source flag for the allocator we just
1040 -- built, since logically this allocator is a replacement of
1041 -- the original allocator node. This is for proper handling
1042 -- of restriction No_Implicit_Heap_Allocations.
1044 Set_Comes_From_Source
1045 (Expression (Temp_Decl), Comes_From_Source (N));
1047 Set_No_Initialization (Expression (Temp_Decl));
1048 Insert_Action (N, Temp_Decl);
1050 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1051 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1054 Node := Relocate_Node (N);
1055 Set_Analyzed (Node);
1058 Make_Object_Declaration (Loc,
1059 Defining_Identifier => Temp,
1060 Constant_Present => True,
1061 Object_Definition => New_Reference_To (Def_Id, Loc),
1062 Expression => Node);
1064 Insert_Action (N, Temp_Decl);
1065 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1068 -- Generate an additional object containing the address of the
1069 -- returned object. The type of this second object declaration
1070 -- is the correct type required for the common processing that
1071 -- is still performed by this subprogram. The displacement of
1072 -- this pointer to reference the component associated with the
1073 -- interface type will be done at the end of common processing.
1076 Make_Object_Declaration (Loc,
1077 Defining_Identifier => Make_Temporary (Loc, 'P'),
1078 Object_Definition => New_Reference_To (PtrT, Loc),
1080 Unchecked_Convert_To (PtrT,
1081 New_Reference_To (Temp, Loc)));
1083 Insert_Action (N, New_Decl);
1085 Temp_Decl := New_Decl;
1086 Temp := Defining_Identifier (New_Decl);
1090 Apply_Accessibility_Check (Temp);
1092 -- Generate the tag assignment
1094 -- Suppress the tag assignment when VM_Target because VM tags are
1095 -- represented implicitly in objects.
1097 if not Tagged_Type_Expansion then
1100 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1101 -- interface objects because in this case the tag does not change.
1103 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
1104 pragma Assert (Is_Class_Wide_Type
1105 (Directly_Designated_Type (Etype (N))));
1108 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
1110 TagR := New_Reference_To (Temp, Loc);
1112 elsif Is_Private_Type (T)
1113 and then Is_Tagged_Type (Underlying_Type (T))
1115 TagT := Underlying_Type (T);
1117 Unchecked_Convert_To (Underlying_Type (T),
1118 Make_Explicit_Dereference (Loc,
1119 Prefix => New_Reference_To (Temp, Loc)));
1122 if Present (TagT) then
1124 Full_T : constant Entity_Id := Underlying_Type (TagT);
1127 Make_Assignment_Statement (Loc,
1129 Make_Selected_Component (Loc,
1132 New_Reference_To (First_Tag_Component (Full_T), Loc)),
1134 Unchecked_Convert_To (RTE (RE_Tag),
1137 (First_Elmt (Access_Disp_Table (Full_T))), Loc)));
1140 -- The previous assignment has to be done in any case
1142 Set_Assignment_OK (Name (Tag_Assign));
1143 Insert_Action (N, Tag_Assign);
1146 if Needs_Finalization (DesigT)
1147 and then Needs_Finalization (T)
1149 -- Generate an Adjust call if the object will be moved. In Ada
1150 -- 2005, the object may be inherently limited, in which case
1151 -- there is no Adjust procedure, and the object is built in
1152 -- place. In Ada 95, the object can be limited but not
1153 -- inherently limited if this allocator came from a return
1154 -- statement (we're allocating the result on the secondary
1155 -- stack). In that case, the object will be moved, so we _do_
1158 if not Aggr_In_Place
1159 and then not Is_Immutably_Limited_Type (T)
1165 -- An unchecked conversion is needed in the classwide
1166 -- case because the designated type can be an ancestor
1167 -- of the subtype mark of the allocator.
1169 Unchecked_Convert_To (T,
1170 Make_Explicit_Dereference (Loc,
1171 Prefix => New_Reference_To (Temp, Loc))),
1176 -- Set_Finalize_Address (<PtrT>FM, <T>FD'Unrestricted_Access);
1178 -- Do not generate this call in the following cases:
1180 -- * .NET/JVM - these targets do not support address arithmetic
1181 -- and unchecked conversion, key elements of Finalize_Address.
1183 -- * Alfa mode - the call is useless and results in unwanted
1186 -- * CodePeer mode - TSS primitive Finalize_Address is not
1187 -- created in this mode.
1189 if VM_Target = No_VM
1190 and then not Alfa_Mode
1191 and then not CodePeer_Mode
1192 and then Present (Finalization_Master (PtrT))
1193 and then Present (Temp_Decl)
1194 and then Nkind (Expression (Temp_Decl)) = N_Allocator
1197 Make_Set_Finalize_Address_Call
1204 Rewrite (N, New_Reference_To (Temp, Loc));
1205 Analyze_And_Resolve (N, PtrT);
1207 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1208 -- component containing the secondary dispatch table of the interface
1211 if Is_Interface (Directly_Designated_Type (PtrT)) then
1212 Displace_Allocator_Pointer (N);
1215 elsif Aggr_In_Place then
1216 Temp := Make_Temporary (Loc, 'P', N);
1218 Make_Object_Declaration (Loc,
1219 Defining_Identifier => Temp,
1220 Object_Definition => New_Reference_To (PtrT, Loc),
1222 Make_Allocator (Loc,
1223 Expression => New_Reference_To (Etype (Exp), Loc)));
1225 -- Copy the Comes_From_Source flag for the allocator we just built,
1226 -- since logically this allocator is a replacement of the original
1227 -- allocator node. This is for proper handling of restriction
1228 -- No_Implicit_Heap_Allocations.
1230 Set_Comes_From_Source
1231 (Expression (Temp_Decl), Comes_From_Source (N));
1233 Set_No_Initialization (Expression (Temp_Decl));
1234 Insert_Action (N, Temp_Decl);
1236 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1237 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1239 -- Attach the object to the associated finalization master. Thisis
1240 -- done manually on .NET/JVM since those compilers do no support
1241 -- pools and cannot benefit from internally generated Allocate and
1242 -- Deallocate procedures.
1244 if VM_Target /= No_VM
1245 and then Is_Controlled (DesigT)
1246 and then Present (Finalization_Master (PtrT))
1250 (Obj_Ref => New_Reference_To (Temp, Loc),
1254 Rewrite (N, New_Reference_To (Temp, Loc));
1255 Analyze_And_Resolve (N, PtrT);
1257 elsif Is_Access_Type (T)
1258 and then Can_Never_Be_Null (T)
1260 Install_Null_Excluding_Check (Exp);
1262 elsif Is_Access_Type (DesigT)
1263 and then Nkind (Exp) = N_Allocator
1264 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1266 -- Apply constraint to designated subtype indication
1268 Apply_Constraint_Check (Expression (Exp),
1269 Designated_Type (DesigT),
1270 No_Sliding => True);
1272 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1274 -- Propagate constraint_error to enclosing allocator
1276 Rewrite (Exp, New_Copy (Expression (Exp)));
1280 Build_Allocate_Deallocate_Proc (N, True);
1283 -- type A is access T1;
1284 -- X : A := new T2'(...);
1285 -- T1 and T2 can be different subtypes, and we might need to check
1286 -- both constraints. First check against the type of the qualified
1289 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1291 if Do_Range_Check (Exp) then
1292 Set_Do_Range_Check (Exp, False);
1293 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1296 -- A check is also needed in cases where the designated subtype is
1297 -- constrained and differs from the subtype given in the qualified
1298 -- expression. Note that the check on the qualified expression does
1299 -- not allow sliding, but this check does (a relaxation from Ada 83).
1301 if Is_Constrained (DesigT)
1302 and then not Subtypes_Statically_Match (T, DesigT)
1304 Apply_Constraint_Check
1305 (Exp, DesigT, No_Sliding => False);
1307 if Do_Range_Check (Exp) then
1308 Set_Do_Range_Check (Exp, False);
1309 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1313 -- For an access to unconstrained packed array, GIGI needs to see an
1314 -- expression with a constrained subtype in order to compute the
1315 -- proper size for the allocator.
1317 if Is_Array_Type (T)
1318 and then not Is_Constrained (T)
1319 and then Is_Packed (T)
1322 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1323 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1326 Make_Subtype_Declaration (Loc,
1327 Defining_Identifier => ConstrT,
1328 Subtype_Indication =>
1329 Make_Subtype_From_Expr (Internal_Exp, T)));
1330 Freeze_Itype (ConstrT, Exp);
1331 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1335 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1336 -- to a build-in-place function, then access to the allocated object
1337 -- must be passed to the function. Currently we limit such functions
1338 -- to those with constrained limited result subtypes, but eventually
1339 -- we plan to expand the allowed forms of functions that are treated
1340 -- as build-in-place.
1342 if Ada_Version >= Ada_2005
1343 and then Is_Build_In_Place_Function_Call (Exp)
1345 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1350 when RE_Not_Available =>
1352 end Expand_Allocator_Expression;
1354 -----------------------------
1355 -- Expand_Array_Comparison --
1356 -----------------------------
1358 -- Expansion is only required in the case of array types. For the unpacked
1359 -- case, an appropriate runtime routine is called. For packed cases, and
1360 -- also in some other cases where a runtime routine cannot be called, the
1361 -- form of the expansion is:
1363 -- [body for greater_nn; boolean_expression]
1365 -- The body is built by Make_Array_Comparison_Op, and the form of the
1366 -- Boolean expression depends on the operator involved.
1368 procedure Expand_Array_Comparison (N : Node_Id) is
1369 Loc : constant Source_Ptr := Sloc (N);
1370 Op1 : Node_Id := Left_Opnd (N);
1371 Op2 : Node_Id := Right_Opnd (N);
1372 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1373 Ctyp : constant Entity_Id := Component_Type (Typ1);
1376 Func_Body : Node_Id;
1377 Func_Name : Entity_Id;
1381 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1382 -- True for byte addressable target
1384 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1385 -- Returns True if the length of the given operand is known to be less
1386 -- than 4. Returns False if this length is known to be four or greater
1387 -- or is not known at compile time.
1389 ------------------------
1390 -- Length_Less_Than_4 --
1391 ------------------------
1393 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1394 Otyp : constant Entity_Id := Etype (Opnd);
1397 if Ekind (Otyp) = E_String_Literal_Subtype then
1398 return String_Literal_Length (Otyp) < 4;
1402 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1403 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1404 Hi : constant Node_Id := Type_High_Bound (Ityp);
1409 if Compile_Time_Known_Value (Lo) then
1410 Lov := Expr_Value (Lo);
1415 if Compile_Time_Known_Value (Hi) then
1416 Hiv := Expr_Value (Hi);
1421 return Hiv < Lov + 3;
1424 end Length_Less_Than_4;
1426 -- Start of processing for Expand_Array_Comparison
1429 -- Deal first with unpacked case, where we can call a runtime routine
1430 -- except that we avoid this for targets for which are not addressable
1431 -- by bytes, and for the JVM/CIL, since they do not support direct
1432 -- addressing of array components.
1434 if not Is_Bit_Packed_Array (Typ1)
1435 and then Byte_Addressable
1436 and then VM_Target = No_VM
1438 -- The call we generate is:
1440 -- Compare_Array_xn[_Unaligned]
1441 -- (left'address, right'address, left'length, right'length) <op> 0
1443 -- x = U for unsigned, S for signed
1444 -- n = 8,16,32,64 for component size
1445 -- Add _Unaligned if length < 4 and component size is 8.
1446 -- <op> is the standard comparison operator
1448 if Component_Size (Typ1) = 8 then
1449 if Length_Less_Than_4 (Op1)
1451 Length_Less_Than_4 (Op2)
1453 if Is_Unsigned_Type (Ctyp) then
1454 Comp := RE_Compare_Array_U8_Unaligned;
1456 Comp := RE_Compare_Array_S8_Unaligned;
1460 if Is_Unsigned_Type (Ctyp) then
1461 Comp := RE_Compare_Array_U8;
1463 Comp := RE_Compare_Array_S8;
1467 elsif Component_Size (Typ1) = 16 then
1468 if Is_Unsigned_Type (Ctyp) then
1469 Comp := RE_Compare_Array_U16;
1471 Comp := RE_Compare_Array_S16;
1474 elsif Component_Size (Typ1) = 32 then
1475 if Is_Unsigned_Type (Ctyp) then
1476 Comp := RE_Compare_Array_U32;
1478 Comp := RE_Compare_Array_S32;
1481 else pragma Assert (Component_Size (Typ1) = 64);
1482 if Is_Unsigned_Type (Ctyp) then
1483 Comp := RE_Compare_Array_U64;
1485 Comp := RE_Compare_Array_S64;
1489 Remove_Side_Effects (Op1, Name_Req => True);
1490 Remove_Side_Effects (Op2, Name_Req => True);
1493 Make_Function_Call (Sloc (Op1),
1494 Name => New_Occurrence_Of (RTE (Comp), Loc),
1496 Parameter_Associations => New_List (
1497 Make_Attribute_Reference (Loc,
1498 Prefix => Relocate_Node (Op1),
1499 Attribute_Name => Name_Address),
1501 Make_Attribute_Reference (Loc,
1502 Prefix => Relocate_Node (Op2),
1503 Attribute_Name => Name_Address),
1505 Make_Attribute_Reference (Loc,
1506 Prefix => Relocate_Node (Op1),
1507 Attribute_Name => Name_Length),
1509 Make_Attribute_Reference (Loc,
1510 Prefix => Relocate_Node (Op2),
1511 Attribute_Name => Name_Length))));
1514 Make_Integer_Literal (Sloc (Op2),
1517 Analyze_And_Resolve (Op1, Standard_Integer);
1518 Analyze_And_Resolve (Op2, Standard_Integer);
1522 -- Cases where we cannot make runtime call
1524 -- For (a <= b) we convert to not (a > b)
1526 if Chars (N) = Name_Op_Le then
1532 Right_Opnd => Op2)));
1533 Analyze_And_Resolve (N, Standard_Boolean);
1536 -- For < the Boolean expression is
1537 -- greater__nn (op2, op1)
1539 elsif Chars (N) = Name_Op_Lt then
1540 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1544 Op1 := Right_Opnd (N);
1545 Op2 := Left_Opnd (N);
1547 -- For (a >= b) we convert to not (a < b)
1549 elsif Chars (N) = Name_Op_Ge then
1555 Right_Opnd => Op2)));
1556 Analyze_And_Resolve (N, Standard_Boolean);
1559 -- For > the Boolean expression is
1560 -- greater__nn (op1, op2)
1563 pragma Assert (Chars (N) = Name_Op_Gt);
1564 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1567 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1569 Make_Function_Call (Loc,
1570 Name => New_Reference_To (Func_Name, Loc),
1571 Parameter_Associations => New_List (Op1, Op2));
1573 Insert_Action (N, Func_Body);
1575 Analyze_And_Resolve (N, Standard_Boolean);
1578 when RE_Not_Available =>
1580 end Expand_Array_Comparison;
1582 ---------------------------
1583 -- Expand_Array_Equality --
1584 ---------------------------
1586 -- Expand an equality function for multi-dimensional arrays. Here is an
1587 -- example of such a function for Nb_Dimension = 2
1589 -- function Enn (A : atyp; B : btyp) return boolean is
1591 -- if (A'length (1) = 0 or else A'length (2) = 0)
1593 -- (B'length (1) = 0 or else B'length (2) = 0)
1595 -- return True; -- RM 4.5.2(22)
1598 -- if A'length (1) /= B'length (1)
1600 -- A'length (2) /= B'length (2)
1602 -- return False; -- RM 4.5.2(23)
1606 -- A1 : Index_T1 := A'first (1);
1607 -- B1 : Index_T1 := B'first (1);
1611 -- A2 : Index_T2 := A'first (2);
1612 -- B2 : Index_T2 := B'first (2);
1615 -- if A (A1, A2) /= B (B1, B2) then
1619 -- exit when A2 = A'last (2);
1620 -- A2 := Index_T2'succ (A2);
1621 -- B2 := Index_T2'succ (B2);
1625 -- exit when A1 = A'last (1);
1626 -- A1 := Index_T1'succ (A1);
1627 -- B1 := Index_T1'succ (B1);
1634 -- Note on the formal types used (atyp and btyp). If either of the arrays
1635 -- is of a private type, we use the underlying type, and do an unchecked
1636 -- conversion of the actual. If either of the arrays has a bound depending
1637 -- on a discriminant, then we use the base type since otherwise we have an
1638 -- escaped discriminant in the function.
1640 -- If both arrays are constrained and have the same bounds, we can generate
1641 -- a loop with an explicit iteration scheme using a 'Range attribute over
1644 function Expand_Array_Equality
1649 Typ : Entity_Id) return Node_Id
1651 Loc : constant Source_Ptr := Sloc (Nod);
1652 Decls : constant List_Id := New_List;
1653 Index_List1 : constant List_Id := New_List;
1654 Index_List2 : constant List_Id := New_List;
1658 Func_Name : Entity_Id;
1659 Func_Body : Node_Id;
1661 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1662 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1666 -- The parameter types to be used for the formals
1671 Num : Int) return Node_Id;
1672 -- This builds the attribute reference Arr'Nam (Expr)
1674 function Component_Equality (Typ : Entity_Id) return Node_Id;
1675 -- Create one statement to compare corresponding components, designated
1676 -- by a full set of indexes.
1678 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1679 -- Given one of the arguments, computes the appropriate type to be used
1680 -- for that argument in the corresponding function formal
1682 function Handle_One_Dimension
1684 Index : Node_Id) return Node_Id;
1685 -- This procedure returns the following code
1688 -- Bn : Index_T := B'First (N);
1692 -- exit when An = A'Last (N);
1693 -- An := Index_T'Succ (An)
1694 -- Bn := Index_T'Succ (Bn)
1698 -- If both indexes are constrained and identical, the procedure
1699 -- returns a simpler loop:
1701 -- for An in A'Range (N) loop
1705 -- N is the dimension for which we are generating a loop. Index is the
1706 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1707 -- xxx statement is either the loop or declare for the next dimension
1708 -- or if this is the last dimension the comparison of corresponding
1709 -- components of the arrays.
1711 -- The actual way the code works is to return the comparison of
1712 -- corresponding components for the N+1 call. That's neater!
1714 function Test_Empty_Arrays return Node_Id;
1715 -- This function constructs the test for both arrays being empty
1716 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1718 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1720 function Test_Lengths_Correspond return Node_Id;
1721 -- This function constructs the test for arrays having different lengths
1722 -- in at least one index position, in which case the resulting code is:
1724 -- A'length (1) /= B'length (1)
1726 -- A'length (2) /= B'length (2)
1737 Num : Int) return Node_Id
1741 Make_Attribute_Reference (Loc,
1742 Attribute_Name => Nam,
1743 Prefix => New_Reference_To (Arr, Loc),
1744 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1747 ------------------------
1748 -- Component_Equality --
1749 ------------------------
1751 function Component_Equality (Typ : Entity_Id) return Node_Id is
1756 -- if a(i1...) /= b(j1...) then return false; end if;
1759 Make_Indexed_Component (Loc,
1760 Prefix => Make_Identifier (Loc, Chars (A)),
1761 Expressions => Index_List1);
1764 Make_Indexed_Component (Loc,
1765 Prefix => Make_Identifier (Loc, Chars (B)),
1766 Expressions => Index_List2);
1768 Test := Expand_Composite_Equality
1769 (Nod, Component_Type (Typ), L, R, Decls);
1771 -- If some (sub)component is an unchecked_union, the whole operation
1772 -- will raise program error.
1774 if Nkind (Test) = N_Raise_Program_Error then
1776 -- This node is going to be inserted at a location where a
1777 -- statement is expected: clear its Etype so analysis will set
1778 -- it to the expected Standard_Void_Type.
1780 Set_Etype (Test, Empty);
1785 Make_Implicit_If_Statement (Nod,
1786 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1787 Then_Statements => New_List (
1788 Make_Simple_Return_Statement (Loc,
1789 Expression => New_Occurrence_Of (Standard_False, Loc))));
1791 end Component_Equality;
1797 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1808 T := Underlying_Type (T);
1810 X := First_Index (T);
1811 while Present (X) loop
1812 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1814 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1827 --------------------------
1828 -- Handle_One_Dimension --
1829 ---------------------------
1831 function Handle_One_Dimension
1833 Index : Node_Id) return Node_Id
1835 Need_Separate_Indexes : constant Boolean :=
1837 or else not Is_Constrained (Ltyp);
1838 -- If the index types are identical, and we are working with
1839 -- constrained types, then we can use the same index for both
1842 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1845 Index_T : Entity_Id;
1850 if N > Number_Dimensions (Ltyp) then
1851 return Component_Equality (Ltyp);
1854 -- Case where we generate a loop
1856 Index_T := Base_Type (Etype (Index));
1858 if Need_Separate_Indexes then
1859 Bn := Make_Temporary (Loc, 'B');
1864 Append (New_Reference_To (An, Loc), Index_List1);
1865 Append (New_Reference_To (Bn, Loc), Index_List2);
1867 Stm_List := New_List (
1868 Handle_One_Dimension (N + 1, Next_Index (Index)));
1870 if Need_Separate_Indexes then
1872 -- Generate guard for loop, followed by increments of indexes
1874 Append_To (Stm_List,
1875 Make_Exit_Statement (Loc,
1878 Left_Opnd => New_Reference_To (An, Loc),
1879 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1881 Append_To (Stm_List,
1882 Make_Assignment_Statement (Loc,
1883 Name => New_Reference_To (An, Loc),
1885 Make_Attribute_Reference (Loc,
1886 Prefix => New_Reference_To (Index_T, Loc),
1887 Attribute_Name => Name_Succ,
1888 Expressions => New_List (New_Reference_To (An, Loc)))));
1890 Append_To (Stm_List,
1891 Make_Assignment_Statement (Loc,
1892 Name => New_Reference_To (Bn, Loc),
1894 Make_Attribute_Reference (Loc,
1895 Prefix => New_Reference_To (Index_T, Loc),
1896 Attribute_Name => Name_Succ,
1897 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1900 -- If separate indexes, we need a declare block for An and Bn, and a
1901 -- loop without an iteration scheme.
1903 if Need_Separate_Indexes then
1905 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1908 Make_Block_Statement (Loc,
1909 Declarations => New_List (
1910 Make_Object_Declaration (Loc,
1911 Defining_Identifier => An,
1912 Object_Definition => New_Reference_To (Index_T, Loc),
1913 Expression => Arr_Attr (A, Name_First, N)),
1915 Make_Object_Declaration (Loc,
1916 Defining_Identifier => Bn,
1917 Object_Definition => New_Reference_To (Index_T, Loc),
1918 Expression => Arr_Attr (B, Name_First, N))),
1920 Handled_Statement_Sequence =>
1921 Make_Handled_Sequence_Of_Statements (Loc,
1922 Statements => New_List (Loop_Stm)));
1924 -- If no separate indexes, return loop statement with explicit
1925 -- iteration scheme on its own
1929 Make_Implicit_Loop_Statement (Nod,
1930 Statements => Stm_List,
1932 Make_Iteration_Scheme (Loc,
1933 Loop_Parameter_Specification =>
1934 Make_Loop_Parameter_Specification (Loc,
1935 Defining_Identifier => An,
1936 Discrete_Subtype_Definition =>
1937 Arr_Attr (A, Name_Range, N))));
1940 end Handle_One_Dimension;
1942 -----------------------
1943 -- Test_Empty_Arrays --
1944 -----------------------
1946 function Test_Empty_Arrays return Node_Id is
1956 for J in 1 .. Number_Dimensions (Ltyp) loop
1959 Left_Opnd => Arr_Attr (A, Name_Length, J),
1960 Right_Opnd => Make_Integer_Literal (Loc, 0));
1964 Left_Opnd => Arr_Attr (B, Name_Length, J),
1965 Right_Opnd => Make_Integer_Literal (Loc, 0));
1974 Left_Opnd => Relocate_Node (Alist),
1975 Right_Opnd => Atest);
1979 Left_Opnd => Relocate_Node (Blist),
1980 Right_Opnd => Btest);
1987 Right_Opnd => Blist);
1988 end Test_Empty_Arrays;
1990 -----------------------------
1991 -- Test_Lengths_Correspond --
1992 -----------------------------
1994 function Test_Lengths_Correspond return Node_Id is
2000 for J in 1 .. Number_Dimensions (Ltyp) loop
2003 Left_Opnd => Arr_Attr (A, Name_Length, J),
2004 Right_Opnd => Arr_Attr (B, Name_Length, J));
2011 Left_Opnd => Relocate_Node (Result),
2012 Right_Opnd => Rtest);
2017 end Test_Lengths_Correspond;
2019 -- Start of processing for Expand_Array_Equality
2022 Ltyp := Get_Arg_Type (Lhs);
2023 Rtyp := Get_Arg_Type (Rhs);
2025 -- For now, if the argument types are not the same, go to the base type,
2026 -- since the code assumes that the formals have the same type. This is
2027 -- fixable in future ???
2029 if Ltyp /= Rtyp then
2030 Ltyp := Base_Type (Ltyp);
2031 Rtyp := Base_Type (Rtyp);
2032 pragma Assert (Ltyp = Rtyp);
2035 -- Build list of formals for function
2037 Formals := New_List (
2038 Make_Parameter_Specification (Loc,
2039 Defining_Identifier => A,
2040 Parameter_Type => New_Reference_To (Ltyp, Loc)),
2042 Make_Parameter_Specification (Loc,
2043 Defining_Identifier => B,
2044 Parameter_Type => New_Reference_To (Rtyp, Loc)));
2046 Func_Name := Make_Temporary (Loc, 'E');
2048 -- Build statement sequence for function
2051 Make_Subprogram_Body (Loc,
2053 Make_Function_Specification (Loc,
2054 Defining_Unit_Name => Func_Name,
2055 Parameter_Specifications => Formals,
2056 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
2058 Declarations => Decls,
2060 Handled_Statement_Sequence =>
2061 Make_Handled_Sequence_Of_Statements (Loc,
2062 Statements => New_List (
2064 Make_Implicit_If_Statement (Nod,
2065 Condition => Test_Empty_Arrays,
2066 Then_Statements => New_List (
2067 Make_Simple_Return_Statement (Loc,
2069 New_Occurrence_Of (Standard_True, Loc)))),
2071 Make_Implicit_If_Statement (Nod,
2072 Condition => Test_Lengths_Correspond,
2073 Then_Statements => New_List (
2074 Make_Simple_Return_Statement (Loc,
2076 New_Occurrence_Of (Standard_False, Loc)))),
2078 Handle_One_Dimension (1, First_Index (Ltyp)),
2080 Make_Simple_Return_Statement (Loc,
2081 Expression => New_Occurrence_Of (Standard_True, Loc)))));
2083 Set_Has_Completion (Func_Name, True);
2084 Set_Is_Inlined (Func_Name);
2086 -- If the array type is distinct from the type of the arguments, it
2087 -- is the full view of a private type. Apply an unchecked conversion
2088 -- to insure that analysis of the call succeeds.
2098 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
2100 L := OK_Convert_To (Ltyp, Lhs);
2104 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
2106 R := OK_Convert_To (Rtyp, Rhs);
2109 Actuals := New_List (L, R);
2112 Append_To (Bodies, Func_Body);
2115 Make_Function_Call (Loc,
2116 Name => New_Reference_To (Func_Name, Loc),
2117 Parameter_Associations => Actuals);
2118 end Expand_Array_Equality;
2120 -----------------------------
2121 -- Expand_Boolean_Operator --
2122 -----------------------------
2124 -- Note that we first get the actual subtypes of the operands, since we
2125 -- always want to deal with types that have bounds.
2127 procedure Expand_Boolean_Operator (N : Node_Id) is
2128 Typ : constant Entity_Id := Etype (N);
2131 -- Special case of bit packed array where both operands are known to be
2132 -- properly aligned. In this case we use an efficient run time routine
2133 -- to carry out the operation (see System.Bit_Ops).
2135 if Is_Bit_Packed_Array (Typ)
2136 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
2137 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
2139 Expand_Packed_Boolean_Operator (N);
2143 -- For the normal non-packed case, the general expansion is to build
2144 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2145 -- and then inserting it into the tree. The original operator node is
2146 -- then rewritten as a call to this function. We also use this in the
2147 -- packed case if either operand is a possibly unaligned object.
2150 Loc : constant Source_Ptr := Sloc (N);
2151 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2152 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2153 Func_Body : Node_Id;
2154 Func_Name : Entity_Id;
2157 Convert_To_Actual_Subtype (L);
2158 Convert_To_Actual_Subtype (R);
2159 Ensure_Defined (Etype (L), N);
2160 Ensure_Defined (Etype (R), N);
2161 Apply_Length_Check (R, Etype (L));
2163 if Nkind (N) = N_Op_Xor then
2164 Silly_Boolean_Array_Xor_Test (N, Etype (L));
2167 if Nkind (Parent (N)) = N_Assignment_Statement
2168 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
2170 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
2172 elsif Nkind (Parent (N)) = N_Op_Not
2173 and then Nkind (N) = N_Op_And
2175 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
2180 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
2181 Func_Name := Defining_Unit_Name (Specification (Func_Body));
2182 Insert_Action (N, Func_Body);
2184 -- Now rewrite the expression with a call
2187 Make_Function_Call (Loc,
2188 Name => New_Reference_To (Func_Name, Loc),
2189 Parameter_Associations =>
2192 Make_Type_Conversion
2193 (Loc, New_Reference_To (Etype (L), Loc), R))));
2195 Analyze_And_Resolve (N, Typ);
2198 end Expand_Boolean_Operator;
2200 -------------------------------
2201 -- Expand_Composite_Equality --
2202 -------------------------------
2204 -- This function is only called for comparing internal fields of composite
2205 -- types when these fields are themselves composites. This is a special
2206 -- case because it is not possible to respect normal Ada visibility rules.
2208 function Expand_Composite_Equality
2213 Bodies : List_Id) return Node_Id
2215 Loc : constant Source_Ptr := Sloc (Nod);
2216 Full_Type : Entity_Id;
2220 function Find_Primitive_Eq return Node_Id;
2221 -- AI05-0123: Locate primitive equality for type if it exists, and
2222 -- build the corresponding call. If operation is abstract, replace
2223 -- call with an explicit raise. Return Empty if there is no primitive.
2225 -----------------------
2226 -- Find_Primitive_Eq --
2227 -----------------------
2229 function Find_Primitive_Eq return Node_Id is
2234 Prim_E := First_Elmt (Collect_Primitive_Operations (Typ));
2235 while Present (Prim_E) loop
2236 Prim := Node (Prim_E);
2238 -- Locate primitive equality with the right signature
2240 if Chars (Prim) = Name_Op_Eq
2241 and then Etype (First_Formal (Prim)) =
2242 Etype (Next_Formal (First_Formal (Prim)))
2243 and then Etype (Prim) = Standard_Boolean
2245 if Is_Abstract_Subprogram (Prim) then
2247 Make_Raise_Program_Error (Loc,
2248 Reason => PE_Explicit_Raise);
2252 Make_Function_Call (Loc,
2253 Name => New_Reference_To (Prim, Loc),
2254 Parameter_Associations => New_List (Lhs, Rhs));
2261 -- If not found, predefined operation will be used
2264 end Find_Primitive_Eq;
2266 -- Start of processing for Expand_Composite_Equality
2269 if Is_Private_Type (Typ) then
2270 Full_Type := Underlying_Type (Typ);
2275 -- Defense against malformed private types with no completion the error
2276 -- will be diagnosed later by check_completion
2278 if No (Full_Type) then
2279 return New_Reference_To (Standard_False, Loc);
2282 Full_Type := Base_Type (Full_Type);
2284 if Is_Array_Type (Full_Type) then
2286 -- If the operand is an elementary type other than a floating-point
2287 -- type, then we can simply use the built-in block bitwise equality,
2288 -- since the predefined equality operators always apply and bitwise
2289 -- equality is fine for all these cases.
2291 if Is_Elementary_Type (Component_Type (Full_Type))
2292 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2294 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2296 -- For composite component types, and floating-point types, use the
2297 -- expansion. This deals with tagged component types (where we use
2298 -- the applicable equality routine) and floating-point, (where we
2299 -- need to worry about negative zeroes), and also the case of any
2300 -- composite type recursively containing such fields.
2303 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2306 elsif Is_Tagged_Type (Full_Type) then
2308 -- Call the primitive operation "=" of this type
2310 if Is_Class_Wide_Type (Full_Type) then
2311 Full_Type := Root_Type (Full_Type);
2314 -- If this is derived from an untagged private type completed with a
2315 -- tagged type, it does not have a full view, so we use the primitive
2316 -- operations of the private type. This check should no longer be
2317 -- necessary when these types receive their full views ???
2319 if Is_Private_Type (Typ)
2320 and then not Is_Tagged_Type (Typ)
2321 and then not Is_Controlled (Typ)
2322 and then Is_Derived_Type (Typ)
2323 and then No (Full_View (Typ))
2325 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2327 Prim := First_Elmt (Primitive_Operations (Full_Type));
2331 Eq_Op := Node (Prim);
2332 exit when Chars (Eq_Op) = Name_Op_Eq
2333 and then Etype (First_Formal (Eq_Op)) =
2334 Etype (Next_Formal (First_Formal (Eq_Op)))
2335 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2337 pragma Assert (Present (Prim));
2340 Eq_Op := Node (Prim);
2343 Make_Function_Call (Loc,
2344 Name => New_Reference_To (Eq_Op, Loc),
2345 Parameter_Associations =>
2347 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2348 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2350 elsif Is_Record_Type (Full_Type) then
2351 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2353 if Present (Eq_Op) then
2354 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2356 -- Inherited equality from parent type. Convert the actuals to
2357 -- match signature of operation.
2360 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2364 Make_Function_Call (Loc,
2365 Name => New_Reference_To (Eq_Op, Loc),
2366 Parameter_Associations => New_List (
2367 OK_Convert_To (T, Lhs),
2368 OK_Convert_To (T, Rhs)));
2372 -- Comparison between Unchecked_Union components
2374 if Is_Unchecked_Union (Full_Type) then
2376 Lhs_Type : Node_Id := Full_Type;
2377 Rhs_Type : Node_Id := Full_Type;
2378 Lhs_Discr_Val : Node_Id;
2379 Rhs_Discr_Val : Node_Id;
2384 if Nkind (Lhs) = N_Selected_Component then
2385 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2390 if Nkind (Rhs) = N_Selected_Component then
2391 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2394 -- Lhs of the composite equality
2396 if Is_Constrained (Lhs_Type) then
2398 -- Since the enclosing record type can never be an
2399 -- Unchecked_Union (this code is executed for records
2400 -- that do not have variants), we may reference its
2403 if Nkind (Lhs) = N_Selected_Component
2404 and then Has_Per_Object_Constraint (
2405 Entity (Selector_Name (Lhs)))
2408 Make_Selected_Component (Loc,
2409 Prefix => Prefix (Lhs),
2412 (Get_Discriminant_Value
2413 (First_Discriminant (Lhs_Type),
2415 Stored_Constraint (Lhs_Type))));
2420 (Get_Discriminant_Value
2421 (First_Discriminant (Lhs_Type),
2423 Stored_Constraint (Lhs_Type)));
2427 -- It is not possible to infer the discriminant since
2428 -- the subtype is not constrained.
2431 Make_Raise_Program_Error (Loc,
2432 Reason => PE_Unchecked_Union_Restriction);
2435 -- Rhs of the composite equality
2437 if Is_Constrained (Rhs_Type) then
2438 if Nkind (Rhs) = N_Selected_Component
2439 and then Has_Per_Object_Constraint
2440 (Entity (Selector_Name (Rhs)))
2443 Make_Selected_Component (Loc,
2444 Prefix => Prefix (Rhs),
2447 (Get_Discriminant_Value
2448 (First_Discriminant (Rhs_Type),
2450 Stored_Constraint (Rhs_Type))));
2455 (Get_Discriminant_Value
2456 (First_Discriminant (Rhs_Type),
2458 Stored_Constraint (Rhs_Type)));
2463 Make_Raise_Program_Error (Loc,
2464 Reason => PE_Unchecked_Union_Restriction);
2467 -- Call the TSS equality function with the inferred
2468 -- discriminant values.
2471 Make_Function_Call (Loc,
2472 Name => New_Reference_To (Eq_Op, Loc),
2473 Parameter_Associations => New_List (
2482 Make_Function_Call (Loc,
2483 Name => New_Reference_To (Eq_Op, Loc),
2484 Parameter_Associations => New_List (Lhs, Rhs));
2488 elsif Ada_Version >= Ada_2012 then
2490 -- if no TSS has been created for the type, check whether there is
2491 -- a primitive equality declared for it.
2494 Ada_2012_Op : constant Node_Id := Find_Primitive_Eq;
2497 if Present (Ada_2012_Op) then
2501 -- Use predefined equality if no user-defined primitive exists
2503 return Make_Op_Eq (Loc, Lhs, Rhs);
2508 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2512 -- If not array or record type, it is predefined equality.
2514 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2516 end Expand_Composite_Equality;
2518 ------------------------
2519 -- Expand_Concatenate --
2520 ------------------------
2522 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2523 Loc : constant Source_Ptr := Sloc (Cnode);
2525 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2526 -- Result type of concatenation
2528 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2529 -- Component type. Elements of this component type can appear as one
2530 -- of the operands of concatenation as well as arrays.
2532 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2535 Ityp : constant Entity_Id := Base_Type (Istyp);
2536 -- Index type. This is the base type of the index subtype, and is used
2537 -- for all computed bounds (which may be out of range of Istyp in the
2538 -- case of null ranges).
2541 -- This is the type we use to do arithmetic to compute the bounds and
2542 -- lengths of operands. The choice of this type is a little subtle and
2543 -- is discussed in a separate section at the start of the body code.
2545 Concatenation_Error : exception;
2546 -- Raised if concatenation is sure to raise a CE
2548 Result_May_Be_Null : Boolean := True;
2549 -- Reset to False if at least one operand is encountered which is known
2550 -- at compile time to be non-null. Used for handling the special case
2551 -- of setting the high bound to the last operand high bound for a null
2552 -- result, thus ensuring a proper high bound in the super-flat case.
2554 N : constant Nat := List_Length (Opnds);
2555 -- Number of concatenation operands including possibly null operands
2558 -- Number of operands excluding any known to be null, except that the
2559 -- last operand is always retained, in case it provides the bounds for
2563 -- Current operand being processed in the loop through operands. After
2564 -- this loop is complete, always contains the last operand (which is not
2565 -- the same as Operands (NN), since null operands are skipped).
2567 -- Arrays describing the operands, only the first NN entries of each
2568 -- array are set (NN < N when we exclude known null operands).
2570 Is_Fixed_Length : array (1 .. N) of Boolean;
2571 -- True if length of corresponding operand known at compile time
2573 Operands : array (1 .. N) of Node_Id;
2574 -- Set to the corresponding entry in the Opnds list (but note that null
2575 -- operands are excluded, so not all entries in the list are stored).
2577 Fixed_Length : array (1 .. N) of Uint;
2578 -- Set to length of operand. Entries in this array are set only if the
2579 -- corresponding entry in Is_Fixed_Length is True.
2581 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2582 -- Set to lower bound of operand. Either an integer literal in the case
2583 -- where the bound is known at compile time, else actual lower bound.
2584 -- The operand low bound is of type Ityp.
2586 Var_Length : array (1 .. N) of Entity_Id;
2587 -- Set to an entity of type Natural that contains the length of an
2588 -- operand whose length is not known at compile time. Entries in this
2589 -- array are set only if the corresponding entry in Is_Fixed_Length
2590 -- is False. The entity is of type Artyp.
2592 Aggr_Length : array (0 .. N) of Node_Id;
2593 -- The J'th entry in an expression node that represents the total length
2594 -- of operands 1 through J. It is either an integer literal node, or a
2595 -- reference to a constant entity with the right value, so it is fine
2596 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2597 -- entry always is set to zero. The length is of type Artyp.
2599 Low_Bound : Node_Id;
2600 -- A tree node representing the low bound of the result (of type Ityp).
2601 -- This is either an integer literal node, or an identifier reference to
2602 -- a constant entity initialized to the appropriate value.
2604 Last_Opnd_Low_Bound : Node_Id;
2605 -- A tree node representing the low bound of the last operand. This
2606 -- need only be set if the result could be null. It is used for the
2607 -- special case of setting the right low bound for a null result.
2608 -- This is of type Ityp.
2610 Last_Opnd_High_Bound : Node_Id;
2611 -- A tree node representing the high bound of the last operand. This
2612 -- need only be set if the result could be null. It is used for the
2613 -- special case of setting the right high bound for a null result.
2614 -- This is of type Ityp.
2616 High_Bound : Node_Id;
2617 -- A tree node representing the high bound of the result (of type Ityp)
2620 -- Result of the concatenation (of type Ityp)
2622 Actions : constant List_Id := New_List;
2623 -- Collect actions to be inserted
2625 Known_Non_Null_Operand_Seen : Boolean;
2626 -- Set True during generation of the assignments of operands into
2627 -- result once an operand known to be non-null has been seen.
2629 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2630 -- This function makes an N_Integer_Literal node that is returned in
2631 -- analyzed form with the type set to Artyp. Importantly this literal
2632 -- is not flagged as static, so that if we do computations with it that
2633 -- result in statically detected out of range conditions, we will not
2634 -- generate error messages but instead warning messages.
2636 function To_Artyp (X : Node_Id) return Node_Id;
2637 -- Given a node of type Ityp, returns the corresponding value of type
2638 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2639 -- For enum types, the Pos of the value is returned.
2641 function To_Ityp (X : Node_Id) return Node_Id;
2642 -- The inverse function (uses Val in the case of enumeration types)
2644 ------------------------
2645 -- Make_Artyp_Literal --
2646 ------------------------
2648 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2649 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2651 Set_Etype (Result, Artyp);
2652 Set_Analyzed (Result, True);
2653 Set_Is_Static_Expression (Result, False);
2655 end Make_Artyp_Literal;
2661 function To_Artyp (X : Node_Id) return Node_Id is
2663 if Ityp = Base_Type (Artyp) then
2666 elsif Is_Enumeration_Type (Ityp) then
2668 Make_Attribute_Reference (Loc,
2669 Prefix => New_Occurrence_Of (Ityp, Loc),
2670 Attribute_Name => Name_Pos,
2671 Expressions => New_List (X));
2674 return Convert_To (Artyp, X);
2682 function To_Ityp (X : Node_Id) return Node_Id is
2684 if Is_Enumeration_Type (Ityp) then
2686 Make_Attribute_Reference (Loc,
2687 Prefix => New_Occurrence_Of (Ityp, Loc),
2688 Attribute_Name => Name_Val,
2689 Expressions => New_List (X));
2691 -- Case where we will do a type conversion
2694 if Ityp = Base_Type (Artyp) then
2697 return Convert_To (Ityp, X);
2702 -- Local Declarations
2704 Opnd_Typ : Entity_Id;
2711 -- Start of processing for Expand_Concatenate
2714 -- Choose an appropriate computational type
2716 -- We will be doing calculations of lengths and bounds in this routine
2717 -- and computing one from the other in some cases, e.g. getting the high
2718 -- bound by adding the length-1 to the low bound.
2720 -- We can't just use the index type, or even its base type for this
2721 -- purpose for two reasons. First it might be an enumeration type which
2722 -- is not suitable for computations of any kind, and second it may
2723 -- simply not have enough range. For example if the index type is
2724 -- -128..+127 then lengths can be up to 256, which is out of range of
2727 -- For enumeration types, we can simply use Standard_Integer, this is
2728 -- sufficient since the actual number of enumeration literals cannot
2729 -- possibly exceed the range of integer (remember we will be doing the
2730 -- arithmetic with POS values, not representation values).
2732 if Is_Enumeration_Type (Ityp) then
2733 Artyp := Standard_Integer;
2735 -- If index type is Positive, we use the standard unsigned type, to give
2736 -- more room on the top of the range, obviating the need for an overflow
2737 -- check when creating the upper bound. This is needed to avoid junk
2738 -- overflow checks in the common case of String types.
2740 -- ??? Disabled for now
2742 -- elsif Istyp = Standard_Positive then
2743 -- Artyp := Standard_Unsigned;
2745 -- For modular types, we use a 32-bit modular type for types whose size
2746 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2747 -- identity type, and for larger unsigned types we use 64-bits.
2749 elsif Is_Modular_Integer_Type (Ityp) then
2750 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
2751 Artyp := Standard_Unsigned;
2752 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
2755 Artyp := RTE (RE_Long_Long_Unsigned);
2758 -- Similar treatment for signed types
2761 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
2762 Artyp := Standard_Integer;
2763 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
2766 Artyp := Standard_Long_Long_Integer;
2770 -- Supply dummy entry at start of length array
2772 Aggr_Length (0) := Make_Artyp_Literal (0);
2774 -- Go through operands setting up the above arrays
2778 Opnd := Remove_Head (Opnds);
2779 Opnd_Typ := Etype (Opnd);
2781 -- The parent got messed up when we put the operands in a list,
2782 -- so now put back the proper parent for the saved operand, that
2783 -- is to say the concatenation node, to make sure that each operand
2784 -- is seen as a subexpression, e.g. if actions must be inserted.
2786 Set_Parent (Opnd, Cnode);
2788 -- Set will be True when we have setup one entry in the array
2792 -- Singleton element (or character literal) case
2794 if Base_Type (Opnd_Typ) = Ctyp then
2796 Operands (NN) := Opnd;
2797 Is_Fixed_Length (NN) := True;
2798 Fixed_Length (NN) := Uint_1;
2799 Result_May_Be_Null := False;
2801 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2802 -- since we know that the result cannot be null).
2804 Opnd_Low_Bound (NN) :=
2805 Make_Attribute_Reference (Loc,
2806 Prefix => New_Reference_To (Istyp, Loc),
2807 Attribute_Name => Name_First);
2811 -- String literal case (can only occur for strings of course)
2813 elsif Nkind (Opnd) = N_String_Literal then
2814 Len := String_Literal_Length (Opnd_Typ);
2817 Result_May_Be_Null := False;
2820 -- Capture last operand low and high bound if result could be null
2822 if J = N and then Result_May_Be_Null then
2823 Last_Opnd_Low_Bound :=
2824 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
2826 Last_Opnd_High_Bound :=
2827 Make_Op_Subtract (Loc,
2829 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
2830 Right_Opnd => Make_Integer_Literal (Loc, 1));
2833 -- Skip null string literal
2835 if J < N and then Len = 0 then
2840 Operands (NN) := Opnd;
2841 Is_Fixed_Length (NN) := True;
2843 -- Set length and bounds
2845 Fixed_Length (NN) := Len;
2847 Opnd_Low_Bound (NN) :=
2848 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
2855 -- Check constrained case with known bounds
2857 if Is_Constrained (Opnd_Typ) then
2859 Index : constant Node_Id := First_Index (Opnd_Typ);
2860 Indx_Typ : constant Entity_Id := Etype (Index);
2861 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
2862 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
2865 -- Fixed length constrained array type with known at compile
2866 -- time bounds is last case of fixed length operand.
2868 if Compile_Time_Known_Value (Lo)
2870 Compile_Time_Known_Value (Hi)
2873 Loval : constant Uint := Expr_Value (Lo);
2874 Hival : constant Uint := Expr_Value (Hi);
2875 Len : constant Uint :=
2876 UI_Max (Hival - Loval + 1, Uint_0);
2880 Result_May_Be_Null := False;
2883 -- Capture last operand bounds if result could be null
2885 if J = N and then Result_May_Be_Null then
2886 Last_Opnd_Low_Bound :=
2888 Make_Integer_Literal (Loc, Expr_Value (Lo)));
2890 Last_Opnd_High_Bound :=
2892 Make_Integer_Literal (Loc, Expr_Value (Hi)));
2895 -- Exclude null length case unless last operand
2897 if J < N and then Len = 0 then
2902 Operands (NN) := Opnd;
2903 Is_Fixed_Length (NN) := True;
2904 Fixed_Length (NN) := Len;
2906 Opnd_Low_Bound (NN) :=
2908 (Make_Integer_Literal (Loc, Expr_Value (Lo)));
2915 -- All cases where the length is not known at compile time, or the
2916 -- special case of an operand which is known to be null but has a
2917 -- lower bound other than 1 or is other than a string type.
2922 -- Capture operand bounds
2924 Opnd_Low_Bound (NN) :=
2925 Make_Attribute_Reference (Loc,
2927 Duplicate_Subexpr (Opnd, Name_Req => True),
2928 Attribute_Name => Name_First);
2930 -- Capture last operand bounds if result could be null
2932 if J = N and Result_May_Be_Null then
2933 Last_Opnd_Low_Bound :=
2935 Make_Attribute_Reference (Loc,
2937 Duplicate_Subexpr (Opnd, Name_Req => True),
2938 Attribute_Name => Name_First));
2940 Last_Opnd_High_Bound :=
2942 Make_Attribute_Reference (Loc,
2944 Duplicate_Subexpr (Opnd, Name_Req => True),
2945 Attribute_Name => Name_Last));
2948 -- Capture length of operand in entity
2950 Operands (NN) := Opnd;
2951 Is_Fixed_Length (NN) := False;
2953 Var_Length (NN) := Make_Temporary (Loc, 'L');
2956 Make_Object_Declaration (Loc,
2957 Defining_Identifier => Var_Length (NN),
2958 Constant_Present => True,
2959 Object_Definition => New_Occurrence_Of (Artyp, Loc),
2961 Make_Attribute_Reference (Loc,
2963 Duplicate_Subexpr (Opnd, Name_Req => True),
2964 Attribute_Name => Name_Length)));
2968 -- Set next entry in aggregate length array
2970 -- For first entry, make either integer literal for fixed length
2971 -- or a reference to the saved length for variable length.
2974 if Is_Fixed_Length (1) then
2975 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
2977 Aggr_Length (1) := New_Reference_To (Var_Length (1), Loc);
2980 -- If entry is fixed length and only fixed lengths so far, make
2981 -- appropriate new integer literal adding new length.
2983 elsif Is_Fixed_Length (NN)
2984 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
2987 Make_Integer_Literal (Loc,
2988 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
2990 -- All other cases, construct an addition node for the length and
2991 -- create an entity initialized to this length.
2994 Ent := Make_Temporary (Loc, 'L');
2996 if Is_Fixed_Length (NN) then
2997 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
2999 Clen := New_Reference_To (Var_Length (NN), Loc);
3003 Make_Object_Declaration (Loc,
3004 Defining_Identifier => Ent,
3005 Constant_Present => True,
3006 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3009 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
3010 Right_Opnd => Clen)));
3012 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
3019 -- If we have only skipped null operands, return the last operand
3026 -- If we have only one non-null operand, return it and we are done.
3027 -- There is one case in which this cannot be done, and that is when
3028 -- the sole operand is of the element type, in which case it must be
3029 -- converted to an array, and the easiest way of doing that is to go
3030 -- through the normal general circuit.
3033 and then Base_Type (Etype (Operands (1))) /= Ctyp
3035 Result := Operands (1);
3039 -- Cases where we have a real concatenation
3041 -- Next step is to find the low bound for the result array that we
3042 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3044 -- If the ultimate ancestor of the index subtype is a constrained array
3045 -- definition, then the lower bound is that of the index subtype as
3046 -- specified by (RM 4.5.3(6)).
3048 -- The right test here is to go to the root type, and then the ultimate
3049 -- ancestor is the first subtype of this root type.
3051 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
3053 Make_Attribute_Reference (Loc,
3055 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
3056 Attribute_Name => Name_First);
3058 -- If the first operand in the list has known length we know that
3059 -- the lower bound of the result is the lower bound of this operand.
3061 elsif Is_Fixed_Length (1) then
3062 Low_Bound := Opnd_Low_Bound (1);
3064 -- OK, we don't know the lower bound, we have to build a horrible
3065 -- expression actions node of the form
3067 -- if Cond1'Length /= 0 then
3070 -- if Opnd2'Length /= 0 then
3075 -- The nesting ends either when we hit an operand whose length is known
3076 -- at compile time, or on reaching the last operand, whose low bound we
3077 -- take unconditionally whether or not it is null. It's easiest to do
3078 -- this with a recursive procedure:
3082 function Get_Known_Bound (J : Nat) return Node_Id;
3083 -- Returns the lower bound determined by operands J .. NN
3085 ---------------------
3086 -- Get_Known_Bound --
3087 ---------------------
3089 function Get_Known_Bound (J : Nat) return Node_Id is
3091 if Is_Fixed_Length (J) or else J = NN then
3092 return New_Copy (Opnd_Low_Bound (J));
3096 Make_Conditional_Expression (Loc,
3097 Expressions => New_List (
3100 Left_Opnd => New_Reference_To (Var_Length (J), Loc),
3101 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3103 New_Copy (Opnd_Low_Bound (J)),
3104 Get_Known_Bound (J + 1)));
3106 end Get_Known_Bound;
3109 Ent := Make_Temporary (Loc, 'L');
3112 Make_Object_Declaration (Loc,
3113 Defining_Identifier => Ent,
3114 Constant_Present => True,
3115 Object_Definition => New_Occurrence_Of (Ityp, Loc),
3116 Expression => Get_Known_Bound (1)));
3118 Low_Bound := New_Reference_To (Ent, Loc);
3122 -- Now we can safely compute the upper bound, normally
3123 -- Low_Bound + Length - 1.
3128 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3130 Make_Op_Subtract (Loc,
3131 Left_Opnd => New_Copy (Aggr_Length (NN)),
3132 Right_Opnd => Make_Artyp_Literal (1))));
3134 -- Note that calculation of the high bound may cause overflow in some
3135 -- very weird cases, so in the general case we need an overflow check on
3136 -- the high bound. We can avoid this for the common case of string types
3137 -- and other types whose index is Positive, since we chose a wider range
3138 -- for the arithmetic type.
3140 if Istyp /= Standard_Positive then
3141 Activate_Overflow_Check (High_Bound);
3144 -- Handle the exceptional case where the result is null, in which case
3145 -- case the bounds come from the last operand (so that we get the proper
3146 -- bounds if the last operand is super-flat).
3148 if Result_May_Be_Null then
3150 Make_Conditional_Expression (Loc,
3151 Expressions => New_List (
3153 Left_Opnd => New_Copy (Aggr_Length (NN)),
3154 Right_Opnd => Make_Artyp_Literal (0)),
3155 Last_Opnd_Low_Bound,
3159 Make_Conditional_Expression (Loc,
3160 Expressions => New_List (
3162 Left_Opnd => New_Copy (Aggr_Length (NN)),
3163 Right_Opnd => Make_Artyp_Literal (0)),
3164 Last_Opnd_High_Bound,
3168 -- Here is where we insert the saved up actions
3170 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3172 -- Now we construct an array object with appropriate bounds. We mark
3173 -- the target as internal to prevent useless initialization when
3174 -- Initialize_Scalars is enabled. Also since this is the actual result
3175 -- entity, we make sure we have debug information for the result.
3177 Ent := Make_Temporary (Loc, 'S');
3178 Set_Is_Internal (Ent);
3179 Set_Needs_Debug_Info (Ent);
3181 -- If the bound is statically known to be out of range, we do not want
3182 -- to abort, we want a warning and a runtime constraint error. Note that
3183 -- we have arranged that the result will not be treated as a static
3184 -- constant, so we won't get an illegality during this insertion.
3186 Insert_Action (Cnode,
3187 Make_Object_Declaration (Loc,
3188 Defining_Identifier => Ent,
3189 Object_Definition =>
3190 Make_Subtype_Indication (Loc,
3191 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
3193 Make_Index_Or_Discriminant_Constraint (Loc,
3194 Constraints => New_List (
3196 Low_Bound => Low_Bound,
3197 High_Bound => High_Bound))))),
3198 Suppress => All_Checks);
3200 -- If the result of the concatenation appears as the initializing
3201 -- expression of an object declaration, we can just rename the
3202 -- result, rather than copying it.
3204 Set_OK_To_Rename (Ent);
3206 -- Catch the static out of range case now
3208 if Raises_Constraint_Error (High_Bound) then
3209 raise Concatenation_Error;
3212 -- Now we will generate the assignments to do the actual concatenation
3214 -- There is one case in which we will not do this, namely when all the
3215 -- following conditions are met:
3217 -- The result type is Standard.String
3219 -- There are nine or fewer retained (non-null) operands
3221 -- The optimization level is -O0
3223 -- The corresponding System.Concat_n.Str_Concat_n routine is
3224 -- available in the run time.
3226 -- The debug flag gnatd.c is not set
3228 -- If all these conditions are met then we generate a call to the
3229 -- relevant concatenation routine. The purpose of this is to avoid
3230 -- undesirable code bloat at -O0.
3232 if Atyp = Standard_String
3233 and then NN in 2 .. 9
3234 and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
3235 and then not Debug_Flag_Dot_C
3238 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3249 if RTE_Available (RR (NN)) then
3251 Opnds : constant List_Id :=
3252 New_List (New_Occurrence_Of (Ent, Loc));
3255 for J in 1 .. NN loop
3256 if Is_List_Member (Operands (J)) then
3257 Remove (Operands (J));
3260 if Base_Type (Etype (Operands (J))) = Ctyp then
3262 Make_Aggregate (Loc,
3263 Component_Associations => New_List (
3264 Make_Component_Association (Loc,
3265 Choices => New_List (
3266 Make_Integer_Literal (Loc, 1)),
3267 Expression => Operands (J)))));
3270 Append_To (Opnds, Operands (J));
3274 Insert_Action (Cnode,
3275 Make_Procedure_Call_Statement (Loc,
3276 Name => New_Reference_To (RTE (RR (NN)), Loc),
3277 Parameter_Associations => Opnds));
3279 Result := New_Reference_To (Ent, Loc);
3286 -- Not special case so generate the assignments
3288 Known_Non_Null_Operand_Seen := False;
3290 for J in 1 .. NN loop
3292 Lo : constant Node_Id :=
3294 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3295 Right_Opnd => Aggr_Length (J - 1));
3297 Hi : constant Node_Id :=
3299 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3301 Make_Op_Subtract (Loc,
3302 Left_Opnd => Aggr_Length (J),
3303 Right_Opnd => Make_Artyp_Literal (1)));
3306 -- Singleton case, simple assignment
3308 if Base_Type (Etype (Operands (J))) = Ctyp then
3309 Known_Non_Null_Operand_Seen := True;
3310 Insert_Action (Cnode,
3311 Make_Assignment_Statement (Loc,
3313 Make_Indexed_Component (Loc,
3314 Prefix => New_Occurrence_Of (Ent, Loc),
3315 Expressions => New_List (To_Ityp (Lo))),
3316 Expression => Operands (J)),
3317 Suppress => All_Checks);
3319 -- Array case, slice assignment, skipped when argument is fixed
3320 -- length and known to be null.
3322 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3325 Make_Assignment_Statement (Loc,
3329 New_Occurrence_Of (Ent, Loc),
3332 Low_Bound => To_Ityp (Lo),
3333 High_Bound => To_Ityp (Hi))),
3334 Expression => Operands (J));
3336 if Is_Fixed_Length (J) then
3337 Known_Non_Null_Operand_Seen := True;
3339 elsif not Known_Non_Null_Operand_Seen then
3341 -- Here if operand length is not statically known and no
3342 -- operand known to be non-null has been processed yet.
3343 -- If operand length is 0, we do not need to perform the
3344 -- assignment, and we must avoid the evaluation of the
3345 -- high bound of the slice, since it may underflow if the
3346 -- low bound is Ityp'First.
3349 Make_Implicit_If_Statement (Cnode,
3353 New_Occurrence_Of (Var_Length (J), Loc),
3354 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3355 Then_Statements => New_List (Assign));
3358 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3364 -- Finally we build the result, which is a reference to the array object
3366 Result := New_Reference_To (Ent, Loc);
3369 Rewrite (Cnode, Result);
3370 Analyze_And_Resolve (Cnode, Atyp);
3373 when Concatenation_Error =>
3375 -- Kill warning generated for the declaration of the static out of
3376 -- range high bound, and instead generate a Constraint_Error with
3377 -- an appropriate specific message.
3379 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3380 Apply_Compile_Time_Constraint_Error
3382 Msg => "concatenation result upper bound out of range?",
3383 Reason => CE_Range_Check_Failed);
3384 -- Set_Etype (Cnode, Atyp);
3385 end Expand_Concatenate;
3387 ------------------------
3388 -- Expand_N_Allocator --
3389 ------------------------
3391 procedure Expand_N_Allocator (N : Node_Id) is
3392 PtrT : constant Entity_Id := Etype (N);
3393 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
3394 Etyp : constant Entity_Id := Etype (Expression (N));
3395 Loc : constant Source_Ptr := Sloc (N);
3401 procedure Rewrite_Coextension (N : Node_Id);
3402 -- Static coextensions have the same lifetime as the entity they
3403 -- constrain. Such occurrences can be rewritten as aliased objects
3404 -- and their unrestricted access used instead of the coextension.
3406 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
3407 -- Given a constrained array type E, returns a node representing the
3408 -- code to compute the size in storage elements for the given type.
3409 -- This is done without using the attribute (which malfunctions for
3412 -------------------------
3413 -- Rewrite_Coextension --
3414 -------------------------
3416 procedure Rewrite_Coextension (N : Node_Id) is
3417 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
3418 Temp_Decl : Node_Id;
3419 Insert_Nod : Node_Id;
3423 -- Cnn : aliased Etyp;
3426 Make_Object_Declaration (Loc,
3427 Defining_Identifier => Temp_Id,
3428 Aliased_Present => True,
3429 Object_Definition => New_Occurrence_Of (Etyp, Loc));
3431 if Nkind (Expression (N)) = N_Qualified_Expression then
3432 Set_Expression (Temp_Decl, Expression (Expression (N)));
3435 -- Find the proper insertion node for the declaration
3437 Insert_Nod := Parent (N);
3438 while Present (Insert_Nod) loop
3440 Nkind (Insert_Nod) in N_Statement_Other_Than_Procedure_Call
3441 or else Nkind (Insert_Nod) = N_Procedure_Call_Statement
3442 or else Nkind (Insert_Nod) in N_Declaration;
3444 Insert_Nod := Parent (Insert_Nod);
3447 Insert_Before (Insert_Nod, Temp_Decl);
3448 Analyze (Temp_Decl);
3451 Make_Attribute_Reference (Loc,
3452 Prefix => New_Occurrence_Of (Temp_Id, Loc),
3453 Attribute_Name => Name_Unrestricted_Access));
3455 Analyze_And_Resolve (N, PtrT);
3456 end Rewrite_Coextension;
3458 ------------------------------
3459 -- Size_In_Storage_Elements --
3460 ------------------------------
3462 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
3464 -- Logically this just returns E'Max_Size_In_Storage_Elements.
3465 -- However, the reason for the existence of this function is
3466 -- to construct a test for sizes too large, which means near the
3467 -- 32-bit limit on a 32-bit machine, and precisely the trouble
3468 -- is that we get overflows when sizes are greater than 2**31.
3470 -- So what we end up doing for array types is to use the expression:
3472 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
3474 -- which avoids this problem. All this is a bit bogus, but it does
3475 -- mean we catch common cases of trying to allocate arrays that
3476 -- are too large, and which in the absence of a check results in
3477 -- undetected chaos ???
3484 for J in 1 .. Number_Dimensions (E) loop
3486 Make_Attribute_Reference (Loc,
3487 Prefix => New_Occurrence_Of (E, Loc),
3488 Attribute_Name => Name_Length,
3489 Expressions => New_List (Make_Integer_Literal (Loc, J)));
3496 Make_Op_Multiply (Loc,
3503 Make_Op_Multiply (Loc,
3506 Make_Attribute_Reference (Loc,
3507 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
3508 Attribute_Name => Name_Max_Size_In_Storage_Elements));
3510 end Size_In_Storage_Elements;
3512 -- Start of processing for Expand_N_Allocator
3515 -- RM E.2.3(22). We enforce that the expected type of an allocator
3516 -- shall not be a remote access-to-class-wide-limited-private type
3518 -- Why is this being done at expansion time, seems clearly wrong ???
3520 Validate_Remote_Access_To_Class_Wide_Type (N);
3522 -- Processing for anonymous access-to-controlled types. These access
3523 -- types receive a special finalization master which appears in the
3524 -- declarations of the enclosing semantic unit. This expansion is done
3525 -- now to ensure that any additional types generated by this routine
3526 -- or Expand_Allocator_Expression inherit the proper type attributes.
3528 if Ekind (PtrT) = E_Anonymous_Access_Type
3529 and then Needs_Finalization (Dtyp)
3531 -- Anonymous access-to-controlled types allocate on the global pool.
3532 -- Do not set this attribute on .NET/JVM since those targets do not
3535 if No (Associated_Storage_Pool (PtrT))
3536 and then VM_Target = No_VM
3538 Set_Associated_Storage_Pool
3539 (PtrT, Get_Global_Pool_For_Access_Type (PtrT));
3542 -- The finalization master must be inserted and analyzed as part of
3543 -- the current semantic unit. This form of expansion is not carried
3544 -- out in Alfa mode because it is useless. Note that the master is
3545 -- updated when analysis changes current units.
3547 if not Alfa_Mode then
3548 Set_Finalization_Master (PtrT, Current_Anonymous_Master);
3552 -- Set the storage pool and find the appropriate version of Allocate to
3553 -- call. Do not overwrite the storage pool if it is already set, which
3554 -- can happen for build-in-place function returns (see
3555 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
3557 if No (Storage_Pool (N)) then
3558 Pool := Associated_Storage_Pool (Root_Type (PtrT));
3560 if Present (Pool) then
3561 Set_Storage_Pool (N, Pool);
3563 if Is_RTE (Pool, RE_SS_Pool) then
3564 if VM_Target = No_VM then
3565 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3568 elsif Is_Class_Wide_Type (Etype (Pool)) then
3569 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
3572 Set_Procedure_To_Call (N,
3573 Find_Prim_Op (Etype (Pool), Name_Allocate));
3578 -- Under certain circumstances we can replace an allocator by an access
3579 -- to statically allocated storage. The conditions, as noted in AARM
3580 -- 3.10 (10c) are as follows:
3582 -- Size and initial value is known at compile time
3583 -- Access type is access-to-constant
3585 -- The allocator is not part of a constraint on a record component,
3586 -- because in that case the inserted actions are delayed until the
3587 -- record declaration is fully analyzed, which is too late for the
3588 -- analysis of the rewritten allocator.
3590 if Is_Access_Constant (PtrT)
3591 and then Nkind (Expression (N)) = N_Qualified_Expression
3592 and then Compile_Time_Known_Value (Expression (Expression (N)))
3593 and then Size_Known_At_Compile_Time
3594 (Etype (Expression (Expression (N))))
3595 and then not Is_Record_Type (Current_Scope)
3597 -- Here we can do the optimization. For the allocator
3601 -- We insert an object declaration
3603 -- Tnn : aliased x := y;
3605 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
3606 -- marked as requiring static allocation.
3608 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
3609 Desig := Subtype_Mark (Expression (N));
3611 -- If context is constrained, use constrained subtype directly,
3612 -- so that the constant is not labelled as having a nominally
3613 -- unconstrained subtype.
3615 if Entity (Desig) = Base_Type (Dtyp) then
3616 Desig := New_Occurrence_Of (Dtyp, Loc);
3620 Make_Object_Declaration (Loc,
3621 Defining_Identifier => Temp,
3622 Aliased_Present => True,
3623 Constant_Present => Is_Access_Constant (PtrT),
3624 Object_Definition => Desig,
3625 Expression => Expression (Expression (N))));
3628 Make_Attribute_Reference (Loc,
3629 Prefix => New_Occurrence_Of (Temp, Loc),
3630 Attribute_Name => Name_Unrestricted_Access));
3632 Analyze_And_Resolve (N, PtrT);
3634 -- We set the variable as statically allocated, since we don't want
3635 -- it going on the stack of the current procedure!
3637 Set_Is_Statically_Allocated (Temp);
3641 -- Same if the allocator is an access discriminant for a local object:
3642 -- instead of an allocator we create a local value and constrain the
3643 -- enclosing object with the corresponding access attribute.
3645 if Is_Static_Coextension (N) then
3646 Rewrite_Coextension (N);
3650 -- Check for size too large, we do this because the back end misses
3651 -- proper checks here and can generate rubbish allocation calls when
3652 -- we are near the limit. We only do this for the 32-bit address case
3653 -- since that is from a practical point of view where we see a problem.
3655 if System_Address_Size = 32
3656 and then not Storage_Checks_Suppressed (PtrT)
3657 and then not Storage_Checks_Suppressed (Dtyp)
3658 and then not Storage_Checks_Suppressed (Etyp)
3660 -- The check we want to generate should look like
3662 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
3663 -- raise Storage_Error;
3666 -- where 3.5 gigabytes is a constant large enough to accommodate any
3667 -- reasonable request for. But we can't do it this way because at
3668 -- least at the moment we don't compute this attribute right, and
3669 -- can silently give wrong results when the result gets large. Since
3670 -- this is all about large results, that's bad, so instead we only
3671 -- apply the check for constrained arrays, and manually compute the
3672 -- value of the attribute ???
3674 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
3676 Make_Raise_Storage_Error (Loc,
3679 Left_Opnd => Size_In_Storage_Elements (Etyp),
3681 Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))),
3682 Reason => SE_Object_Too_Large));
3686 -- Handle case of qualified expression (other than optimization above)
3687 -- First apply constraint checks, because the bounds or discriminants
3688 -- in the aggregate might not match the subtype mark in the allocator.
3690 if Nkind (Expression (N)) = N_Qualified_Expression then
3691 Apply_Constraint_Check
3692 (Expression (Expression (N)), Etype (Expression (N)));
3694 Expand_Allocator_Expression (N);
3698 -- If the allocator is for a type which requires initialization, and
3699 -- there is no initial value (i.e. operand is a subtype indication
3700 -- rather than a qualified expression), then we must generate a call to
3701 -- the initialization routine using an expressions action node:
3703 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
3705 -- Here ptr_T is the pointer type for the allocator, and T is the
3706 -- subtype of the allocator. A special case arises if the designated
3707 -- type of the access type is a task or contains tasks. In this case
3708 -- the call to Init (Temp.all ...) is replaced by code that ensures
3709 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
3710 -- for details). In addition, if the type T is a task T, then the
3711 -- first argument to Init must be converted to the task record type.
3714 T : constant Entity_Id := Entity (Expression (N));
3720 Init_Arg1 : Node_Id;
3721 Temp_Decl : Node_Id;
3722 Temp_Type : Entity_Id;
3725 if No_Initialization (N) then
3727 -- Even though this might be a simple allocation, create a custom
3728 -- Allocate if the context requires it. Since .NET/JVM compilers
3729 -- do not support pools, this step is skipped.
3731 if VM_Target = No_VM
3732 and then Present (Finalization_Master (PtrT))
3734 Build_Allocate_Deallocate_Proc
3736 Is_Allocate => True);
3739 -- Case of no initialization procedure present
3741 elsif not Has_Non_Null_Base_Init_Proc (T) then
3743 -- Case of simple initialization required
3745 if Needs_Simple_Initialization (T) then
3746 Check_Restriction (No_Default_Initialization, N);
3747 Rewrite (Expression (N),
3748 Make_Qualified_Expression (Loc,
3749 Subtype_Mark => New_Occurrence_Of (T, Loc),
3750 Expression => Get_Simple_Init_Val (T, N)));
3752 Analyze_And_Resolve (Expression (Expression (N)), T);
3753 Analyze_And_Resolve (Expression (N), T);
3754 Set_Paren_Count (Expression (Expression (N)), 1);
3755 Expand_N_Allocator (N);
3757 -- No initialization required
3763 -- Case of initialization procedure present, must be called
3766 Check_Restriction (No_Default_Initialization, N);
3768 if not Restriction_Active (No_Default_Initialization) then
3769 Init := Base_Init_Proc (T);
3771 Temp := Make_Temporary (Loc, 'P');
3773 -- Construct argument list for the initialization routine call
3776 Make_Explicit_Dereference (Loc,
3778 New_Reference_To (Temp, Loc));
3780 Set_Assignment_OK (Init_Arg1);
3783 -- The initialization procedure expects a specific type. if the
3784 -- context is access to class wide, indicate that the object
3785 -- being allocated has the right specific type.
3787 if Is_Class_Wide_Type (Dtyp) then
3788 Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1);
3791 -- If designated type is a concurrent type or if it is private
3792 -- type whose definition is a concurrent type, the first
3793 -- argument in the Init routine has to be unchecked conversion
3794 -- to the corresponding record type. If the designated type is
3795 -- a derived type, also convert the argument to its root type.
3797 if Is_Concurrent_Type (T) then
3799 Unchecked_Convert_To (
3800 Corresponding_Record_Type (T), Init_Arg1);
3802 elsif Is_Private_Type (T)
3803 and then Present (Full_View (T))
3804 and then Is_Concurrent_Type (Full_View (T))
3807 Unchecked_Convert_To
3808 (Corresponding_Record_Type (Full_View (T)), Init_Arg1);
3810 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
3812 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
3815 Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1);
3816 Set_Etype (Init_Arg1, Ftyp);
3820 Args := New_List (Init_Arg1);
3822 -- For the task case, pass the Master_Id of the access type as
3823 -- the value of the _Master parameter, and _Chain as the value
3824 -- of the _Chain parameter (_Chain will be defined as part of
3825 -- the generated code for the allocator).
3827 -- In Ada 2005, the context may be a function that returns an
3828 -- anonymous access type. In that case the Master_Id has been
3829 -- created when expanding the function declaration.
3831 if Has_Task (T) then
3832 if No (Master_Id (Base_Type (PtrT))) then
3834 -- The designated type was an incomplete type, and the
3835 -- access type did not get expanded. Salvage it now.
3837 if not Restriction_Active (No_Task_Hierarchy) then
3838 pragma Assert (Present (Parent (Base_Type (PtrT))));
3839 Expand_N_Full_Type_Declaration
3840 (Parent (Base_Type (PtrT)));
3844 -- If the context of the allocator is a declaration or an
3845 -- assignment, we can generate a meaningful image for it,
3846 -- even though subsequent assignments might remove the
3847 -- connection between task and entity. We build this image
3848 -- when the left-hand side is a simple variable, a simple
3849 -- indexed assignment or a simple selected component.
3851 if Nkind (Parent (N)) = N_Assignment_Statement then
3853 Nam : constant Node_Id := Name (Parent (N));
3856 if Is_Entity_Name (Nam) then
3858 Build_Task_Image_Decls
3861 (Entity (Nam), Sloc (Nam)), T);
3863 elsif Nkind_In (Nam, N_Indexed_Component,
3864 N_Selected_Component)
3865 and then Is_Entity_Name (Prefix (Nam))
3868 Build_Task_Image_Decls
3869 (Loc, Nam, Etype (Prefix (Nam)));
3871 Decls := Build_Task_Image_Decls (Loc, T, T);
3875 elsif Nkind (Parent (N)) = N_Object_Declaration then
3877 Build_Task_Image_Decls
3878 (Loc, Defining_Identifier (Parent (N)), T);
3881 Decls := Build_Task_Image_Decls (Loc, T, T);
3884 if Restriction_Active (No_Task_Hierarchy) then
3886 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
3890 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
3893 Append_To (Args, Make_Identifier (Loc, Name_uChain));
3895 Decl := Last (Decls);
3897 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
3899 -- Has_Task is false, Decls not used
3905 -- Add discriminants if discriminated type
3908 Dis : Boolean := False;
3912 if Has_Discriminants (T) then
3916 elsif Is_Private_Type (T)
3917 and then Present (Full_View (T))
3918 and then Has_Discriminants (Full_View (T))
3921 Typ := Full_View (T);
3926 -- If the allocated object will be constrained by the
3927 -- default values for discriminants, then build a subtype
3928 -- with those defaults, and change the allocated subtype
3929 -- to that. Note that this happens in fewer cases in Ada
3932 if not Is_Constrained (Typ)
3933 and then Present (Discriminant_Default_Value
3934 (First_Discriminant (Typ)))
3935 and then (Ada_Version < Ada_2005
3937 Effectively_Has_Constrained_Partial_View
3939 Scop => Current_Scope))
3941 Typ := Build_Default_Subtype (Typ, N);
3942 Set_Expression (N, New_Reference_To (Typ, Loc));
3945 Discr := First_Elmt (Discriminant_Constraint (Typ));
3946 while Present (Discr) loop
3947 Nod := Node (Discr);
3948 Append (New_Copy_Tree (Node (Discr)), Args);
3950 -- AI-416: when the discriminant constraint is an
3951 -- anonymous access type make sure an accessibility
3952 -- check is inserted if necessary (3.10.2(22.q/2))
3954 if Ada_Version >= Ada_2005
3956 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
3958 Apply_Accessibility_Check
3959 (Nod, Typ, Insert_Node => Nod);
3967 -- We set the allocator as analyzed so that when we analyze the
3968 -- expression actions node, we do not get an unwanted recursive
3969 -- expansion of the allocator expression.
3971 Set_Analyzed (N, True);
3972 Nod := Relocate_Node (N);
3974 -- Here is the transformation:
3975 -- input: new Ctrl_Typ
3976 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
3977 -- Ctrl_TypIP (Temp.all, ...);
3978 -- [Deep_]Initialize (Temp.all);
3980 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
3981 -- is the subtype of the allocator.
3984 Make_Object_Declaration (Loc,
3985 Defining_Identifier => Temp,
3986 Constant_Present => True,
3987 Object_Definition => New_Reference_To (Temp_Type, Loc),
3990 Set_Assignment_OK (Temp_Decl);
3991 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
3993 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
3995 -- If the designated type is a task type or contains tasks,
3996 -- create block to activate created tasks, and insert
3997 -- declaration for Task_Image variable ahead of call.
3999 if Has_Task (T) then
4001 L : constant List_Id := New_List;
4004 Build_Task_Allocate_Block (L, Nod, Args);
4006 Insert_List_Before (First (Declarations (Blk)), Decls);
4007 Insert_Actions (N, L);
4012 Make_Procedure_Call_Statement (Loc,
4013 Name => New_Reference_To (Init, Loc),
4014 Parameter_Associations => Args));
4017 if Needs_Finalization (T) then
4020 -- [Deep_]Initialize (Init_Arg1);
4024 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4027 if Present (Finalization_Master (PtrT)) then
4029 -- Special processing for .NET/JVM, the allocated object
4030 -- is attached to the finalization master. Generate:
4032 -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1));
4034 -- Types derived from [Limited_]Controlled are the only
4035 -- ones considered since they have fields Prev and Next.
4037 if VM_Target /= No_VM then
4038 if Is_Controlled (T) then
4041 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4045 -- Default case, generate:
4047 -- Set_Finalize_Address
4048 -- (<PtrT>FM, <T>FD'Unrestricted_Access);
4050 -- Do not generate this call in the following cases:
4052 -- * Alfa mode - the call is useless and results in
4053 -- unwanted expansion.
4055 -- * CodePeer mode - TSS primitive Finalize_Address is
4056 -- not created in this mode.
4059 and then not CodePeer_Mode
4062 Make_Set_Finalize_Address_Call
4070 Rewrite (N, New_Reference_To (Temp, Loc));
4071 Analyze_And_Resolve (N, PtrT);
4076 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
4077 -- object that has been rewritten as a reference, we displace "this"
4078 -- to reference properly its secondary dispatch table.
4080 if Nkind (N) = N_Identifier
4081 and then Is_Interface (Dtyp)
4083 Displace_Allocator_Pointer (N);
4087 when RE_Not_Available =>
4089 end Expand_N_Allocator;
4091 -----------------------
4092 -- Expand_N_And_Then --
4093 -----------------------
4095 procedure Expand_N_And_Then (N : Node_Id)
4096 renames Expand_Short_Circuit_Operator;
4098 ------------------------------
4099 -- Expand_N_Case_Expression --
4100 ------------------------------
4102 procedure Expand_N_Case_Expression (N : Node_Id) is
4103 Loc : constant Source_Ptr := Sloc (N);
4104 Typ : constant Entity_Id := Etype (N);
4116 -- case X is when A => AX, when B => BX ...
4131 -- However, this expansion is wrong for limited types, and also
4132 -- wrong for unconstrained types (since the bounds may not be the
4133 -- same in all branches). Furthermore it involves an extra copy
4134 -- for large objects. So we take care of this by using the following
4135 -- modified expansion for non-scalar types:
4138 -- type Pnn is access all typ;
4142 -- T := AX'Unrestricted_Access;
4144 -- T := BX'Unrestricted_Access;
4150 Make_Case_Statement (Loc,
4151 Expression => Expression (N),
4152 Alternatives => New_List);
4154 Actions := New_List;
4158 if Is_Scalar_Type (Typ) then
4162 Pnn := Make_Temporary (Loc, 'P');
4164 Make_Full_Type_Declaration (Loc,
4165 Defining_Identifier => Pnn,
4167 Make_Access_To_Object_Definition (Loc,
4168 All_Present => True,
4169 Subtype_Indication =>
4170 New_Reference_To (Typ, Loc))));
4174 Tnn := Make_Temporary (Loc, 'T');
4176 Make_Object_Declaration (Loc,
4177 Defining_Identifier => Tnn,
4178 Object_Definition => New_Occurrence_Of (Ttyp, Loc)));
4180 -- Now process the alternatives
4182 Alt := First (Alternatives (N));
4183 while Present (Alt) loop
4185 Aexp : Node_Id := Expression (Alt);
4186 Aloc : constant Source_Ptr := Sloc (Aexp);
4190 -- As described above, take Unrestricted_Access for case of non-
4191 -- scalar types, to avoid big copies, and special cases.
4193 if not Is_Scalar_Type (Typ) then
4195 Make_Attribute_Reference (Aloc,
4196 Prefix => Relocate_Node (Aexp),
4197 Attribute_Name => Name_Unrestricted_Access);
4201 Make_Assignment_Statement (Aloc,
4202 Name => New_Occurrence_Of (Tnn, Loc),
4203 Expression => Aexp));
4205 -- Propagate declarations inserted in the node by Insert_Actions
4206 -- (for example, temporaries generated to remove side effects).
4207 -- These actions must remain attached to the alternative, given
4208 -- that they are generated by the corresponding expression.
4210 if Present (Sinfo.Actions (Alt)) then
4211 Prepend_List (Sinfo.Actions (Alt), Stats);
4215 (Alternatives (Cstmt),
4216 Make_Case_Statement_Alternative (Sloc (Alt),
4217 Discrete_Choices => Discrete_Choices (Alt),
4218 Statements => Stats));
4224 Append_To (Actions, Cstmt);
4226 -- Construct and return final expression with actions
4228 if Is_Scalar_Type (Typ) then
4229 Fexp := New_Occurrence_Of (Tnn, Loc);
4232 Make_Explicit_Dereference (Loc,
4233 Prefix => New_Occurrence_Of (Tnn, Loc));
4237 Make_Expression_With_Actions (Loc,
4239 Actions => Actions));
4241 Analyze_And_Resolve (N, Typ);
4242 end Expand_N_Case_Expression;
4244 -------------------------------------
4245 -- Expand_N_Conditional_Expression --
4246 -------------------------------------
4248 -- Deal with limited types and expression actions
4250 procedure Expand_N_Conditional_Expression (N : Node_Id) is
4251 Loc : constant Source_Ptr := Sloc (N);
4252 Cond : constant Node_Id := First (Expressions (N));
4253 Thenx : constant Node_Id := Next (Cond);
4254 Elsex : constant Node_Id := Next (Thenx);
4255 Typ : constant Entity_Id := Etype (N);
4266 -- Fold at compile time if condition known. We have already folded
4267 -- static conditional expressions, but it is possible to fold any
4268 -- case in which the condition is known at compile time, even though
4269 -- the result is non-static.
4271 -- Note that we don't do the fold of such cases in Sem_Elab because
4272 -- it can cause infinite loops with the expander adding a conditional
4273 -- expression, and Sem_Elab circuitry removing it repeatedly.
4275 if Compile_Time_Known_Value (Cond) then
4276 if Is_True (Expr_Value (Cond)) then
4278 Actions := Then_Actions (N);
4281 Actions := Else_Actions (N);
4286 if Present (Actions) then
4288 -- If we are not allowed to use Expression_With_Actions, just skip
4289 -- the optimization, it is not critical for correctness.
4291 if not Use_Expression_With_Actions then
4292 goto Skip_Optimization;
4296 Make_Expression_With_Actions (Loc,
4297 Expression => Relocate_Node (Expr),
4298 Actions => Actions));
4299 Analyze_And_Resolve (N, Typ);
4302 Rewrite (N, Relocate_Node (Expr));
4305 -- Note that the result is never static (legitimate cases of static
4306 -- conditional expressions were folded in Sem_Eval).
4308 Set_Is_Static_Expression (N, False);
4312 <<Skip_Optimization>>
4314 -- If the type is limited or unconstrained, we expand as follows to
4315 -- avoid any possibility of improper copies.
4317 -- Note: it may be possible to avoid this special processing if the
4318 -- back end uses its own mechanisms for handling by-reference types ???
4320 -- type Ptr is access all Typ;
4324 -- Cnn := then-expr'Unrestricted_Access;
4327 -- Cnn := else-expr'Unrestricted_Access;
4330 -- and replace the conditional expression by a reference to Cnn.all.
4332 -- This special case can be skipped if the back end handles limited
4333 -- types properly and ensures that no incorrect copies are made.
4335 if Is_By_Reference_Type (Typ)
4336 and then not Back_End_Handles_Limited_Types
4338 Cnn := Make_Temporary (Loc, 'C', N);
4341 Make_Full_Type_Declaration (Loc,
4342 Defining_Identifier =>
4343 Make_Temporary (Loc, 'A'),
4345 Make_Access_To_Object_Definition (Loc,
4346 All_Present => True,
4347 Subtype_Indication => New_Reference_To (Typ, Loc)));
4349 Insert_Action (N, P_Decl);
4352 Make_Object_Declaration (Loc,
4353 Defining_Identifier => Cnn,
4354 Object_Definition =>
4355 New_Occurrence_Of (Defining_Identifier (P_Decl), Loc));
4358 Make_Implicit_If_Statement (N,
4359 Condition => Relocate_Node (Cond),
4361 Then_Statements => New_List (
4362 Make_Assignment_Statement (Sloc (Thenx),
4363 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
4365 Make_Attribute_Reference (Loc,
4366 Attribute_Name => Name_Unrestricted_Access,
4367 Prefix => Relocate_Node (Thenx)))),
4369 Else_Statements => New_List (
4370 Make_Assignment_Statement (Sloc (Elsex),
4371 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
4373 Make_Attribute_Reference (Loc,
4374 Attribute_Name => Name_Unrestricted_Access,
4375 Prefix => Relocate_Node (Elsex)))));
4378 Make_Explicit_Dereference (Loc,
4379 Prefix => New_Occurrence_Of (Cnn, Loc));
4381 -- For other types, we only need to expand if there are other actions
4382 -- associated with either branch.
4384 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
4386 -- We have two approaches to handling this. If we are allowed to use
4387 -- N_Expression_With_Actions, then we can just wrap the actions into
4388 -- the appropriate expression.
4390 if Use_Expression_With_Actions then
4391 if Present (Then_Actions (N)) then
4393 Make_Expression_With_Actions (Sloc (Thenx),
4394 Actions => Then_Actions (N),
4395 Expression => Relocate_Node (Thenx)));
4396 Set_Then_Actions (N, No_List);
4397 Analyze_And_Resolve (Thenx, Typ);
4400 if Present (Else_Actions (N)) then
4402 Make_Expression_With_Actions (Sloc (Elsex),
4403 Actions => Else_Actions (N),
4404 Expression => Relocate_Node (Elsex)));
4405 Set_Else_Actions (N, No_List);
4406 Analyze_And_Resolve (Elsex, Typ);
4411 -- if we can't use N_Expression_With_Actions nodes, then we insert
4412 -- the following sequence of actions (using Insert_Actions):
4417 -- Cnn := then-expr;
4423 -- and replace the conditional expression by a reference to Cnn
4426 Cnn := Make_Temporary (Loc, 'C', N);
4429 Make_Object_Declaration (Loc,
4430 Defining_Identifier => Cnn,
4431 Object_Definition => New_Occurrence_Of (Typ, Loc));
4434 Make_Implicit_If_Statement (N,
4435 Condition => Relocate_Node (Cond),
4437 Then_Statements => New_List (
4438 Make_Assignment_Statement (Sloc (Thenx),
4439 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
4440 Expression => Relocate_Node (Thenx))),
4442 Else_Statements => New_List (
4443 Make_Assignment_Statement (Sloc (Elsex),
4444 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
4445 Expression => Relocate_Node (Elsex))));
4447 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
4448 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
4450 New_N := New_Occurrence_Of (Cnn, Loc);
4453 -- If no actions then no expansion needed, gigi will handle it using
4454 -- the same approach as a C conditional expression.
4460 -- Fall through here for either the limited expansion, or the case of
4461 -- inserting actions for non-limited types. In both these cases, we must
4462 -- move the SLOC of the parent If statement to the newly created one and
4463 -- change it to the SLOC of the expression which, after expansion, will
4464 -- correspond to what is being evaluated.
4466 if Present (Parent (N))
4467 and then Nkind (Parent (N)) = N_If_Statement
4469 Set_Sloc (New_If, Sloc (Parent (N)));
4470 Set_Sloc (Parent (N), Loc);
4473 -- Make sure Then_Actions and Else_Actions are appropriately moved
4474 -- to the new if statement.
4476 if Present (Then_Actions (N)) then
4478 (First (Then_Statements (New_If)), Then_Actions (N));
4481 if Present (Else_Actions (N)) then
4483 (First (Else_Statements (New_If)), Else_Actions (N));
4486 Insert_Action (N, Decl);
4487 Insert_Action (N, New_If);
4489 Analyze_And_Resolve (N, Typ);
4490 end Expand_N_Conditional_Expression;
4492 -----------------------------------
4493 -- Expand_N_Explicit_Dereference --
4494 -----------------------------------
4496 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
4498 -- Insert explicit dereference call for the checked storage pool case
4500 Insert_Dereference_Action (Prefix (N));
4502 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
4503 -- we set the atomic sync flag.
4505 if Is_Atomic (Etype (N))
4506 and then not Atomic_Synchronization_Disabled (Etype (N))
4508 Activate_Atomic_Synchronization (N);
4510 end Expand_N_Explicit_Dereference;
4512 --------------------------------------
4513 -- Expand_N_Expression_With_Actions --
4514 --------------------------------------
4516 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
4518 procedure Process_Transient_Object (Decl : Node_Id);
4519 -- Given the declaration of a controlled transient declared inside the
4520 -- Actions list of an Expression_With_Actions, generate all necessary
4521 -- types and hooks in order to properly finalize the transient. This
4522 -- mechanism works in conjunction with Build_Finalizer.
4524 ------------------------------
4525 -- Process_Transient_Object --
4526 ------------------------------
4528 procedure Process_Transient_Object (Decl : Node_Id) is
4530 function Find_Insertion_Node return Node_Id;
4531 -- Complex conditions in if statements may be converted into nested
4532 -- EWAs. In this case, any generated code must be inserted before the
4533 -- if statement to ensure proper visibility of the hook objects. This
4534 -- routine returns the top most short circuit operator or the parent
4535 -- of the EWA if no nesting was detected.
4537 -------------------------
4538 -- Find_Insertion_Node --
4539 -------------------------
4541 function Find_Insertion_Node return Node_Id is
4545 -- Climb up the branches of a complex condition
4548 while Nkind_In (Parent (Par), N_And_Then, N_Op_Not, N_Or_Else) loop
4549 Par := Parent (Par);
4553 end Find_Insertion_Node;
4557 Ins_Node : constant Node_Id := Find_Insertion_Node;
4558 Loc : constant Source_Ptr := Sloc (Decl);
4559 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
4560 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
4561 Desig_Typ : Entity_Id;
4565 Temp_Decl : Node_Id;
4568 -- Start of processing for Process_Transient_Object
4571 -- Step 1: Create the access type which provides a reference to the
4572 -- transient object.
4574 if Is_Access_Type (Obj_Typ) then
4575 Desig_Typ := Directly_Designated_Type (Obj_Typ);
4577 Desig_Typ := Obj_Typ;
4581 -- Ann : access [all] <Desig_Typ>;
4583 Ptr_Id := Make_Temporary (Loc, 'A');
4586 Make_Full_Type_Declaration (Loc,
4587 Defining_Identifier => Ptr_Id,
4589 Make_Access_To_Object_Definition (Loc,
4591 Ekind (Obj_Typ) = E_General_Access_Type,
4592 Subtype_Indication => New_Reference_To (Desig_Typ, Loc)));
4594 Insert_Action (Ins_Node, Ptr_Decl);
4597 -- Step 2: Create a temporary which acts as a hook to the transient
4598 -- object. Generate:
4600 -- Temp : Ptr_Id := null;
4602 Temp_Id := Make_Temporary (Loc, 'T');
4605 Make_Object_Declaration (Loc,
4606 Defining_Identifier => Temp_Id,
4607 Object_Definition => New_Reference_To (Ptr_Id, Loc));
4609 Insert_Action (Ins_Node, Temp_Decl);
4610 Analyze (Temp_Decl);
4612 -- Mark this temporary as created for the purposes of exporting the
4613 -- transient declaration out of the Actions list. This signals the
4614 -- machinery in Build_Finalizer to recognize this special case.
4616 Set_Return_Flag_Or_Transient_Decl (Temp_Id, Decl);
4618 -- Step 3: Hook the transient object to the temporary
4620 if Is_Access_Type (Obj_Typ) then
4621 Expr := Convert_To (Ptr_Id, New_Reference_To (Obj_Id, Loc));
4624 Make_Attribute_Reference (Loc,
4625 Prefix => New_Reference_To (Obj_Id, Loc),
4626 Attribute_Name => Name_Unrestricted_Access);
4630 -- Temp := Ptr_Id (Obj_Id);
4632 -- Temp := Obj_Id'Unrestricted_Access;
4634 Insert_After_And_Analyze (Decl,
4635 Make_Assignment_Statement (Loc,
4636 Name => New_Reference_To (Temp_Id, Loc),
4637 Expression => Expr));
4638 end Process_Transient_Object;
4644 -- Start of processing for Expand_N_Expression_With_Actions
4647 Decl := First (Actions (N));
4648 while Present (Decl) loop
4649 if Nkind (Decl) = N_Object_Declaration
4650 and then Is_Finalizable_Transient (Decl, N)
4652 Process_Transient_Object (Decl);
4657 end Expand_N_Expression_With_Actions;
4663 procedure Expand_N_In (N : Node_Id) is
4664 Loc : constant Source_Ptr := Sloc (N);
4665 Restyp : constant Entity_Id := Etype (N);
4666 Lop : constant Node_Id := Left_Opnd (N);
4667 Rop : constant Node_Id := Right_Opnd (N);
4668 Static : constant Boolean := Is_OK_Static_Expression (N);
4673 procedure Substitute_Valid_Check;
4674 -- Replaces node N by Lop'Valid. This is done when we have an explicit
4675 -- test for the left operand being in range of its subtype.
4677 ----------------------------
4678 -- Substitute_Valid_Check --
4679 ----------------------------
4681 procedure Substitute_Valid_Check is
4684 Make_Attribute_Reference (Loc,
4685 Prefix => Relocate_Node (Lop),
4686 Attribute_Name => Name_Valid));
4688 Analyze_And_Resolve (N, Restyp);
4690 Error_Msg_N ("?explicit membership test may be optimized away", N);
4691 Error_Msg_N -- CODEFIX
4692 ("\?use ''Valid attribute instead", N);
4694 end Substitute_Valid_Check;
4696 -- Start of processing for Expand_N_In
4699 -- If set membership case, expand with separate procedure
4701 if Present (Alternatives (N)) then
4702 Expand_Set_Membership (N);
4706 -- Not set membership, proceed with expansion
4708 Ltyp := Etype (Left_Opnd (N));
4709 Rtyp := Etype (Right_Opnd (N));
4711 -- Check case of explicit test for an expression in range of its
4712 -- subtype. This is suspicious usage and we replace it with a 'Valid
4713 -- test and give a warning. For floating point types however, this is a
4714 -- standard way to check for finite numbers, and using 'Valid would
4715 -- typically be a pessimization. Also skip this test for predicated
4716 -- types, since it is perfectly reasonable to check if a value meets
4719 if Is_Scalar_Type (Ltyp)
4720 and then not Is_Floating_Point_Type (Ltyp)
4721 and then Nkind (Rop) in N_Has_Entity
4722 and then Ltyp = Entity (Rop)
4723 and then Comes_From_Source (N)
4724 and then VM_Target = No_VM
4725 and then not (Is_Discrete_Type (Ltyp)
4726 and then Present (Predicate_Function (Ltyp)))
4728 Substitute_Valid_Check;
4732 -- Do validity check on operands
4734 if Validity_Checks_On and Validity_Check_Operands then
4735 Ensure_Valid (Left_Opnd (N));
4736 Validity_Check_Range (Right_Opnd (N));
4739 -- Case of explicit range
4741 if Nkind (Rop) = N_Range then
4743 Lo : constant Node_Id := Low_Bound (Rop);
4744 Hi : constant Node_Id := High_Bound (Rop);
4746 Lo_Orig : constant Node_Id := Original_Node (Lo);
4747 Hi_Orig : constant Node_Id := Original_Node (Hi);
4749 Lcheck : Compare_Result;
4750 Ucheck : Compare_Result;
4752 Warn1 : constant Boolean :=
4753 Constant_Condition_Warnings
4754 and then Comes_From_Source (N)
4755 and then not In_Instance;
4756 -- This must be true for any of the optimization warnings, we
4757 -- clearly want to give them only for source with the flag on. We
4758 -- also skip these warnings in an instance since it may be the
4759 -- case that different instantiations have different ranges.
4761 Warn2 : constant Boolean :=
4763 and then Nkind (Original_Node (Rop)) = N_Range
4764 and then Is_Integer_Type (Etype (Lo));
4765 -- For the case where only one bound warning is elided, we also
4766 -- insist on an explicit range and an integer type. The reason is
4767 -- that the use of enumeration ranges including an end point is
4768 -- common, as is the use of a subtype name, one of whose bounds is
4769 -- the same as the type of the expression.
4772 -- If test is explicit x'First .. x'Last, replace by valid check
4774 -- Could use some individual comments for this complex test ???
4776 if Is_Scalar_Type (Ltyp)
4777 and then Nkind (Lo_Orig) = N_Attribute_Reference
4778 and then Attribute_Name (Lo_Orig) = Name_First
4779 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
4780 and then Entity (Prefix (Lo_Orig)) = Ltyp
4781 and then Nkind (Hi_Orig) = N_Attribute_Reference
4782 and then Attribute_Name (Hi_Orig) = Name_Last
4783 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
4784 and then Entity (Prefix (Hi_Orig)) = Ltyp
4785 and then Comes_From_Source (N)
4786 and then VM_Target = No_VM
4788 Substitute_Valid_Check;
4792 -- If bounds of type are known at compile time, and the end points
4793 -- are known at compile time and identical, this is another case
4794 -- for substituting a valid test. We only do this for discrete
4795 -- types, since it won't arise in practice for float types.
4797 if Comes_From_Source (N)
4798 and then Is_Discrete_Type (Ltyp)
4799 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
4800 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
4801 and then Compile_Time_Known_Value (Lo)
4802 and then Compile_Time_Known_Value (Hi)
4803 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
4804 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
4806 -- Kill warnings in instances, since they may be cases where we
4807 -- have a test in the generic that makes sense with some types
4808 -- and not with other types.
4810 and then not In_Instance
4812 Substitute_Valid_Check;
4816 -- If we have an explicit range, do a bit of optimization based on
4817 -- range analysis (we may be able to kill one or both checks).
4819 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
4820 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
4822 -- If either check is known to fail, replace result by False since
4823 -- the other check does not matter. Preserve the static flag for
4824 -- legality checks, because we are constant-folding beyond RM 4.9.
4826 if Lcheck = LT or else Ucheck = GT then
4828 Error_Msg_N ("?range test optimized away", N);
4829 Error_Msg_N ("\?value is known to be out of range", N);
4832 Rewrite (N, New_Reference_To (Standard_False, Loc));
4833 Analyze_And_Resolve (N, Restyp);
4834 Set_Is_Static_Expression (N, Static);
4837 -- If both checks are known to succeed, replace result by True,
4838 -- since we know we are in range.
4840 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4842 Error_Msg_N ("?range test optimized away", N);
4843 Error_Msg_N ("\?value is known to be in range", N);
4846 Rewrite (N, New_Reference_To (Standard_True, Loc));
4847 Analyze_And_Resolve (N, Restyp);
4848 Set_Is_Static_Expression (N, Static);
4851 -- If lower bound check succeeds and upper bound check is not
4852 -- known to succeed or fail, then replace the range check with
4853 -- a comparison against the upper bound.
4855 elsif Lcheck in Compare_GE then
4856 if Warn2 and then not In_Instance then
4857 Error_Msg_N ("?lower bound test optimized away", Lo);
4858 Error_Msg_N ("\?value is known to be in range", Lo);
4864 Right_Opnd => High_Bound (Rop)));
4865 Analyze_And_Resolve (N, Restyp);
4868 -- If upper bound check succeeds and lower bound check is not
4869 -- known to succeed or fail, then replace the range check with
4870 -- a comparison against the lower bound.
4872 elsif Ucheck in Compare_LE then
4873 if Warn2 and then not In_Instance then
4874 Error_Msg_N ("?upper bound test optimized away", Hi);
4875 Error_Msg_N ("\?value is known to be in range", Hi);
4881 Right_Opnd => Low_Bound (Rop)));
4882 Analyze_And_Resolve (N, Restyp);
4886 -- We couldn't optimize away the range check, but there is one
4887 -- more issue. If we are checking constant conditionals, then we
4888 -- see if we can determine the outcome assuming everything is
4889 -- valid, and if so give an appropriate warning.
4891 if Warn1 and then not Assume_No_Invalid_Values then
4892 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
4893 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
4895 -- Result is out of range for valid value
4897 if Lcheck = LT or else Ucheck = GT then
4899 ("?value can only be in range if it is invalid", N);
4901 -- Result is in range for valid value
4903 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4905 ("?value can only be out of range if it is invalid", N);
4907 -- Lower bound check succeeds if value is valid
4909 elsif Warn2 and then Lcheck in Compare_GE then
4911 ("?lower bound check only fails if it is invalid", Lo);
4913 -- Upper bound check succeeds if value is valid
4915 elsif Warn2 and then Ucheck in Compare_LE then
4917 ("?upper bound check only fails for invalid values", Hi);
4922 -- For all other cases of an explicit range, nothing to be done
4926 -- Here right operand is a subtype mark
4930 Typ : Entity_Id := Etype (Rop);
4931 Is_Acc : constant Boolean := Is_Access_Type (Typ);
4932 Cond : Node_Id := Empty;
4934 Obj : Node_Id := Lop;
4935 SCIL_Node : Node_Id;
4938 Remove_Side_Effects (Obj);
4940 -- For tagged type, do tagged membership operation
4942 if Is_Tagged_Type (Typ) then
4944 -- No expansion will be performed when VM_Target, as the VM
4945 -- back-ends will handle the membership tests directly (tags
4946 -- are not explicitly represented in Java objects, so the
4947 -- normal tagged membership expansion is not what we want).
4949 if Tagged_Type_Expansion then
4950 Tagged_Membership (N, SCIL_Node, New_N);
4952 Analyze_And_Resolve (N, Restyp);
4954 -- Update decoration of relocated node referenced by the
4957 if Generate_SCIL and then Present (SCIL_Node) then
4958 Set_SCIL_Node (N, SCIL_Node);
4964 -- If type is scalar type, rewrite as x in t'First .. t'Last.
4965 -- This reason we do this is that the bounds may have the wrong
4966 -- type if they come from the original type definition. Also this
4967 -- way we get all the processing above for an explicit range.
4969 -- Don't do this for predicated types, since in this case we
4970 -- want to check the predicate!
4972 elsif Is_Scalar_Type (Typ) then
4973 if No (Predicate_Function (Typ)) then
4977 Make_Attribute_Reference (Loc,
4978 Attribute_Name => Name_First,
4979 Prefix => New_Reference_To (Typ, Loc)),
4982 Make_Attribute_Reference (Loc,
4983 Attribute_Name => Name_Last,
4984 Prefix => New_Reference_To (Typ, Loc))));
4985 Analyze_And_Resolve (N, Restyp);
4990 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4991 -- a membership test if the subtype mark denotes a constrained
4992 -- Unchecked_Union subtype and the expression lacks inferable
4995 elsif Is_Unchecked_Union (Base_Type (Typ))
4996 and then Is_Constrained (Typ)
4997 and then not Has_Inferable_Discriminants (Lop)
5000 Make_Raise_Program_Error (Loc,
5001 Reason => PE_Unchecked_Union_Restriction));
5003 -- Prevent Gigi from generating incorrect code by rewriting the
5006 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5010 -- Here we have a non-scalar type
5013 Typ := Designated_Type (Typ);
5016 if not Is_Constrained (Typ) then
5017 Rewrite (N, New_Reference_To (Standard_True, Loc));
5018 Analyze_And_Resolve (N, Restyp);
5020 -- For the constrained array case, we have to check the subscripts
5021 -- for an exact match if the lengths are non-zero (the lengths
5022 -- must match in any case).
5024 elsif Is_Array_Type (Typ) then
5025 Check_Subscripts : declare
5026 function Build_Attribute_Reference
5029 Dim : Nat) return Node_Id;
5030 -- Build attribute reference E'Nam (Dim)
5032 -------------------------------
5033 -- Build_Attribute_Reference --
5034 -------------------------------
5036 function Build_Attribute_Reference
5039 Dim : Nat) return Node_Id
5043 Make_Attribute_Reference (Loc,
5045 Attribute_Name => Nam,
5046 Expressions => New_List (
5047 Make_Integer_Literal (Loc, Dim)));
5048 end Build_Attribute_Reference;
5050 -- Start of processing for Check_Subscripts
5053 for J in 1 .. Number_Dimensions (Typ) loop
5054 Evolve_And_Then (Cond,
5057 Build_Attribute_Reference
5058 (Duplicate_Subexpr_No_Checks (Obj),
5061 Build_Attribute_Reference
5062 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
5064 Evolve_And_Then (Cond,
5067 Build_Attribute_Reference
5068 (Duplicate_Subexpr_No_Checks (Obj),
5071 Build_Attribute_Reference
5072 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
5081 Right_Opnd => Make_Null (Loc)),
5082 Right_Opnd => Cond);
5086 Analyze_And_Resolve (N, Restyp);
5087 end Check_Subscripts;
5089 -- These are the cases where constraint checks may be required,
5090 -- e.g. records with possible discriminants
5093 -- Expand the test into a series of discriminant comparisons.
5094 -- The expression that is built is the negation of the one that
5095 -- is used for checking discriminant constraints.
5097 Obj := Relocate_Node (Left_Opnd (N));
5099 if Has_Discriminants (Typ) then
5100 Cond := Make_Op_Not (Loc,
5101 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
5104 Cond := Make_Or_Else (Loc,
5108 Right_Opnd => Make_Null (Loc)),
5109 Right_Opnd => Cond);
5113 Cond := New_Occurrence_Of (Standard_True, Loc);
5117 Analyze_And_Resolve (N, Restyp);
5120 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
5121 -- expression of an anonymous access type. This can involve an
5122 -- accessibility test and a tagged type membership test in the
5123 -- case of tagged designated types.
5125 if Ada_Version >= Ada_2012
5127 and then Ekind (Ltyp) = E_Anonymous_Access_Type
5130 Expr_Entity : Entity_Id := Empty;
5132 Param_Level : Node_Id;
5133 Type_Level : Node_Id;
5136 if Is_Entity_Name (Lop) then
5137 Expr_Entity := Param_Entity (Lop);
5139 if not Present (Expr_Entity) then
5140 Expr_Entity := Entity (Lop);
5144 -- If a conversion of the anonymous access value to the
5145 -- tested type would be illegal, then the result is False.
5147 if not Valid_Conversion
5148 (Lop, Rtyp, Lop, Report_Errs => False)
5150 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5151 Analyze_And_Resolve (N, Restyp);
5153 -- Apply an accessibility check if the access object has an
5154 -- associated access level and when the level of the type is
5155 -- less deep than the level of the access parameter. This
5156 -- only occur for access parameters and stand-alone objects
5157 -- of an anonymous access type.
5160 if Present (Expr_Entity)
5163 (Effective_Extra_Accessibility (Expr_Entity))
5164 and then UI_Gt (Object_Access_Level (Lop),
5165 Type_Access_Level (Rtyp))
5169 (Effective_Extra_Accessibility (Expr_Entity), Loc);
5172 Make_Integer_Literal (Loc, Type_Access_Level (Rtyp));
5174 -- Return True only if the accessibility level of the
5175 -- expression entity is not deeper than the level of
5176 -- the tested access type.
5180 Left_Opnd => Relocate_Node (N),
5181 Right_Opnd => Make_Op_Le (Loc,
5182 Left_Opnd => Param_Level,
5183 Right_Opnd => Type_Level)));
5185 Analyze_And_Resolve (N);
5188 -- If the designated type is tagged, do tagged membership
5191 -- *** NOTE: we have to check not null before doing the
5192 -- tagged membership test (but maybe that can be done
5193 -- inside Tagged_Membership?).
5195 if Is_Tagged_Type (Typ) then
5198 Left_Opnd => Relocate_Node (N),
5202 Right_Opnd => Make_Null (Loc))));
5204 -- No expansion will be performed when VM_Target, as
5205 -- the VM back-ends will handle the membership tests
5206 -- directly (tags are not explicitly represented in
5207 -- Java objects, so the normal tagged membership
5208 -- expansion is not what we want).
5210 if Tagged_Type_Expansion then
5212 -- Note that we have to pass Original_Node, because
5213 -- the membership test might already have been
5214 -- rewritten by earlier parts of membership test.
5217 (Original_Node (N), SCIL_Node, New_N);
5219 -- Update decoration of relocated node referenced
5220 -- by the SCIL node.
5222 if Generate_SCIL and then Present (SCIL_Node) then
5223 Set_SCIL_Node (New_N, SCIL_Node);
5228 Left_Opnd => Relocate_Node (N),
5229 Right_Opnd => New_N));
5231 Analyze_And_Resolve (N, Restyp);
5240 -- At this point, we have done the processing required for the basic
5241 -- membership test, but not yet dealt with the predicate.
5245 -- If a predicate is present, then we do the predicate test, but we
5246 -- most certainly want to omit this if we are within the predicate
5247 -- function itself, since otherwise we have an infinite recursion!
5250 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
5254 and then Current_Scope /= PFunc
5258 Left_Opnd => Relocate_Node (N),
5259 Right_Opnd => Make_Predicate_Call (Rtyp, Lop)));
5261 -- Analyze new expression, mark left operand as analyzed to
5262 -- avoid infinite recursion adding predicate calls. Similarly,
5263 -- suppress further range checks on the call.
5265 Set_Analyzed (Left_Opnd (N));
5266 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5268 -- All done, skip attempt at compile time determination of result
5275 --------------------------------
5276 -- Expand_N_Indexed_Component --
5277 --------------------------------
5279 procedure Expand_N_Indexed_Component (N : Node_Id) is
5280 Loc : constant Source_Ptr := Sloc (N);
5281 Typ : constant Entity_Id := Etype (N);
5282 P : constant Node_Id := Prefix (N);
5283 T : constant Entity_Id := Etype (P);
5287 -- A special optimization, if we have an indexed component that is
5288 -- selecting from a slice, then we can eliminate the slice, since, for
5289 -- example, x (i .. j)(k) is identical to x(k). The only difference is
5290 -- the range check required by the slice. The range check for the slice
5291 -- itself has already been generated. The range check for the
5292 -- subscripting operation is ensured by converting the subject to
5293 -- the subtype of the slice.
5295 -- This optimization not only generates better code, avoiding slice
5296 -- messing especially in the packed case, but more importantly bypasses
5297 -- some problems in handling this peculiar case, for example, the issue
5298 -- of dealing specially with object renamings.
5300 if Nkind (P) = N_Slice then
5302 Make_Indexed_Component (Loc,
5303 Prefix => Prefix (P),
5304 Expressions => New_List (
5306 (Etype (First_Index (Etype (P))),
5307 First (Expressions (N))))));
5308 Analyze_And_Resolve (N, Typ);
5312 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
5313 -- function, then additional actuals must be passed.
5315 if Ada_Version >= Ada_2005
5316 and then Is_Build_In_Place_Function_Call (P)
5318 Make_Build_In_Place_Call_In_Anonymous_Context (P);
5321 -- If the prefix is an access type, then we unconditionally rewrite if
5322 -- as an explicit dereference. This simplifies processing for several
5323 -- cases, including packed array cases and certain cases in which checks
5324 -- must be generated. We used to try to do this only when it was
5325 -- necessary, but it cleans up the code to do it all the time.
5327 if Is_Access_Type (T) then
5328 Insert_Explicit_Dereference (P);
5329 Analyze_And_Resolve (P, Designated_Type (T));
5330 Atp := Designated_Type (T);
5335 -- Generate index and validity checks
5337 Generate_Index_Checks (N);
5339 if Validity_Checks_On and then Validity_Check_Subscripts then
5340 Apply_Subscript_Validity_Checks (N);
5343 -- If selecting from an array with atomic components, and atomic sync
5344 -- is not suppressed for this array type, set atomic sync flag.
5346 if (Has_Atomic_Components (Atp)
5347 and then not Atomic_Synchronization_Disabled (Atp))
5348 or else (Is_Atomic (Typ)
5349 and then not Atomic_Synchronization_Disabled (Typ))
5351 Activate_Atomic_Synchronization (N);
5354 -- All done for the non-packed case
5356 if not Is_Packed (Etype (Prefix (N))) then
5360 -- For packed arrays that are not bit-packed (i.e. the case of an array
5361 -- with one or more index types with a non-contiguous enumeration type),
5362 -- we can always use the normal packed element get circuit.
5364 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
5365 Expand_Packed_Element_Reference (N);
5369 -- For a reference to a component of a bit packed array, we have to
5370 -- convert it to a reference to the corresponding Packed_Array_Type.
5371 -- We only want to do this for simple references, and not for:
5373 -- Left side of assignment, or prefix of left side of assignment, or
5374 -- prefix of the prefix, to handle packed arrays of packed arrays,
5375 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
5377 -- Renaming objects in renaming associations
5378 -- This case is handled when a use of the renamed variable occurs
5380 -- Actual parameters for a procedure call
5381 -- This case is handled in Exp_Ch6.Expand_Actuals
5383 -- The second expression in a 'Read attribute reference
5385 -- The prefix of an address or bit or size attribute reference
5387 -- The following circuit detects these exceptions
5390 Child : Node_Id := N;
5391 Parnt : Node_Id := Parent (N);
5395 if Nkind (Parnt) = N_Unchecked_Expression then
5398 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
5399 N_Procedure_Call_Statement)
5400 or else (Nkind (Parnt) = N_Parameter_Association
5402 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
5406 elsif Nkind (Parnt) = N_Attribute_Reference
5407 and then (Attribute_Name (Parnt) = Name_Address
5409 Attribute_Name (Parnt) = Name_Bit
5411 Attribute_Name (Parnt) = Name_Size)
5412 and then Prefix (Parnt) = Child
5416 elsif Nkind (Parnt) = N_Assignment_Statement
5417 and then Name (Parnt) = Child
5421 -- If the expression is an index of an indexed component, it must
5422 -- be expanded regardless of context.
5424 elsif Nkind (Parnt) = N_Indexed_Component
5425 and then Child /= Prefix (Parnt)
5427 Expand_Packed_Element_Reference (N);
5430 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
5431 and then Name (Parent (Parnt)) = Parnt
5435 elsif Nkind (Parnt) = N_Attribute_Reference
5436 and then Attribute_Name (Parnt) = Name_Read
5437 and then Next (First (Expressions (Parnt))) = Child
5441 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
5442 and then Prefix (Parnt) = Child
5447 Expand_Packed_Element_Reference (N);
5451 -- Keep looking up tree for unchecked expression, or if we are the
5452 -- prefix of a possible assignment left side.
5455 Parnt := Parent (Child);
5458 end Expand_N_Indexed_Component;
5460 ---------------------
5461 -- Expand_N_Not_In --
5462 ---------------------
5464 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
5465 -- can be done. This avoids needing to duplicate this expansion code.
5467 procedure Expand_N_Not_In (N : Node_Id) is
5468 Loc : constant Source_Ptr := Sloc (N);
5469 Typ : constant Entity_Id := Etype (N);
5470 Cfs : constant Boolean := Comes_From_Source (N);
5477 Left_Opnd => Left_Opnd (N),
5478 Right_Opnd => Right_Opnd (N))));
5480 -- If this is a set membership, preserve list of alternatives
5482 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
5484 -- We want this to appear as coming from source if original does (see
5485 -- transformations in Expand_N_In).
5487 Set_Comes_From_Source (N, Cfs);
5488 Set_Comes_From_Source (Right_Opnd (N), Cfs);
5490 -- Now analyze transformed node
5492 Analyze_And_Resolve (N, Typ);
5493 end Expand_N_Not_In;
5499 -- The only replacement required is for the case of a null of a type that
5500 -- is an access to protected subprogram, or a subtype thereof. We represent
5501 -- such access values as a record, and so we must replace the occurrence of
5502 -- null by the equivalent record (with a null address and a null pointer in
5503 -- it), so that the backend creates the proper value.
5505 procedure Expand_N_Null (N : Node_Id) is
5506 Loc : constant Source_Ptr := Sloc (N);
5507 Typ : constant Entity_Id := Base_Type (Etype (N));
5511 if Is_Access_Protected_Subprogram_Type (Typ) then
5513 Make_Aggregate (Loc,
5514 Expressions => New_List (
5515 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
5519 Analyze_And_Resolve (N, Equivalent_Type (Typ));
5521 -- For subsequent semantic analysis, the node must retain its type.
5522 -- Gigi in any case replaces this type by the corresponding record
5523 -- type before processing the node.
5529 when RE_Not_Available =>
5533 ---------------------
5534 -- Expand_N_Op_Abs --
5535 ---------------------
5537 procedure Expand_N_Op_Abs (N : Node_Id) is
5538 Loc : constant Source_Ptr := Sloc (N);
5539 Expr : constant Node_Id := Right_Opnd (N);
5542 Unary_Op_Validity_Checks (N);
5544 -- Deal with software overflow checking
5546 if not Backend_Overflow_Checks_On_Target
5547 and then Is_Signed_Integer_Type (Etype (N))
5548 and then Do_Overflow_Check (N)
5550 -- The only case to worry about is when the argument is equal to the
5551 -- largest negative number, so what we do is to insert the check:
5553 -- [constraint_error when Expr = typ'Base'First]
5555 -- with the usual Duplicate_Subexpr use coding for expr
5558 Make_Raise_Constraint_Error (Loc,
5561 Left_Opnd => Duplicate_Subexpr (Expr),
5563 Make_Attribute_Reference (Loc,
5565 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
5566 Attribute_Name => Name_First)),
5567 Reason => CE_Overflow_Check_Failed));
5570 -- Vax floating-point types case
5572 if Vax_Float (Etype (N)) then
5573 Expand_Vax_Arith (N);
5575 end Expand_N_Op_Abs;
5577 ---------------------
5578 -- Expand_N_Op_Add --
5579 ---------------------
5581 procedure Expand_N_Op_Add (N : Node_Id) is
5582 Typ : constant Entity_Id := Etype (N);
5585 Binary_Op_Validity_Checks (N);
5587 -- N + 0 = 0 + N = N for integer types
5589 if Is_Integer_Type (Typ) then
5590 if Compile_Time_Known_Value (Right_Opnd (N))
5591 and then Expr_Value (Right_Opnd (N)) = Uint_0
5593 Rewrite (N, Left_Opnd (N));
5596 elsif Compile_Time_Known_Value (Left_Opnd (N))
5597 and then Expr_Value (Left_Opnd (N)) = Uint_0
5599 Rewrite (N, Right_Opnd (N));
5604 -- Arithmetic overflow checks for signed integer/fixed point types
5606 if Is_Signed_Integer_Type (Typ)
5607 or else Is_Fixed_Point_Type (Typ)
5609 Apply_Arithmetic_Overflow_Check (N);
5612 -- Vax floating-point types case
5614 elsif Vax_Float (Typ) then
5615 Expand_Vax_Arith (N);
5617 end Expand_N_Op_Add;
5619 ---------------------
5620 -- Expand_N_Op_And --
5621 ---------------------
5623 procedure Expand_N_Op_And (N : Node_Id) is
5624 Typ : constant Entity_Id := Etype (N);
5627 Binary_Op_Validity_Checks (N);
5629 if Is_Array_Type (Etype (N)) then
5630 Expand_Boolean_Operator (N);
5632 elsif Is_Boolean_Type (Etype (N)) then
5633 Adjust_Condition (Left_Opnd (N));
5634 Adjust_Condition (Right_Opnd (N));
5635 Set_Etype (N, Standard_Boolean);
5636 Adjust_Result_Type (N, Typ);
5638 elsif Is_Intrinsic_Subprogram (Entity (N)) then
5639 Expand_Intrinsic_Call (N, Entity (N));
5642 end Expand_N_Op_And;
5644 ------------------------
5645 -- Expand_N_Op_Concat --
5646 ------------------------
5648 procedure Expand_N_Op_Concat (N : Node_Id) is
5650 -- List of operands to be concatenated
5653 -- Node which is to be replaced by the result of concatenating the nodes
5654 -- in the list Opnds.
5657 -- Ensure validity of both operands
5659 Binary_Op_Validity_Checks (N);
5661 -- If we are the left operand of a concatenation higher up the tree,
5662 -- then do nothing for now, since we want to deal with a series of
5663 -- concatenations as a unit.
5665 if Nkind (Parent (N)) = N_Op_Concat
5666 and then N = Left_Opnd (Parent (N))
5671 -- We get here with a concatenation whose left operand may be a
5672 -- concatenation itself with a consistent type. We need to process
5673 -- these concatenation operands from left to right, which means
5674 -- from the deepest node in the tree to the highest node.
5677 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
5678 Cnode := Left_Opnd (Cnode);
5681 -- Now Cnode is the deepest concatenation, and its parents are the
5682 -- concatenation nodes above, so now we process bottom up, doing the
5683 -- operations. We gather a string that is as long as possible up to five
5686 -- The outer loop runs more than once if more than one concatenation
5687 -- type is involved.
5690 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
5691 Set_Parent (Opnds, N);
5693 -- The inner loop gathers concatenation operands
5695 Inner : while Cnode /= N
5696 and then Base_Type (Etype (Cnode)) =
5697 Base_Type (Etype (Parent (Cnode)))
5699 Cnode := Parent (Cnode);
5700 Append (Right_Opnd (Cnode), Opnds);
5703 Expand_Concatenate (Cnode, Opnds);
5705 exit Outer when Cnode = N;
5706 Cnode := Parent (Cnode);
5708 end Expand_N_Op_Concat;
5710 ------------------------
5711 -- Expand_N_Op_Divide --
5712 ------------------------
5714 procedure Expand_N_Op_Divide (N : Node_Id) is
5715 Loc : constant Source_Ptr := Sloc (N);
5716 Lopnd : constant Node_Id := Left_Opnd (N);
5717 Ropnd : constant Node_Id := Right_Opnd (N);
5718 Ltyp : constant Entity_Id := Etype (Lopnd);
5719 Rtyp : constant Entity_Id := Etype (Ropnd);
5720 Typ : Entity_Id := Etype (N);
5721 Rknow : constant Boolean := Is_Integer_Type (Typ)
5723 Compile_Time_Known_Value (Ropnd);
5727 Binary_Op_Validity_Checks (N);
5730 Rval := Expr_Value (Ropnd);
5733 -- N / 1 = N for integer types
5735 if Rknow and then Rval = Uint_1 then
5740 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
5741 -- Is_Power_Of_2_For_Shift is set means that we know that our left
5742 -- operand is an unsigned integer, as required for this to work.
5744 if Nkind (Ropnd) = N_Op_Expon
5745 and then Is_Power_Of_2_For_Shift (Ropnd)
5747 -- We cannot do this transformation in configurable run time mode if we
5748 -- have 64-bit integers and long shifts are not available.
5752 or else Support_Long_Shifts_On_Target)
5755 Make_Op_Shift_Right (Loc,
5758 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
5759 Analyze_And_Resolve (N, Typ);
5763 -- Do required fixup of universal fixed operation
5765 if Typ = Universal_Fixed then
5766 Fixup_Universal_Fixed_Operation (N);
5770 -- Divisions with fixed-point results
5772 if Is_Fixed_Point_Type (Typ) then
5774 -- No special processing if Treat_Fixed_As_Integer is set, since
5775 -- from a semantic point of view such operations are simply integer
5776 -- operations and will be treated that way.
5778 if not Treat_Fixed_As_Integer (N) then
5779 if Is_Integer_Type (Rtyp) then
5780 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
5782 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
5786 -- Other cases of division of fixed-point operands. Again we exclude the
5787 -- case where Treat_Fixed_As_Integer is set.
5789 elsif (Is_Fixed_Point_Type (Ltyp) or else
5790 Is_Fixed_Point_Type (Rtyp))
5791 and then not Treat_Fixed_As_Integer (N)
5793 if Is_Integer_Type (Typ) then
5794 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
5796 pragma Assert (Is_Floating_Point_Type (Typ));
5797 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
5800 -- Mixed-mode operations can appear in a non-static universal context,
5801 -- in which case the integer argument must be converted explicitly.
5803 elsif Typ = Universal_Real
5804 and then Is_Integer_Type (Rtyp)
5807 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
5809 Analyze_And_Resolve (Ropnd, Universal_Real);
5811 elsif Typ = Universal_Real
5812 and then Is_Integer_Type (Ltyp)
5815 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
5817 Analyze_And_Resolve (Lopnd, Universal_Real);
5819 -- Non-fixed point cases, do integer zero divide and overflow checks
5821 elsif Is_Integer_Type (Typ) then
5822 Apply_Divide_Check (N);
5824 -- Deal with Vax_Float
5826 elsif Vax_Float (Typ) then
5827 Expand_Vax_Arith (N);
5830 end Expand_N_Op_Divide;
5832 --------------------
5833 -- Expand_N_Op_Eq --
5834 --------------------
5836 procedure Expand_N_Op_Eq (N : Node_Id) is
5837 Loc : constant Source_Ptr := Sloc (N);
5838 Typ : constant Entity_Id := Etype (N);
5839 Lhs : constant Node_Id := Left_Opnd (N);
5840 Rhs : constant Node_Id := Right_Opnd (N);
5841 Bodies : constant List_Id := New_List;
5842 A_Typ : constant Entity_Id := Etype (Lhs);
5844 Typl : Entity_Id := A_Typ;
5845 Op_Name : Entity_Id;
5848 procedure Build_Equality_Call (Eq : Entity_Id);
5849 -- If a constructed equality exists for the type or for its parent,
5850 -- build and analyze call, adding conversions if the operation is
5853 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
5854 -- Determines whether a type has a subcomponent of an unconstrained
5855 -- Unchecked_Union subtype. Typ is a record type.
5857 -------------------------
5858 -- Build_Equality_Call --
5859 -------------------------
5861 procedure Build_Equality_Call (Eq : Entity_Id) is
5862 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
5863 L_Exp : Node_Id := Relocate_Node (Lhs);
5864 R_Exp : Node_Id := Relocate_Node (Rhs);
5867 if Base_Type (Op_Type) /= Base_Type (A_Typ)
5868 and then not Is_Class_Wide_Type (A_Typ)
5870 L_Exp := OK_Convert_To (Op_Type, L_Exp);
5871 R_Exp := OK_Convert_To (Op_Type, R_Exp);
5874 -- If we have an Unchecked_Union, we need to add the inferred
5875 -- discriminant values as actuals in the function call. At this
5876 -- point, the expansion has determined that both operands have
5877 -- inferable discriminants.
5879 if Is_Unchecked_Union (Op_Type) then
5881 Lhs_Type : constant Node_Id := Etype (L_Exp);
5882 Rhs_Type : constant Node_Id := Etype (R_Exp);
5883 Lhs_Discr_Val : Node_Id;
5884 Rhs_Discr_Val : Node_Id;
5887 -- Per-object constrained selected components require special
5888 -- attention. If the enclosing scope of the component is an
5889 -- Unchecked_Union, we cannot reference its discriminants
5890 -- directly. This is why we use the two extra parameters of
5891 -- the equality function of the enclosing Unchecked_Union.
5893 -- type UU_Type (Discr : Integer := 0) is
5896 -- pragma Unchecked_Union (UU_Type);
5898 -- 1. Unchecked_Union enclosing record:
5900 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
5902 -- Comp : UU_Type (Discr);
5904 -- end Enclosing_UU_Type;
5905 -- pragma Unchecked_Union (Enclosing_UU_Type);
5907 -- Obj1 : Enclosing_UU_Type;
5908 -- Obj2 : Enclosing_UU_Type (1);
5910 -- [. . .] Obj1 = Obj2 [. . .]
5914 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
5916 -- A and B are the formal parameters of the equality function
5917 -- of Enclosing_UU_Type. The function always has two extra
5918 -- formals to capture the inferred discriminant values.
5920 -- 2. Non-Unchecked_Union enclosing record:
5923 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
5926 -- Comp : UU_Type (Discr);
5928 -- end Enclosing_Non_UU_Type;
5930 -- Obj1 : Enclosing_Non_UU_Type;
5931 -- Obj2 : Enclosing_Non_UU_Type (1);
5933 -- ... Obj1 = Obj2 ...
5937 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
5938 -- obj1.discr, obj2.discr)) then
5940 -- In this case we can directly reference the discriminants of
5941 -- the enclosing record.
5945 if Nkind (Lhs) = N_Selected_Component
5946 and then Has_Per_Object_Constraint
5947 (Entity (Selector_Name (Lhs)))
5949 -- Enclosing record is an Unchecked_Union, use formal A
5951 if Is_Unchecked_Union
5952 (Scope (Entity (Selector_Name (Lhs))))
5954 Lhs_Discr_Val := Make_Identifier (Loc, Name_A);
5956 -- Enclosing record is of a non-Unchecked_Union type, it is
5957 -- possible to reference the discriminant.
5961 Make_Selected_Component (Loc,
5962 Prefix => Prefix (Lhs),
5965 (Get_Discriminant_Value
5966 (First_Discriminant (Lhs_Type),
5968 Stored_Constraint (Lhs_Type))));
5971 -- Comment needed here ???
5974 -- Infer the discriminant value
5978 (Get_Discriminant_Value
5979 (First_Discriminant (Lhs_Type),
5981 Stored_Constraint (Lhs_Type)));
5986 if Nkind (Rhs) = N_Selected_Component
5987 and then Has_Per_Object_Constraint
5988 (Entity (Selector_Name (Rhs)))
5990 if Is_Unchecked_Union
5991 (Scope (Entity (Selector_Name (Rhs))))
5993 Rhs_Discr_Val := Make_Identifier (Loc, Name_B);
5997 Make_Selected_Component (Loc,
5998 Prefix => Prefix (Rhs),
6000 New_Copy (Get_Discriminant_Value (
6001 First_Discriminant (Rhs_Type),
6003 Stored_Constraint (Rhs_Type))));
6008 New_Copy (Get_Discriminant_Value (
6009 First_Discriminant (Rhs_Type),
6011 Stored_Constraint (Rhs_Type)));
6016 Make_Function_Call (Loc,
6017 Name => New_Reference_To (Eq, Loc),
6018 Parameter_Associations => New_List (
6025 -- Normal case, not an unchecked union
6029 Make_Function_Call (Loc,
6030 Name => New_Reference_To (Eq, Loc),
6031 Parameter_Associations => New_List (L_Exp, R_Exp)));
6034 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6035 end Build_Equality_Call;
6037 ------------------------------------
6038 -- Has_Unconstrained_UU_Component --
6039 ------------------------------------
6041 function Has_Unconstrained_UU_Component
6042 (Typ : Node_Id) return Boolean
6044 Tdef : constant Node_Id :=
6045 Type_Definition (Declaration_Node (Base_Type (Typ)));
6049 function Component_Is_Unconstrained_UU
6050 (Comp : Node_Id) return Boolean;
6051 -- Determines whether the subtype of the component is an
6052 -- unconstrained Unchecked_Union.
6054 function Variant_Is_Unconstrained_UU
6055 (Variant : Node_Id) return Boolean;
6056 -- Determines whether a component of the variant has an unconstrained
6057 -- Unchecked_Union subtype.
6059 -----------------------------------
6060 -- Component_Is_Unconstrained_UU --
6061 -----------------------------------
6063 function Component_Is_Unconstrained_UU
6064 (Comp : Node_Id) return Boolean
6067 if Nkind (Comp) /= N_Component_Declaration then
6072 Sindic : constant Node_Id :=
6073 Subtype_Indication (Component_Definition (Comp));
6076 -- Unconstrained nominal type. In the case of a constraint
6077 -- present, the node kind would have been N_Subtype_Indication.
6079 if Nkind (Sindic) = N_Identifier then
6080 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
6085 end Component_Is_Unconstrained_UU;
6087 ---------------------------------
6088 -- Variant_Is_Unconstrained_UU --
6089 ---------------------------------
6091 function Variant_Is_Unconstrained_UU
6092 (Variant : Node_Id) return Boolean
6094 Clist : constant Node_Id := Component_List (Variant);
6097 if Is_Empty_List (Component_Items (Clist)) then
6101 -- We only need to test one component
6104 Comp : Node_Id := First (Component_Items (Clist));
6107 while Present (Comp) loop
6108 if Component_Is_Unconstrained_UU (Comp) then
6116 -- None of the components withing the variant were of
6117 -- unconstrained Unchecked_Union type.
6120 end Variant_Is_Unconstrained_UU;
6122 -- Start of processing for Has_Unconstrained_UU_Component
6125 if Null_Present (Tdef) then
6129 Clist := Component_List (Tdef);
6130 Vpart := Variant_Part (Clist);
6132 -- Inspect available components
6134 if Present (Component_Items (Clist)) then
6136 Comp : Node_Id := First (Component_Items (Clist));
6139 while Present (Comp) loop
6141 -- One component is sufficient
6143 if Component_Is_Unconstrained_UU (Comp) then
6152 -- Inspect available components withing variants
6154 if Present (Vpart) then
6156 Variant : Node_Id := First (Variants (Vpart));
6159 while Present (Variant) loop
6161 -- One component within a variant is sufficient
6163 if Variant_Is_Unconstrained_UU (Variant) then
6172 -- Neither the available components, nor the components inside the
6173 -- variant parts were of an unconstrained Unchecked_Union subtype.
6176 end Has_Unconstrained_UU_Component;
6178 -- Start of processing for Expand_N_Op_Eq
6181 Binary_Op_Validity_Checks (N);
6183 if Ekind (Typl) = E_Private_Type then
6184 Typl := Underlying_Type (Typl);
6185 elsif Ekind (Typl) = E_Private_Subtype then
6186 Typl := Underlying_Type (Base_Type (Typl));
6191 -- It may happen in error situations that the underlying type is not
6192 -- set. The error will be detected later, here we just defend the
6199 Typl := Base_Type (Typl);
6201 -- Boolean types (requiring handling of non-standard case)
6203 if Is_Boolean_Type (Typl) then
6204 Adjust_Condition (Left_Opnd (N));
6205 Adjust_Condition (Right_Opnd (N));
6206 Set_Etype (N, Standard_Boolean);
6207 Adjust_Result_Type (N, Typ);
6211 elsif Is_Array_Type (Typl) then
6213 -- If we are doing full validity checking, and it is possible for the
6214 -- array elements to be invalid then expand out array comparisons to
6215 -- make sure that we check the array elements.
6217 if Validity_Check_Operands
6218 and then not Is_Known_Valid (Component_Type (Typl))
6221 Save_Force_Validity_Checks : constant Boolean :=
6222 Force_Validity_Checks;
6224 Force_Validity_Checks := True;
6226 Expand_Array_Equality
6228 Relocate_Node (Lhs),
6229 Relocate_Node (Rhs),
6232 Insert_Actions (N, Bodies);
6233 Analyze_And_Resolve (N, Standard_Boolean);
6234 Force_Validity_Checks := Save_Force_Validity_Checks;
6237 -- Packed case where both operands are known aligned
6239 elsif Is_Bit_Packed_Array (Typl)
6240 and then not Is_Possibly_Unaligned_Object (Lhs)
6241 and then not Is_Possibly_Unaligned_Object (Rhs)
6243 Expand_Packed_Eq (N);
6245 -- Where the component type is elementary we can use a block bit
6246 -- comparison (if supported on the target) exception in the case
6247 -- of floating-point (negative zero issues require element by
6248 -- element comparison), and atomic types (where we must be sure
6249 -- to load elements independently) and possibly unaligned arrays.
6251 elsif Is_Elementary_Type (Component_Type (Typl))
6252 and then not Is_Floating_Point_Type (Component_Type (Typl))
6253 and then not Is_Atomic (Component_Type (Typl))
6254 and then not Is_Possibly_Unaligned_Object (Lhs)
6255 and then not Is_Possibly_Unaligned_Object (Rhs)
6256 and then Support_Composite_Compare_On_Target
6260 -- For composite and floating-point cases, expand equality loop to
6261 -- make sure of using proper comparisons for tagged types, and
6262 -- correctly handling the floating-point case.
6266 Expand_Array_Equality
6268 Relocate_Node (Lhs),
6269 Relocate_Node (Rhs),
6272 Insert_Actions (N, Bodies, Suppress => All_Checks);
6273 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6278 elsif Is_Record_Type (Typl) then
6280 -- For tagged types, use the primitive "="
6282 if Is_Tagged_Type (Typl) then
6284 -- No need to do anything else compiling under restriction
6285 -- No_Dispatching_Calls. During the semantic analysis we
6286 -- already notified such violation.
6288 if Restriction_Active (No_Dispatching_Calls) then
6292 -- If this is derived from an untagged private type completed with
6293 -- a tagged type, it does not have a full view, so we use the
6294 -- primitive operations of the private type. This check should no
6295 -- longer be necessary when these types get their full views???
6297 if Is_Private_Type (A_Typ)
6298 and then not Is_Tagged_Type (A_Typ)
6299 and then Is_Derived_Type (A_Typ)
6300 and then No (Full_View (A_Typ))
6302 -- Search for equality operation, checking that the operands
6303 -- have the same type. Note that we must find a matching entry,
6304 -- or something is very wrong!
6306 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
6308 while Present (Prim) loop
6309 exit when Chars (Node (Prim)) = Name_Op_Eq
6310 and then Etype (First_Formal (Node (Prim))) =
6311 Etype (Next_Formal (First_Formal (Node (Prim))))
6313 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
6318 pragma Assert (Present (Prim));
6319 Op_Name := Node (Prim);
6321 -- Find the type's predefined equality or an overriding
6322 -- user- defined equality. The reason for not simply calling
6323 -- Find_Prim_Op here is that there may be a user-defined
6324 -- overloaded equality op that precedes the equality that we want,
6325 -- so we have to explicitly search (e.g., there could be an
6326 -- equality with two different parameter types).
6329 if Is_Class_Wide_Type (Typl) then
6330 Typl := Root_Type (Typl);
6333 Prim := First_Elmt (Primitive_Operations (Typl));
6334 while Present (Prim) loop
6335 exit when Chars (Node (Prim)) = Name_Op_Eq
6336 and then Etype (First_Formal (Node (Prim))) =
6337 Etype (Next_Formal (First_Formal (Node (Prim))))
6339 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
6344 pragma Assert (Present (Prim));
6345 Op_Name := Node (Prim);
6348 Build_Equality_Call (Op_Name);
6350 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
6351 -- predefined equality operator for a type which has a subcomponent
6352 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
6354 elsif Has_Unconstrained_UU_Component (Typl) then
6356 Make_Raise_Program_Error (Loc,
6357 Reason => PE_Unchecked_Union_Restriction));
6359 -- Prevent Gigi from generating incorrect code by rewriting the
6360 -- equality as a standard False.
6363 New_Occurrence_Of (Standard_False, Loc));
6365 elsif Is_Unchecked_Union (Typl) then
6367 -- If we can infer the discriminants of the operands, we make a
6368 -- call to the TSS equality function.
6370 if Has_Inferable_Discriminants (Lhs)
6372 Has_Inferable_Discriminants (Rhs)
6375 (TSS (Root_Type (Typl), TSS_Composite_Equality));
6378 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
6379 -- the predefined equality operator for an Unchecked_Union type
6380 -- if either of the operands lack inferable discriminants.
6383 Make_Raise_Program_Error (Loc,
6384 Reason => PE_Unchecked_Union_Restriction));
6386 -- Prevent Gigi from generating incorrect code by rewriting
6387 -- the equality as a standard False.
6390 New_Occurrence_Of (Standard_False, Loc));
6394 -- If a type support function is present (for complex cases), use it
6396 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
6398 (TSS (Root_Type (Typl), TSS_Composite_Equality));
6400 -- Otherwise expand the component by component equality. Note that
6401 -- we never use block-bit comparisons for records, because of the
6402 -- problems with gaps. The backend will often be able to recombine
6403 -- the separate comparisons that we generate here.
6406 Remove_Side_Effects (Lhs);
6407 Remove_Side_Effects (Rhs);
6409 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
6411 Insert_Actions (N, Bodies, Suppress => All_Checks);
6412 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6416 -- Test if result is known at compile time
6418 Rewrite_Comparison (N);
6420 -- If we still have comparison for Vax_Float, process it
6422 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
6423 Expand_Vax_Comparison (N);
6427 Optimize_Length_Comparison (N);
6430 -----------------------
6431 -- Expand_N_Op_Expon --
6432 -----------------------
6434 procedure Expand_N_Op_Expon (N : Node_Id) is
6435 Loc : constant Source_Ptr := Sloc (N);
6436 Typ : constant Entity_Id := Etype (N);
6437 Rtyp : constant Entity_Id := Root_Type (Typ);
6438 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
6439 Bastyp : constant Node_Id := Etype (Base);
6440 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
6441 Exptyp : constant Entity_Id := Etype (Exp);
6442 Ovflo : constant Boolean := Do_Overflow_Check (N);
6451 Binary_Op_Validity_Checks (N);
6453 -- CodePeer and GNATprove want to see the unexpanded N_Op_Expon node
6455 if CodePeer_Mode or Alfa_Mode then
6459 -- If either operand is of a private type, then we have the use of an
6460 -- intrinsic operator, and we get rid of the privateness, by using root
6461 -- types of underlying types for the actual operation. Otherwise the
6462 -- private types will cause trouble if we expand multiplications or
6463 -- shifts etc. We also do this transformation if the result type is
6464 -- different from the base type.
6466 if Is_Private_Type (Etype (Base))
6467 or else Is_Private_Type (Typ)
6468 or else Is_Private_Type (Exptyp)
6469 or else Rtyp /= Root_Type (Bastyp)
6472 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
6473 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
6477 Unchecked_Convert_To (Typ,
6479 Left_Opnd => Unchecked_Convert_To (Bt, Base),
6480 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
6481 Analyze_And_Resolve (N, Typ);
6486 -- Test for case of known right argument
6488 if Compile_Time_Known_Value (Exp) then
6489 Expv := Expr_Value (Exp);
6491 -- We only fold small non-negative exponents. You might think we
6492 -- could fold small negative exponents for the real case, but we
6493 -- can't because we are required to raise Constraint_Error for
6494 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
6495 -- See ACVC test C4A012B.
6497 if Expv >= 0 and then Expv <= 4 then
6499 -- X ** 0 = 1 (or 1.0)
6503 -- Call Remove_Side_Effects to ensure that any side effects
6504 -- in the ignored left operand (in particular function calls
6505 -- to user defined functions) are properly executed.
6507 Remove_Side_Effects (Base);
6509 if Ekind (Typ) in Integer_Kind then
6510 Xnode := Make_Integer_Literal (Loc, Intval => 1);
6512 Xnode := Make_Real_Literal (Loc, Ureal_1);
6524 Make_Op_Multiply (Loc,
6525 Left_Opnd => Duplicate_Subexpr (Base),
6526 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
6528 -- X ** 3 = X * X * X
6532 Make_Op_Multiply (Loc,
6534 Make_Op_Multiply (Loc,
6535 Left_Opnd => Duplicate_Subexpr (Base),
6536 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
6537 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
6540 -- En : constant base'type := base * base;
6545 Temp := Make_Temporary (Loc, 'E', Base);
6547 Insert_Actions (N, New_List (
6548 Make_Object_Declaration (Loc,
6549 Defining_Identifier => Temp,
6550 Constant_Present => True,
6551 Object_Definition => New_Reference_To (Typ, Loc),
6553 Make_Op_Multiply (Loc,
6554 Left_Opnd => Duplicate_Subexpr (Base),
6555 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
6558 Make_Op_Multiply (Loc,
6559 Left_Opnd => New_Reference_To (Temp, Loc),
6560 Right_Opnd => New_Reference_To (Temp, Loc));
6564 Analyze_And_Resolve (N, Typ);
6569 -- Case of (2 ** expression) appearing as an argument of an integer
6570 -- multiplication, or as the right argument of a division of a non-
6571 -- negative integer. In such cases we leave the node untouched, setting
6572 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
6573 -- of the higher level node converts it into a shift.
6575 -- Another case is 2 ** N in any other context. We simply convert
6576 -- this to 1 * 2 ** N, and then the above transformation applies.
6578 -- Note: this transformation is not applicable for a modular type with
6579 -- a non-binary modulus in the multiplication case, since we get a wrong
6580 -- result if the shift causes an overflow before the modular reduction.
6582 if Nkind (Base) = N_Integer_Literal
6583 and then Intval (Base) = 2
6584 and then Is_Integer_Type (Root_Type (Exptyp))
6585 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
6586 and then Is_Unsigned_Type (Exptyp)
6589 -- First the multiply and divide cases
6591 if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then
6593 P : constant Node_Id := Parent (N);
6594 L : constant Node_Id := Left_Opnd (P);
6595 R : constant Node_Id := Right_Opnd (P);
6598 if (Nkind (P) = N_Op_Multiply
6599 and then not Non_Binary_Modulus (Typ)
6601 ((Is_Integer_Type (Etype (L)) and then R = N)
6603 (Is_Integer_Type (Etype (R)) and then L = N))
6604 and then not Do_Overflow_Check (P))
6606 (Nkind (P) = N_Op_Divide
6607 and then Is_Integer_Type (Etype (L))
6608 and then Is_Unsigned_Type (Etype (L))
6610 and then not Do_Overflow_Check (P))
6612 Set_Is_Power_Of_2_For_Shift (N);
6617 -- Now the other cases
6619 elsif not Non_Binary_Modulus (Typ) then
6621 Make_Op_Multiply (Loc,
6622 Left_Opnd => Make_Integer_Literal (Loc, 1),
6623 Right_Opnd => Relocate_Node (N)));
6624 Analyze_And_Resolve (N, Typ);
6629 -- Fall through if exponentiation must be done using a runtime routine
6631 -- First deal with modular case
6633 if Is_Modular_Integer_Type (Rtyp) then
6635 -- Non-binary case, we call the special exponentiation routine for
6636 -- the non-binary case, converting the argument to Long_Long_Integer
6637 -- and passing the modulus value. Then the result is converted back
6638 -- to the base type.
6640 if Non_Binary_Modulus (Rtyp) then
6643 Make_Function_Call (Loc,
6644 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
6645 Parameter_Associations => New_List (
6646 Convert_To (Standard_Integer, Base),
6647 Make_Integer_Literal (Loc, Modulus (Rtyp)),
6650 -- Binary case, in this case, we call one of two routines, either the
6651 -- unsigned integer case, or the unsigned long long integer case,
6652 -- with a final "and" operation to do the required mod.
6655 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
6656 Ent := RTE (RE_Exp_Unsigned);
6658 Ent := RTE (RE_Exp_Long_Long_Unsigned);
6665 Make_Function_Call (Loc,
6666 Name => New_Reference_To (Ent, Loc),
6667 Parameter_Associations => New_List (
6668 Convert_To (Etype (First_Formal (Ent)), Base),
6671 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
6675 -- Common exit point for modular type case
6677 Analyze_And_Resolve (N, Typ);
6680 -- Signed integer cases, done using either Integer or Long_Long_Integer.
6681 -- It is not worth having routines for Short_[Short_]Integer, since for
6682 -- most machines it would not help, and it would generate more code that
6683 -- might need certification when a certified run time is required.
6685 -- In the integer cases, we have two routines, one for when overflow
6686 -- checks are required, and one when they are not required, since there
6687 -- is a real gain in omitting checks on many machines.
6689 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
6690 or else (Rtyp = Base_Type (Standard_Long_Integer)
6692 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
6693 or else (Rtyp = Universal_Integer)
6695 Etyp := Standard_Long_Long_Integer;
6698 Rent := RE_Exp_Long_Long_Integer;
6700 Rent := RE_Exn_Long_Long_Integer;
6703 elsif Is_Signed_Integer_Type (Rtyp) then
6704 Etyp := Standard_Integer;
6707 Rent := RE_Exp_Integer;
6709 Rent := RE_Exn_Integer;
6712 -- Floating-point cases, always done using Long_Long_Float. We do not
6713 -- need separate routines for the overflow case here, since in the case
6714 -- of floating-point, we generate infinities anyway as a rule (either
6715 -- that or we automatically trap overflow), and if there is an infinity
6716 -- generated and a range check is required, the check will fail anyway.
6719 pragma Assert (Is_Floating_Point_Type (Rtyp));
6720 Etyp := Standard_Long_Long_Float;
6721 Rent := RE_Exn_Long_Long_Float;
6724 -- Common processing for integer cases and floating-point cases.
6725 -- If we are in the right type, we can call runtime routine directly
6728 and then Rtyp /= Universal_Integer
6729 and then Rtyp /= Universal_Real
6732 Make_Function_Call (Loc,
6733 Name => New_Reference_To (RTE (Rent), Loc),
6734 Parameter_Associations => New_List (Base, Exp)));
6736 -- Otherwise we have to introduce conversions (conversions are also
6737 -- required in the universal cases, since the runtime routine is
6738 -- typed using one of the standard types).
6743 Make_Function_Call (Loc,
6744 Name => New_Reference_To (RTE (Rent), Loc),
6745 Parameter_Associations => New_List (
6746 Convert_To (Etyp, Base),
6750 Analyze_And_Resolve (N, Typ);
6754 when RE_Not_Available =>
6756 end Expand_N_Op_Expon;
6758 --------------------
6759 -- Expand_N_Op_Ge --
6760 --------------------
6762 procedure Expand_N_Op_Ge (N : Node_Id) is
6763 Typ : constant Entity_Id := Etype (N);
6764 Op1 : constant Node_Id := Left_Opnd (N);
6765 Op2 : constant Node_Id := Right_Opnd (N);
6766 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6769 Binary_Op_Validity_Checks (N);
6771 if Is_Array_Type (Typ1) then
6772 Expand_Array_Comparison (N);
6776 if Is_Boolean_Type (Typ1) then
6777 Adjust_Condition (Op1);
6778 Adjust_Condition (Op2);
6779 Set_Etype (N, Standard_Boolean);
6780 Adjust_Result_Type (N, Typ);
6783 Rewrite_Comparison (N);
6785 -- If we still have comparison, and Vax_Float type, process it
6787 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6788 Expand_Vax_Comparison (N);
6792 Optimize_Length_Comparison (N);
6795 --------------------
6796 -- Expand_N_Op_Gt --
6797 --------------------
6799 procedure Expand_N_Op_Gt (N : Node_Id) is
6800 Typ : constant Entity_Id := Etype (N);
6801 Op1 : constant Node_Id := Left_Opnd (N);
6802 Op2 : constant Node_Id := Right_Opnd (N);
6803 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6806 Binary_Op_Validity_Checks (N);
6808 if Is_Array_Type (Typ1) then
6809 Expand_Array_Comparison (N);
6813 if Is_Boolean_Type (Typ1) then
6814 Adjust_Condition (Op1);
6815 Adjust_Condition (Op2);
6816 Set_Etype (N, Standard_Boolean);
6817 Adjust_Result_Type (N, Typ);
6820 Rewrite_Comparison (N);
6822 -- If we still have comparison, and Vax_Float type, process it
6824 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6825 Expand_Vax_Comparison (N);
6829 Optimize_Length_Comparison (N);
6832 --------------------
6833 -- Expand_N_Op_Le --
6834 --------------------
6836 procedure Expand_N_Op_Le (N : Node_Id) is
6837 Typ : constant Entity_Id := Etype (N);
6838 Op1 : constant Node_Id := Left_Opnd (N);
6839 Op2 : constant Node_Id := Right_Opnd (N);
6840 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6843 Binary_Op_Validity_Checks (N);
6845 if Is_Array_Type (Typ1) then
6846 Expand_Array_Comparison (N);
6850 if Is_Boolean_Type (Typ1) then
6851 Adjust_Condition (Op1);
6852 Adjust_Condition (Op2);
6853 Set_Etype (N, Standard_Boolean);
6854 Adjust_Result_Type (N, Typ);
6857 Rewrite_Comparison (N);
6859 -- If we still have comparison, and Vax_Float type, process it
6861 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6862 Expand_Vax_Comparison (N);
6866 Optimize_Length_Comparison (N);
6869 --------------------
6870 -- Expand_N_Op_Lt --
6871 --------------------
6873 procedure Expand_N_Op_Lt (N : Node_Id) is
6874 Typ : constant Entity_Id := Etype (N);
6875 Op1 : constant Node_Id := Left_Opnd (N);
6876 Op2 : constant Node_Id := Right_Opnd (N);
6877 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6880 Binary_Op_Validity_Checks (N);
6882 if Is_Array_Type (Typ1) then
6883 Expand_Array_Comparison (N);
6887 if Is_Boolean_Type (Typ1) then
6888 Adjust_Condition (Op1);
6889 Adjust_Condition (Op2);
6890 Set_Etype (N, Standard_Boolean);
6891 Adjust_Result_Type (N, Typ);
6894 Rewrite_Comparison (N);
6896 -- If we still have comparison, and Vax_Float type, process it
6898 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6899 Expand_Vax_Comparison (N);
6903 Optimize_Length_Comparison (N);
6906 -----------------------
6907 -- Expand_N_Op_Minus --
6908 -----------------------
6910 procedure Expand_N_Op_Minus (N : Node_Id) is
6911 Loc : constant Source_Ptr := Sloc (N);
6912 Typ : constant Entity_Id := Etype (N);
6915 Unary_Op_Validity_Checks (N);
6917 if not Backend_Overflow_Checks_On_Target
6918 and then Is_Signed_Integer_Type (Etype (N))
6919 and then Do_Overflow_Check (N)
6921 -- Software overflow checking expands -expr into (0 - expr)
6924 Make_Op_Subtract (Loc,
6925 Left_Opnd => Make_Integer_Literal (Loc, 0),
6926 Right_Opnd => Right_Opnd (N)));
6928 Analyze_And_Resolve (N, Typ);
6930 -- Vax floating-point types case
6932 elsif Vax_Float (Etype (N)) then
6933 Expand_Vax_Arith (N);
6935 end Expand_N_Op_Minus;
6937 ---------------------
6938 -- Expand_N_Op_Mod --
6939 ---------------------
6941 procedure Expand_N_Op_Mod (N : Node_Id) is
6942 Loc : constant Source_Ptr := Sloc (N);
6943 Typ : constant Entity_Id := Etype (N);
6944 Left : constant Node_Id := Left_Opnd (N);
6945 Right : constant Node_Id := Right_Opnd (N);
6946 DOC : constant Boolean := Do_Overflow_Check (N);
6947 DDC : constant Boolean := Do_Division_Check (N);
6957 pragma Warnings (Off, Lhi);
6960 Binary_Op_Validity_Checks (N);
6962 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
6963 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
6965 -- Convert mod to rem if operands are known non-negative. We do this
6966 -- since it is quite likely that this will improve the quality of code,
6967 -- (the operation now corresponds to the hardware remainder), and it
6968 -- does not seem likely that it could be harmful.
6970 if LOK and then Llo >= 0
6972 ROK and then Rlo >= 0
6975 Make_Op_Rem (Sloc (N),
6976 Left_Opnd => Left_Opnd (N),
6977 Right_Opnd => Right_Opnd (N)));
6979 -- Instead of reanalyzing the node we do the analysis manually. This
6980 -- avoids anomalies when the replacement is done in an instance and
6981 -- is epsilon more efficient.
6983 Set_Entity (N, Standard_Entity (S_Op_Rem));
6985 Set_Do_Overflow_Check (N, DOC);
6986 Set_Do_Division_Check (N, DDC);
6987 Expand_N_Op_Rem (N);
6990 -- Otherwise, normal mod processing
6993 if Is_Integer_Type (Etype (N)) then
6994 Apply_Divide_Check (N);
6997 -- Apply optimization x mod 1 = 0. We don't really need that with
6998 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
6999 -- certainly harmless.
7001 if Is_Integer_Type (Etype (N))
7002 and then Compile_Time_Known_Value (Right)
7003 and then Expr_Value (Right) = Uint_1
7005 -- Call Remove_Side_Effects to ensure that any side effects in
7006 -- the ignored left operand (in particular function calls to
7007 -- user defined functions) are properly executed.
7009 Remove_Side_Effects (Left);
7011 Rewrite (N, Make_Integer_Literal (Loc, 0));
7012 Analyze_And_Resolve (N, Typ);
7016 -- Deal with annoying case of largest negative number remainder
7017 -- minus one. Gigi does not handle this case correctly, because
7018 -- it generates a divide instruction which may trap in this case.
7020 -- In fact the check is quite easy, if the right operand is -1, then
7021 -- the mod value is always 0, and we can just ignore the left operand
7022 -- completely in this case.
7024 -- The operand type may be private (e.g. in the expansion of an
7025 -- intrinsic operation) so we must use the underlying type to get the
7026 -- bounds, and convert the literals explicitly.
7030 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
7032 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
7034 ((not LOK) or else (Llo = LLB))
7037 Make_Conditional_Expression (Loc,
7038 Expressions => New_List (
7040 Left_Opnd => Duplicate_Subexpr (Right),
7042 Unchecked_Convert_To (Typ,
7043 Make_Integer_Literal (Loc, -1))),
7044 Unchecked_Convert_To (Typ,
7045 Make_Integer_Literal (Loc, Uint_0)),
7046 Relocate_Node (N))));
7048 Set_Analyzed (Next (Next (First (Expressions (N)))));
7049 Analyze_And_Resolve (N, Typ);
7052 end Expand_N_Op_Mod;
7054 --------------------------
7055 -- Expand_N_Op_Multiply --
7056 --------------------------
7058 procedure Expand_N_Op_Multiply (N : Node_Id) is
7059 Loc : constant Source_Ptr := Sloc (N);
7060 Lop : constant Node_Id := Left_Opnd (N);
7061 Rop : constant Node_Id := Right_Opnd (N);
7063 Lp2 : constant Boolean :=
7064 Nkind (Lop) = N_Op_Expon
7065 and then Is_Power_Of_2_For_Shift (Lop);
7067 Rp2 : constant Boolean :=
7068 Nkind (Rop) = N_Op_Expon
7069 and then Is_Power_Of_2_For_Shift (Rop);
7071 Ltyp : constant Entity_Id := Etype (Lop);
7072 Rtyp : constant Entity_Id := Etype (Rop);
7073 Typ : Entity_Id := Etype (N);
7076 Binary_Op_Validity_Checks (N);
7078 -- Special optimizations for integer types
7080 if Is_Integer_Type (Typ) then
7082 -- N * 0 = 0 for integer types
7084 if Compile_Time_Known_Value (Rop)
7085 and then Expr_Value (Rop) = Uint_0
7087 -- Call Remove_Side_Effects to ensure that any side effects in
7088 -- the ignored left operand (in particular function calls to
7089 -- user defined functions) are properly executed.
7091 Remove_Side_Effects (Lop);
7093 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
7094 Analyze_And_Resolve (N, Typ);
7098 -- Similar handling for 0 * N = 0
7100 if Compile_Time_Known_Value (Lop)
7101 and then Expr_Value (Lop) = Uint_0
7103 Remove_Side_Effects (Rop);
7104 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
7105 Analyze_And_Resolve (N, Typ);
7109 -- N * 1 = 1 * N = N for integer types
7111 -- This optimisation is not done if we are going to
7112 -- rewrite the product 1 * 2 ** N to a shift.
7114 if Compile_Time_Known_Value (Rop)
7115 and then Expr_Value (Rop) = Uint_1
7121 elsif Compile_Time_Known_Value (Lop)
7122 and then Expr_Value (Lop) = Uint_1
7130 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
7131 -- Is_Power_Of_2_For_Shift is set means that we know that our left
7132 -- operand is an integer, as required for this to work.
7137 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
7141 Left_Opnd => Make_Integer_Literal (Loc, 2),
7144 Left_Opnd => Right_Opnd (Lop),
7145 Right_Opnd => Right_Opnd (Rop))));
7146 Analyze_And_Resolve (N, Typ);
7151 Make_Op_Shift_Left (Loc,
7154 Convert_To (Standard_Natural, Right_Opnd (Rop))));
7155 Analyze_And_Resolve (N, Typ);
7159 -- Same processing for the operands the other way round
7163 Make_Op_Shift_Left (Loc,
7166 Convert_To (Standard_Natural, Right_Opnd (Lop))));
7167 Analyze_And_Resolve (N, Typ);
7171 -- Do required fixup of universal fixed operation
7173 if Typ = Universal_Fixed then
7174 Fixup_Universal_Fixed_Operation (N);
7178 -- Multiplications with fixed-point results
7180 if Is_Fixed_Point_Type (Typ) then
7182 -- No special processing if Treat_Fixed_As_Integer is set, since from
7183 -- a semantic point of view such operations are simply integer
7184 -- operations and will be treated that way.
7186 if not Treat_Fixed_As_Integer (N) then
7188 -- Case of fixed * integer => fixed
7190 if Is_Integer_Type (Rtyp) then
7191 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
7193 -- Case of integer * fixed => fixed
7195 elsif Is_Integer_Type (Ltyp) then
7196 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
7198 -- Case of fixed * fixed => fixed
7201 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
7205 -- Other cases of multiplication of fixed-point operands. Again we
7206 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
7208 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
7209 and then not Treat_Fixed_As_Integer (N)
7211 if Is_Integer_Type (Typ) then
7212 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
7214 pragma Assert (Is_Floating_Point_Type (Typ));
7215 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
7218 -- Mixed-mode operations can appear in a non-static universal context,
7219 -- in which case the integer argument must be converted explicitly.
7221 elsif Typ = Universal_Real
7222 and then Is_Integer_Type (Rtyp)
7224 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
7226 Analyze_And_Resolve (Rop, Universal_Real);
7228 elsif Typ = Universal_Real
7229 and then Is_Integer_Type (Ltyp)
7231 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
7233 Analyze_And_Resolve (Lop, Universal_Real);
7235 -- Non-fixed point cases, check software overflow checking required
7237 elsif Is_Signed_Integer_Type (Etype (N)) then
7238 Apply_Arithmetic_Overflow_Check (N);
7240 -- Deal with VAX float case
7242 elsif Vax_Float (Typ) then
7243 Expand_Vax_Arith (N);
7246 end Expand_N_Op_Multiply;
7248 --------------------
7249 -- Expand_N_Op_Ne --
7250 --------------------
7252 procedure Expand_N_Op_Ne (N : Node_Id) is
7253 Typ : constant Entity_Id := Etype (Left_Opnd (N));
7256 -- Case of elementary type with standard operator
7258 if Is_Elementary_Type (Typ)
7259 and then Sloc (Entity (N)) = Standard_Location
7261 Binary_Op_Validity_Checks (N);
7263 -- Boolean types (requiring handling of non-standard case)
7265 if Is_Boolean_Type (Typ) then
7266 Adjust_Condition (Left_Opnd (N));
7267 Adjust_Condition (Right_Opnd (N));
7268 Set_Etype (N, Standard_Boolean);
7269 Adjust_Result_Type (N, Typ);
7272 Rewrite_Comparison (N);
7274 -- If we still have comparison for Vax_Float, process it
7276 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
7277 Expand_Vax_Comparison (N);
7281 -- For all cases other than elementary types, we rewrite node as the
7282 -- negation of an equality operation, and reanalyze. The equality to be
7283 -- used is defined in the same scope and has the same signature. This
7284 -- signature must be set explicitly since in an instance it may not have
7285 -- the same visibility as in the generic unit. This avoids duplicating
7286 -- or factoring the complex code for record/array equality tests etc.
7290 Loc : constant Source_Ptr := Sloc (N);
7292 Ne : constant Entity_Id := Entity (N);
7295 Binary_Op_Validity_Checks (N);
7301 Left_Opnd => Left_Opnd (N),
7302 Right_Opnd => Right_Opnd (N)));
7303 Set_Paren_Count (Right_Opnd (Neg), 1);
7305 if Scope (Ne) /= Standard_Standard then
7306 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
7309 -- For navigation purposes, we want to treat the inequality as an
7310 -- implicit reference to the corresponding equality. Preserve the
7311 -- Comes_From_ source flag to generate proper Xref entries.
7313 Preserve_Comes_From_Source (Neg, N);
7314 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
7316 Analyze_And_Resolve (N, Standard_Boolean);
7320 Optimize_Length_Comparison (N);
7323 ---------------------
7324 -- Expand_N_Op_Not --
7325 ---------------------
7327 -- If the argument is other than a Boolean array type, there is no special
7328 -- expansion required, except for VMS operations on signed integers.
7330 -- For the packed case, we call the special routine in Exp_Pakd, except
7331 -- that if the component size is greater than one, we use the standard
7332 -- routine generating a gruesome loop (it is so peculiar to have packed
7333 -- arrays with non-standard Boolean representations anyway, so it does not
7334 -- matter that we do not handle this case efficiently).
7336 -- For the unpacked case (and for the special packed case where we have non
7337 -- standard Booleans, as discussed above), we generate and insert into the
7338 -- tree the following function definition:
7340 -- function Nnnn (A : arr) is
7343 -- for J in a'range loop
7344 -- B (J) := not A (J);
7349 -- Here arr is the actual subtype of the parameter (and hence always
7350 -- constrained). Then we replace the not with a call to this function.
7352 procedure Expand_N_Op_Not (N : Node_Id) is
7353 Loc : constant Source_Ptr := Sloc (N);
7354 Typ : constant Entity_Id := Etype (N);
7363 Func_Name : Entity_Id;
7364 Loop_Statement : Node_Id;
7367 Unary_Op_Validity_Checks (N);
7369 -- For boolean operand, deal with non-standard booleans
7371 if Is_Boolean_Type (Typ) then
7372 Adjust_Condition (Right_Opnd (N));
7373 Set_Etype (N, Standard_Boolean);
7374 Adjust_Result_Type (N, Typ);
7378 -- For the VMS "not" on signed integer types, use conversion to and from
7379 -- a predefined modular type.
7381 if Is_VMS_Operator (Entity (N)) then
7387 -- If this is a derived type, retrieve original VMS type so that
7388 -- the proper sized type is used for intermediate values.
7390 if Is_Derived_Type (Typ) then
7391 Rtyp := First_Subtype (Etype (Typ));
7396 -- The proper unsigned type must have a size compatible with the
7397 -- operand, to prevent misalignment.
7399 if RM_Size (Rtyp) <= 8 then
7400 Utyp := RTE (RE_Unsigned_8);
7402 elsif RM_Size (Rtyp) <= 16 then
7403 Utyp := RTE (RE_Unsigned_16);
7405 elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then
7406 Utyp := RTE (RE_Unsigned_32);
7409 Utyp := RTE (RE_Long_Long_Unsigned);
7413 Unchecked_Convert_To (Typ,
7415 Unchecked_Convert_To (Utyp, Right_Opnd (N)))));
7416 Analyze_And_Resolve (N, Typ);
7421 -- Only array types need any other processing
7423 if not Is_Array_Type (Typ) then
7427 -- Case of array operand. If bit packed with a component size of 1,
7428 -- handle it in Exp_Pakd if the operand is known to be aligned.
7430 if Is_Bit_Packed_Array (Typ)
7431 and then Component_Size (Typ) = 1
7432 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
7434 Expand_Packed_Not (N);
7438 -- Case of array operand which is not bit-packed. If the context is
7439 -- a safe assignment, call in-place operation, If context is a larger
7440 -- boolean expression in the context of a safe assignment, expansion is
7441 -- done by enclosing operation.
7443 Opnd := Relocate_Node (Right_Opnd (N));
7444 Convert_To_Actual_Subtype (Opnd);
7445 Arr := Etype (Opnd);
7446 Ensure_Defined (Arr, N);
7447 Silly_Boolean_Array_Not_Test (N, Arr);
7449 if Nkind (Parent (N)) = N_Assignment_Statement then
7450 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
7451 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
7454 -- Special case the negation of a binary operation
7456 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
7457 and then Safe_In_Place_Array_Op
7458 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
7460 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
7464 elsif Nkind (Parent (N)) in N_Binary_Op
7465 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
7468 Op1 : constant Node_Id := Left_Opnd (Parent (N));
7469 Op2 : constant Node_Id := Right_Opnd (Parent (N));
7470 Lhs : constant Node_Id := Name (Parent (Parent (N)));
7473 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
7475 -- (not A) op (not B) can be reduced to a single call
7477 if N = Op1 and then Nkind (Op2) = N_Op_Not then
7480 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
7483 -- A xor (not B) can also be special-cased
7485 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
7492 A := Make_Defining_Identifier (Loc, Name_uA);
7493 B := Make_Defining_Identifier (Loc, Name_uB);
7494 J := Make_Defining_Identifier (Loc, Name_uJ);
7497 Make_Indexed_Component (Loc,
7498 Prefix => New_Reference_To (A, Loc),
7499 Expressions => New_List (New_Reference_To (J, Loc)));
7502 Make_Indexed_Component (Loc,
7503 Prefix => New_Reference_To (B, Loc),
7504 Expressions => New_List (New_Reference_To (J, Loc)));
7507 Make_Implicit_Loop_Statement (N,
7508 Identifier => Empty,
7511 Make_Iteration_Scheme (Loc,
7512 Loop_Parameter_Specification =>
7513 Make_Loop_Parameter_Specification (Loc,
7514 Defining_Identifier => J,
7515 Discrete_Subtype_Definition =>
7516 Make_Attribute_Reference (Loc,
7517 Prefix => Make_Identifier (Loc, Chars (A)),
7518 Attribute_Name => Name_Range))),
7520 Statements => New_List (
7521 Make_Assignment_Statement (Loc,
7523 Expression => Make_Op_Not (Loc, A_J))));
7525 Func_Name := Make_Temporary (Loc, 'N');
7526 Set_Is_Inlined (Func_Name);
7529 Make_Subprogram_Body (Loc,
7531 Make_Function_Specification (Loc,
7532 Defining_Unit_Name => Func_Name,
7533 Parameter_Specifications => New_List (
7534 Make_Parameter_Specification (Loc,
7535 Defining_Identifier => A,
7536 Parameter_Type => New_Reference_To (Typ, Loc))),
7537 Result_Definition => New_Reference_To (Typ, Loc)),
7539 Declarations => New_List (
7540 Make_Object_Declaration (Loc,
7541 Defining_Identifier => B,
7542 Object_Definition => New_Reference_To (Arr, Loc))),
7544 Handled_Statement_Sequence =>
7545 Make_Handled_Sequence_Of_Statements (Loc,
7546 Statements => New_List (
7548 Make_Simple_Return_Statement (Loc,
7549 Expression => Make_Identifier (Loc, Chars (B)))))));
7552 Make_Function_Call (Loc,
7553 Name => New_Reference_To (Func_Name, Loc),
7554 Parameter_Associations => New_List (Opnd)));
7556 Analyze_And_Resolve (N, Typ);
7557 end Expand_N_Op_Not;
7559 --------------------
7560 -- Expand_N_Op_Or --
7561 --------------------
7563 procedure Expand_N_Op_Or (N : Node_Id) is
7564 Typ : constant Entity_Id := Etype (N);
7567 Binary_Op_Validity_Checks (N);
7569 if Is_Array_Type (Etype (N)) then
7570 Expand_Boolean_Operator (N);
7572 elsif Is_Boolean_Type (Etype (N)) then
7573 Adjust_Condition (Left_Opnd (N));
7574 Adjust_Condition (Right_Opnd (N));
7575 Set_Etype (N, Standard_Boolean);
7576 Adjust_Result_Type (N, Typ);
7578 elsif Is_Intrinsic_Subprogram (Entity (N)) then
7579 Expand_Intrinsic_Call (N, Entity (N));
7584 ----------------------
7585 -- Expand_N_Op_Plus --
7586 ----------------------
7588 procedure Expand_N_Op_Plus (N : Node_Id) is
7590 Unary_Op_Validity_Checks (N);
7591 end Expand_N_Op_Plus;
7593 ---------------------
7594 -- Expand_N_Op_Rem --
7595 ---------------------
7597 procedure Expand_N_Op_Rem (N : Node_Id) is
7598 Loc : constant Source_Ptr := Sloc (N);
7599 Typ : constant Entity_Id := Etype (N);
7601 Left : constant Node_Id := Left_Opnd (N);
7602 Right : constant Node_Id := Right_Opnd (N);
7610 -- Set if corresponding operand can be negative
7612 pragma Unreferenced (Hi);
7615 Binary_Op_Validity_Checks (N);
7617 if Is_Integer_Type (Etype (N)) then
7618 Apply_Divide_Check (N);
7621 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
7622 -- but it is useful with other back ends (e.g. AAMP), and is certainly
7625 if Is_Integer_Type (Etype (N))
7626 and then Compile_Time_Known_Value (Right)
7627 and then Expr_Value (Right) = Uint_1
7629 -- Call Remove_Side_Effects to ensure that any side effects in the
7630 -- ignored left operand (in particular function calls to user defined
7631 -- functions) are properly executed.
7633 Remove_Side_Effects (Left);
7635 Rewrite (N, Make_Integer_Literal (Loc, 0));
7636 Analyze_And_Resolve (N, Typ);
7640 -- Deal with annoying case of largest negative number remainder minus
7641 -- one. Gigi does not handle this case correctly, because it generates
7642 -- a divide instruction which may trap in this case.
7644 -- In fact the check is quite easy, if the right operand is -1, then
7645 -- the remainder is always 0, and we can just ignore the left operand
7646 -- completely in this case.
7648 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
7649 Lneg := (not OK) or else Lo < 0;
7651 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
7652 Rneg := (not OK) or else Lo < 0;
7654 -- We won't mess with trying to find out if the left operand can really
7655 -- be the largest negative number (that's a pain in the case of private
7656 -- types and this is really marginal). We will just assume that we need
7657 -- the test if the left operand can be negative at all.
7659 if Lneg and Rneg then
7661 Make_Conditional_Expression (Loc,
7662 Expressions => New_List (
7664 Left_Opnd => Duplicate_Subexpr (Right),
7666 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
7668 Unchecked_Convert_To (Typ,
7669 Make_Integer_Literal (Loc, Uint_0)),
7671 Relocate_Node (N))));
7673 Set_Analyzed (Next (Next (First (Expressions (N)))));
7674 Analyze_And_Resolve (N, Typ);
7676 end Expand_N_Op_Rem;
7678 -----------------------------
7679 -- Expand_N_Op_Rotate_Left --
7680 -----------------------------
7682 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
7684 Binary_Op_Validity_Checks (N);
7685 end Expand_N_Op_Rotate_Left;
7687 ------------------------------
7688 -- Expand_N_Op_Rotate_Right --
7689 ------------------------------
7691 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
7693 Binary_Op_Validity_Checks (N);
7694 end Expand_N_Op_Rotate_Right;
7696 ----------------------------
7697 -- Expand_N_Op_Shift_Left --
7698 ----------------------------
7700 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
7702 Binary_Op_Validity_Checks (N);
7703 end Expand_N_Op_Shift_Left;
7705 -----------------------------
7706 -- Expand_N_Op_Shift_Right --
7707 -----------------------------
7709 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
7711 Binary_Op_Validity_Checks (N);
7712 end Expand_N_Op_Shift_Right;
7714 ----------------------------------------
7715 -- Expand_N_Op_Shift_Right_Arithmetic --
7716 ----------------------------------------
7718 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
7720 Binary_Op_Validity_Checks (N);
7721 end Expand_N_Op_Shift_Right_Arithmetic;
7723 --------------------------
7724 -- Expand_N_Op_Subtract --
7725 --------------------------
7727 procedure Expand_N_Op_Subtract (N : Node_Id) is
7728 Typ : constant Entity_Id := Etype (N);
7731 Binary_Op_Validity_Checks (N);
7733 -- N - 0 = N for integer types
7735 if Is_Integer_Type (Typ)
7736 and then Compile_Time_Known_Value (Right_Opnd (N))
7737 and then Expr_Value (Right_Opnd (N)) = 0
7739 Rewrite (N, Left_Opnd (N));
7743 -- Arithmetic overflow checks for signed integer/fixed point types
7745 if Is_Signed_Integer_Type (Typ)
7747 Is_Fixed_Point_Type (Typ)
7749 Apply_Arithmetic_Overflow_Check (N);
7751 -- VAX floating-point types case
7753 elsif Vax_Float (Typ) then
7754 Expand_Vax_Arith (N);
7756 end Expand_N_Op_Subtract;
7758 ---------------------
7759 -- Expand_N_Op_Xor --
7760 ---------------------
7762 procedure Expand_N_Op_Xor (N : Node_Id) is
7763 Typ : constant Entity_Id := Etype (N);
7766 Binary_Op_Validity_Checks (N);
7768 if Is_Array_Type (Etype (N)) then
7769 Expand_Boolean_Operator (N);
7771 elsif Is_Boolean_Type (Etype (N)) then
7772 Adjust_Condition (Left_Opnd (N));
7773 Adjust_Condition (Right_Opnd (N));
7774 Set_Etype (N, Standard_Boolean);
7775 Adjust_Result_Type (N, Typ);
7777 elsif Is_Intrinsic_Subprogram (Entity (N)) then
7778 Expand_Intrinsic_Call (N, Entity (N));
7781 end Expand_N_Op_Xor;
7783 ----------------------
7784 -- Expand_N_Or_Else --
7785 ----------------------
7787 procedure Expand_N_Or_Else (N : Node_Id)
7788 renames Expand_Short_Circuit_Operator;
7790 -----------------------------------
7791 -- Expand_N_Qualified_Expression --
7792 -----------------------------------
7794 procedure Expand_N_Qualified_Expression (N : Node_Id) is
7795 Operand : constant Node_Id := Expression (N);
7796 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
7799 -- Do validity check if validity checking operands
7801 if Validity_Checks_On
7802 and then Validity_Check_Operands
7804 Ensure_Valid (Operand);
7807 -- Apply possible constraint check
7809 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
7811 if Do_Range_Check (Operand) then
7812 Set_Do_Range_Check (Operand, False);
7813 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
7815 end Expand_N_Qualified_Expression;
7817 ------------------------------------
7818 -- Expand_N_Quantified_Expression --
7819 ------------------------------------
7823 -- for all X in range => Cond
7828 -- for X in range loop
7835 -- Conversely, an existentially quantified expression:
7837 -- for some X in range => Cond
7842 -- for X in range loop
7849 -- In both cases, the iteration may be over a container in which case it is
7850 -- given by an iterator specification, not a loop parameter specification.
7852 procedure Expand_N_Quantified_Expression (N : Node_Id) is
7853 Loc : constant Source_Ptr := Sloc (N);
7854 Is_Universal : constant Boolean := All_Present (N);
7855 Actions : constant List_Id := New_List;
7856 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
7864 Make_Object_Declaration (Loc,
7865 Defining_Identifier => Tnn,
7866 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
7868 New_Occurrence_Of (Boolean_Literals (Is_Universal), Loc));
7869 Append_To (Actions, Decl);
7871 Cond := Relocate_Node (Condition (N));
7873 -- Reset flag analyzed in the condition to force its analysis. Required
7874 -- since the previous analysis was done with expansion disabled (see
7875 -- Resolve_Quantified_Expression) and hence checks were not inserted
7876 -- and record comparisons have not been expanded.
7878 Reset_Analyzed_Flags (Cond);
7880 if Is_Universal then
7881 Cond := Make_Op_Not (Loc, Cond);
7885 Make_Implicit_If_Statement (N,
7887 Then_Statements => New_List (
7888 Make_Assignment_Statement (Loc,
7889 Name => New_Occurrence_Of (Tnn, Loc),
7891 New_Occurrence_Of (Boolean_Literals (not Is_Universal), Loc)),
7892 Make_Exit_Statement (Loc)));
7894 if Present (Loop_Parameter_Specification (N)) then
7896 Make_Iteration_Scheme (Loc,
7897 Loop_Parameter_Specification =>
7898 Loop_Parameter_Specification (N));
7901 Make_Iteration_Scheme (Loc,
7902 Iterator_Specification => Iterator_Specification (N));
7906 Make_Loop_Statement (Loc,
7907 Iteration_Scheme => I_Scheme,
7908 Statements => New_List (Test),
7909 End_Label => Empty));
7912 Make_Expression_With_Actions (Loc,
7913 Expression => New_Occurrence_Of (Tnn, Loc),
7914 Actions => Actions));
7916 Analyze_And_Resolve (N, Standard_Boolean);
7917 end Expand_N_Quantified_Expression;
7919 ---------------------------------
7920 -- Expand_N_Selected_Component --
7921 ---------------------------------
7923 procedure Expand_N_Selected_Component (N : Node_Id) is
7924 Loc : constant Source_Ptr := Sloc (N);
7925 Par : constant Node_Id := Parent (N);
7926 P : constant Node_Id := Prefix (N);
7927 Ptyp : Entity_Id := Underlying_Type (Etype (P));
7933 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
7934 -- Gigi needs a temporary for prefixes that depend on a discriminant,
7935 -- unless the context of an assignment can provide size information.
7936 -- Don't we have a general routine that does this???
7938 function Is_Subtype_Declaration return Boolean;
7939 -- The replacement of a discriminant reference by its value is required
7940 -- if this is part of the initialization of an temporary generated by a
7941 -- change of representation. This shows up as the construction of a
7942 -- discriminant constraint for a subtype declared at the same point as
7943 -- the entity in the prefix of the selected component. We recognize this
7944 -- case when the context of the reference is:
7945 -- subtype ST is T(Obj.D);
7946 -- where the entity for Obj comes from source, and ST has the same sloc.
7948 -----------------------
7949 -- In_Left_Hand_Side --
7950 -----------------------
7952 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
7954 return (Nkind (Parent (Comp)) = N_Assignment_Statement
7955 and then Comp = Name (Parent (Comp)))
7956 or else (Present (Parent (Comp))
7957 and then Nkind (Parent (Comp)) in N_Subexpr
7958 and then In_Left_Hand_Side (Parent (Comp)));
7959 end In_Left_Hand_Side;
7961 -----------------------------
7962 -- Is_Subtype_Declaration --
7963 -----------------------------
7965 function Is_Subtype_Declaration return Boolean is
7966 Par : constant Node_Id := Parent (N);
7969 Nkind (Par) = N_Index_Or_Discriminant_Constraint
7970 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
7971 and then Comes_From_Source (Entity (Prefix (N)))
7972 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
7973 end Is_Subtype_Declaration;
7975 -- Start of processing for Expand_N_Selected_Component
7978 -- Insert explicit dereference if required
7980 if Is_Access_Type (Ptyp) then
7982 -- First set prefix type to proper access type, in case it currently
7983 -- has a private (non-access) view of this type.
7985 Set_Etype (P, Ptyp);
7987 Insert_Explicit_Dereference (P);
7988 Analyze_And_Resolve (P, Designated_Type (Ptyp));
7990 if Ekind (Etype (P)) = E_Private_Subtype
7991 and then Is_For_Access_Subtype (Etype (P))
7993 Set_Etype (P, Base_Type (Etype (P)));
7999 -- Deal with discriminant check required
8001 if Do_Discriminant_Check (N) then
8003 -- Present the discriminant checking function to the backend, so that
8004 -- it can inline the call to the function.
8007 (Discriminant_Checking_Func
8008 (Original_Record_Component (Entity (Selector_Name (N)))));
8010 -- Now reset the flag and generate the call
8012 Set_Do_Discriminant_Check (N, False);
8013 Generate_Discriminant_Check (N);
8016 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
8017 -- function, then additional actuals must be passed.
8019 if Ada_Version >= Ada_2005
8020 and then Is_Build_In_Place_Function_Call (P)
8022 Make_Build_In_Place_Call_In_Anonymous_Context (P);
8025 -- Gigi cannot handle unchecked conversions that are the prefix of a
8026 -- selected component with discriminants. This must be checked during
8027 -- expansion, because during analysis the type of the selector is not
8028 -- known at the point the prefix is analyzed. If the conversion is the
8029 -- target of an assignment, then we cannot force the evaluation.
8031 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
8032 and then Has_Discriminants (Etype (N))
8033 and then not In_Left_Hand_Side (N)
8035 Force_Evaluation (Prefix (N));
8038 -- Remaining processing applies only if selector is a discriminant
8040 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
8042 -- If the selector is a discriminant of a constrained record type,
8043 -- we may be able to rewrite the expression with the actual value
8044 -- of the discriminant, a useful optimization in some cases.
8046 if Is_Record_Type (Ptyp)
8047 and then Has_Discriminants (Ptyp)
8048 and then Is_Constrained (Ptyp)
8050 -- Do this optimization for discrete types only, and not for
8051 -- access types (access discriminants get us into trouble!)
8053 if not Is_Discrete_Type (Etype (N)) then
8056 -- Don't do this on the left hand of an assignment statement.
8057 -- Normally one would think that references like this would not
8058 -- occur, but they do in generated code, and mean that we really
8059 -- do want to assign the discriminant!
8061 elsif Nkind (Par) = N_Assignment_Statement
8062 and then Name (Par) = N
8066 -- Don't do this optimization for the prefix of an attribute or
8067 -- the name of an object renaming declaration since these are
8068 -- contexts where we do not want the value anyway.
8070 elsif (Nkind (Par) = N_Attribute_Reference
8071 and then Prefix (Par) = N)
8072 or else Is_Renamed_Object (N)
8076 -- Don't do this optimization if we are within the code for a
8077 -- discriminant check, since the whole point of such a check may
8078 -- be to verify the condition on which the code below depends!
8080 elsif Is_In_Discriminant_Check (N) then
8083 -- Green light to see if we can do the optimization. There is
8084 -- still one condition that inhibits the optimization below but
8085 -- now is the time to check the particular discriminant.
8088 -- Loop through discriminants to find the matching discriminant
8089 -- constraint to see if we can copy it.
8091 Disc := First_Discriminant (Ptyp);
8092 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
8093 Discr_Loop : while Present (Dcon) loop
8094 Dval := Node (Dcon);
8096 -- Check if this is the matching discriminant and if the
8097 -- discriminant value is simple enough to make sense to
8098 -- copy. We don't want to copy complex expressions, and
8099 -- indeed to do so can cause trouble (before we put in
8100 -- this guard, a discriminant expression containing an
8101 -- AND THEN was copied, causing problems for coverage
8104 -- However, if the reference is part of the initialization
8105 -- code generated for an object declaration, we must use
8106 -- the discriminant value from the subtype constraint,
8107 -- because the selected component may be a reference to the
8108 -- object being initialized, whose discriminant is not yet
8109 -- set. This only happens in complex cases involving changes
8110 -- or representation.
8112 if Disc = Entity (Selector_Name (N))
8113 and then (Is_Entity_Name (Dval)
8114 or else Compile_Time_Known_Value (Dval)
8115 or else Is_Subtype_Declaration)
8117 -- Here we have the matching discriminant. Check for
8118 -- the case of a discriminant of a component that is
8119 -- constrained by an outer discriminant, which cannot
8120 -- be optimized away.
8122 if Denotes_Discriminant
8123 (Dval, Check_Concurrent => True)
8127 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
8129 Denotes_Discriminant
8130 (Selector_Name (Original_Node (Dval)), True)
8134 -- Do not retrieve value if constraint is not static. It
8135 -- is generally not useful, and the constraint may be a
8136 -- rewritten outer discriminant in which case it is in
8139 elsif Is_Entity_Name (Dval)
8140 and then Nkind (Parent (Entity (Dval))) =
8141 N_Object_Declaration
8142 and then Present (Expression (Parent (Entity (Dval))))
8144 not Is_Static_Expression
8145 (Expression (Parent (Entity (Dval))))
8149 -- In the context of a case statement, the expression may
8150 -- have the base type of the discriminant, and we need to
8151 -- preserve the constraint to avoid spurious errors on
8154 elsif Nkind (Parent (N)) = N_Case_Statement
8155 and then Etype (Dval) /= Etype (Disc)
8158 Make_Qualified_Expression (Loc,
8160 New_Occurrence_Of (Etype (Disc), Loc),
8162 New_Copy_Tree (Dval)));
8163 Analyze_And_Resolve (N, Etype (Disc));
8165 -- In case that comes out as a static expression,
8166 -- reset it (a selected component is never static).
8168 Set_Is_Static_Expression (N, False);
8171 -- Otherwise we can just copy the constraint, but the
8172 -- result is certainly not static! In some cases the
8173 -- discriminant constraint has been analyzed in the
8174 -- context of the original subtype indication, but for
8175 -- itypes the constraint might not have been analyzed
8176 -- yet, and this must be done now.
8179 Rewrite (N, New_Copy_Tree (Dval));
8180 Analyze_And_Resolve (N);
8181 Set_Is_Static_Expression (N, False);
8187 Next_Discriminant (Disc);
8188 end loop Discr_Loop;
8190 -- Note: the above loop should always find a matching
8191 -- discriminant, but if it does not, we just missed an
8192 -- optimization due to some glitch (perhaps a previous
8193 -- error), so ignore.
8198 -- The only remaining processing is in the case of a discriminant of
8199 -- a concurrent object, where we rewrite the prefix to denote the
8200 -- corresponding record type. If the type is derived and has renamed
8201 -- discriminants, use corresponding discriminant, which is the one
8202 -- that appears in the corresponding record.
8204 if not Is_Concurrent_Type (Ptyp) then
8208 Disc := Entity (Selector_Name (N));
8210 if Is_Derived_Type (Ptyp)
8211 and then Present (Corresponding_Discriminant (Disc))
8213 Disc := Corresponding_Discriminant (Disc);
8217 Make_Selected_Component (Loc,
8219 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
8221 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
8227 -- Set Atomic_Sync_Required if necessary for atomic component
8229 if Nkind (N) = N_Selected_Component then
8231 E : constant Entity_Id := Entity (Selector_Name (N));
8235 -- If component is atomic, but type is not, setting depends on
8236 -- disable/enable state for the component.
8238 if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then
8239 Set := not Atomic_Synchronization_Disabled (E);
8241 -- If component is not atomic, but its type is atomic, setting
8242 -- depends on disable/enable state for the type.
8244 elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then
8245 Set := not Atomic_Synchronization_Disabled (Etype (E));
8247 -- If both component and type are atomic, we disable if either
8248 -- component or its type have sync disabled.
8250 elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then
8251 Set := (not Atomic_Synchronization_Disabled (E))
8253 (not Atomic_Synchronization_Disabled (Etype (E)));
8259 -- Set flag if required
8262 Activate_Atomic_Synchronization (N);
8266 end Expand_N_Selected_Component;
8268 --------------------
8269 -- Expand_N_Slice --
8270 --------------------
8272 procedure Expand_N_Slice (N : Node_Id) is
8273 Loc : constant Source_Ptr := Sloc (N);
8274 Typ : constant Entity_Id := Etype (N);
8275 Pfx : constant Node_Id := Prefix (N);
8276 Ptp : Entity_Id := Etype (Pfx);
8278 function Is_Procedure_Actual (N : Node_Id) return Boolean;
8279 -- Check whether the argument is an actual for a procedure call, in
8280 -- which case the expansion of a bit-packed slice is deferred until the
8281 -- call itself is expanded. The reason this is required is that we might
8282 -- have an IN OUT or OUT parameter, and the copy out is essential, and
8283 -- that copy out would be missed if we created a temporary here in
8284 -- Expand_N_Slice. Note that we don't bother to test specifically for an
8285 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
8286 -- is harmless to defer expansion in the IN case, since the call
8287 -- processing will still generate the appropriate copy in operation,
8288 -- which will take care of the slice.
8290 procedure Make_Temporary_For_Slice;
8291 -- Create a named variable for the value of the slice, in cases where
8292 -- the back-end cannot handle it properly, e.g. when packed types or
8293 -- unaligned slices are involved.
8295 -------------------------
8296 -- Is_Procedure_Actual --
8297 -------------------------
8299 function Is_Procedure_Actual (N : Node_Id) return Boolean is
8300 Par : Node_Id := Parent (N);
8304 -- If our parent is a procedure call we can return
8306 if Nkind (Par) = N_Procedure_Call_Statement then
8309 -- If our parent is a type conversion, keep climbing the tree,
8310 -- since a type conversion can be a procedure actual. Also keep
8311 -- climbing if parameter association or a qualified expression,
8312 -- since these are additional cases that do can appear on
8313 -- procedure actuals.
8315 elsif Nkind_In (Par, N_Type_Conversion,
8316 N_Parameter_Association,
8317 N_Qualified_Expression)
8319 Par := Parent (Par);
8321 -- Any other case is not what we are looking for
8327 end Is_Procedure_Actual;
8329 ------------------------------
8330 -- Make_Temporary_For_Slice --
8331 ------------------------------
8333 procedure Make_Temporary_For_Slice is
8335 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
8339 Make_Object_Declaration (Loc,
8340 Defining_Identifier => Ent,
8341 Object_Definition => New_Occurrence_Of (Typ, Loc));
8343 Set_No_Initialization (Decl);
8345 Insert_Actions (N, New_List (
8347 Make_Assignment_Statement (Loc,
8348 Name => New_Occurrence_Of (Ent, Loc),
8349 Expression => Relocate_Node (N))));
8351 Rewrite (N, New_Occurrence_Of (Ent, Loc));
8352 Analyze_And_Resolve (N, Typ);
8353 end Make_Temporary_For_Slice;
8355 -- Start of processing for Expand_N_Slice
8358 -- Special handling for access types
8360 if Is_Access_Type (Ptp) then
8362 Ptp := Designated_Type (Ptp);
8365 Make_Explicit_Dereference (Sloc (N),
8366 Prefix => Relocate_Node (Pfx)));
8368 Analyze_And_Resolve (Pfx, Ptp);
8371 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
8372 -- function, then additional actuals must be passed.
8374 if Ada_Version >= Ada_2005
8375 and then Is_Build_In_Place_Function_Call (Pfx)
8377 Make_Build_In_Place_Call_In_Anonymous_Context (Pfx);
8380 -- The remaining case to be handled is packed slices. We can leave
8381 -- packed slices as they are in the following situations:
8383 -- 1. Right or left side of an assignment (we can handle this
8384 -- situation correctly in the assignment statement expansion).
8386 -- 2. Prefix of indexed component (the slide is optimized away in this
8387 -- case, see the start of Expand_N_Slice.)
8389 -- 3. Object renaming declaration, since we want the name of the
8390 -- slice, not the value.
8392 -- 4. Argument to procedure call, since copy-in/copy-out handling may
8393 -- be required, and this is handled in the expansion of call
8396 -- 5. Prefix of an address attribute (this is an error which is caught
8397 -- elsewhere, and the expansion would interfere with generating the
8400 if not Is_Packed (Typ) then
8402 -- Apply transformation for actuals of a function call, where
8403 -- Expand_Actuals is not used.
8405 if Nkind (Parent (N)) = N_Function_Call
8406 and then Is_Possibly_Unaligned_Slice (N)
8408 Make_Temporary_For_Slice;
8411 elsif Nkind (Parent (N)) = N_Assignment_Statement
8412 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
8413 and then Parent (N) = Name (Parent (Parent (N))))
8417 elsif Nkind (Parent (N)) = N_Indexed_Component
8418 or else Is_Renamed_Object (N)
8419 or else Is_Procedure_Actual (N)
8423 elsif Nkind (Parent (N)) = N_Attribute_Reference
8424 and then Attribute_Name (Parent (N)) = Name_Address
8429 Make_Temporary_For_Slice;
8433 ------------------------------
8434 -- Expand_N_Type_Conversion --
8435 ------------------------------
8437 procedure Expand_N_Type_Conversion (N : Node_Id) is
8438 Loc : constant Source_Ptr := Sloc (N);
8439 Operand : constant Node_Id := Expression (N);
8440 Target_Type : constant Entity_Id := Etype (N);
8441 Operand_Type : Entity_Id := Etype (Operand);
8443 procedure Handle_Changed_Representation;
8444 -- This is called in the case of record and array type conversions to
8445 -- see if there is a change of representation to be handled. Change of
8446 -- representation is actually handled at the assignment statement level,
8447 -- and what this procedure does is rewrite node N conversion as an
8448 -- assignment to temporary. If there is no change of representation,
8449 -- then the conversion node is unchanged.
8451 procedure Raise_Accessibility_Error;
8452 -- Called when we know that an accessibility check will fail. Rewrites
8453 -- node N to an appropriate raise statement and outputs warning msgs.
8454 -- The Etype of the raise node is set to Target_Type.
8456 procedure Real_Range_Check;
8457 -- Handles generation of range check for real target value
8459 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean;
8460 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
8461 -- evaluates to True.
8463 -----------------------------------
8464 -- Handle_Changed_Representation --
8465 -----------------------------------
8467 procedure Handle_Changed_Representation is
8476 -- Nothing else to do if no change of representation
8478 if Same_Representation (Operand_Type, Target_Type) then
8481 -- The real change of representation work is done by the assignment
8482 -- statement processing. So if this type conversion is appearing as
8483 -- the expression of an assignment statement, nothing needs to be
8484 -- done to the conversion.
8486 elsif Nkind (Parent (N)) = N_Assignment_Statement then
8489 -- Otherwise we need to generate a temporary variable, and do the
8490 -- change of representation assignment into that temporary variable.
8491 -- The conversion is then replaced by a reference to this variable.
8496 -- If type is unconstrained we have to add a constraint, copied
8497 -- from the actual value of the left hand side.
8499 if not Is_Constrained (Target_Type) then
8500 if Has_Discriminants (Operand_Type) then
8501 Disc := First_Discriminant (Operand_Type);
8503 if Disc /= First_Stored_Discriminant (Operand_Type) then
8504 Disc := First_Stored_Discriminant (Operand_Type);
8508 while Present (Disc) loop
8510 Make_Selected_Component (Loc,
8512 Duplicate_Subexpr_Move_Checks (Operand),
8514 Make_Identifier (Loc, Chars (Disc))));
8515 Next_Discriminant (Disc);
8518 elsif Is_Array_Type (Operand_Type) then
8519 N_Ix := First_Index (Target_Type);
8522 for J in 1 .. Number_Dimensions (Operand_Type) loop
8524 -- We convert the bounds explicitly. We use an unchecked
8525 -- conversion because bounds checks are done elsewhere.
8530 Unchecked_Convert_To (Etype (N_Ix),
8531 Make_Attribute_Reference (Loc,
8533 Duplicate_Subexpr_No_Checks
8534 (Operand, Name_Req => True),
8535 Attribute_Name => Name_First,
8536 Expressions => New_List (
8537 Make_Integer_Literal (Loc, J)))),
8540 Unchecked_Convert_To (Etype (N_Ix),
8541 Make_Attribute_Reference (Loc,
8543 Duplicate_Subexpr_No_Checks
8544 (Operand, Name_Req => True),
8545 Attribute_Name => Name_Last,
8546 Expressions => New_List (
8547 Make_Integer_Literal (Loc, J))))));
8554 Odef := New_Occurrence_Of (Target_Type, Loc);
8556 if Present (Cons) then
8558 Make_Subtype_Indication (Loc,
8559 Subtype_Mark => Odef,
8561 Make_Index_Or_Discriminant_Constraint (Loc,
8562 Constraints => Cons));
8565 Temp := Make_Temporary (Loc, 'C');
8567 Make_Object_Declaration (Loc,
8568 Defining_Identifier => Temp,
8569 Object_Definition => Odef);
8571 Set_No_Initialization (Decl, True);
8573 -- Insert required actions. It is essential to suppress checks
8574 -- since we have suppressed default initialization, which means
8575 -- that the variable we create may have no discriminants.
8580 Make_Assignment_Statement (Loc,
8581 Name => New_Occurrence_Of (Temp, Loc),
8582 Expression => Relocate_Node (N))),
8583 Suppress => All_Checks);
8585 Rewrite (N, New_Occurrence_Of (Temp, Loc));
8588 end Handle_Changed_Representation;
8590 -------------------------------
8591 -- Raise_Accessibility_Error --
8592 -------------------------------
8594 procedure Raise_Accessibility_Error is
8597 Make_Raise_Program_Error (Sloc (N),
8598 Reason => PE_Accessibility_Check_Failed));
8599 Set_Etype (N, Target_Type);
8601 Error_Msg_N ("?accessibility check failure", N);
8603 ("\?& will be raised at run time", N, Standard_Program_Error);
8604 end Raise_Accessibility_Error;
8606 ----------------------
8607 -- Real_Range_Check --
8608 ----------------------
8610 -- Case of conversions to floating-point or fixed-point. If range checks
8611 -- are enabled and the target type has a range constraint, we convert:
8617 -- Tnn : typ'Base := typ'Base (x);
8618 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
8621 -- This is necessary when there is a conversion of integer to float or
8622 -- to fixed-point to ensure that the correct checks are made. It is not
8623 -- necessary for float to float where it is enough to simply set the
8624 -- Do_Range_Check flag.
8626 procedure Real_Range_Check is
8627 Btyp : constant Entity_Id := Base_Type (Target_Type);
8628 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
8629 Hi : constant Node_Id := Type_High_Bound (Target_Type);
8630 Xtyp : constant Entity_Id := Etype (Operand);
8635 -- Nothing to do if conversion was rewritten
8637 if Nkind (N) /= N_Type_Conversion then
8641 -- Nothing to do if range checks suppressed, or target has the same
8642 -- range as the base type (or is the base type).
8644 if Range_Checks_Suppressed (Target_Type)
8645 or else (Lo = Type_Low_Bound (Btyp)
8647 Hi = Type_High_Bound (Btyp))
8652 -- Nothing to do if expression is an entity on which checks have been
8655 if Is_Entity_Name (Operand)
8656 and then Range_Checks_Suppressed (Entity (Operand))
8661 -- Nothing to do if bounds are all static and we can tell that the
8662 -- expression is within the bounds of the target. Note that if the
8663 -- operand is of an unconstrained floating-point type, then we do
8664 -- not trust it to be in range (might be infinite)
8667 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
8668 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
8671 if (not Is_Floating_Point_Type (Xtyp)
8672 or else Is_Constrained (Xtyp))
8673 and then Compile_Time_Known_Value (S_Lo)
8674 and then Compile_Time_Known_Value (S_Hi)
8675 and then Compile_Time_Known_Value (Hi)
8676 and then Compile_Time_Known_Value (Lo)
8679 D_Lov : constant Ureal := Expr_Value_R (Lo);
8680 D_Hiv : constant Ureal := Expr_Value_R (Hi);
8685 if Is_Real_Type (Xtyp) then
8686 S_Lov := Expr_Value_R (S_Lo);
8687 S_Hiv := Expr_Value_R (S_Hi);
8689 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
8690 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
8694 and then S_Lov >= D_Lov
8695 and then S_Hiv <= D_Hiv
8697 Set_Do_Range_Check (Operand, False);
8704 -- For float to float conversions, we are done
8706 if Is_Floating_Point_Type (Xtyp)
8708 Is_Floating_Point_Type (Btyp)
8713 -- Otherwise rewrite the conversion as described above
8715 Conv := Relocate_Node (N);
8716 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
8717 Set_Etype (Conv, Btyp);
8719 -- Enable overflow except for case of integer to float conversions,
8720 -- where it is never required, since we can never have overflow in
8723 if not Is_Integer_Type (Etype (Operand)) then
8724 Enable_Overflow_Check (Conv);
8727 Tnn := Make_Temporary (Loc, 'T', Conv);
8729 Insert_Actions (N, New_List (
8730 Make_Object_Declaration (Loc,
8731 Defining_Identifier => Tnn,
8732 Object_Definition => New_Occurrence_Of (Btyp, Loc),
8733 Constant_Present => True,
8734 Expression => Conv),
8736 Make_Raise_Constraint_Error (Loc,
8741 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8743 Make_Attribute_Reference (Loc,
8744 Attribute_Name => Name_First,
8746 New_Occurrence_Of (Target_Type, Loc))),
8750 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8752 Make_Attribute_Reference (Loc,
8753 Attribute_Name => Name_Last,
8755 New_Occurrence_Of (Target_Type, Loc)))),
8756 Reason => CE_Range_Check_Failed)));
8758 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
8759 Analyze_And_Resolve (N, Btyp);
8760 end Real_Range_Check;
8762 -----------------------------
8763 -- Has_Extra_Accessibility --
8764 -----------------------------
8766 -- Returns true for a formal of an anonymous access type or for
8767 -- an Ada 2012-style stand-alone object of an anonymous access type.
8769 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is
8771 if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then
8772 return Present (Effective_Extra_Accessibility (Id));
8776 end Has_Extra_Accessibility;
8778 -- Start of processing for Expand_N_Type_Conversion
8781 -- Nothing at all to do if conversion is to the identical type so remove
8782 -- the conversion completely, it is useless, except that it may carry
8783 -- an Assignment_OK attribute, which must be propagated to the operand.
8785 if Operand_Type = Target_Type then
8786 if Assignment_OK (N) then
8787 Set_Assignment_OK (Operand);
8790 Rewrite (N, Relocate_Node (Operand));
8794 -- Nothing to do if this is the second argument of read. This is a
8795 -- "backwards" conversion that will be handled by the specialized code
8796 -- in attribute processing.
8798 if Nkind (Parent (N)) = N_Attribute_Reference
8799 and then Attribute_Name (Parent (N)) = Name_Read
8800 and then Next (First (Expressions (Parent (N)))) = N
8805 -- Check for case of converting to a type that has an invariant
8806 -- associated with it. This required an invariant check. We convert
8812 -- do invariant_check (typ (expr)) in typ (expr);
8814 -- using Duplicate_Subexpr to avoid multiple side effects
8816 -- Note: the Comes_From_Source check, and then the resetting of this
8817 -- flag prevents what would otherwise be an infinite recursion.
8819 if Has_Invariants (Target_Type)
8820 and then Present (Invariant_Procedure (Target_Type))
8821 and then Comes_From_Source (N)
8823 Set_Comes_From_Source (N, False);
8825 Make_Expression_With_Actions (Loc,
8826 Actions => New_List (
8827 Make_Invariant_Call (Duplicate_Subexpr (N))),
8828 Expression => Duplicate_Subexpr_No_Checks (N)));
8829 Analyze_And_Resolve (N, Target_Type);
8833 -- Here if we may need to expand conversion
8835 -- If the operand of the type conversion is an arithmetic operation on
8836 -- signed integers, and the based type of the signed integer type in
8837 -- question is smaller than Standard.Integer, we promote both of the
8838 -- operands to type Integer.
8840 -- For example, if we have
8842 -- target-type (opnd1 + opnd2)
8844 -- and opnd1 and opnd2 are of type short integer, then we rewrite
8847 -- target-type (integer(opnd1) + integer(opnd2))
8849 -- We do this because we are always allowed to compute in a larger type
8850 -- if we do the right thing with the result, and in this case we are
8851 -- going to do a conversion which will do an appropriate check to make
8852 -- sure that things are in range of the target type in any case. This
8853 -- avoids some unnecessary intermediate overflows.
8855 -- We might consider a similar transformation in the case where the
8856 -- target is a real type or a 64-bit integer type, and the operand
8857 -- is an arithmetic operation using a 32-bit integer type. However,
8858 -- we do not bother with this case, because it could cause significant
8859 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
8860 -- much cheaper, but we don't want different behavior on 32-bit and
8861 -- 64-bit machines. Note that the exclusion of the 64-bit case also
8862 -- handles the configurable run-time cases where 64-bit arithmetic
8863 -- may simply be unavailable.
8865 -- Note: this circuit is partially redundant with respect to the circuit
8866 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
8867 -- the processing here. Also we still need the Checks circuit, since we
8868 -- have to be sure not to generate junk overflow checks in the first
8869 -- place, since it would be trick to remove them here!
8871 if Integer_Promotion_Possible (N) then
8873 -- All conditions met, go ahead with transformation
8881 Make_Type_Conversion (Loc,
8882 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
8883 Expression => Relocate_Node (Right_Opnd (Operand)));
8885 Opnd := New_Op_Node (Nkind (Operand), Loc);
8886 Set_Right_Opnd (Opnd, R);
8888 if Nkind (Operand) in N_Binary_Op then
8890 Make_Type_Conversion (Loc,
8891 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
8892 Expression => Relocate_Node (Left_Opnd (Operand)));
8894 Set_Left_Opnd (Opnd, L);
8898 Make_Type_Conversion (Loc,
8899 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
8900 Expression => Opnd));
8902 Analyze_And_Resolve (N, Target_Type);
8907 -- Do validity check if validity checking operands
8909 if Validity_Checks_On
8910 and then Validity_Check_Operands
8912 Ensure_Valid (Operand);
8915 -- Special case of converting from non-standard boolean type
8917 if Is_Boolean_Type (Operand_Type)
8918 and then (Nonzero_Is_True (Operand_Type))
8920 Adjust_Condition (Operand);
8921 Set_Etype (Operand, Standard_Boolean);
8922 Operand_Type := Standard_Boolean;
8925 -- Case of converting to an access type
8927 if Is_Access_Type (Target_Type) then
8929 -- Apply an accessibility check when the conversion operand is an
8930 -- access parameter (or a renaming thereof), unless conversion was
8931 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
8932 -- Note that other checks may still need to be applied below (such
8933 -- as tagged type checks).
8935 if Is_Entity_Name (Operand)
8936 and then Has_Extra_Accessibility (Entity (Operand))
8937 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
8938 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
8939 or else Attribute_Name (Original_Node (N)) = Name_Access)
8941 Apply_Accessibility_Check
8942 (Operand, Target_Type, Insert_Node => Operand);
8944 -- If the level of the operand type is statically deeper than the
8945 -- level of the target type, then force Program_Error. Note that this
8946 -- can only occur for cases where the attribute is within the body of
8947 -- an instantiation (otherwise the conversion will already have been
8948 -- rejected as illegal). Note: warnings are issued by the analyzer
8949 -- for the instance cases.
8951 elsif In_Instance_Body
8952 and then Type_Access_Level (Operand_Type) >
8953 Type_Access_Level (Target_Type)
8955 Raise_Accessibility_Error;
8957 -- When the operand is a selected access discriminant the check needs
8958 -- to be made against the level of the object denoted by the prefix
8959 -- of the selected name. Force Program_Error for this case as well
8960 -- (this accessibility violation can only happen if within the body
8961 -- of an instantiation).
8963 elsif In_Instance_Body
8964 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
8965 and then Nkind (Operand) = N_Selected_Component
8966 and then Object_Access_Level (Operand) >
8967 Type_Access_Level (Target_Type)
8969 Raise_Accessibility_Error;
8974 -- Case of conversions of tagged types and access to tagged types
8976 -- When needed, that is to say when the expression is class-wide, Add
8977 -- runtime a tag check for (strict) downward conversion by using the
8978 -- membership test, generating:
8980 -- [constraint_error when Operand not in Target_Type'Class]
8982 -- or in the access type case
8984 -- [constraint_error
8985 -- when Operand /= null
8986 -- and then Operand.all not in
8987 -- Designated_Type (Target_Type)'Class]
8989 if (Is_Access_Type (Target_Type)
8990 and then Is_Tagged_Type (Designated_Type (Target_Type)))
8991 or else Is_Tagged_Type (Target_Type)
8993 -- Do not do any expansion in the access type case if the parent is a
8994 -- renaming, since this is an error situation which will be caught by
8995 -- Sem_Ch8, and the expansion can interfere with this error check.
8997 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
9001 -- Otherwise, proceed with processing tagged conversion
9003 Tagged_Conversion : declare
9004 Actual_Op_Typ : Entity_Id;
9005 Actual_Targ_Typ : Entity_Id;
9006 Make_Conversion : Boolean := False;
9007 Root_Op_Typ : Entity_Id;
9009 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
9010 -- Create a membership check to test whether Operand is a member
9011 -- of Targ_Typ. If the original Target_Type is an access, include
9012 -- a test for null value. The check is inserted at N.
9014 --------------------
9015 -- Make_Tag_Check --
9016 --------------------
9018 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
9023 -- [Constraint_Error
9024 -- when Operand /= null
9025 -- and then Operand.all not in Targ_Typ]
9027 if Is_Access_Type (Target_Type) then
9032 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
9033 Right_Opnd => Make_Null (Loc)),
9038 Make_Explicit_Dereference (Loc,
9039 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
9040 Right_Opnd => New_Reference_To (Targ_Typ, Loc)));
9043 -- [Constraint_Error when Operand not in Targ_Typ]
9048 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
9049 Right_Opnd => New_Reference_To (Targ_Typ, Loc));
9053 Make_Raise_Constraint_Error (Loc,
9055 Reason => CE_Tag_Check_Failed));
9058 -- Start of processing for Tagged_Conversion
9061 -- Handle entities from the limited view
9063 if Is_Access_Type (Operand_Type) then
9065 Available_View (Designated_Type (Operand_Type));
9067 Actual_Op_Typ := Operand_Type;
9070 if Is_Access_Type (Target_Type) then
9072 Available_View (Designated_Type (Target_Type));
9074 Actual_Targ_Typ := Target_Type;
9077 Root_Op_Typ := Root_Type (Actual_Op_Typ);
9079 -- Ada 2005 (AI-251): Handle interface type conversion
9081 if Is_Interface (Actual_Op_Typ) then
9082 Expand_Interface_Conversion (N, Is_Static => False);
9086 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
9088 -- Create a runtime tag check for a downward class-wide type
9091 if Is_Class_Wide_Type (Actual_Op_Typ)
9092 and then Actual_Op_Typ /= Actual_Targ_Typ
9093 and then Root_Op_Typ /= Actual_Targ_Typ
9094 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ,
9095 Use_Full_View => True)
9097 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
9098 Make_Conversion := True;
9101 -- AI05-0073: If the result subtype of the function is defined
9102 -- by an access_definition designating a specific tagged type
9103 -- T, a check is made that the result value is null or the tag
9104 -- of the object designated by the result value identifies T.
9105 -- Constraint_Error is raised if this check fails.
9107 if Nkind (Parent (N)) = Sinfo.N_Return_Statement then
9110 Func_Typ : Entity_Id;
9113 -- Climb scope stack looking for the enclosing function
9115 Func := Current_Scope;
9116 while Present (Func)
9117 and then Ekind (Func) /= E_Function
9119 Func := Scope (Func);
9122 -- The function's return subtype must be defined using
9123 -- an access definition.
9125 if Nkind (Result_Definition (Parent (Func))) =
9128 Func_Typ := Directly_Designated_Type (Etype (Func));
9130 -- The return subtype denotes a specific tagged type,
9131 -- in other words, a non class-wide type.
9133 if Is_Tagged_Type (Func_Typ)
9134 and then not Is_Class_Wide_Type (Func_Typ)
9136 Make_Tag_Check (Actual_Targ_Typ);
9137 Make_Conversion := True;
9143 -- We have generated a tag check for either a class-wide type
9144 -- conversion or for AI05-0073.
9146 if Make_Conversion then
9151 Make_Unchecked_Type_Conversion (Loc,
9152 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
9153 Expression => Relocate_Node (Expression (N)));
9155 Analyze_And_Resolve (N, Target_Type);
9159 end Tagged_Conversion;
9161 -- Case of other access type conversions
9163 elsif Is_Access_Type (Target_Type) then
9164 Apply_Constraint_Check (Operand, Target_Type);
9166 -- Case of conversions from a fixed-point type
9168 -- These conversions require special expansion and processing, found in
9169 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
9170 -- since from a semantic point of view, these are simple integer
9171 -- conversions, which do not need further processing.
9173 elsif Is_Fixed_Point_Type (Operand_Type)
9174 and then not Conversion_OK (N)
9176 -- We should never see universal fixed at this case, since the
9177 -- expansion of the constituent divide or multiply should have
9178 -- eliminated the explicit mention of universal fixed.
9180 pragma Assert (Operand_Type /= Universal_Fixed);
9182 -- Check for special case of the conversion to universal real that
9183 -- occurs as a result of the use of a round attribute. In this case,
9184 -- the real type for the conversion is taken from the target type of
9185 -- the Round attribute and the result must be marked as rounded.
9187 if Target_Type = Universal_Real
9188 and then Nkind (Parent (N)) = N_Attribute_Reference
9189 and then Attribute_Name (Parent (N)) = Name_Round
9191 Set_Rounded_Result (N);
9192 Set_Etype (N, Etype (Parent (N)));
9195 -- Otherwise do correct fixed-conversion, but skip these if the
9196 -- Conversion_OK flag is set, because from a semantic point of view
9197 -- these are simple integer conversions needing no further processing
9198 -- (the backend will simply treat them as integers).
9200 if not Conversion_OK (N) then
9201 if Is_Fixed_Point_Type (Etype (N)) then
9202 Expand_Convert_Fixed_To_Fixed (N);
9205 elsif Is_Integer_Type (Etype (N)) then
9206 Expand_Convert_Fixed_To_Integer (N);
9209 pragma Assert (Is_Floating_Point_Type (Etype (N)));
9210 Expand_Convert_Fixed_To_Float (N);
9215 -- Case of conversions to a fixed-point type
9217 -- These conversions require special expansion and processing, found in
9218 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
9219 -- since from a semantic point of view, these are simple integer
9220 -- conversions, which do not need further processing.
9222 elsif Is_Fixed_Point_Type (Target_Type)
9223 and then not Conversion_OK (N)
9225 if Is_Integer_Type (Operand_Type) then
9226 Expand_Convert_Integer_To_Fixed (N);
9229 pragma Assert (Is_Floating_Point_Type (Operand_Type));
9230 Expand_Convert_Float_To_Fixed (N);
9234 -- Case of float-to-integer conversions
9236 -- We also handle float-to-fixed conversions with Conversion_OK set
9237 -- since semantically the fixed-point target is treated as though it
9238 -- were an integer in such cases.
9240 elsif Is_Floating_Point_Type (Operand_Type)
9242 (Is_Integer_Type (Target_Type)
9244 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
9246 -- One more check here, gcc is still not able to do conversions of
9247 -- this type with proper overflow checking, and so gigi is doing an
9248 -- approximation of what is required by doing floating-point compares
9249 -- with the end-point. But that can lose precision in some cases, and
9250 -- give a wrong result. Converting the operand to Universal_Real is
9251 -- helpful, but still does not catch all cases with 64-bit integers
9252 -- on targets with only 64-bit floats.
9254 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
9255 -- Can this code be removed ???
9257 if Do_Range_Check (Operand) then
9259 Make_Type_Conversion (Loc,
9261 New_Occurrence_Of (Universal_Real, Loc),
9263 Relocate_Node (Operand)));
9265 Set_Etype (Operand, Universal_Real);
9266 Enable_Range_Check (Operand);
9267 Set_Do_Range_Check (Expression (Operand), False);
9270 -- Case of array conversions
9272 -- Expansion of array conversions, add required length/range checks but
9273 -- only do this if there is no change of representation. For handling of
9274 -- this case, see Handle_Changed_Representation.
9276 elsif Is_Array_Type (Target_Type) then
9277 if Is_Constrained (Target_Type) then
9278 Apply_Length_Check (Operand, Target_Type);
9280 Apply_Range_Check (Operand, Target_Type);
9283 Handle_Changed_Representation;
9285 -- Case of conversions of discriminated types
9287 -- Add required discriminant checks if target is constrained. Again this
9288 -- change is skipped if we have a change of representation.
9290 elsif Has_Discriminants (Target_Type)
9291 and then Is_Constrained (Target_Type)
9293 Apply_Discriminant_Check (Operand, Target_Type);
9294 Handle_Changed_Representation;
9296 -- Case of all other record conversions. The only processing required
9297 -- is to check for a change of representation requiring the special
9298 -- assignment processing.
9300 elsif Is_Record_Type (Target_Type) then
9302 -- Ada 2005 (AI-216): Program_Error is raised when converting from
9303 -- a derived Unchecked_Union type to an unconstrained type that is
9304 -- not Unchecked_Union if the operand lacks inferable discriminants.
9306 if Is_Derived_Type (Operand_Type)
9307 and then Is_Unchecked_Union (Base_Type (Operand_Type))
9308 and then not Is_Constrained (Target_Type)
9309 and then not Is_Unchecked_Union (Base_Type (Target_Type))
9310 and then not Has_Inferable_Discriminants (Operand)
9312 -- To prevent Gigi from generating illegal code, we generate a
9313 -- Program_Error node, but we give it the target type of the
9317 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
9318 Reason => PE_Unchecked_Union_Restriction);
9321 Set_Etype (PE, Target_Type);
9326 Handle_Changed_Representation;
9329 -- Case of conversions of enumeration types
9331 elsif Is_Enumeration_Type (Target_Type) then
9333 -- Special processing is required if there is a change of
9334 -- representation (from enumeration representation clauses).
9336 if not Same_Representation (Target_Type, Operand_Type) then
9338 -- Convert: x(y) to x'val (ytyp'val (y))
9341 Make_Attribute_Reference (Loc,
9342 Prefix => New_Occurrence_Of (Target_Type, Loc),
9343 Attribute_Name => Name_Val,
9344 Expressions => New_List (
9345 Make_Attribute_Reference (Loc,
9346 Prefix => New_Occurrence_Of (Operand_Type, Loc),
9347 Attribute_Name => Name_Pos,
9348 Expressions => New_List (Operand)))));
9350 Analyze_And_Resolve (N, Target_Type);
9353 -- Case of conversions to floating-point
9355 elsif Is_Floating_Point_Type (Target_Type) then
9359 -- At this stage, either the conversion node has been transformed into
9360 -- some other equivalent expression, or left as a conversion that can be
9361 -- handled by Gigi, in the following cases:
9363 -- Conversions with no change of representation or type
9365 -- Numeric conversions involving integer, floating- and fixed-point
9366 -- values. Fixed-point values are allowed only if Conversion_OK is
9367 -- set, i.e. if the fixed-point values are to be treated as integers.
9369 -- No other conversions should be passed to Gigi
9371 -- Check: are these rules stated in sinfo??? if so, why restate here???
9373 -- The only remaining step is to generate a range check if we still have
9374 -- a type conversion at this stage and Do_Range_Check is set. For now we
9375 -- do this only for conversions of discrete types.
9377 if Nkind (N) = N_Type_Conversion
9378 and then Is_Discrete_Type (Etype (N))
9381 Expr : constant Node_Id := Expression (N);
9386 if Do_Range_Check (Expr)
9387 and then Is_Discrete_Type (Etype (Expr))
9389 Set_Do_Range_Check (Expr, False);
9391 -- Before we do a range check, we have to deal with treating a
9392 -- fixed-point operand as an integer. The way we do this is
9393 -- simply to do an unchecked conversion to an appropriate
9394 -- integer type large enough to hold the result.
9396 -- This code is not active yet, because we are only dealing
9397 -- with discrete types so far ???
9399 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
9400 and then Treat_Fixed_As_Integer (Expr)
9402 Ftyp := Base_Type (Etype (Expr));
9404 if Esize (Ftyp) >= Esize (Standard_Integer) then
9405 Ityp := Standard_Long_Long_Integer;
9407 Ityp := Standard_Integer;
9410 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
9413 -- Reset overflow flag, since the range check will include
9414 -- dealing with possible overflow, and generate the check. If
9415 -- Address is either a source type or target type, suppress
9416 -- range check to avoid typing anomalies when it is a visible
9419 Set_Do_Overflow_Check (N, False);
9420 if not Is_Descendent_Of_Address (Etype (Expr))
9421 and then not Is_Descendent_Of_Address (Target_Type)
9423 Generate_Range_Check
9424 (Expr, Target_Type, CE_Range_Check_Failed);
9430 -- Final step, if the result is a type conversion involving Vax_Float
9431 -- types, then it is subject for further special processing.
9433 if Nkind (N) = N_Type_Conversion
9434 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
9436 Expand_Vax_Conversion (N);
9440 -- Here at end of processing
9443 -- Apply predicate check if required. Note that we can't just call
9444 -- Apply_Predicate_Check here, because the type looks right after
9445 -- the conversion and it would omit the check. The Comes_From_Source
9446 -- guard is necessary to prevent infinite recursions when we generate
9447 -- internal conversions for the purpose of checking predicates.
9449 if Present (Predicate_Function (Target_Type))
9450 and then Target_Type /= Operand_Type
9451 and then Comes_From_Source (N)
9454 New_Expr : constant Node_Id := Duplicate_Subexpr (N);
9457 -- Avoid infinite recursion on the subsequent expansion of
9458 -- of the copy of the original type conversion.
9460 Set_Comes_From_Source (New_Expr, False);
9461 Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr));
9464 end Expand_N_Type_Conversion;
9466 -----------------------------------
9467 -- Expand_N_Unchecked_Expression --
9468 -----------------------------------
9470 -- Remove the unchecked expression node from the tree. Its job was simply
9471 -- to make sure that its constituent expression was handled with checks
9472 -- off, and now that that is done, we can remove it from the tree, and
9473 -- indeed must, since Gigi does not expect to see these nodes.
9475 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
9476 Exp : constant Node_Id := Expression (N);
9478 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
9480 end Expand_N_Unchecked_Expression;
9482 ----------------------------------------
9483 -- Expand_N_Unchecked_Type_Conversion --
9484 ----------------------------------------
9486 -- If this cannot be handled by Gigi and we haven't already made a
9487 -- temporary for it, do it now.
9489 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
9490 Target_Type : constant Entity_Id := Etype (N);
9491 Operand : constant Node_Id := Expression (N);
9492 Operand_Type : constant Entity_Id := Etype (Operand);
9495 -- Nothing at all to do if conversion is to the identical type so remove
9496 -- the conversion completely, it is useless, except that it may carry
9497 -- an Assignment_OK indication which must be propagated to the operand.
9499 if Operand_Type = Target_Type then
9501 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
9503 if Assignment_OK (N) then
9504 Set_Assignment_OK (Operand);
9507 Rewrite (N, Relocate_Node (Operand));
9511 -- If we have a conversion of a compile time known value to a target
9512 -- type and the value is in range of the target type, then we can simply
9513 -- replace the construct by an integer literal of the correct type. We
9514 -- only apply this to integer types being converted. Possibly it may
9515 -- apply in other cases, but it is too much trouble to worry about.
9517 -- Note that we do not do this transformation if the Kill_Range_Check
9518 -- flag is set, since then the value may be outside the expected range.
9519 -- This happens in the Normalize_Scalars case.
9521 -- We also skip this if either the target or operand type is biased
9522 -- because in this case, the unchecked conversion is supposed to
9523 -- preserve the bit pattern, not the integer value.
9525 if Is_Integer_Type (Target_Type)
9526 and then not Has_Biased_Representation (Target_Type)
9527 and then Is_Integer_Type (Operand_Type)
9528 and then not Has_Biased_Representation (Operand_Type)
9529 and then Compile_Time_Known_Value (Operand)
9530 and then not Kill_Range_Check (N)
9533 Val : constant Uint := Expr_Value (Operand);
9536 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
9538 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
9540 Val >= Expr_Value (Type_Low_Bound (Target_Type))
9542 Val <= Expr_Value (Type_High_Bound (Target_Type))
9544 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
9546 -- If Address is the target type, just set the type to avoid a
9547 -- spurious type error on the literal when Address is a visible
9550 if Is_Descendent_Of_Address (Target_Type) then
9551 Set_Etype (N, Target_Type);
9553 Analyze_And_Resolve (N, Target_Type);
9561 -- Nothing to do if conversion is safe
9563 if Safe_Unchecked_Type_Conversion (N) then
9567 -- Otherwise force evaluation unless Assignment_OK flag is set (this
9568 -- flag indicates ??? -- more comments needed here)
9570 if Assignment_OK (N) then
9573 Force_Evaluation (N);
9575 end Expand_N_Unchecked_Type_Conversion;
9577 ----------------------------
9578 -- Expand_Record_Equality --
9579 ----------------------------
9581 -- For non-variant records, Equality is expanded when needed into:
9583 -- and then Lhs.Discr1 = Rhs.Discr1
9585 -- and then Lhs.Discrn = Rhs.Discrn
9586 -- and then Lhs.Cmp1 = Rhs.Cmp1
9588 -- and then Lhs.Cmpn = Rhs.Cmpn
9590 -- The expression is folded by the back-end for adjacent fields. This
9591 -- function is called for tagged record in only one occasion: for imple-
9592 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
9593 -- otherwise the primitive "=" is used directly.
9595 function Expand_Record_Equality
9600 Bodies : List_Id) return Node_Id
9602 Loc : constant Source_Ptr := Sloc (Nod);
9607 First_Time : Boolean := True;
9609 function Suitable_Element (C : Entity_Id) return Entity_Id;
9610 -- Return the first field to compare beginning with C, skipping the
9611 -- inherited components.
9613 ----------------------
9614 -- Suitable_Element --
9615 ----------------------
9617 function Suitable_Element (C : Entity_Id) return Entity_Id is
9622 elsif Ekind (C) /= E_Discriminant
9623 and then Ekind (C) /= E_Component
9625 return Suitable_Element (Next_Entity (C));
9627 elsif Is_Tagged_Type (Typ)
9628 and then C /= Original_Record_Component (C)
9630 return Suitable_Element (Next_Entity (C));
9632 elsif Chars (C) = Name_uTag then
9633 return Suitable_Element (Next_Entity (C));
9635 -- The .NET/JVM version of type Root_Controlled contains two fields
9636 -- which should not be considered part of the object. To achieve
9637 -- proper equiality between two controlled objects on .NET/JVM, skip
9638 -- field _parent whenever it is of type Root_Controlled.
9640 elsif Chars (C) = Name_uParent
9641 and then VM_Target /= No_VM
9642 and then Etype (C) = RTE (RE_Root_Controlled)
9644 return Suitable_Element (Next_Entity (C));
9646 elsif Is_Interface (Etype (C)) then
9647 return Suitable_Element (Next_Entity (C));
9652 end Suitable_Element;
9654 -- Start of processing for Expand_Record_Equality
9657 -- Generates the following code: (assuming that Typ has one Discr and
9658 -- component C2 is also a record)
9661 -- and then Lhs.Discr1 = Rhs.Discr1
9662 -- and then Lhs.C1 = Rhs.C1
9663 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
9665 -- and then Lhs.Cmpn = Rhs.Cmpn
9667 Result := New_Reference_To (Standard_True, Loc);
9668 C := Suitable_Element (First_Entity (Typ));
9669 while Present (C) loop
9677 First_Time := False;
9681 New_Lhs := New_Copy_Tree (Lhs);
9682 New_Rhs := New_Copy_Tree (Rhs);
9686 Expand_Composite_Equality (Nod, Etype (C),
9688 Make_Selected_Component (Loc,
9690 Selector_Name => New_Reference_To (C, Loc)),
9692 Make_Selected_Component (Loc,
9694 Selector_Name => New_Reference_To (C, Loc)),
9697 -- If some (sub)component is an unchecked_union, the whole
9698 -- operation will raise program error.
9700 if Nkind (Check) = N_Raise_Program_Error then
9702 Set_Etype (Result, Standard_Boolean);
9707 Left_Opnd => Result,
9708 Right_Opnd => Check);
9712 C := Suitable_Element (Next_Entity (C));
9716 end Expand_Record_Equality;
9718 ---------------------------
9719 -- Expand_Set_Membership --
9720 ---------------------------
9722 procedure Expand_Set_Membership (N : Node_Id) is
9723 Lop : constant Node_Id := Left_Opnd (N);
9727 function Make_Cond (Alt : Node_Id) return Node_Id;
9728 -- If the alternative is a subtype mark, create a simple membership
9729 -- test. Otherwise create an equality test for it.
9735 function Make_Cond (Alt : Node_Id) return Node_Id is
9737 L : constant Node_Id := New_Copy (Lop);
9738 R : constant Node_Id := Relocate_Node (Alt);
9741 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
9742 or else Nkind (Alt) = N_Range
9745 Make_In (Sloc (Alt),
9750 Make_Op_Eq (Sloc (Alt),
9758 -- Start of processing for Expand_Set_Membership
9761 Remove_Side_Effects (Lop);
9763 Alt := Last (Alternatives (N));
9764 Res := Make_Cond (Alt);
9767 while Present (Alt) loop
9769 Make_Or_Else (Sloc (Alt),
9770 Left_Opnd => Make_Cond (Alt),
9776 Analyze_And_Resolve (N, Standard_Boolean);
9777 end Expand_Set_Membership;
9779 -----------------------------------
9780 -- Expand_Short_Circuit_Operator --
9781 -----------------------------------
9783 -- Deal with special expansion if actions are present for the right operand
9784 -- and deal with optimizing case of arguments being True or False. We also
9785 -- deal with the special case of non-standard boolean values.
9787 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
9788 Loc : constant Source_Ptr := Sloc (N);
9789 Typ : constant Entity_Id := Etype (N);
9790 Left : constant Node_Id := Left_Opnd (N);
9791 Right : constant Node_Id := Right_Opnd (N);
9792 LocR : constant Source_Ptr := Sloc (Right);
9795 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
9796 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
9797 -- If Left = Shortcut_Value then Right need not be evaluated
9799 function Make_Test_Expr (Opnd : Node_Id) return Node_Id;
9800 -- For Opnd a boolean expression, return a Boolean expression equivalent
9801 -- to Opnd /= Shortcut_Value.
9803 --------------------
9804 -- Make_Test_Expr --
9805 --------------------
9807 function Make_Test_Expr (Opnd : Node_Id) return Node_Id is
9809 if Shortcut_Value then
9810 return Make_Op_Not (Sloc (Opnd), Opnd);
9817 -- Entity for a temporary variable holding the value of the operator,
9818 -- used for expansion in the case where actions are present.
9820 -- Start of processing for Expand_Short_Circuit_Operator
9823 -- Deal with non-standard booleans
9825 if Is_Boolean_Type (Typ) then
9826 Adjust_Condition (Left);
9827 Adjust_Condition (Right);
9828 Set_Etype (N, Standard_Boolean);
9831 -- Check for cases where left argument is known to be True or False
9833 if Compile_Time_Known_Value (Left) then
9835 -- Mark SCO for left condition as compile time known
9837 if Generate_SCO and then Comes_From_Source (Left) then
9838 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
9841 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
9842 -- Any actions associated with Right will be executed unconditionally
9843 -- and can thus be inserted into the tree unconditionally.
9845 if Expr_Value_E (Left) /= Shortcut_Ent then
9846 if Present (Actions (N)) then
9847 Insert_Actions (N, Actions (N));
9852 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
9853 -- In this case we can forget the actions associated with Right,
9854 -- since they will never be executed.
9857 Kill_Dead_Code (Right);
9858 Kill_Dead_Code (Actions (N));
9859 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
9862 Adjust_Result_Type (N, Typ);
9866 -- If Actions are present for the right operand, we have to do some
9867 -- special processing. We can't just let these actions filter back into
9868 -- code preceding the short circuit (which is what would have happened
9869 -- if we had not trapped them in the short-circuit form), since they
9870 -- must only be executed if the right operand of the short circuit is
9871 -- executed and not otherwise.
9873 -- the temporary variable C.
9875 if Present (Actions (N)) then
9876 Actlist := Actions (N);
9878 -- The old approach is to expand:
9880 -- left AND THEN right
9884 -- C : Boolean := False;
9892 -- and finally rewrite the operator into a reference to C. Similarly
9893 -- for left OR ELSE right, with negated values. Note that this
9894 -- rewrite causes some difficulties for coverage analysis because
9895 -- of the introduction of the new variable C, which obscures the
9896 -- structure of the test.
9898 -- We use this "old approach" if use of N_Expression_With_Actions
9899 -- is False (see description in Opt of when this is or is not set).
9901 if not Use_Expression_With_Actions then
9902 Op_Var := Make_Temporary (Loc, 'C', Related_Node => N);
9905 Make_Object_Declaration (Loc,
9906 Defining_Identifier =>
9908 Object_Definition =>
9909 New_Occurrence_Of (Standard_Boolean, Loc),
9911 New_Occurrence_Of (Shortcut_Ent, Loc)));
9914 Make_Implicit_If_Statement (Right,
9915 Condition => Make_Test_Expr (Right),
9916 Then_Statements => New_List (
9917 Make_Assignment_Statement (LocR,
9918 Name => New_Occurrence_Of (Op_Var, LocR),
9921 (Boolean_Literals (not Shortcut_Value), LocR)))));
9924 Make_Implicit_If_Statement (Left,
9925 Condition => Make_Test_Expr (Left),
9926 Then_Statements => Actlist));
9928 Rewrite (N, New_Occurrence_Of (Op_Var, Loc));
9929 Analyze_And_Resolve (N, Standard_Boolean);
9931 -- The new approach, activated for now by the use of debug flag
9932 -- -gnatd.X is to use the new Expression_With_Actions node for the
9933 -- right operand of the short-circuit form. This should solve the
9934 -- traceability problems for coverage analysis.
9938 Make_Expression_With_Actions (LocR,
9939 Expression => Relocate_Node (Right),
9940 Actions => Actlist));
9941 Set_Actions (N, No_List);
9942 Analyze_And_Resolve (Right, Standard_Boolean);
9945 Adjust_Result_Type (N, Typ);
9949 -- No actions present, check for cases of right argument True/False
9951 if Compile_Time_Known_Value (Right) then
9953 -- Mark SCO for left condition as compile time known
9955 if Generate_SCO and then Comes_From_Source (Right) then
9956 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
9959 -- Change (Left and then True), (Left or else False) to Left.
9960 -- Note that we know there are no actions associated with the right
9961 -- operand, since we just checked for this case above.
9963 if Expr_Value_E (Right) /= Shortcut_Ent then
9966 -- Change (Left and then False), (Left or else True) to Right,
9967 -- making sure to preserve any side effects associated with the Left
9971 Remove_Side_Effects (Left);
9972 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
9976 Adjust_Result_Type (N, Typ);
9977 end Expand_Short_Circuit_Operator;
9979 -------------------------------------
9980 -- Fixup_Universal_Fixed_Operation --
9981 -------------------------------------
9983 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
9984 Conv : constant Node_Id := Parent (N);
9987 -- We must have a type conversion immediately above us
9989 pragma Assert (Nkind (Conv) = N_Type_Conversion);
9991 -- Normally the type conversion gives our target type. The exception
9992 -- occurs in the case of the Round attribute, where the conversion
9993 -- will be to universal real, and our real type comes from the Round
9994 -- attribute (as well as an indication that we must round the result)
9996 if Nkind (Parent (Conv)) = N_Attribute_Reference
9997 and then Attribute_Name (Parent (Conv)) = Name_Round
9999 Set_Etype (N, Etype (Parent (Conv)));
10000 Set_Rounded_Result (N);
10002 -- Normal case where type comes from conversion above us
10005 Set_Etype (N, Etype (Conv));
10007 end Fixup_Universal_Fixed_Operation;
10009 ---------------------------------
10010 -- Has_Inferable_Discriminants --
10011 ---------------------------------
10013 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
10015 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
10016 -- Determines whether the left-most prefix of a selected component is a
10017 -- formal parameter in a subprogram. Assumes N is a selected component.
10019 --------------------------------
10020 -- Prefix_Is_Formal_Parameter --
10021 --------------------------------
10023 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
10024 Sel_Comp : Node_Id := N;
10027 -- Move to the left-most prefix by climbing up the tree
10029 while Present (Parent (Sel_Comp))
10030 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
10032 Sel_Comp := Parent (Sel_Comp);
10035 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
10036 end Prefix_Is_Formal_Parameter;
10038 -- Start of processing for Has_Inferable_Discriminants
10041 -- For identifiers and indexed components, it is sufficient to have a
10042 -- constrained Unchecked_Union nominal subtype.
10044 if Nkind_In (N, N_Identifier, N_Indexed_Component) then
10045 return Is_Unchecked_Union (Base_Type (Etype (N)))
10047 Is_Constrained (Etype (N));
10049 -- For selected components, the subtype of the selector must be a
10050 -- constrained Unchecked_Union. If the component is subject to a
10051 -- per-object constraint, then the enclosing object must have inferable
10054 elsif Nkind (N) = N_Selected_Component then
10055 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
10057 -- A small hack. If we have a per-object constrained selected
10058 -- component of a formal parameter, return True since we do not
10059 -- know the actual parameter association yet.
10061 if Prefix_Is_Formal_Parameter (N) then
10065 -- Otherwise, check the enclosing object and the selector
10067 return Has_Inferable_Discriminants (Prefix (N))
10069 Has_Inferable_Discriminants (Selector_Name (N));
10072 -- The call to Has_Inferable_Discriminants will determine whether
10073 -- the selector has a constrained Unchecked_Union nominal type.
10075 return Has_Inferable_Discriminants (Selector_Name (N));
10077 -- A qualified expression has inferable discriminants if its subtype
10078 -- mark is a constrained Unchecked_Union subtype.
10080 elsif Nkind (N) = N_Qualified_Expression then
10081 return Is_Unchecked_Union (Subtype_Mark (N))
10083 Is_Constrained (Subtype_Mark (N));
10088 end Has_Inferable_Discriminants;
10090 -------------------------------
10091 -- Insert_Dereference_Action --
10092 -------------------------------
10094 procedure Insert_Dereference_Action (N : Node_Id) is
10095 Loc : constant Source_Ptr := Sloc (N);
10096 Typ : constant Entity_Id := Etype (N);
10097 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
10098 Pnod : constant Node_Id := Parent (N);
10100 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
10101 -- Return true if type of P is derived from Checked_Pool;
10103 -----------------------------
10104 -- Is_Checked_Storage_Pool --
10105 -----------------------------
10107 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
10116 while T /= Etype (T) loop
10117 if Is_RTE (T, RE_Checked_Pool) then
10125 end Is_Checked_Storage_Pool;
10127 -- Start of processing for Insert_Dereference_Action
10130 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
10132 if not (Is_Checked_Storage_Pool (Pool)
10133 and then Comes_From_Source (Original_Node (Pnod)))
10139 Make_Procedure_Call_Statement (Loc,
10140 Name => New_Reference_To (
10141 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
10143 Parameter_Associations => New_List (
10147 New_Reference_To (Pool, Loc),
10149 -- Storage_Address. We use the attribute Pool_Address, which uses
10150 -- the pointer itself to find the address of the object, and which
10151 -- handles unconstrained arrays properly by computing the address
10152 -- of the template. i.e. the correct address of the corresponding
10155 Make_Attribute_Reference (Loc,
10156 Prefix => Duplicate_Subexpr_Move_Checks (N),
10157 Attribute_Name => Name_Pool_Address),
10159 -- Size_In_Storage_Elements
10161 Make_Op_Divide (Loc,
10163 Make_Attribute_Reference (Loc,
10165 Make_Explicit_Dereference (Loc,
10166 Duplicate_Subexpr_Move_Checks (N)),
10167 Attribute_Name => Name_Size),
10169 Make_Integer_Literal (Loc, System_Storage_Unit)),
10173 Make_Attribute_Reference (Loc,
10175 Make_Explicit_Dereference (Loc,
10176 Duplicate_Subexpr_Move_Checks (N)),
10177 Attribute_Name => Name_Alignment))));
10180 when RE_Not_Available =>
10182 end Insert_Dereference_Action;
10184 --------------------------------
10185 -- Integer_Promotion_Possible --
10186 --------------------------------
10188 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
10189 Operand : constant Node_Id := Expression (N);
10190 Operand_Type : constant Entity_Id := Etype (Operand);
10191 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
10194 pragma Assert (Nkind (N) = N_Type_Conversion);
10198 -- We only do the transformation for source constructs. We assume
10199 -- that the expander knows what it is doing when it generates code.
10201 Comes_From_Source (N)
10203 -- If the operand type is Short_Integer or Short_Short_Integer,
10204 -- then we will promote to Integer, which is available on all
10205 -- targets, and is sufficient to ensure no intermediate overflow.
10206 -- Furthermore it is likely to be as efficient or more efficient
10207 -- than using the smaller type for the computation so we do this
10208 -- unconditionally.
10211 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
10213 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
10215 -- Test for interesting operation, which includes addition,
10216 -- division, exponentiation, multiplication, subtraction, absolute
10217 -- value and unary negation. Unary "+" is omitted since it is a
10218 -- no-op and thus can't overflow.
10220 and then Nkind_In (Operand, N_Op_Abs,
10227 end Integer_Promotion_Possible;
10229 ------------------------------
10230 -- Make_Array_Comparison_Op --
10231 ------------------------------
10233 -- This is a hand-coded expansion of the following generic function:
10236 -- type elem is (<>);
10237 -- type index is (<>);
10238 -- type a is array (index range <>) of elem;
10240 -- function Gnnn (X : a; Y: a) return boolean is
10241 -- J : index := Y'first;
10244 -- if X'length = 0 then
10247 -- elsif Y'length = 0 then
10251 -- for I in X'range loop
10252 -- if X (I) = Y (J) then
10253 -- if J = Y'last then
10256 -- J := index'succ (J);
10260 -- return X (I) > Y (J);
10264 -- return X'length > Y'length;
10268 -- Note that since we are essentially doing this expansion by hand, we
10269 -- do not need to generate an actual or formal generic part, just the
10270 -- instantiated function itself.
10272 function Make_Array_Comparison_Op
10274 Nod : Node_Id) return Node_Id
10276 Loc : constant Source_Ptr := Sloc (Nod);
10278 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
10279 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
10280 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
10281 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
10283 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
10285 Loop_Statement : Node_Id;
10286 Loop_Body : Node_Id;
10288 Inner_If : Node_Id;
10289 Final_Expr : Node_Id;
10290 Func_Body : Node_Id;
10291 Func_Name : Entity_Id;
10297 -- if J = Y'last then
10300 -- J := index'succ (J);
10304 Make_Implicit_If_Statement (Nod,
10307 Left_Opnd => New_Reference_To (J, Loc),
10309 Make_Attribute_Reference (Loc,
10310 Prefix => New_Reference_To (Y, Loc),
10311 Attribute_Name => Name_Last)),
10313 Then_Statements => New_List (
10314 Make_Exit_Statement (Loc)),
10318 Make_Assignment_Statement (Loc,
10319 Name => New_Reference_To (J, Loc),
10321 Make_Attribute_Reference (Loc,
10322 Prefix => New_Reference_To (Index, Loc),
10323 Attribute_Name => Name_Succ,
10324 Expressions => New_List (New_Reference_To (J, Loc))))));
10326 -- if X (I) = Y (J) then
10329 -- return X (I) > Y (J);
10333 Make_Implicit_If_Statement (Nod,
10337 Make_Indexed_Component (Loc,
10338 Prefix => New_Reference_To (X, Loc),
10339 Expressions => New_List (New_Reference_To (I, Loc))),
10342 Make_Indexed_Component (Loc,
10343 Prefix => New_Reference_To (Y, Loc),
10344 Expressions => New_List (New_Reference_To (J, Loc)))),
10346 Then_Statements => New_List (Inner_If),
10348 Else_Statements => New_List (
10349 Make_Simple_Return_Statement (Loc,
10353 Make_Indexed_Component (Loc,
10354 Prefix => New_Reference_To (X, Loc),
10355 Expressions => New_List (New_Reference_To (I, Loc))),
10358 Make_Indexed_Component (Loc,
10359 Prefix => New_Reference_To (Y, Loc),
10360 Expressions => New_List (
10361 New_Reference_To (J, Loc)))))));
10363 -- for I in X'range loop
10368 Make_Implicit_Loop_Statement (Nod,
10369 Identifier => Empty,
10371 Iteration_Scheme =>
10372 Make_Iteration_Scheme (Loc,
10373 Loop_Parameter_Specification =>
10374 Make_Loop_Parameter_Specification (Loc,
10375 Defining_Identifier => I,
10376 Discrete_Subtype_Definition =>
10377 Make_Attribute_Reference (Loc,
10378 Prefix => New_Reference_To (X, Loc),
10379 Attribute_Name => Name_Range))),
10381 Statements => New_List (Loop_Body));
10383 -- if X'length = 0 then
10385 -- elsif Y'length = 0 then
10388 -- for ... loop ... end loop;
10389 -- return X'length > Y'length;
10393 Make_Attribute_Reference (Loc,
10394 Prefix => New_Reference_To (X, Loc),
10395 Attribute_Name => Name_Length);
10398 Make_Attribute_Reference (Loc,
10399 Prefix => New_Reference_To (Y, Loc),
10400 Attribute_Name => Name_Length);
10404 Left_Opnd => Length1,
10405 Right_Opnd => Length2);
10408 Make_Implicit_If_Statement (Nod,
10412 Make_Attribute_Reference (Loc,
10413 Prefix => New_Reference_To (X, Loc),
10414 Attribute_Name => Name_Length),
10416 Make_Integer_Literal (Loc, 0)),
10420 Make_Simple_Return_Statement (Loc,
10421 Expression => New_Reference_To (Standard_False, Loc))),
10423 Elsif_Parts => New_List (
10424 Make_Elsif_Part (Loc,
10428 Make_Attribute_Reference (Loc,
10429 Prefix => New_Reference_To (Y, Loc),
10430 Attribute_Name => Name_Length),
10432 Make_Integer_Literal (Loc, 0)),
10436 Make_Simple_Return_Statement (Loc,
10437 Expression => New_Reference_To (Standard_True, Loc))))),
10439 Else_Statements => New_List (
10441 Make_Simple_Return_Statement (Loc,
10442 Expression => Final_Expr)));
10446 Formals := New_List (
10447 Make_Parameter_Specification (Loc,
10448 Defining_Identifier => X,
10449 Parameter_Type => New_Reference_To (Typ, Loc)),
10451 Make_Parameter_Specification (Loc,
10452 Defining_Identifier => Y,
10453 Parameter_Type => New_Reference_To (Typ, Loc)));
10455 -- function Gnnn (...) return boolean is
10456 -- J : index := Y'first;
10461 Func_Name := Make_Temporary (Loc, 'G');
10464 Make_Subprogram_Body (Loc,
10466 Make_Function_Specification (Loc,
10467 Defining_Unit_Name => Func_Name,
10468 Parameter_Specifications => Formals,
10469 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
10471 Declarations => New_List (
10472 Make_Object_Declaration (Loc,
10473 Defining_Identifier => J,
10474 Object_Definition => New_Reference_To (Index, Loc),
10476 Make_Attribute_Reference (Loc,
10477 Prefix => New_Reference_To (Y, Loc),
10478 Attribute_Name => Name_First))),
10480 Handled_Statement_Sequence =>
10481 Make_Handled_Sequence_Of_Statements (Loc,
10482 Statements => New_List (If_Stat)));
10485 end Make_Array_Comparison_Op;
10487 ---------------------------
10488 -- Make_Boolean_Array_Op --
10489 ---------------------------
10491 -- For logical operations on boolean arrays, expand in line the following,
10492 -- replacing 'and' with 'or' or 'xor' where needed:
10494 -- function Annn (A : typ; B: typ) return typ is
10497 -- for J in A'range loop
10498 -- C (J) := A (J) op B (J);
10503 -- Here typ is the boolean array type
10505 function Make_Boolean_Array_Op
10507 N : Node_Id) return Node_Id
10509 Loc : constant Source_Ptr := Sloc (N);
10511 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
10512 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
10513 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
10514 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
10522 Func_Name : Entity_Id;
10523 Func_Body : Node_Id;
10524 Loop_Statement : Node_Id;
10528 Make_Indexed_Component (Loc,
10529 Prefix => New_Reference_To (A, Loc),
10530 Expressions => New_List (New_Reference_To (J, Loc)));
10533 Make_Indexed_Component (Loc,
10534 Prefix => New_Reference_To (B, Loc),
10535 Expressions => New_List (New_Reference_To (J, Loc)));
10538 Make_Indexed_Component (Loc,
10539 Prefix => New_Reference_To (C, Loc),
10540 Expressions => New_List (New_Reference_To (J, Loc)));
10542 if Nkind (N) = N_Op_And then
10546 Right_Opnd => B_J);
10548 elsif Nkind (N) = N_Op_Or then
10552 Right_Opnd => B_J);
10558 Right_Opnd => B_J);
10562 Make_Implicit_Loop_Statement (N,
10563 Identifier => Empty,
10565 Iteration_Scheme =>
10566 Make_Iteration_Scheme (Loc,
10567 Loop_Parameter_Specification =>
10568 Make_Loop_Parameter_Specification (Loc,
10569 Defining_Identifier => J,
10570 Discrete_Subtype_Definition =>
10571 Make_Attribute_Reference (Loc,
10572 Prefix => New_Reference_To (A, Loc),
10573 Attribute_Name => Name_Range))),
10575 Statements => New_List (
10576 Make_Assignment_Statement (Loc,
10578 Expression => Op)));
10580 Formals := New_List (
10581 Make_Parameter_Specification (Loc,
10582 Defining_Identifier => A,
10583 Parameter_Type => New_Reference_To (Typ, Loc)),
10585 Make_Parameter_Specification (Loc,
10586 Defining_Identifier => B,
10587 Parameter_Type => New_Reference_To (Typ, Loc)));
10589 Func_Name := Make_Temporary (Loc, 'A');
10590 Set_Is_Inlined (Func_Name);
10593 Make_Subprogram_Body (Loc,
10595 Make_Function_Specification (Loc,
10596 Defining_Unit_Name => Func_Name,
10597 Parameter_Specifications => Formals,
10598 Result_Definition => New_Reference_To (Typ, Loc)),
10600 Declarations => New_List (
10601 Make_Object_Declaration (Loc,
10602 Defining_Identifier => C,
10603 Object_Definition => New_Reference_To (Typ, Loc))),
10605 Handled_Statement_Sequence =>
10606 Make_Handled_Sequence_Of_Statements (Loc,
10607 Statements => New_List (
10609 Make_Simple_Return_Statement (Loc,
10610 Expression => New_Reference_To (C, Loc)))));
10613 end Make_Boolean_Array_Op;
10615 --------------------------------
10616 -- Optimize_Length_Comparison --
10617 --------------------------------
10619 procedure Optimize_Length_Comparison (N : Node_Id) is
10620 Loc : constant Source_Ptr := Sloc (N);
10621 Typ : constant Entity_Id := Etype (N);
10626 -- First and Last attribute reference nodes, which end up as left and
10627 -- right operands of the optimized result.
10630 -- True for comparison operand of zero
10633 -- Comparison operand, set only if Is_Zero is false
10636 -- Entity whose length is being compared
10639 -- Integer_Literal node for length attribute expression, or Empty
10640 -- if there is no such expression present.
10643 -- Type of array index to which 'Length is applied
10645 Op : Node_Kind := Nkind (N);
10646 -- Kind of comparison operator, gets flipped if operands backwards
10648 function Is_Optimizable (N : Node_Id) return Boolean;
10649 -- Tests N to see if it is an optimizable comparison value (defined as
10650 -- constant zero or one, or something else where the value is known to
10651 -- be positive and in the range of 32-bits, and where the corresponding
10652 -- Length value is also known to be 32-bits. If result is true, sets
10653 -- Is_Zero, Ityp, and Comp accordingly.
10655 function Is_Entity_Length (N : Node_Id) return Boolean;
10656 -- Tests if N is a length attribute applied to a simple entity. If so,
10657 -- returns True, and sets Ent to the entity, and Index to the integer
10658 -- literal provided as an attribute expression, or to Empty if none.
10659 -- Also returns True if the expression is a generated type conversion
10660 -- whose expression is of the desired form. This latter case arises
10661 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
10662 -- to check for being in range, which is not needed in this context.
10663 -- Returns False if neither condition holds.
10665 function Prepare_64 (N : Node_Id) return Node_Id;
10666 -- Given a discrete expression, returns a Long_Long_Integer typed
10667 -- expression representing the underlying value of the expression.
10668 -- This is done with an unchecked conversion to the result type. We
10669 -- use unchecked conversion to handle the enumeration type case.
10671 ----------------------
10672 -- Is_Entity_Length --
10673 ----------------------
10675 function Is_Entity_Length (N : Node_Id) return Boolean is
10677 if Nkind (N) = N_Attribute_Reference
10678 and then Attribute_Name (N) = Name_Length
10679 and then Is_Entity_Name (Prefix (N))
10681 Ent := Entity (Prefix (N));
10683 if Present (Expressions (N)) then
10684 Index := First (Expressions (N));
10691 elsif Nkind (N) = N_Type_Conversion
10692 and then not Comes_From_Source (N)
10694 return Is_Entity_Length (Expression (N));
10699 end Is_Entity_Length;
10701 --------------------
10702 -- Is_Optimizable --
10703 --------------------
10705 function Is_Optimizable (N : Node_Id) return Boolean is
10713 if Compile_Time_Known_Value (N) then
10714 Val := Expr_Value (N);
10716 if Val = Uint_0 then
10721 elsif Val = Uint_1 then
10728 -- Here we have to make sure of being within 32-bits
10730 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
10733 or else Lo < Uint_1
10734 or else Hi > UI_From_Int (Int'Last)
10739 -- Comparison value was within range, so now we must check the index
10740 -- value to make sure it is also within 32-bits.
10742 Indx := First_Index (Etype (Ent));
10744 if Present (Index) then
10745 for J in 2 .. UI_To_Int (Intval (Index)) loop
10750 Ityp := Etype (Indx);
10752 if Esize (Ityp) > 32 then
10759 end Is_Optimizable;
10765 function Prepare_64 (N : Node_Id) return Node_Id is
10767 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
10770 -- Start of processing for Optimize_Length_Comparison
10773 -- Nothing to do if not a comparison
10775 if Op not in N_Op_Compare then
10779 -- Nothing to do if special -gnatd.P debug flag set
10781 if Debug_Flag_Dot_PP then
10785 -- Ent'Length op 0/1
10787 if Is_Entity_Length (Left_Opnd (N))
10788 and then Is_Optimizable (Right_Opnd (N))
10792 -- 0/1 op Ent'Length
10794 elsif Is_Entity_Length (Right_Opnd (N))
10795 and then Is_Optimizable (Left_Opnd (N))
10797 -- Flip comparison to opposite sense
10800 when N_Op_Lt => Op := N_Op_Gt;
10801 when N_Op_Le => Op := N_Op_Ge;
10802 when N_Op_Gt => Op := N_Op_Lt;
10803 when N_Op_Ge => Op := N_Op_Le;
10804 when others => null;
10807 -- Else optimization not possible
10813 -- Fall through if we will do the optimization
10815 -- Cases to handle:
10817 -- X'Length = 0 => X'First > X'Last
10818 -- X'Length = 1 => X'First = X'Last
10819 -- X'Length = n => X'First + (n - 1) = X'Last
10821 -- X'Length /= 0 => X'First <= X'Last
10822 -- X'Length /= 1 => X'First /= X'Last
10823 -- X'Length /= n => X'First + (n - 1) /= X'Last
10825 -- X'Length >= 0 => always true, warn
10826 -- X'Length >= 1 => X'First <= X'Last
10827 -- X'Length >= n => X'First + (n - 1) <= X'Last
10829 -- X'Length > 0 => X'First <= X'Last
10830 -- X'Length > 1 => X'First < X'Last
10831 -- X'Length > n => X'First + (n - 1) < X'Last
10833 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
10834 -- X'Length <= 1 => X'First >= X'Last
10835 -- X'Length <= n => X'First + (n - 1) >= X'Last
10837 -- X'Length < 0 => always false (warn)
10838 -- X'Length < 1 => X'First > X'Last
10839 -- X'Length < n => X'First + (n - 1) > X'Last
10841 -- Note: for the cases of n (not constant 0,1), we require that the
10842 -- corresponding index type be integer or shorter (i.e. not 64-bit),
10843 -- and the same for the comparison value. Then we do the comparison
10844 -- using 64-bit arithmetic (actually long long integer), so that we
10845 -- cannot have overflow intefering with the result.
10847 -- First deal with warning cases
10856 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
10857 Analyze_And_Resolve (N, Typ);
10858 Warn_On_Known_Condition (N);
10865 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
10866 Analyze_And_Resolve (N, Typ);
10867 Warn_On_Known_Condition (N);
10871 if Constant_Condition_Warnings
10872 and then Comes_From_Source (Original_Node (N))
10874 Error_Msg_N ("could replace by ""'=""?", N);
10884 -- Build the First reference we will use
10887 Make_Attribute_Reference (Loc,
10888 Prefix => New_Occurrence_Of (Ent, Loc),
10889 Attribute_Name => Name_First);
10891 if Present (Index) then
10892 Set_Expressions (Left, New_List (New_Copy (Index)));
10895 -- If general value case, then do the addition of (n - 1), and
10896 -- also add the needed conversions to type Long_Long_Integer.
10898 if Present (Comp) then
10901 Left_Opnd => Prepare_64 (Left),
10903 Make_Op_Subtract (Loc,
10904 Left_Opnd => Prepare_64 (Comp),
10905 Right_Opnd => Make_Integer_Literal (Loc, 1)));
10908 -- Build the Last reference we will use
10911 Make_Attribute_Reference (Loc,
10912 Prefix => New_Occurrence_Of (Ent, Loc),
10913 Attribute_Name => Name_Last);
10915 if Present (Index) then
10916 Set_Expressions (Right, New_List (New_Copy (Index)));
10919 -- If general operand, convert Last reference to Long_Long_Integer
10921 if Present (Comp) then
10922 Right := Prepare_64 (Right);
10925 -- Check for cases to optimize
10927 -- X'Length = 0 => X'First > X'Last
10928 -- X'Length < 1 => X'First > X'Last
10929 -- X'Length < n => X'First + (n - 1) > X'Last
10931 if (Is_Zero and then Op = N_Op_Eq)
10932 or else (not Is_Zero and then Op = N_Op_Lt)
10937 Right_Opnd => Right);
10939 -- X'Length = 1 => X'First = X'Last
10940 -- X'Length = n => X'First + (n - 1) = X'Last
10942 elsif not Is_Zero and then Op = N_Op_Eq then
10946 Right_Opnd => Right);
10948 -- X'Length /= 0 => X'First <= X'Last
10949 -- X'Length > 0 => X'First <= X'Last
10951 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
10955 Right_Opnd => Right);
10957 -- X'Length /= 1 => X'First /= X'Last
10958 -- X'Length /= n => X'First + (n - 1) /= X'Last
10960 elsif not Is_Zero and then Op = N_Op_Ne then
10964 Right_Opnd => Right);
10966 -- X'Length >= 1 => X'First <= X'Last
10967 -- X'Length >= n => X'First + (n - 1) <= X'Last
10969 elsif not Is_Zero and then Op = N_Op_Ge then
10973 Right_Opnd => Right);
10975 -- X'Length > 1 => X'First < X'Last
10976 -- X'Length > n => X'First + (n = 1) < X'Last
10978 elsif not Is_Zero and then Op = N_Op_Gt then
10982 Right_Opnd => Right);
10984 -- X'Length <= 1 => X'First >= X'Last
10985 -- X'Length <= n => X'First + (n - 1) >= X'Last
10987 elsif not Is_Zero and then Op = N_Op_Le then
10991 Right_Opnd => Right);
10993 -- Should not happen at this stage
10996 raise Program_Error;
10999 -- Rewrite and finish up
11001 Rewrite (N, Result);
11002 Analyze_And_Resolve (N, Typ);
11004 end Optimize_Length_Comparison;
11006 ------------------------
11007 -- Rewrite_Comparison --
11008 ------------------------
11010 procedure Rewrite_Comparison (N : Node_Id) is
11011 Warning_Generated : Boolean := False;
11012 -- Set to True if first pass with Assume_Valid generates a warning in
11013 -- which case we skip the second pass to avoid warning overloaded.
11016 -- Set to Standard_True or Standard_False
11019 if Nkind (N) = N_Type_Conversion then
11020 Rewrite_Comparison (Expression (N));
11023 elsif Nkind (N) not in N_Op_Compare then
11027 -- Now start looking at the comparison in detail. We potentially go
11028 -- through this loop twice. The first time, Assume_Valid is set False
11029 -- in the call to Compile_Time_Compare. If this call results in a
11030 -- clear result of always True or Always False, that's decisive and
11031 -- we are done. Otherwise we repeat the processing with Assume_Valid
11032 -- set to True to generate additional warnings. We can skip that step
11033 -- if Constant_Condition_Warnings is False.
11035 for AV in False .. True loop
11037 Typ : constant Entity_Id := Etype (N);
11038 Op1 : constant Node_Id := Left_Opnd (N);
11039 Op2 : constant Node_Id := Right_Opnd (N);
11041 Res : constant Compare_Result :=
11042 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
11043 -- Res indicates if compare outcome can be compile time determined
11045 True_Result : Boolean;
11046 False_Result : Boolean;
11049 case N_Op_Compare (Nkind (N)) is
11051 True_Result := Res = EQ;
11052 False_Result := Res = LT or else Res = GT or else Res = NE;
11055 True_Result := Res in Compare_GE;
11056 False_Result := Res = LT;
11059 and then Constant_Condition_Warnings
11060 and then Comes_From_Source (Original_Node (N))
11061 and then Nkind (Original_Node (N)) = N_Op_Ge
11062 and then not In_Instance
11063 and then Is_Integer_Type (Etype (Left_Opnd (N)))
11064 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
11067 ("can never be greater than, could replace by ""'=""?", N);
11068 Warning_Generated := True;
11072 True_Result := Res = GT;
11073 False_Result := Res in Compare_LE;
11076 True_Result := Res = LT;
11077 False_Result := Res in Compare_GE;
11080 True_Result := Res in Compare_LE;
11081 False_Result := Res = GT;
11084 and then Constant_Condition_Warnings
11085 and then Comes_From_Source (Original_Node (N))
11086 and then Nkind (Original_Node (N)) = N_Op_Le
11087 and then not In_Instance
11088 and then Is_Integer_Type (Etype (Left_Opnd (N)))
11089 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
11092 ("can never be less than, could replace by ""'=""?", N);
11093 Warning_Generated := True;
11097 True_Result := Res = NE or else Res = GT or else Res = LT;
11098 False_Result := Res = EQ;
11101 -- If this is the first iteration, then we actually convert the
11102 -- comparison into True or False, if the result is certain.
11105 if True_Result or False_Result then
11106 if True_Result then
11107 Result := Standard_True;
11109 Result := Standard_False;
11114 New_Occurrence_Of (Result, Sloc (N))));
11115 Analyze_And_Resolve (N, Typ);
11116 Warn_On_Known_Condition (N);
11120 -- If this is the second iteration (AV = True), and the original
11121 -- node comes from source and we are not in an instance, then give
11122 -- a warning if we know result would be True or False. Note: we
11123 -- know Constant_Condition_Warnings is set if we get here.
11125 elsif Comes_From_Source (Original_Node (N))
11126 and then not In_Instance
11128 if True_Result then
11130 ("condition can only be False if invalid values present?",
11132 elsif False_Result then
11134 ("condition can only be True if invalid values present?",
11140 -- Skip second iteration if not warning on constant conditions or
11141 -- if the first iteration already generated a warning of some kind or
11142 -- if we are in any case assuming all values are valid (so that the
11143 -- first iteration took care of the valid case).
11145 exit when not Constant_Condition_Warnings;
11146 exit when Warning_Generated;
11147 exit when Assume_No_Invalid_Values;
11149 end Rewrite_Comparison;
11151 ----------------------------
11152 -- Safe_In_Place_Array_Op --
11153 ----------------------------
11155 function Safe_In_Place_Array_Op
11158 Op2 : Node_Id) return Boolean
11160 Target : Entity_Id;
11162 function Is_Safe_Operand (Op : Node_Id) return Boolean;
11163 -- Operand is safe if it cannot overlap part of the target of the
11164 -- operation. If the operand and the target are identical, the operand
11165 -- is safe. The operand can be empty in the case of negation.
11167 function Is_Unaliased (N : Node_Id) return Boolean;
11168 -- Check that N is a stand-alone entity
11174 function Is_Unaliased (N : Node_Id) return Boolean is
11178 and then No (Address_Clause (Entity (N)))
11179 and then No (Renamed_Object (Entity (N)));
11182 ---------------------
11183 -- Is_Safe_Operand --
11184 ---------------------
11186 function Is_Safe_Operand (Op : Node_Id) return Boolean is
11191 elsif Is_Entity_Name (Op) then
11192 return Is_Unaliased (Op);
11194 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
11195 return Is_Unaliased (Prefix (Op));
11197 elsif Nkind (Op) = N_Slice then
11199 Is_Unaliased (Prefix (Op))
11200 and then Entity (Prefix (Op)) /= Target;
11202 elsif Nkind (Op) = N_Op_Not then
11203 return Is_Safe_Operand (Right_Opnd (Op));
11208 end Is_Safe_Operand;
11210 -- Start of processing for Is_Safe_In_Place_Array_Op
11213 -- Skip this processing if the component size is different from system
11214 -- storage unit (since at least for NOT this would cause problems).
11216 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
11219 -- Cannot do in place stuff on VM_Target since cannot pass addresses
11221 elsif VM_Target /= No_VM then
11224 -- Cannot do in place stuff if non-standard Boolean representation
11226 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
11229 elsif not Is_Unaliased (Lhs) then
11233 Target := Entity (Lhs);
11234 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
11236 end Safe_In_Place_Array_Op;
11238 -----------------------
11239 -- Tagged_Membership --
11240 -----------------------
11242 -- There are two different cases to consider depending on whether the right
11243 -- operand is a class-wide type or not. If not we just compare the actual
11244 -- tag of the left expr to the target type tag:
11246 -- Left_Expr.Tag = Right_Type'Tag;
11248 -- If it is a class-wide type we use the RT function CW_Membership which is
11249 -- usually implemented by looking in the ancestor tables contained in the
11250 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
11252 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
11253 -- function IW_Membership which is usually implemented by looking in the
11254 -- table of abstract interface types plus the ancestor table contained in
11255 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
11257 procedure Tagged_Membership
11259 SCIL_Node : out Node_Id;
11260 Result : out Node_Id)
11262 Left : constant Node_Id := Left_Opnd (N);
11263 Right : constant Node_Id := Right_Opnd (N);
11264 Loc : constant Source_Ptr := Sloc (N);
11266 Full_R_Typ : Entity_Id;
11267 Left_Type : Entity_Id;
11268 New_Node : Node_Id;
11269 Right_Type : Entity_Id;
11273 SCIL_Node := Empty;
11275 -- Handle entities from the limited view
11277 Left_Type := Available_View (Etype (Left));
11278 Right_Type := Available_View (Etype (Right));
11280 -- In the case where the type is an access type, the test is applied
11281 -- using the designated types (needed in Ada 2012 for implicit anonymous
11282 -- access conversions, for AI05-0149).
11284 if Is_Access_Type (Right_Type) then
11285 Left_Type := Designated_Type (Left_Type);
11286 Right_Type := Designated_Type (Right_Type);
11289 if Is_Class_Wide_Type (Left_Type) then
11290 Left_Type := Root_Type (Left_Type);
11293 if Is_Class_Wide_Type (Right_Type) then
11294 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
11296 Full_R_Typ := Underlying_Type (Right_Type);
11300 Make_Selected_Component (Loc,
11301 Prefix => Relocate_Node (Left),
11303 New_Reference_To (First_Tag_Component (Left_Type), Loc));
11305 if Is_Class_Wide_Type (Right_Type) then
11307 -- No need to issue a run-time check if we statically know that the
11308 -- result of this membership test is always true. For example,
11309 -- considering the following declarations:
11311 -- type Iface is interface;
11312 -- type T is tagged null record;
11313 -- type DT is new T and Iface with null record;
11318 -- These membership tests are always true:
11321 -- Obj2 in T'Class;
11322 -- Obj2 in Iface'Class;
11324 -- We do not need to handle cases where the membership is illegal.
11327 -- Obj1 in DT'Class; -- Compile time error
11328 -- Obj1 in Iface'Class; -- Compile time error
11330 if not Is_Class_Wide_Type (Left_Type)
11331 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
11332 Use_Full_View => True)
11333 or else (Is_Interface (Etype (Right_Type))
11334 and then Interface_Present_In_Ancestor
11336 Iface => Etype (Right_Type))))
11338 Result := New_Reference_To (Standard_True, Loc);
11342 -- Ada 2005 (AI-251): Class-wide applied to interfaces
11344 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
11346 -- Support to: "Iface_CW_Typ in Typ'Class"
11348 or else Is_Interface (Left_Type)
11350 -- Issue error if IW_Membership operation not available in a
11351 -- configurable run time setting.
11353 if not RTE_Available (RE_IW_Membership) then
11355 ("dynamic membership test on interface types", N);
11361 Make_Function_Call (Loc,
11362 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
11363 Parameter_Associations => New_List (
11364 Make_Attribute_Reference (Loc,
11366 Attribute_Name => Name_Address),
11368 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
11371 -- Ada 95: Normal case
11374 Build_CW_Membership (Loc,
11375 Obj_Tag_Node => Obj_Tag,
11378 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc),
11380 New_Node => New_Node);
11382 -- Generate the SCIL node for this class-wide membership test.
11383 -- Done here because the previous call to Build_CW_Membership
11384 -- relocates Obj_Tag.
11386 if Generate_SCIL then
11387 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
11388 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
11389 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
11392 Result := New_Node;
11395 -- Right_Type is not a class-wide type
11398 -- No need to check the tag of the object if Right_Typ is abstract
11400 if Is_Abstract_Type (Right_Type) then
11401 Result := New_Reference_To (Standard_False, Loc);
11406 Left_Opnd => Obj_Tag,
11409 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
11412 end Tagged_Membership;
11414 ------------------------------
11415 -- Unary_Op_Validity_Checks --
11416 ------------------------------
11418 procedure Unary_Op_Validity_Checks (N : Node_Id) is
11420 if Validity_Checks_On and Validity_Check_Operands then
11421 Ensure_Valid (Right_Opnd (N));
11423 end Unary_Op_Validity_Checks;