1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Aggr; use Exp_Aggr;
33 with Exp_Atag; use Exp_Atag;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Fixd; use Exp_Fixd;
40 with Exp_Pakd; use Exp_Pakd;
41 with Exp_Tss; use Exp_Tss;
42 with Exp_Util; use Exp_Util;
43 with Exp_VFpt; use Exp_VFpt;
44 with Freeze; use Freeze;
45 with Inline; use Inline;
46 with Namet; use Namet;
47 with Nlists; use Nlists;
48 with Nmake; use Nmake;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Res; use Sem_Res;
61 with Sem_SCIL; use Sem_SCIL;
62 with Sem_Type; use Sem_Type;
63 with Sem_Util; use Sem_Util;
64 with Sem_Warn; use Sem_Warn;
65 with Sinfo; use Sinfo;
66 with Snames; use Snames;
67 with Stand; use Stand;
68 with Targparm; use Targparm;
69 with Tbuild; use Tbuild;
70 with Ttypes; use Ttypes;
71 with Uintp; use Uintp;
72 with Urealp; use Urealp;
73 with Validsw; use Validsw;
75 package body Exp_Ch4 is
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
81 procedure Binary_Op_Validity_Checks (N : Node_Id);
82 pragma Inline (Binary_Op_Validity_Checks);
83 -- Performs validity checks for a binary operator
85 procedure Build_Boolean_Array_Proc_Call
89 -- If a boolean array assignment can be done in place, build call to
90 -- corresponding library procedure.
92 procedure Displace_Allocator_Pointer (N : Node_Id);
93 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
94 -- Expand_Allocator_Expression. Allocating class-wide interface objects
95 -- this routine displaces the pointer to the allocated object to reference
96 -- the component referencing the corresponding secondary dispatch table.
98 procedure Expand_Allocator_Expression (N : Node_Id);
99 -- Subsidiary to Expand_N_Allocator, for the case when the expression
100 -- is a qualified expression or an aggregate.
102 procedure Expand_Array_Comparison (N : Node_Id);
103 -- This routine handles expansion of the comparison operators (N_Op_Lt,
104 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
105 -- code for these operators is similar, differing only in the details of
106 -- the actual comparison call that is made. Special processing (call a
109 function Expand_Array_Equality
114 Typ : Entity_Id) return Node_Id;
115 -- Expand an array equality into a call to a function implementing this
116 -- equality, and a call to it. Loc is the location for the generated nodes.
117 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
118 -- on which to attach bodies of local functions that are created in the
119 -- process. It is the responsibility of the caller to insert those bodies
120 -- at the right place. Nod provides the Sloc value for the generated code.
121 -- Normally the types used for the generated equality routine are taken
122 -- from Lhs and Rhs. However, in some situations of generated code, the
123 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
124 -- the type to be used for the formal parameters.
126 procedure Expand_Boolean_Operator (N : Node_Id);
127 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
128 -- case of array type arguments.
130 function Expand_Composite_Equality
135 Bodies : List_Id) return Node_Id;
136 -- Local recursive function used to expand equality for nested composite
137 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
138 -- to attach bodies of local functions that are created in the process.
139 -- This is the responsibility of the caller to insert those bodies at the
140 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
141 -- are the left and right sides for the comparison, and Typ is the type of
142 -- the arrays to compare.
144 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
145 -- Routine to expand concatenation of a sequence of two or more operands
146 -- (in the list Operands) and replace node Cnode with the result of the
147 -- concatenation. The operands can be of any appropriate type, and can
148 -- include both arrays and singleton elements.
150 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
151 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
152 -- fixed. We do not have such a type at runtime, so the purpose of this
153 -- routine is to find the real type by looking up the tree. We also
154 -- determine if the operation must be rounded.
156 function Get_Allocator_Final_List
159 PtrT : Entity_Id) return Entity_Id;
160 -- If the designated type is controlled, build final_list expression for
161 -- created object. If context is an access parameter, create a local access
162 -- type to have a usable finalization list.
164 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
165 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
166 -- discriminants if it has a constrained nominal type, unless the object
167 -- is a component of an enclosing Unchecked_Union object that is subject
168 -- to a per-object constraint and the enclosing object lacks inferable
171 -- An expression of an Unchecked_Union type has inferable discriminants
172 -- if it is either a name of an object with inferable discriminants or a
173 -- qualified expression whose subtype mark denotes a constrained subtype.
175 procedure Insert_Dereference_Action (N : Node_Id);
176 -- N is an expression whose type is an access. When the type of the
177 -- associated storage pool is derived from Checked_Pool, generate a
178 -- call to the 'Dereference' primitive operation.
180 function Make_Array_Comparison_Op
182 Nod : Node_Id) return Node_Id;
183 -- Comparisons between arrays are expanded in line. This function produces
184 -- the body of the implementation of (a > b), where a and b are one-
185 -- dimensional arrays of some discrete type. The original node is then
186 -- expanded into the appropriate call to this function. Nod provides the
187 -- Sloc value for the generated code.
189 function Make_Boolean_Array_Op
191 N : Node_Id) return Node_Id;
192 -- Boolean operations on boolean arrays are expanded in line. This function
193 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
194 -- b). It is used only the normal case and not the packed case. The type
195 -- involved, Typ, is the Boolean array type, and the logical operations in
196 -- the body are simple boolean operations. Note that Typ is always a
197 -- constrained type (the caller has ensured this by using
198 -- Convert_To_Actual_Subtype if necessary).
200 procedure Rewrite_Comparison (N : Node_Id);
201 -- If N is the node for a comparison whose outcome can be determined at
202 -- compile time, then the node N can be rewritten with True or False. If
203 -- the outcome cannot be determined at compile time, the call has no
204 -- effect. If N is a type conversion, then this processing is applied to
205 -- its expression. If N is neither comparison nor a type conversion, the
206 -- call has no effect.
208 function Tagged_Membership (N : Node_Id) return Node_Id;
209 -- Construct the expression corresponding to the tagged membership test.
210 -- Deals with a second operand being (or not) a class-wide type.
212 function Safe_In_Place_Array_Op
215 Op2 : Node_Id) return Boolean;
216 -- In the context of an assignment, where the right-hand side is a boolean
217 -- operation on arrays, check whether operation can be performed in place.
219 procedure Unary_Op_Validity_Checks (N : Node_Id);
220 pragma Inline (Unary_Op_Validity_Checks);
221 -- Performs validity checks for a unary operator
223 -------------------------------
224 -- Binary_Op_Validity_Checks --
225 -------------------------------
227 procedure Binary_Op_Validity_Checks (N : Node_Id) is
229 if Validity_Checks_On and Validity_Check_Operands then
230 Ensure_Valid (Left_Opnd (N));
231 Ensure_Valid (Right_Opnd (N));
233 end Binary_Op_Validity_Checks;
235 ------------------------------------
236 -- Build_Boolean_Array_Proc_Call --
237 ------------------------------------
239 procedure Build_Boolean_Array_Proc_Call
244 Loc : constant Source_Ptr := Sloc (N);
245 Kind : constant Node_Kind := Nkind (Expression (N));
246 Target : constant Node_Id :=
247 Make_Attribute_Reference (Loc,
249 Attribute_Name => Name_Address);
251 Arg1 : constant Node_Id := Op1;
252 Arg2 : Node_Id := Op2;
254 Proc_Name : Entity_Id;
257 if Kind = N_Op_Not then
258 if Nkind (Op1) in N_Binary_Op then
260 -- Use negated version of the binary operators
262 if Nkind (Op1) = N_Op_And then
263 Proc_Name := RTE (RE_Vector_Nand);
265 elsif Nkind (Op1) = N_Op_Or then
266 Proc_Name := RTE (RE_Vector_Nor);
268 else pragma Assert (Nkind (Op1) = N_Op_Xor);
269 Proc_Name := RTE (RE_Vector_Xor);
273 Make_Procedure_Call_Statement (Loc,
274 Name => New_Occurrence_Of (Proc_Name, Loc),
276 Parameter_Associations => New_List (
278 Make_Attribute_Reference (Loc,
279 Prefix => Left_Opnd (Op1),
280 Attribute_Name => Name_Address),
282 Make_Attribute_Reference (Loc,
283 Prefix => Right_Opnd (Op1),
284 Attribute_Name => Name_Address),
286 Make_Attribute_Reference (Loc,
287 Prefix => Left_Opnd (Op1),
288 Attribute_Name => Name_Length)));
291 Proc_Name := RTE (RE_Vector_Not);
294 Make_Procedure_Call_Statement (Loc,
295 Name => New_Occurrence_Of (Proc_Name, Loc),
296 Parameter_Associations => New_List (
299 Make_Attribute_Reference (Loc,
301 Attribute_Name => Name_Address),
303 Make_Attribute_Reference (Loc,
305 Attribute_Name => Name_Length)));
309 -- We use the following equivalences:
311 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
312 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
313 -- (not X) xor (not Y) = X xor Y
314 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
316 if Nkind (Op1) = N_Op_Not then
317 if Kind = N_Op_And then
318 Proc_Name := RTE (RE_Vector_Nor);
320 elsif Kind = N_Op_Or then
321 Proc_Name := RTE (RE_Vector_Nand);
324 Proc_Name := RTE (RE_Vector_Xor);
328 if Kind = N_Op_And then
329 Proc_Name := RTE (RE_Vector_And);
331 elsif Kind = N_Op_Or then
332 Proc_Name := RTE (RE_Vector_Or);
334 elsif Nkind (Op2) = N_Op_Not then
335 Proc_Name := RTE (RE_Vector_Nxor);
336 Arg2 := Right_Opnd (Op2);
339 Proc_Name := RTE (RE_Vector_Xor);
344 Make_Procedure_Call_Statement (Loc,
345 Name => New_Occurrence_Of (Proc_Name, Loc),
346 Parameter_Associations => New_List (
348 Make_Attribute_Reference (Loc,
350 Attribute_Name => Name_Address),
351 Make_Attribute_Reference (Loc,
353 Attribute_Name => Name_Address),
354 Make_Attribute_Reference (Loc,
356 Attribute_Name => Name_Length)));
359 Rewrite (N, Call_Node);
363 when RE_Not_Available =>
365 end Build_Boolean_Array_Proc_Call;
367 --------------------------------
368 -- Displace_Allocator_Pointer --
369 --------------------------------
371 procedure Displace_Allocator_Pointer (N : Node_Id) is
372 Loc : constant Source_Ptr := Sloc (N);
373 Orig_Node : constant Node_Id := Original_Node (N);
379 -- Do nothing in case of VM targets: the virtual machine will handle
380 -- interfaces directly.
382 if not Tagged_Type_Expansion then
386 pragma Assert (Nkind (N) = N_Identifier
387 and then Nkind (Orig_Node) = N_Allocator);
389 PtrT := Etype (Orig_Node);
390 Dtyp := Available_View (Designated_Type (PtrT));
391 Etyp := Etype (Expression (Orig_Node));
393 if Is_Class_Wide_Type (Dtyp)
394 and then Is_Interface (Dtyp)
396 -- If the type of the allocator expression is not an interface type
397 -- we can generate code to reference the record component containing
398 -- the pointer to the secondary dispatch table.
400 if not Is_Interface (Etyp) then
402 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
405 -- 1) Get access to the allocated object
408 Make_Explicit_Dereference (Loc,
413 -- 2) Add the conversion to displace the pointer to reference
414 -- the secondary dispatch table.
416 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
417 Analyze_And_Resolve (N, Dtyp);
419 -- 3) The 'access to the secondary dispatch table will be used
420 -- as the value returned by the allocator.
423 Make_Attribute_Reference (Loc,
424 Prefix => Relocate_Node (N),
425 Attribute_Name => Name_Access));
426 Set_Etype (N, Saved_Typ);
430 -- If the type of the allocator expression is an interface type we
431 -- generate a run-time call to displace "this" to reference the
432 -- component containing the pointer to the secondary dispatch table
433 -- or else raise Constraint_Error if the actual object does not
434 -- implement the target interface. This case corresponds with the
435 -- following example:
437 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
439 -- return new Iface_2'Class'(Obj);
444 Unchecked_Convert_To (PtrT,
445 Make_Function_Call (Loc,
446 Name => New_Reference_To (RTE (RE_Displace), Loc),
447 Parameter_Associations => New_List (
448 Unchecked_Convert_To (RTE (RE_Address),
454 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
456 Analyze_And_Resolve (N, PtrT);
459 end Displace_Allocator_Pointer;
461 ---------------------------------
462 -- Expand_Allocator_Expression --
463 ---------------------------------
465 procedure Expand_Allocator_Expression (N : Node_Id) is
466 Loc : constant Source_Ptr := Sloc (N);
467 Exp : constant Node_Id := Expression (Expression (N));
468 PtrT : constant Entity_Id := Etype (N);
469 DesigT : constant Entity_Id := Designated_Type (PtrT);
471 procedure Apply_Accessibility_Check
473 Built_In_Place : Boolean := False);
474 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
475 -- type, generate an accessibility check to verify that the level of the
476 -- type of the created object is not deeper than the level of the access
477 -- type. If the type of the qualified expression is class- wide, then
478 -- always generate the check (except in the case where it is known to be
479 -- unnecessary, see comment below). Otherwise, only generate the check
480 -- if the level of the qualified expression type is statically deeper
481 -- than the access type.
483 -- Although the static accessibility will generally have been performed
484 -- as a legality check, it won't have been done in cases where the
485 -- allocator appears in generic body, so a run-time check is needed in
486 -- general. One special case is when the access type is declared in the
487 -- same scope as the class-wide allocator, in which case the check can
488 -- never fail, so it need not be generated.
490 -- As an open issue, there seem to be cases where the static level
491 -- associated with the class-wide object's underlying type is not
492 -- sufficient to perform the proper accessibility check, such as for
493 -- allocators in nested subprograms or accept statements initialized by
494 -- class-wide formals when the actual originates outside at a deeper
495 -- static level. The nested subprogram case might require passing
496 -- accessibility levels along with class-wide parameters, and the task
497 -- case seems to be an actual gap in the language rules that needs to
498 -- be fixed by the ARG. ???
500 -------------------------------
501 -- Apply_Accessibility_Check --
502 -------------------------------
504 procedure Apply_Accessibility_Check
506 Built_In_Place : Boolean := False)
511 -- Note: we skip the accessibility check for the VM case, since
512 -- there does not seem to be any practical way of implementing it.
514 if Ada_Version >= Ada_05
515 and then Tagged_Type_Expansion
516 and then Is_Class_Wide_Type (DesigT)
517 and then not Scope_Suppress (Accessibility_Check)
519 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
521 (Is_Class_Wide_Type (Etype (Exp))
522 and then Scope (PtrT) /= Current_Scope))
524 -- If the allocator was built in place Ref is already a reference
525 -- to the access object initialized to the result of the allocator
526 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise
527 -- it is the entity associated with the object containing the
528 -- address of the allocated object.
530 if Built_In_Place then
531 Ref_Node := New_Copy (Ref);
533 Ref_Node := New_Reference_To (Ref, Loc);
537 Make_Raise_Program_Error (Loc,
541 Build_Get_Access_Level (Loc,
542 Make_Attribute_Reference (Loc,
544 Attribute_Name => Name_Tag)),
546 Make_Integer_Literal (Loc,
547 Type_Access_Level (PtrT))),
548 Reason => PE_Accessibility_Check_Failed));
550 end Apply_Accessibility_Check;
554 Indic : constant Node_Id := Subtype_Mark (Expression (N));
555 T : constant Entity_Id := Entity (Indic);
560 TagT : Entity_Id := Empty;
561 -- Type used as source for tag assignment
563 TagR : Node_Id := Empty;
564 -- Target reference for tag assignment
566 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
568 Tag_Assign : Node_Id;
571 -- Start of processing for Expand_Allocator_Expression
574 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
576 if Is_CPP_Constructor_Call (Exp) then
579 -- Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn
581 -- Allocate the object with no expression
583 Node := Relocate_Node (N);
584 Set_Expression (Node, New_Reference_To (Etype (Exp), Loc));
586 -- Avoid its expansion to avoid generating a call to the default
591 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
594 Make_Object_Declaration (Loc,
595 Defining_Identifier => Temp,
596 Constant_Present => True,
597 Object_Definition => New_Reference_To (PtrT, Loc),
598 Expression => Node));
600 Apply_Accessibility_Check (Temp);
602 -- Locate the enclosing list and insert the C++ constructor call
609 while not Is_List_Member (P) loop
613 Insert_List_After_And_Analyze (P,
614 Build_Initialization_Call (Loc,
616 Make_Explicit_Dereference (Loc,
617 Prefix => New_Reference_To (Temp, Loc)),
619 Constructor_Ref => Exp));
622 Rewrite (N, New_Reference_To (Temp, Loc));
623 Analyze_And_Resolve (N, PtrT);
627 -- Ada 2005 (AI-318-02): If the initialization expression is a call
628 -- to a build-in-place function, then access to the allocated object
629 -- must be passed to the function. Currently we limit such functions
630 -- to those with constrained limited result subtypes, but eventually
631 -- we plan to expand the allowed forms of functions that are treated
632 -- as build-in-place.
634 if Ada_Version >= Ada_05
635 and then Is_Build_In_Place_Function_Call (Exp)
637 Make_Build_In_Place_Call_In_Allocator (N, Exp);
638 Apply_Accessibility_Check (N, Built_In_Place => True);
642 -- Actions inserted before:
643 -- Temp : constant ptr_T := new T'(Expression);
644 -- <no CW> Temp._tag := T'tag;
645 -- <CTRL> Adjust (Finalizable (Temp.all));
646 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
648 -- We analyze by hand the new internal allocator to avoid
649 -- any recursion and inappropriate call to Initialize
651 -- We don't want to remove side effects when the expression must be
652 -- built in place. In the case of a build-in-place function call,
653 -- that could lead to a duplication of the call, which was already
654 -- substituted for the allocator.
656 if not Aggr_In_Place then
657 Remove_Side_Effects (Exp);
661 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
663 -- For a class wide allocation generate the following code:
665 -- type Equiv_Record is record ... end record;
666 -- implicit subtype CW is <Class_Wide_Subytpe>;
667 -- temp : PtrT := new CW'(CW!(expr));
669 if Is_Class_Wide_Type (T) then
670 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
672 -- Ada 2005 (AI-251): If the expression is a class-wide interface
673 -- object we generate code to move up "this" to reference the
674 -- base of the object before allocating the new object.
676 -- Note that Exp'Address is recursively expanded into a call
677 -- to Base_Address (Exp.Tag)
679 if Is_Class_Wide_Type (Etype (Exp))
680 and then Is_Interface (Etype (Exp))
681 and then Tagged_Type_Expansion
685 Unchecked_Convert_To (Entity (Indic),
686 Make_Explicit_Dereference (Loc,
687 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
688 Make_Attribute_Reference (Loc,
690 Attribute_Name => Name_Address)))));
695 Unchecked_Convert_To (Entity (Indic), Exp));
698 Analyze_And_Resolve (Expression (N), Entity (Indic));
701 -- Keep separate the management of allocators returning interfaces
703 if not Is_Interface (Directly_Designated_Type (PtrT)) then
704 if Aggr_In_Place then
706 Make_Object_Declaration (Loc,
707 Defining_Identifier => Temp,
708 Object_Definition => New_Reference_To (PtrT, Loc),
711 New_Reference_To (Etype (Exp), Loc)));
713 -- Copy the Comes_From_Source flag for the allocator we just
714 -- built, since logically this allocator is a replacement of
715 -- the original allocator node. This is for proper handling of
716 -- restriction No_Implicit_Heap_Allocations.
718 Set_Comes_From_Source
719 (Expression (Tmp_Node), Comes_From_Source (N));
721 Set_No_Initialization (Expression (Tmp_Node));
722 Insert_Action (N, Tmp_Node);
724 if Needs_Finalization (T)
725 and then Ekind (PtrT) = E_Anonymous_Access_Type
727 -- Create local finalization list for access parameter
729 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
732 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
735 Node := Relocate_Node (N);
738 Make_Object_Declaration (Loc,
739 Defining_Identifier => Temp,
740 Constant_Present => True,
741 Object_Definition => New_Reference_To (PtrT, Loc),
742 Expression => Node));
745 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
746 -- interface type. In this case we use the type of the qualified
747 -- expression to allocate the object.
751 Def_Id : constant Entity_Id :=
752 Make_Defining_Identifier (Loc,
753 New_Internal_Name ('T'));
758 Make_Full_Type_Declaration (Loc,
759 Defining_Identifier => Def_Id,
761 Make_Access_To_Object_Definition (Loc,
763 Null_Exclusion_Present => False,
764 Constant_Present => False,
765 Subtype_Indication =>
766 New_Reference_To (Etype (Exp), Loc)));
768 Insert_Action (N, New_Decl);
770 -- Inherit the final chain to ensure that the expansion of the
771 -- aggregate is correct in case of controlled types
773 if Needs_Finalization (Directly_Designated_Type (PtrT)) then
774 Set_Associated_Final_Chain (Def_Id,
775 Associated_Final_Chain (PtrT));
778 -- Declare the object using the previous type declaration
780 if Aggr_In_Place then
782 Make_Object_Declaration (Loc,
783 Defining_Identifier => Temp,
784 Object_Definition => New_Reference_To (Def_Id, Loc),
787 New_Reference_To (Etype (Exp), Loc)));
789 -- Copy the Comes_From_Source flag for the allocator we just
790 -- built, since logically this allocator is a replacement of
791 -- the original allocator node. This is for proper handling
792 -- of restriction No_Implicit_Heap_Allocations.
794 Set_Comes_From_Source
795 (Expression (Tmp_Node), Comes_From_Source (N));
797 Set_No_Initialization (Expression (Tmp_Node));
798 Insert_Action (N, Tmp_Node);
800 if Needs_Finalization (T)
801 and then Ekind (PtrT) = E_Anonymous_Access_Type
803 -- Create local finalization list for access parameter
806 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
809 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
811 Node := Relocate_Node (N);
814 Make_Object_Declaration (Loc,
815 Defining_Identifier => Temp,
816 Constant_Present => True,
817 Object_Definition => New_Reference_To (Def_Id, Loc),
818 Expression => Node));
821 -- Generate an additional object containing the address of the
822 -- returned object. The type of this second object declaration
823 -- is the correct type required for the common processing that
824 -- is still performed by this subprogram. The displacement of
825 -- this pointer to reference the component associated with the
826 -- interface type will be done at the end of common processing.
829 Make_Object_Declaration (Loc,
830 Defining_Identifier => Make_Defining_Identifier (Loc,
831 New_Internal_Name ('P')),
832 Object_Definition => New_Reference_To (PtrT, Loc),
833 Expression => Unchecked_Convert_To (PtrT,
834 New_Reference_To (Temp, Loc)));
836 Insert_Action (N, New_Decl);
838 Tmp_Node := New_Decl;
839 Temp := Defining_Identifier (New_Decl);
843 Apply_Accessibility_Check (Temp);
845 -- Generate the tag assignment
847 -- Suppress the tag assignment when VM_Target because VM tags are
848 -- represented implicitly in objects.
850 if not Tagged_Type_Expansion then
853 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
854 -- interface objects because in this case the tag does not change.
856 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
857 pragma Assert (Is_Class_Wide_Type
858 (Directly_Designated_Type (Etype (N))));
861 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
863 TagR := New_Reference_To (Temp, Loc);
865 elsif Is_Private_Type (T)
866 and then Is_Tagged_Type (Underlying_Type (T))
868 TagT := Underlying_Type (T);
870 Unchecked_Convert_To (Underlying_Type (T),
871 Make_Explicit_Dereference (Loc,
872 Prefix => New_Reference_To (Temp, Loc)));
875 if Present (TagT) then
877 Make_Assignment_Statement (Loc,
879 Make_Selected_Component (Loc,
882 New_Reference_To (First_Tag_Component (TagT), Loc)),
885 Unchecked_Convert_To (RTE (RE_Tag),
887 (Elists.Node (First_Elmt (Access_Disp_Table (TagT))),
890 -- The previous assignment has to be done in any case
892 Set_Assignment_OK (Name (Tag_Assign));
893 Insert_Action (N, Tag_Assign);
896 if Needs_Finalization (DesigT)
897 and then Needs_Finalization (T)
901 Apool : constant Entity_Id :=
902 Associated_Storage_Pool (PtrT);
905 -- If it is an allocation on the secondary stack (i.e. a value
906 -- returned from a function), the object is attached on the
907 -- caller side as soon as the call is completed (see
908 -- Expand_Ctrl_Function_Call)
910 if Is_RTE (Apool, RE_SS_Pool) then
912 F : constant Entity_Id :=
913 Make_Defining_Identifier (Loc,
914 New_Internal_Name ('F'));
917 Make_Object_Declaration (Loc,
918 Defining_Identifier => F,
919 Object_Definition => New_Reference_To (RTE
920 (RE_Finalizable_Ptr), Loc)));
922 Flist := New_Reference_To (F, Loc);
923 Attach := Make_Integer_Literal (Loc, 1);
926 -- Normal case, not a secondary stack allocation
929 if Needs_Finalization (T)
930 and then Ekind (PtrT) = E_Anonymous_Access_Type
932 -- Create local finalization list for access parameter
935 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
937 Flist := Find_Final_List (PtrT);
940 Attach := Make_Integer_Literal (Loc, 2);
943 -- Generate an Adjust call if the object will be moved. In Ada
944 -- 2005, the object may be inherently limited, in which case
945 -- there is no Adjust procedure, and the object is built in
946 -- place. In Ada 95, the object can be limited but not
947 -- inherently limited if this allocator came from a return
948 -- statement (we're allocating the result on the secondary
949 -- stack). In that case, the object will be moved, so we _do_
953 and then not Is_Inherently_Limited_Type (T)
959 -- An unchecked conversion is needed in the classwide
960 -- case because the designated type can be an ancestor of
961 -- the subtype mark of the allocator.
963 Unchecked_Convert_To (T,
964 Make_Explicit_Dereference (Loc,
965 Prefix => New_Reference_To (Temp, Loc))),
969 With_Attach => Attach,
975 Rewrite (N, New_Reference_To (Temp, Loc));
976 Analyze_And_Resolve (N, PtrT);
978 -- Ada 2005 (AI-251): Displace the pointer to reference the record
979 -- component containing the secondary dispatch table of the interface
982 if Is_Interface (Directly_Designated_Type (PtrT)) then
983 Displace_Allocator_Pointer (N);
986 elsif Aggr_In_Place then
988 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
990 Make_Object_Declaration (Loc,
991 Defining_Identifier => Temp,
992 Object_Definition => New_Reference_To (PtrT, Loc),
993 Expression => Make_Allocator (Loc,
994 New_Reference_To (Etype (Exp), Loc)));
996 -- Copy the Comes_From_Source flag for the allocator we just built,
997 -- since logically this allocator is a replacement of the original
998 -- allocator node. This is for proper handling of restriction
999 -- No_Implicit_Heap_Allocations.
1001 Set_Comes_From_Source
1002 (Expression (Tmp_Node), Comes_From_Source (N));
1004 Set_No_Initialization (Expression (Tmp_Node));
1005 Insert_Action (N, Tmp_Node);
1006 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
1007 Rewrite (N, New_Reference_To (Temp, Loc));
1008 Analyze_And_Resolve (N, PtrT);
1010 elsif Is_Access_Type (T)
1011 and then Can_Never_Be_Null (T)
1013 Install_Null_Excluding_Check (Exp);
1015 elsif Is_Access_Type (DesigT)
1016 and then Nkind (Exp) = N_Allocator
1017 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1019 -- Apply constraint to designated subtype indication
1021 Apply_Constraint_Check (Expression (Exp),
1022 Designated_Type (DesigT),
1023 No_Sliding => True);
1025 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1027 -- Propagate constraint_error to enclosing allocator
1029 Rewrite (Exp, New_Copy (Expression (Exp)));
1033 -- type A is access T1;
1034 -- X : A := new T2'(...);
1035 -- T1 and T2 can be different subtypes, and we might need to check
1036 -- both constraints. First check against the type of the qualified
1039 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1041 if Do_Range_Check (Exp) then
1042 Set_Do_Range_Check (Exp, False);
1043 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1046 -- A check is also needed in cases where the designated subtype is
1047 -- constrained and differs from the subtype given in the qualified
1048 -- expression. Note that the check on the qualified expression does
1049 -- not allow sliding, but this check does (a relaxation from Ada 83).
1051 if Is_Constrained (DesigT)
1052 and then not Subtypes_Statically_Match (T, DesigT)
1054 Apply_Constraint_Check
1055 (Exp, DesigT, No_Sliding => False);
1057 if Do_Range_Check (Exp) then
1058 Set_Do_Range_Check (Exp, False);
1059 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1063 -- For an access to unconstrained packed array, GIGI needs to see an
1064 -- expression with a constrained subtype in order to compute the
1065 -- proper size for the allocator.
1067 if Is_Array_Type (T)
1068 and then not Is_Constrained (T)
1069 and then Is_Packed (T)
1072 ConstrT : constant Entity_Id :=
1073 Make_Defining_Identifier (Loc,
1074 Chars => New_Internal_Name ('A'));
1075 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1078 Make_Subtype_Declaration (Loc,
1079 Defining_Identifier => ConstrT,
1080 Subtype_Indication =>
1081 Make_Subtype_From_Expr (Exp, T)));
1082 Freeze_Itype (ConstrT, Exp);
1083 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1087 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1088 -- to a build-in-place function, then access to the allocated object
1089 -- must be passed to the function. Currently we limit such functions
1090 -- to those with constrained limited result subtypes, but eventually
1091 -- we plan to expand the allowed forms of functions that are treated
1092 -- as build-in-place.
1094 if Ada_Version >= Ada_05
1095 and then Is_Build_In_Place_Function_Call (Exp)
1097 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1102 when RE_Not_Available =>
1104 end Expand_Allocator_Expression;
1106 -----------------------------
1107 -- Expand_Array_Comparison --
1108 -----------------------------
1110 -- Expansion is only required in the case of array types. For the unpacked
1111 -- case, an appropriate runtime routine is called. For packed cases, and
1112 -- also in some other cases where a runtime routine cannot be called, the
1113 -- form of the expansion is:
1115 -- [body for greater_nn; boolean_expression]
1117 -- The body is built by Make_Array_Comparison_Op, and the form of the
1118 -- Boolean expression depends on the operator involved.
1120 procedure Expand_Array_Comparison (N : Node_Id) is
1121 Loc : constant Source_Ptr := Sloc (N);
1122 Op1 : Node_Id := Left_Opnd (N);
1123 Op2 : Node_Id := Right_Opnd (N);
1124 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1125 Ctyp : constant Entity_Id := Component_Type (Typ1);
1128 Func_Body : Node_Id;
1129 Func_Name : Entity_Id;
1133 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1134 -- True for byte addressable target
1136 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1137 -- Returns True if the length of the given operand is known to be less
1138 -- than 4. Returns False if this length is known to be four or greater
1139 -- or is not known at compile time.
1141 ------------------------
1142 -- Length_Less_Than_4 --
1143 ------------------------
1145 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1146 Otyp : constant Entity_Id := Etype (Opnd);
1149 if Ekind (Otyp) = E_String_Literal_Subtype then
1150 return String_Literal_Length (Otyp) < 4;
1154 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1155 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1156 Hi : constant Node_Id := Type_High_Bound (Ityp);
1161 if Compile_Time_Known_Value (Lo) then
1162 Lov := Expr_Value (Lo);
1167 if Compile_Time_Known_Value (Hi) then
1168 Hiv := Expr_Value (Hi);
1173 return Hiv < Lov + 3;
1176 end Length_Less_Than_4;
1178 -- Start of processing for Expand_Array_Comparison
1181 -- Deal first with unpacked case, where we can call a runtime routine
1182 -- except that we avoid this for targets for which are not addressable
1183 -- by bytes, and for the JVM/CIL, since they do not support direct
1184 -- addressing of array components.
1186 if not Is_Bit_Packed_Array (Typ1)
1187 and then Byte_Addressable
1188 and then VM_Target = No_VM
1190 -- The call we generate is:
1192 -- Compare_Array_xn[_Unaligned]
1193 -- (left'address, right'address, left'length, right'length) <op> 0
1195 -- x = U for unsigned, S for signed
1196 -- n = 8,16,32,64 for component size
1197 -- Add _Unaligned if length < 4 and component size is 8.
1198 -- <op> is the standard comparison operator
1200 if Component_Size (Typ1) = 8 then
1201 if Length_Less_Than_4 (Op1)
1203 Length_Less_Than_4 (Op2)
1205 if Is_Unsigned_Type (Ctyp) then
1206 Comp := RE_Compare_Array_U8_Unaligned;
1208 Comp := RE_Compare_Array_S8_Unaligned;
1212 if Is_Unsigned_Type (Ctyp) then
1213 Comp := RE_Compare_Array_U8;
1215 Comp := RE_Compare_Array_S8;
1219 elsif Component_Size (Typ1) = 16 then
1220 if Is_Unsigned_Type (Ctyp) then
1221 Comp := RE_Compare_Array_U16;
1223 Comp := RE_Compare_Array_S16;
1226 elsif Component_Size (Typ1) = 32 then
1227 if Is_Unsigned_Type (Ctyp) then
1228 Comp := RE_Compare_Array_U32;
1230 Comp := RE_Compare_Array_S32;
1233 else pragma Assert (Component_Size (Typ1) = 64);
1234 if Is_Unsigned_Type (Ctyp) then
1235 Comp := RE_Compare_Array_U64;
1237 Comp := RE_Compare_Array_S64;
1241 Remove_Side_Effects (Op1, Name_Req => True);
1242 Remove_Side_Effects (Op2, Name_Req => True);
1245 Make_Function_Call (Sloc (Op1),
1246 Name => New_Occurrence_Of (RTE (Comp), Loc),
1248 Parameter_Associations => New_List (
1249 Make_Attribute_Reference (Loc,
1250 Prefix => Relocate_Node (Op1),
1251 Attribute_Name => Name_Address),
1253 Make_Attribute_Reference (Loc,
1254 Prefix => Relocate_Node (Op2),
1255 Attribute_Name => Name_Address),
1257 Make_Attribute_Reference (Loc,
1258 Prefix => Relocate_Node (Op1),
1259 Attribute_Name => Name_Length),
1261 Make_Attribute_Reference (Loc,
1262 Prefix => Relocate_Node (Op2),
1263 Attribute_Name => Name_Length))));
1266 Make_Integer_Literal (Sloc (Op2),
1269 Analyze_And_Resolve (Op1, Standard_Integer);
1270 Analyze_And_Resolve (Op2, Standard_Integer);
1274 -- Cases where we cannot make runtime call
1276 -- For (a <= b) we convert to not (a > b)
1278 if Chars (N) = Name_Op_Le then
1284 Right_Opnd => Op2)));
1285 Analyze_And_Resolve (N, Standard_Boolean);
1288 -- For < the Boolean expression is
1289 -- greater__nn (op2, op1)
1291 elsif Chars (N) = Name_Op_Lt then
1292 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1296 Op1 := Right_Opnd (N);
1297 Op2 := Left_Opnd (N);
1299 -- For (a >= b) we convert to not (a < b)
1301 elsif Chars (N) = Name_Op_Ge then
1307 Right_Opnd => Op2)));
1308 Analyze_And_Resolve (N, Standard_Boolean);
1311 -- For > the Boolean expression is
1312 -- greater__nn (op1, op2)
1315 pragma Assert (Chars (N) = Name_Op_Gt);
1316 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1319 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1321 Make_Function_Call (Loc,
1322 Name => New_Reference_To (Func_Name, Loc),
1323 Parameter_Associations => New_List (Op1, Op2));
1325 Insert_Action (N, Func_Body);
1327 Analyze_And_Resolve (N, Standard_Boolean);
1330 when RE_Not_Available =>
1332 end Expand_Array_Comparison;
1334 ---------------------------
1335 -- Expand_Array_Equality --
1336 ---------------------------
1338 -- Expand an equality function for multi-dimensional arrays. Here is an
1339 -- example of such a function for Nb_Dimension = 2
1341 -- function Enn (A : atyp; B : btyp) return boolean is
1343 -- if (A'length (1) = 0 or else A'length (2) = 0)
1345 -- (B'length (1) = 0 or else B'length (2) = 0)
1347 -- return True; -- RM 4.5.2(22)
1350 -- if A'length (1) /= B'length (1)
1352 -- A'length (2) /= B'length (2)
1354 -- return False; -- RM 4.5.2(23)
1358 -- A1 : Index_T1 := A'first (1);
1359 -- B1 : Index_T1 := B'first (1);
1363 -- A2 : Index_T2 := A'first (2);
1364 -- B2 : Index_T2 := B'first (2);
1367 -- if A (A1, A2) /= B (B1, B2) then
1371 -- exit when A2 = A'last (2);
1372 -- A2 := Index_T2'succ (A2);
1373 -- B2 := Index_T2'succ (B2);
1377 -- exit when A1 = A'last (1);
1378 -- A1 := Index_T1'succ (A1);
1379 -- B1 := Index_T1'succ (B1);
1386 -- Note on the formal types used (atyp and btyp). If either of the arrays
1387 -- is of a private type, we use the underlying type, and do an unchecked
1388 -- conversion of the actual. If either of the arrays has a bound depending
1389 -- on a discriminant, then we use the base type since otherwise we have an
1390 -- escaped discriminant in the function.
1392 -- If both arrays are constrained and have the same bounds, we can generate
1393 -- a loop with an explicit iteration scheme using a 'Range attribute over
1396 function Expand_Array_Equality
1401 Typ : Entity_Id) return Node_Id
1403 Loc : constant Source_Ptr := Sloc (Nod);
1404 Decls : constant List_Id := New_List;
1405 Index_List1 : constant List_Id := New_List;
1406 Index_List2 : constant List_Id := New_List;
1410 Func_Name : Entity_Id;
1411 Func_Body : Node_Id;
1413 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1414 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1418 -- The parameter types to be used for the formals
1423 Num : Int) return Node_Id;
1424 -- This builds the attribute reference Arr'Nam (Expr)
1426 function Component_Equality (Typ : Entity_Id) return Node_Id;
1427 -- Create one statement to compare corresponding components, designated
1428 -- by a full set of indices.
1430 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1431 -- Given one of the arguments, computes the appropriate type to be used
1432 -- for that argument in the corresponding function formal
1434 function Handle_One_Dimension
1436 Index : Node_Id) return Node_Id;
1437 -- This procedure returns the following code
1440 -- Bn : Index_T := B'First (N);
1444 -- exit when An = A'Last (N);
1445 -- An := Index_T'Succ (An)
1446 -- Bn := Index_T'Succ (Bn)
1450 -- If both indices are constrained and identical, the procedure
1451 -- returns a simpler loop:
1453 -- for An in A'Range (N) loop
1457 -- N is the dimension for which we are generating a loop. Index is the
1458 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1459 -- xxx statement is either the loop or declare for the next dimension
1460 -- or if this is the last dimension the comparison of corresponding
1461 -- components of the arrays.
1463 -- The actual way the code works is to return the comparison of
1464 -- corresponding components for the N+1 call. That's neater!
1466 function Test_Empty_Arrays return Node_Id;
1467 -- This function constructs the test for both arrays being empty
1468 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1470 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1472 function Test_Lengths_Correspond return Node_Id;
1473 -- This function constructs the test for arrays having different lengths
1474 -- in at least one index position, in which case the resulting code is:
1476 -- A'length (1) /= B'length (1)
1478 -- A'length (2) /= B'length (2)
1489 Num : Int) return Node_Id
1493 Make_Attribute_Reference (Loc,
1494 Attribute_Name => Nam,
1495 Prefix => New_Reference_To (Arr, Loc),
1496 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1499 ------------------------
1500 -- Component_Equality --
1501 ------------------------
1503 function Component_Equality (Typ : Entity_Id) return Node_Id is
1508 -- if a(i1...) /= b(j1...) then return false; end if;
1511 Make_Indexed_Component (Loc,
1512 Prefix => Make_Identifier (Loc, Chars (A)),
1513 Expressions => Index_List1);
1516 Make_Indexed_Component (Loc,
1517 Prefix => Make_Identifier (Loc, Chars (B)),
1518 Expressions => Index_List2);
1520 Test := Expand_Composite_Equality
1521 (Nod, Component_Type (Typ), L, R, Decls);
1523 -- If some (sub)component is an unchecked_union, the whole operation
1524 -- will raise program error.
1526 if Nkind (Test) = N_Raise_Program_Error then
1528 -- This node is going to be inserted at a location where a
1529 -- statement is expected: clear its Etype so analysis will set
1530 -- it to the expected Standard_Void_Type.
1532 Set_Etype (Test, Empty);
1537 Make_Implicit_If_Statement (Nod,
1538 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1539 Then_Statements => New_List (
1540 Make_Simple_Return_Statement (Loc,
1541 Expression => New_Occurrence_Of (Standard_False, Loc))));
1543 end Component_Equality;
1549 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1560 T := Underlying_Type (T);
1562 X := First_Index (T);
1563 while Present (X) loop
1564 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1566 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1579 --------------------------
1580 -- Handle_One_Dimension --
1581 ---------------------------
1583 function Handle_One_Dimension
1585 Index : Node_Id) return Node_Id
1587 Need_Separate_Indexes : constant Boolean :=
1589 or else not Is_Constrained (Ltyp);
1590 -- If the index types are identical, and we are working with
1591 -- constrained types, then we can use the same index for both
1594 An : constant Entity_Id := Make_Defining_Identifier (Loc,
1595 Chars => New_Internal_Name ('A'));
1598 Index_T : Entity_Id;
1603 if N > Number_Dimensions (Ltyp) then
1604 return Component_Equality (Ltyp);
1607 -- Case where we generate a loop
1609 Index_T := Base_Type (Etype (Index));
1611 if Need_Separate_Indexes then
1613 Make_Defining_Identifier (Loc,
1614 Chars => New_Internal_Name ('B'));
1619 Append (New_Reference_To (An, Loc), Index_List1);
1620 Append (New_Reference_To (Bn, Loc), Index_List2);
1622 Stm_List := New_List (
1623 Handle_One_Dimension (N + 1, Next_Index (Index)));
1625 if Need_Separate_Indexes then
1627 -- Generate guard for loop, followed by increments of indices
1629 Append_To (Stm_List,
1630 Make_Exit_Statement (Loc,
1633 Left_Opnd => New_Reference_To (An, Loc),
1634 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1636 Append_To (Stm_List,
1637 Make_Assignment_Statement (Loc,
1638 Name => New_Reference_To (An, Loc),
1640 Make_Attribute_Reference (Loc,
1641 Prefix => New_Reference_To (Index_T, Loc),
1642 Attribute_Name => Name_Succ,
1643 Expressions => New_List (New_Reference_To (An, Loc)))));
1645 Append_To (Stm_List,
1646 Make_Assignment_Statement (Loc,
1647 Name => New_Reference_To (Bn, Loc),
1649 Make_Attribute_Reference (Loc,
1650 Prefix => New_Reference_To (Index_T, Loc),
1651 Attribute_Name => Name_Succ,
1652 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1655 -- If separate indexes, we need a declare block for An and Bn, and a
1656 -- loop without an iteration scheme.
1658 if Need_Separate_Indexes then
1660 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1663 Make_Block_Statement (Loc,
1664 Declarations => New_List (
1665 Make_Object_Declaration (Loc,
1666 Defining_Identifier => An,
1667 Object_Definition => New_Reference_To (Index_T, Loc),
1668 Expression => Arr_Attr (A, Name_First, N)),
1670 Make_Object_Declaration (Loc,
1671 Defining_Identifier => Bn,
1672 Object_Definition => New_Reference_To (Index_T, Loc),
1673 Expression => Arr_Attr (B, Name_First, N))),
1675 Handled_Statement_Sequence =>
1676 Make_Handled_Sequence_Of_Statements (Loc,
1677 Statements => New_List (Loop_Stm)));
1679 -- If no separate indexes, return loop statement with explicit
1680 -- iteration scheme on its own
1684 Make_Implicit_Loop_Statement (Nod,
1685 Statements => Stm_List,
1687 Make_Iteration_Scheme (Loc,
1688 Loop_Parameter_Specification =>
1689 Make_Loop_Parameter_Specification (Loc,
1690 Defining_Identifier => An,
1691 Discrete_Subtype_Definition =>
1692 Arr_Attr (A, Name_Range, N))));
1695 end Handle_One_Dimension;
1697 -----------------------
1698 -- Test_Empty_Arrays --
1699 -----------------------
1701 function Test_Empty_Arrays return Node_Id is
1711 for J in 1 .. Number_Dimensions (Ltyp) loop
1714 Left_Opnd => Arr_Attr (A, Name_Length, J),
1715 Right_Opnd => Make_Integer_Literal (Loc, 0));
1719 Left_Opnd => Arr_Attr (B, Name_Length, J),
1720 Right_Opnd => Make_Integer_Literal (Loc, 0));
1729 Left_Opnd => Relocate_Node (Alist),
1730 Right_Opnd => Atest);
1734 Left_Opnd => Relocate_Node (Blist),
1735 Right_Opnd => Btest);
1742 Right_Opnd => Blist);
1743 end Test_Empty_Arrays;
1745 -----------------------------
1746 -- Test_Lengths_Correspond --
1747 -----------------------------
1749 function Test_Lengths_Correspond return Node_Id is
1755 for J in 1 .. Number_Dimensions (Ltyp) loop
1758 Left_Opnd => Arr_Attr (A, Name_Length, J),
1759 Right_Opnd => Arr_Attr (B, Name_Length, J));
1766 Left_Opnd => Relocate_Node (Result),
1767 Right_Opnd => Rtest);
1772 end Test_Lengths_Correspond;
1774 -- Start of processing for Expand_Array_Equality
1777 Ltyp := Get_Arg_Type (Lhs);
1778 Rtyp := Get_Arg_Type (Rhs);
1780 -- For now, if the argument types are not the same, go to the base type,
1781 -- since the code assumes that the formals have the same type. This is
1782 -- fixable in future ???
1784 if Ltyp /= Rtyp then
1785 Ltyp := Base_Type (Ltyp);
1786 Rtyp := Base_Type (Rtyp);
1787 pragma Assert (Ltyp = Rtyp);
1790 -- Build list of formals for function
1792 Formals := New_List (
1793 Make_Parameter_Specification (Loc,
1794 Defining_Identifier => A,
1795 Parameter_Type => New_Reference_To (Ltyp, Loc)),
1797 Make_Parameter_Specification (Loc,
1798 Defining_Identifier => B,
1799 Parameter_Type => New_Reference_To (Rtyp, Loc)));
1801 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
1803 -- Build statement sequence for function
1806 Make_Subprogram_Body (Loc,
1808 Make_Function_Specification (Loc,
1809 Defining_Unit_Name => Func_Name,
1810 Parameter_Specifications => Formals,
1811 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
1813 Declarations => Decls,
1815 Handled_Statement_Sequence =>
1816 Make_Handled_Sequence_Of_Statements (Loc,
1817 Statements => New_List (
1819 Make_Implicit_If_Statement (Nod,
1820 Condition => Test_Empty_Arrays,
1821 Then_Statements => New_List (
1822 Make_Simple_Return_Statement (Loc,
1824 New_Occurrence_Of (Standard_True, Loc)))),
1826 Make_Implicit_If_Statement (Nod,
1827 Condition => Test_Lengths_Correspond,
1828 Then_Statements => New_List (
1829 Make_Simple_Return_Statement (Loc,
1831 New_Occurrence_Of (Standard_False, Loc)))),
1833 Handle_One_Dimension (1, First_Index (Ltyp)),
1835 Make_Simple_Return_Statement (Loc,
1836 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1838 Set_Has_Completion (Func_Name, True);
1839 Set_Is_Inlined (Func_Name);
1841 -- If the array type is distinct from the type of the arguments, it
1842 -- is the full view of a private type. Apply an unchecked conversion
1843 -- to insure that analysis of the call succeeds.
1853 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1855 L := OK_Convert_To (Ltyp, Lhs);
1859 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1861 R := OK_Convert_To (Rtyp, Rhs);
1864 Actuals := New_List (L, R);
1867 Append_To (Bodies, Func_Body);
1870 Make_Function_Call (Loc,
1871 Name => New_Reference_To (Func_Name, Loc),
1872 Parameter_Associations => Actuals);
1873 end Expand_Array_Equality;
1875 -----------------------------
1876 -- Expand_Boolean_Operator --
1877 -----------------------------
1879 -- Note that we first get the actual subtypes of the operands, since we
1880 -- always want to deal with types that have bounds.
1882 procedure Expand_Boolean_Operator (N : Node_Id) is
1883 Typ : constant Entity_Id := Etype (N);
1886 -- Special case of bit packed array where both operands are known to be
1887 -- properly aligned. In this case we use an efficient run time routine
1888 -- to carry out the operation (see System.Bit_Ops).
1890 if Is_Bit_Packed_Array (Typ)
1891 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
1892 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
1894 Expand_Packed_Boolean_Operator (N);
1898 -- For the normal non-packed case, the general expansion is to build
1899 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
1900 -- and then inserting it into the tree. The original operator node is
1901 -- then rewritten as a call to this function. We also use this in the
1902 -- packed case if either operand is a possibly unaligned object.
1905 Loc : constant Source_Ptr := Sloc (N);
1906 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1907 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1908 Func_Body : Node_Id;
1909 Func_Name : Entity_Id;
1912 Convert_To_Actual_Subtype (L);
1913 Convert_To_Actual_Subtype (R);
1914 Ensure_Defined (Etype (L), N);
1915 Ensure_Defined (Etype (R), N);
1916 Apply_Length_Check (R, Etype (L));
1918 if Nkind (N) = N_Op_Xor then
1919 Silly_Boolean_Array_Xor_Test (N, Etype (L));
1922 if Nkind (Parent (N)) = N_Assignment_Statement
1923 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
1925 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
1927 elsif Nkind (Parent (N)) = N_Op_Not
1928 and then Nkind (N) = N_Op_And
1930 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
1935 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
1936 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1937 Insert_Action (N, Func_Body);
1939 -- Now rewrite the expression with a call
1942 Make_Function_Call (Loc,
1943 Name => New_Reference_To (Func_Name, Loc),
1944 Parameter_Associations =>
1947 Make_Type_Conversion
1948 (Loc, New_Reference_To (Etype (L), Loc), R))));
1950 Analyze_And_Resolve (N, Typ);
1953 end Expand_Boolean_Operator;
1955 -------------------------------
1956 -- Expand_Composite_Equality --
1957 -------------------------------
1959 -- This function is only called for comparing internal fields of composite
1960 -- types when these fields are themselves composites. This is a special
1961 -- case because it is not possible to respect normal Ada visibility rules.
1963 function Expand_Composite_Equality
1968 Bodies : List_Id) return Node_Id
1970 Loc : constant Source_Ptr := Sloc (Nod);
1971 Full_Type : Entity_Id;
1976 if Is_Private_Type (Typ) then
1977 Full_Type := Underlying_Type (Typ);
1982 -- Defense against malformed private types with no completion the error
1983 -- will be diagnosed later by check_completion
1985 if No (Full_Type) then
1986 return New_Reference_To (Standard_False, Loc);
1989 Full_Type := Base_Type (Full_Type);
1991 if Is_Array_Type (Full_Type) then
1993 -- If the operand is an elementary type other than a floating-point
1994 -- type, then we can simply use the built-in block bitwise equality,
1995 -- since the predefined equality operators always apply and bitwise
1996 -- equality is fine for all these cases.
1998 if Is_Elementary_Type (Component_Type (Full_Type))
1999 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2001 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2003 -- For composite component types, and floating-point types, use the
2004 -- expansion. This deals with tagged component types (where we use
2005 -- the applicable equality routine) and floating-point, (where we
2006 -- need to worry about negative zeroes), and also the case of any
2007 -- composite type recursively containing such fields.
2010 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2013 elsif Is_Tagged_Type (Full_Type) then
2015 -- Call the primitive operation "=" of this type
2017 if Is_Class_Wide_Type (Full_Type) then
2018 Full_Type := Root_Type (Full_Type);
2021 -- If this is derived from an untagged private type completed with a
2022 -- tagged type, it does not have a full view, so we use the primitive
2023 -- operations of the private type. This check should no longer be
2024 -- necessary when these types receive their full views ???
2026 if Is_Private_Type (Typ)
2027 and then not Is_Tagged_Type (Typ)
2028 and then not Is_Controlled (Typ)
2029 and then Is_Derived_Type (Typ)
2030 and then No (Full_View (Typ))
2032 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2034 Prim := First_Elmt (Primitive_Operations (Full_Type));
2038 Eq_Op := Node (Prim);
2039 exit when Chars (Eq_Op) = Name_Op_Eq
2040 and then Etype (First_Formal (Eq_Op)) =
2041 Etype (Next_Formal (First_Formal (Eq_Op)))
2042 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2044 pragma Assert (Present (Prim));
2047 Eq_Op := Node (Prim);
2050 Make_Function_Call (Loc,
2051 Name => New_Reference_To (Eq_Op, Loc),
2052 Parameter_Associations =>
2054 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2055 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2057 elsif Is_Record_Type (Full_Type) then
2058 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2060 if Present (Eq_Op) then
2061 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2063 -- Inherited equality from parent type. Convert the actuals to
2064 -- match signature of operation.
2067 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2071 Make_Function_Call (Loc,
2072 Name => New_Reference_To (Eq_Op, Loc),
2073 Parameter_Associations =>
2074 New_List (OK_Convert_To (T, Lhs),
2075 OK_Convert_To (T, Rhs)));
2079 -- Comparison between Unchecked_Union components
2081 if Is_Unchecked_Union (Full_Type) then
2083 Lhs_Type : Node_Id := Full_Type;
2084 Rhs_Type : Node_Id := Full_Type;
2085 Lhs_Discr_Val : Node_Id;
2086 Rhs_Discr_Val : Node_Id;
2091 if Nkind (Lhs) = N_Selected_Component then
2092 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2097 if Nkind (Rhs) = N_Selected_Component then
2098 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2101 -- Lhs of the composite equality
2103 if Is_Constrained (Lhs_Type) then
2105 -- Since the enclosing record type can never be an
2106 -- Unchecked_Union (this code is executed for records
2107 -- that do not have variants), we may reference its
2110 if Nkind (Lhs) = N_Selected_Component
2111 and then Has_Per_Object_Constraint (
2112 Entity (Selector_Name (Lhs)))
2115 Make_Selected_Component (Loc,
2116 Prefix => Prefix (Lhs),
2119 Get_Discriminant_Value (
2120 First_Discriminant (Lhs_Type),
2122 Stored_Constraint (Lhs_Type))));
2125 Lhs_Discr_Val := New_Copy (
2126 Get_Discriminant_Value (
2127 First_Discriminant (Lhs_Type),
2129 Stored_Constraint (Lhs_Type)));
2133 -- It is not possible to infer the discriminant since
2134 -- the subtype is not constrained.
2137 Make_Raise_Program_Error (Loc,
2138 Reason => PE_Unchecked_Union_Restriction);
2141 -- Rhs of the composite equality
2143 if Is_Constrained (Rhs_Type) then
2144 if Nkind (Rhs) = N_Selected_Component
2145 and then Has_Per_Object_Constraint (
2146 Entity (Selector_Name (Rhs)))
2149 Make_Selected_Component (Loc,
2150 Prefix => Prefix (Rhs),
2153 Get_Discriminant_Value (
2154 First_Discriminant (Rhs_Type),
2156 Stored_Constraint (Rhs_Type))));
2159 Rhs_Discr_Val := New_Copy (
2160 Get_Discriminant_Value (
2161 First_Discriminant (Rhs_Type),
2163 Stored_Constraint (Rhs_Type)));
2168 Make_Raise_Program_Error (Loc,
2169 Reason => PE_Unchecked_Union_Restriction);
2172 -- Call the TSS equality function with the inferred
2173 -- discriminant values.
2176 Make_Function_Call (Loc,
2177 Name => New_Reference_To (Eq_Op, Loc),
2178 Parameter_Associations => New_List (
2186 -- Shouldn't this be an else, we can't fall through the above
2190 Make_Function_Call (Loc,
2191 Name => New_Reference_To (Eq_Op, Loc),
2192 Parameter_Associations => New_List (Lhs, Rhs));
2196 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2200 -- It can be a simple record or the full view of a scalar private
2202 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2204 end Expand_Composite_Equality;
2206 ------------------------
2207 -- Expand_Concatenate --
2208 ------------------------
2210 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2211 Loc : constant Source_Ptr := Sloc (Cnode);
2213 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2214 -- Result type of concatenation
2216 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2217 -- Component type. Elements of this component type can appear as one
2218 -- of the operands of concatenation as well as arrays.
2220 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2223 Ityp : constant Entity_Id := Base_Type (Istyp);
2224 -- Index type. This is the base type of the index subtype, and is used
2225 -- for all computed bounds (which may be out of range of Istyp in the
2226 -- case of null ranges).
2229 -- This is the type we use to do arithmetic to compute the bounds and
2230 -- lengths of operands. The choice of this type is a little subtle and
2231 -- is discussed in a separate section at the start of the body code.
2233 Concatenation_Error : exception;
2234 -- Raised if concatenation is sure to raise a CE
2236 Result_May_Be_Null : Boolean := True;
2237 -- Reset to False if at least one operand is encountered which is known
2238 -- at compile time to be non-null. Used for handling the special case
2239 -- of setting the high bound to the last operand high bound for a null
2240 -- result, thus ensuring a proper high bound in the super-flat case.
2242 N : constant Nat := List_Length (Opnds);
2243 -- Number of concatenation operands including possibly null operands
2246 -- Number of operands excluding any known to be null, except that the
2247 -- last operand is always retained, in case it provides the bounds for
2251 -- Current operand being processed in the loop through operands. After
2252 -- this loop is complete, always contains the last operand (which is not
2253 -- the same as Operands (NN), since null operands are skipped).
2255 -- Arrays describing the operands, only the first NN entries of each
2256 -- array are set (NN < N when we exclude known null operands).
2258 Is_Fixed_Length : array (1 .. N) of Boolean;
2259 -- True if length of corresponding operand known at compile time
2261 Operands : array (1 .. N) of Node_Id;
2262 -- Set to the corresponding entry in the Opnds list (but note that null
2263 -- operands are excluded, so not all entries in the list are stored).
2265 Fixed_Length : array (1 .. N) of Uint;
2266 -- Set to length of operand. Entries in this array are set only if the
2267 -- corresponding entry in Is_Fixed_Length is True.
2269 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2270 -- Set to lower bound of operand. Either an integer literal in the case
2271 -- where the bound is known at compile time, else actual lower bound.
2272 -- The operand low bound is of type Ityp.
2274 Var_Length : array (1 .. N) of Entity_Id;
2275 -- Set to an entity of type Natural that contains the length of an
2276 -- operand whose length is not known at compile time. Entries in this
2277 -- array are set only if the corresponding entry in Is_Fixed_Length
2278 -- is False. The entity is of type Artyp.
2280 Aggr_Length : array (0 .. N) of Node_Id;
2281 -- The J'th entry in an expression node that represents the total length
2282 -- of operands 1 through J. It is either an integer literal node, or a
2283 -- reference to a constant entity with the right value, so it is fine
2284 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2285 -- entry always is set to zero. The length is of type Artyp.
2287 Low_Bound : Node_Id;
2288 -- A tree node representing the low bound of the result (of type Ityp).
2289 -- This is either an integer literal node, or an identifier reference to
2290 -- a constant entity initialized to the appropriate value.
2292 Last_Opnd_High_Bound : Node_Id;
2293 -- A tree node representing the high bound of the last operand. This
2294 -- need only be set if the result could be null. It is used for the
2295 -- special case of setting the right high bound for a null result.
2296 -- This is of type Ityp.
2298 High_Bound : Node_Id;
2299 -- A tree node representing the high bound of the result (of type Ityp)
2302 -- Result of the concatenation (of type Ityp)
2304 Actions : constant List_Id := New_List;
2305 -- Collect actions to be inserted if Save_Space is False
2307 Save_Space : Boolean;
2308 pragma Warnings (Off, Save_Space);
2309 -- Set to True if we are saving generated code space by calling routines
2310 -- in packages System.Concat_n.
2312 Known_Non_Null_Operand_Seen : Boolean;
2313 -- Set True during generation of the assignements of operands into
2314 -- result once an operand known to be non-null has been seen.
2316 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2317 -- This function makes an N_Integer_Literal node that is returned in
2318 -- analyzed form with the type set to Artyp. Importantly this literal
2319 -- is not flagged as static, so that if we do computations with it that
2320 -- result in statically detected out of range conditions, we will not
2321 -- generate error messages but instead warning messages.
2323 function To_Artyp (X : Node_Id) return Node_Id;
2324 -- Given a node of type Ityp, returns the corresponding value of type
2325 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2326 -- For enum types, the Pos of the value is returned.
2328 function To_Ityp (X : Node_Id) return Node_Id;
2329 -- The inverse function (uses Val in the case of enumeration types)
2331 ------------------------
2332 -- Make_Artyp_Literal --
2333 ------------------------
2335 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2336 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2338 Set_Etype (Result, Artyp);
2339 Set_Analyzed (Result, True);
2340 Set_Is_Static_Expression (Result, False);
2342 end Make_Artyp_Literal;
2348 function To_Artyp (X : Node_Id) return Node_Id is
2350 if Ityp = Base_Type (Artyp) then
2353 elsif Is_Enumeration_Type (Ityp) then
2355 Make_Attribute_Reference (Loc,
2356 Prefix => New_Occurrence_Of (Ityp, Loc),
2357 Attribute_Name => Name_Pos,
2358 Expressions => New_List (X));
2361 return Convert_To (Artyp, X);
2369 function To_Ityp (X : Node_Id) return Node_Id is
2371 if Is_Enumeration_Type (Ityp) then
2373 Make_Attribute_Reference (Loc,
2374 Prefix => New_Occurrence_Of (Ityp, Loc),
2375 Attribute_Name => Name_Val,
2376 Expressions => New_List (X));
2378 -- Case where we will do a type conversion
2381 if Ityp = Base_Type (Artyp) then
2384 return Convert_To (Ityp, X);
2389 -- Local Declarations
2391 Opnd_Typ : Entity_Id;
2399 -- Choose an appropriate computational type
2401 -- We will be doing calculations of lengths and bounds in this routine
2402 -- and computing one from the other in some cases, e.g. getting the high
2403 -- bound by adding the length-1 to the low bound.
2405 -- We can't just use the index type, or even its base type for this
2406 -- purpose for two reasons. First it might be an enumeration type which
2407 -- is not suitable fo computations of any kind, and second it may simply
2408 -- not have enough range. For example if the index type is -128..+127
2409 -- then lengths can be up to 256, which is out of range of the type.
2411 -- For enumeration types, we can simply use Standard_Integer, this is
2412 -- sufficient since the actual number of enumeration literals cannot
2413 -- possibly exceed the range of integer (remember we will be doing the
2414 -- arithmetic with POS values, not representation values).
2416 if Is_Enumeration_Type (Ityp) then
2417 Artyp := Standard_Integer;
2419 -- If index type is Positive, we use the standard unsigned type, to give
2420 -- more room on the top of the range, obviating the need for an overflow
2421 -- check when creating the upper bound. This is needed to avoid junk
2422 -- overflow checks in the common case of String types.
2424 -- ??? Disabled for now
2426 -- elsif Istyp = Standard_Positive then
2427 -- Artyp := Standard_Unsigned;
2429 -- For modular types, we use a 32-bit modular type for types whose size
2430 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2431 -- identity type, and for larger unsigned types we use 64-bits.
2433 elsif Is_Modular_Integer_Type (Ityp) then
2434 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
2435 Artyp := Standard_Unsigned;
2436 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
2439 Artyp := RTE (RE_Long_Long_Unsigned);
2442 -- Similar treatment for signed types
2445 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
2446 Artyp := Standard_Integer;
2447 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
2450 Artyp := Standard_Long_Long_Integer;
2454 -- Supply dummy entry at start of length array
2456 Aggr_Length (0) := Make_Artyp_Literal (0);
2458 -- Go through operands setting up the above arrays
2462 Opnd := Remove_Head (Opnds);
2463 Opnd_Typ := Etype (Opnd);
2465 -- The parent got messed up when we put the operands in a list,
2466 -- so now put back the proper parent for the saved operand.
2468 Set_Parent (Opnd, Parent (Cnode));
2470 -- Set will be True when we have setup one entry in the array
2474 -- Singleton element (or character literal) case
2476 if Base_Type (Opnd_Typ) = Ctyp then
2478 Operands (NN) := Opnd;
2479 Is_Fixed_Length (NN) := True;
2480 Fixed_Length (NN) := Uint_1;
2481 Result_May_Be_Null := False;
2483 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2484 -- since we know that the result cannot be null).
2486 Opnd_Low_Bound (NN) :=
2487 Make_Attribute_Reference (Loc,
2488 Prefix => New_Reference_To (Istyp, Loc),
2489 Attribute_Name => Name_First);
2493 -- String literal case (can only occur for strings of course)
2495 elsif Nkind (Opnd) = N_String_Literal then
2496 Len := String_Literal_Length (Opnd_Typ);
2499 Result_May_Be_Null := False;
2502 -- Capture last operand high bound if result could be null
2504 if J = N and then Result_May_Be_Null then
2505 Last_Opnd_High_Bound :=
2508 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
2509 Right_Opnd => Make_Integer_Literal (Loc, 1));
2512 -- Skip null string literal
2514 if J < N and then Len = 0 then
2519 Operands (NN) := Opnd;
2520 Is_Fixed_Length (NN) := True;
2522 -- Set length and bounds
2524 Fixed_Length (NN) := Len;
2526 Opnd_Low_Bound (NN) :=
2527 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
2534 -- Check constrained case with known bounds
2536 if Is_Constrained (Opnd_Typ) then
2538 Index : constant Node_Id := First_Index (Opnd_Typ);
2539 Indx_Typ : constant Entity_Id := Etype (Index);
2540 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
2541 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
2544 -- Fixed length constrained array type with known at compile
2545 -- time bounds is last case of fixed length operand.
2547 if Compile_Time_Known_Value (Lo)
2549 Compile_Time_Known_Value (Hi)
2552 Loval : constant Uint := Expr_Value (Lo);
2553 Hival : constant Uint := Expr_Value (Hi);
2554 Len : constant Uint :=
2555 UI_Max (Hival - Loval + 1, Uint_0);
2559 Result_May_Be_Null := False;
2562 -- Capture last operand bound if result could be null
2564 if J = N and then Result_May_Be_Null then
2565 Last_Opnd_High_Bound :=
2567 Make_Integer_Literal (Loc,
2568 Intval => Expr_Value (Hi)));
2571 -- Exclude null length case unless last operand
2573 if J < N and then Len = 0 then
2578 Operands (NN) := Opnd;
2579 Is_Fixed_Length (NN) := True;
2580 Fixed_Length (NN) := Len;
2582 Opnd_Low_Bound (NN) := To_Ityp (
2583 Make_Integer_Literal (Loc,
2584 Intval => Expr_Value (Lo)));
2592 -- All cases where the length is not known at compile time, or the
2593 -- special case of an operand which is known to be null but has a
2594 -- lower bound other than 1 or is other than a string type.
2599 -- Capture operand bounds
2601 Opnd_Low_Bound (NN) :=
2602 Make_Attribute_Reference (Loc,
2604 Duplicate_Subexpr (Opnd, Name_Req => True),
2605 Attribute_Name => Name_First);
2607 if J = N and Result_May_Be_Null then
2608 Last_Opnd_High_Bound :=
2610 Make_Attribute_Reference (Loc,
2612 Duplicate_Subexpr (Opnd, Name_Req => True),
2613 Attribute_Name => Name_Last));
2616 -- Capture length of operand in entity
2618 Operands (NN) := Opnd;
2619 Is_Fixed_Length (NN) := False;
2622 Make_Defining_Identifier (Loc,
2623 Chars => New_Internal_Name ('L'));
2626 Make_Object_Declaration (Loc,
2627 Defining_Identifier => Var_Length (NN),
2628 Constant_Present => True,
2630 Object_Definition =>
2631 New_Occurrence_Of (Artyp, Loc),
2634 Make_Attribute_Reference (Loc,
2636 Duplicate_Subexpr (Opnd, Name_Req => True),
2637 Attribute_Name => Name_Length)));
2641 -- Set next entry in aggregate length array
2643 -- For first entry, make either integer literal for fixed length
2644 -- or a reference to the saved length for variable length.
2647 if Is_Fixed_Length (1) then
2649 Make_Integer_Literal (Loc,
2650 Intval => Fixed_Length (1));
2653 New_Reference_To (Var_Length (1), Loc);
2656 -- If entry is fixed length and only fixed lengths so far, make
2657 -- appropriate new integer literal adding new length.
2659 elsif Is_Fixed_Length (NN)
2660 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
2663 Make_Integer_Literal (Loc,
2664 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
2666 -- All other cases, construct an addition node for the length and
2667 -- create an entity initialized to this length.
2671 Make_Defining_Identifier (Loc,
2672 Chars => New_Internal_Name ('L'));
2674 if Is_Fixed_Length (NN) then
2675 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
2677 Clen := New_Reference_To (Var_Length (NN), Loc);
2681 Make_Object_Declaration (Loc,
2682 Defining_Identifier => Ent,
2683 Constant_Present => True,
2685 Object_Definition =>
2686 New_Occurrence_Of (Artyp, Loc),
2690 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
2691 Right_Opnd => Clen)));
2693 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
2700 -- If we have only skipped null operands, return the last operand
2707 -- If we have only one non-null operand, return it and we are done.
2708 -- There is one case in which this cannot be done, and that is when
2709 -- the sole operand is of the element type, in which case it must be
2710 -- converted to an array, and the easiest way of doing that is to go
2711 -- through the normal general circuit.
2714 and then Base_Type (Etype (Operands (1))) /= Ctyp
2716 Result := Operands (1);
2720 -- Cases where we have a real concatenation
2722 -- Next step is to find the low bound for the result array that we
2723 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
2725 -- If the ultimate ancestor of the index subtype is a constrained array
2726 -- definition, then the lower bound is that of the index subtype as
2727 -- specified by (RM 4.5.3(6)).
2729 -- The right test here is to go to the root type, and then the ultimate
2730 -- ancestor is the first subtype of this root type.
2732 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
2734 Make_Attribute_Reference (Loc,
2736 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
2737 Attribute_Name => Name_First);
2739 -- If the first operand in the list has known length we know that
2740 -- the lower bound of the result is the lower bound of this operand.
2742 elsif Is_Fixed_Length (1) then
2743 Low_Bound := Opnd_Low_Bound (1);
2745 -- OK, we don't know the lower bound, we have to build a horrible
2746 -- expression actions node of the form
2748 -- if Cond1'Length /= 0 then
2751 -- if Opnd2'Length /= 0 then
2756 -- The nesting ends either when we hit an operand whose length is known
2757 -- at compile time, or on reaching the last operand, whose low bound we
2758 -- take unconditionally whether or not it is null. It's easiest to do
2759 -- this with a recursive procedure:
2763 function Get_Known_Bound (J : Nat) return Node_Id;
2764 -- Returns the lower bound determined by operands J .. NN
2766 ---------------------
2767 -- Get_Known_Bound --
2768 ---------------------
2770 function Get_Known_Bound (J : Nat) return Node_Id is
2772 if Is_Fixed_Length (J) or else J = NN then
2773 return New_Copy (Opnd_Low_Bound (J));
2777 Make_Conditional_Expression (Loc,
2778 Expressions => New_List (
2781 Left_Opnd => New_Reference_To (Var_Length (J), Loc),
2782 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2784 New_Copy (Opnd_Low_Bound (J)),
2785 Get_Known_Bound (J + 1)));
2787 end Get_Known_Bound;
2791 Make_Defining_Identifier (Loc, Chars => New_Internal_Name ('L'));
2794 Make_Object_Declaration (Loc,
2795 Defining_Identifier => Ent,
2796 Constant_Present => True,
2797 Object_Definition => New_Occurrence_Of (Ityp, Loc),
2798 Expression => Get_Known_Bound (1)));
2800 Low_Bound := New_Reference_To (Ent, Loc);
2804 -- Now we can safely compute the upper bound, normally
2805 -- Low_Bound + Length - 1.
2810 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2812 Make_Op_Subtract (Loc,
2813 Left_Opnd => New_Copy (Aggr_Length (NN)),
2814 Right_Opnd => Make_Artyp_Literal (1))));
2816 -- Note that calculation of the high bound may cause overflow in some
2817 -- very weird cases, so in the general case we need an overflow check on
2818 -- the high bound. We can avoid this for the common case of string types
2819 -- and other types whose index is Positive, since we chose a wider range
2820 -- for the arithmetic type.
2822 if Istyp /= Standard_Positive then
2823 Activate_Overflow_Check (High_Bound);
2826 -- Handle the exceptional case where the result is null, in which case
2827 -- case the bounds come from the last operand (so that we get the proper
2828 -- bounds if the last operand is super-flat).
2830 if Result_May_Be_Null then
2832 Make_Conditional_Expression (Loc,
2833 Expressions => New_List (
2835 Left_Opnd => New_Copy (Aggr_Length (NN)),
2836 Right_Opnd => Make_Artyp_Literal (0)),
2837 Last_Opnd_High_Bound,
2841 -- Here is where we insert the saved up actions
2843 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
2845 -- Now we construct an array object with appropriate bounds
2848 Make_Defining_Identifier (Loc,
2849 Chars => New_Internal_Name ('S'));
2851 -- If the bound is statically known to be out of range, we do not want
2852 -- to abort, we want a warning and a runtime constraint error. Note that
2853 -- we have arranged that the result will not be treated as a static
2854 -- constant, so we won't get an illegality during this insertion.
2856 Insert_Action (Cnode,
2857 Make_Object_Declaration (Loc,
2858 Defining_Identifier => Ent,
2859 Object_Definition =>
2860 Make_Subtype_Indication (Loc,
2861 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
2863 Make_Index_Or_Discriminant_Constraint (Loc,
2864 Constraints => New_List (
2866 Low_Bound => Low_Bound,
2867 High_Bound => High_Bound))))),
2868 Suppress => All_Checks);
2870 -- If the result of the concatenation appears as the initializing
2871 -- expression of an object declaration, we can just rename the
2872 -- result, rather than copying it.
2874 Set_OK_To_Rename (Ent);
2876 -- Catch the static out of range case now
2878 if Raises_Constraint_Error (High_Bound) then
2879 raise Concatenation_Error;
2882 -- Now we will generate the assignments to do the actual concatenation
2884 -- There is one case in which we will not do this, namely when all the
2885 -- following conditions are met:
2887 -- The result type is Standard.String
2889 -- There are nine or fewer retained (non-null) operands
2891 -- The optimization level is -O0
2893 -- The corresponding System.Concat_n.Str_Concat_n routine is
2894 -- available in the run time.
2896 -- The debug flag gnatd.c is not set
2898 -- If all these conditions are met then we generate a call to the
2899 -- relevant concatenation routine. The purpose of this is to avoid
2900 -- undesirable code bloat at -O0.
2902 if Atyp = Standard_String
2903 and then NN in 2 .. 9
2904 and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
2905 and then not Debug_Flag_Dot_C
2908 RR : constant array (Nat range 2 .. 9) of RE_Id :=
2919 if RTE_Available (RR (NN)) then
2921 Opnds : constant List_Id :=
2922 New_List (New_Occurrence_Of (Ent, Loc));
2925 for J in 1 .. NN loop
2926 if Is_List_Member (Operands (J)) then
2927 Remove (Operands (J));
2930 if Base_Type (Etype (Operands (J))) = Ctyp then
2932 Make_Aggregate (Loc,
2933 Component_Associations => New_List (
2934 Make_Component_Association (Loc,
2935 Choices => New_List (
2936 Make_Integer_Literal (Loc, 1)),
2937 Expression => Operands (J)))));
2940 Append_To (Opnds, Operands (J));
2944 Insert_Action (Cnode,
2945 Make_Procedure_Call_Statement (Loc,
2946 Name => New_Reference_To (RTE (RR (NN)), Loc),
2947 Parameter_Associations => Opnds));
2949 Result := New_Reference_To (Ent, Loc);
2956 -- Not special case so generate the assignments
2958 Known_Non_Null_Operand_Seen := False;
2960 for J in 1 .. NN loop
2962 Lo : constant Node_Id :=
2964 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2965 Right_Opnd => Aggr_Length (J - 1));
2967 Hi : constant Node_Id :=
2969 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
2971 Make_Op_Subtract (Loc,
2972 Left_Opnd => Aggr_Length (J),
2973 Right_Opnd => Make_Artyp_Literal (1)));
2976 -- Singleton case, simple assignment
2978 if Base_Type (Etype (Operands (J))) = Ctyp then
2979 Known_Non_Null_Operand_Seen := True;
2980 Insert_Action (Cnode,
2981 Make_Assignment_Statement (Loc,
2983 Make_Indexed_Component (Loc,
2984 Prefix => New_Occurrence_Of (Ent, Loc),
2985 Expressions => New_List (To_Ityp (Lo))),
2986 Expression => Operands (J)),
2987 Suppress => All_Checks);
2989 -- Array case, slice assignment, skipped when argument is fixed
2990 -- length and known to be null.
2992 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
2995 Make_Assignment_Statement (Loc,
2999 New_Occurrence_Of (Ent, Loc),
3002 Low_Bound => To_Ityp (Lo),
3003 High_Bound => To_Ityp (Hi))),
3004 Expression => Operands (J));
3006 if Is_Fixed_Length (J) then
3007 Known_Non_Null_Operand_Seen := True;
3009 elsif not Known_Non_Null_Operand_Seen then
3011 -- Here if operand length is not statically known and no
3012 -- operand known to be non-null has been processed yet.
3013 -- If operand length is 0, we do not need to perform the
3014 -- assignment, and we must avoid the evaluation of the
3015 -- high bound of the slice, since it may underflow if the
3016 -- low bound is Ityp'First.
3019 Make_Implicit_If_Statement (Cnode,
3023 New_Occurrence_Of (Var_Length (J), Loc),
3024 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3029 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3035 -- Finally we build the result, which is a reference to the array object
3037 Result := New_Reference_To (Ent, Loc);
3040 Rewrite (Cnode, Result);
3041 Analyze_And_Resolve (Cnode, Atyp);
3044 when Concatenation_Error =>
3046 -- Kill warning generated for the declaration of the static out of
3047 -- range high bound, and instead generate a Constraint_Error with
3048 -- an appropriate specific message.
3050 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3051 Apply_Compile_Time_Constraint_Error
3053 Msg => "concatenation result upper bound out of range?",
3054 Reason => CE_Range_Check_Failed);
3055 -- Set_Etype (Cnode, Atyp);
3056 end Expand_Concatenate;
3058 ------------------------
3059 -- Expand_N_Allocator --
3060 ------------------------
3062 procedure Expand_N_Allocator (N : Node_Id) is
3063 PtrT : constant Entity_Id := Etype (N);
3064 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
3065 Etyp : constant Entity_Id := Etype (Expression (N));
3066 Loc : constant Source_Ptr := Sloc (N);
3071 procedure Complete_Coextension_Finalization;
3072 -- Generate finalization calls for all nested coextensions of N. This
3073 -- routine may allocate list controllers if necessary.
3075 procedure Rewrite_Coextension (N : Node_Id);
3076 -- Static coextensions have the same lifetime as the entity they
3077 -- constrain. Such occurrences can be rewritten as aliased objects
3078 -- and their unrestricted access used instead of the coextension.
3080 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
3081 -- Given a constrained array type E, returns a node representing the
3082 -- code to compute the size in storage elements for the given type.
3083 -- This is done without using the attribute (which malfunctions for
3086 ---------------------------------------
3087 -- Complete_Coextension_Finalization --
3088 ---------------------------------------
3090 procedure Complete_Coextension_Finalization is
3092 Coext_Elmt : Elmt_Id;
3096 function Inside_A_Return_Statement (N : Node_Id) return Boolean;
3097 -- Determine whether node N is part of a return statement
3099 function Needs_Initialization_Call (N : Node_Id) return Boolean;
3100 -- Determine whether node N is a subtype indicator allocator which
3101 -- acts a coextension. Such coextensions need initialization.
3103 -------------------------------
3104 -- Inside_A_Return_Statement --
3105 -------------------------------
3107 function Inside_A_Return_Statement (N : Node_Id) return Boolean is
3112 while Present (P) loop
3114 (P, N_Extended_Return_Statement, N_Simple_Return_Statement)
3118 -- Stop the traversal when we reach a subprogram body
3120 elsif Nkind (P) = N_Subprogram_Body then
3128 end Inside_A_Return_Statement;
3130 -------------------------------
3131 -- Needs_Initialization_Call --
3132 -------------------------------
3134 function Needs_Initialization_Call (N : Node_Id) return Boolean is
3138 if Nkind (N) = N_Explicit_Dereference
3139 and then Nkind (Prefix (N)) = N_Identifier
3140 and then Nkind (Parent (Entity (Prefix (N)))) =
3141 N_Object_Declaration
3143 Obj_Decl := Parent (Entity (Prefix (N)));
3146 Present (Expression (Obj_Decl))
3147 and then Nkind (Expression (Obj_Decl)) = N_Allocator
3148 and then Nkind (Expression (Expression (Obj_Decl))) /=
3149 N_Qualified_Expression;
3153 end Needs_Initialization_Call;
3155 -- Start of processing for Complete_Coextension_Finalization
3158 -- When a coextension root is inside a return statement, we need to
3159 -- use the finalization chain of the function's scope. This does not
3160 -- apply for controlled named access types because in those cases we
3161 -- can use the finalization chain of the type itself.
3163 if Inside_A_Return_Statement (N)
3165 (Ekind (PtrT) = E_Anonymous_Access_Type
3167 (Ekind (PtrT) = E_Access_Type
3168 and then No (Associated_Final_Chain (PtrT))))
3172 Outer_S : Entity_Id;
3173 S : Entity_Id := Current_Scope;
3176 while Present (S) and then S /= Standard_Standard loop
3177 if Ekind (S) = E_Function then
3178 Outer_S := Scope (S);
3180 -- Retrieve the declaration of the body
3185 (Corresponding_Body (Parent (Parent (S)))));
3192 -- Push the scope of the function body since we are inserting
3193 -- the list before the body, but we are currently in the body
3194 -- itself. Override the finalization list of PtrT since the
3195 -- finalization context is now different.
3197 Push_Scope (Outer_S);
3198 Build_Final_List (Decl, PtrT);
3202 -- The root allocator may not be controlled, but it still needs a
3203 -- finalization list for all nested coextensions.
3205 elsif No (Associated_Final_Chain (PtrT)) then
3206 Build_Final_List (N, PtrT);
3210 Make_Selected_Component (Loc,
3212 New_Reference_To (Associated_Final_Chain (PtrT), Loc),
3214 Make_Identifier (Loc, Name_F));
3216 Coext_Elmt := First_Elmt (Coextensions (N));
3217 while Present (Coext_Elmt) loop
3218 Coext := Node (Coext_Elmt);
3223 if Nkind (Coext) = N_Identifier then
3225 Make_Unchecked_Type_Conversion (Loc,
3226 Subtype_Mark => New_Reference_To (Etype (Coext), Loc),
3228 Make_Explicit_Dereference (Loc,
3229 Prefix => New_Copy_Tree (Coext)));
3231 Ref := New_Copy_Tree (Coext);
3234 -- No initialization call if not allowed
3236 Check_Restriction (No_Default_Initialization, N);
3238 if not Restriction_Active (No_Default_Initialization) then
3242 -- attach_to_final_list (Ref, Flist, 2)
3244 if Needs_Initialization_Call (Coext) then
3248 Typ => Etype (Coext),
3250 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3253 -- attach_to_final_list (Ref, Flist, 2)
3259 Flist_Ref => New_Copy_Tree (Flist),
3260 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3264 Next_Elmt (Coext_Elmt);
3266 end Complete_Coextension_Finalization;
3268 -------------------------
3269 -- Rewrite_Coextension --
3270 -------------------------
3272 procedure Rewrite_Coextension (N : Node_Id) is
3273 Temp : constant Node_Id :=
3274 Make_Defining_Identifier (Loc,
3275 New_Internal_Name ('C'));
3278 -- Cnn : aliased Etyp;
3280 Decl : constant Node_Id :=
3281 Make_Object_Declaration (Loc,
3282 Defining_Identifier => Temp,
3283 Aliased_Present => True,
3284 Object_Definition =>
3285 New_Occurrence_Of (Etyp, Loc));
3289 if Nkind (Expression (N)) = N_Qualified_Expression then
3290 Set_Expression (Decl, Expression (Expression (N)));
3293 -- Find the proper insertion node for the declaration
3296 while Present (Nod) loop
3297 exit when Nkind (Nod) in N_Statement_Other_Than_Procedure_Call
3298 or else Nkind (Nod) = N_Procedure_Call_Statement
3299 or else Nkind (Nod) in N_Declaration;
3300 Nod := Parent (Nod);
3303 Insert_Before (Nod, Decl);
3307 Make_Attribute_Reference (Loc,
3308 Prefix => New_Occurrence_Of (Temp, Loc),
3309 Attribute_Name => Name_Unrestricted_Access));
3311 Analyze_And_Resolve (N, PtrT);
3312 end Rewrite_Coextension;
3314 ------------------------------
3315 -- Size_In_Storage_Elements --
3316 ------------------------------
3318 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
3320 -- Logically this just returns E'Max_Size_In_Storage_Elements.
3321 -- However, the reason for the existence of this function is
3322 -- to construct a test for sizes too large, which means near the
3323 -- 32-bit limit on a 32-bit machine, and precisely the trouble
3324 -- is that we get overflows when sizes are greater than 2**31.
3326 -- So what we end up doing for array types is to use the expression:
3328 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
3330 -- which avoids this problem. All this is a big bogus, but it does
3331 -- mean we catch common cases of trying to allocate arrays that
3332 -- are too large, and which in the absence of a check results in
3333 -- undetected chaos ???
3340 for J in 1 .. Number_Dimensions (E) loop
3342 Make_Attribute_Reference (Loc,
3343 Prefix => New_Occurrence_Of (E, Loc),
3344 Attribute_Name => Name_Length,
3345 Expressions => New_List (
3346 Make_Integer_Literal (Loc, J)));
3353 Make_Op_Multiply (Loc,
3360 Make_Op_Multiply (Loc,
3363 Make_Attribute_Reference (Loc,
3364 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
3365 Attribute_Name => Name_Max_Size_In_Storage_Elements));
3367 end Size_In_Storage_Elements;
3369 -- Start of processing for Expand_N_Allocator
3372 -- RM E.2.3(22). We enforce that the expected type of an allocator
3373 -- shall not be a remote access-to-class-wide-limited-private type
3375 -- Why is this being done at expansion time, seems clearly wrong ???
3377 Validate_Remote_Access_To_Class_Wide_Type (N);
3379 -- Set the Storage Pool
3381 Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT)));
3383 if Present (Storage_Pool (N)) then
3384 if Is_RTE (Storage_Pool (N), RE_SS_Pool) then
3385 if VM_Target = No_VM then
3386 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3389 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
3390 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
3393 Set_Procedure_To_Call (N,
3394 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
3398 -- Under certain circumstances we can replace an allocator by an access
3399 -- to statically allocated storage. The conditions, as noted in AARM
3400 -- 3.10 (10c) are as follows:
3402 -- Size and initial value is known at compile time
3403 -- Access type is access-to-constant
3405 -- The allocator is not part of a constraint on a record component,
3406 -- because in that case the inserted actions are delayed until the
3407 -- record declaration is fully analyzed, which is too late for the
3408 -- analysis of the rewritten allocator.
3410 if Is_Access_Constant (PtrT)
3411 and then Nkind (Expression (N)) = N_Qualified_Expression
3412 and then Compile_Time_Known_Value (Expression (Expression (N)))
3413 and then Size_Known_At_Compile_Time (Etype (Expression
3415 and then not Is_Record_Type (Current_Scope)
3417 -- Here we can do the optimization. For the allocator
3421 -- We insert an object declaration
3423 -- Tnn : aliased x := y;
3425 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
3426 -- marked as requiring static allocation.
3429 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
3431 Desig := Subtype_Mark (Expression (N));
3433 -- If context is constrained, use constrained subtype directly,
3434 -- so that the constant is not labelled as having a nominally
3435 -- unconstrained subtype.
3437 if Entity (Desig) = Base_Type (Dtyp) then
3438 Desig := New_Occurrence_Of (Dtyp, Loc);
3442 Make_Object_Declaration (Loc,
3443 Defining_Identifier => Temp,
3444 Aliased_Present => True,
3445 Constant_Present => Is_Access_Constant (PtrT),
3446 Object_Definition => Desig,
3447 Expression => Expression (Expression (N))));
3450 Make_Attribute_Reference (Loc,
3451 Prefix => New_Occurrence_Of (Temp, Loc),
3452 Attribute_Name => Name_Unrestricted_Access));
3454 Analyze_And_Resolve (N, PtrT);
3456 -- We set the variable as statically allocated, since we don't want
3457 -- it going on the stack of the current procedure!
3459 Set_Is_Statically_Allocated (Temp);
3463 -- Same if the allocator is an access discriminant for a local object:
3464 -- instead of an allocator we create a local value and constrain the
3465 -- the enclosing object with the corresponding access attribute.
3467 if Is_Static_Coextension (N) then
3468 Rewrite_Coextension (N);
3472 -- The current allocator creates an object which may contain nested
3473 -- coextensions. Use the current allocator's finalization list to
3474 -- generate finalization call for all nested coextensions.
3476 if Is_Coextension_Root (N) then
3477 Complete_Coextension_Finalization;
3480 -- Check for size too large, we do this because the back end misses
3481 -- proper checks here and can generate rubbish allocation calls when
3482 -- we are near the limit. We only do this for the 32-bit address case
3483 -- since that is from a practical point of view where we see a problem.
3485 if System_Address_Size = 32
3486 and then not Storage_Checks_Suppressed (PtrT)
3487 and then not Storage_Checks_Suppressed (Dtyp)
3488 and then not Storage_Checks_Suppressed (Etyp)
3490 -- The check we want to generate should look like
3492 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
3493 -- raise Storage_Error;
3496 -- where 3.5 gigabytes is a constant large enough to accomodate any
3497 -- reasonable request for. But we can't do it this way because at
3498 -- least at the moment we don't compute this attribute right, and
3499 -- can silently give wrong results when the result gets large. Since
3500 -- this is all about large results, that's bad, so instead we only
3501 -- apply the check for constrained arrays, and manually compute the
3502 -- value of the attribute ???
3504 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
3506 Make_Raise_Storage_Error (Loc,
3509 Left_Opnd => Size_In_Storage_Elements (Etyp),
3511 Make_Integer_Literal (Loc,
3512 Intval => Uint_7 * (Uint_2 ** 29))),
3513 Reason => SE_Object_Too_Large));
3517 -- Handle case of qualified expression (other than optimization above)
3518 -- First apply constraint checks, because the bounds or discriminants
3519 -- in the aggregate might not match the subtype mark in the allocator.
3521 if Nkind (Expression (N)) = N_Qualified_Expression then
3522 Apply_Constraint_Check
3523 (Expression (Expression (N)), Etype (Expression (N)));
3525 Expand_Allocator_Expression (N);
3529 -- If the allocator is for a type which requires initialization, and
3530 -- there is no initial value (i.e. operand is a subtype indication
3531 -- rather than a qualified expression), then we must generate a call to
3532 -- the initialization routine using an expressions action node:
3534 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
3536 -- Here ptr_T is the pointer type for the allocator, and T is the
3537 -- subtype of the allocator. A special case arises if the designated
3538 -- type of the access type is a task or contains tasks. In this case
3539 -- the call to Init (Temp.all ...) is replaced by code that ensures
3540 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
3541 -- for details). In addition, if the type T is a task T, then the
3542 -- first argument to Init must be converted to the task record type.
3545 T : constant Entity_Id := Entity (Expression (N));
3553 Temp_Decl : Node_Id;
3554 Temp_Type : Entity_Id;
3555 Attach_Level : Uint;
3558 if No_Initialization (N) then
3561 -- Case of no initialization procedure present
3563 elsif not Has_Non_Null_Base_Init_Proc (T) then
3565 -- Case of simple initialization required
3567 if Needs_Simple_Initialization (T) then
3568 Check_Restriction (No_Default_Initialization, N);
3569 Rewrite (Expression (N),
3570 Make_Qualified_Expression (Loc,
3571 Subtype_Mark => New_Occurrence_Of (T, Loc),
3572 Expression => Get_Simple_Init_Val (T, N)));
3574 Analyze_And_Resolve (Expression (Expression (N)), T);
3575 Analyze_And_Resolve (Expression (N), T);
3576 Set_Paren_Count (Expression (Expression (N)), 1);
3577 Expand_N_Allocator (N);
3579 -- No initialization required
3585 -- Case of initialization procedure present, must be called
3588 Check_Restriction (No_Default_Initialization, N);
3590 if not Restriction_Active (No_Default_Initialization) then
3591 Init := Base_Init_Proc (T);
3593 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
3595 -- Construct argument list for the initialization routine call
3598 Make_Explicit_Dereference (Loc,
3599 Prefix => New_Reference_To (Temp, Loc));
3600 Set_Assignment_OK (Arg1);
3603 -- The initialization procedure expects a specific type. if the
3604 -- context is access to class wide, indicate that the object
3605 -- being allocated has the right specific type.
3607 if Is_Class_Wide_Type (Dtyp) then
3608 Arg1 := Unchecked_Convert_To (T, Arg1);
3611 -- If designated type is a concurrent type or if it is private
3612 -- type whose definition is a concurrent type, the first
3613 -- argument in the Init routine has to be unchecked conversion
3614 -- to the corresponding record type. If the designated type is
3615 -- a derived type, we also convert the argument to its root
3618 if Is_Concurrent_Type (T) then
3620 Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1);
3622 elsif Is_Private_Type (T)
3623 and then Present (Full_View (T))
3624 and then Is_Concurrent_Type (Full_View (T))
3627 Unchecked_Convert_To
3628 (Corresponding_Record_Type (Full_View (T)), Arg1);
3630 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
3632 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
3634 Arg1 := OK_Convert_To (Etype (Ftyp), Arg1);
3635 Set_Etype (Arg1, Ftyp);
3639 Args := New_List (Arg1);
3641 -- For the task case, pass the Master_Id of the access type as
3642 -- the value of the _Master parameter, and _Chain as the value
3643 -- of the _Chain parameter (_Chain will be defined as part of
3644 -- the generated code for the allocator).
3646 -- In Ada 2005, the context may be a function that returns an
3647 -- anonymous access type. In that case the Master_Id has been
3648 -- created when expanding the function declaration.
3650 if Has_Task (T) then
3651 if No (Master_Id (Base_Type (PtrT))) then
3653 -- If we have a non-library level task with restriction
3654 -- No_Task_Hierarchy set, then no point in expanding.
3656 if not Is_Library_Level_Entity (T)
3657 and then Restriction_Active (No_Task_Hierarchy)
3662 -- The designated type was an incomplete type, and the
3663 -- access type did not get expanded. Salvage it now.
3665 pragma Assert (Present (Parent (Base_Type (PtrT))));
3666 Expand_N_Full_Type_Declaration
3667 (Parent (Base_Type (PtrT)));
3670 -- If the context of the allocator is a declaration or an
3671 -- assignment, we can generate a meaningful image for it,
3672 -- even though subsequent assignments might remove the
3673 -- connection between task and entity. We build this image
3674 -- when the left-hand side is a simple variable, a simple
3675 -- indexed assignment or a simple selected component.
3677 if Nkind (Parent (N)) = N_Assignment_Statement then
3679 Nam : constant Node_Id := Name (Parent (N));
3682 if Is_Entity_Name (Nam) then
3684 Build_Task_Image_Decls
3687 (Entity (Nam), Sloc (Nam)), T);
3690 (Nam, N_Indexed_Component, N_Selected_Component)
3691 and then Is_Entity_Name (Prefix (Nam))
3694 Build_Task_Image_Decls
3695 (Loc, Nam, Etype (Prefix (Nam)));
3697 Decls := Build_Task_Image_Decls (Loc, T, T);
3701 elsif Nkind (Parent (N)) = N_Object_Declaration then
3703 Build_Task_Image_Decls
3704 (Loc, Defining_Identifier (Parent (N)), T);
3707 Decls := Build_Task_Image_Decls (Loc, T, T);
3712 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
3713 Append_To (Args, Make_Identifier (Loc, Name_uChain));
3715 Decl := Last (Decls);
3717 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
3719 -- Has_Task is false, Decls not used
3725 -- Add discriminants if discriminated type
3728 Dis : Boolean := False;
3732 if Has_Discriminants (T) then
3736 elsif Is_Private_Type (T)
3737 and then Present (Full_View (T))
3738 and then Has_Discriminants (Full_View (T))
3741 Typ := Full_View (T);
3746 -- If the allocated object will be constrained by the
3747 -- default values for discriminants, then build a subtype
3748 -- with those defaults, and change the allocated subtype
3749 -- to that. Note that this happens in fewer cases in Ada
3752 if not Is_Constrained (Typ)
3753 and then Present (Discriminant_Default_Value
3754 (First_Discriminant (Typ)))
3755 and then (Ada_Version < Ada_05
3757 not Has_Constrained_Partial_View (Typ))
3759 Typ := Build_Default_Subtype (Typ, N);
3760 Set_Expression (N, New_Reference_To (Typ, Loc));
3763 Discr := First_Elmt (Discriminant_Constraint (Typ));
3764 while Present (Discr) loop
3765 Nod := Node (Discr);
3766 Append (New_Copy_Tree (Node (Discr)), Args);
3768 -- AI-416: when the discriminant constraint is an
3769 -- anonymous access type make sure an accessibility
3770 -- check is inserted if necessary (3.10.2(22.q/2))
3772 if Ada_Version >= Ada_05
3774 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
3776 Apply_Accessibility_Check
3777 (Nod, Typ, Insert_Node => Nod);
3785 -- We set the allocator as analyzed so that when we analyze the
3786 -- expression actions node, we do not get an unwanted recursive
3787 -- expansion of the allocator expression.
3789 Set_Analyzed (N, True);
3790 Nod := Relocate_Node (N);
3792 -- Here is the transformation:
3794 -- output: Temp : constant ptr_T := new T;
3795 -- Init (Temp.all, ...);
3796 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
3797 -- <CTRL> Initialize (Finalizable (Temp.all));
3799 -- Here ptr_T is the pointer type for the allocator, and is the
3800 -- subtype of the allocator.
3803 Make_Object_Declaration (Loc,
3804 Defining_Identifier => Temp,
3805 Constant_Present => True,
3806 Object_Definition => New_Reference_To (Temp_Type, Loc),
3809 Set_Assignment_OK (Temp_Decl);
3810 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
3812 -- If the designated type is a task type or contains tasks,
3813 -- create block to activate created tasks, and insert
3814 -- declaration for Task_Image variable ahead of call.
3816 if Has_Task (T) then
3818 L : constant List_Id := New_List;
3821 Build_Task_Allocate_Block (L, Nod, Args);
3823 Insert_List_Before (First (Declarations (Blk)), Decls);
3824 Insert_Actions (N, L);
3829 Make_Procedure_Call_Statement (Loc,
3830 Name => New_Reference_To (Init, Loc),
3831 Parameter_Associations => Args));
3834 if Needs_Finalization (T) then
3836 -- Postpone the generation of a finalization call for the
3837 -- current allocator if it acts as a coextension.
3839 if Is_Dynamic_Coextension (N) then
3840 if No (Coextensions (N)) then
3841 Set_Coextensions (N, New_Elmt_List);
3844 Append_Elmt (New_Copy_Tree (Arg1), Coextensions (N));
3848 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
3850 -- Anonymous access types created for access parameters
3851 -- are attached to an explicitly constructed controller,
3852 -- which ensures that they can be finalized properly,
3853 -- even if their deallocation might not happen. The list
3854 -- associated with the controller is doubly-linked. For
3855 -- other anonymous access types, the object may end up
3856 -- on the global final list which is singly-linked.
3857 -- Work needed for access discriminants in Ada 2005 ???
3859 if Ekind (PtrT) = E_Anonymous_Access_Type then
3860 Attach_Level := Uint_1;
3862 Attach_Level := Uint_2;
3867 Ref => New_Copy_Tree (Arg1),
3870 With_Attach => Make_Integer_Literal (Loc,
3871 Intval => Attach_Level)));
3875 Rewrite (N, New_Reference_To (Temp, Loc));
3876 Analyze_And_Resolve (N, PtrT);
3881 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
3882 -- object that has been rewritten as a reference, we displace "this"
3883 -- to reference properly its secondary dispatch table.
3885 if Nkind (N) = N_Identifier
3886 and then Is_Interface (Dtyp)
3888 Displace_Allocator_Pointer (N);
3892 when RE_Not_Available =>
3894 end Expand_N_Allocator;
3896 -----------------------
3897 -- Expand_N_And_Then --
3898 -----------------------
3900 -- Expand into conditional expression if Actions present, and also deal
3901 -- with optimizing case of arguments being True or False.
3903 procedure Expand_N_And_Then (N : Node_Id) is
3904 Loc : constant Source_Ptr := Sloc (N);
3905 Typ : constant Entity_Id := Etype (N);
3906 Left : constant Node_Id := Left_Opnd (N);
3907 Right : constant Node_Id := Right_Opnd (N);
3911 -- Deal with non-standard booleans
3913 if Is_Boolean_Type (Typ) then
3914 Adjust_Condition (Left);
3915 Adjust_Condition (Right);
3916 Set_Etype (N, Standard_Boolean);
3919 -- Check for cases where left argument is known to be True or False
3921 if Compile_Time_Known_Value (Left) then
3923 -- If left argument is True, change (True and then Right) to Right.
3924 -- Any actions associated with Right will be executed unconditionally
3925 -- and can thus be inserted into the tree unconditionally.
3927 if Expr_Value_E (Left) = Standard_True then
3928 if Present (Actions (N)) then
3929 Insert_Actions (N, Actions (N));
3934 -- If left argument is False, change (False and then Right) to False.
3935 -- In this case we can forget the actions associated with Right,
3936 -- since they will never be executed.
3938 else pragma Assert (Expr_Value_E (Left) = Standard_False);
3939 Kill_Dead_Code (Right);
3940 Kill_Dead_Code (Actions (N));
3941 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3944 Adjust_Result_Type (N, Typ);
3948 -- If Actions are present, we expand
3950 -- left and then right
3954 -- if left then right else false end
3956 -- with the actions becoming the Then_Actions of the conditional
3957 -- expression. This conditional expression is then further expanded
3958 -- (and will eventually disappear)
3960 if Present (Actions (N)) then
3961 Actlist := Actions (N);
3963 Make_Conditional_Expression (Loc,
3964 Expressions => New_List (
3967 New_Occurrence_Of (Standard_False, Loc))));
3969 -- If the right part of the expression is a function call then it can
3970 -- be part of the expansion of the predefined equality operator of a
3971 -- tagged type and we may need to adjust its SCIL dispatching node.
3974 and then Nkind (Right) = N_Function_Call
3976 Adjust_SCIL_Node (N, Right);
3979 Set_Then_Actions (N, Actlist);
3980 Analyze_And_Resolve (N, Standard_Boolean);
3981 Adjust_Result_Type (N, Typ);
3985 -- No actions present, check for cases of right argument True/False
3987 if Compile_Time_Known_Value (Right) then
3989 -- Change (Left and then True) to Left. Note that we know there are
3990 -- no actions associated with the True operand, since we just checked
3991 -- for this case above.
3993 if Expr_Value_E (Right) = Standard_True then
3996 -- Change (Left and then False) to False, making sure to preserve any
3997 -- side effects associated with the Left operand.
3999 else pragma Assert (Expr_Value_E (Right) = Standard_False);
4000 Remove_Side_Effects (Left);
4001 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
4005 Adjust_Result_Type (N, Typ);
4006 end Expand_N_And_Then;
4008 -------------------------------------
4009 -- Expand_N_Conditional_Expression --
4010 -------------------------------------
4012 -- Expand into expression actions if then/else actions present
4014 procedure Expand_N_Conditional_Expression (N : Node_Id) is
4015 Loc : constant Source_Ptr := Sloc (N);
4016 Cond : constant Node_Id := First (Expressions (N));
4017 Thenx : constant Node_Id := Next (Cond);
4018 Elsex : constant Node_Id := Next (Thenx);
4019 Typ : constant Entity_Id := Etype (N);
4024 -- If either then or else actions are present, then given:
4026 -- if cond then then-expr else else-expr end
4028 -- we insert the following sequence of actions (using Insert_Actions):
4033 -- Cnn := then-expr;
4039 -- and replace the conditional expression by a reference to Cnn
4041 -- ??? Note: this expansion is wrong for limited types, since it does
4042 -- a copy of a limited value. Similarly it's wrong for unconstrained or
4043 -- class-wide types since in neither case can we have an uninitialized
4044 -- object declaration The proper fix would be to do the following
4047 -- Cnn : access typ;
4050 -- Cnn := then-expr'Unrestricted_Access;
4053 -- Cnn := else-expr'Unrestricted_Access;
4056 -- and replace the conditional expresion by a reference to Cnn.all ???
4058 if Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
4059 Cnn := Make_Temporary (Loc, 'C', N);
4062 Make_Implicit_If_Statement (N,
4063 Condition => Relocate_Node (Cond),
4065 Then_Statements => New_List (
4066 Make_Assignment_Statement (Sloc (Thenx),
4067 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
4068 Expression => Relocate_Node (Thenx))),
4070 Else_Statements => New_List (
4071 Make_Assignment_Statement (Sloc (Elsex),
4072 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
4073 Expression => Relocate_Node (Elsex))));
4075 -- Move the SLOC of the parent If statement to the newly created one
4076 -- and change it to the SLOC of the expression which, after
4077 -- expansion, will correspond to what is being evaluated.
4079 if Present (Parent (N))
4080 and then Nkind (Parent (N)) = N_If_Statement
4082 Set_Sloc (New_If, Sloc (Parent (N)));
4083 Set_Sloc (Parent (N), Loc);
4086 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
4087 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
4089 if Present (Then_Actions (N)) then
4091 (First (Then_Statements (New_If)), Then_Actions (N));
4094 if Present (Else_Actions (N)) then
4096 (First (Else_Statements (New_If)), Else_Actions (N));
4099 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
4102 Make_Object_Declaration (Loc,
4103 Defining_Identifier => Cnn,
4104 Object_Definition => New_Occurrence_Of (Typ, Loc)));
4106 Insert_Action (N, New_If);
4107 Analyze_And_Resolve (N, Typ);
4109 end Expand_N_Conditional_Expression;
4111 -----------------------------------
4112 -- Expand_N_Explicit_Dereference --
4113 -----------------------------------
4115 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
4117 -- Insert explicit dereference call for the checked storage pool case
4119 Insert_Dereference_Action (Prefix (N));
4120 end Expand_N_Explicit_Dereference;
4126 procedure Expand_N_In (N : Node_Id) is
4127 Loc : constant Source_Ptr := Sloc (N);
4128 Rtyp : constant Entity_Id := Etype (N);
4129 Lop : constant Node_Id := Left_Opnd (N);
4130 Rop : constant Node_Id := Right_Opnd (N);
4131 Static : constant Boolean := Is_OK_Static_Expression (N);
4133 procedure Expand_Set_Membership;
4134 -- For each disjunct we create a simple equality or membership test.
4135 -- The whole membership is rewritten as a short-circuit disjunction.
4137 ---------------------------
4138 -- Expand_Set_Membership --
4139 ---------------------------
4141 procedure Expand_Set_Membership is
4145 function Make_Cond (Alt : Node_Id) return Node_Id;
4146 -- If the alternative is a subtype mark, create a simple membership
4147 -- test. Otherwise create an equality test for it.
4153 function Make_Cond (Alt : Node_Id) return Node_Id is
4155 L : constant Node_Id := New_Copy (Lop);
4156 R : constant Node_Id := Relocate_Node (Alt);
4159 if Is_Entity_Name (Alt)
4160 and then Is_Type (Entity (Alt))
4163 Make_In (Sloc (Alt),
4167 Cond := Make_Op_Eq (Sloc (Alt),
4175 -- Start of proessing for Expand_N_In
4178 Alt := Last (Alternatives (N));
4179 Res := Make_Cond (Alt);
4182 while Present (Alt) loop
4184 Make_Or_Else (Sloc (Alt),
4185 Left_Opnd => Make_Cond (Alt),
4191 Analyze_And_Resolve (N, Standard_Boolean);
4192 end Expand_Set_Membership;
4194 procedure Substitute_Valid_Check;
4195 -- Replaces node N by Lop'Valid. This is done when we have an explicit
4196 -- test for the left operand being in range of its subtype.
4198 ----------------------------
4199 -- Substitute_Valid_Check --
4200 ----------------------------
4202 procedure Substitute_Valid_Check is
4205 Make_Attribute_Reference (Loc,
4206 Prefix => Relocate_Node (Lop),
4207 Attribute_Name => Name_Valid));
4209 Analyze_And_Resolve (N, Rtyp);
4211 Error_Msg_N ("?explicit membership test may be optimized away", N);
4212 Error_Msg_N ("\?use ''Valid attribute instead", N);
4214 end Substitute_Valid_Check;
4216 -- Start of processing for Expand_N_In
4220 if Present (Alternatives (N)) then
4221 Remove_Side_Effects (Lop);
4222 Expand_Set_Membership;
4226 -- Check case of explicit test for an expression in range of its
4227 -- subtype. This is suspicious usage and we replace it with a 'Valid
4228 -- test and give a warning.
4230 if Is_Scalar_Type (Etype (Lop))
4231 and then Nkind (Rop) in N_Has_Entity
4232 and then Etype (Lop) = Entity (Rop)
4233 and then Comes_From_Source (N)
4234 and then VM_Target = No_VM
4236 Substitute_Valid_Check;
4240 -- Do validity check on operands
4242 if Validity_Checks_On and Validity_Check_Operands then
4243 Ensure_Valid (Left_Opnd (N));
4244 Validity_Check_Range (Right_Opnd (N));
4247 -- Case of explicit range
4249 if Nkind (Rop) = N_Range then
4251 Lo : constant Node_Id := Low_Bound (Rop);
4252 Hi : constant Node_Id := High_Bound (Rop);
4254 Ltyp : constant Entity_Id := Etype (Lop);
4256 Lo_Orig : constant Node_Id := Original_Node (Lo);
4257 Hi_Orig : constant Node_Id := Original_Node (Hi);
4259 Lcheck : Compare_Result;
4260 Ucheck : Compare_Result;
4262 Warn1 : constant Boolean :=
4263 Constant_Condition_Warnings
4264 and then Comes_From_Source (N)
4265 and then not In_Instance;
4266 -- This must be true for any of the optimization warnings, we
4267 -- clearly want to give them only for source with the flag on.
4268 -- We also skip these warnings in an instance since it may be
4269 -- the case that different instantiations have different ranges.
4271 Warn2 : constant Boolean :=
4273 and then Nkind (Original_Node (Rop)) = N_Range
4274 and then Is_Integer_Type (Etype (Lo));
4275 -- For the case where only one bound warning is elided, we also
4276 -- insist on an explicit range and an integer type. The reason is
4277 -- that the use of enumeration ranges including an end point is
4278 -- common, as is the use of a subtype name, one of whose bounds
4279 -- is the same as the type of the expression.
4282 -- If test is explicit x'first .. x'last, replace by valid check
4284 if Is_Scalar_Type (Ltyp)
4285 and then Nkind (Lo_Orig) = N_Attribute_Reference
4286 and then Attribute_Name (Lo_Orig) = Name_First
4287 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
4288 and then Entity (Prefix (Lo_Orig)) = Ltyp
4289 and then Nkind (Hi_Orig) = N_Attribute_Reference
4290 and then Attribute_Name (Hi_Orig) = Name_Last
4291 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
4292 and then Entity (Prefix (Hi_Orig)) = Ltyp
4293 and then Comes_From_Source (N)
4294 and then VM_Target = No_VM
4296 Substitute_Valid_Check;
4300 -- If bounds of type are known at compile time, and the end points
4301 -- are known at compile time and identical, this is another case
4302 -- for substituting a valid test. We only do this for discrete
4303 -- types, since it won't arise in practice for float types.
4305 if Comes_From_Source (N)
4306 and then Is_Discrete_Type (Ltyp)
4307 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
4308 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
4309 and then Compile_Time_Known_Value (Lo)
4310 and then Compile_Time_Known_Value (Hi)
4311 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
4312 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
4314 -- Kill warnings in instances, since they may be cases where we
4315 -- have a test in the generic that makes sense with some types
4316 -- and not with other types.
4318 and then not In_Instance
4320 Substitute_Valid_Check;
4324 -- If we have an explicit range, do a bit of optimization based
4325 -- on range analysis (we may be able to kill one or both checks).
4327 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
4328 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
4330 -- If either check is known to fail, replace result by False since
4331 -- the other check does not matter. Preserve the static flag for
4332 -- legality checks, because we are constant-folding beyond RM 4.9.
4334 if Lcheck = LT or else Ucheck = GT then
4336 Error_Msg_N ("?range test optimized away", N);
4337 Error_Msg_N ("\?value is known to be out of range", N);
4341 New_Reference_To (Standard_False, Loc));
4342 Analyze_And_Resolve (N, Rtyp);
4343 Set_Is_Static_Expression (N, Static);
4347 -- If both checks are known to succeed, replace result by True,
4348 -- since we know we are in range.
4350 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4352 Error_Msg_N ("?range test optimized away", N);
4353 Error_Msg_N ("\?value is known to be in range", N);
4357 New_Reference_To (Standard_True, Loc));
4358 Analyze_And_Resolve (N, Rtyp);
4359 Set_Is_Static_Expression (N, Static);
4363 -- If lower bound check succeeds and upper bound check is not
4364 -- known to succeed or fail, then replace the range check with
4365 -- a comparison against the upper bound.
4367 elsif Lcheck in Compare_GE then
4368 if Warn2 and then not In_Instance then
4369 Error_Msg_N ("?lower bound test optimized away", Lo);
4370 Error_Msg_N ("\?value is known to be in range", Lo);
4376 Right_Opnd => High_Bound (Rop)));
4377 Analyze_And_Resolve (N, Rtyp);
4381 -- If upper bound check succeeds and lower bound check is not
4382 -- known to succeed or fail, then replace the range check with
4383 -- a comparison against the lower bound.
4385 elsif Ucheck in Compare_LE then
4386 if Warn2 and then not In_Instance then
4387 Error_Msg_N ("?upper bound test optimized away", Hi);
4388 Error_Msg_N ("\?value is known to be in range", Hi);
4394 Right_Opnd => Low_Bound (Rop)));
4395 Analyze_And_Resolve (N, Rtyp);
4400 -- We couldn't optimize away the range check, but there is one
4401 -- more issue. If we are checking constant conditionals, then we
4402 -- see if we can determine the outcome assuming everything is
4403 -- valid, and if so give an appropriate warning.
4405 if Warn1 and then not Assume_No_Invalid_Values then
4406 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
4407 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
4409 -- Result is out of range for valid value
4411 if Lcheck = LT or else Ucheck = GT then
4413 ("?value can only be in range if it is invalid", N);
4415 -- Result is in range for valid value
4417 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
4419 ("?value can only be out of range if it is invalid", N);
4421 -- Lower bound check succeeds if value is valid
4423 elsif Warn2 and then Lcheck in Compare_GE then
4425 ("?lower bound check only fails if it is invalid", Lo);
4427 -- Upper bound check succeeds if value is valid
4429 elsif Warn2 and then Ucheck in Compare_LE then
4431 ("?upper bound check only fails for invalid values", Hi);
4436 -- For all other cases of an explicit range, nothing to be done
4440 -- Here right operand is a subtype mark
4444 Typ : Entity_Id := Etype (Rop);
4445 Is_Acc : constant Boolean := Is_Access_Type (Typ);
4446 Obj : Node_Id := Lop;
4447 Cond : Node_Id := Empty;
4450 Remove_Side_Effects (Obj);
4452 -- For tagged type, do tagged membership operation
4454 if Is_Tagged_Type (Typ) then
4456 -- No expansion will be performed when VM_Target, as the VM
4457 -- back-ends will handle the membership tests directly (tags
4458 -- are not explicitly represented in Java objects, so the
4459 -- normal tagged membership expansion is not what we want).
4461 if Tagged_Type_Expansion then
4462 Rewrite (N, Tagged_Membership (N));
4463 Analyze_And_Resolve (N, Rtyp);
4468 -- If type is scalar type, rewrite as x in t'first .. t'last.
4469 -- This reason we do this is that the bounds may have the wrong
4470 -- type if they come from the original type definition. Also this
4471 -- way we get all the processing above for an explicit range.
4473 elsif Is_Scalar_Type (Typ) then
4477 Make_Attribute_Reference (Loc,
4478 Attribute_Name => Name_First,
4479 Prefix => New_Reference_To (Typ, Loc)),
4482 Make_Attribute_Reference (Loc,
4483 Attribute_Name => Name_Last,
4484 Prefix => New_Reference_To (Typ, Loc))));
4485 Analyze_And_Resolve (N, Rtyp);
4488 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4489 -- a membership test if the subtype mark denotes a constrained
4490 -- Unchecked_Union subtype and the expression lacks inferable
4493 elsif Is_Unchecked_Union (Base_Type (Typ))
4494 and then Is_Constrained (Typ)
4495 and then not Has_Inferable_Discriminants (Lop)
4498 Make_Raise_Program_Error (Loc,
4499 Reason => PE_Unchecked_Union_Restriction));
4501 -- Prevent Gigi from generating incorrect code by rewriting
4502 -- the test as a standard False.
4505 New_Occurrence_Of (Standard_False, Loc));
4510 -- Here we have a non-scalar type
4513 Typ := Designated_Type (Typ);
4516 if not Is_Constrained (Typ) then
4518 New_Reference_To (Standard_True, Loc));
4519 Analyze_And_Resolve (N, Rtyp);
4521 -- For the constrained array case, we have to check the subscripts
4522 -- for an exact match if the lengths are non-zero (the lengths
4523 -- must match in any case).
4525 elsif Is_Array_Type (Typ) then
4527 Check_Subscripts : declare
4528 function Construct_Attribute_Reference
4531 Dim : Nat) return Node_Id;
4532 -- Build attribute reference E'Nam(Dim)
4534 -----------------------------------
4535 -- Construct_Attribute_Reference --
4536 -----------------------------------
4538 function Construct_Attribute_Reference
4541 Dim : Nat) return Node_Id
4545 Make_Attribute_Reference (Loc,
4547 Attribute_Name => Nam,
4548 Expressions => New_List (
4549 Make_Integer_Literal (Loc, Dim)));
4550 end Construct_Attribute_Reference;
4552 -- Start of processing for Check_Subscripts
4555 for J in 1 .. Number_Dimensions (Typ) loop
4556 Evolve_And_Then (Cond,
4559 Construct_Attribute_Reference
4560 (Duplicate_Subexpr_No_Checks (Obj),
4563 Construct_Attribute_Reference
4564 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
4566 Evolve_And_Then (Cond,
4569 Construct_Attribute_Reference
4570 (Duplicate_Subexpr_No_Checks (Obj),
4573 Construct_Attribute_Reference
4574 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
4583 Right_Opnd => Make_Null (Loc)),
4584 Right_Opnd => Cond);
4588 Analyze_And_Resolve (N, Rtyp);
4589 end Check_Subscripts;
4591 -- These are the cases where constraint checks may be required,
4592 -- e.g. records with possible discriminants
4595 -- Expand the test into a series of discriminant comparisons.
4596 -- The expression that is built is the negation of the one that
4597 -- is used for checking discriminant constraints.
4599 Obj := Relocate_Node (Left_Opnd (N));
4601 if Has_Discriminants (Typ) then
4602 Cond := Make_Op_Not (Loc,
4603 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
4606 Cond := Make_Or_Else (Loc,
4610 Right_Opnd => Make_Null (Loc)),
4611 Right_Opnd => Cond);
4615 Cond := New_Occurrence_Of (Standard_True, Loc);
4619 Analyze_And_Resolve (N, Rtyp);
4625 --------------------------------
4626 -- Expand_N_Indexed_Component --
4627 --------------------------------
4629 procedure Expand_N_Indexed_Component (N : Node_Id) is
4630 Loc : constant Source_Ptr := Sloc (N);
4631 Typ : constant Entity_Id := Etype (N);
4632 P : constant Node_Id := Prefix (N);
4633 T : constant Entity_Id := Etype (P);
4636 -- A special optimization, if we have an indexed component that is
4637 -- selecting from a slice, then we can eliminate the slice, since, for
4638 -- example, x (i .. j)(k) is identical to x(k). The only difference is
4639 -- the range check required by the slice. The range check for the slice
4640 -- itself has already been generated. The range check for the
4641 -- subscripting operation is ensured by converting the subject to
4642 -- the subtype of the slice.
4644 -- This optimization not only generates better code, avoiding slice
4645 -- messing especially in the packed case, but more importantly bypasses
4646 -- some problems in handling this peculiar case, for example, the issue
4647 -- of dealing specially with object renamings.
4649 if Nkind (P) = N_Slice then
4651 Make_Indexed_Component (Loc,
4652 Prefix => Prefix (P),
4653 Expressions => New_List (
4655 (Etype (First_Index (Etype (P))),
4656 First (Expressions (N))))));
4657 Analyze_And_Resolve (N, Typ);
4661 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
4662 -- function, then additional actuals must be passed.
4664 if Ada_Version >= Ada_05
4665 and then Is_Build_In_Place_Function_Call (P)
4667 Make_Build_In_Place_Call_In_Anonymous_Context (P);
4670 -- If the prefix is an access type, then we unconditionally rewrite if
4671 -- as an explicit dereference. This simplifies processing for several
4672 -- cases, including packed array cases and certain cases in which checks
4673 -- must be generated. We used to try to do this only when it was
4674 -- necessary, but it cleans up the code to do it all the time.
4676 if Is_Access_Type (T) then
4677 Insert_Explicit_Dereference (P);
4678 Analyze_And_Resolve (P, Designated_Type (T));
4681 -- Generate index and validity checks
4683 Generate_Index_Checks (N);
4685 if Validity_Checks_On and then Validity_Check_Subscripts then
4686 Apply_Subscript_Validity_Checks (N);
4689 -- All done for the non-packed case
4691 if not Is_Packed (Etype (Prefix (N))) then
4695 -- For packed arrays that are not bit-packed (i.e. the case of an array
4696 -- with one or more index types with a non-contiguous enumeration type),
4697 -- we can always use the normal packed element get circuit.
4699 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
4700 Expand_Packed_Element_Reference (N);
4704 -- For a reference to a component of a bit packed array, we have to
4705 -- convert it to a reference to the corresponding Packed_Array_Type.
4706 -- We only want to do this for simple references, and not for:
4708 -- Left side of assignment, or prefix of left side of assignment, or
4709 -- prefix of the prefix, to handle packed arrays of packed arrays,
4710 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
4712 -- Renaming objects in renaming associations
4713 -- This case is handled when a use of the renamed variable occurs
4715 -- Actual parameters for a procedure call
4716 -- This case is handled in Exp_Ch6.Expand_Actuals
4718 -- The second expression in a 'Read attribute reference
4720 -- The prefix of an address or size attribute reference
4722 -- The following circuit detects these exceptions
4725 Child : Node_Id := N;
4726 Parnt : Node_Id := Parent (N);
4730 if Nkind (Parnt) = N_Unchecked_Expression then
4733 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
4734 N_Procedure_Call_Statement)
4735 or else (Nkind (Parnt) = N_Parameter_Association
4737 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
4741 elsif Nkind (Parnt) = N_Attribute_Reference
4742 and then (Attribute_Name (Parnt) = Name_Address
4744 Attribute_Name (Parnt) = Name_Size)
4745 and then Prefix (Parnt) = Child
4749 elsif Nkind (Parnt) = N_Assignment_Statement
4750 and then Name (Parnt) = Child
4754 -- If the expression is an index of an indexed component, it must
4755 -- be expanded regardless of context.
4757 elsif Nkind (Parnt) = N_Indexed_Component
4758 and then Child /= Prefix (Parnt)
4760 Expand_Packed_Element_Reference (N);
4763 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
4764 and then Name (Parent (Parnt)) = Parnt
4768 elsif Nkind (Parnt) = N_Attribute_Reference
4769 and then Attribute_Name (Parnt) = Name_Read
4770 and then Next (First (Expressions (Parnt))) = Child
4774 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
4775 and then Prefix (Parnt) = Child
4780 Expand_Packed_Element_Reference (N);
4784 -- Keep looking up tree for unchecked expression, or if we are the
4785 -- prefix of a possible assignment left side.
4788 Parnt := Parent (Child);
4791 end Expand_N_Indexed_Component;
4793 ---------------------
4794 -- Expand_N_Not_In --
4795 ---------------------
4797 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
4798 -- can be done. This avoids needing to duplicate this expansion code.
4800 procedure Expand_N_Not_In (N : Node_Id) is
4801 Loc : constant Source_Ptr := Sloc (N);
4802 Typ : constant Entity_Id := Etype (N);
4803 Cfs : constant Boolean := Comes_From_Source (N);
4810 Left_Opnd => Left_Opnd (N),
4811 Right_Opnd => Right_Opnd (N))));
4813 -- If this is a set membership, preserve list of alternatives
4815 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
4817 -- We want this to appear as coming from source if original does (see
4818 -- transformations in Expand_N_In).
4820 Set_Comes_From_Source (N, Cfs);
4821 Set_Comes_From_Source (Right_Opnd (N), Cfs);
4823 -- Now analyze transformed node
4825 Analyze_And_Resolve (N, Typ);
4826 end Expand_N_Not_In;
4832 -- The only replacement required is for the case of a null of type that is
4833 -- an access to protected subprogram. We represent such access values as a
4834 -- record, and so we must replace the occurrence of null by the equivalent
4835 -- record (with a null address and a null pointer in it), so that the
4836 -- backend creates the proper value.
4838 procedure Expand_N_Null (N : Node_Id) is
4839 Loc : constant Source_Ptr := Sloc (N);
4840 Typ : constant Entity_Id := Etype (N);
4844 if Is_Access_Protected_Subprogram_Type (Typ) then
4846 Make_Aggregate (Loc,
4847 Expressions => New_List (
4848 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
4852 Analyze_And_Resolve (N, Equivalent_Type (Typ));
4854 -- For subsequent semantic analysis, the node must retain its type.
4855 -- Gigi in any case replaces this type by the corresponding record
4856 -- type before processing the node.
4862 when RE_Not_Available =>
4866 ---------------------
4867 -- Expand_N_Op_Abs --
4868 ---------------------
4870 procedure Expand_N_Op_Abs (N : Node_Id) is
4871 Loc : constant Source_Ptr := Sloc (N);
4872 Expr : constant Node_Id := Right_Opnd (N);
4875 Unary_Op_Validity_Checks (N);
4877 -- Deal with software overflow checking
4879 if not Backend_Overflow_Checks_On_Target
4880 and then Is_Signed_Integer_Type (Etype (N))
4881 and then Do_Overflow_Check (N)
4883 -- The only case to worry about is when the argument is equal to the
4884 -- largest negative number, so what we do is to insert the check:
4886 -- [constraint_error when Expr = typ'Base'First]
4888 -- with the usual Duplicate_Subexpr use coding for expr
4891 Make_Raise_Constraint_Error (Loc,
4894 Left_Opnd => Duplicate_Subexpr (Expr),
4896 Make_Attribute_Reference (Loc,
4898 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
4899 Attribute_Name => Name_First)),
4900 Reason => CE_Overflow_Check_Failed));
4903 -- Vax floating-point types case
4905 if Vax_Float (Etype (N)) then
4906 Expand_Vax_Arith (N);
4908 end Expand_N_Op_Abs;
4910 ---------------------
4911 -- Expand_N_Op_Add --
4912 ---------------------
4914 procedure Expand_N_Op_Add (N : Node_Id) is
4915 Typ : constant Entity_Id := Etype (N);
4918 Binary_Op_Validity_Checks (N);
4920 -- N + 0 = 0 + N = N for integer types
4922 if Is_Integer_Type (Typ) then
4923 if Compile_Time_Known_Value (Right_Opnd (N))
4924 and then Expr_Value (Right_Opnd (N)) = Uint_0
4926 Rewrite (N, Left_Opnd (N));
4929 elsif Compile_Time_Known_Value (Left_Opnd (N))
4930 and then Expr_Value (Left_Opnd (N)) = Uint_0
4932 Rewrite (N, Right_Opnd (N));
4937 -- Arithmetic overflow checks for signed integer/fixed point types
4939 if Is_Signed_Integer_Type (Typ)
4940 or else Is_Fixed_Point_Type (Typ)
4942 Apply_Arithmetic_Overflow_Check (N);
4945 -- Vax floating-point types case
4947 elsif Vax_Float (Typ) then
4948 Expand_Vax_Arith (N);
4950 end Expand_N_Op_Add;
4952 ---------------------
4953 -- Expand_N_Op_And --
4954 ---------------------
4956 procedure Expand_N_Op_And (N : Node_Id) is
4957 Typ : constant Entity_Id := Etype (N);
4960 Binary_Op_Validity_Checks (N);
4962 if Is_Array_Type (Etype (N)) then
4963 Expand_Boolean_Operator (N);
4965 elsif Is_Boolean_Type (Etype (N)) then
4966 Adjust_Condition (Left_Opnd (N));
4967 Adjust_Condition (Right_Opnd (N));
4968 Set_Etype (N, Standard_Boolean);
4969 Adjust_Result_Type (N, Typ);
4971 end Expand_N_Op_And;
4973 ------------------------
4974 -- Expand_N_Op_Concat --
4975 ------------------------
4977 procedure Expand_N_Op_Concat (N : Node_Id) is
4979 -- List of operands to be concatenated
4982 -- Node which is to be replaced by the result of concatenating the nodes
4983 -- in the list Opnds.
4986 -- Ensure validity of both operands
4988 Binary_Op_Validity_Checks (N);
4990 -- If we are the left operand of a concatenation higher up the tree,
4991 -- then do nothing for now, since we want to deal with a series of
4992 -- concatenations as a unit.
4994 if Nkind (Parent (N)) = N_Op_Concat
4995 and then N = Left_Opnd (Parent (N))
5000 -- We get here with a concatenation whose left operand may be a
5001 -- concatenation itself with a consistent type. We need to process
5002 -- these concatenation operands from left to right, which means
5003 -- from the deepest node in the tree to the highest node.
5006 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
5007 Cnode := Left_Opnd (Cnode);
5010 -- Now Opnd is the deepest Opnd, and its parents are the concatenation
5011 -- nodes above, so now we process bottom up, doing the operations. We
5012 -- gather a string that is as long as possible up to five operands
5014 -- The outer loop runs more than once if more than one concatenation
5015 -- type is involved.
5018 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
5019 Set_Parent (Opnds, N);
5021 -- The inner loop gathers concatenation operands
5023 Inner : while Cnode /= N
5024 and then Base_Type (Etype (Cnode)) =
5025 Base_Type (Etype (Parent (Cnode)))
5027 Cnode := Parent (Cnode);
5028 Append (Right_Opnd (Cnode), Opnds);
5031 Expand_Concatenate (Cnode, Opnds);
5033 exit Outer when Cnode = N;
5034 Cnode := Parent (Cnode);
5036 end Expand_N_Op_Concat;
5038 ------------------------
5039 -- Expand_N_Op_Divide --
5040 ------------------------
5042 procedure Expand_N_Op_Divide (N : Node_Id) is
5043 Loc : constant Source_Ptr := Sloc (N);
5044 Lopnd : constant Node_Id := Left_Opnd (N);
5045 Ropnd : constant Node_Id := Right_Opnd (N);
5046 Ltyp : constant Entity_Id := Etype (Lopnd);
5047 Rtyp : constant Entity_Id := Etype (Ropnd);
5048 Typ : Entity_Id := Etype (N);
5049 Rknow : constant Boolean := Is_Integer_Type (Typ)
5051 Compile_Time_Known_Value (Ropnd);
5055 Binary_Op_Validity_Checks (N);
5058 Rval := Expr_Value (Ropnd);
5061 -- N / 1 = N for integer types
5063 if Rknow and then Rval = Uint_1 then
5068 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
5069 -- Is_Power_Of_2_For_Shift is set means that we know that our left
5070 -- operand is an unsigned integer, as required for this to work.
5072 if Nkind (Ropnd) = N_Op_Expon
5073 and then Is_Power_Of_2_For_Shift (Ropnd)
5075 -- We cannot do this transformation in configurable run time mode if we
5076 -- have 64-bit -- integers and long shifts are not available.
5080 or else Support_Long_Shifts_On_Target)
5083 Make_Op_Shift_Right (Loc,
5086 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
5087 Analyze_And_Resolve (N, Typ);
5091 -- Do required fixup of universal fixed operation
5093 if Typ = Universal_Fixed then
5094 Fixup_Universal_Fixed_Operation (N);
5098 -- Divisions with fixed-point results
5100 if Is_Fixed_Point_Type (Typ) then
5102 -- No special processing if Treat_Fixed_As_Integer is set, since
5103 -- from a semantic point of view such operations are simply integer
5104 -- operations and will be treated that way.
5106 if not Treat_Fixed_As_Integer (N) then
5107 if Is_Integer_Type (Rtyp) then
5108 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
5110 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
5114 -- Other cases of division of fixed-point operands. Again we exclude the
5115 -- case where Treat_Fixed_As_Integer is set.
5117 elsif (Is_Fixed_Point_Type (Ltyp) or else
5118 Is_Fixed_Point_Type (Rtyp))
5119 and then not Treat_Fixed_As_Integer (N)
5121 if Is_Integer_Type (Typ) then
5122 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
5124 pragma Assert (Is_Floating_Point_Type (Typ));
5125 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
5128 -- Mixed-mode operations can appear in a non-static universal context,
5129 -- in which case the integer argument must be converted explicitly.
5131 elsif Typ = Universal_Real
5132 and then Is_Integer_Type (Rtyp)
5135 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
5137 Analyze_And_Resolve (Ropnd, Universal_Real);
5139 elsif Typ = Universal_Real
5140 and then Is_Integer_Type (Ltyp)
5143 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
5145 Analyze_And_Resolve (Lopnd, Universal_Real);
5147 -- Non-fixed point cases, do integer zero divide and overflow checks
5149 elsif Is_Integer_Type (Typ) then
5150 Apply_Divide_Check (N);
5152 -- Check for 64-bit division available, or long shifts if the divisor
5153 -- is a small power of 2 (since such divides will be converted into
5156 if Esize (Ltyp) > 32
5157 and then not Support_64_Bit_Divides_On_Target
5160 or else not Support_Long_Shifts_On_Target
5161 or else (Rval /= Uint_2 and then
5162 Rval /= Uint_4 and then
5163 Rval /= Uint_8 and then
5164 Rval /= Uint_16 and then
5165 Rval /= Uint_32 and then
5168 Error_Msg_CRT ("64-bit division", N);
5171 -- Deal with Vax_Float
5173 elsif Vax_Float (Typ) then
5174 Expand_Vax_Arith (N);
5177 end Expand_N_Op_Divide;
5179 --------------------
5180 -- Expand_N_Op_Eq --
5181 --------------------
5183 procedure Expand_N_Op_Eq (N : Node_Id) is
5184 Loc : constant Source_Ptr := Sloc (N);
5185 Typ : constant Entity_Id := Etype (N);
5186 Lhs : constant Node_Id := Left_Opnd (N);
5187 Rhs : constant Node_Id := Right_Opnd (N);
5188 Bodies : constant List_Id := New_List;
5189 A_Typ : constant Entity_Id := Etype (Lhs);
5191 Typl : Entity_Id := A_Typ;
5192 Op_Name : Entity_Id;
5195 procedure Build_Equality_Call (Eq : Entity_Id);
5196 -- If a constructed equality exists for the type or for its parent,
5197 -- build and analyze call, adding conversions if the operation is
5200 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
5201 -- Determines whether a type has a subcomponent of an unconstrained
5202 -- Unchecked_Union subtype. Typ is a record type.
5204 -------------------------
5205 -- Build_Equality_Call --
5206 -------------------------
5208 procedure Build_Equality_Call (Eq : Entity_Id) is
5209 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
5210 L_Exp : Node_Id := Relocate_Node (Lhs);
5211 R_Exp : Node_Id := Relocate_Node (Rhs);
5214 if Base_Type (Op_Type) /= Base_Type (A_Typ)
5215 and then not Is_Class_Wide_Type (A_Typ)
5217 L_Exp := OK_Convert_To (Op_Type, L_Exp);
5218 R_Exp := OK_Convert_To (Op_Type, R_Exp);
5221 -- If we have an Unchecked_Union, we need to add the inferred
5222 -- discriminant values as actuals in the function call. At this
5223 -- point, the expansion has determined that both operands have
5224 -- inferable discriminants.
5226 if Is_Unchecked_Union (Op_Type) then
5228 Lhs_Type : constant Node_Id := Etype (L_Exp);
5229 Rhs_Type : constant Node_Id := Etype (R_Exp);
5230 Lhs_Discr_Val : Node_Id;
5231 Rhs_Discr_Val : Node_Id;
5234 -- Per-object constrained selected components require special
5235 -- attention. If the enclosing scope of the component is an
5236 -- Unchecked_Union, we cannot reference its discriminants
5237 -- directly. This is why we use the two extra parameters of
5238 -- the equality function of the enclosing Unchecked_Union.
5240 -- type UU_Type (Discr : Integer := 0) is
5243 -- pragma Unchecked_Union (UU_Type);
5245 -- 1. Unchecked_Union enclosing record:
5247 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
5249 -- Comp : UU_Type (Discr);
5251 -- end Enclosing_UU_Type;
5252 -- pragma Unchecked_Union (Enclosing_UU_Type);
5254 -- Obj1 : Enclosing_UU_Type;
5255 -- Obj2 : Enclosing_UU_Type (1);
5257 -- [. . .] Obj1 = Obj2 [. . .]
5261 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
5263 -- A and B are the formal parameters of the equality function
5264 -- of Enclosing_UU_Type. The function always has two extra
5265 -- formals to capture the inferred discriminant values.
5267 -- 2. Non-Unchecked_Union enclosing record:
5270 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
5273 -- Comp : UU_Type (Discr);
5275 -- end Enclosing_Non_UU_Type;
5277 -- Obj1 : Enclosing_Non_UU_Type;
5278 -- Obj2 : Enclosing_Non_UU_Type (1);
5280 -- ... Obj1 = Obj2 ...
5284 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
5285 -- obj1.discr, obj2.discr)) then
5287 -- In this case we can directly reference the discriminants of
5288 -- the enclosing record.
5292 if Nkind (Lhs) = N_Selected_Component
5293 and then Has_Per_Object_Constraint
5294 (Entity (Selector_Name (Lhs)))
5296 -- Enclosing record is an Unchecked_Union, use formal A
5298 if Is_Unchecked_Union (Scope
5299 (Entity (Selector_Name (Lhs))))
5302 Make_Identifier (Loc,
5305 -- Enclosing record is of a non-Unchecked_Union type, it is
5306 -- possible to reference the discriminant.
5310 Make_Selected_Component (Loc,
5311 Prefix => Prefix (Lhs),
5314 (Get_Discriminant_Value
5315 (First_Discriminant (Lhs_Type),
5317 Stored_Constraint (Lhs_Type))));
5320 -- Comment needed here ???
5323 -- Infer the discriminant value
5327 (Get_Discriminant_Value
5328 (First_Discriminant (Lhs_Type),
5330 Stored_Constraint (Lhs_Type)));
5335 if Nkind (Rhs) = N_Selected_Component
5336 and then Has_Per_Object_Constraint
5337 (Entity (Selector_Name (Rhs)))
5339 if Is_Unchecked_Union
5340 (Scope (Entity (Selector_Name (Rhs))))
5343 Make_Identifier (Loc,
5348 Make_Selected_Component (Loc,
5349 Prefix => Prefix (Rhs),
5351 New_Copy (Get_Discriminant_Value (
5352 First_Discriminant (Rhs_Type),
5354 Stored_Constraint (Rhs_Type))));
5359 New_Copy (Get_Discriminant_Value (
5360 First_Discriminant (Rhs_Type),
5362 Stored_Constraint (Rhs_Type)));
5367 Make_Function_Call (Loc,
5368 Name => New_Reference_To (Eq, Loc),
5369 Parameter_Associations => New_List (
5376 -- Normal case, not an unchecked union
5380 Make_Function_Call (Loc,
5381 Name => New_Reference_To (Eq, Loc),
5382 Parameter_Associations => New_List (L_Exp, R_Exp)));
5385 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5386 end Build_Equality_Call;
5388 ------------------------------------
5389 -- Has_Unconstrained_UU_Component --
5390 ------------------------------------
5392 function Has_Unconstrained_UU_Component
5393 (Typ : Node_Id) return Boolean
5395 Tdef : constant Node_Id :=
5396 Type_Definition (Declaration_Node (Base_Type (Typ)));
5400 function Component_Is_Unconstrained_UU
5401 (Comp : Node_Id) return Boolean;
5402 -- Determines whether the subtype of the component is an
5403 -- unconstrained Unchecked_Union.
5405 function Variant_Is_Unconstrained_UU
5406 (Variant : Node_Id) return Boolean;
5407 -- Determines whether a component of the variant has an unconstrained
5408 -- Unchecked_Union subtype.
5410 -----------------------------------
5411 -- Component_Is_Unconstrained_UU --
5412 -----------------------------------
5414 function Component_Is_Unconstrained_UU
5415 (Comp : Node_Id) return Boolean
5418 if Nkind (Comp) /= N_Component_Declaration then
5423 Sindic : constant Node_Id :=
5424 Subtype_Indication (Component_Definition (Comp));
5427 -- Unconstrained nominal type. In the case of a constraint
5428 -- present, the node kind would have been N_Subtype_Indication.
5430 if Nkind (Sindic) = N_Identifier then
5431 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
5436 end Component_Is_Unconstrained_UU;
5438 ---------------------------------
5439 -- Variant_Is_Unconstrained_UU --
5440 ---------------------------------
5442 function Variant_Is_Unconstrained_UU
5443 (Variant : Node_Id) return Boolean
5445 Clist : constant Node_Id := Component_List (Variant);
5448 if Is_Empty_List (Component_Items (Clist)) then
5452 -- We only need to test one component
5455 Comp : Node_Id := First (Component_Items (Clist));
5458 while Present (Comp) loop
5459 if Component_Is_Unconstrained_UU (Comp) then
5467 -- None of the components withing the variant were of
5468 -- unconstrained Unchecked_Union type.
5471 end Variant_Is_Unconstrained_UU;
5473 -- Start of processing for Has_Unconstrained_UU_Component
5476 if Null_Present (Tdef) then
5480 Clist := Component_List (Tdef);
5481 Vpart := Variant_Part (Clist);
5483 -- Inspect available components
5485 if Present (Component_Items (Clist)) then
5487 Comp : Node_Id := First (Component_Items (Clist));
5490 while Present (Comp) loop
5492 -- One component is sufficient
5494 if Component_Is_Unconstrained_UU (Comp) then
5503 -- Inspect available components withing variants
5505 if Present (Vpart) then
5507 Variant : Node_Id := First (Variants (Vpart));
5510 while Present (Variant) loop
5512 -- One component within a variant is sufficient
5514 if Variant_Is_Unconstrained_UU (Variant) then
5523 -- Neither the available components, nor the components inside the
5524 -- variant parts were of an unconstrained Unchecked_Union subtype.
5527 end Has_Unconstrained_UU_Component;
5529 -- Start of processing for Expand_N_Op_Eq
5532 Binary_Op_Validity_Checks (N);
5534 if Ekind (Typl) = E_Private_Type then
5535 Typl := Underlying_Type (Typl);
5536 elsif Ekind (Typl) = E_Private_Subtype then
5537 Typl := Underlying_Type (Base_Type (Typl));
5542 -- It may happen in error situations that the underlying type is not
5543 -- set. The error will be detected later, here we just defend the
5550 Typl := Base_Type (Typl);
5552 -- Boolean types (requiring handling of non-standard case)
5554 if Is_Boolean_Type (Typl) then
5555 Adjust_Condition (Left_Opnd (N));
5556 Adjust_Condition (Right_Opnd (N));
5557 Set_Etype (N, Standard_Boolean);
5558 Adjust_Result_Type (N, Typ);
5562 elsif Is_Array_Type (Typl) then
5564 -- If we are doing full validity checking, and it is possible for the
5565 -- array elements to be invalid then expand out array comparisons to
5566 -- make sure that we check the array elements.
5568 if Validity_Check_Operands
5569 and then not Is_Known_Valid (Component_Type (Typl))
5572 Save_Force_Validity_Checks : constant Boolean :=
5573 Force_Validity_Checks;
5575 Force_Validity_Checks := True;
5577 Expand_Array_Equality
5579 Relocate_Node (Lhs),
5580 Relocate_Node (Rhs),
5583 Insert_Actions (N, Bodies);
5584 Analyze_And_Resolve (N, Standard_Boolean);
5585 Force_Validity_Checks := Save_Force_Validity_Checks;
5588 -- Packed case where both operands are known aligned
5590 elsif Is_Bit_Packed_Array (Typl)
5591 and then not Is_Possibly_Unaligned_Object (Lhs)
5592 and then not Is_Possibly_Unaligned_Object (Rhs)
5594 Expand_Packed_Eq (N);
5596 -- Where the component type is elementary we can use a block bit
5597 -- comparison (if supported on the target) exception in the case
5598 -- of floating-point (negative zero issues require element by
5599 -- element comparison), and atomic types (where we must be sure
5600 -- to load elements independently) and possibly unaligned arrays.
5602 elsif Is_Elementary_Type (Component_Type (Typl))
5603 and then not Is_Floating_Point_Type (Component_Type (Typl))
5604 and then not Is_Atomic (Component_Type (Typl))
5605 and then not Is_Possibly_Unaligned_Object (Lhs)
5606 and then not Is_Possibly_Unaligned_Object (Rhs)
5607 and then Support_Composite_Compare_On_Target
5611 -- For composite and floating-point cases, expand equality loop to
5612 -- make sure of using proper comparisons for tagged types, and
5613 -- correctly handling the floating-point case.
5617 Expand_Array_Equality
5619 Relocate_Node (Lhs),
5620 Relocate_Node (Rhs),
5623 Insert_Actions (N, Bodies, Suppress => All_Checks);
5624 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5629 elsif Is_Record_Type (Typl) then
5631 -- For tagged types, use the primitive "="
5633 if Is_Tagged_Type (Typl) then
5635 -- No need to do anything else compiling under restriction
5636 -- No_Dispatching_Calls. During the semantic analysis we
5637 -- already notified such violation.
5639 if Restriction_Active (No_Dispatching_Calls) then
5643 -- If this is derived from an untagged private type completed with
5644 -- a tagged type, it does not have a full view, so we use the
5645 -- primitive operations of the private type. This check should no
5646 -- longer be necessary when these types get their full views???
5648 if Is_Private_Type (A_Typ)
5649 and then not Is_Tagged_Type (A_Typ)
5650 and then Is_Derived_Type (A_Typ)
5651 and then No (Full_View (A_Typ))
5653 -- Search for equality operation, checking that the operands
5654 -- have the same type. Note that we must find a matching entry,
5655 -- or something is very wrong!
5657 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
5659 while Present (Prim) loop
5660 exit when Chars (Node (Prim)) = Name_Op_Eq
5661 and then Etype (First_Formal (Node (Prim))) =
5662 Etype (Next_Formal (First_Formal (Node (Prim))))
5664 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5669 pragma Assert (Present (Prim));
5670 Op_Name := Node (Prim);
5672 -- Find the type's predefined equality or an overriding
5673 -- user- defined equality. The reason for not simply calling
5674 -- Find_Prim_Op here is that there may be a user-defined
5675 -- overloaded equality op that precedes the equality that we want,
5676 -- so we have to explicitly search (e.g., there could be an
5677 -- equality with two different parameter types).
5680 if Is_Class_Wide_Type (Typl) then
5681 Typl := Root_Type (Typl);
5684 Prim := First_Elmt (Primitive_Operations (Typl));
5685 while Present (Prim) loop
5686 exit when Chars (Node (Prim)) = Name_Op_Eq
5687 and then Etype (First_Formal (Node (Prim))) =
5688 Etype (Next_Formal (First_Formal (Node (Prim))))
5690 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5695 pragma Assert (Present (Prim));
5696 Op_Name := Node (Prim);
5699 Build_Equality_Call (Op_Name);
5701 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
5702 -- predefined equality operator for a type which has a subcomponent
5703 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
5705 elsif Has_Unconstrained_UU_Component (Typl) then
5707 Make_Raise_Program_Error (Loc,
5708 Reason => PE_Unchecked_Union_Restriction));
5710 -- Prevent Gigi from generating incorrect code by rewriting the
5711 -- equality as a standard False.
5714 New_Occurrence_Of (Standard_False, Loc));
5716 elsif Is_Unchecked_Union (Typl) then
5718 -- If we can infer the discriminants of the operands, we make a
5719 -- call to the TSS equality function.
5721 if Has_Inferable_Discriminants (Lhs)
5723 Has_Inferable_Discriminants (Rhs)
5726 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5729 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5730 -- the predefined equality operator for an Unchecked_Union type
5731 -- if either of the operands lack inferable discriminants.
5734 Make_Raise_Program_Error (Loc,
5735 Reason => PE_Unchecked_Union_Restriction));
5737 -- Prevent Gigi from generating incorrect code by rewriting
5738 -- the equality as a standard False.
5741 New_Occurrence_Of (Standard_False, Loc));
5745 -- If a type support function is present (for complex cases), use it
5747 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
5749 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5751 -- Otherwise expand the component by component equality. Note that
5752 -- we never use block-bit comparisons for records, because of the
5753 -- problems with gaps. The backend will often be able to recombine
5754 -- the separate comparisons that we generate here.
5757 Remove_Side_Effects (Lhs);
5758 Remove_Side_Effects (Rhs);
5760 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
5762 Insert_Actions (N, Bodies, Suppress => All_Checks);
5763 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5767 -- Test if result is known at compile time
5769 Rewrite_Comparison (N);
5771 -- If we still have comparison for Vax_Float, process it
5773 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
5774 Expand_Vax_Comparison (N);
5779 -----------------------
5780 -- Expand_N_Op_Expon --
5781 -----------------------
5783 procedure Expand_N_Op_Expon (N : Node_Id) is
5784 Loc : constant Source_Ptr := Sloc (N);
5785 Typ : constant Entity_Id := Etype (N);
5786 Rtyp : constant Entity_Id := Root_Type (Typ);
5787 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
5788 Bastyp : constant Node_Id := Etype (Base);
5789 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
5790 Exptyp : constant Entity_Id := Etype (Exp);
5791 Ovflo : constant Boolean := Do_Overflow_Check (N);
5800 Binary_Op_Validity_Checks (N);
5802 -- If either operand is of a private type, then we have the use of an
5803 -- intrinsic operator, and we get rid of the privateness, by using root
5804 -- types of underlying types for the actual operation. Otherwise the
5805 -- private types will cause trouble if we expand multiplications or
5806 -- shifts etc. We also do this transformation if the result type is
5807 -- different from the base type.
5809 if Is_Private_Type (Etype (Base))
5811 Is_Private_Type (Typ)
5813 Is_Private_Type (Exptyp)
5815 Rtyp /= Root_Type (Bastyp)
5818 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
5819 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
5823 Unchecked_Convert_To (Typ,
5825 Left_Opnd => Unchecked_Convert_To (Bt, Base),
5826 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
5827 Analyze_And_Resolve (N, Typ);
5832 -- Test for case of known right argument
5834 if Compile_Time_Known_Value (Exp) then
5835 Expv := Expr_Value (Exp);
5837 -- We only fold small non-negative exponents. You might think we
5838 -- could fold small negative exponents for the real case, but we
5839 -- can't because we are required to raise Constraint_Error for
5840 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
5841 -- See ACVC test C4A012B.
5843 if Expv >= 0 and then Expv <= 4 then
5845 -- X ** 0 = 1 (or 1.0)
5849 -- Call Remove_Side_Effects to ensure that any side effects
5850 -- in the ignored left operand (in particular function calls
5851 -- to user defined functions) are properly executed.
5853 Remove_Side_Effects (Base);
5855 if Ekind (Typ) in Integer_Kind then
5856 Xnode := Make_Integer_Literal (Loc, Intval => 1);
5858 Xnode := Make_Real_Literal (Loc, Ureal_1);
5870 Make_Op_Multiply (Loc,
5871 Left_Opnd => Duplicate_Subexpr (Base),
5872 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
5874 -- X ** 3 = X * X * X
5878 Make_Op_Multiply (Loc,
5880 Make_Op_Multiply (Loc,
5881 Left_Opnd => Duplicate_Subexpr (Base),
5882 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
5883 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
5886 -- En : constant base'type := base * base;
5892 Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
5894 Insert_Actions (N, New_List (
5895 Make_Object_Declaration (Loc,
5896 Defining_Identifier => Temp,
5897 Constant_Present => True,
5898 Object_Definition => New_Reference_To (Typ, Loc),
5900 Make_Op_Multiply (Loc,
5901 Left_Opnd => Duplicate_Subexpr (Base),
5902 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
5905 Make_Op_Multiply (Loc,
5906 Left_Opnd => New_Reference_To (Temp, Loc),
5907 Right_Opnd => New_Reference_To (Temp, Loc));
5911 Analyze_And_Resolve (N, Typ);
5916 -- Case of (2 ** expression) appearing as an argument of an integer
5917 -- multiplication, or as the right argument of a division of a non-
5918 -- negative integer. In such cases we leave the node untouched, setting
5919 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
5920 -- of the higher level node converts it into a shift.
5922 -- Note: this transformation is not applicable for a modular type with
5923 -- a non-binary modulus in the multiplication case, since we get a wrong
5924 -- result if the shift causes an overflow before the modular reduction.
5926 if Nkind (Base) = N_Integer_Literal
5927 and then Intval (Base) = 2
5928 and then Is_Integer_Type (Root_Type (Exptyp))
5929 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
5930 and then Is_Unsigned_Type (Exptyp)
5932 and then Nkind (Parent (N)) in N_Binary_Op
5935 P : constant Node_Id := Parent (N);
5936 L : constant Node_Id := Left_Opnd (P);
5937 R : constant Node_Id := Right_Opnd (P);
5940 if (Nkind (P) = N_Op_Multiply
5941 and then not Non_Binary_Modulus (Typ)
5943 ((Is_Integer_Type (Etype (L)) and then R = N)
5945 (Is_Integer_Type (Etype (R)) and then L = N))
5946 and then not Do_Overflow_Check (P))
5949 (Nkind (P) = N_Op_Divide
5950 and then Is_Integer_Type (Etype (L))
5951 and then Is_Unsigned_Type (Etype (L))
5953 and then not Do_Overflow_Check (P))
5955 Set_Is_Power_Of_2_For_Shift (N);
5961 -- Fall through if exponentiation must be done using a runtime routine
5963 -- First deal with modular case
5965 if Is_Modular_Integer_Type (Rtyp) then
5967 -- Non-binary case, we call the special exponentiation routine for
5968 -- the non-binary case, converting the argument to Long_Long_Integer
5969 -- and passing the modulus value. Then the result is converted back
5970 -- to the base type.
5972 if Non_Binary_Modulus (Rtyp) then
5975 Make_Function_Call (Loc,
5976 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
5977 Parameter_Associations => New_List (
5978 Convert_To (Standard_Integer, Base),
5979 Make_Integer_Literal (Loc, Modulus (Rtyp)),
5982 -- Binary case, in this case, we call one of two routines, either the
5983 -- unsigned integer case, or the unsigned long long integer case,
5984 -- with a final "and" operation to do the required mod.
5987 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
5988 Ent := RTE (RE_Exp_Unsigned);
5990 Ent := RTE (RE_Exp_Long_Long_Unsigned);
5997 Make_Function_Call (Loc,
5998 Name => New_Reference_To (Ent, Loc),
5999 Parameter_Associations => New_List (
6000 Convert_To (Etype (First_Formal (Ent)), Base),
6003 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
6007 -- Common exit point for modular type case
6009 Analyze_And_Resolve (N, Typ);
6012 -- Signed integer cases, done using either Integer or Long_Long_Integer.
6013 -- It is not worth having routines for Short_[Short_]Integer, since for
6014 -- most machines it would not help, and it would generate more code that
6015 -- might need certification when a certified run time is required.
6017 -- In the integer cases, we have two routines, one for when overflow
6018 -- checks are required, and one when they are not required, since there
6019 -- is a real gain in omitting checks on many machines.
6021 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
6022 or else (Rtyp = Base_Type (Standard_Long_Integer)
6024 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
6025 or else (Rtyp = Universal_Integer)
6027 Etyp := Standard_Long_Long_Integer;
6030 Rent := RE_Exp_Long_Long_Integer;
6032 Rent := RE_Exn_Long_Long_Integer;
6035 elsif Is_Signed_Integer_Type (Rtyp) then
6036 Etyp := Standard_Integer;
6039 Rent := RE_Exp_Integer;
6041 Rent := RE_Exn_Integer;
6044 -- Floating-point cases, always done using Long_Long_Float. We do not
6045 -- need separate routines for the overflow case here, since in the case
6046 -- of floating-point, we generate infinities anyway as a rule (either
6047 -- that or we automatically trap overflow), and if there is an infinity
6048 -- generated and a range check is required, the check will fail anyway.
6051 pragma Assert (Is_Floating_Point_Type (Rtyp));
6052 Etyp := Standard_Long_Long_Float;
6053 Rent := RE_Exn_Long_Long_Float;
6056 -- Common processing for integer cases and floating-point cases.
6057 -- If we are in the right type, we can call runtime routine directly
6060 and then Rtyp /= Universal_Integer
6061 and then Rtyp /= Universal_Real
6064 Make_Function_Call (Loc,
6065 Name => New_Reference_To (RTE (Rent), Loc),
6066 Parameter_Associations => New_List (Base, Exp)));
6068 -- Otherwise we have to introduce conversions (conversions are also
6069 -- required in the universal cases, since the runtime routine is
6070 -- typed using one of the standard types).
6075 Make_Function_Call (Loc,
6076 Name => New_Reference_To (RTE (Rent), Loc),
6077 Parameter_Associations => New_List (
6078 Convert_To (Etyp, Base),
6082 Analyze_And_Resolve (N, Typ);
6086 when RE_Not_Available =>
6088 end Expand_N_Op_Expon;
6090 --------------------
6091 -- Expand_N_Op_Ge --
6092 --------------------
6094 procedure Expand_N_Op_Ge (N : Node_Id) is
6095 Typ : constant Entity_Id := Etype (N);
6096 Op1 : constant Node_Id := Left_Opnd (N);
6097 Op2 : constant Node_Id := Right_Opnd (N);
6098 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6101 Binary_Op_Validity_Checks (N);
6103 if Is_Array_Type (Typ1) then
6104 Expand_Array_Comparison (N);
6108 if Is_Boolean_Type (Typ1) then
6109 Adjust_Condition (Op1);
6110 Adjust_Condition (Op2);
6111 Set_Etype (N, Standard_Boolean);
6112 Adjust_Result_Type (N, Typ);
6115 Rewrite_Comparison (N);
6117 -- If we still have comparison, and Vax_Float type, process it
6119 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6120 Expand_Vax_Comparison (N);
6125 --------------------
6126 -- Expand_N_Op_Gt --
6127 --------------------
6129 procedure Expand_N_Op_Gt (N : Node_Id) is
6130 Typ : constant Entity_Id := Etype (N);
6131 Op1 : constant Node_Id := Left_Opnd (N);
6132 Op2 : constant Node_Id := Right_Opnd (N);
6133 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6136 Binary_Op_Validity_Checks (N);
6138 if Is_Array_Type (Typ1) then
6139 Expand_Array_Comparison (N);
6143 if Is_Boolean_Type (Typ1) then
6144 Adjust_Condition (Op1);
6145 Adjust_Condition (Op2);
6146 Set_Etype (N, Standard_Boolean);
6147 Adjust_Result_Type (N, Typ);
6150 Rewrite_Comparison (N);
6152 -- If we still have comparison, and Vax_Float type, process it
6154 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6155 Expand_Vax_Comparison (N);
6160 --------------------
6161 -- Expand_N_Op_Le --
6162 --------------------
6164 procedure Expand_N_Op_Le (N : Node_Id) is
6165 Typ : constant Entity_Id := Etype (N);
6166 Op1 : constant Node_Id := Left_Opnd (N);
6167 Op2 : constant Node_Id := Right_Opnd (N);
6168 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6171 Binary_Op_Validity_Checks (N);
6173 if Is_Array_Type (Typ1) then
6174 Expand_Array_Comparison (N);
6178 if Is_Boolean_Type (Typ1) then
6179 Adjust_Condition (Op1);
6180 Adjust_Condition (Op2);
6181 Set_Etype (N, Standard_Boolean);
6182 Adjust_Result_Type (N, Typ);
6185 Rewrite_Comparison (N);
6187 -- If we still have comparison, and Vax_Float type, process it
6189 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6190 Expand_Vax_Comparison (N);
6195 --------------------
6196 -- Expand_N_Op_Lt --
6197 --------------------
6199 procedure Expand_N_Op_Lt (N : Node_Id) is
6200 Typ : constant Entity_Id := Etype (N);
6201 Op1 : constant Node_Id := Left_Opnd (N);
6202 Op2 : constant Node_Id := Right_Opnd (N);
6203 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
6206 Binary_Op_Validity_Checks (N);
6208 if Is_Array_Type (Typ1) then
6209 Expand_Array_Comparison (N);
6213 if Is_Boolean_Type (Typ1) then
6214 Adjust_Condition (Op1);
6215 Adjust_Condition (Op2);
6216 Set_Etype (N, Standard_Boolean);
6217 Adjust_Result_Type (N, Typ);
6220 Rewrite_Comparison (N);
6222 -- If we still have comparison, and Vax_Float type, process it
6224 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
6225 Expand_Vax_Comparison (N);
6230 -----------------------
6231 -- Expand_N_Op_Minus --
6232 -----------------------
6234 procedure Expand_N_Op_Minus (N : Node_Id) is
6235 Loc : constant Source_Ptr := Sloc (N);
6236 Typ : constant Entity_Id := Etype (N);
6239 Unary_Op_Validity_Checks (N);
6241 if not Backend_Overflow_Checks_On_Target
6242 and then Is_Signed_Integer_Type (Etype (N))
6243 and then Do_Overflow_Check (N)
6245 -- Software overflow checking expands -expr into (0 - expr)
6248 Make_Op_Subtract (Loc,
6249 Left_Opnd => Make_Integer_Literal (Loc, 0),
6250 Right_Opnd => Right_Opnd (N)));
6252 Analyze_And_Resolve (N, Typ);
6254 -- Vax floating-point types case
6256 elsif Vax_Float (Etype (N)) then
6257 Expand_Vax_Arith (N);
6259 end Expand_N_Op_Minus;
6261 ---------------------
6262 -- Expand_N_Op_Mod --
6263 ---------------------
6265 procedure Expand_N_Op_Mod (N : Node_Id) is
6266 Loc : constant Source_Ptr := Sloc (N);
6267 Typ : constant Entity_Id := Etype (N);
6268 Left : constant Node_Id := Left_Opnd (N);
6269 Right : constant Node_Id := Right_Opnd (N);
6270 DOC : constant Boolean := Do_Overflow_Check (N);
6271 DDC : constant Boolean := Do_Division_Check (N);
6281 pragma Warnings (Off, Lhi);
6284 Binary_Op_Validity_Checks (N);
6286 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
6287 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
6289 -- Convert mod to rem if operands are known non-negative. We do this
6290 -- since it is quite likely that this will improve the quality of code,
6291 -- (the operation now corresponds to the hardware remainder), and it
6292 -- does not seem likely that it could be harmful.
6294 if LOK and then Llo >= 0
6296 ROK and then Rlo >= 0
6299 Make_Op_Rem (Sloc (N),
6300 Left_Opnd => Left_Opnd (N),
6301 Right_Opnd => Right_Opnd (N)));
6303 -- Instead of reanalyzing the node we do the analysis manually. This
6304 -- avoids anomalies when the replacement is done in an instance and
6305 -- is epsilon more efficient.
6307 Set_Entity (N, Standard_Entity (S_Op_Rem));
6309 Set_Do_Overflow_Check (N, DOC);
6310 Set_Do_Division_Check (N, DDC);
6311 Expand_N_Op_Rem (N);
6314 -- Otherwise, normal mod processing
6317 if Is_Integer_Type (Etype (N)) then
6318 Apply_Divide_Check (N);
6321 -- Apply optimization x mod 1 = 0. We don't really need that with
6322 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
6323 -- certainly harmless.
6325 if Is_Integer_Type (Etype (N))
6326 and then Compile_Time_Known_Value (Right)
6327 and then Expr_Value (Right) = Uint_1
6329 -- Call Remove_Side_Effects to ensure that any side effects in
6330 -- the ignored left operand (in particular function calls to
6331 -- user defined functions) are properly executed.
6333 Remove_Side_Effects (Left);
6335 Rewrite (N, Make_Integer_Literal (Loc, 0));
6336 Analyze_And_Resolve (N, Typ);
6340 -- Deal with annoying case of largest negative number remainder
6341 -- minus one. Gigi does not handle this case correctly, because
6342 -- it generates a divide instruction which may trap in this case.
6344 -- In fact the check is quite easy, if the right operand is -1, then
6345 -- the mod value is always 0, and we can just ignore the left operand
6346 -- completely in this case.
6348 -- The operand type may be private (e.g. in the expansion of an
6349 -- intrinsic operation) so we must use the underlying type to get the
6350 -- bounds, and convert the literals explicitly.
6354 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
6356 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
6358 ((not LOK) or else (Llo = LLB))
6361 Make_Conditional_Expression (Loc,
6362 Expressions => New_List (
6364 Left_Opnd => Duplicate_Subexpr (Right),
6366 Unchecked_Convert_To (Typ,
6367 Make_Integer_Literal (Loc, -1))),
6368 Unchecked_Convert_To (Typ,
6369 Make_Integer_Literal (Loc, Uint_0)),
6370 Relocate_Node (N))));
6372 Set_Analyzed (Next (Next (First (Expressions (N)))));
6373 Analyze_And_Resolve (N, Typ);
6376 end Expand_N_Op_Mod;
6378 --------------------------
6379 -- Expand_N_Op_Multiply --
6380 --------------------------
6382 procedure Expand_N_Op_Multiply (N : Node_Id) is
6383 Loc : constant Source_Ptr := Sloc (N);
6384 Lop : constant Node_Id := Left_Opnd (N);
6385 Rop : constant Node_Id := Right_Opnd (N);
6387 Lp2 : constant Boolean :=
6388 Nkind (Lop) = N_Op_Expon
6389 and then Is_Power_Of_2_For_Shift (Lop);
6391 Rp2 : constant Boolean :=
6392 Nkind (Rop) = N_Op_Expon
6393 and then Is_Power_Of_2_For_Shift (Rop);
6395 Ltyp : constant Entity_Id := Etype (Lop);
6396 Rtyp : constant Entity_Id := Etype (Rop);
6397 Typ : Entity_Id := Etype (N);
6400 Binary_Op_Validity_Checks (N);
6402 -- Special optimizations for integer types
6404 if Is_Integer_Type (Typ) then
6406 -- N * 0 = 0 for integer types
6408 if Compile_Time_Known_Value (Rop)
6409 and then Expr_Value (Rop) = Uint_0
6411 -- Call Remove_Side_Effects to ensure that any side effects in
6412 -- the ignored left operand (in particular function calls to
6413 -- user defined functions) are properly executed.
6415 Remove_Side_Effects (Lop);
6417 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6418 Analyze_And_Resolve (N, Typ);
6422 -- Similar handling for 0 * N = 0
6424 if Compile_Time_Known_Value (Lop)
6425 and then Expr_Value (Lop) = Uint_0
6427 Remove_Side_Effects (Rop);
6428 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
6429 Analyze_And_Resolve (N, Typ);
6433 -- N * 1 = 1 * N = N for integer types
6435 -- This optimisation is not done if we are going to
6436 -- rewrite the product 1 * 2 ** N to a shift.
6438 if Compile_Time_Known_Value (Rop)
6439 and then Expr_Value (Rop) = Uint_1
6445 elsif Compile_Time_Known_Value (Lop)
6446 and then Expr_Value (Lop) = Uint_1
6454 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
6455 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6456 -- operand is an integer, as required for this to work.
6461 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
6465 Left_Opnd => Make_Integer_Literal (Loc, 2),
6468 Left_Opnd => Right_Opnd (Lop),
6469 Right_Opnd => Right_Opnd (Rop))));
6470 Analyze_And_Resolve (N, Typ);
6475 Make_Op_Shift_Left (Loc,
6478 Convert_To (Standard_Natural, Right_Opnd (Rop))));
6479 Analyze_And_Resolve (N, Typ);
6483 -- Same processing for the operands the other way round
6487 Make_Op_Shift_Left (Loc,
6490 Convert_To (Standard_Natural, Right_Opnd (Lop))));
6491 Analyze_And_Resolve (N, Typ);
6495 -- Do required fixup of universal fixed operation
6497 if Typ = Universal_Fixed then
6498 Fixup_Universal_Fixed_Operation (N);
6502 -- Multiplications with fixed-point results
6504 if Is_Fixed_Point_Type (Typ) then
6506 -- No special processing if Treat_Fixed_As_Integer is set, since from
6507 -- a semantic point of view such operations are simply integer
6508 -- operations and will be treated that way.
6510 if not Treat_Fixed_As_Integer (N) then
6512 -- Case of fixed * integer => fixed
6514 if Is_Integer_Type (Rtyp) then
6515 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
6517 -- Case of integer * fixed => fixed
6519 elsif Is_Integer_Type (Ltyp) then
6520 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
6522 -- Case of fixed * fixed => fixed
6525 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
6529 -- Other cases of multiplication of fixed-point operands. Again we
6530 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
6532 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6533 and then not Treat_Fixed_As_Integer (N)
6535 if Is_Integer_Type (Typ) then
6536 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
6538 pragma Assert (Is_Floating_Point_Type (Typ));
6539 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
6542 -- Mixed-mode operations can appear in a non-static universal context,
6543 -- in which case the integer argument must be converted explicitly.
6545 elsif Typ = Universal_Real
6546 and then Is_Integer_Type (Rtyp)
6548 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
6550 Analyze_And_Resolve (Rop, Universal_Real);
6552 elsif Typ = Universal_Real
6553 and then Is_Integer_Type (Ltyp)
6555 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
6557 Analyze_And_Resolve (Lop, Universal_Real);
6559 -- Non-fixed point cases, check software overflow checking required
6561 elsif Is_Signed_Integer_Type (Etype (N)) then
6562 Apply_Arithmetic_Overflow_Check (N);
6564 -- Deal with VAX float case
6566 elsif Vax_Float (Typ) then
6567 Expand_Vax_Arith (N);
6570 end Expand_N_Op_Multiply;
6572 --------------------
6573 -- Expand_N_Op_Ne --
6574 --------------------
6576 procedure Expand_N_Op_Ne (N : Node_Id) is
6577 Typ : constant Entity_Id := Etype (Left_Opnd (N));
6580 -- Case of elementary type with standard operator
6582 if Is_Elementary_Type (Typ)
6583 and then Sloc (Entity (N)) = Standard_Location
6585 Binary_Op_Validity_Checks (N);
6587 -- Boolean types (requiring handling of non-standard case)
6589 if Is_Boolean_Type (Typ) then
6590 Adjust_Condition (Left_Opnd (N));
6591 Adjust_Condition (Right_Opnd (N));
6592 Set_Etype (N, Standard_Boolean);
6593 Adjust_Result_Type (N, Typ);
6596 Rewrite_Comparison (N);
6598 -- If we still have comparison for Vax_Float, process it
6600 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
6601 Expand_Vax_Comparison (N);
6605 -- For all cases other than elementary types, we rewrite node as the
6606 -- negation of an equality operation, and reanalyze. The equality to be
6607 -- used is defined in the same scope and has the same signature. This
6608 -- signature must be set explicitly since in an instance it may not have
6609 -- the same visibility as in the generic unit. This avoids duplicating
6610 -- or factoring the complex code for record/array equality tests etc.
6614 Loc : constant Source_Ptr := Sloc (N);
6616 Ne : constant Entity_Id := Entity (N);
6619 Binary_Op_Validity_Checks (N);
6625 Left_Opnd => Left_Opnd (N),
6626 Right_Opnd => Right_Opnd (N)));
6627 Set_Paren_Count (Right_Opnd (Neg), 1);
6629 if Scope (Ne) /= Standard_Standard then
6630 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
6633 -- For navigation purposes, the inequality is treated as an
6634 -- implicit reference to the corresponding equality. Preserve the
6635 -- Comes_From_ source flag so that the proper Xref entry is
6638 Preserve_Comes_From_Source (Neg, N);
6639 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
6641 Analyze_And_Resolve (N, Standard_Boolean);
6646 ---------------------
6647 -- Expand_N_Op_Not --
6648 ---------------------
6650 -- If the argument is other than a Boolean array type, there is no special
6651 -- expansion required.
6653 -- For the packed case, we call the special routine in Exp_Pakd, except
6654 -- that if the component size is greater than one, we use the standard
6655 -- routine generating a gruesome loop (it is so peculiar to have packed
6656 -- arrays with non-standard Boolean representations anyway, so it does not
6657 -- matter that we do not handle this case efficiently).
6659 -- For the unpacked case (and for the special packed case where we have non
6660 -- standard Booleans, as discussed above), we generate and insert into the
6661 -- tree the following function definition:
6663 -- function Nnnn (A : arr) is
6666 -- for J in a'range loop
6667 -- B (J) := not A (J);
6672 -- Here arr is the actual subtype of the parameter (and hence always
6673 -- constrained). Then we replace the not with a call to this function.
6675 procedure Expand_N_Op_Not (N : Node_Id) is
6676 Loc : constant Source_Ptr := Sloc (N);
6677 Typ : constant Entity_Id := Etype (N);
6686 Func_Name : Entity_Id;
6687 Loop_Statement : Node_Id;
6690 Unary_Op_Validity_Checks (N);
6692 -- For boolean operand, deal with non-standard booleans
6694 if Is_Boolean_Type (Typ) then
6695 Adjust_Condition (Right_Opnd (N));
6696 Set_Etype (N, Standard_Boolean);
6697 Adjust_Result_Type (N, Typ);
6701 -- Only array types need any other processing
6703 if not Is_Array_Type (Typ) then
6707 -- Case of array operand. If bit packed with a component size of 1,
6708 -- handle it in Exp_Pakd if the operand is known to be aligned.
6710 if Is_Bit_Packed_Array (Typ)
6711 and then Component_Size (Typ) = 1
6712 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
6714 Expand_Packed_Not (N);
6718 -- Case of array operand which is not bit-packed. If the context is
6719 -- a safe assignment, call in-place operation, If context is a larger
6720 -- boolean expression in the context of a safe assignment, expansion is
6721 -- done by enclosing operation.
6723 Opnd := Relocate_Node (Right_Opnd (N));
6724 Convert_To_Actual_Subtype (Opnd);
6725 Arr := Etype (Opnd);
6726 Ensure_Defined (Arr, N);
6727 Silly_Boolean_Array_Not_Test (N, Arr);
6729 if Nkind (Parent (N)) = N_Assignment_Statement then
6730 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
6731 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
6734 -- Special case the negation of a binary operation
6736 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
6737 and then Safe_In_Place_Array_Op
6738 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
6740 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
6744 elsif Nkind (Parent (N)) in N_Binary_Op
6745 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
6748 Op1 : constant Node_Id := Left_Opnd (Parent (N));
6749 Op2 : constant Node_Id := Right_Opnd (Parent (N));
6750 Lhs : constant Node_Id := Name (Parent (Parent (N)));
6753 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
6755 and then Nkind (Op2) = N_Op_Not
6757 -- (not A) op (not B) can be reduced to a single call
6762 and then Nkind (Parent (N)) = N_Op_Xor
6764 -- A xor (not B) can also be special-cased
6772 A := Make_Defining_Identifier (Loc, Name_uA);
6773 B := Make_Defining_Identifier (Loc, Name_uB);
6774 J := Make_Defining_Identifier (Loc, Name_uJ);
6777 Make_Indexed_Component (Loc,
6778 Prefix => New_Reference_To (A, Loc),
6779 Expressions => New_List (New_Reference_To (J, Loc)));
6782 Make_Indexed_Component (Loc,
6783 Prefix => New_Reference_To (B, Loc),
6784 Expressions => New_List (New_Reference_To (J, Loc)));
6787 Make_Implicit_Loop_Statement (N,
6788 Identifier => Empty,
6791 Make_Iteration_Scheme (Loc,
6792 Loop_Parameter_Specification =>
6793 Make_Loop_Parameter_Specification (Loc,
6794 Defining_Identifier => J,
6795 Discrete_Subtype_Definition =>
6796 Make_Attribute_Reference (Loc,
6797 Prefix => Make_Identifier (Loc, Chars (A)),
6798 Attribute_Name => Name_Range))),
6800 Statements => New_List (
6801 Make_Assignment_Statement (Loc,
6803 Expression => Make_Op_Not (Loc, A_J))));
6805 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('N'));
6806 Set_Is_Inlined (Func_Name);
6809 Make_Subprogram_Body (Loc,
6811 Make_Function_Specification (Loc,
6812 Defining_Unit_Name => Func_Name,
6813 Parameter_Specifications => New_List (
6814 Make_Parameter_Specification (Loc,
6815 Defining_Identifier => A,
6816 Parameter_Type => New_Reference_To (Typ, Loc))),
6817 Result_Definition => New_Reference_To (Typ, Loc)),
6819 Declarations => New_List (
6820 Make_Object_Declaration (Loc,
6821 Defining_Identifier => B,
6822 Object_Definition => New_Reference_To (Arr, Loc))),
6824 Handled_Statement_Sequence =>
6825 Make_Handled_Sequence_Of_Statements (Loc,
6826 Statements => New_List (
6828 Make_Simple_Return_Statement (Loc,
6830 Make_Identifier (Loc, Chars (B)))))));
6833 Make_Function_Call (Loc,
6834 Name => New_Reference_To (Func_Name, Loc),
6835 Parameter_Associations => New_List (Opnd)));
6837 Analyze_And_Resolve (N, Typ);
6838 end Expand_N_Op_Not;
6840 --------------------
6841 -- Expand_N_Op_Or --
6842 --------------------
6844 procedure Expand_N_Op_Or (N : Node_Id) is
6845 Typ : constant Entity_Id := Etype (N);
6848 Binary_Op_Validity_Checks (N);
6850 if Is_Array_Type (Etype (N)) then
6851 Expand_Boolean_Operator (N);
6853 elsif Is_Boolean_Type (Etype (N)) then
6854 Adjust_Condition (Left_Opnd (N));
6855 Adjust_Condition (Right_Opnd (N));
6856 Set_Etype (N, Standard_Boolean);
6857 Adjust_Result_Type (N, Typ);
6861 ----------------------
6862 -- Expand_N_Op_Plus --
6863 ----------------------
6865 procedure Expand_N_Op_Plus (N : Node_Id) is
6867 Unary_Op_Validity_Checks (N);
6868 end Expand_N_Op_Plus;
6870 ---------------------
6871 -- Expand_N_Op_Rem --
6872 ---------------------
6874 procedure Expand_N_Op_Rem (N : Node_Id) is
6875 Loc : constant Source_Ptr := Sloc (N);
6876 Typ : constant Entity_Id := Etype (N);
6878 Left : constant Node_Id := Left_Opnd (N);
6879 Right : constant Node_Id := Right_Opnd (N);
6887 -- Set if corresponding operand can be negative
6889 pragma Unreferenced (Hi);
6892 Binary_Op_Validity_Checks (N);
6894 if Is_Integer_Type (Etype (N)) then
6895 Apply_Divide_Check (N);
6898 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
6899 -- but it is useful with other back ends (e.g. AAMP), and is certainly
6902 if Is_Integer_Type (Etype (N))
6903 and then Compile_Time_Known_Value (Right)
6904 and then Expr_Value (Right) = Uint_1
6906 -- Call Remove_Side_Effects to ensure that any side effects in the
6907 -- ignored left operand (in particular function calls to user defined
6908 -- functions) are properly executed.
6910 Remove_Side_Effects (Left);
6912 Rewrite (N, Make_Integer_Literal (Loc, 0));
6913 Analyze_And_Resolve (N, Typ);
6917 -- Deal with annoying case of largest negative number remainder minus
6918 -- one. Gigi does not handle this case correctly, because it generates
6919 -- a divide instruction which may trap in this case.
6921 -- In fact the check is quite easy, if the right operand is -1, then
6922 -- the remainder is always 0, and we can just ignore the left operand
6923 -- completely in this case.
6925 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
6926 Lneg := (not OK) or else Lo < 0;
6928 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
6929 Rneg := (not OK) or else Lo < 0;
6931 -- We won't mess with trying to find out if the left operand can really
6932 -- be the largest negative number (that's a pain in the case of private
6933 -- types and this is really marginal). We will just assume that we need
6934 -- the test if the left operand can be negative at all.
6936 if Lneg and Rneg then
6938 Make_Conditional_Expression (Loc,
6939 Expressions => New_List (
6941 Left_Opnd => Duplicate_Subexpr (Right),
6943 Unchecked_Convert_To (Typ,
6944 Make_Integer_Literal (Loc, -1))),
6946 Unchecked_Convert_To (Typ,
6947 Make_Integer_Literal (Loc, Uint_0)),
6949 Relocate_Node (N))));
6951 Set_Analyzed (Next (Next (First (Expressions (N)))));
6952 Analyze_And_Resolve (N, Typ);
6954 end Expand_N_Op_Rem;
6956 -----------------------------
6957 -- Expand_N_Op_Rotate_Left --
6958 -----------------------------
6960 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
6962 Binary_Op_Validity_Checks (N);
6963 end Expand_N_Op_Rotate_Left;
6965 ------------------------------
6966 -- Expand_N_Op_Rotate_Right --
6967 ------------------------------
6969 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
6971 Binary_Op_Validity_Checks (N);
6972 end Expand_N_Op_Rotate_Right;
6974 ----------------------------
6975 -- Expand_N_Op_Shift_Left --
6976 ----------------------------
6978 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
6980 Binary_Op_Validity_Checks (N);
6981 end Expand_N_Op_Shift_Left;
6983 -----------------------------
6984 -- Expand_N_Op_Shift_Right --
6985 -----------------------------
6987 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
6989 Binary_Op_Validity_Checks (N);
6990 end Expand_N_Op_Shift_Right;
6992 ----------------------------------------
6993 -- Expand_N_Op_Shift_Right_Arithmetic --
6994 ----------------------------------------
6996 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
6998 Binary_Op_Validity_Checks (N);
6999 end Expand_N_Op_Shift_Right_Arithmetic;
7001 --------------------------
7002 -- Expand_N_Op_Subtract --
7003 --------------------------
7005 procedure Expand_N_Op_Subtract (N : Node_Id) is
7006 Typ : constant Entity_Id := Etype (N);
7009 Binary_Op_Validity_Checks (N);
7011 -- N - 0 = N for integer types
7013 if Is_Integer_Type (Typ)
7014 and then Compile_Time_Known_Value (Right_Opnd (N))
7015 and then Expr_Value (Right_Opnd (N)) = 0
7017 Rewrite (N, Left_Opnd (N));
7021 -- Arithmetic overflow checks for signed integer/fixed point types
7023 if Is_Signed_Integer_Type (Typ)
7024 or else Is_Fixed_Point_Type (Typ)
7026 Apply_Arithmetic_Overflow_Check (N);
7028 -- Vax floating-point types case
7030 elsif Vax_Float (Typ) then
7031 Expand_Vax_Arith (N);
7033 end Expand_N_Op_Subtract;
7035 ---------------------
7036 -- Expand_N_Op_Xor --
7037 ---------------------
7039 procedure Expand_N_Op_Xor (N : Node_Id) is
7040 Typ : constant Entity_Id := Etype (N);
7043 Binary_Op_Validity_Checks (N);
7045 if Is_Array_Type (Etype (N)) then
7046 Expand_Boolean_Operator (N);
7048 elsif Is_Boolean_Type (Etype (N)) then
7049 Adjust_Condition (Left_Opnd (N));
7050 Adjust_Condition (Right_Opnd (N));
7051 Set_Etype (N, Standard_Boolean);
7052 Adjust_Result_Type (N, Typ);
7054 end Expand_N_Op_Xor;
7056 ----------------------
7057 -- Expand_N_Or_Else --
7058 ----------------------
7060 -- Expand into conditional expression if Actions present, and also
7061 -- deal with optimizing case of arguments being True or False.
7063 procedure Expand_N_Or_Else (N : Node_Id) is
7064 Loc : constant Source_Ptr := Sloc (N);
7065 Typ : constant Entity_Id := Etype (N);
7066 Left : constant Node_Id := Left_Opnd (N);
7067 Right : constant Node_Id := Right_Opnd (N);
7071 -- Deal with non-standard booleans
7073 if Is_Boolean_Type (Typ) then
7074 Adjust_Condition (Left);
7075 Adjust_Condition (Right);
7076 Set_Etype (N, Standard_Boolean);
7079 -- Check for cases where left argument is known to be True or False
7081 if Compile_Time_Known_Value (Left) then
7083 -- If left argument is False, change (False or else Right) to Right.
7084 -- Any actions associated with Right will be executed unconditionally
7085 -- and can thus be inserted into the tree unconditionally.
7087 if Expr_Value_E (Left) = Standard_False then
7088 if Present (Actions (N)) then
7089 Insert_Actions (N, Actions (N));
7094 -- If left argument is True, change (True and then Right) to True. In
7095 -- this case we can forget the actions associated with Right, since
7096 -- they will never be executed.
7098 else pragma Assert (Expr_Value_E (Left) = Standard_True);
7099 Kill_Dead_Code (Right);
7100 Kill_Dead_Code (Actions (N));
7101 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
7104 Adjust_Result_Type (N, Typ);
7108 -- If Actions are present, we expand
7110 -- left or else right
7114 -- if left then True else right end
7116 -- with the actions becoming the Else_Actions of the conditional
7117 -- expression. This conditional expression is then further expanded
7118 -- (and will eventually disappear)
7120 if Present (Actions (N)) then
7121 Actlist := Actions (N);
7123 Make_Conditional_Expression (Loc,
7124 Expressions => New_List (
7126 New_Occurrence_Of (Standard_True, Loc),
7129 Set_Else_Actions (N, Actlist);
7130 Analyze_And_Resolve (N, Standard_Boolean);
7131 Adjust_Result_Type (N, Typ);
7135 -- No actions present, check for cases of right argument True/False
7137 if Compile_Time_Known_Value (Right) then
7139 -- Change (Left or else False) to Left. Note that we know there are
7140 -- no actions associated with the True operand, since we just checked
7141 -- for this case above.
7143 if Expr_Value_E (Right) = Standard_False then
7146 -- Change (Left or else True) to True, making sure to preserve any
7147 -- side effects associated with the Left operand.
7149 else pragma Assert (Expr_Value_E (Right) = Standard_True);
7150 Remove_Side_Effects (Left);
7152 (N, New_Occurrence_Of (Standard_True, Loc));
7156 Adjust_Result_Type (N, Typ);
7157 end Expand_N_Or_Else;
7159 -----------------------------------
7160 -- Expand_N_Qualified_Expression --
7161 -----------------------------------
7163 procedure Expand_N_Qualified_Expression (N : Node_Id) is
7164 Operand : constant Node_Id := Expression (N);
7165 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
7168 -- Do validity check if validity checking operands
7170 if Validity_Checks_On
7171 and then Validity_Check_Operands
7173 Ensure_Valid (Operand);
7176 -- Apply possible constraint check
7178 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
7180 if Do_Range_Check (Operand) then
7181 Set_Do_Range_Check (Operand, False);
7182 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
7184 end Expand_N_Qualified_Expression;
7186 ---------------------------------
7187 -- Expand_N_Selected_Component --
7188 ---------------------------------
7190 -- If the selector is a discriminant of a concurrent object, rewrite the
7191 -- prefix to denote the corresponding record type.
7193 procedure Expand_N_Selected_Component (N : Node_Id) is
7194 Loc : constant Source_Ptr := Sloc (N);
7195 Par : constant Node_Id := Parent (N);
7196 P : constant Node_Id := Prefix (N);
7197 Ptyp : Entity_Id := Underlying_Type (Etype (P));
7202 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
7203 -- Gigi needs a temporary for prefixes that depend on a discriminant,
7204 -- unless the context of an assignment can provide size information.
7205 -- Don't we have a general routine that does this???
7207 -----------------------
7208 -- In_Left_Hand_Side --
7209 -----------------------
7211 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
7213 return (Nkind (Parent (Comp)) = N_Assignment_Statement
7214 and then Comp = Name (Parent (Comp)))
7215 or else (Present (Parent (Comp))
7216 and then Nkind (Parent (Comp)) in N_Subexpr
7217 and then In_Left_Hand_Side (Parent (Comp)));
7218 end In_Left_Hand_Side;
7220 -- Start of processing for Expand_N_Selected_Component
7223 -- Insert explicit dereference if required
7225 if Is_Access_Type (Ptyp) then
7226 Insert_Explicit_Dereference (P);
7227 Analyze_And_Resolve (P, Designated_Type (Ptyp));
7229 if Ekind (Etype (P)) = E_Private_Subtype
7230 and then Is_For_Access_Subtype (Etype (P))
7232 Set_Etype (P, Base_Type (Etype (P)));
7238 -- Deal with discriminant check required
7240 if Do_Discriminant_Check (N) then
7242 -- Present the discriminant checking function to the backend, so that
7243 -- it can inline the call to the function.
7246 (Discriminant_Checking_Func
7247 (Original_Record_Component (Entity (Selector_Name (N)))));
7249 -- Now reset the flag and generate the call
7251 Set_Do_Discriminant_Check (N, False);
7252 Generate_Discriminant_Check (N);
7255 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7256 -- function, then additional actuals must be passed.
7258 if Ada_Version >= Ada_05
7259 and then Is_Build_In_Place_Function_Call (P)
7261 Make_Build_In_Place_Call_In_Anonymous_Context (P);
7264 -- Gigi cannot handle unchecked conversions that are the prefix of a
7265 -- selected component with discriminants. This must be checked during
7266 -- expansion, because during analysis the type of the selector is not
7267 -- known at the point the prefix is analyzed. If the conversion is the
7268 -- target of an assignment, then we cannot force the evaluation.
7270 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
7271 and then Has_Discriminants (Etype (N))
7272 and then not In_Left_Hand_Side (N)
7274 Force_Evaluation (Prefix (N));
7277 -- Remaining processing applies only if selector is a discriminant
7279 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
7281 -- If the selector is a discriminant of a constrained record type,
7282 -- we may be able to rewrite the expression with the actual value
7283 -- of the discriminant, a useful optimization in some cases.
7285 if Is_Record_Type (Ptyp)
7286 and then Has_Discriminants (Ptyp)
7287 and then Is_Constrained (Ptyp)
7289 -- Do this optimization for discrete types only, and not for
7290 -- access types (access discriminants get us into trouble!)
7292 if not Is_Discrete_Type (Etype (N)) then
7295 -- Don't do this on the left hand of an assignment statement.
7296 -- Normally one would think that references like this would
7297 -- not occur, but they do in generated code, and mean that
7298 -- we really do want to assign the discriminant!
7300 elsif Nkind (Par) = N_Assignment_Statement
7301 and then Name (Par) = N
7305 -- Don't do this optimization for the prefix of an attribute or
7306 -- the operand of an object renaming declaration since these are
7307 -- contexts where we do not want the value anyway.
7309 elsif (Nkind (Par) = N_Attribute_Reference
7310 and then Prefix (Par) = N)
7311 or else Is_Renamed_Object (N)
7315 -- Don't do this optimization if we are within the code for a
7316 -- discriminant check, since the whole point of such a check may
7317 -- be to verify the condition on which the code below depends!
7319 elsif Is_In_Discriminant_Check (N) then
7322 -- Green light to see if we can do the optimization. There is
7323 -- still one condition that inhibits the optimization below but
7324 -- now is the time to check the particular discriminant.
7327 -- Loop through discriminants to find the matching discriminant
7328 -- constraint to see if we can copy it.
7330 Disc := First_Discriminant (Ptyp);
7331 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
7332 Discr_Loop : while Present (Dcon) loop
7334 -- Check if this is the matching discriminant
7336 if Disc = Entity (Selector_Name (N)) then
7338 -- Here we have the matching discriminant. Check for
7339 -- the case of a discriminant of a component that is
7340 -- constrained by an outer discriminant, which cannot
7341 -- be optimized away.
7344 Denotes_Discriminant
7345 (Node (Dcon), Check_Concurrent => True)
7349 -- In the context of a case statement, the expression may
7350 -- have the base type of the discriminant, and we need to
7351 -- preserve the constraint to avoid spurious errors on
7354 elsif Nkind (Parent (N)) = N_Case_Statement
7355 and then Etype (Node (Dcon)) /= Etype (Disc)
7358 Make_Qualified_Expression (Loc,
7360 New_Occurrence_Of (Etype (Disc), Loc),
7362 New_Copy_Tree (Node (Dcon))));
7363 Analyze_And_Resolve (N, Etype (Disc));
7365 -- In case that comes out as a static expression,
7366 -- reset it (a selected component is never static).
7368 Set_Is_Static_Expression (N, False);
7371 -- Otherwise we can just copy the constraint, but the
7372 -- result is certainly not static! In some cases the
7373 -- discriminant constraint has been analyzed in the
7374 -- context of the original subtype indication, but for
7375 -- itypes the constraint might not have been analyzed
7376 -- yet, and this must be done now.
7379 Rewrite (N, New_Copy_Tree (Node (Dcon)));
7380 Analyze_And_Resolve (N);
7381 Set_Is_Static_Expression (N, False);
7387 Next_Discriminant (Disc);
7388 end loop Discr_Loop;
7390 -- Note: the above loop should always find a matching
7391 -- discriminant, but if it does not, we just missed an
7392 -- optimization due to some glitch (perhaps a previous error),
7398 -- The only remaining processing is in the case of a discriminant of
7399 -- a concurrent object, where we rewrite the prefix to denote the
7400 -- corresponding record type. If the type is derived and has renamed
7401 -- discriminants, use corresponding discriminant, which is the one
7402 -- that appears in the corresponding record.
7404 if not Is_Concurrent_Type (Ptyp) then
7408 Disc := Entity (Selector_Name (N));
7410 if Is_Derived_Type (Ptyp)
7411 and then Present (Corresponding_Discriminant (Disc))
7413 Disc := Corresponding_Discriminant (Disc);
7417 Make_Selected_Component (Loc,
7419 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
7421 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
7426 end Expand_N_Selected_Component;
7428 --------------------
7429 -- Expand_N_Slice --
7430 --------------------
7432 procedure Expand_N_Slice (N : Node_Id) is
7433 Loc : constant Source_Ptr := Sloc (N);
7434 Typ : constant Entity_Id := Etype (N);
7435 Pfx : constant Node_Id := Prefix (N);
7436 Ptp : Entity_Id := Etype (Pfx);
7438 function Is_Procedure_Actual (N : Node_Id) return Boolean;
7439 -- Check whether the argument is an actual for a procedure call, in
7440 -- which case the expansion of a bit-packed slice is deferred until the
7441 -- call itself is expanded. The reason this is required is that we might
7442 -- have an IN OUT or OUT parameter, and the copy out is essential, and
7443 -- that copy out would be missed if we created a temporary here in
7444 -- Expand_N_Slice. Note that we don't bother to test specifically for an
7445 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
7446 -- is harmless to defer expansion in the IN case, since the call
7447 -- processing will still generate the appropriate copy in operation,
7448 -- which will take care of the slice.
7450 procedure Make_Temporary_For_Slice;
7451 -- Create a named variable for the value of the slice, in cases where
7452 -- the back-end cannot handle it properly, e.g. when packed types or
7453 -- unaligned slices are involved.
7455 -------------------------
7456 -- Is_Procedure_Actual --
7457 -------------------------
7459 function Is_Procedure_Actual (N : Node_Id) return Boolean is
7460 Par : Node_Id := Parent (N);
7464 -- If our parent is a procedure call we can return
7466 if Nkind (Par) = N_Procedure_Call_Statement then
7469 -- If our parent is a type conversion, keep climbing the tree,
7470 -- since a type conversion can be a procedure actual. Also keep
7471 -- climbing if parameter association or a qualified expression,
7472 -- since these are additional cases that do can appear on
7473 -- procedure actuals.
7475 elsif Nkind_In (Par, N_Type_Conversion,
7476 N_Parameter_Association,
7477 N_Qualified_Expression)
7479 Par := Parent (Par);
7481 -- Any other case is not what we are looking for
7487 end Is_Procedure_Actual;
7489 ------------------------------
7490 -- Make_Temporary_For_Slice --
7491 ------------------------------
7493 procedure Make_Temporary_For_Slice is
7495 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
7498 Make_Object_Declaration (Loc,
7499 Defining_Identifier => Ent,
7500 Object_Definition => New_Occurrence_Of (Typ, Loc));
7502 Set_No_Initialization (Decl);
7504 Insert_Actions (N, New_List (
7506 Make_Assignment_Statement (Loc,
7507 Name => New_Occurrence_Of (Ent, Loc),
7508 Expression => Relocate_Node (N))));
7510 Rewrite (N, New_Occurrence_Of (Ent, Loc));
7511 Analyze_And_Resolve (N, Typ);
7512 end Make_Temporary_For_Slice;
7514 -- Start of processing for Expand_N_Slice
7517 -- Special handling for access types
7519 if Is_Access_Type (Ptp) then
7521 Ptp := Designated_Type (Ptp);
7524 Make_Explicit_Dereference (Sloc (N),
7525 Prefix => Relocate_Node (Pfx)));
7527 Analyze_And_Resolve (Pfx, Ptp);
7530 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7531 -- function, then additional actuals must be passed.
7533 if Ada_Version >= Ada_05
7534 and then Is_Build_In_Place_Function_Call (Pfx)
7536 Make_Build_In_Place_Call_In_Anonymous_Context (Pfx);
7539 -- The remaining case to be handled is packed slices. We can leave
7540 -- packed slices as they are in the following situations:
7542 -- 1. Right or left side of an assignment (we can handle this
7543 -- situation correctly in the assignment statement expansion).
7545 -- 2. Prefix of indexed component (the slide is optimized away in this
7546 -- case, see the start of Expand_N_Slice.)
7548 -- 3. Object renaming declaration, since we want the name of the
7549 -- slice, not the value.
7551 -- 4. Argument to procedure call, since copy-in/copy-out handling may
7552 -- be required, and this is handled in the expansion of call
7555 -- 5. Prefix of an address attribute (this is an error which is caught
7556 -- elsewhere, and the expansion would interfere with generating the
7559 if not Is_Packed (Typ) then
7561 -- Apply transformation for actuals of a function call, where
7562 -- Expand_Actuals is not used.
7564 if Nkind (Parent (N)) = N_Function_Call
7565 and then Is_Possibly_Unaligned_Slice (N)
7567 Make_Temporary_For_Slice;
7570 elsif Nkind (Parent (N)) = N_Assignment_Statement
7571 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
7572 and then Parent (N) = Name (Parent (Parent (N))))
7576 elsif Nkind (Parent (N)) = N_Indexed_Component
7577 or else Is_Renamed_Object (N)
7578 or else Is_Procedure_Actual (N)
7582 elsif Nkind (Parent (N)) = N_Attribute_Reference
7583 and then Attribute_Name (Parent (N)) = Name_Address
7588 Make_Temporary_For_Slice;
7592 ------------------------------
7593 -- Expand_N_Type_Conversion --
7594 ------------------------------
7596 procedure Expand_N_Type_Conversion (N : Node_Id) is
7597 Loc : constant Source_Ptr := Sloc (N);
7598 Operand : constant Node_Id := Expression (N);
7599 Target_Type : constant Entity_Id := Etype (N);
7600 Operand_Type : Entity_Id := Etype (Operand);
7602 procedure Handle_Changed_Representation;
7603 -- This is called in the case of record and array type conversions to
7604 -- see if there is a change of representation to be handled. Change of
7605 -- representation is actually handled at the assignment statement level,
7606 -- and what this procedure does is rewrite node N conversion as an
7607 -- assignment to temporary. If there is no change of representation,
7608 -- then the conversion node is unchanged.
7610 procedure Raise_Accessibility_Error;
7611 -- Called when we know that an accessibility check will fail. Rewrites
7612 -- node N to an appropriate raise statement and outputs warning msgs.
7613 -- The Etype of the raise node is set to Target_Type.
7615 procedure Real_Range_Check;
7616 -- Handles generation of range check for real target value
7618 -----------------------------------
7619 -- Handle_Changed_Representation --
7620 -----------------------------------
7622 procedure Handle_Changed_Representation is
7632 -- Nothing else to do if no change of representation
7634 if Same_Representation (Operand_Type, Target_Type) then
7637 -- The real change of representation work is done by the assignment
7638 -- statement processing. So if this type conversion is appearing as
7639 -- the expression of an assignment statement, nothing needs to be
7640 -- done to the conversion.
7642 elsif Nkind (Parent (N)) = N_Assignment_Statement then
7645 -- Otherwise we need to generate a temporary variable, and do the
7646 -- change of representation assignment into that temporary variable.
7647 -- The conversion is then replaced by a reference to this variable.
7652 -- If type is unconstrained we have to add a constraint, copied
7653 -- from the actual value of the left hand side.
7655 if not Is_Constrained (Target_Type) then
7656 if Has_Discriminants (Operand_Type) then
7657 Disc := First_Discriminant (Operand_Type);
7659 if Disc /= First_Stored_Discriminant (Operand_Type) then
7660 Disc := First_Stored_Discriminant (Operand_Type);
7664 while Present (Disc) loop
7666 Make_Selected_Component (Loc,
7667 Prefix => Duplicate_Subexpr_Move_Checks (Operand),
7669 Make_Identifier (Loc, Chars (Disc))));
7670 Next_Discriminant (Disc);
7673 elsif Is_Array_Type (Operand_Type) then
7674 N_Ix := First_Index (Target_Type);
7677 for J in 1 .. Number_Dimensions (Operand_Type) loop
7679 -- We convert the bounds explicitly. We use an unchecked
7680 -- conversion because bounds checks are done elsewhere.
7685 Unchecked_Convert_To (Etype (N_Ix),
7686 Make_Attribute_Reference (Loc,
7688 Duplicate_Subexpr_No_Checks
7689 (Operand, Name_Req => True),
7690 Attribute_Name => Name_First,
7691 Expressions => New_List (
7692 Make_Integer_Literal (Loc, J)))),
7695 Unchecked_Convert_To (Etype (N_Ix),
7696 Make_Attribute_Reference (Loc,
7698 Duplicate_Subexpr_No_Checks
7699 (Operand, Name_Req => True),
7700 Attribute_Name => Name_Last,
7701 Expressions => New_List (
7702 Make_Integer_Literal (Loc, J))))));
7709 Odef := New_Occurrence_Of (Target_Type, Loc);
7711 if Present (Cons) then
7713 Make_Subtype_Indication (Loc,
7714 Subtype_Mark => Odef,
7716 Make_Index_Or_Discriminant_Constraint (Loc,
7717 Constraints => Cons));
7720 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
7722 Make_Object_Declaration (Loc,
7723 Defining_Identifier => Temp,
7724 Object_Definition => Odef);
7726 Set_No_Initialization (Decl, True);
7728 -- Insert required actions. It is essential to suppress checks
7729 -- since we have suppressed default initialization, which means
7730 -- that the variable we create may have no discriminants.
7735 Make_Assignment_Statement (Loc,
7736 Name => New_Occurrence_Of (Temp, Loc),
7737 Expression => Relocate_Node (N))),
7738 Suppress => All_Checks);
7740 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7743 end Handle_Changed_Representation;
7745 -------------------------------
7746 -- Raise_Accessibility_Error --
7747 -------------------------------
7749 procedure Raise_Accessibility_Error is
7752 Make_Raise_Program_Error (Sloc (N),
7753 Reason => PE_Accessibility_Check_Failed));
7754 Set_Etype (N, Target_Type);
7756 Error_Msg_N ("?accessibility check failure", N);
7758 ("\?& will be raised at run time", N, Standard_Program_Error);
7759 end Raise_Accessibility_Error;
7761 ----------------------
7762 -- Real_Range_Check --
7763 ----------------------
7765 -- Case of conversions to floating-point or fixed-point. If range checks
7766 -- are enabled and the target type has a range constraint, we convert:
7772 -- Tnn : typ'Base := typ'Base (x);
7773 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
7776 -- This is necessary when there is a conversion of integer to float or
7777 -- to fixed-point to ensure that the correct checks are made. It is not
7778 -- necessary for float to float where it is enough to simply set the
7779 -- Do_Range_Check flag.
7781 procedure Real_Range_Check is
7782 Btyp : constant Entity_Id := Base_Type (Target_Type);
7783 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
7784 Hi : constant Node_Id := Type_High_Bound (Target_Type);
7785 Xtyp : constant Entity_Id := Etype (Operand);
7790 -- Nothing to do if conversion was rewritten
7792 if Nkind (N) /= N_Type_Conversion then
7796 -- Nothing to do if range checks suppressed, or target has the same
7797 -- range as the base type (or is the base type).
7799 if Range_Checks_Suppressed (Target_Type)
7800 or else (Lo = Type_Low_Bound (Btyp)
7802 Hi = Type_High_Bound (Btyp))
7807 -- Nothing to do if expression is an entity on which checks have been
7810 if Is_Entity_Name (Operand)
7811 and then Range_Checks_Suppressed (Entity (Operand))
7816 -- Nothing to do if bounds are all static and we can tell that the
7817 -- expression is within the bounds of the target. Note that if the
7818 -- operand is of an unconstrained floating-point type, then we do
7819 -- not trust it to be in range (might be infinite)
7822 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
7823 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
7826 if (not Is_Floating_Point_Type (Xtyp)
7827 or else Is_Constrained (Xtyp))
7828 and then Compile_Time_Known_Value (S_Lo)
7829 and then Compile_Time_Known_Value (S_Hi)
7830 and then Compile_Time_Known_Value (Hi)
7831 and then Compile_Time_Known_Value (Lo)
7834 D_Lov : constant Ureal := Expr_Value_R (Lo);
7835 D_Hiv : constant Ureal := Expr_Value_R (Hi);
7840 if Is_Real_Type (Xtyp) then
7841 S_Lov := Expr_Value_R (S_Lo);
7842 S_Hiv := Expr_Value_R (S_Hi);
7844 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
7845 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
7849 and then S_Lov >= D_Lov
7850 and then S_Hiv <= D_Hiv
7852 Set_Do_Range_Check (Operand, False);
7859 -- For float to float conversions, we are done
7861 if Is_Floating_Point_Type (Xtyp)
7863 Is_Floating_Point_Type (Btyp)
7868 -- Otherwise rewrite the conversion as described above
7870 Conv := Relocate_Node (N);
7871 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
7872 Set_Etype (Conv, Btyp);
7874 -- Enable overflow except for case of integer to float conversions,
7875 -- where it is never required, since we can never have overflow in
7878 if not Is_Integer_Type (Etype (Operand)) then
7879 Enable_Overflow_Check (Conv);
7883 Make_Defining_Identifier (Loc,
7884 Chars => New_Internal_Name ('T'));
7886 Insert_Actions (N, New_List (
7887 Make_Object_Declaration (Loc,
7888 Defining_Identifier => Tnn,
7889 Object_Definition => New_Occurrence_Of (Btyp, Loc),
7890 Expression => Conv),
7892 Make_Raise_Constraint_Error (Loc,
7897 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
7899 Make_Attribute_Reference (Loc,
7900 Attribute_Name => Name_First,
7902 New_Occurrence_Of (Target_Type, Loc))),
7906 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
7908 Make_Attribute_Reference (Loc,
7909 Attribute_Name => Name_Last,
7911 New_Occurrence_Of (Target_Type, Loc)))),
7912 Reason => CE_Range_Check_Failed)));
7914 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
7915 Analyze_And_Resolve (N, Btyp);
7916 end Real_Range_Check;
7918 -- Start of processing for Expand_N_Type_Conversion
7921 -- Nothing at all to do if conversion is to the identical type so remove
7922 -- the conversion completely, it is useless, except that it may carry
7923 -- an Assignment_OK attribute, which must be propagated to the operand.
7925 if Operand_Type = Target_Type then
7926 if Assignment_OK (N) then
7927 Set_Assignment_OK (Operand);
7930 Rewrite (N, Relocate_Node (Operand));
7934 -- Nothing to do if this is the second argument of read. This is a
7935 -- "backwards" conversion that will be handled by the specialized code
7936 -- in attribute processing.
7938 if Nkind (Parent (N)) = N_Attribute_Reference
7939 and then Attribute_Name (Parent (N)) = Name_Read
7940 and then Next (First (Expressions (Parent (N)))) = N
7945 -- Here if we may need to expand conversion
7947 -- If the operand of the type conversion is an arithmetic operation on
7948 -- signed integers, and the based type of the signed integer type in
7949 -- question is smaller than Standard.Integer, we promote both of the
7950 -- operands to type Integer.
7952 -- For example, if we have
7954 -- target-type (opnd1 + opnd2)
7956 -- and opnd1 and opnd2 are of type short integer, then we rewrite
7959 -- target-type (integer(opnd1) + integer(opnd2))
7961 -- We do this because we are always allowed to compute in a larger type
7962 -- if we do the right thing with the result, and in this case we are
7963 -- going to do a conversion which will do an appropriate check to make
7964 -- sure that things are in range of the target type in any case. This
7965 -- avoids some unnecessary intermediate overflows.
7967 -- We might consider a similar transformation in the case where the
7968 -- target is a real type or a 64-bit integer type, and the operand
7969 -- is an arithmetic operation using a 32-bit integer type. However,
7970 -- we do not bother with this case, because it could cause significant
7971 -- ineffiencies on 32-bit machines. On a 64-bit machine it would be
7972 -- much cheaper, but we don't want different behavior on 32-bit and
7973 -- 64-bit machines. Note that the exclusion of the 64-bit case also
7974 -- handles the configurable run-time cases where 64-bit arithmetic
7975 -- may simply be unavailable.
7977 -- Note: this circuit is partially redundant with respect to the circuit
7978 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
7979 -- the processing here. Also we still need the Checks circuit, since we
7980 -- have to be sure not to generate junk overflow checks in the first
7981 -- place, since it would be trick to remove them here!
7984 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
7987 -- Enable transformation if all conditions are met
7990 -- We only do this transformation for source constructs. We assume
7991 -- that the expander knows what it is doing when it generates code.
7993 Comes_From_Source (N)
7995 -- If the operand type is Short_Integer or Short_Short_Integer,
7996 -- then we will promote to Integer, which is available on all
7997 -- targets, and is sufficient to ensure no intermediate overflow.
7998 -- Furthermore it is likely to be as efficient or more efficient
7999 -- than using the smaller type for the computation so we do this
8003 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
8005 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
8007 -- Test for interesting operation, which includes addition,
8008 -- division, exponentiation, multiplication, subtraction, and
8011 and then Nkind_In (Operand, N_Op_Add,
8018 -- All conditions met, go ahead with transformation
8026 Make_Type_Conversion (Loc,
8027 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
8028 Expression => Relocate_Node (Right_Opnd (Operand)));
8030 if Nkind (Operand) = N_Op_Minus then
8031 Opnd := Make_Op_Minus (Loc, Right_Opnd => R);
8035 Make_Type_Conversion (Loc,
8036 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
8037 Expression => Relocate_Node (Left_Opnd (Operand)));
8039 case Nkind (Operand) is
8041 Opnd := Make_Op_Add (Loc, L, R);
8043 Opnd := Make_Op_Divide (Loc, L, R);
8045 Opnd := Make_Op_Expon (Loc, L, R);
8046 when N_Op_Multiply =>
8047 Opnd := Make_Op_Multiply (Loc, L, R);
8048 when N_Op_Subtract =>
8049 Opnd := Make_Op_Subtract (Loc, L, R);
8051 raise Program_Error;
8055 Make_Type_Conversion (Loc,
8056 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
8057 Expression => Opnd));
8059 Analyze_And_Resolve (N, Target_Type);
8066 -- Do validity check if validity checking operands
8068 if Validity_Checks_On
8069 and then Validity_Check_Operands
8071 Ensure_Valid (Operand);
8074 -- Special case of converting from non-standard boolean type
8076 if Is_Boolean_Type (Operand_Type)
8077 and then (Nonzero_Is_True (Operand_Type))
8079 Adjust_Condition (Operand);
8080 Set_Etype (Operand, Standard_Boolean);
8081 Operand_Type := Standard_Boolean;
8084 -- Case of converting to an access type
8086 if Is_Access_Type (Target_Type) then
8088 -- Apply an accessibility check when the conversion operand is an
8089 -- access parameter (or a renaming thereof), unless conversion was
8090 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
8091 -- Note that other checks may still need to be applied below (such
8092 -- as tagged type checks).
8094 if Is_Entity_Name (Operand)
8096 (Is_Formal (Entity (Operand))
8098 (Present (Renamed_Object (Entity (Operand)))
8099 and then Is_Entity_Name (Renamed_Object (Entity (Operand)))
8101 (Entity (Renamed_Object (Entity (Operand))))))
8102 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
8103 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
8104 or else Attribute_Name (Original_Node (N)) = Name_Access)
8106 Apply_Accessibility_Check
8107 (Operand, Target_Type, Insert_Node => Operand);
8109 -- If the level of the operand type is statically deeper than the
8110 -- level of the target type, then force Program_Error. Note that this
8111 -- can only occur for cases where the attribute is within the body of
8112 -- an instantiation (otherwise the conversion will already have been
8113 -- rejected as illegal). Note: warnings are issued by the analyzer
8114 -- for the instance cases.
8116 elsif In_Instance_Body
8117 and then Type_Access_Level (Operand_Type) >
8118 Type_Access_Level (Target_Type)
8120 Raise_Accessibility_Error;
8122 -- When the operand is a selected access discriminant the check needs
8123 -- to be made against the level of the object denoted by the prefix
8124 -- of the selected name. Force Program_Error for this case as well
8125 -- (this accessibility violation can only happen if within the body
8126 -- of an instantiation).
8128 elsif In_Instance_Body
8129 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
8130 and then Nkind (Operand) = N_Selected_Component
8131 and then Object_Access_Level (Operand) >
8132 Type_Access_Level (Target_Type)
8134 Raise_Accessibility_Error;
8139 -- Case of conversions of tagged types and access to tagged types
8141 -- When needed, that is to say when the expression is class-wide, Add
8142 -- runtime a tag check for (strict) downward conversion by using the
8143 -- membership test, generating:
8145 -- [constraint_error when Operand not in Target_Type'Class]
8147 -- or in the access type case
8149 -- [constraint_error
8150 -- when Operand /= null
8151 -- and then Operand.all not in
8152 -- Designated_Type (Target_Type)'Class]
8154 if (Is_Access_Type (Target_Type)
8155 and then Is_Tagged_Type (Designated_Type (Target_Type)))
8156 or else Is_Tagged_Type (Target_Type)
8158 -- Do not do any expansion in the access type case if the parent is a
8159 -- renaming, since this is an error situation which will be caught by
8160 -- Sem_Ch8, and the expansion can interfere with this error check.
8162 if Is_Access_Type (Target_Type)
8163 and then Is_Renamed_Object (N)
8168 -- Otherwise, proceed with processing tagged conversion
8171 Actual_Op_Typ : Entity_Id;
8172 Actual_Targ_Typ : Entity_Id;
8173 Make_Conversion : Boolean := False;
8174 Root_Op_Typ : Entity_Id;
8176 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
8177 -- Create a membership check to test whether Operand is a member
8178 -- of Targ_Typ. If the original Target_Type is an access, include
8179 -- a test for null value. The check is inserted at N.
8181 --------------------
8182 -- Make_Tag_Check --
8183 --------------------
8185 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
8190 -- [Constraint_Error
8191 -- when Operand /= null
8192 -- and then Operand.all not in Targ_Typ]
8194 if Is_Access_Type (Target_Type) then
8199 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
8200 Right_Opnd => Make_Null (Loc)),
8205 Make_Explicit_Dereference (Loc,
8206 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
8207 Right_Opnd => New_Reference_To (Targ_Typ, Loc)));
8210 -- [Constraint_Error when Operand not in Targ_Typ]
8215 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
8216 Right_Opnd => New_Reference_To (Targ_Typ, Loc));
8220 Make_Raise_Constraint_Error (Loc,
8222 Reason => CE_Tag_Check_Failed));
8225 -- Start of processing
8228 if Is_Access_Type (Target_Type) then
8230 -- Handle entities from the limited view
8233 Available_View (Designated_Type (Operand_Type));
8235 Available_View (Designated_Type (Target_Type));
8237 Actual_Op_Typ := Operand_Type;
8238 Actual_Targ_Typ := Target_Type;
8241 Root_Op_Typ := Root_Type (Actual_Op_Typ);
8243 -- Ada 2005 (AI-251): Handle interface type conversion
8245 if Is_Interface (Actual_Op_Typ) then
8246 Expand_Interface_Conversion (N, Is_Static => False);
8250 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
8252 -- Create a runtime tag check for a downward class-wide type
8255 if Is_Class_Wide_Type (Actual_Op_Typ)
8256 and then Actual_Op_Typ /= Actual_Targ_Typ
8257 and then Root_Op_Typ /= Actual_Targ_Typ
8258 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ)
8260 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
8261 Make_Conversion := True;
8264 -- AI05-0073: If the result subtype of the function is defined
8265 -- by an access_definition designating a specific tagged type
8266 -- T, a check is made that the result value is null or the tag
8267 -- of the object designated by the result value identifies T.
8268 -- Constraint_Error is raised if this check fails.
8270 if Nkind (Parent (N)) = Sinfo.N_Return_Statement then
8273 Func_Typ : Entity_Id;
8276 -- Climb scope stack looking for the enclosing function
8278 Func := Current_Scope;
8279 while Present (Func)
8280 and then Ekind (Func) /= E_Function
8282 Func := Scope (Func);
8285 -- The function's return subtype must be defined using
8286 -- an access definition.
8288 if Nkind (Result_Definition (Parent (Func))) =
8291 Func_Typ := Directly_Designated_Type (Etype (Func));
8293 -- The return subtype denotes a specific tagged type,
8294 -- in other words, a non class-wide type.
8296 if Is_Tagged_Type (Func_Typ)
8297 and then not Is_Class_Wide_Type (Func_Typ)
8299 Make_Tag_Check (Actual_Targ_Typ);
8300 Make_Conversion := True;
8306 -- We have generated a tag check for either a class-wide type
8307 -- conversion or for AI05-0073.
8309 if Make_Conversion then
8314 Make_Unchecked_Type_Conversion (Loc,
8315 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
8316 Expression => Relocate_Node (Expression (N)));
8318 Analyze_And_Resolve (N, Target_Type);
8324 -- Case of other access type conversions
8326 elsif Is_Access_Type (Target_Type) then
8327 Apply_Constraint_Check (Operand, Target_Type);
8329 -- Case of conversions from a fixed-point type
8331 -- These conversions require special expansion and processing, found in
8332 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
8333 -- since from a semantic point of view, these are simple integer
8334 -- conversions, which do not need further processing.
8336 elsif Is_Fixed_Point_Type (Operand_Type)
8337 and then not Conversion_OK (N)
8339 -- We should never see universal fixed at this case, since the
8340 -- expansion of the constituent divide or multiply should have
8341 -- eliminated the explicit mention of universal fixed.
8343 pragma Assert (Operand_Type /= Universal_Fixed);
8345 -- Check for special case of the conversion to universal real that
8346 -- occurs as a result of the use of a round attribute. In this case,
8347 -- the real type for the conversion is taken from the target type of
8348 -- the Round attribute and the result must be marked as rounded.
8350 if Target_Type = Universal_Real
8351 and then Nkind (Parent (N)) = N_Attribute_Reference
8352 and then Attribute_Name (Parent (N)) = Name_Round
8354 Set_Rounded_Result (N);
8355 Set_Etype (N, Etype (Parent (N)));
8358 -- Otherwise do correct fixed-conversion, but skip these if the
8359 -- Conversion_OK flag is set, because from a semantic point of
8360 -- view these are simple integer conversions needing no further
8361 -- processing (the backend will simply treat them as integers)
8363 if not Conversion_OK (N) then
8364 if Is_Fixed_Point_Type (Etype (N)) then
8365 Expand_Convert_Fixed_To_Fixed (N);
8368 elsif Is_Integer_Type (Etype (N)) then
8369 Expand_Convert_Fixed_To_Integer (N);
8372 pragma Assert (Is_Floating_Point_Type (Etype (N)));
8373 Expand_Convert_Fixed_To_Float (N);
8378 -- Case of conversions to a fixed-point type
8380 -- These conversions require special expansion and processing, found in
8381 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
8382 -- since from a semantic point of view, these are simple integer
8383 -- conversions, which do not need further processing.
8385 elsif Is_Fixed_Point_Type (Target_Type)
8386 and then not Conversion_OK (N)
8388 if Is_Integer_Type (Operand_Type) then
8389 Expand_Convert_Integer_To_Fixed (N);
8392 pragma Assert (Is_Floating_Point_Type (Operand_Type));
8393 Expand_Convert_Float_To_Fixed (N);
8397 -- Case of float-to-integer conversions
8399 -- We also handle float-to-fixed conversions with Conversion_OK set
8400 -- since semantically the fixed-point target is treated as though it
8401 -- were an integer in such cases.
8403 elsif Is_Floating_Point_Type (Operand_Type)
8405 (Is_Integer_Type (Target_Type)
8407 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
8409 -- One more check here, gcc is still not able to do conversions of
8410 -- this type with proper overflow checking, and so gigi is doing an
8411 -- approximation of what is required by doing floating-point compares
8412 -- with the end-point. But that can lose precision in some cases, and
8413 -- give a wrong result. Converting the operand to Universal_Real is
8414 -- helpful, but still does not catch all cases with 64-bit integers
8415 -- on targets with only 64-bit floats
8417 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
8418 -- Can this code be removed ???
8420 if Do_Range_Check (Operand) then
8422 Make_Type_Conversion (Loc,
8424 New_Occurrence_Of (Universal_Real, Loc),
8426 Relocate_Node (Operand)));
8428 Set_Etype (Operand, Universal_Real);
8429 Enable_Range_Check (Operand);
8430 Set_Do_Range_Check (Expression (Operand), False);
8433 -- Case of array conversions
8435 -- Expansion of array conversions, add required length/range checks but
8436 -- only do this if there is no change of representation. For handling of
8437 -- this case, see Handle_Changed_Representation.
8439 elsif Is_Array_Type (Target_Type) then
8441 if Is_Constrained (Target_Type) then
8442 Apply_Length_Check (Operand, Target_Type);
8444 Apply_Range_Check (Operand, Target_Type);
8447 Handle_Changed_Representation;
8449 -- Case of conversions of discriminated types
8451 -- Add required discriminant checks if target is constrained. Again this
8452 -- change is skipped if we have a change of representation.
8454 elsif Has_Discriminants (Target_Type)
8455 and then Is_Constrained (Target_Type)
8457 Apply_Discriminant_Check (Operand, Target_Type);
8458 Handle_Changed_Representation;
8460 -- Case of all other record conversions. The only processing required
8461 -- is to check for a change of representation requiring the special
8462 -- assignment processing.
8464 elsif Is_Record_Type (Target_Type) then
8466 -- Ada 2005 (AI-216): Program_Error is raised when converting from
8467 -- a derived Unchecked_Union type to an unconstrained type that is
8468 -- not Unchecked_Union if the operand lacks inferable discriminants.
8470 if Is_Derived_Type (Operand_Type)
8471 and then Is_Unchecked_Union (Base_Type (Operand_Type))
8472 and then not Is_Constrained (Target_Type)
8473 and then not Is_Unchecked_Union (Base_Type (Target_Type))
8474 and then not Has_Inferable_Discriminants (Operand)
8476 -- To prevent Gigi from generating illegal code, we generate a
8477 -- Program_Error node, but we give it the target type of the
8481 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
8482 Reason => PE_Unchecked_Union_Restriction);
8485 Set_Etype (PE, Target_Type);
8490 Handle_Changed_Representation;
8493 -- Case of conversions of enumeration types
8495 elsif Is_Enumeration_Type (Target_Type) then
8497 -- Special processing is required if there is a change of
8498 -- representation (from enumeration representation clauses)
8500 if not Same_Representation (Target_Type, Operand_Type) then
8502 -- Convert: x(y) to x'val (ytyp'val (y))
8505 Make_Attribute_Reference (Loc,
8506 Prefix => New_Occurrence_Of (Target_Type, Loc),
8507 Attribute_Name => Name_Val,
8508 Expressions => New_List (
8509 Make_Attribute_Reference (Loc,
8510 Prefix => New_Occurrence_Of (Operand_Type, Loc),
8511 Attribute_Name => Name_Pos,
8512 Expressions => New_List (Operand)))));
8514 Analyze_And_Resolve (N, Target_Type);
8517 -- Case of conversions to floating-point
8519 elsif Is_Floating_Point_Type (Target_Type) then
8523 -- At this stage, either the conversion node has been transformed into
8524 -- some other equivalent expression, or left as a conversion that can
8525 -- be handled by Gigi. The conversions that Gigi can handle are the
8528 -- Conversions with no change of representation or type
8530 -- Numeric conversions involving integer, floating- and fixed-point
8531 -- values. Fixed-point values are allowed only if Conversion_OK is
8532 -- set, i.e. if the fixed-point values are to be treated as integers.
8534 -- No other conversions should be passed to Gigi
8536 -- Check: are these rules stated in sinfo??? if so, why restate here???
8538 -- The only remaining step is to generate a range check if we still have
8539 -- a type conversion at this stage and Do_Range_Check is set. For now we
8540 -- do this only for conversions of discrete types.
8542 if Nkind (N) = N_Type_Conversion
8543 and then Is_Discrete_Type (Etype (N))
8546 Expr : constant Node_Id := Expression (N);
8551 if Do_Range_Check (Expr)
8552 and then Is_Discrete_Type (Etype (Expr))
8554 Set_Do_Range_Check (Expr, False);
8556 -- Before we do a range check, we have to deal with treating a
8557 -- fixed-point operand as an integer. The way we do this is
8558 -- simply to do an unchecked conversion to an appropriate
8559 -- integer type large enough to hold the result.
8561 -- This code is not active yet, because we are only dealing
8562 -- with discrete types so far ???
8564 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
8565 and then Treat_Fixed_As_Integer (Expr)
8567 Ftyp := Base_Type (Etype (Expr));
8569 if Esize (Ftyp) >= Esize (Standard_Integer) then
8570 Ityp := Standard_Long_Long_Integer;
8572 Ityp := Standard_Integer;
8575 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
8578 -- Reset overflow flag, since the range check will include
8579 -- dealing with possible overflow, and generate the check If
8580 -- Address is either a source type or target type, suppress
8581 -- range check to avoid typing anomalies when it is a visible
8584 Set_Do_Overflow_Check (N, False);
8585 if not Is_Descendent_Of_Address (Etype (Expr))
8586 and then not Is_Descendent_Of_Address (Target_Type)
8588 Generate_Range_Check
8589 (Expr, Target_Type, CE_Range_Check_Failed);
8595 -- Final step, if the result is a type conversion involving Vax_Float
8596 -- types, then it is subject for further special processing.
8598 if Nkind (N) = N_Type_Conversion
8599 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
8601 Expand_Vax_Conversion (N);
8604 end Expand_N_Type_Conversion;
8606 -----------------------------------
8607 -- Expand_N_Unchecked_Expression --
8608 -----------------------------------
8610 -- Remove the unchecked expression node from the tree. It's job was simply
8611 -- to make sure that its constituent expression was handled with checks
8612 -- off, and now that that is done, we can remove it from the tree, and
8613 -- indeed must, since gigi does not expect to see these nodes.
8615 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
8616 Exp : constant Node_Id := Expression (N);
8619 Set_Assignment_OK (Exp, Assignment_OK (N) or Assignment_OK (Exp));
8621 end Expand_N_Unchecked_Expression;
8623 ----------------------------------------
8624 -- Expand_N_Unchecked_Type_Conversion --
8625 ----------------------------------------
8627 -- If this cannot be handled by Gigi and we haven't already made a
8628 -- temporary for it, do it now.
8630 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
8631 Target_Type : constant Entity_Id := Etype (N);
8632 Operand : constant Node_Id := Expression (N);
8633 Operand_Type : constant Entity_Id := Etype (Operand);
8636 -- Nothing at all to do if conversion is to the identical type so remove
8637 -- the conversion completely, it is useless, except that it may carry
8638 -- an Assignment_OK indication which must be proprgated to the operand.
8640 if Operand_Type = Target_Type then
8641 if Assignment_OK (N) then
8642 Set_Assignment_OK (Operand);
8645 Rewrite (N, Relocate_Node (Operand));
8649 -- If we have a conversion of a compile time known value to a target
8650 -- type and the value is in range of the target type, then we can simply
8651 -- replace the construct by an integer literal of the correct type. We
8652 -- only apply this to integer types being converted. Possibly it may
8653 -- apply in other cases, but it is too much trouble to worry about.
8655 -- Note that we do not do this transformation if the Kill_Range_Check
8656 -- flag is set, since then the value may be outside the expected range.
8657 -- This happens in the Normalize_Scalars case.
8659 -- We also skip this if either the target or operand type is biased
8660 -- because in this case, the unchecked conversion is supposed to
8661 -- preserve the bit pattern, not the integer value.
8663 if Is_Integer_Type (Target_Type)
8664 and then not Has_Biased_Representation (Target_Type)
8665 and then Is_Integer_Type (Operand_Type)
8666 and then not Has_Biased_Representation (Operand_Type)
8667 and then Compile_Time_Known_Value (Operand)
8668 and then not Kill_Range_Check (N)
8671 Val : constant Uint := Expr_Value (Operand);
8674 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
8676 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
8678 Val >= Expr_Value (Type_Low_Bound (Target_Type))
8680 Val <= Expr_Value (Type_High_Bound (Target_Type))
8682 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
8684 -- If Address is the target type, just set the type to avoid a
8685 -- spurious type error on the literal when Address is a visible
8688 if Is_Descendent_Of_Address (Target_Type) then
8689 Set_Etype (N, Target_Type);
8691 Analyze_And_Resolve (N, Target_Type);
8699 -- Nothing to do if conversion is safe
8701 if Safe_Unchecked_Type_Conversion (N) then
8705 -- Otherwise force evaluation unless Assignment_OK flag is set (this
8706 -- flag indicates ??? -- more comments needed here)
8708 if Assignment_OK (N) then
8711 Force_Evaluation (N);
8713 end Expand_N_Unchecked_Type_Conversion;
8715 ----------------------------
8716 -- Expand_Record_Equality --
8717 ----------------------------
8719 -- For non-variant records, Equality is expanded when needed into:
8721 -- and then Lhs.Discr1 = Rhs.Discr1
8723 -- and then Lhs.Discrn = Rhs.Discrn
8724 -- and then Lhs.Cmp1 = Rhs.Cmp1
8726 -- and then Lhs.Cmpn = Rhs.Cmpn
8728 -- The expression is folded by the back-end for adjacent fields. This
8729 -- function is called for tagged record in only one occasion: for imple-
8730 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
8731 -- otherwise the primitive "=" is used directly.
8733 function Expand_Record_Equality
8738 Bodies : List_Id) return Node_Id
8740 Loc : constant Source_Ptr := Sloc (Nod);
8745 First_Time : Boolean := True;
8747 function Suitable_Element (C : Entity_Id) return Entity_Id;
8748 -- Return the first field to compare beginning with C, skipping the
8749 -- inherited components.
8751 ----------------------
8752 -- Suitable_Element --
8753 ----------------------
8755 function Suitable_Element (C : Entity_Id) return Entity_Id is
8760 elsif Ekind (C) /= E_Discriminant
8761 and then Ekind (C) /= E_Component
8763 return Suitable_Element (Next_Entity (C));
8765 elsif Is_Tagged_Type (Typ)
8766 and then C /= Original_Record_Component (C)
8768 return Suitable_Element (Next_Entity (C));
8770 elsif Chars (C) = Name_uController
8771 or else Chars (C) = Name_uTag
8773 return Suitable_Element (Next_Entity (C));
8775 elsif Is_Interface (Etype (C)) then
8776 return Suitable_Element (Next_Entity (C));
8781 end Suitable_Element;
8783 -- Start of processing for Expand_Record_Equality
8786 -- Generates the following code: (assuming that Typ has one Discr and
8787 -- component C2 is also a record)
8790 -- and then Lhs.Discr1 = Rhs.Discr1
8791 -- and then Lhs.C1 = Rhs.C1
8792 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
8794 -- and then Lhs.Cmpn = Rhs.Cmpn
8796 Result := New_Reference_To (Standard_True, Loc);
8797 C := Suitable_Element (First_Entity (Typ));
8799 while Present (C) loop
8807 First_Time := False;
8811 New_Lhs := New_Copy_Tree (Lhs);
8812 New_Rhs := New_Copy_Tree (Rhs);
8816 Expand_Composite_Equality (Nod, Etype (C),
8818 Make_Selected_Component (Loc,
8820 Selector_Name => New_Reference_To (C, Loc)),
8822 Make_Selected_Component (Loc,
8824 Selector_Name => New_Reference_To (C, Loc)),
8827 -- If some (sub)component is an unchecked_union, the whole
8828 -- operation will raise program error.
8830 if Nkind (Check) = N_Raise_Program_Error then
8832 Set_Etype (Result, Standard_Boolean);
8837 Left_Opnd => Result,
8838 Right_Opnd => Check);
8842 C := Suitable_Element (Next_Entity (C));
8846 end Expand_Record_Equality;
8848 -------------------------------------
8849 -- Fixup_Universal_Fixed_Operation --
8850 -------------------------------------
8852 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
8853 Conv : constant Node_Id := Parent (N);
8856 -- We must have a type conversion immediately above us
8858 pragma Assert (Nkind (Conv) = N_Type_Conversion);
8860 -- Normally the type conversion gives our target type. The exception
8861 -- occurs in the case of the Round attribute, where the conversion
8862 -- will be to universal real, and our real type comes from the Round
8863 -- attribute (as well as an indication that we must round the result)
8865 if Nkind (Parent (Conv)) = N_Attribute_Reference
8866 and then Attribute_Name (Parent (Conv)) = Name_Round
8868 Set_Etype (N, Etype (Parent (Conv)));
8869 Set_Rounded_Result (N);
8871 -- Normal case where type comes from conversion above us
8874 Set_Etype (N, Etype (Conv));
8876 end Fixup_Universal_Fixed_Operation;
8878 ------------------------------
8879 -- Get_Allocator_Final_List --
8880 ------------------------------
8882 function Get_Allocator_Final_List
8885 PtrT : Entity_Id) return Entity_Id
8887 Loc : constant Source_Ptr := Sloc (N);
8889 Owner : Entity_Id := PtrT;
8890 -- The entity whose finalization list must be used to attach the
8891 -- allocated object.
8894 if Ekind (PtrT) = E_Anonymous_Access_Type then
8896 -- If the context is an access parameter, we need to create a
8897 -- non-anonymous access type in order to have a usable final list,
8898 -- because there is otherwise no pool to which the allocated object
8899 -- can belong. We create both the type and the finalization chain
8900 -- here, because freezing an internal type does not create such a
8901 -- chain. The Final_Chain that is thus created is shared by the
8902 -- access parameter. The access type is tested against the result
8903 -- type of the function to exclude allocators whose type is an
8904 -- anonymous access result type. We freeze the type at once to
8905 -- ensure that it is properly decorated for the back-end, even
8906 -- if the context and current scope is a loop.
8908 if Nkind (Associated_Node_For_Itype (PtrT))
8909 in N_Subprogram_Specification
8912 Etype (Defining_Unit_Name (Associated_Node_For_Itype (PtrT)))
8914 Owner := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
8916 Make_Full_Type_Declaration (Loc,
8917 Defining_Identifier => Owner,
8919 Make_Access_To_Object_Definition (Loc,
8920 Subtype_Indication =>
8921 New_Occurrence_Of (T, Loc))));
8923 Freeze_Before (N, Owner);
8924 Build_Final_List (N, Owner);
8925 Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner));
8927 -- Ada 2005 (AI-318-02): If the context is a return object
8928 -- declaration, then the anonymous return subtype is defined to have
8929 -- the same accessibility level as that of the function's result
8930 -- subtype, which means that we want the scope where the function is
8933 elsif Nkind (Associated_Node_For_Itype (PtrT)) = N_Object_Declaration
8934 and then Ekind (Scope (PtrT)) = E_Return_Statement
8936 Owner := Scope (Return_Applies_To (Scope (PtrT)));
8938 -- Case of an access discriminant, or (Ada 2005), of an anonymous
8939 -- access component or anonymous access function result: find the
8940 -- final list associated with the scope of the type. (In the
8941 -- anonymous access component kind, a list controller will have
8942 -- been allocated when freezing the record type, and PtrT has an
8943 -- Associated_Final_Chain attribute designating it.)
8945 elsif No (Associated_Final_Chain (PtrT)) then
8946 Owner := Scope (PtrT);
8950 return Find_Final_List (Owner);
8951 end Get_Allocator_Final_List;
8953 ---------------------------------
8954 -- Has_Inferable_Discriminants --
8955 ---------------------------------
8957 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
8959 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
8960 -- Determines whether the left-most prefix of a selected component is a
8961 -- formal parameter in a subprogram. Assumes N is a selected component.
8963 --------------------------------
8964 -- Prefix_Is_Formal_Parameter --
8965 --------------------------------
8967 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
8968 Sel_Comp : Node_Id := N;
8971 -- Move to the left-most prefix by climbing up the tree
8973 while Present (Parent (Sel_Comp))
8974 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
8976 Sel_Comp := Parent (Sel_Comp);
8979 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
8980 end Prefix_Is_Formal_Parameter;
8982 -- Start of processing for Has_Inferable_Discriminants
8985 -- For identifiers and indexed components, it is sufficient to have a
8986 -- constrained Unchecked_Union nominal subtype.
8988 if Nkind_In (N, N_Identifier, N_Indexed_Component) then
8989 return Is_Unchecked_Union (Base_Type (Etype (N)))
8991 Is_Constrained (Etype (N));
8993 -- For selected components, the subtype of the selector must be a
8994 -- constrained Unchecked_Union. If the component is subject to a
8995 -- per-object constraint, then the enclosing object must have inferable
8998 elsif Nkind (N) = N_Selected_Component then
8999 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
9001 -- A small hack. If we have a per-object constrained selected
9002 -- component of a formal parameter, return True since we do not
9003 -- know the actual parameter association yet.
9005 if Prefix_Is_Formal_Parameter (N) then
9009 -- Otherwise, check the enclosing object and the selector
9011 return Has_Inferable_Discriminants (Prefix (N))
9013 Has_Inferable_Discriminants (Selector_Name (N));
9016 -- The call to Has_Inferable_Discriminants will determine whether
9017 -- the selector has a constrained Unchecked_Union nominal type.
9019 return Has_Inferable_Discriminants (Selector_Name (N));
9021 -- A qualified expression has inferable discriminants if its subtype
9022 -- mark is a constrained Unchecked_Union subtype.
9024 elsif Nkind (N) = N_Qualified_Expression then
9025 return Is_Unchecked_Union (Subtype_Mark (N))
9027 Is_Constrained (Subtype_Mark (N));
9032 end Has_Inferable_Discriminants;
9034 -------------------------------
9035 -- Insert_Dereference_Action --
9036 -------------------------------
9038 procedure Insert_Dereference_Action (N : Node_Id) is
9039 Loc : constant Source_Ptr := Sloc (N);
9040 Typ : constant Entity_Id := Etype (N);
9041 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
9042 Pnod : constant Node_Id := Parent (N);
9044 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
9045 -- Return true if type of P is derived from Checked_Pool;
9047 -----------------------------
9048 -- Is_Checked_Storage_Pool --
9049 -----------------------------
9051 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
9060 while T /= Etype (T) loop
9061 if Is_RTE (T, RE_Checked_Pool) then
9069 end Is_Checked_Storage_Pool;
9071 -- Start of processing for Insert_Dereference_Action
9074 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
9076 if not (Is_Checked_Storage_Pool (Pool)
9077 and then Comes_From_Source (Original_Node (Pnod)))
9083 Make_Procedure_Call_Statement (Loc,
9084 Name => New_Reference_To (
9085 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
9087 Parameter_Associations => New_List (
9091 New_Reference_To (Pool, Loc),
9093 -- Storage_Address. We use the attribute Pool_Address, which uses
9094 -- the pointer itself to find the address of the object, and which
9095 -- handles unconstrained arrays properly by computing the address
9096 -- of the template. i.e. the correct address of the corresponding
9099 Make_Attribute_Reference (Loc,
9100 Prefix => Duplicate_Subexpr_Move_Checks (N),
9101 Attribute_Name => Name_Pool_Address),
9103 -- Size_In_Storage_Elements
9105 Make_Op_Divide (Loc,
9107 Make_Attribute_Reference (Loc,
9109 Make_Explicit_Dereference (Loc,
9110 Duplicate_Subexpr_Move_Checks (N)),
9111 Attribute_Name => Name_Size),
9113 Make_Integer_Literal (Loc, System_Storage_Unit)),
9117 Make_Attribute_Reference (Loc,
9119 Make_Explicit_Dereference (Loc,
9120 Duplicate_Subexpr_Move_Checks (N)),
9121 Attribute_Name => Name_Alignment))));
9124 when RE_Not_Available =>
9126 end Insert_Dereference_Action;
9128 ------------------------------
9129 -- Make_Array_Comparison_Op --
9130 ------------------------------
9132 -- This is a hand-coded expansion of the following generic function:
9135 -- type elem is (<>);
9136 -- type index is (<>);
9137 -- type a is array (index range <>) of elem;
9139 -- function Gnnn (X : a; Y: a) return boolean is
9140 -- J : index := Y'first;
9143 -- if X'length = 0 then
9146 -- elsif Y'length = 0 then
9150 -- for I in X'range loop
9151 -- if X (I) = Y (J) then
9152 -- if J = Y'last then
9155 -- J := index'succ (J);
9159 -- return X (I) > Y (J);
9163 -- return X'length > Y'length;
9167 -- Note that since we are essentially doing this expansion by hand, we
9168 -- do not need to generate an actual or formal generic part, just the
9169 -- instantiated function itself.
9171 function Make_Array_Comparison_Op
9173 Nod : Node_Id) return Node_Id
9175 Loc : constant Source_Ptr := Sloc (Nod);
9177 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
9178 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
9179 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
9180 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
9182 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
9184 Loop_Statement : Node_Id;
9185 Loop_Body : Node_Id;
9188 Final_Expr : Node_Id;
9189 Func_Body : Node_Id;
9190 Func_Name : Entity_Id;
9196 -- if J = Y'last then
9199 -- J := index'succ (J);
9203 Make_Implicit_If_Statement (Nod,
9206 Left_Opnd => New_Reference_To (J, Loc),
9208 Make_Attribute_Reference (Loc,
9209 Prefix => New_Reference_To (Y, Loc),
9210 Attribute_Name => Name_Last)),
9212 Then_Statements => New_List (
9213 Make_Exit_Statement (Loc)),
9217 Make_Assignment_Statement (Loc,
9218 Name => New_Reference_To (J, Loc),
9220 Make_Attribute_Reference (Loc,
9221 Prefix => New_Reference_To (Index, Loc),
9222 Attribute_Name => Name_Succ,
9223 Expressions => New_List (New_Reference_To (J, Loc))))));
9225 -- if X (I) = Y (J) then
9228 -- return X (I) > Y (J);
9232 Make_Implicit_If_Statement (Nod,
9236 Make_Indexed_Component (Loc,
9237 Prefix => New_Reference_To (X, Loc),
9238 Expressions => New_List (New_Reference_To (I, Loc))),
9241 Make_Indexed_Component (Loc,
9242 Prefix => New_Reference_To (Y, Loc),
9243 Expressions => New_List (New_Reference_To (J, Loc)))),
9245 Then_Statements => New_List (Inner_If),
9247 Else_Statements => New_List (
9248 Make_Simple_Return_Statement (Loc,
9252 Make_Indexed_Component (Loc,
9253 Prefix => New_Reference_To (X, Loc),
9254 Expressions => New_List (New_Reference_To (I, Loc))),
9257 Make_Indexed_Component (Loc,
9258 Prefix => New_Reference_To (Y, Loc),
9259 Expressions => New_List (
9260 New_Reference_To (J, Loc)))))));
9262 -- for I in X'range loop
9267 Make_Implicit_Loop_Statement (Nod,
9268 Identifier => Empty,
9271 Make_Iteration_Scheme (Loc,
9272 Loop_Parameter_Specification =>
9273 Make_Loop_Parameter_Specification (Loc,
9274 Defining_Identifier => I,
9275 Discrete_Subtype_Definition =>
9276 Make_Attribute_Reference (Loc,
9277 Prefix => New_Reference_To (X, Loc),
9278 Attribute_Name => Name_Range))),
9280 Statements => New_List (Loop_Body));
9282 -- if X'length = 0 then
9284 -- elsif Y'length = 0 then
9287 -- for ... loop ... end loop;
9288 -- return X'length > Y'length;
9292 Make_Attribute_Reference (Loc,
9293 Prefix => New_Reference_To (X, Loc),
9294 Attribute_Name => Name_Length);
9297 Make_Attribute_Reference (Loc,
9298 Prefix => New_Reference_To (Y, Loc),
9299 Attribute_Name => Name_Length);
9303 Left_Opnd => Length1,
9304 Right_Opnd => Length2);
9307 Make_Implicit_If_Statement (Nod,
9311 Make_Attribute_Reference (Loc,
9312 Prefix => New_Reference_To (X, Loc),
9313 Attribute_Name => Name_Length),
9315 Make_Integer_Literal (Loc, 0)),
9319 Make_Simple_Return_Statement (Loc,
9320 Expression => New_Reference_To (Standard_False, Loc))),
9322 Elsif_Parts => New_List (
9323 Make_Elsif_Part (Loc,
9327 Make_Attribute_Reference (Loc,
9328 Prefix => New_Reference_To (Y, Loc),
9329 Attribute_Name => Name_Length),
9331 Make_Integer_Literal (Loc, 0)),
9335 Make_Simple_Return_Statement (Loc,
9336 Expression => New_Reference_To (Standard_True, Loc))))),
9338 Else_Statements => New_List (
9340 Make_Simple_Return_Statement (Loc,
9341 Expression => Final_Expr)));
9345 Formals := New_List (
9346 Make_Parameter_Specification (Loc,
9347 Defining_Identifier => X,
9348 Parameter_Type => New_Reference_To (Typ, Loc)),
9350 Make_Parameter_Specification (Loc,
9351 Defining_Identifier => Y,
9352 Parameter_Type => New_Reference_To (Typ, Loc)));
9354 -- function Gnnn (...) return boolean is
9355 -- J : index := Y'first;
9360 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('G'));
9363 Make_Subprogram_Body (Loc,
9365 Make_Function_Specification (Loc,
9366 Defining_Unit_Name => Func_Name,
9367 Parameter_Specifications => Formals,
9368 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
9370 Declarations => New_List (
9371 Make_Object_Declaration (Loc,
9372 Defining_Identifier => J,
9373 Object_Definition => New_Reference_To (Index, Loc),
9375 Make_Attribute_Reference (Loc,
9376 Prefix => New_Reference_To (Y, Loc),
9377 Attribute_Name => Name_First))),
9379 Handled_Statement_Sequence =>
9380 Make_Handled_Sequence_Of_Statements (Loc,
9381 Statements => New_List (If_Stat)));
9384 end Make_Array_Comparison_Op;
9386 ---------------------------
9387 -- Make_Boolean_Array_Op --
9388 ---------------------------
9390 -- For logical operations on boolean arrays, expand in line the following,
9391 -- replacing 'and' with 'or' or 'xor' where needed:
9393 -- function Annn (A : typ; B: typ) return typ is
9396 -- for J in A'range loop
9397 -- C (J) := A (J) op B (J);
9402 -- Here typ is the boolean array type
9404 function Make_Boolean_Array_Op
9406 N : Node_Id) return Node_Id
9408 Loc : constant Source_Ptr := Sloc (N);
9410 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
9411 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
9412 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
9413 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
9421 Func_Name : Entity_Id;
9422 Func_Body : Node_Id;
9423 Loop_Statement : Node_Id;
9427 Make_Indexed_Component (Loc,
9428 Prefix => New_Reference_To (A, Loc),
9429 Expressions => New_List (New_Reference_To (J, Loc)));
9432 Make_Indexed_Component (Loc,
9433 Prefix => New_Reference_To (B, Loc),
9434 Expressions => New_List (New_Reference_To (J, Loc)));
9437 Make_Indexed_Component (Loc,
9438 Prefix => New_Reference_To (C, Loc),
9439 Expressions => New_List (New_Reference_To (J, Loc)));
9441 if Nkind (N) = N_Op_And then
9447 elsif Nkind (N) = N_Op_Or then
9461 Make_Implicit_Loop_Statement (N,
9462 Identifier => Empty,
9465 Make_Iteration_Scheme (Loc,
9466 Loop_Parameter_Specification =>
9467 Make_Loop_Parameter_Specification (Loc,
9468 Defining_Identifier => J,
9469 Discrete_Subtype_Definition =>
9470 Make_Attribute_Reference (Loc,
9471 Prefix => New_Reference_To (A, Loc),
9472 Attribute_Name => Name_Range))),
9474 Statements => New_List (
9475 Make_Assignment_Statement (Loc,
9477 Expression => Op)));
9479 Formals := New_List (
9480 Make_Parameter_Specification (Loc,
9481 Defining_Identifier => A,
9482 Parameter_Type => New_Reference_To (Typ, Loc)),
9484 Make_Parameter_Specification (Loc,
9485 Defining_Identifier => B,
9486 Parameter_Type => New_Reference_To (Typ, Loc)));
9489 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
9490 Set_Is_Inlined (Func_Name);
9493 Make_Subprogram_Body (Loc,
9495 Make_Function_Specification (Loc,
9496 Defining_Unit_Name => Func_Name,
9497 Parameter_Specifications => Formals,
9498 Result_Definition => New_Reference_To (Typ, Loc)),
9500 Declarations => New_List (
9501 Make_Object_Declaration (Loc,
9502 Defining_Identifier => C,
9503 Object_Definition => New_Reference_To (Typ, Loc))),
9505 Handled_Statement_Sequence =>
9506 Make_Handled_Sequence_Of_Statements (Loc,
9507 Statements => New_List (
9509 Make_Simple_Return_Statement (Loc,
9510 Expression => New_Reference_To (C, Loc)))));
9513 end Make_Boolean_Array_Op;
9515 ------------------------
9516 -- Rewrite_Comparison --
9517 ------------------------
9519 procedure Rewrite_Comparison (N : Node_Id) is
9520 Warning_Generated : Boolean := False;
9521 -- Set to True if first pass with Assume_Valid generates a warning in
9522 -- which case we skip the second pass to avoid warning overloaded.
9525 -- Set to Standard_True or Standard_False
9528 if Nkind (N) = N_Type_Conversion then
9529 Rewrite_Comparison (Expression (N));
9532 elsif Nkind (N) not in N_Op_Compare then
9536 -- Now start looking at the comparison in detail. We potentially go
9537 -- through this loop twice. The first time, Assume_Valid is set False
9538 -- in the call to Compile_Time_Compare. If this call results in a
9539 -- clear result of always True or Always False, that's decisive and
9540 -- we are done. Otherwise we repeat the processing with Assume_Valid
9541 -- set to True to generate additional warnings. We can stil that step
9542 -- if Constant_Condition_Warnings is False.
9544 for AV in False .. True loop
9546 Typ : constant Entity_Id := Etype (N);
9547 Op1 : constant Node_Id := Left_Opnd (N);
9548 Op2 : constant Node_Id := Right_Opnd (N);
9550 Res : constant Compare_Result :=
9551 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
9552 -- Res indicates if compare outcome can be compile time determined
9554 True_Result : Boolean;
9555 False_Result : Boolean;
9558 case N_Op_Compare (Nkind (N)) is
9560 True_Result := Res = EQ;
9561 False_Result := Res = LT or else Res = GT or else Res = NE;
9564 True_Result := Res in Compare_GE;
9565 False_Result := Res = LT;
9568 and then Constant_Condition_Warnings
9569 and then Comes_From_Source (Original_Node (N))
9570 and then Nkind (Original_Node (N)) = N_Op_Ge
9571 and then not In_Instance
9572 and then Is_Integer_Type (Etype (Left_Opnd (N)))
9573 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
9576 ("can never be greater than, could replace by ""'=""?", N);
9577 Warning_Generated := True;
9581 True_Result := Res = GT;
9582 False_Result := Res in Compare_LE;
9585 True_Result := Res = LT;
9586 False_Result := Res in Compare_GE;
9589 True_Result := Res in Compare_LE;
9590 False_Result := Res = GT;
9593 and then Constant_Condition_Warnings
9594 and then Comes_From_Source (Original_Node (N))
9595 and then Nkind (Original_Node (N)) = N_Op_Le
9596 and then not In_Instance
9597 and then Is_Integer_Type (Etype (Left_Opnd (N)))
9598 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
9601 ("can never be less than, could replace by ""'=""?", N);
9602 Warning_Generated := True;
9606 True_Result := Res = NE or else Res = GT or else Res = LT;
9607 False_Result := Res = EQ;
9610 -- If this is the first iteration, then we actually convert the
9611 -- comparison into True or False, if the result is certain.
9614 if True_Result or False_Result then
9616 Result := Standard_True;
9618 Result := Standard_False;
9623 New_Occurrence_Of (Result, Sloc (N))));
9624 Analyze_And_Resolve (N, Typ);
9625 Warn_On_Known_Condition (N);
9629 -- If this is the second iteration (AV = True), and the original
9630 -- node comes from source and we are not in an instance, then
9631 -- give a warning if we know result would be True or False. Note
9632 -- we know Constant_Condition_Warnings is set if we get here.
9634 elsif Comes_From_Source (Original_Node (N))
9635 and then not In_Instance
9639 ("condition can only be False if invalid values present?",
9641 elsif False_Result then
9643 ("condition can only be True if invalid values present?",
9649 -- Skip second iteration if not warning on constant conditions or
9650 -- if the first iteration already generated a warning of some kind
9651 -- or if we are in any case assuming all values are valid (so that
9652 -- the first iteration took care of the valid case).
9654 exit when not Constant_Condition_Warnings;
9655 exit when Warning_Generated;
9656 exit when Assume_No_Invalid_Values;
9658 end Rewrite_Comparison;
9660 ----------------------------
9661 -- Safe_In_Place_Array_Op --
9662 ----------------------------
9664 function Safe_In_Place_Array_Op
9667 Op2 : Node_Id) return Boolean
9671 function Is_Safe_Operand (Op : Node_Id) return Boolean;
9672 -- Operand is safe if it cannot overlap part of the target of the
9673 -- operation. If the operand and the target are identical, the operand
9674 -- is safe. The operand can be empty in the case of negation.
9676 function Is_Unaliased (N : Node_Id) return Boolean;
9677 -- Check that N is a stand-alone entity
9683 function Is_Unaliased (N : Node_Id) return Boolean is
9687 and then No (Address_Clause (Entity (N)))
9688 and then No (Renamed_Object (Entity (N)));
9691 ---------------------
9692 -- Is_Safe_Operand --
9693 ---------------------
9695 function Is_Safe_Operand (Op : Node_Id) return Boolean is
9700 elsif Is_Entity_Name (Op) then
9701 return Is_Unaliased (Op);
9703 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
9704 return Is_Unaliased (Prefix (Op));
9706 elsif Nkind (Op) = N_Slice then
9708 Is_Unaliased (Prefix (Op))
9709 and then Entity (Prefix (Op)) /= Target;
9711 elsif Nkind (Op) = N_Op_Not then
9712 return Is_Safe_Operand (Right_Opnd (Op));
9717 end Is_Safe_Operand;
9719 -- Start of processing for Is_Safe_In_Place_Array_Op
9722 -- Skip this processing if the component size is different from system
9723 -- storage unit (since at least for NOT this would cause problems).
9725 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
9728 -- Cannot do in place stuff on VM_Target since cannot pass addresses
9730 elsif VM_Target /= No_VM then
9733 -- Cannot do in place stuff if non-standard Boolean representation
9735 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
9738 elsif not Is_Unaliased (Lhs) then
9741 Target := Entity (Lhs);
9744 Is_Safe_Operand (Op1)
9745 and then Is_Safe_Operand (Op2);
9747 end Safe_In_Place_Array_Op;
9749 -----------------------
9750 -- Tagged_Membership --
9751 -----------------------
9753 -- There are two different cases to consider depending on whether the right
9754 -- operand is a class-wide type or not. If not we just compare the actual
9755 -- tag of the left expr to the target type tag:
9757 -- Left_Expr.Tag = Right_Type'Tag;
9759 -- If it is a class-wide type we use the RT function CW_Membership which is
9760 -- usually implemented by looking in the ancestor tables contained in the
9761 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
9763 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
9764 -- function IW_Membership which is usually implemented by looking in the
9765 -- table of abstract interface types plus the ancestor table contained in
9766 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
9768 function Tagged_Membership (N : Node_Id) return Node_Id is
9769 Left : constant Node_Id := Left_Opnd (N);
9770 Right : constant Node_Id := Right_Opnd (N);
9771 Loc : constant Source_Ptr := Sloc (N);
9773 Left_Type : Entity_Id;
9774 Right_Type : Entity_Id;
9778 -- Handle entities from the limited view
9780 Left_Type := Available_View (Etype (Left));
9781 Right_Type := Available_View (Etype (Right));
9783 if Is_Class_Wide_Type (Left_Type) then
9784 Left_Type := Root_Type (Left_Type);
9788 Make_Selected_Component (Loc,
9789 Prefix => Relocate_Node (Left),
9791 New_Reference_To (First_Tag_Component (Left_Type), Loc));
9793 if Is_Class_Wide_Type (Right_Type) then
9795 -- No need to issue a run-time check if we statically know that the
9796 -- result of this membership test is always true. For example,
9797 -- considering the following declarations:
9799 -- type Iface is interface;
9800 -- type T is tagged null record;
9801 -- type DT is new T and Iface with null record;
9806 -- These membership tests are always true:
9810 -- Obj2 in Iface'Class;
9812 -- We do not need to handle cases where the membership is illegal.
9815 -- Obj1 in DT'Class; -- Compile time error
9816 -- Obj1 in Iface'Class; -- Compile time error
9818 if not Is_Class_Wide_Type (Left_Type)
9819 and then (Is_Ancestor (Etype (Right_Type), Left_Type)
9820 or else (Is_Interface (Etype (Right_Type))
9821 and then Interface_Present_In_Ancestor
9823 Iface => Etype (Right_Type))))
9825 return New_Reference_To (Standard_True, Loc);
9828 -- Ada 2005 (AI-251): Class-wide applied to interfaces
9830 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
9832 -- Support to: "Iface_CW_Typ in Typ'Class"
9834 or else Is_Interface (Left_Type)
9836 -- Issue error if IW_Membership operation not available in a
9837 -- configurable run time setting.
9839 if not RTE_Available (RE_IW_Membership) then
9841 ("dynamic membership test on interface types", N);
9846 Make_Function_Call (Loc,
9847 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
9848 Parameter_Associations => New_List (
9849 Make_Attribute_Reference (Loc,
9851 Attribute_Name => Name_Address),
9854 (Access_Disp_Table (Root_Type (Right_Type)))),
9857 -- Ada 95: Normal case
9861 Build_CW_Membership (Loc,
9862 Obj_Tag_Node => Obj_Tag,
9866 (Access_Disp_Table (Root_Type (Right_Type)))),
9870 -- Right_Type is not a class-wide type
9873 -- No need to check the tag of the object if Right_Typ is abstract
9875 if Is_Abstract_Type (Right_Type) then
9876 return New_Reference_To (Standard_False, Loc);
9881 Left_Opnd => Obj_Tag,
9884 (Node (First_Elmt (Access_Disp_Table (Right_Type))), Loc));
9887 end Tagged_Membership;
9889 ------------------------------
9890 -- Unary_Op_Validity_Checks --
9891 ------------------------------
9893 procedure Unary_Op_Validity_Checks (N : Node_Id) is
9895 if Validity_Checks_On and Validity_Check_Operands then
9896 Ensure_Valid (Right_Opnd (N));
9898 end Unary_Op_Validity_Checks;