1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, 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 VM_Target /= No_VM then
385 pragma Assert (Nkind (N) = N_Identifier
386 and then Nkind (Orig_Node) = N_Allocator);
388 PtrT := Etype (Orig_Node);
389 Dtyp := 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 VM_Target = No_VM
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 -- Ada 2005 (AI-318-02): If the initialization expression is a call
576 -- to a build-in-place function, then access to the allocated object
577 -- must be passed to the function. Currently we limit such functions
578 -- to those with constrained limited result subtypes, but eventually
579 -- we plan to expand the allowed forms of functions that are treated
580 -- as build-in-place.
582 if Ada_Version >= Ada_05
583 and then Is_Build_In_Place_Function_Call (Exp)
585 Make_Build_In_Place_Call_In_Allocator (N, Exp);
586 Apply_Accessibility_Check (N, Built_In_Place => True);
590 -- Actions inserted before:
591 -- Temp : constant ptr_T := new T'(Expression);
592 -- <no CW> Temp._tag := T'tag;
593 -- <CTRL> Adjust (Finalizable (Temp.all));
594 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
596 -- We analyze by hand the new internal allocator to avoid
597 -- any recursion and inappropriate call to Initialize
599 -- We don't want to remove side effects when the expression must be
600 -- built in place. In the case of a build-in-place function call,
601 -- that could lead to a duplication of the call, which was already
602 -- substituted for the allocator.
604 if not Aggr_In_Place then
605 Remove_Side_Effects (Exp);
609 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
611 -- For a class wide allocation generate the following code:
613 -- type Equiv_Record is record ... end record;
614 -- implicit subtype CW is <Class_Wide_Subytpe>;
615 -- temp : PtrT := new CW'(CW!(expr));
617 if Is_Class_Wide_Type (T) then
618 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
620 -- Ada 2005 (AI-251): If the expression is a class-wide interface
621 -- object we generate code to move up "this" to reference the
622 -- base of the object before allocating the new object.
624 -- Note that Exp'Address is recursively expanded into a call
625 -- to Base_Address (Exp.Tag)
627 if Is_Class_Wide_Type (Etype (Exp))
628 and then Is_Interface (Etype (Exp))
629 and then VM_Target = No_VM
633 Unchecked_Convert_To (Entity (Indic),
634 Make_Explicit_Dereference (Loc,
635 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
636 Make_Attribute_Reference (Loc,
638 Attribute_Name => Name_Address)))));
643 Unchecked_Convert_To (Entity (Indic), Exp));
646 Analyze_And_Resolve (Expression (N), Entity (Indic));
649 -- Keep separate the management of allocators returning interfaces
651 if not Is_Interface (Directly_Designated_Type (PtrT)) then
652 if Aggr_In_Place then
654 Make_Object_Declaration (Loc,
655 Defining_Identifier => Temp,
656 Object_Definition => New_Reference_To (PtrT, Loc),
659 New_Reference_To (Etype (Exp), Loc)));
661 -- Copy the Comes_From_Source flag for the allocator we just
662 -- built, since logically this allocator is a replacement of
663 -- the original allocator node. This is for proper handling of
664 -- restriction No_Implicit_Heap_Allocations.
666 Set_Comes_From_Source
667 (Expression (Tmp_Node), Comes_From_Source (N));
669 Set_No_Initialization (Expression (Tmp_Node));
670 Insert_Action (N, Tmp_Node);
672 if Needs_Finalization (T)
673 and then Ekind (PtrT) = E_Anonymous_Access_Type
675 -- Create local finalization list for access parameter
677 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
680 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
683 Node := Relocate_Node (N);
686 Make_Object_Declaration (Loc,
687 Defining_Identifier => Temp,
688 Constant_Present => True,
689 Object_Definition => New_Reference_To (PtrT, Loc),
690 Expression => Node));
693 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
694 -- interface type. In this case we use the type of the qualified
695 -- expression to allocate the object.
699 Def_Id : constant Entity_Id :=
700 Make_Defining_Identifier (Loc,
701 New_Internal_Name ('T'));
706 Make_Full_Type_Declaration (Loc,
707 Defining_Identifier => Def_Id,
709 Make_Access_To_Object_Definition (Loc,
711 Null_Exclusion_Present => False,
712 Constant_Present => False,
713 Subtype_Indication =>
714 New_Reference_To (Etype (Exp), Loc)));
716 Insert_Action (N, New_Decl);
718 -- Inherit the final chain to ensure that the expansion of the
719 -- aggregate is correct in case of controlled types
721 if Needs_Finalization (Directly_Designated_Type (PtrT)) then
722 Set_Associated_Final_Chain (Def_Id,
723 Associated_Final_Chain (PtrT));
726 -- Declare the object using the previous type declaration
728 if Aggr_In_Place then
730 Make_Object_Declaration (Loc,
731 Defining_Identifier => Temp,
732 Object_Definition => New_Reference_To (Def_Id, Loc),
735 New_Reference_To (Etype (Exp), Loc)));
737 -- Copy the Comes_From_Source flag for the allocator we just
738 -- built, since logically this allocator is a replacement of
739 -- the original allocator node. This is for proper handling
740 -- of restriction No_Implicit_Heap_Allocations.
742 Set_Comes_From_Source
743 (Expression (Tmp_Node), Comes_From_Source (N));
745 Set_No_Initialization (Expression (Tmp_Node));
746 Insert_Action (N, Tmp_Node);
748 if Needs_Finalization (T)
749 and then Ekind (PtrT) = E_Anonymous_Access_Type
751 -- Create local finalization list for access parameter
754 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
757 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
759 Node := Relocate_Node (N);
762 Make_Object_Declaration (Loc,
763 Defining_Identifier => Temp,
764 Constant_Present => True,
765 Object_Definition => New_Reference_To (Def_Id, Loc),
766 Expression => Node));
769 -- Generate an additional object containing the address of the
770 -- returned object. The type of this second object declaration
771 -- is the correct type required for the common processing that
772 -- is still performed by this subprogram. The displacement of
773 -- this pointer to reference the component associated with the
774 -- interface type will be done at the end of common processing.
777 Make_Object_Declaration (Loc,
778 Defining_Identifier => Make_Defining_Identifier (Loc,
779 New_Internal_Name ('P')),
780 Object_Definition => New_Reference_To (PtrT, Loc),
781 Expression => Unchecked_Convert_To (PtrT,
782 New_Reference_To (Temp, Loc)));
784 Insert_Action (N, New_Decl);
786 Tmp_Node := New_Decl;
787 Temp := Defining_Identifier (New_Decl);
791 Apply_Accessibility_Check (Temp);
793 -- Generate the tag assignment
795 -- Suppress the tag assignment when VM_Target because VM tags are
796 -- represented implicitly in objects.
798 if VM_Target /= No_VM then
801 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
802 -- interface objects because in this case the tag does not change.
804 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
805 pragma Assert (Is_Class_Wide_Type
806 (Directly_Designated_Type (Etype (N))));
809 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
811 TagR := New_Reference_To (Temp, Loc);
813 elsif Is_Private_Type (T)
814 and then Is_Tagged_Type (Underlying_Type (T))
816 TagT := Underlying_Type (T);
818 Unchecked_Convert_To (Underlying_Type (T),
819 Make_Explicit_Dereference (Loc,
820 Prefix => New_Reference_To (Temp, Loc)));
823 if Present (TagT) then
825 Make_Assignment_Statement (Loc,
827 Make_Selected_Component (Loc,
830 New_Reference_To (First_Tag_Component (TagT), Loc)),
833 Unchecked_Convert_To (RTE (RE_Tag),
835 (Elists.Node (First_Elmt (Access_Disp_Table (TagT))),
838 -- The previous assignment has to be done in any case
840 Set_Assignment_OK (Name (Tag_Assign));
841 Insert_Action (N, Tag_Assign);
844 if Needs_Finalization (DesigT)
845 and then Needs_Finalization (T)
849 Apool : constant Entity_Id :=
850 Associated_Storage_Pool (PtrT);
853 -- If it is an allocation on the secondary stack (i.e. a value
854 -- returned from a function), the object is attached on the
855 -- caller side as soon as the call is completed (see
856 -- Expand_Ctrl_Function_Call)
858 if Is_RTE (Apool, RE_SS_Pool) then
860 F : constant Entity_Id :=
861 Make_Defining_Identifier (Loc,
862 New_Internal_Name ('F'));
865 Make_Object_Declaration (Loc,
866 Defining_Identifier => F,
867 Object_Definition => New_Reference_To (RTE
868 (RE_Finalizable_Ptr), Loc)));
870 Flist := New_Reference_To (F, Loc);
871 Attach := Make_Integer_Literal (Loc, 1);
874 -- Normal case, not a secondary stack allocation
877 if Needs_Finalization (T)
878 and then Ekind (PtrT) = E_Anonymous_Access_Type
880 -- Create local finalization list for access parameter
883 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
885 Flist := Find_Final_List (PtrT);
888 Attach := Make_Integer_Literal (Loc, 2);
891 -- Generate an Adjust call if the object will be moved. In Ada
892 -- 2005, the object may be inherently limited, in which case
893 -- there is no Adjust procedure, and the object is built in
894 -- place. In Ada 95, the object can be limited but not
895 -- inherently limited if this allocator came from a return
896 -- statement (we're allocating the result on the secondary
897 -- stack). In that case, the object will be moved, so we _do_
901 and then not Is_Inherently_Limited_Type (T)
907 -- An unchecked conversion is needed in the classwide
908 -- case because the designated type can be an ancestor of
909 -- the subtype mark of the allocator.
911 Unchecked_Convert_To (T,
912 Make_Explicit_Dereference (Loc,
913 Prefix => New_Reference_To (Temp, Loc))),
917 With_Attach => Attach,
923 Rewrite (N, New_Reference_To (Temp, Loc));
924 Analyze_And_Resolve (N, PtrT);
926 -- Ada 2005 (AI-251): Displace the pointer to reference the record
927 -- component containing the secondary dispatch table of the interface
930 if Is_Interface (Directly_Designated_Type (PtrT)) then
931 Displace_Allocator_Pointer (N);
934 elsif Aggr_In_Place then
936 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
938 Make_Object_Declaration (Loc,
939 Defining_Identifier => Temp,
940 Object_Definition => New_Reference_To (PtrT, Loc),
941 Expression => Make_Allocator (Loc,
942 New_Reference_To (Etype (Exp), Loc)));
944 -- Copy the Comes_From_Source flag for the allocator we just built,
945 -- since logically this allocator is a replacement of the original
946 -- allocator node. This is for proper handling of restriction
947 -- No_Implicit_Heap_Allocations.
949 Set_Comes_From_Source
950 (Expression (Tmp_Node), Comes_From_Source (N));
952 Set_No_Initialization (Expression (Tmp_Node));
953 Insert_Action (N, Tmp_Node);
954 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
955 Rewrite (N, New_Reference_To (Temp, Loc));
956 Analyze_And_Resolve (N, PtrT);
958 elsif Is_Access_Type (T)
959 and then Can_Never_Be_Null (T)
961 Install_Null_Excluding_Check (Exp);
963 elsif Is_Access_Type (DesigT)
964 and then Nkind (Exp) = N_Allocator
965 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
967 -- Apply constraint to designated subtype indication
969 Apply_Constraint_Check (Expression (Exp),
970 Designated_Type (DesigT),
973 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
975 -- Propagate constraint_error to enclosing allocator
977 Rewrite (Exp, New_Copy (Expression (Exp)));
980 -- First check against the type of the qualified expression
982 -- NOTE: The commented call should be correct, but for some reason
983 -- causes the compiler to bomb (sigsegv) on ACVC test c34007g, so for
984 -- now we just perform the old (incorrect) test against the
985 -- designated subtype with no sliding in the else part of the if
986 -- statement below. ???
988 -- Apply_Constraint_Check (Exp, T, No_Sliding => True);
990 -- A check is also needed in cases where the designated subtype is
991 -- constrained and differs from the subtype given in the qualified
992 -- expression. Note that the check on the qualified expression does
993 -- not allow sliding, but this check does (a relaxation from Ada 83).
995 if Is_Constrained (DesigT)
996 and then not Subtypes_Statically_Match (T, DesigT)
998 Apply_Constraint_Check
999 (Exp, DesigT, No_Sliding => False);
1001 -- The nonsliding check should really be performed (unconditionally)
1002 -- against the subtype of the qualified expression, but that causes a
1003 -- problem with c34007g (see above), so for now we retain this.
1006 Apply_Constraint_Check
1007 (Exp, DesigT, No_Sliding => True);
1010 -- For an access to unconstrained packed array, GIGI needs to see an
1011 -- expression with a constrained subtype in order to compute the
1012 -- proper size for the allocator.
1014 if Is_Array_Type (T)
1015 and then not Is_Constrained (T)
1016 and then Is_Packed (T)
1019 ConstrT : constant Entity_Id :=
1020 Make_Defining_Identifier (Loc,
1021 Chars => New_Internal_Name ('A'));
1022 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1025 Make_Subtype_Declaration (Loc,
1026 Defining_Identifier => ConstrT,
1027 Subtype_Indication =>
1028 Make_Subtype_From_Expr (Exp, T)));
1029 Freeze_Itype (ConstrT, Exp);
1030 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1034 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1035 -- to a build-in-place function, then access to the allocated object
1036 -- must be passed to the function. Currently we limit such functions
1037 -- to those with constrained limited result subtypes, but eventually
1038 -- we plan to expand the allowed forms of functions that are treated
1039 -- as build-in-place.
1041 if Ada_Version >= Ada_05
1042 and then Is_Build_In_Place_Function_Call (Exp)
1044 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1049 when RE_Not_Available =>
1051 end Expand_Allocator_Expression;
1053 -----------------------------
1054 -- Expand_Array_Comparison --
1055 -----------------------------
1057 -- Expansion is only required in the case of array types. For the unpacked
1058 -- case, an appropriate runtime routine is called. For packed cases, and
1059 -- also in some other cases where a runtime routine cannot be called, the
1060 -- form of the expansion is:
1062 -- [body for greater_nn; boolean_expression]
1064 -- The body is built by Make_Array_Comparison_Op, and the form of the
1065 -- Boolean expression depends on the operator involved.
1067 procedure Expand_Array_Comparison (N : Node_Id) is
1068 Loc : constant Source_Ptr := Sloc (N);
1069 Op1 : Node_Id := Left_Opnd (N);
1070 Op2 : Node_Id := Right_Opnd (N);
1071 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1072 Ctyp : constant Entity_Id := Component_Type (Typ1);
1075 Func_Body : Node_Id;
1076 Func_Name : Entity_Id;
1080 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1081 -- True for byte addressable target
1083 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1084 -- Returns True if the length of the given operand is known to be less
1085 -- than 4. Returns False if this length is known to be four or greater
1086 -- or is not known at compile time.
1088 ------------------------
1089 -- Length_Less_Than_4 --
1090 ------------------------
1092 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1093 Otyp : constant Entity_Id := Etype (Opnd);
1096 if Ekind (Otyp) = E_String_Literal_Subtype then
1097 return String_Literal_Length (Otyp) < 4;
1101 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1102 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1103 Hi : constant Node_Id := Type_High_Bound (Ityp);
1108 if Compile_Time_Known_Value (Lo) then
1109 Lov := Expr_Value (Lo);
1114 if Compile_Time_Known_Value (Hi) then
1115 Hiv := Expr_Value (Hi);
1120 return Hiv < Lov + 3;
1123 end Length_Less_Than_4;
1125 -- Start of processing for Expand_Array_Comparison
1128 -- Deal first with unpacked case, where we can call a runtime routine
1129 -- except that we avoid this for targets for which are not addressable
1130 -- by bytes, and for the JVM/CIL, since they do not support direct
1131 -- addressing of array components.
1133 if not Is_Bit_Packed_Array (Typ1)
1134 and then Byte_Addressable
1135 and then VM_Target = No_VM
1137 -- The call we generate is:
1139 -- Compare_Array_xn[_Unaligned]
1140 -- (left'address, right'address, left'length, right'length) <op> 0
1142 -- x = U for unsigned, S for signed
1143 -- n = 8,16,32,64 for component size
1144 -- Add _Unaligned if length < 4 and component size is 8.
1145 -- <op> is the standard comparison operator
1147 if Component_Size (Typ1) = 8 then
1148 if Length_Less_Than_4 (Op1)
1150 Length_Less_Than_4 (Op2)
1152 if Is_Unsigned_Type (Ctyp) then
1153 Comp := RE_Compare_Array_U8_Unaligned;
1155 Comp := RE_Compare_Array_S8_Unaligned;
1159 if Is_Unsigned_Type (Ctyp) then
1160 Comp := RE_Compare_Array_U8;
1162 Comp := RE_Compare_Array_S8;
1166 elsif Component_Size (Typ1) = 16 then
1167 if Is_Unsigned_Type (Ctyp) then
1168 Comp := RE_Compare_Array_U16;
1170 Comp := RE_Compare_Array_S16;
1173 elsif Component_Size (Typ1) = 32 then
1174 if Is_Unsigned_Type (Ctyp) then
1175 Comp := RE_Compare_Array_U32;
1177 Comp := RE_Compare_Array_S32;
1180 else pragma Assert (Component_Size (Typ1) = 64);
1181 if Is_Unsigned_Type (Ctyp) then
1182 Comp := RE_Compare_Array_U64;
1184 Comp := RE_Compare_Array_S64;
1188 Remove_Side_Effects (Op1, Name_Req => True);
1189 Remove_Side_Effects (Op2, Name_Req => True);
1192 Make_Function_Call (Sloc (Op1),
1193 Name => New_Occurrence_Of (RTE (Comp), Loc),
1195 Parameter_Associations => New_List (
1196 Make_Attribute_Reference (Loc,
1197 Prefix => Relocate_Node (Op1),
1198 Attribute_Name => Name_Address),
1200 Make_Attribute_Reference (Loc,
1201 Prefix => Relocate_Node (Op2),
1202 Attribute_Name => Name_Address),
1204 Make_Attribute_Reference (Loc,
1205 Prefix => Relocate_Node (Op1),
1206 Attribute_Name => Name_Length),
1208 Make_Attribute_Reference (Loc,
1209 Prefix => Relocate_Node (Op2),
1210 Attribute_Name => Name_Length))));
1213 Make_Integer_Literal (Sloc (Op2),
1216 Analyze_And_Resolve (Op1, Standard_Integer);
1217 Analyze_And_Resolve (Op2, Standard_Integer);
1221 -- Cases where we cannot make runtime call
1223 -- For (a <= b) we convert to not (a > b)
1225 if Chars (N) = Name_Op_Le then
1231 Right_Opnd => Op2)));
1232 Analyze_And_Resolve (N, Standard_Boolean);
1235 -- For < the Boolean expression is
1236 -- greater__nn (op2, op1)
1238 elsif Chars (N) = Name_Op_Lt then
1239 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1243 Op1 := Right_Opnd (N);
1244 Op2 := Left_Opnd (N);
1246 -- For (a >= b) we convert to not (a < b)
1248 elsif Chars (N) = Name_Op_Ge then
1254 Right_Opnd => Op2)));
1255 Analyze_And_Resolve (N, Standard_Boolean);
1258 -- For > the Boolean expression is
1259 -- greater__nn (op1, op2)
1262 pragma Assert (Chars (N) = Name_Op_Gt);
1263 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1266 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1268 Make_Function_Call (Loc,
1269 Name => New_Reference_To (Func_Name, Loc),
1270 Parameter_Associations => New_List (Op1, Op2));
1272 Insert_Action (N, Func_Body);
1274 Analyze_And_Resolve (N, Standard_Boolean);
1277 when RE_Not_Available =>
1279 end Expand_Array_Comparison;
1281 ---------------------------
1282 -- Expand_Array_Equality --
1283 ---------------------------
1285 -- Expand an equality function for multi-dimensional arrays. Here is an
1286 -- example of such a function for Nb_Dimension = 2
1288 -- function Enn (A : atyp; B : btyp) return boolean is
1290 -- if (A'length (1) = 0 or else A'length (2) = 0)
1292 -- (B'length (1) = 0 or else B'length (2) = 0)
1294 -- return True; -- RM 4.5.2(22)
1297 -- if A'length (1) /= B'length (1)
1299 -- A'length (2) /= B'length (2)
1301 -- return False; -- RM 4.5.2(23)
1305 -- A1 : Index_T1 := A'first (1);
1306 -- B1 : Index_T1 := B'first (1);
1310 -- A2 : Index_T2 := A'first (2);
1311 -- B2 : Index_T2 := B'first (2);
1314 -- if A (A1, A2) /= B (B1, B2) then
1318 -- exit when A2 = A'last (2);
1319 -- A2 := Index_T2'succ (A2);
1320 -- B2 := Index_T2'succ (B2);
1324 -- exit when A1 = A'last (1);
1325 -- A1 := Index_T1'succ (A1);
1326 -- B1 := Index_T1'succ (B1);
1333 -- Note on the formal types used (atyp and btyp). If either of the arrays
1334 -- is of a private type, we use the underlying type, and do an unchecked
1335 -- conversion of the actual. If either of the arrays has a bound depending
1336 -- on a discriminant, then we use the base type since otherwise we have an
1337 -- escaped discriminant in the function.
1339 -- If both arrays are constrained and have the same bounds, we can generate
1340 -- a loop with an explicit iteration scheme using a 'Range attribute over
1343 function Expand_Array_Equality
1348 Typ : Entity_Id) return Node_Id
1350 Loc : constant Source_Ptr := Sloc (Nod);
1351 Decls : constant List_Id := New_List;
1352 Index_List1 : constant List_Id := New_List;
1353 Index_List2 : constant List_Id := New_List;
1357 Func_Name : Entity_Id;
1358 Func_Body : Node_Id;
1360 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1361 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1365 -- The parameter types to be used for the formals
1370 Num : Int) return Node_Id;
1371 -- This builds the attribute reference Arr'Nam (Expr)
1373 function Component_Equality (Typ : Entity_Id) return Node_Id;
1374 -- Create one statement to compare corresponding components, designated
1375 -- by a full set of indices.
1377 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1378 -- Given one of the arguments, computes the appropriate type to be used
1379 -- for that argument in the corresponding function formal
1381 function Handle_One_Dimension
1383 Index : Node_Id) return Node_Id;
1384 -- This procedure returns the following code
1387 -- Bn : Index_T := B'First (N);
1391 -- exit when An = A'Last (N);
1392 -- An := Index_T'Succ (An)
1393 -- Bn := Index_T'Succ (Bn)
1397 -- If both indices are constrained and identical, the procedure
1398 -- returns a simpler loop:
1400 -- for An in A'Range (N) loop
1404 -- N is the dimension for which we are generating a loop. Index is the
1405 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1406 -- xxx statement is either the loop or declare for the next dimension
1407 -- or if this is the last dimension the comparison of corresponding
1408 -- components of the arrays.
1410 -- The actual way the code works is to return the comparison of
1411 -- corresponding components for the N+1 call. That's neater!
1413 function Test_Empty_Arrays return Node_Id;
1414 -- This function constructs the test for both arrays being empty
1415 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1417 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1419 function Test_Lengths_Correspond return Node_Id;
1420 -- This function constructs the test for arrays having different lengths
1421 -- in at least one index position, in which case the resulting code is:
1423 -- A'length (1) /= B'length (1)
1425 -- A'length (2) /= B'length (2)
1436 Num : Int) return Node_Id
1440 Make_Attribute_Reference (Loc,
1441 Attribute_Name => Nam,
1442 Prefix => New_Reference_To (Arr, Loc),
1443 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1446 ------------------------
1447 -- Component_Equality --
1448 ------------------------
1450 function Component_Equality (Typ : Entity_Id) return Node_Id is
1455 -- if a(i1...) /= b(j1...) then return false; end if;
1458 Make_Indexed_Component (Loc,
1459 Prefix => Make_Identifier (Loc, Chars (A)),
1460 Expressions => Index_List1);
1463 Make_Indexed_Component (Loc,
1464 Prefix => Make_Identifier (Loc, Chars (B)),
1465 Expressions => Index_List2);
1467 Test := Expand_Composite_Equality
1468 (Nod, Component_Type (Typ), L, R, Decls);
1470 -- If some (sub)component is an unchecked_union, the whole operation
1471 -- will raise program error.
1473 if Nkind (Test) = N_Raise_Program_Error then
1475 -- This node is going to be inserted at a location where a
1476 -- statement is expected: clear its Etype so analysis will set
1477 -- it to the expected Standard_Void_Type.
1479 Set_Etype (Test, Empty);
1484 Make_Implicit_If_Statement (Nod,
1485 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1486 Then_Statements => New_List (
1487 Make_Simple_Return_Statement (Loc,
1488 Expression => New_Occurrence_Of (Standard_False, Loc))));
1490 end Component_Equality;
1496 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1507 T := Underlying_Type (T);
1509 X := First_Index (T);
1510 while Present (X) loop
1511 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1513 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1526 --------------------------
1527 -- Handle_One_Dimension --
1528 ---------------------------
1530 function Handle_One_Dimension
1532 Index : Node_Id) return Node_Id
1534 Need_Separate_Indexes : constant Boolean :=
1536 or else not Is_Constrained (Ltyp);
1537 -- If the index types are identical, and we are working with
1538 -- constrained types, then we can use the same index for both
1541 An : constant Entity_Id := Make_Defining_Identifier (Loc,
1542 Chars => New_Internal_Name ('A'));
1545 Index_T : Entity_Id;
1550 if N > Number_Dimensions (Ltyp) then
1551 return Component_Equality (Ltyp);
1554 -- Case where we generate a loop
1556 Index_T := Base_Type (Etype (Index));
1558 if Need_Separate_Indexes then
1560 Make_Defining_Identifier (Loc,
1561 Chars => New_Internal_Name ('B'));
1566 Append (New_Reference_To (An, Loc), Index_List1);
1567 Append (New_Reference_To (Bn, Loc), Index_List2);
1569 Stm_List := New_List (
1570 Handle_One_Dimension (N + 1, Next_Index (Index)));
1572 if Need_Separate_Indexes then
1574 -- Generate guard for loop, followed by increments of indices
1576 Append_To (Stm_List,
1577 Make_Exit_Statement (Loc,
1580 Left_Opnd => New_Reference_To (An, Loc),
1581 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1583 Append_To (Stm_List,
1584 Make_Assignment_Statement (Loc,
1585 Name => New_Reference_To (An, Loc),
1587 Make_Attribute_Reference (Loc,
1588 Prefix => New_Reference_To (Index_T, Loc),
1589 Attribute_Name => Name_Succ,
1590 Expressions => New_List (New_Reference_To (An, Loc)))));
1592 Append_To (Stm_List,
1593 Make_Assignment_Statement (Loc,
1594 Name => New_Reference_To (Bn, Loc),
1596 Make_Attribute_Reference (Loc,
1597 Prefix => New_Reference_To (Index_T, Loc),
1598 Attribute_Name => Name_Succ,
1599 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1602 -- If separate indexes, we need a declare block for An and Bn, and a
1603 -- loop without an iteration scheme.
1605 if Need_Separate_Indexes then
1607 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1610 Make_Block_Statement (Loc,
1611 Declarations => New_List (
1612 Make_Object_Declaration (Loc,
1613 Defining_Identifier => An,
1614 Object_Definition => New_Reference_To (Index_T, Loc),
1615 Expression => Arr_Attr (A, Name_First, N)),
1617 Make_Object_Declaration (Loc,
1618 Defining_Identifier => Bn,
1619 Object_Definition => New_Reference_To (Index_T, Loc),
1620 Expression => Arr_Attr (B, Name_First, N))),
1622 Handled_Statement_Sequence =>
1623 Make_Handled_Sequence_Of_Statements (Loc,
1624 Statements => New_List (Loop_Stm)));
1626 -- If no separate indexes, return loop statement with explicit
1627 -- iteration scheme on its own
1631 Make_Implicit_Loop_Statement (Nod,
1632 Statements => Stm_List,
1634 Make_Iteration_Scheme (Loc,
1635 Loop_Parameter_Specification =>
1636 Make_Loop_Parameter_Specification (Loc,
1637 Defining_Identifier => An,
1638 Discrete_Subtype_Definition =>
1639 Arr_Attr (A, Name_Range, N))));
1642 end Handle_One_Dimension;
1644 -----------------------
1645 -- Test_Empty_Arrays --
1646 -----------------------
1648 function Test_Empty_Arrays return Node_Id is
1658 for J in 1 .. Number_Dimensions (Ltyp) loop
1661 Left_Opnd => Arr_Attr (A, Name_Length, J),
1662 Right_Opnd => Make_Integer_Literal (Loc, 0));
1666 Left_Opnd => Arr_Attr (B, Name_Length, J),
1667 Right_Opnd => Make_Integer_Literal (Loc, 0));
1676 Left_Opnd => Relocate_Node (Alist),
1677 Right_Opnd => Atest);
1681 Left_Opnd => Relocate_Node (Blist),
1682 Right_Opnd => Btest);
1689 Right_Opnd => Blist);
1690 end Test_Empty_Arrays;
1692 -----------------------------
1693 -- Test_Lengths_Correspond --
1694 -----------------------------
1696 function Test_Lengths_Correspond return Node_Id is
1702 for J in 1 .. Number_Dimensions (Ltyp) loop
1705 Left_Opnd => Arr_Attr (A, Name_Length, J),
1706 Right_Opnd => Arr_Attr (B, Name_Length, J));
1713 Left_Opnd => Relocate_Node (Result),
1714 Right_Opnd => Rtest);
1719 end Test_Lengths_Correspond;
1721 -- Start of processing for Expand_Array_Equality
1724 Ltyp := Get_Arg_Type (Lhs);
1725 Rtyp := Get_Arg_Type (Rhs);
1727 -- For now, if the argument types are not the same, go to the base type,
1728 -- since the code assumes that the formals have the same type. This is
1729 -- fixable in future ???
1731 if Ltyp /= Rtyp then
1732 Ltyp := Base_Type (Ltyp);
1733 Rtyp := Base_Type (Rtyp);
1734 pragma Assert (Ltyp = Rtyp);
1737 -- Build list of formals for function
1739 Formals := New_List (
1740 Make_Parameter_Specification (Loc,
1741 Defining_Identifier => A,
1742 Parameter_Type => New_Reference_To (Ltyp, Loc)),
1744 Make_Parameter_Specification (Loc,
1745 Defining_Identifier => B,
1746 Parameter_Type => New_Reference_To (Rtyp, Loc)));
1748 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
1750 -- Build statement sequence for function
1753 Make_Subprogram_Body (Loc,
1755 Make_Function_Specification (Loc,
1756 Defining_Unit_Name => Func_Name,
1757 Parameter_Specifications => Formals,
1758 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
1760 Declarations => Decls,
1762 Handled_Statement_Sequence =>
1763 Make_Handled_Sequence_Of_Statements (Loc,
1764 Statements => New_List (
1766 Make_Implicit_If_Statement (Nod,
1767 Condition => Test_Empty_Arrays,
1768 Then_Statements => New_List (
1769 Make_Simple_Return_Statement (Loc,
1771 New_Occurrence_Of (Standard_True, Loc)))),
1773 Make_Implicit_If_Statement (Nod,
1774 Condition => Test_Lengths_Correspond,
1775 Then_Statements => New_List (
1776 Make_Simple_Return_Statement (Loc,
1778 New_Occurrence_Of (Standard_False, Loc)))),
1780 Handle_One_Dimension (1, First_Index (Ltyp)),
1782 Make_Simple_Return_Statement (Loc,
1783 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1785 Set_Has_Completion (Func_Name, True);
1786 Set_Is_Inlined (Func_Name);
1788 -- If the array type is distinct from the type of the arguments, it
1789 -- is the full view of a private type. Apply an unchecked conversion
1790 -- to insure that analysis of the call succeeds.
1800 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1802 L := OK_Convert_To (Ltyp, Lhs);
1806 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1808 R := OK_Convert_To (Rtyp, Rhs);
1811 Actuals := New_List (L, R);
1814 Append_To (Bodies, Func_Body);
1817 Make_Function_Call (Loc,
1818 Name => New_Reference_To (Func_Name, Loc),
1819 Parameter_Associations => Actuals);
1820 end Expand_Array_Equality;
1822 -----------------------------
1823 -- Expand_Boolean_Operator --
1824 -----------------------------
1826 -- Note that we first get the actual subtypes of the operands, since we
1827 -- always want to deal with types that have bounds.
1829 procedure Expand_Boolean_Operator (N : Node_Id) is
1830 Typ : constant Entity_Id := Etype (N);
1833 -- Special case of bit packed array where both operands are known to be
1834 -- properly aligned. In this case we use an efficient run time routine
1835 -- to carry out the operation (see System.Bit_Ops).
1837 if Is_Bit_Packed_Array (Typ)
1838 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
1839 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
1841 Expand_Packed_Boolean_Operator (N);
1845 -- For the normal non-packed case, the general expansion is to build
1846 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
1847 -- and then inserting it into the tree. The original operator node is
1848 -- then rewritten as a call to this function. We also use this in the
1849 -- packed case if either operand is a possibly unaligned object.
1852 Loc : constant Source_Ptr := Sloc (N);
1853 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1854 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1855 Func_Body : Node_Id;
1856 Func_Name : Entity_Id;
1859 Convert_To_Actual_Subtype (L);
1860 Convert_To_Actual_Subtype (R);
1861 Ensure_Defined (Etype (L), N);
1862 Ensure_Defined (Etype (R), N);
1863 Apply_Length_Check (R, Etype (L));
1865 if Nkind (N) = N_Op_Xor then
1866 Silly_Boolean_Array_Xor_Test (N, Etype (L));
1869 if Nkind (Parent (N)) = N_Assignment_Statement
1870 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
1872 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
1874 elsif Nkind (Parent (N)) = N_Op_Not
1875 and then Nkind (N) = N_Op_And
1877 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
1882 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
1883 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1884 Insert_Action (N, Func_Body);
1886 -- Now rewrite the expression with a call
1889 Make_Function_Call (Loc,
1890 Name => New_Reference_To (Func_Name, Loc),
1891 Parameter_Associations =>
1894 Make_Type_Conversion
1895 (Loc, New_Reference_To (Etype (L), Loc), R))));
1897 Analyze_And_Resolve (N, Typ);
1900 end Expand_Boolean_Operator;
1902 -------------------------------
1903 -- Expand_Composite_Equality --
1904 -------------------------------
1906 -- This function is only called for comparing internal fields of composite
1907 -- types when these fields are themselves composites. This is a special
1908 -- case because it is not possible to respect normal Ada visibility rules.
1910 function Expand_Composite_Equality
1915 Bodies : List_Id) return Node_Id
1917 Loc : constant Source_Ptr := Sloc (Nod);
1918 Full_Type : Entity_Id;
1923 if Is_Private_Type (Typ) then
1924 Full_Type := Underlying_Type (Typ);
1929 -- Defense against malformed private types with no completion the error
1930 -- will be diagnosed later by check_completion
1932 if No (Full_Type) then
1933 return New_Reference_To (Standard_False, Loc);
1936 Full_Type := Base_Type (Full_Type);
1938 if Is_Array_Type (Full_Type) then
1940 -- If the operand is an elementary type other than a floating-point
1941 -- type, then we can simply use the built-in block bitwise equality,
1942 -- since the predefined equality operators always apply and bitwise
1943 -- equality is fine for all these cases.
1945 if Is_Elementary_Type (Component_Type (Full_Type))
1946 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
1948 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
1950 -- For composite component types, and floating-point types, use the
1951 -- expansion. This deals with tagged component types (where we use
1952 -- the applicable equality routine) and floating-point, (where we
1953 -- need to worry about negative zeroes), and also the case of any
1954 -- composite type recursively containing such fields.
1957 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
1960 elsif Is_Tagged_Type (Full_Type) then
1962 -- Call the primitive operation "=" of this type
1964 if Is_Class_Wide_Type (Full_Type) then
1965 Full_Type := Root_Type (Full_Type);
1968 -- If this is derived from an untagged private type completed with a
1969 -- tagged type, it does not have a full view, so we use the primitive
1970 -- operations of the private type. This check should no longer be
1971 -- necessary when these types receive their full views ???
1973 if Is_Private_Type (Typ)
1974 and then not Is_Tagged_Type (Typ)
1975 and then not Is_Controlled (Typ)
1976 and then Is_Derived_Type (Typ)
1977 and then No (Full_View (Typ))
1979 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
1981 Prim := First_Elmt (Primitive_Operations (Full_Type));
1985 Eq_Op := Node (Prim);
1986 exit when Chars (Eq_Op) = Name_Op_Eq
1987 and then Etype (First_Formal (Eq_Op)) =
1988 Etype (Next_Formal (First_Formal (Eq_Op)))
1989 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
1991 pragma Assert (Present (Prim));
1994 Eq_Op := Node (Prim);
1997 Make_Function_Call (Loc,
1998 Name => New_Reference_To (Eq_Op, Loc),
1999 Parameter_Associations =>
2001 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2002 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2004 elsif Is_Record_Type (Full_Type) then
2005 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2007 if Present (Eq_Op) then
2008 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2010 -- Inherited equality from parent type. Convert the actuals to
2011 -- match signature of operation.
2014 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2018 Make_Function_Call (Loc,
2019 Name => New_Reference_To (Eq_Op, Loc),
2020 Parameter_Associations =>
2021 New_List (OK_Convert_To (T, Lhs),
2022 OK_Convert_To (T, Rhs)));
2026 -- Comparison between Unchecked_Union components
2028 if Is_Unchecked_Union (Full_Type) then
2030 Lhs_Type : Node_Id := Full_Type;
2031 Rhs_Type : Node_Id := Full_Type;
2032 Lhs_Discr_Val : Node_Id;
2033 Rhs_Discr_Val : Node_Id;
2038 if Nkind (Lhs) = N_Selected_Component then
2039 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2044 if Nkind (Rhs) = N_Selected_Component then
2045 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2048 -- Lhs of the composite equality
2050 if Is_Constrained (Lhs_Type) then
2052 -- Since the enclosing record type can never be an
2053 -- Unchecked_Union (this code is executed for records
2054 -- that do not have variants), we may reference its
2057 if Nkind (Lhs) = N_Selected_Component
2058 and then Has_Per_Object_Constraint (
2059 Entity (Selector_Name (Lhs)))
2062 Make_Selected_Component (Loc,
2063 Prefix => Prefix (Lhs),
2066 Get_Discriminant_Value (
2067 First_Discriminant (Lhs_Type),
2069 Stored_Constraint (Lhs_Type))));
2072 Lhs_Discr_Val := New_Copy (
2073 Get_Discriminant_Value (
2074 First_Discriminant (Lhs_Type),
2076 Stored_Constraint (Lhs_Type)));
2080 -- It is not possible to infer the discriminant since
2081 -- the subtype is not constrained.
2084 Make_Raise_Program_Error (Loc,
2085 Reason => PE_Unchecked_Union_Restriction);
2088 -- Rhs of the composite equality
2090 if Is_Constrained (Rhs_Type) then
2091 if Nkind (Rhs) = N_Selected_Component
2092 and then Has_Per_Object_Constraint (
2093 Entity (Selector_Name (Rhs)))
2096 Make_Selected_Component (Loc,
2097 Prefix => Prefix (Rhs),
2100 Get_Discriminant_Value (
2101 First_Discriminant (Rhs_Type),
2103 Stored_Constraint (Rhs_Type))));
2106 Rhs_Discr_Val := New_Copy (
2107 Get_Discriminant_Value (
2108 First_Discriminant (Rhs_Type),
2110 Stored_Constraint (Rhs_Type)));
2115 Make_Raise_Program_Error (Loc,
2116 Reason => PE_Unchecked_Union_Restriction);
2119 -- Call the TSS equality function with the inferred
2120 -- discriminant values.
2123 Make_Function_Call (Loc,
2124 Name => New_Reference_To (Eq_Op, Loc),
2125 Parameter_Associations => New_List (
2133 -- Shouldn't this be an else, we can't fall through the above
2137 Make_Function_Call (Loc,
2138 Name => New_Reference_To (Eq_Op, Loc),
2139 Parameter_Associations => New_List (Lhs, Rhs));
2143 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2147 -- It can be a simple record or the full view of a scalar private
2149 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2151 end Expand_Composite_Equality;
2153 ------------------------
2154 -- Expand_Concatenate --
2155 ------------------------
2157 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2158 Loc : constant Source_Ptr := Sloc (Cnode);
2160 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2161 -- Result type of concatenation
2163 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2164 -- Component type. Elements of this component type can appear as one
2165 -- of the operands of concatenation as well as arrays.
2167 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2170 Ityp : constant Entity_Id := Base_Type (Istyp);
2171 -- Index type. This is the base type of the index subtype, and is used
2172 -- for all computed bounds (which may be out of range of Istyp in the
2173 -- case of null ranges).
2176 -- This is the type we use to do arithmetic to compute the bounds and
2177 -- lengths of operands. The choice of this type is a little subtle and
2178 -- is discussed in a separate section at the start of the body code.
2180 Concatenation_Error : exception;
2181 -- Raised if concatenation is sure to raise a CE
2183 Result_May_Be_Null : Boolean := True;
2184 -- Reset to False if at least one operand is encountered which is known
2185 -- at compile time to be non-null. Used for handling the special case
2186 -- of setting the high bound to the last operand high bound for a null
2187 -- result, thus ensuring a proper high bound in the super-flat case.
2189 N : constant Nat := List_Length (Opnds);
2190 -- Number of concatenation operands including possibly null operands
2193 -- Number of operands excluding any known to be null, except that the
2194 -- last operand is always retained, in case it provides the bounds for
2198 -- Current operand being processed in the loop through operands. After
2199 -- this loop is complete, always contains the last operand (which is not
2200 -- the same as Operands (NN), since null operands are skipped).
2202 -- Arrays describing the operands, only the first NN entries of each
2203 -- array are set (NN < N when we exclude known null operands).
2205 Is_Fixed_Length : array (1 .. N) of Boolean;
2206 -- True if length of corresponding operand known at compile time
2208 Operands : array (1 .. N) of Node_Id;
2209 -- Set to the corresponding entry in the Opnds list (but note that null
2210 -- operands are excluded, so not all entries in the list are stored).
2212 Fixed_Length : array (1 .. N) of Uint;
2213 -- Set to length of operand. Entries in this array are set only if the
2214 -- corresponding entry in Is_Fixed_Length is True.
2216 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2217 -- Set to lower bound of operand. Either an integer literal in the case
2218 -- where the bound is known at compile time, else actual lower bound.
2219 -- The operand low bound is of type Ityp.
2221 Var_Length : array (1 .. N) of Entity_Id;
2222 -- Set to an entity of type Natural that contains the length of an
2223 -- operand whose length is not known at compile time. Entries in this
2224 -- array are set only if the corresponding entry in Is_Fixed_Length
2225 -- is False. The entity is of type Artyp.
2227 Aggr_Length : array (0 .. N) of Node_Id;
2228 -- The J'th entry in an expression node that represents the total length
2229 -- of operands 1 through J. It is either an integer literal node, or a
2230 -- reference to a constant entity with the right value, so it is fine
2231 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2232 -- entry always is set to zero. The length is of type Artyp.
2234 Low_Bound : Node_Id;
2235 -- A tree node representing the low bound of the result (of type Ityp).
2236 -- This is either an integer literal node, or an identifier reference to
2237 -- a constant entity initialized to the appropriate value.
2239 Last_Opnd_High_Bound : Node_Id;
2240 -- A tree node representing the high bound of the last operand. This
2241 -- need only be set if the result could be null. It is used for the
2242 -- special case of setting the right high bound for a null result.
2243 -- This is of type Ityp.
2245 High_Bound : Node_Id;
2246 -- A tree node representing the high bound of the result (of type Ityp)
2249 -- Result of the concatenation (of type Ityp)
2251 Known_Non_Null_Operand_Seen : Boolean;
2252 -- Set True during generation of the assignements of operands into
2253 -- result once an operand known to be non-null has been seen.
2255 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2256 -- This function makes an N_Integer_Literal node that is returned in
2257 -- analyzed form with the type set to Artyp. Importantly this literal
2258 -- is not flagged as static, so that if we do computations with it that
2259 -- result in statically detected out of range conditions, we will not
2260 -- generate error messages but instead warning messages.
2262 function To_Artyp (X : Node_Id) return Node_Id;
2263 -- Given a node of type Ityp, returns the corresponding value of type
2264 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2265 -- For enum types, the Pos of the value is returned.
2267 function To_Ityp (X : Node_Id) return Node_Id;
2268 -- The inverse function (uses Val in the case of enumeration types)
2270 ------------------------
2271 -- Make_Artyp_Literal --
2272 ------------------------
2274 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2275 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2277 Set_Etype (Result, Artyp);
2278 Set_Analyzed (Result, True);
2279 Set_Is_Static_Expression (Result, False);
2281 end Make_Artyp_Literal;
2287 function To_Artyp (X : Node_Id) return Node_Id is
2289 if Ityp = Base_Type (Artyp) then
2292 elsif Is_Enumeration_Type (Ityp) then
2294 Make_Attribute_Reference (Loc,
2295 Prefix => New_Occurrence_Of (Ityp, Loc),
2296 Attribute_Name => Name_Pos,
2297 Expressions => New_List (X));
2300 return Convert_To (Artyp, X);
2308 function To_Ityp (X : Node_Id) return Node_Id is
2310 if Is_Enumeration_Type (Ityp) then
2312 Make_Attribute_Reference (Loc,
2313 Prefix => New_Occurrence_Of (Ityp, Loc),
2314 Attribute_Name => Name_Val,
2315 Expressions => New_List (X));
2317 -- Case where we will do a type conversion
2320 if Ityp = Base_Type (Artyp) then
2323 return Convert_To (Ityp, X);
2328 -- Local Declarations
2330 Opnd_Typ : Entity_Id;
2338 -- Choose an appropriate computational type
2340 -- We will be doing calculations of lengths and bounds in this routine
2341 -- and computing one from the other in some cases, e.g. getting the high
2342 -- bound by adding the length-1 to the low bound.
2344 -- We can't just use the index type, or even its base type for this
2345 -- purpose for two reasons. First it might be an enumeration type which
2346 -- is not suitable fo computations of any kind, and second it may simply
2347 -- not have enough range. For example if the index type is -128..+127
2348 -- then lengths can be up to 256, which is out of range of the type.
2350 -- For enumeration types, we can simply use Standard_Integer, this is
2351 -- sufficient since the actual number of enumeration literals cannot
2352 -- possibly exceed the range of integer (remember we will be doing the
2353 -- arithmetic with POS values, not representation values).
2355 if Is_Enumeration_Type (Ityp) then
2356 Artyp := Standard_Integer;
2358 -- If index type is Positive, we use the standard unsigned type, to give
2359 -- more room on the top of the range, obviating the need for an overflow
2360 -- check when creating the upper bound. This is needed to avoid junk
2361 -- overflow checks in the common case of String types.
2363 -- ??? Disabled for now
2365 -- elsif Istyp = Standard_Positive then
2366 -- Artyp := Standard_Unsigned;
2368 -- For modular types, we use a 32-bit modular type for types whose size
2369 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2370 -- identity type, and for larger unsigned types we use 64-bits.
2372 elsif Is_Modular_Integer_Type (Ityp) then
2373 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
2374 Artyp := Standard_Unsigned;
2375 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
2378 Artyp := RTE (RE_Long_Long_Unsigned);
2381 -- Similar treatment for signed types
2384 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
2385 Artyp := Standard_Integer;
2386 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
2389 Artyp := Standard_Long_Long_Integer;
2393 -- Supply dummy entry at start of length array
2395 Aggr_Length (0) := Make_Artyp_Literal (0);
2397 -- Go through operands setting up the above arrays
2401 Opnd := Remove_Head (Opnds);
2402 Opnd_Typ := Etype (Opnd);
2404 -- The parent got messed up when we put the operands in a list,
2405 -- so now put back the proper parent for the saved operand.
2407 Set_Parent (Opnd, Parent (Cnode));
2409 -- Set will be True when we have setup one entry in the array
2413 -- Singleton element (or character literal) case
2415 if Base_Type (Opnd_Typ) = Ctyp then
2417 Operands (NN) := Opnd;
2418 Is_Fixed_Length (NN) := True;
2419 Fixed_Length (NN) := Uint_1;
2420 Result_May_Be_Null := False;
2422 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2423 -- since we know that the result cannot be null).
2425 Opnd_Low_Bound (NN) :=
2426 Make_Attribute_Reference (Loc,
2427 Prefix => New_Reference_To (Istyp, Loc),
2428 Attribute_Name => Name_First);
2432 -- String literal case (can only occur for strings of course)
2434 elsif Nkind (Opnd) = N_String_Literal then
2435 Len := String_Literal_Length (Opnd_Typ);
2438 Result_May_Be_Null := False;
2441 -- Capture last operand high bound if result could be null
2443 if J = N and then Result_May_Be_Null then
2444 Last_Opnd_High_Bound :=
2447 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
2448 Right_Opnd => Make_Integer_Literal (Loc, 1));
2451 -- Skip null string literal
2453 if J < N and then Len = 0 then
2458 Operands (NN) := Opnd;
2459 Is_Fixed_Length (NN) := True;
2461 -- Set length and bounds
2463 Fixed_Length (NN) := Len;
2465 Opnd_Low_Bound (NN) :=
2466 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
2473 -- Check constrained case with known bounds
2475 if Is_Constrained (Opnd_Typ) then
2477 Index : constant Node_Id := First_Index (Opnd_Typ);
2478 Indx_Typ : constant Entity_Id := Etype (Index);
2479 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
2480 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
2483 -- Fixed length constrained array type with known at compile
2484 -- time bounds is last case of fixed length operand.
2486 if Compile_Time_Known_Value (Lo)
2488 Compile_Time_Known_Value (Hi)
2491 Loval : constant Uint := Expr_Value (Lo);
2492 Hival : constant Uint := Expr_Value (Hi);
2493 Len : constant Uint :=
2494 UI_Max (Hival - Loval + 1, Uint_0);
2498 Result_May_Be_Null := False;
2501 -- Capture last operand bound if result could be null
2503 if J = N and then Result_May_Be_Null then
2504 Last_Opnd_High_Bound :=
2506 Make_Integer_Literal (Loc,
2507 Intval => Expr_Value (Hi)));
2510 -- Exclude null length case unless last operand
2512 if J < N and then Len = 0 then
2517 Operands (NN) := Opnd;
2518 Is_Fixed_Length (NN) := True;
2519 Fixed_Length (NN) := Len;
2521 Opnd_Low_Bound (NN) := To_Ityp (
2522 Make_Integer_Literal (Loc,
2523 Intval => Expr_Value (Lo)));
2531 -- All cases where the length is not known at compile time, or the
2532 -- special case of an operand which is known to be null but has a
2533 -- lower bound other than 1 or is other than a string type.
2538 -- Capture operand bounds
2540 Opnd_Low_Bound (NN) :=
2541 Make_Attribute_Reference (Loc,
2543 Duplicate_Subexpr (Opnd, Name_Req => True),
2544 Attribute_Name => Name_First);
2546 if J = N and Result_May_Be_Null then
2547 Last_Opnd_High_Bound :=
2549 Make_Attribute_Reference (Loc,
2551 Duplicate_Subexpr (Opnd, Name_Req => True),
2552 Attribute_Name => Name_Last));
2555 -- Capture length of operand in entity
2557 Operands (NN) := Opnd;
2558 Is_Fixed_Length (NN) := False;
2561 Make_Defining_Identifier (Loc,
2562 Chars => New_Internal_Name ('L'));
2564 Insert_Action (Cnode,
2565 Make_Object_Declaration (Loc,
2566 Defining_Identifier => Var_Length (NN),
2567 Constant_Present => True,
2569 Object_Definition =>
2570 New_Occurrence_Of (Artyp, Loc),
2573 Make_Attribute_Reference (Loc,
2575 Duplicate_Subexpr (Opnd, Name_Req => True),
2576 Attribute_Name => Name_Length)),
2578 Suppress => All_Checks);
2582 -- Set next entry in aggregate length array
2584 -- For first entry, make either integer literal for fixed length
2585 -- or a reference to the saved length for variable length.
2588 if Is_Fixed_Length (1) then
2590 Make_Integer_Literal (Loc,
2591 Intval => Fixed_Length (1));
2594 New_Reference_To (Var_Length (1), Loc);
2597 -- If entry is fixed length and only fixed lengths so far, make
2598 -- appropriate new integer literal adding new length.
2600 elsif Is_Fixed_Length (NN)
2601 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
2604 Make_Integer_Literal (Loc,
2605 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
2607 -- All other cases, construct an addition node for the length and
2608 -- create an entity initialized to this length.
2612 Make_Defining_Identifier (Loc,
2613 Chars => New_Internal_Name ('L'));
2615 if Is_Fixed_Length (NN) then
2616 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
2618 Clen := New_Reference_To (Var_Length (NN), Loc);
2621 Insert_Action (Cnode,
2622 Make_Object_Declaration (Loc,
2623 Defining_Identifier => Ent,
2624 Constant_Present => True,
2626 Object_Definition =>
2627 New_Occurrence_Of (Artyp, Loc),
2631 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
2632 Right_Opnd => Clen)),
2634 Suppress => All_Checks);
2636 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
2643 -- If we have only skipped null operands, return the last operand
2650 -- If we have only one non-null operand, return it and we are done.
2651 -- There is one case in which this cannot be done, and that is when
2652 -- the sole operand is of the element type, in which case it must be
2653 -- converted to an array, and the easiest way of doing that is to go
2654 -- through the normal general circuit.
2657 and then Base_Type (Etype (Operands (1))) /= Ctyp
2659 Result := Operands (1);
2663 -- Cases where we have a real concatenation
2665 -- Next step is to find the low bound for the result array that we
2666 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
2668 -- If the ultimate ancestor of the index subtype is a constrained array
2669 -- definition, then the lower bound is that of the index subtype as
2670 -- specified by (RM 4.5.3(6)).
2672 -- The right test here is to go to the root type, and then the ultimate
2673 -- ancestor is the first subtype of this root type.
2675 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
2677 Make_Attribute_Reference (Loc,
2679 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
2680 Attribute_Name => Name_First);
2682 -- If the first operand in the list has known length we know that
2683 -- the lower bound of the result is the lower bound of this operand.
2685 elsif Is_Fixed_Length (1) then
2686 Low_Bound := Opnd_Low_Bound (1);
2688 -- OK, we don't know the lower bound, we have to build a horrible
2689 -- expression actions node of the form
2691 -- if Cond1'Length /= 0 then
2694 -- if Opnd2'Length /= 0 then
2699 -- The nesting ends either when we hit an operand whose length is known
2700 -- at compile time, or on reaching the last operand, whose low bound we
2701 -- take unconditionally whether or not it is null. It's easiest to do
2702 -- this with a recursive procedure:
2706 function Get_Known_Bound (J : Nat) return Node_Id;
2707 -- Returns the lower bound determined by operands J .. NN
2709 ---------------------
2710 -- Get_Known_Bound --
2711 ---------------------
2713 function Get_Known_Bound (J : Nat) return Node_Id is
2715 if Is_Fixed_Length (J) or else J = NN then
2716 return New_Copy (Opnd_Low_Bound (J));
2720 Make_Conditional_Expression (Loc,
2721 Expressions => New_List (
2724 Left_Opnd => New_Reference_To (Var_Length (J), Loc),
2725 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2727 New_Copy (Opnd_Low_Bound (J)),
2728 Get_Known_Bound (J + 1)));
2730 end Get_Known_Bound;
2734 Make_Defining_Identifier (Loc, Chars => New_Internal_Name ('L'));
2736 Insert_Action (Cnode,
2737 Make_Object_Declaration (Loc,
2738 Defining_Identifier => Ent,
2739 Constant_Present => True,
2740 Object_Definition => New_Occurrence_Of (Ityp, Loc),
2741 Expression => Get_Known_Bound (1)),
2742 Suppress => All_Checks);
2744 Low_Bound := New_Reference_To (Ent, Loc);
2748 -- Now we can safely compute the upper bound, normally
2749 -- Low_Bound + Length - 1.
2754 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2756 Make_Op_Subtract (Loc,
2757 Left_Opnd => New_Copy (Aggr_Length (NN)),
2758 Right_Opnd => Make_Artyp_Literal (1))));
2760 -- Note that calculation of the high bound may cause overflow in some
2761 -- very weird cases, so in the general case we need an overflow check on
2762 -- the high bound. We can avoid this for the common case of string types
2763 -- and other types whose index is Positive, since we chose a wider range
2764 -- for the arithmetic type.
2766 if Istyp /= Standard_Positive then
2767 Activate_Overflow_Check (High_Bound);
2770 -- Handle the exceptional case where the result is null, in which case
2771 -- case the bounds come from the last operand (so that we get the proper
2772 -- bounds if the last operand is super-flat).
2774 if Result_May_Be_Null then
2776 Make_Conditional_Expression (Loc,
2777 Expressions => New_List (
2779 Left_Opnd => New_Copy (Aggr_Length (NN)),
2780 Right_Opnd => Make_Artyp_Literal (0)),
2781 Last_Opnd_High_Bound,
2785 -- Now we construct an array object with appropriate bounds
2788 Make_Defining_Identifier (Loc,
2789 Chars => New_Internal_Name ('S'));
2791 -- If the bound is statically known to be out of range, we do not want
2792 -- to abort, we want a warning and a runtime constraint error. Note that
2793 -- we have arranged that the result will not be treated as a static
2794 -- constant, so we won't get an illegality during this insertion.
2796 Insert_Action (Cnode,
2797 Make_Object_Declaration (Loc,
2798 Defining_Identifier => Ent,
2799 Object_Definition =>
2800 Make_Subtype_Indication (Loc,
2801 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
2803 Make_Index_Or_Discriminant_Constraint (Loc,
2804 Constraints => New_List (
2806 Low_Bound => Low_Bound,
2807 High_Bound => High_Bound))))),
2808 Suppress => All_Checks);
2810 -- Catch the static out of range case now
2812 if Raises_Constraint_Error (High_Bound) then
2813 raise Concatenation_Error;
2816 -- Now we will generate the assignments to do the actual concatenation
2818 -- There is one case in which we will not do this, namely when all the
2819 -- following conditions are met:
2821 -- The result type is Standard.String
2823 -- There are nine or fewer retained (non-null) operands
2825 -- The optimization level is -O0
2827 -- The corresponding System.Concat_n.Str_Concat_n routine is
2828 -- available in the run time.
2830 -- The debug flag gnatd.c is not set
2832 -- If all these conditions are met then we generate a call to the
2833 -- relevant concatenation routine. The purpose of this is to avoid
2834 -- undesirable code bloat at -O0.
2836 if Atyp = Standard_String
2837 and then NN in 2 .. 9
2838 and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
2839 and then not Debug_Flag_Dot_C
2842 RR : constant array (Nat range 2 .. 9) of RE_Id :=
2853 if RTE_Available (RR (NN)) then
2855 Opnds : constant List_Id :=
2856 New_List (New_Occurrence_Of (Ent, Loc));
2859 for J in 1 .. NN loop
2860 if Is_List_Member (Operands (J)) then
2861 Remove (Operands (J));
2864 if Base_Type (Etype (Operands (J))) = Ctyp then
2866 Make_Aggregate (Loc,
2867 Component_Associations => New_List (
2868 Make_Component_Association (Loc,
2869 Choices => New_List (
2870 Make_Integer_Literal (Loc, 1)),
2871 Expression => Operands (J)))));
2874 Append_To (Opnds, Operands (J));
2878 Insert_Action (Cnode,
2879 Make_Procedure_Call_Statement (Loc,
2880 Name => New_Reference_To (RTE (RR (NN)), Loc),
2881 Parameter_Associations => Opnds));
2883 Result := New_Reference_To (Ent, Loc);
2890 -- Not special case so generate the assignments
2892 Known_Non_Null_Operand_Seen := False;
2894 for J in 1 .. NN loop
2896 Lo : constant Node_Id :=
2898 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2899 Right_Opnd => Aggr_Length (J - 1));
2901 Hi : constant Node_Id :=
2903 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2905 Make_Op_Subtract (Loc,
2906 Left_Opnd => Aggr_Length (J),
2907 Right_Opnd => Make_Artyp_Literal (1)));
2910 -- Singleton case, simple assignment
2912 if Base_Type (Etype (Operands (J))) = Ctyp then
2913 Known_Non_Null_Operand_Seen := True;
2914 Insert_Action (Cnode,
2915 Make_Assignment_Statement (Loc,
2917 Make_Indexed_Component (Loc,
2918 Prefix => New_Occurrence_Of (Ent, Loc),
2919 Expressions => New_List (To_Ityp (Lo))),
2920 Expression => Operands (J)),
2921 Suppress => All_Checks);
2923 -- Array case, slice assignment, skipped when argument is fixed
2924 -- length and known to be null.
2926 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
2929 Make_Assignment_Statement (Loc,
2933 New_Occurrence_Of (Ent, Loc),
2936 Low_Bound => To_Ityp (Lo),
2937 High_Bound => To_Ityp (Hi))),
2938 Expression => Operands (J));
2940 if Is_Fixed_Length (J) then
2941 Known_Non_Null_Operand_Seen := True;
2943 elsif not Known_Non_Null_Operand_Seen then
2945 -- Here if operand length is not statically known and no
2946 -- operand known to be non-null has been processed yet.
2947 -- If operand length is 0, we do not need to perform the
2948 -- assignment, and we must avoid the evaluation of the
2949 -- high bound of the slice, since it may underflow if the
2950 -- low bound is Ityp'First.
2953 Make_Implicit_If_Statement (Cnode,
2957 New_Occurrence_Of (Var_Length (J), Loc),
2958 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2963 Insert_Action (Cnode, Assign, Suppress => All_Checks);
2969 -- Finally we build the result, which is a reference to the array object
2971 Result := New_Reference_To (Ent, Loc);
2974 Rewrite (Cnode, Result);
2975 Analyze_And_Resolve (Cnode, Atyp);
2978 when Concatenation_Error =>
2980 -- Kill warning generated for the declaration of the static out of
2981 -- range high bound, and instead generate a Constraint_Error with
2982 -- an appropriate specific message.
2984 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
2985 Apply_Compile_Time_Constraint_Error
2987 Msg => "concatenation result upper bound out of range?",
2988 Reason => CE_Range_Check_Failed);
2989 -- Set_Etype (Cnode, Atyp);
2990 end Expand_Concatenate;
2992 ------------------------
2993 -- Expand_N_Allocator --
2994 ------------------------
2996 procedure Expand_N_Allocator (N : Node_Id) is
2997 PtrT : constant Entity_Id := Etype (N);
2998 Dtyp : constant Entity_Id := Designated_Type (PtrT);
2999 Etyp : constant Entity_Id := Etype (Expression (N));
3000 Loc : constant Source_Ptr := Sloc (N);
3005 procedure Complete_Coextension_Finalization;
3006 -- Generate finalization calls for all nested coextensions of N. This
3007 -- routine may allocate list controllers if necessary.
3009 procedure Rewrite_Coextension (N : Node_Id);
3010 -- Static coextensions have the same lifetime as the entity they
3011 -- constrain. Such occurrences can be rewritten as aliased objects
3012 -- and their unrestricted access used instead of the coextension.
3014 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
3015 -- Given a constrained array type E, returns a node representing the
3016 -- code to compute the size in storage elements for the given type.
3017 -- This is done without using the attribute (which malfunctions for
3020 ---------------------------------------
3021 -- Complete_Coextension_Finalization --
3022 ---------------------------------------
3024 procedure Complete_Coextension_Finalization is
3026 Coext_Elmt : Elmt_Id;
3030 function Inside_A_Return_Statement (N : Node_Id) return Boolean;
3031 -- Determine whether node N is part of a return statement
3033 function Needs_Initialization_Call (N : Node_Id) return Boolean;
3034 -- Determine whether node N is a subtype indicator allocator which
3035 -- acts a coextension. Such coextensions need initialization.
3037 -------------------------------
3038 -- Inside_A_Return_Statement --
3039 -------------------------------
3041 function Inside_A_Return_Statement (N : Node_Id) return Boolean is
3046 while Present (P) loop
3048 (P, N_Extended_Return_Statement, N_Simple_Return_Statement)
3052 -- Stop the traversal when we reach a subprogram body
3054 elsif Nkind (P) = N_Subprogram_Body then
3062 end Inside_A_Return_Statement;
3064 -------------------------------
3065 -- Needs_Initialization_Call --
3066 -------------------------------
3068 function Needs_Initialization_Call (N : Node_Id) return Boolean is
3072 if Nkind (N) = N_Explicit_Dereference
3073 and then Nkind (Prefix (N)) = N_Identifier
3074 and then Nkind (Parent (Entity (Prefix (N)))) =
3075 N_Object_Declaration
3077 Obj_Decl := Parent (Entity (Prefix (N)));
3080 Present (Expression (Obj_Decl))
3081 and then Nkind (Expression (Obj_Decl)) = N_Allocator
3082 and then Nkind (Expression (Expression (Obj_Decl))) /=
3083 N_Qualified_Expression;
3087 end Needs_Initialization_Call;
3089 -- Start of processing for Complete_Coextension_Finalization
3092 -- When a coextension root is inside a return statement, we need to
3093 -- use the finalization chain of the function's scope. This does not
3094 -- apply for controlled named access types because in those cases we
3095 -- can use the finalization chain of the type itself.
3097 if Inside_A_Return_Statement (N)
3099 (Ekind (PtrT) = E_Anonymous_Access_Type
3101 (Ekind (PtrT) = E_Access_Type
3102 and then No (Associated_Final_Chain (PtrT))))
3106 Outer_S : Entity_Id;
3107 S : Entity_Id := Current_Scope;
3110 while Present (S) and then S /= Standard_Standard loop
3111 if Ekind (S) = E_Function then
3112 Outer_S := Scope (S);
3114 -- Retrieve the declaration of the body
3119 (Corresponding_Body (Parent (Parent (S)))));
3126 -- Push the scope of the function body since we are inserting
3127 -- the list before the body, but we are currently in the body
3128 -- itself. Override the finalization list of PtrT since the
3129 -- finalization context is now different.
3131 Push_Scope (Outer_S);
3132 Build_Final_List (Decl, PtrT);
3136 -- The root allocator may not be controlled, but it still needs a
3137 -- finalization list for all nested coextensions.
3139 elsif No (Associated_Final_Chain (PtrT)) then
3140 Build_Final_List (N, PtrT);
3144 Make_Selected_Component (Loc,
3146 New_Reference_To (Associated_Final_Chain (PtrT), Loc),
3148 Make_Identifier (Loc, Name_F));
3150 Coext_Elmt := First_Elmt (Coextensions (N));
3151 while Present (Coext_Elmt) loop
3152 Coext := Node (Coext_Elmt);
3157 if Nkind (Coext) = N_Identifier then
3159 Make_Unchecked_Type_Conversion (Loc,
3160 Subtype_Mark => New_Reference_To (Etype (Coext), Loc),
3162 Make_Explicit_Dereference (Loc,
3163 Prefix => New_Copy_Tree (Coext)));
3165 Ref := New_Copy_Tree (Coext);
3168 -- No initialization call if not allowed
3170 Check_Restriction (No_Default_Initialization, N);
3172 if not Restriction_Active (No_Default_Initialization) then
3176 -- attach_to_final_list (Ref, Flist, 2)
3178 if Needs_Initialization_Call (Coext) then
3182 Typ => Etype (Coext),
3184 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3187 -- attach_to_final_list (Ref, Flist, 2)
3193 Flist_Ref => New_Copy_Tree (Flist),
3194 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3198 Next_Elmt (Coext_Elmt);
3200 end Complete_Coextension_Finalization;
3202 -------------------------
3203 -- Rewrite_Coextension --
3204 -------------------------
3206 procedure Rewrite_Coextension (N : Node_Id) is
3207 Temp : constant Node_Id :=
3208 Make_Defining_Identifier (Loc,
3209 New_Internal_Name ('C'));
3212 -- Cnn : aliased Etyp;
3214 Decl : constant Node_Id :=
3215 Make_Object_Declaration (Loc,
3216 Defining_Identifier => Temp,
3217 Aliased_Present => True,
3218 Object_Definition =>
3219 New_Occurrence_Of (Etyp, Loc));
3223 if Nkind (Expression (N)) = N_Qualified_Expression then
3224 Set_Expression (Decl, Expression (Expression (N)));
3227 -- Find the proper insertion node for the declaration
3230 while Present (Nod) loop
3231 exit when Nkind (Nod) in N_Statement_Other_Than_Procedure_Call
3232 or else Nkind (Nod) = N_Procedure_Call_Statement
3233 or else Nkind (Nod) in N_Declaration;
3234 Nod := Parent (Nod);
3237 Insert_Before (Nod, Decl);
3241 Make_Attribute_Reference (Loc,
3242 Prefix => New_Occurrence_Of (Temp, Loc),
3243 Attribute_Name => Name_Unrestricted_Access));
3245 Analyze_And_Resolve (N, PtrT);
3246 end Rewrite_Coextension;
3248 ------------------------------
3249 -- Size_In_Storage_Elements --
3250 ------------------------------
3252 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
3254 -- Logically this just returns E'Max_Size_In_Storage_Elements.
3255 -- However, the reason for the existence of this function is
3256 -- to construct a test for sizes too large, which means near the
3257 -- 32-bit limit on a 32-bit machine, and precisely the trouble
3258 -- is that we get overflows when sizes are greater than 2**31.
3260 -- So what we end up doing for array types is to use the expression:
3262 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
3264 -- which avoids this problem. All this is a big bogus, but it does
3265 -- mean we catch common cases of trying to allocate arrays that
3266 -- are too large, and which in the absence of a check results in
3267 -- undetected chaos ???
3274 for J in 1 .. Number_Dimensions (E) loop
3276 Make_Attribute_Reference (Loc,
3277 Prefix => New_Occurrence_Of (E, Loc),
3278 Attribute_Name => Name_Length,
3279 Expressions => New_List (
3280 Make_Integer_Literal (Loc, J)));
3287 Make_Op_Multiply (Loc,
3294 Make_Op_Multiply (Loc,
3297 Make_Attribute_Reference (Loc,
3298 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
3299 Attribute_Name => Name_Max_Size_In_Storage_Elements));
3301 end Size_In_Storage_Elements;
3303 -- Start of processing for Expand_N_Allocator
3306 -- RM E.2.3(22). We enforce that the expected type of an allocator
3307 -- shall not be a remote access-to-class-wide-limited-private type
3309 -- Why is this being done at expansion time, seems clearly wrong ???
3311 Validate_Remote_Access_To_Class_Wide_Type (N);
3313 -- Set the Storage Pool
3315 Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT)));
3317 if Present (Storage_Pool (N)) then
3318 if Is_RTE (Storage_Pool (N), RE_SS_Pool) then
3319 if VM_Target = No_VM then
3320 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3323 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
3324 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
3327 Set_Procedure_To_Call (N,
3328 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
3332 -- Under certain circumstances we can replace an allocator by an access
3333 -- to statically allocated storage. The conditions, as noted in AARM
3334 -- 3.10 (10c) are as follows:
3336 -- Size and initial value is known at compile time
3337 -- Access type is access-to-constant
3339 -- The allocator is not part of a constraint on a record component,
3340 -- because in that case the inserted actions are delayed until the
3341 -- record declaration is fully analyzed, which is too late for the
3342 -- analysis of the rewritten allocator.
3344 if Is_Access_Constant (PtrT)
3345 and then Nkind (Expression (N)) = N_Qualified_Expression
3346 and then Compile_Time_Known_Value (Expression (Expression (N)))
3347 and then Size_Known_At_Compile_Time (Etype (Expression
3349 and then not Is_Record_Type (Current_Scope)
3351 -- Here we can do the optimization. For the allocator
3355 -- We insert an object declaration
3357 -- Tnn : aliased x := y;
3359 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
3360 -- marked as requiring static allocation.
3363 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
3365 Desig := Subtype_Mark (Expression (N));
3367 -- If context is constrained, use constrained subtype directly,
3368 -- so that the constant is not labelled as having a nominally
3369 -- unconstrained subtype.
3371 if Entity (Desig) = Base_Type (Dtyp) then
3372 Desig := New_Occurrence_Of (Dtyp, Loc);
3376 Make_Object_Declaration (Loc,
3377 Defining_Identifier => Temp,
3378 Aliased_Present => True,
3379 Constant_Present => Is_Access_Constant (PtrT),
3380 Object_Definition => Desig,
3381 Expression => Expression (Expression (N))));
3384 Make_Attribute_Reference (Loc,
3385 Prefix => New_Occurrence_Of (Temp, Loc),
3386 Attribute_Name => Name_Unrestricted_Access));
3388 Analyze_And_Resolve (N, PtrT);
3390 -- We set the variable as statically allocated, since we don't want
3391 -- it going on the stack of the current procedure!
3393 Set_Is_Statically_Allocated (Temp);
3397 -- Same if the allocator is an access discriminant for a local object:
3398 -- instead of an allocator we create a local value and constrain the
3399 -- the enclosing object with the corresponding access attribute.
3401 if Is_Static_Coextension (N) then
3402 Rewrite_Coextension (N);
3406 -- The current allocator creates an object which may contain nested
3407 -- coextensions. Use the current allocator's finalization list to
3408 -- generate finalization call for all nested coextensions.
3410 if Is_Coextension_Root (N) then
3411 Complete_Coextension_Finalization;
3414 -- Check for size too large, we do this because the back end misses
3415 -- proper checks here and can generate rubbish allocation calls when
3416 -- we are near the limit. We only do this for the 32-bit address case
3417 -- since that is from a practical point of view where we see a problem.
3419 if System_Address_Size = 32
3420 and then not Storage_Checks_Suppressed (PtrT)
3421 and then not Storage_Checks_Suppressed (Dtyp)
3422 and then not Storage_Checks_Suppressed (Etyp)
3424 -- The check we want to generate should look like
3426 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
3427 -- raise Storage_Error;
3430 -- where 3.5 gigabytes is a constant large enough to accomodate any
3431 -- reasonable request for. But we can't do it this way because at
3432 -- least at the moment we don't compute this attribute right, and
3433 -- can silently give wrong results when the result gets large. Since
3434 -- this is all about large results, that's bad, so instead we only
3435 -- apply the check for constrained arrays, and manually compute the
3436 -- value of the attribute ???
3438 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
3440 Make_Raise_Storage_Error (Loc,
3443 Left_Opnd => Size_In_Storage_Elements (Etyp),
3445 Make_Integer_Literal (Loc,
3446 Intval => Uint_7 * (Uint_2 ** 29))),
3447 Reason => SE_Object_Too_Large));
3451 -- Handle case of qualified expression (other than optimization above)
3452 -- First apply constraint checks, because the bounds or discriminants
3453 -- in the aggregate might not match the subtype mark in the allocator.
3455 if Nkind (Expression (N)) = N_Qualified_Expression then
3456 Apply_Constraint_Check
3457 (Expression (Expression (N)), Etype (Expression (N)));
3459 Expand_Allocator_Expression (N);
3463 -- If the allocator is for a type which requires initialization, and
3464 -- there is no initial value (i.e. operand is a subtype indication
3465 -- rather than a qualified expression), then we must generate a call to
3466 -- the initialization routine using an expressions action node:
3468 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
3470 -- Here ptr_T is the pointer type for the allocator, and T is the
3471 -- subtype of the allocator. A special case arises if the designated
3472 -- type of the access type is a task or contains tasks. In this case
3473 -- the call to Init (Temp.all ...) is replaced by code that ensures
3474 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
3475 -- for details). In addition, if the type T is a task T, then the
3476 -- first argument to Init must be converted to the task record type.
3479 T : constant Entity_Id := Entity (Expression (N));
3487 Temp_Decl : Node_Id;
3488 Temp_Type : Entity_Id;
3489 Attach_Level : Uint;
3492 if No_Initialization (N) then
3495 -- Case of no initialization procedure present
3497 elsif not Has_Non_Null_Base_Init_Proc (T) then
3499 -- Case of simple initialization required
3501 if Needs_Simple_Initialization (T) then
3502 Check_Restriction (No_Default_Initialization, N);
3503 Rewrite (Expression (N),
3504 Make_Qualified_Expression (Loc,
3505 Subtype_Mark => New_Occurrence_Of (T, Loc),
3506 Expression => Get_Simple_Init_Val (T, N)));
3508 Analyze_And_Resolve (Expression (Expression (N)), T);
3509 Analyze_And_Resolve (Expression (N), T);
3510 Set_Paren_Count (Expression (Expression (N)), 1);
3511 Expand_N_Allocator (N);
3513 -- No initialization required
3519 -- Case of initialization procedure present, must be called
3522 Check_Restriction (No_Default_Initialization, N);
3524 if not Restriction_Active (No_Default_Initialization) then
3525 Init := Base_Init_Proc (T);
3527 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
3529 -- Construct argument list for the initialization routine call
3532 Make_Explicit_Dereference (Loc,
3533 Prefix => New_Reference_To (Temp, Loc));
3534 Set_Assignment_OK (Arg1);
3537 -- The initialization procedure expects a specific type. if the
3538 -- context is access to class wide, indicate that the object
3539 -- being allocated has the right specific type.
3541 if Is_Class_Wide_Type (Dtyp) then
3542 Arg1 := Unchecked_Convert_To (T, Arg1);
3545 -- If designated type is a concurrent type or if it is private
3546 -- type whose definition is a concurrent type, the first
3547 -- argument in the Init routine has to be unchecked conversion
3548 -- to the corresponding record type. If the designated type is
3549 -- a derived type, we also convert the argument to its root
3552 if Is_Concurrent_Type (T) then
3554 Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1);
3556 elsif Is_Private_Type (T)
3557 and then Present (Full_View (T))
3558 and then Is_Concurrent_Type (Full_View (T))
3561 Unchecked_Convert_To
3562 (Corresponding_Record_Type (Full_View (T)), Arg1);
3564 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
3566 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
3568 Arg1 := OK_Convert_To (Etype (Ftyp), Arg1);
3569 Set_Etype (Arg1, Ftyp);
3573 Args := New_List (Arg1);
3575 -- For the task case, pass the Master_Id of the access type as
3576 -- the value of the _Master parameter, and _Chain as the value
3577 -- of the _Chain parameter (_Chain will be defined as part of
3578 -- the generated code for the allocator).
3580 -- In Ada 2005, the context may be a function that returns an
3581 -- anonymous access type. In that case the Master_Id has been
3582 -- created when expanding the function declaration.
3584 if Has_Task (T) then
3585 if No (Master_Id (Base_Type (PtrT))) then
3587 -- If we have a non-library level task with restriction
3588 -- No_Task_Hierarchy set, then no point in expanding.
3590 if not Is_Library_Level_Entity (T)
3591 and then Restriction_Active (No_Task_Hierarchy)
3596 -- The designated type was an incomplete type, and the
3597 -- access type did not get expanded. Salvage it now.
3599 pragma Assert (Present (Parent (Base_Type (PtrT))));
3600 Expand_N_Full_Type_Declaration
3601 (Parent (Base_Type (PtrT)));
3604 -- If the context of the allocator is a declaration or an
3605 -- assignment, we can generate a meaningful image for it,
3606 -- even though subsequent assignments might remove the
3607 -- connection between task and entity. We build this image
3608 -- when the left-hand side is a simple variable, a simple
3609 -- indexed assignment or a simple selected component.
3611 if Nkind (Parent (N)) = N_Assignment_Statement then
3613 Nam : constant Node_Id := Name (Parent (N));
3616 if Is_Entity_Name (Nam) then
3618 Build_Task_Image_Decls
3621 (Entity (Nam), Sloc (Nam)), T);
3624 (Nam, N_Indexed_Component, N_Selected_Component)
3625 and then Is_Entity_Name (Prefix (Nam))
3628 Build_Task_Image_Decls
3629 (Loc, Nam, Etype (Prefix (Nam)));
3631 Decls := Build_Task_Image_Decls (Loc, T, T);
3635 elsif Nkind (Parent (N)) = N_Object_Declaration then
3637 Build_Task_Image_Decls
3638 (Loc, Defining_Identifier (Parent (N)), T);
3641 Decls := Build_Task_Image_Decls (Loc, T, T);
3646 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
3647 Append_To (Args, Make_Identifier (Loc, Name_uChain));
3649 Decl := Last (Decls);
3651 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
3653 -- Has_Task is false, Decls not used
3659 -- Add discriminants if discriminated type
3662 Dis : Boolean := False;
3666 if Has_Discriminants (T) then
3670 elsif Is_Private_Type (T)
3671 and then Present (Full_View (T))
3672 and then Has_Discriminants (Full_View (T))
3675 Typ := Full_View (T);
3680 -- If the allocated object will be constrained by the
3681 -- default values for discriminants, then build a subtype
3682 -- with those defaults, and change the allocated subtype
3683 -- to that. Note that this happens in fewer cases in Ada
3686 if not Is_Constrained (Typ)
3687 and then Present (Discriminant_Default_Value
3688 (First_Discriminant (Typ)))
3689 and then (Ada_Version < Ada_05
3691 not Has_Constrained_Partial_View (Typ))
3693 Typ := Build_Default_Subtype (Typ, N);
3694 Set_Expression (N, New_Reference_To (Typ, Loc));
3697 Discr := First_Elmt (Discriminant_Constraint (Typ));
3698 while Present (Discr) loop
3699 Nod := Node (Discr);
3700 Append (New_Copy_Tree (Node (Discr)), Args);
3702 -- AI-416: when the discriminant constraint is an
3703 -- anonymous access type make sure an accessibility
3704 -- check is inserted if necessary (3.10.2(22.q/2))
3706 if Ada_Version >= Ada_05
3708 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
3710 Apply_Accessibility_Check
3711 (Nod, Typ, Insert_Node => Nod);
3719 -- We set the allocator as analyzed so that when we analyze the
3720 -- expression actions node, we do not get an unwanted recursive
3721 -- expansion of the allocator expression.
3723 Set_Analyzed (N, True);
3724 Nod := Relocate_Node (N);
3726 -- Here is the transformation:
3728 -- output: Temp : constant ptr_T := new T;
3729 -- Init (Temp.all, ...);
3730 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
3731 -- <CTRL> Initialize (Finalizable (Temp.all));
3733 -- Here ptr_T is the pointer type for the allocator, and is the
3734 -- subtype of the allocator.
3737 Make_Object_Declaration (Loc,
3738 Defining_Identifier => Temp,
3739 Constant_Present => True,
3740 Object_Definition => New_Reference_To (Temp_Type, Loc),
3743 Set_Assignment_OK (Temp_Decl);
3744 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
3746 -- If the designated type is a task type or contains tasks,
3747 -- create block to activate created tasks, and insert
3748 -- declaration for Task_Image variable ahead of call.
3750 if Has_Task (T) then
3752 L : constant List_Id := New_List;
3755 Build_Task_Allocate_Block (L, Nod, Args);
3757 Insert_List_Before (First (Declarations (Blk)), Decls);
3758 Insert_Actions (N, L);
3763 Make_Procedure_Call_Statement (Loc,
3764 Name => New_Reference_To (Init, Loc),
3765 Parameter_Associations => Args));
3768 if Needs_Finalization (T) then
3770 -- Postpone the generation of a finalization call for the
3771 -- current allocator if it acts as a coextension.
3773 if Is_Dynamic_Coextension (N) then
3774 if No (Coextensions (N)) then
3775 Set_Coextensions (N, New_Elmt_List);
3778 Append_Elmt (New_Copy_Tree (Arg1), Coextensions (N));
3782 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
3784 -- Anonymous access types created for access parameters
3785 -- are attached to an explicitly constructed controller,
3786 -- which ensures that they can be finalized properly,
3787 -- even if their deallocation might not happen. The list
3788 -- associated with the controller is doubly-linked. For
3789 -- other anonymous access types, the object may end up
3790 -- on the global final list which is singly-linked.
3791 -- Work needed for access discriminants in Ada 2005 ???
3793 if Ekind (PtrT) = E_Anonymous_Access_Type then
3794 Attach_Level := Uint_1;
3796 Attach_Level := Uint_2;
3801 Ref => New_Copy_Tree (Arg1),
3804 With_Attach => Make_Integer_Literal (Loc,
3805 Intval => Attach_Level)));
3809 Rewrite (N, New_Reference_To (Temp, Loc));
3810 Analyze_And_Resolve (N, PtrT);
3815 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
3816 -- object that has been rewritten as a reference, we displace "this"
3817 -- to reference properly its secondary dispatch table.
3819 if Nkind (N) = N_Identifier
3820 and then Is_Interface (Dtyp)
3822 Displace_Allocator_Pointer (N);
3826 when RE_Not_Available =>
3828 end Expand_N_Allocator;
3830 -----------------------
3831 -- Expand_N_And_Then --
3832 -----------------------
3834 -- Expand into conditional expression if Actions present, and also deal
3835 -- with optimizing case of arguments being True or False.
3837 procedure Expand_N_And_Then (N : Node_Id) is
3838 Loc : constant Source_Ptr := Sloc (N);
3839 Typ : constant Entity_Id := Etype (N);
3840 Left : constant Node_Id := Left_Opnd (N);
3841 Right : constant Node_Id := Right_Opnd (N);
3845 -- Deal with non-standard booleans
3847 if Is_Boolean_Type (Typ) then
3848 Adjust_Condition (Left);
3849 Adjust_Condition (Right);
3850 Set_Etype (N, Standard_Boolean);
3853 -- Check for cases where left argument is known to be True or False
3855 if Compile_Time_Known_Value (Left) then
3857 -- If left argument is True, change (True and then Right) to Right.
3858 -- Any actions associated with Right will be executed unconditionally
3859 -- and can thus be inserted into the tree unconditionally.
3861 if Expr_Value_E (Left) = Standard_True then
3862 if Present (Actions (N)) then
3863 Insert_Actions (N, Actions (N));
3868 -- If left argument is False, change (False and then Right) to False.
3869 -- In this case we can forget the actions associated with Right,
3870 -- since they will never be executed.
3872 else pragma Assert (Expr_Value_E (Left) = Standard_False);
3873 Kill_Dead_Code (Right);
3874 Kill_Dead_Code (Actions (N));
3875 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3878 Adjust_Result_Type (N, Typ);
3882 -- If Actions are present, we expand
3884 -- left and then right
3888 -- if left then right else false end
3890 -- with the actions becoming the Then_Actions of the conditional
3891 -- expression. This conditional expression is then further expanded
3892 -- (and will eventually disappear)
3894 if Present (Actions (N)) then
3895 Actlist := Actions (N);
3897 Make_Conditional_Expression (Loc,
3898 Expressions => New_List (
3901 New_Occurrence_Of (Standard_False, Loc))));
3903 Set_Then_Actions (N, Actlist);
3904 Analyze_And_Resolve (N, Standard_Boolean);
3905 Adjust_Result_Type (N, Typ);
3909 -- No actions present, check for cases of right argument True/False
3911 if Compile_Time_Known_Value (Right) then
3913 -- Change (Left and then True) to Left. Note that we know there are
3914 -- no actions associated with the True operand, since we just checked
3915 -- for this case above.
3917 if Expr_Value_E (Right) = Standard_True then
3920 -- Change (Left and then False) to False, making sure to preserve any
3921 -- side effects associated with the Left operand.
3923 else pragma Assert (Expr_Value_E (Right) = Standard_False);
3924 Remove_Side_Effects (Left);
3926 (N, New_Occurrence_Of (Standard_False, Loc));
3930 Adjust_Result_Type (N, Typ);
3931 end Expand_N_And_Then;
3933 -------------------------------------
3934 -- Expand_N_Conditional_Expression --
3935 -------------------------------------
3937 -- Expand into expression actions if then/else actions present
3939 procedure Expand_N_Conditional_Expression (N : Node_Id) is
3940 Loc : constant Source_Ptr := Sloc (N);
3941 Cond : constant Node_Id := First (Expressions (N));
3942 Thenx : constant Node_Id := Next (Cond);
3943 Elsex : constant Node_Id := Next (Thenx);
3944 Typ : constant Entity_Id := Etype (N);
3949 -- If either then or else actions are present, then given:
3951 -- if cond then then-expr else else-expr end
3953 -- we insert the following sequence of actions (using Insert_Actions):
3958 -- Cnn := then-expr;
3964 -- and replace the conditional expression by a reference to Cnn
3966 if Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
3967 Cnn := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
3970 Make_Implicit_If_Statement (N,
3971 Condition => Relocate_Node (Cond),
3973 Then_Statements => New_List (
3974 Make_Assignment_Statement (Sloc (Thenx),
3975 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
3976 Expression => Relocate_Node (Thenx))),
3978 Else_Statements => New_List (
3979 Make_Assignment_Statement (Sloc (Elsex),
3980 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
3981 Expression => Relocate_Node (Elsex))));
3983 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
3984 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
3986 if Present (Then_Actions (N)) then
3988 (First (Then_Statements (New_If)), Then_Actions (N));
3991 if Present (Else_Actions (N)) then
3993 (First (Else_Statements (New_If)), Else_Actions (N));
3996 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
3999 Make_Object_Declaration (Loc,
4000 Defining_Identifier => Cnn,
4001 Object_Definition => New_Occurrence_Of (Typ, Loc)));
4003 Insert_Action (N, New_If);
4004 Analyze_And_Resolve (N, Typ);
4006 end Expand_N_Conditional_Expression;
4008 -----------------------------------
4009 -- Expand_N_Explicit_Dereference --
4010 -----------------------------------
4012 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
4014 -- Insert explicit dereference call for the checked storage pool case
4016 Insert_Dereference_Action (Prefix (N));
4017 end Expand_N_Explicit_Dereference;
4023 procedure Expand_N_In (N : Node_Id) is
4024 Loc : constant Source_Ptr := Sloc (N);
4025 Rtyp : constant Entity_Id := Etype (N);
4026 Lop : constant Node_Id := Left_Opnd (N);
4027 Rop : constant Node_Id := Right_Opnd (N);
4028 Static : constant Boolean := Is_OK_Static_Expression (N);
4030 procedure Substitute_Valid_Check;
4031 -- Replaces node N by Lop'Valid. This is done when we have an explicit
4032 -- test for the left operand being in range of its subtype.
4034 ----------------------------
4035 -- Substitute_Valid_Check --
4036 ----------------------------
4038 procedure Substitute_Valid_Check is
4041 Make_Attribute_Reference (Loc,
4042 Prefix => Relocate_Node (Lop),
4043 Attribute_Name => Name_Valid));
4045 Analyze_And_Resolve (N, Rtyp);
4047 Error_Msg_N ("?explicit membership test may be optimized away", N);
4048 Error_Msg_N ("\?use ''Valid attribute instead", N);
4050 end Substitute_Valid_Check;
4052 -- Start of processing for Expand_N_In
4055 -- Check case of explicit test for an expression in range of its
4056 -- subtype. This is suspicious usage and we replace it with a 'Valid
4057 -- test and give a warning.
4059 if Is_Scalar_Type (Etype (Lop))
4060 and then Nkind (Rop) in N_Has_Entity
4061 and then Etype (Lop) = Entity (Rop)
4062 and then Comes_From_Source (N)
4063 and then VM_Target = No_VM
4065 Substitute_Valid_Check;
4069 -- Do validity check on operands
4071 if Validity_Checks_On and Validity_Check_Operands then
4072 Ensure_Valid (Left_Opnd (N));
4073 Validity_Check_Range (Right_Opnd (N));
4076 -- Case of explicit range
4078 if Nkind (Rop) = N_Range then
4080 Lo : constant Node_Id := Low_Bound (Rop);
4081 Hi : constant Node_Id := High_Bound (Rop);
4083 Ltyp : constant Entity_Id := Etype (Lop);
4085 Lo_Orig : constant Node_Id := Original_Node (Lo);
4086 Hi_Orig : constant Node_Id := Original_Node (Hi);
4088 Lcheck : Compare_Result;
4089 Ucheck : Compare_Result;
4091 Warn1 : constant Boolean :=
4092 Constant_Condition_Warnings
4093 and then Comes_From_Source (N)
4094 and then not In_Instance;
4095 -- This must be true for any of the optimization warnings, we
4096 -- clearly want to give them only for source with the flag on.
4097 -- We also skip these warnings in an instance since it may be
4098 -- the case that different instantiations have different ranges.
4100 Warn2 : constant Boolean :=
4102 and then Nkind (Original_Node (Rop)) = N_Range
4103 and then Is_Integer_Type (Etype (Lo));
4104 -- For the case where only one bound warning is elided, we also
4105 -- insist on an explicit range and an integer type. The reason is
4106 -- that the use of enumeration ranges including an end point is
4107 -- common, as is the use of a subtype name, one of whose bounds
4108 -- is the same as the type of the expression.
4111 -- If test is explicit x'first .. x'last, replace by valid check
4113 if Is_Scalar_Type (Ltyp)
4114 and then Nkind (Lo_Orig) = N_Attribute_Reference
4115 and then Attribute_Name (Lo_Orig) = Name_First
4116 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
4117 and then Entity (Prefix (Lo_Orig)) = Ltyp
4118 and then Nkind (Hi_Orig) = N_Attribute_Reference
4119 and then Attribute_Name (Hi_Orig) = Name_Last
4120 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
4121 and then Entity (Prefix (Hi_Orig)) = Ltyp
4122 and then Comes_From_Source (N)
4123 and then VM_Target = No_VM
4125 Substitute_Valid_Check;
4129 -- If bounds of type are known at compile time, and the end points
4130 -- are known at compile time and identical, this is another case
4131 -- for substituting a valid test. We only do this for discrete
4132 -- types, since it won't arise in practice for float types.
4134 if Comes_From_Source (N)
4135 and then Is_Discrete_Type (Ltyp)
4136 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
4137 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
4138 and then Compile_Time_Known_Value (Lo)
4139 and then Compile_Time_Known_Value (Hi)
4140 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
4141 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
4143 -- Kill warnings in instances, since they may be cases where we
4144 -- have a test in the generic that makes sense with some types
4145 -- and not with other types.
4147 and then not In_Instance
4149 Substitute_Valid_Check;
4153 -- If we have an explicit range, do a bit of optimization based
4154 -- on range analysis (we may be able to kill one or both checks).
4156 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
4157 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
4159 -- If either check is known to fail, replace result by False since
4160 -- the other check does not matter. Preserve the static flag for
4161 -- legality checks, because we are constant-folding beyond RM 4.9.
4163 if Lcheck = LT or else Ucheck = GT then
4165 Error_Msg_N ("?range test optimized away", N);
4166 Error_Msg_N ("\?value is known to be out of range", N);
4170 New_Reference_To (Standard_False, Loc));
4171 Analyze_And_Resolve (N, Rtyp);
4172 Set_Is_Static_Expression (N, Static);
4176 -- If both checks are known to succeed, replace result by True,
4177 -- since we know we are in range.
4179 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4181 Error_Msg_N ("?range test optimized away", N);
4182 Error_Msg_N ("\?value is known to be in range", N);
4186 New_Reference_To (Standard_True, Loc));
4187 Analyze_And_Resolve (N, Rtyp);
4188 Set_Is_Static_Expression (N, Static);
4192 -- If lower bound check succeeds and upper bound check is not
4193 -- known to succeed or fail, then replace the range check with
4194 -- a comparison against the upper bound.
4196 elsif Lcheck in Compare_GE then
4197 if Warn2 and then not In_Instance then
4198 Error_Msg_N ("?lower bound test optimized away", Lo);
4199 Error_Msg_N ("\?value is known to be in range", Lo);
4205 Right_Opnd => High_Bound (Rop)));
4206 Analyze_And_Resolve (N, Rtyp);
4210 -- If upper bound check succeeds and lower bound check is not
4211 -- known to succeed or fail, then replace the range check with
4212 -- a comparison against the lower bound.
4214 elsif Ucheck in Compare_LE then
4215 if Warn2 and then not In_Instance then
4216 Error_Msg_N ("?upper bound test optimized away", Hi);
4217 Error_Msg_N ("\?value is known to be in range", Hi);
4223 Right_Opnd => Low_Bound (Rop)));
4224 Analyze_And_Resolve (N, Rtyp);
4229 -- We couldn't optimize away the range check, but there is one
4230 -- more issue. If we are checking constant conditionals, then we
4231 -- see if we can determine the outcome assuming everything is
4232 -- valid, and if so give an appropriate warning.
4234 if Warn1 and then not Assume_No_Invalid_Values then
4235 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
4236 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
4238 -- Result is out of range for valid value
4240 if Lcheck = LT or else Ucheck = GT then
4242 ("?value can only be in range if it is invalid", N);
4244 -- Result is in range for valid value
4246 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4248 ("?value can only be out of range if it is invalid", N);
4250 -- Lower bound check succeeds if value is valid
4252 elsif Warn2 and then Lcheck in Compare_GE then
4254 ("?lower bound check only fails if it is invalid", Lo);
4256 -- Upper bound check succeeds if value is valid
4258 elsif Warn2 and then Ucheck in Compare_LE then
4260 ("?upper bound check only fails for invalid values", Hi);
4265 -- For all other cases of an explicit range, nothing to be done
4269 -- Here right operand is a subtype mark
4273 Typ : Entity_Id := Etype (Rop);
4274 Is_Acc : constant Boolean := Is_Access_Type (Typ);
4275 Obj : Node_Id := Lop;
4276 Cond : Node_Id := Empty;
4279 Remove_Side_Effects (Obj);
4281 -- For tagged type, do tagged membership operation
4283 if Is_Tagged_Type (Typ) then
4285 -- No expansion will be performed when VM_Target, as the VM
4286 -- back-ends will handle the membership tests directly (tags
4287 -- are not explicitly represented in Java objects, so the
4288 -- normal tagged membership expansion is not what we want).
4290 if VM_Target = No_VM then
4291 Rewrite (N, Tagged_Membership (N));
4292 Analyze_And_Resolve (N, Rtyp);
4297 -- If type is scalar type, rewrite as x in t'first .. t'last.
4298 -- This reason we do this is that the bounds may have the wrong
4299 -- type if they come from the original type definition. Also this
4300 -- way we get all the processing above for an explicit range.
4302 elsif Is_Scalar_Type (Typ) then
4306 Make_Attribute_Reference (Loc,
4307 Attribute_Name => Name_First,
4308 Prefix => New_Reference_To (Typ, Loc)),
4311 Make_Attribute_Reference (Loc,
4312 Attribute_Name => Name_Last,
4313 Prefix => New_Reference_To (Typ, Loc))));
4314 Analyze_And_Resolve (N, Rtyp);
4317 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4318 -- a membership test if the subtype mark denotes a constrained
4319 -- Unchecked_Union subtype and the expression lacks inferable
4322 elsif Is_Unchecked_Union (Base_Type (Typ))
4323 and then Is_Constrained (Typ)
4324 and then not Has_Inferable_Discriminants (Lop)
4327 Make_Raise_Program_Error (Loc,
4328 Reason => PE_Unchecked_Union_Restriction));
4330 -- Prevent Gigi from generating incorrect code by rewriting
4331 -- the test as a standard False.
4334 New_Occurrence_Of (Standard_False, Loc));
4339 -- Here we have a non-scalar type
4342 Typ := Designated_Type (Typ);
4345 if not Is_Constrained (Typ) then
4347 New_Reference_To (Standard_True, Loc));
4348 Analyze_And_Resolve (N, Rtyp);
4350 -- For the constrained array case, we have to check the subscripts
4351 -- for an exact match if the lengths are non-zero (the lengths
4352 -- must match in any case).
4354 elsif Is_Array_Type (Typ) then
4356 Check_Subscripts : declare
4357 function Construct_Attribute_Reference
4360 Dim : Nat) return Node_Id;
4361 -- Build attribute reference E'Nam(Dim)
4363 -----------------------------------
4364 -- Construct_Attribute_Reference --
4365 -----------------------------------
4367 function Construct_Attribute_Reference
4370 Dim : Nat) return Node_Id
4374 Make_Attribute_Reference (Loc,
4376 Attribute_Name => Nam,
4377 Expressions => New_List (
4378 Make_Integer_Literal (Loc, Dim)));
4379 end Construct_Attribute_Reference;
4381 -- Start of processing for Check_Subscripts
4384 for J in 1 .. Number_Dimensions (Typ) loop
4385 Evolve_And_Then (Cond,
4388 Construct_Attribute_Reference
4389 (Duplicate_Subexpr_No_Checks (Obj),
4392 Construct_Attribute_Reference
4393 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
4395 Evolve_And_Then (Cond,
4398 Construct_Attribute_Reference
4399 (Duplicate_Subexpr_No_Checks (Obj),
4402 Construct_Attribute_Reference
4403 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
4412 Right_Opnd => Make_Null (Loc)),
4413 Right_Opnd => Cond);
4417 Analyze_And_Resolve (N, Rtyp);
4418 end Check_Subscripts;
4420 -- These are the cases where constraint checks may be required,
4421 -- e.g. records with possible discriminants
4424 -- Expand the test into a series of discriminant comparisons.
4425 -- The expression that is built is the negation of the one that
4426 -- is used for checking discriminant constraints.
4428 Obj := Relocate_Node (Left_Opnd (N));
4430 if Has_Discriminants (Typ) then
4431 Cond := Make_Op_Not (Loc,
4432 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
4435 Cond := Make_Or_Else (Loc,
4439 Right_Opnd => Make_Null (Loc)),
4440 Right_Opnd => Cond);
4444 Cond := New_Occurrence_Of (Standard_True, Loc);
4448 Analyze_And_Resolve (N, Rtyp);
4454 --------------------------------
4455 -- Expand_N_Indexed_Component --
4456 --------------------------------
4458 procedure Expand_N_Indexed_Component (N : Node_Id) is
4459 Loc : constant Source_Ptr := Sloc (N);
4460 Typ : constant Entity_Id := Etype (N);
4461 P : constant Node_Id := Prefix (N);
4462 T : constant Entity_Id := Etype (P);
4465 -- A special optimization, if we have an indexed component that is
4466 -- selecting from a slice, then we can eliminate the slice, since, for
4467 -- example, x (i .. j)(k) is identical to x(k). The only difference is
4468 -- the range check required by the slice. The range check for the slice
4469 -- itself has already been generated. The range check for the
4470 -- subscripting operation is ensured by converting the subject to
4471 -- the subtype of the slice.
4473 -- This optimization not only generates better code, avoiding slice
4474 -- messing especially in the packed case, but more importantly bypasses
4475 -- some problems in handling this peculiar case, for example, the issue
4476 -- of dealing specially with object renamings.
4478 if Nkind (P) = N_Slice then
4480 Make_Indexed_Component (Loc,
4481 Prefix => Prefix (P),
4482 Expressions => New_List (
4484 (Etype (First_Index (Etype (P))),
4485 First (Expressions (N))))));
4486 Analyze_And_Resolve (N, Typ);
4490 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
4491 -- function, then additional actuals must be passed.
4493 if Ada_Version >= Ada_05
4494 and then Is_Build_In_Place_Function_Call (P)
4496 Make_Build_In_Place_Call_In_Anonymous_Context (P);
4499 -- If the prefix is an access type, then we unconditionally rewrite if
4500 -- as an explicit deference. This simplifies processing for several
4501 -- cases, including packed array cases and certain cases in which checks
4502 -- must be generated. We used to try to do this only when it was
4503 -- necessary, but it cleans up the code to do it all the time.
4505 if Is_Access_Type (T) then
4506 Insert_Explicit_Dereference (P);
4507 Analyze_And_Resolve (P, Designated_Type (T));
4510 -- Generate index and validity checks
4512 Generate_Index_Checks (N);
4514 if Validity_Checks_On and then Validity_Check_Subscripts then
4515 Apply_Subscript_Validity_Checks (N);
4518 -- All done for the non-packed case
4520 if not Is_Packed (Etype (Prefix (N))) then
4524 -- For packed arrays that are not bit-packed (i.e. the case of an array
4525 -- with one or more index types with a non-contiguous enumeration type),
4526 -- we can always use the normal packed element get circuit.
4528 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
4529 Expand_Packed_Element_Reference (N);
4533 -- For a reference to a component of a bit packed array, we have to
4534 -- convert it to a reference to the corresponding Packed_Array_Type.
4535 -- We only want to do this for simple references, and not for:
4537 -- Left side of assignment, or prefix of left side of assignment, or
4538 -- prefix of the prefix, to handle packed arrays of packed arrays,
4539 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
4541 -- Renaming objects in renaming associations
4542 -- This case is handled when a use of the renamed variable occurs
4544 -- Actual parameters for a procedure call
4545 -- This case is handled in Exp_Ch6.Expand_Actuals
4547 -- The second expression in a 'Read attribute reference
4549 -- The prefix of an address or size attribute reference
4551 -- The following circuit detects these exceptions
4554 Child : Node_Id := N;
4555 Parnt : Node_Id := Parent (N);
4559 if Nkind (Parnt) = N_Unchecked_Expression then
4562 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
4563 N_Procedure_Call_Statement)
4564 or else (Nkind (Parnt) = N_Parameter_Association
4566 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
4570 elsif Nkind (Parnt) = N_Attribute_Reference
4571 and then (Attribute_Name (Parnt) = Name_Address
4573 Attribute_Name (Parnt) = Name_Size)
4574 and then Prefix (Parnt) = Child
4578 elsif Nkind (Parnt) = N_Assignment_Statement
4579 and then Name (Parnt) = Child
4583 -- If the expression is an index of an indexed component, it must
4584 -- be expanded regardless of context.
4586 elsif Nkind (Parnt) = N_Indexed_Component
4587 and then Child /= Prefix (Parnt)
4589 Expand_Packed_Element_Reference (N);
4592 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
4593 and then Name (Parent (Parnt)) = Parnt
4597 elsif Nkind (Parnt) = N_Attribute_Reference
4598 and then Attribute_Name (Parnt) = Name_Read
4599 and then Next (First (Expressions (Parnt))) = Child
4603 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
4604 and then Prefix (Parnt) = Child
4609 Expand_Packed_Element_Reference (N);
4613 -- Keep looking up tree for unchecked expression, or if we are the
4614 -- prefix of a possible assignment left side.
4617 Parnt := Parent (Child);
4620 end Expand_N_Indexed_Component;
4622 ---------------------
4623 -- Expand_N_Not_In --
4624 ---------------------
4626 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
4627 -- can be done. This avoids needing to duplicate this expansion code.
4629 procedure Expand_N_Not_In (N : Node_Id) is
4630 Loc : constant Source_Ptr := Sloc (N);
4631 Typ : constant Entity_Id := Etype (N);
4632 Cfs : constant Boolean := Comes_From_Source (N);
4639 Left_Opnd => Left_Opnd (N),
4640 Right_Opnd => Right_Opnd (N))));
4642 -- We want this to appear as coming from source if original does (see
4643 -- transformations in Expand_N_In).
4645 Set_Comes_From_Source (N, Cfs);
4646 Set_Comes_From_Source (Right_Opnd (N), Cfs);
4648 -- Now analyze transformed node
4650 Analyze_And_Resolve (N, Typ);
4651 end Expand_N_Not_In;
4657 -- The only replacement required is for the case of a null of type that is
4658 -- an access to protected subprogram. We represent such access values as a
4659 -- record, and so we must replace the occurrence of null by the equivalent
4660 -- record (with a null address and a null pointer in it), so that the
4661 -- backend creates the proper value.
4663 procedure Expand_N_Null (N : Node_Id) is
4664 Loc : constant Source_Ptr := Sloc (N);
4665 Typ : constant Entity_Id := Etype (N);
4669 if Is_Access_Protected_Subprogram_Type (Typ) then
4671 Make_Aggregate (Loc,
4672 Expressions => New_List (
4673 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
4677 Analyze_And_Resolve (N, Equivalent_Type (Typ));
4679 -- For subsequent semantic analysis, the node must retain its type.
4680 -- Gigi in any case replaces this type by the corresponding record
4681 -- type before processing the node.
4687 when RE_Not_Available =>
4691 ---------------------
4692 -- Expand_N_Op_Abs --
4693 ---------------------
4695 procedure Expand_N_Op_Abs (N : Node_Id) is
4696 Loc : constant Source_Ptr := Sloc (N);
4697 Expr : constant Node_Id := Right_Opnd (N);
4700 Unary_Op_Validity_Checks (N);
4702 -- Deal with software overflow checking
4704 if not Backend_Overflow_Checks_On_Target
4705 and then Is_Signed_Integer_Type (Etype (N))
4706 and then Do_Overflow_Check (N)
4708 -- The only case to worry about is when the argument is equal to the
4709 -- largest negative number, so what we do is to insert the check:
4711 -- [constraint_error when Expr = typ'Base'First]
4713 -- with the usual Duplicate_Subexpr use coding for expr
4716 Make_Raise_Constraint_Error (Loc,
4719 Left_Opnd => Duplicate_Subexpr (Expr),
4721 Make_Attribute_Reference (Loc,
4723 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
4724 Attribute_Name => Name_First)),
4725 Reason => CE_Overflow_Check_Failed));
4728 -- Vax floating-point types case
4730 if Vax_Float (Etype (N)) then
4731 Expand_Vax_Arith (N);
4733 end Expand_N_Op_Abs;
4735 ---------------------
4736 -- Expand_N_Op_Add --
4737 ---------------------
4739 procedure Expand_N_Op_Add (N : Node_Id) is
4740 Typ : constant Entity_Id := Etype (N);
4743 Binary_Op_Validity_Checks (N);
4745 -- N + 0 = 0 + N = N for integer types
4747 if Is_Integer_Type (Typ) then
4748 if Compile_Time_Known_Value (Right_Opnd (N))
4749 and then Expr_Value (Right_Opnd (N)) = Uint_0
4751 Rewrite (N, Left_Opnd (N));
4754 elsif Compile_Time_Known_Value (Left_Opnd (N))
4755 and then Expr_Value (Left_Opnd (N)) = Uint_0
4757 Rewrite (N, Right_Opnd (N));
4762 -- Arithmetic overflow checks for signed integer/fixed point types
4764 if Is_Signed_Integer_Type (Typ)
4765 or else Is_Fixed_Point_Type (Typ)
4767 Apply_Arithmetic_Overflow_Check (N);
4770 -- Vax floating-point types case
4772 elsif Vax_Float (Typ) then
4773 Expand_Vax_Arith (N);
4775 end Expand_N_Op_Add;
4777 ---------------------
4778 -- Expand_N_Op_And --
4779 ---------------------
4781 procedure Expand_N_Op_And (N : Node_Id) is
4782 Typ : constant Entity_Id := Etype (N);
4785 Binary_Op_Validity_Checks (N);
4787 if Is_Array_Type (Etype (N)) then
4788 Expand_Boolean_Operator (N);
4790 elsif Is_Boolean_Type (Etype (N)) then
4791 Adjust_Condition (Left_Opnd (N));
4792 Adjust_Condition (Right_Opnd (N));
4793 Set_Etype (N, Standard_Boolean);
4794 Adjust_Result_Type (N, Typ);
4796 end Expand_N_Op_And;
4798 ------------------------
4799 -- Expand_N_Op_Concat --
4800 ------------------------
4802 procedure Expand_N_Op_Concat (N : Node_Id) is
4804 -- List of operands to be concatenated
4807 -- Node which is to be replaced by the result of concatenating the nodes
4808 -- in the list Opnds.
4811 -- Ensure validity of both operands
4813 Binary_Op_Validity_Checks (N);
4815 -- If we are the left operand of a concatenation higher up the tree,
4816 -- then do nothing for now, since we want to deal with a series of
4817 -- concatenations as a unit.
4819 if Nkind (Parent (N)) = N_Op_Concat
4820 and then N = Left_Opnd (Parent (N))
4825 -- We get here with a concatenation whose left operand may be a
4826 -- concatenation itself with a consistent type. We need to process
4827 -- these concatenation operands from left to right, which means
4828 -- from the deepest node in the tree to the highest node.
4831 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
4832 Cnode := Left_Opnd (Cnode);
4835 -- Now Opnd is the deepest Opnd, and its parents are the concatenation
4836 -- nodes above, so now we process bottom up, doing the operations. We
4837 -- gather a string that is as long as possible up to five operands
4839 -- The outer loop runs more than once if more than one concatenation
4840 -- type is involved.
4843 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
4844 Set_Parent (Opnds, N);
4846 -- The inner loop gathers concatenation operands
4848 Inner : while Cnode /= N
4849 and then Base_Type (Etype (Cnode)) =
4850 Base_Type (Etype (Parent (Cnode)))
4852 Cnode := Parent (Cnode);
4853 Append (Right_Opnd (Cnode), Opnds);
4856 Expand_Concatenate (Cnode, Opnds);
4858 exit Outer when Cnode = N;
4859 Cnode := Parent (Cnode);
4861 end Expand_N_Op_Concat;
4863 ------------------------
4864 -- Expand_N_Op_Divide --
4865 ------------------------
4867 procedure Expand_N_Op_Divide (N : Node_Id) is
4868 Loc : constant Source_Ptr := Sloc (N);
4869 Lopnd : constant Node_Id := Left_Opnd (N);
4870 Ropnd : constant Node_Id := Right_Opnd (N);
4871 Ltyp : constant Entity_Id := Etype (Lopnd);
4872 Rtyp : constant Entity_Id := Etype (Ropnd);
4873 Typ : Entity_Id := Etype (N);
4874 Rknow : constant Boolean := Is_Integer_Type (Typ)
4876 Compile_Time_Known_Value (Ropnd);
4880 Binary_Op_Validity_Checks (N);
4883 Rval := Expr_Value (Ropnd);
4886 -- N / 1 = N for integer types
4888 if Rknow and then Rval = Uint_1 then
4893 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
4894 -- Is_Power_Of_2_For_Shift is set means that we know that our left
4895 -- operand is an unsigned integer, as required for this to work.
4897 if Nkind (Ropnd) = N_Op_Expon
4898 and then Is_Power_Of_2_For_Shift (Ropnd)
4900 -- We cannot do this transformation in configurable run time mode if we
4901 -- have 64-bit -- integers and long shifts are not available.
4905 or else Support_Long_Shifts_On_Target)
4908 Make_Op_Shift_Right (Loc,
4911 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
4912 Analyze_And_Resolve (N, Typ);
4916 -- Do required fixup of universal fixed operation
4918 if Typ = Universal_Fixed then
4919 Fixup_Universal_Fixed_Operation (N);
4923 -- Divisions with fixed-point results
4925 if Is_Fixed_Point_Type (Typ) then
4927 -- No special processing if Treat_Fixed_As_Integer is set, since
4928 -- from a semantic point of view such operations are simply integer
4929 -- operations and will be treated that way.
4931 if not Treat_Fixed_As_Integer (N) then
4932 if Is_Integer_Type (Rtyp) then
4933 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
4935 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
4939 -- Other cases of division of fixed-point operands. Again we exclude the
4940 -- case where Treat_Fixed_As_Integer is set.
4942 elsif (Is_Fixed_Point_Type (Ltyp) or else
4943 Is_Fixed_Point_Type (Rtyp))
4944 and then not Treat_Fixed_As_Integer (N)
4946 if Is_Integer_Type (Typ) then
4947 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
4949 pragma Assert (Is_Floating_Point_Type (Typ));
4950 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
4953 -- Mixed-mode operations can appear in a non-static universal context,
4954 -- in which case the integer argument must be converted explicitly.
4956 elsif Typ = Universal_Real
4957 and then Is_Integer_Type (Rtyp)
4960 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
4962 Analyze_And_Resolve (Ropnd, Universal_Real);
4964 elsif Typ = Universal_Real
4965 and then Is_Integer_Type (Ltyp)
4968 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
4970 Analyze_And_Resolve (Lopnd, Universal_Real);
4972 -- Non-fixed point cases, do integer zero divide and overflow checks
4974 elsif Is_Integer_Type (Typ) then
4975 Apply_Divide_Check (N);
4977 -- Check for 64-bit division available, or long shifts if the divisor
4978 -- is a small power of 2 (since such divides will be converted into
4981 if Esize (Ltyp) > 32
4982 and then not Support_64_Bit_Divides_On_Target
4985 or else not Support_Long_Shifts_On_Target
4986 or else (Rval /= Uint_2 and then
4987 Rval /= Uint_4 and then
4988 Rval /= Uint_8 and then
4989 Rval /= Uint_16 and then
4990 Rval /= Uint_32 and then
4993 Error_Msg_CRT ("64-bit division", N);
4996 -- Deal with Vax_Float
4998 elsif Vax_Float (Typ) then
4999 Expand_Vax_Arith (N);
5002 end Expand_N_Op_Divide;
5004 --------------------
5005 -- Expand_N_Op_Eq --
5006 --------------------
5008 procedure Expand_N_Op_Eq (N : Node_Id) is
5009 Loc : constant Source_Ptr := Sloc (N);
5010 Typ : constant Entity_Id := Etype (N);
5011 Lhs : constant Node_Id := Left_Opnd (N);
5012 Rhs : constant Node_Id := Right_Opnd (N);
5013 Bodies : constant List_Id := New_List;
5014 A_Typ : constant Entity_Id := Etype (Lhs);
5016 Typl : Entity_Id := A_Typ;
5017 Op_Name : Entity_Id;
5020 procedure Build_Equality_Call (Eq : Entity_Id);
5021 -- If a constructed equality exists for the type or for its parent,
5022 -- build and analyze call, adding conversions if the operation is
5025 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
5026 -- Determines whether a type has a subcomponent of an unconstrained
5027 -- Unchecked_Union subtype. Typ is a record type.
5029 -------------------------
5030 -- Build_Equality_Call --
5031 -------------------------
5033 procedure Build_Equality_Call (Eq : Entity_Id) is
5034 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
5035 L_Exp : Node_Id := Relocate_Node (Lhs);
5036 R_Exp : Node_Id := Relocate_Node (Rhs);
5039 if Base_Type (Op_Type) /= Base_Type (A_Typ)
5040 and then not Is_Class_Wide_Type (A_Typ)
5042 L_Exp := OK_Convert_To (Op_Type, L_Exp);
5043 R_Exp := OK_Convert_To (Op_Type, R_Exp);
5046 -- If we have an Unchecked_Union, we need to add the inferred
5047 -- discriminant values as actuals in the function call. At this
5048 -- point, the expansion has determined that both operands have
5049 -- inferable discriminants.
5051 if Is_Unchecked_Union (Op_Type) then
5053 Lhs_Type : constant Node_Id := Etype (L_Exp);
5054 Rhs_Type : constant Node_Id := Etype (R_Exp);
5055 Lhs_Discr_Val : Node_Id;
5056 Rhs_Discr_Val : Node_Id;
5059 -- Per-object constrained selected components require special
5060 -- attention. If the enclosing scope of the component is an
5061 -- Unchecked_Union, we cannot reference its discriminants
5062 -- directly. This is why we use the two extra parameters of
5063 -- the equality function of the enclosing Unchecked_Union.
5065 -- type UU_Type (Discr : Integer := 0) is
5068 -- pragma Unchecked_Union (UU_Type);
5070 -- 1. Unchecked_Union enclosing record:
5072 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
5074 -- Comp : UU_Type (Discr);
5076 -- end Enclosing_UU_Type;
5077 -- pragma Unchecked_Union (Enclosing_UU_Type);
5079 -- Obj1 : Enclosing_UU_Type;
5080 -- Obj2 : Enclosing_UU_Type (1);
5082 -- [. . .] Obj1 = Obj2 [. . .]
5086 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
5088 -- A and B are the formal parameters of the equality function
5089 -- of Enclosing_UU_Type. The function always has two extra
5090 -- formals to capture the inferred discriminant values.
5092 -- 2. Non-Unchecked_Union enclosing record:
5095 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
5098 -- Comp : UU_Type (Discr);
5100 -- end Enclosing_Non_UU_Type;
5102 -- Obj1 : Enclosing_Non_UU_Type;
5103 -- Obj2 : Enclosing_Non_UU_Type (1);
5105 -- ... Obj1 = Obj2 ...
5109 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
5110 -- obj1.discr, obj2.discr)) then
5112 -- In this case we can directly reference the discriminants of
5113 -- the enclosing record.
5117 if Nkind (Lhs) = N_Selected_Component
5118 and then Has_Per_Object_Constraint
5119 (Entity (Selector_Name (Lhs)))
5121 -- Enclosing record is an Unchecked_Union, use formal A
5123 if Is_Unchecked_Union (Scope
5124 (Entity (Selector_Name (Lhs))))
5127 Make_Identifier (Loc,
5130 -- Enclosing record is of a non-Unchecked_Union type, it is
5131 -- possible to reference the discriminant.
5135 Make_Selected_Component (Loc,
5136 Prefix => Prefix (Lhs),
5139 (Get_Discriminant_Value
5140 (First_Discriminant (Lhs_Type),
5142 Stored_Constraint (Lhs_Type))));
5145 -- Comment needed here ???
5148 -- Infer the discriminant value
5152 (Get_Discriminant_Value
5153 (First_Discriminant (Lhs_Type),
5155 Stored_Constraint (Lhs_Type)));
5160 if Nkind (Rhs) = N_Selected_Component
5161 and then Has_Per_Object_Constraint
5162 (Entity (Selector_Name (Rhs)))
5164 if Is_Unchecked_Union
5165 (Scope (Entity (Selector_Name (Rhs))))
5168 Make_Identifier (Loc,
5173 Make_Selected_Component (Loc,
5174 Prefix => Prefix (Rhs),
5176 New_Copy (Get_Discriminant_Value (
5177 First_Discriminant (Rhs_Type),
5179 Stored_Constraint (Rhs_Type))));
5184 New_Copy (Get_Discriminant_Value (
5185 First_Discriminant (Rhs_Type),
5187 Stored_Constraint (Rhs_Type)));
5192 Make_Function_Call (Loc,
5193 Name => New_Reference_To (Eq, Loc),
5194 Parameter_Associations => New_List (
5201 -- Normal case, not an unchecked union
5205 Make_Function_Call (Loc,
5206 Name => New_Reference_To (Eq, Loc),
5207 Parameter_Associations => New_List (L_Exp, R_Exp)));
5210 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5211 end Build_Equality_Call;
5213 ------------------------------------
5214 -- Has_Unconstrained_UU_Component --
5215 ------------------------------------
5217 function Has_Unconstrained_UU_Component
5218 (Typ : Node_Id) return Boolean
5220 Tdef : constant Node_Id :=
5221 Type_Definition (Declaration_Node (Base_Type (Typ)));
5225 function Component_Is_Unconstrained_UU
5226 (Comp : Node_Id) return Boolean;
5227 -- Determines whether the subtype of the component is an
5228 -- unconstrained Unchecked_Union.
5230 function Variant_Is_Unconstrained_UU
5231 (Variant : Node_Id) return Boolean;
5232 -- Determines whether a component of the variant has an unconstrained
5233 -- Unchecked_Union subtype.
5235 -----------------------------------
5236 -- Component_Is_Unconstrained_UU --
5237 -----------------------------------
5239 function Component_Is_Unconstrained_UU
5240 (Comp : Node_Id) return Boolean
5243 if Nkind (Comp) /= N_Component_Declaration then
5248 Sindic : constant Node_Id :=
5249 Subtype_Indication (Component_Definition (Comp));
5252 -- Unconstrained nominal type. In the case of a constraint
5253 -- present, the node kind would have been N_Subtype_Indication.
5255 if Nkind (Sindic) = N_Identifier then
5256 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
5261 end Component_Is_Unconstrained_UU;
5263 ---------------------------------
5264 -- Variant_Is_Unconstrained_UU --
5265 ---------------------------------
5267 function Variant_Is_Unconstrained_UU
5268 (Variant : Node_Id) return Boolean
5270 Clist : constant Node_Id := Component_List (Variant);
5273 if Is_Empty_List (Component_Items (Clist)) then
5277 -- We only need to test one component
5280 Comp : Node_Id := First (Component_Items (Clist));
5283 while Present (Comp) loop
5284 if Component_Is_Unconstrained_UU (Comp) then
5292 -- None of the components withing the variant were of
5293 -- unconstrained Unchecked_Union type.
5296 end Variant_Is_Unconstrained_UU;
5298 -- Start of processing for Has_Unconstrained_UU_Component
5301 if Null_Present (Tdef) then
5305 Clist := Component_List (Tdef);
5306 Vpart := Variant_Part (Clist);
5308 -- Inspect available components
5310 if Present (Component_Items (Clist)) then
5312 Comp : Node_Id := First (Component_Items (Clist));
5315 while Present (Comp) loop
5317 -- One component is sufficient
5319 if Component_Is_Unconstrained_UU (Comp) then
5328 -- Inspect available components withing variants
5330 if Present (Vpart) then
5332 Variant : Node_Id := First (Variants (Vpart));
5335 while Present (Variant) loop
5337 -- One component within a variant is sufficient
5339 if Variant_Is_Unconstrained_UU (Variant) then
5348 -- Neither the available components, nor the components inside the
5349 -- variant parts were of an unconstrained Unchecked_Union subtype.
5352 end Has_Unconstrained_UU_Component;
5354 -- Start of processing for Expand_N_Op_Eq
5357 Binary_Op_Validity_Checks (N);
5359 if Ekind (Typl) = E_Private_Type then
5360 Typl := Underlying_Type (Typl);
5361 elsif Ekind (Typl) = E_Private_Subtype then
5362 Typl := Underlying_Type (Base_Type (Typl));
5367 -- It may happen in error situations that the underlying type is not
5368 -- set. The error will be detected later, here we just defend the
5375 Typl := Base_Type (Typl);
5377 -- Boolean types (requiring handling of non-standard case)
5379 if Is_Boolean_Type (Typl) then
5380 Adjust_Condition (Left_Opnd (N));
5381 Adjust_Condition (Right_Opnd (N));
5382 Set_Etype (N, Standard_Boolean);
5383 Adjust_Result_Type (N, Typ);
5387 elsif Is_Array_Type (Typl) then
5389 -- If we are doing full validity checking, and it is possible for the
5390 -- array elements to be invalid then expand out array comparisons to
5391 -- make sure that we check the array elements.
5393 if Validity_Check_Operands
5394 and then not Is_Known_Valid (Component_Type (Typl))
5397 Save_Force_Validity_Checks : constant Boolean :=
5398 Force_Validity_Checks;
5400 Force_Validity_Checks := True;
5402 Expand_Array_Equality
5404 Relocate_Node (Lhs),
5405 Relocate_Node (Rhs),
5408 Insert_Actions (N, Bodies);
5409 Analyze_And_Resolve (N, Standard_Boolean);
5410 Force_Validity_Checks := Save_Force_Validity_Checks;
5413 -- Packed case where both operands are known aligned
5415 elsif Is_Bit_Packed_Array (Typl)
5416 and then not Is_Possibly_Unaligned_Object (Lhs)
5417 and then not Is_Possibly_Unaligned_Object (Rhs)
5419 Expand_Packed_Eq (N);
5421 -- Where the component type is elementary we can use a block bit
5422 -- comparison (if supported on the target) exception in the case
5423 -- of floating-point (negative zero issues require element by
5424 -- element comparison), and atomic types (where we must be sure
5425 -- to load elements independently) and possibly unaligned arrays.
5427 elsif Is_Elementary_Type (Component_Type (Typl))
5428 and then not Is_Floating_Point_Type (Component_Type (Typl))
5429 and then not Is_Atomic (Component_Type (Typl))
5430 and then not Is_Possibly_Unaligned_Object (Lhs)
5431 and then not Is_Possibly_Unaligned_Object (Rhs)
5432 and then Support_Composite_Compare_On_Target
5436 -- For composite and floating-point cases, expand equality loop to
5437 -- make sure of using proper comparisons for tagged types, and
5438 -- correctly handling the floating-point case.
5442 Expand_Array_Equality
5444 Relocate_Node (Lhs),
5445 Relocate_Node (Rhs),
5448 Insert_Actions (N, Bodies, Suppress => All_Checks);
5449 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5454 elsif Is_Record_Type (Typl) then
5456 -- For tagged types, use the primitive "="
5458 if Is_Tagged_Type (Typl) then
5460 -- No need to do anything else compiling under restriction
5461 -- No_Dispatching_Calls. During the semantic analysis we
5462 -- already notified such violation.
5464 if Restriction_Active (No_Dispatching_Calls) then
5468 -- If this is derived from an untagged private type completed with
5469 -- a tagged type, it does not have a full view, so we use the
5470 -- primitive operations of the private type. This check should no
5471 -- longer be necessary when these types get their full views???
5473 if Is_Private_Type (A_Typ)
5474 and then not Is_Tagged_Type (A_Typ)
5475 and then Is_Derived_Type (A_Typ)
5476 and then No (Full_View (A_Typ))
5478 -- Search for equality operation, checking that the operands
5479 -- have the same type. Note that we must find a matching entry,
5480 -- or something is very wrong!
5482 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
5484 while Present (Prim) loop
5485 exit when Chars (Node (Prim)) = Name_Op_Eq
5486 and then Etype (First_Formal (Node (Prim))) =
5487 Etype (Next_Formal (First_Formal (Node (Prim))))
5489 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5494 pragma Assert (Present (Prim));
5495 Op_Name := Node (Prim);
5497 -- Find the type's predefined equality or an overriding
5498 -- user- defined equality. The reason for not simply calling
5499 -- Find_Prim_Op here is that there may be a user-defined
5500 -- overloaded equality op that precedes the equality that we want,
5501 -- so we have to explicitly search (e.g., there could be an
5502 -- equality with two different parameter types).
5505 if Is_Class_Wide_Type (Typl) then
5506 Typl := Root_Type (Typl);
5509 Prim := First_Elmt (Primitive_Operations (Typl));
5510 while Present (Prim) loop
5511 exit when Chars (Node (Prim)) = Name_Op_Eq
5512 and then Etype (First_Formal (Node (Prim))) =
5513 Etype (Next_Formal (First_Formal (Node (Prim))))
5515 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5520 pragma Assert (Present (Prim));
5521 Op_Name := Node (Prim);
5524 Build_Equality_Call (Op_Name);
5526 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
5527 -- predefined equality operator for a type which has a subcomponent
5528 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
5530 elsif Has_Unconstrained_UU_Component (Typl) then
5532 Make_Raise_Program_Error (Loc,
5533 Reason => PE_Unchecked_Union_Restriction));
5535 -- Prevent Gigi from generating incorrect code by rewriting the
5536 -- equality as a standard False.
5539 New_Occurrence_Of (Standard_False, Loc));
5541 elsif Is_Unchecked_Union (Typl) then
5543 -- If we can infer the discriminants of the operands, we make a
5544 -- call to the TSS equality function.
5546 if Has_Inferable_Discriminants (Lhs)
5548 Has_Inferable_Discriminants (Rhs)
5551 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5554 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5555 -- the predefined equality operator for an Unchecked_Union type
5556 -- if either of the operands lack inferable discriminants.
5559 Make_Raise_Program_Error (Loc,
5560 Reason => PE_Unchecked_Union_Restriction));
5562 -- Prevent Gigi from generating incorrect code by rewriting
5563 -- the equality as a standard False.
5566 New_Occurrence_Of (Standard_False, Loc));
5570 -- If a type support function is present (for complex cases), use it
5572 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
5574 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5576 -- Otherwise expand the component by component equality. Note that
5577 -- we never use block-bit comparisons for records, because of the
5578 -- problems with gaps. The backend will often be able to recombine
5579 -- the separate comparisons that we generate here.
5582 Remove_Side_Effects (Lhs);
5583 Remove_Side_Effects (Rhs);
5585 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
5587 Insert_Actions (N, Bodies, Suppress => All_Checks);
5588 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5592 -- Test if result is known at compile time
5594 Rewrite_Comparison (N);
5596 -- If we still have comparison for Vax_Float, process it
5598 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
5599 Expand_Vax_Comparison (N);
5604 -----------------------
5605 -- Expand_N_Op_Expon --
5606 -----------------------
5608 procedure Expand_N_Op_Expon (N : Node_Id) is
5609 Loc : constant Source_Ptr := Sloc (N);
5610 Typ : constant Entity_Id := Etype (N);
5611 Rtyp : constant Entity_Id := Root_Type (Typ);
5612 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
5613 Bastyp : constant Node_Id := Etype (Base);
5614 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
5615 Exptyp : constant Entity_Id := Etype (Exp);
5616 Ovflo : constant Boolean := Do_Overflow_Check (N);
5625 Binary_Op_Validity_Checks (N);
5627 -- If either operand is of a private type, then we have the use of an
5628 -- intrinsic operator, and we get rid of the privateness, by using root
5629 -- types of underlying types for the actual operation. Otherwise the
5630 -- private types will cause trouble if we expand multiplications or
5631 -- shifts etc. We also do this transformation if the result type is
5632 -- different from the base type.
5634 if Is_Private_Type (Etype (Base))
5636 Is_Private_Type (Typ)
5638 Is_Private_Type (Exptyp)
5640 Rtyp /= Root_Type (Bastyp)
5643 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
5644 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
5648 Unchecked_Convert_To (Typ,
5650 Left_Opnd => Unchecked_Convert_To (Bt, Base),
5651 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
5652 Analyze_And_Resolve (N, Typ);
5657 -- Test for case of known right argument
5659 if Compile_Time_Known_Value (Exp) then
5660 Expv := Expr_Value (Exp);
5662 -- We only fold small non-negative exponents. You might think we
5663 -- could fold small negative exponents for the real case, but we
5664 -- can't because we are required to raise Constraint_Error for
5665 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
5666 -- See ACVC test C4A012B.
5668 if Expv >= 0 and then Expv <= 4 then
5670 -- X ** 0 = 1 (or 1.0)
5674 -- Call Remove_Side_Effects to ensure that any side effects
5675 -- in the ignored left operand (in particular function calls
5676 -- to user defined functions) are properly executed.
5678 Remove_Side_Effects (Base);
5680 if Ekind (Typ) in Integer_Kind then
5681 Xnode := Make_Integer_Literal (Loc, Intval => 1);
5683 Xnode := Make_Real_Literal (Loc, Ureal_1);
5695 Make_Op_Multiply (Loc,
5696 Left_Opnd => Duplicate_Subexpr (Base),
5697 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
5699 -- X ** 3 = X * X * X
5703 Make_Op_Multiply (Loc,
5705 Make_Op_Multiply (Loc,
5706 Left_Opnd => Duplicate_Subexpr (Base),
5707 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
5708 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
5711 -- En : constant base'type := base * base;
5717 Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
5719 Insert_Actions (N, New_List (
5720 Make_Object_Declaration (Loc,
5721 Defining_Identifier => Temp,
5722 Constant_Present => True,
5723 Object_Definition => New_Reference_To (Typ, Loc),
5725 Make_Op_Multiply (Loc,
5726 Left_Opnd => Duplicate_Subexpr (Base),
5727 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
5730 Make_Op_Multiply (Loc,
5731 Left_Opnd => New_Reference_To (Temp, Loc),
5732 Right_Opnd => New_Reference_To (Temp, Loc));
5736 Analyze_And_Resolve (N, Typ);
5741 -- Case of (2 ** expression) appearing as an argument of an integer
5742 -- multiplication, or as the right argument of a division of a non-
5743 -- negative integer. In such cases we leave the node untouched, setting
5744 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
5745 -- of the higher level node converts it into a shift.
5747 -- Note: this transformation is not applicable for a modular type with
5748 -- a non-binary modulus in the multiplication case, since we get a wrong
5749 -- result if the shift causes an overflow before the modular reduction.
5751 if Nkind (Base) = N_Integer_Literal
5752 and then Intval (Base) = 2
5753 and then Is_Integer_Type (Root_Type (Exptyp))
5754 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
5755 and then Is_Unsigned_Type (Exptyp)
5757 and then Nkind (Parent (N)) in N_Binary_Op
5760 P : constant Node_Id := Parent (N);
5761 L : constant Node_Id := Left_Opnd (P);
5762 R : constant Node_Id := Right_Opnd (P);
5765 if (Nkind (P) = N_Op_Multiply
5766 and then not Non_Binary_Modulus (Typ)
5768 ((Is_Integer_Type (Etype (L)) and then R = N)
5770 (Is_Integer_Type (Etype (R)) and then L = N))
5771 and then not Do_Overflow_Check (P))
5774 (Nkind (P) = N_Op_Divide
5775 and then Is_Integer_Type (Etype (L))
5776 and then Is_Unsigned_Type (Etype (L))
5778 and then not Do_Overflow_Check (P))
5780 Set_Is_Power_Of_2_For_Shift (N);
5786 -- Fall through if exponentiation must be done using a runtime routine
5788 -- First deal with modular case
5790 if Is_Modular_Integer_Type (Rtyp) then
5792 -- Non-binary case, we call the special exponentiation routine for
5793 -- the non-binary case, converting the argument to Long_Long_Integer
5794 -- and passing the modulus value. Then the result is converted back
5795 -- to the base type.
5797 if Non_Binary_Modulus (Rtyp) then
5800 Make_Function_Call (Loc,
5801 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
5802 Parameter_Associations => New_List (
5803 Convert_To (Standard_Integer, Base),
5804 Make_Integer_Literal (Loc, Modulus (Rtyp)),
5807 -- Binary case, in this case, we call one of two routines, either the
5808 -- unsigned integer case, or the unsigned long long integer case,
5809 -- with a final "and" operation to do the required mod.
5812 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
5813 Ent := RTE (RE_Exp_Unsigned);
5815 Ent := RTE (RE_Exp_Long_Long_Unsigned);
5822 Make_Function_Call (Loc,
5823 Name => New_Reference_To (Ent, Loc),
5824 Parameter_Associations => New_List (
5825 Convert_To (Etype (First_Formal (Ent)), Base),
5828 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
5832 -- Common exit point for modular type case
5834 Analyze_And_Resolve (N, Typ);
5837 -- Signed integer cases, done using either Integer or Long_Long_Integer.
5838 -- It is not worth having routines for Short_[Short_]Integer, since for
5839 -- most machines it would not help, and it would generate more code that
5840 -- might need certification when a certified run time is required.
5842 -- In the integer cases, we have two routines, one for when overflow
5843 -- checks are required, and one when they are not required, since there
5844 -- is a real gain in omitting checks on many machines.
5846 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
5847 or else (Rtyp = Base_Type (Standard_Long_Integer)
5849 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
5850 or else (Rtyp = Universal_Integer)
5852 Etyp := Standard_Long_Long_Integer;
5855 Rent := RE_Exp_Long_Long_Integer;
5857 Rent := RE_Exn_Long_Long_Integer;
5860 elsif Is_Signed_Integer_Type (Rtyp) then
5861 Etyp := Standard_Integer;
5864 Rent := RE_Exp_Integer;
5866 Rent := RE_Exn_Integer;
5869 -- Floating-point cases, always done using Long_Long_Float. We do not
5870 -- need separate routines for the overflow case here, since in the case
5871 -- of floating-point, we generate infinities anyway as a rule (either
5872 -- that or we automatically trap overflow), and if there is an infinity
5873 -- generated and a range check is required, the check will fail anyway.
5876 pragma Assert (Is_Floating_Point_Type (Rtyp));
5877 Etyp := Standard_Long_Long_Float;
5878 Rent := RE_Exn_Long_Long_Float;
5881 -- Common processing for integer cases and floating-point cases.
5882 -- If we are in the right type, we can call runtime routine directly
5885 and then Rtyp /= Universal_Integer
5886 and then Rtyp /= Universal_Real
5889 Make_Function_Call (Loc,
5890 Name => New_Reference_To (RTE (Rent), Loc),
5891 Parameter_Associations => New_List (Base, Exp)));
5893 -- Otherwise we have to introduce conversions (conversions are also
5894 -- required in the universal cases, since the runtime routine is
5895 -- typed using one of the standard types).
5900 Make_Function_Call (Loc,
5901 Name => New_Reference_To (RTE (Rent), Loc),
5902 Parameter_Associations => New_List (
5903 Convert_To (Etyp, Base),
5907 Analyze_And_Resolve (N, Typ);
5911 when RE_Not_Available =>
5913 end Expand_N_Op_Expon;
5915 --------------------
5916 -- Expand_N_Op_Ge --
5917 --------------------
5919 procedure Expand_N_Op_Ge (N : Node_Id) is
5920 Typ : constant Entity_Id := Etype (N);
5921 Op1 : constant Node_Id := Left_Opnd (N);
5922 Op2 : constant Node_Id := Right_Opnd (N);
5923 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
5926 Binary_Op_Validity_Checks (N);
5928 if Is_Array_Type (Typ1) then
5929 Expand_Array_Comparison (N);
5933 if Is_Boolean_Type (Typ1) then
5934 Adjust_Condition (Op1);
5935 Adjust_Condition (Op2);
5936 Set_Etype (N, Standard_Boolean);
5937 Adjust_Result_Type (N, Typ);
5940 Rewrite_Comparison (N);
5942 -- If we still have comparison, and Vax_Float type, process it
5944 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
5945 Expand_Vax_Comparison (N);
5950 --------------------
5951 -- Expand_N_Op_Gt --
5952 --------------------
5954 procedure Expand_N_Op_Gt (N : Node_Id) is
5955 Typ : constant Entity_Id := Etype (N);
5956 Op1 : constant Node_Id := Left_Opnd (N);
5957 Op2 : constant Node_Id := Right_Opnd (N);
5958 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
5961 Binary_Op_Validity_Checks (N);
5963 if Is_Array_Type (Typ1) then
5964 Expand_Array_Comparison (N);
5968 if Is_Boolean_Type (Typ1) then
5969 Adjust_Condition (Op1);
5970 Adjust_Condition (Op2);
5971 Set_Etype (N, Standard_Boolean);
5972 Adjust_Result_Type (N, Typ);
5975 Rewrite_Comparison (N);
5977 -- If we still have comparison, and Vax_Float type, process it
5979 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
5980 Expand_Vax_Comparison (N);
5985 --------------------
5986 -- Expand_N_Op_Le --
5987 --------------------
5989 procedure Expand_N_Op_Le (N : Node_Id) is
5990 Typ : constant Entity_Id := Etype (N);
5991 Op1 : constant Node_Id := Left_Opnd (N);
5992 Op2 : constant Node_Id := Right_Opnd (N);
5993 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
5996 Binary_Op_Validity_Checks (N);
5998 if Is_Array_Type (Typ1) then
5999 Expand_Array_Comparison (N);
6003 if Is_Boolean_Type (Typ1) then
6004 Adjust_Condition (Op1);
6005 Adjust_Condition (Op2);
6006 Set_Etype (N, Standard_Boolean);
6007 Adjust_Result_Type (N, Typ);
6010 Rewrite_Comparison (N);
6012 -- If we still have comparison, and Vax_Float type, process it
6014 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6015 Expand_Vax_Comparison (N);
6020 --------------------
6021 -- Expand_N_Op_Lt --
6022 --------------------
6024 procedure Expand_N_Op_Lt (N : Node_Id) is
6025 Typ : constant Entity_Id := Etype (N);
6026 Op1 : constant Node_Id := Left_Opnd (N);
6027 Op2 : constant Node_Id := Right_Opnd (N);
6028 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6031 Binary_Op_Validity_Checks (N);
6033 if Is_Array_Type (Typ1) then
6034 Expand_Array_Comparison (N);
6038 if Is_Boolean_Type (Typ1) then
6039 Adjust_Condition (Op1);
6040 Adjust_Condition (Op2);
6041 Set_Etype (N, Standard_Boolean);
6042 Adjust_Result_Type (N, Typ);
6045 Rewrite_Comparison (N);
6047 -- If we still have comparison, and Vax_Float type, process it
6049 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6050 Expand_Vax_Comparison (N);
6055 -----------------------
6056 -- Expand_N_Op_Minus --
6057 -----------------------
6059 procedure Expand_N_Op_Minus (N : Node_Id) is
6060 Loc : constant Source_Ptr := Sloc (N);
6061 Typ : constant Entity_Id := Etype (N);
6064 Unary_Op_Validity_Checks (N);
6066 if not Backend_Overflow_Checks_On_Target
6067 and then Is_Signed_Integer_Type (Etype (N))
6068 and then Do_Overflow_Check (N)
6070 -- Software overflow checking expands -expr into (0 - expr)
6073 Make_Op_Subtract (Loc,
6074 Left_Opnd => Make_Integer_Literal (Loc, 0),
6075 Right_Opnd => Right_Opnd (N)));
6077 Analyze_And_Resolve (N, Typ);
6079 -- Vax floating-point types case
6081 elsif Vax_Float (Etype (N)) then
6082 Expand_Vax_Arith (N);
6084 end Expand_N_Op_Minus;
6086 ---------------------
6087 -- Expand_N_Op_Mod --
6088 ---------------------
6090 procedure Expand_N_Op_Mod (N : Node_Id) is
6091 Loc : constant Source_Ptr := Sloc (N);
6092 Typ : constant Entity_Id := Etype (N);
6093 Left : constant Node_Id := Left_Opnd (N);
6094 Right : constant Node_Id := Right_Opnd (N);
6095 DOC : constant Boolean := Do_Overflow_Check (N);
6096 DDC : constant Boolean := Do_Division_Check (N);
6106 pragma Warnings (Off, Lhi);
6109 Binary_Op_Validity_Checks (N);
6111 Determine_Range (Right, ROK, Rlo, Rhi);
6112 Determine_Range (Left, LOK, Llo, Lhi);
6114 -- Convert mod to rem if operands are known non-negative. We do this
6115 -- since it is quite likely that this will improve the quality of code,
6116 -- (the operation now corresponds to the hardware remainder), and it
6117 -- does not seem likely that it could be harmful.
6119 if LOK and then Llo >= 0
6121 ROK and then Rlo >= 0
6124 Make_Op_Rem (Sloc (N),
6125 Left_Opnd => Left_Opnd (N),
6126 Right_Opnd => Right_Opnd (N)));
6128 -- Instead of reanalyzing the node we do the analysis manually. This
6129 -- avoids anomalies when the replacement is done in an instance and
6130 -- is epsilon more efficient.
6132 Set_Entity (N, Standard_Entity (S_Op_Rem));
6134 Set_Do_Overflow_Check (N, DOC);
6135 Set_Do_Division_Check (N, DDC);
6136 Expand_N_Op_Rem (N);
6139 -- Otherwise, normal mod processing
6142 if Is_Integer_Type (Etype (N)) then
6143 Apply_Divide_Check (N);
6146 -- Apply optimization x mod 1 = 0. We don't really need that with
6147 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
6148 -- certainly harmless.
6150 if Is_Integer_Type (Etype (N))
6151 and then Compile_Time_Known_Value (Right)
6152 and then Expr_Value (Right) = Uint_1
6154 -- Call Remove_Side_Effects to ensure that any side effects in
6155 -- the ignored left operand (in particular function calls to
6156 -- user defined functions) are properly executed.
6158 Remove_Side_Effects (Left);
6160 Rewrite (N, Make_Integer_Literal (Loc, 0));
6161 Analyze_And_Resolve (N, Typ);
6165 -- Deal with annoying case of largest negative number remainder
6166 -- minus one. Gigi does not handle this case correctly, because
6167 -- it generates a divide instruction which may trap in this case.
6169 -- In fact the check is quite easy, if the right operand is -1, then
6170 -- the mod value is always 0, and we can just ignore the left operand
6171 -- completely in this case.
6173 -- The operand type may be private (e.g. in the expansion of an
6174 -- intrinsic operation) so we must use the underlying type to get the
6175 -- bounds, and convert the literals explicitly.
6179 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
6181 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
6183 ((not LOK) or else (Llo = LLB))
6186 Make_Conditional_Expression (Loc,
6187 Expressions => New_List (
6189 Left_Opnd => Duplicate_Subexpr (Right),
6191 Unchecked_Convert_To (Typ,
6192 Make_Integer_Literal (Loc, -1))),
6193 Unchecked_Convert_To (Typ,
6194 Make_Integer_Literal (Loc, Uint_0)),
6195 Relocate_Node (N))));
6197 Set_Analyzed (Next (Next (First (Expressions (N)))));
6198 Analyze_And_Resolve (N, Typ);
6201 end Expand_N_Op_Mod;
6203 --------------------------
6204 -- Expand_N_Op_Multiply --
6205 --------------------------
6207 procedure Expand_N_Op_Multiply (N : Node_Id) is
6208 Loc : constant Source_Ptr := Sloc (N);
6209 Lop : constant Node_Id := Left_Opnd (N);
6210 Rop : constant Node_Id := Right_Opnd (N);
6212 Lp2 : constant Boolean :=
6213 Nkind (Lop) = N_Op_Expon
6214 and then Is_Power_Of_2_For_Shift (Lop);
6216 Rp2 : constant Boolean :=
6217 Nkind (Rop) = N_Op_Expon
6218 and then Is_Power_Of_2_For_Shift (Rop);
6220 Ltyp : constant Entity_Id := Etype (Lop);
6221 Rtyp : constant Entity_Id := Etype (Rop);
6222 Typ : Entity_Id := Etype (N);
6225 Binary_Op_Validity_Checks (N);
6227 -- Special optimizations for integer types
6229 if Is_Integer_Type (Typ) then
6231 -- N * 0 = 0 for integer types
6233 if Compile_Time_Known_Value (Rop)
6234 and then Expr_Value (Rop) = Uint_0
6236 -- Call Remove_Side_Effects to ensure that any side effects in
6237 -- the ignored left operand (in particular function calls to
6238 -- user defined functions) are properly executed.
6240 Remove_Side_Effects (Lop);
6242 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6243 Analyze_And_Resolve (N, Typ);
6247 -- Similar handling for 0 * N = 0
6249 if Compile_Time_Known_Value (Lop)
6250 and then Expr_Value (Lop) = Uint_0
6252 Remove_Side_Effects (Rop);
6253 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6254 Analyze_And_Resolve (N, Typ);
6258 -- N * 1 = 1 * N = N for integer types
6260 -- This optimisation is not done if we are going to
6261 -- rewrite the product 1 * 2 ** N to a shift.
6263 if Compile_Time_Known_Value (Rop)
6264 and then Expr_Value (Rop) = Uint_1
6270 elsif Compile_Time_Known_Value (Lop)
6271 and then Expr_Value (Lop) = Uint_1
6279 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
6280 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6281 -- operand is an integer, as required for this to work.
6286 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
6290 Left_Opnd => Make_Integer_Literal (Loc, 2),
6293 Left_Opnd => Right_Opnd (Lop),
6294 Right_Opnd => Right_Opnd (Rop))));
6295 Analyze_And_Resolve (N, Typ);
6300 Make_Op_Shift_Left (Loc,
6303 Convert_To (Standard_Natural, Right_Opnd (Rop))));
6304 Analyze_And_Resolve (N, Typ);
6308 -- Same processing for the operands the other way round
6312 Make_Op_Shift_Left (Loc,
6315 Convert_To (Standard_Natural, Right_Opnd (Lop))));
6316 Analyze_And_Resolve (N, Typ);
6320 -- Do required fixup of universal fixed operation
6322 if Typ = Universal_Fixed then
6323 Fixup_Universal_Fixed_Operation (N);
6327 -- Multiplications with fixed-point results
6329 if Is_Fixed_Point_Type (Typ) then
6331 -- No special processing if Treat_Fixed_As_Integer is set, since from
6332 -- a semantic point of view such operations are simply integer
6333 -- operations and will be treated that way.
6335 if not Treat_Fixed_As_Integer (N) then
6337 -- Case of fixed * integer => fixed
6339 if Is_Integer_Type (Rtyp) then
6340 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
6342 -- Case of integer * fixed => fixed
6344 elsif Is_Integer_Type (Ltyp) then
6345 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
6347 -- Case of fixed * fixed => fixed
6350 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
6354 -- Other cases of multiplication of fixed-point operands. Again we
6355 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
6357 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6358 and then not Treat_Fixed_As_Integer (N)
6360 if Is_Integer_Type (Typ) then
6361 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
6363 pragma Assert (Is_Floating_Point_Type (Typ));
6364 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
6367 -- Mixed-mode operations can appear in a non-static universal context,
6368 -- in which case the integer argument must be converted explicitly.
6370 elsif Typ = Universal_Real
6371 and then Is_Integer_Type (Rtyp)
6373 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
6375 Analyze_And_Resolve (Rop, Universal_Real);
6377 elsif Typ = Universal_Real
6378 and then Is_Integer_Type (Ltyp)
6380 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
6382 Analyze_And_Resolve (Lop, Universal_Real);
6384 -- Non-fixed point cases, check software overflow checking required
6386 elsif Is_Signed_Integer_Type (Etype (N)) then
6387 Apply_Arithmetic_Overflow_Check (N);
6389 -- Deal with VAX float case
6391 elsif Vax_Float (Typ) then
6392 Expand_Vax_Arith (N);
6395 end Expand_N_Op_Multiply;
6397 --------------------
6398 -- Expand_N_Op_Ne --
6399 --------------------
6401 procedure Expand_N_Op_Ne (N : Node_Id) is
6402 Typ : constant Entity_Id := Etype (Left_Opnd (N));
6405 -- Case of elementary type with standard operator
6407 if Is_Elementary_Type (Typ)
6408 and then Sloc (Entity (N)) = Standard_Location
6410 Binary_Op_Validity_Checks (N);
6412 -- Boolean types (requiring handling of non-standard case)
6414 if Is_Boolean_Type (Typ) then
6415 Adjust_Condition (Left_Opnd (N));
6416 Adjust_Condition (Right_Opnd (N));
6417 Set_Etype (N, Standard_Boolean);
6418 Adjust_Result_Type (N, Typ);
6421 Rewrite_Comparison (N);
6423 -- If we still have comparison for Vax_Float, process it
6425 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
6426 Expand_Vax_Comparison (N);
6430 -- For all cases other than elementary types, we rewrite node as the
6431 -- negation of an equality operation, and reanalyze. The equality to be
6432 -- used is defined in the same scope and has the same signature. This
6433 -- signature must be set explicitly since in an instance it may not have
6434 -- the same visibility as in the generic unit. This avoids duplicating
6435 -- or factoring the complex code for record/array equality tests etc.
6439 Loc : constant Source_Ptr := Sloc (N);
6441 Ne : constant Entity_Id := Entity (N);
6444 Binary_Op_Validity_Checks (N);
6450 Left_Opnd => Left_Opnd (N),
6451 Right_Opnd => Right_Opnd (N)));
6452 Set_Paren_Count (Right_Opnd (Neg), 1);
6454 if Scope (Ne) /= Standard_Standard then
6455 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
6458 -- For navigation purposes, the inequality is treated as an
6459 -- implicit reference to the corresponding equality. Preserve the
6460 -- Comes_From_ source flag so that the proper Xref entry is
6463 Preserve_Comes_From_Source (Neg, N);
6464 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
6466 Analyze_And_Resolve (N, Standard_Boolean);
6471 ---------------------
6472 -- Expand_N_Op_Not --
6473 ---------------------
6475 -- If the argument is other than a Boolean array type, there is no special
6476 -- expansion required.
6478 -- For the packed case, we call the special routine in Exp_Pakd, except
6479 -- that if the component size is greater than one, we use the standard
6480 -- routine generating a gruesome loop (it is so peculiar to have packed
6481 -- arrays with non-standard Boolean representations anyway, so it does not
6482 -- matter that we do not handle this case efficiently).
6484 -- For the unpacked case (and for the special packed case where we have non
6485 -- standard Booleans, as discussed above), we generate and insert into the
6486 -- tree the following function definition:
6488 -- function Nnnn (A : arr) is
6491 -- for J in a'range loop
6492 -- B (J) := not A (J);
6497 -- Here arr is the actual subtype of the parameter (and hence always
6498 -- constrained). Then we replace the not with a call to this function.
6500 procedure Expand_N_Op_Not (N : Node_Id) is
6501 Loc : constant Source_Ptr := Sloc (N);
6502 Typ : constant Entity_Id := Etype (N);
6511 Func_Name : Entity_Id;
6512 Loop_Statement : Node_Id;
6515 Unary_Op_Validity_Checks (N);
6517 -- For boolean operand, deal with non-standard booleans
6519 if Is_Boolean_Type (Typ) then
6520 Adjust_Condition (Right_Opnd (N));
6521 Set_Etype (N, Standard_Boolean);
6522 Adjust_Result_Type (N, Typ);
6526 -- Only array types need any other processing
6528 if not Is_Array_Type (Typ) then
6532 -- Case of array operand. If bit packed with a component size of 1,
6533 -- handle it in Exp_Pakd if the operand is known to be aligned.
6535 if Is_Bit_Packed_Array (Typ)
6536 and then Component_Size (Typ) = 1
6537 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
6539 Expand_Packed_Not (N);
6543 -- Case of array operand which is not bit-packed. If the context is
6544 -- a safe assignment, call in-place operation, If context is a larger
6545 -- boolean expression in the context of a safe assignment, expansion is
6546 -- done by enclosing operation.
6548 Opnd := Relocate_Node (Right_Opnd (N));
6549 Convert_To_Actual_Subtype (Opnd);
6550 Arr := Etype (Opnd);
6551 Ensure_Defined (Arr, N);
6552 Silly_Boolean_Array_Not_Test (N, Arr);
6554 if Nkind (Parent (N)) = N_Assignment_Statement then
6555 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
6556 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
6559 -- Special case the negation of a binary operation
6561 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
6562 and then Safe_In_Place_Array_Op
6563 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
6565 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
6569 elsif Nkind (Parent (N)) in N_Binary_Op
6570 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
6573 Op1 : constant Node_Id := Left_Opnd (Parent (N));
6574 Op2 : constant Node_Id := Right_Opnd (Parent (N));
6575 Lhs : constant Node_Id := Name (Parent (Parent (N)));
6578 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
6580 and then Nkind (Op2) = N_Op_Not
6582 -- (not A) op (not B) can be reduced to a single call
6587 and then Nkind (Parent (N)) = N_Op_Xor
6589 -- A xor (not B) can also be special-cased
6597 A := Make_Defining_Identifier (Loc, Name_uA);
6598 B := Make_Defining_Identifier (Loc, Name_uB);
6599 J := Make_Defining_Identifier (Loc, Name_uJ);
6602 Make_Indexed_Component (Loc,
6603 Prefix => New_Reference_To (A, Loc),
6604 Expressions => New_List (New_Reference_To (J, Loc)));
6607 Make_Indexed_Component (Loc,
6608 Prefix => New_Reference_To (B, Loc),
6609 Expressions => New_List (New_Reference_To (J, Loc)));
6612 Make_Implicit_Loop_Statement (N,
6613 Identifier => Empty,
6616 Make_Iteration_Scheme (Loc,
6617 Loop_Parameter_Specification =>
6618 Make_Loop_Parameter_Specification (Loc,
6619 Defining_Identifier => J,
6620 Discrete_Subtype_Definition =>
6621 Make_Attribute_Reference (Loc,
6622 Prefix => Make_Identifier (Loc, Chars (A)),
6623 Attribute_Name => Name_Range))),
6625 Statements => New_List (
6626 Make_Assignment_Statement (Loc,
6628 Expression => Make_Op_Not (Loc, A_J))));
6630 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('N'));
6631 Set_Is_Inlined (Func_Name);
6634 Make_Subprogram_Body (Loc,
6636 Make_Function_Specification (Loc,
6637 Defining_Unit_Name => Func_Name,
6638 Parameter_Specifications => New_List (
6639 Make_Parameter_Specification (Loc,
6640 Defining_Identifier => A,
6641 Parameter_Type => New_Reference_To (Typ, Loc))),
6642 Result_Definition => New_Reference_To (Typ, Loc)),
6644 Declarations => New_List (
6645 Make_Object_Declaration (Loc,
6646 Defining_Identifier => B,
6647 Object_Definition => New_Reference_To (Arr, Loc))),
6649 Handled_Statement_Sequence =>
6650 Make_Handled_Sequence_Of_Statements (Loc,
6651 Statements => New_List (
6653 Make_Simple_Return_Statement (Loc,
6655 Make_Identifier (Loc, Chars (B)))))));
6658 Make_Function_Call (Loc,
6659 Name => New_Reference_To (Func_Name, Loc),
6660 Parameter_Associations => New_List (Opnd)));
6662 Analyze_And_Resolve (N, Typ);
6663 end Expand_N_Op_Not;
6665 --------------------
6666 -- Expand_N_Op_Or --
6667 --------------------
6669 procedure Expand_N_Op_Or (N : Node_Id) is
6670 Typ : constant Entity_Id := Etype (N);
6673 Binary_Op_Validity_Checks (N);
6675 if Is_Array_Type (Etype (N)) then
6676 Expand_Boolean_Operator (N);
6678 elsif Is_Boolean_Type (Etype (N)) then
6679 Adjust_Condition (Left_Opnd (N));
6680 Adjust_Condition (Right_Opnd (N));
6681 Set_Etype (N, Standard_Boolean);
6682 Adjust_Result_Type (N, Typ);
6686 ----------------------
6687 -- Expand_N_Op_Plus --
6688 ----------------------
6690 procedure Expand_N_Op_Plus (N : Node_Id) is
6692 Unary_Op_Validity_Checks (N);
6693 end Expand_N_Op_Plus;
6695 ---------------------
6696 -- Expand_N_Op_Rem --
6697 ---------------------
6699 procedure Expand_N_Op_Rem (N : Node_Id) is
6700 Loc : constant Source_Ptr := Sloc (N);
6701 Typ : constant Entity_Id := Etype (N);
6703 Left : constant Node_Id := Left_Opnd (N);
6704 Right : constant Node_Id := Right_Opnd (N);
6714 pragma Warnings (Off, Lhi);
6717 Binary_Op_Validity_Checks (N);
6719 if Is_Integer_Type (Etype (N)) then
6720 Apply_Divide_Check (N);
6723 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
6724 -- but it is useful with other back ends (e.g. AAMP), and is certainly
6727 if Is_Integer_Type (Etype (N))
6728 and then Compile_Time_Known_Value (Right)
6729 and then Expr_Value (Right) = Uint_1
6731 -- Call Remove_Side_Effects to ensure that any side effects in the
6732 -- ignored left operand (in particular function calls to user defined
6733 -- functions) are properly executed.
6735 Remove_Side_Effects (Left);
6737 Rewrite (N, Make_Integer_Literal (Loc, 0));
6738 Analyze_And_Resolve (N, Typ);
6742 -- Deal with annoying case of largest negative number remainder minus
6743 -- one. Gigi does not handle this case correctly, because it generates
6744 -- a divide instruction which may trap in this case.
6746 -- In fact the check is quite easy, if the right operand is -1, then
6747 -- the remainder is always 0, and we can just ignore the left operand
6748 -- completely in this case.
6750 Determine_Range (Right, ROK, Rlo, Rhi);
6751 Determine_Range (Left, LOK, Llo, Lhi);
6753 -- The operand type may be private (e.g. in the expansion of an
6754 -- intrinsic operation) so we must use the underlying type to get the
6755 -- bounds, and convert the literals explicitly.
6759 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
6761 -- Now perform the test, generating code only if needed
6763 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
6765 ((not LOK) or else (Llo = LLB))
6768 Make_Conditional_Expression (Loc,
6769 Expressions => New_List (
6771 Left_Opnd => Duplicate_Subexpr (Right),
6773 Unchecked_Convert_To (Typ,
6774 Make_Integer_Literal (Loc, -1))),
6776 Unchecked_Convert_To (Typ,
6777 Make_Integer_Literal (Loc, Uint_0)),
6779 Relocate_Node (N))));
6781 Set_Analyzed (Next (Next (First (Expressions (N)))));
6782 Analyze_And_Resolve (N, Typ);
6784 end Expand_N_Op_Rem;
6786 -----------------------------
6787 -- Expand_N_Op_Rotate_Left --
6788 -----------------------------
6790 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
6792 Binary_Op_Validity_Checks (N);
6793 end Expand_N_Op_Rotate_Left;
6795 ------------------------------
6796 -- Expand_N_Op_Rotate_Right --
6797 ------------------------------
6799 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
6801 Binary_Op_Validity_Checks (N);
6802 end Expand_N_Op_Rotate_Right;
6804 ----------------------------
6805 -- Expand_N_Op_Shift_Left --
6806 ----------------------------
6808 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
6810 Binary_Op_Validity_Checks (N);
6811 end Expand_N_Op_Shift_Left;
6813 -----------------------------
6814 -- Expand_N_Op_Shift_Right --
6815 -----------------------------
6817 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
6819 Binary_Op_Validity_Checks (N);
6820 end Expand_N_Op_Shift_Right;
6822 ----------------------------------------
6823 -- Expand_N_Op_Shift_Right_Arithmetic --
6824 ----------------------------------------
6826 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
6828 Binary_Op_Validity_Checks (N);
6829 end Expand_N_Op_Shift_Right_Arithmetic;
6831 --------------------------
6832 -- Expand_N_Op_Subtract --
6833 --------------------------
6835 procedure Expand_N_Op_Subtract (N : Node_Id) is
6836 Typ : constant Entity_Id := Etype (N);
6839 Binary_Op_Validity_Checks (N);
6841 -- N - 0 = N for integer types
6843 if Is_Integer_Type (Typ)
6844 and then Compile_Time_Known_Value (Right_Opnd (N))
6845 and then Expr_Value (Right_Opnd (N)) = 0
6847 Rewrite (N, Left_Opnd (N));
6851 -- Arithmetic overflow checks for signed integer/fixed point types
6853 if Is_Signed_Integer_Type (Typ)
6854 or else Is_Fixed_Point_Type (Typ)
6856 Apply_Arithmetic_Overflow_Check (N);
6858 -- Vax floating-point types case
6860 elsif Vax_Float (Typ) then
6861 Expand_Vax_Arith (N);
6863 end Expand_N_Op_Subtract;
6865 ---------------------
6866 -- Expand_N_Op_Xor --
6867 ---------------------
6869 procedure Expand_N_Op_Xor (N : Node_Id) is
6870 Typ : constant Entity_Id := Etype (N);
6873 Binary_Op_Validity_Checks (N);
6875 if Is_Array_Type (Etype (N)) then
6876 Expand_Boolean_Operator (N);
6878 elsif Is_Boolean_Type (Etype (N)) then
6879 Adjust_Condition (Left_Opnd (N));
6880 Adjust_Condition (Right_Opnd (N));
6881 Set_Etype (N, Standard_Boolean);
6882 Adjust_Result_Type (N, Typ);
6884 end Expand_N_Op_Xor;
6886 ----------------------
6887 -- Expand_N_Or_Else --
6888 ----------------------
6890 -- Expand into conditional expression if Actions present, and also
6891 -- deal with optimizing case of arguments being True or False.
6893 procedure Expand_N_Or_Else (N : Node_Id) is
6894 Loc : constant Source_Ptr := Sloc (N);
6895 Typ : constant Entity_Id := Etype (N);
6896 Left : constant Node_Id := Left_Opnd (N);
6897 Right : constant Node_Id := Right_Opnd (N);
6901 -- Deal with non-standard booleans
6903 if Is_Boolean_Type (Typ) then
6904 Adjust_Condition (Left);
6905 Adjust_Condition (Right);
6906 Set_Etype (N, Standard_Boolean);
6909 -- Check for cases where left argument is known to be True or False
6911 if Compile_Time_Known_Value (Left) then
6913 -- If left argument is False, change (False or else Right) to Right.
6914 -- Any actions associated with Right will be executed unconditionally
6915 -- and can thus be inserted into the tree unconditionally.
6917 if Expr_Value_E (Left) = Standard_False then
6918 if Present (Actions (N)) then
6919 Insert_Actions (N, Actions (N));
6924 -- If left argument is True, change (True and then Right) to True. In
6925 -- this case we can forget the actions associated with Right, since
6926 -- they will never be executed.
6928 else pragma Assert (Expr_Value_E (Left) = Standard_True);
6929 Kill_Dead_Code (Right);
6930 Kill_Dead_Code (Actions (N));
6931 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
6934 Adjust_Result_Type (N, Typ);
6938 -- If Actions are present, we expand
6940 -- left or else right
6944 -- if left then True else right end
6946 -- with the actions becoming the Else_Actions of the conditional
6947 -- expression. This conditional expression is then further expanded
6948 -- (and will eventually disappear)
6950 if Present (Actions (N)) then
6951 Actlist := Actions (N);
6953 Make_Conditional_Expression (Loc,
6954 Expressions => New_List (
6956 New_Occurrence_Of (Standard_True, Loc),
6959 Set_Else_Actions (N, Actlist);
6960 Analyze_And_Resolve (N, Standard_Boolean);
6961 Adjust_Result_Type (N, Typ);
6965 -- No actions present, check for cases of right argument True/False
6967 if Compile_Time_Known_Value (Right) then
6969 -- Change (Left or else False) to Left. Note that we know there are
6970 -- no actions associated with the True operand, since we just checked
6971 -- for this case above.
6973 if Expr_Value_E (Right) = Standard_False then
6976 -- Change (Left or else True) to True, making sure to preserve any
6977 -- side effects associated with the Left operand.
6979 else pragma Assert (Expr_Value_E (Right) = Standard_True);
6980 Remove_Side_Effects (Left);
6982 (N, New_Occurrence_Of (Standard_True, Loc));
6986 Adjust_Result_Type (N, Typ);
6987 end Expand_N_Or_Else;
6989 -----------------------------------
6990 -- Expand_N_Qualified_Expression --
6991 -----------------------------------
6993 procedure Expand_N_Qualified_Expression (N : Node_Id) is
6994 Operand : constant Node_Id := Expression (N);
6995 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
6998 -- Do validity check if validity checking operands
7000 if Validity_Checks_On
7001 and then Validity_Check_Operands
7003 Ensure_Valid (Operand);
7006 -- Apply possible constraint check
7008 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
7009 end Expand_N_Qualified_Expression;
7011 ---------------------------------
7012 -- Expand_N_Selected_Component --
7013 ---------------------------------
7015 -- If the selector is a discriminant of a concurrent object, rewrite the
7016 -- prefix to denote the corresponding record type.
7018 procedure Expand_N_Selected_Component (N : Node_Id) is
7019 Loc : constant Source_Ptr := Sloc (N);
7020 Par : constant Node_Id := Parent (N);
7021 P : constant Node_Id := Prefix (N);
7022 Ptyp : Entity_Id := Underlying_Type (Etype (P));
7027 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
7028 -- Gigi needs a temporary for prefixes that depend on a discriminant,
7029 -- unless the context of an assignment can provide size information.
7030 -- Don't we have a general routine that does this???
7032 -----------------------
7033 -- In_Left_Hand_Side --
7034 -----------------------
7036 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
7038 return (Nkind (Parent (Comp)) = N_Assignment_Statement
7039 and then Comp = Name (Parent (Comp)))
7040 or else (Present (Parent (Comp))
7041 and then Nkind (Parent (Comp)) in N_Subexpr
7042 and then In_Left_Hand_Side (Parent (Comp)));
7043 end In_Left_Hand_Side;
7045 -- Start of processing for Expand_N_Selected_Component
7048 -- Insert explicit dereference if required
7050 if Is_Access_Type (Ptyp) then
7051 Insert_Explicit_Dereference (P);
7052 Analyze_And_Resolve (P, Designated_Type (Ptyp));
7054 if Ekind (Etype (P)) = E_Private_Subtype
7055 and then Is_For_Access_Subtype (Etype (P))
7057 Set_Etype (P, Base_Type (Etype (P)));
7063 -- Deal with discriminant check required
7065 if Do_Discriminant_Check (N) then
7067 -- Present the discriminant checking function to the backend, so that
7068 -- it can inline the call to the function.
7071 (Discriminant_Checking_Func
7072 (Original_Record_Component (Entity (Selector_Name (N)))));
7074 -- Now reset the flag and generate the call
7076 Set_Do_Discriminant_Check (N, False);
7077 Generate_Discriminant_Check (N);
7080 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7081 -- function, then additional actuals must be passed.
7083 if Ada_Version >= Ada_05
7084 and then Is_Build_In_Place_Function_Call (P)
7086 Make_Build_In_Place_Call_In_Anonymous_Context (P);
7089 -- Gigi cannot handle unchecked conversions that are the prefix of a
7090 -- selected component with discriminants. This must be checked during
7091 -- expansion, because during analysis the type of the selector is not
7092 -- known at the point the prefix is analyzed. If the conversion is the
7093 -- target of an assignment, then we cannot force the evaluation.
7095 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
7096 and then Has_Discriminants (Etype (N))
7097 and then not In_Left_Hand_Side (N)
7099 Force_Evaluation (Prefix (N));
7102 -- Remaining processing applies only if selector is a discriminant
7104 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
7106 -- If the selector is a discriminant of a constrained record type,
7107 -- we may be able to rewrite the expression with the actual value
7108 -- of the discriminant, a useful optimization in some cases.
7110 if Is_Record_Type (Ptyp)
7111 and then Has_Discriminants (Ptyp)
7112 and then Is_Constrained (Ptyp)
7114 -- Do this optimization for discrete types only, and not for
7115 -- access types (access discriminants get us into trouble!)
7117 if not Is_Discrete_Type (Etype (N)) then
7120 -- Don't do this on the left hand of an assignment statement.
7121 -- Normally one would think that references like this would
7122 -- not occur, but they do in generated code, and mean that
7123 -- we really do want to assign the discriminant!
7125 elsif Nkind (Par) = N_Assignment_Statement
7126 and then Name (Par) = N
7130 -- Don't do this optimization for the prefix of an attribute or
7131 -- the operand of an object renaming declaration since these are
7132 -- contexts where we do not want the value anyway.
7134 elsif (Nkind (Par) = N_Attribute_Reference
7135 and then Prefix (Par) = N)
7136 or else Is_Renamed_Object (N)
7140 -- Don't do this optimization if we are within the code for a
7141 -- discriminant check, since the whole point of such a check may
7142 -- be to verify the condition on which the code below depends!
7144 elsif Is_In_Discriminant_Check (N) then
7147 -- Green light to see if we can do the optimization. There is
7148 -- still one condition that inhibits the optimization below but
7149 -- now is the time to check the particular discriminant.
7152 -- Loop through discriminants to find the matching discriminant
7153 -- constraint to see if we can copy it.
7155 Disc := First_Discriminant (Ptyp);
7156 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
7157 Discr_Loop : while Present (Dcon) loop
7159 -- Check if this is the matching discriminant
7161 if Disc = Entity (Selector_Name (N)) then
7163 -- Here we have the matching discriminant. Check for
7164 -- the case of a discriminant of a component that is
7165 -- constrained by an outer discriminant, which cannot
7166 -- be optimized away.
7169 Denotes_Discriminant
7170 (Node (Dcon), Check_Concurrent => True)
7174 -- In the context of a case statement, the expression may
7175 -- have the base type of the discriminant, and we need to
7176 -- preserve the constraint to avoid spurious errors on
7179 elsif Nkind (Parent (N)) = N_Case_Statement
7180 and then Etype (Node (Dcon)) /= Etype (Disc)
7183 Make_Qualified_Expression (Loc,
7185 New_Occurrence_Of (Etype (Disc), Loc),
7187 New_Copy_Tree (Node (Dcon))));
7188 Analyze_And_Resolve (N, Etype (Disc));
7190 -- In case that comes out as a static expression,
7191 -- reset it (a selected component is never static).
7193 Set_Is_Static_Expression (N, False);
7196 -- Otherwise we can just copy the constraint, but the
7197 -- result is certainly not static! In some cases the
7198 -- discriminant constraint has been analyzed in the
7199 -- context of the original subtype indication, but for
7200 -- itypes the constraint might not have been analyzed
7201 -- yet, and this must be done now.
7204 Rewrite (N, New_Copy_Tree (Node (Dcon)));
7205 Analyze_And_Resolve (N);
7206 Set_Is_Static_Expression (N, False);
7212 Next_Discriminant (Disc);
7213 end loop Discr_Loop;
7215 -- Note: the above loop should always find a matching
7216 -- discriminant, but if it does not, we just missed an
7217 -- optimization due to some glitch (perhaps a previous error),
7223 -- The only remaining processing is in the case of a discriminant of
7224 -- a concurrent object, where we rewrite the prefix to denote the
7225 -- corresponding record type. If the type is derived and has renamed
7226 -- discriminants, use corresponding discriminant, which is the one
7227 -- that appears in the corresponding record.
7229 if not Is_Concurrent_Type (Ptyp) then
7233 Disc := Entity (Selector_Name (N));
7235 if Is_Derived_Type (Ptyp)
7236 and then Present (Corresponding_Discriminant (Disc))
7238 Disc := Corresponding_Discriminant (Disc);
7242 Make_Selected_Component (Loc,
7244 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
7246 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
7251 end Expand_N_Selected_Component;
7253 --------------------
7254 -- Expand_N_Slice --
7255 --------------------
7257 procedure Expand_N_Slice (N : Node_Id) is
7258 Loc : constant Source_Ptr := Sloc (N);
7259 Typ : constant Entity_Id := Etype (N);
7260 Pfx : constant Node_Id := Prefix (N);
7261 Ptp : Entity_Id := Etype (Pfx);
7263 function Is_Procedure_Actual (N : Node_Id) return Boolean;
7264 -- Check whether the argument is an actual for a procedure call, in
7265 -- which case the expansion of a bit-packed slice is deferred until the
7266 -- call itself is expanded. The reason this is required is that we might
7267 -- have an IN OUT or OUT parameter, and the copy out is essential, and
7268 -- that copy out would be missed if we created a temporary here in
7269 -- Expand_N_Slice. Note that we don't bother to test specifically for an
7270 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
7271 -- is harmless to defer expansion in the IN case, since the call
7272 -- processing will still generate the appropriate copy in operation,
7273 -- which will take care of the slice.
7275 procedure Make_Temporary;
7276 -- Create a named variable for the value of the slice, in cases where
7277 -- the back-end cannot handle it properly, e.g. when packed types or
7278 -- unaligned slices are involved.
7280 -------------------------
7281 -- Is_Procedure_Actual --
7282 -------------------------
7284 function Is_Procedure_Actual (N : Node_Id) return Boolean is
7285 Par : Node_Id := Parent (N);
7289 -- If our parent is a procedure call we can return
7291 if Nkind (Par) = N_Procedure_Call_Statement then
7294 -- If our parent is a type conversion, keep climbing the tree,
7295 -- since a type conversion can be a procedure actual. Also keep
7296 -- climbing if parameter association or a qualified expression,
7297 -- since these are additional cases that do can appear on
7298 -- procedure actuals.
7300 elsif Nkind_In (Par, N_Type_Conversion,
7301 N_Parameter_Association,
7302 N_Qualified_Expression)
7304 Par := Parent (Par);
7306 -- Any other case is not what we are looking for
7312 end Is_Procedure_Actual;
7314 --------------------
7315 -- Make_Temporary --
7316 --------------------
7318 procedure Make_Temporary is
7320 Ent : constant Entity_Id :=
7321 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
7324 Make_Object_Declaration (Loc,
7325 Defining_Identifier => Ent,
7326 Object_Definition => New_Occurrence_Of (Typ, Loc));
7328 Set_No_Initialization (Decl);
7330 Insert_Actions (N, New_List (
7332 Make_Assignment_Statement (Loc,
7333 Name => New_Occurrence_Of (Ent, Loc),
7334 Expression => Relocate_Node (N))));
7336 Rewrite (N, New_Occurrence_Of (Ent, Loc));
7337 Analyze_And_Resolve (N, Typ);
7340 -- Start of processing for Expand_N_Slice
7343 -- Special handling for access types
7345 if Is_Access_Type (Ptp) then
7347 Ptp := Designated_Type (Ptp);
7350 Make_Explicit_Dereference (Sloc (N),
7351 Prefix => Relocate_Node (Pfx)));
7353 Analyze_And_Resolve (Pfx, Ptp);
7356 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7357 -- function, then additional actuals must be passed.
7359 if Ada_Version >= Ada_05
7360 and then Is_Build_In_Place_Function_Call (Pfx)
7362 Make_Build_In_Place_Call_In_Anonymous_Context (Pfx);
7365 -- Range checks are potentially also needed for cases involving a slice
7366 -- indexed by a subtype indication, but Do_Range_Check can currently
7367 -- only be set for expressions ???
7369 if not Index_Checks_Suppressed (Ptp)
7370 and then (not Is_Entity_Name (Pfx)
7371 or else not Index_Checks_Suppressed (Entity (Pfx)))
7372 and then Nkind (Discrete_Range (N)) /= N_Subtype_Indication
7374 -- Do not enable range check to nodes associated with the frontend
7375 -- expansion of the dispatch table. We first check if Ada.Tags is
7376 -- already loaded to avoid the addition of an undesired dependence
7377 -- on such run-time unit.
7382 (RTU_Loaded (Ada_Tags)
7383 and then Nkind (Prefix (N)) = N_Selected_Component
7384 and then Present (Entity (Selector_Name (Prefix (N))))
7385 and then Entity (Selector_Name (Prefix (N))) =
7386 RTE_Record_Component (RE_Prims_Ptr)))
7388 Enable_Range_Check (Discrete_Range (N));
7391 -- The remaining case to be handled is packed slices. We can leave
7392 -- packed slices as they are in the following situations:
7394 -- 1. Right or left side of an assignment (we can handle this
7395 -- situation correctly in the assignment statement expansion).
7397 -- 2. Prefix of indexed component (the slide is optimized away in this
7398 -- case, see the start of Expand_N_Slice.)
7400 -- 3. Object renaming declaration, since we want the name of the
7401 -- slice, not the value.
7403 -- 4. Argument to procedure call, since copy-in/copy-out handling may
7404 -- be required, and this is handled in the expansion of call
7407 -- 5. Prefix of an address attribute (this is an error which is caught
7408 -- elsewhere, and the expansion would interfere with generating the
7411 if not Is_Packed (Typ) then
7413 -- Apply transformation for actuals of a function call, where
7414 -- Expand_Actuals is not used.
7416 if Nkind (Parent (N)) = N_Function_Call
7417 and then Is_Possibly_Unaligned_Slice (N)
7422 elsif Nkind (Parent (N)) = N_Assignment_Statement
7423 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
7424 and then Parent (N) = Name (Parent (Parent (N))))
7428 elsif Nkind (Parent (N)) = N_Indexed_Component
7429 or else Is_Renamed_Object (N)
7430 or else Is_Procedure_Actual (N)
7434 elsif Nkind (Parent (N)) = N_Attribute_Reference
7435 and then Attribute_Name (Parent (N)) = Name_Address
7444 ------------------------------
7445 -- Expand_N_Type_Conversion --
7446 ------------------------------
7448 procedure Expand_N_Type_Conversion (N : Node_Id) is
7449 Loc : constant Source_Ptr := Sloc (N);
7450 Operand : constant Node_Id := Expression (N);
7451 Target_Type : constant Entity_Id := Etype (N);
7452 Operand_Type : Entity_Id := Etype (Operand);
7454 procedure Handle_Changed_Representation;
7455 -- This is called in the case of record and array type conversions to
7456 -- see if there is a change of representation to be handled. Change of
7457 -- representation is actually handled at the assignment statement level,
7458 -- and what this procedure does is rewrite node N conversion as an
7459 -- assignment to temporary. If there is no change of representation,
7460 -- then the conversion node is unchanged.
7462 procedure Real_Range_Check;
7463 -- Handles generation of range check for real target value
7465 -----------------------------------
7466 -- Handle_Changed_Representation --
7467 -----------------------------------
7469 procedure Handle_Changed_Representation is
7478 -- Nothing else to do if no change of representation
7480 if Same_Representation (Operand_Type, Target_Type) then
7483 -- The real change of representation work is done by the assignment
7484 -- statement processing. So if this type conversion is appearing as
7485 -- the expression of an assignment statement, nothing needs to be
7486 -- done to the conversion.
7488 elsif Nkind (Parent (N)) = N_Assignment_Statement then
7491 -- Otherwise we need to generate a temporary variable, and do the
7492 -- change of representation assignment into that temporary variable.
7493 -- The conversion is then replaced by a reference to this variable.
7498 -- If type is unconstrained we have to add a constraint, copied
7499 -- from the actual value of the left hand side.
7501 if not Is_Constrained (Target_Type) then
7502 if Has_Discriminants (Operand_Type) then
7503 Disc := First_Discriminant (Operand_Type);
7505 if Disc /= First_Stored_Discriminant (Operand_Type) then
7506 Disc := First_Stored_Discriminant (Operand_Type);
7510 while Present (Disc) loop
7512 Make_Selected_Component (Loc,
7513 Prefix => Duplicate_Subexpr_Move_Checks (Operand),
7515 Make_Identifier (Loc, Chars (Disc))));
7516 Next_Discriminant (Disc);
7519 elsif Is_Array_Type (Operand_Type) then
7520 N_Ix := First_Index (Target_Type);
7523 for J in 1 .. Number_Dimensions (Operand_Type) loop
7525 -- We convert the bounds explicitly. We use an unchecked
7526 -- conversion because bounds checks are done elsewhere.
7531 Unchecked_Convert_To (Etype (N_Ix),
7532 Make_Attribute_Reference (Loc,
7534 Duplicate_Subexpr_No_Checks
7535 (Operand, Name_Req => True),
7536 Attribute_Name => Name_First,
7537 Expressions => New_List (
7538 Make_Integer_Literal (Loc, J)))),
7541 Unchecked_Convert_To (Etype (N_Ix),
7542 Make_Attribute_Reference (Loc,
7544 Duplicate_Subexpr_No_Checks
7545 (Operand, Name_Req => True),
7546 Attribute_Name => Name_Last,
7547 Expressions => New_List (
7548 Make_Integer_Literal (Loc, J))))));
7555 Odef := New_Occurrence_Of (Target_Type, Loc);
7557 if Present (Cons) then
7559 Make_Subtype_Indication (Loc,
7560 Subtype_Mark => Odef,
7562 Make_Index_Or_Discriminant_Constraint (Loc,
7563 Constraints => Cons));
7566 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
7568 Make_Object_Declaration (Loc,
7569 Defining_Identifier => Temp,
7570 Object_Definition => Odef);
7572 Set_No_Initialization (Decl, True);
7574 -- Insert required actions. It is essential to suppress checks
7575 -- since we have suppressed default initialization, which means
7576 -- that the variable we create may have no discriminants.
7581 Make_Assignment_Statement (Loc,
7582 Name => New_Occurrence_Of (Temp, Loc),
7583 Expression => Relocate_Node (N))),
7584 Suppress => All_Checks);
7586 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7589 end Handle_Changed_Representation;
7591 ----------------------
7592 -- Real_Range_Check --
7593 ----------------------
7595 -- Case of conversions to floating-point or fixed-point. If range checks
7596 -- are enabled and the target type has a range constraint, we convert:
7602 -- Tnn : typ'Base := typ'Base (x);
7603 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
7606 -- This is necessary when there is a conversion of integer to float or
7607 -- to fixed-point to ensure that the correct checks are made. It is not
7608 -- necessary for float to float where it is enough to simply set the
7609 -- Do_Range_Check flag.
7611 procedure Real_Range_Check is
7612 Btyp : constant Entity_Id := Base_Type (Target_Type);
7613 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
7614 Hi : constant Node_Id := Type_High_Bound (Target_Type);
7615 Xtyp : constant Entity_Id := Etype (Operand);
7620 -- Nothing to do if conversion was rewritten
7622 if Nkind (N) /= N_Type_Conversion then
7626 -- Nothing to do if range checks suppressed, or target has the same
7627 -- range as the base type (or is the base type).
7629 if Range_Checks_Suppressed (Target_Type)
7630 or else (Lo = Type_Low_Bound (Btyp)
7632 Hi = Type_High_Bound (Btyp))
7637 -- Nothing to do if expression is an entity on which checks have been
7640 if Is_Entity_Name (Operand)
7641 and then Range_Checks_Suppressed (Entity (Operand))
7646 -- Nothing to do if bounds are all static and we can tell that the
7647 -- expression is within the bounds of the target. Note that if the
7648 -- operand is of an unconstrained floating-point type, then we do
7649 -- not trust it to be in range (might be infinite)
7652 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
7653 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
7656 if (not Is_Floating_Point_Type (Xtyp)
7657 or else Is_Constrained (Xtyp))
7658 and then Compile_Time_Known_Value (S_Lo)
7659 and then Compile_Time_Known_Value (S_Hi)
7660 and then Compile_Time_Known_Value (Hi)
7661 and then Compile_Time_Known_Value (Lo)
7664 D_Lov : constant Ureal := Expr_Value_R (Lo);
7665 D_Hiv : constant Ureal := Expr_Value_R (Hi);
7670 if Is_Real_Type (Xtyp) then
7671 S_Lov := Expr_Value_R (S_Lo);
7672 S_Hiv := Expr_Value_R (S_Hi);
7674 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
7675 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
7679 and then S_Lov >= D_Lov
7680 and then S_Hiv <= D_Hiv
7682 Set_Do_Range_Check (Operand, False);
7689 -- For float to float conversions, we are done
7691 if Is_Floating_Point_Type (Xtyp)
7693 Is_Floating_Point_Type (Btyp)
7698 -- Otherwise rewrite the conversion as described above
7700 Conv := Relocate_Node (N);
7702 (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
7703 Set_Etype (Conv, Btyp);
7705 -- Enable overflow except for case of integer to float conversions,
7706 -- where it is never required, since we can never have overflow in
7709 if not Is_Integer_Type (Etype (Operand)) then
7710 Enable_Overflow_Check (Conv);
7714 Make_Defining_Identifier (Loc,
7715 Chars => New_Internal_Name ('T'));
7717 Insert_Actions (N, New_List (
7718 Make_Object_Declaration (Loc,
7719 Defining_Identifier => Tnn,
7720 Object_Definition => New_Occurrence_Of (Btyp, Loc),
7721 Expression => Conv),
7723 Make_Raise_Constraint_Error (Loc,
7728 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
7730 Make_Attribute_Reference (Loc,
7731 Attribute_Name => Name_First,
7733 New_Occurrence_Of (Target_Type, Loc))),
7737 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
7739 Make_Attribute_Reference (Loc,
7740 Attribute_Name => Name_Last,
7742 New_Occurrence_Of (Target_Type, Loc)))),
7743 Reason => CE_Range_Check_Failed)));
7745 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
7746 Analyze_And_Resolve (N, Btyp);
7747 end Real_Range_Check;
7749 -- Start of processing for Expand_N_Type_Conversion
7752 -- Nothing at all to do if conversion is to the identical type so remove
7753 -- the conversion completely, it is useless.
7755 if Operand_Type = Target_Type then
7756 Rewrite (N, Relocate_Node (Operand));
7760 -- Nothing to do if this is the second argument of read. This is a
7761 -- "backwards" conversion that will be handled by the specialized code
7762 -- in attribute processing.
7764 if Nkind (Parent (N)) = N_Attribute_Reference
7765 and then Attribute_Name (Parent (N)) = Name_Read
7766 and then Next (First (Expressions (Parent (N)))) = N
7771 -- Here if we may need to expand conversion
7773 -- Do validity check if validity checking operands
7775 if Validity_Checks_On
7776 and then Validity_Check_Operands
7778 Ensure_Valid (Operand);
7781 -- Special case of converting from non-standard boolean type
7783 if Is_Boolean_Type (Operand_Type)
7784 and then (Nonzero_Is_True (Operand_Type))
7786 Adjust_Condition (Operand);
7787 Set_Etype (Operand, Standard_Boolean);
7788 Operand_Type := Standard_Boolean;
7791 -- Case of converting to an access type
7793 if Is_Access_Type (Target_Type) then
7795 -- Apply an accessibility check when the conversion operand is an
7796 -- access parameter (or a renaming thereof), unless conversion was
7797 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
7798 -- Note that other checks may still need to be applied below (such
7799 -- as tagged type checks).
7801 if Is_Entity_Name (Operand)
7803 (Is_Formal (Entity (Operand))
7805 (Present (Renamed_Object (Entity (Operand)))
7806 and then Is_Entity_Name (Renamed_Object (Entity (Operand)))
7808 (Entity (Renamed_Object (Entity (Operand))))))
7809 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
7810 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
7811 or else Attribute_Name (Original_Node (N)) = Name_Access)
7813 Apply_Accessibility_Check
7814 (Operand, Target_Type, Insert_Node => Operand);
7816 -- If the level of the operand type is statically deeper than the
7817 -- level of the target type, then force Program_Error. Note that this
7818 -- can only occur for cases where the attribute is within the body of
7819 -- an instantiation (otherwise the conversion will already have been
7820 -- rejected as illegal). Note: warnings are issued by the analyzer
7821 -- for the instance cases.
7823 elsif In_Instance_Body
7824 and then Type_Access_Level (Operand_Type) >
7825 Type_Access_Level (Target_Type)
7828 Make_Raise_Program_Error (Sloc (N),
7829 Reason => PE_Accessibility_Check_Failed));
7830 Set_Etype (N, Target_Type);
7832 -- When the operand is a selected access discriminant the check needs
7833 -- to be made against the level of the object denoted by the prefix
7834 -- of the selected name. Force Program_Error for this case as well
7835 -- (this accessibility violation can only happen if within the body
7836 -- of an instantiation).
7838 elsif In_Instance_Body
7839 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
7840 and then Nkind (Operand) = N_Selected_Component
7841 and then Object_Access_Level (Operand) >
7842 Type_Access_Level (Target_Type)
7845 Make_Raise_Program_Error (Sloc (N),
7846 Reason => PE_Accessibility_Check_Failed));
7847 Set_Etype (N, Target_Type);
7851 -- Case of conversions of tagged types and access to tagged types
7853 -- When needed, that is to say when the expression is class-wide, Add
7854 -- runtime a tag check for (strict) downward conversion by using the
7855 -- membership test, generating:
7857 -- [constraint_error when Operand not in Target_Type'Class]
7859 -- or in the access type case
7861 -- [constraint_error
7862 -- when Operand /= null
7863 -- and then Operand.all not in
7864 -- Designated_Type (Target_Type)'Class]
7866 if (Is_Access_Type (Target_Type)
7867 and then Is_Tagged_Type (Designated_Type (Target_Type)))
7868 or else Is_Tagged_Type (Target_Type)
7870 -- Do not do any expansion in the access type case if the parent is a
7871 -- renaming, since this is an error situation which will be caught by
7872 -- Sem_Ch8, and the expansion can interfere with this error check.
7874 if Is_Access_Type (Target_Type)
7875 and then Is_Renamed_Object (N)
7880 -- Otherwise, proceed with processing tagged conversion
7883 Actual_Op_Typ : Entity_Id;
7884 Actual_Targ_Typ : Entity_Id;
7885 Make_Conversion : Boolean := False;
7886 Root_Op_Typ : Entity_Id;
7888 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
7889 -- Create a membership check to test whether Operand is a member
7890 -- of Targ_Typ. If the original Target_Type is an access, include
7891 -- a test for null value. The check is inserted at N.
7893 --------------------
7894 -- Make_Tag_Check --
7895 --------------------
7897 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
7902 -- [Constraint_Error
7903 -- when Operand /= null
7904 -- and then Operand.all not in Targ_Typ]
7906 if Is_Access_Type (Target_Type) then
7911 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
7912 Right_Opnd => Make_Null (Loc)),
7917 Make_Explicit_Dereference (Loc,
7918 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
7919 Right_Opnd => New_Reference_To (Targ_Typ, Loc)));
7922 -- [Constraint_Error when Operand not in Targ_Typ]
7927 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
7928 Right_Opnd => New_Reference_To (Targ_Typ, Loc));
7932 Make_Raise_Constraint_Error (Loc,
7934 Reason => CE_Tag_Check_Failed));
7937 -- Start of processing
7940 if Is_Access_Type (Target_Type) then
7941 Actual_Op_Typ := Designated_Type (Operand_Type);
7942 Actual_Targ_Typ := Designated_Type (Target_Type);
7945 Actual_Op_Typ := Operand_Type;
7946 Actual_Targ_Typ := Target_Type;
7949 Root_Op_Typ := Root_Type (Actual_Op_Typ);
7951 -- Ada 2005 (AI-251): Handle interface type conversion
7953 if Is_Interface (Actual_Op_Typ) then
7954 Expand_Interface_Conversion (N, Is_Static => False);
7958 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
7960 -- Create a runtime tag check for a downward class-wide type
7963 if Is_Class_Wide_Type (Actual_Op_Typ)
7964 and then Root_Op_Typ /= Actual_Targ_Typ
7965 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ)
7967 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
7968 Make_Conversion := True;
7971 -- AI05-0073: If the result subtype of the function is defined
7972 -- by an access_definition designating a specific tagged type
7973 -- T, a check is made that the result value is null or the tag
7974 -- of the object designated by the result value identifies T.
7975 -- Constraint_Error is raised if this check fails.
7977 if Nkind (Parent (N)) = Sinfo.N_Return_Statement then
7980 Func_Typ : Entity_Id;
7983 -- Climb scope stack looking for the enclosing function
7985 Func := Current_Scope;
7986 while Present (Func)
7987 and then Ekind (Func) /= E_Function
7989 Func := Scope (Func);
7992 -- The function's return subtype must be defined using
7993 -- an access definition.
7995 if Nkind (Result_Definition (Parent (Func))) =
7998 Func_Typ := Directly_Designated_Type (Etype (Func));
8000 -- The return subtype denotes a specific tagged type,
8001 -- in other words, a non class-wide type.
8003 if Is_Tagged_Type (Func_Typ)
8004 and then not Is_Class_Wide_Type (Func_Typ)
8006 Make_Tag_Check (Actual_Targ_Typ);
8007 Make_Conversion := True;
8013 -- We have generated a tag check for either a class-wide type
8014 -- conversion or for AI05-0073.
8016 if Make_Conversion then
8021 Make_Unchecked_Type_Conversion (Loc,
8022 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
8023 Expression => Relocate_Node (Expression (N)));
8025 Analyze_And_Resolve (N, Target_Type);
8031 -- Case of other access type conversions
8033 elsif Is_Access_Type (Target_Type) then
8034 Apply_Constraint_Check (Operand, Target_Type);
8036 -- Case of conversions from a fixed-point type
8038 -- These conversions require special expansion and processing, found in
8039 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
8040 -- since from a semantic point of view, these are simple integer
8041 -- conversions, which do not need further processing.
8043 elsif Is_Fixed_Point_Type (Operand_Type)
8044 and then not Conversion_OK (N)
8046 -- We should never see universal fixed at this case, since the
8047 -- expansion of the constituent divide or multiply should have
8048 -- eliminated the explicit mention of universal fixed.
8050 pragma Assert (Operand_Type /= Universal_Fixed);
8052 -- Check for special case of the conversion to universal real that
8053 -- occurs as a result of the use of a round attribute. In this case,
8054 -- the real type for the conversion is taken from the target type of
8055 -- the Round attribute and the result must be marked as rounded.
8057 if Target_Type = Universal_Real
8058 and then Nkind (Parent (N)) = N_Attribute_Reference
8059 and then Attribute_Name (Parent (N)) = Name_Round
8061 Set_Rounded_Result (N);
8062 Set_Etype (N, Etype (Parent (N)));
8065 -- Otherwise do correct fixed-conversion, but skip these if the
8066 -- Conversion_OK flag is set, because from a semantic point of
8067 -- view these are simple integer conversions needing no further
8068 -- processing (the backend will simply treat them as integers)
8070 if not Conversion_OK (N) then
8071 if Is_Fixed_Point_Type (Etype (N)) then
8072 Expand_Convert_Fixed_To_Fixed (N);
8075 elsif Is_Integer_Type (Etype (N)) then
8076 Expand_Convert_Fixed_To_Integer (N);
8079 pragma Assert (Is_Floating_Point_Type (Etype (N)));
8080 Expand_Convert_Fixed_To_Float (N);
8085 -- Case of conversions to a fixed-point type
8087 -- These conversions require special expansion and processing, found in
8088 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
8089 -- since from a semantic point of view, these are simple integer
8090 -- conversions, which do not need further processing.
8092 elsif Is_Fixed_Point_Type (Target_Type)
8093 and then not Conversion_OK (N)
8095 if Is_Integer_Type (Operand_Type) then
8096 Expand_Convert_Integer_To_Fixed (N);
8099 pragma Assert (Is_Floating_Point_Type (Operand_Type));
8100 Expand_Convert_Float_To_Fixed (N);
8104 -- Case of float-to-integer conversions
8106 -- We also handle float-to-fixed conversions with Conversion_OK set
8107 -- since semantically the fixed-point target is treated as though it
8108 -- were an integer in such cases.
8110 elsif Is_Floating_Point_Type (Operand_Type)
8112 (Is_Integer_Type (Target_Type)
8114 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
8116 -- One more check here, gcc is still not able to do conversions of
8117 -- this type with proper overflow checking, and so gigi is doing an
8118 -- approximation of what is required by doing floating-point compares
8119 -- with the end-point. But that can lose precision in some cases, and
8120 -- give a wrong result. Converting the operand to Universal_Real is
8121 -- helpful, but still does not catch all cases with 64-bit integers
8122 -- on targets with only 64-bit floats
8124 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
8125 -- Can this code be removed ???
8127 if Do_Range_Check (Operand) then
8129 Make_Type_Conversion (Loc,
8131 New_Occurrence_Of (Universal_Real, Loc),
8133 Relocate_Node (Operand)));
8135 Set_Etype (Operand, Universal_Real);
8136 Enable_Range_Check (Operand);
8137 Set_Do_Range_Check (Expression (Operand), False);
8140 -- Case of array conversions
8142 -- Expansion of array conversions, add required length/range checks but
8143 -- only do this if there is no change of representation. For handling of
8144 -- this case, see Handle_Changed_Representation.
8146 elsif Is_Array_Type (Target_Type) then
8148 if Is_Constrained (Target_Type) then
8149 Apply_Length_Check (Operand, Target_Type);
8151 Apply_Range_Check (Operand, Target_Type);
8154 Handle_Changed_Representation;
8156 -- Case of conversions of discriminated types
8158 -- Add required discriminant checks if target is constrained. Again this
8159 -- change is skipped if we have a change of representation.
8161 elsif Has_Discriminants (Target_Type)
8162 and then Is_Constrained (Target_Type)
8164 Apply_Discriminant_Check (Operand, Target_Type);
8165 Handle_Changed_Representation;
8167 -- Case of all other record conversions. The only processing required
8168 -- is to check for a change of representation requiring the special
8169 -- assignment processing.
8171 elsif Is_Record_Type (Target_Type) then
8173 -- Ada 2005 (AI-216): Program_Error is raised when converting from
8174 -- a derived Unchecked_Union type to an unconstrained type that is
8175 -- not Unchecked_Union if the operand lacks inferable discriminants.
8177 if Is_Derived_Type (Operand_Type)
8178 and then Is_Unchecked_Union (Base_Type (Operand_Type))
8179 and then not Is_Constrained (Target_Type)
8180 and then not Is_Unchecked_Union (Base_Type (Target_Type))
8181 and then not Has_Inferable_Discriminants (Operand)
8183 -- To prevent Gigi from generating illegal code, we generate a
8184 -- Program_Error node, but we give it the target type of the
8188 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
8189 Reason => PE_Unchecked_Union_Restriction);
8192 Set_Etype (PE, Target_Type);
8197 Handle_Changed_Representation;
8200 -- Case of conversions of enumeration types
8202 elsif Is_Enumeration_Type (Target_Type) then
8204 -- Special processing is required if there is a change of
8205 -- representation (from enumeration representation clauses)
8207 if not Same_Representation (Target_Type, Operand_Type) then
8209 -- Convert: x(y) to x'val (ytyp'val (y))
8212 Make_Attribute_Reference (Loc,
8213 Prefix => New_Occurrence_Of (Target_Type, Loc),
8214 Attribute_Name => Name_Val,
8215 Expressions => New_List (
8216 Make_Attribute_Reference (Loc,
8217 Prefix => New_Occurrence_Of (Operand_Type, Loc),
8218 Attribute_Name => Name_Pos,
8219 Expressions => New_List (Operand)))));
8221 Analyze_And_Resolve (N, Target_Type);
8224 -- Case of conversions to floating-point
8226 elsif Is_Floating_Point_Type (Target_Type) then
8230 -- At this stage, either the conversion node has been transformed into
8231 -- some other equivalent expression, or left as a conversion that can
8232 -- be handled by Gigi. The conversions that Gigi can handle are the
8235 -- Conversions with no change of representation or type
8237 -- Numeric conversions involving integer, floating- and fixed-point
8238 -- values. Fixed-point values are allowed only if Conversion_OK is
8239 -- set, i.e. if the fixed-point values are to be treated as integers.
8241 -- No other conversions should be passed to Gigi
8243 -- Check: are these rules stated in sinfo??? if so, why restate here???
8245 -- The only remaining step is to generate a range check if we still have
8246 -- a type conversion at this stage and Do_Range_Check is set. For now we
8247 -- do this only for conversions of discrete types.
8249 if Nkind (N) = N_Type_Conversion
8250 and then Is_Discrete_Type (Etype (N))
8253 Expr : constant Node_Id := Expression (N);
8258 if Do_Range_Check (Expr)
8259 and then Is_Discrete_Type (Etype (Expr))
8261 Set_Do_Range_Check (Expr, False);
8263 -- Before we do a range check, we have to deal with treating a
8264 -- fixed-point operand as an integer. The way we do this is
8265 -- simply to do an unchecked conversion to an appropriate
8266 -- integer type large enough to hold the result.
8268 -- This code is not active yet, because we are only dealing
8269 -- with discrete types so far ???
8271 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
8272 and then Treat_Fixed_As_Integer (Expr)
8274 Ftyp := Base_Type (Etype (Expr));
8276 if Esize (Ftyp) >= Esize (Standard_Integer) then
8277 Ityp := Standard_Long_Long_Integer;
8279 Ityp := Standard_Integer;
8282 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
8285 -- Reset overflow flag, since the range check will include
8286 -- dealing with possible overflow, and generate the check If
8287 -- Address is either a source type or target type, suppress
8288 -- range check to avoid typing anomalies when it is a visible
8291 Set_Do_Overflow_Check (N, False);
8292 if not Is_Descendent_Of_Address (Etype (Expr))
8293 and then not Is_Descendent_Of_Address (Target_Type)
8295 Generate_Range_Check
8296 (Expr, Target_Type, CE_Range_Check_Failed);
8302 -- Final step, if the result is a type conversion involving Vax_Float
8303 -- types, then it is subject for further special processing.
8305 if Nkind (N) = N_Type_Conversion
8306 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
8308 Expand_Vax_Conversion (N);
8311 end Expand_N_Type_Conversion;
8313 -----------------------------------
8314 -- Expand_N_Unchecked_Expression --
8315 -----------------------------------
8317 -- Remove the unchecked expression node from the tree. It's job was simply
8318 -- to make sure that its constituent expression was handled with checks
8319 -- off, and now that that is done, we can remove it from the tree, and
8320 -- indeed must, since gigi does not expect to see these nodes.
8322 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
8323 Exp : constant Node_Id := Expression (N);
8326 Set_Assignment_OK (Exp, Assignment_OK (N) or Assignment_OK (Exp));
8328 end Expand_N_Unchecked_Expression;
8330 ----------------------------------------
8331 -- Expand_N_Unchecked_Type_Conversion --
8332 ----------------------------------------
8334 -- If this cannot be handled by Gigi and we haven't already made a
8335 -- temporary for it, do it now.
8337 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
8338 Target_Type : constant Entity_Id := Etype (N);
8339 Operand : constant Node_Id := Expression (N);
8340 Operand_Type : constant Entity_Id := Etype (Operand);
8343 -- If we have a conversion of a compile time known value to a target
8344 -- type and the value is in range of the target type, then we can simply
8345 -- replace the construct by an integer literal of the correct type. We
8346 -- only apply this to integer types being converted. Possibly it may
8347 -- apply in other cases, but it is too much trouble to worry about.
8349 -- Note that we do not do this transformation if the Kill_Range_Check
8350 -- flag is set, since then the value may be outside the expected range.
8351 -- This happens in the Normalize_Scalars case.
8353 -- We also skip this if either the target or operand type is biased
8354 -- because in this case, the unchecked conversion is supposed to
8355 -- preserve the bit pattern, not the integer value.
8357 if Is_Integer_Type (Target_Type)
8358 and then not Has_Biased_Representation (Target_Type)
8359 and then Is_Integer_Type (Operand_Type)
8360 and then not Has_Biased_Representation (Operand_Type)
8361 and then Compile_Time_Known_Value (Operand)
8362 and then not Kill_Range_Check (N)
8365 Val : constant Uint := Expr_Value (Operand);
8368 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
8370 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
8372 Val >= Expr_Value (Type_Low_Bound (Target_Type))
8374 Val <= Expr_Value (Type_High_Bound (Target_Type))
8376 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
8378 -- If Address is the target type, just set the type to avoid a
8379 -- spurious type error on the literal when Address is a visible
8382 if Is_Descendent_Of_Address (Target_Type) then
8383 Set_Etype (N, Target_Type);
8385 Analyze_And_Resolve (N, Target_Type);
8393 -- Nothing to do if conversion is safe
8395 if Safe_Unchecked_Type_Conversion (N) then
8399 -- Otherwise force evaluation unless Assignment_OK flag is set (this
8400 -- flag indicates ??? -- more comments needed here)
8402 if Assignment_OK (N) then
8405 Force_Evaluation (N);
8407 end Expand_N_Unchecked_Type_Conversion;
8409 ----------------------------
8410 -- Expand_Record_Equality --
8411 ----------------------------
8413 -- For non-variant records, Equality is expanded when needed into:
8415 -- and then Lhs.Discr1 = Rhs.Discr1
8417 -- and then Lhs.Discrn = Rhs.Discrn
8418 -- and then Lhs.Cmp1 = Rhs.Cmp1
8420 -- and then Lhs.Cmpn = Rhs.Cmpn
8422 -- The expression is folded by the back-end for adjacent fields. This
8423 -- function is called for tagged record in only one occasion: for imple-
8424 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
8425 -- otherwise the primitive "=" is used directly.
8427 function Expand_Record_Equality
8432 Bodies : List_Id) return Node_Id
8434 Loc : constant Source_Ptr := Sloc (Nod);
8439 First_Time : Boolean := True;
8441 function Suitable_Element (C : Entity_Id) return Entity_Id;
8442 -- Return the first field to compare beginning with C, skipping the
8443 -- inherited components.
8445 ----------------------
8446 -- Suitable_Element --
8447 ----------------------
8449 function Suitable_Element (C : Entity_Id) return Entity_Id is
8454 elsif Ekind (C) /= E_Discriminant
8455 and then Ekind (C) /= E_Component
8457 return Suitable_Element (Next_Entity (C));
8459 elsif Is_Tagged_Type (Typ)
8460 and then C /= Original_Record_Component (C)
8462 return Suitable_Element (Next_Entity (C));
8464 elsif Chars (C) = Name_uController
8465 or else Chars (C) = Name_uTag
8467 return Suitable_Element (Next_Entity (C));
8469 elsif Is_Interface (Etype (C)) then
8470 return Suitable_Element (Next_Entity (C));
8475 end Suitable_Element;
8477 -- Start of processing for Expand_Record_Equality
8480 -- Generates the following code: (assuming that Typ has one Discr and
8481 -- component C2 is also a record)
8484 -- and then Lhs.Discr1 = Rhs.Discr1
8485 -- and then Lhs.C1 = Rhs.C1
8486 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
8488 -- and then Lhs.Cmpn = Rhs.Cmpn
8490 Result := New_Reference_To (Standard_True, Loc);
8491 C := Suitable_Element (First_Entity (Typ));
8493 while Present (C) loop
8501 First_Time := False;
8505 New_Lhs := New_Copy_Tree (Lhs);
8506 New_Rhs := New_Copy_Tree (Rhs);
8510 Expand_Composite_Equality (Nod, Etype (C),
8512 Make_Selected_Component (Loc,
8514 Selector_Name => New_Reference_To (C, Loc)),
8516 Make_Selected_Component (Loc,
8518 Selector_Name => New_Reference_To (C, Loc)),
8521 -- If some (sub)component is an unchecked_union, the whole
8522 -- operation will raise program error.
8524 if Nkind (Check) = N_Raise_Program_Error then
8526 Set_Etype (Result, Standard_Boolean);
8531 Left_Opnd => Result,
8532 Right_Opnd => Check);
8536 C := Suitable_Element (Next_Entity (C));
8540 end Expand_Record_Equality;
8542 -------------------------------------
8543 -- Fixup_Universal_Fixed_Operation --
8544 -------------------------------------
8546 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
8547 Conv : constant Node_Id := Parent (N);
8550 -- We must have a type conversion immediately above us
8552 pragma Assert (Nkind (Conv) = N_Type_Conversion);
8554 -- Normally the type conversion gives our target type. The exception
8555 -- occurs in the case of the Round attribute, where the conversion
8556 -- will be to universal real, and our real type comes from the Round
8557 -- attribute (as well as an indication that we must round the result)
8559 if Nkind (Parent (Conv)) = N_Attribute_Reference
8560 and then Attribute_Name (Parent (Conv)) = Name_Round
8562 Set_Etype (N, Etype (Parent (Conv)));
8563 Set_Rounded_Result (N);
8565 -- Normal case where type comes from conversion above us
8568 Set_Etype (N, Etype (Conv));
8570 end Fixup_Universal_Fixed_Operation;
8572 ------------------------------
8573 -- Get_Allocator_Final_List --
8574 ------------------------------
8576 function Get_Allocator_Final_List
8579 PtrT : Entity_Id) return Entity_Id
8581 Loc : constant Source_Ptr := Sloc (N);
8583 Owner : Entity_Id := PtrT;
8584 -- The entity whose finalization list must be used to attach the
8585 -- allocated object.
8588 if Ekind (PtrT) = E_Anonymous_Access_Type then
8590 -- If the context is an access parameter, we need to create a
8591 -- non-anonymous access type in order to have a usable final list,
8592 -- because there is otherwise no pool to which the allocated object
8593 -- can belong. We create both the type and the finalization chain
8594 -- here, because freezing an internal type does not create such a
8595 -- chain. The Final_Chain that is thus created is shared by the
8596 -- access parameter. The access type is tested against the result
8597 -- type of the function to exclude allocators whose type is an
8598 -- anonymous access result type. We freeze the type at once to
8599 -- ensure that it is properly decorated for the back-end, even
8600 -- if the context and current scope is a loop.
8602 if Nkind (Associated_Node_For_Itype (PtrT))
8603 in N_Subprogram_Specification
8606 Etype (Defining_Unit_Name (Associated_Node_For_Itype (PtrT)))
8608 Owner := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
8610 Make_Full_Type_Declaration (Loc,
8611 Defining_Identifier => Owner,
8613 Make_Access_To_Object_Definition (Loc,
8614 Subtype_Indication =>
8615 New_Occurrence_Of (T, Loc))));
8617 Freeze_Before (N, Owner);
8618 Build_Final_List (N, Owner);
8619 Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner));
8621 -- Ada 2005 (AI-318-02): If the context is a return object
8622 -- declaration, then the anonymous return subtype is defined to have
8623 -- the same accessibility level as that of the function's result
8624 -- subtype, which means that we want the scope where the function is
8627 elsif Nkind (Associated_Node_For_Itype (PtrT)) = N_Object_Declaration
8628 and then Ekind (Scope (PtrT)) = E_Return_Statement
8630 Owner := Scope (Return_Applies_To (Scope (PtrT)));
8632 -- Case of an access discriminant, or (Ada 2005), of an anonymous
8633 -- access component or anonymous access function result: find the
8634 -- final list associated with the scope of the type. (In the
8635 -- anonymous access component kind, a list controller will have
8636 -- been allocated when freezing the record type, and PtrT has an
8637 -- Associated_Final_Chain attribute designating it.)
8639 elsif No (Associated_Final_Chain (PtrT)) then
8640 Owner := Scope (PtrT);
8644 return Find_Final_List (Owner);
8645 end Get_Allocator_Final_List;
8647 ---------------------------------
8648 -- Has_Inferable_Discriminants --
8649 ---------------------------------
8651 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
8653 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
8654 -- Determines whether the left-most prefix of a selected component is a
8655 -- formal parameter in a subprogram. Assumes N is a selected component.
8657 --------------------------------
8658 -- Prefix_Is_Formal_Parameter --
8659 --------------------------------
8661 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
8662 Sel_Comp : Node_Id := N;
8665 -- Move to the left-most prefix by climbing up the tree
8667 while Present (Parent (Sel_Comp))
8668 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
8670 Sel_Comp := Parent (Sel_Comp);
8673 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
8674 end Prefix_Is_Formal_Parameter;
8676 -- Start of processing for Has_Inferable_Discriminants
8679 -- For identifiers and indexed components, it is sufficient to have a
8680 -- constrained Unchecked_Union nominal subtype.
8682 if Nkind_In (N, N_Identifier, N_Indexed_Component) then
8683 return Is_Unchecked_Union (Base_Type (Etype (N)))
8685 Is_Constrained (Etype (N));
8687 -- For selected components, the subtype of the selector must be a
8688 -- constrained Unchecked_Union. If the component is subject to a
8689 -- per-object constraint, then the enclosing object must have inferable
8692 elsif Nkind (N) = N_Selected_Component then
8693 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
8695 -- A small hack. If we have a per-object constrained selected
8696 -- component of a formal parameter, return True since we do not
8697 -- know the actual parameter association yet.
8699 if Prefix_Is_Formal_Parameter (N) then
8703 -- Otherwise, check the enclosing object and the selector
8705 return Has_Inferable_Discriminants (Prefix (N))
8707 Has_Inferable_Discriminants (Selector_Name (N));
8710 -- The call to Has_Inferable_Discriminants will determine whether
8711 -- the selector has a constrained Unchecked_Union nominal type.
8713 return Has_Inferable_Discriminants (Selector_Name (N));
8715 -- A qualified expression has inferable discriminants if its subtype
8716 -- mark is a constrained Unchecked_Union subtype.
8718 elsif Nkind (N) = N_Qualified_Expression then
8719 return Is_Unchecked_Union (Subtype_Mark (N))
8721 Is_Constrained (Subtype_Mark (N));
8726 end Has_Inferable_Discriminants;
8728 -------------------------------
8729 -- Insert_Dereference_Action --
8730 -------------------------------
8732 procedure Insert_Dereference_Action (N : Node_Id) is
8733 Loc : constant Source_Ptr := Sloc (N);
8734 Typ : constant Entity_Id := Etype (N);
8735 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
8736 Pnod : constant Node_Id := Parent (N);
8738 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
8739 -- Return true if type of P is derived from Checked_Pool;
8741 -----------------------------
8742 -- Is_Checked_Storage_Pool --
8743 -----------------------------
8745 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
8754 while T /= Etype (T) loop
8755 if Is_RTE (T, RE_Checked_Pool) then
8763 end Is_Checked_Storage_Pool;
8765 -- Start of processing for Insert_Dereference_Action
8768 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
8770 if not (Is_Checked_Storage_Pool (Pool)
8771 and then Comes_From_Source (Original_Node (Pnod)))
8777 Make_Procedure_Call_Statement (Loc,
8778 Name => New_Reference_To (
8779 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
8781 Parameter_Associations => New_List (
8785 New_Reference_To (Pool, Loc),
8787 -- Storage_Address. We use the attribute Pool_Address, which uses
8788 -- the pointer itself to find the address of the object, and which
8789 -- handles unconstrained arrays properly by computing the address
8790 -- of the template. i.e. the correct address of the corresponding
8793 Make_Attribute_Reference (Loc,
8794 Prefix => Duplicate_Subexpr_Move_Checks (N),
8795 Attribute_Name => Name_Pool_Address),
8797 -- Size_In_Storage_Elements
8799 Make_Op_Divide (Loc,
8801 Make_Attribute_Reference (Loc,
8803 Make_Explicit_Dereference (Loc,
8804 Duplicate_Subexpr_Move_Checks (N)),
8805 Attribute_Name => Name_Size),
8807 Make_Integer_Literal (Loc, System_Storage_Unit)),
8811 Make_Attribute_Reference (Loc,
8813 Make_Explicit_Dereference (Loc,
8814 Duplicate_Subexpr_Move_Checks (N)),
8815 Attribute_Name => Name_Alignment))));
8818 when RE_Not_Available =>
8820 end Insert_Dereference_Action;
8822 ------------------------------
8823 -- Make_Array_Comparison_Op --
8824 ------------------------------
8826 -- This is a hand-coded expansion of the following generic function:
8829 -- type elem is (<>);
8830 -- type index is (<>);
8831 -- type a is array (index range <>) of elem;
8833 -- function Gnnn (X : a; Y: a) return boolean is
8834 -- J : index := Y'first;
8837 -- if X'length = 0 then
8840 -- elsif Y'length = 0 then
8844 -- for I in X'range loop
8845 -- if X (I) = Y (J) then
8846 -- if J = Y'last then
8849 -- J := index'succ (J);
8853 -- return X (I) > Y (J);
8857 -- return X'length > Y'length;
8861 -- Note that since we are essentially doing this expansion by hand, we
8862 -- do not need to generate an actual or formal generic part, just the
8863 -- instantiated function itself.
8865 function Make_Array_Comparison_Op
8867 Nod : Node_Id) return Node_Id
8869 Loc : constant Source_Ptr := Sloc (Nod);
8871 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
8872 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
8873 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
8874 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
8876 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
8878 Loop_Statement : Node_Id;
8879 Loop_Body : Node_Id;
8882 Final_Expr : Node_Id;
8883 Func_Body : Node_Id;
8884 Func_Name : Entity_Id;
8890 -- if J = Y'last then
8893 -- J := index'succ (J);
8897 Make_Implicit_If_Statement (Nod,
8900 Left_Opnd => New_Reference_To (J, Loc),
8902 Make_Attribute_Reference (Loc,
8903 Prefix => New_Reference_To (Y, Loc),
8904 Attribute_Name => Name_Last)),
8906 Then_Statements => New_List (
8907 Make_Exit_Statement (Loc)),
8911 Make_Assignment_Statement (Loc,
8912 Name => New_Reference_To (J, Loc),
8914 Make_Attribute_Reference (Loc,
8915 Prefix => New_Reference_To (Index, Loc),
8916 Attribute_Name => Name_Succ,
8917 Expressions => New_List (New_Reference_To (J, Loc))))));
8919 -- if X (I) = Y (J) then
8922 -- return X (I) > Y (J);
8926 Make_Implicit_If_Statement (Nod,
8930 Make_Indexed_Component (Loc,
8931 Prefix => New_Reference_To (X, Loc),
8932 Expressions => New_List (New_Reference_To (I, Loc))),
8935 Make_Indexed_Component (Loc,
8936 Prefix => New_Reference_To (Y, Loc),
8937 Expressions => New_List (New_Reference_To (J, Loc)))),
8939 Then_Statements => New_List (Inner_If),
8941 Else_Statements => New_List (
8942 Make_Simple_Return_Statement (Loc,
8946 Make_Indexed_Component (Loc,
8947 Prefix => New_Reference_To (X, Loc),
8948 Expressions => New_List (New_Reference_To (I, Loc))),
8951 Make_Indexed_Component (Loc,
8952 Prefix => New_Reference_To (Y, Loc),
8953 Expressions => New_List (
8954 New_Reference_To (J, Loc)))))));
8956 -- for I in X'range loop
8961 Make_Implicit_Loop_Statement (Nod,
8962 Identifier => Empty,
8965 Make_Iteration_Scheme (Loc,
8966 Loop_Parameter_Specification =>
8967 Make_Loop_Parameter_Specification (Loc,
8968 Defining_Identifier => I,
8969 Discrete_Subtype_Definition =>
8970 Make_Attribute_Reference (Loc,
8971 Prefix => New_Reference_To (X, Loc),
8972 Attribute_Name => Name_Range))),
8974 Statements => New_List (Loop_Body));
8976 -- if X'length = 0 then
8978 -- elsif Y'length = 0 then
8981 -- for ... loop ... end loop;
8982 -- return X'length > Y'length;
8986 Make_Attribute_Reference (Loc,
8987 Prefix => New_Reference_To (X, Loc),
8988 Attribute_Name => Name_Length);
8991 Make_Attribute_Reference (Loc,
8992 Prefix => New_Reference_To (Y, Loc),
8993 Attribute_Name => Name_Length);
8997 Left_Opnd => Length1,
8998 Right_Opnd => Length2);
9001 Make_Implicit_If_Statement (Nod,
9005 Make_Attribute_Reference (Loc,
9006 Prefix => New_Reference_To (X, Loc),
9007 Attribute_Name => Name_Length),
9009 Make_Integer_Literal (Loc, 0)),
9013 Make_Simple_Return_Statement (Loc,
9014 Expression => New_Reference_To (Standard_False, Loc))),
9016 Elsif_Parts => New_List (
9017 Make_Elsif_Part (Loc,
9021 Make_Attribute_Reference (Loc,
9022 Prefix => New_Reference_To (Y, Loc),
9023 Attribute_Name => Name_Length),
9025 Make_Integer_Literal (Loc, 0)),
9029 Make_Simple_Return_Statement (Loc,
9030 Expression => New_Reference_To (Standard_True, Loc))))),
9032 Else_Statements => New_List (
9034 Make_Simple_Return_Statement (Loc,
9035 Expression => Final_Expr)));
9039 Formals := New_List (
9040 Make_Parameter_Specification (Loc,
9041 Defining_Identifier => X,
9042 Parameter_Type => New_Reference_To (Typ, Loc)),
9044 Make_Parameter_Specification (Loc,
9045 Defining_Identifier => Y,
9046 Parameter_Type => New_Reference_To (Typ, Loc)));
9048 -- function Gnnn (...) return boolean is
9049 -- J : index := Y'first;
9054 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('G'));
9057 Make_Subprogram_Body (Loc,
9059 Make_Function_Specification (Loc,
9060 Defining_Unit_Name => Func_Name,
9061 Parameter_Specifications => Formals,
9062 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
9064 Declarations => New_List (
9065 Make_Object_Declaration (Loc,
9066 Defining_Identifier => J,
9067 Object_Definition => New_Reference_To (Index, Loc),
9069 Make_Attribute_Reference (Loc,
9070 Prefix => New_Reference_To (Y, Loc),
9071 Attribute_Name => Name_First))),
9073 Handled_Statement_Sequence =>
9074 Make_Handled_Sequence_Of_Statements (Loc,
9075 Statements => New_List (If_Stat)));
9078 end Make_Array_Comparison_Op;
9080 ---------------------------
9081 -- Make_Boolean_Array_Op --
9082 ---------------------------
9084 -- For logical operations on boolean arrays, expand in line the following,
9085 -- replacing 'and' with 'or' or 'xor' where needed:
9087 -- function Annn (A : typ; B: typ) return typ is
9090 -- for J in A'range loop
9091 -- C (J) := A (J) op B (J);
9096 -- Here typ is the boolean array type
9098 function Make_Boolean_Array_Op
9100 N : Node_Id) return Node_Id
9102 Loc : constant Source_Ptr := Sloc (N);
9104 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
9105 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
9106 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
9107 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
9115 Func_Name : Entity_Id;
9116 Func_Body : Node_Id;
9117 Loop_Statement : Node_Id;
9121 Make_Indexed_Component (Loc,
9122 Prefix => New_Reference_To (A, Loc),
9123 Expressions => New_List (New_Reference_To (J, Loc)));
9126 Make_Indexed_Component (Loc,
9127 Prefix => New_Reference_To (B, Loc),
9128 Expressions => New_List (New_Reference_To (J, Loc)));
9131 Make_Indexed_Component (Loc,
9132 Prefix => New_Reference_To (C, Loc),
9133 Expressions => New_List (New_Reference_To (J, Loc)));
9135 if Nkind (N) = N_Op_And then
9141 elsif Nkind (N) = N_Op_Or then
9155 Make_Implicit_Loop_Statement (N,
9156 Identifier => Empty,
9159 Make_Iteration_Scheme (Loc,
9160 Loop_Parameter_Specification =>
9161 Make_Loop_Parameter_Specification (Loc,
9162 Defining_Identifier => J,
9163 Discrete_Subtype_Definition =>
9164 Make_Attribute_Reference (Loc,
9165 Prefix => New_Reference_To (A, Loc),
9166 Attribute_Name => Name_Range))),
9168 Statements => New_List (
9169 Make_Assignment_Statement (Loc,
9171 Expression => Op)));
9173 Formals := New_List (
9174 Make_Parameter_Specification (Loc,
9175 Defining_Identifier => A,
9176 Parameter_Type => New_Reference_To (Typ, Loc)),
9178 Make_Parameter_Specification (Loc,
9179 Defining_Identifier => B,
9180 Parameter_Type => New_Reference_To (Typ, Loc)));
9183 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
9184 Set_Is_Inlined (Func_Name);
9187 Make_Subprogram_Body (Loc,
9189 Make_Function_Specification (Loc,
9190 Defining_Unit_Name => Func_Name,
9191 Parameter_Specifications => Formals,
9192 Result_Definition => New_Reference_To (Typ, Loc)),
9194 Declarations => New_List (
9195 Make_Object_Declaration (Loc,
9196 Defining_Identifier => C,
9197 Object_Definition => New_Reference_To (Typ, Loc))),
9199 Handled_Statement_Sequence =>
9200 Make_Handled_Sequence_Of_Statements (Loc,
9201 Statements => New_List (
9203 Make_Simple_Return_Statement (Loc,
9204 Expression => New_Reference_To (C, Loc)))));
9207 end Make_Boolean_Array_Op;
9209 ------------------------
9210 -- Rewrite_Comparison --
9211 ------------------------
9213 procedure Rewrite_Comparison (N : Node_Id) is
9214 Warning_Generated : Boolean := False;
9215 -- Set to True if first pass with Assume_Valid generates a warning in
9216 -- which case we skip the second pass to avoid warning overloaded.
9219 -- Set to Standard_True or Standard_False
9222 if Nkind (N) = N_Type_Conversion then
9223 Rewrite_Comparison (Expression (N));
9226 elsif Nkind (N) not in N_Op_Compare then
9230 -- Now start looking at the comparison in detail. We potentially go
9231 -- through this loop twice. The first time, Assume_Valid is set False
9232 -- in the call to Compile_Time_Compare. If this call results in a
9233 -- clear result of always True or Always False, that's decisive and
9234 -- we are done. Otherwise we repeat the processing with Assume_Valid
9235 -- set to True to generate additional warnings. We can stil that step
9236 -- if Constant_Condition_Warnings is False.
9238 for AV in False .. True loop
9240 Typ : constant Entity_Id := Etype (N);
9241 Op1 : constant Node_Id := Left_Opnd (N);
9242 Op2 : constant Node_Id := Right_Opnd (N);
9244 Res : constant Compare_Result :=
9245 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
9246 -- Res indicates if compare outcome can be compile time determined
9248 True_Result : Boolean;
9249 False_Result : Boolean;
9252 case N_Op_Compare (Nkind (N)) is
9254 True_Result := Res = EQ;
9255 False_Result := Res = LT or else Res = GT or else Res = NE;
9258 True_Result := Res in Compare_GE;
9259 False_Result := Res = LT;
9262 and then Constant_Condition_Warnings
9263 and then Comes_From_Source (Original_Node (N))
9264 and then Nkind (Original_Node (N)) = N_Op_Ge
9265 and then not In_Instance
9266 and then Is_Integer_Type (Etype (Left_Opnd (N)))
9267 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
9270 ("can never be greater than, could replace by ""'=""?", N);
9271 Warning_Generated := True;
9275 True_Result := Res = GT;
9276 False_Result := Res in Compare_LE;
9279 True_Result := Res = LT;
9280 False_Result := Res in Compare_GE;
9283 True_Result := Res in Compare_LE;
9284 False_Result := Res = GT;
9287 and then Constant_Condition_Warnings
9288 and then Comes_From_Source (Original_Node (N))
9289 and then Nkind (Original_Node (N)) = N_Op_Le
9290 and then not In_Instance
9291 and then Is_Integer_Type (Etype (Left_Opnd (N)))
9292 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
9295 ("can never be less than, could replace by ""'=""?", N);
9296 Warning_Generated := True;
9300 True_Result := Res = NE or else Res = GT or else Res = LT;
9301 False_Result := Res = EQ;
9304 -- If this is the first iteration, then we actually convert the
9305 -- comparison into True or False, if the result is certain.
9308 if True_Result or False_Result then
9310 Result := Standard_True;
9312 Result := Standard_False;
9317 New_Occurrence_Of (Result, Sloc (N))));
9318 Analyze_And_Resolve (N, Typ);
9319 Warn_On_Known_Condition (N);
9323 -- If this is the second iteration (AV = True), and the original
9324 -- node comes from source and we are not in an instance, then
9325 -- give a warning if we know result would be True or False. Note
9326 -- we know Constant_Condition_Warnings is set if we get here.
9328 elsif Comes_From_Source (Original_Node (N))
9329 and then not In_Instance
9333 ("condition can only be False if invalid values present?",
9335 elsif False_Result then
9337 ("condition can only be True if invalid values present?",
9343 -- Skip second iteration if not warning on constant conditions or
9344 -- if the first iteration already generated a warning of some kind
9345 -- or if we are in any case assuming all values are valid (so that
9346 -- the first iteration took care of the valid case).
9348 exit when not Constant_Condition_Warnings;
9349 exit when Warning_Generated;
9350 exit when Assume_No_Invalid_Values;
9352 end Rewrite_Comparison;
9354 ----------------------------
9355 -- Safe_In_Place_Array_Op --
9356 ----------------------------
9358 function Safe_In_Place_Array_Op
9361 Op2 : Node_Id) return Boolean
9365 function Is_Safe_Operand (Op : Node_Id) return Boolean;
9366 -- Operand is safe if it cannot overlap part of the target of the
9367 -- operation. If the operand and the target are identical, the operand
9368 -- is safe. The operand can be empty in the case of negation.
9370 function Is_Unaliased (N : Node_Id) return Boolean;
9371 -- Check that N is a stand-alone entity
9377 function Is_Unaliased (N : Node_Id) return Boolean is
9381 and then No (Address_Clause (Entity (N)))
9382 and then No (Renamed_Object (Entity (N)));
9385 ---------------------
9386 -- Is_Safe_Operand --
9387 ---------------------
9389 function Is_Safe_Operand (Op : Node_Id) return Boolean is
9394 elsif Is_Entity_Name (Op) then
9395 return Is_Unaliased (Op);
9397 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
9398 return Is_Unaliased (Prefix (Op));
9400 elsif Nkind (Op) = N_Slice then
9402 Is_Unaliased (Prefix (Op))
9403 and then Entity (Prefix (Op)) /= Target;
9405 elsif Nkind (Op) = N_Op_Not then
9406 return Is_Safe_Operand (Right_Opnd (Op));
9411 end Is_Safe_Operand;
9413 -- Start of processing for Is_Safe_In_Place_Array_Op
9416 -- Skip this processing if the component size is different from system
9417 -- storage unit (since at least for NOT this would cause problems).
9419 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
9422 -- Cannot do in place stuff on VM_Target since cannot pass addresses
9424 elsif VM_Target /= No_VM then
9427 -- Cannot do in place stuff if non-standard Boolean representation
9429 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
9432 elsif not Is_Unaliased (Lhs) then
9435 Target := Entity (Lhs);
9438 Is_Safe_Operand (Op1)
9439 and then Is_Safe_Operand (Op2);
9441 end Safe_In_Place_Array_Op;
9443 -----------------------
9444 -- Tagged_Membership --
9445 -----------------------
9447 -- There are two different cases to consider depending on whether the right
9448 -- operand is a class-wide type or not. If not we just compare the actual
9449 -- tag of the left expr to the target type tag:
9451 -- Left_Expr.Tag = Right_Type'Tag;
9453 -- If it is a class-wide type we use the RT function CW_Membership which is
9454 -- usually implemented by looking in the ancestor tables contained in the
9455 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
9457 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
9458 -- function IW_Membership which is usually implemented by looking in the
9459 -- table of abstract interface types plus the ancestor table contained in
9460 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
9462 function Tagged_Membership (N : Node_Id) return Node_Id is
9463 Left : constant Node_Id := Left_Opnd (N);
9464 Right : constant Node_Id := Right_Opnd (N);
9465 Loc : constant Source_Ptr := Sloc (N);
9467 Left_Type : Entity_Id;
9468 Right_Type : Entity_Id;
9472 Left_Type := Etype (Left);
9473 Right_Type := Etype (Right);
9475 if Is_Class_Wide_Type (Left_Type) then
9476 Left_Type := Root_Type (Left_Type);
9480 Make_Selected_Component (Loc,
9481 Prefix => Relocate_Node (Left),
9483 New_Reference_To (First_Tag_Component (Left_Type), Loc));
9485 if Is_Class_Wide_Type (Right_Type) then
9487 -- No need to issue a run-time check if we statically know that the
9488 -- result of this membership test is always true. For example,
9489 -- considering the following declarations:
9491 -- type Iface is interface;
9492 -- type T is tagged null record;
9493 -- type DT is new T and Iface with null record;
9498 -- These membership tests are always true:
9502 -- Obj2 in Iface'Class;
9504 -- We do not need to handle cases where the membership is illegal.
9507 -- Obj1 in DT'Class; -- Compile time error
9508 -- Obj1 in Iface'Class; -- Compile time error
9510 if not Is_Class_Wide_Type (Left_Type)
9511 and then (Is_Ancestor (Etype (Right_Type), Left_Type)
9512 or else (Is_Interface (Etype (Right_Type))
9513 and then Interface_Present_In_Ancestor
9515 Iface => Etype (Right_Type))))
9517 return New_Reference_To (Standard_True, Loc);
9520 -- Ada 2005 (AI-251): Class-wide applied to interfaces
9522 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
9524 -- Support to: "Iface_CW_Typ in Typ'Class"
9526 or else Is_Interface (Left_Type)
9528 -- Issue error if IW_Membership operation not available in a
9529 -- configurable run time setting.
9531 if not RTE_Available (RE_IW_Membership) then
9533 ("dynamic membership test on interface types", N);
9538 Make_Function_Call (Loc,
9539 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
9540 Parameter_Associations => New_List (
9541 Make_Attribute_Reference (Loc,
9543 Attribute_Name => Name_Address),
9546 (Access_Disp_Table (Root_Type (Right_Type)))),
9549 -- Ada 95: Normal case
9553 Build_CW_Membership (Loc,
9554 Obj_Tag_Node => Obj_Tag,
9558 (Access_Disp_Table (Root_Type (Right_Type)))),
9562 -- Right_Type is not a class-wide type
9565 -- No need to check the tag of the object if Right_Typ is abstract
9567 if Is_Abstract_Type (Right_Type) then
9568 return New_Reference_To (Standard_False, Loc);
9573 Left_Opnd => Obj_Tag,
9576 (Node (First_Elmt (Access_Disp_Table (Right_Type))), Loc));
9579 end Tagged_Membership;
9581 ------------------------------
9582 -- Unary_Op_Validity_Checks --
9583 ------------------------------
9585 procedure Unary_Op_Validity_Checks (N : Node_Id) is
9587 if Validity_Checks_On and Validity_Check_Operands then
9588 Ensure_Valid (Right_Opnd (N));
9590 end Unary_Op_Validity_Checks;