1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, 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 Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Aggr; use Exp_Aggr;
32 with Exp_Atag; use Exp_Atag;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Fixd; use Exp_Fixd;
39 with Exp_Pakd; use Exp_Pakd;
40 with Exp_Tss; use Exp_Tss;
41 with Exp_Util; use Exp_Util;
42 with Exp_VFpt; use Exp_VFpt;
43 with Freeze; use Freeze;
44 with Inline; use Inline;
45 with Namet; use Namet;
46 with Nlists; use Nlists;
47 with Nmake; use Nmake;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Type; use Sem_Type;
60 with Sem_Util; use Sem_Util;
61 with Sem_Warn; use Sem_Warn;
62 with Sinfo; use Sinfo;
63 with Snames; use Snames;
64 with Stand; use Stand;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Uintp; use Uintp;
69 with Urealp; use Urealp;
70 with Validsw; use Validsw;
72 package body Exp_Ch4 is
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
78 procedure Binary_Op_Validity_Checks (N : Node_Id);
79 pragma Inline (Binary_Op_Validity_Checks);
80 -- Performs validity checks for a binary operator
82 procedure Build_Boolean_Array_Proc_Call
86 -- If a boolean array assignment can be done in place, build call to
87 -- corresponding library procedure.
89 procedure Displace_Allocator_Pointer (N : Node_Id);
90 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
91 -- Expand_Allocator_Expression. Allocating class-wide interface objects
92 -- this routine displaces the pointer to the allocated object to reference
93 -- the component referencing the corresponding secondary dispatch table.
95 procedure Expand_Allocator_Expression (N : Node_Id);
96 -- Subsidiary to Expand_N_Allocator, for the case when the expression
97 -- is a qualified expression or an aggregate.
99 procedure Expand_Array_Comparison (N : Node_Id);
100 -- This routine handles expansion of the comparison operators (N_Op_Lt,
101 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
102 -- code for these operators is similar, differing only in the details of
103 -- the actual comparison call that is made. Special processing (call a
106 function Expand_Array_Equality
111 Typ : Entity_Id) return Node_Id;
112 -- Expand an array equality into a call to a function implementing this
113 -- equality, and a call to it. Loc is the location for the generated nodes.
114 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
115 -- on which to attach bodies of local functions that are created in the
116 -- process. It is the responsibility of the caller to insert those bodies
117 -- at the right place. Nod provides the Sloc value for the generated code.
118 -- Normally the types used for the generated equality routine are taken
119 -- from Lhs and Rhs. However, in some situations of generated code, the
120 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
121 -- the type to be used for the formal parameters.
123 procedure Expand_Boolean_Operator (N : Node_Id);
124 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
125 -- case of array type arguments.
127 function Expand_Composite_Equality
132 Bodies : List_Id) return Node_Id;
133 -- Local recursive function used to expand equality for nested composite
134 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
135 -- to attach bodies of local functions that are created in the process.
136 -- This is the responsibility of the caller to insert those bodies at the
137 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
138 -- are the left and right sides for the comparison, and Typ is the type of
139 -- the arrays to compare.
141 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
142 -- Routine to expand concatenation of a sequence of two or more operands
143 -- (in the list Operands) and replace node Cnode with the result of the
144 -- concatenation. The operands can be of any appropriate type, and can
145 -- include both arrays and singleton elements.
147 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
148 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
149 -- fixed. We do not have such a type at runtime, so the purpose of this
150 -- routine is to find the real type by looking up the tree. We also
151 -- determine if the operation must be rounded.
153 function Get_Allocator_Final_List
156 PtrT : Entity_Id) return Entity_Id;
157 -- If the designated type is controlled, build final_list expression for
158 -- created object. If context is an access parameter, create a local access
159 -- type to have a usable finalization list.
161 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
162 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
163 -- discriminants if it has a constrained nominal type, unless the object
164 -- is a component of an enclosing Unchecked_Union object that is subject
165 -- to a per-object constraint and the enclosing object lacks inferable
168 -- An expression of an Unchecked_Union type has inferable discriminants
169 -- if it is either a name of an object with inferable discriminants or a
170 -- qualified expression whose subtype mark denotes a constrained subtype.
172 procedure Insert_Dereference_Action (N : Node_Id);
173 -- N is an expression whose type is an access. When the type of the
174 -- associated storage pool is derived from Checked_Pool, generate a
175 -- call to the 'Dereference' primitive operation.
177 function Make_Array_Comparison_Op
179 Nod : Node_Id) return Node_Id;
180 -- Comparisons between arrays are expanded in line. This function produces
181 -- the body of the implementation of (a > b), where a and b are one-
182 -- dimensional arrays of some discrete type. The original node is then
183 -- expanded into the appropriate call to this function. Nod provides the
184 -- Sloc value for the generated code.
186 function Make_Boolean_Array_Op
188 N : Node_Id) return Node_Id;
189 -- Boolean operations on boolean arrays are expanded in line. This function
190 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
191 -- b). It is used only the normal case and not the packed case. The type
192 -- involved, Typ, is the Boolean array type, and the logical operations in
193 -- the body are simple boolean operations. Note that Typ is always a
194 -- constrained type (the caller has ensured this by using
195 -- Convert_To_Actual_Subtype if necessary).
197 procedure Rewrite_Comparison (N : Node_Id);
198 -- If N is the node for a comparison whose outcome can be determined at
199 -- compile time, then the node N can be rewritten with True or False. If
200 -- the outcome cannot be determined at compile time, the call has no
201 -- effect. If N is a type conversion, then this processing is applied to
202 -- its expression. If N is neither comparison nor a type conversion, the
203 -- call has no effect.
205 function Tagged_Membership (N : Node_Id) return Node_Id;
206 -- Construct the expression corresponding to the tagged membership test.
207 -- Deals with a second operand being (or not) a class-wide type.
209 function Safe_In_Place_Array_Op
212 Op2 : Node_Id) return Boolean;
213 -- In the context of an assignment, where the right-hand side is a boolean
214 -- operation on arrays, check whether operation can be performed in place.
216 procedure Unary_Op_Validity_Checks (N : Node_Id);
217 pragma Inline (Unary_Op_Validity_Checks);
218 -- Performs validity checks for a unary operator
220 -------------------------------
221 -- Binary_Op_Validity_Checks --
222 -------------------------------
224 procedure Binary_Op_Validity_Checks (N : Node_Id) is
226 if Validity_Checks_On and Validity_Check_Operands then
227 Ensure_Valid (Left_Opnd (N));
228 Ensure_Valid (Right_Opnd (N));
230 end Binary_Op_Validity_Checks;
232 ------------------------------------
233 -- Build_Boolean_Array_Proc_Call --
234 ------------------------------------
236 procedure Build_Boolean_Array_Proc_Call
241 Loc : constant Source_Ptr := Sloc (N);
242 Kind : constant Node_Kind := Nkind (Expression (N));
243 Target : constant Node_Id :=
244 Make_Attribute_Reference (Loc,
246 Attribute_Name => Name_Address);
248 Arg1 : constant Node_Id := Op1;
249 Arg2 : Node_Id := Op2;
251 Proc_Name : Entity_Id;
254 if Kind = N_Op_Not then
255 if Nkind (Op1) in N_Binary_Op then
257 -- Use negated version of the binary operators
259 if Nkind (Op1) = N_Op_And then
260 Proc_Name := RTE (RE_Vector_Nand);
262 elsif Nkind (Op1) = N_Op_Or then
263 Proc_Name := RTE (RE_Vector_Nor);
265 else pragma Assert (Nkind (Op1) = N_Op_Xor);
266 Proc_Name := RTE (RE_Vector_Xor);
270 Make_Procedure_Call_Statement (Loc,
271 Name => New_Occurrence_Of (Proc_Name, Loc),
273 Parameter_Associations => New_List (
275 Make_Attribute_Reference (Loc,
276 Prefix => Left_Opnd (Op1),
277 Attribute_Name => Name_Address),
279 Make_Attribute_Reference (Loc,
280 Prefix => Right_Opnd (Op1),
281 Attribute_Name => Name_Address),
283 Make_Attribute_Reference (Loc,
284 Prefix => Left_Opnd (Op1),
285 Attribute_Name => Name_Length)));
288 Proc_Name := RTE (RE_Vector_Not);
291 Make_Procedure_Call_Statement (Loc,
292 Name => New_Occurrence_Of (Proc_Name, Loc),
293 Parameter_Associations => New_List (
296 Make_Attribute_Reference (Loc,
298 Attribute_Name => Name_Address),
300 Make_Attribute_Reference (Loc,
302 Attribute_Name => Name_Length)));
306 -- We use the following equivalences:
308 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
309 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
310 -- (not X) xor (not Y) = X xor Y
311 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
313 if Nkind (Op1) = N_Op_Not then
314 if Kind = N_Op_And then
315 Proc_Name := RTE (RE_Vector_Nor);
317 elsif Kind = N_Op_Or then
318 Proc_Name := RTE (RE_Vector_Nand);
321 Proc_Name := RTE (RE_Vector_Xor);
325 if Kind = N_Op_And then
326 Proc_Name := RTE (RE_Vector_And);
328 elsif Kind = N_Op_Or then
329 Proc_Name := RTE (RE_Vector_Or);
331 elsif Nkind (Op2) = N_Op_Not then
332 Proc_Name := RTE (RE_Vector_Nxor);
333 Arg2 := Right_Opnd (Op2);
336 Proc_Name := RTE (RE_Vector_Xor);
341 Make_Procedure_Call_Statement (Loc,
342 Name => New_Occurrence_Of (Proc_Name, Loc),
343 Parameter_Associations => New_List (
345 Make_Attribute_Reference (Loc,
347 Attribute_Name => Name_Address),
348 Make_Attribute_Reference (Loc,
350 Attribute_Name => Name_Address),
351 Make_Attribute_Reference (Loc,
353 Attribute_Name => Name_Length)));
356 Rewrite (N, Call_Node);
360 when RE_Not_Available =>
362 end Build_Boolean_Array_Proc_Call;
364 --------------------------------
365 -- Displace_Allocator_Pointer --
366 --------------------------------
368 procedure Displace_Allocator_Pointer (N : Node_Id) is
369 Loc : constant Source_Ptr := Sloc (N);
370 Orig_Node : constant Node_Id := Original_Node (N);
376 -- Do nothing in case of VM targets: the virtual machine will handle
377 -- interfaces directly.
379 if VM_Target /= No_VM then
383 pragma Assert (Nkind (N) = N_Identifier
384 and then Nkind (Orig_Node) = N_Allocator);
386 PtrT := Etype (Orig_Node);
387 Dtyp := Designated_Type (PtrT);
388 Etyp := Etype (Expression (Orig_Node));
390 if Is_Class_Wide_Type (Dtyp)
391 and then Is_Interface (Dtyp)
393 -- If the type of the allocator expression is not an interface type
394 -- we can generate code to reference the record component containing
395 -- the pointer to the secondary dispatch table.
397 if not Is_Interface (Etyp) then
399 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
402 -- 1) Get access to the allocated object
405 Make_Explicit_Dereference (Loc,
410 -- 2) Add the conversion to displace the pointer to reference
411 -- the secondary dispatch table.
413 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
414 Analyze_And_Resolve (N, Dtyp);
416 -- 3) The 'access to the secondary dispatch table will be used
417 -- as the value returned by the allocator.
420 Make_Attribute_Reference (Loc,
421 Prefix => Relocate_Node (N),
422 Attribute_Name => Name_Access));
423 Set_Etype (N, Saved_Typ);
427 -- If the type of the allocator expression is an interface type we
428 -- generate a run-time call to displace "this" to reference the
429 -- component containing the pointer to the secondary dispatch table
430 -- or else raise Constraint_Error if the actual object does not
431 -- implement the target interface. This case corresponds with the
432 -- following example:
434 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
436 -- return new Iface_2'Class'(Obj);
441 Unchecked_Convert_To (PtrT,
442 Make_Function_Call (Loc,
443 Name => New_Reference_To (RTE (RE_Displace), Loc),
444 Parameter_Associations => New_List (
445 Unchecked_Convert_To (RTE (RE_Address),
451 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
453 Analyze_And_Resolve (N, PtrT);
456 end Displace_Allocator_Pointer;
458 ---------------------------------
459 -- Expand_Allocator_Expression --
460 ---------------------------------
462 procedure Expand_Allocator_Expression (N : Node_Id) is
463 Loc : constant Source_Ptr := Sloc (N);
464 Exp : constant Node_Id := Expression (Expression (N));
465 PtrT : constant Entity_Id := Etype (N);
466 DesigT : constant Entity_Id := Designated_Type (PtrT);
468 procedure Apply_Accessibility_Check
470 Built_In_Place : Boolean := False);
471 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
472 -- type, generate an accessibility check to verify that the level of the
473 -- type of the created object is not deeper than the level of the access
474 -- type. If the type of the qualified expression is class- wide, then
475 -- always generate the check (except in the case where it is known to be
476 -- unnecessary, see comment below). Otherwise, only generate the check
477 -- if the level of the qualified expression type is statically deeper
478 -- than the access type.
480 -- Although the static accessibility will generally have been performed
481 -- as a legality check, it won't have been done in cases where the
482 -- allocator appears in generic body, so a run-time check is needed in
483 -- general. One special case is when the access type is declared in the
484 -- same scope as the class-wide allocator, in which case the check can
485 -- never fail, so it need not be generated.
487 -- As an open issue, there seem to be cases where the static level
488 -- associated with the class-wide object's underlying type is not
489 -- sufficient to perform the proper accessibility check, such as for
490 -- allocators in nested subprograms or accept statements initialized by
491 -- class-wide formals when the actual originates outside at a deeper
492 -- static level. The nested subprogram case might require passing
493 -- accessibility levels along with class-wide parameters, and the task
494 -- case seems to be an actual gap in the language rules that needs to
495 -- be fixed by the ARG. ???
497 -------------------------------
498 -- Apply_Accessibility_Check --
499 -------------------------------
501 procedure Apply_Accessibility_Check
503 Built_In_Place : Boolean := False)
508 -- Note: we skip the accessibility check for the VM case, since
509 -- there does not seem to be any practical way of implementing it.
511 if Ada_Version >= Ada_05
512 and then VM_Target = No_VM
513 and then Is_Class_Wide_Type (DesigT)
514 and then not Scope_Suppress (Accessibility_Check)
516 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
518 (Is_Class_Wide_Type (Etype (Exp))
519 and then Scope (PtrT) /= Current_Scope))
521 -- If the allocator was built in place Ref is already a reference
522 -- to the access object initialized to the result of the allocator
523 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise
524 -- it is the entity associated with the object containing the
525 -- address of the allocated object.
527 if Built_In_Place then
528 Ref_Node := New_Copy (Ref);
530 Ref_Node := New_Reference_To (Ref, Loc);
534 Make_Raise_Program_Error (Loc,
538 Build_Get_Access_Level (Loc,
539 Make_Attribute_Reference (Loc,
541 Attribute_Name => Name_Tag)),
543 Make_Integer_Literal (Loc,
544 Type_Access_Level (PtrT))),
545 Reason => PE_Accessibility_Check_Failed));
547 end Apply_Accessibility_Check;
551 Indic : constant Node_Id := Subtype_Mark (Expression (N));
552 T : constant Entity_Id := Entity (Indic);
557 TagT : Entity_Id := Empty;
558 -- Type used as source for tag assignment
560 TagR : Node_Id := Empty;
561 -- Target reference for tag assignment
563 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
565 Tag_Assign : Node_Id;
568 -- Start of processing for Expand_Allocator_Expression
571 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
573 -- Ada 2005 (AI-318-02): If the initialization expression is a call
574 -- to a build-in-place function, then access to the allocated object
575 -- must be passed to the function. Currently we limit such functions
576 -- to those with constrained limited result subtypes, but eventually
577 -- we plan to expand the allowed forms of functions that are treated
578 -- as build-in-place.
580 if Ada_Version >= Ada_05
581 and then Is_Build_In_Place_Function_Call (Exp)
583 Make_Build_In_Place_Call_In_Allocator (N, Exp);
584 Apply_Accessibility_Check (N, Built_In_Place => True);
588 -- Actions inserted before:
589 -- Temp : constant ptr_T := new T'(Expression);
590 -- <no CW> Temp._tag := T'tag;
591 -- <CTRL> Adjust (Finalizable (Temp.all));
592 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
594 -- We analyze by hand the new internal allocator to avoid
595 -- any recursion and inappropriate call to Initialize
597 -- We don't want to remove side effects when the expression must be
598 -- built in place. In the case of a build-in-place function call,
599 -- that could lead to a duplication of the call, which was already
600 -- substituted for the allocator.
602 if not Aggr_In_Place then
603 Remove_Side_Effects (Exp);
607 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
609 -- For a class wide allocation generate the following code:
611 -- type Equiv_Record is record ... end record;
612 -- implicit subtype CW is <Class_Wide_Subytpe>;
613 -- temp : PtrT := new CW'(CW!(expr));
615 if Is_Class_Wide_Type (T) then
616 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
618 -- Ada 2005 (AI-251): If the expression is a class-wide interface
619 -- object we generate code to move up "this" to reference the
620 -- base of the object before allocating the new object.
622 -- Note that Exp'Address is recursively expanded into a call
623 -- to Base_Address (Exp.Tag)
625 if Is_Class_Wide_Type (Etype (Exp))
626 and then Is_Interface (Etype (Exp))
627 and then VM_Target = No_VM
631 Unchecked_Convert_To (Entity (Indic),
632 Make_Explicit_Dereference (Loc,
633 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
634 Make_Attribute_Reference (Loc,
636 Attribute_Name => Name_Address)))));
641 Unchecked_Convert_To (Entity (Indic), Exp));
644 Analyze_And_Resolve (Expression (N), Entity (Indic));
647 -- Keep separate the management of allocators returning interfaces
649 if not Is_Interface (Directly_Designated_Type (PtrT)) then
650 if Aggr_In_Place then
652 Make_Object_Declaration (Loc,
653 Defining_Identifier => Temp,
654 Object_Definition => New_Reference_To (PtrT, Loc),
657 New_Reference_To (Etype (Exp), Loc)));
659 Set_Comes_From_Source
660 (Expression (Tmp_Node), Comes_From_Source (N));
662 Set_No_Initialization (Expression (Tmp_Node));
663 Insert_Action (N, Tmp_Node);
665 if Needs_Finalization (T)
666 and then Ekind (PtrT) = E_Anonymous_Access_Type
668 -- Create local finalization list for access parameter
670 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
673 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
675 Node := Relocate_Node (N);
678 Make_Object_Declaration (Loc,
679 Defining_Identifier => Temp,
680 Constant_Present => True,
681 Object_Definition => New_Reference_To (PtrT, Loc),
682 Expression => Node));
685 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
686 -- interface type. In this case we use the type of the qualified
687 -- expression to allocate the object.
691 Def_Id : constant Entity_Id :=
692 Make_Defining_Identifier (Loc,
693 New_Internal_Name ('T'));
698 Make_Full_Type_Declaration (Loc,
699 Defining_Identifier => Def_Id,
701 Make_Access_To_Object_Definition (Loc,
703 Null_Exclusion_Present => False,
704 Constant_Present => False,
705 Subtype_Indication =>
706 New_Reference_To (Etype (Exp), Loc)));
708 Insert_Action (N, New_Decl);
710 -- Inherit the final chain to ensure that the expansion of the
711 -- aggregate is correct in case of controlled types
713 if Needs_Finalization (Directly_Designated_Type (PtrT)) then
714 Set_Associated_Final_Chain (Def_Id,
715 Associated_Final_Chain (PtrT));
718 -- Declare the object using the previous type declaration
720 if Aggr_In_Place then
722 Make_Object_Declaration (Loc,
723 Defining_Identifier => Temp,
724 Object_Definition => New_Reference_To (Def_Id, Loc),
727 New_Reference_To (Etype (Exp), Loc)));
729 Set_Comes_From_Source
730 (Expression (Tmp_Node), Comes_From_Source (N));
732 Set_No_Initialization (Expression (Tmp_Node));
733 Insert_Action (N, Tmp_Node);
735 if Needs_Finalization (T)
736 and then Ekind (PtrT) = E_Anonymous_Access_Type
738 -- Create local finalization list for access parameter
741 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
744 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
746 Node := Relocate_Node (N);
749 Make_Object_Declaration (Loc,
750 Defining_Identifier => Temp,
751 Constant_Present => True,
752 Object_Definition => New_Reference_To (Def_Id, Loc),
753 Expression => Node));
756 -- Generate an additional object containing the address of the
757 -- returned object. The type of this second object declaration
758 -- is the correct type required for the common processing that
759 -- is still performed by this subprogram. The displacement of
760 -- this pointer to reference the component associated with the
761 -- interface type will be done at the end of common processing.
764 Make_Object_Declaration (Loc,
765 Defining_Identifier => Make_Defining_Identifier (Loc,
766 New_Internal_Name ('P')),
767 Object_Definition => New_Reference_To (PtrT, Loc),
768 Expression => Unchecked_Convert_To (PtrT,
769 New_Reference_To (Temp, Loc)));
771 Insert_Action (N, New_Decl);
773 Tmp_Node := New_Decl;
774 Temp := Defining_Identifier (New_Decl);
778 Apply_Accessibility_Check (Temp);
780 -- Generate the tag assignment
782 -- Suppress the tag assignment when VM_Target because VM tags are
783 -- represented implicitly in objects.
785 if VM_Target /= No_VM then
788 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
789 -- interface objects because in this case the tag does not change.
791 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
792 pragma Assert (Is_Class_Wide_Type
793 (Directly_Designated_Type (Etype (N))));
796 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
798 TagR := New_Reference_To (Temp, Loc);
800 elsif Is_Private_Type (T)
801 and then Is_Tagged_Type (Underlying_Type (T))
803 TagT := Underlying_Type (T);
805 Unchecked_Convert_To (Underlying_Type (T),
806 Make_Explicit_Dereference (Loc,
807 Prefix => New_Reference_To (Temp, Loc)));
810 if Present (TagT) then
812 Make_Assignment_Statement (Loc,
814 Make_Selected_Component (Loc,
817 New_Reference_To (First_Tag_Component (TagT), Loc)),
820 Unchecked_Convert_To (RTE (RE_Tag),
822 (Elists.Node (First_Elmt (Access_Disp_Table (TagT))),
825 -- The previous assignment has to be done in any case
827 Set_Assignment_OK (Name (Tag_Assign));
828 Insert_Action (N, Tag_Assign);
831 if Needs_Finalization (DesigT)
832 and then Needs_Finalization (T)
836 Apool : constant Entity_Id :=
837 Associated_Storage_Pool (PtrT);
840 -- If it is an allocation on the secondary stack (i.e. a value
841 -- returned from a function), the object is attached on the
842 -- caller side as soon as the call is completed (see
843 -- Expand_Ctrl_Function_Call)
845 if Is_RTE (Apool, RE_SS_Pool) then
847 F : constant Entity_Id :=
848 Make_Defining_Identifier (Loc,
849 New_Internal_Name ('F'));
852 Make_Object_Declaration (Loc,
853 Defining_Identifier => F,
854 Object_Definition => New_Reference_To (RTE
855 (RE_Finalizable_Ptr), Loc)));
857 Flist := New_Reference_To (F, Loc);
858 Attach := Make_Integer_Literal (Loc, 1);
861 -- Normal case, not a secondary stack allocation
864 if Needs_Finalization (T)
865 and then Ekind (PtrT) = E_Anonymous_Access_Type
867 -- Create local finalization list for access parameter
870 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
872 Flist := Find_Final_List (PtrT);
875 Attach := Make_Integer_Literal (Loc, 2);
878 -- Generate an Adjust call if the object will be moved. In Ada
879 -- 2005, the object may be inherently limited, in which case
880 -- there is no Adjust procedure, and the object is built in
881 -- place. In Ada 95, the object can be limited but not
882 -- inherently limited if this allocator came from a return
883 -- statement (we're allocating the result on the secondary
884 -- stack). In that case, the object will be moved, so we _do_
888 and then not Is_Inherently_Limited_Type (T)
894 -- An unchecked conversion is needed in the classwide
895 -- case because the designated type can be an ancestor of
896 -- the subtype mark of the allocator.
898 Unchecked_Convert_To (T,
899 Make_Explicit_Dereference (Loc,
900 Prefix => New_Reference_To (Temp, Loc))),
904 With_Attach => Attach,
910 Rewrite (N, New_Reference_To (Temp, Loc));
911 Analyze_And_Resolve (N, PtrT);
913 -- Ada 2005 (AI-251): Displace the pointer to reference the record
914 -- component containing the secondary dispatch table of the interface
917 if Is_Interface (Directly_Designated_Type (PtrT)) then
918 Displace_Allocator_Pointer (N);
921 elsif Aggr_In_Place then
923 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
925 Make_Object_Declaration (Loc,
926 Defining_Identifier => Temp,
927 Object_Definition => New_Reference_To (PtrT, Loc),
928 Expression => Make_Allocator (Loc,
929 New_Reference_To (Etype (Exp), Loc)));
931 Set_Comes_From_Source
932 (Expression (Tmp_Node), Comes_From_Source (N));
934 Set_No_Initialization (Expression (Tmp_Node));
935 Insert_Action (N, Tmp_Node);
936 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
937 Rewrite (N, New_Reference_To (Temp, Loc));
938 Analyze_And_Resolve (N, PtrT);
940 elsif Is_Access_Type (T)
941 and then Can_Never_Be_Null (T)
943 Install_Null_Excluding_Check (Exp);
945 elsif Is_Access_Type (DesigT)
946 and then Nkind (Exp) = N_Allocator
947 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
949 -- Apply constraint to designated subtype indication
951 Apply_Constraint_Check (Expression (Exp),
952 Designated_Type (DesigT),
955 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
957 -- Propagate constraint_error to enclosing allocator
959 Rewrite (Exp, New_Copy (Expression (Exp)));
962 -- First check against the type of the qualified expression
964 -- NOTE: The commented call should be correct, but for some reason
965 -- causes the compiler to bomb (sigsegv) on ACVC test c34007g, so for
966 -- now we just perform the old (incorrect) test against the
967 -- designated subtype with no sliding in the else part of the if
968 -- statement below. ???
970 -- Apply_Constraint_Check (Exp, T, No_Sliding => True);
972 -- A check is also needed in cases where the designated subtype is
973 -- constrained and differs from the subtype given in the qualified
974 -- expression. Note that the check on the qualified expression does
975 -- not allow sliding, but this check does (a relaxation from Ada 83).
977 if Is_Constrained (DesigT)
978 and then not Subtypes_Statically_Match (T, DesigT)
980 Apply_Constraint_Check
981 (Exp, DesigT, No_Sliding => False);
983 -- The nonsliding check should really be performed (unconditionally)
984 -- against the subtype of the qualified expression, but that causes a
985 -- problem with c34007g (see above), so for now we retain this.
988 Apply_Constraint_Check
989 (Exp, DesigT, No_Sliding => True);
992 -- For an access to unconstrained packed array, GIGI needs to see an
993 -- expression with a constrained subtype in order to compute the
994 -- proper size for the allocator.
997 and then not Is_Constrained (T)
998 and then Is_Packed (T)
1001 ConstrT : constant Entity_Id :=
1002 Make_Defining_Identifier (Loc,
1003 Chars => New_Internal_Name ('A'));
1004 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1007 Make_Subtype_Declaration (Loc,
1008 Defining_Identifier => ConstrT,
1009 Subtype_Indication =>
1010 Make_Subtype_From_Expr (Exp, T)));
1011 Freeze_Itype (ConstrT, Exp);
1012 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1016 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1017 -- to a build-in-place function, then access to the allocated object
1018 -- must be passed to the function. Currently we limit such functions
1019 -- to those with constrained limited result subtypes, but eventually
1020 -- we plan to expand the allowed forms of functions that are treated
1021 -- as build-in-place.
1023 if Ada_Version >= Ada_05
1024 and then Is_Build_In_Place_Function_Call (Exp)
1026 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1031 when RE_Not_Available =>
1033 end Expand_Allocator_Expression;
1035 -----------------------------
1036 -- Expand_Array_Comparison --
1037 -----------------------------
1039 -- Expansion is only required in the case of array types. For the unpacked
1040 -- case, an appropriate runtime routine is called. For packed cases, and
1041 -- also in some other cases where a runtime routine cannot be called, the
1042 -- form of the expansion is:
1044 -- [body for greater_nn; boolean_expression]
1046 -- The body is built by Make_Array_Comparison_Op, and the form of the
1047 -- Boolean expression depends on the operator involved.
1049 procedure Expand_Array_Comparison (N : Node_Id) is
1050 Loc : constant Source_Ptr := Sloc (N);
1051 Op1 : Node_Id := Left_Opnd (N);
1052 Op2 : Node_Id := Right_Opnd (N);
1053 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1054 Ctyp : constant Entity_Id := Component_Type (Typ1);
1057 Func_Body : Node_Id;
1058 Func_Name : Entity_Id;
1062 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1063 -- True for byte addressable target
1065 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1066 -- Returns True if the length of the given operand is known to be less
1067 -- than 4. Returns False if this length is known to be four or greater
1068 -- or is not known at compile time.
1070 ------------------------
1071 -- Length_Less_Than_4 --
1072 ------------------------
1074 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1075 Otyp : constant Entity_Id := Etype (Opnd);
1078 if Ekind (Otyp) = E_String_Literal_Subtype then
1079 return String_Literal_Length (Otyp) < 4;
1083 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1084 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1085 Hi : constant Node_Id := Type_High_Bound (Ityp);
1090 if Compile_Time_Known_Value (Lo) then
1091 Lov := Expr_Value (Lo);
1096 if Compile_Time_Known_Value (Hi) then
1097 Hiv := Expr_Value (Hi);
1102 return Hiv < Lov + 3;
1105 end Length_Less_Than_4;
1107 -- Start of processing for Expand_Array_Comparison
1110 -- Deal first with unpacked case, where we can call a runtime routine
1111 -- except that we avoid this for targets for which are not addressable
1112 -- by bytes, and for the JVM/CIL, since they do not support direct
1113 -- addressing of array components.
1115 if not Is_Bit_Packed_Array (Typ1)
1116 and then Byte_Addressable
1117 and then VM_Target = No_VM
1119 -- The call we generate is:
1121 -- Compare_Array_xn[_Unaligned]
1122 -- (left'address, right'address, left'length, right'length) <op> 0
1124 -- x = U for unsigned, S for signed
1125 -- n = 8,16,32,64 for component size
1126 -- Add _Unaligned if length < 4 and component size is 8.
1127 -- <op> is the standard comparison operator
1129 if Component_Size (Typ1) = 8 then
1130 if Length_Less_Than_4 (Op1)
1132 Length_Less_Than_4 (Op2)
1134 if Is_Unsigned_Type (Ctyp) then
1135 Comp := RE_Compare_Array_U8_Unaligned;
1137 Comp := RE_Compare_Array_S8_Unaligned;
1141 if Is_Unsigned_Type (Ctyp) then
1142 Comp := RE_Compare_Array_U8;
1144 Comp := RE_Compare_Array_S8;
1148 elsif Component_Size (Typ1) = 16 then
1149 if Is_Unsigned_Type (Ctyp) then
1150 Comp := RE_Compare_Array_U16;
1152 Comp := RE_Compare_Array_S16;
1155 elsif Component_Size (Typ1) = 32 then
1156 if Is_Unsigned_Type (Ctyp) then
1157 Comp := RE_Compare_Array_U32;
1159 Comp := RE_Compare_Array_S32;
1162 else pragma Assert (Component_Size (Typ1) = 64);
1163 if Is_Unsigned_Type (Ctyp) then
1164 Comp := RE_Compare_Array_U64;
1166 Comp := RE_Compare_Array_S64;
1170 Remove_Side_Effects (Op1, Name_Req => True);
1171 Remove_Side_Effects (Op2, Name_Req => True);
1174 Make_Function_Call (Sloc (Op1),
1175 Name => New_Occurrence_Of (RTE (Comp), Loc),
1177 Parameter_Associations => New_List (
1178 Make_Attribute_Reference (Loc,
1179 Prefix => Relocate_Node (Op1),
1180 Attribute_Name => Name_Address),
1182 Make_Attribute_Reference (Loc,
1183 Prefix => Relocate_Node (Op2),
1184 Attribute_Name => Name_Address),
1186 Make_Attribute_Reference (Loc,
1187 Prefix => Relocate_Node (Op1),
1188 Attribute_Name => Name_Length),
1190 Make_Attribute_Reference (Loc,
1191 Prefix => Relocate_Node (Op2),
1192 Attribute_Name => Name_Length))));
1195 Make_Integer_Literal (Sloc (Op2),
1198 Analyze_And_Resolve (Op1, Standard_Integer);
1199 Analyze_And_Resolve (Op2, Standard_Integer);
1203 -- Cases where we cannot make runtime call
1205 -- For (a <= b) we convert to not (a > b)
1207 if Chars (N) = Name_Op_Le then
1213 Right_Opnd => Op2)));
1214 Analyze_And_Resolve (N, Standard_Boolean);
1217 -- For < the Boolean expression is
1218 -- greater__nn (op2, op1)
1220 elsif Chars (N) = Name_Op_Lt then
1221 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1225 Op1 := Right_Opnd (N);
1226 Op2 := Left_Opnd (N);
1228 -- For (a >= b) we convert to not (a < b)
1230 elsif Chars (N) = Name_Op_Ge then
1236 Right_Opnd => Op2)));
1237 Analyze_And_Resolve (N, Standard_Boolean);
1240 -- For > the Boolean expression is
1241 -- greater__nn (op1, op2)
1244 pragma Assert (Chars (N) = Name_Op_Gt);
1245 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1248 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1250 Make_Function_Call (Loc,
1251 Name => New_Reference_To (Func_Name, Loc),
1252 Parameter_Associations => New_List (Op1, Op2));
1254 Insert_Action (N, Func_Body);
1256 Analyze_And_Resolve (N, Standard_Boolean);
1259 when RE_Not_Available =>
1261 end Expand_Array_Comparison;
1263 ---------------------------
1264 -- Expand_Array_Equality --
1265 ---------------------------
1267 -- Expand an equality function for multi-dimensional arrays. Here is an
1268 -- example of such a function for Nb_Dimension = 2
1270 -- function Enn (A : atyp; B : btyp) return boolean is
1272 -- if (A'length (1) = 0 or else A'length (2) = 0)
1274 -- (B'length (1) = 0 or else B'length (2) = 0)
1276 -- return True; -- RM 4.5.2(22)
1279 -- if A'length (1) /= B'length (1)
1281 -- A'length (2) /= B'length (2)
1283 -- return False; -- RM 4.5.2(23)
1287 -- A1 : Index_T1 := A'first (1);
1288 -- B1 : Index_T1 := B'first (1);
1292 -- A2 : Index_T2 := A'first (2);
1293 -- B2 : Index_T2 := B'first (2);
1296 -- if A (A1, A2) /= B (B1, B2) then
1300 -- exit when A2 = A'last (2);
1301 -- A2 := Index_T2'succ (A2);
1302 -- B2 := Index_T2'succ (B2);
1306 -- exit when A1 = A'last (1);
1307 -- A1 := Index_T1'succ (A1);
1308 -- B1 := Index_T1'succ (B1);
1315 -- Note on the formal types used (atyp and btyp). If either of the arrays
1316 -- is of a private type, we use the underlying type, and do an unchecked
1317 -- conversion of the actual. If either of the arrays has a bound depending
1318 -- on a discriminant, then we use the base type since otherwise we have an
1319 -- escaped discriminant in the function.
1321 -- If both arrays are constrained and have the same bounds, we can generate
1322 -- a loop with an explicit iteration scheme using a 'Range attribute over
1325 function Expand_Array_Equality
1330 Typ : Entity_Id) return Node_Id
1332 Loc : constant Source_Ptr := Sloc (Nod);
1333 Decls : constant List_Id := New_List;
1334 Index_List1 : constant List_Id := New_List;
1335 Index_List2 : constant List_Id := New_List;
1339 Func_Name : Entity_Id;
1340 Func_Body : Node_Id;
1342 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1343 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1347 -- The parameter types to be used for the formals
1352 Num : Int) return Node_Id;
1353 -- This builds the attribute reference Arr'Nam (Expr)
1355 function Component_Equality (Typ : Entity_Id) return Node_Id;
1356 -- Create one statement to compare corresponding components, designated
1357 -- by a full set of indices.
1359 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1360 -- Given one of the arguments, computes the appropriate type to be used
1361 -- for that argument in the corresponding function formal
1363 function Handle_One_Dimension
1365 Index : Node_Id) return Node_Id;
1366 -- This procedure returns the following code
1369 -- Bn : Index_T := B'First (N);
1373 -- exit when An = A'Last (N);
1374 -- An := Index_T'Succ (An)
1375 -- Bn := Index_T'Succ (Bn)
1379 -- If both indices are constrained and identical, the procedure
1380 -- returns a simpler loop:
1382 -- for An in A'Range (N) loop
1386 -- N is the dimension for which we are generating a loop. Index is the
1387 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1388 -- xxx statement is either the loop or declare for the next dimension
1389 -- or if this is the last dimension the comparison of corresponding
1390 -- components of the arrays.
1392 -- The actual way the code works is to return the comparison of
1393 -- corresponding components for the N+1 call. That's neater!
1395 function Test_Empty_Arrays return Node_Id;
1396 -- This function constructs the test for both arrays being empty
1397 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1399 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1401 function Test_Lengths_Correspond return Node_Id;
1402 -- This function constructs the test for arrays having different lengths
1403 -- in at least one index position, in which case the resulting code is:
1405 -- A'length (1) /= B'length (1)
1407 -- A'length (2) /= B'length (2)
1418 Num : Int) return Node_Id
1422 Make_Attribute_Reference (Loc,
1423 Attribute_Name => Nam,
1424 Prefix => New_Reference_To (Arr, Loc),
1425 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1428 ------------------------
1429 -- Component_Equality --
1430 ------------------------
1432 function Component_Equality (Typ : Entity_Id) return Node_Id is
1437 -- if a(i1...) /= b(j1...) then return false; end if;
1440 Make_Indexed_Component (Loc,
1441 Prefix => Make_Identifier (Loc, Chars (A)),
1442 Expressions => Index_List1);
1445 Make_Indexed_Component (Loc,
1446 Prefix => Make_Identifier (Loc, Chars (B)),
1447 Expressions => Index_List2);
1449 Test := Expand_Composite_Equality
1450 (Nod, Component_Type (Typ), L, R, Decls);
1452 -- If some (sub)component is an unchecked_union, the whole operation
1453 -- will raise program error.
1455 if Nkind (Test) = N_Raise_Program_Error then
1457 -- This node is going to be inserted at a location where a
1458 -- statement is expected: clear its Etype so analysis will set
1459 -- it to the expected Standard_Void_Type.
1461 Set_Etype (Test, Empty);
1466 Make_Implicit_If_Statement (Nod,
1467 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1468 Then_Statements => New_List (
1469 Make_Simple_Return_Statement (Loc,
1470 Expression => New_Occurrence_Of (Standard_False, Loc))));
1472 end Component_Equality;
1478 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1489 T := Underlying_Type (T);
1491 X := First_Index (T);
1492 while Present (X) loop
1493 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1495 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1508 --------------------------
1509 -- Handle_One_Dimension --
1510 ---------------------------
1512 function Handle_One_Dimension
1514 Index : Node_Id) return Node_Id
1516 Need_Separate_Indexes : constant Boolean :=
1518 or else not Is_Constrained (Ltyp);
1519 -- If the index types are identical, and we are working with
1520 -- constrained types, then we can use the same index for both
1523 An : constant Entity_Id := Make_Defining_Identifier (Loc,
1524 Chars => New_Internal_Name ('A'));
1527 Index_T : Entity_Id;
1532 if N > Number_Dimensions (Ltyp) then
1533 return Component_Equality (Ltyp);
1536 -- Case where we generate a loop
1538 Index_T := Base_Type (Etype (Index));
1540 if Need_Separate_Indexes then
1542 Make_Defining_Identifier (Loc,
1543 Chars => New_Internal_Name ('B'));
1548 Append (New_Reference_To (An, Loc), Index_List1);
1549 Append (New_Reference_To (Bn, Loc), Index_List2);
1551 Stm_List := New_List (
1552 Handle_One_Dimension (N + 1, Next_Index (Index)));
1554 if Need_Separate_Indexes then
1556 -- Generate guard for loop, followed by increments of indices
1558 Append_To (Stm_List,
1559 Make_Exit_Statement (Loc,
1562 Left_Opnd => New_Reference_To (An, Loc),
1563 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1565 Append_To (Stm_List,
1566 Make_Assignment_Statement (Loc,
1567 Name => New_Reference_To (An, Loc),
1569 Make_Attribute_Reference (Loc,
1570 Prefix => New_Reference_To (Index_T, Loc),
1571 Attribute_Name => Name_Succ,
1572 Expressions => New_List (New_Reference_To (An, Loc)))));
1574 Append_To (Stm_List,
1575 Make_Assignment_Statement (Loc,
1576 Name => New_Reference_To (Bn, Loc),
1578 Make_Attribute_Reference (Loc,
1579 Prefix => New_Reference_To (Index_T, Loc),
1580 Attribute_Name => Name_Succ,
1581 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1584 -- If separate indexes, we need a declare block for An and Bn, and a
1585 -- loop without an iteration scheme.
1587 if Need_Separate_Indexes then
1589 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1592 Make_Block_Statement (Loc,
1593 Declarations => New_List (
1594 Make_Object_Declaration (Loc,
1595 Defining_Identifier => An,
1596 Object_Definition => New_Reference_To (Index_T, Loc),
1597 Expression => Arr_Attr (A, Name_First, N)),
1599 Make_Object_Declaration (Loc,
1600 Defining_Identifier => Bn,
1601 Object_Definition => New_Reference_To (Index_T, Loc),
1602 Expression => Arr_Attr (B, Name_First, N))),
1604 Handled_Statement_Sequence =>
1605 Make_Handled_Sequence_Of_Statements (Loc,
1606 Statements => New_List (Loop_Stm)));
1608 -- If no separate indexes, return loop statement with explicit
1609 -- iteration scheme on its own
1613 Make_Implicit_Loop_Statement (Nod,
1614 Statements => Stm_List,
1616 Make_Iteration_Scheme (Loc,
1617 Loop_Parameter_Specification =>
1618 Make_Loop_Parameter_Specification (Loc,
1619 Defining_Identifier => An,
1620 Discrete_Subtype_Definition =>
1621 Arr_Attr (A, Name_Range, N))));
1624 end Handle_One_Dimension;
1626 -----------------------
1627 -- Test_Empty_Arrays --
1628 -----------------------
1630 function Test_Empty_Arrays return Node_Id is
1640 for J in 1 .. Number_Dimensions (Ltyp) loop
1643 Left_Opnd => Arr_Attr (A, Name_Length, J),
1644 Right_Opnd => Make_Integer_Literal (Loc, 0));
1648 Left_Opnd => Arr_Attr (B, Name_Length, J),
1649 Right_Opnd => Make_Integer_Literal (Loc, 0));
1658 Left_Opnd => Relocate_Node (Alist),
1659 Right_Opnd => Atest);
1663 Left_Opnd => Relocate_Node (Blist),
1664 Right_Opnd => Btest);
1671 Right_Opnd => Blist);
1672 end Test_Empty_Arrays;
1674 -----------------------------
1675 -- Test_Lengths_Correspond --
1676 -----------------------------
1678 function Test_Lengths_Correspond return Node_Id is
1684 for J in 1 .. Number_Dimensions (Ltyp) loop
1687 Left_Opnd => Arr_Attr (A, Name_Length, J),
1688 Right_Opnd => Arr_Attr (B, Name_Length, J));
1695 Left_Opnd => Relocate_Node (Result),
1696 Right_Opnd => Rtest);
1701 end Test_Lengths_Correspond;
1703 -- Start of processing for Expand_Array_Equality
1706 Ltyp := Get_Arg_Type (Lhs);
1707 Rtyp := Get_Arg_Type (Rhs);
1709 -- For now, if the argument types are not the same, go to the base type,
1710 -- since the code assumes that the formals have the same type. This is
1711 -- fixable in future ???
1713 if Ltyp /= Rtyp then
1714 Ltyp := Base_Type (Ltyp);
1715 Rtyp := Base_Type (Rtyp);
1716 pragma Assert (Ltyp = Rtyp);
1719 -- Build list of formals for function
1721 Formals := New_List (
1722 Make_Parameter_Specification (Loc,
1723 Defining_Identifier => A,
1724 Parameter_Type => New_Reference_To (Ltyp, Loc)),
1726 Make_Parameter_Specification (Loc,
1727 Defining_Identifier => B,
1728 Parameter_Type => New_Reference_To (Rtyp, Loc)));
1730 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
1732 -- Build statement sequence for function
1735 Make_Subprogram_Body (Loc,
1737 Make_Function_Specification (Loc,
1738 Defining_Unit_Name => Func_Name,
1739 Parameter_Specifications => Formals,
1740 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
1742 Declarations => Decls,
1744 Handled_Statement_Sequence =>
1745 Make_Handled_Sequence_Of_Statements (Loc,
1746 Statements => New_List (
1748 Make_Implicit_If_Statement (Nod,
1749 Condition => Test_Empty_Arrays,
1750 Then_Statements => New_List (
1751 Make_Simple_Return_Statement (Loc,
1753 New_Occurrence_Of (Standard_True, Loc)))),
1755 Make_Implicit_If_Statement (Nod,
1756 Condition => Test_Lengths_Correspond,
1757 Then_Statements => New_List (
1758 Make_Simple_Return_Statement (Loc,
1760 New_Occurrence_Of (Standard_False, Loc)))),
1762 Handle_One_Dimension (1, First_Index (Ltyp)),
1764 Make_Simple_Return_Statement (Loc,
1765 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1767 Set_Has_Completion (Func_Name, True);
1768 Set_Is_Inlined (Func_Name);
1770 -- If the array type is distinct from the type of the arguments, it
1771 -- is the full view of a private type. Apply an unchecked conversion
1772 -- to insure that analysis of the call succeeds.
1782 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1784 L := OK_Convert_To (Ltyp, Lhs);
1788 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1790 R := OK_Convert_To (Rtyp, Rhs);
1793 Actuals := New_List (L, R);
1796 Append_To (Bodies, Func_Body);
1799 Make_Function_Call (Loc,
1800 Name => New_Reference_To (Func_Name, Loc),
1801 Parameter_Associations => Actuals);
1802 end Expand_Array_Equality;
1804 -----------------------------
1805 -- Expand_Boolean_Operator --
1806 -----------------------------
1808 -- Note that we first get the actual subtypes of the operands, since we
1809 -- always want to deal with types that have bounds.
1811 procedure Expand_Boolean_Operator (N : Node_Id) is
1812 Typ : constant Entity_Id := Etype (N);
1815 -- Special case of bit packed array where both operands are known to be
1816 -- properly aligned. In this case we use an efficient run time routine
1817 -- to carry out the operation (see System.Bit_Ops).
1819 if Is_Bit_Packed_Array (Typ)
1820 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
1821 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
1823 Expand_Packed_Boolean_Operator (N);
1827 -- For the normal non-packed case, the general expansion is to build
1828 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
1829 -- and then inserting it into the tree. The original operator node is
1830 -- then rewritten as a call to this function. We also use this in the
1831 -- packed case if either operand is a possibly unaligned object.
1834 Loc : constant Source_Ptr := Sloc (N);
1835 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1836 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1837 Func_Body : Node_Id;
1838 Func_Name : Entity_Id;
1841 Convert_To_Actual_Subtype (L);
1842 Convert_To_Actual_Subtype (R);
1843 Ensure_Defined (Etype (L), N);
1844 Ensure_Defined (Etype (R), N);
1845 Apply_Length_Check (R, Etype (L));
1847 if Nkind (N) = N_Op_Xor then
1848 Silly_Boolean_Array_Xor_Test (N, Etype (L));
1851 if Nkind (Parent (N)) = N_Assignment_Statement
1852 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
1854 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
1856 elsif Nkind (Parent (N)) = N_Op_Not
1857 and then Nkind (N) = N_Op_And
1859 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
1864 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
1865 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1866 Insert_Action (N, Func_Body);
1868 -- Now rewrite the expression with a call
1871 Make_Function_Call (Loc,
1872 Name => New_Reference_To (Func_Name, Loc),
1873 Parameter_Associations =>
1876 Make_Type_Conversion
1877 (Loc, New_Reference_To (Etype (L), Loc), R))));
1879 Analyze_And_Resolve (N, Typ);
1882 end Expand_Boolean_Operator;
1884 -------------------------------
1885 -- Expand_Composite_Equality --
1886 -------------------------------
1888 -- This function is only called for comparing internal fields of composite
1889 -- types when these fields are themselves composites. This is a special
1890 -- case because it is not possible to respect normal Ada visibility rules.
1892 function Expand_Composite_Equality
1897 Bodies : List_Id) return Node_Id
1899 Loc : constant Source_Ptr := Sloc (Nod);
1900 Full_Type : Entity_Id;
1905 if Is_Private_Type (Typ) then
1906 Full_Type := Underlying_Type (Typ);
1911 -- Defense against malformed private types with no completion the error
1912 -- will be diagnosed later by check_completion
1914 if No (Full_Type) then
1915 return New_Reference_To (Standard_False, Loc);
1918 Full_Type := Base_Type (Full_Type);
1920 if Is_Array_Type (Full_Type) then
1922 -- If the operand is an elementary type other than a floating-point
1923 -- type, then we can simply use the built-in block bitwise equality,
1924 -- since the predefined equality operators always apply and bitwise
1925 -- equality is fine for all these cases.
1927 if Is_Elementary_Type (Component_Type (Full_Type))
1928 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
1930 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
1932 -- For composite component types, and floating-point types, use the
1933 -- expansion. This deals with tagged component types (where we use
1934 -- the applicable equality routine) and floating-point, (where we
1935 -- need to worry about negative zeroes), and also the case of any
1936 -- composite type recursively containing such fields.
1939 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
1942 elsif Is_Tagged_Type (Full_Type) then
1944 -- Call the primitive operation "=" of this type
1946 if Is_Class_Wide_Type (Full_Type) then
1947 Full_Type := Root_Type (Full_Type);
1950 -- If this is derived from an untagged private type completed with a
1951 -- tagged type, it does not have a full view, so we use the primitive
1952 -- operations of the private type. This check should no longer be
1953 -- necessary when these types receive their full views ???
1955 if Is_Private_Type (Typ)
1956 and then not Is_Tagged_Type (Typ)
1957 and then not Is_Controlled (Typ)
1958 and then Is_Derived_Type (Typ)
1959 and then No (Full_View (Typ))
1961 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
1963 Prim := First_Elmt (Primitive_Operations (Full_Type));
1967 Eq_Op := Node (Prim);
1968 exit when Chars (Eq_Op) = Name_Op_Eq
1969 and then Etype (First_Formal (Eq_Op)) =
1970 Etype (Next_Formal (First_Formal (Eq_Op)))
1971 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
1973 pragma Assert (Present (Prim));
1976 Eq_Op := Node (Prim);
1979 Make_Function_Call (Loc,
1980 Name => New_Reference_To (Eq_Op, Loc),
1981 Parameter_Associations =>
1983 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
1984 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
1986 elsif Is_Record_Type (Full_Type) then
1987 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
1989 if Present (Eq_Op) then
1990 if Etype (First_Formal (Eq_Op)) /= Full_Type then
1992 -- Inherited equality from parent type. Convert the actuals to
1993 -- match signature of operation.
1996 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2000 Make_Function_Call (Loc,
2001 Name => New_Reference_To (Eq_Op, Loc),
2002 Parameter_Associations =>
2003 New_List (OK_Convert_To (T, Lhs),
2004 OK_Convert_To (T, Rhs)));
2008 -- Comparison between Unchecked_Union components
2010 if Is_Unchecked_Union (Full_Type) then
2012 Lhs_Type : Node_Id := Full_Type;
2013 Rhs_Type : Node_Id := Full_Type;
2014 Lhs_Discr_Val : Node_Id;
2015 Rhs_Discr_Val : Node_Id;
2020 if Nkind (Lhs) = N_Selected_Component then
2021 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2026 if Nkind (Rhs) = N_Selected_Component then
2027 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2030 -- Lhs of the composite equality
2032 if Is_Constrained (Lhs_Type) then
2034 -- Since the enclosing record type can never be an
2035 -- Unchecked_Union (this code is executed for records
2036 -- that do not have variants), we may reference its
2039 if Nkind (Lhs) = N_Selected_Component
2040 and then Has_Per_Object_Constraint (
2041 Entity (Selector_Name (Lhs)))
2044 Make_Selected_Component (Loc,
2045 Prefix => Prefix (Lhs),
2048 Get_Discriminant_Value (
2049 First_Discriminant (Lhs_Type),
2051 Stored_Constraint (Lhs_Type))));
2054 Lhs_Discr_Val := New_Copy (
2055 Get_Discriminant_Value (
2056 First_Discriminant (Lhs_Type),
2058 Stored_Constraint (Lhs_Type)));
2062 -- It is not possible to infer the discriminant since
2063 -- the subtype is not constrained.
2066 Make_Raise_Program_Error (Loc,
2067 Reason => PE_Unchecked_Union_Restriction);
2070 -- Rhs of the composite equality
2072 if Is_Constrained (Rhs_Type) then
2073 if Nkind (Rhs) = N_Selected_Component
2074 and then Has_Per_Object_Constraint (
2075 Entity (Selector_Name (Rhs)))
2078 Make_Selected_Component (Loc,
2079 Prefix => Prefix (Rhs),
2082 Get_Discriminant_Value (
2083 First_Discriminant (Rhs_Type),
2085 Stored_Constraint (Rhs_Type))));
2088 Rhs_Discr_Val := New_Copy (
2089 Get_Discriminant_Value (
2090 First_Discriminant (Rhs_Type),
2092 Stored_Constraint (Rhs_Type)));
2097 Make_Raise_Program_Error (Loc,
2098 Reason => PE_Unchecked_Union_Restriction);
2101 -- Call the TSS equality function with the inferred
2102 -- discriminant values.
2105 Make_Function_Call (Loc,
2106 Name => New_Reference_To (Eq_Op, Loc),
2107 Parameter_Associations => New_List (
2115 -- Shouldn't this be an else, we can't fall through the above
2119 Make_Function_Call (Loc,
2120 Name => New_Reference_To (Eq_Op, Loc),
2121 Parameter_Associations => New_List (Lhs, Rhs));
2125 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2129 -- It can be a simple record or the full view of a scalar private
2131 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2133 end Expand_Composite_Equality;
2135 ------------------------
2136 -- Expand_Concatenate --
2137 ------------------------
2139 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2140 Loc : constant Source_Ptr := Sloc (Cnode);
2142 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2143 -- Result type of concatenation
2145 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2146 -- Component type. Elements of this component type can appear as one
2147 -- of the operands of concatenation as well as arrays.
2149 Ityp : constant Entity_Id := Etype (First_Index (Atyp));
2153 -- This is the type we use to do arithmetic to compute the bounds and
2154 -- lengths of operands. The choice of this type is a little subtle and
2155 -- is discussed in a separate section at the start of the body code.
2157 Concatenation_Error : exception;
2158 -- Raised if concatenation is sure to raise a CE
2160 Result_May_Be_Null : Boolean := True;
2161 -- Reset to False if at least one operand is encountered which is known
2162 -- at compile time to be non-null. Used for handling the special case
2163 -- of setting the high bound to the last operand high bound for a null
2164 -- result, thus ensuring a proper high bound in the super-flat case.
2166 N : constant Nat := List_Length (Opnds);
2167 -- Number of concatenation operands including possibly null operands
2170 -- Number of operands excluding any known to be null, except that the
2171 -- last operand is always retained, in case it provides the bounds for
2175 -- Current operand being processed in the loop through operands. After
2176 -- this loop is complete, always contains the last operand (which is not
2177 -- the same as Operands (NN), since null operands are skipped).
2179 -- Arrays describing the operands, only the first NN entries of each
2180 -- array are set (NN < N when we exclude known null operands).
2182 Is_Fixed_Length : array (1 .. N) of Boolean;
2183 -- True if length of corresponding operand known at compile time
2185 Operands : array (1 .. N) of Node_Id;
2186 -- Set to the corresponding entry in the Opnds list (but note that null
2187 -- operands are excluded, so not all entries in the list are stored).
2189 Fixed_Length : array (1 .. N) of Uint;
2190 -- Set to length of operand. Entries in this array are set only if the
2191 -- corresponding entry in Is_Fixed_Length is True.
2193 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2194 -- Set to lower bound of operand. Either an integer literal in the case
2195 -- where the bound is known at compile time, else actual lower bound.
2196 -- The operand low bound is of type Ityp.
2198 Var_Length : array (1 .. N) of Entity_Id;
2199 -- Set to an entity of type Natural that contains the length of an
2200 -- operand whose length is not known at compile time. Entries in this
2201 -- array are set only if the corresponding entry in Is_Fixed_Length
2202 -- is False. The entity is of type Intyp.
2204 Aggr_Length : array (0 .. N) of Node_Id;
2205 -- The J'th entry in an expression node that represents the total length
2206 -- of operands 1 through J. It is either an integer literal node, or a
2207 -- reference to a constant entity with the right value, so it is fine
2208 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2209 -- entry always is set to zero. The length is of type Intyp.
2211 Low_Bound : Node_Id;
2212 -- A tree node representing the low bound of the result (of type Ityp).
2213 -- This is either an integer literal node, or an identifier reference to
2214 -- a constant entity initialized to the appropriate value.
2216 Last_Opnd_High_Bound : Node_Id;
2217 -- A tree node representing the high bound of the last operand. This
2218 -- need only be set if the result could be null. It is used for the
2219 -- special case of setting the right high bound for a null result.
2220 -- This is of type Ityp.
2222 High_Bound : Node_Id;
2223 -- A tree node representing the high bound of the result (of type Ityp)
2226 -- Result of the concatenation (of type Ityp)
2228 function To_Intyp (X : Node_Id) return Node_Id;
2229 -- Given a node of type Ityp, returns the corresponding value of type
2230 -- Intyp. For non-enumeration types, this is the identity. For enum
2231 -- types, the Pos of the value is returned.
2233 function To_Ityp (X : Node_Id) return Node_Id;
2234 -- The inverse function (uses Val in the case of enumeration types)
2240 function To_Intyp (X : Node_Id) return Node_Id is
2242 if Base_Type (Ityp) = Base_Type (Intyp) then
2245 elsif Is_Enumeration_Type (Ityp) then
2247 Make_Attribute_Reference (Loc,
2248 Prefix => New_Occurrence_Of (Ityp, Loc),
2249 Attribute_Name => Name_Pos,
2250 Expressions => New_List (X));
2253 return Convert_To (Intyp, X);
2261 function To_Ityp (X : Node_Id) return Node_Id is
2263 if Is_Enumeration_Type (Ityp) then
2265 Make_Attribute_Reference (Loc,
2266 Prefix => New_Occurrence_Of (Ityp, Loc),
2267 Attribute_Name => Name_Val,
2268 Expressions => New_List (X));
2270 -- Case where we will do a type conversion
2273 -- If the value is known at compile time, and known to be out of
2274 -- range of the index type or the base type, we can signal that
2275 -- we are sure to have a constraint error at run time.
2277 -- There are two reasons for doing this. First of all, it is of
2278 -- course nice to detect situations of certain exceptions, and
2279 -- generate a warning. But there is a more important reason. If
2280 -- the high bound is out of range of the base type, and is a
2281 -- literal, then that would cause a compilation illegality when
2282 -- we analyzed and resolved the expression.
2284 Set_Parent (X, Cnode);
2285 Analyze_And_Resolve (X);
2287 if Compile_Time_Compare
2288 (X, Type_High_Bound (Ityp),
2289 Assume_Valid => False) = GT
2291 Compile_Time_Compare
2292 (X, Type_High_Bound (Base_Type (Ityp)),
2293 Assume_Valid => False) = GT
2295 Apply_Compile_Time_Constraint_Error
2297 Msg => "concatenation result upper bound out of range?",
2298 Reason => CE_Range_Check_Failed);
2299 raise Concatenation_Error;
2302 if Base_Type (Ityp) = Base_Type (Intyp) then
2305 return Convert_To (Ityp, X);
2311 -- Local Declarations
2313 Opnd_Typ : Entity_Id;
2321 Aggr_Length (0) := Make_Integer_Literal (Loc, 0);
2323 -- Choose an appropriate computational type
2325 -- We will be doing calculations of lengths and bounds in this routine
2326 -- and computing one from the other in some cases, e.g. getting the high
2327 -- bound by adding the length-1 to the low bound.
2329 -- We can't just use the index type, or even its base type for this
2330 -- purpose for two reasons. First it might be an enumeration type which
2331 -- is not suitable fo computations of any kind, and second it may simply
2332 -- not have enough range. For example if the index type is -128..+127
2333 -- then lengths can be up to 256, which is out of range of the type.
2335 -- For enumeration types, we can simply use Standard_Integer, this is
2336 -- sufficient since the actual number of enumeration literals cannot
2337 -- possibly exceed the range of integer (remember we will be doing the
2338 -- arithmetic with POS values, not representation values).
2340 if Is_Enumeration_Type (Ityp) then
2341 Intyp := Standard_Integer;
2343 -- For modular types, we use a 32-bit modular type for types whose size
2344 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2345 -- identity type, and for larger unsigned types we use 64-bits.
2347 elsif Is_Modular_Integer_Type (Ityp) then
2348 if RM_Size (Base_Type (Ityp)) < RM_Size (Standard_Unsigned) then
2349 Intyp := Standard_Unsigned;
2350 elsif RM_Size (Base_Type (Ityp)) = RM_Size (Standard_Unsigned) then
2351 Intyp := Base_Type (Ityp);
2353 Intyp := RTE (RE_Long_Long_Unsigned);
2356 -- Similar treatment for signed types
2359 if RM_Size (Base_Type (Ityp)) < RM_Size (Standard_Integer) then
2360 Intyp := Standard_Integer;
2361 elsif RM_Size (Base_Type (Ityp)) = RM_Size (Standard_Integer) then
2362 Intyp := Base_Type (Ityp);
2364 Intyp := Standard_Long_Long_Integer;
2368 -- Go through operands setting up the above arrays
2372 Opnd := Remove_Head (Opnds);
2373 Opnd_Typ := Etype (Opnd);
2375 -- The parent got messed up when we put the operands in a list,
2376 -- so now put back the proper parent for the saved operand.
2378 Set_Parent (Opnd, Parent (Cnode));
2380 -- Set will be True when we have setup one entry in the array
2384 -- Singleton element (or character literal) case
2386 if Base_Type (Opnd_Typ) = Ctyp then
2388 Operands (NN) := Opnd;
2389 Is_Fixed_Length (NN) := True;
2390 Fixed_Length (NN) := Uint_1;
2391 Result_May_Be_Null := False;
2393 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2394 -- since we know that the result cannot be null).
2396 Opnd_Low_Bound (NN) :=
2397 Make_Attribute_Reference (Loc,
2398 Prefix => New_Reference_To (Ityp, Loc),
2399 Attribute_Name => Name_First);
2403 -- String literal case (can only occur for strings of course)
2405 elsif Nkind (Opnd) = N_String_Literal then
2406 Len := String_Literal_Length (Opnd_Typ);
2409 Result_May_Be_Null := False;
2412 -- Capture last operand high bound if result could be null
2414 if J = N and then Result_May_Be_Null then
2415 Last_Opnd_High_Bound :=
2418 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
2419 Right_Opnd => Make_Integer_Literal (Loc, 1));
2422 -- Skip null string literal
2424 if J < N and then Len = 0 then
2429 Operands (NN) := Opnd;
2430 Is_Fixed_Length (NN) := True;
2432 -- Set length and bounds
2434 Fixed_Length (NN) := Len;
2436 Opnd_Low_Bound (NN) :=
2437 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
2444 -- Check constrained case with known bounds
2446 if Is_Constrained (Opnd_Typ) then
2448 Index : constant Node_Id := First_Index (Opnd_Typ);
2449 Indx_Typ : constant Entity_Id := Etype (Index);
2450 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
2451 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
2454 -- Fixed length constrained array type with known at compile
2455 -- time bounds is last case of fixed length operand.
2457 if Compile_Time_Known_Value (Lo)
2459 Compile_Time_Known_Value (Hi)
2462 Loval : constant Uint := Expr_Value (Lo);
2463 Hival : constant Uint := Expr_Value (Hi);
2464 Len : constant Uint :=
2465 UI_Max (Hival - Loval + 1, Uint_0);
2469 Result_May_Be_Null := False;
2472 -- Capture last operand bound if result could be null
2474 if J = N and then Result_May_Be_Null then
2475 Last_Opnd_High_Bound :=
2477 Make_Integer_Literal (Loc,
2478 Intval => Expr_Value (Hi)));
2481 -- Exclude null length case unless last operand
2483 if J < N and then Len = 0 then
2488 Operands (NN) := Opnd;
2489 Is_Fixed_Length (NN) := True;
2490 Fixed_Length (NN) := Len;
2492 Opnd_Low_Bound (NN) := To_Ityp (
2493 Make_Integer_Literal (Loc,
2494 Intval => Expr_Value (Lo)));
2502 -- All cases where the length is not known at compile time, or the
2503 -- special case of an operand which is known to be null but has a
2504 -- lower bound other than 1 or is other than a string type.
2509 -- Capture operand bounds
2511 Opnd_Low_Bound (NN) :=
2512 Make_Attribute_Reference (Loc,
2514 Duplicate_Subexpr (Opnd, Name_Req => True),
2515 Attribute_Name => Name_First);
2517 if J = N and Result_May_Be_Null then
2518 Last_Opnd_High_Bound :=
2520 Make_Attribute_Reference (Loc,
2522 Duplicate_Subexpr (Opnd, Name_Req => True),
2523 Attribute_Name => Name_Last));
2526 -- Capture length of operand in entity
2528 Operands (NN) := Opnd;
2529 Is_Fixed_Length (NN) := False;
2532 Make_Defining_Identifier (Loc,
2533 Chars => New_Internal_Name ('L'));
2535 Insert_Action (Cnode,
2536 Make_Object_Declaration (Loc,
2537 Defining_Identifier => Var_Length (NN),
2538 Constant_Present => True,
2540 Object_Definition =>
2541 New_Occurrence_Of (Intyp, Loc),
2544 Make_Attribute_Reference (Loc,
2546 Duplicate_Subexpr (Opnd, Name_Req => True),
2547 Attribute_Name => Name_Length)),
2549 Suppress => All_Checks);
2553 -- Set next entry in aggregate length array
2555 -- For first entry, make either integer literal for fixed length
2556 -- or a reference to the saved length for variable length.
2559 if Is_Fixed_Length (1) then
2561 Make_Integer_Literal (Loc,
2562 Intval => Fixed_Length (1));
2565 New_Reference_To (Var_Length (1), Loc);
2568 -- If entry is fixed length and only fixed lengths so far, make
2569 -- appropriate new integer literal adding new length.
2571 elsif Is_Fixed_Length (NN)
2572 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
2575 Make_Integer_Literal (Loc,
2576 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
2578 -- All other cases, construct an addition node for the length and
2579 -- create an entity initialized to this length.
2583 Make_Defining_Identifier (Loc,
2584 Chars => New_Internal_Name ('L'));
2586 if Is_Fixed_Length (NN) then
2587 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
2589 Clen := New_Reference_To (Var_Length (NN), Loc);
2592 Insert_Action (Cnode,
2593 Make_Object_Declaration (Loc,
2594 Defining_Identifier => Ent,
2595 Constant_Present => True,
2597 Object_Definition =>
2598 New_Occurrence_Of (Intyp, Loc),
2602 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
2603 Right_Opnd => Clen)),
2605 Suppress => All_Checks);
2608 Make_Identifier (Loc,
2609 Chars => Chars (Ent));
2616 -- If we have only skipped null operands, return the last operand
2623 -- If we have only one non-null operand, return it and we are done.
2624 -- There is one case in which this cannot be done, and that is when
2625 -- the sole operand is of the element type, in which case it must be
2626 -- converted to an array, and the easiest way of doing that is to go
2627 -- through the normal general circuit.
2630 and then Base_Type (Etype (Operands (1))) /= Ctyp
2632 Result := Operands (1);
2636 -- Cases where we have a real concatenation
2638 -- Next step is to find the low bound for the result array that we
2639 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
2641 -- If the ultimate ancestor of the index subtype is a constrained array
2642 -- definition, then the lower bound is that of the index subtype as
2643 -- specified by (RM 4.5.3(6)).
2645 -- The right test here is to go to the root type, and then the ultimate
2646 -- ancestor is the first subtype of this root type.
2648 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
2650 Make_Attribute_Reference (Loc,
2652 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
2653 Attribute_Name => Name_First);
2655 -- If the first operand in the list has known length we know that
2656 -- the lower bound of the result is the lower bound of this operand.
2658 elsif Is_Fixed_Length (1) then
2659 Low_Bound := Opnd_Low_Bound (1);
2661 -- OK, we don't know the lower bound, we have to build a horrible
2662 -- expression actions node of the form
2664 -- if Cond1'Length /= 0 then
2667 -- if Opnd2'Length /= 0 then
2672 -- The nesting ends either when we hit an operand whose length is known
2673 -- at compile time, or on reaching the last operand, whose low bound we
2674 -- take unconditionally whether or not it is null. It's easiest to do
2675 -- this with a recursive procedure:
2679 function Get_Known_Bound (J : Nat) return Node_Id;
2680 -- Returns the lower bound determined by operands J .. NN
2682 ---------------------
2683 -- Get_Known_Bound --
2684 ---------------------
2686 function Get_Known_Bound (J : Nat) return Node_Id is
2688 if Is_Fixed_Length (J) or else J = NN then
2689 return New_Copy (Opnd_Low_Bound (J));
2693 Make_Conditional_Expression (Loc,
2694 Expressions => New_List (
2697 Left_Opnd => New_Reference_To (Var_Length (J), Loc),
2698 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2700 New_Copy (Opnd_Low_Bound (J)),
2701 Get_Known_Bound (J + 1)));
2703 end Get_Known_Bound;
2707 Make_Defining_Identifier (Loc,
2708 Chars => New_Internal_Name ('L'));
2710 Insert_Action (Cnode,
2711 Make_Object_Declaration (Loc,
2712 Defining_Identifier => Ent,
2713 Constant_Present => True,
2714 Object_Definition => New_Occurrence_Of (Ityp, Loc),
2715 Expression => Get_Known_Bound (1)),
2716 Suppress => All_Checks);
2718 Low_Bound := New_Reference_To (Ent, Loc);
2722 -- Now find the upper bound, normally this is Low_Bound + Length - 1
2727 Left_Opnd => To_Intyp (New_Copy (Low_Bound)),
2729 Make_Op_Subtract (Loc,
2730 Left_Opnd => New_Copy (Aggr_Length (NN)),
2731 Right_Opnd => Make_Integer_Literal (Loc, 1))));
2733 -- But there is one exception, namely when the result is null in which
2734 -- case the bounds come from the last operand (so that we get the proper
2735 -- bounds if the last operand is super-flat).
2737 if Result_May_Be_Null then
2739 Make_Conditional_Expression (Loc,
2740 Expressions => New_List (
2742 Left_Opnd => New_Copy (Aggr_Length (NN)),
2743 Right_Opnd => Make_Integer_Literal (Loc, 0)),
2744 Last_Opnd_High_Bound,
2748 -- Now we construct an array object with appropriate bounds
2751 Make_Defining_Identifier (Loc,
2752 Chars => New_Internal_Name ('S'));
2754 Insert_Action (Cnode,
2755 Make_Object_Declaration (Loc,
2756 Defining_Identifier => Ent,
2757 Object_Definition =>
2758 Make_Subtype_Indication (Loc,
2759 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
2761 Make_Index_Or_Discriminant_Constraint (Loc,
2762 Constraints => New_List (
2764 Low_Bound => Low_Bound,
2765 High_Bound => High_Bound))))),
2767 Suppress => All_Checks);
2769 -- Now we will generate the assignments to do the actual concatenation
2771 for J in 1 .. NN loop
2773 Lo : constant Node_Id :=
2775 Left_Opnd => To_Intyp (New_Copy (Low_Bound)),
2776 Right_Opnd => Aggr_Length (J - 1));
2778 Hi : constant Node_Id :=
2780 Left_Opnd => To_Intyp (New_Copy (Low_Bound)),
2782 Make_Op_Subtract (Loc,
2783 Left_Opnd => Aggr_Length (J),
2784 Right_Opnd => Make_Integer_Literal (Loc, 1)));
2787 -- Singleton case, simple assignment
2789 if Base_Type (Etype (Operands (J))) = Ctyp then
2790 Insert_Action (Cnode,
2791 Make_Assignment_Statement (Loc,
2793 Make_Indexed_Component (Loc,
2794 Prefix => New_Occurrence_Of (Ent, Loc),
2795 Expressions => New_List (To_Ityp (Lo))),
2796 Expression => Operands (J)),
2797 Suppress => All_Checks);
2799 -- Array case, slice assignment
2802 Insert_Action (Cnode,
2803 Make_Assignment_Statement (Loc,
2806 Prefix => New_Occurrence_Of (Ent, Loc),
2809 Low_Bound => To_Ityp (Lo),
2810 High_Bound => To_Ityp (Hi))),
2811 Expression => Operands (J)),
2812 Suppress => All_Checks);
2817 -- Finally we build the result, which is a reference to the array object
2819 Result := New_Reference_To (Ent, Loc);
2822 Rewrite (Cnode, Result);
2823 Analyze_And_Resolve (Cnode, Atyp);
2826 when Concatenation_Error =>
2827 Set_Etype (Cnode, Atyp);
2828 end Expand_Concatenate;
2830 ------------------------
2831 -- Expand_N_Allocator --
2832 ------------------------
2834 procedure Expand_N_Allocator (N : Node_Id) is
2835 PtrT : constant Entity_Id := Etype (N);
2836 Dtyp : constant Entity_Id := Designated_Type (PtrT);
2837 Etyp : constant Entity_Id := Etype (Expression (N));
2838 Loc : constant Source_Ptr := Sloc (N);
2843 procedure Complete_Coextension_Finalization;
2844 -- Generate finalization calls for all nested coextensions of N. This
2845 -- routine may allocate list controllers if necessary.
2847 procedure Rewrite_Coextension (N : Node_Id);
2848 -- Static coextensions have the same lifetime as the entity they
2849 -- constrain. Such occurrences can be rewritten as aliased objects
2850 -- and their unrestricted access used instead of the coextension.
2852 ---------------------------------------
2853 -- Complete_Coextension_Finalization --
2854 ---------------------------------------
2856 procedure Complete_Coextension_Finalization is
2858 Coext_Elmt : Elmt_Id;
2862 function Inside_A_Return_Statement (N : Node_Id) return Boolean;
2863 -- Determine whether node N is part of a return statement
2865 function Needs_Initialization_Call (N : Node_Id) return Boolean;
2866 -- Determine whether node N is a subtype indicator allocator which
2867 -- acts a coextension. Such coextensions need initialization.
2869 -------------------------------
2870 -- Inside_A_Return_Statement --
2871 -------------------------------
2873 function Inside_A_Return_Statement (N : Node_Id) return Boolean is
2878 while Present (P) loop
2880 (P, N_Extended_Return_Statement, N_Simple_Return_Statement)
2884 -- Stop the traversal when we reach a subprogram body
2886 elsif Nkind (P) = N_Subprogram_Body then
2894 end Inside_A_Return_Statement;
2896 -------------------------------
2897 -- Needs_Initialization_Call --
2898 -------------------------------
2900 function Needs_Initialization_Call (N : Node_Id) return Boolean is
2904 if Nkind (N) = N_Explicit_Dereference
2905 and then Nkind (Prefix (N)) = N_Identifier
2906 and then Nkind (Parent (Entity (Prefix (N)))) =
2907 N_Object_Declaration
2909 Obj_Decl := Parent (Entity (Prefix (N)));
2912 Present (Expression (Obj_Decl))
2913 and then Nkind (Expression (Obj_Decl)) = N_Allocator
2914 and then Nkind (Expression (Expression (Obj_Decl))) /=
2915 N_Qualified_Expression;
2919 end Needs_Initialization_Call;
2921 -- Start of processing for Complete_Coextension_Finalization
2924 -- When a coextension root is inside a return statement, we need to
2925 -- use the finalization chain of the function's scope. This does not
2926 -- apply for controlled named access types because in those cases we
2927 -- can use the finalization chain of the type itself.
2929 if Inside_A_Return_Statement (N)
2931 (Ekind (PtrT) = E_Anonymous_Access_Type
2933 (Ekind (PtrT) = E_Access_Type
2934 and then No (Associated_Final_Chain (PtrT))))
2938 Outer_S : Entity_Id;
2939 S : Entity_Id := Current_Scope;
2942 while Present (S) and then S /= Standard_Standard loop
2943 if Ekind (S) = E_Function then
2944 Outer_S := Scope (S);
2946 -- Retrieve the declaration of the body
2948 Decl := Parent (Parent (
2949 Corresponding_Body (Parent (Parent (S)))));
2956 -- Push the scope of the function body since we are inserting
2957 -- the list before the body, but we are currently in the body
2958 -- itself. Override the finalization list of PtrT since the
2959 -- finalization context is now different.
2961 Push_Scope (Outer_S);
2962 Build_Final_List (Decl, PtrT);
2966 -- The root allocator may not be controlled, but it still needs a
2967 -- finalization list for all nested coextensions.
2969 elsif No (Associated_Final_Chain (PtrT)) then
2970 Build_Final_List (N, PtrT);
2974 Make_Selected_Component (Loc,
2976 New_Reference_To (Associated_Final_Chain (PtrT), Loc),
2978 Make_Identifier (Loc, Name_F));
2980 Coext_Elmt := First_Elmt (Coextensions (N));
2981 while Present (Coext_Elmt) loop
2982 Coext := Node (Coext_Elmt);
2987 if Nkind (Coext) = N_Identifier then
2989 Make_Unchecked_Type_Conversion (Loc,
2990 Subtype_Mark => New_Reference_To (Etype (Coext), Loc),
2992 Make_Explicit_Dereference (Loc,
2993 Prefix => New_Copy_Tree (Coext)));
2995 Ref := New_Copy_Tree (Coext);
2998 -- No initialization call if not allowed
3000 Check_Restriction (No_Default_Initialization, N);
3002 if not Restriction_Active (No_Default_Initialization) then
3006 -- attach_to_final_list (Ref, Flist, 2)
3008 if Needs_Initialization_Call (Coext) then
3012 Typ => Etype (Coext),
3014 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3017 -- attach_to_final_list (Ref, Flist, 2)
3023 Flist_Ref => New_Copy_Tree (Flist),
3024 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
3028 Next_Elmt (Coext_Elmt);
3030 end Complete_Coextension_Finalization;
3032 -------------------------
3033 -- Rewrite_Coextension --
3034 -------------------------
3036 procedure Rewrite_Coextension (N : Node_Id) is
3037 Temp : constant Node_Id :=
3038 Make_Defining_Identifier (Loc,
3039 New_Internal_Name ('C'));
3042 -- Cnn : aliased Etyp;
3044 Decl : constant Node_Id :=
3045 Make_Object_Declaration (Loc,
3046 Defining_Identifier => Temp,
3047 Aliased_Present => True,
3048 Object_Definition =>
3049 New_Occurrence_Of (Etyp, Loc));
3053 if Nkind (Expression (N)) = N_Qualified_Expression then
3054 Set_Expression (Decl, Expression (Expression (N)));
3057 -- Find the proper insertion node for the declaration
3060 while Present (Nod) loop
3061 exit when Nkind (Nod) in N_Statement_Other_Than_Procedure_Call
3062 or else Nkind (Nod) = N_Procedure_Call_Statement
3063 or else Nkind (Nod) in N_Declaration;
3064 Nod := Parent (Nod);
3067 Insert_Before (Nod, Decl);
3071 Make_Attribute_Reference (Loc,
3072 Prefix => New_Occurrence_Of (Temp, Loc),
3073 Attribute_Name => Name_Unrestricted_Access));
3075 Analyze_And_Resolve (N, PtrT);
3076 end Rewrite_Coextension;
3078 -- Start of processing for Expand_N_Allocator
3081 -- RM E.2.3(22). We enforce that the expected type of an allocator
3082 -- shall not be a remote access-to-class-wide-limited-private type
3084 -- Why is this being done at expansion time, seems clearly wrong ???
3086 Validate_Remote_Access_To_Class_Wide_Type (N);
3088 -- Set the Storage Pool
3090 Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT)));
3092 if Present (Storage_Pool (N)) then
3093 if Is_RTE (Storage_Pool (N), RE_SS_Pool) then
3094 if VM_Target = No_VM then
3095 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3098 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
3099 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
3102 Set_Procedure_To_Call (N,
3103 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
3107 -- Under certain circumstances we can replace an allocator by an access
3108 -- to statically allocated storage. The conditions, as noted in AARM
3109 -- 3.10 (10c) are as follows:
3111 -- Size and initial value is known at compile time
3112 -- Access type is access-to-constant
3114 -- The allocator is not part of a constraint on a record component,
3115 -- because in that case the inserted actions are delayed until the
3116 -- record declaration is fully analyzed, which is too late for the
3117 -- analysis of the rewritten allocator.
3119 if Is_Access_Constant (PtrT)
3120 and then Nkind (Expression (N)) = N_Qualified_Expression
3121 and then Compile_Time_Known_Value (Expression (Expression (N)))
3122 and then Size_Known_At_Compile_Time (Etype (Expression
3124 and then not Is_Record_Type (Current_Scope)
3126 -- Here we can do the optimization. For the allocator
3130 -- We insert an object declaration
3132 -- Tnn : aliased x := y;
3134 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
3135 -- marked as requiring static allocation.
3138 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
3140 Desig := Subtype_Mark (Expression (N));
3142 -- If context is constrained, use constrained subtype directly,
3143 -- so that the constant is not labelled as having a nominally
3144 -- unconstrained subtype.
3146 if Entity (Desig) = Base_Type (Dtyp) then
3147 Desig := New_Occurrence_Of (Dtyp, Loc);
3151 Make_Object_Declaration (Loc,
3152 Defining_Identifier => Temp,
3153 Aliased_Present => True,
3154 Constant_Present => Is_Access_Constant (PtrT),
3155 Object_Definition => Desig,
3156 Expression => Expression (Expression (N))));
3159 Make_Attribute_Reference (Loc,
3160 Prefix => New_Occurrence_Of (Temp, Loc),
3161 Attribute_Name => Name_Unrestricted_Access));
3163 Analyze_And_Resolve (N, PtrT);
3165 -- We set the variable as statically allocated, since we don't want
3166 -- it going on the stack of the current procedure!
3168 Set_Is_Statically_Allocated (Temp);
3172 -- Same if the allocator is an access discriminant for a local object:
3173 -- instead of an allocator we create a local value and constrain the
3174 -- the enclosing object with the corresponding access attribute.
3176 if Is_Static_Coextension (N) then
3177 Rewrite_Coextension (N);
3181 -- The current allocator creates an object which may contain nested
3182 -- coextensions. Use the current allocator's finalization list to
3183 -- generate finalization call for all nested coextensions.
3185 if Is_Coextension_Root (N) then
3186 Complete_Coextension_Finalization;
3189 -- Handle case of qualified expression (other than optimization above)
3191 if Nkind (Expression (N)) = N_Qualified_Expression then
3192 Expand_Allocator_Expression (N);
3196 -- If the allocator is for a type which requires initialization, and
3197 -- there is no initial value (i.e. operand is a subtype indication
3198 -- rather than a qualified expression), then we must generate a call to
3199 -- the initialization routine using an expressions action node:
3201 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
3203 -- Here ptr_T is the pointer type for the allocator, and T is the
3204 -- subtype of the allocator. A special case arises if the designated
3205 -- type of the access type is a task or contains tasks. In this case
3206 -- the call to Init (Temp.all ...) is replaced by code that ensures
3207 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
3208 -- for details). In addition, if the type T is a task T, then the
3209 -- first argument to Init must be converted to the task record type.
3212 T : constant Entity_Id := Entity (Expression (N));
3220 Temp_Decl : Node_Id;
3221 Temp_Type : Entity_Id;
3222 Attach_Level : Uint;
3225 if No_Initialization (N) then
3228 -- Case of no initialization procedure present
3230 elsif not Has_Non_Null_Base_Init_Proc (T) then
3232 -- Case of simple initialization required
3234 if Needs_Simple_Initialization (T) then
3235 Check_Restriction (No_Default_Initialization, N);
3236 Rewrite (Expression (N),
3237 Make_Qualified_Expression (Loc,
3238 Subtype_Mark => New_Occurrence_Of (T, Loc),
3239 Expression => Get_Simple_Init_Val (T, N)));
3241 Analyze_And_Resolve (Expression (Expression (N)), T);
3242 Analyze_And_Resolve (Expression (N), T);
3243 Set_Paren_Count (Expression (Expression (N)), 1);
3244 Expand_N_Allocator (N);
3246 -- No initialization required
3252 -- Case of initialization procedure present, must be called
3255 Check_Restriction (No_Default_Initialization, N);
3257 if not Restriction_Active (No_Default_Initialization) then
3258 Init := Base_Init_Proc (T);
3260 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
3262 -- Construct argument list for the initialization routine call
3265 Make_Explicit_Dereference (Loc,
3266 Prefix => New_Reference_To (Temp, Loc));
3267 Set_Assignment_OK (Arg1);
3270 -- The initialization procedure expects a specific type. if the
3271 -- context is access to class wide, indicate that the object
3272 -- being allocated has the right specific type.
3274 if Is_Class_Wide_Type (Dtyp) then
3275 Arg1 := Unchecked_Convert_To (T, Arg1);
3278 -- If designated type is a concurrent type or if it is private
3279 -- type whose definition is a concurrent type, the first
3280 -- argument in the Init routine has to be unchecked conversion
3281 -- to the corresponding record type. If the designated type is
3282 -- a derived type, we also convert the argument to its root
3285 if Is_Concurrent_Type (T) then
3287 Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1);
3289 elsif Is_Private_Type (T)
3290 and then Present (Full_View (T))
3291 and then Is_Concurrent_Type (Full_View (T))
3294 Unchecked_Convert_To
3295 (Corresponding_Record_Type (Full_View (T)), Arg1);
3297 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
3299 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
3301 Arg1 := OK_Convert_To (Etype (Ftyp), Arg1);
3302 Set_Etype (Arg1, Ftyp);
3306 Args := New_List (Arg1);
3308 -- For the task case, pass the Master_Id of the access type as
3309 -- the value of the _Master parameter, and _Chain as the value
3310 -- of the _Chain parameter (_Chain will be defined as part of
3311 -- the generated code for the allocator).
3313 -- In Ada 2005, the context may be a function that returns an
3314 -- anonymous access type. In that case the Master_Id has been
3315 -- created when expanding the function declaration.
3317 if Has_Task (T) then
3318 if No (Master_Id (Base_Type (PtrT))) then
3320 -- If we have a non-library level task with restriction
3321 -- No_Task_Hierarchy set, then no point in expanding.
3323 if not Is_Library_Level_Entity (T)
3324 and then Restriction_Active (No_Task_Hierarchy)
3329 -- The designated type was an incomplete type, and the
3330 -- access type did not get expanded. Salvage it now.
3332 pragma Assert (Present (Parent (Base_Type (PtrT))));
3333 Expand_N_Full_Type_Declaration
3334 (Parent (Base_Type (PtrT)));
3337 -- If the context of the allocator is a declaration or an
3338 -- assignment, we can generate a meaningful image for it,
3339 -- even though subsequent assignments might remove the
3340 -- connection between task and entity. We build this image
3341 -- when the left-hand side is a simple variable, a simple
3342 -- indexed assignment or a simple selected component.
3344 if Nkind (Parent (N)) = N_Assignment_Statement then
3346 Nam : constant Node_Id := Name (Parent (N));
3349 if Is_Entity_Name (Nam) then
3351 Build_Task_Image_Decls
3354 (Entity (Nam), Sloc (Nam)), T);
3357 (Nam, N_Indexed_Component, N_Selected_Component)
3358 and then Is_Entity_Name (Prefix (Nam))
3361 Build_Task_Image_Decls
3362 (Loc, Nam, Etype (Prefix (Nam)));
3364 Decls := Build_Task_Image_Decls (Loc, T, T);
3368 elsif Nkind (Parent (N)) = N_Object_Declaration then
3370 Build_Task_Image_Decls
3371 (Loc, Defining_Identifier (Parent (N)), T);
3374 Decls := Build_Task_Image_Decls (Loc, T, T);
3379 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
3380 Append_To (Args, Make_Identifier (Loc, Name_uChain));
3382 Decl := Last (Decls);
3384 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
3386 -- Has_Task is false, Decls not used
3392 -- Add discriminants if discriminated type
3395 Dis : Boolean := False;
3399 if Has_Discriminants (T) then
3403 elsif Is_Private_Type (T)
3404 and then Present (Full_View (T))
3405 and then Has_Discriminants (Full_View (T))
3408 Typ := Full_View (T);
3413 -- If the allocated object will be constrained by the
3414 -- default values for discriminants, then build a subtype
3415 -- with those defaults, and change the allocated subtype
3416 -- to that. Note that this happens in fewer cases in Ada
3419 if not Is_Constrained (Typ)
3420 and then Present (Discriminant_Default_Value
3421 (First_Discriminant (Typ)))
3422 and then (Ada_Version < Ada_05
3424 not Has_Constrained_Partial_View (Typ))
3426 Typ := Build_Default_Subtype (Typ, N);
3427 Set_Expression (N, New_Reference_To (Typ, Loc));
3430 Discr := First_Elmt (Discriminant_Constraint (Typ));
3431 while Present (Discr) loop
3432 Nod := Node (Discr);
3433 Append (New_Copy_Tree (Node (Discr)), Args);
3435 -- AI-416: when the discriminant constraint is an
3436 -- anonymous access type make sure an accessibility
3437 -- check is inserted if necessary (3.10.2(22.q/2))
3439 if Ada_Version >= Ada_05
3441 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
3443 Apply_Accessibility_Check
3444 (Nod, Typ, Insert_Node => Nod);
3452 -- We set the allocator as analyzed so that when we analyze the
3453 -- expression actions node, we do not get an unwanted recursive
3454 -- expansion of the allocator expression.
3456 Set_Analyzed (N, True);
3457 Nod := Relocate_Node (N);
3459 -- Here is the transformation:
3461 -- output: Temp : constant ptr_T := new T;
3462 -- Init (Temp.all, ...);
3463 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
3464 -- <CTRL> Initialize (Finalizable (Temp.all));
3466 -- Here ptr_T is the pointer type for the allocator, and is the
3467 -- subtype of the allocator.
3470 Make_Object_Declaration (Loc,
3471 Defining_Identifier => Temp,
3472 Constant_Present => True,
3473 Object_Definition => New_Reference_To (Temp_Type, Loc),
3476 Set_Assignment_OK (Temp_Decl);
3477 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
3479 -- If the designated type is a task type or contains tasks,
3480 -- create block to activate created tasks, and insert
3481 -- declaration for Task_Image variable ahead of call.
3483 if Has_Task (T) then
3485 L : constant List_Id := New_List;
3488 Build_Task_Allocate_Block (L, Nod, Args);
3490 Insert_List_Before (First (Declarations (Blk)), Decls);
3491 Insert_Actions (N, L);
3496 Make_Procedure_Call_Statement (Loc,
3497 Name => New_Reference_To (Init, Loc),
3498 Parameter_Associations => Args));
3501 if Needs_Finalization (T) then
3503 -- Postpone the generation of a finalization call for the
3504 -- current allocator if it acts as a coextension.
3506 if Is_Dynamic_Coextension (N) then
3507 if No (Coextensions (N)) then
3508 Set_Coextensions (N, New_Elmt_List);
3511 Append_Elmt (New_Copy_Tree (Arg1), Coextensions (N));
3515 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
3517 -- Anonymous access types created for access parameters
3518 -- are attached to an explicitly constructed controller,
3519 -- which ensures that they can be finalized properly,
3520 -- even if their deallocation might not happen. The list
3521 -- associated with the controller is doubly-linked. For
3522 -- other anonymous access types, the object may end up
3523 -- on the global final list which is singly-linked.
3524 -- Work needed for access discriminants in Ada 2005 ???
3526 if Ekind (PtrT) = E_Anonymous_Access_Type
3528 Nkind (Associated_Node_For_Itype (PtrT))
3529 not in N_Subprogram_Specification
3531 Attach_Level := Uint_1;
3533 Attach_Level := Uint_2;
3538 Ref => New_Copy_Tree (Arg1),
3541 With_Attach => Make_Integer_Literal (Loc,
3542 Intval => Attach_Level)));
3546 Rewrite (N, New_Reference_To (Temp, Loc));
3547 Analyze_And_Resolve (N, PtrT);
3552 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
3553 -- object that has been rewritten as a reference, we displace "this"
3554 -- to reference properly its secondary dispatch table.
3556 if Nkind (N) = N_Identifier
3557 and then Is_Interface (Dtyp)
3559 Displace_Allocator_Pointer (N);
3563 when RE_Not_Available =>
3565 end Expand_N_Allocator;
3567 -----------------------
3568 -- Expand_N_And_Then --
3569 -----------------------
3571 -- Expand into conditional expression if Actions present, and also deal
3572 -- with optimizing case of arguments being True or False.
3574 procedure Expand_N_And_Then (N : Node_Id) is
3575 Loc : constant Source_Ptr := Sloc (N);
3576 Typ : constant Entity_Id := Etype (N);
3577 Left : constant Node_Id := Left_Opnd (N);
3578 Right : constant Node_Id := Right_Opnd (N);
3582 -- Deal with non-standard booleans
3584 if Is_Boolean_Type (Typ) then
3585 Adjust_Condition (Left);
3586 Adjust_Condition (Right);
3587 Set_Etype (N, Standard_Boolean);
3590 -- Check for cases where left argument is known to be True or False
3592 if Compile_Time_Known_Value (Left) then
3594 -- If left argument is True, change (True and then Right) to Right.
3595 -- Any actions associated with Right will be executed unconditionally
3596 -- and can thus be inserted into the tree unconditionally.
3598 if Expr_Value_E (Left) = Standard_True then
3599 if Present (Actions (N)) then
3600 Insert_Actions (N, Actions (N));
3605 -- If left argument is False, change (False and then Right) to False.
3606 -- In this case we can forget the actions associated with Right,
3607 -- since they will never be executed.
3609 else pragma Assert (Expr_Value_E (Left) = Standard_False);
3610 Kill_Dead_Code (Right);
3611 Kill_Dead_Code (Actions (N));
3612 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3615 Adjust_Result_Type (N, Typ);
3619 -- If Actions are present, we expand
3621 -- left and then right
3625 -- if left then right else false end
3627 -- with the actions becoming the Then_Actions of the conditional
3628 -- expression. This conditional expression is then further expanded
3629 -- (and will eventually disappear)
3631 if Present (Actions (N)) then
3632 Actlist := Actions (N);
3634 Make_Conditional_Expression (Loc,
3635 Expressions => New_List (
3638 New_Occurrence_Of (Standard_False, Loc))));
3640 Set_Then_Actions (N, Actlist);
3641 Analyze_And_Resolve (N, Standard_Boolean);
3642 Adjust_Result_Type (N, Typ);
3646 -- No actions present, check for cases of right argument True/False
3648 if Compile_Time_Known_Value (Right) then
3650 -- Change (Left and then True) to Left. Note that we know there are
3651 -- no actions associated with the True operand, since we just checked
3652 -- for this case above.
3654 if Expr_Value_E (Right) = Standard_True then
3657 -- Change (Left and then False) to False, making sure to preserve any
3658 -- side effects associated with the Left operand.
3660 else pragma Assert (Expr_Value_E (Right) = Standard_False);
3661 Remove_Side_Effects (Left);
3663 (N, New_Occurrence_Of (Standard_False, Loc));
3667 Adjust_Result_Type (N, Typ);
3668 end Expand_N_And_Then;
3670 -------------------------------------
3671 -- Expand_N_Conditional_Expression --
3672 -------------------------------------
3674 -- Expand into expression actions if then/else actions present
3676 procedure Expand_N_Conditional_Expression (N : Node_Id) is
3677 Loc : constant Source_Ptr := Sloc (N);
3678 Cond : constant Node_Id := First (Expressions (N));
3679 Thenx : constant Node_Id := Next (Cond);
3680 Elsex : constant Node_Id := Next (Thenx);
3681 Typ : constant Entity_Id := Etype (N);
3686 -- If either then or else actions are present, then given:
3688 -- if cond then then-expr else else-expr end
3690 -- we insert the following sequence of actions (using Insert_Actions):
3695 -- Cnn := then-expr;
3701 -- and replace the conditional expression by a reference to Cnn
3703 if Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
3704 Cnn := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
3707 Make_Implicit_If_Statement (N,
3708 Condition => Relocate_Node (Cond),
3710 Then_Statements => New_List (
3711 Make_Assignment_Statement (Sloc (Thenx),
3712 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
3713 Expression => Relocate_Node (Thenx))),
3715 Else_Statements => New_List (
3716 Make_Assignment_Statement (Sloc (Elsex),
3717 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
3718 Expression => Relocate_Node (Elsex))));
3720 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
3721 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
3723 if Present (Then_Actions (N)) then
3725 (First (Then_Statements (New_If)), Then_Actions (N));
3728 if Present (Else_Actions (N)) then
3730 (First (Else_Statements (New_If)), Else_Actions (N));
3733 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
3736 Make_Object_Declaration (Loc,
3737 Defining_Identifier => Cnn,
3738 Object_Definition => New_Occurrence_Of (Typ, Loc)));
3740 Insert_Action (N, New_If);
3741 Analyze_And_Resolve (N, Typ);
3743 end Expand_N_Conditional_Expression;
3745 -----------------------------------
3746 -- Expand_N_Explicit_Dereference --
3747 -----------------------------------
3749 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
3751 -- Insert explicit dereference call for the checked storage pool case
3753 Insert_Dereference_Action (Prefix (N));
3754 end Expand_N_Explicit_Dereference;
3760 procedure Expand_N_In (N : Node_Id) is
3761 Loc : constant Source_Ptr := Sloc (N);
3762 Rtyp : constant Entity_Id := Etype (N);
3763 Lop : constant Node_Id := Left_Opnd (N);
3764 Rop : constant Node_Id := Right_Opnd (N);
3765 Static : constant Boolean := Is_OK_Static_Expression (N);
3767 procedure Substitute_Valid_Check;
3768 -- Replaces node N by Lop'Valid. This is done when we have an explicit
3769 -- test for the left operand being in range of its subtype.
3771 ----------------------------
3772 -- Substitute_Valid_Check --
3773 ----------------------------
3775 procedure Substitute_Valid_Check is
3778 Make_Attribute_Reference (Loc,
3779 Prefix => Relocate_Node (Lop),
3780 Attribute_Name => Name_Valid));
3782 Analyze_And_Resolve (N, Rtyp);
3784 Error_Msg_N ("?explicit membership test may be optimized away", N);
3785 Error_Msg_N ("\?use ''Valid attribute instead", N);
3787 end Substitute_Valid_Check;
3789 -- Start of processing for Expand_N_In
3792 -- Check case of explicit test for an expression in range of its
3793 -- subtype. This is suspicious usage and we replace it with a 'Valid
3794 -- test and give a warning.
3796 if Is_Scalar_Type (Etype (Lop))
3797 and then Nkind (Rop) in N_Has_Entity
3798 and then Etype (Lop) = Entity (Rop)
3799 and then Comes_From_Source (N)
3800 and then VM_Target = No_VM
3802 Substitute_Valid_Check;
3806 -- Do validity check on operands
3808 if Validity_Checks_On and Validity_Check_Operands then
3809 Ensure_Valid (Left_Opnd (N));
3810 Validity_Check_Range (Right_Opnd (N));
3813 -- Case of explicit range
3815 if Nkind (Rop) = N_Range then
3817 Lo : constant Node_Id := Low_Bound (Rop);
3818 Hi : constant Node_Id := High_Bound (Rop);
3820 Ltyp : constant Entity_Id := Etype (Lop);
3822 Lo_Orig : constant Node_Id := Original_Node (Lo);
3823 Hi_Orig : constant Node_Id := Original_Node (Hi);
3825 Lcheck : Compare_Result;
3826 Ucheck : Compare_Result;
3828 Warn1 : constant Boolean :=
3829 Constant_Condition_Warnings
3830 and then Comes_From_Source (N)
3831 and then not In_Instance;
3832 -- This must be true for any of the optimization warnings, we
3833 -- clearly want to give them only for source with the flag on.
3834 -- We also skip these warnings in an instance since it may be
3835 -- the case that different instantiations have different ranges.
3837 Warn2 : constant Boolean :=
3839 and then Nkind (Original_Node (Rop)) = N_Range
3840 and then Is_Integer_Type (Etype (Lo));
3841 -- For the case where only one bound warning is elided, we also
3842 -- insist on an explicit range and an integer type. The reason is
3843 -- that the use of enumeration ranges including an end point is
3844 -- common, as is the use of a subtype name, one of whose bounds
3845 -- is the same as the type of the expression.
3848 -- If test is explicit x'first .. x'last, replace by valid check
3850 if Is_Scalar_Type (Ltyp)
3851 and then Nkind (Lo_Orig) = N_Attribute_Reference
3852 and then Attribute_Name (Lo_Orig) = Name_First
3853 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
3854 and then Entity (Prefix (Lo_Orig)) = Ltyp
3855 and then Nkind (Hi_Orig) = N_Attribute_Reference
3856 and then Attribute_Name (Hi_Orig) = Name_Last
3857 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
3858 and then Entity (Prefix (Hi_Orig)) = Ltyp
3859 and then Comes_From_Source (N)
3860 and then VM_Target = No_VM
3862 Substitute_Valid_Check;
3866 -- If bounds of type are known at compile time, and the end points
3867 -- are known at compile time and identical, this is another case
3868 -- for substituting a valid test. We only do this for discrete
3869 -- types, since it won't arise in practice for float types.
3871 if Comes_From_Source (N)
3872 and then Is_Discrete_Type (Ltyp)
3873 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
3874 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
3875 and then Compile_Time_Known_Value (Lo)
3876 and then Compile_Time_Known_Value (Hi)
3877 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
3878 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
3880 -- Kill warnings in instances, since they may be cases where we
3881 -- have a test in the generic that makes sense with some types
3882 -- and not with other types.
3884 and then not In_Instance
3886 Substitute_Valid_Check;
3890 -- If we have an explicit range, do a bit of optimization based
3891 -- on range analysis (we may be able to kill one or both checks).
3893 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
3894 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
3896 -- If either check is known to fail, replace result by False since
3897 -- the other check does not matter. Preserve the static flag for
3898 -- legality checks, because we are constant-folding beyond RM 4.9.
3900 if Lcheck = LT or else Ucheck = GT then
3902 Error_Msg_N ("?range test optimized away", N);
3903 Error_Msg_N ("\?value is known to be out of range", N);
3907 New_Reference_To (Standard_False, Loc));
3908 Analyze_And_Resolve (N, Rtyp);
3909 Set_Is_Static_Expression (N, Static);
3913 -- If both checks are known to succeed, replace result by True,
3914 -- since we know we are in range.
3916 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
3918 Error_Msg_N ("?range test optimized away", N);
3919 Error_Msg_N ("\?value is known to be in range", N);
3923 New_Reference_To (Standard_True, Loc));
3924 Analyze_And_Resolve (N, Rtyp);
3925 Set_Is_Static_Expression (N, Static);
3929 -- If lower bound check succeeds and upper bound check is not
3930 -- known to succeed or fail, then replace the range check with
3931 -- a comparison against the upper bound.
3933 elsif Lcheck in Compare_GE then
3934 if Warn2 and then not In_Instance then
3935 Error_Msg_N ("?lower bound test optimized away", Lo);
3936 Error_Msg_N ("\?value is known to be in range", Lo);
3942 Right_Opnd => High_Bound (Rop)));
3943 Analyze_And_Resolve (N, Rtyp);
3947 -- If upper bound check succeeds and lower bound check is not
3948 -- known to succeed or fail, then replace the range check with
3949 -- a comparison against the lower bound.
3951 elsif Ucheck in Compare_LE then
3952 if Warn2 and then not In_Instance then
3953 Error_Msg_N ("?upper bound test optimized away", Hi);
3954 Error_Msg_N ("\?value is known to be in range", Hi);
3960 Right_Opnd => Low_Bound (Rop)));
3961 Analyze_And_Resolve (N, Rtyp);
3966 -- We couldn't optimize away the range check, but there is one
3967 -- more issue. If we are checking constant conditionals, then we
3968 -- see if we can determine the outcome assuming everything is
3969 -- valid, and if so give an appropriate warning.
3971 if Warn1 and then not Assume_No_Invalid_Values then
3972 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
3973 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
3975 -- Result is out of range for valid value
3977 if Lcheck = LT or else Ucheck = GT then
3979 ("?value can only be in range if it is invalid", N);
3981 -- Result is in range for valid value
3983 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
3985 ("?value can only be out of range if it is invalid", N);
3987 -- Lower bound check succeeds if value is valid
3989 elsif Warn2 and then Lcheck in Compare_GE then
3991 ("?lower bound check only fails if it is invalid", Lo);
3993 -- Upper bound check succeeds if value is valid
3995 elsif Warn2 and then Ucheck in Compare_LE then
3997 ("?upper bound check only fails for invalid values", Hi);
4002 -- For all other cases of an explicit range, nothing to be done
4006 -- Here right operand is a subtype mark
4010 Typ : Entity_Id := Etype (Rop);
4011 Is_Acc : constant Boolean := Is_Access_Type (Typ);
4012 Obj : Node_Id := Lop;
4013 Cond : Node_Id := Empty;
4016 Remove_Side_Effects (Obj);
4018 -- For tagged type, do tagged membership operation
4020 if Is_Tagged_Type (Typ) then
4022 -- No expansion will be performed when VM_Target, as the VM
4023 -- back-ends will handle the membership tests directly (tags
4024 -- are not explicitly represented in Java objects, so the
4025 -- normal tagged membership expansion is not what we want).
4027 if VM_Target = No_VM then
4028 Rewrite (N, Tagged_Membership (N));
4029 Analyze_And_Resolve (N, Rtyp);
4034 -- If type is scalar type, rewrite as x in t'first .. t'last.
4035 -- This reason we do this is that the bounds may have the wrong
4036 -- type if they come from the original type definition. Also this
4037 -- way we get all the processing above for an explicit range.
4039 elsif Is_Scalar_Type (Typ) then
4043 Make_Attribute_Reference (Loc,
4044 Attribute_Name => Name_First,
4045 Prefix => New_Reference_To (Typ, Loc)),
4048 Make_Attribute_Reference (Loc,
4049 Attribute_Name => Name_Last,
4050 Prefix => New_Reference_To (Typ, Loc))));
4051 Analyze_And_Resolve (N, Rtyp);
4054 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4055 -- a membership test if the subtype mark denotes a constrained
4056 -- Unchecked_Union subtype and the expression lacks inferable
4059 elsif Is_Unchecked_Union (Base_Type (Typ))
4060 and then Is_Constrained (Typ)
4061 and then not Has_Inferable_Discriminants (Lop)
4064 Make_Raise_Program_Error (Loc,
4065 Reason => PE_Unchecked_Union_Restriction));
4067 -- Prevent Gigi from generating incorrect code by rewriting
4068 -- the test as a standard False.
4071 New_Occurrence_Of (Standard_False, Loc));
4076 -- Here we have a non-scalar type
4079 Typ := Designated_Type (Typ);
4082 if not Is_Constrained (Typ) then
4084 New_Reference_To (Standard_True, Loc));
4085 Analyze_And_Resolve (N, Rtyp);
4087 -- For the constrained array case, we have to check the subscripts
4088 -- for an exact match if the lengths are non-zero (the lengths
4089 -- must match in any case).
4091 elsif Is_Array_Type (Typ) then
4093 Check_Subscripts : declare
4094 function Construct_Attribute_Reference
4097 Dim : Nat) return Node_Id;
4098 -- Build attribute reference E'Nam(Dim)
4100 -----------------------------------
4101 -- Construct_Attribute_Reference --
4102 -----------------------------------
4104 function Construct_Attribute_Reference
4107 Dim : Nat) return Node_Id
4111 Make_Attribute_Reference (Loc,
4113 Attribute_Name => Nam,
4114 Expressions => New_List (
4115 Make_Integer_Literal (Loc, Dim)));
4116 end Construct_Attribute_Reference;
4118 -- Start processing for Check_Subscripts
4121 for J in 1 .. Number_Dimensions (Typ) loop
4122 Evolve_And_Then (Cond,
4125 Construct_Attribute_Reference
4126 (Duplicate_Subexpr_No_Checks (Obj),
4129 Construct_Attribute_Reference
4130 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
4132 Evolve_And_Then (Cond,
4135 Construct_Attribute_Reference
4136 (Duplicate_Subexpr_No_Checks (Obj),
4139 Construct_Attribute_Reference
4140 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
4149 Right_Opnd => Make_Null (Loc)),
4150 Right_Opnd => Cond);
4154 Analyze_And_Resolve (N, Rtyp);
4155 end Check_Subscripts;
4157 -- These are the cases where constraint checks may be required,
4158 -- e.g. records with possible discriminants
4161 -- Expand the test into a series of discriminant comparisons.
4162 -- The expression that is built is the negation of the one that
4163 -- is used for checking discriminant constraints.
4165 Obj := Relocate_Node (Left_Opnd (N));
4167 if Has_Discriminants (Typ) then
4168 Cond := Make_Op_Not (Loc,
4169 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
4172 Cond := Make_Or_Else (Loc,
4176 Right_Opnd => Make_Null (Loc)),
4177 Right_Opnd => Cond);
4181 Cond := New_Occurrence_Of (Standard_True, Loc);
4185 Analyze_And_Resolve (N, Rtyp);
4191 --------------------------------
4192 -- Expand_N_Indexed_Component --
4193 --------------------------------
4195 procedure Expand_N_Indexed_Component (N : Node_Id) is
4196 Loc : constant Source_Ptr := Sloc (N);
4197 Typ : constant Entity_Id := Etype (N);
4198 P : constant Node_Id := Prefix (N);
4199 T : constant Entity_Id := Etype (P);
4202 -- A special optimization, if we have an indexed component that is
4203 -- selecting from a slice, then we can eliminate the slice, since, for
4204 -- example, x (i .. j)(k) is identical to x(k). The only difference is
4205 -- the range check required by the slice. The range check for the slice
4206 -- itself has already been generated. The range check for the
4207 -- subscripting operation is ensured by converting the subject to
4208 -- the subtype of the slice.
4210 -- This optimization not only generates better code, avoiding slice
4211 -- messing especially in the packed case, but more importantly bypasses
4212 -- some problems in handling this peculiar case, for example, the issue
4213 -- of dealing specially with object renamings.
4215 if Nkind (P) = N_Slice then
4217 Make_Indexed_Component (Loc,
4218 Prefix => Prefix (P),
4219 Expressions => New_List (
4221 (Etype (First_Index (Etype (P))),
4222 First (Expressions (N))))));
4223 Analyze_And_Resolve (N, Typ);
4227 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
4228 -- function, then additional actuals must be passed.
4230 if Ada_Version >= Ada_05
4231 and then Is_Build_In_Place_Function_Call (P)
4233 Make_Build_In_Place_Call_In_Anonymous_Context (P);
4236 -- If the prefix is an access type, then we unconditionally rewrite if
4237 -- as an explicit deference. This simplifies processing for several
4238 -- cases, including packed array cases and certain cases in which checks
4239 -- must be generated. We used to try to do this only when it was
4240 -- necessary, but it cleans up the code to do it all the time.
4242 if Is_Access_Type (T) then
4243 Insert_Explicit_Dereference (P);
4244 Analyze_And_Resolve (P, Designated_Type (T));
4247 -- Generate index and validity checks
4249 Generate_Index_Checks (N);
4251 if Validity_Checks_On and then Validity_Check_Subscripts then
4252 Apply_Subscript_Validity_Checks (N);
4255 -- All done for the non-packed case
4257 if not Is_Packed (Etype (Prefix (N))) then
4261 -- For packed arrays that are not bit-packed (i.e. the case of an array
4262 -- with one or more index types with a non-contiguous enumeration type),
4263 -- we can always use the normal packed element get circuit.
4265 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
4266 Expand_Packed_Element_Reference (N);
4270 -- For a reference to a component of a bit packed array, we have to
4271 -- convert it to a reference to the corresponding Packed_Array_Type.
4272 -- We only want to do this for simple references, and not for:
4274 -- Left side of assignment, or prefix of left side of assignment, or
4275 -- prefix of the prefix, to handle packed arrays of packed arrays,
4276 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
4278 -- Renaming objects in renaming associations
4279 -- This case is handled when a use of the renamed variable occurs
4281 -- Actual parameters for a procedure call
4282 -- This case is handled in Exp_Ch6.Expand_Actuals
4284 -- The second expression in a 'Read attribute reference
4286 -- The prefix of an address or size attribute reference
4288 -- The following circuit detects these exceptions
4291 Child : Node_Id := N;
4292 Parnt : Node_Id := Parent (N);
4296 if Nkind (Parnt) = N_Unchecked_Expression then
4299 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
4300 N_Procedure_Call_Statement)
4301 or else (Nkind (Parnt) = N_Parameter_Association
4303 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
4307 elsif Nkind (Parnt) = N_Attribute_Reference
4308 and then (Attribute_Name (Parnt) = Name_Address
4310 Attribute_Name (Parnt) = Name_Size)
4311 and then Prefix (Parnt) = Child
4315 elsif Nkind (Parnt) = N_Assignment_Statement
4316 and then Name (Parnt) = Child
4320 -- If the expression is an index of an indexed component, it must
4321 -- be expanded regardless of context.
4323 elsif Nkind (Parnt) = N_Indexed_Component
4324 and then Child /= Prefix (Parnt)
4326 Expand_Packed_Element_Reference (N);
4329 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
4330 and then Name (Parent (Parnt)) = Parnt
4334 elsif Nkind (Parnt) = N_Attribute_Reference
4335 and then Attribute_Name (Parnt) = Name_Read
4336 and then Next (First (Expressions (Parnt))) = Child
4340 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
4341 and then Prefix (Parnt) = Child
4346 Expand_Packed_Element_Reference (N);
4350 -- Keep looking up tree for unchecked expression, or if we are the
4351 -- prefix of a possible assignment left side.
4354 Parnt := Parent (Child);
4357 end Expand_N_Indexed_Component;
4359 ---------------------
4360 -- Expand_N_Not_In --
4361 ---------------------
4363 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
4364 -- can be done. This avoids needing to duplicate this expansion code.
4366 procedure Expand_N_Not_In (N : Node_Id) is
4367 Loc : constant Source_Ptr := Sloc (N);
4368 Typ : constant Entity_Id := Etype (N);
4369 Cfs : constant Boolean := Comes_From_Source (N);
4376 Left_Opnd => Left_Opnd (N),
4377 Right_Opnd => Right_Opnd (N))));
4379 -- We want this to appear as coming from source if original does (see
4380 -- transformations in Expand_N_In).
4382 Set_Comes_From_Source (N, Cfs);
4383 Set_Comes_From_Source (Right_Opnd (N), Cfs);
4385 -- Now analyze transformed node
4387 Analyze_And_Resolve (N, Typ);
4388 end Expand_N_Not_In;
4394 -- The only replacement required is for the case of a null of type that is
4395 -- an access to protected subprogram. We represent such access values as a
4396 -- record, and so we must replace the occurrence of null by the equivalent
4397 -- record (with a null address and a null pointer in it), so that the
4398 -- backend creates the proper value.
4400 procedure Expand_N_Null (N : Node_Id) is
4401 Loc : constant Source_Ptr := Sloc (N);
4402 Typ : constant Entity_Id := Etype (N);
4406 if Is_Access_Protected_Subprogram_Type (Typ) then
4408 Make_Aggregate (Loc,
4409 Expressions => New_List (
4410 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
4414 Analyze_And_Resolve (N, Equivalent_Type (Typ));
4416 -- For subsequent semantic analysis, the node must retain its type.
4417 -- Gigi in any case replaces this type by the corresponding record
4418 -- type before processing the node.
4424 when RE_Not_Available =>
4428 ---------------------
4429 -- Expand_N_Op_Abs --
4430 ---------------------
4432 procedure Expand_N_Op_Abs (N : Node_Id) is
4433 Loc : constant Source_Ptr := Sloc (N);
4434 Expr : constant Node_Id := Right_Opnd (N);
4437 Unary_Op_Validity_Checks (N);
4439 -- Deal with software overflow checking
4441 if not Backend_Overflow_Checks_On_Target
4442 and then Is_Signed_Integer_Type (Etype (N))
4443 and then Do_Overflow_Check (N)
4445 -- The only case to worry about is when the argument is equal to the
4446 -- largest negative number, so what we do is to insert the check:
4448 -- [constraint_error when Expr = typ'Base'First]
4450 -- with the usual Duplicate_Subexpr use coding for expr
4453 Make_Raise_Constraint_Error (Loc,
4456 Left_Opnd => Duplicate_Subexpr (Expr),
4458 Make_Attribute_Reference (Loc,
4460 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
4461 Attribute_Name => Name_First)),
4462 Reason => CE_Overflow_Check_Failed));
4465 -- Vax floating-point types case
4467 if Vax_Float (Etype (N)) then
4468 Expand_Vax_Arith (N);
4470 end Expand_N_Op_Abs;
4472 ---------------------
4473 -- Expand_N_Op_Add --
4474 ---------------------
4476 procedure Expand_N_Op_Add (N : Node_Id) is
4477 Typ : constant Entity_Id := Etype (N);
4480 Binary_Op_Validity_Checks (N);
4482 -- N + 0 = 0 + N = N for integer types
4484 if Is_Integer_Type (Typ) then
4485 if Compile_Time_Known_Value (Right_Opnd (N))
4486 and then Expr_Value (Right_Opnd (N)) = Uint_0
4488 Rewrite (N, Left_Opnd (N));
4491 elsif Compile_Time_Known_Value (Left_Opnd (N))
4492 and then Expr_Value (Left_Opnd (N)) = Uint_0
4494 Rewrite (N, Right_Opnd (N));
4499 -- Arithmetic overflow checks for signed integer/fixed point types
4501 if Is_Signed_Integer_Type (Typ)
4502 or else Is_Fixed_Point_Type (Typ)
4504 Apply_Arithmetic_Overflow_Check (N);
4507 -- Vax floating-point types case
4509 elsif Vax_Float (Typ) then
4510 Expand_Vax_Arith (N);
4512 end Expand_N_Op_Add;
4514 ---------------------
4515 -- Expand_N_Op_And --
4516 ---------------------
4518 procedure Expand_N_Op_And (N : Node_Id) is
4519 Typ : constant Entity_Id := Etype (N);
4522 Binary_Op_Validity_Checks (N);
4524 if Is_Array_Type (Etype (N)) then
4525 Expand_Boolean_Operator (N);
4527 elsif Is_Boolean_Type (Etype (N)) then
4528 Adjust_Condition (Left_Opnd (N));
4529 Adjust_Condition (Right_Opnd (N));
4530 Set_Etype (N, Standard_Boolean);
4531 Adjust_Result_Type (N, Typ);
4533 end Expand_N_Op_And;
4535 ------------------------
4536 -- Expand_N_Op_Concat --
4537 ------------------------
4539 procedure Expand_N_Op_Concat (N : Node_Id) is
4541 -- List of operands to be concatenated
4544 -- Node which is to be replaced by the result of concatenating the nodes
4545 -- in the list Opnds.
4548 -- Ensure validity of both operands
4550 Binary_Op_Validity_Checks (N);
4552 -- If we are the left operand of a concatenation higher up the tree,
4553 -- then do nothing for now, since we want to deal with a series of
4554 -- concatenations as a unit.
4556 if Nkind (Parent (N)) = N_Op_Concat
4557 and then N = Left_Opnd (Parent (N))
4562 -- We get here with a concatenation whose left operand may be a
4563 -- concatenation itself with a consistent type. We need to process
4564 -- these concatenation operands from left to right, which means
4565 -- from the deepest node in the tree to the highest node.
4568 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
4569 Cnode := Left_Opnd (Cnode);
4572 -- Now Opnd is the deepest Opnd, and its parents are the concatenation
4573 -- nodes above, so now we process bottom up, doing the operations. We
4574 -- gather a string that is as long as possible up to five operands
4576 -- The outer loop runs more than once if more than one concatenation
4577 -- type is involved.
4580 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
4581 Set_Parent (Opnds, N);
4583 -- The inner loop gathers concatenation operands
4585 Inner : while Cnode /= N
4586 and then Base_Type (Etype (Cnode)) =
4587 Base_Type (Etype (Parent (Cnode)))
4589 Cnode := Parent (Cnode);
4590 Append (Right_Opnd (Cnode), Opnds);
4593 Expand_Concatenate (Cnode, Opnds);
4595 exit Outer when Cnode = N;
4596 Cnode := Parent (Cnode);
4598 end Expand_N_Op_Concat;
4600 ------------------------
4601 -- Expand_N_Op_Divide --
4602 ------------------------
4604 procedure Expand_N_Op_Divide (N : Node_Id) is
4605 Loc : constant Source_Ptr := Sloc (N);
4606 Lopnd : constant Node_Id := Left_Opnd (N);
4607 Ropnd : constant Node_Id := Right_Opnd (N);
4608 Ltyp : constant Entity_Id := Etype (Lopnd);
4609 Rtyp : constant Entity_Id := Etype (Ropnd);
4610 Typ : Entity_Id := Etype (N);
4611 Rknow : constant Boolean := Is_Integer_Type (Typ)
4613 Compile_Time_Known_Value (Ropnd);
4617 Binary_Op_Validity_Checks (N);
4620 Rval := Expr_Value (Ropnd);
4623 -- N / 1 = N for integer types
4625 if Rknow and then Rval = Uint_1 then
4630 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
4631 -- Is_Power_Of_2_For_Shift is set means that we know that our left
4632 -- operand is an unsigned integer, as required for this to work.
4634 if Nkind (Ropnd) = N_Op_Expon
4635 and then Is_Power_Of_2_For_Shift (Ropnd)
4637 -- We cannot do this transformation in configurable run time mode if we
4638 -- have 64-bit -- integers and long shifts are not available.
4642 or else Support_Long_Shifts_On_Target)
4645 Make_Op_Shift_Right (Loc,
4648 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
4649 Analyze_And_Resolve (N, Typ);
4653 -- Do required fixup of universal fixed operation
4655 if Typ = Universal_Fixed then
4656 Fixup_Universal_Fixed_Operation (N);
4660 -- Divisions with fixed-point results
4662 if Is_Fixed_Point_Type (Typ) then
4664 -- No special processing if Treat_Fixed_As_Integer is set, since
4665 -- from a semantic point of view such operations are simply integer
4666 -- operations and will be treated that way.
4668 if not Treat_Fixed_As_Integer (N) then
4669 if Is_Integer_Type (Rtyp) then
4670 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
4672 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
4676 -- Other cases of division of fixed-point operands. Again we exclude the
4677 -- case where Treat_Fixed_As_Integer is set.
4679 elsif (Is_Fixed_Point_Type (Ltyp) or else
4680 Is_Fixed_Point_Type (Rtyp))
4681 and then not Treat_Fixed_As_Integer (N)
4683 if Is_Integer_Type (Typ) then
4684 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
4686 pragma Assert (Is_Floating_Point_Type (Typ));
4687 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
4690 -- Mixed-mode operations can appear in a non-static universal context,
4691 -- in which case the integer argument must be converted explicitly.
4693 elsif Typ = Universal_Real
4694 and then Is_Integer_Type (Rtyp)
4697 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
4699 Analyze_And_Resolve (Ropnd, Universal_Real);
4701 elsif Typ = Universal_Real
4702 and then Is_Integer_Type (Ltyp)
4705 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
4707 Analyze_And_Resolve (Lopnd, Universal_Real);
4709 -- Non-fixed point cases, do integer zero divide and overflow checks
4711 elsif Is_Integer_Type (Typ) then
4712 Apply_Divide_Check (N);
4714 -- Check for 64-bit division available, or long shifts if the divisor
4715 -- is a small power of 2 (since such divides will be converted into
4718 if Esize (Ltyp) > 32
4719 and then not Support_64_Bit_Divides_On_Target
4722 or else not Support_Long_Shifts_On_Target
4723 or else (Rval /= Uint_2 and then
4724 Rval /= Uint_4 and then
4725 Rval /= Uint_8 and then
4726 Rval /= Uint_16 and then
4727 Rval /= Uint_32 and then
4730 Error_Msg_CRT ("64-bit division", N);
4733 -- Deal with Vax_Float
4735 elsif Vax_Float (Typ) then
4736 Expand_Vax_Arith (N);
4739 end Expand_N_Op_Divide;
4741 --------------------
4742 -- Expand_N_Op_Eq --
4743 --------------------
4745 procedure Expand_N_Op_Eq (N : Node_Id) is
4746 Loc : constant Source_Ptr := Sloc (N);
4747 Typ : constant Entity_Id := Etype (N);
4748 Lhs : constant Node_Id := Left_Opnd (N);
4749 Rhs : constant Node_Id := Right_Opnd (N);
4750 Bodies : constant List_Id := New_List;
4751 A_Typ : constant Entity_Id := Etype (Lhs);
4753 Typl : Entity_Id := A_Typ;
4754 Op_Name : Entity_Id;
4757 procedure Build_Equality_Call (Eq : Entity_Id);
4758 -- If a constructed equality exists for the type or for its parent,
4759 -- build and analyze call, adding conversions if the operation is
4762 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
4763 -- Determines whether a type has a subcomponent of an unconstrained
4764 -- Unchecked_Union subtype. Typ is a record type.
4766 -------------------------
4767 -- Build_Equality_Call --
4768 -------------------------
4770 procedure Build_Equality_Call (Eq : Entity_Id) is
4771 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
4772 L_Exp : Node_Id := Relocate_Node (Lhs);
4773 R_Exp : Node_Id := Relocate_Node (Rhs);
4776 if Base_Type (Op_Type) /= Base_Type (A_Typ)
4777 and then not Is_Class_Wide_Type (A_Typ)
4779 L_Exp := OK_Convert_To (Op_Type, L_Exp);
4780 R_Exp := OK_Convert_To (Op_Type, R_Exp);
4783 -- If we have an Unchecked_Union, we need to add the inferred
4784 -- discriminant values as actuals in the function call. At this
4785 -- point, the expansion has determined that both operands have
4786 -- inferable discriminants.
4788 if Is_Unchecked_Union (Op_Type) then
4790 Lhs_Type : constant Node_Id := Etype (L_Exp);
4791 Rhs_Type : constant Node_Id := Etype (R_Exp);
4792 Lhs_Discr_Val : Node_Id;
4793 Rhs_Discr_Val : Node_Id;
4796 -- Per-object constrained selected components require special
4797 -- attention. If the enclosing scope of the component is an
4798 -- Unchecked_Union, we cannot reference its discriminants
4799 -- directly. This is why we use the two extra parameters of
4800 -- the equality function of the enclosing Unchecked_Union.
4802 -- type UU_Type (Discr : Integer := 0) is
4805 -- pragma Unchecked_Union (UU_Type);
4807 -- 1. Unchecked_Union enclosing record:
4809 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
4811 -- Comp : UU_Type (Discr);
4813 -- end Enclosing_UU_Type;
4814 -- pragma Unchecked_Union (Enclosing_UU_Type);
4816 -- Obj1 : Enclosing_UU_Type;
4817 -- Obj2 : Enclosing_UU_Type (1);
4819 -- [. . .] Obj1 = Obj2 [. . .]
4823 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
4825 -- A and B are the formal parameters of the equality function
4826 -- of Enclosing_UU_Type. The function always has two extra
4827 -- formals to capture the inferred discriminant values.
4829 -- 2. Non-Unchecked_Union enclosing record:
4832 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
4835 -- Comp : UU_Type (Discr);
4837 -- end Enclosing_Non_UU_Type;
4839 -- Obj1 : Enclosing_Non_UU_Type;
4840 -- Obj2 : Enclosing_Non_UU_Type (1);
4842 -- ... Obj1 = Obj2 ...
4846 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
4847 -- obj1.discr, obj2.discr)) then
4849 -- In this case we can directly reference the discriminants of
4850 -- the enclosing record.
4854 if Nkind (Lhs) = N_Selected_Component
4855 and then Has_Per_Object_Constraint
4856 (Entity (Selector_Name (Lhs)))
4858 -- Enclosing record is an Unchecked_Union, use formal A
4860 if Is_Unchecked_Union (Scope
4861 (Entity (Selector_Name (Lhs))))
4864 Make_Identifier (Loc,
4867 -- Enclosing record is of a non-Unchecked_Union type, it is
4868 -- possible to reference the discriminant.
4872 Make_Selected_Component (Loc,
4873 Prefix => Prefix (Lhs),
4876 (Get_Discriminant_Value
4877 (First_Discriminant (Lhs_Type),
4879 Stored_Constraint (Lhs_Type))));
4882 -- Comment needed here ???
4885 -- Infer the discriminant value
4889 (Get_Discriminant_Value
4890 (First_Discriminant (Lhs_Type),
4892 Stored_Constraint (Lhs_Type)));
4897 if Nkind (Rhs) = N_Selected_Component
4898 and then Has_Per_Object_Constraint
4899 (Entity (Selector_Name (Rhs)))
4901 if Is_Unchecked_Union
4902 (Scope (Entity (Selector_Name (Rhs))))
4905 Make_Identifier (Loc,
4910 Make_Selected_Component (Loc,
4911 Prefix => Prefix (Rhs),
4913 New_Copy (Get_Discriminant_Value (
4914 First_Discriminant (Rhs_Type),
4916 Stored_Constraint (Rhs_Type))));
4921 New_Copy (Get_Discriminant_Value (
4922 First_Discriminant (Rhs_Type),
4924 Stored_Constraint (Rhs_Type)));
4929 Make_Function_Call (Loc,
4930 Name => New_Reference_To (Eq, Loc),
4931 Parameter_Associations => New_List (
4938 -- Normal case, not an unchecked union
4942 Make_Function_Call (Loc,
4943 Name => New_Reference_To (Eq, Loc),
4944 Parameter_Associations => New_List (L_Exp, R_Exp)));
4947 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
4948 end Build_Equality_Call;
4950 ------------------------------------
4951 -- Has_Unconstrained_UU_Component --
4952 ------------------------------------
4954 function Has_Unconstrained_UU_Component
4955 (Typ : Node_Id) return Boolean
4957 Tdef : constant Node_Id :=
4958 Type_Definition (Declaration_Node (Base_Type (Typ)));
4962 function Component_Is_Unconstrained_UU
4963 (Comp : Node_Id) return Boolean;
4964 -- Determines whether the subtype of the component is an
4965 -- unconstrained Unchecked_Union.
4967 function Variant_Is_Unconstrained_UU
4968 (Variant : Node_Id) return Boolean;
4969 -- Determines whether a component of the variant has an unconstrained
4970 -- Unchecked_Union subtype.
4972 -----------------------------------
4973 -- Component_Is_Unconstrained_UU --
4974 -----------------------------------
4976 function Component_Is_Unconstrained_UU
4977 (Comp : Node_Id) return Boolean
4980 if Nkind (Comp) /= N_Component_Declaration then
4985 Sindic : constant Node_Id :=
4986 Subtype_Indication (Component_Definition (Comp));
4989 -- Unconstrained nominal type. In the case of a constraint
4990 -- present, the node kind would have been N_Subtype_Indication.
4992 if Nkind (Sindic) = N_Identifier then
4993 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
4998 end Component_Is_Unconstrained_UU;
5000 ---------------------------------
5001 -- Variant_Is_Unconstrained_UU --
5002 ---------------------------------
5004 function Variant_Is_Unconstrained_UU
5005 (Variant : Node_Id) return Boolean
5007 Clist : constant Node_Id := Component_List (Variant);
5010 if Is_Empty_List (Component_Items (Clist)) then
5014 -- We only need to test one component
5017 Comp : Node_Id := First (Component_Items (Clist));
5020 while Present (Comp) loop
5021 if Component_Is_Unconstrained_UU (Comp) then
5029 -- None of the components withing the variant were of
5030 -- unconstrained Unchecked_Union type.
5033 end Variant_Is_Unconstrained_UU;
5035 -- Start of processing for Has_Unconstrained_UU_Component
5038 if Null_Present (Tdef) then
5042 Clist := Component_List (Tdef);
5043 Vpart := Variant_Part (Clist);
5045 -- Inspect available components
5047 if Present (Component_Items (Clist)) then
5049 Comp : Node_Id := First (Component_Items (Clist));
5052 while Present (Comp) loop
5054 -- One component is sufficient
5056 if Component_Is_Unconstrained_UU (Comp) then
5065 -- Inspect available components withing variants
5067 if Present (Vpart) then
5069 Variant : Node_Id := First (Variants (Vpart));
5072 while Present (Variant) loop
5074 -- One component within a variant is sufficient
5076 if Variant_Is_Unconstrained_UU (Variant) then
5085 -- Neither the available components, nor the components inside the
5086 -- variant parts were of an unconstrained Unchecked_Union subtype.
5089 end Has_Unconstrained_UU_Component;
5091 -- Start of processing for Expand_N_Op_Eq
5094 Binary_Op_Validity_Checks (N);
5096 if Ekind (Typl) = E_Private_Type then
5097 Typl := Underlying_Type (Typl);
5098 elsif Ekind (Typl) = E_Private_Subtype then
5099 Typl := Underlying_Type (Base_Type (Typl));
5104 -- It may happen in error situations that the underlying type is not
5105 -- set. The error will be detected later, here we just defend the
5112 Typl := Base_Type (Typl);
5114 -- Boolean types (requiring handling of non-standard case)
5116 if Is_Boolean_Type (Typl) then
5117 Adjust_Condition (Left_Opnd (N));
5118 Adjust_Condition (Right_Opnd (N));
5119 Set_Etype (N, Standard_Boolean);
5120 Adjust_Result_Type (N, Typ);
5124 elsif Is_Array_Type (Typl) then
5126 -- If we are doing full validity checking, and it is possible for the
5127 -- array elements to be invalid then expand out array comparisons to
5128 -- make sure that we check the array elements.
5130 if Validity_Check_Operands
5131 and then not Is_Known_Valid (Component_Type (Typl))
5134 Save_Force_Validity_Checks : constant Boolean :=
5135 Force_Validity_Checks;
5137 Force_Validity_Checks := True;
5139 Expand_Array_Equality
5141 Relocate_Node (Lhs),
5142 Relocate_Node (Rhs),
5145 Insert_Actions (N, Bodies);
5146 Analyze_And_Resolve (N, Standard_Boolean);
5147 Force_Validity_Checks := Save_Force_Validity_Checks;
5150 -- Packed case where both operands are known aligned
5152 elsif Is_Bit_Packed_Array (Typl)
5153 and then not Is_Possibly_Unaligned_Object (Lhs)
5154 and then not Is_Possibly_Unaligned_Object (Rhs)
5156 Expand_Packed_Eq (N);
5158 -- Where the component type is elementary we can use a block bit
5159 -- comparison (if supported on the target) exception in the case
5160 -- of floating-point (negative zero issues require element by
5161 -- element comparison), and atomic types (where we must be sure
5162 -- to load elements independently) and possibly unaligned arrays.
5164 elsif Is_Elementary_Type (Component_Type (Typl))
5165 and then not Is_Floating_Point_Type (Component_Type (Typl))
5166 and then not Is_Atomic (Component_Type (Typl))
5167 and then not Is_Possibly_Unaligned_Object (Lhs)
5168 and then not Is_Possibly_Unaligned_Object (Rhs)
5169 and then Support_Composite_Compare_On_Target
5173 -- For composite and floating-point cases, expand equality loop to
5174 -- make sure of using proper comparisons for tagged types, and
5175 -- correctly handling the floating-point case.
5179 Expand_Array_Equality
5181 Relocate_Node (Lhs),
5182 Relocate_Node (Rhs),
5185 Insert_Actions (N, Bodies, Suppress => All_Checks);
5186 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5191 elsif Is_Record_Type (Typl) then
5193 -- For tagged types, use the primitive "="
5195 if Is_Tagged_Type (Typl) then
5197 -- No need to do anything else compiling under restriction
5198 -- No_Dispatching_Calls. During the semantic analysis we
5199 -- already notified such violation.
5201 if Restriction_Active (No_Dispatching_Calls) then
5205 -- If this is derived from an untagged private type completed with
5206 -- a tagged type, it does not have a full view, so we use the
5207 -- primitive operations of the private type. This check should no
5208 -- longer be necessary when these types get their full views???
5210 if Is_Private_Type (A_Typ)
5211 and then not Is_Tagged_Type (A_Typ)
5212 and then Is_Derived_Type (A_Typ)
5213 and then No (Full_View (A_Typ))
5215 -- Search for equality operation, checking that the operands
5216 -- have the same type. Note that we must find a matching entry,
5217 -- or something is very wrong!
5219 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
5221 while Present (Prim) loop
5222 exit when Chars (Node (Prim)) = Name_Op_Eq
5223 and then Etype (First_Formal (Node (Prim))) =
5224 Etype (Next_Formal (First_Formal (Node (Prim))))
5226 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5231 pragma Assert (Present (Prim));
5232 Op_Name := Node (Prim);
5234 -- Find the type's predefined equality or an overriding
5235 -- user- defined equality. The reason for not simply calling
5236 -- Find_Prim_Op here is that there may be a user-defined
5237 -- overloaded equality op that precedes the equality that we want,
5238 -- so we have to explicitly search (e.g., there could be an
5239 -- equality with two different parameter types).
5242 if Is_Class_Wide_Type (Typl) then
5243 Typl := Root_Type (Typl);
5246 Prim := First_Elmt (Primitive_Operations (Typl));
5247 while Present (Prim) loop
5248 exit when Chars (Node (Prim)) = Name_Op_Eq
5249 and then Etype (First_Formal (Node (Prim))) =
5250 Etype (Next_Formal (First_Formal (Node (Prim))))
5252 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5257 pragma Assert (Present (Prim));
5258 Op_Name := Node (Prim);
5261 Build_Equality_Call (Op_Name);
5263 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
5264 -- predefined equality operator for a type which has a subcomponent
5265 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
5267 elsif Has_Unconstrained_UU_Component (Typl) then
5269 Make_Raise_Program_Error (Loc,
5270 Reason => PE_Unchecked_Union_Restriction));
5272 -- Prevent Gigi from generating incorrect code by rewriting the
5273 -- equality as a standard False.
5276 New_Occurrence_Of (Standard_False, Loc));
5278 elsif Is_Unchecked_Union (Typl) then
5280 -- If we can infer the discriminants of the operands, we make a
5281 -- call to the TSS equality function.
5283 if Has_Inferable_Discriminants (Lhs)
5285 Has_Inferable_Discriminants (Rhs)
5288 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5291 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5292 -- the predefined equality operator for an Unchecked_Union type
5293 -- if either of the operands lack inferable discriminants.
5296 Make_Raise_Program_Error (Loc,
5297 Reason => PE_Unchecked_Union_Restriction));
5299 -- Prevent Gigi from generating incorrect code by rewriting
5300 -- the equality as a standard False.
5303 New_Occurrence_Of (Standard_False, Loc));
5307 -- If a type support function is present (for complex cases), use it
5309 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
5311 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5313 -- Otherwise expand the component by component equality. Note that
5314 -- we never use block-bit comparisons for records, because of the
5315 -- problems with gaps. The backend will often be able to recombine
5316 -- the separate comparisons that we generate here.
5319 Remove_Side_Effects (Lhs);
5320 Remove_Side_Effects (Rhs);
5322 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
5324 Insert_Actions (N, Bodies, Suppress => All_Checks);
5325 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5329 -- Test if result is known at compile time
5331 Rewrite_Comparison (N);
5333 -- If we still have comparison for Vax_Float, process it
5335 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
5336 Expand_Vax_Comparison (N);
5341 -----------------------
5342 -- Expand_N_Op_Expon --
5343 -----------------------
5345 procedure Expand_N_Op_Expon (N : Node_Id) is
5346 Loc : constant Source_Ptr := Sloc (N);
5347 Typ : constant Entity_Id := Etype (N);
5348 Rtyp : constant Entity_Id := Root_Type (Typ);
5349 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
5350 Bastyp : constant Node_Id := Etype (Base);
5351 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
5352 Exptyp : constant Entity_Id := Etype (Exp);
5353 Ovflo : constant Boolean := Do_Overflow_Check (N);
5362 Binary_Op_Validity_Checks (N);
5364 -- If either operand is of a private type, then we have the use of an
5365 -- intrinsic operator, and we get rid of the privateness, by using root
5366 -- types of underlying types for the actual operation. Otherwise the
5367 -- private types will cause trouble if we expand multiplications or
5368 -- shifts etc. We also do this transformation if the result type is
5369 -- different from the base type.
5371 if Is_Private_Type (Etype (Base))
5373 Is_Private_Type (Typ)
5375 Is_Private_Type (Exptyp)
5377 Rtyp /= Root_Type (Bastyp)
5380 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
5381 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
5385 Unchecked_Convert_To (Typ,
5387 Left_Opnd => Unchecked_Convert_To (Bt, Base),
5388 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
5389 Analyze_And_Resolve (N, Typ);
5394 -- Test for case of known right argument
5396 if Compile_Time_Known_Value (Exp) then
5397 Expv := Expr_Value (Exp);
5399 -- We only fold small non-negative exponents. You might think we
5400 -- could fold small negative exponents for the real case, but we
5401 -- can't because we are required to raise Constraint_Error for
5402 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
5403 -- See ACVC test C4A012B.
5405 if Expv >= 0 and then Expv <= 4 then
5407 -- X ** 0 = 1 (or 1.0)
5411 -- Call Remove_Side_Effects to ensure that any side effects
5412 -- in the ignored left operand (in particular function calls
5413 -- to user defined functions) are properly executed.
5415 Remove_Side_Effects (Base);
5417 if Ekind (Typ) in Integer_Kind then
5418 Xnode := Make_Integer_Literal (Loc, Intval => 1);
5420 Xnode := Make_Real_Literal (Loc, Ureal_1);
5432 Make_Op_Multiply (Loc,
5433 Left_Opnd => Duplicate_Subexpr (Base),
5434 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
5436 -- X ** 3 = X * X * X
5440 Make_Op_Multiply (Loc,
5442 Make_Op_Multiply (Loc,
5443 Left_Opnd => Duplicate_Subexpr (Base),
5444 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
5445 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
5448 -- En : constant base'type := base * base;
5454 Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
5456 Insert_Actions (N, New_List (
5457 Make_Object_Declaration (Loc,
5458 Defining_Identifier => Temp,
5459 Constant_Present => True,
5460 Object_Definition => New_Reference_To (Typ, Loc),
5462 Make_Op_Multiply (Loc,
5463 Left_Opnd => Duplicate_Subexpr (Base),
5464 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
5467 Make_Op_Multiply (Loc,
5468 Left_Opnd => New_Reference_To (Temp, Loc),
5469 Right_Opnd => New_Reference_To (Temp, Loc));
5473 Analyze_And_Resolve (N, Typ);
5478 -- Case of (2 ** expression) appearing as an argument of an integer
5479 -- multiplication, or as the right argument of a division of a non-
5480 -- negative integer. In such cases we leave the node untouched, setting
5481 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
5482 -- of the higher level node converts it into a shift.
5484 -- Note: this transformation is not applicable for a modular type with
5485 -- a non-binary modulus in the multiplication case, since we get a wrong
5486 -- result if the shift causes an overflow before the modular reduction.
5488 if Nkind (Base) = N_Integer_Literal
5489 and then Intval (Base) = 2
5490 and then Is_Integer_Type (Root_Type (Exptyp))
5491 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
5492 and then Is_Unsigned_Type (Exptyp)
5494 and then Nkind (Parent (N)) in N_Binary_Op
5497 P : constant Node_Id := Parent (N);
5498 L : constant Node_Id := Left_Opnd (P);
5499 R : constant Node_Id := Right_Opnd (P);
5502 if (Nkind (P) = N_Op_Multiply
5503 and then not Non_Binary_Modulus (Typ)
5505 ((Is_Integer_Type (Etype (L)) and then R = N)
5507 (Is_Integer_Type (Etype (R)) and then L = N))
5508 and then not Do_Overflow_Check (P))
5511 (Nkind (P) = N_Op_Divide
5512 and then Is_Integer_Type (Etype (L))
5513 and then Is_Unsigned_Type (Etype (L))
5515 and then not Do_Overflow_Check (P))
5517 Set_Is_Power_Of_2_For_Shift (N);
5523 -- Fall through if exponentiation must be done using a runtime routine
5525 -- First deal with modular case
5527 if Is_Modular_Integer_Type (Rtyp) then
5529 -- Non-binary case, we call the special exponentiation routine for
5530 -- the non-binary case, converting the argument to Long_Long_Integer
5531 -- and passing the modulus value. Then the result is converted back
5532 -- to the base type.
5534 if Non_Binary_Modulus (Rtyp) then
5537 Make_Function_Call (Loc,
5538 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
5539 Parameter_Associations => New_List (
5540 Convert_To (Standard_Integer, Base),
5541 Make_Integer_Literal (Loc, Modulus (Rtyp)),
5544 -- Binary case, in this case, we call one of two routines, either the
5545 -- unsigned integer case, or the unsigned long long integer case,
5546 -- with a final "and" operation to do the required mod.
5549 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
5550 Ent := RTE (RE_Exp_Unsigned);
5552 Ent := RTE (RE_Exp_Long_Long_Unsigned);
5559 Make_Function_Call (Loc,
5560 Name => New_Reference_To (Ent, Loc),
5561 Parameter_Associations => New_List (
5562 Convert_To (Etype (First_Formal (Ent)), Base),
5565 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
5569 -- Common exit point for modular type case
5571 Analyze_And_Resolve (N, Typ);
5574 -- Signed integer cases, done using either Integer or Long_Long_Integer.
5575 -- It is not worth having routines for Short_[Short_]Integer, since for
5576 -- most machines it would not help, and it would generate more code that
5577 -- might need certification when a certified run time is required.
5579 -- In the integer cases, we have two routines, one for when overflow
5580 -- checks are required, and one when they are not required, since there
5581 -- is a real gain in omitting checks on many machines.
5583 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
5584 or else (Rtyp = Base_Type (Standard_Long_Integer)
5586 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
5587 or else (Rtyp = Universal_Integer)
5589 Etyp := Standard_Long_Long_Integer;
5592 Rent := RE_Exp_Long_Long_Integer;
5594 Rent := RE_Exn_Long_Long_Integer;
5597 elsif Is_Signed_Integer_Type (Rtyp) then
5598 Etyp := Standard_Integer;
5601 Rent := RE_Exp_Integer;
5603 Rent := RE_Exn_Integer;
5606 -- Floating-point cases, always done using Long_Long_Float. We do not
5607 -- need separate routines for the overflow case here, since in the case
5608 -- of floating-point, we generate infinities anyway as a rule (either
5609 -- that or we automatically trap overflow), and if there is an infinity
5610 -- generated and a range check is required, the check will fail anyway.
5613 pragma Assert (Is_Floating_Point_Type (Rtyp));
5614 Etyp := Standard_Long_Long_Float;
5615 Rent := RE_Exn_Long_Long_Float;
5618 -- Common processing for integer cases and floating-point cases.
5619 -- If we are in the right type, we can call runtime routine directly
5622 and then Rtyp /= Universal_Integer
5623 and then Rtyp /= Universal_Real
5626 Make_Function_Call (Loc,
5627 Name => New_Reference_To (RTE (Rent), Loc),
5628 Parameter_Associations => New_List (Base, Exp)));
5630 -- Otherwise we have to introduce conversions (conversions are also
5631 -- required in the universal cases, since the runtime routine is
5632 -- typed using one of the standard types.
5637 Make_Function_Call (Loc,
5638 Name => New_Reference_To (RTE (Rent), Loc),
5639 Parameter_Associations => New_List (
5640 Convert_To (Etyp, Base),
5644 Analyze_And_Resolve (N, Typ);
5648 when RE_Not_Available =>
5650 end Expand_N_Op_Expon;
5652 --------------------
5653 -- Expand_N_Op_Ge --
5654 --------------------
5656 procedure Expand_N_Op_Ge (N : Node_Id) is
5657 Typ : constant Entity_Id := Etype (N);
5658 Op1 : constant Node_Id := Left_Opnd (N);
5659 Op2 : constant Node_Id := Right_Opnd (N);
5660 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
5663 Binary_Op_Validity_Checks (N);
5665 if Is_Array_Type (Typ1) then
5666 Expand_Array_Comparison (N);
5670 if Is_Boolean_Type (Typ1) then
5671 Adjust_Condition (Op1);
5672 Adjust_Condition (Op2);
5673 Set_Etype (N, Standard_Boolean);
5674 Adjust_Result_Type (N, Typ);
5677 Rewrite_Comparison (N);
5679 -- If we still have comparison, and Vax_Float type, process it
5681 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
5682 Expand_Vax_Comparison (N);
5687 --------------------
5688 -- Expand_N_Op_Gt --
5689 --------------------
5691 procedure Expand_N_Op_Gt (N : Node_Id) is
5692 Typ : constant Entity_Id := Etype (N);
5693 Op1 : constant Node_Id := Left_Opnd (N);
5694 Op2 : constant Node_Id := Right_Opnd (N);
5695 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
5698 Binary_Op_Validity_Checks (N);
5700 if Is_Array_Type (Typ1) then
5701 Expand_Array_Comparison (N);
5705 if Is_Boolean_Type (Typ1) then
5706 Adjust_Condition (Op1);
5707 Adjust_Condition (Op2);
5708 Set_Etype (N, Standard_Boolean);
5709 Adjust_Result_Type (N, Typ);
5712 Rewrite_Comparison (N);
5714 -- If we still have comparison, and Vax_Float type, process it
5716 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
5717 Expand_Vax_Comparison (N);
5722 --------------------
5723 -- Expand_N_Op_Le --
5724 --------------------
5726 procedure Expand_N_Op_Le (N : Node_Id) is
5727 Typ : constant Entity_Id := Etype (N);
5728 Op1 : constant Node_Id := Left_Opnd (N);
5729 Op2 : constant Node_Id := Right_Opnd (N);
5730 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
5733 Binary_Op_Validity_Checks (N);
5735 if Is_Array_Type (Typ1) then
5736 Expand_Array_Comparison (N);
5740 if Is_Boolean_Type (Typ1) then
5741 Adjust_Condition (Op1);
5742 Adjust_Condition (Op2);
5743 Set_Etype (N, Standard_Boolean);
5744 Adjust_Result_Type (N, Typ);
5747 Rewrite_Comparison (N);
5749 -- If we still have comparison, and Vax_Float type, process it
5751 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
5752 Expand_Vax_Comparison (N);
5757 --------------------
5758 -- Expand_N_Op_Lt --
5759 --------------------
5761 procedure Expand_N_Op_Lt (N : Node_Id) is
5762 Typ : constant Entity_Id := Etype (N);
5763 Op1 : constant Node_Id := Left_Opnd (N);
5764 Op2 : constant Node_Id := Right_Opnd (N);
5765 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
5768 Binary_Op_Validity_Checks (N);
5770 if Is_Array_Type (Typ1) then
5771 Expand_Array_Comparison (N);
5775 if Is_Boolean_Type (Typ1) then
5776 Adjust_Condition (Op1);
5777 Adjust_Condition (Op2);
5778 Set_Etype (N, Standard_Boolean);
5779 Adjust_Result_Type (N, Typ);
5782 Rewrite_Comparison (N);
5784 -- If we still have comparison, and Vax_Float type, process it
5786 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
5787 Expand_Vax_Comparison (N);
5792 -----------------------
5793 -- Expand_N_Op_Minus --
5794 -----------------------
5796 procedure Expand_N_Op_Minus (N : Node_Id) is
5797 Loc : constant Source_Ptr := Sloc (N);
5798 Typ : constant Entity_Id := Etype (N);
5801 Unary_Op_Validity_Checks (N);
5803 if not Backend_Overflow_Checks_On_Target
5804 and then Is_Signed_Integer_Type (Etype (N))
5805 and then Do_Overflow_Check (N)
5807 -- Software overflow checking expands -expr into (0 - expr)
5810 Make_Op_Subtract (Loc,
5811 Left_Opnd => Make_Integer_Literal (Loc, 0),
5812 Right_Opnd => Right_Opnd (N)));
5814 Analyze_And_Resolve (N, Typ);
5816 -- Vax floating-point types case
5818 elsif Vax_Float (Etype (N)) then
5819 Expand_Vax_Arith (N);
5821 end Expand_N_Op_Minus;
5823 ---------------------
5824 -- Expand_N_Op_Mod --
5825 ---------------------
5827 procedure Expand_N_Op_Mod (N : Node_Id) is
5828 Loc : constant Source_Ptr := Sloc (N);
5829 Typ : constant Entity_Id := Etype (N);
5830 Left : constant Node_Id := Left_Opnd (N);
5831 Right : constant Node_Id := Right_Opnd (N);
5832 DOC : constant Boolean := Do_Overflow_Check (N);
5833 DDC : constant Boolean := Do_Division_Check (N);
5843 pragma Warnings (Off, Lhi);
5846 Binary_Op_Validity_Checks (N);
5848 Determine_Range (Right, ROK, Rlo, Rhi);
5849 Determine_Range (Left, LOK, Llo, Lhi);
5851 -- Convert mod to rem if operands are known non-negative. We do this
5852 -- since it is quite likely that this will improve the quality of code,
5853 -- (the operation now corresponds to the hardware remainder), and it
5854 -- does not seem likely that it could be harmful.
5856 if LOK and then Llo >= 0
5858 ROK and then Rlo >= 0
5861 Make_Op_Rem (Sloc (N),
5862 Left_Opnd => Left_Opnd (N),
5863 Right_Opnd => Right_Opnd (N)));
5865 -- Instead of reanalyzing the node we do the analysis manually. This
5866 -- avoids anomalies when the replacement is done in an instance and
5867 -- is epsilon more efficient.
5869 Set_Entity (N, Standard_Entity (S_Op_Rem));
5871 Set_Do_Overflow_Check (N, DOC);
5872 Set_Do_Division_Check (N, DDC);
5873 Expand_N_Op_Rem (N);
5876 -- Otherwise, normal mod processing
5879 if Is_Integer_Type (Etype (N)) then
5880 Apply_Divide_Check (N);
5883 -- Apply optimization x mod 1 = 0. We don't really need that with
5884 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
5885 -- certainly harmless.
5887 if Is_Integer_Type (Etype (N))
5888 and then Compile_Time_Known_Value (Right)
5889 and then Expr_Value (Right) = Uint_1
5891 -- Call Remove_Side_Effects to ensure that any side effects in
5892 -- the ignored left operand (in particular function calls to
5893 -- user defined functions) are properly executed.
5895 Remove_Side_Effects (Left);
5897 Rewrite (N, Make_Integer_Literal (Loc, 0));
5898 Analyze_And_Resolve (N, Typ);
5902 -- Deal with annoying case of largest negative number remainder
5903 -- minus one. Gigi does not handle this case correctly, because
5904 -- it generates a divide instruction which may trap in this case.
5906 -- In fact the check is quite easy, if the right operand is -1, then
5907 -- the mod value is always 0, and we can just ignore the left operand
5908 -- completely in this case.
5910 -- The operand type may be private (e.g. in the expansion of an
5911 -- intrinsic operation) so we must use the underlying type to get the
5912 -- bounds, and convert the literals explicitly.
5916 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
5918 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
5920 ((not LOK) or else (Llo = LLB))
5923 Make_Conditional_Expression (Loc,
5924 Expressions => New_List (
5926 Left_Opnd => Duplicate_Subexpr (Right),
5928 Unchecked_Convert_To (Typ,
5929 Make_Integer_Literal (Loc, -1))),
5930 Unchecked_Convert_To (Typ,
5931 Make_Integer_Literal (Loc, Uint_0)),
5932 Relocate_Node (N))));
5934 Set_Analyzed (Next (Next (First (Expressions (N)))));
5935 Analyze_And_Resolve (N, Typ);
5938 end Expand_N_Op_Mod;
5940 --------------------------
5941 -- Expand_N_Op_Multiply --
5942 --------------------------
5944 procedure Expand_N_Op_Multiply (N : Node_Id) is
5945 Loc : constant Source_Ptr := Sloc (N);
5946 Lop : constant Node_Id := Left_Opnd (N);
5947 Rop : constant Node_Id := Right_Opnd (N);
5949 Lp2 : constant Boolean :=
5950 Nkind (Lop) = N_Op_Expon
5951 and then Is_Power_Of_2_For_Shift (Lop);
5953 Rp2 : constant Boolean :=
5954 Nkind (Rop) = N_Op_Expon
5955 and then Is_Power_Of_2_For_Shift (Rop);
5957 Ltyp : constant Entity_Id := Etype (Lop);
5958 Rtyp : constant Entity_Id := Etype (Rop);
5959 Typ : Entity_Id := Etype (N);
5962 Binary_Op_Validity_Checks (N);
5964 -- Special optimizations for integer types
5966 if Is_Integer_Type (Typ) then
5968 -- N * 0 = 0 for integer types
5970 if Compile_Time_Known_Value (Rop)
5971 and then Expr_Value (Rop) = Uint_0
5973 -- Call Remove_Side_Effects to ensure that any side effects in
5974 -- the ignored left operand (in particular function calls to
5975 -- user defined functions) are properly executed.
5977 Remove_Side_Effects (Lop);
5979 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
5980 Analyze_And_Resolve (N, Typ);
5984 -- Similar handling for 0 * N = 0
5986 if Compile_Time_Known_Value (Lop)
5987 and then Expr_Value (Lop) = Uint_0
5989 Remove_Side_Effects (Rop);
5990 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
5991 Analyze_And_Resolve (N, Typ);
5995 -- N * 1 = 1 * N = N for integer types
5997 -- This optimisation is not done if we are going to
5998 -- rewrite the product 1 * 2 ** N to a shift.
6000 if Compile_Time_Known_Value (Rop)
6001 and then Expr_Value (Rop) = Uint_1
6007 elsif Compile_Time_Known_Value (Lop)
6008 and then Expr_Value (Lop) = Uint_1
6016 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
6017 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6018 -- operand is an integer, as required for this to work.
6023 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
6027 Left_Opnd => Make_Integer_Literal (Loc, 2),
6030 Left_Opnd => Right_Opnd (Lop),
6031 Right_Opnd => Right_Opnd (Rop))));
6032 Analyze_And_Resolve (N, Typ);
6037 Make_Op_Shift_Left (Loc,
6040 Convert_To (Standard_Natural, Right_Opnd (Rop))));
6041 Analyze_And_Resolve (N, Typ);
6045 -- Same processing for the operands the other way round
6049 Make_Op_Shift_Left (Loc,
6052 Convert_To (Standard_Natural, Right_Opnd (Lop))));
6053 Analyze_And_Resolve (N, Typ);
6057 -- Do required fixup of universal fixed operation
6059 if Typ = Universal_Fixed then
6060 Fixup_Universal_Fixed_Operation (N);
6064 -- Multiplications with fixed-point results
6066 if Is_Fixed_Point_Type (Typ) then
6068 -- No special processing if Treat_Fixed_As_Integer is set, since from
6069 -- a semantic point of view such operations are simply integer
6070 -- operations and will be treated that way.
6072 if not Treat_Fixed_As_Integer (N) then
6074 -- Case of fixed * integer => fixed
6076 if Is_Integer_Type (Rtyp) then
6077 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
6079 -- Case of integer * fixed => fixed
6081 elsif Is_Integer_Type (Ltyp) then
6082 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
6084 -- Case of fixed * fixed => fixed
6087 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
6091 -- Other cases of multiplication of fixed-point operands. Again we
6092 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
6094 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6095 and then not Treat_Fixed_As_Integer (N)
6097 if Is_Integer_Type (Typ) then
6098 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
6100 pragma Assert (Is_Floating_Point_Type (Typ));
6101 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
6104 -- Mixed-mode operations can appear in a non-static universal context,
6105 -- in which case the integer argument must be converted explicitly.
6107 elsif Typ = Universal_Real
6108 and then Is_Integer_Type (Rtyp)
6110 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
6112 Analyze_And_Resolve (Rop, Universal_Real);
6114 elsif Typ = Universal_Real
6115 and then Is_Integer_Type (Ltyp)
6117 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
6119 Analyze_And_Resolve (Lop, Universal_Real);
6121 -- Non-fixed point cases, check software overflow checking required
6123 elsif Is_Signed_Integer_Type (Etype (N)) then
6124 Apply_Arithmetic_Overflow_Check (N);
6126 -- Deal with VAX float case
6128 elsif Vax_Float (Typ) then
6129 Expand_Vax_Arith (N);
6132 end Expand_N_Op_Multiply;
6134 --------------------
6135 -- Expand_N_Op_Ne --
6136 --------------------
6138 procedure Expand_N_Op_Ne (N : Node_Id) is
6139 Typ : constant Entity_Id := Etype (Left_Opnd (N));
6142 -- Case of elementary type with standard operator
6144 if Is_Elementary_Type (Typ)
6145 and then Sloc (Entity (N)) = Standard_Location
6147 Binary_Op_Validity_Checks (N);
6149 -- Boolean types (requiring handling of non-standard case)
6151 if Is_Boolean_Type (Typ) then
6152 Adjust_Condition (Left_Opnd (N));
6153 Adjust_Condition (Right_Opnd (N));
6154 Set_Etype (N, Standard_Boolean);
6155 Adjust_Result_Type (N, Typ);
6158 Rewrite_Comparison (N);
6160 -- If we still have comparison for Vax_Float, process it
6162 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
6163 Expand_Vax_Comparison (N);
6167 -- For all cases other than elementary types, we rewrite node as the
6168 -- negation of an equality operation, and reanalyze. The equality to be
6169 -- used is defined in the same scope and has the same signature. This
6170 -- signature must be set explicitly since in an instance it may not have
6171 -- the same visibility as in the generic unit. This avoids duplicating
6172 -- or factoring the complex code for record/array equality tests etc.
6176 Loc : constant Source_Ptr := Sloc (N);
6178 Ne : constant Entity_Id := Entity (N);
6181 Binary_Op_Validity_Checks (N);
6187 Left_Opnd => Left_Opnd (N),
6188 Right_Opnd => Right_Opnd (N)));
6189 Set_Paren_Count (Right_Opnd (Neg), 1);
6191 if Scope (Ne) /= Standard_Standard then
6192 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
6195 -- For navigation purposes, the inequality is treated as an
6196 -- implicit reference to the corresponding equality. Preserve the
6197 -- Comes_From_ source flag so that the proper Xref entry is
6200 Preserve_Comes_From_Source (Neg, N);
6201 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
6203 Analyze_And_Resolve (N, Standard_Boolean);
6208 ---------------------
6209 -- Expand_N_Op_Not --
6210 ---------------------
6212 -- If the argument is other than a Boolean array type, there is no special
6213 -- expansion required.
6215 -- For the packed case, we call the special routine in Exp_Pakd, except
6216 -- that if the component size is greater than one, we use the standard
6217 -- routine generating a gruesome loop (it is so peculiar to have packed
6218 -- arrays with non-standard Boolean representations anyway, so it does not
6219 -- matter that we do not handle this case efficiently).
6221 -- For the unpacked case (and for the special packed case where we have non
6222 -- standard Booleans, as discussed above), we generate and insert into the
6223 -- tree the following function definition:
6225 -- function Nnnn (A : arr) is
6228 -- for J in a'range loop
6229 -- B (J) := not A (J);
6234 -- Here arr is the actual subtype of the parameter (and hence always
6235 -- constrained). Then we replace the not with a call to this function.
6237 procedure Expand_N_Op_Not (N : Node_Id) is
6238 Loc : constant Source_Ptr := Sloc (N);
6239 Typ : constant Entity_Id := Etype (N);
6248 Func_Name : Entity_Id;
6249 Loop_Statement : Node_Id;
6252 Unary_Op_Validity_Checks (N);
6254 -- For boolean operand, deal with non-standard booleans
6256 if Is_Boolean_Type (Typ) then
6257 Adjust_Condition (Right_Opnd (N));
6258 Set_Etype (N, Standard_Boolean);
6259 Adjust_Result_Type (N, Typ);
6263 -- Only array types need any other processing
6265 if not Is_Array_Type (Typ) then
6269 -- Case of array operand. If bit packed with a component size of 1,
6270 -- handle it in Exp_Pakd if the operand is known to be aligned.
6272 if Is_Bit_Packed_Array (Typ)
6273 and then Component_Size (Typ) = 1
6274 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
6276 Expand_Packed_Not (N);
6280 -- Case of array operand which is not bit-packed. If the context is
6281 -- a safe assignment, call in-place operation, If context is a larger
6282 -- boolean expression in the context of a safe assignment, expansion is
6283 -- done by enclosing operation.
6285 Opnd := Relocate_Node (Right_Opnd (N));
6286 Convert_To_Actual_Subtype (Opnd);
6287 Arr := Etype (Opnd);
6288 Ensure_Defined (Arr, N);
6289 Silly_Boolean_Array_Not_Test (N, Arr);
6291 if Nkind (Parent (N)) = N_Assignment_Statement then
6292 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
6293 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
6296 -- Special case the negation of a binary operation
6298 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
6299 and then Safe_In_Place_Array_Op
6300 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
6302 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
6306 elsif Nkind (Parent (N)) in N_Binary_Op
6307 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
6310 Op1 : constant Node_Id := Left_Opnd (Parent (N));
6311 Op2 : constant Node_Id := Right_Opnd (Parent (N));
6312 Lhs : constant Node_Id := Name (Parent (Parent (N)));
6315 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
6317 and then Nkind (Op2) = N_Op_Not
6319 -- (not A) op (not B) can be reduced to a single call
6324 and then Nkind (Parent (N)) = N_Op_Xor
6326 -- A xor (not B) can also be special-cased
6334 A := Make_Defining_Identifier (Loc, Name_uA);
6335 B := Make_Defining_Identifier (Loc, Name_uB);
6336 J := Make_Defining_Identifier (Loc, Name_uJ);
6339 Make_Indexed_Component (Loc,
6340 Prefix => New_Reference_To (A, Loc),
6341 Expressions => New_List (New_Reference_To (J, Loc)));
6344 Make_Indexed_Component (Loc,
6345 Prefix => New_Reference_To (B, Loc),
6346 Expressions => New_List (New_Reference_To (J, Loc)));
6349 Make_Implicit_Loop_Statement (N,
6350 Identifier => Empty,
6353 Make_Iteration_Scheme (Loc,
6354 Loop_Parameter_Specification =>
6355 Make_Loop_Parameter_Specification (Loc,
6356 Defining_Identifier => J,
6357 Discrete_Subtype_Definition =>
6358 Make_Attribute_Reference (Loc,
6359 Prefix => Make_Identifier (Loc, Chars (A)),
6360 Attribute_Name => Name_Range))),
6362 Statements => New_List (
6363 Make_Assignment_Statement (Loc,
6365 Expression => Make_Op_Not (Loc, A_J))));
6367 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('N'));
6368 Set_Is_Inlined (Func_Name);
6371 Make_Subprogram_Body (Loc,
6373 Make_Function_Specification (Loc,
6374 Defining_Unit_Name => Func_Name,
6375 Parameter_Specifications => New_List (
6376 Make_Parameter_Specification (Loc,
6377 Defining_Identifier => A,
6378 Parameter_Type => New_Reference_To (Typ, Loc))),
6379 Result_Definition => New_Reference_To (Typ, Loc)),
6381 Declarations => New_List (
6382 Make_Object_Declaration (Loc,
6383 Defining_Identifier => B,
6384 Object_Definition => New_Reference_To (Arr, Loc))),
6386 Handled_Statement_Sequence =>
6387 Make_Handled_Sequence_Of_Statements (Loc,
6388 Statements => New_List (
6390 Make_Simple_Return_Statement (Loc,
6392 Make_Identifier (Loc, Chars (B)))))));
6395 Make_Function_Call (Loc,
6396 Name => New_Reference_To (Func_Name, Loc),
6397 Parameter_Associations => New_List (Opnd)));
6399 Analyze_And_Resolve (N, Typ);
6400 end Expand_N_Op_Not;
6402 --------------------
6403 -- Expand_N_Op_Or --
6404 --------------------
6406 procedure Expand_N_Op_Or (N : Node_Id) is
6407 Typ : constant Entity_Id := Etype (N);
6410 Binary_Op_Validity_Checks (N);
6412 if Is_Array_Type (Etype (N)) then
6413 Expand_Boolean_Operator (N);
6415 elsif Is_Boolean_Type (Etype (N)) then
6416 Adjust_Condition (Left_Opnd (N));
6417 Adjust_Condition (Right_Opnd (N));
6418 Set_Etype (N, Standard_Boolean);
6419 Adjust_Result_Type (N, Typ);
6423 ----------------------
6424 -- Expand_N_Op_Plus --
6425 ----------------------
6427 procedure Expand_N_Op_Plus (N : Node_Id) is
6429 Unary_Op_Validity_Checks (N);
6430 end Expand_N_Op_Plus;
6432 ---------------------
6433 -- Expand_N_Op_Rem --
6434 ---------------------
6436 procedure Expand_N_Op_Rem (N : Node_Id) is
6437 Loc : constant Source_Ptr := Sloc (N);
6438 Typ : constant Entity_Id := Etype (N);
6440 Left : constant Node_Id := Left_Opnd (N);
6441 Right : constant Node_Id := Right_Opnd (N);
6451 pragma Warnings (Off, Lhi);
6454 Binary_Op_Validity_Checks (N);
6456 if Is_Integer_Type (Etype (N)) then
6457 Apply_Divide_Check (N);
6460 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
6461 -- but it is useful with other back ends (e.g. AAMP), and is certainly
6464 if Is_Integer_Type (Etype (N))
6465 and then Compile_Time_Known_Value (Right)
6466 and then Expr_Value (Right) = Uint_1
6468 -- Call Remove_Side_Effects to ensure that any side effects in the
6469 -- ignored left operand (in particular function calls to user defined
6470 -- functions) are properly executed.
6472 Remove_Side_Effects (Left);
6474 Rewrite (N, Make_Integer_Literal (Loc, 0));
6475 Analyze_And_Resolve (N, Typ);
6479 -- Deal with annoying case of largest negative number remainder minus
6480 -- one. Gigi does not handle this case correctly, because it generates
6481 -- a divide instruction which may trap in this case.
6483 -- In fact the check is quite easy, if the right operand is -1, then
6484 -- the remainder is always 0, and we can just ignore the left operand
6485 -- completely in this case.
6487 Determine_Range (Right, ROK, Rlo, Rhi);
6488 Determine_Range (Left, LOK, Llo, Lhi);
6490 -- The operand type may be private (e.g. in the expansion of an
6491 -- intrinsic operation) so we must use the underlying type to get the
6492 -- bounds, and convert the literals explicitly.
6496 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
6498 -- Now perform the test, generating code only if needed
6500 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
6502 ((not LOK) or else (Llo = LLB))
6505 Make_Conditional_Expression (Loc,
6506 Expressions => New_List (
6508 Left_Opnd => Duplicate_Subexpr (Right),
6510 Unchecked_Convert_To (Typ,
6511 Make_Integer_Literal (Loc, -1))),
6513 Unchecked_Convert_To (Typ,
6514 Make_Integer_Literal (Loc, Uint_0)),
6516 Relocate_Node (N))));
6518 Set_Analyzed (Next (Next (First (Expressions (N)))));
6519 Analyze_And_Resolve (N, Typ);
6521 end Expand_N_Op_Rem;
6523 -----------------------------
6524 -- Expand_N_Op_Rotate_Left --
6525 -----------------------------
6527 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
6529 Binary_Op_Validity_Checks (N);
6530 end Expand_N_Op_Rotate_Left;
6532 ------------------------------
6533 -- Expand_N_Op_Rotate_Right --
6534 ------------------------------
6536 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
6538 Binary_Op_Validity_Checks (N);
6539 end Expand_N_Op_Rotate_Right;
6541 ----------------------------
6542 -- Expand_N_Op_Shift_Left --
6543 ----------------------------
6545 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
6547 Binary_Op_Validity_Checks (N);
6548 end Expand_N_Op_Shift_Left;
6550 -----------------------------
6551 -- Expand_N_Op_Shift_Right --
6552 -----------------------------
6554 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
6556 Binary_Op_Validity_Checks (N);
6557 end Expand_N_Op_Shift_Right;
6559 ----------------------------------------
6560 -- Expand_N_Op_Shift_Right_Arithmetic --
6561 ----------------------------------------
6563 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
6565 Binary_Op_Validity_Checks (N);
6566 end Expand_N_Op_Shift_Right_Arithmetic;
6568 --------------------------
6569 -- Expand_N_Op_Subtract --
6570 --------------------------
6572 procedure Expand_N_Op_Subtract (N : Node_Id) is
6573 Typ : constant Entity_Id := Etype (N);
6576 Binary_Op_Validity_Checks (N);
6578 -- N - 0 = N for integer types
6580 if Is_Integer_Type (Typ)
6581 and then Compile_Time_Known_Value (Right_Opnd (N))
6582 and then Expr_Value (Right_Opnd (N)) = 0
6584 Rewrite (N, Left_Opnd (N));
6588 -- Arithmetic overflow checks for signed integer/fixed point types
6590 if Is_Signed_Integer_Type (Typ)
6591 or else Is_Fixed_Point_Type (Typ)
6593 Apply_Arithmetic_Overflow_Check (N);
6595 -- Vax floating-point types case
6597 elsif Vax_Float (Typ) then
6598 Expand_Vax_Arith (N);
6600 end Expand_N_Op_Subtract;
6602 ---------------------
6603 -- Expand_N_Op_Xor --
6604 ---------------------
6606 procedure Expand_N_Op_Xor (N : Node_Id) is
6607 Typ : constant Entity_Id := Etype (N);
6610 Binary_Op_Validity_Checks (N);
6612 if Is_Array_Type (Etype (N)) then
6613 Expand_Boolean_Operator (N);
6615 elsif Is_Boolean_Type (Etype (N)) then
6616 Adjust_Condition (Left_Opnd (N));
6617 Adjust_Condition (Right_Opnd (N));
6618 Set_Etype (N, Standard_Boolean);
6619 Adjust_Result_Type (N, Typ);
6621 end Expand_N_Op_Xor;
6623 ----------------------
6624 -- Expand_N_Or_Else --
6625 ----------------------
6627 -- Expand into conditional expression if Actions present, and also
6628 -- deal with optimizing case of arguments being True or False.
6630 procedure Expand_N_Or_Else (N : Node_Id) is
6631 Loc : constant Source_Ptr := Sloc (N);
6632 Typ : constant Entity_Id := Etype (N);
6633 Left : constant Node_Id := Left_Opnd (N);
6634 Right : constant Node_Id := Right_Opnd (N);
6638 -- Deal with non-standard booleans
6640 if Is_Boolean_Type (Typ) then
6641 Adjust_Condition (Left);
6642 Adjust_Condition (Right);
6643 Set_Etype (N, Standard_Boolean);
6646 -- Check for cases where left argument is known to be True or False
6648 if Compile_Time_Known_Value (Left) then
6650 -- If left argument is False, change (False or else Right) to Right.
6651 -- Any actions associated with Right will be executed unconditionally
6652 -- and can thus be inserted into the tree unconditionally.
6654 if Expr_Value_E (Left) = Standard_False then
6655 if Present (Actions (N)) then
6656 Insert_Actions (N, Actions (N));
6661 -- If left argument is True, change (True and then Right) to True. In
6662 -- this case we can forget the actions associated with Right, since
6663 -- they will never be executed.
6665 else pragma Assert (Expr_Value_E (Left) = Standard_True);
6666 Kill_Dead_Code (Right);
6667 Kill_Dead_Code (Actions (N));
6668 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
6671 Adjust_Result_Type (N, Typ);
6675 -- If Actions are present, we expand
6677 -- left or else right
6681 -- if left then True else right end
6683 -- with the actions becoming the Else_Actions of the conditional
6684 -- expression. This conditional expression is then further expanded
6685 -- (and will eventually disappear)
6687 if Present (Actions (N)) then
6688 Actlist := Actions (N);
6690 Make_Conditional_Expression (Loc,
6691 Expressions => New_List (
6693 New_Occurrence_Of (Standard_True, Loc),
6696 Set_Else_Actions (N, Actlist);
6697 Analyze_And_Resolve (N, Standard_Boolean);
6698 Adjust_Result_Type (N, Typ);
6702 -- No actions present, check for cases of right argument True/False
6704 if Compile_Time_Known_Value (Right) then
6706 -- Change (Left or else False) to Left. Note that we know there are
6707 -- no actions associated with the True operand, since we just checked
6708 -- for this case above.
6710 if Expr_Value_E (Right) = Standard_False then
6713 -- Change (Left or else True) to True, making sure to preserve any
6714 -- side effects associated with the Left operand.
6716 else pragma Assert (Expr_Value_E (Right) = Standard_True);
6717 Remove_Side_Effects (Left);
6719 (N, New_Occurrence_Of (Standard_True, Loc));
6723 Adjust_Result_Type (N, Typ);
6724 end Expand_N_Or_Else;
6726 -----------------------------------
6727 -- Expand_N_Qualified_Expression --
6728 -----------------------------------
6730 procedure Expand_N_Qualified_Expression (N : Node_Id) is
6731 Operand : constant Node_Id := Expression (N);
6732 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
6735 -- Do validity check if validity checking operands
6737 if Validity_Checks_On
6738 and then Validity_Check_Operands
6740 Ensure_Valid (Operand);
6743 -- Apply possible constraint check
6745 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
6746 end Expand_N_Qualified_Expression;
6748 ---------------------------------
6749 -- Expand_N_Selected_Component --
6750 ---------------------------------
6752 -- If the selector is a discriminant of a concurrent object, rewrite the
6753 -- prefix to denote the corresponding record type.
6755 procedure Expand_N_Selected_Component (N : Node_Id) is
6756 Loc : constant Source_Ptr := Sloc (N);
6757 Par : constant Node_Id := Parent (N);
6758 P : constant Node_Id := Prefix (N);
6759 Ptyp : Entity_Id := Underlying_Type (Etype (P));
6764 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
6765 -- Gigi needs a temporary for prefixes that depend on a discriminant,
6766 -- unless the context of an assignment can provide size information.
6767 -- Don't we have a general routine that does this???
6769 -----------------------
6770 -- In_Left_Hand_Side --
6771 -----------------------
6773 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
6775 return (Nkind (Parent (Comp)) = N_Assignment_Statement
6776 and then Comp = Name (Parent (Comp)))
6777 or else (Present (Parent (Comp))
6778 and then Nkind (Parent (Comp)) in N_Subexpr
6779 and then In_Left_Hand_Side (Parent (Comp)));
6780 end In_Left_Hand_Side;
6782 -- Start of processing for Expand_N_Selected_Component
6785 -- Insert explicit dereference if required
6787 if Is_Access_Type (Ptyp) then
6788 Insert_Explicit_Dereference (P);
6789 Analyze_And_Resolve (P, Designated_Type (Ptyp));
6791 if Ekind (Etype (P)) = E_Private_Subtype
6792 and then Is_For_Access_Subtype (Etype (P))
6794 Set_Etype (P, Base_Type (Etype (P)));
6800 -- Deal with discriminant check required
6802 if Do_Discriminant_Check (N) then
6804 -- Present the discriminant checking function to the backend, so that
6805 -- it can inline the call to the function.
6808 (Discriminant_Checking_Func
6809 (Original_Record_Component (Entity (Selector_Name (N)))));
6811 -- Now reset the flag and generate the call
6813 Set_Do_Discriminant_Check (N, False);
6814 Generate_Discriminant_Check (N);
6817 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6818 -- function, then additional actuals must be passed.
6820 if Ada_Version >= Ada_05
6821 and then Is_Build_In_Place_Function_Call (P)
6823 Make_Build_In_Place_Call_In_Anonymous_Context (P);
6826 -- Gigi cannot handle unchecked conversions that are the prefix of a
6827 -- selected component with discriminants. This must be checked during
6828 -- expansion, because during analysis the type of the selector is not
6829 -- known at the point the prefix is analyzed. If the conversion is the
6830 -- target of an assignment, then we cannot force the evaluation.
6832 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
6833 and then Has_Discriminants (Etype (N))
6834 and then not In_Left_Hand_Side (N)
6836 Force_Evaluation (Prefix (N));
6839 -- Remaining processing applies only if selector is a discriminant
6841 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
6843 -- If the selector is a discriminant of a constrained record type,
6844 -- we may be able to rewrite the expression with the actual value
6845 -- of the discriminant, a useful optimization in some cases.
6847 if Is_Record_Type (Ptyp)
6848 and then Has_Discriminants (Ptyp)
6849 and then Is_Constrained (Ptyp)
6851 -- Do this optimization for discrete types only, and not for
6852 -- access types (access discriminants get us into trouble!)
6854 if not Is_Discrete_Type (Etype (N)) then
6857 -- Don't do this on the left hand of an assignment statement.
6858 -- Normally one would think that references like this would
6859 -- not occur, but they do in generated code, and mean that
6860 -- we really do want to assign the discriminant!
6862 elsif Nkind (Par) = N_Assignment_Statement
6863 and then Name (Par) = N
6867 -- Don't do this optimization for the prefix of an attribute or
6868 -- the operand of an object renaming declaration since these are
6869 -- contexts where we do not want the value anyway.
6871 elsif (Nkind (Par) = N_Attribute_Reference
6872 and then Prefix (Par) = N)
6873 or else Is_Renamed_Object (N)
6877 -- Don't do this optimization if we are within the code for a
6878 -- discriminant check, since the whole point of such a check may
6879 -- be to verify the condition on which the code below depends!
6881 elsif Is_In_Discriminant_Check (N) then
6884 -- Green light to see if we can do the optimization. There is
6885 -- still one condition that inhibits the optimization below but
6886 -- now is the time to check the particular discriminant.
6889 -- Loop through discriminants to find the matching discriminant
6890 -- constraint to see if we can copy it.
6892 Disc := First_Discriminant (Ptyp);
6893 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
6894 Discr_Loop : while Present (Dcon) loop
6896 -- Check if this is the matching discriminant
6898 if Disc = Entity (Selector_Name (N)) then
6900 -- Here we have the matching discriminant. Check for
6901 -- the case of a discriminant of a component that is
6902 -- constrained by an outer discriminant, which cannot
6903 -- be optimized away.
6906 Denotes_Discriminant
6907 (Node (Dcon), Check_Concurrent => True)
6911 -- In the context of a case statement, the expression may
6912 -- have the base type of the discriminant, and we need to
6913 -- preserve the constraint to avoid spurious errors on
6916 elsif Nkind (Parent (N)) = N_Case_Statement
6917 and then Etype (Node (Dcon)) /= Etype (Disc)
6920 Make_Qualified_Expression (Loc,
6922 New_Occurrence_Of (Etype (Disc), Loc),
6924 New_Copy_Tree (Node (Dcon))));
6925 Analyze_And_Resolve (N, Etype (Disc));
6927 -- In case that comes out as a static expression,
6928 -- reset it (a selected component is never static).
6930 Set_Is_Static_Expression (N, False);
6933 -- Otherwise we can just copy the constraint, but the
6934 -- result is certainly not static! In some cases the
6935 -- discriminant constraint has been analyzed in the
6936 -- context of the original subtype indication, but for
6937 -- itypes the constraint might not have been analyzed
6938 -- yet, and this must be done now.
6941 Rewrite (N, New_Copy_Tree (Node (Dcon)));
6942 Analyze_And_Resolve (N);
6943 Set_Is_Static_Expression (N, False);
6949 Next_Discriminant (Disc);
6950 end loop Discr_Loop;
6952 -- Note: the above loop should always find a matching
6953 -- discriminant, but if it does not, we just missed an
6954 -- optimization due to some glitch (perhaps a previous error),
6960 -- The only remaining processing is in the case of a discriminant of
6961 -- a concurrent object, where we rewrite the prefix to denote the
6962 -- corresponding record type. If the type is derived and has renamed
6963 -- discriminants, use corresponding discriminant, which is the one
6964 -- that appears in the corresponding record.
6966 if not Is_Concurrent_Type (Ptyp) then
6970 Disc := Entity (Selector_Name (N));
6972 if Is_Derived_Type (Ptyp)
6973 and then Present (Corresponding_Discriminant (Disc))
6975 Disc := Corresponding_Discriminant (Disc);
6979 Make_Selected_Component (Loc,
6981 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
6983 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
6988 end Expand_N_Selected_Component;
6990 --------------------
6991 -- Expand_N_Slice --
6992 --------------------
6994 procedure Expand_N_Slice (N : Node_Id) is
6995 Loc : constant Source_Ptr := Sloc (N);
6996 Typ : constant Entity_Id := Etype (N);
6997 Pfx : constant Node_Id := Prefix (N);
6998 Ptp : Entity_Id := Etype (Pfx);
7000 function Is_Procedure_Actual (N : Node_Id) return Boolean;
7001 -- Check whether the argument is an actual for a procedure call, in
7002 -- which case the expansion of a bit-packed slice is deferred until the
7003 -- call itself is expanded. The reason this is required is that we might
7004 -- have an IN OUT or OUT parameter, and the copy out is essential, and
7005 -- that copy out would be missed if we created a temporary here in
7006 -- Expand_N_Slice. Note that we don't bother to test specifically for an
7007 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
7008 -- is harmless to defer expansion in the IN case, since the call
7009 -- processing will still generate the appropriate copy in operation,
7010 -- which will take care of the slice.
7012 procedure Make_Temporary;
7013 -- Create a named variable for the value of the slice, in cases where
7014 -- the back-end cannot handle it properly, e.g. when packed types or
7015 -- unaligned slices are involved.
7017 -------------------------
7018 -- Is_Procedure_Actual --
7019 -------------------------
7021 function Is_Procedure_Actual (N : Node_Id) return Boolean is
7022 Par : Node_Id := Parent (N);
7026 -- If our parent is a procedure call we can return
7028 if Nkind (Par) = N_Procedure_Call_Statement then
7031 -- If our parent is a type conversion, keep climbing the tree,
7032 -- since a type conversion can be a procedure actual. Also keep
7033 -- climbing if parameter association or a qualified expression,
7034 -- since these are additional cases that do can appear on
7035 -- procedure actuals.
7037 elsif Nkind_In (Par, N_Type_Conversion,
7038 N_Parameter_Association,
7039 N_Qualified_Expression)
7041 Par := Parent (Par);
7043 -- Any other case is not what we are looking for
7049 end Is_Procedure_Actual;
7051 --------------------
7052 -- Make_Temporary --
7053 --------------------
7055 procedure Make_Temporary is
7057 Ent : constant Entity_Id :=
7058 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
7061 Make_Object_Declaration (Loc,
7062 Defining_Identifier => Ent,
7063 Object_Definition => New_Occurrence_Of (Typ, Loc));
7065 Set_No_Initialization (Decl);
7067 Insert_Actions (N, New_List (
7069 Make_Assignment_Statement (Loc,
7070 Name => New_Occurrence_Of (Ent, Loc),
7071 Expression => Relocate_Node (N))));
7073 Rewrite (N, New_Occurrence_Of (Ent, Loc));
7074 Analyze_And_Resolve (N, Typ);
7077 -- Start of processing for Expand_N_Slice
7080 -- Special handling for access types
7082 if Is_Access_Type (Ptp) then
7084 Ptp := Designated_Type (Ptp);
7087 Make_Explicit_Dereference (Sloc (N),
7088 Prefix => Relocate_Node (Pfx)));
7090 Analyze_And_Resolve (Pfx, Ptp);
7093 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7094 -- function, then additional actuals must be passed.
7096 if Ada_Version >= Ada_05
7097 and then Is_Build_In_Place_Function_Call (Pfx)
7099 Make_Build_In_Place_Call_In_Anonymous_Context (Pfx);
7102 -- Range checks are potentially also needed for cases involving a slice
7103 -- indexed by a subtype indication, but Do_Range_Check can currently
7104 -- only be set for expressions ???
7106 if not Index_Checks_Suppressed (Ptp)
7107 and then (not Is_Entity_Name (Pfx)
7108 or else not Index_Checks_Suppressed (Entity (Pfx)))
7109 and then Nkind (Discrete_Range (N)) /= N_Subtype_Indication
7111 -- Do not enable range check to nodes associated with the frontend
7112 -- expansion of the dispatch table. We first check if Ada.Tags is
7113 -- already loaded to avoid the addition of an undesired dependence
7114 -- on such run-time unit.
7119 (RTU_Loaded (Ada_Tags)
7120 and then Nkind (Prefix (N)) = N_Selected_Component
7121 and then Present (Entity (Selector_Name (Prefix (N))))
7122 and then Entity (Selector_Name (Prefix (N))) =
7123 RTE_Record_Component (RE_Prims_Ptr)))
7125 Enable_Range_Check (Discrete_Range (N));
7128 -- The remaining case to be handled is packed slices. We can leave
7129 -- packed slices as they are in the following situations:
7131 -- 1. Right or left side of an assignment (we can handle this
7132 -- situation correctly in the assignment statement expansion).
7134 -- 2. Prefix of indexed component (the slide is optimized away in this
7135 -- case, see the start of Expand_N_Slice.)
7137 -- 3. Object renaming declaration, since we want the name of the
7138 -- slice, not the value.
7140 -- 4. Argument to procedure call, since copy-in/copy-out handling may
7141 -- be required, and this is handled in the expansion of call
7144 -- 5. Prefix of an address attribute (this is an error which is caught
7145 -- elsewhere, and the expansion would interfere with generating the
7148 if not Is_Packed (Typ) then
7150 -- Apply transformation for actuals of a function call, where
7151 -- Expand_Actuals is not used.
7153 if Nkind (Parent (N)) = N_Function_Call
7154 and then Is_Possibly_Unaligned_Slice (N)
7159 elsif Nkind (Parent (N)) = N_Assignment_Statement
7160 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
7161 and then Parent (N) = Name (Parent (Parent (N))))
7165 elsif Nkind (Parent (N)) = N_Indexed_Component
7166 or else Is_Renamed_Object (N)
7167 or else Is_Procedure_Actual (N)
7171 elsif Nkind (Parent (N)) = N_Attribute_Reference
7172 and then Attribute_Name (Parent (N)) = Name_Address
7181 ------------------------------
7182 -- Expand_N_Type_Conversion --
7183 ------------------------------
7185 procedure Expand_N_Type_Conversion (N : Node_Id) is
7186 Loc : constant Source_Ptr := Sloc (N);
7187 Operand : constant Node_Id := Expression (N);
7188 Target_Type : constant Entity_Id := Etype (N);
7189 Operand_Type : Entity_Id := Etype (Operand);
7191 procedure Handle_Changed_Representation;
7192 -- This is called in the case of record and array type conversions to
7193 -- see if there is a change of representation to be handled. Change of
7194 -- representation is actually handled at the assignment statement level,
7195 -- and what this procedure does is rewrite node N conversion as an
7196 -- assignment to temporary. If there is no change of representation,
7197 -- then the conversion node is unchanged.
7199 procedure Real_Range_Check;
7200 -- Handles generation of range check for real target value
7202 -----------------------------------
7203 -- Handle_Changed_Representation --
7204 -----------------------------------
7206 procedure Handle_Changed_Representation is
7215 -- Nothing else to do if no change of representation
7217 if Same_Representation (Operand_Type, Target_Type) then
7220 -- The real change of representation work is done by the assignment
7221 -- statement processing. So if this type conversion is appearing as
7222 -- the expression of an assignment statement, nothing needs to be
7223 -- done to the conversion.
7225 elsif Nkind (Parent (N)) = N_Assignment_Statement then
7228 -- Otherwise we need to generate a temporary variable, and do the
7229 -- change of representation assignment into that temporary variable.
7230 -- The conversion is then replaced by a reference to this variable.
7235 -- If type is unconstrained we have to add a constraint, copied
7236 -- from the actual value of the left hand side.
7238 if not Is_Constrained (Target_Type) then
7239 if Has_Discriminants (Operand_Type) then
7240 Disc := First_Discriminant (Operand_Type);
7242 if Disc /= First_Stored_Discriminant (Operand_Type) then
7243 Disc := First_Stored_Discriminant (Operand_Type);
7247 while Present (Disc) loop
7249 Make_Selected_Component (Loc,
7250 Prefix => Duplicate_Subexpr_Move_Checks (Operand),
7252 Make_Identifier (Loc, Chars (Disc))));
7253 Next_Discriminant (Disc);
7256 elsif Is_Array_Type (Operand_Type) then
7257 N_Ix := First_Index (Target_Type);
7260 for J in 1 .. Number_Dimensions (Operand_Type) loop
7262 -- We convert the bounds explicitly. We use an unchecked
7263 -- conversion because bounds checks are done elsewhere.
7268 Unchecked_Convert_To (Etype (N_Ix),
7269 Make_Attribute_Reference (Loc,
7271 Duplicate_Subexpr_No_Checks
7272 (Operand, Name_Req => True),
7273 Attribute_Name => Name_First,
7274 Expressions => New_List (
7275 Make_Integer_Literal (Loc, J)))),
7278 Unchecked_Convert_To (Etype (N_Ix),
7279 Make_Attribute_Reference (Loc,
7281 Duplicate_Subexpr_No_Checks
7282 (Operand, Name_Req => True),
7283 Attribute_Name => Name_Last,
7284 Expressions => New_List (
7285 Make_Integer_Literal (Loc, J))))));
7292 Odef := New_Occurrence_Of (Target_Type, Loc);
7294 if Present (Cons) then
7296 Make_Subtype_Indication (Loc,
7297 Subtype_Mark => Odef,
7299 Make_Index_Or_Discriminant_Constraint (Loc,
7300 Constraints => Cons));
7303 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
7305 Make_Object_Declaration (Loc,
7306 Defining_Identifier => Temp,
7307 Object_Definition => Odef);
7309 Set_No_Initialization (Decl, True);
7311 -- Insert required actions. It is essential to suppress checks
7312 -- since we have suppressed default initialization, which means
7313 -- that the variable we create may have no discriminants.
7318 Make_Assignment_Statement (Loc,
7319 Name => New_Occurrence_Of (Temp, Loc),
7320 Expression => Relocate_Node (N))),
7321 Suppress => All_Checks);
7323 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7326 end Handle_Changed_Representation;
7328 ----------------------
7329 -- Real_Range_Check --
7330 ----------------------
7332 -- Case of conversions to floating-point or fixed-point. If range checks
7333 -- are enabled and the target type has a range constraint, we convert:
7339 -- Tnn : typ'Base := typ'Base (x);
7340 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
7343 -- This is necessary when there is a conversion of integer to float or
7344 -- to fixed-point to ensure that the correct checks are made. It is not
7345 -- necessary for float to float where it is enough to simply set the
7346 -- Do_Range_Check flag.
7348 procedure Real_Range_Check is
7349 Btyp : constant Entity_Id := Base_Type (Target_Type);
7350 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
7351 Hi : constant Node_Id := Type_High_Bound (Target_Type);
7352 Xtyp : constant Entity_Id := Etype (Operand);
7357 -- Nothing to do if conversion was rewritten
7359 if Nkind (N) /= N_Type_Conversion then
7363 -- Nothing to do if range checks suppressed, or target has the same
7364 -- range as the base type (or is the base type).
7366 if Range_Checks_Suppressed (Target_Type)
7367 or else (Lo = Type_Low_Bound (Btyp)
7369 Hi = Type_High_Bound (Btyp))
7374 -- Nothing to do if expression is an entity on which checks have been
7377 if Is_Entity_Name (Operand)
7378 and then Range_Checks_Suppressed (Entity (Operand))
7383 -- Nothing to do if bounds are all static and we can tell that the
7384 -- expression is within the bounds of the target. Note that if the
7385 -- operand is of an unconstrained floating-point type, then we do
7386 -- not trust it to be in range (might be infinite)
7389 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
7390 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
7393 if (not Is_Floating_Point_Type (Xtyp)
7394 or else Is_Constrained (Xtyp))
7395 and then Compile_Time_Known_Value (S_Lo)
7396 and then Compile_Time_Known_Value (S_Hi)
7397 and then Compile_Time_Known_Value (Hi)
7398 and then Compile_Time_Known_Value (Lo)
7401 D_Lov : constant Ureal := Expr_Value_R (Lo);
7402 D_Hiv : constant Ureal := Expr_Value_R (Hi);
7407 if Is_Real_Type (Xtyp) then
7408 S_Lov := Expr_Value_R (S_Lo);
7409 S_Hiv := Expr_Value_R (S_Hi);
7411 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
7412 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
7416 and then S_Lov >= D_Lov
7417 and then S_Hiv <= D_Hiv
7419 Set_Do_Range_Check (Operand, False);
7426 -- For float to float conversions, we are done
7428 if Is_Floating_Point_Type (Xtyp)
7430 Is_Floating_Point_Type (Btyp)
7435 -- Otherwise rewrite the conversion as described above
7437 Conv := Relocate_Node (N);
7439 (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
7440 Set_Etype (Conv, Btyp);
7442 -- Enable overflow except for case of integer to float conversions,
7443 -- where it is never required, since we can never have overflow in
7446 if not Is_Integer_Type (Etype (Operand)) then
7447 Enable_Overflow_Check (Conv);
7451 Make_Defining_Identifier (Loc,
7452 Chars => New_Internal_Name ('T'));
7454 Insert_Actions (N, New_List (
7455 Make_Object_Declaration (Loc,
7456 Defining_Identifier => Tnn,
7457 Object_Definition => New_Occurrence_Of (Btyp, Loc),
7458 Expression => Conv),
7460 Make_Raise_Constraint_Error (Loc,
7465 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
7467 Make_Attribute_Reference (Loc,
7468 Attribute_Name => Name_First,
7470 New_Occurrence_Of (Target_Type, Loc))),
7474 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
7476 Make_Attribute_Reference (Loc,
7477 Attribute_Name => Name_Last,
7479 New_Occurrence_Of (Target_Type, Loc)))),
7480 Reason => CE_Range_Check_Failed)));
7482 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
7483 Analyze_And_Resolve (N, Btyp);
7484 end Real_Range_Check;
7486 -- Start of processing for Expand_N_Type_Conversion
7489 -- Nothing at all to do if conversion is to the identical type so remove
7490 -- the conversion completely, it is useless.
7492 if Operand_Type = Target_Type then
7493 Rewrite (N, Relocate_Node (Operand));
7497 -- Nothing to do if this is the second argument of read. This is a
7498 -- "backwards" conversion that will be handled by the specialized code
7499 -- in attribute processing.
7501 if Nkind (Parent (N)) = N_Attribute_Reference
7502 and then Attribute_Name (Parent (N)) = Name_Read
7503 and then Next (First (Expressions (Parent (N)))) = N
7508 -- Here if we may need to expand conversion
7510 -- Do validity check if validity checking operands
7512 if Validity_Checks_On
7513 and then Validity_Check_Operands
7515 Ensure_Valid (Operand);
7518 -- Special case of converting from non-standard boolean type
7520 if Is_Boolean_Type (Operand_Type)
7521 and then (Nonzero_Is_True (Operand_Type))
7523 Adjust_Condition (Operand);
7524 Set_Etype (Operand, Standard_Boolean);
7525 Operand_Type := Standard_Boolean;
7528 -- Case of converting to an access type
7530 if Is_Access_Type (Target_Type) then
7532 -- Apply an accessibility check when the conversion operand is an
7533 -- access parameter (or a renaming thereof), unless conversion was
7534 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
7535 -- Note that other checks may still need to be applied below (such
7536 -- as tagged type checks).
7538 if Is_Entity_Name (Operand)
7540 (Is_Formal (Entity (Operand))
7542 (Present (Renamed_Object (Entity (Operand)))
7543 and then Is_Entity_Name (Renamed_Object (Entity (Operand)))
7545 (Entity (Renamed_Object (Entity (Operand))))))
7546 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
7547 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
7548 or else Attribute_Name (Original_Node (N)) = Name_Access)
7550 Apply_Accessibility_Check
7551 (Operand, Target_Type, Insert_Node => Operand);
7553 -- If the level of the operand type is statically deeper than the
7554 -- level of the target type, then force Program_Error. Note that this
7555 -- can only occur for cases where the attribute is within the body of
7556 -- an instantiation (otherwise the conversion will already have been
7557 -- rejected as illegal). Note: warnings are issued by the analyzer
7558 -- for the instance cases.
7560 elsif In_Instance_Body
7561 and then Type_Access_Level (Operand_Type) >
7562 Type_Access_Level (Target_Type)
7565 Make_Raise_Program_Error (Sloc (N),
7566 Reason => PE_Accessibility_Check_Failed));
7567 Set_Etype (N, Target_Type);
7569 -- When the operand is a selected access discriminant the check needs
7570 -- to be made against the level of the object denoted by the prefix
7571 -- of the selected name. Force Program_Error for this case as well
7572 -- (this accessibility violation can only happen if within the body
7573 -- of an instantiation).
7575 elsif In_Instance_Body
7576 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
7577 and then Nkind (Operand) = N_Selected_Component
7578 and then Object_Access_Level (Operand) >
7579 Type_Access_Level (Target_Type)
7582 Make_Raise_Program_Error (Sloc (N),
7583 Reason => PE_Accessibility_Check_Failed));
7584 Set_Etype (N, Target_Type);
7588 -- Case of conversions of tagged types and access to tagged types
7590 -- When needed, that is to say when the expression is class-wide, Add
7591 -- runtime a tag check for (strict) downward conversion by using the
7592 -- membership test, generating:
7594 -- [constraint_error when Operand not in Target_Type'Class]
7596 -- or in the access type case
7598 -- [constraint_error
7599 -- when Operand /= null
7600 -- and then Operand.all not in
7601 -- Designated_Type (Target_Type)'Class]
7603 if (Is_Access_Type (Target_Type)
7604 and then Is_Tagged_Type (Designated_Type (Target_Type)))
7605 or else Is_Tagged_Type (Target_Type)
7607 -- Do not do any expansion in the access type case if the parent is a
7608 -- renaming, since this is an error situation which will be caught by
7609 -- Sem_Ch8, and the expansion can interfere with this error check.
7611 if Is_Access_Type (Target_Type)
7612 and then Is_Renamed_Object (N)
7617 -- Otherwise, proceed with processing tagged conversion
7620 Actual_Op_Typ : Entity_Id;
7621 Actual_Targ_Typ : Entity_Id;
7622 Make_Conversion : Boolean := False;
7623 Root_Op_Typ : Entity_Id;
7625 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
7626 -- Create a membership check to test whether Operand is a member
7627 -- of Targ_Typ. If the original Target_Type is an access, include
7628 -- a test for null value. The check is inserted at N.
7630 --------------------
7631 -- Make_Tag_Check --
7632 --------------------
7634 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
7639 -- [Constraint_Error
7640 -- when Operand /= null
7641 -- and then Operand.all not in Targ_Typ]
7643 if Is_Access_Type (Target_Type) then
7648 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
7649 Right_Opnd => Make_Null (Loc)),
7654 Make_Explicit_Dereference (Loc,
7655 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
7656 Right_Opnd => New_Reference_To (Targ_Typ, Loc)));
7659 -- [Constraint_Error when Operand not in Targ_Typ]
7664 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
7665 Right_Opnd => New_Reference_To (Targ_Typ, Loc));
7669 Make_Raise_Constraint_Error (Loc,
7671 Reason => CE_Tag_Check_Failed));
7674 -- Start of processing
7677 if Is_Access_Type (Target_Type) then
7678 Actual_Op_Typ := Designated_Type (Operand_Type);
7679 Actual_Targ_Typ := Designated_Type (Target_Type);
7682 Actual_Op_Typ := Operand_Type;
7683 Actual_Targ_Typ := Target_Type;
7686 Root_Op_Typ := Root_Type (Actual_Op_Typ);
7688 -- Ada 2005 (AI-251): Handle interface type conversion
7690 if Is_Interface (Actual_Op_Typ) then
7691 Expand_Interface_Conversion (N, Is_Static => False);
7695 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
7697 -- Create a runtime tag check for a downward class-wide type
7700 if Is_Class_Wide_Type (Actual_Op_Typ)
7701 and then Root_Op_Typ /= Actual_Targ_Typ
7702 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ)
7704 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
7705 Make_Conversion := True;
7708 -- AI05-0073: If the result subtype of the function is defined
7709 -- by an access_definition designating a specific tagged type
7710 -- T, a check is made that the result value is null or the tag
7711 -- of the object designated by the result value identifies T.
7712 -- Constraint_Error is raised if this check fails.
7714 if Nkind (Parent (N)) = Sinfo.N_Return_Statement then
7717 Func_Typ : Entity_Id;
7720 -- Climb scope stack looking for the enclosing function
7722 Func := Current_Scope;
7723 while Present (Func)
7724 and then Ekind (Func) /= E_Function
7726 Func := Scope (Func);
7729 -- The function's return subtype must be defined using
7730 -- an access definition.
7732 if Nkind (Result_Definition (Parent (Func))) =
7735 Func_Typ := Directly_Designated_Type (Etype (Func));
7737 -- The return subtype denotes a specific tagged type,
7738 -- in other words, a non class-wide type.
7740 if Is_Tagged_Type (Func_Typ)
7741 and then not Is_Class_Wide_Type (Func_Typ)
7743 Make_Tag_Check (Actual_Targ_Typ);
7744 Make_Conversion := True;
7750 -- We have generated a tag check for either a class-wide type
7751 -- conversion or for AI05-0073.
7753 if Make_Conversion then
7758 Make_Unchecked_Type_Conversion (Loc,
7759 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
7760 Expression => Relocate_Node (Expression (N)));
7762 Analyze_And_Resolve (N, Target_Type);
7768 -- Case of other access type conversions
7770 elsif Is_Access_Type (Target_Type) then
7771 Apply_Constraint_Check (Operand, Target_Type);
7773 -- Case of conversions from a fixed-point type
7775 -- These conversions require special expansion and processing, found in
7776 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
7777 -- since from a semantic point of view, these are simple integer
7778 -- conversions, which do not need further processing.
7780 elsif Is_Fixed_Point_Type (Operand_Type)
7781 and then not Conversion_OK (N)
7783 -- We should never see universal fixed at this case, since the
7784 -- expansion of the constituent divide or multiply should have
7785 -- eliminated the explicit mention of universal fixed.
7787 pragma Assert (Operand_Type /= Universal_Fixed);
7789 -- Check for special case of the conversion to universal real that
7790 -- occurs as a result of the use of a round attribute. In this case,
7791 -- the real type for the conversion is taken from the target type of
7792 -- the Round attribute and the result must be marked as rounded.
7794 if Target_Type = Universal_Real
7795 and then Nkind (Parent (N)) = N_Attribute_Reference
7796 and then Attribute_Name (Parent (N)) = Name_Round
7798 Set_Rounded_Result (N);
7799 Set_Etype (N, Etype (Parent (N)));
7802 -- Otherwise do correct fixed-conversion, but skip these if the
7803 -- Conversion_OK flag is set, because from a semantic point of
7804 -- view these are simple integer conversions needing no further
7805 -- processing (the backend will simply treat them as integers)
7807 if not Conversion_OK (N) then
7808 if Is_Fixed_Point_Type (Etype (N)) then
7809 Expand_Convert_Fixed_To_Fixed (N);
7812 elsif Is_Integer_Type (Etype (N)) then
7813 Expand_Convert_Fixed_To_Integer (N);
7816 pragma Assert (Is_Floating_Point_Type (Etype (N)));
7817 Expand_Convert_Fixed_To_Float (N);
7822 -- Case of conversions to a fixed-point type
7824 -- These conversions require special expansion and processing, found in
7825 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
7826 -- since from a semantic point of view, these are simple integer
7827 -- conversions, which do not need further processing.
7829 elsif Is_Fixed_Point_Type (Target_Type)
7830 and then not Conversion_OK (N)
7832 if Is_Integer_Type (Operand_Type) then
7833 Expand_Convert_Integer_To_Fixed (N);
7836 pragma Assert (Is_Floating_Point_Type (Operand_Type));
7837 Expand_Convert_Float_To_Fixed (N);
7841 -- Case of float-to-integer conversions
7843 -- We also handle float-to-fixed conversions with Conversion_OK set
7844 -- since semantically the fixed-point target is treated as though it
7845 -- were an integer in such cases.
7847 elsif Is_Floating_Point_Type (Operand_Type)
7849 (Is_Integer_Type (Target_Type)
7851 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
7853 -- One more check here, gcc is still not able to do conversions of
7854 -- this type with proper overflow checking, and so gigi is doing an
7855 -- approximation of what is required by doing floating-point compares
7856 -- with the end-point. But that can lose precision in some cases, and
7857 -- give a wrong result. Converting the operand to Universal_Real is
7858 -- helpful, but still does not catch all cases with 64-bit integers
7859 -- on targets with only 64-bit floats
7861 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
7862 -- Can this code be removed ???
7864 if Do_Range_Check (Operand) then
7866 Make_Type_Conversion (Loc,
7868 New_Occurrence_Of (Universal_Real, Loc),
7870 Relocate_Node (Operand)));
7872 Set_Etype (Operand, Universal_Real);
7873 Enable_Range_Check (Operand);
7874 Set_Do_Range_Check (Expression (Operand), False);
7877 -- Case of array conversions
7879 -- Expansion of array conversions, add required length/range checks but
7880 -- only do this if there is no change of representation. For handling of
7881 -- this case, see Handle_Changed_Representation.
7883 elsif Is_Array_Type (Target_Type) then
7885 if Is_Constrained (Target_Type) then
7886 Apply_Length_Check (Operand, Target_Type);
7888 Apply_Range_Check (Operand, Target_Type);
7891 Handle_Changed_Representation;
7893 -- Case of conversions of discriminated types
7895 -- Add required discriminant checks if target is constrained. Again this
7896 -- change is skipped if we have a change of representation.
7898 elsif Has_Discriminants (Target_Type)
7899 and then Is_Constrained (Target_Type)
7901 Apply_Discriminant_Check (Operand, Target_Type);
7902 Handle_Changed_Representation;
7904 -- Case of all other record conversions. The only processing required
7905 -- is to check for a change of representation requiring the special
7906 -- assignment processing.
7908 elsif Is_Record_Type (Target_Type) then
7910 -- Ada 2005 (AI-216): Program_Error is raised when converting from
7911 -- a derived Unchecked_Union type to an unconstrained type that is
7912 -- not Unchecked_Union if the operand lacks inferable discriminants.
7914 if Is_Derived_Type (Operand_Type)
7915 and then Is_Unchecked_Union (Base_Type (Operand_Type))
7916 and then not Is_Constrained (Target_Type)
7917 and then not Is_Unchecked_Union (Base_Type (Target_Type))
7918 and then not Has_Inferable_Discriminants (Operand)
7920 -- To prevent Gigi from generating illegal code, we generate a
7921 -- Program_Error node, but we give it the target type of the
7925 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
7926 Reason => PE_Unchecked_Union_Restriction);
7929 Set_Etype (PE, Target_Type);
7934 Handle_Changed_Representation;
7937 -- Case of conversions of enumeration types
7939 elsif Is_Enumeration_Type (Target_Type) then
7941 -- Special processing is required if there is a change of
7942 -- representation (from enumeration representation clauses)
7944 if not Same_Representation (Target_Type, Operand_Type) then
7946 -- Convert: x(y) to x'val (ytyp'val (y))
7949 Make_Attribute_Reference (Loc,
7950 Prefix => New_Occurrence_Of (Target_Type, Loc),
7951 Attribute_Name => Name_Val,
7952 Expressions => New_List (
7953 Make_Attribute_Reference (Loc,
7954 Prefix => New_Occurrence_Of (Operand_Type, Loc),
7955 Attribute_Name => Name_Pos,
7956 Expressions => New_List (Operand)))));
7958 Analyze_And_Resolve (N, Target_Type);
7961 -- Case of conversions to floating-point
7963 elsif Is_Floating_Point_Type (Target_Type) then
7967 -- At this stage, either the conversion node has been transformed into
7968 -- some other equivalent expression, or left as a conversion that can
7969 -- be handled by Gigi. The conversions that Gigi can handle are the
7972 -- Conversions with no change of representation or type
7974 -- Numeric conversions involving integer, floating- and fixed-point
7975 -- values. Fixed-point values are allowed only if Conversion_OK is
7976 -- set, i.e. if the fixed-point values are to be treated as integers.
7978 -- No other conversions should be passed to Gigi
7980 -- Check: are these rules stated in sinfo??? if so, why restate here???
7982 -- The only remaining step is to generate a range check if we still have
7983 -- a type conversion at this stage and Do_Range_Check is set. For now we
7984 -- do this only for conversions of discrete types.
7986 if Nkind (N) = N_Type_Conversion
7987 and then Is_Discrete_Type (Etype (N))
7990 Expr : constant Node_Id := Expression (N);
7995 if Do_Range_Check (Expr)
7996 and then Is_Discrete_Type (Etype (Expr))
7998 Set_Do_Range_Check (Expr, False);
8000 -- Before we do a range check, we have to deal with treating a
8001 -- fixed-point operand as an integer. The way we do this is
8002 -- simply to do an unchecked conversion to an appropriate
8003 -- integer type large enough to hold the result.
8005 -- This code is not active yet, because we are only dealing
8006 -- with discrete types so far ???
8008 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
8009 and then Treat_Fixed_As_Integer (Expr)
8011 Ftyp := Base_Type (Etype (Expr));
8013 if Esize (Ftyp) >= Esize (Standard_Integer) then
8014 Ityp := Standard_Long_Long_Integer;
8016 Ityp := Standard_Integer;
8019 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
8022 -- Reset overflow flag, since the range check will include
8023 -- dealing with possible overflow, and generate the check If
8024 -- Address is either a source type or target type, suppress
8025 -- range check to avoid typing anomalies when it is a visible
8028 Set_Do_Overflow_Check (N, False);
8029 if not Is_Descendent_Of_Address (Etype (Expr))
8030 and then not Is_Descendent_Of_Address (Target_Type)
8032 Generate_Range_Check
8033 (Expr, Target_Type, CE_Range_Check_Failed);
8039 -- Final step, if the result is a type conversion involving Vax_Float
8040 -- types, then it is subject for further special processing.
8042 if Nkind (N) = N_Type_Conversion
8043 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
8045 Expand_Vax_Conversion (N);
8048 end Expand_N_Type_Conversion;
8050 -----------------------------------
8051 -- Expand_N_Unchecked_Expression --
8052 -----------------------------------
8054 -- Remove the unchecked expression node from the tree. It's job was simply
8055 -- to make sure that its constituent expression was handled with checks
8056 -- off, and now that that is done, we can remove it from the tree, and
8057 -- indeed must, since gigi does not expect to see these nodes.
8059 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
8060 Exp : constant Node_Id := Expression (N);
8063 Set_Assignment_OK (Exp, Assignment_OK (N) or Assignment_OK (Exp));
8065 end Expand_N_Unchecked_Expression;
8067 ----------------------------------------
8068 -- Expand_N_Unchecked_Type_Conversion --
8069 ----------------------------------------
8071 -- If this cannot be handled by Gigi and we haven't already made a
8072 -- temporary for it, do it now.
8074 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
8075 Target_Type : constant Entity_Id := Etype (N);
8076 Operand : constant Node_Id := Expression (N);
8077 Operand_Type : constant Entity_Id := Etype (Operand);
8080 -- If we have a conversion of a compile time known value to a target
8081 -- type and the value is in range of the target type, then we can simply
8082 -- replace the construct by an integer literal of the correct type. We
8083 -- only apply this to integer types being converted. Possibly it may
8084 -- apply in other cases, but it is too much trouble to worry about.
8086 -- Note that we do not do this transformation if the Kill_Range_Check
8087 -- flag is set, since then the value may be outside the expected range.
8088 -- This happens in the Normalize_Scalars case.
8090 -- We also skip this if either the target or operand type is biased
8091 -- because in this case, the unchecked conversion is supposed to
8092 -- preserve the bit pattern, not the integer value.
8094 if Is_Integer_Type (Target_Type)
8095 and then not Has_Biased_Representation (Target_Type)
8096 and then Is_Integer_Type (Operand_Type)
8097 and then not Has_Biased_Representation (Operand_Type)
8098 and then Compile_Time_Known_Value (Operand)
8099 and then not Kill_Range_Check (N)
8102 Val : constant Uint := Expr_Value (Operand);
8105 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
8107 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
8109 Val >= Expr_Value (Type_Low_Bound (Target_Type))
8111 Val <= Expr_Value (Type_High_Bound (Target_Type))
8113 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
8115 -- If Address is the target type, just set the type to avoid a
8116 -- spurious type error on the literal when Address is a visible
8119 if Is_Descendent_Of_Address (Target_Type) then
8120 Set_Etype (N, Target_Type);
8122 Analyze_And_Resolve (N, Target_Type);
8130 -- Nothing to do if conversion is safe
8132 if Safe_Unchecked_Type_Conversion (N) then
8136 -- Otherwise force evaluation unless Assignment_OK flag is set (this
8137 -- flag indicates ??? -- more comments needed here)
8139 if Assignment_OK (N) then
8142 Force_Evaluation (N);
8144 end Expand_N_Unchecked_Type_Conversion;
8146 ----------------------------
8147 -- Expand_Record_Equality --
8148 ----------------------------
8150 -- For non-variant records, Equality is expanded when needed into:
8152 -- and then Lhs.Discr1 = Rhs.Discr1
8154 -- and then Lhs.Discrn = Rhs.Discrn
8155 -- and then Lhs.Cmp1 = Rhs.Cmp1
8157 -- and then Lhs.Cmpn = Rhs.Cmpn
8159 -- The expression is folded by the back-end for adjacent fields. This
8160 -- function is called for tagged record in only one occasion: for imple-
8161 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
8162 -- otherwise the primitive "=" is used directly.
8164 function Expand_Record_Equality
8169 Bodies : List_Id) return Node_Id
8171 Loc : constant Source_Ptr := Sloc (Nod);
8176 First_Time : Boolean := True;
8178 function Suitable_Element (C : Entity_Id) return Entity_Id;
8179 -- Return the first field to compare beginning with C, skipping the
8180 -- inherited components.
8182 ----------------------
8183 -- Suitable_Element --
8184 ----------------------
8186 function Suitable_Element (C : Entity_Id) return Entity_Id is
8191 elsif Ekind (C) /= E_Discriminant
8192 and then Ekind (C) /= E_Component
8194 return Suitable_Element (Next_Entity (C));
8196 elsif Is_Tagged_Type (Typ)
8197 and then C /= Original_Record_Component (C)
8199 return Suitable_Element (Next_Entity (C));
8201 elsif Chars (C) = Name_uController
8202 or else Chars (C) = Name_uTag
8204 return Suitable_Element (Next_Entity (C));
8206 elsif Is_Interface (Etype (C)) then
8207 return Suitable_Element (Next_Entity (C));
8212 end Suitable_Element;
8214 -- Start of processing for Expand_Record_Equality
8217 -- Generates the following code: (assuming that Typ has one Discr and
8218 -- component C2 is also a record)
8221 -- and then Lhs.Discr1 = Rhs.Discr1
8222 -- and then Lhs.C1 = Rhs.C1
8223 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
8225 -- and then Lhs.Cmpn = Rhs.Cmpn
8227 Result := New_Reference_To (Standard_True, Loc);
8228 C := Suitable_Element (First_Entity (Typ));
8230 while Present (C) loop
8238 First_Time := False;
8242 New_Lhs := New_Copy_Tree (Lhs);
8243 New_Rhs := New_Copy_Tree (Rhs);
8247 Expand_Composite_Equality (Nod, Etype (C),
8249 Make_Selected_Component (Loc,
8251 Selector_Name => New_Reference_To (C, Loc)),
8253 Make_Selected_Component (Loc,
8255 Selector_Name => New_Reference_To (C, Loc)),
8258 -- If some (sub)component is an unchecked_union, the whole
8259 -- operation will raise program error.
8261 if Nkind (Check) = N_Raise_Program_Error then
8263 Set_Etype (Result, Standard_Boolean);
8268 Left_Opnd => Result,
8269 Right_Opnd => Check);
8273 C := Suitable_Element (Next_Entity (C));
8277 end Expand_Record_Equality;
8279 -------------------------------------
8280 -- Fixup_Universal_Fixed_Operation --
8281 -------------------------------------
8283 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
8284 Conv : constant Node_Id := Parent (N);
8287 -- We must have a type conversion immediately above us
8289 pragma Assert (Nkind (Conv) = N_Type_Conversion);
8291 -- Normally the type conversion gives our target type. The exception
8292 -- occurs in the case of the Round attribute, where the conversion
8293 -- will be to universal real, and our real type comes from the Round
8294 -- attribute (as well as an indication that we must round the result)
8296 if Nkind (Parent (Conv)) = N_Attribute_Reference
8297 and then Attribute_Name (Parent (Conv)) = Name_Round
8299 Set_Etype (N, Etype (Parent (Conv)));
8300 Set_Rounded_Result (N);
8302 -- Normal case where type comes from conversion above us
8305 Set_Etype (N, Etype (Conv));
8307 end Fixup_Universal_Fixed_Operation;
8309 ------------------------------
8310 -- Get_Allocator_Final_List --
8311 ------------------------------
8313 function Get_Allocator_Final_List
8316 PtrT : Entity_Id) return Entity_Id
8318 Loc : constant Source_Ptr := Sloc (N);
8320 Owner : Entity_Id := PtrT;
8321 -- The entity whose finalization list must be used to attach the
8322 -- allocated object.
8325 if Ekind (PtrT) = E_Anonymous_Access_Type then
8327 -- If the context is an access parameter, we need to create a
8328 -- non-anonymous access type in order to have a usable final list,
8329 -- because there is otherwise no pool to which the allocated object
8330 -- can belong. We create both the type and the finalization chain
8331 -- here, because freezing an internal type does not create such a
8332 -- chain. The Final_Chain that is thus created is shared by the
8333 -- access parameter. The access type is tested against the result
8334 -- type of the function to exclude allocators whose type is an
8335 -- anonymous access result type. We freeze the type at once to
8336 -- ensure that it is properly decorated for the back-end, even
8337 -- if the context and current scope is a loop.
8339 if Nkind (Associated_Node_For_Itype (PtrT))
8340 in N_Subprogram_Specification
8343 Etype (Defining_Unit_Name (Associated_Node_For_Itype (PtrT)))
8345 Owner := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
8347 Make_Full_Type_Declaration (Loc,
8348 Defining_Identifier => Owner,
8350 Make_Access_To_Object_Definition (Loc,
8351 Subtype_Indication =>
8352 New_Occurrence_Of (T, Loc))));
8354 Freeze_Before (N, Owner);
8355 Build_Final_List (N, Owner);
8356 Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner));
8358 -- Ada 2005 (AI-318-02): If the context is a return object
8359 -- declaration, then the anonymous return subtype is defined to have
8360 -- the same accessibility level as that of the function's result
8361 -- subtype, which means that we want the scope where the function is
8364 elsif Nkind (Associated_Node_For_Itype (PtrT)) = N_Object_Declaration
8365 and then Ekind (Scope (PtrT)) = E_Return_Statement
8367 Owner := Scope (Return_Applies_To (Scope (PtrT)));
8369 -- Case of an access discriminant, or (Ada 2005), of an anonymous
8370 -- access component or anonymous access function result: find the
8371 -- final list associated with the scope of the type. (In the
8372 -- anonymous access component kind, a list controller will have
8373 -- been allocated when freezing the record type, and PtrT has an
8374 -- Associated_Final_Chain attribute designating it.)
8376 elsif No (Associated_Final_Chain (PtrT)) then
8377 Owner := Scope (PtrT);
8381 return Find_Final_List (Owner);
8382 end Get_Allocator_Final_List;
8384 ---------------------------------
8385 -- Has_Inferable_Discriminants --
8386 ---------------------------------
8388 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
8390 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
8391 -- Determines whether the left-most prefix of a selected component is a
8392 -- formal parameter in a subprogram. Assumes N is a selected component.
8394 --------------------------------
8395 -- Prefix_Is_Formal_Parameter --
8396 --------------------------------
8398 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
8399 Sel_Comp : Node_Id := N;
8402 -- Move to the left-most prefix by climbing up the tree
8404 while Present (Parent (Sel_Comp))
8405 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
8407 Sel_Comp := Parent (Sel_Comp);
8410 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
8411 end Prefix_Is_Formal_Parameter;
8413 -- Start of processing for Has_Inferable_Discriminants
8416 -- For identifiers and indexed components, it is sufficient to have a
8417 -- constrained Unchecked_Union nominal subtype.
8419 if Nkind_In (N, N_Identifier, N_Indexed_Component) then
8420 return Is_Unchecked_Union (Base_Type (Etype (N)))
8422 Is_Constrained (Etype (N));
8424 -- For selected components, the subtype of the selector must be a
8425 -- constrained Unchecked_Union. If the component is subject to a
8426 -- per-object constraint, then the enclosing object must have inferable
8429 elsif Nkind (N) = N_Selected_Component then
8430 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
8432 -- A small hack. If we have a per-object constrained selected
8433 -- component of a formal parameter, return True since we do not
8434 -- know the actual parameter association yet.
8436 if Prefix_Is_Formal_Parameter (N) then
8440 -- Otherwise, check the enclosing object and the selector
8442 return Has_Inferable_Discriminants (Prefix (N))
8444 Has_Inferable_Discriminants (Selector_Name (N));
8447 -- The call to Has_Inferable_Discriminants will determine whether
8448 -- the selector has a constrained Unchecked_Union nominal type.
8450 return Has_Inferable_Discriminants (Selector_Name (N));
8452 -- A qualified expression has inferable discriminants if its subtype
8453 -- mark is a constrained Unchecked_Union subtype.
8455 elsif Nkind (N) = N_Qualified_Expression then
8456 return Is_Unchecked_Union (Subtype_Mark (N))
8458 Is_Constrained (Subtype_Mark (N));
8463 end Has_Inferable_Discriminants;
8465 -------------------------------
8466 -- Insert_Dereference_Action --
8467 -------------------------------
8469 procedure Insert_Dereference_Action (N : Node_Id) is
8470 Loc : constant Source_Ptr := Sloc (N);
8471 Typ : constant Entity_Id := Etype (N);
8472 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
8473 Pnod : constant Node_Id := Parent (N);
8475 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
8476 -- Return true if type of P is derived from Checked_Pool;
8478 -----------------------------
8479 -- Is_Checked_Storage_Pool --
8480 -----------------------------
8482 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
8491 while T /= Etype (T) loop
8492 if Is_RTE (T, RE_Checked_Pool) then
8500 end Is_Checked_Storage_Pool;
8502 -- Start of processing for Insert_Dereference_Action
8505 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
8507 if not (Is_Checked_Storage_Pool (Pool)
8508 and then Comes_From_Source (Original_Node (Pnod)))
8514 Make_Procedure_Call_Statement (Loc,
8515 Name => New_Reference_To (
8516 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
8518 Parameter_Associations => New_List (
8522 New_Reference_To (Pool, Loc),
8524 -- Storage_Address. We use the attribute Pool_Address, which uses
8525 -- the pointer itself to find the address of the object, and which
8526 -- handles unconstrained arrays properly by computing the address
8527 -- of the template. i.e. the correct address of the corresponding
8530 Make_Attribute_Reference (Loc,
8531 Prefix => Duplicate_Subexpr_Move_Checks (N),
8532 Attribute_Name => Name_Pool_Address),
8534 -- Size_In_Storage_Elements
8536 Make_Op_Divide (Loc,
8538 Make_Attribute_Reference (Loc,
8540 Make_Explicit_Dereference (Loc,
8541 Duplicate_Subexpr_Move_Checks (N)),
8542 Attribute_Name => Name_Size),
8544 Make_Integer_Literal (Loc, System_Storage_Unit)),
8548 Make_Attribute_Reference (Loc,
8550 Make_Explicit_Dereference (Loc,
8551 Duplicate_Subexpr_Move_Checks (N)),
8552 Attribute_Name => Name_Alignment))));
8555 when RE_Not_Available =>
8557 end Insert_Dereference_Action;
8559 ------------------------------
8560 -- Make_Array_Comparison_Op --
8561 ------------------------------
8563 -- This is a hand-coded expansion of the following generic function:
8566 -- type elem is (<>);
8567 -- type index is (<>);
8568 -- type a is array (index range <>) of elem;
8570 -- function Gnnn (X : a; Y: a) return boolean is
8571 -- J : index := Y'first;
8574 -- if X'length = 0 then
8577 -- elsif Y'length = 0 then
8581 -- for I in X'range loop
8582 -- if X (I) = Y (J) then
8583 -- if J = Y'last then
8586 -- J := index'succ (J);
8590 -- return X (I) > Y (J);
8594 -- return X'length > Y'length;
8598 -- Note that since we are essentially doing this expansion by hand, we
8599 -- do not need to generate an actual or formal generic part, just the
8600 -- instantiated function itself.
8602 function Make_Array_Comparison_Op
8604 Nod : Node_Id) return Node_Id
8606 Loc : constant Source_Ptr := Sloc (Nod);
8608 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
8609 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
8610 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
8611 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
8613 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
8615 Loop_Statement : Node_Id;
8616 Loop_Body : Node_Id;
8619 Final_Expr : Node_Id;
8620 Func_Body : Node_Id;
8621 Func_Name : Entity_Id;
8627 -- if J = Y'last then
8630 -- J := index'succ (J);
8634 Make_Implicit_If_Statement (Nod,
8637 Left_Opnd => New_Reference_To (J, Loc),
8639 Make_Attribute_Reference (Loc,
8640 Prefix => New_Reference_To (Y, Loc),
8641 Attribute_Name => Name_Last)),
8643 Then_Statements => New_List (
8644 Make_Exit_Statement (Loc)),
8648 Make_Assignment_Statement (Loc,
8649 Name => New_Reference_To (J, Loc),
8651 Make_Attribute_Reference (Loc,
8652 Prefix => New_Reference_To (Index, Loc),
8653 Attribute_Name => Name_Succ,
8654 Expressions => New_List (New_Reference_To (J, Loc))))));
8656 -- if X (I) = Y (J) then
8659 -- return X (I) > Y (J);
8663 Make_Implicit_If_Statement (Nod,
8667 Make_Indexed_Component (Loc,
8668 Prefix => New_Reference_To (X, Loc),
8669 Expressions => New_List (New_Reference_To (I, Loc))),
8672 Make_Indexed_Component (Loc,
8673 Prefix => New_Reference_To (Y, Loc),
8674 Expressions => New_List (New_Reference_To (J, Loc)))),
8676 Then_Statements => New_List (Inner_If),
8678 Else_Statements => New_List (
8679 Make_Simple_Return_Statement (Loc,
8683 Make_Indexed_Component (Loc,
8684 Prefix => New_Reference_To (X, Loc),
8685 Expressions => New_List (New_Reference_To (I, Loc))),
8688 Make_Indexed_Component (Loc,
8689 Prefix => New_Reference_To (Y, Loc),
8690 Expressions => New_List (
8691 New_Reference_To (J, Loc)))))));
8693 -- for I in X'range loop
8698 Make_Implicit_Loop_Statement (Nod,
8699 Identifier => Empty,
8702 Make_Iteration_Scheme (Loc,
8703 Loop_Parameter_Specification =>
8704 Make_Loop_Parameter_Specification (Loc,
8705 Defining_Identifier => I,
8706 Discrete_Subtype_Definition =>
8707 Make_Attribute_Reference (Loc,
8708 Prefix => New_Reference_To (X, Loc),
8709 Attribute_Name => Name_Range))),
8711 Statements => New_List (Loop_Body));
8713 -- if X'length = 0 then
8715 -- elsif Y'length = 0 then
8718 -- for ... loop ... end loop;
8719 -- return X'length > Y'length;
8723 Make_Attribute_Reference (Loc,
8724 Prefix => New_Reference_To (X, Loc),
8725 Attribute_Name => Name_Length);
8728 Make_Attribute_Reference (Loc,
8729 Prefix => New_Reference_To (Y, Loc),
8730 Attribute_Name => Name_Length);
8734 Left_Opnd => Length1,
8735 Right_Opnd => Length2);
8738 Make_Implicit_If_Statement (Nod,
8742 Make_Attribute_Reference (Loc,
8743 Prefix => New_Reference_To (X, Loc),
8744 Attribute_Name => Name_Length),
8746 Make_Integer_Literal (Loc, 0)),
8750 Make_Simple_Return_Statement (Loc,
8751 Expression => New_Reference_To (Standard_False, Loc))),
8753 Elsif_Parts => New_List (
8754 Make_Elsif_Part (Loc,
8758 Make_Attribute_Reference (Loc,
8759 Prefix => New_Reference_To (Y, Loc),
8760 Attribute_Name => Name_Length),
8762 Make_Integer_Literal (Loc, 0)),
8766 Make_Simple_Return_Statement (Loc,
8767 Expression => New_Reference_To (Standard_True, Loc))))),
8769 Else_Statements => New_List (
8771 Make_Simple_Return_Statement (Loc,
8772 Expression => Final_Expr)));
8776 Formals := New_List (
8777 Make_Parameter_Specification (Loc,
8778 Defining_Identifier => X,
8779 Parameter_Type => New_Reference_To (Typ, Loc)),
8781 Make_Parameter_Specification (Loc,
8782 Defining_Identifier => Y,
8783 Parameter_Type => New_Reference_To (Typ, Loc)));
8785 -- function Gnnn (...) return boolean is
8786 -- J : index := Y'first;
8791 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('G'));
8794 Make_Subprogram_Body (Loc,
8796 Make_Function_Specification (Loc,
8797 Defining_Unit_Name => Func_Name,
8798 Parameter_Specifications => Formals,
8799 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
8801 Declarations => New_List (
8802 Make_Object_Declaration (Loc,
8803 Defining_Identifier => J,
8804 Object_Definition => New_Reference_To (Index, Loc),
8806 Make_Attribute_Reference (Loc,
8807 Prefix => New_Reference_To (Y, Loc),
8808 Attribute_Name => Name_First))),
8810 Handled_Statement_Sequence =>
8811 Make_Handled_Sequence_Of_Statements (Loc,
8812 Statements => New_List (If_Stat)));
8815 end Make_Array_Comparison_Op;
8817 ---------------------------
8818 -- Make_Boolean_Array_Op --
8819 ---------------------------
8821 -- For logical operations on boolean arrays, expand in line the following,
8822 -- replacing 'and' with 'or' or 'xor' where needed:
8824 -- function Annn (A : typ; B: typ) return typ is
8827 -- for J in A'range loop
8828 -- C (J) := A (J) op B (J);
8833 -- Here typ is the boolean array type
8835 function Make_Boolean_Array_Op
8837 N : Node_Id) return Node_Id
8839 Loc : constant Source_Ptr := Sloc (N);
8841 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
8842 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
8843 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
8844 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
8852 Func_Name : Entity_Id;
8853 Func_Body : Node_Id;
8854 Loop_Statement : Node_Id;
8858 Make_Indexed_Component (Loc,
8859 Prefix => New_Reference_To (A, Loc),
8860 Expressions => New_List (New_Reference_To (J, Loc)));
8863 Make_Indexed_Component (Loc,
8864 Prefix => New_Reference_To (B, Loc),
8865 Expressions => New_List (New_Reference_To (J, Loc)));
8868 Make_Indexed_Component (Loc,
8869 Prefix => New_Reference_To (C, Loc),
8870 Expressions => New_List (New_Reference_To (J, Loc)));
8872 if Nkind (N) = N_Op_And then
8878 elsif Nkind (N) = N_Op_Or then
8892 Make_Implicit_Loop_Statement (N,
8893 Identifier => Empty,
8896 Make_Iteration_Scheme (Loc,
8897 Loop_Parameter_Specification =>
8898 Make_Loop_Parameter_Specification (Loc,
8899 Defining_Identifier => J,
8900 Discrete_Subtype_Definition =>
8901 Make_Attribute_Reference (Loc,
8902 Prefix => New_Reference_To (A, Loc),
8903 Attribute_Name => Name_Range))),
8905 Statements => New_List (
8906 Make_Assignment_Statement (Loc,
8908 Expression => Op)));
8910 Formals := New_List (
8911 Make_Parameter_Specification (Loc,
8912 Defining_Identifier => A,
8913 Parameter_Type => New_Reference_To (Typ, Loc)),
8915 Make_Parameter_Specification (Loc,
8916 Defining_Identifier => B,
8917 Parameter_Type => New_Reference_To (Typ, Loc)));
8920 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
8921 Set_Is_Inlined (Func_Name);
8924 Make_Subprogram_Body (Loc,
8926 Make_Function_Specification (Loc,
8927 Defining_Unit_Name => Func_Name,
8928 Parameter_Specifications => Formals,
8929 Result_Definition => New_Reference_To (Typ, Loc)),
8931 Declarations => New_List (
8932 Make_Object_Declaration (Loc,
8933 Defining_Identifier => C,
8934 Object_Definition => New_Reference_To (Typ, Loc))),
8936 Handled_Statement_Sequence =>
8937 Make_Handled_Sequence_Of_Statements (Loc,
8938 Statements => New_List (
8940 Make_Simple_Return_Statement (Loc,
8941 Expression => New_Reference_To (C, Loc)))));
8944 end Make_Boolean_Array_Op;
8946 ------------------------
8947 -- Rewrite_Comparison --
8948 ------------------------
8950 procedure Rewrite_Comparison (N : Node_Id) is
8951 Warning_Generated : Boolean := False;
8952 -- Set to True if first pass with Assume_Valid generates a warning in
8953 -- which case we skip the second pass to avoid warning overloaded.
8956 -- Set to Standard_True or Standard_False
8959 if Nkind (N) = N_Type_Conversion then
8960 Rewrite_Comparison (Expression (N));
8963 elsif Nkind (N) not in N_Op_Compare then
8967 -- Now start looking at the comparison in detail. We potentially go
8968 -- through this loop twice. The first time, Assume_Valid is set False
8969 -- in the call to Compile_Time_Compare. If this call results in a
8970 -- clear result of always True or Always False, that's decisive and
8971 -- we are done. Otherwise we repeat the processing with Assume_Valid
8972 -- set to True to generate additional warnings. We can stil that step
8973 -- if Constant_Condition_Warnings is False.
8975 for AV in False .. True loop
8977 Typ : constant Entity_Id := Etype (N);
8978 Op1 : constant Node_Id := Left_Opnd (N);
8979 Op2 : constant Node_Id := Right_Opnd (N);
8981 Res : constant Compare_Result :=
8982 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
8983 -- Res indicates if compare outcome can be compile time determined
8985 True_Result : Boolean;
8986 False_Result : Boolean;
8989 case N_Op_Compare (Nkind (N)) is
8991 True_Result := Res = EQ;
8992 False_Result := Res = LT or else Res = GT or else Res = NE;
8995 True_Result := Res in Compare_GE;
8996 False_Result := Res = LT;
8999 and then Constant_Condition_Warnings
9000 and then Comes_From_Source (Original_Node (N))
9001 and then Nkind (Original_Node (N)) = N_Op_Ge
9002 and then not In_Instance
9003 and then Is_Integer_Type (Etype (Left_Opnd (N)))
9004 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
9007 ("can never be greater than, could replace by ""'=""?", N);
9008 Warning_Generated := True;
9012 True_Result := Res = GT;
9013 False_Result := Res in Compare_LE;
9016 True_Result := Res = LT;
9017 False_Result := Res in Compare_GE;
9020 True_Result := Res in Compare_LE;
9021 False_Result := Res = GT;
9024 and then Constant_Condition_Warnings
9025 and then Comes_From_Source (Original_Node (N))
9026 and then Nkind (Original_Node (N)) = N_Op_Le
9027 and then not In_Instance
9028 and then Is_Integer_Type (Etype (Left_Opnd (N)))
9029 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
9032 ("can never be less than, could replace by ""'=""?", N);
9033 Warning_Generated := True;
9037 True_Result := Res = NE or else Res = GT or else Res = LT;
9038 False_Result := Res = EQ;
9041 -- If this is the first iteration, then we actually convert the
9042 -- comparison into True or False, if the result is certain.
9045 if True_Result or False_Result then
9047 Result := Standard_True;
9049 Result := Standard_False;
9054 New_Occurrence_Of (Result, Sloc (N))));
9055 Analyze_And_Resolve (N, Typ);
9056 Warn_On_Known_Condition (N);
9060 -- If this is the second iteration (AV = True), and the original
9061 -- node comes from source and we are not in an instance, then
9062 -- give a warning if we know result would be True or False. Note
9063 -- we know Constant_Condition_Warnings is set if we get here.
9065 elsif Comes_From_Source (Original_Node (N))
9066 and then not In_Instance
9070 ("condition can only be False if invalid values present?",
9072 elsif False_Result then
9074 ("condition can only be True if invalid values present?",
9080 -- Skip second iteration if not warning on constant conditions or
9081 -- if the first iteration already generated a warning of some kind
9082 -- or if we are in any case assuming all values are valid (so that
9083 -- the first iteration took care of the valid case).
9085 exit when not Constant_Condition_Warnings;
9086 exit when Warning_Generated;
9087 exit when Assume_No_Invalid_Values;
9089 end Rewrite_Comparison;
9091 ----------------------------
9092 -- Safe_In_Place_Array_Op --
9093 ----------------------------
9095 function Safe_In_Place_Array_Op
9098 Op2 : Node_Id) return Boolean
9102 function Is_Safe_Operand (Op : Node_Id) return Boolean;
9103 -- Operand is safe if it cannot overlap part of the target of the
9104 -- operation. If the operand and the target are identical, the operand
9105 -- is safe. The operand can be empty in the case of negation.
9107 function Is_Unaliased (N : Node_Id) return Boolean;
9108 -- Check that N is a stand-alone entity
9114 function Is_Unaliased (N : Node_Id) return Boolean is
9118 and then No (Address_Clause (Entity (N)))
9119 and then No (Renamed_Object (Entity (N)));
9122 ---------------------
9123 -- Is_Safe_Operand --
9124 ---------------------
9126 function Is_Safe_Operand (Op : Node_Id) return Boolean is
9131 elsif Is_Entity_Name (Op) then
9132 return Is_Unaliased (Op);
9134 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
9135 return Is_Unaliased (Prefix (Op));
9137 elsif Nkind (Op) = N_Slice then
9139 Is_Unaliased (Prefix (Op))
9140 and then Entity (Prefix (Op)) /= Target;
9142 elsif Nkind (Op) = N_Op_Not then
9143 return Is_Safe_Operand (Right_Opnd (Op));
9148 end Is_Safe_Operand;
9150 -- Start of processing for Is_Safe_In_Place_Array_Op
9153 -- Skip this processing if the component size is different from system
9154 -- storage unit (since at least for NOT this would cause problems).
9156 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
9159 -- Cannot do in place stuff on VM_Target since cannot pass addresses
9161 elsif VM_Target /= No_VM then
9164 -- Cannot do in place stuff if non-standard Boolean representation
9166 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
9169 elsif not Is_Unaliased (Lhs) then
9172 Target := Entity (Lhs);
9175 Is_Safe_Operand (Op1)
9176 and then Is_Safe_Operand (Op2);
9178 end Safe_In_Place_Array_Op;
9180 -----------------------
9181 -- Tagged_Membership --
9182 -----------------------
9184 -- There are two different cases to consider depending on whether the right
9185 -- operand is a class-wide type or not. If not we just compare the actual
9186 -- tag of the left expr to the target type tag:
9188 -- Left_Expr.Tag = Right_Type'Tag;
9190 -- If it is a class-wide type we use the RT function CW_Membership which is
9191 -- usually implemented by looking in the ancestor tables contained in the
9192 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
9194 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
9195 -- function IW_Membership which is usually implemented by looking in the
9196 -- table of abstract interface types plus the ancestor table contained in
9197 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
9199 function Tagged_Membership (N : Node_Id) return Node_Id is
9200 Left : constant Node_Id := Left_Opnd (N);
9201 Right : constant Node_Id := Right_Opnd (N);
9202 Loc : constant Source_Ptr := Sloc (N);
9204 Left_Type : Entity_Id;
9205 Right_Type : Entity_Id;
9209 Left_Type := Etype (Left);
9210 Right_Type := Etype (Right);
9212 if Is_Class_Wide_Type (Left_Type) then
9213 Left_Type := Root_Type (Left_Type);
9217 Make_Selected_Component (Loc,
9218 Prefix => Relocate_Node (Left),
9220 New_Reference_To (First_Tag_Component (Left_Type), Loc));
9222 if Is_Class_Wide_Type (Right_Type) then
9224 -- No need to issue a run-time check if we statically know that the
9225 -- result of this membership test is always true. For example,
9226 -- considering the following declarations:
9228 -- type Iface is interface;
9229 -- type T is tagged null record;
9230 -- type DT is new T and Iface with null record;
9235 -- These membership tests are always true:
9239 -- Obj2 in Iface'Class;
9241 -- We do not need to handle cases where the membership is illegal.
9244 -- Obj1 in DT'Class; -- Compile time error
9245 -- Obj1 in Iface'Class; -- Compile time error
9247 if not Is_Class_Wide_Type (Left_Type)
9248 and then (Is_Ancestor (Etype (Right_Type), Left_Type)
9249 or else (Is_Interface (Etype (Right_Type))
9250 and then Interface_Present_In_Ancestor
9252 Iface => Etype (Right_Type))))
9254 return New_Reference_To (Standard_True, Loc);
9257 -- Ada 2005 (AI-251): Class-wide applied to interfaces
9259 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
9261 -- Support to: "Iface_CW_Typ in Typ'Class"
9263 or else Is_Interface (Left_Type)
9265 -- Issue error if IW_Membership operation not available in a
9266 -- configurable run time setting.
9268 if not RTE_Available (RE_IW_Membership) then
9270 ("dynamic membership test on interface types", N);
9275 Make_Function_Call (Loc,
9276 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
9277 Parameter_Associations => New_List (
9278 Make_Attribute_Reference (Loc,
9280 Attribute_Name => Name_Address),
9283 (Access_Disp_Table (Root_Type (Right_Type)))),
9286 -- Ada 95: Normal case
9290 Build_CW_Membership (Loc,
9291 Obj_Tag_Node => Obj_Tag,
9295 (Access_Disp_Table (Root_Type (Right_Type)))),
9299 -- Right_Type is not a class-wide type
9302 -- No need to check the tag of the object if Right_Typ is abstract
9304 if Is_Abstract_Type (Right_Type) then
9305 return New_Reference_To (Standard_False, Loc);
9310 Left_Opnd => Obj_Tag,
9313 (Node (First_Elmt (Access_Disp_Table (Right_Type))), Loc));
9316 end Tagged_Membership;
9318 ------------------------------
9319 -- Unary_Op_Validity_Checks --
9320 ------------------------------
9322 procedure Unary_Op_Validity_Checks (N : Node_Id) is
9324 if Validity_Checks_On and Validity_Check_Operands then
9325 Ensure_Valid (Right_Opnd (N));
9327 end Unary_Op_Validity_Checks;