1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, 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 Freeze; use Freeze;
46 with Inline; use Inline;
48 with Namet; use Namet;
49 with Nlists; use Nlists;
50 with Nmake; use Nmake;
52 with Par_SCO; use Par_SCO;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
57 with Sem_Aux; use Sem_Aux;
58 with Sem_Cat; use Sem_Cat;
59 with Sem_Ch3; use Sem_Ch3;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Sinfo; use Sinfo;
68 with Snames; use Snames;
69 with Stand; use Stand;
70 with SCIL_LL; use SCIL_LL;
71 with Targparm; use Targparm;
72 with Tbuild; use Tbuild;
73 with Ttypes; use Ttypes;
74 with Uintp; use Uintp;
75 with Urealp; use Urealp;
76 with Validsw; use Validsw;
78 package body Exp_Ch4 is
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 procedure Binary_Op_Validity_Checks (N : Node_Id);
85 pragma Inline (Binary_Op_Validity_Checks);
86 -- Performs validity checks for a binary operator
88 procedure Build_Boolean_Array_Proc_Call
92 -- If a boolean array assignment can be done in place, build call to
93 -- corresponding library procedure.
95 function Current_Anonymous_Master return Entity_Id;
96 -- Return the entity of the heterogeneous finalization master belonging to
97 -- the current unit (either function, package or procedure). This master
98 -- services all anonymous access-to-controlled types. If the current unit
99 -- does not have such master, create one.
101 procedure Displace_Allocator_Pointer (N : Node_Id);
102 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
103 -- Expand_Allocator_Expression. Allocating class-wide interface objects
104 -- this routine displaces the pointer to the allocated object to reference
105 -- the component referencing the corresponding secondary dispatch table.
107 procedure Expand_Allocator_Expression (N : Node_Id);
108 -- Subsidiary to Expand_N_Allocator, for the case when the expression
109 -- is a qualified expression or an aggregate.
111 procedure Expand_Array_Comparison (N : Node_Id);
112 -- This routine handles expansion of the comparison operators (N_Op_Lt,
113 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
114 -- code for these operators is similar, differing only in the details of
115 -- the actual comparison call that is made. Special processing (call a
118 function Expand_Array_Equality
123 Typ : Entity_Id) return Node_Id;
124 -- Expand an array equality into a call to a function implementing this
125 -- equality, and a call to it. Loc is the location for the generated nodes.
126 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
127 -- on which to attach bodies of local functions that are created in the
128 -- process. It is the responsibility of the caller to insert those bodies
129 -- at the right place. Nod provides the Sloc value for the generated code.
130 -- Normally the types used for the generated equality routine are taken
131 -- from Lhs and Rhs. However, in some situations of generated code, the
132 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
133 -- the type to be used for the formal parameters.
135 procedure Expand_Boolean_Operator (N : Node_Id);
136 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
137 -- case of array type arguments.
139 procedure Expand_Short_Circuit_Operator (N : Node_Id);
140 -- Common expansion processing for short-circuit boolean operators
142 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id);
143 -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is
144 -- where we allow comparison of "out of range" values.
146 function Expand_Composite_Equality
151 Bodies : List_Id) return Node_Id;
152 -- Local recursive function used to expand equality for nested composite
153 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
154 -- to attach bodies of local functions that are created in the process. It
155 -- is the responsibility of the caller to insert those bodies at the right
156 -- place. Nod provides the Sloc value for generated code. Lhs and Rhs are
157 -- the left and right sides for the comparison, and Typ is the type of the
158 -- objects to compare.
160 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
161 -- Routine to expand concatenation of a sequence of two or more operands
162 -- (in the list Operands) and replace node Cnode with the result of the
163 -- concatenation. The operands can be of any appropriate type, and can
164 -- include both arrays and singleton elements.
166 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id);
167 -- N is an N_In membership test mode, with the overflow check mode set to
168 -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed
169 -- integer type. This is a case where top level processing is required to
170 -- handle overflow checks in subtrees.
172 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
173 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
174 -- fixed. We do not have such a type at runtime, so the purpose of this
175 -- routine is to find the real type by looking up the tree. We also
176 -- determine if the operation must be rounded.
178 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
179 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
180 -- discriminants if it has a constrained nominal type, unless the object
181 -- is a component of an enclosing Unchecked_Union object that is subject
182 -- to a per-object constraint and the enclosing object lacks inferable
185 -- An expression of an Unchecked_Union type has inferable discriminants
186 -- if it is either a name of an object with inferable discriminants or a
187 -- qualified expression whose subtype mark denotes a constrained subtype.
189 procedure Insert_Dereference_Action (N : Node_Id);
190 -- N is an expression whose type is an access. When the type of the
191 -- associated storage pool is derived from Checked_Pool, generate a
192 -- call to the 'Dereference' primitive operation.
194 function Make_Array_Comparison_Op
196 Nod : Node_Id) return Node_Id;
197 -- Comparisons between arrays are expanded in line. This function produces
198 -- the body of the implementation of (a > b), where a and b are one-
199 -- dimensional arrays of some discrete type. The original node is then
200 -- expanded into the appropriate call to this function. Nod provides the
201 -- Sloc value for the generated code.
203 function Make_Boolean_Array_Op
205 N : Node_Id) return Node_Id;
206 -- Boolean operations on boolean arrays are expanded in line. This function
207 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
208 -- b). It is used only the normal case and not the packed case. The type
209 -- involved, Typ, is the Boolean array type, and the logical operations in
210 -- the body are simple boolean operations. Note that Typ is always a
211 -- constrained type (the caller has ensured this by using
212 -- Convert_To_Actual_Subtype if necessary).
214 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean;
215 -- For signed arithmetic operations when the current overflow mode is
216 -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks
217 -- as the first thing we do. We then return. We count on the recursive
218 -- apparatus for overflow checks to call us back with an equivalent
219 -- operation that is in CHECKED mode, avoiding a recursive entry into this
220 -- routine, and that is when we will proceed with the expansion of the
221 -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do
222 -- these optimizations without first making this check, since there may be
223 -- operands further down the tree that are relying on the recursive calls
224 -- triggered by the top level nodes to properly process overflow checking
225 -- and remaining expansion on these nodes. Note that this call back may be
226 -- skipped if the operation is done in Bignum mode but that's fine, since
227 -- the Bignum call takes care of everything.
229 procedure Optimize_Length_Comparison (N : Node_Id);
230 -- Given an expression, if it is of the form X'Length op N (or the other
231 -- way round), where N is known at compile time to be 0 or 1, and X is a
232 -- simple entity, and op is a comparison operator, optimizes it into a
233 -- comparison of First and Last.
235 procedure Process_Transient_Object
238 -- Subsidiary routine to the expansion of expression_with_actions and if
239 -- expressions. Generate all the necessary code to finalize a transient
240 -- controlled object when the enclosing context is elaborated or evaluated.
241 -- Decl denotes the declaration of the transient controlled object which is
242 -- usually the result of a controlled function call. Rel_Node denotes the
243 -- context, either an expression_with_actions or an if expression.
245 procedure Rewrite_Comparison (N : Node_Id);
246 -- If N is the node for a comparison whose outcome can be determined at
247 -- compile time, then the node N can be rewritten with True or False. If
248 -- the outcome cannot be determined at compile time, the call has no
249 -- effect. If N is a type conversion, then this processing is applied to
250 -- its expression. If N is neither comparison nor a type conversion, the
251 -- call has no effect.
253 procedure Tagged_Membership
255 SCIL_Node : out Node_Id;
256 Result : out Node_Id);
257 -- Construct the expression corresponding to the tagged membership test.
258 -- Deals with a second operand being (or not) a class-wide type.
260 function Safe_In_Place_Array_Op
263 Op2 : Node_Id) return Boolean;
264 -- In the context of an assignment, where the right-hand side is a boolean
265 -- operation on arrays, check whether operation can be performed in place.
267 procedure Unary_Op_Validity_Checks (N : Node_Id);
268 pragma Inline (Unary_Op_Validity_Checks);
269 -- Performs validity checks for a unary operator
271 -------------------------------
272 -- Binary_Op_Validity_Checks --
273 -------------------------------
275 procedure Binary_Op_Validity_Checks (N : Node_Id) is
277 if Validity_Checks_On and Validity_Check_Operands then
278 Ensure_Valid (Left_Opnd (N));
279 Ensure_Valid (Right_Opnd (N));
281 end Binary_Op_Validity_Checks;
283 ------------------------------------
284 -- Build_Boolean_Array_Proc_Call --
285 ------------------------------------
287 procedure Build_Boolean_Array_Proc_Call
292 Loc : constant Source_Ptr := Sloc (N);
293 Kind : constant Node_Kind := Nkind (Expression (N));
294 Target : constant Node_Id :=
295 Make_Attribute_Reference (Loc,
297 Attribute_Name => Name_Address);
299 Arg1 : Node_Id := Op1;
300 Arg2 : Node_Id := Op2;
302 Proc_Name : Entity_Id;
305 if Kind = N_Op_Not then
306 if Nkind (Op1) in N_Binary_Op then
308 -- Use negated version of the binary operators
310 if Nkind (Op1) = N_Op_And then
311 Proc_Name := RTE (RE_Vector_Nand);
313 elsif Nkind (Op1) = N_Op_Or then
314 Proc_Name := RTE (RE_Vector_Nor);
316 else pragma Assert (Nkind (Op1) = N_Op_Xor);
317 Proc_Name := RTE (RE_Vector_Xor);
321 Make_Procedure_Call_Statement (Loc,
322 Name => New_Occurrence_Of (Proc_Name, Loc),
324 Parameter_Associations => New_List (
326 Make_Attribute_Reference (Loc,
327 Prefix => Left_Opnd (Op1),
328 Attribute_Name => Name_Address),
330 Make_Attribute_Reference (Loc,
331 Prefix => Right_Opnd (Op1),
332 Attribute_Name => Name_Address),
334 Make_Attribute_Reference (Loc,
335 Prefix => Left_Opnd (Op1),
336 Attribute_Name => Name_Length)));
339 Proc_Name := RTE (RE_Vector_Not);
342 Make_Procedure_Call_Statement (Loc,
343 Name => New_Occurrence_Of (Proc_Name, Loc),
344 Parameter_Associations => New_List (
347 Make_Attribute_Reference (Loc,
349 Attribute_Name => Name_Address),
351 Make_Attribute_Reference (Loc,
353 Attribute_Name => Name_Length)));
357 -- We use the following equivalences:
359 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
360 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
361 -- (not X) xor (not Y) = X xor Y
362 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
364 if Nkind (Op1) = N_Op_Not then
365 Arg1 := Right_Opnd (Op1);
366 Arg2 := Right_Opnd (Op2);
368 if Kind = N_Op_And then
369 Proc_Name := RTE (RE_Vector_Nor);
370 elsif Kind = N_Op_Or then
371 Proc_Name := RTE (RE_Vector_Nand);
373 Proc_Name := RTE (RE_Vector_Xor);
377 if Kind = N_Op_And then
378 Proc_Name := RTE (RE_Vector_And);
379 elsif Kind = N_Op_Or then
380 Proc_Name := RTE (RE_Vector_Or);
381 elsif Nkind (Op2) = N_Op_Not then
382 Proc_Name := RTE (RE_Vector_Nxor);
383 Arg2 := Right_Opnd (Op2);
385 Proc_Name := RTE (RE_Vector_Xor);
390 Make_Procedure_Call_Statement (Loc,
391 Name => New_Occurrence_Of (Proc_Name, Loc),
392 Parameter_Associations => New_List (
394 Make_Attribute_Reference (Loc,
396 Attribute_Name => Name_Address),
397 Make_Attribute_Reference (Loc,
399 Attribute_Name => Name_Address),
400 Make_Attribute_Reference (Loc,
402 Attribute_Name => Name_Length)));
405 Rewrite (N, Call_Node);
409 when RE_Not_Available =>
411 end Build_Boolean_Array_Proc_Call;
413 ------------------------------
414 -- Current_Anonymous_Master --
415 ------------------------------
417 function Current_Anonymous_Master return Entity_Id is
418 function Create_Anonymous_Master
419 (Unit_Id : Entity_Id;
420 Unit_Decl : Node_Id) return Entity_Id;
421 -- Create a new anonymous master for a compilation unit denoted by its
422 -- entity Unit_Id and declaration Unit_Decl. The declaration of the new
423 -- master along with any specialized initialization is inserted at the
424 -- top of the unit's declarations (see body for special cases). Return
425 -- the entity of the anonymous master.
427 -----------------------------
428 -- Create_Anonymous_Master --
429 -----------------------------
431 function Create_Anonymous_Master
432 (Unit_Id : Entity_Id;
433 Unit_Decl : Node_Id) return Entity_Id
435 Insert_Nod : Node_Id := Empty;
436 -- The point of insertion into the declarative list of the unit. All
437 -- nodes are inserted before Insert_Nod.
439 procedure Insert_And_Analyze (Decls : List_Id; N : Node_Id);
440 -- Insert arbitrary node N in declarative list Decls and analyze it
442 ------------------------
443 -- Insert_And_Analyze --
444 ------------------------
446 procedure Insert_And_Analyze (Decls : List_Id; N : Node_Id) is
448 -- The declarative list is already populated, the nodes are
449 -- inserted at the top of the list, preserving their order.
451 if Present (Insert_Nod) then
452 Insert_Before (Insert_Nod, N);
454 -- Otherwise append to the declarations to preserve order
457 Append_To (Decls, N);
461 end Insert_And_Analyze;
465 Loc : constant Source_Ptr := Sloc (Unit_Id);
466 Spec_Id : constant Entity_Id := Unique_Defining_Entity (Unit_Decl);
472 -- Start of processing for Create_Anonymous_Master
475 -- Find the declarative list of the unit
477 if Nkind (Unit_Decl) = N_Package_Declaration then
478 Unit_Spec := Specification (Unit_Decl);
479 Decls := Visible_Declarations (Unit_Spec);
482 Decls := New_List (Make_Null_Statement (Loc));
483 Set_Visible_Declarations (Unit_Spec, Decls);
486 -- Package or subprogram body
488 -- ??? A subprogram declaration that acts as a compilation unit may
489 -- contain a formal parameter of an anonymous access-to-controlled
490 -- type initialized by an allocator.
492 -- procedure Comp_Unit_Proc (Param : access Ctrl := new Ctrl);
494 -- There is no suitable place to create the anonymous master as the
495 -- subprogram is not in a declarative list.
498 Decls := Declarations (Unit_Decl);
501 Decls := New_List (Make_Null_Statement (Loc));
502 Set_Declarations (Unit_Decl, Decls);
506 -- The anonymous master and all initialization actions are inserted
507 -- before the first declaration (if any).
509 Insert_Nod := First (Decls);
511 -- Since the anonymous master and all its initialization actions are
512 -- inserted at top level, use the scope of the unit when analyzing.
514 Push_Scope (Spec_Id);
516 -- Step 1: Anonymous master creation
518 -- Use a unique prefix in case the same unit requires two anonymous
519 -- masters, one for the spec (S) and one for the body (B).
521 if Ekind_In (Unit_Id, E_Function, E_Package, E_Procedure) then
528 Make_Defining_Identifier (Loc,
530 (Related_Id => Chars (Unit_Id),
534 Set_Anonymous_Master (Unit_Id, FM_Id);
537 -- <FM_Id> : Finalization_Master;
539 Insert_And_Analyze (Decls,
540 Make_Object_Declaration (Loc,
541 Defining_Identifier => FM_Id,
543 New_Occurrence_Of (RTE (RE_Finalization_Master), Loc)));
545 -- Step 2: Initialization actions
549 -- (<FM_Id>, Global_Pool_Object'Unrestricted_Access);
551 Insert_And_Analyze (Decls,
552 Make_Procedure_Call_Statement (Loc,
554 New_Occurrence_Of (RTE (RE_Set_Base_Pool), Loc),
555 Parameter_Associations => New_List (
556 New_Occurrence_Of (FM_Id, Loc),
557 Make_Attribute_Reference (Loc,
559 New_Occurrence_Of (RTE (RE_Global_Pool_Object), Loc),
560 Attribute_Name => Name_Unrestricted_Access))));
563 -- Set_Is_Heterogeneous (<FM_Id>);
565 Insert_And_Analyze (Decls,
566 Make_Procedure_Call_Statement (Loc,
568 New_Occurrence_Of (RTE (RE_Set_Is_Heterogeneous), Loc),
569 Parameter_Associations => New_List (
570 New_Occurrence_Of (FM_Id, Loc))));
574 end Create_Anonymous_Master;
576 -- Local declarations
581 -- Start of processing for Current_Anonymous_Master
584 Unit_Decl := Unit (Cunit (Current_Sem_Unit));
585 Unit_Id := Defining_Entity (Unit_Decl);
587 -- The compilation unit is a package instantiation. In this case the
588 -- anonymous master is associated with the package spec as both the
589 -- spec and body appear at the same level.
591 if Nkind (Unit_Decl) = N_Package_Body
592 and then Nkind (Original_Node (Unit_Decl)) = N_Package_Instantiation
594 Unit_Id := Corresponding_Spec (Unit_Decl);
595 Unit_Decl := Unit_Declaration_Node (Unit_Id);
598 if Present (Anonymous_Master (Unit_Id)) then
599 return Anonymous_Master (Unit_Id);
601 -- Create a new anonymous master when allocating an object of anonymous
602 -- access-to-controlled type for the first time.
605 return Create_Anonymous_Master (Unit_Id, Unit_Decl);
607 end Current_Anonymous_Master;
609 --------------------------------
610 -- Displace_Allocator_Pointer --
611 --------------------------------
613 procedure Displace_Allocator_Pointer (N : Node_Id) is
614 Loc : constant Source_Ptr := Sloc (N);
615 Orig_Node : constant Node_Id := Original_Node (N);
621 -- Do nothing in case of VM targets: the virtual machine will handle
622 -- interfaces directly.
624 if not Tagged_Type_Expansion then
628 pragma Assert (Nkind (N) = N_Identifier
629 and then Nkind (Orig_Node) = N_Allocator);
631 PtrT := Etype (Orig_Node);
632 Dtyp := Available_View (Designated_Type (PtrT));
633 Etyp := Etype (Expression (Orig_Node));
635 if Is_Class_Wide_Type (Dtyp) and then Is_Interface (Dtyp) then
637 -- If the type of the allocator expression is not an interface type
638 -- we can generate code to reference the record component containing
639 -- the pointer to the secondary dispatch table.
641 if not Is_Interface (Etyp) then
643 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
646 -- 1) Get access to the allocated object
649 Make_Explicit_Dereference (Loc, Relocate_Node (N)));
653 -- 2) Add the conversion to displace the pointer to reference
654 -- the secondary dispatch table.
656 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
657 Analyze_And_Resolve (N, Dtyp);
659 -- 3) The 'access to the secondary dispatch table will be used
660 -- as the value returned by the allocator.
663 Make_Attribute_Reference (Loc,
664 Prefix => Relocate_Node (N),
665 Attribute_Name => Name_Access));
666 Set_Etype (N, Saved_Typ);
670 -- If the type of the allocator expression is an interface type we
671 -- generate a run-time call to displace "this" to reference the
672 -- component containing the pointer to the secondary dispatch table
673 -- or else raise Constraint_Error if the actual object does not
674 -- implement the target interface. This case corresponds to the
675 -- following example:
677 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
679 -- return new Iface_2'Class'(Obj);
684 Unchecked_Convert_To (PtrT,
685 Make_Function_Call (Loc,
686 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
687 Parameter_Associations => New_List (
688 Unchecked_Convert_To (RTE (RE_Address),
694 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
696 Analyze_And_Resolve (N, PtrT);
699 end Displace_Allocator_Pointer;
701 ---------------------------------
702 -- Expand_Allocator_Expression --
703 ---------------------------------
705 procedure Expand_Allocator_Expression (N : Node_Id) is
706 Loc : constant Source_Ptr := Sloc (N);
707 Exp : constant Node_Id := Expression (Expression (N));
708 PtrT : constant Entity_Id := Etype (N);
709 DesigT : constant Entity_Id := Designated_Type (PtrT);
711 procedure Apply_Accessibility_Check
713 Built_In_Place : Boolean := False);
714 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
715 -- type, generate an accessibility check to verify that the level of the
716 -- type of the created object is not deeper than the level of the access
717 -- type. If the type of the qualified expression is class-wide, then
718 -- always generate the check (except in the case where it is known to be
719 -- unnecessary, see comment below). Otherwise, only generate the check
720 -- if the level of the qualified expression type is statically deeper
721 -- than the access type.
723 -- Although the static accessibility will generally have been performed
724 -- as a legality check, it won't have been done in cases where the
725 -- allocator appears in generic body, so a run-time check is needed in
726 -- general. One special case is when the access type is declared in the
727 -- same scope as the class-wide allocator, in which case the check can
728 -- never fail, so it need not be generated.
730 -- As an open issue, there seem to be cases where the static level
731 -- associated with the class-wide object's underlying type is not
732 -- sufficient to perform the proper accessibility check, such as for
733 -- allocators in nested subprograms or accept statements initialized by
734 -- class-wide formals when the actual originates outside at a deeper
735 -- static level. The nested subprogram case might require passing
736 -- accessibility levels along with class-wide parameters, and the task
737 -- case seems to be an actual gap in the language rules that needs to
738 -- be fixed by the ARG. ???
740 -------------------------------
741 -- Apply_Accessibility_Check --
742 -------------------------------
744 procedure Apply_Accessibility_Check
746 Built_In_Place : Boolean := False)
748 Pool_Id : constant Entity_Id := Associated_Storage_Pool (PtrT);
756 if Ada_Version >= Ada_2005
757 and then Is_Class_Wide_Type (DesigT)
758 and then Tagged_Type_Expansion
759 and then not Scope_Suppress.Suppress (Accessibility_Check)
761 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
763 (Is_Class_Wide_Type (Etype (Exp))
764 and then Scope (PtrT) /= Current_Scope))
766 -- If the allocator was built in place, Ref is already a reference
767 -- to the access object initialized to the result of the allocator
768 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
769 -- Remove_Side_Effects for cases where the build-in-place call may
770 -- still be the prefix of the reference (to avoid generating
771 -- duplicate calls). Otherwise, it is the entity associated with
772 -- the object containing the address of the allocated object.
774 if Built_In_Place then
775 Remove_Side_Effects (Ref);
776 Obj_Ref := New_Copy_Tree (Ref);
778 Obj_Ref := New_Occurrence_Of (Ref, Loc);
781 -- For access to interface types we must generate code to displace
782 -- the pointer to the base of the object since the subsequent code
783 -- references components located in the TSD of the object (which
784 -- is associated with the primary dispatch table --see a-tags.ads)
785 -- and also generates code invoking Free, which requires also a
786 -- reference to the base of the unallocated object.
788 if Is_Interface (DesigT) and then Tagged_Type_Expansion then
790 Unchecked_Convert_To (Etype (Obj_Ref),
791 Make_Function_Call (Loc,
793 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
794 Parameter_Associations => New_List (
795 Unchecked_Convert_To (RTE (RE_Address),
796 New_Copy_Tree (Obj_Ref)))));
799 -- Step 1: Create the object clean up code
803 -- Deallocate the object if the accessibility check fails. This
804 -- is done only on targets or profiles that support deallocation.
808 if RTE_Available (RE_Free) then
809 Free_Stmt := Make_Free_Statement (Loc, New_Copy_Tree (Obj_Ref));
810 Set_Storage_Pool (Free_Stmt, Pool_Id);
812 Append_To (Stmts, Free_Stmt);
814 -- The target or profile cannot deallocate objects
820 -- Finalize the object if applicable. Generate:
822 -- [Deep_]Finalize (Obj_Ref.all);
824 if Needs_Finalization (DesigT) then
828 Make_Explicit_Dereference (Loc, New_Copy (Obj_Ref)),
831 -- When the target or profile supports deallocation, wrap the
832 -- finalization call in a block to ensure proper deallocation
833 -- even if finalization fails. Generate:
843 if Present (Free_Stmt) then
845 Make_Block_Statement (Loc,
846 Handled_Statement_Sequence =>
847 Make_Handled_Sequence_Of_Statements (Loc,
848 Statements => New_List (Fin_Call),
850 Exception_Handlers => New_List (
851 Make_Exception_Handler (Loc,
852 Exception_Choices => New_List (
853 Make_Others_Choice (Loc)),
854 Statements => New_List (
855 New_Copy_Tree (Free_Stmt),
856 Make_Raise_Statement (Loc))))));
859 Prepend_To (Stmts, Fin_Call);
862 -- Signal the accessibility failure through a Program_Error
865 Make_Raise_Program_Error (Loc,
866 Condition => New_Occurrence_Of (Standard_True, Loc),
867 Reason => PE_Accessibility_Check_Failed));
869 -- Step 2: Create the accessibility comparison
875 Make_Attribute_Reference (Loc,
877 Attribute_Name => Name_Tag);
879 -- For tagged types, determine the accessibility level by looking
880 -- at the type specific data of the dispatch table. Generate:
882 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
884 if Tagged_Type_Expansion then
885 Cond := Build_Get_Access_Level (Loc, Obj_Ref);
887 -- Use a runtime call to determine the accessibility level when
888 -- compiling on virtual machine targets. Generate:
890 -- Get_Access_Level (Ref'Tag)
894 Make_Function_Call (Loc,
896 New_Occurrence_Of (RTE (RE_Get_Access_Level), Loc),
897 Parameter_Associations => New_List (Obj_Ref));
904 Make_Integer_Literal (Loc, Type_Access_Level (PtrT)));
906 -- Due to the complexity and side effects of the check, utilize an
907 -- if statement instead of the regular Program_Error circuitry.
910 Make_Implicit_If_Statement (N,
912 Then_Statements => Stmts));
914 end Apply_Accessibility_Check;
918 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
919 Indic : constant Node_Id := Subtype_Mark (Expression (N));
920 T : constant Entity_Id := Entity (Indic);
922 Tag_Assign : Node_Id;
926 TagT : Entity_Id := Empty;
927 -- Type used as source for tag assignment
929 TagR : Node_Id := Empty;
930 -- Target reference for tag assignment
932 -- Start of processing for Expand_Allocator_Expression
935 -- Handle call to C++ constructor
937 if Is_CPP_Constructor_Call (Exp) then
938 Make_CPP_Constructor_Call_In_Allocator
940 Function_Call => Exp);
944 -- In the case of an Ada 2012 allocator whose initial value comes from a
945 -- function call, pass "the accessibility level determined by the point
946 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
947 -- Expand_Call but it couldn't be done there (because the Etype of the
948 -- allocator wasn't set then) so we generate the parameter here. See
949 -- the Boolean variable Defer in (a block within) Expand_Call.
951 if Ada_Version >= Ada_2012 and then Nkind (Exp) = N_Function_Call then
956 if Nkind (Name (Exp)) = N_Explicit_Dereference then
957 Subp := Designated_Type (Etype (Prefix (Name (Exp))));
959 Subp := Entity (Name (Exp));
962 Subp := Ultimate_Alias (Subp);
964 if Present (Extra_Accessibility_Of_Result (Subp)) then
965 Add_Extra_Actual_To_Call
966 (Subprogram_Call => Exp,
967 Extra_Formal => Extra_Accessibility_Of_Result (Subp),
968 Extra_Actual => Dynamic_Accessibility_Level (PtrT));
973 -- Case of tagged type or type requiring finalization
975 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
977 -- Ada 2005 (AI-318-02): If the initialization expression is a call
978 -- to a build-in-place function, then access to the allocated object
979 -- must be passed to the function. Currently we limit such functions
980 -- to those with constrained limited result subtypes, but eventually
981 -- we plan to expand the allowed forms of functions that are treated
982 -- as build-in-place.
984 if Ada_Version >= Ada_2005
985 and then Is_Build_In_Place_Function_Call (Exp)
987 Make_Build_In_Place_Call_In_Allocator (N, Exp);
988 Apply_Accessibility_Check (N, Built_In_Place => True);
992 -- Actions inserted before:
993 -- Temp : constant ptr_T := new T'(Expression);
994 -- Temp._tag = T'tag; -- when not class-wide
995 -- [Deep_]Adjust (Temp.all);
997 -- We analyze by hand the new internal allocator to avoid any
998 -- recursion and inappropriate call to Initialize.
1000 -- We don't want to remove side effects when the expression must be
1001 -- built in place. In the case of a build-in-place function call,
1002 -- that could lead to a duplication of the call, which was already
1003 -- substituted for the allocator.
1005 if not Aggr_In_Place then
1006 Remove_Side_Effects (Exp);
1009 Temp := Make_Temporary (Loc, 'P', N);
1011 -- For a class wide allocation generate the following code:
1013 -- type Equiv_Record is record ... end record;
1014 -- implicit subtype CW is <Class_Wide_Subytpe>;
1015 -- temp : PtrT := new CW'(CW!(expr));
1017 if Is_Class_Wide_Type (T) then
1018 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
1020 -- Ada 2005 (AI-251): If the expression is a class-wide interface
1021 -- object we generate code to move up "this" to reference the
1022 -- base of the object before allocating the new object.
1024 -- Note that Exp'Address is recursively expanded into a call
1025 -- to Base_Address (Exp.Tag)
1027 if Is_Class_Wide_Type (Etype (Exp))
1028 and then Is_Interface (Etype (Exp))
1029 and then Tagged_Type_Expansion
1033 Unchecked_Convert_To (Entity (Indic),
1034 Make_Explicit_Dereference (Loc,
1035 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
1036 Make_Attribute_Reference (Loc,
1038 Attribute_Name => Name_Address)))));
1042 Unchecked_Convert_To (Entity (Indic), Exp));
1045 Analyze_And_Resolve (Expression (N), Entity (Indic));
1048 -- Processing for allocators returning non-interface types
1050 if not Is_Interface (Directly_Designated_Type (PtrT)) then
1051 if Aggr_In_Place then
1053 Make_Object_Declaration (Loc,
1054 Defining_Identifier => Temp,
1055 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1057 Make_Allocator (Loc,
1059 New_Occurrence_Of (Etype (Exp), Loc)));
1061 -- Copy the Comes_From_Source flag for the allocator we just
1062 -- built, since logically this allocator is a replacement of
1063 -- the original allocator node. This is for proper handling of
1064 -- restriction No_Implicit_Heap_Allocations.
1066 Set_Comes_From_Source
1067 (Expression (Temp_Decl), Comes_From_Source (N));
1069 Set_No_Initialization (Expression (Temp_Decl));
1070 Insert_Action (N, Temp_Decl);
1072 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1073 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1076 Node := Relocate_Node (N);
1077 Set_Analyzed (Node);
1080 Make_Object_Declaration (Loc,
1081 Defining_Identifier => Temp,
1082 Constant_Present => True,
1083 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1084 Expression => Node);
1086 Insert_Action (N, Temp_Decl);
1087 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1090 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
1091 -- interface type. In this case we use the type of the qualified
1092 -- expression to allocate the object.
1096 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
1101 Make_Full_Type_Declaration (Loc,
1102 Defining_Identifier => Def_Id,
1104 Make_Access_To_Object_Definition (Loc,
1105 All_Present => True,
1106 Null_Exclusion_Present => False,
1108 Is_Access_Constant (Etype (N)),
1109 Subtype_Indication =>
1110 New_Occurrence_Of (Etype (Exp), Loc)));
1112 Insert_Action (N, New_Decl);
1114 -- Inherit the allocation-related attributes from the original
1117 Set_Finalization_Master
1118 (Def_Id, Finalization_Master (PtrT));
1120 Set_Associated_Storage_Pool
1121 (Def_Id, Associated_Storage_Pool (PtrT));
1123 -- Declare the object using the previous type declaration
1125 if Aggr_In_Place then
1127 Make_Object_Declaration (Loc,
1128 Defining_Identifier => Temp,
1129 Object_Definition => New_Occurrence_Of (Def_Id, Loc),
1131 Make_Allocator (Loc,
1132 New_Occurrence_Of (Etype (Exp), Loc)));
1134 -- Copy the Comes_From_Source flag for the allocator we just
1135 -- built, since logically this allocator is a replacement of
1136 -- the original allocator node. This is for proper handling
1137 -- of restriction No_Implicit_Heap_Allocations.
1139 Set_Comes_From_Source
1140 (Expression (Temp_Decl), Comes_From_Source (N));
1142 Set_No_Initialization (Expression (Temp_Decl));
1143 Insert_Action (N, Temp_Decl);
1145 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1146 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1149 Node := Relocate_Node (N);
1150 Set_Analyzed (Node);
1153 Make_Object_Declaration (Loc,
1154 Defining_Identifier => Temp,
1155 Constant_Present => True,
1156 Object_Definition => New_Occurrence_Of (Def_Id, Loc),
1157 Expression => Node);
1159 Insert_Action (N, Temp_Decl);
1160 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1163 -- Generate an additional object containing the address of the
1164 -- returned object. The type of this second object declaration
1165 -- is the correct type required for the common processing that
1166 -- is still performed by this subprogram. The displacement of
1167 -- this pointer to reference the component associated with the
1168 -- interface type will be done at the end of common processing.
1171 Make_Object_Declaration (Loc,
1172 Defining_Identifier => Make_Temporary (Loc, 'P'),
1173 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1175 Unchecked_Convert_To (PtrT,
1176 New_Occurrence_Of (Temp, Loc)));
1178 Insert_Action (N, New_Decl);
1180 Temp_Decl := New_Decl;
1181 Temp := Defining_Identifier (New_Decl);
1185 Apply_Accessibility_Check (Temp);
1187 -- Generate the tag assignment
1189 -- Suppress the tag assignment for VM targets because VM tags are
1190 -- represented implicitly in objects.
1192 if not Tagged_Type_Expansion then
1195 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1196 -- interface objects because in this case the tag does not change.
1198 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
1199 pragma Assert (Is_Class_Wide_Type
1200 (Directly_Designated_Type (Etype (N))));
1203 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
1205 TagR := New_Occurrence_Of (Temp, Loc);
1207 elsif Is_Private_Type (T)
1208 and then Is_Tagged_Type (Underlying_Type (T))
1210 TagT := Underlying_Type (T);
1212 Unchecked_Convert_To (Underlying_Type (T),
1213 Make_Explicit_Dereference (Loc,
1214 Prefix => New_Occurrence_Of (Temp, Loc)));
1217 if Present (TagT) then
1219 Full_T : constant Entity_Id := Underlying_Type (TagT);
1223 Make_Assignment_Statement (Loc,
1225 Make_Selected_Component (Loc,
1229 (First_Tag_Component (Full_T), Loc)),
1232 Unchecked_Convert_To (RTE (RE_Tag),
1235 (First_Elmt (Access_Disp_Table (Full_T))), Loc)));
1238 -- The previous assignment has to be done in any case
1240 Set_Assignment_OK (Name (Tag_Assign));
1241 Insert_Action (N, Tag_Assign);
1244 if Needs_Finalization (DesigT) and then Needs_Finalization (T) then
1246 -- Generate an Adjust call if the object will be moved. In Ada
1247 -- 2005, the object may be inherently limited, in which case
1248 -- there is no Adjust procedure, and the object is built in
1249 -- place. In Ada 95, the object can be limited but not
1250 -- inherently limited if this allocator came from a return
1251 -- statement (we're allocating the result on the secondary
1252 -- stack). In that case, the object will be moved, so we _do_
1255 if not Aggr_In_Place
1256 and then not Is_Limited_View (T)
1260 -- An unchecked conversion is needed in the classwide case
1261 -- because the designated type can be an ancestor of the
1262 -- subtype mark of the allocator.
1266 Unchecked_Convert_To (T,
1267 Make_Explicit_Dereference (Loc,
1268 Prefix => New_Occurrence_Of (Temp, Loc))),
1273 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1274 Analyze_And_Resolve (N, PtrT);
1276 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1277 -- component containing the secondary dispatch table of the interface
1280 if Is_Interface (Directly_Designated_Type (PtrT)) then
1281 Displace_Allocator_Pointer (N);
1284 elsif Aggr_In_Place then
1285 Temp := Make_Temporary (Loc, 'P', N);
1287 Make_Object_Declaration (Loc,
1288 Defining_Identifier => Temp,
1289 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1291 Make_Allocator (Loc,
1292 Expression => New_Occurrence_Of (Etype (Exp), Loc)));
1294 -- Copy the Comes_From_Source flag for the allocator we just built,
1295 -- since logically this allocator is a replacement of the original
1296 -- allocator node. This is for proper handling of restriction
1297 -- No_Implicit_Heap_Allocations.
1299 Set_Comes_From_Source
1300 (Expression (Temp_Decl), Comes_From_Source (N));
1302 Set_No_Initialization (Expression (Temp_Decl));
1303 Insert_Action (N, Temp_Decl);
1305 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1306 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1308 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1309 Analyze_And_Resolve (N, PtrT);
1311 elsif Is_Access_Type (T) and then Can_Never_Be_Null (T) then
1312 Install_Null_Excluding_Check (Exp);
1314 elsif Is_Access_Type (DesigT)
1315 and then Nkind (Exp) = N_Allocator
1316 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1318 -- Apply constraint to designated subtype indication
1320 Apply_Constraint_Check
1321 (Expression (Exp), Designated_Type (DesigT), No_Sliding => True);
1323 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1325 -- Propagate constraint_error to enclosing allocator
1327 Rewrite (Exp, New_Copy (Expression (Exp)));
1331 Build_Allocate_Deallocate_Proc (N, True);
1334 -- type A is access T1;
1335 -- X : A := new T2'(...);
1336 -- T1 and T2 can be different subtypes, and we might need to check
1337 -- both constraints. First check against the type of the qualified
1340 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1342 if Do_Range_Check (Exp) then
1343 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1346 -- A check is also needed in cases where the designated subtype is
1347 -- constrained and differs from the subtype given in the qualified
1348 -- expression. Note that the check on the qualified expression does
1349 -- not allow sliding, but this check does (a relaxation from Ada 83).
1351 if Is_Constrained (DesigT)
1352 and then not Subtypes_Statically_Match (T, DesigT)
1354 Apply_Constraint_Check
1355 (Exp, DesigT, No_Sliding => False);
1357 if Do_Range_Check (Exp) then
1358 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1362 -- For an access to unconstrained packed array, GIGI needs to see an
1363 -- expression with a constrained subtype in order to compute the
1364 -- proper size for the allocator.
1366 if Is_Array_Type (T)
1367 and then not Is_Constrained (T)
1368 and then Is_Packed (T)
1371 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1372 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1375 Make_Subtype_Declaration (Loc,
1376 Defining_Identifier => ConstrT,
1377 Subtype_Indication =>
1378 Make_Subtype_From_Expr (Internal_Exp, T)));
1379 Freeze_Itype (ConstrT, Exp);
1380 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1384 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1385 -- to a build-in-place function, then access to the allocated object
1386 -- must be passed to the function. Currently we limit such functions
1387 -- to those with constrained limited result subtypes, but eventually
1388 -- we plan to expand the allowed forms of functions that are treated
1389 -- as build-in-place.
1391 if Ada_Version >= Ada_2005
1392 and then Is_Build_In_Place_Function_Call (Exp)
1394 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1399 when RE_Not_Available =>
1401 end Expand_Allocator_Expression;
1403 -----------------------------
1404 -- Expand_Array_Comparison --
1405 -----------------------------
1407 -- Expansion is only required in the case of array types. For the unpacked
1408 -- case, an appropriate runtime routine is called. For packed cases, and
1409 -- also in some other cases where a runtime routine cannot be called, the
1410 -- form of the expansion is:
1412 -- [body for greater_nn; boolean_expression]
1414 -- The body is built by Make_Array_Comparison_Op, and the form of the
1415 -- Boolean expression depends on the operator involved.
1417 procedure Expand_Array_Comparison (N : Node_Id) is
1418 Loc : constant Source_Ptr := Sloc (N);
1419 Op1 : Node_Id := Left_Opnd (N);
1420 Op2 : Node_Id := Right_Opnd (N);
1421 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1422 Ctyp : constant Entity_Id := Component_Type (Typ1);
1425 Func_Body : Node_Id;
1426 Func_Name : Entity_Id;
1430 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1431 -- True for byte addressable target
1433 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1434 -- Returns True if the length of the given operand is known to be less
1435 -- than 4. Returns False if this length is known to be four or greater
1436 -- or is not known at compile time.
1438 ------------------------
1439 -- Length_Less_Than_4 --
1440 ------------------------
1442 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1443 Otyp : constant Entity_Id := Etype (Opnd);
1446 if Ekind (Otyp) = E_String_Literal_Subtype then
1447 return String_Literal_Length (Otyp) < 4;
1451 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1452 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1453 Hi : constant Node_Id := Type_High_Bound (Ityp);
1458 if Compile_Time_Known_Value (Lo) then
1459 Lov := Expr_Value (Lo);
1464 if Compile_Time_Known_Value (Hi) then
1465 Hiv := Expr_Value (Hi);
1470 return Hiv < Lov + 3;
1473 end Length_Less_Than_4;
1475 -- Start of processing for Expand_Array_Comparison
1478 -- Deal first with unpacked case, where we can call a runtime routine
1479 -- except that we avoid this for targets for which are not addressable
1482 if not Is_Bit_Packed_Array (Typ1)
1483 and then Byte_Addressable
1485 -- The call we generate is:
1487 -- Compare_Array_xn[_Unaligned]
1488 -- (left'address, right'address, left'length, right'length) <op> 0
1490 -- x = U for unsigned, S for signed
1491 -- n = 8,16,32,64 for component size
1492 -- Add _Unaligned if length < 4 and component size is 8.
1493 -- <op> is the standard comparison operator
1495 if Component_Size (Typ1) = 8 then
1496 if Length_Less_Than_4 (Op1)
1498 Length_Less_Than_4 (Op2)
1500 if Is_Unsigned_Type (Ctyp) then
1501 Comp := RE_Compare_Array_U8_Unaligned;
1503 Comp := RE_Compare_Array_S8_Unaligned;
1507 if Is_Unsigned_Type (Ctyp) then
1508 Comp := RE_Compare_Array_U8;
1510 Comp := RE_Compare_Array_S8;
1514 elsif Component_Size (Typ1) = 16 then
1515 if Is_Unsigned_Type (Ctyp) then
1516 Comp := RE_Compare_Array_U16;
1518 Comp := RE_Compare_Array_S16;
1521 elsif Component_Size (Typ1) = 32 then
1522 if Is_Unsigned_Type (Ctyp) then
1523 Comp := RE_Compare_Array_U32;
1525 Comp := RE_Compare_Array_S32;
1528 else pragma Assert (Component_Size (Typ1) = 64);
1529 if Is_Unsigned_Type (Ctyp) then
1530 Comp := RE_Compare_Array_U64;
1532 Comp := RE_Compare_Array_S64;
1536 if RTE_Available (Comp) then
1538 -- Expand to a call only if the runtime function is available,
1539 -- otherwise fall back to inline code.
1541 Remove_Side_Effects (Op1, Name_Req => True);
1542 Remove_Side_Effects (Op2, Name_Req => True);
1545 Make_Function_Call (Sloc (Op1),
1546 Name => New_Occurrence_Of (RTE (Comp), Loc),
1548 Parameter_Associations => New_List (
1549 Make_Attribute_Reference (Loc,
1550 Prefix => Relocate_Node (Op1),
1551 Attribute_Name => Name_Address),
1553 Make_Attribute_Reference (Loc,
1554 Prefix => Relocate_Node (Op2),
1555 Attribute_Name => Name_Address),
1557 Make_Attribute_Reference (Loc,
1558 Prefix => Relocate_Node (Op1),
1559 Attribute_Name => Name_Length),
1561 Make_Attribute_Reference (Loc,
1562 Prefix => Relocate_Node (Op2),
1563 Attribute_Name => Name_Length))));
1566 Make_Integer_Literal (Sloc (Op2),
1569 Analyze_And_Resolve (Op1, Standard_Integer);
1570 Analyze_And_Resolve (Op2, Standard_Integer);
1575 -- Cases where we cannot make runtime call
1577 -- For (a <= b) we convert to not (a > b)
1579 if Chars (N) = Name_Op_Le then
1585 Right_Opnd => Op2)));
1586 Analyze_And_Resolve (N, Standard_Boolean);
1589 -- For < the Boolean expression is
1590 -- greater__nn (op2, op1)
1592 elsif Chars (N) = Name_Op_Lt then
1593 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1597 Op1 := Right_Opnd (N);
1598 Op2 := Left_Opnd (N);
1600 -- For (a >= b) we convert to not (a < b)
1602 elsif Chars (N) = Name_Op_Ge then
1608 Right_Opnd => Op2)));
1609 Analyze_And_Resolve (N, Standard_Boolean);
1612 -- For > the Boolean expression is
1613 -- greater__nn (op1, op2)
1616 pragma Assert (Chars (N) = Name_Op_Gt);
1617 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1620 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1622 Make_Function_Call (Loc,
1623 Name => New_Occurrence_Of (Func_Name, Loc),
1624 Parameter_Associations => New_List (Op1, Op2));
1626 Insert_Action (N, Func_Body);
1628 Analyze_And_Resolve (N, Standard_Boolean);
1629 end Expand_Array_Comparison;
1631 ---------------------------
1632 -- Expand_Array_Equality --
1633 ---------------------------
1635 -- Expand an equality function for multi-dimensional arrays. Here is an
1636 -- example of such a function for Nb_Dimension = 2
1638 -- function Enn (A : atyp; B : btyp) return boolean is
1640 -- if (A'length (1) = 0 or else A'length (2) = 0)
1642 -- (B'length (1) = 0 or else B'length (2) = 0)
1644 -- return True; -- RM 4.5.2(22)
1647 -- if A'length (1) /= B'length (1)
1649 -- A'length (2) /= B'length (2)
1651 -- return False; -- RM 4.5.2(23)
1655 -- A1 : Index_T1 := A'first (1);
1656 -- B1 : Index_T1 := B'first (1);
1660 -- A2 : Index_T2 := A'first (2);
1661 -- B2 : Index_T2 := B'first (2);
1664 -- if A (A1, A2) /= B (B1, B2) then
1668 -- exit when A2 = A'last (2);
1669 -- A2 := Index_T2'succ (A2);
1670 -- B2 := Index_T2'succ (B2);
1674 -- exit when A1 = A'last (1);
1675 -- A1 := Index_T1'succ (A1);
1676 -- B1 := Index_T1'succ (B1);
1683 -- Note on the formal types used (atyp and btyp). If either of the arrays
1684 -- is of a private type, we use the underlying type, and do an unchecked
1685 -- conversion of the actual. If either of the arrays has a bound depending
1686 -- on a discriminant, then we use the base type since otherwise we have an
1687 -- escaped discriminant in the function.
1689 -- If both arrays are constrained and have the same bounds, we can generate
1690 -- a loop with an explicit iteration scheme using a 'Range attribute over
1693 function Expand_Array_Equality
1698 Typ : Entity_Id) return Node_Id
1700 Loc : constant Source_Ptr := Sloc (Nod);
1701 Decls : constant List_Id := New_List;
1702 Index_List1 : constant List_Id := New_List;
1703 Index_List2 : constant List_Id := New_List;
1707 Func_Name : Entity_Id;
1708 Func_Body : Node_Id;
1710 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1711 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1715 -- The parameter types to be used for the formals
1720 Num : Int) return Node_Id;
1721 -- This builds the attribute reference Arr'Nam (Expr)
1723 function Component_Equality (Typ : Entity_Id) return Node_Id;
1724 -- Create one statement to compare corresponding components, designated
1725 -- by a full set of indexes.
1727 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1728 -- Given one of the arguments, computes the appropriate type to be used
1729 -- for that argument in the corresponding function formal
1731 function Handle_One_Dimension
1733 Index : Node_Id) return Node_Id;
1734 -- This procedure returns the following code
1737 -- Bn : Index_T := B'First (N);
1741 -- exit when An = A'Last (N);
1742 -- An := Index_T'Succ (An)
1743 -- Bn := Index_T'Succ (Bn)
1747 -- If both indexes are constrained and identical, the procedure
1748 -- returns a simpler loop:
1750 -- for An in A'Range (N) loop
1754 -- N is the dimension for which we are generating a loop. Index is the
1755 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1756 -- xxx statement is either the loop or declare for the next dimension
1757 -- or if this is the last dimension the comparison of corresponding
1758 -- components of the arrays.
1760 -- The actual way the code works is to return the comparison of
1761 -- corresponding components for the N+1 call. That's neater.
1763 function Test_Empty_Arrays return Node_Id;
1764 -- This function constructs the test for both arrays being empty
1765 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1767 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1769 function Test_Lengths_Correspond return Node_Id;
1770 -- This function constructs the test for arrays having different lengths
1771 -- in at least one index position, in which case the resulting code is:
1773 -- A'length (1) /= B'length (1)
1775 -- A'length (2) /= B'length (2)
1786 Num : Int) return Node_Id
1790 Make_Attribute_Reference (Loc,
1791 Attribute_Name => Nam,
1792 Prefix => New_Occurrence_Of (Arr, Loc),
1793 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1796 ------------------------
1797 -- Component_Equality --
1798 ------------------------
1800 function Component_Equality (Typ : Entity_Id) return Node_Id is
1805 -- if a(i1...) /= b(j1...) then return false; end if;
1808 Make_Indexed_Component (Loc,
1809 Prefix => Make_Identifier (Loc, Chars (A)),
1810 Expressions => Index_List1);
1813 Make_Indexed_Component (Loc,
1814 Prefix => Make_Identifier (Loc, Chars (B)),
1815 Expressions => Index_List2);
1817 Test := Expand_Composite_Equality
1818 (Nod, Component_Type (Typ), L, R, Decls);
1820 -- If some (sub)component is an unchecked_union, the whole operation
1821 -- will raise program error.
1823 if Nkind (Test) = N_Raise_Program_Error then
1825 -- This node is going to be inserted at a location where a
1826 -- statement is expected: clear its Etype so analysis will set
1827 -- it to the expected Standard_Void_Type.
1829 Set_Etype (Test, Empty);
1834 Make_Implicit_If_Statement (Nod,
1835 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1836 Then_Statements => New_List (
1837 Make_Simple_Return_Statement (Loc,
1838 Expression => New_Occurrence_Of (Standard_False, Loc))));
1840 end Component_Equality;
1846 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1857 T := Underlying_Type (T);
1859 X := First_Index (T);
1860 while Present (X) loop
1861 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1863 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1876 --------------------------
1877 -- Handle_One_Dimension --
1878 ---------------------------
1880 function Handle_One_Dimension
1882 Index : Node_Id) return Node_Id
1884 Need_Separate_Indexes : constant Boolean :=
1885 Ltyp /= Rtyp or else not Is_Constrained (Ltyp);
1886 -- If the index types are identical, and we are working with
1887 -- constrained types, then we can use the same index for both
1890 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1893 Index_T : Entity_Id;
1898 if N > Number_Dimensions (Ltyp) then
1899 return Component_Equality (Ltyp);
1902 -- Case where we generate a loop
1904 Index_T := Base_Type (Etype (Index));
1906 if Need_Separate_Indexes then
1907 Bn := Make_Temporary (Loc, 'B');
1912 Append (New_Occurrence_Of (An, Loc), Index_List1);
1913 Append (New_Occurrence_Of (Bn, Loc), Index_List2);
1915 Stm_List := New_List (
1916 Handle_One_Dimension (N + 1, Next_Index (Index)));
1918 if Need_Separate_Indexes then
1920 -- Generate guard for loop, followed by increments of indexes
1922 Append_To (Stm_List,
1923 Make_Exit_Statement (Loc,
1926 Left_Opnd => New_Occurrence_Of (An, Loc),
1927 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1929 Append_To (Stm_List,
1930 Make_Assignment_Statement (Loc,
1931 Name => New_Occurrence_Of (An, Loc),
1933 Make_Attribute_Reference (Loc,
1934 Prefix => New_Occurrence_Of (Index_T, Loc),
1935 Attribute_Name => Name_Succ,
1936 Expressions => New_List (
1937 New_Occurrence_Of (An, Loc)))));
1939 Append_To (Stm_List,
1940 Make_Assignment_Statement (Loc,
1941 Name => New_Occurrence_Of (Bn, Loc),
1943 Make_Attribute_Reference (Loc,
1944 Prefix => New_Occurrence_Of (Index_T, Loc),
1945 Attribute_Name => Name_Succ,
1946 Expressions => New_List (
1947 New_Occurrence_Of (Bn, Loc)))));
1950 -- If separate indexes, we need a declare block for An and Bn, and a
1951 -- loop without an iteration scheme.
1953 if Need_Separate_Indexes then
1955 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1958 Make_Block_Statement (Loc,
1959 Declarations => New_List (
1960 Make_Object_Declaration (Loc,
1961 Defining_Identifier => An,
1962 Object_Definition => New_Occurrence_Of (Index_T, Loc),
1963 Expression => Arr_Attr (A, Name_First, N)),
1965 Make_Object_Declaration (Loc,
1966 Defining_Identifier => Bn,
1967 Object_Definition => New_Occurrence_Of (Index_T, Loc),
1968 Expression => Arr_Attr (B, Name_First, N))),
1970 Handled_Statement_Sequence =>
1971 Make_Handled_Sequence_Of_Statements (Loc,
1972 Statements => New_List (Loop_Stm)));
1974 -- If no separate indexes, return loop statement with explicit
1975 -- iteration scheme on its own
1979 Make_Implicit_Loop_Statement (Nod,
1980 Statements => Stm_List,
1982 Make_Iteration_Scheme (Loc,
1983 Loop_Parameter_Specification =>
1984 Make_Loop_Parameter_Specification (Loc,
1985 Defining_Identifier => An,
1986 Discrete_Subtype_Definition =>
1987 Arr_Attr (A, Name_Range, N))));
1990 end Handle_One_Dimension;
1992 -----------------------
1993 -- Test_Empty_Arrays --
1994 -----------------------
1996 function Test_Empty_Arrays return Node_Id is
2006 for J in 1 .. Number_Dimensions (Ltyp) loop
2009 Left_Opnd => Arr_Attr (A, Name_Length, J),
2010 Right_Opnd => Make_Integer_Literal (Loc, 0));
2014 Left_Opnd => Arr_Attr (B, Name_Length, J),
2015 Right_Opnd => Make_Integer_Literal (Loc, 0));
2024 Left_Opnd => Relocate_Node (Alist),
2025 Right_Opnd => Atest);
2029 Left_Opnd => Relocate_Node (Blist),
2030 Right_Opnd => Btest);
2037 Right_Opnd => Blist);
2038 end Test_Empty_Arrays;
2040 -----------------------------
2041 -- Test_Lengths_Correspond --
2042 -----------------------------
2044 function Test_Lengths_Correspond return Node_Id is
2050 for J in 1 .. Number_Dimensions (Ltyp) loop
2053 Left_Opnd => Arr_Attr (A, Name_Length, J),
2054 Right_Opnd => Arr_Attr (B, Name_Length, J));
2061 Left_Opnd => Relocate_Node (Result),
2062 Right_Opnd => Rtest);
2067 end Test_Lengths_Correspond;
2069 -- Start of processing for Expand_Array_Equality
2072 Ltyp := Get_Arg_Type (Lhs);
2073 Rtyp := Get_Arg_Type (Rhs);
2075 -- For now, if the argument types are not the same, go to the base type,
2076 -- since the code assumes that the formals have the same type. This is
2077 -- fixable in future ???
2079 if Ltyp /= Rtyp then
2080 Ltyp := Base_Type (Ltyp);
2081 Rtyp := Base_Type (Rtyp);
2082 pragma Assert (Ltyp = Rtyp);
2085 -- Build list of formals for function
2087 Formals := New_List (
2088 Make_Parameter_Specification (Loc,
2089 Defining_Identifier => A,
2090 Parameter_Type => New_Occurrence_Of (Ltyp, Loc)),
2092 Make_Parameter_Specification (Loc,
2093 Defining_Identifier => B,
2094 Parameter_Type => New_Occurrence_Of (Rtyp, Loc)));
2096 Func_Name := Make_Temporary (Loc, 'E');
2098 -- Build statement sequence for function
2101 Make_Subprogram_Body (Loc,
2103 Make_Function_Specification (Loc,
2104 Defining_Unit_Name => Func_Name,
2105 Parameter_Specifications => Formals,
2106 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
2108 Declarations => Decls,
2110 Handled_Statement_Sequence =>
2111 Make_Handled_Sequence_Of_Statements (Loc,
2112 Statements => New_List (
2114 Make_Implicit_If_Statement (Nod,
2115 Condition => Test_Empty_Arrays,
2116 Then_Statements => New_List (
2117 Make_Simple_Return_Statement (Loc,
2119 New_Occurrence_Of (Standard_True, Loc)))),
2121 Make_Implicit_If_Statement (Nod,
2122 Condition => Test_Lengths_Correspond,
2123 Then_Statements => New_List (
2124 Make_Simple_Return_Statement (Loc,
2125 Expression => New_Occurrence_Of (Standard_False, Loc)))),
2127 Handle_One_Dimension (1, First_Index (Ltyp)),
2129 Make_Simple_Return_Statement (Loc,
2130 Expression => New_Occurrence_Of (Standard_True, Loc)))));
2132 Set_Has_Completion (Func_Name, True);
2133 Set_Is_Inlined (Func_Name);
2135 -- If the array type is distinct from the type of the arguments, it
2136 -- is the full view of a private type. Apply an unchecked conversion
2137 -- to insure that analysis of the call succeeds.
2147 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
2149 L := OK_Convert_To (Ltyp, Lhs);
2153 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
2155 R := OK_Convert_To (Rtyp, Rhs);
2158 Actuals := New_List (L, R);
2161 Append_To (Bodies, Func_Body);
2164 Make_Function_Call (Loc,
2165 Name => New_Occurrence_Of (Func_Name, Loc),
2166 Parameter_Associations => Actuals);
2167 end Expand_Array_Equality;
2169 -----------------------------
2170 -- Expand_Boolean_Operator --
2171 -----------------------------
2173 -- Note that we first get the actual subtypes of the operands, since we
2174 -- always want to deal with types that have bounds.
2176 procedure Expand_Boolean_Operator (N : Node_Id) is
2177 Typ : constant Entity_Id := Etype (N);
2180 -- Special case of bit packed array where both operands are known to be
2181 -- properly aligned. In this case we use an efficient run time routine
2182 -- to carry out the operation (see System.Bit_Ops).
2184 if Is_Bit_Packed_Array (Typ)
2185 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
2186 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
2188 Expand_Packed_Boolean_Operator (N);
2192 -- For the normal non-packed case, the general expansion is to build
2193 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2194 -- and then inserting it into the tree. The original operator node is
2195 -- then rewritten as a call to this function. We also use this in the
2196 -- packed case if either operand is a possibly unaligned object.
2199 Loc : constant Source_Ptr := Sloc (N);
2200 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2201 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2202 Func_Body : Node_Id;
2203 Func_Name : Entity_Id;
2206 Convert_To_Actual_Subtype (L);
2207 Convert_To_Actual_Subtype (R);
2208 Ensure_Defined (Etype (L), N);
2209 Ensure_Defined (Etype (R), N);
2210 Apply_Length_Check (R, Etype (L));
2212 if Nkind (N) = N_Op_Xor then
2213 Silly_Boolean_Array_Xor_Test (N, Etype (L));
2216 if Nkind (Parent (N)) = N_Assignment_Statement
2217 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
2219 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
2221 elsif Nkind (Parent (N)) = N_Op_Not
2222 and then Nkind (N) = N_Op_And
2223 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
2224 and then Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
2229 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
2230 Func_Name := Defining_Unit_Name (Specification (Func_Body));
2231 Insert_Action (N, Func_Body);
2233 -- Now rewrite the expression with a call
2236 Make_Function_Call (Loc,
2237 Name => New_Occurrence_Of (Func_Name, Loc),
2238 Parameter_Associations =>
2241 Make_Type_Conversion
2242 (Loc, New_Occurrence_Of (Etype (L), Loc), R))));
2244 Analyze_And_Resolve (N, Typ);
2247 end Expand_Boolean_Operator;
2249 ------------------------------------------------
2250 -- Expand_Compare_Minimize_Eliminate_Overflow --
2251 ------------------------------------------------
2253 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is
2254 Loc : constant Source_Ptr := Sloc (N);
2256 Result_Type : constant Entity_Id := Etype (N);
2257 -- Capture result type (could be a derived boolean type)
2262 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
2263 -- Entity for Long_Long_Integer'Base
2265 Check : constant Overflow_Mode_Type := Overflow_Check_Mode;
2266 -- Current overflow checking mode
2269 procedure Set_False;
2270 -- These procedures rewrite N with an occurrence of Standard_True or
2271 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2277 procedure Set_False is
2279 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2280 Warn_On_Known_Condition (N);
2287 procedure Set_True is
2289 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
2290 Warn_On_Known_Condition (N);
2293 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2296 -- Nothing to do unless we have a comparison operator with operands
2297 -- that are signed integer types, and we are operating in either
2298 -- MINIMIZED or ELIMINATED overflow checking mode.
2300 if Nkind (N) not in N_Op_Compare
2301 or else Check not in Minimized_Or_Eliminated
2302 or else not Is_Signed_Integer_Type (Etype (Left_Opnd (N)))
2307 -- OK, this is the case we are interested in. First step is to process
2308 -- our operands using the Minimize_Eliminate circuitry which applies
2309 -- this processing to the two operand subtrees.
2311 Minimize_Eliminate_Overflows
2312 (Left_Opnd (N), Llo, Lhi, Top_Level => False);
2313 Minimize_Eliminate_Overflows
2314 (Right_Opnd (N), Rlo, Rhi, Top_Level => False);
2316 -- See if the range information decides the result of the comparison.
2317 -- We can only do this if we in fact have full range information (which
2318 -- won't be the case if either operand is bignum at this stage).
2320 if Llo /= No_Uint and then Rlo /= No_Uint then
2321 case N_Op_Compare (Nkind (N)) is
2323 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2325 elsif Llo > Rhi or else Lhi < Rlo then
2332 elsif Lhi < Rlo then
2339 elsif Lhi <= Rlo then
2346 elsif Lhi <= Rlo then
2353 elsif Lhi < Rlo then
2358 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2360 elsif Llo > Rhi or else Lhi < Rlo then
2365 -- All done if we did the rewrite
2367 if Nkind (N) not in N_Op_Compare then
2372 -- Otherwise, time to do the comparison
2375 Ltype : constant Entity_Id := Etype (Left_Opnd (N));
2376 Rtype : constant Entity_Id := Etype (Right_Opnd (N));
2379 -- If the two operands have the same signed integer type we are
2380 -- all set, nothing more to do. This is the case where either
2381 -- both operands were unchanged, or we rewrote both of them to
2382 -- be Long_Long_Integer.
2384 -- Note: Entity for the comparison may be wrong, but it's not worth
2385 -- the effort to change it, since the back end does not use it.
2387 if Is_Signed_Integer_Type (Ltype)
2388 and then Base_Type (Ltype) = Base_Type (Rtype)
2392 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2394 elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then
2396 Left : Node_Id := Left_Opnd (N);
2397 Right : Node_Id := Right_Opnd (N);
2398 -- Bignum references for left and right operands
2401 if not Is_RTE (Ltype, RE_Bignum) then
2402 Left := Convert_To_Bignum (Left);
2403 elsif not Is_RTE (Rtype, RE_Bignum) then
2404 Right := Convert_To_Bignum (Right);
2407 -- We rewrite our node with:
2410 -- Bnn : Result_Type;
2412 -- M : Mark_Id := SS_Mark;
2414 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2422 Blk : constant Node_Id := Make_Bignum_Block (Loc);
2423 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
2427 case N_Op_Compare (Nkind (N)) is
2428 when N_Op_Eq => Ent := RE_Big_EQ;
2429 when N_Op_Ge => Ent := RE_Big_GE;
2430 when N_Op_Gt => Ent := RE_Big_GT;
2431 when N_Op_Le => Ent := RE_Big_LE;
2432 when N_Op_Lt => Ent := RE_Big_LT;
2433 when N_Op_Ne => Ent := RE_Big_NE;
2436 -- Insert assignment to Bnn into the bignum block
2439 (First (Statements (Handled_Statement_Sequence (Blk))),
2440 Make_Assignment_Statement (Loc,
2441 Name => New_Occurrence_Of (Bnn, Loc),
2443 Make_Function_Call (Loc,
2445 New_Occurrence_Of (RTE (Ent), Loc),
2446 Parameter_Associations => New_List (Left, Right))));
2448 -- Now do the rewrite with expression actions
2451 Make_Expression_With_Actions (Loc,
2452 Actions => New_List (
2453 Make_Object_Declaration (Loc,
2454 Defining_Identifier => Bnn,
2455 Object_Definition =>
2456 New_Occurrence_Of (Result_Type, Loc)),
2458 Expression => New_Occurrence_Of (Bnn, Loc)));
2459 Analyze_And_Resolve (N, Result_Type);
2463 -- No bignums involved, but types are different, so we must have
2464 -- rewritten one of the operands as a Long_Long_Integer but not
2467 -- If left operand is Long_Long_Integer, convert right operand
2468 -- and we are done (with a comparison of two Long_Long_Integers).
2470 elsif Ltype = LLIB then
2471 Convert_To_And_Rewrite (LLIB, Right_Opnd (N));
2472 Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks);
2475 -- If right operand is Long_Long_Integer, convert left operand
2476 -- and we are done (with a comparison of two Long_Long_Integers).
2478 -- This is the only remaining possibility
2480 else pragma Assert (Rtype = LLIB);
2481 Convert_To_And_Rewrite (LLIB, Left_Opnd (N));
2482 Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks);
2486 end Expand_Compare_Minimize_Eliminate_Overflow;
2488 -------------------------------
2489 -- Expand_Composite_Equality --
2490 -------------------------------
2492 -- This function is only called for comparing internal fields of composite
2493 -- types when these fields are themselves composites. This is a special
2494 -- case because it is not possible to respect normal Ada visibility rules.
2496 function Expand_Composite_Equality
2501 Bodies : List_Id) return Node_Id
2503 Loc : constant Source_Ptr := Sloc (Nod);
2504 Full_Type : Entity_Id;
2508 function Find_Primitive_Eq return Node_Id;
2509 -- AI05-0123: Locate primitive equality for type if it exists, and
2510 -- build the corresponding call. If operation is abstract, replace
2511 -- call with an explicit raise. Return Empty if there is no primitive.
2513 -----------------------
2514 -- Find_Primitive_Eq --
2515 -----------------------
2517 function Find_Primitive_Eq return Node_Id is
2522 Prim_E := First_Elmt (Collect_Primitive_Operations (Typ));
2523 while Present (Prim_E) loop
2524 Prim := Node (Prim_E);
2526 -- Locate primitive equality with the right signature
2528 if Chars (Prim) = Name_Op_Eq
2529 and then Etype (First_Formal (Prim)) =
2530 Etype (Next_Formal (First_Formal (Prim)))
2531 and then Etype (Prim) = Standard_Boolean
2533 if Is_Abstract_Subprogram (Prim) then
2535 Make_Raise_Program_Error (Loc,
2536 Reason => PE_Explicit_Raise);
2540 Make_Function_Call (Loc,
2541 Name => New_Occurrence_Of (Prim, Loc),
2542 Parameter_Associations => New_List (Lhs, Rhs));
2549 -- If not found, predefined operation will be used
2552 end Find_Primitive_Eq;
2554 -- Start of processing for Expand_Composite_Equality
2557 if Is_Private_Type (Typ) then
2558 Full_Type := Underlying_Type (Typ);
2563 -- If the private type has no completion the context may be the
2564 -- expansion of a composite equality for a composite type with some
2565 -- still incomplete components. The expression will not be analyzed
2566 -- until the enclosing type is completed, at which point this will be
2567 -- properly expanded, unless there is a bona fide completion error.
2569 if No (Full_Type) then
2570 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2573 Full_Type := Base_Type (Full_Type);
2575 -- When the base type itself is private, use the full view to expand
2576 -- the composite equality.
2578 if Is_Private_Type (Full_Type) then
2579 Full_Type := Underlying_Type (Full_Type);
2582 -- Case of array types
2584 if Is_Array_Type (Full_Type) then
2586 -- If the operand is an elementary type other than a floating-point
2587 -- type, then we can simply use the built-in block bitwise equality,
2588 -- since the predefined equality operators always apply and bitwise
2589 -- equality is fine for all these cases.
2591 if Is_Elementary_Type (Component_Type (Full_Type))
2592 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2594 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2596 -- For composite component types, and floating-point types, use the
2597 -- expansion. This deals with tagged component types (where we use
2598 -- the applicable equality routine) and floating-point, (where we
2599 -- need to worry about negative zeroes), and also the case of any
2600 -- composite type recursively containing such fields.
2603 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2606 -- Case of tagged record types
2608 elsif Is_Tagged_Type (Full_Type) then
2610 -- Call the primitive operation "=" of this type
2612 if Is_Class_Wide_Type (Full_Type) then
2613 Full_Type := Root_Type (Full_Type);
2616 -- If this is derived from an untagged private type completed with a
2617 -- tagged type, it does not have a full view, so we use the primitive
2618 -- operations of the private type. This check should no longer be
2619 -- necessary when these types receive their full views ???
2621 if Is_Private_Type (Typ)
2622 and then not Is_Tagged_Type (Typ)
2623 and then not Is_Controlled (Typ)
2624 and then Is_Derived_Type (Typ)
2625 and then No (Full_View (Typ))
2627 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2629 Prim := First_Elmt (Primitive_Operations (Full_Type));
2633 Eq_Op := Node (Prim);
2634 exit when Chars (Eq_Op) = Name_Op_Eq
2635 and then Etype (First_Formal (Eq_Op)) =
2636 Etype (Next_Formal (First_Formal (Eq_Op)))
2637 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2639 pragma Assert (Present (Prim));
2642 Eq_Op := Node (Prim);
2645 Make_Function_Call (Loc,
2646 Name => New_Occurrence_Of (Eq_Op, Loc),
2647 Parameter_Associations =>
2649 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2650 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2652 -- Case of untagged record types
2654 elsif Is_Record_Type (Full_Type) then
2655 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2657 if Present (Eq_Op) then
2658 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2660 -- Inherited equality from parent type. Convert the actuals to
2661 -- match signature of operation.
2664 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2668 Make_Function_Call (Loc,
2669 Name => New_Occurrence_Of (Eq_Op, Loc),
2670 Parameter_Associations => New_List (
2671 OK_Convert_To (T, Lhs),
2672 OK_Convert_To (T, Rhs)));
2676 -- Comparison between Unchecked_Union components
2678 if Is_Unchecked_Union (Full_Type) then
2680 Lhs_Type : Node_Id := Full_Type;
2681 Rhs_Type : Node_Id := Full_Type;
2682 Lhs_Discr_Val : Node_Id;
2683 Rhs_Discr_Val : Node_Id;
2688 if Nkind (Lhs) = N_Selected_Component then
2689 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2694 if Nkind (Rhs) = N_Selected_Component then
2695 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2698 -- Lhs of the composite equality
2700 if Is_Constrained (Lhs_Type) then
2702 -- Since the enclosing record type can never be an
2703 -- Unchecked_Union (this code is executed for records
2704 -- that do not have variants), we may reference its
2707 if Nkind (Lhs) = N_Selected_Component
2708 and then Has_Per_Object_Constraint
2709 (Entity (Selector_Name (Lhs)))
2712 Make_Selected_Component (Loc,
2713 Prefix => Prefix (Lhs),
2716 (Get_Discriminant_Value
2717 (First_Discriminant (Lhs_Type),
2719 Stored_Constraint (Lhs_Type))));
2724 (Get_Discriminant_Value
2725 (First_Discriminant (Lhs_Type),
2727 Stored_Constraint (Lhs_Type)));
2731 -- It is not possible to infer the discriminant since
2732 -- the subtype is not constrained.
2735 Make_Raise_Program_Error (Loc,
2736 Reason => PE_Unchecked_Union_Restriction);
2739 -- Rhs of the composite equality
2741 if Is_Constrained (Rhs_Type) then
2742 if Nkind (Rhs) = N_Selected_Component
2743 and then Has_Per_Object_Constraint
2744 (Entity (Selector_Name (Rhs)))
2747 Make_Selected_Component (Loc,
2748 Prefix => Prefix (Rhs),
2751 (Get_Discriminant_Value
2752 (First_Discriminant (Rhs_Type),
2754 Stored_Constraint (Rhs_Type))));
2759 (Get_Discriminant_Value
2760 (First_Discriminant (Rhs_Type),
2762 Stored_Constraint (Rhs_Type)));
2767 Make_Raise_Program_Error (Loc,
2768 Reason => PE_Unchecked_Union_Restriction);
2771 -- Call the TSS equality function with the inferred
2772 -- discriminant values.
2775 Make_Function_Call (Loc,
2776 Name => New_Occurrence_Of (Eq_Op, Loc),
2777 Parameter_Associations => New_List (
2784 -- All cases other than comparing Unchecked_Union types
2788 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2791 Make_Function_Call (Loc,
2793 New_Occurrence_Of (Eq_Op, Loc),
2794 Parameter_Associations => New_List (
2795 OK_Convert_To (T, Lhs),
2796 OK_Convert_To (T, Rhs)));
2801 -- Equality composes in Ada 2012 for untagged record types. It also
2802 -- composes for bounded strings, because they are part of the
2803 -- predefined environment. We could make it compose for bounded
2804 -- strings by making them tagged, or by making sure all subcomponents
2805 -- are set to the same value, even when not used. Instead, we have
2806 -- this special case in the compiler, because it's more efficient.
2808 elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then
2810 -- If no TSS has been created for the type, check whether there is
2811 -- a primitive equality declared for it.
2814 Op : constant Node_Id := Find_Primitive_Eq;
2817 -- Use user-defined primitive if it exists, otherwise use
2818 -- predefined equality.
2820 if Present (Op) then
2823 return Make_Op_Eq (Loc, Lhs, Rhs);
2828 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2831 -- Non-composite types (always use predefined equality)
2834 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2836 end Expand_Composite_Equality;
2838 ------------------------
2839 -- Expand_Concatenate --
2840 ------------------------
2842 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2843 Loc : constant Source_Ptr := Sloc (Cnode);
2845 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2846 -- Result type of concatenation
2848 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2849 -- Component type. Elements of this component type can appear as one
2850 -- of the operands of concatenation as well as arrays.
2852 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2855 Ityp : constant Entity_Id := Base_Type (Istyp);
2856 -- Index type. This is the base type of the index subtype, and is used
2857 -- for all computed bounds (which may be out of range of Istyp in the
2858 -- case of null ranges).
2861 -- This is the type we use to do arithmetic to compute the bounds and
2862 -- lengths of operands. The choice of this type is a little subtle and
2863 -- is discussed in a separate section at the start of the body code.
2865 Concatenation_Error : exception;
2866 -- Raised if concatenation is sure to raise a CE
2868 Result_May_Be_Null : Boolean := True;
2869 -- Reset to False if at least one operand is encountered which is known
2870 -- at compile time to be non-null. Used for handling the special case
2871 -- of setting the high bound to the last operand high bound for a null
2872 -- result, thus ensuring a proper high bound in the super-flat case.
2874 N : constant Nat := List_Length (Opnds);
2875 -- Number of concatenation operands including possibly null operands
2878 -- Number of operands excluding any known to be null, except that the
2879 -- last operand is always retained, in case it provides the bounds for
2883 -- Current operand being processed in the loop through operands. After
2884 -- this loop is complete, always contains the last operand (which is not
2885 -- the same as Operands (NN), since null operands are skipped).
2887 -- Arrays describing the operands, only the first NN entries of each
2888 -- array are set (NN < N when we exclude known null operands).
2890 Is_Fixed_Length : array (1 .. N) of Boolean;
2891 -- True if length of corresponding operand known at compile time
2893 Operands : array (1 .. N) of Node_Id;
2894 -- Set to the corresponding entry in the Opnds list (but note that null
2895 -- operands are excluded, so not all entries in the list are stored).
2897 Fixed_Length : array (1 .. N) of Uint;
2898 -- Set to length of operand. Entries in this array are set only if the
2899 -- corresponding entry in Is_Fixed_Length is True.
2901 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2902 -- Set to lower bound of operand. Either an integer literal in the case
2903 -- where the bound is known at compile time, else actual lower bound.
2904 -- The operand low bound is of type Ityp.
2906 Var_Length : array (1 .. N) of Entity_Id;
2907 -- Set to an entity of type Natural that contains the length of an
2908 -- operand whose length is not known at compile time. Entries in this
2909 -- array are set only if the corresponding entry in Is_Fixed_Length
2910 -- is False. The entity is of type Artyp.
2912 Aggr_Length : array (0 .. N) of Node_Id;
2913 -- The J'th entry in an expression node that represents the total length
2914 -- of operands 1 through J. It is either an integer literal node, or a
2915 -- reference to a constant entity with the right value, so it is fine
2916 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2917 -- entry always is set to zero. The length is of type Artyp.
2919 Low_Bound : Node_Id;
2920 -- A tree node representing the low bound of the result (of type Ityp).
2921 -- This is either an integer literal node, or an identifier reference to
2922 -- a constant entity initialized to the appropriate value.
2924 Last_Opnd_Low_Bound : Node_Id;
2925 -- A tree node representing the low bound of the last operand. This
2926 -- need only be set if the result could be null. It is used for the
2927 -- special case of setting the right low bound for a null result.
2928 -- This is of type Ityp.
2930 Last_Opnd_High_Bound : Node_Id;
2931 -- A tree node representing the high bound of the last operand. This
2932 -- need only be set if the result could be null. It is used for the
2933 -- special case of setting the right high bound for a null result.
2934 -- This is of type Ityp.
2936 High_Bound : Node_Id;
2937 -- A tree node representing the high bound of the result (of type Ityp)
2940 -- Result of the concatenation (of type Ityp)
2942 Actions : constant List_Id := New_List;
2943 -- Collect actions to be inserted
2945 Known_Non_Null_Operand_Seen : Boolean;
2946 -- Set True during generation of the assignments of operands into
2947 -- result once an operand known to be non-null has been seen.
2949 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2950 -- This function makes an N_Integer_Literal node that is returned in
2951 -- analyzed form with the type set to Artyp. Importantly this literal
2952 -- is not flagged as static, so that if we do computations with it that
2953 -- result in statically detected out of range conditions, we will not
2954 -- generate error messages but instead warning messages.
2956 function To_Artyp (X : Node_Id) return Node_Id;
2957 -- Given a node of type Ityp, returns the corresponding value of type
2958 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2959 -- For enum types, the Pos of the value is returned.
2961 function To_Ityp (X : Node_Id) return Node_Id;
2962 -- The inverse function (uses Val in the case of enumeration types)
2964 ------------------------
2965 -- Make_Artyp_Literal --
2966 ------------------------
2968 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2969 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2971 Set_Etype (Result, Artyp);
2972 Set_Analyzed (Result, True);
2973 Set_Is_Static_Expression (Result, False);
2975 end Make_Artyp_Literal;
2981 function To_Artyp (X : Node_Id) return Node_Id is
2983 if Ityp = Base_Type (Artyp) then
2986 elsif Is_Enumeration_Type (Ityp) then
2988 Make_Attribute_Reference (Loc,
2989 Prefix => New_Occurrence_Of (Ityp, Loc),
2990 Attribute_Name => Name_Pos,
2991 Expressions => New_List (X));
2994 return Convert_To (Artyp, X);
3002 function To_Ityp (X : Node_Id) return Node_Id is
3004 if Is_Enumeration_Type (Ityp) then
3006 Make_Attribute_Reference (Loc,
3007 Prefix => New_Occurrence_Of (Ityp, Loc),
3008 Attribute_Name => Name_Val,
3009 Expressions => New_List (X));
3011 -- Case where we will do a type conversion
3014 if Ityp = Base_Type (Artyp) then
3017 return Convert_To (Ityp, X);
3022 -- Local Declarations
3024 Lib_Level_Target : constant Boolean :=
3025 Nkind (Parent (Cnode)) = N_Object_Declaration
3027 Is_Library_Level_Entity (Defining_Identifier (Parent (Cnode)));
3029 -- If the concatenation declares a library level entity, we call the
3030 -- built-in concatenation routines to prevent code bloat, regardless
3031 -- of optimization level. This is space-efficient, and prevent linking
3032 -- problems when units are compiled with different optimizations.
3034 Opnd_Typ : Entity_Id;
3041 -- Start of processing for Expand_Concatenate
3044 -- Choose an appropriate computational type
3046 -- We will be doing calculations of lengths and bounds in this routine
3047 -- and computing one from the other in some cases, e.g. getting the high
3048 -- bound by adding the length-1 to the low bound.
3050 -- We can't just use the index type, or even its base type for this
3051 -- purpose for two reasons. First it might be an enumeration type which
3052 -- is not suitable for computations of any kind, and second it may
3053 -- simply not have enough range. For example if the index type is
3054 -- -128..+127 then lengths can be up to 256, which is out of range of
3057 -- For enumeration types, we can simply use Standard_Integer, this is
3058 -- sufficient since the actual number of enumeration literals cannot
3059 -- possibly exceed the range of integer (remember we will be doing the
3060 -- arithmetic with POS values, not representation values).
3062 if Is_Enumeration_Type (Ityp) then
3063 Artyp := Standard_Integer;
3065 -- If index type is Positive, we use the standard unsigned type, to give
3066 -- more room on the top of the range, obviating the need for an overflow
3067 -- check when creating the upper bound. This is needed to avoid junk
3068 -- overflow checks in the common case of String types.
3070 -- ??? Disabled for now
3072 -- elsif Istyp = Standard_Positive then
3073 -- Artyp := Standard_Unsigned;
3075 -- For modular types, we use a 32-bit modular type for types whose size
3076 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
3077 -- identity type, and for larger unsigned types we use 64-bits.
3079 elsif Is_Modular_Integer_Type (Ityp) then
3080 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
3081 Artyp := Standard_Unsigned;
3082 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
3085 Artyp := RTE (RE_Long_Long_Unsigned);
3088 -- Similar treatment for signed types
3091 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
3092 Artyp := Standard_Integer;
3093 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
3096 Artyp := Standard_Long_Long_Integer;
3100 -- Supply dummy entry at start of length array
3102 Aggr_Length (0) := Make_Artyp_Literal (0);
3104 -- Go through operands setting up the above arrays
3108 Opnd := Remove_Head (Opnds);
3109 Opnd_Typ := Etype (Opnd);
3111 -- The parent got messed up when we put the operands in a list,
3112 -- so now put back the proper parent for the saved operand, that
3113 -- is to say the concatenation node, to make sure that each operand
3114 -- is seen as a subexpression, e.g. if actions must be inserted.
3116 Set_Parent (Opnd, Cnode);
3118 -- Set will be True when we have setup one entry in the array
3122 -- Singleton element (or character literal) case
3124 if Base_Type (Opnd_Typ) = Ctyp then
3126 Operands (NN) := Opnd;
3127 Is_Fixed_Length (NN) := True;
3128 Fixed_Length (NN) := Uint_1;
3129 Result_May_Be_Null := False;
3131 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
3132 -- since we know that the result cannot be null).
3134 Opnd_Low_Bound (NN) :=
3135 Make_Attribute_Reference (Loc,
3136 Prefix => New_Occurrence_Of (Istyp, Loc),
3137 Attribute_Name => Name_First);
3141 -- String literal case (can only occur for strings of course)
3143 elsif Nkind (Opnd) = N_String_Literal then
3144 Len := String_Literal_Length (Opnd_Typ);
3147 Result_May_Be_Null := False;
3150 -- Capture last operand low and high bound if result could be null
3152 if J = N and then Result_May_Be_Null then
3153 Last_Opnd_Low_Bound :=
3154 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3156 Last_Opnd_High_Bound :=
3157 Make_Op_Subtract (Loc,
3159 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
3160 Right_Opnd => Make_Integer_Literal (Loc, 1));
3163 -- Skip null string literal
3165 if J < N and then Len = 0 then
3170 Operands (NN) := Opnd;
3171 Is_Fixed_Length (NN) := True;
3173 -- Set length and bounds
3175 Fixed_Length (NN) := Len;
3177 Opnd_Low_Bound (NN) :=
3178 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3185 -- Check constrained case with known bounds
3187 if Is_Constrained (Opnd_Typ) then
3189 Index : constant Node_Id := First_Index (Opnd_Typ);
3190 Indx_Typ : constant Entity_Id := Etype (Index);
3191 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
3192 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
3195 -- Fixed length constrained array type with known at compile
3196 -- time bounds is last case of fixed length operand.
3198 if Compile_Time_Known_Value (Lo)
3200 Compile_Time_Known_Value (Hi)
3203 Loval : constant Uint := Expr_Value (Lo);
3204 Hival : constant Uint := Expr_Value (Hi);
3205 Len : constant Uint :=
3206 UI_Max (Hival - Loval + 1, Uint_0);
3210 Result_May_Be_Null := False;
3213 -- Capture last operand bounds if result could be null
3215 if J = N and then Result_May_Be_Null then
3216 Last_Opnd_Low_Bound :=
3218 Make_Integer_Literal (Loc, Expr_Value (Lo)));
3220 Last_Opnd_High_Bound :=
3222 Make_Integer_Literal (Loc, Expr_Value (Hi)));
3225 -- Exclude null length case unless last operand
3227 if J < N and then Len = 0 then
3232 Operands (NN) := Opnd;
3233 Is_Fixed_Length (NN) := True;
3234 Fixed_Length (NN) := Len;
3236 Opnd_Low_Bound (NN) :=
3238 (Make_Integer_Literal (Loc, Expr_Value (Lo)));
3245 -- All cases where the length is not known at compile time, or the
3246 -- special case of an operand which is known to be null but has a
3247 -- lower bound other than 1 or is other than a string type.
3252 -- Capture operand bounds
3254 Opnd_Low_Bound (NN) :=
3255 Make_Attribute_Reference (Loc,
3257 Duplicate_Subexpr (Opnd, Name_Req => True),
3258 Attribute_Name => Name_First);
3260 -- Capture last operand bounds if result could be null
3262 if J = N and Result_May_Be_Null then
3263 Last_Opnd_Low_Bound :=
3265 Make_Attribute_Reference (Loc,
3267 Duplicate_Subexpr (Opnd, Name_Req => True),
3268 Attribute_Name => Name_First));
3270 Last_Opnd_High_Bound :=
3272 Make_Attribute_Reference (Loc,
3274 Duplicate_Subexpr (Opnd, Name_Req => True),
3275 Attribute_Name => Name_Last));
3278 -- Capture length of operand in entity
3280 Operands (NN) := Opnd;
3281 Is_Fixed_Length (NN) := False;
3283 Var_Length (NN) := Make_Temporary (Loc, 'L');
3286 Make_Object_Declaration (Loc,
3287 Defining_Identifier => Var_Length (NN),
3288 Constant_Present => True,
3289 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3291 Make_Attribute_Reference (Loc,
3293 Duplicate_Subexpr (Opnd, Name_Req => True),
3294 Attribute_Name => Name_Length)));
3298 -- Set next entry in aggregate length array
3300 -- For first entry, make either integer literal for fixed length
3301 -- or a reference to the saved length for variable length.
3304 if Is_Fixed_Length (1) then
3305 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
3307 Aggr_Length (1) := New_Occurrence_Of (Var_Length (1), Loc);
3310 -- If entry is fixed length and only fixed lengths so far, make
3311 -- appropriate new integer literal adding new length.
3313 elsif Is_Fixed_Length (NN)
3314 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
3317 Make_Integer_Literal (Loc,
3318 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
3320 -- All other cases, construct an addition node for the length and
3321 -- create an entity initialized to this length.
3324 Ent := Make_Temporary (Loc, 'L');
3326 if Is_Fixed_Length (NN) then
3327 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
3329 Clen := New_Occurrence_Of (Var_Length (NN), Loc);
3333 Make_Object_Declaration (Loc,
3334 Defining_Identifier => Ent,
3335 Constant_Present => True,
3336 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3339 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
3340 Right_Opnd => Clen)));
3342 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
3349 -- If we have only skipped null operands, return the last operand
3356 -- If we have only one non-null operand, return it and we are done.
3357 -- There is one case in which this cannot be done, and that is when
3358 -- the sole operand is of the element type, in which case it must be
3359 -- converted to an array, and the easiest way of doing that is to go
3360 -- through the normal general circuit.
3362 if NN = 1 and then Base_Type (Etype (Operands (1))) /= Ctyp then
3363 Result := Operands (1);
3367 -- Cases where we have a real concatenation
3369 -- Next step is to find the low bound for the result array that we
3370 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3372 -- If the ultimate ancestor of the index subtype is a constrained array
3373 -- definition, then the lower bound is that of the index subtype as
3374 -- specified by (RM 4.5.3(6)).
3376 -- The right test here is to go to the root type, and then the ultimate
3377 -- ancestor is the first subtype of this root type.
3379 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
3381 Make_Attribute_Reference (Loc,
3383 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
3384 Attribute_Name => Name_First);
3386 -- If the first operand in the list has known length we know that
3387 -- the lower bound of the result is the lower bound of this operand.
3389 elsif Is_Fixed_Length (1) then
3390 Low_Bound := Opnd_Low_Bound (1);
3392 -- OK, we don't know the lower bound, we have to build a horrible
3393 -- if expression node of the form
3395 -- if Cond1'Length /= 0 then
3398 -- if Opnd2'Length /= 0 then
3403 -- The nesting ends either when we hit an operand whose length is known
3404 -- at compile time, or on reaching the last operand, whose low bound we
3405 -- take unconditionally whether or not it is null. It's easiest to do
3406 -- this with a recursive procedure:
3410 function Get_Known_Bound (J : Nat) return Node_Id;
3411 -- Returns the lower bound determined by operands J .. NN
3413 ---------------------
3414 -- Get_Known_Bound --
3415 ---------------------
3417 function Get_Known_Bound (J : Nat) return Node_Id is
3419 if Is_Fixed_Length (J) or else J = NN then
3420 return New_Copy (Opnd_Low_Bound (J));
3424 Make_If_Expression (Loc,
3425 Expressions => New_List (
3429 New_Occurrence_Of (Var_Length (J), Loc),
3431 Make_Integer_Literal (Loc, 0)),
3433 New_Copy (Opnd_Low_Bound (J)),
3434 Get_Known_Bound (J + 1)));
3436 end Get_Known_Bound;
3439 Ent := Make_Temporary (Loc, 'L');
3442 Make_Object_Declaration (Loc,
3443 Defining_Identifier => Ent,
3444 Constant_Present => True,
3445 Object_Definition => New_Occurrence_Of (Ityp, Loc),
3446 Expression => Get_Known_Bound (1)));
3448 Low_Bound := New_Occurrence_Of (Ent, Loc);
3452 -- Now we can safely compute the upper bound, normally
3453 -- Low_Bound + Length - 1.
3458 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3460 Make_Op_Subtract (Loc,
3461 Left_Opnd => New_Copy (Aggr_Length (NN)),
3462 Right_Opnd => Make_Artyp_Literal (1))));
3464 -- Note that calculation of the high bound may cause overflow in some
3465 -- very weird cases, so in the general case we need an overflow check on
3466 -- the high bound. We can avoid this for the common case of string types
3467 -- and other types whose index is Positive, since we chose a wider range
3468 -- for the arithmetic type.
3470 if Istyp /= Standard_Positive then
3471 Activate_Overflow_Check (High_Bound);
3474 -- Handle the exceptional case where the result is null, in which case
3475 -- case the bounds come from the last operand (so that we get the proper
3476 -- bounds if the last operand is super-flat).
3478 if Result_May_Be_Null then
3480 Make_If_Expression (Loc,
3481 Expressions => New_List (
3483 Left_Opnd => New_Copy (Aggr_Length (NN)),
3484 Right_Opnd => Make_Artyp_Literal (0)),
3485 Last_Opnd_Low_Bound,
3489 Make_If_Expression (Loc,
3490 Expressions => New_List (
3492 Left_Opnd => New_Copy (Aggr_Length (NN)),
3493 Right_Opnd => Make_Artyp_Literal (0)),
3494 Last_Opnd_High_Bound,
3498 -- Here is where we insert the saved up actions
3500 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3502 -- Now we construct an array object with appropriate bounds. We mark
3503 -- the target as internal to prevent useless initialization when
3504 -- Initialize_Scalars is enabled. Also since this is the actual result
3505 -- entity, we make sure we have debug information for the result.
3507 Ent := Make_Temporary (Loc, 'S');
3508 Set_Is_Internal (Ent);
3509 Set_Needs_Debug_Info (Ent);
3511 -- If the bound is statically known to be out of range, we do not want
3512 -- to abort, we want a warning and a runtime constraint error. Note that
3513 -- we have arranged that the result will not be treated as a static
3514 -- constant, so we won't get an illegality during this insertion.
3516 Insert_Action (Cnode,
3517 Make_Object_Declaration (Loc,
3518 Defining_Identifier => Ent,
3519 Object_Definition =>
3520 Make_Subtype_Indication (Loc,
3521 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
3523 Make_Index_Or_Discriminant_Constraint (Loc,
3524 Constraints => New_List (
3526 Low_Bound => Low_Bound,
3527 High_Bound => High_Bound))))),
3528 Suppress => All_Checks);
3530 -- If the result of the concatenation appears as the initializing
3531 -- expression of an object declaration, we can just rename the
3532 -- result, rather than copying it.
3534 Set_OK_To_Rename (Ent);
3536 -- Catch the static out of range case now
3538 if Raises_Constraint_Error (High_Bound) then
3539 raise Concatenation_Error;
3542 -- Now we will generate the assignments to do the actual concatenation
3544 -- There is one case in which we will not do this, namely when all the
3545 -- following conditions are met:
3547 -- The result type is Standard.String
3549 -- There are nine or fewer retained (non-null) operands
3551 -- The optimization level is -O0
3553 -- The corresponding System.Concat_n.Str_Concat_n routine is
3554 -- available in the run time.
3556 -- The debug flag gnatd.c is not set
3558 -- If all these conditions are met then we generate a call to the
3559 -- relevant concatenation routine. The purpose of this is to avoid
3560 -- undesirable code bloat at -O0.
3562 if Atyp = Standard_String
3563 and then NN in 2 .. 9
3564 and then (Lib_Level_Target
3565 or else ((Optimization_Level = 0 or else Debug_Flag_Dot_CC)
3566 and then not Debug_Flag_Dot_C))
3569 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3580 if RTE_Available (RR (NN)) then
3582 Opnds : constant List_Id :=
3583 New_List (New_Occurrence_Of (Ent, Loc));
3586 for J in 1 .. NN loop
3587 if Is_List_Member (Operands (J)) then
3588 Remove (Operands (J));
3591 if Base_Type (Etype (Operands (J))) = Ctyp then
3593 Make_Aggregate (Loc,
3594 Component_Associations => New_List (
3595 Make_Component_Association (Loc,
3596 Choices => New_List (
3597 Make_Integer_Literal (Loc, 1)),
3598 Expression => Operands (J)))));
3601 Append_To (Opnds, Operands (J));
3605 Insert_Action (Cnode,
3606 Make_Procedure_Call_Statement (Loc,
3607 Name => New_Occurrence_Of (RTE (RR (NN)), Loc),
3608 Parameter_Associations => Opnds));
3610 Result := New_Occurrence_Of (Ent, Loc);
3617 -- Not special case so generate the assignments
3619 Known_Non_Null_Operand_Seen := False;
3621 for J in 1 .. NN loop
3623 Lo : constant Node_Id :=
3625 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3626 Right_Opnd => Aggr_Length (J - 1));
3628 Hi : constant Node_Id :=
3630 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3632 Make_Op_Subtract (Loc,
3633 Left_Opnd => Aggr_Length (J),
3634 Right_Opnd => Make_Artyp_Literal (1)));
3637 -- Singleton case, simple assignment
3639 if Base_Type (Etype (Operands (J))) = Ctyp then
3640 Known_Non_Null_Operand_Seen := True;
3641 Insert_Action (Cnode,
3642 Make_Assignment_Statement (Loc,
3644 Make_Indexed_Component (Loc,
3645 Prefix => New_Occurrence_Of (Ent, Loc),
3646 Expressions => New_List (To_Ityp (Lo))),
3647 Expression => Operands (J)),
3648 Suppress => All_Checks);
3650 -- Array case, slice assignment, skipped when argument is fixed
3651 -- length and known to be null.
3653 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3656 Make_Assignment_Statement (Loc,
3660 New_Occurrence_Of (Ent, Loc),
3663 Low_Bound => To_Ityp (Lo),
3664 High_Bound => To_Ityp (Hi))),
3665 Expression => Operands (J));
3667 if Is_Fixed_Length (J) then
3668 Known_Non_Null_Operand_Seen := True;
3670 elsif not Known_Non_Null_Operand_Seen then
3672 -- Here if operand length is not statically known and no
3673 -- operand known to be non-null has been processed yet.
3674 -- If operand length is 0, we do not need to perform the
3675 -- assignment, and we must avoid the evaluation of the
3676 -- high bound of the slice, since it may underflow if the
3677 -- low bound is Ityp'First.
3680 Make_Implicit_If_Statement (Cnode,
3684 New_Occurrence_Of (Var_Length (J), Loc),
3685 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3686 Then_Statements => New_List (Assign));
3689 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3695 -- Finally we build the result, which is a reference to the array object
3697 Result := New_Occurrence_Of (Ent, Loc);
3700 Rewrite (Cnode, Result);
3701 Analyze_And_Resolve (Cnode, Atyp);
3704 when Concatenation_Error =>
3706 -- Kill warning generated for the declaration of the static out of
3707 -- range high bound, and instead generate a Constraint_Error with
3708 -- an appropriate specific message.
3710 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3711 Apply_Compile_Time_Constraint_Error
3713 Msg => "concatenation result upper bound out of range??",
3714 Reason => CE_Range_Check_Failed);
3715 end Expand_Concatenate;
3717 ---------------------------------------------------
3718 -- Expand_Membership_Minimize_Eliminate_Overflow --
3719 ---------------------------------------------------
3721 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is
3722 pragma Assert (Nkind (N) = N_In);
3723 -- Despite the name, this routine applies only to N_In, not to
3724 -- N_Not_In. The latter is always rewritten as not (X in Y).
3726 Result_Type : constant Entity_Id := Etype (N);
3727 -- Capture result type, may be a derived boolean type
3729 Loc : constant Source_Ptr := Sloc (N);
3730 Lop : constant Node_Id := Left_Opnd (N);
3731 Rop : constant Node_Id := Right_Opnd (N);
3733 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3734 -- is thus tempting to capture these values, but due to the rewrites
3735 -- that occur as a result of overflow checking, these values change
3736 -- as we go along, and it is safe just to always use Etype explicitly.
3738 Restype : constant Entity_Id := Etype (N);
3742 -- Bounds in Minimize calls, not used currently
3744 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
3745 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3748 Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False);
3750 -- If right operand is a subtype name, and the subtype name has no
3751 -- predicate, then we can just replace the right operand with an
3752 -- explicit range T'First .. T'Last, and use the explicit range code.
3754 if Nkind (Rop) /= N_Range
3755 and then No (Predicate_Function (Etype (Rop)))
3758 Rtyp : constant Entity_Id := Etype (Rop);
3763 Make_Attribute_Reference (Loc,
3764 Attribute_Name => Name_First,
3765 Prefix => New_Occurrence_Of (Rtyp, Loc)),
3767 Make_Attribute_Reference (Loc,
3768 Attribute_Name => Name_Last,
3769 Prefix => New_Occurrence_Of (Rtyp, Loc))));
3770 Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks);
3774 -- Here for the explicit range case. Note that the bounds of the range
3775 -- have not been processed for minimized or eliminated checks.
3777 if Nkind (Rop) = N_Range then
3778 Minimize_Eliminate_Overflows
3779 (Low_Bound (Rop), Lo, Hi, Top_Level => False);
3780 Minimize_Eliminate_Overflows
3781 (High_Bound (Rop), Lo, Hi, Top_Level => False);
3783 -- We have A in B .. C, treated as A >= B and then A <= C
3787 if Is_RTE (Etype (Lop), RE_Bignum)
3788 or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum)
3789 or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum)
3792 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3793 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3794 L : constant Entity_Id :=
3795 Make_Defining_Identifier (Loc, Name_uL);
3796 Lopnd : constant Node_Id := Convert_To_Bignum (Lop);
3797 Lbound : constant Node_Id :=
3798 Convert_To_Bignum (Low_Bound (Rop));
3799 Hbound : constant Node_Id :=
3800 Convert_To_Bignum (High_Bound (Rop));
3802 -- Now we rewrite the membership test node to look like
3805 -- Bnn : Result_Type;
3807 -- M : Mark_Id := SS_Mark;
3808 -- L : Bignum := Lopnd;
3810 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3818 -- Insert declaration of L into declarations of bignum block
3821 (Last (Declarations (Blk)),
3822 Make_Object_Declaration (Loc,
3823 Defining_Identifier => L,
3824 Object_Definition =>
3825 New_Occurrence_Of (RTE (RE_Bignum), Loc),
3826 Expression => Lopnd));
3828 -- Insert assignment to Bnn into expressions of bignum block
3831 (First (Statements (Handled_Statement_Sequence (Blk))),
3832 Make_Assignment_Statement (Loc,
3833 Name => New_Occurrence_Of (Bnn, Loc),
3837 Make_Function_Call (Loc,
3839 New_Occurrence_Of (RTE (RE_Big_GE), Loc),
3840 Parameter_Associations => New_List (
3841 New_Occurrence_Of (L, Loc),
3845 Make_Function_Call (Loc,
3847 New_Occurrence_Of (RTE (RE_Big_LE), Loc),
3848 Parameter_Associations => New_List (
3849 New_Occurrence_Of (L, Loc),
3852 -- Now rewrite the node
3855 Make_Expression_With_Actions (Loc,
3856 Actions => New_List (
3857 Make_Object_Declaration (Loc,
3858 Defining_Identifier => Bnn,
3859 Object_Definition =>
3860 New_Occurrence_Of (Result_Type, Loc)),
3862 Expression => New_Occurrence_Of (Bnn, Loc)));
3863 Analyze_And_Resolve (N, Result_Type);
3867 -- Here if no bignums around
3870 -- Case where types are all the same
3872 if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop)))
3874 Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop)))
3878 -- If types are not all the same, it means that we have rewritten
3879 -- at least one of them to be of type Long_Long_Integer, and we
3880 -- will convert the other operands to Long_Long_Integer.
3883 Convert_To_And_Rewrite (LLIB, Lop);
3884 Set_Analyzed (Lop, False);
3885 Analyze_And_Resolve (Lop, LLIB);
3887 -- For the right operand, avoid unnecessary recursion into
3888 -- this routine, we know that overflow is not possible.
3890 Convert_To_And_Rewrite (LLIB, Low_Bound (Rop));
3891 Convert_To_And_Rewrite (LLIB, High_Bound (Rop));
3892 Set_Analyzed (Rop, False);
3893 Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check);
3896 -- Now the three operands are of the same signed integer type,
3897 -- so we can use the normal expansion routine for membership,
3898 -- setting the flag to prevent recursion into this procedure.
3900 Set_No_Minimize_Eliminate (N);
3904 -- Right operand is a subtype name and the subtype has a predicate. We
3905 -- have to make sure the predicate is checked, and for that we need to
3906 -- use the standard N_In circuitry with appropriate types.
3909 pragma Assert (Present (Predicate_Function (Etype (Rop))));
3911 -- If types are "right", just call Expand_N_In preventing recursion
3913 if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then
3914 Set_No_Minimize_Eliminate (N);
3919 elsif Is_RTE (Etype (Lop), RE_Bignum) then
3921 -- For X in T, we want to rewrite our node as
3924 -- Bnn : Result_Type;
3927 -- M : Mark_Id := SS_Mark;
3928 -- Lnn : Long_Long_Integer'Base
3934 -- if not Bignum_In_LLI_Range (Nnn) then
3937 -- Lnn := From_Bignum (Nnn);
3939 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3940 -- and then T'Base (Lnn) in T;
3949 -- A bit gruesome, but there doesn't seem to be a simpler way
3952 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3953 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3954 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N);
3955 Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N);
3956 T : constant Entity_Id := Etype (Rop);
3957 TB : constant Entity_Id := Base_Type (T);
3961 -- Mark the last membership operation to prevent recursion
3965 Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)),
3966 Right_Opnd => New_Occurrence_Of (T, Loc));
3967 Set_No_Minimize_Eliminate (Nin);
3969 -- Now decorate the block
3972 (Last (Declarations (Blk)),
3973 Make_Object_Declaration (Loc,
3974 Defining_Identifier => Lnn,
3975 Object_Definition => New_Occurrence_Of (LLIB, Loc)));
3978 (Last (Declarations (Blk)),
3979 Make_Object_Declaration (Loc,
3980 Defining_Identifier => Nnn,
3981 Object_Definition =>
3982 New_Occurrence_Of (RTE (RE_Bignum), Loc)));
3985 (First (Statements (Handled_Statement_Sequence (Blk))),
3987 Make_Assignment_Statement (Loc,
3988 Name => New_Occurrence_Of (Nnn, Loc),
3989 Expression => Relocate_Node (Lop)),
3991 Make_Implicit_If_Statement (N,
3995 Make_Function_Call (Loc,
3998 (RTE (RE_Bignum_In_LLI_Range), Loc),
3999 Parameter_Associations => New_List (
4000 New_Occurrence_Of (Nnn, Loc)))),
4002 Then_Statements => New_List (
4003 Make_Assignment_Statement (Loc,
4004 Name => New_Occurrence_Of (Bnn, Loc),
4006 New_Occurrence_Of (Standard_False, Loc))),
4008 Else_Statements => New_List (
4009 Make_Assignment_Statement (Loc,
4010 Name => New_Occurrence_Of (Lnn, Loc),
4012 Make_Function_Call (Loc,
4014 New_Occurrence_Of (RTE (RE_From_Bignum), Loc),
4015 Parameter_Associations => New_List (
4016 New_Occurrence_Of (Nnn, Loc)))),
4018 Make_Assignment_Statement (Loc,
4019 Name => New_Occurrence_Of (Bnn, Loc),
4024 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4029 Make_Attribute_Reference (Loc,
4030 Attribute_Name => Name_First,
4032 New_Occurrence_Of (TB, Loc))),
4036 Make_Attribute_Reference (Loc,
4037 Attribute_Name => Name_Last,
4039 New_Occurrence_Of (TB, Loc))))),
4041 Right_Opnd => Nin))))));
4043 -- Now we can do the rewrite
4046 Make_Expression_With_Actions (Loc,
4047 Actions => New_List (
4048 Make_Object_Declaration (Loc,
4049 Defining_Identifier => Bnn,
4050 Object_Definition =>
4051 New_Occurrence_Of (Result_Type, Loc)),
4053 Expression => New_Occurrence_Of (Bnn, Loc)));
4054 Analyze_And_Resolve (N, Result_Type);
4058 -- Not bignum case, but types don't match (this means we rewrote the
4059 -- left operand to be Long_Long_Integer).
4062 pragma Assert (Base_Type (Etype (Lop)) = LLIB);
4064 -- We rewrite the membership test as (where T is the type with
4065 -- the predicate, i.e. the type of the right operand)
4067 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
4068 -- and then T'Base (Lop) in T
4071 T : constant Entity_Id := Etype (Rop);
4072 TB : constant Entity_Id := Base_Type (T);
4076 -- The last membership test is marked to prevent recursion
4080 Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)),
4081 Right_Opnd => New_Occurrence_Of (T, Loc));
4082 Set_No_Minimize_Eliminate (Nin);
4084 -- Now do the rewrite
4095 Make_Attribute_Reference (Loc,
4096 Attribute_Name => Name_First,
4098 New_Occurrence_Of (TB, Loc))),
4101 Make_Attribute_Reference (Loc,
4102 Attribute_Name => Name_Last,
4104 New_Occurrence_Of (TB, Loc))))),
4105 Right_Opnd => Nin));
4106 Set_Analyzed (N, False);
4107 Analyze_And_Resolve (N, Restype);
4111 end Expand_Membership_Minimize_Eliminate_Overflow;
4113 ------------------------
4114 -- Expand_N_Allocator --
4115 ------------------------
4117 procedure Expand_N_Allocator (N : Node_Id) is
4118 Etyp : constant Entity_Id := Etype (Expression (N));
4119 Loc : constant Source_Ptr := Sloc (N);
4120 PtrT : constant Entity_Id := Etype (N);
4122 procedure Rewrite_Coextension (N : Node_Id);
4123 -- Static coextensions have the same lifetime as the entity they
4124 -- constrain. Such occurrences can be rewritten as aliased objects
4125 -- and their unrestricted access used instead of the coextension.
4127 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
4128 -- Given a constrained array type E, returns a node representing the
4129 -- code to compute the size in storage elements for the given type.
4130 -- This is done without using the attribute (which malfunctions for
4133 -------------------------
4134 -- Rewrite_Coextension --
4135 -------------------------
4137 procedure Rewrite_Coextension (N : Node_Id) is
4138 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
4139 Temp_Decl : Node_Id;
4143 -- Cnn : aliased Etyp;
4146 Make_Object_Declaration (Loc,
4147 Defining_Identifier => Temp_Id,
4148 Aliased_Present => True,
4149 Object_Definition => New_Occurrence_Of (Etyp, Loc));
4151 if Nkind (Expression (N)) = N_Qualified_Expression then
4152 Set_Expression (Temp_Decl, Expression (Expression (N)));
4155 Insert_Action (N, Temp_Decl);
4157 Make_Attribute_Reference (Loc,
4158 Prefix => New_Occurrence_Of (Temp_Id, Loc),
4159 Attribute_Name => Name_Unrestricted_Access));
4161 Analyze_And_Resolve (N, PtrT);
4162 end Rewrite_Coextension;
4164 ------------------------------
4165 -- Size_In_Storage_Elements --
4166 ------------------------------
4168 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
4170 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4171 -- However, the reason for the existence of this function is
4172 -- to construct a test for sizes too large, which means near the
4173 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4174 -- is that we get overflows when sizes are greater than 2**31.
4176 -- So what we end up doing for array types is to use the expression:
4178 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4180 -- which avoids this problem. All this is a bit bogus, but it does
4181 -- mean we catch common cases of trying to allocate arrays that
4182 -- are too large, and which in the absence of a check results in
4183 -- undetected chaos ???
4185 -- Note in particular that this is a pessimistic estimate in the
4186 -- case of packed array types, where an array element might occupy
4187 -- just a fraction of a storage element???
4194 for J in 1 .. Number_Dimensions (E) loop
4196 Make_Attribute_Reference (Loc,
4197 Prefix => New_Occurrence_Of (E, Loc),
4198 Attribute_Name => Name_Length,
4199 Expressions => New_List (Make_Integer_Literal (Loc, J)));
4206 Make_Op_Multiply (Loc,
4213 Make_Op_Multiply (Loc,
4216 Make_Attribute_Reference (Loc,
4217 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
4218 Attribute_Name => Name_Max_Size_In_Storage_Elements));
4220 end Size_In_Storage_Elements;
4224 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
4228 Rel_Typ : Entity_Id;
4231 -- Start of processing for Expand_N_Allocator
4234 -- RM E.2.3(22). We enforce that the expected type of an allocator
4235 -- shall not be a remote access-to-class-wide-limited-private type
4237 -- Why is this being done at expansion time, seems clearly wrong ???
4239 Validate_Remote_Access_To_Class_Wide_Type (N);
4241 -- Processing for anonymous access-to-controlled types. These access
4242 -- types receive a special finalization master which appears in the
4243 -- declarations of the enclosing semantic unit. This expansion is done
4244 -- now to ensure that any additional types generated by this routine or
4245 -- Expand_Allocator_Expression inherit the proper type attributes.
4247 if (Ekind (PtrT) = E_Anonymous_Access_Type
4248 or else (Is_Itype (PtrT) and then No (Finalization_Master (PtrT))))
4249 and then Needs_Finalization (Dtyp)
4251 -- Detect the allocation of an anonymous controlled object where the
4252 -- type of the context is named. For example:
4254 -- procedure Proc (Ptr : Named_Access_Typ);
4255 -- Proc (new Designated_Typ);
4257 -- Regardless of the anonymous-to-named access type conversion, the
4258 -- lifetime of the object must be associated with the named access
4259 -- type. Use the finalization-related attributes of this type.
4261 if Nkind_In (Parent (N), N_Type_Conversion,
4262 N_Unchecked_Type_Conversion)
4263 and then Ekind_In (Etype (Parent (N)), E_Access_Subtype,
4265 E_General_Access_Type)
4267 Rel_Typ := Etype (Parent (N));
4272 -- Anonymous access-to-controlled types allocate on the global pool.
4273 -- Note that this is a "root type only" attribute.
4275 if No (Associated_Storage_Pool (PtrT)) then
4276 if Present (Rel_Typ) then
4277 Set_Associated_Storage_Pool
4278 (Root_Type (PtrT), Associated_Storage_Pool (Rel_Typ));
4280 Set_Associated_Storage_Pool
4281 (Root_Type (PtrT), RTE (RE_Global_Pool_Object));
4285 -- The finalization master must be inserted and analyzed as part of
4286 -- the current semantic unit. Note that the master is updated when
4287 -- analysis changes current units. Note that this is a "root type
4290 if Present (Rel_Typ) then
4291 Set_Finalization_Master
4292 (Root_Type (PtrT), Finalization_Master (Rel_Typ));
4294 Set_Finalization_Master
4295 (Root_Type (PtrT), Current_Anonymous_Master);
4299 -- Set the storage pool and find the appropriate version of Allocate to
4300 -- call. Do not overwrite the storage pool if it is already set, which
4301 -- can happen for build-in-place function returns (see
4302 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4304 if No (Storage_Pool (N)) then
4305 Pool := Associated_Storage_Pool (Root_Type (PtrT));
4307 if Present (Pool) then
4308 Set_Storage_Pool (N, Pool);
4310 if Is_RTE (Pool, RE_SS_Pool) then
4311 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
4313 -- In the case of an allocator for a simple storage pool, locate
4314 -- and save a reference to the pool type's Allocate routine.
4316 elsif Present (Get_Rep_Pragma
4317 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4320 Pool_Type : constant Entity_Id := Base_Type (Etype (Pool));
4321 Alloc_Op : Entity_Id;
4323 Alloc_Op := Get_Name_Entity_Id (Name_Allocate);
4324 while Present (Alloc_Op) loop
4325 if Scope (Alloc_Op) = Scope (Pool_Type)
4326 and then Present (First_Formal (Alloc_Op))
4327 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
4329 Set_Procedure_To_Call (N, Alloc_Op);
4332 Alloc_Op := Homonym (Alloc_Op);
4337 elsif Is_Class_Wide_Type (Etype (Pool)) then
4338 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
4341 Set_Procedure_To_Call (N,
4342 Find_Prim_Op (Etype (Pool), Name_Allocate));
4347 -- Under certain circumstances we can replace an allocator by an access
4348 -- to statically allocated storage. The conditions, as noted in AARM
4349 -- 3.10 (10c) are as follows:
4351 -- Size and initial value is known at compile time
4352 -- Access type is access-to-constant
4354 -- The allocator is not part of a constraint on a record component,
4355 -- because in that case the inserted actions are delayed until the
4356 -- record declaration is fully analyzed, which is too late for the
4357 -- analysis of the rewritten allocator.
4359 if Is_Access_Constant (PtrT)
4360 and then Nkind (Expression (N)) = N_Qualified_Expression
4361 and then Compile_Time_Known_Value (Expression (Expression (N)))
4362 and then Size_Known_At_Compile_Time
4363 (Etype (Expression (Expression (N))))
4364 and then not Is_Record_Type (Current_Scope)
4366 -- Here we can do the optimization. For the allocator
4370 -- We insert an object declaration
4372 -- Tnn : aliased x := y;
4374 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4375 -- marked as requiring static allocation.
4377 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
4378 Desig := Subtype_Mark (Expression (N));
4380 -- If context is constrained, use constrained subtype directly,
4381 -- so that the constant is not labelled as having a nominally
4382 -- unconstrained subtype.
4384 if Entity (Desig) = Base_Type (Dtyp) then
4385 Desig := New_Occurrence_Of (Dtyp, Loc);
4389 Make_Object_Declaration (Loc,
4390 Defining_Identifier => Temp,
4391 Aliased_Present => True,
4392 Constant_Present => Is_Access_Constant (PtrT),
4393 Object_Definition => Desig,
4394 Expression => Expression (Expression (N))));
4397 Make_Attribute_Reference (Loc,
4398 Prefix => New_Occurrence_Of (Temp, Loc),
4399 Attribute_Name => Name_Unrestricted_Access));
4401 Analyze_And_Resolve (N, PtrT);
4403 -- We set the variable as statically allocated, since we don't want
4404 -- it going on the stack of the current procedure.
4406 Set_Is_Statically_Allocated (Temp);
4410 -- Same if the allocator is an access discriminant for a local object:
4411 -- instead of an allocator we create a local value and constrain the
4412 -- enclosing object with the corresponding access attribute.
4414 if Is_Static_Coextension (N) then
4415 Rewrite_Coextension (N);
4419 -- Check for size too large, we do this because the back end misses
4420 -- proper checks here and can generate rubbish allocation calls when
4421 -- we are near the limit. We only do this for the 32-bit address case
4422 -- since that is from a practical point of view where we see a problem.
4424 if System_Address_Size = 32
4425 and then not Storage_Checks_Suppressed (PtrT)
4426 and then not Storage_Checks_Suppressed (Dtyp)
4427 and then not Storage_Checks_Suppressed (Etyp)
4429 -- The check we want to generate should look like
4431 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4432 -- raise Storage_Error;
4435 -- where 3.5 gigabytes is a constant large enough to accommodate any
4436 -- reasonable request for. But we can't do it this way because at
4437 -- least at the moment we don't compute this attribute right, and
4438 -- can silently give wrong results when the result gets large. Since
4439 -- this is all about large results, that's bad, so instead we only
4440 -- apply the check for constrained arrays, and manually compute the
4441 -- value of the attribute ???
4443 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
4445 Make_Raise_Storage_Error (Loc,
4448 Left_Opnd => Size_In_Storage_Elements (Etyp),
4450 Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))),
4451 Reason => SE_Object_Too_Large));
4455 -- If no storage pool has been specified and we have the restriction
4456 -- No_Standard_Allocators_After_Elaboration is present, then generate
4457 -- a call to Elaboration_Allocators.Check_Standard_Allocator.
4459 if Nkind (N) = N_Allocator
4460 and then No (Storage_Pool (N))
4461 and then Restriction_Active (No_Standard_Allocators_After_Elaboration)
4464 Make_Procedure_Call_Statement (Loc,
4466 New_Occurrence_Of (RTE (RE_Check_Standard_Allocator), Loc)));
4469 -- Handle case of qualified expression (other than optimization above)
4470 -- First apply constraint checks, because the bounds or discriminants
4471 -- in the aggregate might not match the subtype mark in the allocator.
4473 if Nkind (Expression (N)) = N_Qualified_Expression then
4474 Apply_Constraint_Check
4475 (Expression (Expression (N)), Etype (Expression (N)));
4477 Expand_Allocator_Expression (N);
4481 -- If the allocator is for a type which requires initialization, and
4482 -- there is no initial value (i.e. operand is a subtype indication
4483 -- rather than a qualified expression), then we must generate a call to
4484 -- the initialization routine using an expressions action node:
4486 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4488 -- Here ptr_T is the pointer type for the allocator, and T is the
4489 -- subtype of the allocator. A special case arises if the designated
4490 -- type of the access type is a task or contains tasks. In this case
4491 -- the call to Init (Temp.all ...) is replaced by code that ensures
4492 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4493 -- for details). In addition, if the type T is a task type, then the
4494 -- first argument to Init must be converted to the task record type.
4497 T : constant Entity_Id := Entity (Expression (N));
4503 Init_Arg1 : Node_Id;
4504 Temp_Decl : Node_Id;
4505 Temp_Type : Entity_Id;
4508 if No_Initialization (N) then
4510 -- Even though this might be a simple allocation, create a custom
4511 -- Allocate if the context requires it.
4513 if Present (Finalization_Master (PtrT)) then
4514 Build_Allocate_Deallocate_Proc
4516 Is_Allocate => True);
4519 -- Case of no initialization procedure present
4521 elsif not Has_Non_Null_Base_Init_Proc (T) then
4523 -- Case of simple initialization required
4525 if Needs_Simple_Initialization (T) then
4526 Check_Restriction (No_Default_Initialization, N);
4527 Rewrite (Expression (N),
4528 Make_Qualified_Expression (Loc,
4529 Subtype_Mark => New_Occurrence_Of (T, Loc),
4530 Expression => Get_Simple_Init_Val (T, N)));
4532 Analyze_And_Resolve (Expression (Expression (N)), T);
4533 Analyze_And_Resolve (Expression (N), T);
4534 Set_Paren_Count (Expression (Expression (N)), 1);
4535 Expand_N_Allocator (N);
4537 -- No initialization required
4543 -- Case of initialization procedure present, must be called
4546 Check_Restriction (No_Default_Initialization, N);
4548 if not Restriction_Active (No_Default_Initialization) then
4549 Init := Base_Init_Proc (T);
4551 Temp := Make_Temporary (Loc, 'P');
4553 -- Construct argument list for the initialization routine call
4556 Make_Explicit_Dereference (Loc,
4558 New_Occurrence_Of (Temp, Loc));
4560 Set_Assignment_OK (Init_Arg1);
4563 -- The initialization procedure expects a specific type. if the
4564 -- context is access to class wide, indicate that the object
4565 -- being allocated has the right specific type.
4567 if Is_Class_Wide_Type (Dtyp) then
4568 Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1);
4571 -- If designated type is a concurrent type or if it is private
4572 -- type whose definition is a concurrent type, the first
4573 -- argument in the Init routine has to be unchecked conversion
4574 -- to the corresponding record type. If the designated type is
4575 -- a derived type, also convert the argument to its root type.
4577 if Is_Concurrent_Type (T) then
4579 Unchecked_Convert_To (
4580 Corresponding_Record_Type (T), Init_Arg1);
4582 elsif Is_Private_Type (T)
4583 and then Present (Full_View (T))
4584 and then Is_Concurrent_Type (Full_View (T))
4587 Unchecked_Convert_To
4588 (Corresponding_Record_Type (Full_View (T)), Init_Arg1);
4590 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
4592 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
4595 Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1);
4596 Set_Etype (Init_Arg1, Ftyp);
4600 Args := New_List (Init_Arg1);
4602 -- For the task case, pass the Master_Id of the access type as
4603 -- the value of the _Master parameter, and _Chain as the value
4604 -- of the _Chain parameter (_Chain will be defined as part of
4605 -- the generated code for the allocator).
4607 -- In Ada 2005, the context may be a function that returns an
4608 -- anonymous access type. In that case the Master_Id has been
4609 -- created when expanding the function declaration.
4611 if Has_Task (T) then
4612 if No (Master_Id (Base_Type (PtrT))) then
4614 -- The designated type was an incomplete type, and the
4615 -- access type did not get expanded. Salvage it now.
4617 if not Restriction_Active (No_Task_Hierarchy) then
4618 if Present (Parent (Base_Type (PtrT))) then
4619 Expand_N_Full_Type_Declaration
4620 (Parent (Base_Type (PtrT)));
4622 -- The only other possibility is an itype. For this
4623 -- case, the master must exist in the context. This is
4624 -- the case when the allocator initializes an access
4625 -- component in an init-proc.
4628 pragma Assert (Is_Itype (PtrT));
4629 Build_Master_Renaming (PtrT, N);
4634 -- If the context of the allocator is a declaration or an
4635 -- assignment, we can generate a meaningful image for it,
4636 -- even though subsequent assignments might remove the
4637 -- connection between task and entity. We build this image
4638 -- when the left-hand side is a simple variable, a simple
4639 -- indexed assignment or a simple selected component.
4641 if Nkind (Parent (N)) = N_Assignment_Statement then
4643 Nam : constant Node_Id := Name (Parent (N));
4646 if Is_Entity_Name (Nam) then
4648 Build_Task_Image_Decls
4651 (Entity (Nam), Sloc (Nam)), T);
4653 elsif Nkind_In (Nam, N_Indexed_Component,
4654 N_Selected_Component)
4655 and then Is_Entity_Name (Prefix (Nam))
4658 Build_Task_Image_Decls
4659 (Loc, Nam, Etype (Prefix (Nam)));
4661 Decls := Build_Task_Image_Decls (Loc, T, T);
4665 elsif Nkind (Parent (N)) = N_Object_Declaration then
4667 Build_Task_Image_Decls
4668 (Loc, Defining_Identifier (Parent (N)), T);
4671 Decls := Build_Task_Image_Decls (Loc, T, T);
4674 if Restriction_Active (No_Task_Hierarchy) then
4676 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
4680 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
4683 Append_To (Args, Make_Identifier (Loc, Name_uChain));
4685 Decl := Last (Decls);
4687 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
4689 -- Has_Task is false, Decls not used
4695 -- Add discriminants if discriminated type
4698 Dis : Boolean := False;
4702 if Has_Discriminants (T) then
4706 elsif Is_Private_Type (T)
4707 and then Present (Full_View (T))
4708 and then Has_Discriminants (Full_View (T))
4711 Typ := Full_View (T);
4716 -- If the allocated object will be constrained by the
4717 -- default values for discriminants, then build a subtype
4718 -- with those defaults, and change the allocated subtype
4719 -- to that. Note that this happens in fewer cases in Ada
4722 if not Is_Constrained (Typ)
4723 and then Present (Discriminant_Default_Value
4724 (First_Discriminant (Typ)))
4725 and then (Ada_Version < Ada_2005
4727 Object_Type_Has_Constrained_Partial_View
4728 (Typ, Current_Scope))
4730 Typ := Build_Default_Subtype (Typ, N);
4731 Set_Expression (N, New_Occurrence_Of (Typ, Loc));
4734 Discr := First_Elmt (Discriminant_Constraint (Typ));
4735 while Present (Discr) loop
4736 Nod := Node (Discr);
4737 Append (New_Copy_Tree (Node (Discr)), Args);
4739 -- AI-416: when the discriminant constraint is an
4740 -- anonymous access type make sure an accessibility
4741 -- check is inserted if necessary (3.10.2(22.q/2))
4743 if Ada_Version >= Ada_2005
4745 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
4747 Apply_Accessibility_Check
4748 (Nod, Typ, Insert_Node => Nod);
4756 -- We set the allocator as analyzed so that when we analyze
4757 -- the if expression node, we do not get an unwanted recursive
4758 -- expansion of the allocator expression.
4760 Set_Analyzed (N, True);
4761 Nod := Relocate_Node (N);
4763 -- Here is the transformation:
4764 -- input: new Ctrl_Typ
4765 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4766 -- Ctrl_TypIP (Temp.all, ...);
4767 -- [Deep_]Initialize (Temp.all);
4769 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4770 -- is the subtype of the allocator.
4773 Make_Object_Declaration (Loc,
4774 Defining_Identifier => Temp,
4775 Constant_Present => True,
4776 Object_Definition => New_Occurrence_Of (Temp_Type, Loc),
4779 Set_Assignment_OK (Temp_Decl);
4780 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
4782 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
4784 -- If the designated type is a task type or contains tasks,
4785 -- create block to activate created tasks, and insert
4786 -- declaration for Task_Image variable ahead of call.
4788 if Has_Task (T) then
4790 L : constant List_Id := New_List;
4793 Build_Task_Allocate_Block (L, Nod, Args);
4795 Insert_List_Before (First (Declarations (Blk)), Decls);
4796 Insert_Actions (N, L);
4801 Make_Procedure_Call_Statement (Loc,
4802 Name => New_Occurrence_Of (Init, Loc),
4803 Parameter_Associations => Args));
4806 if Needs_Finalization (T) then
4809 -- [Deep_]Initialize (Init_Arg1);
4813 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4817 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4818 Analyze_And_Resolve (N, PtrT);
4823 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
4824 -- object that has been rewritten as a reference, we displace "this"
4825 -- to reference properly its secondary dispatch table.
4827 if Nkind (N) = N_Identifier and then Is_Interface (Dtyp) then
4828 Displace_Allocator_Pointer (N);
4832 when RE_Not_Available =>
4834 end Expand_N_Allocator;
4836 -----------------------
4837 -- Expand_N_And_Then --
4838 -----------------------
4840 procedure Expand_N_And_Then (N : Node_Id)
4841 renames Expand_Short_Circuit_Operator;
4843 ------------------------------
4844 -- Expand_N_Case_Expression --
4845 ------------------------------
4847 procedure Expand_N_Case_Expression (N : Node_Id) is
4848 Loc : constant Source_Ptr := Sloc (N);
4849 Typ : constant Entity_Id := Etype (N);
4860 -- Check for MINIMIZED/ELIMINATED overflow mode
4862 if Minimized_Eliminated_Overflow_Check (N) then
4863 Apply_Arithmetic_Overflow_Check (N);
4867 -- If the case expression is a predicate specification, and the type
4868 -- to which it applies has a static predicate aspect, do not expand,
4869 -- because it will be converted to the proper predicate form later.
4871 if Ekind_In (Current_Scope, E_Function, E_Procedure)
4872 and then Is_Predicate_Function (Current_Scope)
4874 Has_Static_Predicate_Aspect (Etype (First_Entity (Current_Scope)))
4881 -- case X is when A => AX, when B => BX ...
4896 -- However, this expansion is wrong for limited types, and also
4897 -- wrong for unconstrained types (since the bounds may not be the
4898 -- same in all branches). Furthermore it involves an extra copy
4899 -- for large objects. So we take care of this by using the following
4900 -- modified expansion for non-elementary types:
4903 -- type Pnn is access all typ;
4907 -- T := AX'Unrestricted_Access;
4909 -- T := BX'Unrestricted_Access;
4915 Make_Case_Statement (Loc,
4916 Expression => Expression (N),
4917 Alternatives => New_List);
4919 -- Preserve the original context for which the case statement is being
4920 -- generated. This is needed by the finalization machinery to prevent
4921 -- the premature finalization of controlled objects found within the
4924 Set_From_Conditional_Expression (Cstmt);
4926 Actions := New_List;
4930 if Is_Elementary_Type (Typ) then
4934 Pnn := Make_Temporary (Loc, 'P');
4936 Make_Full_Type_Declaration (Loc,
4937 Defining_Identifier => Pnn,
4939 Make_Access_To_Object_Definition (Loc,
4940 All_Present => True,
4941 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
4945 Tnn := Make_Temporary (Loc, 'T');
4947 -- Create declaration for target of expression, and indicate that it
4948 -- does not require initialization.
4951 Make_Object_Declaration (Loc,
4952 Defining_Identifier => Tnn,
4953 Object_Definition => New_Occurrence_Of (Ttyp, Loc));
4954 Set_No_Initialization (Decl);
4955 Append_To (Actions, Decl);
4957 -- Now process the alternatives
4959 Alt := First (Alternatives (N));
4960 while Present (Alt) loop
4962 Aexp : Node_Id := Expression (Alt);
4963 Aloc : constant Source_Ptr := Sloc (Aexp);
4967 -- As described above, take Unrestricted_Access for case of non-
4968 -- scalar types, to avoid big copies, and special cases.
4970 if not Is_Elementary_Type (Typ) then
4972 Make_Attribute_Reference (Aloc,
4973 Prefix => Relocate_Node (Aexp),
4974 Attribute_Name => Name_Unrestricted_Access);
4978 Make_Assignment_Statement (Aloc,
4979 Name => New_Occurrence_Of (Tnn, Loc),
4980 Expression => Aexp));
4982 -- Propagate declarations inserted in the node by Insert_Actions
4983 -- (for example, temporaries generated to remove side effects).
4984 -- These actions must remain attached to the alternative, given
4985 -- that they are generated by the corresponding expression.
4987 if Present (Sinfo.Actions (Alt)) then
4988 Prepend_List (Sinfo.Actions (Alt), Stats);
4992 (Alternatives (Cstmt),
4993 Make_Case_Statement_Alternative (Sloc (Alt),
4994 Discrete_Choices => Discrete_Choices (Alt),
4995 Statements => Stats));
5001 Append_To (Actions, Cstmt);
5003 -- Construct and return final expression with actions
5005 if Is_Elementary_Type (Typ) then
5006 Fexp := New_Occurrence_Of (Tnn, Loc);
5009 Make_Explicit_Dereference (Loc,
5010 Prefix => New_Occurrence_Of (Tnn, Loc));
5014 Make_Expression_With_Actions (Loc,
5016 Actions => Actions));
5018 Analyze_And_Resolve (N, Typ);
5019 end Expand_N_Case_Expression;
5021 -----------------------------------
5022 -- Expand_N_Explicit_Dereference --
5023 -----------------------------------
5025 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
5027 -- Insert explicit dereference call for the checked storage pool case
5029 Insert_Dereference_Action (Prefix (N));
5031 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5032 -- we set the atomic sync flag.
5034 if Is_Atomic (Etype (N))
5035 and then not Atomic_Synchronization_Disabled (Etype (N))
5037 Activate_Atomic_Synchronization (N);
5039 end Expand_N_Explicit_Dereference;
5041 --------------------------------------
5042 -- Expand_N_Expression_With_Actions --
5043 --------------------------------------
5045 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
5046 Acts : constant List_Id := Actions (N);
5048 procedure Force_Boolean_Evaluation (Expr : Node_Id);
5049 -- Force the evaluation of Boolean expression Expr
5051 function Process_Action (Act : Node_Id) return Traverse_Result;
5052 -- Inspect and process a single action of an expression_with_actions for
5053 -- transient controlled objects. If such objects are found, the routine
5054 -- generates code to clean them up when the context of the expression is
5055 -- evaluated or elaborated.
5057 ------------------------------
5058 -- Force_Boolean_Evaluation --
5059 ------------------------------
5061 procedure Force_Boolean_Evaluation (Expr : Node_Id) is
5062 Loc : constant Source_Ptr := Sloc (N);
5063 Flag_Decl : Node_Id;
5064 Flag_Id : Entity_Id;
5067 -- Relocate the expression to the actions list by capturing its value
5068 -- in a Boolean flag. Generate:
5069 -- Flag : constant Boolean := Expr;
5071 Flag_Id := Make_Temporary (Loc, 'F');
5074 Make_Object_Declaration (Loc,
5075 Defining_Identifier => Flag_Id,
5076 Constant_Present => True,
5077 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
5078 Expression => Relocate_Node (Expr));
5080 Append (Flag_Decl, Acts);
5081 Analyze (Flag_Decl);
5083 -- Replace the expression with a reference to the flag
5085 Rewrite (Expression (N), New_Occurrence_Of (Flag_Id, Loc));
5086 Analyze (Expression (N));
5087 end Force_Boolean_Evaluation;
5089 --------------------
5090 -- Process_Action --
5091 --------------------
5093 function Process_Action (Act : Node_Id) return Traverse_Result is
5095 if Nkind (Act) = N_Object_Declaration
5096 and then Is_Finalizable_Transient (Act, N)
5098 Process_Transient_Object (Act, N);
5101 -- Avoid processing temporary function results multiple times when
5102 -- dealing with nested expression_with_actions.
5104 elsif Nkind (Act) = N_Expression_With_Actions then
5107 -- Do not process temporary function results in loops. This is done
5108 -- by Expand_N_Loop_Statement and Build_Finalizer.
5110 elsif Nkind (Act) = N_Loop_Statement then
5117 procedure Process_Single_Action is new Traverse_Proc (Process_Action);
5123 -- Start of processing for Expand_N_Expression_With_Actions
5126 -- Do not evaluate the expression when it denotes an entity because the
5127 -- expression_with_actions node will be replaced by the reference.
5129 if Is_Entity_Name (Expression (N)) then
5132 -- Do not evaluate the expression when there are no actions because the
5133 -- expression_with_actions node will be replaced by the expression.
5135 elsif No (Acts) or else Is_Empty_List (Acts) then
5138 -- Force the evaluation of the expression by capturing its value in a
5139 -- temporary. This ensures that aliases of transient controlled objects
5140 -- do not leak to the expression of the expression_with_actions node:
5143 -- Trans_Id : Ctrl_Typ : ...;
5144 -- Alias : ... := Trans_Id;
5145 -- in ... Alias ... end;
5147 -- In the example above, Trans_Id cannot be finalized at the end of the
5148 -- actions list because this may affect the alias and the final value of
5149 -- the expression_with_actions. Forcing the evaluation encapsulates the
5150 -- reference to the Alias within the actions list:
5153 -- Trans_Id : Ctrl_Typ : ...;
5154 -- Alias : ... := Trans_Id;
5155 -- Val : constant Boolean := ... Alias ...;
5156 -- <finalize Trans_Id>
5159 -- Once this transformation is performed, it is safe to finalize the
5160 -- transient controlled object at the end of the actions list.
5162 -- Note that Force_Evaluation does not remove side effects in operators
5163 -- because it assumes that all operands are evaluated and side effect
5164 -- free. This is not the case when an operand depends implicitly on the
5165 -- transient controlled object through the use of access types.
5167 elsif Is_Boolean_Type (Etype (Expression (N))) then
5168 Force_Boolean_Evaluation (Expression (N));
5170 -- The expression of an expression_with_actions node may not necessarily
5171 -- be Boolean when the node appears in an if expression. In this case do
5172 -- the usual forced evaluation to encapsulate potential aliasing.
5175 Force_Evaluation (Expression (N));
5178 -- Process all transient controlled objects found within the actions of
5181 Act := First (Acts);
5182 while Present (Act) loop
5183 Process_Single_Action (Act);
5187 -- Deal with case where there are no actions. In this case we simply
5188 -- rewrite the node with its expression since we don't need the actions
5189 -- and the specification of this node does not allow a null action list.
5191 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5192 -- the expanded tree and relying on being able to retrieve the original
5193 -- tree in cases like this. This raises a whole lot of issues of whether
5194 -- we have problems elsewhere, which will be addressed in the future???
5196 if Is_Empty_List (Acts) then
5197 Rewrite (N, Relocate_Node (Expression (N)));
5199 end Expand_N_Expression_With_Actions;
5201 ----------------------------
5202 -- Expand_N_If_Expression --
5203 ----------------------------
5205 -- Deal with limited types and condition actions
5207 procedure Expand_N_If_Expression (N : Node_Id) is
5208 procedure Process_Actions (Actions : List_Id);
5209 -- Inspect and process a single action list of an if expression for
5210 -- transient controlled objects. If such objects are found, the routine
5211 -- generates code to clean them up when the context of the expression is
5212 -- evaluated or elaborated.
5214 ---------------------
5215 -- Process_Actions --
5216 ---------------------
5218 procedure Process_Actions (Actions : List_Id) is
5222 Act := First (Actions);
5223 while Present (Act) loop
5224 if Nkind (Act) = N_Object_Declaration
5225 and then Is_Finalizable_Transient (Act, N)
5227 Process_Transient_Object (Act, N);
5232 end Process_Actions;
5236 Loc : constant Source_Ptr := Sloc (N);
5237 Cond : constant Node_Id := First (Expressions (N));
5238 Thenx : constant Node_Id := Next (Cond);
5239 Elsex : constant Node_Id := Next (Thenx);
5240 Typ : constant Entity_Id := Etype (N);
5248 Ptr_Typ : Entity_Id;
5250 -- Start of processing for Expand_N_If_Expression
5253 -- Check for MINIMIZED/ELIMINATED overflow mode
5255 if Minimized_Eliminated_Overflow_Check (N) then
5256 Apply_Arithmetic_Overflow_Check (N);
5260 -- Fold at compile time if condition known. We have already folded
5261 -- static if expressions, but it is possible to fold any case in which
5262 -- the condition is known at compile time, even though the result is
5265 -- Note that we don't do the fold of such cases in Sem_Elab because
5266 -- it can cause infinite loops with the expander adding a conditional
5267 -- expression, and Sem_Elab circuitry removing it repeatedly.
5269 if Compile_Time_Known_Value (Cond) then
5270 if Is_True (Expr_Value (Cond)) then
5272 Actions := Then_Actions (N);
5275 Actions := Else_Actions (N);
5280 if Present (Actions) then
5282 Make_Expression_With_Actions (Loc,
5283 Expression => Relocate_Node (Expr),
5284 Actions => Actions));
5285 Analyze_And_Resolve (N, Typ);
5287 Rewrite (N, Relocate_Node (Expr));
5290 -- Note that the result is never static (legitimate cases of static
5291 -- if expressions were folded in Sem_Eval).
5293 Set_Is_Static_Expression (N, False);
5297 -- If the type is limited, and the back end does not handle limited
5298 -- types, then we expand as follows to avoid the possibility of
5299 -- improper copying.
5301 -- type Ptr is access all Typ;
5305 -- Cnn := then-expr'Unrestricted_Access;
5308 -- Cnn := else-expr'Unrestricted_Access;
5311 -- and replace the if expression by a reference to Cnn.all.
5313 -- This special case can be skipped if the back end handles limited
5314 -- types properly and ensures that no incorrect copies are made.
5316 if Is_By_Reference_Type (Typ)
5317 and then not Back_End_Handles_Limited_Types
5319 -- When the "then" or "else" expressions involve controlled function
5320 -- calls, generated temporaries are chained on the corresponding list
5321 -- of actions. These temporaries need to be finalized after the if
5322 -- expression is evaluated.
5324 Process_Actions (Then_Actions (N));
5325 Process_Actions (Else_Actions (N));
5328 -- type Ann is access all Typ;
5330 Ptr_Typ := Make_Temporary (Loc, 'A');
5333 Make_Full_Type_Declaration (Loc,
5334 Defining_Identifier => Ptr_Typ,
5336 Make_Access_To_Object_Definition (Loc,
5337 All_Present => True,
5338 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
5343 Cnn := Make_Temporary (Loc, 'C', N);
5346 Make_Object_Declaration (Loc,
5347 Defining_Identifier => Cnn,
5348 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc));
5352 -- Cnn := <Thenx>'Unrestricted_Access;
5354 -- Cnn := <Elsex>'Unrestricted_Access;
5358 Make_Implicit_If_Statement (N,
5359 Condition => Relocate_Node (Cond),
5360 Then_Statements => New_List (
5361 Make_Assignment_Statement (Sloc (Thenx),
5362 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
5364 Make_Attribute_Reference (Loc,
5365 Prefix => Relocate_Node (Thenx),
5366 Attribute_Name => Name_Unrestricted_Access))),
5368 Else_Statements => New_List (
5369 Make_Assignment_Statement (Sloc (Elsex),
5370 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
5372 Make_Attribute_Reference (Loc,
5373 Prefix => Relocate_Node (Elsex),
5374 Attribute_Name => Name_Unrestricted_Access))));
5376 -- Preserve the original context for which the if statement is being
5377 -- generated. This is needed by the finalization machinery to prevent
5378 -- the premature finalization of controlled objects found within the
5381 Set_From_Conditional_Expression (New_If);
5384 Make_Explicit_Dereference (Loc,
5385 Prefix => New_Occurrence_Of (Cnn, Loc));
5387 -- If the result is an unconstrained array and the if expression is in a
5388 -- context other than the initializing expression of the declaration of
5389 -- an object, then we pull out the if expression as follows:
5391 -- Cnn : constant typ := if-expression
5393 -- and then replace the if expression with an occurrence of Cnn. This
5394 -- avoids the need in the back end to create on-the-fly variable length
5395 -- temporaries (which it cannot do!)
5397 -- Note that the test for being in an object declaration avoids doing an
5398 -- unnecessary expansion, and also avoids infinite recursion.
5400 elsif Is_Array_Type (Typ) and then not Is_Constrained (Typ)
5401 and then (Nkind (Parent (N)) /= N_Object_Declaration
5402 or else Expression (Parent (N)) /= N)
5405 Cnn : constant Node_Id := Make_Temporary (Loc, 'C', N);
5408 Make_Object_Declaration (Loc,
5409 Defining_Identifier => Cnn,
5410 Constant_Present => True,
5411 Object_Definition => New_Occurrence_Of (Typ, Loc),
5412 Expression => Relocate_Node (N),
5413 Has_Init_Expression => True));
5415 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
5419 -- For other types, we only need to expand if there are other actions
5420 -- associated with either branch.
5422 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
5424 -- We now wrap the actions into the appropriate expression
5426 if Present (Then_Actions (N)) then
5428 Make_Expression_With_Actions (Sloc (Thenx),
5429 Actions => Then_Actions (N),
5430 Expression => Relocate_Node (Thenx)));
5432 Set_Then_Actions (N, No_List);
5433 Analyze_And_Resolve (Thenx, Typ);
5436 if Present (Else_Actions (N)) then
5438 Make_Expression_With_Actions (Sloc (Elsex),
5439 Actions => Else_Actions (N),
5440 Expression => Relocate_Node (Elsex)));
5442 Set_Else_Actions (N, No_List);
5443 Analyze_And_Resolve (Elsex, Typ);
5448 -- If no actions then no expansion needed, gigi will handle it using the
5449 -- same approach as a C conditional expression.
5455 -- Fall through here for either the limited expansion, or the case of
5456 -- inserting actions for non-limited types. In both these cases, we must
5457 -- move the SLOC of the parent If statement to the newly created one and
5458 -- change it to the SLOC of the expression which, after expansion, will
5459 -- correspond to what is being evaluated.
5461 if Present (Parent (N)) and then Nkind (Parent (N)) = N_If_Statement then
5462 Set_Sloc (New_If, Sloc (Parent (N)));
5463 Set_Sloc (Parent (N), Loc);
5466 -- Make sure Then_Actions and Else_Actions are appropriately moved
5467 -- to the new if statement.
5469 if Present (Then_Actions (N)) then
5471 (First (Then_Statements (New_If)), Then_Actions (N));
5474 if Present (Else_Actions (N)) then
5476 (First (Else_Statements (New_If)), Else_Actions (N));
5479 Insert_Action (N, Decl);
5480 Insert_Action (N, New_If);
5482 Analyze_And_Resolve (N, Typ);
5483 end Expand_N_If_Expression;
5489 procedure Expand_N_In (N : Node_Id) is
5490 Loc : constant Source_Ptr := Sloc (N);
5491 Restyp : constant Entity_Id := Etype (N);
5492 Lop : constant Node_Id := Left_Opnd (N);
5493 Rop : constant Node_Id := Right_Opnd (N);
5494 Static : constant Boolean := Is_OK_Static_Expression (N);
5499 procedure Substitute_Valid_Check;
5500 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5501 -- test for the left operand being in range of its subtype.
5503 ----------------------------
5504 -- Substitute_Valid_Check --
5505 ----------------------------
5507 procedure Substitute_Valid_Check is
5510 Make_Attribute_Reference (Loc,
5511 Prefix => Relocate_Node (Lop),
5512 Attribute_Name => Name_Valid));
5514 Analyze_And_Resolve (N, Restyp);
5516 -- Give warning unless overflow checking is MINIMIZED or ELIMINATED,
5517 -- in which case, this usage makes sense, and in any case, we have
5518 -- actually eliminated the danger of optimization above.
5520 if Overflow_Check_Mode not in Minimized_Or_Eliminated then
5522 ("??explicit membership test may be optimized away", N);
5523 Error_Msg_N -- CODEFIX
5524 ("\??use ''Valid attribute instead", N);
5528 end Substitute_Valid_Check;
5530 -- Start of processing for Expand_N_In
5533 -- If set membership case, expand with separate procedure
5535 if Present (Alternatives (N)) then
5536 Expand_Set_Membership (N);
5540 -- Not set membership, proceed with expansion
5542 Ltyp := Etype (Left_Opnd (N));
5543 Rtyp := Etype (Right_Opnd (N));
5545 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
5546 -- type, then expand with a separate procedure. Note the use of the
5547 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5549 if Overflow_Check_Mode in Minimized_Or_Eliminated
5550 and then Is_Signed_Integer_Type (Ltyp)
5551 and then not No_Minimize_Eliminate (N)
5553 Expand_Membership_Minimize_Eliminate_Overflow (N);
5557 -- Check case of explicit test for an expression in range of its
5558 -- subtype. This is suspicious usage and we replace it with a 'Valid
5559 -- test and give a warning for scalar types.
5561 if Is_Scalar_Type (Ltyp)
5563 -- Only relevant for source comparisons
5565 and then Comes_From_Source (N)
5567 -- In floating-point this is a standard way to check for finite values
5568 -- and using 'Valid would typically be a pessimization.
5570 and then not Is_Floating_Point_Type (Ltyp)
5572 -- Don't give the message unless right operand is a type entity and
5573 -- the type of the left operand matches this type. Note that this
5574 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
5575 -- checks have changed the type of the left operand.
5577 and then Nkind (Rop) in N_Has_Entity
5578 and then Ltyp = Entity (Rop)
5580 -- Skip this for predicated types, where such expressions are a
5581 -- reasonable way of testing if something meets the predicate.
5583 and then not Present (Predicate_Function (Ltyp))
5585 Substitute_Valid_Check;
5589 -- Do validity check on operands
5591 if Validity_Checks_On and Validity_Check_Operands then
5592 Ensure_Valid (Left_Opnd (N));
5593 Validity_Check_Range (Right_Opnd (N));
5596 -- Case of explicit range
5598 if Nkind (Rop) = N_Range then
5600 Lo : constant Node_Id := Low_Bound (Rop);
5601 Hi : constant Node_Id := High_Bound (Rop);
5603 Lo_Orig : constant Node_Id := Original_Node (Lo);
5604 Hi_Orig : constant Node_Id := Original_Node (Hi);
5606 Lcheck : Compare_Result;
5607 Ucheck : Compare_Result;
5609 Warn1 : constant Boolean :=
5610 Constant_Condition_Warnings
5611 and then Comes_From_Source (N)
5612 and then not In_Instance;
5613 -- This must be true for any of the optimization warnings, we
5614 -- clearly want to give them only for source with the flag on. We
5615 -- also skip these warnings in an instance since it may be the
5616 -- case that different instantiations have different ranges.
5618 Warn2 : constant Boolean :=
5620 and then Nkind (Original_Node (Rop)) = N_Range
5621 and then Is_Integer_Type (Etype (Lo));
5622 -- For the case where only one bound warning is elided, we also
5623 -- insist on an explicit range and an integer type. The reason is
5624 -- that the use of enumeration ranges including an end point is
5625 -- common, as is the use of a subtype name, one of whose bounds is
5626 -- the same as the type of the expression.
5629 -- If test is explicit x'First .. x'Last, replace by valid check
5631 -- Could use some individual comments for this complex test ???
5633 if Is_Scalar_Type (Ltyp)
5635 -- And left operand is X'First where X matches left operand
5636 -- type (this eliminates cases of type mismatch, including
5637 -- the cases where ELIMINATED/MINIMIZED mode has changed the
5638 -- type of the left operand.
5640 and then Nkind (Lo_Orig) = N_Attribute_Reference
5641 and then Attribute_Name (Lo_Orig) = Name_First
5642 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
5643 and then Entity (Prefix (Lo_Orig)) = Ltyp
5645 -- Same tests for right operand
5647 and then Nkind (Hi_Orig) = N_Attribute_Reference
5648 and then Attribute_Name (Hi_Orig) = Name_Last
5649 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
5650 and then Entity (Prefix (Hi_Orig)) = Ltyp
5652 -- Relevant only for source cases
5654 and then Comes_From_Source (N)
5656 Substitute_Valid_Check;
5660 -- If bounds of type are known at compile time, and the end points
5661 -- are known at compile time and identical, this is another case
5662 -- for substituting a valid test. We only do this for discrete
5663 -- types, since it won't arise in practice for float types.
5665 if Comes_From_Source (N)
5666 and then Is_Discrete_Type (Ltyp)
5667 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
5668 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
5669 and then Compile_Time_Known_Value (Lo)
5670 and then Compile_Time_Known_Value (Hi)
5671 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
5672 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
5674 -- Kill warnings in instances, since they may be cases where we
5675 -- have a test in the generic that makes sense with some types
5676 -- and not with other types.
5678 and then not In_Instance
5680 Substitute_Valid_Check;
5684 -- If we have an explicit range, do a bit of optimization based on
5685 -- range analysis (we may be able to kill one or both checks).
5687 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
5688 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
5690 -- If either check is known to fail, replace result by False since
5691 -- the other check does not matter. Preserve the static flag for
5692 -- legality checks, because we are constant-folding beyond RM 4.9.
5694 if Lcheck = LT or else Ucheck = GT then
5696 Error_Msg_N ("?c?range test optimized away", N);
5697 Error_Msg_N ("\?c?value is known to be out of range", N);
5700 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5701 Analyze_And_Resolve (N, Restyp);
5702 Set_Is_Static_Expression (N, Static);
5705 -- If both checks are known to succeed, replace result by True,
5706 -- since we know we are in range.
5708 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
5710 Error_Msg_N ("?c?range test optimized away", N);
5711 Error_Msg_N ("\?c?value is known to be in range", N);
5714 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5715 Analyze_And_Resolve (N, Restyp);
5716 Set_Is_Static_Expression (N, Static);
5719 -- If lower bound check succeeds and upper bound check is not
5720 -- known to succeed or fail, then replace the range check with
5721 -- a comparison against the upper bound.
5723 elsif Lcheck in Compare_GE then
5724 if Warn2 and then not In_Instance then
5725 Error_Msg_N ("??lower bound test optimized away", Lo);
5726 Error_Msg_N ("\??value is known to be in range", Lo);
5732 Right_Opnd => High_Bound (Rop)));
5733 Analyze_And_Resolve (N, Restyp);
5736 -- If upper bound check succeeds and lower bound check is not
5737 -- known to succeed or fail, then replace the range check with
5738 -- a comparison against the lower bound.
5740 elsif Ucheck in Compare_LE then
5741 if Warn2 and then not In_Instance then
5742 Error_Msg_N ("??upper bound test optimized away", Hi);
5743 Error_Msg_N ("\??value is known to be in range", Hi);
5749 Right_Opnd => Low_Bound (Rop)));
5750 Analyze_And_Resolve (N, Restyp);
5754 -- We couldn't optimize away the range check, but there is one
5755 -- more issue. If we are checking constant conditionals, then we
5756 -- see if we can determine the outcome assuming everything is
5757 -- valid, and if so give an appropriate warning.
5759 if Warn1 and then not Assume_No_Invalid_Values then
5760 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
5761 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
5763 -- Result is out of range for valid value
5765 if Lcheck = LT or else Ucheck = GT then
5767 ("?c?value can only be in range if it is invalid", N);
5769 -- Result is in range for valid value
5771 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
5773 ("?c?value can only be out of range if it is invalid", N);
5775 -- Lower bound check succeeds if value is valid
5777 elsif Warn2 and then Lcheck in Compare_GE then
5779 ("?c?lower bound check only fails if it is invalid", Lo);
5781 -- Upper bound check succeeds if value is valid
5783 elsif Warn2 and then Ucheck in Compare_LE then
5785 ("?c?upper bound check only fails for invalid values", Hi);
5790 -- For all other cases of an explicit range, nothing to be done
5794 -- Here right operand is a subtype mark
5798 Typ : Entity_Id := Etype (Rop);
5799 Is_Acc : constant Boolean := Is_Access_Type (Typ);
5800 Cond : Node_Id := Empty;
5802 Obj : Node_Id := Lop;
5803 SCIL_Node : Node_Id;
5806 Remove_Side_Effects (Obj);
5808 -- For tagged type, do tagged membership operation
5810 if Is_Tagged_Type (Typ) then
5812 -- No expansion will be performed for VM targets, as the VM
5813 -- back-ends will handle the membership tests directly.
5815 if Tagged_Type_Expansion then
5816 Tagged_Membership (N, SCIL_Node, New_N);
5818 Analyze_And_Resolve (N, Restyp);
5820 -- Update decoration of relocated node referenced by the
5823 if Generate_SCIL and then Present (SCIL_Node) then
5824 Set_SCIL_Node (N, SCIL_Node);
5830 -- If type is scalar type, rewrite as x in t'First .. t'Last.
5831 -- This reason we do this is that the bounds may have the wrong
5832 -- type if they come from the original type definition. Also this
5833 -- way we get all the processing above for an explicit range.
5835 -- Don't do this for predicated types, since in this case we
5836 -- want to check the predicate.
5838 elsif Is_Scalar_Type (Typ) then
5839 if No (Predicate_Function (Typ)) then
5843 Make_Attribute_Reference (Loc,
5844 Attribute_Name => Name_First,
5845 Prefix => New_Occurrence_Of (Typ, Loc)),
5848 Make_Attribute_Reference (Loc,
5849 Attribute_Name => Name_Last,
5850 Prefix => New_Occurrence_Of (Typ, Loc))));
5851 Analyze_And_Resolve (N, Restyp);
5856 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5857 -- a membership test if the subtype mark denotes a constrained
5858 -- Unchecked_Union subtype and the expression lacks inferable
5861 elsif Is_Unchecked_Union (Base_Type (Typ))
5862 and then Is_Constrained (Typ)
5863 and then not Has_Inferable_Discriminants (Lop)
5866 Make_Raise_Program_Error (Loc,
5867 Reason => PE_Unchecked_Union_Restriction));
5869 -- Prevent Gigi from generating incorrect code by rewriting the
5870 -- test as False. What is this undocumented thing about ???
5872 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5876 -- Here we have a non-scalar type
5879 Typ := Designated_Type (Typ);
5882 if not Is_Constrained (Typ) then
5883 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5884 Analyze_And_Resolve (N, Restyp);
5886 -- For the constrained array case, we have to check the subscripts
5887 -- for an exact match if the lengths are non-zero (the lengths
5888 -- must match in any case).
5890 elsif Is_Array_Type (Typ) then
5891 Check_Subscripts : declare
5892 function Build_Attribute_Reference
5895 Dim : Nat) return Node_Id;
5896 -- Build attribute reference E'Nam (Dim)
5898 -------------------------------
5899 -- Build_Attribute_Reference --
5900 -------------------------------
5902 function Build_Attribute_Reference
5905 Dim : Nat) return Node_Id
5909 Make_Attribute_Reference (Loc,
5911 Attribute_Name => Nam,
5912 Expressions => New_List (
5913 Make_Integer_Literal (Loc, Dim)));
5914 end Build_Attribute_Reference;
5916 -- Start of processing for Check_Subscripts
5919 for J in 1 .. Number_Dimensions (Typ) loop
5920 Evolve_And_Then (Cond,
5923 Build_Attribute_Reference
5924 (Duplicate_Subexpr_No_Checks (Obj),
5927 Build_Attribute_Reference
5928 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
5930 Evolve_And_Then (Cond,
5933 Build_Attribute_Reference
5934 (Duplicate_Subexpr_No_Checks (Obj),
5937 Build_Attribute_Reference
5938 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
5947 Right_Opnd => Make_Null (Loc)),
5948 Right_Opnd => Cond);
5952 Analyze_And_Resolve (N, Restyp);
5953 end Check_Subscripts;
5955 -- These are the cases where constraint checks may be required,
5956 -- e.g. records with possible discriminants
5959 -- Expand the test into a series of discriminant comparisons.
5960 -- The expression that is built is the negation of the one that
5961 -- is used for checking discriminant constraints.
5963 Obj := Relocate_Node (Left_Opnd (N));
5965 if Has_Discriminants (Typ) then
5966 Cond := Make_Op_Not (Loc,
5967 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
5970 Cond := Make_Or_Else (Loc,
5974 Right_Opnd => Make_Null (Loc)),
5975 Right_Opnd => Cond);
5979 Cond := New_Occurrence_Of (Standard_True, Loc);
5983 Analyze_And_Resolve (N, Restyp);
5986 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
5987 -- expression of an anonymous access type. This can involve an
5988 -- accessibility test and a tagged type membership test in the
5989 -- case of tagged designated types.
5991 if Ada_Version >= Ada_2012
5993 and then Ekind (Ltyp) = E_Anonymous_Access_Type
5996 Expr_Entity : Entity_Id := Empty;
5998 Param_Level : Node_Id;
5999 Type_Level : Node_Id;
6002 if Is_Entity_Name (Lop) then
6003 Expr_Entity := Param_Entity (Lop);
6005 if not Present (Expr_Entity) then
6006 Expr_Entity := Entity (Lop);
6010 -- If a conversion of the anonymous access value to the
6011 -- tested type would be illegal, then the result is False.
6013 if not Valid_Conversion
6014 (Lop, Rtyp, Lop, Report_Errs => False)
6016 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
6017 Analyze_And_Resolve (N, Restyp);
6019 -- Apply an accessibility check if the access object has an
6020 -- associated access level and when the level of the type is
6021 -- less deep than the level of the access parameter. This
6022 -- only occur for access parameters and stand-alone objects
6023 -- of an anonymous access type.
6026 if Present (Expr_Entity)
6029 (Effective_Extra_Accessibility (Expr_Entity))
6030 and then UI_Gt (Object_Access_Level (Lop),
6031 Type_Access_Level (Rtyp))
6035 (Effective_Extra_Accessibility (Expr_Entity), Loc);
6038 Make_Integer_Literal (Loc, Type_Access_Level (Rtyp));
6040 -- Return True only if the accessibility level of the
6041 -- expression entity is not deeper than the level of
6042 -- the tested access type.
6046 Left_Opnd => Relocate_Node (N),
6047 Right_Opnd => Make_Op_Le (Loc,
6048 Left_Opnd => Param_Level,
6049 Right_Opnd => Type_Level)));
6051 Analyze_And_Resolve (N);
6054 -- If the designated type is tagged, do tagged membership
6057 -- *** NOTE: we have to check not null before doing the
6058 -- tagged membership test (but maybe that can be done
6059 -- inside Tagged_Membership?).
6061 if Is_Tagged_Type (Typ) then
6064 Left_Opnd => Relocate_Node (N),
6068 Right_Opnd => Make_Null (Loc))));
6070 -- No expansion will be performed for VM targets, as
6071 -- the VM back-ends will handle the membership tests
6074 if Tagged_Type_Expansion then
6076 -- Note that we have to pass Original_Node, because
6077 -- the membership test might already have been
6078 -- rewritten by earlier parts of membership test.
6081 (Original_Node (N), SCIL_Node, New_N);
6083 -- Update decoration of relocated node referenced
6084 -- by the SCIL node.
6086 if Generate_SCIL and then Present (SCIL_Node) then
6087 Set_SCIL_Node (New_N, SCIL_Node);
6092 Left_Opnd => Relocate_Node (N),
6093 Right_Opnd => New_N));
6095 Analyze_And_Resolve (N, Restyp);
6104 -- At this point, we have done the processing required for the basic
6105 -- membership test, but not yet dealt with the predicate.
6109 -- If a predicate is present, then we do the predicate test, but we
6110 -- most certainly want to omit this if we are within the predicate
6111 -- function itself, since otherwise we have an infinite recursion.
6112 -- The check should also not be emitted when testing against a range
6113 -- (the check is only done when the right operand is a subtype; see
6114 -- RM12-4.5.2 (28.1/3-30/3)).
6117 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
6121 and then Current_Scope /= PFunc
6122 and then Nkind (Rop) /= N_Range
6126 Left_Opnd => Relocate_Node (N),
6127 Right_Opnd => Make_Predicate_Call (Rtyp, Lop, Mem => True)));
6129 -- Analyze new expression, mark left operand as analyzed to
6130 -- avoid infinite recursion adding predicate calls. Similarly,
6131 -- suppress further range checks on the call.
6133 Set_Analyzed (Left_Opnd (N));
6134 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6136 -- All done, skip attempt at compile time determination of result
6143 --------------------------------
6144 -- Expand_N_Indexed_Component --
6145 --------------------------------
6147 procedure Expand_N_Indexed_Component (N : Node_Id) is
6148 Loc : constant Source_Ptr := Sloc (N);
6149 Typ : constant Entity_Id := Etype (N);
6150 P : constant Node_Id := Prefix (N);
6151 T : constant Entity_Id := Etype (P);
6155 -- A special optimization, if we have an indexed component that is
6156 -- selecting from a slice, then we can eliminate the slice, since, for
6157 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6158 -- the range check required by the slice. The range check for the slice
6159 -- itself has already been generated. The range check for the
6160 -- subscripting operation is ensured by converting the subject to
6161 -- the subtype of the slice.
6163 -- This optimization not only generates better code, avoiding slice
6164 -- messing especially in the packed case, but more importantly bypasses
6165 -- some problems in handling this peculiar case, for example, the issue
6166 -- of dealing specially with object renamings.
6168 if Nkind (P) = N_Slice
6170 -- This optimization is disabled for CodePeer because it can transform
6171 -- an index-check constraint_error into a range-check constraint_error
6172 -- and CodePeer cares about that distinction.
6174 and then not CodePeer_Mode
6177 Make_Indexed_Component (Loc,
6178 Prefix => Prefix (P),
6179 Expressions => New_List (
6181 (Etype (First_Index (Etype (P))),
6182 First (Expressions (N))))));
6183 Analyze_And_Resolve (N, Typ);
6187 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6188 -- function, then additional actuals must be passed.
6190 if Ada_Version >= Ada_2005
6191 and then Is_Build_In_Place_Function_Call (P)
6193 Make_Build_In_Place_Call_In_Anonymous_Context (P);
6196 -- If the prefix is an access type, then we unconditionally rewrite if
6197 -- as an explicit dereference. This simplifies processing for several
6198 -- cases, including packed array cases and certain cases in which checks
6199 -- must be generated. We used to try to do this only when it was
6200 -- necessary, but it cleans up the code to do it all the time.
6202 if Is_Access_Type (T) then
6203 Insert_Explicit_Dereference (P);
6204 Analyze_And_Resolve (P, Designated_Type (T));
6205 Atp := Designated_Type (T);
6210 -- Generate index and validity checks
6212 Generate_Index_Checks (N);
6214 if Validity_Checks_On and then Validity_Check_Subscripts then
6215 Apply_Subscript_Validity_Checks (N);
6218 -- If selecting from an array with atomic components, and atomic sync
6219 -- is not suppressed for this array type, set atomic sync flag.
6221 if (Has_Atomic_Components (Atp)
6222 and then not Atomic_Synchronization_Disabled (Atp))
6223 or else (Is_Atomic (Typ)
6224 and then not Atomic_Synchronization_Disabled (Typ))
6226 Activate_Atomic_Synchronization (N);
6229 -- All done for the non-packed case
6231 if not Is_Packed (Etype (Prefix (N))) then
6235 -- For packed arrays that are not bit-packed (i.e. the case of an array
6236 -- with one or more index types with a non-contiguous enumeration type),
6237 -- we can always use the normal packed element get circuit.
6239 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
6240 Expand_Packed_Element_Reference (N);
6244 -- For a reference to a component of a bit packed array, we convert it
6245 -- to a reference to the corresponding Packed_Array_Impl_Type. We only
6246 -- want to do this for simple references, and not for:
6248 -- Left side of assignment, or prefix of left side of assignment, or
6249 -- prefix of the prefix, to handle packed arrays of packed arrays,
6250 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6252 -- Renaming objects in renaming associations
6253 -- This case is handled when a use of the renamed variable occurs
6255 -- Actual parameters for a procedure call
6256 -- This case is handled in Exp_Ch6.Expand_Actuals
6258 -- The second expression in a 'Read attribute reference
6260 -- The prefix of an address or bit or size attribute reference
6262 -- The following circuit detects these exceptions. Note that we need to
6263 -- deal with implicit dereferences when climbing up the parent chain,
6264 -- with the additional difficulty that the type of parents may have yet
6265 -- to be resolved since prefixes are usually resolved first.
6268 Child : Node_Id := N;
6269 Parnt : Node_Id := Parent (N);
6273 if Nkind (Parnt) = N_Unchecked_Expression then
6276 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
6277 N_Procedure_Call_Statement)
6278 or else (Nkind (Parnt) = N_Parameter_Association
6280 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
6284 elsif Nkind (Parnt) = N_Attribute_Reference
6285 and then Nam_In (Attribute_Name (Parnt), Name_Address,
6288 and then Prefix (Parnt) = Child
6292 elsif Nkind (Parnt) = N_Assignment_Statement
6293 and then Name (Parnt) = Child
6297 -- If the expression is an index of an indexed component, it must
6298 -- be expanded regardless of context.
6300 elsif Nkind (Parnt) = N_Indexed_Component
6301 and then Child /= Prefix (Parnt)
6303 Expand_Packed_Element_Reference (N);
6306 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
6307 and then Name (Parent (Parnt)) = Parnt
6311 elsif Nkind (Parnt) = N_Attribute_Reference
6312 and then Attribute_Name (Parnt) = Name_Read
6313 and then Next (First (Expressions (Parnt))) = Child
6317 elsif Nkind (Parnt) = N_Indexed_Component
6318 and then Prefix (Parnt) = Child
6322 elsif Nkind (Parnt) = N_Selected_Component
6323 and then Prefix (Parnt) = Child
6324 and then not (Present (Etype (Selector_Name (Parnt)))
6326 Is_Access_Type (Etype (Selector_Name (Parnt))))
6330 -- If the parent is a dereference, either implicit or explicit,
6331 -- then the packed reference needs to be expanded.
6334 Expand_Packed_Element_Reference (N);
6338 -- Keep looking up tree for unchecked expression, or if we are the
6339 -- prefix of a possible assignment left side.
6342 Parnt := Parent (Child);
6345 end Expand_N_Indexed_Component;
6347 ---------------------
6348 -- Expand_N_Not_In --
6349 ---------------------
6351 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6352 -- can be done. This avoids needing to duplicate this expansion code.
6354 procedure Expand_N_Not_In (N : Node_Id) is
6355 Loc : constant Source_Ptr := Sloc (N);
6356 Typ : constant Entity_Id := Etype (N);
6357 Cfs : constant Boolean := Comes_From_Source (N);
6364 Left_Opnd => Left_Opnd (N),
6365 Right_Opnd => Right_Opnd (N))));
6367 -- If this is a set membership, preserve list of alternatives
6369 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
6371 -- We want this to appear as coming from source if original does (see
6372 -- transformations in Expand_N_In).
6374 Set_Comes_From_Source (N, Cfs);
6375 Set_Comes_From_Source (Right_Opnd (N), Cfs);
6377 -- Now analyze transformed node
6379 Analyze_And_Resolve (N, Typ);
6380 end Expand_N_Not_In;
6386 -- The only replacement required is for the case of a null of a type that
6387 -- is an access to protected subprogram, or a subtype thereof. We represent
6388 -- such access values as a record, and so we must replace the occurrence of
6389 -- null by the equivalent record (with a null address and a null pointer in
6390 -- it), so that the backend creates the proper value.
6392 procedure Expand_N_Null (N : Node_Id) is
6393 Loc : constant Source_Ptr := Sloc (N);
6394 Typ : constant Entity_Id := Base_Type (Etype (N));
6398 if Is_Access_Protected_Subprogram_Type (Typ) then
6400 Make_Aggregate (Loc,
6401 Expressions => New_List (
6402 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
6406 Analyze_And_Resolve (N, Equivalent_Type (Typ));
6408 -- For subsequent semantic analysis, the node must retain its type.
6409 -- Gigi in any case replaces this type by the corresponding record
6410 -- type before processing the node.
6416 when RE_Not_Available =>
6420 ---------------------
6421 -- Expand_N_Op_Abs --
6422 ---------------------
6424 procedure Expand_N_Op_Abs (N : Node_Id) is
6425 Loc : constant Source_Ptr := Sloc (N);
6426 Expr : constant Node_Id := Right_Opnd (N);
6429 Unary_Op_Validity_Checks (N);
6431 -- Check for MINIMIZED/ELIMINATED overflow mode
6433 if Minimized_Eliminated_Overflow_Check (N) then
6434 Apply_Arithmetic_Overflow_Check (N);
6438 -- Deal with software overflow checking
6440 if not Backend_Overflow_Checks_On_Target
6441 and then Is_Signed_Integer_Type (Etype (N))
6442 and then Do_Overflow_Check (N)
6444 -- The only case to worry about is when the argument is equal to the
6445 -- largest negative number, so what we do is to insert the check:
6447 -- [constraint_error when Expr = typ'Base'First]
6449 -- with the usual Duplicate_Subexpr use coding for expr
6452 Make_Raise_Constraint_Error (Loc,
6455 Left_Opnd => Duplicate_Subexpr (Expr),
6457 Make_Attribute_Reference (Loc,
6459 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
6460 Attribute_Name => Name_First)),
6461 Reason => CE_Overflow_Check_Failed));
6463 end Expand_N_Op_Abs;
6465 ---------------------
6466 -- Expand_N_Op_Add --
6467 ---------------------
6469 procedure Expand_N_Op_Add (N : Node_Id) is
6470 Typ : constant Entity_Id := Etype (N);
6473 Binary_Op_Validity_Checks (N);
6475 -- Check for MINIMIZED/ELIMINATED overflow mode
6477 if Minimized_Eliminated_Overflow_Check (N) then
6478 Apply_Arithmetic_Overflow_Check (N);
6482 -- N + 0 = 0 + N = N for integer types
6484 if Is_Integer_Type (Typ) then
6485 if Compile_Time_Known_Value (Right_Opnd (N))
6486 and then Expr_Value (Right_Opnd (N)) = Uint_0
6488 Rewrite (N, Left_Opnd (N));
6491 elsif Compile_Time_Known_Value (Left_Opnd (N))
6492 and then Expr_Value (Left_Opnd (N)) = Uint_0
6494 Rewrite (N, Right_Opnd (N));
6499 -- Arithmetic overflow checks for signed integer/fixed point types
6501 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
6502 Apply_Arithmetic_Overflow_Check (N);
6506 -- Overflow checks for floating-point if -gnateF mode active
6508 Check_Float_Op_Overflow (N);
6509 end Expand_N_Op_Add;
6511 ---------------------
6512 -- Expand_N_Op_And --
6513 ---------------------
6515 procedure Expand_N_Op_And (N : Node_Id) is
6516 Typ : constant Entity_Id := Etype (N);
6519 Binary_Op_Validity_Checks (N);
6521 if Is_Array_Type (Etype (N)) then
6522 Expand_Boolean_Operator (N);
6524 elsif Is_Boolean_Type (Etype (N)) then
6525 Adjust_Condition (Left_Opnd (N));
6526 Adjust_Condition (Right_Opnd (N));
6527 Set_Etype (N, Standard_Boolean);
6528 Adjust_Result_Type (N, Typ);
6530 elsif Is_Intrinsic_Subprogram (Entity (N)) then
6531 Expand_Intrinsic_Call (N, Entity (N));
6534 end Expand_N_Op_And;
6536 ------------------------
6537 -- Expand_N_Op_Concat --
6538 ------------------------
6540 procedure Expand_N_Op_Concat (N : Node_Id) is
6542 -- List of operands to be concatenated
6545 -- Node which is to be replaced by the result of concatenating the nodes
6546 -- in the list Opnds.
6549 -- Ensure validity of both operands
6551 Binary_Op_Validity_Checks (N);
6553 -- If we are the left operand of a concatenation higher up the tree,
6554 -- then do nothing for now, since we want to deal with a series of
6555 -- concatenations as a unit.
6557 if Nkind (Parent (N)) = N_Op_Concat
6558 and then N = Left_Opnd (Parent (N))
6563 -- We get here with a concatenation whose left operand may be a
6564 -- concatenation itself with a consistent type. We need to process
6565 -- these concatenation operands from left to right, which means
6566 -- from the deepest node in the tree to the highest node.
6569 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
6570 Cnode := Left_Opnd (Cnode);
6573 -- Now Cnode is the deepest concatenation, and its parents are the
6574 -- concatenation nodes above, so now we process bottom up, doing the
6577 -- The outer loop runs more than once if more than one concatenation
6578 -- type is involved.
6581 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
6582 Set_Parent (Opnds, N);
6584 -- The inner loop gathers concatenation operands
6586 Inner : while Cnode /= N
6587 and then Base_Type (Etype (Cnode)) =
6588 Base_Type (Etype (Parent (Cnode)))
6590 Cnode := Parent (Cnode);
6591 Append (Right_Opnd (Cnode), Opnds);
6594 -- Note: The following code is a temporary workaround for N731-034
6595 -- and N829-028 and will be kept until the general issue of internal
6596 -- symbol serialization is addressed. The workaround is kept under a
6597 -- debug switch to avoid permiating into the general case.
6599 -- Wrap the node to concatenate into an expression actions node to
6600 -- keep it nicely packaged. This is useful in the case of an assert
6601 -- pragma with a concatenation where we want to be able to delete
6602 -- the concatenation and all its expansion stuff.
6604 if Debug_Flag_Dot_H then
6606 Cnod : constant Node_Id := Relocate_Node (Cnode);
6607 Typ : constant Entity_Id := Base_Type (Etype (Cnode));
6610 -- Note: use Rewrite rather than Replace here, so that for
6611 -- example Why_Not_Static can find the original concatenation
6615 Make_Expression_With_Actions (Sloc (Cnode),
6616 Actions => New_List (Make_Null_Statement (Sloc (Cnode))),
6617 Expression => Cnod));
6619 Expand_Concatenate (Cnod, Opnds);
6620 Analyze_And_Resolve (Cnode, Typ);
6626 Expand_Concatenate (Cnode, Opnds);
6629 exit Outer when Cnode = N;
6630 Cnode := Parent (Cnode);
6632 end Expand_N_Op_Concat;
6634 ------------------------
6635 -- Expand_N_Op_Divide --
6636 ------------------------
6638 procedure Expand_N_Op_Divide (N : Node_Id) is
6639 Loc : constant Source_Ptr := Sloc (N);
6640 Lopnd : constant Node_Id := Left_Opnd (N);
6641 Ropnd : constant Node_Id := Right_Opnd (N);
6642 Ltyp : constant Entity_Id := Etype (Lopnd);
6643 Rtyp : constant Entity_Id := Etype (Ropnd);
6644 Typ : Entity_Id := Etype (N);
6645 Rknow : constant Boolean := Is_Integer_Type (Typ)
6647 Compile_Time_Known_Value (Ropnd);
6651 Binary_Op_Validity_Checks (N);
6653 -- Check for MINIMIZED/ELIMINATED overflow mode
6655 if Minimized_Eliminated_Overflow_Check (N) then
6656 Apply_Arithmetic_Overflow_Check (N);
6660 -- Otherwise proceed with expansion of division
6663 Rval := Expr_Value (Ropnd);
6666 -- N / 1 = N for integer types
6668 if Rknow and then Rval = Uint_1 then
6673 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
6674 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6675 -- operand is an unsigned integer, as required for this to work.
6677 if Nkind (Ropnd) = N_Op_Expon
6678 and then Is_Power_Of_2_For_Shift (Ropnd)
6680 -- We cannot do this transformation in configurable run time mode if we
6681 -- have 64-bit integers and long shifts are not available.
6683 and then (Esize (Ltyp) <= 32 or else Support_Long_Shifts_On_Target)
6686 Make_Op_Shift_Right (Loc,
6689 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
6690 Analyze_And_Resolve (N, Typ);
6694 -- Do required fixup of universal fixed operation
6696 if Typ = Universal_Fixed then
6697 Fixup_Universal_Fixed_Operation (N);
6701 -- Divisions with fixed-point results
6703 if Is_Fixed_Point_Type (Typ) then
6705 -- Deal with divide-by-zero check if back end cannot handle them
6706 -- and the flag is set indicating that we need such a check. Note
6707 -- that we don't need to bother here with the case of mixed-mode
6708 -- (Right operand an integer type), since these will be rewritten
6709 -- with conversions to a divide with a fixed-point right operand.
6711 if Do_Division_Check (N)
6712 and then not Backend_Divide_Checks_On_Target
6713 and then not Is_Integer_Type (Rtyp)
6715 Set_Do_Division_Check (N, False);
6717 Make_Raise_Constraint_Error (Loc,
6720 Left_Opnd => Duplicate_Subexpr_Move_Checks (Ropnd),
6721 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
6722 Reason => CE_Divide_By_Zero));
6725 -- No special processing if Treat_Fixed_As_Integer is set, since
6726 -- from a semantic point of view such operations are simply integer
6727 -- operations and will be treated that way.
6729 if not Treat_Fixed_As_Integer (N) then
6730 if Is_Integer_Type (Rtyp) then
6731 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
6733 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
6737 -- Other cases of division of fixed-point operands. Again we exclude the
6738 -- case where Treat_Fixed_As_Integer is set.
6740 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6741 and then not Treat_Fixed_As_Integer (N)
6743 if Is_Integer_Type (Typ) then
6744 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
6746 pragma Assert (Is_Floating_Point_Type (Typ));
6747 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
6750 -- Mixed-mode operations can appear in a non-static universal context,
6751 -- in which case the integer argument must be converted explicitly.
6753 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
6755 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
6757 Analyze_And_Resolve (Ropnd, Universal_Real);
6759 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
6761 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
6763 Analyze_And_Resolve (Lopnd, Universal_Real);
6765 -- Non-fixed point cases, do integer zero divide and overflow checks
6767 elsif Is_Integer_Type (Typ) then
6768 Apply_Divide_Checks (N);
6771 -- Overflow checks for floating-point if -gnateF mode active
6773 Check_Float_Op_Overflow (N);
6774 end Expand_N_Op_Divide;
6776 --------------------
6777 -- Expand_N_Op_Eq --
6778 --------------------
6780 procedure Expand_N_Op_Eq (N : Node_Id) is
6781 Loc : constant Source_Ptr := Sloc (N);
6782 Typ : constant Entity_Id := Etype (N);
6783 Lhs : constant Node_Id := Left_Opnd (N);
6784 Rhs : constant Node_Id := Right_Opnd (N);
6785 Bodies : constant List_Id := New_List;
6786 A_Typ : constant Entity_Id := Etype (Lhs);
6788 Typl : Entity_Id := A_Typ;
6789 Op_Name : Entity_Id;
6792 procedure Build_Equality_Call (Eq : Entity_Id);
6793 -- If a constructed equality exists for the type or for its parent,
6794 -- build and analyze call, adding conversions if the operation is
6797 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
6798 -- Determines whether a type has a subcomponent of an unconstrained
6799 -- Unchecked_Union subtype. Typ is a record type.
6801 -------------------------
6802 -- Build_Equality_Call --
6803 -------------------------
6805 procedure Build_Equality_Call (Eq : Entity_Id) is
6806 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
6807 L_Exp : Node_Id := Relocate_Node (Lhs);
6808 R_Exp : Node_Id := Relocate_Node (Rhs);
6811 -- Adjust operands if necessary to comparison type
6813 if Base_Type (Op_Type) /= Base_Type (A_Typ)
6814 and then not Is_Class_Wide_Type (A_Typ)
6816 L_Exp := OK_Convert_To (Op_Type, L_Exp);
6817 R_Exp := OK_Convert_To (Op_Type, R_Exp);
6820 -- If we have an Unchecked_Union, we need to add the inferred
6821 -- discriminant values as actuals in the function call. At this
6822 -- point, the expansion has determined that both operands have
6823 -- inferable discriminants.
6825 if Is_Unchecked_Union (Op_Type) then
6827 Lhs_Type : constant Node_Id := Etype (L_Exp);
6828 Rhs_Type : constant Node_Id := Etype (R_Exp);
6830 Lhs_Discr_Vals : Elist_Id;
6831 -- List of inferred discriminant values for left operand.
6833 Rhs_Discr_Vals : Elist_Id;
6834 -- List of inferred discriminant values for right operand.
6839 Lhs_Discr_Vals := New_Elmt_List;
6840 Rhs_Discr_Vals := New_Elmt_List;
6842 -- Per-object constrained selected components require special
6843 -- attention. If the enclosing scope of the component is an
6844 -- Unchecked_Union, we cannot reference its discriminants
6845 -- directly. This is why we use the extra parameters of the
6846 -- equality function of the enclosing Unchecked_Union.
6848 -- type UU_Type (Discr : Integer := 0) is
6851 -- pragma Unchecked_Union (UU_Type);
6853 -- 1. Unchecked_Union enclosing record:
6855 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
6857 -- Comp : UU_Type (Discr);
6859 -- end Enclosing_UU_Type;
6860 -- pragma Unchecked_Union (Enclosing_UU_Type);
6862 -- Obj1 : Enclosing_UU_Type;
6863 -- Obj2 : Enclosing_UU_Type (1);
6865 -- [. . .] Obj1 = Obj2 [. . .]
6869 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
6871 -- A and B are the formal parameters of the equality function
6872 -- of Enclosing_UU_Type. The function always has two extra
6873 -- formals to capture the inferred discriminant values for
6874 -- each discriminant of the type.
6876 -- 2. Non-Unchecked_Union enclosing record:
6879 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
6882 -- Comp : UU_Type (Discr);
6884 -- end Enclosing_Non_UU_Type;
6886 -- Obj1 : Enclosing_Non_UU_Type;
6887 -- Obj2 : Enclosing_Non_UU_Type (1);
6889 -- ... Obj1 = Obj2 ...
6893 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
6894 -- obj1.discr, obj2.discr)) then
6896 -- In this case we can directly reference the discriminants of
6897 -- the enclosing record.
6899 -- Process left operand of equality
6901 if Nkind (Lhs) = N_Selected_Component
6903 Has_Per_Object_Constraint (Entity (Selector_Name (Lhs)))
6905 -- If enclosing record is an Unchecked_Union, use formals
6906 -- corresponding to each discriminant. The name of the
6907 -- formal is that of the discriminant, with added suffix,
6908 -- see Exp_Ch3.Build_Record_Equality for details.
6910 if Is_Unchecked_Union (Scope (Entity (Selector_Name (Lhs))))
6914 (Scope (Entity (Selector_Name (Lhs))));
6915 while Present (Discr) loop
6917 (Make_Identifier (Loc,
6918 Chars => New_External_Name (Chars (Discr), 'A')),
6919 To => Lhs_Discr_Vals);
6920 Next_Discriminant (Discr);
6923 -- If enclosing record is of a non-Unchecked_Union type, it
6924 -- is possible to reference its discriminants directly.
6927 Discr := First_Discriminant (Lhs_Type);
6928 while Present (Discr) loop
6930 (Make_Selected_Component (Loc,
6931 Prefix => Prefix (Lhs),
6934 (Get_Discriminant_Value (Discr,
6936 Stored_Constraint (Lhs_Type)))),
6937 To => Lhs_Discr_Vals);
6938 Next_Discriminant (Discr);
6942 -- Otherwise operand is on object with a constrained type.
6943 -- Infer the discriminant values from the constraint.
6947 Discr := First_Discriminant (Lhs_Type);
6948 while Present (Discr) loop
6951 (Get_Discriminant_Value (Discr,
6953 Stored_Constraint (Lhs_Type))),
6954 To => Lhs_Discr_Vals);
6955 Next_Discriminant (Discr);
6959 -- Similar processing for right operand of equality
6961 if Nkind (Rhs) = N_Selected_Component
6963 Has_Per_Object_Constraint (Entity (Selector_Name (Rhs)))
6965 if Is_Unchecked_Union
6966 (Scope (Entity (Selector_Name (Rhs))))
6970 (Scope (Entity (Selector_Name (Rhs))));
6971 while Present (Discr) loop
6973 (Make_Identifier (Loc,
6974 Chars => New_External_Name (Chars (Discr), 'B')),
6975 To => Rhs_Discr_Vals);
6976 Next_Discriminant (Discr);
6980 Discr := First_Discriminant (Rhs_Type);
6981 while Present (Discr) loop
6983 (Make_Selected_Component (Loc,
6984 Prefix => Prefix (Rhs),
6986 New_Copy (Get_Discriminant_Value
6989 Stored_Constraint (Rhs_Type)))),
6990 To => Rhs_Discr_Vals);
6991 Next_Discriminant (Discr);
6996 Discr := First_Discriminant (Rhs_Type);
6997 while Present (Discr) loop
6999 (New_Copy (Get_Discriminant_Value
7002 Stored_Constraint (Rhs_Type))),
7003 To => Rhs_Discr_Vals);
7004 Next_Discriminant (Discr);
7008 -- Now merge the list of discriminant values so that values
7009 -- of corresponding discriminants are adjacent.
7017 Params := New_List (L_Exp, R_Exp);
7018 L_Elmt := First_Elmt (Lhs_Discr_Vals);
7019 R_Elmt := First_Elmt (Rhs_Discr_Vals);
7020 while Present (L_Elmt) loop
7021 Append_To (Params, Node (L_Elmt));
7022 Append_To (Params, Node (R_Elmt));
7028 Make_Function_Call (Loc,
7029 Name => New_Occurrence_Of (Eq, Loc),
7030 Parameter_Associations => Params));
7034 -- Normal case, not an unchecked union
7038 Make_Function_Call (Loc,
7039 Name => New_Occurrence_Of (Eq, Loc),
7040 Parameter_Associations => New_List (L_Exp, R_Exp)));
7043 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7044 end Build_Equality_Call;
7046 ------------------------------------
7047 -- Has_Unconstrained_UU_Component --
7048 ------------------------------------
7050 function Has_Unconstrained_UU_Component
7051 (Typ : Node_Id) return Boolean
7053 Tdef : constant Node_Id :=
7054 Type_Definition (Declaration_Node (Base_Type (Typ)));
7058 function Component_Is_Unconstrained_UU
7059 (Comp : Node_Id) return Boolean;
7060 -- Determines whether the subtype of the component is an
7061 -- unconstrained Unchecked_Union.
7063 function Variant_Is_Unconstrained_UU
7064 (Variant : Node_Id) return Boolean;
7065 -- Determines whether a component of the variant has an unconstrained
7066 -- Unchecked_Union subtype.
7068 -----------------------------------
7069 -- Component_Is_Unconstrained_UU --
7070 -----------------------------------
7072 function Component_Is_Unconstrained_UU
7073 (Comp : Node_Id) return Boolean
7076 if Nkind (Comp) /= N_Component_Declaration then
7081 Sindic : constant Node_Id :=
7082 Subtype_Indication (Component_Definition (Comp));
7085 -- Unconstrained nominal type. In the case of a constraint
7086 -- present, the node kind would have been N_Subtype_Indication.
7088 if Nkind (Sindic) = N_Identifier then
7089 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
7094 end Component_Is_Unconstrained_UU;
7096 ---------------------------------
7097 -- Variant_Is_Unconstrained_UU --
7098 ---------------------------------
7100 function Variant_Is_Unconstrained_UU
7101 (Variant : Node_Id) return Boolean
7103 Clist : constant Node_Id := Component_List (Variant);
7106 if Is_Empty_List (Component_Items (Clist)) then
7110 -- We only need to test one component
7113 Comp : Node_Id := First (Component_Items (Clist));
7116 while Present (Comp) loop
7117 if Component_Is_Unconstrained_UU (Comp) then
7125 -- None of the components withing the variant were of
7126 -- unconstrained Unchecked_Union type.
7129 end Variant_Is_Unconstrained_UU;
7131 -- Start of processing for Has_Unconstrained_UU_Component
7134 if Null_Present (Tdef) then
7138 Clist := Component_List (Tdef);
7139 Vpart := Variant_Part (Clist);
7141 -- Inspect available components
7143 if Present (Component_Items (Clist)) then
7145 Comp : Node_Id := First (Component_Items (Clist));
7148 while Present (Comp) loop
7150 -- One component is sufficient
7152 if Component_Is_Unconstrained_UU (Comp) then
7161 -- Inspect available components withing variants
7163 if Present (Vpart) then
7165 Variant : Node_Id := First (Variants (Vpart));
7168 while Present (Variant) loop
7170 -- One component within a variant is sufficient
7172 if Variant_Is_Unconstrained_UU (Variant) then
7181 -- Neither the available components, nor the components inside the
7182 -- variant parts were of an unconstrained Unchecked_Union subtype.
7185 end Has_Unconstrained_UU_Component;
7187 -- Start of processing for Expand_N_Op_Eq
7190 Binary_Op_Validity_Checks (N);
7192 -- Deal with private types
7194 if Ekind (Typl) = E_Private_Type then
7195 Typl := Underlying_Type (Typl);
7196 elsif Ekind (Typl) = E_Private_Subtype then
7197 Typl := Underlying_Type (Base_Type (Typl));
7202 -- It may happen in error situations that the underlying type is not
7203 -- set. The error will be detected later, here we just defend the
7210 -- Now get the implementation base type (note that plain Base_Type here
7211 -- might lead us back to the private type, which is not what we want!)
7213 Typl := Implementation_Base_Type (Typl);
7215 -- Equality between variant records results in a call to a routine
7216 -- that has conditional tests of the discriminant value(s), and hence
7217 -- violates the No_Implicit_Conditionals restriction.
7219 if Has_Variant_Part (Typl) then
7224 Check_Restriction (Msg, No_Implicit_Conditionals, N);
7228 ("\comparison of variant records tests discriminants", N);
7234 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7235 -- means we no longer have a comparison operation, we are all done.
7237 Expand_Compare_Minimize_Eliminate_Overflow (N);
7239 if Nkind (N) /= N_Op_Eq then
7243 -- Boolean types (requiring handling of non-standard case)
7245 if Is_Boolean_Type (Typl) then
7246 Adjust_Condition (Left_Opnd (N));
7247 Adjust_Condition (Right_Opnd (N));
7248 Set_Etype (N, Standard_Boolean);
7249 Adjust_Result_Type (N, Typ);
7253 elsif Is_Array_Type (Typl) then
7255 -- If we are doing full validity checking, and it is possible for the
7256 -- array elements to be invalid then expand out array comparisons to
7257 -- make sure that we check the array elements.
7259 if Validity_Check_Operands
7260 and then not Is_Known_Valid (Component_Type (Typl))
7263 Save_Force_Validity_Checks : constant Boolean :=
7264 Force_Validity_Checks;
7266 Force_Validity_Checks := True;
7268 Expand_Array_Equality
7270 Relocate_Node (Lhs),
7271 Relocate_Node (Rhs),
7274 Insert_Actions (N, Bodies);
7275 Analyze_And_Resolve (N, Standard_Boolean);
7276 Force_Validity_Checks := Save_Force_Validity_Checks;
7279 -- Packed case where both operands are known aligned
7281 elsif Is_Bit_Packed_Array (Typl)
7282 and then not Is_Possibly_Unaligned_Object (Lhs)
7283 and then not Is_Possibly_Unaligned_Object (Rhs)
7285 Expand_Packed_Eq (N);
7287 -- Where the component type is elementary we can use a block bit
7288 -- comparison (if supported on the target) exception in the case
7289 -- of floating-point (negative zero issues require element by
7290 -- element comparison), and atomic/VFA types (where we must be sure
7291 -- to load elements independently) and possibly unaligned arrays.
7293 elsif Is_Elementary_Type (Component_Type (Typl))
7294 and then not Is_Floating_Point_Type (Component_Type (Typl))
7295 and then not Is_Atomic_Or_VFA (Component_Type (Typl))
7296 and then not Is_Possibly_Unaligned_Object (Lhs)
7297 and then not Is_Possibly_Unaligned_Object (Rhs)
7298 and then Support_Composite_Compare_On_Target
7302 -- For composite and floating-point cases, expand equality loop to
7303 -- make sure of using proper comparisons for tagged types, and
7304 -- correctly handling the floating-point case.
7308 Expand_Array_Equality
7310 Relocate_Node (Lhs),
7311 Relocate_Node (Rhs),
7314 Insert_Actions (N, Bodies, Suppress => All_Checks);
7315 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7320 elsif Is_Record_Type (Typl) then
7322 -- For tagged types, use the primitive "="
7324 if Is_Tagged_Type (Typl) then
7326 -- No need to do anything else compiling under restriction
7327 -- No_Dispatching_Calls. During the semantic analysis we
7328 -- already notified such violation.
7330 if Restriction_Active (No_Dispatching_Calls) then
7334 -- If this is derived from an untagged private type completed with
7335 -- a tagged type, it does not have a full view, so we use the
7336 -- primitive operations of the private type. This check should no
7337 -- longer be necessary when these types get their full views???
7339 if Is_Private_Type (A_Typ)
7340 and then not Is_Tagged_Type (A_Typ)
7341 and then Is_Derived_Type (A_Typ)
7342 and then No (Full_View (A_Typ))
7344 -- Search for equality operation, checking that the operands
7345 -- have the same type. Note that we must find a matching entry,
7346 -- or something is very wrong.
7348 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
7350 while Present (Prim) loop
7351 exit when Chars (Node (Prim)) = Name_Op_Eq
7352 and then Etype (First_Formal (Node (Prim))) =
7353 Etype (Next_Formal (First_Formal (Node (Prim))))
7355 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7360 pragma Assert (Present (Prim));
7361 Op_Name := Node (Prim);
7363 -- Find the type's predefined equality or an overriding
7364 -- user-defined equality. The reason for not simply calling
7365 -- Find_Prim_Op here is that there may be a user-defined
7366 -- overloaded equality op that precedes the equality that we
7367 -- want, so we have to explicitly search (e.g., there could be
7368 -- an equality with two different parameter types).
7371 if Is_Class_Wide_Type (Typl) then
7372 Typl := Find_Specific_Type (Typl);
7375 Prim := First_Elmt (Primitive_Operations (Typl));
7376 while Present (Prim) loop
7377 exit when Chars (Node (Prim)) = Name_Op_Eq
7378 and then Etype (First_Formal (Node (Prim))) =
7379 Etype (Next_Formal (First_Formal (Node (Prim))))
7381 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7386 pragma Assert (Present (Prim));
7387 Op_Name := Node (Prim);
7390 Build_Equality_Call (Op_Name);
7392 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7393 -- predefined equality operator for a type which has a subcomponent
7394 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7396 elsif Has_Unconstrained_UU_Component (Typl) then
7398 Make_Raise_Program_Error (Loc,
7399 Reason => PE_Unchecked_Union_Restriction));
7401 -- Prevent Gigi from generating incorrect code by rewriting the
7402 -- equality as a standard False. (is this documented somewhere???)
7405 New_Occurrence_Of (Standard_False, Loc));
7407 elsif Is_Unchecked_Union (Typl) then
7409 -- If we can infer the discriminants of the operands, we make a
7410 -- call to the TSS equality function.
7412 if Has_Inferable_Discriminants (Lhs)
7414 Has_Inferable_Discriminants (Rhs)
7417 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7420 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7421 -- the predefined equality operator for an Unchecked_Union type
7422 -- if either of the operands lack inferable discriminants.
7425 Make_Raise_Program_Error (Loc,
7426 Reason => PE_Unchecked_Union_Restriction));
7428 -- Emit a warning on source equalities only, otherwise the
7429 -- message may appear out of place due to internal use. The
7430 -- warning is unconditional because it is required by the
7433 if Comes_From_Source (N) then
7435 ("Unchecked_Union discriminants cannot be determined??",
7438 ("\Program_Error will be raised for equality operation??",
7442 -- Prevent Gigi from generating incorrect code by rewriting
7443 -- the equality as a standard False (documented where???).
7446 New_Occurrence_Of (Standard_False, Loc));
7449 -- If a type support function is present (for complex cases), use it
7451 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
7453 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7455 -- When comparing two Bounded_Strings, use the primitive equality of
7456 -- the root Super_String type.
7458 elsif Is_Bounded_String (Typl) then
7460 First_Elmt (Collect_Primitive_Operations (Root_Type (Typl)));
7462 while Present (Prim) loop
7463 exit when Chars (Node (Prim)) = Name_Op_Eq
7464 and then Etype (First_Formal (Node (Prim))) =
7465 Etype (Next_Formal (First_Formal (Node (Prim))))
7466 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7471 -- A Super_String type should always have a primitive equality
7473 pragma Assert (Present (Prim));
7474 Build_Equality_Call (Node (Prim));
7476 -- Otherwise expand the component by component equality. Note that
7477 -- we never use block-bit comparisons for records, because of the
7478 -- problems with gaps. The backend will often be able to recombine
7479 -- the separate comparisons that we generate here.
7482 Remove_Side_Effects (Lhs);
7483 Remove_Side_Effects (Rhs);
7485 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
7487 Insert_Actions (N, Bodies, Suppress => All_Checks);
7488 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7492 -- Test if result is known at compile time
7494 Rewrite_Comparison (N);
7496 -- Special optimization of length comparison
7498 Optimize_Length_Comparison (N);
7500 -- One more special case: if we have a comparison of X'Result = expr
7501 -- in floating-point, then if not already there, change expr to be
7502 -- f'Machine (expr) to eliminate surprise from extra precision.
7504 if Is_Floating_Point_Type (Typl)
7505 and then Nkind (Original_Node (Lhs)) = N_Attribute_Reference
7506 and then Attribute_Name (Original_Node (Lhs)) = Name_Result
7508 -- Stick in the Typ'Machine call if not already there
7510 if Nkind (Rhs) /= N_Attribute_Reference
7511 or else Attribute_Name (Rhs) /= Name_Machine
7514 Make_Attribute_Reference (Loc,
7515 Prefix => New_Occurrence_Of (Typl, Loc),
7516 Attribute_Name => Name_Machine,
7517 Expressions => New_List (Relocate_Node (Rhs))));
7518 Analyze_And_Resolve (Rhs, Typl);
7523 -----------------------
7524 -- Expand_N_Op_Expon --
7525 -----------------------
7527 procedure Expand_N_Op_Expon (N : Node_Id) is
7528 Loc : constant Source_Ptr := Sloc (N);
7529 Typ : constant Entity_Id := Etype (N);
7530 Rtyp : constant Entity_Id := Root_Type (Typ);
7531 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
7532 Bastyp : constant Node_Id := Etype (Base);
7533 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
7534 Exptyp : constant Entity_Id := Etype (Exp);
7535 Ovflo : constant Boolean := Do_Overflow_Check (N);
7543 function Wrap_MA (Exp : Node_Id) return Node_Id;
7544 -- Given an expression Exp, if the root type is Float or Long_Float,
7545 -- then wrap the expression in a call of Bastyp'Machine, to stop any
7546 -- extra precision. This is done to ensure that X**A = X**B when A is
7547 -- a static constant and B is a variable with the same value. For any
7548 -- other type, the node Exp is returned unchanged.
7554 function Wrap_MA (Exp : Node_Id) return Node_Id is
7555 Loc : constant Source_Ptr := Sloc (Exp);
7557 if Rtyp = Standard_Float or else Rtyp = Standard_Long_Float then
7559 Make_Attribute_Reference (Loc,
7560 Attribute_Name => Name_Machine,
7561 Prefix => New_Occurrence_Of (Bastyp, Loc),
7562 Expressions => New_List (Relocate_Node (Exp)));
7568 -- Start of processing for Expand_N_Op
7571 Binary_Op_Validity_Checks (N);
7573 -- CodePeer wants to see the unexpanded N_Op_Expon node
7575 if CodePeer_Mode then
7579 -- If either operand is of a private type, then we have the use of an
7580 -- intrinsic operator, and we get rid of the privateness, by using root
7581 -- types of underlying types for the actual operation. Otherwise the
7582 -- private types will cause trouble if we expand multiplications or
7583 -- shifts etc. We also do this transformation if the result type is
7584 -- different from the base type.
7586 if Is_Private_Type (Etype (Base))
7587 or else Is_Private_Type (Typ)
7588 or else Is_Private_Type (Exptyp)
7589 or else Rtyp /= Root_Type (Bastyp)
7592 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
7593 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
7596 Unchecked_Convert_To (Typ,
7598 Left_Opnd => Unchecked_Convert_To (Bt, Base),
7599 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
7600 Analyze_And_Resolve (N, Typ);
7605 -- Check for MINIMIZED/ELIMINATED overflow mode
7607 if Minimized_Eliminated_Overflow_Check (N) then
7608 Apply_Arithmetic_Overflow_Check (N);
7612 -- Test for case of known right argument where we can replace the
7613 -- exponentiation by an equivalent expression using multiplication.
7615 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
7616 -- configurable run-time mode, we may not have the exponentiation
7617 -- routine available, and we don't want the legality of the program
7618 -- to depend on how clever the compiler is in knowing values.
7620 if CRT_Safe_Compile_Time_Known_Value (Exp) then
7621 Expv := Expr_Value (Exp);
7623 -- We only fold small non-negative exponents. You might think we
7624 -- could fold small negative exponents for the real case, but we
7625 -- can't because we are required to raise Constraint_Error for
7626 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
7627 -- See ACVC test C4A012B, and it is not worth generating the test.
7629 if Expv >= 0 and then Expv <= 4 then
7631 -- X ** 0 = 1 (or 1.0)
7635 -- Call Remove_Side_Effects to ensure that any side effects
7636 -- in the ignored left operand (in particular function calls
7637 -- to user defined functions) are properly executed.
7639 Remove_Side_Effects (Base);
7641 if Ekind (Typ) in Integer_Kind then
7642 Xnode := Make_Integer_Literal (Loc, Intval => 1);
7644 Xnode := Make_Real_Literal (Loc, Ureal_1);
7657 Make_Op_Multiply (Loc,
7658 Left_Opnd => Duplicate_Subexpr (Base),
7659 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)));
7661 -- X ** 3 = X * X * X
7666 Make_Op_Multiply (Loc,
7668 Make_Op_Multiply (Loc,
7669 Left_Opnd => Duplicate_Subexpr (Base),
7670 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
7671 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)));
7676 -- En : constant base'type := base * base;
7681 pragma Assert (Expv = 4);
7682 Temp := Make_Temporary (Loc, 'E', Base);
7685 Make_Expression_With_Actions (Loc,
7686 Actions => New_List (
7687 Make_Object_Declaration (Loc,
7688 Defining_Identifier => Temp,
7689 Constant_Present => True,
7690 Object_Definition => New_Occurrence_Of (Typ, Loc),
7693 Make_Op_Multiply (Loc,
7695 Duplicate_Subexpr (Base),
7697 Duplicate_Subexpr_No_Checks (Base))))),
7701 Make_Op_Multiply (Loc,
7702 Left_Opnd => New_Occurrence_Of (Temp, Loc),
7703 Right_Opnd => New_Occurrence_Of (Temp, Loc))));
7707 Analyze_And_Resolve (N, Typ);
7712 -- Deal with optimizing 2 ** expression to shift where possible
7714 -- Note: we used to check that Exptyp was an unsigned type. But that is
7715 -- an unnecessary check, since if Exp is negative, we have a run-time
7716 -- error that is either caught (so we get the right result) or we have
7717 -- suppressed the check, in which case the code is erroneous anyway.
7719 if Is_Integer_Type (Rtyp)
7721 -- The base value must be "safe compile-time known", and exactly 2
7723 and then Nkind (Base) = N_Integer_Literal
7724 and then CRT_Safe_Compile_Time_Known_Value (Base)
7725 and then Expr_Value (Base) = Uint_2
7727 -- We only handle cases where the right type is a integer
7729 and then Is_Integer_Type (Root_Type (Exptyp))
7730 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
7732 -- This transformation is not applicable for a modular type with a
7733 -- nonbinary modulus because we do not handle modular reduction in
7734 -- a correct manner if we attempt this transformation in this case.
7736 and then not Non_Binary_Modulus (Typ)
7738 -- Handle the cases where our parent is a division or multiplication
7739 -- specially. In these cases we can convert to using a shift at the
7740 -- parent level if we are not doing overflow checking, since it is
7741 -- too tricky to combine the overflow check at the parent level.
7744 and then Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply)
7747 P : constant Node_Id := Parent (N);
7748 L : constant Node_Id := Left_Opnd (P);
7749 R : constant Node_Id := Right_Opnd (P);
7752 if (Nkind (P) = N_Op_Multiply
7754 ((Is_Integer_Type (Etype (L)) and then R = N)
7756 (Is_Integer_Type (Etype (R)) and then L = N))
7757 and then not Do_Overflow_Check (P))
7760 (Nkind (P) = N_Op_Divide
7761 and then Is_Integer_Type (Etype (L))
7762 and then Is_Unsigned_Type (Etype (L))
7764 and then not Do_Overflow_Check (P))
7766 Set_Is_Power_Of_2_For_Shift (N);
7771 -- Here we just have 2 ** N on its own, so we can convert this to a
7772 -- shift node. We are prepared to deal with overflow here, and we
7773 -- also have to handle proper modular reduction for binary modular.
7782 -- Maximum shift count with no overflow
7785 -- Set True if we must test the shift count
7788 -- Node for test against TestS
7791 -- Compute maximum shift based on the underlying size. For a
7792 -- modular type this is one less than the size.
7794 if Is_Modular_Integer_Type (Typ) then
7796 -- For modular integer types, this is the size of the value
7797 -- being shifted minus one. Any larger values will cause
7798 -- modular reduction to a result of zero. Note that we do
7799 -- want the RM_Size here (e.g. mod 2 ** 7, we want a result
7800 -- of 6, since 2**7 should be reduced to zero).
7802 MaxS := RM_Size (Rtyp) - 1;
7804 -- For signed integer types, we use the size of the value
7805 -- being shifted minus 2. Larger values cause overflow.
7808 MaxS := Esize (Rtyp) - 2;
7811 -- Determine range to see if it can be larger than MaxS
7814 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
7815 TestS := (not OK) or else Hi > MaxS;
7817 -- Signed integer case
7819 if Is_Signed_Integer_Type (Typ) then
7821 -- Generate overflow check if overflow is active. Note that
7822 -- we can simply ignore the possibility of overflow if the
7823 -- flag is not set (means that overflow cannot happen or
7824 -- that overflow checks are suppressed).
7826 if Ovflo and TestS then
7828 Make_Raise_Constraint_Error (Loc,
7831 Left_Opnd => Duplicate_Subexpr (Right_Opnd (N)),
7832 Right_Opnd => Make_Integer_Literal (Loc, MaxS)),
7833 Reason => CE_Overflow_Check_Failed));
7836 -- Now rewrite node as Shift_Left (1, right-operand)
7839 Make_Op_Shift_Left (Loc,
7840 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
7841 Right_Opnd => Right_Opnd (N)));
7843 -- Modular integer case
7845 else pragma Assert (Is_Modular_Integer_Type (Typ));
7847 -- If shift count can be greater than MaxS, we need to wrap
7848 -- the shift in a test that will reduce the result value to
7849 -- zero if this shift count is exceeded.
7853 -- Note: build node for the comparison first, before we
7854 -- reuse the Right_Opnd, so that we have proper parents
7855 -- in place for the Duplicate_Subexpr call.
7859 Left_Opnd => Duplicate_Subexpr (Right_Opnd (N)),
7860 Right_Opnd => Make_Integer_Literal (Loc, MaxS));
7863 Make_If_Expression (Loc,
7864 Expressions => New_List (
7866 Make_Integer_Literal (Loc, Uint_0),
7867 Make_Op_Shift_Left (Loc,
7868 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
7869 Right_Opnd => Right_Opnd (N)))));
7871 -- If we know shift count cannot be greater than MaxS, then
7872 -- it is safe to just rewrite as a shift with no test.
7876 Make_Op_Shift_Left (Loc,
7877 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
7878 Right_Opnd => Right_Opnd (N)));
7882 Analyze_And_Resolve (N, Typ);
7888 -- Fall through if exponentiation must be done using a runtime routine
7890 -- First deal with modular case
7892 if Is_Modular_Integer_Type (Rtyp) then
7894 -- Nonbinary modular case, we call the special exponentiation
7895 -- routine for the nonbinary case, converting the argument to
7896 -- Long_Long_Integer and passing the modulus value. Then the
7897 -- result is converted back to the base type.
7899 if Non_Binary_Modulus (Rtyp) then
7902 Make_Function_Call (Loc,
7904 New_Occurrence_Of (RTE (RE_Exp_Modular), Loc),
7905 Parameter_Associations => New_List (
7906 Convert_To (RTE (RE_Unsigned), Base),
7907 Make_Integer_Literal (Loc, Modulus (Rtyp)),
7910 -- Binary modular case, in this case, we call one of two routines,
7911 -- either the unsigned integer case, or the unsigned long long
7912 -- integer case, with a final "and" operation to do the required mod.
7915 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
7916 Ent := RTE (RE_Exp_Unsigned);
7918 Ent := RTE (RE_Exp_Long_Long_Unsigned);
7925 Make_Function_Call (Loc,
7926 Name => New_Occurrence_Of (Ent, Loc),
7927 Parameter_Associations => New_List (
7928 Convert_To (Etype (First_Formal (Ent)), Base),
7931 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
7935 -- Common exit point for modular type case
7937 Analyze_And_Resolve (N, Typ);
7940 -- Signed integer cases, done using either Integer or Long_Long_Integer.
7941 -- It is not worth having routines for Short_[Short_]Integer, since for
7942 -- most machines it would not help, and it would generate more code that
7943 -- might need certification when a certified run time is required.
7945 -- In the integer cases, we have two routines, one for when overflow
7946 -- checks are required, and one when they are not required, since there
7947 -- is a real gain in omitting checks on many machines.
7949 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
7950 or else (Rtyp = Base_Type (Standard_Long_Integer)
7952 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
7953 or else Rtyp = Universal_Integer
7955 Etyp := Standard_Long_Long_Integer;
7957 -- Overflow checking is the only choice on the AAMP target, where
7958 -- arithmetic instructions check overflow automatically, so only
7959 -- one version of the exponentiation unit is needed.
7961 if Ovflo or AAMP_On_Target then
7962 Rent := RE_Exp_Long_Long_Integer;
7964 Rent := RE_Exn_Long_Long_Integer;
7967 elsif Is_Signed_Integer_Type (Rtyp) then
7968 Etyp := Standard_Integer;
7970 -- Overflow checking is the only choice on the AAMP target, where
7971 -- arithmetic instructions check overflow automatically, so only
7972 -- one version of the exponentiation unit is needed.
7974 if Ovflo or AAMP_On_Target then
7975 Rent := RE_Exp_Integer;
7977 Rent := RE_Exn_Integer;
7980 -- Floating-point cases. We do not need separate routines for the
7981 -- overflow case here, since in the case of floating-point, we generate
7982 -- infinities anyway as a rule (either that or we automatically trap
7983 -- overflow), and if there is an infinity generated and a range check
7984 -- is required, the check will fail anyway.
7986 -- Historical note: we used to convert everything to Long_Long_Float
7987 -- and call a single common routine, but this had the undesirable effect
7988 -- of giving different results for small static exponent values and the
7989 -- same dynamic values.
7992 pragma Assert (Is_Floating_Point_Type (Rtyp));
7994 if Rtyp = Standard_Float then
7995 Etyp := Standard_Float;
7996 Rent := RE_Exn_Float;
7998 elsif Rtyp = Standard_Long_Float then
7999 Etyp := Standard_Long_Float;
8000 Rent := RE_Exn_Long_Float;
8003 Etyp := Standard_Long_Long_Float;
8004 Rent := RE_Exn_Long_Long_Float;
8008 -- Common processing for integer cases and floating-point cases.
8009 -- If we are in the right type, we can call runtime routine directly
8012 and then Rtyp /= Universal_Integer
8013 and then Rtyp /= Universal_Real
8017 Make_Function_Call (Loc,
8018 Name => New_Occurrence_Of (RTE (Rent), Loc),
8019 Parameter_Associations => New_List (Base, Exp))));
8021 -- Otherwise we have to introduce conversions (conversions are also
8022 -- required in the universal cases, since the runtime routine is
8023 -- typed using one of the standard types).
8028 Make_Function_Call (Loc,
8029 Name => New_Occurrence_Of (RTE (Rent), Loc),
8030 Parameter_Associations => New_List (
8031 Convert_To (Etyp, Base),
8035 Analyze_And_Resolve (N, Typ);
8039 when RE_Not_Available =>
8041 end Expand_N_Op_Expon;
8043 --------------------
8044 -- Expand_N_Op_Ge --
8045 --------------------
8047 procedure Expand_N_Op_Ge (N : Node_Id) is
8048 Typ : constant Entity_Id := Etype (N);
8049 Op1 : constant Node_Id := Left_Opnd (N);
8050 Op2 : constant Node_Id := Right_Opnd (N);
8051 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8054 Binary_Op_Validity_Checks (N);
8056 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8057 -- means we no longer have a comparison operation, we are all done.
8059 Expand_Compare_Minimize_Eliminate_Overflow (N);
8061 if Nkind (N) /= N_Op_Ge then
8067 if Is_Array_Type (Typ1) then
8068 Expand_Array_Comparison (N);
8072 -- Deal with boolean operands
8074 if Is_Boolean_Type (Typ1) then
8075 Adjust_Condition (Op1);
8076 Adjust_Condition (Op2);
8077 Set_Etype (N, Standard_Boolean);
8078 Adjust_Result_Type (N, Typ);
8081 Rewrite_Comparison (N);
8083 Optimize_Length_Comparison (N);
8086 --------------------
8087 -- Expand_N_Op_Gt --
8088 --------------------
8090 procedure Expand_N_Op_Gt (N : Node_Id) is
8091 Typ : constant Entity_Id := Etype (N);
8092 Op1 : constant Node_Id := Left_Opnd (N);
8093 Op2 : constant Node_Id := Right_Opnd (N);
8094 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8097 Binary_Op_Validity_Checks (N);
8099 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8100 -- means we no longer have a comparison operation, we are all done.
8102 Expand_Compare_Minimize_Eliminate_Overflow (N);
8104 if Nkind (N) /= N_Op_Gt then
8108 -- Deal with array type operands
8110 if Is_Array_Type (Typ1) then
8111 Expand_Array_Comparison (N);
8115 -- Deal with boolean type operands
8117 if Is_Boolean_Type (Typ1) then
8118 Adjust_Condition (Op1);
8119 Adjust_Condition (Op2);
8120 Set_Etype (N, Standard_Boolean);
8121 Adjust_Result_Type (N, Typ);
8124 Rewrite_Comparison (N);
8126 Optimize_Length_Comparison (N);
8129 --------------------
8130 -- Expand_N_Op_Le --
8131 --------------------
8133 procedure Expand_N_Op_Le (N : Node_Id) is
8134 Typ : constant Entity_Id := Etype (N);
8135 Op1 : constant Node_Id := Left_Opnd (N);
8136 Op2 : constant Node_Id := Right_Opnd (N);
8137 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8140 Binary_Op_Validity_Checks (N);
8142 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8143 -- means we no longer have a comparison operation, we are all done.
8145 Expand_Compare_Minimize_Eliminate_Overflow (N);
8147 if Nkind (N) /= N_Op_Le then
8151 -- Deal with array type operands
8153 if Is_Array_Type (Typ1) then
8154 Expand_Array_Comparison (N);
8158 -- Deal with Boolean type operands
8160 if Is_Boolean_Type (Typ1) then
8161 Adjust_Condition (Op1);
8162 Adjust_Condition (Op2);
8163 Set_Etype (N, Standard_Boolean);
8164 Adjust_Result_Type (N, Typ);
8167 Rewrite_Comparison (N);
8169 Optimize_Length_Comparison (N);
8172 --------------------
8173 -- Expand_N_Op_Lt --
8174 --------------------
8176 procedure Expand_N_Op_Lt (N : Node_Id) is
8177 Typ : constant Entity_Id := Etype (N);
8178 Op1 : constant Node_Id := Left_Opnd (N);
8179 Op2 : constant Node_Id := Right_Opnd (N);
8180 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8183 Binary_Op_Validity_Checks (N);
8185 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8186 -- means we no longer have a comparison operation, we are all done.
8188 Expand_Compare_Minimize_Eliminate_Overflow (N);
8190 if Nkind (N) /= N_Op_Lt then
8194 -- Deal with array type operands
8196 if Is_Array_Type (Typ1) then
8197 Expand_Array_Comparison (N);
8201 -- Deal with Boolean type operands
8203 if Is_Boolean_Type (Typ1) then
8204 Adjust_Condition (Op1);
8205 Adjust_Condition (Op2);
8206 Set_Etype (N, Standard_Boolean);
8207 Adjust_Result_Type (N, Typ);
8210 Rewrite_Comparison (N);
8212 Optimize_Length_Comparison (N);
8215 -----------------------
8216 -- Expand_N_Op_Minus --
8217 -----------------------
8219 procedure Expand_N_Op_Minus (N : Node_Id) is
8220 Loc : constant Source_Ptr := Sloc (N);
8221 Typ : constant Entity_Id := Etype (N);
8224 Unary_Op_Validity_Checks (N);
8226 -- Check for MINIMIZED/ELIMINATED overflow mode
8228 if Minimized_Eliminated_Overflow_Check (N) then
8229 Apply_Arithmetic_Overflow_Check (N);
8233 if not Backend_Overflow_Checks_On_Target
8234 and then Is_Signed_Integer_Type (Etype (N))
8235 and then Do_Overflow_Check (N)
8237 -- Software overflow checking expands -expr into (0 - expr)
8240 Make_Op_Subtract (Loc,
8241 Left_Opnd => Make_Integer_Literal (Loc, 0),
8242 Right_Opnd => Right_Opnd (N)));
8244 Analyze_And_Resolve (N, Typ);
8246 end Expand_N_Op_Minus;
8248 ---------------------
8249 -- Expand_N_Op_Mod --
8250 ---------------------
8252 procedure Expand_N_Op_Mod (N : Node_Id) is
8253 Loc : constant Source_Ptr := Sloc (N);
8254 Typ : constant Entity_Id := Etype (N);
8255 DDC : constant Boolean := Do_Division_Check (N);
8268 pragma Warnings (Off, Lhi);
8271 Binary_Op_Validity_Checks (N);
8273 -- Check for MINIMIZED/ELIMINATED overflow mode
8275 if Minimized_Eliminated_Overflow_Check (N) then
8276 Apply_Arithmetic_Overflow_Check (N);
8280 if Is_Integer_Type (Etype (N)) then
8281 Apply_Divide_Checks (N);
8283 -- All done if we don't have a MOD any more, which can happen as a
8284 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8286 if Nkind (N) /= N_Op_Mod then
8291 -- Proceed with expansion of mod operator
8293 Left := Left_Opnd (N);
8294 Right := Right_Opnd (N);
8296 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
8297 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
8299 -- Convert mod to rem if operands are both known to be non-negative, or
8300 -- both known to be non-positive (these are the cases in which rem and
8301 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
8302 -- likely that this will improve the quality of code, (the operation now
8303 -- corresponds to the hardware remainder), and it does not seem likely
8304 -- that it could be harmful. It also avoids some cases of the elaborate
8305 -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %).
8308 and then ((Llo >= 0 and then Rlo >= 0)
8310 (Lhi <= 0 and then Rhi <= 0))
8313 Make_Op_Rem (Sloc (N),
8314 Left_Opnd => Left_Opnd (N),
8315 Right_Opnd => Right_Opnd (N)));
8317 -- Instead of reanalyzing the node we do the analysis manually. This
8318 -- avoids anomalies when the replacement is done in an instance and
8319 -- is epsilon more efficient.
8321 Set_Entity (N, Standard_Entity (S_Op_Rem));
8323 Set_Do_Division_Check (N, DDC);
8324 Expand_N_Op_Rem (N);
8328 -- Otherwise, normal mod processing
8331 -- Apply optimization x mod 1 = 0. We don't really need that with
8332 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
8333 -- certainly harmless.
8335 if Is_Integer_Type (Etype (N))
8336 and then Compile_Time_Known_Value (Right)
8337 and then Expr_Value (Right) = Uint_1
8339 -- Call Remove_Side_Effects to ensure that any side effects in
8340 -- the ignored left operand (in particular function calls to
8341 -- user defined functions) are properly executed.
8343 Remove_Side_Effects (Left);
8345 Rewrite (N, Make_Integer_Literal (Loc, 0));
8346 Analyze_And_Resolve (N, Typ);
8350 -- If we still have a mod operator and we are in Modify_Tree_For_C
8351 -- mode, and we have a signed integer type, then here is where we do
8352 -- the rewrite in terms of Rem. Note this rewrite bypasses the need
8353 -- for the special handling of the annoying case of largest negative
8354 -- number mod minus one.
8356 if Nkind (N) = N_Op_Mod
8357 and then Is_Signed_Integer_Type (Typ)
8358 and then Modify_Tree_For_C
8360 -- In the general case, we expand A mod B as
8362 -- Tnn : constant typ := A rem B;
8364 -- (if (A >= 0) = (B >= 0) then Tnn
8365 -- elsif Tnn = 0 then 0
8368 -- The comparison can be written simply as A >= 0 if we know that
8369 -- B >= 0 which is a very common case.
8371 -- An important optimization is when B is known at compile time
8372 -- to be 2**K for some constant. In this case we can simply AND
8373 -- the left operand with the bit string 2**K-1 (i.e. K 1-bits)
8374 -- and that works for both the positive and negative cases.
8377 P2 : constant Nat := Power_Of_Two (Right);
8382 Unchecked_Convert_To (Typ,
8385 Unchecked_Convert_To
8386 (Corresponding_Unsigned_Type (Typ), Left),
8388 Make_Integer_Literal (Loc, 2 ** P2 - 1))));
8389 Analyze_And_Resolve (N, Typ);
8394 -- Here for the full rewrite
8397 Tnn : constant Entity_Id := Make_Temporary (Sloc (N), 'T', N);
8403 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
8404 Right_Opnd => Make_Integer_Literal (Loc, 0));
8406 if not LOK or else Rlo < 0 then
8412 Left_Opnd => Duplicate_Subexpr_No_Checks (Right),
8413 Right_Opnd => Make_Integer_Literal (Loc, 0)));
8417 Make_Object_Declaration (Loc,
8418 Defining_Identifier => Tnn,
8419 Constant_Present => True,
8420 Object_Definition => New_Occurrence_Of (Typ, Loc),
8424 Right_Opnd => Right)));
8427 Make_If_Expression (Loc,
8428 Expressions => New_List (
8430 New_Occurrence_Of (Tnn, Loc),
8431 Make_If_Expression (Loc,
8433 Expressions => New_List (
8435 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8436 Right_Opnd => Make_Integer_Literal (Loc, 0)),
8437 Make_Integer_Literal (Loc, 0),
8439 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8441 Duplicate_Subexpr_No_Checks (Right)))))));
8443 Analyze_And_Resolve (N, Typ);
8448 -- Deal with annoying case of largest negative number mod minus one.
8449 -- Gigi may not handle this case correctly, because on some targets,
8450 -- the mod value is computed using a divide instruction which gives
8451 -- an overflow trap for this case.
8453 -- It would be a bit more efficient to figure out which targets
8454 -- this is really needed for, but in practice it is reasonable
8455 -- to do the following special check in all cases, since it means
8456 -- we get a clearer message, and also the overhead is minimal given
8457 -- that division is expensive in any case.
8459 -- In fact the check is quite easy, if the right operand is -1, then
8460 -- the mod value is always 0, and we can just ignore the left operand
8461 -- completely in this case.
8463 -- This only applies if we still have a mod operator. Skip if we
8464 -- have already rewritten this (e.g. in the case of eliminated
8465 -- overflow checks which have driven us into bignum mode).
8467 if Nkind (N) = N_Op_Mod then
8469 -- The operand type may be private (e.g. in the expansion of an
8470 -- intrinsic operation) so we must use the underlying type to get
8471 -- the bounds, and convert the literals explicitly.
8475 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
8477 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
8478 and then ((not LOK) or else (Llo = LLB))
8481 Make_If_Expression (Loc,
8482 Expressions => New_List (
8484 Left_Opnd => Duplicate_Subexpr (Right),
8486 Unchecked_Convert_To (Typ,
8487 Make_Integer_Literal (Loc, -1))),
8488 Unchecked_Convert_To (Typ,
8489 Make_Integer_Literal (Loc, Uint_0)),
8490 Relocate_Node (N))));
8492 Set_Analyzed (Next (Next (First (Expressions (N)))));
8493 Analyze_And_Resolve (N, Typ);
8497 end Expand_N_Op_Mod;
8499 --------------------------
8500 -- Expand_N_Op_Multiply --
8501 --------------------------
8503 procedure Expand_N_Op_Multiply (N : Node_Id) is
8504 Loc : constant Source_Ptr := Sloc (N);
8505 Lop : constant Node_Id := Left_Opnd (N);
8506 Rop : constant Node_Id := Right_Opnd (N);
8508 Lp2 : constant Boolean :=
8509 Nkind (Lop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Lop);
8510 Rp2 : constant Boolean :=
8511 Nkind (Rop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Rop);
8513 Ltyp : constant Entity_Id := Etype (Lop);
8514 Rtyp : constant Entity_Id := Etype (Rop);
8515 Typ : Entity_Id := Etype (N);
8518 Binary_Op_Validity_Checks (N);
8520 -- Check for MINIMIZED/ELIMINATED overflow mode
8522 if Minimized_Eliminated_Overflow_Check (N) then
8523 Apply_Arithmetic_Overflow_Check (N);
8527 -- Special optimizations for integer types
8529 if Is_Integer_Type (Typ) then
8531 -- N * 0 = 0 for integer types
8533 if Compile_Time_Known_Value (Rop)
8534 and then Expr_Value (Rop) = Uint_0
8536 -- Call Remove_Side_Effects to ensure that any side effects in
8537 -- the ignored left operand (in particular function calls to
8538 -- user defined functions) are properly executed.
8540 Remove_Side_Effects (Lop);
8542 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8543 Analyze_And_Resolve (N, Typ);
8547 -- Similar handling for 0 * N = 0
8549 if Compile_Time_Known_Value (Lop)
8550 and then Expr_Value (Lop) = Uint_0
8552 Remove_Side_Effects (Rop);
8553 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8554 Analyze_And_Resolve (N, Typ);
8558 -- N * 1 = 1 * N = N for integer types
8560 -- This optimisation is not done if we are going to
8561 -- rewrite the product 1 * 2 ** N to a shift.
8563 if Compile_Time_Known_Value (Rop)
8564 and then Expr_Value (Rop) = Uint_1
8570 elsif Compile_Time_Known_Value (Lop)
8571 and then Expr_Value (Lop) = Uint_1
8579 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
8580 -- Is_Power_Of_2_For_Shift is set means that we know that our left
8581 -- operand is an integer, as required for this to work.
8586 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
8590 Left_Opnd => Make_Integer_Literal (Loc, 2),
8593 Left_Opnd => Right_Opnd (Lop),
8594 Right_Opnd => Right_Opnd (Rop))));
8595 Analyze_And_Resolve (N, Typ);
8599 -- If the result is modular, perform the reduction of the result
8602 if Is_Modular_Integer_Type (Typ)
8603 and then not Non_Binary_Modulus (Typ)
8608 Make_Op_Shift_Left (Loc,
8611 Convert_To (Standard_Natural, Right_Opnd (Rop))),
8613 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
8617 Make_Op_Shift_Left (Loc,
8620 Convert_To (Standard_Natural, Right_Opnd (Rop))));
8623 Analyze_And_Resolve (N, Typ);
8627 -- Same processing for the operands the other way round
8630 if Is_Modular_Integer_Type (Typ)
8631 and then not Non_Binary_Modulus (Typ)
8636 Make_Op_Shift_Left (Loc,
8639 Convert_To (Standard_Natural, Right_Opnd (Lop))),
8641 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
8645 Make_Op_Shift_Left (Loc,
8648 Convert_To (Standard_Natural, Right_Opnd (Lop))));
8651 Analyze_And_Resolve (N, Typ);
8655 -- Do required fixup of universal fixed operation
8657 if Typ = Universal_Fixed then
8658 Fixup_Universal_Fixed_Operation (N);
8662 -- Multiplications with fixed-point results
8664 if Is_Fixed_Point_Type (Typ) then
8666 -- No special processing if Treat_Fixed_As_Integer is set, since from
8667 -- a semantic point of view such operations are simply integer
8668 -- operations and will be treated that way.
8670 if not Treat_Fixed_As_Integer (N) then
8672 -- Case of fixed * integer => fixed
8674 if Is_Integer_Type (Rtyp) then
8675 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
8677 -- Case of integer * fixed => fixed
8679 elsif Is_Integer_Type (Ltyp) then
8680 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
8682 -- Case of fixed * fixed => fixed
8685 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
8689 -- Other cases of multiplication of fixed-point operands. Again we
8690 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
8692 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
8693 and then not Treat_Fixed_As_Integer (N)
8695 if Is_Integer_Type (Typ) then
8696 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
8698 pragma Assert (Is_Floating_Point_Type (Typ));
8699 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
8702 -- Mixed-mode operations can appear in a non-static universal context,
8703 -- in which case the integer argument must be converted explicitly.
8705 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
8706 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
8707 Analyze_And_Resolve (Rop, Universal_Real);
8709 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
8710 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
8711 Analyze_And_Resolve (Lop, Universal_Real);
8713 -- Non-fixed point cases, check software overflow checking required
8715 elsif Is_Signed_Integer_Type (Etype (N)) then
8716 Apply_Arithmetic_Overflow_Check (N);
8719 -- Overflow checks for floating-point if -gnateF mode active
8721 Check_Float_Op_Overflow (N);
8722 end Expand_N_Op_Multiply;
8724 --------------------
8725 -- Expand_N_Op_Ne --
8726 --------------------
8728 procedure Expand_N_Op_Ne (N : Node_Id) is
8729 Typ : constant Entity_Id := Etype (Left_Opnd (N));
8732 -- Case of elementary type with standard operator
8734 if Is_Elementary_Type (Typ)
8735 and then Sloc (Entity (N)) = Standard_Location
8737 Binary_Op_Validity_Checks (N);
8739 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
8740 -- means we no longer have a /= operation, we are all done.
8742 Expand_Compare_Minimize_Eliminate_Overflow (N);
8744 if Nkind (N) /= N_Op_Ne then
8748 -- Boolean types (requiring handling of non-standard case)
8750 if Is_Boolean_Type (Typ) then
8751 Adjust_Condition (Left_Opnd (N));
8752 Adjust_Condition (Right_Opnd (N));
8753 Set_Etype (N, Standard_Boolean);
8754 Adjust_Result_Type (N, Typ);
8757 Rewrite_Comparison (N);
8759 -- For all cases other than elementary types, we rewrite node as the
8760 -- negation of an equality operation, and reanalyze. The equality to be
8761 -- used is defined in the same scope and has the same signature. This
8762 -- signature must be set explicitly since in an instance it may not have
8763 -- the same visibility as in the generic unit. This avoids duplicating
8764 -- or factoring the complex code for record/array equality tests etc.
8768 Loc : constant Source_Ptr := Sloc (N);
8770 Ne : constant Entity_Id := Entity (N);
8773 Binary_Op_Validity_Checks (N);
8779 Left_Opnd => Left_Opnd (N),
8780 Right_Opnd => Right_Opnd (N)));
8781 Set_Paren_Count (Right_Opnd (Neg), 1);
8783 if Scope (Ne) /= Standard_Standard then
8784 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
8787 -- For navigation purposes, we want to treat the inequality as an
8788 -- implicit reference to the corresponding equality. Preserve the
8789 -- Comes_From_ source flag to generate proper Xref entries.
8791 Preserve_Comes_From_Source (Neg, N);
8792 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
8794 Analyze_And_Resolve (N, Standard_Boolean);
8798 Optimize_Length_Comparison (N);
8801 ---------------------
8802 -- Expand_N_Op_Not --
8803 ---------------------
8805 -- If the argument is other than a Boolean array type, there is no special
8806 -- expansion required, except for dealing with validity checks, and non-
8807 -- standard boolean representations.
8809 -- For the packed array case, we call the special routine in Exp_Pakd,
8810 -- except that if the component size is greater than one, we use the
8811 -- standard routine generating a gruesome loop (it is so peculiar to have
8812 -- packed arrays with non-standard Boolean representations anyway, so it
8813 -- does not matter that we do not handle this case efficiently).
8815 -- For the unpacked array case (and for the special packed case where we
8816 -- have non standard Booleans, as discussed above), we generate and insert
8817 -- into the tree the following function definition:
8819 -- function Nnnn (A : arr) is
8822 -- for J in a'range loop
8823 -- B (J) := not A (J);
8828 -- Here arr is the actual subtype of the parameter (and hence always
8829 -- constrained). Then we replace the not with a call to this function.
8831 procedure Expand_N_Op_Not (N : Node_Id) is
8832 Loc : constant Source_Ptr := Sloc (N);
8833 Typ : constant Entity_Id := Etype (N);
8842 Func_Name : Entity_Id;
8843 Loop_Statement : Node_Id;
8846 Unary_Op_Validity_Checks (N);
8848 -- For boolean operand, deal with non-standard booleans
8850 if Is_Boolean_Type (Typ) then
8851 Adjust_Condition (Right_Opnd (N));
8852 Set_Etype (N, Standard_Boolean);
8853 Adjust_Result_Type (N, Typ);
8857 -- Only array types need any other processing
8859 if not Is_Array_Type (Typ) then
8863 -- Case of array operand. If bit packed with a component size of 1,
8864 -- handle it in Exp_Pakd if the operand is known to be aligned.
8866 if Is_Bit_Packed_Array (Typ)
8867 and then Component_Size (Typ) = 1
8868 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
8870 Expand_Packed_Not (N);
8874 -- Case of array operand which is not bit-packed. If the context is
8875 -- a safe assignment, call in-place operation, If context is a larger
8876 -- boolean expression in the context of a safe assignment, expansion is
8877 -- done by enclosing operation.
8879 Opnd := Relocate_Node (Right_Opnd (N));
8880 Convert_To_Actual_Subtype (Opnd);
8881 Arr := Etype (Opnd);
8882 Ensure_Defined (Arr, N);
8883 Silly_Boolean_Array_Not_Test (N, Arr);
8885 if Nkind (Parent (N)) = N_Assignment_Statement then
8886 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
8887 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8890 -- Special case the negation of a binary operation
8892 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
8893 and then Safe_In_Place_Array_Op
8894 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
8896 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8900 elsif Nkind (Parent (N)) in N_Binary_Op
8901 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
8904 Op1 : constant Node_Id := Left_Opnd (Parent (N));
8905 Op2 : constant Node_Id := Right_Opnd (Parent (N));
8906 Lhs : constant Node_Id := Name (Parent (Parent (N)));
8909 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
8911 -- (not A) op (not B) can be reduced to a single call
8913 if N = Op1 and then Nkind (Op2) = N_Op_Not then
8916 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
8919 -- A xor (not B) can also be special-cased
8921 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
8928 A := Make_Defining_Identifier (Loc, Name_uA);
8929 B := Make_Defining_Identifier (Loc, Name_uB);
8930 J := Make_Defining_Identifier (Loc, Name_uJ);
8933 Make_Indexed_Component (Loc,
8934 Prefix => New_Occurrence_Of (A, Loc),
8935 Expressions => New_List (New_Occurrence_Of (J, Loc)));
8938 Make_Indexed_Component (Loc,
8939 Prefix => New_Occurrence_Of (B, Loc),
8940 Expressions => New_List (New_Occurrence_Of (J, Loc)));
8943 Make_Implicit_Loop_Statement (N,
8944 Identifier => Empty,
8947 Make_Iteration_Scheme (Loc,
8948 Loop_Parameter_Specification =>
8949 Make_Loop_Parameter_Specification (Loc,
8950 Defining_Identifier => J,
8951 Discrete_Subtype_Definition =>
8952 Make_Attribute_Reference (Loc,
8953 Prefix => Make_Identifier (Loc, Chars (A)),
8954 Attribute_Name => Name_Range))),
8956 Statements => New_List (
8957 Make_Assignment_Statement (Loc,
8959 Expression => Make_Op_Not (Loc, A_J))));
8961 Func_Name := Make_Temporary (Loc, 'N');
8962 Set_Is_Inlined (Func_Name);
8965 Make_Subprogram_Body (Loc,
8967 Make_Function_Specification (Loc,
8968 Defining_Unit_Name => Func_Name,
8969 Parameter_Specifications => New_List (
8970 Make_Parameter_Specification (Loc,
8971 Defining_Identifier => A,
8972 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8973 Result_Definition => New_Occurrence_Of (Typ, Loc)),
8975 Declarations => New_List (
8976 Make_Object_Declaration (Loc,
8977 Defining_Identifier => B,
8978 Object_Definition => New_Occurrence_Of (Arr, Loc))),
8980 Handled_Statement_Sequence =>
8981 Make_Handled_Sequence_Of_Statements (Loc,
8982 Statements => New_List (
8984 Make_Simple_Return_Statement (Loc,
8985 Expression => Make_Identifier (Loc, Chars (B)))))));
8988 Make_Function_Call (Loc,
8989 Name => New_Occurrence_Of (Func_Name, Loc),
8990 Parameter_Associations => New_List (Opnd)));
8992 Analyze_And_Resolve (N, Typ);
8993 end Expand_N_Op_Not;
8995 --------------------
8996 -- Expand_N_Op_Or --
8997 --------------------
8999 procedure Expand_N_Op_Or (N : Node_Id) is
9000 Typ : constant Entity_Id := Etype (N);
9003 Binary_Op_Validity_Checks (N);
9005 if Is_Array_Type (Etype (N)) then
9006 Expand_Boolean_Operator (N);
9008 elsif Is_Boolean_Type (Etype (N)) then
9009 Adjust_Condition (Left_Opnd (N));
9010 Adjust_Condition (Right_Opnd (N));
9011 Set_Etype (N, Standard_Boolean);
9012 Adjust_Result_Type (N, Typ);
9014 elsif Is_Intrinsic_Subprogram (Entity (N)) then
9015 Expand_Intrinsic_Call (N, Entity (N));
9020 ----------------------
9021 -- Expand_N_Op_Plus --
9022 ----------------------
9024 procedure Expand_N_Op_Plus (N : Node_Id) is
9026 Unary_Op_Validity_Checks (N);
9028 -- Check for MINIMIZED/ELIMINATED overflow mode
9030 if Minimized_Eliminated_Overflow_Check (N) then
9031 Apply_Arithmetic_Overflow_Check (N);
9034 end Expand_N_Op_Plus;
9036 ---------------------
9037 -- Expand_N_Op_Rem --
9038 ---------------------
9040 procedure Expand_N_Op_Rem (N : Node_Id) is
9041 Loc : constant Source_Ptr := Sloc (N);
9042 Typ : constant Entity_Id := Etype (N);
9053 -- Set if corresponding operand can be negative
9055 pragma Unreferenced (Hi);
9058 Binary_Op_Validity_Checks (N);
9060 -- Check for MINIMIZED/ELIMINATED overflow mode
9062 if Minimized_Eliminated_Overflow_Check (N) then
9063 Apply_Arithmetic_Overflow_Check (N);
9067 if Is_Integer_Type (Etype (N)) then
9068 Apply_Divide_Checks (N);
9070 -- All done if we don't have a REM any more, which can happen as a
9071 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
9073 if Nkind (N) /= N_Op_Rem then
9078 -- Proceed with expansion of REM
9080 Left := Left_Opnd (N);
9081 Right := Right_Opnd (N);
9083 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
9084 -- but it is useful with other back ends (e.g. AAMP), and is certainly
9087 if Is_Integer_Type (Etype (N))
9088 and then Compile_Time_Known_Value (Right)
9089 and then Expr_Value (Right) = Uint_1
9091 -- Call Remove_Side_Effects to ensure that any side effects in the
9092 -- ignored left operand (in particular function calls to user defined
9093 -- functions) are properly executed.
9095 Remove_Side_Effects (Left);
9097 Rewrite (N, Make_Integer_Literal (Loc, 0));
9098 Analyze_And_Resolve (N, Typ);
9102 -- Deal with annoying case of largest negative number remainder minus
9103 -- one. Gigi may not handle this case correctly, because on some
9104 -- targets, the mod value is computed using a divide instruction
9105 -- which gives an overflow trap for this case.
9107 -- It would be a bit more efficient to figure out which targets this
9108 -- is really needed for, but in practice it is reasonable to do the
9109 -- following special check in all cases, since it means we get a clearer
9110 -- message, and also the overhead is minimal given that division is
9111 -- expensive in any case.
9113 -- In fact the check is quite easy, if the right operand is -1, then
9114 -- the remainder is always 0, and we can just ignore the left operand
9115 -- completely in this case.
9117 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
9118 Lneg := (not OK) or else Lo < 0;
9120 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
9121 Rneg := (not OK) or else Lo < 0;
9123 -- We won't mess with trying to find out if the left operand can really
9124 -- be the largest negative number (that's a pain in the case of private
9125 -- types and this is really marginal). We will just assume that we need
9126 -- the test if the left operand can be negative at all.
9128 if Lneg and Rneg then
9130 Make_If_Expression (Loc,
9131 Expressions => New_List (
9133 Left_Opnd => Duplicate_Subexpr (Right),
9135 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
9137 Unchecked_Convert_To (Typ,
9138 Make_Integer_Literal (Loc, Uint_0)),
9140 Relocate_Node (N))));
9142 Set_Analyzed (Next (Next (First (Expressions (N)))));
9143 Analyze_And_Resolve (N, Typ);
9145 end Expand_N_Op_Rem;
9147 -----------------------------
9148 -- Expand_N_Op_Rotate_Left --
9149 -----------------------------
9151 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
9153 Binary_Op_Validity_Checks (N);
9155 -- If we are in Modify_Tree_For_C mode, there is no rotate left in C,
9156 -- so we rewrite in terms of logical shifts
9158 -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits)
9160 -- where Bits is the shift count mod Esize (the mod operation here
9161 -- deals with ludicrous large shift counts, which are apparently OK).
9163 -- What about nonbinary modulus ???
9166 Loc : constant Source_Ptr := Sloc (N);
9167 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
9168 Typ : constant Entity_Id := Etype (N);
9171 if Modify_Tree_For_C then
9172 Rewrite (Right_Opnd (N),
9174 Left_Opnd => Relocate_Node (Right_Opnd (N)),
9175 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
9177 Analyze_And_Resolve (Right_Opnd (N), Rtp);
9182 Make_Op_Shift_Left (Loc,
9183 Left_Opnd => Left_Opnd (N),
9184 Right_Opnd => Right_Opnd (N)),
9187 Make_Op_Shift_Right (Loc,
9188 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
9190 Make_Op_Subtract (Loc,
9191 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
9193 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
9195 Analyze_And_Resolve (N, Typ);
9198 end Expand_N_Op_Rotate_Left;
9200 ------------------------------
9201 -- Expand_N_Op_Rotate_Right --
9202 ------------------------------
9204 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
9206 Binary_Op_Validity_Checks (N);
9208 -- If we are in Modify_Tree_For_C mode, there is no rotate right in C,
9209 -- so we rewrite in terms of logical shifts
9211 -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits)
9213 -- where Bits is the shift count mod Esize (the mod operation here
9214 -- deals with ludicrous large shift counts, which are apparently OK).
9216 -- What about nonbinary modulus ???
9219 Loc : constant Source_Ptr := Sloc (N);
9220 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
9221 Typ : constant Entity_Id := Etype (N);
9224 Rewrite (Right_Opnd (N),
9226 Left_Opnd => Relocate_Node (Right_Opnd (N)),
9227 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
9229 Analyze_And_Resolve (Right_Opnd (N), Rtp);
9231 if Modify_Tree_For_C then
9235 Make_Op_Shift_Right (Loc,
9236 Left_Opnd => Left_Opnd (N),
9237 Right_Opnd => Right_Opnd (N)),
9240 Make_Op_Shift_Left (Loc,
9241 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
9243 Make_Op_Subtract (Loc,
9244 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
9246 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
9248 Analyze_And_Resolve (N, Typ);
9251 end Expand_N_Op_Rotate_Right;
9253 ----------------------------
9254 -- Expand_N_Op_Shift_Left --
9255 ----------------------------
9257 -- Note: nothing in this routine depends on left as opposed to right shifts
9258 -- so we share the routine for expanding shift right operations.
9260 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
9262 Binary_Op_Validity_Checks (N);
9264 -- If we are in Modify_Tree_For_C mode, then ensure that the right
9265 -- operand is not greater than the word size (since that would not
9266 -- be defined properly by the corresponding C shift operator).
9268 if Modify_Tree_For_C then
9270 Right : constant Node_Id := Right_Opnd (N);
9271 Loc : constant Source_Ptr := Sloc (Right);
9272 Typ : constant Entity_Id := Etype (N);
9273 Siz : constant Uint := Esize (Typ);
9280 if Compile_Time_Known_Value (Right) then
9281 if Expr_Value (Right) >= Siz then
9282 Rewrite (N, Make_Integer_Literal (Loc, 0));
9283 Analyze_And_Resolve (N, Typ);
9286 -- Not compile time known, find range
9289 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
9291 -- Nothing to do if known to be OK range, otherwise expand
9293 if not OK or else Hi >= Siz then
9295 -- Prevent recursion on copy of shift node
9297 Orig := Relocate_Node (N);
9298 Set_Analyzed (Orig);
9300 -- Now do the rewrite
9303 Make_If_Expression (Loc,
9304 Expressions => New_List (
9306 Left_Opnd => Duplicate_Subexpr_Move_Checks (Right),
9307 Right_Opnd => Make_Integer_Literal (Loc, Siz)),
9308 Make_Integer_Literal (Loc, 0),
9310 Analyze_And_Resolve (N, Typ);
9315 end Expand_N_Op_Shift_Left;
9317 -----------------------------
9318 -- Expand_N_Op_Shift_Right --
9319 -----------------------------
9321 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
9323 -- Share shift left circuit
9325 Expand_N_Op_Shift_Left (N);
9326 end Expand_N_Op_Shift_Right;
9328 ----------------------------------------
9329 -- Expand_N_Op_Shift_Right_Arithmetic --
9330 ----------------------------------------
9332 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
9334 Binary_Op_Validity_Checks (N);
9336 -- If we are in Modify_Tree_For_C mode, there is no shift right
9337 -- arithmetic in C, so we rewrite in terms of logical shifts.
9339 -- Shift_Right (Num, Bits) or
9341 -- then not (Shift_Right (Mask, bits))
9344 -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1)
9346 -- Note: in almost all C compilers it would work to just shift a
9347 -- signed integer right, but it's undefined and we cannot rely on it.
9349 -- Note: the above works fine for shift counts greater than or equal
9350 -- to the word size, since in this case (not (Shift_Right (Mask, bits)))
9351 -- generates all 1'bits.
9353 -- What about nonbinary modulus ???
9356 Loc : constant Source_Ptr := Sloc (N);
9357 Typ : constant Entity_Id := Etype (N);
9358 Sign : constant Uint := 2 ** (Esize (Typ) - 1);
9359 Mask : constant Uint := (2 ** Esize (Typ)) - 1;
9360 Left : constant Node_Id := Left_Opnd (N);
9361 Right : constant Node_Id := Right_Opnd (N);
9365 if Modify_Tree_For_C then
9367 -- Here if not (Shift_Right (Mask, bits)) can be computed at
9368 -- compile time as a single constant.
9370 if Compile_Time_Known_Value (Right) then
9372 Val : constant Uint := Expr_Value (Right);
9375 if Val >= Esize (Typ) then
9376 Maskx := Make_Integer_Literal (Loc, Mask);
9380 Make_Integer_Literal (Loc,
9381 Intval => Mask - (Mask / (2 ** Expr_Value (Right))));
9389 Make_Op_Shift_Right (Loc,
9390 Left_Opnd => Make_Integer_Literal (Loc, Mask),
9391 Right_Opnd => Duplicate_Subexpr_No_Checks (Right)));
9394 -- Now do the rewrite
9399 Make_Op_Shift_Right (Loc,
9401 Right_Opnd => Right),
9403 Make_If_Expression (Loc,
9404 Expressions => New_List (
9406 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
9407 Right_Opnd => Make_Integer_Literal (Loc, Sign)),
9409 Make_Integer_Literal (Loc, 0)))));
9410 Analyze_And_Resolve (N, Typ);
9413 end Expand_N_Op_Shift_Right_Arithmetic;
9415 --------------------------
9416 -- Expand_N_Op_Subtract --
9417 --------------------------
9419 procedure Expand_N_Op_Subtract (N : Node_Id) is
9420 Typ : constant Entity_Id := Etype (N);
9423 Binary_Op_Validity_Checks (N);
9425 -- Check for MINIMIZED/ELIMINATED overflow mode
9427 if Minimized_Eliminated_Overflow_Check (N) then
9428 Apply_Arithmetic_Overflow_Check (N);
9432 -- N - 0 = N for integer types
9434 if Is_Integer_Type (Typ)
9435 and then Compile_Time_Known_Value (Right_Opnd (N))
9436 and then Expr_Value (Right_Opnd (N)) = 0
9438 Rewrite (N, Left_Opnd (N));
9442 -- Arithmetic overflow checks for signed integer/fixed point types
9444 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
9445 Apply_Arithmetic_Overflow_Check (N);
9448 -- Overflow checks for floating-point if -gnateF mode active
9450 Check_Float_Op_Overflow (N);
9451 end Expand_N_Op_Subtract;
9453 ---------------------
9454 -- Expand_N_Op_Xor --
9455 ---------------------
9457 procedure Expand_N_Op_Xor (N : Node_Id) is
9458 Typ : constant Entity_Id := Etype (N);
9461 Binary_Op_Validity_Checks (N);
9463 if Is_Array_Type (Etype (N)) then
9464 Expand_Boolean_Operator (N);
9466 elsif Is_Boolean_Type (Etype (N)) then
9467 Adjust_Condition (Left_Opnd (N));
9468 Adjust_Condition (Right_Opnd (N));
9469 Set_Etype (N, Standard_Boolean);
9470 Adjust_Result_Type (N, Typ);
9472 elsif Is_Intrinsic_Subprogram (Entity (N)) then
9473 Expand_Intrinsic_Call (N, Entity (N));
9476 end Expand_N_Op_Xor;
9478 ----------------------
9479 -- Expand_N_Or_Else --
9480 ----------------------
9482 procedure Expand_N_Or_Else (N : Node_Id)
9483 renames Expand_Short_Circuit_Operator;
9485 -----------------------------------
9486 -- Expand_N_Qualified_Expression --
9487 -----------------------------------
9489 procedure Expand_N_Qualified_Expression (N : Node_Id) is
9490 Operand : constant Node_Id := Expression (N);
9491 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
9494 -- Do validity check if validity checking operands
9496 if Validity_Checks_On and Validity_Check_Operands then
9497 Ensure_Valid (Operand);
9500 -- Apply possible constraint check
9502 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
9504 if Do_Range_Check (Operand) then
9505 Set_Do_Range_Check (Operand, False);
9506 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
9508 end Expand_N_Qualified_Expression;
9510 ------------------------------------
9511 -- Expand_N_Quantified_Expression --
9512 ------------------------------------
9516 -- for all X in range => Cond
9521 -- for X in range loop
9528 -- Similarly, an existentially quantified expression:
9530 -- for some X in range => Cond
9535 -- for X in range loop
9542 -- In both cases, the iteration may be over a container in which case it is
9543 -- given by an iterator specification, not a loop parameter specification.
9545 procedure Expand_N_Quantified_Expression (N : Node_Id) is
9546 Actions : constant List_Id := New_List;
9547 For_All : constant Boolean := All_Present (N);
9548 Iter_Spec : constant Node_Id := Iterator_Specification (N);
9549 Loc : constant Source_Ptr := Sloc (N);
9550 Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N);
9557 -- Create the declaration of the flag which tracks the status of the
9558 -- quantified expression. Generate:
9560 -- Flag : Boolean := (True | False);
9562 Flag := Make_Temporary (Loc, 'T', N);
9565 Make_Object_Declaration (Loc,
9566 Defining_Identifier => Flag,
9567 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
9569 New_Occurrence_Of (Boolean_Literals (For_All), Loc)));
9571 -- Construct the circuitry which tracks the status of the quantified
9572 -- expression. Generate:
9574 -- if [not] Cond then
9575 -- Flag := (False | True);
9579 Cond := Relocate_Node (Condition (N));
9582 Cond := Make_Op_Not (Loc, Cond);
9586 Make_Implicit_If_Statement (N,
9588 Then_Statements => New_List (
9589 Make_Assignment_Statement (Loc,
9590 Name => New_Occurrence_Of (Flag, Loc),
9592 New_Occurrence_Of (Boolean_Literals (not For_All), Loc)),
9593 Make_Exit_Statement (Loc))));
9595 -- Build the loop equivalent of the quantified expression
9597 if Present (Iter_Spec) then
9599 Make_Iteration_Scheme (Loc,
9600 Iterator_Specification => Iter_Spec);
9603 Make_Iteration_Scheme (Loc,
9604 Loop_Parameter_Specification => Loop_Spec);
9608 Make_Loop_Statement (Loc,
9609 Iteration_Scheme => Scheme,
9610 Statements => Stmts,
9611 End_Label => Empty));
9613 -- Transform the quantified expression
9616 Make_Expression_With_Actions (Loc,
9617 Expression => New_Occurrence_Of (Flag, Loc),
9618 Actions => Actions));
9619 Analyze_And_Resolve (N, Standard_Boolean);
9620 end Expand_N_Quantified_Expression;
9622 ---------------------------------
9623 -- Expand_N_Selected_Component --
9624 ---------------------------------
9626 procedure Expand_N_Selected_Component (N : Node_Id) is
9627 Loc : constant Source_Ptr := Sloc (N);
9628 Par : constant Node_Id := Parent (N);
9629 P : constant Node_Id := Prefix (N);
9630 S : constant Node_Id := Selector_Name (N);
9631 Ptyp : Entity_Id := Underlying_Type (Etype (P));
9637 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
9638 -- Gigi needs a temporary for prefixes that depend on a discriminant,
9639 -- unless the context of an assignment can provide size information.
9640 -- Don't we have a general routine that does this???
9642 function Is_Subtype_Declaration return Boolean;
9643 -- The replacement of a discriminant reference by its value is required
9644 -- if this is part of the initialization of an temporary generated by a
9645 -- change of representation. This shows up as the construction of a
9646 -- discriminant constraint for a subtype declared at the same point as
9647 -- the entity in the prefix of the selected component. We recognize this
9648 -- case when the context of the reference is:
9649 -- subtype ST is T(Obj.D);
9650 -- where the entity for Obj comes from source, and ST has the same sloc.
9652 -----------------------
9653 -- In_Left_Hand_Side --
9654 -----------------------
9656 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
9658 return (Nkind (Parent (Comp)) = N_Assignment_Statement
9659 and then Comp = Name (Parent (Comp)))
9660 or else (Present (Parent (Comp))
9661 and then Nkind (Parent (Comp)) in N_Subexpr
9662 and then In_Left_Hand_Side (Parent (Comp)));
9663 end In_Left_Hand_Side;
9665 -----------------------------
9666 -- Is_Subtype_Declaration --
9667 -----------------------------
9669 function Is_Subtype_Declaration return Boolean is
9670 Par : constant Node_Id := Parent (N);
9673 Nkind (Par) = N_Index_Or_Discriminant_Constraint
9674 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
9675 and then Comes_From_Source (Entity (Prefix (N)))
9676 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
9677 end Is_Subtype_Declaration;
9679 -- Start of processing for Expand_N_Selected_Component
9682 -- Insert explicit dereference if required
9684 if Is_Access_Type (Ptyp) then
9686 -- First set prefix type to proper access type, in case it currently
9687 -- has a private (non-access) view of this type.
9689 Set_Etype (P, Ptyp);
9691 Insert_Explicit_Dereference (P);
9692 Analyze_And_Resolve (P, Designated_Type (Ptyp));
9694 if Ekind (Etype (P)) = E_Private_Subtype
9695 and then Is_For_Access_Subtype (Etype (P))
9697 Set_Etype (P, Base_Type (Etype (P)));
9703 -- Deal with discriminant check required
9705 if Do_Discriminant_Check (N) then
9706 if Present (Discriminant_Checking_Func
9707 (Original_Record_Component (Entity (S))))
9709 -- Present the discriminant checking function to the backend, so
9710 -- that it can inline the call to the function.
9713 (Discriminant_Checking_Func
9714 (Original_Record_Component (Entity (S))),
9717 -- Now reset the flag and generate the call
9719 Set_Do_Discriminant_Check (N, False);
9720 Generate_Discriminant_Check (N);
9722 -- In the case of Unchecked_Union, no discriminant checking is
9723 -- actually performed.
9726 Set_Do_Discriminant_Check (N, False);
9730 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9731 -- function, then additional actuals must be passed.
9733 if Ada_Version >= Ada_2005
9734 and then Is_Build_In_Place_Function_Call (P)
9736 Make_Build_In_Place_Call_In_Anonymous_Context (P);
9739 -- Gigi cannot handle unchecked conversions that are the prefix of a
9740 -- selected component with discriminants. This must be checked during
9741 -- expansion, because during analysis the type of the selector is not
9742 -- known at the point the prefix is analyzed. If the conversion is the
9743 -- target of an assignment, then we cannot force the evaluation.
9745 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
9746 and then Has_Discriminants (Etype (N))
9747 and then not In_Left_Hand_Side (N)
9749 Force_Evaluation (Prefix (N));
9752 -- Remaining processing applies only if selector is a discriminant
9754 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
9756 -- If the selector is a discriminant of a constrained record type,
9757 -- we may be able to rewrite the expression with the actual value
9758 -- of the discriminant, a useful optimization in some cases.
9760 if Is_Record_Type (Ptyp)
9761 and then Has_Discriminants (Ptyp)
9762 and then Is_Constrained (Ptyp)
9764 -- Do this optimization for discrete types only, and not for
9765 -- access types (access discriminants get us into trouble).
9767 if not Is_Discrete_Type (Etype (N)) then
9770 -- Don't do this on the left hand of an assignment statement.
9771 -- Normally one would think that references like this would not
9772 -- occur, but they do in generated code, and mean that we really
9773 -- do want to assign the discriminant.
9775 elsif Nkind (Par) = N_Assignment_Statement
9776 and then Name (Par) = N
9780 -- Don't do this optimization for the prefix of an attribute or
9781 -- the name of an object renaming declaration since these are
9782 -- contexts where we do not want the value anyway.
9784 elsif (Nkind (Par) = N_Attribute_Reference
9785 and then Prefix (Par) = N)
9786 or else Is_Renamed_Object (N)
9790 -- Don't do this optimization if we are within the code for a
9791 -- discriminant check, since the whole point of such a check may
9792 -- be to verify the condition on which the code below depends.
9794 elsif Is_In_Discriminant_Check (N) then
9797 -- Green light to see if we can do the optimization. There is
9798 -- still one condition that inhibits the optimization below but
9799 -- now is the time to check the particular discriminant.
9802 -- Loop through discriminants to find the matching discriminant
9803 -- constraint to see if we can copy it.
9805 Disc := First_Discriminant (Ptyp);
9806 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
9807 Discr_Loop : while Present (Dcon) loop
9808 Dval := Node (Dcon);
9810 -- Check if this is the matching discriminant and if the
9811 -- discriminant value is simple enough to make sense to
9812 -- copy. We don't want to copy complex expressions, and
9813 -- indeed to do so can cause trouble (before we put in
9814 -- this guard, a discriminant expression containing an
9815 -- AND THEN was copied, causing problems for coverage
9818 -- However, if the reference is part of the initialization
9819 -- code generated for an object declaration, we must use
9820 -- the discriminant value from the subtype constraint,
9821 -- because the selected component may be a reference to the
9822 -- object being initialized, whose discriminant is not yet
9823 -- set. This only happens in complex cases involving changes
9824 -- or representation.
9826 if Disc = Entity (Selector_Name (N))
9827 and then (Is_Entity_Name (Dval)
9828 or else Compile_Time_Known_Value (Dval)
9829 or else Is_Subtype_Declaration)
9831 -- Here we have the matching discriminant. Check for
9832 -- the case of a discriminant of a component that is
9833 -- constrained by an outer discriminant, which cannot
9834 -- be optimized away.
9836 if Denotes_Discriminant
9837 (Dval, Check_Concurrent => True)
9841 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
9843 Denotes_Discriminant
9844 (Selector_Name (Original_Node (Dval)), True)
9848 -- Do not retrieve value if constraint is not static. It
9849 -- is generally not useful, and the constraint may be a
9850 -- rewritten outer discriminant in which case it is in
9853 elsif Is_Entity_Name (Dval)
9855 Nkind (Parent (Entity (Dval))) = N_Object_Declaration
9856 and then Present (Expression (Parent (Entity (Dval))))
9858 Is_OK_Static_Expression
9859 (Expression (Parent (Entity (Dval))))
9863 -- In the context of a case statement, the expression may
9864 -- have the base type of the discriminant, and we need to
9865 -- preserve the constraint to avoid spurious errors on
9868 elsif Nkind (Parent (N)) = N_Case_Statement
9869 and then Etype (Dval) /= Etype (Disc)
9872 Make_Qualified_Expression (Loc,
9874 New_Occurrence_Of (Etype (Disc), Loc),
9876 New_Copy_Tree (Dval)));
9877 Analyze_And_Resolve (N, Etype (Disc));
9879 -- In case that comes out as a static expression,
9880 -- reset it (a selected component is never static).
9882 Set_Is_Static_Expression (N, False);
9885 -- Otherwise we can just copy the constraint, but the
9886 -- result is certainly not static. In some cases the
9887 -- discriminant constraint has been analyzed in the
9888 -- context of the original subtype indication, but for
9889 -- itypes the constraint might not have been analyzed
9890 -- yet, and this must be done now.
9893 Rewrite (N, New_Copy_Tree (Dval));
9894 Analyze_And_Resolve (N);
9895 Set_Is_Static_Expression (N, False);
9901 Next_Discriminant (Disc);
9902 end loop Discr_Loop;
9904 -- Note: the above loop should always find a matching
9905 -- discriminant, but if it does not, we just missed an
9906 -- optimization due to some glitch (perhaps a previous
9907 -- error), so ignore.
9912 -- The only remaining processing is in the case of a discriminant of
9913 -- a concurrent object, where we rewrite the prefix to denote the
9914 -- corresponding record type. If the type is derived and has renamed
9915 -- discriminants, use corresponding discriminant, which is the one
9916 -- that appears in the corresponding record.
9918 if not Is_Concurrent_Type (Ptyp) then
9922 Disc := Entity (Selector_Name (N));
9924 if Is_Derived_Type (Ptyp)
9925 and then Present (Corresponding_Discriminant (Disc))
9927 Disc := Corresponding_Discriminant (Disc);
9931 Make_Selected_Component (Loc,
9933 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
9935 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
9941 -- Set Atomic_Sync_Required if necessary for atomic component
9943 if Nkind (N) = N_Selected_Component then
9945 E : constant Entity_Id := Entity (Selector_Name (N));
9949 -- If component is atomic, but type is not, setting depends on
9950 -- disable/enable state for the component.
9952 if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then
9953 Set := not Atomic_Synchronization_Disabled (E);
9955 -- If component is not atomic, but its type is atomic, setting
9956 -- depends on disable/enable state for the type.
9958 elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9959 Set := not Atomic_Synchronization_Disabled (Etype (E));
9961 -- If both component and type are atomic, we disable if either
9962 -- component or its type have sync disabled.
9964 elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9965 Set := (not Atomic_Synchronization_Disabled (E))
9967 (not Atomic_Synchronization_Disabled (Etype (E)));
9973 -- Set flag if required
9976 Activate_Atomic_Synchronization (N);
9980 end Expand_N_Selected_Component;
9982 --------------------
9983 -- Expand_N_Slice --
9984 --------------------
9986 procedure Expand_N_Slice (N : Node_Id) is
9987 Loc : constant Source_Ptr := Sloc (N);
9988 Typ : constant Entity_Id := Etype (N);
9990 function Is_Procedure_Actual (N : Node_Id) return Boolean;
9991 -- Check whether the argument is an actual for a procedure call, in
9992 -- which case the expansion of a bit-packed slice is deferred until the
9993 -- call itself is expanded. The reason this is required is that we might
9994 -- have an IN OUT or OUT parameter, and the copy out is essential, and
9995 -- that copy out would be missed if we created a temporary here in
9996 -- Expand_N_Slice. Note that we don't bother to test specifically for an
9997 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
9998 -- is harmless to defer expansion in the IN case, since the call
9999 -- processing will still generate the appropriate copy in operation,
10000 -- which will take care of the slice.
10002 procedure Make_Temporary_For_Slice;
10003 -- Create a named variable for the value of the slice, in cases where
10004 -- the back-end cannot handle it properly, e.g. when packed types or
10005 -- unaligned slices are involved.
10007 -------------------------
10008 -- Is_Procedure_Actual --
10009 -------------------------
10011 function Is_Procedure_Actual (N : Node_Id) return Boolean is
10012 Par : Node_Id := Parent (N);
10016 -- If our parent is a procedure call we can return
10018 if Nkind (Par) = N_Procedure_Call_Statement then
10021 -- If our parent is a type conversion, keep climbing the tree,
10022 -- since a type conversion can be a procedure actual. Also keep
10023 -- climbing if parameter association or a qualified expression,
10024 -- since these are additional cases that do can appear on
10025 -- procedure actuals.
10027 elsif Nkind_In (Par, N_Type_Conversion,
10028 N_Parameter_Association,
10029 N_Qualified_Expression)
10031 Par := Parent (Par);
10033 -- Any other case is not what we are looking for
10039 end Is_Procedure_Actual;
10041 ------------------------------
10042 -- Make_Temporary_For_Slice --
10043 ------------------------------
10045 procedure Make_Temporary_For_Slice is
10046 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
10051 Make_Object_Declaration (Loc,
10052 Defining_Identifier => Ent,
10053 Object_Definition => New_Occurrence_Of (Typ, Loc));
10055 Set_No_Initialization (Decl);
10057 Insert_Actions (N, New_List (
10059 Make_Assignment_Statement (Loc,
10060 Name => New_Occurrence_Of (Ent, Loc),
10061 Expression => Relocate_Node (N))));
10063 Rewrite (N, New_Occurrence_Of (Ent, Loc));
10064 Analyze_And_Resolve (N, Typ);
10065 end Make_Temporary_For_Slice;
10069 Pref : constant Node_Id := Prefix (N);
10070 Pref_Typ : Entity_Id := Etype (Pref);
10072 -- Start of processing for Expand_N_Slice
10075 -- Special handling for access types
10077 if Is_Access_Type (Pref_Typ) then
10078 Pref_Typ := Designated_Type (Pref_Typ);
10081 Make_Explicit_Dereference (Sloc (N),
10082 Prefix => Relocate_Node (Pref)));
10084 Analyze_And_Resolve (Pref, Pref_Typ);
10087 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
10088 -- function, then additional actuals must be passed.
10090 if Ada_Version >= Ada_2005
10091 and then Is_Build_In_Place_Function_Call (Pref)
10093 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
10096 -- The remaining case to be handled is packed slices. We can leave
10097 -- packed slices as they are in the following situations:
10099 -- 1. Right or left side of an assignment (we can handle this
10100 -- situation correctly in the assignment statement expansion).
10102 -- 2. Prefix of indexed component (the slide is optimized away in this
10103 -- case, see the start of Expand_N_Slice.)
10105 -- 3. Object renaming declaration, since we want the name of the
10106 -- slice, not the value.
10108 -- 4. Argument to procedure call, since copy-in/copy-out handling may
10109 -- be required, and this is handled in the expansion of call
10112 -- 5. Prefix of an address attribute (this is an error which is caught
10113 -- elsewhere, and the expansion would interfere with generating the
10116 if not Is_Packed (Typ) then
10118 -- Apply transformation for actuals of a function call, where
10119 -- Expand_Actuals is not used.
10121 if Nkind (Parent (N)) = N_Function_Call
10122 and then Is_Possibly_Unaligned_Slice (N)
10124 Make_Temporary_For_Slice;
10127 elsif Nkind (Parent (N)) = N_Assignment_Statement
10128 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
10129 and then Parent (N) = Name (Parent (Parent (N))))
10133 elsif Nkind (Parent (N)) = N_Indexed_Component
10134 or else Is_Renamed_Object (N)
10135 or else Is_Procedure_Actual (N)
10139 elsif Nkind (Parent (N)) = N_Attribute_Reference
10140 and then Attribute_Name (Parent (N)) = Name_Address
10145 Make_Temporary_For_Slice;
10147 end Expand_N_Slice;
10149 ------------------------------
10150 -- Expand_N_Type_Conversion --
10151 ------------------------------
10153 procedure Expand_N_Type_Conversion (N : Node_Id) is
10154 Loc : constant Source_Ptr := Sloc (N);
10155 Operand : constant Node_Id := Expression (N);
10156 Target_Type : constant Entity_Id := Etype (N);
10157 Operand_Type : Entity_Id := Etype (Operand);
10159 procedure Handle_Changed_Representation;
10160 -- This is called in the case of record and array type conversions to
10161 -- see if there is a change of representation to be handled. Change of
10162 -- representation is actually handled at the assignment statement level,
10163 -- and what this procedure does is rewrite node N conversion as an
10164 -- assignment to temporary. If there is no change of representation,
10165 -- then the conversion node is unchanged.
10167 procedure Raise_Accessibility_Error;
10168 -- Called when we know that an accessibility check will fail. Rewrites
10169 -- node N to an appropriate raise statement and outputs warning msgs.
10170 -- The Etype of the raise node is set to Target_Type. Note that in this
10171 -- case the rest of the processing should be skipped (i.e. the call to
10172 -- this procedure will be followed by "goto Done").
10174 procedure Real_Range_Check;
10175 -- Handles generation of range check for real target value
10177 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean;
10178 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
10179 -- evaluates to True.
10181 -----------------------------------
10182 -- Handle_Changed_Representation --
10183 -----------------------------------
10185 procedure Handle_Changed_Representation is
10194 -- Nothing else to do if no change of representation
10196 if Same_Representation (Operand_Type, Target_Type) then
10199 -- The real change of representation work is done by the assignment
10200 -- statement processing. So if this type conversion is appearing as
10201 -- the expression of an assignment statement, nothing needs to be
10202 -- done to the conversion.
10204 elsif Nkind (Parent (N)) = N_Assignment_Statement then
10207 -- Otherwise we need to generate a temporary variable, and do the
10208 -- change of representation assignment into that temporary variable.
10209 -- The conversion is then replaced by a reference to this variable.
10214 -- If type is unconstrained we have to add a constraint, copied
10215 -- from the actual value of the left hand side.
10217 if not Is_Constrained (Target_Type) then
10218 if Has_Discriminants (Operand_Type) then
10219 Disc := First_Discriminant (Operand_Type);
10221 if Disc /= First_Stored_Discriminant (Operand_Type) then
10222 Disc := First_Stored_Discriminant (Operand_Type);
10226 while Present (Disc) loop
10228 Make_Selected_Component (Loc,
10230 Duplicate_Subexpr_Move_Checks (Operand),
10232 Make_Identifier (Loc, Chars (Disc))));
10233 Next_Discriminant (Disc);
10236 elsif Is_Array_Type (Operand_Type) then
10237 N_Ix := First_Index (Target_Type);
10240 for J in 1 .. Number_Dimensions (Operand_Type) loop
10242 -- We convert the bounds explicitly. We use an unchecked
10243 -- conversion because bounds checks are done elsewhere.
10248 Unchecked_Convert_To (Etype (N_Ix),
10249 Make_Attribute_Reference (Loc,
10251 Duplicate_Subexpr_No_Checks
10252 (Operand, Name_Req => True),
10253 Attribute_Name => Name_First,
10254 Expressions => New_List (
10255 Make_Integer_Literal (Loc, J)))),
10258 Unchecked_Convert_To (Etype (N_Ix),
10259 Make_Attribute_Reference (Loc,
10261 Duplicate_Subexpr_No_Checks
10262 (Operand, Name_Req => True),
10263 Attribute_Name => Name_Last,
10264 Expressions => New_List (
10265 Make_Integer_Literal (Loc, J))))));
10272 Odef := New_Occurrence_Of (Target_Type, Loc);
10274 if Present (Cons) then
10276 Make_Subtype_Indication (Loc,
10277 Subtype_Mark => Odef,
10279 Make_Index_Or_Discriminant_Constraint (Loc,
10280 Constraints => Cons));
10283 Temp := Make_Temporary (Loc, 'C');
10285 Make_Object_Declaration (Loc,
10286 Defining_Identifier => Temp,
10287 Object_Definition => Odef);
10289 Set_No_Initialization (Decl, True);
10291 -- Insert required actions. It is essential to suppress checks
10292 -- since we have suppressed default initialization, which means
10293 -- that the variable we create may have no discriminants.
10298 Make_Assignment_Statement (Loc,
10299 Name => New_Occurrence_Of (Temp, Loc),
10300 Expression => Relocate_Node (N))),
10301 Suppress => All_Checks);
10303 Rewrite (N, New_Occurrence_Of (Temp, Loc));
10306 end Handle_Changed_Representation;
10308 -------------------------------
10309 -- Raise_Accessibility_Error --
10310 -------------------------------
10312 procedure Raise_Accessibility_Error is
10314 Error_Msg_Warn := SPARK_Mode /= On;
10316 Make_Raise_Program_Error (Sloc (N),
10317 Reason => PE_Accessibility_Check_Failed));
10318 Set_Etype (N, Target_Type);
10320 Error_Msg_N ("<<accessibility check failure", N);
10321 Error_Msg_NE ("\<<& [", N, Standard_Program_Error);
10322 end Raise_Accessibility_Error;
10324 ----------------------
10325 -- Real_Range_Check --
10326 ----------------------
10328 -- Case of conversions to floating-point or fixed-point. If range checks
10329 -- are enabled and the target type has a range constraint, we convert:
10335 -- Tnn : typ'Base := typ'Base (x);
10336 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
10339 -- This is necessary when there is a conversion of integer to float or
10340 -- to fixed-point to ensure that the correct checks are made. It is not
10341 -- necessary for float to float where it is enough to simply set the
10342 -- Do_Range_Check flag.
10344 procedure Real_Range_Check is
10345 Btyp : constant Entity_Id := Base_Type (Target_Type);
10346 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
10347 Hi : constant Node_Id := Type_High_Bound (Target_Type);
10348 Xtyp : constant Entity_Id := Etype (Operand);
10353 -- Nothing to do if conversion was rewritten
10355 if Nkind (N) /= N_Type_Conversion then
10359 -- Nothing to do if range checks suppressed, or target has the same
10360 -- range as the base type (or is the base type).
10362 if Range_Checks_Suppressed (Target_Type)
10363 or else (Lo = Type_Low_Bound (Btyp)
10365 Hi = Type_High_Bound (Btyp))
10370 -- Nothing to do if expression is an entity on which checks have been
10373 if Is_Entity_Name (Operand)
10374 and then Range_Checks_Suppressed (Entity (Operand))
10379 -- Nothing to do if bounds are all static and we can tell that the
10380 -- expression is within the bounds of the target. Note that if the
10381 -- operand is of an unconstrained floating-point type, then we do
10382 -- not trust it to be in range (might be infinite)
10385 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
10386 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
10389 if (not Is_Floating_Point_Type (Xtyp)
10390 or else Is_Constrained (Xtyp))
10391 and then Compile_Time_Known_Value (S_Lo)
10392 and then Compile_Time_Known_Value (S_Hi)
10393 and then Compile_Time_Known_Value (Hi)
10394 and then Compile_Time_Known_Value (Lo)
10397 D_Lov : constant Ureal := Expr_Value_R (Lo);
10398 D_Hiv : constant Ureal := Expr_Value_R (Hi);
10403 if Is_Real_Type (Xtyp) then
10404 S_Lov := Expr_Value_R (S_Lo);
10405 S_Hiv := Expr_Value_R (S_Hi);
10407 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
10408 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
10412 and then S_Lov >= D_Lov
10413 and then S_Hiv <= D_Hiv
10415 -- Unset the range check flag on the current value of
10416 -- Expression (N), since the captured Operand may have
10417 -- been rewritten (such as for the case of a conversion
10418 -- to a fixed-point type).
10420 Set_Do_Range_Check (Expression (N), False);
10428 -- For float to float conversions, we are done
10430 if Is_Floating_Point_Type (Xtyp)
10432 Is_Floating_Point_Type (Btyp)
10437 -- Otherwise rewrite the conversion as described above
10439 Conv := Relocate_Node (N);
10440 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
10441 Set_Etype (Conv, Btyp);
10443 -- Enable overflow except for case of integer to float conversions,
10444 -- where it is never required, since we can never have overflow in
10447 if not Is_Integer_Type (Etype (Operand)) then
10448 Enable_Overflow_Check (Conv);
10451 Tnn := Make_Temporary (Loc, 'T', Conv);
10453 Insert_Actions (N, New_List (
10454 Make_Object_Declaration (Loc,
10455 Defining_Identifier => Tnn,
10456 Object_Definition => New_Occurrence_Of (Btyp, Loc),
10457 Constant_Present => True,
10458 Expression => Conv),
10460 Make_Raise_Constraint_Error (Loc,
10465 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
10467 Make_Attribute_Reference (Loc,
10468 Attribute_Name => Name_First,
10470 New_Occurrence_Of (Target_Type, Loc))),
10474 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
10476 Make_Attribute_Reference (Loc,
10477 Attribute_Name => Name_Last,
10479 New_Occurrence_Of (Target_Type, Loc)))),
10480 Reason => CE_Range_Check_Failed)));
10482 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
10483 Analyze_And_Resolve (N, Btyp);
10484 end Real_Range_Check;
10486 -----------------------------
10487 -- Has_Extra_Accessibility --
10488 -----------------------------
10490 -- Returns true for a formal of an anonymous access type or for
10491 -- an Ada 2012-style stand-alone object of an anonymous access type.
10493 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is
10495 if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then
10496 return Present (Effective_Extra_Accessibility (Id));
10500 end Has_Extra_Accessibility;
10502 -- Start of processing for Expand_N_Type_Conversion
10505 -- First remove check marks put by the semantic analysis on the type
10506 -- conversion between array types. We need these checks, and they will
10507 -- be generated by this expansion routine, but we do not depend on these
10508 -- flags being set, and since we do intend to expand the checks in the
10509 -- front end, we don't want them on the tree passed to the back end.
10511 if Is_Array_Type (Target_Type) then
10512 if Is_Constrained (Target_Type) then
10513 Set_Do_Length_Check (N, False);
10515 Set_Do_Range_Check (Operand, False);
10519 -- Nothing at all to do if conversion is to the identical type so remove
10520 -- the conversion completely, it is useless, except that it may carry
10521 -- an Assignment_OK attribute, which must be propagated to the operand.
10523 if Operand_Type = Target_Type then
10524 if Assignment_OK (N) then
10525 Set_Assignment_OK (Operand);
10528 Rewrite (N, Relocate_Node (Operand));
10532 -- Nothing to do if this is the second argument of read. This is a
10533 -- "backwards" conversion that will be handled by the specialized code
10534 -- in attribute processing.
10536 if Nkind (Parent (N)) = N_Attribute_Reference
10537 and then Attribute_Name (Parent (N)) = Name_Read
10538 and then Next (First (Expressions (Parent (N)))) = N
10543 -- Check for case of converting to a type that has an invariant
10544 -- associated with it. This required an invariant check. We convert
10550 -- do invariant_check (typ (expr)) in typ (expr);
10552 -- using Duplicate_Subexpr to avoid multiple side effects
10554 -- Note: the Comes_From_Source check, and then the resetting of this
10555 -- flag prevents what would otherwise be an infinite recursion.
10557 if Has_Invariants (Target_Type)
10558 and then Present (Invariant_Procedure (Target_Type))
10559 and then Comes_From_Source (N)
10561 Set_Comes_From_Source (N, False);
10563 Make_Expression_With_Actions (Loc,
10564 Actions => New_List (
10565 Make_Invariant_Call (Duplicate_Subexpr (N))),
10566 Expression => Duplicate_Subexpr_No_Checks (N)));
10567 Analyze_And_Resolve (N, Target_Type);
10571 -- Here if we may need to expand conversion
10573 -- If the operand of the type conversion is an arithmetic operation on
10574 -- signed integers, and the based type of the signed integer type in
10575 -- question is smaller than Standard.Integer, we promote both of the
10576 -- operands to type Integer.
10578 -- For example, if we have
10580 -- target-type (opnd1 + opnd2)
10582 -- and opnd1 and opnd2 are of type short integer, then we rewrite
10585 -- target-type (integer(opnd1) + integer(opnd2))
10587 -- We do this because we are always allowed to compute in a larger type
10588 -- if we do the right thing with the result, and in this case we are
10589 -- going to do a conversion which will do an appropriate check to make
10590 -- sure that things are in range of the target type in any case. This
10591 -- avoids some unnecessary intermediate overflows.
10593 -- We might consider a similar transformation in the case where the
10594 -- target is a real type or a 64-bit integer type, and the operand
10595 -- is an arithmetic operation using a 32-bit integer type. However,
10596 -- we do not bother with this case, because it could cause significant
10597 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
10598 -- much cheaper, but we don't want different behavior on 32-bit and
10599 -- 64-bit machines. Note that the exclusion of the 64-bit case also
10600 -- handles the configurable run-time cases where 64-bit arithmetic
10601 -- may simply be unavailable.
10603 -- Note: this circuit is partially redundant with respect to the circuit
10604 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
10605 -- the processing here. Also we still need the Checks circuit, since we
10606 -- have to be sure not to generate junk overflow checks in the first
10607 -- place, since it would be trick to remove them here.
10609 if Integer_Promotion_Possible (N) then
10611 -- All conditions met, go ahead with transformation
10619 Make_Type_Conversion (Loc,
10620 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
10621 Expression => Relocate_Node (Right_Opnd (Operand)));
10623 Opnd := New_Op_Node (Nkind (Operand), Loc);
10624 Set_Right_Opnd (Opnd, R);
10626 if Nkind (Operand) in N_Binary_Op then
10628 Make_Type_Conversion (Loc,
10629 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
10630 Expression => Relocate_Node (Left_Opnd (Operand)));
10632 Set_Left_Opnd (Opnd, L);
10636 Make_Type_Conversion (Loc,
10637 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
10638 Expression => Opnd));
10640 Analyze_And_Resolve (N, Target_Type);
10645 -- Do validity check if validity checking operands
10647 if Validity_Checks_On and Validity_Check_Operands then
10648 Ensure_Valid (Operand);
10651 -- Special case of converting from non-standard boolean type
10653 if Is_Boolean_Type (Operand_Type)
10654 and then (Nonzero_Is_True (Operand_Type))
10656 Adjust_Condition (Operand);
10657 Set_Etype (Operand, Standard_Boolean);
10658 Operand_Type := Standard_Boolean;
10661 -- Case of converting to an access type
10663 if Is_Access_Type (Target_Type) then
10665 -- Apply an accessibility check when the conversion operand is an
10666 -- access parameter (or a renaming thereof), unless conversion was
10667 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
10668 -- Note that other checks may still need to be applied below (such
10669 -- as tagged type checks).
10671 if Is_Entity_Name (Operand)
10672 and then Has_Extra_Accessibility (Entity (Operand))
10673 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
10674 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
10675 or else Attribute_Name (Original_Node (N)) = Name_Access)
10677 Apply_Accessibility_Check
10678 (Operand, Target_Type, Insert_Node => Operand);
10680 -- If the level of the operand type is statically deeper than the
10681 -- level of the target type, then force Program_Error. Note that this
10682 -- can only occur for cases where the attribute is within the body of
10683 -- an instantiation, otherwise the conversion will already have been
10684 -- rejected as illegal.
10686 -- Note: warnings are issued by the analyzer for the instance cases
10688 elsif In_Instance_Body
10690 -- The case where the target type is an anonymous access type of
10691 -- a discriminant is excluded, because the level of such a type
10692 -- depends on the context and currently the level returned for such
10693 -- types is zero, resulting in warnings about about check failures
10694 -- in certain legal cases involving class-wide interfaces as the
10695 -- designated type (some cases, such as return statements, are
10696 -- checked at run time, but not clear if these are handled right
10697 -- in general, see 3.10.2(12/2-12.5/3) ???).
10700 not (Ekind (Target_Type) = E_Anonymous_Access_Type
10701 and then Present (Associated_Node_For_Itype (Target_Type))
10702 and then Nkind (Associated_Node_For_Itype (Target_Type)) =
10703 N_Discriminant_Specification)
10705 Type_Access_Level (Operand_Type) > Type_Access_Level (Target_Type)
10707 Raise_Accessibility_Error;
10710 -- When the operand is a selected access discriminant the check needs
10711 -- to be made against the level of the object denoted by the prefix
10712 -- of the selected name. Force Program_Error for this case as well
10713 -- (this accessibility violation can only happen if within the body
10714 -- of an instantiation).
10716 elsif In_Instance_Body
10717 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
10718 and then Nkind (Operand) = N_Selected_Component
10719 and then Object_Access_Level (Operand) >
10720 Type_Access_Level (Target_Type)
10722 Raise_Accessibility_Error;
10727 -- Case of conversions of tagged types and access to tagged types
10729 -- When needed, that is to say when the expression is class-wide, Add
10730 -- runtime a tag check for (strict) downward conversion by using the
10731 -- membership test, generating:
10733 -- [constraint_error when Operand not in Target_Type'Class]
10735 -- or in the access type case
10737 -- [constraint_error
10738 -- when Operand /= null
10739 -- and then Operand.all not in
10740 -- Designated_Type (Target_Type)'Class]
10742 if (Is_Access_Type (Target_Type)
10743 and then Is_Tagged_Type (Designated_Type (Target_Type)))
10744 or else Is_Tagged_Type (Target_Type)
10746 -- Do not do any expansion in the access type case if the parent is a
10747 -- renaming, since this is an error situation which will be caught by
10748 -- Sem_Ch8, and the expansion can interfere with this error check.
10750 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
10754 -- Otherwise, proceed with processing tagged conversion
10756 Tagged_Conversion : declare
10757 Actual_Op_Typ : Entity_Id;
10758 Actual_Targ_Typ : Entity_Id;
10759 Make_Conversion : Boolean := False;
10760 Root_Op_Typ : Entity_Id;
10762 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
10763 -- Create a membership check to test whether Operand is a member
10764 -- of Targ_Typ. If the original Target_Type is an access, include
10765 -- a test for null value. The check is inserted at N.
10767 --------------------
10768 -- Make_Tag_Check --
10769 --------------------
10771 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
10776 -- [Constraint_Error
10777 -- when Operand /= null
10778 -- and then Operand.all not in Targ_Typ]
10780 if Is_Access_Type (Target_Type) then
10782 Make_And_Then (Loc,
10785 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10786 Right_Opnd => Make_Null (Loc)),
10791 Make_Explicit_Dereference (Loc,
10792 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
10793 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc)));
10796 -- [Constraint_Error when Operand not in Targ_Typ]
10801 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10802 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc));
10806 Make_Raise_Constraint_Error (Loc,
10808 Reason => CE_Tag_Check_Failed));
10809 end Make_Tag_Check;
10811 -- Start of processing for Tagged_Conversion
10814 -- Handle entities from the limited view
10816 if Is_Access_Type (Operand_Type) then
10818 Available_View (Designated_Type (Operand_Type));
10820 Actual_Op_Typ := Operand_Type;
10823 if Is_Access_Type (Target_Type) then
10825 Available_View (Designated_Type (Target_Type));
10827 Actual_Targ_Typ := Target_Type;
10830 Root_Op_Typ := Root_Type (Actual_Op_Typ);
10832 -- Ada 2005 (AI-251): Handle interface type conversion
10834 if Is_Interface (Actual_Op_Typ)
10836 Is_Interface (Actual_Targ_Typ)
10838 Expand_Interface_Conversion (N);
10842 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
10844 -- Create a runtime tag check for a downward class-wide type
10847 if Is_Class_Wide_Type (Actual_Op_Typ)
10848 and then Actual_Op_Typ /= Actual_Targ_Typ
10849 and then Root_Op_Typ /= Actual_Targ_Typ
10850 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ,
10851 Use_Full_View => True)
10853 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
10854 Make_Conversion := True;
10857 -- AI05-0073: If the result subtype of the function is defined
10858 -- by an access_definition designating a specific tagged type
10859 -- T, a check is made that the result value is null or the tag
10860 -- of the object designated by the result value identifies T.
10861 -- Constraint_Error is raised if this check fails.
10863 if Nkind (Parent (N)) = N_Simple_Return_Statement then
10866 Func_Typ : Entity_Id;
10869 -- Climb scope stack looking for the enclosing function
10871 Func := Current_Scope;
10872 while Present (Func)
10873 and then Ekind (Func) /= E_Function
10875 Func := Scope (Func);
10878 -- The function's return subtype must be defined using
10879 -- an access definition.
10881 if Nkind (Result_Definition (Parent (Func))) =
10882 N_Access_Definition
10884 Func_Typ := Directly_Designated_Type (Etype (Func));
10886 -- The return subtype denotes a specific tagged type,
10887 -- in other words, a non class-wide type.
10889 if Is_Tagged_Type (Func_Typ)
10890 and then not Is_Class_Wide_Type (Func_Typ)
10892 Make_Tag_Check (Actual_Targ_Typ);
10893 Make_Conversion := True;
10899 -- We have generated a tag check for either a class-wide type
10900 -- conversion or for AI05-0073.
10902 if Make_Conversion then
10907 Make_Unchecked_Type_Conversion (Loc,
10908 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
10909 Expression => Relocate_Node (Expression (N)));
10911 Analyze_And_Resolve (N, Target_Type);
10915 end Tagged_Conversion;
10917 -- Case of other access type conversions
10919 elsif Is_Access_Type (Target_Type) then
10920 Apply_Constraint_Check (Operand, Target_Type);
10922 -- Case of conversions from a fixed-point type
10924 -- These conversions require special expansion and processing, found in
10925 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
10926 -- since from a semantic point of view, these are simple integer
10927 -- conversions, which do not need further processing.
10929 elsif Is_Fixed_Point_Type (Operand_Type)
10930 and then not Conversion_OK (N)
10932 -- We should never see universal fixed at this case, since the
10933 -- expansion of the constituent divide or multiply should have
10934 -- eliminated the explicit mention of universal fixed.
10936 pragma Assert (Operand_Type /= Universal_Fixed);
10938 -- Check for special case of the conversion to universal real that
10939 -- occurs as a result of the use of a round attribute. In this case,
10940 -- the real type for the conversion is taken from the target type of
10941 -- the Round attribute and the result must be marked as rounded.
10943 if Target_Type = Universal_Real
10944 and then Nkind (Parent (N)) = N_Attribute_Reference
10945 and then Attribute_Name (Parent (N)) = Name_Round
10947 Set_Rounded_Result (N);
10948 Set_Etype (N, Etype (Parent (N)));
10951 -- Otherwise do correct fixed-conversion, but skip these if the
10952 -- Conversion_OK flag is set, because from a semantic point of view
10953 -- these are simple integer conversions needing no further processing
10954 -- (the backend will simply treat them as integers).
10956 if not Conversion_OK (N) then
10957 if Is_Fixed_Point_Type (Etype (N)) then
10958 Expand_Convert_Fixed_To_Fixed (N);
10961 elsif Is_Integer_Type (Etype (N)) then
10962 Expand_Convert_Fixed_To_Integer (N);
10965 pragma Assert (Is_Floating_Point_Type (Etype (N)));
10966 Expand_Convert_Fixed_To_Float (N);
10971 -- Case of conversions to a fixed-point type
10973 -- These conversions require special expansion and processing, found in
10974 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
10975 -- since from a semantic point of view, these are simple integer
10976 -- conversions, which do not need further processing.
10978 elsif Is_Fixed_Point_Type (Target_Type)
10979 and then not Conversion_OK (N)
10981 if Is_Integer_Type (Operand_Type) then
10982 Expand_Convert_Integer_To_Fixed (N);
10985 pragma Assert (Is_Floating_Point_Type (Operand_Type));
10986 Expand_Convert_Float_To_Fixed (N);
10990 -- Case of float-to-integer conversions
10992 -- We also handle float-to-fixed conversions with Conversion_OK set
10993 -- since semantically the fixed-point target is treated as though it
10994 -- were an integer in such cases.
10996 elsif Is_Floating_Point_Type (Operand_Type)
10998 (Is_Integer_Type (Target_Type)
11000 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
11002 -- One more check here, gcc is still not able to do conversions of
11003 -- this type with proper overflow checking, and so gigi is doing an
11004 -- approximation of what is required by doing floating-point compares
11005 -- with the end-point. But that can lose precision in some cases, and
11006 -- give a wrong result. Converting the operand to Universal_Real is
11007 -- helpful, but still does not catch all cases with 64-bit integers
11008 -- on targets with only 64-bit floats.
11010 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
11011 -- Can this code be removed ???
11013 if Do_Range_Check (Operand) then
11015 Make_Type_Conversion (Loc,
11017 New_Occurrence_Of (Universal_Real, Loc),
11019 Relocate_Node (Operand)));
11021 Set_Etype (Operand, Universal_Real);
11022 Enable_Range_Check (Operand);
11023 Set_Do_Range_Check (Expression (Operand), False);
11026 -- Case of array conversions
11028 -- Expansion of array conversions, add required length/range checks but
11029 -- only do this if there is no change of representation. For handling of
11030 -- this case, see Handle_Changed_Representation.
11032 elsif Is_Array_Type (Target_Type) then
11033 if Is_Constrained (Target_Type) then
11034 Apply_Length_Check (Operand, Target_Type);
11036 Apply_Range_Check (Operand, Target_Type);
11039 Handle_Changed_Representation;
11041 -- Case of conversions of discriminated types
11043 -- Add required discriminant checks if target is constrained. Again this
11044 -- change is skipped if we have a change of representation.
11046 elsif Has_Discriminants (Target_Type)
11047 and then Is_Constrained (Target_Type)
11049 Apply_Discriminant_Check (Operand, Target_Type);
11050 Handle_Changed_Representation;
11052 -- Case of all other record conversions. The only processing required
11053 -- is to check for a change of representation requiring the special
11054 -- assignment processing.
11056 elsif Is_Record_Type (Target_Type) then
11058 -- Ada 2005 (AI-216): Program_Error is raised when converting from
11059 -- a derived Unchecked_Union type to an unconstrained type that is
11060 -- not Unchecked_Union if the operand lacks inferable discriminants.
11062 if Is_Derived_Type (Operand_Type)
11063 and then Is_Unchecked_Union (Base_Type (Operand_Type))
11064 and then not Is_Constrained (Target_Type)
11065 and then not Is_Unchecked_Union (Base_Type (Target_Type))
11066 and then not Has_Inferable_Discriminants (Operand)
11068 -- To prevent Gigi from generating illegal code, we generate a
11069 -- Program_Error node, but we give it the target type of the
11070 -- conversion (is this requirement documented somewhere ???)
11073 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
11074 Reason => PE_Unchecked_Union_Restriction);
11077 Set_Etype (PE, Target_Type);
11082 Handle_Changed_Representation;
11085 -- Case of conversions of enumeration types
11087 elsif Is_Enumeration_Type (Target_Type) then
11089 -- Special processing is required if there is a change of
11090 -- representation (from enumeration representation clauses).
11092 if not Same_Representation (Target_Type, Operand_Type) then
11094 -- Convert: x(y) to x'val (ytyp'val (y))
11097 Make_Attribute_Reference (Loc,
11098 Prefix => New_Occurrence_Of (Target_Type, Loc),
11099 Attribute_Name => Name_Val,
11100 Expressions => New_List (
11101 Make_Attribute_Reference (Loc,
11102 Prefix => New_Occurrence_Of (Operand_Type, Loc),
11103 Attribute_Name => Name_Pos,
11104 Expressions => New_List (Operand)))));
11106 Analyze_And_Resolve (N, Target_Type);
11109 -- Case of conversions to floating-point
11111 elsif Is_Floating_Point_Type (Target_Type) then
11115 -- At this stage, either the conversion node has been transformed into
11116 -- some other equivalent expression, or left as a conversion that can be
11117 -- handled by Gigi, in the following cases:
11119 -- Conversions with no change of representation or type
11121 -- Numeric conversions involving integer, floating- and fixed-point
11122 -- values. Fixed-point values are allowed only if Conversion_OK is
11123 -- set, i.e. if the fixed-point values are to be treated as integers.
11125 -- No other conversions should be passed to Gigi
11127 -- Check: are these rules stated in sinfo??? if so, why restate here???
11129 -- The only remaining step is to generate a range check if we still have
11130 -- a type conversion at this stage and Do_Range_Check is set. For now we
11131 -- do this only for conversions of discrete types and for float-to-float
11134 if Nkind (N) = N_Type_Conversion then
11136 -- For now we only support floating-point cases where both source
11137 -- and target are floating-point types. Conversions where the source
11138 -- and target involve integer or fixed-point types are still TBD,
11139 -- though not clear whether those can even happen at this point, due
11140 -- to transformations above. ???
11142 if Is_Floating_Point_Type (Etype (N))
11143 and then Is_Floating_Point_Type (Etype (Expression (N)))
11145 if Do_Range_Check (Expression (N))
11146 and then Is_Floating_Point_Type (Target_Type)
11148 Generate_Range_Check
11149 (Expression (N), Target_Type, CE_Range_Check_Failed);
11152 -- Discrete-to-discrete conversions
11154 elsif Is_Discrete_Type (Etype (N)) then
11156 Expr : constant Node_Id := Expression (N);
11161 if Do_Range_Check (Expr)
11162 and then Is_Discrete_Type (Etype (Expr))
11164 Set_Do_Range_Check (Expr, False);
11166 -- Before we do a range check, we have to deal with treating
11167 -- a fixed-point operand as an integer. The way we do this
11168 -- is simply to do an unchecked conversion to an appropriate
11169 -- integer type large enough to hold the result.
11171 -- This code is not active yet, because we are only dealing
11172 -- with discrete types so far ???
11174 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
11175 and then Treat_Fixed_As_Integer (Expr)
11177 Ftyp := Base_Type (Etype (Expr));
11179 if Esize (Ftyp) >= Esize (Standard_Integer) then
11180 Ityp := Standard_Long_Long_Integer;
11182 Ityp := Standard_Integer;
11185 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
11188 -- Reset overflow flag, since the range check will include
11189 -- dealing with possible overflow, and generate the check.
11190 -- If Address is either a source type or target type,
11191 -- suppress range check to avoid typing anomalies when
11192 -- it is a visible integer type.
11194 Set_Do_Overflow_Check (N, False);
11196 if not Is_Descendent_Of_Address (Etype (Expr))
11197 and then not Is_Descendent_Of_Address (Target_Type)
11199 Generate_Range_Check
11200 (Expr, Target_Type, CE_Range_Check_Failed);
11207 -- Here at end of processing
11210 -- Apply predicate check if required. Note that we can't just call
11211 -- Apply_Predicate_Check here, because the type looks right after
11212 -- the conversion and it would omit the check. The Comes_From_Source
11213 -- guard is necessary to prevent infinite recursions when we generate
11214 -- internal conversions for the purpose of checking predicates.
11216 if Present (Predicate_Function (Target_Type))
11217 and then Target_Type /= Operand_Type
11218 and then Comes_From_Source (N)
11221 New_Expr : constant Node_Id := Duplicate_Subexpr (N);
11224 -- Avoid infinite recursion on the subsequent expansion of
11225 -- of the copy of the original type conversion.
11227 Set_Comes_From_Source (New_Expr, False);
11228 Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr));
11231 end Expand_N_Type_Conversion;
11233 -----------------------------------
11234 -- Expand_N_Unchecked_Expression --
11235 -----------------------------------
11237 -- Remove the unchecked expression node from the tree. Its job was simply
11238 -- to make sure that its constituent expression was handled with checks
11239 -- off, and now that that is done, we can remove it from the tree, and
11240 -- indeed must, since Gigi does not expect to see these nodes.
11242 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
11243 Exp : constant Node_Id := Expression (N);
11245 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
11247 end Expand_N_Unchecked_Expression;
11249 ----------------------------------------
11250 -- Expand_N_Unchecked_Type_Conversion --
11251 ----------------------------------------
11253 -- If this cannot be handled by Gigi and we haven't already made a
11254 -- temporary for it, do it now.
11256 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
11257 Target_Type : constant Entity_Id := Etype (N);
11258 Operand : constant Node_Id := Expression (N);
11259 Operand_Type : constant Entity_Id := Etype (Operand);
11262 -- Nothing at all to do if conversion is to the identical type so remove
11263 -- the conversion completely, it is useless, except that it may carry
11264 -- an Assignment_OK indication which must be propagated to the operand.
11266 if Operand_Type = Target_Type then
11268 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
11270 if Assignment_OK (N) then
11271 Set_Assignment_OK (Operand);
11274 Rewrite (N, Relocate_Node (Operand));
11278 -- If we have a conversion of a compile time known value to a target
11279 -- type and the value is in range of the target type, then we can simply
11280 -- replace the construct by an integer literal of the correct type. We
11281 -- only apply this to integer types being converted. Possibly it may
11282 -- apply in other cases, but it is too much trouble to worry about.
11284 -- Note that we do not do this transformation if the Kill_Range_Check
11285 -- flag is set, since then the value may be outside the expected range.
11286 -- This happens in the Normalize_Scalars case.
11288 -- We also skip this if either the target or operand type is biased
11289 -- because in this case, the unchecked conversion is supposed to
11290 -- preserve the bit pattern, not the integer value.
11292 if Is_Integer_Type (Target_Type)
11293 and then not Has_Biased_Representation (Target_Type)
11294 and then Is_Integer_Type (Operand_Type)
11295 and then not Has_Biased_Representation (Operand_Type)
11296 and then Compile_Time_Known_Value (Operand)
11297 and then not Kill_Range_Check (N)
11300 Val : constant Uint := Expr_Value (Operand);
11303 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
11305 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
11307 Val >= Expr_Value (Type_Low_Bound (Target_Type))
11309 Val <= Expr_Value (Type_High_Bound (Target_Type))
11311 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
11313 -- If Address is the target type, just set the type to avoid a
11314 -- spurious type error on the literal when Address is a visible
11317 if Is_Descendent_Of_Address (Target_Type) then
11318 Set_Etype (N, Target_Type);
11320 Analyze_And_Resolve (N, Target_Type);
11328 -- Nothing to do if conversion is safe
11330 if Safe_Unchecked_Type_Conversion (N) then
11334 -- Otherwise force evaluation unless Assignment_OK flag is set (this
11335 -- flag indicates ??? More comments needed here)
11337 if Assignment_OK (N) then
11340 Force_Evaluation (N);
11342 end Expand_N_Unchecked_Type_Conversion;
11344 ----------------------------
11345 -- Expand_Record_Equality --
11346 ----------------------------
11348 -- For non-variant records, Equality is expanded when needed into:
11350 -- and then Lhs.Discr1 = Rhs.Discr1
11352 -- and then Lhs.Discrn = Rhs.Discrn
11353 -- and then Lhs.Cmp1 = Rhs.Cmp1
11355 -- and then Lhs.Cmpn = Rhs.Cmpn
11357 -- The expression is folded by the back-end for adjacent fields. This
11358 -- function is called for tagged record in only one occasion: for imple-
11359 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
11360 -- otherwise the primitive "=" is used directly.
11362 function Expand_Record_Equality
11367 Bodies : List_Id) return Node_Id
11369 Loc : constant Source_Ptr := Sloc (Nod);
11374 First_Time : Boolean := True;
11376 function Element_To_Compare (C : Entity_Id) return Entity_Id;
11377 -- Return the next discriminant or component to compare, starting with
11378 -- C, skipping inherited components.
11380 ------------------------
11381 -- Element_To_Compare --
11382 ------------------------
11384 function Element_To_Compare (C : Entity_Id) return Entity_Id is
11390 -- Exit loop when the next element to be compared is found, or
11391 -- there is no more such element.
11393 exit when No (Comp);
11395 exit when Ekind_In (Comp, E_Discriminant, E_Component)
11398 -- Skip inherited components
11400 -- Note: for a tagged type, we always generate the "=" primitive
11401 -- for the base type (not on the first subtype), so the test for
11402 -- Comp /= Original_Record_Component (Comp) is True for
11403 -- inherited components only.
11405 (Is_Tagged_Type (Typ)
11406 and then Comp /= Original_Record_Component (Comp))
11410 or else Chars (Comp) = Name_uTag
11412 -- Skip interface elements (secondary tags???)
11414 or else Is_Interface (Etype (Comp)));
11416 Next_Entity (Comp);
11420 end Element_To_Compare;
11422 -- Start of processing for Expand_Record_Equality
11425 -- Generates the following code: (assuming that Typ has one Discr and
11426 -- component C2 is also a record)
11429 -- and then Lhs.Discr1 = Rhs.Discr1
11430 -- and then Lhs.C1 = Rhs.C1
11431 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
11433 -- and then Lhs.Cmpn = Rhs.Cmpn
11435 Result := New_Occurrence_Of (Standard_True, Loc);
11436 C := Element_To_Compare (First_Entity (Typ));
11437 while Present (C) loop
11445 First_Time := False;
11449 New_Lhs := New_Copy_Tree (Lhs);
11450 New_Rhs := New_Copy_Tree (Rhs);
11454 Expand_Composite_Equality (Nod, Etype (C),
11456 Make_Selected_Component (Loc,
11458 Selector_Name => New_Occurrence_Of (C, Loc)),
11460 Make_Selected_Component (Loc,
11462 Selector_Name => New_Occurrence_Of (C, Loc)),
11465 -- If some (sub)component is an unchecked_union, the whole
11466 -- operation will raise program error.
11468 if Nkind (Check) = N_Raise_Program_Error then
11470 Set_Etype (Result, Standard_Boolean);
11474 Make_And_Then (Loc,
11475 Left_Opnd => Result,
11476 Right_Opnd => Check);
11480 C := Element_To_Compare (Next_Entity (C));
11484 end Expand_Record_Equality;
11486 ---------------------------
11487 -- Expand_Set_Membership --
11488 ---------------------------
11490 procedure Expand_Set_Membership (N : Node_Id) is
11491 Lop : constant Node_Id := Left_Opnd (N);
11495 function Make_Cond (Alt : Node_Id) return Node_Id;
11496 -- If the alternative is a subtype mark, create a simple membership
11497 -- test. Otherwise create an equality test for it.
11503 function Make_Cond (Alt : Node_Id) return Node_Id is
11505 L : constant Node_Id := New_Copy (Lop);
11506 R : constant Node_Id := Relocate_Node (Alt);
11509 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
11510 or else Nkind (Alt) = N_Range
11513 Make_In (Sloc (Alt),
11518 Make_Op_Eq (Sloc (Alt),
11526 -- Start of processing for Expand_Set_Membership
11529 Remove_Side_Effects (Lop);
11531 Alt := Last (Alternatives (N));
11532 Res := Make_Cond (Alt);
11535 while Present (Alt) loop
11537 Make_Or_Else (Sloc (Alt),
11538 Left_Opnd => Make_Cond (Alt),
11539 Right_Opnd => Res);
11544 Analyze_And_Resolve (N, Standard_Boolean);
11545 end Expand_Set_Membership;
11547 -----------------------------------
11548 -- Expand_Short_Circuit_Operator --
11549 -----------------------------------
11551 -- Deal with special expansion if actions are present for the right operand
11552 -- and deal with optimizing case of arguments being True or False. We also
11553 -- deal with the special case of non-standard boolean values.
11555 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
11556 Loc : constant Source_Ptr := Sloc (N);
11557 Typ : constant Entity_Id := Etype (N);
11558 Left : constant Node_Id := Left_Opnd (N);
11559 Right : constant Node_Id := Right_Opnd (N);
11560 LocR : constant Source_Ptr := Sloc (Right);
11563 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
11564 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
11565 -- If Left = Shortcut_Value then Right need not be evaluated
11568 -- Deal with non-standard booleans
11570 if Is_Boolean_Type (Typ) then
11571 Adjust_Condition (Left);
11572 Adjust_Condition (Right);
11573 Set_Etype (N, Standard_Boolean);
11576 -- Check for cases where left argument is known to be True or False
11578 if Compile_Time_Known_Value (Left) then
11580 -- Mark SCO for left condition as compile time known
11582 if Generate_SCO and then Comes_From_Source (Left) then
11583 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
11586 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
11587 -- Any actions associated with Right will be executed unconditionally
11588 -- and can thus be inserted into the tree unconditionally.
11590 if Expr_Value_E (Left) /= Shortcut_Ent then
11591 if Present (Actions (N)) then
11592 Insert_Actions (N, Actions (N));
11595 Rewrite (N, Right);
11597 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
11598 -- In this case we can forget the actions associated with Right,
11599 -- since they will never be executed.
11602 Kill_Dead_Code (Right);
11603 Kill_Dead_Code (Actions (N));
11604 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
11607 Adjust_Result_Type (N, Typ);
11611 -- If Actions are present for the right operand, we have to do some
11612 -- special processing. We can't just let these actions filter back into
11613 -- code preceding the short circuit (which is what would have happened
11614 -- if we had not trapped them in the short-circuit form), since they
11615 -- must only be executed if the right operand of the short circuit is
11616 -- executed and not otherwise.
11618 if Present (Actions (N)) then
11619 Actlist := Actions (N);
11621 -- We now use an Expression_With_Actions node for the right operand
11622 -- of the short-circuit form. Note that this solves the traceability
11623 -- problems for coverage analysis.
11626 Make_Expression_With_Actions (LocR,
11627 Expression => Relocate_Node (Right),
11628 Actions => Actlist));
11630 Set_Actions (N, No_List);
11631 Analyze_And_Resolve (Right, Standard_Boolean);
11633 Adjust_Result_Type (N, Typ);
11637 -- No actions present, check for cases of right argument True/False
11639 if Compile_Time_Known_Value (Right) then
11641 -- Mark SCO for left condition as compile time known
11643 if Generate_SCO and then Comes_From_Source (Right) then
11644 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
11647 -- Change (Left and then True), (Left or else False) to Left.
11648 -- Note that we know there are no actions associated with the right
11649 -- operand, since we just checked for this case above.
11651 if Expr_Value_E (Right) /= Shortcut_Ent then
11654 -- Change (Left and then False), (Left or else True) to Right,
11655 -- making sure to preserve any side effects associated with the Left
11659 Remove_Side_Effects (Left);
11660 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
11664 Adjust_Result_Type (N, Typ);
11665 end Expand_Short_Circuit_Operator;
11667 -------------------------------------
11668 -- Fixup_Universal_Fixed_Operation --
11669 -------------------------------------
11671 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
11672 Conv : constant Node_Id := Parent (N);
11675 -- We must have a type conversion immediately above us
11677 pragma Assert (Nkind (Conv) = N_Type_Conversion);
11679 -- Normally the type conversion gives our target type. The exception
11680 -- occurs in the case of the Round attribute, where the conversion
11681 -- will be to universal real, and our real type comes from the Round
11682 -- attribute (as well as an indication that we must round the result)
11684 if Nkind (Parent (Conv)) = N_Attribute_Reference
11685 and then Attribute_Name (Parent (Conv)) = Name_Round
11687 Set_Etype (N, Etype (Parent (Conv)));
11688 Set_Rounded_Result (N);
11690 -- Normal case where type comes from conversion above us
11693 Set_Etype (N, Etype (Conv));
11695 end Fixup_Universal_Fixed_Operation;
11697 ---------------------------------
11698 -- Has_Inferable_Discriminants --
11699 ---------------------------------
11701 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
11703 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
11704 -- Determines whether the left-most prefix of a selected component is a
11705 -- formal parameter in a subprogram. Assumes N is a selected component.
11707 --------------------------------
11708 -- Prefix_Is_Formal_Parameter --
11709 --------------------------------
11711 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
11712 Sel_Comp : Node_Id;
11715 -- Move to the left-most prefix by climbing up the tree
11718 while Present (Parent (Sel_Comp))
11719 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
11721 Sel_Comp := Parent (Sel_Comp);
11724 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
11725 end Prefix_Is_Formal_Parameter;
11727 -- Start of processing for Has_Inferable_Discriminants
11730 -- For selected components, the subtype of the selector must be a
11731 -- constrained Unchecked_Union. If the component is subject to a
11732 -- per-object constraint, then the enclosing object must have inferable
11735 if Nkind (N) = N_Selected_Component then
11736 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
11738 -- A small hack. If we have a per-object constrained selected
11739 -- component of a formal parameter, return True since we do not
11740 -- know the actual parameter association yet.
11742 if Prefix_Is_Formal_Parameter (N) then
11745 -- Otherwise, check the enclosing object and the selector
11748 return Has_Inferable_Discriminants (Prefix (N))
11749 and then Has_Inferable_Discriminants (Selector_Name (N));
11752 -- The call to Has_Inferable_Discriminants will determine whether
11753 -- the selector has a constrained Unchecked_Union nominal type.
11756 return Has_Inferable_Discriminants (Selector_Name (N));
11759 -- A qualified expression has inferable discriminants if its subtype
11760 -- mark is a constrained Unchecked_Union subtype.
11762 elsif Nkind (N) = N_Qualified_Expression then
11763 return Is_Unchecked_Union (Etype (Subtype_Mark (N)))
11764 and then Is_Constrained (Etype (Subtype_Mark (N)));
11766 -- For all other names, it is sufficient to have a constrained
11767 -- Unchecked_Union nominal subtype.
11770 return Is_Unchecked_Union (Base_Type (Etype (N)))
11771 and then Is_Constrained (Etype (N));
11773 end Has_Inferable_Discriminants;
11775 -------------------------------
11776 -- Insert_Dereference_Action --
11777 -------------------------------
11779 procedure Insert_Dereference_Action (N : Node_Id) is
11781 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
11782 -- Return true if type of P is derived from Checked_Pool;
11784 -----------------------------
11785 -- Is_Checked_Storage_Pool --
11786 -----------------------------
11788 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
11797 while T /= Etype (T) loop
11798 if Is_RTE (T, RE_Checked_Pool) then
11806 end Is_Checked_Storage_Pool;
11810 Typ : constant Entity_Id := Etype (N);
11811 Desig : constant Entity_Id := Available_View (Designated_Type (Typ));
11812 Loc : constant Source_Ptr := Sloc (N);
11813 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
11814 Pnod : constant Node_Id := Parent (N);
11820 Size_Bits : Node_Id;
11823 -- Start of processing for Insert_Dereference_Action
11826 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
11828 -- Do not re-expand a dereference which has already been processed by
11831 if Has_Dereference_Action (Pnod) then
11834 -- Do not perform this type of expansion for internally-generated
11837 elsif not Comes_From_Source (Original_Node (Pnod)) then
11840 -- A dereference action is only applicable to objects which have been
11841 -- allocated on a checked pool.
11843 elsif not Is_Checked_Storage_Pool (Pool) then
11847 -- Extract the address of the dereferenced object. Generate:
11849 -- Addr : System.Address := <N>'Pool_Address;
11851 Addr := Make_Temporary (Loc, 'P');
11854 Make_Object_Declaration (Loc,
11855 Defining_Identifier => Addr,
11856 Object_Definition =>
11857 New_Occurrence_Of (RTE (RE_Address), Loc),
11859 Make_Attribute_Reference (Loc,
11860 Prefix => Duplicate_Subexpr_Move_Checks (N),
11861 Attribute_Name => Name_Pool_Address)));
11863 -- Calculate the size of the dereferenced object. Generate:
11865 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
11868 Make_Explicit_Dereference (Loc,
11869 Prefix => Duplicate_Subexpr_Move_Checks (N));
11870 Set_Has_Dereference_Action (Deref);
11873 Make_Attribute_Reference (Loc,
11875 Attribute_Name => Name_Size);
11877 -- Special case of an unconstrained array: need to add descriptor size
11879 if Is_Array_Type (Desig)
11880 and then not Is_Constrained (First_Subtype (Desig))
11885 Make_Attribute_Reference (Loc,
11887 New_Occurrence_Of (First_Subtype (Desig), Loc),
11888 Attribute_Name => Name_Descriptor_Size),
11889 Right_Opnd => Size_Bits);
11892 Size := Make_Temporary (Loc, 'S');
11894 Make_Object_Declaration (Loc,
11895 Defining_Identifier => Size,
11896 Object_Definition =>
11897 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
11899 Make_Op_Divide (Loc,
11900 Left_Opnd => Size_Bits,
11901 Right_Opnd => Make_Integer_Literal (Loc, System_Storage_Unit))));
11903 -- Calculate the alignment of the dereferenced object. Generate:
11904 -- Alig : constant Storage_Count := <N>.all'Alignment;
11907 Make_Explicit_Dereference (Loc,
11908 Prefix => Duplicate_Subexpr_Move_Checks (N));
11909 Set_Has_Dereference_Action (Deref);
11911 Alig := Make_Temporary (Loc, 'A');
11913 Make_Object_Declaration (Loc,
11914 Defining_Identifier => Alig,
11915 Object_Definition =>
11916 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
11918 Make_Attribute_Reference (Loc,
11920 Attribute_Name => Name_Alignment)));
11922 -- A dereference of a controlled object requires special processing. The
11923 -- finalization machinery requests additional space from the underlying
11924 -- pool to allocate and hide two pointers. As a result, a checked pool
11925 -- may mark the wrong memory as valid. Since checked pools do not have
11926 -- knowledge of hidden pointers, we have to bring the two pointers back
11927 -- in view in order to restore the original state of the object.
11929 if Needs_Finalization (Desig) then
11931 -- Adjust the address and size of the dereferenced object. Generate:
11932 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
11935 Make_Procedure_Call_Statement (Loc,
11937 New_Occurrence_Of (RTE (RE_Adjust_Controlled_Dereference), Loc),
11938 Parameter_Associations => New_List (
11939 New_Occurrence_Of (Addr, Loc),
11940 New_Occurrence_Of (Size, Loc),
11941 New_Occurrence_Of (Alig, Loc)));
11943 -- Class-wide types complicate things because we cannot determine
11944 -- statically whether the actual object is truly controlled. We must
11945 -- generate a runtime check to detect this property. Generate:
11947 -- if Needs_Finalization (<N>.all'Tag) then
11951 if Is_Class_Wide_Type (Desig) then
11953 Make_Explicit_Dereference (Loc,
11954 Prefix => Duplicate_Subexpr_Move_Checks (N));
11955 Set_Has_Dereference_Action (Deref);
11958 Make_Implicit_If_Statement (N,
11960 Make_Function_Call (Loc,
11962 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
11963 Parameter_Associations => New_List (
11964 Make_Attribute_Reference (Loc,
11966 Attribute_Name => Name_Tag))),
11967 Then_Statements => New_List (Stmt));
11970 Insert_Action (N, Stmt);
11974 -- Dereference (Pool, Addr, Size, Alig);
11977 Make_Procedure_Call_Statement (Loc,
11980 (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
11981 Parameter_Associations => New_List (
11982 New_Occurrence_Of (Pool, Loc),
11983 New_Occurrence_Of (Addr, Loc),
11984 New_Occurrence_Of (Size, Loc),
11985 New_Occurrence_Of (Alig, Loc))));
11987 -- Mark the explicit dereference as processed to avoid potential
11988 -- infinite expansion.
11990 Set_Has_Dereference_Action (Pnod);
11993 when RE_Not_Available =>
11995 end Insert_Dereference_Action;
11997 --------------------------------
11998 -- Integer_Promotion_Possible --
11999 --------------------------------
12001 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
12002 Operand : constant Node_Id := Expression (N);
12003 Operand_Type : constant Entity_Id := Etype (Operand);
12004 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
12007 pragma Assert (Nkind (N) = N_Type_Conversion);
12011 -- We only do the transformation for source constructs. We assume
12012 -- that the expander knows what it is doing when it generates code.
12014 Comes_From_Source (N)
12016 -- If the operand type is Short_Integer or Short_Short_Integer,
12017 -- then we will promote to Integer, which is available on all
12018 -- targets, and is sufficient to ensure no intermediate overflow.
12019 -- Furthermore it is likely to be as efficient or more efficient
12020 -- than using the smaller type for the computation so we do this
12021 -- unconditionally.
12024 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
12026 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
12028 -- Test for interesting operation, which includes addition,
12029 -- division, exponentiation, multiplication, subtraction, absolute
12030 -- value and unary negation. Unary "+" is omitted since it is a
12031 -- no-op and thus can't overflow.
12033 and then Nkind_In (Operand, N_Op_Abs,
12040 end Integer_Promotion_Possible;
12042 ------------------------------
12043 -- Make_Array_Comparison_Op --
12044 ------------------------------
12046 -- This is a hand-coded expansion of the following generic function:
12049 -- type elem is (<>);
12050 -- type index is (<>);
12051 -- type a is array (index range <>) of elem;
12053 -- function Gnnn (X : a; Y: a) return boolean is
12054 -- J : index := Y'first;
12057 -- if X'length = 0 then
12060 -- elsif Y'length = 0 then
12064 -- for I in X'range loop
12065 -- if X (I) = Y (J) then
12066 -- if J = Y'last then
12069 -- J := index'succ (J);
12073 -- return X (I) > Y (J);
12077 -- return X'length > Y'length;
12081 -- Note that since we are essentially doing this expansion by hand, we
12082 -- do not need to generate an actual or formal generic part, just the
12083 -- instantiated function itself.
12085 -- Perhaps we could have the actual generic available in the run-time,
12086 -- obtained by rtsfind, and actually expand a real instantiation ???
12088 function Make_Array_Comparison_Op
12090 Nod : Node_Id) return Node_Id
12092 Loc : constant Source_Ptr := Sloc (Nod);
12094 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
12095 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
12096 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
12097 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
12099 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
12101 Loop_Statement : Node_Id;
12102 Loop_Body : Node_Id;
12104 Inner_If : Node_Id;
12105 Final_Expr : Node_Id;
12106 Func_Body : Node_Id;
12107 Func_Name : Entity_Id;
12113 -- if J = Y'last then
12116 -- J := index'succ (J);
12120 Make_Implicit_If_Statement (Nod,
12123 Left_Opnd => New_Occurrence_Of (J, Loc),
12125 Make_Attribute_Reference (Loc,
12126 Prefix => New_Occurrence_Of (Y, Loc),
12127 Attribute_Name => Name_Last)),
12129 Then_Statements => New_List (
12130 Make_Exit_Statement (Loc)),
12134 Make_Assignment_Statement (Loc,
12135 Name => New_Occurrence_Of (J, Loc),
12137 Make_Attribute_Reference (Loc,
12138 Prefix => New_Occurrence_Of (Index, Loc),
12139 Attribute_Name => Name_Succ,
12140 Expressions => New_List (New_Occurrence_Of (J, Loc))))));
12142 -- if X (I) = Y (J) then
12145 -- return X (I) > Y (J);
12149 Make_Implicit_If_Statement (Nod,
12153 Make_Indexed_Component (Loc,
12154 Prefix => New_Occurrence_Of (X, Loc),
12155 Expressions => New_List (New_Occurrence_Of (I, Loc))),
12158 Make_Indexed_Component (Loc,
12159 Prefix => New_Occurrence_Of (Y, Loc),
12160 Expressions => New_List (New_Occurrence_Of (J, Loc)))),
12162 Then_Statements => New_List (Inner_If),
12164 Else_Statements => New_List (
12165 Make_Simple_Return_Statement (Loc,
12169 Make_Indexed_Component (Loc,
12170 Prefix => New_Occurrence_Of (X, Loc),
12171 Expressions => New_List (New_Occurrence_Of (I, Loc))),
12174 Make_Indexed_Component (Loc,
12175 Prefix => New_Occurrence_Of (Y, Loc),
12176 Expressions => New_List (
12177 New_Occurrence_Of (J, Loc)))))));
12179 -- for I in X'range loop
12184 Make_Implicit_Loop_Statement (Nod,
12185 Identifier => Empty,
12187 Iteration_Scheme =>
12188 Make_Iteration_Scheme (Loc,
12189 Loop_Parameter_Specification =>
12190 Make_Loop_Parameter_Specification (Loc,
12191 Defining_Identifier => I,
12192 Discrete_Subtype_Definition =>
12193 Make_Attribute_Reference (Loc,
12194 Prefix => New_Occurrence_Of (X, Loc),
12195 Attribute_Name => Name_Range))),
12197 Statements => New_List (Loop_Body));
12199 -- if X'length = 0 then
12201 -- elsif Y'length = 0 then
12204 -- for ... loop ... end loop;
12205 -- return X'length > Y'length;
12209 Make_Attribute_Reference (Loc,
12210 Prefix => New_Occurrence_Of (X, Loc),
12211 Attribute_Name => Name_Length);
12214 Make_Attribute_Reference (Loc,
12215 Prefix => New_Occurrence_Of (Y, Loc),
12216 Attribute_Name => Name_Length);
12220 Left_Opnd => Length1,
12221 Right_Opnd => Length2);
12224 Make_Implicit_If_Statement (Nod,
12228 Make_Attribute_Reference (Loc,
12229 Prefix => New_Occurrence_Of (X, Loc),
12230 Attribute_Name => Name_Length),
12232 Make_Integer_Literal (Loc, 0)),
12236 Make_Simple_Return_Statement (Loc,
12237 Expression => New_Occurrence_Of (Standard_False, Loc))),
12239 Elsif_Parts => New_List (
12240 Make_Elsif_Part (Loc,
12244 Make_Attribute_Reference (Loc,
12245 Prefix => New_Occurrence_Of (Y, Loc),
12246 Attribute_Name => Name_Length),
12248 Make_Integer_Literal (Loc, 0)),
12252 Make_Simple_Return_Statement (Loc,
12253 Expression => New_Occurrence_Of (Standard_True, Loc))))),
12255 Else_Statements => New_List (
12257 Make_Simple_Return_Statement (Loc,
12258 Expression => Final_Expr)));
12262 Formals := New_List (
12263 Make_Parameter_Specification (Loc,
12264 Defining_Identifier => X,
12265 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
12267 Make_Parameter_Specification (Loc,
12268 Defining_Identifier => Y,
12269 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
12271 -- function Gnnn (...) return boolean is
12272 -- J : index := Y'first;
12277 Func_Name := Make_Temporary (Loc, 'G');
12280 Make_Subprogram_Body (Loc,
12282 Make_Function_Specification (Loc,
12283 Defining_Unit_Name => Func_Name,
12284 Parameter_Specifications => Formals,
12285 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
12287 Declarations => New_List (
12288 Make_Object_Declaration (Loc,
12289 Defining_Identifier => J,
12290 Object_Definition => New_Occurrence_Of (Index, Loc),
12292 Make_Attribute_Reference (Loc,
12293 Prefix => New_Occurrence_Of (Y, Loc),
12294 Attribute_Name => Name_First))),
12296 Handled_Statement_Sequence =>
12297 Make_Handled_Sequence_Of_Statements (Loc,
12298 Statements => New_List (If_Stat)));
12301 end Make_Array_Comparison_Op;
12303 ---------------------------
12304 -- Make_Boolean_Array_Op --
12305 ---------------------------
12307 -- For logical operations on boolean arrays, expand in line the following,
12308 -- replacing 'and' with 'or' or 'xor' where needed:
12310 -- function Annn (A : typ; B: typ) return typ is
12313 -- for J in A'range loop
12314 -- C (J) := A (J) op B (J);
12319 -- Here typ is the boolean array type
12321 function Make_Boolean_Array_Op
12323 N : Node_Id) return Node_Id
12325 Loc : constant Source_Ptr := Sloc (N);
12327 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
12328 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
12329 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
12330 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
12338 Func_Name : Entity_Id;
12339 Func_Body : Node_Id;
12340 Loop_Statement : Node_Id;
12344 Make_Indexed_Component (Loc,
12345 Prefix => New_Occurrence_Of (A, Loc),
12346 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12349 Make_Indexed_Component (Loc,
12350 Prefix => New_Occurrence_Of (B, Loc),
12351 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12354 Make_Indexed_Component (Loc,
12355 Prefix => New_Occurrence_Of (C, Loc),
12356 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12358 if Nkind (N) = N_Op_And then
12362 Right_Opnd => B_J);
12364 elsif Nkind (N) = N_Op_Or then
12368 Right_Opnd => B_J);
12374 Right_Opnd => B_J);
12378 Make_Implicit_Loop_Statement (N,
12379 Identifier => Empty,
12381 Iteration_Scheme =>
12382 Make_Iteration_Scheme (Loc,
12383 Loop_Parameter_Specification =>
12384 Make_Loop_Parameter_Specification (Loc,
12385 Defining_Identifier => J,
12386 Discrete_Subtype_Definition =>
12387 Make_Attribute_Reference (Loc,
12388 Prefix => New_Occurrence_Of (A, Loc),
12389 Attribute_Name => Name_Range))),
12391 Statements => New_List (
12392 Make_Assignment_Statement (Loc,
12394 Expression => Op)));
12396 Formals := New_List (
12397 Make_Parameter_Specification (Loc,
12398 Defining_Identifier => A,
12399 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
12401 Make_Parameter_Specification (Loc,
12402 Defining_Identifier => B,
12403 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
12405 Func_Name := Make_Temporary (Loc, 'A');
12406 Set_Is_Inlined (Func_Name);
12409 Make_Subprogram_Body (Loc,
12411 Make_Function_Specification (Loc,
12412 Defining_Unit_Name => Func_Name,
12413 Parameter_Specifications => Formals,
12414 Result_Definition => New_Occurrence_Of (Typ, Loc)),
12416 Declarations => New_List (
12417 Make_Object_Declaration (Loc,
12418 Defining_Identifier => C,
12419 Object_Definition => New_Occurrence_Of (Typ, Loc))),
12421 Handled_Statement_Sequence =>
12422 Make_Handled_Sequence_Of_Statements (Loc,
12423 Statements => New_List (
12425 Make_Simple_Return_Statement (Loc,
12426 Expression => New_Occurrence_Of (C, Loc)))));
12429 end Make_Boolean_Array_Op;
12431 -----------------------------------------
12432 -- Minimized_Eliminated_Overflow_Check --
12433 -----------------------------------------
12435 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is
12438 Is_Signed_Integer_Type (Etype (N))
12439 and then Overflow_Check_Mode in Minimized_Or_Eliminated;
12440 end Minimized_Eliminated_Overflow_Check;
12442 --------------------------------
12443 -- Optimize_Length_Comparison --
12444 --------------------------------
12446 procedure Optimize_Length_Comparison (N : Node_Id) is
12447 Loc : constant Source_Ptr := Sloc (N);
12448 Typ : constant Entity_Id := Etype (N);
12453 -- First and Last attribute reference nodes, which end up as left and
12454 -- right operands of the optimized result.
12457 -- True for comparison operand of zero
12460 -- Comparison operand, set only if Is_Zero is false
12463 -- Entity whose length is being compared
12466 -- Integer_Literal node for length attribute expression, or Empty
12467 -- if there is no such expression present.
12470 -- Type of array index to which 'Length is applied
12472 Op : Node_Kind := Nkind (N);
12473 -- Kind of comparison operator, gets flipped if operands backwards
12475 function Is_Optimizable (N : Node_Id) return Boolean;
12476 -- Tests N to see if it is an optimizable comparison value (defined as
12477 -- constant zero or one, or something else where the value is known to
12478 -- be positive and in the range of 32-bits, and where the corresponding
12479 -- Length value is also known to be 32-bits. If result is true, sets
12480 -- Is_Zero, Ityp, and Comp accordingly.
12482 function Is_Entity_Length (N : Node_Id) return Boolean;
12483 -- Tests if N is a length attribute applied to a simple entity. If so,
12484 -- returns True, and sets Ent to the entity, and Index to the integer
12485 -- literal provided as an attribute expression, or to Empty if none.
12486 -- Also returns True if the expression is a generated type conversion
12487 -- whose expression is of the desired form. This latter case arises
12488 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
12489 -- to check for being in range, which is not needed in this context.
12490 -- Returns False if neither condition holds.
12492 function Prepare_64 (N : Node_Id) return Node_Id;
12493 -- Given a discrete expression, returns a Long_Long_Integer typed
12494 -- expression representing the underlying value of the expression.
12495 -- This is done with an unchecked conversion to the result type. We
12496 -- use unchecked conversion to handle the enumeration type case.
12498 ----------------------
12499 -- Is_Entity_Length --
12500 ----------------------
12502 function Is_Entity_Length (N : Node_Id) return Boolean is
12504 if Nkind (N) = N_Attribute_Reference
12505 and then Attribute_Name (N) = Name_Length
12506 and then Is_Entity_Name (Prefix (N))
12508 Ent := Entity (Prefix (N));
12510 if Present (Expressions (N)) then
12511 Index := First (Expressions (N));
12518 elsif Nkind (N) = N_Type_Conversion
12519 and then not Comes_From_Source (N)
12521 return Is_Entity_Length (Expression (N));
12526 end Is_Entity_Length;
12528 --------------------
12529 -- Is_Optimizable --
12530 --------------------
12532 function Is_Optimizable (N : Node_Id) return Boolean is
12540 if Compile_Time_Known_Value (N) then
12541 Val := Expr_Value (N);
12543 if Val = Uint_0 then
12548 elsif Val = Uint_1 then
12555 -- Here we have to make sure of being within 32-bits
12557 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
12560 or else Lo < Uint_1
12561 or else Hi > UI_From_Int (Int'Last)
12566 -- Comparison value was within range, so now we must check the index
12567 -- value to make sure it is also within 32-bits.
12569 Indx := First_Index (Etype (Ent));
12571 if Present (Index) then
12572 for J in 2 .. UI_To_Int (Intval (Index)) loop
12577 Ityp := Etype (Indx);
12579 if Esize (Ityp) > 32 then
12586 end Is_Optimizable;
12592 function Prepare_64 (N : Node_Id) return Node_Id is
12594 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
12597 -- Start of processing for Optimize_Length_Comparison
12600 -- Nothing to do if not a comparison
12602 if Op not in N_Op_Compare then
12606 -- Nothing to do if special -gnatd.P debug flag set
12608 if Debug_Flag_Dot_PP then
12612 -- Ent'Length op 0/1
12614 if Is_Entity_Length (Left_Opnd (N))
12615 and then Is_Optimizable (Right_Opnd (N))
12619 -- 0/1 op Ent'Length
12621 elsif Is_Entity_Length (Right_Opnd (N))
12622 and then Is_Optimizable (Left_Opnd (N))
12624 -- Flip comparison to opposite sense
12627 when N_Op_Lt => Op := N_Op_Gt;
12628 when N_Op_Le => Op := N_Op_Ge;
12629 when N_Op_Gt => Op := N_Op_Lt;
12630 when N_Op_Ge => Op := N_Op_Le;
12631 when others => null;
12634 -- Else optimization not possible
12640 -- Fall through if we will do the optimization
12642 -- Cases to handle:
12644 -- X'Length = 0 => X'First > X'Last
12645 -- X'Length = 1 => X'First = X'Last
12646 -- X'Length = n => X'First + (n - 1) = X'Last
12648 -- X'Length /= 0 => X'First <= X'Last
12649 -- X'Length /= 1 => X'First /= X'Last
12650 -- X'Length /= n => X'First + (n - 1) /= X'Last
12652 -- X'Length >= 0 => always true, warn
12653 -- X'Length >= 1 => X'First <= X'Last
12654 -- X'Length >= n => X'First + (n - 1) <= X'Last
12656 -- X'Length > 0 => X'First <= X'Last
12657 -- X'Length > 1 => X'First < X'Last
12658 -- X'Length > n => X'First + (n - 1) < X'Last
12660 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
12661 -- X'Length <= 1 => X'First >= X'Last
12662 -- X'Length <= n => X'First + (n - 1) >= X'Last
12664 -- X'Length < 0 => always false (warn)
12665 -- X'Length < 1 => X'First > X'Last
12666 -- X'Length < n => X'First + (n - 1) > X'Last
12668 -- Note: for the cases of n (not constant 0,1), we require that the
12669 -- corresponding index type be integer or shorter (i.e. not 64-bit),
12670 -- and the same for the comparison value. Then we do the comparison
12671 -- using 64-bit arithmetic (actually long long integer), so that we
12672 -- cannot have overflow intefering with the result.
12674 -- First deal with warning cases
12683 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
12684 Analyze_And_Resolve (N, Typ);
12685 Warn_On_Known_Condition (N);
12692 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
12693 Analyze_And_Resolve (N, Typ);
12694 Warn_On_Known_Condition (N);
12698 if Constant_Condition_Warnings
12699 and then Comes_From_Source (Original_Node (N))
12701 Error_Msg_N ("could replace by ""'=""?c?", N);
12711 -- Build the First reference we will use
12714 Make_Attribute_Reference (Loc,
12715 Prefix => New_Occurrence_Of (Ent, Loc),
12716 Attribute_Name => Name_First);
12718 if Present (Index) then
12719 Set_Expressions (Left, New_List (New_Copy (Index)));
12722 -- If general value case, then do the addition of (n - 1), and
12723 -- also add the needed conversions to type Long_Long_Integer.
12725 if Present (Comp) then
12728 Left_Opnd => Prepare_64 (Left),
12730 Make_Op_Subtract (Loc,
12731 Left_Opnd => Prepare_64 (Comp),
12732 Right_Opnd => Make_Integer_Literal (Loc, 1)));
12735 -- Build the Last reference we will use
12738 Make_Attribute_Reference (Loc,
12739 Prefix => New_Occurrence_Of (Ent, Loc),
12740 Attribute_Name => Name_Last);
12742 if Present (Index) then
12743 Set_Expressions (Right, New_List (New_Copy (Index)));
12746 -- If general operand, convert Last reference to Long_Long_Integer
12748 if Present (Comp) then
12749 Right := Prepare_64 (Right);
12752 -- Check for cases to optimize
12754 -- X'Length = 0 => X'First > X'Last
12755 -- X'Length < 1 => X'First > X'Last
12756 -- X'Length < n => X'First + (n - 1) > X'Last
12758 if (Is_Zero and then Op = N_Op_Eq)
12759 or else (not Is_Zero and then Op = N_Op_Lt)
12764 Right_Opnd => Right);
12766 -- X'Length = 1 => X'First = X'Last
12767 -- X'Length = n => X'First + (n - 1) = X'Last
12769 elsif not Is_Zero and then Op = N_Op_Eq then
12773 Right_Opnd => Right);
12775 -- X'Length /= 0 => X'First <= X'Last
12776 -- X'Length > 0 => X'First <= X'Last
12778 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
12782 Right_Opnd => Right);
12784 -- X'Length /= 1 => X'First /= X'Last
12785 -- X'Length /= n => X'First + (n - 1) /= X'Last
12787 elsif not Is_Zero and then Op = N_Op_Ne then
12791 Right_Opnd => Right);
12793 -- X'Length >= 1 => X'First <= X'Last
12794 -- X'Length >= n => X'First + (n - 1) <= X'Last
12796 elsif not Is_Zero and then Op = N_Op_Ge then
12800 Right_Opnd => Right);
12802 -- X'Length > 1 => X'First < X'Last
12803 -- X'Length > n => X'First + (n = 1) < X'Last
12805 elsif not Is_Zero and then Op = N_Op_Gt then
12809 Right_Opnd => Right);
12811 -- X'Length <= 1 => X'First >= X'Last
12812 -- X'Length <= n => X'First + (n - 1) >= X'Last
12814 elsif not Is_Zero and then Op = N_Op_Le then
12818 Right_Opnd => Right);
12820 -- Should not happen at this stage
12823 raise Program_Error;
12826 -- Rewrite and finish up
12828 Rewrite (N, Result);
12829 Analyze_And_Resolve (N, Typ);
12831 end Optimize_Length_Comparison;
12833 ------------------------------
12834 -- Process_Transient_Object --
12835 ------------------------------
12837 procedure Process_Transient_Object
12839 Rel_Node : Node_Id)
12841 Loc : constant Source_Ptr := Sloc (Decl);
12842 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
12843 Obj_Typ : constant Node_Id := Etype (Obj_Id);
12844 Desig_Typ : Entity_Id;
12846 Hook_Id : Entity_Id;
12847 Hook_Insert : Node_Id;
12848 Ptr_Id : Entity_Id;
12850 Hook_Context : constant Node_Id := Find_Hook_Context (Rel_Node);
12851 -- The node on which to insert the hook as an action. This is usually
12852 -- the innermost enclosing non-transient construct.
12854 Fin_Context : Node_Id;
12855 -- The node after which to insert the finalization actions of the
12856 -- transient controlled object.
12859 if Is_Boolean_Type (Etype (Rel_Node)) then
12860 Fin_Context := Last (Actions (Rel_Node));
12862 Fin_Context := Hook_Context;
12865 -- Step 1: Create the access type which provides a reference to the
12866 -- transient controlled object.
12868 if Is_Access_Type (Obj_Typ) then
12869 Desig_Typ := Directly_Designated_Type (Obj_Typ);
12871 Desig_Typ := Obj_Typ;
12874 Desig_Typ := Base_Type (Desig_Typ);
12877 -- Ann : access [all] <Desig_Typ>;
12879 Ptr_Id := Make_Temporary (Loc, 'A');
12881 Insert_Action (Hook_Context,
12882 Make_Full_Type_Declaration (Loc,
12883 Defining_Identifier => Ptr_Id,
12885 Make_Access_To_Object_Definition (Loc,
12886 All_Present => Ekind (Obj_Typ) = E_General_Access_Type,
12887 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc))));
12889 -- Step 2: Create a temporary which acts as a hook to the transient
12890 -- controlled object. Generate:
12892 -- Hook : Ptr_Id := null;
12894 Hook_Id := Make_Temporary (Loc, 'T');
12896 Insert_Action (Hook_Context,
12897 Make_Object_Declaration (Loc,
12898 Defining_Identifier => Hook_Id,
12899 Object_Definition => New_Occurrence_Of (Ptr_Id, Loc)));
12901 -- Mark the hook as created for the purposes of exporting the transient
12902 -- controlled object out of the expression_with_action or if expression.
12903 -- This signals the machinery in Build_Finalizer to treat this case in
12904 -- a special manner.
12906 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Decl);
12908 -- Step 3: Associate the transient object to the hook
12910 -- This must be inserted right after the object declaration, so that
12911 -- the assignment is executed if, and only if, the object is actually
12912 -- created (whereas the declaration of the hook pointer, and the
12913 -- finalization call, may be inserted at an outer level, and may
12914 -- remain unused for some executions, if the actual creation of
12915 -- the object is conditional).
12917 -- The use of unchecked conversion / unrestricted access is needed to
12918 -- avoid an accessibility violation. Note that the finalization code is
12919 -- structured in such a way that the "hook" is processed only when it
12920 -- points to an existing object.
12922 if Is_Access_Type (Obj_Typ) then
12924 Unchecked_Convert_To
12926 Expr => New_Occurrence_Of (Obj_Id, Loc));
12929 Make_Attribute_Reference (Loc,
12930 Prefix => New_Occurrence_Of (Obj_Id, Loc),
12931 Attribute_Name => Name_Unrestricted_Access);
12935 -- Hook := Ptr_Id (Obj_Id);
12937 -- Hook := Obj_Id'Unrestricted_Access;
12939 -- When the transient object is initialized by an aggregate, the hook
12940 -- must capture the object after the last component assignment takes
12941 -- place. Only then is the object fully initialized.
12943 if Ekind (Obj_Id) = E_Variable
12944 and then Present (Last_Aggregate_Assignment (Obj_Id))
12946 Hook_Insert := Last_Aggregate_Assignment (Obj_Id);
12948 -- Otherwise the hook seizes the related object immediately
12951 Hook_Insert := Decl;
12954 Insert_After_And_Analyze (Hook_Insert,
12955 Make_Assignment_Statement (Loc,
12956 Name => New_Occurrence_Of (Hook_Id, Loc),
12957 Expression => Expr));
12959 -- Step 4: Finalize the hook after the context has been evaluated or
12960 -- elaborated. Generate:
12962 -- if Hook /= null then
12963 -- [Deep_]Finalize (Hook.all);
12967 -- When the node is part of a return statement, there is no need to
12968 -- insert a finalization call, as the general finalization mechanism
12969 -- (see Build_Finalizer) would take care of the transient controlled
12970 -- object on subprogram exit. Note that it would also be impossible to
12971 -- insert the finalization code after the return statement as this will
12972 -- render it unreachable.
12974 if Nkind (Fin_Context) = N_Simple_Return_Statement then
12977 -- Otherwise finalize the hook
12980 Insert_Action_After (Fin_Context,
12981 Make_Implicit_If_Statement (Decl,
12984 Left_Opnd => New_Occurrence_Of (Hook_Id, Loc),
12985 Right_Opnd => Make_Null (Loc)),
12987 Then_Statements => New_List (
12990 Make_Explicit_Dereference (Loc,
12991 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
12994 Make_Assignment_Statement (Loc,
12995 Name => New_Occurrence_Of (Hook_Id, Loc),
12996 Expression => Make_Null (Loc)))));
12998 end Process_Transient_Object;
13000 ------------------------
13001 -- Rewrite_Comparison --
13002 ------------------------
13004 procedure Rewrite_Comparison (N : Node_Id) is
13005 Warning_Generated : Boolean := False;
13006 -- Set to True if first pass with Assume_Valid generates a warning in
13007 -- which case we skip the second pass to avoid warning overloaded.
13010 -- Set to Standard_True or Standard_False
13013 if Nkind (N) = N_Type_Conversion then
13014 Rewrite_Comparison (Expression (N));
13017 elsif Nkind (N) not in N_Op_Compare then
13021 -- Now start looking at the comparison in detail. We potentially go
13022 -- through this loop twice. The first time, Assume_Valid is set False
13023 -- in the call to Compile_Time_Compare. If this call results in a
13024 -- clear result of always True or Always False, that's decisive and
13025 -- we are done. Otherwise we repeat the processing with Assume_Valid
13026 -- set to True to generate additional warnings. We can skip that step
13027 -- if Constant_Condition_Warnings is False.
13029 for AV in False .. True loop
13031 Typ : constant Entity_Id := Etype (N);
13032 Op1 : constant Node_Id := Left_Opnd (N);
13033 Op2 : constant Node_Id := Right_Opnd (N);
13035 Res : constant Compare_Result :=
13036 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
13037 -- Res indicates if compare outcome can be compile time determined
13039 True_Result : Boolean;
13040 False_Result : Boolean;
13043 case N_Op_Compare (Nkind (N)) is
13045 True_Result := Res = EQ;
13046 False_Result := Res = LT or else Res = GT or else Res = NE;
13049 True_Result := Res in Compare_GE;
13050 False_Result := Res = LT;
13053 and then Constant_Condition_Warnings
13054 and then Comes_From_Source (Original_Node (N))
13055 and then Nkind (Original_Node (N)) = N_Op_Ge
13056 and then not In_Instance
13057 and then Is_Integer_Type (Etype (Left_Opnd (N)))
13058 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
13061 ("can never be greater than, could replace by ""'=""?c?",
13063 Warning_Generated := True;
13067 True_Result := Res = GT;
13068 False_Result := Res in Compare_LE;
13071 True_Result := Res = LT;
13072 False_Result := Res in Compare_GE;
13075 True_Result := Res in Compare_LE;
13076 False_Result := Res = GT;
13079 and then Constant_Condition_Warnings
13080 and then Comes_From_Source (Original_Node (N))
13081 and then Nkind (Original_Node (N)) = N_Op_Le
13082 and then not In_Instance
13083 and then Is_Integer_Type (Etype (Left_Opnd (N)))
13084 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
13087 ("can never be less than, could replace by ""'=""?c?", N);
13088 Warning_Generated := True;
13092 True_Result := Res = NE or else Res = GT or else Res = LT;
13093 False_Result := Res = EQ;
13096 -- If this is the first iteration, then we actually convert the
13097 -- comparison into True or False, if the result is certain.
13100 if True_Result or False_Result then
13101 Result := Boolean_Literals (True_Result);
13104 New_Occurrence_Of (Result, Sloc (N))));
13105 Analyze_And_Resolve (N, Typ);
13106 Warn_On_Known_Condition (N);
13110 -- If this is the second iteration (AV = True), and the original
13111 -- node comes from source and we are not in an instance, then give
13112 -- a warning if we know result would be True or False. Note: we
13113 -- know Constant_Condition_Warnings is set if we get here.
13115 elsif Comes_From_Source (Original_Node (N))
13116 and then not In_Instance
13118 if True_Result then
13120 ("condition can only be False if invalid values present??",
13122 elsif False_Result then
13124 ("condition can only be True if invalid values present??",
13130 -- Skip second iteration if not warning on constant conditions or
13131 -- if the first iteration already generated a warning of some kind or
13132 -- if we are in any case assuming all values are valid (so that the
13133 -- first iteration took care of the valid case).
13135 exit when not Constant_Condition_Warnings;
13136 exit when Warning_Generated;
13137 exit when Assume_No_Invalid_Values;
13139 end Rewrite_Comparison;
13141 ----------------------------
13142 -- Safe_In_Place_Array_Op --
13143 ----------------------------
13145 function Safe_In_Place_Array_Op
13148 Op2 : Node_Id) return Boolean
13150 Target : Entity_Id;
13152 function Is_Safe_Operand (Op : Node_Id) return Boolean;
13153 -- Operand is safe if it cannot overlap part of the target of the
13154 -- operation. If the operand and the target are identical, the operand
13155 -- is safe. The operand can be empty in the case of negation.
13157 function Is_Unaliased (N : Node_Id) return Boolean;
13158 -- Check that N is a stand-alone entity
13164 function Is_Unaliased (N : Node_Id) return Boolean is
13168 and then No (Address_Clause (Entity (N)))
13169 and then No (Renamed_Object (Entity (N)));
13172 ---------------------
13173 -- Is_Safe_Operand --
13174 ---------------------
13176 function Is_Safe_Operand (Op : Node_Id) return Boolean is
13181 elsif Is_Entity_Name (Op) then
13182 return Is_Unaliased (Op);
13184 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
13185 return Is_Unaliased (Prefix (Op));
13187 elsif Nkind (Op) = N_Slice then
13189 Is_Unaliased (Prefix (Op))
13190 and then Entity (Prefix (Op)) /= Target;
13192 elsif Nkind (Op) = N_Op_Not then
13193 return Is_Safe_Operand (Right_Opnd (Op));
13198 end Is_Safe_Operand;
13200 -- Start of processing for Safe_In_Place_Array_Op
13203 -- Skip this processing if the component size is different from system
13204 -- storage unit (since at least for NOT this would cause problems).
13206 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
13209 -- Cannot do in place stuff if non-standard Boolean representation
13211 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
13214 elsif not Is_Unaliased (Lhs) then
13218 Target := Entity (Lhs);
13219 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
13221 end Safe_In_Place_Array_Op;
13223 -----------------------
13224 -- Tagged_Membership --
13225 -----------------------
13227 -- There are two different cases to consider depending on whether the right
13228 -- operand is a class-wide type or not. If not we just compare the actual
13229 -- tag of the left expr to the target type tag:
13231 -- Left_Expr.Tag = Right_Type'Tag;
13233 -- If it is a class-wide type we use the RT function CW_Membership which is
13234 -- usually implemented by looking in the ancestor tables contained in the
13235 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
13237 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
13238 -- function IW_Membership which is usually implemented by looking in the
13239 -- table of abstract interface types plus the ancestor table contained in
13240 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
13242 procedure Tagged_Membership
13244 SCIL_Node : out Node_Id;
13245 Result : out Node_Id)
13247 Left : constant Node_Id := Left_Opnd (N);
13248 Right : constant Node_Id := Right_Opnd (N);
13249 Loc : constant Source_Ptr := Sloc (N);
13251 Full_R_Typ : Entity_Id;
13252 Left_Type : Entity_Id;
13253 New_Node : Node_Id;
13254 Right_Type : Entity_Id;
13258 SCIL_Node := Empty;
13260 -- Handle entities from the limited view
13262 Left_Type := Available_View (Etype (Left));
13263 Right_Type := Available_View (Etype (Right));
13265 -- In the case where the type is an access type, the test is applied
13266 -- using the designated types (needed in Ada 2012 for implicit anonymous
13267 -- access conversions, for AI05-0149).
13269 if Is_Access_Type (Right_Type) then
13270 Left_Type := Designated_Type (Left_Type);
13271 Right_Type := Designated_Type (Right_Type);
13274 if Is_Class_Wide_Type (Left_Type) then
13275 Left_Type := Root_Type (Left_Type);
13278 if Is_Class_Wide_Type (Right_Type) then
13279 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
13281 Full_R_Typ := Underlying_Type (Right_Type);
13285 Make_Selected_Component (Loc,
13286 Prefix => Relocate_Node (Left),
13288 New_Occurrence_Of (First_Tag_Component (Left_Type), Loc));
13290 if Is_Class_Wide_Type (Right_Type) then
13292 -- No need to issue a run-time check if we statically know that the
13293 -- result of this membership test is always true. For example,
13294 -- considering the following declarations:
13296 -- type Iface is interface;
13297 -- type T is tagged null record;
13298 -- type DT is new T and Iface with null record;
13303 -- These membership tests are always true:
13306 -- Obj2 in T'Class;
13307 -- Obj2 in Iface'Class;
13309 -- We do not need to handle cases where the membership is illegal.
13312 -- Obj1 in DT'Class; -- Compile time error
13313 -- Obj1 in Iface'Class; -- Compile time error
13315 if not Is_Class_Wide_Type (Left_Type)
13316 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
13317 Use_Full_View => True)
13318 or else (Is_Interface (Etype (Right_Type))
13319 and then Interface_Present_In_Ancestor
13321 Iface => Etype (Right_Type))))
13323 Result := New_Occurrence_Of (Standard_True, Loc);
13327 -- Ada 2005 (AI-251): Class-wide applied to interfaces
13329 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
13331 -- Support to: "Iface_CW_Typ in Typ'Class"
13333 or else Is_Interface (Left_Type)
13335 -- Issue error if IW_Membership operation not available in a
13336 -- configurable run time setting.
13338 if not RTE_Available (RE_IW_Membership) then
13340 ("dynamic membership test on interface types", N);
13346 Make_Function_Call (Loc,
13347 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
13348 Parameter_Associations => New_List (
13349 Make_Attribute_Reference (Loc,
13351 Attribute_Name => Name_Address),
13352 New_Occurrence_Of (
13353 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
13356 -- Ada 95: Normal case
13359 Build_CW_Membership (Loc,
13360 Obj_Tag_Node => Obj_Tag,
13362 New_Occurrence_Of (
13363 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc),
13365 New_Node => New_Node);
13367 -- Generate the SCIL node for this class-wide membership test.
13368 -- Done here because the previous call to Build_CW_Membership
13369 -- relocates Obj_Tag.
13371 if Generate_SCIL then
13372 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
13373 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
13374 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
13377 Result := New_Node;
13380 -- Right_Type is not a class-wide type
13383 -- No need to check the tag of the object if Right_Typ is abstract
13385 if Is_Abstract_Type (Right_Type) then
13386 Result := New_Occurrence_Of (Standard_False, Loc);
13391 Left_Opnd => Obj_Tag,
13394 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
13397 end Tagged_Membership;
13399 ------------------------------
13400 -- Unary_Op_Validity_Checks --
13401 ------------------------------
13403 procedure Unary_Op_Validity_Checks (N : Node_Id) is
13405 if Validity_Checks_On and Validity_Check_Operands then
13406 Ensure_Valid (Right_Opnd (N));
13408 end Unary_Op_Validity_Checks;