1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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 Par_SCO; use Par_SCO;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch3; use Sem_Ch3;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Res; use Sem_Res;
62 with Sem_Type; use Sem_Type;
63 with Sem_Util; use Sem_Util;
64 with Sem_Warn; use Sem_Warn;
65 with Sinfo; use Sinfo;
66 with Snames; use Snames;
67 with Stand; use Stand;
68 with SCIL_LL; use SCIL_LL;
69 with Targparm; use Targparm;
70 with Tbuild; use Tbuild;
71 with Ttypes; use Ttypes;
72 with Uintp; use Uintp;
73 with Urealp; use Urealp;
74 with Validsw; use Validsw;
76 package body Exp_Ch4 is
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Binary_Op_Validity_Checks (N : Node_Id);
83 pragma Inline (Binary_Op_Validity_Checks);
84 -- Performs validity checks for a binary operator
86 procedure Build_Boolean_Array_Proc_Call
90 -- If a boolean array assignment can be done in place, build call to
91 -- corresponding library procedure.
93 procedure Displace_Allocator_Pointer (N : Node_Id);
94 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
95 -- Expand_Allocator_Expression. Allocating class-wide interface objects
96 -- this routine displaces the pointer to the allocated object to reference
97 -- the component referencing the corresponding secondary dispatch table.
99 procedure Expand_Allocator_Expression (N : Node_Id);
100 -- Subsidiary to Expand_N_Allocator, for the case when the expression
101 -- is a qualified expression or an aggregate.
103 procedure Expand_Array_Comparison (N : Node_Id);
104 -- This routine handles expansion of the comparison operators (N_Op_Lt,
105 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
106 -- code for these operators is similar, differing only in the details of
107 -- the actual comparison call that is made. Special processing (call a
110 function Expand_Array_Equality
115 Typ : Entity_Id) return Node_Id;
116 -- Expand an array equality into a call to a function implementing this
117 -- equality, and a call to it. Loc is the location for the generated nodes.
118 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
119 -- on which to attach bodies of local functions that are created in the
120 -- process. It is the responsibility of the caller to insert those bodies
121 -- at the right place. Nod provides the Sloc value for the generated code.
122 -- Normally the types used for the generated equality routine are taken
123 -- from Lhs and Rhs. However, in some situations of generated code, the
124 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
125 -- the type to be used for the formal parameters.
127 procedure Expand_Boolean_Operator (N : Node_Id);
128 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
129 -- case of array type arguments.
131 procedure Expand_Short_Circuit_Operator (N : Node_Id);
132 -- Common expansion processing for short-circuit boolean operators
134 function Expand_Composite_Equality
139 Bodies : List_Id) return Node_Id;
140 -- Local recursive function used to expand equality for nested composite
141 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
142 -- to attach bodies of local functions that are created in the process.
143 -- This is the responsibility of the caller to insert those bodies at the
144 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
145 -- are the left and right sides for the comparison, and Typ is the type of
146 -- the arrays to compare.
148 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
149 -- Routine to expand concatenation of a sequence of two or more operands
150 -- (in the list Operands) and replace node Cnode with the result of the
151 -- concatenation. The operands can be of any appropriate type, and can
152 -- include both arrays and singleton elements.
154 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
155 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
156 -- fixed. We do not have such a type at runtime, so the purpose of this
157 -- routine is to find the real type by looking up the tree. We also
158 -- determine if the operation must be rounded.
160 function Get_Allocator_Final_List
163 PtrT : Entity_Id) return Entity_Id;
164 -- If the designated type is controlled, build final_list expression for
165 -- created object. If context is an access parameter, create a local access
166 -- type to have a usable finalization list.
168 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
169 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
170 -- discriminants if it has a constrained nominal type, unless the object
171 -- is a component of an enclosing Unchecked_Union object that is subject
172 -- to a per-object constraint and the enclosing object lacks inferable
175 -- An expression of an Unchecked_Union type has inferable discriminants
176 -- if it is either a name of an object with inferable discriminants or a
177 -- qualified expression whose subtype mark denotes a constrained subtype.
179 procedure Insert_Dereference_Action (N : Node_Id);
180 -- N is an expression whose type is an access. When the type of the
181 -- associated storage pool is derived from Checked_Pool, generate a
182 -- call to the 'Dereference' primitive operation.
184 function Make_Array_Comparison_Op
186 Nod : Node_Id) return Node_Id;
187 -- Comparisons between arrays are expanded in line. This function produces
188 -- the body of the implementation of (a > b), where a and b are one-
189 -- dimensional arrays of some discrete type. The original node is then
190 -- expanded into the appropriate call to this function. Nod provides the
191 -- Sloc value for the generated code.
193 function Make_Boolean_Array_Op
195 N : Node_Id) return Node_Id;
196 -- Boolean operations on boolean arrays are expanded in line. This function
197 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
198 -- b). It is used only the normal case and not the packed case. The type
199 -- involved, Typ, is the Boolean array type, and the logical operations in
200 -- the body are simple boolean operations. Note that Typ is always a
201 -- constrained type (the caller has ensured this by using
202 -- Convert_To_Actual_Subtype if necessary).
204 procedure Rewrite_Comparison (N : Node_Id);
205 -- If N is the node for a comparison whose outcome can be determined at
206 -- compile time, then the node N can be rewritten with True or False. If
207 -- the outcome cannot be determined at compile time, the call has no
208 -- effect. If N is a type conversion, then this processing is applied to
209 -- its expression. If N is neither comparison nor a type conversion, the
210 -- call has no effect.
212 procedure Tagged_Membership
214 SCIL_Node : out Node_Id;
215 Result : out Node_Id);
216 -- Construct the expression corresponding to the tagged membership test.
217 -- Deals with a second operand being (or not) a class-wide type.
219 function Safe_In_Place_Array_Op
222 Op2 : Node_Id) return Boolean;
223 -- In the context of an assignment, where the right-hand side is a boolean
224 -- operation on arrays, check whether operation can be performed in place.
226 procedure Unary_Op_Validity_Checks (N : Node_Id);
227 pragma Inline (Unary_Op_Validity_Checks);
228 -- Performs validity checks for a unary operator
230 -------------------------------
231 -- Binary_Op_Validity_Checks --
232 -------------------------------
234 procedure Binary_Op_Validity_Checks (N : Node_Id) is
236 if Validity_Checks_On and Validity_Check_Operands then
237 Ensure_Valid (Left_Opnd (N));
238 Ensure_Valid (Right_Opnd (N));
240 end Binary_Op_Validity_Checks;
242 ------------------------------------
243 -- Build_Boolean_Array_Proc_Call --
244 ------------------------------------
246 procedure Build_Boolean_Array_Proc_Call
251 Loc : constant Source_Ptr := Sloc (N);
252 Kind : constant Node_Kind := Nkind (Expression (N));
253 Target : constant Node_Id :=
254 Make_Attribute_Reference (Loc,
256 Attribute_Name => Name_Address);
258 Arg1 : constant Node_Id := Op1;
259 Arg2 : Node_Id := Op2;
261 Proc_Name : Entity_Id;
264 if Kind = N_Op_Not then
265 if Nkind (Op1) in N_Binary_Op then
267 -- Use negated version of the binary operators
269 if Nkind (Op1) = N_Op_And then
270 Proc_Name := RTE (RE_Vector_Nand);
272 elsif Nkind (Op1) = N_Op_Or then
273 Proc_Name := RTE (RE_Vector_Nor);
275 else pragma Assert (Nkind (Op1) = N_Op_Xor);
276 Proc_Name := RTE (RE_Vector_Xor);
280 Make_Procedure_Call_Statement (Loc,
281 Name => New_Occurrence_Of (Proc_Name, Loc),
283 Parameter_Associations => New_List (
285 Make_Attribute_Reference (Loc,
286 Prefix => Left_Opnd (Op1),
287 Attribute_Name => Name_Address),
289 Make_Attribute_Reference (Loc,
290 Prefix => Right_Opnd (Op1),
291 Attribute_Name => Name_Address),
293 Make_Attribute_Reference (Loc,
294 Prefix => Left_Opnd (Op1),
295 Attribute_Name => Name_Length)));
298 Proc_Name := RTE (RE_Vector_Not);
301 Make_Procedure_Call_Statement (Loc,
302 Name => New_Occurrence_Of (Proc_Name, Loc),
303 Parameter_Associations => New_List (
306 Make_Attribute_Reference (Loc,
308 Attribute_Name => Name_Address),
310 Make_Attribute_Reference (Loc,
312 Attribute_Name => Name_Length)));
316 -- We use the following equivalences:
318 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
319 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
320 -- (not X) xor (not Y) = X xor Y
321 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
323 if Nkind (Op1) = N_Op_Not then
324 if Kind = N_Op_And then
325 Proc_Name := RTE (RE_Vector_Nor);
326 elsif Kind = N_Op_Or then
327 Proc_Name := RTE (RE_Vector_Nand);
329 Proc_Name := RTE (RE_Vector_Xor);
333 if Kind = N_Op_And then
334 Proc_Name := RTE (RE_Vector_And);
335 elsif Kind = N_Op_Or then
336 Proc_Name := RTE (RE_Vector_Or);
337 elsif Nkind (Op2) = N_Op_Not then
338 Proc_Name := RTE (RE_Vector_Nxor);
339 Arg2 := Right_Opnd (Op2);
341 Proc_Name := RTE (RE_Vector_Xor);
346 Make_Procedure_Call_Statement (Loc,
347 Name => New_Occurrence_Of (Proc_Name, Loc),
348 Parameter_Associations => New_List (
350 Make_Attribute_Reference (Loc,
352 Attribute_Name => Name_Address),
353 Make_Attribute_Reference (Loc,
355 Attribute_Name => Name_Address),
356 Make_Attribute_Reference (Loc,
358 Attribute_Name => Name_Length)));
361 Rewrite (N, Call_Node);
365 when RE_Not_Available =>
367 end Build_Boolean_Array_Proc_Call;
369 --------------------------------
370 -- Displace_Allocator_Pointer --
371 --------------------------------
373 procedure Displace_Allocator_Pointer (N : Node_Id) is
374 Loc : constant Source_Ptr := Sloc (N);
375 Orig_Node : constant Node_Id := Original_Node (N);
381 -- Do nothing in case of VM targets: the virtual machine will handle
382 -- interfaces directly.
384 if not Tagged_Type_Expansion then
388 pragma Assert (Nkind (N) = N_Identifier
389 and then Nkind (Orig_Node) = N_Allocator);
391 PtrT := Etype (Orig_Node);
392 Dtyp := Available_View (Designated_Type (PtrT));
393 Etyp := Etype (Expression (Orig_Node));
395 if Is_Class_Wide_Type (Dtyp)
396 and then Is_Interface (Dtyp)
398 -- If the type of the allocator expression is not an interface type
399 -- we can generate code to reference the record component containing
400 -- the pointer to the secondary dispatch table.
402 if not Is_Interface (Etyp) then
404 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
407 -- 1) Get access to the allocated object
410 Make_Explicit_Dereference (Loc,
415 -- 2) Add the conversion to displace the pointer to reference
416 -- the secondary dispatch table.
418 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
419 Analyze_And_Resolve (N, Dtyp);
421 -- 3) The 'access to the secondary dispatch table will be used
422 -- as the value returned by the allocator.
425 Make_Attribute_Reference (Loc,
426 Prefix => Relocate_Node (N),
427 Attribute_Name => Name_Access));
428 Set_Etype (N, Saved_Typ);
432 -- If the type of the allocator expression is an interface type we
433 -- generate a run-time call to displace "this" to reference the
434 -- component containing the pointer to the secondary dispatch table
435 -- or else raise Constraint_Error if the actual object does not
436 -- implement the target interface. This case corresponds with the
437 -- following example:
439 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
441 -- return new Iface_2'Class'(Obj);
446 Unchecked_Convert_To (PtrT,
447 Make_Function_Call (Loc,
448 Name => New_Reference_To (RTE (RE_Displace), Loc),
449 Parameter_Associations => New_List (
450 Unchecked_Convert_To (RTE (RE_Address),
456 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
458 Analyze_And_Resolve (N, PtrT);
461 end Displace_Allocator_Pointer;
463 ---------------------------------
464 -- Expand_Allocator_Expression --
465 ---------------------------------
467 procedure Expand_Allocator_Expression (N : Node_Id) is
468 Loc : constant Source_Ptr := Sloc (N);
469 Exp : constant Node_Id := Expression (Expression (N));
470 PtrT : constant Entity_Id := Etype (N);
471 DesigT : constant Entity_Id := Designated_Type (PtrT);
473 procedure Apply_Accessibility_Check
475 Built_In_Place : Boolean := False);
476 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
477 -- type, generate an accessibility check to verify that the level of the
478 -- type of the created object is not deeper than the level of the access
479 -- type. If the type of the qualified expression is class- wide, then
480 -- always generate the check (except in the case where it is known to be
481 -- unnecessary, see comment below). Otherwise, only generate the check
482 -- if the level of the qualified expression type is statically deeper
483 -- than the access type.
485 -- Although the static accessibility will generally have been performed
486 -- as a legality check, it won't have been done in cases where the
487 -- allocator appears in generic body, so a run-time check is needed in
488 -- general. One special case is when the access type is declared in the
489 -- same scope as the class-wide allocator, in which case the check can
490 -- never fail, so it need not be generated.
492 -- As an open issue, there seem to be cases where the static level
493 -- associated with the class-wide object's underlying type is not
494 -- sufficient to perform the proper accessibility check, such as for
495 -- allocators in nested subprograms or accept statements initialized by
496 -- class-wide formals when the actual originates outside at a deeper
497 -- static level. The nested subprogram case might require passing
498 -- accessibility levels along with class-wide parameters, and the task
499 -- case seems to be an actual gap in the language rules that needs to
500 -- be fixed by the ARG. ???
502 -------------------------------
503 -- Apply_Accessibility_Check --
504 -------------------------------
506 procedure Apply_Accessibility_Check
508 Built_In_Place : Boolean := False)
513 -- Note: we skip the accessibility check for the VM case, since
514 -- there does not seem to be any practical way of implementing it.
516 if Ada_Version >= Ada_05
517 and then Tagged_Type_Expansion
518 and then Is_Class_Wide_Type (DesigT)
519 and then not Scope_Suppress (Accessibility_Check)
521 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
523 (Is_Class_Wide_Type (Etype (Exp))
524 and then Scope (PtrT) /= Current_Scope))
526 -- If the allocator was built in place Ref is already a reference
527 -- to the access object initialized to the result of the allocator
528 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise
529 -- it is the entity associated with the object containing the
530 -- address of the allocated object.
532 if Built_In_Place then
533 Ref_Node := New_Copy (Ref);
535 Ref_Node := New_Reference_To (Ref, Loc);
539 Make_Raise_Program_Error (Loc,
543 Build_Get_Access_Level (Loc,
544 Make_Attribute_Reference (Loc,
546 Attribute_Name => Name_Tag)),
548 Make_Integer_Literal (Loc,
549 Type_Access_Level (PtrT))),
550 Reason => PE_Accessibility_Check_Failed));
552 end Apply_Accessibility_Check;
556 Indic : constant Node_Id := Subtype_Mark (Expression (N));
557 T : constant Entity_Id := Entity (Indic);
562 TagT : Entity_Id := Empty;
563 -- Type used as source for tag assignment
565 TagR : Node_Id := Empty;
566 -- Target reference for tag assignment
568 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
570 Tag_Assign : Node_Id;
573 -- Start of processing for Expand_Allocator_Expression
576 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
578 if Is_CPP_Constructor_Call (Exp) then
581 -- Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn
583 -- Allocate the object with no expression
585 Node := Relocate_Node (N);
586 Set_Expression (Node, New_Reference_To (Etype (Exp), Loc));
588 -- Avoid its expansion to avoid generating a call to the default
593 Temp := Make_Temporary (Loc, 'P', N);
596 Make_Object_Declaration (Loc,
597 Defining_Identifier => Temp,
598 Constant_Present => True,
599 Object_Definition => New_Reference_To (PtrT, Loc),
600 Expression => Node));
602 Apply_Accessibility_Check (Temp);
604 -- Locate the enclosing list and insert the C++ constructor call
611 while not Is_List_Member (P) loop
615 Insert_List_After_And_Analyze (P,
616 Build_Initialization_Call (Loc,
618 Make_Explicit_Dereference (Loc,
619 Prefix => New_Reference_To (Temp, Loc)),
621 Constructor_Ref => Exp));
624 Rewrite (N, New_Reference_To (Temp, Loc));
625 Analyze_And_Resolve (N, PtrT);
629 -- Ada 2005 (AI-318-02): If the initialization expression is a call
630 -- to a build-in-place function, then access to the allocated object
631 -- must be passed to the function. Currently we limit such functions
632 -- to those with constrained limited result subtypes, but eventually
633 -- we plan to expand the allowed forms of functions that are treated
634 -- as build-in-place.
636 if Ada_Version >= Ada_05
637 and then Is_Build_In_Place_Function_Call (Exp)
639 Make_Build_In_Place_Call_In_Allocator (N, Exp);
640 Apply_Accessibility_Check (N, Built_In_Place => True);
644 -- Actions inserted before:
645 -- Temp : constant ptr_T := new T'(Expression);
646 -- <no CW> Temp._tag := T'tag;
647 -- <CTRL> Adjust (Finalizable (Temp.all));
648 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
650 -- We analyze by hand the new internal allocator to avoid
651 -- any recursion and inappropriate call to Initialize
653 -- We don't want to remove side effects when the expression must be
654 -- built in place. In the case of a build-in-place function call,
655 -- that could lead to a duplication of the call, which was already
656 -- substituted for the allocator.
658 if not Aggr_In_Place then
659 Remove_Side_Effects (Exp);
662 Temp := Make_Temporary (Loc, 'P', N);
664 -- For a class wide allocation generate the following code:
666 -- type Equiv_Record is record ... end record;
667 -- implicit subtype CW is <Class_Wide_Subytpe>;
668 -- temp : PtrT := new CW'(CW!(expr));
670 if Is_Class_Wide_Type (T) then
671 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
673 -- Ada 2005 (AI-251): If the expression is a class-wide interface
674 -- object we generate code to move up "this" to reference the
675 -- base of the object before allocating the new object.
677 -- Note that Exp'Address is recursively expanded into a call
678 -- to Base_Address (Exp.Tag)
680 if Is_Class_Wide_Type (Etype (Exp))
681 and then Is_Interface (Etype (Exp))
682 and then Tagged_Type_Expansion
686 Unchecked_Convert_To (Entity (Indic),
687 Make_Explicit_Dereference (Loc,
688 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
689 Make_Attribute_Reference (Loc,
691 Attribute_Name => Name_Address)))));
696 Unchecked_Convert_To (Entity (Indic), Exp));
699 Analyze_And_Resolve (Expression (N), Entity (Indic));
702 -- Keep separate the management of allocators returning interfaces
704 if not Is_Interface (Directly_Designated_Type (PtrT)) then
705 if Aggr_In_Place then
707 Make_Object_Declaration (Loc,
708 Defining_Identifier => Temp,
709 Object_Definition => New_Reference_To (PtrT, Loc),
712 New_Reference_To (Etype (Exp), Loc)));
714 -- Copy the Comes_From_Source flag for the allocator we just
715 -- built, since logically this allocator is a replacement of
716 -- the original allocator node. This is for proper handling of
717 -- restriction No_Implicit_Heap_Allocations.
719 Set_Comes_From_Source
720 (Expression (Tmp_Node), Comes_From_Source (N));
722 Set_No_Initialization (Expression (Tmp_Node));
723 Insert_Action (N, Tmp_Node);
725 if Needs_Finalization (T)
726 and then Ekind (PtrT) = E_Anonymous_Access_Type
728 -- Create local finalization list for access parameter
730 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
733 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
736 Node := Relocate_Node (N);
739 Make_Object_Declaration (Loc,
740 Defining_Identifier => Temp,
741 Constant_Present => True,
742 Object_Definition => New_Reference_To (PtrT, Loc),
743 Expression => Node));
746 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
747 -- interface type. In this case we use the type of the qualified
748 -- expression to allocate the object.
752 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
757 Make_Full_Type_Declaration (Loc,
758 Defining_Identifier => Def_Id,
760 Make_Access_To_Object_Definition (Loc,
762 Null_Exclusion_Present => False,
763 Constant_Present => False,
764 Subtype_Indication =>
765 New_Reference_To (Etype (Exp), Loc)));
767 Insert_Action (N, New_Decl);
769 -- Inherit the final chain to ensure that the expansion of the
770 -- aggregate is correct in case of controlled types
772 if Needs_Finalization (Directly_Designated_Type (PtrT)) then
773 Set_Associated_Final_Chain (Def_Id,
774 Associated_Final_Chain (PtrT));
777 -- Declare the object using the previous type declaration
779 if Aggr_In_Place then
781 Make_Object_Declaration (Loc,
782 Defining_Identifier => Temp,
783 Object_Definition => New_Reference_To (Def_Id, Loc),
786 New_Reference_To (Etype (Exp), Loc)));
788 -- Copy the Comes_From_Source flag for the allocator we just
789 -- built, since logically this allocator is a replacement of
790 -- the original allocator node. This is for proper handling
791 -- of restriction No_Implicit_Heap_Allocations.
793 Set_Comes_From_Source
794 (Expression (Tmp_Node), Comes_From_Source (N));
796 Set_No_Initialization (Expression (Tmp_Node));
797 Insert_Action (N, Tmp_Node);
799 if Needs_Finalization (T)
800 and then Ekind (PtrT) = E_Anonymous_Access_Type
802 -- Create local finalization list for access parameter
805 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
808 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
810 Node := Relocate_Node (N);
813 Make_Object_Declaration (Loc,
814 Defining_Identifier => Temp,
815 Constant_Present => True,
816 Object_Definition => New_Reference_To (Def_Id, Loc),
817 Expression => Node));
820 -- Generate an additional object containing the address of the
821 -- returned object. The type of this second object declaration
822 -- is the correct type required for the common processing that
823 -- is still performed by this subprogram. The displacement of
824 -- this pointer to reference the component associated with the
825 -- interface type will be done at the end of common processing.
828 Make_Object_Declaration (Loc,
829 Defining_Identifier => Make_Temporary (Loc, 'P'),
830 Object_Definition => New_Reference_To (PtrT, Loc),
831 Expression => Unchecked_Convert_To (PtrT,
832 New_Reference_To (Temp, Loc)));
834 Insert_Action (N, New_Decl);
836 Tmp_Node := New_Decl;
837 Temp := Defining_Identifier (New_Decl);
841 Apply_Accessibility_Check (Temp);
843 -- Generate the tag assignment
845 -- Suppress the tag assignment when VM_Target because VM tags are
846 -- represented implicitly in objects.
848 if not Tagged_Type_Expansion then
851 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
852 -- interface objects because in this case the tag does not change.
854 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
855 pragma Assert (Is_Class_Wide_Type
856 (Directly_Designated_Type (Etype (N))));
859 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
861 TagR := New_Reference_To (Temp, Loc);
863 elsif Is_Private_Type (T)
864 and then Is_Tagged_Type (Underlying_Type (T))
866 TagT := Underlying_Type (T);
868 Unchecked_Convert_To (Underlying_Type (T),
869 Make_Explicit_Dereference (Loc,
870 Prefix => New_Reference_To (Temp, Loc)));
873 if Present (TagT) then
875 Make_Assignment_Statement (Loc,
877 Make_Selected_Component (Loc,
880 New_Reference_To (First_Tag_Component (TagT), Loc)),
883 Unchecked_Convert_To (RTE (RE_Tag),
885 (Elists.Node (First_Elmt (Access_Disp_Table (TagT))),
888 -- The previous assignment has to be done in any case
890 Set_Assignment_OK (Name (Tag_Assign));
891 Insert_Action (N, Tag_Assign);
894 if Needs_Finalization (DesigT)
895 and then Needs_Finalization (T)
899 Apool : constant Entity_Id :=
900 Associated_Storage_Pool (PtrT);
903 -- If it is an allocation on the secondary stack (i.e. a value
904 -- returned from a function), the object is attached on the
905 -- caller side as soon as the call is completed (see
906 -- Expand_Ctrl_Function_Call)
908 if Is_RTE (Apool, RE_SS_Pool) then
910 F : constant Entity_Id := Make_Temporary (Loc, 'F');
913 Make_Object_Declaration (Loc,
914 Defining_Identifier => F,
916 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc)));
917 Flist := New_Reference_To (F, Loc);
918 Attach := Make_Integer_Literal (Loc, 1);
921 -- Normal case, not a secondary stack allocation
924 if Needs_Finalization (T)
925 and then Ekind (PtrT) = E_Anonymous_Access_Type
927 -- Create local finalization list for access parameter
930 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
932 Flist := Find_Final_List (PtrT);
935 Attach := Make_Integer_Literal (Loc, 2);
938 -- Generate an Adjust call if the object will be moved. In Ada
939 -- 2005, the object may be inherently limited, in which case
940 -- there is no Adjust procedure, and the object is built in
941 -- place. In Ada 95, the object can be limited but not
942 -- inherently limited if this allocator came from a return
943 -- statement (we're allocating the result on the secondary
944 -- stack). In that case, the object will be moved, so we _do_
948 and then not Is_Inherently_Limited_Type (T)
954 -- An unchecked conversion is needed in the classwide
955 -- case because the designated type can be an ancestor of
956 -- the subtype mark of the allocator.
958 Unchecked_Convert_To (T,
959 Make_Explicit_Dereference (Loc,
960 Prefix => New_Reference_To (Temp, Loc))),
964 With_Attach => Attach,
970 Rewrite (N, New_Reference_To (Temp, Loc));
971 Analyze_And_Resolve (N, PtrT);
973 -- Ada 2005 (AI-251): Displace the pointer to reference the record
974 -- component containing the secondary dispatch table of the interface
977 if Is_Interface (Directly_Designated_Type (PtrT)) then
978 Displace_Allocator_Pointer (N);
981 elsif Aggr_In_Place then
982 Temp := Make_Temporary (Loc, 'P', N);
984 Make_Object_Declaration (Loc,
985 Defining_Identifier => Temp,
986 Object_Definition => New_Reference_To (PtrT, Loc),
987 Expression => Make_Allocator (Loc,
988 New_Reference_To (Etype (Exp), Loc)));
990 -- Copy the Comes_From_Source flag for the allocator we just built,
991 -- since logically this allocator is a replacement of the original
992 -- allocator node. This is for proper handling of restriction
993 -- No_Implicit_Heap_Allocations.
995 Set_Comes_From_Source
996 (Expression (Tmp_Node), Comes_From_Source (N));
998 Set_No_Initialization (Expression (Tmp_Node));
999 Insert_Action (N, Tmp_Node);
1000 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
1001 Rewrite (N, New_Reference_To (Temp, Loc));
1002 Analyze_And_Resolve (N, PtrT);
1004 elsif Is_Access_Type (T)
1005 and then Can_Never_Be_Null (T)
1007 Install_Null_Excluding_Check (Exp);
1009 elsif Is_Access_Type (DesigT)
1010 and then Nkind (Exp) = N_Allocator
1011 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1013 -- Apply constraint to designated subtype indication
1015 Apply_Constraint_Check (Expression (Exp),
1016 Designated_Type (DesigT),
1017 No_Sliding => True);
1019 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1021 -- Propagate constraint_error to enclosing allocator
1023 Rewrite (Exp, New_Copy (Expression (Exp)));
1027 -- type A is access T1;
1028 -- X : A := new T2'(...);
1029 -- T1 and T2 can be different subtypes, and we might need to check
1030 -- both constraints. First check against the type of the qualified
1033 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1035 if Do_Range_Check (Exp) then
1036 Set_Do_Range_Check (Exp, False);
1037 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1040 -- A check is also needed in cases where the designated subtype is
1041 -- constrained and differs from the subtype given in the qualified
1042 -- expression. Note that the check on the qualified expression does
1043 -- not allow sliding, but this check does (a relaxation from Ada 83).
1045 if Is_Constrained (DesigT)
1046 and then not Subtypes_Statically_Match (T, DesigT)
1048 Apply_Constraint_Check
1049 (Exp, DesigT, No_Sliding => False);
1051 if Do_Range_Check (Exp) then
1052 Set_Do_Range_Check (Exp, False);
1053 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1057 -- For an access to unconstrained packed array, GIGI needs to see an
1058 -- expression with a constrained subtype in order to compute the
1059 -- proper size for the allocator.
1061 if Is_Array_Type (T)
1062 and then not Is_Constrained (T)
1063 and then Is_Packed (T)
1066 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1067 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1070 Make_Subtype_Declaration (Loc,
1071 Defining_Identifier => ConstrT,
1072 Subtype_Indication =>
1073 Make_Subtype_From_Expr (Exp, T)));
1074 Freeze_Itype (ConstrT, Exp);
1075 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1079 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1080 -- to a build-in-place function, then access to the allocated object
1081 -- must be passed to the function. Currently we limit such functions
1082 -- to those with constrained limited result subtypes, but eventually
1083 -- we plan to expand the allowed forms of functions that are treated
1084 -- as build-in-place.
1086 if Ada_Version >= Ada_05
1087 and then Is_Build_In_Place_Function_Call (Exp)
1089 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1094 when RE_Not_Available =>
1096 end Expand_Allocator_Expression;
1098 -----------------------------
1099 -- Expand_Array_Comparison --
1100 -----------------------------
1102 -- Expansion is only required in the case of array types. For the unpacked
1103 -- case, an appropriate runtime routine is called. For packed cases, and
1104 -- also in some other cases where a runtime routine cannot be called, the
1105 -- form of the expansion is:
1107 -- [body for greater_nn; boolean_expression]
1109 -- The body is built by Make_Array_Comparison_Op, and the form of the
1110 -- Boolean expression depends on the operator involved.
1112 procedure Expand_Array_Comparison (N : Node_Id) is
1113 Loc : constant Source_Ptr := Sloc (N);
1114 Op1 : Node_Id := Left_Opnd (N);
1115 Op2 : Node_Id := Right_Opnd (N);
1116 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1117 Ctyp : constant Entity_Id := Component_Type (Typ1);
1120 Func_Body : Node_Id;
1121 Func_Name : Entity_Id;
1125 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1126 -- True for byte addressable target
1128 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1129 -- Returns True if the length of the given operand is known to be less
1130 -- than 4. Returns False if this length is known to be four or greater
1131 -- or is not known at compile time.
1133 ------------------------
1134 -- Length_Less_Than_4 --
1135 ------------------------
1137 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1138 Otyp : constant Entity_Id := Etype (Opnd);
1141 if Ekind (Otyp) = E_String_Literal_Subtype then
1142 return String_Literal_Length (Otyp) < 4;
1146 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1147 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1148 Hi : constant Node_Id := Type_High_Bound (Ityp);
1153 if Compile_Time_Known_Value (Lo) then
1154 Lov := Expr_Value (Lo);
1159 if Compile_Time_Known_Value (Hi) then
1160 Hiv := Expr_Value (Hi);
1165 return Hiv < Lov + 3;
1168 end Length_Less_Than_4;
1170 -- Start of processing for Expand_Array_Comparison
1173 -- Deal first with unpacked case, where we can call a runtime routine
1174 -- except that we avoid this for targets for which are not addressable
1175 -- by bytes, and for the JVM/CIL, since they do not support direct
1176 -- addressing of array components.
1178 if not Is_Bit_Packed_Array (Typ1)
1179 and then Byte_Addressable
1180 and then VM_Target = No_VM
1182 -- The call we generate is:
1184 -- Compare_Array_xn[_Unaligned]
1185 -- (left'address, right'address, left'length, right'length) <op> 0
1187 -- x = U for unsigned, S for signed
1188 -- n = 8,16,32,64 for component size
1189 -- Add _Unaligned if length < 4 and component size is 8.
1190 -- <op> is the standard comparison operator
1192 if Component_Size (Typ1) = 8 then
1193 if Length_Less_Than_4 (Op1)
1195 Length_Less_Than_4 (Op2)
1197 if Is_Unsigned_Type (Ctyp) then
1198 Comp := RE_Compare_Array_U8_Unaligned;
1200 Comp := RE_Compare_Array_S8_Unaligned;
1204 if Is_Unsigned_Type (Ctyp) then
1205 Comp := RE_Compare_Array_U8;
1207 Comp := RE_Compare_Array_S8;
1211 elsif Component_Size (Typ1) = 16 then
1212 if Is_Unsigned_Type (Ctyp) then
1213 Comp := RE_Compare_Array_U16;
1215 Comp := RE_Compare_Array_S16;
1218 elsif Component_Size (Typ1) = 32 then
1219 if Is_Unsigned_Type (Ctyp) then
1220 Comp := RE_Compare_Array_U32;
1222 Comp := RE_Compare_Array_S32;
1225 else pragma Assert (Component_Size (Typ1) = 64);
1226 if Is_Unsigned_Type (Ctyp) then
1227 Comp := RE_Compare_Array_U64;
1229 Comp := RE_Compare_Array_S64;
1233 Remove_Side_Effects (Op1, Name_Req => True);
1234 Remove_Side_Effects (Op2, Name_Req => True);
1237 Make_Function_Call (Sloc (Op1),
1238 Name => New_Occurrence_Of (RTE (Comp), Loc),
1240 Parameter_Associations => New_List (
1241 Make_Attribute_Reference (Loc,
1242 Prefix => Relocate_Node (Op1),
1243 Attribute_Name => Name_Address),
1245 Make_Attribute_Reference (Loc,
1246 Prefix => Relocate_Node (Op2),
1247 Attribute_Name => Name_Address),
1249 Make_Attribute_Reference (Loc,
1250 Prefix => Relocate_Node (Op1),
1251 Attribute_Name => Name_Length),
1253 Make_Attribute_Reference (Loc,
1254 Prefix => Relocate_Node (Op2),
1255 Attribute_Name => Name_Length))));
1258 Make_Integer_Literal (Sloc (Op2),
1261 Analyze_And_Resolve (Op1, Standard_Integer);
1262 Analyze_And_Resolve (Op2, Standard_Integer);
1266 -- Cases where we cannot make runtime call
1268 -- For (a <= b) we convert to not (a > b)
1270 if Chars (N) = Name_Op_Le then
1276 Right_Opnd => Op2)));
1277 Analyze_And_Resolve (N, Standard_Boolean);
1280 -- For < the Boolean expression is
1281 -- greater__nn (op2, op1)
1283 elsif Chars (N) = Name_Op_Lt then
1284 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1288 Op1 := Right_Opnd (N);
1289 Op2 := Left_Opnd (N);
1291 -- For (a >= b) we convert to not (a < b)
1293 elsif Chars (N) = Name_Op_Ge then
1299 Right_Opnd => Op2)));
1300 Analyze_And_Resolve (N, Standard_Boolean);
1303 -- For > the Boolean expression is
1304 -- greater__nn (op1, op2)
1307 pragma Assert (Chars (N) = Name_Op_Gt);
1308 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1311 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1313 Make_Function_Call (Loc,
1314 Name => New_Reference_To (Func_Name, Loc),
1315 Parameter_Associations => New_List (Op1, Op2));
1317 Insert_Action (N, Func_Body);
1319 Analyze_And_Resolve (N, Standard_Boolean);
1322 when RE_Not_Available =>
1324 end Expand_Array_Comparison;
1326 ---------------------------
1327 -- Expand_Array_Equality --
1328 ---------------------------
1330 -- Expand an equality function for multi-dimensional arrays. Here is an
1331 -- example of such a function for Nb_Dimension = 2
1333 -- function Enn (A : atyp; B : btyp) return boolean is
1335 -- if (A'length (1) = 0 or else A'length (2) = 0)
1337 -- (B'length (1) = 0 or else B'length (2) = 0)
1339 -- return True; -- RM 4.5.2(22)
1342 -- if A'length (1) /= B'length (1)
1344 -- A'length (2) /= B'length (2)
1346 -- return False; -- RM 4.5.2(23)
1350 -- A1 : Index_T1 := A'first (1);
1351 -- B1 : Index_T1 := B'first (1);
1355 -- A2 : Index_T2 := A'first (2);
1356 -- B2 : Index_T2 := B'first (2);
1359 -- if A (A1, A2) /= B (B1, B2) then
1363 -- exit when A2 = A'last (2);
1364 -- A2 := Index_T2'succ (A2);
1365 -- B2 := Index_T2'succ (B2);
1369 -- exit when A1 = A'last (1);
1370 -- A1 := Index_T1'succ (A1);
1371 -- B1 := Index_T1'succ (B1);
1378 -- Note on the formal types used (atyp and btyp). If either of the arrays
1379 -- is of a private type, we use the underlying type, and do an unchecked
1380 -- conversion of the actual. If either of the arrays has a bound depending
1381 -- on a discriminant, then we use the base type since otherwise we have an
1382 -- escaped discriminant in the function.
1384 -- If both arrays are constrained and have the same bounds, we can generate
1385 -- a loop with an explicit iteration scheme using a 'Range attribute over
1388 function Expand_Array_Equality
1393 Typ : Entity_Id) return Node_Id
1395 Loc : constant Source_Ptr := Sloc (Nod);
1396 Decls : constant List_Id := New_List;
1397 Index_List1 : constant List_Id := New_List;
1398 Index_List2 : constant List_Id := New_List;
1402 Func_Name : Entity_Id;
1403 Func_Body : Node_Id;
1405 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1406 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1410 -- The parameter types to be used for the formals
1415 Num : Int) return Node_Id;
1416 -- This builds the attribute reference Arr'Nam (Expr)
1418 function Component_Equality (Typ : Entity_Id) return Node_Id;
1419 -- Create one statement to compare corresponding components, designated
1420 -- by a full set of indices.
1422 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1423 -- Given one of the arguments, computes the appropriate type to be used
1424 -- for that argument in the corresponding function formal
1426 function Handle_One_Dimension
1428 Index : Node_Id) return Node_Id;
1429 -- This procedure returns the following code
1432 -- Bn : Index_T := B'First (N);
1436 -- exit when An = A'Last (N);
1437 -- An := Index_T'Succ (An)
1438 -- Bn := Index_T'Succ (Bn)
1442 -- If both indices are constrained and identical, the procedure
1443 -- returns a simpler loop:
1445 -- for An in A'Range (N) loop
1449 -- N is the dimension for which we are generating a loop. Index is the
1450 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1451 -- xxx statement is either the loop or declare for the next dimension
1452 -- or if this is the last dimension the comparison of corresponding
1453 -- components of the arrays.
1455 -- The actual way the code works is to return the comparison of
1456 -- corresponding components for the N+1 call. That's neater!
1458 function Test_Empty_Arrays return Node_Id;
1459 -- This function constructs the test for both arrays being empty
1460 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1462 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1464 function Test_Lengths_Correspond return Node_Id;
1465 -- This function constructs the test for arrays having different lengths
1466 -- in at least one index position, in which case the resulting code is:
1468 -- A'length (1) /= B'length (1)
1470 -- A'length (2) /= B'length (2)
1481 Num : Int) return Node_Id
1485 Make_Attribute_Reference (Loc,
1486 Attribute_Name => Nam,
1487 Prefix => New_Reference_To (Arr, Loc),
1488 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1491 ------------------------
1492 -- Component_Equality --
1493 ------------------------
1495 function Component_Equality (Typ : Entity_Id) return Node_Id is
1500 -- if a(i1...) /= b(j1...) then return false; end if;
1503 Make_Indexed_Component (Loc,
1504 Prefix => Make_Identifier (Loc, Chars (A)),
1505 Expressions => Index_List1);
1508 Make_Indexed_Component (Loc,
1509 Prefix => Make_Identifier (Loc, Chars (B)),
1510 Expressions => Index_List2);
1512 Test := Expand_Composite_Equality
1513 (Nod, Component_Type (Typ), L, R, Decls);
1515 -- If some (sub)component is an unchecked_union, the whole operation
1516 -- will raise program error.
1518 if Nkind (Test) = N_Raise_Program_Error then
1520 -- This node is going to be inserted at a location where a
1521 -- statement is expected: clear its Etype so analysis will set
1522 -- it to the expected Standard_Void_Type.
1524 Set_Etype (Test, Empty);
1529 Make_Implicit_If_Statement (Nod,
1530 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1531 Then_Statements => New_List (
1532 Make_Simple_Return_Statement (Loc,
1533 Expression => New_Occurrence_Of (Standard_False, Loc))));
1535 end Component_Equality;
1541 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1552 T := Underlying_Type (T);
1554 X := First_Index (T);
1555 while Present (X) loop
1556 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1558 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1571 --------------------------
1572 -- Handle_One_Dimension --
1573 ---------------------------
1575 function Handle_One_Dimension
1577 Index : Node_Id) return Node_Id
1579 Need_Separate_Indexes : constant Boolean :=
1581 or else not Is_Constrained (Ltyp);
1582 -- If the index types are identical, and we are working with
1583 -- constrained types, then we can use the same index for both
1586 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1589 Index_T : Entity_Id;
1594 if N > Number_Dimensions (Ltyp) then
1595 return Component_Equality (Ltyp);
1598 -- Case where we generate a loop
1600 Index_T := Base_Type (Etype (Index));
1602 if Need_Separate_Indexes then
1603 Bn := Make_Temporary (Loc, 'B');
1608 Append (New_Reference_To (An, Loc), Index_List1);
1609 Append (New_Reference_To (Bn, Loc), Index_List2);
1611 Stm_List := New_List (
1612 Handle_One_Dimension (N + 1, Next_Index (Index)));
1614 if Need_Separate_Indexes then
1616 -- Generate guard for loop, followed by increments of indices
1618 Append_To (Stm_List,
1619 Make_Exit_Statement (Loc,
1622 Left_Opnd => New_Reference_To (An, Loc),
1623 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1625 Append_To (Stm_List,
1626 Make_Assignment_Statement (Loc,
1627 Name => New_Reference_To (An, Loc),
1629 Make_Attribute_Reference (Loc,
1630 Prefix => New_Reference_To (Index_T, Loc),
1631 Attribute_Name => Name_Succ,
1632 Expressions => New_List (New_Reference_To (An, Loc)))));
1634 Append_To (Stm_List,
1635 Make_Assignment_Statement (Loc,
1636 Name => New_Reference_To (Bn, Loc),
1638 Make_Attribute_Reference (Loc,
1639 Prefix => New_Reference_To (Index_T, Loc),
1640 Attribute_Name => Name_Succ,
1641 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1644 -- If separate indexes, we need a declare block for An and Bn, and a
1645 -- loop without an iteration scheme.
1647 if Need_Separate_Indexes then
1649 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1652 Make_Block_Statement (Loc,
1653 Declarations => New_List (
1654 Make_Object_Declaration (Loc,
1655 Defining_Identifier => An,
1656 Object_Definition => New_Reference_To (Index_T, Loc),
1657 Expression => Arr_Attr (A, Name_First, N)),
1659 Make_Object_Declaration (Loc,
1660 Defining_Identifier => Bn,
1661 Object_Definition => New_Reference_To (Index_T, Loc),
1662 Expression => Arr_Attr (B, Name_First, N))),
1664 Handled_Statement_Sequence =>
1665 Make_Handled_Sequence_Of_Statements (Loc,
1666 Statements => New_List (Loop_Stm)));
1668 -- If no separate indexes, return loop statement with explicit
1669 -- iteration scheme on its own
1673 Make_Implicit_Loop_Statement (Nod,
1674 Statements => Stm_List,
1676 Make_Iteration_Scheme (Loc,
1677 Loop_Parameter_Specification =>
1678 Make_Loop_Parameter_Specification (Loc,
1679 Defining_Identifier => An,
1680 Discrete_Subtype_Definition =>
1681 Arr_Attr (A, Name_Range, N))));
1684 end Handle_One_Dimension;
1686 -----------------------
1687 -- Test_Empty_Arrays --
1688 -----------------------
1690 function Test_Empty_Arrays return Node_Id is
1700 for J in 1 .. Number_Dimensions (Ltyp) loop
1703 Left_Opnd => Arr_Attr (A, Name_Length, J),
1704 Right_Opnd => Make_Integer_Literal (Loc, 0));
1708 Left_Opnd => Arr_Attr (B, Name_Length, J),
1709 Right_Opnd => Make_Integer_Literal (Loc, 0));
1718 Left_Opnd => Relocate_Node (Alist),
1719 Right_Opnd => Atest);
1723 Left_Opnd => Relocate_Node (Blist),
1724 Right_Opnd => Btest);
1731 Right_Opnd => Blist);
1732 end Test_Empty_Arrays;
1734 -----------------------------
1735 -- Test_Lengths_Correspond --
1736 -----------------------------
1738 function Test_Lengths_Correspond return Node_Id is
1744 for J in 1 .. Number_Dimensions (Ltyp) loop
1747 Left_Opnd => Arr_Attr (A, Name_Length, J),
1748 Right_Opnd => Arr_Attr (B, Name_Length, J));
1755 Left_Opnd => Relocate_Node (Result),
1756 Right_Opnd => Rtest);
1761 end Test_Lengths_Correspond;
1763 -- Start of processing for Expand_Array_Equality
1766 Ltyp := Get_Arg_Type (Lhs);
1767 Rtyp := Get_Arg_Type (Rhs);
1769 -- For now, if the argument types are not the same, go to the base type,
1770 -- since the code assumes that the formals have the same type. This is
1771 -- fixable in future ???
1773 if Ltyp /= Rtyp then
1774 Ltyp := Base_Type (Ltyp);
1775 Rtyp := Base_Type (Rtyp);
1776 pragma Assert (Ltyp = Rtyp);
1779 -- Build list of formals for function
1781 Formals := New_List (
1782 Make_Parameter_Specification (Loc,
1783 Defining_Identifier => A,
1784 Parameter_Type => New_Reference_To (Ltyp, Loc)),
1786 Make_Parameter_Specification (Loc,
1787 Defining_Identifier => B,
1788 Parameter_Type => New_Reference_To (Rtyp, Loc)));
1790 Func_Name := Make_Temporary (Loc, 'E');
1792 -- Build statement sequence for function
1795 Make_Subprogram_Body (Loc,
1797 Make_Function_Specification (Loc,
1798 Defining_Unit_Name => Func_Name,
1799 Parameter_Specifications => Formals,
1800 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
1802 Declarations => Decls,
1804 Handled_Statement_Sequence =>
1805 Make_Handled_Sequence_Of_Statements (Loc,
1806 Statements => New_List (
1808 Make_Implicit_If_Statement (Nod,
1809 Condition => Test_Empty_Arrays,
1810 Then_Statements => New_List (
1811 Make_Simple_Return_Statement (Loc,
1813 New_Occurrence_Of (Standard_True, Loc)))),
1815 Make_Implicit_If_Statement (Nod,
1816 Condition => Test_Lengths_Correspond,
1817 Then_Statements => New_List (
1818 Make_Simple_Return_Statement (Loc,
1820 New_Occurrence_Of (Standard_False, Loc)))),
1822 Handle_One_Dimension (1, First_Index (Ltyp)),
1824 Make_Simple_Return_Statement (Loc,
1825 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1827 Set_Has_Completion (Func_Name, True);
1828 Set_Is_Inlined (Func_Name);
1830 -- If the array type is distinct from the type of the arguments, it
1831 -- is the full view of a private type. Apply an unchecked conversion
1832 -- to insure that analysis of the call succeeds.
1842 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1844 L := OK_Convert_To (Ltyp, Lhs);
1848 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1850 R := OK_Convert_To (Rtyp, Rhs);
1853 Actuals := New_List (L, R);
1856 Append_To (Bodies, Func_Body);
1859 Make_Function_Call (Loc,
1860 Name => New_Reference_To (Func_Name, Loc),
1861 Parameter_Associations => Actuals);
1862 end Expand_Array_Equality;
1864 -----------------------------
1865 -- Expand_Boolean_Operator --
1866 -----------------------------
1868 -- Note that we first get the actual subtypes of the operands, since we
1869 -- always want to deal with types that have bounds.
1871 procedure Expand_Boolean_Operator (N : Node_Id) is
1872 Typ : constant Entity_Id := Etype (N);
1875 -- Special case of bit packed array where both operands are known to be
1876 -- properly aligned. In this case we use an efficient run time routine
1877 -- to carry out the operation (see System.Bit_Ops).
1879 if Is_Bit_Packed_Array (Typ)
1880 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
1881 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
1883 Expand_Packed_Boolean_Operator (N);
1887 -- For the normal non-packed case, the general expansion is to build
1888 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
1889 -- and then inserting it into the tree. The original operator node is
1890 -- then rewritten as a call to this function. We also use this in the
1891 -- packed case if either operand is a possibly unaligned object.
1894 Loc : constant Source_Ptr := Sloc (N);
1895 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1896 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1897 Func_Body : Node_Id;
1898 Func_Name : Entity_Id;
1901 Convert_To_Actual_Subtype (L);
1902 Convert_To_Actual_Subtype (R);
1903 Ensure_Defined (Etype (L), N);
1904 Ensure_Defined (Etype (R), N);
1905 Apply_Length_Check (R, Etype (L));
1907 if Nkind (N) = N_Op_Xor then
1908 Silly_Boolean_Array_Xor_Test (N, Etype (L));
1911 if Nkind (Parent (N)) = N_Assignment_Statement
1912 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
1914 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
1916 elsif Nkind (Parent (N)) = N_Op_Not
1917 and then Nkind (N) = N_Op_And
1919 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
1924 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
1925 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1926 Insert_Action (N, Func_Body);
1928 -- Now rewrite the expression with a call
1931 Make_Function_Call (Loc,
1932 Name => New_Reference_To (Func_Name, Loc),
1933 Parameter_Associations =>
1936 Make_Type_Conversion
1937 (Loc, New_Reference_To (Etype (L), Loc), R))));
1939 Analyze_And_Resolve (N, Typ);
1942 end Expand_Boolean_Operator;
1944 -------------------------------
1945 -- Expand_Composite_Equality --
1946 -------------------------------
1948 -- This function is only called for comparing internal fields of composite
1949 -- types when these fields are themselves composites. This is a special
1950 -- case because it is not possible to respect normal Ada visibility rules.
1952 function Expand_Composite_Equality
1957 Bodies : List_Id) return Node_Id
1959 Loc : constant Source_Ptr := Sloc (Nod);
1960 Full_Type : Entity_Id;
1965 if Is_Private_Type (Typ) then
1966 Full_Type := Underlying_Type (Typ);
1971 -- Defense against malformed private types with no completion the error
1972 -- will be diagnosed later by check_completion
1974 if No (Full_Type) then
1975 return New_Reference_To (Standard_False, Loc);
1978 Full_Type := Base_Type (Full_Type);
1980 if Is_Array_Type (Full_Type) then
1982 -- If the operand is an elementary type other than a floating-point
1983 -- type, then we can simply use the built-in block bitwise equality,
1984 -- since the predefined equality operators always apply and bitwise
1985 -- equality is fine for all these cases.
1987 if Is_Elementary_Type (Component_Type (Full_Type))
1988 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
1990 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
1992 -- For composite component types, and floating-point types, use the
1993 -- expansion. This deals with tagged component types (where we use
1994 -- the applicable equality routine) and floating-point, (where we
1995 -- need to worry about negative zeroes), and also the case of any
1996 -- composite type recursively containing such fields.
1999 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2002 elsif Is_Tagged_Type (Full_Type) then
2004 -- Call the primitive operation "=" of this type
2006 if Is_Class_Wide_Type (Full_Type) then
2007 Full_Type := Root_Type (Full_Type);
2010 -- If this is derived from an untagged private type completed with a
2011 -- tagged type, it does not have a full view, so we use the primitive
2012 -- operations of the private type. This check should no longer be
2013 -- necessary when these types receive their full views ???
2015 if Is_Private_Type (Typ)
2016 and then not Is_Tagged_Type (Typ)
2017 and then not Is_Controlled (Typ)
2018 and then Is_Derived_Type (Typ)
2019 and then No (Full_View (Typ))
2021 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2023 Prim := First_Elmt (Primitive_Operations (Full_Type));
2027 Eq_Op := Node (Prim);
2028 exit when Chars (Eq_Op) = Name_Op_Eq
2029 and then Etype (First_Formal (Eq_Op)) =
2030 Etype (Next_Formal (First_Formal (Eq_Op)))
2031 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2033 pragma Assert (Present (Prim));
2036 Eq_Op := Node (Prim);
2039 Make_Function_Call (Loc,
2040 Name => New_Reference_To (Eq_Op, Loc),
2041 Parameter_Associations =>
2043 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2044 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2046 elsif Is_Record_Type (Full_Type) then
2047 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2049 if Present (Eq_Op) then
2050 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2052 -- Inherited equality from parent type. Convert the actuals to
2053 -- match signature of operation.
2056 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2060 Make_Function_Call (Loc,
2061 Name => New_Reference_To (Eq_Op, Loc),
2062 Parameter_Associations =>
2063 New_List (OK_Convert_To (T, Lhs),
2064 OK_Convert_To (T, Rhs)));
2068 -- Comparison between Unchecked_Union components
2070 if Is_Unchecked_Union (Full_Type) then
2072 Lhs_Type : Node_Id := Full_Type;
2073 Rhs_Type : Node_Id := Full_Type;
2074 Lhs_Discr_Val : Node_Id;
2075 Rhs_Discr_Val : Node_Id;
2080 if Nkind (Lhs) = N_Selected_Component then
2081 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2086 if Nkind (Rhs) = N_Selected_Component then
2087 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2090 -- Lhs of the composite equality
2092 if Is_Constrained (Lhs_Type) then
2094 -- Since the enclosing record type can never be an
2095 -- Unchecked_Union (this code is executed for records
2096 -- that do not have variants), we may reference its
2099 if Nkind (Lhs) = N_Selected_Component
2100 and then Has_Per_Object_Constraint (
2101 Entity (Selector_Name (Lhs)))
2104 Make_Selected_Component (Loc,
2105 Prefix => Prefix (Lhs),
2108 Get_Discriminant_Value (
2109 First_Discriminant (Lhs_Type),
2111 Stored_Constraint (Lhs_Type))));
2114 Lhs_Discr_Val := New_Copy (
2115 Get_Discriminant_Value (
2116 First_Discriminant (Lhs_Type),
2118 Stored_Constraint (Lhs_Type)));
2122 -- It is not possible to infer the discriminant since
2123 -- the subtype is not constrained.
2126 Make_Raise_Program_Error (Loc,
2127 Reason => PE_Unchecked_Union_Restriction);
2130 -- Rhs of the composite equality
2132 if Is_Constrained (Rhs_Type) then
2133 if Nkind (Rhs) = N_Selected_Component
2134 and then Has_Per_Object_Constraint (
2135 Entity (Selector_Name (Rhs)))
2138 Make_Selected_Component (Loc,
2139 Prefix => Prefix (Rhs),
2142 Get_Discriminant_Value (
2143 First_Discriminant (Rhs_Type),
2145 Stored_Constraint (Rhs_Type))));
2148 Rhs_Discr_Val := New_Copy (
2149 Get_Discriminant_Value (
2150 First_Discriminant (Rhs_Type),
2152 Stored_Constraint (Rhs_Type)));
2157 Make_Raise_Program_Error (Loc,
2158 Reason => PE_Unchecked_Union_Restriction);
2161 -- Call the TSS equality function with the inferred
2162 -- discriminant values.
2165 Make_Function_Call (Loc,
2166 Name => New_Reference_To (Eq_Op, Loc),
2167 Parameter_Associations => New_List (
2175 -- Shouldn't this be an else, we can't fall through the above
2179 Make_Function_Call (Loc,
2180 Name => New_Reference_To (Eq_Op, Loc),
2181 Parameter_Associations => New_List (Lhs, Rhs));
2185 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2189 -- It can be a simple record or the full view of a scalar private
2191 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2193 end Expand_Composite_Equality;
2195 ------------------------
2196 -- Expand_Concatenate --
2197 ------------------------
2199 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2200 Loc : constant Source_Ptr := Sloc (Cnode);
2202 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2203 -- Result type of concatenation
2205 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2206 -- Component type. Elements of this component type can appear as one
2207 -- of the operands of concatenation as well as arrays.
2209 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2212 Ityp : constant Entity_Id := Base_Type (Istyp);
2213 -- Index type. This is the base type of the index subtype, and is used
2214 -- for all computed bounds (which may be out of range of Istyp in the
2215 -- case of null ranges).
2218 -- This is the type we use to do arithmetic to compute the bounds and
2219 -- lengths of operands. The choice of this type is a little subtle and
2220 -- is discussed in a separate section at the start of the body code.
2222 Concatenation_Error : exception;
2223 -- Raised if concatenation is sure to raise a CE
2225 Result_May_Be_Null : Boolean := True;
2226 -- Reset to False if at least one operand is encountered which is known
2227 -- at compile time to be non-null. Used for handling the special case
2228 -- of setting the high bound to the last operand high bound for a null
2229 -- result, thus ensuring a proper high bound in the super-flat case.
2231 N : constant Nat := List_Length (Opnds);
2232 -- Number of concatenation operands including possibly null operands
2235 -- Number of operands excluding any known to be null, except that the
2236 -- last operand is always retained, in case it provides the bounds for
2240 -- Current operand being processed in the loop through operands. After
2241 -- this loop is complete, always contains the last operand (which is not
2242 -- the same as Operands (NN), since null operands are skipped).
2244 -- Arrays describing the operands, only the first NN entries of each
2245 -- array are set (NN < N when we exclude known null operands).
2247 Is_Fixed_Length : array (1 .. N) of Boolean;
2248 -- True if length of corresponding operand known at compile time
2250 Operands : array (1 .. N) of Node_Id;
2251 -- Set to the corresponding entry in the Opnds list (but note that null
2252 -- operands are excluded, so not all entries in the list are stored).
2254 Fixed_Length : array (1 .. N) of Uint;
2255 -- Set to length of operand. Entries in this array are set only if the
2256 -- corresponding entry in Is_Fixed_Length is True.
2258 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2259 -- Set to lower bound of operand. Either an integer literal in the case
2260 -- where the bound is known at compile time, else actual lower bound.
2261 -- The operand low bound is of type Ityp.
2263 Var_Length : array (1 .. N) of Entity_Id;
2264 -- Set to an entity of type Natural that contains the length of an
2265 -- operand whose length is not known at compile time. Entries in this
2266 -- array are set only if the corresponding entry in Is_Fixed_Length
2267 -- is False. The entity is of type Artyp.
2269 Aggr_Length : array (0 .. N) of Node_Id;
2270 -- The J'th entry in an expression node that represents the total length
2271 -- of operands 1 through J. It is either an integer literal node, or a
2272 -- reference to a constant entity with the right value, so it is fine
2273 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2274 -- entry always is set to zero. The length is of type Artyp.
2276 Low_Bound : Node_Id;
2277 -- A tree node representing the low bound of the result (of type Ityp).
2278 -- This is either an integer literal node, or an identifier reference to
2279 -- a constant entity initialized to the appropriate value.
2281 Last_Opnd_High_Bound : Node_Id;
2282 -- A tree node representing the high bound of the last operand. This
2283 -- need only be set if the result could be null. It is used for the
2284 -- special case of setting the right high bound for a null result.
2285 -- This is of type Ityp.
2287 High_Bound : Node_Id;
2288 -- A tree node representing the high bound of the result (of type Ityp)
2291 -- Result of the concatenation (of type Ityp)
2293 Actions : constant List_Id := New_List;
2294 -- Collect actions to be inserted if Save_Space is False
2296 Save_Space : Boolean;
2297 pragma Warnings (Off, Save_Space);
2298 -- Set to True if we are saving generated code space by calling routines
2299 -- in packages System.Concat_n.
2301 Known_Non_Null_Operand_Seen : Boolean;
2302 -- Set True during generation of the assignements of operands into
2303 -- result once an operand known to be non-null has been seen.
2305 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2306 -- This function makes an N_Integer_Literal node that is returned in
2307 -- analyzed form with the type set to Artyp. Importantly this literal
2308 -- is not flagged as static, so that if we do computations with it that
2309 -- result in statically detected out of range conditions, we will not
2310 -- generate error messages but instead warning messages.
2312 function To_Artyp (X : Node_Id) return Node_Id;
2313 -- Given a node of type Ityp, returns the corresponding value of type
2314 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2315 -- For enum types, the Pos of the value is returned.
2317 function To_Ityp (X : Node_Id) return Node_Id;
2318 -- The inverse function (uses Val in the case of enumeration types)
2320 ------------------------
2321 -- Make_Artyp_Literal --
2322 ------------------------
2324 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2325 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2327 Set_Etype (Result, Artyp);
2328 Set_Analyzed (Result, True);
2329 Set_Is_Static_Expression (Result, False);
2331 end Make_Artyp_Literal;
2337 function To_Artyp (X : Node_Id) return Node_Id is
2339 if Ityp = Base_Type (Artyp) then
2342 elsif Is_Enumeration_Type (Ityp) then
2344 Make_Attribute_Reference (Loc,
2345 Prefix => New_Occurrence_Of (Ityp, Loc),
2346 Attribute_Name => Name_Pos,
2347 Expressions => New_List (X));
2350 return Convert_To (Artyp, X);
2358 function To_Ityp (X : Node_Id) return Node_Id is
2360 if Is_Enumeration_Type (Ityp) then
2362 Make_Attribute_Reference (Loc,
2363 Prefix => New_Occurrence_Of (Ityp, Loc),
2364 Attribute_Name => Name_Val,
2365 Expressions => New_List (X));
2367 -- Case where we will do a type conversion
2370 if Ityp = Base_Type (Artyp) then
2373 return Convert_To (Ityp, X);
2378 -- Local Declarations
2380 Opnd_Typ : Entity_Id;
2388 -- Choose an appropriate computational type
2390 -- We will be doing calculations of lengths and bounds in this routine
2391 -- and computing one from the other in some cases, e.g. getting the high
2392 -- bound by adding the length-1 to the low bound.
2394 -- We can't just use the index type, or even its base type for this
2395 -- purpose for two reasons. First it might be an enumeration type which
2396 -- is not suitable fo computations of any kind, and second it may simply
2397 -- not have enough range. For example if the index type is -128..+127
2398 -- then lengths can be up to 256, which is out of range of the type.
2400 -- For enumeration types, we can simply use Standard_Integer, this is
2401 -- sufficient since the actual number of enumeration literals cannot
2402 -- possibly exceed the range of integer (remember we will be doing the
2403 -- arithmetic with POS values, not representation values).
2405 if Is_Enumeration_Type (Ityp) then
2406 Artyp := Standard_Integer;
2408 -- If index type is Positive, we use the standard unsigned type, to give
2409 -- more room on the top of the range, obviating the need for an overflow
2410 -- check when creating the upper bound. This is needed to avoid junk
2411 -- overflow checks in the common case of String types.
2413 -- ??? Disabled for now
2415 -- elsif Istyp = Standard_Positive then
2416 -- Artyp := Standard_Unsigned;
2418 -- For modular types, we use a 32-bit modular type for types whose size
2419 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2420 -- identity type, and for larger unsigned types we use 64-bits.
2422 elsif Is_Modular_Integer_Type (Ityp) then
2423 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
2424 Artyp := Standard_Unsigned;
2425 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
2428 Artyp := RTE (RE_Long_Long_Unsigned);
2431 -- Similar treatment for signed types
2434 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
2435 Artyp := Standard_Integer;
2436 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
2439 Artyp := Standard_Long_Long_Integer;
2443 -- Supply dummy entry at start of length array
2445 Aggr_Length (0) := Make_Artyp_Literal (0);
2447 -- Go through operands setting up the above arrays
2451 Opnd := Remove_Head (Opnds);
2452 Opnd_Typ := Etype (Opnd);
2454 -- The parent got messed up when we put the operands in a list,
2455 -- so now put back the proper parent for the saved operand.
2457 Set_Parent (Opnd, Parent (Cnode));
2459 -- Set will be True when we have setup one entry in the array
2463 -- Singleton element (or character literal) case
2465 if Base_Type (Opnd_Typ) = Ctyp then
2467 Operands (NN) := Opnd;
2468 Is_Fixed_Length (NN) := True;
2469 Fixed_Length (NN) := Uint_1;
2470 Result_May_Be_Null := False;
2472 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2473 -- since we know that the result cannot be null).
2475 Opnd_Low_Bound (NN) :=
2476 Make_Attribute_Reference (Loc,
2477 Prefix => New_Reference_To (Istyp, Loc),
2478 Attribute_Name => Name_First);
2482 -- String literal case (can only occur for strings of course)
2484 elsif Nkind (Opnd) = N_String_Literal then
2485 Len := String_Literal_Length (Opnd_Typ);
2488 Result_May_Be_Null := False;
2491 -- Capture last operand high bound if result could be null
2493 if J = N and then Result_May_Be_Null then
2494 Last_Opnd_High_Bound :=
2497 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
2498 Right_Opnd => Make_Integer_Literal (Loc, 1));
2501 -- Skip null string literal
2503 if J < N and then Len = 0 then
2508 Operands (NN) := Opnd;
2509 Is_Fixed_Length (NN) := True;
2511 -- Set length and bounds
2513 Fixed_Length (NN) := Len;
2515 Opnd_Low_Bound (NN) :=
2516 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
2523 -- Check constrained case with known bounds
2525 if Is_Constrained (Opnd_Typ) then
2527 Index : constant Node_Id := First_Index (Opnd_Typ);
2528 Indx_Typ : constant Entity_Id := Etype (Index);
2529 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
2530 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
2533 -- Fixed length constrained array type with known at compile
2534 -- time bounds is last case of fixed length operand.
2536 if Compile_Time_Known_Value (Lo)
2538 Compile_Time_Known_Value (Hi)
2541 Loval : constant Uint := Expr_Value (Lo);
2542 Hival : constant Uint := Expr_Value (Hi);
2543 Len : constant Uint :=
2544 UI_Max (Hival - Loval + 1, Uint_0);
2548 Result_May_Be_Null := False;
2551 -- Capture last operand bound if result could be null
2553 if J = N and then Result_May_Be_Null then
2554 Last_Opnd_High_Bound :=
2556 Make_Integer_Literal (Loc,
2557 Intval => Expr_Value (Hi)));
2560 -- Exclude null length case unless last operand
2562 if J < N and then Len = 0 then
2567 Operands (NN) := Opnd;
2568 Is_Fixed_Length (NN) := True;
2569 Fixed_Length (NN) := Len;
2571 Opnd_Low_Bound (NN) := To_Ityp (
2572 Make_Integer_Literal (Loc,
2573 Intval => Expr_Value (Lo)));
2581 -- All cases where the length is not known at compile time, or the
2582 -- special case of an operand which is known to be null but has a
2583 -- lower bound other than 1 or is other than a string type.
2588 -- Capture operand bounds
2590 Opnd_Low_Bound (NN) :=
2591 Make_Attribute_Reference (Loc,
2593 Duplicate_Subexpr (Opnd, Name_Req => True),
2594 Attribute_Name => Name_First);
2596 if J = N and Result_May_Be_Null then
2597 Last_Opnd_High_Bound :=
2599 Make_Attribute_Reference (Loc,
2601 Duplicate_Subexpr (Opnd, Name_Req => True),
2602 Attribute_Name => Name_Last));
2605 -- Capture length of operand in entity
2607 Operands (NN) := Opnd;
2608 Is_Fixed_Length (NN) := False;
2610 Var_Length (NN) := Make_Temporary (Loc, 'L');
2613 Make_Object_Declaration (Loc,
2614 Defining_Identifier => Var_Length (NN),
2615 Constant_Present => True,
2617 Object_Definition =>
2618 New_Occurrence_Of (Artyp, Loc),
2621 Make_Attribute_Reference (Loc,
2623 Duplicate_Subexpr (Opnd, Name_Req => True),
2624 Attribute_Name => Name_Length)));
2628 -- Set next entry in aggregate length array
2630 -- For first entry, make either integer literal for fixed length
2631 -- or a reference to the saved length for variable length.
2634 if Is_Fixed_Length (1) then
2636 Make_Integer_Literal (Loc,
2637 Intval => Fixed_Length (1));
2640 New_Reference_To (Var_Length (1), Loc);
2643 -- If entry is fixed length and only fixed lengths so far, make
2644 -- appropriate new integer literal adding new length.
2646 elsif Is_Fixed_Length (NN)
2647 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
2650 Make_Integer_Literal (Loc,
2651 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
2653 -- All other cases, construct an addition node for the length and
2654 -- create an entity initialized to this length.
2657 Ent := Make_Temporary (Loc, 'L');
2659 if Is_Fixed_Length (NN) then
2660 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
2662 Clen := New_Reference_To (Var_Length (NN), Loc);
2666 Make_Object_Declaration (Loc,
2667 Defining_Identifier => Ent,
2668 Constant_Present => True,
2670 Object_Definition =>
2671 New_Occurrence_Of (Artyp, Loc),
2675 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
2676 Right_Opnd => Clen)));
2678 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
2685 -- If we have only skipped null operands, return the last operand
2692 -- If we have only one non-null operand, return it and we are done.
2693 -- There is one case in which this cannot be done, and that is when
2694 -- the sole operand is of the element type, in which case it must be
2695 -- converted to an array, and the easiest way of doing that is to go
2696 -- through the normal general circuit.
2699 and then Base_Type (Etype (Operands (1))) /= Ctyp
2701 Result := Operands (1);
2705 -- Cases where we have a real concatenation
2707 -- Next step is to find the low bound for the result array that we
2708 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
2710 -- If the ultimate ancestor of the index subtype is a constrained array
2711 -- definition, then the lower bound is that of the index subtype as
2712 -- specified by (RM 4.5.3(6)).
2714 -- The right test here is to go to the root type, and then the ultimate
2715 -- ancestor is the first subtype of this root type.
2717 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
2719 Make_Attribute_Reference (Loc,
2721 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
2722 Attribute_Name => Name_First);
2724 -- If the first operand in the list has known length we know that
2725 -- the lower bound of the result is the lower bound of this operand.
2727 elsif Is_Fixed_Length (1) then
2728 Low_Bound := Opnd_Low_Bound (1);
2730 -- OK, we don't know the lower bound, we have to build a horrible
2731 -- expression actions node of the form
2733 -- if Cond1'Length /= 0 then
2736 -- if Opnd2'Length /= 0 then
2741 -- The nesting ends either when we hit an operand whose length is known
2742 -- at compile time, or on reaching the last operand, whose low bound we
2743 -- take unconditionally whether or not it is null. It's easiest to do
2744 -- this with a recursive procedure:
2748 function Get_Known_Bound (J : Nat) return Node_Id;
2749 -- Returns the lower bound determined by operands J .. NN
2751 ---------------------
2752 -- Get_Known_Bound --
2753 ---------------------
2755 function Get_Known_Bound (J : Nat) return Node_Id is
2757 if Is_Fixed_Length (J) or else J = NN then
2758 return New_Copy (Opnd_Low_Bound (J));
2762 Make_Conditional_Expression (Loc,
2763 Expressions => New_List (
2766 Left_Opnd => New_Reference_To (Var_Length (J), Loc),
2767 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2769 New_Copy (Opnd_Low_Bound (J)),
2770 Get_Known_Bound (J + 1)));
2772 end Get_Known_Bound;
2775 Ent := Make_Temporary (Loc, 'L');
2778 Make_Object_Declaration (Loc,
2779 Defining_Identifier => Ent,
2780 Constant_Present => True,
2781 Object_Definition => New_Occurrence_Of (Ityp, Loc),
2782 Expression => Get_Known_Bound (1)));
2784 Low_Bound := New_Reference_To (Ent, Loc);
2788 -- Now we can safely compute the upper bound, normally
2789 -- Low_Bound + Length - 1.
2794 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2796 Make_Op_Subtract (Loc,
2797 Left_Opnd => New_Copy (Aggr_Length (NN)),
2798 Right_Opnd => Make_Artyp_Literal (1))));
2800 -- Note that calculation of the high bound may cause overflow in some
2801 -- very weird cases, so in the general case we need an overflow check on
2802 -- the high bound. We can avoid this for the common case of string types
2803 -- and other types whose index is Positive, since we chose a wider range
2804 -- for the arithmetic type.
2806 if Istyp /= Standard_Positive then
2807 Activate_Overflow_Check (High_Bound);
2810 -- Handle the exceptional case where the result is null, in which case
2811 -- case the bounds come from the last operand (so that we get the proper
2812 -- bounds if the last operand is super-flat).
2814 if Result_May_Be_Null then
2816 Make_Conditional_Expression (Loc,
2817 Expressions => New_List (
2819 Left_Opnd => New_Copy (Aggr_Length (NN)),
2820 Right_Opnd => Make_Artyp_Literal (0)),
2821 Last_Opnd_High_Bound,
2825 -- Here is where we insert the saved up actions
2827 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
2829 -- Now we construct an array object with appropriate bounds. We mark
2830 -- the target as internal to prevent useless initialization when
2831 -- Initialize_Scalars is enabled.
2833 Ent := Make_Temporary (Loc, 'S');
2834 Set_Is_Internal (Ent);
2836 -- If the bound is statically known to be out of range, we do not want
2837 -- to abort, we want a warning and a runtime constraint error. Note that
2838 -- we have arranged that the result will not be treated as a static
2839 -- constant, so we won't get an illegality during this insertion.
2841 Insert_Action (Cnode,
2842 Make_Object_Declaration (Loc,
2843 Defining_Identifier => Ent,
2844 Object_Definition =>
2845 Make_Subtype_Indication (Loc,
2846 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
2848 Make_Index_Or_Discriminant_Constraint (Loc,
2849 Constraints => New_List (
2851 Low_Bound => Low_Bound,
2852 High_Bound => High_Bound))))),
2853 Suppress => All_Checks);
2855 -- If the result of the concatenation appears as the initializing
2856 -- expression of an object declaration, we can just rename the
2857 -- result, rather than copying it.
2859 Set_OK_To_Rename (Ent);
2861 -- Catch the static out of range case now
2863 if Raises_Constraint_Error (High_Bound) then
2864 raise Concatenation_Error;
2867 -- Now we will generate the assignments to do the actual concatenation
2869 -- There is one case in which we will not do this, namely when all the
2870 -- following conditions are met:
2872 -- The result type is Standard.String
2874 -- There are nine or fewer retained (non-null) operands
2876 -- The optimization level is -O0
2878 -- The corresponding System.Concat_n.Str_Concat_n routine is
2879 -- available in the run time.
2881 -- The debug flag gnatd.c is not set
2883 -- If all these conditions are met then we generate a call to the
2884 -- relevant concatenation routine. The purpose of this is to avoid
2885 -- undesirable code bloat at -O0.
2887 if Atyp = Standard_String
2888 and then NN in 2 .. 9
2889 and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
2890 and then not Debug_Flag_Dot_C
2893 RR : constant array (Nat range 2 .. 9) of RE_Id :=
2904 if RTE_Available (RR (NN)) then
2906 Opnds : constant List_Id :=
2907 New_List (New_Occurrence_Of (Ent, Loc));
2910 for J in 1 .. NN loop
2911 if Is_List_Member (Operands (J)) then
2912 Remove (Operands (J));
2915 if Base_Type (Etype (Operands (J))) = Ctyp then
2917 Make_Aggregate (Loc,
2918 Component_Associations => New_List (
2919 Make_Component_Association (Loc,
2920 Choices => New_List (
2921 Make_Integer_Literal (Loc, 1)),
2922 Expression => Operands (J)))));
2925 Append_To (Opnds, Operands (J));
2929 Insert_Action (Cnode,
2930 Make_Procedure_Call_Statement (Loc,
2931 Name => New_Reference_To (RTE (RR (NN)), Loc),
2932 Parameter_Associations => Opnds));
2934 Result := New_Reference_To (Ent, Loc);
2941 -- Not special case so generate the assignments
2943 Known_Non_Null_Operand_Seen := False;
2945 for J in 1 .. NN loop
2947 Lo : constant Node_Id :=
2949 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2950 Right_Opnd => Aggr_Length (J - 1));
2952 Hi : constant Node_Id :=
2954 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2956 Make_Op_Subtract (Loc,
2957 Left_Opnd => Aggr_Length (J),
2958 Right_Opnd => Make_Artyp_Literal (1)));
2961 -- Singleton case, simple assignment
2963 if Base_Type (Etype (Operands (J))) = Ctyp then
2964 Known_Non_Null_Operand_Seen := True;
2965 Insert_Action (Cnode,
2966 Make_Assignment_Statement (Loc,
2968 Make_Indexed_Component (Loc,
2969 Prefix => New_Occurrence_Of (Ent, Loc),
2970 Expressions => New_List (To_Ityp (Lo))),
2971 Expression => Operands (J)),
2972 Suppress => All_Checks);
2974 -- Array case, slice assignment, skipped when argument is fixed
2975 -- length and known to be null.
2977 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
2980 Make_Assignment_Statement (Loc,
2984 New_Occurrence_Of (Ent, Loc),
2987 Low_Bound => To_Ityp (Lo),
2988 High_Bound => To_Ityp (Hi))),
2989 Expression => Operands (J));
2991 if Is_Fixed_Length (J) then
2992 Known_Non_Null_Operand_Seen := True;
2994 elsif not Known_Non_Null_Operand_Seen then
2996 -- Here if operand length is not statically known and no
2997 -- operand known to be non-null has been processed yet.
2998 -- If operand length is 0, we do not need to perform the
2999 -- assignment, and we must avoid the evaluation of the
3000 -- high bound of the slice, since it may underflow if the
3001 -- low bound is Ityp'First.
3004 Make_Implicit_If_Statement (Cnode,
3008 New_Occurrence_Of (Var_Length (J), Loc),
3009 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3014 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3020 -- Finally we build the result, which is a reference to the array object
3022 Result := New_Reference_To (Ent, Loc);
3025 Rewrite (Cnode, Result);
3026 Analyze_And_Resolve (Cnode, Atyp);
3029 when Concatenation_Error =>
3031 -- Kill warning generated for the declaration of the static out of
3032 -- range high bound, and instead generate a Constraint_Error with
3033 -- an appropriate specific message.
3035 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3036 Apply_Compile_Time_Constraint_Error
3038 Msg => "concatenation result upper bound out of range?",
3039 Reason => CE_Range_Check_Failed);
3040 -- Set_Etype (Cnode, Atyp);
3041 end Expand_Concatenate;
3043 ------------------------
3044 -- Expand_N_Allocator --
3045 ------------------------
3047 procedure Expand_N_Allocator (N : Node_Id) is
3048 PtrT : constant Entity_Id := Etype (N);
3049 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
3050 Etyp : constant Entity_Id := Etype (Expression (N));
3051 Loc : constant Source_Ptr := Sloc (N);
3056 procedure Complete_Coextension_Finalization;
3057 -- Generate finalization calls for all nested coextensions of N. This
3058 -- routine may allocate list controllers if necessary.
3060 procedure Rewrite_Coextension (N : Node_Id);
3061 -- Static coextensions have the same lifetime as the entity they
3062 -- constrain. Such occurrences can be rewritten as aliased objects
3063 -- and their unrestricted access used instead of the coextension.
3065 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
3066 -- Given a constrained array type E, returns a node representing the
3067 -- code to compute the size in storage elements for the given type.
3068 -- This is done without using the attribute (which malfunctions for
3071 ---------------------------------------
3072 -- Complete_Coextension_Finalization --
3073 ---------------------------------------
3075 procedure Complete_Coextension_Finalization is
3077 Coext_Elmt : Elmt_Id;
3081 function Inside_A_Return_Statement (N : Node_Id) return Boolean;
3082 -- Determine whether node N is part of a return statement
3084 function Needs_Initialization_Call (N : Node_Id) return Boolean;
3085 -- Determine whether node N is a subtype indicator allocator which
3086 -- acts a coextension. Such coextensions need initialization.
3088 -------------------------------
3089 -- Inside_A_Return_Statement --
3090 -------------------------------
3092 function Inside_A_Return_Statement (N : Node_Id) return Boolean is
3097 while Present (P) loop
3099 (P, N_Extended_Return_Statement, N_Simple_Return_Statement)
3103 -- Stop the traversal when we reach a subprogram body
3105 elsif Nkind (P) = N_Subprogram_Body then
3113 end Inside_A_Return_Statement;
3115 -------------------------------
3116 -- Needs_Initialization_Call --
3117 -------------------------------
3119 function Needs_Initialization_Call (N : Node_Id) return Boolean is
3123 if Nkind (N) = N_Explicit_Dereference
3124 and then Nkind (Prefix (N)) = N_Identifier
3125 and then Nkind (Parent (Entity (Prefix (N)))) =
3126 N_Object_Declaration
3128 Obj_Decl := Parent (Entity (Prefix (N)));
3131 Present (Expression (Obj_Decl))
3132 and then Nkind (Expression (Obj_Decl)) = N_Allocator
3133 and then Nkind (Expression (Expression (Obj_Decl))) /=
3134 N_Qualified_Expression;
3138 end Needs_Initialization_Call;
3140 -- Start of processing for Complete_Coextension_Finalization
3143 -- When a coextension root is inside a return statement, we need to
3144 -- use the finalization chain of the function's scope. This does not
3145 -- apply for controlled named access types because in those cases we
3146 -- can use the finalization chain of the type itself.
3148 if Inside_A_Return_Statement (N)
3150 (Ekind (PtrT) = E_Anonymous_Access_Type
3152 (Ekind (PtrT) = E_Access_Type
3153 and then No (Associated_Final_Chain (PtrT))))
3157 Outer_S : Entity_Id;
3162 while Present (S) and then S /= Standard_Standard loop
3163 if Ekind (S) = E_Function then
3164 Outer_S := Scope (S);
3166 -- Retrieve the declaration of the body
3171 (Corresponding_Body (Parent (Parent (S)))));
3178 -- Push the scope of the function body since we are inserting
3179 -- the list before the body, but we are currently in the body
3180 -- itself. Override the finalization list of PtrT since the
3181 -- finalization context is now different.
3183 Push_Scope (Outer_S);
3184 Build_Final_List (Decl, PtrT);
3188 -- The root allocator may not be controlled, but it still needs a
3189 -- finalization list for all nested coextensions.
3191 elsif No (Associated_Final_Chain (PtrT)) then
3192 Build_Final_List (N, PtrT);
3196 Make_Selected_Component (Loc,
3198 New_Reference_To (Associated_Final_Chain (PtrT), Loc),
3200 Make_Identifier (Loc, Name_F));
3202 Coext_Elmt := First_Elmt (Coextensions (N));
3203 while Present (Coext_Elmt) loop
3204 Coext := Node (Coext_Elmt);
3209 if Nkind (Coext) = N_Identifier then
3211 Make_Unchecked_Type_Conversion (Loc,
3212 Subtype_Mark => New_Reference_To (Etype (Coext), Loc),
3214 Make_Explicit_Dereference (Loc,
3215 Prefix => New_Copy_Tree (Coext)));
3217 Ref := New_Copy_Tree (Coext);
3220 -- No initialization call if not allowed
3222 Check_Restriction (No_Default_Initialization, N);
3224 if not Restriction_Active (No_Default_Initialization) then
3228 -- attach_to_final_list (Ref, Flist, 2)
3230 if Needs_Initialization_Call (Coext) then
3234 Typ => Etype (Coext),
3236 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3239 -- attach_to_final_list (Ref, Flist, 2)
3245 Flist_Ref => New_Copy_Tree (Flist),
3246 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3250 Next_Elmt (Coext_Elmt);
3252 end Complete_Coextension_Finalization;
3254 -------------------------
3255 -- Rewrite_Coextension --
3256 -------------------------
3258 procedure Rewrite_Coextension (N : Node_Id) is
3259 Temp : constant Node_Id := Make_Temporary (Loc, 'C');
3262 -- Cnn : aliased Etyp;
3264 Decl : constant Node_Id :=
3265 Make_Object_Declaration (Loc,
3266 Defining_Identifier => Temp,
3267 Aliased_Present => True,
3268 Object_Definition =>
3269 New_Occurrence_Of (Etyp, Loc));
3273 if Nkind (Expression (N)) = N_Qualified_Expression then
3274 Set_Expression (Decl, Expression (Expression (N)));
3277 -- Find the proper insertion node for the declaration
3280 while Present (Nod) loop
3281 exit when Nkind (Nod) in N_Statement_Other_Than_Procedure_Call
3282 or else Nkind (Nod) = N_Procedure_Call_Statement
3283 or else Nkind (Nod) in N_Declaration;
3284 Nod := Parent (Nod);
3287 Insert_Before (Nod, Decl);
3291 Make_Attribute_Reference (Loc,
3292 Prefix => New_Occurrence_Of (Temp, Loc),
3293 Attribute_Name => Name_Unrestricted_Access));
3295 Analyze_And_Resolve (N, PtrT);
3296 end Rewrite_Coextension;
3298 ------------------------------
3299 -- Size_In_Storage_Elements --
3300 ------------------------------
3302 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
3304 -- Logically this just returns E'Max_Size_In_Storage_Elements.
3305 -- However, the reason for the existence of this function is
3306 -- to construct a test for sizes too large, which means near the
3307 -- 32-bit limit on a 32-bit machine, and precisely the trouble
3308 -- is that we get overflows when sizes are greater than 2**31.
3310 -- So what we end up doing for array types is to use the expression:
3312 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
3314 -- which avoids this problem. All this is a big bogus, but it does
3315 -- mean we catch common cases of trying to allocate arrays that
3316 -- are too large, and which in the absence of a check results in
3317 -- undetected chaos ???
3324 for J in 1 .. Number_Dimensions (E) loop
3326 Make_Attribute_Reference (Loc,
3327 Prefix => New_Occurrence_Of (E, Loc),
3328 Attribute_Name => Name_Length,
3329 Expressions => New_List (
3330 Make_Integer_Literal (Loc, J)));
3337 Make_Op_Multiply (Loc,
3344 Make_Op_Multiply (Loc,
3347 Make_Attribute_Reference (Loc,
3348 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
3349 Attribute_Name => Name_Max_Size_In_Storage_Elements));
3351 end Size_In_Storage_Elements;
3353 -- Start of processing for Expand_N_Allocator
3356 -- RM E.2.3(22). We enforce that the expected type of an allocator
3357 -- shall not be a remote access-to-class-wide-limited-private type
3359 -- Why is this being done at expansion time, seems clearly wrong ???
3361 Validate_Remote_Access_To_Class_Wide_Type (N);
3363 -- Set the Storage Pool
3365 Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT)));
3367 if Present (Storage_Pool (N)) then
3368 if Is_RTE (Storage_Pool (N), RE_SS_Pool) then
3369 if VM_Target = No_VM then
3370 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3373 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
3374 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
3377 Set_Procedure_To_Call (N,
3378 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
3382 -- Under certain circumstances we can replace an allocator by an access
3383 -- to statically allocated storage. The conditions, as noted in AARM
3384 -- 3.10 (10c) are as follows:
3386 -- Size and initial value is known at compile time
3387 -- Access type is access-to-constant
3389 -- The allocator is not part of a constraint on a record component,
3390 -- because in that case the inserted actions are delayed until the
3391 -- record declaration is fully analyzed, which is too late for the
3392 -- analysis of the rewritten allocator.
3394 if Is_Access_Constant (PtrT)
3395 and then Nkind (Expression (N)) = N_Qualified_Expression
3396 and then Compile_Time_Known_Value (Expression (Expression (N)))
3397 and then Size_Known_At_Compile_Time (Etype (Expression
3399 and then not Is_Record_Type (Current_Scope)
3401 -- Here we can do the optimization. For the allocator
3405 -- We insert an object declaration
3407 -- Tnn : aliased x := y;
3409 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
3410 -- marked as requiring static allocation.
3412 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
3413 Desig := Subtype_Mark (Expression (N));
3415 -- If context is constrained, use constrained subtype directly,
3416 -- so that the constant is not labelled as having a nominally
3417 -- unconstrained subtype.
3419 if Entity (Desig) = Base_Type (Dtyp) then
3420 Desig := New_Occurrence_Of (Dtyp, Loc);
3424 Make_Object_Declaration (Loc,
3425 Defining_Identifier => Temp,
3426 Aliased_Present => True,
3427 Constant_Present => Is_Access_Constant (PtrT),
3428 Object_Definition => Desig,
3429 Expression => Expression (Expression (N))));
3432 Make_Attribute_Reference (Loc,
3433 Prefix => New_Occurrence_Of (Temp, Loc),
3434 Attribute_Name => Name_Unrestricted_Access));
3436 Analyze_And_Resolve (N, PtrT);
3438 -- We set the variable as statically allocated, since we don't want
3439 -- it going on the stack of the current procedure!
3441 Set_Is_Statically_Allocated (Temp);
3445 -- Same if the allocator is an access discriminant for a local object:
3446 -- instead of an allocator we create a local value and constrain the
3447 -- the enclosing object with the corresponding access attribute.
3449 if Is_Static_Coextension (N) then
3450 Rewrite_Coextension (N);
3454 -- The current allocator creates an object which may contain nested
3455 -- coextensions. Use the current allocator's finalization list to
3456 -- generate finalization call for all nested coextensions.
3458 if Is_Coextension_Root (N) then
3459 Complete_Coextension_Finalization;
3462 -- Check for size too large, we do this because the back end misses
3463 -- proper checks here and can generate rubbish allocation calls when
3464 -- we are near the limit. We only do this for the 32-bit address case
3465 -- since that is from a practical point of view where we see a problem.
3467 if System_Address_Size = 32
3468 and then not Storage_Checks_Suppressed (PtrT)
3469 and then not Storage_Checks_Suppressed (Dtyp)
3470 and then not Storage_Checks_Suppressed (Etyp)
3472 -- The check we want to generate should look like
3474 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
3475 -- raise Storage_Error;
3478 -- where 3.5 gigabytes is a constant large enough to accomodate any
3479 -- reasonable request for. But we can't do it this way because at
3480 -- least at the moment we don't compute this attribute right, and
3481 -- can silently give wrong results when the result gets large. Since
3482 -- this is all about large results, that's bad, so instead we only
3483 -- apply the check for constrained arrays, and manually compute the
3484 -- value of the attribute ???
3486 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
3488 Make_Raise_Storage_Error (Loc,
3491 Left_Opnd => Size_In_Storage_Elements (Etyp),
3493 Make_Integer_Literal (Loc,
3494 Intval => Uint_7 * (Uint_2 ** 29))),
3495 Reason => SE_Object_Too_Large));
3499 -- Handle case of qualified expression (other than optimization above)
3500 -- First apply constraint checks, because the bounds or discriminants
3501 -- in the aggregate might not match the subtype mark in the allocator.
3503 if Nkind (Expression (N)) = N_Qualified_Expression then
3504 Apply_Constraint_Check
3505 (Expression (Expression (N)), Etype (Expression (N)));
3507 Expand_Allocator_Expression (N);
3511 -- If the allocator is for a type which requires initialization, and
3512 -- there is no initial value (i.e. operand is a subtype indication
3513 -- rather than a qualified expression), then we must generate a call to
3514 -- the initialization routine using an expressions action node:
3516 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
3518 -- Here ptr_T is the pointer type for the allocator, and T is the
3519 -- subtype of the allocator. A special case arises if the designated
3520 -- type of the access type is a task or contains tasks. In this case
3521 -- the call to Init (Temp.all ...) is replaced by code that ensures
3522 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
3523 -- for details). In addition, if the type T is a task T, then the
3524 -- first argument to Init must be converted to the task record type.
3527 T : constant Entity_Id := Entity (Expression (N));
3535 Temp_Decl : Node_Id;
3536 Temp_Type : Entity_Id;
3537 Attach_Level : Uint;
3540 if No_Initialization (N) then
3543 -- Case of no initialization procedure present
3545 elsif not Has_Non_Null_Base_Init_Proc (T) then
3547 -- Case of simple initialization required
3549 if Needs_Simple_Initialization (T) then
3550 Check_Restriction (No_Default_Initialization, N);
3551 Rewrite (Expression (N),
3552 Make_Qualified_Expression (Loc,
3553 Subtype_Mark => New_Occurrence_Of (T, Loc),
3554 Expression => Get_Simple_Init_Val (T, N)));
3556 Analyze_And_Resolve (Expression (Expression (N)), T);
3557 Analyze_And_Resolve (Expression (N), T);
3558 Set_Paren_Count (Expression (Expression (N)), 1);
3559 Expand_N_Allocator (N);
3561 -- No initialization required
3567 -- Case of initialization procedure present, must be called
3570 Check_Restriction (No_Default_Initialization, N);
3572 if not Restriction_Active (No_Default_Initialization) then
3573 Init := Base_Init_Proc (T);
3575 Temp := Make_Temporary (Loc, 'P');
3577 -- Construct argument list for the initialization routine call
3580 Make_Explicit_Dereference (Loc,
3581 Prefix => New_Reference_To (Temp, Loc));
3582 Set_Assignment_OK (Arg1);
3585 -- The initialization procedure expects a specific type. if the
3586 -- context is access to class wide, indicate that the object
3587 -- being allocated has the right specific type.
3589 if Is_Class_Wide_Type (Dtyp) then
3590 Arg1 := Unchecked_Convert_To (T, Arg1);
3593 -- If designated type is a concurrent type or if it is private
3594 -- type whose definition is a concurrent type, the first
3595 -- argument in the Init routine has to be unchecked conversion
3596 -- to the corresponding record type. If the designated type is
3597 -- a derived type, we also convert the argument to its root
3600 if Is_Concurrent_Type (T) then
3602 Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1);
3604 elsif Is_Private_Type (T)
3605 and then Present (Full_View (T))
3606 and then Is_Concurrent_Type (Full_View (T))
3609 Unchecked_Convert_To
3610 (Corresponding_Record_Type (Full_View (T)), Arg1);
3612 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
3614 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
3616 Arg1 := OK_Convert_To (Etype (Ftyp), Arg1);
3617 Set_Etype (Arg1, Ftyp);
3621 Args := New_List (Arg1);
3623 -- For the task case, pass the Master_Id of the access type as
3624 -- the value of the _Master parameter, and _Chain as the value
3625 -- of the _Chain parameter (_Chain will be defined as part of
3626 -- the generated code for the allocator).
3628 -- In Ada 2005, the context may be a function that returns an
3629 -- anonymous access type. In that case the Master_Id has been
3630 -- created when expanding the function declaration.
3632 if Has_Task (T) then
3633 if No (Master_Id (Base_Type (PtrT))) then
3635 -- If we have a non-library level task with restriction
3636 -- No_Task_Hierarchy set, then no point in expanding.
3638 if not Is_Library_Level_Entity (T)
3639 and then Restriction_Active (No_Task_Hierarchy)
3644 -- The designated type was an incomplete type, and the
3645 -- access type did not get expanded. Salvage it now.
3647 if not Restriction_Active (No_Task_Hierarchy) then
3648 pragma Assert (Present (Parent (Base_Type (PtrT))));
3649 Expand_N_Full_Type_Declaration
3650 (Parent (Base_Type (PtrT)));
3654 -- If the context of the allocator is a declaration or an
3655 -- assignment, we can generate a meaningful image for it,
3656 -- even though subsequent assignments might remove the
3657 -- connection between task and entity. We build this image
3658 -- when the left-hand side is a simple variable, a simple
3659 -- indexed assignment or a simple selected component.
3661 if Nkind (Parent (N)) = N_Assignment_Statement then
3663 Nam : constant Node_Id := Name (Parent (N));
3666 if Is_Entity_Name (Nam) then
3668 Build_Task_Image_Decls
3671 (Entity (Nam), Sloc (Nam)), T);
3674 (Nam, N_Indexed_Component, N_Selected_Component)
3675 and then Is_Entity_Name (Prefix (Nam))
3678 Build_Task_Image_Decls
3679 (Loc, Nam, Etype (Prefix (Nam)));
3681 Decls := Build_Task_Image_Decls (Loc, T, T);
3685 elsif Nkind (Parent (N)) = N_Object_Declaration then
3687 Build_Task_Image_Decls
3688 (Loc, Defining_Identifier (Parent (N)), T);
3691 Decls := Build_Task_Image_Decls (Loc, T, T);
3694 if Restriction_Active (No_Task_Hierarchy) then
3695 -- 3 is System.Tasking.Library_Task_Level
3696 Append_To (Args, Make_Integer_Literal (Loc, 3));
3700 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
3703 Append_To (Args, Make_Identifier (Loc, Name_uChain));
3705 Decl := Last (Decls);
3707 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
3709 -- Has_Task is false, Decls not used
3715 -- Add discriminants if discriminated type
3718 Dis : Boolean := False;
3722 if Has_Discriminants (T) then
3726 elsif Is_Private_Type (T)
3727 and then Present (Full_View (T))
3728 and then Has_Discriminants (Full_View (T))
3731 Typ := Full_View (T);
3736 -- If the allocated object will be constrained by the
3737 -- default values for discriminants, then build a subtype
3738 -- with those defaults, and change the allocated subtype
3739 -- to that. Note that this happens in fewer cases in Ada
3742 if not Is_Constrained (Typ)
3743 and then Present (Discriminant_Default_Value
3744 (First_Discriminant (Typ)))
3745 and then (Ada_Version < Ada_05
3747 not Has_Constrained_Partial_View (Typ))
3749 Typ := Build_Default_Subtype (Typ, N);
3750 Set_Expression (N, New_Reference_To (Typ, Loc));
3753 Discr := First_Elmt (Discriminant_Constraint (Typ));
3754 while Present (Discr) loop
3755 Nod := Node (Discr);
3756 Append (New_Copy_Tree (Node (Discr)), Args);
3758 -- AI-416: when the discriminant constraint is an
3759 -- anonymous access type make sure an accessibility
3760 -- check is inserted if necessary (3.10.2(22.q/2))
3762 if Ada_Version >= Ada_05
3764 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
3766 Apply_Accessibility_Check
3767 (Nod, Typ, Insert_Node => Nod);
3775 -- We set the allocator as analyzed so that when we analyze the
3776 -- expression actions node, we do not get an unwanted recursive
3777 -- expansion of the allocator expression.
3779 Set_Analyzed (N, True);
3780 Nod := Relocate_Node (N);
3782 -- Here is the transformation:
3784 -- output: Temp : constant ptr_T := new T;
3785 -- Init (Temp.all, ...);
3786 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
3787 -- <CTRL> Initialize (Finalizable (Temp.all));
3789 -- Here ptr_T is the pointer type for the allocator, and is the
3790 -- subtype of the allocator.
3793 Make_Object_Declaration (Loc,
3794 Defining_Identifier => Temp,
3795 Constant_Present => True,
3796 Object_Definition => New_Reference_To (Temp_Type, Loc),
3799 Set_Assignment_OK (Temp_Decl);
3800 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
3802 -- If the designated type is a task type or contains tasks,
3803 -- create block to activate created tasks, and insert
3804 -- declaration for Task_Image variable ahead of call.
3806 if Has_Task (T) then
3808 L : constant List_Id := New_List;
3811 Build_Task_Allocate_Block (L, Nod, Args);
3813 Insert_List_Before (First (Declarations (Blk)), Decls);
3814 Insert_Actions (N, L);
3819 Make_Procedure_Call_Statement (Loc,
3820 Name => New_Reference_To (Init, Loc),
3821 Parameter_Associations => Args));
3824 if Needs_Finalization (T) then
3826 -- Postpone the generation of a finalization call for the
3827 -- current allocator if it acts as a coextension.
3829 if Is_Dynamic_Coextension (N) then
3830 if No (Coextensions (N)) then
3831 Set_Coextensions (N, New_Elmt_List);
3834 Append_Elmt (New_Copy_Tree (Arg1), Coextensions (N));
3838 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
3840 -- Anonymous access types created for access parameters
3841 -- are attached to an explicitly constructed controller,
3842 -- which ensures that they can be finalized properly,
3843 -- even if their deallocation might not happen. The list
3844 -- associated with the controller is doubly-linked. For
3845 -- other anonymous access types, the object may end up
3846 -- on the global final list which is singly-linked.
3847 -- Work needed for access discriminants in Ada 2005 ???
3849 if Ekind (PtrT) = E_Anonymous_Access_Type then
3850 Attach_Level := Uint_1;
3852 Attach_Level := Uint_2;
3857 Ref => New_Copy_Tree (Arg1),
3860 With_Attach => Make_Integer_Literal (Loc,
3861 Intval => Attach_Level)));
3865 Rewrite (N, New_Reference_To (Temp, Loc));
3866 Analyze_And_Resolve (N, PtrT);
3871 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
3872 -- object that has been rewritten as a reference, we displace "this"
3873 -- to reference properly its secondary dispatch table.
3875 if Nkind (N) = N_Identifier
3876 and then Is_Interface (Dtyp)
3878 Displace_Allocator_Pointer (N);
3882 when RE_Not_Available =>
3884 end Expand_N_Allocator;
3886 -----------------------
3887 -- Expand_N_And_Then --
3888 -----------------------
3890 procedure Expand_N_And_Then (N : Node_Id)
3891 renames Expand_Short_Circuit_Operator;
3893 ------------------------------
3894 -- Expand_N_Case_Expression --
3895 ------------------------------
3897 procedure Expand_N_Case_Expression (N : Node_Id) is
3898 Loc : constant Source_Ptr := Sloc (N);
3899 Typ : constant Entity_Id := Etype (N);
3911 -- case X is when A => AX, when B => BX ...
3926 -- However, this expansion is wrong for limited types, and also
3927 -- wrong for unconstrained types (since the bounds may not be the
3928 -- same in all branches). Furthermore it involves an extra copy
3929 -- for large objects. So we take care of this by using the following
3930 -- modified expansion for non-scalar types:
3933 -- type Pnn is access all typ;
3937 -- T := AX'Unrestricted_Access;
3939 -- T := BX'Unrestricted_Access;
3945 Make_Case_Statement (Loc,
3946 Expression => Expression (N),
3947 Alternatives => New_List);
3949 Actions := New_List;
3953 if Is_Scalar_Type (Typ) then
3957 Pnn := Make_Temporary (Loc, 'P');
3959 Make_Full_Type_Declaration (Loc,
3960 Defining_Identifier => Pnn,
3962 Make_Access_To_Object_Definition (Loc,
3963 All_Present => True,
3964 Subtype_Indication =>
3965 New_Reference_To (Typ, Loc))));
3969 Tnn := Make_Temporary (Loc, 'T');
3971 Make_Object_Declaration (Loc,
3972 Defining_Identifier => Tnn,
3973 Object_Definition => New_Occurrence_Of (Ttyp, Loc)));
3975 -- Now process the alternatives
3977 Alt := First (Alternatives (N));
3978 while Present (Alt) loop
3980 Aexp : Node_Id := Expression (Alt);
3981 Aloc : constant Source_Ptr := Sloc (Aexp);
3984 if not Is_Scalar_Type (Typ) then
3986 Make_Attribute_Reference (Aloc,
3987 Prefix => Relocate_Node (Aexp),
3988 Attribute_Name => Name_Unrestricted_Access);
3992 (Alternatives (Cstmt),
3993 Make_Case_Statement_Alternative (Sloc (Alt),
3994 Discrete_Choices => Discrete_Choices (Alt),
3995 Statements => New_List (
3996 Make_Assignment_Statement (Aloc,
3997 Name => New_Occurrence_Of (Tnn, Loc),
3998 Expression => Aexp))));
4004 Append_To (Actions, Cstmt);
4006 -- Construct and return final expression with actions
4008 if Is_Scalar_Type (Typ) then
4009 Fexp := New_Occurrence_Of (Tnn, Loc);
4012 Make_Explicit_Dereference (Loc,
4013 Prefix => New_Occurrence_Of (Tnn, Loc));
4017 Make_Expression_With_Actions (Loc,
4019 Actions => Actions));
4021 Analyze_And_Resolve (N, Typ);
4022 end Expand_N_Case_Expression;
4024 -------------------------------------
4025 -- Expand_N_Conditional_Expression --
4026 -------------------------------------
4028 -- Deal with limited types and expression actions
4030 procedure Expand_N_Conditional_Expression (N : Node_Id) is
4031 Loc : constant Source_Ptr := Sloc (N);
4032 Cond : constant Node_Id := First (Expressions (N));
4033 Thenx : constant Node_Id := Next (Cond);
4034 Elsex : constant Node_Id := Next (Thenx);
4035 Typ : constant Entity_Id := Etype (N);
4046 -- Fold at compile time if condition known. We have already folded
4047 -- static conditional expressions, but it is possible to fold any
4048 -- case in which the condition is known at compile time, even though
4049 -- the result is non-static.
4051 -- Note that we don't do the fold of such cases in Sem_Elab because
4052 -- it can cause infinite loops with the expander adding a conditional
4053 -- expression, and Sem_Elab circuitry removing it repeatedly.
4055 if Compile_Time_Known_Value (Cond) then
4056 if Is_True (Expr_Value (Cond)) then
4058 Actions := Then_Actions (N);
4061 Actions := Else_Actions (N);
4066 if Present (Actions) then
4068 -- If we are not allowed to use Expression_With_Actions, just
4069 -- skip the optimization, it is not critical for correctness.
4071 if not Use_Expression_With_Actions then
4072 goto Skip_Optimization;
4076 Make_Expression_With_Actions (Loc,
4077 Expression => Relocate_Node (Expr),
4078 Actions => Actions));
4079 Analyze_And_Resolve (N, Typ);
4082 Rewrite (N, Relocate_Node (Expr));
4085 -- Note that the result is never static (legitimate cases of static
4086 -- conditional expressions were folded in Sem_Eval).
4088 Set_Is_Static_Expression (N, False);
4092 <<Skip_Optimization>>
4094 -- If the type is limited or unconstrained, we expand as follows to
4095 -- avoid any possibility of improper copies.
4097 -- Note: it may be possible to avoid this special processing if the
4098 -- back end uses its own mechanisms for handling by-reference types ???
4100 -- type Ptr is access all Typ;
4104 -- Cnn := then-expr'Unrestricted_Access;
4107 -- Cnn := else-expr'Unrestricted_Access;
4110 -- and replace the conditional expresion by a reference to Cnn.all.
4112 -- This special case can be skipped if the back end handles limited
4113 -- types properly and ensures that no incorrect copies are made.
4115 if Is_By_Reference_Type (Typ)
4116 and then not Back_End_Handles_Limited_Types
4118 Cnn := Make_Temporary (Loc, 'C', N);
4121 Make_Full_Type_Declaration (Loc,
4122 Defining_Identifier => Make_Temporary (Loc, 'A'),
4124 Make_Access_To_Object_Definition (Loc,
4125 All_Present => True,
4126 Subtype_Indication =>
4127 New_Reference_To (Typ, Loc)));
4129 Insert_Action (N, P_Decl);
4132 Make_Object_Declaration (Loc,
4133 Defining_Identifier => Cnn,
4134 Object_Definition =>
4135 New_Occurrence_Of (Defining_Identifier (P_Decl), Loc));
4138 Make_Implicit_If_Statement (N,
4139 Condition => Relocate_Node (Cond),
4141 Then_Statements => New_List (
4142 Make_Assignment_Statement (Sloc (Thenx),
4143 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
4145 Make_Attribute_Reference (Loc,
4146 Attribute_Name => Name_Unrestricted_Access,
4147 Prefix => Relocate_Node (Thenx)))),
4149 Else_Statements => New_List (
4150 Make_Assignment_Statement (Sloc (Elsex),
4151 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
4153 Make_Attribute_Reference (Loc,
4154 Attribute_Name => Name_Unrestricted_Access,
4155 Prefix => Relocate_Node (Elsex)))));
4158 Make_Explicit_Dereference (Loc,
4159 Prefix => New_Occurrence_Of (Cnn, Loc));
4161 -- For other types, we only need to expand if there are other actions
4162 -- associated with either branch.
4164 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
4166 -- We have two approaches to handling this. If we are allowed to use
4167 -- N_Expression_With_Actions, then we can just wrap the actions into
4168 -- the appropriate expression.
4170 if Use_Expression_With_Actions then
4171 if Present (Then_Actions (N)) then
4173 Make_Expression_With_Actions (Sloc (Thenx),
4174 Actions => Then_Actions (N),
4175 Expression => Relocate_Node (Thenx)));
4176 Set_Then_Actions (N, No_List);
4177 Analyze_And_Resolve (Thenx, Typ);
4180 if Present (Else_Actions (N)) then
4182 Make_Expression_With_Actions (Sloc (Elsex),
4183 Actions => Else_Actions (N),
4184 Expression => Relocate_Node (Elsex)));
4185 Set_Else_Actions (N, No_List);
4186 Analyze_And_Resolve (Elsex, Typ);
4191 -- if we can't use N_Expression_With_Actions nodes, then we insert
4192 -- the following sequence of actions (using Insert_Actions):
4197 -- Cnn := then-expr;
4203 -- and replace the conditional expression by a reference to Cnn
4206 Cnn := Make_Temporary (Loc, 'C', N);
4209 Make_Object_Declaration (Loc,
4210 Defining_Identifier => Cnn,
4211 Object_Definition => New_Occurrence_Of (Typ, Loc));
4214 Make_Implicit_If_Statement (N,
4215 Condition => Relocate_Node (Cond),
4217 Then_Statements => New_List (
4218 Make_Assignment_Statement (Sloc (Thenx),
4219 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
4220 Expression => Relocate_Node (Thenx))),
4222 Else_Statements => New_List (
4223 Make_Assignment_Statement (Sloc (Elsex),
4224 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
4225 Expression => Relocate_Node (Elsex))));
4227 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
4228 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
4230 New_N := New_Occurrence_Of (Cnn, Loc);
4233 -- If no actions then no expansion needed, gigi will handle it using
4234 -- the same approach as a C conditional expression.
4240 -- Fall through here for either the limited expansion, or the case of
4241 -- inserting actions for non-limited types. In both these cases, we must
4242 -- move the SLOC of the parent If statement to the newly created one and
4243 -- change it to the SLOC of the expression which, after expansion, will
4244 -- correspond to what is being evaluated.
4246 if Present (Parent (N))
4247 and then Nkind (Parent (N)) = N_If_Statement
4249 Set_Sloc (New_If, Sloc (Parent (N)));
4250 Set_Sloc (Parent (N), Loc);
4253 -- Make sure Then_Actions and Else_Actions are appropriately moved
4254 -- to the new if statement.
4256 if Present (Then_Actions (N)) then
4258 (First (Then_Statements (New_If)), Then_Actions (N));
4261 if Present (Else_Actions (N)) then
4263 (First (Else_Statements (New_If)), Else_Actions (N));
4266 Insert_Action (N, Decl);
4267 Insert_Action (N, New_If);
4269 Analyze_And_Resolve (N, Typ);
4270 end Expand_N_Conditional_Expression;
4272 -----------------------------------
4273 -- Expand_N_Explicit_Dereference --
4274 -----------------------------------
4276 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
4278 -- Insert explicit dereference call for the checked storage pool case
4280 Insert_Dereference_Action (Prefix (N));
4281 end Expand_N_Explicit_Dereference;
4287 procedure Expand_N_In (N : Node_Id) is
4288 Loc : constant Source_Ptr := Sloc (N);
4289 Rtyp : constant Entity_Id := Etype (N);
4290 Lop : constant Node_Id := Left_Opnd (N);
4291 Rop : constant Node_Id := Right_Opnd (N);
4292 Static : constant Boolean := Is_OK_Static_Expression (N);
4294 procedure Expand_Set_Membership;
4295 -- For each disjunct we create a simple equality or membership test.
4296 -- The whole membership is rewritten as a short-circuit disjunction.
4298 ---------------------------
4299 -- Expand_Set_Membership --
4300 ---------------------------
4302 procedure Expand_Set_Membership is
4306 function Make_Cond (Alt : Node_Id) return Node_Id;
4307 -- If the alternative is a subtype mark, create a simple membership
4308 -- test. Otherwise create an equality test for it.
4314 function Make_Cond (Alt : Node_Id) return Node_Id is
4316 L : constant Node_Id := New_Copy (Lop);
4317 R : constant Node_Id := Relocate_Node (Alt);
4320 if Is_Entity_Name (Alt)
4321 and then Is_Type (Entity (Alt))
4324 Make_In (Sloc (Alt),
4328 Cond := Make_Op_Eq (Sloc (Alt),
4336 -- Start of proessing for Expand_N_In
4339 Alt := Last (Alternatives (N));
4340 Res := Make_Cond (Alt);
4343 while Present (Alt) loop
4345 Make_Or_Else (Sloc (Alt),
4346 Left_Opnd => Make_Cond (Alt),
4352 Analyze_And_Resolve (N, Standard_Boolean);
4353 end Expand_Set_Membership;
4355 procedure Substitute_Valid_Check;
4356 -- Replaces node N by Lop'Valid. This is done when we have an explicit
4357 -- test for the left operand being in range of its subtype.
4359 ----------------------------
4360 -- Substitute_Valid_Check --
4361 ----------------------------
4363 procedure Substitute_Valid_Check is
4366 Make_Attribute_Reference (Loc,
4367 Prefix => Relocate_Node (Lop),
4368 Attribute_Name => Name_Valid));
4370 Analyze_And_Resolve (N, Rtyp);
4372 Error_Msg_N ("?explicit membership test may be optimized away", N);
4373 Error_Msg_N -- CODEFIX
4374 ("\?use ''Valid attribute instead", N);
4376 end Substitute_Valid_Check;
4378 -- Start of processing for Expand_N_In
4381 if Present (Alternatives (N)) then
4382 Remove_Side_Effects (Lop);
4383 Expand_Set_Membership;
4387 -- Check case of explicit test for an expression in range of its
4388 -- subtype. This is suspicious usage and we replace it with a 'Valid
4389 -- test and give a warning. For floating point types however, this is a
4390 -- standard way to check for finite numbers, and using 'Valid vould
4391 -- typically be a pessimization.
4393 if Is_Scalar_Type (Etype (Lop))
4394 and then not Is_Floating_Point_Type (Etype (Lop))
4395 and then Nkind (Rop) in N_Has_Entity
4396 and then Etype (Lop) = Entity (Rop)
4397 and then Comes_From_Source (N)
4398 and then VM_Target = No_VM
4400 Substitute_Valid_Check;
4404 -- Do validity check on operands
4406 if Validity_Checks_On and Validity_Check_Operands then
4407 Ensure_Valid (Left_Opnd (N));
4408 Validity_Check_Range (Right_Opnd (N));
4411 -- Case of explicit range
4413 if Nkind (Rop) = N_Range then
4415 Lo : constant Node_Id := Low_Bound (Rop);
4416 Hi : constant Node_Id := High_Bound (Rop);
4418 Ltyp : constant Entity_Id := Etype (Lop);
4420 Lo_Orig : constant Node_Id := Original_Node (Lo);
4421 Hi_Orig : constant Node_Id := Original_Node (Hi);
4423 Lcheck : Compare_Result;
4424 Ucheck : Compare_Result;
4426 Warn1 : constant Boolean :=
4427 Constant_Condition_Warnings
4428 and then Comes_From_Source (N)
4429 and then not In_Instance;
4430 -- This must be true for any of the optimization warnings, we
4431 -- clearly want to give them only for source with the flag on. We
4432 -- also skip these warnings in an instance since it may be the
4433 -- case that different instantiations have different ranges.
4435 Warn2 : constant Boolean :=
4437 and then Nkind (Original_Node (Rop)) = N_Range
4438 and then Is_Integer_Type (Etype (Lo));
4439 -- For the case where only one bound warning is elided, we also
4440 -- insist on an explicit range and an integer type. The reason is
4441 -- that the use of enumeration ranges including an end point is
4442 -- common, as is the use of a subtype name, one of whose bounds is
4443 -- the same as the type of the expression.
4446 -- If test is explicit x'first .. x'last, replace by valid check
4448 if Is_Scalar_Type (Ltyp)
4449 and then Nkind (Lo_Orig) = N_Attribute_Reference
4450 and then Attribute_Name (Lo_Orig) = Name_First
4451 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
4452 and then Entity (Prefix (Lo_Orig)) = Ltyp
4453 and then Nkind (Hi_Orig) = N_Attribute_Reference
4454 and then Attribute_Name (Hi_Orig) = Name_Last
4455 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
4456 and then Entity (Prefix (Hi_Orig)) = Ltyp
4457 and then Comes_From_Source (N)
4458 and then VM_Target = No_VM
4460 Substitute_Valid_Check;
4464 -- If bounds of type are known at compile time, and the end points
4465 -- are known at compile time and identical, this is another case
4466 -- for substituting a valid test. We only do this for discrete
4467 -- types, since it won't arise in practice for float types.
4469 if Comes_From_Source (N)
4470 and then Is_Discrete_Type (Ltyp)
4471 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
4472 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
4473 and then Compile_Time_Known_Value (Lo)
4474 and then Compile_Time_Known_Value (Hi)
4475 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
4476 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
4478 -- Kill warnings in instances, since they may be cases where we
4479 -- have a test in the generic that makes sense with some types
4480 -- and not with other types.
4482 and then not In_Instance
4484 Substitute_Valid_Check;
4488 -- If we have an explicit range, do a bit of optimization based on
4489 -- range analysis (we may be able to kill one or both checks).
4491 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
4492 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
4494 -- If either check is known to fail, replace result by False since
4495 -- the other check does not matter. Preserve the static flag for
4496 -- legality checks, because we are constant-folding beyond RM 4.9.
4498 if Lcheck = LT or else Ucheck = GT then
4500 Error_Msg_N ("?range test optimized away", N);
4501 Error_Msg_N ("\?value is known to be out of range", N);
4504 Rewrite (N, New_Reference_To (Standard_False, Loc));
4505 Analyze_And_Resolve (N, Rtyp);
4506 Set_Is_Static_Expression (N, Static);
4510 -- If both checks are known to succeed, replace result by True,
4511 -- since we know we are in range.
4513 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4515 Error_Msg_N ("?range test optimized away", N);
4516 Error_Msg_N ("\?value is known to be in range", N);
4519 Rewrite (N, New_Reference_To (Standard_True, Loc));
4520 Analyze_And_Resolve (N, Rtyp);
4521 Set_Is_Static_Expression (N, Static);
4525 -- If lower bound check succeeds and upper bound check is not
4526 -- known to succeed or fail, then replace the range check with
4527 -- a comparison against the upper bound.
4529 elsif Lcheck in Compare_GE then
4530 if Warn2 and then not In_Instance then
4531 Error_Msg_N ("?lower bound test optimized away", Lo);
4532 Error_Msg_N ("\?value is known to be in range", Lo);
4538 Right_Opnd => High_Bound (Rop)));
4539 Analyze_And_Resolve (N, Rtyp);
4543 -- If upper bound check succeeds and lower bound check is not
4544 -- known to succeed or fail, then replace the range check with
4545 -- a comparison against the lower bound.
4547 elsif Ucheck in Compare_LE then
4548 if Warn2 and then not In_Instance then
4549 Error_Msg_N ("?upper bound test optimized away", Hi);
4550 Error_Msg_N ("\?value is known to be in range", Hi);
4556 Right_Opnd => Low_Bound (Rop)));
4557 Analyze_And_Resolve (N, Rtyp);
4562 -- We couldn't optimize away the range check, but there is one
4563 -- more issue. If we are checking constant conditionals, then we
4564 -- see if we can determine the outcome assuming everything is
4565 -- valid, and if so give an appropriate warning.
4567 if Warn1 and then not Assume_No_Invalid_Values then
4568 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
4569 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
4571 -- Result is out of range for valid value
4573 if Lcheck = LT or else Ucheck = GT then
4575 ("?value can only be in range if it is invalid", N);
4577 -- Result is in range for valid value
4579 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4581 ("?value can only be out of range if it is invalid", N);
4583 -- Lower bound check succeeds if value is valid
4585 elsif Warn2 and then Lcheck in Compare_GE then
4587 ("?lower bound check only fails if it is invalid", Lo);
4589 -- Upper bound check succeeds if value is valid
4591 elsif Warn2 and then Ucheck in Compare_LE then
4593 ("?upper bound check only fails for invalid values", Hi);
4598 -- For all other cases of an explicit range, nothing to be done
4602 -- Here right operand is a subtype mark
4606 Typ : Entity_Id := Etype (Rop);
4607 Is_Acc : constant Boolean := Is_Access_Type (Typ);
4608 Cond : Node_Id := Empty;
4610 Obj : Node_Id := Lop;
4611 SCIL_Node : Node_Id;
4614 Remove_Side_Effects (Obj);
4616 -- For tagged type, do tagged membership operation
4618 if Is_Tagged_Type (Typ) then
4620 -- No expansion will be performed when VM_Target, as the VM
4621 -- back-ends will handle the membership tests directly (tags
4622 -- are not explicitly represented in Java objects, so the
4623 -- normal tagged membership expansion is not what we want).
4625 if Tagged_Type_Expansion then
4626 Tagged_Membership (N, SCIL_Node, New_N);
4628 Analyze_And_Resolve (N, Rtyp);
4630 -- Update decoration of relocated node referenced by the
4633 if Generate_SCIL and then Present (SCIL_Node) then
4634 Set_SCIL_Node (N, SCIL_Node);
4640 -- If type is scalar type, rewrite as x in t'first .. t'last.
4641 -- This reason we do this is that the bounds may have the wrong
4642 -- type if they come from the original type definition. Also this
4643 -- way we get all the processing above for an explicit range.
4645 elsif Is_Scalar_Type (Typ) then
4649 Make_Attribute_Reference (Loc,
4650 Attribute_Name => Name_First,
4651 Prefix => New_Reference_To (Typ, Loc)),
4654 Make_Attribute_Reference (Loc,
4655 Attribute_Name => Name_Last,
4656 Prefix => New_Reference_To (Typ, Loc))));
4657 Analyze_And_Resolve (N, Rtyp);
4660 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4661 -- a membership test if the subtype mark denotes a constrained
4662 -- Unchecked_Union subtype and the expression lacks inferable
4665 elsif Is_Unchecked_Union (Base_Type (Typ))
4666 and then Is_Constrained (Typ)
4667 and then not Has_Inferable_Discriminants (Lop)
4670 Make_Raise_Program_Error (Loc,
4671 Reason => PE_Unchecked_Union_Restriction));
4673 -- Prevent Gigi from generating incorrect code by rewriting the
4676 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4680 -- Here we have a non-scalar type
4683 Typ := Designated_Type (Typ);
4686 if not Is_Constrained (Typ) then
4687 Rewrite (N, New_Reference_To (Standard_True, Loc));
4688 Analyze_And_Resolve (N, Rtyp);
4690 -- For the constrained array case, we have to check the subscripts
4691 -- for an exact match if the lengths are non-zero (the lengths
4692 -- must match in any case).
4694 elsif Is_Array_Type (Typ) then
4695 Check_Subscripts : declare
4696 function Build_Attribute_Reference
4699 Dim : Nat) return Node_Id;
4700 -- Build attribute reference E'Nam (Dim)
4702 -------------------------------
4703 -- Build_Attribute_Reference --
4704 -------------------------------
4706 function Build_Attribute_Reference
4709 Dim : Nat) return Node_Id
4713 Make_Attribute_Reference (Loc,
4715 Attribute_Name => Nam,
4716 Expressions => New_List (
4717 Make_Integer_Literal (Loc, Dim)));
4718 end Build_Attribute_Reference;
4720 -- Start of processing for Check_Subscripts
4723 for J in 1 .. Number_Dimensions (Typ) loop
4724 Evolve_And_Then (Cond,
4727 Build_Attribute_Reference
4728 (Duplicate_Subexpr_No_Checks (Obj),
4731 Build_Attribute_Reference
4732 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
4734 Evolve_And_Then (Cond,
4737 Build_Attribute_Reference
4738 (Duplicate_Subexpr_No_Checks (Obj),
4741 Build_Attribute_Reference
4742 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
4751 Right_Opnd => Make_Null (Loc)),
4752 Right_Opnd => Cond);
4756 Analyze_And_Resolve (N, Rtyp);
4757 end Check_Subscripts;
4759 -- These are the cases where constraint checks may be required,
4760 -- e.g. records with possible discriminants
4763 -- Expand the test into a series of discriminant comparisons.
4764 -- The expression that is built is the negation of the one that
4765 -- is used for checking discriminant constraints.
4767 Obj := Relocate_Node (Left_Opnd (N));
4769 if Has_Discriminants (Typ) then
4770 Cond := Make_Op_Not (Loc,
4771 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
4774 Cond := Make_Or_Else (Loc,
4778 Right_Opnd => Make_Null (Loc)),
4779 Right_Opnd => Cond);
4783 Cond := New_Occurrence_Of (Standard_True, Loc);
4787 Analyze_And_Resolve (N, Rtyp);
4793 --------------------------------
4794 -- Expand_N_Indexed_Component --
4795 --------------------------------
4797 procedure Expand_N_Indexed_Component (N : Node_Id) is
4798 Loc : constant Source_Ptr := Sloc (N);
4799 Typ : constant Entity_Id := Etype (N);
4800 P : constant Node_Id := Prefix (N);
4801 T : constant Entity_Id := Etype (P);
4804 -- A special optimization, if we have an indexed component that is
4805 -- selecting from a slice, then we can eliminate the slice, since, for
4806 -- example, x (i .. j)(k) is identical to x(k). The only difference is
4807 -- the range check required by the slice. The range check for the slice
4808 -- itself has already been generated. The range check for the
4809 -- subscripting operation is ensured by converting the subject to
4810 -- the subtype of the slice.
4812 -- This optimization not only generates better code, avoiding slice
4813 -- messing especially in the packed case, but more importantly bypasses
4814 -- some problems in handling this peculiar case, for example, the issue
4815 -- of dealing specially with object renamings.
4817 if Nkind (P) = N_Slice then
4819 Make_Indexed_Component (Loc,
4820 Prefix => Prefix (P),
4821 Expressions => New_List (
4823 (Etype (First_Index (Etype (P))),
4824 First (Expressions (N))))));
4825 Analyze_And_Resolve (N, Typ);
4829 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
4830 -- function, then additional actuals must be passed.
4832 if Ada_Version >= Ada_05
4833 and then Is_Build_In_Place_Function_Call (P)
4835 Make_Build_In_Place_Call_In_Anonymous_Context (P);
4838 -- If the prefix is an access type, then we unconditionally rewrite if
4839 -- as an explicit dereference. This simplifies processing for several
4840 -- cases, including packed array cases and certain cases in which checks
4841 -- must be generated. We used to try to do this only when it was
4842 -- necessary, but it cleans up the code to do it all the time.
4844 if Is_Access_Type (T) then
4845 Insert_Explicit_Dereference (P);
4846 Analyze_And_Resolve (P, Designated_Type (T));
4849 -- Generate index and validity checks
4851 Generate_Index_Checks (N);
4853 if Validity_Checks_On and then Validity_Check_Subscripts then
4854 Apply_Subscript_Validity_Checks (N);
4857 -- All done for the non-packed case
4859 if not Is_Packed (Etype (Prefix (N))) then
4863 -- For packed arrays that are not bit-packed (i.e. the case of an array
4864 -- with one or more index types with a non-contiguous enumeration type),
4865 -- we can always use the normal packed element get circuit.
4867 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
4868 Expand_Packed_Element_Reference (N);
4872 -- For a reference to a component of a bit packed array, we have to
4873 -- convert it to a reference to the corresponding Packed_Array_Type.
4874 -- We only want to do this for simple references, and not for:
4876 -- Left side of assignment, or prefix of left side of assignment, or
4877 -- prefix of the prefix, to handle packed arrays of packed arrays,
4878 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
4880 -- Renaming objects in renaming associations
4881 -- This case is handled when a use of the renamed variable occurs
4883 -- Actual parameters for a procedure call
4884 -- This case is handled in Exp_Ch6.Expand_Actuals
4886 -- The second expression in a 'Read attribute reference
4888 -- The prefix of an address or bit or size attribute reference
4890 -- The following circuit detects these exceptions
4893 Child : Node_Id := N;
4894 Parnt : Node_Id := Parent (N);
4898 if Nkind (Parnt) = N_Unchecked_Expression then
4901 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
4902 N_Procedure_Call_Statement)
4903 or else (Nkind (Parnt) = N_Parameter_Association
4905 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
4909 elsif Nkind (Parnt) = N_Attribute_Reference
4910 and then (Attribute_Name (Parnt) = Name_Address
4912 Attribute_Name (Parnt) = Name_Bit
4914 Attribute_Name (Parnt) = Name_Size)
4915 and then Prefix (Parnt) = Child
4919 elsif Nkind (Parnt) = N_Assignment_Statement
4920 and then Name (Parnt) = Child
4924 -- If the expression is an index of an indexed component, it must
4925 -- be expanded regardless of context.
4927 elsif Nkind (Parnt) = N_Indexed_Component
4928 and then Child /= Prefix (Parnt)
4930 Expand_Packed_Element_Reference (N);
4933 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
4934 and then Name (Parent (Parnt)) = Parnt
4938 elsif Nkind (Parnt) = N_Attribute_Reference
4939 and then Attribute_Name (Parnt) = Name_Read
4940 and then Next (First (Expressions (Parnt))) = Child
4944 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
4945 and then Prefix (Parnt) = Child
4950 Expand_Packed_Element_Reference (N);
4954 -- Keep looking up tree for unchecked expression, or if we are the
4955 -- prefix of a possible assignment left side.
4958 Parnt := Parent (Child);
4961 end Expand_N_Indexed_Component;
4963 ---------------------
4964 -- Expand_N_Not_In --
4965 ---------------------
4967 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
4968 -- can be done. This avoids needing to duplicate this expansion code.
4970 procedure Expand_N_Not_In (N : Node_Id) is
4971 Loc : constant Source_Ptr := Sloc (N);
4972 Typ : constant Entity_Id := Etype (N);
4973 Cfs : constant Boolean := Comes_From_Source (N);
4980 Left_Opnd => Left_Opnd (N),
4981 Right_Opnd => Right_Opnd (N))));
4983 -- If this is a set membership, preserve list of alternatives
4985 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
4987 -- We want this to appear as coming from source if original does (see
4988 -- transformations in Expand_N_In).
4990 Set_Comes_From_Source (N, Cfs);
4991 Set_Comes_From_Source (Right_Opnd (N), Cfs);
4993 -- Now analyze transformed node
4995 Analyze_And_Resolve (N, Typ);
4996 end Expand_N_Not_In;
5002 -- The only replacement required is for the case of a null of type that is
5003 -- an access to protected subprogram. We represent such access values as a
5004 -- record, and so we must replace the occurrence of null by the equivalent
5005 -- record (with a null address and a null pointer in it), so that the
5006 -- backend creates the proper value.
5008 procedure Expand_N_Null (N : Node_Id) is
5009 Loc : constant Source_Ptr := Sloc (N);
5010 Typ : constant Entity_Id := Etype (N);
5014 if Is_Access_Protected_Subprogram_Type (Typ) then
5016 Make_Aggregate (Loc,
5017 Expressions => New_List (
5018 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
5022 Analyze_And_Resolve (N, Equivalent_Type (Typ));
5024 -- For subsequent semantic analysis, the node must retain its type.
5025 -- Gigi in any case replaces this type by the corresponding record
5026 -- type before processing the node.
5032 when RE_Not_Available =>
5036 ---------------------
5037 -- Expand_N_Op_Abs --
5038 ---------------------
5040 procedure Expand_N_Op_Abs (N : Node_Id) is
5041 Loc : constant Source_Ptr := Sloc (N);
5042 Expr : constant Node_Id := Right_Opnd (N);
5045 Unary_Op_Validity_Checks (N);
5047 -- Deal with software overflow checking
5049 if not Backend_Overflow_Checks_On_Target
5050 and then Is_Signed_Integer_Type (Etype (N))
5051 and then Do_Overflow_Check (N)
5053 -- The only case to worry about is when the argument is equal to the
5054 -- largest negative number, so what we do is to insert the check:
5056 -- [constraint_error when Expr = typ'Base'First]
5058 -- with the usual Duplicate_Subexpr use coding for expr
5061 Make_Raise_Constraint_Error (Loc,
5064 Left_Opnd => Duplicate_Subexpr (Expr),
5066 Make_Attribute_Reference (Loc,
5068 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
5069 Attribute_Name => Name_First)),
5070 Reason => CE_Overflow_Check_Failed));
5073 -- Vax floating-point types case
5075 if Vax_Float (Etype (N)) then
5076 Expand_Vax_Arith (N);
5078 end Expand_N_Op_Abs;
5080 ---------------------
5081 -- Expand_N_Op_Add --
5082 ---------------------
5084 procedure Expand_N_Op_Add (N : Node_Id) is
5085 Typ : constant Entity_Id := Etype (N);
5088 Binary_Op_Validity_Checks (N);
5090 -- N + 0 = 0 + N = N for integer types
5092 if Is_Integer_Type (Typ) then
5093 if Compile_Time_Known_Value (Right_Opnd (N))
5094 and then Expr_Value (Right_Opnd (N)) = Uint_0
5096 Rewrite (N, Left_Opnd (N));
5099 elsif Compile_Time_Known_Value (Left_Opnd (N))
5100 and then Expr_Value (Left_Opnd (N)) = Uint_0
5102 Rewrite (N, Right_Opnd (N));
5107 -- Arithmetic overflow checks for signed integer/fixed point types
5109 if Is_Signed_Integer_Type (Typ)
5110 or else Is_Fixed_Point_Type (Typ)
5112 Apply_Arithmetic_Overflow_Check (N);
5115 -- Vax floating-point types case
5117 elsif Vax_Float (Typ) then
5118 Expand_Vax_Arith (N);
5120 end Expand_N_Op_Add;
5122 ---------------------
5123 -- Expand_N_Op_And --
5124 ---------------------
5126 procedure Expand_N_Op_And (N : Node_Id) is
5127 Typ : constant Entity_Id := Etype (N);
5130 Binary_Op_Validity_Checks (N);
5132 if Is_Array_Type (Etype (N)) then
5133 Expand_Boolean_Operator (N);
5135 elsif Is_Boolean_Type (Etype (N)) then
5137 -- Replace AND by AND THEN if Short_Circuit_And_Or active and the
5138 -- type is standard Boolean (do not mess with AND that uses a non-
5139 -- standard Boolean type, because something strange is going on).
5141 if Short_Circuit_And_Or and then Typ = Standard_Boolean then
5143 Make_And_Then (Sloc (N),
5144 Left_Opnd => Relocate_Node (Left_Opnd (N)),
5145 Right_Opnd => Relocate_Node (Right_Opnd (N))));
5146 Analyze_And_Resolve (N, Typ);
5148 -- Otherwise, adjust conditions
5151 Adjust_Condition (Left_Opnd (N));
5152 Adjust_Condition (Right_Opnd (N));
5153 Set_Etype (N, Standard_Boolean);
5154 Adjust_Result_Type (N, Typ);
5157 end Expand_N_Op_And;
5159 ------------------------
5160 -- Expand_N_Op_Concat --
5161 ------------------------
5163 procedure Expand_N_Op_Concat (N : Node_Id) is
5165 -- List of operands to be concatenated
5168 -- Node which is to be replaced by the result of concatenating the nodes
5169 -- in the list Opnds.
5172 -- Ensure validity of both operands
5174 Binary_Op_Validity_Checks (N);
5176 -- If we are the left operand of a concatenation higher up the tree,
5177 -- then do nothing for now, since we want to deal with a series of
5178 -- concatenations as a unit.
5180 if Nkind (Parent (N)) = N_Op_Concat
5181 and then N = Left_Opnd (Parent (N))
5186 -- We get here with a concatenation whose left operand may be a
5187 -- concatenation itself with a consistent type. We need to process
5188 -- these concatenation operands from left to right, which means
5189 -- from the deepest node in the tree to the highest node.
5192 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
5193 Cnode := Left_Opnd (Cnode);
5196 -- Now Cnode is the deepest concatenation, and its parents are the
5197 -- concatenation nodes above, so now we process bottom up, doing the
5198 -- operations. We gather a string that is as long as possible up to five
5201 -- The outer loop runs more than once if more than one concatenation
5202 -- type is involved.
5205 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
5206 Set_Parent (Opnds, N);
5208 -- The inner loop gathers concatenation operands
5210 Inner : while Cnode /= N
5211 and then Base_Type (Etype (Cnode)) =
5212 Base_Type (Etype (Parent (Cnode)))
5214 Cnode := Parent (Cnode);
5215 Append (Right_Opnd (Cnode), Opnds);
5218 Expand_Concatenate (Cnode, Opnds);
5220 exit Outer when Cnode = N;
5221 Cnode := Parent (Cnode);
5223 end Expand_N_Op_Concat;
5225 ------------------------
5226 -- Expand_N_Op_Divide --
5227 ------------------------
5229 procedure Expand_N_Op_Divide (N : Node_Id) is
5230 Loc : constant Source_Ptr := Sloc (N);
5231 Lopnd : constant Node_Id := Left_Opnd (N);
5232 Ropnd : constant Node_Id := Right_Opnd (N);
5233 Ltyp : constant Entity_Id := Etype (Lopnd);
5234 Rtyp : constant Entity_Id := Etype (Ropnd);
5235 Typ : Entity_Id := Etype (N);
5236 Rknow : constant Boolean := Is_Integer_Type (Typ)
5238 Compile_Time_Known_Value (Ropnd);
5242 Binary_Op_Validity_Checks (N);
5245 Rval := Expr_Value (Ropnd);
5248 -- N / 1 = N for integer types
5250 if Rknow and then Rval = Uint_1 then
5255 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
5256 -- Is_Power_Of_2_For_Shift is set means that we know that our left
5257 -- operand is an unsigned integer, as required for this to work.
5259 if Nkind (Ropnd) = N_Op_Expon
5260 and then Is_Power_Of_2_For_Shift (Ropnd)
5262 -- We cannot do this transformation in configurable run time mode if we
5263 -- have 64-bit integers and long shifts are not available.
5267 or else Support_Long_Shifts_On_Target)
5270 Make_Op_Shift_Right (Loc,
5273 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
5274 Analyze_And_Resolve (N, Typ);
5278 -- Do required fixup of universal fixed operation
5280 if Typ = Universal_Fixed then
5281 Fixup_Universal_Fixed_Operation (N);
5285 -- Divisions with fixed-point results
5287 if Is_Fixed_Point_Type (Typ) then
5289 -- No special processing if Treat_Fixed_As_Integer is set, since
5290 -- from a semantic point of view such operations are simply integer
5291 -- operations and will be treated that way.
5293 if not Treat_Fixed_As_Integer (N) then
5294 if Is_Integer_Type (Rtyp) then
5295 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
5297 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
5301 -- Other cases of division of fixed-point operands. Again we exclude the
5302 -- case where Treat_Fixed_As_Integer is set.
5304 elsif (Is_Fixed_Point_Type (Ltyp) or else
5305 Is_Fixed_Point_Type (Rtyp))
5306 and then not Treat_Fixed_As_Integer (N)
5308 if Is_Integer_Type (Typ) then
5309 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
5311 pragma Assert (Is_Floating_Point_Type (Typ));
5312 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
5315 -- Mixed-mode operations can appear in a non-static universal context,
5316 -- in which case the integer argument must be converted explicitly.
5318 elsif Typ = Universal_Real
5319 and then Is_Integer_Type (Rtyp)
5322 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
5324 Analyze_And_Resolve (Ropnd, Universal_Real);
5326 elsif Typ = Universal_Real
5327 and then Is_Integer_Type (Ltyp)
5330 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
5332 Analyze_And_Resolve (Lopnd, Universal_Real);
5334 -- Non-fixed point cases, do integer zero divide and overflow checks
5336 elsif Is_Integer_Type (Typ) then
5337 Apply_Divide_Check (N);
5339 -- Check for 64-bit division available, or long shifts if the divisor
5340 -- is a small power of 2 (since such divides will be converted into
5343 if Esize (Ltyp) > 32
5344 and then not Support_64_Bit_Divides_On_Target
5347 or else not Support_Long_Shifts_On_Target
5348 or else (Rval /= Uint_2 and then
5349 Rval /= Uint_4 and then
5350 Rval /= Uint_8 and then
5351 Rval /= Uint_16 and then
5352 Rval /= Uint_32 and then
5355 Error_Msg_CRT ("64-bit division", N);
5358 -- Deal with Vax_Float
5360 elsif Vax_Float (Typ) then
5361 Expand_Vax_Arith (N);
5364 end Expand_N_Op_Divide;
5366 --------------------
5367 -- Expand_N_Op_Eq --
5368 --------------------
5370 procedure Expand_N_Op_Eq (N : Node_Id) is
5371 Loc : constant Source_Ptr := Sloc (N);
5372 Typ : constant Entity_Id := Etype (N);
5373 Lhs : constant Node_Id := Left_Opnd (N);
5374 Rhs : constant Node_Id := Right_Opnd (N);
5375 Bodies : constant List_Id := New_List;
5376 A_Typ : constant Entity_Id := Etype (Lhs);
5378 Typl : Entity_Id := A_Typ;
5379 Op_Name : Entity_Id;
5382 procedure Build_Equality_Call (Eq : Entity_Id);
5383 -- If a constructed equality exists for the type or for its parent,
5384 -- build and analyze call, adding conversions if the operation is
5387 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
5388 -- Determines whether a type has a subcomponent of an unconstrained
5389 -- Unchecked_Union subtype. Typ is a record type.
5391 -------------------------
5392 -- Build_Equality_Call --
5393 -------------------------
5395 procedure Build_Equality_Call (Eq : Entity_Id) is
5396 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
5397 L_Exp : Node_Id := Relocate_Node (Lhs);
5398 R_Exp : Node_Id := Relocate_Node (Rhs);
5401 if Base_Type (Op_Type) /= Base_Type (A_Typ)
5402 and then not Is_Class_Wide_Type (A_Typ)
5404 L_Exp := OK_Convert_To (Op_Type, L_Exp);
5405 R_Exp := OK_Convert_To (Op_Type, R_Exp);
5408 -- If we have an Unchecked_Union, we need to add the inferred
5409 -- discriminant values as actuals in the function call. At this
5410 -- point, the expansion has determined that both operands have
5411 -- inferable discriminants.
5413 if Is_Unchecked_Union (Op_Type) then
5415 Lhs_Type : constant Node_Id := Etype (L_Exp);
5416 Rhs_Type : constant Node_Id := Etype (R_Exp);
5417 Lhs_Discr_Val : Node_Id;
5418 Rhs_Discr_Val : Node_Id;
5421 -- Per-object constrained selected components require special
5422 -- attention. If the enclosing scope of the component is an
5423 -- Unchecked_Union, we cannot reference its discriminants
5424 -- directly. This is why we use the two extra parameters of
5425 -- the equality function of the enclosing Unchecked_Union.
5427 -- type UU_Type (Discr : Integer := 0) is
5430 -- pragma Unchecked_Union (UU_Type);
5432 -- 1. Unchecked_Union enclosing record:
5434 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
5436 -- Comp : UU_Type (Discr);
5438 -- end Enclosing_UU_Type;
5439 -- pragma Unchecked_Union (Enclosing_UU_Type);
5441 -- Obj1 : Enclosing_UU_Type;
5442 -- Obj2 : Enclosing_UU_Type (1);
5444 -- [. . .] Obj1 = Obj2 [. . .]
5448 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
5450 -- A and B are the formal parameters of the equality function
5451 -- of Enclosing_UU_Type. The function always has two extra
5452 -- formals to capture the inferred discriminant values.
5454 -- 2. Non-Unchecked_Union enclosing record:
5457 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
5460 -- Comp : UU_Type (Discr);
5462 -- end Enclosing_Non_UU_Type;
5464 -- Obj1 : Enclosing_Non_UU_Type;
5465 -- Obj2 : Enclosing_Non_UU_Type (1);
5467 -- ... Obj1 = Obj2 ...
5471 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
5472 -- obj1.discr, obj2.discr)) then
5474 -- In this case we can directly reference the discriminants of
5475 -- the enclosing record.
5479 if Nkind (Lhs) = N_Selected_Component
5480 and then Has_Per_Object_Constraint
5481 (Entity (Selector_Name (Lhs)))
5483 -- Enclosing record is an Unchecked_Union, use formal A
5485 if Is_Unchecked_Union (Scope
5486 (Entity (Selector_Name (Lhs))))
5489 Make_Identifier (Loc,
5492 -- Enclosing record is of a non-Unchecked_Union type, it is
5493 -- possible to reference the discriminant.
5497 Make_Selected_Component (Loc,
5498 Prefix => Prefix (Lhs),
5501 (Get_Discriminant_Value
5502 (First_Discriminant (Lhs_Type),
5504 Stored_Constraint (Lhs_Type))));
5507 -- Comment needed here ???
5510 -- Infer the discriminant value
5514 (Get_Discriminant_Value
5515 (First_Discriminant (Lhs_Type),
5517 Stored_Constraint (Lhs_Type)));
5522 if Nkind (Rhs) = N_Selected_Component
5523 and then Has_Per_Object_Constraint
5524 (Entity (Selector_Name (Rhs)))
5526 if Is_Unchecked_Union
5527 (Scope (Entity (Selector_Name (Rhs))))
5530 Make_Identifier (Loc,
5535 Make_Selected_Component (Loc,
5536 Prefix => Prefix (Rhs),
5538 New_Copy (Get_Discriminant_Value (
5539 First_Discriminant (Rhs_Type),
5541 Stored_Constraint (Rhs_Type))));
5546 New_Copy (Get_Discriminant_Value (
5547 First_Discriminant (Rhs_Type),
5549 Stored_Constraint (Rhs_Type)));
5554 Make_Function_Call (Loc,
5555 Name => New_Reference_To (Eq, Loc),
5556 Parameter_Associations => New_List (
5563 -- Normal case, not an unchecked union
5567 Make_Function_Call (Loc,
5568 Name => New_Reference_To (Eq, Loc),
5569 Parameter_Associations => New_List (L_Exp, R_Exp)));
5572 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5573 end Build_Equality_Call;
5575 ------------------------------------
5576 -- Has_Unconstrained_UU_Component --
5577 ------------------------------------
5579 function Has_Unconstrained_UU_Component
5580 (Typ : Node_Id) return Boolean
5582 Tdef : constant Node_Id :=
5583 Type_Definition (Declaration_Node (Base_Type (Typ)));
5587 function Component_Is_Unconstrained_UU
5588 (Comp : Node_Id) return Boolean;
5589 -- Determines whether the subtype of the component is an
5590 -- unconstrained Unchecked_Union.
5592 function Variant_Is_Unconstrained_UU
5593 (Variant : Node_Id) return Boolean;
5594 -- Determines whether a component of the variant has an unconstrained
5595 -- Unchecked_Union subtype.
5597 -----------------------------------
5598 -- Component_Is_Unconstrained_UU --
5599 -----------------------------------
5601 function Component_Is_Unconstrained_UU
5602 (Comp : Node_Id) return Boolean
5605 if Nkind (Comp) /= N_Component_Declaration then
5610 Sindic : constant Node_Id :=
5611 Subtype_Indication (Component_Definition (Comp));
5614 -- Unconstrained nominal type. In the case of a constraint
5615 -- present, the node kind would have been N_Subtype_Indication.
5617 if Nkind (Sindic) = N_Identifier then
5618 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
5623 end Component_Is_Unconstrained_UU;
5625 ---------------------------------
5626 -- Variant_Is_Unconstrained_UU --
5627 ---------------------------------
5629 function Variant_Is_Unconstrained_UU
5630 (Variant : Node_Id) return Boolean
5632 Clist : constant Node_Id := Component_List (Variant);
5635 if Is_Empty_List (Component_Items (Clist)) then
5639 -- We only need to test one component
5642 Comp : Node_Id := First (Component_Items (Clist));
5645 while Present (Comp) loop
5646 if Component_Is_Unconstrained_UU (Comp) then
5654 -- None of the components withing the variant were of
5655 -- unconstrained Unchecked_Union type.
5658 end Variant_Is_Unconstrained_UU;
5660 -- Start of processing for Has_Unconstrained_UU_Component
5663 if Null_Present (Tdef) then
5667 Clist := Component_List (Tdef);
5668 Vpart := Variant_Part (Clist);
5670 -- Inspect available components
5672 if Present (Component_Items (Clist)) then
5674 Comp : Node_Id := First (Component_Items (Clist));
5677 while Present (Comp) loop
5679 -- One component is sufficient
5681 if Component_Is_Unconstrained_UU (Comp) then
5690 -- Inspect available components withing variants
5692 if Present (Vpart) then
5694 Variant : Node_Id := First (Variants (Vpart));
5697 while Present (Variant) loop
5699 -- One component within a variant is sufficient
5701 if Variant_Is_Unconstrained_UU (Variant) then
5710 -- Neither the available components, nor the components inside the
5711 -- variant parts were of an unconstrained Unchecked_Union subtype.
5714 end Has_Unconstrained_UU_Component;
5716 -- Start of processing for Expand_N_Op_Eq
5719 Binary_Op_Validity_Checks (N);
5721 if Ekind (Typl) = E_Private_Type then
5722 Typl := Underlying_Type (Typl);
5723 elsif Ekind (Typl) = E_Private_Subtype then
5724 Typl := Underlying_Type (Base_Type (Typl));
5729 -- It may happen in error situations that the underlying type is not
5730 -- set. The error will be detected later, here we just defend the
5737 Typl := Base_Type (Typl);
5739 -- Boolean types (requiring handling of non-standard case)
5741 if Is_Boolean_Type (Typl) then
5742 Adjust_Condition (Left_Opnd (N));
5743 Adjust_Condition (Right_Opnd (N));
5744 Set_Etype (N, Standard_Boolean);
5745 Adjust_Result_Type (N, Typ);
5749 elsif Is_Array_Type (Typl) then
5751 -- If we are doing full validity checking, and it is possible for the
5752 -- array elements to be invalid then expand out array comparisons to
5753 -- make sure that we check the array elements.
5755 if Validity_Check_Operands
5756 and then not Is_Known_Valid (Component_Type (Typl))
5759 Save_Force_Validity_Checks : constant Boolean :=
5760 Force_Validity_Checks;
5762 Force_Validity_Checks := True;
5764 Expand_Array_Equality
5766 Relocate_Node (Lhs),
5767 Relocate_Node (Rhs),
5770 Insert_Actions (N, Bodies);
5771 Analyze_And_Resolve (N, Standard_Boolean);
5772 Force_Validity_Checks := Save_Force_Validity_Checks;
5775 -- Packed case where both operands are known aligned
5777 elsif Is_Bit_Packed_Array (Typl)
5778 and then not Is_Possibly_Unaligned_Object (Lhs)
5779 and then not Is_Possibly_Unaligned_Object (Rhs)
5781 Expand_Packed_Eq (N);
5783 -- Where the component type is elementary we can use a block bit
5784 -- comparison (if supported on the target) exception in the case
5785 -- of floating-point (negative zero issues require element by
5786 -- element comparison), and atomic types (where we must be sure
5787 -- to load elements independently) and possibly unaligned arrays.
5789 elsif Is_Elementary_Type (Component_Type (Typl))
5790 and then not Is_Floating_Point_Type (Component_Type (Typl))
5791 and then not Is_Atomic (Component_Type (Typl))
5792 and then not Is_Possibly_Unaligned_Object (Lhs)
5793 and then not Is_Possibly_Unaligned_Object (Rhs)
5794 and then Support_Composite_Compare_On_Target
5798 -- For composite and floating-point cases, expand equality loop to
5799 -- make sure of using proper comparisons for tagged types, and
5800 -- correctly handling the floating-point case.
5804 Expand_Array_Equality
5806 Relocate_Node (Lhs),
5807 Relocate_Node (Rhs),
5810 Insert_Actions (N, Bodies, Suppress => All_Checks);
5811 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5816 elsif Is_Record_Type (Typl) then
5818 -- For tagged types, use the primitive "="
5820 if Is_Tagged_Type (Typl) then
5822 -- No need to do anything else compiling under restriction
5823 -- No_Dispatching_Calls. During the semantic analysis we
5824 -- already notified such violation.
5826 if Restriction_Active (No_Dispatching_Calls) then
5830 -- If this is derived from an untagged private type completed with
5831 -- a tagged type, it does not have a full view, so we use the
5832 -- primitive operations of the private type. This check should no
5833 -- longer be necessary when these types get their full views???
5835 if Is_Private_Type (A_Typ)
5836 and then not Is_Tagged_Type (A_Typ)
5837 and then Is_Derived_Type (A_Typ)
5838 and then No (Full_View (A_Typ))
5840 -- Search for equality operation, checking that the operands
5841 -- have the same type. Note that we must find a matching entry,
5842 -- or something is very wrong!
5844 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
5846 while Present (Prim) loop
5847 exit when Chars (Node (Prim)) = Name_Op_Eq
5848 and then Etype (First_Formal (Node (Prim))) =
5849 Etype (Next_Formal (First_Formal (Node (Prim))))
5851 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5856 pragma Assert (Present (Prim));
5857 Op_Name := Node (Prim);
5859 -- Find the type's predefined equality or an overriding
5860 -- user- defined equality. The reason for not simply calling
5861 -- Find_Prim_Op here is that there may be a user-defined
5862 -- overloaded equality op that precedes the equality that we want,
5863 -- so we have to explicitly search (e.g., there could be an
5864 -- equality with two different parameter types).
5867 if Is_Class_Wide_Type (Typl) then
5868 Typl := Root_Type (Typl);
5871 Prim := First_Elmt (Primitive_Operations (Typl));
5872 while Present (Prim) loop
5873 exit when Chars (Node (Prim)) = Name_Op_Eq
5874 and then Etype (First_Formal (Node (Prim))) =
5875 Etype (Next_Formal (First_Formal (Node (Prim))))
5877 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5882 pragma Assert (Present (Prim));
5883 Op_Name := Node (Prim);
5886 Build_Equality_Call (Op_Name);
5888 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
5889 -- predefined equality operator for a type which has a subcomponent
5890 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
5892 elsif Has_Unconstrained_UU_Component (Typl) then
5894 Make_Raise_Program_Error (Loc,
5895 Reason => PE_Unchecked_Union_Restriction));
5897 -- Prevent Gigi from generating incorrect code by rewriting the
5898 -- equality as a standard False.
5901 New_Occurrence_Of (Standard_False, Loc));
5903 elsif Is_Unchecked_Union (Typl) then
5905 -- If we can infer the discriminants of the operands, we make a
5906 -- call to the TSS equality function.
5908 if Has_Inferable_Discriminants (Lhs)
5910 Has_Inferable_Discriminants (Rhs)
5913 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5916 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5917 -- the predefined equality operator for an Unchecked_Union type
5918 -- if either of the operands lack inferable discriminants.
5921 Make_Raise_Program_Error (Loc,
5922 Reason => PE_Unchecked_Union_Restriction));
5924 -- Prevent Gigi from generating incorrect code by rewriting
5925 -- the equality as a standard False.
5928 New_Occurrence_Of (Standard_False, Loc));
5932 -- If a type support function is present (for complex cases), use it
5934 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
5936 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5938 -- Otherwise expand the component by component equality. Note that
5939 -- we never use block-bit comparisons for records, because of the
5940 -- problems with gaps. The backend will often be able to recombine
5941 -- the separate comparisons that we generate here.
5944 Remove_Side_Effects (Lhs);
5945 Remove_Side_Effects (Rhs);
5947 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
5949 Insert_Actions (N, Bodies, Suppress => All_Checks);
5950 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5954 -- Test if result is known at compile time
5956 Rewrite_Comparison (N);
5958 -- If we still have comparison for Vax_Float, process it
5960 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
5961 Expand_Vax_Comparison (N);
5966 -----------------------
5967 -- Expand_N_Op_Expon --
5968 -----------------------
5970 procedure Expand_N_Op_Expon (N : Node_Id) is
5971 Loc : constant Source_Ptr := Sloc (N);
5972 Typ : constant Entity_Id := Etype (N);
5973 Rtyp : constant Entity_Id := Root_Type (Typ);
5974 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
5975 Bastyp : constant Node_Id := Etype (Base);
5976 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
5977 Exptyp : constant Entity_Id := Etype (Exp);
5978 Ovflo : constant Boolean := Do_Overflow_Check (N);
5987 Binary_Op_Validity_Checks (N);
5989 -- If either operand is of a private type, then we have the use of an
5990 -- intrinsic operator, and we get rid of the privateness, by using root
5991 -- types of underlying types for the actual operation. Otherwise the
5992 -- private types will cause trouble if we expand multiplications or
5993 -- shifts etc. We also do this transformation if the result type is
5994 -- different from the base type.
5996 if Is_Private_Type (Etype (Base))
5998 Is_Private_Type (Typ)
6000 Is_Private_Type (Exptyp)
6002 Rtyp /= Root_Type (Bastyp)
6005 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
6006 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
6010 Unchecked_Convert_To (Typ,
6012 Left_Opnd => Unchecked_Convert_To (Bt, Base),
6013 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
6014 Analyze_And_Resolve (N, Typ);
6019 -- Test for case of known right argument
6021 if Compile_Time_Known_Value (Exp) then
6022 Expv := Expr_Value (Exp);
6024 -- We only fold small non-negative exponents. You might think we
6025 -- could fold small negative exponents for the real case, but we
6026 -- can't because we are required to raise Constraint_Error for
6027 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
6028 -- See ACVC test C4A012B.
6030 if Expv >= 0 and then Expv <= 4 then
6032 -- X ** 0 = 1 (or 1.0)
6036 -- Call Remove_Side_Effects to ensure that any side effects
6037 -- in the ignored left operand (in particular function calls
6038 -- to user defined functions) are properly executed.
6040 Remove_Side_Effects (Base);
6042 if Ekind (Typ) in Integer_Kind then
6043 Xnode := Make_Integer_Literal (Loc, Intval => 1);
6045 Xnode := Make_Real_Literal (Loc, Ureal_1);
6057 Make_Op_Multiply (Loc,
6058 Left_Opnd => Duplicate_Subexpr (Base),
6059 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
6061 -- X ** 3 = X * X * X
6065 Make_Op_Multiply (Loc,
6067 Make_Op_Multiply (Loc,
6068 Left_Opnd => Duplicate_Subexpr (Base),
6069 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
6070 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
6073 -- En : constant base'type := base * base;
6078 Temp := Make_Temporary (Loc, 'E', Base);
6080 Insert_Actions (N, New_List (
6081 Make_Object_Declaration (Loc,
6082 Defining_Identifier => Temp,
6083 Constant_Present => True,
6084 Object_Definition => New_Reference_To (Typ, Loc),
6086 Make_Op_Multiply (Loc,
6087 Left_Opnd => Duplicate_Subexpr (Base),
6088 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
6091 Make_Op_Multiply (Loc,
6092 Left_Opnd => New_Reference_To (Temp, Loc),
6093 Right_Opnd => New_Reference_To (Temp, Loc));
6097 Analyze_And_Resolve (N, Typ);
6102 -- Case of (2 ** expression) appearing as an argument of an integer
6103 -- multiplication, or as the right argument of a division of a non-
6104 -- negative integer. In such cases we leave the node untouched, setting
6105 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
6106 -- of the higher level node converts it into a shift.
6108 -- Another case is 2 ** N in any other context. We simply convert
6109 -- this to 1 * 2 ** N, and then the above transformation applies.
6111 -- Note: this transformation is not applicable for a modular type with
6112 -- a non-binary modulus in the multiplication case, since we get a wrong
6113 -- result if the shift causes an overflow before the modular reduction.
6115 if Nkind (Base) = N_Integer_Literal
6116 and then Intval (Base) = 2
6117 and then Is_Integer_Type (Root_Type (Exptyp))
6118 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
6119 and then Is_Unsigned_Type (Exptyp)
6122 -- First the multiply and divide cases
6124 if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then
6126 P : constant Node_Id := Parent (N);
6127 L : constant Node_Id := Left_Opnd (P);
6128 R : constant Node_Id := Right_Opnd (P);
6131 if (Nkind (P) = N_Op_Multiply
6132 and then not Non_Binary_Modulus (Typ)
6134 ((Is_Integer_Type (Etype (L)) and then R = N)
6136 (Is_Integer_Type (Etype (R)) and then L = N))
6137 and then not Do_Overflow_Check (P))
6139 (Nkind (P) = N_Op_Divide
6140 and then Is_Integer_Type (Etype (L))
6141 and then Is_Unsigned_Type (Etype (L))
6143 and then not Do_Overflow_Check (P))
6145 Set_Is_Power_Of_2_For_Shift (N);
6150 -- Now the other cases
6152 elsif not Non_Binary_Modulus (Typ) then
6154 Make_Op_Multiply (Loc,
6155 Left_Opnd => Make_Integer_Literal (Loc, 1),
6156 Right_Opnd => Relocate_Node (N)));
6157 Analyze_And_Resolve (N, Typ);
6162 -- Fall through if exponentiation must be done using a runtime routine
6164 -- First deal with modular case
6166 if Is_Modular_Integer_Type (Rtyp) then
6168 -- Non-binary case, we call the special exponentiation routine for
6169 -- the non-binary case, converting the argument to Long_Long_Integer
6170 -- and passing the modulus value. Then the result is converted back
6171 -- to the base type.
6173 if Non_Binary_Modulus (Rtyp) then
6176 Make_Function_Call (Loc,
6177 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
6178 Parameter_Associations => New_List (
6179 Convert_To (Standard_Integer, Base),
6180 Make_Integer_Literal (Loc, Modulus (Rtyp)),
6183 -- Binary case, in this case, we call one of two routines, either the
6184 -- unsigned integer case, or the unsigned long long integer case,
6185 -- with a final "and" operation to do the required mod.
6188 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
6189 Ent := RTE (RE_Exp_Unsigned);
6191 Ent := RTE (RE_Exp_Long_Long_Unsigned);
6198 Make_Function_Call (Loc,
6199 Name => New_Reference_To (Ent, Loc),
6200 Parameter_Associations => New_List (
6201 Convert_To (Etype (First_Formal (Ent)), Base),
6204 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
6208 -- Common exit point for modular type case
6210 Analyze_And_Resolve (N, Typ);
6213 -- Signed integer cases, done using either Integer or Long_Long_Integer.
6214 -- It is not worth having routines for Short_[Short_]Integer, since for
6215 -- most machines it would not help, and it would generate more code that
6216 -- might need certification when a certified run time is required.
6218 -- In the integer cases, we have two routines, one for when overflow
6219 -- checks are required, and one when they are not required, since there
6220 -- is a real gain in omitting checks on many machines.
6222 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
6223 or else (Rtyp = Base_Type (Standard_Long_Integer)
6225 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
6226 or else (Rtyp = Universal_Integer)
6228 Etyp := Standard_Long_Long_Integer;
6231 Rent := RE_Exp_Long_Long_Integer;
6233 Rent := RE_Exn_Long_Long_Integer;
6236 elsif Is_Signed_Integer_Type (Rtyp) then
6237 Etyp := Standard_Integer;
6240 Rent := RE_Exp_Integer;
6242 Rent := RE_Exn_Integer;
6245 -- Floating-point cases, always done using Long_Long_Float. We do not
6246 -- need separate routines for the overflow case here, since in the case
6247 -- of floating-point, we generate infinities anyway as a rule (either
6248 -- that or we automatically trap overflow), and if there is an infinity
6249 -- generated and a range check is required, the check will fail anyway.
6252 pragma Assert (Is_Floating_Point_Type (Rtyp));
6253 Etyp := Standard_Long_Long_Float;
6254 Rent := RE_Exn_Long_Long_Float;
6257 -- Common processing for integer cases and floating-point cases.
6258 -- If we are in the right type, we can call runtime routine directly
6261 and then Rtyp /= Universal_Integer
6262 and then Rtyp /= Universal_Real
6265 Make_Function_Call (Loc,
6266 Name => New_Reference_To (RTE (Rent), Loc),
6267 Parameter_Associations => New_List (Base, Exp)));
6269 -- Otherwise we have to introduce conversions (conversions are also
6270 -- required in the universal cases, since the runtime routine is
6271 -- typed using one of the standard types).
6276 Make_Function_Call (Loc,
6277 Name => New_Reference_To (RTE (Rent), Loc),
6278 Parameter_Associations => New_List (
6279 Convert_To (Etyp, Base),
6283 Analyze_And_Resolve (N, Typ);
6287 when RE_Not_Available =>
6289 end Expand_N_Op_Expon;
6291 --------------------
6292 -- Expand_N_Op_Ge --
6293 --------------------
6295 procedure Expand_N_Op_Ge (N : Node_Id) is
6296 Typ : constant Entity_Id := Etype (N);
6297 Op1 : constant Node_Id := Left_Opnd (N);
6298 Op2 : constant Node_Id := Right_Opnd (N);
6299 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6302 Binary_Op_Validity_Checks (N);
6304 if Is_Array_Type (Typ1) then
6305 Expand_Array_Comparison (N);
6309 if Is_Boolean_Type (Typ1) then
6310 Adjust_Condition (Op1);
6311 Adjust_Condition (Op2);
6312 Set_Etype (N, Standard_Boolean);
6313 Adjust_Result_Type (N, Typ);
6316 Rewrite_Comparison (N);
6318 -- If we still have comparison, and Vax_Float type, process it
6320 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6321 Expand_Vax_Comparison (N);
6326 --------------------
6327 -- Expand_N_Op_Gt --
6328 --------------------
6330 procedure Expand_N_Op_Gt (N : Node_Id) is
6331 Typ : constant Entity_Id := Etype (N);
6332 Op1 : constant Node_Id := Left_Opnd (N);
6333 Op2 : constant Node_Id := Right_Opnd (N);
6334 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6337 Binary_Op_Validity_Checks (N);
6339 if Is_Array_Type (Typ1) then
6340 Expand_Array_Comparison (N);
6344 if Is_Boolean_Type (Typ1) then
6345 Adjust_Condition (Op1);
6346 Adjust_Condition (Op2);
6347 Set_Etype (N, Standard_Boolean);
6348 Adjust_Result_Type (N, Typ);
6351 Rewrite_Comparison (N);
6353 -- If we still have comparison, and Vax_Float type, process it
6355 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6356 Expand_Vax_Comparison (N);
6361 --------------------
6362 -- Expand_N_Op_Le --
6363 --------------------
6365 procedure Expand_N_Op_Le (N : Node_Id) is
6366 Typ : constant Entity_Id := Etype (N);
6367 Op1 : constant Node_Id := Left_Opnd (N);
6368 Op2 : constant Node_Id := Right_Opnd (N);
6369 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6372 Binary_Op_Validity_Checks (N);
6374 if Is_Array_Type (Typ1) then
6375 Expand_Array_Comparison (N);
6379 if Is_Boolean_Type (Typ1) then
6380 Adjust_Condition (Op1);
6381 Adjust_Condition (Op2);
6382 Set_Etype (N, Standard_Boolean);
6383 Adjust_Result_Type (N, Typ);
6386 Rewrite_Comparison (N);
6388 -- If we still have comparison, and Vax_Float type, process it
6390 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6391 Expand_Vax_Comparison (N);
6396 --------------------
6397 -- Expand_N_Op_Lt --
6398 --------------------
6400 procedure Expand_N_Op_Lt (N : Node_Id) is
6401 Typ : constant Entity_Id := Etype (N);
6402 Op1 : constant Node_Id := Left_Opnd (N);
6403 Op2 : constant Node_Id := Right_Opnd (N);
6404 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6407 Binary_Op_Validity_Checks (N);
6409 if Is_Array_Type (Typ1) then
6410 Expand_Array_Comparison (N);
6414 if Is_Boolean_Type (Typ1) then
6415 Adjust_Condition (Op1);
6416 Adjust_Condition (Op2);
6417 Set_Etype (N, Standard_Boolean);
6418 Adjust_Result_Type (N, Typ);
6421 Rewrite_Comparison (N);
6423 -- If we still have comparison, and Vax_Float type, process it
6425 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6426 Expand_Vax_Comparison (N);
6431 -----------------------
6432 -- Expand_N_Op_Minus --
6433 -----------------------
6435 procedure Expand_N_Op_Minus (N : Node_Id) is
6436 Loc : constant Source_Ptr := Sloc (N);
6437 Typ : constant Entity_Id := Etype (N);
6440 Unary_Op_Validity_Checks (N);
6442 if not Backend_Overflow_Checks_On_Target
6443 and then Is_Signed_Integer_Type (Etype (N))
6444 and then Do_Overflow_Check (N)
6446 -- Software overflow checking expands -expr into (0 - expr)
6449 Make_Op_Subtract (Loc,
6450 Left_Opnd => Make_Integer_Literal (Loc, 0),
6451 Right_Opnd => Right_Opnd (N)));
6453 Analyze_And_Resolve (N, Typ);
6455 -- Vax floating-point types case
6457 elsif Vax_Float (Etype (N)) then
6458 Expand_Vax_Arith (N);
6460 end Expand_N_Op_Minus;
6462 ---------------------
6463 -- Expand_N_Op_Mod --
6464 ---------------------
6466 procedure Expand_N_Op_Mod (N : Node_Id) is
6467 Loc : constant Source_Ptr := Sloc (N);
6468 Typ : constant Entity_Id := Etype (N);
6469 Left : constant Node_Id := Left_Opnd (N);
6470 Right : constant Node_Id := Right_Opnd (N);
6471 DOC : constant Boolean := Do_Overflow_Check (N);
6472 DDC : constant Boolean := Do_Division_Check (N);
6482 pragma Warnings (Off, Lhi);
6485 Binary_Op_Validity_Checks (N);
6487 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
6488 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
6490 -- Convert mod to rem if operands are known non-negative. We do this
6491 -- since it is quite likely that this will improve the quality of code,
6492 -- (the operation now corresponds to the hardware remainder), and it
6493 -- does not seem likely that it could be harmful.
6495 if LOK and then Llo >= 0
6497 ROK and then Rlo >= 0
6500 Make_Op_Rem (Sloc (N),
6501 Left_Opnd => Left_Opnd (N),
6502 Right_Opnd => Right_Opnd (N)));
6504 -- Instead of reanalyzing the node we do the analysis manually. This
6505 -- avoids anomalies when the replacement is done in an instance and
6506 -- is epsilon more efficient.
6508 Set_Entity (N, Standard_Entity (S_Op_Rem));
6510 Set_Do_Overflow_Check (N, DOC);
6511 Set_Do_Division_Check (N, DDC);
6512 Expand_N_Op_Rem (N);
6515 -- Otherwise, normal mod processing
6518 if Is_Integer_Type (Etype (N)) then
6519 Apply_Divide_Check (N);
6522 -- Apply optimization x mod 1 = 0. We don't really need that with
6523 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
6524 -- certainly harmless.
6526 if Is_Integer_Type (Etype (N))
6527 and then Compile_Time_Known_Value (Right)
6528 and then Expr_Value (Right) = Uint_1
6530 -- Call Remove_Side_Effects to ensure that any side effects in
6531 -- the ignored left operand (in particular function calls to
6532 -- user defined functions) are properly executed.
6534 Remove_Side_Effects (Left);
6536 Rewrite (N, Make_Integer_Literal (Loc, 0));
6537 Analyze_And_Resolve (N, Typ);
6541 -- Deal with annoying case of largest negative number remainder
6542 -- minus one. Gigi does not handle this case correctly, because
6543 -- it generates a divide instruction which may trap in this case.
6545 -- In fact the check is quite easy, if the right operand is -1, then
6546 -- the mod value is always 0, and we can just ignore the left operand
6547 -- completely in this case.
6549 -- The operand type may be private (e.g. in the expansion of an
6550 -- intrinsic operation) so we must use the underlying type to get the
6551 -- bounds, and convert the literals explicitly.
6555 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
6557 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
6559 ((not LOK) or else (Llo = LLB))
6562 Make_Conditional_Expression (Loc,
6563 Expressions => New_List (
6565 Left_Opnd => Duplicate_Subexpr (Right),
6567 Unchecked_Convert_To (Typ,
6568 Make_Integer_Literal (Loc, -1))),
6569 Unchecked_Convert_To (Typ,
6570 Make_Integer_Literal (Loc, Uint_0)),
6571 Relocate_Node (N))));
6573 Set_Analyzed (Next (Next (First (Expressions (N)))));
6574 Analyze_And_Resolve (N, Typ);
6577 end Expand_N_Op_Mod;
6579 --------------------------
6580 -- Expand_N_Op_Multiply --
6581 --------------------------
6583 procedure Expand_N_Op_Multiply (N : Node_Id) is
6584 Loc : constant Source_Ptr := Sloc (N);
6585 Lop : constant Node_Id := Left_Opnd (N);
6586 Rop : constant Node_Id := Right_Opnd (N);
6588 Lp2 : constant Boolean :=
6589 Nkind (Lop) = N_Op_Expon
6590 and then Is_Power_Of_2_For_Shift (Lop);
6592 Rp2 : constant Boolean :=
6593 Nkind (Rop) = N_Op_Expon
6594 and then Is_Power_Of_2_For_Shift (Rop);
6596 Ltyp : constant Entity_Id := Etype (Lop);
6597 Rtyp : constant Entity_Id := Etype (Rop);
6598 Typ : Entity_Id := Etype (N);
6601 Binary_Op_Validity_Checks (N);
6603 -- Special optimizations for integer types
6605 if Is_Integer_Type (Typ) then
6607 -- N * 0 = 0 for integer types
6609 if Compile_Time_Known_Value (Rop)
6610 and then Expr_Value (Rop) = Uint_0
6612 -- Call Remove_Side_Effects to ensure that any side effects in
6613 -- the ignored left operand (in particular function calls to
6614 -- user defined functions) are properly executed.
6616 Remove_Side_Effects (Lop);
6618 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6619 Analyze_And_Resolve (N, Typ);
6623 -- Similar handling for 0 * N = 0
6625 if Compile_Time_Known_Value (Lop)
6626 and then Expr_Value (Lop) = Uint_0
6628 Remove_Side_Effects (Rop);
6629 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6630 Analyze_And_Resolve (N, Typ);
6634 -- N * 1 = 1 * N = N for integer types
6636 -- This optimisation is not done if we are going to
6637 -- rewrite the product 1 * 2 ** N to a shift.
6639 if Compile_Time_Known_Value (Rop)
6640 and then Expr_Value (Rop) = Uint_1
6646 elsif Compile_Time_Known_Value (Lop)
6647 and then Expr_Value (Lop) = Uint_1
6655 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
6656 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6657 -- operand is an integer, as required for this to work.
6662 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
6666 Left_Opnd => Make_Integer_Literal (Loc, 2),
6669 Left_Opnd => Right_Opnd (Lop),
6670 Right_Opnd => Right_Opnd (Rop))));
6671 Analyze_And_Resolve (N, Typ);
6676 Make_Op_Shift_Left (Loc,
6679 Convert_To (Standard_Natural, Right_Opnd (Rop))));
6680 Analyze_And_Resolve (N, Typ);
6684 -- Same processing for the operands the other way round
6688 Make_Op_Shift_Left (Loc,
6691 Convert_To (Standard_Natural, Right_Opnd (Lop))));
6692 Analyze_And_Resolve (N, Typ);
6696 -- Do required fixup of universal fixed operation
6698 if Typ = Universal_Fixed then
6699 Fixup_Universal_Fixed_Operation (N);
6703 -- Multiplications with fixed-point results
6705 if Is_Fixed_Point_Type (Typ) then
6707 -- No special processing if Treat_Fixed_As_Integer is set, since from
6708 -- a semantic point of view such operations are simply integer
6709 -- operations and will be treated that way.
6711 if not Treat_Fixed_As_Integer (N) then
6713 -- Case of fixed * integer => fixed
6715 if Is_Integer_Type (Rtyp) then
6716 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
6718 -- Case of integer * fixed => fixed
6720 elsif Is_Integer_Type (Ltyp) then
6721 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
6723 -- Case of fixed * fixed => fixed
6726 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
6730 -- Other cases of multiplication of fixed-point operands. Again we
6731 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
6733 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6734 and then not Treat_Fixed_As_Integer (N)
6736 if Is_Integer_Type (Typ) then
6737 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
6739 pragma Assert (Is_Floating_Point_Type (Typ));
6740 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
6743 -- Mixed-mode operations can appear in a non-static universal context,
6744 -- in which case the integer argument must be converted explicitly.
6746 elsif Typ = Universal_Real
6747 and then Is_Integer_Type (Rtyp)
6749 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
6751 Analyze_And_Resolve (Rop, Universal_Real);
6753 elsif Typ = Universal_Real
6754 and then Is_Integer_Type (Ltyp)
6756 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
6758 Analyze_And_Resolve (Lop, Universal_Real);
6760 -- Non-fixed point cases, check software overflow checking required
6762 elsif Is_Signed_Integer_Type (Etype (N)) then
6763 Apply_Arithmetic_Overflow_Check (N);
6765 -- Deal with VAX float case
6767 elsif Vax_Float (Typ) then
6768 Expand_Vax_Arith (N);
6771 end Expand_N_Op_Multiply;
6773 --------------------
6774 -- Expand_N_Op_Ne --
6775 --------------------
6777 procedure Expand_N_Op_Ne (N : Node_Id) is
6778 Typ : constant Entity_Id := Etype (Left_Opnd (N));
6781 -- Case of elementary type with standard operator
6783 if Is_Elementary_Type (Typ)
6784 and then Sloc (Entity (N)) = Standard_Location
6786 Binary_Op_Validity_Checks (N);
6788 -- Boolean types (requiring handling of non-standard case)
6790 if Is_Boolean_Type (Typ) then
6791 Adjust_Condition (Left_Opnd (N));
6792 Adjust_Condition (Right_Opnd (N));
6793 Set_Etype (N, Standard_Boolean);
6794 Adjust_Result_Type (N, Typ);
6797 Rewrite_Comparison (N);
6799 -- If we still have comparison for Vax_Float, process it
6801 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
6802 Expand_Vax_Comparison (N);
6806 -- For all cases other than elementary types, we rewrite node as the
6807 -- negation of an equality operation, and reanalyze. The equality to be
6808 -- used is defined in the same scope and has the same signature. This
6809 -- signature must be set explicitly since in an instance it may not have
6810 -- the same visibility as in the generic unit. This avoids duplicating
6811 -- or factoring the complex code for record/array equality tests etc.
6815 Loc : constant Source_Ptr := Sloc (N);
6817 Ne : constant Entity_Id := Entity (N);
6820 Binary_Op_Validity_Checks (N);
6826 Left_Opnd => Left_Opnd (N),
6827 Right_Opnd => Right_Opnd (N)));
6828 Set_Paren_Count (Right_Opnd (Neg), 1);
6830 if Scope (Ne) /= Standard_Standard then
6831 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
6834 -- For navigation purposes, the inequality is treated as an
6835 -- implicit reference to the corresponding equality. Preserve the
6836 -- Comes_From_ source flag so that the proper Xref entry is
6839 Preserve_Comes_From_Source (Neg, N);
6840 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
6842 Analyze_And_Resolve (N, Standard_Boolean);
6847 ---------------------
6848 -- Expand_N_Op_Not --
6849 ---------------------
6851 -- If the argument is other than a Boolean array type, there is no special
6852 -- expansion required, except for VMS operations on signed integers.
6854 -- For the packed case, we call the special routine in Exp_Pakd, except
6855 -- that if the component size is greater than one, we use the standard
6856 -- routine generating a gruesome loop (it is so peculiar to have packed
6857 -- arrays with non-standard Boolean representations anyway, so it does not
6858 -- matter that we do not handle this case efficiently).
6860 -- For the unpacked case (and for the special packed case where we have non
6861 -- standard Booleans, as discussed above), we generate and insert into the
6862 -- tree the following function definition:
6864 -- function Nnnn (A : arr) is
6867 -- for J in a'range loop
6868 -- B (J) := not A (J);
6873 -- Here arr is the actual subtype of the parameter (and hence always
6874 -- constrained). Then we replace the not with a call to this function.
6876 procedure Expand_N_Op_Not (N : Node_Id) is
6877 Loc : constant Source_Ptr := Sloc (N);
6878 Typ : constant Entity_Id := Etype (N);
6887 Func_Name : Entity_Id;
6888 Loop_Statement : Node_Id;
6891 Unary_Op_Validity_Checks (N);
6893 -- For boolean operand, deal with non-standard booleans
6895 if Is_Boolean_Type (Typ) then
6896 Adjust_Condition (Right_Opnd (N));
6897 Set_Etype (N, Standard_Boolean);
6898 Adjust_Result_Type (N, Typ);
6902 -- For the VMS "not" on signed integer types, use conversion to and
6903 -- from a predefined modular type.
6905 if Is_VMS_Operator (Entity (N)) then
6911 -- If this is a derived type, retrieve original VMS type so that
6912 -- the proper sized type is used for intermediate values.
6914 if Is_Derived_Type (Typ) then
6915 Rtyp := First_Subtype (Etype (Typ));
6920 -- The proper unsigned type must have a size compatible with the
6921 -- operand, to prevent misalignment.
6923 if RM_Size (Rtyp) <= 8 then
6924 Utyp := RTE (RE_Unsigned_8);
6926 elsif RM_Size (Rtyp) <= 16 then
6927 Utyp := RTE (RE_Unsigned_16);
6929 elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then
6930 Utyp := RTE (RE_Unsigned_32);
6933 Utyp := RTE (RE_Long_Long_Unsigned);
6937 Unchecked_Convert_To (Typ,
6939 Unchecked_Convert_To (Utyp, Right_Opnd (N)))));
6940 Analyze_And_Resolve (N, Typ);
6945 -- Only array types need any other processing
6947 if not Is_Array_Type (Typ) then
6951 -- Case of array operand. If bit packed with a component size of 1,
6952 -- handle it in Exp_Pakd if the operand is known to be aligned.
6954 if Is_Bit_Packed_Array (Typ)
6955 and then Component_Size (Typ) = 1
6956 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
6958 Expand_Packed_Not (N);
6962 -- Case of array operand which is not bit-packed. If the context is
6963 -- a safe assignment, call in-place operation, If context is a larger
6964 -- boolean expression in the context of a safe assignment, expansion is
6965 -- done by enclosing operation.
6967 Opnd := Relocate_Node (Right_Opnd (N));
6968 Convert_To_Actual_Subtype (Opnd);
6969 Arr := Etype (Opnd);
6970 Ensure_Defined (Arr, N);
6971 Silly_Boolean_Array_Not_Test (N, Arr);
6973 if Nkind (Parent (N)) = N_Assignment_Statement then
6974 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
6975 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
6978 -- Special case the negation of a binary operation
6980 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
6981 and then Safe_In_Place_Array_Op
6982 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
6984 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
6988 elsif Nkind (Parent (N)) in N_Binary_Op
6989 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
6992 Op1 : constant Node_Id := Left_Opnd (Parent (N));
6993 Op2 : constant Node_Id := Right_Opnd (Parent (N));
6994 Lhs : constant Node_Id := Name (Parent (Parent (N)));
6997 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
6999 -- (not A) op (not B) can be reduced to a single call
7001 if N = Op1 and then Nkind (Op2) = N_Op_Not then
7004 -- A xor (not B) can also be special-cased
7006 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
7013 A := Make_Defining_Identifier (Loc, Name_uA);
7014 B := Make_Defining_Identifier (Loc, Name_uB);
7015 J := Make_Defining_Identifier (Loc, Name_uJ);
7018 Make_Indexed_Component (Loc,
7019 Prefix => New_Reference_To (A, Loc),
7020 Expressions => New_List (New_Reference_To (J, Loc)));
7023 Make_Indexed_Component (Loc,
7024 Prefix => New_Reference_To (B, Loc),
7025 Expressions => New_List (New_Reference_To (J, Loc)));
7028 Make_Implicit_Loop_Statement (N,
7029 Identifier => Empty,
7032 Make_Iteration_Scheme (Loc,
7033 Loop_Parameter_Specification =>
7034 Make_Loop_Parameter_Specification (Loc,
7035 Defining_Identifier => J,
7036 Discrete_Subtype_Definition =>
7037 Make_Attribute_Reference (Loc,
7038 Prefix => Make_Identifier (Loc, Chars (A)),
7039 Attribute_Name => Name_Range))),
7041 Statements => New_List (
7042 Make_Assignment_Statement (Loc,
7044 Expression => Make_Op_Not (Loc, A_J))));
7046 Func_Name := Make_Temporary (Loc, 'N');
7047 Set_Is_Inlined (Func_Name);
7050 Make_Subprogram_Body (Loc,
7052 Make_Function_Specification (Loc,
7053 Defining_Unit_Name => Func_Name,
7054 Parameter_Specifications => New_List (
7055 Make_Parameter_Specification (Loc,
7056 Defining_Identifier => A,
7057 Parameter_Type => New_Reference_To (Typ, Loc))),
7058 Result_Definition => New_Reference_To (Typ, Loc)),
7060 Declarations => New_List (
7061 Make_Object_Declaration (Loc,
7062 Defining_Identifier => B,
7063 Object_Definition => New_Reference_To (Arr, Loc))),
7065 Handled_Statement_Sequence =>
7066 Make_Handled_Sequence_Of_Statements (Loc,
7067 Statements => New_List (
7069 Make_Simple_Return_Statement (Loc,
7070 Expression => Make_Identifier (Loc, Chars (B)))))));
7073 Make_Function_Call (Loc,
7074 Name => New_Reference_To (Func_Name, Loc),
7075 Parameter_Associations => New_List (Opnd)));
7077 Analyze_And_Resolve (N, Typ);
7078 end Expand_N_Op_Not;
7080 --------------------
7081 -- Expand_N_Op_Or --
7082 --------------------
7084 procedure Expand_N_Op_Or (N : Node_Id) is
7085 Typ : constant Entity_Id := Etype (N);
7088 Binary_Op_Validity_Checks (N);
7090 if Is_Array_Type (Etype (N)) then
7091 Expand_Boolean_Operator (N);
7093 elsif Is_Boolean_Type (Etype (N)) then
7095 -- Replace OR by OR ELSE if Short_Circuit_And_Or active and the type
7096 -- is standard Boolean (do not mess with AND that uses a non-standard
7097 -- Boolean type, because something strange is going on).
7099 if Short_Circuit_And_Or and then Typ = Standard_Boolean then
7101 Make_Or_Else (Sloc (N),
7102 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7103 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7104 Analyze_And_Resolve (N, Typ);
7106 -- Otherwise, adjust conditions
7109 Adjust_Condition (Left_Opnd (N));
7110 Adjust_Condition (Right_Opnd (N));
7111 Set_Etype (N, Standard_Boolean);
7112 Adjust_Result_Type (N, Typ);
7117 ----------------------
7118 -- Expand_N_Op_Plus --
7119 ----------------------
7121 procedure Expand_N_Op_Plus (N : Node_Id) is
7123 Unary_Op_Validity_Checks (N);
7124 end Expand_N_Op_Plus;
7126 ---------------------
7127 -- Expand_N_Op_Rem --
7128 ---------------------
7130 procedure Expand_N_Op_Rem (N : Node_Id) is
7131 Loc : constant Source_Ptr := Sloc (N);
7132 Typ : constant Entity_Id := Etype (N);
7134 Left : constant Node_Id := Left_Opnd (N);
7135 Right : constant Node_Id := Right_Opnd (N);
7143 -- Set if corresponding operand can be negative
7145 pragma Unreferenced (Hi);
7148 Binary_Op_Validity_Checks (N);
7150 if Is_Integer_Type (Etype (N)) then
7151 Apply_Divide_Check (N);
7154 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
7155 -- but it is useful with other back ends (e.g. AAMP), and is certainly
7158 if Is_Integer_Type (Etype (N))
7159 and then Compile_Time_Known_Value (Right)
7160 and then Expr_Value (Right) = Uint_1
7162 -- Call Remove_Side_Effects to ensure that any side effects in the
7163 -- ignored left operand (in particular function calls to user defined
7164 -- functions) are properly executed.
7166 Remove_Side_Effects (Left);
7168 Rewrite (N, Make_Integer_Literal (Loc, 0));
7169 Analyze_And_Resolve (N, Typ);
7173 -- Deal with annoying case of largest negative number remainder minus
7174 -- one. Gigi does not handle this case correctly, because it generates
7175 -- a divide instruction which may trap in this case.
7177 -- In fact the check is quite easy, if the right operand is -1, then
7178 -- the remainder is always 0, and we can just ignore the left operand
7179 -- completely in this case.
7181 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
7182 Lneg := (not OK) or else Lo < 0;
7184 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
7185 Rneg := (not OK) or else Lo < 0;
7187 -- We won't mess with trying to find out if the left operand can really
7188 -- be the largest negative number (that's a pain in the case of private
7189 -- types and this is really marginal). We will just assume that we need
7190 -- the test if the left operand can be negative at all.
7192 if Lneg and Rneg then
7194 Make_Conditional_Expression (Loc,
7195 Expressions => New_List (
7197 Left_Opnd => Duplicate_Subexpr (Right),
7199 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
7201 Unchecked_Convert_To (Typ,
7202 Make_Integer_Literal (Loc, Uint_0)),
7204 Relocate_Node (N))));
7206 Set_Analyzed (Next (Next (First (Expressions (N)))));
7207 Analyze_And_Resolve (N, Typ);
7209 end Expand_N_Op_Rem;
7211 -----------------------------
7212 -- Expand_N_Op_Rotate_Left --
7213 -----------------------------
7215 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
7217 Binary_Op_Validity_Checks (N);
7218 end Expand_N_Op_Rotate_Left;
7220 ------------------------------
7221 -- Expand_N_Op_Rotate_Right --
7222 ------------------------------
7224 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
7226 Binary_Op_Validity_Checks (N);
7227 end Expand_N_Op_Rotate_Right;
7229 ----------------------------
7230 -- Expand_N_Op_Shift_Left --
7231 ----------------------------
7233 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
7235 Binary_Op_Validity_Checks (N);
7236 end Expand_N_Op_Shift_Left;
7238 -----------------------------
7239 -- Expand_N_Op_Shift_Right --
7240 -----------------------------
7242 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
7244 Binary_Op_Validity_Checks (N);
7245 end Expand_N_Op_Shift_Right;
7247 ----------------------------------------
7248 -- Expand_N_Op_Shift_Right_Arithmetic --
7249 ----------------------------------------
7251 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
7253 Binary_Op_Validity_Checks (N);
7254 end Expand_N_Op_Shift_Right_Arithmetic;
7256 --------------------------
7257 -- Expand_N_Op_Subtract --
7258 --------------------------
7260 procedure Expand_N_Op_Subtract (N : Node_Id) is
7261 Typ : constant Entity_Id := Etype (N);
7264 Binary_Op_Validity_Checks (N);
7266 -- N - 0 = N for integer types
7268 if Is_Integer_Type (Typ)
7269 and then Compile_Time_Known_Value (Right_Opnd (N))
7270 and then Expr_Value (Right_Opnd (N)) = 0
7272 Rewrite (N, Left_Opnd (N));
7276 -- Arithmetic overflow checks for signed integer/fixed point types
7278 if Is_Signed_Integer_Type (Typ)
7280 Is_Fixed_Point_Type (Typ)
7282 Apply_Arithmetic_Overflow_Check (N);
7284 -- VAX floating-point types case
7286 elsif Vax_Float (Typ) then
7287 Expand_Vax_Arith (N);
7289 end Expand_N_Op_Subtract;
7291 ---------------------
7292 -- Expand_N_Op_Xor --
7293 ---------------------
7295 procedure Expand_N_Op_Xor (N : Node_Id) is
7296 Typ : constant Entity_Id := Etype (N);
7299 Binary_Op_Validity_Checks (N);
7301 if Is_Array_Type (Etype (N)) then
7302 Expand_Boolean_Operator (N);
7304 elsif Is_Boolean_Type (Etype (N)) then
7305 Adjust_Condition (Left_Opnd (N));
7306 Adjust_Condition (Right_Opnd (N));
7307 Set_Etype (N, Standard_Boolean);
7308 Adjust_Result_Type (N, Typ);
7310 end Expand_N_Op_Xor;
7312 ----------------------
7313 -- Expand_N_Or_Else --
7314 ----------------------
7316 procedure Expand_N_Or_Else (N : Node_Id)
7317 renames Expand_Short_Circuit_Operator;
7319 -----------------------------------
7320 -- Expand_N_Qualified_Expression --
7321 -----------------------------------
7323 procedure Expand_N_Qualified_Expression (N : Node_Id) is
7324 Operand : constant Node_Id := Expression (N);
7325 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
7328 -- Do validity check if validity checking operands
7330 if Validity_Checks_On
7331 and then Validity_Check_Operands
7333 Ensure_Valid (Operand);
7336 -- Apply possible constraint check
7338 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
7340 if Do_Range_Check (Operand) then
7341 Set_Do_Range_Check (Operand, False);
7342 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
7344 end Expand_N_Qualified_Expression;
7346 ---------------------------------
7347 -- Expand_N_Selected_Component --
7348 ---------------------------------
7350 -- If the selector is a discriminant of a concurrent object, rewrite the
7351 -- prefix to denote the corresponding record type.
7353 procedure Expand_N_Selected_Component (N : Node_Id) is
7354 Loc : constant Source_Ptr := Sloc (N);
7355 Par : constant Node_Id := Parent (N);
7356 P : constant Node_Id := Prefix (N);
7357 Ptyp : Entity_Id := Underlying_Type (Etype (P));
7362 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
7363 -- Gigi needs a temporary for prefixes that depend on a discriminant,
7364 -- unless the context of an assignment can provide size information.
7365 -- Don't we have a general routine that does this???
7367 -----------------------
7368 -- In_Left_Hand_Side --
7369 -----------------------
7371 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
7373 return (Nkind (Parent (Comp)) = N_Assignment_Statement
7374 and then Comp = Name (Parent (Comp)))
7375 or else (Present (Parent (Comp))
7376 and then Nkind (Parent (Comp)) in N_Subexpr
7377 and then In_Left_Hand_Side (Parent (Comp)));
7378 end In_Left_Hand_Side;
7380 -- Start of processing for Expand_N_Selected_Component
7383 -- Insert explicit dereference if required
7385 if Is_Access_Type (Ptyp) then
7386 Insert_Explicit_Dereference (P);
7387 Analyze_And_Resolve (P, Designated_Type (Ptyp));
7389 if Ekind (Etype (P)) = E_Private_Subtype
7390 and then Is_For_Access_Subtype (Etype (P))
7392 Set_Etype (P, Base_Type (Etype (P)));
7398 -- Deal with discriminant check required
7400 if Do_Discriminant_Check (N) then
7402 -- Present the discriminant checking function to the backend, so that
7403 -- it can inline the call to the function.
7406 (Discriminant_Checking_Func
7407 (Original_Record_Component (Entity (Selector_Name (N)))));
7409 -- Now reset the flag and generate the call
7411 Set_Do_Discriminant_Check (N, False);
7412 Generate_Discriminant_Check (N);
7415 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7416 -- function, then additional actuals must be passed.
7418 if Ada_Version >= Ada_05
7419 and then Is_Build_In_Place_Function_Call (P)
7421 Make_Build_In_Place_Call_In_Anonymous_Context (P);
7424 -- Gigi cannot handle unchecked conversions that are the prefix of a
7425 -- selected component with discriminants. This must be checked during
7426 -- expansion, because during analysis the type of the selector is not
7427 -- known at the point the prefix is analyzed. If the conversion is the
7428 -- target of an assignment, then we cannot force the evaluation.
7430 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
7431 and then Has_Discriminants (Etype (N))
7432 and then not In_Left_Hand_Side (N)
7434 Force_Evaluation (Prefix (N));
7437 -- Remaining processing applies only if selector is a discriminant
7439 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
7441 -- If the selector is a discriminant of a constrained record type,
7442 -- we may be able to rewrite the expression with the actual value
7443 -- of the discriminant, a useful optimization in some cases.
7445 if Is_Record_Type (Ptyp)
7446 and then Has_Discriminants (Ptyp)
7447 and then Is_Constrained (Ptyp)
7449 -- Do this optimization for discrete types only, and not for
7450 -- access types (access discriminants get us into trouble!)
7452 if not Is_Discrete_Type (Etype (N)) then
7455 -- Don't do this on the left hand of an assignment statement.
7456 -- Normally one would think that references like this would not
7457 -- occur, but they do in generated code, and mean that we really
7458 -- do want to assign the discriminant!
7460 elsif Nkind (Par) = N_Assignment_Statement
7461 and then Name (Par) = N
7465 -- Don't do this optimization for the prefix of an attribute or
7466 -- the name of an object renaming declaration since these are
7467 -- contexts where we do not want the value anyway.
7469 elsif (Nkind (Par) = N_Attribute_Reference
7470 and then Prefix (Par) = N)
7471 or else Is_Renamed_Object (N)
7475 -- If this is a discriminant of a component of a mutable record,
7476 -- or a renaming of such, no optimization is possible, and value
7477 -- must be retrieved anew. Note that in the previous case we may
7478 -- be dealing with a renaming declaration, while here we may have
7479 -- a use of a renaming.
7481 elsif Nkind (P) = N_Selected_Component
7482 and then Is_Record_Type (Etype (Prefix (P)))
7483 and then not Is_Constrained (Etype (Prefix (P)))
7487 -- Don't do this optimization if we are within the code for a
7488 -- discriminant check, since the whole point of such a check may
7489 -- be to verify the condition on which the code below depends!
7491 elsif Is_In_Discriminant_Check (N) then
7494 -- Green light to see if we can do the optimization. There is
7495 -- still one condition that inhibits the optimization below but
7496 -- now is the time to check the particular discriminant.
7499 -- Loop through discriminants to find the matching discriminant
7500 -- constraint to see if we can copy it.
7502 Disc := First_Discriminant (Ptyp);
7503 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
7504 Discr_Loop : while Present (Dcon) loop
7506 -- Check if this is the matching discriminant
7508 if Disc = Entity (Selector_Name (N)) then
7510 -- Here we have the matching discriminant. Check for
7511 -- the case of a discriminant of a component that is
7512 -- constrained by an outer discriminant, which cannot
7513 -- be optimized away.
7516 Denotes_Discriminant
7517 (Node (Dcon), Check_Concurrent => True)
7521 -- In the context of a case statement, the expression may
7522 -- have the base type of the discriminant, and we need to
7523 -- preserve the constraint to avoid spurious errors on
7526 elsif Nkind (Parent (N)) = N_Case_Statement
7527 and then Etype (Node (Dcon)) /= Etype (Disc)
7530 Make_Qualified_Expression (Loc,
7532 New_Occurrence_Of (Etype (Disc), Loc),
7534 New_Copy_Tree (Node (Dcon))));
7535 Analyze_And_Resolve (N, Etype (Disc));
7537 -- In case that comes out as a static expression,
7538 -- reset it (a selected component is never static).
7540 Set_Is_Static_Expression (N, False);
7543 -- Otherwise we can just copy the constraint, but the
7544 -- result is certainly not static! In some cases the
7545 -- discriminant constraint has been analyzed in the
7546 -- context of the original subtype indication, but for
7547 -- itypes the constraint might not have been analyzed
7548 -- yet, and this must be done now.
7551 Rewrite (N, New_Copy_Tree (Node (Dcon)));
7552 Analyze_And_Resolve (N);
7553 Set_Is_Static_Expression (N, False);
7559 Next_Discriminant (Disc);
7560 end loop Discr_Loop;
7562 -- Note: the above loop should always find a matching
7563 -- discriminant, but if it does not, we just missed an
7564 -- optimization due to some glitch (perhaps a previous error),
7570 -- The only remaining processing is in the case of a discriminant of
7571 -- a concurrent object, where we rewrite the prefix to denote the
7572 -- corresponding record type. If the type is derived and has renamed
7573 -- discriminants, use corresponding discriminant, which is the one
7574 -- that appears in the corresponding record.
7576 if not Is_Concurrent_Type (Ptyp) then
7580 Disc := Entity (Selector_Name (N));
7582 if Is_Derived_Type (Ptyp)
7583 and then Present (Corresponding_Discriminant (Disc))
7585 Disc := Corresponding_Discriminant (Disc);
7589 Make_Selected_Component (Loc,
7591 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
7593 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
7598 end Expand_N_Selected_Component;
7600 --------------------
7601 -- Expand_N_Slice --
7602 --------------------
7604 procedure Expand_N_Slice (N : Node_Id) is
7605 Loc : constant Source_Ptr := Sloc (N);
7606 Typ : constant Entity_Id := Etype (N);
7607 Pfx : constant Node_Id := Prefix (N);
7608 Ptp : Entity_Id := Etype (Pfx);
7610 function Is_Procedure_Actual (N : Node_Id) return Boolean;
7611 -- Check whether the argument is an actual for a procedure call, in
7612 -- which case the expansion of a bit-packed slice is deferred until the
7613 -- call itself is expanded. The reason this is required is that we might
7614 -- have an IN OUT or OUT parameter, and the copy out is essential, and
7615 -- that copy out would be missed if we created a temporary here in
7616 -- Expand_N_Slice. Note that we don't bother to test specifically for an
7617 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
7618 -- is harmless to defer expansion in the IN case, since the call
7619 -- processing will still generate the appropriate copy in operation,
7620 -- which will take care of the slice.
7622 procedure Make_Temporary_For_Slice;
7623 -- Create a named variable for the value of the slice, in cases where
7624 -- the back-end cannot handle it properly, e.g. when packed types or
7625 -- unaligned slices are involved.
7627 -------------------------
7628 -- Is_Procedure_Actual --
7629 -------------------------
7631 function Is_Procedure_Actual (N : Node_Id) return Boolean is
7632 Par : Node_Id := Parent (N);
7636 -- If our parent is a procedure call we can return
7638 if Nkind (Par) = N_Procedure_Call_Statement then
7641 -- If our parent is a type conversion, keep climbing the tree,
7642 -- since a type conversion can be a procedure actual. Also keep
7643 -- climbing if parameter association or a qualified expression,
7644 -- since these are additional cases that do can appear on
7645 -- procedure actuals.
7647 elsif Nkind_In (Par, N_Type_Conversion,
7648 N_Parameter_Association,
7649 N_Qualified_Expression)
7651 Par := Parent (Par);
7653 -- Any other case is not what we are looking for
7659 end Is_Procedure_Actual;
7661 ------------------------------
7662 -- Make_Temporary_For_Slice --
7663 ------------------------------
7665 procedure Make_Temporary_For_Slice is
7667 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
7671 Make_Object_Declaration (Loc,
7672 Defining_Identifier => Ent,
7673 Object_Definition => New_Occurrence_Of (Typ, Loc));
7675 Set_No_Initialization (Decl);
7677 Insert_Actions (N, New_List (
7679 Make_Assignment_Statement (Loc,
7680 Name => New_Occurrence_Of (Ent, Loc),
7681 Expression => Relocate_Node (N))));
7683 Rewrite (N, New_Occurrence_Of (Ent, Loc));
7684 Analyze_And_Resolve (N, Typ);
7685 end Make_Temporary_For_Slice;
7687 -- Start of processing for Expand_N_Slice
7690 -- Special handling for access types
7692 if Is_Access_Type (Ptp) then
7694 Ptp := Designated_Type (Ptp);
7697 Make_Explicit_Dereference (Sloc (N),
7698 Prefix => Relocate_Node (Pfx)));
7700 Analyze_And_Resolve (Pfx, Ptp);
7703 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7704 -- function, then additional actuals must be passed.
7706 if Ada_Version >= Ada_05
7707 and then Is_Build_In_Place_Function_Call (Pfx)
7709 Make_Build_In_Place_Call_In_Anonymous_Context (Pfx);
7712 -- The remaining case to be handled is packed slices. We can leave
7713 -- packed slices as they are in the following situations:
7715 -- 1. Right or left side of an assignment (we can handle this
7716 -- situation correctly in the assignment statement expansion).
7718 -- 2. Prefix of indexed component (the slide is optimized away in this
7719 -- case, see the start of Expand_N_Slice.)
7721 -- 3. Object renaming declaration, since we want the name of the
7722 -- slice, not the value.
7724 -- 4. Argument to procedure call, since copy-in/copy-out handling may
7725 -- be required, and this is handled in the expansion of call
7728 -- 5. Prefix of an address attribute (this is an error which is caught
7729 -- elsewhere, and the expansion would interfere with generating the
7732 if not Is_Packed (Typ) then
7734 -- Apply transformation for actuals of a function call, where
7735 -- Expand_Actuals is not used.
7737 if Nkind (Parent (N)) = N_Function_Call
7738 and then Is_Possibly_Unaligned_Slice (N)
7740 Make_Temporary_For_Slice;
7743 elsif Nkind (Parent (N)) = N_Assignment_Statement
7744 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
7745 and then Parent (N) = Name (Parent (Parent (N))))
7749 elsif Nkind (Parent (N)) = N_Indexed_Component
7750 or else Is_Renamed_Object (N)
7751 or else Is_Procedure_Actual (N)
7755 elsif Nkind (Parent (N)) = N_Attribute_Reference
7756 and then Attribute_Name (Parent (N)) = Name_Address
7761 Make_Temporary_For_Slice;
7765 ------------------------------
7766 -- Expand_N_Type_Conversion --
7767 ------------------------------
7769 procedure Expand_N_Type_Conversion (N : Node_Id) is
7770 Loc : constant Source_Ptr := Sloc (N);
7771 Operand : constant Node_Id := Expression (N);
7772 Target_Type : constant Entity_Id := Etype (N);
7773 Operand_Type : Entity_Id := Etype (Operand);
7775 procedure Handle_Changed_Representation;
7776 -- This is called in the case of record and array type conversions to
7777 -- see if there is a change of representation to be handled. Change of
7778 -- representation is actually handled at the assignment statement level,
7779 -- and what this procedure does is rewrite node N conversion as an
7780 -- assignment to temporary. If there is no change of representation,
7781 -- then the conversion node is unchanged.
7783 procedure Raise_Accessibility_Error;
7784 -- Called when we know that an accessibility check will fail. Rewrites
7785 -- node N to an appropriate raise statement and outputs warning msgs.
7786 -- The Etype of the raise node is set to Target_Type.
7788 procedure Real_Range_Check;
7789 -- Handles generation of range check for real target value
7791 -----------------------------------
7792 -- Handle_Changed_Representation --
7793 -----------------------------------
7795 procedure Handle_Changed_Representation is
7804 -- Nothing else to do if no change of representation
7806 if Same_Representation (Operand_Type, Target_Type) then
7809 -- The real change of representation work is done by the assignment
7810 -- statement processing. So if this type conversion is appearing as
7811 -- the expression of an assignment statement, nothing needs to be
7812 -- done to the conversion.
7814 elsif Nkind (Parent (N)) = N_Assignment_Statement then
7817 -- Otherwise we need to generate a temporary variable, and do the
7818 -- change of representation assignment into that temporary variable.
7819 -- The conversion is then replaced by a reference to this variable.
7824 -- If type is unconstrained we have to add a constraint, copied
7825 -- from the actual value of the left hand side.
7827 if not Is_Constrained (Target_Type) then
7828 if Has_Discriminants (Operand_Type) then
7829 Disc := First_Discriminant (Operand_Type);
7831 if Disc /= First_Stored_Discriminant (Operand_Type) then
7832 Disc := First_Stored_Discriminant (Operand_Type);
7836 while Present (Disc) loop
7838 Make_Selected_Component (Loc,
7839 Prefix => Duplicate_Subexpr_Move_Checks (Operand),
7841 Make_Identifier (Loc, Chars (Disc))));
7842 Next_Discriminant (Disc);
7845 elsif Is_Array_Type (Operand_Type) then
7846 N_Ix := First_Index (Target_Type);
7849 for J in 1 .. Number_Dimensions (Operand_Type) loop
7851 -- We convert the bounds explicitly. We use an unchecked
7852 -- conversion because bounds checks are done elsewhere.
7857 Unchecked_Convert_To (Etype (N_Ix),
7858 Make_Attribute_Reference (Loc,
7860 Duplicate_Subexpr_No_Checks
7861 (Operand, Name_Req => True),
7862 Attribute_Name => Name_First,
7863 Expressions => New_List (
7864 Make_Integer_Literal (Loc, J)))),
7867 Unchecked_Convert_To (Etype (N_Ix),
7868 Make_Attribute_Reference (Loc,
7870 Duplicate_Subexpr_No_Checks
7871 (Operand, Name_Req => True),
7872 Attribute_Name => Name_Last,
7873 Expressions => New_List (
7874 Make_Integer_Literal (Loc, J))))));
7881 Odef := New_Occurrence_Of (Target_Type, Loc);
7883 if Present (Cons) then
7885 Make_Subtype_Indication (Loc,
7886 Subtype_Mark => Odef,
7888 Make_Index_Or_Discriminant_Constraint (Loc,
7889 Constraints => Cons));
7892 Temp := Make_Temporary (Loc, 'C');
7894 Make_Object_Declaration (Loc,
7895 Defining_Identifier => Temp,
7896 Object_Definition => Odef);
7898 Set_No_Initialization (Decl, True);
7900 -- Insert required actions. It is essential to suppress checks
7901 -- since we have suppressed default initialization, which means
7902 -- that the variable we create may have no discriminants.
7907 Make_Assignment_Statement (Loc,
7908 Name => New_Occurrence_Of (Temp, Loc),
7909 Expression => Relocate_Node (N))),
7910 Suppress => All_Checks);
7912 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7915 end Handle_Changed_Representation;
7917 -------------------------------
7918 -- Raise_Accessibility_Error --
7919 -------------------------------
7921 procedure Raise_Accessibility_Error is
7924 Make_Raise_Program_Error (Sloc (N),
7925 Reason => PE_Accessibility_Check_Failed));
7926 Set_Etype (N, Target_Type);
7928 Error_Msg_N ("?accessibility check failure", N);
7930 ("\?& will be raised at run time", N, Standard_Program_Error);
7931 end Raise_Accessibility_Error;
7933 ----------------------
7934 -- Real_Range_Check --
7935 ----------------------
7937 -- Case of conversions to floating-point or fixed-point. If range checks
7938 -- are enabled and the target type has a range constraint, we convert:
7944 -- Tnn : typ'Base := typ'Base (x);
7945 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
7948 -- This is necessary when there is a conversion of integer to float or
7949 -- to fixed-point to ensure that the correct checks are made. It is not
7950 -- necessary for float to float where it is enough to simply set the
7951 -- Do_Range_Check flag.
7953 procedure Real_Range_Check is
7954 Btyp : constant Entity_Id := Base_Type (Target_Type);
7955 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
7956 Hi : constant Node_Id := Type_High_Bound (Target_Type);
7957 Xtyp : constant Entity_Id := Etype (Operand);
7962 -- Nothing to do if conversion was rewritten
7964 if Nkind (N) /= N_Type_Conversion then
7968 -- Nothing to do if range checks suppressed, or target has the same
7969 -- range as the base type (or is the base type).
7971 if Range_Checks_Suppressed (Target_Type)
7972 or else (Lo = Type_Low_Bound (Btyp)
7974 Hi = Type_High_Bound (Btyp))
7979 -- Nothing to do if expression is an entity on which checks have been
7982 if Is_Entity_Name (Operand)
7983 and then Range_Checks_Suppressed (Entity (Operand))
7988 -- Nothing to do if bounds are all static and we can tell that the
7989 -- expression is within the bounds of the target. Note that if the
7990 -- operand is of an unconstrained floating-point type, then we do
7991 -- not trust it to be in range (might be infinite)
7994 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
7995 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
7998 if (not Is_Floating_Point_Type (Xtyp)
7999 or else Is_Constrained (Xtyp))
8000 and then Compile_Time_Known_Value (S_Lo)
8001 and then Compile_Time_Known_Value (S_Hi)
8002 and then Compile_Time_Known_Value (Hi)
8003 and then Compile_Time_Known_Value (Lo)
8006 D_Lov : constant Ureal := Expr_Value_R (Lo);
8007 D_Hiv : constant Ureal := Expr_Value_R (Hi);
8012 if Is_Real_Type (Xtyp) then
8013 S_Lov := Expr_Value_R (S_Lo);
8014 S_Hiv := Expr_Value_R (S_Hi);
8016 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
8017 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
8021 and then S_Lov >= D_Lov
8022 and then S_Hiv <= D_Hiv
8024 Set_Do_Range_Check (Operand, False);
8031 -- For float to float conversions, we are done
8033 if Is_Floating_Point_Type (Xtyp)
8035 Is_Floating_Point_Type (Btyp)
8040 -- Otherwise rewrite the conversion as described above
8042 Conv := Relocate_Node (N);
8043 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
8044 Set_Etype (Conv, Btyp);
8046 -- Enable overflow except for case of integer to float conversions,
8047 -- where it is never required, since we can never have overflow in
8050 if not Is_Integer_Type (Etype (Operand)) then
8051 Enable_Overflow_Check (Conv);
8054 Tnn := Make_Temporary (Loc, 'T', Conv);
8056 Insert_Actions (N, New_List (
8057 Make_Object_Declaration (Loc,
8058 Defining_Identifier => Tnn,
8059 Object_Definition => New_Occurrence_Of (Btyp, Loc),
8060 Expression => Conv),
8062 Make_Raise_Constraint_Error (Loc,
8067 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8069 Make_Attribute_Reference (Loc,
8070 Attribute_Name => Name_First,
8072 New_Occurrence_Of (Target_Type, Loc))),
8076 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8078 Make_Attribute_Reference (Loc,
8079 Attribute_Name => Name_Last,
8081 New_Occurrence_Of (Target_Type, Loc)))),
8082 Reason => CE_Range_Check_Failed)));
8084 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
8085 Analyze_And_Resolve (N, Btyp);
8086 end Real_Range_Check;
8088 -- Start of processing for Expand_N_Type_Conversion
8091 -- Nothing at all to do if conversion is to the identical type so remove
8092 -- the conversion completely, it is useless, except that it may carry
8093 -- an Assignment_OK attribute, which must be propagated to the operand.
8095 if Operand_Type = Target_Type then
8096 if Assignment_OK (N) then
8097 Set_Assignment_OK (Operand);
8100 Rewrite (N, Relocate_Node (Operand));
8104 -- Nothing to do if this is the second argument of read. This is a
8105 -- "backwards" conversion that will be handled by the specialized code
8106 -- in attribute processing.
8108 if Nkind (Parent (N)) = N_Attribute_Reference
8109 and then Attribute_Name (Parent (N)) = Name_Read
8110 and then Next (First (Expressions (Parent (N)))) = N
8115 -- Here if we may need to expand conversion
8117 -- If the operand of the type conversion is an arithmetic operation on
8118 -- signed integers, and the based type of the signed integer type in
8119 -- question is smaller than Standard.Integer, we promote both of the
8120 -- operands to type Integer.
8122 -- For example, if we have
8124 -- target-type (opnd1 + opnd2)
8126 -- and opnd1 and opnd2 are of type short integer, then we rewrite
8129 -- target-type (integer(opnd1) + integer(opnd2))
8131 -- We do this because we are always allowed to compute in a larger type
8132 -- if we do the right thing with the result, and in this case we are
8133 -- going to do a conversion which will do an appropriate check to make
8134 -- sure that things are in range of the target type in any case. This
8135 -- avoids some unnecessary intermediate overflows.
8137 -- We might consider a similar transformation in the case where the
8138 -- target is a real type or a 64-bit integer type, and the operand
8139 -- is an arithmetic operation using a 32-bit integer type. However,
8140 -- we do not bother with this case, because it could cause significant
8141 -- ineffiencies on 32-bit machines. On a 64-bit machine it would be
8142 -- much cheaper, but we don't want different behavior on 32-bit and
8143 -- 64-bit machines. Note that the exclusion of the 64-bit case also
8144 -- handles the configurable run-time cases where 64-bit arithmetic
8145 -- may simply be unavailable.
8147 -- Note: this circuit is partially redundant with respect to the circuit
8148 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
8149 -- the processing here. Also we still need the Checks circuit, since we
8150 -- have to be sure not to generate junk overflow checks in the first
8151 -- place, since it would be trick to remove them here!
8153 if Integer_Promotion_Possible (N) then
8155 -- All conditions met, go ahead with transformation
8163 Make_Type_Conversion (Loc,
8164 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
8165 Expression => Relocate_Node (Right_Opnd (Operand)));
8167 Opnd := New_Op_Node (Nkind (Operand), Loc);
8168 Set_Right_Opnd (Opnd, R);
8170 if Nkind (Operand) in N_Binary_Op then
8172 Make_Type_Conversion (Loc,
8173 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
8174 Expression => Relocate_Node (Left_Opnd (Operand)));
8176 Set_Left_Opnd (Opnd, L);
8180 Make_Type_Conversion (Loc,
8181 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
8182 Expression => Opnd));
8184 Analyze_And_Resolve (N, Target_Type);
8189 -- Do validity check if validity checking operands
8191 if Validity_Checks_On
8192 and then Validity_Check_Operands
8194 Ensure_Valid (Operand);
8197 -- Special case of converting from non-standard boolean type
8199 if Is_Boolean_Type (Operand_Type)
8200 and then (Nonzero_Is_True (Operand_Type))
8202 Adjust_Condition (Operand);
8203 Set_Etype (Operand, Standard_Boolean);
8204 Operand_Type := Standard_Boolean;
8207 -- Case of converting to an access type
8209 if Is_Access_Type (Target_Type) then
8211 -- Apply an accessibility check when the conversion operand is an
8212 -- access parameter (or a renaming thereof), unless conversion was
8213 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
8214 -- Note that other checks may still need to be applied below (such
8215 -- as tagged type checks).
8217 if Is_Entity_Name (Operand)
8219 (Is_Formal (Entity (Operand))
8221 (Present (Renamed_Object (Entity (Operand)))
8222 and then Is_Entity_Name (Renamed_Object (Entity (Operand)))
8224 (Entity (Renamed_Object (Entity (Operand))))))
8225 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
8226 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
8227 or else Attribute_Name (Original_Node (N)) = Name_Access)
8229 Apply_Accessibility_Check
8230 (Operand, Target_Type, Insert_Node => Operand);
8232 -- If the level of the operand type is statically deeper than the
8233 -- level of the target type, then force Program_Error. Note that this
8234 -- can only occur for cases where the attribute is within the body of
8235 -- an instantiation (otherwise the conversion will already have been
8236 -- rejected as illegal). Note: warnings are issued by the analyzer
8237 -- for the instance cases.
8239 elsif In_Instance_Body
8240 and then Type_Access_Level (Operand_Type) >
8241 Type_Access_Level (Target_Type)
8243 Raise_Accessibility_Error;
8245 -- When the operand is a selected access discriminant the check needs
8246 -- to be made against the level of the object denoted by the prefix
8247 -- of the selected name. Force Program_Error for this case as well
8248 -- (this accessibility violation can only happen if within the body
8249 -- of an instantiation).
8251 elsif In_Instance_Body
8252 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
8253 and then Nkind (Operand) = N_Selected_Component
8254 and then Object_Access_Level (Operand) >
8255 Type_Access_Level (Target_Type)
8257 Raise_Accessibility_Error;
8262 -- Case of conversions of tagged types and access to tagged types
8264 -- When needed, that is to say when the expression is class-wide, Add
8265 -- runtime a tag check for (strict) downward conversion by using the
8266 -- membership test, generating:
8268 -- [constraint_error when Operand not in Target_Type'Class]
8270 -- or in the access type case
8272 -- [constraint_error
8273 -- when Operand /= null
8274 -- and then Operand.all not in
8275 -- Designated_Type (Target_Type)'Class]
8277 if (Is_Access_Type (Target_Type)
8278 and then Is_Tagged_Type (Designated_Type (Target_Type)))
8279 or else Is_Tagged_Type (Target_Type)
8281 -- Do not do any expansion in the access type case if the parent is a
8282 -- renaming, since this is an error situation which will be caught by
8283 -- Sem_Ch8, and the expansion can interfere with this error check.
8285 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
8289 -- Otherwise, proceed with processing tagged conversion
8291 Tagged_Conversion : declare
8292 Actual_Op_Typ : Entity_Id;
8293 Actual_Targ_Typ : Entity_Id;
8294 Make_Conversion : Boolean := False;
8295 Root_Op_Typ : Entity_Id;
8297 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
8298 -- Create a membership check to test whether Operand is a member
8299 -- of Targ_Typ. If the original Target_Type is an access, include
8300 -- a test for null value. The check is inserted at N.
8302 --------------------
8303 -- Make_Tag_Check --
8304 --------------------
8306 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
8311 -- [Constraint_Error
8312 -- when Operand /= null
8313 -- and then Operand.all not in Targ_Typ]
8315 if Is_Access_Type (Target_Type) then
8320 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
8321 Right_Opnd => Make_Null (Loc)),
8326 Make_Explicit_Dereference (Loc,
8327 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
8328 Right_Opnd => New_Reference_To (Targ_Typ, Loc)));
8331 -- [Constraint_Error when Operand not in Targ_Typ]
8336 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
8337 Right_Opnd => New_Reference_To (Targ_Typ, Loc));
8341 Make_Raise_Constraint_Error (Loc,
8343 Reason => CE_Tag_Check_Failed));
8346 -- Start of processing for Tagged_Conversion
8349 if Is_Access_Type (Target_Type) then
8351 -- Handle entities from the limited view
8354 Available_View (Designated_Type (Operand_Type));
8356 Available_View (Designated_Type (Target_Type));
8358 Actual_Op_Typ := Operand_Type;
8359 Actual_Targ_Typ := Target_Type;
8362 Root_Op_Typ := Root_Type (Actual_Op_Typ);
8364 -- Ada 2005 (AI-251): Handle interface type conversion
8366 if Is_Interface (Actual_Op_Typ) then
8367 Expand_Interface_Conversion (N, Is_Static => False);
8371 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
8373 -- Create a runtime tag check for a downward class-wide type
8376 if Is_Class_Wide_Type (Actual_Op_Typ)
8377 and then Actual_Op_Typ /= Actual_Targ_Typ
8378 and then Root_Op_Typ /= Actual_Targ_Typ
8379 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ)
8381 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
8382 Make_Conversion := True;
8385 -- AI05-0073: If the result subtype of the function is defined
8386 -- by an access_definition designating a specific tagged type
8387 -- T, a check is made that the result value is null or the tag
8388 -- of the object designated by the result value identifies T.
8389 -- Constraint_Error is raised if this check fails.
8391 if Nkind (Parent (N)) = Sinfo.N_Return_Statement then
8394 Func_Typ : Entity_Id;
8397 -- Climb scope stack looking for the enclosing function
8399 Func := Current_Scope;
8400 while Present (Func)
8401 and then Ekind (Func) /= E_Function
8403 Func := Scope (Func);
8406 -- The function's return subtype must be defined using
8407 -- an access definition.
8409 if Nkind (Result_Definition (Parent (Func))) =
8412 Func_Typ := Directly_Designated_Type (Etype (Func));
8414 -- The return subtype denotes a specific tagged type,
8415 -- in other words, a non class-wide type.
8417 if Is_Tagged_Type (Func_Typ)
8418 and then not Is_Class_Wide_Type (Func_Typ)
8420 Make_Tag_Check (Actual_Targ_Typ);
8421 Make_Conversion := True;
8427 -- We have generated a tag check for either a class-wide type
8428 -- conversion or for AI05-0073.
8430 if Make_Conversion then
8435 Make_Unchecked_Type_Conversion (Loc,
8436 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
8437 Expression => Relocate_Node (Expression (N)));
8439 Analyze_And_Resolve (N, Target_Type);
8443 end Tagged_Conversion;
8445 -- Case of other access type conversions
8447 elsif Is_Access_Type (Target_Type) then
8448 Apply_Constraint_Check (Operand, Target_Type);
8450 -- Case of conversions from a fixed-point type
8452 -- These conversions require special expansion and processing, found in
8453 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
8454 -- since from a semantic point of view, these are simple integer
8455 -- conversions, which do not need further processing.
8457 elsif Is_Fixed_Point_Type (Operand_Type)
8458 and then not Conversion_OK (N)
8460 -- We should never see universal fixed at this case, since the
8461 -- expansion of the constituent divide or multiply should have
8462 -- eliminated the explicit mention of universal fixed.
8464 pragma Assert (Operand_Type /= Universal_Fixed);
8466 -- Check for special case of the conversion to universal real that
8467 -- occurs as a result of the use of a round attribute. In this case,
8468 -- the real type for the conversion is taken from the target type of
8469 -- the Round attribute and the result must be marked as rounded.
8471 if Target_Type = Universal_Real
8472 and then Nkind (Parent (N)) = N_Attribute_Reference
8473 and then Attribute_Name (Parent (N)) = Name_Round
8475 Set_Rounded_Result (N);
8476 Set_Etype (N, Etype (Parent (N)));
8479 -- Otherwise do correct fixed-conversion, but skip these if the
8480 -- Conversion_OK flag is set, because from a semantic point of view
8481 -- these are simple integer conversions needing no further processing
8482 -- (the backend will simply treat them as integers).
8484 if not Conversion_OK (N) then
8485 if Is_Fixed_Point_Type (Etype (N)) then
8486 Expand_Convert_Fixed_To_Fixed (N);
8489 elsif Is_Integer_Type (Etype (N)) then
8490 Expand_Convert_Fixed_To_Integer (N);
8493 pragma Assert (Is_Floating_Point_Type (Etype (N)));
8494 Expand_Convert_Fixed_To_Float (N);
8499 -- Case of conversions to a fixed-point type
8501 -- These conversions require special expansion and processing, found in
8502 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
8503 -- since from a semantic point of view, these are simple integer
8504 -- conversions, which do not need further processing.
8506 elsif Is_Fixed_Point_Type (Target_Type)
8507 and then not Conversion_OK (N)
8509 if Is_Integer_Type (Operand_Type) then
8510 Expand_Convert_Integer_To_Fixed (N);
8513 pragma Assert (Is_Floating_Point_Type (Operand_Type));
8514 Expand_Convert_Float_To_Fixed (N);
8518 -- Case of float-to-integer conversions
8520 -- We also handle float-to-fixed conversions with Conversion_OK set
8521 -- since semantically the fixed-point target is treated as though it
8522 -- were an integer in such cases.
8524 elsif Is_Floating_Point_Type (Operand_Type)
8526 (Is_Integer_Type (Target_Type)
8528 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
8530 -- One more check here, gcc is still not able to do conversions of
8531 -- this type with proper overflow checking, and so gigi is doing an
8532 -- approximation of what is required by doing floating-point compares
8533 -- with the end-point. But that can lose precision in some cases, and
8534 -- give a wrong result. Converting the operand to Universal_Real is
8535 -- helpful, but still does not catch all cases with 64-bit integers
8536 -- on targets with only 64-bit floats.
8538 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
8539 -- Can this code be removed ???
8541 if Do_Range_Check (Operand) then
8543 Make_Type_Conversion (Loc,
8545 New_Occurrence_Of (Universal_Real, Loc),
8547 Relocate_Node (Operand)));
8549 Set_Etype (Operand, Universal_Real);
8550 Enable_Range_Check (Operand);
8551 Set_Do_Range_Check (Expression (Operand), False);
8554 -- Case of array conversions
8556 -- Expansion of array conversions, add required length/range checks but
8557 -- only do this if there is no change of representation. For handling of
8558 -- this case, see Handle_Changed_Representation.
8560 elsif Is_Array_Type (Target_Type) then
8562 if Is_Constrained (Target_Type) then
8563 Apply_Length_Check (Operand, Target_Type);
8565 Apply_Range_Check (Operand, Target_Type);
8568 Handle_Changed_Representation;
8570 -- Case of conversions of discriminated types
8572 -- Add required discriminant checks if target is constrained. Again this
8573 -- change is skipped if we have a change of representation.
8575 elsif Has_Discriminants (Target_Type)
8576 and then Is_Constrained (Target_Type)
8578 Apply_Discriminant_Check (Operand, Target_Type);
8579 Handle_Changed_Representation;
8581 -- Case of all other record conversions. The only processing required
8582 -- is to check for a change of representation requiring the special
8583 -- assignment processing.
8585 elsif Is_Record_Type (Target_Type) then
8587 -- Ada 2005 (AI-216): Program_Error is raised when converting from
8588 -- a derived Unchecked_Union type to an unconstrained type that is
8589 -- not Unchecked_Union if the operand lacks inferable discriminants.
8591 if Is_Derived_Type (Operand_Type)
8592 and then Is_Unchecked_Union (Base_Type (Operand_Type))
8593 and then not Is_Constrained (Target_Type)
8594 and then not Is_Unchecked_Union (Base_Type (Target_Type))
8595 and then not Has_Inferable_Discriminants (Operand)
8597 -- To prevent Gigi from generating illegal code, we generate a
8598 -- Program_Error node, but we give it the target type of the
8602 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
8603 Reason => PE_Unchecked_Union_Restriction);
8606 Set_Etype (PE, Target_Type);
8611 Handle_Changed_Representation;
8614 -- Case of conversions of enumeration types
8616 elsif Is_Enumeration_Type (Target_Type) then
8618 -- Special processing is required if there is a change of
8619 -- representation (from enumeration representation clauses).
8621 if not Same_Representation (Target_Type, Operand_Type) then
8623 -- Convert: x(y) to x'val (ytyp'val (y))
8626 Make_Attribute_Reference (Loc,
8627 Prefix => New_Occurrence_Of (Target_Type, Loc),
8628 Attribute_Name => Name_Val,
8629 Expressions => New_List (
8630 Make_Attribute_Reference (Loc,
8631 Prefix => New_Occurrence_Of (Operand_Type, Loc),
8632 Attribute_Name => Name_Pos,
8633 Expressions => New_List (Operand)))));
8635 Analyze_And_Resolve (N, Target_Type);
8638 -- Case of conversions to floating-point
8640 elsif Is_Floating_Point_Type (Target_Type) then
8644 -- At this stage, either the conversion node has been transformed into
8645 -- some other equivalent expression, or left as a conversion that can be
8646 -- handled by Gigi, in the following cases:
8648 -- Conversions with no change of representation or type
8650 -- Numeric conversions involving integer, floating- and fixed-point
8651 -- values. Fixed-point values are allowed only if Conversion_OK is
8652 -- set, i.e. if the fixed-point values are to be treated as integers.
8654 -- No other conversions should be passed to Gigi
8656 -- Check: are these rules stated in sinfo??? if so, why restate here???
8658 -- The only remaining step is to generate a range check if we still have
8659 -- a type conversion at this stage and Do_Range_Check is set. For now we
8660 -- do this only for conversions of discrete types.
8662 if Nkind (N) = N_Type_Conversion
8663 and then Is_Discrete_Type (Etype (N))
8666 Expr : constant Node_Id := Expression (N);
8671 if Do_Range_Check (Expr)
8672 and then Is_Discrete_Type (Etype (Expr))
8674 Set_Do_Range_Check (Expr, False);
8676 -- Before we do a range check, we have to deal with treating a
8677 -- fixed-point operand as an integer. The way we do this is
8678 -- simply to do an unchecked conversion to an appropriate
8679 -- integer type large enough to hold the result.
8681 -- This code is not active yet, because we are only dealing
8682 -- with discrete types so far ???
8684 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
8685 and then Treat_Fixed_As_Integer (Expr)
8687 Ftyp := Base_Type (Etype (Expr));
8689 if Esize (Ftyp) >= Esize (Standard_Integer) then
8690 Ityp := Standard_Long_Long_Integer;
8692 Ityp := Standard_Integer;
8695 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
8698 -- Reset overflow flag, since the range check will include
8699 -- dealing with possible overflow, and generate the check. If
8700 -- Address is either a source type or target type, suppress
8701 -- range check to avoid typing anomalies when it is a visible
8704 Set_Do_Overflow_Check (N, False);
8705 if not Is_Descendent_Of_Address (Etype (Expr))
8706 and then not Is_Descendent_Of_Address (Target_Type)
8708 Generate_Range_Check
8709 (Expr, Target_Type, CE_Range_Check_Failed);
8715 -- Final step, if the result is a type conversion involving Vax_Float
8716 -- types, then it is subject for further special processing.
8718 if Nkind (N) = N_Type_Conversion
8719 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
8721 Expand_Vax_Conversion (N);
8724 end Expand_N_Type_Conversion;
8726 -----------------------------------
8727 -- Expand_N_Unchecked_Expression --
8728 -----------------------------------
8730 -- Remove the unchecked expression node from the tree. Its job was simply
8731 -- to make sure that its constituent expression was handled with checks
8732 -- off, and now that that is done, we can remove it from the tree, and
8733 -- indeed must, since Gigi does not expect to see these nodes.
8735 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
8736 Exp : constant Node_Id := Expression (N);
8738 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
8740 end Expand_N_Unchecked_Expression;
8742 ----------------------------------------
8743 -- Expand_N_Unchecked_Type_Conversion --
8744 ----------------------------------------
8746 -- If this cannot be handled by Gigi and we haven't already made a
8747 -- temporary for it, do it now.
8749 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
8750 Target_Type : constant Entity_Id := Etype (N);
8751 Operand : constant Node_Id := Expression (N);
8752 Operand_Type : constant Entity_Id := Etype (Operand);
8755 -- Nothing at all to do if conversion is to the identical type so remove
8756 -- the conversion completely, it is useless, except that it may carry
8757 -- an Assignment_OK indication which must be propagated to the operand.
8759 if Operand_Type = Target_Type then
8761 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
8763 if Assignment_OK (N) then
8764 Set_Assignment_OK (Operand);
8767 Rewrite (N, Relocate_Node (Operand));
8771 -- If we have a conversion of a compile time known value to a target
8772 -- type and the value is in range of the target type, then we can simply
8773 -- replace the construct by an integer literal of the correct type. We
8774 -- only apply this to integer types being converted. Possibly it may
8775 -- apply in other cases, but it is too much trouble to worry about.
8777 -- Note that we do not do this transformation if the Kill_Range_Check
8778 -- flag is set, since then the value may be outside the expected range.
8779 -- This happens in the Normalize_Scalars case.
8781 -- We also skip this if either the target or operand type is biased
8782 -- because in this case, the unchecked conversion is supposed to
8783 -- preserve the bit pattern, not the integer value.
8785 if Is_Integer_Type (Target_Type)
8786 and then not Has_Biased_Representation (Target_Type)
8787 and then Is_Integer_Type (Operand_Type)
8788 and then not Has_Biased_Representation (Operand_Type)
8789 and then Compile_Time_Known_Value (Operand)
8790 and then not Kill_Range_Check (N)
8793 Val : constant Uint := Expr_Value (Operand);
8796 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
8798 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
8800 Val >= Expr_Value (Type_Low_Bound (Target_Type))
8802 Val <= Expr_Value (Type_High_Bound (Target_Type))
8804 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
8806 -- If Address is the target type, just set the type to avoid a
8807 -- spurious type error on the literal when Address is a visible
8810 if Is_Descendent_Of_Address (Target_Type) then
8811 Set_Etype (N, Target_Type);
8813 Analyze_And_Resolve (N, Target_Type);
8821 -- Nothing to do if conversion is safe
8823 if Safe_Unchecked_Type_Conversion (N) then
8827 -- Otherwise force evaluation unless Assignment_OK flag is set (this
8828 -- flag indicates ??? -- more comments needed here)
8830 if Assignment_OK (N) then
8833 Force_Evaluation (N);
8835 end Expand_N_Unchecked_Type_Conversion;
8837 ----------------------------
8838 -- Expand_Record_Equality --
8839 ----------------------------
8841 -- For non-variant records, Equality is expanded when needed into:
8843 -- and then Lhs.Discr1 = Rhs.Discr1
8845 -- and then Lhs.Discrn = Rhs.Discrn
8846 -- and then Lhs.Cmp1 = Rhs.Cmp1
8848 -- and then Lhs.Cmpn = Rhs.Cmpn
8850 -- The expression is folded by the back-end for adjacent fields. This
8851 -- function is called for tagged record in only one occasion: for imple-
8852 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
8853 -- otherwise the primitive "=" is used directly.
8855 function Expand_Record_Equality
8860 Bodies : List_Id) return Node_Id
8862 Loc : constant Source_Ptr := Sloc (Nod);
8867 First_Time : Boolean := True;
8869 function Suitable_Element (C : Entity_Id) return Entity_Id;
8870 -- Return the first field to compare beginning with C, skipping the
8871 -- inherited components.
8873 ----------------------
8874 -- Suitable_Element --
8875 ----------------------
8877 function Suitable_Element (C : Entity_Id) return Entity_Id is
8882 elsif Ekind (C) /= E_Discriminant
8883 and then Ekind (C) /= E_Component
8885 return Suitable_Element (Next_Entity (C));
8887 elsif Is_Tagged_Type (Typ)
8888 and then C /= Original_Record_Component (C)
8890 return Suitable_Element (Next_Entity (C));
8892 elsif Chars (C) = Name_uController
8893 or else Chars (C) = Name_uTag
8895 return Suitable_Element (Next_Entity (C));
8897 elsif Is_Interface (Etype (C)) then
8898 return Suitable_Element (Next_Entity (C));
8903 end Suitable_Element;
8905 -- Start of processing for Expand_Record_Equality
8908 -- Generates the following code: (assuming that Typ has one Discr and
8909 -- component C2 is also a record)
8912 -- and then Lhs.Discr1 = Rhs.Discr1
8913 -- and then Lhs.C1 = Rhs.C1
8914 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
8916 -- and then Lhs.Cmpn = Rhs.Cmpn
8918 Result := New_Reference_To (Standard_True, Loc);
8919 C := Suitable_Element (First_Entity (Typ));
8920 while Present (C) loop
8928 First_Time := False;
8932 New_Lhs := New_Copy_Tree (Lhs);
8933 New_Rhs := New_Copy_Tree (Rhs);
8937 Expand_Composite_Equality (Nod, Etype (C),
8939 Make_Selected_Component (Loc,
8941 Selector_Name => New_Reference_To (C, Loc)),
8943 Make_Selected_Component (Loc,
8945 Selector_Name => New_Reference_To (C, Loc)),
8948 -- If some (sub)component is an unchecked_union, the whole
8949 -- operation will raise program error.
8951 if Nkind (Check) = N_Raise_Program_Error then
8953 Set_Etype (Result, Standard_Boolean);
8958 Left_Opnd => Result,
8959 Right_Opnd => Check);
8963 C := Suitable_Element (Next_Entity (C));
8967 end Expand_Record_Equality;
8969 -----------------------------------
8970 -- Expand_Short_Circuit_Operator --
8971 -----------------------------------
8973 -- Deal with special expansion if actions are present for the right operand
8974 -- and deal with optimizing case of arguments being True or False. We also
8975 -- deal with the special case of non-standard boolean values.
8977 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
8978 Loc : constant Source_Ptr := Sloc (N);
8979 Typ : constant Entity_Id := Etype (N);
8980 Left : constant Node_Id := Left_Opnd (N);
8981 Right : constant Node_Id := Right_Opnd (N);
8982 LocR : constant Source_Ptr := Sloc (Right);
8985 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
8986 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
8987 -- If Left = Shortcut_Value then Right need not be evaluated
8989 function Make_Test_Expr (Opnd : Node_Id) return Node_Id;
8990 -- For Opnd a boolean expression, return a Boolean expression equivalent
8991 -- to Opnd /= Shortcut_Value.
8993 --------------------
8994 -- Make_Test_Expr --
8995 --------------------
8997 function Make_Test_Expr (Opnd : Node_Id) return Node_Id is
8999 if Shortcut_Value then
9000 return Make_Op_Not (Sloc (Opnd), Opnd);
9007 -- Entity for a temporary variable holding the value of the operator,
9008 -- used for expansion in the case where actions are present.
9010 -- Start of processing for Expand_Short_Circuit_Operator
9013 -- Deal with non-standard booleans
9015 if Is_Boolean_Type (Typ) then
9016 Adjust_Condition (Left);
9017 Adjust_Condition (Right);
9018 Set_Etype (N, Standard_Boolean);
9021 -- Check for cases where left argument is known to be True or False
9023 if Compile_Time_Known_Value (Left) then
9025 -- Mark SCO for left condition as compile time known
9027 if Generate_SCO and then Comes_From_Source (Left) then
9028 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
9031 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
9032 -- Any actions associated with Right will be executed unconditionally
9033 -- and can thus be inserted into the tree unconditionally.
9035 if Expr_Value_E (Left) /= Shortcut_Ent then
9036 if Present (Actions (N)) then
9037 Insert_Actions (N, Actions (N));
9042 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
9043 -- In this case we can forget the actions associated with Right,
9044 -- since they will never be executed.
9047 Kill_Dead_Code (Right);
9048 Kill_Dead_Code (Actions (N));
9049 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
9052 Adjust_Result_Type (N, Typ);
9056 -- If Actions are present for the right operand, we have to do some
9057 -- special processing. We can't just let these actions filter back into
9058 -- code preceding the short circuit (which is what would have happened
9059 -- if we had not trapped them in the short-circuit form), since they
9060 -- must only be executed if the right operand of the short circuit is
9061 -- executed and not otherwise.
9063 -- the temporary variable C.
9065 if Present (Actions (N)) then
9066 Actlist := Actions (N);
9068 -- The old approach is to expand:
9070 -- left AND THEN right
9074 -- C : Boolean := False;
9082 -- and finally rewrite the operator into a reference to C. Similarly
9083 -- for left OR ELSE right, with negated values. Note that this
9084 -- rewrite causes some difficulties for coverage analysis because
9085 -- of the introduction of the new variable C, which obscures the
9086 -- structure of the test.
9088 -- We use this "old approach" if use of N_Expression_With_Actions
9089 -- is False (see description in Opt of when this is or is not set).
9091 if not Use_Expression_With_Actions then
9092 Op_Var := Make_Temporary (Loc, 'C', Related_Node => N);
9095 Make_Object_Declaration (Loc,
9096 Defining_Identifier =>
9098 Object_Definition =>
9099 New_Occurrence_Of (Standard_Boolean, Loc),
9101 New_Occurrence_Of (Shortcut_Ent, Loc)));
9104 Make_Implicit_If_Statement (Right,
9105 Condition => Make_Test_Expr (Right),
9106 Then_Statements => New_List (
9107 Make_Assignment_Statement (LocR,
9108 Name => New_Occurrence_Of (Op_Var, LocR),
9111 (Boolean_Literals (not Shortcut_Value), LocR)))));
9114 Make_Implicit_If_Statement (Left,
9115 Condition => Make_Test_Expr (Left),
9116 Then_Statements => Actlist));
9118 Rewrite (N, New_Occurrence_Of (Op_Var, Loc));
9119 Analyze_And_Resolve (N, Standard_Boolean);
9121 -- The new approach, activated for now by the use of debug flag
9122 -- -gnatd.X is to use the new Expression_With_Actions node for the
9123 -- right operand of the short-circuit form. This should solve the
9124 -- traceability problems for coverage analysis.
9128 Make_Expression_With_Actions (LocR,
9129 Expression => Relocate_Node (Right),
9130 Actions => Actlist));
9131 Set_Actions (N, No_List);
9132 Analyze_And_Resolve (Right, Standard_Boolean);
9135 Adjust_Result_Type (N, Typ);
9139 -- No actions present, check for cases of right argument True/False
9141 if Compile_Time_Known_Value (Right) then
9143 -- Mark SCO for left condition as compile time known
9145 if Generate_SCO and then Comes_From_Source (Right) then
9146 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
9149 -- Change (Left and then True), (Left or else False) to Left.
9150 -- Note that we know there are no actions associated with the right
9151 -- operand, since we just checked for this case above.
9153 if Expr_Value_E (Right) /= Shortcut_Ent then
9156 -- Change (Left and then False), (Left or else True) to Right,
9157 -- making sure to preserve any side effects associated with the Left
9161 Remove_Side_Effects (Left);
9162 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
9166 Adjust_Result_Type (N, Typ);
9167 end Expand_Short_Circuit_Operator;
9169 -------------------------------------
9170 -- Fixup_Universal_Fixed_Operation --
9171 -------------------------------------
9173 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
9174 Conv : constant Node_Id := Parent (N);
9177 -- We must have a type conversion immediately above us
9179 pragma Assert (Nkind (Conv) = N_Type_Conversion);
9181 -- Normally the type conversion gives our target type. The exception
9182 -- occurs in the case of the Round attribute, where the conversion
9183 -- will be to universal real, and our real type comes from the Round
9184 -- attribute (as well as an indication that we must round the result)
9186 if Nkind (Parent (Conv)) = N_Attribute_Reference
9187 and then Attribute_Name (Parent (Conv)) = Name_Round
9189 Set_Etype (N, Etype (Parent (Conv)));
9190 Set_Rounded_Result (N);
9192 -- Normal case where type comes from conversion above us
9195 Set_Etype (N, Etype (Conv));
9197 end Fixup_Universal_Fixed_Operation;
9199 ------------------------------
9200 -- Get_Allocator_Final_List --
9201 ------------------------------
9203 function Get_Allocator_Final_List
9206 PtrT : Entity_Id) return Entity_Id
9208 Loc : constant Source_Ptr := Sloc (N);
9210 Owner : Entity_Id := PtrT;
9211 -- The entity whose finalization list must be used to attach the
9212 -- allocated object.
9215 if Ekind (PtrT) = E_Anonymous_Access_Type then
9217 -- If the context is an access parameter, we need to create a
9218 -- non-anonymous access type in order to have a usable final list,
9219 -- because there is otherwise no pool to which the allocated object
9220 -- can belong. We create both the type and the finalization chain
9221 -- here, because freezing an internal type does not create such a
9222 -- chain. The Final_Chain that is thus created is shared by the
9223 -- access parameter. The access type is tested against the result
9224 -- type of the function to exclude allocators whose type is an
9225 -- anonymous access result type. We freeze the type at once to
9226 -- ensure that it is properly decorated for the back-end, even
9227 -- if the context and current scope is a loop.
9229 if Nkind (Associated_Node_For_Itype (PtrT))
9230 in N_Subprogram_Specification
9233 Etype (Defining_Unit_Name (Associated_Node_For_Itype (PtrT)))
9235 Owner := Make_Temporary (Loc, 'J');
9237 Make_Full_Type_Declaration (Loc,
9238 Defining_Identifier => Owner,
9240 Make_Access_To_Object_Definition (Loc,
9241 Subtype_Indication =>
9242 New_Occurrence_Of (T, Loc))));
9244 Freeze_Before (N, Owner);
9245 Build_Final_List (N, Owner);
9246 Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner));
9248 -- Ada 2005 (AI-318-02): If the context is a return object
9249 -- declaration, then the anonymous return subtype is defined to have
9250 -- the same accessibility level as that of the function's result
9251 -- subtype, which means that we want the scope where the function is
9254 elsif Nkind (Associated_Node_For_Itype (PtrT)) = N_Object_Declaration
9255 and then Ekind (Scope (PtrT)) = E_Return_Statement
9257 Owner := Scope (Return_Applies_To (Scope (PtrT)));
9259 -- Case of an access discriminant, or (Ada 2005) of an anonymous
9260 -- access component or anonymous access function result: find the
9261 -- final list associated with the scope of the type. (In the
9262 -- anonymous access component kind, a list controller will have
9263 -- been allocated when freezing the record type, and PtrT has an
9264 -- Associated_Final_Chain attribute designating it.)
9266 elsif No (Associated_Final_Chain (PtrT)) then
9267 Owner := Scope (PtrT);
9271 return Find_Final_List (Owner);
9272 end Get_Allocator_Final_List;
9274 ---------------------------------
9275 -- Has_Inferable_Discriminants --
9276 ---------------------------------
9278 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
9280 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
9281 -- Determines whether the left-most prefix of a selected component is a
9282 -- formal parameter in a subprogram. Assumes N is a selected component.
9284 --------------------------------
9285 -- Prefix_Is_Formal_Parameter --
9286 --------------------------------
9288 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
9289 Sel_Comp : Node_Id := N;
9292 -- Move to the left-most prefix by climbing up the tree
9294 while Present (Parent (Sel_Comp))
9295 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
9297 Sel_Comp := Parent (Sel_Comp);
9300 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
9301 end Prefix_Is_Formal_Parameter;
9303 -- Start of processing for Has_Inferable_Discriminants
9306 -- For identifiers and indexed components, it is sufficient to have a
9307 -- constrained Unchecked_Union nominal subtype.
9309 if Nkind_In (N, N_Identifier, N_Indexed_Component) then
9310 return Is_Unchecked_Union (Base_Type (Etype (N)))
9312 Is_Constrained (Etype (N));
9314 -- For selected components, the subtype of the selector must be a
9315 -- constrained Unchecked_Union. If the component is subject to a
9316 -- per-object constraint, then the enclosing object must have inferable
9319 elsif Nkind (N) = N_Selected_Component then
9320 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
9322 -- A small hack. If we have a per-object constrained selected
9323 -- component of a formal parameter, return True since we do not
9324 -- know the actual parameter association yet.
9326 if Prefix_Is_Formal_Parameter (N) then
9330 -- Otherwise, check the enclosing object and the selector
9332 return Has_Inferable_Discriminants (Prefix (N))
9334 Has_Inferable_Discriminants (Selector_Name (N));
9337 -- The call to Has_Inferable_Discriminants will determine whether
9338 -- the selector has a constrained Unchecked_Union nominal type.
9340 return Has_Inferable_Discriminants (Selector_Name (N));
9342 -- A qualified expression has inferable discriminants if its subtype
9343 -- mark is a constrained Unchecked_Union subtype.
9345 elsif Nkind (N) = N_Qualified_Expression then
9346 return Is_Unchecked_Union (Subtype_Mark (N))
9348 Is_Constrained (Subtype_Mark (N));
9353 end Has_Inferable_Discriminants;
9355 -------------------------------
9356 -- Insert_Dereference_Action --
9357 -------------------------------
9359 procedure Insert_Dereference_Action (N : Node_Id) is
9360 Loc : constant Source_Ptr := Sloc (N);
9361 Typ : constant Entity_Id := Etype (N);
9362 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
9363 Pnod : constant Node_Id := Parent (N);
9365 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
9366 -- Return true if type of P is derived from Checked_Pool;
9368 -----------------------------
9369 -- Is_Checked_Storage_Pool --
9370 -----------------------------
9372 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
9381 while T /= Etype (T) loop
9382 if Is_RTE (T, RE_Checked_Pool) then
9390 end Is_Checked_Storage_Pool;
9392 -- Start of processing for Insert_Dereference_Action
9395 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
9397 if not (Is_Checked_Storage_Pool (Pool)
9398 and then Comes_From_Source (Original_Node (Pnod)))
9404 Make_Procedure_Call_Statement (Loc,
9405 Name => New_Reference_To (
9406 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
9408 Parameter_Associations => New_List (
9412 New_Reference_To (Pool, Loc),
9414 -- Storage_Address. We use the attribute Pool_Address, which uses
9415 -- the pointer itself to find the address of the object, and which
9416 -- handles unconstrained arrays properly by computing the address
9417 -- of the template. i.e. the correct address of the corresponding
9420 Make_Attribute_Reference (Loc,
9421 Prefix => Duplicate_Subexpr_Move_Checks (N),
9422 Attribute_Name => Name_Pool_Address),
9424 -- Size_In_Storage_Elements
9426 Make_Op_Divide (Loc,
9428 Make_Attribute_Reference (Loc,
9430 Make_Explicit_Dereference (Loc,
9431 Duplicate_Subexpr_Move_Checks (N)),
9432 Attribute_Name => Name_Size),
9434 Make_Integer_Literal (Loc, System_Storage_Unit)),
9438 Make_Attribute_Reference (Loc,
9440 Make_Explicit_Dereference (Loc,
9441 Duplicate_Subexpr_Move_Checks (N)),
9442 Attribute_Name => Name_Alignment))));
9445 when RE_Not_Available =>
9447 end Insert_Dereference_Action;
9449 --------------------------------
9450 -- Integer_Promotion_Possible --
9451 --------------------------------
9453 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
9454 Operand : constant Node_Id := Expression (N);
9455 Operand_Type : constant Entity_Id := Etype (Operand);
9456 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
9459 pragma Assert (Nkind (N) = N_Type_Conversion);
9463 -- We only do the transformation for source constructs. We assume
9464 -- that the expander knows what it is doing when it generates code.
9466 Comes_From_Source (N)
9468 -- If the operand type is Short_Integer or Short_Short_Integer,
9469 -- then we will promote to Integer, which is available on all
9470 -- targets, and is sufficient to ensure no intermediate overflow.
9471 -- Furthermore it is likely to be as efficient or more efficient
9472 -- than using the smaller type for the computation so we do this
9476 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
9478 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
9480 -- Test for interesting operation, which includes addition,
9481 -- division, exponentiation, multiplication, subtraction, absolute
9482 -- value and unary negation. Unary "+" is omitted since it is a
9483 -- no-op and thus can't overflow.
9485 and then Nkind_In (Operand, N_Op_Abs,
9492 end Integer_Promotion_Possible;
9494 ------------------------------
9495 -- Make_Array_Comparison_Op --
9496 ------------------------------
9498 -- This is a hand-coded expansion of the following generic function:
9501 -- type elem is (<>);
9502 -- type index is (<>);
9503 -- type a is array (index range <>) of elem;
9505 -- function Gnnn (X : a; Y: a) return boolean is
9506 -- J : index := Y'first;
9509 -- if X'length = 0 then
9512 -- elsif Y'length = 0 then
9516 -- for I in X'range loop
9517 -- if X (I) = Y (J) then
9518 -- if J = Y'last then
9521 -- J := index'succ (J);
9525 -- return X (I) > Y (J);
9529 -- return X'length > Y'length;
9533 -- Note that since we are essentially doing this expansion by hand, we
9534 -- do not need to generate an actual or formal generic part, just the
9535 -- instantiated function itself.
9537 function Make_Array_Comparison_Op
9539 Nod : Node_Id) return Node_Id
9541 Loc : constant Source_Ptr := Sloc (Nod);
9543 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
9544 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
9545 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
9546 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
9548 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
9550 Loop_Statement : Node_Id;
9551 Loop_Body : Node_Id;
9554 Final_Expr : Node_Id;
9555 Func_Body : Node_Id;
9556 Func_Name : Entity_Id;
9562 -- if J = Y'last then
9565 -- J := index'succ (J);
9569 Make_Implicit_If_Statement (Nod,
9572 Left_Opnd => New_Reference_To (J, Loc),
9574 Make_Attribute_Reference (Loc,
9575 Prefix => New_Reference_To (Y, Loc),
9576 Attribute_Name => Name_Last)),
9578 Then_Statements => New_List (
9579 Make_Exit_Statement (Loc)),
9583 Make_Assignment_Statement (Loc,
9584 Name => New_Reference_To (J, Loc),
9586 Make_Attribute_Reference (Loc,
9587 Prefix => New_Reference_To (Index, Loc),
9588 Attribute_Name => Name_Succ,
9589 Expressions => New_List (New_Reference_To (J, Loc))))));
9591 -- if X (I) = Y (J) then
9594 -- return X (I) > Y (J);
9598 Make_Implicit_If_Statement (Nod,
9602 Make_Indexed_Component (Loc,
9603 Prefix => New_Reference_To (X, Loc),
9604 Expressions => New_List (New_Reference_To (I, Loc))),
9607 Make_Indexed_Component (Loc,
9608 Prefix => New_Reference_To (Y, Loc),
9609 Expressions => New_List (New_Reference_To (J, Loc)))),
9611 Then_Statements => New_List (Inner_If),
9613 Else_Statements => New_List (
9614 Make_Simple_Return_Statement (Loc,
9618 Make_Indexed_Component (Loc,
9619 Prefix => New_Reference_To (X, Loc),
9620 Expressions => New_List (New_Reference_To (I, Loc))),
9623 Make_Indexed_Component (Loc,
9624 Prefix => New_Reference_To (Y, Loc),
9625 Expressions => New_List (
9626 New_Reference_To (J, Loc)))))));
9628 -- for I in X'range loop
9633 Make_Implicit_Loop_Statement (Nod,
9634 Identifier => Empty,
9637 Make_Iteration_Scheme (Loc,
9638 Loop_Parameter_Specification =>
9639 Make_Loop_Parameter_Specification (Loc,
9640 Defining_Identifier => I,
9641 Discrete_Subtype_Definition =>
9642 Make_Attribute_Reference (Loc,
9643 Prefix => New_Reference_To (X, Loc),
9644 Attribute_Name => Name_Range))),
9646 Statements => New_List (Loop_Body));
9648 -- if X'length = 0 then
9650 -- elsif Y'length = 0 then
9653 -- for ... loop ... end loop;
9654 -- return X'length > Y'length;
9658 Make_Attribute_Reference (Loc,
9659 Prefix => New_Reference_To (X, Loc),
9660 Attribute_Name => Name_Length);
9663 Make_Attribute_Reference (Loc,
9664 Prefix => New_Reference_To (Y, Loc),
9665 Attribute_Name => Name_Length);
9669 Left_Opnd => Length1,
9670 Right_Opnd => Length2);
9673 Make_Implicit_If_Statement (Nod,
9677 Make_Attribute_Reference (Loc,
9678 Prefix => New_Reference_To (X, Loc),
9679 Attribute_Name => Name_Length),
9681 Make_Integer_Literal (Loc, 0)),
9685 Make_Simple_Return_Statement (Loc,
9686 Expression => New_Reference_To (Standard_False, Loc))),
9688 Elsif_Parts => New_List (
9689 Make_Elsif_Part (Loc,
9693 Make_Attribute_Reference (Loc,
9694 Prefix => New_Reference_To (Y, Loc),
9695 Attribute_Name => Name_Length),
9697 Make_Integer_Literal (Loc, 0)),
9701 Make_Simple_Return_Statement (Loc,
9702 Expression => New_Reference_To (Standard_True, Loc))))),
9704 Else_Statements => New_List (
9706 Make_Simple_Return_Statement (Loc,
9707 Expression => Final_Expr)));
9711 Formals := New_List (
9712 Make_Parameter_Specification (Loc,
9713 Defining_Identifier => X,
9714 Parameter_Type => New_Reference_To (Typ, Loc)),
9716 Make_Parameter_Specification (Loc,
9717 Defining_Identifier => Y,
9718 Parameter_Type => New_Reference_To (Typ, Loc)));
9720 -- function Gnnn (...) return boolean is
9721 -- J : index := Y'first;
9726 Func_Name := Make_Temporary (Loc, 'G');
9729 Make_Subprogram_Body (Loc,
9731 Make_Function_Specification (Loc,
9732 Defining_Unit_Name => Func_Name,
9733 Parameter_Specifications => Formals,
9734 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
9736 Declarations => New_List (
9737 Make_Object_Declaration (Loc,
9738 Defining_Identifier => J,
9739 Object_Definition => New_Reference_To (Index, Loc),
9741 Make_Attribute_Reference (Loc,
9742 Prefix => New_Reference_To (Y, Loc),
9743 Attribute_Name => Name_First))),
9745 Handled_Statement_Sequence =>
9746 Make_Handled_Sequence_Of_Statements (Loc,
9747 Statements => New_List (If_Stat)));
9750 end Make_Array_Comparison_Op;
9752 ---------------------------
9753 -- Make_Boolean_Array_Op --
9754 ---------------------------
9756 -- For logical operations on boolean arrays, expand in line the following,
9757 -- replacing 'and' with 'or' or 'xor' where needed:
9759 -- function Annn (A : typ; B: typ) return typ is
9762 -- for J in A'range loop
9763 -- C (J) := A (J) op B (J);
9768 -- Here typ is the boolean array type
9770 function Make_Boolean_Array_Op
9772 N : Node_Id) return Node_Id
9774 Loc : constant Source_Ptr := Sloc (N);
9776 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
9777 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
9778 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
9779 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
9787 Func_Name : Entity_Id;
9788 Func_Body : Node_Id;
9789 Loop_Statement : Node_Id;
9793 Make_Indexed_Component (Loc,
9794 Prefix => New_Reference_To (A, Loc),
9795 Expressions => New_List (New_Reference_To (J, Loc)));
9798 Make_Indexed_Component (Loc,
9799 Prefix => New_Reference_To (B, Loc),
9800 Expressions => New_List (New_Reference_To (J, Loc)));
9803 Make_Indexed_Component (Loc,
9804 Prefix => New_Reference_To (C, Loc),
9805 Expressions => New_List (New_Reference_To (J, Loc)));
9807 if Nkind (N) = N_Op_And then
9813 elsif Nkind (N) = N_Op_Or then
9827 Make_Implicit_Loop_Statement (N,
9828 Identifier => Empty,
9831 Make_Iteration_Scheme (Loc,
9832 Loop_Parameter_Specification =>
9833 Make_Loop_Parameter_Specification (Loc,
9834 Defining_Identifier => J,
9835 Discrete_Subtype_Definition =>
9836 Make_Attribute_Reference (Loc,
9837 Prefix => New_Reference_To (A, Loc),
9838 Attribute_Name => Name_Range))),
9840 Statements => New_List (
9841 Make_Assignment_Statement (Loc,
9843 Expression => Op)));
9845 Formals := New_List (
9846 Make_Parameter_Specification (Loc,
9847 Defining_Identifier => A,
9848 Parameter_Type => New_Reference_To (Typ, Loc)),
9850 Make_Parameter_Specification (Loc,
9851 Defining_Identifier => B,
9852 Parameter_Type => New_Reference_To (Typ, Loc)));
9854 Func_Name := Make_Temporary (Loc, 'A');
9855 Set_Is_Inlined (Func_Name);
9858 Make_Subprogram_Body (Loc,
9860 Make_Function_Specification (Loc,
9861 Defining_Unit_Name => Func_Name,
9862 Parameter_Specifications => Formals,
9863 Result_Definition => New_Reference_To (Typ, Loc)),
9865 Declarations => New_List (
9866 Make_Object_Declaration (Loc,
9867 Defining_Identifier => C,
9868 Object_Definition => New_Reference_To (Typ, Loc))),
9870 Handled_Statement_Sequence =>
9871 Make_Handled_Sequence_Of_Statements (Loc,
9872 Statements => New_List (
9874 Make_Simple_Return_Statement (Loc,
9875 Expression => New_Reference_To (C, Loc)))));
9878 end Make_Boolean_Array_Op;
9880 ------------------------
9881 -- Rewrite_Comparison --
9882 ------------------------
9884 procedure Rewrite_Comparison (N : Node_Id) is
9885 Warning_Generated : Boolean := False;
9886 -- Set to True if first pass with Assume_Valid generates a warning in
9887 -- which case we skip the second pass to avoid warning overloaded.
9890 -- Set to Standard_True or Standard_False
9893 if Nkind (N) = N_Type_Conversion then
9894 Rewrite_Comparison (Expression (N));
9897 elsif Nkind (N) not in N_Op_Compare then
9901 -- Now start looking at the comparison in detail. We potentially go
9902 -- through this loop twice. The first time, Assume_Valid is set False
9903 -- in the call to Compile_Time_Compare. If this call results in a
9904 -- clear result of always True or Always False, that's decisive and
9905 -- we are done. Otherwise we repeat the processing with Assume_Valid
9906 -- set to True to generate additional warnings. We can skip that step
9907 -- if Constant_Condition_Warnings is False.
9909 for AV in False .. True loop
9911 Typ : constant Entity_Id := Etype (N);
9912 Op1 : constant Node_Id := Left_Opnd (N);
9913 Op2 : constant Node_Id := Right_Opnd (N);
9915 Res : constant Compare_Result :=
9916 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
9917 -- Res indicates if compare outcome can be compile time determined
9919 True_Result : Boolean;
9920 False_Result : Boolean;
9923 case N_Op_Compare (Nkind (N)) is
9925 True_Result := Res = EQ;
9926 False_Result := Res = LT or else Res = GT or else Res = NE;
9929 True_Result := Res in Compare_GE;
9930 False_Result := Res = LT;
9933 and then Constant_Condition_Warnings
9934 and then Comes_From_Source (Original_Node (N))
9935 and then Nkind (Original_Node (N)) = N_Op_Ge
9936 and then not In_Instance
9937 and then Is_Integer_Type (Etype (Left_Opnd (N)))
9938 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
9941 ("can never be greater than, could replace by ""'=""?", N);
9942 Warning_Generated := True;
9946 True_Result := Res = GT;
9947 False_Result := Res in Compare_LE;
9950 True_Result := Res = LT;
9951 False_Result := Res in Compare_GE;
9954 True_Result := Res in Compare_LE;
9955 False_Result := Res = GT;
9958 and then Constant_Condition_Warnings
9959 and then Comes_From_Source (Original_Node (N))
9960 and then Nkind (Original_Node (N)) = N_Op_Le
9961 and then not In_Instance
9962 and then Is_Integer_Type (Etype (Left_Opnd (N)))
9963 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
9966 ("can never be less than, could replace by ""'=""?", N);
9967 Warning_Generated := True;
9971 True_Result := Res = NE or else Res = GT or else Res = LT;
9972 False_Result := Res = EQ;
9975 -- If this is the first iteration, then we actually convert the
9976 -- comparison into True or False, if the result is certain.
9979 if True_Result or False_Result then
9981 Result := Standard_True;
9983 Result := Standard_False;
9988 New_Occurrence_Of (Result, Sloc (N))));
9989 Analyze_And_Resolve (N, Typ);
9990 Warn_On_Known_Condition (N);
9994 -- If this is the second iteration (AV = True), and the original
9995 -- node comes from source and we are not in an instance, then give
9996 -- a warning if we know result would be True or False. Note: we
9997 -- know Constant_Condition_Warnings is set if we get here.
9999 elsif Comes_From_Source (Original_Node (N))
10000 and then not In_Instance
10002 if True_Result then
10004 ("condition can only be False if invalid values present?",
10006 elsif False_Result then
10008 ("condition can only be True if invalid values present?",
10014 -- Skip second iteration if not warning on constant conditions or
10015 -- if the first iteration already generated a warning of some kind or
10016 -- if we are in any case assuming all values are valid (so that the
10017 -- first iteration took care of the valid case).
10019 exit when not Constant_Condition_Warnings;
10020 exit when Warning_Generated;
10021 exit when Assume_No_Invalid_Values;
10023 end Rewrite_Comparison;
10025 ----------------------------
10026 -- Safe_In_Place_Array_Op --
10027 ----------------------------
10029 function Safe_In_Place_Array_Op
10032 Op2 : Node_Id) return Boolean
10034 Target : Entity_Id;
10036 function Is_Safe_Operand (Op : Node_Id) return Boolean;
10037 -- Operand is safe if it cannot overlap part of the target of the
10038 -- operation. If the operand and the target are identical, the operand
10039 -- is safe. The operand can be empty in the case of negation.
10041 function Is_Unaliased (N : Node_Id) return Boolean;
10042 -- Check that N is a stand-alone entity
10048 function Is_Unaliased (N : Node_Id) return Boolean is
10052 and then No (Address_Clause (Entity (N)))
10053 and then No (Renamed_Object (Entity (N)));
10056 ---------------------
10057 -- Is_Safe_Operand --
10058 ---------------------
10060 function Is_Safe_Operand (Op : Node_Id) return Boolean is
10065 elsif Is_Entity_Name (Op) then
10066 return Is_Unaliased (Op);
10068 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
10069 return Is_Unaliased (Prefix (Op));
10071 elsif Nkind (Op) = N_Slice then
10073 Is_Unaliased (Prefix (Op))
10074 and then Entity (Prefix (Op)) /= Target;
10076 elsif Nkind (Op) = N_Op_Not then
10077 return Is_Safe_Operand (Right_Opnd (Op));
10082 end Is_Safe_Operand;
10084 -- Start of processing for Is_Safe_In_Place_Array_Op
10087 -- Skip this processing if the component size is different from system
10088 -- storage unit (since at least for NOT this would cause problems).
10090 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
10093 -- Cannot do in place stuff on VM_Target since cannot pass addresses
10095 elsif VM_Target /= No_VM then
10098 -- Cannot do in place stuff if non-standard Boolean representation
10100 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
10103 elsif not Is_Unaliased (Lhs) then
10107 Target := Entity (Lhs);
10108 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
10110 end Safe_In_Place_Array_Op;
10112 -----------------------
10113 -- Tagged_Membership --
10114 -----------------------
10116 -- There are two different cases to consider depending on whether the right
10117 -- operand is a class-wide type or not. If not we just compare the actual
10118 -- tag of the left expr to the target type tag:
10120 -- Left_Expr.Tag = Right_Type'Tag;
10122 -- If it is a class-wide type we use the RT function CW_Membership which is
10123 -- usually implemented by looking in the ancestor tables contained in the
10124 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
10126 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
10127 -- function IW_Membership which is usually implemented by looking in the
10128 -- table of abstract interface types plus the ancestor table contained in
10129 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
10131 procedure Tagged_Membership
10133 SCIL_Node : out Node_Id;
10134 Result : out Node_Id)
10136 Left : constant Node_Id := Left_Opnd (N);
10137 Right : constant Node_Id := Right_Opnd (N);
10138 Loc : constant Source_Ptr := Sloc (N);
10140 Left_Type : Entity_Id;
10141 New_Node : Node_Id;
10142 Right_Type : Entity_Id;
10146 SCIL_Node := Empty;
10148 -- Handle entities from the limited view
10150 Left_Type := Available_View (Etype (Left));
10151 Right_Type := Available_View (Etype (Right));
10153 if Is_Class_Wide_Type (Left_Type) then
10154 Left_Type := Root_Type (Left_Type);
10158 Make_Selected_Component (Loc,
10159 Prefix => Relocate_Node (Left),
10161 New_Reference_To (First_Tag_Component (Left_Type), Loc));
10163 if Is_Class_Wide_Type (Right_Type) then
10165 -- No need to issue a run-time check if we statically know that the
10166 -- result of this membership test is always true. For example,
10167 -- considering the following declarations:
10169 -- type Iface is interface;
10170 -- type T is tagged null record;
10171 -- type DT is new T and Iface with null record;
10176 -- These membership tests are always true:
10179 -- Obj2 in T'Class;
10180 -- Obj2 in Iface'Class;
10182 -- We do not need to handle cases where the membership is illegal.
10185 -- Obj1 in DT'Class; -- Compile time error
10186 -- Obj1 in Iface'Class; -- Compile time error
10188 if not Is_Class_Wide_Type (Left_Type)
10189 and then (Is_Ancestor (Etype (Right_Type), Left_Type)
10190 or else (Is_Interface (Etype (Right_Type))
10191 and then Interface_Present_In_Ancestor
10193 Iface => Etype (Right_Type))))
10195 Result := New_Reference_To (Standard_True, Loc);
10199 -- Ada 2005 (AI-251): Class-wide applied to interfaces
10201 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
10203 -- Support to: "Iface_CW_Typ in Typ'Class"
10205 or else Is_Interface (Left_Type)
10207 -- Issue error if IW_Membership operation not available in a
10208 -- configurable run time setting.
10210 if not RTE_Available (RE_IW_Membership) then
10212 ("dynamic membership test on interface types", N);
10218 Make_Function_Call (Loc,
10219 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
10220 Parameter_Associations => New_List (
10221 Make_Attribute_Reference (Loc,
10223 Attribute_Name => Name_Address),
10226 (Access_Disp_Table (Root_Type (Right_Type)))),
10229 -- Ada 95: Normal case
10232 Build_CW_Membership (Loc,
10233 Obj_Tag_Node => Obj_Tag,
10237 (Access_Disp_Table (Root_Type (Right_Type)))),
10240 New_Node => New_Node);
10242 -- Generate the SCIL node for this class-wide membership test.
10243 -- Done here because the previous call to Build_CW_Membership
10244 -- relocates Obj_Tag.
10246 if Generate_SCIL then
10247 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
10248 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
10249 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
10252 Result := New_Node;
10255 -- Right_Type is not a class-wide type
10258 -- No need to check the tag of the object if Right_Typ is abstract
10260 if Is_Abstract_Type (Right_Type) then
10261 Result := New_Reference_To (Standard_False, Loc);
10266 Left_Opnd => Obj_Tag,
10269 (Node (First_Elmt (Access_Disp_Table (Right_Type))), Loc));
10272 end Tagged_Membership;
10274 ------------------------------
10275 -- Unary_Op_Validity_Checks --
10276 ------------------------------
10278 procedure Unary_Op_Validity_Checks (N : Node_Id) is
10280 if Validity_Checks_On and Validity_Check_Operands then
10281 Ensure_Valid (Right_Opnd (N));
10283 end Unary_Op_Validity_Checks;