1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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_Intr; use Exp_Intr;
41 with Exp_Pakd; use Exp_Pakd;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Exp_VFpt; use Exp_VFpt;
45 with Freeze; use Freeze;
46 with Inline; use Inline;
48 with Namet; use Namet;
49 with Nlists; use Nlists;
50 with Nmake; use Nmake;
52 with Par_SCO; use Par_SCO;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
57 with Sem_Aux; use Sem_Aux;
58 with Sem_Cat; use Sem_Cat;
59 with Sem_Ch3; use Sem_Ch3;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Res; use Sem_Res;
63 with Sem_Type; use Sem_Type;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn; use Sem_Warn;
66 with Sinfo; use Sinfo;
67 with Snames; use Snames;
68 with Stand; use Stand;
69 with SCIL_LL; use SCIL_LL;
70 with Targparm; use Targparm;
71 with Tbuild; use Tbuild;
72 with Ttypes; use Ttypes;
73 with Uintp; use Uintp;
74 with Urealp; use Urealp;
75 with Validsw; use Validsw;
77 package body Exp_Ch4 is
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 procedure Binary_Op_Validity_Checks (N : Node_Id);
84 pragma Inline (Binary_Op_Validity_Checks);
85 -- Performs validity checks for a binary operator
87 procedure Build_Boolean_Array_Proc_Call
91 -- If a boolean array assignment can be done in place, build call to
92 -- corresponding library procedure.
94 procedure Complete_Controlled_Allocation (Temp_Decl : Node_Id);
95 -- Subsidiary to Expand_N_Allocator and Expand_Allocator_Expression. Formal
96 -- Temp_Decl is the declaration of a temporary which hold the value of the
97 -- original allocator. Create a custom Allocate routine for the expression
98 -- of Temp_Decl. The routine does special processing for anonymous access
101 procedure Displace_Allocator_Pointer (N : Node_Id);
102 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
103 -- Expand_Allocator_Expression. Allocating class-wide interface objects
104 -- this routine displaces the pointer to the allocated object to reference
105 -- the component referencing the corresponding secondary dispatch table.
107 procedure Expand_Allocator_Expression (N : Node_Id);
108 -- Subsidiary to Expand_N_Allocator, for the case when the expression
109 -- is a qualified expression or an aggregate.
111 procedure Expand_Array_Comparison (N : Node_Id);
112 -- This routine handles expansion of the comparison operators (N_Op_Lt,
113 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
114 -- code for these operators is similar, differing only in the details of
115 -- the actual comparison call that is made. Special processing (call a
118 function Expand_Array_Equality
123 Typ : Entity_Id) return Node_Id;
124 -- Expand an array equality into a call to a function implementing this
125 -- equality, and a call to it. Loc is the location for the generated nodes.
126 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
127 -- on which to attach bodies of local functions that are created in the
128 -- process. It is the responsibility of the caller to insert those bodies
129 -- at the right place. Nod provides the Sloc value for the generated code.
130 -- Normally the types used for the generated equality routine are taken
131 -- from Lhs and Rhs. However, in some situations of generated code, the
132 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
133 -- the type to be used for the formal parameters.
135 procedure Expand_Boolean_Operator (N : Node_Id);
136 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
137 -- case of array type arguments.
139 procedure Expand_Short_Circuit_Operator (N : Node_Id);
140 -- Common expansion processing for short-circuit boolean operators
142 function Expand_Composite_Equality
147 Bodies : List_Id) return Node_Id;
148 -- Local recursive function used to expand equality for nested composite
149 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
150 -- to attach bodies of local functions that are created in the process.
151 -- This is the responsibility of the caller to insert those bodies at the
152 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
153 -- are the left and right sides for the comparison, and Typ is the type of
154 -- the arrays to compare.
156 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
157 -- Routine to expand concatenation of a sequence of two or more operands
158 -- (in the list Operands) and replace node Cnode with the result of the
159 -- concatenation. The operands can be of any appropriate type, and can
160 -- include both arrays and singleton elements.
162 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
163 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
164 -- fixed. We do not have such a type at runtime, so the purpose of this
165 -- routine is to find the real type by looking up the tree. We also
166 -- determine if the operation must be rounded.
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 Optimize_Length_Comparison (N : Node_Id);
205 -- Given an expression, if it is of the form X'Length op N (or the other
206 -- way round), where N is known at compile time to be 0 or 1, and X is a
207 -- simple entity, and op is a comparison operator, optimizes it into a
208 -- comparison of First and Last.
210 procedure Rewrite_Comparison (N : Node_Id);
211 -- If N is the node for a comparison whose outcome can be determined at
212 -- compile time, then the node N can be rewritten with True or False. If
213 -- the outcome cannot be determined at compile time, the call has no
214 -- effect. If N is a type conversion, then this processing is applied to
215 -- its expression. If N is neither comparison nor a type conversion, the
216 -- call has no effect.
218 procedure Tagged_Membership
220 SCIL_Node : out Node_Id;
221 Result : out Node_Id);
222 -- Construct the expression corresponding to the tagged membership test.
223 -- Deals with a second operand being (or not) a class-wide type.
225 function Safe_In_Place_Array_Op
228 Op2 : Node_Id) return Boolean;
229 -- In the context of an assignment, where the right-hand side is a boolean
230 -- operation on arrays, check whether operation can be performed in place.
232 procedure Unary_Op_Validity_Checks (N : Node_Id);
233 pragma Inline (Unary_Op_Validity_Checks);
234 -- Performs validity checks for a unary operator
236 -------------------------------
237 -- Binary_Op_Validity_Checks --
238 -------------------------------
240 procedure Binary_Op_Validity_Checks (N : Node_Id) is
242 if Validity_Checks_On and Validity_Check_Operands then
243 Ensure_Valid (Left_Opnd (N));
244 Ensure_Valid (Right_Opnd (N));
246 end Binary_Op_Validity_Checks;
248 ------------------------------------
249 -- Build_Boolean_Array_Proc_Call --
250 ------------------------------------
252 procedure Build_Boolean_Array_Proc_Call
257 Loc : constant Source_Ptr := Sloc (N);
258 Kind : constant Node_Kind := Nkind (Expression (N));
259 Target : constant Node_Id :=
260 Make_Attribute_Reference (Loc,
262 Attribute_Name => Name_Address);
264 Arg1 : Node_Id := Op1;
265 Arg2 : Node_Id := Op2;
267 Proc_Name : Entity_Id;
270 if Kind = N_Op_Not then
271 if Nkind (Op1) in N_Binary_Op then
273 -- Use negated version of the binary operators
275 if Nkind (Op1) = N_Op_And then
276 Proc_Name := RTE (RE_Vector_Nand);
278 elsif Nkind (Op1) = N_Op_Or then
279 Proc_Name := RTE (RE_Vector_Nor);
281 else pragma Assert (Nkind (Op1) = N_Op_Xor);
282 Proc_Name := RTE (RE_Vector_Xor);
286 Make_Procedure_Call_Statement (Loc,
287 Name => New_Occurrence_Of (Proc_Name, Loc),
289 Parameter_Associations => New_List (
291 Make_Attribute_Reference (Loc,
292 Prefix => Left_Opnd (Op1),
293 Attribute_Name => Name_Address),
295 Make_Attribute_Reference (Loc,
296 Prefix => Right_Opnd (Op1),
297 Attribute_Name => Name_Address),
299 Make_Attribute_Reference (Loc,
300 Prefix => Left_Opnd (Op1),
301 Attribute_Name => Name_Length)));
304 Proc_Name := RTE (RE_Vector_Not);
307 Make_Procedure_Call_Statement (Loc,
308 Name => New_Occurrence_Of (Proc_Name, Loc),
309 Parameter_Associations => New_List (
312 Make_Attribute_Reference (Loc,
314 Attribute_Name => Name_Address),
316 Make_Attribute_Reference (Loc,
318 Attribute_Name => Name_Length)));
322 -- We use the following equivalences:
324 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
325 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
326 -- (not X) xor (not Y) = X xor Y
327 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
329 if Nkind (Op1) = N_Op_Not then
330 Arg1 := Right_Opnd (Op1);
331 Arg2 := Right_Opnd (Op2);
332 if Kind = N_Op_And then
333 Proc_Name := RTE (RE_Vector_Nor);
334 elsif Kind = N_Op_Or then
335 Proc_Name := RTE (RE_Vector_Nand);
337 Proc_Name := RTE (RE_Vector_Xor);
341 if Kind = N_Op_And then
342 Proc_Name := RTE (RE_Vector_And);
343 elsif Kind = N_Op_Or then
344 Proc_Name := RTE (RE_Vector_Or);
345 elsif Nkind (Op2) = N_Op_Not then
346 Proc_Name := RTE (RE_Vector_Nxor);
347 Arg2 := Right_Opnd (Op2);
349 Proc_Name := RTE (RE_Vector_Xor);
354 Make_Procedure_Call_Statement (Loc,
355 Name => New_Occurrence_Of (Proc_Name, Loc),
356 Parameter_Associations => New_List (
358 Make_Attribute_Reference (Loc,
360 Attribute_Name => Name_Address),
361 Make_Attribute_Reference (Loc,
363 Attribute_Name => Name_Address),
364 Make_Attribute_Reference (Loc,
366 Attribute_Name => Name_Length)));
369 Rewrite (N, Call_Node);
373 when RE_Not_Available =>
375 end Build_Boolean_Array_Proc_Call;
377 ------------------------------------
378 -- Complete_Controlled_Allocation --
379 ------------------------------------
381 procedure Complete_Controlled_Allocation (Temp_Decl : Node_Id) is
382 pragma Assert (Nkind (Temp_Decl) = N_Object_Declaration);
384 Ptr_Typ : constant Entity_Id := Etype (Defining_Identifier (Temp_Decl));
386 function First_Declaration_Of_Current_Unit return Node_Id;
387 -- Return the current unit's first declaration. If the declaration list
388 -- is empty, the routine generates a null statement and returns it.
390 ---------------------------------------
391 -- First_Declaration_Of_Current_Unit --
392 ---------------------------------------
394 function First_Declaration_Of_Current_Unit return Node_Id is
395 Sem_U : Node_Id := Unit (Cunit (Current_Sem_Unit));
400 if Nkind (Sem_U) = N_Package_Declaration then
401 Sem_U := Specification (Sem_U);
402 Decls := Visible_Declarations (Sem_U);
405 Decl := Make_Null_Statement (Sloc (Sem_U));
406 Decls := New_List (Decl);
407 Set_Visible_Declarations (Sem_U, Decls);
409 Decl := First (Decls);
413 Decls := Declarations (Sem_U);
416 Decl := Make_Null_Statement (Sloc (Sem_U));
417 Decls := New_List (Decl);
418 Set_Declarations (Sem_U, Decls);
420 Decl := First (Decls);
425 end First_Declaration_Of_Current_Unit;
427 -- Start of processing for Complete_Controlled_Allocation
430 -- Certain run-time configurations and targets do not provide support
431 -- for controlled types.
433 if Restriction_Active (No_Finalization) then
436 -- Do nothing if the access type may never allocate an object
438 elsif No_Pool_Assigned (Ptr_Typ) then
441 -- Access-to-controlled types are not supported on .NET/JVM
443 elsif VM_Target /= No_VM then
447 -- Processing for anonymous access-to-controlled types. These access
448 -- types receive a special collection which appears on the declarations
449 -- of the enclosing semantic unit.
451 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
452 and then No (Associated_Collection (Ptr_Typ))
454 (not Restriction_Active (No_Nested_Finalization)
455 or else Is_Library_Level_Entity (Ptr_Typ))
458 Pool_Id : constant Entity_Id := RTE (RE_Global_Pool_Object);
459 Scop : Node_Id := Cunit_Entity (Current_Sem_Unit);
462 -- Use the scope of the current semantic unit when analyzing
464 if Ekind (Scop) = E_Subprogram_Body then
465 Scop := Corresponding_Spec (Parent (Parent (Parent (Scop))));
468 Build_Finalization_Collection
470 Ins_Node => First_Declaration_Of_Current_Unit,
473 -- Decorate the anonymous access type and the allocator node
475 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
476 Set_Storage_Pool (Expression (Temp_Decl), Pool_Id);
480 -- Since the temporary object reuses the original allocator, generate a
481 -- custom Allocate routine for the temporary.
483 if Present (Associated_Collection (Ptr_Typ)) then
484 Build_Allocate_Deallocate_Proc
486 Is_Allocate => True);
488 end Complete_Controlled_Allocation;
490 --------------------------------
491 -- Displace_Allocator_Pointer --
492 --------------------------------
494 procedure Displace_Allocator_Pointer (N : Node_Id) is
495 Loc : constant Source_Ptr := Sloc (N);
496 Orig_Node : constant Node_Id := Original_Node (N);
502 -- Do nothing in case of VM targets: the virtual machine will handle
503 -- interfaces directly.
505 if not Tagged_Type_Expansion then
509 pragma Assert (Nkind (N) = N_Identifier
510 and then Nkind (Orig_Node) = N_Allocator);
512 PtrT := Etype (Orig_Node);
513 Dtyp := Available_View (Designated_Type (PtrT));
514 Etyp := Etype (Expression (Orig_Node));
516 if Is_Class_Wide_Type (Dtyp)
517 and then Is_Interface (Dtyp)
519 -- If the type of the allocator expression is not an interface type
520 -- we can generate code to reference the record component containing
521 -- the pointer to the secondary dispatch table.
523 if not Is_Interface (Etyp) then
525 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
528 -- 1) Get access to the allocated object
531 Make_Explicit_Dereference (Loc,
536 -- 2) Add the conversion to displace the pointer to reference
537 -- the secondary dispatch table.
539 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
540 Analyze_And_Resolve (N, Dtyp);
542 -- 3) The 'access to the secondary dispatch table will be used
543 -- as the value returned by the allocator.
546 Make_Attribute_Reference (Loc,
547 Prefix => Relocate_Node (N),
548 Attribute_Name => Name_Access));
549 Set_Etype (N, Saved_Typ);
553 -- If the type of the allocator expression is an interface type we
554 -- generate a run-time call to displace "this" to reference the
555 -- component containing the pointer to the secondary dispatch table
556 -- or else raise Constraint_Error if the actual object does not
557 -- implement the target interface. This case corresponds with the
558 -- following example:
560 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
562 -- return new Iface_2'Class'(Obj);
567 Unchecked_Convert_To (PtrT,
568 Make_Function_Call (Loc,
569 Name => New_Reference_To (RTE (RE_Displace), Loc),
570 Parameter_Associations => New_List (
571 Unchecked_Convert_To (RTE (RE_Address),
577 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
579 Analyze_And_Resolve (N, PtrT);
582 end Displace_Allocator_Pointer;
584 ---------------------------------
585 -- Expand_Allocator_Expression --
586 ---------------------------------
588 procedure Expand_Allocator_Expression (N : Node_Id) is
589 Loc : constant Source_Ptr := Sloc (N);
590 Exp : constant Node_Id := Expression (Expression (N));
591 PtrT : constant Entity_Id := Etype (N);
592 DesigT : constant Entity_Id := Designated_Type (PtrT);
594 procedure Apply_Accessibility_Check
596 Built_In_Place : Boolean := False);
597 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
598 -- type, generate an accessibility check to verify that the level of the
599 -- type of the created object is not deeper than the level of the access
600 -- type. If the type of the qualified expression is class- wide, then
601 -- always generate the check (except in the case where it is known to be
602 -- unnecessary, see comment below). Otherwise, only generate the check
603 -- if the level of the qualified expression type is statically deeper
604 -- than the access type.
606 -- Although the static accessibility will generally have been performed
607 -- as a legality check, it won't have been done in cases where the
608 -- allocator appears in generic body, so a run-time check is needed in
609 -- general. One special case is when the access type is declared in the
610 -- same scope as the class-wide allocator, in which case the check can
611 -- never fail, so it need not be generated.
613 -- As an open issue, there seem to be cases where the static level
614 -- associated with the class-wide object's underlying type is not
615 -- sufficient to perform the proper accessibility check, such as for
616 -- allocators in nested subprograms or accept statements initialized by
617 -- class-wide formals when the actual originates outside at a deeper
618 -- static level. The nested subprogram case might require passing
619 -- accessibility levels along with class-wide parameters, and the task
620 -- case seems to be an actual gap in the language rules that needs to
621 -- be fixed by the ARG. ???
623 -------------------------------
624 -- Apply_Accessibility_Check --
625 -------------------------------
627 procedure Apply_Accessibility_Check
629 Built_In_Place : Boolean := False)
634 -- Note: we skip the accessibility check for the VM case, since
635 -- there does not seem to be any practical way of implementing it.
637 if Ada_Version >= Ada_2005
638 and then Tagged_Type_Expansion
639 and then Is_Class_Wide_Type (DesigT)
640 and then not Scope_Suppress (Accessibility_Check)
642 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
644 (Is_Class_Wide_Type (Etype (Exp))
645 and then Scope (PtrT) /= Current_Scope))
647 -- If the allocator was built in place Ref is already a reference
648 -- to the access object initialized to the result of the allocator
649 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise
650 -- it is the entity associated with the object containing the
651 -- address of the allocated object.
653 if Built_In_Place then
654 Ref_Node := New_Copy (Ref);
656 Ref_Node := New_Reference_To (Ref, Loc);
660 Make_Raise_Program_Error (Loc,
664 Build_Get_Access_Level (Loc,
665 Make_Attribute_Reference (Loc,
667 Attribute_Name => Name_Tag)),
669 Make_Integer_Literal (Loc, Type_Access_Level (PtrT))),
670 Reason => PE_Accessibility_Check_Failed));
672 end Apply_Accessibility_Check;
676 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
677 Indic : constant Node_Id := Subtype_Mark (Expression (N));
678 T : constant Entity_Id := Entity (Indic);
680 Tag_Assign : Node_Id;
684 TagT : Entity_Id := Empty;
685 -- Type used as source for tag assignment
687 TagR : Node_Id := Empty;
688 -- Target reference for tag assignment
690 -- Start of processing for Expand_Allocator_Expression
693 if Is_Tagged_Type (T)
694 or else Needs_Finalization (T)
696 if Is_CPP_Constructor_Call (Exp) then
699 -- Pnnn : constant ptr_T := new (T);
700 -- Init (Pnnn.all,...);
702 -- Allocate the object without an expression
704 Node := Relocate_Node (N);
705 Set_Expression (Node, New_Reference_To (Etype (Exp), Loc));
707 -- Avoid its expansion to avoid generating a call to the default
712 Temp := Make_Temporary (Loc, 'P', N);
715 Make_Object_Declaration (Loc,
716 Defining_Identifier => Temp,
717 Constant_Present => True,
718 Object_Definition => New_Reference_To (PtrT, Loc),
720 Insert_Action (N, Temp_Decl);
722 Apply_Accessibility_Check (Temp);
724 -- Locate the enclosing list and insert the C++ constructor call
731 while not Is_List_Member (P) loop
735 Insert_List_After_And_Analyze (P,
736 Build_Initialization_Call (Loc,
738 Make_Explicit_Dereference (Loc,
739 Prefix => New_Reference_To (Temp, Loc)),
741 Constructor_Ref => Exp));
744 Rewrite (N, New_Reference_To (Temp, Loc));
745 Analyze_And_Resolve (N, PtrT);
749 -- Ada 2005 (AI-318-02): If the initialization expression is a call
750 -- to a build-in-place function, then access to the allocated object
751 -- must be passed to the function. Currently we limit such functions
752 -- to those with constrained limited result subtypes, but eventually
753 -- we plan to expand the allowed forms of functions that are treated
754 -- as build-in-place.
756 if Ada_Version >= Ada_2005
757 and then Is_Build_In_Place_Function_Call (Exp)
759 Make_Build_In_Place_Call_In_Allocator (N, Exp);
760 Apply_Accessibility_Check (N, Built_In_Place => True);
764 -- Actions inserted before:
765 -- Temp : constant ptr_T := new T'(Expression);
766 -- <no CW> Temp._tag := T'tag;
767 -- <CTRL> Adjust (Finalizable (Temp.all));
768 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
770 -- We analyze by hand the new internal allocator to avoid
771 -- any recursion and inappropriate call to Initialize
773 -- We don't want to remove side effects when the expression must be
774 -- built in place. In the case of a build-in-place function call,
775 -- that could lead to a duplication of the call, which was already
776 -- substituted for the allocator.
778 if not Aggr_In_Place then
779 Remove_Side_Effects (Exp);
782 Temp := Make_Temporary (Loc, 'P', N);
784 -- For a class wide allocation generate the following code:
786 -- type Equiv_Record is record ... end record;
787 -- implicit subtype CW is <Class_Wide_Subytpe>;
788 -- temp : PtrT := new CW'(CW!(expr));
790 if Is_Class_Wide_Type (T) then
791 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
793 -- Ada 2005 (AI-251): If the expression is a class-wide interface
794 -- object we generate code to move up "this" to reference the
795 -- base of the object before allocating the new object.
797 -- Note that Exp'Address is recursively expanded into a call
798 -- to Base_Address (Exp.Tag)
800 if Is_Class_Wide_Type (Etype (Exp))
801 and then Is_Interface (Etype (Exp))
802 and then Tagged_Type_Expansion
806 Unchecked_Convert_To (Entity (Indic),
807 Make_Explicit_Dereference (Loc,
808 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
809 Make_Attribute_Reference (Loc,
811 Attribute_Name => Name_Address)))));
815 Unchecked_Convert_To (Entity (Indic), Exp));
818 Analyze_And_Resolve (Expression (N), Entity (Indic));
821 -- Processing for allocators returning non-interface types
823 if not Is_Interface (Directly_Designated_Type (PtrT)) then
824 if Aggr_In_Place then
826 Make_Object_Declaration (Loc,
827 Defining_Identifier => Temp,
828 Object_Definition => New_Reference_To (PtrT, Loc),
832 New_Reference_To (Etype (Exp), Loc)));
834 -- Copy the Comes_From_Source flag for the allocator we just
835 -- built, since logically this allocator is a replacement of
836 -- the original allocator node. This is for proper handling of
837 -- restriction No_Implicit_Heap_Allocations.
839 Set_Comes_From_Source
840 (Expression (Temp_Decl), Comes_From_Source (N));
842 Set_No_Initialization (Expression (Temp_Decl));
843 Insert_Action (N, Temp_Decl);
845 Complete_Controlled_Allocation (Temp_Decl);
846 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
848 -- Attach the object to the associated finalization collection.
849 -- This is done manually on .NET/JVM since those compilers do
850 -- no support pools and can't benefit from internally generated
851 -- Allocate / Deallocate procedures.
853 if VM_Target /= No_VM
854 and then Is_Controlled (DesigT)
855 and then Present (Associated_Collection (PtrT))
860 New_Reference_To (Temp, Loc),
865 Node := Relocate_Node (N);
869 Make_Object_Declaration (Loc,
870 Defining_Identifier => Temp,
871 Constant_Present => True,
872 Object_Definition => New_Reference_To (PtrT, Loc),
875 Insert_Action (N, Temp_Decl);
876 Complete_Controlled_Allocation (Temp_Decl);
878 -- Attach the object to the associated finalization collection.
879 -- This is done manually on .NET/JVM since those compilers do
880 -- no support pools and can't benefit from internally generated
881 -- Allocate / Deallocate procedures.
883 if VM_Target /= No_VM
884 and then Is_Controlled (DesigT)
885 and then Present (Associated_Collection (PtrT))
890 New_Reference_To (Temp, Loc),
895 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
896 -- interface type. In this case we use the type of the qualified
897 -- expression to allocate the object.
901 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
906 Make_Full_Type_Declaration (Loc,
907 Defining_Identifier => Def_Id,
909 Make_Access_To_Object_Definition (Loc,
911 Null_Exclusion_Present => False,
912 Constant_Present => False,
913 Subtype_Indication =>
914 New_Reference_To (Etype (Exp), Loc)));
916 Insert_Action (N, New_Decl);
918 -- Inherit the allocation-related attributes from the original
921 Set_Associated_Collection (Def_Id,
922 Associated_Collection (PtrT));
924 Set_Associated_Storage_Pool (Def_Id,
925 Associated_Storage_Pool (PtrT));
927 -- Declare the object using the previous type declaration
929 if Aggr_In_Place then
931 Make_Object_Declaration (Loc,
932 Defining_Identifier => Temp,
933 Object_Definition => New_Reference_To (Def_Id, Loc),
936 New_Reference_To (Etype (Exp), Loc)));
938 -- Copy the Comes_From_Source flag for the allocator we just
939 -- built, since logically this allocator is a replacement of
940 -- the original allocator node. This is for proper handling
941 -- of restriction No_Implicit_Heap_Allocations.
943 Set_Comes_From_Source
944 (Expression (Temp_Decl), Comes_From_Source (N));
946 Set_No_Initialization (Expression (Temp_Decl));
947 Insert_Action (N, Temp_Decl);
949 Complete_Controlled_Allocation (Temp_Decl);
950 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
953 Node := Relocate_Node (N);
957 Make_Object_Declaration (Loc,
958 Defining_Identifier => Temp,
959 Constant_Present => True,
960 Object_Definition => New_Reference_To (Def_Id, Loc),
963 Insert_Action (N, Temp_Decl);
964 Complete_Controlled_Allocation (Temp_Decl);
967 -- Generate an additional object containing the address of the
968 -- returned object. The type of this second object declaration
969 -- is the correct type required for the common processing that
970 -- is still performed by this subprogram. The displacement of
971 -- this pointer to reference the component associated with the
972 -- interface type will be done at the end of common processing.
975 Make_Object_Declaration (Loc,
976 Defining_Identifier => Make_Temporary (Loc, 'P'),
977 Object_Definition => New_Reference_To (PtrT, Loc),
979 Unchecked_Convert_To (PtrT,
980 New_Reference_To (Temp, Loc)));
982 Insert_Action (N, New_Decl);
984 Temp_Decl := New_Decl;
985 Temp := Defining_Identifier (New_Decl);
989 Apply_Accessibility_Check (Temp);
991 -- Generate the tag assignment
993 -- Suppress the tag assignment when VM_Target because VM tags are
994 -- represented implicitly in objects.
996 if not Tagged_Type_Expansion then
999 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1000 -- interface objects because in this case the tag does not change.
1002 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
1003 pragma Assert (Is_Class_Wide_Type
1004 (Directly_Designated_Type (Etype (N))));
1007 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
1009 TagR := New_Reference_To (Temp, Loc);
1011 elsif Is_Private_Type (T)
1012 and then Is_Tagged_Type (Underlying_Type (T))
1014 TagT := Underlying_Type (T);
1016 Unchecked_Convert_To (Underlying_Type (T),
1017 Make_Explicit_Dereference (Loc,
1018 Prefix => New_Reference_To (Temp, Loc)));
1021 if Present (TagT) then
1023 Full_T : constant Entity_Id := Underlying_Type (TagT);
1026 Make_Assignment_Statement (Loc,
1028 Make_Selected_Component (Loc,
1031 New_Reference_To (First_Tag_Component (Full_T), Loc)),
1033 Unchecked_Convert_To (RTE (RE_Tag),
1036 (First_Elmt (Access_Disp_Table (Full_T))), Loc)));
1039 -- The previous assignment has to be done in any case
1041 Set_Assignment_OK (Name (Tag_Assign));
1042 Insert_Action (N, Tag_Assign);
1045 if Needs_Finalization (DesigT)
1046 and then Needs_Finalization (T)
1048 -- Generate an Adjust call if the object will be moved. In Ada
1049 -- 2005, the object may be inherently limited, in which case
1050 -- there is no Adjust procedure, and the object is built in
1051 -- place. In Ada 95, the object can be limited but not
1052 -- inherently limited if this allocator came from a return
1053 -- statement (we're allocating the result on the secondary
1054 -- stack). In that case, the object will be moved, so we _do_
1057 if not Aggr_In_Place
1058 and then not Is_Immutably_Limited_Type (T)
1064 -- An unchecked conversion is needed in the classwide
1065 -- case because the designated type can be an ancestor
1066 -- of the subtype mark of the allocator.
1068 Unchecked_Convert_To (T,
1069 Make_Explicit_Dereference (Loc,
1070 Prefix => New_Reference_To (Temp, Loc))),
1075 -- Set_Finalize_Address_Ptr
1076 -- (Collection, <Finalize_Address>'Unrestricted_Access)
1078 -- Since .NET/JVM compilers do not support address arithmetic,
1079 -- this call is skipped. The same is done for CodePeer because
1080 -- Finalize_Address is never generated.
1082 if VM_Target = No_VM
1083 and then not CodePeer_Mode
1084 and then Present (Associated_Collection (PtrT))
1087 Make_Set_Finalize_Address_Ptr_Call
1094 Rewrite (N, New_Reference_To (Temp, Loc));
1095 Analyze_And_Resolve (N, PtrT);
1097 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1098 -- component containing the secondary dispatch table of the interface
1101 if Is_Interface (Directly_Designated_Type (PtrT)) then
1102 Displace_Allocator_Pointer (N);
1105 elsif Aggr_In_Place then
1106 Temp := Make_Temporary (Loc, 'P', N);
1108 Make_Object_Declaration (Loc,
1109 Defining_Identifier => Temp,
1110 Object_Definition => New_Reference_To (PtrT, Loc),
1112 Make_Allocator (Loc,
1113 Expression => New_Reference_To (Etype (Exp), Loc)));
1115 -- Copy the Comes_From_Source flag for the allocator we just built,
1116 -- since logically this allocator is a replacement of the original
1117 -- allocator node. This is for proper handling of restriction
1118 -- No_Implicit_Heap_Allocations.
1120 Set_Comes_From_Source
1121 (Expression (Temp_Decl), Comes_From_Source (N));
1123 Set_No_Initialization (Expression (Temp_Decl));
1124 Insert_Action (N, Temp_Decl);
1126 Complete_Controlled_Allocation (Temp_Decl);
1127 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1129 -- Attach the object to the associated finalization collection. This
1130 -- is done manually on .NET/JVM since those compilers do no support
1131 -- pools and cannot benefit from internally generated Allocate and
1132 -- Deallocate procedures.
1134 if VM_Target /= No_VM
1135 and then Is_Controlled (DesigT)
1136 and then Present (Associated_Collection (PtrT))
1140 (Obj_Ref => New_Reference_To (Temp, Loc),
1144 Rewrite (N, New_Reference_To (Temp, Loc));
1145 Analyze_And_Resolve (N, PtrT);
1147 elsif Is_Access_Type (T)
1148 and then Can_Never_Be_Null (T)
1150 Install_Null_Excluding_Check (Exp);
1152 elsif Is_Access_Type (DesigT)
1153 and then Nkind (Exp) = N_Allocator
1154 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1156 -- Apply constraint to designated subtype indication
1158 Apply_Constraint_Check (Expression (Exp),
1159 Designated_Type (DesigT),
1160 No_Sliding => True);
1162 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1164 -- Propagate constraint_error to enclosing allocator
1166 Rewrite (Exp, New_Copy (Expression (Exp)));
1170 -- type A is access T1;
1171 -- X : A := new T2'(...);
1172 -- T1 and T2 can be different subtypes, and we might need to check
1173 -- both constraints. First check against the type of the qualified
1176 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1178 if Do_Range_Check (Exp) then
1179 Set_Do_Range_Check (Exp, False);
1180 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1183 -- A check is also needed in cases where the designated subtype is
1184 -- constrained and differs from the subtype given in the qualified
1185 -- expression. Note that the check on the qualified expression does
1186 -- not allow sliding, but this check does (a relaxation from Ada 83).
1188 if Is_Constrained (DesigT)
1189 and then not Subtypes_Statically_Match (T, DesigT)
1191 Apply_Constraint_Check
1192 (Exp, DesigT, No_Sliding => False);
1194 if Do_Range_Check (Exp) then
1195 Set_Do_Range_Check (Exp, False);
1196 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1200 -- For an access to unconstrained packed array, GIGI needs to see an
1201 -- expression with a constrained subtype in order to compute the
1202 -- proper size for the allocator.
1204 if Is_Array_Type (T)
1205 and then not Is_Constrained (T)
1206 and then Is_Packed (T)
1209 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1210 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1213 Make_Subtype_Declaration (Loc,
1214 Defining_Identifier => ConstrT,
1215 Subtype_Indication => Make_Subtype_From_Expr (Exp, T)));
1216 Freeze_Itype (ConstrT, Exp);
1217 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1221 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1222 -- to a build-in-place function, then access to the allocated object
1223 -- must be passed to the function. Currently we limit such functions
1224 -- to those with constrained limited result subtypes, but eventually
1225 -- we plan to expand the allowed forms of functions that are treated
1226 -- as build-in-place.
1228 if Ada_Version >= Ada_2005
1229 and then Is_Build_In_Place_Function_Call (Exp)
1231 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1236 when RE_Not_Available =>
1238 end Expand_Allocator_Expression;
1240 -----------------------------
1241 -- Expand_Array_Comparison --
1242 -----------------------------
1244 -- Expansion is only required in the case of array types. For the unpacked
1245 -- case, an appropriate runtime routine is called. For packed cases, and
1246 -- also in some other cases where a runtime routine cannot be called, the
1247 -- form of the expansion is:
1249 -- [body for greater_nn; boolean_expression]
1251 -- The body is built by Make_Array_Comparison_Op, and the form of the
1252 -- Boolean expression depends on the operator involved.
1254 procedure Expand_Array_Comparison (N : Node_Id) is
1255 Loc : constant Source_Ptr := Sloc (N);
1256 Op1 : Node_Id := Left_Opnd (N);
1257 Op2 : Node_Id := Right_Opnd (N);
1258 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1259 Ctyp : constant Entity_Id := Component_Type (Typ1);
1262 Func_Body : Node_Id;
1263 Func_Name : Entity_Id;
1267 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1268 -- True for byte addressable target
1270 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1271 -- Returns True if the length of the given operand is known to be less
1272 -- than 4. Returns False if this length is known to be four or greater
1273 -- or is not known at compile time.
1275 ------------------------
1276 -- Length_Less_Than_4 --
1277 ------------------------
1279 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1280 Otyp : constant Entity_Id := Etype (Opnd);
1283 if Ekind (Otyp) = E_String_Literal_Subtype then
1284 return String_Literal_Length (Otyp) < 4;
1288 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1289 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1290 Hi : constant Node_Id := Type_High_Bound (Ityp);
1295 if Compile_Time_Known_Value (Lo) then
1296 Lov := Expr_Value (Lo);
1301 if Compile_Time_Known_Value (Hi) then
1302 Hiv := Expr_Value (Hi);
1307 return Hiv < Lov + 3;
1310 end Length_Less_Than_4;
1312 -- Start of processing for Expand_Array_Comparison
1315 -- Deal first with unpacked case, where we can call a runtime routine
1316 -- except that we avoid this for targets for which are not addressable
1317 -- by bytes, and for the JVM/CIL, since they do not support direct
1318 -- addressing of array components.
1320 if not Is_Bit_Packed_Array (Typ1)
1321 and then Byte_Addressable
1322 and then VM_Target = No_VM
1324 -- The call we generate is:
1326 -- Compare_Array_xn[_Unaligned]
1327 -- (left'address, right'address, left'length, right'length) <op> 0
1329 -- x = U for unsigned, S for signed
1330 -- n = 8,16,32,64 for component size
1331 -- Add _Unaligned if length < 4 and component size is 8.
1332 -- <op> is the standard comparison operator
1334 if Component_Size (Typ1) = 8 then
1335 if Length_Less_Than_4 (Op1)
1337 Length_Less_Than_4 (Op2)
1339 if Is_Unsigned_Type (Ctyp) then
1340 Comp := RE_Compare_Array_U8_Unaligned;
1342 Comp := RE_Compare_Array_S8_Unaligned;
1346 if Is_Unsigned_Type (Ctyp) then
1347 Comp := RE_Compare_Array_U8;
1349 Comp := RE_Compare_Array_S8;
1353 elsif Component_Size (Typ1) = 16 then
1354 if Is_Unsigned_Type (Ctyp) then
1355 Comp := RE_Compare_Array_U16;
1357 Comp := RE_Compare_Array_S16;
1360 elsif Component_Size (Typ1) = 32 then
1361 if Is_Unsigned_Type (Ctyp) then
1362 Comp := RE_Compare_Array_U32;
1364 Comp := RE_Compare_Array_S32;
1367 else pragma Assert (Component_Size (Typ1) = 64);
1368 if Is_Unsigned_Type (Ctyp) then
1369 Comp := RE_Compare_Array_U64;
1371 Comp := RE_Compare_Array_S64;
1375 Remove_Side_Effects (Op1, Name_Req => True);
1376 Remove_Side_Effects (Op2, Name_Req => True);
1379 Make_Function_Call (Sloc (Op1),
1380 Name => New_Occurrence_Of (RTE (Comp), Loc),
1382 Parameter_Associations => New_List (
1383 Make_Attribute_Reference (Loc,
1384 Prefix => Relocate_Node (Op1),
1385 Attribute_Name => Name_Address),
1387 Make_Attribute_Reference (Loc,
1388 Prefix => Relocate_Node (Op2),
1389 Attribute_Name => Name_Address),
1391 Make_Attribute_Reference (Loc,
1392 Prefix => Relocate_Node (Op1),
1393 Attribute_Name => Name_Length),
1395 Make_Attribute_Reference (Loc,
1396 Prefix => Relocate_Node (Op2),
1397 Attribute_Name => Name_Length))));
1400 Make_Integer_Literal (Sloc (Op2),
1403 Analyze_And_Resolve (Op1, Standard_Integer);
1404 Analyze_And_Resolve (Op2, Standard_Integer);
1408 -- Cases where we cannot make runtime call
1410 -- For (a <= b) we convert to not (a > b)
1412 if Chars (N) = Name_Op_Le then
1418 Right_Opnd => Op2)));
1419 Analyze_And_Resolve (N, Standard_Boolean);
1422 -- For < the Boolean expression is
1423 -- greater__nn (op2, op1)
1425 elsif Chars (N) = Name_Op_Lt then
1426 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1430 Op1 := Right_Opnd (N);
1431 Op2 := Left_Opnd (N);
1433 -- For (a >= b) we convert to not (a < b)
1435 elsif Chars (N) = Name_Op_Ge then
1441 Right_Opnd => Op2)));
1442 Analyze_And_Resolve (N, Standard_Boolean);
1445 -- For > the Boolean expression is
1446 -- greater__nn (op1, op2)
1449 pragma Assert (Chars (N) = Name_Op_Gt);
1450 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1453 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1455 Make_Function_Call (Loc,
1456 Name => New_Reference_To (Func_Name, Loc),
1457 Parameter_Associations => New_List (Op1, Op2));
1459 Insert_Action (N, Func_Body);
1461 Analyze_And_Resolve (N, Standard_Boolean);
1464 when RE_Not_Available =>
1466 end Expand_Array_Comparison;
1468 ---------------------------
1469 -- Expand_Array_Equality --
1470 ---------------------------
1472 -- Expand an equality function for multi-dimensional arrays. Here is an
1473 -- example of such a function for Nb_Dimension = 2
1475 -- function Enn (A : atyp; B : btyp) return boolean is
1477 -- if (A'length (1) = 0 or else A'length (2) = 0)
1479 -- (B'length (1) = 0 or else B'length (2) = 0)
1481 -- return True; -- RM 4.5.2(22)
1484 -- if A'length (1) /= B'length (1)
1486 -- A'length (2) /= B'length (2)
1488 -- return False; -- RM 4.5.2(23)
1492 -- A1 : Index_T1 := A'first (1);
1493 -- B1 : Index_T1 := B'first (1);
1497 -- A2 : Index_T2 := A'first (2);
1498 -- B2 : Index_T2 := B'first (2);
1501 -- if A (A1, A2) /= B (B1, B2) then
1505 -- exit when A2 = A'last (2);
1506 -- A2 := Index_T2'succ (A2);
1507 -- B2 := Index_T2'succ (B2);
1511 -- exit when A1 = A'last (1);
1512 -- A1 := Index_T1'succ (A1);
1513 -- B1 := Index_T1'succ (B1);
1520 -- Note on the formal types used (atyp and btyp). If either of the arrays
1521 -- is of a private type, we use the underlying type, and do an unchecked
1522 -- conversion of the actual. If either of the arrays has a bound depending
1523 -- on a discriminant, then we use the base type since otherwise we have an
1524 -- escaped discriminant in the function.
1526 -- If both arrays are constrained and have the same bounds, we can generate
1527 -- a loop with an explicit iteration scheme using a 'Range attribute over
1530 function Expand_Array_Equality
1535 Typ : Entity_Id) return Node_Id
1537 Loc : constant Source_Ptr := Sloc (Nod);
1538 Decls : constant List_Id := New_List;
1539 Index_List1 : constant List_Id := New_List;
1540 Index_List2 : constant List_Id := New_List;
1544 Func_Name : Entity_Id;
1545 Func_Body : Node_Id;
1547 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1548 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1552 -- The parameter types to be used for the formals
1557 Num : Int) return Node_Id;
1558 -- This builds the attribute reference Arr'Nam (Expr)
1560 function Component_Equality (Typ : Entity_Id) return Node_Id;
1561 -- Create one statement to compare corresponding components, designated
1562 -- by a full set of indexes.
1564 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1565 -- Given one of the arguments, computes the appropriate type to be used
1566 -- for that argument in the corresponding function formal
1568 function Handle_One_Dimension
1570 Index : Node_Id) return Node_Id;
1571 -- This procedure returns the following code
1574 -- Bn : Index_T := B'First (N);
1578 -- exit when An = A'Last (N);
1579 -- An := Index_T'Succ (An)
1580 -- Bn := Index_T'Succ (Bn)
1584 -- If both indexes are constrained and identical, the procedure
1585 -- returns a simpler loop:
1587 -- for An in A'Range (N) loop
1591 -- N is the dimension for which we are generating a loop. Index is the
1592 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1593 -- xxx statement is either the loop or declare for the next dimension
1594 -- or if this is the last dimension the comparison of corresponding
1595 -- components of the arrays.
1597 -- The actual way the code works is to return the comparison of
1598 -- corresponding components for the N+1 call. That's neater!
1600 function Test_Empty_Arrays return Node_Id;
1601 -- This function constructs the test for both arrays being empty
1602 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1604 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1606 function Test_Lengths_Correspond return Node_Id;
1607 -- This function constructs the test for arrays having different lengths
1608 -- in at least one index position, in which case the resulting code is:
1610 -- A'length (1) /= B'length (1)
1612 -- A'length (2) /= B'length (2)
1623 Num : Int) return Node_Id
1627 Make_Attribute_Reference (Loc,
1628 Attribute_Name => Nam,
1629 Prefix => New_Reference_To (Arr, Loc),
1630 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1633 ------------------------
1634 -- Component_Equality --
1635 ------------------------
1637 function Component_Equality (Typ : Entity_Id) return Node_Id is
1642 -- if a(i1...) /= b(j1...) then return false; end if;
1645 Make_Indexed_Component (Loc,
1646 Prefix => Make_Identifier (Loc, Chars (A)),
1647 Expressions => Index_List1);
1650 Make_Indexed_Component (Loc,
1651 Prefix => Make_Identifier (Loc, Chars (B)),
1652 Expressions => Index_List2);
1654 Test := Expand_Composite_Equality
1655 (Nod, Component_Type (Typ), L, R, Decls);
1657 -- If some (sub)component is an unchecked_union, the whole operation
1658 -- will raise program error.
1660 if Nkind (Test) = N_Raise_Program_Error then
1662 -- This node is going to be inserted at a location where a
1663 -- statement is expected: clear its Etype so analysis will set
1664 -- it to the expected Standard_Void_Type.
1666 Set_Etype (Test, Empty);
1671 Make_Implicit_If_Statement (Nod,
1672 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1673 Then_Statements => New_List (
1674 Make_Simple_Return_Statement (Loc,
1675 Expression => New_Occurrence_Of (Standard_False, Loc))));
1677 end Component_Equality;
1683 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1694 T := Underlying_Type (T);
1696 X := First_Index (T);
1697 while Present (X) loop
1698 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1700 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1713 --------------------------
1714 -- Handle_One_Dimension --
1715 ---------------------------
1717 function Handle_One_Dimension
1719 Index : Node_Id) return Node_Id
1721 Need_Separate_Indexes : constant Boolean :=
1723 or else not Is_Constrained (Ltyp);
1724 -- If the index types are identical, and we are working with
1725 -- constrained types, then we can use the same index for both
1728 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1731 Index_T : Entity_Id;
1736 if N > Number_Dimensions (Ltyp) then
1737 return Component_Equality (Ltyp);
1740 -- Case where we generate a loop
1742 Index_T := Base_Type (Etype (Index));
1744 if Need_Separate_Indexes then
1745 Bn := Make_Temporary (Loc, 'B');
1750 Append (New_Reference_To (An, Loc), Index_List1);
1751 Append (New_Reference_To (Bn, Loc), Index_List2);
1753 Stm_List := New_List (
1754 Handle_One_Dimension (N + 1, Next_Index (Index)));
1756 if Need_Separate_Indexes then
1758 -- Generate guard for loop, followed by increments of indexes
1760 Append_To (Stm_List,
1761 Make_Exit_Statement (Loc,
1764 Left_Opnd => New_Reference_To (An, Loc),
1765 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1767 Append_To (Stm_List,
1768 Make_Assignment_Statement (Loc,
1769 Name => New_Reference_To (An, Loc),
1771 Make_Attribute_Reference (Loc,
1772 Prefix => New_Reference_To (Index_T, Loc),
1773 Attribute_Name => Name_Succ,
1774 Expressions => New_List (New_Reference_To (An, Loc)))));
1776 Append_To (Stm_List,
1777 Make_Assignment_Statement (Loc,
1778 Name => New_Reference_To (Bn, Loc),
1780 Make_Attribute_Reference (Loc,
1781 Prefix => New_Reference_To (Index_T, Loc),
1782 Attribute_Name => Name_Succ,
1783 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1786 -- If separate indexes, we need a declare block for An and Bn, and a
1787 -- loop without an iteration scheme.
1789 if Need_Separate_Indexes then
1791 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1794 Make_Block_Statement (Loc,
1795 Declarations => New_List (
1796 Make_Object_Declaration (Loc,
1797 Defining_Identifier => An,
1798 Object_Definition => New_Reference_To (Index_T, Loc),
1799 Expression => Arr_Attr (A, Name_First, N)),
1801 Make_Object_Declaration (Loc,
1802 Defining_Identifier => Bn,
1803 Object_Definition => New_Reference_To (Index_T, Loc),
1804 Expression => Arr_Attr (B, Name_First, N))),
1806 Handled_Statement_Sequence =>
1807 Make_Handled_Sequence_Of_Statements (Loc,
1808 Statements => New_List (Loop_Stm)));
1810 -- If no separate indexes, return loop statement with explicit
1811 -- iteration scheme on its own
1815 Make_Implicit_Loop_Statement (Nod,
1816 Statements => Stm_List,
1818 Make_Iteration_Scheme (Loc,
1819 Loop_Parameter_Specification =>
1820 Make_Loop_Parameter_Specification (Loc,
1821 Defining_Identifier => An,
1822 Discrete_Subtype_Definition =>
1823 Arr_Attr (A, Name_Range, N))));
1826 end Handle_One_Dimension;
1828 -----------------------
1829 -- Test_Empty_Arrays --
1830 -----------------------
1832 function Test_Empty_Arrays return Node_Id is
1842 for J in 1 .. Number_Dimensions (Ltyp) loop
1845 Left_Opnd => Arr_Attr (A, Name_Length, J),
1846 Right_Opnd => Make_Integer_Literal (Loc, 0));
1850 Left_Opnd => Arr_Attr (B, Name_Length, J),
1851 Right_Opnd => Make_Integer_Literal (Loc, 0));
1860 Left_Opnd => Relocate_Node (Alist),
1861 Right_Opnd => Atest);
1865 Left_Opnd => Relocate_Node (Blist),
1866 Right_Opnd => Btest);
1873 Right_Opnd => Blist);
1874 end Test_Empty_Arrays;
1876 -----------------------------
1877 -- Test_Lengths_Correspond --
1878 -----------------------------
1880 function Test_Lengths_Correspond return Node_Id is
1886 for J in 1 .. Number_Dimensions (Ltyp) loop
1889 Left_Opnd => Arr_Attr (A, Name_Length, J),
1890 Right_Opnd => Arr_Attr (B, Name_Length, J));
1897 Left_Opnd => Relocate_Node (Result),
1898 Right_Opnd => Rtest);
1903 end Test_Lengths_Correspond;
1905 -- Start of processing for Expand_Array_Equality
1908 Ltyp := Get_Arg_Type (Lhs);
1909 Rtyp := Get_Arg_Type (Rhs);
1911 -- For now, if the argument types are not the same, go to the base type,
1912 -- since the code assumes that the formals have the same type. This is
1913 -- fixable in future ???
1915 if Ltyp /= Rtyp then
1916 Ltyp := Base_Type (Ltyp);
1917 Rtyp := Base_Type (Rtyp);
1918 pragma Assert (Ltyp = Rtyp);
1921 -- Build list of formals for function
1923 Formals := New_List (
1924 Make_Parameter_Specification (Loc,
1925 Defining_Identifier => A,
1926 Parameter_Type => New_Reference_To (Ltyp, Loc)),
1928 Make_Parameter_Specification (Loc,
1929 Defining_Identifier => B,
1930 Parameter_Type => New_Reference_To (Rtyp, Loc)));
1932 Func_Name := Make_Temporary (Loc, 'E');
1934 -- Build statement sequence for function
1937 Make_Subprogram_Body (Loc,
1939 Make_Function_Specification (Loc,
1940 Defining_Unit_Name => Func_Name,
1941 Parameter_Specifications => Formals,
1942 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
1944 Declarations => Decls,
1946 Handled_Statement_Sequence =>
1947 Make_Handled_Sequence_Of_Statements (Loc,
1948 Statements => New_List (
1950 Make_Implicit_If_Statement (Nod,
1951 Condition => Test_Empty_Arrays,
1952 Then_Statements => New_List (
1953 Make_Simple_Return_Statement (Loc,
1955 New_Occurrence_Of (Standard_True, Loc)))),
1957 Make_Implicit_If_Statement (Nod,
1958 Condition => Test_Lengths_Correspond,
1959 Then_Statements => New_List (
1960 Make_Simple_Return_Statement (Loc,
1962 New_Occurrence_Of (Standard_False, Loc)))),
1964 Handle_One_Dimension (1, First_Index (Ltyp)),
1966 Make_Simple_Return_Statement (Loc,
1967 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1969 Set_Has_Completion (Func_Name, True);
1970 Set_Is_Inlined (Func_Name);
1972 -- If the array type is distinct from the type of the arguments, it
1973 -- is the full view of a private type. Apply an unchecked conversion
1974 -- to insure that analysis of the call succeeds.
1984 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1986 L := OK_Convert_To (Ltyp, Lhs);
1990 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1992 R := OK_Convert_To (Rtyp, Rhs);
1995 Actuals := New_List (L, R);
1998 Append_To (Bodies, Func_Body);
2001 Make_Function_Call (Loc,
2002 Name => New_Reference_To (Func_Name, Loc),
2003 Parameter_Associations => Actuals);
2004 end Expand_Array_Equality;
2006 -----------------------------
2007 -- Expand_Boolean_Operator --
2008 -----------------------------
2010 -- Note that we first get the actual subtypes of the operands, since we
2011 -- always want to deal with types that have bounds.
2013 procedure Expand_Boolean_Operator (N : Node_Id) is
2014 Typ : constant Entity_Id := Etype (N);
2017 -- Special case of bit packed array where both operands are known to be
2018 -- properly aligned. In this case we use an efficient run time routine
2019 -- to carry out the operation (see System.Bit_Ops).
2021 if Is_Bit_Packed_Array (Typ)
2022 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
2023 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
2025 Expand_Packed_Boolean_Operator (N);
2029 -- For the normal non-packed case, the general expansion is to build
2030 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2031 -- and then inserting it into the tree. The original operator node is
2032 -- then rewritten as a call to this function. We also use this in the
2033 -- packed case if either operand is a possibly unaligned object.
2036 Loc : constant Source_Ptr := Sloc (N);
2037 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2038 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2039 Func_Body : Node_Id;
2040 Func_Name : Entity_Id;
2043 Convert_To_Actual_Subtype (L);
2044 Convert_To_Actual_Subtype (R);
2045 Ensure_Defined (Etype (L), N);
2046 Ensure_Defined (Etype (R), N);
2047 Apply_Length_Check (R, Etype (L));
2049 if Nkind (N) = N_Op_Xor then
2050 Silly_Boolean_Array_Xor_Test (N, Etype (L));
2053 if Nkind (Parent (N)) = N_Assignment_Statement
2054 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
2056 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
2058 elsif Nkind (Parent (N)) = N_Op_Not
2059 and then Nkind (N) = N_Op_And
2061 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
2066 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
2067 Func_Name := Defining_Unit_Name (Specification (Func_Body));
2068 Insert_Action (N, Func_Body);
2070 -- Now rewrite the expression with a call
2073 Make_Function_Call (Loc,
2074 Name => New_Reference_To (Func_Name, Loc),
2075 Parameter_Associations =>
2078 Make_Type_Conversion
2079 (Loc, New_Reference_To (Etype (L), Loc), R))));
2081 Analyze_And_Resolve (N, Typ);
2084 end Expand_Boolean_Operator;
2086 -------------------------------
2087 -- Expand_Composite_Equality --
2088 -------------------------------
2090 -- This function is only called for comparing internal fields of composite
2091 -- types when these fields are themselves composites. This is a special
2092 -- case because it is not possible to respect normal Ada visibility rules.
2094 function Expand_Composite_Equality
2099 Bodies : List_Id) return Node_Id
2101 Loc : constant Source_Ptr := Sloc (Nod);
2102 Full_Type : Entity_Id;
2106 function Find_Primitive_Eq return Node_Id;
2107 -- AI05-0123: Locate primitive equality for type if it exists, and
2108 -- build the corresponding call. If operation is abstract, replace
2109 -- call with an explicit raise. Return Empty if there is no primitive.
2111 -----------------------
2112 -- Find_Primitive_Eq --
2113 -----------------------
2115 function Find_Primitive_Eq return Node_Id is
2120 Prim_E := First_Elmt (Collect_Primitive_Operations (Typ));
2121 while Present (Prim_E) loop
2122 Prim := Node (Prim_E);
2124 -- Locate primitive equality with the right signature
2126 if Chars (Prim) = Name_Op_Eq
2127 and then Etype (First_Formal (Prim)) =
2128 Etype (Next_Formal (First_Formal (Prim)))
2129 and then Etype (Prim) = Standard_Boolean
2131 if Is_Abstract_Subprogram (Prim) then
2133 Make_Raise_Program_Error (Loc,
2134 Reason => PE_Explicit_Raise);
2138 Make_Function_Call (Loc,
2139 Name => New_Reference_To (Prim, Loc),
2140 Parameter_Associations => New_List (Lhs, Rhs));
2147 -- If not found, predefined operation will be used
2150 end Find_Primitive_Eq;
2152 -- Start of processing for Expand_Composite_Equality
2155 if Is_Private_Type (Typ) then
2156 Full_Type := Underlying_Type (Typ);
2161 -- Defense against malformed private types with no completion the error
2162 -- will be diagnosed later by check_completion
2164 if No (Full_Type) then
2165 return New_Reference_To (Standard_False, Loc);
2168 Full_Type := Base_Type (Full_Type);
2170 if Is_Array_Type (Full_Type) then
2172 -- If the operand is an elementary type other than a floating-point
2173 -- type, then we can simply use the built-in block bitwise equality,
2174 -- since the predefined equality operators always apply and bitwise
2175 -- equality is fine for all these cases.
2177 if Is_Elementary_Type (Component_Type (Full_Type))
2178 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2180 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2182 -- For composite component types, and floating-point types, use the
2183 -- expansion. This deals with tagged component types (where we use
2184 -- the applicable equality routine) and floating-point, (where we
2185 -- need to worry about negative zeroes), and also the case of any
2186 -- composite type recursively containing such fields.
2189 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2192 elsif Is_Tagged_Type (Full_Type) then
2194 -- Call the primitive operation "=" of this type
2196 if Is_Class_Wide_Type (Full_Type) then
2197 Full_Type := Root_Type (Full_Type);
2200 -- If this is derived from an untagged private type completed with a
2201 -- tagged type, it does not have a full view, so we use the primitive
2202 -- operations of the private type. This check should no longer be
2203 -- necessary when these types receive their full views ???
2205 if Is_Private_Type (Typ)
2206 and then not Is_Tagged_Type (Typ)
2207 and then not Is_Controlled (Typ)
2208 and then Is_Derived_Type (Typ)
2209 and then No (Full_View (Typ))
2211 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2213 Prim := First_Elmt (Primitive_Operations (Full_Type));
2217 Eq_Op := Node (Prim);
2218 exit when Chars (Eq_Op) = Name_Op_Eq
2219 and then Etype (First_Formal (Eq_Op)) =
2220 Etype (Next_Formal (First_Formal (Eq_Op)))
2221 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2223 pragma Assert (Present (Prim));
2226 Eq_Op := Node (Prim);
2229 Make_Function_Call (Loc,
2230 Name => New_Reference_To (Eq_Op, Loc),
2231 Parameter_Associations =>
2233 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2234 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2236 elsif Is_Record_Type (Full_Type) then
2237 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2239 if Present (Eq_Op) then
2240 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2242 -- Inherited equality from parent type. Convert the actuals to
2243 -- match signature of operation.
2246 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2250 Make_Function_Call (Loc,
2251 Name => New_Reference_To (Eq_Op, Loc),
2252 Parameter_Associations => New_List (
2253 OK_Convert_To (T, Lhs),
2254 OK_Convert_To (T, Rhs)));
2258 -- Comparison between Unchecked_Union components
2260 if Is_Unchecked_Union (Full_Type) then
2262 Lhs_Type : Node_Id := Full_Type;
2263 Rhs_Type : Node_Id := Full_Type;
2264 Lhs_Discr_Val : Node_Id;
2265 Rhs_Discr_Val : Node_Id;
2270 if Nkind (Lhs) = N_Selected_Component then
2271 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2276 if Nkind (Rhs) = N_Selected_Component then
2277 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2280 -- Lhs of the composite equality
2282 if Is_Constrained (Lhs_Type) then
2284 -- Since the enclosing record type can never be an
2285 -- Unchecked_Union (this code is executed for records
2286 -- that do not have variants), we may reference its
2289 if Nkind (Lhs) = N_Selected_Component
2290 and then Has_Per_Object_Constraint (
2291 Entity (Selector_Name (Lhs)))
2294 Make_Selected_Component (Loc,
2295 Prefix => Prefix (Lhs),
2298 (Get_Discriminant_Value
2299 (First_Discriminant (Lhs_Type),
2301 Stored_Constraint (Lhs_Type))));
2306 (Get_Discriminant_Value
2307 (First_Discriminant (Lhs_Type),
2309 Stored_Constraint (Lhs_Type)));
2313 -- It is not possible to infer the discriminant since
2314 -- the subtype is not constrained.
2317 Make_Raise_Program_Error (Loc,
2318 Reason => PE_Unchecked_Union_Restriction);
2321 -- Rhs of the composite equality
2323 if Is_Constrained (Rhs_Type) then
2324 if Nkind (Rhs) = N_Selected_Component
2325 and then Has_Per_Object_Constraint
2326 (Entity (Selector_Name (Rhs)))
2329 Make_Selected_Component (Loc,
2330 Prefix => Prefix (Rhs),
2333 (Get_Discriminant_Value
2334 (First_Discriminant (Rhs_Type),
2336 Stored_Constraint (Rhs_Type))));
2341 (Get_Discriminant_Value
2342 (First_Discriminant (Rhs_Type),
2344 Stored_Constraint (Rhs_Type)));
2349 Make_Raise_Program_Error (Loc,
2350 Reason => PE_Unchecked_Union_Restriction);
2353 -- Call the TSS equality function with the inferred
2354 -- discriminant values.
2357 Make_Function_Call (Loc,
2358 Name => New_Reference_To (Eq_Op, Loc),
2359 Parameter_Associations => New_List (
2368 Make_Function_Call (Loc,
2369 Name => New_Reference_To (Eq_Op, Loc),
2370 Parameter_Associations => New_List (Lhs, Rhs));
2374 elsif Ada_Version >= Ada_2012 then
2376 -- if no TSS has been created for the type, check whether there is
2377 -- a primitive equality declared for it.
2380 Ada_2012_Op : constant Node_Id := Find_Primitive_Eq;
2383 if Present (Ada_2012_Op) then
2387 -- Use predefined equality if no user-defined primitive exists
2389 return Make_Op_Eq (Loc, Lhs, Rhs);
2394 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2398 -- If not array or record type, it is predefined equality.
2400 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2402 end Expand_Composite_Equality;
2404 ------------------------
2405 -- Expand_Concatenate --
2406 ------------------------
2408 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2409 Loc : constant Source_Ptr := Sloc (Cnode);
2411 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2412 -- Result type of concatenation
2414 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2415 -- Component type. Elements of this component type can appear as one
2416 -- of the operands of concatenation as well as arrays.
2418 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2421 Ityp : constant Entity_Id := Base_Type (Istyp);
2422 -- Index type. This is the base type of the index subtype, and is used
2423 -- for all computed bounds (which may be out of range of Istyp in the
2424 -- case of null ranges).
2427 -- This is the type we use to do arithmetic to compute the bounds and
2428 -- lengths of operands. The choice of this type is a little subtle and
2429 -- is discussed in a separate section at the start of the body code.
2431 Concatenation_Error : exception;
2432 -- Raised if concatenation is sure to raise a CE
2434 Result_May_Be_Null : Boolean := True;
2435 -- Reset to False if at least one operand is encountered which is known
2436 -- at compile time to be non-null. Used for handling the special case
2437 -- of setting the high bound to the last operand high bound for a null
2438 -- result, thus ensuring a proper high bound in the super-flat case.
2440 N : constant Nat := List_Length (Opnds);
2441 -- Number of concatenation operands including possibly null operands
2444 -- Number of operands excluding any known to be null, except that the
2445 -- last operand is always retained, in case it provides the bounds for
2449 -- Current operand being processed in the loop through operands. After
2450 -- this loop is complete, always contains the last operand (which is not
2451 -- the same as Operands (NN), since null operands are skipped).
2453 -- Arrays describing the operands, only the first NN entries of each
2454 -- array are set (NN < N when we exclude known null operands).
2456 Is_Fixed_Length : array (1 .. N) of Boolean;
2457 -- True if length of corresponding operand known at compile time
2459 Operands : array (1 .. N) of Node_Id;
2460 -- Set to the corresponding entry in the Opnds list (but note that null
2461 -- operands are excluded, so not all entries in the list are stored).
2463 Fixed_Length : array (1 .. N) of Uint;
2464 -- Set to length of operand. Entries in this array are set only if the
2465 -- corresponding entry in Is_Fixed_Length is True.
2467 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2468 -- Set to lower bound of operand. Either an integer literal in the case
2469 -- where the bound is known at compile time, else actual lower bound.
2470 -- The operand low bound is of type Ityp.
2472 Var_Length : array (1 .. N) of Entity_Id;
2473 -- Set to an entity of type Natural that contains the length of an
2474 -- operand whose length is not known at compile time. Entries in this
2475 -- array are set only if the corresponding entry in Is_Fixed_Length
2476 -- is False. The entity is of type Artyp.
2478 Aggr_Length : array (0 .. N) of Node_Id;
2479 -- The J'th entry in an expression node that represents the total length
2480 -- of operands 1 through J. It is either an integer literal node, or a
2481 -- reference to a constant entity with the right value, so it is fine
2482 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2483 -- entry always is set to zero. The length is of type Artyp.
2485 Low_Bound : Node_Id;
2486 -- A tree node representing the low bound of the result (of type Ityp).
2487 -- This is either an integer literal node, or an identifier reference to
2488 -- a constant entity initialized to the appropriate value.
2490 Last_Opnd_High_Bound : Node_Id;
2491 -- A tree node representing the high bound of the last operand. This
2492 -- need only be set if the result could be null. It is used for the
2493 -- special case of setting the right high bound for a null result.
2494 -- This is of type Ityp.
2496 High_Bound : Node_Id;
2497 -- A tree node representing the high bound of the result (of type Ityp)
2500 -- Result of the concatenation (of type Ityp)
2502 Actions : constant List_Id := New_List;
2503 -- Collect actions to be inserted if Save_Space is False
2505 Save_Space : Boolean;
2506 pragma Warnings (Off, Save_Space);
2507 -- Set to True if we are saving generated code space by calling routines
2508 -- in packages System.Concat_n.
2510 Known_Non_Null_Operand_Seen : Boolean;
2511 -- Set True during generation of the assignments of operands into
2512 -- result once an operand known to be non-null has been seen.
2514 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2515 -- This function makes an N_Integer_Literal node that is returned in
2516 -- analyzed form with the type set to Artyp. Importantly this literal
2517 -- is not flagged as static, so that if we do computations with it that
2518 -- result in statically detected out of range conditions, we will not
2519 -- generate error messages but instead warning messages.
2521 function To_Artyp (X : Node_Id) return Node_Id;
2522 -- Given a node of type Ityp, returns the corresponding value of type
2523 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2524 -- For enum types, the Pos of the value is returned.
2526 function To_Ityp (X : Node_Id) return Node_Id;
2527 -- The inverse function (uses Val in the case of enumeration types)
2529 ------------------------
2530 -- Make_Artyp_Literal --
2531 ------------------------
2533 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2534 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2536 Set_Etype (Result, Artyp);
2537 Set_Analyzed (Result, True);
2538 Set_Is_Static_Expression (Result, False);
2540 end Make_Artyp_Literal;
2546 function To_Artyp (X : Node_Id) return Node_Id is
2548 if Ityp = Base_Type (Artyp) then
2551 elsif Is_Enumeration_Type (Ityp) then
2553 Make_Attribute_Reference (Loc,
2554 Prefix => New_Occurrence_Of (Ityp, Loc),
2555 Attribute_Name => Name_Pos,
2556 Expressions => New_List (X));
2559 return Convert_To (Artyp, X);
2567 function To_Ityp (X : Node_Id) return Node_Id is
2569 if Is_Enumeration_Type (Ityp) then
2571 Make_Attribute_Reference (Loc,
2572 Prefix => New_Occurrence_Of (Ityp, Loc),
2573 Attribute_Name => Name_Val,
2574 Expressions => New_List (X));
2576 -- Case where we will do a type conversion
2579 if Ityp = Base_Type (Artyp) then
2582 return Convert_To (Ityp, X);
2587 -- Local Declarations
2589 Opnd_Typ : Entity_Id;
2597 -- Choose an appropriate computational type
2599 -- We will be doing calculations of lengths and bounds in this routine
2600 -- and computing one from the other in some cases, e.g. getting the high
2601 -- bound by adding the length-1 to the low bound.
2603 -- We can't just use the index type, or even its base type for this
2604 -- purpose for two reasons. First it might be an enumeration type which
2605 -- is not suitable for computations of any kind, and second it may
2606 -- simply not have enough range. For example if the index type is
2607 -- -128..+127 then lengths can be up to 256, which is out of range of
2610 -- For enumeration types, we can simply use Standard_Integer, this is
2611 -- sufficient since the actual number of enumeration literals cannot
2612 -- possibly exceed the range of integer (remember we will be doing the
2613 -- arithmetic with POS values, not representation values).
2615 if Is_Enumeration_Type (Ityp) then
2616 Artyp := Standard_Integer;
2618 -- If index type is Positive, we use the standard unsigned type, to give
2619 -- more room on the top of the range, obviating the need for an overflow
2620 -- check when creating the upper bound. This is needed to avoid junk
2621 -- overflow checks in the common case of String types.
2623 -- ??? Disabled for now
2625 -- elsif Istyp = Standard_Positive then
2626 -- Artyp := Standard_Unsigned;
2628 -- For modular types, we use a 32-bit modular type for types whose size
2629 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2630 -- identity type, and for larger unsigned types we use 64-bits.
2632 elsif Is_Modular_Integer_Type (Ityp) then
2633 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
2634 Artyp := Standard_Unsigned;
2635 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
2638 Artyp := RTE (RE_Long_Long_Unsigned);
2641 -- Similar treatment for signed types
2644 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
2645 Artyp := Standard_Integer;
2646 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
2649 Artyp := Standard_Long_Long_Integer;
2653 -- Supply dummy entry at start of length array
2655 Aggr_Length (0) := Make_Artyp_Literal (0);
2657 -- Go through operands setting up the above arrays
2661 Opnd := Remove_Head (Opnds);
2662 Opnd_Typ := Etype (Opnd);
2664 -- The parent got messed up when we put the operands in a list,
2665 -- so now put back the proper parent for the saved operand, that
2666 -- is to say the concatenation node, to make sure that each operand
2667 -- is seen as a subexpression, e.g. if actions must be inserted.
2669 Set_Parent (Opnd, Cnode);
2671 -- Set will be True when we have setup one entry in the array
2675 -- Singleton element (or character literal) case
2677 if Base_Type (Opnd_Typ) = Ctyp then
2679 Operands (NN) := Opnd;
2680 Is_Fixed_Length (NN) := True;
2681 Fixed_Length (NN) := Uint_1;
2682 Result_May_Be_Null := False;
2684 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2685 -- since we know that the result cannot be null).
2687 Opnd_Low_Bound (NN) :=
2688 Make_Attribute_Reference (Loc,
2689 Prefix => New_Reference_To (Istyp, Loc),
2690 Attribute_Name => Name_First);
2694 -- String literal case (can only occur for strings of course)
2696 elsif Nkind (Opnd) = N_String_Literal then
2697 Len := String_Literal_Length (Opnd_Typ);
2700 Result_May_Be_Null := False;
2703 -- Capture last operand high bound if result could be null
2705 if J = N and then Result_May_Be_Null then
2706 Last_Opnd_High_Bound :=
2709 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
2710 Right_Opnd => Make_Integer_Literal (Loc, 1));
2713 -- Skip null string literal
2715 if J < N and then Len = 0 then
2720 Operands (NN) := Opnd;
2721 Is_Fixed_Length (NN) := True;
2723 -- Set length and bounds
2725 Fixed_Length (NN) := Len;
2727 Opnd_Low_Bound (NN) :=
2728 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
2735 -- Check constrained case with known bounds
2737 if Is_Constrained (Opnd_Typ) then
2739 Index : constant Node_Id := First_Index (Opnd_Typ);
2740 Indx_Typ : constant Entity_Id := Etype (Index);
2741 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
2742 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
2745 -- Fixed length constrained array type with known at compile
2746 -- time bounds is last case of fixed length operand.
2748 if Compile_Time_Known_Value (Lo)
2750 Compile_Time_Known_Value (Hi)
2753 Loval : constant Uint := Expr_Value (Lo);
2754 Hival : constant Uint := Expr_Value (Hi);
2755 Len : constant Uint :=
2756 UI_Max (Hival - Loval + 1, Uint_0);
2760 Result_May_Be_Null := False;
2763 -- Capture last operand bound if result could be null
2765 if J = N and then Result_May_Be_Null then
2766 Last_Opnd_High_Bound :=
2768 Make_Integer_Literal (Loc, Expr_Value (Hi)));
2771 -- Exclude null length case unless last operand
2773 if J < N and then Len = 0 then
2778 Operands (NN) := Opnd;
2779 Is_Fixed_Length (NN) := True;
2780 Fixed_Length (NN) := Len;
2782 Opnd_Low_Bound (NN) :=
2784 (Make_Integer_Literal (Loc, Expr_Value (Lo)));
2791 -- All cases where the length is not known at compile time, or the
2792 -- special case of an operand which is known to be null but has a
2793 -- lower bound other than 1 or is other than a string type.
2798 -- Capture operand bounds
2800 Opnd_Low_Bound (NN) :=
2801 Make_Attribute_Reference (Loc,
2803 Duplicate_Subexpr (Opnd, Name_Req => True),
2804 Attribute_Name => Name_First);
2806 if J = N and Result_May_Be_Null then
2807 Last_Opnd_High_Bound :=
2809 Make_Attribute_Reference (Loc,
2811 Duplicate_Subexpr (Opnd, Name_Req => True),
2812 Attribute_Name => Name_Last));
2815 -- Capture length of operand in entity
2817 Operands (NN) := Opnd;
2818 Is_Fixed_Length (NN) := False;
2820 Var_Length (NN) := Make_Temporary (Loc, 'L');
2823 Make_Object_Declaration (Loc,
2824 Defining_Identifier => Var_Length (NN),
2825 Constant_Present => True,
2826 Object_Definition => New_Occurrence_Of (Artyp, Loc),
2828 Make_Attribute_Reference (Loc,
2830 Duplicate_Subexpr (Opnd, Name_Req => True),
2831 Attribute_Name => Name_Length)));
2835 -- Set next entry in aggregate length array
2837 -- For first entry, make either integer literal for fixed length
2838 -- or a reference to the saved length for variable length.
2841 if Is_Fixed_Length (1) then
2842 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
2844 Aggr_Length (1) := New_Reference_To (Var_Length (1), Loc);
2847 -- If entry is fixed length and only fixed lengths so far, make
2848 -- appropriate new integer literal adding new length.
2850 elsif Is_Fixed_Length (NN)
2851 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
2854 Make_Integer_Literal (Loc,
2855 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
2857 -- All other cases, construct an addition node for the length and
2858 -- create an entity initialized to this length.
2861 Ent := Make_Temporary (Loc, 'L');
2863 if Is_Fixed_Length (NN) then
2864 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
2866 Clen := New_Reference_To (Var_Length (NN), Loc);
2870 Make_Object_Declaration (Loc,
2871 Defining_Identifier => Ent,
2872 Constant_Present => True,
2873 Object_Definition => New_Occurrence_Of (Artyp, Loc),
2876 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
2877 Right_Opnd => Clen)));
2879 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
2886 -- If we have only skipped null operands, return the last operand
2893 -- If we have only one non-null operand, return it and we are done.
2894 -- There is one case in which this cannot be done, and that is when
2895 -- the sole operand is of the element type, in which case it must be
2896 -- converted to an array, and the easiest way of doing that is to go
2897 -- through the normal general circuit.
2900 and then Base_Type (Etype (Operands (1))) /= Ctyp
2902 Result := Operands (1);
2906 -- Cases where we have a real concatenation
2908 -- Next step is to find the low bound for the result array that we
2909 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
2911 -- If the ultimate ancestor of the index subtype is a constrained array
2912 -- definition, then the lower bound is that of the index subtype as
2913 -- specified by (RM 4.5.3(6)).
2915 -- The right test here is to go to the root type, and then the ultimate
2916 -- ancestor is the first subtype of this root type.
2918 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
2920 Make_Attribute_Reference (Loc,
2922 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
2923 Attribute_Name => Name_First);
2925 -- If the first operand in the list has known length we know that
2926 -- the lower bound of the result is the lower bound of this operand.
2928 elsif Is_Fixed_Length (1) then
2929 Low_Bound := Opnd_Low_Bound (1);
2931 -- OK, we don't know the lower bound, we have to build a horrible
2932 -- expression actions node of the form
2934 -- if Cond1'Length /= 0 then
2937 -- if Opnd2'Length /= 0 then
2942 -- The nesting ends either when we hit an operand whose length is known
2943 -- at compile time, or on reaching the last operand, whose low bound we
2944 -- take unconditionally whether or not it is null. It's easiest to do
2945 -- this with a recursive procedure:
2949 function Get_Known_Bound (J : Nat) return Node_Id;
2950 -- Returns the lower bound determined by operands J .. NN
2952 ---------------------
2953 -- Get_Known_Bound --
2954 ---------------------
2956 function Get_Known_Bound (J : Nat) return Node_Id is
2958 if Is_Fixed_Length (J) or else J = NN then
2959 return New_Copy (Opnd_Low_Bound (J));
2963 Make_Conditional_Expression (Loc,
2964 Expressions => New_List (
2967 Left_Opnd => New_Reference_To (Var_Length (J), Loc),
2968 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2970 New_Copy (Opnd_Low_Bound (J)),
2971 Get_Known_Bound (J + 1)));
2973 end Get_Known_Bound;
2976 Ent := Make_Temporary (Loc, 'L');
2979 Make_Object_Declaration (Loc,
2980 Defining_Identifier => Ent,
2981 Constant_Present => True,
2982 Object_Definition => New_Occurrence_Of (Ityp, Loc),
2983 Expression => Get_Known_Bound (1)));
2985 Low_Bound := New_Reference_To (Ent, Loc);
2989 -- Now we can safely compute the upper bound, normally
2990 -- Low_Bound + Length - 1.
2995 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2997 Make_Op_Subtract (Loc,
2998 Left_Opnd => New_Copy (Aggr_Length (NN)),
2999 Right_Opnd => Make_Artyp_Literal (1))));
3001 -- Note that calculation of the high bound may cause overflow in some
3002 -- very weird cases, so in the general case we need an overflow check on
3003 -- the high bound. We can avoid this for the common case of string types
3004 -- and other types whose index is Positive, since we chose a wider range
3005 -- for the arithmetic type.
3007 if Istyp /= Standard_Positive then
3008 Activate_Overflow_Check (High_Bound);
3011 -- Handle the exceptional case where the result is null, in which case
3012 -- case the bounds come from the last operand (so that we get the proper
3013 -- bounds if the last operand is super-flat).
3015 if Result_May_Be_Null then
3017 Make_Conditional_Expression (Loc,
3018 Expressions => New_List (
3020 Left_Opnd => New_Copy (Aggr_Length (NN)),
3021 Right_Opnd => Make_Artyp_Literal (0)),
3022 Last_Opnd_High_Bound,
3026 -- Here is where we insert the saved up actions
3028 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3030 -- Now we construct an array object with appropriate bounds. We mark
3031 -- the target as internal to prevent useless initialization when
3032 -- Initialize_Scalars is enabled. Also since this is the actual result
3033 -- entity, we make sure we have debug information for the result.
3035 Ent := Make_Temporary (Loc, 'S');
3036 Set_Is_Internal (Ent);
3037 Set_Needs_Debug_Info (Ent);
3039 -- If the bound is statically known to be out of range, we do not want
3040 -- to abort, we want a warning and a runtime constraint error. Note that
3041 -- we have arranged that the result will not be treated as a static
3042 -- constant, so we won't get an illegality during this insertion.
3044 Insert_Action (Cnode,
3045 Make_Object_Declaration (Loc,
3046 Defining_Identifier => Ent,
3047 Object_Definition =>
3048 Make_Subtype_Indication (Loc,
3049 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
3051 Make_Index_Or_Discriminant_Constraint (Loc,
3052 Constraints => New_List (
3054 Low_Bound => Low_Bound,
3055 High_Bound => High_Bound))))),
3056 Suppress => All_Checks);
3058 -- If the result of the concatenation appears as the initializing
3059 -- expression of an object declaration, we can just rename the
3060 -- result, rather than copying it.
3062 Set_OK_To_Rename (Ent);
3064 -- Catch the static out of range case now
3066 if Raises_Constraint_Error (High_Bound) then
3067 raise Concatenation_Error;
3070 -- Now we will generate the assignments to do the actual concatenation
3072 -- There is one case in which we will not do this, namely when all the
3073 -- following conditions are met:
3075 -- The result type is Standard.String
3077 -- There are nine or fewer retained (non-null) operands
3079 -- The optimization level is -O0
3081 -- The corresponding System.Concat_n.Str_Concat_n routine is
3082 -- available in the run time.
3084 -- The debug flag gnatd.c is not set
3086 -- If all these conditions are met then we generate a call to the
3087 -- relevant concatenation routine. The purpose of this is to avoid
3088 -- undesirable code bloat at -O0.
3090 if Atyp = Standard_String
3091 and then NN in 2 .. 9
3092 and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
3093 and then not Debug_Flag_Dot_C
3096 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3107 if RTE_Available (RR (NN)) then
3109 Opnds : constant List_Id :=
3110 New_List (New_Occurrence_Of (Ent, Loc));
3113 for J in 1 .. NN loop
3114 if Is_List_Member (Operands (J)) then
3115 Remove (Operands (J));
3118 if Base_Type (Etype (Operands (J))) = Ctyp then
3120 Make_Aggregate (Loc,
3121 Component_Associations => New_List (
3122 Make_Component_Association (Loc,
3123 Choices => New_List (
3124 Make_Integer_Literal (Loc, 1)),
3125 Expression => Operands (J)))));
3128 Append_To (Opnds, Operands (J));
3132 Insert_Action (Cnode,
3133 Make_Procedure_Call_Statement (Loc,
3134 Name => New_Reference_To (RTE (RR (NN)), Loc),
3135 Parameter_Associations => Opnds));
3137 Result := New_Reference_To (Ent, Loc);
3144 -- Not special case so generate the assignments
3146 Known_Non_Null_Operand_Seen := False;
3148 for J in 1 .. NN loop
3150 Lo : constant Node_Id :=
3152 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3153 Right_Opnd => Aggr_Length (J - 1));
3155 Hi : constant Node_Id :=
3157 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3159 Make_Op_Subtract (Loc,
3160 Left_Opnd => Aggr_Length (J),
3161 Right_Opnd => Make_Artyp_Literal (1)));
3164 -- Singleton case, simple assignment
3166 if Base_Type (Etype (Operands (J))) = Ctyp then
3167 Known_Non_Null_Operand_Seen := True;
3168 Insert_Action (Cnode,
3169 Make_Assignment_Statement (Loc,
3171 Make_Indexed_Component (Loc,
3172 Prefix => New_Occurrence_Of (Ent, Loc),
3173 Expressions => New_List (To_Ityp (Lo))),
3174 Expression => Operands (J)),
3175 Suppress => All_Checks);
3177 -- Array case, slice assignment, skipped when argument is fixed
3178 -- length and known to be null.
3180 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3183 Make_Assignment_Statement (Loc,
3187 New_Occurrence_Of (Ent, Loc),
3190 Low_Bound => To_Ityp (Lo),
3191 High_Bound => To_Ityp (Hi))),
3192 Expression => Operands (J));
3194 if Is_Fixed_Length (J) then
3195 Known_Non_Null_Operand_Seen := True;
3197 elsif not Known_Non_Null_Operand_Seen then
3199 -- Here if operand length is not statically known and no
3200 -- operand known to be non-null has been processed yet.
3201 -- If operand length is 0, we do not need to perform the
3202 -- assignment, and we must avoid the evaluation of the
3203 -- high bound of the slice, since it may underflow if the
3204 -- low bound is Ityp'First.
3207 Make_Implicit_If_Statement (Cnode,
3211 New_Occurrence_Of (Var_Length (J), Loc),
3212 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3213 Then_Statements => New_List (Assign));
3216 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3222 -- Finally we build the result, which is a reference to the array object
3224 Result := New_Reference_To (Ent, Loc);
3227 Rewrite (Cnode, Result);
3228 Analyze_And_Resolve (Cnode, Atyp);
3231 when Concatenation_Error =>
3233 -- Kill warning generated for the declaration of the static out of
3234 -- range high bound, and instead generate a Constraint_Error with
3235 -- an appropriate specific message.
3237 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3238 Apply_Compile_Time_Constraint_Error
3240 Msg => "concatenation result upper bound out of range?",
3241 Reason => CE_Range_Check_Failed);
3242 -- Set_Etype (Cnode, Atyp);
3243 end Expand_Concatenate;
3245 ------------------------
3246 -- Expand_N_Allocator --
3247 ------------------------
3249 procedure Expand_N_Allocator (N : Node_Id) is
3250 PtrT : constant Entity_Id := Etype (N);
3251 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
3252 Etyp : constant Entity_Id := Etype (Expression (N));
3253 Loc : constant Source_Ptr := Sloc (N);
3258 procedure Rewrite_Coextension (N : Node_Id);
3259 -- Static coextensions have the same lifetime as the entity they
3260 -- constrain. Such occurrences can be rewritten as aliased objects
3261 -- and their unrestricted access used instead of the coextension.
3263 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
3264 -- Given a constrained array type E, returns a node representing the
3265 -- code to compute the size in storage elements for the given type.
3266 -- This is done without using the attribute (which malfunctions for
3269 -------------------------
3270 -- Rewrite_Coextension --
3271 -------------------------
3273 procedure Rewrite_Coextension (N : Node_Id) is
3274 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
3275 Temp_Decl : Node_Id;
3276 Insert_Nod : Node_Id;
3280 -- Cnn : aliased Etyp;
3283 Make_Object_Declaration (Loc,
3284 Defining_Identifier => Temp_Id,
3285 Aliased_Present => True,
3286 Object_Definition => New_Occurrence_Of (Etyp, Loc));
3288 if Nkind (Expression (N)) = N_Qualified_Expression then
3289 Set_Expression (Temp_Decl, Expression (Expression (N)));
3292 -- Find the proper insertion node for the declaration
3294 Insert_Nod := Parent (N);
3295 while Present (Insert_Nod) loop
3297 Nkind (Insert_Nod) in N_Statement_Other_Than_Procedure_Call
3298 or else Nkind (Insert_Nod) = N_Procedure_Call_Statement
3299 or else Nkind (Insert_Nod) in N_Declaration;
3301 Insert_Nod := Parent (Insert_Nod);
3304 Insert_Before (Insert_Nod, Temp_Decl);
3305 Analyze (Temp_Decl);
3308 Make_Attribute_Reference (Loc,
3309 Prefix => New_Occurrence_Of (Temp_Id, Loc),
3310 Attribute_Name => Name_Unrestricted_Access));
3312 Analyze_And_Resolve (N, PtrT);
3313 end Rewrite_Coextension;
3315 ------------------------------
3316 -- Size_In_Storage_Elements --
3317 ------------------------------
3319 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
3321 -- Logically this just returns E'Max_Size_In_Storage_Elements.
3322 -- However, the reason for the existence of this function is
3323 -- to construct a test for sizes too large, which means near the
3324 -- 32-bit limit on a 32-bit machine, and precisely the trouble
3325 -- is that we get overflows when sizes are greater than 2**31.
3327 -- So what we end up doing for array types is to use the expression:
3329 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
3331 -- which avoids this problem. All this is a bit bogus, but it does
3332 -- mean we catch common cases of trying to allocate arrays that
3333 -- are too large, and which in the absence of a check results in
3334 -- undetected chaos ???
3341 for J in 1 .. Number_Dimensions (E) loop
3343 Make_Attribute_Reference (Loc,
3344 Prefix => New_Occurrence_Of (E, Loc),
3345 Attribute_Name => Name_Length,
3346 Expressions => New_List (Make_Integer_Literal (Loc, J)));
3353 Make_Op_Multiply (Loc,
3360 Make_Op_Multiply (Loc,
3363 Make_Attribute_Reference (Loc,
3364 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
3365 Attribute_Name => Name_Max_Size_In_Storage_Elements));
3367 end Size_In_Storage_Elements;
3369 -- Start of processing for Expand_N_Allocator
3372 -- RM E.2.3(22). We enforce that the expected type of an allocator
3373 -- shall not be a remote access-to-class-wide-limited-private type
3375 -- Why is this being done at expansion time, seems clearly wrong ???
3377 Validate_Remote_Access_To_Class_Wide_Type (N);
3379 -- Set the Storage Pool
3381 Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT)));
3383 if Present (Storage_Pool (N)) then
3384 if Is_RTE (Storage_Pool (N), RE_SS_Pool) then
3385 if VM_Target = No_VM then
3386 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3389 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
3390 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
3393 Set_Procedure_To_Call (N,
3394 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
3398 -- Under certain circumstances we can replace an allocator by an access
3399 -- to statically allocated storage. The conditions, as noted in AARM
3400 -- 3.10 (10c) are as follows:
3402 -- Size and initial value is known at compile time
3403 -- Access type is access-to-constant
3405 -- The allocator is not part of a constraint on a record component,
3406 -- because in that case the inserted actions are delayed until the
3407 -- record declaration is fully analyzed, which is too late for the
3408 -- analysis of the rewritten allocator.
3410 if Is_Access_Constant (PtrT)
3411 and then Nkind (Expression (N)) = N_Qualified_Expression
3412 and then Compile_Time_Known_Value (Expression (Expression (N)))
3413 and then Size_Known_At_Compile_Time
3414 (Etype (Expression (Expression (N))))
3415 and then not Is_Record_Type (Current_Scope)
3417 -- Here we can do the optimization. For the allocator
3421 -- We insert an object declaration
3423 -- Tnn : aliased x := y;
3425 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
3426 -- marked as requiring static allocation.
3428 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
3429 Desig := Subtype_Mark (Expression (N));
3431 -- If context is constrained, use constrained subtype directly,
3432 -- so that the constant is not labelled as having a nominally
3433 -- unconstrained subtype.
3435 if Entity (Desig) = Base_Type (Dtyp) then
3436 Desig := New_Occurrence_Of (Dtyp, Loc);
3440 Make_Object_Declaration (Loc,
3441 Defining_Identifier => Temp,
3442 Aliased_Present => True,
3443 Constant_Present => Is_Access_Constant (PtrT),
3444 Object_Definition => Desig,
3445 Expression => Expression (Expression (N))));
3448 Make_Attribute_Reference (Loc,
3449 Prefix => New_Occurrence_Of (Temp, Loc),
3450 Attribute_Name => Name_Unrestricted_Access));
3452 Analyze_And_Resolve (N, PtrT);
3454 -- We set the variable as statically allocated, since we don't want
3455 -- it going on the stack of the current procedure!
3457 Set_Is_Statically_Allocated (Temp);
3461 -- Same if the allocator is an access discriminant for a local object:
3462 -- instead of an allocator we create a local value and constrain the
3463 -- enclosing object with the corresponding access attribute.
3465 if Is_Static_Coextension (N) then
3466 Rewrite_Coextension (N);
3470 -- Check for size too large, we do this because the back end misses
3471 -- proper checks here and can generate rubbish allocation calls when
3472 -- we are near the limit. We only do this for the 32-bit address case
3473 -- since that is from a practical point of view where we see a problem.
3475 if System_Address_Size = 32
3476 and then not Storage_Checks_Suppressed (PtrT)
3477 and then not Storage_Checks_Suppressed (Dtyp)
3478 and then not Storage_Checks_Suppressed (Etyp)
3480 -- The check we want to generate should look like
3482 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
3483 -- raise Storage_Error;
3486 -- where 3.5 gigabytes is a constant large enough to accommodate any
3487 -- reasonable request for. But we can't do it this way because at
3488 -- least at the moment we don't compute this attribute right, and
3489 -- can silently give wrong results when the result gets large. Since
3490 -- this is all about large results, that's bad, so instead we only
3491 -- apply the check for constrained arrays, and manually compute the
3492 -- value of the attribute ???
3494 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
3496 Make_Raise_Storage_Error (Loc,
3499 Left_Opnd => Size_In_Storage_Elements (Etyp),
3501 Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))),
3502 Reason => SE_Object_Too_Large));
3506 -- Handle case of qualified expression (other than optimization above)
3507 -- First apply constraint checks, because the bounds or discriminants
3508 -- in the aggregate might not match the subtype mark in the allocator.
3510 if Nkind (Expression (N)) = N_Qualified_Expression then
3511 Apply_Constraint_Check
3512 (Expression (Expression (N)), Etype (Expression (N)));
3514 Expand_Allocator_Expression (N);
3518 -- If the allocator is for a type which requires initialization, and
3519 -- there is no initial value (i.e. operand is a subtype indication
3520 -- rather than a qualified expression), then we must generate a call to
3521 -- the initialization routine using an expressions action node:
3523 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
3525 -- Here ptr_T is the pointer type for the allocator, and T is the
3526 -- subtype of the allocator. A special case arises if the designated
3527 -- type of the access type is a task or contains tasks. In this case
3528 -- the call to Init (Temp.all ...) is replaced by code that ensures
3529 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
3530 -- for details). In addition, if the type T is a task T, then the
3531 -- first argument to Init must be converted to the task record type.
3534 T : constant Entity_Id := Entity (Expression (N));
3540 Init_Arg1 : Node_Id;
3541 Temp_Decl : Node_Id;
3542 Temp_Type : Entity_Id;
3545 if No_Initialization (N) then
3547 -- Even though this might be a simple allocation, create a custom
3548 -- Allocate if the context requires it. Since .NET/JVM compilers
3549 -- do not support pools, this step is skipped.
3551 if VM_Target = No_VM
3552 and then Present (Associated_Collection (PtrT))
3554 Build_Allocate_Deallocate_Proc
3556 Is_Allocate => True);
3559 -- Case of no initialization procedure present
3561 elsif not Has_Non_Null_Base_Init_Proc (T) then
3563 -- Case of simple initialization required
3565 if Needs_Simple_Initialization (T) then
3566 Check_Restriction (No_Default_Initialization, N);
3567 Rewrite (Expression (N),
3568 Make_Qualified_Expression (Loc,
3569 Subtype_Mark => New_Occurrence_Of (T, Loc),
3570 Expression => Get_Simple_Init_Val (T, N)));
3572 Analyze_And_Resolve (Expression (Expression (N)), T);
3573 Analyze_And_Resolve (Expression (N), T);
3574 Set_Paren_Count (Expression (Expression (N)), 1);
3575 Expand_N_Allocator (N);
3577 -- No initialization required
3583 -- Case of initialization procedure present, must be called
3586 Check_Restriction (No_Default_Initialization, N);
3588 if not Restriction_Active (No_Default_Initialization) then
3589 Init := Base_Init_Proc (T);
3591 Temp := Make_Temporary (Loc, 'P');
3593 -- Construct argument list for the initialization routine call
3596 Make_Explicit_Dereference (Loc,
3598 New_Reference_To (Temp, Loc));
3600 Set_Assignment_OK (Init_Arg1);
3603 -- The initialization procedure expects a specific type. if the
3604 -- context is access to class wide, indicate that the object
3605 -- being allocated has the right specific type.
3607 if Is_Class_Wide_Type (Dtyp) then
3608 Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1);
3611 -- If designated type is a concurrent type or if it is private
3612 -- type whose definition is a concurrent type, the first
3613 -- argument in the Init routine has to be unchecked conversion
3614 -- to the corresponding record type. If the designated type is
3615 -- a derived type, also convert the argument to its root type.
3617 if Is_Concurrent_Type (T) then
3619 Unchecked_Convert_To (
3620 Corresponding_Record_Type (T), Init_Arg1);
3622 elsif Is_Private_Type (T)
3623 and then Present (Full_View (T))
3624 and then Is_Concurrent_Type (Full_View (T))
3627 Unchecked_Convert_To
3628 (Corresponding_Record_Type (Full_View (T)), Init_Arg1);
3630 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
3632 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
3635 Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1);
3636 Set_Etype (Init_Arg1, Ftyp);
3640 Args := New_List (Init_Arg1);
3642 -- For the task case, pass the Master_Id of the access type as
3643 -- the value of the _Master parameter, and _Chain as the value
3644 -- of the _Chain parameter (_Chain will be defined as part of
3645 -- the generated code for the allocator).
3647 -- In Ada 2005, the context may be a function that returns an
3648 -- anonymous access type. In that case the Master_Id has been
3649 -- created when expanding the function declaration.
3651 if Has_Task (T) then
3652 if No (Master_Id (Base_Type (PtrT))) then
3654 -- The designated type was an incomplete type, and the
3655 -- access type did not get expanded. Salvage it now.
3657 if not Restriction_Active (No_Task_Hierarchy) then
3658 pragma Assert (Present (Parent (Base_Type (PtrT))));
3659 Expand_N_Full_Type_Declaration
3660 (Parent (Base_Type (PtrT)));
3664 -- If the context of the allocator is a declaration or an
3665 -- assignment, we can generate a meaningful image for it,
3666 -- even though subsequent assignments might remove the
3667 -- connection between task and entity. We build this image
3668 -- when the left-hand side is a simple variable, a simple
3669 -- indexed assignment or a simple selected component.
3671 if Nkind (Parent (N)) = N_Assignment_Statement then
3673 Nam : constant Node_Id := Name (Parent (N));
3676 if Is_Entity_Name (Nam) then
3678 Build_Task_Image_Decls
3681 (Entity (Nam), Sloc (Nam)), T);
3683 elsif Nkind_In (Nam, N_Indexed_Component,
3684 N_Selected_Component)
3685 and then Is_Entity_Name (Prefix (Nam))
3688 Build_Task_Image_Decls
3689 (Loc, Nam, Etype (Prefix (Nam)));
3691 Decls := Build_Task_Image_Decls (Loc, T, T);
3695 elsif Nkind (Parent (N)) = N_Object_Declaration then
3697 Build_Task_Image_Decls
3698 (Loc, Defining_Identifier (Parent (N)), T);
3701 Decls := Build_Task_Image_Decls (Loc, T, T);
3704 if Restriction_Active (No_Task_Hierarchy) then
3706 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
3710 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
3713 Append_To (Args, Make_Identifier (Loc, Name_uChain));
3715 Decl := Last (Decls);
3717 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
3719 -- Has_Task is false, Decls not used
3725 -- Add discriminants if discriminated type
3728 Dis : Boolean := False;
3732 if Has_Discriminants (T) then
3736 elsif Is_Private_Type (T)
3737 and then Present (Full_View (T))
3738 and then Has_Discriminants (Full_View (T))
3741 Typ := Full_View (T);
3746 -- If the allocated object will be constrained by the
3747 -- default values for discriminants, then build a subtype
3748 -- with those defaults, and change the allocated subtype
3749 -- to that. Note that this happens in fewer cases in Ada
3752 if not Is_Constrained (Typ)
3753 and then Present (Discriminant_Default_Value
3754 (First_Discriminant (Typ)))
3755 and then (Ada_Version < Ada_2005
3757 not Has_Constrained_Partial_View (Typ))
3759 Typ := Build_Default_Subtype (Typ, N);
3760 Set_Expression (N, New_Reference_To (Typ, Loc));
3763 Discr := First_Elmt (Discriminant_Constraint (Typ));
3764 while Present (Discr) loop
3765 Nod := Node (Discr);
3766 Append (New_Copy_Tree (Node (Discr)), Args);
3768 -- AI-416: when the discriminant constraint is an
3769 -- anonymous access type make sure an accessibility
3770 -- check is inserted if necessary (3.10.2(22.q/2))
3772 if Ada_Version >= Ada_2005
3774 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
3776 Apply_Accessibility_Check
3777 (Nod, Typ, Insert_Node => Nod);
3785 -- We set the allocator as analyzed so that when we analyze the
3786 -- expression actions node, we do not get an unwanted recursive
3787 -- expansion of the allocator expression.
3789 Set_Analyzed (N, True);
3790 Nod := Relocate_Node (N);
3792 -- Here is the transformation:
3794 -- output: Temp : constant ptr_T := new T;
3795 -- Init (Temp.all, ...);
3796 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
3797 -- <CTRL> Initialize (Finalizable (Temp.all));
3799 -- Here ptr_T is the pointer type for the allocator, and is the
3800 -- subtype of the allocator.
3803 Make_Object_Declaration (Loc,
3804 Defining_Identifier => Temp,
3805 Constant_Present => True,
3806 Object_Definition => New_Reference_To (Temp_Type, Loc),
3809 Set_Assignment_OK (Temp_Decl);
3810 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
3812 Complete_Controlled_Allocation (Temp_Decl);
3814 -- If the designated type is a task type or contains tasks,
3815 -- create block to activate created tasks, and insert
3816 -- declaration for Task_Image variable ahead of call.
3818 if Has_Task (T) then
3820 L : constant List_Id := New_List;
3823 Build_Task_Allocate_Block (L, Nod, Args);
3825 Insert_List_Before (First (Declarations (Blk)), Decls);
3826 Insert_Actions (N, L);
3831 Make_Procedure_Call_Statement (Loc,
3832 Name => New_Reference_To (Init, Loc),
3833 Parameter_Associations => Args));
3836 if Needs_Finalization (T) then
3839 -- [Deep_]Initialize (Init_Arg1);
3843 (Obj_Ref => New_Copy_Tree (Init_Arg1),
3846 if Present (Associated_Collection (PtrT)) then
3848 -- Special processing for .NET/JVM, the allocated object
3849 -- is attached to the finalization collection. Generate:
3851 -- Attach (<PtrT>FC, Root_Controlled_Ptr (Init_Arg1));
3853 -- Types derived from [Limited_]Controlled are the only
3854 -- ones considered since they have fields Prev and Next.
3856 if VM_Target /= No_VM then
3857 if Is_Controlled (T) then
3860 (Obj_Ref => New_Copy_Tree (Init_Arg1),
3864 -- Default case, generate:
3866 -- Set_Finalize_Address_Ptr
3867 -- (Pool, <Finalize_Address>'Unrestricted_Access)
3869 -- Do not generate the above for CodePeer compilations
3870 -- because Finalize_Address is never built.
3872 elsif not CodePeer_Mode then
3874 Make_Set_Finalize_Address_Ptr_Call
3882 Rewrite (N, New_Reference_To (Temp, Loc));
3883 Analyze_And_Resolve (N, PtrT);
3888 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
3889 -- object that has been rewritten as a reference, we displace "this"
3890 -- to reference properly its secondary dispatch table.
3892 if Nkind (N) = N_Identifier
3893 and then Is_Interface (Dtyp)
3895 Displace_Allocator_Pointer (N);
3899 when RE_Not_Available =>
3901 end Expand_N_Allocator;
3903 -----------------------
3904 -- Expand_N_And_Then --
3905 -----------------------
3907 procedure Expand_N_And_Then (N : Node_Id)
3908 renames Expand_Short_Circuit_Operator;
3910 ------------------------------
3911 -- Expand_N_Case_Expression --
3912 ------------------------------
3914 procedure Expand_N_Case_Expression (N : Node_Id) is
3915 Loc : constant Source_Ptr := Sloc (N);
3916 Typ : constant Entity_Id := Etype (N);
3928 -- case X is when A => AX, when B => BX ...
3943 -- However, this expansion is wrong for limited types, and also
3944 -- wrong for unconstrained types (since the bounds may not be the
3945 -- same in all branches). Furthermore it involves an extra copy
3946 -- for large objects. So we take care of this by using the following
3947 -- modified expansion for non-scalar types:
3950 -- type Pnn is access all typ;
3954 -- T := AX'Unrestricted_Access;
3956 -- T := BX'Unrestricted_Access;
3962 Make_Case_Statement (Loc,
3963 Expression => Expression (N),
3964 Alternatives => New_List);
3966 Actions := New_List;
3970 if Is_Scalar_Type (Typ) then
3974 Pnn := Make_Temporary (Loc, 'P');
3976 Make_Full_Type_Declaration (Loc,
3977 Defining_Identifier => Pnn,
3979 Make_Access_To_Object_Definition (Loc,
3980 All_Present => True,
3981 Subtype_Indication =>
3982 New_Reference_To (Typ, Loc))));
3986 Tnn := Make_Temporary (Loc, 'T');
3988 Make_Object_Declaration (Loc,
3989 Defining_Identifier => Tnn,
3990 Object_Definition => New_Occurrence_Of (Ttyp, Loc)));
3992 -- Now process the alternatives
3994 Alt := First (Alternatives (N));
3995 while Present (Alt) loop
3997 Aexp : Node_Id := Expression (Alt);
3998 Aloc : constant Source_Ptr := Sloc (Aexp);
4001 -- Propagate declarations inserted in the node by Insert_Actions
4002 -- (for example, temporaries generated to remove side effects).
4004 Append_List_To (Actions, Sinfo.Actions (Alt));
4006 if not Is_Scalar_Type (Typ) then
4008 Make_Attribute_Reference (Aloc,
4009 Prefix => Relocate_Node (Aexp),
4010 Attribute_Name => Name_Unrestricted_Access);
4014 (Alternatives (Cstmt),
4015 Make_Case_Statement_Alternative (Sloc (Alt),
4016 Discrete_Choices => Discrete_Choices (Alt),
4017 Statements => New_List (
4018 Make_Assignment_Statement (Aloc,
4019 Name => New_Occurrence_Of (Tnn, Loc),
4020 Expression => Aexp))));
4026 Append_To (Actions, Cstmt);
4028 -- Construct and return final expression with actions
4030 if Is_Scalar_Type (Typ) then
4031 Fexp := New_Occurrence_Of (Tnn, Loc);
4034 Make_Explicit_Dereference (Loc,
4035 Prefix => New_Occurrence_Of (Tnn, Loc));
4039 Make_Expression_With_Actions (Loc,
4041 Actions => Actions));
4043 Analyze_And_Resolve (N, Typ);
4044 end Expand_N_Case_Expression;
4046 -------------------------------------
4047 -- Expand_N_Conditional_Expression --
4048 -------------------------------------
4050 -- Deal with limited types and expression actions
4052 procedure Expand_N_Conditional_Expression (N : Node_Id) is
4053 Loc : constant Source_Ptr := Sloc (N);
4054 Cond : constant Node_Id := First (Expressions (N));
4055 Thenx : constant Node_Id := Next (Cond);
4056 Elsex : constant Node_Id := Next (Thenx);
4057 Typ : constant Entity_Id := Etype (N);
4068 -- Fold at compile time if condition known. We have already folded
4069 -- static conditional expressions, but it is possible to fold any
4070 -- case in which the condition is known at compile time, even though
4071 -- the result is non-static.
4073 -- Note that we don't do the fold of such cases in Sem_Elab because
4074 -- it can cause infinite loops with the expander adding a conditional
4075 -- expression, and Sem_Elab circuitry removing it repeatedly.
4077 if Compile_Time_Known_Value (Cond) then
4078 if Is_True (Expr_Value (Cond)) then
4080 Actions := Then_Actions (N);
4083 Actions := Else_Actions (N);
4088 if Present (Actions) then
4090 -- If we are not allowed to use Expression_With_Actions, just
4091 -- skip the optimization, it is not critical for correctness.
4093 if not Use_Expression_With_Actions then
4094 goto Skip_Optimization;
4098 Make_Expression_With_Actions (Loc,
4099 Expression => Relocate_Node (Expr),
4100 Actions => Actions));
4101 Analyze_And_Resolve (N, Typ);
4104 Rewrite (N, Relocate_Node (Expr));
4107 -- Note that the result is never static (legitimate cases of static
4108 -- conditional expressions were folded in Sem_Eval).
4110 Set_Is_Static_Expression (N, False);
4114 <<Skip_Optimization>>
4116 -- If the type is limited or unconstrained, we expand as follows to
4117 -- avoid any possibility of improper copies.
4119 -- Note: it may be possible to avoid this special processing if the
4120 -- back end uses its own mechanisms for handling by-reference types ???
4122 -- type Ptr is access all Typ;
4126 -- Cnn := then-expr'Unrestricted_Access;
4129 -- Cnn := else-expr'Unrestricted_Access;
4132 -- and replace the conditional expression by a reference to Cnn.all.
4134 -- This special case can be skipped if the back end handles limited
4135 -- types properly and ensures that no incorrect copies are made.
4137 if Is_By_Reference_Type (Typ)
4138 and then not Back_End_Handles_Limited_Types
4140 Cnn := Make_Temporary (Loc, 'C', N);
4143 Make_Full_Type_Declaration (Loc,
4144 Defining_Identifier =>
4145 Make_Temporary (Loc, 'A'),
4147 Make_Access_To_Object_Definition (Loc,
4148 All_Present => True,
4149 Subtype_Indication => New_Reference_To (Typ, Loc)));
4151 Insert_Action (N, P_Decl);
4154 Make_Object_Declaration (Loc,
4155 Defining_Identifier => Cnn,
4156 Object_Definition =>
4157 New_Occurrence_Of (Defining_Identifier (P_Decl), Loc));
4160 Make_Implicit_If_Statement (N,
4161 Condition => Relocate_Node (Cond),
4163 Then_Statements => New_List (
4164 Make_Assignment_Statement (Sloc (Thenx),
4165 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
4167 Make_Attribute_Reference (Loc,
4168 Attribute_Name => Name_Unrestricted_Access,
4169 Prefix => Relocate_Node (Thenx)))),
4171 Else_Statements => New_List (
4172 Make_Assignment_Statement (Sloc (Elsex),
4173 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
4175 Make_Attribute_Reference (Loc,
4176 Attribute_Name => Name_Unrestricted_Access,
4177 Prefix => Relocate_Node (Elsex)))));
4180 Make_Explicit_Dereference (Loc,
4181 Prefix => New_Occurrence_Of (Cnn, Loc));
4183 -- For other types, we only need to expand if there are other actions
4184 -- associated with either branch.
4186 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
4188 -- We have two approaches to handling this. If we are allowed to use
4189 -- N_Expression_With_Actions, then we can just wrap the actions into
4190 -- the appropriate expression.
4192 if Use_Expression_With_Actions then
4193 if Present (Then_Actions (N)) then
4195 Make_Expression_With_Actions (Sloc (Thenx),
4196 Actions => Then_Actions (N),
4197 Expression => Relocate_Node (Thenx)));
4198 Set_Then_Actions (N, No_List);
4199 Analyze_And_Resolve (Thenx, Typ);
4202 if Present (Else_Actions (N)) then
4204 Make_Expression_With_Actions (Sloc (Elsex),
4205 Actions => Else_Actions (N),
4206 Expression => Relocate_Node (Elsex)));
4207 Set_Else_Actions (N, No_List);
4208 Analyze_And_Resolve (Elsex, Typ);
4213 -- if we can't use N_Expression_With_Actions nodes, then we insert
4214 -- the following sequence of actions (using Insert_Actions):
4219 -- Cnn := then-expr;
4225 -- and replace the conditional expression by a reference to Cnn
4228 Cnn := Make_Temporary (Loc, 'C', N);
4231 Make_Object_Declaration (Loc,
4232 Defining_Identifier => Cnn,
4233 Object_Definition => New_Occurrence_Of (Typ, Loc));
4236 Make_Implicit_If_Statement (N,
4237 Condition => Relocate_Node (Cond),
4239 Then_Statements => New_List (
4240 Make_Assignment_Statement (Sloc (Thenx),
4241 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
4242 Expression => Relocate_Node (Thenx))),
4244 Else_Statements => New_List (
4245 Make_Assignment_Statement (Sloc (Elsex),
4246 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
4247 Expression => Relocate_Node (Elsex))));
4249 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
4250 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
4252 New_N := New_Occurrence_Of (Cnn, Loc);
4255 -- If no actions then no expansion needed, gigi will handle it using
4256 -- the same approach as a C conditional expression.
4262 -- Fall through here for either the limited expansion, or the case of
4263 -- inserting actions for non-limited types. In both these cases, we must
4264 -- move the SLOC of the parent If statement to the newly created one and
4265 -- change it to the SLOC of the expression which, after expansion, will
4266 -- correspond to what is being evaluated.
4268 if Present (Parent (N))
4269 and then Nkind (Parent (N)) = N_If_Statement
4271 Set_Sloc (New_If, Sloc (Parent (N)));
4272 Set_Sloc (Parent (N), Loc);
4275 -- Make sure Then_Actions and Else_Actions are appropriately moved
4276 -- to the new if statement.
4278 if Present (Then_Actions (N)) then
4280 (First (Then_Statements (New_If)), Then_Actions (N));
4283 if Present (Else_Actions (N)) then
4285 (First (Else_Statements (New_If)), Else_Actions (N));
4288 Insert_Action (N, Decl);
4289 Insert_Action (N, New_If);
4291 Analyze_And_Resolve (N, Typ);
4292 end Expand_N_Conditional_Expression;
4294 -----------------------------------
4295 -- Expand_N_Explicit_Dereference --
4296 -----------------------------------
4298 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
4300 -- Insert explicit dereference call for the checked storage pool case
4302 Insert_Dereference_Action (Prefix (N));
4303 end Expand_N_Explicit_Dereference;
4305 --------------------------------------
4306 -- Expand_N_Expression_With_Actions --
4307 --------------------------------------
4309 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
4311 procedure Process_Transient_Object (Decl : Node_Id);
4312 -- Given the declaration of a controlled transient declared inside the
4313 -- Actions list of an Expression_With_Actions, generate all necessary
4314 -- types and hooks in order to properly finalize the transient. This
4315 -- mechanism works in conjunction with Build_Finalizer.
4317 ------------------------------
4318 -- Process_Transient_Object --
4319 ------------------------------
4321 procedure Process_Transient_Object (Decl : Node_Id) is
4322 Ins_Nod : constant Node_Id := Parent (N);
4323 -- To avoid the insertion of generated code in the list of Actions,
4324 -- Insert_Action must look at the parent field of the EWA.
4326 Loc : constant Source_Ptr := Sloc (Decl);
4327 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
4328 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
4329 Desig_Typ : Entity_Id;
4333 Temp_Decl : Node_Id;
4337 -- Step 1: Create the access type which provides a reference to
4338 -- the transient object.
4340 if Is_Access_Type (Obj_Typ) then
4341 Desig_Typ := Directly_Designated_Type (Obj_Typ);
4343 Desig_Typ := Obj_Typ;
4347 -- Ann : access [all] <Desig_Typ>;
4349 Ptr_Id := Make_Temporary (Loc, 'A');
4352 Make_Full_Type_Declaration (Loc,
4353 Defining_Identifier => Ptr_Id,
4355 Make_Access_To_Object_Definition (Loc,
4357 Ekind (Obj_Typ) = E_General_Access_Type,
4358 Subtype_Indication =>
4359 New_Reference_To (Desig_Typ, Loc)));
4361 Insert_Action (Ins_Nod, Ptr_Decl);
4364 -- Step 2: Create a temporary which acts as a hook to the transient
4365 -- object. Generate:
4367 -- Temp : Ptr_Id := null;
4369 Temp_Id := Make_Temporary (Loc, 'T');
4372 Make_Object_Declaration (Loc,
4373 Defining_Identifier => Temp_Id,
4374 Object_Definition => New_Reference_To (Ptr_Id, Loc));
4376 Insert_Action (Ins_Nod, Temp_Decl);
4377 Analyze (Temp_Decl);
4379 -- Mark this temporary as created for the purposes of "exporting" the
4380 -- transient declaration out of the Actions list. This signals the
4381 -- machinery in Build_Finalizer to recognize this special case.
4383 Set_Return_Flag_Or_Transient_Decl (Temp_Id, Decl);
4385 -- Step 3: "Hook" the transient object to the temporary
4387 if Is_Access_Type (Obj_Typ) then
4388 Expr := Convert_To (Ptr_Id, New_Reference_To (Obj_Id, Loc));
4391 Make_Attribute_Reference (Loc,
4393 New_Reference_To (Obj_Id, Loc),
4394 Attribute_Name => Name_Unrestricted_Access);
4398 -- Temp := Ptr_Id (Obj_Id);
4400 -- Temp := Obj_Id'Unrestricted_Access;
4402 Insert_After_And_Analyze (Decl,
4403 Make_Assignment_Statement (Loc,
4404 Name => New_Reference_To (Temp_Id, Loc),
4405 Expression => Expr));
4406 end Process_Transient_Object;
4410 -- Start of processing for Expand_N_Expression_With_Actions
4413 Decl := First (Actions (N));
4414 while Present (Decl) loop
4415 if Nkind (Decl) = N_Object_Declaration
4416 and then Is_Finalizable_Transient (Decl, N)
4418 Process_Transient_Object (Decl);
4423 end Expand_N_Expression_With_Actions;
4429 procedure Expand_N_In (N : Node_Id) is
4430 Loc : constant Source_Ptr := Sloc (N);
4431 Restyp : constant Entity_Id := Etype (N);
4432 Lop : constant Node_Id := Left_Opnd (N);
4433 Rop : constant Node_Id := Right_Opnd (N);
4434 Static : constant Boolean := Is_OK_Static_Expression (N);
4439 procedure Expand_Set_Membership;
4440 -- For each choice we create a simple equality or membership test.
4441 -- The whole membership is rewritten connecting these with OR ELSE.
4443 ---------------------------
4444 -- Expand_Set_Membership --
4445 ---------------------------
4447 procedure Expand_Set_Membership is
4451 function Make_Cond (Alt : Node_Id) return Node_Id;
4452 -- If the alternative is a subtype mark, create a simple membership
4453 -- test. Otherwise create an equality test for it.
4459 function Make_Cond (Alt : Node_Id) return Node_Id is
4461 L : constant Node_Id := New_Copy (Lop);
4462 R : constant Node_Id := Relocate_Node (Alt);
4465 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
4466 or else Nkind (Alt) = N_Range
4469 Make_In (Sloc (Alt),
4474 Make_Op_Eq (Sloc (Alt),
4482 -- Start of processing for Expand_Set_Membership
4485 Alt := Last (Alternatives (N));
4486 Res := Make_Cond (Alt);
4489 while Present (Alt) loop
4491 Make_Or_Else (Sloc (Alt),
4492 Left_Opnd => Make_Cond (Alt),
4498 Analyze_And_Resolve (N, Standard_Boolean);
4499 end Expand_Set_Membership;
4501 procedure Substitute_Valid_Check;
4502 -- Replaces node N by Lop'Valid. This is done when we have an explicit
4503 -- test for the left operand being in range of its subtype.
4505 ----------------------------
4506 -- Substitute_Valid_Check --
4507 ----------------------------
4509 procedure Substitute_Valid_Check is
4512 Make_Attribute_Reference (Loc,
4513 Prefix => Relocate_Node (Lop),
4514 Attribute_Name => Name_Valid));
4516 Analyze_And_Resolve (N, Restyp);
4518 Error_Msg_N ("?explicit membership test may be optimized away", N);
4519 Error_Msg_N -- CODEFIX
4520 ("\?use ''Valid attribute instead", N);
4522 end Substitute_Valid_Check;
4524 -- Start of processing for Expand_N_In
4527 -- If set membership case, expand with separate procedure
4529 if Present (Alternatives (N)) then
4530 Remove_Side_Effects (Lop);
4531 Expand_Set_Membership;
4535 -- Not set membership, proceed with expansion
4537 Ltyp := Etype (Left_Opnd (N));
4538 Rtyp := Etype (Right_Opnd (N));
4540 -- Check case of explicit test for an expression in range of its
4541 -- subtype. This is suspicious usage and we replace it with a 'Valid
4542 -- test and give a warning. For floating point types however, this is a
4543 -- standard way to check for finite numbers, and using 'Valid would
4544 -- typically be a pessimization. Also skip this test for predicated
4545 -- types, since it is perfectly reasonable to check if a value meets
4548 if Is_Scalar_Type (Ltyp)
4549 and then not Is_Floating_Point_Type (Ltyp)
4550 and then Nkind (Rop) in N_Has_Entity
4551 and then Ltyp = Entity (Rop)
4552 and then Comes_From_Source (N)
4553 and then VM_Target = No_VM
4554 and then not (Is_Discrete_Type (Ltyp)
4555 and then Present (Predicate_Function (Ltyp)))
4557 Substitute_Valid_Check;
4561 -- Do validity check on operands
4563 if Validity_Checks_On and Validity_Check_Operands then
4564 Ensure_Valid (Left_Opnd (N));
4565 Validity_Check_Range (Right_Opnd (N));
4568 -- Case of explicit range
4570 if Nkind (Rop) = N_Range then
4572 Lo : constant Node_Id := Low_Bound (Rop);
4573 Hi : constant Node_Id := High_Bound (Rop);
4575 Lo_Orig : constant Node_Id := Original_Node (Lo);
4576 Hi_Orig : constant Node_Id := Original_Node (Hi);
4578 Lcheck : Compare_Result;
4579 Ucheck : Compare_Result;
4581 Warn1 : constant Boolean :=
4582 Constant_Condition_Warnings
4583 and then Comes_From_Source (N)
4584 and then not In_Instance;
4585 -- This must be true for any of the optimization warnings, we
4586 -- clearly want to give them only for source with the flag on. We
4587 -- also skip these warnings in an instance since it may be the
4588 -- case that different instantiations have different ranges.
4590 Warn2 : constant Boolean :=
4592 and then Nkind (Original_Node (Rop)) = N_Range
4593 and then Is_Integer_Type (Etype (Lo));
4594 -- For the case where only one bound warning is elided, we also
4595 -- insist on an explicit range and an integer type. The reason is
4596 -- that the use of enumeration ranges including an end point is
4597 -- common, as is the use of a subtype name, one of whose bounds is
4598 -- the same as the type of the expression.
4601 -- If test is explicit x'First .. x'Last, replace by valid check
4603 -- Could use some individual comments for this complex test ???
4605 if Is_Scalar_Type (Ltyp)
4606 and then Nkind (Lo_Orig) = N_Attribute_Reference
4607 and then Attribute_Name (Lo_Orig) = Name_First
4608 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
4609 and then Entity (Prefix (Lo_Orig)) = Ltyp
4610 and then Nkind (Hi_Orig) = N_Attribute_Reference
4611 and then Attribute_Name (Hi_Orig) = Name_Last
4612 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
4613 and then Entity (Prefix (Hi_Orig)) = Ltyp
4614 and then Comes_From_Source (N)
4615 and then VM_Target = No_VM
4617 Substitute_Valid_Check;
4621 -- If bounds of type are known at compile time, and the end points
4622 -- are known at compile time and identical, this is another case
4623 -- for substituting a valid test. We only do this for discrete
4624 -- types, since it won't arise in practice for float types.
4626 if Comes_From_Source (N)
4627 and then Is_Discrete_Type (Ltyp)
4628 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
4629 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
4630 and then Compile_Time_Known_Value (Lo)
4631 and then Compile_Time_Known_Value (Hi)
4632 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
4633 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
4635 -- Kill warnings in instances, since they may be cases where we
4636 -- have a test in the generic that makes sense with some types
4637 -- and not with other types.
4639 and then not In_Instance
4641 Substitute_Valid_Check;
4645 -- If we have an explicit range, do a bit of optimization based on
4646 -- range analysis (we may be able to kill one or both checks).
4648 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
4649 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
4651 -- If either check is known to fail, replace result by False since
4652 -- the other check does not matter. Preserve the static flag for
4653 -- legality checks, because we are constant-folding beyond RM 4.9.
4655 if Lcheck = LT or else Ucheck = GT then
4657 Error_Msg_N ("?range test optimized away", N);
4658 Error_Msg_N ("\?value is known to be out of range", N);
4661 Rewrite (N, New_Reference_To (Standard_False, Loc));
4662 Analyze_And_Resolve (N, Restyp);
4663 Set_Is_Static_Expression (N, Static);
4666 -- If both checks are known to succeed, replace result by True,
4667 -- since we know we are in range.
4669 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4671 Error_Msg_N ("?range test optimized away", N);
4672 Error_Msg_N ("\?value is known to be in range", N);
4675 Rewrite (N, New_Reference_To (Standard_True, Loc));
4676 Analyze_And_Resolve (N, Restyp);
4677 Set_Is_Static_Expression (N, Static);
4680 -- If lower bound check succeeds and upper bound check is not
4681 -- known to succeed or fail, then replace the range check with
4682 -- a comparison against the upper bound.
4684 elsif Lcheck in Compare_GE then
4685 if Warn2 and then not In_Instance then
4686 Error_Msg_N ("?lower bound test optimized away", Lo);
4687 Error_Msg_N ("\?value is known to be in range", Lo);
4693 Right_Opnd => High_Bound (Rop)));
4694 Analyze_And_Resolve (N, Restyp);
4697 -- If upper bound check succeeds and lower bound check is not
4698 -- known to succeed or fail, then replace the range check with
4699 -- a comparison against the lower bound.
4701 elsif Ucheck in Compare_LE then
4702 if Warn2 and then not In_Instance then
4703 Error_Msg_N ("?upper bound test optimized away", Hi);
4704 Error_Msg_N ("\?value is known to be in range", Hi);
4710 Right_Opnd => Low_Bound (Rop)));
4711 Analyze_And_Resolve (N, Restyp);
4715 -- We couldn't optimize away the range check, but there is one
4716 -- more issue. If we are checking constant conditionals, then we
4717 -- see if we can determine the outcome assuming everything is
4718 -- valid, and if so give an appropriate warning.
4720 if Warn1 and then not Assume_No_Invalid_Values then
4721 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
4722 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
4724 -- Result is out of range for valid value
4726 if Lcheck = LT or else Ucheck = GT then
4728 ("?value can only be in range if it is invalid", N);
4730 -- Result is in range for valid value
4732 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4734 ("?value can only be out of range if it is invalid", N);
4736 -- Lower bound check succeeds if value is valid
4738 elsif Warn2 and then Lcheck in Compare_GE then
4740 ("?lower bound check only fails if it is invalid", Lo);
4742 -- Upper bound check succeeds if value is valid
4744 elsif Warn2 and then Ucheck in Compare_LE then
4746 ("?upper bound check only fails for invalid values", Hi);
4751 -- For all other cases of an explicit range, nothing to be done
4755 -- Here right operand is a subtype mark
4759 Typ : Entity_Id := Etype (Rop);
4760 Is_Acc : constant Boolean := Is_Access_Type (Typ);
4761 Cond : Node_Id := Empty;
4763 Obj : Node_Id := Lop;
4764 SCIL_Node : Node_Id;
4767 Remove_Side_Effects (Obj);
4769 -- For tagged type, do tagged membership operation
4771 if Is_Tagged_Type (Typ) then
4773 -- No expansion will be performed when VM_Target, as the VM
4774 -- back-ends will handle the membership tests directly (tags
4775 -- are not explicitly represented in Java objects, so the
4776 -- normal tagged membership expansion is not what we want).
4778 if Tagged_Type_Expansion then
4779 Tagged_Membership (N, SCIL_Node, New_N);
4781 Analyze_And_Resolve (N, Restyp);
4783 -- Update decoration of relocated node referenced by the
4786 if Generate_SCIL and then Present (SCIL_Node) then
4787 Set_SCIL_Node (N, SCIL_Node);
4793 -- If type is scalar type, rewrite as x in t'First .. t'Last.
4794 -- This reason we do this is that the bounds may have the wrong
4795 -- type if they come from the original type definition. Also this
4796 -- way we get all the processing above for an explicit range.
4798 -- Don't do this for predicated types, since in this case we
4799 -- want to check the predicate!
4801 elsif Is_Scalar_Type (Typ) then
4802 if No (Predicate_Function (Typ)) then
4806 Make_Attribute_Reference (Loc,
4807 Attribute_Name => Name_First,
4808 Prefix => New_Reference_To (Typ, Loc)),
4811 Make_Attribute_Reference (Loc,
4812 Attribute_Name => Name_Last,
4813 Prefix => New_Reference_To (Typ, Loc))));
4814 Analyze_And_Resolve (N, Restyp);
4819 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4820 -- a membership test if the subtype mark denotes a constrained
4821 -- Unchecked_Union subtype and the expression lacks inferable
4824 elsif Is_Unchecked_Union (Base_Type (Typ))
4825 and then Is_Constrained (Typ)
4826 and then not Has_Inferable_Discriminants (Lop)
4829 Make_Raise_Program_Error (Loc,
4830 Reason => PE_Unchecked_Union_Restriction));
4832 -- Prevent Gigi from generating incorrect code by rewriting the
4835 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4839 -- Here we have a non-scalar type
4842 Typ := Designated_Type (Typ);
4845 if not Is_Constrained (Typ) then
4846 Rewrite (N, New_Reference_To (Standard_True, Loc));
4847 Analyze_And_Resolve (N, Restyp);
4849 -- For the constrained array case, we have to check the subscripts
4850 -- for an exact match if the lengths are non-zero (the lengths
4851 -- must match in any case).
4853 elsif Is_Array_Type (Typ) then
4854 Check_Subscripts : declare
4855 function Build_Attribute_Reference
4858 Dim : Nat) return Node_Id;
4859 -- Build attribute reference E'Nam (Dim)
4861 -------------------------------
4862 -- Build_Attribute_Reference --
4863 -------------------------------
4865 function Build_Attribute_Reference
4868 Dim : Nat) return Node_Id
4872 Make_Attribute_Reference (Loc,
4874 Attribute_Name => Nam,
4875 Expressions => New_List (
4876 Make_Integer_Literal (Loc, Dim)));
4877 end Build_Attribute_Reference;
4879 -- Start of processing for Check_Subscripts
4882 for J in 1 .. Number_Dimensions (Typ) loop
4883 Evolve_And_Then (Cond,
4886 Build_Attribute_Reference
4887 (Duplicate_Subexpr_No_Checks (Obj),
4890 Build_Attribute_Reference
4891 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
4893 Evolve_And_Then (Cond,
4896 Build_Attribute_Reference
4897 (Duplicate_Subexpr_No_Checks (Obj),
4900 Build_Attribute_Reference
4901 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
4910 Right_Opnd => Make_Null (Loc)),
4911 Right_Opnd => Cond);
4915 Analyze_And_Resolve (N, Restyp);
4916 end Check_Subscripts;
4918 -- These are the cases where constraint checks may be required,
4919 -- e.g. records with possible discriminants
4922 -- Expand the test into a series of discriminant comparisons.
4923 -- The expression that is built is the negation of the one that
4924 -- is used for checking discriminant constraints.
4926 Obj := Relocate_Node (Left_Opnd (N));
4928 if Has_Discriminants (Typ) then
4929 Cond := Make_Op_Not (Loc,
4930 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
4933 Cond := Make_Or_Else (Loc,
4937 Right_Opnd => Make_Null (Loc)),
4938 Right_Opnd => Cond);
4942 Cond := New_Occurrence_Of (Standard_True, Loc);
4946 Analyze_And_Resolve (N, Restyp);
4951 -- At this point, we have done the processing required for the basic
4952 -- membership test, but not yet dealt with the predicate.
4956 -- If a predicate is present, then we do the predicate test, but we
4957 -- most certainly want to omit this if we are within the predicate
4958 -- function itself, since otherwise we have an infinite recursion!
4961 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
4965 and then Current_Scope /= PFunc
4969 Left_Opnd => Relocate_Node (N),
4970 Right_Opnd => Make_Predicate_Call (Rtyp, Lop)));
4972 -- Analyze new expression, mark left operand as analyzed to
4973 -- avoid infinite recursion adding predicate calls.
4975 Set_Analyzed (Left_Opnd (N));
4976 Analyze_And_Resolve (N, Standard_Boolean);
4978 -- All done, skip attempt at compile time determination of result
4985 --------------------------------
4986 -- Expand_N_Indexed_Component --
4987 --------------------------------
4989 procedure Expand_N_Indexed_Component (N : Node_Id) is
4990 Loc : constant Source_Ptr := Sloc (N);
4991 Typ : constant Entity_Id := Etype (N);
4992 P : constant Node_Id := Prefix (N);
4993 T : constant Entity_Id := Etype (P);
4996 -- A special optimization, if we have an indexed component that is
4997 -- selecting from a slice, then we can eliminate the slice, since, for
4998 -- example, x (i .. j)(k) is identical to x(k). The only difference is
4999 -- the range check required by the slice. The range check for the slice
5000 -- itself has already been generated. The range check for the
5001 -- subscripting operation is ensured by converting the subject to
5002 -- the subtype of the slice.
5004 -- This optimization not only generates better code, avoiding slice
5005 -- messing especially in the packed case, but more importantly bypasses
5006 -- some problems in handling this peculiar case, for example, the issue
5007 -- of dealing specially with object renamings.
5009 if Nkind (P) = N_Slice then
5011 Make_Indexed_Component (Loc,
5012 Prefix => Prefix (P),
5013 Expressions => New_List (
5015 (Etype (First_Index (Etype (P))),
5016 First (Expressions (N))))));
5017 Analyze_And_Resolve (N, Typ);
5021 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
5022 -- function, then additional actuals must be passed.
5024 if Ada_Version >= Ada_2005
5025 and then Is_Build_In_Place_Function_Call (P)
5027 Make_Build_In_Place_Call_In_Anonymous_Context (P);
5030 -- If the prefix is an access type, then we unconditionally rewrite if
5031 -- as an explicit dereference. This simplifies processing for several
5032 -- cases, including packed array cases and certain cases in which checks
5033 -- must be generated. We used to try to do this only when it was
5034 -- necessary, but it cleans up the code to do it all the time.
5036 if Is_Access_Type (T) then
5037 Insert_Explicit_Dereference (P);
5038 Analyze_And_Resolve (P, Designated_Type (T));
5041 -- Generate index and validity checks
5043 Generate_Index_Checks (N);
5045 if Validity_Checks_On and then Validity_Check_Subscripts then
5046 Apply_Subscript_Validity_Checks (N);
5049 -- All done for the non-packed case
5051 if not Is_Packed (Etype (Prefix (N))) then
5055 -- For packed arrays that are not bit-packed (i.e. the case of an array
5056 -- with one or more index types with a non-contiguous enumeration type),
5057 -- we can always use the normal packed element get circuit.
5059 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
5060 Expand_Packed_Element_Reference (N);
5064 -- For a reference to a component of a bit packed array, we have to
5065 -- convert it to a reference to the corresponding Packed_Array_Type.
5066 -- We only want to do this for simple references, and not for:
5068 -- Left side of assignment, or prefix of left side of assignment, or
5069 -- prefix of the prefix, to handle packed arrays of packed arrays,
5070 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
5072 -- Renaming objects in renaming associations
5073 -- This case is handled when a use of the renamed variable occurs
5075 -- Actual parameters for a procedure call
5076 -- This case is handled in Exp_Ch6.Expand_Actuals
5078 -- The second expression in a 'Read attribute reference
5080 -- The prefix of an address or bit or size attribute reference
5082 -- The following circuit detects these exceptions
5085 Child : Node_Id := N;
5086 Parnt : Node_Id := Parent (N);
5090 if Nkind (Parnt) = N_Unchecked_Expression then
5093 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
5094 N_Procedure_Call_Statement)
5095 or else (Nkind (Parnt) = N_Parameter_Association
5097 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
5101 elsif Nkind (Parnt) = N_Attribute_Reference
5102 and then (Attribute_Name (Parnt) = Name_Address
5104 Attribute_Name (Parnt) = Name_Bit
5106 Attribute_Name (Parnt) = Name_Size)
5107 and then Prefix (Parnt) = Child
5111 elsif Nkind (Parnt) = N_Assignment_Statement
5112 and then Name (Parnt) = Child
5116 -- If the expression is an index of an indexed component, it must
5117 -- be expanded regardless of context.
5119 elsif Nkind (Parnt) = N_Indexed_Component
5120 and then Child /= Prefix (Parnt)
5122 Expand_Packed_Element_Reference (N);
5125 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
5126 and then Name (Parent (Parnt)) = Parnt
5130 elsif Nkind (Parnt) = N_Attribute_Reference
5131 and then Attribute_Name (Parnt) = Name_Read
5132 and then Next (First (Expressions (Parnt))) = Child
5136 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
5137 and then Prefix (Parnt) = Child
5142 Expand_Packed_Element_Reference (N);
5146 -- Keep looking up tree for unchecked expression, or if we are the
5147 -- prefix of a possible assignment left side.
5150 Parnt := Parent (Child);
5153 end Expand_N_Indexed_Component;
5155 ---------------------
5156 -- Expand_N_Not_In --
5157 ---------------------
5159 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
5160 -- can be done. This avoids needing to duplicate this expansion code.
5162 procedure Expand_N_Not_In (N : Node_Id) is
5163 Loc : constant Source_Ptr := Sloc (N);
5164 Typ : constant Entity_Id := Etype (N);
5165 Cfs : constant Boolean := Comes_From_Source (N);
5172 Left_Opnd => Left_Opnd (N),
5173 Right_Opnd => Right_Opnd (N))));
5175 -- If this is a set membership, preserve list of alternatives
5177 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
5179 -- We want this to appear as coming from source if original does (see
5180 -- transformations in Expand_N_In).
5182 Set_Comes_From_Source (N, Cfs);
5183 Set_Comes_From_Source (Right_Opnd (N), Cfs);
5185 -- Now analyze transformed node
5187 Analyze_And_Resolve (N, Typ);
5188 end Expand_N_Not_In;
5194 -- The only replacement required is for the case of a null of a type that
5195 -- is an access to protected subprogram, or a subtype thereof. We represent
5196 -- such access values as a record, and so we must replace the occurrence of
5197 -- null by the equivalent record (with a null address and a null pointer in
5198 -- it), so that the backend creates the proper value.
5200 procedure Expand_N_Null (N : Node_Id) is
5201 Loc : constant Source_Ptr := Sloc (N);
5202 Typ : constant Entity_Id := Base_Type (Etype (N));
5206 if Is_Access_Protected_Subprogram_Type (Typ) then
5208 Make_Aggregate (Loc,
5209 Expressions => New_List (
5210 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
5214 Analyze_And_Resolve (N, Equivalent_Type (Typ));
5216 -- For subsequent semantic analysis, the node must retain its type.
5217 -- Gigi in any case replaces this type by the corresponding record
5218 -- type before processing the node.
5224 when RE_Not_Available =>
5228 ---------------------
5229 -- Expand_N_Op_Abs --
5230 ---------------------
5232 procedure Expand_N_Op_Abs (N : Node_Id) is
5233 Loc : constant Source_Ptr := Sloc (N);
5234 Expr : constant Node_Id := Right_Opnd (N);
5237 Unary_Op_Validity_Checks (N);
5239 -- Deal with software overflow checking
5241 if not Backend_Overflow_Checks_On_Target
5242 and then Is_Signed_Integer_Type (Etype (N))
5243 and then Do_Overflow_Check (N)
5245 -- The only case to worry about is when the argument is equal to the
5246 -- largest negative number, so what we do is to insert the check:
5248 -- [constraint_error when Expr = typ'Base'First]
5250 -- with the usual Duplicate_Subexpr use coding for expr
5253 Make_Raise_Constraint_Error (Loc,
5256 Left_Opnd => Duplicate_Subexpr (Expr),
5258 Make_Attribute_Reference (Loc,
5260 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
5261 Attribute_Name => Name_First)),
5262 Reason => CE_Overflow_Check_Failed));
5265 -- Vax floating-point types case
5267 if Vax_Float (Etype (N)) then
5268 Expand_Vax_Arith (N);
5270 end Expand_N_Op_Abs;
5272 ---------------------
5273 -- Expand_N_Op_Add --
5274 ---------------------
5276 procedure Expand_N_Op_Add (N : Node_Id) is
5277 Typ : constant Entity_Id := Etype (N);
5280 Binary_Op_Validity_Checks (N);
5282 -- N + 0 = 0 + N = N for integer types
5284 if Is_Integer_Type (Typ) then
5285 if Compile_Time_Known_Value (Right_Opnd (N))
5286 and then Expr_Value (Right_Opnd (N)) = Uint_0
5288 Rewrite (N, Left_Opnd (N));
5291 elsif Compile_Time_Known_Value (Left_Opnd (N))
5292 and then Expr_Value (Left_Opnd (N)) = Uint_0
5294 Rewrite (N, Right_Opnd (N));
5299 -- Arithmetic overflow checks for signed integer/fixed point types
5301 if Is_Signed_Integer_Type (Typ)
5302 or else Is_Fixed_Point_Type (Typ)
5304 Apply_Arithmetic_Overflow_Check (N);
5307 -- Vax floating-point types case
5309 elsif Vax_Float (Typ) then
5310 Expand_Vax_Arith (N);
5312 end Expand_N_Op_Add;
5314 ---------------------
5315 -- Expand_N_Op_And --
5316 ---------------------
5318 procedure Expand_N_Op_And (N : Node_Id) is
5319 Typ : constant Entity_Id := Etype (N);
5322 Binary_Op_Validity_Checks (N);
5324 if Is_Array_Type (Etype (N)) then
5325 Expand_Boolean_Operator (N);
5327 elsif Is_Boolean_Type (Etype (N)) then
5329 -- Replace AND by AND THEN if Short_Circuit_And_Or active and the
5330 -- type is standard Boolean (do not mess with AND that uses a non-
5331 -- standard Boolean type, because something strange is going on).
5333 if Short_Circuit_And_Or and then Typ = Standard_Boolean then
5335 Make_And_Then (Sloc (N),
5336 Left_Opnd => Relocate_Node (Left_Opnd (N)),
5337 Right_Opnd => Relocate_Node (Right_Opnd (N))));
5338 Analyze_And_Resolve (N, Typ);
5340 -- Otherwise, adjust conditions
5343 Adjust_Condition (Left_Opnd (N));
5344 Adjust_Condition (Right_Opnd (N));
5345 Set_Etype (N, Standard_Boolean);
5346 Adjust_Result_Type (N, Typ);
5349 elsif Is_Intrinsic_Subprogram (Entity (N)) then
5350 Expand_Intrinsic_Call (N, Entity (N));
5353 end Expand_N_Op_And;
5355 ------------------------
5356 -- Expand_N_Op_Concat --
5357 ------------------------
5359 procedure Expand_N_Op_Concat (N : Node_Id) is
5361 -- List of operands to be concatenated
5364 -- Node which is to be replaced by the result of concatenating the nodes
5365 -- in the list Opnds.
5368 -- Ensure validity of both operands
5370 Binary_Op_Validity_Checks (N);
5372 -- If we are the left operand of a concatenation higher up the tree,
5373 -- then do nothing for now, since we want to deal with a series of
5374 -- concatenations as a unit.
5376 if Nkind (Parent (N)) = N_Op_Concat
5377 and then N = Left_Opnd (Parent (N))
5382 -- We get here with a concatenation whose left operand may be a
5383 -- concatenation itself with a consistent type. We need to process
5384 -- these concatenation operands from left to right, which means
5385 -- from the deepest node in the tree to the highest node.
5388 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
5389 Cnode := Left_Opnd (Cnode);
5392 -- Now Cnode is the deepest concatenation, and its parents are the
5393 -- concatenation nodes above, so now we process bottom up, doing the
5394 -- operations. We gather a string that is as long as possible up to five
5397 -- The outer loop runs more than once if more than one concatenation
5398 -- type is involved.
5401 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
5402 Set_Parent (Opnds, N);
5404 -- The inner loop gathers concatenation operands
5406 Inner : while Cnode /= N
5407 and then Base_Type (Etype (Cnode)) =
5408 Base_Type (Etype (Parent (Cnode)))
5410 Cnode := Parent (Cnode);
5411 Append (Right_Opnd (Cnode), Opnds);
5414 Expand_Concatenate (Cnode, Opnds);
5416 exit Outer when Cnode = N;
5417 Cnode := Parent (Cnode);
5419 end Expand_N_Op_Concat;
5421 ------------------------
5422 -- Expand_N_Op_Divide --
5423 ------------------------
5425 procedure Expand_N_Op_Divide (N : Node_Id) is
5426 Loc : constant Source_Ptr := Sloc (N);
5427 Lopnd : constant Node_Id := Left_Opnd (N);
5428 Ropnd : constant Node_Id := Right_Opnd (N);
5429 Ltyp : constant Entity_Id := Etype (Lopnd);
5430 Rtyp : constant Entity_Id := Etype (Ropnd);
5431 Typ : Entity_Id := Etype (N);
5432 Rknow : constant Boolean := Is_Integer_Type (Typ)
5434 Compile_Time_Known_Value (Ropnd);
5438 Binary_Op_Validity_Checks (N);
5441 Rval := Expr_Value (Ropnd);
5444 -- N / 1 = N for integer types
5446 if Rknow and then Rval = Uint_1 then
5451 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
5452 -- Is_Power_Of_2_For_Shift is set means that we know that our left
5453 -- operand is an unsigned integer, as required for this to work.
5455 if Nkind (Ropnd) = N_Op_Expon
5456 and then Is_Power_Of_2_For_Shift (Ropnd)
5458 -- We cannot do this transformation in configurable run time mode if we
5459 -- have 64-bit integers and long shifts are not available.
5463 or else Support_Long_Shifts_On_Target)
5466 Make_Op_Shift_Right (Loc,
5469 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
5470 Analyze_And_Resolve (N, Typ);
5474 -- Do required fixup of universal fixed operation
5476 if Typ = Universal_Fixed then
5477 Fixup_Universal_Fixed_Operation (N);
5481 -- Divisions with fixed-point results
5483 if Is_Fixed_Point_Type (Typ) then
5485 -- No special processing if Treat_Fixed_As_Integer is set, since
5486 -- from a semantic point of view such operations are simply integer
5487 -- operations and will be treated that way.
5489 if not Treat_Fixed_As_Integer (N) then
5490 if Is_Integer_Type (Rtyp) then
5491 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
5493 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
5497 -- Other cases of division of fixed-point operands. Again we exclude the
5498 -- case where Treat_Fixed_As_Integer is set.
5500 elsif (Is_Fixed_Point_Type (Ltyp) or else
5501 Is_Fixed_Point_Type (Rtyp))
5502 and then not Treat_Fixed_As_Integer (N)
5504 if Is_Integer_Type (Typ) then
5505 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
5507 pragma Assert (Is_Floating_Point_Type (Typ));
5508 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
5511 -- Mixed-mode operations can appear in a non-static universal context,
5512 -- in which case the integer argument must be converted explicitly.
5514 elsif Typ = Universal_Real
5515 and then Is_Integer_Type (Rtyp)
5518 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
5520 Analyze_And_Resolve (Ropnd, Universal_Real);
5522 elsif Typ = Universal_Real
5523 and then Is_Integer_Type (Ltyp)
5526 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
5528 Analyze_And_Resolve (Lopnd, Universal_Real);
5530 -- Non-fixed point cases, do integer zero divide and overflow checks
5532 elsif Is_Integer_Type (Typ) then
5533 Apply_Divide_Check (N);
5535 -- Check for 64-bit division available, or long shifts if the divisor
5536 -- is a small power of 2 (since such divides will be converted into
5539 if Esize (Ltyp) > 32
5540 and then not Support_64_Bit_Divides_On_Target
5543 or else not Support_Long_Shifts_On_Target
5544 or else (Rval /= Uint_2 and then
5545 Rval /= Uint_4 and then
5546 Rval /= Uint_8 and then
5547 Rval /= Uint_16 and then
5548 Rval /= Uint_32 and then
5551 Error_Msg_CRT ("64-bit division", N);
5554 -- Deal with Vax_Float
5556 elsif Vax_Float (Typ) then
5557 Expand_Vax_Arith (N);
5560 end Expand_N_Op_Divide;
5562 --------------------
5563 -- Expand_N_Op_Eq --
5564 --------------------
5566 procedure Expand_N_Op_Eq (N : Node_Id) is
5567 Loc : constant Source_Ptr := Sloc (N);
5568 Typ : constant Entity_Id := Etype (N);
5569 Lhs : constant Node_Id := Left_Opnd (N);
5570 Rhs : constant Node_Id := Right_Opnd (N);
5571 Bodies : constant List_Id := New_List;
5572 A_Typ : constant Entity_Id := Etype (Lhs);
5574 Typl : Entity_Id := A_Typ;
5575 Op_Name : Entity_Id;
5578 procedure Build_Equality_Call (Eq : Entity_Id);
5579 -- If a constructed equality exists for the type or for its parent,
5580 -- build and analyze call, adding conversions if the operation is
5583 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
5584 -- Determines whether a type has a subcomponent of an unconstrained
5585 -- Unchecked_Union subtype. Typ is a record type.
5587 -------------------------
5588 -- Build_Equality_Call --
5589 -------------------------
5591 procedure Build_Equality_Call (Eq : Entity_Id) is
5592 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
5593 L_Exp : Node_Id := Relocate_Node (Lhs);
5594 R_Exp : Node_Id := Relocate_Node (Rhs);
5597 if Base_Type (Op_Type) /= Base_Type (A_Typ)
5598 and then not Is_Class_Wide_Type (A_Typ)
5600 L_Exp := OK_Convert_To (Op_Type, L_Exp);
5601 R_Exp := OK_Convert_To (Op_Type, R_Exp);
5604 -- If we have an Unchecked_Union, we need to add the inferred
5605 -- discriminant values as actuals in the function call. At this
5606 -- point, the expansion has determined that both operands have
5607 -- inferable discriminants.
5609 if Is_Unchecked_Union (Op_Type) then
5611 Lhs_Type : constant Node_Id := Etype (L_Exp);
5612 Rhs_Type : constant Node_Id := Etype (R_Exp);
5613 Lhs_Discr_Val : Node_Id;
5614 Rhs_Discr_Val : Node_Id;
5617 -- Per-object constrained selected components require special
5618 -- attention. If the enclosing scope of the component is an
5619 -- Unchecked_Union, we cannot reference its discriminants
5620 -- directly. This is why we use the two extra parameters of
5621 -- the equality function of the enclosing Unchecked_Union.
5623 -- type UU_Type (Discr : Integer := 0) is
5626 -- pragma Unchecked_Union (UU_Type);
5628 -- 1. Unchecked_Union enclosing record:
5630 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
5632 -- Comp : UU_Type (Discr);
5634 -- end Enclosing_UU_Type;
5635 -- pragma Unchecked_Union (Enclosing_UU_Type);
5637 -- Obj1 : Enclosing_UU_Type;
5638 -- Obj2 : Enclosing_UU_Type (1);
5640 -- [. . .] Obj1 = Obj2 [. . .]
5644 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
5646 -- A and B are the formal parameters of the equality function
5647 -- of Enclosing_UU_Type. The function always has two extra
5648 -- formals to capture the inferred discriminant values.
5650 -- 2. Non-Unchecked_Union enclosing record:
5653 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
5656 -- Comp : UU_Type (Discr);
5658 -- end Enclosing_Non_UU_Type;
5660 -- Obj1 : Enclosing_Non_UU_Type;
5661 -- Obj2 : Enclosing_Non_UU_Type (1);
5663 -- ... Obj1 = Obj2 ...
5667 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
5668 -- obj1.discr, obj2.discr)) then
5670 -- In this case we can directly reference the discriminants of
5671 -- the enclosing record.
5675 if Nkind (Lhs) = N_Selected_Component
5676 and then Has_Per_Object_Constraint
5677 (Entity (Selector_Name (Lhs)))
5679 -- Enclosing record is an Unchecked_Union, use formal A
5681 if Is_Unchecked_Union
5682 (Scope (Entity (Selector_Name (Lhs))))
5684 Lhs_Discr_Val := Make_Identifier (Loc, Name_A);
5686 -- Enclosing record is of a non-Unchecked_Union type, it is
5687 -- possible to reference the discriminant.
5691 Make_Selected_Component (Loc,
5692 Prefix => Prefix (Lhs),
5695 (Get_Discriminant_Value
5696 (First_Discriminant (Lhs_Type),
5698 Stored_Constraint (Lhs_Type))));
5701 -- Comment needed here ???
5704 -- Infer the discriminant value
5708 (Get_Discriminant_Value
5709 (First_Discriminant (Lhs_Type),
5711 Stored_Constraint (Lhs_Type)));
5716 if Nkind (Rhs) = N_Selected_Component
5717 and then Has_Per_Object_Constraint
5718 (Entity (Selector_Name (Rhs)))
5720 if Is_Unchecked_Union
5721 (Scope (Entity (Selector_Name (Rhs))))
5723 Rhs_Discr_Val := Make_Identifier (Loc, Name_B);
5727 Make_Selected_Component (Loc,
5728 Prefix => Prefix (Rhs),
5730 New_Copy (Get_Discriminant_Value (
5731 First_Discriminant (Rhs_Type),
5733 Stored_Constraint (Rhs_Type))));
5738 New_Copy (Get_Discriminant_Value (
5739 First_Discriminant (Rhs_Type),
5741 Stored_Constraint (Rhs_Type)));
5746 Make_Function_Call (Loc,
5747 Name => New_Reference_To (Eq, Loc),
5748 Parameter_Associations => New_List (
5755 -- Normal case, not an unchecked union
5759 Make_Function_Call (Loc,
5760 Name => New_Reference_To (Eq, Loc),
5761 Parameter_Associations => New_List (L_Exp, R_Exp)));
5764 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5765 end Build_Equality_Call;
5767 ------------------------------------
5768 -- Has_Unconstrained_UU_Component --
5769 ------------------------------------
5771 function Has_Unconstrained_UU_Component
5772 (Typ : Node_Id) return Boolean
5774 Tdef : constant Node_Id :=
5775 Type_Definition (Declaration_Node (Base_Type (Typ)));
5779 function Component_Is_Unconstrained_UU
5780 (Comp : Node_Id) return Boolean;
5781 -- Determines whether the subtype of the component is an
5782 -- unconstrained Unchecked_Union.
5784 function Variant_Is_Unconstrained_UU
5785 (Variant : Node_Id) return Boolean;
5786 -- Determines whether a component of the variant has an unconstrained
5787 -- Unchecked_Union subtype.
5789 -----------------------------------
5790 -- Component_Is_Unconstrained_UU --
5791 -----------------------------------
5793 function Component_Is_Unconstrained_UU
5794 (Comp : Node_Id) return Boolean
5797 if Nkind (Comp) /= N_Component_Declaration then
5802 Sindic : constant Node_Id :=
5803 Subtype_Indication (Component_Definition (Comp));
5806 -- Unconstrained nominal type. In the case of a constraint
5807 -- present, the node kind would have been N_Subtype_Indication.
5809 if Nkind (Sindic) = N_Identifier then
5810 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
5815 end Component_Is_Unconstrained_UU;
5817 ---------------------------------
5818 -- Variant_Is_Unconstrained_UU --
5819 ---------------------------------
5821 function Variant_Is_Unconstrained_UU
5822 (Variant : Node_Id) return Boolean
5824 Clist : constant Node_Id := Component_List (Variant);
5827 if Is_Empty_List (Component_Items (Clist)) then
5831 -- We only need to test one component
5834 Comp : Node_Id := First (Component_Items (Clist));
5837 while Present (Comp) loop
5838 if Component_Is_Unconstrained_UU (Comp) then
5846 -- None of the components withing the variant were of
5847 -- unconstrained Unchecked_Union type.
5850 end Variant_Is_Unconstrained_UU;
5852 -- Start of processing for Has_Unconstrained_UU_Component
5855 if Null_Present (Tdef) then
5859 Clist := Component_List (Tdef);
5860 Vpart := Variant_Part (Clist);
5862 -- Inspect available components
5864 if Present (Component_Items (Clist)) then
5866 Comp : Node_Id := First (Component_Items (Clist));
5869 while Present (Comp) loop
5871 -- One component is sufficient
5873 if Component_Is_Unconstrained_UU (Comp) then
5882 -- Inspect available components withing variants
5884 if Present (Vpart) then
5886 Variant : Node_Id := First (Variants (Vpart));
5889 while Present (Variant) loop
5891 -- One component within a variant is sufficient
5893 if Variant_Is_Unconstrained_UU (Variant) then
5902 -- Neither the available components, nor the components inside the
5903 -- variant parts were of an unconstrained Unchecked_Union subtype.
5906 end Has_Unconstrained_UU_Component;
5908 -- Start of processing for Expand_N_Op_Eq
5911 Binary_Op_Validity_Checks (N);
5913 if Ekind (Typl) = E_Private_Type then
5914 Typl := Underlying_Type (Typl);
5915 elsif Ekind (Typl) = E_Private_Subtype then
5916 Typl := Underlying_Type (Base_Type (Typl));
5921 -- It may happen in error situations that the underlying type is not
5922 -- set. The error will be detected later, here we just defend the
5929 Typl := Base_Type (Typl);
5931 -- Boolean types (requiring handling of non-standard case)
5933 if Is_Boolean_Type (Typl) then
5934 Adjust_Condition (Left_Opnd (N));
5935 Adjust_Condition (Right_Opnd (N));
5936 Set_Etype (N, Standard_Boolean);
5937 Adjust_Result_Type (N, Typ);
5941 elsif Is_Array_Type (Typl) then
5943 -- If we are doing full validity checking, and it is possible for the
5944 -- array elements to be invalid then expand out array comparisons to
5945 -- make sure that we check the array elements.
5947 if Validity_Check_Operands
5948 and then not Is_Known_Valid (Component_Type (Typl))
5951 Save_Force_Validity_Checks : constant Boolean :=
5952 Force_Validity_Checks;
5954 Force_Validity_Checks := True;
5956 Expand_Array_Equality
5958 Relocate_Node (Lhs),
5959 Relocate_Node (Rhs),
5962 Insert_Actions (N, Bodies);
5963 Analyze_And_Resolve (N, Standard_Boolean);
5964 Force_Validity_Checks := Save_Force_Validity_Checks;
5967 -- Packed case where both operands are known aligned
5969 elsif Is_Bit_Packed_Array (Typl)
5970 and then not Is_Possibly_Unaligned_Object (Lhs)
5971 and then not Is_Possibly_Unaligned_Object (Rhs)
5973 Expand_Packed_Eq (N);
5975 -- Where the component type is elementary we can use a block bit
5976 -- comparison (if supported on the target) exception in the case
5977 -- of floating-point (negative zero issues require element by
5978 -- element comparison), and atomic types (where we must be sure
5979 -- to load elements independently) and possibly unaligned arrays.
5981 elsif Is_Elementary_Type (Component_Type (Typl))
5982 and then not Is_Floating_Point_Type (Component_Type (Typl))
5983 and then not Is_Atomic (Component_Type (Typl))
5984 and then not Is_Possibly_Unaligned_Object (Lhs)
5985 and then not Is_Possibly_Unaligned_Object (Rhs)
5986 and then Support_Composite_Compare_On_Target
5990 -- For composite and floating-point cases, expand equality loop to
5991 -- make sure of using proper comparisons for tagged types, and
5992 -- correctly handling the floating-point case.
5996 Expand_Array_Equality
5998 Relocate_Node (Lhs),
5999 Relocate_Node (Rhs),
6002 Insert_Actions (N, Bodies, Suppress => All_Checks);
6003 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6008 elsif Is_Record_Type (Typl) then
6010 -- For tagged types, use the primitive "="
6012 if Is_Tagged_Type (Typl) then
6014 -- No need to do anything else compiling under restriction
6015 -- No_Dispatching_Calls. During the semantic analysis we
6016 -- already notified such violation.
6018 if Restriction_Active (No_Dispatching_Calls) then
6022 -- If this is derived from an untagged private type completed with
6023 -- a tagged type, it does not have a full view, so we use the
6024 -- primitive operations of the private type. This check should no
6025 -- longer be necessary when these types get their full views???
6027 if Is_Private_Type (A_Typ)
6028 and then not Is_Tagged_Type (A_Typ)
6029 and then Is_Derived_Type (A_Typ)
6030 and then No (Full_View (A_Typ))
6032 -- Search for equality operation, checking that the operands
6033 -- have the same type. Note that we must find a matching entry,
6034 -- or something is very wrong!
6036 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
6038 while Present (Prim) loop
6039 exit when Chars (Node (Prim)) = Name_Op_Eq
6040 and then Etype (First_Formal (Node (Prim))) =
6041 Etype (Next_Formal (First_Formal (Node (Prim))))
6043 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
6048 pragma Assert (Present (Prim));
6049 Op_Name := Node (Prim);
6051 -- Find the type's predefined equality or an overriding
6052 -- user- defined equality. The reason for not simply calling
6053 -- Find_Prim_Op here is that there may be a user-defined
6054 -- overloaded equality op that precedes the equality that we want,
6055 -- so we have to explicitly search (e.g., there could be an
6056 -- equality with two different parameter types).
6059 if Is_Class_Wide_Type (Typl) then
6060 Typl := Root_Type (Typl);
6063 Prim := First_Elmt (Primitive_Operations (Typl));
6064 while Present (Prim) loop
6065 exit when Chars (Node (Prim)) = Name_Op_Eq
6066 and then Etype (First_Formal (Node (Prim))) =
6067 Etype (Next_Formal (First_Formal (Node (Prim))))
6069 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
6074 pragma Assert (Present (Prim));
6075 Op_Name := Node (Prim);
6078 Build_Equality_Call (Op_Name);
6080 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
6081 -- predefined equality operator for a type which has a subcomponent
6082 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
6084 elsif Has_Unconstrained_UU_Component (Typl) then
6086 Make_Raise_Program_Error (Loc,
6087 Reason => PE_Unchecked_Union_Restriction));
6089 -- Prevent Gigi from generating incorrect code by rewriting the
6090 -- equality as a standard False.
6093 New_Occurrence_Of (Standard_False, Loc));
6095 elsif Is_Unchecked_Union (Typl) then
6097 -- If we can infer the discriminants of the operands, we make a
6098 -- call to the TSS equality function.
6100 if Has_Inferable_Discriminants (Lhs)
6102 Has_Inferable_Discriminants (Rhs)
6105 (TSS (Root_Type (Typl), TSS_Composite_Equality));
6108 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
6109 -- the predefined equality operator for an Unchecked_Union type
6110 -- if either of the operands lack inferable discriminants.
6113 Make_Raise_Program_Error (Loc,
6114 Reason => PE_Unchecked_Union_Restriction));
6116 -- Prevent Gigi from generating incorrect code by rewriting
6117 -- the equality as a standard False.
6120 New_Occurrence_Of (Standard_False, Loc));
6124 -- If a type support function is present (for complex cases), use it
6126 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
6128 (TSS (Root_Type (Typl), TSS_Composite_Equality));
6130 -- Otherwise expand the component by component equality. Note that
6131 -- we never use block-bit comparisons for records, because of the
6132 -- problems with gaps. The backend will often be able to recombine
6133 -- the separate comparisons that we generate here.
6136 Remove_Side_Effects (Lhs);
6137 Remove_Side_Effects (Rhs);
6139 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
6141 Insert_Actions (N, Bodies, Suppress => All_Checks);
6142 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6146 -- Test if result is known at compile time
6148 Rewrite_Comparison (N);
6150 -- If we still have comparison for Vax_Float, process it
6152 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
6153 Expand_Vax_Comparison (N);
6157 Optimize_Length_Comparison (N);
6160 -----------------------
6161 -- Expand_N_Op_Expon --
6162 -----------------------
6164 procedure Expand_N_Op_Expon (N : Node_Id) is
6165 Loc : constant Source_Ptr := Sloc (N);
6166 Typ : constant Entity_Id := Etype (N);
6167 Rtyp : constant Entity_Id := Root_Type (Typ);
6168 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
6169 Bastyp : constant Node_Id := Etype (Base);
6170 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
6171 Exptyp : constant Entity_Id := Etype (Exp);
6172 Ovflo : constant Boolean := Do_Overflow_Check (N);
6181 Binary_Op_Validity_Checks (N);
6183 -- If either operand is of a private type, then we have the use of an
6184 -- intrinsic operator, and we get rid of the privateness, by using root
6185 -- types of underlying types for the actual operation. Otherwise the
6186 -- private types will cause trouble if we expand multiplications or
6187 -- shifts etc. We also do this transformation if the result type is
6188 -- different from the base type.
6190 if Is_Private_Type (Etype (Base))
6192 Is_Private_Type (Typ)
6194 Is_Private_Type (Exptyp)
6196 Rtyp /= Root_Type (Bastyp)
6199 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
6200 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
6204 Unchecked_Convert_To (Typ,
6206 Left_Opnd => Unchecked_Convert_To (Bt, Base),
6207 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
6208 Analyze_And_Resolve (N, Typ);
6213 -- Test for case of known right argument
6215 if Compile_Time_Known_Value (Exp) then
6216 Expv := Expr_Value (Exp);
6218 -- We only fold small non-negative exponents. You might think we
6219 -- could fold small negative exponents for the real case, but we
6220 -- can't because we are required to raise Constraint_Error for
6221 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
6222 -- See ACVC test C4A012B.
6224 if Expv >= 0 and then Expv <= 4 then
6226 -- X ** 0 = 1 (or 1.0)
6230 -- Call Remove_Side_Effects to ensure that any side effects
6231 -- in the ignored left operand (in particular function calls
6232 -- to user defined functions) are properly executed.
6234 Remove_Side_Effects (Base);
6236 if Ekind (Typ) in Integer_Kind then
6237 Xnode := Make_Integer_Literal (Loc, Intval => 1);
6239 Xnode := Make_Real_Literal (Loc, Ureal_1);
6251 Make_Op_Multiply (Loc,
6252 Left_Opnd => Duplicate_Subexpr (Base),
6253 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
6255 -- X ** 3 = X * X * X
6259 Make_Op_Multiply (Loc,
6261 Make_Op_Multiply (Loc,
6262 Left_Opnd => Duplicate_Subexpr (Base),
6263 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
6264 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
6267 -- En : constant base'type := base * base;
6272 Temp := Make_Temporary (Loc, 'E', Base);
6274 Insert_Actions (N, New_List (
6275 Make_Object_Declaration (Loc,
6276 Defining_Identifier => Temp,
6277 Constant_Present => True,
6278 Object_Definition => New_Reference_To (Typ, Loc),
6280 Make_Op_Multiply (Loc,
6281 Left_Opnd => Duplicate_Subexpr (Base),
6282 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
6285 Make_Op_Multiply (Loc,
6286 Left_Opnd => New_Reference_To (Temp, Loc),
6287 Right_Opnd => New_Reference_To (Temp, Loc));
6291 Analyze_And_Resolve (N, Typ);
6296 -- Case of (2 ** expression) appearing as an argument of an integer
6297 -- multiplication, or as the right argument of a division of a non-
6298 -- negative integer. In such cases we leave the node untouched, setting
6299 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
6300 -- of the higher level node converts it into a shift.
6302 -- Another case is 2 ** N in any other context. We simply convert
6303 -- this to 1 * 2 ** N, and then the above transformation applies.
6305 -- Note: this transformation is not applicable for a modular type with
6306 -- a non-binary modulus in the multiplication case, since we get a wrong
6307 -- result if the shift causes an overflow before the modular reduction.
6309 if Nkind (Base) = N_Integer_Literal
6310 and then Intval (Base) = 2
6311 and then Is_Integer_Type (Root_Type (Exptyp))
6312 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
6313 and then Is_Unsigned_Type (Exptyp)
6316 -- First the multiply and divide cases
6318 if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then
6320 P : constant Node_Id := Parent (N);
6321 L : constant Node_Id := Left_Opnd (P);
6322 R : constant Node_Id := Right_Opnd (P);
6325 if (Nkind (P) = N_Op_Multiply
6326 and then not Non_Binary_Modulus (Typ)
6328 ((Is_Integer_Type (Etype (L)) and then R = N)
6330 (Is_Integer_Type (Etype (R)) and then L = N))
6331 and then not Do_Overflow_Check (P))
6333 (Nkind (P) = N_Op_Divide
6334 and then Is_Integer_Type (Etype (L))
6335 and then Is_Unsigned_Type (Etype (L))
6337 and then not Do_Overflow_Check (P))
6339 Set_Is_Power_Of_2_For_Shift (N);
6344 -- Now the other cases
6346 elsif not Non_Binary_Modulus (Typ) then
6348 Make_Op_Multiply (Loc,
6349 Left_Opnd => Make_Integer_Literal (Loc, 1),
6350 Right_Opnd => Relocate_Node (N)));
6351 Analyze_And_Resolve (N, Typ);
6356 -- Fall through if exponentiation must be done using a runtime routine
6358 -- First deal with modular case
6360 if Is_Modular_Integer_Type (Rtyp) then
6362 -- Non-binary case, we call the special exponentiation routine for
6363 -- the non-binary case, converting the argument to Long_Long_Integer
6364 -- and passing the modulus value. Then the result is converted back
6365 -- to the base type.
6367 if Non_Binary_Modulus (Rtyp) then
6370 Make_Function_Call (Loc,
6371 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
6372 Parameter_Associations => New_List (
6373 Convert_To (Standard_Integer, Base),
6374 Make_Integer_Literal (Loc, Modulus (Rtyp)),
6377 -- Binary case, in this case, we call one of two routines, either the
6378 -- unsigned integer case, or the unsigned long long integer case,
6379 -- with a final "and" operation to do the required mod.
6382 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
6383 Ent := RTE (RE_Exp_Unsigned);
6385 Ent := RTE (RE_Exp_Long_Long_Unsigned);
6392 Make_Function_Call (Loc,
6393 Name => New_Reference_To (Ent, Loc),
6394 Parameter_Associations => New_List (
6395 Convert_To (Etype (First_Formal (Ent)), Base),
6398 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
6402 -- Common exit point for modular type case
6404 Analyze_And_Resolve (N, Typ);
6407 -- Signed integer cases, done using either Integer or Long_Long_Integer.
6408 -- It is not worth having routines for Short_[Short_]Integer, since for
6409 -- most machines it would not help, and it would generate more code that
6410 -- might need certification when a certified run time is required.
6412 -- In the integer cases, we have two routines, one for when overflow
6413 -- checks are required, and one when they are not required, since there
6414 -- is a real gain in omitting checks on many machines.
6416 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
6417 or else (Rtyp = Base_Type (Standard_Long_Integer)
6419 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
6420 or else (Rtyp = Universal_Integer)
6422 Etyp := Standard_Long_Long_Integer;
6425 Rent := RE_Exp_Long_Long_Integer;
6427 Rent := RE_Exn_Long_Long_Integer;
6430 elsif Is_Signed_Integer_Type (Rtyp) then
6431 Etyp := Standard_Integer;
6434 Rent := RE_Exp_Integer;
6436 Rent := RE_Exn_Integer;
6439 -- Floating-point cases, always done using Long_Long_Float. We do not
6440 -- need separate routines for the overflow case here, since in the case
6441 -- of floating-point, we generate infinities anyway as a rule (either
6442 -- that or we automatically trap overflow), and if there is an infinity
6443 -- generated and a range check is required, the check will fail anyway.
6446 pragma Assert (Is_Floating_Point_Type (Rtyp));
6447 Etyp := Standard_Long_Long_Float;
6448 Rent := RE_Exn_Long_Long_Float;
6451 -- Common processing for integer cases and floating-point cases.
6452 -- If we are in the right type, we can call runtime routine directly
6455 and then Rtyp /= Universal_Integer
6456 and then Rtyp /= Universal_Real
6459 Make_Function_Call (Loc,
6460 Name => New_Reference_To (RTE (Rent), Loc),
6461 Parameter_Associations => New_List (Base, Exp)));
6463 -- Otherwise we have to introduce conversions (conversions are also
6464 -- required in the universal cases, since the runtime routine is
6465 -- typed using one of the standard types).
6470 Make_Function_Call (Loc,
6471 Name => New_Reference_To (RTE (Rent), Loc),
6472 Parameter_Associations => New_List (
6473 Convert_To (Etyp, Base),
6477 Analyze_And_Resolve (N, Typ);
6481 when RE_Not_Available =>
6483 end Expand_N_Op_Expon;
6485 --------------------
6486 -- Expand_N_Op_Ge --
6487 --------------------
6489 procedure Expand_N_Op_Ge (N : Node_Id) is
6490 Typ : constant Entity_Id := Etype (N);
6491 Op1 : constant Node_Id := Left_Opnd (N);
6492 Op2 : constant Node_Id := Right_Opnd (N);
6493 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6496 Binary_Op_Validity_Checks (N);
6498 if Is_Array_Type (Typ1) then
6499 Expand_Array_Comparison (N);
6503 if Is_Boolean_Type (Typ1) then
6504 Adjust_Condition (Op1);
6505 Adjust_Condition (Op2);
6506 Set_Etype (N, Standard_Boolean);
6507 Adjust_Result_Type (N, Typ);
6510 Rewrite_Comparison (N);
6512 -- If we still have comparison, and Vax_Float type, process it
6514 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6515 Expand_Vax_Comparison (N);
6519 Optimize_Length_Comparison (N);
6522 --------------------
6523 -- Expand_N_Op_Gt --
6524 --------------------
6526 procedure Expand_N_Op_Gt (N : Node_Id) is
6527 Typ : constant Entity_Id := Etype (N);
6528 Op1 : constant Node_Id := Left_Opnd (N);
6529 Op2 : constant Node_Id := Right_Opnd (N);
6530 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6533 Binary_Op_Validity_Checks (N);
6535 if Is_Array_Type (Typ1) then
6536 Expand_Array_Comparison (N);
6540 if Is_Boolean_Type (Typ1) then
6541 Adjust_Condition (Op1);
6542 Adjust_Condition (Op2);
6543 Set_Etype (N, Standard_Boolean);
6544 Adjust_Result_Type (N, Typ);
6547 Rewrite_Comparison (N);
6549 -- If we still have comparison, and Vax_Float type, process it
6551 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6552 Expand_Vax_Comparison (N);
6556 Optimize_Length_Comparison (N);
6559 --------------------
6560 -- Expand_N_Op_Le --
6561 --------------------
6563 procedure Expand_N_Op_Le (N : Node_Id) is
6564 Typ : constant Entity_Id := Etype (N);
6565 Op1 : constant Node_Id := Left_Opnd (N);
6566 Op2 : constant Node_Id := Right_Opnd (N);
6567 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6570 Binary_Op_Validity_Checks (N);
6572 if Is_Array_Type (Typ1) then
6573 Expand_Array_Comparison (N);
6577 if Is_Boolean_Type (Typ1) then
6578 Adjust_Condition (Op1);
6579 Adjust_Condition (Op2);
6580 Set_Etype (N, Standard_Boolean);
6581 Adjust_Result_Type (N, Typ);
6584 Rewrite_Comparison (N);
6586 -- If we still have comparison, and Vax_Float type, process it
6588 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6589 Expand_Vax_Comparison (N);
6593 Optimize_Length_Comparison (N);
6596 --------------------
6597 -- Expand_N_Op_Lt --
6598 --------------------
6600 procedure Expand_N_Op_Lt (N : Node_Id) is
6601 Typ : constant Entity_Id := Etype (N);
6602 Op1 : constant Node_Id := Left_Opnd (N);
6603 Op2 : constant Node_Id := Right_Opnd (N);
6604 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6607 Binary_Op_Validity_Checks (N);
6609 if Is_Array_Type (Typ1) then
6610 Expand_Array_Comparison (N);
6614 if Is_Boolean_Type (Typ1) then
6615 Adjust_Condition (Op1);
6616 Adjust_Condition (Op2);
6617 Set_Etype (N, Standard_Boolean);
6618 Adjust_Result_Type (N, Typ);
6621 Rewrite_Comparison (N);
6623 -- If we still have comparison, and Vax_Float type, process it
6625 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6626 Expand_Vax_Comparison (N);
6630 Optimize_Length_Comparison (N);
6633 -----------------------
6634 -- Expand_N_Op_Minus --
6635 -----------------------
6637 procedure Expand_N_Op_Minus (N : Node_Id) is
6638 Loc : constant Source_Ptr := Sloc (N);
6639 Typ : constant Entity_Id := Etype (N);
6642 Unary_Op_Validity_Checks (N);
6644 if not Backend_Overflow_Checks_On_Target
6645 and then Is_Signed_Integer_Type (Etype (N))
6646 and then Do_Overflow_Check (N)
6648 -- Software overflow checking expands -expr into (0 - expr)
6651 Make_Op_Subtract (Loc,
6652 Left_Opnd => Make_Integer_Literal (Loc, 0),
6653 Right_Opnd => Right_Opnd (N)));
6655 Analyze_And_Resolve (N, Typ);
6657 -- Vax floating-point types case
6659 elsif Vax_Float (Etype (N)) then
6660 Expand_Vax_Arith (N);
6662 end Expand_N_Op_Minus;
6664 ---------------------
6665 -- Expand_N_Op_Mod --
6666 ---------------------
6668 procedure Expand_N_Op_Mod (N : Node_Id) is
6669 Loc : constant Source_Ptr := Sloc (N);
6670 Typ : constant Entity_Id := Etype (N);
6671 Left : constant Node_Id := Left_Opnd (N);
6672 Right : constant Node_Id := Right_Opnd (N);
6673 DOC : constant Boolean := Do_Overflow_Check (N);
6674 DDC : constant Boolean := Do_Division_Check (N);
6684 pragma Warnings (Off, Lhi);
6687 Binary_Op_Validity_Checks (N);
6689 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
6690 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
6692 -- Convert mod to rem if operands are known non-negative. We do this
6693 -- since it is quite likely that this will improve the quality of code,
6694 -- (the operation now corresponds to the hardware remainder), and it
6695 -- does not seem likely that it could be harmful.
6697 if LOK and then Llo >= 0
6699 ROK and then Rlo >= 0
6702 Make_Op_Rem (Sloc (N),
6703 Left_Opnd => Left_Opnd (N),
6704 Right_Opnd => Right_Opnd (N)));
6706 -- Instead of reanalyzing the node we do the analysis manually. This
6707 -- avoids anomalies when the replacement is done in an instance and
6708 -- is epsilon more efficient.
6710 Set_Entity (N, Standard_Entity (S_Op_Rem));
6712 Set_Do_Overflow_Check (N, DOC);
6713 Set_Do_Division_Check (N, DDC);
6714 Expand_N_Op_Rem (N);
6717 -- Otherwise, normal mod processing
6720 if Is_Integer_Type (Etype (N)) then
6721 Apply_Divide_Check (N);
6724 -- Apply optimization x mod 1 = 0. We don't really need that with
6725 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
6726 -- certainly harmless.
6728 if Is_Integer_Type (Etype (N))
6729 and then Compile_Time_Known_Value (Right)
6730 and then Expr_Value (Right) = Uint_1
6732 -- Call Remove_Side_Effects to ensure that any side effects in
6733 -- the ignored left operand (in particular function calls to
6734 -- user defined functions) are properly executed.
6736 Remove_Side_Effects (Left);
6738 Rewrite (N, Make_Integer_Literal (Loc, 0));
6739 Analyze_And_Resolve (N, Typ);
6743 -- Deal with annoying case of largest negative number remainder
6744 -- minus one. Gigi does not handle this case correctly, because
6745 -- it generates a divide instruction which may trap in this case.
6747 -- In fact the check is quite easy, if the right operand is -1, then
6748 -- the mod value is always 0, and we can just ignore the left operand
6749 -- completely in this case.
6751 -- The operand type may be private (e.g. in the expansion of an
6752 -- intrinsic operation) so we must use the underlying type to get the
6753 -- bounds, and convert the literals explicitly.
6757 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
6759 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
6761 ((not LOK) or else (Llo = LLB))
6764 Make_Conditional_Expression (Loc,
6765 Expressions => New_List (
6767 Left_Opnd => Duplicate_Subexpr (Right),
6769 Unchecked_Convert_To (Typ,
6770 Make_Integer_Literal (Loc, -1))),
6771 Unchecked_Convert_To (Typ,
6772 Make_Integer_Literal (Loc, Uint_0)),
6773 Relocate_Node (N))));
6775 Set_Analyzed (Next (Next (First (Expressions (N)))));
6776 Analyze_And_Resolve (N, Typ);
6779 end Expand_N_Op_Mod;
6781 --------------------------
6782 -- Expand_N_Op_Multiply --
6783 --------------------------
6785 procedure Expand_N_Op_Multiply (N : Node_Id) is
6786 Loc : constant Source_Ptr := Sloc (N);
6787 Lop : constant Node_Id := Left_Opnd (N);
6788 Rop : constant Node_Id := Right_Opnd (N);
6790 Lp2 : constant Boolean :=
6791 Nkind (Lop) = N_Op_Expon
6792 and then Is_Power_Of_2_For_Shift (Lop);
6794 Rp2 : constant Boolean :=
6795 Nkind (Rop) = N_Op_Expon
6796 and then Is_Power_Of_2_For_Shift (Rop);
6798 Ltyp : constant Entity_Id := Etype (Lop);
6799 Rtyp : constant Entity_Id := Etype (Rop);
6800 Typ : Entity_Id := Etype (N);
6803 Binary_Op_Validity_Checks (N);
6805 -- Special optimizations for integer types
6807 if Is_Integer_Type (Typ) then
6809 -- N * 0 = 0 for integer types
6811 if Compile_Time_Known_Value (Rop)
6812 and then Expr_Value (Rop) = Uint_0
6814 -- Call Remove_Side_Effects to ensure that any side effects in
6815 -- the ignored left operand (in particular function calls to
6816 -- user defined functions) are properly executed.
6818 Remove_Side_Effects (Lop);
6820 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6821 Analyze_And_Resolve (N, Typ);
6825 -- Similar handling for 0 * N = 0
6827 if Compile_Time_Known_Value (Lop)
6828 and then Expr_Value (Lop) = Uint_0
6830 Remove_Side_Effects (Rop);
6831 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6832 Analyze_And_Resolve (N, Typ);
6836 -- N * 1 = 1 * N = N for integer types
6838 -- This optimisation is not done if we are going to
6839 -- rewrite the product 1 * 2 ** N to a shift.
6841 if Compile_Time_Known_Value (Rop)
6842 and then Expr_Value (Rop) = Uint_1
6848 elsif Compile_Time_Known_Value (Lop)
6849 and then Expr_Value (Lop) = Uint_1
6857 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
6858 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6859 -- operand is an integer, as required for this to work.
6864 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
6868 Left_Opnd => Make_Integer_Literal (Loc, 2),
6871 Left_Opnd => Right_Opnd (Lop),
6872 Right_Opnd => Right_Opnd (Rop))));
6873 Analyze_And_Resolve (N, Typ);
6878 Make_Op_Shift_Left (Loc,
6881 Convert_To (Standard_Natural, Right_Opnd (Rop))));
6882 Analyze_And_Resolve (N, Typ);
6886 -- Same processing for the operands the other way round
6890 Make_Op_Shift_Left (Loc,
6893 Convert_To (Standard_Natural, Right_Opnd (Lop))));
6894 Analyze_And_Resolve (N, Typ);
6898 -- Do required fixup of universal fixed operation
6900 if Typ = Universal_Fixed then
6901 Fixup_Universal_Fixed_Operation (N);
6905 -- Multiplications with fixed-point results
6907 if Is_Fixed_Point_Type (Typ) then
6909 -- No special processing if Treat_Fixed_As_Integer is set, since from
6910 -- a semantic point of view such operations are simply integer
6911 -- operations and will be treated that way.
6913 if not Treat_Fixed_As_Integer (N) then
6915 -- Case of fixed * integer => fixed
6917 if Is_Integer_Type (Rtyp) then
6918 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
6920 -- Case of integer * fixed => fixed
6922 elsif Is_Integer_Type (Ltyp) then
6923 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
6925 -- Case of fixed * fixed => fixed
6928 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
6932 -- Other cases of multiplication of fixed-point operands. Again we
6933 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
6935 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6936 and then not Treat_Fixed_As_Integer (N)
6938 if Is_Integer_Type (Typ) then
6939 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
6941 pragma Assert (Is_Floating_Point_Type (Typ));
6942 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
6945 -- Mixed-mode operations can appear in a non-static universal context,
6946 -- in which case the integer argument must be converted explicitly.
6948 elsif Typ = Universal_Real
6949 and then Is_Integer_Type (Rtyp)
6951 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
6953 Analyze_And_Resolve (Rop, Universal_Real);
6955 elsif Typ = Universal_Real
6956 and then Is_Integer_Type (Ltyp)
6958 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
6960 Analyze_And_Resolve (Lop, Universal_Real);
6962 -- Non-fixed point cases, check software overflow checking required
6964 elsif Is_Signed_Integer_Type (Etype (N)) then
6965 Apply_Arithmetic_Overflow_Check (N);
6967 -- Deal with VAX float case
6969 elsif Vax_Float (Typ) then
6970 Expand_Vax_Arith (N);
6973 end Expand_N_Op_Multiply;
6975 --------------------
6976 -- Expand_N_Op_Ne --
6977 --------------------
6979 procedure Expand_N_Op_Ne (N : Node_Id) is
6980 Typ : constant Entity_Id := Etype (Left_Opnd (N));
6983 -- Case of elementary type with standard operator
6985 if Is_Elementary_Type (Typ)
6986 and then Sloc (Entity (N)) = Standard_Location
6988 Binary_Op_Validity_Checks (N);
6990 -- Boolean types (requiring handling of non-standard case)
6992 if Is_Boolean_Type (Typ) then
6993 Adjust_Condition (Left_Opnd (N));
6994 Adjust_Condition (Right_Opnd (N));
6995 Set_Etype (N, Standard_Boolean);
6996 Adjust_Result_Type (N, Typ);
6999 Rewrite_Comparison (N);
7001 -- If we still have comparison for Vax_Float, process it
7003 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
7004 Expand_Vax_Comparison (N);
7008 -- For all cases other than elementary types, we rewrite node as the
7009 -- negation of an equality operation, and reanalyze. The equality to be
7010 -- used is defined in the same scope and has the same signature. This
7011 -- signature must be set explicitly since in an instance it may not have
7012 -- the same visibility as in the generic unit. This avoids duplicating
7013 -- or factoring the complex code for record/array equality tests etc.
7017 Loc : constant Source_Ptr := Sloc (N);
7019 Ne : constant Entity_Id := Entity (N);
7022 Binary_Op_Validity_Checks (N);
7028 Left_Opnd => Left_Opnd (N),
7029 Right_Opnd => Right_Opnd (N)));
7030 Set_Paren_Count (Right_Opnd (Neg), 1);
7032 if Scope (Ne) /= Standard_Standard then
7033 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
7036 -- For navigation purposes, we want to treat the inequality as an
7037 -- implicit reference to the corresponding equality. Preserve the
7038 -- Comes_From_ source flag to generate proper Xref entries.
7040 Preserve_Comes_From_Source (Neg, N);
7041 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
7043 Analyze_And_Resolve (N, Standard_Boolean);
7047 Optimize_Length_Comparison (N);
7050 ---------------------
7051 -- Expand_N_Op_Not --
7052 ---------------------
7054 -- If the argument is other than a Boolean array type, there is no special
7055 -- expansion required, except for VMS operations on signed integers.
7057 -- For the packed case, we call the special routine in Exp_Pakd, except
7058 -- that if the component size is greater than one, we use the standard
7059 -- routine generating a gruesome loop (it is so peculiar to have packed
7060 -- arrays with non-standard Boolean representations anyway, so it does not
7061 -- matter that we do not handle this case efficiently).
7063 -- For the unpacked case (and for the special packed case where we have non
7064 -- standard Booleans, as discussed above), we generate and insert into the
7065 -- tree the following function definition:
7067 -- function Nnnn (A : arr) is
7070 -- for J in a'range loop
7071 -- B (J) := not A (J);
7076 -- Here arr is the actual subtype of the parameter (and hence always
7077 -- constrained). Then we replace the not with a call to this function.
7079 procedure Expand_N_Op_Not (N : Node_Id) is
7080 Loc : constant Source_Ptr := Sloc (N);
7081 Typ : constant Entity_Id := Etype (N);
7090 Func_Name : Entity_Id;
7091 Loop_Statement : Node_Id;
7094 Unary_Op_Validity_Checks (N);
7096 -- For boolean operand, deal with non-standard booleans
7098 if Is_Boolean_Type (Typ) then
7099 Adjust_Condition (Right_Opnd (N));
7100 Set_Etype (N, Standard_Boolean);
7101 Adjust_Result_Type (N, Typ);
7105 -- For the VMS "not" on signed integer types, use conversion to and from
7106 -- a predefined modular type.
7108 if Is_VMS_Operator (Entity (N)) then
7114 -- If this is a derived type, retrieve original VMS type so that
7115 -- the proper sized type is used for intermediate values.
7117 if Is_Derived_Type (Typ) then
7118 Rtyp := First_Subtype (Etype (Typ));
7123 -- The proper unsigned type must have a size compatible with the
7124 -- operand, to prevent misalignment.
7126 if RM_Size (Rtyp) <= 8 then
7127 Utyp := RTE (RE_Unsigned_8);
7129 elsif RM_Size (Rtyp) <= 16 then
7130 Utyp := RTE (RE_Unsigned_16);
7132 elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then
7133 Utyp := RTE (RE_Unsigned_32);
7136 Utyp := RTE (RE_Long_Long_Unsigned);
7140 Unchecked_Convert_To (Typ,
7142 Unchecked_Convert_To (Utyp, Right_Opnd (N)))));
7143 Analyze_And_Resolve (N, Typ);
7148 -- Only array types need any other processing
7150 if not Is_Array_Type (Typ) then
7154 -- Case of array operand. If bit packed with a component size of 1,
7155 -- handle it in Exp_Pakd if the operand is known to be aligned.
7157 if Is_Bit_Packed_Array (Typ)
7158 and then Component_Size (Typ) = 1
7159 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
7161 Expand_Packed_Not (N);
7165 -- Case of array operand which is not bit-packed. If the context is
7166 -- a safe assignment, call in-place operation, If context is a larger
7167 -- boolean expression in the context of a safe assignment, expansion is
7168 -- done by enclosing operation.
7170 Opnd := Relocate_Node (Right_Opnd (N));
7171 Convert_To_Actual_Subtype (Opnd);
7172 Arr := Etype (Opnd);
7173 Ensure_Defined (Arr, N);
7174 Silly_Boolean_Array_Not_Test (N, Arr);
7176 if Nkind (Parent (N)) = N_Assignment_Statement then
7177 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
7178 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
7181 -- Special case the negation of a binary operation
7183 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
7184 and then Safe_In_Place_Array_Op
7185 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
7187 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
7191 elsif Nkind (Parent (N)) in N_Binary_Op
7192 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
7195 Op1 : constant Node_Id := Left_Opnd (Parent (N));
7196 Op2 : constant Node_Id := Right_Opnd (Parent (N));
7197 Lhs : constant Node_Id := Name (Parent (Parent (N)));
7200 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
7202 -- (not A) op (not B) can be reduced to a single call
7204 if N = Op1 and then Nkind (Op2) = N_Op_Not then
7207 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
7210 -- A xor (not B) can also be special-cased
7212 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
7219 A := Make_Defining_Identifier (Loc, Name_uA);
7220 B := Make_Defining_Identifier (Loc, Name_uB);
7221 J := Make_Defining_Identifier (Loc, Name_uJ);
7224 Make_Indexed_Component (Loc,
7225 Prefix => New_Reference_To (A, Loc),
7226 Expressions => New_List (New_Reference_To (J, Loc)));
7229 Make_Indexed_Component (Loc,
7230 Prefix => New_Reference_To (B, Loc),
7231 Expressions => New_List (New_Reference_To (J, Loc)));
7234 Make_Implicit_Loop_Statement (N,
7235 Identifier => Empty,
7238 Make_Iteration_Scheme (Loc,
7239 Loop_Parameter_Specification =>
7240 Make_Loop_Parameter_Specification (Loc,
7241 Defining_Identifier => J,
7242 Discrete_Subtype_Definition =>
7243 Make_Attribute_Reference (Loc,
7244 Prefix => Make_Identifier (Loc, Chars (A)),
7245 Attribute_Name => Name_Range))),
7247 Statements => New_List (
7248 Make_Assignment_Statement (Loc,
7250 Expression => Make_Op_Not (Loc, A_J))));
7252 Func_Name := Make_Temporary (Loc, 'N');
7253 Set_Is_Inlined (Func_Name);
7256 Make_Subprogram_Body (Loc,
7258 Make_Function_Specification (Loc,
7259 Defining_Unit_Name => Func_Name,
7260 Parameter_Specifications => New_List (
7261 Make_Parameter_Specification (Loc,
7262 Defining_Identifier => A,
7263 Parameter_Type => New_Reference_To (Typ, Loc))),
7264 Result_Definition => New_Reference_To (Typ, Loc)),
7266 Declarations => New_List (
7267 Make_Object_Declaration (Loc,
7268 Defining_Identifier => B,
7269 Object_Definition => New_Reference_To (Arr, Loc))),
7271 Handled_Statement_Sequence =>
7272 Make_Handled_Sequence_Of_Statements (Loc,
7273 Statements => New_List (
7275 Make_Simple_Return_Statement (Loc,
7276 Expression => Make_Identifier (Loc, Chars (B)))))));
7279 Make_Function_Call (Loc,
7280 Name => New_Reference_To (Func_Name, Loc),
7281 Parameter_Associations => New_List (Opnd)));
7283 Analyze_And_Resolve (N, Typ);
7284 end Expand_N_Op_Not;
7286 --------------------
7287 -- Expand_N_Op_Or --
7288 --------------------
7290 procedure Expand_N_Op_Or (N : Node_Id) is
7291 Typ : constant Entity_Id := Etype (N);
7294 Binary_Op_Validity_Checks (N);
7296 if Is_Array_Type (Etype (N)) then
7297 Expand_Boolean_Operator (N);
7299 elsif Is_Boolean_Type (Etype (N)) then
7301 -- Replace OR by OR ELSE if Short_Circuit_And_Or active and the type
7302 -- is standard Boolean (do not mess with AND that uses a non-standard
7303 -- Boolean type, because something strange is going on).
7305 if Short_Circuit_And_Or and then Typ = Standard_Boolean then
7307 Make_Or_Else (Sloc (N),
7308 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7309 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7310 Analyze_And_Resolve (N, Typ);
7312 -- Otherwise, adjust conditions
7315 Adjust_Condition (Left_Opnd (N));
7316 Adjust_Condition (Right_Opnd (N));
7317 Set_Etype (N, Standard_Boolean);
7318 Adjust_Result_Type (N, Typ);
7321 elsif Is_Intrinsic_Subprogram (Entity (N)) then
7322 Expand_Intrinsic_Call (N, Entity (N));
7327 ----------------------
7328 -- Expand_N_Op_Plus --
7329 ----------------------
7331 procedure Expand_N_Op_Plus (N : Node_Id) is
7333 Unary_Op_Validity_Checks (N);
7334 end Expand_N_Op_Plus;
7336 ---------------------
7337 -- Expand_N_Op_Rem --
7338 ---------------------
7340 procedure Expand_N_Op_Rem (N : Node_Id) is
7341 Loc : constant Source_Ptr := Sloc (N);
7342 Typ : constant Entity_Id := Etype (N);
7344 Left : constant Node_Id := Left_Opnd (N);
7345 Right : constant Node_Id := Right_Opnd (N);
7353 -- Set if corresponding operand can be negative
7355 pragma Unreferenced (Hi);
7358 Binary_Op_Validity_Checks (N);
7360 if Is_Integer_Type (Etype (N)) then
7361 Apply_Divide_Check (N);
7364 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
7365 -- but it is useful with other back ends (e.g. AAMP), and is certainly
7368 if Is_Integer_Type (Etype (N))
7369 and then Compile_Time_Known_Value (Right)
7370 and then Expr_Value (Right) = Uint_1
7372 -- Call Remove_Side_Effects to ensure that any side effects in the
7373 -- ignored left operand (in particular function calls to user defined
7374 -- functions) are properly executed.
7376 Remove_Side_Effects (Left);
7378 Rewrite (N, Make_Integer_Literal (Loc, 0));
7379 Analyze_And_Resolve (N, Typ);
7383 -- Deal with annoying case of largest negative number remainder minus
7384 -- one. Gigi does not handle this case correctly, because it generates
7385 -- a divide instruction which may trap in this case.
7387 -- In fact the check is quite easy, if the right operand is -1, then
7388 -- the remainder is always 0, and we can just ignore the left operand
7389 -- completely in this case.
7391 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
7392 Lneg := (not OK) or else Lo < 0;
7394 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
7395 Rneg := (not OK) or else Lo < 0;
7397 -- We won't mess with trying to find out if the left operand can really
7398 -- be the largest negative number (that's a pain in the case of private
7399 -- types and this is really marginal). We will just assume that we need
7400 -- the test if the left operand can be negative at all.
7402 if Lneg and Rneg then
7404 Make_Conditional_Expression (Loc,
7405 Expressions => New_List (
7407 Left_Opnd => Duplicate_Subexpr (Right),
7409 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
7411 Unchecked_Convert_To (Typ,
7412 Make_Integer_Literal (Loc, Uint_0)),
7414 Relocate_Node (N))));
7416 Set_Analyzed (Next (Next (First (Expressions (N)))));
7417 Analyze_And_Resolve (N, Typ);
7419 end Expand_N_Op_Rem;
7421 -----------------------------
7422 -- Expand_N_Op_Rotate_Left --
7423 -----------------------------
7425 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
7427 Binary_Op_Validity_Checks (N);
7428 end Expand_N_Op_Rotate_Left;
7430 ------------------------------
7431 -- Expand_N_Op_Rotate_Right --
7432 ------------------------------
7434 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
7436 Binary_Op_Validity_Checks (N);
7437 end Expand_N_Op_Rotate_Right;
7439 ----------------------------
7440 -- Expand_N_Op_Shift_Left --
7441 ----------------------------
7443 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
7445 Binary_Op_Validity_Checks (N);
7446 end Expand_N_Op_Shift_Left;
7448 -----------------------------
7449 -- Expand_N_Op_Shift_Right --
7450 -----------------------------
7452 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
7454 Binary_Op_Validity_Checks (N);
7455 end Expand_N_Op_Shift_Right;
7457 ----------------------------------------
7458 -- Expand_N_Op_Shift_Right_Arithmetic --
7459 ----------------------------------------
7461 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
7463 Binary_Op_Validity_Checks (N);
7464 end Expand_N_Op_Shift_Right_Arithmetic;
7466 --------------------------
7467 -- Expand_N_Op_Subtract --
7468 --------------------------
7470 procedure Expand_N_Op_Subtract (N : Node_Id) is
7471 Typ : constant Entity_Id := Etype (N);
7474 Binary_Op_Validity_Checks (N);
7476 -- N - 0 = N for integer types
7478 if Is_Integer_Type (Typ)
7479 and then Compile_Time_Known_Value (Right_Opnd (N))
7480 and then Expr_Value (Right_Opnd (N)) = 0
7482 Rewrite (N, Left_Opnd (N));
7486 -- Arithmetic overflow checks for signed integer/fixed point types
7488 if Is_Signed_Integer_Type (Typ)
7490 Is_Fixed_Point_Type (Typ)
7492 Apply_Arithmetic_Overflow_Check (N);
7494 -- VAX floating-point types case
7496 elsif Vax_Float (Typ) then
7497 Expand_Vax_Arith (N);
7499 end Expand_N_Op_Subtract;
7501 ---------------------
7502 -- Expand_N_Op_Xor --
7503 ---------------------
7505 procedure Expand_N_Op_Xor (N : Node_Id) is
7506 Typ : constant Entity_Id := Etype (N);
7509 Binary_Op_Validity_Checks (N);
7511 if Is_Array_Type (Etype (N)) then
7512 Expand_Boolean_Operator (N);
7514 elsif Is_Boolean_Type (Etype (N)) then
7515 Adjust_Condition (Left_Opnd (N));
7516 Adjust_Condition (Right_Opnd (N));
7517 Set_Etype (N, Standard_Boolean);
7518 Adjust_Result_Type (N, Typ);
7520 elsif Is_Intrinsic_Subprogram (Entity (N)) then
7521 Expand_Intrinsic_Call (N, Entity (N));
7524 end Expand_N_Op_Xor;
7526 ----------------------
7527 -- Expand_N_Or_Else --
7528 ----------------------
7530 procedure Expand_N_Or_Else (N : Node_Id)
7531 renames Expand_Short_Circuit_Operator;
7533 -----------------------------------
7534 -- Expand_N_Qualified_Expression --
7535 -----------------------------------
7537 procedure Expand_N_Qualified_Expression (N : Node_Id) is
7538 Operand : constant Node_Id := Expression (N);
7539 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
7542 -- Do validity check if validity checking operands
7544 if Validity_Checks_On
7545 and then Validity_Check_Operands
7547 Ensure_Valid (Operand);
7550 -- Apply possible constraint check
7552 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
7554 if Do_Range_Check (Operand) then
7555 Set_Do_Range_Check (Operand, False);
7556 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
7558 end Expand_N_Qualified_Expression;
7560 ------------------------------------
7561 -- Expand_N_Quantified_Expression --
7562 ------------------------------------
7566 -- for all X in range => Cond
7571 -- for X in range loop
7578 -- Conversely, an existentially quantified expression:
7580 -- for some X in range => Cond
7585 -- for X in range loop
7592 -- In both cases, the iteration may be over a container in which case it is
7593 -- given by an iterator specification, not a loop parameter specification.
7595 procedure Expand_N_Quantified_Expression (N : Node_Id) is
7596 Loc : constant Source_Ptr := Sloc (N);
7597 Is_Universal : constant Boolean := All_Present (N);
7598 Actions : constant List_Id := New_List;
7599 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
7607 Make_Object_Declaration (Loc,
7608 Defining_Identifier => Tnn,
7609 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
7611 New_Occurrence_Of (Boolean_Literals (Is_Universal), Loc));
7612 Append_To (Actions, Decl);
7614 Cond := Relocate_Node (Condition (N));
7616 -- Reset flag analyzed in the condition to force its analysis. Required
7617 -- since the previous analysis was done with expansion disabled (see
7618 -- Resolve_Quantified_Expression) and hence checks were not inserted
7619 -- and record comparisons have not been expanded.
7621 Reset_Analyzed_Flags (Cond);
7623 if Is_Universal then
7624 Cond := Make_Op_Not (Loc, Cond);
7628 Make_Implicit_If_Statement (N,
7630 Then_Statements => New_List (
7631 Make_Assignment_Statement (Loc,
7632 Name => New_Occurrence_Of (Tnn, Loc),
7634 New_Occurrence_Of (Boolean_Literals (not Is_Universal), Loc)),
7635 Make_Exit_Statement (Loc)));
7637 if Present (Loop_Parameter_Specification (N)) then
7639 Make_Iteration_Scheme (Loc,
7640 Loop_Parameter_Specification =>
7641 Loop_Parameter_Specification (N));
7644 Make_Iteration_Scheme (Loc,
7645 Iterator_Specification => Iterator_Specification (N));
7649 Make_Loop_Statement (Loc,
7650 Iteration_Scheme => I_Scheme,
7651 Statements => New_List (Test),
7652 End_Label => Empty));
7654 -- The components of the scheme have already been analyzed, and the loop
7655 -- parameter declaration has been processed.
7657 Set_Analyzed (Iteration_Scheme (Last (Actions)));
7660 Make_Expression_With_Actions (Loc,
7661 Expression => New_Occurrence_Of (Tnn, Loc),
7662 Actions => Actions));
7664 Analyze_And_Resolve (N, Standard_Boolean);
7665 end Expand_N_Quantified_Expression;
7667 ---------------------------------
7668 -- Expand_N_Selected_Component --
7669 ---------------------------------
7671 -- If the selector is a discriminant of a concurrent object, rewrite the
7672 -- prefix to denote the corresponding record type.
7674 procedure Expand_N_Selected_Component (N : Node_Id) is
7675 Loc : constant Source_Ptr := Sloc (N);
7676 Par : constant Node_Id := Parent (N);
7677 P : constant Node_Id := Prefix (N);
7678 Ptyp : Entity_Id := Underlying_Type (Etype (P));
7684 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
7685 -- Gigi needs a temporary for prefixes that depend on a discriminant,
7686 -- unless the context of an assignment can provide size information.
7687 -- Don't we have a general routine that does this???
7689 function Is_Subtype_Declaration return Boolean;
7690 -- The replacement of a discriminant reference by its value is required
7691 -- if this is part of the initialization of an temporary generated by a
7692 -- change of representation. This shows up as the construction of a
7693 -- discriminant constraint for a subtype declared at the same point as
7694 -- the entity in the prefix of the selected component. We recognize this
7695 -- case when the context of the reference is:
7696 -- subtype ST is T(Obj.D);
7697 -- where the entity for Obj comes from source, and ST has the same sloc.
7699 -----------------------
7700 -- In_Left_Hand_Side --
7701 -----------------------
7703 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
7705 return (Nkind (Parent (Comp)) = N_Assignment_Statement
7706 and then Comp = Name (Parent (Comp)))
7707 or else (Present (Parent (Comp))
7708 and then Nkind (Parent (Comp)) in N_Subexpr
7709 and then In_Left_Hand_Side (Parent (Comp)));
7710 end In_Left_Hand_Side;
7712 -----------------------------
7713 -- Is_Subtype_Declaration --
7714 -----------------------------
7716 function Is_Subtype_Declaration return Boolean is
7717 Par : constant Node_Id := Parent (N);
7720 Nkind (Par) = N_Index_Or_Discriminant_Constraint
7721 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
7722 and then Comes_From_Source (Entity (Prefix (N)))
7723 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
7724 end Is_Subtype_Declaration;
7726 -- Start of processing for Expand_N_Selected_Component
7729 -- Insert explicit dereference if required
7731 if Is_Access_Type (Ptyp) then
7732 Insert_Explicit_Dereference (P);
7733 Analyze_And_Resolve (P, Designated_Type (Ptyp));
7735 if Ekind (Etype (P)) = E_Private_Subtype
7736 and then Is_For_Access_Subtype (Etype (P))
7738 Set_Etype (P, Base_Type (Etype (P)));
7744 -- Deal with discriminant check required
7746 if Do_Discriminant_Check (N) then
7748 -- Present the discriminant checking function to the backend, so that
7749 -- it can inline the call to the function.
7752 (Discriminant_Checking_Func
7753 (Original_Record_Component (Entity (Selector_Name (N)))));
7755 -- Now reset the flag and generate the call
7757 Set_Do_Discriminant_Check (N, False);
7758 Generate_Discriminant_Check (N);
7761 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7762 -- function, then additional actuals must be passed.
7764 if Ada_Version >= Ada_2005
7765 and then Is_Build_In_Place_Function_Call (P)
7767 Make_Build_In_Place_Call_In_Anonymous_Context (P);
7770 -- Gigi cannot handle unchecked conversions that are the prefix of a
7771 -- selected component with discriminants. This must be checked during
7772 -- expansion, because during analysis the type of the selector is not
7773 -- known at the point the prefix is analyzed. If the conversion is the
7774 -- target of an assignment, then we cannot force the evaluation.
7776 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
7777 and then Has_Discriminants (Etype (N))
7778 and then not In_Left_Hand_Side (N)
7780 Force_Evaluation (Prefix (N));
7783 -- Remaining processing applies only if selector is a discriminant
7785 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
7787 -- If the selector is a discriminant of a constrained record type,
7788 -- we may be able to rewrite the expression with the actual value
7789 -- of the discriminant, a useful optimization in some cases.
7791 if Is_Record_Type (Ptyp)
7792 and then Has_Discriminants (Ptyp)
7793 and then Is_Constrained (Ptyp)
7795 -- Do this optimization for discrete types only, and not for
7796 -- access types (access discriminants get us into trouble!)
7798 if not Is_Discrete_Type (Etype (N)) then
7801 -- Don't do this on the left hand of an assignment statement.
7802 -- Normally one would think that references like this would not
7803 -- occur, but they do in generated code, and mean that we really
7804 -- do want to assign the discriminant!
7806 elsif Nkind (Par) = N_Assignment_Statement
7807 and then Name (Par) = N
7811 -- Don't do this optimization for the prefix of an attribute or
7812 -- the name of an object renaming declaration since these are
7813 -- contexts where we do not want the value anyway.
7815 elsif (Nkind (Par) = N_Attribute_Reference
7816 and then Prefix (Par) = N)
7817 or else Is_Renamed_Object (N)
7821 -- Don't do this optimization if we are within the code for a
7822 -- discriminant check, since the whole point of such a check may
7823 -- be to verify the condition on which the code below depends!
7825 elsif Is_In_Discriminant_Check (N) then
7828 -- Green light to see if we can do the optimization. There is
7829 -- still one condition that inhibits the optimization below but
7830 -- now is the time to check the particular discriminant.
7833 -- Loop through discriminants to find the matching discriminant
7834 -- constraint to see if we can copy it.
7836 Disc := First_Discriminant (Ptyp);
7837 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
7838 Discr_Loop : while Present (Dcon) loop
7839 Dval := Node (Dcon);
7841 -- Check if this is the matching discriminant and if the
7842 -- discriminant value is simple enough to make sense to
7843 -- copy. We don't want to copy complex expressions, and
7844 -- indeed to do so can cause trouble (before we put in
7845 -- this guard, a discriminant expression containing an
7846 -- AND THEN was copied, causing problems for coverage
7849 -- However, if the reference is part of the initialization
7850 -- code generated for an object declaration, we must use
7851 -- the discriminant value from the subtype constraint,
7852 -- because the selected component may be a reference to the
7853 -- object being initialized, whose discriminant is not yet
7854 -- set. This only happens in complex cases involving changes
7855 -- or representation.
7857 if Disc = Entity (Selector_Name (N))
7858 and then (Is_Entity_Name (Dval)
7859 or else Compile_Time_Known_Value (Dval)
7860 or else Is_Subtype_Declaration)
7862 -- Here we have the matching discriminant. Check for
7863 -- the case of a discriminant of a component that is
7864 -- constrained by an outer discriminant, which cannot
7865 -- be optimized away.
7867 if Denotes_Discriminant
7868 (Dval, Check_Concurrent => True)
7872 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
7874 Denotes_Discriminant
7875 (Selector_Name (Original_Node (Dval)), True)
7879 -- Do not retrieve value if constraint is not static. It
7880 -- is generally not useful, and the constraint may be a
7881 -- rewritten outer discriminant in which case it is in
7884 elsif Is_Entity_Name (Dval)
7885 and then Nkind (Parent (Entity (Dval))) =
7886 N_Object_Declaration
7887 and then Present (Expression (Parent (Entity (Dval))))
7889 not Is_Static_Expression
7890 (Expression (Parent (Entity (Dval))))
7894 -- In the context of a case statement, the expression may
7895 -- have the base type of the discriminant, and we need to
7896 -- preserve the constraint to avoid spurious errors on
7899 elsif Nkind (Parent (N)) = N_Case_Statement
7900 and then Etype (Dval) /= Etype (Disc)
7903 Make_Qualified_Expression (Loc,
7905 New_Occurrence_Of (Etype (Disc), Loc),
7907 New_Copy_Tree (Dval)));
7908 Analyze_And_Resolve (N, Etype (Disc));
7910 -- In case that comes out as a static expression,
7911 -- reset it (a selected component is never static).
7913 Set_Is_Static_Expression (N, False);
7916 -- Otherwise we can just copy the constraint, but the
7917 -- result is certainly not static! In some cases the
7918 -- discriminant constraint has been analyzed in the
7919 -- context of the original subtype indication, but for
7920 -- itypes the constraint might not have been analyzed
7921 -- yet, and this must be done now.
7924 Rewrite (N, New_Copy_Tree (Dval));
7925 Analyze_And_Resolve (N);
7926 Set_Is_Static_Expression (N, False);
7932 Next_Discriminant (Disc);
7933 end loop Discr_Loop;
7935 -- Note: the above loop should always find a matching
7936 -- discriminant, but if it does not, we just missed an
7937 -- optimization due to some glitch (perhaps a previous
7938 -- error), so ignore.
7943 -- The only remaining processing is in the case of a discriminant of
7944 -- a concurrent object, where we rewrite the prefix to denote the
7945 -- corresponding record type. If the type is derived and has renamed
7946 -- discriminants, use corresponding discriminant, which is the one
7947 -- that appears in the corresponding record.
7949 if not Is_Concurrent_Type (Ptyp) then
7953 Disc := Entity (Selector_Name (N));
7955 if Is_Derived_Type (Ptyp)
7956 and then Present (Corresponding_Discriminant (Disc))
7958 Disc := Corresponding_Discriminant (Disc);
7962 Make_Selected_Component (Loc,
7964 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
7966 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
7971 end Expand_N_Selected_Component;
7973 --------------------
7974 -- Expand_N_Slice --
7975 --------------------
7977 procedure Expand_N_Slice (N : Node_Id) is
7978 Loc : constant Source_Ptr := Sloc (N);
7979 Typ : constant Entity_Id := Etype (N);
7980 Pfx : constant Node_Id := Prefix (N);
7981 Ptp : Entity_Id := Etype (Pfx);
7983 function Is_Procedure_Actual (N : Node_Id) return Boolean;
7984 -- Check whether the argument is an actual for a procedure call, in
7985 -- which case the expansion of a bit-packed slice is deferred until the
7986 -- call itself is expanded. The reason this is required is that we might
7987 -- have an IN OUT or OUT parameter, and the copy out is essential, and
7988 -- that copy out would be missed if we created a temporary here in
7989 -- Expand_N_Slice. Note that we don't bother to test specifically for an
7990 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
7991 -- is harmless to defer expansion in the IN case, since the call
7992 -- processing will still generate the appropriate copy in operation,
7993 -- which will take care of the slice.
7995 procedure Make_Temporary_For_Slice;
7996 -- Create a named variable for the value of the slice, in cases where
7997 -- the back-end cannot handle it properly, e.g. when packed types or
7998 -- unaligned slices are involved.
8000 -------------------------
8001 -- Is_Procedure_Actual --
8002 -------------------------
8004 function Is_Procedure_Actual (N : Node_Id) return Boolean is
8005 Par : Node_Id := Parent (N);
8009 -- If our parent is a procedure call we can return
8011 if Nkind (Par) = N_Procedure_Call_Statement then
8014 -- If our parent is a type conversion, keep climbing the tree,
8015 -- since a type conversion can be a procedure actual. Also keep
8016 -- climbing if parameter association or a qualified expression,
8017 -- since these are additional cases that do can appear on
8018 -- procedure actuals.
8020 elsif Nkind_In (Par, N_Type_Conversion,
8021 N_Parameter_Association,
8022 N_Qualified_Expression)
8024 Par := Parent (Par);
8026 -- Any other case is not what we are looking for
8032 end Is_Procedure_Actual;
8034 ------------------------------
8035 -- Make_Temporary_For_Slice --
8036 ------------------------------
8038 procedure Make_Temporary_For_Slice is
8040 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
8044 Make_Object_Declaration (Loc,
8045 Defining_Identifier => Ent,
8046 Object_Definition => New_Occurrence_Of (Typ, Loc));
8048 Set_No_Initialization (Decl);
8050 Insert_Actions (N, New_List (
8052 Make_Assignment_Statement (Loc,
8053 Name => New_Occurrence_Of (Ent, Loc),
8054 Expression => Relocate_Node (N))));
8056 Rewrite (N, New_Occurrence_Of (Ent, Loc));
8057 Analyze_And_Resolve (N, Typ);
8058 end Make_Temporary_For_Slice;
8060 -- Start of processing for Expand_N_Slice
8063 -- Special handling for access types
8065 if Is_Access_Type (Ptp) then
8067 Ptp := Designated_Type (Ptp);
8070 Make_Explicit_Dereference (Sloc (N),
8071 Prefix => Relocate_Node (Pfx)));
8073 Analyze_And_Resolve (Pfx, Ptp);
8076 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
8077 -- function, then additional actuals must be passed.
8079 if Ada_Version >= Ada_2005
8080 and then Is_Build_In_Place_Function_Call (Pfx)
8082 Make_Build_In_Place_Call_In_Anonymous_Context (Pfx);
8085 -- The remaining case to be handled is packed slices. We can leave
8086 -- packed slices as they are in the following situations:
8088 -- 1. Right or left side of an assignment (we can handle this
8089 -- situation correctly in the assignment statement expansion).
8091 -- 2. Prefix of indexed component (the slide is optimized away in this
8092 -- case, see the start of Expand_N_Slice.)
8094 -- 3. Object renaming declaration, since we want the name of the
8095 -- slice, not the value.
8097 -- 4. Argument to procedure call, since copy-in/copy-out handling may
8098 -- be required, and this is handled in the expansion of call
8101 -- 5. Prefix of an address attribute (this is an error which is caught
8102 -- elsewhere, and the expansion would interfere with generating the
8105 if not Is_Packed (Typ) then
8107 -- Apply transformation for actuals of a function call, where
8108 -- Expand_Actuals is not used.
8110 if Nkind (Parent (N)) = N_Function_Call
8111 and then Is_Possibly_Unaligned_Slice (N)
8113 Make_Temporary_For_Slice;
8116 elsif Nkind (Parent (N)) = N_Assignment_Statement
8117 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
8118 and then Parent (N) = Name (Parent (Parent (N))))
8122 elsif Nkind (Parent (N)) = N_Indexed_Component
8123 or else Is_Renamed_Object (N)
8124 or else Is_Procedure_Actual (N)
8128 elsif Nkind (Parent (N)) = N_Attribute_Reference
8129 and then Attribute_Name (Parent (N)) = Name_Address
8134 Make_Temporary_For_Slice;
8138 ------------------------------
8139 -- Expand_N_Type_Conversion --
8140 ------------------------------
8142 procedure Expand_N_Type_Conversion (N : Node_Id) is
8143 Loc : constant Source_Ptr := Sloc (N);
8144 Operand : constant Node_Id := Expression (N);
8145 Target_Type : constant Entity_Id := Etype (N);
8146 Operand_Type : Entity_Id := Etype (Operand);
8148 procedure Handle_Changed_Representation;
8149 -- This is called in the case of record and array type conversions to
8150 -- see if there is a change of representation to be handled. Change of
8151 -- representation is actually handled at the assignment statement level,
8152 -- and what this procedure does is rewrite node N conversion as an
8153 -- assignment to temporary. If there is no change of representation,
8154 -- then the conversion node is unchanged.
8156 procedure Raise_Accessibility_Error;
8157 -- Called when we know that an accessibility check will fail. Rewrites
8158 -- node N to an appropriate raise statement and outputs warning msgs.
8159 -- The Etype of the raise node is set to Target_Type.
8161 procedure Real_Range_Check;
8162 -- Handles generation of range check for real target value
8164 -----------------------------------
8165 -- Handle_Changed_Representation --
8166 -----------------------------------
8168 procedure Handle_Changed_Representation is
8177 -- Nothing else to do if no change of representation
8179 if Same_Representation (Operand_Type, Target_Type) then
8182 -- The real change of representation work is done by the assignment
8183 -- statement processing. So if this type conversion is appearing as
8184 -- the expression of an assignment statement, nothing needs to be
8185 -- done to the conversion.
8187 elsif Nkind (Parent (N)) = N_Assignment_Statement then
8190 -- Otherwise we need to generate a temporary variable, and do the
8191 -- change of representation assignment into that temporary variable.
8192 -- The conversion is then replaced by a reference to this variable.
8197 -- If type is unconstrained we have to add a constraint, copied
8198 -- from the actual value of the left hand side.
8200 if not Is_Constrained (Target_Type) then
8201 if Has_Discriminants (Operand_Type) then
8202 Disc := First_Discriminant (Operand_Type);
8204 if Disc /= First_Stored_Discriminant (Operand_Type) then
8205 Disc := First_Stored_Discriminant (Operand_Type);
8209 while Present (Disc) loop
8211 Make_Selected_Component (Loc,
8213 Duplicate_Subexpr_Move_Checks (Operand),
8215 Make_Identifier (Loc, Chars (Disc))));
8216 Next_Discriminant (Disc);
8219 elsif Is_Array_Type (Operand_Type) then
8220 N_Ix := First_Index (Target_Type);
8223 for J in 1 .. Number_Dimensions (Operand_Type) loop
8225 -- We convert the bounds explicitly. We use an unchecked
8226 -- conversion because bounds checks are done elsewhere.
8231 Unchecked_Convert_To (Etype (N_Ix),
8232 Make_Attribute_Reference (Loc,
8234 Duplicate_Subexpr_No_Checks
8235 (Operand, Name_Req => True),
8236 Attribute_Name => Name_First,
8237 Expressions => New_List (
8238 Make_Integer_Literal (Loc, J)))),
8241 Unchecked_Convert_To (Etype (N_Ix),
8242 Make_Attribute_Reference (Loc,
8244 Duplicate_Subexpr_No_Checks
8245 (Operand, Name_Req => True),
8246 Attribute_Name => Name_Last,
8247 Expressions => New_List (
8248 Make_Integer_Literal (Loc, J))))));
8255 Odef := New_Occurrence_Of (Target_Type, Loc);
8257 if Present (Cons) then
8259 Make_Subtype_Indication (Loc,
8260 Subtype_Mark => Odef,
8262 Make_Index_Or_Discriminant_Constraint (Loc,
8263 Constraints => Cons));
8266 Temp := Make_Temporary (Loc, 'C');
8268 Make_Object_Declaration (Loc,
8269 Defining_Identifier => Temp,
8270 Object_Definition => Odef);
8272 Set_No_Initialization (Decl, True);
8274 -- Insert required actions. It is essential to suppress checks
8275 -- since we have suppressed default initialization, which means
8276 -- that the variable we create may have no discriminants.
8281 Make_Assignment_Statement (Loc,
8282 Name => New_Occurrence_Of (Temp, Loc),
8283 Expression => Relocate_Node (N))),
8284 Suppress => All_Checks);
8286 Rewrite (N, New_Occurrence_Of (Temp, Loc));
8289 end Handle_Changed_Representation;
8291 -------------------------------
8292 -- Raise_Accessibility_Error --
8293 -------------------------------
8295 procedure Raise_Accessibility_Error is
8298 Make_Raise_Program_Error (Sloc (N),
8299 Reason => PE_Accessibility_Check_Failed));
8300 Set_Etype (N, Target_Type);
8302 Error_Msg_N ("?accessibility check failure", N);
8304 ("\?& will be raised at run time", N, Standard_Program_Error);
8305 end Raise_Accessibility_Error;
8307 ----------------------
8308 -- Real_Range_Check --
8309 ----------------------
8311 -- Case of conversions to floating-point or fixed-point. If range checks
8312 -- are enabled and the target type has a range constraint, we convert:
8318 -- Tnn : typ'Base := typ'Base (x);
8319 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
8322 -- This is necessary when there is a conversion of integer to float or
8323 -- to fixed-point to ensure that the correct checks are made. It is not
8324 -- necessary for float to float where it is enough to simply set the
8325 -- Do_Range_Check flag.
8327 procedure Real_Range_Check is
8328 Btyp : constant Entity_Id := Base_Type (Target_Type);
8329 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
8330 Hi : constant Node_Id := Type_High_Bound (Target_Type);
8331 Xtyp : constant Entity_Id := Etype (Operand);
8336 -- Nothing to do if conversion was rewritten
8338 if Nkind (N) /= N_Type_Conversion then
8342 -- Nothing to do if range checks suppressed, or target has the same
8343 -- range as the base type (or is the base type).
8345 if Range_Checks_Suppressed (Target_Type)
8346 or else (Lo = Type_Low_Bound (Btyp)
8348 Hi = Type_High_Bound (Btyp))
8353 -- Nothing to do if expression is an entity on which checks have been
8356 if Is_Entity_Name (Operand)
8357 and then Range_Checks_Suppressed (Entity (Operand))
8362 -- Nothing to do if bounds are all static and we can tell that the
8363 -- expression is within the bounds of the target. Note that if the
8364 -- operand is of an unconstrained floating-point type, then we do
8365 -- not trust it to be in range (might be infinite)
8368 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
8369 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
8372 if (not Is_Floating_Point_Type (Xtyp)
8373 or else Is_Constrained (Xtyp))
8374 and then Compile_Time_Known_Value (S_Lo)
8375 and then Compile_Time_Known_Value (S_Hi)
8376 and then Compile_Time_Known_Value (Hi)
8377 and then Compile_Time_Known_Value (Lo)
8380 D_Lov : constant Ureal := Expr_Value_R (Lo);
8381 D_Hiv : constant Ureal := Expr_Value_R (Hi);
8386 if Is_Real_Type (Xtyp) then
8387 S_Lov := Expr_Value_R (S_Lo);
8388 S_Hiv := Expr_Value_R (S_Hi);
8390 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
8391 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
8395 and then S_Lov >= D_Lov
8396 and then S_Hiv <= D_Hiv
8398 Set_Do_Range_Check (Operand, False);
8405 -- For float to float conversions, we are done
8407 if Is_Floating_Point_Type (Xtyp)
8409 Is_Floating_Point_Type (Btyp)
8414 -- Otherwise rewrite the conversion as described above
8416 Conv := Relocate_Node (N);
8417 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
8418 Set_Etype (Conv, Btyp);
8420 -- Enable overflow except for case of integer to float conversions,
8421 -- where it is never required, since we can never have overflow in
8424 if not Is_Integer_Type (Etype (Operand)) then
8425 Enable_Overflow_Check (Conv);
8428 Tnn := Make_Temporary (Loc, 'T', Conv);
8430 Insert_Actions (N, New_List (
8431 Make_Object_Declaration (Loc,
8432 Defining_Identifier => Tnn,
8433 Object_Definition => New_Occurrence_Of (Btyp, Loc),
8434 Constant_Present => True,
8435 Expression => Conv),
8437 Make_Raise_Constraint_Error (Loc,
8442 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8444 Make_Attribute_Reference (Loc,
8445 Attribute_Name => Name_First,
8447 New_Occurrence_Of (Target_Type, Loc))),
8451 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8453 Make_Attribute_Reference (Loc,
8454 Attribute_Name => Name_Last,
8456 New_Occurrence_Of (Target_Type, Loc)))),
8457 Reason => CE_Range_Check_Failed)));
8459 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
8460 Analyze_And_Resolve (N, Btyp);
8461 end Real_Range_Check;
8463 -- Start of processing for Expand_N_Type_Conversion
8466 -- Nothing at all to do if conversion is to the identical type so remove
8467 -- the conversion completely, it is useless, except that it may carry
8468 -- an Assignment_OK attribute, which must be propagated to the operand.
8470 if Operand_Type = Target_Type then
8471 if Assignment_OK (N) then
8472 Set_Assignment_OK (Operand);
8475 Rewrite (N, Relocate_Node (Operand));
8479 -- Nothing to do if this is the second argument of read. This is a
8480 -- "backwards" conversion that will be handled by the specialized code
8481 -- in attribute processing.
8483 if Nkind (Parent (N)) = N_Attribute_Reference
8484 and then Attribute_Name (Parent (N)) = Name_Read
8485 and then Next (First (Expressions (Parent (N)))) = N
8490 -- Check for case of converting to a type that has an invariant
8491 -- associated with it. This required an invariant check. We convert
8497 -- do invariant_check (typ (expr)) in typ (expr);
8499 -- using Duplicate_Subexpr to avoid multiple side effects
8501 -- Note: the Comes_From_Source check, and then the resetting of this
8502 -- flag prevents what would otherwise be an infinite recursion.
8504 if Has_Invariants (Target_Type)
8505 and then Present (Invariant_Procedure (Target_Type))
8506 and then Comes_From_Source (N)
8508 Set_Comes_From_Source (N, False);
8510 Make_Expression_With_Actions (Loc,
8511 Actions => New_List (
8512 Make_Invariant_Call (Duplicate_Subexpr (N))),
8513 Expression => Duplicate_Subexpr_No_Checks (N)));
8514 Analyze_And_Resolve (N, Target_Type);
8518 -- Here if we may need to expand conversion
8520 -- If the operand of the type conversion is an arithmetic operation on
8521 -- signed integers, and the based type of the signed integer type in
8522 -- question is smaller than Standard.Integer, we promote both of the
8523 -- operands to type Integer.
8525 -- For example, if we have
8527 -- target-type (opnd1 + opnd2)
8529 -- and opnd1 and opnd2 are of type short integer, then we rewrite
8532 -- target-type (integer(opnd1) + integer(opnd2))
8534 -- We do this because we are always allowed to compute in a larger type
8535 -- if we do the right thing with the result, and in this case we are
8536 -- going to do a conversion which will do an appropriate check to make
8537 -- sure that things are in range of the target type in any case. This
8538 -- avoids some unnecessary intermediate overflows.
8540 -- We might consider a similar transformation in the case where the
8541 -- target is a real type or a 64-bit integer type, and the operand
8542 -- is an arithmetic operation using a 32-bit integer type. However,
8543 -- we do not bother with this case, because it could cause significant
8544 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
8545 -- much cheaper, but we don't want different behavior on 32-bit and
8546 -- 64-bit machines. Note that the exclusion of the 64-bit case also
8547 -- handles the configurable run-time cases where 64-bit arithmetic
8548 -- may simply be unavailable.
8550 -- Note: this circuit is partially redundant with respect to the circuit
8551 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
8552 -- the processing here. Also we still need the Checks circuit, since we
8553 -- have to be sure not to generate junk overflow checks in the first
8554 -- place, since it would be trick to remove them here!
8556 if Integer_Promotion_Possible (N) then
8558 -- All conditions met, go ahead with transformation
8566 Make_Type_Conversion (Loc,
8567 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
8568 Expression => Relocate_Node (Right_Opnd (Operand)));
8570 Opnd := New_Op_Node (Nkind (Operand), Loc);
8571 Set_Right_Opnd (Opnd, R);
8573 if Nkind (Operand) in N_Binary_Op then
8575 Make_Type_Conversion (Loc,
8576 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
8577 Expression => Relocate_Node (Left_Opnd (Operand)));
8579 Set_Left_Opnd (Opnd, L);
8583 Make_Type_Conversion (Loc,
8584 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
8585 Expression => Opnd));
8587 Analyze_And_Resolve (N, Target_Type);
8592 -- Do validity check if validity checking operands
8594 if Validity_Checks_On
8595 and then Validity_Check_Operands
8597 Ensure_Valid (Operand);
8600 -- Special case of converting from non-standard boolean type
8602 if Is_Boolean_Type (Operand_Type)
8603 and then (Nonzero_Is_True (Operand_Type))
8605 Adjust_Condition (Operand);
8606 Set_Etype (Operand, Standard_Boolean);
8607 Operand_Type := Standard_Boolean;
8610 -- Case of converting to an access type
8612 if Is_Access_Type (Target_Type) then
8614 -- Apply an accessibility check when the conversion operand is an
8615 -- access parameter (or a renaming thereof), unless conversion was
8616 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
8617 -- Note that other checks may still need to be applied below (such
8618 -- as tagged type checks).
8620 if Is_Entity_Name (Operand)
8622 (Is_Formal (Entity (Operand))
8624 (Present (Renamed_Object (Entity (Operand)))
8625 and then Is_Entity_Name (Renamed_Object (Entity (Operand)))
8627 (Entity (Renamed_Object (Entity (Operand))))))
8628 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
8629 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
8630 or else Attribute_Name (Original_Node (N)) = Name_Access)
8632 Apply_Accessibility_Check
8633 (Operand, Target_Type, Insert_Node => Operand);
8635 -- If the level of the operand type is statically deeper than the
8636 -- level of the target type, then force Program_Error. Note that this
8637 -- can only occur for cases where the attribute is within the body of
8638 -- an instantiation (otherwise the conversion will already have been
8639 -- rejected as illegal). Note: warnings are issued by the analyzer
8640 -- for the instance cases.
8642 elsif In_Instance_Body
8643 and then Type_Access_Level (Operand_Type) >
8644 Type_Access_Level (Target_Type)
8646 Raise_Accessibility_Error;
8648 -- When the operand is a selected access discriminant the check needs
8649 -- to be made against the level of the object denoted by the prefix
8650 -- of the selected name. Force Program_Error for this case as well
8651 -- (this accessibility violation can only happen if within the body
8652 -- of an instantiation).
8654 elsif In_Instance_Body
8655 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
8656 and then Nkind (Operand) = N_Selected_Component
8657 and then Object_Access_Level (Operand) >
8658 Type_Access_Level (Target_Type)
8660 Raise_Accessibility_Error;
8665 -- Case of conversions of tagged types and access to tagged types
8667 -- When needed, that is to say when the expression is class-wide, Add
8668 -- runtime a tag check for (strict) downward conversion by using the
8669 -- membership test, generating:
8671 -- [constraint_error when Operand not in Target_Type'Class]
8673 -- or in the access type case
8675 -- [constraint_error
8676 -- when Operand /= null
8677 -- and then Operand.all not in
8678 -- Designated_Type (Target_Type)'Class]
8680 if (Is_Access_Type (Target_Type)
8681 and then Is_Tagged_Type (Designated_Type (Target_Type)))
8682 or else Is_Tagged_Type (Target_Type)
8684 -- Do not do any expansion in the access type case if the parent is a
8685 -- renaming, since this is an error situation which will be caught by
8686 -- Sem_Ch8, and the expansion can interfere with this error check.
8688 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
8692 -- Otherwise, proceed with processing tagged conversion
8694 Tagged_Conversion : declare
8695 Actual_Op_Typ : Entity_Id;
8696 Actual_Targ_Typ : Entity_Id;
8697 Make_Conversion : Boolean := False;
8698 Root_Op_Typ : Entity_Id;
8700 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
8701 -- Create a membership check to test whether Operand is a member
8702 -- of Targ_Typ. If the original Target_Type is an access, include
8703 -- a test for null value. The check is inserted at N.
8705 --------------------
8706 -- Make_Tag_Check --
8707 --------------------
8709 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
8714 -- [Constraint_Error
8715 -- when Operand /= null
8716 -- and then Operand.all not in Targ_Typ]
8718 if Is_Access_Type (Target_Type) then
8723 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
8724 Right_Opnd => Make_Null (Loc)),
8729 Make_Explicit_Dereference (Loc,
8730 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
8731 Right_Opnd => New_Reference_To (Targ_Typ, Loc)));
8734 -- [Constraint_Error when Operand not in Targ_Typ]
8739 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
8740 Right_Opnd => New_Reference_To (Targ_Typ, Loc));
8744 Make_Raise_Constraint_Error (Loc,
8746 Reason => CE_Tag_Check_Failed));
8749 -- Start of processing for Tagged_Conversion
8752 -- Handle entities from the limited view
8754 if Is_Access_Type (Operand_Type) then
8756 Available_View (Designated_Type (Operand_Type));
8758 Actual_Op_Typ := Operand_Type;
8761 if Is_Access_Type (Target_Type) then
8763 Available_View (Designated_Type (Target_Type));
8765 Actual_Targ_Typ := Target_Type;
8768 Root_Op_Typ := Root_Type (Actual_Op_Typ);
8770 -- Ada 2005 (AI-251): Handle interface type conversion
8772 if Is_Interface (Actual_Op_Typ) then
8773 Expand_Interface_Conversion (N, Is_Static => False);
8777 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
8779 -- Create a runtime tag check for a downward class-wide type
8782 if Is_Class_Wide_Type (Actual_Op_Typ)
8783 and then Actual_Op_Typ /= Actual_Targ_Typ
8784 and then Root_Op_Typ /= Actual_Targ_Typ
8785 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ,
8786 Use_Full_View => True)
8788 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
8789 Make_Conversion := True;
8792 -- AI05-0073: If the result subtype of the function is defined
8793 -- by an access_definition designating a specific tagged type
8794 -- T, a check is made that the result value is null or the tag
8795 -- of the object designated by the result value identifies T.
8796 -- Constraint_Error is raised if this check fails.
8798 if Nkind (Parent (N)) = Sinfo.N_Return_Statement then
8801 Func_Typ : Entity_Id;
8804 -- Climb scope stack looking for the enclosing function
8806 Func := Current_Scope;
8807 while Present (Func)
8808 and then Ekind (Func) /= E_Function
8810 Func := Scope (Func);
8813 -- The function's return subtype must be defined using
8814 -- an access definition.
8816 if Nkind (Result_Definition (Parent (Func))) =
8819 Func_Typ := Directly_Designated_Type (Etype (Func));
8821 -- The return subtype denotes a specific tagged type,
8822 -- in other words, a non class-wide type.
8824 if Is_Tagged_Type (Func_Typ)
8825 and then not Is_Class_Wide_Type (Func_Typ)
8827 Make_Tag_Check (Actual_Targ_Typ);
8828 Make_Conversion := True;
8834 -- We have generated a tag check for either a class-wide type
8835 -- conversion or for AI05-0073.
8837 if Make_Conversion then
8842 Make_Unchecked_Type_Conversion (Loc,
8843 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
8844 Expression => Relocate_Node (Expression (N)));
8846 Analyze_And_Resolve (N, Target_Type);
8850 end Tagged_Conversion;
8852 -- Case of other access type conversions
8854 elsif Is_Access_Type (Target_Type) then
8855 Apply_Constraint_Check (Operand, Target_Type);
8857 -- Case of conversions from a fixed-point type
8859 -- These conversions require special expansion and processing, found in
8860 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
8861 -- since from a semantic point of view, these are simple integer
8862 -- conversions, which do not need further processing.
8864 elsif Is_Fixed_Point_Type (Operand_Type)
8865 and then not Conversion_OK (N)
8867 -- We should never see universal fixed at this case, since the
8868 -- expansion of the constituent divide or multiply should have
8869 -- eliminated the explicit mention of universal fixed.
8871 pragma Assert (Operand_Type /= Universal_Fixed);
8873 -- Check for special case of the conversion to universal real that
8874 -- occurs as a result of the use of a round attribute. In this case,
8875 -- the real type for the conversion is taken from the target type of
8876 -- the Round attribute and the result must be marked as rounded.
8878 if Target_Type = Universal_Real
8879 and then Nkind (Parent (N)) = N_Attribute_Reference
8880 and then Attribute_Name (Parent (N)) = Name_Round
8882 Set_Rounded_Result (N);
8883 Set_Etype (N, Etype (Parent (N)));
8886 -- Otherwise do correct fixed-conversion, but skip these if the
8887 -- Conversion_OK flag is set, because from a semantic point of view
8888 -- these are simple integer conversions needing no further processing
8889 -- (the backend will simply treat them as integers).
8891 if not Conversion_OK (N) then
8892 if Is_Fixed_Point_Type (Etype (N)) then
8893 Expand_Convert_Fixed_To_Fixed (N);
8896 elsif Is_Integer_Type (Etype (N)) then
8897 Expand_Convert_Fixed_To_Integer (N);
8900 pragma Assert (Is_Floating_Point_Type (Etype (N)));
8901 Expand_Convert_Fixed_To_Float (N);
8906 -- Case of conversions to a fixed-point type
8908 -- These conversions require special expansion and processing, found in
8909 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
8910 -- since from a semantic point of view, these are simple integer
8911 -- conversions, which do not need further processing.
8913 elsif Is_Fixed_Point_Type (Target_Type)
8914 and then not Conversion_OK (N)
8916 if Is_Integer_Type (Operand_Type) then
8917 Expand_Convert_Integer_To_Fixed (N);
8920 pragma Assert (Is_Floating_Point_Type (Operand_Type));
8921 Expand_Convert_Float_To_Fixed (N);
8925 -- Case of float-to-integer conversions
8927 -- We also handle float-to-fixed conversions with Conversion_OK set
8928 -- since semantically the fixed-point target is treated as though it
8929 -- were an integer in such cases.
8931 elsif Is_Floating_Point_Type (Operand_Type)
8933 (Is_Integer_Type (Target_Type)
8935 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
8937 -- One more check here, gcc is still not able to do conversions of
8938 -- this type with proper overflow checking, and so gigi is doing an
8939 -- approximation of what is required by doing floating-point compares
8940 -- with the end-point. But that can lose precision in some cases, and
8941 -- give a wrong result. Converting the operand to Universal_Real is
8942 -- helpful, but still does not catch all cases with 64-bit integers
8943 -- on targets with only 64-bit floats.
8945 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
8946 -- Can this code be removed ???
8948 if Do_Range_Check (Operand) then
8950 Make_Type_Conversion (Loc,
8952 New_Occurrence_Of (Universal_Real, Loc),
8954 Relocate_Node (Operand)));
8956 Set_Etype (Operand, Universal_Real);
8957 Enable_Range_Check (Operand);
8958 Set_Do_Range_Check (Expression (Operand), False);
8961 -- Case of array conversions
8963 -- Expansion of array conversions, add required length/range checks but
8964 -- only do this if there is no change of representation. For handling of
8965 -- this case, see Handle_Changed_Representation.
8967 elsif Is_Array_Type (Target_Type) then
8968 if Is_Constrained (Target_Type) then
8969 Apply_Length_Check (Operand, Target_Type);
8971 Apply_Range_Check (Operand, Target_Type);
8974 Handle_Changed_Representation;
8976 -- Case of conversions of discriminated types
8978 -- Add required discriminant checks if target is constrained. Again this
8979 -- change is skipped if we have a change of representation.
8981 elsif Has_Discriminants (Target_Type)
8982 and then Is_Constrained (Target_Type)
8984 Apply_Discriminant_Check (Operand, Target_Type);
8985 Handle_Changed_Representation;
8987 -- Case of all other record conversions. The only processing required
8988 -- is to check for a change of representation requiring the special
8989 -- assignment processing.
8991 elsif Is_Record_Type (Target_Type) then
8993 -- Ada 2005 (AI-216): Program_Error is raised when converting from
8994 -- a derived Unchecked_Union type to an unconstrained type that is
8995 -- not Unchecked_Union if the operand lacks inferable discriminants.
8997 if Is_Derived_Type (Operand_Type)
8998 and then Is_Unchecked_Union (Base_Type (Operand_Type))
8999 and then not Is_Constrained (Target_Type)
9000 and then not Is_Unchecked_Union (Base_Type (Target_Type))
9001 and then not Has_Inferable_Discriminants (Operand)
9003 -- To prevent Gigi from generating illegal code, we generate a
9004 -- Program_Error node, but we give it the target type of the
9008 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
9009 Reason => PE_Unchecked_Union_Restriction);
9012 Set_Etype (PE, Target_Type);
9017 Handle_Changed_Representation;
9020 -- Case of conversions of enumeration types
9022 elsif Is_Enumeration_Type (Target_Type) then
9024 -- Special processing is required if there is a change of
9025 -- representation (from enumeration representation clauses).
9027 if not Same_Representation (Target_Type, Operand_Type) then
9029 -- Convert: x(y) to x'val (ytyp'val (y))
9032 Make_Attribute_Reference (Loc,
9033 Prefix => New_Occurrence_Of (Target_Type, Loc),
9034 Attribute_Name => Name_Val,
9035 Expressions => New_List (
9036 Make_Attribute_Reference (Loc,
9037 Prefix => New_Occurrence_Of (Operand_Type, Loc),
9038 Attribute_Name => Name_Pos,
9039 Expressions => New_List (Operand)))));
9041 Analyze_And_Resolve (N, Target_Type);
9044 -- Case of conversions to floating-point
9046 elsif Is_Floating_Point_Type (Target_Type) then
9050 -- At this stage, either the conversion node has been transformed into
9051 -- some other equivalent expression, or left as a conversion that can be
9052 -- handled by Gigi, in the following cases:
9054 -- Conversions with no change of representation or type
9056 -- Numeric conversions involving integer, floating- and fixed-point
9057 -- values. Fixed-point values are allowed only if Conversion_OK is
9058 -- set, i.e. if the fixed-point values are to be treated as integers.
9060 -- No other conversions should be passed to Gigi
9062 -- Check: are these rules stated in sinfo??? if so, why restate here???
9064 -- The only remaining step is to generate a range check if we still have
9065 -- a type conversion at this stage and Do_Range_Check is set. For now we
9066 -- do this only for conversions of discrete types.
9068 if Nkind (N) = N_Type_Conversion
9069 and then Is_Discrete_Type (Etype (N))
9072 Expr : constant Node_Id := Expression (N);
9077 if Do_Range_Check (Expr)
9078 and then Is_Discrete_Type (Etype (Expr))
9080 Set_Do_Range_Check (Expr, False);
9082 -- Before we do a range check, we have to deal with treating a
9083 -- fixed-point operand as an integer. The way we do this is
9084 -- simply to do an unchecked conversion to an appropriate
9085 -- integer type large enough to hold the result.
9087 -- This code is not active yet, because we are only dealing
9088 -- with discrete types so far ???
9090 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
9091 and then Treat_Fixed_As_Integer (Expr)
9093 Ftyp := Base_Type (Etype (Expr));
9095 if Esize (Ftyp) >= Esize (Standard_Integer) then
9096 Ityp := Standard_Long_Long_Integer;
9098 Ityp := Standard_Integer;
9101 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
9104 -- Reset overflow flag, since the range check will include
9105 -- dealing with possible overflow, and generate the check. If
9106 -- Address is either a source type or target type, suppress
9107 -- range check to avoid typing anomalies when it is a visible
9110 Set_Do_Overflow_Check (N, False);
9111 if not Is_Descendent_Of_Address (Etype (Expr))
9112 and then not Is_Descendent_Of_Address (Target_Type)
9114 Generate_Range_Check
9115 (Expr, Target_Type, CE_Range_Check_Failed);
9121 -- Final step, if the result is a type conversion involving Vax_Float
9122 -- types, then it is subject for further special processing.
9124 if Nkind (N) = N_Type_Conversion
9125 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
9127 Expand_Vax_Conversion (N);
9131 -- Here at end of processing
9134 -- Apply predicate check if required. Note that we can't just call
9135 -- Apply_Predicate_Check here, because the type looks right after
9136 -- the conversion and it would omit the check. The Comes_From_Source
9137 -- guard is necessary to prevent infinite recursions when we generate
9138 -- internal conversions for the purpose of checking predicates.
9140 if Present (Predicate_Function (Target_Type))
9141 and then Target_Type /= Operand_Type
9142 and then Comes_From_Source (N)
9145 Make_Predicate_Check (Target_Type, Duplicate_Subexpr (N)));
9147 end Expand_N_Type_Conversion;
9149 -----------------------------------
9150 -- Expand_N_Unchecked_Expression --
9151 -----------------------------------
9153 -- Remove the unchecked expression node from the tree. Its job was simply
9154 -- to make sure that its constituent expression was handled with checks
9155 -- off, and now that that is done, we can remove it from the tree, and
9156 -- indeed must, since Gigi does not expect to see these nodes.
9158 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
9159 Exp : constant Node_Id := Expression (N);
9161 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
9163 end Expand_N_Unchecked_Expression;
9165 ----------------------------------------
9166 -- Expand_N_Unchecked_Type_Conversion --
9167 ----------------------------------------
9169 -- If this cannot be handled by Gigi and we haven't already made a
9170 -- temporary for it, do it now.
9172 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
9173 Target_Type : constant Entity_Id := Etype (N);
9174 Operand : constant Node_Id := Expression (N);
9175 Operand_Type : constant Entity_Id := Etype (Operand);
9178 -- Nothing at all to do if conversion is to the identical type so remove
9179 -- the conversion completely, it is useless, except that it may carry
9180 -- an Assignment_OK indication which must be propagated to the operand.
9182 if Operand_Type = Target_Type then
9184 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
9186 if Assignment_OK (N) then
9187 Set_Assignment_OK (Operand);
9190 Rewrite (N, Relocate_Node (Operand));
9194 -- If we have a conversion of a compile time known value to a target
9195 -- type and the value is in range of the target type, then we can simply
9196 -- replace the construct by an integer literal of the correct type. We
9197 -- only apply this to integer types being converted. Possibly it may
9198 -- apply in other cases, but it is too much trouble to worry about.
9200 -- Note that we do not do this transformation if the Kill_Range_Check
9201 -- flag is set, since then the value may be outside the expected range.
9202 -- This happens in the Normalize_Scalars case.
9204 -- We also skip this if either the target or operand type is biased
9205 -- because in this case, the unchecked conversion is supposed to
9206 -- preserve the bit pattern, not the integer value.
9208 if Is_Integer_Type (Target_Type)
9209 and then not Has_Biased_Representation (Target_Type)
9210 and then Is_Integer_Type (Operand_Type)
9211 and then not Has_Biased_Representation (Operand_Type)
9212 and then Compile_Time_Known_Value (Operand)
9213 and then not Kill_Range_Check (N)
9216 Val : constant Uint := Expr_Value (Operand);
9219 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
9221 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
9223 Val >= Expr_Value (Type_Low_Bound (Target_Type))
9225 Val <= Expr_Value (Type_High_Bound (Target_Type))
9227 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
9229 -- If Address is the target type, just set the type to avoid a
9230 -- spurious type error on the literal when Address is a visible
9233 if Is_Descendent_Of_Address (Target_Type) then
9234 Set_Etype (N, Target_Type);
9236 Analyze_And_Resolve (N, Target_Type);
9244 -- Nothing to do if conversion is safe
9246 if Safe_Unchecked_Type_Conversion (N) then
9250 -- Otherwise force evaluation unless Assignment_OK flag is set (this
9251 -- flag indicates ??? -- more comments needed here)
9253 if Assignment_OK (N) then
9256 Force_Evaluation (N);
9258 end Expand_N_Unchecked_Type_Conversion;
9260 ----------------------------
9261 -- Expand_Record_Equality --
9262 ----------------------------
9264 -- For non-variant records, Equality is expanded when needed into:
9266 -- and then Lhs.Discr1 = Rhs.Discr1
9268 -- and then Lhs.Discrn = Rhs.Discrn
9269 -- and then Lhs.Cmp1 = Rhs.Cmp1
9271 -- and then Lhs.Cmpn = Rhs.Cmpn
9273 -- The expression is folded by the back-end for adjacent fields. This
9274 -- function is called for tagged record in only one occasion: for imple-
9275 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
9276 -- otherwise the primitive "=" is used directly.
9278 function Expand_Record_Equality
9283 Bodies : List_Id) return Node_Id
9285 Loc : constant Source_Ptr := Sloc (Nod);
9290 First_Time : Boolean := True;
9292 function Suitable_Element (C : Entity_Id) return Entity_Id;
9293 -- Return the first field to compare beginning with C, skipping the
9294 -- inherited components.
9296 ----------------------
9297 -- Suitable_Element --
9298 ----------------------
9300 function Suitable_Element (C : Entity_Id) return Entity_Id is
9305 elsif Ekind (C) /= E_Discriminant
9306 and then Ekind (C) /= E_Component
9308 return Suitable_Element (Next_Entity (C));
9310 elsif Is_Tagged_Type (Typ)
9311 and then C /= Original_Record_Component (C)
9313 return Suitable_Element (Next_Entity (C));
9315 elsif Chars (C) = Name_uTag then
9316 return Suitable_Element (Next_Entity (C));
9318 elsif Is_Interface (Etype (C)) then
9319 return Suitable_Element (Next_Entity (C));
9324 end Suitable_Element;
9326 -- Start of processing for Expand_Record_Equality
9329 -- Generates the following code: (assuming that Typ has one Discr and
9330 -- component C2 is also a record)
9333 -- and then Lhs.Discr1 = Rhs.Discr1
9334 -- and then Lhs.C1 = Rhs.C1
9335 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
9337 -- and then Lhs.Cmpn = Rhs.Cmpn
9339 Result := New_Reference_To (Standard_True, Loc);
9340 C := Suitable_Element (First_Entity (Typ));
9341 while Present (C) loop
9349 First_Time := False;
9353 New_Lhs := New_Copy_Tree (Lhs);
9354 New_Rhs := New_Copy_Tree (Rhs);
9358 Expand_Composite_Equality (Nod, Etype (C),
9360 Make_Selected_Component (Loc,
9362 Selector_Name => New_Reference_To (C, Loc)),
9364 Make_Selected_Component (Loc,
9366 Selector_Name => New_Reference_To (C, Loc)),
9369 -- If some (sub)component is an unchecked_union, the whole
9370 -- operation will raise program error.
9372 if Nkind (Check) = N_Raise_Program_Error then
9374 Set_Etype (Result, Standard_Boolean);
9379 Left_Opnd => Result,
9380 Right_Opnd => Check);
9384 C := Suitable_Element (Next_Entity (C));
9388 end Expand_Record_Equality;
9390 -----------------------------------
9391 -- Expand_Short_Circuit_Operator --
9392 -----------------------------------
9394 -- Deal with special expansion if actions are present for the right operand
9395 -- and deal with optimizing case of arguments being True or False. We also
9396 -- deal with the special case of non-standard boolean values.
9398 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
9399 Loc : constant Source_Ptr := Sloc (N);
9400 Typ : constant Entity_Id := Etype (N);
9401 Left : constant Node_Id := Left_Opnd (N);
9402 Right : constant Node_Id := Right_Opnd (N);
9403 LocR : constant Source_Ptr := Sloc (Right);
9406 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
9407 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
9408 -- If Left = Shortcut_Value then Right need not be evaluated
9410 function Make_Test_Expr (Opnd : Node_Id) return Node_Id;
9411 -- For Opnd a boolean expression, return a Boolean expression equivalent
9412 -- to Opnd /= Shortcut_Value.
9414 --------------------
9415 -- Make_Test_Expr --
9416 --------------------
9418 function Make_Test_Expr (Opnd : Node_Id) return Node_Id is
9420 if Shortcut_Value then
9421 return Make_Op_Not (Sloc (Opnd), Opnd);
9428 -- Entity for a temporary variable holding the value of the operator,
9429 -- used for expansion in the case where actions are present.
9431 -- Start of processing for Expand_Short_Circuit_Operator
9434 -- Deal with non-standard booleans
9436 if Is_Boolean_Type (Typ) then
9437 Adjust_Condition (Left);
9438 Adjust_Condition (Right);
9439 Set_Etype (N, Standard_Boolean);
9442 -- Check for cases where left argument is known to be True or False
9444 if Compile_Time_Known_Value (Left) then
9446 -- Mark SCO for left condition as compile time known
9448 if Generate_SCO and then Comes_From_Source (Left) then
9449 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
9452 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
9453 -- Any actions associated with Right will be executed unconditionally
9454 -- and can thus be inserted into the tree unconditionally.
9456 if Expr_Value_E (Left) /= Shortcut_Ent then
9457 if Present (Actions (N)) then
9458 Insert_Actions (N, Actions (N));
9463 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
9464 -- In this case we can forget the actions associated with Right,
9465 -- since they will never be executed.
9468 Kill_Dead_Code (Right);
9469 Kill_Dead_Code (Actions (N));
9470 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
9473 Adjust_Result_Type (N, Typ);
9477 -- If Actions are present for the right operand, we have to do some
9478 -- special processing. We can't just let these actions filter back into
9479 -- code preceding the short circuit (which is what would have happened
9480 -- if we had not trapped them in the short-circuit form), since they
9481 -- must only be executed if the right operand of the short circuit is
9482 -- executed and not otherwise.
9484 -- the temporary variable C.
9486 if Present (Actions (N)) then
9487 Actlist := Actions (N);
9489 -- The old approach is to expand:
9491 -- left AND THEN right
9495 -- C : Boolean := False;
9503 -- and finally rewrite the operator into a reference to C. Similarly
9504 -- for left OR ELSE right, with negated values. Note that this
9505 -- rewrite causes some difficulties for coverage analysis because
9506 -- of the introduction of the new variable C, which obscures the
9507 -- structure of the test.
9509 -- We use this "old approach" if use of N_Expression_With_Actions
9510 -- is False (see description in Opt of when this is or is not set).
9512 if not Use_Expression_With_Actions then
9513 Op_Var := Make_Temporary (Loc, 'C', Related_Node => N);
9516 Make_Object_Declaration (Loc,
9517 Defining_Identifier =>
9519 Object_Definition =>
9520 New_Occurrence_Of (Standard_Boolean, Loc),
9522 New_Occurrence_Of (Shortcut_Ent, Loc)));
9525 Make_Implicit_If_Statement (Right,
9526 Condition => Make_Test_Expr (Right),
9527 Then_Statements => New_List (
9528 Make_Assignment_Statement (LocR,
9529 Name => New_Occurrence_Of (Op_Var, LocR),
9532 (Boolean_Literals (not Shortcut_Value), LocR)))));
9535 Make_Implicit_If_Statement (Left,
9536 Condition => Make_Test_Expr (Left),
9537 Then_Statements => Actlist));
9539 Rewrite (N, New_Occurrence_Of (Op_Var, Loc));
9540 Analyze_And_Resolve (N, Standard_Boolean);
9542 -- The new approach, activated for now by the use of debug flag
9543 -- -gnatd.X is to use the new Expression_With_Actions node for the
9544 -- right operand of the short-circuit form. This should solve the
9545 -- traceability problems for coverage analysis.
9549 Make_Expression_With_Actions (LocR,
9550 Expression => Relocate_Node (Right),
9551 Actions => Actlist));
9552 Set_Actions (N, No_List);
9553 Analyze_And_Resolve (Right, Standard_Boolean);
9556 Adjust_Result_Type (N, Typ);
9560 -- No actions present, check for cases of right argument True/False
9562 if Compile_Time_Known_Value (Right) then
9564 -- Mark SCO for left condition as compile time known
9566 if Generate_SCO and then Comes_From_Source (Right) then
9567 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
9570 -- Change (Left and then True), (Left or else False) to Left.
9571 -- Note that we know there are no actions associated with the right
9572 -- operand, since we just checked for this case above.
9574 if Expr_Value_E (Right) /= Shortcut_Ent then
9577 -- Change (Left and then False), (Left or else True) to Right,
9578 -- making sure to preserve any side effects associated with the Left
9582 Remove_Side_Effects (Left);
9583 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
9587 Adjust_Result_Type (N, Typ);
9588 end Expand_Short_Circuit_Operator;
9590 -------------------------------------
9591 -- Fixup_Universal_Fixed_Operation --
9592 -------------------------------------
9594 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
9595 Conv : constant Node_Id := Parent (N);
9598 -- We must have a type conversion immediately above us
9600 pragma Assert (Nkind (Conv) = N_Type_Conversion);
9602 -- Normally the type conversion gives our target type. The exception
9603 -- occurs in the case of the Round attribute, where the conversion
9604 -- will be to universal real, and our real type comes from the Round
9605 -- attribute (as well as an indication that we must round the result)
9607 if Nkind (Parent (Conv)) = N_Attribute_Reference
9608 and then Attribute_Name (Parent (Conv)) = Name_Round
9610 Set_Etype (N, Etype (Parent (Conv)));
9611 Set_Rounded_Result (N);
9613 -- Normal case where type comes from conversion above us
9616 Set_Etype (N, Etype (Conv));
9618 end Fixup_Universal_Fixed_Operation;
9620 ---------------------------------
9621 -- Has_Inferable_Discriminants --
9622 ---------------------------------
9624 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
9626 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
9627 -- Determines whether the left-most prefix of a selected component is a
9628 -- formal parameter in a subprogram. Assumes N is a selected component.
9630 --------------------------------
9631 -- Prefix_Is_Formal_Parameter --
9632 --------------------------------
9634 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
9635 Sel_Comp : Node_Id := N;
9638 -- Move to the left-most prefix by climbing up the tree
9640 while Present (Parent (Sel_Comp))
9641 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
9643 Sel_Comp := Parent (Sel_Comp);
9646 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
9647 end Prefix_Is_Formal_Parameter;
9649 -- Start of processing for Has_Inferable_Discriminants
9652 -- For identifiers and indexed components, it is sufficient to have a
9653 -- constrained Unchecked_Union nominal subtype.
9655 if Nkind_In (N, N_Identifier, N_Indexed_Component) then
9656 return Is_Unchecked_Union (Base_Type (Etype (N)))
9658 Is_Constrained (Etype (N));
9660 -- For selected components, the subtype of the selector must be a
9661 -- constrained Unchecked_Union. If the component is subject to a
9662 -- per-object constraint, then the enclosing object must have inferable
9665 elsif Nkind (N) = N_Selected_Component then
9666 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
9668 -- A small hack. If we have a per-object constrained selected
9669 -- component of a formal parameter, return True since we do not
9670 -- know the actual parameter association yet.
9672 if Prefix_Is_Formal_Parameter (N) then
9676 -- Otherwise, check the enclosing object and the selector
9678 return Has_Inferable_Discriminants (Prefix (N))
9680 Has_Inferable_Discriminants (Selector_Name (N));
9683 -- The call to Has_Inferable_Discriminants will determine whether
9684 -- the selector has a constrained Unchecked_Union nominal type.
9686 return Has_Inferable_Discriminants (Selector_Name (N));
9688 -- A qualified expression has inferable discriminants if its subtype
9689 -- mark is a constrained Unchecked_Union subtype.
9691 elsif Nkind (N) = N_Qualified_Expression then
9692 return Is_Unchecked_Union (Subtype_Mark (N))
9694 Is_Constrained (Subtype_Mark (N));
9699 end Has_Inferable_Discriminants;
9701 -------------------------------
9702 -- Insert_Dereference_Action --
9703 -------------------------------
9705 procedure Insert_Dereference_Action (N : Node_Id) is
9706 Loc : constant Source_Ptr := Sloc (N);
9707 Typ : constant Entity_Id := Etype (N);
9708 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
9709 Pnod : constant Node_Id := Parent (N);
9711 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
9712 -- Return true if type of P is derived from Checked_Pool;
9714 -----------------------------
9715 -- Is_Checked_Storage_Pool --
9716 -----------------------------
9718 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
9727 while T /= Etype (T) loop
9728 if Is_RTE (T, RE_Checked_Pool) then
9736 end Is_Checked_Storage_Pool;
9738 -- Start of processing for Insert_Dereference_Action
9741 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
9743 if not (Is_Checked_Storage_Pool (Pool)
9744 and then Comes_From_Source (Original_Node (Pnod)))
9750 Make_Procedure_Call_Statement (Loc,
9751 Name => New_Reference_To (
9752 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
9754 Parameter_Associations => New_List (
9758 New_Reference_To (Pool, Loc),
9760 -- Storage_Address. We use the attribute Pool_Address, which uses
9761 -- the pointer itself to find the address of the object, and which
9762 -- handles unconstrained arrays properly by computing the address
9763 -- of the template. i.e. the correct address of the corresponding
9766 Make_Attribute_Reference (Loc,
9767 Prefix => Duplicate_Subexpr_Move_Checks (N),
9768 Attribute_Name => Name_Pool_Address),
9770 -- Size_In_Storage_Elements
9772 Make_Op_Divide (Loc,
9774 Make_Attribute_Reference (Loc,
9776 Make_Explicit_Dereference (Loc,
9777 Duplicate_Subexpr_Move_Checks (N)),
9778 Attribute_Name => Name_Size),
9780 Make_Integer_Literal (Loc, System_Storage_Unit)),
9784 Make_Attribute_Reference (Loc,
9786 Make_Explicit_Dereference (Loc,
9787 Duplicate_Subexpr_Move_Checks (N)),
9788 Attribute_Name => Name_Alignment))));
9791 when RE_Not_Available =>
9793 end Insert_Dereference_Action;
9795 --------------------------------
9796 -- Integer_Promotion_Possible --
9797 --------------------------------
9799 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
9800 Operand : constant Node_Id := Expression (N);
9801 Operand_Type : constant Entity_Id := Etype (Operand);
9802 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
9805 pragma Assert (Nkind (N) = N_Type_Conversion);
9809 -- We only do the transformation for source constructs. We assume
9810 -- that the expander knows what it is doing when it generates code.
9812 Comes_From_Source (N)
9814 -- If the operand type is Short_Integer or Short_Short_Integer,
9815 -- then we will promote to Integer, which is available on all
9816 -- targets, and is sufficient to ensure no intermediate overflow.
9817 -- Furthermore it is likely to be as efficient or more efficient
9818 -- than using the smaller type for the computation so we do this
9822 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
9824 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
9826 -- Test for interesting operation, which includes addition,
9827 -- division, exponentiation, multiplication, subtraction, absolute
9828 -- value and unary negation. Unary "+" is omitted since it is a
9829 -- no-op and thus can't overflow.
9831 and then Nkind_In (Operand, N_Op_Abs,
9838 end Integer_Promotion_Possible;
9840 ------------------------------
9841 -- Make_Array_Comparison_Op --
9842 ------------------------------
9844 -- This is a hand-coded expansion of the following generic function:
9847 -- type elem is (<>);
9848 -- type index is (<>);
9849 -- type a is array (index range <>) of elem;
9851 -- function Gnnn (X : a; Y: a) return boolean is
9852 -- J : index := Y'first;
9855 -- if X'length = 0 then
9858 -- elsif Y'length = 0 then
9862 -- for I in X'range loop
9863 -- if X (I) = Y (J) then
9864 -- if J = Y'last then
9867 -- J := index'succ (J);
9871 -- return X (I) > Y (J);
9875 -- return X'length > Y'length;
9879 -- Note that since we are essentially doing this expansion by hand, we
9880 -- do not need to generate an actual or formal generic part, just the
9881 -- instantiated function itself.
9883 function Make_Array_Comparison_Op
9885 Nod : Node_Id) return Node_Id
9887 Loc : constant Source_Ptr := Sloc (Nod);
9889 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
9890 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
9891 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
9892 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
9894 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
9896 Loop_Statement : Node_Id;
9897 Loop_Body : Node_Id;
9900 Final_Expr : Node_Id;
9901 Func_Body : Node_Id;
9902 Func_Name : Entity_Id;
9908 -- if J = Y'last then
9911 -- J := index'succ (J);
9915 Make_Implicit_If_Statement (Nod,
9918 Left_Opnd => New_Reference_To (J, Loc),
9920 Make_Attribute_Reference (Loc,
9921 Prefix => New_Reference_To (Y, Loc),
9922 Attribute_Name => Name_Last)),
9924 Then_Statements => New_List (
9925 Make_Exit_Statement (Loc)),
9929 Make_Assignment_Statement (Loc,
9930 Name => New_Reference_To (J, Loc),
9932 Make_Attribute_Reference (Loc,
9933 Prefix => New_Reference_To (Index, Loc),
9934 Attribute_Name => Name_Succ,
9935 Expressions => New_List (New_Reference_To (J, Loc))))));
9937 -- if X (I) = Y (J) then
9940 -- return X (I) > Y (J);
9944 Make_Implicit_If_Statement (Nod,
9948 Make_Indexed_Component (Loc,
9949 Prefix => New_Reference_To (X, Loc),
9950 Expressions => New_List (New_Reference_To (I, Loc))),
9953 Make_Indexed_Component (Loc,
9954 Prefix => New_Reference_To (Y, Loc),
9955 Expressions => New_List (New_Reference_To (J, Loc)))),
9957 Then_Statements => New_List (Inner_If),
9959 Else_Statements => New_List (
9960 Make_Simple_Return_Statement (Loc,
9964 Make_Indexed_Component (Loc,
9965 Prefix => New_Reference_To (X, Loc),
9966 Expressions => New_List (New_Reference_To (I, Loc))),
9969 Make_Indexed_Component (Loc,
9970 Prefix => New_Reference_To (Y, Loc),
9971 Expressions => New_List (
9972 New_Reference_To (J, Loc)))))));
9974 -- for I in X'range loop
9979 Make_Implicit_Loop_Statement (Nod,
9980 Identifier => Empty,
9983 Make_Iteration_Scheme (Loc,
9984 Loop_Parameter_Specification =>
9985 Make_Loop_Parameter_Specification (Loc,
9986 Defining_Identifier => I,
9987 Discrete_Subtype_Definition =>
9988 Make_Attribute_Reference (Loc,
9989 Prefix => New_Reference_To (X, Loc),
9990 Attribute_Name => Name_Range))),
9992 Statements => New_List (Loop_Body));
9994 -- if X'length = 0 then
9996 -- elsif Y'length = 0 then
9999 -- for ... loop ... end loop;
10000 -- return X'length > Y'length;
10004 Make_Attribute_Reference (Loc,
10005 Prefix => New_Reference_To (X, Loc),
10006 Attribute_Name => Name_Length);
10009 Make_Attribute_Reference (Loc,
10010 Prefix => New_Reference_To (Y, Loc),
10011 Attribute_Name => Name_Length);
10015 Left_Opnd => Length1,
10016 Right_Opnd => Length2);
10019 Make_Implicit_If_Statement (Nod,
10023 Make_Attribute_Reference (Loc,
10024 Prefix => New_Reference_To (X, Loc),
10025 Attribute_Name => Name_Length),
10027 Make_Integer_Literal (Loc, 0)),
10031 Make_Simple_Return_Statement (Loc,
10032 Expression => New_Reference_To (Standard_False, Loc))),
10034 Elsif_Parts => New_List (
10035 Make_Elsif_Part (Loc,
10039 Make_Attribute_Reference (Loc,
10040 Prefix => New_Reference_To (Y, Loc),
10041 Attribute_Name => Name_Length),
10043 Make_Integer_Literal (Loc, 0)),
10047 Make_Simple_Return_Statement (Loc,
10048 Expression => New_Reference_To (Standard_True, Loc))))),
10050 Else_Statements => New_List (
10052 Make_Simple_Return_Statement (Loc,
10053 Expression => Final_Expr)));
10057 Formals := New_List (
10058 Make_Parameter_Specification (Loc,
10059 Defining_Identifier => X,
10060 Parameter_Type => New_Reference_To (Typ, Loc)),
10062 Make_Parameter_Specification (Loc,
10063 Defining_Identifier => Y,
10064 Parameter_Type => New_Reference_To (Typ, Loc)));
10066 -- function Gnnn (...) return boolean is
10067 -- J : index := Y'first;
10072 Func_Name := Make_Temporary (Loc, 'G');
10075 Make_Subprogram_Body (Loc,
10077 Make_Function_Specification (Loc,
10078 Defining_Unit_Name => Func_Name,
10079 Parameter_Specifications => Formals,
10080 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
10082 Declarations => New_List (
10083 Make_Object_Declaration (Loc,
10084 Defining_Identifier => J,
10085 Object_Definition => New_Reference_To (Index, Loc),
10087 Make_Attribute_Reference (Loc,
10088 Prefix => New_Reference_To (Y, Loc),
10089 Attribute_Name => Name_First))),
10091 Handled_Statement_Sequence =>
10092 Make_Handled_Sequence_Of_Statements (Loc,
10093 Statements => New_List (If_Stat)));
10096 end Make_Array_Comparison_Op;
10098 ---------------------------
10099 -- Make_Boolean_Array_Op --
10100 ---------------------------
10102 -- For logical operations on boolean arrays, expand in line the following,
10103 -- replacing 'and' with 'or' or 'xor' where needed:
10105 -- function Annn (A : typ; B: typ) return typ is
10108 -- for J in A'range loop
10109 -- C (J) := A (J) op B (J);
10114 -- Here typ is the boolean array type
10116 function Make_Boolean_Array_Op
10118 N : Node_Id) return Node_Id
10120 Loc : constant Source_Ptr := Sloc (N);
10122 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
10123 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
10124 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
10125 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
10133 Func_Name : Entity_Id;
10134 Func_Body : Node_Id;
10135 Loop_Statement : Node_Id;
10139 Make_Indexed_Component (Loc,
10140 Prefix => New_Reference_To (A, Loc),
10141 Expressions => New_List (New_Reference_To (J, Loc)));
10144 Make_Indexed_Component (Loc,
10145 Prefix => New_Reference_To (B, Loc),
10146 Expressions => New_List (New_Reference_To (J, Loc)));
10149 Make_Indexed_Component (Loc,
10150 Prefix => New_Reference_To (C, Loc),
10151 Expressions => New_List (New_Reference_To (J, Loc)));
10153 if Nkind (N) = N_Op_And then
10157 Right_Opnd => B_J);
10159 elsif Nkind (N) = N_Op_Or then
10163 Right_Opnd => B_J);
10169 Right_Opnd => B_J);
10173 Make_Implicit_Loop_Statement (N,
10174 Identifier => Empty,
10176 Iteration_Scheme =>
10177 Make_Iteration_Scheme (Loc,
10178 Loop_Parameter_Specification =>
10179 Make_Loop_Parameter_Specification (Loc,
10180 Defining_Identifier => J,
10181 Discrete_Subtype_Definition =>
10182 Make_Attribute_Reference (Loc,
10183 Prefix => New_Reference_To (A, Loc),
10184 Attribute_Name => Name_Range))),
10186 Statements => New_List (
10187 Make_Assignment_Statement (Loc,
10189 Expression => Op)));
10191 Formals := New_List (
10192 Make_Parameter_Specification (Loc,
10193 Defining_Identifier => A,
10194 Parameter_Type => New_Reference_To (Typ, Loc)),
10196 Make_Parameter_Specification (Loc,
10197 Defining_Identifier => B,
10198 Parameter_Type => New_Reference_To (Typ, Loc)));
10200 Func_Name := Make_Temporary (Loc, 'A');
10201 Set_Is_Inlined (Func_Name);
10204 Make_Subprogram_Body (Loc,
10206 Make_Function_Specification (Loc,
10207 Defining_Unit_Name => Func_Name,
10208 Parameter_Specifications => Formals,
10209 Result_Definition => New_Reference_To (Typ, Loc)),
10211 Declarations => New_List (
10212 Make_Object_Declaration (Loc,
10213 Defining_Identifier => C,
10214 Object_Definition => New_Reference_To (Typ, Loc))),
10216 Handled_Statement_Sequence =>
10217 Make_Handled_Sequence_Of_Statements (Loc,
10218 Statements => New_List (
10220 Make_Simple_Return_Statement (Loc,
10221 Expression => New_Reference_To (C, Loc)))));
10224 end Make_Boolean_Array_Op;
10226 --------------------------------
10227 -- Optimize_Length_Comparison --
10228 --------------------------------
10230 procedure Optimize_Length_Comparison (N : Node_Id) is
10231 Loc : constant Source_Ptr := Sloc (N);
10232 Typ : constant Entity_Id := Etype (N);
10237 -- First and Last attribute reference nodes, which end up as left and
10238 -- right operands of the optimized result.
10241 -- True for comparison operand of zero
10244 -- Comparison operand, set only if Is_Zero is false
10247 -- Entity whose length is being compared
10250 -- Integer_Literal node for length attribute expression, or Empty
10251 -- if there is no such expression present.
10254 -- Type of array index to which 'Length is applied
10256 Op : Node_Kind := Nkind (N);
10257 -- Kind of comparison operator, gets flipped if operands backwards
10259 function Is_Optimizable (N : Node_Id) return Boolean;
10260 -- Tests N to see if it is an optimizable comparison value (defined as
10261 -- constant zero or one, or something else where the value is known to
10262 -- be positive and in the range of 32-bits, and where the corresponding
10263 -- Length value is also known to be 32-bits. If result is true, sets
10264 -- Is_Zero, Ityp, and Comp accordingly.
10266 function Is_Entity_Length (N : Node_Id) return Boolean;
10267 -- Tests if N is a length attribute applied to a simple entity. If so,
10268 -- returns True, and sets Ent to the entity, and Index to the integer
10269 -- literal provided as an attribute expression, or to Empty if none.
10270 -- Also returns True if the expression is a generated type conversion
10271 -- whose expression is of the desired form. This latter case arises
10272 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
10273 -- to check for being in range, which is not needed in this context.
10274 -- Returns False if neither condition holds.
10276 function Prepare_64 (N : Node_Id) return Node_Id;
10277 -- Given a discrete expression, returns a Long_Long_Integer typed
10278 -- expression representing the underlying value of the expression.
10279 -- This is done with an unchecked conversion to the result type. We
10280 -- use unchecked conversion to handle the enumeration type case.
10282 ----------------------
10283 -- Is_Entity_Length --
10284 ----------------------
10286 function Is_Entity_Length (N : Node_Id) return Boolean is
10288 if Nkind (N) = N_Attribute_Reference
10289 and then Attribute_Name (N) = Name_Length
10290 and then Is_Entity_Name (Prefix (N))
10292 Ent := Entity (Prefix (N));
10294 if Present (Expressions (N)) then
10295 Index := First (Expressions (N));
10302 elsif Nkind (N) = N_Type_Conversion
10303 and then not Comes_From_Source (N)
10305 return Is_Entity_Length (Expression (N));
10310 end Is_Entity_Length;
10312 --------------------
10313 -- Is_Optimizable --
10314 --------------------
10316 function Is_Optimizable (N : Node_Id) return Boolean is
10324 if Compile_Time_Known_Value (N) then
10325 Val := Expr_Value (N);
10327 if Val = Uint_0 then
10332 elsif Val = Uint_1 then
10339 -- Here we have to make sure of being within 32-bits
10341 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
10344 or else Lo < Uint_1
10345 or else Hi > UI_From_Int (Int'Last)
10350 -- Comparison value was within range, so now we must check the index
10351 -- value to make sure it is also within 32-bits.
10353 Indx := First_Index (Etype (Ent));
10355 if Present (Index) then
10356 for J in 2 .. UI_To_Int (Intval (Index)) loop
10361 Ityp := Etype (Indx);
10363 if Esize (Ityp) > 32 then
10370 end Is_Optimizable;
10376 function Prepare_64 (N : Node_Id) return Node_Id is
10378 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
10381 -- Start of processing for Optimize_Length_Comparison
10384 -- Nothing to do if not a comparison
10386 if Op not in N_Op_Compare then
10390 -- Nothing to do if special -gnatd.P debug flag set
10392 if Debug_Flag_Dot_PP then
10396 -- Ent'Length op 0/1
10398 if Is_Entity_Length (Left_Opnd (N))
10399 and then Is_Optimizable (Right_Opnd (N))
10403 -- 0/1 op Ent'Length
10405 elsif Is_Entity_Length (Right_Opnd (N))
10406 and then Is_Optimizable (Left_Opnd (N))
10408 -- Flip comparison to opposite sense
10411 when N_Op_Lt => Op := N_Op_Gt;
10412 when N_Op_Le => Op := N_Op_Ge;
10413 when N_Op_Gt => Op := N_Op_Lt;
10414 when N_Op_Ge => Op := N_Op_Le;
10415 when others => null;
10418 -- Else optimization not possible
10424 -- Fall through if we will do the optimization
10426 -- Cases to handle:
10428 -- X'Length = 0 => X'First > X'Last
10429 -- X'Length = 1 => X'First = X'Last
10430 -- X'Length = n => X'First + (n - 1) = X'Last
10432 -- X'Length /= 0 => X'First <= X'Last
10433 -- X'Length /= 1 => X'First /= X'Last
10434 -- X'Length /= n => X'First + (n - 1) /= X'Last
10436 -- X'Length >= 0 => always true, warn
10437 -- X'Length >= 1 => X'First <= X'Last
10438 -- X'Length >= n => X'First + (n - 1) <= X'Last
10440 -- X'Length > 0 => X'First <= X'Last
10441 -- X'Length > 1 => X'First < X'Last
10442 -- X'Length > n => X'First + (n - 1) < X'Last
10444 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
10445 -- X'Length <= 1 => X'First >= X'Last
10446 -- X'Length <= n => X'First + (n - 1) >= X'Last
10448 -- X'Length < 0 => always false (warn)
10449 -- X'Length < 1 => X'First > X'Last
10450 -- X'Length < n => X'First + (n - 1) > X'Last
10452 -- Note: for the cases of n (not constant 0,1), we require that the
10453 -- corresponding index type be integer or shorter (i.e. not 64-bit),
10454 -- and the same for the comparison value. Then we do the comparison
10455 -- using 64-bit arithmetic (actually long long integer), so that we
10456 -- cannot have overflow intefering with the result.
10458 -- First deal with warning cases
10467 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
10468 Analyze_And_Resolve (N, Typ);
10469 Warn_On_Known_Condition (N);
10476 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
10477 Analyze_And_Resolve (N, Typ);
10478 Warn_On_Known_Condition (N);
10482 if Constant_Condition_Warnings
10483 and then Comes_From_Source (Original_Node (N))
10485 Error_Msg_N ("could replace by ""'=""?", N);
10495 -- Build the First reference we will use
10498 Make_Attribute_Reference (Loc,
10499 Prefix => New_Occurrence_Of (Ent, Loc),
10500 Attribute_Name => Name_First);
10502 if Present (Index) then
10503 Set_Expressions (Left, New_List (New_Copy (Index)));
10506 -- If general value case, then do the addition of (n - 1), and
10507 -- also add the needed conversions to type Long_Long_Integer.
10509 if Present (Comp) then
10512 Left_Opnd => Prepare_64 (Left),
10514 Make_Op_Subtract (Loc,
10515 Left_Opnd => Prepare_64 (Comp),
10516 Right_Opnd => Make_Integer_Literal (Loc, 1)));
10519 -- Build the Last reference we will use
10522 Make_Attribute_Reference (Loc,
10523 Prefix => New_Occurrence_Of (Ent, Loc),
10524 Attribute_Name => Name_Last);
10526 if Present (Index) then
10527 Set_Expressions (Right, New_List (New_Copy (Index)));
10530 -- If general operand, convert Last reference to Long_Long_Integer
10532 if Present (Comp) then
10533 Right := Prepare_64 (Right);
10536 -- Check for cases to optimize
10538 -- X'Length = 0 => X'First > X'Last
10539 -- X'Length < 1 => X'First > X'Last
10540 -- X'Length < n => X'First + (n - 1) > X'Last
10542 if (Is_Zero and then Op = N_Op_Eq)
10543 or else (not Is_Zero and then Op = N_Op_Lt)
10548 Right_Opnd => Right);
10550 -- X'Length = 1 => X'First = X'Last
10551 -- X'Length = n => X'First + (n - 1) = X'Last
10553 elsif not Is_Zero and then Op = N_Op_Eq then
10557 Right_Opnd => Right);
10559 -- X'Length /= 0 => X'First <= X'Last
10560 -- X'Length > 0 => X'First <= X'Last
10562 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
10566 Right_Opnd => Right);
10568 -- X'Length /= 1 => X'First /= X'Last
10569 -- X'Length /= n => X'First + (n - 1) /= X'Last
10571 elsif not Is_Zero and then Op = N_Op_Ne then
10575 Right_Opnd => Right);
10577 -- X'Length >= 1 => X'First <= X'Last
10578 -- X'Length >= n => X'First + (n - 1) <= X'Last
10580 elsif not Is_Zero and then Op = N_Op_Ge then
10584 Right_Opnd => Right);
10586 -- X'Length > 1 => X'First < X'Last
10587 -- X'Length > n => X'First + (n = 1) < X'Last
10589 elsif not Is_Zero and then Op = N_Op_Gt then
10593 Right_Opnd => Right);
10595 -- X'Length <= 1 => X'First >= X'Last
10596 -- X'Length <= n => X'First + (n - 1) >= X'Last
10598 elsif not Is_Zero and then Op = N_Op_Le then
10602 Right_Opnd => Right);
10604 -- Should not happen at this stage
10607 raise Program_Error;
10610 -- Rewrite and finish up
10612 Rewrite (N, Result);
10613 Analyze_And_Resolve (N, Typ);
10615 end Optimize_Length_Comparison;
10617 ------------------------
10618 -- Rewrite_Comparison --
10619 ------------------------
10621 procedure Rewrite_Comparison (N : Node_Id) is
10622 Warning_Generated : Boolean := False;
10623 -- Set to True if first pass with Assume_Valid generates a warning in
10624 -- which case we skip the second pass to avoid warning overloaded.
10627 -- Set to Standard_True or Standard_False
10630 if Nkind (N) = N_Type_Conversion then
10631 Rewrite_Comparison (Expression (N));
10634 elsif Nkind (N) not in N_Op_Compare then
10638 -- Now start looking at the comparison in detail. We potentially go
10639 -- through this loop twice. The first time, Assume_Valid is set False
10640 -- in the call to Compile_Time_Compare. If this call results in a
10641 -- clear result of always True or Always False, that's decisive and
10642 -- we are done. Otherwise we repeat the processing with Assume_Valid
10643 -- set to True to generate additional warnings. We can skip that step
10644 -- if Constant_Condition_Warnings is False.
10646 for AV in False .. True loop
10648 Typ : constant Entity_Id := Etype (N);
10649 Op1 : constant Node_Id := Left_Opnd (N);
10650 Op2 : constant Node_Id := Right_Opnd (N);
10652 Res : constant Compare_Result :=
10653 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
10654 -- Res indicates if compare outcome can be compile time determined
10656 True_Result : Boolean;
10657 False_Result : Boolean;
10660 case N_Op_Compare (Nkind (N)) is
10662 True_Result := Res = EQ;
10663 False_Result := Res = LT or else Res = GT or else Res = NE;
10666 True_Result := Res in Compare_GE;
10667 False_Result := Res = LT;
10670 and then Constant_Condition_Warnings
10671 and then Comes_From_Source (Original_Node (N))
10672 and then Nkind (Original_Node (N)) = N_Op_Ge
10673 and then not In_Instance
10674 and then Is_Integer_Type (Etype (Left_Opnd (N)))
10675 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
10678 ("can never be greater than, could replace by ""'=""?", N);
10679 Warning_Generated := True;
10683 True_Result := Res = GT;
10684 False_Result := Res in Compare_LE;
10687 True_Result := Res = LT;
10688 False_Result := Res in Compare_GE;
10691 True_Result := Res in Compare_LE;
10692 False_Result := Res = GT;
10695 and then Constant_Condition_Warnings
10696 and then Comes_From_Source (Original_Node (N))
10697 and then Nkind (Original_Node (N)) = N_Op_Le
10698 and then not In_Instance
10699 and then Is_Integer_Type (Etype (Left_Opnd (N)))
10700 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
10703 ("can never be less than, could replace by ""'=""?", N);
10704 Warning_Generated := True;
10708 True_Result := Res = NE or else Res = GT or else Res = LT;
10709 False_Result := Res = EQ;
10712 -- If this is the first iteration, then we actually convert the
10713 -- comparison into True or False, if the result is certain.
10716 if True_Result or False_Result then
10717 if True_Result then
10718 Result := Standard_True;
10720 Result := Standard_False;
10725 New_Occurrence_Of (Result, Sloc (N))));
10726 Analyze_And_Resolve (N, Typ);
10727 Warn_On_Known_Condition (N);
10731 -- If this is the second iteration (AV = True), and the original
10732 -- node comes from source and we are not in an instance, then give
10733 -- a warning if we know result would be True or False. Note: we
10734 -- know Constant_Condition_Warnings is set if we get here.
10736 elsif Comes_From_Source (Original_Node (N))
10737 and then not In_Instance
10739 if True_Result then
10741 ("condition can only be False if invalid values present?",
10743 elsif False_Result then
10745 ("condition can only be True if invalid values present?",
10751 -- Skip second iteration if not warning on constant conditions or
10752 -- if the first iteration already generated a warning of some kind or
10753 -- if we are in any case assuming all values are valid (so that the
10754 -- first iteration took care of the valid case).
10756 exit when not Constant_Condition_Warnings;
10757 exit when Warning_Generated;
10758 exit when Assume_No_Invalid_Values;
10760 end Rewrite_Comparison;
10762 ----------------------------
10763 -- Safe_In_Place_Array_Op --
10764 ----------------------------
10766 function Safe_In_Place_Array_Op
10769 Op2 : Node_Id) return Boolean
10771 Target : Entity_Id;
10773 function Is_Safe_Operand (Op : Node_Id) return Boolean;
10774 -- Operand is safe if it cannot overlap part of the target of the
10775 -- operation. If the operand and the target are identical, the operand
10776 -- is safe. The operand can be empty in the case of negation.
10778 function Is_Unaliased (N : Node_Id) return Boolean;
10779 -- Check that N is a stand-alone entity
10785 function Is_Unaliased (N : Node_Id) return Boolean is
10789 and then No (Address_Clause (Entity (N)))
10790 and then No (Renamed_Object (Entity (N)));
10793 ---------------------
10794 -- Is_Safe_Operand --
10795 ---------------------
10797 function Is_Safe_Operand (Op : Node_Id) return Boolean is
10802 elsif Is_Entity_Name (Op) then
10803 return Is_Unaliased (Op);
10805 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
10806 return Is_Unaliased (Prefix (Op));
10808 elsif Nkind (Op) = N_Slice then
10810 Is_Unaliased (Prefix (Op))
10811 and then Entity (Prefix (Op)) /= Target;
10813 elsif Nkind (Op) = N_Op_Not then
10814 return Is_Safe_Operand (Right_Opnd (Op));
10819 end Is_Safe_Operand;
10821 -- Start of processing for Is_Safe_In_Place_Array_Op
10824 -- Skip this processing if the component size is different from system
10825 -- storage unit (since at least for NOT this would cause problems).
10827 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
10830 -- Cannot do in place stuff on VM_Target since cannot pass addresses
10832 elsif VM_Target /= No_VM then
10835 -- Cannot do in place stuff if non-standard Boolean representation
10837 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
10840 elsif not Is_Unaliased (Lhs) then
10844 Target := Entity (Lhs);
10845 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
10847 end Safe_In_Place_Array_Op;
10849 -----------------------
10850 -- Tagged_Membership --
10851 -----------------------
10853 -- There are two different cases to consider depending on whether the right
10854 -- operand is a class-wide type or not. If not we just compare the actual
10855 -- tag of the left expr to the target type tag:
10857 -- Left_Expr.Tag = Right_Type'Tag;
10859 -- If it is a class-wide type we use the RT function CW_Membership which is
10860 -- usually implemented by looking in the ancestor tables contained in the
10861 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
10863 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
10864 -- function IW_Membership which is usually implemented by looking in the
10865 -- table of abstract interface types plus the ancestor table contained in
10866 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
10868 procedure Tagged_Membership
10870 SCIL_Node : out Node_Id;
10871 Result : out Node_Id)
10873 Left : constant Node_Id := Left_Opnd (N);
10874 Right : constant Node_Id := Right_Opnd (N);
10875 Loc : constant Source_Ptr := Sloc (N);
10877 Full_R_Typ : Entity_Id;
10878 Left_Type : Entity_Id;
10879 New_Node : Node_Id;
10880 Right_Type : Entity_Id;
10884 SCIL_Node := Empty;
10886 -- Handle entities from the limited view
10888 Left_Type := Available_View (Etype (Left));
10889 Right_Type := Available_View (Etype (Right));
10891 if Is_Class_Wide_Type (Left_Type) then
10892 Left_Type := Root_Type (Left_Type);
10895 if Is_Class_Wide_Type (Right_Type) then
10896 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
10898 Full_R_Typ := Underlying_Type (Right_Type);
10902 Make_Selected_Component (Loc,
10903 Prefix => Relocate_Node (Left),
10905 New_Reference_To (First_Tag_Component (Left_Type), Loc));
10907 if Is_Class_Wide_Type (Right_Type) then
10909 -- No need to issue a run-time check if we statically know that the
10910 -- result of this membership test is always true. For example,
10911 -- considering the following declarations:
10913 -- type Iface is interface;
10914 -- type T is tagged null record;
10915 -- type DT is new T and Iface with null record;
10920 -- These membership tests are always true:
10923 -- Obj2 in T'Class;
10924 -- Obj2 in Iface'Class;
10926 -- We do not need to handle cases where the membership is illegal.
10929 -- Obj1 in DT'Class; -- Compile time error
10930 -- Obj1 in Iface'Class; -- Compile time error
10932 if not Is_Class_Wide_Type (Left_Type)
10933 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
10934 Use_Full_View => True)
10935 or else (Is_Interface (Etype (Right_Type))
10936 and then Interface_Present_In_Ancestor
10938 Iface => Etype (Right_Type))))
10940 Result := New_Reference_To (Standard_True, Loc);
10944 -- Ada 2005 (AI-251): Class-wide applied to interfaces
10946 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
10948 -- Support to: "Iface_CW_Typ in Typ'Class"
10950 or else Is_Interface (Left_Type)
10952 -- Issue error if IW_Membership operation not available in a
10953 -- configurable run time setting.
10955 if not RTE_Available (RE_IW_Membership) then
10957 ("dynamic membership test on interface types", N);
10963 Make_Function_Call (Loc,
10964 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
10965 Parameter_Associations => New_List (
10966 Make_Attribute_Reference (Loc,
10968 Attribute_Name => Name_Address),
10970 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
10973 -- Ada 95: Normal case
10976 Build_CW_Membership (Loc,
10977 Obj_Tag_Node => Obj_Tag,
10980 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc),
10982 New_Node => New_Node);
10984 -- Generate the SCIL node for this class-wide membership test.
10985 -- Done here because the previous call to Build_CW_Membership
10986 -- relocates Obj_Tag.
10988 if Generate_SCIL then
10989 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
10990 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
10991 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
10994 Result := New_Node;
10997 -- Right_Type is not a class-wide type
11000 -- No need to check the tag of the object if Right_Typ is abstract
11002 if Is_Abstract_Type (Right_Type) then
11003 Result := New_Reference_To (Standard_False, Loc);
11008 Left_Opnd => Obj_Tag,
11011 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
11014 end Tagged_Membership;
11016 ------------------------------
11017 -- Unary_Op_Validity_Checks --
11018 ------------------------------
11020 procedure Unary_Op_Validity_Checks (N : Node_Id) is
11022 if Validity_Checks_On and Validity_Check_Operands then
11023 Ensure_Valid (Right_Opnd (N));
11025 end Unary_Op_Validity_Checks;