1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Aggr; use Exp_Aggr;
33 with Exp_Atag; use Exp_Atag;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Fixd; use Exp_Fixd;
40 with Exp_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;
47 with Namet; use Namet;
48 with Nlists; use Nlists;
49 with Nmake; use Nmake;
51 with Par_SCO; use Par_SCO;
52 with Restrict; use Restrict;
53 with Rident; use Rident;
54 with Rtsfind; use Rtsfind;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch3; use Sem_Ch3;
59 with Sem_Ch8; use Sem_Ch8;
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 Displace_Allocator_Pointer (N : Node_Id);
95 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
96 -- Expand_Allocator_Expression. Allocating class-wide interface objects
97 -- this routine displaces the pointer to the allocated object to reference
98 -- the component referencing the corresponding secondary dispatch table.
100 procedure Expand_Allocator_Expression (N : Node_Id);
101 -- Subsidiary to Expand_N_Allocator, for the case when the expression
102 -- is a qualified expression or an aggregate.
104 procedure Expand_Array_Comparison (N : Node_Id);
105 -- This routine handles expansion of the comparison operators (N_Op_Lt,
106 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
107 -- code for these operators is similar, differing only in the details of
108 -- the actual comparison call that is made. Special processing (call a
111 function Expand_Array_Equality
116 Typ : Entity_Id) return Node_Id;
117 -- Expand an array equality into a call to a function implementing this
118 -- equality, and a call to it. Loc is the location for the generated nodes.
119 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
120 -- on which to attach bodies of local functions that are created in the
121 -- process. It is the responsibility of the caller to insert those bodies
122 -- at the right place. Nod provides the Sloc value for the generated code.
123 -- Normally the types used for the generated equality routine are taken
124 -- from Lhs and Rhs. However, in some situations of generated code, the
125 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
126 -- the type to be used for the formal parameters.
128 procedure Expand_Boolean_Operator (N : Node_Id);
129 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
130 -- case of array type arguments.
132 procedure Expand_Short_Circuit_Operator (N : Node_Id);
133 -- Common expansion processing for short-circuit boolean operators
135 function Expand_Composite_Equality
140 Bodies : List_Id) return Node_Id;
141 -- Local recursive function used to expand equality for nested composite
142 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
143 -- to attach bodies of local functions that are created in the process.
144 -- This is the responsibility of the caller to insert those bodies at the
145 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
146 -- are the left and right sides for the comparison, and Typ is the type of
147 -- the arrays to compare.
149 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
150 -- Routine to expand concatenation of a sequence of two or more operands
151 -- (in the list Operands) and replace node Cnode with the result of the
152 -- concatenation. The operands can be of any appropriate type, and can
153 -- include both arrays and singleton elements.
155 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
156 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
157 -- fixed. We do not have such a type at runtime, so the purpose of this
158 -- routine is to find the real type by looking up the tree. We also
159 -- determine if the operation must be rounded.
161 function Get_Allocator_Final_List
164 PtrT : Entity_Id) return Entity_Id;
165 -- If the designated type is controlled, build final_list expression for
166 -- created object. If context is an access parameter, create a local access
167 -- type to have a usable finalization list.
169 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
170 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
171 -- discriminants if it has a constrained nominal type, unless the object
172 -- is a component of an enclosing Unchecked_Union object that is subject
173 -- to a per-object constraint and the enclosing object lacks inferable
176 -- An expression of an Unchecked_Union type has inferable discriminants
177 -- if it is either a name of an object with inferable discriminants or a
178 -- qualified expression whose subtype mark denotes a constrained subtype.
180 procedure Insert_Dereference_Action (N : Node_Id);
181 -- N is an expression whose type is an access. When the type of the
182 -- associated storage pool is derived from Checked_Pool, generate a
183 -- call to the 'Dereference' primitive operation.
185 function Make_Array_Comparison_Op
187 Nod : Node_Id) return Node_Id;
188 -- Comparisons between arrays are expanded in line. This function produces
189 -- the body of the implementation of (a > b), where a and b are one-
190 -- dimensional arrays of some discrete type. The original node is then
191 -- expanded into the appropriate call to this function. Nod provides the
192 -- Sloc value for the generated code.
194 function Make_Boolean_Array_Op
196 N : Node_Id) return Node_Id;
197 -- Boolean operations on boolean arrays are expanded in line. This function
198 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
199 -- b). It is used only the normal case and not the packed case. The type
200 -- involved, Typ, is the Boolean array type, and the logical operations in
201 -- the body are simple boolean operations. Note that Typ is always a
202 -- constrained type (the caller has ensured this by using
203 -- Convert_To_Actual_Subtype if necessary).
205 procedure Rewrite_Comparison (N : Node_Id);
206 -- If N is the node for a comparison whose outcome can be determined at
207 -- compile time, then the node N can be rewritten with True or False. If
208 -- the outcome cannot be determined at compile time, the call has no
209 -- effect. If N is a type conversion, then this processing is applied to
210 -- its expression. If N is neither comparison nor a type conversion, the
211 -- call has no effect.
213 procedure Tagged_Membership
215 SCIL_Node : out Node_Id;
216 Result : out Node_Id);
217 -- Construct the expression corresponding to the tagged membership test.
218 -- Deals with a second operand being (or not) a class-wide type.
220 function Safe_In_Place_Array_Op
223 Op2 : Node_Id) return Boolean;
224 -- In the context of an assignment, where the right-hand side is a boolean
225 -- operation on arrays, check whether operation can be performed in place.
227 procedure Unary_Op_Validity_Checks (N : Node_Id);
228 pragma Inline (Unary_Op_Validity_Checks);
229 -- Performs validity checks for a unary operator
231 -------------------------------
232 -- Binary_Op_Validity_Checks --
233 -------------------------------
235 procedure Binary_Op_Validity_Checks (N : Node_Id) is
237 if Validity_Checks_On and Validity_Check_Operands then
238 Ensure_Valid (Left_Opnd (N));
239 Ensure_Valid (Right_Opnd (N));
241 end Binary_Op_Validity_Checks;
243 ------------------------------------
244 -- Build_Boolean_Array_Proc_Call --
245 ------------------------------------
247 procedure Build_Boolean_Array_Proc_Call
252 Loc : constant Source_Ptr := Sloc (N);
253 Kind : constant Node_Kind := Nkind (Expression (N));
254 Target : constant Node_Id :=
255 Make_Attribute_Reference (Loc,
257 Attribute_Name => Name_Address);
259 Arg1 : Node_Id := Op1;
260 Arg2 : Node_Id := Op2;
262 Proc_Name : Entity_Id;
265 if Kind = N_Op_Not then
266 if Nkind (Op1) in N_Binary_Op then
268 -- Use negated version of the binary operators
270 if Nkind (Op1) = N_Op_And then
271 Proc_Name := RTE (RE_Vector_Nand);
273 elsif Nkind (Op1) = N_Op_Or then
274 Proc_Name := RTE (RE_Vector_Nor);
276 else pragma Assert (Nkind (Op1) = N_Op_Xor);
277 Proc_Name := RTE (RE_Vector_Xor);
281 Make_Procedure_Call_Statement (Loc,
282 Name => New_Occurrence_Of (Proc_Name, Loc),
284 Parameter_Associations => New_List (
286 Make_Attribute_Reference (Loc,
287 Prefix => Left_Opnd (Op1),
288 Attribute_Name => Name_Address),
290 Make_Attribute_Reference (Loc,
291 Prefix => Right_Opnd (Op1),
292 Attribute_Name => Name_Address),
294 Make_Attribute_Reference (Loc,
295 Prefix => Left_Opnd (Op1),
296 Attribute_Name => Name_Length)));
299 Proc_Name := RTE (RE_Vector_Not);
302 Make_Procedure_Call_Statement (Loc,
303 Name => New_Occurrence_Of (Proc_Name, Loc),
304 Parameter_Associations => New_List (
307 Make_Attribute_Reference (Loc,
309 Attribute_Name => Name_Address),
311 Make_Attribute_Reference (Loc,
313 Attribute_Name => Name_Length)));
317 -- We use the following equivalences:
319 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
320 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
321 -- (not X) xor (not Y) = X xor Y
322 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
324 if Nkind (Op1) = N_Op_Not then
325 Arg1 := Right_Opnd (Op1);
326 Arg2 := Right_Opnd (Op2);
327 if Kind = N_Op_And then
328 Proc_Name := RTE (RE_Vector_Nor);
329 elsif Kind = N_Op_Or then
330 Proc_Name := RTE (RE_Vector_Nand);
332 Proc_Name := RTE (RE_Vector_Xor);
336 if Kind = N_Op_And then
337 Proc_Name := RTE (RE_Vector_And);
338 elsif Kind = N_Op_Or then
339 Proc_Name := RTE (RE_Vector_Or);
340 elsif Nkind (Op2) = N_Op_Not then
341 Proc_Name := RTE (RE_Vector_Nxor);
342 Arg2 := Right_Opnd (Op2);
344 Proc_Name := RTE (RE_Vector_Xor);
349 Make_Procedure_Call_Statement (Loc,
350 Name => New_Occurrence_Of (Proc_Name, Loc),
351 Parameter_Associations => New_List (
353 Make_Attribute_Reference (Loc,
355 Attribute_Name => Name_Address),
356 Make_Attribute_Reference (Loc,
358 Attribute_Name => Name_Address),
359 Make_Attribute_Reference (Loc,
361 Attribute_Name => Name_Length)));
364 Rewrite (N, Call_Node);
368 when RE_Not_Available =>
370 end Build_Boolean_Array_Proc_Call;
372 --------------------------------
373 -- Displace_Allocator_Pointer --
374 --------------------------------
376 procedure Displace_Allocator_Pointer (N : Node_Id) is
377 Loc : constant Source_Ptr := Sloc (N);
378 Orig_Node : constant Node_Id := Original_Node (N);
384 -- Do nothing in case of VM targets: the virtual machine will handle
385 -- interfaces directly.
387 if not Tagged_Type_Expansion then
391 pragma Assert (Nkind (N) = N_Identifier
392 and then Nkind (Orig_Node) = N_Allocator);
394 PtrT := Etype (Orig_Node);
395 Dtyp := Available_View (Designated_Type (PtrT));
396 Etyp := Etype (Expression (Orig_Node));
398 if Is_Class_Wide_Type (Dtyp)
399 and then Is_Interface (Dtyp)
401 -- If the type of the allocator expression is not an interface type
402 -- we can generate code to reference the record component containing
403 -- the pointer to the secondary dispatch table.
405 if not Is_Interface (Etyp) then
407 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
410 -- 1) Get access to the allocated object
413 Make_Explicit_Dereference (Loc,
418 -- 2) Add the conversion to displace the pointer to reference
419 -- the secondary dispatch table.
421 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
422 Analyze_And_Resolve (N, Dtyp);
424 -- 3) The 'access to the secondary dispatch table will be used
425 -- as the value returned by the allocator.
428 Make_Attribute_Reference (Loc,
429 Prefix => Relocate_Node (N),
430 Attribute_Name => Name_Access));
431 Set_Etype (N, Saved_Typ);
435 -- If the type of the allocator expression is an interface type we
436 -- generate a run-time call to displace "this" to reference the
437 -- component containing the pointer to the secondary dispatch table
438 -- or else raise Constraint_Error if the actual object does not
439 -- implement the target interface. This case corresponds with the
440 -- following example:
442 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
444 -- return new Iface_2'Class'(Obj);
449 Unchecked_Convert_To (PtrT,
450 Make_Function_Call (Loc,
451 Name => New_Reference_To (RTE (RE_Displace), Loc),
452 Parameter_Associations => New_List (
453 Unchecked_Convert_To (RTE (RE_Address),
459 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
461 Analyze_And_Resolve (N, PtrT);
464 end Displace_Allocator_Pointer;
466 ---------------------------------
467 -- Expand_Allocator_Expression --
468 ---------------------------------
470 procedure Expand_Allocator_Expression (N : Node_Id) is
471 Loc : constant Source_Ptr := Sloc (N);
472 Exp : constant Node_Id := Expression (Expression (N));
473 PtrT : constant Entity_Id := Etype (N);
474 DesigT : constant Entity_Id := Designated_Type (PtrT);
476 procedure Apply_Accessibility_Check
478 Built_In_Place : Boolean := False);
479 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
480 -- type, generate an accessibility check to verify that the level of the
481 -- type of the created object is not deeper than the level of the access
482 -- type. If the type of the qualified expression is class- wide, then
483 -- always generate the check (except in the case where it is known to be
484 -- unnecessary, see comment below). Otherwise, only generate the check
485 -- if the level of the qualified expression type is statically deeper
486 -- than the access type.
488 -- Although the static accessibility will generally have been performed
489 -- as a legality check, it won't have been done in cases where the
490 -- allocator appears in generic body, so a run-time check is needed in
491 -- general. One special case is when the access type is declared in the
492 -- same scope as the class-wide allocator, in which case the check can
493 -- never fail, so it need not be generated.
495 -- As an open issue, there seem to be cases where the static level
496 -- associated with the class-wide object's underlying type is not
497 -- sufficient to perform the proper accessibility check, such as for
498 -- allocators in nested subprograms or accept statements initialized by
499 -- class-wide formals when the actual originates outside at a deeper
500 -- static level. The nested subprogram case might require passing
501 -- accessibility levels along with class-wide parameters, and the task
502 -- case seems to be an actual gap in the language rules that needs to
503 -- be fixed by the ARG. ???
505 -------------------------------
506 -- Apply_Accessibility_Check --
507 -------------------------------
509 procedure Apply_Accessibility_Check
511 Built_In_Place : Boolean := False)
516 -- Note: we skip the accessibility check for the VM case, since
517 -- there does not seem to be any practical way of implementing it.
519 if Ada_Version >= Ada_2005
520 and then Tagged_Type_Expansion
521 and then Is_Class_Wide_Type (DesigT)
522 and then not Scope_Suppress (Accessibility_Check)
524 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
526 (Is_Class_Wide_Type (Etype (Exp))
527 and then Scope (PtrT) /= Current_Scope))
529 -- If the allocator was built in place Ref is already a reference
530 -- to the access object initialized to the result of the allocator
531 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise
532 -- it is the entity associated with the object containing the
533 -- address of the allocated object.
535 if Built_In_Place then
536 Ref_Node := New_Copy (Ref);
538 Ref_Node := New_Reference_To (Ref, Loc);
542 Make_Raise_Program_Error (Loc,
546 Build_Get_Access_Level (Loc,
547 Make_Attribute_Reference (Loc,
549 Attribute_Name => Name_Tag)),
551 Make_Integer_Literal (Loc,
552 Type_Access_Level (PtrT))),
553 Reason => PE_Accessibility_Check_Failed));
555 end Apply_Accessibility_Check;
559 Indic : constant Node_Id := Subtype_Mark (Expression (N));
560 T : constant Entity_Id := Entity (Indic);
565 TagT : Entity_Id := Empty;
566 -- Type used as source for tag assignment
568 TagR : Node_Id := Empty;
569 -- Target reference for tag assignment
571 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
573 Tag_Assign : Node_Id;
576 -- Start of processing for Expand_Allocator_Expression
579 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
581 if Is_CPP_Constructor_Call (Exp) then
584 -- Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn
586 -- Allocate the object with no expression
588 Node := Relocate_Node (N);
589 Set_Expression (Node, New_Reference_To (Etype (Exp), Loc));
591 -- Avoid its expansion to avoid generating a call to the default
596 Temp := Make_Temporary (Loc, 'P', N);
599 Make_Object_Declaration (Loc,
600 Defining_Identifier => Temp,
601 Constant_Present => True,
602 Object_Definition => New_Reference_To (PtrT, Loc),
603 Expression => Node));
605 Apply_Accessibility_Check (Temp);
607 -- Locate the enclosing list and insert the C++ constructor call
614 while not Is_List_Member (P) loop
618 Insert_List_After_And_Analyze (P,
619 Build_Initialization_Call (Loc,
621 Make_Explicit_Dereference (Loc,
622 Prefix => New_Reference_To (Temp, Loc)),
624 Constructor_Ref => Exp));
627 Rewrite (N, New_Reference_To (Temp, Loc));
628 Analyze_And_Resolve (N, PtrT);
632 -- Ada 2005 (AI-318-02): If the initialization expression is a call
633 -- to a build-in-place function, then access to the allocated object
634 -- must be passed to the function. Currently we limit such functions
635 -- to those with constrained limited result subtypes, but eventually
636 -- we plan to expand the allowed forms of functions that are treated
637 -- as build-in-place.
639 if Ada_Version >= Ada_2005
640 and then Is_Build_In_Place_Function_Call (Exp)
642 Make_Build_In_Place_Call_In_Allocator (N, Exp);
643 Apply_Accessibility_Check (N, Built_In_Place => True);
647 -- Actions inserted before:
648 -- Temp : constant ptr_T := new T'(Expression);
649 -- <no CW> Temp._tag := T'tag;
650 -- <CTRL> Adjust (Finalizable (Temp.all));
651 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
653 -- We analyze by hand the new internal allocator to avoid
654 -- any recursion and inappropriate call to Initialize
656 -- We don't want to remove side effects when the expression must be
657 -- built in place. In the case of a build-in-place function call,
658 -- that could lead to a duplication of the call, which was already
659 -- substituted for the allocator.
661 if not Aggr_In_Place then
662 Remove_Side_Effects (Exp);
665 Temp := Make_Temporary (Loc, 'P', N);
667 -- For a class wide allocation generate the following code:
669 -- type Equiv_Record is record ... end record;
670 -- implicit subtype CW is <Class_Wide_Subytpe>;
671 -- temp : PtrT := new CW'(CW!(expr));
673 if Is_Class_Wide_Type (T) then
674 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
676 -- Ada 2005 (AI-251): If the expression is a class-wide interface
677 -- object we generate code to move up "this" to reference the
678 -- base of the object before allocating the new object.
680 -- Note that Exp'Address is recursively expanded into a call
681 -- to Base_Address (Exp.Tag)
683 if Is_Class_Wide_Type (Etype (Exp))
684 and then Is_Interface (Etype (Exp))
685 and then Tagged_Type_Expansion
689 Unchecked_Convert_To (Entity (Indic),
690 Make_Explicit_Dereference (Loc,
691 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
692 Make_Attribute_Reference (Loc,
694 Attribute_Name => Name_Address)))));
699 Unchecked_Convert_To (Entity (Indic), Exp));
702 Analyze_And_Resolve (Expression (N), Entity (Indic));
705 -- Keep separate the management of allocators returning interfaces
707 if not Is_Interface (Directly_Designated_Type (PtrT)) then
708 if Aggr_In_Place then
710 Make_Object_Declaration (Loc,
711 Defining_Identifier => Temp,
712 Object_Definition => New_Reference_To (PtrT, Loc),
715 New_Reference_To (Etype (Exp), Loc)));
717 -- Copy the Comes_From_Source flag for the allocator we just
718 -- built, since logically this allocator is a replacement of
719 -- the original allocator node. This is for proper handling of
720 -- restriction No_Implicit_Heap_Allocations.
722 Set_Comes_From_Source
723 (Expression (Tmp_Node), Comes_From_Source (N));
725 Set_No_Initialization (Expression (Tmp_Node));
726 Insert_Action (N, Tmp_Node);
728 if Needs_Finalization (T)
729 and then Ekind (PtrT) = E_Anonymous_Access_Type
731 -- Create local finalization list for access parameter
733 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
736 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
739 Node := Relocate_Node (N);
742 Make_Object_Declaration (Loc,
743 Defining_Identifier => Temp,
744 Constant_Present => True,
745 Object_Definition => New_Reference_To (PtrT, Loc),
746 Expression => Node));
749 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
750 -- interface type. In this case we use the type of the qualified
751 -- expression to allocate the object.
755 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
760 Make_Full_Type_Declaration (Loc,
761 Defining_Identifier => Def_Id,
763 Make_Access_To_Object_Definition (Loc,
765 Null_Exclusion_Present => False,
766 Constant_Present => False,
767 Subtype_Indication =>
768 New_Reference_To (Etype (Exp), Loc)));
770 Insert_Action (N, New_Decl);
772 -- Inherit the final chain to ensure that the expansion of the
773 -- aggregate is correct in case of controlled types
775 if Needs_Finalization (Directly_Designated_Type (PtrT)) then
776 Set_Associated_Final_Chain (Def_Id,
777 Associated_Final_Chain (PtrT));
780 -- Declare the object using the previous type declaration
782 if Aggr_In_Place then
784 Make_Object_Declaration (Loc,
785 Defining_Identifier => Temp,
786 Object_Definition => New_Reference_To (Def_Id, Loc),
789 New_Reference_To (Etype (Exp), Loc)));
791 -- Copy the Comes_From_Source flag for the allocator we just
792 -- built, since logically this allocator is a replacement of
793 -- the original allocator node. This is for proper handling
794 -- of restriction No_Implicit_Heap_Allocations.
796 Set_Comes_From_Source
797 (Expression (Tmp_Node), Comes_From_Source (N));
799 Set_No_Initialization (Expression (Tmp_Node));
800 Insert_Action (N, Tmp_Node);
802 if Needs_Finalization (T)
803 and then Ekind (PtrT) = E_Anonymous_Access_Type
805 -- Create local finalization list for access parameter
808 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
811 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
813 Node := Relocate_Node (N);
816 Make_Object_Declaration (Loc,
817 Defining_Identifier => Temp,
818 Constant_Present => True,
819 Object_Definition => New_Reference_To (Def_Id, Loc),
820 Expression => Node));
823 -- Generate an additional object containing the address of the
824 -- returned object. The type of this second object declaration
825 -- is the correct type required for the common processing that
826 -- is still performed by this subprogram. The displacement of
827 -- this pointer to reference the component associated with the
828 -- interface type will be done at the end of common processing.
831 Make_Object_Declaration (Loc,
832 Defining_Identifier => Make_Temporary (Loc, 'P'),
833 Object_Definition => New_Reference_To (PtrT, Loc),
834 Expression => Unchecked_Convert_To (PtrT,
835 New_Reference_To (Temp, Loc)));
837 Insert_Action (N, New_Decl);
839 Tmp_Node := New_Decl;
840 Temp := Defining_Identifier (New_Decl);
844 Apply_Accessibility_Check (Temp);
846 -- Generate the tag assignment
848 -- Suppress the tag assignment when VM_Target because VM tags are
849 -- represented implicitly in objects.
851 if not Tagged_Type_Expansion then
854 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
855 -- interface objects because in this case the tag does not change.
857 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
858 pragma Assert (Is_Class_Wide_Type
859 (Directly_Designated_Type (Etype (N))));
862 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
864 TagR := New_Reference_To (Temp, Loc);
866 elsif Is_Private_Type (T)
867 and then Is_Tagged_Type (Underlying_Type (T))
869 TagT := Underlying_Type (T);
871 Unchecked_Convert_To (Underlying_Type (T),
872 Make_Explicit_Dereference (Loc,
873 Prefix => New_Reference_To (Temp, Loc)));
876 if Present (TagT) then
878 Make_Assignment_Statement (Loc,
880 Make_Selected_Component (Loc,
883 New_Reference_To (First_Tag_Component (TagT), Loc)),
886 Unchecked_Convert_To (RTE (RE_Tag),
888 (Elists.Node (First_Elmt (Access_Disp_Table (TagT))),
891 -- The previous assignment has to be done in any case
893 Set_Assignment_OK (Name (Tag_Assign));
894 Insert_Action (N, Tag_Assign);
897 if Needs_Finalization (DesigT)
898 and then Needs_Finalization (T)
902 Apool : constant Entity_Id :=
903 Associated_Storage_Pool (PtrT);
906 -- If it is an allocation on the secondary stack (i.e. a value
907 -- returned from a function), the object is attached on the
908 -- caller side as soon as the call is completed (see
909 -- Expand_Ctrl_Function_Call)
911 if Is_RTE (Apool, RE_SS_Pool) then
913 F : constant Entity_Id := Make_Temporary (Loc, 'F');
916 Make_Object_Declaration (Loc,
917 Defining_Identifier => F,
919 New_Reference_To (RTE (RE_Finalizable_Ptr), Loc)));
920 Flist := New_Reference_To (F, Loc);
921 Attach := Make_Integer_Literal (Loc, 1);
924 -- Normal case, not a secondary stack allocation
927 if Needs_Finalization (T)
928 and then Ekind (PtrT) = E_Anonymous_Access_Type
930 -- Create local finalization list for access parameter
933 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
935 Flist := Find_Final_List (PtrT);
938 Attach := Make_Integer_Literal (Loc, 2);
941 -- Generate an Adjust call if the object will be moved. In Ada
942 -- 2005, the object may be inherently limited, in which case
943 -- there is no Adjust procedure, and the object is built in
944 -- place. In Ada 95, the object can be limited but not
945 -- inherently limited if this allocator came from a return
946 -- statement (we're allocating the result on the secondary
947 -- stack). In that case, the object will be moved, so we _do_
951 and then not Is_Immutably_Limited_Type (T)
957 -- An unchecked conversion is needed in the classwide
958 -- case because the designated type can be an ancestor of
959 -- the subtype mark of the allocator.
961 Unchecked_Convert_To (T,
962 Make_Explicit_Dereference (Loc,
963 Prefix => New_Reference_To (Temp, Loc))),
967 With_Attach => Attach,
973 Rewrite (N, New_Reference_To (Temp, Loc));
974 Analyze_And_Resolve (N, PtrT);
976 -- Ada 2005 (AI-251): Displace the pointer to reference the record
977 -- component containing the secondary dispatch table of the interface
980 if Is_Interface (Directly_Designated_Type (PtrT)) then
981 Displace_Allocator_Pointer (N);
984 elsif Aggr_In_Place then
985 Temp := Make_Temporary (Loc, 'P', N);
987 Make_Object_Declaration (Loc,
988 Defining_Identifier => Temp,
989 Object_Definition => New_Reference_To (PtrT, Loc),
990 Expression => Make_Allocator (Loc,
991 New_Reference_To (Etype (Exp), Loc)));
993 -- Copy the Comes_From_Source flag for the allocator we just built,
994 -- since logically this allocator is a replacement of the original
995 -- allocator node. This is for proper handling of restriction
996 -- No_Implicit_Heap_Allocations.
998 Set_Comes_From_Source
999 (Expression (Tmp_Node), Comes_From_Source (N));
1001 Set_No_Initialization (Expression (Tmp_Node));
1002 Insert_Action (N, Tmp_Node);
1003 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
1004 Rewrite (N, New_Reference_To (Temp, Loc));
1005 Analyze_And_Resolve (N, PtrT);
1007 elsif Is_Access_Type (T)
1008 and then Can_Never_Be_Null (T)
1010 Install_Null_Excluding_Check (Exp);
1012 elsif Is_Access_Type (DesigT)
1013 and then Nkind (Exp) = N_Allocator
1014 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1016 -- Apply constraint to designated subtype indication
1018 Apply_Constraint_Check (Expression (Exp),
1019 Designated_Type (DesigT),
1020 No_Sliding => True);
1022 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1024 -- Propagate constraint_error to enclosing allocator
1026 Rewrite (Exp, New_Copy (Expression (Exp)));
1030 -- type A is access T1;
1031 -- X : A := new T2'(...);
1032 -- T1 and T2 can be different subtypes, and we might need to check
1033 -- both constraints. First check against the type of the qualified
1036 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1038 if Do_Range_Check (Exp) then
1039 Set_Do_Range_Check (Exp, False);
1040 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1043 -- A check is also needed in cases where the designated subtype is
1044 -- constrained and differs from the subtype given in the qualified
1045 -- expression. Note that the check on the qualified expression does
1046 -- not allow sliding, but this check does (a relaxation from Ada 83).
1048 if Is_Constrained (DesigT)
1049 and then not Subtypes_Statically_Match (T, DesigT)
1051 Apply_Constraint_Check
1052 (Exp, DesigT, No_Sliding => False);
1054 if Do_Range_Check (Exp) then
1055 Set_Do_Range_Check (Exp, False);
1056 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1060 -- For an access to unconstrained packed array, GIGI needs to see an
1061 -- expression with a constrained subtype in order to compute the
1062 -- proper size for the allocator.
1064 if Is_Array_Type (T)
1065 and then not Is_Constrained (T)
1066 and then Is_Packed (T)
1069 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1070 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1073 Make_Subtype_Declaration (Loc,
1074 Defining_Identifier => ConstrT,
1075 Subtype_Indication =>
1076 Make_Subtype_From_Expr (Exp, T)));
1077 Freeze_Itype (ConstrT, Exp);
1078 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1082 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1083 -- to a build-in-place function, then access to the allocated object
1084 -- must be passed to the function. Currently we limit such functions
1085 -- to those with constrained limited result subtypes, but eventually
1086 -- we plan to expand the allowed forms of functions that are treated
1087 -- as build-in-place.
1089 if Ada_Version >= Ada_2005
1090 and then Is_Build_In_Place_Function_Call (Exp)
1092 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1097 when RE_Not_Available =>
1099 end Expand_Allocator_Expression;
1101 -----------------------------
1102 -- Expand_Array_Comparison --
1103 -----------------------------
1105 -- Expansion is only required in the case of array types. For the unpacked
1106 -- case, an appropriate runtime routine is called. For packed cases, and
1107 -- also in some other cases where a runtime routine cannot be called, the
1108 -- form of the expansion is:
1110 -- [body for greater_nn; boolean_expression]
1112 -- The body is built by Make_Array_Comparison_Op, and the form of the
1113 -- Boolean expression depends on the operator involved.
1115 procedure Expand_Array_Comparison (N : Node_Id) is
1116 Loc : constant Source_Ptr := Sloc (N);
1117 Op1 : Node_Id := Left_Opnd (N);
1118 Op2 : Node_Id := Right_Opnd (N);
1119 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1120 Ctyp : constant Entity_Id := Component_Type (Typ1);
1123 Func_Body : Node_Id;
1124 Func_Name : Entity_Id;
1128 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1129 -- True for byte addressable target
1131 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1132 -- Returns True if the length of the given operand is known to be less
1133 -- than 4. Returns False if this length is known to be four or greater
1134 -- or is not known at compile time.
1136 ------------------------
1137 -- Length_Less_Than_4 --
1138 ------------------------
1140 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1141 Otyp : constant Entity_Id := Etype (Opnd);
1144 if Ekind (Otyp) = E_String_Literal_Subtype then
1145 return String_Literal_Length (Otyp) < 4;
1149 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1150 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1151 Hi : constant Node_Id := Type_High_Bound (Ityp);
1156 if Compile_Time_Known_Value (Lo) then
1157 Lov := Expr_Value (Lo);
1162 if Compile_Time_Known_Value (Hi) then
1163 Hiv := Expr_Value (Hi);
1168 return Hiv < Lov + 3;
1171 end Length_Less_Than_4;
1173 -- Start of processing for Expand_Array_Comparison
1176 -- Deal first with unpacked case, where we can call a runtime routine
1177 -- except that we avoid this for targets for which are not addressable
1178 -- by bytes, and for the JVM/CIL, since they do not support direct
1179 -- addressing of array components.
1181 if not Is_Bit_Packed_Array (Typ1)
1182 and then Byte_Addressable
1183 and then VM_Target = No_VM
1185 -- The call we generate is:
1187 -- Compare_Array_xn[_Unaligned]
1188 -- (left'address, right'address, left'length, right'length) <op> 0
1190 -- x = U for unsigned, S for signed
1191 -- n = 8,16,32,64 for component size
1192 -- Add _Unaligned if length < 4 and component size is 8.
1193 -- <op> is the standard comparison operator
1195 if Component_Size (Typ1) = 8 then
1196 if Length_Less_Than_4 (Op1)
1198 Length_Less_Than_4 (Op2)
1200 if Is_Unsigned_Type (Ctyp) then
1201 Comp := RE_Compare_Array_U8_Unaligned;
1203 Comp := RE_Compare_Array_S8_Unaligned;
1207 if Is_Unsigned_Type (Ctyp) then
1208 Comp := RE_Compare_Array_U8;
1210 Comp := RE_Compare_Array_S8;
1214 elsif Component_Size (Typ1) = 16 then
1215 if Is_Unsigned_Type (Ctyp) then
1216 Comp := RE_Compare_Array_U16;
1218 Comp := RE_Compare_Array_S16;
1221 elsif Component_Size (Typ1) = 32 then
1222 if Is_Unsigned_Type (Ctyp) then
1223 Comp := RE_Compare_Array_U32;
1225 Comp := RE_Compare_Array_S32;
1228 else pragma Assert (Component_Size (Typ1) = 64);
1229 if Is_Unsigned_Type (Ctyp) then
1230 Comp := RE_Compare_Array_U64;
1232 Comp := RE_Compare_Array_S64;
1236 Remove_Side_Effects (Op1, Name_Req => True);
1237 Remove_Side_Effects (Op2, Name_Req => True);
1240 Make_Function_Call (Sloc (Op1),
1241 Name => New_Occurrence_Of (RTE (Comp), Loc),
1243 Parameter_Associations => New_List (
1244 Make_Attribute_Reference (Loc,
1245 Prefix => Relocate_Node (Op1),
1246 Attribute_Name => Name_Address),
1248 Make_Attribute_Reference (Loc,
1249 Prefix => Relocate_Node (Op2),
1250 Attribute_Name => Name_Address),
1252 Make_Attribute_Reference (Loc,
1253 Prefix => Relocate_Node (Op1),
1254 Attribute_Name => Name_Length),
1256 Make_Attribute_Reference (Loc,
1257 Prefix => Relocate_Node (Op2),
1258 Attribute_Name => Name_Length))));
1261 Make_Integer_Literal (Sloc (Op2),
1264 Analyze_And_Resolve (Op1, Standard_Integer);
1265 Analyze_And_Resolve (Op2, Standard_Integer);
1269 -- Cases where we cannot make runtime call
1271 -- For (a <= b) we convert to not (a > b)
1273 if Chars (N) = Name_Op_Le then
1279 Right_Opnd => Op2)));
1280 Analyze_And_Resolve (N, Standard_Boolean);
1283 -- For < the Boolean expression is
1284 -- greater__nn (op2, op1)
1286 elsif Chars (N) = Name_Op_Lt then
1287 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1291 Op1 := Right_Opnd (N);
1292 Op2 := Left_Opnd (N);
1294 -- For (a >= b) we convert to not (a < b)
1296 elsif Chars (N) = Name_Op_Ge then
1302 Right_Opnd => Op2)));
1303 Analyze_And_Resolve (N, Standard_Boolean);
1306 -- For > the Boolean expression is
1307 -- greater__nn (op1, op2)
1310 pragma Assert (Chars (N) = Name_Op_Gt);
1311 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1314 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1316 Make_Function_Call (Loc,
1317 Name => New_Reference_To (Func_Name, Loc),
1318 Parameter_Associations => New_List (Op1, Op2));
1320 Insert_Action (N, Func_Body);
1322 Analyze_And_Resolve (N, Standard_Boolean);
1325 when RE_Not_Available =>
1327 end Expand_Array_Comparison;
1329 ---------------------------
1330 -- Expand_Array_Equality --
1331 ---------------------------
1333 -- Expand an equality function for multi-dimensional arrays. Here is an
1334 -- example of such a function for Nb_Dimension = 2
1336 -- function Enn (A : atyp; B : btyp) return boolean is
1338 -- if (A'length (1) = 0 or else A'length (2) = 0)
1340 -- (B'length (1) = 0 or else B'length (2) = 0)
1342 -- return True; -- RM 4.5.2(22)
1345 -- if A'length (1) /= B'length (1)
1347 -- A'length (2) /= B'length (2)
1349 -- return False; -- RM 4.5.2(23)
1353 -- A1 : Index_T1 := A'first (1);
1354 -- B1 : Index_T1 := B'first (1);
1358 -- A2 : Index_T2 := A'first (2);
1359 -- B2 : Index_T2 := B'first (2);
1362 -- if A (A1, A2) /= B (B1, B2) then
1366 -- exit when A2 = A'last (2);
1367 -- A2 := Index_T2'succ (A2);
1368 -- B2 := Index_T2'succ (B2);
1372 -- exit when A1 = A'last (1);
1373 -- A1 := Index_T1'succ (A1);
1374 -- B1 := Index_T1'succ (B1);
1381 -- Note on the formal types used (atyp and btyp). If either of the arrays
1382 -- is of a private type, we use the underlying type, and do an unchecked
1383 -- conversion of the actual. If either of the arrays has a bound depending
1384 -- on a discriminant, then we use the base type since otherwise we have an
1385 -- escaped discriminant in the function.
1387 -- If both arrays are constrained and have the same bounds, we can generate
1388 -- a loop with an explicit iteration scheme using a 'Range attribute over
1391 function Expand_Array_Equality
1396 Typ : Entity_Id) return Node_Id
1398 Loc : constant Source_Ptr := Sloc (Nod);
1399 Decls : constant List_Id := New_List;
1400 Index_List1 : constant List_Id := New_List;
1401 Index_List2 : constant List_Id := New_List;
1405 Func_Name : Entity_Id;
1406 Func_Body : Node_Id;
1408 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1409 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1413 -- The parameter types to be used for the formals
1418 Num : Int) return Node_Id;
1419 -- This builds the attribute reference Arr'Nam (Expr)
1421 function Component_Equality (Typ : Entity_Id) return Node_Id;
1422 -- Create one statement to compare corresponding components, designated
1423 -- by a full set of indexes.
1425 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1426 -- Given one of the arguments, computes the appropriate type to be used
1427 -- for that argument in the corresponding function formal
1429 function Handle_One_Dimension
1431 Index : Node_Id) return Node_Id;
1432 -- This procedure returns the following code
1435 -- Bn : Index_T := B'First (N);
1439 -- exit when An = A'Last (N);
1440 -- An := Index_T'Succ (An)
1441 -- Bn := Index_T'Succ (Bn)
1445 -- If both indexes are constrained and identical, the procedure
1446 -- returns a simpler loop:
1448 -- for An in A'Range (N) loop
1452 -- N is the dimension for which we are generating a loop. Index is the
1453 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1454 -- xxx statement is either the loop or declare for the next dimension
1455 -- or if this is the last dimension the comparison of corresponding
1456 -- components of the arrays.
1458 -- The actual way the code works is to return the comparison of
1459 -- corresponding components for the N+1 call. That's neater!
1461 function Test_Empty_Arrays return Node_Id;
1462 -- This function constructs the test for both arrays being empty
1463 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1465 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1467 function Test_Lengths_Correspond return Node_Id;
1468 -- This function constructs the test for arrays having different lengths
1469 -- in at least one index position, in which case the resulting code is:
1471 -- A'length (1) /= B'length (1)
1473 -- A'length (2) /= B'length (2)
1484 Num : Int) return Node_Id
1488 Make_Attribute_Reference (Loc,
1489 Attribute_Name => Nam,
1490 Prefix => New_Reference_To (Arr, Loc),
1491 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1494 ------------------------
1495 -- Component_Equality --
1496 ------------------------
1498 function Component_Equality (Typ : Entity_Id) return Node_Id is
1503 -- if a(i1...) /= b(j1...) then return false; end if;
1506 Make_Indexed_Component (Loc,
1507 Prefix => Make_Identifier (Loc, Chars (A)),
1508 Expressions => Index_List1);
1511 Make_Indexed_Component (Loc,
1512 Prefix => Make_Identifier (Loc, Chars (B)),
1513 Expressions => Index_List2);
1515 Test := Expand_Composite_Equality
1516 (Nod, Component_Type (Typ), L, R, Decls);
1518 -- If some (sub)component is an unchecked_union, the whole operation
1519 -- will raise program error.
1521 if Nkind (Test) = N_Raise_Program_Error then
1523 -- This node is going to be inserted at a location where a
1524 -- statement is expected: clear its Etype so analysis will set
1525 -- it to the expected Standard_Void_Type.
1527 Set_Etype (Test, Empty);
1532 Make_Implicit_If_Statement (Nod,
1533 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1534 Then_Statements => New_List (
1535 Make_Simple_Return_Statement (Loc,
1536 Expression => New_Occurrence_Of (Standard_False, Loc))));
1538 end Component_Equality;
1544 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1555 T := Underlying_Type (T);
1557 X := First_Index (T);
1558 while Present (X) loop
1559 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1561 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1574 --------------------------
1575 -- Handle_One_Dimension --
1576 ---------------------------
1578 function Handle_One_Dimension
1580 Index : Node_Id) return Node_Id
1582 Need_Separate_Indexes : constant Boolean :=
1584 or else not Is_Constrained (Ltyp);
1585 -- If the index types are identical, and we are working with
1586 -- constrained types, then we can use the same index for both
1589 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1592 Index_T : Entity_Id;
1597 if N > Number_Dimensions (Ltyp) then
1598 return Component_Equality (Ltyp);
1601 -- Case where we generate a loop
1603 Index_T := Base_Type (Etype (Index));
1605 if Need_Separate_Indexes then
1606 Bn := Make_Temporary (Loc, 'B');
1611 Append (New_Reference_To (An, Loc), Index_List1);
1612 Append (New_Reference_To (Bn, Loc), Index_List2);
1614 Stm_List := New_List (
1615 Handle_One_Dimension (N + 1, Next_Index (Index)));
1617 if Need_Separate_Indexes then
1619 -- Generate guard for loop, followed by increments of indexes
1621 Append_To (Stm_List,
1622 Make_Exit_Statement (Loc,
1625 Left_Opnd => New_Reference_To (An, Loc),
1626 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1628 Append_To (Stm_List,
1629 Make_Assignment_Statement (Loc,
1630 Name => New_Reference_To (An, Loc),
1632 Make_Attribute_Reference (Loc,
1633 Prefix => New_Reference_To (Index_T, Loc),
1634 Attribute_Name => Name_Succ,
1635 Expressions => New_List (New_Reference_To (An, Loc)))));
1637 Append_To (Stm_List,
1638 Make_Assignment_Statement (Loc,
1639 Name => New_Reference_To (Bn, Loc),
1641 Make_Attribute_Reference (Loc,
1642 Prefix => New_Reference_To (Index_T, Loc),
1643 Attribute_Name => Name_Succ,
1644 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1647 -- If separate indexes, we need a declare block for An and Bn, and a
1648 -- loop without an iteration scheme.
1650 if Need_Separate_Indexes then
1652 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1655 Make_Block_Statement (Loc,
1656 Declarations => New_List (
1657 Make_Object_Declaration (Loc,
1658 Defining_Identifier => An,
1659 Object_Definition => New_Reference_To (Index_T, Loc),
1660 Expression => Arr_Attr (A, Name_First, N)),
1662 Make_Object_Declaration (Loc,
1663 Defining_Identifier => Bn,
1664 Object_Definition => New_Reference_To (Index_T, Loc),
1665 Expression => Arr_Attr (B, Name_First, N))),
1667 Handled_Statement_Sequence =>
1668 Make_Handled_Sequence_Of_Statements (Loc,
1669 Statements => New_List (Loop_Stm)));
1671 -- If no separate indexes, return loop statement with explicit
1672 -- iteration scheme on its own
1676 Make_Implicit_Loop_Statement (Nod,
1677 Statements => Stm_List,
1679 Make_Iteration_Scheme (Loc,
1680 Loop_Parameter_Specification =>
1681 Make_Loop_Parameter_Specification (Loc,
1682 Defining_Identifier => An,
1683 Discrete_Subtype_Definition =>
1684 Arr_Attr (A, Name_Range, N))));
1687 end Handle_One_Dimension;
1689 -----------------------
1690 -- Test_Empty_Arrays --
1691 -----------------------
1693 function Test_Empty_Arrays return Node_Id is
1703 for J in 1 .. Number_Dimensions (Ltyp) loop
1706 Left_Opnd => Arr_Attr (A, Name_Length, J),
1707 Right_Opnd => Make_Integer_Literal (Loc, 0));
1711 Left_Opnd => Arr_Attr (B, Name_Length, J),
1712 Right_Opnd => Make_Integer_Literal (Loc, 0));
1721 Left_Opnd => Relocate_Node (Alist),
1722 Right_Opnd => Atest);
1726 Left_Opnd => Relocate_Node (Blist),
1727 Right_Opnd => Btest);
1734 Right_Opnd => Blist);
1735 end Test_Empty_Arrays;
1737 -----------------------------
1738 -- Test_Lengths_Correspond --
1739 -----------------------------
1741 function Test_Lengths_Correspond return Node_Id is
1747 for J in 1 .. Number_Dimensions (Ltyp) loop
1750 Left_Opnd => Arr_Attr (A, Name_Length, J),
1751 Right_Opnd => Arr_Attr (B, Name_Length, J));
1758 Left_Opnd => Relocate_Node (Result),
1759 Right_Opnd => Rtest);
1764 end Test_Lengths_Correspond;
1766 -- Start of processing for Expand_Array_Equality
1769 Ltyp := Get_Arg_Type (Lhs);
1770 Rtyp := Get_Arg_Type (Rhs);
1772 -- For now, if the argument types are not the same, go to the base type,
1773 -- since the code assumes that the formals have the same type. This is
1774 -- fixable in future ???
1776 if Ltyp /= Rtyp then
1777 Ltyp := Base_Type (Ltyp);
1778 Rtyp := Base_Type (Rtyp);
1779 pragma Assert (Ltyp = Rtyp);
1782 -- Build list of formals for function
1784 Formals := New_List (
1785 Make_Parameter_Specification (Loc,
1786 Defining_Identifier => A,
1787 Parameter_Type => New_Reference_To (Ltyp, Loc)),
1789 Make_Parameter_Specification (Loc,
1790 Defining_Identifier => B,
1791 Parameter_Type => New_Reference_To (Rtyp, Loc)));
1793 Func_Name := Make_Temporary (Loc, 'E');
1795 -- Build statement sequence for function
1798 Make_Subprogram_Body (Loc,
1800 Make_Function_Specification (Loc,
1801 Defining_Unit_Name => Func_Name,
1802 Parameter_Specifications => Formals,
1803 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
1805 Declarations => Decls,
1807 Handled_Statement_Sequence =>
1808 Make_Handled_Sequence_Of_Statements (Loc,
1809 Statements => New_List (
1811 Make_Implicit_If_Statement (Nod,
1812 Condition => Test_Empty_Arrays,
1813 Then_Statements => New_List (
1814 Make_Simple_Return_Statement (Loc,
1816 New_Occurrence_Of (Standard_True, Loc)))),
1818 Make_Implicit_If_Statement (Nod,
1819 Condition => Test_Lengths_Correspond,
1820 Then_Statements => New_List (
1821 Make_Simple_Return_Statement (Loc,
1823 New_Occurrence_Of (Standard_False, Loc)))),
1825 Handle_One_Dimension (1, First_Index (Ltyp)),
1827 Make_Simple_Return_Statement (Loc,
1828 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1830 Set_Has_Completion (Func_Name, True);
1831 Set_Is_Inlined (Func_Name);
1833 -- If the array type is distinct from the type of the arguments, it
1834 -- is the full view of a private type. Apply an unchecked conversion
1835 -- to insure that analysis of the call succeeds.
1845 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1847 L := OK_Convert_To (Ltyp, Lhs);
1851 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1853 R := OK_Convert_To (Rtyp, Rhs);
1856 Actuals := New_List (L, R);
1859 Append_To (Bodies, Func_Body);
1862 Make_Function_Call (Loc,
1863 Name => New_Reference_To (Func_Name, Loc),
1864 Parameter_Associations => Actuals);
1865 end Expand_Array_Equality;
1867 -----------------------------
1868 -- Expand_Boolean_Operator --
1869 -----------------------------
1871 -- Note that we first get the actual subtypes of the operands, since we
1872 -- always want to deal with types that have bounds.
1874 procedure Expand_Boolean_Operator (N : Node_Id) is
1875 Typ : constant Entity_Id := Etype (N);
1878 -- Special case of bit packed array where both operands are known to be
1879 -- properly aligned. In this case we use an efficient run time routine
1880 -- to carry out the operation (see System.Bit_Ops).
1882 if Is_Bit_Packed_Array (Typ)
1883 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
1884 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
1886 Expand_Packed_Boolean_Operator (N);
1890 -- For the normal non-packed case, the general expansion is to build
1891 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
1892 -- and then inserting it into the tree. The original operator node is
1893 -- then rewritten as a call to this function. We also use this in the
1894 -- packed case if either operand is a possibly unaligned object.
1897 Loc : constant Source_Ptr := Sloc (N);
1898 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1899 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1900 Func_Body : Node_Id;
1901 Func_Name : Entity_Id;
1904 Convert_To_Actual_Subtype (L);
1905 Convert_To_Actual_Subtype (R);
1906 Ensure_Defined (Etype (L), N);
1907 Ensure_Defined (Etype (R), N);
1908 Apply_Length_Check (R, Etype (L));
1910 if Nkind (N) = N_Op_Xor then
1911 Silly_Boolean_Array_Xor_Test (N, Etype (L));
1914 if Nkind (Parent (N)) = N_Assignment_Statement
1915 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
1917 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
1919 elsif Nkind (Parent (N)) = N_Op_Not
1920 and then Nkind (N) = N_Op_And
1922 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
1927 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
1928 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1929 Insert_Action (N, Func_Body);
1931 -- Now rewrite the expression with a call
1934 Make_Function_Call (Loc,
1935 Name => New_Reference_To (Func_Name, Loc),
1936 Parameter_Associations =>
1939 Make_Type_Conversion
1940 (Loc, New_Reference_To (Etype (L), Loc), R))));
1942 Analyze_And_Resolve (N, Typ);
1945 end Expand_Boolean_Operator;
1947 -------------------------------
1948 -- Expand_Composite_Equality --
1949 -------------------------------
1951 -- This function is only called for comparing internal fields of composite
1952 -- types when these fields are themselves composites. This is a special
1953 -- case because it is not possible to respect normal Ada visibility rules.
1955 function Expand_Composite_Equality
1960 Bodies : List_Id) return Node_Id
1962 Loc : constant Source_Ptr := Sloc (Nod);
1963 Full_Type : Entity_Id;
1968 if Is_Private_Type (Typ) then
1969 Full_Type := Underlying_Type (Typ);
1974 -- Defense against malformed private types with no completion the error
1975 -- will be diagnosed later by check_completion
1977 if No (Full_Type) then
1978 return New_Reference_To (Standard_False, Loc);
1981 Full_Type := Base_Type (Full_Type);
1983 if Is_Array_Type (Full_Type) then
1985 -- If the operand is an elementary type other than a floating-point
1986 -- type, then we can simply use the built-in block bitwise equality,
1987 -- since the predefined equality operators always apply and bitwise
1988 -- equality is fine for all these cases.
1990 if Is_Elementary_Type (Component_Type (Full_Type))
1991 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
1993 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
1995 -- For composite component types, and floating-point types, use the
1996 -- expansion. This deals with tagged component types (where we use
1997 -- the applicable equality routine) and floating-point, (where we
1998 -- need to worry about negative zeroes), and also the case of any
1999 -- composite type recursively containing such fields.
2002 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2005 elsif Is_Tagged_Type (Full_Type) then
2007 -- Call the primitive operation "=" of this type
2009 if Is_Class_Wide_Type (Full_Type) then
2010 Full_Type := Root_Type (Full_Type);
2013 -- If this is derived from an untagged private type completed with a
2014 -- tagged type, it does not have a full view, so we use the primitive
2015 -- operations of the private type. This check should no longer be
2016 -- necessary when these types receive their full views ???
2018 if Is_Private_Type (Typ)
2019 and then not Is_Tagged_Type (Typ)
2020 and then not Is_Controlled (Typ)
2021 and then Is_Derived_Type (Typ)
2022 and then No (Full_View (Typ))
2024 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2026 Prim := First_Elmt (Primitive_Operations (Full_Type));
2030 Eq_Op := Node (Prim);
2031 exit when Chars (Eq_Op) = Name_Op_Eq
2032 and then Etype (First_Formal (Eq_Op)) =
2033 Etype (Next_Formal (First_Formal (Eq_Op)))
2034 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2036 pragma Assert (Present (Prim));
2039 Eq_Op := Node (Prim);
2042 Make_Function_Call (Loc,
2043 Name => New_Reference_To (Eq_Op, Loc),
2044 Parameter_Associations =>
2046 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2047 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2049 elsif Is_Record_Type (Full_Type) then
2050 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2052 if Present (Eq_Op) then
2053 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2055 -- Inherited equality from parent type. Convert the actuals to
2056 -- match signature of operation.
2059 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2063 Make_Function_Call (Loc,
2064 Name => New_Reference_To (Eq_Op, Loc),
2065 Parameter_Associations =>
2066 New_List (OK_Convert_To (T, Lhs),
2067 OK_Convert_To (T, Rhs)));
2071 -- Comparison between Unchecked_Union components
2073 if Is_Unchecked_Union (Full_Type) then
2075 Lhs_Type : Node_Id := Full_Type;
2076 Rhs_Type : Node_Id := Full_Type;
2077 Lhs_Discr_Val : Node_Id;
2078 Rhs_Discr_Val : Node_Id;
2083 if Nkind (Lhs) = N_Selected_Component then
2084 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2089 if Nkind (Rhs) = N_Selected_Component then
2090 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2093 -- Lhs of the composite equality
2095 if Is_Constrained (Lhs_Type) then
2097 -- Since the enclosing record type can never be an
2098 -- Unchecked_Union (this code is executed for records
2099 -- that do not have variants), we may reference its
2102 if Nkind (Lhs) = N_Selected_Component
2103 and then Has_Per_Object_Constraint (
2104 Entity (Selector_Name (Lhs)))
2107 Make_Selected_Component (Loc,
2108 Prefix => Prefix (Lhs),
2111 Get_Discriminant_Value (
2112 First_Discriminant (Lhs_Type),
2114 Stored_Constraint (Lhs_Type))));
2117 Lhs_Discr_Val := New_Copy (
2118 Get_Discriminant_Value (
2119 First_Discriminant (Lhs_Type),
2121 Stored_Constraint (Lhs_Type)));
2125 -- It is not possible to infer the discriminant since
2126 -- the subtype is not constrained.
2129 Make_Raise_Program_Error (Loc,
2130 Reason => PE_Unchecked_Union_Restriction);
2133 -- Rhs of the composite equality
2135 if Is_Constrained (Rhs_Type) then
2136 if Nkind (Rhs) = N_Selected_Component
2137 and then Has_Per_Object_Constraint (
2138 Entity (Selector_Name (Rhs)))
2141 Make_Selected_Component (Loc,
2142 Prefix => Prefix (Rhs),
2145 Get_Discriminant_Value (
2146 First_Discriminant (Rhs_Type),
2148 Stored_Constraint (Rhs_Type))));
2151 Rhs_Discr_Val := New_Copy (
2152 Get_Discriminant_Value (
2153 First_Discriminant (Rhs_Type),
2155 Stored_Constraint (Rhs_Type)));
2160 Make_Raise_Program_Error (Loc,
2161 Reason => PE_Unchecked_Union_Restriction);
2164 -- Call the TSS equality function with the inferred
2165 -- discriminant values.
2168 Make_Function_Call (Loc,
2169 Name => New_Reference_To (Eq_Op, Loc),
2170 Parameter_Associations => New_List (
2179 Make_Function_Call (Loc,
2180 Name => New_Reference_To (Eq_Op, Loc),
2181 Parameter_Associations => New_List (Lhs, Rhs));
2185 elsif Ada_Version >= Ada_2012 then
2187 -- if no TSS has been created for the type, check whether there is
2188 -- a primitive equality declared for it. If it is abstract replace
2189 -- the call with an explicit raise (AI05-0123).
2195 Prim := First_Elmt (Collect_Primitive_Operations (Full_Type));
2196 while Present (Prim) loop
2198 -- Locate primitive equality with the right signature
2200 if Chars (Node (Prim)) = Name_Op_Eq
2201 and then Etype (First_Formal (Node (Prim))) =
2202 Etype (Next_Formal (First_Formal (Node (Prim))))
2203 and then Etype (Node (Prim)) = Standard_Boolean
2205 if Is_Abstract_Subprogram (Node (Prim)) then
2207 Make_Raise_Program_Error (Loc,
2208 Reason => PE_Explicit_Raise);
2211 Make_Function_Call (Loc,
2212 Name => New_Reference_To (Node (Prim), Loc),
2213 Parameter_Associations => New_List (Lhs, Rhs));
2221 -- Use predefined equality iff no user-defined primitive exists
2223 return Make_Op_Eq (Loc, Lhs, Rhs);
2226 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2230 -- If not array or record type, it is predefined equality.
2232 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2234 end Expand_Composite_Equality;
2236 ------------------------
2237 -- Expand_Concatenate --
2238 ------------------------
2240 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2241 Loc : constant Source_Ptr := Sloc (Cnode);
2243 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2244 -- Result type of concatenation
2246 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2247 -- Component type. Elements of this component type can appear as one
2248 -- of the operands of concatenation as well as arrays.
2250 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2253 Ityp : constant Entity_Id := Base_Type (Istyp);
2254 -- Index type. This is the base type of the index subtype, and is used
2255 -- for all computed bounds (which may be out of range of Istyp in the
2256 -- case of null ranges).
2259 -- This is the type we use to do arithmetic to compute the bounds and
2260 -- lengths of operands. The choice of this type is a little subtle and
2261 -- is discussed in a separate section at the start of the body code.
2263 Concatenation_Error : exception;
2264 -- Raised if concatenation is sure to raise a CE
2266 Result_May_Be_Null : Boolean := True;
2267 -- Reset to False if at least one operand is encountered which is known
2268 -- at compile time to be non-null. Used for handling the special case
2269 -- of setting the high bound to the last operand high bound for a null
2270 -- result, thus ensuring a proper high bound in the super-flat case.
2272 N : constant Nat := List_Length (Opnds);
2273 -- Number of concatenation operands including possibly null operands
2276 -- Number of operands excluding any known to be null, except that the
2277 -- last operand is always retained, in case it provides the bounds for
2281 -- Current operand being processed in the loop through operands. After
2282 -- this loop is complete, always contains the last operand (which is not
2283 -- the same as Operands (NN), since null operands are skipped).
2285 -- Arrays describing the operands, only the first NN entries of each
2286 -- array are set (NN < N when we exclude known null operands).
2288 Is_Fixed_Length : array (1 .. N) of Boolean;
2289 -- True if length of corresponding operand known at compile time
2291 Operands : array (1 .. N) of Node_Id;
2292 -- Set to the corresponding entry in the Opnds list (but note that null
2293 -- operands are excluded, so not all entries in the list are stored).
2295 Fixed_Length : array (1 .. N) of Uint;
2296 -- Set to length of operand. Entries in this array are set only if the
2297 -- corresponding entry in Is_Fixed_Length is True.
2299 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2300 -- Set to lower bound of operand. Either an integer literal in the case
2301 -- where the bound is known at compile time, else actual lower bound.
2302 -- The operand low bound is of type Ityp.
2304 Var_Length : array (1 .. N) of Entity_Id;
2305 -- Set to an entity of type Natural that contains the length of an
2306 -- operand whose length is not known at compile time. Entries in this
2307 -- array are set only if the corresponding entry in Is_Fixed_Length
2308 -- is False. The entity is of type Artyp.
2310 Aggr_Length : array (0 .. N) of Node_Id;
2311 -- The J'th entry in an expression node that represents the total length
2312 -- of operands 1 through J. It is either an integer literal node, or a
2313 -- reference to a constant entity with the right value, so it is fine
2314 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2315 -- entry always is set to zero. The length is of type Artyp.
2317 Low_Bound : Node_Id;
2318 -- A tree node representing the low bound of the result (of type Ityp).
2319 -- This is either an integer literal node, or an identifier reference to
2320 -- a constant entity initialized to the appropriate value.
2322 Last_Opnd_High_Bound : Node_Id;
2323 -- A tree node representing the high bound of the last operand. This
2324 -- need only be set if the result could be null. It is used for the
2325 -- special case of setting the right high bound for a null result.
2326 -- This is of type Ityp.
2328 High_Bound : Node_Id;
2329 -- A tree node representing the high bound of the result (of type Ityp)
2332 -- Result of the concatenation (of type Ityp)
2334 Actions : constant List_Id := New_List;
2335 -- Collect actions to be inserted if Save_Space is False
2337 Save_Space : Boolean;
2338 pragma Warnings (Off, Save_Space);
2339 -- Set to True if we are saving generated code space by calling routines
2340 -- in packages System.Concat_n.
2342 Known_Non_Null_Operand_Seen : Boolean;
2343 -- Set True during generation of the assignements of operands into
2344 -- result once an operand known to be non-null has been seen.
2346 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2347 -- This function makes an N_Integer_Literal node that is returned in
2348 -- analyzed form with the type set to Artyp. Importantly this literal
2349 -- is not flagged as static, so that if we do computations with it that
2350 -- result in statically detected out of range conditions, we will not
2351 -- generate error messages but instead warning messages.
2353 function To_Artyp (X : Node_Id) return Node_Id;
2354 -- Given a node of type Ityp, returns the corresponding value of type
2355 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2356 -- For enum types, the Pos of the value is returned.
2358 function To_Ityp (X : Node_Id) return Node_Id;
2359 -- The inverse function (uses Val in the case of enumeration types)
2361 ------------------------
2362 -- Make_Artyp_Literal --
2363 ------------------------
2365 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2366 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2368 Set_Etype (Result, Artyp);
2369 Set_Analyzed (Result, True);
2370 Set_Is_Static_Expression (Result, False);
2372 end Make_Artyp_Literal;
2378 function To_Artyp (X : Node_Id) return Node_Id is
2380 if Ityp = Base_Type (Artyp) then
2383 elsif Is_Enumeration_Type (Ityp) then
2385 Make_Attribute_Reference (Loc,
2386 Prefix => New_Occurrence_Of (Ityp, Loc),
2387 Attribute_Name => Name_Pos,
2388 Expressions => New_List (X));
2391 return Convert_To (Artyp, X);
2399 function To_Ityp (X : Node_Id) return Node_Id is
2401 if Is_Enumeration_Type (Ityp) then
2403 Make_Attribute_Reference (Loc,
2404 Prefix => New_Occurrence_Of (Ityp, Loc),
2405 Attribute_Name => Name_Val,
2406 Expressions => New_List (X));
2408 -- Case where we will do a type conversion
2411 if Ityp = Base_Type (Artyp) then
2414 return Convert_To (Ityp, X);
2419 -- Local Declarations
2421 Opnd_Typ : Entity_Id;
2429 -- Choose an appropriate computational type
2431 -- We will be doing calculations of lengths and bounds in this routine
2432 -- and computing one from the other in some cases, e.g. getting the high
2433 -- bound by adding the length-1 to the low bound.
2435 -- We can't just use the index type, or even its base type for this
2436 -- purpose for two reasons. First it might be an enumeration type which
2437 -- is not suitable fo computations of any kind, and second it may simply
2438 -- not have enough range. For example if the index type is -128..+127
2439 -- then lengths can be up to 256, which is out of range of the type.
2441 -- For enumeration types, we can simply use Standard_Integer, this is
2442 -- sufficient since the actual number of enumeration literals cannot
2443 -- possibly exceed the range of integer (remember we will be doing the
2444 -- arithmetic with POS values, not representation values).
2446 if Is_Enumeration_Type (Ityp) then
2447 Artyp := Standard_Integer;
2449 -- If index type is Positive, we use the standard unsigned type, to give
2450 -- more room on the top of the range, obviating the need for an overflow
2451 -- check when creating the upper bound. This is needed to avoid junk
2452 -- overflow checks in the common case of String types.
2454 -- ??? Disabled for now
2456 -- elsif Istyp = Standard_Positive then
2457 -- Artyp := Standard_Unsigned;
2459 -- For modular types, we use a 32-bit modular type for types whose size
2460 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2461 -- identity type, and for larger unsigned types we use 64-bits.
2463 elsif Is_Modular_Integer_Type (Ityp) then
2464 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
2465 Artyp := Standard_Unsigned;
2466 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
2469 Artyp := RTE (RE_Long_Long_Unsigned);
2472 -- Similar treatment for signed types
2475 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
2476 Artyp := Standard_Integer;
2477 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
2480 Artyp := Standard_Long_Long_Integer;
2484 -- Supply dummy entry at start of length array
2486 Aggr_Length (0) := Make_Artyp_Literal (0);
2488 -- Go through operands setting up the above arrays
2492 Opnd := Remove_Head (Opnds);
2493 Opnd_Typ := Etype (Opnd);
2495 -- The parent got messed up when we put the operands in a list,
2496 -- so now put back the proper parent for the saved operand.
2498 Set_Parent (Opnd, Parent (Cnode));
2500 -- Set will be True when we have setup one entry in the array
2504 -- Singleton element (or character literal) case
2506 if Base_Type (Opnd_Typ) = Ctyp then
2508 Operands (NN) := Opnd;
2509 Is_Fixed_Length (NN) := True;
2510 Fixed_Length (NN) := Uint_1;
2511 Result_May_Be_Null := False;
2513 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2514 -- since we know that the result cannot be null).
2516 Opnd_Low_Bound (NN) :=
2517 Make_Attribute_Reference (Loc,
2518 Prefix => New_Reference_To (Istyp, Loc),
2519 Attribute_Name => Name_First);
2523 -- String literal case (can only occur for strings of course)
2525 elsif Nkind (Opnd) = N_String_Literal then
2526 Len := String_Literal_Length (Opnd_Typ);
2529 Result_May_Be_Null := False;
2532 -- Capture last operand high bound if result could be null
2534 if J = N and then Result_May_Be_Null then
2535 Last_Opnd_High_Bound :=
2538 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
2539 Right_Opnd => Make_Integer_Literal (Loc, 1));
2542 -- Skip null string literal
2544 if J < N and then Len = 0 then
2549 Operands (NN) := Opnd;
2550 Is_Fixed_Length (NN) := True;
2552 -- Set length and bounds
2554 Fixed_Length (NN) := Len;
2556 Opnd_Low_Bound (NN) :=
2557 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
2564 -- Check constrained case with known bounds
2566 if Is_Constrained (Opnd_Typ) then
2568 Index : constant Node_Id := First_Index (Opnd_Typ);
2569 Indx_Typ : constant Entity_Id := Etype (Index);
2570 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
2571 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
2574 -- Fixed length constrained array type with known at compile
2575 -- time bounds is last case of fixed length operand.
2577 if Compile_Time_Known_Value (Lo)
2579 Compile_Time_Known_Value (Hi)
2582 Loval : constant Uint := Expr_Value (Lo);
2583 Hival : constant Uint := Expr_Value (Hi);
2584 Len : constant Uint :=
2585 UI_Max (Hival - Loval + 1, Uint_0);
2589 Result_May_Be_Null := False;
2592 -- Capture last operand bound if result could be null
2594 if J = N and then Result_May_Be_Null then
2595 Last_Opnd_High_Bound :=
2597 Make_Integer_Literal (Loc,
2598 Intval => Expr_Value (Hi)));
2601 -- Exclude null length case unless last operand
2603 if J < N and then Len = 0 then
2608 Operands (NN) := Opnd;
2609 Is_Fixed_Length (NN) := True;
2610 Fixed_Length (NN) := Len;
2612 Opnd_Low_Bound (NN) := To_Ityp (
2613 Make_Integer_Literal (Loc,
2614 Intval => Expr_Value (Lo)));
2622 -- All cases where the length is not known at compile time, or the
2623 -- special case of an operand which is known to be null but has a
2624 -- lower bound other than 1 or is other than a string type.
2629 -- Capture operand bounds
2631 Opnd_Low_Bound (NN) :=
2632 Make_Attribute_Reference (Loc,
2634 Duplicate_Subexpr (Opnd, Name_Req => True),
2635 Attribute_Name => Name_First);
2637 if J = N and Result_May_Be_Null then
2638 Last_Opnd_High_Bound :=
2640 Make_Attribute_Reference (Loc,
2642 Duplicate_Subexpr (Opnd, Name_Req => True),
2643 Attribute_Name => Name_Last));
2646 -- Capture length of operand in entity
2648 Operands (NN) := Opnd;
2649 Is_Fixed_Length (NN) := False;
2651 Var_Length (NN) := Make_Temporary (Loc, 'L');
2654 Make_Object_Declaration (Loc,
2655 Defining_Identifier => Var_Length (NN),
2656 Constant_Present => True,
2658 Object_Definition =>
2659 New_Occurrence_Of (Artyp, Loc),
2662 Make_Attribute_Reference (Loc,
2664 Duplicate_Subexpr (Opnd, Name_Req => True),
2665 Attribute_Name => Name_Length)));
2669 -- Set next entry in aggregate length array
2671 -- For first entry, make either integer literal for fixed length
2672 -- or a reference to the saved length for variable length.
2675 if Is_Fixed_Length (1) then
2677 Make_Integer_Literal (Loc,
2678 Intval => Fixed_Length (1));
2681 New_Reference_To (Var_Length (1), Loc);
2684 -- If entry is fixed length and only fixed lengths so far, make
2685 -- appropriate new integer literal adding new length.
2687 elsif Is_Fixed_Length (NN)
2688 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
2691 Make_Integer_Literal (Loc,
2692 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
2694 -- All other cases, construct an addition node for the length and
2695 -- create an entity initialized to this length.
2698 Ent := Make_Temporary (Loc, 'L');
2700 if Is_Fixed_Length (NN) then
2701 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
2703 Clen := New_Reference_To (Var_Length (NN), Loc);
2707 Make_Object_Declaration (Loc,
2708 Defining_Identifier => Ent,
2709 Constant_Present => True,
2711 Object_Definition =>
2712 New_Occurrence_Of (Artyp, Loc),
2716 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
2717 Right_Opnd => Clen)));
2719 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
2726 -- If we have only skipped null operands, return the last operand
2733 -- If we have only one non-null operand, return it and we are done.
2734 -- There is one case in which this cannot be done, and that is when
2735 -- the sole operand is of the element type, in which case it must be
2736 -- converted to an array, and the easiest way of doing that is to go
2737 -- through the normal general circuit.
2740 and then Base_Type (Etype (Operands (1))) /= Ctyp
2742 Result := Operands (1);
2746 -- Cases where we have a real concatenation
2748 -- Next step is to find the low bound for the result array that we
2749 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
2751 -- If the ultimate ancestor of the index subtype is a constrained array
2752 -- definition, then the lower bound is that of the index subtype as
2753 -- specified by (RM 4.5.3(6)).
2755 -- The right test here is to go to the root type, and then the ultimate
2756 -- ancestor is the first subtype of this root type.
2758 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
2760 Make_Attribute_Reference (Loc,
2762 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
2763 Attribute_Name => Name_First);
2765 -- If the first operand in the list has known length we know that
2766 -- the lower bound of the result is the lower bound of this operand.
2768 elsif Is_Fixed_Length (1) then
2769 Low_Bound := Opnd_Low_Bound (1);
2771 -- OK, we don't know the lower bound, we have to build a horrible
2772 -- expression actions node of the form
2774 -- if Cond1'Length /= 0 then
2777 -- if Opnd2'Length /= 0 then
2782 -- The nesting ends either when we hit an operand whose length is known
2783 -- at compile time, or on reaching the last operand, whose low bound we
2784 -- take unconditionally whether or not it is null. It's easiest to do
2785 -- this with a recursive procedure:
2789 function Get_Known_Bound (J : Nat) return Node_Id;
2790 -- Returns the lower bound determined by operands J .. NN
2792 ---------------------
2793 -- Get_Known_Bound --
2794 ---------------------
2796 function Get_Known_Bound (J : Nat) return Node_Id is
2798 if Is_Fixed_Length (J) or else J = NN then
2799 return New_Copy (Opnd_Low_Bound (J));
2803 Make_Conditional_Expression (Loc,
2804 Expressions => New_List (
2807 Left_Opnd => New_Reference_To (Var_Length (J), Loc),
2808 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2810 New_Copy (Opnd_Low_Bound (J)),
2811 Get_Known_Bound (J + 1)));
2813 end Get_Known_Bound;
2816 Ent := Make_Temporary (Loc, 'L');
2819 Make_Object_Declaration (Loc,
2820 Defining_Identifier => Ent,
2821 Constant_Present => True,
2822 Object_Definition => New_Occurrence_Of (Ityp, Loc),
2823 Expression => Get_Known_Bound (1)));
2825 Low_Bound := New_Reference_To (Ent, Loc);
2829 -- Now we can safely compute the upper bound, normally
2830 -- Low_Bound + Length - 1.
2835 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2837 Make_Op_Subtract (Loc,
2838 Left_Opnd => New_Copy (Aggr_Length (NN)),
2839 Right_Opnd => Make_Artyp_Literal (1))));
2841 -- Note that calculation of the high bound may cause overflow in some
2842 -- very weird cases, so in the general case we need an overflow check on
2843 -- the high bound. We can avoid this for the common case of string types
2844 -- and other types whose index is Positive, since we chose a wider range
2845 -- for the arithmetic type.
2847 if Istyp /= Standard_Positive then
2848 Activate_Overflow_Check (High_Bound);
2851 -- Handle the exceptional case where the result is null, in which case
2852 -- case the bounds come from the last operand (so that we get the proper
2853 -- bounds if the last operand is super-flat).
2855 if Result_May_Be_Null then
2857 Make_Conditional_Expression (Loc,
2858 Expressions => New_List (
2860 Left_Opnd => New_Copy (Aggr_Length (NN)),
2861 Right_Opnd => Make_Artyp_Literal (0)),
2862 Last_Opnd_High_Bound,
2866 -- Here is where we insert the saved up actions
2868 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
2870 -- Now we construct an array object with appropriate bounds. We mark
2871 -- the target as internal to prevent useless initialization when
2872 -- Initialize_Scalars is enabled.
2874 Ent := Make_Temporary (Loc, 'S');
2875 Set_Is_Internal (Ent);
2877 -- If the bound is statically known to be out of range, we do not want
2878 -- to abort, we want a warning and a runtime constraint error. Note that
2879 -- we have arranged that the result will not be treated as a static
2880 -- constant, so we won't get an illegality during this insertion.
2882 Insert_Action (Cnode,
2883 Make_Object_Declaration (Loc,
2884 Defining_Identifier => Ent,
2885 Object_Definition =>
2886 Make_Subtype_Indication (Loc,
2887 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
2889 Make_Index_Or_Discriminant_Constraint (Loc,
2890 Constraints => New_List (
2892 Low_Bound => Low_Bound,
2893 High_Bound => High_Bound))))),
2894 Suppress => All_Checks);
2896 -- If the result of the concatenation appears as the initializing
2897 -- expression of an object declaration, we can just rename the
2898 -- result, rather than copying it.
2900 Set_OK_To_Rename (Ent);
2902 -- Catch the static out of range case now
2904 if Raises_Constraint_Error (High_Bound) then
2905 raise Concatenation_Error;
2908 -- Now we will generate the assignments to do the actual concatenation
2910 -- There is one case in which we will not do this, namely when all the
2911 -- following conditions are met:
2913 -- The result type is Standard.String
2915 -- There are nine or fewer retained (non-null) operands
2917 -- The optimization level is -O0
2919 -- The corresponding System.Concat_n.Str_Concat_n routine is
2920 -- available in the run time.
2922 -- The debug flag gnatd.c is not set
2924 -- If all these conditions are met then we generate a call to the
2925 -- relevant concatenation routine. The purpose of this is to avoid
2926 -- undesirable code bloat at -O0.
2928 if Atyp = Standard_String
2929 and then NN in 2 .. 9
2930 and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
2931 and then not Debug_Flag_Dot_C
2934 RR : constant array (Nat range 2 .. 9) of RE_Id :=
2945 if RTE_Available (RR (NN)) then
2947 Opnds : constant List_Id :=
2948 New_List (New_Occurrence_Of (Ent, Loc));
2951 for J in 1 .. NN loop
2952 if Is_List_Member (Operands (J)) then
2953 Remove (Operands (J));
2956 if Base_Type (Etype (Operands (J))) = Ctyp then
2958 Make_Aggregate (Loc,
2959 Component_Associations => New_List (
2960 Make_Component_Association (Loc,
2961 Choices => New_List (
2962 Make_Integer_Literal (Loc, 1)),
2963 Expression => Operands (J)))));
2966 Append_To (Opnds, Operands (J));
2970 Insert_Action (Cnode,
2971 Make_Procedure_Call_Statement (Loc,
2972 Name => New_Reference_To (RTE (RR (NN)), Loc),
2973 Parameter_Associations => Opnds));
2975 Result := New_Reference_To (Ent, Loc);
2982 -- Not special case so generate the assignments
2984 Known_Non_Null_Operand_Seen := False;
2986 for J in 1 .. NN loop
2988 Lo : constant Node_Id :=
2990 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2991 Right_Opnd => Aggr_Length (J - 1));
2993 Hi : constant Node_Id :=
2995 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2997 Make_Op_Subtract (Loc,
2998 Left_Opnd => Aggr_Length (J),
2999 Right_Opnd => Make_Artyp_Literal (1)));
3002 -- Singleton case, simple assignment
3004 if Base_Type (Etype (Operands (J))) = Ctyp then
3005 Known_Non_Null_Operand_Seen := True;
3006 Insert_Action (Cnode,
3007 Make_Assignment_Statement (Loc,
3009 Make_Indexed_Component (Loc,
3010 Prefix => New_Occurrence_Of (Ent, Loc),
3011 Expressions => New_List (To_Ityp (Lo))),
3012 Expression => Operands (J)),
3013 Suppress => All_Checks);
3015 -- Array case, slice assignment, skipped when argument is fixed
3016 -- length and known to be null.
3018 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3021 Make_Assignment_Statement (Loc,
3025 New_Occurrence_Of (Ent, Loc),
3028 Low_Bound => To_Ityp (Lo),
3029 High_Bound => To_Ityp (Hi))),
3030 Expression => Operands (J));
3032 if Is_Fixed_Length (J) then
3033 Known_Non_Null_Operand_Seen := True;
3035 elsif not Known_Non_Null_Operand_Seen then
3037 -- Here if operand length is not statically known and no
3038 -- operand known to be non-null has been processed yet.
3039 -- If operand length is 0, we do not need to perform the
3040 -- assignment, and we must avoid the evaluation of the
3041 -- high bound of the slice, since it may underflow if the
3042 -- low bound is Ityp'First.
3045 Make_Implicit_If_Statement (Cnode,
3049 New_Occurrence_Of (Var_Length (J), Loc),
3050 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3055 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3061 -- Finally we build the result, which is a reference to the array object
3063 Result := New_Reference_To (Ent, Loc);
3066 Rewrite (Cnode, Result);
3067 Analyze_And_Resolve (Cnode, Atyp);
3070 when Concatenation_Error =>
3072 -- Kill warning generated for the declaration of the static out of
3073 -- range high bound, and instead generate a Constraint_Error with
3074 -- an appropriate specific message.
3076 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3077 Apply_Compile_Time_Constraint_Error
3079 Msg => "concatenation result upper bound out of range?",
3080 Reason => CE_Range_Check_Failed);
3081 -- Set_Etype (Cnode, Atyp);
3082 end Expand_Concatenate;
3084 ------------------------
3085 -- Expand_N_Allocator --
3086 ------------------------
3088 procedure Expand_N_Allocator (N : Node_Id) is
3089 PtrT : constant Entity_Id := Etype (N);
3090 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
3091 Etyp : constant Entity_Id := Etype (Expression (N));
3092 Loc : constant Source_Ptr := Sloc (N);
3097 procedure Complete_Coextension_Finalization;
3098 -- Generate finalization calls for all nested coextensions of N. This
3099 -- routine may allocate list controllers if necessary.
3101 procedure Rewrite_Coextension (N : Node_Id);
3102 -- Static coextensions have the same lifetime as the entity they
3103 -- constrain. Such occurrences can be rewritten as aliased objects
3104 -- and their unrestricted access used instead of the coextension.
3106 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
3107 -- Given a constrained array type E, returns a node representing the
3108 -- code to compute the size in storage elements for the given type.
3109 -- This is done without using the attribute (which malfunctions for
3112 ---------------------------------------
3113 -- Complete_Coextension_Finalization --
3114 ---------------------------------------
3116 procedure Complete_Coextension_Finalization is
3118 Coext_Elmt : Elmt_Id;
3122 function Inside_A_Return_Statement (N : Node_Id) return Boolean;
3123 -- Determine whether node N is part of a return statement
3125 function Needs_Initialization_Call (N : Node_Id) return Boolean;
3126 -- Determine whether node N is a subtype indicator allocator which
3127 -- acts a coextension. Such coextensions need initialization.
3129 -------------------------------
3130 -- Inside_A_Return_Statement --
3131 -------------------------------
3133 function Inside_A_Return_Statement (N : Node_Id) return Boolean is
3138 while Present (P) loop
3140 (P, N_Extended_Return_Statement, N_Simple_Return_Statement)
3144 -- Stop the traversal when we reach a subprogram body
3146 elsif Nkind (P) = N_Subprogram_Body then
3154 end Inside_A_Return_Statement;
3156 -------------------------------
3157 -- Needs_Initialization_Call --
3158 -------------------------------
3160 function Needs_Initialization_Call (N : Node_Id) return Boolean is
3164 if Nkind (N) = N_Explicit_Dereference
3165 and then Nkind (Prefix (N)) = N_Identifier
3166 and then Nkind (Parent (Entity (Prefix (N)))) =
3167 N_Object_Declaration
3169 Obj_Decl := Parent (Entity (Prefix (N)));
3172 Present (Expression (Obj_Decl))
3173 and then Nkind (Expression (Obj_Decl)) = N_Allocator
3174 and then Nkind (Expression (Expression (Obj_Decl))) /=
3175 N_Qualified_Expression;
3179 end Needs_Initialization_Call;
3181 -- Start of processing for Complete_Coextension_Finalization
3184 -- When a coextension root is inside a return statement, we need to
3185 -- use the finalization chain of the function's scope. This does not
3186 -- apply for controlled named access types because in those cases we
3187 -- can use the finalization chain of the type itself.
3189 if Inside_A_Return_Statement (N)
3191 (Ekind (PtrT) = E_Anonymous_Access_Type
3193 (Ekind (PtrT) = E_Access_Type
3194 and then No (Associated_Final_Chain (PtrT))))
3198 Outer_S : Entity_Id;
3203 while Present (S) and then S /= Standard_Standard loop
3204 if Ekind (S) = E_Function then
3205 Outer_S := Scope (S);
3207 -- Retrieve the declaration of the body
3212 (Corresponding_Body (Parent (Parent (S)))));
3219 -- Push the scope of the function body since we are inserting
3220 -- the list before the body, but we are currently in the body
3221 -- itself. Override the finalization list of PtrT since the
3222 -- finalization context is now different.
3224 Push_Scope (Outer_S);
3225 Build_Final_List (Decl, PtrT);
3229 -- The root allocator may not be controlled, but it still needs a
3230 -- finalization list for all nested coextensions.
3232 elsif No (Associated_Final_Chain (PtrT)) then
3233 Build_Final_List (N, PtrT);
3237 Make_Selected_Component (Loc,
3239 New_Reference_To (Associated_Final_Chain (PtrT), Loc),
3241 Make_Identifier (Loc, Name_F));
3243 Coext_Elmt := First_Elmt (Coextensions (N));
3244 while Present (Coext_Elmt) loop
3245 Coext := Node (Coext_Elmt);
3250 if Nkind (Coext) = N_Identifier then
3252 Make_Unchecked_Type_Conversion (Loc,
3253 Subtype_Mark => New_Reference_To (Etype (Coext), Loc),
3255 Make_Explicit_Dereference (Loc,
3256 Prefix => New_Copy_Tree (Coext)));
3258 Ref := New_Copy_Tree (Coext);
3261 -- No initialization call if not allowed
3263 Check_Restriction (No_Default_Initialization, N);
3265 if not Restriction_Active (No_Default_Initialization) then
3269 -- attach_to_final_list (Ref, Flist, 2)
3271 if Needs_Initialization_Call (Coext) then
3275 Typ => Etype (Coext),
3277 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3280 -- attach_to_final_list (Ref, Flist, 2)
3286 Flist_Ref => New_Copy_Tree (Flist),
3287 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3291 Next_Elmt (Coext_Elmt);
3293 end Complete_Coextension_Finalization;
3295 -------------------------
3296 -- Rewrite_Coextension --
3297 -------------------------
3299 procedure Rewrite_Coextension (N : Node_Id) is
3300 Temp : constant Node_Id := Make_Temporary (Loc, 'C');
3303 -- Cnn : aliased Etyp;
3305 Decl : constant Node_Id :=
3306 Make_Object_Declaration (Loc,
3307 Defining_Identifier => Temp,
3308 Aliased_Present => True,
3309 Object_Definition =>
3310 New_Occurrence_Of (Etyp, Loc));
3314 if Nkind (Expression (N)) = N_Qualified_Expression then
3315 Set_Expression (Decl, Expression (Expression (N)));
3318 -- Find the proper insertion node for the declaration
3321 while Present (Nod) loop
3322 exit when Nkind (Nod) in N_Statement_Other_Than_Procedure_Call
3323 or else Nkind (Nod) = N_Procedure_Call_Statement
3324 or else Nkind (Nod) in N_Declaration;
3325 Nod := Parent (Nod);
3328 Insert_Before (Nod, Decl);
3332 Make_Attribute_Reference (Loc,
3333 Prefix => New_Occurrence_Of (Temp, Loc),
3334 Attribute_Name => Name_Unrestricted_Access));
3336 Analyze_And_Resolve (N, PtrT);
3337 end Rewrite_Coextension;
3339 ------------------------------
3340 -- Size_In_Storage_Elements --
3341 ------------------------------
3343 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
3345 -- Logically this just returns E'Max_Size_In_Storage_Elements.
3346 -- However, the reason for the existence of this function is
3347 -- to construct a test for sizes too large, which means near the
3348 -- 32-bit limit on a 32-bit machine, and precisely the trouble
3349 -- is that we get overflows when sizes are greater than 2**31.
3351 -- So what we end up doing for array types is to use the expression:
3353 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
3355 -- which avoids this problem. All this is a bit bogus, but it does
3356 -- mean we catch common cases of trying to allocate arrays that
3357 -- are too large, and which in the absence of a check results in
3358 -- undetected chaos ???
3365 for J in 1 .. Number_Dimensions (E) loop
3367 Make_Attribute_Reference (Loc,
3368 Prefix => New_Occurrence_Of (E, Loc),
3369 Attribute_Name => Name_Length,
3370 Expressions => New_List (
3371 Make_Integer_Literal (Loc, J)));
3378 Make_Op_Multiply (Loc,
3385 Make_Op_Multiply (Loc,
3388 Make_Attribute_Reference (Loc,
3389 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
3390 Attribute_Name => Name_Max_Size_In_Storage_Elements));
3392 end Size_In_Storage_Elements;
3394 -- Start of processing for Expand_N_Allocator
3397 -- RM E.2.3(22). We enforce that the expected type of an allocator
3398 -- shall not be a remote access-to-class-wide-limited-private type
3400 -- Why is this being done at expansion time, seems clearly wrong ???
3402 Validate_Remote_Access_To_Class_Wide_Type (N);
3404 -- Set the Storage Pool
3406 Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT)));
3408 if Present (Storage_Pool (N)) then
3409 if Is_RTE (Storage_Pool (N), RE_SS_Pool) then
3410 if VM_Target = No_VM then
3411 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3414 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
3415 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
3418 Set_Procedure_To_Call (N,
3419 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
3423 -- Under certain circumstances we can replace an allocator by an access
3424 -- to statically allocated storage. The conditions, as noted in AARM
3425 -- 3.10 (10c) are as follows:
3427 -- Size and initial value is known at compile time
3428 -- Access type is access-to-constant
3430 -- The allocator is not part of a constraint on a record component,
3431 -- because in that case the inserted actions are delayed until the
3432 -- record declaration is fully analyzed, which is too late for the
3433 -- analysis of the rewritten allocator.
3435 if Is_Access_Constant (PtrT)
3436 and then Nkind (Expression (N)) = N_Qualified_Expression
3437 and then Compile_Time_Known_Value (Expression (Expression (N)))
3438 and then Size_Known_At_Compile_Time (Etype (Expression
3440 and then not Is_Record_Type (Current_Scope)
3442 -- Here we can do the optimization. For the allocator
3446 -- We insert an object declaration
3448 -- Tnn : aliased x := y;
3450 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
3451 -- marked as requiring static allocation.
3453 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
3454 Desig := Subtype_Mark (Expression (N));
3456 -- If context is constrained, use constrained subtype directly,
3457 -- so that the constant is not labelled as having a nominally
3458 -- unconstrained subtype.
3460 if Entity (Desig) = Base_Type (Dtyp) then
3461 Desig := New_Occurrence_Of (Dtyp, Loc);
3465 Make_Object_Declaration (Loc,
3466 Defining_Identifier => Temp,
3467 Aliased_Present => True,
3468 Constant_Present => Is_Access_Constant (PtrT),
3469 Object_Definition => Desig,
3470 Expression => Expression (Expression (N))));
3473 Make_Attribute_Reference (Loc,
3474 Prefix => New_Occurrence_Of (Temp, Loc),
3475 Attribute_Name => Name_Unrestricted_Access));
3477 Analyze_And_Resolve (N, PtrT);
3479 -- We set the variable as statically allocated, since we don't want
3480 -- it going on the stack of the current procedure!
3482 Set_Is_Statically_Allocated (Temp);
3486 -- Same if the allocator is an access discriminant for a local object:
3487 -- instead of an allocator we create a local value and constrain the
3488 -- the enclosing object with the corresponding access attribute.
3490 if Is_Static_Coextension (N) then
3491 Rewrite_Coextension (N);
3495 -- The current allocator creates an object which may contain nested
3496 -- coextensions. Use the current allocator's finalization list to
3497 -- generate finalization call for all nested coextensions.
3499 if Is_Coextension_Root (N) then
3500 Complete_Coextension_Finalization;
3503 -- Check for size too large, we do this because the back end misses
3504 -- proper checks here and can generate rubbish allocation calls when
3505 -- we are near the limit. We only do this for the 32-bit address case
3506 -- since that is from a practical point of view where we see a problem.
3508 if System_Address_Size = 32
3509 and then not Storage_Checks_Suppressed (PtrT)
3510 and then not Storage_Checks_Suppressed (Dtyp)
3511 and then not Storage_Checks_Suppressed (Etyp)
3513 -- The check we want to generate should look like
3515 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
3516 -- raise Storage_Error;
3519 -- where 3.5 gigabytes is a constant large enough to accomodate any
3520 -- reasonable request for. But we can't do it this way because at
3521 -- least at the moment we don't compute this attribute right, and
3522 -- can silently give wrong results when the result gets large. Since
3523 -- this is all about large results, that's bad, so instead we only
3524 -- apply the check for constrained arrays, and manually compute the
3525 -- value of the attribute ???
3527 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
3529 Make_Raise_Storage_Error (Loc,
3532 Left_Opnd => Size_In_Storage_Elements (Etyp),
3534 Make_Integer_Literal (Loc,
3535 Intval => Uint_7 * (Uint_2 ** 29))),
3536 Reason => SE_Object_Too_Large));
3540 -- Handle case of qualified expression (other than optimization above)
3541 -- First apply constraint checks, because the bounds or discriminants
3542 -- in the aggregate might not match the subtype mark in the allocator.
3544 if Nkind (Expression (N)) = N_Qualified_Expression then
3545 Apply_Constraint_Check
3546 (Expression (Expression (N)), Etype (Expression (N)));
3548 Expand_Allocator_Expression (N);
3552 -- If the allocator is for a type which requires initialization, and
3553 -- there is no initial value (i.e. operand is a subtype indication
3554 -- rather than a qualified expression), then we must generate a call to
3555 -- the initialization routine using an expressions action node:
3557 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
3559 -- Here ptr_T is the pointer type for the allocator, and T is the
3560 -- subtype of the allocator. A special case arises if the designated
3561 -- type of the access type is a task or contains tasks. In this case
3562 -- the call to Init (Temp.all ...) is replaced by code that ensures
3563 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
3564 -- for details). In addition, if the type T is a task T, then the
3565 -- first argument to Init must be converted to the task record type.
3568 T : constant Entity_Id := Entity (Expression (N));
3576 Temp_Decl : Node_Id;
3577 Temp_Type : Entity_Id;
3578 Attach_Level : Uint;
3581 if No_Initialization (N) then
3584 -- Case of no initialization procedure present
3586 elsif not Has_Non_Null_Base_Init_Proc (T) then
3588 -- Case of simple initialization required
3590 if Needs_Simple_Initialization (T) then
3591 Check_Restriction (No_Default_Initialization, N);
3592 Rewrite (Expression (N),
3593 Make_Qualified_Expression (Loc,
3594 Subtype_Mark => New_Occurrence_Of (T, Loc),
3595 Expression => Get_Simple_Init_Val (T, N)));
3597 Analyze_And_Resolve (Expression (Expression (N)), T);
3598 Analyze_And_Resolve (Expression (N), T);
3599 Set_Paren_Count (Expression (Expression (N)), 1);
3600 Expand_N_Allocator (N);
3602 -- No initialization required
3608 -- Case of initialization procedure present, must be called
3611 Check_Restriction (No_Default_Initialization, N);
3613 if not Restriction_Active (No_Default_Initialization) then
3614 Init := Base_Init_Proc (T);
3616 Temp := Make_Temporary (Loc, 'P');
3618 -- Construct argument list for the initialization routine call
3621 Make_Explicit_Dereference (Loc,
3622 Prefix => New_Reference_To (Temp, Loc));
3623 Set_Assignment_OK (Arg1);
3626 -- The initialization procedure expects a specific type. if the
3627 -- context is access to class wide, indicate that the object
3628 -- being allocated has the right specific type.
3630 if Is_Class_Wide_Type (Dtyp) then
3631 Arg1 := Unchecked_Convert_To (T, Arg1);
3634 -- If designated type is a concurrent type or if it is private
3635 -- type whose definition is a concurrent type, the first
3636 -- argument in the Init routine has to be unchecked conversion
3637 -- to the corresponding record type. If the designated type is
3638 -- a derived type, we also convert the argument to its root
3641 if Is_Concurrent_Type (T) then
3643 Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1);
3645 elsif Is_Private_Type (T)
3646 and then Present (Full_View (T))
3647 and then Is_Concurrent_Type (Full_View (T))
3650 Unchecked_Convert_To
3651 (Corresponding_Record_Type (Full_View (T)), Arg1);
3653 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
3655 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
3657 Arg1 := OK_Convert_To (Etype (Ftyp), Arg1);
3658 Set_Etype (Arg1, Ftyp);
3662 Args := New_List (Arg1);
3664 -- For the task case, pass the Master_Id of the access type as
3665 -- the value of the _Master parameter, and _Chain as the value
3666 -- of the _Chain parameter (_Chain will be defined as part of
3667 -- the generated code for the allocator).
3669 -- In Ada 2005, the context may be a function that returns an
3670 -- anonymous access type. In that case the Master_Id has been
3671 -- created when expanding the function declaration.
3673 if Has_Task (T) then
3674 if No (Master_Id (Base_Type (PtrT))) then
3676 -- The designated type was an incomplete type, and the
3677 -- access type did not get expanded. Salvage it now.
3679 if not Restriction_Active (No_Task_Hierarchy) then
3680 pragma Assert (Present (Parent (Base_Type (PtrT))));
3681 Expand_N_Full_Type_Declaration
3682 (Parent (Base_Type (PtrT)));
3686 -- If the context of the allocator is a declaration or an
3687 -- assignment, we can generate a meaningful image for it,
3688 -- even though subsequent assignments might remove the
3689 -- connection between task and entity. We build this image
3690 -- when the left-hand side is a simple variable, a simple
3691 -- indexed assignment or a simple selected component.
3693 if Nkind (Parent (N)) = N_Assignment_Statement then
3695 Nam : constant Node_Id := Name (Parent (N));
3698 if Is_Entity_Name (Nam) then
3700 Build_Task_Image_Decls
3703 (Entity (Nam), Sloc (Nam)), T);
3706 (Nam, N_Indexed_Component, N_Selected_Component)
3707 and then Is_Entity_Name (Prefix (Nam))
3710 Build_Task_Image_Decls
3711 (Loc, Nam, Etype (Prefix (Nam)));
3713 Decls := Build_Task_Image_Decls (Loc, T, T);
3717 elsif Nkind (Parent (N)) = N_Object_Declaration then
3719 Build_Task_Image_Decls
3720 (Loc, Defining_Identifier (Parent (N)), T);
3723 Decls := Build_Task_Image_Decls (Loc, T, T);
3726 if Restriction_Active (No_Task_Hierarchy) then
3728 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
3732 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
3735 Append_To (Args, Make_Identifier (Loc, Name_uChain));
3737 Decl := Last (Decls);
3739 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
3741 -- Has_Task is false, Decls not used
3747 -- Add discriminants if discriminated type
3750 Dis : Boolean := False;
3754 if Has_Discriminants (T) then
3758 elsif Is_Private_Type (T)
3759 and then Present (Full_View (T))
3760 and then Has_Discriminants (Full_View (T))
3763 Typ := Full_View (T);
3768 -- If the allocated object will be constrained by the
3769 -- default values for discriminants, then build a subtype
3770 -- with those defaults, and change the allocated subtype
3771 -- to that. Note that this happens in fewer cases in Ada
3774 if not Is_Constrained (Typ)
3775 and then Present (Discriminant_Default_Value
3776 (First_Discriminant (Typ)))
3777 and then (Ada_Version < Ada_2005
3779 not Has_Constrained_Partial_View (Typ))
3781 Typ := Build_Default_Subtype (Typ, N);
3782 Set_Expression (N, New_Reference_To (Typ, Loc));
3785 Discr := First_Elmt (Discriminant_Constraint (Typ));
3786 while Present (Discr) loop
3787 Nod := Node (Discr);
3788 Append (New_Copy_Tree (Node (Discr)), Args);
3790 -- AI-416: when the discriminant constraint is an
3791 -- anonymous access type make sure an accessibility
3792 -- check is inserted if necessary (3.10.2(22.q/2))
3794 if Ada_Version >= Ada_2005
3796 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
3798 Apply_Accessibility_Check
3799 (Nod, Typ, Insert_Node => Nod);
3807 -- We set the allocator as analyzed so that when we analyze the
3808 -- expression actions node, we do not get an unwanted recursive
3809 -- expansion of the allocator expression.
3811 Set_Analyzed (N, True);
3812 Nod := Relocate_Node (N);
3814 -- Here is the transformation:
3816 -- output: Temp : constant ptr_T := new T;
3817 -- Init (Temp.all, ...);
3818 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
3819 -- <CTRL> Initialize (Finalizable (Temp.all));
3821 -- Here ptr_T is the pointer type for the allocator, and is the
3822 -- subtype of the allocator.
3825 Make_Object_Declaration (Loc,
3826 Defining_Identifier => Temp,
3827 Constant_Present => True,
3828 Object_Definition => New_Reference_To (Temp_Type, Loc),
3831 Set_Assignment_OK (Temp_Decl);
3832 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
3834 -- If the designated type is a task type or contains tasks,
3835 -- create block to activate created tasks, and insert
3836 -- declaration for Task_Image variable ahead of call.
3838 if Has_Task (T) then
3840 L : constant List_Id := New_List;
3843 Build_Task_Allocate_Block (L, Nod, Args);
3845 Insert_List_Before (First (Declarations (Blk)), Decls);
3846 Insert_Actions (N, L);
3851 Make_Procedure_Call_Statement (Loc,
3852 Name => New_Reference_To (Init, Loc),
3853 Parameter_Associations => Args));
3856 if Needs_Finalization (T) then
3858 -- Postpone the generation of a finalization call for the
3859 -- current allocator if it acts as a coextension.
3861 if Is_Dynamic_Coextension (N) then
3862 if No (Coextensions (N)) then
3863 Set_Coextensions (N, New_Elmt_List);
3866 Append_Elmt (New_Copy_Tree (Arg1), Coextensions (N));
3870 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
3872 -- Anonymous access types created for access parameters
3873 -- are attached to an explicitly constructed controller,
3874 -- which ensures that they can be finalized properly,
3875 -- even if their deallocation might not happen. The list
3876 -- associated with the controller is doubly-linked. For
3877 -- other anonymous access types, the object may end up
3878 -- on the global final list which is singly-linked.
3879 -- Work needed for access discriminants in Ada 2005 ???
3881 if Ekind (PtrT) = E_Anonymous_Access_Type then
3882 Attach_Level := Uint_1;
3884 Attach_Level := Uint_2;
3889 Ref => New_Copy_Tree (Arg1),
3892 With_Attach => Make_Integer_Literal (Loc,
3893 Intval => Attach_Level)));
3897 Rewrite (N, New_Reference_To (Temp, Loc));
3898 Analyze_And_Resolve (N, PtrT);
3903 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
3904 -- object that has been rewritten as a reference, we displace "this"
3905 -- to reference properly its secondary dispatch table.
3907 if Nkind (N) = N_Identifier
3908 and then Is_Interface (Dtyp)
3910 Displace_Allocator_Pointer (N);
3914 when RE_Not_Available =>
3916 end Expand_N_Allocator;
3918 -----------------------
3919 -- Expand_N_And_Then --
3920 -----------------------
3922 procedure Expand_N_And_Then (N : Node_Id)
3923 renames Expand_Short_Circuit_Operator;
3925 ------------------------------
3926 -- Expand_N_Case_Expression --
3927 ------------------------------
3929 procedure Expand_N_Case_Expression (N : Node_Id) is
3930 Loc : constant Source_Ptr := Sloc (N);
3931 Typ : constant Entity_Id := Etype (N);
3943 -- case X is when A => AX, when B => BX ...
3958 -- However, this expansion is wrong for limited types, and also
3959 -- wrong for unconstrained types (since the bounds may not be the
3960 -- same in all branches). Furthermore it involves an extra copy
3961 -- for large objects. So we take care of this by using the following
3962 -- modified expansion for non-scalar types:
3965 -- type Pnn is access all typ;
3969 -- T := AX'Unrestricted_Access;
3971 -- T := BX'Unrestricted_Access;
3977 Make_Case_Statement (Loc,
3978 Expression => Expression (N),
3979 Alternatives => New_List);
3981 Actions := New_List;
3985 if Is_Scalar_Type (Typ) then
3989 Pnn := Make_Temporary (Loc, 'P');
3991 Make_Full_Type_Declaration (Loc,
3992 Defining_Identifier => Pnn,
3994 Make_Access_To_Object_Definition (Loc,
3995 All_Present => True,
3996 Subtype_Indication =>
3997 New_Reference_To (Typ, Loc))));
4001 Tnn := Make_Temporary (Loc, 'T');
4003 Make_Object_Declaration (Loc,
4004 Defining_Identifier => Tnn,
4005 Object_Definition => New_Occurrence_Of (Ttyp, Loc)));
4007 -- Now process the alternatives
4009 Alt := First (Alternatives (N));
4010 while Present (Alt) loop
4012 Aexp : Node_Id := Expression (Alt);
4013 Aloc : constant Source_Ptr := Sloc (Aexp);
4016 if not Is_Scalar_Type (Typ) then
4018 Make_Attribute_Reference (Aloc,
4019 Prefix => Relocate_Node (Aexp),
4020 Attribute_Name => Name_Unrestricted_Access);
4024 (Alternatives (Cstmt),
4025 Make_Case_Statement_Alternative (Sloc (Alt),
4026 Discrete_Choices => Discrete_Choices (Alt),
4027 Statements => New_List (
4028 Make_Assignment_Statement (Aloc,
4029 Name => New_Occurrence_Of (Tnn, Loc),
4030 Expression => Aexp))));
4036 Append_To (Actions, Cstmt);
4038 -- Construct and return final expression with actions
4040 if Is_Scalar_Type (Typ) then
4041 Fexp := New_Occurrence_Of (Tnn, Loc);
4044 Make_Explicit_Dereference (Loc,
4045 Prefix => New_Occurrence_Of (Tnn, Loc));
4049 Make_Expression_With_Actions (Loc,
4051 Actions => Actions));
4053 Analyze_And_Resolve (N, Typ);
4054 end Expand_N_Case_Expression;
4056 -------------------------------------
4057 -- Expand_N_Conditional_Expression --
4058 -------------------------------------
4060 -- Deal with limited types and expression actions
4062 procedure Expand_N_Conditional_Expression (N : Node_Id) is
4063 Loc : constant Source_Ptr := Sloc (N);
4064 Cond : constant Node_Id := First (Expressions (N));
4065 Thenx : constant Node_Id := Next (Cond);
4066 Elsex : constant Node_Id := Next (Thenx);
4067 Typ : constant Entity_Id := Etype (N);
4078 -- Fold at compile time if condition known. We have already folded
4079 -- static conditional expressions, but it is possible to fold any
4080 -- case in which the condition is known at compile time, even though
4081 -- the result is non-static.
4083 -- Note that we don't do the fold of such cases in Sem_Elab because
4084 -- it can cause infinite loops with the expander adding a conditional
4085 -- expression, and Sem_Elab circuitry removing it repeatedly.
4087 if Compile_Time_Known_Value (Cond) then
4088 if Is_True (Expr_Value (Cond)) then
4090 Actions := Then_Actions (N);
4093 Actions := Else_Actions (N);
4098 if Present (Actions) then
4100 -- If we are not allowed to use Expression_With_Actions, just
4101 -- skip the optimization, it is not critical for correctness.
4103 if not Use_Expression_With_Actions then
4104 goto Skip_Optimization;
4108 Make_Expression_With_Actions (Loc,
4109 Expression => Relocate_Node (Expr),
4110 Actions => Actions));
4111 Analyze_And_Resolve (N, Typ);
4114 Rewrite (N, Relocate_Node (Expr));
4117 -- Note that the result is never static (legitimate cases of static
4118 -- conditional expressions were folded in Sem_Eval).
4120 Set_Is_Static_Expression (N, False);
4124 <<Skip_Optimization>>
4126 -- If the type is limited or unconstrained, we expand as follows to
4127 -- avoid any possibility of improper copies.
4129 -- Note: it may be possible to avoid this special processing if the
4130 -- back end uses its own mechanisms for handling by-reference types ???
4132 -- type Ptr is access all Typ;
4136 -- Cnn := then-expr'Unrestricted_Access;
4139 -- Cnn := else-expr'Unrestricted_Access;
4142 -- and replace the conditional expresion by a reference to Cnn.all.
4144 -- This special case can be skipped if the back end handles limited
4145 -- types properly and ensures that no incorrect copies are made.
4147 if Is_By_Reference_Type (Typ)
4148 and then not Back_End_Handles_Limited_Types
4150 Cnn := Make_Temporary (Loc, 'C', N);
4153 Make_Full_Type_Declaration (Loc,
4154 Defining_Identifier => Make_Temporary (Loc, 'A'),
4156 Make_Access_To_Object_Definition (Loc,
4157 All_Present => True,
4158 Subtype_Indication =>
4159 New_Reference_To (Typ, Loc)));
4161 Insert_Action (N, P_Decl);
4164 Make_Object_Declaration (Loc,
4165 Defining_Identifier => Cnn,
4166 Object_Definition =>
4167 New_Occurrence_Of (Defining_Identifier (P_Decl), Loc));
4170 Make_Implicit_If_Statement (N,
4171 Condition => Relocate_Node (Cond),
4173 Then_Statements => New_List (
4174 Make_Assignment_Statement (Sloc (Thenx),
4175 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
4177 Make_Attribute_Reference (Loc,
4178 Attribute_Name => Name_Unrestricted_Access,
4179 Prefix => Relocate_Node (Thenx)))),
4181 Else_Statements => New_List (
4182 Make_Assignment_Statement (Sloc (Elsex),
4183 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
4185 Make_Attribute_Reference (Loc,
4186 Attribute_Name => Name_Unrestricted_Access,
4187 Prefix => Relocate_Node (Elsex)))));
4190 Make_Explicit_Dereference (Loc,
4191 Prefix => New_Occurrence_Of (Cnn, Loc));
4193 -- For other types, we only need to expand if there are other actions
4194 -- associated with either branch.
4196 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
4198 -- We have two approaches to handling this. If we are allowed to use
4199 -- N_Expression_With_Actions, then we can just wrap the actions into
4200 -- the appropriate expression.
4202 if Use_Expression_With_Actions then
4203 if Present (Then_Actions (N)) then
4205 Make_Expression_With_Actions (Sloc (Thenx),
4206 Actions => Then_Actions (N),
4207 Expression => Relocate_Node (Thenx)));
4208 Set_Then_Actions (N, No_List);
4209 Analyze_And_Resolve (Thenx, Typ);
4212 if Present (Else_Actions (N)) then
4214 Make_Expression_With_Actions (Sloc (Elsex),
4215 Actions => Else_Actions (N),
4216 Expression => Relocate_Node (Elsex)));
4217 Set_Else_Actions (N, No_List);
4218 Analyze_And_Resolve (Elsex, Typ);
4223 -- if we can't use N_Expression_With_Actions nodes, then we insert
4224 -- the following sequence of actions (using Insert_Actions):
4229 -- Cnn := then-expr;
4235 -- and replace the conditional expression by a reference to Cnn
4238 Cnn := Make_Temporary (Loc, 'C', N);
4241 Make_Object_Declaration (Loc,
4242 Defining_Identifier => Cnn,
4243 Object_Definition => New_Occurrence_Of (Typ, Loc));
4246 Make_Implicit_If_Statement (N,
4247 Condition => Relocate_Node (Cond),
4249 Then_Statements => New_List (
4250 Make_Assignment_Statement (Sloc (Thenx),
4251 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
4252 Expression => Relocate_Node (Thenx))),
4254 Else_Statements => New_List (
4255 Make_Assignment_Statement (Sloc (Elsex),
4256 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
4257 Expression => Relocate_Node (Elsex))));
4259 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
4260 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
4262 New_N := New_Occurrence_Of (Cnn, Loc);
4265 -- If no actions then no expansion needed, gigi will handle it using
4266 -- the same approach as a C conditional expression.
4272 -- Fall through here for either the limited expansion, or the case of
4273 -- inserting actions for non-limited types. In both these cases, we must
4274 -- move the SLOC of the parent If statement to the newly created one and
4275 -- change it to the SLOC of the expression which, after expansion, will
4276 -- correspond to what is being evaluated.
4278 if Present (Parent (N))
4279 and then Nkind (Parent (N)) = N_If_Statement
4281 Set_Sloc (New_If, Sloc (Parent (N)));
4282 Set_Sloc (Parent (N), Loc);
4285 -- Make sure Then_Actions and Else_Actions are appropriately moved
4286 -- to the new if statement.
4288 if Present (Then_Actions (N)) then
4290 (First (Then_Statements (New_If)), Then_Actions (N));
4293 if Present (Else_Actions (N)) then
4295 (First (Else_Statements (New_If)), Else_Actions (N));
4298 Insert_Action (N, Decl);
4299 Insert_Action (N, New_If);
4301 Analyze_And_Resolve (N, Typ);
4302 end Expand_N_Conditional_Expression;
4304 -----------------------------------
4305 -- Expand_N_Explicit_Dereference --
4306 -----------------------------------
4308 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
4310 -- Insert explicit dereference call for the checked storage pool case
4312 Insert_Dereference_Action (Prefix (N));
4313 end Expand_N_Explicit_Dereference;
4319 procedure Expand_N_In (N : Node_Id) is
4320 Loc : constant Source_Ptr := Sloc (N);
4321 Restyp : constant Entity_Id := Etype (N);
4322 Lop : constant Node_Id := Left_Opnd (N);
4323 Rop : constant Node_Id := Right_Opnd (N);
4324 Static : constant Boolean := Is_OK_Static_Expression (N);
4329 procedure Expand_Set_Membership;
4330 -- For each choice we create a simple equality or membership test.
4331 -- The whole membership is rewritten connecting these with OR ELSE.
4333 ---------------------------
4334 -- Expand_Set_Membership --
4335 ---------------------------
4337 procedure Expand_Set_Membership is
4341 function Make_Cond (Alt : Node_Id) return Node_Id;
4342 -- If the alternative is a subtype mark, create a simple membership
4343 -- test. Otherwise create an equality test for it.
4349 function Make_Cond (Alt : Node_Id) return Node_Id is
4351 L : constant Node_Id := New_Copy (Lop);
4352 R : constant Node_Id := Relocate_Node (Alt);
4355 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
4356 or else Nkind (Alt) = N_Range
4359 Make_In (Sloc (Alt),
4364 Make_Op_Eq (Sloc (Alt),
4372 -- Start of processing for Expand_Set_Membership
4375 Alt := Last (Alternatives (N));
4376 Res := Make_Cond (Alt);
4379 while Present (Alt) loop
4381 Make_Or_Else (Sloc (Alt),
4382 Left_Opnd => Make_Cond (Alt),
4388 Analyze_And_Resolve (N, Standard_Boolean);
4389 end Expand_Set_Membership;
4391 procedure Substitute_Valid_Check;
4392 -- Replaces node N by Lop'Valid. This is done when we have an explicit
4393 -- test for the left operand being in range of its subtype.
4395 ----------------------------
4396 -- Substitute_Valid_Check --
4397 ----------------------------
4399 procedure Substitute_Valid_Check is
4402 Make_Attribute_Reference (Loc,
4403 Prefix => Relocate_Node (Lop),
4404 Attribute_Name => Name_Valid));
4406 Analyze_And_Resolve (N, Restyp);
4408 Error_Msg_N ("?explicit membership test may be optimized away", N);
4409 Error_Msg_N -- CODEFIX
4410 ("\?use ''Valid attribute instead", N);
4412 end Substitute_Valid_Check;
4414 -- Start of processing for Expand_N_In
4417 -- If set membersip case, expand with separate procedure
4419 if Present (Alternatives (N)) then
4420 Remove_Side_Effects (Lop);
4421 Expand_Set_Membership;
4425 -- Not set membership, proceed with expansion
4427 Ltyp := Etype (Left_Opnd (N));
4428 Rtyp := Etype (Right_Opnd (N));
4430 -- Check case of explicit test for an expression in range of its
4431 -- subtype. This is suspicious usage and we replace it with a 'Valid
4432 -- test and give a warning. For floating point types however, this is a
4433 -- standard way to check for finite numbers, and using 'Valid would
4434 -- typically be a pessimization. Also skip this test for predicated
4435 -- types, since it is perfectly reasonable to check if a value meets
4438 if Is_Scalar_Type (Ltyp)
4439 and then not Is_Floating_Point_Type (Ltyp)
4440 and then Nkind (Rop) in N_Has_Entity
4441 and then Ltyp = Entity (Rop)
4442 and then Comes_From_Source (N)
4443 and then VM_Target = No_VM
4444 and then not (Is_Discrete_Type (Ltyp)
4445 and then Present (Predicate_Function (Ltyp)))
4447 Substitute_Valid_Check;
4451 -- Do validity check on operands
4453 if Validity_Checks_On and Validity_Check_Operands then
4454 Ensure_Valid (Left_Opnd (N));
4455 Validity_Check_Range (Right_Opnd (N));
4458 -- Case of explicit range
4460 if Nkind (Rop) = N_Range then
4462 Lo : constant Node_Id := Low_Bound (Rop);
4463 Hi : constant Node_Id := High_Bound (Rop);
4465 Lo_Orig : constant Node_Id := Original_Node (Lo);
4466 Hi_Orig : constant Node_Id := Original_Node (Hi);
4468 Lcheck : Compare_Result;
4469 Ucheck : Compare_Result;
4471 Warn1 : constant Boolean :=
4472 Constant_Condition_Warnings
4473 and then Comes_From_Source (N)
4474 and then not In_Instance;
4475 -- This must be true for any of the optimization warnings, we
4476 -- clearly want to give them only for source with the flag on. We
4477 -- also skip these warnings in an instance since it may be the
4478 -- case that different instantiations have different ranges.
4480 Warn2 : constant Boolean :=
4482 and then Nkind (Original_Node (Rop)) = N_Range
4483 and then Is_Integer_Type (Etype (Lo));
4484 -- For the case where only one bound warning is elided, we also
4485 -- insist on an explicit range and an integer type. The reason is
4486 -- that the use of enumeration ranges including an end point is
4487 -- common, as is the use of a subtype name, one of whose bounds is
4488 -- the same as the type of the expression.
4491 -- If test is explicit x'First .. x'Last, replace by valid check
4493 -- Could use some individual comments for this complex test ???
4495 if Is_Scalar_Type (Ltyp)
4496 and then Nkind (Lo_Orig) = N_Attribute_Reference
4497 and then Attribute_Name (Lo_Orig) = Name_First
4498 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
4499 and then Entity (Prefix (Lo_Orig)) = Ltyp
4500 and then Nkind (Hi_Orig) = N_Attribute_Reference
4501 and then Attribute_Name (Hi_Orig) = Name_Last
4502 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
4503 and then Entity (Prefix (Hi_Orig)) = Ltyp
4504 and then Comes_From_Source (N)
4505 and then VM_Target = No_VM
4507 Substitute_Valid_Check;
4511 -- If bounds of type are known at compile time, and the end points
4512 -- are known at compile time and identical, this is another case
4513 -- for substituting a valid test. We only do this for discrete
4514 -- types, since it won't arise in practice for float types.
4516 if Comes_From_Source (N)
4517 and then Is_Discrete_Type (Ltyp)
4518 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
4519 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
4520 and then Compile_Time_Known_Value (Lo)
4521 and then Compile_Time_Known_Value (Hi)
4522 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
4523 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
4525 -- Kill warnings in instances, since they may be cases where we
4526 -- have a test in the generic that makes sense with some types
4527 -- and not with other types.
4529 and then not In_Instance
4531 Substitute_Valid_Check;
4535 -- If we have an explicit range, do a bit of optimization based on
4536 -- range analysis (we may be able to kill one or both checks).
4538 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
4539 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
4541 -- If either check is known to fail, replace result by False since
4542 -- the other check does not matter. Preserve the static flag for
4543 -- legality checks, because we are constant-folding beyond RM 4.9.
4545 if Lcheck = LT or else Ucheck = GT then
4547 Error_Msg_N ("?range test optimized away", N);
4548 Error_Msg_N ("\?value is known to be out of range", N);
4551 Rewrite (N, New_Reference_To (Standard_False, Loc));
4552 Analyze_And_Resolve (N, Restyp);
4553 Set_Is_Static_Expression (N, Static);
4556 -- If both checks are known to succeed, replace result by True,
4557 -- since we know we are in range.
4559 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4561 Error_Msg_N ("?range test optimized away", N);
4562 Error_Msg_N ("\?value is known to be in range", N);
4565 Rewrite (N, New_Reference_To (Standard_True, Loc));
4566 Analyze_And_Resolve (N, Restyp);
4567 Set_Is_Static_Expression (N, Static);
4570 -- If lower bound check succeeds and upper bound check is not
4571 -- known to succeed or fail, then replace the range check with
4572 -- a comparison against the upper bound.
4574 elsif Lcheck in Compare_GE then
4575 if Warn2 and then not In_Instance then
4576 Error_Msg_N ("?lower bound test optimized away", Lo);
4577 Error_Msg_N ("\?value is known to be in range", Lo);
4583 Right_Opnd => High_Bound (Rop)));
4584 Analyze_And_Resolve (N, Restyp);
4587 -- If upper bound check succeeds and lower bound check is not
4588 -- known to succeed or fail, then replace the range check with
4589 -- a comparison against the lower bound.
4591 elsif Ucheck in Compare_LE then
4592 if Warn2 and then not In_Instance then
4593 Error_Msg_N ("?upper bound test optimized away", Hi);
4594 Error_Msg_N ("\?value is known to be in range", Hi);
4600 Right_Opnd => Low_Bound (Rop)));
4601 Analyze_And_Resolve (N, Restyp);
4605 -- We couldn't optimize away the range check, but there is one
4606 -- more issue. If we are checking constant conditionals, then we
4607 -- see if we can determine the outcome assuming everything is
4608 -- valid, and if so give an appropriate warning.
4610 if Warn1 and then not Assume_No_Invalid_Values then
4611 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
4612 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
4614 -- Result is out of range for valid value
4616 if Lcheck = LT or else Ucheck = GT then
4618 ("?value can only be in range if it is invalid", N);
4620 -- Result is in range for valid value
4622 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4624 ("?value can only be out of range if it is invalid", N);
4626 -- Lower bound check succeeds if value is valid
4628 elsif Warn2 and then Lcheck in Compare_GE then
4630 ("?lower bound check only fails if it is invalid", Lo);
4632 -- Upper bound check succeeds if value is valid
4634 elsif Warn2 and then Ucheck in Compare_LE then
4636 ("?upper bound check only fails for invalid values", Hi);
4641 -- For all other cases of an explicit range, nothing to be done
4645 -- Here right operand is a subtype mark
4649 Typ : Entity_Id := Etype (Rop);
4650 Is_Acc : constant Boolean := Is_Access_Type (Typ);
4651 Cond : Node_Id := Empty;
4653 Obj : Node_Id := Lop;
4654 SCIL_Node : Node_Id;
4657 Remove_Side_Effects (Obj);
4659 -- For tagged type, do tagged membership operation
4661 if Is_Tagged_Type (Typ) then
4663 -- No expansion will be performed when VM_Target, as the VM
4664 -- back-ends will handle the membership tests directly (tags
4665 -- are not explicitly represented in Java objects, so the
4666 -- normal tagged membership expansion is not what we want).
4668 if Tagged_Type_Expansion then
4669 Tagged_Membership (N, SCIL_Node, New_N);
4671 Analyze_And_Resolve (N, Restyp);
4673 -- Update decoration of relocated node referenced by the
4676 if Generate_SCIL and then Present (SCIL_Node) then
4677 Set_SCIL_Node (N, SCIL_Node);
4683 -- If type is scalar type, rewrite as x in t'First .. t'Last.
4684 -- This reason we do this is that the bounds may have the wrong
4685 -- type if they come from the original type definition. Also this
4686 -- way we get all the processing above for an explicit range.
4688 -- Don't do this for predicated types, since in this case we
4689 -- want to check the predicate!
4691 elsif Is_Scalar_Type (Typ) then
4692 if No (Predicate_Function (Typ)) then
4696 Make_Attribute_Reference (Loc,
4697 Attribute_Name => Name_First,
4698 Prefix => New_Reference_To (Typ, Loc)),
4701 Make_Attribute_Reference (Loc,
4702 Attribute_Name => Name_Last,
4703 Prefix => New_Reference_To (Typ, Loc))));
4704 Analyze_And_Resolve (N, Restyp);
4709 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4710 -- a membership test if the subtype mark denotes a constrained
4711 -- Unchecked_Union subtype and the expression lacks inferable
4714 elsif Is_Unchecked_Union (Base_Type (Typ))
4715 and then Is_Constrained (Typ)
4716 and then not Has_Inferable_Discriminants (Lop)
4719 Make_Raise_Program_Error (Loc,
4720 Reason => PE_Unchecked_Union_Restriction));
4722 -- Prevent Gigi from generating incorrect code by rewriting the
4725 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4729 -- Here we have a non-scalar type
4732 Typ := Designated_Type (Typ);
4735 if not Is_Constrained (Typ) then
4736 Rewrite (N, New_Reference_To (Standard_True, Loc));
4737 Analyze_And_Resolve (N, Restyp);
4739 -- For the constrained array case, we have to check the subscripts
4740 -- for an exact match if the lengths are non-zero (the lengths
4741 -- must match in any case).
4743 elsif Is_Array_Type (Typ) then
4744 Check_Subscripts : declare
4745 function Build_Attribute_Reference
4748 Dim : Nat) return Node_Id;
4749 -- Build attribute reference E'Nam (Dim)
4751 -------------------------------
4752 -- Build_Attribute_Reference --
4753 -------------------------------
4755 function Build_Attribute_Reference
4758 Dim : Nat) return Node_Id
4762 Make_Attribute_Reference (Loc,
4764 Attribute_Name => Nam,
4765 Expressions => New_List (
4766 Make_Integer_Literal (Loc, Dim)));
4767 end Build_Attribute_Reference;
4769 -- Start of processing for Check_Subscripts
4772 for J in 1 .. Number_Dimensions (Typ) loop
4773 Evolve_And_Then (Cond,
4776 Build_Attribute_Reference
4777 (Duplicate_Subexpr_No_Checks (Obj),
4780 Build_Attribute_Reference
4781 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
4783 Evolve_And_Then (Cond,
4786 Build_Attribute_Reference
4787 (Duplicate_Subexpr_No_Checks (Obj),
4790 Build_Attribute_Reference
4791 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
4800 Right_Opnd => Make_Null (Loc)),
4801 Right_Opnd => Cond);
4805 Analyze_And_Resolve (N, Restyp);
4806 end Check_Subscripts;
4808 -- These are the cases where constraint checks may be required,
4809 -- e.g. records with possible discriminants
4812 -- Expand the test into a series of discriminant comparisons.
4813 -- The expression that is built is the negation of the one that
4814 -- is used for checking discriminant constraints.
4816 Obj := Relocate_Node (Left_Opnd (N));
4818 if Has_Discriminants (Typ) then
4819 Cond := Make_Op_Not (Loc,
4820 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
4823 Cond := Make_Or_Else (Loc,
4827 Right_Opnd => Make_Null (Loc)),
4828 Right_Opnd => Cond);
4832 Cond := New_Occurrence_Of (Standard_True, Loc);
4836 Analyze_And_Resolve (N, Restyp);
4841 -- At this point, we have done the processing required for the basic
4842 -- membership test, but not yet dealt with the predicate.
4846 -- If a predicate is present, then we do the predicate test, but we
4847 -- most certainly want to omit this if we are within the predicate
4848 -- function itself, since otherwise we have an infinite recursion!
4851 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
4855 and then Current_Scope /= PFunc
4859 Left_Opnd => Relocate_Node (N),
4860 Right_Opnd => Make_Predicate_Call (Rtyp, Lop)));
4862 -- Analyze new expression, mark left operand as analyzed to
4863 -- avoid infinite recursion adding predicate calls.
4865 Set_Analyzed (Left_Opnd (N));
4866 Analyze_And_Resolve (N, Standard_Boolean);
4868 -- All done, skip attempt at compile time determination of result
4875 --------------------------------
4876 -- Expand_N_Indexed_Component --
4877 --------------------------------
4879 procedure Expand_N_Indexed_Component (N : Node_Id) is
4880 Loc : constant Source_Ptr := Sloc (N);
4881 Typ : constant Entity_Id := Etype (N);
4882 P : constant Node_Id := Prefix (N);
4883 T : constant Entity_Id := Etype (P);
4886 -- A special optimization, if we have an indexed component that is
4887 -- selecting from a slice, then we can eliminate the slice, since, for
4888 -- example, x (i .. j)(k) is identical to x(k). The only difference is
4889 -- the range check required by the slice. The range check for the slice
4890 -- itself has already been generated. The range check for the
4891 -- subscripting operation is ensured by converting the subject to
4892 -- the subtype of the slice.
4894 -- This optimization not only generates better code, avoiding slice
4895 -- messing especially in the packed case, but more importantly bypasses
4896 -- some problems in handling this peculiar case, for example, the issue
4897 -- of dealing specially with object renamings.
4899 if Nkind (P) = N_Slice then
4901 Make_Indexed_Component (Loc,
4902 Prefix => Prefix (P),
4903 Expressions => New_List (
4905 (Etype (First_Index (Etype (P))),
4906 First (Expressions (N))))));
4907 Analyze_And_Resolve (N, Typ);
4911 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
4912 -- function, then additional actuals must be passed.
4914 if Ada_Version >= Ada_2005
4915 and then Is_Build_In_Place_Function_Call (P)
4917 Make_Build_In_Place_Call_In_Anonymous_Context (P);
4920 -- If the prefix is an access type, then we unconditionally rewrite if
4921 -- as an explicit dereference. This simplifies processing for several
4922 -- cases, including packed array cases and certain cases in which checks
4923 -- must be generated. We used to try to do this only when it was
4924 -- necessary, but it cleans up the code to do it all the time.
4926 if Is_Access_Type (T) then
4927 Insert_Explicit_Dereference (P);
4928 Analyze_And_Resolve (P, Designated_Type (T));
4931 -- Generate index and validity checks
4933 Generate_Index_Checks (N);
4935 if Validity_Checks_On and then Validity_Check_Subscripts then
4936 Apply_Subscript_Validity_Checks (N);
4939 -- All done for the non-packed case
4941 if not Is_Packed (Etype (Prefix (N))) then
4945 -- For packed arrays that are not bit-packed (i.e. the case of an array
4946 -- with one or more index types with a non-contiguous enumeration type),
4947 -- we can always use the normal packed element get circuit.
4949 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
4950 Expand_Packed_Element_Reference (N);
4954 -- For a reference to a component of a bit packed array, we have to
4955 -- convert it to a reference to the corresponding Packed_Array_Type.
4956 -- We only want to do this for simple references, and not for:
4958 -- Left side of assignment, or prefix of left side of assignment, or
4959 -- prefix of the prefix, to handle packed arrays of packed arrays,
4960 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
4962 -- Renaming objects in renaming associations
4963 -- This case is handled when a use of the renamed variable occurs
4965 -- Actual parameters for a procedure call
4966 -- This case is handled in Exp_Ch6.Expand_Actuals
4968 -- The second expression in a 'Read attribute reference
4970 -- The prefix of an address or bit or size attribute reference
4972 -- The following circuit detects these exceptions
4975 Child : Node_Id := N;
4976 Parnt : Node_Id := Parent (N);
4980 if Nkind (Parnt) = N_Unchecked_Expression then
4983 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
4984 N_Procedure_Call_Statement)
4985 or else (Nkind (Parnt) = N_Parameter_Association
4987 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
4991 elsif Nkind (Parnt) = N_Attribute_Reference
4992 and then (Attribute_Name (Parnt) = Name_Address
4994 Attribute_Name (Parnt) = Name_Bit
4996 Attribute_Name (Parnt) = Name_Size)
4997 and then Prefix (Parnt) = Child
5001 elsif Nkind (Parnt) = N_Assignment_Statement
5002 and then Name (Parnt) = Child
5006 -- If the expression is an index of an indexed component, it must
5007 -- be expanded regardless of context.
5009 elsif Nkind (Parnt) = N_Indexed_Component
5010 and then Child /= Prefix (Parnt)
5012 Expand_Packed_Element_Reference (N);
5015 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
5016 and then Name (Parent (Parnt)) = Parnt
5020 elsif Nkind (Parnt) = N_Attribute_Reference
5021 and then Attribute_Name (Parnt) = Name_Read
5022 and then Next (First (Expressions (Parnt))) = Child
5026 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
5027 and then Prefix (Parnt) = Child
5032 Expand_Packed_Element_Reference (N);
5036 -- Keep looking up tree for unchecked expression, or if we are the
5037 -- prefix of a possible assignment left side.
5040 Parnt := Parent (Child);
5043 end Expand_N_Indexed_Component;
5045 ---------------------
5046 -- Expand_N_Not_In --
5047 ---------------------
5049 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
5050 -- can be done. This avoids needing to duplicate this expansion code.
5052 procedure Expand_N_Not_In (N : Node_Id) is
5053 Loc : constant Source_Ptr := Sloc (N);
5054 Typ : constant Entity_Id := Etype (N);
5055 Cfs : constant Boolean := Comes_From_Source (N);
5062 Left_Opnd => Left_Opnd (N),
5063 Right_Opnd => Right_Opnd (N))));
5065 -- If this is a set membership, preserve list of alternatives
5067 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
5069 -- We want this to appear as coming from source if original does (see
5070 -- transformations in Expand_N_In).
5072 Set_Comes_From_Source (N, Cfs);
5073 Set_Comes_From_Source (Right_Opnd (N), Cfs);
5075 -- Now analyze transformed node
5077 Analyze_And_Resolve (N, Typ);
5078 end Expand_N_Not_In;
5084 -- The only replacement required is for the case of a null of a type that
5085 -- is an access to protected subprogram, or a subtype thereof. We represent
5086 -- such access values as a record, and so we must replace the occurrence of
5087 -- null by the equivalent record (with a null address and a null pointer in
5088 -- it), so that the backend creates the proper value.
5090 procedure Expand_N_Null (N : Node_Id) is
5091 Loc : constant Source_Ptr := Sloc (N);
5092 Typ : constant Entity_Id := Base_Type (Etype (N));
5096 if Is_Access_Protected_Subprogram_Type (Typ) then
5098 Make_Aggregate (Loc,
5099 Expressions => New_List (
5100 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
5104 Analyze_And_Resolve (N, Equivalent_Type (Typ));
5106 -- For subsequent semantic analysis, the node must retain its type.
5107 -- Gigi in any case replaces this type by the corresponding record
5108 -- type before processing the node.
5114 when RE_Not_Available =>
5118 ---------------------
5119 -- Expand_N_Op_Abs --
5120 ---------------------
5122 procedure Expand_N_Op_Abs (N : Node_Id) is
5123 Loc : constant Source_Ptr := Sloc (N);
5124 Expr : constant Node_Id := Right_Opnd (N);
5127 Unary_Op_Validity_Checks (N);
5129 -- Deal with software overflow checking
5131 if not Backend_Overflow_Checks_On_Target
5132 and then Is_Signed_Integer_Type (Etype (N))
5133 and then Do_Overflow_Check (N)
5135 -- The only case to worry about is when the argument is equal to the
5136 -- largest negative number, so what we do is to insert the check:
5138 -- [constraint_error when Expr = typ'Base'First]
5140 -- with the usual Duplicate_Subexpr use coding for expr
5143 Make_Raise_Constraint_Error (Loc,
5146 Left_Opnd => Duplicate_Subexpr (Expr),
5148 Make_Attribute_Reference (Loc,
5150 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
5151 Attribute_Name => Name_First)),
5152 Reason => CE_Overflow_Check_Failed));
5155 -- Vax floating-point types case
5157 if Vax_Float (Etype (N)) then
5158 Expand_Vax_Arith (N);
5160 end Expand_N_Op_Abs;
5162 ---------------------
5163 -- Expand_N_Op_Add --
5164 ---------------------
5166 procedure Expand_N_Op_Add (N : Node_Id) is
5167 Typ : constant Entity_Id := Etype (N);
5170 Binary_Op_Validity_Checks (N);
5172 -- N + 0 = 0 + N = N for integer types
5174 if Is_Integer_Type (Typ) then
5175 if Compile_Time_Known_Value (Right_Opnd (N))
5176 and then Expr_Value (Right_Opnd (N)) = Uint_0
5178 Rewrite (N, Left_Opnd (N));
5181 elsif Compile_Time_Known_Value (Left_Opnd (N))
5182 and then Expr_Value (Left_Opnd (N)) = Uint_0
5184 Rewrite (N, Right_Opnd (N));
5189 -- Arithmetic overflow checks for signed integer/fixed point types
5191 if Is_Signed_Integer_Type (Typ)
5192 or else Is_Fixed_Point_Type (Typ)
5194 Apply_Arithmetic_Overflow_Check (N);
5197 -- Vax floating-point types case
5199 elsif Vax_Float (Typ) then
5200 Expand_Vax_Arith (N);
5202 end Expand_N_Op_Add;
5204 ---------------------
5205 -- Expand_N_Op_And --
5206 ---------------------
5208 procedure Expand_N_Op_And (N : Node_Id) is
5209 Typ : constant Entity_Id := Etype (N);
5212 Binary_Op_Validity_Checks (N);
5214 if Is_Array_Type (Etype (N)) then
5215 Expand_Boolean_Operator (N);
5217 elsif Is_Boolean_Type (Etype (N)) then
5219 -- Replace AND by AND THEN if Short_Circuit_And_Or active and the
5220 -- type is standard Boolean (do not mess with AND that uses a non-
5221 -- standard Boolean type, because something strange is going on).
5223 if Short_Circuit_And_Or and then Typ = Standard_Boolean then
5225 Make_And_Then (Sloc (N),
5226 Left_Opnd => Relocate_Node (Left_Opnd (N)),
5227 Right_Opnd => Relocate_Node (Right_Opnd (N))));
5228 Analyze_And_Resolve (N, Typ);
5230 -- Otherwise, adjust conditions
5233 Adjust_Condition (Left_Opnd (N));
5234 Adjust_Condition (Right_Opnd (N));
5235 Set_Etype (N, Standard_Boolean);
5236 Adjust_Result_Type (N, Typ);
5239 elsif Is_Intrinsic_Subprogram (Entity (N)) then
5240 Expand_Intrinsic_Call (N, Entity (N));
5243 end Expand_N_Op_And;
5245 ------------------------
5246 -- Expand_N_Op_Concat --
5247 ------------------------
5249 procedure Expand_N_Op_Concat (N : Node_Id) is
5251 -- List of operands to be concatenated
5254 -- Node which is to be replaced by the result of concatenating the nodes
5255 -- in the list Opnds.
5258 -- Ensure validity of both operands
5260 Binary_Op_Validity_Checks (N);
5262 -- If we are the left operand of a concatenation higher up the tree,
5263 -- then do nothing for now, since we want to deal with a series of
5264 -- concatenations as a unit.
5266 if Nkind (Parent (N)) = N_Op_Concat
5267 and then N = Left_Opnd (Parent (N))
5272 -- We get here with a concatenation whose left operand may be a
5273 -- concatenation itself with a consistent type. We need to process
5274 -- these concatenation operands from left to right, which means
5275 -- from the deepest node in the tree to the highest node.
5278 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
5279 Cnode := Left_Opnd (Cnode);
5282 -- Now Cnode is the deepest concatenation, and its parents are the
5283 -- concatenation nodes above, so now we process bottom up, doing the
5284 -- operations. We gather a string that is as long as possible up to five
5287 -- The outer loop runs more than once if more than one concatenation
5288 -- type is involved.
5291 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
5292 Set_Parent (Opnds, N);
5294 -- The inner loop gathers concatenation operands
5296 Inner : while Cnode /= N
5297 and then Base_Type (Etype (Cnode)) =
5298 Base_Type (Etype (Parent (Cnode)))
5300 Cnode := Parent (Cnode);
5301 Append (Right_Opnd (Cnode), Opnds);
5304 Expand_Concatenate (Cnode, Opnds);
5306 exit Outer when Cnode = N;
5307 Cnode := Parent (Cnode);
5309 end Expand_N_Op_Concat;
5311 ------------------------
5312 -- Expand_N_Op_Divide --
5313 ------------------------
5315 procedure Expand_N_Op_Divide (N : Node_Id) is
5316 Loc : constant Source_Ptr := Sloc (N);
5317 Lopnd : constant Node_Id := Left_Opnd (N);
5318 Ropnd : constant Node_Id := Right_Opnd (N);
5319 Ltyp : constant Entity_Id := Etype (Lopnd);
5320 Rtyp : constant Entity_Id := Etype (Ropnd);
5321 Typ : Entity_Id := Etype (N);
5322 Rknow : constant Boolean := Is_Integer_Type (Typ)
5324 Compile_Time_Known_Value (Ropnd);
5328 Binary_Op_Validity_Checks (N);
5331 Rval := Expr_Value (Ropnd);
5334 -- N / 1 = N for integer types
5336 if Rknow and then Rval = Uint_1 then
5341 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
5342 -- Is_Power_Of_2_For_Shift is set means that we know that our left
5343 -- operand is an unsigned integer, as required for this to work.
5345 if Nkind (Ropnd) = N_Op_Expon
5346 and then Is_Power_Of_2_For_Shift (Ropnd)
5348 -- We cannot do this transformation in configurable run time mode if we
5349 -- have 64-bit integers and long shifts are not available.
5353 or else Support_Long_Shifts_On_Target)
5356 Make_Op_Shift_Right (Loc,
5359 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
5360 Analyze_And_Resolve (N, Typ);
5364 -- Do required fixup of universal fixed operation
5366 if Typ = Universal_Fixed then
5367 Fixup_Universal_Fixed_Operation (N);
5371 -- Divisions with fixed-point results
5373 if Is_Fixed_Point_Type (Typ) then
5375 -- No special processing if Treat_Fixed_As_Integer is set, since
5376 -- from a semantic point of view such operations are simply integer
5377 -- operations and will be treated that way.
5379 if not Treat_Fixed_As_Integer (N) then
5380 if Is_Integer_Type (Rtyp) then
5381 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
5383 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
5387 -- Other cases of division of fixed-point operands. Again we exclude the
5388 -- case where Treat_Fixed_As_Integer is set.
5390 elsif (Is_Fixed_Point_Type (Ltyp) or else
5391 Is_Fixed_Point_Type (Rtyp))
5392 and then not Treat_Fixed_As_Integer (N)
5394 if Is_Integer_Type (Typ) then
5395 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
5397 pragma Assert (Is_Floating_Point_Type (Typ));
5398 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
5401 -- Mixed-mode operations can appear in a non-static universal context,
5402 -- in which case the integer argument must be converted explicitly.
5404 elsif Typ = Universal_Real
5405 and then Is_Integer_Type (Rtyp)
5408 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
5410 Analyze_And_Resolve (Ropnd, Universal_Real);
5412 elsif Typ = Universal_Real
5413 and then Is_Integer_Type (Ltyp)
5416 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
5418 Analyze_And_Resolve (Lopnd, Universal_Real);
5420 -- Non-fixed point cases, do integer zero divide and overflow checks
5422 elsif Is_Integer_Type (Typ) then
5423 Apply_Divide_Check (N);
5425 -- Check for 64-bit division available, or long shifts if the divisor
5426 -- is a small power of 2 (since such divides will be converted into
5429 if Esize (Ltyp) > 32
5430 and then not Support_64_Bit_Divides_On_Target
5433 or else not Support_Long_Shifts_On_Target
5434 or else (Rval /= Uint_2 and then
5435 Rval /= Uint_4 and then
5436 Rval /= Uint_8 and then
5437 Rval /= Uint_16 and then
5438 Rval /= Uint_32 and then
5441 Error_Msg_CRT ("64-bit division", N);
5444 -- Deal with Vax_Float
5446 elsif Vax_Float (Typ) then
5447 Expand_Vax_Arith (N);
5450 end Expand_N_Op_Divide;
5452 --------------------
5453 -- Expand_N_Op_Eq --
5454 --------------------
5456 procedure Expand_N_Op_Eq (N : Node_Id) is
5457 Loc : constant Source_Ptr := Sloc (N);
5458 Typ : constant Entity_Id := Etype (N);
5459 Lhs : constant Node_Id := Left_Opnd (N);
5460 Rhs : constant Node_Id := Right_Opnd (N);
5461 Bodies : constant List_Id := New_List;
5462 A_Typ : constant Entity_Id := Etype (Lhs);
5464 Typl : Entity_Id := A_Typ;
5465 Op_Name : Entity_Id;
5468 procedure Build_Equality_Call (Eq : Entity_Id);
5469 -- If a constructed equality exists for the type or for its parent,
5470 -- build and analyze call, adding conversions if the operation is
5473 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
5474 -- Determines whether a type has a subcomponent of an unconstrained
5475 -- Unchecked_Union subtype. Typ is a record type.
5477 -------------------------
5478 -- Build_Equality_Call --
5479 -------------------------
5481 procedure Build_Equality_Call (Eq : Entity_Id) is
5482 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
5483 L_Exp : Node_Id := Relocate_Node (Lhs);
5484 R_Exp : Node_Id := Relocate_Node (Rhs);
5487 if Base_Type (Op_Type) /= Base_Type (A_Typ)
5488 and then not Is_Class_Wide_Type (A_Typ)
5490 L_Exp := OK_Convert_To (Op_Type, L_Exp);
5491 R_Exp := OK_Convert_To (Op_Type, R_Exp);
5494 -- If we have an Unchecked_Union, we need to add the inferred
5495 -- discriminant values as actuals in the function call. At this
5496 -- point, the expansion has determined that both operands have
5497 -- inferable discriminants.
5499 if Is_Unchecked_Union (Op_Type) then
5501 Lhs_Type : constant Node_Id := Etype (L_Exp);
5502 Rhs_Type : constant Node_Id := Etype (R_Exp);
5503 Lhs_Discr_Val : Node_Id;
5504 Rhs_Discr_Val : Node_Id;
5507 -- Per-object constrained selected components require special
5508 -- attention. If the enclosing scope of the component is an
5509 -- Unchecked_Union, we cannot reference its discriminants
5510 -- directly. This is why we use the two extra parameters of
5511 -- the equality function of the enclosing Unchecked_Union.
5513 -- type UU_Type (Discr : Integer := 0) is
5516 -- pragma Unchecked_Union (UU_Type);
5518 -- 1. Unchecked_Union enclosing record:
5520 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
5522 -- Comp : UU_Type (Discr);
5524 -- end Enclosing_UU_Type;
5525 -- pragma Unchecked_Union (Enclosing_UU_Type);
5527 -- Obj1 : Enclosing_UU_Type;
5528 -- Obj2 : Enclosing_UU_Type (1);
5530 -- [. . .] Obj1 = Obj2 [. . .]
5534 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
5536 -- A and B are the formal parameters of the equality function
5537 -- of Enclosing_UU_Type. The function always has two extra
5538 -- formals to capture the inferred discriminant values.
5540 -- 2. Non-Unchecked_Union enclosing record:
5543 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
5546 -- Comp : UU_Type (Discr);
5548 -- end Enclosing_Non_UU_Type;
5550 -- Obj1 : Enclosing_Non_UU_Type;
5551 -- Obj2 : Enclosing_Non_UU_Type (1);
5553 -- ... Obj1 = Obj2 ...
5557 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
5558 -- obj1.discr, obj2.discr)) then
5560 -- In this case we can directly reference the discriminants of
5561 -- the enclosing record.
5565 if Nkind (Lhs) = N_Selected_Component
5566 and then Has_Per_Object_Constraint
5567 (Entity (Selector_Name (Lhs)))
5569 -- Enclosing record is an Unchecked_Union, use formal A
5571 if Is_Unchecked_Union (Scope
5572 (Entity (Selector_Name (Lhs))))
5575 Make_Identifier (Loc,
5578 -- Enclosing record is of a non-Unchecked_Union type, it is
5579 -- possible to reference the discriminant.
5583 Make_Selected_Component (Loc,
5584 Prefix => Prefix (Lhs),
5587 (Get_Discriminant_Value
5588 (First_Discriminant (Lhs_Type),
5590 Stored_Constraint (Lhs_Type))));
5593 -- Comment needed here ???
5596 -- Infer the discriminant value
5600 (Get_Discriminant_Value
5601 (First_Discriminant (Lhs_Type),
5603 Stored_Constraint (Lhs_Type)));
5608 if Nkind (Rhs) = N_Selected_Component
5609 and then Has_Per_Object_Constraint
5610 (Entity (Selector_Name (Rhs)))
5612 if Is_Unchecked_Union
5613 (Scope (Entity (Selector_Name (Rhs))))
5616 Make_Identifier (Loc,
5621 Make_Selected_Component (Loc,
5622 Prefix => Prefix (Rhs),
5624 New_Copy (Get_Discriminant_Value (
5625 First_Discriminant (Rhs_Type),
5627 Stored_Constraint (Rhs_Type))));
5632 New_Copy (Get_Discriminant_Value (
5633 First_Discriminant (Rhs_Type),
5635 Stored_Constraint (Rhs_Type)));
5640 Make_Function_Call (Loc,
5641 Name => New_Reference_To (Eq, Loc),
5642 Parameter_Associations => New_List (
5649 -- Normal case, not an unchecked union
5653 Make_Function_Call (Loc,
5654 Name => New_Reference_To (Eq, Loc),
5655 Parameter_Associations => New_List (L_Exp, R_Exp)));
5658 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5659 end Build_Equality_Call;
5661 ------------------------------------
5662 -- Has_Unconstrained_UU_Component --
5663 ------------------------------------
5665 function Has_Unconstrained_UU_Component
5666 (Typ : Node_Id) return Boolean
5668 Tdef : constant Node_Id :=
5669 Type_Definition (Declaration_Node (Base_Type (Typ)));
5673 function Component_Is_Unconstrained_UU
5674 (Comp : Node_Id) return Boolean;
5675 -- Determines whether the subtype of the component is an
5676 -- unconstrained Unchecked_Union.
5678 function Variant_Is_Unconstrained_UU
5679 (Variant : Node_Id) return Boolean;
5680 -- Determines whether a component of the variant has an unconstrained
5681 -- Unchecked_Union subtype.
5683 -----------------------------------
5684 -- Component_Is_Unconstrained_UU --
5685 -----------------------------------
5687 function Component_Is_Unconstrained_UU
5688 (Comp : Node_Id) return Boolean
5691 if Nkind (Comp) /= N_Component_Declaration then
5696 Sindic : constant Node_Id :=
5697 Subtype_Indication (Component_Definition (Comp));
5700 -- Unconstrained nominal type. In the case of a constraint
5701 -- present, the node kind would have been N_Subtype_Indication.
5703 if Nkind (Sindic) = N_Identifier then
5704 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
5709 end Component_Is_Unconstrained_UU;
5711 ---------------------------------
5712 -- Variant_Is_Unconstrained_UU --
5713 ---------------------------------
5715 function Variant_Is_Unconstrained_UU
5716 (Variant : Node_Id) return Boolean
5718 Clist : constant Node_Id := Component_List (Variant);
5721 if Is_Empty_List (Component_Items (Clist)) then
5725 -- We only need to test one component
5728 Comp : Node_Id := First (Component_Items (Clist));
5731 while Present (Comp) loop
5732 if Component_Is_Unconstrained_UU (Comp) then
5740 -- None of the components withing the variant were of
5741 -- unconstrained Unchecked_Union type.
5744 end Variant_Is_Unconstrained_UU;
5746 -- Start of processing for Has_Unconstrained_UU_Component
5749 if Null_Present (Tdef) then
5753 Clist := Component_List (Tdef);
5754 Vpart := Variant_Part (Clist);
5756 -- Inspect available components
5758 if Present (Component_Items (Clist)) then
5760 Comp : Node_Id := First (Component_Items (Clist));
5763 while Present (Comp) loop
5765 -- One component is sufficient
5767 if Component_Is_Unconstrained_UU (Comp) then
5776 -- Inspect available components withing variants
5778 if Present (Vpart) then
5780 Variant : Node_Id := First (Variants (Vpart));
5783 while Present (Variant) loop
5785 -- One component within a variant is sufficient
5787 if Variant_Is_Unconstrained_UU (Variant) then
5796 -- Neither the available components, nor the components inside the
5797 -- variant parts were of an unconstrained Unchecked_Union subtype.
5800 end Has_Unconstrained_UU_Component;
5802 -- Start of processing for Expand_N_Op_Eq
5805 Binary_Op_Validity_Checks (N);
5807 if Ekind (Typl) = E_Private_Type then
5808 Typl := Underlying_Type (Typl);
5809 elsif Ekind (Typl) = E_Private_Subtype then
5810 Typl := Underlying_Type (Base_Type (Typl));
5815 -- It may happen in error situations that the underlying type is not
5816 -- set. The error will be detected later, here we just defend the
5823 Typl := Base_Type (Typl);
5825 -- Boolean types (requiring handling of non-standard case)
5827 if Is_Boolean_Type (Typl) then
5828 Adjust_Condition (Left_Opnd (N));
5829 Adjust_Condition (Right_Opnd (N));
5830 Set_Etype (N, Standard_Boolean);
5831 Adjust_Result_Type (N, Typ);
5835 elsif Is_Array_Type (Typl) then
5837 -- If we are doing full validity checking, and it is possible for the
5838 -- array elements to be invalid then expand out array comparisons to
5839 -- make sure that we check the array elements.
5841 if Validity_Check_Operands
5842 and then not Is_Known_Valid (Component_Type (Typl))
5845 Save_Force_Validity_Checks : constant Boolean :=
5846 Force_Validity_Checks;
5848 Force_Validity_Checks := True;
5850 Expand_Array_Equality
5852 Relocate_Node (Lhs),
5853 Relocate_Node (Rhs),
5856 Insert_Actions (N, Bodies);
5857 Analyze_And_Resolve (N, Standard_Boolean);
5858 Force_Validity_Checks := Save_Force_Validity_Checks;
5861 -- Packed case where both operands are known aligned
5863 elsif Is_Bit_Packed_Array (Typl)
5864 and then not Is_Possibly_Unaligned_Object (Lhs)
5865 and then not Is_Possibly_Unaligned_Object (Rhs)
5867 Expand_Packed_Eq (N);
5869 -- Where the component type is elementary we can use a block bit
5870 -- comparison (if supported on the target) exception in the case
5871 -- of floating-point (negative zero issues require element by
5872 -- element comparison), and atomic types (where we must be sure
5873 -- to load elements independently) and possibly unaligned arrays.
5875 elsif Is_Elementary_Type (Component_Type (Typl))
5876 and then not Is_Floating_Point_Type (Component_Type (Typl))
5877 and then not Is_Atomic (Component_Type (Typl))
5878 and then not Is_Possibly_Unaligned_Object (Lhs)
5879 and then not Is_Possibly_Unaligned_Object (Rhs)
5880 and then Support_Composite_Compare_On_Target
5884 -- For composite and floating-point cases, expand equality loop to
5885 -- make sure of using proper comparisons for tagged types, and
5886 -- correctly handling the floating-point case.
5890 Expand_Array_Equality
5892 Relocate_Node (Lhs),
5893 Relocate_Node (Rhs),
5896 Insert_Actions (N, Bodies, Suppress => All_Checks);
5897 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5902 elsif Is_Record_Type (Typl) then
5904 -- For tagged types, use the primitive "="
5906 if Is_Tagged_Type (Typl) then
5908 -- No need to do anything else compiling under restriction
5909 -- No_Dispatching_Calls. During the semantic analysis we
5910 -- already notified such violation.
5912 if Restriction_Active (No_Dispatching_Calls) then
5916 -- If this is derived from an untagged private type completed with
5917 -- a tagged type, it does not have a full view, so we use the
5918 -- primitive operations of the private type. This check should no
5919 -- longer be necessary when these types get their full views???
5921 if Is_Private_Type (A_Typ)
5922 and then not Is_Tagged_Type (A_Typ)
5923 and then Is_Derived_Type (A_Typ)
5924 and then No (Full_View (A_Typ))
5926 -- Search for equality operation, checking that the operands
5927 -- have the same type. Note that we must find a matching entry,
5928 -- or something is very wrong!
5930 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
5932 while Present (Prim) loop
5933 exit when Chars (Node (Prim)) = Name_Op_Eq
5934 and then Etype (First_Formal (Node (Prim))) =
5935 Etype (Next_Formal (First_Formal (Node (Prim))))
5937 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5942 pragma Assert (Present (Prim));
5943 Op_Name := Node (Prim);
5945 -- Find the type's predefined equality or an overriding
5946 -- user- defined equality. The reason for not simply calling
5947 -- Find_Prim_Op here is that there may be a user-defined
5948 -- overloaded equality op that precedes the equality that we want,
5949 -- so we have to explicitly search (e.g., there could be an
5950 -- equality with two different parameter types).
5953 if Is_Class_Wide_Type (Typl) then
5954 Typl := Root_Type (Typl);
5957 Prim := First_Elmt (Primitive_Operations (Typl));
5958 while Present (Prim) loop
5959 exit when Chars (Node (Prim)) = Name_Op_Eq
5960 and then Etype (First_Formal (Node (Prim))) =
5961 Etype (Next_Formal (First_Formal (Node (Prim))))
5963 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5968 pragma Assert (Present (Prim));
5969 Op_Name := Node (Prim);
5972 Build_Equality_Call (Op_Name);
5974 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
5975 -- predefined equality operator for a type which has a subcomponent
5976 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
5978 elsif Has_Unconstrained_UU_Component (Typl) then
5980 Make_Raise_Program_Error (Loc,
5981 Reason => PE_Unchecked_Union_Restriction));
5983 -- Prevent Gigi from generating incorrect code by rewriting the
5984 -- equality as a standard False.
5987 New_Occurrence_Of (Standard_False, Loc));
5989 elsif Is_Unchecked_Union (Typl) then
5991 -- If we can infer the discriminants of the operands, we make a
5992 -- call to the TSS equality function.
5994 if Has_Inferable_Discriminants (Lhs)
5996 Has_Inferable_Discriminants (Rhs)
5999 (TSS (Root_Type (Typl), TSS_Composite_Equality));
6002 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
6003 -- the predefined equality operator for an Unchecked_Union type
6004 -- if either of the operands lack inferable discriminants.
6007 Make_Raise_Program_Error (Loc,
6008 Reason => PE_Unchecked_Union_Restriction));
6010 -- Prevent Gigi from generating incorrect code by rewriting
6011 -- the equality as a standard False.
6014 New_Occurrence_Of (Standard_False, Loc));
6018 -- If a type support function is present (for complex cases), use it
6020 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
6022 (TSS (Root_Type (Typl), TSS_Composite_Equality));
6024 -- Otherwise expand the component by component equality. Note that
6025 -- we never use block-bit comparisons for records, because of the
6026 -- problems with gaps. The backend will often be able to recombine
6027 -- the separate comparisons that we generate here.
6030 Remove_Side_Effects (Lhs);
6031 Remove_Side_Effects (Rhs);
6033 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
6035 Insert_Actions (N, Bodies, Suppress => All_Checks);
6036 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6040 -- Test if result is known at compile time
6042 Rewrite_Comparison (N);
6044 -- If we still have comparison for Vax_Float, process it
6046 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
6047 Expand_Vax_Comparison (N);
6052 -----------------------
6053 -- Expand_N_Op_Expon --
6054 -----------------------
6056 procedure Expand_N_Op_Expon (N : Node_Id) is
6057 Loc : constant Source_Ptr := Sloc (N);
6058 Typ : constant Entity_Id := Etype (N);
6059 Rtyp : constant Entity_Id := Root_Type (Typ);
6060 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
6061 Bastyp : constant Node_Id := Etype (Base);
6062 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
6063 Exptyp : constant Entity_Id := Etype (Exp);
6064 Ovflo : constant Boolean := Do_Overflow_Check (N);
6073 Binary_Op_Validity_Checks (N);
6075 -- If either operand is of a private type, then we have the use of an
6076 -- intrinsic operator, and we get rid of the privateness, by using root
6077 -- types of underlying types for the actual operation. Otherwise the
6078 -- private types will cause trouble if we expand multiplications or
6079 -- shifts etc. We also do this transformation if the result type is
6080 -- different from the base type.
6082 if Is_Private_Type (Etype (Base))
6084 Is_Private_Type (Typ)
6086 Is_Private_Type (Exptyp)
6088 Rtyp /= Root_Type (Bastyp)
6091 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
6092 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
6096 Unchecked_Convert_To (Typ,
6098 Left_Opnd => Unchecked_Convert_To (Bt, Base),
6099 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
6100 Analyze_And_Resolve (N, Typ);
6105 -- Test for case of known right argument
6107 if Compile_Time_Known_Value (Exp) then
6108 Expv := Expr_Value (Exp);
6110 -- We only fold small non-negative exponents. You might think we
6111 -- could fold small negative exponents for the real case, but we
6112 -- can't because we are required to raise Constraint_Error for
6113 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
6114 -- See ACVC test C4A012B.
6116 if Expv >= 0 and then Expv <= 4 then
6118 -- X ** 0 = 1 (or 1.0)
6122 -- Call Remove_Side_Effects to ensure that any side effects
6123 -- in the ignored left operand (in particular function calls
6124 -- to user defined functions) are properly executed.
6126 Remove_Side_Effects (Base);
6128 if Ekind (Typ) in Integer_Kind then
6129 Xnode := Make_Integer_Literal (Loc, Intval => 1);
6131 Xnode := Make_Real_Literal (Loc, Ureal_1);
6143 Make_Op_Multiply (Loc,
6144 Left_Opnd => Duplicate_Subexpr (Base),
6145 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
6147 -- X ** 3 = X * X * X
6151 Make_Op_Multiply (Loc,
6153 Make_Op_Multiply (Loc,
6154 Left_Opnd => Duplicate_Subexpr (Base),
6155 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
6156 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
6159 -- En : constant base'type := base * base;
6164 Temp := Make_Temporary (Loc, 'E', Base);
6166 Insert_Actions (N, New_List (
6167 Make_Object_Declaration (Loc,
6168 Defining_Identifier => Temp,
6169 Constant_Present => True,
6170 Object_Definition => New_Reference_To (Typ, Loc),
6172 Make_Op_Multiply (Loc,
6173 Left_Opnd => Duplicate_Subexpr (Base),
6174 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
6177 Make_Op_Multiply (Loc,
6178 Left_Opnd => New_Reference_To (Temp, Loc),
6179 Right_Opnd => New_Reference_To (Temp, Loc));
6183 Analyze_And_Resolve (N, Typ);
6188 -- Case of (2 ** expression) appearing as an argument of an integer
6189 -- multiplication, or as the right argument of a division of a non-
6190 -- negative integer. In such cases we leave the node untouched, setting
6191 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
6192 -- of the higher level node converts it into a shift.
6194 -- Another case is 2 ** N in any other context. We simply convert
6195 -- this to 1 * 2 ** N, and then the above transformation applies.
6197 -- Note: this transformation is not applicable for a modular type with
6198 -- a non-binary modulus in the multiplication case, since we get a wrong
6199 -- result if the shift causes an overflow before the modular reduction.
6201 if Nkind (Base) = N_Integer_Literal
6202 and then Intval (Base) = 2
6203 and then Is_Integer_Type (Root_Type (Exptyp))
6204 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
6205 and then Is_Unsigned_Type (Exptyp)
6208 -- First the multiply and divide cases
6210 if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then
6212 P : constant Node_Id := Parent (N);
6213 L : constant Node_Id := Left_Opnd (P);
6214 R : constant Node_Id := Right_Opnd (P);
6217 if (Nkind (P) = N_Op_Multiply
6218 and then not Non_Binary_Modulus (Typ)
6220 ((Is_Integer_Type (Etype (L)) and then R = N)
6222 (Is_Integer_Type (Etype (R)) and then L = N))
6223 and then not Do_Overflow_Check (P))
6225 (Nkind (P) = N_Op_Divide
6226 and then Is_Integer_Type (Etype (L))
6227 and then Is_Unsigned_Type (Etype (L))
6229 and then not Do_Overflow_Check (P))
6231 Set_Is_Power_Of_2_For_Shift (N);
6236 -- Now the other cases
6238 elsif not Non_Binary_Modulus (Typ) then
6240 Make_Op_Multiply (Loc,
6241 Left_Opnd => Make_Integer_Literal (Loc, 1),
6242 Right_Opnd => Relocate_Node (N)));
6243 Analyze_And_Resolve (N, Typ);
6248 -- Fall through if exponentiation must be done using a runtime routine
6250 -- First deal with modular case
6252 if Is_Modular_Integer_Type (Rtyp) then
6254 -- Non-binary case, we call the special exponentiation routine for
6255 -- the non-binary case, converting the argument to Long_Long_Integer
6256 -- and passing the modulus value. Then the result is converted back
6257 -- to the base type.
6259 if Non_Binary_Modulus (Rtyp) then
6262 Make_Function_Call (Loc,
6263 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
6264 Parameter_Associations => New_List (
6265 Convert_To (Standard_Integer, Base),
6266 Make_Integer_Literal (Loc, Modulus (Rtyp)),
6269 -- Binary case, in this case, we call one of two routines, either the
6270 -- unsigned integer case, or the unsigned long long integer case,
6271 -- with a final "and" operation to do the required mod.
6274 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
6275 Ent := RTE (RE_Exp_Unsigned);
6277 Ent := RTE (RE_Exp_Long_Long_Unsigned);
6284 Make_Function_Call (Loc,
6285 Name => New_Reference_To (Ent, Loc),
6286 Parameter_Associations => New_List (
6287 Convert_To (Etype (First_Formal (Ent)), Base),
6290 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
6294 -- Common exit point for modular type case
6296 Analyze_And_Resolve (N, Typ);
6299 -- Signed integer cases, done using either Integer or Long_Long_Integer.
6300 -- It is not worth having routines for Short_[Short_]Integer, since for
6301 -- most machines it would not help, and it would generate more code that
6302 -- might need certification when a certified run time is required.
6304 -- In the integer cases, we have two routines, one for when overflow
6305 -- checks are required, and one when they are not required, since there
6306 -- is a real gain in omitting checks on many machines.
6308 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
6309 or else (Rtyp = Base_Type (Standard_Long_Integer)
6311 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
6312 or else (Rtyp = Universal_Integer)
6314 Etyp := Standard_Long_Long_Integer;
6317 Rent := RE_Exp_Long_Long_Integer;
6319 Rent := RE_Exn_Long_Long_Integer;
6322 elsif Is_Signed_Integer_Type (Rtyp) then
6323 Etyp := Standard_Integer;
6326 Rent := RE_Exp_Integer;
6328 Rent := RE_Exn_Integer;
6331 -- Floating-point cases, always done using Long_Long_Float. We do not
6332 -- need separate routines for the overflow case here, since in the case
6333 -- of floating-point, we generate infinities anyway as a rule (either
6334 -- that or we automatically trap overflow), and if there is an infinity
6335 -- generated and a range check is required, the check will fail anyway.
6338 pragma Assert (Is_Floating_Point_Type (Rtyp));
6339 Etyp := Standard_Long_Long_Float;
6340 Rent := RE_Exn_Long_Long_Float;
6343 -- Common processing for integer cases and floating-point cases.
6344 -- If we are in the right type, we can call runtime routine directly
6347 and then Rtyp /= Universal_Integer
6348 and then Rtyp /= Universal_Real
6351 Make_Function_Call (Loc,
6352 Name => New_Reference_To (RTE (Rent), Loc),
6353 Parameter_Associations => New_List (Base, Exp)));
6355 -- Otherwise we have to introduce conversions (conversions are also
6356 -- required in the universal cases, since the runtime routine is
6357 -- typed using one of the standard types).
6362 Make_Function_Call (Loc,
6363 Name => New_Reference_To (RTE (Rent), Loc),
6364 Parameter_Associations => New_List (
6365 Convert_To (Etyp, Base),
6369 Analyze_And_Resolve (N, Typ);
6373 when RE_Not_Available =>
6375 end Expand_N_Op_Expon;
6377 --------------------
6378 -- Expand_N_Op_Ge --
6379 --------------------
6381 procedure Expand_N_Op_Ge (N : Node_Id) is
6382 Typ : constant Entity_Id := Etype (N);
6383 Op1 : constant Node_Id := Left_Opnd (N);
6384 Op2 : constant Node_Id := Right_Opnd (N);
6385 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6388 Binary_Op_Validity_Checks (N);
6390 if Is_Array_Type (Typ1) then
6391 Expand_Array_Comparison (N);
6395 if Is_Boolean_Type (Typ1) then
6396 Adjust_Condition (Op1);
6397 Adjust_Condition (Op2);
6398 Set_Etype (N, Standard_Boolean);
6399 Adjust_Result_Type (N, Typ);
6402 Rewrite_Comparison (N);
6404 -- If we still have comparison, and Vax_Float type, process it
6406 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6407 Expand_Vax_Comparison (N);
6412 --------------------
6413 -- Expand_N_Op_Gt --
6414 --------------------
6416 procedure Expand_N_Op_Gt (N : Node_Id) is
6417 Typ : constant Entity_Id := Etype (N);
6418 Op1 : constant Node_Id := Left_Opnd (N);
6419 Op2 : constant Node_Id := Right_Opnd (N);
6420 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6423 Binary_Op_Validity_Checks (N);
6425 if Is_Array_Type (Typ1) then
6426 Expand_Array_Comparison (N);
6430 if Is_Boolean_Type (Typ1) then
6431 Adjust_Condition (Op1);
6432 Adjust_Condition (Op2);
6433 Set_Etype (N, Standard_Boolean);
6434 Adjust_Result_Type (N, Typ);
6437 Rewrite_Comparison (N);
6439 -- If we still have comparison, and Vax_Float type, process it
6441 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6442 Expand_Vax_Comparison (N);
6447 --------------------
6448 -- Expand_N_Op_Le --
6449 --------------------
6451 procedure Expand_N_Op_Le (N : Node_Id) is
6452 Typ : constant Entity_Id := Etype (N);
6453 Op1 : constant Node_Id := Left_Opnd (N);
6454 Op2 : constant Node_Id := Right_Opnd (N);
6455 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6458 Binary_Op_Validity_Checks (N);
6460 if Is_Array_Type (Typ1) then
6461 Expand_Array_Comparison (N);
6465 if Is_Boolean_Type (Typ1) then
6466 Adjust_Condition (Op1);
6467 Adjust_Condition (Op2);
6468 Set_Etype (N, Standard_Boolean);
6469 Adjust_Result_Type (N, Typ);
6472 Rewrite_Comparison (N);
6474 -- If we still have comparison, and Vax_Float type, process it
6476 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6477 Expand_Vax_Comparison (N);
6482 --------------------
6483 -- Expand_N_Op_Lt --
6484 --------------------
6486 procedure Expand_N_Op_Lt (N : Node_Id) is
6487 Typ : constant Entity_Id := Etype (N);
6488 Op1 : constant Node_Id := Left_Opnd (N);
6489 Op2 : constant Node_Id := Right_Opnd (N);
6490 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6493 Binary_Op_Validity_Checks (N);
6495 if Is_Array_Type (Typ1) then
6496 Expand_Array_Comparison (N);
6500 if Is_Boolean_Type (Typ1) then
6501 Adjust_Condition (Op1);
6502 Adjust_Condition (Op2);
6503 Set_Etype (N, Standard_Boolean);
6504 Adjust_Result_Type (N, Typ);
6507 Rewrite_Comparison (N);
6509 -- If we still have comparison, and Vax_Float type, process it
6511 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6512 Expand_Vax_Comparison (N);
6517 -----------------------
6518 -- Expand_N_Op_Minus --
6519 -----------------------
6521 procedure Expand_N_Op_Minus (N : Node_Id) is
6522 Loc : constant Source_Ptr := Sloc (N);
6523 Typ : constant Entity_Id := Etype (N);
6526 Unary_Op_Validity_Checks (N);
6528 if not Backend_Overflow_Checks_On_Target
6529 and then Is_Signed_Integer_Type (Etype (N))
6530 and then Do_Overflow_Check (N)
6532 -- Software overflow checking expands -expr into (0 - expr)
6535 Make_Op_Subtract (Loc,
6536 Left_Opnd => Make_Integer_Literal (Loc, 0),
6537 Right_Opnd => Right_Opnd (N)));
6539 Analyze_And_Resolve (N, Typ);
6541 -- Vax floating-point types case
6543 elsif Vax_Float (Etype (N)) then
6544 Expand_Vax_Arith (N);
6546 end Expand_N_Op_Minus;
6548 ---------------------
6549 -- Expand_N_Op_Mod --
6550 ---------------------
6552 procedure Expand_N_Op_Mod (N : Node_Id) is
6553 Loc : constant Source_Ptr := Sloc (N);
6554 Typ : constant Entity_Id := Etype (N);
6555 Left : constant Node_Id := Left_Opnd (N);
6556 Right : constant Node_Id := Right_Opnd (N);
6557 DOC : constant Boolean := Do_Overflow_Check (N);
6558 DDC : constant Boolean := Do_Division_Check (N);
6568 pragma Warnings (Off, Lhi);
6571 Binary_Op_Validity_Checks (N);
6573 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
6574 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
6576 -- Convert mod to rem if operands are known non-negative. We do this
6577 -- since it is quite likely that this will improve the quality of code,
6578 -- (the operation now corresponds to the hardware remainder), and it
6579 -- does not seem likely that it could be harmful.
6581 if LOK and then Llo >= 0
6583 ROK and then Rlo >= 0
6586 Make_Op_Rem (Sloc (N),
6587 Left_Opnd => Left_Opnd (N),
6588 Right_Opnd => Right_Opnd (N)));
6590 -- Instead of reanalyzing the node we do the analysis manually. This
6591 -- avoids anomalies when the replacement is done in an instance and
6592 -- is epsilon more efficient.
6594 Set_Entity (N, Standard_Entity (S_Op_Rem));
6596 Set_Do_Overflow_Check (N, DOC);
6597 Set_Do_Division_Check (N, DDC);
6598 Expand_N_Op_Rem (N);
6601 -- Otherwise, normal mod processing
6604 if Is_Integer_Type (Etype (N)) then
6605 Apply_Divide_Check (N);
6608 -- Apply optimization x mod 1 = 0. We don't really need that with
6609 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
6610 -- certainly harmless.
6612 if Is_Integer_Type (Etype (N))
6613 and then Compile_Time_Known_Value (Right)
6614 and then Expr_Value (Right) = Uint_1
6616 -- Call Remove_Side_Effects to ensure that any side effects in
6617 -- the ignored left operand (in particular function calls to
6618 -- user defined functions) are properly executed.
6620 Remove_Side_Effects (Left);
6622 Rewrite (N, Make_Integer_Literal (Loc, 0));
6623 Analyze_And_Resolve (N, Typ);
6627 -- Deal with annoying case of largest negative number remainder
6628 -- minus one. Gigi does not handle this case correctly, because
6629 -- it generates a divide instruction which may trap in this case.
6631 -- In fact the check is quite easy, if the right operand is -1, then
6632 -- the mod value is always 0, and we can just ignore the left operand
6633 -- completely in this case.
6635 -- The operand type may be private (e.g. in the expansion of an
6636 -- intrinsic operation) so we must use the underlying type to get the
6637 -- bounds, and convert the literals explicitly.
6641 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
6643 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
6645 ((not LOK) or else (Llo = LLB))
6648 Make_Conditional_Expression (Loc,
6649 Expressions => New_List (
6651 Left_Opnd => Duplicate_Subexpr (Right),
6653 Unchecked_Convert_To (Typ,
6654 Make_Integer_Literal (Loc, -1))),
6655 Unchecked_Convert_To (Typ,
6656 Make_Integer_Literal (Loc, Uint_0)),
6657 Relocate_Node (N))));
6659 Set_Analyzed (Next (Next (First (Expressions (N)))));
6660 Analyze_And_Resolve (N, Typ);
6663 end Expand_N_Op_Mod;
6665 --------------------------
6666 -- Expand_N_Op_Multiply --
6667 --------------------------
6669 procedure Expand_N_Op_Multiply (N : Node_Id) is
6670 Loc : constant Source_Ptr := Sloc (N);
6671 Lop : constant Node_Id := Left_Opnd (N);
6672 Rop : constant Node_Id := Right_Opnd (N);
6674 Lp2 : constant Boolean :=
6675 Nkind (Lop) = N_Op_Expon
6676 and then Is_Power_Of_2_For_Shift (Lop);
6678 Rp2 : constant Boolean :=
6679 Nkind (Rop) = N_Op_Expon
6680 and then Is_Power_Of_2_For_Shift (Rop);
6682 Ltyp : constant Entity_Id := Etype (Lop);
6683 Rtyp : constant Entity_Id := Etype (Rop);
6684 Typ : Entity_Id := Etype (N);
6687 Binary_Op_Validity_Checks (N);
6689 -- Special optimizations for integer types
6691 if Is_Integer_Type (Typ) then
6693 -- N * 0 = 0 for integer types
6695 if Compile_Time_Known_Value (Rop)
6696 and then Expr_Value (Rop) = Uint_0
6698 -- Call Remove_Side_Effects to ensure that any side effects in
6699 -- the ignored left operand (in particular function calls to
6700 -- user defined functions) are properly executed.
6702 Remove_Side_Effects (Lop);
6704 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6705 Analyze_And_Resolve (N, Typ);
6709 -- Similar handling for 0 * N = 0
6711 if Compile_Time_Known_Value (Lop)
6712 and then Expr_Value (Lop) = Uint_0
6714 Remove_Side_Effects (Rop);
6715 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6716 Analyze_And_Resolve (N, Typ);
6720 -- N * 1 = 1 * N = N for integer types
6722 -- This optimisation is not done if we are going to
6723 -- rewrite the product 1 * 2 ** N to a shift.
6725 if Compile_Time_Known_Value (Rop)
6726 and then Expr_Value (Rop) = Uint_1
6732 elsif Compile_Time_Known_Value (Lop)
6733 and then Expr_Value (Lop) = Uint_1
6741 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
6742 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6743 -- operand is an integer, as required for this to work.
6748 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
6752 Left_Opnd => Make_Integer_Literal (Loc, 2),
6755 Left_Opnd => Right_Opnd (Lop),
6756 Right_Opnd => Right_Opnd (Rop))));
6757 Analyze_And_Resolve (N, Typ);
6762 Make_Op_Shift_Left (Loc,
6765 Convert_To (Standard_Natural, Right_Opnd (Rop))));
6766 Analyze_And_Resolve (N, Typ);
6770 -- Same processing for the operands the other way round
6774 Make_Op_Shift_Left (Loc,
6777 Convert_To (Standard_Natural, Right_Opnd (Lop))));
6778 Analyze_And_Resolve (N, Typ);
6782 -- Do required fixup of universal fixed operation
6784 if Typ = Universal_Fixed then
6785 Fixup_Universal_Fixed_Operation (N);
6789 -- Multiplications with fixed-point results
6791 if Is_Fixed_Point_Type (Typ) then
6793 -- No special processing if Treat_Fixed_As_Integer is set, since from
6794 -- a semantic point of view such operations are simply integer
6795 -- operations and will be treated that way.
6797 if not Treat_Fixed_As_Integer (N) then
6799 -- Case of fixed * integer => fixed
6801 if Is_Integer_Type (Rtyp) then
6802 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
6804 -- Case of integer * fixed => fixed
6806 elsif Is_Integer_Type (Ltyp) then
6807 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
6809 -- Case of fixed * fixed => fixed
6812 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
6816 -- Other cases of multiplication of fixed-point operands. Again we
6817 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
6819 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6820 and then not Treat_Fixed_As_Integer (N)
6822 if Is_Integer_Type (Typ) then
6823 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
6825 pragma Assert (Is_Floating_Point_Type (Typ));
6826 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
6829 -- Mixed-mode operations can appear in a non-static universal context,
6830 -- in which case the integer argument must be converted explicitly.
6832 elsif Typ = Universal_Real
6833 and then Is_Integer_Type (Rtyp)
6835 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
6837 Analyze_And_Resolve (Rop, Universal_Real);
6839 elsif Typ = Universal_Real
6840 and then Is_Integer_Type (Ltyp)
6842 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
6844 Analyze_And_Resolve (Lop, Universal_Real);
6846 -- Non-fixed point cases, check software overflow checking required
6848 elsif Is_Signed_Integer_Type (Etype (N)) then
6849 Apply_Arithmetic_Overflow_Check (N);
6851 -- Deal with VAX float case
6853 elsif Vax_Float (Typ) then
6854 Expand_Vax_Arith (N);
6857 end Expand_N_Op_Multiply;
6859 --------------------
6860 -- Expand_N_Op_Ne --
6861 --------------------
6863 procedure Expand_N_Op_Ne (N : Node_Id) is
6864 Typ : constant Entity_Id := Etype (Left_Opnd (N));
6867 -- Case of elementary type with standard operator
6869 if Is_Elementary_Type (Typ)
6870 and then Sloc (Entity (N)) = Standard_Location
6872 Binary_Op_Validity_Checks (N);
6874 -- Boolean types (requiring handling of non-standard case)
6876 if Is_Boolean_Type (Typ) then
6877 Adjust_Condition (Left_Opnd (N));
6878 Adjust_Condition (Right_Opnd (N));
6879 Set_Etype (N, Standard_Boolean);
6880 Adjust_Result_Type (N, Typ);
6883 Rewrite_Comparison (N);
6885 -- If we still have comparison for Vax_Float, process it
6887 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
6888 Expand_Vax_Comparison (N);
6892 -- For all cases other than elementary types, we rewrite node as the
6893 -- negation of an equality operation, and reanalyze. The equality to be
6894 -- used is defined in the same scope and has the same signature. This
6895 -- signature must be set explicitly since in an instance it may not have
6896 -- the same visibility as in the generic unit. This avoids duplicating
6897 -- or factoring the complex code for record/array equality tests etc.
6901 Loc : constant Source_Ptr := Sloc (N);
6903 Ne : constant Entity_Id := Entity (N);
6906 Binary_Op_Validity_Checks (N);
6912 Left_Opnd => Left_Opnd (N),
6913 Right_Opnd => Right_Opnd (N)));
6914 Set_Paren_Count (Right_Opnd (Neg), 1);
6916 if Scope (Ne) /= Standard_Standard then
6917 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
6920 -- For navigation purposes, the inequality is treated as an
6921 -- implicit reference to the corresponding equality. Preserve the
6922 -- Comes_From_ source flag so that the proper Xref entry is
6925 Preserve_Comes_From_Source (Neg, N);
6926 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
6928 Analyze_And_Resolve (N, Standard_Boolean);
6933 ---------------------
6934 -- Expand_N_Op_Not --
6935 ---------------------
6937 -- If the argument is other than a Boolean array type, there is no special
6938 -- expansion required, except for VMS operations on signed integers.
6940 -- For the packed case, we call the special routine in Exp_Pakd, except
6941 -- that if the component size is greater than one, we use the standard
6942 -- routine generating a gruesome loop (it is so peculiar to have packed
6943 -- arrays with non-standard Boolean representations anyway, so it does not
6944 -- matter that we do not handle this case efficiently).
6946 -- For the unpacked case (and for the special packed case where we have non
6947 -- standard Booleans, as discussed above), we generate and insert into the
6948 -- tree the following function definition:
6950 -- function Nnnn (A : arr) is
6953 -- for J in a'range loop
6954 -- B (J) := not A (J);
6959 -- Here arr is the actual subtype of the parameter (and hence always
6960 -- constrained). Then we replace the not with a call to this function.
6962 procedure Expand_N_Op_Not (N : Node_Id) is
6963 Loc : constant Source_Ptr := Sloc (N);
6964 Typ : constant Entity_Id := Etype (N);
6973 Func_Name : Entity_Id;
6974 Loop_Statement : Node_Id;
6977 Unary_Op_Validity_Checks (N);
6979 -- For boolean operand, deal with non-standard booleans
6981 if Is_Boolean_Type (Typ) then
6982 Adjust_Condition (Right_Opnd (N));
6983 Set_Etype (N, Standard_Boolean);
6984 Adjust_Result_Type (N, Typ);
6988 -- For the VMS "not" on signed integer types, use conversion to and
6989 -- from a predefined modular type.
6991 if Is_VMS_Operator (Entity (N)) then
6997 -- If this is a derived type, retrieve original VMS type so that
6998 -- the proper sized type is used for intermediate values.
7000 if Is_Derived_Type (Typ) then
7001 Rtyp := First_Subtype (Etype (Typ));
7006 -- The proper unsigned type must have a size compatible with the
7007 -- operand, to prevent misalignment.
7009 if RM_Size (Rtyp) <= 8 then
7010 Utyp := RTE (RE_Unsigned_8);
7012 elsif RM_Size (Rtyp) <= 16 then
7013 Utyp := RTE (RE_Unsigned_16);
7015 elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then
7016 Utyp := RTE (RE_Unsigned_32);
7019 Utyp := RTE (RE_Long_Long_Unsigned);
7023 Unchecked_Convert_To (Typ,
7025 Unchecked_Convert_To (Utyp, Right_Opnd (N)))));
7026 Analyze_And_Resolve (N, Typ);
7031 -- Only array types need any other processing
7033 if not Is_Array_Type (Typ) then
7037 -- Case of array operand. If bit packed with a component size of 1,
7038 -- handle it in Exp_Pakd if the operand is known to be aligned.
7040 if Is_Bit_Packed_Array (Typ)
7041 and then Component_Size (Typ) = 1
7042 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
7044 Expand_Packed_Not (N);
7048 -- Case of array operand which is not bit-packed. If the context is
7049 -- a safe assignment, call in-place operation, If context is a larger
7050 -- boolean expression in the context of a safe assignment, expansion is
7051 -- done by enclosing operation.
7053 Opnd := Relocate_Node (Right_Opnd (N));
7054 Convert_To_Actual_Subtype (Opnd);
7055 Arr := Etype (Opnd);
7056 Ensure_Defined (Arr, N);
7057 Silly_Boolean_Array_Not_Test (N, Arr);
7059 if Nkind (Parent (N)) = N_Assignment_Statement then
7060 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
7061 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
7064 -- Special case the negation of a binary operation
7066 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
7067 and then Safe_In_Place_Array_Op
7068 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
7070 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
7074 elsif Nkind (Parent (N)) in N_Binary_Op
7075 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
7078 Op1 : constant Node_Id := Left_Opnd (Parent (N));
7079 Op2 : constant Node_Id := Right_Opnd (Parent (N));
7080 Lhs : constant Node_Id := Name (Parent (Parent (N)));
7083 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
7085 -- (not A) op (not B) can be reduced to a single call
7087 if N = Op1 and then Nkind (Op2) = N_Op_Not then
7090 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
7093 -- A xor (not B) can also be special-cased
7095 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
7102 A := Make_Defining_Identifier (Loc, Name_uA);
7103 B := Make_Defining_Identifier (Loc, Name_uB);
7104 J := Make_Defining_Identifier (Loc, Name_uJ);
7107 Make_Indexed_Component (Loc,
7108 Prefix => New_Reference_To (A, Loc),
7109 Expressions => New_List (New_Reference_To (J, Loc)));
7112 Make_Indexed_Component (Loc,
7113 Prefix => New_Reference_To (B, Loc),
7114 Expressions => New_List (New_Reference_To (J, Loc)));
7117 Make_Implicit_Loop_Statement (N,
7118 Identifier => Empty,
7121 Make_Iteration_Scheme (Loc,
7122 Loop_Parameter_Specification =>
7123 Make_Loop_Parameter_Specification (Loc,
7124 Defining_Identifier => J,
7125 Discrete_Subtype_Definition =>
7126 Make_Attribute_Reference (Loc,
7127 Prefix => Make_Identifier (Loc, Chars (A)),
7128 Attribute_Name => Name_Range))),
7130 Statements => New_List (
7131 Make_Assignment_Statement (Loc,
7133 Expression => Make_Op_Not (Loc, A_J))));
7135 Func_Name := Make_Temporary (Loc, 'N');
7136 Set_Is_Inlined (Func_Name);
7139 Make_Subprogram_Body (Loc,
7141 Make_Function_Specification (Loc,
7142 Defining_Unit_Name => Func_Name,
7143 Parameter_Specifications => New_List (
7144 Make_Parameter_Specification (Loc,
7145 Defining_Identifier => A,
7146 Parameter_Type => New_Reference_To (Typ, Loc))),
7147 Result_Definition => New_Reference_To (Typ, Loc)),
7149 Declarations => New_List (
7150 Make_Object_Declaration (Loc,
7151 Defining_Identifier => B,
7152 Object_Definition => New_Reference_To (Arr, Loc))),
7154 Handled_Statement_Sequence =>
7155 Make_Handled_Sequence_Of_Statements (Loc,
7156 Statements => New_List (
7158 Make_Simple_Return_Statement (Loc,
7159 Expression => Make_Identifier (Loc, Chars (B)))))));
7162 Make_Function_Call (Loc,
7163 Name => New_Reference_To (Func_Name, Loc),
7164 Parameter_Associations => New_List (Opnd)));
7166 Analyze_And_Resolve (N, Typ);
7167 end Expand_N_Op_Not;
7169 --------------------
7170 -- Expand_N_Op_Or --
7171 --------------------
7173 procedure Expand_N_Op_Or (N : Node_Id) is
7174 Typ : constant Entity_Id := Etype (N);
7177 Binary_Op_Validity_Checks (N);
7179 if Is_Array_Type (Etype (N)) then
7180 Expand_Boolean_Operator (N);
7182 elsif Is_Boolean_Type (Etype (N)) then
7184 -- Replace OR by OR ELSE if Short_Circuit_And_Or active and the type
7185 -- is standard Boolean (do not mess with AND that uses a non-standard
7186 -- Boolean type, because something strange is going on).
7188 if Short_Circuit_And_Or and then Typ = Standard_Boolean then
7190 Make_Or_Else (Sloc (N),
7191 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7192 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7193 Analyze_And_Resolve (N, Typ);
7195 -- Otherwise, adjust conditions
7198 Adjust_Condition (Left_Opnd (N));
7199 Adjust_Condition (Right_Opnd (N));
7200 Set_Etype (N, Standard_Boolean);
7201 Adjust_Result_Type (N, Typ);
7204 elsif Is_Intrinsic_Subprogram (Entity (N)) then
7205 Expand_Intrinsic_Call (N, Entity (N));
7210 ----------------------
7211 -- Expand_N_Op_Plus --
7212 ----------------------
7214 procedure Expand_N_Op_Plus (N : Node_Id) is
7216 Unary_Op_Validity_Checks (N);
7217 end Expand_N_Op_Plus;
7219 ---------------------
7220 -- Expand_N_Op_Rem --
7221 ---------------------
7223 procedure Expand_N_Op_Rem (N : Node_Id) is
7224 Loc : constant Source_Ptr := Sloc (N);
7225 Typ : constant Entity_Id := Etype (N);
7227 Left : constant Node_Id := Left_Opnd (N);
7228 Right : constant Node_Id := Right_Opnd (N);
7236 -- Set if corresponding operand can be negative
7238 pragma Unreferenced (Hi);
7241 Binary_Op_Validity_Checks (N);
7243 if Is_Integer_Type (Etype (N)) then
7244 Apply_Divide_Check (N);
7247 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
7248 -- but it is useful with other back ends (e.g. AAMP), and is certainly
7251 if Is_Integer_Type (Etype (N))
7252 and then Compile_Time_Known_Value (Right)
7253 and then Expr_Value (Right) = Uint_1
7255 -- Call Remove_Side_Effects to ensure that any side effects in the
7256 -- ignored left operand (in particular function calls to user defined
7257 -- functions) are properly executed.
7259 Remove_Side_Effects (Left);
7261 Rewrite (N, Make_Integer_Literal (Loc, 0));
7262 Analyze_And_Resolve (N, Typ);
7266 -- Deal with annoying case of largest negative number remainder minus
7267 -- one. Gigi does not handle this case correctly, because it generates
7268 -- a divide instruction which may trap in this case.
7270 -- In fact the check is quite easy, if the right operand is -1, then
7271 -- the remainder is always 0, and we can just ignore the left operand
7272 -- completely in this case.
7274 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
7275 Lneg := (not OK) or else Lo < 0;
7277 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
7278 Rneg := (not OK) or else Lo < 0;
7280 -- We won't mess with trying to find out if the left operand can really
7281 -- be the largest negative number (that's a pain in the case of private
7282 -- types and this is really marginal). We will just assume that we need
7283 -- the test if the left operand can be negative at all.
7285 if Lneg and Rneg then
7287 Make_Conditional_Expression (Loc,
7288 Expressions => New_List (
7290 Left_Opnd => Duplicate_Subexpr (Right),
7292 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
7294 Unchecked_Convert_To (Typ,
7295 Make_Integer_Literal (Loc, Uint_0)),
7297 Relocate_Node (N))));
7299 Set_Analyzed (Next (Next (First (Expressions (N)))));
7300 Analyze_And_Resolve (N, Typ);
7302 end Expand_N_Op_Rem;
7304 -----------------------------
7305 -- Expand_N_Op_Rotate_Left --
7306 -----------------------------
7308 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
7310 Binary_Op_Validity_Checks (N);
7311 end Expand_N_Op_Rotate_Left;
7313 ------------------------------
7314 -- Expand_N_Op_Rotate_Right --
7315 ------------------------------
7317 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
7319 Binary_Op_Validity_Checks (N);
7320 end Expand_N_Op_Rotate_Right;
7322 ----------------------------
7323 -- Expand_N_Op_Shift_Left --
7324 ----------------------------
7326 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
7328 Binary_Op_Validity_Checks (N);
7329 end Expand_N_Op_Shift_Left;
7331 -----------------------------
7332 -- Expand_N_Op_Shift_Right --
7333 -----------------------------
7335 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
7337 Binary_Op_Validity_Checks (N);
7338 end Expand_N_Op_Shift_Right;
7340 ----------------------------------------
7341 -- Expand_N_Op_Shift_Right_Arithmetic --
7342 ----------------------------------------
7344 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
7346 Binary_Op_Validity_Checks (N);
7347 end Expand_N_Op_Shift_Right_Arithmetic;
7349 --------------------------
7350 -- Expand_N_Op_Subtract --
7351 --------------------------
7353 procedure Expand_N_Op_Subtract (N : Node_Id) is
7354 Typ : constant Entity_Id := Etype (N);
7357 Binary_Op_Validity_Checks (N);
7359 -- N - 0 = N for integer types
7361 if Is_Integer_Type (Typ)
7362 and then Compile_Time_Known_Value (Right_Opnd (N))
7363 and then Expr_Value (Right_Opnd (N)) = 0
7365 Rewrite (N, Left_Opnd (N));
7369 -- Arithmetic overflow checks for signed integer/fixed point types
7371 if Is_Signed_Integer_Type (Typ)
7373 Is_Fixed_Point_Type (Typ)
7375 Apply_Arithmetic_Overflow_Check (N);
7377 -- VAX floating-point types case
7379 elsif Vax_Float (Typ) then
7380 Expand_Vax_Arith (N);
7382 end Expand_N_Op_Subtract;
7384 ---------------------
7385 -- Expand_N_Op_Xor --
7386 ---------------------
7388 procedure Expand_N_Op_Xor (N : Node_Id) is
7389 Typ : constant Entity_Id := Etype (N);
7392 Binary_Op_Validity_Checks (N);
7394 if Is_Array_Type (Etype (N)) then
7395 Expand_Boolean_Operator (N);
7397 elsif Is_Boolean_Type (Etype (N)) then
7398 Adjust_Condition (Left_Opnd (N));
7399 Adjust_Condition (Right_Opnd (N));
7400 Set_Etype (N, Standard_Boolean);
7401 Adjust_Result_Type (N, Typ);
7403 elsif Is_Intrinsic_Subprogram (Entity (N)) then
7404 Expand_Intrinsic_Call (N, Entity (N));
7407 end Expand_N_Op_Xor;
7409 ----------------------
7410 -- Expand_N_Or_Else --
7411 ----------------------
7413 procedure Expand_N_Or_Else (N : Node_Id)
7414 renames Expand_Short_Circuit_Operator;
7416 -----------------------------------
7417 -- Expand_N_Qualified_Expression --
7418 -----------------------------------
7420 procedure Expand_N_Qualified_Expression (N : Node_Id) is
7421 Operand : constant Node_Id := Expression (N);
7422 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
7425 -- Do validity check if validity checking operands
7427 if Validity_Checks_On
7428 and then Validity_Check_Operands
7430 Ensure_Valid (Operand);
7433 -- Apply possible constraint check
7435 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
7437 if Do_Range_Check (Operand) then
7438 Set_Do_Range_Check (Operand, False);
7439 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
7441 end Expand_N_Qualified_Expression;
7443 ------------------------------------
7444 -- Expand_N_Quantified_Expression --
7445 ------------------------------------
7449 -- for all X in range => Cond
7454 -- for X in range loop
7461 -- Conversely, an existentially quantified expression:
7463 -- for some X in range => Cond
7468 -- for X in range loop
7475 -- In both cases, the iteration may be over a container in which case it is
7476 -- given by an iterator specification, not a loop parameter specification.
7478 procedure Expand_N_Quantified_Expression (N : Node_Id) is
7479 Loc : constant Source_Ptr := Sloc (N);
7480 Is_Universal : constant Boolean := All_Present (N);
7481 Actions : constant List_Id := New_List;
7482 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
7490 Make_Object_Declaration (Loc,
7491 Defining_Identifier => Tnn,
7492 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
7494 New_Occurrence_Of (Boolean_Literals (Is_Universal), Loc));
7495 Append_To (Actions, Decl);
7497 Cond := Relocate_Node (Condition (N));
7499 if Is_Universal then
7500 Cond := Make_Op_Not (Loc, Cond);
7504 Make_Implicit_If_Statement (N,
7506 Then_Statements => New_List (
7507 Make_Assignment_Statement (Loc,
7508 Name => New_Occurrence_Of (Tnn, Loc),
7510 New_Occurrence_Of (Boolean_Literals (not Is_Universal), Loc)),
7511 Make_Exit_Statement (Loc)));
7513 if Present (Loop_Parameter_Specification (N)) then
7515 Make_Iteration_Scheme (Loc,
7516 Loop_Parameter_Specification =>
7517 Loop_Parameter_Specification (N));
7520 Make_Iteration_Scheme (Loc,
7521 Iterator_Specification => Iterator_Specification (N));
7525 Make_Loop_Statement (Loc,
7526 Iteration_Scheme => I_Scheme,
7527 Statements => New_List (Test),
7528 End_Label => Empty));
7530 -- The components of the scheme have already been analyzed, and the loop
7531 -- parameter declaration has been processed.
7533 Set_Analyzed (Iteration_Scheme (Last (Actions)));
7536 Make_Expression_With_Actions (Loc,
7537 Expression => New_Occurrence_Of (Tnn, Loc),
7538 Actions => Actions));
7540 Analyze_And_Resolve (N, Standard_Boolean);
7541 end Expand_N_Quantified_Expression;
7543 ---------------------------------
7544 -- Expand_N_Selected_Component --
7545 ---------------------------------
7547 -- If the selector is a discriminant of a concurrent object, rewrite the
7548 -- prefix to denote the corresponding record type.
7550 procedure Expand_N_Selected_Component (N : Node_Id) is
7551 Loc : constant Source_Ptr := Sloc (N);
7552 Par : constant Node_Id := Parent (N);
7553 P : constant Node_Id := Prefix (N);
7554 Ptyp : Entity_Id := Underlying_Type (Etype (P));
7560 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
7561 -- Gigi needs a temporary for prefixes that depend on a discriminant,
7562 -- unless the context of an assignment can provide size information.
7563 -- Don't we have a general routine that does this???
7565 -----------------------
7566 -- In_Left_Hand_Side --
7567 -----------------------
7569 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
7571 return (Nkind (Parent (Comp)) = N_Assignment_Statement
7572 and then Comp = Name (Parent (Comp)))
7573 or else (Present (Parent (Comp))
7574 and then Nkind (Parent (Comp)) in N_Subexpr
7575 and then In_Left_Hand_Side (Parent (Comp)));
7576 end In_Left_Hand_Side;
7578 -- Start of processing for Expand_N_Selected_Component
7581 -- Insert explicit dereference if required
7583 if Is_Access_Type (Ptyp) then
7584 Insert_Explicit_Dereference (P);
7585 Analyze_And_Resolve (P, Designated_Type (Ptyp));
7587 if Ekind (Etype (P)) = E_Private_Subtype
7588 and then Is_For_Access_Subtype (Etype (P))
7590 Set_Etype (P, Base_Type (Etype (P)));
7596 -- Deal with discriminant check required
7598 if Do_Discriminant_Check (N) then
7600 -- Present the discriminant checking function to the backend, so that
7601 -- it can inline the call to the function.
7604 (Discriminant_Checking_Func
7605 (Original_Record_Component (Entity (Selector_Name (N)))));
7607 -- Now reset the flag and generate the call
7609 Set_Do_Discriminant_Check (N, False);
7610 Generate_Discriminant_Check (N);
7613 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7614 -- function, then additional actuals must be passed.
7616 if Ada_Version >= Ada_2005
7617 and then Is_Build_In_Place_Function_Call (P)
7619 Make_Build_In_Place_Call_In_Anonymous_Context (P);
7622 -- Gigi cannot handle unchecked conversions that are the prefix of a
7623 -- selected component with discriminants. This must be checked during
7624 -- expansion, because during analysis the type of the selector is not
7625 -- known at the point the prefix is analyzed. If the conversion is the
7626 -- target of an assignment, then we cannot force the evaluation.
7628 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
7629 and then Has_Discriminants (Etype (N))
7630 and then not In_Left_Hand_Side (N)
7632 Force_Evaluation (Prefix (N));
7635 -- Remaining processing applies only if selector is a discriminant
7637 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
7639 -- If the selector is a discriminant of a constrained record type,
7640 -- we may be able to rewrite the expression with the actual value
7641 -- of the discriminant, a useful optimization in some cases.
7643 if Is_Record_Type (Ptyp)
7644 and then Has_Discriminants (Ptyp)
7645 and then Is_Constrained (Ptyp)
7647 -- Do this optimization for discrete types only, and not for
7648 -- access types (access discriminants get us into trouble!)
7650 if not Is_Discrete_Type (Etype (N)) then
7653 -- Don't do this on the left hand of an assignment statement.
7654 -- Normally one would think that references like this would not
7655 -- occur, but they do in generated code, and mean that we really
7656 -- do want to assign the discriminant!
7658 elsif Nkind (Par) = N_Assignment_Statement
7659 and then Name (Par) = N
7663 -- Don't do this optimization for the prefix of an attribute or
7664 -- the name of an object renaming declaration since these are
7665 -- contexts where we do not want the value anyway.
7667 elsif (Nkind (Par) = N_Attribute_Reference
7668 and then Prefix (Par) = N)
7669 or else Is_Renamed_Object (N)
7673 -- Don't do this optimization if we are within the code for a
7674 -- discriminant check, since the whole point of such a check may
7675 -- be to verify the condition on which the code below depends!
7677 elsif Is_In_Discriminant_Check (N) then
7680 -- Green light to see if we can do the optimization. There is
7681 -- still one condition that inhibits the optimization below but
7682 -- now is the time to check the particular discriminant.
7685 -- Loop through discriminants to find the matching discriminant
7686 -- constraint to see if we can copy it.
7688 Disc := First_Discriminant (Ptyp);
7689 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
7690 Discr_Loop : while Present (Dcon) loop
7691 Dval := Node (Dcon);
7693 -- Check if this is the matching discriminant
7695 if Disc = Entity (Selector_Name (N)) then
7697 -- Here we have the matching discriminant. Check for
7698 -- the case of a discriminant of a component that is
7699 -- constrained by an outer discriminant, which cannot
7700 -- be optimized away.
7702 if Denotes_Discriminant
7703 (Dval, Check_Concurrent => True)
7707 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
7709 Denotes_Discriminant
7710 (Selector_Name (Original_Node (Dval)), True)
7714 -- Do not retrieve value if constraint is not static. It
7715 -- is generally not useful, and the constraint may be a
7716 -- rewritten outer discriminant in which case it is in
7719 elsif Is_Entity_Name (Dval)
7720 and then Nkind (Parent (Entity (Dval)))
7721 = N_Object_Declaration
7722 and then Present (Expression (Parent (Entity (Dval))))
7724 not Is_Static_Expression
7725 (Expression (Parent (Entity (Dval))))
7729 -- In the context of a case statement, the expression may
7730 -- have the base type of the discriminant, and we need to
7731 -- preserve the constraint to avoid spurious errors on
7734 elsif Nkind (Parent (N)) = N_Case_Statement
7735 and then Etype (Dval) /= Etype (Disc)
7738 Make_Qualified_Expression (Loc,
7740 New_Occurrence_Of (Etype (Disc), Loc),
7742 New_Copy_Tree (Dval)));
7743 Analyze_And_Resolve (N, Etype (Disc));
7745 -- In case that comes out as a static expression,
7746 -- reset it (a selected component is never static).
7748 Set_Is_Static_Expression (N, False);
7751 -- Otherwise we can just copy the constraint, but the
7752 -- result is certainly not static! In some cases the
7753 -- discriminant constraint has been analyzed in the
7754 -- context of the original subtype indication, but for
7755 -- itypes the constraint might not have been analyzed
7756 -- yet, and this must be done now.
7759 Rewrite (N, New_Copy_Tree (Dval));
7760 Analyze_And_Resolve (N);
7761 Set_Is_Static_Expression (N, False);
7767 Next_Discriminant (Disc);
7768 end loop Discr_Loop;
7770 -- Note: the above loop should always find a matching
7771 -- discriminant, but if it does not, we just missed an
7772 -- optimization due to some glitch (perhaps a previous error),
7778 -- The only remaining processing is in the case of a discriminant of
7779 -- a concurrent object, where we rewrite the prefix to denote the
7780 -- corresponding record type. If the type is derived and has renamed
7781 -- discriminants, use corresponding discriminant, which is the one
7782 -- that appears in the corresponding record.
7784 if not Is_Concurrent_Type (Ptyp) then
7788 Disc := Entity (Selector_Name (N));
7790 if Is_Derived_Type (Ptyp)
7791 and then Present (Corresponding_Discriminant (Disc))
7793 Disc := Corresponding_Discriminant (Disc);
7797 Make_Selected_Component (Loc,
7799 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
7801 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
7806 end Expand_N_Selected_Component;
7808 --------------------
7809 -- Expand_N_Slice --
7810 --------------------
7812 procedure Expand_N_Slice (N : Node_Id) is
7813 Loc : constant Source_Ptr := Sloc (N);
7814 Typ : constant Entity_Id := Etype (N);
7815 Pfx : constant Node_Id := Prefix (N);
7816 Ptp : Entity_Id := Etype (Pfx);
7818 function Is_Procedure_Actual (N : Node_Id) return Boolean;
7819 -- Check whether the argument is an actual for a procedure call, in
7820 -- which case the expansion of a bit-packed slice is deferred until the
7821 -- call itself is expanded. The reason this is required is that we might
7822 -- have an IN OUT or OUT parameter, and the copy out is essential, and
7823 -- that copy out would be missed if we created a temporary here in
7824 -- Expand_N_Slice. Note that we don't bother to test specifically for an
7825 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
7826 -- is harmless to defer expansion in the IN case, since the call
7827 -- processing will still generate the appropriate copy in operation,
7828 -- which will take care of the slice.
7830 procedure Make_Temporary_For_Slice;
7831 -- Create a named variable for the value of the slice, in cases where
7832 -- the back-end cannot handle it properly, e.g. when packed types or
7833 -- unaligned slices are involved.
7835 -------------------------
7836 -- Is_Procedure_Actual --
7837 -------------------------
7839 function Is_Procedure_Actual (N : Node_Id) return Boolean is
7840 Par : Node_Id := Parent (N);
7844 -- If our parent is a procedure call we can return
7846 if Nkind (Par) = N_Procedure_Call_Statement then
7849 -- If our parent is a type conversion, keep climbing the tree,
7850 -- since a type conversion can be a procedure actual. Also keep
7851 -- climbing if parameter association or a qualified expression,
7852 -- since these are additional cases that do can appear on
7853 -- procedure actuals.
7855 elsif Nkind_In (Par, N_Type_Conversion,
7856 N_Parameter_Association,
7857 N_Qualified_Expression)
7859 Par := Parent (Par);
7861 -- Any other case is not what we are looking for
7867 end Is_Procedure_Actual;
7869 ------------------------------
7870 -- Make_Temporary_For_Slice --
7871 ------------------------------
7873 procedure Make_Temporary_For_Slice is
7875 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
7879 Make_Object_Declaration (Loc,
7880 Defining_Identifier => Ent,
7881 Object_Definition => New_Occurrence_Of (Typ, Loc));
7883 Set_No_Initialization (Decl);
7885 Insert_Actions (N, New_List (
7887 Make_Assignment_Statement (Loc,
7888 Name => New_Occurrence_Of (Ent, Loc),
7889 Expression => Relocate_Node (N))));
7891 Rewrite (N, New_Occurrence_Of (Ent, Loc));
7892 Analyze_And_Resolve (N, Typ);
7893 end Make_Temporary_For_Slice;
7895 -- Start of processing for Expand_N_Slice
7898 -- Special handling for access types
7900 if Is_Access_Type (Ptp) then
7902 Ptp := Designated_Type (Ptp);
7905 Make_Explicit_Dereference (Sloc (N),
7906 Prefix => Relocate_Node (Pfx)));
7908 Analyze_And_Resolve (Pfx, Ptp);
7911 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7912 -- function, then additional actuals must be passed.
7914 if Ada_Version >= Ada_2005
7915 and then Is_Build_In_Place_Function_Call (Pfx)
7917 Make_Build_In_Place_Call_In_Anonymous_Context (Pfx);
7920 -- The remaining case to be handled is packed slices. We can leave
7921 -- packed slices as they are in the following situations:
7923 -- 1. Right or left side of an assignment (we can handle this
7924 -- situation correctly in the assignment statement expansion).
7926 -- 2. Prefix of indexed component (the slide is optimized away in this
7927 -- case, see the start of Expand_N_Slice.)
7929 -- 3. Object renaming declaration, since we want the name of the
7930 -- slice, not the value.
7932 -- 4. Argument to procedure call, since copy-in/copy-out handling may
7933 -- be required, and this is handled in the expansion of call
7936 -- 5. Prefix of an address attribute (this is an error which is caught
7937 -- elsewhere, and the expansion would interfere with generating the
7940 if not Is_Packed (Typ) then
7942 -- Apply transformation for actuals of a function call, where
7943 -- Expand_Actuals is not used.
7945 if Nkind (Parent (N)) = N_Function_Call
7946 and then Is_Possibly_Unaligned_Slice (N)
7948 Make_Temporary_For_Slice;
7951 elsif Nkind (Parent (N)) = N_Assignment_Statement
7952 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
7953 and then Parent (N) = Name (Parent (Parent (N))))
7957 elsif Nkind (Parent (N)) = N_Indexed_Component
7958 or else Is_Renamed_Object (N)
7959 or else Is_Procedure_Actual (N)
7963 elsif Nkind (Parent (N)) = N_Attribute_Reference
7964 and then Attribute_Name (Parent (N)) = Name_Address
7969 Make_Temporary_For_Slice;
7973 ------------------------------
7974 -- Expand_N_Type_Conversion --
7975 ------------------------------
7977 procedure Expand_N_Type_Conversion (N : Node_Id) is
7978 Loc : constant Source_Ptr := Sloc (N);
7979 Operand : constant Node_Id := Expression (N);
7980 Target_Type : constant Entity_Id := Etype (N);
7981 Operand_Type : Entity_Id := Etype (Operand);
7983 procedure Handle_Changed_Representation;
7984 -- This is called in the case of record and array type conversions to
7985 -- see if there is a change of representation to be handled. Change of
7986 -- representation is actually handled at the assignment statement level,
7987 -- and what this procedure does is rewrite node N conversion as an
7988 -- assignment to temporary. If there is no change of representation,
7989 -- then the conversion node is unchanged.
7991 procedure Raise_Accessibility_Error;
7992 -- Called when we know that an accessibility check will fail. Rewrites
7993 -- node N to an appropriate raise statement and outputs warning msgs.
7994 -- The Etype of the raise node is set to Target_Type.
7996 procedure Real_Range_Check;
7997 -- Handles generation of range check for real target value
7999 -----------------------------------
8000 -- Handle_Changed_Representation --
8001 -----------------------------------
8003 procedure Handle_Changed_Representation is
8012 -- Nothing else to do if no change of representation
8014 if Same_Representation (Operand_Type, Target_Type) then
8017 -- The real change of representation work is done by the assignment
8018 -- statement processing. So if this type conversion is appearing as
8019 -- the expression of an assignment statement, nothing needs to be
8020 -- done to the conversion.
8022 elsif Nkind (Parent (N)) = N_Assignment_Statement then
8025 -- Otherwise we need to generate a temporary variable, and do the
8026 -- change of representation assignment into that temporary variable.
8027 -- The conversion is then replaced by a reference to this variable.
8032 -- If type is unconstrained we have to add a constraint, copied
8033 -- from the actual value of the left hand side.
8035 if not Is_Constrained (Target_Type) then
8036 if Has_Discriminants (Operand_Type) then
8037 Disc := First_Discriminant (Operand_Type);
8039 if Disc /= First_Stored_Discriminant (Operand_Type) then
8040 Disc := First_Stored_Discriminant (Operand_Type);
8044 while Present (Disc) loop
8046 Make_Selected_Component (Loc,
8047 Prefix => Duplicate_Subexpr_Move_Checks (Operand),
8049 Make_Identifier (Loc, Chars (Disc))));
8050 Next_Discriminant (Disc);
8053 elsif Is_Array_Type (Operand_Type) then
8054 N_Ix := First_Index (Target_Type);
8057 for J in 1 .. Number_Dimensions (Operand_Type) loop
8059 -- We convert the bounds explicitly. We use an unchecked
8060 -- conversion because bounds checks are done elsewhere.
8065 Unchecked_Convert_To (Etype (N_Ix),
8066 Make_Attribute_Reference (Loc,
8068 Duplicate_Subexpr_No_Checks
8069 (Operand, Name_Req => True),
8070 Attribute_Name => Name_First,
8071 Expressions => New_List (
8072 Make_Integer_Literal (Loc, J)))),
8075 Unchecked_Convert_To (Etype (N_Ix),
8076 Make_Attribute_Reference (Loc,
8078 Duplicate_Subexpr_No_Checks
8079 (Operand, Name_Req => True),
8080 Attribute_Name => Name_Last,
8081 Expressions => New_List (
8082 Make_Integer_Literal (Loc, J))))));
8089 Odef := New_Occurrence_Of (Target_Type, Loc);
8091 if Present (Cons) then
8093 Make_Subtype_Indication (Loc,
8094 Subtype_Mark => Odef,
8096 Make_Index_Or_Discriminant_Constraint (Loc,
8097 Constraints => Cons));
8100 Temp := Make_Temporary (Loc, 'C');
8102 Make_Object_Declaration (Loc,
8103 Defining_Identifier => Temp,
8104 Object_Definition => Odef);
8106 Set_No_Initialization (Decl, True);
8108 -- Insert required actions. It is essential to suppress checks
8109 -- since we have suppressed default initialization, which means
8110 -- that the variable we create may have no discriminants.
8115 Make_Assignment_Statement (Loc,
8116 Name => New_Occurrence_Of (Temp, Loc),
8117 Expression => Relocate_Node (N))),
8118 Suppress => All_Checks);
8120 Rewrite (N, New_Occurrence_Of (Temp, Loc));
8123 end Handle_Changed_Representation;
8125 -------------------------------
8126 -- Raise_Accessibility_Error --
8127 -------------------------------
8129 procedure Raise_Accessibility_Error is
8132 Make_Raise_Program_Error (Sloc (N),
8133 Reason => PE_Accessibility_Check_Failed));
8134 Set_Etype (N, Target_Type);
8136 Error_Msg_N ("?accessibility check failure", N);
8138 ("\?& will be raised at run time", N, Standard_Program_Error);
8139 end Raise_Accessibility_Error;
8141 ----------------------
8142 -- Real_Range_Check --
8143 ----------------------
8145 -- Case of conversions to floating-point or fixed-point. If range checks
8146 -- are enabled and the target type has a range constraint, we convert:
8152 -- Tnn : typ'Base := typ'Base (x);
8153 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
8156 -- This is necessary when there is a conversion of integer to float or
8157 -- to fixed-point to ensure that the correct checks are made. It is not
8158 -- necessary for float to float where it is enough to simply set the
8159 -- Do_Range_Check flag.
8161 procedure Real_Range_Check is
8162 Btyp : constant Entity_Id := Base_Type (Target_Type);
8163 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
8164 Hi : constant Node_Id := Type_High_Bound (Target_Type);
8165 Xtyp : constant Entity_Id := Etype (Operand);
8170 -- Nothing to do if conversion was rewritten
8172 if Nkind (N) /= N_Type_Conversion then
8176 -- Nothing to do if range checks suppressed, or target has the same
8177 -- range as the base type (or is the base type).
8179 if Range_Checks_Suppressed (Target_Type)
8180 or else (Lo = Type_Low_Bound (Btyp)
8182 Hi = Type_High_Bound (Btyp))
8187 -- Nothing to do if expression is an entity on which checks have been
8190 if Is_Entity_Name (Operand)
8191 and then Range_Checks_Suppressed (Entity (Operand))
8196 -- Nothing to do if bounds are all static and we can tell that the
8197 -- expression is within the bounds of the target. Note that if the
8198 -- operand is of an unconstrained floating-point type, then we do
8199 -- not trust it to be in range (might be infinite)
8202 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
8203 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
8206 if (not Is_Floating_Point_Type (Xtyp)
8207 or else Is_Constrained (Xtyp))
8208 and then Compile_Time_Known_Value (S_Lo)
8209 and then Compile_Time_Known_Value (S_Hi)
8210 and then Compile_Time_Known_Value (Hi)
8211 and then Compile_Time_Known_Value (Lo)
8214 D_Lov : constant Ureal := Expr_Value_R (Lo);
8215 D_Hiv : constant Ureal := Expr_Value_R (Hi);
8220 if Is_Real_Type (Xtyp) then
8221 S_Lov := Expr_Value_R (S_Lo);
8222 S_Hiv := Expr_Value_R (S_Hi);
8224 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
8225 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
8229 and then S_Lov >= D_Lov
8230 and then S_Hiv <= D_Hiv
8232 Set_Do_Range_Check (Operand, False);
8239 -- For float to float conversions, we are done
8241 if Is_Floating_Point_Type (Xtyp)
8243 Is_Floating_Point_Type (Btyp)
8248 -- Otherwise rewrite the conversion as described above
8250 Conv := Relocate_Node (N);
8251 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
8252 Set_Etype (Conv, Btyp);
8254 -- Enable overflow except for case of integer to float conversions,
8255 -- where it is never required, since we can never have overflow in
8258 if not Is_Integer_Type (Etype (Operand)) then
8259 Enable_Overflow_Check (Conv);
8262 Tnn := Make_Temporary (Loc, 'T', Conv);
8264 Insert_Actions (N, New_List (
8265 Make_Object_Declaration (Loc,
8266 Defining_Identifier => Tnn,
8267 Object_Definition => New_Occurrence_Of (Btyp, Loc),
8268 Constant_Present => True,
8269 Expression => Conv),
8271 Make_Raise_Constraint_Error (Loc,
8276 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8278 Make_Attribute_Reference (Loc,
8279 Attribute_Name => Name_First,
8281 New_Occurrence_Of (Target_Type, Loc))),
8285 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8287 Make_Attribute_Reference (Loc,
8288 Attribute_Name => Name_Last,
8290 New_Occurrence_Of (Target_Type, Loc)))),
8291 Reason => CE_Range_Check_Failed)));
8293 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
8294 Analyze_And_Resolve (N, Btyp);
8295 end Real_Range_Check;
8297 -- Start of processing for Expand_N_Type_Conversion
8300 -- Nothing at all to do if conversion is to the identical type so remove
8301 -- the conversion completely, it is useless, except that it may carry
8302 -- an Assignment_OK attribute, which must be propagated to the operand.
8304 if Operand_Type = Target_Type then
8305 if Assignment_OK (N) then
8306 Set_Assignment_OK (Operand);
8309 Rewrite (N, Relocate_Node (Operand));
8313 -- Nothing to do if this is the second argument of read. This is a
8314 -- "backwards" conversion that will be handled by the specialized code
8315 -- in attribute processing.
8317 if Nkind (Parent (N)) = N_Attribute_Reference
8318 and then Attribute_Name (Parent (N)) = Name_Read
8319 and then Next (First (Expressions (Parent (N)))) = N
8324 -- Check for case of converting to a type that has an invariant
8325 -- associated with it. This required an invariant check. We convert
8331 -- do invariant_check (typ (expr)) in typ (expr);
8333 -- using Duplicate_Subexpr to avoid multiple side effects
8335 -- Note: the Comes_From_Source check, and then the resetting of this
8336 -- flag prevents what would otherwise be an infinite recursion.
8338 if Has_Invariants (Target_Type)
8339 and then Present (Invariant_Procedure (Target_Type))
8340 and then Comes_From_Source (N)
8342 Set_Comes_From_Source (N, False);
8344 Make_Expression_With_Actions (Loc,
8345 Actions => New_List (
8346 Make_Invariant_Call (Duplicate_Subexpr (N))),
8347 Expression => Duplicate_Subexpr_No_Checks (N)));
8348 Analyze_And_Resolve (N, Target_Type);
8352 -- Here if we may need to expand conversion
8354 -- If the operand of the type conversion is an arithmetic operation on
8355 -- signed integers, and the based type of the signed integer type in
8356 -- question is smaller than Standard.Integer, we promote both of the
8357 -- operands to type Integer.
8359 -- For example, if we have
8361 -- target-type (opnd1 + opnd2)
8363 -- and opnd1 and opnd2 are of type short integer, then we rewrite
8366 -- target-type (integer(opnd1) + integer(opnd2))
8368 -- We do this because we are always allowed to compute in a larger type
8369 -- if we do the right thing with the result, and in this case we are
8370 -- going to do a conversion which will do an appropriate check to make
8371 -- sure that things are in range of the target type in any case. This
8372 -- avoids some unnecessary intermediate overflows.
8374 -- We might consider a similar transformation in the case where the
8375 -- target is a real type or a 64-bit integer type, and the operand
8376 -- is an arithmetic operation using a 32-bit integer type. However,
8377 -- we do not bother with this case, because it could cause significant
8378 -- ineffiencies on 32-bit machines. On a 64-bit machine it would be
8379 -- much cheaper, but we don't want different behavior on 32-bit and
8380 -- 64-bit machines. Note that the exclusion of the 64-bit case also
8381 -- handles the configurable run-time cases where 64-bit arithmetic
8382 -- may simply be unavailable.
8384 -- Note: this circuit is partially redundant with respect to the circuit
8385 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
8386 -- the processing here. Also we still need the Checks circuit, since we
8387 -- have to be sure not to generate junk overflow checks in the first
8388 -- place, since it would be trick to remove them here!
8390 if Integer_Promotion_Possible (N) then
8392 -- All conditions met, go ahead with transformation
8400 Make_Type_Conversion (Loc,
8401 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
8402 Expression => Relocate_Node (Right_Opnd (Operand)));
8404 Opnd := New_Op_Node (Nkind (Operand), Loc);
8405 Set_Right_Opnd (Opnd, R);
8407 if Nkind (Operand) in N_Binary_Op then
8409 Make_Type_Conversion (Loc,
8410 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
8411 Expression => Relocate_Node (Left_Opnd (Operand)));
8413 Set_Left_Opnd (Opnd, L);
8417 Make_Type_Conversion (Loc,
8418 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
8419 Expression => Opnd));
8421 Analyze_And_Resolve (N, Target_Type);
8426 -- Do validity check if validity checking operands
8428 if Validity_Checks_On
8429 and then Validity_Check_Operands
8431 Ensure_Valid (Operand);
8434 -- Special case of converting from non-standard boolean type
8436 if Is_Boolean_Type (Operand_Type)
8437 and then (Nonzero_Is_True (Operand_Type))
8439 Adjust_Condition (Operand);
8440 Set_Etype (Operand, Standard_Boolean);
8441 Operand_Type := Standard_Boolean;
8444 -- Case of converting to an access type
8446 if Is_Access_Type (Target_Type) then
8448 -- Apply an accessibility check when the conversion operand is an
8449 -- access parameter (or a renaming thereof), unless conversion was
8450 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
8451 -- Note that other checks may still need to be applied below (such
8452 -- as tagged type checks).
8454 if Is_Entity_Name (Operand)
8456 (Is_Formal (Entity (Operand))
8458 (Present (Renamed_Object (Entity (Operand)))
8459 and then Is_Entity_Name (Renamed_Object (Entity (Operand)))
8461 (Entity (Renamed_Object (Entity (Operand))))))
8462 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
8463 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
8464 or else Attribute_Name (Original_Node (N)) = Name_Access)
8466 Apply_Accessibility_Check
8467 (Operand, Target_Type, Insert_Node => Operand);
8469 -- If the level of the operand type is statically deeper than the
8470 -- level of the target type, then force Program_Error. Note that this
8471 -- can only occur for cases where the attribute is within the body of
8472 -- an instantiation (otherwise the conversion will already have been
8473 -- rejected as illegal). Note: warnings are issued by the analyzer
8474 -- for the instance cases.
8476 elsif In_Instance_Body
8477 and then Type_Access_Level (Operand_Type) >
8478 Type_Access_Level (Target_Type)
8480 Raise_Accessibility_Error;
8482 -- When the operand is a selected access discriminant the check needs
8483 -- to be made against the level of the object denoted by the prefix
8484 -- of the selected name. Force Program_Error for this case as well
8485 -- (this accessibility violation can only happen if within the body
8486 -- of an instantiation).
8488 elsif In_Instance_Body
8489 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
8490 and then Nkind (Operand) = N_Selected_Component
8491 and then Object_Access_Level (Operand) >
8492 Type_Access_Level (Target_Type)
8494 Raise_Accessibility_Error;
8499 -- Case of conversions of tagged types and access to tagged types
8501 -- When needed, that is to say when the expression is class-wide, Add
8502 -- runtime a tag check for (strict) downward conversion by using the
8503 -- membership test, generating:
8505 -- [constraint_error when Operand not in Target_Type'Class]
8507 -- or in the access type case
8509 -- [constraint_error
8510 -- when Operand /= null
8511 -- and then Operand.all not in
8512 -- Designated_Type (Target_Type)'Class]
8514 if (Is_Access_Type (Target_Type)
8515 and then Is_Tagged_Type (Designated_Type (Target_Type)))
8516 or else Is_Tagged_Type (Target_Type)
8518 -- Do not do any expansion in the access type case if the parent is a
8519 -- renaming, since this is an error situation which will be caught by
8520 -- Sem_Ch8, and the expansion can interfere with this error check.
8522 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
8526 -- Otherwise, proceed with processing tagged conversion
8528 Tagged_Conversion : declare
8529 Actual_Op_Typ : Entity_Id;
8530 Actual_Targ_Typ : Entity_Id;
8531 Make_Conversion : Boolean := False;
8532 Root_Op_Typ : Entity_Id;
8534 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
8535 -- Create a membership check to test whether Operand is a member
8536 -- of Targ_Typ. If the original Target_Type is an access, include
8537 -- a test for null value. The check is inserted at N.
8539 --------------------
8540 -- Make_Tag_Check --
8541 --------------------
8543 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
8548 -- [Constraint_Error
8549 -- when Operand /= null
8550 -- and then Operand.all not in Targ_Typ]
8552 if Is_Access_Type (Target_Type) then
8557 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
8558 Right_Opnd => Make_Null (Loc)),
8563 Make_Explicit_Dereference (Loc,
8564 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
8565 Right_Opnd => New_Reference_To (Targ_Typ, Loc)));
8568 -- [Constraint_Error when Operand not in Targ_Typ]
8573 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
8574 Right_Opnd => New_Reference_To (Targ_Typ, Loc));
8578 Make_Raise_Constraint_Error (Loc,
8580 Reason => CE_Tag_Check_Failed));
8583 -- Start of processing for Tagged_Conversion
8586 if Is_Access_Type (Target_Type) then
8588 -- Handle entities from the limited view
8591 Available_View (Designated_Type (Operand_Type));
8593 Available_View (Designated_Type (Target_Type));
8595 Actual_Op_Typ := Operand_Type;
8596 Actual_Targ_Typ := Target_Type;
8599 Root_Op_Typ := Root_Type (Actual_Op_Typ);
8601 -- Ada 2005 (AI-251): Handle interface type conversion
8603 if Is_Interface (Actual_Op_Typ) then
8604 Expand_Interface_Conversion (N, Is_Static => False);
8608 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
8610 -- Create a runtime tag check for a downward class-wide type
8613 if Is_Class_Wide_Type (Actual_Op_Typ)
8614 and then Actual_Op_Typ /= Actual_Targ_Typ
8615 and then Root_Op_Typ /= Actual_Targ_Typ
8616 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ)
8618 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
8619 Make_Conversion := True;
8622 -- AI05-0073: If the result subtype of the function is defined
8623 -- by an access_definition designating a specific tagged type
8624 -- T, a check is made that the result value is null or the tag
8625 -- of the object designated by the result value identifies T.
8626 -- Constraint_Error is raised if this check fails.
8628 if Nkind (Parent (N)) = Sinfo.N_Return_Statement then
8631 Func_Typ : Entity_Id;
8634 -- Climb scope stack looking for the enclosing function
8636 Func := Current_Scope;
8637 while Present (Func)
8638 and then Ekind (Func) /= E_Function
8640 Func := Scope (Func);
8643 -- The function's return subtype must be defined using
8644 -- an access definition.
8646 if Nkind (Result_Definition (Parent (Func))) =
8649 Func_Typ := Directly_Designated_Type (Etype (Func));
8651 -- The return subtype denotes a specific tagged type,
8652 -- in other words, a non class-wide type.
8654 if Is_Tagged_Type (Func_Typ)
8655 and then not Is_Class_Wide_Type (Func_Typ)
8657 Make_Tag_Check (Actual_Targ_Typ);
8658 Make_Conversion := True;
8664 -- We have generated a tag check for either a class-wide type
8665 -- conversion or for AI05-0073.
8667 if Make_Conversion then
8672 Make_Unchecked_Type_Conversion (Loc,
8673 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
8674 Expression => Relocate_Node (Expression (N)));
8676 Analyze_And_Resolve (N, Target_Type);
8680 end Tagged_Conversion;
8682 -- Case of other access type conversions
8684 elsif Is_Access_Type (Target_Type) then
8685 Apply_Constraint_Check (Operand, Target_Type);
8687 -- Case of conversions from a fixed-point type
8689 -- These conversions require special expansion and processing, found in
8690 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
8691 -- since from a semantic point of view, these are simple integer
8692 -- conversions, which do not need further processing.
8694 elsif Is_Fixed_Point_Type (Operand_Type)
8695 and then not Conversion_OK (N)
8697 -- We should never see universal fixed at this case, since the
8698 -- expansion of the constituent divide or multiply should have
8699 -- eliminated the explicit mention of universal fixed.
8701 pragma Assert (Operand_Type /= Universal_Fixed);
8703 -- Check for special case of the conversion to universal real that
8704 -- occurs as a result of the use of a round attribute. In this case,
8705 -- the real type for the conversion is taken from the target type of
8706 -- the Round attribute and the result must be marked as rounded.
8708 if Target_Type = Universal_Real
8709 and then Nkind (Parent (N)) = N_Attribute_Reference
8710 and then Attribute_Name (Parent (N)) = Name_Round
8712 Set_Rounded_Result (N);
8713 Set_Etype (N, Etype (Parent (N)));
8716 -- Otherwise do correct fixed-conversion, but skip these if the
8717 -- Conversion_OK flag is set, because from a semantic point of view
8718 -- these are simple integer conversions needing no further processing
8719 -- (the backend will simply treat them as integers).
8721 if not Conversion_OK (N) then
8722 if Is_Fixed_Point_Type (Etype (N)) then
8723 Expand_Convert_Fixed_To_Fixed (N);
8726 elsif Is_Integer_Type (Etype (N)) then
8727 Expand_Convert_Fixed_To_Integer (N);
8730 pragma Assert (Is_Floating_Point_Type (Etype (N)));
8731 Expand_Convert_Fixed_To_Float (N);
8736 -- Case of conversions to a fixed-point type
8738 -- These conversions require special expansion and processing, found in
8739 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
8740 -- since from a semantic point of view, these are simple integer
8741 -- conversions, which do not need further processing.
8743 elsif Is_Fixed_Point_Type (Target_Type)
8744 and then not Conversion_OK (N)
8746 if Is_Integer_Type (Operand_Type) then
8747 Expand_Convert_Integer_To_Fixed (N);
8750 pragma Assert (Is_Floating_Point_Type (Operand_Type));
8751 Expand_Convert_Float_To_Fixed (N);
8755 -- Case of float-to-integer conversions
8757 -- We also handle float-to-fixed conversions with Conversion_OK set
8758 -- since semantically the fixed-point target is treated as though it
8759 -- were an integer in such cases.
8761 elsif Is_Floating_Point_Type (Operand_Type)
8763 (Is_Integer_Type (Target_Type)
8765 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
8767 -- One more check here, gcc is still not able to do conversions of
8768 -- this type with proper overflow checking, and so gigi is doing an
8769 -- approximation of what is required by doing floating-point compares
8770 -- with the end-point. But that can lose precision in some cases, and
8771 -- give a wrong result. Converting the operand to Universal_Real is
8772 -- helpful, but still does not catch all cases with 64-bit integers
8773 -- on targets with only 64-bit floats.
8775 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
8776 -- Can this code be removed ???
8778 if Do_Range_Check (Operand) then
8780 Make_Type_Conversion (Loc,
8782 New_Occurrence_Of (Universal_Real, Loc),
8784 Relocate_Node (Operand)));
8786 Set_Etype (Operand, Universal_Real);
8787 Enable_Range_Check (Operand);
8788 Set_Do_Range_Check (Expression (Operand), False);
8791 -- Case of array conversions
8793 -- Expansion of array conversions, add required length/range checks but
8794 -- only do this if there is no change of representation. For handling of
8795 -- this case, see Handle_Changed_Representation.
8797 elsif Is_Array_Type (Target_Type) then
8798 if Is_Constrained (Target_Type) then
8799 Apply_Length_Check (Operand, Target_Type);
8801 Apply_Range_Check (Operand, Target_Type);
8804 Handle_Changed_Representation;
8806 -- Case of conversions of discriminated types
8808 -- Add required discriminant checks if target is constrained. Again this
8809 -- change is skipped if we have a change of representation.
8811 elsif Has_Discriminants (Target_Type)
8812 and then Is_Constrained (Target_Type)
8814 Apply_Discriminant_Check (Operand, Target_Type);
8815 Handle_Changed_Representation;
8817 -- Case of all other record conversions. The only processing required
8818 -- is to check for a change of representation requiring the special
8819 -- assignment processing.
8821 elsif Is_Record_Type (Target_Type) then
8823 -- Ada 2005 (AI-216): Program_Error is raised when converting from
8824 -- a derived Unchecked_Union type to an unconstrained type that is
8825 -- not Unchecked_Union if the operand lacks inferable discriminants.
8827 if Is_Derived_Type (Operand_Type)
8828 and then Is_Unchecked_Union (Base_Type (Operand_Type))
8829 and then not Is_Constrained (Target_Type)
8830 and then not Is_Unchecked_Union (Base_Type (Target_Type))
8831 and then not Has_Inferable_Discriminants (Operand)
8833 -- To prevent Gigi from generating illegal code, we generate a
8834 -- Program_Error node, but we give it the target type of the
8838 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
8839 Reason => PE_Unchecked_Union_Restriction);
8842 Set_Etype (PE, Target_Type);
8847 Handle_Changed_Representation;
8850 -- Case of conversions of enumeration types
8852 elsif Is_Enumeration_Type (Target_Type) then
8854 -- Special processing is required if there is a change of
8855 -- representation (from enumeration representation clauses).
8857 if not Same_Representation (Target_Type, Operand_Type) then
8859 -- Convert: x(y) to x'val (ytyp'val (y))
8862 Make_Attribute_Reference (Loc,
8863 Prefix => New_Occurrence_Of (Target_Type, Loc),
8864 Attribute_Name => Name_Val,
8865 Expressions => New_List (
8866 Make_Attribute_Reference (Loc,
8867 Prefix => New_Occurrence_Of (Operand_Type, Loc),
8868 Attribute_Name => Name_Pos,
8869 Expressions => New_List (Operand)))));
8871 Analyze_And_Resolve (N, Target_Type);
8874 -- Case of conversions to floating-point
8876 elsif Is_Floating_Point_Type (Target_Type) then
8880 -- At this stage, either the conversion node has been transformed into
8881 -- some other equivalent expression, or left as a conversion that can be
8882 -- handled by Gigi, in the following cases:
8884 -- Conversions with no change of representation or type
8886 -- Numeric conversions involving integer, floating- and fixed-point
8887 -- values. Fixed-point values are allowed only if Conversion_OK is
8888 -- set, i.e. if the fixed-point values are to be treated as integers.
8890 -- No other conversions should be passed to Gigi
8892 -- Check: are these rules stated in sinfo??? if so, why restate here???
8894 -- The only remaining step is to generate a range check if we still have
8895 -- a type conversion at this stage and Do_Range_Check is set. For now we
8896 -- do this only for conversions of discrete types.
8898 if Nkind (N) = N_Type_Conversion
8899 and then Is_Discrete_Type (Etype (N))
8902 Expr : constant Node_Id := Expression (N);
8907 if Do_Range_Check (Expr)
8908 and then Is_Discrete_Type (Etype (Expr))
8910 Set_Do_Range_Check (Expr, False);
8912 -- Before we do a range check, we have to deal with treating a
8913 -- fixed-point operand as an integer. The way we do this is
8914 -- simply to do an unchecked conversion to an appropriate
8915 -- integer type large enough to hold the result.
8917 -- This code is not active yet, because we are only dealing
8918 -- with discrete types so far ???
8920 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
8921 and then Treat_Fixed_As_Integer (Expr)
8923 Ftyp := Base_Type (Etype (Expr));
8925 if Esize (Ftyp) >= Esize (Standard_Integer) then
8926 Ityp := Standard_Long_Long_Integer;
8928 Ityp := Standard_Integer;
8931 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
8934 -- Reset overflow flag, since the range check will include
8935 -- dealing with possible overflow, and generate the check. If
8936 -- Address is either a source type or target type, suppress
8937 -- range check to avoid typing anomalies when it is a visible
8940 Set_Do_Overflow_Check (N, False);
8941 if not Is_Descendent_Of_Address (Etype (Expr))
8942 and then not Is_Descendent_Of_Address (Target_Type)
8944 Generate_Range_Check
8945 (Expr, Target_Type, CE_Range_Check_Failed);
8951 -- Final step, if the result is a type conversion involving Vax_Float
8952 -- types, then it is subject for further special processing.
8954 if Nkind (N) = N_Type_Conversion
8955 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
8957 Expand_Vax_Conversion (N);
8961 -- Here at end of processing
8964 -- Apply predicate check if required. Note that we can't just call
8965 -- Apply_Predicate_Check here, because the type looks right after
8966 -- the conversion and it would omit the check. The Comes_From_Source
8967 -- guard is necessary to prevent infinite recursions when we generate
8968 -- internal conversions for the purpose of checking predicates.
8970 if Present (Predicate_Function (Target_Type))
8971 and then Target_Type /= Operand_Type
8972 and then Comes_From_Source (N)
8975 Make_Predicate_Check (Target_Type, Duplicate_Subexpr (N)));
8977 end Expand_N_Type_Conversion;
8979 -----------------------------------
8980 -- Expand_N_Unchecked_Expression --
8981 -----------------------------------
8983 -- Remove the unchecked expression node from the tree. Its job was simply
8984 -- to make sure that its constituent expression was handled with checks
8985 -- off, and now that that is done, we can remove it from the tree, and
8986 -- indeed must, since Gigi does not expect to see these nodes.
8988 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
8989 Exp : constant Node_Id := Expression (N);
8991 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
8993 end Expand_N_Unchecked_Expression;
8995 ----------------------------------------
8996 -- Expand_N_Unchecked_Type_Conversion --
8997 ----------------------------------------
8999 -- If this cannot be handled by Gigi and we haven't already made a
9000 -- temporary for it, do it now.
9002 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
9003 Target_Type : constant Entity_Id := Etype (N);
9004 Operand : constant Node_Id := Expression (N);
9005 Operand_Type : constant Entity_Id := Etype (Operand);
9008 -- Nothing at all to do if conversion is to the identical type so remove
9009 -- the conversion completely, it is useless, except that it may carry
9010 -- an Assignment_OK indication which must be propagated to the operand.
9012 if Operand_Type = Target_Type then
9014 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
9016 if Assignment_OK (N) then
9017 Set_Assignment_OK (Operand);
9020 Rewrite (N, Relocate_Node (Operand));
9024 -- If we have a conversion of a compile time known value to a target
9025 -- type and the value is in range of the target type, then we can simply
9026 -- replace the construct by an integer literal of the correct type. We
9027 -- only apply this to integer types being converted. Possibly it may
9028 -- apply in other cases, but it is too much trouble to worry about.
9030 -- Note that we do not do this transformation if the Kill_Range_Check
9031 -- flag is set, since then the value may be outside the expected range.
9032 -- This happens in the Normalize_Scalars case.
9034 -- We also skip this if either the target or operand type is biased
9035 -- because in this case, the unchecked conversion is supposed to
9036 -- preserve the bit pattern, not the integer value.
9038 if Is_Integer_Type (Target_Type)
9039 and then not Has_Biased_Representation (Target_Type)
9040 and then Is_Integer_Type (Operand_Type)
9041 and then not Has_Biased_Representation (Operand_Type)
9042 and then Compile_Time_Known_Value (Operand)
9043 and then not Kill_Range_Check (N)
9046 Val : constant Uint := Expr_Value (Operand);
9049 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
9051 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
9053 Val >= Expr_Value (Type_Low_Bound (Target_Type))
9055 Val <= Expr_Value (Type_High_Bound (Target_Type))
9057 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
9059 -- If Address is the target type, just set the type to avoid a
9060 -- spurious type error on the literal when Address is a visible
9063 if Is_Descendent_Of_Address (Target_Type) then
9064 Set_Etype (N, Target_Type);
9066 Analyze_And_Resolve (N, Target_Type);
9074 -- Nothing to do if conversion is safe
9076 if Safe_Unchecked_Type_Conversion (N) then
9080 -- Otherwise force evaluation unless Assignment_OK flag is set (this
9081 -- flag indicates ??? -- more comments needed here)
9083 if Assignment_OK (N) then
9086 Force_Evaluation (N);
9088 end Expand_N_Unchecked_Type_Conversion;
9090 ----------------------------
9091 -- Expand_Record_Equality --
9092 ----------------------------
9094 -- For non-variant records, Equality is expanded when needed into:
9096 -- and then Lhs.Discr1 = Rhs.Discr1
9098 -- and then Lhs.Discrn = Rhs.Discrn
9099 -- and then Lhs.Cmp1 = Rhs.Cmp1
9101 -- and then Lhs.Cmpn = Rhs.Cmpn
9103 -- The expression is folded by the back-end for adjacent fields. This
9104 -- function is called for tagged record in only one occasion: for imple-
9105 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
9106 -- otherwise the primitive "=" is used directly.
9108 function Expand_Record_Equality
9113 Bodies : List_Id) return Node_Id
9115 Loc : constant Source_Ptr := Sloc (Nod);
9120 First_Time : Boolean := True;
9122 function Suitable_Element (C : Entity_Id) return Entity_Id;
9123 -- Return the first field to compare beginning with C, skipping the
9124 -- inherited components.
9126 ----------------------
9127 -- Suitable_Element --
9128 ----------------------
9130 function Suitable_Element (C : Entity_Id) return Entity_Id is
9135 elsif Ekind (C) /= E_Discriminant
9136 and then Ekind (C) /= E_Component
9138 return Suitable_Element (Next_Entity (C));
9140 elsif Is_Tagged_Type (Typ)
9141 and then C /= Original_Record_Component (C)
9143 return Suitable_Element (Next_Entity (C));
9145 elsif Chars (C) = Name_uController
9146 or else Chars (C) = Name_uTag
9148 return Suitable_Element (Next_Entity (C));
9150 elsif Is_Interface (Etype (C)) then
9151 return Suitable_Element (Next_Entity (C));
9156 end Suitable_Element;
9158 -- Start of processing for Expand_Record_Equality
9161 -- Generates the following code: (assuming that Typ has one Discr and
9162 -- component C2 is also a record)
9165 -- and then Lhs.Discr1 = Rhs.Discr1
9166 -- and then Lhs.C1 = Rhs.C1
9167 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
9169 -- and then Lhs.Cmpn = Rhs.Cmpn
9171 Result := New_Reference_To (Standard_True, Loc);
9172 C := Suitable_Element (First_Entity (Typ));
9173 while Present (C) loop
9181 First_Time := False;
9185 New_Lhs := New_Copy_Tree (Lhs);
9186 New_Rhs := New_Copy_Tree (Rhs);
9190 Expand_Composite_Equality (Nod, Etype (C),
9192 Make_Selected_Component (Loc,
9194 Selector_Name => New_Reference_To (C, Loc)),
9196 Make_Selected_Component (Loc,
9198 Selector_Name => New_Reference_To (C, Loc)),
9201 -- If some (sub)component is an unchecked_union, the whole
9202 -- operation will raise program error.
9204 if Nkind (Check) = N_Raise_Program_Error then
9206 Set_Etype (Result, Standard_Boolean);
9211 Left_Opnd => Result,
9212 Right_Opnd => Check);
9216 C := Suitable_Element (Next_Entity (C));
9220 end Expand_Record_Equality;
9222 -----------------------------------
9223 -- Expand_Short_Circuit_Operator --
9224 -----------------------------------
9226 -- Deal with special expansion if actions are present for the right operand
9227 -- and deal with optimizing case of arguments being True or False. We also
9228 -- deal with the special case of non-standard boolean values.
9230 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
9231 Loc : constant Source_Ptr := Sloc (N);
9232 Typ : constant Entity_Id := Etype (N);
9233 Left : constant Node_Id := Left_Opnd (N);
9234 Right : constant Node_Id := Right_Opnd (N);
9235 LocR : constant Source_Ptr := Sloc (Right);
9238 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
9239 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
9240 -- If Left = Shortcut_Value then Right need not be evaluated
9242 function Make_Test_Expr (Opnd : Node_Id) return Node_Id;
9243 -- For Opnd a boolean expression, return a Boolean expression equivalent
9244 -- to Opnd /= Shortcut_Value.
9246 --------------------
9247 -- Make_Test_Expr --
9248 --------------------
9250 function Make_Test_Expr (Opnd : Node_Id) return Node_Id is
9252 if Shortcut_Value then
9253 return Make_Op_Not (Sloc (Opnd), Opnd);
9260 -- Entity for a temporary variable holding the value of the operator,
9261 -- used for expansion in the case where actions are present.
9263 -- Start of processing for Expand_Short_Circuit_Operator
9266 -- Deal with non-standard booleans
9268 if Is_Boolean_Type (Typ) then
9269 Adjust_Condition (Left);
9270 Adjust_Condition (Right);
9271 Set_Etype (N, Standard_Boolean);
9274 -- Check for cases where left argument is known to be True or False
9276 if Compile_Time_Known_Value (Left) then
9278 -- Mark SCO for left condition as compile time known
9280 if Generate_SCO and then Comes_From_Source (Left) then
9281 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
9284 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
9285 -- Any actions associated with Right will be executed unconditionally
9286 -- and can thus be inserted into the tree unconditionally.
9288 if Expr_Value_E (Left) /= Shortcut_Ent then
9289 if Present (Actions (N)) then
9290 Insert_Actions (N, Actions (N));
9295 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
9296 -- In this case we can forget the actions associated with Right,
9297 -- since they will never be executed.
9300 Kill_Dead_Code (Right);
9301 Kill_Dead_Code (Actions (N));
9302 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
9305 Adjust_Result_Type (N, Typ);
9309 -- If Actions are present for the right operand, we have to do some
9310 -- special processing. We can't just let these actions filter back into
9311 -- code preceding the short circuit (which is what would have happened
9312 -- if we had not trapped them in the short-circuit form), since they
9313 -- must only be executed if the right operand of the short circuit is
9314 -- executed and not otherwise.
9316 -- the temporary variable C.
9318 if Present (Actions (N)) then
9319 Actlist := Actions (N);
9321 -- The old approach is to expand:
9323 -- left AND THEN right
9327 -- C : Boolean := False;
9335 -- and finally rewrite the operator into a reference to C. Similarly
9336 -- for left OR ELSE right, with negated values. Note that this
9337 -- rewrite causes some difficulties for coverage analysis because
9338 -- of the introduction of the new variable C, which obscures the
9339 -- structure of the test.
9341 -- We use this "old approach" if use of N_Expression_With_Actions
9342 -- is False (see description in Opt of when this is or is not set).
9344 if not Use_Expression_With_Actions then
9345 Op_Var := Make_Temporary (Loc, 'C', Related_Node => N);
9348 Make_Object_Declaration (Loc,
9349 Defining_Identifier =>
9351 Object_Definition =>
9352 New_Occurrence_Of (Standard_Boolean, Loc),
9354 New_Occurrence_Of (Shortcut_Ent, Loc)));
9357 Make_Implicit_If_Statement (Right,
9358 Condition => Make_Test_Expr (Right),
9359 Then_Statements => New_List (
9360 Make_Assignment_Statement (LocR,
9361 Name => New_Occurrence_Of (Op_Var, LocR),
9364 (Boolean_Literals (not Shortcut_Value), LocR)))));
9367 Make_Implicit_If_Statement (Left,
9368 Condition => Make_Test_Expr (Left),
9369 Then_Statements => Actlist));
9371 Rewrite (N, New_Occurrence_Of (Op_Var, Loc));
9372 Analyze_And_Resolve (N, Standard_Boolean);
9374 -- The new approach, activated for now by the use of debug flag
9375 -- -gnatd.X is to use the new Expression_With_Actions node for the
9376 -- right operand of the short-circuit form. This should solve the
9377 -- traceability problems for coverage analysis.
9381 Make_Expression_With_Actions (LocR,
9382 Expression => Relocate_Node (Right),
9383 Actions => Actlist));
9384 Set_Actions (N, No_List);
9385 Analyze_And_Resolve (Right, Standard_Boolean);
9388 Adjust_Result_Type (N, Typ);
9392 -- No actions present, check for cases of right argument True/False
9394 if Compile_Time_Known_Value (Right) then
9396 -- Mark SCO for left condition as compile time known
9398 if Generate_SCO and then Comes_From_Source (Right) then
9399 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
9402 -- Change (Left and then True), (Left or else False) to Left.
9403 -- Note that we know there are no actions associated with the right
9404 -- operand, since we just checked for this case above.
9406 if Expr_Value_E (Right) /= Shortcut_Ent then
9409 -- Change (Left and then False), (Left or else True) to Right,
9410 -- making sure to preserve any side effects associated with the Left
9414 Remove_Side_Effects (Left);
9415 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
9419 Adjust_Result_Type (N, Typ);
9420 end Expand_Short_Circuit_Operator;
9422 -------------------------------------
9423 -- Fixup_Universal_Fixed_Operation --
9424 -------------------------------------
9426 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
9427 Conv : constant Node_Id := Parent (N);
9430 -- We must have a type conversion immediately above us
9432 pragma Assert (Nkind (Conv) = N_Type_Conversion);
9434 -- Normally the type conversion gives our target type. The exception
9435 -- occurs in the case of the Round attribute, where the conversion
9436 -- will be to universal real, and our real type comes from the Round
9437 -- attribute (as well as an indication that we must round the result)
9439 if Nkind (Parent (Conv)) = N_Attribute_Reference
9440 and then Attribute_Name (Parent (Conv)) = Name_Round
9442 Set_Etype (N, Etype (Parent (Conv)));
9443 Set_Rounded_Result (N);
9445 -- Normal case where type comes from conversion above us
9448 Set_Etype (N, Etype (Conv));
9450 end Fixup_Universal_Fixed_Operation;
9452 ------------------------------
9453 -- Get_Allocator_Final_List --
9454 ------------------------------
9456 function Get_Allocator_Final_List
9459 PtrT : Entity_Id) return Entity_Id
9461 Loc : constant Source_Ptr := Sloc (N);
9463 Owner : Entity_Id := PtrT;
9464 -- The entity whose finalization list must be used to attach the
9465 -- allocated object.
9468 if Ekind (PtrT) = E_Anonymous_Access_Type then
9470 -- If the context is an access parameter, we need to create a
9471 -- non-anonymous access type in order to have a usable final list,
9472 -- because there is otherwise no pool to which the allocated object
9473 -- can belong. We create both the type and the finalization chain
9474 -- here, because freezing an internal type does not create such a
9475 -- chain. The Final_Chain that is thus created is shared by the
9476 -- access parameter. The access type is tested against the result
9477 -- type of the function to exclude allocators whose type is an
9478 -- anonymous access result type. We freeze the type at once to
9479 -- ensure that it is properly decorated for the back-end, even
9480 -- if the context and current scope is a loop.
9482 if Nkind (Associated_Node_For_Itype (PtrT))
9483 in N_Subprogram_Specification
9486 Etype (Defining_Unit_Name (Associated_Node_For_Itype (PtrT)))
9488 Owner := Make_Temporary (Loc, 'J');
9490 Make_Full_Type_Declaration (Loc,
9491 Defining_Identifier => Owner,
9493 Make_Access_To_Object_Definition (Loc,
9494 Subtype_Indication =>
9495 New_Occurrence_Of (T, Loc))));
9497 Freeze_Before (N, Owner);
9498 Build_Final_List (N, Owner);
9499 Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner));
9501 -- Ada 2005 (AI-318-02): If the context is a return object
9502 -- declaration, then the anonymous return subtype is defined to have
9503 -- the same accessibility level as that of the function's result
9504 -- subtype, which means that we want the scope where the function is
9507 elsif Nkind (Associated_Node_For_Itype (PtrT)) = N_Object_Declaration
9508 and then Ekind (Scope (PtrT)) = E_Return_Statement
9510 Owner := Scope (Return_Applies_To (Scope (PtrT)));
9512 -- Case of an access discriminant, or (Ada 2005) of an anonymous
9513 -- access component or anonymous access function result: find the
9514 -- final list associated with the scope of the type. (In the
9515 -- anonymous access component kind, a list controller will have
9516 -- been allocated when freezing the record type, and PtrT has an
9517 -- Associated_Final_Chain attribute designating it.)
9519 elsif No (Associated_Final_Chain (PtrT)) then
9520 Owner := Scope (PtrT);
9524 return Find_Final_List (Owner);
9525 end Get_Allocator_Final_List;
9527 ---------------------------------
9528 -- Has_Inferable_Discriminants --
9529 ---------------------------------
9531 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
9533 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
9534 -- Determines whether the left-most prefix of a selected component is a
9535 -- formal parameter in a subprogram. Assumes N is a selected component.
9537 --------------------------------
9538 -- Prefix_Is_Formal_Parameter --
9539 --------------------------------
9541 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
9542 Sel_Comp : Node_Id := N;
9545 -- Move to the left-most prefix by climbing up the tree
9547 while Present (Parent (Sel_Comp))
9548 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
9550 Sel_Comp := Parent (Sel_Comp);
9553 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
9554 end Prefix_Is_Formal_Parameter;
9556 -- Start of processing for Has_Inferable_Discriminants
9559 -- For identifiers and indexed components, it is sufficient to have a
9560 -- constrained Unchecked_Union nominal subtype.
9562 if Nkind_In (N, N_Identifier, N_Indexed_Component) then
9563 return Is_Unchecked_Union (Base_Type (Etype (N)))
9565 Is_Constrained (Etype (N));
9567 -- For selected components, the subtype of the selector must be a
9568 -- constrained Unchecked_Union. If the component is subject to a
9569 -- per-object constraint, then the enclosing object must have inferable
9572 elsif Nkind (N) = N_Selected_Component then
9573 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
9575 -- A small hack. If we have a per-object constrained selected
9576 -- component of a formal parameter, return True since we do not
9577 -- know the actual parameter association yet.
9579 if Prefix_Is_Formal_Parameter (N) then
9583 -- Otherwise, check the enclosing object and the selector
9585 return Has_Inferable_Discriminants (Prefix (N))
9587 Has_Inferable_Discriminants (Selector_Name (N));
9590 -- The call to Has_Inferable_Discriminants will determine whether
9591 -- the selector has a constrained Unchecked_Union nominal type.
9593 return Has_Inferable_Discriminants (Selector_Name (N));
9595 -- A qualified expression has inferable discriminants if its subtype
9596 -- mark is a constrained Unchecked_Union subtype.
9598 elsif Nkind (N) = N_Qualified_Expression then
9599 return Is_Unchecked_Union (Subtype_Mark (N))
9601 Is_Constrained (Subtype_Mark (N));
9606 end Has_Inferable_Discriminants;
9608 -------------------------------
9609 -- Insert_Dereference_Action --
9610 -------------------------------
9612 procedure Insert_Dereference_Action (N : Node_Id) is
9613 Loc : constant Source_Ptr := Sloc (N);
9614 Typ : constant Entity_Id := Etype (N);
9615 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
9616 Pnod : constant Node_Id := Parent (N);
9618 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
9619 -- Return true if type of P is derived from Checked_Pool;
9621 -----------------------------
9622 -- Is_Checked_Storage_Pool --
9623 -----------------------------
9625 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
9634 while T /= Etype (T) loop
9635 if Is_RTE (T, RE_Checked_Pool) then
9643 end Is_Checked_Storage_Pool;
9645 -- Start of processing for Insert_Dereference_Action
9648 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
9650 if not (Is_Checked_Storage_Pool (Pool)
9651 and then Comes_From_Source (Original_Node (Pnod)))
9657 Make_Procedure_Call_Statement (Loc,
9658 Name => New_Reference_To (
9659 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
9661 Parameter_Associations => New_List (
9665 New_Reference_To (Pool, Loc),
9667 -- Storage_Address. We use the attribute Pool_Address, which uses
9668 -- the pointer itself to find the address of the object, and which
9669 -- handles unconstrained arrays properly by computing the address
9670 -- of the template. i.e. the correct address of the corresponding
9673 Make_Attribute_Reference (Loc,
9674 Prefix => Duplicate_Subexpr_Move_Checks (N),
9675 Attribute_Name => Name_Pool_Address),
9677 -- Size_In_Storage_Elements
9679 Make_Op_Divide (Loc,
9681 Make_Attribute_Reference (Loc,
9683 Make_Explicit_Dereference (Loc,
9684 Duplicate_Subexpr_Move_Checks (N)),
9685 Attribute_Name => Name_Size),
9687 Make_Integer_Literal (Loc, System_Storage_Unit)),
9691 Make_Attribute_Reference (Loc,
9693 Make_Explicit_Dereference (Loc,
9694 Duplicate_Subexpr_Move_Checks (N)),
9695 Attribute_Name => Name_Alignment))));
9698 when RE_Not_Available =>
9700 end Insert_Dereference_Action;
9702 --------------------------------
9703 -- Integer_Promotion_Possible --
9704 --------------------------------
9706 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
9707 Operand : constant Node_Id := Expression (N);
9708 Operand_Type : constant Entity_Id := Etype (Operand);
9709 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
9712 pragma Assert (Nkind (N) = N_Type_Conversion);
9716 -- We only do the transformation for source constructs. We assume
9717 -- that the expander knows what it is doing when it generates code.
9719 Comes_From_Source (N)
9721 -- If the operand type is Short_Integer or Short_Short_Integer,
9722 -- then we will promote to Integer, which is available on all
9723 -- targets, and is sufficient to ensure no intermediate overflow.
9724 -- Furthermore it is likely to be as efficient or more efficient
9725 -- than using the smaller type for the computation so we do this
9729 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
9731 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
9733 -- Test for interesting operation, which includes addition,
9734 -- division, exponentiation, multiplication, subtraction, absolute
9735 -- value and unary negation. Unary "+" is omitted since it is a
9736 -- no-op and thus can't overflow.
9738 and then Nkind_In (Operand, N_Op_Abs,
9745 end Integer_Promotion_Possible;
9747 ------------------------------
9748 -- Make_Array_Comparison_Op --
9749 ------------------------------
9751 -- This is a hand-coded expansion of the following generic function:
9754 -- type elem is (<>);
9755 -- type index is (<>);
9756 -- type a is array (index range <>) of elem;
9758 -- function Gnnn (X : a; Y: a) return boolean is
9759 -- J : index := Y'first;
9762 -- if X'length = 0 then
9765 -- elsif Y'length = 0 then
9769 -- for I in X'range loop
9770 -- if X (I) = Y (J) then
9771 -- if J = Y'last then
9774 -- J := index'succ (J);
9778 -- return X (I) > Y (J);
9782 -- return X'length > Y'length;
9786 -- Note that since we are essentially doing this expansion by hand, we
9787 -- do not need to generate an actual or formal generic part, just the
9788 -- instantiated function itself.
9790 function Make_Array_Comparison_Op
9792 Nod : Node_Id) return Node_Id
9794 Loc : constant Source_Ptr := Sloc (Nod);
9796 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
9797 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
9798 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
9799 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
9801 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
9803 Loop_Statement : Node_Id;
9804 Loop_Body : Node_Id;
9807 Final_Expr : Node_Id;
9808 Func_Body : Node_Id;
9809 Func_Name : Entity_Id;
9815 -- if J = Y'last then
9818 -- J := index'succ (J);
9822 Make_Implicit_If_Statement (Nod,
9825 Left_Opnd => New_Reference_To (J, Loc),
9827 Make_Attribute_Reference (Loc,
9828 Prefix => New_Reference_To (Y, Loc),
9829 Attribute_Name => Name_Last)),
9831 Then_Statements => New_List (
9832 Make_Exit_Statement (Loc)),
9836 Make_Assignment_Statement (Loc,
9837 Name => New_Reference_To (J, Loc),
9839 Make_Attribute_Reference (Loc,
9840 Prefix => New_Reference_To (Index, Loc),
9841 Attribute_Name => Name_Succ,
9842 Expressions => New_List (New_Reference_To (J, Loc))))));
9844 -- if X (I) = Y (J) then
9847 -- return X (I) > Y (J);
9851 Make_Implicit_If_Statement (Nod,
9855 Make_Indexed_Component (Loc,
9856 Prefix => New_Reference_To (X, Loc),
9857 Expressions => New_List (New_Reference_To (I, Loc))),
9860 Make_Indexed_Component (Loc,
9861 Prefix => New_Reference_To (Y, Loc),
9862 Expressions => New_List (New_Reference_To (J, Loc)))),
9864 Then_Statements => New_List (Inner_If),
9866 Else_Statements => New_List (
9867 Make_Simple_Return_Statement (Loc,
9871 Make_Indexed_Component (Loc,
9872 Prefix => New_Reference_To (X, Loc),
9873 Expressions => New_List (New_Reference_To (I, Loc))),
9876 Make_Indexed_Component (Loc,
9877 Prefix => New_Reference_To (Y, Loc),
9878 Expressions => New_List (
9879 New_Reference_To (J, Loc)))))));
9881 -- for I in X'range loop
9886 Make_Implicit_Loop_Statement (Nod,
9887 Identifier => Empty,
9890 Make_Iteration_Scheme (Loc,
9891 Loop_Parameter_Specification =>
9892 Make_Loop_Parameter_Specification (Loc,
9893 Defining_Identifier => I,
9894 Discrete_Subtype_Definition =>
9895 Make_Attribute_Reference (Loc,
9896 Prefix => New_Reference_To (X, Loc),
9897 Attribute_Name => Name_Range))),
9899 Statements => New_List (Loop_Body));
9901 -- if X'length = 0 then
9903 -- elsif Y'length = 0 then
9906 -- for ... loop ... end loop;
9907 -- return X'length > Y'length;
9911 Make_Attribute_Reference (Loc,
9912 Prefix => New_Reference_To (X, Loc),
9913 Attribute_Name => Name_Length);
9916 Make_Attribute_Reference (Loc,
9917 Prefix => New_Reference_To (Y, Loc),
9918 Attribute_Name => Name_Length);
9922 Left_Opnd => Length1,
9923 Right_Opnd => Length2);
9926 Make_Implicit_If_Statement (Nod,
9930 Make_Attribute_Reference (Loc,
9931 Prefix => New_Reference_To (X, Loc),
9932 Attribute_Name => Name_Length),
9934 Make_Integer_Literal (Loc, 0)),
9938 Make_Simple_Return_Statement (Loc,
9939 Expression => New_Reference_To (Standard_False, Loc))),
9941 Elsif_Parts => New_List (
9942 Make_Elsif_Part (Loc,
9946 Make_Attribute_Reference (Loc,
9947 Prefix => New_Reference_To (Y, Loc),
9948 Attribute_Name => Name_Length),
9950 Make_Integer_Literal (Loc, 0)),
9954 Make_Simple_Return_Statement (Loc,
9955 Expression => New_Reference_To (Standard_True, Loc))))),
9957 Else_Statements => New_List (
9959 Make_Simple_Return_Statement (Loc,
9960 Expression => Final_Expr)));
9964 Formals := New_List (
9965 Make_Parameter_Specification (Loc,
9966 Defining_Identifier => X,
9967 Parameter_Type => New_Reference_To (Typ, Loc)),
9969 Make_Parameter_Specification (Loc,
9970 Defining_Identifier => Y,
9971 Parameter_Type => New_Reference_To (Typ, Loc)));
9973 -- function Gnnn (...) return boolean is
9974 -- J : index := Y'first;
9979 Func_Name := Make_Temporary (Loc, 'G');
9982 Make_Subprogram_Body (Loc,
9984 Make_Function_Specification (Loc,
9985 Defining_Unit_Name => Func_Name,
9986 Parameter_Specifications => Formals,
9987 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
9989 Declarations => New_List (
9990 Make_Object_Declaration (Loc,
9991 Defining_Identifier => J,
9992 Object_Definition => New_Reference_To (Index, Loc),
9994 Make_Attribute_Reference (Loc,
9995 Prefix => New_Reference_To (Y, Loc),
9996 Attribute_Name => Name_First))),
9998 Handled_Statement_Sequence =>
9999 Make_Handled_Sequence_Of_Statements (Loc,
10000 Statements => New_List (If_Stat)));
10003 end Make_Array_Comparison_Op;
10005 ---------------------------
10006 -- Make_Boolean_Array_Op --
10007 ---------------------------
10009 -- For logical operations on boolean arrays, expand in line the following,
10010 -- replacing 'and' with 'or' or 'xor' where needed:
10012 -- function Annn (A : typ; B: typ) return typ is
10015 -- for J in A'range loop
10016 -- C (J) := A (J) op B (J);
10021 -- Here typ is the boolean array type
10023 function Make_Boolean_Array_Op
10025 N : Node_Id) return Node_Id
10027 Loc : constant Source_Ptr := Sloc (N);
10029 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
10030 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
10031 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
10032 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
10040 Func_Name : Entity_Id;
10041 Func_Body : Node_Id;
10042 Loop_Statement : Node_Id;
10046 Make_Indexed_Component (Loc,
10047 Prefix => New_Reference_To (A, Loc),
10048 Expressions => New_List (New_Reference_To (J, Loc)));
10051 Make_Indexed_Component (Loc,
10052 Prefix => New_Reference_To (B, Loc),
10053 Expressions => New_List (New_Reference_To (J, Loc)));
10056 Make_Indexed_Component (Loc,
10057 Prefix => New_Reference_To (C, Loc),
10058 Expressions => New_List (New_Reference_To (J, Loc)));
10060 if Nkind (N) = N_Op_And then
10064 Right_Opnd => B_J);
10066 elsif Nkind (N) = N_Op_Or then
10070 Right_Opnd => B_J);
10076 Right_Opnd => B_J);
10080 Make_Implicit_Loop_Statement (N,
10081 Identifier => Empty,
10083 Iteration_Scheme =>
10084 Make_Iteration_Scheme (Loc,
10085 Loop_Parameter_Specification =>
10086 Make_Loop_Parameter_Specification (Loc,
10087 Defining_Identifier => J,
10088 Discrete_Subtype_Definition =>
10089 Make_Attribute_Reference (Loc,
10090 Prefix => New_Reference_To (A, Loc),
10091 Attribute_Name => Name_Range))),
10093 Statements => New_List (
10094 Make_Assignment_Statement (Loc,
10096 Expression => Op)));
10098 Formals := New_List (
10099 Make_Parameter_Specification (Loc,
10100 Defining_Identifier => A,
10101 Parameter_Type => New_Reference_To (Typ, Loc)),
10103 Make_Parameter_Specification (Loc,
10104 Defining_Identifier => B,
10105 Parameter_Type => New_Reference_To (Typ, Loc)));
10107 Func_Name := Make_Temporary (Loc, 'A');
10108 Set_Is_Inlined (Func_Name);
10111 Make_Subprogram_Body (Loc,
10113 Make_Function_Specification (Loc,
10114 Defining_Unit_Name => Func_Name,
10115 Parameter_Specifications => Formals,
10116 Result_Definition => New_Reference_To (Typ, Loc)),
10118 Declarations => New_List (
10119 Make_Object_Declaration (Loc,
10120 Defining_Identifier => C,
10121 Object_Definition => New_Reference_To (Typ, Loc))),
10123 Handled_Statement_Sequence =>
10124 Make_Handled_Sequence_Of_Statements (Loc,
10125 Statements => New_List (
10127 Make_Simple_Return_Statement (Loc,
10128 Expression => New_Reference_To (C, Loc)))));
10131 end Make_Boolean_Array_Op;
10133 ------------------------
10134 -- Rewrite_Comparison --
10135 ------------------------
10137 procedure Rewrite_Comparison (N : Node_Id) is
10138 Warning_Generated : Boolean := False;
10139 -- Set to True if first pass with Assume_Valid generates a warning in
10140 -- which case we skip the second pass to avoid warning overloaded.
10143 -- Set to Standard_True or Standard_False
10146 if Nkind (N) = N_Type_Conversion then
10147 Rewrite_Comparison (Expression (N));
10150 elsif Nkind (N) not in N_Op_Compare then
10154 -- Now start looking at the comparison in detail. We potentially go
10155 -- through this loop twice. The first time, Assume_Valid is set False
10156 -- in the call to Compile_Time_Compare. If this call results in a
10157 -- clear result of always True or Always False, that's decisive and
10158 -- we are done. Otherwise we repeat the processing with Assume_Valid
10159 -- set to True to generate additional warnings. We can skip that step
10160 -- if Constant_Condition_Warnings is False.
10162 for AV in False .. True loop
10164 Typ : constant Entity_Id := Etype (N);
10165 Op1 : constant Node_Id := Left_Opnd (N);
10166 Op2 : constant Node_Id := Right_Opnd (N);
10168 Res : constant Compare_Result :=
10169 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
10170 -- Res indicates if compare outcome can be compile time determined
10172 True_Result : Boolean;
10173 False_Result : Boolean;
10176 case N_Op_Compare (Nkind (N)) is
10178 True_Result := Res = EQ;
10179 False_Result := Res = LT or else Res = GT or else Res = NE;
10182 True_Result := Res in Compare_GE;
10183 False_Result := Res = LT;
10186 and then Constant_Condition_Warnings
10187 and then Comes_From_Source (Original_Node (N))
10188 and then Nkind (Original_Node (N)) = N_Op_Ge
10189 and then not In_Instance
10190 and then Is_Integer_Type (Etype (Left_Opnd (N)))
10191 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
10194 ("can never be greater than, could replace by ""'=""?", N);
10195 Warning_Generated := True;
10199 True_Result := Res = GT;
10200 False_Result := Res in Compare_LE;
10203 True_Result := Res = LT;
10204 False_Result := Res in Compare_GE;
10207 True_Result := Res in Compare_LE;
10208 False_Result := Res = GT;
10211 and then Constant_Condition_Warnings
10212 and then Comes_From_Source (Original_Node (N))
10213 and then Nkind (Original_Node (N)) = N_Op_Le
10214 and then not In_Instance
10215 and then Is_Integer_Type (Etype (Left_Opnd (N)))
10216 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
10219 ("can never be less than, could replace by ""'=""?", N);
10220 Warning_Generated := True;
10224 True_Result := Res = NE or else Res = GT or else Res = LT;
10225 False_Result := Res = EQ;
10228 -- If this is the first iteration, then we actually convert the
10229 -- comparison into True or False, if the result is certain.
10232 if True_Result or False_Result then
10233 if True_Result then
10234 Result := Standard_True;
10236 Result := Standard_False;
10241 New_Occurrence_Of (Result, Sloc (N))));
10242 Analyze_And_Resolve (N, Typ);
10243 Warn_On_Known_Condition (N);
10247 -- If this is the second iteration (AV = True), and the original
10248 -- node comes from source and we are not in an instance, then give
10249 -- a warning if we know result would be True or False. Note: we
10250 -- know Constant_Condition_Warnings is set if we get here.
10252 elsif Comes_From_Source (Original_Node (N))
10253 and then not In_Instance
10255 if True_Result then
10257 ("condition can only be False if invalid values present?",
10259 elsif False_Result then
10261 ("condition can only be True if invalid values present?",
10267 -- Skip second iteration if not warning on constant conditions or
10268 -- if the first iteration already generated a warning of some kind or
10269 -- if we are in any case assuming all values are valid (so that the
10270 -- first iteration took care of the valid case).
10272 exit when not Constant_Condition_Warnings;
10273 exit when Warning_Generated;
10274 exit when Assume_No_Invalid_Values;
10276 end Rewrite_Comparison;
10278 ----------------------------
10279 -- Safe_In_Place_Array_Op --
10280 ----------------------------
10282 function Safe_In_Place_Array_Op
10285 Op2 : Node_Id) return Boolean
10287 Target : Entity_Id;
10289 function Is_Safe_Operand (Op : Node_Id) return Boolean;
10290 -- Operand is safe if it cannot overlap part of the target of the
10291 -- operation. If the operand and the target are identical, the operand
10292 -- is safe. The operand can be empty in the case of negation.
10294 function Is_Unaliased (N : Node_Id) return Boolean;
10295 -- Check that N is a stand-alone entity
10301 function Is_Unaliased (N : Node_Id) return Boolean is
10305 and then No (Address_Clause (Entity (N)))
10306 and then No (Renamed_Object (Entity (N)));
10309 ---------------------
10310 -- Is_Safe_Operand --
10311 ---------------------
10313 function Is_Safe_Operand (Op : Node_Id) return Boolean is
10318 elsif Is_Entity_Name (Op) then
10319 return Is_Unaliased (Op);
10321 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
10322 return Is_Unaliased (Prefix (Op));
10324 elsif Nkind (Op) = N_Slice then
10326 Is_Unaliased (Prefix (Op))
10327 and then Entity (Prefix (Op)) /= Target;
10329 elsif Nkind (Op) = N_Op_Not then
10330 return Is_Safe_Operand (Right_Opnd (Op));
10335 end Is_Safe_Operand;
10337 -- Start of processing for Is_Safe_In_Place_Array_Op
10340 -- Skip this processing if the component size is different from system
10341 -- storage unit (since at least for NOT this would cause problems).
10343 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
10346 -- Cannot do in place stuff on VM_Target since cannot pass addresses
10348 elsif VM_Target /= No_VM then
10351 -- Cannot do in place stuff if non-standard Boolean representation
10353 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
10356 elsif not Is_Unaliased (Lhs) then
10360 Target := Entity (Lhs);
10361 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
10363 end Safe_In_Place_Array_Op;
10365 -----------------------
10366 -- Tagged_Membership --
10367 -----------------------
10369 -- There are two different cases to consider depending on whether the right
10370 -- operand is a class-wide type or not. If not we just compare the actual
10371 -- tag of the left expr to the target type tag:
10373 -- Left_Expr.Tag = Right_Type'Tag;
10375 -- If it is a class-wide type we use the RT function CW_Membership which is
10376 -- usually implemented by looking in the ancestor tables contained in the
10377 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
10379 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
10380 -- function IW_Membership which is usually implemented by looking in the
10381 -- table of abstract interface types plus the ancestor table contained in
10382 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
10384 procedure Tagged_Membership
10386 SCIL_Node : out Node_Id;
10387 Result : out Node_Id)
10389 Left : constant Node_Id := Left_Opnd (N);
10390 Right : constant Node_Id := Right_Opnd (N);
10391 Loc : constant Source_Ptr := Sloc (N);
10393 Left_Type : Entity_Id;
10394 New_Node : Node_Id;
10395 Right_Type : Entity_Id;
10399 SCIL_Node := Empty;
10401 -- Handle entities from the limited view
10403 Left_Type := Available_View (Etype (Left));
10404 Right_Type := Available_View (Etype (Right));
10406 if Is_Class_Wide_Type (Left_Type) then
10407 Left_Type := Root_Type (Left_Type);
10411 Make_Selected_Component (Loc,
10412 Prefix => Relocate_Node (Left),
10414 New_Reference_To (First_Tag_Component (Left_Type), Loc));
10416 if Is_Class_Wide_Type (Right_Type) then
10418 -- No need to issue a run-time check if we statically know that the
10419 -- result of this membership test is always true. For example,
10420 -- considering the following declarations:
10422 -- type Iface is interface;
10423 -- type T is tagged null record;
10424 -- type DT is new T and Iface with null record;
10429 -- These membership tests are always true:
10432 -- Obj2 in T'Class;
10433 -- Obj2 in Iface'Class;
10435 -- We do not need to handle cases where the membership is illegal.
10438 -- Obj1 in DT'Class; -- Compile time error
10439 -- Obj1 in Iface'Class; -- Compile time error
10441 if not Is_Class_Wide_Type (Left_Type)
10442 and then (Is_Ancestor (Etype (Right_Type), Left_Type)
10443 or else (Is_Interface (Etype (Right_Type))
10444 and then Interface_Present_In_Ancestor
10446 Iface => Etype (Right_Type))))
10448 Result := New_Reference_To (Standard_True, Loc);
10452 -- Ada 2005 (AI-251): Class-wide applied to interfaces
10454 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
10456 -- Support to: "Iface_CW_Typ in Typ'Class"
10458 or else Is_Interface (Left_Type)
10460 -- Issue error if IW_Membership operation not available in a
10461 -- configurable run time setting.
10463 if not RTE_Available (RE_IW_Membership) then
10465 ("dynamic membership test on interface types", N);
10471 Make_Function_Call (Loc,
10472 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
10473 Parameter_Associations => New_List (
10474 Make_Attribute_Reference (Loc,
10476 Attribute_Name => Name_Address),
10479 (Access_Disp_Table (Root_Type (Right_Type)))),
10482 -- Ada 95: Normal case
10485 Build_CW_Membership (Loc,
10486 Obj_Tag_Node => Obj_Tag,
10490 (Access_Disp_Table (Root_Type (Right_Type)))),
10493 New_Node => New_Node);
10495 -- Generate the SCIL node for this class-wide membership test.
10496 -- Done here because the previous call to Build_CW_Membership
10497 -- relocates Obj_Tag.
10499 if Generate_SCIL then
10500 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
10501 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
10502 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
10505 Result := New_Node;
10508 -- Right_Type is not a class-wide type
10511 -- No need to check the tag of the object if Right_Typ is abstract
10513 if Is_Abstract_Type (Right_Type) then
10514 Result := New_Reference_To (Standard_False, Loc);
10519 Left_Opnd => Obj_Tag,
10522 (Node (First_Elmt (Access_Disp_Table (Right_Type))), Loc));
10525 end Tagged_Membership;
10527 ------------------------------
10528 -- Unary_Op_Validity_Checks --
10529 ------------------------------
10531 procedure Unary_Op_Validity_Checks (N : Node_Id) is
10533 if Validity_Checks_On and Validity_Check_Operands then
10534 Ensure_Valid (Right_Opnd (N));
10536 end Unary_Op_Validity_Checks;