1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, 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_Ch3; use Exp_Ch3;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Fixd; use Exp_Fixd;
40 with Exp_Pakd; use Exp_Pakd;
41 with Exp_Tss; use Exp_Tss;
42 with Exp_Util; use Exp_Util;
43 with Exp_VFpt; use Exp_VFpt;
44 with Freeze; use Freeze;
45 with Inline; use Inline;
46 with Namet; use Namet;
47 with Nlists; use Nlists;
48 with Nmake; use Nmake;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Res; use Sem_Res;
61 with Sem_Type; use Sem_Type;
62 with Sem_Util; use Sem_Util;
63 with Sem_Warn; use Sem_Warn;
64 with Sinfo; use Sinfo;
65 with Snames; use Snames;
66 with Stand; use Stand;
67 with Targparm; use Targparm;
68 with Tbuild; use Tbuild;
69 with Ttypes; use Ttypes;
70 with Uintp; use Uintp;
71 with Urealp; use Urealp;
72 with Validsw; use Validsw;
74 package body Exp_Ch4 is
76 -----------------------
77 -- Local Subprograms --
78 -----------------------
80 procedure Binary_Op_Validity_Checks (N : Node_Id);
81 pragma Inline (Binary_Op_Validity_Checks);
82 -- Performs validity checks for a binary operator
84 procedure Build_Boolean_Array_Proc_Call
88 -- If a boolean array assignment can be done in place, build call to
89 -- corresponding library procedure.
91 procedure Displace_Allocator_Pointer (N : Node_Id);
92 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
93 -- Expand_Allocator_Expression. Allocating class-wide interface objects
94 -- this routine displaces the pointer to the allocated object to reference
95 -- the component referencing the corresponding secondary dispatch table.
97 procedure Expand_Allocator_Expression (N : Node_Id);
98 -- Subsidiary to Expand_N_Allocator, for the case when the expression
99 -- is a qualified expression or an aggregate.
101 procedure Expand_Array_Comparison (N : Node_Id);
102 -- This routine handles expansion of the comparison operators (N_Op_Lt,
103 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
104 -- code for these operators is similar, differing only in the details of
105 -- the actual comparison call that is made. Special processing (call a
108 function Expand_Array_Equality
113 Typ : Entity_Id) return Node_Id;
114 -- Expand an array equality into a call to a function implementing this
115 -- equality, and a call to it. Loc is the location for the generated nodes.
116 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
117 -- on which to attach bodies of local functions that are created in the
118 -- process. It is the responsibility of the caller to insert those bodies
119 -- at the right place. Nod provides the Sloc value for the generated code.
120 -- Normally the types used for the generated equality routine are taken
121 -- from Lhs and Rhs. However, in some situations of generated code, the
122 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
123 -- the type to be used for the formal parameters.
125 procedure Expand_Boolean_Operator (N : Node_Id);
126 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
127 -- case of array type arguments.
129 function Expand_Composite_Equality
134 Bodies : List_Id) return Node_Id;
135 -- Local recursive function used to expand equality for nested composite
136 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
137 -- to attach bodies of local functions that are created in the process.
138 -- This is the responsibility of the caller to insert those bodies at the
139 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
140 -- are the left and right sides for the comparison, and Typ is the type of
141 -- the arrays to compare.
143 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
144 -- Routine to expand concatenation of a sequence of two or more operands
145 -- (in the list Operands) and replace node Cnode with the result of the
146 -- concatenation. The operands can be of any appropriate type, and can
147 -- include both arrays and singleton elements.
149 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
150 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
151 -- fixed. We do not have such a type at runtime, so the purpose of this
152 -- routine is to find the real type by looking up the tree. We also
153 -- determine if the operation must be rounded.
155 function Get_Allocator_Final_List
158 PtrT : Entity_Id) return Entity_Id;
159 -- If the designated type is controlled, build final_list expression for
160 -- created object. If context is an access parameter, create a local access
161 -- type to have a usable finalization list.
163 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
164 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
165 -- discriminants if it has a constrained nominal type, unless the object
166 -- is a component of an enclosing Unchecked_Union object that is subject
167 -- to a per-object constraint and the enclosing object lacks inferable
170 -- An expression of an Unchecked_Union type has inferable discriminants
171 -- if it is either a name of an object with inferable discriminants or a
172 -- qualified expression whose subtype mark denotes a constrained subtype.
174 procedure Insert_Dereference_Action (N : Node_Id);
175 -- N is an expression whose type is an access. When the type of the
176 -- associated storage pool is derived from Checked_Pool, generate a
177 -- call to the 'Dereference' primitive operation.
179 function Make_Array_Comparison_Op
181 Nod : Node_Id) return Node_Id;
182 -- Comparisons between arrays are expanded in line. This function produces
183 -- the body of the implementation of (a > b), where a and b are one-
184 -- dimensional arrays of some discrete type. The original node is then
185 -- expanded into the appropriate call to this function. Nod provides the
186 -- Sloc value for the generated code.
188 function Make_Boolean_Array_Op
190 N : Node_Id) return Node_Id;
191 -- Boolean operations on boolean arrays are expanded in line. This function
192 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
193 -- b). It is used only the normal case and not the packed case. The type
194 -- involved, Typ, is the Boolean array type, and the logical operations in
195 -- the body are simple boolean operations. Note that Typ is always a
196 -- constrained type (the caller has ensured this by using
197 -- Convert_To_Actual_Subtype if necessary).
199 procedure Rewrite_Comparison (N : Node_Id);
200 -- If N is the node for a comparison whose outcome can be determined at
201 -- compile time, then the node N can be rewritten with True or False. If
202 -- the outcome cannot be determined at compile time, the call has no
203 -- effect. If N is a type conversion, then this processing is applied to
204 -- its expression. If N is neither comparison nor a type conversion, the
205 -- call has no effect.
207 function Tagged_Membership (N : Node_Id) return Node_Id;
208 -- Construct the expression corresponding to the tagged membership test.
209 -- Deals with a second operand being (or not) a class-wide type.
211 function Safe_In_Place_Array_Op
214 Op2 : Node_Id) return Boolean;
215 -- In the context of an assignment, where the right-hand side is a boolean
216 -- operation on arrays, check whether operation can be performed in place.
218 procedure Unary_Op_Validity_Checks (N : Node_Id);
219 pragma Inline (Unary_Op_Validity_Checks);
220 -- Performs validity checks for a unary operator
222 -------------------------------
223 -- Binary_Op_Validity_Checks --
224 -------------------------------
226 procedure Binary_Op_Validity_Checks (N : Node_Id) is
228 if Validity_Checks_On and Validity_Check_Operands then
229 Ensure_Valid (Left_Opnd (N));
230 Ensure_Valid (Right_Opnd (N));
232 end Binary_Op_Validity_Checks;
234 ------------------------------------
235 -- Build_Boolean_Array_Proc_Call --
236 ------------------------------------
238 procedure Build_Boolean_Array_Proc_Call
243 Loc : constant Source_Ptr := Sloc (N);
244 Kind : constant Node_Kind := Nkind (Expression (N));
245 Target : constant Node_Id :=
246 Make_Attribute_Reference (Loc,
248 Attribute_Name => Name_Address);
250 Arg1 : constant Node_Id := Op1;
251 Arg2 : Node_Id := Op2;
253 Proc_Name : Entity_Id;
256 if Kind = N_Op_Not then
257 if Nkind (Op1) in N_Binary_Op then
259 -- Use negated version of the binary operators
261 if Nkind (Op1) = N_Op_And then
262 Proc_Name := RTE (RE_Vector_Nand);
264 elsif Nkind (Op1) = N_Op_Or then
265 Proc_Name := RTE (RE_Vector_Nor);
267 else pragma Assert (Nkind (Op1) = N_Op_Xor);
268 Proc_Name := RTE (RE_Vector_Xor);
272 Make_Procedure_Call_Statement (Loc,
273 Name => New_Occurrence_Of (Proc_Name, Loc),
275 Parameter_Associations => New_List (
277 Make_Attribute_Reference (Loc,
278 Prefix => Left_Opnd (Op1),
279 Attribute_Name => Name_Address),
281 Make_Attribute_Reference (Loc,
282 Prefix => Right_Opnd (Op1),
283 Attribute_Name => Name_Address),
285 Make_Attribute_Reference (Loc,
286 Prefix => Left_Opnd (Op1),
287 Attribute_Name => Name_Length)));
290 Proc_Name := RTE (RE_Vector_Not);
293 Make_Procedure_Call_Statement (Loc,
294 Name => New_Occurrence_Of (Proc_Name, Loc),
295 Parameter_Associations => New_List (
298 Make_Attribute_Reference (Loc,
300 Attribute_Name => Name_Address),
302 Make_Attribute_Reference (Loc,
304 Attribute_Name => Name_Length)));
308 -- We use the following equivalences:
310 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
311 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
312 -- (not X) xor (not Y) = X xor Y
313 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
315 if Nkind (Op1) = N_Op_Not then
316 if Kind = N_Op_And then
317 Proc_Name := RTE (RE_Vector_Nor);
319 elsif Kind = N_Op_Or then
320 Proc_Name := RTE (RE_Vector_Nand);
323 Proc_Name := RTE (RE_Vector_Xor);
327 if Kind = N_Op_And then
328 Proc_Name := RTE (RE_Vector_And);
330 elsif Kind = N_Op_Or then
331 Proc_Name := RTE (RE_Vector_Or);
333 elsif Nkind (Op2) = N_Op_Not then
334 Proc_Name := RTE (RE_Vector_Nxor);
335 Arg2 := Right_Opnd (Op2);
338 Proc_Name := RTE (RE_Vector_Xor);
343 Make_Procedure_Call_Statement (Loc,
344 Name => New_Occurrence_Of (Proc_Name, Loc),
345 Parameter_Associations => New_List (
347 Make_Attribute_Reference (Loc,
349 Attribute_Name => Name_Address),
350 Make_Attribute_Reference (Loc,
352 Attribute_Name => Name_Address),
353 Make_Attribute_Reference (Loc,
355 Attribute_Name => Name_Length)));
358 Rewrite (N, Call_Node);
362 when RE_Not_Available =>
364 end Build_Boolean_Array_Proc_Call;
366 --------------------------------
367 -- Displace_Allocator_Pointer --
368 --------------------------------
370 procedure Displace_Allocator_Pointer (N : Node_Id) is
371 Loc : constant Source_Ptr := Sloc (N);
372 Orig_Node : constant Node_Id := Original_Node (N);
378 -- Do nothing in case of VM targets: the virtual machine will handle
379 -- interfaces directly.
381 if not Tagged_Type_Expansion then
385 pragma Assert (Nkind (N) = N_Identifier
386 and then Nkind (Orig_Node) = N_Allocator);
388 PtrT := Etype (Orig_Node);
389 Dtyp := Available_View (Designated_Type (PtrT));
390 Etyp := Etype (Expression (Orig_Node));
392 if Is_Class_Wide_Type (Dtyp)
393 and then Is_Interface (Dtyp)
395 -- If the type of the allocator expression is not an interface type
396 -- we can generate code to reference the record component containing
397 -- the pointer to the secondary dispatch table.
399 if not Is_Interface (Etyp) then
401 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
404 -- 1) Get access to the allocated object
407 Make_Explicit_Dereference (Loc,
412 -- 2) Add the conversion to displace the pointer to reference
413 -- the secondary dispatch table.
415 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
416 Analyze_And_Resolve (N, Dtyp);
418 -- 3) The 'access to the secondary dispatch table will be used
419 -- as the value returned by the allocator.
422 Make_Attribute_Reference (Loc,
423 Prefix => Relocate_Node (N),
424 Attribute_Name => Name_Access));
425 Set_Etype (N, Saved_Typ);
429 -- If the type of the allocator expression is an interface type we
430 -- generate a run-time call to displace "this" to reference the
431 -- component containing the pointer to the secondary dispatch table
432 -- or else raise Constraint_Error if the actual object does not
433 -- implement the target interface. This case corresponds with the
434 -- following example:
436 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
438 -- return new Iface_2'Class'(Obj);
443 Unchecked_Convert_To (PtrT,
444 Make_Function_Call (Loc,
445 Name => New_Reference_To (RTE (RE_Displace), Loc),
446 Parameter_Associations => New_List (
447 Unchecked_Convert_To (RTE (RE_Address),
453 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
455 Analyze_And_Resolve (N, PtrT);
458 end Displace_Allocator_Pointer;
460 ---------------------------------
461 -- Expand_Allocator_Expression --
462 ---------------------------------
464 procedure Expand_Allocator_Expression (N : Node_Id) is
465 Loc : constant Source_Ptr := Sloc (N);
466 Exp : constant Node_Id := Expression (Expression (N));
467 PtrT : constant Entity_Id := Etype (N);
468 DesigT : constant Entity_Id := Designated_Type (PtrT);
470 procedure Apply_Accessibility_Check
472 Built_In_Place : Boolean := False);
473 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
474 -- type, generate an accessibility check to verify that the level of the
475 -- type of the created object is not deeper than the level of the access
476 -- type. If the type of the qualified expression is class- wide, then
477 -- always generate the check (except in the case where it is known to be
478 -- unnecessary, see comment below). Otherwise, only generate the check
479 -- if the level of the qualified expression type is statically deeper
480 -- than the access type.
482 -- Although the static accessibility will generally have been performed
483 -- as a legality check, it won't have been done in cases where the
484 -- allocator appears in generic body, so a run-time check is needed in
485 -- general. One special case is when the access type is declared in the
486 -- same scope as the class-wide allocator, in which case the check can
487 -- never fail, so it need not be generated.
489 -- As an open issue, there seem to be cases where the static level
490 -- associated with the class-wide object's underlying type is not
491 -- sufficient to perform the proper accessibility check, such as for
492 -- allocators in nested subprograms or accept statements initialized by
493 -- class-wide formals when the actual originates outside at a deeper
494 -- static level. The nested subprogram case might require passing
495 -- accessibility levels along with class-wide parameters, and the task
496 -- case seems to be an actual gap in the language rules that needs to
497 -- be fixed by the ARG. ???
499 -------------------------------
500 -- Apply_Accessibility_Check --
501 -------------------------------
503 procedure Apply_Accessibility_Check
505 Built_In_Place : Boolean := False)
510 -- Note: we skip the accessibility check for the VM case, since
511 -- there does not seem to be any practical way of implementing it.
513 if Ada_Version >= Ada_05
514 and then Tagged_Type_Expansion
515 and then Is_Class_Wide_Type (DesigT)
516 and then not Scope_Suppress (Accessibility_Check)
518 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
520 (Is_Class_Wide_Type (Etype (Exp))
521 and then Scope (PtrT) /= Current_Scope))
523 -- If the allocator was built in place Ref is already a reference
524 -- to the access object initialized to the result of the allocator
525 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise
526 -- it is the entity associated with the object containing the
527 -- address of the allocated object.
529 if Built_In_Place then
530 Ref_Node := New_Copy (Ref);
532 Ref_Node := New_Reference_To (Ref, Loc);
536 Make_Raise_Program_Error (Loc,
540 Build_Get_Access_Level (Loc,
541 Make_Attribute_Reference (Loc,
543 Attribute_Name => Name_Tag)),
545 Make_Integer_Literal (Loc,
546 Type_Access_Level (PtrT))),
547 Reason => PE_Accessibility_Check_Failed));
549 end Apply_Accessibility_Check;
553 Indic : constant Node_Id := Subtype_Mark (Expression (N));
554 T : constant Entity_Id := Entity (Indic);
559 TagT : Entity_Id := Empty;
560 -- Type used as source for tag assignment
562 TagR : Node_Id := Empty;
563 -- Target reference for tag assignment
565 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
567 Tag_Assign : Node_Id;
570 -- Start of processing for Expand_Allocator_Expression
573 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
575 if Is_CPP_Constructor_Call (Exp) then
578 -- Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn
580 -- Allocate the object with no expression
582 Node := Relocate_Node (N);
583 Set_Expression (Node, New_Reference_To (Etype (Exp), Loc));
585 -- Avoid its expansion to avoid generating a call to the default
590 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
593 Make_Object_Declaration (Loc,
594 Defining_Identifier => Temp,
595 Constant_Present => True,
596 Object_Definition => New_Reference_To (PtrT, Loc),
597 Expression => Node));
599 Apply_Accessibility_Check (Temp);
601 -- Locate the enclosing list and insert the C++ constructor call
608 while not Is_List_Member (P) loop
612 Insert_List_After_And_Analyze (P,
613 Build_Initialization_Call (Loc,
615 Make_Explicit_Dereference (Loc,
616 Prefix => New_Reference_To (Temp, Loc)),
618 Constructor_Ref => Exp));
621 Rewrite (N, New_Reference_To (Temp, Loc));
622 Analyze_And_Resolve (N, PtrT);
626 -- Ada 2005 (AI-318-02): If the initialization expression is a call
627 -- to a build-in-place function, then access to the allocated object
628 -- must be passed to the function. Currently we limit such functions
629 -- to those with constrained limited result subtypes, but eventually
630 -- we plan to expand the allowed forms of functions that are treated
631 -- as build-in-place.
633 if Ada_Version >= Ada_05
634 and then Is_Build_In_Place_Function_Call (Exp)
636 Make_Build_In_Place_Call_In_Allocator (N, Exp);
637 Apply_Accessibility_Check (N, Built_In_Place => True);
641 -- Actions inserted before:
642 -- Temp : constant ptr_T := new T'(Expression);
643 -- <no CW> Temp._tag := T'tag;
644 -- <CTRL> Adjust (Finalizable (Temp.all));
645 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
647 -- We analyze by hand the new internal allocator to avoid
648 -- any recursion and inappropriate call to Initialize
650 -- We don't want to remove side effects when the expression must be
651 -- built in place. In the case of a build-in-place function call,
652 -- that could lead to a duplication of the call, which was already
653 -- substituted for the allocator.
655 if not Aggr_In_Place then
656 Remove_Side_Effects (Exp);
660 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
662 -- For a class wide allocation generate the following code:
664 -- type Equiv_Record is record ... end record;
665 -- implicit subtype CW is <Class_Wide_Subytpe>;
666 -- temp : PtrT := new CW'(CW!(expr));
668 if Is_Class_Wide_Type (T) then
669 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
671 -- Ada 2005 (AI-251): If the expression is a class-wide interface
672 -- object we generate code to move up "this" to reference the
673 -- base of the object before allocating the new object.
675 -- Note that Exp'Address is recursively expanded into a call
676 -- to Base_Address (Exp.Tag)
678 if Is_Class_Wide_Type (Etype (Exp))
679 and then Is_Interface (Etype (Exp))
680 and then Tagged_Type_Expansion
684 Unchecked_Convert_To (Entity (Indic),
685 Make_Explicit_Dereference (Loc,
686 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
687 Make_Attribute_Reference (Loc,
689 Attribute_Name => Name_Address)))));
694 Unchecked_Convert_To (Entity (Indic), Exp));
697 Analyze_And_Resolve (Expression (N), Entity (Indic));
700 -- Keep separate the management of allocators returning interfaces
702 if not Is_Interface (Directly_Designated_Type (PtrT)) then
703 if Aggr_In_Place then
705 Make_Object_Declaration (Loc,
706 Defining_Identifier => Temp,
707 Object_Definition => New_Reference_To (PtrT, Loc),
710 New_Reference_To (Etype (Exp), Loc)));
712 -- Copy the Comes_From_Source flag for the allocator we just
713 -- built, since logically this allocator is a replacement of
714 -- the original allocator node. This is for proper handling of
715 -- restriction No_Implicit_Heap_Allocations.
717 Set_Comes_From_Source
718 (Expression (Tmp_Node), Comes_From_Source (N));
720 Set_No_Initialization (Expression (Tmp_Node));
721 Insert_Action (N, Tmp_Node);
723 if Needs_Finalization (T)
724 and then Ekind (PtrT) = E_Anonymous_Access_Type
726 -- Create local finalization list for access parameter
728 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
731 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
734 Node := Relocate_Node (N);
737 Make_Object_Declaration (Loc,
738 Defining_Identifier => Temp,
739 Constant_Present => True,
740 Object_Definition => New_Reference_To (PtrT, Loc),
741 Expression => Node));
744 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
745 -- interface type. In this case we use the type of the qualified
746 -- expression to allocate the object.
750 Def_Id : constant Entity_Id :=
751 Make_Defining_Identifier (Loc,
752 New_Internal_Name ('T'));
757 Make_Full_Type_Declaration (Loc,
758 Defining_Identifier => Def_Id,
760 Make_Access_To_Object_Definition (Loc,
762 Null_Exclusion_Present => False,
763 Constant_Present => False,
764 Subtype_Indication =>
765 New_Reference_To (Etype (Exp), Loc)));
767 Insert_Action (N, New_Decl);
769 -- Inherit the final chain to ensure that the expansion of the
770 -- aggregate is correct in case of controlled types
772 if Needs_Finalization (Directly_Designated_Type (PtrT)) then
773 Set_Associated_Final_Chain (Def_Id,
774 Associated_Final_Chain (PtrT));
777 -- Declare the object using the previous type declaration
779 if Aggr_In_Place then
781 Make_Object_Declaration (Loc,
782 Defining_Identifier => Temp,
783 Object_Definition => New_Reference_To (Def_Id, Loc),
786 New_Reference_To (Etype (Exp), Loc)));
788 -- Copy the Comes_From_Source flag for the allocator we just
789 -- built, since logically this allocator is a replacement of
790 -- the original allocator node. This is for proper handling
791 -- of restriction No_Implicit_Heap_Allocations.
793 Set_Comes_From_Source
794 (Expression (Tmp_Node), Comes_From_Source (N));
796 Set_No_Initialization (Expression (Tmp_Node));
797 Insert_Action (N, Tmp_Node);
799 if Needs_Finalization (T)
800 and then Ekind (PtrT) = E_Anonymous_Access_Type
802 -- Create local finalization list for access parameter
805 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
808 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
810 Node := Relocate_Node (N);
813 Make_Object_Declaration (Loc,
814 Defining_Identifier => Temp,
815 Constant_Present => True,
816 Object_Definition => New_Reference_To (Def_Id, Loc),
817 Expression => Node));
820 -- Generate an additional object containing the address of the
821 -- returned object. The type of this second object declaration
822 -- is the correct type required for the common processing that
823 -- is still performed by this subprogram. The displacement of
824 -- this pointer to reference the component associated with the
825 -- interface type will be done at the end of common processing.
828 Make_Object_Declaration (Loc,
829 Defining_Identifier => Make_Defining_Identifier (Loc,
830 New_Internal_Name ('P')),
831 Object_Definition => New_Reference_To (PtrT, Loc),
832 Expression => Unchecked_Convert_To (PtrT,
833 New_Reference_To (Temp, Loc)));
835 Insert_Action (N, New_Decl);
837 Tmp_Node := New_Decl;
838 Temp := Defining_Identifier (New_Decl);
842 Apply_Accessibility_Check (Temp);
844 -- Generate the tag assignment
846 -- Suppress the tag assignment when VM_Target because VM tags are
847 -- represented implicitly in objects.
849 if not Tagged_Type_Expansion then
852 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
853 -- interface objects because in this case the tag does not change.
855 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
856 pragma Assert (Is_Class_Wide_Type
857 (Directly_Designated_Type (Etype (N))));
860 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
862 TagR := New_Reference_To (Temp, Loc);
864 elsif Is_Private_Type (T)
865 and then Is_Tagged_Type (Underlying_Type (T))
867 TagT := Underlying_Type (T);
869 Unchecked_Convert_To (Underlying_Type (T),
870 Make_Explicit_Dereference (Loc,
871 Prefix => New_Reference_To (Temp, Loc)));
874 if Present (TagT) then
876 Make_Assignment_Statement (Loc,
878 Make_Selected_Component (Loc,
881 New_Reference_To (First_Tag_Component (TagT), Loc)),
884 Unchecked_Convert_To (RTE (RE_Tag),
886 (Elists.Node (First_Elmt (Access_Disp_Table (TagT))),
889 -- The previous assignment has to be done in any case
891 Set_Assignment_OK (Name (Tag_Assign));
892 Insert_Action (N, Tag_Assign);
895 if Needs_Finalization (DesigT)
896 and then Needs_Finalization (T)
900 Apool : constant Entity_Id :=
901 Associated_Storage_Pool (PtrT);
904 -- If it is an allocation on the secondary stack (i.e. a value
905 -- returned from a function), the object is attached on the
906 -- caller side as soon as the call is completed (see
907 -- Expand_Ctrl_Function_Call)
909 if Is_RTE (Apool, RE_SS_Pool) then
911 F : constant Entity_Id :=
912 Make_Defining_Identifier (Loc,
913 New_Internal_Name ('F'));
916 Make_Object_Declaration (Loc,
917 Defining_Identifier => F,
918 Object_Definition => New_Reference_To (RTE
919 (RE_Finalizable_Ptr), Loc)));
921 Flist := New_Reference_To (F, Loc);
922 Attach := Make_Integer_Literal (Loc, 1);
925 -- Normal case, not a secondary stack allocation
928 if Needs_Finalization (T)
929 and then Ekind (PtrT) = E_Anonymous_Access_Type
931 -- Create local finalization list for access parameter
934 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
936 Flist := Find_Final_List (PtrT);
939 Attach := Make_Integer_Literal (Loc, 2);
942 -- Generate an Adjust call if the object will be moved. In Ada
943 -- 2005, the object may be inherently limited, in which case
944 -- there is no Adjust procedure, and the object is built in
945 -- place. In Ada 95, the object can be limited but not
946 -- inherently limited if this allocator came from a return
947 -- statement (we're allocating the result on the secondary
948 -- stack). In that case, the object will be moved, so we _do_
952 and then not Is_Inherently_Limited_Type (T)
958 -- An unchecked conversion is needed in the classwide
959 -- case because the designated type can be an ancestor of
960 -- the subtype mark of the allocator.
962 Unchecked_Convert_To (T,
963 Make_Explicit_Dereference (Loc,
964 Prefix => New_Reference_To (Temp, Loc))),
968 With_Attach => Attach,
974 Rewrite (N, New_Reference_To (Temp, Loc));
975 Analyze_And_Resolve (N, PtrT);
977 -- Ada 2005 (AI-251): Displace the pointer to reference the record
978 -- component containing the secondary dispatch table of the interface
981 if Is_Interface (Directly_Designated_Type (PtrT)) then
982 Displace_Allocator_Pointer (N);
985 elsif Aggr_In_Place then
987 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
989 Make_Object_Declaration (Loc,
990 Defining_Identifier => Temp,
991 Object_Definition => New_Reference_To (PtrT, Loc),
992 Expression => Make_Allocator (Loc,
993 New_Reference_To (Etype (Exp), Loc)));
995 -- Copy the Comes_From_Source flag for the allocator we just built,
996 -- since logically this allocator is a replacement of the original
997 -- allocator node. This is for proper handling of restriction
998 -- No_Implicit_Heap_Allocations.
1000 Set_Comes_From_Source
1001 (Expression (Tmp_Node), Comes_From_Source (N));
1003 Set_No_Initialization (Expression (Tmp_Node));
1004 Insert_Action (N, Tmp_Node);
1005 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
1006 Rewrite (N, New_Reference_To (Temp, Loc));
1007 Analyze_And_Resolve (N, PtrT);
1009 elsif Is_Access_Type (T)
1010 and then Can_Never_Be_Null (T)
1012 Install_Null_Excluding_Check (Exp);
1014 elsif Is_Access_Type (DesigT)
1015 and then Nkind (Exp) = N_Allocator
1016 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1018 -- Apply constraint to designated subtype indication
1020 Apply_Constraint_Check (Expression (Exp),
1021 Designated_Type (DesigT),
1022 No_Sliding => True);
1024 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1026 -- Propagate constraint_error to enclosing allocator
1028 Rewrite (Exp, New_Copy (Expression (Exp)));
1032 -- type A is access T1;
1033 -- X : A := new T2'(...);
1034 -- T1 and T2 can be different subtypes, and we might need to check
1035 -- both constraints. First check against the type of the qualified
1038 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1040 if Do_Range_Check (Exp) then
1041 Set_Do_Range_Check (Exp, False);
1042 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1045 -- A check is also needed in cases where the designated subtype is
1046 -- constrained and differs from the subtype given in the qualified
1047 -- expression. Note that the check on the qualified expression does
1048 -- not allow sliding, but this check does (a relaxation from Ada 83).
1050 if Is_Constrained (DesigT)
1051 and then not Subtypes_Statically_Match (T, DesigT)
1053 Apply_Constraint_Check
1054 (Exp, DesigT, No_Sliding => False);
1056 if Do_Range_Check (Exp) then
1057 Set_Do_Range_Check (Exp, False);
1058 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1062 -- For an access to unconstrained packed array, GIGI needs to see an
1063 -- expression with a constrained subtype in order to compute the
1064 -- proper size for the allocator.
1066 if Is_Array_Type (T)
1067 and then not Is_Constrained (T)
1068 and then Is_Packed (T)
1071 ConstrT : constant Entity_Id :=
1072 Make_Defining_Identifier (Loc,
1073 Chars => New_Internal_Name ('A'));
1074 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1077 Make_Subtype_Declaration (Loc,
1078 Defining_Identifier => ConstrT,
1079 Subtype_Indication =>
1080 Make_Subtype_From_Expr (Exp, T)));
1081 Freeze_Itype (ConstrT, Exp);
1082 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1086 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1087 -- to a build-in-place function, then access to the allocated object
1088 -- must be passed to the function. Currently we limit such functions
1089 -- to those with constrained limited result subtypes, but eventually
1090 -- we plan to expand the allowed forms of functions that are treated
1091 -- as build-in-place.
1093 if Ada_Version >= Ada_05
1094 and then Is_Build_In_Place_Function_Call (Exp)
1096 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1101 when RE_Not_Available =>
1103 end Expand_Allocator_Expression;
1105 -----------------------------
1106 -- Expand_Array_Comparison --
1107 -----------------------------
1109 -- Expansion is only required in the case of array types. For the unpacked
1110 -- case, an appropriate runtime routine is called. For packed cases, and
1111 -- also in some other cases where a runtime routine cannot be called, the
1112 -- form of the expansion is:
1114 -- [body for greater_nn; boolean_expression]
1116 -- The body is built by Make_Array_Comparison_Op, and the form of the
1117 -- Boolean expression depends on the operator involved.
1119 procedure Expand_Array_Comparison (N : Node_Id) is
1120 Loc : constant Source_Ptr := Sloc (N);
1121 Op1 : Node_Id := Left_Opnd (N);
1122 Op2 : Node_Id := Right_Opnd (N);
1123 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1124 Ctyp : constant Entity_Id := Component_Type (Typ1);
1127 Func_Body : Node_Id;
1128 Func_Name : Entity_Id;
1132 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1133 -- True for byte addressable target
1135 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1136 -- Returns True if the length of the given operand is known to be less
1137 -- than 4. Returns False if this length is known to be four or greater
1138 -- or is not known at compile time.
1140 ------------------------
1141 -- Length_Less_Than_4 --
1142 ------------------------
1144 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1145 Otyp : constant Entity_Id := Etype (Opnd);
1148 if Ekind (Otyp) = E_String_Literal_Subtype then
1149 return String_Literal_Length (Otyp) < 4;
1153 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1154 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1155 Hi : constant Node_Id := Type_High_Bound (Ityp);
1160 if Compile_Time_Known_Value (Lo) then
1161 Lov := Expr_Value (Lo);
1166 if Compile_Time_Known_Value (Hi) then
1167 Hiv := Expr_Value (Hi);
1172 return Hiv < Lov + 3;
1175 end Length_Less_Than_4;
1177 -- Start of processing for Expand_Array_Comparison
1180 -- Deal first with unpacked case, where we can call a runtime routine
1181 -- except that we avoid this for targets for which are not addressable
1182 -- by bytes, and for the JVM/CIL, since they do not support direct
1183 -- addressing of array components.
1185 if not Is_Bit_Packed_Array (Typ1)
1186 and then Byte_Addressable
1187 and then VM_Target = No_VM
1189 -- The call we generate is:
1191 -- Compare_Array_xn[_Unaligned]
1192 -- (left'address, right'address, left'length, right'length) <op> 0
1194 -- x = U for unsigned, S for signed
1195 -- n = 8,16,32,64 for component size
1196 -- Add _Unaligned if length < 4 and component size is 8.
1197 -- <op> is the standard comparison operator
1199 if Component_Size (Typ1) = 8 then
1200 if Length_Less_Than_4 (Op1)
1202 Length_Less_Than_4 (Op2)
1204 if Is_Unsigned_Type (Ctyp) then
1205 Comp := RE_Compare_Array_U8_Unaligned;
1207 Comp := RE_Compare_Array_S8_Unaligned;
1211 if Is_Unsigned_Type (Ctyp) then
1212 Comp := RE_Compare_Array_U8;
1214 Comp := RE_Compare_Array_S8;
1218 elsif Component_Size (Typ1) = 16 then
1219 if Is_Unsigned_Type (Ctyp) then
1220 Comp := RE_Compare_Array_U16;
1222 Comp := RE_Compare_Array_S16;
1225 elsif Component_Size (Typ1) = 32 then
1226 if Is_Unsigned_Type (Ctyp) then
1227 Comp := RE_Compare_Array_U32;
1229 Comp := RE_Compare_Array_S32;
1232 else pragma Assert (Component_Size (Typ1) = 64);
1233 if Is_Unsigned_Type (Ctyp) then
1234 Comp := RE_Compare_Array_U64;
1236 Comp := RE_Compare_Array_S64;
1240 Remove_Side_Effects (Op1, Name_Req => True);
1241 Remove_Side_Effects (Op2, Name_Req => True);
1244 Make_Function_Call (Sloc (Op1),
1245 Name => New_Occurrence_Of (RTE (Comp), Loc),
1247 Parameter_Associations => New_List (
1248 Make_Attribute_Reference (Loc,
1249 Prefix => Relocate_Node (Op1),
1250 Attribute_Name => Name_Address),
1252 Make_Attribute_Reference (Loc,
1253 Prefix => Relocate_Node (Op2),
1254 Attribute_Name => Name_Address),
1256 Make_Attribute_Reference (Loc,
1257 Prefix => Relocate_Node (Op1),
1258 Attribute_Name => Name_Length),
1260 Make_Attribute_Reference (Loc,
1261 Prefix => Relocate_Node (Op2),
1262 Attribute_Name => Name_Length))));
1265 Make_Integer_Literal (Sloc (Op2),
1268 Analyze_And_Resolve (Op1, Standard_Integer);
1269 Analyze_And_Resolve (Op2, Standard_Integer);
1273 -- Cases where we cannot make runtime call
1275 -- For (a <= b) we convert to not (a > b)
1277 if Chars (N) = Name_Op_Le then
1283 Right_Opnd => Op2)));
1284 Analyze_And_Resolve (N, Standard_Boolean);
1287 -- For < the Boolean expression is
1288 -- greater__nn (op2, op1)
1290 elsif Chars (N) = Name_Op_Lt then
1291 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1295 Op1 := Right_Opnd (N);
1296 Op2 := Left_Opnd (N);
1298 -- For (a >= b) we convert to not (a < b)
1300 elsif Chars (N) = Name_Op_Ge then
1306 Right_Opnd => Op2)));
1307 Analyze_And_Resolve (N, Standard_Boolean);
1310 -- For > the Boolean expression is
1311 -- greater__nn (op1, op2)
1314 pragma Assert (Chars (N) = Name_Op_Gt);
1315 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1318 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1320 Make_Function_Call (Loc,
1321 Name => New_Reference_To (Func_Name, Loc),
1322 Parameter_Associations => New_List (Op1, Op2));
1324 Insert_Action (N, Func_Body);
1326 Analyze_And_Resolve (N, Standard_Boolean);
1329 when RE_Not_Available =>
1331 end Expand_Array_Comparison;
1333 ---------------------------
1334 -- Expand_Array_Equality --
1335 ---------------------------
1337 -- Expand an equality function for multi-dimensional arrays. Here is an
1338 -- example of such a function for Nb_Dimension = 2
1340 -- function Enn (A : atyp; B : btyp) return boolean is
1342 -- if (A'length (1) = 0 or else A'length (2) = 0)
1344 -- (B'length (1) = 0 or else B'length (2) = 0)
1346 -- return True; -- RM 4.5.2(22)
1349 -- if A'length (1) /= B'length (1)
1351 -- A'length (2) /= B'length (2)
1353 -- return False; -- RM 4.5.2(23)
1357 -- A1 : Index_T1 := A'first (1);
1358 -- B1 : Index_T1 := B'first (1);
1362 -- A2 : Index_T2 := A'first (2);
1363 -- B2 : Index_T2 := B'first (2);
1366 -- if A (A1, A2) /= B (B1, B2) then
1370 -- exit when A2 = A'last (2);
1371 -- A2 := Index_T2'succ (A2);
1372 -- B2 := Index_T2'succ (B2);
1376 -- exit when A1 = A'last (1);
1377 -- A1 := Index_T1'succ (A1);
1378 -- B1 := Index_T1'succ (B1);
1385 -- Note on the formal types used (atyp and btyp). If either of the arrays
1386 -- is of a private type, we use the underlying type, and do an unchecked
1387 -- conversion of the actual. If either of the arrays has a bound depending
1388 -- on a discriminant, then we use the base type since otherwise we have an
1389 -- escaped discriminant in the function.
1391 -- If both arrays are constrained and have the same bounds, we can generate
1392 -- a loop with an explicit iteration scheme using a 'Range attribute over
1395 function Expand_Array_Equality
1400 Typ : Entity_Id) return Node_Id
1402 Loc : constant Source_Ptr := Sloc (Nod);
1403 Decls : constant List_Id := New_List;
1404 Index_List1 : constant List_Id := New_List;
1405 Index_List2 : constant List_Id := New_List;
1409 Func_Name : Entity_Id;
1410 Func_Body : Node_Id;
1412 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1413 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1417 -- The parameter types to be used for the formals
1422 Num : Int) return Node_Id;
1423 -- This builds the attribute reference Arr'Nam (Expr)
1425 function Component_Equality (Typ : Entity_Id) return Node_Id;
1426 -- Create one statement to compare corresponding components, designated
1427 -- by a full set of indices.
1429 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1430 -- Given one of the arguments, computes the appropriate type to be used
1431 -- for that argument in the corresponding function formal
1433 function Handle_One_Dimension
1435 Index : Node_Id) return Node_Id;
1436 -- This procedure returns the following code
1439 -- Bn : Index_T := B'First (N);
1443 -- exit when An = A'Last (N);
1444 -- An := Index_T'Succ (An)
1445 -- Bn := Index_T'Succ (Bn)
1449 -- If both indices are constrained and identical, the procedure
1450 -- returns a simpler loop:
1452 -- for An in A'Range (N) loop
1456 -- N is the dimension for which we are generating a loop. Index is the
1457 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1458 -- xxx statement is either the loop or declare for the next dimension
1459 -- or if this is the last dimension the comparison of corresponding
1460 -- components of the arrays.
1462 -- The actual way the code works is to return the comparison of
1463 -- corresponding components for the N+1 call. That's neater!
1465 function Test_Empty_Arrays return Node_Id;
1466 -- This function constructs the test for both arrays being empty
1467 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1469 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1471 function Test_Lengths_Correspond return Node_Id;
1472 -- This function constructs the test for arrays having different lengths
1473 -- in at least one index position, in which case the resulting code is:
1475 -- A'length (1) /= B'length (1)
1477 -- A'length (2) /= B'length (2)
1488 Num : Int) return Node_Id
1492 Make_Attribute_Reference (Loc,
1493 Attribute_Name => Nam,
1494 Prefix => New_Reference_To (Arr, Loc),
1495 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1498 ------------------------
1499 -- Component_Equality --
1500 ------------------------
1502 function Component_Equality (Typ : Entity_Id) return Node_Id is
1507 -- if a(i1...) /= b(j1...) then return false; end if;
1510 Make_Indexed_Component (Loc,
1511 Prefix => Make_Identifier (Loc, Chars (A)),
1512 Expressions => Index_List1);
1515 Make_Indexed_Component (Loc,
1516 Prefix => Make_Identifier (Loc, Chars (B)),
1517 Expressions => Index_List2);
1519 Test := Expand_Composite_Equality
1520 (Nod, Component_Type (Typ), L, R, Decls);
1522 -- If some (sub)component is an unchecked_union, the whole operation
1523 -- will raise program error.
1525 if Nkind (Test) = N_Raise_Program_Error then
1527 -- This node is going to be inserted at a location where a
1528 -- statement is expected: clear its Etype so analysis will set
1529 -- it to the expected Standard_Void_Type.
1531 Set_Etype (Test, Empty);
1536 Make_Implicit_If_Statement (Nod,
1537 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1538 Then_Statements => New_List (
1539 Make_Simple_Return_Statement (Loc,
1540 Expression => New_Occurrence_Of (Standard_False, Loc))));
1542 end Component_Equality;
1548 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1559 T := Underlying_Type (T);
1561 X := First_Index (T);
1562 while Present (X) loop
1563 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1565 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1578 --------------------------
1579 -- Handle_One_Dimension --
1580 ---------------------------
1582 function Handle_One_Dimension
1584 Index : Node_Id) return Node_Id
1586 Need_Separate_Indexes : constant Boolean :=
1588 or else not Is_Constrained (Ltyp);
1589 -- If the index types are identical, and we are working with
1590 -- constrained types, then we can use the same index for both
1593 An : constant Entity_Id := Make_Defining_Identifier (Loc,
1594 Chars => New_Internal_Name ('A'));
1597 Index_T : Entity_Id;
1602 if N > Number_Dimensions (Ltyp) then
1603 return Component_Equality (Ltyp);
1606 -- Case where we generate a loop
1608 Index_T := Base_Type (Etype (Index));
1610 if Need_Separate_Indexes then
1612 Make_Defining_Identifier (Loc,
1613 Chars => New_Internal_Name ('B'));
1618 Append (New_Reference_To (An, Loc), Index_List1);
1619 Append (New_Reference_To (Bn, Loc), Index_List2);
1621 Stm_List := New_List (
1622 Handle_One_Dimension (N + 1, Next_Index (Index)));
1624 if Need_Separate_Indexes then
1626 -- Generate guard for loop, followed by increments of indices
1628 Append_To (Stm_List,
1629 Make_Exit_Statement (Loc,
1632 Left_Opnd => New_Reference_To (An, Loc),
1633 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1635 Append_To (Stm_List,
1636 Make_Assignment_Statement (Loc,
1637 Name => New_Reference_To (An, Loc),
1639 Make_Attribute_Reference (Loc,
1640 Prefix => New_Reference_To (Index_T, Loc),
1641 Attribute_Name => Name_Succ,
1642 Expressions => New_List (New_Reference_To (An, Loc)))));
1644 Append_To (Stm_List,
1645 Make_Assignment_Statement (Loc,
1646 Name => New_Reference_To (Bn, Loc),
1648 Make_Attribute_Reference (Loc,
1649 Prefix => New_Reference_To (Index_T, Loc),
1650 Attribute_Name => Name_Succ,
1651 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1654 -- If separate indexes, we need a declare block for An and Bn, and a
1655 -- loop without an iteration scheme.
1657 if Need_Separate_Indexes then
1659 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1662 Make_Block_Statement (Loc,
1663 Declarations => New_List (
1664 Make_Object_Declaration (Loc,
1665 Defining_Identifier => An,
1666 Object_Definition => New_Reference_To (Index_T, Loc),
1667 Expression => Arr_Attr (A, Name_First, N)),
1669 Make_Object_Declaration (Loc,
1670 Defining_Identifier => Bn,
1671 Object_Definition => New_Reference_To (Index_T, Loc),
1672 Expression => Arr_Attr (B, Name_First, N))),
1674 Handled_Statement_Sequence =>
1675 Make_Handled_Sequence_Of_Statements (Loc,
1676 Statements => New_List (Loop_Stm)));
1678 -- If no separate indexes, return loop statement with explicit
1679 -- iteration scheme on its own
1683 Make_Implicit_Loop_Statement (Nod,
1684 Statements => Stm_List,
1686 Make_Iteration_Scheme (Loc,
1687 Loop_Parameter_Specification =>
1688 Make_Loop_Parameter_Specification (Loc,
1689 Defining_Identifier => An,
1690 Discrete_Subtype_Definition =>
1691 Arr_Attr (A, Name_Range, N))));
1694 end Handle_One_Dimension;
1696 -----------------------
1697 -- Test_Empty_Arrays --
1698 -----------------------
1700 function Test_Empty_Arrays return Node_Id is
1710 for J in 1 .. Number_Dimensions (Ltyp) loop
1713 Left_Opnd => Arr_Attr (A, Name_Length, J),
1714 Right_Opnd => Make_Integer_Literal (Loc, 0));
1718 Left_Opnd => Arr_Attr (B, Name_Length, J),
1719 Right_Opnd => Make_Integer_Literal (Loc, 0));
1728 Left_Opnd => Relocate_Node (Alist),
1729 Right_Opnd => Atest);
1733 Left_Opnd => Relocate_Node (Blist),
1734 Right_Opnd => Btest);
1741 Right_Opnd => Blist);
1742 end Test_Empty_Arrays;
1744 -----------------------------
1745 -- Test_Lengths_Correspond --
1746 -----------------------------
1748 function Test_Lengths_Correspond return Node_Id is
1754 for J in 1 .. Number_Dimensions (Ltyp) loop
1757 Left_Opnd => Arr_Attr (A, Name_Length, J),
1758 Right_Opnd => Arr_Attr (B, Name_Length, J));
1765 Left_Opnd => Relocate_Node (Result),
1766 Right_Opnd => Rtest);
1771 end Test_Lengths_Correspond;
1773 -- Start of processing for Expand_Array_Equality
1776 Ltyp := Get_Arg_Type (Lhs);
1777 Rtyp := Get_Arg_Type (Rhs);
1779 -- For now, if the argument types are not the same, go to the base type,
1780 -- since the code assumes that the formals have the same type. This is
1781 -- fixable in future ???
1783 if Ltyp /= Rtyp then
1784 Ltyp := Base_Type (Ltyp);
1785 Rtyp := Base_Type (Rtyp);
1786 pragma Assert (Ltyp = Rtyp);
1789 -- Build list of formals for function
1791 Formals := New_List (
1792 Make_Parameter_Specification (Loc,
1793 Defining_Identifier => A,
1794 Parameter_Type => New_Reference_To (Ltyp, Loc)),
1796 Make_Parameter_Specification (Loc,
1797 Defining_Identifier => B,
1798 Parameter_Type => New_Reference_To (Rtyp, Loc)));
1800 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
1802 -- Build statement sequence for function
1805 Make_Subprogram_Body (Loc,
1807 Make_Function_Specification (Loc,
1808 Defining_Unit_Name => Func_Name,
1809 Parameter_Specifications => Formals,
1810 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
1812 Declarations => Decls,
1814 Handled_Statement_Sequence =>
1815 Make_Handled_Sequence_Of_Statements (Loc,
1816 Statements => New_List (
1818 Make_Implicit_If_Statement (Nod,
1819 Condition => Test_Empty_Arrays,
1820 Then_Statements => New_List (
1821 Make_Simple_Return_Statement (Loc,
1823 New_Occurrence_Of (Standard_True, Loc)))),
1825 Make_Implicit_If_Statement (Nod,
1826 Condition => Test_Lengths_Correspond,
1827 Then_Statements => New_List (
1828 Make_Simple_Return_Statement (Loc,
1830 New_Occurrence_Of (Standard_False, Loc)))),
1832 Handle_One_Dimension (1, First_Index (Ltyp)),
1834 Make_Simple_Return_Statement (Loc,
1835 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1837 Set_Has_Completion (Func_Name, True);
1838 Set_Is_Inlined (Func_Name);
1840 -- If the array type is distinct from the type of the arguments, it
1841 -- is the full view of a private type. Apply an unchecked conversion
1842 -- to insure that analysis of the call succeeds.
1852 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1854 L := OK_Convert_To (Ltyp, Lhs);
1858 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1860 R := OK_Convert_To (Rtyp, Rhs);
1863 Actuals := New_List (L, R);
1866 Append_To (Bodies, Func_Body);
1869 Make_Function_Call (Loc,
1870 Name => New_Reference_To (Func_Name, Loc),
1871 Parameter_Associations => Actuals);
1872 end Expand_Array_Equality;
1874 -----------------------------
1875 -- Expand_Boolean_Operator --
1876 -----------------------------
1878 -- Note that we first get the actual subtypes of the operands, since we
1879 -- always want to deal with types that have bounds.
1881 procedure Expand_Boolean_Operator (N : Node_Id) is
1882 Typ : constant Entity_Id := Etype (N);
1885 -- Special case of bit packed array where both operands are known to be
1886 -- properly aligned. In this case we use an efficient run time routine
1887 -- to carry out the operation (see System.Bit_Ops).
1889 if Is_Bit_Packed_Array (Typ)
1890 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
1891 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
1893 Expand_Packed_Boolean_Operator (N);
1897 -- For the normal non-packed case, the general expansion is to build
1898 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
1899 -- and then inserting it into the tree. The original operator node is
1900 -- then rewritten as a call to this function. We also use this in the
1901 -- packed case if either operand is a possibly unaligned object.
1904 Loc : constant Source_Ptr := Sloc (N);
1905 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1906 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1907 Func_Body : Node_Id;
1908 Func_Name : Entity_Id;
1911 Convert_To_Actual_Subtype (L);
1912 Convert_To_Actual_Subtype (R);
1913 Ensure_Defined (Etype (L), N);
1914 Ensure_Defined (Etype (R), N);
1915 Apply_Length_Check (R, Etype (L));
1917 if Nkind (N) = N_Op_Xor then
1918 Silly_Boolean_Array_Xor_Test (N, Etype (L));
1921 if Nkind (Parent (N)) = N_Assignment_Statement
1922 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
1924 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
1926 elsif Nkind (Parent (N)) = N_Op_Not
1927 and then Nkind (N) = N_Op_And
1929 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
1934 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
1935 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1936 Insert_Action (N, Func_Body);
1938 -- Now rewrite the expression with a call
1941 Make_Function_Call (Loc,
1942 Name => New_Reference_To (Func_Name, Loc),
1943 Parameter_Associations =>
1946 Make_Type_Conversion
1947 (Loc, New_Reference_To (Etype (L), Loc), R))));
1949 Analyze_And_Resolve (N, Typ);
1952 end Expand_Boolean_Operator;
1954 -------------------------------
1955 -- Expand_Composite_Equality --
1956 -------------------------------
1958 -- This function is only called for comparing internal fields of composite
1959 -- types when these fields are themselves composites. This is a special
1960 -- case because it is not possible to respect normal Ada visibility rules.
1962 function Expand_Composite_Equality
1967 Bodies : List_Id) return Node_Id
1969 Loc : constant Source_Ptr := Sloc (Nod);
1970 Full_Type : Entity_Id;
1975 if Is_Private_Type (Typ) then
1976 Full_Type := Underlying_Type (Typ);
1981 -- Defense against malformed private types with no completion the error
1982 -- will be diagnosed later by check_completion
1984 if No (Full_Type) then
1985 return New_Reference_To (Standard_False, Loc);
1988 Full_Type := Base_Type (Full_Type);
1990 if Is_Array_Type (Full_Type) then
1992 -- If the operand is an elementary type other than a floating-point
1993 -- type, then we can simply use the built-in block bitwise equality,
1994 -- since the predefined equality operators always apply and bitwise
1995 -- equality is fine for all these cases.
1997 if Is_Elementary_Type (Component_Type (Full_Type))
1998 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2000 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2002 -- For composite component types, and floating-point types, use the
2003 -- expansion. This deals with tagged component types (where we use
2004 -- the applicable equality routine) and floating-point, (where we
2005 -- need to worry about negative zeroes), and also the case of any
2006 -- composite type recursively containing such fields.
2009 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2012 elsif Is_Tagged_Type (Full_Type) then
2014 -- Call the primitive operation "=" of this type
2016 if Is_Class_Wide_Type (Full_Type) then
2017 Full_Type := Root_Type (Full_Type);
2020 -- If this is derived from an untagged private type completed with a
2021 -- tagged type, it does not have a full view, so we use the primitive
2022 -- operations of the private type. This check should no longer be
2023 -- necessary when these types receive their full views ???
2025 if Is_Private_Type (Typ)
2026 and then not Is_Tagged_Type (Typ)
2027 and then not Is_Controlled (Typ)
2028 and then Is_Derived_Type (Typ)
2029 and then No (Full_View (Typ))
2031 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2033 Prim := First_Elmt (Primitive_Operations (Full_Type));
2037 Eq_Op := Node (Prim);
2038 exit when Chars (Eq_Op) = Name_Op_Eq
2039 and then Etype (First_Formal (Eq_Op)) =
2040 Etype (Next_Formal (First_Formal (Eq_Op)))
2041 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2043 pragma Assert (Present (Prim));
2046 Eq_Op := Node (Prim);
2049 Make_Function_Call (Loc,
2050 Name => New_Reference_To (Eq_Op, Loc),
2051 Parameter_Associations =>
2053 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2054 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2056 elsif Is_Record_Type (Full_Type) then
2057 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2059 if Present (Eq_Op) then
2060 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2062 -- Inherited equality from parent type. Convert the actuals to
2063 -- match signature of operation.
2066 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2070 Make_Function_Call (Loc,
2071 Name => New_Reference_To (Eq_Op, Loc),
2072 Parameter_Associations =>
2073 New_List (OK_Convert_To (T, Lhs),
2074 OK_Convert_To (T, Rhs)));
2078 -- Comparison between Unchecked_Union components
2080 if Is_Unchecked_Union (Full_Type) then
2082 Lhs_Type : Node_Id := Full_Type;
2083 Rhs_Type : Node_Id := Full_Type;
2084 Lhs_Discr_Val : Node_Id;
2085 Rhs_Discr_Val : Node_Id;
2090 if Nkind (Lhs) = N_Selected_Component then
2091 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2096 if Nkind (Rhs) = N_Selected_Component then
2097 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2100 -- Lhs of the composite equality
2102 if Is_Constrained (Lhs_Type) then
2104 -- Since the enclosing record type can never be an
2105 -- Unchecked_Union (this code is executed for records
2106 -- that do not have variants), we may reference its
2109 if Nkind (Lhs) = N_Selected_Component
2110 and then Has_Per_Object_Constraint (
2111 Entity (Selector_Name (Lhs)))
2114 Make_Selected_Component (Loc,
2115 Prefix => Prefix (Lhs),
2118 Get_Discriminant_Value (
2119 First_Discriminant (Lhs_Type),
2121 Stored_Constraint (Lhs_Type))));
2124 Lhs_Discr_Val := New_Copy (
2125 Get_Discriminant_Value (
2126 First_Discriminant (Lhs_Type),
2128 Stored_Constraint (Lhs_Type)));
2132 -- It is not possible to infer the discriminant since
2133 -- the subtype is not constrained.
2136 Make_Raise_Program_Error (Loc,
2137 Reason => PE_Unchecked_Union_Restriction);
2140 -- Rhs of the composite equality
2142 if Is_Constrained (Rhs_Type) then
2143 if Nkind (Rhs) = N_Selected_Component
2144 and then Has_Per_Object_Constraint (
2145 Entity (Selector_Name (Rhs)))
2148 Make_Selected_Component (Loc,
2149 Prefix => Prefix (Rhs),
2152 Get_Discriminant_Value (
2153 First_Discriminant (Rhs_Type),
2155 Stored_Constraint (Rhs_Type))));
2158 Rhs_Discr_Val := New_Copy (
2159 Get_Discriminant_Value (
2160 First_Discriminant (Rhs_Type),
2162 Stored_Constraint (Rhs_Type)));
2167 Make_Raise_Program_Error (Loc,
2168 Reason => PE_Unchecked_Union_Restriction);
2171 -- Call the TSS equality function with the inferred
2172 -- discriminant values.
2175 Make_Function_Call (Loc,
2176 Name => New_Reference_To (Eq_Op, Loc),
2177 Parameter_Associations => New_List (
2185 -- Shouldn't this be an else, we can't fall through the above
2189 Make_Function_Call (Loc,
2190 Name => New_Reference_To (Eq_Op, Loc),
2191 Parameter_Associations => New_List (Lhs, Rhs));
2195 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2199 -- It can be a simple record or the full view of a scalar private
2201 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2203 end Expand_Composite_Equality;
2205 ------------------------
2206 -- Expand_Concatenate --
2207 ------------------------
2209 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2210 Loc : constant Source_Ptr := Sloc (Cnode);
2212 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2213 -- Result type of concatenation
2215 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2216 -- Component type. Elements of this component type can appear as one
2217 -- of the operands of concatenation as well as arrays.
2219 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2222 Ityp : constant Entity_Id := Base_Type (Istyp);
2223 -- Index type. This is the base type of the index subtype, and is used
2224 -- for all computed bounds (which may be out of range of Istyp in the
2225 -- case of null ranges).
2228 -- This is the type we use to do arithmetic to compute the bounds and
2229 -- lengths of operands. The choice of this type is a little subtle and
2230 -- is discussed in a separate section at the start of the body code.
2232 Concatenation_Error : exception;
2233 -- Raised if concatenation is sure to raise a CE
2235 Result_May_Be_Null : Boolean := True;
2236 -- Reset to False if at least one operand is encountered which is known
2237 -- at compile time to be non-null. Used for handling the special case
2238 -- of setting the high bound to the last operand high bound for a null
2239 -- result, thus ensuring a proper high bound in the super-flat case.
2241 N : constant Nat := List_Length (Opnds);
2242 -- Number of concatenation operands including possibly null operands
2245 -- Number of operands excluding any known to be null, except that the
2246 -- last operand is always retained, in case it provides the bounds for
2250 -- Current operand being processed in the loop through operands. After
2251 -- this loop is complete, always contains the last operand (which is not
2252 -- the same as Operands (NN), since null operands are skipped).
2254 -- Arrays describing the operands, only the first NN entries of each
2255 -- array are set (NN < N when we exclude known null operands).
2257 Is_Fixed_Length : array (1 .. N) of Boolean;
2258 -- True if length of corresponding operand known at compile time
2260 Operands : array (1 .. N) of Node_Id;
2261 -- Set to the corresponding entry in the Opnds list (but note that null
2262 -- operands are excluded, so not all entries in the list are stored).
2264 Fixed_Length : array (1 .. N) of Uint;
2265 -- Set to length of operand. Entries in this array are set only if the
2266 -- corresponding entry in Is_Fixed_Length is True.
2268 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2269 -- Set to lower bound of operand. Either an integer literal in the case
2270 -- where the bound is known at compile time, else actual lower bound.
2271 -- The operand low bound is of type Ityp.
2273 Var_Length : array (1 .. N) of Entity_Id;
2274 -- Set to an entity of type Natural that contains the length of an
2275 -- operand whose length is not known at compile time. Entries in this
2276 -- array are set only if the corresponding entry in Is_Fixed_Length
2277 -- is False. The entity is of type Artyp.
2279 Aggr_Length : array (0 .. N) of Node_Id;
2280 -- The J'th entry in an expression node that represents the total length
2281 -- of operands 1 through J. It is either an integer literal node, or a
2282 -- reference to a constant entity with the right value, so it is fine
2283 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2284 -- entry always is set to zero. The length is of type Artyp.
2286 Low_Bound : Node_Id;
2287 -- A tree node representing the low bound of the result (of type Ityp).
2288 -- This is either an integer literal node, or an identifier reference to
2289 -- a constant entity initialized to the appropriate value.
2291 Last_Opnd_High_Bound : Node_Id;
2292 -- A tree node representing the high bound of the last operand. This
2293 -- need only be set if the result could be null. It is used for the
2294 -- special case of setting the right high bound for a null result.
2295 -- This is of type Ityp.
2297 High_Bound : Node_Id;
2298 -- A tree node representing the high bound of the result (of type Ityp)
2301 -- Result of the concatenation (of type Ityp)
2303 Actions : constant List_Id := New_List;
2304 -- Collect actions to be inserted if Save_Space is False
2306 Save_Space : Boolean;
2307 pragma Warnings (Off, Save_Space);
2308 -- Set to True if we are saving generated code space by calling routines
2309 -- in packages System.Concat_n.
2311 Known_Non_Null_Operand_Seen : Boolean;
2312 -- Set True during generation of the assignements of operands into
2313 -- result once an operand known to be non-null has been seen.
2315 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2316 -- This function makes an N_Integer_Literal node that is returned in
2317 -- analyzed form with the type set to Artyp. Importantly this literal
2318 -- is not flagged as static, so that if we do computations with it that
2319 -- result in statically detected out of range conditions, we will not
2320 -- generate error messages but instead warning messages.
2322 function To_Artyp (X : Node_Id) return Node_Id;
2323 -- Given a node of type Ityp, returns the corresponding value of type
2324 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2325 -- For enum types, the Pos of the value is returned.
2327 function To_Ityp (X : Node_Id) return Node_Id;
2328 -- The inverse function (uses Val in the case of enumeration types)
2330 ------------------------
2331 -- Make_Artyp_Literal --
2332 ------------------------
2334 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2335 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2337 Set_Etype (Result, Artyp);
2338 Set_Analyzed (Result, True);
2339 Set_Is_Static_Expression (Result, False);
2341 end Make_Artyp_Literal;
2347 function To_Artyp (X : Node_Id) return Node_Id is
2349 if Ityp = Base_Type (Artyp) then
2352 elsif Is_Enumeration_Type (Ityp) then
2354 Make_Attribute_Reference (Loc,
2355 Prefix => New_Occurrence_Of (Ityp, Loc),
2356 Attribute_Name => Name_Pos,
2357 Expressions => New_List (X));
2360 return Convert_To (Artyp, X);
2368 function To_Ityp (X : Node_Id) return Node_Id is
2370 if Is_Enumeration_Type (Ityp) then
2372 Make_Attribute_Reference (Loc,
2373 Prefix => New_Occurrence_Of (Ityp, Loc),
2374 Attribute_Name => Name_Val,
2375 Expressions => New_List (X));
2377 -- Case where we will do a type conversion
2380 if Ityp = Base_Type (Artyp) then
2383 return Convert_To (Ityp, X);
2388 -- Local Declarations
2390 Opnd_Typ : Entity_Id;
2398 -- Choose an appropriate computational type
2400 -- We will be doing calculations of lengths and bounds in this routine
2401 -- and computing one from the other in some cases, e.g. getting the high
2402 -- bound by adding the length-1 to the low bound.
2404 -- We can't just use the index type, or even its base type for this
2405 -- purpose for two reasons. First it might be an enumeration type which
2406 -- is not suitable fo computations of any kind, and second it may simply
2407 -- not have enough range. For example if the index type is -128..+127
2408 -- then lengths can be up to 256, which is out of range of the type.
2410 -- For enumeration types, we can simply use Standard_Integer, this is
2411 -- sufficient since the actual number of enumeration literals cannot
2412 -- possibly exceed the range of integer (remember we will be doing the
2413 -- arithmetic with POS values, not representation values).
2415 if Is_Enumeration_Type (Ityp) then
2416 Artyp := Standard_Integer;
2418 -- If index type is Positive, we use the standard unsigned type, to give
2419 -- more room on the top of the range, obviating the need for an overflow
2420 -- check when creating the upper bound. This is needed to avoid junk
2421 -- overflow checks in the common case of String types.
2423 -- ??? Disabled for now
2425 -- elsif Istyp = Standard_Positive then
2426 -- Artyp := Standard_Unsigned;
2428 -- For modular types, we use a 32-bit modular type for types whose size
2429 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2430 -- identity type, and for larger unsigned types we use 64-bits.
2432 elsif Is_Modular_Integer_Type (Ityp) then
2433 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
2434 Artyp := Standard_Unsigned;
2435 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
2438 Artyp := RTE (RE_Long_Long_Unsigned);
2441 -- Similar treatment for signed types
2444 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
2445 Artyp := Standard_Integer;
2446 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
2449 Artyp := Standard_Long_Long_Integer;
2453 -- Supply dummy entry at start of length array
2455 Aggr_Length (0) := Make_Artyp_Literal (0);
2457 -- Go through operands setting up the above arrays
2461 Opnd := Remove_Head (Opnds);
2462 Opnd_Typ := Etype (Opnd);
2464 -- The parent got messed up when we put the operands in a list,
2465 -- so now put back the proper parent for the saved operand.
2467 Set_Parent (Opnd, Parent (Cnode));
2469 -- Set will be True when we have setup one entry in the array
2473 -- Singleton element (or character literal) case
2475 if Base_Type (Opnd_Typ) = Ctyp then
2477 Operands (NN) := Opnd;
2478 Is_Fixed_Length (NN) := True;
2479 Fixed_Length (NN) := Uint_1;
2480 Result_May_Be_Null := False;
2482 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2483 -- since we know that the result cannot be null).
2485 Opnd_Low_Bound (NN) :=
2486 Make_Attribute_Reference (Loc,
2487 Prefix => New_Reference_To (Istyp, Loc),
2488 Attribute_Name => Name_First);
2492 -- String literal case (can only occur for strings of course)
2494 elsif Nkind (Opnd) = N_String_Literal then
2495 Len := String_Literal_Length (Opnd_Typ);
2498 Result_May_Be_Null := False;
2501 -- Capture last operand high bound if result could be null
2503 if J = N and then Result_May_Be_Null then
2504 Last_Opnd_High_Bound :=
2507 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
2508 Right_Opnd => Make_Integer_Literal (Loc, 1));
2511 -- Skip null string literal
2513 if J < N and then Len = 0 then
2518 Operands (NN) := Opnd;
2519 Is_Fixed_Length (NN) := True;
2521 -- Set length and bounds
2523 Fixed_Length (NN) := Len;
2525 Opnd_Low_Bound (NN) :=
2526 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
2533 -- Check constrained case with known bounds
2535 if Is_Constrained (Opnd_Typ) then
2537 Index : constant Node_Id := First_Index (Opnd_Typ);
2538 Indx_Typ : constant Entity_Id := Etype (Index);
2539 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
2540 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
2543 -- Fixed length constrained array type with known at compile
2544 -- time bounds is last case of fixed length operand.
2546 if Compile_Time_Known_Value (Lo)
2548 Compile_Time_Known_Value (Hi)
2551 Loval : constant Uint := Expr_Value (Lo);
2552 Hival : constant Uint := Expr_Value (Hi);
2553 Len : constant Uint :=
2554 UI_Max (Hival - Loval + 1, Uint_0);
2558 Result_May_Be_Null := False;
2561 -- Capture last operand bound if result could be null
2563 if J = N and then Result_May_Be_Null then
2564 Last_Opnd_High_Bound :=
2566 Make_Integer_Literal (Loc,
2567 Intval => Expr_Value (Hi)));
2570 -- Exclude null length case unless last operand
2572 if J < N and then Len = 0 then
2577 Operands (NN) := Opnd;
2578 Is_Fixed_Length (NN) := True;
2579 Fixed_Length (NN) := Len;
2581 Opnd_Low_Bound (NN) := To_Ityp (
2582 Make_Integer_Literal (Loc,
2583 Intval => Expr_Value (Lo)));
2591 -- All cases where the length is not known at compile time, or the
2592 -- special case of an operand which is known to be null but has a
2593 -- lower bound other than 1 or is other than a string type.
2598 -- Capture operand bounds
2600 Opnd_Low_Bound (NN) :=
2601 Make_Attribute_Reference (Loc,
2603 Duplicate_Subexpr (Opnd, Name_Req => True),
2604 Attribute_Name => Name_First);
2606 if J = N and Result_May_Be_Null then
2607 Last_Opnd_High_Bound :=
2609 Make_Attribute_Reference (Loc,
2611 Duplicate_Subexpr (Opnd, Name_Req => True),
2612 Attribute_Name => Name_Last));
2615 -- Capture length of operand in entity
2617 Operands (NN) := Opnd;
2618 Is_Fixed_Length (NN) := False;
2621 Make_Defining_Identifier (Loc,
2622 Chars => New_Internal_Name ('L'));
2625 Make_Object_Declaration (Loc,
2626 Defining_Identifier => Var_Length (NN),
2627 Constant_Present => True,
2629 Object_Definition =>
2630 New_Occurrence_Of (Artyp, Loc),
2633 Make_Attribute_Reference (Loc,
2635 Duplicate_Subexpr (Opnd, Name_Req => True),
2636 Attribute_Name => Name_Length)));
2640 -- Set next entry in aggregate length array
2642 -- For first entry, make either integer literal for fixed length
2643 -- or a reference to the saved length for variable length.
2646 if Is_Fixed_Length (1) then
2648 Make_Integer_Literal (Loc,
2649 Intval => Fixed_Length (1));
2652 New_Reference_To (Var_Length (1), Loc);
2655 -- If entry is fixed length and only fixed lengths so far, make
2656 -- appropriate new integer literal adding new length.
2658 elsif Is_Fixed_Length (NN)
2659 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
2662 Make_Integer_Literal (Loc,
2663 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
2665 -- All other cases, construct an addition node for the length and
2666 -- create an entity initialized to this length.
2670 Make_Defining_Identifier (Loc,
2671 Chars => New_Internal_Name ('L'));
2673 if Is_Fixed_Length (NN) then
2674 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
2676 Clen := New_Reference_To (Var_Length (NN), Loc);
2680 Make_Object_Declaration (Loc,
2681 Defining_Identifier => Ent,
2682 Constant_Present => True,
2684 Object_Definition =>
2685 New_Occurrence_Of (Artyp, Loc),
2689 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
2690 Right_Opnd => Clen)));
2692 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
2699 -- If we have only skipped null operands, return the last operand
2706 -- If we have only one non-null operand, return it and we are done.
2707 -- There is one case in which this cannot be done, and that is when
2708 -- the sole operand is of the element type, in which case it must be
2709 -- converted to an array, and the easiest way of doing that is to go
2710 -- through the normal general circuit.
2713 and then Base_Type (Etype (Operands (1))) /= Ctyp
2715 Result := Operands (1);
2719 -- Cases where we have a real concatenation
2721 -- Next step is to find the low bound for the result array that we
2722 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
2724 -- If the ultimate ancestor of the index subtype is a constrained array
2725 -- definition, then the lower bound is that of the index subtype as
2726 -- specified by (RM 4.5.3(6)).
2728 -- The right test here is to go to the root type, and then the ultimate
2729 -- ancestor is the first subtype of this root type.
2731 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
2733 Make_Attribute_Reference (Loc,
2735 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
2736 Attribute_Name => Name_First);
2738 -- If the first operand in the list has known length we know that
2739 -- the lower bound of the result is the lower bound of this operand.
2741 elsif Is_Fixed_Length (1) then
2742 Low_Bound := Opnd_Low_Bound (1);
2744 -- OK, we don't know the lower bound, we have to build a horrible
2745 -- expression actions node of the form
2747 -- if Cond1'Length /= 0 then
2750 -- if Opnd2'Length /= 0 then
2755 -- The nesting ends either when we hit an operand whose length is known
2756 -- at compile time, or on reaching the last operand, whose low bound we
2757 -- take unconditionally whether or not it is null. It's easiest to do
2758 -- this with a recursive procedure:
2762 function Get_Known_Bound (J : Nat) return Node_Id;
2763 -- Returns the lower bound determined by operands J .. NN
2765 ---------------------
2766 -- Get_Known_Bound --
2767 ---------------------
2769 function Get_Known_Bound (J : Nat) return Node_Id is
2771 if Is_Fixed_Length (J) or else J = NN then
2772 return New_Copy (Opnd_Low_Bound (J));
2776 Make_Conditional_Expression (Loc,
2777 Expressions => New_List (
2780 Left_Opnd => New_Reference_To (Var_Length (J), Loc),
2781 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2783 New_Copy (Opnd_Low_Bound (J)),
2784 Get_Known_Bound (J + 1)));
2786 end Get_Known_Bound;
2790 Make_Defining_Identifier (Loc, Chars => New_Internal_Name ('L'));
2793 Make_Object_Declaration (Loc,
2794 Defining_Identifier => Ent,
2795 Constant_Present => True,
2796 Object_Definition => New_Occurrence_Of (Ityp, Loc),
2797 Expression => Get_Known_Bound (1)));
2799 Low_Bound := New_Reference_To (Ent, Loc);
2803 -- Now we can safely compute the upper bound, normally
2804 -- Low_Bound + Length - 1.
2809 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2811 Make_Op_Subtract (Loc,
2812 Left_Opnd => New_Copy (Aggr_Length (NN)),
2813 Right_Opnd => Make_Artyp_Literal (1))));
2815 -- Note that calculation of the high bound may cause overflow in some
2816 -- very weird cases, so in the general case we need an overflow check on
2817 -- the high bound. We can avoid this for the common case of string types
2818 -- and other types whose index is Positive, since we chose a wider range
2819 -- for the arithmetic type.
2821 if Istyp /= Standard_Positive then
2822 Activate_Overflow_Check (High_Bound);
2825 -- Handle the exceptional case where the result is null, in which case
2826 -- case the bounds come from the last operand (so that we get the proper
2827 -- bounds if the last operand is super-flat).
2829 if Result_May_Be_Null then
2831 Make_Conditional_Expression (Loc,
2832 Expressions => New_List (
2834 Left_Opnd => New_Copy (Aggr_Length (NN)),
2835 Right_Opnd => Make_Artyp_Literal (0)),
2836 Last_Opnd_High_Bound,
2840 -- Here is where we insert the saved up actions
2842 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
2844 -- Now we construct an array object with appropriate bounds
2847 Make_Defining_Identifier (Loc,
2848 Chars => New_Internal_Name ('S'));
2850 -- If the bound is statically known to be out of range, we do not want
2851 -- to abort, we want a warning and a runtime constraint error. Note that
2852 -- we have arranged that the result will not be treated as a static
2853 -- constant, so we won't get an illegality during this insertion.
2855 Insert_Action (Cnode,
2856 Make_Object_Declaration (Loc,
2857 Defining_Identifier => Ent,
2858 Object_Definition =>
2859 Make_Subtype_Indication (Loc,
2860 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
2862 Make_Index_Or_Discriminant_Constraint (Loc,
2863 Constraints => New_List (
2865 Low_Bound => Low_Bound,
2866 High_Bound => High_Bound))))),
2867 Suppress => All_Checks);
2869 -- If the result of the concatenation appears as the initializing
2870 -- expression of an object declaration, we can just rename the
2871 -- result, rather than copying it.
2873 Set_OK_To_Rename (Ent);
2875 -- Catch the static out of range case now
2877 if Raises_Constraint_Error (High_Bound) then
2878 raise Concatenation_Error;
2881 -- Now we will generate the assignments to do the actual concatenation
2883 -- There is one case in which we will not do this, namely when all the
2884 -- following conditions are met:
2886 -- The result type is Standard.String
2888 -- There are nine or fewer retained (non-null) operands
2890 -- The optimization level is -O0
2892 -- The corresponding System.Concat_n.Str_Concat_n routine is
2893 -- available in the run time.
2895 -- The debug flag gnatd.c is not set
2897 -- If all these conditions are met then we generate a call to the
2898 -- relevant concatenation routine. The purpose of this is to avoid
2899 -- undesirable code bloat at -O0.
2901 if Atyp = Standard_String
2902 and then NN in 2 .. 9
2903 and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
2904 and then not Debug_Flag_Dot_C
2907 RR : constant array (Nat range 2 .. 9) of RE_Id :=
2918 if RTE_Available (RR (NN)) then
2920 Opnds : constant List_Id :=
2921 New_List (New_Occurrence_Of (Ent, Loc));
2924 for J in 1 .. NN loop
2925 if Is_List_Member (Operands (J)) then
2926 Remove (Operands (J));
2929 if Base_Type (Etype (Operands (J))) = Ctyp then
2931 Make_Aggregate (Loc,
2932 Component_Associations => New_List (
2933 Make_Component_Association (Loc,
2934 Choices => New_List (
2935 Make_Integer_Literal (Loc, 1)),
2936 Expression => Operands (J)))));
2939 Append_To (Opnds, Operands (J));
2943 Insert_Action (Cnode,
2944 Make_Procedure_Call_Statement (Loc,
2945 Name => New_Reference_To (RTE (RR (NN)), Loc),
2946 Parameter_Associations => Opnds));
2948 Result := New_Reference_To (Ent, Loc);
2955 -- Not special case so generate the assignments
2957 Known_Non_Null_Operand_Seen := False;
2959 for J in 1 .. NN loop
2961 Lo : constant Node_Id :=
2963 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2964 Right_Opnd => Aggr_Length (J - 1));
2966 Hi : constant Node_Id :=
2968 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2970 Make_Op_Subtract (Loc,
2971 Left_Opnd => Aggr_Length (J),
2972 Right_Opnd => Make_Artyp_Literal (1)));
2975 -- Singleton case, simple assignment
2977 if Base_Type (Etype (Operands (J))) = Ctyp then
2978 Known_Non_Null_Operand_Seen := True;
2979 Insert_Action (Cnode,
2980 Make_Assignment_Statement (Loc,
2982 Make_Indexed_Component (Loc,
2983 Prefix => New_Occurrence_Of (Ent, Loc),
2984 Expressions => New_List (To_Ityp (Lo))),
2985 Expression => Operands (J)),
2986 Suppress => All_Checks);
2988 -- Array case, slice assignment, skipped when argument is fixed
2989 -- length and known to be null.
2991 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
2994 Make_Assignment_Statement (Loc,
2998 New_Occurrence_Of (Ent, Loc),
3001 Low_Bound => To_Ityp (Lo),
3002 High_Bound => To_Ityp (Hi))),
3003 Expression => Operands (J));
3005 if Is_Fixed_Length (J) then
3006 Known_Non_Null_Operand_Seen := True;
3008 elsif not Known_Non_Null_Operand_Seen then
3010 -- Here if operand length is not statically known and no
3011 -- operand known to be non-null has been processed yet.
3012 -- If operand length is 0, we do not need to perform the
3013 -- assignment, and we must avoid the evaluation of the
3014 -- high bound of the slice, since it may underflow if the
3015 -- low bound is Ityp'First.
3018 Make_Implicit_If_Statement (Cnode,
3022 New_Occurrence_Of (Var_Length (J), Loc),
3023 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3028 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3034 -- Finally we build the result, which is a reference to the array object
3036 Result := New_Reference_To (Ent, Loc);
3039 Rewrite (Cnode, Result);
3040 Analyze_And_Resolve (Cnode, Atyp);
3043 when Concatenation_Error =>
3045 -- Kill warning generated for the declaration of the static out of
3046 -- range high bound, and instead generate a Constraint_Error with
3047 -- an appropriate specific message.
3049 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3050 Apply_Compile_Time_Constraint_Error
3052 Msg => "concatenation result upper bound out of range?",
3053 Reason => CE_Range_Check_Failed);
3054 -- Set_Etype (Cnode, Atyp);
3055 end Expand_Concatenate;
3057 ------------------------
3058 -- Expand_N_Allocator --
3059 ------------------------
3061 procedure Expand_N_Allocator (N : Node_Id) is
3062 PtrT : constant Entity_Id := Etype (N);
3063 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
3064 Etyp : constant Entity_Id := Etype (Expression (N));
3065 Loc : constant Source_Ptr := Sloc (N);
3070 procedure Complete_Coextension_Finalization;
3071 -- Generate finalization calls for all nested coextensions of N. This
3072 -- routine may allocate list controllers if necessary.
3074 procedure Rewrite_Coextension (N : Node_Id);
3075 -- Static coextensions have the same lifetime as the entity they
3076 -- constrain. Such occurrences can be rewritten as aliased objects
3077 -- and their unrestricted access used instead of the coextension.
3079 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
3080 -- Given a constrained array type E, returns a node representing the
3081 -- code to compute the size in storage elements for the given type.
3082 -- This is done without using the attribute (which malfunctions for
3085 ---------------------------------------
3086 -- Complete_Coextension_Finalization --
3087 ---------------------------------------
3089 procedure Complete_Coextension_Finalization is
3091 Coext_Elmt : Elmt_Id;
3095 function Inside_A_Return_Statement (N : Node_Id) return Boolean;
3096 -- Determine whether node N is part of a return statement
3098 function Needs_Initialization_Call (N : Node_Id) return Boolean;
3099 -- Determine whether node N is a subtype indicator allocator which
3100 -- acts a coextension. Such coextensions need initialization.
3102 -------------------------------
3103 -- Inside_A_Return_Statement --
3104 -------------------------------
3106 function Inside_A_Return_Statement (N : Node_Id) return Boolean is
3111 while Present (P) loop
3113 (P, N_Extended_Return_Statement, N_Simple_Return_Statement)
3117 -- Stop the traversal when we reach a subprogram body
3119 elsif Nkind (P) = N_Subprogram_Body then
3127 end Inside_A_Return_Statement;
3129 -------------------------------
3130 -- Needs_Initialization_Call --
3131 -------------------------------
3133 function Needs_Initialization_Call (N : Node_Id) return Boolean is
3137 if Nkind (N) = N_Explicit_Dereference
3138 and then Nkind (Prefix (N)) = N_Identifier
3139 and then Nkind (Parent (Entity (Prefix (N)))) =
3140 N_Object_Declaration
3142 Obj_Decl := Parent (Entity (Prefix (N)));
3145 Present (Expression (Obj_Decl))
3146 and then Nkind (Expression (Obj_Decl)) = N_Allocator
3147 and then Nkind (Expression (Expression (Obj_Decl))) /=
3148 N_Qualified_Expression;
3152 end Needs_Initialization_Call;
3154 -- Start of processing for Complete_Coextension_Finalization
3157 -- When a coextension root is inside a return statement, we need to
3158 -- use the finalization chain of the function's scope. This does not
3159 -- apply for controlled named access types because in those cases we
3160 -- can use the finalization chain of the type itself.
3162 if Inside_A_Return_Statement (N)
3164 (Ekind (PtrT) = E_Anonymous_Access_Type
3166 (Ekind (PtrT) = E_Access_Type
3167 and then No (Associated_Final_Chain (PtrT))))
3171 Outer_S : Entity_Id;
3172 S : Entity_Id := Current_Scope;
3175 while Present (S) and then S /= Standard_Standard loop
3176 if Ekind (S) = E_Function then
3177 Outer_S := Scope (S);
3179 -- Retrieve the declaration of the body
3184 (Corresponding_Body (Parent (Parent (S)))));
3191 -- Push the scope of the function body since we are inserting
3192 -- the list before the body, but we are currently in the body
3193 -- itself. Override the finalization list of PtrT since the
3194 -- finalization context is now different.
3196 Push_Scope (Outer_S);
3197 Build_Final_List (Decl, PtrT);
3201 -- The root allocator may not be controlled, but it still needs a
3202 -- finalization list for all nested coextensions.
3204 elsif No (Associated_Final_Chain (PtrT)) then
3205 Build_Final_List (N, PtrT);
3209 Make_Selected_Component (Loc,
3211 New_Reference_To (Associated_Final_Chain (PtrT), Loc),
3213 Make_Identifier (Loc, Name_F));
3215 Coext_Elmt := First_Elmt (Coextensions (N));
3216 while Present (Coext_Elmt) loop
3217 Coext := Node (Coext_Elmt);
3222 if Nkind (Coext) = N_Identifier then
3224 Make_Unchecked_Type_Conversion (Loc,
3225 Subtype_Mark => New_Reference_To (Etype (Coext), Loc),
3227 Make_Explicit_Dereference (Loc,
3228 Prefix => New_Copy_Tree (Coext)));
3230 Ref := New_Copy_Tree (Coext);
3233 -- No initialization call if not allowed
3235 Check_Restriction (No_Default_Initialization, N);
3237 if not Restriction_Active (No_Default_Initialization) then
3241 -- attach_to_final_list (Ref, Flist, 2)
3243 if Needs_Initialization_Call (Coext) then
3247 Typ => Etype (Coext),
3249 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3252 -- attach_to_final_list (Ref, Flist, 2)
3258 Flist_Ref => New_Copy_Tree (Flist),
3259 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3263 Next_Elmt (Coext_Elmt);
3265 end Complete_Coextension_Finalization;
3267 -------------------------
3268 -- Rewrite_Coextension --
3269 -------------------------
3271 procedure Rewrite_Coextension (N : Node_Id) is
3272 Temp : constant Node_Id :=
3273 Make_Defining_Identifier (Loc,
3274 New_Internal_Name ('C'));
3277 -- Cnn : aliased Etyp;
3279 Decl : constant Node_Id :=
3280 Make_Object_Declaration (Loc,
3281 Defining_Identifier => Temp,
3282 Aliased_Present => True,
3283 Object_Definition =>
3284 New_Occurrence_Of (Etyp, Loc));
3288 if Nkind (Expression (N)) = N_Qualified_Expression then
3289 Set_Expression (Decl, Expression (Expression (N)));
3292 -- Find the proper insertion node for the declaration
3295 while Present (Nod) loop
3296 exit when Nkind (Nod) in N_Statement_Other_Than_Procedure_Call
3297 or else Nkind (Nod) = N_Procedure_Call_Statement
3298 or else Nkind (Nod) in N_Declaration;
3299 Nod := Parent (Nod);
3302 Insert_Before (Nod, Decl);
3306 Make_Attribute_Reference (Loc,
3307 Prefix => New_Occurrence_Of (Temp, Loc),
3308 Attribute_Name => Name_Unrestricted_Access));
3310 Analyze_And_Resolve (N, PtrT);
3311 end Rewrite_Coextension;
3313 ------------------------------
3314 -- Size_In_Storage_Elements --
3315 ------------------------------
3317 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
3319 -- Logically this just returns E'Max_Size_In_Storage_Elements.
3320 -- However, the reason for the existence of this function is
3321 -- to construct a test for sizes too large, which means near the
3322 -- 32-bit limit on a 32-bit machine, and precisely the trouble
3323 -- is that we get overflows when sizes are greater than 2**31.
3325 -- So what we end up doing for array types is to use the expression:
3327 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
3329 -- which avoids this problem. All this is a big bogus, but it does
3330 -- mean we catch common cases of trying to allocate arrays that
3331 -- are too large, and which in the absence of a check results in
3332 -- undetected chaos ???
3339 for J in 1 .. Number_Dimensions (E) loop
3341 Make_Attribute_Reference (Loc,
3342 Prefix => New_Occurrence_Of (E, Loc),
3343 Attribute_Name => Name_Length,
3344 Expressions => New_List (
3345 Make_Integer_Literal (Loc, J)));
3352 Make_Op_Multiply (Loc,
3359 Make_Op_Multiply (Loc,
3362 Make_Attribute_Reference (Loc,
3363 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
3364 Attribute_Name => Name_Max_Size_In_Storage_Elements));
3366 end Size_In_Storage_Elements;
3368 -- Start of processing for Expand_N_Allocator
3371 -- RM E.2.3(22). We enforce that the expected type of an allocator
3372 -- shall not be a remote access-to-class-wide-limited-private type
3374 -- Why is this being done at expansion time, seems clearly wrong ???
3376 Validate_Remote_Access_To_Class_Wide_Type (N);
3378 -- Set the Storage Pool
3380 Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT)));
3382 if Present (Storage_Pool (N)) then
3383 if Is_RTE (Storage_Pool (N), RE_SS_Pool) then
3384 if VM_Target = No_VM then
3385 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3388 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
3389 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
3392 Set_Procedure_To_Call (N,
3393 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
3397 -- Under certain circumstances we can replace an allocator by an access
3398 -- to statically allocated storage. The conditions, as noted in AARM
3399 -- 3.10 (10c) are as follows:
3401 -- Size and initial value is known at compile time
3402 -- Access type is access-to-constant
3404 -- The allocator is not part of a constraint on a record component,
3405 -- because in that case the inserted actions are delayed until the
3406 -- record declaration is fully analyzed, which is too late for the
3407 -- analysis of the rewritten allocator.
3409 if Is_Access_Constant (PtrT)
3410 and then Nkind (Expression (N)) = N_Qualified_Expression
3411 and then Compile_Time_Known_Value (Expression (Expression (N)))
3412 and then Size_Known_At_Compile_Time (Etype (Expression
3414 and then not Is_Record_Type (Current_Scope)
3416 -- Here we can do the optimization. For the allocator
3420 -- We insert an object declaration
3422 -- Tnn : aliased x := y;
3424 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
3425 -- marked as requiring static allocation.
3428 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
3430 Desig := Subtype_Mark (Expression (N));
3432 -- If context is constrained, use constrained subtype directly,
3433 -- so that the constant is not labelled as having a nominally
3434 -- unconstrained subtype.
3436 if Entity (Desig) = Base_Type (Dtyp) then
3437 Desig := New_Occurrence_Of (Dtyp, Loc);
3441 Make_Object_Declaration (Loc,
3442 Defining_Identifier => Temp,
3443 Aliased_Present => True,
3444 Constant_Present => Is_Access_Constant (PtrT),
3445 Object_Definition => Desig,
3446 Expression => Expression (Expression (N))));
3449 Make_Attribute_Reference (Loc,
3450 Prefix => New_Occurrence_Of (Temp, Loc),
3451 Attribute_Name => Name_Unrestricted_Access));
3453 Analyze_And_Resolve (N, PtrT);
3455 -- We set the variable as statically allocated, since we don't want
3456 -- it going on the stack of the current procedure!
3458 Set_Is_Statically_Allocated (Temp);
3462 -- Same if the allocator is an access discriminant for a local object:
3463 -- instead of an allocator we create a local value and constrain the
3464 -- the enclosing object with the corresponding access attribute.
3466 if Is_Static_Coextension (N) then
3467 Rewrite_Coextension (N);
3471 -- The current allocator creates an object which may contain nested
3472 -- coextensions. Use the current allocator's finalization list to
3473 -- generate finalization call for all nested coextensions.
3475 if Is_Coextension_Root (N) then
3476 Complete_Coextension_Finalization;
3479 -- Check for size too large, we do this because the back end misses
3480 -- proper checks here and can generate rubbish allocation calls when
3481 -- we are near the limit. We only do this for the 32-bit address case
3482 -- since that is from a practical point of view where we see a problem.
3484 if System_Address_Size = 32
3485 and then not Storage_Checks_Suppressed (PtrT)
3486 and then not Storage_Checks_Suppressed (Dtyp)
3487 and then not Storage_Checks_Suppressed (Etyp)
3489 -- The check we want to generate should look like
3491 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
3492 -- raise Storage_Error;
3495 -- where 3.5 gigabytes is a constant large enough to accomodate any
3496 -- reasonable request for. But we can't do it this way because at
3497 -- least at the moment we don't compute this attribute right, and
3498 -- can silently give wrong results when the result gets large. Since
3499 -- this is all about large results, that's bad, so instead we only
3500 -- apply the check for constrained arrays, and manually compute the
3501 -- value of the attribute ???
3503 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
3505 Make_Raise_Storage_Error (Loc,
3508 Left_Opnd => Size_In_Storage_Elements (Etyp),
3510 Make_Integer_Literal (Loc,
3511 Intval => Uint_7 * (Uint_2 ** 29))),
3512 Reason => SE_Object_Too_Large));
3516 -- Handle case of qualified expression (other than optimization above)
3517 -- First apply constraint checks, because the bounds or discriminants
3518 -- in the aggregate might not match the subtype mark in the allocator.
3520 if Nkind (Expression (N)) = N_Qualified_Expression then
3521 Apply_Constraint_Check
3522 (Expression (Expression (N)), Etype (Expression (N)));
3524 Expand_Allocator_Expression (N);
3528 -- If the allocator is for a type which requires initialization, and
3529 -- there is no initial value (i.e. operand is a subtype indication
3530 -- rather than a qualified expression), then we must generate a call to
3531 -- the initialization routine using an expressions action node:
3533 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
3535 -- Here ptr_T is the pointer type for the allocator, and T is the
3536 -- subtype of the allocator. A special case arises if the designated
3537 -- type of the access type is a task or contains tasks. In this case
3538 -- the call to Init (Temp.all ...) is replaced by code that ensures
3539 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
3540 -- for details). In addition, if the type T is a task T, then the
3541 -- first argument to Init must be converted to the task record type.
3544 T : constant Entity_Id := Entity (Expression (N));
3552 Temp_Decl : Node_Id;
3553 Temp_Type : Entity_Id;
3554 Attach_Level : Uint;
3557 if No_Initialization (N) then
3560 -- Case of no initialization procedure present
3562 elsif not Has_Non_Null_Base_Init_Proc (T) then
3564 -- Case of simple initialization required
3566 if Needs_Simple_Initialization (T) then
3567 Check_Restriction (No_Default_Initialization, N);
3568 Rewrite (Expression (N),
3569 Make_Qualified_Expression (Loc,
3570 Subtype_Mark => New_Occurrence_Of (T, Loc),
3571 Expression => Get_Simple_Init_Val (T, N)));
3573 Analyze_And_Resolve (Expression (Expression (N)), T);
3574 Analyze_And_Resolve (Expression (N), T);
3575 Set_Paren_Count (Expression (Expression (N)), 1);
3576 Expand_N_Allocator (N);
3578 -- No initialization required
3584 -- Case of initialization procedure present, must be called
3587 Check_Restriction (No_Default_Initialization, N);
3589 if not Restriction_Active (No_Default_Initialization) then
3590 Init := Base_Init_Proc (T);
3592 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
3594 -- Construct argument list for the initialization routine call
3597 Make_Explicit_Dereference (Loc,
3598 Prefix => New_Reference_To (Temp, Loc));
3599 Set_Assignment_OK (Arg1);
3602 -- The initialization procedure expects a specific type. if the
3603 -- context is access to class wide, indicate that the object
3604 -- being allocated has the right specific type.
3606 if Is_Class_Wide_Type (Dtyp) then
3607 Arg1 := Unchecked_Convert_To (T, Arg1);
3610 -- If designated type is a concurrent type or if it is private
3611 -- type whose definition is a concurrent type, the first
3612 -- argument in the Init routine has to be unchecked conversion
3613 -- to the corresponding record type. If the designated type is
3614 -- a derived type, we also convert the argument to its root
3617 if Is_Concurrent_Type (T) then
3619 Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1);
3621 elsif Is_Private_Type (T)
3622 and then Present (Full_View (T))
3623 and then Is_Concurrent_Type (Full_View (T))
3626 Unchecked_Convert_To
3627 (Corresponding_Record_Type (Full_View (T)), Arg1);
3629 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
3631 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
3633 Arg1 := OK_Convert_To (Etype (Ftyp), Arg1);
3634 Set_Etype (Arg1, Ftyp);
3638 Args := New_List (Arg1);
3640 -- For the task case, pass the Master_Id of the access type as
3641 -- the value of the _Master parameter, and _Chain as the value
3642 -- of the _Chain parameter (_Chain will be defined as part of
3643 -- the generated code for the allocator).
3645 -- In Ada 2005, the context may be a function that returns an
3646 -- anonymous access type. In that case the Master_Id has been
3647 -- created when expanding the function declaration.
3649 if Has_Task (T) then
3650 if No (Master_Id (Base_Type (PtrT))) then
3652 -- If we have a non-library level task with restriction
3653 -- No_Task_Hierarchy set, then no point in expanding.
3655 if not Is_Library_Level_Entity (T)
3656 and then Restriction_Active (No_Task_Hierarchy)
3661 -- The designated type was an incomplete type, and the
3662 -- access type did not get expanded. Salvage it now.
3664 pragma Assert (Present (Parent (Base_Type (PtrT))));
3665 Expand_N_Full_Type_Declaration
3666 (Parent (Base_Type (PtrT)));
3669 -- If the context of the allocator is a declaration or an
3670 -- assignment, we can generate a meaningful image for it,
3671 -- even though subsequent assignments might remove the
3672 -- connection between task and entity. We build this image
3673 -- when the left-hand side is a simple variable, a simple
3674 -- indexed assignment or a simple selected component.
3676 if Nkind (Parent (N)) = N_Assignment_Statement then
3678 Nam : constant Node_Id := Name (Parent (N));
3681 if Is_Entity_Name (Nam) then
3683 Build_Task_Image_Decls
3686 (Entity (Nam), Sloc (Nam)), T);
3689 (Nam, N_Indexed_Component, N_Selected_Component)
3690 and then Is_Entity_Name (Prefix (Nam))
3693 Build_Task_Image_Decls
3694 (Loc, Nam, Etype (Prefix (Nam)));
3696 Decls := Build_Task_Image_Decls (Loc, T, T);
3700 elsif Nkind (Parent (N)) = N_Object_Declaration then
3702 Build_Task_Image_Decls
3703 (Loc, Defining_Identifier (Parent (N)), T);
3706 Decls := Build_Task_Image_Decls (Loc, T, T);
3711 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
3712 Append_To (Args, Make_Identifier (Loc, Name_uChain));
3714 Decl := Last (Decls);
3716 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
3718 -- Has_Task is false, Decls not used
3724 -- Add discriminants if discriminated type
3727 Dis : Boolean := False;
3731 if Has_Discriminants (T) then
3735 elsif Is_Private_Type (T)
3736 and then Present (Full_View (T))
3737 and then Has_Discriminants (Full_View (T))
3740 Typ := Full_View (T);
3745 -- If the allocated object will be constrained by the
3746 -- default values for discriminants, then build a subtype
3747 -- with those defaults, and change the allocated subtype
3748 -- to that. Note that this happens in fewer cases in Ada
3751 if not Is_Constrained (Typ)
3752 and then Present (Discriminant_Default_Value
3753 (First_Discriminant (Typ)))
3754 and then (Ada_Version < Ada_05
3756 not Has_Constrained_Partial_View (Typ))
3758 Typ := Build_Default_Subtype (Typ, N);
3759 Set_Expression (N, New_Reference_To (Typ, Loc));
3762 Discr := First_Elmt (Discriminant_Constraint (Typ));
3763 while Present (Discr) loop
3764 Nod := Node (Discr);
3765 Append (New_Copy_Tree (Node (Discr)), Args);
3767 -- AI-416: when the discriminant constraint is an
3768 -- anonymous access type make sure an accessibility
3769 -- check is inserted if necessary (3.10.2(22.q/2))
3771 if Ada_Version >= Ada_05
3773 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
3775 Apply_Accessibility_Check
3776 (Nod, Typ, Insert_Node => Nod);
3784 -- We set the allocator as analyzed so that when we analyze the
3785 -- expression actions node, we do not get an unwanted recursive
3786 -- expansion of the allocator expression.
3788 Set_Analyzed (N, True);
3789 Nod := Relocate_Node (N);
3791 -- Here is the transformation:
3793 -- output: Temp : constant ptr_T := new T;
3794 -- Init (Temp.all, ...);
3795 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
3796 -- <CTRL> Initialize (Finalizable (Temp.all));
3798 -- Here ptr_T is the pointer type for the allocator, and is the
3799 -- subtype of the allocator.
3802 Make_Object_Declaration (Loc,
3803 Defining_Identifier => Temp,
3804 Constant_Present => True,
3805 Object_Definition => New_Reference_To (Temp_Type, Loc),
3808 Set_Assignment_OK (Temp_Decl);
3809 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
3811 -- If the designated type is a task type or contains tasks,
3812 -- create block to activate created tasks, and insert
3813 -- declaration for Task_Image variable ahead of call.
3815 if Has_Task (T) then
3817 L : constant List_Id := New_List;
3820 Build_Task_Allocate_Block (L, Nod, Args);
3822 Insert_List_Before (First (Declarations (Blk)), Decls);
3823 Insert_Actions (N, L);
3828 Make_Procedure_Call_Statement (Loc,
3829 Name => New_Reference_To (Init, Loc),
3830 Parameter_Associations => Args));
3833 if Needs_Finalization (T) then
3835 -- Postpone the generation of a finalization call for the
3836 -- current allocator if it acts as a coextension.
3838 if Is_Dynamic_Coextension (N) then
3839 if No (Coextensions (N)) then
3840 Set_Coextensions (N, New_Elmt_List);
3843 Append_Elmt (New_Copy_Tree (Arg1), Coextensions (N));
3847 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
3849 -- Anonymous access types created for access parameters
3850 -- are attached to an explicitly constructed controller,
3851 -- which ensures that they can be finalized properly,
3852 -- even if their deallocation might not happen. The list
3853 -- associated with the controller is doubly-linked. For
3854 -- other anonymous access types, the object may end up
3855 -- on the global final list which is singly-linked.
3856 -- Work needed for access discriminants in Ada 2005 ???
3858 if Ekind (PtrT) = E_Anonymous_Access_Type then
3859 Attach_Level := Uint_1;
3861 Attach_Level := Uint_2;
3866 Ref => New_Copy_Tree (Arg1),
3869 With_Attach => Make_Integer_Literal (Loc,
3870 Intval => Attach_Level)));
3874 Rewrite (N, New_Reference_To (Temp, Loc));
3875 Analyze_And_Resolve (N, PtrT);
3880 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
3881 -- object that has been rewritten as a reference, we displace "this"
3882 -- to reference properly its secondary dispatch table.
3884 if Nkind (N) = N_Identifier
3885 and then Is_Interface (Dtyp)
3887 Displace_Allocator_Pointer (N);
3891 when RE_Not_Available =>
3893 end Expand_N_Allocator;
3895 -----------------------
3896 -- Expand_N_And_Then --
3897 -----------------------
3899 -- Expand into conditional expression if Actions present, and also deal
3900 -- with optimizing case of arguments being True or False.
3902 procedure Expand_N_And_Then (N : Node_Id) is
3903 Loc : constant Source_Ptr := Sloc (N);
3904 Typ : constant Entity_Id := Etype (N);
3905 Left : constant Node_Id := Left_Opnd (N);
3906 Right : constant Node_Id := Right_Opnd (N);
3910 -- Deal with non-standard booleans
3912 if Is_Boolean_Type (Typ) then
3913 Adjust_Condition (Left);
3914 Adjust_Condition (Right);
3915 Set_Etype (N, Standard_Boolean);
3918 -- Check for cases where left argument is known to be True or False
3920 if Compile_Time_Known_Value (Left) then
3922 -- If left argument is True, change (True and then Right) to Right.
3923 -- Any actions associated with Right will be executed unconditionally
3924 -- and can thus be inserted into the tree unconditionally.
3926 if Expr_Value_E (Left) = Standard_True then
3927 if Present (Actions (N)) then
3928 Insert_Actions (N, Actions (N));
3933 -- If left argument is False, change (False and then Right) to False.
3934 -- In this case we can forget the actions associated with Right,
3935 -- since they will never be executed.
3937 else pragma Assert (Expr_Value_E (Left) = Standard_False);
3938 Kill_Dead_Code (Right);
3939 Kill_Dead_Code (Actions (N));
3940 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3943 Adjust_Result_Type (N, Typ);
3947 -- If Actions are present, we expand
3949 -- left and then right
3953 -- if left then right else false end
3955 -- with the actions becoming the Then_Actions of the conditional
3956 -- expression. This conditional expression is then further expanded
3957 -- (and will eventually disappear)
3959 if Present (Actions (N)) then
3960 Actlist := Actions (N);
3962 Make_Conditional_Expression (Loc,
3963 Expressions => New_List (
3966 New_Occurrence_Of (Standard_False, Loc))));
3968 Set_Then_Actions (N, Actlist);
3969 Analyze_And_Resolve (N, Standard_Boolean);
3970 Adjust_Result_Type (N, Typ);
3974 -- No actions present, check for cases of right argument True/False
3976 if Compile_Time_Known_Value (Right) then
3978 -- Change (Left and then True) to Left. Note that we know there are
3979 -- no actions associated with the True operand, since we just checked
3980 -- for this case above.
3982 if Expr_Value_E (Right) = Standard_True then
3985 -- Change (Left and then False) to False, making sure to preserve any
3986 -- side effects associated with the Left operand.
3988 else pragma Assert (Expr_Value_E (Right) = Standard_False);
3989 Remove_Side_Effects (Left);
3990 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3994 Adjust_Result_Type (N, Typ);
3995 end Expand_N_And_Then;
3997 -------------------------------------
3998 -- Expand_N_Conditional_Expression --
3999 -------------------------------------
4001 -- Expand into expression actions if then/else actions present
4003 procedure Expand_N_Conditional_Expression (N : Node_Id) is
4004 Loc : constant Source_Ptr := Sloc (N);
4005 Cond : constant Node_Id := First (Expressions (N));
4006 Thenx : constant Node_Id := Next (Cond);
4007 Elsex : constant Node_Id := Next (Thenx);
4008 Typ : constant Entity_Id := Etype (N);
4013 -- If either then or else actions are present, then given:
4015 -- if cond then then-expr else else-expr end
4017 -- we insert the following sequence of actions (using Insert_Actions):
4022 -- Cnn := then-expr;
4028 -- and replace the conditional expression by a reference to Cnn
4030 -- ??? Note: this expansion is wrong for limited types, since it does
4031 -- a copy of a limited value. The proper fix would be to do the
4032 -- following expansion:
4034 -- Cnn : access typ;
4037 -- Cnn := then-expr'Unrestricted_Access;
4040 -- Cnn := else-expr'Unrestricted_Access;
4043 -- and replace the conditional expresion by a reference to Cnn.all ???
4045 if Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
4046 Cnn := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
4049 Make_Implicit_If_Statement (N,
4050 Condition => Relocate_Node (Cond),
4052 Then_Statements => New_List (
4053 Make_Assignment_Statement (Sloc (Thenx),
4054 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
4055 Expression => Relocate_Node (Thenx))),
4057 Else_Statements => New_List (
4058 Make_Assignment_Statement (Sloc (Elsex),
4059 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
4060 Expression => Relocate_Node (Elsex))));
4062 -- Move the SLOC of the parent If statement to the newly created one
4063 -- and change it to the SLOC of the expression which, after
4064 -- expansion, will correspond to what is being evaluated.
4066 if Present (Parent (N))
4067 and then Nkind (Parent (N)) = N_If_Statement
4069 Set_Sloc (New_If, Sloc (Parent (N)));
4070 Set_Sloc (Parent (N), Loc);
4073 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
4074 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
4076 if Present (Then_Actions (N)) then
4078 (First (Then_Statements (New_If)), Then_Actions (N));
4081 if Present (Else_Actions (N)) then
4083 (First (Else_Statements (New_If)), Else_Actions (N));
4086 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
4089 Make_Object_Declaration (Loc,
4090 Defining_Identifier => Cnn,
4091 Object_Definition => New_Occurrence_Of (Typ, Loc)));
4093 Insert_Action (N, New_If);
4094 Analyze_And_Resolve (N, Typ);
4096 -- Link temporary to original expression, for Codepeer
4098 Set_Related_Expression (Cnn, Original_Node (N));
4100 end Expand_N_Conditional_Expression;
4102 -----------------------------------
4103 -- Expand_N_Explicit_Dereference --
4104 -----------------------------------
4106 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
4108 -- Insert explicit dereference call for the checked storage pool case
4110 Insert_Dereference_Action (Prefix (N));
4111 end Expand_N_Explicit_Dereference;
4117 procedure Expand_N_In (N : Node_Id) is
4118 Loc : constant Source_Ptr := Sloc (N);
4119 Rtyp : constant Entity_Id := Etype (N);
4120 Lop : constant Node_Id := Left_Opnd (N);
4121 Rop : constant Node_Id := Right_Opnd (N);
4122 Static : constant Boolean := Is_OK_Static_Expression (N);
4124 procedure Expand_Set_Membership;
4125 -- For each disjunct we create a simple equality or membership test.
4126 -- The whole membership is rewritten as a short-circuit disjunction.
4128 ---------------------------
4129 -- Expand_Set_Membership --
4130 ---------------------------
4132 procedure Expand_Set_Membership is
4136 function Make_Cond (Alt : Node_Id) return Node_Id;
4137 -- If the alternative is a subtype mark, create a simple membership
4138 -- test. Otherwise create an equality test for it.
4144 function Make_Cond (Alt : Node_Id) return Node_Id is
4146 L : constant Node_Id := New_Copy (Lop);
4147 R : constant Node_Id := Relocate_Node (Alt);
4150 if Is_Entity_Name (Alt)
4151 and then Is_Type (Entity (Alt))
4154 Make_In (Sloc (Alt),
4158 Cond := Make_Op_Eq (Sloc (Alt),
4166 -- Start of proessing for Expand_N_In
4169 Alt := Last (Alternatives (N));
4170 Res := Make_Cond (Alt);
4173 while Present (Alt) loop
4175 Make_Or_Else (Sloc (Alt),
4176 Left_Opnd => Make_Cond (Alt),
4182 Analyze_And_Resolve (N, Standard_Boolean);
4183 end Expand_Set_Membership;
4185 procedure Substitute_Valid_Check;
4186 -- Replaces node N by Lop'Valid. This is done when we have an explicit
4187 -- test for the left operand being in range of its subtype.
4189 ----------------------------
4190 -- Substitute_Valid_Check --
4191 ----------------------------
4193 procedure Substitute_Valid_Check is
4196 Make_Attribute_Reference (Loc,
4197 Prefix => Relocate_Node (Lop),
4198 Attribute_Name => Name_Valid));
4200 Analyze_And_Resolve (N, Rtyp);
4202 Error_Msg_N ("?explicit membership test may be optimized away", N);
4203 Error_Msg_N ("\?use ''Valid attribute instead", N);
4205 end Substitute_Valid_Check;
4207 -- Start of processing for Expand_N_In
4211 if Present (Alternatives (N)) then
4212 Remove_Side_Effects (Lop);
4213 Expand_Set_Membership;
4217 -- Check case of explicit test for an expression in range of its
4218 -- subtype. This is suspicious usage and we replace it with a 'Valid
4219 -- test and give a warning.
4221 if Is_Scalar_Type (Etype (Lop))
4222 and then Nkind (Rop) in N_Has_Entity
4223 and then Etype (Lop) = Entity (Rop)
4224 and then Comes_From_Source (N)
4225 and then VM_Target = No_VM
4227 Substitute_Valid_Check;
4231 -- Do validity check on operands
4233 if Validity_Checks_On and Validity_Check_Operands then
4234 Ensure_Valid (Left_Opnd (N));
4235 Validity_Check_Range (Right_Opnd (N));
4238 -- Case of explicit range
4240 if Nkind (Rop) = N_Range then
4242 Lo : constant Node_Id := Low_Bound (Rop);
4243 Hi : constant Node_Id := High_Bound (Rop);
4245 Ltyp : constant Entity_Id := Etype (Lop);
4247 Lo_Orig : constant Node_Id := Original_Node (Lo);
4248 Hi_Orig : constant Node_Id := Original_Node (Hi);
4250 Lcheck : Compare_Result;
4251 Ucheck : Compare_Result;
4253 Warn1 : constant Boolean :=
4254 Constant_Condition_Warnings
4255 and then Comes_From_Source (N)
4256 and then not In_Instance;
4257 -- This must be true for any of the optimization warnings, we
4258 -- clearly want to give them only for source with the flag on.
4259 -- We also skip these warnings in an instance since it may be
4260 -- the case that different instantiations have different ranges.
4262 Warn2 : constant Boolean :=
4264 and then Nkind (Original_Node (Rop)) = N_Range
4265 and then Is_Integer_Type (Etype (Lo));
4266 -- For the case where only one bound warning is elided, we also
4267 -- insist on an explicit range and an integer type. The reason is
4268 -- that the use of enumeration ranges including an end point is
4269 -- common, as is the use of a subtype name, one of whose bounds
4270 -- is the same as the type of the expression.
4273 -- If test is explicit x'first .. x'last, replace by valid check
4275 if Is_Scalar_Type (Ltyp)
4276 and then Nkind (Lo_Orig) = N_Attribute_Reference
4277 and then Attribute_Name (Lo_Orig) = Name_First
4278 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
4279 and then Entity (Prefix (Lo_Orig)) = Ltyp
4280 and then Nkind (Hi_Orig) = N_Attribute_Reference
4281 and then Attribute_Name (Hi_Orig) = Name_Last
4282 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
4283 and then Entity (Prefix (Hi_Orig)) = Ltyp
4284 and then Comes_From_Source (N)
4285 and then VM_Target = No_VM
4287 Substitute_Valid_Check;
4291 -- If bounds of type are known at compile time, and the end points
4292 -- are known at compile time and identical, this is another case
4293 -- for substituting a valid test. We only do this for discrete
4294 -- types, since it won't arise in practice for float types.
4296 if Comes_From_Source (N)
4297 and then Is_Discrete_Type (Ltyp)
4298 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
4299 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
4300 and then Compile_Time_Known_Value (Lo)
4301 and then Compile_Time_Known_Value (Hi)
4302 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
4303 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
4305 -- Kill warnings in instances, since they may be cases where we
4306 -- have a test in the generic that makes sense with some types
4307 -- and not with other types.
4309 and then not In_Instance
4311 Substitute_Valid_Check;
4315 -- If we have an explicit range, do a bit of optimization based
4316 -- on range analysis (we may be able to kill one or both checks).
4318 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
4319 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
4321 -- If either check is known to fail, replace result by False since
4322 -- the other check does not matter. Preserve the static flag for
4323 -- legality checks, because we are constant-folding beyond RM 4.9.
4325 if Lcheck = LT or else Ucheck = GT then
4327 Error_Msg_N ("?range test optimized away", N);
4328 Error_Msg_N ("\?value is known to be out of range", N);
4332 New_Reference_To (Standard_False, Loc));
4333 Analyze_And_Resolve (N, Rtyp);
4334 Set_Is_Static_Expression (N, Static);
4338 -- If both checks are known to succeed, replace result by True,
4339 -- since we know we are in range.
4341 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4343 Error_Msg_N ("?range test optimized away", N);
4344 Error_Msg_N ("\?value is known to be in range", N);
4348 New_Reference_To (Standard_True, Loc));
4349 Analyze_And_Resolve (N, Rtyp);
4350 Set_Is_Static_Expression (N, Static);
4354 -- If lower bound check succeeds and upper bound check is not
4355 -- known to succeed or fail, then replace the range check with
4356 -- a comparison against the upper bound.
4358 elsif Lcheck in Compare_GE then
4359 if Warn2 and then not In_Instance then
4360 Error_Msg_N ("?lower bound test optimized away", Lo);
4361 Error_Msg_N ("\?value is known to be in range", Lo);
4367 Right_Opnd => High_Bound (Rop)));
4368 Analyze_And_Resolve (N, Rtyp);
4372 -- If upper bound check succeeds and lower bound check is not
4373 -- known to succeed or fail, then replace the range check with
4374 -- a comparison against the lower bound.
4376 elsif Ucheck in Compare_LE then
4377 if Warn2 and then not In_Instance then
4378 Error_Msg_N ("?upper bound test optimized away", Hi);
4379 Error_Msg_N ("\?value is known to be in range", Hi);
4385 Right_Opnd => Low_Bound (Rop)));
4386 Analyze_And_Resolve (N, Rtyp);
4391 -- We couldn't optimize away the range check, but there is one
4392 -- more issue. If we are checking constant conditionals, then we
4393 -- see if we can determine the outcome assuming everything is
4394 -- valid, and if so give an appropriate warning.
4396 if Warn1 and then not Assume_No_Invalid_Values then
4397 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
4398 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
4400 -- Result is out of range for valid value
4402 if Lcheck = LT or else Ucheck = GT then
4404 ("?value can only be in range if it is invalid", N);
4406 -- Result is in range for valid value
4408 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4410 ("?value can only be out of range if it is invalid", N);
4412 -- Lower bound check succeeds if value is valid
4414 elsif Warn2 and then Lcheck in Compare_GE then
4416 ("?lower bound check only fails if it is invalid", Lo);
4418 -- Upper bound check succeeds if value is valid
4420 elsif Warn2 and then Ucheck in Compare_LE then
4422 ("?upper bound check only fails for invalid values", Hi);
4427 -- For all other cases of an explicit range, nothing to be done
4431 -- Here right operand is a subtype mark
4435 Typ : Entity_Id := Etype (Rop);
4436 Is_Acc : constant Boolean := Is_Access_Type (Typ);
4437 Obj : Node_Id := Lop;
4438 Cond : Node_Id := Empty;
4441 Remove_Side_Effects (Obj);
4443 -- For tagged type, do tagged membership operation
4445 if Is_Tagged_Type (Typ) then
4447 -- No expansion will be performed when VM_Target, as the VM
4448 -- back-ends will handle the membership tests directly (tags
4449 -- are not explicitly represented in Java objects, so the
4450 -- normal tagged membership expansion is not what we want).
4452 if Tagged_Type_Expansion then
4453 Rewrite (N, Tagged_Membership (N));
4454 Analyze_And_Resolve (N, Rtyp);
4459 -- If type is scalar type, rewrite as x in t'first .. t'last.
4460 -- This reason we do this is that the bounds may have the wrong
4461 -- type if they come from the original type definition. Also this
4462 -- way we get all the processing above for an explicit range.
4464 elsif Is_Scalar_Type (Typ) then
4468 Make_Attribute_Reference (Loc,
4469 Attribute_Name => Name_First,
4470 Prefix => New_Reference_To (Typ, Loc)),
4473 Make_Attribute_Reference (Loc,
4474 Attribute_Name => Name_Last,
4475 Prefix => New_Reference_To (Typ, Loc))));
4476 Analyze_And_Resolve (N, Rtyp);
4479 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4480 -- a membership test if the subtype mark denotes a constrained
4481 -- Unchecked_Union subtype and the expression lacks inferable
4484 elsif Is_Unchecked_Union (Base_Type (Typ))
4485 and then Is_Constrained (Typ)
4486 and then not Has_Inferable_Discriminants (Lop)
4489 Make_Raise_Program_Error (Loc,
4490 Reason => PE_Unchecked_Union_Restriction));
4492 -- Prevent Gigi from generating incorrect code by rewriting
4493 -- the test as a standard False.
4496 New_Occurrence_Of (Standard_False, Loc));
4501 -- Here we have a non-scalar type
4504 Typ := Designated_Type (Typ);
4507 if not Is_Constrained (Typ) then
4509 New_Reference_To (Standard_True, Loc));
4510 Analyze_And_Resolve (N, Rtyp);
4512 -- For the constrained array case, we have to check the subscripts
4513 -- for an exact match if the lengths are non-zero (the lengths
4514 -- must match in any case).
4516 elsif Is_Array_Type (Typ) then
4518 Check_Subscripts : declare
4519 function Construct_Attribute_Reference
4522 Dim : Nat) return Node_Id;
4523 -- Build attribute reference E'Nam(Dim)
4525 -----------------------------------
4526 -- Construct_Attribute_Reference --
4527 -----------------------------------
4529 function Construct_Attribute_Reference
4532 Dim : Nat) return Node_Id
4536 Make_Attribute_Reference (Loc,
4538 Attribute_Name => Nam,
4539 Expressions => New_List (
4540 Make_Integer_Literal (Loc, Dim)));
4541 end Construct_Attribute_Reference;
4543 -- Start of processing for Check_Subscripts
4546 for J in 1 .. Number_Dimensions (Typ) loop
4547 Evolve_And_Then (Cond,
4550 Construct_Attribute_Reference
4551 (Duplicate_Subexpr_No_Checks (Obj),
4554 Construct_Attribute_Reference
4555 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
4557 Evolve_And_Then (Cond,
4560 Construct_Attribute_Reference
4561 (Duplicate_Subexpr_No_Checks (Obj),
4564 Construct_Attribute_Reference
4565 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
4574 Right_Opnd => Make_Null (Loc)),
4575 Right_Opnd => Cond);
4579 Analyze_And_Resolve (N, Rtyp);
4580 end Check_Subscripts;
4582 -- These are the cases where constraint checks may be required,
4583 -- e.g. records with possible discriminants
4586 -- Expand the test into a series of discriminant comparisons.
4587 -- The expression that is built is the negation of the one that
4588 -- is used for checking discriminant constraints.
4590 Obj := Relocate_Node (Left_Opnd (N));
4592 if Has_Discriminants (Typ) then
4593 Cond := Make_Op_Not (Loc,
4594 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
4597 Cond := Make_Or_Else (Loc,
4601 Right_Opnd => Make_Null (Loc)),
4602 Right_Opnd => Cond);
4606 Cond := New_Occurrence_Of (Standard_True, Loc);
4610 Analyze_And_Resolve (N, Rtyp);
4616 --------------------------------
4617 -- Expand_N_Indexed_Component --
4618 --------------------------------
4620 procedure Expand_N_Indexed_Component (N : Node_Id) is
4621 Loc : constant Source_Ptr := Sloc (N);
4622 Typ : constant Entity_Id := Etype (N);
4623 P : constant Node_Id := Prefix (N);
4624 T : constant Entity_Id := Etype (P);
4627 -- A special optimization, if we have an indexed component that is
4628 -- selecting from a slice, then we can eliminate the slice, since, for
4629 -- example, x (i .. j)(k) is identical to x(k). The only difference is
4630 -- the range check required by the slice. The range check for the slice
4631 -- itself has already been generated. The range check for the
4632 -- subscripting operation is ensured by converting the subject to
4633 -- the subtype of the slice.
4635 -- This optimization not only generates better code, avoiding slice
4636 -- messing especially in the packed case, but more importantly bypasses
4637 -- some problems in handling this peculiar case, for example, the issue
4638 -- of dealing specially with object renamings.
4640 if Nkind (P) = N_Slice then
4642 Make_Indexed_Component (Loc,
4643 Prefix => Prefix (P),
4644 Expressions => New_List (
4646 (Etype (First_Index (Etype (P))),
4647 First (Expressions (N))))));
4648 Analyze_And_Resolve (N, Typ);
4652 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
4653 -- function, then additional actuals must be passed.
4655 if Ada_Version >= Ada_05
4656 and then Is_Build_In_Place_Function_Call (P)
4658 Make_Build_In_Place_Call_In_Anonymous_Context (P);
4661 -- If the prefix is an access type, then we unconditionally rewrite if
4662 -- as an explicit deference. This simplifies processing for several
4663 -- cases, including packed array cases and certain cases in which checks
4664 -- must be generated. We used to try to do this only when it was
4665 -- necessary, but it cleans up the code to do it all the time.
4667 if Is_Access_Type (T) then
4668 Insert_Explicit_Dereference (P);
4669 Analyze_And_Resolve (P, Designated_Type (T));
4672 -- Generate index and validity checks
4674 Generate_Index_Checks (N);
4676 if Validity_Checks_On and then Validity_Check_Subscripts then
4677 Apply_Subscript_Validity_Checks (N);
4680 -- All done for the non-packed case
4682 if not Is_Packed (Etype (Prefix (N))) then
4686 -- For packed arrays that are not bit-packed (i.e. the case of an array
4687 -- with one or more index types with a non-contiguous enumeration type),
4688 -- we can always use the normal packed element get circuit.
4690 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
4691 Expand_Packed_Element_Reference (N);
4695 -- For a reference to a component of a bit packed array, we have to
4696 -- convert it to a reference to the corresponding Packed_Array_Type.
4697 -- We only want to do this for simple references, and not for:
4699 -- Left side of assignment, or prefix of left side of assignment, or
4700 -- prefix of the prefix, to handle packed arrays of packed arrays,
4701 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
4703 -- Renaming objects in renaming associations
4704 -- This case is handled when a use of the renamed variable occurs
4706 -- Actual parameters for a procedure call
4707 -- This case is handled in Exp_Ch6.Expand_Actuals
4709 -- The second expression in a 'Read attribute reference
4711 -- The prefix of an address or size attribute reference
4713 -- The following circuit detects these exceptions
4716 Child : Node_Id := N;
4717 Parnt : Node_Id := Parent (N);
4721 if Nkind (Parnt) = N_Unchecked_Expression then
4724 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
4725 N_Procedure_Call_Statement)
4726 or else (Nkind (Parnt) = N_Parameter_Association
4728 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
4732 elsif Nkind (Parnt) = N_Attribute_Reference
4733 and then (Attribute_Name (Parnt) = Name_Address
4735 Attribute_Name (Parnt) = Name_Size)
4736 and then Prefix (Parnt) = Child
4740 elsif Nkind (Parnt) = N_Assignment_Statement
4741 and then Name (Parnt) = Child
4745 -- If the expression is an index of an indexed component, it must
4746 -- be expanded regardless of context.
4748 elsif Nkind (Parnt) = N_Indexed_Component
4749 and then Child /= Prefix (Parnt)
4751 Expand_Packed_Element_Reference (N);
4754 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
4755 and then Name (Parent (Parnt)) = Parnt
4759 elsif Nkind (Parnt) = N_Attribute_Reference
4760 and then Attribute_Name (Parnt) = Name_Read
4761 and then Next (First (Expressions (Parnt))) = Child
4765 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
4766 and then Prefix (Parnt) = Child
4771 Expand_Packed_Element_Reference (N);
4775 -- Keep looking up tree for unchecked expression, or if we are the
4776 -- prefix of a possible assignment left side.
4779 Parnt := Parent (Child);
4782 end Expand_N_Indexed_Component;
4784 ---------------------
4785 -- Expand_N_Not_In --
4786 ---------------------
4788 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
4789 -- can be done. This avoids needing to duplicate this expansion code.
4791 procedure Expand_N_Not_In (N : Node_Id) is
4792 Loc : constant Source_Ptr := Sloc (N);
4793 Typ : constant Entity_Id := Etype (N);
4794 Cfs : constant Boolean := Comes_From_Source (N);
4801 Left_Opnd => Left_Opnd (N),
4802 Right_Opnd => Right_Opnd (N))));
4804 -- If this is a set membership, preserve list of alternatives
4806 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
4808 -- We want this to appear as coming from source if original does (see
4809 -- transformations in Expand_N_In).
4811 Set_Comes_From_Source (N, Cfs);
4812 Set_Comes_From_Source (Right_Opnd (N), Cfs);
4814 -- Now analyze transformed node
4816 Analyze_And_Resolve (N, Typ);
4817 end Expand_N_Not_In;
4823 -- The only replacement required is for the case of a null of type that is
4824 -- an access to protected subprogram. We represent such access values as a
4825 -- record, and so we must replace the occurrence of null by the equivalent
4826 -- record (with a null address and a null pointer in it), so that the
4827 -- backend creates the proper value.
4829 procedure Expand_N_Null (N : Node_Id) is
4830 Loc : constant Source_Ptr := Sloc (N);
4831 Typ : constant Entity_Id := Etype (N);
4835 if Is_Access_Protected_Subprogram_Type (Typ) then
4837 Make_Aggregate (Loc,
4838 Expressions => New_List (
4839 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
4843 Analyze_And_Resolve (N, Equivalent_Type (Typ));
4845 -- For subsequent semantic analysis, the node must retain its type.
4846 -- Gigi in any case replaces this type by the corresponding record
4847 -- type before processing the node.
4853 when RE_Not_Available =>
4857 ---------------------
4858 -- Expand_N_Op_Abs --
4859 ---------------------
4861 procedure Expand_N_Op_Abs (N : Node_Id) is
4862 Loc : constant Source_Ptr := Sloc (N);
4863 Expr : constant Node_Id := Right_Opnd (N);
4866 Unary_Op_Validity_Checks (N);
4868 -- Deal with software overflow checking
4870 if not Backend_Overflow_Checks_On_Target
4871 and then Is_Signed_Integer_Type (Etype (N))
4872 and then Do_Overflow_Check (N)
4874 -- The only case to worry about is when the argument is equal to the
4875 -- largest negative number, so what we do is to insert the check:
4877 -- [constraint_error when Expr = typ'Base'First]
4879 -- with the usual Duplicate_Subexpr use coding for expr
4882 Make_Raise_Constraint_Error (Loc,
4885 Left_Opnd => Duplicate_Subexpr (Expr),
4887 Make_Attribute_Reference (Loc,
4889 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
4890 Attribute_Name => Name_First)),
4891 Reason => CE_Overflow_Check_Failed));
4894 -- Vax floating-point types case
4896 if Vax_Float (Etype (N)) then
4897 Expand_Vax_Arith (N);
4899 end Expand_N_Op_Abs;
4901 ---------------------
4902 -- Expand_N_Op_Add --
4903 ---------------------
4905 procedure Expand_N_Op_Add (N : Node_Id) is
4906 Typ : constant Entity_Id := Etype (N);
4909 Binary_Op_Validity_Checks (N);
4911 -- N + 0 = 0 + N = N for integer types
4913 if Is_Integer_Type (Typ) then
4914 if Compile_Time_Known_Value (Right_Opnd (N))
4915 and then Expr_Value (Right_Opnd (N)) = Uint_0
4917 Rewrite (N, Left_Opnd (N));
4920 elsif Compile_Time_Known_Value (Left_Opnd (N))
4921 and then Expr_Value (Left_Opnd (N)) = Uint_0
4923 Rewrite (N, Right_Opnd (N));
4928 -- Arithmetic overflow checks for signed integer/fixed point types
4930 if Is_Signed_Integer_Type (Typ)
4931 or else Is_Fixed_Point_Type (Typ)
4933 Apply_Arithmetic_Overflow_Check (N);
4936 -- Vax floating-point types case
4938 elsif Vax_Float (Typ) then
4939 Expand_Vax_Arith (N);
4941 end Expand_N_Op_Add;
4943 ---------------------
4944 -- Expand_N_Op_And --
4945 ---------------------
4947 procedure Expand_N_Op_And (N : Node_Id) is
4948 Typ : constant Entity_Id := Etype (N);
4951 Binary_Op_Validity_Checks (N);
4953 if Is_Array_Type (Etype (N)) then
4954 Expand_Boolean_Operator (N);
4956 elsif Is_Boolean_Type (Etype (N)) then
4957 Adjust_Condition (Left_Opnd (N));
4958 Adjust_Condition (Right_Opnd (N));
4959 Set_Etype (N, Standard_Boolean);
4960 Adjust_Result_Type (N, Typ);
4962 end Expand_N_Op_And;
4964 ------------------------
4965 -- Expand_N_Op_Concat --
4966 ------------------------
4968 procedure Expand_N_Op_Concat (N : Node_Id) is
4970 -- List of operands to be concatenated
4973 -- Node which is to be replaced by the result of concatenating the nodes
4974 -- in the list Opnds.
4977 -- Ensure validity of both operands
4979 Binary_Op_Validity_Checks (N);
4981 -- If we are the left operand of a concatenation higher up the tree,
4982 -- then do nothing for now, since we want to deal with a series of
4983 -- concatenations as a unit.
4985 if Nkind (Parent (N)) = N_Op_Concat
4986 and then N = Left_Opnd (Parent (N))
4991 -- We get here with a concatenation whose left operand may be a
4992 -- concatenation itself with a consistent type. We need to process
4993 -- these concatenation operands from left to right, which means
4994 -- from the deepest node in the tree to the highest node.
4997 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
4998 Cnode := Left_Opnd (Cnode);
5001 -- Now Opnd is the deepest Opnd, and its parents are the concatenation
5002 -- nodes above, so now we process bottom up, doing the operations. We
5003 -- gather a string that is as long as possible up to five operands
5005 -- The outer loop runs more than once if more than one concatenation
5006 -- type is involved.
5009 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
5010 Set_Parent (Opnds, N);
5012 -- The inner loop gathers concatenation operands
5014 Inner : while Cnode /= N
5015 and then Base_Type (Etype (Cnode)) =
5016 Base_Type (Etype (Parent (Cnode)))
5018 Cnode := Parent (Cnode);
5019 Append (Right_Opnd (Cnode), Opnds);
5022 Expand_Concatenate (Cnode, Opnds);
5024 exit Outer when Cnode = N;
5025 Cnode := Parent (Cnode);
5027 end Expand_N_Op_Concat;
5029 ------------------------
5030 -- Expand_N_Op_Divide --
5031 ------------------------
5033 procedure Expand_N_Op_Divide (N : Node_Id) is
5034 Loc : constant Source_Ptr := Sloc (N);
5035 Lopnd : constant Node_Id := Left_Opnd (N);
5036 Ropnd : constant Node_Id := Right_Opnd (N);
5037 Ltyp : constant Entity_Id := Etype (Lopnd);
5038 Rtyp : constant Entity_Id := Etype (Ropnd);
5039 Typ : Entity_Id := Etype (N);
5040 Rknow : constant Boolean := Is_Integer_Type (Typ)
5042 Compile_Time_Known_Value (Ropnd);
5046 Binary_Op_Validity_Checks (N);
5049 Rval := Expr_Value (Ropnd);
5052 -- N / 1 = N for integer types
5054 if Rknow and then Rval = Uint_1 then
5059 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
5060 -- Is_Power_Of_2_For_Shift is set means that we know that our left
5061 -- operand is an unsigned integer, as required for this to work.
5063 if Nkind (Ropnd) = N_Op_Expon
5064 and then Is_Power_Of_2_For_Shift (Ropnd)
5066 -- We cannot do this transformation in configurable run time mode if we
5067 -- have 64-bit -- integers and long shifts are not available.
5071 or else Support_Long_Shifts_On_Target)
5074 Make_Op_Shift_Right (Loc,
5077 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
5078 Analyze_And_Resolve (N, Typ);
5082 -- Do required fixup of universal fixed operation
5084 if Typ = Universal_Fixed then
5085 Fixup_Universal_Fixed_Operation (N);
5089 -- Divisions with fixed-point results
5091 if Is_Fixed_Point_Type (Typ) then
5093 -- No special processing if Treat_Fixed_As_Integer is set, since
5094 -- from a semantic point of view such operations are simply integer
5095 -- operations and will be treated that way.
5097 if not Treat_Fixed_As_Integer (N) then
5098 if Is_Integer_Type (Rtyp) then
5099 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
5101 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
5105 -- Other cases of division of fixed-point operands. Again we exclude the
5106 -- case where Treat_Fixed_As_Integer is set.
5108 elsif (Is_Fixed_Point_Type (Ltyp) or else
5109 Is_Fixed_Point_Type (Rtyp))
5110 and then not Treat_Fixed_As_Integer (N)
5112 if Is_Integer_Type (Typ) then
5113 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
5115 pragma Assert (Is_Floating_Point_Type (Typ));
5116 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
5119 -- Mixed-mode operations can appear in a non-static universal context,
5120 -- in which case the integer argument must be converted explicitly.
5122 elsif Typ = Universal_Real
5123 and then Is_Integer_Type (Rtyp)
5126 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
5128 Analyze_And_Resolve (Ropnd, Universal_Real);
5130 elsif Typ = Universal_Real
5131 and then Is_Integer_Type (Ltyp)
5134 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
5136 Analyze_And_Resolve (Lopnd, Universal_Real);
5138 -- Non-fixed point cases, do integer zero divide and overflow checks
5140 elsif Is_Integer_Type (Typ) then
5141 Apply_Divide_Check (N);
5143 -- Check for 64-bit division available, or long shifts if the divisor
5144 -- is a small power of 2 (since such divides will be converted into
5147 if Esize (Ltyp) > 32
5148 and then not Support_64_Bit_Divides_On_Target
5151 or else not Support_Long_Shifts_On_Target
5152 or else (Rval /= Uint_2 and then
5153 Rval /= Uint_4 and then
5154 Rval /= Uint_8 and then
5155 Rval /= Uint_16 and then
5156 Rval /= Uint_32 and then
5159 Error_Msg_CRT ("64-bit division", N);
5162 -- Deal with Vax_Float
5164 elsif Vax_Float (Typ) then
5165 Expand_Vax_Arith (N);
5168 end Expand_N_Op_Divide;
5170 --------------------
5171 -- Expand_N_Op_Eq --
5172 --------------------
5174 procedure Expand_N_Op_Eq (N : Node_Id) is
5175 Loc : constant Source_Ptr := Sloc (N);
5176 Typ : constant Entity_Id := Etype (N);
5177 Lhs : constant Node_Id := Left_Opnd (N);
5178 Rhs : constant Node_Id := Right_Opnd (N);
5179 Bodies : constant List_Id := New_List;
5180 A_Typ : constant Entity_Id := Etype (Lhs);
5182 Typl : Entity_Id := A_Typ;
5183 Op_Name : Entity_Id;
5186 procedure Build_Equality_Call (Eq : Entity_Id);
5187 -- If a constructed equality exists for the type or for its parent,
5188 -- build and analyze call, adding conversions if the operation is
5191 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
5192 -- Determines whether a type has a subcomponent of an unconstrained
5193 -- Unchecked_Union subtype. Typ is a record type.
5195 -------------------------
5196 -- Build_Equality_Call --
5197 -------------------------
5199 procedure Build_Equality_Call (Eq : Entity_Id) is
5200 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
5201 L_Exp : Node_Id := Relocate_Node (Lhs);
5202 R_Exp : Node_Id := Relocate_Node (Rhs);
5205 if Base_Type (Op_Type) /= Base_Type (A_Typ)
5206 and then not Is_Class_Wide_Type (A_Typ)
5208 L_Exp := OK_Convert_To (Op_Type, L_Exp);
5209 R_Exp := OK_Convert_To (Op_Type, R_Exp);
5212 -- If we have an Unchecked_Union, we need to add the inferred
5213 -- discriminant values as actuals in the function call. At this
5214 -- point, the expansion has determined that both operands have
5215 -- inferable discriminants.
5217 if Is_Unchecked_Union (Op_Type) then
5219 Lhs_Type : constant Node_Id := Etype (L_Exp);
5220 Rhs_Type : constant Node_Id := Etype (R_Exp);
5221 Lhs_Discr_Val : Node_Id;
5222 Rhs_Discr_Val : Node_Id;
5225 -- Per-object constrained selected components require special
5226 -- attention. If the enclosing scope of the component is an
5227 -- Unchecked_Union, we cannot reference its discriminants
5228 -- directly. This is why we use the two extra parameters of
5229 -- the equality function of the enclosing Unchecked_Union.
5231 -- type UU_Type (Discr : Integer := 0) is
5234 -- pragma Unchecked_Union (UU_Type);
5236 -- 1. Unchecked_Union enclosing record:
5238 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
5240 -- Comp : UU_Type (Discr);
5242 -- end Enclosing_UU_Type;
5243 -- pragma Unchecked_Union (Enclosing_UU_Type);
5245 -- Obj1 : Enclosing_UU_Type;
5246 -- Obj2 : Enclosing_UU_Type (1);
5248 -- [. . .] Obj1 = Obj2 [. . .]
5252 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
5254 -- A and B are the formal parameters of the equality function
5255 -- of Enclosing_UU_Type. The function always has two extra
5256 -- formals to capture the inferred discriminant values.
5258 -- 2. Non-Unchecked_Union enclosing record:
5261 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
5264 -- Comp : UU_Type (Discr);
5266 -- end Enclosing_Non_UU_Type;
5268 -- Obj1 : Enclosing_Non_UU_Type;
5269 -- Obj2 : Enclosing_Non_UU_Type (1);
5271 -- ... Obj1 = Obj2 ...
5275 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
5276 -- obj1.discr, obj2.discr)) then
5278 -- In this case we can directly reference the discriminants of
5279 -- the enclosing record.
5283 if Nkind (Lhs) = N_Selected_Component
5284 and then Has_Per_Object_Constraint
5285 (Entity (Selector_Name (Lhs)))
5287 -- Enclosing record is an Unchecked_Union, use formal A
5289 if Is_Unchecked_Union (Scope
5290 (Entity (Selector_Name (Lhs))))
5293 Make_Identifier (Loc,
5296 -- Enclosing record is of a non-Unchecked_Union type, it is
5297 -- possible to reference the discriminant.
5301 Make_Selected_Component (Loc,
5302 Prefix => Prefix (Lhs),
5305 (Get_Discriminant_Value
5306 (First_Discriminant (Lhs_Type),
5308 Stored_Constraint (Lhs_Type))));
5311 -- Comment needed here ???
5314 -- Infer the discriminant value
5318 (Get_Discriminant_Value
5319 (First_Discriminant (Lhs_Type),
5321 Stored_Constraint (Lhs_Type)));
5326 if Nkind (Rhs) = N_Selected_Component
5327 and then Has_Per_Object_Constraint
5328 (Entity (Selector_Name (Rhs)))
5330 if Is_Unchecked_Union
5331 (Scope (Entity (Selector_Name (Rhs))))
5334 Make_Identifier (Loc,
5339 Make_Selected_Component (Loc,
5340 Prefix => Prefix (Rhs),
5342 New_Copy (Get_Discriminant_Value (
5343 First_Discriminant (Rhs_Type),
5345 Stored_Constraint (Rhs_Type))));
5350 New_Copy (Get_Discriminant_Value (
5351 First_Discriminant (Rhs_Type),
5353 Stored_Constraint (Rhs_Type)));
5358 Make_Function_Call (Loc,
5359 Name => New_Reference_To (Eq, Loc),
5360 Parameter_Associations => New_List (
5367 -- Normal case, not an unchecked union
5371 Make_Function_Call (Loc,
5372 Name => New_Reference_To (Eq, Loc),
5373 Parameter_Associations => New_List (L_Exp, R_Exp)));
5376 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5377 end Build_Equality_Call;
5379 ------------------------------------
5380 -- Has_Unconstrained_UU_Component --
5381 ------------------------------------
5383 function Has_Unconstrained_UU_Component
5384 (Typ : Node_Id) return Boolean
5386 Tdef : constant Node_Id :=
5387 Type_Definition (Declaration_Node (Base_Type (Typ)));
5391 function Component_Is_Unconstrained_UU
5392 (Comp : Node_Id) return Boolean;
5393 -- Determines whether the subtype of the component is an
5394 -- unconstrained Unchecked_Union.
5396 function Variant_Is_Unconstrained_UU
5397 (Variant : Node_Id) return Boolean;
5398 -- Determines whether a component of the variant has an unconstrained
5399 -- Unchecked_Union subtype.
5401 -----------------------------------
5402 -- Component_Is_Unconstrained_UU --
5403 -----------------------------------
5405 function Component_Is_Unconstrained_UU
5406 (Comp : Node_Id) return Boolean
5409 if Nkind (Comp) /= N_Component_Declaration then
5414 Sindic : constant Node_Id :=
5415 Subtype_Indication (Component_Definition (Comp));
5418 -- Unconstrained nominal type. In the case of a constraint
5419 -- present, the node kind would have been N_Subtype_Indication.
5421 if Nkind (Sindic) = N_Identifier then
5422 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
5427 end Component_Is_Unconstrained_UU;
5429 ---------------------------------
5430 -- Variant_Is_Unconstrained_UU --
5431 ---------------------------------
5433 function Variant_Is_Unconstrained_UU
5434 (Variant : Node_Id) return Boolean
5436 Clist : constant Node_Id := Component_List (Variant);
5439 if Is_Empty_List (Component_Items (Clist)) then
5443 -- We only need to test one component
5446 Comp : Node_Id := First (Component_Items (Clist));
5449 while Present (Comp) loop
5450 if Component_Is_Unconstrained_UU (Comp) then
5458 -- None of the components withing the variant were of
5459 -- unconstrained Unchecked_Union type.
5462 end Variant_Is_Unconstrained_UU;
5464 -- Start of processing for Has_Unconstrained_UU_Component
5467 if Null_Present (Tdef) then
5471 Clist := Component_List (Tdef);
5472 Vpart := Variant_Part (Clist);
5474 -- Inspect available components
5476 if Present (Component_Items (Clist)) then
5478 Comp : Node_Id := First (Component_Items (Clist));
5481 while Present (Comp) loop
5483 -- One component is sufficient
5485 if Component_Is_Unconstrained_UU (Comp) then
5494 -- Inspect available components withing variants
5496 if Present (Vpart) then
5498 Variant : Node_Id := First (Variants (Vpart));
5501 while Present (Variant) loop
5503 -- One component within a variant is sufficient
5505 if Variant_Is_Unconstrained_UU (Variant) then
5514 -- Neither the available components, nor the components inside the
5515 -- variant parts were of an unconstrained Unchecked_Union subtype.
5518 end Has_Unconstrained_UU_Component;
5520 -- Start of processing for Expand_N_Op_Eq
5523 Binary_Op_Validity_Checks (N);
5525 if Ekind (Typl) = E_Private_Type then
5526 Typl := Underlying_Type (Typl);
5527 elsif Ekind (Typl) = E_Private_Subtype then
5528 Typl := Underlying_Type (Base_Type (Typl));
5533 -- It may happen in error situations that the underlying type is not
5534 -- set. The error will be detected later, here we just defend the
5541 Typl := Base_Type (Typl);
5543 -- Boolean types (requiring handling of non-standard case)
5545 if Is_Boolean_Type (Typl) then
5546 Adjust_Condition (Left_Opnd (N));
5547 Adjust_Condition (Right_Opnd (N));
5548 Set_Etype (N, Standard_Boolean);
5549 Adjust_Result_Type (N, Typ);
5553 elsif Is_Array_Type (Typl) then
5555 -- If we are doing full validity checking, and it is possible for the
5556 -- array elements to be invalid then expand out array comparisons to
5557 -- make sure that we check the array elements.
5559 if Validity_Check_Operands
5560 and then not Is_Known_Valid (Component_Type (Typl))
5563 Save_Force_Validity_Checks : constant Boolean :=
5564 Force_Validity_Checks;
5566 Force_Validity_Checks := True;
5568 Expand_Array_Equality
5570 Relocate_Node (Lhs),
5571 Relocate_Node (Rhs),
5574 Insert_Actions (N, Bodies);
5575 Analyze_And_Resolve (N, Standard_Boolean);
5576 Force_Validity_Checks := Save_Force_Validity_Checks;
5579 -- Packed case where both operands are known aligned
5581 elsif Is_Bit_Packed_Array (Typl)
5582 and then not Is_Possibly_Unaligned_Object (Lhs)
5583 and then not Is_Possibly_Unaligned_Object (Rhs)
5585 Expand_Packed_Eq (N);
5587 -- Where the component type is elementary we can use a block bit
5588 -- comparison (if supported on the target) exception in the case
5589 -- of floating-point (negative zero issues require element by
5590 -- element comparison), and atomic types (where we must be sure
5591 -- to load elements independently) and possibly unaligned arrays.
5593 elsif Is_Elementary_Type (Component_Type (Typl))
5594 and then not Is_Floating_Point_Type (Component_Type (Typl))
5595 and then not Is_Atomic (Component_Type (Typl))
5596 and then not Is_Possibly_Unaligned_Object (Lhs)
5597 and then not Is_Possibly_Unaligned_Object (Rhs)
5598 and then Support_Composite_Compare_On_Target
5602 -- For composite and floating-point cases, expand equality loop to
5603 -- make sure of using proper comparisons for tagged types, and
5604 -- correctly handling the floating-point case.
5608 Expand_Array_Equality
5610 Relocate_Node (Lhs),
5611 Relocate_Node (Rhs),
5614 Insert_Actions (N, Bodies, Suppress => All_Checks);
5615 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5620 elsif Is_Record_Type (Typl) then
5622 -- For tagged types, use the primitive "="
5624 if Is_Tagged_Type (Typl) then
5626 -- No need to do anything else compiling under restriction
5627 -- No_Dispatching_Calls. During the semantic analysis we
5628 -- already notified such violation.
5630 if Restriction_Active (No_Dispatching_Calls) then
5634 -- If this is derived from an untagged private type completed with
5635 -- a tagged type, it does not have a full view, so we use the
5636 -- primitive operations of the private type. This check should no
5637 -- longer be necessary when these types get their full views???
5639 if Is_Private_Type (A_Typ)
5640 and then not Is_Tagged_Type (A_Typ)
5641 and then Is_Derived_Type (A_Typ)
5642 and then No (Full_View (A_Typ))
5644 -- Search for equality operation, checking that the operands
5645 -- have the same type. Note that we must find a matching entry,
5646 -- or something is very wrong!
5648 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
5650 while Present (Prim) loop
5651 exit when Chars (Node (Prim)) = Name_Op_Eq
5652 and then Etype (First_Formal (Node (Prim))) =
5653 Etype (Next_Formal (First_Formal (Node (Prim))))
5655 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5660 pragma Assert (Present (Prim));
5661 Op_Name := Node (Prim);
5663 -- Find the type's predefined equality or an overriding
5664 -- user- defined equality. The reason for not simply calling
5665 -- Find_Prim_Op here is that there may be a user-defined
5666 -- overloaded equality op that precedes the equality that we want,
5667 -- so we have to explicitly search (e.g., there could be an
5668 -- equality with two different parameter types).
5671 if Is_Class_Wide_Type (Typl) then
5672 Typl := Root_Type (Typl);
5675 Prim := First_Elmt (Primitive_Operations (Typl));
5676 while Present (Prim) loop
5677 exit when Chars (Node (Prim)) = Name_Op_Eq
5678 and then Etype (First_Formal (Node (Prim))) =
5679 Etype (Next_Formal (First_Formal (Node (Prim))))
5681 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5686 pragma Assert (Present (Prim));
5687 Op_Name := Node (Prim);
5690 Build_Equality_Call (Op_Name);
5692 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
5693 -- predefined equality operator for a type which has a subcomponent
5694 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
5696 elsif Has_Unconstrained_UU_Component (Typl) then
5698 Make_Raise_Program_Error (Loc,
5699 Reason => PE_Unchecked_Union_Restriction));
5701 -- Prevent Gigi from generating incorrect code by rewriting the
5702 -- equality as a standard False.
5705 New_Occurrence_Of (Standard_False, Loc));
5707 elsif Is_Unchecked_Union (Typl) then
5709 -- If we can infer the discriminants of the operands, we make a
5710 -- call to the TSS equality function.
5712 if Has_Inferable_Discriminants (Lhs)
5714 Has_Inferable_Discriminants (Rhs)
5717 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5720 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5721 -- the predefined equality operator for an Unchecked_Union type
5722 -- if either of the operands lack inferable discriminants.
5725 Make_Raise_Program_Error (Loc,
5726 Reason => PE_Unchecked_Union_Restriction));
5728 -- Prevent Gigi from generating incorrect code by rewriting
5729 -- the equality as a standard False.
5732 New_Occurrence_Of (Standard_False, Loc));
5736 -- If a type support function is present (for complex cases), use it
5738 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
5740 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5742 -- Otherwise expand the component by component equality. Note that
5743 -- we never use block-bit comparisons for records, because of the
5744 -- problems with gaps. The backend will often be able to recombine
5745 -- the separate comparisons that we generate here.
5748 Remove_Side_Effects (Lhs);
5749 Remove_Side_Effects (Rhs);
5751 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
5753 Insert_Actions (N, Bodies, Suppress => All_Checks);
5754 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5758 -- Test if result is known at compile time
5760 Rewrite_Comparison (N);
5762 -- If we still have comparison for Vax_Float, process it
5764 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
5765 Expand_Vax_Comparison (N);
5770 -----------------------
5771 -- Expand_N_Op_Expon --
5772 -----------------------
5774 procedure Expand_N_Op_Expon (N : Node_Id) is
5775 Loc : constant Source_Ptr := Sloc (N);
5776 Typ : constant Entity_Id := Etype (N);
5777 Rtyp : constant Entity_Id := Root_Type (Typ);
5778 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
5779 Bastyp : constant Node_Id := Etype (Base);
5780 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
5781 Exptyp : constant Entity_Id := Etype (Exp);
5782 Ovflo : constant Boolean := Do_Overflow_Check (N);
5791 Binary_Op_Validity_Checks (N);
5793 -- If either operand is of a private type, then we have the use of an
5794 -- intrinsic operator, and we get rid of the privateness, by using root
5795 -- types of underlying types for the actual operation. Otherwise the
5796 -- private types will cause trouble if we expand multiplications or
5797 -- shifts etc. We also do this transformation if the result type is
5798 -- different from the base type.
5800 if Is_Private_Type (Etype (Base))
5802 Is_Private_Type (Typ)
5804 Is_Private_Type (Exptyp)
5806 Rtyp /= Root_Type (Bastyp)
5809 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
5810 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
5814 Unchecked_Convert_To (Typ,
5816 Left_Opnd => Unchecked_Convert_To (Bt, Base),
5817 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
5818 Analyze_And_Resolve (N, Typ);
5823 -- Test for case of known right argument
5825 if Compile_Time_Known_Value (Exp) then
5826 Expv := Expr_Value (Exp);
5828 -- We only fold small non-negative exponents. You might think we
5829 -- could fold small negative exponents for the real case, but we
5830 -- can't because we are required to raise Constraint_Error for
5831 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
5832 -- See ACVC test C4A012B.
5834 if Expv >= 0 and then Expv <= 4 then
5836 -- X ** 0 = 1 (or 1.0)
5840 -- Call Remove_Side_Effects to ensure that any side effects
5841 -- in the ignored left operand (in particular function calls
5842 -- to user defined functions) are properly executed.
5844 Remove_Side_Effects (Base);
5846 if Ekind (Typ) in Integer_Kind then
5847 Xnode := Make_Integer_Literal (Loc, Intval => 1);
5849 Xnode := Make_Real_Literal (Loc, Ureal_1);
5861 Make_Op_Multiply (Loc,
5862 Left_Opnd => Duplicate_Subexpr (Base),
5863 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
5865 -- X ** 3 = X * X * X
5869 Make_Op_Multiply (Loc,
5871 Make_Op_Multiply (Loc,
5872 Left_Opnd => Duplicate_Subexpr (Base),
5873 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
5874 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
5877 -- En : constant base'type := base * base;
5883 Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
5885 Insert_Actions (N, New_List (
5886 Make_Object_Declaration (Loc,
5887 Defining_Identifier => Temp,
5888 Constant_Present => True,
5889 Object_Definition => New_Reference_To (Typ, Loc),
5891 Make_Op_Multiply (Loc,
5892 Left_Opnd => Duplicate_Subexpr (Base),
5893 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
5896 Make_Op_Multiply (Loc,
5897 Left_Opnd => New_Reference_To (Temp, Loc),
5898 Right_Opnd => New_Reference_To (Temp, Loc));
5902 Analyze_And_Resolve (N, Typ);
5907 -- Case of (2 ** expression) appearing as an argument of an integer
5908 -- multiplication, or as the right argument of a division of a non-
5909 -- negative integer. In such cases we leave the node untouched, setting
5910 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
5911 -- of the higher level node converts it into a shift.
5913 -- Note: this transformation is not applicable for a modular type with
5914 -- a non-binary modulus in the multiplication case, since we get a wrong
5915 -- result if the shift causes an overflow before the modular reduction.
5917 if Nkind (Base) = N_Integer_Literal
5918 and then Intval (Base) = 2
5919 and then Is_Integer_Type (Root_Type (Exptyp))
5920 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
5921 and then Is_Unsigned_Type (Exptyp)
5923 and then Nkind (Parent (N)) in N_Binary_Op
5926 P : constant Node_Id := Parent (N);
5927 L : constant Node_Id := Left_Opnd (P);
5928 R : constant Node_Id := Right_Opnd (P);
5931 if (Nkind (P) = N_Op_Multiply
5932 and then not Non_Binary_Modulus (Typ)
5934 ((Is_Integer_Type (Etype (L)) and then R = N)
5936 (Is_Integer_Type (Etype (R)) and then L = N))
5937 and then not Do_Overflow_Check (P))
5940 (Nkind (P) = N_Op_Divide
5941 and then Is_Integer_Type (Etype (L))
5942 and then Is_Unsigned_Type (Etype (L))
5944 and then not Do_Overflow_Check (P))
5946 Set_Is_Power_Of_2_For_Shift (N);
5952 -- Fall through if exponentiation must be done using a runtime routine
5954 -- First deal with modular case
5956 if Is_Modular_Integer_Type (Rtyp) then
5958 -- Non-binary case, we call the special exponentiation routine for
5959 -- the non-binary case, converting the argument to Long_Long_Integer
5960 -- and passing the modulus value. Then the result is converted back
5961 -- to the base type.
5963 if Non_Binary_Modulus (Rtyp) then
5966 Make_Function_Call (Loc,
5967 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
5968 Parameter_Associations => New_List (
5969 Convert_To (Standard_Integer, Base),
5970 Make_Integer_Literal (Loc, Modulus (Rtyp)),
5973 -- Binary case, in this case, we call one of two routines, either the
5974 -- unsigned integer case, or the unsigned long long integer case,
5975 -- with a final "and" operation to do the required mod.
5978 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
5979 Ent := RTE (RE_Exp_Unsigned);
5981 Ent := RTE (RE_Exp_Long_Long_Unsigned);
5988 Make_Function_Call (Loc,
5989 Name => New_Reference_To (Ent, Loc),
5990 Parameter_Associations => New_List (
5991 Convert_To (Etype (First_Formal (Ent)), Base),
5994 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
5998 -- Common exit point for modular type case
6000 Analyze_And_Resolve (N, Typ);
6003 -- Signed integer cases, done using either Integer or Long_Long_Integer.
6004 -- It is not worth having routines for Short_[Short_]Integer, since for
6005 -- most machines it would not help, and it would generate more code that
6006 -- might need certification when a certified run time is required.
6008 -- In the integer cases, we have two routines, one for when overflow
6009 -- checks are required, and one when they are not required, since there
6010 -- is a real gain in omitting checks on many machines.
6012 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
6013 or else (Rtyp = Base_Type (Standard_Long_Integer)
6015 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
6016 or else (Rtyp = Universal_Integer)
6018 Etyp := Standard_Long_Long_Integer;
6021 Rent := RE_Exp_Long_Long_Integer;
6023 Rent := RE_Exn_Long_Long_Integer;
6026 elsif Is_Signed_Integer_Type (Rtyp) then
6027 Etyp := Standard_Integer;
6030 Rent := RE_Exp_Integer;
6032 Rent := RE_Exn_Integer;
6035 -- Floating-point cases, always done using Long_Long_Float. We do not
6036 -- need separate routines for the overflow case here, since in the case
6037 -- of floating-point, we generate infinities anyway as a rule (either
6038 -- that or we automatically trap overflow), and if there is an infinity
6039 -- generated and a range check is required, the check will fail anyway.
6042 pragma Assert (Is_Floating_Point_Type (Rtyp));
6043 Etyp := Standard_Long_Long_Float;
6044 Rent := RE_Exn_Long_Long_Float;
6047 -- Common processing for integer cases and floating-point cases.
6048 -- If we are in the right type, we can call runtime routine directly
6051 and then Rtyp /= Universal_Integer
6052 and then Rtyp /= Universal_Real
6055 Make_Function_Call (Loc,
6056 Name => New_Reference_To (RTE (Rent), Loc),
6057 Parameter_Associations => New_List (Base, Exp)));
6059 -- Otherwise we have to introduce conversions (conversions are also
6060 -- required in the universal cases, since the runtime routine is
6061 -- typed using one of the standard types).
6066 Make_Function_Call (Loc,
6067 Name => New_Reference_To (RTE (Rent), Loc),
6068 Parameter_Associations => New_List (
6069 Convert_To (Etyp, Base),
6073 Analyze_And_Resolve (N, Typ);
6077 when RE_Not_Available =>
6079 end Expand_N_Op_Expon;
6081 --------------------
6082 -- Expand_N_Op_Ge --
6083 --------------------
6085 procedure Expand_N_Op_Ge (N : Node_Id) is
6086 Typ : constant Entity_Id := Etype (N);
6087 Op1 : constant Node_Id := Left_Opnd (N);
6088 Op2 : constant Node_Id := Right_Opnd (N);
6089 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6092 Binary_Op_Validity_Checks (N);
6094 if Is_Array_Type (Typ1) then
6095 Expand_Array_Comparison (N);
6099 if Is_Boolean_Type (Typ1) then
6100 Adjust_Condition (Op1);
6101 Adjust_Condition (Op2);
6102 Set_Etype (N, Standard_Boolean);
6103 Adjust_Result_Type (N, Typ);
6106 Rewrite_Comparison (N);
6108 -- If we still have comparison, and Vax_Float type, process it
6110 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6111 Expand_Vax_Comparison (N);
6116 --------------------
6117 -- Expand_N_Op_Gt --
6118 --------------------
6120 procedure Expand_N_Op_Gt (N : Node_Id) is
6121 Typ : constant Entity_Id := Etype (N);
6122 Op1 : constant Node_Id := Left_Opnd (N);
6123 Op2 : constant Node_Id := Right_Opnd (N);
6124 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6127 Binary_Op_Validity_Checks (N);
6129 if Is_Array_Type (Typ1) then
6130 Expand_Array_Comparison (N);
6134 if Is_Boolean_Type (Typ1) then
6135 Adjust_Condition (Op1);
6136 Adjust_Condition (Op2);
6137 Set_Etype (N, Standard_Boolean);
6138 Adjust_Result_Type (N, Typ);
6141 Rewrite_Comparison (N);
6143 -- If we still have comparison, and Vax_Float type, process it
6145 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6146 Expand_Vax_Comparison (N);
6151 --------------------
6152 -- Expand_N_Op_Le --
6153 --------------------
6155 procedure Expand_N_Op_Le (N : Node_Id) is
6156 Typ : constant Entity_Id := Etype (N);
6157 Op1 : constant Node_Id := Left_Opnd (N);
6158 Op2 : constant Node_Id := Right_Opnd (N);
6159 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6162 Binary_Op_Validity_Checks (N);
6164 if Is_Array_Type (Typ1) then
6165 Expand_Array_Comparison (N);
6169 if Is_Boolean_Type (Typ1) then
6170 Adjust_Condition (Op1);
6171 Adjust_Condition (Op2);
6172 Set_Etype (N, Standard_Boolean);
6173 Adjust_Result_Type (N, Typ);
6176 Rewrite_Comparison (N);
6178 -- If we still have comparison, and Vax_Float type, process it
6180 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6181 Expand_Vax_Comparison (N);
6186 --------------------
6187 -- Expand_N_Op_Lt --
6188 --------------------
6190 procedure Expand_N_Op_Lt (N : Node_Id) is
6191 Typ : constant Entity_Id := Etype (N);
6192 Op1 : constant Node_Id := Left_Opnd (N);
6193 Op2 : constant Node_Id := Right_Opnd (N);
6194 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6197 Binary_Op_Validity_Checks (N);
6199 if Is_Array_Type (Typ1) then
6200 Expand_Array_Comparison (N);
6204 if Is_Boolean_Type (Typ1) then
6205 Adjust_Condition (Op1);
6206 Adjust_Condition (Op2);
6207 Set_Etype (N, Standard_Boolean);
6208 Adjust_Result_Type (N, Typ);
6211 Rewrite_Comparison (N);
6213 -- If we still have comparison, and Vax_Float type, process it
6215 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6216 Expand_Vax_Comparison (N);
6221 -----------------------
6222 -- Expand_N_Op_Minus --
6223 -----------------------
6225 procedure Expand_N_Op_Minus (N : Node_Id) is
6226 Loc : constant Source_Ptr := Sloc (N);
6227 Typ : constant Entity_Id := Etype (N);
6230 Unary_Op_Validity_Checks (N);
6232 if not Backend_Overflow_Checks_On_Target
6233 and then Is_Signed_Integer_Type (Etype (N))
6234 and then Do_Overflow_Check (N)
6236 -- Software overflow checking expands -expr into (0 - expr)
6239 Make_Op_Subtract (Loc,
6240 Left_Opnd => Make_Integer_Literal (Loc, 0),
6241 Right_Opnd => Right_Opnd (N)));
6243 Analyze_And_Resolve (N, Typ);
6245 -- Vax floating-point types case
6247 elsif Vax_Float (Etype (N)) then
6248 Expand_Vax_Arith (N);
6250 end Expand_N_Op_Minus;
6252 ---------------------
6253 -- Expand_N_Op_Mod --
6254 ---------------------
6256 procedure Expand_N_Op_Mod (N : Node_Id) is
6257 Loc : constant Source_Ptr := Sloc (N);
6258 Typ : constant Entity_Id := Etype (N);
6259 Left : constant Node_Id := Left_Opnd (N);
6260 Right : constant Node_Id := Right_Opnd (N);
6261 DOC : constant Boolean := Do_Overflow_Check (N);
6262 DDC : constant Boolean := Do_Division_Check (N);
6272 pragma Warnings (Off, Lhi);
6275 Binary_Op_Validity_Checks (N);
6277 Determine_Range (Right, ROK, Rlo, Rhi);
6278 Determine_Range (Left, LOK, Llo, Lhi);
6280 -- Convert mod to rem if operands are known non-negative. We do this
6281 -- since it is quite likely that this will improve the quality of code,
6282 -- (the operation now corresponds to the hardware remainder), and it
6283 -- does not seem likely that it could be harmful.
6285 if LOK and then Llo >= 0
6287 ROK and then Rlo >= 0
6290 Make_Op_Rem (Sloc (N),
6291 Left_Opnd => Left_Opnd (N),
6292 Right_Opnd => Right_Opnd (N)));
6294 -- Instead of reanalyzing the node we do the analysis manually. This
6295 -- avoids anomalies when the replacement is done in an instance and
6296 -- is epsilon more efficient.
6298 Set_Entity (N, Standard_Entity (S_Op_Rem));
6300 Set_Do_Overflow_Check (N, DOC);
6301 Set_Do_Division_Check (N, DDC);
6302 Expand_N_Op_Rem (N);
6305 -- Otherwise, normal mod processing
6308 if Is_Integer_Type (Etype (N)) then
6309 Apply_Divide_Check (N);
6312 -- Apply optimization x mod 1 = 0. We don't really need that with
6313 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
6314 -- certainly harmless.
6316 if Is_Integer_Type (Etype (N))
6317 and then Compile_Time_Known_Value (Right)
6318 and then Expr_Value (Right) = Uint_1
6320 -- Call Remove_Side_Effects to ensure that any side effects in
6321 -- the ignored left operand (in particular function calls to
6322 -- user defined functions) are properly executed.
6324 Remove_Side_Effects (Left);
6326 Rewrite (N, Make_Integer_Literal (Loc, 0));
6327 Analyze_And_Resolve (N, Typ);
6331 -- Deal with annoying case of largest negative number remainder
6332 -- minus one. Gigi does not handle this case correctly, because
6333 -- it generates a divide instruction which may trap in this case.
6335 -- In fact the check is quite easy, if the right operand is -1, then
6336 -- the mod value is always 0, and we can just ignore the left operand
6337 -- completely in this case.
6339 -- The operand type may be private (e.g. in the expansion of an
6340 -- intrinsic operation) so we must use the underlying type to get the
6341 -- bounds, and convert the literals explicitly.
6345 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
6347 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
6349 ((not LOK) or else (Llo = LLB))
6352 Make_Conditional_Expression (Loc,
6353 Expressions => New_List (
6355 Left_Opnd => Duplicate_Subexpr (Right),
6357 Unchecked_Convert_To (Typ,
6358 Make_Integer_Literal (Loc, -1))),
6359 Unchecked_Convert_To (Typ,
6360 Make_Integer_Literal (Loc, Uint_0)),
6361 Relocate_Node (N))));
6363 Set_Analyzed (Next (Next (First (Expressions (N)))));
6364 Analyze_And_Resolve (N, Typ);
6367 end Expand_N_Op_Mod;
6369 --------------------------
6370 -- Expand_N_Op_Multiply --
6371 --------------------------
6373 procedure Expand_N_Op_Multiply (N : Node_Id) is
6374 Loc : constant Source_Ptr := Sloc (N);
6375 Lop : constant Node_Id := Left_Opnd (N);
6376 Rop : constant Node_Id := Right_Opnd (N);
6378 Lp2 : constant Boolean :=
6379 Nkind (Lop) = N_Op_Expon
6380 and then Is_Power_Of_2_For_Shift (Lop);
6382 Rp2 : constant Boolean :=
6383 Nkind (Rop) = N_Op_Expon
6384 and then Is_Power_Of_2_For_Shift (Rop);
6386 Ltyp : constant Entity_Id := Etype (Lop);
6387 Rtyp : constant Entity_Id := Etype (Rop);
6388 Typ : Entity_Id := Etype (N);
6391 Binary_Op_Validity_Checks (N);
6393 -- Special optimizations for integer types
6395 if Is_Integer_Type (Typ) then
6397 -- N * 0 = 0 for integer types
6399 if Compile_Time_Known_Value (Rop)
6400 and then Expr_Value (Rop) = Uint_0
6402 -- Call Remove_Side_Effects to ensure that any side effects in
6403 -- the ignored left operand (in particular function calls to
6404 -- user defined functions) are properly executed.
6406 Remove_Side_Effects (Lop);
6408 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6409 Analyze_And_Resolve (N, Typ);
6413 -- Similar handling for 0 * N = 0
6415 if Compile_Time_Known_Value (Lop)
6416 and then Expr_Value (Lop) = Uint_0
6418 Remove_Side_Effects (Rop);
6419 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6420 Analyze_And_Resolve (N, Typ);
6424 -- N * 1 = 1 * N = N for integer types
6426 -- This optimisation is not done if we are going to
6427 -- rewrite the product 1 * 2 ** N to a shift.
6429 if Compile_Time_Known_Value (Rop)
6430 and then Expr_Value (Rop) = Uint_1
6436 elsif Compile_Time_Known_Value (Lop)
6437 and then Expr_Value (Lop) = Uint_1
6445 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
6446 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6447 -- operand is an integer, as required for this to work.
6452 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
6456 Left_Opnd => Make_Integer_Literal (Loc, 2),
6459 Left_Opnd => Right_Opnd (Lop),
6460 Right_Opnd => Right_Opnd (Rop))));
6461 Analyze_And_Resolve (N, Typ);
6466 Make_Op_Shift_Left (Loc,
6469 Convert_To (Standard_Natural, Right_Opnd (Rop))));
6470 Analyze_And_Resolve (N, Typ);
6474 -- Same processing for the operands the other way round
6478 Make_Op_Shift_Left (Loc,
6481 Convert_To (Standard_Natural, Right_Opnd (Lop))));
6482 Analyze_And_Resolve (N, Typ);
6486 -- Do required fixup of universal fixed operation
6488 if Typ = Universal_Fixed then
6489 Fixup_Universal_Fixed_Operation (N);
6493 -- Multiplications with fixed-point results
6495 if Is_Fixed_Point_Type (Typ) then
6497 -- No special processing if Treat_Fixed_As_Integer is set, since from
6498 -- a semantic point of view such operations are simply integer
6499 -- operations and will be treated that way.
6501 if not Treat_Fixed_As_Integer (N) then
6503 -- Case of fixed * integer => fixed
6505 if Is_Integer_Type (Rtyp) then
6506 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
6508 -- Case of integer * fixed => fixed
6510 elsif Is_Integer_Type (Ltyp) then
6511 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
6513 -- Case of fixed * fixed => fixed
6516 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
6520 -- Other cases of multiplication of fixed-point operands. Again we
6521 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
6523 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6524 and then not Treat_Fixed_As_Integer (N)
6526 if Is_Integer_Type (Typ) then
6527 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
6529 pragma Assert (Is_Floating_Point_Type (Typ));
6530 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
6533 -- Mixed-mode operations can appear in a non-static universal context,
6534 -- in which case the integer argument must be converted explicitly.
6536 elsif Typ = Universal_Real
6537 and then Is_Integer_Type (Rtyp)
6539 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
6541 Analyze_And_Resolve (Rop, Universal_Real);
6543 elsif Typ = Universal_Real
6544 and then Is_Integer_Type (Ltyp)
6546 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
6548 Analyze_And_Resolve (Lop, Universal_Real);
6550 -- Non-fixed point cases, check software overflow checking required
6552 elsif Is_Signed_Integer_Type (Etype (N)) then
6553 Apply_Arithmetic_Overflow_Check (N);
6555 -- Deal with VAX float case
6557 elsif Vax_Float (Typ) then
6558 Expand_Vax_Arith (N);
6561 end Expand_N_Op_Multiply;
6563 --------------------
6564 -- Expand_N_Op_Ne --
6565 --------------------
6567 procedure Expand_N_Op_Ne (N : Node_Id) is
6568 Typ : constant Entity_Id := Etype (Left_Opnd (N));
6571 -- Case of elementary type with standard operator
6573 if Is_Elementary_Type (Typ)
6574 and then Sloc (Entity (N)) = Standard_Location
6576 Binary_Op_Validity_Checks (N);
6578 -- Boolean types (requiring handling of non-standard case)
6580 if Is_Boolean_Type (Typ) then
6581 Adjust_Condition (Left_Opnd (N));
6582 Adjust_Condition (Right_Opnd (N));
6583 Set_Etype (N, Standard_Boolean);
6584 Adjust_Result_Type (N, Typ);
6587 Rewrite_Comparison (N);
6589 -- If we still have comparison for Vax_Float, process it
6591 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
6592 Expand_Vax_Comparison (N);
6596 -- For all cases other than elementary types, we rewrite node as the
6597 -- negation of an equality operation, and reanalyze. The equality to be
6598 -- used is defined in the same scope and has the same signature. This
6599 -- signature must be set explicitly since in an instance it may not have
6600 -- the same visibility as in the generic unit. This avoids duplicating
6601 -- or factoring the complex code for record/array equality tests etc.
6605 Loc : constant Source_Ptr := Sloc (N);
6607 Ne : constant Entity_Id := Entity (N);
6610 Binary_Op_Validity_Checks (N);
6616 Left_Opnd => Left_Opnd (N),
6617 Right_Opnd => Right_Opnd (N)));
6618 Set_Paren_Count (Right_Opnd (Neg), 1);
6620 if Scope (Ne) /= Standard_Standard then
6621 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
6624 -- For navigation purposes, the inequality is treated as an
6625 -- implicit reference to the corresponding equality. Preserve the
6626 -- Comes_From_ source flag so that the proper Xref entry is
6629 Preserve_Comes_From_Source (Neg, N);
6630 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
6632 Analyze_And_Resolve (N, Standard_Boolean);
6637 ---------------------
6638 -- Expand_N_Op_Not --
6639 ---------------------
6641 -- If the argument is other than a Boolean array type, there is no special
6642 -- expansion required.
6644 -- For the packed case, we call the special routine in Exp_Pakd, except
6645 -- that if the component size is greater than one, we use the standard
6646 -- routine generating a gruesome loop (it is so peculiar to have packed
6647 -- arrays with non-standard Boolean representations anyway, so it does not
6648 -- matter that we do not handle this case efficiently).
6650 -- For the unpacked case (and for the special packed case where we have non
6651 -- standard Booleans, as discussed above), we generate and insert into the
6652 -- tree the following function definition:
6654 -- function Nnnn (A : arr) is
6657 -- for J in a'range loop
6658 -- B (J) := not A (J);
6663 -- Here arr is the actual subtype of the parameter (and hence always
6664 -- constrained). Then we replace the not with a call to this function.
6666 procedure Expand_N_Op_Not (N : Node_Id) is
6667 Loc : constant Source_Ptr := Sloc (N);
6668 Typ : constant Entity_Id := Etype (N);
6677 Func_Name : Entity_Id;
6678 Loop_Statement : Node_Id;
6681 Unary_Op_Validity_Checks (N);
6683 -- For boolean operand, deal with non-standard booleans
6685 if Is_Boolean_Type (Typ) then
6686 Adjust_Condition (Right_Opnd (N));
6687 Set_Etype (N, Standard_Boolean);
6688 Adjust_Result_Type (N, Typ);
6692 -- Only array types need any other processing
6694 if not Is_Array_Type (Typ) then
6698 -- Case of array operand. If bit packed with a component size of 1,
6699 -- handle it in Exp_Pakd if the operand is known to be aligned.
6701 if Is_Bit_Packed_Array (Typ)
6702 and then Component_Size (Typ) = 1
6703 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
6705 Expand_Packed_Not (N);
6709 -- Case of array operand which is not bit-packed. If the context is
6710 -- a safe assignment, call in-place operation, If context is a larger
6711 -- boolean expression in the context of a safe assignment, expansion is
6712 -- done by enclosing operation.
6714 Opnd := Relocate_Node (Right_Opnd (N));
6715 Convert_To_Actual_Subtype (Opnd);
6716 Arr := Etype (Opnd);
6717 Ensure_Defined (Arr, N);
6718 Silly_Boolean_Array_Not_Test (N, Arr);
6720 if Nkind (Parent (N)) = N_Assignment_Statement then
6721 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
6722 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
6725 -- Special case the negation of a binary operation
6727 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
6728 and then Safe_In_Place_Array_Op
6729 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
6731 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
6735 elsif Nkind (Parent (N)) in N_Binary_Op
6736 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
6739 Op1 : constant Node_Id := Left_Opnd (Parent (N));
6740 Op2 : constant Node_Id := Right_Opnd (Parent (N));
6741 Lhs : constant Node_Id := Name (Parent (Parent (N)));
6744 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
6746 and then Nkind (Op2) = N_Op_Not
6748 -- (not A) op (not B) can be reduced to a single call
6753 and then Nkind (Parent (N)) = N_Op_Xor
6755 -- A xor (not B) can also be special-cased
6763 A := Make_Defining_Identifier (Loc, Name_uA);
6764 B := Make_Defining_Identifier (Loc, Name_uB);
6765 J := Make_Defining_Identifier (Loc, Name_uJ);
6768 Make_Indexed_Component (Loc,
6769 Prefix => New_Reference_To (A, Loc),
6770 Expressions => New_List (New_Reference_To (J, Loc)));
6773 Make_Indexed_Component (Loc,
6774 Prefix => New_Reference_To (B, Loc),
6775 Expressions => New_List (New_Reference_To (J, Loc)));
6778 Make_Implicit_Loop_Statement (N,
6779 Identifier => Empty,
6782 Make_Iteration_Scheme (Loc,
6783 Loop_Parameter_Specification =>
6784 Make_Loop_Parameter_Specification (Loc,
6785 Defining_Identifier => J,
6786 Discrete_Subtype_Definition =>
6787 Make_Attribute_Reference (Loc,
6788 Prefix => Make_Identifier (Loc, Chars (A)),
6789 Attribute_Name => Name_Range))),
6791 Statements => New_List (
6792 Make_Assignment_Statement (Loc,
6794 Expression => Make_Op_Not (Loc, A_J))));
6796 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('N'));
6797 Set_Is_Inlined (Func_Name);
6800 Make_Subprogram_Body (Loc,
6802 Make_Function_Specification (Loc,
6803 Defining_Unit_Name => Func_Name,
6804 Parameter_Specifications => New_List (
6805 Make_Parameter_Specification (Loc,
6806 Defining_Identifier => A,
6807 Parameter_Type => New_Reference_To (Typ, Loc))),
6808 Result_Definition => New_Reference_To (Typ, Loc)),
6810 Declarations => New_List (
6811 Make_Object_Declaration (Loc,
6812 Defining_Identifier => B,
6813 Object_Definition => New_Reference_To (Arr, Loc))),
6815 Handled_Statement_Sequence =>
6816 Make_Handled_Sequence_Of_Statements (Loc,
6817 Statements => New_List (
6819 Make_Simple_Return_Statement (Loc,
6821 Make_Identifier (Loc, Chars (B)))))));
6824 Make_Function_Call (Loc,
6825 Name => New_Reference_To (Func_Name, Loc),
6826 Parameter_Associations => New_List (Opnd)));
6828 Analyze_And_Resolve (N, Typ);
6829 end Expand_N_Op_Not;
6831 --------------------
6832 -- Expand_N_Op_Or --
6833 --------------------
6835 procedure Expand_N_Op_Or (N : Node_Id) is
6836 Typ : constant Entity_Id := Etype (N);
6839 Binary_Op_Validity_Checks (N);
6841 if Is_Array_Type (Etype (N)) then
6842 Expand_Boolean_Operator (N);
6844 elsif Is_Boolean_Type (Etype (N)) then
6845 Adjust_Condition (Left_Opnd (N));
6846 Adjust_Condition (Right_Opnd (N));
6847 Set_Etype (N, Standard_Boolean);
6848 Adjust_Result_Type (N, Typ);
6852 ----------------------
6853 -- Expand_N_Op_Plus --
6854 ----------------------
6856 procedure Expand_N_Op_Plus (N : Node_Id) is
6858 Unary_Op_Validity_Checks (N);
6859 end Expand_N_Op_Plus;
6861 ---------------------
6862 -- Expand_N_Op_Rem --
6863 ---------------------
6865 procedure Expand_N_Op_Rem (N : Node_Id) is
6866 Loc : constant Source_Ptr := Sloc (N);
6867 Typ : constant Entity_Id := Etype (N);
6869 Left : constant Node_Id := Left_Opnd (N);
6870 Right : constant Node_Id := Right_Opnd (N);
6880 pragma Warnings (Off, Lhi);
6883 Binary_Op_Validity_Checks (N);
6885 if Is_Integer_Type (Etype (N)) then
6886 Apply_Divide_Check (N);
6889 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
6890 -- but it is useful with other back ends (e.g. AAMP), and is certainly
6893 if Is_Integer_Type (Etype (N))
6894 and then Compile_Time_Known_Value (Right)
6895 and then Expr_Value (Right) = Uint_1
6897 -- Call Remove_Side_Effects to ensure that any side effects in the
6898 -- ignored left operand (in particular function calls to user defined
6899 -- functions) are properly executed.
6901 Remove_Side_Effects (Left);
6903 Rewrite (N, Make_Integer_Literal (Loc, 0));
6904 Analyze_And_Resolve (N, Typ);
6908 -- Deal with annoying case of largest negative number remainder minus
6909 -- one. Gigi does not handle this case correctly, because it generates
6910 -- a divide instruction which may trap in this case.
6912 -- In fact the check is quite easy, if the right operand is -1, then
6913 -- the remainder is always 0, and we can just ignore the left operand
6914 -- completely in this case.
6916 Determine_Range (Right, ROK, Rlo, Rhi);
6917 Determine_Range (Left, LOK, Llo, Lhi);
6919 -- The operand type may be private (e.g. in the expansion of an
6920 -- intrinsic operation) so we must use the underlying type to get the
6921 -- bounds, and convert the literals explicitly.
6925 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
6927 -- Now perform the test, generating code only if needed
6929 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
6931 ((not LOK) or else (Llo = LLB))
6934 Make_Conditional_Expression (Loc,
6935 Expressions => New_List (
6937 Left_Opnd => Duplicate_Subexpr (Right),
6939 Unchecked_Convert_To (Typ,
6940 Make_Integer_Literal (Loc, -1))),
6942 Unchecked_Convert_To (Typ,
6943 Make_Integer_Literal (Loc, Uint_0)),
6945 Relocate_Node (N))));
6947 Set_Analyzed (Next (Next (First (Expressions (N)))));
6948 Analyze_And_Resolve (N, Typ);
6950 end Expand_N_Op_Rem;
6952 -----------------------------
6953 -- Expand_N_Op_Rotate_Left --
6954 -----------------------------
6956 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
6958 Binary_Op_Validity_Checks (N);
6959 end Expand_N_Op_Rotate_Left;
6961 ------------------------------
6962 -- Expand_N_Op_Rotate_Right --
6963 ------------------------------
6965 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
6967 Binary_Op_Validity_Checks (N);
6968 end Expand_N_Op_Rotate_Right;
6970 ----------------------------
6971 -- Expand_N_Op_Shift_Left --
6972 ----------------------------
6974 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
6976 Binary_Op_Validity_Checks (N);
6977 end Expand_N_Op_Shift_Left;
6979 -----------------------------
6980 -- Expand_N_Op_Shift_Right --
6981 -----------------------------
6983 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
6985 Binary_Op_Validity_Checks (N);
6986 end Expand_N_Op_Shift_Right;
6988 ----------------------------------------
6989 -- Expand_N_Op_Shift_Right_Arithmetic --
6990 ----------------------------------------
6992 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
6994 Binary_Op_Validity_Checks (N);
6995 end Expand_N_Op_Shift_Right_Arithmetic;
6997 --------------------------
6998 -- Expand_N_Op_Subtract --
6999 --------------------------
7001 procedure Expand_N_Op_Subtract (N : Node_Id) is
7002 Typ : constant Entity_Id := Etype (N);
7005 Binary_Op_Validity_Checks (N);
7007 -- N - 0 = N for integer types
7009 if Is_Integer_Type (Typ)
7010 and then Compile_Time_Known_Value (Right_Opnd (N))
7011 and then Expr_Value (Right_Opnd (N)) = 0
7013 Rewrite (N, Left_Opnd (N));
7017 -- Arithmetic overflow checks for signed integer/fixed point types
7019 if Is_Signed_Integer_Type (Typ)
7020 or else Is_Fixed_Point_Type (Typ)
7022 Apply_Arithmetic_Overflow_Check (N);
7024 -- Vax floating-point types case
7026 elsif Vax_Float (Typ) then
7027 Expand_Vax_Arith (N);
7029 end Expand_N_Op_Subtract;
7031 ---------------------
7032 -- Expand_N_Op_Xor --
7033 ---------------------
7035 procedure Expand_N_Op_Xor (N : Node_Id) is
7036 Typ : constant Entity_Id := Etype (N);
7039 Binary_Op_Validity_Checks (N);
7041 if Is_Array_Type (Etype (N)) then
7042 Expand_Boolean_Operator (N);
7044 elsif Is_Boolean_Type (Etype (N)) then
7045 Adjust_Condition (Left_Opnd (N));
7046 Adjust_Condition (Right_Opnd (N));
7047 Set_Etype (N, Standard_Boolean);
7048 Adjust_Result_Type (N, Typ);
7050 end Expand_N_Op_Xor;
7052 ----------------------
7053 -- Expand_N_Or_Else --
7054 ----------------------
7056 -- Expand into conditional expression if Actions present, and also
7057 -- deal with optimizing case of arguments being True or False.
7059 procedure Expand_N_Or_Else (N : Node_Id) is
7060 Loc : constant Source_Ptr := Sloc (N);
7061 Typ : constant Entity_Id := Etype (N);
7062 Left : constant Node_Id := Left_Opnd (N);
7063 Right : constant Node_Id := Right_Opnd (N);
7067 -- Deal with non-standard booleans
7069 if Is_Boolean_Type (Typ) then
7070 Adjust_Condition (Left);
7071 Adjust_Condition (Right);
7072 Set_Etype (N, Standard_Boolean);
7075 -- Check for cases where left argument is known to be True or False
7077 if Compile_Time_Known_Value (Left) then
7079 -- If left argument is False, change (False or else Right) to Right.
7080 -- Any actions associated with Right will be executed unconditionally
7081 -- and can thus be inserted into the tree unconditionally.
7083 if Expr_Value_E (Left) = Standard_False then
7084 if Present (Actions (N)) then
7085 Insert_Actions (N, Actions (N));
7090 -- If left argument is True, change (True and then Right) to True. In
7091 -- this case we can forget the actions associated with Right, since
7092 -- they will never be executed.
7094 else pragma Assert (Expr_Value_E (Left) = Standard_True);
7095 Kill_Dead_Code (Right);
7096 Kill_Dead_Code (Actions (N));
7097 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
7100 Adjust_Result_Type (N, Typ);
7104 -- If Actions are present, we expand
7106 -- left or else right
7110 -- if left then True else right end
7112 -- with the actions becoming the Else_Actions of the conditional
7113 -- expression. This conditional expression is then further expanded
7114 -- (and will eventually disappear)
7116 if Present (Actions (N)) then
7117 Actlist := Actions (N);
7119 Make_Conditional_Expression (Loc,
7120 Expressions => New_List (
7122 New_Occurrence_Of (Standard_True, Loc),
7125 Set_Else_Actions (N, Actlist);
7126 Analyze_And_Resolve (N, Standard_Boolean);
7127 Adjust_Result_Type (N, Typ);
7131 -- No actions present, check for cases of right argument True/False
7133 if Compile_Time_Known_Value (Right) then
7135 -- Change (Left or else False) to Left. Note that we know there are
7136 -- no actions associated with the True operand, since we just checked
7137 -- for this case above.
7139 if Expr_Value_E (Right) = Standard_False then
7142 -- Change (Left or else True) to True, making sure to preserve any
7143 -- side effects associated with the Left operand.
7145 else pragma Assert (Expr_Value_E (Right) = Standard_True);
7146 Remove_Side_Effects (Left);
7148 (N, New_Occurrence_Of (Standard_True, Loc));
7152 Adjust_Result_Type (N, Typ);
7153 end Expand_N_Or_Else;
7155 -----------------------------------
7156 -- Expand_N_Qualified_Expression --
7157 -----------------------------------
7159 procedure Expand_N_Qualified_Expression (N : Node_Id) is
7160 Operand : constant Node_Id := Expression (N);
7161 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
7164 -- Do validity check if validity checking operands
7166 if Validity_Checks_On
7167 and then Validity_Check_Operands
7169 Ensure_Valid (Operand);
7172 -- Apply possible constraint check
7174 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
7176 if Do_Range_Check (Operand) then
7177 Set_Do_Range_Check (Operand, False);
7178 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
7180 end Expand_N_Qualified_Expression;
7182 ---------------------------------
7183 -- Expand_N_Selected_Component --
7184 ---------------------------------
7186 -- If the selector is a discriminant of a concurrent object, rewrite the
7187 -- prefix to denote the corresponding record type.
7189 procedure Expand_N_Selected_Component (N : Node_Id) is
7190 Loc : constant Source_Ptr := Sloc (N);
7191 Par : constant Node_Id := Parent (N);
7192 P : constant Node_Id := Prefix (N);
7193 Ptyp : Entity_Id := Underlying_Type (Etype (P));
7198 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
7199 -- Gigi needs a temporary for prefixes that depend on a discriminant,
7200 -- unless the context of an assignment can provide size information.
7201 -- Don't we have a general routine that does this???
7203 -----------------------
7204 -- In_Left_Hand_Side --
7205 -----------------------
7207 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
7209 return (Nkind (Parent (Comp)) = N_Assignment_Statement
7210 and then Comp = Name (Parent (Comp)))
7211 or else (Present (Parent (Comp))
7212 and then Nkind (Parent (Comp)) in N_Subexpr
7213 and then In_Left_Hand_Side (Parent (Comp)));
7214 end In_Left_Hand_Side;
7216 -- Start of processing for Expand_N_Selected_Component
7219 -- Insert explicit dereference if required
7221 if Is_Access_Type (Ptyp) then
7222 Insert_Explicit_Dereference (P);
7223 Analyze_And_Resolve (P, Designated_Type (Ptyp));
7225 if Ekind (Etype (P)) = E_Private_Subtype
7226 and then Is_For_Access_Subtype (Etype (P))
7228 Set_Etype (P, Base_Type (Etype (P)));
7234 -- Deal with discriminant check required
7236 if Do_Discriminant_Check (N) then
7238 -- Present the discriminant checking function to the backend, so that
7239 -- it can inline the call to the function.
7242 (Discriminant_Checking_Func
7243 (Original_Record_Component (Entity (Selector_Name (N)))));
7245 -- Now reset the flag and generate the call
7247 Set_Do_Discriminant_Check (N, False);
7248 Generate_Discriminant_Check (N);
7251 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7252 -- function, then additional actuals must be passed.
7254 if Ada_Version >= Ada_05
7255 and then Is_Build_In_Place_Function_Call (P)
7257 Make_Build_In_Place_Call_In_Anonymous_Context (P);
7260 -- Gigi cannot handle unchecked conversions that are the prefix of a
7261 -- selected component with discriminants. This must be checked during
7262 -- expansion, because during analysis the type of the selector is not
7263 -- known at the point the prefix is analyzed. If the conversion is the
7264 -- target of an assignment, then we cannot force the evaluation.
7266 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
7267 and then Has_Discriminants (Etype (N))
7268 and then not In_Left_Hand_Side (N)
7270 Force_Evaluation (Prefix (N));
7273 -- Remaining processing applies only if selector is a discriminant
7275 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
7277 -- If the selector is a discriminant of a constrained record type,
7278 -- we may be able to rewrite the expression with the actual value
7279 -- of the discriminant, a useful optimization in some cases.
7281 if Is_Record_Type (Ptyp)
7282 and then Has_Discriminants (Ptyp)
7283 and then Is_Constrained (Ptyp)
7285 -- Do this optimization for discrete types only, and not for
7286 -- access types (access discriminants get us into trouble!)
7288 if not Is_Discrete_Type (Etype (N)) then
7291 -- Don't do this on the left hand of an assignment statement.
7292 -- Normally one would think that references like this would
7293 -- not occur, but they do in generated code, and mean that
7294 -- we really do want to assign the discriminant!
7296 elsif Nkind (Par) = N_Assignment_Statement
7297 and then Name (Par) = N
7301 -- Don't do this optimization for the prefix of an attribute or
7302 -- the operand of an object renaming declaration since these are
7303 -- contexts where we do not want the value anyway.
7305 elsif (Nkind (Par) = N_Attribute_Reference
7306 and then Prefix (Par) = N)
7307 or else Is_Renamed_Object (N)
7311 -- Don't do this optimization if we are within the code for a
7312 -- discriminant check, since the whole point of such a check may
7313 -- be to verify the condition on which the code below depends!
7315 elsif Is_In_Discriminant_Check (N) then
7318 -- Green light to see if we can do the optimization. There is
7319 -- still one condition that inhibits the optimization below but
7320 -- now is the time to check the particular discriminant.
7323 -- Loop through discriminants to find the matching discriminant
7324 -- constraint to see if we can copy it.
7326 Disc := First_Discriminant (Ptyp);
7327 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
7328 Discr_Loop : while Present (Dcon) loop
7330 -- Check if this is the matching discriminant
7332 if Disc = Entity (Selector_Name (N)) then
7334 -- Here we have the matching discriminant. Check for
7335 -- the case of a discriminant of a component that is
7336 -- constrained by an outer discriminant, which cannot
7337 -- be optimized away.
7340 Denotes_Discriminant
7341 (Node (Dcon), Check_Concurrent => True)
7345 -- In the context of a case statement, the expression may
7346 -- have the base type of the discriminant, and we need to
7347 -- preserve the constraint to avoid spurious errors on
7350 elsif Nkind (Parent (N)) = N_Case_Statement
7351 and then Etype (Node (Dcon)) /= Etype (Disc)
7354 Make_Qualified_Expression (Loc,
7356 New_Occurrence_Of (Etype (Disc), Loc),
7358 New_Copy_Tree (Node (Dcon))));
7359 Analyze_And_Resolve (N, Etype (Disc));
7361 -- In case that comes out as a static expression,
7362 -- reset it (a selected component is never static).
7364 Set_Is_Static_Expression (N, False);
7367 -- Otherwise we can just copy the constraint, but the
7368 -- result is certainly not static! In some cases the
7369 -- discriminant constraint has been analyzed in the
7370 -- context of the original subtype indication, but for
7371 -- itypes the constraint might not have been analyzed
7372 -- yet, and this must be done now.
7375 Rewrite (N, New_Copy_Tree (Node (Dcon)));
7376 Analyze_And_Resolve (N);
7377 Set_Is_Static_Expression (N, False);
7383 Next_Discriminant (Disc);
7384 end loop Discr_Loop;
7386 -- Note: the above loop should always find a matching
7387 -- discriminant, but if it does not, we just missed an
7388 -- optimization due to some glitch (perhaps a previous error),
7394 -- The only remaining processing is in the case of a discriminant of
7395 -- a concurrent object, where we rewrite the prefix to denote the
7396 -- corresponding record type. If the type is derived and has renamed
7397 -- discriminants, use corresponding discriminant, which is the one
7398 -- that appears in the corresponding record.
7400 if not Is_Concurrent_Type (Ptyp) then
7404 Disc := Entity (Selector_Name (N));
7406 if Is_Derived_Type (Ptyp)
7407 and then Present (Corresponding_Discriminant (Disc))
7409 Disc := Corresponding_Discriminant (Disc);
7413 Make_Selected_Component (Loc,
7415 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
7417 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
7422 end Expand_N_Selected_Component;
7424 --------------------
7425 -- Expand_N_Slice --
7426 --------------------
7428 procedure Expand_N_Slice (N : Node_Id) is
7429 Loc : constant Source_Ptr := Sloc (N);
7430 Typ : constant Entity_Id := Etype (N);
7431 Pfx : constant Node_Id := Prefix (N);
7432 Ptp : Entity_Id := Etype (Pfx);
7434 function Is_Procedure_Actual (N : Node_Id) return Boolean;
7435 -- Check whether the argument is an actual for a procedure call, in
7436 -- which case the expansion of a bit-packed slice is deferred until the
7437 -- call itself is expanded. The reason this is required is that we might
7438 -- have an IN OUT or OUT parameter, and the copy out is essential, and
7439 -- that copy out would be missed if we created a temporary here in
7440 -- Expand_N_Slice. Note that we don't bother to test specifically for an
7441 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
7442 -- is harmless to defer expansion in the IN case, since the call
7443 -- processing will still generate the appropriate copy in operation,
7444 -- which will take care of the slice.
7446 procedure Make_Temporary;
7447 -- Create a named variable for the value of the slice, in cases where
7448 -- the back-end cannot handle it properly, e.g. when packed types or
7449 -- unaligned slices are involved.
7451 -------------------------
7452 -- Is_Procedure_Actual --
7453 -------------------------
7455 function Is_Procedure_Actual (N : Node_Id) return Boolean is
7456 Par : Node_Id := Parent (N);
7460 -- If our parent is a procedure call we can return
7462 if Nkind (Par) = N_Procedure_Call_Statement then
7465 -- If our parent is a type conversion, keep climbing the tree,
7466 -- since a type conversion can be a procedure actual. Also keep
7467 -- climbing if parameter association or a qualified expression,
7468 -- since these are additional cases that do can appear on
7469 -- procedure actuals.
7471 elsif Nkind_In (Par, N_Type_Conversion,
7472 N_Parameter_Association,
7473 N_Qualified_Expression)
7475 Par := Parent (Par);
7477 -- Any other case is not what we are looking for
7483 end Is_Procedure_Actual;
7485 --------------------
7486 -- Make_Temporary --
7487 --------------------
7489 procedure Make_Temporary is
7491 Ent : constant Entity_Id :=
7492 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
7495 Make_Object_Declaration (Loc,
7496 Defining_Identifier => Ent,
7497 Object_Definition => New_Occurrence_Of (Typ, Loc));
7499 Set_No_Initialization (Decl);
7501 Insert_Actions (N, New_List (
7503 Make_Assignment_Statement (Loc,
7504 Name => New_Occurrence_Of (Ent, Loc),
7505 Expression => Relocate_Node (N))));
7507 Rewrite (N, New_Occurrence_Of (Ent, Loc));
7508 Analyze_And_Resolve (N, Typ);
7511 -- Start of processing for Expand_N_Slice
7514 -- Special handling for access types
7516 if Is_Access_Type (Ptp) then
7518 Ptp := Designated_Type (Ptp);
7521 Make_Explicit_Dereference (Sloc (N),
7522 Prefix => Relocate_Node (Pfx)));
7524 Analyze_And_Resolve (Pfx, Ptp);
7527 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7528 -- function, then additional actuals must be passed.
7530 if Ada_Version >= Ada_05
7531 and then Is_Build_In_Place_Function_Call (Pfx)
7533 Make_Build_In_Place_Call_In_Anonymous_Context (Pfx);
7536 -- The remaining case to be handled is packed slices. We can leave
7537 -- packed slices as they are in the following situations:
7539 -- 1. Right or left side of an assignment (we can handle this
7540 -- situation correctly in the assignment statement expansion).
7542 -- 2. Prefix of indexed component (the slide is optimized away in this
7543 -- case, see the start of Expand_N_Slice.)
7545 -- 3. Object renaming declaration, since we want the name of the
7546 -- slice, not the value.
7548 -- 4. Argument to procedure call, since copy-in/copy-out handling may
7549 -- be required, and this is handled in the expansion of call
7552 -- 5. Prefix of an address attribute (this is an error which is caught
7553 -- elsewhere, and the expansion would interfere with generating the
7556 if not Is_Packed (Typ) then
7558 -- Apply transformation for actuals of a function call, where
7559 -- Expand_Actuals is not used.
7561 if Nkind (Parent (N)) = N_Function_Call
7562 and then Is_Possibly_Unaligned_Slice (N)
7567 elsif Nkind (Parent (N)) = N_Assignment_Statement
7568 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
7569 and then Parent (N) = Name (Parent (Parent (N))))
7573 elsif Nkind (Parent (N)) = N_Indexed_Component
7574 or else Is_Renamed_Object (N)
7575 or else Is_Procedure_Actual (N)
7579 elsif Nkind (Parent (N)) = N_Attribute_Reference
7580 and then Attribute_Name (Parent (N)) = Name_Address
7589 ------------------------------
7590 -- Expand_N_Type_Conversion --
7591 ------------------------------
7593 procedure Expand_N_Type_Conversion (N : Node_Id) is
7594 Loc : constant Source_Ptr := Sloc (N);
7595 Operand : constant Node_Id := Expression (N);
7596 Target_Type : constant Entity_Id := Etype (N);
7597 Operand_Type : Entity_Id := Etype (Operand);
7599 procedure Handle_Changed_Representation;
7600 -- This is called in the case of record and array type conversions to
7601 -- see if there is a change of representation to be handled. Change of
7602 -- representation is actually handled at the assignment statement level,
7603 -- and what this procedure does is rewrite node N conversion as an
7604 -- assignment to temporary. If there is no change of representation,
7605 -- then the conversion node is unchanged.
7607 procedure Raise_Accessibility_Error;
7608 -- Called when we know that an accessibility check will fail. Rewrites
7609 -- node N to an appropriate raise statement and outputs warning msgs.
7610 -- The Etype of the raise node is set to Target_Type.
7612 procedure Real_Range_Check;
7613 -- Handles generation of range check for real target value
7615 -----------------------------------
7616 -- Handle_Changed_Representation --
7617 -----------------------------------
7619 procedure Handle_Changed_Representation is
7628 -- Nothing else to do if no change of representation
7630 if Same_Representation (Operand_Type, Target_Type) then
7633 -- The real change of representation work is done by the assignment
7634 -- statement processing. So if this type conversion is appearing as
7635 -- the expression of an assignment statement, nothing needs to be
7636 -- done to the conversion.
7638 elsif Nkind (Parent (N)) = N_Assignment_Statement then
7641 -- Otherwise we need to generate a temporary variable, and do the
7642 -- change of representation assignment into that temporary variable.
7643 -- The conversion is then replaced by a reference to this variable.
7648 -- If type is unconstrained we have to add a constraint, copied
7649 -- from the actual value of the left hand side.
7651 if not Is_Constrained (Target_Type) then
7652 if Has_Discriminants (Operand_Type) then
7653 Disc := First_Discriminant (Operand_Type);
7655 if Disc /= First_Stored_Discriminant (Operand_Type) then
7656 Disc := First_Stored_Discriminant (Operand_Type);
7660 while Present (Disc) loop
7662 Make_Selected_Component (Loc,
7663 Prefix => Duplicate_Subexpr_Move_Checks (Operand),
7665 Make_Identifier (Loc, Chars (Disc))));
7666 Next_Discriminant (Disc);
7669 elsif Is_Array_Type (Operand_Type) then
7670 N_Ix := First_Index (Target_Type);
7673 for J in 1 .. Number_Dimensions (Operand_Type) loop
7675 -- We convert the bounds explicitly. We use an unchecked
7676 -- conversion because bounds checks are done elsewhere.
7681 Unchecked_Convert_To (Etype (N_Ix),
7682 Make_Attribute_Reference (Loc,
7684 Duplicate_Subexpr_No_Checks
7685 (Operand, Name_Req => True),
7686 Attribute_Name => Name_First,
7687 Expressions => New_List (
7688 Make_Integer_Literal (Loc, J)))),
7691 Unchecked_Convert_To (Etype (N_Ix),
7692 Make_Attribute_Reference (Loc,
7694 Duplicate_Subexpr_No_Checks
7695 (Operand, Name_Req => True),
7696 Attribute_Name => Name_Last,
7697 Expressions => New_List (
7698 Make_Integer_Literal (Loc, J))))));
7705 Odef := New_Occurrence_Of (Target_Type, Loc);
7707 if Present (Cons) then
7709 Make_Subtype_Indication (Loc,
7710 Subtype_Mark => Odef,
7712 Make_Index_Or_Discriminant_Constraint (Loc,
7713 Constraints => Cons));
7716 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
7718 Make_Object_Declaration (Loc,
7719 Defining_Identifier => Temp,
7720 Object_Definition => Odef);
7722 Set_No_Initialization (Decl, True);
7724 -- Insert required actions. It is essential to suppress checks
7725 -- since we have suppressed default initialization, which means
7726 -- that the variable we create may have no discriminants.
7731 Make_Assignment_Statement (Loc,
7732 Name => New_Occurrence_Of (Temp, Loc),
7733 Expression => Relocate_Node (N))),
7734 Suppress => All_Checks);
7736 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7739 end Handle_Changed_Representation;
7741 -------------------------------
7742 -- Raise_Accessibility_Error --
7743 -------------------------------
7745 procedure Raise_Accessibility_Error is
7748 Make_Raise_Program_Error (Sloc (N),
7749 Reason => PE_Accessibility_Check_Failed));
7750 Set_Etype (N, Target_Type);
7752 Error_Msg_N ("?accessibility check failure", N);
7754 ("\?& will be raised at run time", N, Standard_Program_Error);
7755 end Raise_Accessibility_Error;
7757 ----------------------
7758 -- Real_Range_Check --
7759 ----------------------
7761 -- Case of conversions to floating-point or fixed-point. If range checks
7762 -- are enabled and the target type has a range constraint, we convert:
7768 -- Tnn : typ'Base := typ'Base (x);
7769 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
7772 -- This is necessary when there is a conversion of integer to float or
7773 -- to fixed-point to ensure that the correct checks are made. It is not
7774 -- necessary for float to float where it is enough to simply set the
7775 -- Do_Range_Check flag.
7777 procedure Real_Range_Check is
7778 Btyp : constant Entity_Id := Base_Type (Target_Type);
7779 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
7780 Hi : constant Node_Id := Type_High_Bound (Target_Type);
7781 Xtyp : constant Entity_Id := Etype (Operand);
7786 -- Nothing to do if conversion was rewritten
7788 if Nkind (N) /= N_Type_Conversion then
7792 -- Nothing to do if range checks suppressed, or target has the same
7793 -- range as the base type (or is the base type).
7795 if Range_Checks_Suppressed (Target_Type)
7796 or else (Lo = Type_Low_Bound (Btyp)
7798 Hi = Type_High_Bound (Btyp))
7803 -- Nothing to do if expression is an entity on which checks have been
7806 if Is_Entity_Name (Operand)
7807 and then Range_Checks_Suppressed (Entity (Operand))
7812 -- Nothing to do if bounds are all static and we can tell that the
7813 -- expression is within the bounds of the target. Note that if the
7814 -- operand is of an unconstrained floating-point type, then we do
7815 -- not trust it to be in range (might be infinite)
7818 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
7819 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
7822 if (not Is_Floating_Point_Type (Xtyp)
7823 or else Is_Constrained (Xtyp))
7824 and then Compile_Time_Known_Value (S_Lo)
7825 and then Compile_Time_Known_Value (S_Hi)
7826 and then Compile_Time_Known_Value (Hi)
7827 and then Compile_Time_Known_Value (Lo)
7830 D_Lov : constant Ureal := Expr_Value_R (Lo);
7831 D_Hiv : constant Ureal := Expr_Value_R (Hi);
7836 if Is_Real_Type (Xtyp) then
7837 S_Lov := Expr_Value_R (S_Lo);
7838 S_Hiv := Expr_Value_R (S_Hi);
7840 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
7841 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
7845 and then S_Lov >= D_Lov
7846 and then S_Hiv <= D_Hiv
7848 Set_Do_Range_Check (Operand, False);
7855 -- For float to float conversions, we are done
7857 if Is_Floating_Point_Type (Xtyp)
7859 Is_Floating_Point_Type (Btyp)
7864 -- Otherwise rewrite the conversion as described above
7866 Conv := Relocate_Node (N);
7868 (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
7869 Set_Etype (Conv, Btyp);
7871 -- Enable overflow except for case of integer to float conversions,
7872 -- where it is never required, since we can never have overflow in
7875 if not Is_Integer_Type (Etype (Operand)) then
7876 Enable_Overflow_Check (Conv);
7880 Make_Defining_Identifier (Loc,
7881 Chars => New_Internal_Name ('T'));
7883 Insert_Actions (N, New_List (
7884 Make_Object_Declaration (Loc,
7885 Defining_Identifier => Tnn,
7886 Object_Definition => New_Occurrence_Of (Btyp, Loc),
7887 Expression => Conv),
7889 Make_Raise_Constraint_Error (Loc,
7894 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
7896 Make_Attribute_Reference (Loc,
7897 Attribute_Name => Name_First,
7899 New_Occurrence_Of (Target_Type, Loc))),
7903 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
7905 Make_Attribute_Reference (Loc,
7906 Attribute_Name => Name_Last,
7908 New_Occurrence_Of (Target_Type, Loc)))),
7909 Reason => CE_Range_Check_Failed)));
7911 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
7912 Analyze_And_Resolve (N, Btyp);
7913 end Real_Range_Check;
7915 -- Start of processing for Expand_N_Type_Conversion
7918 -- Nothing at all to do if conversion is to the identical type so remove
7919 -- the conversion completely, it is useless.
7921 if Operand_Type = Target_Type then
7922 Rewrite (N, Relocate_Node (Operand));
7926 -- Nothing to do if this is the second argument of read. This is a
7927 -- "backwards" conversion that will be handled by the specialized code
7928 -- in attribute processing.
7930 if Nkind (Parent (N)) = N_Attribute_Reference
7931 and then Attribute_Name (Parent (N)) = Name_Read
7932 and then Next (First (Expressions (Parent (N)))) = N
7937 -- Here if we may need to expand conversion
7939 -- Do validity check if validity checking operands
7941 if Validity_Checks_On
7942 and then Validity_Check_Operands
7944 Ensure_Valid (Operand);
7947 -- Special case of converting from non-standard boolean type
7949 if Is_Boolean_Type (Operand_Type)
7950 and then (Nonzero_Is_True (Operand_Type))
7952 Adjust_Condition (Operand);
7953 Set_Etype (Operand, Standard_Boolean);
7954 Operand_Type := Standard_Boolean;
7957 -- Case of converting to an access type
7959 if Is_Access_Type (Target_Type) then
7961 -- Apply an accessibility check when the conversion operand is an
7962 -- access parameter (or a renaming thereof), unless conversion was
7963 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
7964 -- Note that other checks may still need to be applied below (such
7965 -- as tagged type checks).
7967 if Is_Entity_Name (Operand)
7969 (Is_Formal (Entity (Operand))
7971 (Present (Renamed_Object (Entity (Operand)))
7972 and then Is_Entity_Name (Renamed_Object (Entity (Operand)))
7974 (Entity (Renamed_Object (Entity (Operand))))))
7975 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
7976 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
7977 or else Attribute_Name (Original_Node (N)) = Name_Access)
7979 Apply_Accessibility_Check
7980 (Operand, Target_Type, Insert_Node => Operand);
7982 -- If the level of the operand type is statically deeper than the
7983 -- level of the target type, then force Program_Error. Note that this
7984 -- can only occur for cases where the attribute is within the body of
7985 -- an instantiation (otherwise the conversion will already have been
7986 -- rejected as illegal). Note: warnings are issued by the analyzer
7987 -- for the instance cases.
7989 elsif In_Instance_Body
7990 and then Type_Access_Level (Operand_Type) >
7991 Type_Access_Level (Target_Type)
7993 Raise_Accessibility_Error;
7995 -- When the operand is a selected access discriminant the check needs
7996 -- to be made against the level of the object denoted by the prefix
7997 -- of the selected name. Force Program_Error for this case as well
7998 -- (this accessibility violation can only happen if within the body
7999 -- of an instantiation).
8001 elsif In_Instance_Body
8002 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
8003 and then Nkind (Operand) = N_Selected_Component
8004 and then Object_Access_Level (Operand) >
8005 Type_Access_Level (Target_Type)
8007 Raise_Accessibility_Error;
8012 -- Case of conversions of tagged types and access to tagged types
8014 -- When needed, that is to say when the expression is class-wide, Add
8015 -- runtime a tag check for (strict) downward conversion by using the
8016 -- membership test, generating:
8018 -- [constraint_error when Operand not in Target_Type'Class]
8020 -- or in the access type case
8022 -- [constraint_error
8023 -- when Operand /= null
8024 -- and then Operand.all not in
8025 -- Designated_Type (Target_Type)'Class]
8027 if (Is_Access_Type (Target_Type)
8028 and then Is_Tagged_Type (Designated_Type (Target_Type)))
8029 or else Is_Tagged_Type (Target_Type)
8031 -- Do not do any expansion in the access type case if the parent is a
8032 -- renaming, since this is an error situation which will be caught by
8033 -- Sem_Ch8, and the expansion can interfere with this error check.
8035 if Is_Access_Type (Target_Type)
8036 and then Is_Renamed_Object (N)
8041 -- Otherwise, proceed with processing tagged conversion
8044 Actual_Op_Typ : Entity_Id;
8045 Actual_Targ_Typ : Entity_Id;
8046 Make_Conversion : Boolean := False;
8047 Root_Op_Typ : Entity_Id;
8049 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
8050 -- Create a membership check to test whether Operand is a member
8051 -- of Targ_Typ. If the original Target_Type is an access, include
8052 -- a test for null value. The check is inserted at N.
8054 --------------------
8055 -- Make_Tag_Check --
8056 --------------------
8058 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
8063 -- [Constraint_Error
8064 -- when Operand /= null
8065 -- and then Operand.all not in Targ_Typ]
8067 if Is_Access_Type (Target_Type) then
8072 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
8073 Right_Opnd => Make_Null (Loc)),
8078 Make_Explicit_Dereference (Loc,
8079 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
8080 Right_Opnd => New_Reference_To (Targ_Typ, Loc)));
8083 -- [Constraint_Error when Operand not in Targ_Typ]
8088 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
8089 Right_Opnd => New_Reference_To (Targ_Typ, Loc));
8093 Make_Raise_Constraint_Error (Loc,
8095 Reason => CE_Tag_Check_Failed));
8098 -- Start of processing
8101 if Is_Access_Type (Target_Type) then
8103 -- Handle entities from the limited view
8106 Available_View (Designated_Type (Operand_Type));
8108 Available_View (Designated_Type (Target_Type));
8110 Actual_Op_Typ := Operand_Type;
8111 Actual_Targ_Typ := Target_Type;
8114 Root_Op_Typ := Root_Type (Actual_Op_Typ);
8116 -- Ada 2005 (AI-251): Handle interface type conversion
8118 if Is_Interface (Actual_Op_Typ) then
8119 Expand_Interface_Conversion (N, Is_Static => False);
8123 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
8125 -- Create a runtime tag check for a downward class-wide type
8128 if Is_Class_Wide_Type (Actual_Op_Typ)
8129 and then Actual_Op_Typ /= Actual_Targ_Typ
8130 and then Root_Op_Typ /= Actual_Targ_Typ
8131 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ)
8133 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
8134 Make_Conversion := True;
8137 -- AI05-0073: If the result subtype of the function is defined
8138 -- by an access_definition designating a specific tagged type
8139 -- T, a check is made that the result value is null or the tag
8140 -- of the object designated by the result value identifies T.
8141 -- Constraint_Error is raised if this check fails.
8143 if Nkind (Parent (N)) = Sinfo.N_Return_Statement then
8146 Func_Typ : Entity_Id;
8149 -- Climb scope stack looking for the enclosing function
8151 Func := Current_Scope;
8152 while Present (Func)
8153 and then Ekind (Func) /= E_Function
8155 Func := Scope (Func);
8158 -- The function's return subtype must be defined using
8159 -- an access definition.
8161 if Nkind (Result_Definition (Parent (Func))) =
8164 Func_Typ := Directly_Designated_Type (Etype (Func));
8166 -- The return subtype denotes a specific tagged type,
8167 -- in other words, a non class-wide type.
8169 if Is_Tagged_Type (Func_Typ)
8170 and then not Is_Class_Wide_Type (Func_Typ)
8172 Make_Tag_Check (Actual_Targ_Typ);
8173 Make_Conversion := True;
8179 -- We have generated a tag check for either a class-wide type
8180 -- conversion or for AI05-0073.
8182 if Make_Conversion then
8187 Make_Unchecked_Type_Conversion (Loc,
8188 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
8189 Expression => Relocate_Node (Expression (N)));
8191 Analyze_And_Resolve (N, Target_Type);
8197 -- Case of other access type conversions
8199 elsif Is_Access_Type (Target_Type) then
8200 Apply_Constraint_Check (Operand, Target_Type);
8202 -- Case of conversions from a fixed-point type
8204 -- These conversions require special expansion and processing, found in
8205 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
8206 -- since from a semantic point of view, these are simple integer
8207 -- conversions, which do not need further processing.
8209 elsif Is_Fixed_Point_Type (Operand_Type)
8210 and then not Conversion_OK (N)
8212 -- We should never see universal fixed at this case, since the
8213 -- expansion of the constituent divide or multiply should have
8214 -- eliminated the explicit mention of universal fixed.
8216 pragma Assert (Operand_Type /= Universal_Fixed);
8218 -- Check for special case of the conversion to universal real that
8219 -- occurs as a result of the use of a round attribute. In this case,
8220 -- the real type for the conversion is taken from the target type of
8221 -- the Round attribute and the result must be marked as rounded.
8223 if Target_Type = Universal_Real
8224 and then Nkind (Parent (N)) = N_Attribute_Reference
8225 and then Attribute_Name (Parent (N)) = Name_Round
8227 Set_Rounded_Result (N);
8228 Set_Etype (N, Etype (Parent (N)));
8231 -- Otherwise do correct fixed-conversion, but skip these if the
8232 -- Conversion_OK flag is set, because from a semantic point of
8233 -- view these are simple integer conversions needing no further
8234 -- processing (the backend will simply treat them as integers)
8236 if not Conversion_OK (N) then
8237 if Is_Fixed_Point_Type (Etype (N)) then
8238 Expand_Convert_Fixed_To_Fixed (N);
8241 elsif Is_Integer_Type (Etype (N)) then
8242 Expand_Convert_Fixed_To_Integer (N);
8245 pragma Assert (Is_Floating_Point_Type (Etype (N)));
8246 Expand_Convert_Fixed_To_Float (N);
8251 -- Case of conversions to a fixed-point type
8253 -- These conversions require special expansion and processing, found in
8254 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
8255 -- since from a semantic point of view, these are simple integer
8256 -- conversions, which do not need further processing.
8258 elsif Is_Fixed_Point_Type (Target_Type)
8259 and then not Conversion_OK (N)
8261 if Is_Integer_Type (Operand_Type) then
8262 Expand_Convert_Integer_To_Fixed (N);
8265 pragma Assert (Is_Floating_Point_Type (Operand_Type));
8266 Expand_Convert_Float_To_Fixed (N);
8270 -- Case of float-to-integer conversions
8272 -- We also handle float-to-fixed conversions with Conversion_OK set
8273 -- since semantically the fixed-point target is treated as though it
8274 -- were an integer in such cases.
8276 elsif Is_Floating_Point_Type (Operand_Type)
8278 (Is_Integer_Type (Target_Type)
8280 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
8282 -- One more check here, gcc is still not able to do conversions of
8283 -- this type with proper overflow checking, and so gigi is doing an
8284 -- approximation of what is required by doing floating-point compares
8285 -- with the end-point. But that can lose precision in some cases, and
8286 -- give a wrong result. Converting the operand to Universal_Real is
8287 -- helpful, but still does not catch all cases with 64-bit integers
8288 -- on targets with only 64-bit floats
8290 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
8291 -- Can this code be removed ???
8293 if Do_Range_Check (Operand) then
8295 Make_Type_Conversion (Loc,
8297 New_Occurrence_Of (Universal_Real, Loc),
8299 Relocate_Node (Operand)));
8301 Set_Etype (Operand, Universal_Real);
8302 Enable_Range_Check (Operand);
8303 Set_Do_Range_Check (Expression (Operand), False);
8306 -- Case of array conversions
8308 -- Expansion of array conversions, add required length/range checks but
8309 -- only do this if there is no change of representation. For handling of
8310 -- this case, see Handle_Changed_Representation.
8312 elsif Is_Array_Type (Target_Type) then
8314 if Is_Constrained (Target_Type) then
8315 Apply_Length_Check (Operand, Target_Type);
8317 Apply_Range_Check (Operand, Target_Type);
8320 Handle_Changed_Representation;
8322 -- Case of conversions of discriminated types
8324 -- Add required discriminant checks if target is constrained. Again this
8325 -- change is skipped if we have a change of representation.
8327 elsif Has_Discriminants (Target_Type)
8328 and then Is_Constrained (Target_Type)
8330 Apply_Discriminant_Check (Operand, Target_Type);
8331 Handle_Changed_Representation;
8333 -- Case of all other record conversions. The only processing required
8334 -- is to check for a change of representation requiring the special
8335 -- assignment processing.
8337 elsif Is_Record_Type (Target_Type) then
8339 -- Ada 2005 (AI-216): Program_Error is raised when converting from
8340 -- a derived Unchecked_Union type to an unconstrained type that is
8341 -- not Unchecked_Union if the operand lacks inferable discriminants.
8343 if Is_Derived_Type (Operand_Type)
8344 and then Is_Unchecked_Union (Base_Type (Operand_Type))
8345 and then not Is_Constrained (Target_Type)
8346 and then not Is_Unchecked_Union (Base_Type (Target_Type))
8347 and then not Has_Inferable_Discriminants (Operand)
8349 -- To prevent Gigi from generating illegal code, we generate a
8350 -- Program_Error node, but we give it the target type of the
8354 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
8355 Reason => PE_Unchecked_Union_Restriction);
8358 Set_Etype (PE, Target_Type);
8363 Handle_Changed_Representation;
8366 -- Case of conversions of enumeration types
8368 elsif Is_Enumeration_Type (Target_Type) then
8370 -- Special processing is required if there is a change of
8371 -- representation (from enumeration representation clauses)
8373 if not Same_Representation (Target_Type, Operand_Type) then
8375 -- Convert: x(y) to x'val (ytyp'val (y))
8378 Make_Attribute_Reference (Loc,
8379 Prefix => New_Occurrence_Of (Target_Type, Loc),
8380 Attribute_Name => Name_Val,
8381 Expressions => New_List (
8382 Make_Attribute_Reference (Loc,
8383 Prefix => New_Occurrence_Of (Operand_Type, Loc),
8384 Attribute_Name => Name_Pos,
8385 Expressions => New_List (Operand)))));
8387 Analyze_And_Resolve (N, Target_Type);
8390 -- Case of conversions to floating-point
8392 elsif Is_Floating_Point_Type (Target_Type) then
8396 -- At this stage, either the conversion node has been transformed into
8397 -- some other equivalent expression, or left as a conversion that can
8398 -- be handled by Gigi. The conversions that Gigi can handle are the
8401 -- Conversions with no change of representation or type
8403 -- Numeric conversions involving integer, floating- and fixed-point
8404 -- values. Fixed-point values are allowed only if Conversion_OK is
8405 -- set, i.e. if the fixed-point values are to be treated as integers.
8407 -- No other conversions should be passed to Gigi
8409 -- Check: are these rules stated in sinfo??? if so, why restate here???
8411 -- The only remaining step is to generate a range check if we still have
8412 -- a type conversion at this stage and Do_Range_Check is set. For now we
8413 -- do this only for conversions of discrete types.
8415 if Nkind (N) = N_Type_Conversion
8416 and then Is_Discrete_Type (Etype (N))
8419 Expr : constant Node_Id := Expression (N);
8424 if Do_Range_Check (Expr)
8425 and then Is_Discrete_Type (Etype (Expr))
8427 Set_Do_Range_Check (Expr, False);
8429 -- Before we do a range check, we have to deal with treating a
8430 -- fixed-point operand as an integer. The way we do this is
8431 -- simply to do an unchecked conversion to an appropriate
8432 -- integer type large enough to hold the result.
8434 -- This code is not active yet, because we are only dealing
8435 -- with discrete types so far ???
8437 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
8438 and then Treat_Fixed_As_Integer (Expr)
8440 Ftyp := Base_Type (Etype (Expr));
8442 if Esize (Ftyp) >= Esize (Standard_Integer) then
8443 Ityp := Standard_Long_Long_Integer;
8445 Ityp := Standard_Integer;
8448 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
8451 -- Reset overflow flag, since the range check will include
8452 -- dealing with possible overflow, and generate the check If
8453 -- Address is either a source type or target type, suppress
8454 -- range check to avoid typing anomalies when it is a visible
8457 Set_Do_Overflow_Check (N, False);
8458 if not Is_Descendent_Of_Address (Etype (Expr))
8459 and then not Is_Descendent_Of_Address (Target_Type)
8461 Generate_Range_Check
8462 (Expr, Target_Type, CE_Range_Check_Failed);
8468 -- Final step, if the result is a type conversion involving Vax_Float
8469 -- types, then it is subject for further special processing.
8471 if Nkind (N) = N_Type_Conversion
8472 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
8474 Expand_Vax_Conversion (N);
8477 end Expand_N_Type_Conversion;
8479 -----------------------------------
8480 -- Expand_N_Unchecked_Expression --
8481 -----------------------------------
8483 -- Remove the unchecked expression node from the tree. It's job was simply
8484 -- to make sure that its constituent expression was handled with checks
8485 -- off, and now that that is done, we can remove it from the tree, and
8486 -- indeed must, since gigi does not expect to see these nodes.
8488 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
8489 Exp : constant Node_Id := Expression (N);
8492 Set_Assignment_OK (Exp, Assignment_OK (N) or Assignment_OK (Exp));
8494 end Expand_N_Unchecked_Expression;
8496 ----------------------------------------
8497 -- Expand_N_Unchecked_Type_Conversion --
8498 ----------------------------------------
8500 -- If this cannot be handled by Gigi and we haven't already made a
8501 -- temporary for it, do it now.
8503 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
8504 Target_Type : constant Entity_Id := Etype (N);
8505 Operand : constant Node_Id := Expression (N);
8506 Operand_Type : constant Entity_Id := Etype (Operand);
8509 -- If we have a conversion of a compile time known value to a target
8510 -- type and the value is in range of the target type, then we can simply
8511 -- replace the construct by an integer literal of the correct type. We
8512 -- only apply this to integer types being converted. Possibly it may
8513 -- apply in other cases, but it is too much trouble to worry about.
8515 -- Note that we do not do this transformation if the Kill_Range_Check
8516 -- flag is set, since then the value may be outside the expected range.
8517 -- This happens in the Normalize_Scalars case.
8519 -- We also skip this if either the target or operand type is biased
8520 -- because in this case, the unchecked conversion is supposed to
8521 -- preserve the bit pattern, not the integer value.
8523 if Is_Integer_Type (Target_Type)
8524 and then not Has_Biased_Representation (Target_Type)
8525 and then Is_Integer_Type (Operand_Type)
8526 and then not Has_Biased_Representation (Operand_Type)
8527 and then Compile_Time_Known_Value (Operand)
8528 and then not Kill_Range_Check (N)
8531 Val : constant Uint := Expr_Value (Operand);
8534 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
8536 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
8538 Val >= Expr_Value (Type_Low_Bound (Target_Type))
8540 Val <= Expr_Value (Type_High_Bound (Target_Type))
8542 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
8544 -- If Address is the target type, just set the type to avoid a
8545 -- spurious type error on the literal when Address is a visible
8548 if Is_Descendent_Of_Address (Target_Type) then
8549 Set_Etype (N, Target_Type);
8551 Analyze_And_Resolve (N, Target_Type);
8559 -- Nothing to do if conversion is safe
8561 if Safe_Unchecked_Type_Conversion (N) then
8565 -- Otherwise force evaluation unless Assignment_OK flag is set (this
8566 -- flag indicates ??? -- more comments needed here)
8568 if Assignment_OK (N) then
8571 Force_Evaluation (N);
8573 end Expand_N_Unchecked_Type_Conversion;
8575 ----------------------------
8576 -- Expand_Record_Equality --
8577 ----------------------------
8579 -- For non-variant records, Equality is expanded when needed into:
8581 -- and then Lhs.Discr1 = Rhs.Discr1
8583 -- and then Lhs.Discrn = Rhs.Discrn
8584 -- and then Lhs.Cmp1 = Rhs.Cmp1
8586 -- and then Lhs.Cmpn = Rhs.Cmpn
8588 -- The expression is folded by the back-end for adjacent fields. This
8589 -- function is called for tagged record in only one occasion: for imple-
8590 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
8591 -- otherwise the primitive "=" is used directly.
8593 function Expand_Record_Equality
8598 Bodies : List_Id) return Node_Id
8600 Loc : constant Source_Ptr := Sloc (Nod);
8605 First_Time : Boolean := True;
8607 function Suitable_Element (C : Entity_Id) return Entity_Id;
8608 -- Return the first field to compare beginning with C, skipping the
8609 -- inherited components.
8611 ----------------------
8612 -- Suitable_Element --
8613 ----------------------
8615 function Suitable_Element (C : Entity_Id) return Entity_Id is
8620 elsif Ekind (C) /= E_Discriminant
8621 and then Ekind (C) /= E_Component
8623 return Suitable_Element (Next_Entity (C));
8625 elsif Is_Tagged_Type (Typ)
8626 and then C /= Original_Record_Component (C)
8628 return Suitable_Element (Next_Entity (C));
8630 elsif Chars (C) = Name_uController
8631 or else Chars (C) = Name_uTag
8633 return Suitable_Element (Next_Entity (C));
8635 elsif Is_Interface (Etype (C)) then
8636 return Suitable_Element (Next_Entity (C));
8641 end Suitable_Element;
8643 -- Start of processing for Expand_Record_Equality
8646 -- Generates the following code: (assuming that Typ has one Discr and
8647 -- component C2 is also a record)
8650 -- and then Lhs.Discr1 = Rhs.Discr1
8651 -- and then Lhs.C1 = Rhs.C1
8652 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
8654 -- and then Lhs.Cmpn = Rhs.Cmpn
8656 Result := New_Reference_To (Standard_True, Loc);
8657 C := Suitable_Element (First_Entity (Typ));
8659 while Present (C) loop
8667 First_Time := False;
8671 New_Lhs := New_Copy_Tree (Lhs);
8672 New_Rhs := New_Copy_Tree (Rhs);
8676 Expand_Composite_Equality (Nod, Etype (C),
8678 Make_Selected_Component (Loc,
8680 Selector_Name => New_Reference_To (C, Loc)),
8682 Make_Selected_Component (Loc,
8684 Selector_Name => New_Reference_To (C, Loc)),
8687 -- If some (sub)component is an unchecked_union, the whole
8688 -- operation will raise program error.
8690 if Nkind (Check) = N_Raise_Program_Error then
8692 Set_Etype (Result, Standard_Boolean);
8697 Left_Opnd => Result,
8698 Right_Opnd => Check);
8702 C := Suitable_Element (Next_Entity (C));
8706 end Expand_Record_Equality;
8708 -------------------------------------
8709 -- Fixup_Universal_Fixed_Operation --
8710 -------------------------------------
8712 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
8713 Conv : constant Node_Id := Parent (N);
8716 -- We must have a type conversion immediately above us
8718 pragma Assert (Nkind (Conv) = N_Type_Conversion);
8720 -- Normally the type conversion gives our target type. The exception
8721 -- occurs in the case of the Round attribute, where the conversion
8722 -- will be to universal real, and our real type comes from the Round
8723 -- attribute (as well as an indication that we must round the result)
8725 if Nkind (Parent (Conv)) = N_Attribute_Reference
8726 and then Attribute_Name (Parent (Conv)) = Name_Round
8728 Set_Etype (N, Etype (Parent (Conv)));
8729 Set_Rounded_Result (N);
8731 -- Normal case where type comes from conversion above us
8734 Set_Etype (N, Etype (Conv));
8736 end Fixup_Universal_Fixed_Operation;
8738 ------------------------------
8739 -- Get_Allocator_Final_List --
8740 ------------------------------
8742 function Get_Allocator_Final_List
8745 PtrT : Entity_Id) return Entity_Id
8747 Loc : constant Source_Ptr := Sloc (N);
8749 Owner : Entity_Id := PtrT;
8750 -- The entity whose finalization list must be used to attach the
8751 -- allocated object.
8754 if Ekind (PtrT) = E_Anonymous_Access_Type then
8756 -- If the context is an access parameter, we need to create a
8757 -- non-anonymous access type in order to have a usable final list,
8758 -- because there is otherwise no pool to which the allocated object
8759 -- can belong. We create both the type and the finalization chain
8760 -- here, because freezing an internal type does not create such a
8761 -- chain. The Final_Chain that is thus created is shared by the
8762 -- access parameter. The access type is tested against the result
8763 -- type of the function to exclude allocators whose type is an
8764 -- anonymous access result type. We freeze the type at once to
8765 -- ensure that it is properly decorated for the back-end, even
8766 -- if the context and current scope is a loop.
8768 if Nkind (Associated_Node_For_Itype (PtrT))
8769 in N_Subprogram_Specification
8772 Etype (Defining_Unit_Name (Associated_Node_For_Itype (PtrT)))
8774 Owner := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
8776 Make_Full_Type_Declaration (Loc,
8777 Defining_Identifier => Owner,
8779 Make_Access_To_Object_Definition (Loc,
8780 Subtype_Indication =>
8781 New_Occurrence_Of (T, Loc))));
8783 Freeze_Before (N, Owner);
8784 Build_Final_List (N, Owner);
8785 Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner));
8787 -- Ada 2005 (AI-318-02): If the context is a return object
8788 -- declaration, then the anonymous return subtype is defined to have
8789 -- the same accessibility level as that of the function's result
8790 -- subtype, which means that we want the scope where the function is
8793 elsif Nkind (Associated_Node_For_Itype (PtrT)) = N_Object_Declaration
8794 and then Ekind (Scope (PtrT)) = E_Return_Statement
8796 Owner := Scope (Return_Applies_To (Scope (PtrT)));
8798 -- Case of an access discriminant, or (Ada 2005), of an anonymous
8799 -- access component or anonymous access function result: find the
8800 -- final list associated with the scope of the type. (In the
8801 -- anonymous access component kind, a list controller will have
8802 -- been allocated when freezing the record type, and PtrT has an
8803 -- Associated_Final_Chain attribute designating it.)
8805 elsif No (Associated_Final_Chain (PtrT)) then
8806 Owner := Scope (PtrT);
8810 return Find_Final_List (Owner);
8811 end Get_Allocator_Final_List;
8813 ---------------------------------
8814 -- Has_Inferable_Discriminants --
8815 ---------------------------------
8817 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
8819 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
8820 -- Determines whether the left-most prefix of a selected component is a
8821 -- formal parameter in a subprogram. Assumes N is a selected component.
8823 --------------------------------
8824 -- Prefix_Is_Formal_Parameter --
8825 --------------------------------
8827 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
8828 Sel_Comp : Node_Id := N;
8831 -- Move to the left-most prefix by climbing up the tree
8833 while Present (Parent (Sel_Comp))
8834 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
8836 Sel_Comp := Parent (Sel_Comp);
8839 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
8840 end Prefix_Is_Formal_Parameter;
8842 -- Start of processing for Has_Inferable_Discriminants
8845 -- For identifiers and indexed components, it is sufficient to have a
8846 -- constrained Unchecked_Union nominal subtype.
8848 if Nkind_In (N, N_Identifier, N_Indexed_Component) then
8849 return Is_Unchecked_Union (Base_Type (Etype (N)))
8851 Is_Constrained (Etype (N));
8853 -- For selected components, the subtype of the selector must be a
8854 -- constrained Unchecked_Union. If the component is subject to a
8855 -- per-object constraint, then the enclosing object must have inferable
8858 elsif Nkind (N) = N_Selected_Component then
8859 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
8861 -- A small hack. If we have a per-object constrained selected
8862 -- component of a formal parameter, return True since we do not
8863 -- know the actual parameter association yet.
8865 if Prefix_Is_Formal_Parameter (N) then
8869 -- Otherwise, check the enclosing object and the selector
8871 return Has_Inferable_Discriminants (Prefix (N))
8873 Has_Inferable_Discriminants (Selector_Name (N));
8876 -- The call to Has_Inferable_Discriminants will determine whether
8877 -- the selector has a constrained Unchecked_Union nominal type.
8879 return Has_Inferable_Discriminants (Selector_Name (N));
8881 -- A qualified expression has inferable discriminants if its subtype
8882 -- mark is a constrained Unchecked_Union subtype.
8884 elsif Nkind (N) = N_Qualified_Expression then
8885 return Is_Unchecked_Union (Subtype_Mark (N))
8887 Is_Constrained (Subtype_Mark (N));
8892 end Has_Inferable_Discriminants;
8894 -------------------------------
8895 -- Insert_Dereference_Action --
8896 -------------------------------
8898 procedure Insert_Dereference_Action (N : Node_Id) is
8899 Loc : constant Source_Ptr := Sloc (N);
8900 Typ : constant Entity_Id := Etype (N);
8901 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
8902 Pnod : constant Node_Id := Parent (N);
8904 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
8905 -- Return true if type of P is derived from Checked_Pool;
8907 -----------------------------
8908 -- Is_Checked_Storage_Pool --
8909 -----------------------------
8911 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
8920 while T /= Etype (T) loop
8921 if Is_RTE (T, RE_Checked_Pool) then
8929 end Is_Checked_Storage_Pool;
8931 -- Start of processing for Insert_Dereference_Action
8934 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
8936 if not (Is_Checked_Storage_Pool (Pool)
8937 and then Comes_From_Source (Original_Node (Pnod)))
8943 Make_Procedure_Call_Statement (Loc,
8944 Name => New_Reference_To (
8945 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
8947 Parameter_Associations => New_List (
8951 New_Reference_To (Pool, Loc),
8953 -- Storage_Address. We use the attribute Pool_Address, which uses
8954 -- the pointer itself to find the address of the object, and which
8955 -- handles unconstrained arrays properly by computing the address
8956 -- of the template. i.e. the correct address of the corresponding
8959 Make_Attribute_Reference (Loc,
8960 Prefix => Duplicate_Subexpr_Move_Checks (N),
8961 Attribute_Name => Name_Pool_Address),
8963 -- Size_In_Storage_Elements
8965 Make_Op_Divide (Loc,
8967 Make_Attribute_Reference (Loc,
8969 Make_Explicit_Dereference (Loc,
8970 Duplicate_Subexpr_Move_Checks (N)),
8971 Attribute_Name => Name_Size),
8973 Make_Integer_Literal (Loc, System_Storage_Unit)),
8977 Make_Attribute_Reference (Loc,
8979 Make_Explicit_Dereference (Loc,
8980 Duplicate_Subexpr_Move_Checks (N)),
8981 Attribute_Name => Name_Alignment))));
8984 when RE_Not_Available =>
8986 end Insert_Dereference_Action;
8988 ------------------------------
8989 -- Make_Array_Comparison_Op --
8990 ------------------------------
8992 -- This is a hand-coded expansion of the following generic function:
8995 -- type elem is (<>);
8996 -- type index is (<>);
8997 -- type a is array (index range <>) of elem;
8999 -- function Gnnn (X : a; Y: a) return boolean is
9000 -- J : index := Y'first;
9003 -- if X'length = 0 then
9006 -- elsif Y'length = 0 then
9010 -- for I in X'range loop
9011 -- if X (I) = Y (J) then
9012 -- if J = Y'last then
9015 -- J := index'succ (J);
9019 -- return X (I) > Y (J);
9023 -- return X'length > Y'length;
9027 -- Note that since we are essentially doing this expansion by hand, we
9028 -- do not need to generate an actual or formal generic part, just the
9029 -- instantiated function itself.
9031 function Make_Array_Comparison_Op
9033 Nod : Node_Id) return Node_Id
9035 Loc : constant Source_Ptr := Sloc (Nod);
9037 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
9038 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
9039 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
9040 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
9042 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
9044 Loop_Statement : Node_Id;
9045 Loop_Body : Node_Id;
9048 Final_Expr : Node_Id;
9049 Func_Body : Node_Id;
9050 Func_Name : Entity_Id;
9056 -- if J = Y'last then
9059 -- J := index'succ (J);
9063 Make_Implicit_If_Statement (Nod,
9066 Left_Opnd => New_Reference_To (J, Loc),
9068 Make_Attribute_Reference (Loc,
9069 Prefix => New_Reference_To (Y, Loc),
9070 Attribute_Name => Name_Last)),
9072 Then_Statements => New_List (
9073 Make_Exit_Statement (Loc)),
9077 Make_Assignment_Statement (Loc,
9078 Name => New_Reference_To (J, Loc),
9080 Make_Attribute_Reference (Loc,
9081 Prefix => New_Reference_To (Index, Loc),
9082 Attribute_Name => Name_Succ,
9083 Expressions => New_List (New_Reference_To (J, Loc))))));
9085 -- if X (I) = Y (J) then
9088 -- return X (I) > Y (J);
9092 Make_Implicit_If_Statement (Nod,
9096 Make_Indexed_Component (Loc,
9097 Prefix => New_Reference_To (X, Loc),
9098 Expressions => New_List (New_Reference_To (I, Loc))),
9101 Make_Indexed_Component (Loc,
9102 Prefix => New_Reference_To (Y, Loc),
9103 Expressions => New_List (New_Reference_To (J, Loc)))),
9105 Then_Statements => New_List (Inner_If),
9107 Else_Statements => New_List (
9108 Make_Simple_Return_Statement (Loc,
9112 Make_Indexed_Component (Loc,
9113 Prefix => New_Reference_To (X, Loc),
9114 Expressions => New_List (New_Reference_To (I, Loc))),
9117 Make_Indexed_Component (Loc,
9118 Prefix => New_Reference_To (Y, Loc),
9119 Expressions => New_List (
9120 New_Reference_To (J, Loc)))))));
9122 -- for I in X'range loop
9127 Make_Implicit_Loop_Statement (Nod,
9128 Identifier => Empty,
9131 Make_Iteration_Scheme (Loc,
9132 Loop_Parameter_Specification =>
9133 Make_Loop_Parameter_Specification (Loc,
9134 Defining_Identifier => I,
9135 Discrete_Subtype_Definition =>
9136 Make_Attribute_Reference (Loc,
9137 Prefix => New_Reference_To (X, Loc),
9138 Attribute_Name => Name_Range))),
9140 Statements => New_List (Loop_Body));
9142 -- if X'length = 0 then
9144 -- elsif Y'length = 0 then
9147 -- for ... loop ... end loop;
9148 -- return X'length > Y'length;
9152 Make_Attribute_Reference (Loc,
9153 Prefix => New_Reference_To (X, Loc),
9154 Attribute_Name => Name_Length);
9157 Make_Attribute_Reference (Loc,
9158 Prefix => New_Reference_To (Y, Loc),
9159 Attribute_Name => Name_Length);
9163 Left_Opnd => Length1,
9164 Right_Opnd => Length2);
9167 Make_Implicit_If_Statement (Nod,
9171 Make_Attribute_Reference (Loc,
9172 Prefix => New_Reference_To (X, Loc),
9173 Attribute_Name => Name_Length),
9175 Make_Integer_Literal (Loc, 0)),
9179 Make_Simple_Return_Statement (Loc,
9180 Expression => New_Reference_To (Standard_False, Loc))),
9182 Elsif_Parts => New_List (
9183 Make_Elsif_Part (Loc,
9187 Make_Attribute_Reference (Loc,
9188 Prefix => New_Reference_To (Y, Loc),
9189 Attribute_Name => Name_Length),
9191 Make_Integer_Literal (Loc, 0)),
9195 Make_Simple_Return_Statement (Loc,
9196 Expression => New_Reference_To (Standard_True, Loc))))),
9198 Else_Statements => New_List (
9200 Make_Simple_Return_Statement (Loc,
9201 Expression => Final_Expr)));
9205 Formals := New_List (
9206 Make_Parameter_Specification (Loc,
9207 Defining_Identifier => X,
9208 Parameter_Type => New_Reference_To (Typ, Loc)),
9210 Make_Parameter_Specification (Loc,
9211 Defining_Identifier => Y,
9212 Parameter_Type => New_Reference_To (Typ, Loc)));
9214 -- function Gnnn (...) return boolean is
9215 -- J : index := Y'first;
9220 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('G'));
9223 Make_Subprogram_Body (Loc,
9225 Make_Function_Specification (Loc,
9226 Defining_Unit_Name => Func_Name,
9227 Parameter_Specifications => Formals,
9228 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
9230 Declarations => New_List (
9231 Make_Object_Declaration (Loc,
9232 Defining_Identifier => J,
9233 Object_Definition => New_Reference_To (Index, Loc),
9235 Make_Attribute_Reference (Loc,
9236 Prefix => New_Reference_To (Y, Loc),
9237 Attribute_Name => Name_First))),
9239 Handled_Statement_Sequence =>
9240 Make_Handled_Sequence_Of_Statements (Loc,
9241 Statements => New_List (If_Stat)));
9244 end Make_Array_Comparison_Op;
9246 ---------------------------
9247 -- Make_Boolean_Array_Op --
9248 ---------------------------
9250 -- For logical operations on boolean arrays, expand in line the following,
9251 -- replacing 'and' with 'or' or 'xor' where needed:
9253 -- function Annn (A : typ; B: typ) return typ is
9256 -- for J in A'range loop
9257 -- C (J) := A (J) op B (J);
9262 -- Here typ is the boolean array type
9264 function Make_Boolean_Array_Op
9266 N : Node_Id) return Node_Id
9268 Loc : constant Source_Ptr := Sloc (N);
9270 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
9271 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
9272 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
9273 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
9281 Func_Name : Entity_Id;
9282 Func_Body : Node_Id;
9283 Loop_Statement : Node_Id;
9287 Make_Indexed_Component (Loc,
9288 Prefix => New_Reference_To (A, Loc),
9289 Expressions => New_List (New_Reference_To (J, Loc)));
9292 Make_Indexed_Component (Loc,
9293 Prefix => New_Reference_To (B, Loc),
9294 Expressions => New_List (New_Reference_To (J, Loc)));
9297 Make_Indexed_Component (Loc,
9298 Prefix => New_Reference_To (C, Loc),
9299 Expressions => New_List (New_Reference_To (J, Loc)));
9301 if Nkind (N) = N_Op_And then
9307 elsif Nkind (N) = N_Op_Or then
9321 Make_Implicit_Loop_Statement (N,
9322 Identifier => Empty,
9325 Make_Iteration_Scheme (Loc,
9326 Loop_Parameter_Specification =>
9327 Make_Loop_Parameter_Specification (Loc,
9328 Defining_Identifier => J,
9329 Discrete_Subtype_Definition =>
9330 Make_Attribute_Reference (Loc,
9331 Prefix => New_Reference_To (A, Loc),
9332 Attribute_Name => Name_Range))),
9334 Statements => New_List (
9335 Make_Assignment_Statement (Loc,
9337 Expression => Op)));
9339 Formals := New_List (
9340 Make_Parameter_Specification (Loc,
9341 Defining_Identifier => A,
9342 Parameter_Type => New_Reference_To (Typ, Loc)),
9344 Make_Parameter_Specification (Loc,
9345 Defining_Identifier => B,
9346 Parameter_Type => New_Reference_To (Typ, Loc)));
9349 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
9350 Set_Is_Inlined (Func_Name);
9353 Make_Subprogram_Body (Loc,
9355 Make_Function_Specification (Loc,
9356 Defining_Unit_Name => Func_Name,
9357 Parameter_Specifications => Formals,
9358 Result_Definition => New_Reference_To (Typ, Loc)),
9360 Declarations => New_List (
9361 Make_Object_Declaration (Loc,
9362 Defining_Identifier => C,
9363 Object_Definition => New_Reference_To (Typ, Loc))),
9365 Handled_Statement_Sequence =>
9366 Make_Handled_Sequence_Of_Statements (Loc,
9367 Statements => New_List (
9369 Make_Simple_Return_Statement (Loc,
9370 Expression => New_Reference_To (C, Loc)))));
9373 end Make_Boolean_Array_Op;
9375 ------------------------
9376 -- Rewrite_Comparison --
9377 ------------------------
9379 procedure Rewrite_Comparison (N : Node_Id) is
9380 Warning_Generated : Boolean := False;
9381 -- Set to True if first pass with Assume_Valid generates a warning in
9382 -- which case we skip the second pass to avoid warning overloaded.
9385 -- Set to Standard_True or Standard_False
9388 if Nkind (N) = N_Type_Conversion then
9389 Rewrite_Comparison (Expression (N));
9392 elsif Nkind (N) not in N_Op_Compare then
9396 -- Now start looking at the comparison in detail. We potentially go
9397 -- through this loop twice. The first time, Assume_Valid is set False
9398 -- in the call to Compile_Time_Compare. If this call results in a
9399 -- clear result of always True or Always False, that's decisive and
9400 -- we are done. Otherwise we repeat the processing with Assume_Valid
9401 -- set to True to generate additional warnings. We can stil that step
9402 -- if Constant_Condition_Warnings is False.
9404 for AV in False .. True loop
9406 Typ : constant Entity_Id := Etype (N);
9407 Op1 : constant Node_Id := Left_Opnd (N);
9408 Op2 : constant Node_Id := Right_Opnd (N);
9410 Res : constant Compare_Result :=
9411 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
9412 -- Res indicates if compare outcome can be compile time determined
9414 True_Result : Boolean;
9415 False_Result : Boolean;
9418 case N_Op_Compare (Nkind (N)) is
9420 True_Result := Res = EQ;
9421 False_Result := Res = LT or else Res = GT or else Res = NE;
9424 True_Result := Res in Compare_GE;
9425 False_Result := Res = LT;
9428 and then Constant_Condition_Warnings
9429 and then Comes_From_Source (Original_Node (N))
9430 and then Nkind (Original_Node (N)) = N_Op_Ge
9431 and then not In_Instance
9432 and then Is_Integer_Type (Etype (Left_Opnd (N)))
9433 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
9436 ("can never be greater than, could replace by ""'=""?", N);
9437 Warning_Generated := True;
9441 True_Result := Res = GT;
9442 False_Result := Res in Compare_LE;
9445 True_Result := Res = LT;
9446 False_Result := Res in Compare_GE;
9449 True_Result := Res in Compare_LE;
9450 False_Result := Res = GT;
9453 and then Constant_Condition_Warnings
9454 and then Comes_From_Source (Original_Node (N))
9455 and then Nkind (Original_Node (N)) = N_Op_Le
9456 and then not In_Instance
9457 and then Is_Integer_Type (Etype (Left_Opnd (N)))
9458 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
9461 ("can never be less than, could replace by ""'=""?", N);
9462 Warning_Generated := True;
9466 True_Result := Res = NE or else Res = GT or else Res = LT;
9467 False_Result := Res = EQ;
9470 -- If this is the first iteration, then we actually convert the
9471 -- comparison into True or False, if the result is certain.
9474 if True_Result or False_Result then
9476 Result := Standard_True;
9478 Result := Standard_False;
9483 New_Occurrence_Of (Result, Sloc (N))));
9484 Analyze_And_Resolve (N, Typ);
9485 Warn_On_Known_Condition (N);
9489 -- If this is the second iteration (AV = True), and the original
9490 -- node comes from source and we are not in an instance, then
9491 -- give a warning if we know result would be True or False. Note
9492 -- we know Constant_Condition_Warnings is set if we get here.
9494 elsif Comes_From_Source (Original_Node (N))
9495 and then not In_Instance
9499 ("condition can only be False if invalid values present?",
9501 elsif False_Result then
9503 ("condition can only be True if invalid values present?",
9509 -- Skip second iteration if not warning on constant conditions or
9510 -- if the first iteration already generated a warning of some kind
9511 -- or if we are in any case assuming all values are valid (so that
9512 -- the first iteration took care of the valid case).
9514 exit when not Constant_Condition_Warnings;
9515 exit when Warning_Generated;
9516 exit when Assume_No_Invalid_Values;
9518 end Rewrite_Comparison;
9520 ----------------------------
9521 -- Safe_In_Place_Array_Op --
9522 ----------------------------
9524 function Safe_In_Place_Array_Op
9527 Op2 : Node_Id) return Boolean
9531 function Is_Safe_Operand (Op : Node_Id) return Boolean;
9532 -- Operand is safe if it cannot overlap part of the target of the
9533 -- operation. If the operand and the target are identical, the operand
9534 -- is safe. The operand can be empty in the case of negation.
9536 function Is_Unaliased (N : Node_Id) return Boolean;
9537 -- Check that N is a stand-alone entity
9543 function Is_Unaliased (N : Node_Id) return Boolean is
9547 and then No (Address_Clause (Entity (N)))
9548 and then No (Renamed_Object (Entity (N)));
9551 ---------------------
9552 -- Is_Safe_Operand --
9553 ---------------------
9555 function Is_Safe_Operand (Op : Node_Id) return Boolean is
9560 elsif Is_Entity_Name (Op) then
9561 return Is_Unaliased (Op);
9563 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
9564 return Is_Unaliased (Prefix (Op));
9566 elsif Nkind (Op) = N_Slice then
9568 Is_Unaliased (Prefix (Op))
9569 and then Entity (Prefix (Op)) /= Target;
9571 elsif Nkind (Op) = N_Op_Not then
9572 return Is_Safe_Operand (Right_Opnd (Op));
9577 end Is_Safe_Operand;
9579 -- Start of processing for Is_Safe_In_Place_Array_Op
9582 -- Skip this processing if the component size is different from system
9583 -- storage unit (since at least for NOT this would cause problems).
9585 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
9588 -- Cannot do in place stuff on VM_Target since cannot pass addresses
9590 elsif VM_Target /= No_VM then
9593 -- Cannot do in place stuff if non-standard Boolean representation
9595 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
9598 elsif not Is_Unaliased (Lhs) then
9601 Target := Entity (Lhs);
9604 Is_Safe_Operand (Op1)
9605 and then Is_Safe_Operand (Op2);
9607 end Safe_In_Place_Array_Op;
9609 -----------------------
9610 -- Tagged_Membership --
9611 -----------------------
9613 -- There are two different cases to consider depending on whether the right
9614 -- operand is a class-wide type or not. If not we just compare the actual
9615 -- tag of the left expr to the target type tag:
9617 -- Left_Expr.Tag = Right_Type'Tag;
9619 -- If it is a class-wide type we use the RT function CW_Membership which is
9620 -- usually implemented by looking in the ancestor tables contained in the
9621 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
9623 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
9624 -- function IW_Membership which is usually implemented by looking in the
9625 -- table of abstract interface types plus the ancestor table contained in
9626 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
9628 function Tagged_Membership (N : Node_Id) return Node_Id is
9629 Left : constant Node_Id := Left_Opnd (N);
9630 Right : constant Node_Id := Right_Opnd (N);
9631 Loc : constant Source_Ptr := Sloc (N);
9633 Left_Type : Entity_Id;
9634 Right_Type : Entity_Id;
9638 -- Handle entities from the limited view
9640 Left_Type := Available_View (Etype (Left));
9641 Right_Type := Available_View (Etype (Right));
9643 if Is_Class_Wide_Type (Left_Type) then
9644 Left_Type := Root_Type (Left_Type);
9648 Make_Selected_Component (Loc,
9649 Prefix => Relocate_Node (Left),
9651 New_Reference_To (First_Tag_Component (Left_Type), Loc));
9653 if Is_Class_Wide_Type (Right_Type) then
9655 -- No need to issue a run-time check if we statically know that the
9656 -- result of this membership test is always true. For example,
9657 -- considering the following declarations:
9659 -- type Iface is interface;
9660 -- type T is tagged null record;
9661 -- type DT is new T and Iface with null record;
9666 -- These membership tests are always true:
9670 -- Obj2 in Iface'Class;
9672 -- We do not need to handle cases where the membership is illegal.
9675 -- Obj1 in DT'Class; -- Compile time error
9676 -- Obj1 in Iface'Class; -- Compile time error
9678 if not Is_Class_Wide_Type (Left_Type)
9679 and then (Is_Ancestor (Etype (Right_Type), Left_Type)
9680 or else (Is_Interface (Etype (Right_Type))
9681 and then Interface_Present_In_Ancestor
9683 Iface => Etype (Right_Type))))
9685 return New_Reference_To (Standard_True, Loc);
9688 -- Ada 2005 (AI-251): Class-wide applied to interfaces
9690 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
9692 -- Support to: "Iface_CW_Typ in Typ'Class"
9694 or else Is_Interface (Left_Type)
9696 -- Issue error if IW_Membership operation not available in a
9697 -- configurable run time setting.
9699 if not RTE_Available (RE_IW_Membership) then
9701 ("dynamic membership test on interface types", N);
9706 Make_Function_Call (Loc,
9707 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
9708 Parameter_Associations => New_List (
9709 Make_Attribute_Reference (Loc,
9711 Attribute_Name => Name_Address),
9714 (Access_Disp_Table (Root_Type (Right_Type)))),
9717 -- Ada 95: Normal case
9721 Build_CW_Membership (Loc,
9722 Obj_Tag_Node => Obj_Tag,
9726 (Access_Disp_Table (Root_Type (Right_Type)))),
9730 -- Right_Type is not a class-wide type
9733 -- No need to check the tag of the object if Right_Typ is abstract
9735 if Is_Abstract_Type (Right_Type) then
9736 return New_Reference_To (Standard_False, Loc);
9741 Left_Opnd => Obj_Tag,
9744 (Node (First_Elmt (Access_Disp_Table (Right_Type))), Loc));
9747 end Tagged_Membership;
9749 ------------------------------
9750 -- Unary_Op_Validity_Checks --
9751 ------------------------------
9753 procedure Unary_Op_Validity_Checks (N : Node_Id) is
9755 if Validity_Checks_On and Validity_Check_Operands then
9756 Ensure_Valid (Right_Opnd (N));
9758 end Unary_Op_Validity_Checks;