1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Aggr; use Exp_Aggr;
33 with Exp_Atag; use Exp_Atag;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Fixd; use Exp_Fixd;
40 with Exp_Pakd; use Exp_Pakd;
41 with Exp_Tss; use Exp_Tss;
42 with Exp_Util; use Exp_Util;
43 with Exp_VFpt; use Exp_VFpt;
44 with Freeze; use Freeze;
45 with Inline; use Inline;
46 with Namet; use Namet;
47 with Nlists; use Nlists;
48 with Nmake; use Nmake;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Res; use Sem_Res;
61 with Sem_Type; use Sem_Type;
62 with Sem_Util; use Sem_Util;
63 with Sem_Warn; use Sem_Warn;
64 with Sinfo; use Sinfo;
65 with Snames; use Snames;
66 with Stand; use Stand;
67 with Targparm; use Targparm;
68 with Tbuild; use Tbuild;
69 with Ttypes; use Ttypes;
70 with Uintp; use Uintp;
71 with Urealp; use Urealp;
72 with Validsw; use Validsw;
74 package body Exp_Ch4 is
76 -----------------------
77 -- Local Subprograms --
78 -----------------------
80 procedure Binary_Op_Validity_Checks (N : Node_Id);
81 pragma Inline (Binary_Op_Validity_Checks);
82 -- Performs validity checks for a binary operator
84 procedure Build_Boolean_Array_Proc_Call
88 -- If a boolean array assignment can be done in place, build call to
89 -- corresponding library procedure.
91 procedure Displace_Allocator_Pointer (N : Node_Id);
92 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
93 -- Expand_Allocator_Expression. Allocating class-wide interface objects
94 -- this routine displaces the pointer to the allocated object to reference
95 -- the component referencing the corresponding secondary dispatch table.
97 procedure Expand_Allocator_Expression (N : Node_Id);
98 -- Subsidiary to Expand_N_Allocator, for the case when the expression
99 -- is a qualified expression or an aggregate.
101 procedure Expand_Array_Comparison (N : Node_Id);
102 -- This routine handles expansion of the comparison operators (N_Op_Lt,
103 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
104 -- code for these operators is similar, differing only in the details of
105 -- the actual comparison call that is made. Special processing (call a
108 function Expand_Array_Equality
113 Typ : Entity_Id) return Node_Id;
114 -- Expand an array equality into a call to a function implementing this
115 -- equality, and a call to it. Loc is the location for the generated nodes.
116 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
117 -- on which to attach bodies of local functions that are created in the
118 -- process. It is the responsibility of the caller to insert those bodies
119 -- at the right place. Nod provides the Sloc value for the generated code.
120 -- Normally the types used for the generated equality routine are taken
121 -- from Lhs and Rhs. However, in some situations of generated code, the
122 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
123 -- the type to be used for the formal parameters.
125 procedure Expand_Boolean_Operator (N : Node_Id);
126 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
127 -- case of array type arguments.
129 function Expand_Composite_Equality
134 Bodies : List_Id) return Node_Id;
135 -- Local recursive function used to expand equality for nested composite
136 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
137 -- to attach bodies of local functions that are created in the process.
138 -- This is the responsibility of the caller to insert those bodies at the
139 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
140 -- are the left and right sides for the comparison, and Typ is the type of
141 -- the arrays to compare.
143 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
144 -- Routine to expand concatenation of a sequence of two or more operands
145 -- (in the list Operands) and replace node Cnode with the result of the
146 -- concatenation. The operands can be of any appropriate type, and can
147 -- include both arrays and singleton elements.
149 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
150 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
151 -- fixed. We do not have such a type at runtime, so the purpose of this
152 -- routine is to find the real type by looking up the tree. We also
153 -- determine if the operation must be rounded.
155 function Get_Allocator_Final_List
158 PtrT : Entity_Id) return Entity_Id;
159 -- If the designated type is controlled, build final_list expression for
160 -- created object. If context is an access parameter, create a local access
161 -- type to have a usable finalization list.
163 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
164 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
165 -- discriminants if it has a constrained nominal type, unless the object
166 -- is a component of an enclosing Unchecked_Union object that is subject
167 -- to a per-object constraint and the enclosing object lacks inferable
170 -- An expression of an Unchecked_Union type has inferable discriminants
171 -- if it is either a name of an object with inferable discriminants or a
172 -- qualified expression whose subtype mark denotes a constrained subtype.
174 procedure Insert_Dereference_Action (N : Node_Id);
175 -- N is an expression whose type is an access. When the type of the
176 -- associated storage pool is derived from Checked_Pool, generate a
177 -- call to the 'Dereference' primitive operation.
179 function Make_Array_Comparison_Op
181 Nod : Node_Id) return Node_Id;
182 -- Comparisons between arrays are expanded in line. This function produces
183 -- the body of the implementation of (a > b), where a and b are one-
184 -- dimensional arrays of some discrete type. The original node is then
185 -- expanded into the appropriate call to this function. Nod provides the
186 -- Sloc value for the generated code.
188 function Make_Boolean_Array_Op
190 N : Node_Id) return Node_Id;
191 -- Boolean operations on boolean arrays are expanded in line. This function
192 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
193 -- b). It is used only the normal case and not the packed case. The type
194 -- involved, Typ, is the Boolean array type, and the logical operations in
195 -- the body are simple boolean operations. Note that Typ is always a
196 -- constrained type (the caller has ensured this by using
197 -- Convert_To_Actual_Subtype if necessary).
199 procedure Rewrite_Comparison (N : Node_Id);
200 -- If N is the node for a comparison whose outcome can be determined at
201 -- compile time, then the node N can be rewritten with True or False. If
202 -- the outcome cannot be determined at compile time, the call has no
203 -- effect. If N is a type conversion, then this processing is applied to
204 -- its expression. If N is neither comparison nor a type conversion, the
205 -- call has no effect.
207 function Tagged_Membership (N : Node_Id) return Node_Id;
208 -- Construct the expression corresponding to the tagged membership test.
209 -- Deals with a second operand being (or not) a class-wide type.
211 function Safe_In_Place_Array_Op
214 Op2 : Node_Id) return Boolean;
215 -- In the context of an assignment, where the right-hand side is a boolean
216 -- operation on arrays, check whether operation can be performed in place.
218 procedure Unary_Op_Validity_Checks (N : Node_Id);
219 pragma Inline (Unary_Op_Validity_Checks);
220 -- Performs validity checks for a unary operator
222 -------------------------------
223 -- Binary_Op_Validity_Checks --
224 -------------------------------
226 procedure Binary_Op_Validity_Checks (N : Node_Id) is
228 if Validity_Checks_On and Validity_Check_Operands then
229 Ensure_Valid (Left_Opnd (N));
230 Ensure_Valid (Right_Opnd (N));
232 end Binary_Op_Validity_Checks;
234 ------------------------------------
235 -- Build_Boolean_Array_Proc_Call --
236 ------------------------------------
238 procedure Build_Boolean_Array_Proc_Call
243 Loc : constant Source_Ptr := Sloc (N);
244 Kind : constant Node_Kind := Nkind (Expression (N));
245 Target : constant Node_Id :=
246 Make_Attribute_Reference (Loc,
248 Attribute_Name => Name_Address);
250 Arg1 : constant Node_Id := Op1;
251 Arg2 : Node_Id := Op2;
253 Proc_Name : Entity_Id;
256 if Kind = N_Op_Not then
257 if Nkind (Op1) in N_Binary_Op then
259 -- Use negated version of the binary operators
261 if Nkind (Op1) = N_Op_And then
262 Proc_Name := RTE (RE_Vector_Nand);
264 elsif Nkind (Op1) = N_Op_Or then
265 Proc_Name := RTE (RE_Vector_Nor);
267 else pragma Assert (Nkind (Op1) = N_Op_Xor);
268 Proc_Name := RTE (RE_Vector_Xor);
272 Make_Procedure_Call_Statement (Loc,
273 Name => New_Occurrence_Of (Proc_Name, Loc),
275 Parameter_Associations => New_List (
277 Make_Attribute_Reference (Loc,
278 Prefix => Left_Opnd (Op1),
279 Attribute_Name => Name_Address),
281 Make_Attribute_Reference (Loc,
282 Prefix => Right_Opnd (Op1),
283 Attribute_Name => Name_Address),
285 Make_Attribute_Reference (Loc,
286 Prefix => Left_Opnd (Op1),
287 Attribute_Name => Name_Length)));
290 Proc_Name := RTE (RE_Vector_Not);
293 Make_Procedure_Call_Statement (Loc,
294 Name => New_Occurrence_Of (Proc_Name, Loc),
295 Parameter_Associations => New_List (
298 Make_Attribute_Reference (Loc,
300 Attribute_Name => Name_Address),
302 Make_Attribute_Reference (Loc,
304 Attribute_Name => Name_Length)));
308 -- We use the following equivalences:
310 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
311 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
312 -- (not X) xor (not Y) = X xor Y
313 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
315 if Nkind (Op1) = N_Op_Not then
316 if Kind = N_Op_And then
317 Proc_Name := RTE (RE_Vector_Nor);
319 elsif Kind = N_Op_Or then
320 Proc_Name := RTE (RE_Vector_Nand);
323 Proc_Name := RTE (RE_Vector_Xor);
327 if Kind = N_Op_And then
328 Proc_Name := RTE (RE_Vector_And);
330 elsif Kind = N_Op_Or then
331 Proc_Name := RTE (RE_Vector_Or);
333 elsif Nkind (Op2) = N_Op_Not then
334 Proc_Name := RTE (RE_Vector_Nxor);
335 Arg2 := Right_Opnd (Op2);
338 Proc_Name := RTE (RE_Vector_Xor);
343 Make_Procedure_Call_Statement (Loc,
344 Name => New_Occurrence_Of (Proc_Name, Loc),
345 Parameter_Associations => New_List (
347 Make_Attribute_Reference (Loc,
349 Attribute_Name => Name_Address),
350 Make_Attribute_Reference (Loc,
352 Attribute_Name => Name_Address),
353 Make_Attribute_Reference (Loc,
355 Attribute_Name => Name_Length)));
358 Rewrite (N, Call_Node);
362 when RE_Not_Available =>
364 end Build_Boolean_Array_Proc_Call;
366 --------------------------------
367 -- Displace_Allocator_Pointer --
368 --------------------------------
370 procedure Displace_Allocator_Pointer (N : Node_Id) is
371 Loc : constant Source_Ptr := Sloc (N);
372 Orig_Node : constant Node_Id := Original_Node (N);
378 -- Do nothing in case of VM targets: the virtual machine will handle
379 -- interfaces directly.
381 if not Tagged_Type_Expansion then
385 pragma Assert (Nkind (N) = N_Identifier
386 and then Nkind (Orig_Node) = N_Allocator);
388 PtrT := Etype (Orig_Node);
389 Dtyp := Available_View (Designated_Type (PtrT));
390 Etyp := Etype (Expression (Orig_Node));
392 if Is_Class_Wide_Type (Dtyp)
393 and then Is_Interface (Dtyp)
395 -- If the type of the allocator expression is not an interface type
396 -- we can generate code to reference the record component containing
397 -- the pointer to the secondary dispatch table.
399 if not Is_Interface (Etyp) then
401 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
404 -- 1) Get access to the allocated object
407 Make_Explicit_Dereference (Loc,
412 -- 2) Add the conversion to displace the pointer to reference
413 -- the secondary dispatch table.
415 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
416 Analyze_And_Resolve (N, Dtyp);
418 -- 3) The 'access to the secondary dispatch table will be used
419 -- as the value returned by the allocator.
422 Make_Attribute_Reference (Loc,
423 Prefix => Relocate_Node (N),
424 Attribute_Name => Name_Access));
425 Set_Etype (N, Saved_Typ);
429 -- If the type of the allocator expression is an interface type we
430 -- generate a run-time call to displace "this" to reference the
431 -- component containing the pointer to the secondary dispatch table
432 -- or else raise Constraint_Error if the actual object does not
433 -- implement the target interface. This case corresponds with the
434 -- following example:
436 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
438 -- return new Iface_2'Class'(Obj);
443 Unchecked_Convert_To (PtrT,
444 Make_Function_Call (Loc,
445 Name => New_Reference_To (RTE (RE_Displace), Loc),
446 Parameter_Associations => New_List (
447 Unchecked_Convert_To (RTE (RE_Address),
453 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
455 Analyze_And_Resolve (N, PtrT);
458 end Displace_Allocator_Pointer;
460 ---------------------------------
461 -- Expand_Allocator_Expression --
462 ---------------------------------
464 procedure Expand_Allocator_Expression (N : Node_Id) is
465 Loc : constant Source_Ptr := Sloc (N);
466 Exp : constant Node_Id := Expression (Expression (N));
467 PtrT : constant Entity_Id := Etype (N);
468 DesigT : constant Entity_Id := Designated_Type (PtrT);
470 procedure Apply_Accessibility_Check
472 Built_In_Place : Boolean := False);
473 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
474 -- type, generate an accessibility check to verify that the level of the
475 -- type of the created object is not deeper than the level of the access
476 -- type. If the type of the qualified expression is class- wide, then
477 -- always generate the check (except in the case where it is known to be
478 -- unnecessary, see comment below). Otherwise, only generate the check
479 -- if the level of the qualified expression type is statically deeper
480 -- than the access type.
482 -- Although the static accessibility will generally have been performed
483 -- as a legality check, it won't have been done in cases where the
484 -- allocator appears in generic body, so a run-time check is needed in
485 -- general. One special case is when the access type is declared in the
486 -- same scope as the class-wide allocator, in which case the check can
487 -- never fail, so it need not be generated.
489 -- As an open issue, there seem to be cases where the static level
490 -- associated with the class-wide object's underlying type is not
491 -- sufficient to perform the proper accessibility check, such as for
492 -- allocators in nested subprograms or accept statements initialized by
493 -- class-wide formals when the actual originates outside at a deeper
494 -- static level. The nested subprogram case might require passing
495 -- accessibility levels along with class-wide parameters, and the task
496 -- case seems to be an actual gap in the language rules that needs to
497 -- be fixed by the ARG. ???
499 -------------------------------
500 -- Apply_Accessibility_Check --
501 -------------------------------
503 procedure Apply_Accessibility_Check
505 Built_In_Place : Boolean := False)
510 -- Note: we skip the accessibility check for the VM case, since
511 -- there does not seem to be any practical way of implementing it.
513 if Ada_Version >= Ada_05
514 and then Tagged_Type_Expansion
515 and then Is_Class_Wide_Type (DesigT)
516 and then not Scope_Suppress (Accessibility_Check)
518 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
520 (Is_Class_Wide_Type (Etype (Exp))
521 and then Scope (PtrT) /= Current_Scope))
523 -- If the allocator was built in place Ref is already a reference
524 -- to the access object initialized to the result of the allocator
525 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise
526 -- it is the entity associated with the object containing the
527 -- address of the allocated object.
529 if Built_In_Place then
530 Ref_Node := New_Copy (Ref);
532 Ref_Node := New_Reference_To (Ref, Loc);
536 Make_Raise_Program_Error (Loc,
540 Build_Get_Access_Level (Loc,
541 Make_Attribute_Reference (Loc,
543 Attribute_Name => Name_Tag)),
545 Make_Integer_Literal (Loc,
546 Type_Access_Level (PtrT))),
547 Reason => PE_Accessibility_Check_Failed));
549 end Apply_Accessibility_Check;
553 Indic : constant Node_Id := Subtype_Mark (Expression (N));
554 T : constant Entity_Id := Entity (Indic);
559 TagT : Entity_Id := Empty;
560 -- Type used as source for tag assignment
562 TagR : Node_Id := Empty;
563 -- Target reference for tag assignment
565 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
567 Tag_Assign : Node_Id;
570 -- Start of processing for Expand_Allocator_Expression
573 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
575 -- 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 Tagged_Type_Expansion
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 not Tagged_Type_Expansion 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)));
981 -- type A is access T1;
982 -- X : A := new T2'(...);
983 -- T1 and T2 can be different subtypes, and we might need to check
984 -- both constraints. First check against the type of the qualified
987 Apply_Constraint_Check (Exp, T, No_Sliding => True);
989 -- A check is also needed in cases where the designated subtype is
990 -- constrained and differs from the subtype given in the qualified
991 -- expression. Note that the check on the qualified expression does
992 -- not allow sliding, but this check does (a relaxation from Ada 83).
994 if Is_Constrained (DesigT)
995 and then not Subtypes_Statically_Match (T, DesigT)
997 Apply_Constraint_Check
998 (Exp, DesigT, No_Sliding => False);
1001 -- For an access to unconstrained packed array, GIGI needs to see an
1002 -- expression with a constrained subtype in order to compute the
1003 -- proper size for the allocator.
1005 if Is_Array_Type (T)
1006 and then not Is_Constrained (T)
1007 and then Is_Packed (T)
1010 ConstrT : constant Entity_Id :=
1011 Make_Defining_Identifier (Loc,
1012 Chars => New_Internal_Name ('A'));
1013 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1016 Make_Subtype_Declaration (Loc,
1017 Defining_Identifier => ConstrT,
1018 Subtype_Indication =>
1019 Make_Subtype_From_Expr (Exp, T)));
1020 Freeze_Itype (ConstrT, Exp);
1021 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1025 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1026 -- to a build-in-place function, then access to the allocated object
1027 -- must be passed to the function. Currently we limit such functions
1028 -- to those with constrained limited result subtypes, but eventually
1029 -- we plan to expand the allowed forms of functions that are treated
1030 -- as build-in-place.
1032 if Ada_Version >= Ada_05
1033 and then Is_Build_In_Place_Function_Call (Exp)
1035 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1040 when RE_Not_Available =>
1042 end Expand_Allocator_Expression;
1044 -----------------------------
1045 -- Expand_Array_Comparison --
1046 -----------------------------
1048 -- Expansion is only required in the case of array types. For the unpacked
1049 -- case, an appropriate runtime routine is called. For packed cases, and
1050 -- also in some other cases where a runtime routine cannot be called, the
1051 -- form of the expansion is:
1053 -- [body for greater_nn; boolean_expression]
1055 -- The body is built by Make_Array_Comparison_Op, and the form of the
1056 -- Boolean expression depends on the operator involved.
1058 procedure Expand_Array_Comparison (N : Node_Id) is
1059 Loc : constant Source_Ptr := Sloc (N);
1060 Op1 : Node_Id := Left_Opnd (N);
1061 Op2 : Node_Id := Right_Opnd (N);
1062 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1063 Ctyp : constant Entity_Id := Component_Type (Typ1);
1066 Func_Body : Node_Id;
1067 Func_Name : Entity_Id;
1071 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1072 -- True for byte addressable target
1074 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1075 -- Returns True if the length of the given operand is known to be less
1076 -- than 4. Returns False if this length is known to be four or greater
1077 -- or is not known at compile time.
1079 ------------------------
1080 -- Length_Less_Than_4 --
1081 ------------------------
1083 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1084 Otyp : constant Entity_Id := Etype (Opnd);
1087 if Ekind (Otyp) = E_String_Literal_Subtype then
1088 return String_Literal_Length (Otyp) < 4;
1092 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1093 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1094 Hi : constant Node_Id := Type_High_Bound (Ityp);
1099 if Compile_Time_Known_Value (Lo) then
1100 Lov := Expr_Value (Lo);
1105 if Compile_Time_Known_Value (Hi) then
1106 Hiv := Expr_Value (Hi);
1111 return Hiv < Lov + 3;
1114 end Length_Less_Than_4;
1116 -- Start of processing for Expand_Array_Comparison
1119 -- Deal first with unpacked case, where we can call a runtime routine
1120 -- except that we avoid this for targets for which are not addressable
1121 -- by bytes, and for the JVM/CIL, since they do not support direct
1122 -- addressing of array components.
1124 if not Is_Bit_Packed_Array (Typ1)
1125 and then Byte_Addressable
1126 and then VM_Target = No_VM
1128 -- The call we generate is:
1130 -- Compare_Array_xn[_Unaligned]
1131 -- (left'address, right'address, left'length, right'length) <op> 0
1133 -- x = U for unsigned, S for signed
1134 -- n = 8,16,32,64 for component size
1135 -- Add _Unaligned if length < 4 and component size is 8.
1136 -- <op> is the standard comparison operator
1138 if Component_Size (Typ1) = 8 then
1139 if Length_Less_Than_4 (Op1)
1141 Length_Less_Than_4 (Op2)
1143 if Is_Unsigned_Type (Ctyp) then
1144 Comp := RE_Compare_Array_U8_Unaligned;
1146 Comp := RE_Compare_Array_S8_Unaligned;
1150 if Is_Unsigned_Type (Ctyp) then
1151 Comp := RE_Compare_Array_U8;
1153 Comp := RE_Compare_Array_S8;
1157 elsif Component_Size (Typ1) = 16 then
1158 if Is_Unsigned_Type (Ctyp) then
1159 Comp := RE_Compare_Array_U16;
1161 Comp := RE_Compare_Array_S16;
1164 elsif Component_Size (Typ1) = 32 then
1165 if Is_Unsigned_Type (Ctyp) then
1166 Comp := RE_Compare_Array_U32;
1168 Comp := RE_Compare_Array_S32;
1171 else pragma Assert (Component_Size (Typ1) = 64);
1172 if Is_Unsigned_Type (Ctyp) then
1173 Comp := RE_Compare_Array_U64;
1175 Comp := RE_Compare_Array_S64;
1179 Remove_Side_Effects (Op1, Name_Req => True);
1180 Remove_Side_Effects (Op2, Name_Req => True);
1183 Make_Function_Call (Sloc (Op1),
1184 Name => New_Occurrence_Of (RTE (Comp), Loc),
1186 Parameter_Associations => New_List (
1187 Make_Attribute_Reference (Loc,
1188 Prefix => Relocate_Node (Op1),
1189 Attribute_Name => Name_Address),
1191 Make_Attribute_Reference (Loc,
1192 Prefix => Relocate_Node (Op2),
1193 Attribute_Name => Name_Address),
1195 Make_Attribute_Reference (Loc,
1196 Prefix => Relocate_Node (Op1),
1197 Attribute_Name => Name_Length),
1199 Make_Attribute_Reference (Loc,
1200 Prefix => Relocate_Node (Op2),
1201 Attribute_Name => Name_Length))));
1204 Make_Integer_Literal (Sloc (Op2),
1207 Analyze_And_Resolve (Op1, Standard_Integer);
1208 Analyze_And_Resolve (Op2, Standard_Integer);
1212 -- Cases where we cannot make runtime call
1214 -- For (a <= b) we convert to not (a > b)
1216 if Chars (N) = Name_Op_Le then
1222 Right_Opnd => Op2)));
1223 Analyze_And_Resolve (N, Standard_Boolean);
1226 -- For < the Boolean expression is
1227 -- greater__nn (op2, op1)
1229 elsif Chars (N) = Name_Op_Lt then
1230 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1234 Op1 := Right_Opnd (N);
1235 Op2 := Left_Opnd (N);
1237 -- For (a >= b) we convert to not (a < b)
1239 elsif Chars (N) = Name_Op_Ge then
1245 Right_Opnd => Op2)));
1246 Analyze_And_Resolve (N, Standard_Boolean);
1249 -- For > the Boolean expression is
1250 -- greater__nn (op1, op2)
1253 pragma Assert (Chars (N) = Name_Op_Gt);
1254 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1257 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1259 Make_Function_Call (Loc,
1260 Name => New_Reference_To (Func_Name, Loc),
1261 Parameter_Associations => New_List (Op1, Op2));
1263 Insert_Action (N, Func_Body);
1265 Analyze_And_Resolve (N, Standard_Boolean);
1268 when RE_Not_Available =>
1270 end Expand_Array_Comparison;
1272 ---------------------------
1273 -- Expand_Array_Equality --
1274 ---------------------------
1276 -- Expand an equality function for multi-dimensional arrays. Here is an
1277 -- example of such a function for Nb_Dimension = 2
1279 -- function Enn (A : atyp; B : btyp) return boolean is
1281 -- if (A'length (1) = 0 or else A'length (2) = 0)
1283 -- (B'length (1) = 0 or else B'length (2) = 0)
1285 -- return True; -- RM 4.5.2(22)
1288 -- if A'length (1) /= B'length (1)
1290 -- A'length (2) /= B'length (2)
1292 -- return False; -- RM 4.5.2(23)
1296 -- A1 : Index_T1 := A'first (1);
1297 -- B1 : Index_T1 := B'first (1);
1301 -- A2 : Index_T2 := A'first (2);
1302 -- B2 : Index_T2 := B'first (2);
1305 -- if A (A1, A2) /= B (B1, B2) then
1309 -- exit when A2 = A'last (2);
1310 -- A2 := Index_T2'succ (A2);
1311 -- B2 := Index_T2'succ (B2);
1315 -- exit when A1 = A'last (1);
1316 -- A1 := Index_T1'succ (A1);
1317 -- B1 := Index_T1'succ (B1);
1324 -- Note on the formal types used (atyp and btyp). If either of the arrays
1325 -- is of a private type, we use the underlying type, and do an unchecked
1326 -- conversion of the actual. If either of the arrays has a bound depending
1327 -- on a discriminant, then we use the base type since otherwise we have an
1328 -- escaped discriminant in the function.
1330 -- If both arrays are constrained and have the same bounds, we can generate
1331 -- a loop with an explicit iteration scheme using a 'Range attribute over
1334 function Expand_Array_Equality
1339 Typ : Entity_Id) return Node_Id
1341 Loc : constant Source_Ptr := Sloc (Nod);
1342 Decls : constant List_Id := New_List;
1343 Index_List1 : constant List_Id := New_List;
1344 Index_List2 : constant List_Id := New_List;
1348 Func_Name : Entity_Id;
1349 Func_Body : Node_Id;
1351 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1352 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1356 -- The parameter types to be used for the formals
1361 Num : Int) return Node_Id;
1362 -- This builds the attribute reference Arr'Nam (Expr)
1364 function Component_Equality (Typ : Entity_Id) return Node_Id;
1365 -- Create one statement to compare corresponding components, designated
1366 -- by a full set of indices.
1368 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1369 -- Given one of the arguments, computes the appropriate type to be used
1370 -- for that argument in the corresponding function formal
1372 function Handle_One_Dimension
1374 Index : Node_Id) return Node_Id;
1375 -- This procedure returns the following code
1378 -- Bn : Index_T := B'First (N);
1382 -- exit when An = A'Last (N);
1383 -- An := Index_T'Succ (An)
1384 -- Bn := Index_T'Succ (Bn)
1388 -- If both indices are constrained and identical, the procedure
1389 -- returns a simpler loop:
1391 -- for An in A'Range (N) loop
1395 -- N is the dimension for which we are generating a loop. Index is the
1396 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1397 -- xxx statement is either the loop or declare for the next dimension
1398 -- or if this is the last dimension the comparison of corresponding
1399 -- components of the arrays.
1401 -- The actual way the code works is to return the comparison of
1402 -- corresponding components for the N+1 call. That's neater!
1404 function Test_Empty_Arrays return Node_Id;
1405 -- This function constructs the test for both arrays being empty
1406 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1408 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1410 function Test_Lengths_Correspond return Node_Id;
1411 -- This function constructs the test for arrays having different lengths
1412 -- in at least one index position, in which case the resulting code is:
1414 -- A'length (1) /= B'length (1)
1416 -- A'length (2) /= B'length (2)
1427 Num : Int) return Node_Id
1431 Make_Attribute_Reference (Loc,
1432 Attribute_Name => Nam,
1433 Prefix => New_Reference_To (Arr, Loc),
1434 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1437 ------------------------
1438 -- Component_Equality --
1439 ------------------------
1441 function Component_Equality (Typ : Entity_Id) return Node_Id is
1446 -- if a(i1...) /= b(j1...) then return false; end if;
1449 Make_Indexed_Component (Loc,
1450 Prefix => Make_Identifier (Loc, Chars (A)),
1451 Expressions => Index_List1);
1454 Make_Indexed_Component (Loc,
1455 Prefix => Make_Identifier (Loc, Chars (B)),
1456 Expressions => Index_List2);
1458 Test := Expand_Composite_Equality
1459 (Nod, Component_Type (Typ), L, R, Decls);
1461 -- If some (sub)component is an unchecked_union, the whole operation
1462 -- will raise program error.
1464 if Nkind (Test) = N_Raise_Program_Error then
1466 -- This node is going to be inserted at a location where a
1467 -- statement is expected: clear its Etype so analysis will set
1468 -- it to the expected Standard_Void_Type.
1470 Set_Etype (Test, Empty);
1475 Make_Implicit_If_Statement (Nod,
1476 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1477 Then_Statements => New_List (
1478 Make_Simple_Return_Statement (Loc,
1479 Expression => New_Occurrence_Of (Standard_False, Loc))));
1481 end Component_Equality;
1487 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1498 T := Underlying_Type (T);
1500 X := First_Index (T);
1501 while Present (X) loop
1502 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1504 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1517 --------------------------
1518 -- Handle_One_Dimension --
1519 ---------------------------
1521 function Handle_One_Dimension
1523 Index : Node_Id) return Node_Id
1525 Need_Separate_Indexes : constant Boolean :=
1527 or else not Is_Constrained (Ltyp);
1528 -- If the index types are identical, and we are working with
1529 -- constrained types, then we can use the same index for both
1532 An : constant Entity_Id := Make_Defining_Identifier (Loc,
1533 Chars => New_Internal_Name ('A'));
1536 Index_T : Entity_Id;
1541 if N > Number_Dimensions (Ltyp) then
1542 return Component_Equality (Ltyp);
1545 -- Case where we generate a loop
1547 Index_T := Base_Type (Etype (Index));
1549 if Need_Separate_Indexes then
1551 Make_Defining_Identifier (Loc,
1552 Chars => New_Internal_Name ('B'));
1557 Append (New_Reference_To (An, Loc), Index_List1);
1558 Append (New_Reference_To (Bn, Loc), Index_List2);
1560 Stm_List := New_List (
1561 Handle_One_Dimension (N + 1, Next_Index (Index)));
1563 if Need_Separate_Indexes then
1565 -- Generate guard for loop, followed by increments of indices
1567 Append_To (Stm_List,
1568 Make_Exit_Statement (Loc,
1571 Left_Opnd => New_Reference_To (An, Loc),
1572 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1574 Append_To (Stm_List,
1575 Make_Assignment_Statement (Loc,
1576 Name => New_Reference_To (An, Loc),
1578 Make_Attribute_Reference (Loc,
1579 Prefix => New_Reference_To (Index_T, Loc),
1580 Attribute_Name => Name_Succ,
1581 Expressions => New_List (New_Reference_To (An, Loc)))));
1583 Append_To (Stm_List,
1584 Make_Assignment_Statement (Loc,
1585 Name => New_Reference_To (Bn, 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 (Bn, Loc)))));
1593 -- If separate indexes, we need a declare block for An and Bn, and a
1594 -- loop without an iteration scheme.
1596 if Need_Separate_Indexes then
1598 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1601 Make_Block_Statement (Loc,
1602 Declarations => New_List (
1603 Make_Object_Declaration (Loc,
1604 Defining_Identifier => An,
1605 Object_Definition => New_Reference_To (Index_T, Loc),
1606 Expression => Arr_Attr (A, Name_First, N)),
1608 Make_Object_Declaration (Loc,
1609 Defining_Identifier => Bn,
1610 Object_Definition => New_Reference_To (Index_T, Loc),
1611 Expression => Arr_Attr (B, Name_First, N))),
1613 Handled_Statement_Sequence =>
1614 Make_Handled_Sequence_Of_Statements (Loc,
1615 Statements => New_List (Loop_Stm)));
1617 -- If no separate indexes, return loop statement with explicit
1618 -- iteration scheme on its own
1622 Make_Implicit_Loop_Statement (Nod,
1623 Statements => Stm_List,
1625 Make_Iteration_Scheme (Loc,
1626 Loop_Parameter_Specification =>
1627 Make_Loop_Parameter_Specification (Loc,
1628 Defining_Identifier => An,
1629 Discrete_Subtype_Definition =>
1630 Arr_Attr (A, Name_Range, N))));
1633 end Handle_One_Dimension;
1635 -----------------------
1636 -- Test_Empty_Arrays --
1637 -----------------------
1639 function Test_Empty_Arrays return Node_Id is
1649 for J in 1 .. Number_Dimensions (Ltyp) loop
1652 Left_Opnd => Arr_Attr (A, Name_Length, J),
1653 Right_Opnd => Make_Integer_Literal (Loc, 0));
1657 Left_Opnd => Arr_Attr (B, Name_Length, J),
1658 Right_Opnd => Make_Integer_Literal (Loc, 0));
1667 Left_Opnd => Relocate_Node (Alist),
1668 Right_Opnd => Atest);
1672 Left_Opnd => Relocate_Node (Blist),
1673 Right_Opnd => Btest);
1680 Right_Opnd => Blist);
1681 end Test_Empty_Arrays;
1683 -----------------------------
1684 -- Test_Lengths_Correspond --
1685 -----------------------------
1687 function Test_Lengths_Correspond return Node_Id is
1693 for J in 1 .. Number_Dimensions (Ltyp) loop
1696 Left_Opnd => Arr_Attr (A, Name_Length, J),
1697 Right_Opnd => Arr_Attr (B, Name_Length, J));
1704 Left_Opnd => Relocate_Node (Result),
1705 Right_Opnd => Rtest);
1710 end Test_Lengths_Correspond;
1712 -- Start of processing for Expand_Array_Equality
1715 Ltyp := Get_Arg_Type (Lhs);
1716 Rtyp := Get_Arg_Type (Rhs);
1718 -- For now, if the argument types are not the same, go to the base type,
1719 -- since the code assumes that the formals have the same type. This is
1720 -- fixable in future ???
1722 if Ltyp /= Rtyp then
1723 Ltyp := Base_Type (Ltyp);
1724 Rtyp := Base_Type (Rtyp);
1725 pragma Assert (Ltyp = Rtyp);
1728 -- Build list of formals for function
1730 Formals := New_List (
1731 Make_Parameter_Specification (Loc,
1732 Defining_Identifier => A,
1733 Parameter_Type => New_Reference_To (Ltyp, Loc)),
1735 Make_Parameter_Specification (Loc,
1736 Defining_Identifier => B,
1737 Parameter_Type => New_Reference_To (Rtyp, Loc)));
1739 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
1741 -- Build statement sequence for function
1744 Make_Subprogram_Body (Loc,
1746 Make_Function_Specification (Loc,
1747 Defining_Unit_Name => Func_Name,
1748 Parameter_Specifications => Formals,
1749 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
1751 Declarations => Decls,
1753 Handled_Statement_Sequence =>
1754 Make_Handled_Sequence_Of_Statements (Loc,
1755 Statements => New_List (
1757 Make_Implicit_If_Statement (Nod,
1758 Condition => Test_Empty_Arrays,
1759 Then_Statements => New_List (
1760 Make_Simple_Return_Statement (Loc,
1762 New_Occurrence_Of (Standard_True, Loc)))),
1764 Make_Implicit_If_Statement (Nod,
1765 Condition => Test_Lengths_Correspond,
1766 Then_Statements => New_List (
1767 Make_Simple_Return_Statement (Loc,
1769 New_Occurrence_Of (Standard_False, Loc)))),
1771 Handle_One_Dimension (1, First_Index (Ltyp)),
1773 Make_Simple_Return_Statement (Loc,
1774 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1776 Set_Has_Completion (Func_Name, True);
1777 Set_Is_Inlined (Func_Name);
1779 -- If the array type is distinct from the type of the arguments, it
1780 -- is the full view of a private type. Apply an unchecked conversion
1781 -- to insure that analysis of the call succeeds.
1791 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1793 L := OK_Convert_To (Ltyp, Lhs);
1797 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1799 R := OK_Convert_To (Rtyp, Rhs);
1802 Actuals := New_List (L, R);
1805 Append_To (Bodies, Func_Body);
1808 Make_Function_Call (Loc,
1809 Name => New_Reference_To (Func_Name, Loc),
1810 Parameter_Associations => Actuals);
1811 end Expand_Array_Equality;
1813 -----------------------------
1814 -- Expand_Boolean_Operator --
1815 -----------------------------
1817 -- Note that we first get the actual subtypes of the operands, since we
1818 -- always want to deal with types that have bounds.
1820 procedure Expand_Boolean_Operator (N : Node_Id) is
1821 Typ : constant Entity_Id := Etype (N);
1824 -- Special case of bit packed array where both operands are known to be
1825 -- properly aligned. In this case we use an efficient run time routine
1826 -- to carry out the operation (see System.Bit_Ops).
1828 if Is_Bit_Packed_Array (Typ)
1829 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
1830 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
1832 Expand_Packed_Boolean_Operator (N);
1836 -- For the normal non-packed case, the general expansion is to build
1837 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
1838 -- and then inserting it into the tree. The original operator node is
1839 -- then rewritten as a call to this function. We also use this in the
1840 -- packed case if either operand is a possibly unaligned object.
1843 Loc : constant Source_Ptr := Sloc (N);
1844 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1845 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1846 Func_Body : Node_Id;
1847 Func_Name : Entity_Id;
1850 Convert_To_Actual_Subtype (L);
1851 Convert_To_Actual_Subtype (R);
1852 Ensure_Defined (Etype (L), N);
1853 Ensure_Defined (Etype (R), N);
1854 Apply_Length_Check (R, Etype (L));
1856 if Nkind (N) = N_Op_Xor then
1857 Silly_Boolean_Array_Xor_Test (N, Etype (L));
1860 if Nkind (Parent (N)) = N_Assignment_Statement
1861 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
1863 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
1865 elsif Nkind (Parent (N)) = N_Op_Not
1866 and then Nkind (N) = N_Op_And
1868 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
1873 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
1874 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1875 Insert_Action (N, Func_Body);
1877 -- Now rewrite the expression with a call
1880 Make_Function_Call (Loc,
1881 Name => New_Reference_To (Func_Name, Loc),
1882 Parameter_Associations =>
1885 Make_Type_Conversion
1886 (Loc, New_Reference_To (Etype (L), Loc), R))));
1888 Analyze_And_Resolve (N, Typ);
1891 end Expand_Boolean_Operator;
1893 -------------------------------
1894 -- Expand_Composite_Equality --
1895 -------------------------------
1897 -- This function is only called for comparing internal fields of composite
1898 -- types when these fields are themselves composites. This is a special
1899 -- case because it is not possible to respect normal Ada visibility rules.
1901 function Expand_Composite_Equality
1906 Bodies : List_Id) return Node_Id
1908 Loc : constant Source_Ptr := Sloc (Nod);
1909 Full_Type : Entity_Id;
1914 if Is_Private_Type (Typ) then
1915 Full_Type := Underlying_Type (Typ);
1920 -- Defense against malformed private types with no completion the error
1921 -- will be diagnosed later by check_completion
1923 if No (Full_Type) then
1924 return New_Reference_To (Standard_False, Loc);
1927 Full_Type := Base_Type (Full_Type);
1929 if Is_Array_Type (Full_Type) then
1931 -- If the operand is an elementary type other than a floating-point
1932 -- type, then we can simply use the built-in block bitwise equality,
1933 -- since the predefined equality operators always apply and bitwise
1934 -- equality is fine for all these cases.
1936 if Is_Elementary_Type (Component_Type (Full_Type))
1937 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
1939 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
1941 -- For composite component types, and floating-point types, use the
1942 -- expansion. This deals with tagged component types (where we use
1943 -- the applicable equality routine) and floating-point, (where we
1944 -- need to worry about negative zeroes), and also the case of any
1945 -- composite type recursively containing such fields.
1948 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
1951 elsif Is_Tagged_Type (Full_Type) then
1953 -- Call the primitive operation "=" of this type
1955 if Is_Class_Wide_Type (Full_Type) then
1956 Full_Type := Root_Type (Full_Type);
1959 -- If this is derived from an untagged private type completed with a
1960 -- tagged type, it does not have a full view, so we use the primitive
1961 -- operations of the private type. This check should no longer be
1962 -- necessary when these types receive their full views ???
1964 if Is_Private_Type (Typ)
1965 and then not Is_Tagged_Type (Typ)
1966 and then not Is_Controlled (Typ)
1967 and then Is_Derived_Type (Typ)
1968 and then No (Full_View (Typ))
1970 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
1972 Prim := First_Elmt (Primitive_Operations (Full_Type));
1976 Eq_Op := Node (Prim);
1977 exit when Chars (Eq_Op) = Name_Op_Eq
1978 and then Etype (First_Formal (Eq_Op)) =
1979 Etype (Next_Formal (First_Formal (Eq_Op)))
1980 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
1982 pragma Assert (Present (Prim));
1985 Eq_Op := Node (Prim);
1988 Make_Function_Call (Loc,
1989 Name => New_Reference_To (Eq_Op, Loc),
1990 Parameter_Associations =>
1992 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
1993 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
1995 elsif Is_Record_Type (Full_Type) then
1996 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
1998 if Present (Eq_Op) then
1999 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2001 -- Inherited equality from parent type. Convert the actuals to
2002 -- match signature of operation.
2005 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2009 Make_Function_Call (Loc,
2010 Name => New_Reference_To (Eq_Op, Loc),
2011 Parameter_Associations =>
2012 New_List (OK_Convert_To (T, Lhs),
2013 OK_Convert_To (T, Rhs)));
2017 -- Comparison between Unchecked_Union components
2019 if Is_Unchecked_Union (Full_Type) then
2021 Lhs_Type : Node_Id := Full_Type;
2022 Rhs_Type : Node_Id := Full_Type;
2023 Lhs_Discr_Val : Node_Id;
2024 Rhs_Discr_Val : Node_Id;
2029 if Nkind (Lhs) = N_Selected_Component then
2030 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2035 if Nkind (Rhs) = N_Selected_Component then
2036 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2039 -- Lhs of the composite equality
2041 if Is_Constrained (Lhs_Type) then
2043 -- Since the enclosing record type can never be an
2044 -- Unchecked_Union (this code is executed for records
2045 -- that do not have variants), we may reference its
2048 if Nkind (Lhs) = N_Selected_Component
2049 and then Has_Per_Object_Constraint (
2050 Entity (Selector_Name (Lhs)))
2053 Make_Selected_Component (Loc,
2054 Prefix => Prefix (Lhs),
2057 Get_Discriminant_Value (
2058 First_Discriminant (Lhs_Type),
2060 Stored_Constraint (Lhs_Type))));
2063 Lhs_Discr_Val := New_Copy (
2064 Get_Discriminant_Value (
2065 First_Discriminant (Lhs_Type),
2067 Stored_Constraint (Lhs_Type)));
2071 -- It is not possible to infer the discriminant since
2072 -- the subtype is not constrained.
2075 Make_Raise_Program_Error (Loc,
2076 Reason => PE_Unchecked_Union_Restriction);
2079 -- Rhs of the composite equality
2081 if Is_Constrained (Rhs_Type) then
2082 if Nkind (Rhs) = N_Selected_Component
2083 and then Has_Per_Object_Constraint (
2084 Entity (Selector_Name (Rhs)))
2087 Make_Selected_Component (Loc,
2088 Prefix => Prefix (Rhs),
2091 Get_Discriminant_Value (
2092 First_Discriminant (Rhs_Type),
2094 Stored_Constraint (Rhs_Type))));
2097 Rhs_Discr_Val := New_Copy (
2098 Get_Discriminant_Value (
2099 First_Discriminant (Rhs_Type),
2101 Stored_Constraint (Rhs_Type)));
2106 Make_Raise_Program_Error (Loc,
2107 Reason => PE_Unchecked_Union_Restriction);
2110 -- Call the TSS equality function with the inferred
2111 -- discriminant values.
2114 Make_Function_Call (Loc,
2115 Name => New_Reference_To (Eq_Op, Loc),
2116 Parameter_Associations => New_List (
2124 -- Shouldn't this be an else, we can't fall through the above
2128 Make_Function_Call (Loc,
2129 Name => New_Reference_To (Eq_Op, Loc),
2130 Parameter_Associations => New_List (Lhs, Rhs));
2134 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2138 -- It can be a simple record or the full view of a scalar private
2140 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2142 end Expand_Composite_Equality;
2144 ------------------------
2145 -- Expand_Concatenate --
2146 ------------------------
2148 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2149 Loc : constant Source_Ptr := Sloc (Cnode);
2151 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2152 -- Result type of concatenation
2154 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2155 -- Component type. Elements of this component type can appear as one
2156 -- of the operands of concatenation as well as arrays.
2158 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2161 Ityp : constant Entity_Id := Base_Type (Istyp);
2162 -- Index type. This is the base type of the index subtype, and is used
2163 -- for all computed bounds (which may be out of range of Istyp in the
2164 -- case of null ranges).
2167 -- This is the type we use to do arithmetic to compute the bounds and
2168 -- lengths of operands. The choice of this type is a little subtle and
2169 -- is discussed in a separate section at the start of the body code.
2171 Concatenation_Error : exception;
2172 -- Raised if concatenation is sure to raise a CE
2174 Result_May_Be_Null : Boolean := True;
2175 -- Reset to False if at least one operand is encountered which is known
2176 -- at compile time to be non-null. Used for handling the special case
2177 -- of setting the high bound to the last operand high bound for a null
2178 -- result, thus ensuring a proper high bound in the super-flat case.
2180 N : constant Nat := List_Length (Opnds);
2181 -- Number of concatenation operands including possibly null operands
2184 -- Number of operands excluding any known to be null, except that the
2185 -- last operand is always retained, in case it provides the bounds for
2189 -- Current operand being processed in the loop through operands. After
2190 -- this loop is complete, always contains the last operand (which is not
2191 -- the same as Operands (NN), since null operands are skipped).
2193 -- Arrays describing the operands, only the first NN entries of each
2194 -- array are set (NN < N when we exclude known null operands).
2196 Is_Fixed_Length : array (1 .. N) of Boolean;
2197 -- True if length of corresponding operand known at compile time
2199 Operands : array (1 .. N) of Node_Id;
2200 -- Set to the corresponding entry in the Opnds list (but note that null
2201 -- operands are excluded, so not all entries in the list are stored).
2203 Fixed_Length : array (1 .. N) of Uint;
2204 -- Set to length of operand. Entries in this array are set only if the
2205 -- corresponding entry in Is_Fixed_Length is True.
2207 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2208 -- Set to lower bound of operand. Either an integer literal in the case
2209 -- where the bound is known at compile time, else actual lower bound.
2210 -- The operand low bound is of type Ityp.
2212 Var_Length : array (1 .. N) of Entity_Id;
2213 -- Set to an entity of type Natural that contains the length of an
2214 -- operand whose length is not known at compile time. Entries in this
2215 -- array are set only if the corresponding entry in Is_Fixed_Length
2216 -- is False. The entity is of type Artyp.
2218 Aggr_Length : array (0 .. N) of Node_Id;
2219 -- The J'th entry in an expression node that represents the total length
2220 -- of operands 1 through J. It is either an integer literal node, or a
2221 -- reference to a constant entity with the right value, so it is fine
2222 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2223 -- entry always is set to zero. The length is of type Artyp.
2225 Low_Bound : Node_Id;
2226 -- A tree node representing the low bound of the result (of type Ityp).
2227 -- This is either an integer literal node, or an identifier reference to
2228 -- a constant entity initialized to the appropriate value.
2230 Last_Opnd_High_Bound : Node_Id;
2231 -- A tree node representing the high bound of the last operand. This
2232 -- need only be set if the result could be null. It is used for the
2233 -- special case of setting the right high bound for a null result.
2234 -- This is of type Ityp.
2236 High_Bound : Node_Id;
2237 -- A tree node representing the high bound of the result (of type Ityp)
2240 -- Result of the concatenation (of type Ityp)
2242 Actions : constant List_Id := New_List;
2243 -- Collect actions to be inserted if Save_Space is False
2245 Save_Space : Boolean;
2246 pragma Warnings (Off, Save_Space);
2247 -- Set to True if we are saving generated code space by calling routines
2248 -- in packages System.Concat_n.
2250 Known_Non_Null_Operand_Seen : Boolean;
2251 -- Set True during generation of the assignements of operands into
2252 -- result once an operand known to be non-null has been seen.
2254 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2255 -- This function makes an N_Integer_Literal node that is returned in
2256 -- analyzed form with the type set to Artyp. Importantly this literal
2257 -- is not flagged as static, so that if we do computations with it that
2258 -- result in statically detected out of range conditions, we will not
2259 -- generate error messages but instead warning messages.
2261 function To_Artyp (X : Node_Id) return Node_Id;
2262 -- Given a node of type Ityp, returns the corresponding value of type
2263 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2264 -- For enum types, the Pos of the value is returned.
2266 function To_Ityp (X : Node_Id) return Node_Id;
2267 -- The inverse function (uses Val in the case of enumeration types)
2269 ------------------------
2270 -- Make_Artyp_Literal --
2271 ------------------------
2273 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2274 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2276 Set_Etype (Result, Artyp);
2277 Set_Analyzed (Result, True);
2278 Set_Is_Static_Expression (Result, False);
2280 end Make_Artyp_Literal;
2286 function To_Artyp (X : Node_Id) return Node_Id is
2288 if Ityp = Base_Type (Artyp) then
2291 elsif Is_Enumeration_Type (Ityp) then
2293 Make_Attribute_Reference (Loc,
2294 Prefix => New_Occurrence_Of (Ityp, Loc),
2295 Attribute_Name => Name_Pos,
2296 Expressions => New_List (X));
2299 return Convert_To (Artyp, X);
2307 function To_Ityp (X : Node_Id) return Node_Id is
2309 if Is_Enumeration_Type (Ityp) then
2311 Make_Attribute_Reference (Loc,
2312 Prefix => New_Occurrence_Of (Ityp, Loc),
2313 Attribute_Name => Name_Val,
2314 Expressions => New_List (X));
2316 -- Case where we will do a type conversion
2319 if Ityp = Base_Type (Artyp) then
2322 return Convert_To (Ityp, X);
2327 -- Local Declarations
2329 Opnd_Typ : Entity_Id;
2337 -- Choose an appropriate computational type
2339 -- We will be doing calculations of lengths and bounds in this routine
2340 -- and computing one from the other in some cases, e.g. getting the high
2341 -- bound by adding the length-1 to the low bound.
2343 -- We can't just use the index type, or even its base type for this
2344 -- purpose for two reasons. First it might be an enumeration type which
2345 -- is not suitable fo computations of any kind, and second it may simply
2346 -- not have enough range. For example if the index type is -128..+127
2347 -- then lengths can be up to 256, which is out of range of the type.
2349 -- For enumeration types, we can simply use Standard_Integer, this is
2350 -- sufficient since the actual number of enumeration literals cannot
2351 -- possibly exceed the range of integer (remember we will be doing the
2352 -- arithmetic with POS values, not representation values).
2354 if Is_Enumeration_Type (Ityp) then
2355 Artyp := Standard_Integer;
2357 -- If index type is Positive, we use the standard unsigned type, to give
2358 -- more room on the top of the range, obviating the need for an overflow
2359 -- check when creating the upper bound. This is needed to avoid junk
2360 -- overflow checks in the common case of String types.
2362 -- ??? Disabled for now
2364 -- elsif Istyp = Standard_Positive then
2365 -- Artyp := Standard_Unsigned;
2367 -- For modular types, we use a 32-bit modular type for types whose size
2368 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2369 -- identity type, and for larger unsigned types we use 64-bits.
2371 elsif Is_Modular_Integer_Type (Ityp) then
2372 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
2373 Artyp := Standard_Unsigned;
2374 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
2377 Artyp := RTE (RE_Long_Long_Unsigned);
2380 -- Similar treatment for signed types
2383 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
2384 Artyp := Standard_Integer;
2385 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
2388 Artyp := Standard_Long_Long_Integer;
2392 -- Supply dummy entry at start of length array
2394 Aggr_Length (0) := Make_Artyp_Literal (0);
2396 -- Go through operands setting up the above arrays
2400 Opnd := Remove_Head (Opnds);
2401 Opnd_Typ := Etype (Opnd);
2403 -- The parent got messed up when we put the operands in a list,
2404 -- so now put back the proper parent for the saved operand.
2406 Set_Parent (Opnd, Parent (Cnode));
2408 -- Set will be True when we have setup one entry in the array
2412 -- Singleton element (or character literal) case
2414 if Base_Type (Opnd_Typ) = Ctyp then
2416 Operands (NN) := Opnd;
2417 Is_Fixed_Length (NN) := True;
2418 Fixed_Length (NN) := Uint_1;
2419 Result_May_Be_Null := False;
2421 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2422 -- since we know that the result cannot be null).
2424 Opnd_Low_Bound (NN) :=
2425 Make_Attribute_Reference (Loc,
2426 Prefix => New_Reference_To (Istyp, Loc),
2427 Attribute_Name => Name_First);
2431 -- String literal case (can only occur for strings of course)
2433 elsif Nkind (Opnd) = N_String_Literal then
2434 Len := String_Literal_Length (Opnd_Typ);
2437 Result_May_Be_Null := False;
2440 -- Capture last operand high bound if result could be null
2442 if J = N and then Result_May_Be_Null then
2443 Last_Opnd_High_Bound :=
2446 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
2447 Right_Opnd => Make_Integer_Literal (Loc, 1));
2450 -- Skip null string literal
2452 if J < N and then Len = 0 then
2457 Operands (NN) := Opnd;
2458 Is_Fixed_Length (NN) := True;
2460 -- Set length and bounds
2462 Fixed_Length (NN) := Len;
2464 Opnd_Low_Bound (NN) :=
2465 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
2472 -- Check constrained case with known bounds
2474 if Is_Constrained (Opnd_Typ) then
2476 Index : constant Node_Id := First_Index (Opnd_Typ);
2477 Indx_Typ : constant Entity_Id := Etype (Index);
2478 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
2479 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
2482 -- Fixed length constrained array type with known at compile
2483 -- time bounds is last case of fixed length operand.
2485 if Compile_Time_Known_Value (Lo)
2487 Compile_Time_Known_Value (Hi)
2490 Loval : constant Uint := Expr_Value (Lo);
2491 Hival : constant Uint := Expr_Value (Hi);
2492 Len : constant Uint :=
2493 UI_Max (Hival - Loval + 1, Uint_0);
2497 Result_May_Be_Null := False;
2500 -- Capture last operand bound if result could be null
2502 if J = N and then Result_May_Be_Null then
2503 Last_Opnd_High_Bound :=
2505 Make_Integer_Literal (Loc,
2506 Intval => Expr_Value (Hi)));
2509 -- Exclude null length case unless last operand
2511 if J < N and then Len = 0 then
2516 Operands (NN) := Opnd;
2517 Is_Fixed_Length (NN) := True;
2518 Fixed_Length (NN) := Len;
2520 Opnd_Low_Bound (NN) := To_Ityp (
2521 Make_Integer_Literal (Loc,
2522 Intval => Expr_Value (Lo)));
2530 -- All cases where the length is not known at compile time, or the
2531 -- special case of an operand which is known to be null but has a
2532 -- lower bound other than 1 or is other than a string type.
2537 -- Capture operand bounds
2539 Opnd_Low_Bound (NN) :=
2540 Make_Attribute_Reference (Loc,
2542 Duplicate_Subexpr (Opnd, Name_Req => True),
2543 Attribute_Name => Name_First);
2545 if J = N and Result_May_Be_Null then
2546 Last_Opnd_High_Bound :=
2548 Make_Attribute_Reference (Loc,
2550 Duplicate_Subexpr (Opnd, Name_Req => True),
2551 Attribute_Name => Name_Last));
2554 -- Capture length of operand in entity
2556 Operands (NN) := Opnd;
2557 Is_Fixed_Length (NN) := False;
2560 Make_Defining_Identifier (Loc,
2561 Chars => New_Internal_Name ('L'));
2564 Make_Object_Declaration (Loc,
2565 Defining_Identifier => Var_Length (NN),
2566 Constant_Present => True,
2568 Object_Definition =>
2569 New_Occurrence_Of (Artyp, Loc),
2572 Make_Attribute_Reference (Loc,
2574 Duplicate_Subexpr (Opnd, Name_Req => True),
2575 Attribute_Name => Name_Length)));
2579 -- Set next entry in aggregate length array
2581 -- For first entry, make either integer literal for fixed length
2582 -- or a reference to the saved length for variable length.
2585 if Is_Fixed_Length (1) then
2587 Make_Integer_Literal (Loc,
2588 Intval => Fixed_Length (1));
2591 New_Reference_To (Var_Length (1), Loc);
2594 -- If entry is fixed length and only fixed lengths so far, make
2595 -- appropriate new integer literal adding new length.
2597 elsif Is_Fixed_Length (NN)
2598 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
2601 Make_Integer_Literal (Loc,
2602 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
2604 -- All other cases, construct an addition node for the length and
2605 -- create an entity initialized to this length.
2609 Make_Defining_Identifier (Loc,
2610 Chars => New_Internal_Name ('L'));
2612 if Is_Fixed_Length (NN) then
2613 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
2615 Clen := New_Reference_To (Var_Length (NN), Loc);
2619 Make_Object_Declaration (Loc,
2620 Defining_Identifier => Ent,
2621 Constant_Present => True,
2623 Object_Definition =>
2624 New_Occurrence_Of (Artyp, Loc),
2628 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
2629 Right_Opnd => Clen)));
2631 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
2638 -- If we have only skipped null operands, return the last operand
2645 -- If we have only one non-null operand, return it and we are done.
2646 -- There is one case in which this cannot be done, and that is when
2647 -- the sole operand is of the element type, in which case it must be
2648 -- converted to an array, and the easiest way of doing that is to go
2649 -- through the normal general circuit.
2652 and then Base_Type (Etype (Operands (1))) /= Ctyp
2654 Result := Operands (1);
2658 -- Cases where we have a real concatenation
2660 -- Next step is to find the low bound for the result array that we
2661 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
2663 -- If the ultimate ancestor of the index subtype is a constrained array
2664 -- definition, then the lower bound is that of the index subtype as
2665 -- specified by (RM 4.5.3(6)).
2667 -- The right test here is to go to the root type, and then the ultimate
2668 -- ancestor is the first subtype of this root type.
2670 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
2672 Make_Attribute_Reference (Loc,
2674 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
2675 Attribute_Name => Name_First);
2677 -- If the first operand in the list has known length we know that
2678 -- the lower bound of the result is the lower bound of this operand.
2680 elsif Is_Fixed_Length (1) then
2681 Low_Bound := Opnd_Low_Bound (1);
2683 -- OK, we don't know the lower bound, we have to build a horrible
2684 -- expression actions node of the form
2686 -- if Cond1'Length /= 0 then
2689 -- if Opnd2'Length /= 0 then
2694 -- The nesting ends either when we hit an operand whose length is known
2695 -- at compile time, or on reaching the last operand, whose low bound we
2696 -- take unconditionally whether or not it is null. It's easiest to do
2697 -- this with a recursive procedure:
2701 function Get_Known_Bound (J : Nat) return Node_Id;
2702 -- Returns the lower bound determined by operands J .. NN
2704 ---------------------
2705 -- Get_Known_Bound --
2706 ---------------------
2708 function Get_Known_Bound (J : Nat) return Node_Id is
2710 if Is_Fixed_Length (J) or else J = NN then
2711 return New_Copy (Opnd_Low_Bound (J));
2715 Make_Conditional_Expression (Loc,
2716 Expressions => New_List (
2719 Left_Opnd => New_Reference_To (Var_Length (J), Loc),
2720 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2722 New_Copy (Opnd_Low_Bound (J)),
2723 Get_Known_Bound (J + 1)));
2725 end Get_Known_Bound;
2729 Make_Defining_Identifier (Loc, Chars => New_Internal_Name ('L'));
2732 Make_Object_Declaration (Loc,
2733 Defining_Identifier => Ent,
2734 Constant_Present => True,
2735 Object_Definition => New_Occurrence_Of (Ityp, Loc),
2736 Expression => Get_Known_Bound (1)));
2738 Low_Bound := New_Reference_To (Ent, Loc);
2742 -- Now we can safely compute the upper bound, normally
2743 -- Low_Bound + Length - 1.
2748 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2750 Make_Op_Subtract (Loc,
2751 Left_Opnd => New_Copy (Aggr_Length (NN)),
2752 Right_Opnd => Make_Artyp_Literal (1))));
2754 -- Note that calculation of the high bound may cause overflow in some
2755 -- very weird cases, so in the general case we need an overflow check on
2756 -- the high bound. We can avoid this for the common case of string types
2757 -- and other types whose index is Positive, since we chose a wider range
2758 -- for the arithmetic type.
2760 if Istyp /= Standard_Positive then
2761 Activate_Overflow_Check (High_Bound);
2764 -- Handle the exceptional case where the result is null, in which case
2765 -- case the bounds come from the last operand (so that we get the proper
2766 -- bounds if the last operand is super-flat).
2768 if Result_May_Be_Null then
2770 Make_Conditional_Expression (Loc,
2771 Expressions => New_List (
2773 Left_Opnd => New_Copy (Aggr_Length (NN)),
2774 Right_Opnd => Make_Artyp_Literal (0)),
2775 Last_Opnd_High_Bound,
2779 -- Here is where we insert the saved up actions
2781 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
2783 -- Now we construct an array object with appropriate bounds
2786 Make_Defining_Identifier (Loc,
2787 Chars => New_Internal_Name ('S'));
2789 -- If the bound is statically known to be out of range, we do not want
2790 -- to abort, we want a warning and a runtime constraint error. Note that
2791 -- we have arranged that the result will not be treated as a static
2792 -- constant, so we won't get an illegality during this insertion.
2794 Insert_Action (Cnode,
2795 Make_Object_Declaration (Loc,
2796 Defining_Identifier => Ent,
2797 Object_Definition =>
2798 Make_Subtype_Indication (Loc,
2799 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
2801 Make_Index_Or_Discriminant_Constraint (Loc,
2802 Constraints => New_List (
2804 Low_Bound => Low_Bound,
2805 High_Bound => High_Bound))))),
2806 Suppress => All_Checks);
2808 -- If the result of the concatenation appears as the initializing
2809 -- expression of an object declaration, we can just rename the
2810 -- result, rather than copying it.
2812 Set_OK_To_Rename (Ent);
2814 -- Catch the static out of range case now
2816 if Raises_Constraint_Error (High_Bound) then
2817 raise Concatenation_Error;
2820 -- Now we will generate the assignments to do the actual concatenation
2822 -- There is one case in which we will not do this, namely when all the
2823 -- following conditions are met:
2825 -- The result type is Standard.String
2827 -- There are nine or fewer retained (non-null) operands
2829 -- The optimization level is -O0
2831 -- The corresponding System.Concat_n.Str_Concat_n routine is
2832 -- available in the run time.
2834 -- The debug flag gnatd.c is not set
2836 -- If all these conditions are met then we generate a call to the
2837 -- relevant concatenation routine. The purpose of this is to avoid
2838 -- undesirable code bloat at -O0.
2840 if Atyp = Standard_String
2841 and then NN in 2 .. 9
2842 and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
2843 and then not Debug_Flag_Dot_C
2846 RR : constant array (Nat range 2 .. 9) of RE_Id :=
2857 if RTE_Available (RR (NN)) then
2859 Opnds : constant List_Id :=
2860 New_List (New_Occurrence_Of (Ent, Loc));
2863 for J in 1 .. NN loop
2864 if Is_List_Member (Operands (J)) then
2865 Remove (Operands (J));
2868 if Base_Type (Etype (Operands (J))) = Ctyp then
2870 Make_Aggregate (Loc,
2871 Component_Associations => New_List (
2872 Make_Component_Association (Loc,
2873 Choices => New_List (
2874 Make_Integer_Literal (Loc, 1)),
2875 Expression => Operands (J)))));
2878 Append_To (Opnds, Operands (J));
2882 Insert_Action (Cnode,
2883 Make_Procedure_Call_Statement (Loc,
2884 Name => New_Reference_To (RTE (RR (NN)), Loc),
2885 Parameter_Associations => Opnds));
2887 Result := New_Reference_To (Ent, Loc);
2894 -- Not special case so generate the assignments
2896 Known_Non_Null_Operand_Seen := False;
2898 for J in 1 .. NN loop
2900 Lo : constant Node_Id :=
2902 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2903 Right_Opnd => Aggr_Length (J - 1));
2905 Hi : constant Node_Id :=
2907 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2909 Make_Op_Subtract (Loc,
2910 Left_Opnd => Aggr_Length (J),
2911 Right_Opnd => Make_Artyp_Literal (1)));
2914 -- Singleton case, simple assignment
2916 if Base_Type (Etype (Operands (J))) = Ctyp then
2917 Known_Non_Null_Operand_Seen := True;
2918 Insert_Action (Cnode,
2919 Make_Assignment_Statement (Loc,
2921 Make_Indexed_Component (Loc,
2922 Prefix => New_Occurrence_Of (Ent, Loc),
2923 Expressions => New_List (To_Ityp (Lo))),
2924 Expression => Operands (J)),
2925 Suppress => All_Checks);
2927 -- Array case, slice assignment, skipped when argument is fixed
2928 -- length and known to be null.
2930 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
2933 Make_Assignment_Statement (Loc,
2937 New_Occurrence_Of (Ent, Loc),
2940 Low_Bound => To_Ityp (Lo),
2941 High_Bound => To_Ityp (Hi))),
2942 Expression => Operands (J));
2944 if Is_Fixed_Length (J) then
2945 Known_Non_Null_Operand_Seen := True;
2947 elsif not Known_Non_Null_Operand_Seen then
2949 -- Here if operand length is not statically known and no
2950 -- operand known to be non-null has been processed yet.
2951 -- If operand length is 0, we do not need to perform the
2952 -- assignment, and we must avoid the evaluation of the
2953 -- high bound of the slice, since it may underflow if the
2954 -- low bound is Ityp'First.
2957 Make_Implicit_If_Statement (Cnode,
2961 New_Occurrence_Of (Var_Length (J), Loc),
2962 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2967 Insert_Action (Cnode, Assign, Suppress => All_Checks);
2973 -- Finally we build the result, which is a reference to the array object
2975 Result := New_Reference_To (Ent, Loc);
2978 Rewrite (Cnode, Result);
2979 Analyze_And_Resolve (Cnode, Atyp);
2982 when Concatenation_Error =>
2984 -- Kill warning generated for the declaration of the static out of
2985 -- range high bound, and instead generate a Constraint_Error with
2986 -- an appropriate specific message.
2988 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
2989 Apply_Compile_Time_Constraint_Error
2991 Msg => "concatenation result upper bound out of range?",
2992 Reason => CE_Range_Check_Failed);
2993 -- Set_Etype (Cnode, Atyp);
2994 end Expand_Concatenate;
2996 ------------------------
2997 -- Expand_N_Allocator --
2998 ------------------------
3000 procedure Expand_N_Allocator (N : Node_Id) is
3001 PtrT : constant Entity_Id := Etype (N);
3002 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
3003 Etyp : constant Entity_Id := Etype (Expression (N));
3004 Loc : constant Source_Ptr := Sloc (N);
3009 procedure Complete_Coextension_Finalization;
3010 -- Generate finalization calls for all nested coextensions of N. This
3011 -- routine may allocate list controllers if necessary.
3013 procedure Rewrite_Coextension (N : Node_Id);
3014 -- Static coextensions have the same lifetime as the entity they
3015 -- constrain. Such occurrences can be rewritten as aliased objects
3016 -- and their unrestricted access used instead of the coextension.
3018 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
3019 -- Given a constrained array type E, returns a node representing the
3020 -- code to compute the size in storage elements for the given type.
3021 -- This is done without using the attribute (which malfunctions for
3024 ---------------------------------------
3025 -- Complete_Coextension_Finalization --
3026 ---------------------------------------
3028 procedure Complete_Coextension_Finalization is
3030 Coext_Elmt : Elmt_Id;
3034 function Inside_A_Return_Statement (N : Node_Id) return Boolean;
3035 -- Determine whether node N is part of a return statement
3037 function Needs_Initialization_Call (N : Node_Id) return Boolean;
3038 -- Determine whether node N is a subtype indicator allocator which
3039 -- acts a coextension. Such coextensions need initialization.
3041 -------------------------------
3042 -- Inside_A_Return_Statement --
3043 -------------------------------
3045 function Inside_A_Return_Statement (N : Node_Id) return Boolean is
3050 while Present (P) loop
3052 (P, N_Extended_Return_Statement, N_Simple_Return_Statement)
3056 -- Stop the traversal when we reach a subprogram body
3058 elsif Nkind (P) = N_Subprogram_Body then
3066 end Inside_A_Return_Statement;
3068 -------------------------------
3069 -- Needs_Initialization_Call --
3070 -------------------------------
3072 function Needs_Initialization_Call (N : Node_Id) return Boolean is
3076 if Nkind (N) = N_Explicit_Dereference
3077 and then Nkind (Prefix (N)) = N_Identifier
3078 and then Nkind (Parent (Entity (Prefix (N)))) =
3079 N_Object_Declaration
3081 Obj_Decl := Parent (Entity (Prefix (N)));
3084 Present (Expression (Obj_Decl))
3085 and then Nkind (Expression (Obj_Decl)) = N_Allocator
3086 and then Nkind (Expression (Expression (Obj_Decl))) /=
3087 N_Qualified_Expression;
3091 end Needs_Initialization_Call;
3093 -- Start of processing for Complete_Coextension_Finalization
3096 -- When a coextension root is inside a return statement, we need to
3097 -- use the finalization chain of the function's scope. This does not
3098 -- apply for controlled named access types because in those cases we
3099 -- can use the finalization chain of the type itself.
3101 if Inside_A_Return_Statement (N)
3103 (Ekind (PtrT) = E_Anonymous_Access_Type
3105 (Ekind (PtrT) = E_Access_Type
3106 and then No (Associated_Final_Chain (PtrT))))
3110 Outer_S : Entity_Id;
3111 S : Entity_Id := Current_Scope;
3114 while Present (S) and then S /= Standard_Standard loop
3115 if Ekind (S) = E_Function then
3116 Outer_S := Scope (S);
3118 -- Retrieve the declaration of the body
3123 (Corresponding_Body (Parent (Parent (S)))));
3130 -- Push the scope of the function body since we are inserting
3131 -- the list before the body, but we are currently in the body
3132 -- itself. Override the finalization list of PtrT since the
3133 -- finalization context is now different.
3135 Push_Scope (Outer_S);
3136 Build_Final_List (Decl, PtrT);
3140 -- The root allocator may not be controlled, but it still needs a
3141 -- finalization list for all nested coextensions.
3143 elsif No (Associated_Final_Chain (PtrT)) then
3144 Build_Final_List (N, PtrT);
3148 Make_Selected_Component (Loc,
3150 New_Reference_To (Associated_Final_Chain (PtrT), Loc),
3152 Make_Identifier (Loc, Name_F));
3154 Coext_Elmt := First_Elmt (Coextensions (N));
3155 while Present (Coext_Elmt) loop
3156 Coext := Node (Coext_Elmt);
3161 if Nkind (Coext) = N_Identifier then
3163 Make_Unchecked_Type_Conversion (Loc,
3164 Subtype_Mark => New_Reference_To (Etype (Coext), Loc),
3166 Make_Explicit_Dereference (Loc,
3167 Prefix => New_Copy_Tree (Coext)));
3169 Ref := New_Copy_Tree (Coext);
3172 -- No initialization call if not allowed
3174 Check_Restriction (No_Default_Initialization, N);
3176 if not Restriction_Active (No_Default_Initialization) then
3180 -- attach_to_final_list (Ref, Flist, 2)
3182 if Needs_Initialization_Call (Coext) then
3186 Typ => Etype (Coext),
3188 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3191 -- attach_to_final_list (Ref, Flist, 2)
3197 Flist_Ref => New_Copy_Tree (Flist),
3198 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3202 Next_Elmt (Coext_Elmt);
3204 end Complete_Coextension_Finalization;
3206 -------------------------
3207 -- Rewrite_Coextension --
3208 -------------------------
3210 procedure Rewrite_Coextension (N : Node_Id) is
3211 Temp : constant Node_Id :=
3212 Make_Defining_Identifier (Loc,
3213 New_Internal_Name ('C'));
3216 -- Cnn : aliased Etyp;
3218 Decl : constant Node_Id :=
3219 Make_Object_Declaration (Loc,
3220 Defining_Identifier => Temp,
3221 Aliased_Present => True,
3222 Object_Definition =>
3223 New_Occurrence_Of (Etyp, Loc));
3227 if Nkind (Expression (N)) = N_Qualified_Expression then
3228 Set_Expression (Decl, Expression (Expression (N)));
3231 -- Find the proper insertion node for the declaration
3234 while Present (Nod) loop
3235 exit when Nkind (Nod) in N_Statement_Other_Than_Procedure_Call
3236 or else Nkind (Nod) = N_Procedure_Call_Statement
3237 or else Nkind (Nod) in N_Declaration;
3238 Nod := Parent (Nod);
3241 Insert_Before (Nod, Decl);
3245 Make_Attribute_Reference (Loc,
3246 Prefix => New_Occurrence_Of (Temp, Loc),
3247 Attribute_Name => Name_Unrestricted_Access));
3249 Analyze_And_Resolve (N, PtrT);
3250 end Rewrite_Coextension;
3252 ------------------------------
3253 -- Size_In_Storage_Elements --
3254 ------------------------------
3256 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
3258 -- Logically this just returns E'Max_Size_In_Storage_Elements.
3259 -- However, the reason for the existence of this function is
3260 -- to construct a test for sizes too large, which means near the
3261 -- 32-bit limit on a 32-bit machine, and precisely the trouble
3262 -- is that we get overflows when sizes are greater than 2**31.
3264 -- So what we end up doing for array types is to use the expression:
3266 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
3268 -- which avoids this problem. All this is a big bogus, but it does
3269 -- mean we catch common cases of trying to allocate arrays that
3270 -- are too large, and which in the absence of a check results in
3271 -- undetected chaos ???
3278 for J in 1 .. Number_Dimensions (E) loop
3280 Make_Attribute_Reference (Loc,
3281 Prefix => New_Occurrence_Of (E, Loc),
3282 Attribute_Name => Name_Length,
3283 Expressions => New_List (
3284 Make_Integer_Literal (Loc, J)));
3291 Make_Op_Multiply (Loc,
3298 Make_Op_Multiply (Loc,
3301 Make_Attribute_Reference (Loc,
3302 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
3303 Attribute_Name => Name_Max_Size_In_Storage_Elements));
3305 end Size_In_Storage_Elements;
3307 -- Start of processing for Expand_N_Allocator
3310 -- RM E.2.3(22). We enforce that the expected type of an allocator
3311 -- shall not be a remote access-to-class-wide-limited-private type
3313 -- Why is this being done at expansion time, seems clearly wrong ???
3315 Validate_Remote_Access_To_Class_Wide_Type (N);
3317 -- Set the Storage Pool
3319 Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT)));
3321 if Present (Storage_Pool (N)) then
3322 if Is_RTE (Storage_Pool (N), RE_SS_Pool) then
3323 if VM_Target = No_VM then
3324 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3327 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
3328 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
3331 Set_Procedure_To_Call (N,
3332 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
3336 -- Under certain circumstances we can replace an allocator by an access
3337 -- to statically allocated storage. The conditions, as noted in AARM
3338 -- 3.10 (10c) are as follows:
3340 -- Size and initial value is known at compile time
3341 -- Access type is access-to-constant
3343 -- The allocator is not part of a constraint on a record component,
3344 -- because in that case the inserted actions are delayed until the
3345 -- record declaration is fully analyzed, which is too late for the
3346 -- analysis of the rewritten allocator.
3348 if Is_Access_Constant (PtrT)
3349 and then Nkind (Expression (N)) = N_Qualified_Expression
3350 and then Compile_Time_Known_Value (Expression (Expression (N)))
3351 and then Size_Known_At_Compile_Time (Etype (Expression
3353 and then not Is_Record_Type (Current_Scope)
3355 -- Here we can do the optimization. For the allocator
3359 -- We insert an object declaration
3361 -- Tnn : aliased x := y;
3363 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
3364 -- marked as requiring static allocation.
3367 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
3369 Desig := Subtype_Mark (Expression (N));
3371 -- If context is constrained, use constrained subtype directly,
3372 -- so that the constant is not labelled as having a nominally
3373 -- unconstrained subtype.
3375 if Entity (Desig) = Base_Type (Dtyp) then
3376 Desig := New_Occurrence_Of (Dtyp, Loc);
3380 Make_Object_Declaration (Loc,
3381 Defining_Identifier => Temp,
3382 Aliased_Present => True,
3383 Constant_Present => Is_Access_Constant (PtrT),
3384 Object_Definition => Desig,
3385 Expression => Expression (Expression (N))));
3388 Make_Attribute_Reference (Loc,
3389 Prefix => New_Occurrence_Of (Temp, Loc),
3390 Attribute_Name => Name_Unrestricted_Access));
3392 Analyze_And_Resolve (N, PtrT);
3394 -- We set the variable as statically allocated, since we don't want
3395 -- it going on the stack of the current procedure!
3397 Set_Is_Statically_Allocated (Temp);
3401 -- Same if the allocator is an access discriminant for a local object:
3402 -- instead of an allocator we create a local value and constrain the
3403 -- the enclosing object with the corresponding access attribute.
3405 if Is_Static_Coextension (N) then
3406 Rewrite_Coextension (N);
3410 -- The current allocator creates an object which may contain nested
3411 -- coextensions. Use the current allocator's finalization list to
3412 -- generate finalization call for all nested coextensions.
3414 if Is_Coextension_Root (N) then
3415 Complete_Coextension_Finalization;
3418 -- Check for size too large, we do this because the back end misses
3419 -- proper checks here and can generate rubbish allocation calls when
3420 -- we are near the limit. We only do this for the 32-bit address case
3421 -- since that is from a practical point of view where we see a problem.
3423 if System_Address_Size = 32
3424 and then not Storage_Checks_Suppressed (PtrT)
3425 and then not Storage_Checks_Suppressed (Dtyp)
3426 and then not Storage_Checks_Suppressed (Etyp)
3428 -- The check we want to generate should look like
3430 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
3431 -- raise Storage_Error;
3434 -- where 3.5 gigabytes is a constant large enough to accomodate any
3435 -- reasonable request for. But we can't do it this way because at
3436 -- least at the moment we don't compute this attribute right, and
3437 -- can silently give wrong results when the result gets large. Since
3438 -- this is all about large results, that's bad, so instead we only
3439 -- apply the check for constrained arrays, and manually compute the
3440 -- value of the attribute ???
3442 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
3444 Make_Raise_Storage_Error (Loc,
3447 Left_Opnd => Size_In_Storage_Elements (Etyp),
3449 Make_Integer_Literal (Loc,
3450 Intval => Uint_7 * (Uint_2 ** 29))),
3451 Reason => SE_Object_Too_Large));
3455 -- Handle case of qualified expression (other than optimization above)
3456 -- First apply constraint checks, because the bounds or discriminants
3457 -- in the aggregate might not match the subtype mark in the allocator.
3459 if Nkind (Expression (N)) = N_Qualified_Expression then
3460 Apply_Constraint_Check
3461 (Expression (Expression (N)), Etype (Expression (N)));
3463 Expand_Allocator_Expression (N);
3467 -- If the allocator is for a type which requires initialization, and
3468 -- there is no initial value (i.e. operand is a subtype indication
3469 -- rather than a qualified expression), then we must generate a call to
3470 -- the initialization routine using an expressions action node:
3472 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
3474 -- Here ptr_T is the pointer type for the allocator, and T is the
3475 -- subtype of the allocator. A special case arises if the designated
3476 -- type of the access type is a task or contains tasks. In this case
3477 -- the call to Init (Temp.all ...) is replaced by code that ensures
3478 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
3479 -- for details). In addition, if the type T is a task T, then the
3480 -- first argument to Init must be converted to the task record type.
3483 T : constant Entity_Id := Entity (Expression (N));
3491 Temp_Decl : Node_Id;
3492 Temp_Type : Entity_Id;
3493 Attach_Level : Uint;
3496 if No_Initialization (N) then
3499 -- Case of no initialization procedure present
3501 elsif not Has_Non_Null_Base_Init_Proc (T) then
3503 -- Case of simple initialization required
3505 if Needs_Simple_Initialization (T) then
3506 Check_Restriction (No_Default_Initialization, N);
3507 Rewrite (Expression (N),
3508 Make_Qualified_Expression (Loc,
3509 Subtype_Mark => New_Occurrence_Of (T, Loc),
3510 Expression => Get_Simple_Init_Val (T, N)));
3512 Analyze_And_Resolve (Expression (Expression (N)), T);
3513 Analyze_And_Resolve (Expression (N), T);
3514 Set_Paren_Count (Expression (Expression (N)), 1);
3515 Expand_N_Allocator (N);
3517 -- No initialization required
3523 -- Case of initialization procedure present, must be called
3526 Check_Restriction (No_Default_Initialization, N);
3528 if not Restriction_Active (No_Default_Initialization) then
3529 Init := Base_Init_Proc (T);
3531 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
3533 -- Construct argument list for the initialization routine call
3536 Make_Explicit_Dereference (Loc,
3537 Prefix => New_Reference_To (Temp, Loc));
3538 Set_Assignment_OK (Arg1);
3541 -- The initialization procedure expects a specific type. if the
3542 -- context is access to class wide, indicate that the object
3543 -- being allocated has the right specific type.
3545 if Is_Class_Wide_Type (Dtyp) then
3546 Arg1 := Unchecked_Convert_To (T, Arg1);
3549 -- If designated type is a concurrent type or if it is private
3550 -- type whose definition is a concurrent type, the first
3551 -- argument in the Init routine has to be unchecked conversion
3552 -- to the corresponding record type. If the designated type is
3553 -- a derived type, we also convert the argument to its root
3556 if Is_Concurrent_Type (T) then
3558 Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1);
3560 elsif Is_Private_Type (T)
3561 and then Present (Full_View (T))
3562 and then Is_Concurrent_Type (Full_View (T))
3565 Unchecked_Convert_To
3566 (Corresponding_Record_Type (Full_View (T)), Arg1);
3568 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
3570 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
3572 Arg1 := OK_Convert_To (Etype (Ftyp), Arg1);
3573 Set_Etype (Arg1, Ftyp);
3577 Args := New_List (Arg1);
3579 -- For the task case, pass the Master_Id of the access type as
3580 -- the value of the _Master parameter, and _Chain as the value
3581 -- of the _Chain parameter (_Chain will be defined as part of
3582 -- the generated code for the allocator).
3584 -- In Ada 2005, the context may be a function that returns an
3585 -- anonymous access type. In that case the Master_Id has been
3586 -- created when expanding the function declaration.
3588 if Has_Task (T) then
3589 if No (Master_Id (Base_Type (PtrT))) then
3591 -- If we have a non-library level task with restriction
3592 -- No_Task_Hierarchy set, then no point in expanding.
3594 if not Is_Library_Level_Entity (T)
3595 and then Restriction_Active (No_Task_Hierarchy)
3600 -- The designated type was an incomplete type, and the
3601 -- access type did not get expanded. Salvage it now.
3603 pragma Assert (Present (Parent (Base_Type (PtrT))));
3604 Expand_N_Full_Type_Declaration
3605 (Parent (Base_Type (PtrT)));
3608 -- If the context of the allocator is a declaration or an
3609 -- assignment, we can generate a meaningful image for it,
3610 -- even though subsequent assignments might remove the
3611 -- connection between task and entity. We build this image
3612 -- when the left-hand side is a simple variable, a simple
3613 -- indexed assignment or a simple selected component.
3615 if Nkind (Parent (N)) = N_Assignment_Statement then
3617 Nam : constant Node_Id := Name (Parent (N));
3620 if Is_Entity_Name (Nam) then
3622 Build_Task_Image_Decls
3625 (Entity (Nam), Sloc (Nam)), T);
3628 (Nam, N_Indexed_Component, N_Selected_Component)
3629 and then Is_Entity_Name (Prefix (Nam))
3632 Build_Task_Image_Decls
3633 (Loc, Nam, Etype (Prefix (Nam)));
3635 Decls := Build_Task_Image_Decls (Loc, T, T);
3639 elsif Nkind (Parent (N)) = N_Object_Declaration then
3641 Build_Task_Image_Decls
3642 (Loc, Defining_Identifier (Parent (N)), T);
3645 Decls := Build_Task_Image_Decls (Loc, T, T);
3650 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
3651 Append_To (Args, Make_Identifier (Loc, Name_uChain));
3653 Decl := Last (Decls);
3655 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
3657 -- Has_Task is false, Decls not used
3663 -- Add discriminants if discriminated type
3666 Dis : Boolean := False;
3670 if Has_Discriminants (T) then
3674 elsif Is_Private_Type (T)
3675 and then Present (Full_View (T))
3676 and then Has_Discriminants (Full_View (T))
3679 Typ := Full_View (T);
3684 -- If the allocated object will be constrained by the
3685 -- default values for discriminants, then build a subtype
3686 -- with those defaults, and change the allocated subtype
3687 -- to that. Note that this happens in fewer cases in Ada
3690 if not Is_Constrained (Typ)
3691 and then Present (Discriminant_Default_Value
3692 (First_Discriminant (Typ)))
3693 and then (Ada_Version < Ada_05
3695 not Has_Constrained_Partial_View (Typ))
3697 Typ := Build_Default_Subtype (Typ, N);
3698 Set_Expression (N, New_Reference_To (Typ, Loc));
3701 Discr := First_Elmt (Discriminant_Constraint (Typ));
3702 while Present (Discr) loop
3703 Nod := Node (Discr);
3704 Append (New_Copy_Tree (Node (Discr)), Args);
3706 -- AI-416: when the discriminant constraint is an
3707 -- anonymous access type make sure an accessibility
3708 -- check is inserted if necessary (3.10.2(22.q/2))
3710 if Ada_Version >= Ada_05
3712 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
3714 Apply_Accessibility_Check
3715 (Nod, Typ, Insert_Node => Nod);
3723 -- We set the allocator as analyzed so that when we analyze the
3724 -- expression actions node, we do not get an unwanted recursive
3725 -- expansion of the allocator expression.
3727 Set_Analyzed (N, True);
3728 Nod := Relocate_Node (N);
3730 -- Here is the transformation:
3732 -- output: Temp : constant ptr_T := new T;
3733 -- Init (Temp.all, ...);
3734 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
3735 -- <CTRL> Initialize (Finalizable (Temp.all));
3737 -- Here ptr_T is the pointer type for the allocator, and is the
3738 -- subtype of the allocator.
3741 Make_Object_Declaration (Loc,
3742 Defining_Identifier => Temp,
3743 Constant_Present => True,
3744 Object_Definition => New_Reference_To (Temp_Type, Loc),
3747 Set_Assignment_OK (Temp_Decl);
3748 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
3750 -- If the designated type is a task type or contains tasks,
3751 -- create block to activate created tasks, and insert
3752 -- declaration for Task_Image variable ahead of call.
3754 if Has_Task (T) then
3756 L : constant List_Id := New_List;
3759 Build_Task_Allocate_Block (L, Nod, Args);
3761 Insert_List_Before (First (Declarations (Blk)), Decls);
3762 Insert_Actions (N, L);
3767 Make_Procedure_Call_Statement (Loc,
3768 Name => New_Reference_To (Init, Loc),
3769 Parameter_Associations => Args));
3772 if Needs_Finalization (T) then
3774 -- Postpone the generation of a finalization call for the
3775 -- current allocator if it acts as a coextension.
3777 if Is_Dynamic_Coextension (N) then
3778 if No (Coextensions (N)) then
3779 Set_Coextensions (N, New_Elmt_List);
3782 Append_Elmt (New_Copy_Tree (Arg1), Coextensions (N));
3786 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
3788 -- Anonymous access types created for access parameters
3789 -- are attached to an explicitly constructed controller,
3790 -- which ensures that they can be finalized properly,
3791 -- even if their deallocation might not happen. The list
3792 -- associated with the controller is doubly-linked. For
3793 -- other anonymous access types, the object may end up
3794 -- on the global final list which is singly-linked.
3795 -- Work needed for access discriminants in Ada 2005 ???
3797 if Ekind (PtrT) = E_Anonymous_Access_Type then
3798 Attach_Level := Uint_1;
3800 Attach_Level := Uint_2;
3805 Ref => New_Copy_Tree (Arg1),
3808 With_Attach => Make_Integer_Literal (Loc,
3809 Intval => Attach_Level)));
3813 Rewrite (N, New_Reference_To (Temp, Loc));
3814 Analyze_And_Resolve (N, PtrT);
3819 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
3820 -- object that has been rewritten as a reference, we displace "this"
3821 -- to reference properly its secondary dispatch table.
3823 if Nkind (N) = N_Identifier
3824 and then Is_Interface (Dtyp)
3826 Displace_Allocator_Pointer (N);
3830 when RE_Not_Available =>
3832 end Expand_N_Allocator;
3834 -----------------------
3835 -- Expand_N_And_Then --
3836 -----------------------
3838 -- Expand into conditional expression if Actions present, and also deal
3839 -- with optimizing case of arguments being True or False.
3841 procedure Expand_N_And_Then (N : Node_Id) is
3842 Loc : constant Source_Ptr := Sloc (N);
3843 Typ : constant Entity_Id := Etype (N);
3844 Left : constant Node_Id := Left_Opnd (N);
3845 Right : constant Node_Id := Right_Opnd (N);
3849 -- Deal with non-standard booleans
3851 if Is_Boolean_Type (Typ) then
3852 Adjust_Condition (Left);
3853 Adjust_Condition (Right);
3854 Set_Etype (N, Standard_Boolean);
3857 -- Check for cases where left argument is known to be True or False
3859 if Compile_Time_Known_Value (Left) then
3861 -- If left argument is True, change (True and then Right) to Right.
3862 -- Any actions associated with Right will be executed unconditionally
3863 -- and can thus be inserted into the tree unconditionally.
3865 if Expr_Value_E (Left) = Standard_True then
3866 if Present (Actions (N)) then
3867 Insert_Actions (N, Actions (N));
3872 -- If left argument is False, change (False and then Right) to False.
3873 -- In this case we can forget the actions associated with Right,
3874 -- since they will never be executed.
3876 else pragma Assert (Expr_Value_E (Left) = Standard_False);
3877 Kill_Dead_Code (Right);
3878 Kill_Dead_Code (Actions (N));
3879 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3882 Adjust_Result_Type (N, Typ);
3886 -- If Actions are present, we expand
3888 -- left and then right
3892 -- if left then right else false end
3894 -- with the actions becoming the Then_Actions of the conditional
3895 -- expression. This conditional expression is then further expanded
3896 -- (and will eventually disappear)
3898 if Present (Actions (N)) then
3899 Actlist := Actions (N);
3901 Make_Conditional_Expression (Loc,
3902 Expressions => New_List (
3905 New_Occurrence_Of (Standard_False, Loc))));
3907 Set_Then_Actions (N, Actlist);
3908 Analyze_And_Resolve (N, Standard_Boolean);
3909 Adjust_Result_Type (N, Typ);
3913 -- No actions present, check for cases of right argument True/False
3915 if Compile_Time_Known_Value (Right) then
3917 -- Change (Left and then True) to Left. Note that we know there are
3918 -- no actions associated with the True operand, since we just checked
3919 -- for this case above.
3921 if Expr_Value_E (Right) = Standard_True then
3924 -- Change (Left and then False) to False, making sure to preserve any
3925 -- side effects associated with the Left operand.
3927 else pragma Assert (Expr_Value_E (Right) = Standard_False);
3928 Remove_Side_Effects (Left);
3930 (N, New_Occurrence_Of (Standard_False, Loc));
3934 Adjust_Result_Type (N, Typ);
3935 end Expand_N_And_Then;
3937 -------------------------------------
3938 -- Expand_N_Conditional_Expression --
3939 -------------------------------------
3941 -- Expand into expression actions if then/else actions present
3943 procedure Expand_N_Conditional_Expression (N : Node_Id) is
3944 Loc : constant Source_Ptr := Sloc (N);
3945 Cond : constant Node_Id := First (Expressions (N));
3946 Thenx : constant Node_Id := Next (Cond);
3947 Elsex : constant Node_Id := Next (Thenx);
3948 Typ : constant Entity_Id := Etype (N);
3953 -- If either then or else actions are present, then given:
3955 -- if cond then then-expr else else-expr end
3957 -- we insert the following sequence of actions (using Insert_Actions):
3962 -- Cnn := then-expr;
3968 -- and replace the conditional expression by a reference to Cnn
3970 if Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
3971 Cnn := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
3974 Make_Implicit_If_Statement (N,
3975 Condition => Relocate_Node (Cond),
3977 Then_Statements => New_List (
3978 Make_Assignment_Statement (Sloc (Thenx),
3979 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
3980 Expression => Relocate_Node (Thenx))),
3982 Else_Statements => New_List (
3983 Make_Assignment_Statement (Sloc (Elsex),
3984 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
3985 Expression => Relocate_Node (Elsex))));
3987 -- Move the SLOC of the parent If statement to the newly created
3988 -- one and change it to the SLOC of the expression which, after
3989 -- expansion, will correspond to what is being evaluated.
3991 if Present (Parent (N))
3992 and then Nkind (Parent (N)) = N_If_Statement
3994 Set_Sloc (New_If, Sloc (Parent (N)));
3995 Set_Sloc (Parent (N), Loc);
3998 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
3999 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
4001 if Present (Then_Actions (N)) then
4003 (First (Then_Statements (New_If)), Then_Actions (N));
4006 if Present (Else_Actions (N)) then
4008 (First (Else_Statements (New_If)), Else_Actions (N));
4011 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
4014 Make_Object_Declaration (Loc,
4015 Defining_Identifier => Cnn,
4016 Object_Definition => New_Occurrence_Of (Typ, Loc)));
4018 Insert_Action (N, New_If);
4019 Analyze_And_Resolve (N, Typ);
4021 end Expand_N_Conditional_Expression;
4023 -----------------------------------
4024 -- Expand_N_Explicit_Dereference --
4025 -----------------------------------
4027 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
4029 -- Insert explicit dereference call for the checked storage pool case
4031 Insert_Dereference_Action (Prefix (N));
4032 end Expand_N_Explicit_Dereference;
4038 procedure Expand_N_In (N : Node_Id) is
4039 Loc : constant Source_Ptr := Sloc (N);
4040 Rtyp : constant Entity_Id := Etype (N);
4041 Lop : constant Node_Id := Left_Opnd (N);
4042 Rop : constant Node_Id := Right_Opnd (N);
4043 Static : constant Boolean := Is_OK_Static_Expression (N);
4045 procedure Substitute_Valid_Check;
4046 -- Replaces node N by Lop'Valid. This is done when we have an explicit
4047 -- test for the left operand being in range of its subtype.
4049 ----------------------------
4050 -- Substitute_Valid_Check --
4051 ----------------------------
4053 procedure Substitute_Valid_Check is
4056 Make_Attribute_Reference (Loc,
4057 Prefix => Relocate_Node (Lop),
4058 Attribute_Name => Name_Valid));
4060 Analyze_And_Resolve (N, Rtyp);
4062 Error_Msg_N ("?explicit membership test may be optimized away", N);
4063 Error_Msg_N ("\?use ''Valid attribute instead", N);
4065 end Substitute_Valid_Check;
4067 -- Start of processing for Expand_N_In
4070 -- Check case of explicit test for an expression in range of its
4071 -- subtype. This is suspicious usage and we replace it with a 'Valid
4072 -- test and give a warning.
4074 if Is_Scalar_Type (Etype (Lop))
4075 and then Nkind (Rop) in N_Has_Entity
4076 and then Etype (Lop) = Entity (Rop)
4077 and then Comes_From_Source (N)
4078 and then VM_Target = No_VM
4080 Substitute_Valid_Check;
4084 -- Do validity check on operands
4086 if Validity_Checks_On and Validity_Check_Operands then
4087 Ensure_Valid (Left_Opnd (N));
4088 Validity_Check_Range (Right_Opnd (N));
4091 -- Case of explicit range
4093 if Nkind (Rop) = N_Range then
4095 Lo : constant Node_Id := Low_Bound (Rop);
4096 Hi : constant Node_Id := High_Bound (Rop);
4098 Ltyp : constant Entity_Id := Etype (Lop);
4100 Lo_Orig : constant Node_Id := Original_Node (Lo);
4101 Hi_Orig : constant Node_Id := Original_Node (Hi);
4103 Lcheck : Compare_Result;
4104 Ucheck : Compare_Result;
4106 Warn1 : constant Boolean :=
4107 Constant_Condition_Warnings
4108 and then Comes_From_Source (N)
4109 and then not In_Instance;
4110 -- This must be true for any of the optimization warnings, we
4111 -- clearly want to give them only for source with the flag on.
4112 -- We also skip these warnings in an instance since it may be
4113 -- the case that different instantiations have different ranges.
4115 Warn2 : constant Boolean :=
4117 and then Nkind (Original_Node (Rop)) = N_Range
4118 and then Is_Integer_Type (Etype (Lo));
4119 -- For the case where only one bound warning is elided, we also
4120 -- insist on an explicit range and an integer type. The reason is
4121 -- that the use of enumeration ranges including an end point is
4122 -- common, as is the use of a subtype name, one of whose bounds
4123 -- is the same as the type of the expression.
4126 -- If test is explicit x'first .. x'last, replace by valid check
4128 if Is_Scalar_Type (Ltyp)
4129 and then Nkind (Lo_Orig) = N_Attribute_Reference
4130 and then Attribute_Name (Lo_Orig) = Name_First
4131 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
4132 and then Entity (Prefix (Lo_Orig)) = Ltyp
4133 and then Nkind (Hi_Orig) = N_Attribute_Reference
4134 and then Attribute_Name (Hi_Orig) = Name_Last
4135 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
4136 and then Entity (Prefix (Hi_Orig)) = Ltyp
4137 and then Comes_From_Source (N)
4138 and then VM_Target = No_VM
4140 Substitute_Valid_Check;
4144 -- If bounds of type are known at compile time, and the end points
4145 -- are known at compile time and identical, this is another case
4146 -- for substituting a valid test. We only do this for discrete
4147 -- types, since it won't arise in practice for float types.
4149 if Comes_From_Source (N)
4150 and then Is_Discrete_Type (Ltyp)
4151 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
4152 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
4153 and then Compile_Time_Known_Value (Lo)
4154 and then Compile_Time_Known_Value (Hi)
4155 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
4156 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
4158 -- Kill warnings in instances, since they may be cases where we
4159 -- have a test in the generic that makes sense with some types
4160 -- and not with other types.
4162 and then not In_Instance
4164 Substitute_Valid_Check;
4168 -- If we have an explicit range, do a bit of optimization based
4169 -- on range analysis (we may be able to kill one or both checks).
4171 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
4172 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
4174 -- If either check is known to fail, replace result by False since
4175 -- the other check does not matter. Preserve the static flag for
4176 -- legality checks, because we are constant-folding beyond RM 4.9.
4178 if Lcheck = LT or else Ucheck = GT then
4180 Error_Msg_N ("?range test optimized away", N);
4181 Error_Msg_N ("\?value is known to be out of range", N);
4185 New_Reference_To (Standard_False, Loc));
4186 Analyze_And_Resolve (N, Rtyp);
4187 Set_Is_Static_Expression (N, Static);
4191 -- If both checks are known to succeed, replace result by True,
4192 -- since we know we are in range.
4194 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4196 Error_Msg_N ("?range test optimized away", N);
4197 Error_Msg_N ("\?value is known to be in range", N);
4201 New_Reference_To (Standard_True, Loc));
4202 Analyze_And_Resolve (N, Rtyp);
4203 Set_Is_Static_Expression (N, Static);
4207 -- If lower bound check succeeds and upper bound check is not
4208 -- known to succeed or fail, then replace the range check with
4209 -- a comparison against the upper bound.
4211 elsif Lcheck in Compare_GE then
4212 if Warn2 and then not In_Instance then
4213 Error_Msg_N ("?lower bound test optimized away", Lo);
4214 Error_Msg_N ("\?value is known to be in range", Lo);
4220 Right_Opnd => High_Bound (Rop)));
4221 Analyze_And_Resolve (N, Rtyp);
4225 -- If upper bound check succeeds and lower bound check is not
4226 -- known to succeed or fail, then replace the range check with
4227 -- a comparison against the lower bound.
4229 elsif Ucheck in Compare_LE then
4230 if Warn2 and then not In_Instance then
4231 Error_Msg_N ("?upper bound test optimized away", Hi);
4232 Error_Msg_N ("\?value is known to be in range", Hi);
4238 Right_Opnd => Low_Bound (Rop)));
4239 Analyze_And_Resolve (N, Rtyp);
4244 -- We couldn't optimize away the range check, but there is one
4245 -- more issue. If we are checking constant conditionals, then we
4246 -- see if we can determine the outcome assuming everything is
4247 -- valid, and if so give an appropriate warning.
4249 if Warn1 and then not Assume_No_Invalid_Values then
4250 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
4251 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
4253 -- Result is out of range for valid value
4255 if Lcheck = LT or else Ucheck = GT then
4257 ("?value can only be in range if it is invalid", N);
4259 -- Result is in range for valid value
4261 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4263 ("?value can only be out of range if it is invalid", N);
4265 -- Lower bound check succeeds if value is valid
4267 elsif Warn2 and then Lcheck in Compare_GE then
4269 ("?lower bound check only fails if it is invalid", Lo);
4271 -- Upper bound check succeeds if value is valid
4273 elsif Warn2 and then Ucheck in Compare_LE then
4275 ("?upper bound check only fails for invalid values", Hi);
4280 -- For all other cases of an explicit range, nothing to be done
4284 -- Here right operand is a subtype mark
4288 Typ : Entity_Id := Etype (Rop);
4289 Is_Acc : constant Boolean := Is_Access_Type (Typ);
4290 Obj : Node_Id := Lop;
4291 Cond : Node_Id := Empty;
4294 Remove_Side_Effects (Obj);
4296 -- For tagged type, do tagged membership operation
4298 if Is_Tagged_Type (Typ) then
4300 -- No expansion will be performed when VM_Target, as the VM
4301 -- back-ends will handle the membership tests directly (tags
4302 -- are not explicitly represented in Java objects, so the
4303 -- normal tagged membership expansion is not what we want).
4305 if Tagged_Type_Expansion then
4306 Rewrite (N, Tagged_Membership (N));
4307 Analyze_And_Resolve (N, Rtyp);
4312 -- If type is scalar type, rewrite as x in t'first .. t'last.
4313 -- This reason we do this is that the bounds may have the wrong
4314 -- type if they come from the original type definition. Also this
4315 -- way we get all the processing above for an explicit range.
4317 elsif Is_Scalar_Type (Typ) then
4321 Make_Attribute_Reference (Loc,
4322 Attribute_Name => Name_First,
4323 Prefix => New_Reference_To (Typ, Loc)),
4326 Make_Attribute_Reference (Loc,
4327 Attribute_Name => Name_Last,
4328 Prefix => New_Reference_To (Typ, Loc))));
4329 Analyze_And_Resolve (N, Rtyp);
4332 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4333 -- a membership test if the subtype mark denotes a constrained
4334 -- Unchecked_Union subtype and the expression lacks inferable
4337 elsif Is_Unchecked_Union (Base_Type (Typ))
4338 and then Is_Constrained (Typ)
4339 and then not Has_Inferable_Discriminants (Lop)
4342 Make_Raise_Program_Error (Loc,
4343 Reason => PE_Unchecked_Union_Restriction));
4345 -- Prevent Gigi from generating incorrect code by rewriting
4346 -- the test as a standard False.
4349 New_Occurrence_Of (Standard_False, Loc));
4354 -- Here we have a non-scalar type
4357 Typ := Designated_Type (Typ);
4360 if not Is_Constrained (Typ) then
4362 New_Reference_To (Standard_True, Loc));
4363 Analyze_And_Resolve (N, Rtyp);
4365 -- For the constrained array case, we have to check the subscripts
4366 -- for an exact match if the lengths are non-zero (the lengths
4367 -- must match in any case).
4369 elsif Is_Array_Type (Typ) then
4371 Check_Subscripts : declare
4372 function Construct_Attribute_Reference
4375 Dim : Nat) return Node_Id;
4376 -- Build attribute reference E'Nam(Dim)
4378 -----------------------------------
4379 -- Construct_Attribute_Reference --
4380 -----------------------------------
4382 function Construct_Attribute_Reference
4385 Dim : Nat) return Node_Id
4389 Make_Attribute_Reference (Loc,
4391 Attribute_Name => Nam,
4392 Expressions => New_List (
4393 Make_Integer_Literal (Loc, Dim)));
4394 end Construct_Attribute_Reference;
4396 -- Start of processing for Check_Subscripts
4399 for J in 1 .. Number_Dimensions (Typ) loop
4400 Evolve_And_Then (Cond,
4403 Construct_Attribute_Reference
4404 (Duplicate_Subexpr_No_Checks (Obj),
4407 Construct_Attribute_Reference
4408 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
4410 Evolve_And_Then (Cond,
4413 Construct_Attribute_Reference
4414 (Duplicate_Subexpr_No_Checks (Obj),
4417 Construct_Attribute_Reference
4418 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
4427 Right_Opnd => Make_Null (Loc)),
4428 Right_Opnd => Cond);
4432 Analyze_And_Resolve (N, Rtyp);
4433 end Check_Subscripts;
4435 -- These are the cases where constraint checks may be required,
4436 -- e.g. records with possible discriminants
4439 -- Expand the test into a series of discriminant comparisons.
4440 -- The expression that is built is the negation of the one that
4441 -- is used for checking discriminant constraints.
4443 Obj := Relocate_Node (Left_Opnd (N));
4445 if Has_Discriminants (Typ) then
4446 Cond := Make_Op_Not (Loc,
4447 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
4450 Cond := Make_Or_Else (Loc,
4454 Right_Opnd => Make_Null (Loc)),
4455 Right_Opnd => Cond);
4459 Cond := New_Occurrence_Of (Standard_True, Loc);
4463 Analyze_And_Resolve (N, Rtyp);
4469 --------------------------------
4470 -- Expand_N_Indexed_Component --
4471 --------------------------------
4473 procedure Expand_N_Indexed_Component (N : Node_Id) is
4474 Loc : constant Source_Ptr := Sloc (N);
4475 Typ : constant Entity_Id := Etype (N);
4476 P : constant Node_Id := Prefix (N);
4477 T : constant Entity_Id := Etype (P);
4480 -- A special optimization, if we have an indexed component that is
4481 -- selecting from a slice, then we can eliminate the slice, since, for
4482 -- example, x (i .. j)(k) is identical to x(k). The only difference is
4483 -- the range check required by the slice. The range check for the slice
4484 -- itself has already been generated. The range check for the
4485 -- subscripting operation is ensured by converting the subject to
4486 -- the subtype of the slice.
4488 -- This optimization not only generates better code, avoiding slice
4489 -- messing especially in the packed case, but more importantly bypasses
4490 -- some problems in handling this peculiar case, for example, the issue
4491 -- of dealing specially with object renamings.
4493 if Nkind (P) = N_Slice then
4495 Make_Indexed_Component (Loc,
4496 Prefix => Prefix (P),
4497 Expressions => New_List (
4499 (Etype (First_Index (Etype (P))),
4500 First (Expressions (N))))));
4501 Analyze_And_Resolve (N, Typ);
4505 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
4506 -- function, then additional actuals must be passed.
4508 if Ada_Version >= Ada_05
4509 and then Is_Build_In_Place_Function_Call (P)
4511 Make_Build_In_Place_Call_In_Anonymous_Context (P);
4514 -- If the prefix is an access type, then we unconditionally rewrite if
4515 -- as an explicit deference. This simplifies processing for several
4516 -- cases, including packed array cases and certain cases in which checks
4517 -- must be generated. We used to try to do this only when it was
4518 -- necessary, but it cleans up the code to do it all the time.
4520 if Is_Access_Type (T) then
4521 Insert_Explicit_Dereference (P);
4522 Analyze_And_Resolve (P, Designated_Type (T));
4525 -- Generate index and validity checks
4527 Generate_Index_Checks (N);
4529 if Validity_Checks_On and then Validity_Check_Subscripts then
4530 Apply_Subscript_Validity_Checks (N);
4533 -- All done for the non-packed case
4535 if not Is_Packed (Etype (Prefix (N))) then
4539 -- For packed arrays that are not bit-packed (i.e. the case of an array
4540 -- with one or more index types with a non-contiguous enumeration type),
4541 -- we can always use the normal packed element get circuit.
4543 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
4544 Expand_Packed_Element_Reference (N);
4548 -- For a reference to a component of a bit packed array, we have to
4549 -- convert it to a reference to the corresponding Packed_Array_Type.
4550 -- We only want to do this for simple references, and not for:
4552 -- Left side of assignment, or prefix of left side of assignment, or
4553 -- prefix of the prefix, to handle packed arrays of packed arrays,
4554 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
4556 -- Renaming objects in renaming associations
4557 -- This case is handled when a use of the renamed variable occurs
4559 -- Actual parameters for a procedure call
4560 -- This case is handled in Exp_Ch6.Expand_Actuals
4562 -- The second expression in a 'Read attribute reference
4564 -- The prefix of an address or size attribute reference
4566 -- The following circuit detects these exceptions
4569 Child : Node_Id := N;
4570 Parnt : Node_Id := Parent (N);
4574 if Nkind (Parnt) = N_Unchecked_Expression then
4577 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
4578 N_Procedure_Call_Statement)
4579 or else (Nkind (Parnt) = N_Parameter_Association
4581 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
4585 elsif Nkind (Parnt) = N_Attribute_Reference
4586 and then (Attribute_Name (Parnt) = Name_Address
4588 Attribute_Name (Parnt) = Name_Size)
4589 and then Prefix (Parnt) = Child
4593 elsif Nkind (Parnt) = N_Assignment_Statement
4594 and then Name (Parnt) = Child
4598 -- If the expression is an index of an indexed component, it must
4599 -- be expanded regardless of context.
4601 elsif Nkind (Parnt) = N_Indexed_Component
4602 and then Child /= Prefix (Parnt)
4604 Expand_Packed_Element_Reference (N);
4607 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
4608 and then Name (Parent (Parnt)) = Parnt
4612 elsif Nkind (Parnt) = N_Attribute_Reference
4613 and then Attribute_Name (Parnt) = Name_Read
4614 and then Next (First (Expressions (Parnt))) = Child
4618 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
4619 and then Prefix (Parnt) = Child
4624 Expand_Packed_Element_Reference (N);
4628 -- Keep looking up tree for unchecked expression, or if we are the
4629 -- prefix of a possible assignment left side.
4632 Parnt := Parent (Child);
4635 end Expand_N_Indexed_Component;
4637 ---------------------
4638 -- Expand_N_Not_In --
4639 ---------------------
4641 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
4642 -- can be done. This avoids needing to duplicate this expansion code.
4644 procedure Expand_N_Not_In (N : Node_Id) is
4645 Loc : constant Source_Ptr := Sloc (N);
4646 Typ : constant Entity_Id := Etype (N);
4647 Cfs : constant Boolean := Comes_From_Source (N);
4654 Left_Opnd => Left_Opnd (N),
4655 Right_Opnd => Right_Opnd (N))));
4657 -- We want this to appear as coming from source if original does (see
4658 -- transformations in Expand_N_In).
4660 Set_Comes_From_Source (N, Cfs);
4661 Set_Comes_From_Source (Right_Opnd (N), Cfs);
4663 -- Now analyze transformed node
4665 Analyze_And_Resolve (N, Typ);
4666 end Expand_N_Not_In;
4672 -- The only replacement required is for the case of a null of type that is
4673 -- an access to protected subprogram. We represent such access values as a
4674 -- record, and so we must replace the occurrence of null by the equivalent
4675 -- record (with a null address and a null pointer in it), so that the
4676 -- backend creates the proper value.
4678 procedure Expand_N_Null (N : Node_Id) is
4679 Loc : constant Source_Ptr := Sloc (N);
4680 Typ : constant Entity_Id := Etype (N);
4684 if Is_Access_Protected_Subprogram_Type (Typ) then
4686 Make_Aggregate (Loc,
4687 Expressions => New_List (
4688 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
4692 Analyze_And_Resolve (N, Equivalent_Type (Typ));
4694 -- For subsequent semantic analysis, the node must retain its type.
4695 -- Gigi in any case replaces this type by the corresponding record
4696 -- type before processing the node.
4702 when RE_Not_Available =>
4706 ---------------------
4707 -- Expand_N_Op_Abs --
4708 ---------------------
4710 procedure Expand_N_Op_Abs (N : Node_Id) is
4711 Loc : constant Source_Ptr := Sloc (N);
4712 Expr : constant Node_Id := Right_Opnd (N);
4715 Unary_Op_Validity_Checks (N);
4717 -- Deal with software overflow checking
4719 if not Backend_Overflow_Checks_On_Target
4720 and then Is_Signed_Integer_Type (Etype (N))
4721 and then Do_Overflow_Check (N)
4723 -- The only case to worry about is when the argument is equal to the
4724 -- largest negative number, so what we do is to insert the check:
4726 -- [constraint_error when Expr = typ'Base'First]
4728 -- with the usual Duplicate_Subexpr use coding for expr
4731 Make_Raise_Constraint_Error (Loc,
4734 Left_Opnd => Duplicate_Subexpr (Expr),
4736 Make_Attribute_Reference (Loc,
4738 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
4739 Attribute_Name => Name_First)),
4740 Reason => CE_Overflow_Check_Failed));
4743 -- Vax floating-point types case
4745 if Vax_Float (Etype (N)) then
4746 Expand_Vax_Arith (N);
4748 end Expand_N_Op_Abs;
4750 ---------------------
4751 -- Expand_N_Op_Add --
4752 ---------------------
4754 procedure Expand_N_Op_Add (N : Node_Id) is
4755 Typ : constant Entity_Id := Etype (N);
4758 Binary_Op_Validity_Checks (N);
4760 -- N + 0 = 0 + N = N for integer types
4762 if Is_Integer_Type (Typ) then
4763 if Compile_Time_Known_Value (Right_Opnd (N))
4764 and then Expr_Value (Right_Opnd (N)) = Uint_0
4766 Rewrite (N, Left_Opnd (N));
4769 elsif Compile_Time_Known_Value (Left_Opnd (N))
4770 and then Expr_Value (Left_Opnd (N)) = Uint_0
4772 Rewrite (N, Right_Opnd (N));
4777 -- Arithmetic overflow checks for signed integer/fixed point types
4779 if Is_Signed_Integer_Type (Typ)
4780 or else Is_Fixed_Point_Type (Typ)
4782 Apply_Arithmetic_Overflow_Check (N);
4785 -- Vax floating-point types case
4787 elsif Vax_Float (Typ) then
4788 Expand_Vax_Arith (N);
4790 end Expand_N_Op_Add;
4792 ---------------------
4793 -- Expand_N_Op_And --
4794 ---------------------
4796 procedure Expand_N_Op_And (N : Node_Id) is
4797 Typ : constant Entity_Id := Etype (N);
4800 Binary_Op_Validity_Checks (N);
4802 if Is_Array_Type (Etype (N)) then
4803 Expand_Boolean_Operator (N);
4805 elsif Is_Boolean_Type (Etype (N)) then
4806 Adjust_Condition (Left_Opnd (N));
4807 Adjust_Condition (Right_Opnd (N));
4808 Set_Etype (N, Standard_Boolean);
4809 Adjust_Result_Type (N, Typ);
4811 end Expand_N_Op_And;
4813 ------------------------
4814 -- Expand_N_Op_Concat --
4815 ------------------------
4817 procedure Expand_N_Op_Concat (N : Node_Id) is
4819 -- List of operands to be concatenated
4822 -- Node which is to be replaced by the result of concatenating the nodes
4823 -- in the list Opnds.
4826 -- Ensure validity of both operands
4828 Binary_Op_Validity_Checks (N);
4830 -- If we are the left operand of a concatenation higher up the tree,
4831 -- then do nothing for now, since we want to deal with a series of
4832 -- concatenations as a unit.
4834 if Nkind (Parent (N)) = N_Op_Concat
4835 and then N = Left_Opnd (Parent (N))
4840 -- We get here with a concatenation whose left operand may be a
4841 -- concatenation itself with a consistent type. We need to process
4842 -- these concatenation operands from left to right, which means
4843 -- from the deepest node in the tree to the highest node.
4846 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
4847 Cnode := Left_Opnd (Cnode);
4850 -- Now Opnd is the deepest Opnd, and its parents are the concatenation
4851 -- nodes above, so now we process bottom up, doing the operations. We
4852 -- gather a string that is as long as possible up to five operands
4854 -- The outer loop runs more than once if more than one concatenation
4855 -- type is involved.
4858 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
4859 Set_Parent (Opnds, N);
4861 -- The inner loop gathers concatenation operands
4863 Inner : while Cnode /= N
4864 and then Base_Type (Etype (Cnode)) =
4865 Base_Type (Etype (Parent (Cnode)))
4867 Cnode := Parent (Cnode);
4868 Append (Right_Opnd (Cnode), Opnds);
4871 Expand_Concatenate (Cnode, Opnds);
4873 exit Outer when Cnode = N;
4874 Cnode := Parent (Cnode);
4876 end Expand_N_Op_Concat;
4878 ------------------------
4879 -- Expand_N_Op_Divide --
4880 ------------------------
4882 procedure Expand_N_Op_Divide (N : Node_Id) is
4883 Loc : constant Source_Ptr := Sloc (N);
4884 Lopnd : constant Node_Id := Left_Opnd (N);
4885 Ropnd : constant Node_Id := Right_Opnd (N);
4886 Ltyp : constant Entity_Id := Etype (Lopnd);
4887 Rtyp : constant Entity_Id := Etype (Ropnd);
4888 Typ : Entity_Id := Etype (N);
4889 Rknow : constant Boolean := Is_Integer_Type (Typ)
4891 Compile_Time_Known_Value (Ropnd);
4895 Binary_Op_Validity_Checks (N);
4898 Rval := Expr_Value (Ropnd);
4901 -- N / 1 = N for integer types
4903 if Rknow and then Rval = Uint_1 then
4908 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
4909 -- Is_Power_Of_2_For_Shift is set means that we know that our left
4910 -- operand is an unsigned integer, as required for this to work.
4912 if Nkind (Ropnd) = N_Op_Expon
4913 and then Is_Power_Of_2_For_Shift (Ropnd)
4915 -- We cannot do this transformation in configurable run time mode if we
4916 -- have 64-bit -- integers and long shifts are not available.
4920 or else Support_Long_Shifts_On_Target)
4923 Make_Op_Shift_Right (Loc,
4926 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
4927 Analyze_And_Resolve (N, Typ);
4931 -- Do required fixup of universal fixed operation
4933 if Typ = Universal_Fixed then
4934 Fixup_Universal_Fixed_Operation (N);
4938 -- Divisions with fixed-point results
4940 if Is_Fixed_Point_Type (Typ) then
4942 -- No special processing if Treat_Fixed_As_Integer is set, since
4943 -- from a semantic point of view such operations are simply integer
4944 -- operations and will be treated that way.
4946 if not Treat_Fixed_As_Integer (N) then
4947 if Is_Integer_Type (Rtyp) then
4948 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
4950 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
4954 -- Other cases of division of fixed-point operands. Again we exclude the
4955 -- case where Treat_Fixed_As_Integer is set.
4957 elsif (Is_Fixed_Point_Type (Ltyp) or else
4958 Is_Fixed_Point_Type (Rtyp))
4959 and then not Treat_Fixed_As_Integer (N)
4961 if Is_Integer_Type (Typ) then
4962 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
4964 pragma Assert (Is_Floating_Point_Type (Typ));
4965 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
4968 -- Mixed-mode operations can appear in a non-static universal context,
4969 -- in which case the integer argument must be converted explicitly.
4971 elsif Typ = Universal_Real
4972 and then Is_Integer_Type (Rtyp)
4975 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
4977 Analyze_And_Resolve (Ropnd, Universal_Real);
4979 elsif Typ = Universal_Real
4980 and then Is_Integer_Type (Ltyp)
4983 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
4985 Analyze_And_Resolve (Lopnd, Universal_Real);
4987 -- Non-fixed point cases, do integer zero divide and overflow checks
4989 elsif Is_Integer_Type (Typ) then
4990 Apply_Divide_Check (N);
4992 -- Check for 64-bit division available, or long shifts if the divisor
4993 -- is a small power of 2 (since such divides will be converted into
4996 if Esize (Ltyp) > 32
4997 and then not Support_64_Bit_Divides_On_Target
5000 or else not Support_Long_Shifts_On_Target
5001 or else (Rval /= Uint_2 and then
5002 Rval /= Uint_4 and then
5003 Rval /= Uint_8 and then
5004 Rval /= Uint_16 and then
5005 Rval /= Uint_32 and then
5008 Error_Msg_CRT ("64-bit division", N);
5011 -- Deal with Vax_Float
5013 elsif Vax_Float (Typ) then
5014 Expand_Vax_Arith (N);
5017 end Expand_N_Op_Divide;
5019 --------------------
5020 -- Expand_N_Op_Eq --
5021 --------------------
5023 procedure Expand_N_Op_Eq (N : Node_Id) is
5024 Loc : constant Source_Ptr := Sloc (N);
5025 Typ : constant Entity_Id := Etype (N);
5026 Lhs : constant Node_Id := Left_Opnd (N);
5027 Rhs : constant Node_Id := Right_Opnd (N);
5028 Bodies : constant List_Id := New_List;
5029 A_Typ : constant Entity_Id := Etype (Lhs);
5031 Typl : Entity_Id := A_Typ;
5032 Op_Name : Entity_Id;
5035 procedure Build_Equality_Call (Eq : Entity_Id);
5036 -- If a constructed equality exists for the type or for its parent,
5037 -- build and analyze call, adding conversions if the operation is
5040 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
5041 -- Determines whether a type has a subcomponent of an unconstrained
5042 -- Unchecked_Union subtype. Typ is a record type.
5044 -------------------------
5045 -- Build_Equality_Call --
5046 -------------------------
5048 procedure Build_Equality_Call (Eq : Entity_Id) is
5049 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
5050 L_Exp : Node_Id := Relocate_Node (Lhs);
5051 R_Exp : Node_Id := Relocate_Node (Rhs);
5054 if Base_Type (Op_Type) /= Base_Type (A_Typ)
5055 and then not Is_Class_Wide_Type (A_Typ)
5057 L_Exp := OK_Convert_To (Op_Type, L_Exp);
5058 R_Exp := OK_Convert_To (Op_Type, R_Exp);
5061 -- If we have an Unchecked_Union, we need to add the inferred
5062 -- discriminant values as actuals in the function call. At this
5063 -- point, the expansion has determined that both operands have
5064 -- inferable discriminants.
5066 if Is_Unchecked_Union (Op_Type) then
5068 Lhs_Type : constant Node_Id := Etype (L_Exp);
5069 Rhs_Type : constant Node_Id := Etype (R_Exp);
5070 Lhs_Discr_Val : Node_Id;
5071 Rhs_Discr_Val : Node_Id;
5074 -- Per-object constrained selected components require special
5075 -- attention. If the enclosing scope of the component is an
5076 -- Unchecked_Union, we cannot reference its discriminants
5077 -- directly. This is why we use the two extra parameters of
5078 -- the equality function of the enclosing Unchecked_Union.
5080 -- type UU_Type (Discr : Integer := 0) is
5083 -- pragma Unchecked_Union (UU_Type);
5085 -- 1. Unchecked_Union enclosing record:
5087 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
5089 -- Comp : UU_Type (Discr);
5091 -- end Enclosing_UU_Type;
5092 -- pragma Unchecked_Union (Enclosing_UU_Type);
5094 -- Obj1 : Enclosing_UU_Type;
5095 -- Obj2 : Enclosing_UU_Type (1);
5097 -- [. . .] Obj1 = Obj2 [. . .]
5101 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
5103 -- A and B are the formal parameters of the equality function
5104 -- of Enclosing_UU_Type. The function always has two extra
5105 -- formals to capture the inferred discriminant values.
5107 -- 2. Non-Unchecked_Union enclosing record:
5110 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
5113 -- Comp : UU_Type (Discr);
5115 -- end Enclosing_Non_UU_Type;
5117 -- Obj1 : Enclosing_Non_UU_Type;
5118 -- Obj2 : Enclosing_Non_UU_Type (1);
5120 -- ... Obj1 = Obj2 ...
5124 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
5125 -- obj1.discr, obj2.discr)) then
5127 -- In this case we can directly reference the discriminants of
5128 -- the enclosing record.
5132 if Nkind (Lhs) = N_Selected_Component
5133 and then Has_Per_Object_Constraint
5134 (Entity (Selector_Name (Lhs)))
5136 -- Enclosing record is an Unchecked_Union, use formal A
5138 if Is_Unchecked_Union (Scope
5139 (Entity (Selector_Name (Lhs))))
5142 Make_Identifier (Loc,
5145 -- Enclosing record is of a non-Unchecked_Union type, it is
5146 -- possible to reference the discriminant.
5150 Make_Selected_Component (Loc,
5151 Prefix => Prefix (Lhs),
5154 (Get_Discriminant_Value
5155 (First_Discriminant (Lhs_Type),
5157 Stored_Constraint (Lhs_Type))));
5160 -- Comment needed here ???
5163 -- Infer the discriminant value
5167 (Get_Discriminant_Value
5168 (First_Discriminant (Lhs_Type),
5170 Stored_Constraint (Lhs_Type)));
5175 if Nkind (Rhs) = N_Selected_Component
5176 and then Has_Per_Object_Constraint
5177 (Entity (Selector_Name (Rhs)))
5179 if Is_Unchecked_Union
5180 (Scope (Entity (Selector_Name (Rhs))))
5183 Make_Identifier (Loc,
5188 Make_Selected_Component (Loc,
5189 Prefix => Prefix (Rhs),
5191 New_Copy (Get_Discriminant_Value (
5192 First_Discriminant (Rhs_Type),
5194 Stored_Constraint (Rhs_Type))));
5199 New_Copy (Get_Discriminant_Value (
5200 First_Discriminant (Rhs_Type),
5202 Stored_Constraint (Rhs_Type)));
5207 Make_Function_Call (Loc,
5208 Name => New_Reference_To (Eq, Loc),
5209 Parameter_Associations => New_List (
5216 -- Normal case, not an unchecked union
5220 Make_Function_Call (Loc,
5221 Name => New_Reference_To (Eq, Loc),
5222 Parameter_Associations => New_List (L_Exp, R_Exp)));
5225 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5226 end Build_Equality_Call;
5228 ------------------------------------
5229 -- Has_Unconstrained_UU_Component --
5230 ------------------------------------
5232 function Has_Unconstrained_UU_Component
5233 (Typ : Node_Id) return Boolean
5235 Tdef : constant Node_Id :=
5236 Type_Definition (Declaration_Node (Base_Type (Typ)));
5240 function Component_Is_Unconstrained_UU
5241 (Comp : Node_Id) return Boolean;
5242 -- Determines whether the subtype of the component is an
5243 -- unconstrained Unchecked_Union.
5245 function Variant_Is_Unconstrained_UU
5246 (Variant : Node_Id) return Boolean;
5247 -- Determines whether a component of the variant has an unconstrained
5248 -- Unchecked_Union subtype.
5250 -----------------------------------
5251 -- Component_Is_Unconstrained_UU --
5252 -----------------------------------
5254 function Component_Is_Unconstrained_UU
5255 (Comp : Node_Id) return Boolean
5258 if Nkind (Comp) /= N_Component_Declaration then
5263 Sindic : constant Node_Id :=
5264 Subtype_Indication (Component_Definition (Comp));
5267 -- Unconstrained nominal type. In the case of a constraint
5268 -- present, the node kind would have been N_Subtype_Indication.
5270 if Nkind (Sindic) = N_Identifier then
5271 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
5276 end Component_Is_Unconstrained_UU;
5278 ---------------------------------
5279 -- Variant_Is_Unconstrained_UU --
5280 ---------------------------------
5282 function Variant_Is_Unconstrained_UU
5283 (Variant : Node_Id) return Boolean
5285 Clist : constant Node_Id := Component_List (Variant);
5288 if Is_Empty_List (Component_Items (Clist)) then
5292 -- We only need to test one component
5295 Comp : Node_Id := First (Component_Items (Clist));
5298 while Present (Comp) loop
5299 if Component_Is_Unconstrained_UU (Comp) then
5307 -- None of the components withing the variant were of
5308 -- unconstrained Unchecked_Union type.
5311 end Variant_Is_Unconstrained_UU;
5313 -- Start of processing for Has_Unconstrained_UU_Component
5316 if Null_Present (Tdef) then
5320 Clist := Component_List (Tdef);
5321 Vpart := Variant_Part (Clist);
5323 -- Inspect available components
5325 if Present (Component_Items (Clist)) then
5327 Comp : Node_Id := First (Component_Items (Clist));
5330 while Present (Comp) loop
5332 -- One component is sufficient
5334 if Component_Is_Unconstrained_UU (Comp) then
5343 -- Inspect available components withing variants
5345 if Present (Vpart) then
5347 Variant : Node_Id := First (Variants (Vpart));
5350 while Present (Variant) loop
5352 -- One component within a variant is sufficient
5354 if Variant_Is_Unconstrained_UU (Variant) then
5363 -- Neither the available components, nor the components inside the
5364 -- variant parts were of an unconstrained Unchecked_Union subtype.
5367 end Has_Unconstrained_UU_Component;
5369 -- Start of processing for Expand_N_Op_Eq
5372 Binary_Op_Validity_Checks (N);
5374 if Ekind (Typl) = E_Private_Type then
5375 Typl := Underlying_Type (Typl);
5376 elsif Ekind (Typl) = E_Private_Subtype then
5377 Typl := Underlying_Type (Base_Type (Typl));
5382 -- It may happen in error situations that the underlying type is not
5383 -- set. The error will be detected later, here we just defend the
5390 Typl := Base_Type (Typl);
5392 -- Boolean types (requiring handling of non-standard case)
5394 if Is_Boolean_Type (Typl) then
5395 Adjust_Condition (Left_Opnd (N));
5396 Adjust_Condition (Right_Opnd (N));
5397 Set_Etype (N, Standard_Boolean);
5398 Adjust_Result_Type (N, Typ);
5402 elsif Is_Array_Type (Typl) then
5404 -- If we are doing full validity checking, and it is possible for the
5405 -- array elements to be invalid then expand out array comparisons to
5406 -- make sure that we check the array elements.
5408 if Validity_Check_Operands
5409 and then not Is_Known_Valid (Component_Type (Typl))
5412 Save_Force_Validity_Checks : constant Boolean :=
5413 Force_Validity_Checks;
5415 Force_Validity_Checks := True;
5417 Expand_Array_Equality
5419 Relocate_Node (Lhs),
5420 Relocate_Node (Rhs),
5423 Insert_Actions (N, Bodies);
5424 Analyze_And_Resolve (N, Standard_Boolean);
5425 Force_Validity_Checks := Save_Force_Validity_Checks;
5428 -- Packed case where both operands are known aligned
5430 elsif Is_Bit_Packed_Array (Typl)
5431 and then not Is_Possibly_Unaligned_Object (Lhs)
5432 and then not Is_Possibly_Unaligned_Object (Rhs)
5434 Expand_Packed_Eq (N);
5436 -- Where the component type is elementary we can use a block bit
5437 -- comparison (if supported on the target) exception in the case
5438 -- of floating-point (negative zero issues require element by
5439 -- element comparison), and atomic types (where we must be sure
5440 -- to load elements independently) and possibly unaligned arrays.
5442 elsif Is_Elementary_Type (Component_Type (Typl))
5443 and then not Is_Floating_Point_Type (Component_Type (Typl))
5444 and then not Is_Atomic (Component_Type (Typl))
5445 and then not Is_Possibly_Unaligned_Object (Lhs)
5446 and then not Is_Possibly_Unaligned_Object (Rhs)
5447 and then Support_Composite_Compare_On_Target
5451 -- For composite and floating-point cases, expand equality loop to
5452 -- make sure of using proper comparisons for tagged types, and
5453 -- correctly handling the floating-point case.
5457 Expand_Array_Equality
5459 Relocate_Node (Lhs),
5460 Relocate_Node (Rhs),
5463 Insert_Actions (N, Bodies, Suppress => All_Checks);
5464 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5469 elsif Is_Record_Type (Typl) then
5471 -- For tagged types, use the primitive "="
5473 if Is_Tagged_Type (Typl) then
5475 -- No need to do anything else compiling under restriction
5476 -- No_Dispatching_Calls. During the semantic analysis we
5477 -- already notified such violation.
5479 if Restriction_Active (No_Dispatching_Calls) then
5483 -- If this is derived from an untagged private type completed with
5484 -- a tagged type, it does not have a full view, so we use the
5485 -- primitive operations of the private type. This check should no
5486 -- longer be necessary when these types get their full views???
5488 if Is_Private_Type (A_Typ)
5489 and then not Is_Tagged_Type (A_Typ)
5490 and then Is_Derived_Type (A_Typ)
5491 and then No (Full_View (A_Typ))
5493 -- Search for equality operation, checking that the operands
5494 -- have the same type. Note that we must find a matching entry,
5495 -- or something is very wrong!
5497 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
5499 while Present (Prim) loop
5500 exit when Chars (Node (Prim)) = Name_Op_Eq
5501 and then Etype (First_Formal (Node (Prim))) =
5502 Etype (Next_Formal (First_Formal (Node (Prim))))
5504 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5509 pragma Assert (Present (Prim));
5510 Op_Name := Node (Prim);
5512 -- Find the type's predefined equality or an overriding
5513 -- user- defined equality. The reason for not simply calling
5514 -- Find_Prim_Op here is that there may be a user-defined
5515 -- overloaded equality op that precedes the equality that we want,
5516 -- so we have to explicitly search (e.g., there could be an
5517 -- equality with two different parameter types).
5520 if Is_Class_Wide_Type (Typl) then
5521 Typl := Root_Type (Typl);
5524 Prim := First_Elmt (Primitive_Operations (Typl));
5525 while Present (Prim) loop
5526 exit when Chars (Node (Prim)) = Name_Op_Eq
5527 and then Etype (First_Formal (Node (Prim))) =
5528 Etype (Next_Formal (First_Formal (Node (Prim))))
5530 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5535 pragma Assert (Present (Prim));
5536 Op_Name := Node (Prim);
5539 Build_Equality_Call (Op_Name);
5541 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
5542 -- predefined equality operator for a type which has a subcomponent
5543 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
5545 elsif Has_Unconstrained_UU_Component (Typl) then
5547 Make_Raise_Program_Error (Loc,
5548 Reason => PE_Unchecked_Union_Restriction));
5550 -- Prevent Gigi from generating incorrect code by rewriting the
5551 -- equality as a standard False.
5554 New_Occurrence_Of (Standard_False, Loc));
5556 elsif Is_Unchecked_Union (Typl) then
5558 -- If we can infer the discriminants of the operands, we make a
5559 -- call to the TSS equality function.
5561 if Has_Inferable_Discriminants (Lhs)
5563 Has_Inferable_Discriminants (Rhs)
5566 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5569 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5570 -- the predefined equality operator for an Unchecked_Union type
5571 -- if either of the operands lack inferable discriminants.
5574 Make_Raise_Program_Error (Loc,
5575 Reason => PE_Unchecked_Union_Restriction));
5577 -- Prevent Gigi from generating incorrect code by rewriting
5578 -- the equality as a standard False.
5581 New_Occurrence_Of (Standard_False, Loc));
5585 -- If a type support function is present (for complex cases), use it
5587 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
5589 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5591 -- Otherwise expand the component by component equality. Note that
5592 -- we never use block-bit comparisons for records, because of the
5593 -- problems with gaps. The backend will often be able to recombine
5594 -- the separate comparisons that we generate here.
5597 Remove_Side_Effects (Lhs);
5598 Remove_Side_Effects (Rhs);
5600 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
5602 Insert_Actions (N, Bodies, Suppress => All_Checks);
5603 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5607 -- Test if result is known at compile time
5609 Rewrite_Comparison (N);
5611 -- If we still have comparison for Vax_Float, process it
5613 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
5614 Expand_Vax_Comparison (N);
5619 -----------------------
5620 -- Expand_N_Op_Expon --
5621 -----------------------
5623 procedure Expand_N_Op_Expon (N : Node_Id) is
5624 Loc : constant Source_Ptr := Sloc (N);
5625 Typ : constant Entity_Id := Etype (N);
5626 Rtyp : constant Entity_Id := Root_Type (Typ);
5627 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
5628 Bastyp : constant Node_Id := Etype (Base);
5629 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
5630 Exptyp : constant Entity_Id := Etype (Exp);
5631 Ovflo : constant Boolean := Do_Overflow_Check (N);
5640 Binary_Op_Validity_Checks (N);
5642 -- If either operand is of a private type, then we have the use of an
5643 -- intrinsic operator, and we get rid of the privateness, by using root
5644 -- types of underlying types for the actual operation. Otherwise the
5645 -- private types will cause trouble if we expand multiplications or
5646 -- shifts etc. We also do this transformation if the result type is
5647 -- different from the base type.
5649 if Is_Private_Type (Etype (Base))
5651 Is_Private_Type (Typ)
5653 Is_Private_Type (Exptyp)
5655 Rtyp /= Root_Type (Bastyp)
5658 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
5659 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
5663 Unchecked_Convert_To (Typ,
5665 Left_Opnd => Unchecked_Convert_To (Bt, Base),
5666 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
5667 Analyze_And_Resolve (N, Typ);
5672 -- Test for case of known right argument
5674 if Compile_Time_Known_Value (Exp) then
5675 Expv := Expr_Value (Exp);
5677 -- We only fold small non-negative exponents. You might think we
5678 -- could fold small negative exponents for the real case, but we
5679 -- can't because we are required to raise Constraint_Error for
5680 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
5681 -- See ACVC test C4A012B.
5683 if Expv >= 0 and then Expv <= 4 then
5685 -- X ** 0 = 1 (or 1.0)
5689 -- Call Remove_Side_Effects to ensure that any side effects
5690 -- in the ignored left operand (in particular function calls
5691 -- to user defined functions) are properly executed.
5693 Remove_Side_Effects (Base);
5695 if Ekind (Typ) in Integer_Kind then
5696 Xnode := Make_Integer_Literal (Loc, Intval => 1);
5698 Xnode := Make_Real_Literal (Loc, Ureal_1);
5710 Make_Op_Multiply (Loc,
5711 Left_Opnd => Duplicate_Subexpr (Base),
5712 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
5714 -- X ** 3 = X * X * X
5718 Make_Op_Multiply (Loc,
5720 Make_Op_Multiply (Loc,
5721 Left_Opnd => Duplicate_Subexpr (Base),
5722 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
5723 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
5726 -- En : constant base'type := base * base;
5732 Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
5734 Insert_Actions (N, New_List (
5735 Make_Object_Declaration (Loc,
5736 Defining_Identifier => Temp,
5737 Constant_Present => True,
5738 Object_Definition => New_Reference_To (Typ, Loc),
5740 Make_Op_Multiply (Loc,
5741 Left_Opnd => Duplicate_Subexpr (Base),
5742 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
5745 Make_Op_Multiply (Loc,
5746 Left_Opnd => New_Reference_To (Temp, Loc),
5747 Right_Opnd => New_Reference_To (Temp, Loc));
5751 Analyze_And_Resolve (N, Typ);
5756 -- Case of (2 ** expression) appearing as an argument of an integer
5757 -- multiplication, or as the right argument of a division of a non-
5758 -- negative integer. In such cases we leave the node untouched, setting
5759 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
5760 -- of the higher level node converts it into a shift.
5762 -- Note: this transformation is not applicable for a modular type with
5763 -- a non-binary modulus in the multiplication case, since we get a wrong
5764 -- result if the shift causes an overflow before the modular reduction.
5766 if Nkind (Base) = N_Integer_Literal
5767 and then Intval (Base) = 2
5768 and then Is_Integer_Type (Root_Type (Exptyp))
5769 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
5770 and then Is_Unsigned_Type (Exptyp)
5772 and then Nkind (Parent (N)) in N_Binary_Op
5775 P : constant Node_Id := Parent (N);
5776 L : constant Node_Id := Left_Opnd (P);
5777 R : constant Node_Id := Right_Opnd (P);
5780 if (Nkind (P) = N_Op_Multiply
5781 and then not Non_Binary_Modulus (Typ)
5783 ((Is_Integer_Type (Etype (L)) and then R = N)
5785 (Is_Integer_Type (Etype (R)) and then L = N))
5786 and then not Do_Overflow_Check (P))
5789 (Nkind (P) = N_Op_Divide
5790 and then Is_Integer_Type (Etype (L))
5791 and then Is_Unsigned_Type (Etype (L))
5793 and then not Do_Overflow_Check (P))
5795 Set_Is_Power_Of_2_For_Shift (N);
5801 -- Fall through if exponentiation must be done using a runtime routine
5803 -- First deal with modular case
5805 if Is_Modular_Integer_Type (Rtyp) then
5807 -- Non-binary case, we call the special exponentiation routine for
5808 -- the non-binary case, converting the argument to Long_Long_Integer
5809 -- and passing the modulus value. Then the result is converted back
5810 -- to the base type.
5812 if Non_Binary_Modulus (Rtyp) then
5815 Make_Function_Call (Loc,
5816 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
5817 Parameter_Associations => New_List (
5818 Convert_To (Standard_Integer, Base),
5819 Make_Integer_Literal (Loc, Modulus (Rtyp)),
5822 -- Binary case, in this case, we call one of two routines, either the
5823 -- unsigned integer case, or the unsigned long long integer case,
5824 -- with a final "and" operation to do the required mod.
5827 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
5828 Ent := RTE (RE_Exp_Unsigned);
5830 Ent := RTE (RE_Exp_Long_Long_Unsigned);
5837 Make_Function_Call (Loc,
5838 Name => New_Reference_To (Ent, Loc),
5839 Parameter_Associations => New_List (
5840 Convert_To (Etype (First_Formal (Ent)), Base),
5843 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
5847 -- Common exit point for modular type case
5849 Analyze_And_Resolve (N, Typ);
5852 -- Signed integer cases, done using either Integer or Long_Long_Integer.
5853 -- It is not worth having routines for Short_[Short_]Integer, since for
5854 -- most machines it would not help, and it would generate more code that
5855 -- might need certification when a certified run time is required.
5857 -- In the integer cases, we have two routines, one for when overflow
5858 -- checks are required, and one when they are not required, since there
5859 -- is a real gain in omitting checks on many machines.
5861 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
5862 or else (Rtyp = Base_Type (Standard_Long_Integer)
5864 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
5865 or else (Rtyp = Universal_Integer)
5867 Etyp := Standard_Long_Long_Integer;
5870 Rent := RE_Exp_Long_Long_Integer;
5872 Rent := RE_Exn_Long_Long_Integer;
5875 elsif Is_Signed_Integer_Type (Rtyp) then
5876 Etyp := Standard_Integer;
5879 Rent := RE_Exp_Integer;
5881 Rent := RE_Exn_Integer;
5884 -- Floating-point cases, always done using Long_Long_Float. We do not
5885 -- need separate routines for the overflow case here, since in the case
5886 -- of floating-point, we generate infinities anyway as a rule (either
5887 -- that or we automatically trap overflow), and if there is an infinity
5888 -- generated and a range check is required, the check will fail anyway.
5891 pragma Assert (Is_Floating_Point_Type (Rtyp));
5892 Etyp := Standard_Long_Long_Float;
5893 Rent := RE_Exn_Long_Long_Float;
5896 -- Common processing for integer cases and floating-point cases.
5897 -- If we are in the right type, we can call runtime routine directly
5900 and then Rtyp /= Universal_Integer
5901 and then Rtyp /= Universal_Real
5904 Make_Function_Call (Loc,
5905 Name => New_Reference_To (RTE (Rent), Loc),
5906 Parameter_Associations => New_List (Base, Exp)));
5908 -- Otherwise we have to introduce conversions (conversions are also
5909 -- required in the universal cases, since the runtime routine is
5910 -- typed using one of the standard types).
5915 Make_Function_Call (Loc,
5916 Name => New_Reference_To (RTE (Rent), Loc),
5917 Parameter_Associations => New_List (
5918 Convert_To (Etyp, Base),
5922 Analyze_And_Resolve (N, Typ);
5926 when RE_Not_Available =>
5928 end Expand_N_Op_Expon;
5930 --------------------
5931 -- Expand_N_Op_Ge --
5932 --------------------
5934 procedure Expand_N_Op_Ge (N : Node_Id) is
5935 Typ : constant Entity_Id := Etype (N);
5936 Op1 : constant Node_Id := Left_Opnd (N);
5937 Op2 : constant Node_Id := Right_Opnd (N);
5938 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
5941 Binary_Op_Validity_Checks (N);
5943 if Is_Array_Type (Typ1) then
5944 Expand_Array_Comparison (N);
5948 if Is_Boolean_Type (Typ1) then
5949 Adjust_Condition (Op1);
5950 Adjust_Condition (Op2);
5951 Set_Etype (N, Standard_Boolean);
5952 Adjust_Result_Type (N, Typ);
5955 Rewrite_Comparison (N);
5957 -- If we still have comparison, and Vax_Float type, process it
5959 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
5960 Expand_Vax_Comparison (N);
5965 --------------------
5966 -- Expand_N_Op_Gt --
5967 --------------------
5969 procedure Expand_N_Op_Gt (N : Node_Id) is
5970 Typ : constant Entity_Id := Etype (N);
5971 Op1 : constant Node_Id := Left_Opnd (N);
5972 Op2 : constant Node_Id := Right_Opnd (N);
5973 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
5976 Binary_Op_Validity_Checks (N);
5978 if Is_Array_Type (Typ1) then
5979 Expand_Array_Comparison (N);
5983 if Is_Boolean_Type (Typ1) then
5984 Adjust_Condition (Op1);
5985 Adjust_Condition (Op2);
5986 Set_Etype (N, Standard_Boolean);
5987 Adjust_Result_Type (N, Typ);
5990 Rewrite_Comparison (N);
5992 -- If we still have comparison, and Vax_Float type, process it
5994 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
5995 Expand_Vax_Comparison (N);
6000 --------------------
6001 -- Expand_N_Op_Le --
6002 --------------------
6004 procedure Expand_N_Op_Le (N : Node_Id) is
6005 Typ : constant Entity_Id := Etype (N);
6006 Op1 : constant Node_Id := Left_Opnd (N);
6007 Op2 : constant Node_Id := Right_Opnd (N);
6008 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6011 Binary_Op_Validity_Checks (N);
6013 if Is_Array_Type (Typ1) then
6014 Expand_Array_Comparison (N);
6018 if Is_Boolean_Type (Typ1) then
6019 Adjust_Condition (Op1);
6020 Adjust_Condition (Op2);
6021 Set_Etype (N, Standard_Boolean);
6022 Adjust_Result_Type (N, Typ);
6025 Rewrite_Comparison (N);
6027 -- If we still have comparison, and Vax_Float type, process it
6029 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6030 Expand_Vax_Comparison (N);
6035 --------------------
6036 -- Expand_N_Op_Lt --
6037 --------------------
6039 procedure Expand_N_Op_Lt (N : Node_Id) is
6040 Typ : constant Entity_Id := Etype (N);
6041 Op1 : constant Node_Id := Left_Opnd (N);
6042 Op2 : constant Node_Id := Right_Opnd (N);
6043 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6046 Binary_Op_Validity_Checks (N);
6048 if Is_Array_Type (Typ1) then
6049 Expand_Array_Comparison (N);
6053 if Is_Boolean_Type (Typ1) then
6054 Adjust_Condition (Op1);
6055 Adjust_Condition (Op2);
6056 Set_Etype (N, Standard_Boolean);
6057 Adjust_Result_Type (N, Typ);
6060 Rewrite_Comparison (N);
6062 -- If we still have comparison, and Vax_Float type, process it
6064 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6065 Expand_Vax_Comparison (N);
6070 -----------------------
6071 -- Expand_N_Op_Minus --
6072 -----------------------
6074 procedure Expand_N_Op_Minus (N : Node_Id) is
6075 Loc : constant Source_Ptr := Sloc (N);
6076 Typ : constant Entity_Id := Etype (N);
6079 Unary_Op_Validity_Checks (N);
6081 if not Backend_Overflow_Checks_On_Target
6082 and then Is_Signed_Integer_Type (Etype (N))
6083 and then Do_Overflow_Check (N)
6085 -- Software overflow checking expands -expr into (0 - expr)
6088 Make_Op_Subtract (Loc,
6089 Left_Opnd => Make_Integer_Literal (Loc, 0),
6090 Right_Opnd => Right_Opnd (N)));
6092 Analyze_And_Resolve (N, Typ);
6094 -- Vax floating-point types case
6096 elsif Vax_Float (Etype (N)) then
6097 Expand_Vax_Arith (N);
6099 end Expand_N_Op_Minus;
6101 ---------------------
6102 -- Expand_N_Op_Mod --
6103 ---------------------
6105 procedure Expand_N_Op_Mod (N : Node_Id) is
6106 Loc : constant Source_Ptr := Sloc (N);
6107 Typ : constant Entity_Id := Etype (N);
6108 Left : constant Node_Id := Left_Opnd (N);
6109 Right : constant Node_Id := Right_Opnd (N);
6110 DOC : constant Boolean := Do_Overflow_Check (N);
6111 DDC : constant Boolean := Do_Division_Check (N);
6121 pragma Warnings (Off, Lhi);
6124 Binary_Op_Validity_Checks (N);
6126 Determine_Range (Right, ROK, Rlo, Rhi);
6127 Determine_Range (Left, LOK, Llo, Lhi);
6129 -- Convert mod to rem if operands are known non-negative. We do this
6130 -- since it is quite likely that this will improve the quality of code,
6131 -- (the operation now corresponds to the hardware remainder), and it
6132 -- does not seem likely that it could be harmful.
6134 if LOK and then Llo >= 0
6136 ROK and then Rlo >= 0
6139 Make_Op_Rem (Sloc (N),
6140 Left_Opnd => Left_Opnd (N),
6141 Right_Opnd => Right_Opnd (N)));
6143 -- Instead of reanalyzing the node we do the analysis manually. This
6144 -- avoids anomalies when the replacement is done in an instance and
6145 -- is epsilon more efficient.
6147 Set_Entity (N, Standard_Entity (S_Op_Rem));
6149 Set_Do_Overflow_Check (N, DOC);
6150 Set_Do_Division_Check (N, DDC);
6151 Expand_N_Op_Rem (N);
6154 -- Otherwise, normal mod processing
6157 if Is_Integer_Type (Etype (N)) then
6158 Apply_Divide_Check (N);
6161 -- Apply optimization x mod 1 = 0. We don't really need that with
6162 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
6163 -- certainly harmless.
6165 if Is_Integer_Type (Etype (N))
6166 and then Compile_Time_Known_Value (Right)
6167 and then Expr_Value (Right) = Uint_1
6169 -- Call Remove_Side_Effects to ensure that any side effects in
6170 -- the ignored left operand (in particular function calls to
6171 -- user defined functions) are properly executed.
6173 Remove_Side_Effects (Left);
6175 Rewrite (N, Make_Integer_Literal (Loc, 0));
6176 Analyze_And_Resolve (N, Typ);
6180 -- Deal with annoying case of largest negative number remainder
6181 -- minus one. Gigi does not handle this case correctly, because
6182 -- it generates a divide instruction which may trap in this case.
6184 -- In fact the check is quite easy, if the right operand is -1, then
6185 -- the mod value is always 0, and we can just ignore the left operand
6186 -- completely in this case.
6188 -- The operand type may be private (e.g. in the expansion of an
6189 -- intrinsic operation) so we must use the underlying type to get the
6190 -- bounds, and convert the literals explicitly.
6194 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
6196 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
6198 ((not LOK) or else (Llo = LLB))
6201 Make_Conditional_Expression (Loc,
6202 Expressions => New_List (
6204 Left_Opnd => Duplicate_Subexpr (Right),
6206 Unchecked_Convert_To (Typ,
6207 Make_Integer_Literal (Loc, -1))),
6208 Unchecked_Convert_To (Typ,
6209 Make_Integer_Literal (Loc, Uint_0)),
6210 Relocate_Node (N))));
6212 Set_Analyzed (Next (Next (First (Expressions (N)))));
6213 Analyze_And_Resolve (N, Typ);
6216 end Expand_N_Op_Mod;
6218 --------------------------
6219 -- Expand_N_Op_Multiply --
6220 --------------------------
6222 procedure Expand_N_Op_Multiply (N : Node_Id) is
6223 Loc : constant Source_Ptr := Sloc (N);
6224 Lop : constant Node_Id := Left_Opnd (N);
6225 Rop : constant Node_Id := Right_Opnd (N);
6227 Lp2 : constant Boolean :=
6228 Nkind (Lop) = N_Op_Expon
6229 and then Is_Power_Of_2_For_Shift (Lop);
6231 Rp2 : constant Boolean :=
6232 Nkind (Rop) = N_Op_Expon
6233 and then Is_Power_Of_2_For_Shift (Rop);
6235 Ltyp : constant Entity_Id := Etype (Lop);
6236 Rtyp : constant Entity_Id := Etype (Rop);
6237 Typ : Entity_Id := Etype (N);
6240 Binary_Op_Validity_Checks (N);
6242 -- Special optimizations for integer types
6244 if Is_Integer_Type (Typ) then
6246 -- N * 0 = 0 for integer types
6248 if Compile_Time_Known_Value (Rop)
6249 and then Expr_Value (Rop) = Uint_0
6251 -- Call Remove_Side_Effects to ensure that any side effects in
6252 -- the ignored left operand (in particular function calls to
6253 -- user defined functions) are properly executed.
6255 Remove_Side_Effects (Lop);
6257 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6258 Analyze_And_Resolve (N, Typ);
6262 -- Similar handling for 0 * N = 0
6264 if Compile_Time_Known_Value (Lop)
6265 and then Expr_Value (Lop) = Uint_0
6267 Remove_Side_Effects (Rop);
6268 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6269 Analyze_And_Resolve (N, Typ);
6273 -- N * 1 = 1 * N = N for integer types
6275 -- This optimisation is not done if we are going to
6276 -- rewrite the product 1 * 2 ** N to a shift.
6278 if Compile_Time_Known_Value (Rop)
6279 and then Expr_Value (Rop) = Uint_1
6285 elsif Compile_Time_Known_Value (Lop)
6286 and then Expr_Value (Lop) = Uint_1
6294 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
6295 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6296 -- operand is an integer, as required for this to work.
6301 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
6305 Left_Opnd => Make_Integer_Literal (Loc, 2),
6308 Left_Opnd => Right_Opnd (Lop),
6309 Right_Opnd => Right_Opnd (Rop))));
6310 Analyze_And_Resolve (N, Typ);
6315 Make_Op_Shift_Left (Loc,
6318 Convert_To (Standard_Natural, Right_Opnd (Rop))));
6319 Analyze_And_Resolve (N, Typ);
6323 -- Same processing for the operands the other way round
6327 Make_Op_Shift_Left (Loc,
6330 Convert_To (Standard_Natural, Right_Opnd (Lop))));
6331 Analyze_And_Resolve (N, Typ);
6335 -- Do required fixup of universal fixed operation
6337 if Typ = Universal_Fixed then
6338 Fixup_Universal_Fixed_Operation (N);
6342 -- Multiplications with fixed-point results
6344 if Is_Fixed_Point_Type (Typ) then
6346 -- No special processing if Treat_Fixed_As_Integer is set, since from
6347 -- a semantic point of view such operations are simply integer
6348 -- operations and will be treated that way.
6350 if not Treat_Fixed_As_Integer (N) then
6352 -- Case of fixed * integer => fixed
6354 if Is_Integer_Type (Rtyp) then
6355 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
6357 -- Case of integer * fixed => fixed
6359 elsif Is_Integer_Type (Ltyp) then
6360 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
6362 -- Case of fixed * fixed => fixed
6365 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
6369 -- Other cases of multiplication of fixed-point operands. Again we
6370 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
6372 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6373 and then not Treat_Fixed_As_Integer (N)
6375 if Is_Integer_Type (Typ) then
6376 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
6378 pragma Assert (Is_Floating_Point_Type (Typ));
6379 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
6382 -- Mixed-mode operations can appear in a non-static universal context,
6383 -- in which case the integer argument must be converted explicitly.
6385 elsif Typ = Universal_Real
6386 and then Is_Integer_Type (Rtyp)
6388 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
6390 Analyze_And_Resolve (Rop, Universal_Real);
6392 elsif Typ = Universal_Real
6393 and then Is_Integer_Type (Ltyp)
6395 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
6397 Analyze_And_Resolve (Lop, Universal_Real);
6399 -- Non-fixed point cases, check software overflow checking required
6401 elsif Is_Signed_Integer_Type (Etype (N)) then
6402 Apply_Arithmetic_Overflow_Check (N);
6404 -- Deal with VAX float case
6406 elsif Vax_Float (Typ) then
6407 Expand_Vax_Arith (N);
6410 end Expand_N_Op_Multiply;
6412 --------------------
6413 -- Expand_N_Op_Ne --
6414 --------------------
6416 procedure Expand_N_Op_Ne (N : Node_Id) is
6417 Typ : constant Entity_Id := Etype (Left_Opnd (N));
6420 -- Case of elementary type with standard operator
6422 if Is_Elementary_Type (Typ)
6423 and then Sloc (Entity (N)) = Standard_Location
6425 Binary_Op_Validity_Checks (N);
6427 -- Boolean types (requiring handling of non-standard case)
6429 if Is_Boolean_Type (Typ) then
6430 Adjust_Condition (Left_Opnd (N));
6431 Adjust_Condition (Right_Opnd (N));
6432 Set_Etype (N, Standard_Boolean);
6433 Adjust_Result_Type (N, Typ);
6436 Rewrite_Comparison (N);
6438 -- If we still have comparison for Vax_Float, process it
6440 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
6441 Expand_Vax_Comparison (N);
6445 -- For all cases other than elementary types, we rewrite node as the
6446 -- negation of an equality operation, and reanalyze. The equality to be
6447 -- used is defined in the same scope and has the same signature. This
6448 -- signature must be set explicitly since in an instance it may not have
6449 -- the same visibility as in the generic unit. This avoids duplicating
6450 -- or factoring the complex code for record/array equality tests etc.
6454 Loc : constant Source_Ptr := Sloc (N);
6456 Ne : constant Entity_Id := Entity (N);
6459 Binary_Op_Validity_Checks (N);
6465 Left_Opnd => Left_Opnd (N),
6466 Right_Opnd => Right_Opnd (N)));
6467 Set_Paren_Count (Right_Opnd (Neg), 1);
6469 if Scope (Ne) /= Standard_Standard then
6470 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
6473 -- For navigation purposes, the inequality is treated as an
6474 -- implicit reference to the corresponding equality. Preserve the
6475 -- Comes_From_ source flag so that the proper Xref entry is
6478 Preserve_Comes_From_Source (Neg, N);
6479 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
6481 Analyze_And_Resolve (N, Standard_Boolean);
6486 ---------------------
6487 -- Expand_N_Op_Not --
6488 ---------------------
6490 -- If the argument is other than a Boolean array type, there is no special
6491 -- expansion required.
6493 -- For the packed case, we call the special routine in Exp_Pakd, except
6494 -- that if the component size is greater than one, we use the standard
6495 -- routine generating a gruesome loop (it is so peculiar to have packed
6496 -- arrays with non-standard Boolean representations anyway, so it does not
6497 -- matter that we do not handle this case efficiently).
6499 -- For the unpacked case (and for the special packed case where we have non
6500 -- standard Booleans, as discussed above), we generate and insert into the
6501 -- tree the following function definition:
6503 -- function Nnnn (A : arr) is
6506 -- for J in a'range loop
6507 -- B (J) := not A (J);
6512 -- Here arr is the actual subtype of the parameter (and hence always
6513 -- constrained). Then we replace the not with a call to this function.
6515 procedure Expand_N_Op_Not (N : Node_Id) is
6516 Loc : constant Source_Ptr := Sloc (N);
6517 Typ : constant Entity_Id := Etype (N);
6526 Func_Name : Entity_Id;
6527 Loop_Statement : Node_Id;
6530 Unary_Op_Validity_Checks (N);
6532 -- For boolean operand, deal with non-standard booleans
6534 if Is_Boolean_Type (Typ) then
6535 Adjust_Condition (Right_Opnd (N));
6536 Set_Etype (N, Standard_Boolean);
6537 Adjust_Result_Type (N, Typ);
6541 -- Only array types need any other processing
6543 if not Is_Array_Type (Typ) then
6547 -- Case of array operand. If bit packed with a component size of 1,
6548 -- handle it in Exp_Pakd if the operand is known to be aligned.
6550 if Is_Bit_Packed_Array (Typ)
6551 and then Component_Size (Typ) = 1
6552 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
6554 Expand_Packed_Not (N);
6558 -- Case of array operand which is not bit-packed. If the context is
6559 -- a safe assignment, call in-place operation, If context is a larger
6560 -- boolean expression in the context of a safe assignment, expansion is
6561 -- done by enclosing operation.
6563 Opnd := Relocate_Node (Right_Opnd (N));
6564 Convert_To_Actual_Subtype (Opnd);
6565 Arr := Etype (Opnd);
6566 Ensure_Defined (Arr, N);
6567 Silly_Boolean_Array_Not_Test (N, Arr);
6569 if Nkind (Parent (N)) = N_Assignment_Statement then
6570 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
6571 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
6574 -- Special case the negation of a binary operation
6576 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
6577 and then Safe_In_Place_Array_Op
6578 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
6580 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
6584 elsif Nkind (Parent (N)) in N_Binary_Op
6585 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
6588 Op1 : constant Node_Id := Left_Opnd (Parent (N));
6589 Op2 : constant Node_Id := Right_Opnd (Parent (N));
6590 Lhs : constant Node_Id := Name (Parent (Parent (N)));
6593 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
6595 and then Nkind (Op2) = N_Op_Not
6597 -- (not A) op (not B) can be reduced to a single call
6602 and then Nkind (Parent (N)) = N_Op_Xor
6604 -- A xor (not B) can also be special-cased
6612 A := Make_Defining_Identifier (Loc, Name_uA);
6613 B := Make_Defining_Identifier (Loc, Name_uB);
6614 J := Make_Defining_Identifier (Loc, Name_uJ);
6617 Make_Indexed_Component (Loc,
6618 Prefix => New_Reference_To (A, Loc),
6619 Expressions => New_List (New_Reference_To (J, Loc)));
6622 Make_Indexed_Component (Loc,
6623 Prefix => New_Reference_To (B, Loc),
6624 Expressions => New_List (New_Reference_To (J, Loc)));
6627 Make_Implicit_Loop_Statement (N,
6628 Identifier => Empty,
6631 Make_Iteration_Scheme (Loc,
6632 Loop_Parameter_Specification =>
6633 Make_Loop_Parameter_Specification (Loc,
6634 Defining_Identifier => J,
6635 Discrete_Subtype_Definition =>
6636 Make_Attribute_Reference (Loc,
6637 Prefix => Make_Identifier (Loc, Chars (A)),
6638 Attribute_Name => Name_Range))),
6640 Statements => New_List (
6641 Make_Assignment_Statement (Loc,
6643 Expression => Make_Op_Not (Loc, A_J))));
6645 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('N'));
6646 Set_Is_Inlined (Func_Name);
6649 Make_Subprogram_Body (Loc,
6651 Make_Function_Specification (Loc,
6652 Defining_Unit_Name => Func_Name,
6653 Parameter_Specifications => New_List (
6654 Make_Parameter_Specification (Loc,
6655 Defining_Identifier => A,
6656 Parameter_Type => New_Reference_To (Typ, Loc))),
6657 Result_Definition => New_Reference_To (Typ, Loc)),
6659 Declarations => New_List (
6660 Make_Object_Declaration (Loc,
6661 Defining_Identifier => B,
6662 Object_Definition => New_Reference_To (Arr, Loc))),
6664 Handled_Statement_Sequence =>
6665 Make_Handled_Sequence_Of_Statements (Loc,
6666 Statements => New_List (
6668 Make_Simple_Return_Statement (Loc,
6670 Make_Identifier (Loc, Chars (B)))))));
6673 Make_Function_Call (Loc,
6674 Name => New_Reference_To (Func_Name, Loc),
6675 Parameter_Associations => New_List (Opnd)));
6677 Analyze_And_Resolve (N, Typ);
6678 end Expand_N_Op_Not;
6680 --------------------
6681 -- Expand_N_Op_Or --
6682 --------------------
6684 procedure Expand_N_Op_Or (N : Node_Id) is
6685 Typ : constant Entity_Id := Etype (N);
6688 Binary_Op_Validity_Checks (N);
6690 if Is_Array_Type (Etype (N)) then
6691 Expand_Boolean_Operator (N);
6693 elsif Is_Boolean_Type (Etype (N)) then
6694 Adjust_Condition (Left_Opnd (N));
6695 Adjust_Condition (Right_Opnd (N));
6696 Set_Etype (N, Standard_Boolean);
6697 Adjust_Result_Type (N, Typ);
6701 ----------------------
6702 -- Expand_N_Op_Plus --
6703 ----------------------
6705 procedure Expand_N_Op_Plus (N : Node_Id) is
6707 Unary_Op_Validity_Checks (N);
6708 end Expand_N_Op_Plus;
6710 ---------------------
6711 -- Expand_N_Op_Rem --
6712 ---------------------
6714 procedure Expand_N_Op_Rem (N : Node_Id) is
6715 Loc : constant Source_Ptr := Sloc (N);
6716 Typ : constant Entity_Id := Etype (N);
6718 Left : constant Node_Id := Left_Opnd (N);
6719 Right : constant Node_Id := Right_Opnd (N);
6729 pragma Warnings (Off, Lhi);
6732 Binary_Op_Validity_Checks (N);
6734 if Is_Integer_Type (Etype (N)) then
6735 Apply_Divide_Check (N);
6738 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
6739 -- but it is useful with other back ends (e.g. AAMP), and is certainly
6742 if Is_Integer_Type (Etype (N))
6743 and then Compile_Time_Known_Value (Right)
6744 and then Expr_Value (Right) = Uint_1
6746 -- Call Remove_Side_Effects to ensure that any side effects in the
6747 -- ignored left operand (in particular function calls to user defined
6748 -- functions) are properly executed.
6750 Remove_Side_Effects (Left);
6752 Rewrite (N, Make_Integer_Literal (Loc, 0));
6753 Analyze_And_Resolve (N, Typ);
6757 -- Deal with annoying case of largest negative number remainder minus
6758 -- one. Gigi does not handle this case correctly, because it generates
6759 -- a divide instruction which may trap in this case.
6761 -- In fact the check is quite easy, if the right operand is -1, then
6762 -- the remainder is always 0, and we can just ignore the left operand
6763 -- completely in this case.
6765 Determine_Range (Right, ROK, Rlo, Rhi);
6766 Determine_Range (Left, LOK, Llo, Lhi);
6768 -- The operand type may be private (e.g. in the expansion of an
6769 -- intrinsic operation) so we must use the underlying type to get the
6770 -- bounds, and convert the literals explicitly.
6774 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
6776 -- Now perform the test, generating code only if needed
6778 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
6780 ((not LOK) or else (Llo = LLB))
6783 Make_Conditional_Expression (Loc,
6784 Expressions => New_List (
6786 Left_Opnd => Duplicate_Subexpr (Right),
6788 Unchecked_Convert_To (Typ,
6789 Make_Integer_Literal (Loc, -1))),
6791 Unchecked_Convert_To (Typ,
6792 Make_Integer_Literal (Loc, Uint_0)),
6794 Relocate_Node (N))));
6796 Set_Analyzed (Next (Next (First (Expressions (N)))));
6797 Analyze_And_Resolve (N, Typ);
6799 end Expand_N_Op_Rem;
6801 -----------------------------
6802 -- Expand_N_Op_Rotate_Left --
6803 -----------------------------
6805 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
6807 Binary_Op_Validity_Checks (N);
6808 end Expand_N_Op_Rotate_Left;
6810 ------------------------------
6811 -- Expand_N_Op_Rotate_Right --
6812 ------------------------------
6814 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
6816 Binary_Op_Validity_Checks (N);
6817 end Expand_N_Op_Rotate_Right;
6819 ----------------------------
6820 -- Expand_N_Op_Shift_Left --
6821 ----------------------------
6823 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
6825 Binary_Op_Validity_Checks (N);
6826 end Expand_N_Op_Shift_Left;
6828 -----------------------------
6829 -- Expand_N_Op_Shift_Right --
6830 -----------------------------
6832 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
6834 Binary_Op_Validity_Checks (N);
6835 end Expand_N_Op_Shift_Right;
6837 ----------------------------------------
6838 -- Expand_N_Op_Shift_Right_Arithmetic --
6839 ----------------------------------------
6841 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
6843 Binary_Op_Validity_Checks (N);
6844 end Expand_N_Op_Shift_Right_Arithmetic;
6846 --------------------------
6847 -- Expand_N_Op_Subtract --
6848 --------------------------
6850 procedure Expand_N_Op_Subtract (N : Node_Id) is
6851 Typ : constant Entity_Id := Etype (N);
6854 Binary_Op_Validity_Checks (N);
6856 -- N - 0 = N for integer types
6858 if Is_Integer_Type (Typ)
6859 and then Compile_Time_Known_Value (Right_Opnd (N))
6860 and then Expr_Value (Right_Opnd (N)) = 0
6862 Rewrite (N, Left_Opnd (N));
6866 -- Arithmetic overflow checks for signed integer/fixed point types
6868 if Is_Signed_Integer_Type (Typ)
6869 or else Is_Fixed_Point_Type (Typ)
6871 Apply_Arithmetic_Overflow_Check (N);
6873 -- Vax floating-point types case
6875 elsif Vax_Float (Typ) then
6876 Expand_Vax_Arith (N);
6878 end Expand_N_Op_Subtract;
6880 ---------------------
6881 -- Expand_N_Op_Xor --
6882 ---------------------
6884 procedure Expand_N_Op_Xor (N : Node_Id) is
6885 Typ : constant Entity_Id := Etype (N);
6888 Binary_Op_Validity_Checks (N);
6890 if Is_Array_Type (Etype (N)) then
6891 Expand_Boolean_Operator (N);
6893 elsif Is_Boolean_Type (Etype (N)) then
6894 Adjust_Condition (Left_Opnd (N));
6895 Adjust_Condition (Right_Opnd (N));
6896 Set_Etype (N, Standard_Boolean);
6897 Adjust_Result_Type (N, Typ);
6899 end Expand_N_Op_Xor;
6901 ----------------------
6902 -- Expand_N_Or_Else --
6903 ----------------------
6905 -- Expand into conditional expression if Actions present, and also
6906 -- deal with optimizing case of arguments being True or False.
6908 procedure Expand_N_Or_Else (N : Node_Id) is
6909 Loc : constant Source_Ptr := Sloc (N);
6910 Typ : constant Entity_Id := Etype (N);
6911 Left : constant Node_Id := Left_Opnd (N);
6912 Right : constant Node_Id := Right_Opnd (N);
6916 -- Deal with non-standard booleans
6918 if Is_Boolean_Type (Typ) then
6919 Adjust_Condition (Left);
6920 Adjust_Condition (Right);
6921 Set_Etype (N, Standard_Boolean);
6924 -- Check for cases where left argument is known to be True or False
6926 if Compile_Time_Known_Value (Left) then
6928 -- If left argument is False, change (False or else Right) to Right.
6929 -- Any actions associated with Right will be executed unconditionally
6930 -- and can thus be inserted into the tree unconditionally.
6932 if Expr_Value_E (Left) = Standard_False then
6933 if Present (Actions (N)) then
6934 Insert_Actions (N, Actions (N));
6939 -- If left argument is True, change (True and then Right) to True. In
6940 -- this case we can forget the actions associated with Right, since
6941 -- they will never be executed.
6943 else pragma Assert (Expr_Value_E (Left) = Standard_True);
6944 Kill_Dead_Code (Right);
6945 Kill_Dead_Code (Actions (N));
6946 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
6949 Adjust_Result_Type (N, Typ);
6953 -- If Actions are present, we expand
6955 -- left or else right
6959 -- if left then True else right end
6961 -- with the actions becoming the Else_Actions of the conditional
6962 -- expression. This conditional expression is then further expanded
6963 -- (and will eventually disappear)
6965 if Present (Actions (N)) then
6966 Actlist := Actions (N);
6968 Make_Conditional_Expression (Loc,
6969 Expressions => New_List (
6971 New_Occurrence_Of (Standard_True, Loc),
6974 Set_Else_Actions (N, Actlist);
6975 Analyze_And_Resolve (N, Standard_Boolean);
6976 Adjust_Result_Type (N, Typ);
6980 -- No actions present, check for cases of right argument True/False
6982 if Compile_Time_Known_Value (Right) then
6984 -- Change (Left or else False) to Left. Note that we know there are
6985 -- no actions associated with the True operand, since we just checked
6986 -- for this case above.
6988 if Expr_Value_E (Right) = Standard_False then
6991 -- Change (Left or else True) to True, making sure to preserve any
6992 -- side effects associated with the Left operand.
6994 else pragma Assert (Expr_Value_E (Right) = Standard_True);
6995 Remove_Side_Effects (Left);
6997 (N, New_Occurrence_Of (Standard_True, Loc));
7001 Adjust_Result_Type (N, Typ);
7002 end Expand_N_Or_Else;
7004 -----------------------------------
7005 -- Expand_N_Qualified_Expression --
7006 -----------------------------------
7008 procedure Expand_N_Qualified_Expression (N : Node_Id) is
7009 Operand : constant Node_Id := Expression (N);
7010 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
7013 -- Do validity check if validity checking operands
7015 if Validity_Checks_On
7016 and then Validity_Check_Operands
7018 Ensure_Valid (Operand);
7021 -- Apply possible constraint check
7023 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
7024 end Expand_N_Qualified_Expression;
7026 ---------------------------------
7027 -- Expand_N_Selected_Component --
7028 ---------------------------------
7030 -- If the selector is a discriminant of a concurrent object, rewrite the
7031 -- prefix to denote the corresponding record type.
7033 procedure Expand_N_Selected_Component (N : Node_Id) is
7034 Loc : constant Source_Ptr := Sloc (N);
7035 Par : constant Node_Id := Parent (N);
7036 P : constant Node_Id := Prefix (N);
7037 Ptyp : Entity_Id := Underlying_Type (Etype (P));
7042 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
7043 -- Gigi needs a temporary for prefixes that depend on a discriminant,
7044 -- unless the context of an assignment can provide size information.
7045 -- Don't we have a general routine that does this???
7047 -----------------------
7048 -- In_Left_Hand_Side --
7049 -----------------------
7051 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
7053 return (Nkind (Parent (Comp)) = N_Assignment_Statement
7054 and then Comp = Name (Parent (Comp)))
7055 or else (Present (Parent (Comp))
7056 and then Nkind (Parent (Comp)) in N_Subexpr
7057 and then In_Left_Hand_Side (Parent (Comp)));
7058 end In_Left_Hand_Side;
7060 -- Start of processing for Expand_N_Selected_Component
7063 -- Insert explicit dereference if required
7065 if Is_Access_Type (Ptyp) then
7066 Insert_Explicit_Dereference (P);
7067 Analyze_And_Resolve (P, Designated_Type (Ptyp));
7069 if Ekind (Etype (P)) = E_Private_Subtype
7070 and then Is_For_Access_Subtype (Etype (P))
7072 Set_Etype (P, Base_Type (Etype (P)));
7078 -- Deal with discriminant check required
7080 if Do_Discriminant_Check (N) then
7082 -- Present the discriminant checking function to the backend, so that
7083 -- it can inline the call to the function.
7086 (Discriminant_Checking_Func
7087 (Original_Record_Component (Entity (Selector_Name (N)))));
7089 -- Now reset the flag and generate the call
7091 Set_Do_Discriminant_Check (N, False);
7092 Generate_Discriminant_Check (N);
7095 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7096 -- function, then additional actuals must be passed.
7098 if Ada_Version >= Ada_05
7099 and then Is_Build_In_Place_Function_Call (P)
7101 Make_Build_In_Place_Call_In_Anonymous_Context (P);
7104 -- Gigi cannot handle unchecked conversions that are the prefix of a
7105 -- selected component with discriminants. This must be checked during
7106 -- expansion, because during analysis the type of the selector is not
7107 -- known at the point the prefix is analyzed. If the conversion is the
7108 -- target of an assignment, then we cannot force the evaluation.
7110 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
7111 and then Has_Discriminants (Etype (N))
7112 and then not In_Left_Hand_Side (N)
7114 Force_Evaluation (Prefix (N));
7117 -- Remaining processing applies only if selector is a discriminant
7119 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
7121 -- If the selector is a discriminant of a constrained record type,
7122 -- we may be able to rewrite the expression with the actual value
7123 -- of the discriminant, a useful optimization in some cases.
7125 if Is_Record_Type (Ptyp)
7126 and then Has_Discriminants (Ptyp)
7127 and then Is_Constrained (Ptyp)
7129 -- Do this optimization for discrete types only, and not for
7130 -- access types (access discriminants get us into trouble!)
7132 if not Is_Discrete_Type (Etype (N)) then
7135 -- Don't do this on the left hand of an assignment statement.
7136 -- Normally one would think that references like this would
7137 -- not occur, but they do in generated code, and mean that
7138 -- we really do want to assign the discriminant!
7140 elsif Nkind (Par) = N_Assignment_Statement
7141 and then Name (Par) = N
7145 -- Don't do this optimization for the prefix of an attribute or
7146 -- the operand of an object renaming declaration since these are
7147 -- contexts where we do not want the value anyway.
7149 elsif (Nkind (Par) = N_Attribute_Reference
7150 and then Prefix (Par) = N)
7151 or else Is_Renamed_Object (N)
7155 -- Don't do this optimization if we are within the code for a
7156 -- discriminant check, since the whole point of such a check may
7157 -- be to verify the condition on which the code below depends!
7159 elsif Is_In_Discriminant_Check (N) then
7162 -- Green light to see if we can do the optimization. There is
7163 -- still one condition that inhibits the optimization below but
7164 -- now is the time to check the particular discriminant.
7167 -- Loop through discriminants to find the matching discriminant
7168 -- constraint to see if we can copy it.
7170 Disc := First_Discriminant (Ptyp);
7171 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
7172 Discr_Loop : while Present (Dcon) loop
7174 -- Check if this is the matching discriminant
7176 if Disc = Entity (Selector_Name (N)) then
7178 -- Here we have the matching discriminant. Check for
7179 -- the case of a discriminant of a component that is
7180 -- constrained by an outer discriminant, which cannot
7181 -- be optimized away.
7184 Denotes_Discriminant
7185 (Node (Dcon), Check_Concurrent => True)
7189 -- In the context of a case statement, the expression may
7190 -- have the base type of the discriminant, and we need to
7191 -- preserve the constraint to avoid spurious errors on
7194 elsif Nkind (Parent (N)) = N_Case_Statement
7195 and then Etype (Node (Dcon)) /= Etype (Disc)
7198 Make_Qualified_Expression (Loc,
7200 New_Occurrence_Of (Etype (Disc), Loc),
7202 New_Copy_Tree (Node (Dcon))));
7203 Analyze_And_Resolve (N, Etype (Disc));
7205 -- In case that comes out as a static expression,
7206 -- reset it (a selected component is never static).
7208 Set_Is_Static_Expression (N, False);
7211 -- Otherwise we can just copy the constraint, but the
7212 -- result is certainly not static! In some cases the
7213 -- discriminant constraint has been analyzed in the
7214 -- context of the original subtype indication, but for
7215 -- itypes the constraint might not have been analyzed
7216 -- yet, and this must be done now.
7219 Rewrite (N, New_Copy_Tree (Node (Dcon)));
7220 Analyze_And_Resolve (N);
7221 Set_Is_Static_Expression (N, False);
7227 Next_Discriminant (Disc);
7228 end loop Discr_Loop;
7230 -- Note: the above loop should always find a matching
7231 -- discriminant, but if it does not, we just missed an
7232 -- optimization due to some glitch (perhaps a previous error),
7238 -- The only remaining processing is in the case of a discriminant of
7239 -- a concurrent object, where we rewrite the prefix to denote the
7240 -- corresponding record type. If the type is derived and has renamed
7241 -- discriminants, use corresponding discriminant, which is the one
7242 -- that appears in the corresponding record.
7244 if not Is_Concurrent_Type (Ptyp) then
7248 Disc := Entity (Selector_Name (N));
7250 if Is_Derived_Type (Ptyp)
7251 and then Present (Corresponding_Discriminant (Disc))
7253 Disc := Corresponding_Discriminant (Disc);
7257 Make_Selected_Component (Loc,
7259 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
7261 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
7266 end Expand_N_Selected_Component;
7268 --------------------
7269 -- Expand_N_Slice --
7270 --------------------
7272 procedure Expand_N_Slice (N : Node_Id) is
7273 Loc : constant Source_Ptr := Sloc (N);
7274 Typ : constant Entity_Id := Etype (N);
7275 Pfx : constant Node_Id := Prefix (N);
7276 Ptp : Entity_Id := Etype (Pfx);
7278 function Is_Procedure_Actual (N : Node_Id) return Boolean;
7279 -- Check whether the argument is an actual for a procedure call, in
7280 -- which case the expansion of a bit-packed slice is deferred until the
7281 -- call itself is expanded. The reason this is required is that we might
7282 -- have an IN OUT or OUT parameter, and the copy out is essential, and
7283 -- that copy out would be missed if we created a temporary here in
7284 -- Expand_N_Slice. Note that we don't bother to test specifically for an
7285 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
7286 -- is harmless to defer expansion in the IN case, since the call
7287 -- processing will still generate the appropriate copy in operation,
7288 -- which will take care of the slice.
7290 procedure Make_Temporary;
7291 -- Create a named variable for the value of the slice, in cases where
7292 -- the back-end cannot handle it properly, e.g. when packed types or
7293 -- unaligned slices are involved.
7295 -------------------------
7296 -- Is_Procedure_Actual --
7297 -------------------------
7299 function Is_Procedure_Actual (N : Node_Id) return Boolean is
7300 Par : Node_Id := Parent (N);
7304 -- If our parent is a procedure call we can return
7306 if Nkind (Par) = N_Procedure_Call_Statement then
7309 -- If our parent is a type conversion, keep climbing the tree,
7310 -- since a type conversion can be a procedure actual. Also keep
7311 -- climbing if parameter association or a qualified expression,
7312 -- since these are additional cases that do can appear on
7313 -- procedure actuals.
7315 elsif Nkind_In (Par, N_Type_Conversion,
7316 N_Parameter_Association,
7317 N_Qualified_Expression)
7319 Par := Parent (Par);
7321 -- Any other case is not what we are looking for
7327 end Is_Procedure_Actual;
7329 --------------------
7330 -- Make_Temporary --
7331 --------------------
7333 procedure Make_Temporary is
7335 Ent : constant Entity_Id :=
7336 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
7339 Make_Object_Declaration (Loc,
7340 Defining_Identifier => Ent,
7341 Object_Definition => New_Occurrence_Of (Typ, Loc));
7343 Set_No_Initialization (Decl);
7345 Insert_Actions (N, New_List (
7347 Make_Assignment_Statement (Loc,
7348 Name => New_Occurrence_Of (Ent, Loc),
7349 Expression => Relocate_Node (N))));
7351 Rewrite (N, New_Occurrence_Of (Ent, Loc));
7352 Analyze_And_Resolve (N, Typ);
7355 -- Start of processing for Expand_N_Slice
7358 -- Special handling for access types
7360 if Is_Access_Type (Ptp) then
7362 Ptp := Designated_Type (Ptp);
7365 Make_Explicit_Dereference (Sloc (N),
7366 Prefix => Relocate_Node (Pfx)));
7368 Analyze_And_Resolve (Pfx, Ptp);
7371 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7372 -- function, then additional actuals must be passed.
7374 if Ada_Version >= Ada_05
7375 and then Is_Build_In_Place_Function_Call (Pfx)
7377 Make_Build_In_Place_Call_In_Anonymous_Context (Pfx);
7380 -- Range checks are potentially also needed for cases involving a slice
7381 -- indexed by a subtype indication, but Do_Range_Check can currently
7382 -- only be set for expressions ???
7384 if not Index_Checks_Suppressed (Ptp)
7385 and then (not Is_Entity_Name (Pfx)
7386 or else not Index_Checks_Suppressed (Entity (Pfx)))
7387 and then Nkind (Discrete_Range (N)) /= N_Subtype_Indication
7389 -- Do not enable range check to nodes associated with the frontend
7390 -- expansion of the dispatch table. We first check if Ada.Tags is
7391 -- already loaded to avoid the addition of an undesired dependence
7392 -- on such run-time unit.
7395 (not Tagged_Type_Expansion
7397 (RTU_Loaded (Ada_Tags)
7398 and then Nkind (Prefix (N)) = N_Selected_Component
7399 and then Present (Entity (Selector_Name (Prefix (N))))
7400 and then Entity (Selector_Name (Prefix (N))) =
7401 RTE_Record_Component (RE_Prims_Ptr)))
7403 Enable_Range_Check (Discrete_Range (N));
7406 -- The remaining case to be handled is packed slices. We can leave
7407 -- packed slices as they are in the following situations:
7409 -- 1. Right or left side of an assignment (we can handle this
7410 -- situation correctly in the assignment statement expansion).
7412 -- 2. Prefix of indexed component (the slide is optimized away in this
7413 -- case, see the start of Expand_N_Slice.)
7415 -- 3. Object renaming declaration, since we want the name of the
7416 -- slice, not the value.
7418 -- 4. Argument to procedure call, since copy-in/copy-out handling may
7419 -- be required, and this is handled in the expansion of call
7422 -- 5. Prefix of an address attribute (this is an error which is caught
7423 -- elsewhere, and the expansion would interfere with generating the
7426 if not Is_Packed (Typ) then
7428 -- Apply transformation for actuals of a function call, where
7429 -- Expand_Actuals is not used.
7431 if Nkind (Parent (N)) = N_Function_Call
7432 and then Is_Possibly_Unaligned_Slice (N)
7437 elsif Nkind (Parent (N)) = N_Assignment_Statement
7438 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
7439 and then Parent (N) = Name (Parent (Parent (N))))
7443 elsif Nkind (Parent (N)) = N_Indexed_Component
7444 or else Is_Renamed_Object (N)
7445 or else Is_Procedure_Actual (N)
7449 elsif Nkind (Parent (N)) = N_Attribute_Reference
7450 and then Attribute_Name (Parent (N)) = Name_Address
7459 ------------------------------
7460 -- Expand_N_Type_Conversion --
7461 ------------------------------
7463 procedure Expand_N_Type_Conversion (N : Node_Id) is
7464 Loc : constant Source_Ptr := Sloc (N);
7465 Operand : constant Node_Id := Expression (N);
7466 Target_Type : constant Entity_Id := Etype (N);
7467 Operand_Type : Entity_Id := Etype (Operand);
7469 procedure Handle_Changed_Representation;
7470 -- This is called in the case of record and array type conversions to
7471 -- see if there is a change of representation to be handled. Change of
7472 -- representation is actually handled at the assignment statement level,
7473 -- and what this procedure does is rewrite node N conversion as an
7474 -- assignment to temporary. If there is no change of representation,
7475 -- then the conversion node is unchanged.
7477 procedure Real_Range_Check;
7478 -- Handles generation of range check for real target value
7480 -----------------------------------
7481 -- Handle_Changed_Representation --
7482 -----------------------------------
7484 procedure Handle_Changed_Representation is
7493 -- Nothing else to do if no change of representation
7495 if Same_Representation (Operand_Type, Target_Type) then
7498 -- The real change of representation work is done by the assignment
7499 -- statement processing. So if this type conversion is appearing as
7500 -- the expression of an assignment statement, nothing needs to be
7501 -- done to the conversion.
7503 elsif Nkind (Parent (N)) = N_Assignment_Statement then
7506 -- Otherwise we need to generate a temporary variable, and do the
7507 -- change of representation assignment into that temporary variable.
7508 -- The conversion is then replaced by a reference to this variable.
7513 -- If type is unconstrained we have to add a constraint, copied
7514 -- from the actual value of the left hand side.
7516 if not Is_Constrained (Target_Type) then
7517 if Has_Discriminants (Operand_Type) then
7518 Disc := First_Discriminant (Operand_Type);
7520 if Disc /= First_Stored_Discriminant (Operand_Type) then
7521 Disc := First_Stored_Discriminant (Operand_Type);
7525 while Present (Disc) loop
7527 Make_Selected_Component (Loc,
7528 Prefix => Duplicate_Subexpr_Move_Checks (Operand),
7530 Make_Identifier (Loc, Chars (Disc))));
7531 Next_Discriminant (Disc);
7534 elsif Is_Array_Type (Operand_Type) then
7535 N_Ix := First_Index (Target_Type);
7538 for J in 1 .. Number_Dimensions (Operand_Type) loop
7540 -- We convert the bounds explicitly. We use an unchecked
7541 -- conversion because bounds checks are done elsewhere.
7546 Unchecked_Convert_To (Etype (N_Ix),
7547 Make_Attribute_Reference (Loc,
7549 Duplicate_Subexpr_No_Checks
7550 (Operand, Name_Req => True),
7551 Attribute_Name => Name_First,
7552 Expressions => New_List (
7553 Make_Integer_Literal (Loc, J)))),
7556 Unchecked_Convert_To (Etype (N_Ix),
7557 Make_Attribute_Reference (Loc,
7559 Duplicate_Subexpr_No_Checks
7560 (Operand, Name_Req => True),
7561 Attribute_Name => Name_Last,
7562 Expressions => New_List (
7563 Make_Integer_Literal (Loc, J))))));
7570 Odef := New_Occurrence_Of (Target_Type, Loc);
7572 if Present (Cons) then
7574 Make_Subtype_Indication (Loc,
7575 Subtype_Mark => Odef,
7577 Make_Index_Or_Discriminant_Constraint (Loc,
7578 Constraints => Cons));
7581 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
7583 Make_Object_Declaration (Loc,
7584 Defining_Identifier => Temp,
7585 Object_Definition => Odef);
7587 Set_No_Initialization (Decl, True);
7589 -- Insert required actions. It is essential to suppress checks
7590 -- since we have suppressed default initialization, which means
7591 -- that the variable we create may have no discriminants.
7596 Make_Assignment_Statement (Loc,
7597 Name => New_Occurrence_Of (Temp, Loc),
7598 Expression => Relocate_Node (N))),
7599 Suppress => All_Checks);
7601 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7604 end Handle_Changed_Representation;
7606 ----------------------
7607 -- Real_Range_Check --
7608 ----------------------
7610 -- Case of conversions to floating-point or fixed-point. If range checks
7611 -- are enabled and the target type has a range constraint, we convert:
7617 -- Tnn : typ'Base := typ'Base (x);
7618 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
7621 -- This is necessary when there is a conversion of integer to float or
7622 -- to fixed-point to ensure that the correct checks are made. It is not
7623 -- necessary for float to float where it is enough to simply set the
7624 -- Do_Range_Check flag.
7626 procedure Real_Range_Check is
7627 Btyp : constant Entity_Id := Base_Type (Target_Type);
7628 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
7629 Hi : constant Node_Id := Type_High_Bound (Target_Type);
7630 Xtyp : constant Entity_Id := Etype (Operand);
7635 -- Nothing to do if conversion was rewritten
7637 if Nkind (N) /= N_Type_Conversion then
7641 -- Nothing to do if range checks suppressed, or target has the same
7642 -- range as the base type (or is the base type).
7644 if Range_Checks_Suppressed (Target_Type)
7645 or else (Lo = Type_Low_Bound (Btyp)
7647 Hi = Type_High_Bound (Btyp))
7652 -- Nothing to do if expression is an entity on which checks have been
7655 if Is_Entity_Name (Operand)
7656 and then Range_Checks_Suppressed (Entity (Operand))
7661 -- Nothing to do if bounds are all static and we can tell that the
7662 -- expression is within the bounds of the target. Note that if the
7663 -- operand is of an unconstrained floating-point type, then we do
7664 -- not trust it to be in range (might be infinite)
7667 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
7668 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
7671 if (not Is_Floating_Point_Type (Xtyp)
7672 or else Is_Constrained (Xtyp))
7673 and then Compile_Time_Known_Value (S_Lo)
7674 and then Compile_Time_Known_Value (S_Hi)
7675 and then Compile_Time_Known_Value (Hi)
7676 and then Compile_Time_Known_Value (Lo)
7679 D_Lov : constant Ureal := Expr_Value_R (Lo);
7680 D_Hiv : constant Ureal := Expr_Value_R (Hi);
7685 if Is_Real_Type (Xtyp) then
7686 S_Lov := Expr_Value_R (S_Lo);
7687 S_Hiv := Expr_Value_R (S_Hi);
7689 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
7690 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
7694 and then S_Lov >= D_Lov
7695 and then S_Hiv <= D_Hiv
7697 Set_Do_Range_Check (Operand, False);
7704 -- For float to float conversions, we are done
7706 if Is_Floating_Point_Type (Xtyp)
7708 Is_Floating_Point_Type (Btyp)
7713 -- Otherwise rewrite the conversion as described above
7715 Conv := Relocate_Node (N);
7717 (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
7718 Set_Etype (Conv, Btyp);
7720 -- Enable overflow except for case of integer to float conversions,
7721 -- where it is never required, since we can never have overflow in
7724 if not Is_Integer_Type (Etype (Operand)) then
7725 Enable_Overflow_Check (Conv);
7729 Make_Defining_Identifier (Loc,
7730 Chars => New_Internal_Name ('T'));
7732 Insert_Actions (N, New_List (
7733 Make_Object_Declaration (Loc,
7734 Defining_Identifier => Tnn,
7735 Object_Definition => New_Occurrence_Of (Btyp, Loc),
7736 Expression => Conv),
7738 Make_Raise_Constraint_Error (Loc,
7743 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
7745 Make_Attribute_Reference (Loc,
7746 Attribute_Name => Name_First,
7748 New_Occurrence_Of (Target_Type, Loc))),
7752 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
7754 Make_Attribute_Reference (Loc,
7755 Attribute_Name => Name_Last,
7757 New_Occurrence_Of (Target_Type, Loc)))),
7758 Reason => CE_Range_Check_Failed)));
7760 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
7761 Analyze_And_Resolve (N, Btyp);
7762 end Real_Range_Check;
7764 -- Start of processing for Expand_N_Type_Conversion
7767 -- Nothing at all to do if conversion is to the identical type so remove
7768 -- the conversion completely, it is useless.
7770 if Operand_Type = Target_Type then
7771 Rewrite (N, Relocate_Node (Operand));
7775 -- Nothing to do if this is the second argument of read. This is a
7776 -- "backwards" conversion that will be handled by the specialized code
7777 -- in attribute processing.
7779 if Nkind (Parent (N)) = N_Attribute_Reference
7780 and then Attribute_Name (Parent (N)) = Name_Read
7781 and then Next (First (Expressions (Parent (N)))) = N
7786 -- Here if we may need to expand conversion
7788 -- Do validity check if validity checking operands
7790 if Validity_Checks_On
7791 and then Validity_Check_Operands
7793 Ensure_Valid (Operand);
7796 -- Special case of converting from non-standard boolean type
7798 if Is_Boolean_Type (Operand_Type)
7799 and then (Nonzero_Is_True (Operand_Type))
7801 Adjust_Condition (Operand);
7802 Set_Etype (Operand, Standard_Boolean);
7803 Operand_Type := Standard_Boolean;
7806 -- Case of converting to an access type
7808 if Is_Access_Type (Target_Type) then
7810 -- Apply an accessibility check when the conversion operand is an
7811 -- access parameter (or a renaming thereof), unless conversion was
7812 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
7813 -- Note that other checks may still need to be applied below (such
7814 -- as tagged type checks).
7816 if Is_Entity_Name (Operand)
7818 (Is_Formal (Entity (Operand))
7820 (Present (Renamed_Object (Entity (Operand)))
7821 and then Is_Entity_Name (Renamed_Object (Entity (Operand)))
7823 (Entity (Renamed_Object (Entity (Operand))))))
7824 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
7825 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
7826 or else Attribute_Name (Original_Node (N)) = Name_Access)
7828 Apply_Accessibility_Check
7829 (Operand, Target_Type, Insert_Node => Operand);
7831 -- If the level of the operand type is statically deeper than the
7832 -- level of the target type, then force Program_Error. Note that this
7833 -- can only occur for cases where the attribute is within the body of
7834 -- an instantiation (otherwise the conversion will already have been
7835 -- rejected as illegal). Note: warnings are issued by the analyzer
7836 -- for the instance cases.
7838 elsif In_Instance_Body
7839 and then Type_Access_Level (Operand_Type) >
7840 Type_Access_Level (Target_Type)
7843 Make_Raise_Program_Error (Sloc (N),
7844 Reason => PE_Accessibility_Check_Failed));
7845 Set_Etype (N, Target_Type);
7847 -- When the operand is a selected access discriminant the check needs
7848 -- to be made against the level of the object denoted by the prefix
7849 -- of the selected name. Force Program_Error for this case as well
7850 -- (this accessibility violation can only happen if within the body
7851 -- of an instantiation).
7853 elsif In_Instance_Body
7854 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
7855 and then Nkind (Operand) = N_Selected_Component
7856 and then Object_Access_Level (Operand) >
7857 Type_Access_Level (Target_Type)
7860 Make_Raise_Program_Error (Sloc (N),
7861 Reason => PE_Accessibility_Check_Failed));
7862 Set_Etype (N, Target_Type);
7868 -- Case of conversions of tagged types and access to tagged types
7870 -- When needed, that is to say when the expression is class-wide, Add
7871 -- runtime a tag check for (strict) downward conversion by using the
7872 -- membership test, generating:
7874 -- [constraint_error when Operand not in Target_Type'Class]
7876 -- or in the access type case
7878 -- [constraint_error
7879 -- when Operand /= null
7880 -- and then Operand.all not in
7881 -- Designated_Type (Target_Type)'Class]
7883 if (Is_Access_Type (Target_Type)
7884 and then Is_Tagged_Type (Designated_Type (Target_Type)))
7885 or else Is_Tagged_Type (Target_Type)
7887 -- Do not do any expansion in the access type case if the parent is a
7888 -- renaming, since this is an error situation which will be caught by
7889 -- Sem_Ch8, and the expansion can interfere with this error check.
7891 if Is_Access_Type (Target_Type)
7892 and then Is_Renamed_Object (N)
7897 -- Otherwise, proceed with processing tagged conversion
7900 Actual_Op_Typ : Entity_Id;
7901 Actual_Targ_Typ : Entity_Id;
7902 Make_Conversion : Boolean := False;
7903 Root_Op_Typ : Entity_Id;
7905 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
7906 -- Create a membership check to test whether Operand is a member
7907 -- of Targ_Typ. If the original Target_Type is an access, include
7908 -- a test for null value. The check is inserted at N.
7910 --------------------
7911 -- Make_Tag_Check --
7912 --------------------
7914 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
7919 -- [Constraint_Error
7920 -- when Operand /= null
7921 -- and then Operand.all not in Targ_Typ]
7923 if Is_Access_Type (Target_Type) then
7928 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
7929 Right_Opnd => Make_Null (Loc)),
7934 Make_Explicit_Dereference (Loc,
7935 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
7936 Right_Opnd => New_Reference_To (Targ_Typ, Loc)));
7939 -- [Constraint_Error when Operand not in Targ_Typ]
7944 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
7945 Right_Opnd => New_Reference_To (Targ_Typ, Loc));
7949 Make_Raise_Constraint_Error (Loc,
7951 Reason => CE_Tag_Check_Failed));
7954 -- Start of processing
7957 if Is_Access_Type (Target_Type) then
7958 Actual_Op_Typ := Designated_Type (Operand_Type);
7959 Actual_Targ_Typ := Designated_Type (Target_Type);
7962 Actual_Op_Typ := Operand_Type;
7963 Actual_Targ_Typ := Target_Type;
7966 Root_Op_Typ := Root_Type (Actual_Op_Typ);
7968 -- Ada 2005 (AI-251): Handle interface type conversion
7970 if Is_Interface (Actual_Op_Typ) then
7971 Expand_Interface_Conversion (N, Is_Static => False);
7975 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
7977 -- Create a runtime tag check for a downward class-wide type
7980 if Is_Class_Wide_Type (Actual_Op_Typ)
7981 and then Root_Op_Typ /= Actual_Targ_Typ
7982 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ)
7984 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
7985 Make_Conversion := True;
7988 -- AI05-0073: If the result subtype of the function is defined
7989 -- by an access_definition designating a specific tagged type
7990 -- T, a check is made that the result value is null or the tag
7991 -- of the object designated by the result value identifies T.
7992 -- Constraint_Error is raised if this check fails.
7994 if Nkind (Parent (N)) = Sinfo.N_Return_Statement then
7997 Func_Typ : Entity_Id;
8000 -- Climb scope stack looking for the enclosing function
8002 Func := Current_Scope;
8003 while Present (Func)
8004 and then Ekind (Func) /= E_Function
8006 Func := Scope (Func);
8009 -- The function's return subtype must be defined using
8010 -- an access definition.
8012 if Nkind (Result_Definition (Parent (Func))) =
8015 Func_Typ := Directly_Designated_Type (Etype (Func));
8017 -- The return subtype denotes a specific tagged type,
8018 -- in other words, a non class-wide type.
8020 if Is_Tagged_Type (Func_Typ)
8021 and then not Is_Class_Wide_Type (Func_Typ)
8023 Make_Tag_Check (Actual_Targ_Typ);
8024 Make_Conversion := True;
8030 -- We have generated a tag check for either a class-wide type
8031 -- conversion or for AI05-0073.
8033 if Make_Conversion then
8038 Make_Unchecked_Type_Conversion (Loc,
8039 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
8040 Expression => Relocate_Node (Expression (N)));
8042 Analyze_And_Resolve (N, Target_Type);
8048 -- Case of other access type conversions
8050 elsif Is_Access_Type (Target_Type) then
8051 Apply_Constraint_Check (Operand, Target_Type);
8053 -- Case of conversions from a fixed-point type
8055 -- These conversions require special expansion and processing, found in
8056 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
8057 -- since from a semantic point of view, these are simple integer
8058 -- conversions, which do not need further processing.
8060 elsif Is_Fixed_Point_Type (Operand_Type)
8061 and then not Conversion_OK (N)
8063 -- We should never see universal fixed at this case, since the
8064 -- expansion of the constituent divide or multiply should have
8065 -- eliminated the explicit mention of universal fixed.
8067 pragma Assert (Operand_Type /= Universal_Fixed);
8069 -- Check for special case of the conversion to universal real that
8070 -- occurs as a result of the use of a round attribute. In this case,
8071 -- the real type for the conversion is taken from the target type of
8072 -- the Round attribute and the result must be marked as rounded.
8074 if Target_Type = Universal_Real
8075 and then Nkind (Parent (N)) = N_Attribute_Reference
8076 and then Attribute_Name (Parent (N)) = Name_Round
8078 Set_Rounded_Result (N);
8079 Set_Etype (N, Etype (Parent (N)));
8082 -- Otherwise do correct fixed-conversion, but skip these if the
8083 -- Conversion_OK flag is set, because from a semantic point of
8084 -- view these are simple integer conversions needing no further
8085 -- processing (the backend will simply treat them as integers)
8087 if not Conversion_OK (N) then
8088 if Is_Fixed_Point_Type (Etype (N)) then
8089 Expand_Convert_Fixed_To_Fixed (N);
8092 elsif Is_Integer_Type (Etype (N)) then
8093 Expand_Convert_Fixed_To_Integer (N);
8096 pragma Assert (Is_Floating_Point_Type (Etype (N)));
8097 Expand_Convert_Fixed_To_Float (N);
8102 -- Case of conversions to a fixed-point type
8104 -- These conversions require special expansion and processing, found in
8105 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
8106 -- since from a semantic point of view, these are simple integer
8107 -- conversions, which do not need further processing.
8109 elsif Is_Fixed_Point_Type (Target_Type)
8110 and then not Conversion_OK (N)
8112 if Is_Integer_Type (Operand_Type) then
8113 Expand_Convert_Integer_To_Fixed (N);
8116 pragma Assert (Is_Floating_Point_Type (Operand_Type));
8117 Expand_Convert_Float_To_Fixed (N);
8121 -- Case of float-to-integer conversions
8123 -- We also handle float-to-fixed conversions with Conversion_OK set
8124 -- since semantically the fixed-point target is treated as though it
8125 -- were an integer in such cases.
8127 elsif Is_Floating_Point_Type (Operand_Type)
8129 (Is_Integer_Type (Target_Type)
8131 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
8133 -- One more check here, gcc is still not able to do conversions of
8134 -- this type with proper overflow checking, and so gigi is doing an
8135 -- approximation of what is required by doing floating-point compares
8136 -- with the end-point. But that can lose precision in some cases, and
8137 -- give a wrong result. Converting the operand to Universal_Real is
8138 -- helpful, but still does not catch all cases with 64-bit integers
8139 -- on targets with only 64-bit floats
8141 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
8142 -- Can this code be removed ???
8144 if Do_Range_Check (Operand) then
8146 Make_Type_Conversion (Loc,
8148 New_Occurrence_Of (Universal_Real, Loc),
8150 Relocate_Node (Operand)));
8152 Set_Etype (Operand, Universal_Real);
8153 Enable_Range_Check (Operand);
8154 Set_Do_Range_Check (Expression (Operand), False);
8157 -- Case of array conversions
8159 -- Expansion of array conversions, add required length/range checks but
8160 -- only do this if there is no change of representation. For handling of
8161 -- this case, see Handle_Changed_Representation.
8163 elsif Is_Array_Type (Target_Type) then
8165 if Is_Constrained (Target_Type) then
8166 Apply_Length_Check (Operand, Target_Type);
8168 Apply_Range_Check (Operand, Target_Type);
8171 Handle_Changed_Representation;
8173 -- Case of conversions of discriminated types
8175 -- Add required discriminant checks if target is constrained. Again this
8176 -- change is skipped if we have a change of representation.
8178 elsif Has_Discriminants (Target_Type)
8179 and then Is_Constrained (Target_Type)
8181 Apply_Discriminant_Check (Operand, Target_Type);
8182 Handle_Changed_Representation;
8184 -- Case of all other record conversions. The only processing required
8185 -- is to check for a change of representation requiring the special
8186 -- assignment processing.
8188 elsif Is_Record_Type (Target_Type) then
8190 -- Ada 2005 (AI-216): Program_Error is raised when converting from
8191 -- a derived Unchecked_Union type to an unconstrained type that is
8192 -- not Unchecked_Union if the operand lacks inferable discriminants.
8194 if Is_Derived_Type (Operand_Type)
8195 and then Is_Unchecked_Union (Base_Type (Operand_Type))
8196 and then not Is_Constrained (Target_Type)
8197 and then not Is_Unchecked_Union (Base_Type (Target_Type))
8198 and then not Has_Inferable_Discriminants (Operand)
8200 -- To prevent Gigi from generating illegal code, we generate a
8201 -- Program_Error node, but we give it the target type of the
8205 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
8206 Reason => PE_Unchecked_Union_Restriction);
8209 Set_Etype (PE, Target_Type);
8214 Handle_Changed_Representation;
8217 -- Case of conversions of enumeration types
8219 elsif Is_Enumeration_Type (Target_Type) then
8221 -- Special processing is required if there is a change of
8222 -- representation (from enumeration representation clauses)
8224 if not Same_Representation (Target_Type, Operand_Type) then
8226 -- Convert: x(y) to x'val (ytyp'val (y))
8229 Make_Attribute_Reference (Loc,
8230 Prefix => New_Occurrence_Of (Target_Type, Loc),
8231 Attribute_Name => Name_Val,
8232 Expressions => New_List (
8233 Make_Attribute_Reference (Loc,
8234 Prefix => New_Occurrence_Of (Operand_Type, Loc),
8235 Attribute_Name => Name_Pos,
8236 Expressions => New_List (Operand)))));
8238 Analyze_And_Resolve (N, Target_Type);
8241 -- Case of conversions to floating-point
8243 elsif Is_Floating_Point_Type (Target_Type) then
8247 -- At this stage, either the conversion node has been transformed into
8248 -- some other equivalent expression, or left as a conversion that can
8249 -- be handled by Gigi. The conversions that Gigi can handle are the
8252 -- Conversions with no change of representation or type
8254 -- Numeric conversions involving integer, floating- and fixed-point
8255 -- values. Fixed-point values are allowed only if Conversion_OK is
8256 -- set, i.e. if the fixed-point values are to be treated as integers.
8258 -- No other conversions should be passed to Gigi
8260 -- Check: are these rules stated in sinfo??? if so, why restate here???
8262 -- The only remaining step is to generate a range check if we still have
8263 -- a type conversion at this stage and Do_Range_Check is set. For now we
8264 -- do this only for conversions of discrete types.
8266 if Nkind (N) = N_Type_Conversion
8267 and then Is_Discrete_Type (Etype (N))
8270 Expr : constant Node_Id := Expression (N);
8275 if Do_Range_Check (Expr)
8276 and then Is_Discrete_Type (Etype (Expr))
8278 Set_Do_Range_Check (Expr, False);
8280 -- Before we do a range check, we have to deal with treating a
8281 -- fixed-point operand as an integer. The way we do this is
8282 -- simply to do an unchecked conversion to an appropriate
8283 -- integer type large enough to hold the result.
8285 -- This code is not active yet, because we are only dealing
8286 -- with discrete types so far ???
8288 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
8289 and then Treat_Fixed_As_Integer (Expr)
8291 Ftyp := Base_Type (Etype (Expr));
8293 if Esize (Ftyp) >= Esize (Standard_Integer) then
8294 Ityp := Standard_Long_Long_Integer;
8296 Ityp := Standard_Integer;
8299 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
8302 -- Reset overflow flag, since the range check will include
8303 -- dealing with possible overflow, and generate the check If
8304 -- Address is either a source type or target type, suppress
8305 -- range check to avoid typing anomalies when it is a visible
8308 Set_Do_Overflow_Check (N, False);
8309 if not Is_Descendent_Of_Address (Etype (Expr))
8310 and then not Is_Descendent_Of_Address (Target_Type)
8312 Generate_Range_Check
8313 (Expr, Target_Type, CE_Range_Check_Failed);
8319 -- Final step, if the result is a type conversion involving Vax_Float
8320 -- types, then it is subject for further special processing.
8322 if Nkind (N) = N_Type_Conversion
8323 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
8325 Expand_Vax_Conversion (N);
8328 end Expand_N_Type_Conversion;
8330 -----------------------------------
8331 -- Expand_N_Unchecked_Expression --
8332 -----------------------------------
8334 -- Remove the unchecked expression node from the tree. It's job was simply
8335 -- to make sure that its constituent expression was handled with checks
8336 -- off, and now that that is done, we can remove it from the tree, and
8337 -- indeed must, since gigi does not expect to see these nodes.
8339 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
8340 Exp : constant Node_Id := Expression (N);
8343 Set_Assignment_OK (Exp, Assignment_OK (N) or Assignment_OK (Exp));
8345 end Expand_N_Unchecked_Expression;
8347 ----------------------------------------
8348 -- Expand_N_Unchecked_Type_Conversion --
8349 ----------------------------------------
8351 -- If this cannot be handled by Gigi and we haven't already made a
8352 -- temporary for it, do it now.
8354 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
8355 Target_Type : constant Entity_Id := Etype (N);
8356 Operand : constant Node_Id := Expression (N);
8357 Operand_Type : constant Entity_Id := Etype (Operand);
8360 -- If we have a conversion of a compile time known value to a target
8361 -- type and the value is in range of the target type, then we can simply
8362 -- replace the construct by an integer literal of the correct type. We
8363 -- only apply this to integer types being converted. Possibly it may
8364 -- apply in other cases, but it is too much trouble to worry about.
8366 -- Note that we do not do this transformation if the Kill_Range_Check
8367 -- flag is set, since then the value may be outside the expected range.
8368 -- This happens in the Normalize_Scalars case.
8370 -- We also skip this if either the target or operand type is biased
8371 -- because in this case, the unchecked conversion is supposed to
8372 -- preserve the bit pattern, not the integer value.
8374 if Is_Integer_Type (Target_Type)
8375 and then not Has_Biased_Representation (Target_Type)
8376 and then Is_Integer_Type (Operand_Type)
8377 and then not Has_Biased_Representation (Operand_Type)
8378 and then Compile_Time_Known_Value (Operand)
8379 and then not Kill_Range_Check (N)
8382 Val : constant Uint := Expr_Value (Operand);
8385 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
8387 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
8389 Val >= Expr_Value (Type_Low_Bound (Target_Type))
8391 Val <= Expr_Value (Type_High_Bound (Target_Type))
8393 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
8395 -- If Address is the target type, just set the type to avoid a
8396 -- spurious type error on the literal when Address is a visible
8399 if Is_Descendent_Of_Address (Target_Type) then
8400 Set_Etype (N, Target_Type);
8402 Analyze_And_Resolve (N, Target_Type);
8410 -- Nothing to do if conversion is safe
8412 if Safe_Unchecked_Type_Conversion (N) then
8416 -- Otherwise force evaluation unless Assignment_OK flag is set (this
8417 -- flag indicates ??? -- more comments needed here)
8419 if Assignment_OK (N) then
8422 Force_Evaluation (N);
8424 end Expand_N_Unchecked_Type_Conversion;
8426 ----------------------------
8427 -- Expand_Record_Equality --
8428 ----------------------------
8430 -- For non-variant records, Equality is expanded when needed into:
8432 -- and then Lhs.Discr1 = Rhs.Discr1
8434 -- and then Lhs.Discrn = Rhs.Discrn
8435 -- and then Lhs.Cmp1 = Rhs.Cmp1
8437 -- and then Lhs.Cmpn = Rhs.Cmpn
8439 -- The expression is folded by the back-end for adjacent fields. This
8440 -- function is called for tagged record in only one occasion: for imple-
8441 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
8442 -- otherwise the primitive "=" is used directly.
8444 function Expand_Record_Equality
8449 Bodies : List_Id) return Node_Id
8451 Loc : constant Source_Ptr := Sloc (Nod);
8456 First_Time : Boolean := True;
8458 function Suitable_Element (C : Entity_Id) return Entity_Id;
8459 -- Return the first field to compare beginning with C, skipping the
8460 -- inherited components.
8462 ----------------------
8463 -- Suitable_Element --
8464 ----------------------
8466 function Suitable_Element (C : Entity_Id) return Entity_Id is
8471 elsif Ekind (C) /= E_Discriminant
8472 and then Ekind (C) /= E_Component
8474 return Suitable_Element (Next_Entity (C));
8476 elsif Is_Tagged_Type (Typ)
8477 and then C /= Original_Record_Component (C)
8479 return Suitable_Element (Next_Entity (C));
8481 elsif Chars (C) = Name_uController
8482 or else Chars (C) = Name_uTag
8484 return Suitable_Element (Next_Entity (C));
8486 elsif Is_Interface (Etype (C)) then
8487 return Suitable_Element (Next_Entity (C));
8492 end Suitable_Element;
8494 -- Start of processing for Expand_Record_Equality
8497 -- Generates the following code: (assuming that Typ has one Discr and
8498 -- component C2 is also a record)
8501 -- and then Lhs.Discr1 = Rhs.Discr1
8502 -- and then Lhs.C1 = Rhs.C1
8503 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
8505 -- and then Lhs.Cmpn = Rhs.Cmpn
8507 Result := New_Reference_To (Standard_True, Loc);
8508 C := Suitable_Element (First_Entity (Typ));
8510 while Present (C) loop
8518 First_Time := False;
8522 New_Lhs := New_Copy_Tree (Lhs);
8523 New_Rhs := New_Copy_Tree (Rhs);
8527 Expand_Composite_Equality (Nod, Etype (C),
8529 Make_Selected_Component (Loc,
8531 Selector_Name => New_Reference_To (C, Loc)),
8533 Make_Selected_Component (Loc,
8535 Selector_Name => New_Reference_To (C, Loc)),
8538 -- If some (sub)component is an unchecked_union, the whole
8539 -- operation will raise program error.
8541 if Nkind (Check) = N_Raise_Program_Error then
8543 Set_Etype (Result, Standard_Boolean);
8548 Left_Opnd => Result,
8549 Right_Opnd => Check);
8553 C := Suitable_Element (Next_Entity (C));
8557 end Expand_Record_Equality;
8559 -------------------------------------
8560 -- Fixup_Universal_Fixed_Operation --
8561 -------------------------------------
8563 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
8564 Conv : constant Node_Id := Parent (N);
8567 -- We must have a type conversion immediately above us
8569 pragma Assert (Nkind (Conv) = N_Type_Conversion);
8571 -- Normally the type conversion gives our target type. The exception
8572 -- occurs in the case of the Round attribute, where the conversion
8573 -- will be to universal real, and our real type comes from the Round
8574 -- attribute (as well as an indication that we must round the result)
8576 if Nkind (Parent (Conv)) = N_Attribute_Reference
8577 and then Attribute_Name (Parent (Conv)) = Name_Round
8579 Set_Etype (N, Etype (Parent (Conv)));
8580 Set_Rounded_Result (N);
8582 -- Normal case where type comes from conversion above us
8585 Set_Etype (N, Etype (Conv));
8587 end Fixup_Universal_Fixed_Operation;
8589 ------------------------------
8590 -- Get_Allocator_Final_List --
8591 ------------------------------
8593 function Get_Allocator_Final_List
8596 PtrT : Entity_Id) return Entity_Id
8598 Loc : constant Source_Ptr := Sloc (N);
8600 Owner : Entity_Id := PtrT;
8601 -- The entity whose finalization list must be used to attach the
8602 -- allocated object.
8605 if Ekind (PtrT) = E_Anonymous_Access_Type then
8607 -- If the context is an access parameter, we need to create a
8608 -- non-anonymous access type in order to have a usable final list,
8609 -- because there is otherwise no pool to which the allocated object
8610 -- can belong. We create both the type and the finalization chain
8611 -- here, because freezing an internal type does not create such a
8612 -- chain. The Final_Chain that is thus created is shared by the
8613 -- access parameter. The access type is tested against the result
8614 -- type of the function to exclude allocators whose type is an
8615 -- anonymous access result type. We freeze the type at once to
8616 -- ensure that it is properly decorated for the back-end, even
8617 -- if the context and current scope is a loop.
8619 if Nkind (Associated_Node_For_Itype (PtrT))
8620 in N_Subprogram_Specification
8623 Etype (Defining_Unit_Name (Associated_Node_For_Itype (PtrT)))
8625 Owner := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
8627 Make_Full_Type_Declaration (Loc,
8628 Defining_Identifier => Owner,
8630 Make_Access_To_Object_Definition (Loc,
8631 Subtype_Indication =>
8632 New_Occurrence_Of (T, Loc))));
8634 Freeze_Before (N, Owner);
8635 Build_Final_List (N, Owner);
8636 Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner));
8638 -- Ada 2005 (AI-318-02): If the context is a return object
8639 -- declaration, then the anonymous return subtype is defined to have
8640 -- the same accessibility level as that of the function's result
8641 -- subtype, which means that we want the scope where the function is
8644 elsif Nkind (Associated_Node_For_Itype (PtrT)) = N_Object_Declaration
8645 and then Ekind (Scope (PtrT)) = E_Return_Statement
8647 Owner := Scope (Return_Applies_To (Scope (PtrT)));
8649 -- Case of an access discriminant, or (Ada 2005), of an anonymous
8650 -- access component or anonymous access function result: find the
8651 -- final list associated with the scope of the type. (In the
8652 -- anonymous access component kind, a list controller will have
8653 -- been allocated when freezing the record type, and PtrT has an
8654 -- Associated_Final_Chain attribute designating it.)
8656 elsif No (Associated_Final_Chain (PtrT)) then
8657 Owner := Scope (PtrT);
8661 return Find_Final_List (Owner);
8662 end Get_Allocator_Final_List;
8664 ---------------------------------
8665 -- Has_Inferable_Discriminants --
8666 ---------------------------------
8668 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
8670 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
8671 -- Determines whether the left-most prefix of a selected component is a
8672 -- formal parameter in a subprogram. Assumes N is a selected component.
8674 --------------------------------
8675 -- Prefix_Is_Formal_Parameter --
8676 --------------------------------
8678 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
8679 Sel_Comp : Node_Id := N;
8682 -- Move to the left-most prefix by climbing up the tree
8684 while Present (Parent (Sel_Comp))
8685 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
8687 Sel_Comp := Parent (Sel_Comp);
8690 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
8691 end Prefix_Is_Formal_Parameter;
8693 -- Start of processing for Has_Inferable_Discriminants
8696 -- For identifiers and indexed components, it is sufficient to have a
8697 -- constrained Unchecked_Union nominal subtype.
8699 if Nkind_In (N, N_Identifier, N_Indexed_Component) then
8700 return Is_Unchecked_Union (Base_Type (Etype (N)))
8702 Is_Constrained (Etype (N));
8704 -- For selected components, the subtype of the selector must be a
8705 -- constrained Unchecked_Union. If the component is subject to a
8706 -- per-object constraint, then the enclosing object must have inferable
8709 elsif Nkind (N) = N_Selected_Component then
8710 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
8712 -- A small hack. If we have a per-object constrained selected
8713 -- component of a formal parameter, return True since we do not
8714 -- know the actual parameter association yet.
8716 if Prefix_Is_Formal_Parameter (N) then
8720 -- Otherwise, check the enclosing object and the selector
8722 return Has_Inferable_Discriminants (Prefix (N))
8724 Has_Inferable_Discriminants (Selector_Name (N));
8727 -- The call to Has_Inferable_Discriminants will determine whether
8728 -- the selector has a constrained Unchecked_Union nominal type.
8730 return Has_Inferable_Discriminants (Selector_Name (N));
8732 -- A qualified expression has inferable discriminants if its subtype
8733 -- mark is a constrained Unchecked_Union subtype.
8735 elsif Nkind (N) = N_Qualified_Expression then
8736 return Is_Unchecked_Union (Subtype_Mark (N))
8738 Is_Constrained (Subtype_Mark (N));
8743 end Has_Inferable_Discriminants;
8745 -------------------------------
8746 -- Insert_Dereference_Action --
8747 -------------------------------
8749 procedure Insert_Dereference_Action (N : Node_Id) is
8750 Loc : constant Source_Ptr := Sloc (N);
8751 Typ : constant Entity_Id := Etype (N);
8752 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
8753 Pnod : constant Node_Id := Parent (N);
8755 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
8756 -- Return true if type of P is derived from Checked_Pool;
8758 -----------------------------
8759 -- Is_Checked_Storage_Pool --
8760 -----------------------------
8762 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
8771 while T /= Etype (T) loop
8772 if Is_RTE (T, RE_Checked_Pool) then
8780 end Is_Checked_Storage_Pool;
8782 -- Start of processing for Insert_Dereference_Action
8785 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
8787 if not (Is_Checked_Storage_Pool (Pool)
8788 and then Comes_From_Source (Original_Node (Pnod)))
8794 Make_Procedure_Call_Statement (Loc,
8795 Name => New_Reference_To (
8796 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
8798 Parameter_Associations => New_List (
8802 New_Reference_To (Pool, Loc),
8804 -- Storage_Address. We use the attribute Pool_Address, which uses
8805 -- the pointer itself to find the address of the object, and which
8806 -- handles unconstrained arrays properly by computing the address
8807 -- of the template. i.e. the correct address of the corresponding
8810 Make_Attribute_Reference (Loc,
8811 Prefix => Duplicate_Subexpr_Move_Checks (N),
8812 Attribute_Name => Name_Pool_Address),
8814 -- Size_In_Storage_Elements
8816 Make_Op_Divide (Loc,
8818 Make_Attribute_Reference (Loc,
8820 Make_Explicit_Dereference (Loc,
8821 Duplicate_Subexpr_Move_Checks (N)),
8822 Attribute_Name => Name_Size),
8824 Make_Integer_Literal (Loc, System_Storage_Unit)),
8828 Make_Attribute_Reference (Loc,
8830 Make_Explicit_Dereference (Loc,
8831 Duplicate_Subexpr_Move_Checks (N)),
8832 Attribute_Name => Name_Alignment))));
8835 when RE_Not_Available =>
8837 end Insert_Dereference_Action;
8839 ------------------------------
8840 -- Make_Array_Comparison_Op --
8841 ------------------------------
8843 -- This is a hand-coded expansion of the following generic function:
8846 -- type elem is (<>);
8847 -- type index is (<>);
8848 -- type a is array (index range <>) of elem;
8850 -- function Gnnn (X : a; Y: a) return boolean is
8851 -- J : index := Y'first;
8854 -- if X'length = 0 then
8857 -- elsif Y'length = 0 then
8861 -- for I in X'range loop
8862 -- if X (I) = Y (J) then
8863 -- if J = Y'last then
8866 -- J := index'succ (J);
8870 -- return X (I) > Y (J);
8874 -- return X'length > Y'length;
8878 -- Note that since we are essentially doing this expansion by hand, we
8879 -- do not need to generate an actual or formal generic part, just the
8880 -- instantiated function itself.
8882 function Make_Array_Comparison_Op
8884 Nod : Node_Id) return Node_Id
8886 Loc : constant Source_Ptr := Sloc (Nod);
8888 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
8889 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
8890 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
8891 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
8893 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
8895 Loop_Statement : Node_Id;
8896 Loop_Body : Node_Id;
8899 Final_Expr : Node_Id;
8900 Func_Body : Node_Id;
8901 Func_Name : Entity_Id;
8907 -- if J = Y'last then
8910 -- J := index'succ (J);
8914 Make_Implicit_If_Statement (Nod,
8917 Left_Opnd => New_Reference_To (J, Loc),
8919 Make_Attribute_Reference (Loc,
8920 Prefix => New_Reference_To (Y, Loc),
8921 Attribute_Name => Name_Last)),
8923 Then_Statements => New_List (
8924 Make_Exit_Statement (Loc)),
8928 Make_Assignment_Statement (Loc,
8929 Name => New_Reference_To (J, Loc),
8931 Make_Attribute_Reference (Loc,
8932 Prefix => New_Reference_To (Index, Loc),
8933 Attribute_Name => Name_Succ,
8934 Expressions => New_List (New_Reference_To (J, Loc))))));
8936 -- if X (I) = Y (J) then
8939 -- return X (I) > Y (J);
8943 Make_Implicit_If_Statement (Nod,
8947 Make_Indexed_Component (Loc,
8948 Prefix => New_Reference_To (X, Loc),
8949 Expressions => New_List (New_Reference_To (I, Loc))),
8952 Make_Indexed_Component (Loc,
8953 Prefix => New_Reference_To (Y, Loc),
8954 Expressions => New_List (New_Reference_To (J, Loc)))),
8956 Then_Statements => New_List (Inner_If),
8958 Else_Statements => New_List (
8959 Make_Simple_Return_Statement (Loc,
8963 Make_Indexed_Component (Loc,
8964 Prefix => New_Reference_To (X, Loc),
8965 Expressions => New_List (New_Reference_To (I, Loc))),
8968 Make_Indexed_Component (Loc,
8969 Prefix => New_Reference_To (Y, Loc),
8970 Expressions => New_List (
8971 New_Reference_To (J, Loc)))))));
8973 -- for I in X'range loop
8978 Make_Implicit_Loop_Statement (Nod,
8979 Identifier => Empty,
8982 Make_Iteration_Scheme (Loc,
8983 Loop_Parameter_Specification =>
8984 Make_Loop_Parameter_Specification (Loc,
8985 Defining_Identifier => I,
8986 Discrete_Subtype_Definition =>
8987 Make_Attribute_Reference (Loc,
8988 Prefix => New_Reference_To (X, Loc),
8989 Attribute_Name => Name_Range))),
8991 Statements => New_List (Loop_Body));
8993 -- if X'length = 0 then
8995 -- elsif Y'length = 0 then
8998 -- for ... loop ... end loop;
8999 -- return X'length > Y'length;
9003 Make_Attribute_Reference (Loc,
9004 Prefix => New_Reference_To (X, Loc),
9005 Attribute_Name => Name_Length);
9008 Make_Attribute_Reference (Loc,
9009 Prefix => New_Reference_To (Y, Loc),
9010 Attribute_Name => Name_Length);
9014 Left_Opnd => Length1,
9015 Right_Opnd => Length2);
9018 Make_Implicit_If_Statement (Nod,
9022 Make_Attribute_Reference (Loc,
9023 Prefix => New_Reference_To (X, Loc),
9024 Attribute_Name => Name_Length),
9026 Make_Integer_Literal (Loc, 0)),
9030 Make_Simple_Return_Statement (Loc,
9031 Expression => New_Reference_To (Standard_False, Loc))),
9033 Elsif_Parts => New_List (
9034 Make_Elsif_Part (Loc,
9038 Make_Attribute_Reference (Loc,
9039 Prefix => New_Reference_To (Y, Loc),
9040 Attribute_Name => Name_Length),
9042 Make_Integer_Literal (Loc, 0)),
9046 Make_Simple_Return_Statement (Loc,
9047 Expression => New_Reference_To (Standard_True, Loc))))),
9049 Else_Statements => New_List (
9051 Make_Simple_Return_Statement (Loc,
9052 Expression => Final_Expr)));
9056 Formals := New_List (
9057 Make_Parameter_Specification (Loc,
9058 Defining_Identifier => X,
9059 Parameter_Type => New_Reference_To (Typ, Loc)),
9061 Make_Parameter_Specification (Loc,
9062 Defining_Identifier => Y,
9063 Parameter_Type => New_Reference_To (Typ, Loc)));
9065 -- function Gnnn (...) return boolean is
9066 -- J : index := Y'first;
9071 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('G'));
9074 Make_Subprogram_Body (Loc,
9076 Make_Function_Specification (Loc,
9077 Defining_Unit_Name => Func_Name,
9078 Parameter_Specifications => Formals,
9079 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
9081 Declarations => New_List (
9082 Make_Object_Declaration (Loc,
9083 Defining_Identifier => J,
9084 Object_Definition => New_Reference_To (Index, Loc),
9086 Make_Attribute_Reference (Loc,
9087 Prefix => New_Reference_To (Y, Loc),
9088 Attribute_Name => Name_First))),
9090 Handled_Statement_Sequence =>
9091 Make_Handled_Sequence_Of_Statements (Loc,
9092 Statements => New_List (If_Stat)));
9095 end Make_Array_Comparison_Op;
9097 ---------------------------
9098 -- Make_Boolean_Array_Op --
9099 ---------------------------
9101 -- For logical operations on boolean arrays, expand in line the following,
9102 -- replacing 'and' with 'or' or 'xor' where needed:
9104 -- function Annn (A : typ; B: typ) return typ is
9107 -- for J in A'range loop
9108 -- C (J) := A (J) op B (J);
9113 -- Here typ is the boolean array type
9115 function Make_Boolean_Array_Op
9117 N : Node_Id) return Node_Id
9119 Loc : constant Source_Ptr := Sloc (N);
9121 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
9122 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
9123 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
9124 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
9132 Func_Name : Entity_Id;
9133 Func_Body : Node_Id;
9134 Loop_Statement : Node_Id;
9138 Make_Indexed_Component (Loc,
9139 Prefix => New_Reference_To (A, Loc),
9140 Expressions => New_List (New_Reference_To (J, Loc)));
9143 Make_Indexed_Component (Loc,
9144 Prefix => New_Reference_To (B, Loc),
9145 Expressions => New_List (New_Reference_To (J, Loc)));
9148 Make_Indexed_Component (Loc,
9149 Prefix => New_Reference_To (C, Loc),
9150 Expressions => New_List (New_Reference_To (J, Loc)));
9152 if Nkind (N) = N_Op_And then
9158 elsif Nkind (N) = N_Op_Or then
9172 Make_Implicit_Loop_Statement (N,
9173 Identifier => Empty,
9176 Make_Iteration_Scheme (Loc,
9177 Loop_Parameter_Specification =>
9178 Make_Loop_Parameter_Specification (Loc,
9179 Defining_Identifier => J,
9180 Discrete_Subtype_Definition =>
9181 Make_Attribute_Reference (Loc,
9182 Prefix => New_Reference_To (A, Loc),
9183 Attribute_Name => Name_Range))),
9185 Statements => New_List (
9186 Make_Assignment_Statement (Loc,
9188 Expression => Op)));
9190 Formals := New_List (
9191 Make_Parameter_Specification (Loc,
9192 Defining_Identifier => A,
9193 Parameter_Type => New_Reference_To (Typ, Loc)),
9195 Make_Parameter_Specification (Loc,
9196 Defining_Identifier => B,
9197 Parameter_Type => New_Reference_To (Typ, Loc)));
9200 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
9201 Set_Is_Inlined (Func_Name);
9204 Make_Subprogram_Body (Loc,
9206 Make_Function_Specification (Loc,
9207 Defining_Unit_Name => Func_Name,
9208 Parameter_Specifications => Formals,
9209 Result_Definition => New_Reference_To (Typ, Loc)),
9211 Declarations => New_List (
9212 Make_Object_Declaration (Loc,
9213 Defining_Identifier => C,
9214 Object_Definition => New_Reference_To (Typ, Loc))),
9216 Handled_Statement_Sequence =>
9217 Make_Handled_Sequence_Of_Statements (Loc,
9218 Statements => New_List (
9220 Make_Simple_Return_Statement (Loc,
9221 Expression => New_Reference_To (C, Loc)))));
9224 end Make_Boolean_Array_Op;
9226 ------------------------
9227 -- Rewrite_Comparison --
9228 ------------------------
9230 procedure Rewrite_Comparison (N : Node_Id) is
9231 Warning_Generated : Boolean := False;
9232 -- Set to True if first pass with Assume_Valid generates a warning in
9233 -- which case we skip the second pass to avoid warning overloaded.
9236 -- Set to Standard_True or Standard_False
9239 if Nkind (N) = N_Type_Conversion then
9240 Rewrite_Comparison (Expression (N));
9243 elsif Nkind (N) not in N_Op_Compare then
9247 -- Now start looking at the comparison in detail. We potentially go
9248 -- through this loop twice. The first time, Assume_Valid is set False
9249 -- in the call to Compile_Time_Compare. If this call results in a
9250 -- clear result of always True or Always False, that's decisive and
9251 -- we are done. Otherwise we repeat the processing with Assume_Valid
9252 -- set to True to generate additional warnings. We can stil that step
9253 -- if Constant_Condition_Warnings is False.
9255 for AV in False .. True loop
9257 Typ : constant Entity_Id := Etype (N);
9258 Op1 : constant Node_Id := Left_Opnd (N);
9259 Op2 : constant Node_Id := Right_Opnd (N);
9261 Res : constant Compare_Result :=
9262 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
9263 -- Res indicates if compare outcome can be compile time determined
9265 True_Result : Boolean;
9266 False_Result : Boolean;
9269 case N_Op_Compare (Nkind (N)) is
9271 True_Result := Res = EQ;
9272 False_Result := Res = LT or else Res = GT or else Res = NE;
9275 True_Result := Res in Compare_GE;
9276 False_Result := Res = LT;
9279 and then Constant_Condition_Warnings
9280 and then Comes_From_Source (Original_Node (N))
9281 and then Nkind (Original_Node (N)) = N_Op_Ge
9282 and then not In_Instance
9283 and then Is_Integer_Type (Etype (Left_Opnd (N)))
9284 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
9287 ("can never be greater than, could replace by ""'=""?", N);
9288 Warning_Generated := True;
9292 True_Result := Res = GT;
9293 False_Result := Res in Compare_LE;
9296 True_Result := Res = LT;
9297 False_Result := Res in Compare_GE;
9300 True_Result := Res in Compare_LE;
9301 False_Result := Res = GT;
9304 and then Constant_Condition_Warnings
9305 and then Comes_From_Source (Original_Node (N))
9306 and then Nkind (Original_Node (N)) = N_Op_Le
9307 and then not In_Instance
9308 and then Is_Integer_Type (Etype (Left_Opnd (N)))
9309 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
9312 ("can never be less than, could replace by ""'=""?", N);
9313 Warning_Generated := True;
9317 True_Result := Res = NE or else Res = GT or else Res = LT;
9318 False_Result := Res = EQ;
9321 -- If this is the first iteration, then we actually convert the
9322 -- comparison into True or False, if the result is certain.
9325 if True_Result or False_Result then
9327 Result := Standard_True;
9329 Result := Standard_False;
9334 New_Occurrence_Of (Result, Sloc (N))));
9335 Analyze_And_Resolve (N, Typ);
9336 Warn_On_Known_Condition (N);
9340 -- If this is the second iteration (AV = True), and the original
9341 -- node comes from source and we are not in an instance, then
9342 -- give a warning if we know result would be True or False. Note
9343 -- we know Constant_Condition_Warnings is set if we get here.
9345 elsif Comes_From_Source (Original_Node (N))
9346 and then not In_Instance
9350 ("condition can only be False if invalid values present?",
9352 elsif False_Result then
9354 ("condition can only be True if invalid values present?",
9360 -- Skip second iteration if not warning on constant conditions or
9361 -- if the first iteration already generated a warning of some kind
9362 -- or if we are in any case assuming all values are valid (so that
9363 -- the first iteration took care of the valid case).
9365 exit when not Constant_Condition_Warnings;
9366 exit when Warning_Generated;
9367 exit when Assume_No_Invalid_Values;
9369 end Rewrite_Comparison;
9371 ----------------------------
9372 -- Safe_In_Place_Array_Op --
9373 ----------------------------
9375 function Safe_In_Place_Array_Op
9378 Op2 : Node_Id) return Boolean
9382 function Is_Safe_Operand (Op : Node_Id) return Boolean;
9383 -- Operand is safe if it cannot overlap part of the target of the
9384 -- operation. If the operand and the target are identical, the operand
9385 -- is safe. The operand can be empty in the case of negation.
9387 function Is_Unaliased (N : Node_Id) return Boolean;
9388 -- Check that N is a stand-alone entity
9394 function Is_Unaliased (N : Node_Id) return Boolean is
9398 and then No (Address_Clause (Entity (N)))
9399 and then No (Renamed_Object (Entity (N)));
9402 ---------------------
9403 -- Is_Safe_Operand --
9404 ---------------------
9406 function Is_Safe_Operand (Op : Node_Id) return Boolean is
9411 elsif Is_Entity_Name (Op) then
9412 return Is_Unaliased (Op);
9414 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
9415 return Is_Unaliased (Prefix (Op));
9417 elsif Nkind (Op) = N_Slice then
9419 Is_Unaliased (Prefix (Op))
9420 and then Entity (Prefix (Op)) /= Target;
9422 elsif Nkind (Op) = N_Op_Not then
9423 return Is_Safe_Operand (Right_Opnd (Op));
9428 end Is_Safe_Operand;
9430 -- Start of processing for Is_Safe_In_Place_Array_Op
9433 -- Skip this processing if the component size is different from system
9434 -- storage unit (since at least for NOT this would cause problems).
9436 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
9439 -- Cannot do in place stuff on VM_Target since cannot pass addresses
9441 elsif VM_Target /= No_VM then
9444 -- Cannot do in place stuff if non-standard Boolean representation
9446 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
9449 elsif not Is_Unaliased (Lhs) then
9452 Target := Entity (Lhs);
9455 Is_Safe_Operand (Op1)
9456 and then Is_Safe_Operand (Op2);
9458 end Safe_In_Place_Array_Op;
9460 -----------------------
9461 -- Tagged_Membership --
9462 -----------------------
9464 -- There are two different cases to consider depending on whether the right
9465 -- operand is a class-wide type or not. If not we just compare the actual
9466 -- tag of the left expr to the target type tag:
9468 -- Left_Expr.Tag = Right_Type'Tag;
9470 -- If it is a class-wide type we use the RT function CW_Membership which is
9471 -- usually implemented by looking in the ancestor tables contained in the
9472 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
9474 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
9475 -- function IW_Membership which is usually implemented by looking in the
9476 -- table of abstract interface types plus the ancestor table contained in
9477 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
9479 function Tagged_Membership (N : Node_Id) return Node_Id is
9480 Left : constant Node_Id := Left_Opnd (N);
9481 Right : constant Node_Id := Right_Opnd (N);
9482 Loc : constant Source_Ptr := Sloc (N);
9484 Left_Type : Entity_Id;
9485 Right_Type : Entity_Id;
9489 Left_Type := Etype (Left);
9490 Right_Type := Etype (Right);
9492 if Is_Class_Wide_Type (Left_Type) then
9493 Left_Type := Root_Type (Left_Type);
9497 Make_Selected_Component (Loc,
9498 Prefix => Relocate_Node (Left),
9500 New_Reference_To (First_Tag_Component (Left_Type), Loc));
9502 if Is_Class_Wide_Type (Right_Type) then
9504 -- No need to issue a run-time check if we statically know that the
9505 -- result of this membership test is always true. For example,
9506 -- considering the following declarations:
9508 -- type Iface is interface;
9509 -- type T is tagged null record;
9510 -- type DT is new T and Iface with null record;
9515 -- These membership tests are always true:
9519 -- Obj2 in Iface'Class;
9521 -- We do not need to handle cases where the membership is illegal.
9524 -- Obj1 in DT'Class; -- Compile time error
9525 -- Obj1 in Iface'Class; -- Compile time error
9527 if not Is_Class_Wide_Type (Left_Type)
9528 and then (Is_Ancestor (Etype (Right_Type), Left_Type)
9529 or else (Is_Interface (Etype (Right_Type))
9530 and then Interface_Present_In_Ancestor
9532 Iface => Etype (Right_Type))))
9534 return New_Reference_To (Standard_True, Loc);
9537 -- Ada 2005 (AI-251): Class-wide applied to interfaces
9539 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
9541 -- Support to: "Iface_CW_Typ in Typ'Class"
9543 or else Is_Interface (Left_Type)
9545 -- Issue error if IW_Membership operation not available in a
9546 -- configurable run time setting.
9548 if not RTE_Available (RE_IW_Membership) then
9550 ("dynamic membership test on interface types", N);
9555 Make_Function_Call (Loc,
9556 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
9557 Parameter_Associations => New_List (
9558 Make_Attribute_Reference (Loc,
9560 Attribute_Name => Name_Address),
9563 (Access_Disp_Table (Root_Type (Right_Type)))),
9566 -- Ada 95: Normal case
9570 Build_CW_Membership (Loc,
9571 Obj_Tag_Node => Obj_Tag,
9575 (Access_Disp_Table (Root_Type (Right_Type)))),
9579 -- Right_Type is not a class-wide type
9582 -- No need to check the tag of the object if Right_Typ is abstract
9584 if Is_Abstract_Type (Right_Type) then
9585 return New_Reference_To (Standard_False, Loc);
9590 Left_Opnd => Obj_Tag,
9593 (Node (First_Elmt (Access_Disp_Table (Right_Type))), Loc));
9596 end Tagged_Membership;
9598 ------------------------------
9599 -- Unary_Op_Validity_Checks --
9600 ------------------------------
9602 procedure Unary_Op_Validity_Checks (N : Node_Id) is
9604 if Validity_Checks_On and Validity_Check_Operands then
9605 Ensure_Valid (Right_Opnd (N));
9607 end Unary_Op_Validity_Checks;