1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Debug
; use Debug
;
29 with Einfo
; use Einfo
;
30 with Elists
; use Elists
;
31 with Errout
; use Errout
;
32 with Exp_Aggr
; use Exp_Aggr
;
33 with Exp_Atag
; use Exp_Atag
;
34 with Exp_Ch2
; use Exp_Ch2
;
35 with Exp_Ch3
; use Exp_Ch3
;
36 with Exp_Ch6
; use Exp_Ch6
;
37 with Exp_Ch7
; use Exp_Ch7
;
38 with Exp_Ch9
; use Exp_Ch9
;
39 with Exp_Disp
; use Exp_Disp
;
40 with Exp_Fixd
; use Exp_Fixd
;
41 with Exp_Intr
; use Exp_Intr
;
42 with Exp_Pakd
; use Exp_Pakd
;
43 with Exp_Tss
; use Exp_Tss
;
44 with Exp_Util
; use Exp_Util
;
45 with Freeze
; use Freeze
;
46 with Inline
; use Inline
;
48 with Namet
; use Namet
;
49 with Nlists
; use Nlists
;
50 with Nmake
; use Nmake
;
52 with Par_SCO
; use Par_SCO
;
53 with Restrict
; use Restrict
;
54 with Rident
; use Rident
;
55 with Rtsfind
; use Rtsfind
;
57 with Sem_Aux
; use Sem_Aux
;
58 with Sem_Cat
; use Sem_Cat
;
59 with Sem_Ch3
; use Sem_Ch3
;
60 with Sem_Ch8
; use Sem_Ch8
;
61 with Sem_Ch13
; use Sem_Ch13
;
62 with Sem_Eval
; use Sem_Eval
;
63 with Sem_Res
; use Sem_Res
;
64 with Sem_Type
; use Sem_Type
;
65 with Sem_Util
; use Sem_Util
;
66 with Sem_Warn
; use Sem_Warn
;
67 with Sinfo
; use Sinfo
;
68 with Snames
; use Snames
;
69 with Stand
; use Stand
;
70 with SCIL_LL
; use SCIL_LL
;
71 with Targparm
; use Targparm
;
72 with Tbuild
; use Tbuild
;
73 with Ttypes
; use Ttypes
;
74 with Uintp
; use Uintp
;
75 with Urealp
; use Urealp
;
76 with Validsw
; use Validsw
;
78 package body Exp_Ch4
is
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 procedure Binary_Op_Validity_Checks
(N
: Node_Id
);
85 pragma Inline
(Binary_Op_Validity_Checks
);
86 -- Performs validity checks for a binary operator
88 procedure Build_Boolean_Array_Proc_Call
92 -- If a boolean array assignment can be done in place, build call to
93 -- corresponding library procedure.
95 function Current_Anonymous_Master
return Entity_Id
;
96 -- Return the entity of the heterogeneous finalization master belonging to
97 -- the current unit (either function, package or procedure). This master
98 -- services all anonymous access-to-controlled types. If the current unit
99 -- does not have such master, create one.
101 procedure Displace_Allocator_Pointer
(N
: Node_Id
);
102 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
103 -- Expand_Allocator_Expression. Allocating class-wide interface objects
104 -- this routine displaces the pointer to the allocated object to reference
105 -- the component referencing the corresponding secondary dispatch table.
107 procedure Expand_Allocator_Expression
(N
: Node_Id
);
108 -- Subsidiary to Expand_N_Allocator, for the case when the expression
109 -- is a qualified expression or an aggregate.
111 procedure Expand_Array_Comparison
(N
: Node_Id
);
112 -- This routine handles expansion of the comparison operators (N_Op_Lt,
113 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
114 -- code for these operators is similar, differing only in the details of
115 -- the actual comparison call that is made. Special processing (call a
118 function Expand_Array_Equality
123 Typ
: Entity_Id
) return Node_Id
;
124 -- Expand an array equality into a call to a function implementing this
125 -- equality, and a call to it. Loc is the location for the generated nodes.
126 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
127 -- on which to attach bodies of local functions that are created in the
128 -- process. It is the responsibility of the caller to insert those bodies
129 -- at the right place. Nod provides the Sloc value for the generated code.
130 -- Normally the types used for the generated equality routine are taken
131 -- from Lhs and Rhs. However, in some situations of generated code, the
132 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
133 -- the type to be used for the formal parameters.
135 procedure Expand_Boolean_Operator
(N
: Node_Id
);
136 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
137 -- case of array type arguments.
139 procedure Expand_Short_Circuit_Operator
(N
: Node_Id
);
140 -- Common expansion processing for short-circuit boolean operators
142 procedure Expand_Compare_Minimize_Eliminate_Overflow
(N
: Node_Id
);
143 -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is
144 -- where we allow comparison of "out of range" values.
146 function Expand_Composite_Equality
151 Bodies
: List_Id
) return Node_Id
;
152 -- Local recursive function used to expand equality for nested composite
153 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
154 -- to attach bodies of local functions that are created in the process. It
155 -- is the responsibility of the caller to insert those bodies at the right
156 -- place. Nod provides the Sloc value for generated code. Lhs and Rhs are
157 -- the left and right sides for the comparison, and Typ is the type of the
158 -- objects to compare.
160 procedure Expand_Concatenate
(Cnode
: Node_Id
; Opnds
: List_Id
);
161 -- Routine to expand concatenation of a sequence of two or more operands
162 -- (in the list Operands) and replace node Cnode with the result of the
163 -- concatenation. The operands can be of any appropriate type, and can
164 -- include both arrays and singleton elements.
166 procedure Expand_Membership_Minimize_Eliminate_Overflow
(N
: Node_Id
);
167 -- N is an N_In membership test mode, with the overflow check mode set to
168 -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed
169 -- integer type. This is a case where top level processing is required to
170 -- handle overflow checks in subtrees.
172 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
);
173 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
174 -- fixed. We do not have such a type at runtime, so the purpose of this
175 -- routine is to find the real type by looking up the tree. We also
176 -- determine if the operation must be rounded.
178 function Has_Inferable_Discriminants
(N
: Node_Id
) return Boolean;
179 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
180 -- discriminants if it has a constrained nominal type, unless the object
181 -- is a component of an enclosing Unchecked_Union object that is subject
182 -- to a per-object constraint and the enclosing object lacks inferable
185 -- An expression of an Unchecked_Union type has inferable discriminants
186 -- if it is either a name of an object with inferable discriminants or a
187 -- qualified expression whose subtype mark denotes a constrained subtype.
189 procedure Insert_Dereference_Action
(N
: Node_Id
);
190 -- N is an expression whose type is an access. When the type of the
191 -- associated storage pool is derived from Checked_Pool, generate a
192 -- call to the 'Dereference' primitive operation.
194 function Make_Array_Comparison_Op
196 Nod
: Node_Id
) return Node_Id
;
197 -- Comparisons between arrays are expanded in line. This function produces
198 -- the body of the implementation of (a > b), where a and b are one-
199 -- dimensional arrays of some discrete type. The original node is then
200 -- expanded into the appropriate call to this function. Nod provides the
201 -- Sloc value for the generated code.
203 function Make_Boolean_Array_Op
205 N
: Node_Id
) return Node_Id
;
206 -- Boolean operations on boolean arrays are expanded in line. This function
207 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
208 -- b). It is used only the normal case and not the packed case. The type
209 -- involved, Typ, is the Boolean array type, and the logical operations in
210 -- the body are simple boolean operations. Note that Typ is always a
211 -- constrained type (the caller has ensured this by using
212 -- Convert_To_Actual_Subtype if necessary).
214 function Minimized_Eliminated_Overflow_Check
(N
: Node_Id
) return Boolean;
215 -- For signed arithmetic operations when the current overflow mode is
216 -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks
217 -- as the first thing we do. We then return. We count on the recursive
218 -- apparatus for overflow checks to call us back with an equivalent
219 -- operation that is in CHECKED mode, avoiding a recursive entry into this
220 -- routine, and that is when we will proceed with the expansion of the
221 -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do
222 -- these optimizations without first making this check, since there may be
223 -- operands further down the tree that are relying on the recursive calls
224 -- triggered by the top level nodes to properly process overflow checking
225 -- and remaining expansion on these nodes. Note that this call back may be
226 -- skipped if the operation is done in Bignum mode but that's fine, since
227 -- the Bignum call takes care of everything.
229 procedure Optimize_Length_Comparison
(N
: Node_Id
);
230 -- Given an expression, if it is of the form X'Length op N (or the other
231 -- way round), where N is known at compile time to be 0 or 1, and X is a
232 -- simple entity, and op is a comparison operator, optimizes it into a
233 -- comparison of First and Last.
235 procedure Process_Transient_Object
238 -- Subsidiary routine to the expansion of expression_with_actions and if
239 -- expressions. Generate all the necessary code to finalize a transient
240 -- controlled object when the enclosing context is elaborated or evaluated.
241 -- Decl denotes the declaration of the transient controlled object which is
242 -- usually the result of a controlled function call. Rel_Node denotes the
243 -- context, either an expression_with_actions or an if expression.
245 procedure Rewrite_Comparison
(N
: Node_Id
);
246 -- If N is the node for a comparison whose outcome can be determined at
247 -- compile time, then the node N can be rewritten with True or False. If
248 -- the outcome cannot be determined at compile time, the call has no
249 -- effect. If N is a type conversion, then this processing is applied to
250 -- its expression. If N is neither comparison nor a type conversion, the
251 -- call has no effect.
253 procedure Tagged_Membership
255 SCIL_Node
: out Node_Id
;
256 Result
: out Node_Id
);
257 -- Construct the expression corresponding to the tagged membership test.
258 -- Deals with a second operand being (or not) a class-wide type.
260 function Safe_In_Place_Array_Op
263 Op2
: Node_Id
) return Boolean;
264 -- In the context of an assignment, where the right-hand side is a boolean
265 -- operation on arrays, check whether operation can be performed in place.
267 procedure Unary_Op_Validity_Checks
(N
: Node_Id
);
268 pragma Inline
(Unary_Op_Validity_Checks
);
269 -- Performs validity checks for a unary operator
271 -------------------------------
272 -- Binary_Op_Validity_Checks --
273 -------------------------------
275 procedure Binary_Op_Validity_Checks
(N
: Node_Id
) is
277 if Validity_Checks_On
and Validity_Check_Operands
then
278 Ensure_Valid
(Left_Opnd
(N
));
279 Ensure_Valid
(Right_Opnd
(N
));
281 end Binary_Op_Validity_Checks
;
283 ------------------------------------
284 -- Build_Boolean_Array_Proc_Call --
285 ------------------------------------
287 procedure Build_Boolean_Array_Proc_Call
292 Loc
: constant Source_Ptr
:= Sloc
(N
);
293 Kind
: constant Node_Kind
:= Nkind
(Expression
(N
));
294 Target
: constant Node_Id
:=
295 Make_Attribute_Reference
(Loc
,
297 Attribute_Name
=> Name_Address
);
299 Arg1
: Node_Id
:= Op1
;
300 Arg2
: Node_Id
:= Op2
;
302 Proc_Name
: Entity_Id
;
305 if Kind
= N_Op_Not
then
306 if Nkind
(Op1
) in N_Binary_Op
then
308 -- Use negated version of the binary operators
310 if Nkind
(Op1
) = N_Op_And
then
311 Proc_Name
:= RTE
(RE_Vector_Nand
);
313 elsif Nkind
(Op1
) = N_Op_Or
then
314 Proc_Name
:= RTE
(RE_Vector_Nor
);
316 else pragma Assert
(Nkind
(Op1
) = N_Op_Xor
);
317 Proc_Name
:= RTE
(RE_Vector_Xor
);
321 Make_Procedure_Call_Statement
(Loc
,
322 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
324 Parameter_Associations
=> New_List
(
326 Make_Attribute_Reference
(Loc
,
327 Prefix
=> Left_Opnd
(Op1
),
328 Attribute_Name
=> Name_Address
),
330 Make_Attribute_Reference
(Loc
,
331 Prefix
=> Right_Opnd
(Op1
),
332 Attribute_Name
=> Name_Address
),
334 Make_Attribute_Reference
(Loc
,
335 Prefix
=> Left_Opnd
(Op1
),
336 Attribute_Name
=> Name_Length
)));
339 Proc_Name
:= RTE
(RE_Vector_Not
);
342 Make_Procedure_Call_Statement
(Loc
,
343 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
344 Parameter_Associations
=> New_List
(
347 Make_Attribute_Reference
(Loc
,
349 Attribute_Name
=> Name_Address
),
351 Make_Attribute_Reference
(Loc
,
353 Attribute_Name
=> Name_Length
)));
357 -- We use the following equivalences:
359 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
360 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
361 -- (not X) xor (not Y) = X xor Y
362 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
364 if Nkind
(Op1
) = N_Op_Not
then
365 Arg1
:= Right_Opnd
(Op1
);
366 Arg2
:= Right_Opnd
(Op2
);
368 if Kind
= N_Op_And
then
369 Proc_Name
:= RTE
(RE_Vector_Nor
);
370 elsif Kind
= N_Op_Or
then
371 Proc_Name
:= RTE
(RE_Vector_Nand
);
373 Proc_Name
:= RTE
(RE_Vector_Xor
);
377 if Kind
= N_Op_And
then
378 Proc_Name
:= RTE
(RE_Vector_And
);
379 elsif Kind
= N_Op_Or
then
380 Proc_Name
:= RTE
(RE_Vector_Or
);
381 elsif Nkind
(Op2
) = N_Op_Not
then
382 Proc_Name
:= RTE
(RE_Vector_Nxor
);
383 Arg2
:= Right_Opnd
(Op2
);
385 Proc_Name
:= RTE
(RE_Vector_Xor
);
390 Make_Procedure_Call_Statement
(Loc
,
391 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
392 Parameter_Associations
=> New_List
(
394 Make_Attribute_Reference
(Loc
,
396 Attribute_Name
=> Name_Address
),
397 Make_Attribute_Reference
(Loc
,
399 Attribute_Name
=> Name_Address
),
400 Make_Attribute_Reference
(Loc
,
402 Attribute_Name
=> Name_Length
)));
405 Rewrite
(N
, Call_Node
);
409 when RE_Not_Available
=>
411 end Build_Boolean_Array_Proc_Call
;
413 ------------------------------
414 -- Current_Anonymous_Master --
415 ------------------------------
417 function Current_Anonymous_Master
return Entity_Id
is
425 Unit_Id
:= Cunit_Entity
(Current_Sem_Unit
);
427 -- Find the entity of the current unit
429 if Ekind
(Unit_Id
) = E_Subprogram_Body
then
431 -- When processing subprogram bodies, the proper scope is always that
434 Subp_Body
:= Unit_Id
;
435 while Present
(Subp_Body
)
436 and then Nkind
(Subp_Body
) /= N_Subprogram_Body
438 Subp_Body
:= Parent
(Subp_Body
);
441 Unit_Id
:= Corresponding_Spec
(Subp_Body
);
444 Loc
:= Sloc
(Unit_Id
);
445 Unit_Decl
:= Unit
(Cunit
(Current_Sem_Unit
));
447 -- Find the declarations list of the current unit
449 if Nkind
(Unit_Decl
) = N_Package_Declaration
then
450 Unit_Decl
:= Specification
(Unit_Decl
);
451 Decls
:= Visible_Declarations
(Unit_Decl
);
454 Decls
:= New_List
(Make_Null_Statement
(Loc
));
455 Set_Visible_Declarations
(Unit_Decl
, Decls
);
457 elsif Is_Empty_List
(Decls
) then
458 Append_To
(Decls
, Make_Null_Statement
(Loc
));
462 Decls
:= Declarations
(Unit_Decl
);
465 Decls
:= New_List
(Make_Null_Statement
(Loc
));
466 Set_Declarations
(Unit_Decl
, Decls
);
468 elsif Is_Empty_List
(Decls
) then
469 Append_To
(Decls
, Make_Null_Statement
(Loc
));
473 -- The current unit has an existing anonymous master, traverse its
474 -- declarations and locate the entity.
476 if Has_Anonymous_Master
(Unit_Id
) then
479 Fin_Mas_Id
: Entity_Id
;
482 Decl
:= First
(Decls
);
483 while Present
(Decl
) loop
485 -- Look for the first variable in the declarations whole type
486 -- is Finalization_Master.
488 if Nkind
(Decl
) = N_Object_Declaration
then
489 Fin_Mas_Id
:= Defining_Identifier
(Decl
);
491 if Ekind
(Fin_Mas_Id
) = E_Variable
492 and then Etype
(Fin_Mas_Id
) = RTE
(RE_Finalization_Master
)
501 -- The master was not found even though the unit was labeled as
507 -- Create a new anonymous master
511 First_Decl
: constant Node_Id
:= First
(Decls
);
513 Fin_Mas_Id
: Entity_Id
;
516 -- Since the master and its associated initialization is inserted
517 -- at top level, use the scope of the unit when analyzing.
519 Push_Scope
(Unit_Id
);
521 -- Create the finalization master
524 Make_Defining_Identifier
(Loc
,
525 Chars
=> New_External_Name
(Chars
(Unit_Id
), "AM"));
528 -- <Fin_Mas_Id> : Finalization_Master;
531 Make_Object_Declaration
(Loc
,
532 Defining_Identifier
=> Fin_Mas_Id
,
534 New_Occurrence_Of
(RTE
(RE_Finalization_Master
), Loc
));
536 Insert_Before_And_Analyze
(First_Decl
, Action
);
538 -- Mark the unit to prevent the generation of multiple masters
540 Set_Has_Anonymous_Master
(Unit_Id
);
542 -- Do not set the base pool and mode of operation on .NET/JVM
543 -- since those targets do not support pools and all VM masters
544 -- are heterogeneous by default.
546 if VM_Target
= No_VM
then
550 -- (<Fin_Mas_Id>, Global_Pool_Object'Unrestricted_Access);
553 Make_Procedure_Call_Statement
(Loc
,
555 New_Occurrence_Of
(RTE
(RE_Set_Base_Pool
), Loc
),
557 Parameter_Associations
=> New_List
(
558 New_Occurrence_Of
(Fin_Mas_Id
, Loc
),
559 Make_Attribute_Reference
(Loc
,
561 New_Occurrence_Of
(RTE
(RE_Global_Pool_Object
), Loc
),
562 Attribute_Name
=> Name_Unrestricted_Access
)));
564 Insert_Before_And_Analyze
(First_Decl
, Action
);
567 -- Set_Is_Heterogeneous (<Fin_Mas_Id>);
570 Make_Procedure_Call_Statement
(Loc
,
572 New_Occurrence_Of
(RTE
(RE_Set_Is_Heterogeneous
), Loc
),
573 Parameter_Associations
=> New_List
(
574 New_Occurrence_Of
(Fin_Mas_Id
, Loc
)));
576 Insert_Before_And_Analyze
(First_Decl
, Action
);
579 -- Restore the original state of the scope stack
586 end Current_Anonymous_Master
;
588 --------------------------------
589 -- Displace_Allocator_Pointer --
590 --------------------------------
592 procedure Displace_Allocator_Pointer
(N
: Node_Id
) is
593 Loc
: constant Source_Ptr
:= Sloc
(N
);
594 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
600 -- Do nothing in case of VM targets: the virtual machine will handle
601 -- interfaces directly.
603 if not Tagged_Type_Expansion
then
607 pragma Assert
(Nkind
(N
) = N_Identifier
608 and then Nkind
(Orig_Node
) = N_Allocator
);
610 PtrT
:= Etype
(Orig_Node
);
611 Dtyp
:= Available_View
(Designated_Type
(PtrT
));
612 Etyp
:= Etype
(Expression
(Orig_Node
));
614 if Is_Class_Wide_Type
(Dtyp
) and then Is_Interface
(Dtyp
) then
616 -- If the type of the allocator expression is not an interface type
617 -- we can generate code to reference the record component containing
618 -- the pointer to the secondary dispatch table.
620 if not Is_Interface
(Etyp
) then
622 Saved_Typ
: constant Entity_Id
:= Etype
(Orig_Node
);
625 -- 1) Get access to the allocated object
628 Make_Explicit_Dereference
(Loc
, Relocate_Node
(N
)));
632 -- 2) Add the conversion to displace the pointer to reference
633 -- the secondary dispatch table.
635 Rewrite
(N
, Convert_To
(Dtyp
, Relocate_Node
(N
)));
636 Analyze_And_Resolve
(N
, Dtyp
);
638 -- 3) The 'access to the secondary dispatch table will be used
639 -- as the value returned by the allocator.
642 Make_Attribute_Reference
(Loc
,
643 Prefix
=> Relocate_Node
(N
),
644 Attribute_Name
=> Name_Access
));
645 Set_Etype
(N
, Saved_Typ
);
649 -- If the type of the allocator expression is an interface type we
650 -- generate a run-time call to displace "this" to reference the
651 -- component containing the pointer to the secondary dispatch table
652 -- or else raise Constraint_Error if the actual object does not
653 -- implement the target interface. This case corresponds to the
654 -- following example:
656 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
658 -- return new Iface_2'Class'(Obj);
663 Unchecked_Convert_To
(PtrT
,
664 Make_Function_Call
(Loc
,
665 Name
=> New_Occurrence_Of
(RTE
(RE_Displace
), Loc
),
666 Parameter_Associations
=> New_List
(
667 Unchecked_Convert_To
(RTE
(RE_Address
),
673 (Access_Disp_Table
(Etype
(Base_Type
(Dtyp
))))),
675 Analyze_And_Resolve
(N
, PtrT
);
678 end Displace_Allocator_Pointer
;
680 ---------------------------------
681 -- Expand_Allocator_Expression --
682 ---------------------------------
684 procedure Expand_Allocator_Expression
(N
: Node_Id
) is
685 Loc
: constant Source_Ptr
:= Sloc
(N
);
686 Exp
: constant Node_Id
:= Expression
(Expression
(N
));
687 PtrT
: constant Entity_Id
:= Etype
(N
);
688 DesigT
: constant Entity_Id
:= Designated_Type
(PtrT
);
690 procedure Apply_Accessibility_Check
692 Built_In_Place
: Boolean := False);
693 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
694 -- type, generate an accessibility check to verify that the level of the
695 -- type of the created object is not deeper than the level of the access
696 -- type. If the type of the qualified expression is class-wide, then
697 -- always generate the check (except in the case where it is known to be
698 -- unnecessary, see comment below). Otherwise, only generate the check
699 -- if the level of the qualified expression type is statically deeper
700 -- than the access type.
702 -- Although the static accessibility will generally have been performed
703 -- as a legality check, it won't have been done in cases where the
704 -- allocator appears in generic body, so a run-time check is needed in
705 -- general. One special case is when the access type is declared in the
706 -- same scope as the class-wide allocator, in which case the check can
707 -- never fail, so it need not be generated.
709 -- As an open issue, there seem to be cases where the static level
710 -- associated with the class-wide object's underlying type is not
711 -- sufficient to perform the proper accessibility check, such as for
712 -- allocators in nested subprograms or accept statements initialized by
713 -- class-wide formals when the actual originates outside at a deeper
714 -- static level. The nested subprogram case might require passing
715 -- accessibility levels along with class-wide parameters, and the task
716 -- case seems to be an actual gap in the language rules that needs to
717 -- be fixed by the ARG. ???
719 -------------------------------
720 -- Apply_Accessibility_Check --
721 -------------------------------
723 procedure Apply_Accessibility_Check
725 Built_In_Place
: Boolean := False)
727 Pool_Id
: constant Entity_Id
:= Associated_Storage_Pool
(PtrT
);
735 if Ada_Version
>= Ada_2005
736 and then Is_Class_Wide_Type
(DesigT
)
737 and then (Tagged_Type_Expansion
or else VM_Target
/= No_VM
)
738 and then not Scope_Suppress
.Suppress
(Accessibility_Check
)
740 (Type_Access_Level
(Etype
(Exp
)) > Type_Access_Level
(PtrT
)
742 (Is_Class_Wide_Type
(Etype
(Exp
))
743 and then Scope
(PtrT
) /= Current_Scope
))
745 -- If the allocator was built in place, Ref is already a reference
746 -- to the access object initialized to the result of the allocator
747 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
748 -- Remove_Side_Effects for cases where the build-in-place call may
749 -- still be the prefix of the reference (to avoid generating
750 -- duplicate calls). Otherwise, it is the entity associated with
751 -- the object containing the address of the allocated object.
753 if Built_In_Place
then
754 Remove_Side_Effects
(Ref
);
755 Obj_Ref
:= New_Copy_Tree
(Ref
);
757 Obj_Ref
:= New_Occurrence_Of
(Ref
, Loc
);
760 -- For access to interface types we must generate code to displace
761 -- the pointer to the base of the object since the subsequent code
762 -- references components located in the TSD of the object (which
763 -- is associated with the primary dispatch table --see a-tags.ads)
764 -- and also generates code invoking Free, which requires also a
765 -- reference to the base of the unallocated object.
767 if Is_Interface
(DesigT
) and then Tagged_Type_Expansion
then
769 Unchecked_Convert_To
(Etype
(Obj_Ref
),
770 Make_Function_Call
(Loc
,
772 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
773 Parameter_Associations
=> New_List
(
774 Unchecked_Convert_To
(RTE
(RE_Address
),
775 New_Copy_Tree
(Obj_Ref
)))));
778 -- Step 1: Create the object clean up code
782 -- Deallocate the object if the accessibility check fails. This
783 -- is done only on targets or profiles that support deallocation.
787 if RTE_Available
(RE_Free
) then
788 Free_Stmt
:= Make_Free_Statement
(Loc
, New_Copy_Tree
(Obj_Ref
));
789 Set_Storage_Pool
(Free_Stmt
, Pool_Id
);
791 Append_To
(Stmts
, Free_Stmt
);
793 -- The target or profile cannot deallocate objects
799 -- Finalize the object if applicable. Generate:
801 -- [Deep_]Finalize (Obj_Ref.all);
803 if Needs_Finalization
(DesigT
) then
807 Make_Explicit_Dereference
(Loc
, New_Copy
(Obj_Ref
)),
810 -- When the target or profile supports deallocation, wrap the
811 -- finalization call in a block to ensure proper deallocation
812 -- even if finalization fails. Generate:
822 if Present
(Free_Stmt
) then
824 Make_Block_Statement
(Loc
,
825 Handled_Statement_Sequence
=>
826 Make_Handled_Sequence_Of_Statements
(Loc
,
827 Statements
=> New_List
(Fin_Call
),
829 Exception_Handlers
=> New_List
(
830 Make_Exception_Handler
(Loc
,
831 Exception_Choices
=> New_List
(
832 Make_Others_Choice
(Loc
)),
834 Statements
=> New_List
(
835 New_Copy_Tree
(Free_Stmt
),
836 Make_Raise_Statement
(Loc
))))));
839 Prepend_To
(Stmts
, Fin_Call
);
842 -- Signal the accessibility failure through a Program_Error
845 Make_Raise_Program_Error
(Loc
,
846 Condition
=> New_Occurrence_Of
(Standard_True
, Loc
),
847 Reason
=> PE_Accessibility_Check_Failed
));
849 -- Step 2: Create the accessibility comparison
855 Make_Attribute_Reference
(Loc
,
857 Attribute_Name
=> Name_Tag
);
859 -- For tagged types, determine the accessibility level by looking
860 -- at the type specific data of the dispatch table. Generate:
862 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
864 if Tagged_Type_Expansion
then
865 Cond
:= Build_Get_Access_Level
(Loc
, Obj_Ref
);
867 -- Use a runtime call to determine the accessibility level when
868 -- compiling on virtual machine targets. Generate:
870 -- Get_Access_Level (Ref'Tag)
874 Make_Function_Call
(Loc
,
876 New_Occurrence_Of
(RTE
(RE_Get_Access_Level
), Loc
),
877 Parameter_Associations
=> New_List
(Obj_Ref
));
884 Make_Integer_Literal
(Loc
, Type_Access_Level
(PtrT
)));
886 -- Due to the complexity and side effects of the check, utilize an
887 -- if statement instead of the regular Program_Error circuitry.
890 Make_Implicit_If_Statement
(N
,
892 Then_Statements
=> Stmts
));
894 end Apply_Accessibility_Check
;
898 Aggr_In_Place
: constant Boolean := Is_Delayed_Aggregate
(Exp
);
899 Indic
: constant Node_Id
:= Subtype_Mark
(Expression
(N
));
900 T
: constant Entity_Id
:= Entity
(Indic
);
902 Tag_Assign
: Node_Id
;
906 TagT
: Entity_Id
:= Empty
;
907 -- Type used as source for tag assignment
909 TagR
: Node_Id
:= Empty
;
910 -- Target reference for tag assignment
912 -- Start of processing for Expand_Allocator_Expression
915 -- Handle call to C++ constructor
917 if Is_CPP_Constructor_Call
(Exp
) then
918 Make_CPP_Constructor_Call_In_Allocator
920 Function_Call
=> Exp
);
924 -- In the case of an Ada 2012 allocator whose initial value comes from a
925 -- function call, pass "the accessibility level determined by the point
926 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
927 -- Expand_Call but it couldn't be done there (because the Etype of the
928 -- allocator wasn't set then) so we generate the parameter here. See
929 -- the Boolean variable Defer in (a block within) Expand_Call.
931 if Ada_Version
>= Ada_2012
and then Nkind
(Exp
) = N_Function_Call
then
936 if Nkind
(Name
(Exp
)) = N_Explicit_Dereference
then
937 Subp
:= Designated_Type
(Etype
(Prefix
(Name
(Exp
))));
939 Subp
:= Entity
(Name
(Exp
));
942 Subp
:= Ultimate_Alias
(Subp
);
944 if Present
(Extra_Accessibility_Of_Result
(Subp
)) then
945 Add_Extra_Actual_To_Call
946 (Subprogram_Call
=> Exp
,
947 Extra_Formal
=> Extra_Accessibility_Of_Result
(Subp
),
948 Extra_Actual
=> Dynamic_Accessibility_Level
(PtrT
));
953 -- Case of tagged type or type requiring finalization
955 if Is_Tagged_Type
(T
) or else Needs_Finalization
(T
) then
957 -- Ada 2005 (AI-318-02): If the initialization expression is a call
958 -- to a build-in-place function, then access to the allocated object
959 -- must be passed to the function. Currently we limit such functions
960 -- to those with constrained limited result subtypes, but eventually
961 -- we plan to expand the allowed forms of functions that are treated
962 -- as build-in-place.
964 if Ada_Version
>= Ada_2005
965 and then Is_Build_In_Place_Function_Call
(Exp
)
967 Make_Build_In_Place_Call_In_Allocator
(N
, Exp
);
968 Apply_Accessibility_Check
(N
, Built_In_Place
=> True);
972 -- Actions inserted before:
973 -- Temp : constant ptr_T := new T'(Expression);
974 -- Temp._tag = T'tag; -- when not class-wide
975 -- [Deep_]Adjust (Temp.all);
977 -- We analyze by hand the new internal allocator to avoid any
978 -- recursion and inappropriate call to Initialize.
980 -- We don't want to remove side effects when the expression must be
981 -- built in place. In the case of a build-in-place function call,
982 -- that could lead to a duplication of the call, which was already
983 -- substituted for the allocator.
985 if not Aggr_In_Place
then
986 Remove_Side_Effects
(Exp
);
989 Temp
:= Make_Temporary
(Loc
, 'P', N
);
991 -- For a class wide allocation generate the following code:
993 -- type Equiv_Record is record ... end record;
994 -- implicit subtype CW is <Class_Wide_Subytpe>;
995 -- temp : PtrT := new CW'(CW!(expr));
997 if Is_Class_Wide_Type
(T
) then
998 Expand_Subtype_From_Expr
(Empty
, T
, Indic
, Exp
);
1000 -- Ada 2005 (AI-251): If the expression is a class-wide interface
1001 -- object we generate code to move up "this" to reference the
1002 -- base of the object before allocating the new object.
1004 -- Note that Exp'Address is recursively expanded into a call
1005 -- to Base_Address (Exp.Tag)
1007 if Is_Class_Wide_Type
(Etype
(Exp
))
1008 and then Is_Interface
(Etype
(Exp
))
1009 and then Tagged_Type_Expansion
1013 Unchecked_Convert_To
(Entity
(Indic
),
1014 Make_Explicit_Dereference
(Loc
,
1015 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
1016 Make_Attribute_Reference
(Loc
,
1018 Attribute_Name
=> Name_Address
)))));
1022 Unchecked_Convert_To
(Entity
(Indic
), Exp
));
1025 Analyze_And_Resolve
(Expression
(N
), Entity
(Indic
));
1028 -- Processing for allocators returning non-interface types
1030 if not Is_Interface
(Directly_Designated_Type
(PtrT
)) then
1031 if Aggr_In_Place
then
1033 Make_Object_Declaration
(Loc
,
1034 Defining_Identifier
=> Temp
,
1035 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
1037 Make_Allocator
(Loc
,
1039 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
1041 -- Copy the Comes_From_Source flag for the allocator we just
1042 -- built, since logically this allocator is a replacement of
1043 -- the original allocator node. This is for proper handling of
1044 -- restriction No_Implicit_Heap_Allocations.
1046 Set_Comes_From_Source
1047 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
1049 Set_No_Initialization
(Expression
(Temp_Decl
));
1050 Insert_Action
(N
, Temp_Decl
);
1052 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1053 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
1055 -- Attach the object to the associated finalization master.
1056 -- This is done manually on .NET/JVM since those compilers do
1057 -- no support pools and can't benefit from internally generated
1058 -- Allocate / Deallocate procedures.
1060 if VM_Target
/= No_VM
1061 and then Is_Controlled
(DesigT
)
1062 and then Present
(Finalization_Master
(PtrT
))
1066 (Obj_Ref
=> New_Occurrence_Of
(Temp
, Loc
),
1071 Node
:= Relocate_Node
(N
);
1072 Set_Analyzed
(Node
);
1075 Make_Object_Declaration
(Loc
,
1076 Defining_Identifier
=> Temp
,
1077 Constant_Present
=> True,
1078 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
1079 Expression
=> Node
);
1081 Insert_Action
(N
, Temp_Decl
);
1082 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1084 -- Attach the object to the associated finalization master.
1085 -- This is done manually on .NET/JVM since those compilers do
1086 -- no support pools and can't benefit from internally generated
1087 -- Allocate / Deallocate procedures.
1089 if VM_Target
/= No_VM
1090 and then Is_Controlled
(DesigT
)
1091 and then Present
(Finalization_Master
(PtrT
))
1095 (Obj_Ref
=> New_Occurrence_Of
(Temp
, Loc
),
1100 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
1101 -- interface type. In this case we use the type of the qualified
1102 -- expression to allocate the object.
1106 Def_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
1111 Make_Full_Type_Declaration
(Loc
,
1112 Defining_Identifier
=> Def_Id
,
1114 Make_Access_To_Object_Definition
(Loc
,
1115 All_Present
=> True,
1116 Null_Exclusion_Present
=> False,
1118 Is_Access_Constant
(Etype
(N
)),
1119 Subtype_Indication
=>
1120 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
1122 Insert_Action
(N
, New_Decl
);
1124 -- Inherit the allocation-related attributes from the original
1127 Set_Finalization_Master
1128 (Def_Id
, Finalization_Master
(PtrT
));
1130 Set_Associated_Storage_Pool
1131 (Def_Id
, Associated_Storage_Pool
(PtrT
));
1133 -- Declare the object using the previous type declaration
1135 if Aggr_In_Place
then
1137 Make_Object_Declaration
(Loc
,
1138 Defining_Identifier
=> Temp
,
1139 Object_Definition
=> New_Occurrence_Of
(Def_Id
, Loc
),
1141 Make_Allocator
(Loc
,
1142 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
1144 -- Copy the Comes_From_Source flag for the allocator we just
1145 -- built, since logically this allocator is a replacement of
1146 -- the original allocator node. This is for proper handling
1147 -- of restriction No_Implicit_Heap_Allocations.
1149 Set_Comes_From_Source
1150 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
1152 Set_No_Initialization
(Expression
(Temp_Decl
));
1153 Insert_Action
(N
, Temp_Decl
);
1155 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1156 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
1159 Node
:= Relocate_Node
(N
);
1160 Set_Analyzed
(Node
);
1163 Make_Object_Declaration
(Loc
,
1164 Defining_Identifier
=> Temp
,
1165 Constant_Present
=> True,
1166 Object_Definition
=> New_Occurrence_Of
(Def_Id
, Loc
),
1167 Expression
=> Node
);
1169 Insert_Action
(N
, Temp_Decl
);
1170 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1173 -- Generate an additional object containing the address of the
1174 -- returned object. The type of this second object declaration
1175 -- is the correct type required for the common processing that
1176 -- is still performed by this subprogram. The displacement of
1177 -- this pointer to reference the component associated with the
1178 -- interface type will be done at the end of common processing.
1181 Make_Object_Declaration
(Loc
,
1182 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
1183 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
1185 Unchecked_Convert_To
(PtrT
,
1186 New_Occurrence_Of
(Temp
, Loc
)));
1188 Insert_Action
(N
, New_Decl
);
1190 Temp_Decl
:= New_Decl
;
1191 Temp
:= Defining_Identifier
(New_Decl
);
1195 Apply_Accessibility_Check
(Temp
);
1197 -- Generate the tag assignment
1199 -- Suppress the tag assignment when VM_Target because VM tags are
1200 -- represented implicitly in objects.
1202 if not Tagged_Type_Expansion
then
1205 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1206 -- interface objects because in this case the tag does not change.
1208 elsif Is_Interface
(Directly_Designated_Type
(Etype
(N
))) then
1209 pragma Assert
(Is_Class_Wide_Type
1210 (Directly_Designated_Type
(Etype
(N
))));
1213 elsif Is_Tagged_Type
(T
) and then not Is_Class_Wide_Type
(T
) then
1215 TagR
:= New_Occurrence_Of
(Temp
, Loc
);
1217 elsif Is_Private_Type
(T
)
1218 and then Is_Tagged_Type
(Underlying_Type
(T
))
1220 TagT
:= Underlying_Type
(T
);
1222 Unchecked_Convert_To
(Underlying_Type
(T
),
1223 Make_Explicit_Dereference
(Loc
,
1224 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
1227 if Present
(TagT
) then
1229 Full_T
: constant Entity_Id
:= Underlying_Type
(TagT
);
1233 Make_Assignment_Statement
(Loc
,
1235 Make_Selected_Component
(Loc
,
1239 (First_Tag_Component
(Full_T
), Loc
)),
1242 Unchecked_Convert_To
(RTE
(RE_Tag
),
1245 (First_Elmt
(Access_Disp_Table
(Full_T
))), Loc
)));
1248 -- The previous assignment has to be done in any case
1250 Set_Assignment_OK
(Name
(Tag_Assign
));
1251 Insert_Action
(N
, Tag_Assign
);
1254 if Needs_Finalization
(DesigT
) and then Needs_Finalization
(T
) then
1256 -- Generate an Adjust call if the object will be moved. In Ada
1257 -- 2005, the object may be inherently limited, in which case
1258 -- there is no Adjust procedure, and the object is built in
1259 -- place. In Ada 95, the object can be limited but not
1260 -- inherently limited if this allocator came from a return
1261 -- statement (we're allocating the result on the secondary
1262 -- stack). In that case, the object will be moved, so we _do_
1265 if not Aggr_In_Place
1266 and then not Is_Limited_View
(T
)
1270 -- An unchecked conversion is needed in the classwide case
1271 -- because the designated type can be an ancestor of the
1272 -- subtype mark of the allocator.
1276 Unchecked_Convert_To
(T
,
1277 Make_Explicit_Dereference
(Loc
,
1278 Prefix
=> New_Occurrence_Of
(Temp
, Loc
))),
1283 -- Set_Finalize_Address (<PtrT>FM, <T>FD'Unrestricted_Access);
1285 -- Do not generate this call in the following cases:
1287 -- * .NET/JVM - these targets do not support address arithmetic
1288 -- and unchecked conversion, key elements of Finalize_Address.
1290 -- * CodePeer mode - TSS primitive Finalize_Address is not
1291 -- created in this mode.
1293 if VM_Target
= No_VM
1294 and then not CodePeer_Mode
1295 and then Present
(Finalization_Master
(PtrT
))
1296 and then Present
(Temp_Decl
)
1297 and then Nkind
(Expression
(Temp_Decl
)) = N_Allocator
1300 Make_Set_Finalize_Address_Call
1307 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
1308 Analyze_And_Resolve
(N
, PtrT
);
1310 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1311 -- component containing the secondary dispatch table of the interface
1314 if Is_Interface
(Directly_Designated_Type
(PtrT
)) then
1315 Displace_Allocator_Pointer
(N
);
1318 elsif Aggr_In_Place
then
1319 Temp
:= Make_Temporary
(Loc
, 'P', N
);
1321 Make_Object_Declaration
(Loc
,
1322 Defining_Identifier
=> Temp
,
1323 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
1325 Make_Allocator
(Loc
,
1326 Expression
=> New_Occurrence_Of
(Etype
(Exp
), Loc
)));
1328 -- Copy the Comes_From_Source flag for the allocator we just built,
1329 -- since logically this allocator is a replacement of the original
1330 -- allocator node. This is for proper handling of restriction
1331 -- No_Implicit_Heap_Allocations.
1333 Set_Comes_From_Source
1334 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
1336 Set_No_Initialization
(Expression
(Temp_Decl
));
1337 Insert_Action
(N
, Temp_Decl
);
1339 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1340 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
1342 -- Attach the object to the associated finalization master. Thisis
1343 -- done manually on .NET/JVM since those compilers do no support
1344 -- pools and cannot benefit from internally generated Allocate and
1345 -- Deallocate procedures.
1347 if VM_Target
/= No_VM
1348 and then Is_Controlled
(DesigT
)
1349 and then Present
(Finalization_Master
(PtrT
))
1353 (Obj_Ref
=> New_Occurrence_Of
(Temp
, Loc
),
1357 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
1358 Analyze_And_Resolve
(N
, PtrT
);
1360 elsif Is_Access_Type
(T
) and then Can_Never_Be_Null
(T
) then
1361 Install_Null_Excluding_Check
(Exp
);
1363 elsif Is_Access_Type
(DesigT
)
1364 and then Nkind
(Exp
) = N_Allocator
1365 and then Nkind
(Expression
(Exp
)) /= N_Qualified_Expression
1367 -- Apply constraint to designated subtype indication
1369 Apply_Constraint_Check
1370 (Expression
(Exp
), Designated_Type
(DesigT
), No_Sliding
=> True);
1372 if Nkind
(Expression
(Exp
)) = N_Raise_Constraint_Error
then
1374 -- Propagate constraint_error to enclosing allocator
1376 Rewrite
(Exp
, New_Copy
(Expression
(Exp
)));
1380 Build_Allocate_Deallocate_Proc
(N
, True);
1383 -- type A is access T1;
1384 -- X : A := new T2'(...);
1385 -- T1 and T2 can be different subtypes, and we might need to check
1386 -- both constraints. First check against the type of the qualified
1389 Apply_Constraint_Check
(Exp
, T
, No_Sliding
=> True);
1391 if Do_Range_Check
(Exp
) then
1392 Generate_Range_Check
(Exp
, DesigT
, CE_Range_Check_Failed
);
1395 -- A check is also needed in cases where the designated subtype is
1396 -- constrained and differs from the subtype given in the qualified
1397 -- expression. Note that the check on the qualified expression does
1398 -- not allow sliding, but this check does (a relaxation from Ada 83).
1400 if Is_Constrained
(DesigT
)
1401 and then not Subtypes_Statically_Match
(T
, DesigT
)
1403 Apply_Constraint_Check
1404 (Exp
, DesigT
, No_Sliding
=> False);
1406 if Do_Range_Check
(Exp
) then
1407 Generate_Range_Check
(Exp
, DesigT
, CE_Range_Check_Failed
);
1411 -- For an access to unconstrained packed array, GIGI needs to see an
1412 -- expression with a constrained subtype in order to compute the
1413 -- proper size for the allocator.
1415 if Is_Array_Type
(T
)
1416 and then not Is_Constrained
(T
)
1417 and then Is_Packed
(T
)
1420 ConstrT
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
1421 Internal_Exp
: constant Node_Id
:= Relocate_Node
(Exp
);
1424 Make_Subtype_Declaration
(Loc
,
1425 Defining_Identifier
=> ConstrT
,
1426 Subtype_Indication
=>
1427 Make_Subtype_From_Expr
(Internal_Exp
, T
)));
1428 Freeze_Itype
(ConstrT
, Exp
);
1429 Rewrite
(Exp
, OK_Convert_To
(ConstrT
, Internal_Exp
));
1433 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1434 -- to a build-in-place function, then access to the allocated object
1435 -- must be passed to the function. Currently we limit such functions
1436 -- to those with constrained limited result subtypes, but eventually
1437 -- we plan to expand the allowed forms of functions that are treated
1438 -- as build-in-place.
1440 if Ada_Version
>= Ada_2005
1441 and then Is_Build_In_Place_Function_Call
(Exp
)
1443 Make_Build_In_Place_Call_In_Allocator
(N
, Exp
);
1448 when RE_Not_Available
=>
1450 end Expand_Allocator_Expression
;
1452 -----------------------------
1453 -- Expand_Array_Comparison --
1454 -----------------------------
1456 -- Expansion is only required in the case of array types. For the unpacked
1457 -- case, an appropriate runtime routine is called. For packed cases, and
1458 -- also in some other cases where a runtime routine cannot be called, the
1459 -- form of the expansion is:
1461 -- [body for greater_nn; boolean_expression]
1463 -- The body is built by Make_Array_Comparison_Op, and the form of the
1464 -- Boolean expression depends on the operator involved.
1466 procedure Expand_Array_Comparison
(N
: Node_Id
) is
1467 Loc
: constant Source_Ptr
:= Sloc
(N
);
1468 Op1
: Node_Id
:= Left_Opnd
(N
);
1469 Op2
: Node_Id
:= Right_Opnd
(N
);
1470 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
1471 Ctyp
: constant Entity_Id
:= Component_Type
(Typ1
);
1474 Func_Body
: Node_Id
;
1475 Func_Name
: Entity_Id
;
1479 Byte_Addressable
: constant Boolean := System_Storage_Unit
= Byte
'Size;
1480 -- True for byte addressable target
1482 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean;
1483 -- Returns True if the length of the given operand is known to be less
1484 -- than 4. Returns False if this length is known to be four or greater
1485 -- or is not known at compile time.
1487 ------------------------
1488 -- Length_Less_Than_4 --
1489 ------------------------
1491 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean is
1492 Otyp
: constant Entity_Id
:= Etype
(Opnd
);
1495 if Ekind
(Otyp
) = E_String_Literal_Subtype
then
1496 return String_Literal_Length
(Otyp
) < 4;
1500 Ityp
: constant Entity_Id
:= Etype
(First_Index
(Otyp
));
1501 Lo
: constant Node_Id
:= Type_Low_Bound
(Ityp
);
1502 Hi
: constant Node_Id
:= Type_High_Bound
(Ityp
);
1507 if Compile_Time_Known_Value
(Lo
) then
1508 Lov
:= Expr_Value
(Lo
);
1513 if Compile_Time_Known_Value
(Hi
) then
1514 Hiv
:= Expr_Value
(Hi
);
1519 return Hiv
< Lov
+ 3;
1522 end Length_Less_Than_4
;
1524 -- Start of processing for Expand_Array_Comparison
1527 -- Deal first with unpacked case, where we can call a runtime routine
1528 -- except that we avoid this for targets for which are not addressable
1529 -- by bytes, and for the JVM/CIL, since they do not support direct
1530 -- addressing of array components.
1532 if not Is_Bit_Packed_Array
(Typ1
)
1533 and then Byte_Addressable
1534 and then VM_Target
= No_VM
1536 -- The call we generate is:
1538 -- Compare_Array_xn[_Unaligned]
1539 -- (left'address, right'address, left'length, right'length) <op> 0
1541 -- x = U for unsigned, S for signed
1542 -- n = 8,16,32,64 for component size
1543 -- Add _Unaligned if length < 4 and component size is 8.
1544 -- <op> is the standard comparison operator
1546 if Component_Size
(Typ1
) = 8 then
1547 if Length_Less_Than_4
(Op1
)
1549 Length_Less_Than_4
(Op2
)
1551 if Is_Unsigned_Type
(Ctyp
) then
1552 Comp
:= RE_Compare_Array_U8_Unaligned
;
1554 Comp
:= RE_Compare_Array_S8_Unaligned
;
1558 if Is_Unsigned_Type
(Ctyp
) then
1559 Comp
:= RE_Compare_Array_U8
;
1561 Comp
:= RE_Compare_Array_S8
;
1565 elsif Component_Size
(Typ1
) = 16 then
1566 if Is_Unsigned_Type
(Ctyp
) then
1567 Comp
:= RE_Compare_Array_U16
;
1569 Comp
:= RE_Compare_Array_S16
;
1572 elsif Component_Size
(Typ1
) = 32 then
1573 if Is_Unsigned_Type
(Ctyp
) then
1574 Comp
:= RE_Compare_Array_U32
;
1576 Comp
:= RE_Compare_Array_S32
;
1579 else pragma Assert
(Component_Size
(Typ1
) = 64);
1580 if Is_Unsigned_Type
(Ctyp
) then
1581 Comp
:= RE_Compare_Array_U64
;
1583 Comp
:= RE_Compare_Array_S64
;
1587 Remove_Side_Effects
(Op1
, Name_Req
=> True);
1588 Remove_Side_Effects
(Op2
, Name_Req
=> True);
1591 Make_Function_Call
(Sloc
(Op1
),
1592 Name
=> New_Occurrence_Of
(RTE
(Comp
), Loc
),
1594 Parameter_Associations
=> New_List
(
1595 Make_Attribute_Reference
(Loc
,
1596 Prefix
=> Relocate_Node
(Op1
),
1597 Attribute_Name
=> Name_Address
),
1599 Make_Attribute_Reference
(Loc
,
1600 Prefix
=> Relocate_Node
(Op2
),
1601 Attribute_Name
=> Name_Address
),
1603 Make_Attribute_Reference
(Loc
,
1604 Prefix
=> Relocate_Node
(Op1
),
1605 Attribute_Name
=> Name_Length
),
1607 Make_Attribute_Reference
(Loc
,
1608 Prefix
=> Relocate_Node
(Op2
),
1609 Attribute_Name
=> Name_Length
))));
1612 Make_Integer_Literal
(Sloc
(Op2
),
1615 Analyze_And_Resolve
(Op1
, Standard_Integer
);
1616 Analyze_And_Resolve
(Op2
, Standard_Integer
);
1620 -- Cases where we cannot make runtime call
1622 -- For (a <= b) we convert to not (a > b)
1624 if Chars
(N
) = Name_Op_Le
then
1630 Right_Opnd
=> Op2
)));
1631 Analyze_And_Resolve
(N
, Standard_Boolean
);
1634 -- For < the Boolean expression is
1635 -- greater__nn (op2, op1)
1637 elsif Chars
(N
) = Name_Op_Lt
then
1638 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
1642 Op1
:= Right_Opnd
(N
);
1643 Op2
:= Left_Opnd
(N
);
1645 -- For (a >= b) we convert to not (a < b)
1647 elsif Chars
(N
) = Name_Op_Ge
then
1653 Right_Opnd
=> Op2
)));
1654 Analyze_And_Resolve
(N
, Standard_Boolean
);
1657 -- For > the Boolean expression is
1658 -- greater__nn (op1, op2)
1661 pragma Assert
(Chars
(N
) = Name_Op_Gt
);
1662 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
1665 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
1667 Make_Function_Call
(Loc
,
1668 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
1669 Parameter_Associations
=> New_List
(Op1
, Op2
));
1671 Insert_Action
(N
, Func_Body
);
1673 Analyze_And_Resolve
(N
, Standard_Boolean
);
1676 when RE_Not_Available
=>
1678 end Expand_Array_Comparison
;
1680 ---------------------------
1681 -- Expand_Array_Equality --
1682 ---------------------------
1684 -- Expand an equality function for multi-dimensional arrays. Here is an
1685 -- example of such a function for Nb_Dimension = 2
1687 -- function Enn (A : atyp; B : btyp) return boolean is
1689 -- if (A'length (1) = 0 or else A'length (2) = 0)
1691 -- (B'length (1) = 0 or else B'length (2) = 0)
1693 -- return True; -- RM 4.5.2(22)
1696 -- if A'length (1) /= B'length (1)
1698 -- A'length (2) /= B'length (2)
1700 -- return False; -- RM 4.5.2(23)
1704 -- A1 : Index_T1 := A'first (1);
1705 -- B1 : Index_T1 := B'first (1);
1709 -- A2 : Index_T2 := A'first (2);
1710 -- B2 : Index_T2 := B'first (2);
1713 -- if A (A1, A2) /= B (B1, B2) then
1717 -- exit when A2 = A'last (2);
1718 -- A2 := Index_T2'succ (A2);
1719 -- B2 := Index_T2'succ (B2);
1723 -- exit when A1 = A'last (1);
1724 -- A1 := Index_T1'succ (A1);
1725 -- B1 := Index_T1'succ (B1);
1732 -- Note on the formal types used (atyp and btyp). If either of the arrays
1733 -- is of a private type, we use the underlying type, and do an unchecked
1734 -- conversion of the actual. If either of the arrays has a bound depending
1735 -- on a discriminant, then we use the base type since otherwise we have an
1736 -- escaped discriminant in the function.
1738 -- If both arrays are constrained and have the same bounds, we can generate
1739 -- a loop with an explicit iteration scheme using a 'Range attribute over
1742 function Expand_Array_Equality
1747 Typ
: Entity_Id
) return Node_Id
1749 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
1750 Decls
: constant List_Id
:= New_List
;
1751 Index_List1
: constant List_Id
:= New_List
;
1752 Index_List2
: constant List_Id
:= New_List
;
1756 Func_Name
: Entity_Id
;
1757 Func_Body
: Node_Id
;
1759 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
1760 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
1764 -- The parameter types to be used for the formals
1769 Num
: Int
) return Node_Id
;
1770 -- This builds the attribute reference Arr'Nam (Expr)
1772 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
;
1773 -- Create one statement to compare corresponding components, designated
1774 -- by a full set of indexes.
1776 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
;
1777 -- Given one of the arguments, computes the appropriate type to be used
1778 -- for that argument in the corresponding function formal
1780 function Handle_One_Dimension
1782 Index
: Node_Id
) return Node_Id
;
1783 -- This procedure returns the following code
1786 -- Bn : Index_T := B'First (N);
1790 -- exit when An = A'Last (N);
1791 -- An := Index_T'Succ (An)
1792 -- Bn := Index_T'Succ (Bn)
1796 -- If both indexes are constrained and identical, the procedure
1797 -- returns a simpler loop:
1799 -- for An in A'Range (N) loop
1803 -- N is the dimension for which we are generating a loop. Index is the
1804 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1805 -- xxx statement is either the loop or declare for the next dimension
1806 -- or if this is the last dimension the comparison of corresponding
1807 -- components of the arrays.
1809 -- The actual way the code works is to return the comparison of
1810 -- corresponding components for the N+1 call. That's neater.
1812 function Test_Empty_Arrays
return Node_Id
;
1813 -- This function constructs the test for both arrays being empty
1814 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1816 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1818 function Test_Lengths_Correspond
return Node_Id
;
1819 -- This function constructs the test for arrays having different lengths
1820 -- in at least one index position, in which case the resulting code is:
1822 -- A'length (1) /= B'length (1)
1824 -- A'length (2) /= B'length (2)
1835 Num
: Int
) return Node_Id
1839 Make_Attribute_Reference
(Loc
,
1840 Attribute_Name
=> Nam
,
1841 Prefix
=> New_Occurrence_Of
(Arr
, Loc
),
1842 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, Num
)));
1845 ------------------------
1846 -- Component_Equality --
1847 ------------------------
1849 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
is
1854 -- if a(i1...) /= b(j1...) then return false; end if;
1857 Make_Indexed_Component
(Loc
,
1858 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
1859 Expressions
=> Index_List1
);
1862 Make_Indexed_Component
(Loc
,
1863 Prefix
=> Make_Identifier
(Loc
, Chars
(B
)),
1864 Expressions
=> Index_List2
);
1866 Test
:= Expand_Composite_Equality
1867 (Nod
, Component_Type
(Typ
), L
, R
, Decls
);
1869 -- If some (sub)component is an unchecked_union, the whole operation
1870 -- will raise program error.
1872 if Nkind
(Test
) = N_Raise_Program_Error
then
1874 -- This node is going to be inserted at a location where a
1875 -- statement is expected: clear its Etype so analysis will set
1876 -- it to the expected Standard_Void_Type.
1878 Set_Etype
(Test
, Empty
);
1883 Make_Implicit_If_Statement
(Nod
,
1884 Condition
=> Make_Op_Not
(Loc
, Right_Opnd
=> Test
),
1885 Then_Statements
=> New_List
(
1886 Make_Simple_Return_Statement
(Loc
,
1887 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))));
1889 end Component_Equality
;
1895 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
is
1906 T
:= Underlying_Type
(T
);
1908 X
:= First_Index
(T
);
1909 while Present
(X
) loop
1910 if Denotes_Discriminant
(Type_Low_Bound
(Etype
(X
)))
1912 Denotes_Discriminant
(Type_High_Bound
(Etype
(X
)))
1925 --------------------------
1926 -- Handle_One_Dimension --
1927 ---------------------------
1929 function Handle_One_Dimension
1931 Index
: Node_Id
) return Node_Id
1933 Need_Separate_Indexes
: constant Boolean :=
1934 Ltyp
/= Rtyp
or else not Is_Constrained
(Ltyp
);
1935 -- If the index types are identical, and we are working with
1936 -- constrained types, then we can use the same index for both
1939 An
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
1942 Index_T
: Entity_Id
;
1947 if N
> Number_Dimensions
(Ltyp
) then
1948 return Component_Equality
(Ltyp
);
1951 -- Case where we generate a loop
1953 Index_T
:= Base_Type
(Etype
(Index
));
1955 if Need_Separate_Indexes
then
1956 Bn
:= Make_Temporary
(Loc
, 'B');
1961 Append
(New_Occurrence_Of
(An
, Loc
), Index_List1
);
1962 Append
(New_Occurrence_Of
(Bn
, Loc
), Index_List2
);
1964 Stm_List
:= New_List
(
1965 Handle_One_Dimension
(N
+ 1, Next_Index
(Index
)));
1967 if Need_Separate_Indexes
then
1969 -- Generate guard for loop, followed by increments of indexes
1971 Append_To
(Stm_List
,
1972 Make_Exit_Statement
(Loc
,
1975 Left_Opnd
=> New_Occurrence_Of
(An
, Loc
),
1976 Right_Opnd
=> Arr_Attr
(A
, Name_Last
, N
))));
1978 Append_To
(Stm_List
,
1979 Make_Assignment_Statement
(Loc
,
1980 Name
=> New_Occurrence_Of
(An
, Loc
),
1982 Make_Attribute_Reference
(Loc
,
1983 Prefix
=> New_Occurrence_Of
(Index_T
, Loc
),
1984 Attribute_Name
=> Name_Succ
,
1985 Expressions
=> New_List
(
1986 New_Occurrence_Of
(An
, Loc
)))));
1988 Append_To
(Stm_List
,
1989 Make_Assignment_Statement
(Loc
,
1990 Name
=> New_Occurrence_Of
(Bn
, Loc
),
1992 Make_Attribute_Reference
(Loc
,
1993 Prefix
=> New_Occurrence_Of
(Index_T
, Loc
),
1994 Attribute_Name
=> Name_Succ
,
1995 Expressions
=> New_List
(
1996 New_Occurrence_Of
(Bn
, Loc
)))));
1999 -- If separate indexes, we need a declare block for An and Bn, and a
2000 -- loop without an iteration scheme.
2002 if Need_Separate_Indexes
then
2004 Make_Implicit_Loop_Statement
(Nod
, Statements
=> Stm_List
);
2007 Make_Block_Statement
(Loc
,
2008 Declarations
=> New_List
(
2009 Make_Object_Declaration
(Loc
,
2010 Defining_Identifier
=> An
,
2011 Object_Definition
=> New_Occurrence_Of
(Index_T
, Loc
),
2012 Expression
=> Arr_Attr
(A
, Name_First
, N
)),
2014 Make_Object_Declaration
(Loc
,
2015 Defining_Identifier
=> Bn
,
2016 Object_Definition
=> New_Occurrence_Of
(Index_T
, Loc
),
2017 Expression
=> Arr_Attr
(B
, Name_First
, N
))),
2019 Handled_Statement_Sequence
=>
2020 Make_Handled_Sequence_Of_Statements
(Loc
,
2021 Statements
=> New_List
(Loop_Stm
)));
2023 -- If no separate indexes, return loop statement with explicit
2024 -- iteration scheme on its own
2028 Make_Implicit_Loop_Statement
(Nod
,
2029 Statements
=> Stm_List
,
2031 Make_Iteration_Scheme
(Loc
,
2032 Loop_Parameter_Specification
=>
2033 Make_Loop_Parameter_Specification
(Loc
,
2034 Defining_Identifier
=> An
,
2035 Discrete_Subtype_Definition
=>
2036 Arr_Attr
(A
, Name_Range
, N
))));
2039 end Handle_One_Dimension
;
2041 -----------------------
2042 -- Test_Empty_Arrays --
2043 -----------------------
2045 function Test_Empty_Arrays
return Node_Id
is
2055 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
2058 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
2059 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
2063 Left_Opnd
=> Arr_Attr
(B
, Name_Length
, J
),
2064 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
2073 Left_Opnd
=> Relocate_Node
(Alist
),
2074 Right_Opnd
=> Atest
);
2078 Left_Opnd
=> Relocate_Node
(Blist
),
2079 Right_Opnd
=> Btest
);
2086 Right_Opnd
=> Blist
);
2087 end Test_Empty_Arrays
;
2089 -----------------------------
2090 -- Test_Lengths_Correspond --
2091 -----------------------------
2093 function Test_Lengths_Correspond
return Node_Id
is
2099 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
2102 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
2103 Right_Opnd
=> Arr_Attr
(B
, Name_Length
, J
));
2110 Left_Opnd
=> Relocate_Node
(Result
),
2111 Right_Opnd
=> Rtest
);
2116 end Test_Lengths_Correspond
;
2118 -- Start of processing for Expand_Array_Equality
2121 Ltyp
:= Get_Arg_Type
(Lhs
);
2122 Rtyp
:= Get_Arg_Type
(Rhs
);
2124 -- For now, if the argument types are not the same, go to the base type,
2125 -- since the code assumes that the formals have the same type. This is
2126 -- fixable in future ???
2128 if Ltyp
/= Rtyp
then
2129 Ltyp
:= Base_Type
(Ltyp
);
2130 Rtyp
:= Base_Type
(Rtyp
);
2131 pragma Assert
(Ltyp
= Rtyp
);
2134 -- Build list of formals for function
2136 Formals
:= New_List
(
2137 Make_Parameter_Specification
(Loc
,
2138 Defining_Identifier
=> A
,
2139 Parameter_Type
=> New_Occurrence_Of
(Ltyp
, Loc
)),
2141 Make_Parameter_Specification
(Loc
,
2142 Defining_Identifier
=> B
,
2143 Parameter_Type
=> New_Occurrence_Of
(Rtyp
, Loc
)));
2145 Func_Name
:= Make_Temporary
(Loc
, 'E');
2147 -- Build statement sequence for function
2150 Make_Subprogram_Body
(Loc
,
2152 Make_Function_Specification
(Loc
,
2153 Defining_Unit_Name
=> Func_Name
,
2154 Parameter_Specifications
=> Formals
,
2155 Result_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
)),
2157 Declarations
=> Decls
,
2159 Handled_Statement_Sequence
=>
2160 Make_Handled_Sequence_Of_Statements
(Loc
,
2161 Statements
=> New_List
(
2163 Make_Implicit_If_Statement
(Nod
,
2164 Condition
=> Test_Empty_Arrays
,
2165 Then_Statements
=> New_List
(
2166 Make_Simple_Return_Statement
(Loc
,
2168 New_Occurrence_Of
(Standard_True
, Loc
)))),
2170 Make_Implicit_If_Statement
(Nod
,
2171 Condition
=> Test_Lengths_Correspond
,
2172 Then_Statements
=> New_List
(
2173 Make_Simple_Return_Statement
(Loc
,
2174 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
)))),
2176 Handle_One_Dimension
(1, First_Index
(Ltyp
)),
2178 Make_Simple_Return_Statement
(Loc
,
2179 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)))));
2181 Set_Has_Completion
(Func_Name
, True);
2182 Set_Is_Inlined
(Func_Name
);
2184 -- If the array type is distinct from the type of the arguments, it
2185 -- is the full view of a private type. Apply an unchecked conversion
2186 -- to insure that analysis of the call succeeds.
2196 or else Base_Type
(Etype
(Lhs
)) /= Base_Type
(Ltyp
)
2198 L
:= OK_Convert_To
(Ltyp
, Lhs
);
2202 or else Base_Type
(Etype
(Rhs
)) /= Base_Type
(Rtyp
)
2204 R
:= OK_Convert_To
(Rtyp
, Rhs
);
2207 Actuals
:= New_List
(L
, R
);
2210 Append_To
(Bodies
, Func_Body
);
2213 Make_Function_Call
(Loc
,
2214 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
2215 Parameter_Associations
=> Actuals
);
2216 end Expand_Array_Equality
;
2218 -----------------------------
2219 -- Expand_Boolean_Operator --
2220 -----------------------------
2222 -- Note that we first get the actual subtypes of the operands, since we
2223 -- always want to deal with types that have bounds.
2225 procedure Expand_Boolean_Operator
(N
: Node_Id
) is
2226 Typ
: constant Entity_Id
:= Etype
(N
);
2229 -- Special case of bit packed array where both operands are known to be
2230 -- properly aligned. In this case we use an efficient run time routine
2231 -- to carry out the operation (see System.Bit_Ops).
2233 if Is_Bit_Packed_Array
(Typ
)
2234 and then not Is_Possibly_Unaligned_Object
(Left_Opnd
(N
))
2235 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
2237 Expand_Packed_Boolean_Operator
(N
);
2241 -- For the normal non-packed case, the general expansion is to build
2242 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2243 -- and then inserting it into the tree. The original operator node is
2244 -- then rewritten as a call to this function. We also use this in the
2245 -- packed case if either operand is a possibly unaligned object.
2248 Loc
: constant Source_Ptr
:= Sloc
(N
);
2249 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
2250 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
2251 Func_Body
: Node_Id
;
2252 Func_Name
: Entity_Id
;
2255 Convert_To_Actual_Subtype
(L
);
2256 Convert_To_Actual_Subtype
(R
);
2257 Ensure_Defined
(Etype
(L
), N
);
2258 Ensure_Defined
(Etype
(R
), N
);
2259 Apply_Length_Check
(R
, Etype
(L
));
2261 if Nkind
(N
) = N_Op_Xor
then
2262 Silly_Boolean_Array_Xor_Test
(N
, Etype
(L
));
2265 if Nkind
(Parent
(N
)) = N_Assignment_Statement
2266 and then Safe_In_Place_Array_Op
(Name
(Parent
(N
)), L
, R
)
2268 Build_Boolean_Array_Proc_Call
(Parent
(N
), L
, R
);
2270 elsif Nkind
(Parent
(N
)) = N_Op_Not
2271 and then Nkind
(N
) = N_Op_And
2272 and then Safe_In_Place_Array_Op
(Name
(Parent
(Parent
(N
))), L
, R
)
2277 Func_Body
:= Make_Boolean_Array_Op
(Etype
(L
), N
);
2278 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
2279 Insert_Action
(N
, Func_Body
);
2281 -- Now rewrite the expression with a call
2284 Make_Function_Call
(Loc
,
2285 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
2286 Parameter_Associations
=>
2289 Make_Type_Conversion
2290 (Loc
, New_Occurrence_Of
(Etype
(L
), Loc
), R
))));
2292 Analyze_And_Resolve
(N
, Typ
);
2295 end Expand_Boolean_Operator
;
2297 ------------------------------------------------
2298 -- Expand_Compare_Minimize_Eliminate_Overflow --
2299 ------------------------------------------------
2301 procedure Expand_Compare_Minimize_Eliminate_Overflow
(N
: Node_Id
) is
2302 Loc
: constant Source_Ptr
:= Sloc
(N
);
2304 Result_Type
: constant Entity_Id
:= Etype
(N
);
2305 -- Capture result type (could be a derived boolean type)
2310 LLIB
: constant Entity_Id
:= Base_Type
(Standard_Long_Long_Integer
);
2311 -- Entity for Long_Long_Integer'Base
2313 Check
: constant Overflow_Mode_Type
:= Overflow_Check_Mode
;
2314 -- Current overflow checking mode
2317 procedure Set_False
;
2318 -- These procedures rewrite N with an occurrence of Standard_True or
2319 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2325 procedure Set_False
is
2327 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
2328 Warn_On_Known_Condition
(N
);
2335 procedure Set_True
is
2337 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
2338 Warn_On_Known_Condition
(N
);
2341 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2344 -- Nothing to do unless we have a comparison operator with operands
2345 -- that are signed integer types, and we are operating in either
2346 -- MINIMIZED or ELIMINATED overflow checking mode.
2348 if Nkind
(N
) not in N_Op_Compare
2349 or else Check
not in Minimized_Or_Eliminated
2350 or else not Is_Signed_Integer_Type
(Etype
(Left_Opnd
(N
)))
2355 -- OK, this is the case we are interested in. First step is to process
2356 -- our operands using the Minimize_Eliminate circuitry which applies
2357 -- this processing to the two operand subtrees.
2359 Minimize_Eliminate_Overflows
2360 (Left_Opnd
(N
), Llo
, Lhi
, Top_Level
=> False);
2361 Minimize_Eliminate_Overflows
2362 (Right_Opnd
(N
), Rlo
, Rhi
, Top_Level
=> False);
2364 -- See if the range information decides the result of the comparison.
2365 -- We can only do this if we in fact have full range information (which
2366 -- won't be the case if either operand is bignum at this stage).
2368 if Llo
/= No_Uint
and then Rlo
/= No_Uint
then
2369 case N_Op_Compare
(Nkind
(N
)) is
2371 if Llo
= Lhi
and then Rlo
= Rhi
and then Llo
= Rlo
then
2373 elsif Llo
> Rhi
or else Lhi
< Rlo
then
2380 elsif Lhi
< Rlo
then
2387 elsif Lhi
<= Rlo
then
2394 elsif Lhi
<= Rlo
then
2401 elsif Lhi
< Rlo
then
2406 if Llo
= Lhi
and then Rlo
= Rhi
and then Llo
= Rlo
then
2408 elsif Llo
> Rhi
or else Lhi
< Rlo
then
2413 -- All done if we did the rewrite
2415 if Nkind
(N
) not in N_Op_Compare
then
2420 -- Otherwise, time to do the comparison
2423 Ltype
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
2424 Rtype
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
2427 -- If the two operands have the same signed integer type we are
2428 -- all set, nothing more to do. This is the case where either
2429 -- both operands were unchanged, or we rewrote both of them to
2430 -- be Long_Long_Integer.
2432 -- Note: Entity for the comparison may be wrong, but it's not worth
2433 -- the effort to change it, since the back end does not use it.
2435 if Is_Signed_Integer_Type
(Ltype
)
2436 and then Base_Type
(Ltype
) = Base_Type
(Rtype
)
2440 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2442 elsif Is_RTE
(Ltype
, RE_Bignum
) or else Is_RTE
(Rtype
, RE_Bignum
) then
2444 Left
: Node_Id
:= Left_Opnd
(N
);
2445 Right
: Node_Id
:= Right_Opnd
(N
);
2446 -- Bignum references for left and right operands
2449 if not Is_RTE
(Ltype
, RE_Bignum
) then
2450 Left
:= Convert_To_Bignum
(Left
);
2451 elsif not Is_RTE
(Rtype
, RE_Bignum
) then
2452 Right
:= Convert_To_Bignum
(Right
);
2455 -- We rewrite our node with:
2458 -- Bnn : Result_Type;
2460 -- M : Mark_Id := SS_Mark;
2462 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2470 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
2471 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
2475 case N_Op_Compare
(Nkind
(N
)) is
2476 when N_Op_Eq
=> Ent
:= RE_Big_EQ
;
2477 when N_Op_Ge
=> Ent
:= RE_Big_GE
;
2478 when N_Op_Gt
=> Ent
:= RE_Big_GT
;
2479 when N_Op_Le
=> Ent
:= RE_Big_LE
;
2480 when N_Op_Lt
=> Ent
:= RE_Big_LT
;
2481 when N_Op_Ne
=> Ent
:= RE_Big_NE
;
2484 -- Insert assignment to Bnn into the bignum block
2487 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
2488 Make_Assignment_Statement
(Loc
,
2489 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
2491 Make_Function_Call
(Loc
,
2493 New_Occurrence_Of
(RTE
(Ent
), Loc
),
2494 Parameter_Associations
=> New_List
(Left
, Right
))));
2496 -- Now do the rewrite with expression actions
2499 Make_Expression_With_Actions
(Loc
,
2500 Actions
=> New_List
(
2501 Make_Object_Declaration
(Loc
,
2502 Defining_Identifier
=> Bnn
,
2503 Object_Definition
=>
2504 New_Occurrence_Of
(Result_Type
, Loc
)),
2506 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
2507 Analyze_And_Resolve
(N
, Result_Type
);
2511 -- No bignums involved, but types are different, so we must have
2512 -- rewritten one of the operands as a Long_Long_Integer but not
2515 -- If left operand is Long_Long_Integer, convert right operand
2516 -- and we are done (with a comparison of two Long_Long_Integers).
2518 elsif Ltype
= LLIB
then
2519 Convert_To_And_Rewrite
(LLIB
, Right_Opnd
(N
));
2520 Analyze_And_Resolve
(Right_Opnd
(N
), LLIB
, Suppress
=> All_Checks
);
2523 -- If right operand is Long_Long_Integer, convert left operand
2524 -- and we are done (with a comparison of two Long_Long_Integers).
2526 -- This is the only remaining possibility
2528 else pragma Assert
(Rtype
= LLIB
);
2529 Convert_To_And_Rewrite
(LLIB
, Left_Opnd
(N
));
2530 Analyze_And_Resolve
(Left_Opnd
(N
), LLIB
, Suppress
=> All_Checks
);
2534 end Expand_Compare_Minimize_Eliminate_Overflow
;
2536 -------------------------------
2537 -- Expand_Composite_Equality --
2538 -------------------------------
2540 -- This function is only called for comparing internal fields of composite
2541 -- types when these fields are themselves composites. This is a special
2542 -- case because it is not possible to respect normal Ada visibility rules.
2544 function Expand_Composite_Equality
2549 Bodies
: List_Id
) return Node_Id
2551 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
2552 Full_Type
: Entity_Id
;
2556 function Find_Primitive_Eq
return Node_Id
;
2557 -- AI05-0123: Locate primitive equality for type if it exists, and
2558 -- build the corresponding call. If operation is abstract, replace
2559 -- call with an explicit raise. Return Empty if there is no primitive.
2561 -----------------------
2562 -- Find_Primitive_Eq --
2563 -----------------------
2565 function Find_Primitive_Eq
return Node_Id
is
2570 Prim_E
:= First_Elmt
(Collect_Primitive_Operations
(Typ
));
2571 while Present
(Prim_E
) loop
2572 Prim
:= Node
(Prim_E
);
2574 -- Locate primitive equality with the right signature
2576 if Chars
(Prim
) = Name_Op_Eq
2577 and then Etype
(First_Formal
(Prim
)) =
2578 Etype
(Next_Formal
(First_Formal
(Prim
)))
2579 and then Etype
(Prim
) = Standard_Boolean
2581 if Is_Abstract_Subprogram
(Prim
) then
2583 Make_Raise_Program_Error
(Loc
,
2584 Reason
=> PE_Explicit_Raise
);
2588 Make_Function_Call
(Loc
,
2589 Name
=> New_Occurrence_Of
(Prim
, Loc
),
2590 Parameter_Associations
=> New_List
(Lhs
, Rhs
));
2597 -- If not found, predefined operation will be used
2600 end Find_Primitive_Eq
;
2602 -- Start of processing for Expand_Composite_Equality
2605 if Is_Private_Type
(Typ
) then
2606 Full_Type
:= Underlying_Type
(Typ
);
2611 -- If the private type has no completion the context may be the
2612 -- expansion of a composite equality for a composite type with some
2613 -- still incomplete components. The expression will not be analyzed
2614 -- until the enclosing type is completed, at which point this will be
2615 -- properly expanded, unless there is a bona fide completion error.
2617 if No
(Full_Type
) then
2618 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2621 Full_Type
:= Base_Type
(Full_Type
);
2623 -- When the base type itself is private, use the full view to expand
2624 -- the composite equality.
2626 if Is_Private_Type
(Full_Type
) then
2627 Full_Type
:= Underlying_Type
(Full_Type
);
2630 -- Case of array types
2632 if Is_Array_Type
(Full_Type
) then
2634 -- If the operand is an elementary type other than a floating-point
2635 -- type, then we can simply use the built-in block bitwise equality,
2636 -- since the predefined equality operators always apply and bitwise
2637 -- equality is fine for all these cases.
2639 if Is_Elementary_Type
(Component_Type
(Full_Type
))
2640 and then not Is_Floating_Point_Type
(Component_Type
(Full_Type
))
2642 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2644 -- For composite component types, and floating-point types, use the
2645 -- expansion. This deals with tagged component types (where we use
2646 -- the applicable equality routine) and floating-point, (where we
2647 -- need to worry about negative zeroes), and also the case of any
2648 -- composite type recursively containing such fields.
2651 return Expand_Array_Equality
(Nod
, Lhs
, Rhs
, Bodies
, Full_Type
);
2654 -- Case of tagged record types
2656 elsif Is_Tagged_Type
(Full_Type
) then
2658 -- Call the primitive operation "=" of this type
2660 if Is_Class_Wide_Type
(Full_Type
) then
2661 Full_Type
:= Root_Type
(Full_Type
);
2664 -- If this is derived from an untagged private type completed with a
2665 -- tagged type, it does not have a full view, so we use the primitive
2666 -- operations of the private type. This check should no longer be
2667 -- necessary when these types receive their full views ???
2669 if Is_Private_Type
(Typ
)
2670 and then not Is_Tagged_Type
(Typ
)
2671 and then not Is_Controlled
(Typ
)
2672 and then Is_Derived_Type
(Typ
)
2673 and then No
(Full_View
(Typ
))
2675 Prim
:= First_Elmt
(Collect_Primitive_Operations
(Typ
));
2677 Prim
:= First_Elmt
(Primitive_Operations
(Full_Type
));
2681 Eq_Op
:= Node
(Prim
);
2682 exit when Chars
(Eq_Op
) = Name_Op_Eq
2683 and then Etype
(First_Formal
(Eq_Op
)) =
2684 Etype
(Next_Formal
(First_Formal
(Eq_Op
)))
2685 and then Base_Type
(Etype
(Eq_Op
)) = Standard_Boolean
;
2687 pragma Assert
(Present
(Prim
));
2690 Eq_Op
:= Node
(Prim
);
2693 Make_Function_Call
(Loc
,
2694 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2695 Parameter_Associations
=>
2697 (Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Lhs
),
2698 Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Rhs
)));
2700 -- Case of untagged record types
2702 elsif Is_Record_Type
(Full_Type
) then
2703 Eq_Op
:= TSS
(Full_Type
, TSS_Composite_Equality
);
2705 if Present
(Eq_Op
) then
2706 if Etype
(First_Formal
(Eq_Op
)) /= Full_Type
then
2708 -- Inherited equality from parent type. Convert the actuals to
2709 -- match signature of operation.
2712 T
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
2716 Make_Function_Call
(Loc
,
2717 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2718 Parameter_Associations
=> New_List
(
2719 OK_Convert_To
(T
, Lhs
),
2720 OK_Convert_To
(T
, Rhs
)));
2724 -- Comparison between Unchecked_Union components
2726 if Is_Unchecked_Union
(Full_Type
) then
2728 Lhs_Type
: Node_Id
:= Full_Type
;
2729 Rhs_Type
: Node_Id
:= Full_Type
;
2730 Lhs_Discr_Val
: Node_Id
;
2731 Rhs_Discr_Val
: Node_Id
;
2736 if Nkind
(Lhs
) = N_Selected_Component
then
2737 Lhs_Type
:= Etype
(Entity
(Selector_Name
(Lhs
)));
2742 if Nkind
(Rhs
) = N_Selected_Component
then
2743 Rhs_Type
:= Etype
(Entity
(Selector_Name
(Rhs
)));
2746 -- Lhs of the composite equality
2748 if Is_Constrained
(Lhs_Type
) then
2750 -- Since the enclosing record type can never be an
2751 -- Unchecked_Union (this code is executed for records
2752 -- that do not have variants), we may reference its
2755 if Nkind
(Lhs
) = N_Selected_Component
2756 and then Has_Per_Object_Constraint
2757 (Entity
(Selector_Name
(Lhs
)))
2760 Make_Selected_Component
(Loc
,
2761 Prefix
=> Prefix
(Lhs
),
2764 (Get_Discriminant_Value
2765 (First_Discriminant
(Lhs_Type
),
2767 Stored_Constraint
(Lhs_Type
))));
2772 (Get_Discriminant_Value
2773 (First_Discriminant
(Lhs_Type
),
2775 Stored_Constraint
(Lhs_Type
)));
2779 -- It is not possible to infer the discriminant since
2780 -- the subtype is not constrained.
2783 Make_Raise_Program_Error
(Loc
,
2784 Reason
=> PE_Unchecked_Union_Restriction
);
2787 -- Rhs of the composite equality
2789 if Is_Constrained
(Rhs_Type
) then
2790 if Nkind
(Rhs
) = N_Selected_Component
2791 and then Has_Per_Object_Constraint
2792 (Entity
(Selector_Name
(Rhs
)))
2795 Make_Selected_Component
(Loc
,
2796 Prefix
=> Prefix
(Rhs
),
2799 (Get_Discriminant_Value
2800 (First_Discriminant
(Rhs_Type
),
2802 Stored_Constraint
(Rhs_Type
))));
2807 (Get_Discriminant_Value
2808 (First_Discriminant
(Rhs_Type
),
2810 Stored_Constraint
(Rhs_Type
)));
2815 Make_Raise_Program_Error
(Loc
,
2816 Reason
=> PE_Unchecked_Union_Restriction
);
2819 -- Call the TSS equality function with the inferred
2820 -- discriminant values.
2823 Make_Function_Call
(Loc
,
2824 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2825 Parameter_Associations
=> New_List
(
2832 -- All cases other than comparing Unchecked_Union types
2836 T
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
2839 Make_Function_Call
(Loc
,
2841 New_Occurrence_Of
(Eq_Op
, Loc
),
2842 Parameter_Associations
=> New_List
(
2843 OK_Convert_To
(T
, Lhs
),
2844 OK_Convert_To
(T
, Rhs
)));
2849 -- Equality composes in Ada 2012 for untagged record types. It also
2850 -- composes for bounded strings, because they are part of the
2851 -- predefined environment. We could make it compose for bounded
2852 -- strings by making them tagged, or by making sure all subcomponents
2853 -- are set to the same value, even when not used. Instead, we have
2854 -- this special case in the compiler, because it's more efficient.
2856 elsif Ada_Version
>= Ada_2012
or else Is_Bounded_String
(Typ
) then
2858 -- If no TSS has been created for the type, check whether there is
2859 -- a primitive equality declared for it.
2862 Op
: constant Node_Id
:= Find_Primitive_Eq
;
2865 -- Use user-defined primitive if it exists, otherwise use
2866 -- predefined equality.
2868 if Present
(Op
) then
2871 return Make_Op_Eq
(Loc
, Lhs
, Rhs
);
2876 return Expand_Record_Equality
(Nod
, Full_Type
, Lhs
, Rhs
, Bodies
);
2879 -- Non-composite types (always use predefined equality)
2882 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2884 end Expand_Composite_Equality
;
2886 ------------------------
2887 -- Expand_Concatenate --
2888 ------------------------
2890 procedure Expand_Concatenate
(Cnode
: Node_Id
; Opnds
: List_Id
) is
2891 Loc
: constant Source_Ptr
:= Sloc
(Cnode
);
2893 Atyp
: constant Entity_Id
:= Base_Type
(Etype
(Cnode
));
2894 -- Result type of concatenation
2896 Ctyp
: constant Entity_Id
:= Base_Type
(Component_Type
(Etype
(Cnode
)));
2897 -- Component type. Elements of this component type can appear as one
2898 -- of the operands of concatenation as well as arrays.
2900 Istyp
: constant Entity_Id
:= Etype
(First_Index
(Atyp
));
2903 Ityp
: constant Entity_Id
:= Base_Type
(Istyp
);
2904 -- Index type. This is the base type of the index subtype, and is used
2905 -- for all computed bounds (which may be out of range of Istyp in the
2906 -- case of null ranges).
2909 -- This is the type we use to do arithmetic to compute the bounds and
2910 -- lengths of operands. The choice of this type is a little subtle and
2911 -- is discussed in a separate section at the start of the body code.
2913 Concatenation_Error
: exception;
2914 -- Raised if concatenation is sure to raise a CE
2916 Result_May_Be_Null
: Boolean := True;
2917 -- Reset to False if at least one operand is encountered which is known
2918 -- at compile time to be non-null. Used for handling the special case
2919 -- of setting the high bound to the last operand high bound for a null
2920 -- result, thus ensuring a proper high bound in the super-flat case.
2922 N
: constant Nat
:= List_Length
(Opnds
);
2923 -- Number of concatenation operands including possibly null operands
2926 -- Number of operands excluding any known to be null, except that the
2927 -- last operand is always retained, in case it provides the bounds for
2931 -- Current operand being processed in the loop through operands. After
2932 -- this loop is complete, always contains the last operand (which is not
2933 -- the same as Operands (NN), since null operands are skipped).
2935 -- Arrays describing the operands, only the first NN entries of each
2936 -- array are set (NN < N when we exclude known null operands).
2938 Is_Fixed_Length
: array (1 .. N
) of Boolean;
2939 -- True if length of corresponding operand known at compile time
2941 Operands
: array (1 .. N
) of Node_Id
;
2942 -- Set to the corresponding entry in the Opnds list (but note that null
2943 -- operands are excluded, so not all entries in the list are stored).
2945 Fixed_Length
: array (1 .. N
) of Uint
;
2946 -- Set to length of operand. Entries in this array are set only if the
2947 -- corresponding entry in Is_Fixed_Length is True.
2949 Opnd_Low_Bound
: array (1 .. N
) of Node_Id
;
2950 -- Set to lower bound of operand. Either an integer literal in the case
2951 -- where the bound is known at compile time, else actual lower bound.
2952 -- The operand low bound is of type Ityp.
2954 Var_Length
: array (1 .. N
) of Entity_Id
;
2955 -- Set to an entity of type Natural that contains the length of an
2956 -- operand whose length is not known at compile time. Entries in this
2957 -- array are set only if the corresponding entry in Is_Fixed_Length
2958 -- is False. The entity is of type Artyp.
2960 Aggr_Length
: array (0 .. N
) of Node_Id
;
2961 -- The J'th entry in an expression node that represents the total length
2962 -- of operands 1 through J. It is either an integer literal node, or a
2963 -- reference to a constant entity with the right value, so it is fine
2964 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2965 -- entry always is set to zero. The length is of type Artyp.
2967 Low_Bound
: Node_Id
;
2968 -- A tree node representing the low bound of the result (of type Ityp).
2969 -- This is either an integer literal node, or an identifier reference to
2970 -- a constant entity initialized to the appropriate value.
2972 Last_Opnd_Low_Bound
: Node_Id
;
2973 -- A tree node representing the low bound of the last operand. This
2974 -- need only be set if the result could be null. It is used for the
2975 -- special case of setting the right low bound for a null result.
2976 -- This is of type Ityp.
2978 Last_Opnd_High_Bound
: Node_Id
;
2979 -- A tree node representing the high bound of the last operand. This
2980 -- need only be set if the result could be null. It is used for the
2981 -- special case of setting the right high bound for a null result.
2982 -- This is of type Ityp.
2984 High_Bound
: Node_Id
;
2985 -- A tree node representing the high bound of the result (of type Ityp)
2988 -- Result of the concatenation (of type Ityp)
2990 Actions
: constant List_Id
:= New_List
;
2991 -- Collect actions to be inserted
2993 Known_Non_Null_Operand_Seen
: Boolean;
2994 -- Set True during generation of the assignments of operands into
2995 -- result once an operand known to be non-null has been seen.
2997 function Make_Artyp_Literal
(Val
: Nat
) return Node_Id
;
2998 -- This function makes an N_Integer_Literal node that is returned in
2999 -- analyzed form with the type set to Artyp. Importantly this literal
3000 -- is not flagged as static, so that if we do computations with it that
3001 -- result in statically detected out of range conditions, we will not
3002 -- generate error messages but instead warning messages.
3004 function To_Artyp
(X
: Node_Id
) return Node_Id
;
3005 -- Given a node of type Ityp, returns the corresponding value of type
3006 -- Artyp. For non-enumeration types, this is a plain integer conversion.
3007 -- For enum types, the Pos of the value is returned.
3009 function To_Ityp
(X
: Node_Id
) return Node_Id
;
3010 -- The inverse function (uses Val in the case of enumeration types)
3012 ------------------------
3013 -- Make_Artyp_Literal --
3014 ------------------------
3016 function Make_Artyp_Literal
(Val
: Nat
) return Node_Id
is
3017 Result
: constant Node_Id
:= Make_Integer_Literal
(Loc
, Val
);
3019 Set_Etype
(Result
, Artyp
);
3020 Set_Analyzed
(Result
, True);
3021 Set_Is_Static_Expression
(Result
, False);
3023 end Make_Artyp_Literal
;
3029 function To_Artyp
(X
: Node_Id
) return Node_Id
is
3031 if Ityp
= Base_Type
(Artyp
) then
3034 elsif Is_Enumeration_Type
(Ityp
) then
3036 Make_Attribute_Reference
(Loc
,
3037 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
3038 Attribute_Name
=> Name_Pos
,
3039 Expressions
=> New_List
(X
));
3042 return Convert_To
(Artyp
, X
);
3050 function To_Ityp
(X
: Node_Id
) return Node_Id
is
3052 if Is_Enumeration_Type
(Ityp
) then
3054 Make_Attribute_Reference
(Loc
,
3055 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
3056 Attribute_Name
=> Name_Val
,
3057 Expressions
=> New_List
(X
));
3059 -- Case where we will do a type conversion
3062 if Ityp
= Base_Type
(Artyp
) then
3065 return Convert_To
(Ityp
, X
);
3070 -- Local Declarations
3072 Lib_Level_Target
: constant Boolean :=
3073 Nkind
(Parent
(Cnode
)) = N_Object_Declaration
3075 Is_Library_Level_Entity
(Defining_Identifier
(Parent
(Cnode
)));
3077 -- If the concatenation declares a library level entity, we call the
3078 -- built-in concatenation routines to prevent code bloat, regardless
3079 -- of optimization level. This is space-efficient, and prevent linking
3080 -- problems when units are compiled with different optimizations.
3082 Opnd_Typ
: Entity_Id
;
3089 -- Start of processing for Expand_Concatenate
3092 -- Choose an appropriate computational type
3094 -- We will be doing calculations of lengths and bounds in this routine
3095 -- and computing one from the other in some cases, e.g. getting the high
3096 -- bound by adding the length-1 to the low bound.
3098 -- We can't just use the index type, or even its base type for this
3099 -- purpose for two reasons. First it might be an enumeration type which
3100 -- is not suitable for computations of any kind, and second it may
3101 -- simply not have enough range. For example if the index type is
3102 -- -128..+127 then lengths can be up to 256, which is out of range of
3105 -- For enumeration types, we can simply use Standard_Integer, this is
3106 -- sufficient since the actual number of enumeration literals cannot
3107 -- possibly exceed the range of integer (remember we will be doing the
3108 -- arithmetic with POS values, not representation values).
3110 if Is_Enumeration_Type
(Ityp
) then
3111 Artyp
:= Standard_Integer
;
3113 -- If index type is Positive, we use the standard unsigned type, to give
3114 -- more room on the top of the range, obviating the need for an overflow
3115 -- check when creating the upper bound. This is needed to avoid junk
3116 -- overflow checks in the common case of String types.
3118 -- ??? Disabled for now
3120 -- elsif Istyp = Standard_Positive then
3121 -- Artyp := Standard_Unsigned;
3123 -- For modular types, we use a 32-bit modular type for types whose size
3124 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
3125 -- identity type, and for larger unsigned types we use 64-bits.
3127 elsif Is_Modular_Integer_Type
(Ityp
) then
3128 if RM_Size
(Ityp
) < RM_Size
(Standard_Unsigned
) then
3129 Artyp
:= Standard_Unsigned
;
3130 elsif RM_Size
(Ityp
) = RM_Size
(Standard_Unsigned
) then
3133 Artyp
:= RTE
(RE_Long_Long_Unsigned
);
3136 -- Similar treatment for signed types
3139 if RM_Size
(Ityp
) < RM_Size
(Standard_Integer
) then
3140 Artyp
:= Standard_Integer
;
3141 elsif RM_Size
(Ityp
) = RM_Size
(Standard_Integer
) then
3144 Artyp
:= Standard_Long_Long_Integer
;
3148 -- Supply dummy entry at start of length array
3150 Aggr_Length
(0) := Make_Artyp_Literal
(0);
3152 -- Go through operands setting up the above arrays
3156 Opnd
:= Remove_Head
(Opnds
);
3157 Opnd_Typ
:= Etype
(Opnd
);
3159 -- The parent got messed up when we put the operands in a list,
3160 -- so now put back the proper parent for the saved operand, that
3161 -- is to say the concatenation node, to make sure that each operand
3162 -- is seen as a subexpression, e.g. if actions must be inserted.
3164 Set_Parent
(Opnd
, Cnode
);
3166 -- Set will be True when we have setup one entry in the array
3170 -- Singleton element (or character literal) case
3172 if Base_Type
(Opnd_Typ
) = Ctyp
then
3174 Operands
(NN
) := Opnd
;
3175 Is_Fixed_Length
(NN
) := True;
3176 Fixed_Length
(NN
) := Uint_1
;
3177 Result_May_Be_Null
:= False;
3179 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
3180 -- since we know that the result cannot be null).
3182 Opnd_Low_Bound
(NN
) :=
3183 Make_Attribute_Reference
(Loc
,
3184 Prefix
=> New_Occurrence_Of
(Istyp
, Loc
),
3185 Attribute_Name
=> Name_First
);
3189 -- String literal case (can only occur for strings of course)
3191 elsif Nkind
(Opnd
) = N_String_Literal
then
3192 Len
:= String_Literal_Length
(Opnd_Typ
);
3195 Result_May_Be_Null
:= False;
3198 -- Capture last operand low and high bound if result could be null
3200 if J
= N
and then Result_May_Be_Null
then
3201 Last_Opnd_Low_Bound
:=
3202 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
));
3204 Last_Opnd_High_Bound
:=
3205 Make_Op_Subtract
(Loc
,
3207 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
)),
3208 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
3211 -- Skip null string literal
3213 if J
< N
and then Len
= 0 then
3218 Operands
(NN
) := Opnd
;
3219 Is_Fixed_Length
(NN
) := True;
3221 -- Set length and bounds
3223 Fixed_Length
(NN
) := Len
;
3225 Opnd_Low_Bound
(NN
) :=
3226 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
));
3233 -- Check constrained case with known bounds
3235 if Is_Constrained
(Opnd_Typ
) then
3237 Index
: constant Node_Id
:= First_Index
(Opnd_Typ
);
3238 Indx_Typ
: constant Entity_Id
:= Etype
(Index
);
3239 Lo
: constant Node_Id
:= Type_Low_Bound
(Indx_Typ
);
3240 Hi
: constant Node_Id
:= Type_High_Bound
(Indx_Typ
);
3243 -- Fixed length constrained array type with known at compile
3244 -- time bounds is last case of fixed length operand.
3246 if Compile_Time_Known_Value
(Lo
)
3248 Compile_Time_Known_Value
(Hi
)
3251 Loval
: constant Uint
:= Expr_Value
(Lo
);
3252 Hival
: constant Uint
:= Expr_Value
(Hi
);
3253 Len
: constant Uint
:=
3254 UI_Max
(Hival
- Loval
+ 1, Uint_0
);
3258 Result_May_Be_Null
:= False;
3261 -- Capture last operand bounds if result could be null
3263 if J
= N
and then Result_May_Be_Null
then
3264 Last_Opnd_Low_Bound
:=
3266 Make_Integer_Literal
(Loc
, Expr_Value
(Lo
)));
3268 Last_Opnd_High_Bound
:=
3270 Make_Integer_Literal
(Loc
, Expr_Value
(Hi
)));
3273 -- Exclude null length case unless last operand
3275 if J
< N
and then Len
= 0 then
3280 Operands
(NN
) := Opnd
;
3281 Is_Fixed_Length
(NN
) := True;
3282 Fixed_Length
(NN
) := Len
;
3284 Opnd_Low_Bound
(NN
) :=
3286 (Make_Integer_Literal
(Loc
, Expr_Value
(Lo
)));
3293 -- All cases where the length is not known at compile time, or the
3294 -- special case of an operand which is known to be null but has a
3295 -- lower bound other than 1 or is other than a string type.
3300 -- Capture operand bounds
3302 Opnd_Low_Bound
(NN
) :=
3303 Make_Attribute_Reference
(Loc
,
3305 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3306 Attribute_Name
=> Name_First
);
3308 -- Capture last operand bounds if result could be null
3310 if J
= N
and Result_May_Be_Null
then
3311 Last_Opnd_Low_Bound
:=
3313 Make_Attribute_Reference
(Loc
,
3315 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3316 Attribute_Name
=> Name_First
));
3318 Last_Opnd_High_Bound
:=
3320 Make_Attribute_Reference
(Loc
,
3322 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3323 Attribute_Name
=> Name_Last
));
3326 -- Capture length of operand in entity
3328 Operands
(NN
) := Opnd
;
3329 Is_Fixed_Length
(NN
) := False;
3331 Var_Length
(NN
) := Make_Temporary
(Loc
, 'L');
3334 Make_Object_Declaration
(Loc
,
3335 Defining_Identifier
=> Var_Length
(NN
),
3336 Constant_Present
=> True,
3337 Object_Definition
=> New_Occurrence_Of
(Artyp
, Loc
),
3339 Make_Attribute_Reference
(Loc
,
3341 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3342 Attribute_Name
=> Name_Length
)));
3346 -- Set next entry in aggregate length array
3348 -- For first entry, make either integer literal for fixed length
3349 -- or a reference to the saved length for variable length.
3352 if Is_Fixed_Length
(1) then
3353 Aggr_Length
(1) := Make_Integer_Literal
(Loc
, Fixed_Length
(1));
3355 Aggr_Length
(1) := New_Occurrence_Of
(Var_Length
(1), Loc
);
3358 -- If entry is fixed length and only fixed lengths so far, make
3359 -- appropriate new integer literal adding new length.
3361 elsif Is_Fixed_Length
(NN
)
3362 and then Nkind
(Aggr_Length
(NN
- 1)) = N_Integer_Literal
3365 Make_Integer_Literal
(Loc
,
3366 Intval
=> Fixed_Length
(NN
) + Intval
(Aggr_Length
(NN
- 1)));
3368 -- All other cases, construct an addition node for the length and
3369 -- create an entity initialized to this length.
3372 Ent
:= Make_Temporary
(Loc
, 'L');
3374 if Is_Fixed_Length
(NN
) then
3375 Clen
:= Make_Integer_Literal
(Loc
, Fixed_Length
(NN
));
3377 Clen
:= New_Occurrence_Of
(Var_Length
(NN
), Loc
);
3381 Make_Object_Declaration
(Loc
,
3382 Defining_Identifier
=> Ent
,
3383 Constant_Present
=> True,
3384 Object_Definition
=> New_Occurrence_Of
(Artyp
, Loc
),
3387 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
- 1)),
3388 Right_Opnd
=> Clen
)));
3390 Aggr_Length
(NN
) := Make_Identifier
(Loc
, Chars
=> Chars
(Ent
));
3397 -- If we have only skipped null operands, return the last operand
3404 -- If we have only one non-null operand, return it and we are done.
3405 -- There is one case in which this cannot be done, and that is when
3406 -- the sole operand is of the element type, in which case it must be
3407 -- converted to an array, and the easiest way of doing that is to go
3408 -- through the normal general circuit.
3410 if NN
= 1 and then Base_Type
(Etype
(Operands
(1))) /= Ctyp
then
3411 Result
:= Operands
(1);
3415 -- Cases where we have a real concatenation
3417 -- Next step is to find the low bound for the result array that we
3418 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3420 -- If the ultimate ancestor of the index subtype is a constrained array
3421 -- definition, then the lower bound is that of the index subtype as
3422 -- specified by (RM 4.5.3(6)).
3424 -- The right test here is to go to the root type, and then the ultimate
3425 -- ancestor is the first subtype of this root type.
3427 if Is_Constrained
(First_Subtype
(Root_Type
(Atyp
))) then
3429 Make_Attribute_Reference
(Loc
,
3431 New_Occurrence_Of
(First_Subtype
(Root_Type
(Atyp
)), Loc
),
3432 Attribute_Name
=> Name_First
);
3434 -- If the first operand in the list has known length we know that
3435 -- the lower bound of the result is the lower bound of this operand.
3437 elsif Is_Fixed_Length
(1) then
3438 Low_Bound
:= Opnd_Low_Bound
(1);
3440 -- OK, we don't know the lower bound, we have to build a horrible
3441 -- if expression node of the form
3443 -- if Cond1'Length /= 0 then
3446 -- if Opnd2'Length /= 0 then
3451 -- The nesting ends either when we hit an operand whose length is known
3452 -- at compile time, or on reaching the last operand, whose low bound we
3453 -- take unconditionally whether or not it is null. It's easiest to do
3454 -- this with a recursive procedure:
3458 function Get_Known_Bound
(J
: Nat
) return Node_Id
;
3459 -- Returns the lower bound determined by operands J .. NN
3461 ---------------------
3462 -- Get_Known_Bound --
3463 ---------------------
3465 function Get_Known_Bound
(J
: Nat
) return Node_Id
is
3467 if Is_Fixed_Length
(J
) or else J
= NN
then
3468 return New_Copy
(Opnd_Low_Bound
(J
));
3472 Make_If_Expression
(Loc
,
3473 Expressions
=> New_List
(
3477 New_Occurrence_Of
(Var_Length
(J
), Loc
),
3479 Make_Integer_Literal
(Loc
, 0)),
3481 New_Copy
(Opnd_Low_Bound
(J
)),
3482 Get_Known_Bound
(J
+ 1)));
3484 end Get_Known_Bound
;
3487 Ent
:= Make_Temporary
(Loc
, 'L');
3490 Make_Object_Declaration
(Loc
,
3491 Defining_Identifier
=> Ent
,
3492 Constant_Present
=> True,
3493 Object_Definition
=> New_Occurrence_Of
(Ityp
, Loc
),
3494 Expression
=> Get_Known_Bound
(1)));
3496 Low_Bound
:= New_Occurrence_Of
(Ent
, Loc
);
3500 -- Now we can safely compute the upper bound, normally
3501 -- Low_Bound + Length - 1.
3506 Left_Opnd
=> To_Artyp
(New_Copy
(Low_Bound
)),
3508 Make_Op_Subtract
(Loc
,
3509 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
)),
3510 Right_Opnd
=> Make_Artyp_Literal
(1))));
3512 -- Note that calculation of the high bound may cause overflow in some
3513 -- very weird cases, so in the general case we need an overflow check on
3514 -- the high bound. We can avoid this for the common case of string types
3515 -- and other types whose index is Positive, since we chose a wider range
3516 -- for the arithmetic type.
3518 if Istyp
/= Standard_Positive
then
3519 Activate_Overflow_Check
(High_Bound
);
3522 -- Handle the exceptional case where the result is null, in which case
3523 -- case the bounds come from the last operand (so that we get the proper
3524 -- bounds if the last operand is super-flat).
3526 if Result_May_Be_Null
then
3528 Make_If_Expression
(Loc
,
3529 Expressions
=> New_List
(
3531 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
)),
3532 Right_Opnd
=> Make_Artyp_Literal
(0)),
3533 Last_Opnd_Low_Bound
,
3537 Make_If_Expression
(Loc
,
3538 Expressions
=> New_List
(
3540 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
)),
3541 Right_Opnd
=> Make_Artyp_Literal
(0)),
3542 Last_Opnd_High_Bound
,
3546 -- Here is where we insert the saved up actions
3548 Insert_Actions
(Cnode
, Actions
, Suppress
=> All_Checks
);
3550 -- Now we construct an array object with appropriate bounds. We mark
3551 -- the target as internal to prevent useless initialization when
3552 -- Initialize_Scalars is enabled. Also since this is the actual result
3553 -- entity, we make sure we have debug information for the result.
3555 Ent
:= Make_Temporary
(Loc
, 'S');
3556 Set_Is_Internal
(Ent
);
3557 Set_Needs_Debug_Info
(Ent
);
3559 -- If the bound is statically known to be out of range, we do not want
3560 -- to abort, we want a warning and a runtime constraint error. Note that
3561 -- we have arranged that the result will not be treated as a static
3562 -- constant, so we won't get an illegality during this insertion.
3564 Insert_Action
(Cnode
,
3565 Make_Object_Declaration
(Loc
,
3566 Defining_Identifier
=> Ent
,
3567 Object_Definition
=>
3568 Make_Subtype_Indication
(Loc
,
3569 Subtype_Mark
=> New_Occurrence_Of
(Atyp
, Loc
),
3571 Make_Index_Or_Discriminant_Constraint
(Loc
,
3572 Constraints
=> New_List
(
3574 Low_Bound
=> Low_Bound
,
3575 High_Bound
=> High_Bound
))))),
3576 Suppress
=> All_Checks
);
3578 -- If the result of the concatenation appears as the initializing
3579 -- expression of an object declaration, we can just rename the
3580 -- result, rather than copying it.
3582 Set_OK_To_Rename
(Ent
);
3584 -- Catch the static out of range case now
3586 if Raises_Constraint_Error
(High_Bound
) then
3587 raise Concatenation_Error
;
3590 -- Now we will generate the assignments to do the actual concatenation
3592 -- There is one case in which we will not do this, namely when all the
3593 -- following conditions are met:
3595 -- The result type is Standard.String
3597 -- There are nine or fewer retained (non-null) operands
3599 -- The optimization level is -O0
3601 -- The corresponding System.Concat_n.Str_Concat_n routine is
3602 -- available in the run time.
3604 -- The debug flag gnatd.c is not set
3606 -- If all these conditions are met then we generate a call to the
3607 -- relevant concatenation routine. The purpose of this is to avoid
3608 -- undesirable code bloat at -O0.
3610 if Atyp
= Standard_String
3611 and then NN
in 2 .. 9
3612 and then (Lib_Level_Target
3613 or else ((Optimization_Level
= 0 or else Debug_Flag_Dot_CC
)
3614 and then not Debug_Flag_Dot_C
))
3617 RR
: constant array (Nat
range 2 .. 9) of RE_Id
:=
3628 if RTE_Available
(RR
(NN
)) then
3630 Opnds
: constant List_Id
:=
3631 New_List
(New_Occurrence_Of
(Ent
, Loc
));
3634 for J
in 1 .. NN
loop
3635 if Is_List_Member
(Operands
(J
)) then
3636 Remove
(Operands
(J
));
3639 if Base_Type
(Etype
(Operands
(J
))) = Ctyp
then
3641 Make_Aggregate
(Loc
,
3642 Component_Associations
=> New_List
(
3643 Make_Component_Association
(Loc
,
3644 Choices
=> New_List
(
3645 Make_Integer_Literal
(Loc
, 1)),
3646 Expression
=> Operands
(J
)))));
3649 Append_To
(Opnds
, Operands
(J
));
3653 Insert_Action
(Cnode
,
3654 Make_Procedure_Call_Statement
(Loc
,
3655 Name
=> New_Occurrence_Of
(RTE
(RR
(NN
)), Loc
),
3656 Parameter_Associations
=> Opnds
));
3658 Result
:= New_Occurrence_Of
(Ent
, Loc
);
3665 -- Not special case so generate the assignments
3667 Known_Non_Null_Operand_Seen
:= False;
3669 for J
in 1 .. NN
loop
3671 Lo
: constant Node_Id
:=
3673 Left_Opnd
=> To_Artyp
(New_Copy
(Low_Bound
)),
3674 Right_Opnd
=> Aggr_Length
(J
- 1));
3676 Hi
: constant Node_Id
:=
3678 Left_Opnd
=> To_Artyp
(New_Copy
(Low_Bound
)),
3680 Make_Op_Subtract
(Loc
,
3681 Left_Opnd
=> Aggr_Length
(J
),
3682 Right_Opnd
=> Make_Artyp_Literal
(1)));
3685 -- Singleton case, simple assignment
3687 if Base_Type
(Etype
(Operands
(J
))) = Ctyp
then
3688 Known_Non_Null_Operand_Seen
:= True;
3689 Insert_Action
(Cnode
,
3690 Make_Assignment_Statement
(Loc
,
3692 Make_Indexed_Component
(Loc
,
3693 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
3694 Expressions
=> New_List
(To_Ityp
(Lo
))),
3695 Expression
=> Operands
(J
)),
3696 Suppress
=> All_Checks
);
3698 -- Array case, slice assignment, skipped when argument is fixed
3699 -- length and known to be null.
3701 elsif (not Is_Fixed_Length
(J
)) or else (Fixed_Length
(J
) > 0) then
3704 Make_Assignment_Statement
(Loc
,
3708 New_Occurrence_Of
(Ent
, Loc
),
3711 Low_Bound
=> To_Ityp
(Lo
),
3712 High_Bound
=> To_Ityp
(Hi
))),
3713 Expression
=> Operands
(J
));
3715 if Is_Fixed_Length
(J
) then
3716 Known_Non_Null_Operand_Seen
:= True;
3718 elsif not Known_Non_Null_Operand_Seen
then
3720 -- Here if operand length is not statically known and no
3721 -- operand known to be non-null has been processed yet.
3722 -- If operand length is 0, we do not need to perform the
3723 -- assignment, and we must avoid the evaluation of the
3724 -- high bound of the slice, since it may underflow if the
3725 -- low bound is Ityp'First.
3728 Make_Implicit_If_Statement
(Cnode
,
3732 New_Occurrence_Of
(Var_Length
(J
), Loc
),
3733 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
3734 Then_Statements
=> New_List
(Assign
));
3737 Insert_Action
(Cnode
, Assign
, Suppress
=> All_Checks
);
3743 -- Finally we build the result, which is a reference to the array object
3745 Result
:= New_Occurrence_Of
(Ent
, Loc
);
3748 Rewrite
(Cnode
, Result
);
3749 Analyze_And_Resolve
(Cnode
, Atyp
);
3752 when Concatenation_Error
=>
3754 -- Kill warning generated for the declaration of the static out of
3755 -- range high bound, and instead generate a Constraint_Error with
3756 -- an appropriate specific message.
3758 Kill_Dead_Code
(Declaration_Node
(Entity
(High_Bound
)));
3759 Apply_Compile_Time_Constraint_Error
3761 Msg
=> "concatenation result upper bound out of range??",
3762 Reason
=> CE_Range_Check_Failed
);
3763 end Expand_Concatenate
;
3765 ---------------------------------------------------
3766 -- Expand_Membership_Minimize_Eliminate_Overflow --
3767 ---------------------------------------------------
3769 procedure Expand_Membership_Minimize_Eliminate_Overflow
(N
: Node_Id
) is
3770 pragma Assert
(Nkind
(N
) = N_In
);
3771 -- Despite the name, this routine applies only to N_In, not to
3772 -- N_Not_In. The latter is always rewritten as not (X in Y).
3774 Result_Type
: constant Entity_Id
:= Etype
(N
);
3775 -- Capture result type, may be a derived boolean type
3777 Loc
: constant Source_Ptr
:= Sloc
(N
);
3778 Lop
: constant Node_Id
:= Left_Opnd
(N
);
3779 Rop
: constant Node_Id
:= Right_Opnd
(N
);
3781 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3782 -- is thus tempting to capture these values, but due to the rewrites
3783 -- that occur as a result of overflow checking, these values change
3784 -- as we go along, and it is safe just to always use Etype explicitly.
3786 Restype
: constant Entity_Id
:= Etype
(N
);
3790 -- Bounds in Minimize calls, not used currently
3792 LLIB
: constant Entity_Id
:= Base_Type
(Standard_Long_Long_Integer
);
3793 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3796 Minimize_Eliminate_Overflows
(Lop
, Lo
, Hi
, Top_Level
=> False);
3798 -- If right operand is a subtype name, and the subtype name has no
3799 -- predicate, then we can just replace the right operand with an
3800 -- explicit range T'First .. T'Last, and use the explicit range code.
3802 if Nkind
(Rop
) /= N_Range
3803 and then No
(Predicate_Function
(Etype
(Rop
)))
3806 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
3811 Make_Attribute_Reference
(Loc
,
3812 Attribute_Name
=> Name_First
,
3813 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
)),
3815 Make_Attribute_Reference
(Loc
,
3816 Attribute_Name
=> Name_Last
,
3817 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
))));
3818 Analyze_And_Resolve
(Rop
, Rtyp
, Suppress
=> All_Checks
);
3822 -- Here for the explicit range case. Note that the bounds of the range
3823 -- have not been processed for minimized or eliminated checks.
3825 if Nkind
(Rop
) = N_Range
then
3826 Minimize_Eliminate_Overflows
3827 (Low_Bound
(Rop
), Lo
, Hi
, Top_Level
=> False);
3828 Minimize_Eliminate_Overflows
3829 (High_Bound
(Rop
), Lo
, Hi
, Top_Level
=> False);
3831 -- We have A in B .. C, treated as A >= B and then A <= C
3835 if Is_RTE
(Etype
(Lop
), RE_Bignum
)
3836 or else Is_RTE
(Etype
(Low_Bound
(Rop
)), RE_Bignum
)
3837 or else Is_RTE
(Etype
(High_Bound
(Rop
)), RE_Bignum
)
3840 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
3841 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
3842 L
: constant Entity_Id
:=
3843 Make_Defining_Identifier
(Loc
, Name_uL
);
3844 Lopnd
: constant Node_Id
:= Convert_To_Bignum
(Lop
);
3845 Lbound
: constant Node_Id
:=
3846 Convert_To_Bignum
(Low_Bound
(Rop
));
3847 Hbound
: constant Node_Id
:=
3848 Convert_To_Bignum
(High_Bound
(Rop
));
3850 -- Now we rewrite the membership test node to look like
3853 -- Bnn : Result_Type;
3855 -- M : Mark_Id := SS_Mark;
3856 -- L : Bignum := Lopnd;
3858 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3866 -- Insert declaration of L into declarations of bignum block
3869 (Last
(Declarations
(Blk
)),
3870 Make_Object_Declaration
(Loc
,
3871 Defining_Identifier
=> L
,
3872 Object_Definition
=>
3873 New_Occurrence_Of
(RTE
(RE_Bignum
), Loc
),
3874 Expression
=> Lopnd
));
3876 -- Insert assignment to Bnn into expressions of bignum block
3879 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
3880 Make_Assignment_Statement
(Loc
,
3881 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
3885 Make_Function_Call
(Loc
,
3887 New_Occurrence_Of
(RTE
(RE_Big_GE
), Loc
),
3888 Parameter_Associations
=> New_List
(
3889 New_Occurrence_Of
(L
, Loc
),
3893 Make_Function_Call
(Loc
,
3895 New_Occurrence_Of
(RTE
(RE_Big_LE
), Loc
),
3896 Parameter_Associations
=> New_List
(
3897 New_Occurrence_Of
(L
, Loc
),
3900 -- Now rewrite the node
3903 Make_Expression_With_Actions
(Loc
,
3904 Actions
=> New_List
(
3905 Make_Object_Declaration
(Loc
,
3906 Defining_Identifier
=> Bnn
,
3907 Object_Definition
=>
3908 New_Occurrence_Of
(Result_Type
, Loc
)),
3910 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
3911 Analyze_And_Resolve
(N
, Result_Type
);
3915 -- Here if no bignums around
3918 -- Case where types are all the same
3920 if Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(Low_Bound
(Rop
)))
3922 Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(High_Bound
(Rop
)))
3926 -- If types are not all the same, it means that we have rewritten
3927 -- at least one of them to be of type Long_Long_Integer, and we
3928 -- will convert the other operands to Long_Long_Integer.
3931 Convert_To_And_Rewrite
(LLIB
, Lop
);
3932 Set_Analyzed
(Lop
, False);
3933 Analyze_And_Resolve
(Lop
, LLIB
);
3935 -- For the right operand, avoid unnecessary recursion into
3936 -- this routine, we know that overflow is not possible.
3938 Convert_To_And_Rewrite
(LLIB
, Low_Bound
(Rop
));
3939 Convert_To_And_Rewrite
(LLIB
, High_Bound
(Rop
));
3940 Set_Analyzed
(Rop
, False);
3941 Analyze_And_Resolve
(Rop
, LLIB
, Suppress
=> Overflow_Check
);
3944 -- Now the three operands are of the same signed integer type,
3945 -- so we can use the normal expansion routine for membership,
3946 -- setting the flag to prevent recursion into this procedure.
3948 Set_No_Minimize_Eliminate
(N
);
3952 -- Right operand is a subtype name and the subtype has a predicate. We
3953 -- have to make sure the predicate is checked, and for that we need to
3954 -- use the standard N_In circuitry with appropriate types.
3957 pragma Assert
(Present
(Predicate_Function
(Etype
(Rop
))));
3959 -- If types are "right", just call Expand_N_In preventing recursion
3961 if Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(Rop
)) then
3962 Set_No_Minimize_Eliminate
(N
);
3967 elsif Is_RTE
(Etype
(Lop
), RE_Bignum
) then
3969 -- For X in T, we want to rewrite our node as
3972 -- Bnn : Result_Type;
3975 -- M : Mark_Id := SS_Mark;
3976 -- Lnn : Long_Long_Integer'Base
3982 -- if not Bignum_In_LLI_Range (Nnn) then
3985 -- Lnn := From_Bignum (Nnn);
3987 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3988 -- and then T'Base (Lnn) in T;
3997 -- A bit gruesome, but there doesn't seem to be a simpler way
4000 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
4001 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
4002 Lnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'L', N
);
4003 Nnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'N', N
);
4004 T
: constant Entity_Id
:= Etype
(Rop
);
4005 TB
: constant Entity_Id
:= Base_Type
(T
);
4009 -- Mark the last membership operation to prevent recursion
4013 Left_Opnd
=> Convert_To
(TB
, New_Occurrence_Of
(Lnn
, Loc
)),
4014 Right_Opnd
=> New_Occurrence_Of
(T
, Loc
));
4015 Set_No_Minimize_Eliminate
(Nin
);
4017 -- Now decorate the block
4020 (Last
(Declarations
(Blk
)),
4021 Make_Object_Declaration
(Loc
,
4022 Defining_Identifier
=> Lnn
,
4023 Object_Definition
=> New_Occurrence_Of
(LLIB
, Loc
)));
4026 (Last
(Declarations
(Blk
)),
4027 Make_Object_Declaration
(Loc
,
4028 Defining_Identifier
=> Nnn
,
4029 Object_Definition
=>
4030 New_Occurrence_Of
(RTE
(RE_Bignum
), Loc
)));
4033 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
4035 Make_Assignment_Statement
(Loc
,
4036 Name
=> New_Occurrence_Of
(Nnn
, Loc
),
4037 Expression
=> Relocate_Node
(Lop
)),
4039 Make_Implicit_If_Statement
(N
,
4043 Make_Function_Call
(Loc
,
4046 (RTE
(RE_Bignum_In_LLI_Range
), Loc
),
4047 Parameter_Associations
=> New_List
(
4048 New_Occurrence_Of
(Nnn
, Loc
)))),
4050 Then_Statements
=> New_List
(
4051 Make_Assignment_Statement
(Loc
,
4052 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
4054 New_Occurrence_Of
(Standard_False
, Loc
))),
4056 Else_Statements
=> New_List
(
4057 Make_Assignment_Statement
(Loc
,
4058 Name
=> New_Occurrence_Of
(Lnn
, Loc
),
4060 Make_Function_Call
(Loc
,
4062 New_Occurrence_Of
(RTE
(RE_From_Bignum
), Loc
),
4063 Parameter_Associations
=> New_List
(
4064 New_Occurrence_Of
(Nnn
, Loc
)))),
4066 Make_Assignment_Statement
(Loc
,
4067 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
4072 Left_Opnd
=> New_Occurrence_Of
(Lnn
, Loc
),
4077 Make_Attribute_Reference
(Loc
,
4078 Attribute_Name
=> Name_First
,
4080 New_Occurrence_Of
(TB
, Loc
))),
4084 Make_Attribute_Reference
(Loc
,
4085 Attribute_Name
=> Name_Last
,
4087 New_Occurrence_Of
(TB
, Loc
))))),
4089 Right_Opnd
=> Nin
))))));
4091 -- Now we can do the rewrite
4094 Make_Expression_With_Actions
(Loc
,
4095 Actions
=> New_List
(
4096 Make_Object_Declaration
(Loc
,
4097 Defining_Identifier
=> Bnn
,
4098 Object_Definition
=>
4099 New_Occurrence_Of
(Result_Type
, Loc
)),
4101 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
4102 Analyze_And_Resolve
(N
, Result_Type
);
4106 -- Not bignum case, but types don't match (this means we rewrote the
4107 -- left operand to be Long_Long_Integer).
4110 pragma Assert
(Base_Type
(Etype
(Lop
)) = LLIB
);
4112 -- We rewrite the membership test as (where T is the type with
4113 -- the predicate, i.e. the type of the right operand)
4115 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
4116 -- and then T'Base (Lop) in T
4119 T
: constant Entity_Id
:= Etype
(Rop
);
4120 TB
: constant Entity_Id
:= Base_Type
(T
);
4124 -- The last membership test is marked to prevent recursion
4128 Left_Opnd
=> Convert_To
(TB
, Duplicate_Subexpr
(Lop
)),
4129 Right_Opnd
=> New_Occurrence_Of
(T
, Loc
));
4130 Set_No_Minimize_Eliminate
(Nin
);
4132 -- Now do the rewrite
4143 Make_Attribute_Reference
(Loc
,
4144 Attribute_Name
=> Name_First
,
4146 New_Occurrence_Of
(TB
, Loc
))),
4149 Make_Attribute_Reference
(Loc
,
4150 Attribute_Name
=> Name_Last
,
4152 New_Occurrence_Of
(TB
, Loc
))))),
4153 Right_Opnd
=> Nin
));
4154 Set_Analyzed
(N
, False);
4155 Analyze_And_Resolve
(N
, Restype
);
4159 end Expand_Membership_Minimize_Eliminate_Overflow
;
4161 ------------------------
4162 -- Expand_N_Allocator --
4163 ------------------------
4165 procedure Expand_N_Allocator
(N
: Node_Id
) is
4166 Etyp
: constant Entity_Id
:= Etype
(Expression
(N
));
4167 Loc
: constant Source_Ptr
:= Sloc
(N
);
4168 PtrT
: constant Entity_Id
:= Etype
(N
);
4170 procedure Rewrite_Coextension
(N
: Node_Id
);
4171 -- Static coextensions have the same lifetime as the entity they
4172 -- constrain. Such occurrences can be rewritten as aliased objects
4173 -- and their unrestricted access used instead of the coextension.
4175 function Size_In_Storage_Elements
(E
: Entity_Id
) return Node_Id
;
4176 -- Given a constrained array type E, returns a node representing the
4177 -- code to compute the size in storage elements for the given type.
4178 -- This is done without using the attribute (which malfunctions for
4181 -------------------------
4182 -- Rewrite_Coextension --
4183 -------------------------
4185 procedure Rewrite_Coextension
(N
: Node_Id
) is
4186 Temp_Id
: constant Node_Id
:= Make_Temporary
(Loc
, 'C');
4187 Temp_Decl
: Node_Id
;
4191 -- Cnn : aliased Etyp;
4194 Make_Object_Declaration
(Loc
,
4195 Defining_Identifier
=> Temp_Id
,
4196 Aliased_Present
=> True,
4197 Object_Definition
=> New_Occurrence_Of
(Etyp
, Loc
));
4199 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
4200 Set_Expression
(Temp_Decl
, Expression
(Expression
(N
)));
4203 Insert_Action
(N
, Temp_Decl
);
4205 Make_Attribute_Reference
(Loc
,
4206 Prefix
=> New_Occurrence_Of
(Temp_Id
, Loc
),
4207 Attribute_Name
=> Name_Unrestricted_Access
));
4209 Analyze_And_Resolve
(N
, PtrT
);
4210 end Rewrite_Coextension
;
4212 ------------------------------
4213 -- Size_In_Storage_Elements --
4214 ------------------------------
4216 function Size_In_Storage_Elements
(E
: Entity_Id
) return Node_Id
is
4218 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4219 -- However, the reason for the existence of this function is
4220 -- to construct a test for sizes too large, which means near the
4221 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4222 -- is that we get overflows when sizes are greater than 2**31.
4224 -- So what we end up doing for array types is to use the expression:
4226 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4228 -- which avoids this problem. All this is a bit bogus, but it does
4229 -- mean we catch common cases of trying to allocate arrays that
4230 -- are too large, and which in the absence of a check results in
4231 -- undetected chaos ???
4233 -- Note in particular that this is a pessimistic estimate in the
4234 -- case of packed array types, where an array element might occupy
4235 -- just a fraction of a storage element???
4242 for J
in 1 .. Number_Dimensions
(E
) loop
4244 Make_Attribute_Reference
(Loc
,
4245 Prefix
=> New_Occurrence_Of
(E
, Loc
),
4246 Attribute_Name
=> Name_Length
,
4247 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, J
)));
4254 Make_Op_Multiply
(Loc
,
4261 Make_Op_Multiply
(Loc
,
4264 Make_Attribute_Reference
(Loc
,
4265 Prefix
=> New_Occurrence_Of
(Component_Type
(E
), Loc
),
4266 Attribute_Name
=> Name_Max_Size_In_Storage_Elements
));
4268 end Size_In_Storage_Elements
;
4272 Dtyp
: constant Entity_Id
:= Available_View
(Designated_Type
(PtrT
));
4276 Rel_Typ
: Entity_Id
;
4279 -- Start of processing for Expand_N_Allocator
4282 -- RM E.2.3(22). We enforce that the expected type of an allocator
4283 -- shall not be a remote access-to-class-wide-limited-private type
4285 -- Why is this being done at expansion time, seems clearly wrong ???
4287 Validate_Remote_Access_To_Class_Wide_Type
(N
);
4289 -- Processing for anonymous access-to-controlled types. These access
4290 -- types receive a special finalization master which appears in the
4291 -- declarations of the enclosing semantic unit. This expansion is done
4292 -- now to ensure that any additional types generated by this routine or
4293 -- Expand_Allocator_Expression inherit the proper type attributes.
4295 if (Ekind
(PtrT
) = E_Anonymous_Access_Type
4296 or else (Is_Itype
(PtrT
) and then No
(Finalization_Master
(PtrT
))))
4297 and then Needs_Finalization
(Dtyp
)
4299 -- Detect the allocation of an anonymous controlled object where the
4300 -- type of the context is named. For example:
4302 -- procedure Proc (Ptr : Named_Access_Typ);
4303 -- Proc (new Designated_Typ);
4305 -- Regardless of the anonymous-to-named access type conversion, the
4306 -- lifetime of the object must be associated with the named access
4307 -- type. Use the finalization-related attributes of this type.
4309 if Nkind_In
(Parent
(N
), N_Type_Conversion
,
4310 N_Unchecked_Type_Conversion
)
4311 and then Ekind_In
(Etype
(Parent
(N
)), E_Access_Subtype
,
4313 E_General_Access_Type
)
4315 Rel_Typ
:= Etype
(Parent
(N
));
4320 -- Anonymous access-to-controlled types allocate on the global pool.
4321 -- Do not set this attribute on .NET/JVM since those targets do not
4322 -- support pools. Note that this is a "root type only" attribute.
4324 if No
(Associated_Storage_Pool
(PtrT
)) and then VM_Target
= No_VM
then
4325 if Present
(Rel_Typ
) then
4326 Set_Associated_Storage_Pool
4327 (Root_Type
(PtrT
), Associated_Storage_Pool
(Rel_Typ
));
4329 Set_Associated_Storage_Pool
4330 (Root_Type
(PtrT
), RTE
(RE_Global_Pool_Object
));
4334 -- The finalization master must be inserted and analyzed as part of
4335 -- the current semantic unit. Note that the master is updated when
4336 -- analysis changes current units. Note that this is a "root type
4339 if Present
(Rel_Typ
) then
4340 Set_Finalization_Master
4341 (Root_Type
(PtrT
), Finalization_Master
(Rel_Typ
));
4343 Set_Finalization_Master
4344 (Root_Type
(PtrT
), Current_Anonymous_Master
);
4348 -- Set the storage pool and find the appropriate version of Allocate to
4349 -- call. Do not overwrite the storage pool if it is already set, which
4350 -- can happen for build-in-place function returns (see
4351 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4353 if No
(Storage_Pool
(N
)) then
4354 Pool
:= Associated_Storage_Pool
(Root_Type
(PtrT
));
4356 if Present
(Pool
) then
4357 Set_Storage_Pool
(N
, Pool
);
4359 if Is_RTE
(Pool
, RE_SS_Pool
) then
4360 if VM_Target
= No_VM
then
4361 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
4364 -- In the case of an allocator for a simple storage pool, locate
4365 -- and save a reference to the pool type's Allocate routine.
4367 elsif Present
(Get_Rep_Pragma
4368 (Etype
(Pool
), Name_Simple_Storage_Pool_Type
))
4371 Pool_Type
: constant Entity_Id
:= Base_Type
(Etype
(Pool
));
4372 Alloc_Op
: Entity_Id
;
4374 Alloc_Op
:= Get_Name_Entity_Id
(Name_Allocate
);
4375 while Present
(Alloc_Op
) loop
4376 if Scope
(Alloc_Op
) = Scope
(Pool_Type
)
4377 and then Present
(First_Formal
(Alloc_Op
))
4378 and then Etype
(First_Formal
(Alloc_Op
)) = Pool_Type
4380 Set_Procedure_To_Call
(N
, Alloc_Op
);
4383 Alloc_Op
:= Homonym
(Alloc_Op
);
4388 elsif Is_Class_Wide_Type
(Etype
(Pool
)) then
4389 Set_Procedure_To_Call
(N
, RTE
(RE_Allocate_Any
));
4392 Set_Procedure_To_Call
(N
,
4393 Find_Prim_Op
(Etype
(Pool
), Name_Allocate
));
4398 -- Under certain circumstances we can replace an allocator by an access
4399 -- to statically allocated storage. The conditions, as noted in AARM
4400 -- 3.10 (10c) are as follows:
4402 -- Size and initial value is known at compile time
4403 -- Access type is access-to-constant
4405 -- The allocator is not part of a constraint on a record component,
4406 -- because in that case the inserted actions are delayed until the
4407 -- record declaration is fully analyzed, which is too late for the
4408 -- analysis of the rewritten allocator.
4410 if Is_Access_Constant
(PtrT
)
4411 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
4412 and then Compile_Time_Known_Value
(Expression
(Expression
(N
)))
4413 and then Size_Known_At_Compile_Time
4414 (Etype
(Expression
(Expression
(N
))))
4415 and then not Is_Record_Type
(Current_Scope
)
4417 -- Here we can do the optimization. For the allocator
4421 -- We insert an object declaration
4423 -- Tnn : aliased x := y;
4425 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4426 -- marked as requiring static allocation.
4428 Temp
:= Make_Temporary
(Loc
, 'T', Expression
(Expression
(N
)));
4429 Desig
:= Subtype_Mark
(Expression
(N
));
4431 -- If context is constrained, use constrained subtype directly,
4432 -- so that the constant is not labelled as having a nominally
4433 -- unconstrained subtype.
4435 if Entity
(Desig
) = Base_Type
(Dtyp
) then
4436 Desig
:= New_Occurrence_Of
(Dtyp
, Loc
);
4440 Make_Object_Declaration
(Loc
,
4441 Defining_Identifier
=> Temp
,
4442 Aliased_Present
=> True,
4443 Constant_Present
=> Is_Access_Constant
(PtrT
),
4444 Object_Definition
=> Desig
,
4445 Expression
=> Expression
(Expression
(N
))));
4448 Make_Attribute_Reference
(Loc
,
4449 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
4450 Attribute_Name
=> Name_Unrestricted_Access
));
4452 Analyze_And_Resolve
(N
, PtrT
);
4454 -- We set the variable as statically allocated, since we don't want
4455 -- it going on the stack of the current procedure.
4457 Set_Is_Statically_Allocated
(Temp
);
4461 -- Same if the allocator is an access discriminant for a local object:
4462 -- instead of an allocator we create a local value and constrain the
4463 -- enclosing object with the corresponding access attribute.
4465 if Is_Static_Coextension
(N
) then
4466 Rewrite_Coextension
(N
);
4470 -- Check for size too large, we do this because the back end misses
4471 -- proper checks here and can generate rubbish allocation calls when
4472 -- we are near the limit. We only do this for the 32-bit address case
4473 -- since that is from a practical point of view where we see a problem.
4475 if System_Address_Size
= 32
4476 and then not Storage_Checks_Suppressed
(PtrT
)
4477 and then not Storage_Checks_Suppressed
(Dtyp
)
4478 and then not Storage_Checks_Suppressed
(Etyp
)
4480 -- The check we want to generate should look like
4482 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4483 -- raise Storage_Error;
4486 -- where 3.5 gigabytes is a constant large enough to accommodate any
4487 -- reasonable request for. But we can't do it this way because at
4488 -- least at the moment we don't compute this attribute right, and
4489 -- can silently give wrong results when the result gets large. Since
4490 -- this is all about large results, that's bad, so instead we only
4491 -- apply the check for constrained arrays, and manually compute the
4492 -- value of the attribute ???
4494 if Is_Array_Type
(Etyp
) and then Is_Constrained
(Etyp
) then
4496 Make_Raise_Storage_Error
(Loc
,
4499 Left_Opnd
=> Size_In_Storage_Elements
(Etyp
),
4501 Make_Integer_Literal
(Loc
, Uint_7
* (Uint_2
** 29))),
4502 Reason
=> SE_Object_Too_Large
));
4506 -- If no storage pool has been specified and we have the restriction
4507 -- No_Standard_Allocators_After_Elaboration is present, then generate
4508 -- a call to Elaboration_Allocators.Check_Standard_Allocator.
4510 if Nkind
(N
) = N_Allocator
4511 and then No
(Storage_Pool
(N
))
4512 and then Restriction_Active
(No_Standard_Allocators_After_Elaboration
)
4515 Make_Procedure_Call_Statement
(Loc
,
4517 New_Occurrence_Of
(RTE
(RE_Check_Standard_Allocator
), Loc
)));
4520 -- Handle case of qualified expression (other than optimization above)
4521 -- First apply constraint checks, because the bounds or discriminants
4522 -- in the aggregate might not match the subtype mark in the allocator.
4524 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
4525 Apply_Constraint_Check
4526 (Expression
(Expression
(N
)), Etype
(Expression
(N
)));
4528 Expand_Allocator_Expression
(N
);
4532 -- If the allocator is for a type which requires initialization, and
4533 -- there is no initial value (i.e. operand is a subtype indication
4534 -- rather than a qualified expression), then we must generate a call to
4535 -- the initialization routine using an expressions action node:
4537 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4539 -- Here ptr_T is the pointer type for the allocator, and T is the
4540 -- subtype of the allocator. A special case arises if the designated
4541 -- type of the access type is a task or contains tasks. In this case
4542 -- the call to Init (Temp.all ...) is replaced by code that ensures
4543 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4544 -- for details). In addition, if the type T is a task type, then the
4545 -- first argument to Init must be converted to the task record type.
4548 T
: constant Entity_Id
:= Entity
(Expression
(N
));
4554 Init_Arg1
: Node_Id
;
4555 Temp_Decl
: Node_Id
;
4556 Temp_Type
: Entity_Id
;
4559 if No_Initialization
(N
) then
4561 -- Even though this might be a simple allocation, create a custom
4562 -- Allocate if the context requires it. Since .NET/JVM compilers
4563 -- do not support pools, this step is skipped.
4565 if VM_Target
= No_VM
4566 and then Present
(Finalization_Master
(PtrT
))
4568 Build_Allocate_Deallocate_Proc
4570 Is_Allocate
=> True);
4573 -- Case of no initialization procedure present
4575 elsif not Has_Non_Null_Base_Init_Proc
(T
) then
4577 -- Case of simple initialization required
4579 if Needs_Simple_Initialization
(T
) then
4580 Check_Restriction
(No_Default_Initialization
, N
);
4581 Rewrite
(Expression
(N
),
4582 Make_Qualified_Expression
(Loc
,
4583 Subtype_Mark
=> New_Occurrence_Of
(T
, Loc
),
4584 Expression
=> Get_Simple_Init_Val
(T
, N
)));
4586 Analyze_And_Resolve
(Expression
(Expression
(N
)), T
);
4587 Analyze_And_Resolve
(Expression
(N
), T
);
4588 Set_Paren_Count
(Expression
(Expression
(N
)), 1);
4589 Expand_N_Allocator
(N
);
4591 -- No initialization required
4597 -- Case of initialization procedure present, must be called
4600 Check_Restriction
(No_Default_Initialization
, N
);
4602 if not Restriction_Active
(No_Default_Initialization
) then
4603 Init
:= Base_Init_Proc
(T
);
4605 Temp
:= Make_Temporary
(Loc
, 'P');
4607 -- Construct argument list for the initialization routine call
4610 Make_Explicit_Dereference
(Loc
,
4612 New_Occurrence_Of
(Temp
, Loc
));
4614 Set_Assignment_OK
(Init_Arg1
);
4617 -- The initialization procedure expects a specific type. if the
4618 -- context is access to class wide, indicate that the object
4619 -- being allocated has the right specific type.
4621 if Is_Class_Wide_Type
(Dtyp
) then
4622 Init_Arg1
:= Unchecked_Convert_To
(T
, Init_Arg1
);
4625 -- If designated type is a concurrent type or if it is private
4626 -- type whose definition is a concurrent type, the first
4627 -- argument in the Init routine has to be unchecked conversion
4628 -- to the corresponding record type. If the designated type is
4629 -- a derived type, also convert the argument to its root type.
4631 if Is_Concurrent_Type
(T
) then
4633 Unchecked_Convert_To
(
4634 Corresponding_Record_Type
(T
), Init_Arg1
);
4636 elsif Is_Private_Type
(T
)
4637 and then Present
(Full_View
(T
))
4638 and then Is_Concurrent_Type
(Full_View
(T
))
4641 Unchecked_Convert_To
4642 (Corresponding_Record_Type
(Full_View
(T
)), Init_Arg1
);
4644 elsif Etype
(First_Formal
(Init
)) /= Base_Type
(T
) then
4646 Ftyp
: constant Entity_Id
:= Etype
(First_Formal
(Init
));
4649 Init_Arg1
:= OK_Convert_To
(Etype
(Ftyp
), Init_Arg1
);
4650 Set_Etype
(Init_Arg1
, Ftyp
);
4654 Args
:= New_List
(Init_Arg1
);
4656 -- For the task case, pass the Master_Id of the access type as
4657 -- the value of the _Master parameter, and _Chain as the value
4658 -- of the _Chain parameter (_Chain will be defined as part of
4659 -- the generated code for the allocator).
4661 -- In Ada 2005, the context may be a function that returns an
4662 -- anonymous access type. In that case the Master_Id has been
4663 -- created when expanding the function declaration.
4665 if Has_Task
(T
) then
4666 if No
(Master_Id
(Base_Type
(PtrT
))) then
4668 -- The designated type was an incomplete type, and the
4669 -- access type did not get expanded. Salvage it now.
4671 if not Restriction_Active
(No_Task_Hierarchy
) then
4672 if Present
(Parent
(Base_Type
(PtrT
))) then
4673 Expand_N_Full_Type_Declaration
4674 (Parent
(Base_Type
(PtrT
)));
4676 -- The only other possibility is an itype. For this
4677 -- case, the master must exist in the context. This is
4678 -- the case when the allocator initializes an access
4679 -- component in an init-proc.
4682 pragma Assert
(Is_Itype
(PtrT
));
4683 Build_Master_Renaming
(PtrT
, N
);
4688 -- If the context of the allocator is a declaration or an
4689 -- assignment, we can generate a meaningful image for it,
4690 -- even though subsequent assignments might remove the
4691 -- connection between task and entity. We build this image
4692 -- when the left-hand side is a simple variable, a simple
4693 -- indexed assignment or a simple selected component.
4695 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
4697 Nam
: constant Node_Id
:= Name
(Parent
(N
));
4700 if Is_Entity_Name
(Nam
) then
4702 Build_Task_Image_Decls
4705 (Entity
(Nam
), Sloc
(Nam
)), T
);
4707 elsif Nkind_In
(Nam
, N_Indexed_Component
,
4708 N_Selected_Component
)
4709 and then Is_Entity_Name
(Prefix
(Nam
))
4712 Build_Task_Image_Decls
4713 (Loc
, Nam
, Etype
(Prefix
(Nam
)));
4715 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
4719 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
4721 Build_Task_Image_Decls
4722 (Loc
, Defining_Identifier
(Parent
(N
)), T
);
4725 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
4728 if Restriction_Active
(No_Task_Hierarchy
) then
4730 New_Occurrence_Of
(RTE
(RE_Library_Task_Level
), Loc
));
4734 (Master_Id
(Base_Type
(Root_Type
(PtrT
))), Loc
));
4737 Append_To
(Args
, Make_Identifier
(Loc
, Name_uChain
));
4739 Decl
:= Last
(Decls
);
4741 New_Occurrence_Of
(Defining_Identifier
(Decl
), Loc
));
4743 -- Has_Task is false, Decls not used
4749 -- Add discriminants if discriminated type
4752 Dis
: Boolean := False;
4756 if Has_Discriminants
(T
) then
4760 elsif Is_Private_Type
(T
)
4761 and then Present
(Full_View
(T
))
4762 and then Has_Discriminants
(Full_View
(T
))
4765 Typ
:= Full_View
(T
);
4770 -- If the allocated object will be constrained by the
4771 -- default values for discriminants, then build a subtype
4772 -- with those defaults, and change the allocated subtype
4773 -- to that. Note that this happens in fewer cases in Ada
4776 if not Is_Constrained
(Typ
)
4777 and then Present
(Discriminant_Default_Value
4778 (First_Discriminant
(Typ
)))
4779 and then (Ada_Version
< Ada_2005
4781 Object_Type_Has_Constrained_Partial_View
4782 (Typ
, Current_Scope
))
4784 Typ
:= Build_Default_Subtype
(Typ
, N
);
4785 Set_Expression
(N
, New_Occurrence_Of
(Typ
, Loc
));
4788 Discr
:= First_Elmt
(Discriminant_Constraint
(Typ
));
4789 while Present
(Discr
) loop
4790 Nod
:= Node
(Discr
);
4791 Append
(New_Copy_Tree
(Node
(Discr
)), Args
);
4793 -- AI-416: when the discriminant constraint is an
4794 -- anonymous access type make sure an accessibility
4795 -- check is inserted if necessary (3.10.2(22.q/2))
4797 if Ada_Version
>= Ada_2005
4799 Ekind
(Etype
(Nod
)) = E_Anonymous_Access_Type
4801 Apply_Accessibility_Check
4802 (Nod
, Typ
, Insert_Node
=> Nod
);
4810 -- We set the allocator as analyzed so that when we analyze
4811 -- the if expression node, we do not get an unwanted recursive
4812 -- expansion of the allocator expression.
4814 Set_Analyzed
(N
, True);
4815 Nod
:= Relocate_Node
(N
);
4817 -- Here is the transformation:
4818 -- input: new Ctrl_Typ
4819 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4820 -- Ctrl_TypIP (Temp.all, ...);
4821 -- [Deep_]Initialize (Temp.all);
4823 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4824 -- is the subtype of the allocator.
4827 Make_Object_Declaration
(Loc
,
4828 Defining_Identifier
=> Temp
,
4829 Constant_Present
=> True,
4830 Object_Definition
=> New_Occurrence_Of
(Temp_Type
, Loc
),
4833 Set_Assignment_OK
(Temp_Decl
);
4834 Insert_Action
(N
, Temp_Decl
, Suppress
=> All_Checks
);
4836 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
4838 -- If the designated type is a task type or contains tasks,
4839 -- create block to activate created tasks, and insert
4840 -- declaration for Task_Image variable ahead of call.
4842 if Has_Task
(T
) then
4844 L
: constant List_Id
:= New_List
;
4847 Build_Task_Allocate_Block
(L
, Nod
, Args
);
4849 Insert_List_Before
(First
(Declarations
(Blk
)), Decls
);
4850 Insert_Actions
(N
, L
);
4855 Make_Procedure_Call_Statement
(Loc
,
4856 Name
=> New_Occurrence_Of
(Init
, Loc
),
4857 Parameter_Associations
=> Args
));
4860 if Needs_Finalization
(T
) then
4863 -- [Deep_]Initialize (Init_Arg1);
4867 (Obj_Ref
=> New_Copy_Tree
(Init_Arg1
),
4870 if Present
(Finalization_Master
(PtrT
)) then
4872 -- Special processing for .NET/JVM, the allocated object
4873 -- is attached to the finalization master. Generate:
4875 -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1));
4877 -- Types derived from [Limited_]Controlled are the only
4878 -- ones considered since they have fields Prev and Next.
4880 if VM_Target
/= No_VM
then
4881 if Is_Controlled
(T
) then
4884 (Obj_Ref
=> New_Copy_Tree
(Init_Arg1
),
4888 -- Default case, generate:
4890 -- Set_Finalize_Address
4891 -- (<PtrT>FM, <T>FD'Unrestricted_Access);
4893 -- Do not generate this call in CodePeer mode, as TSS
4894 -- primitive Finalize_Address is not created in this
4897 elsif not CodePeer_Mode
then
4899 Make_Set_Finalize_Address_Call
4907 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
4908 Analyze_And_Resolve
(N
, PtrT
);
4913 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
4914 -- object that has been rewritten as a reference, we displace "this"
4915 -- to reference properly its secondary dispatch table.
4917 if Nkind
(N
) = N_Identifier
and then Is_Interface
(Dtyp
) then
4918 Displace_Allocator_Pointer
(N
);
4922 when RE_Not_Available
=>
4924 end Expand_N_Allocator
;
4926 -----------------------
4927 -- Expand_N_And_Then --
4928 -----------------------
4930 procedure Expand_N_And_Then
(N
: Node_Id
)
4931 renames Expand_Short_Circuit_Operator
;
4933 ------------------------------
4934 -- Expand_N_Case_Expression --
4935 ------------------------------
4937 procedure Expand_N_Case_Expression
(N
: Node_Id
) is
4938 Loc
: constant Source_Ptr
:= Sloc
(N
);
4939 Typ
: constant Entity_Id
:= Etype
(N
);
4950 -- Check for MINIMIZED/ELIMINATED overflow mode
4952 if Minimized_Eliminated_Overflow_Check
(N
) then
4953 Apply_Arithmetic_Overflow_Check
(N
);
4957 -- If the case expression is a predicate specification, do not
4958 -- expand, because it will be converted to the proper predicate
4959 -- form when building the predicate function.
4961 if Ekind_In
(Current_Scope
, E_Function
, E_Procedure
)
4962 and then Is_Predicate_Function
(Current_Scope
)
4969 -- case X is when A => AX, when B => BX ...
4984 -- However, this expansion is wrong for limited types, and also
4985 -- wrong for unconstrained types (since the bounds may not be the
4986 -- same in all branches). Furthermore it involves an extra copy
4987 -- for large objects. So we take care of this by using the following
4988 -- modified expansion for non-elementary types:
4991 -- type Pnn is access all typ;
4995 -- T := AX'Unrestricted_Access;
4997 -- T := BX'Unrestricted_Access;
5003 Make_Case_Statement
(Loc
,
5004 Expression
=> Expression
(N
),
5005 Alternatives
=> New_List
);
5007 -- Preserve the original context for which the case statement is being
5008 -- generated. This is needed by the finalization machinery to prevent
5009 -- the premature finalization of controlled objects found within the
5012 Set_From_Conditional_Expression
(Cstmt
);
5014 Actions
:= New_List
;
5018 if Is_Elementary_Type
(Typ
) then
5022 Pnn
:= Make_Temporary
(Loc
, 'P');
5024 Make_Full_Type_Declaration
(Loc
,
5025 Defining_Identifier
=> Pnn
,
5027 Make_Access_To_Object_Definition
(Loc
,
5028 All_Present
=> True,
5029 Subtype_Indication
=> New_Occurrence_Of
(Typ
, Loc
))));
5033 Tnn
:= Make_Temporary
(Loc
, 'T');
5035 -- Create declaration for target of expression, and indicate that it
5036 -- does not require initialization.
5039 Make_Object_Declaration
(Loc
,
5040 Defining_Identifier
=> Tnn
,
5041 Object_Definition
=> New_Occurrence_Of
(Ttyp
, Loc
));
5042 Set_No_Initialization
(Decl
);
5043 Append_To
(Actions
, Decl
);
5045 -- Now process the alternatives
5047 Alt
:= First
(Alternatives
(N
));
5048 while Present
(Alt
) loop
5050 Aexp
: Node_Id
:= Expression
(Alt
);
5051 Aloc
: constant Source_Ptr
:= Sloc
(Aexp
);
5055 -- As described above, take Unrestricted_Access for case of non-
5056 -- scalar types, to avoid big copies, and special cases.
5058 if not Is_Elementary_Type
(Typ
) then
5060 Make_Attribute_Reference
(Aloc
,
5061 Prefix
=> Relocate_Node
(Aexp
),
5062 Attribute_Name
=> Name_Unrestricted_Access
);
5066 Make_Assignment_Statement
(Aloc
,
5067 Name
=> New_Occurrence_Of
(Tnn
, Loc
),
5068 Expression
=> Aexp
));
5070 -- Propagate declarations inserted in the node by Insert_Actions
5071 -- (for example, temporaries generated to remove side effects).
5072 -- These actions must remain attached to the alternative, given
5073 -- that they are generated by the corresponding expression.
5075 if Present
(Sinfo
.Actions
(Alt
)) then
5076 Prepend_List
(Sinfo
.Actions
(Alt
), Stats
);
5080 (Alternatives
(Cstmt
),
5081 Make_Case_Statement_Alternative
(Sloc
(Alt
),
5082 Discrete_Choices
=> Discrete_Choices
(Alt
),
5083 Statements
=> Stats
));
5089 Append_To
(Actions
, Cstmt
);
5091 -- Construct and return final expression with actions
5093 if Is_Elementary_Type
(Typ
) then
5094 Fexp
:= New_Occurrence_Of
(Tnn
, Loc
);
5097 Make_Explicit_Dereference
(Loc
,
5098 Prefix
=> New_Occurrence_Of
(Tnn
, Loc
));
5102 Make_Expression_With_Actions
(Loc
,
5104 Actions
=> Actions
));
5106 Analyze_And_Resolve
(N
, Typ
);
5107 end Expand_N_Case_Expression
;
5109 -----------------------------------
5110 -- Expand_N_Explicit_Dereference --
5111 -----------------------------------
5113 procedure Expand_N_Explicit_Dereference
(N
: Node_Id
) is
5115 -- Insert explicit dereference call for the checked storage pool case
5117 Insert_Dereference_Action
(Prefix
(N
));
5119 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5120 -- we set the atomic sync flag.
5122 if Is_Atomic
(Etype
(N
))
5123 and then not Atomic_Synchronization_Disabled
(Etype
(N
))
5125 Activate_Atomic_Synchronization
(N
);
5127 end Expand_N_Explicit_Dereference
;
5129 --------------------------------------
5130 -- Expand_N_Expression_With_Actions --
5131 --------------------------------------
5133 procedure Expand_N_Expression_With_Actions
(N
: Node_Id
) is
5135 function Process_Action
(Act
: Node_Id
) return Traverse_Result
;
5136 -- Inspect and process a single action of an expression_with_actions for
5137 -- transient controlled objects. If such objects are found, the routine
5138 -- generates code to clean them up when the context of the expression is
5139 -- evaluated or elaborated.
5141 --------------------
5142 -- Process_Action --
5143 --------------------
5145 function Process_Action
(Act
: Node_Id
) return Traverse_Result
is
5147 if Nkind
(Act
) = N_Object_Declaration
5148 and then Is_Finalizable_Transient
(Act
, N
)
5150 Process_Transient_Object
(Act
, N
);
5153 -- Avoid processing temporary function results multiple times when
5154 -- dealing with nested expression_with_actions.
5156 elsif Nkind
(Act
) = N_Expression_With_Actions
then
5159 -- Do not process temporary function results in loops. This is done
5160 -- by Expand_N_Loop_Statement and Build_Finalizer.
5162 elsif Nkind
(Act
) = N_Loop_Statement
then
5169 procedure Process_Single_Action
is new Traverse_Proc
(Process_Action
);
5175 -- Start of processing for Expand_N_Expression_With_Actions
5178 -- Process the actions as described above
5180 Act
:= First
(Actions
(N
));
5181 while Present
(Act
) loop
5182 Process_Single_Action
(Act
);
5186 -- Deal with case where there are no actions. In this case we simply
5187 -- rewrite the node with its expression since we don't need the actions
5188 -- and the specification of this node does not allow a null action list.
5190 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5191 -- the expanded tree and relying on being able to retrieve the original
5192 -- tree in cases like this. This raises a whole lot of issues of whether
5193 -- we have problems elsewhere, which will be addressed in the future???
5195 if Is_Empty_List
(Actions
(N
)) then
5196 Rewrite
(N
, Relocate_Node
(Expression
(N
)));
5198 end Expand_N_Expression_With_Actions
;
5200 ----------------------------
5201 -- Expand_N_If_Expression --
5202 ----------------------------
5204 -- Deal with limited types and condition actions
5206 procedure Expand_N_If_Expression
(N
: Node_Id
) is
5207 procedure Process_Actions
(Actions
: List_Id
);
5208 -- Inspect and process a single action list of an if expression for
5209 -- transient controlled objects. If such objects are found, the routine
5210 -- generates code to clean them up when the context of the expression is
5211 -- evaluated or elaborated.
5213 ---------------------
5214 -- Process_Actions --
5215 ---------------------
5217 procedure Process_Actions
(Actions
: List_Id
) is
5221 Act
:= First
(Actions
);
5222 while Present
(Act
) loop
5223 if Nkind
(Act
) = N_Object_Declaration
5224 and then Is_Finalizable_Transient
(Act
, N
)
5226 Process_Transient_Object
(Act
, N
);
5231 end Process_Actions
;
5235 Loc
: constant Source_Ptr
:= Sloc
(N
);
5236 Cond
: constant Node_Id
:= First
(Expressions
(N
));
5237 Thenx
: constant Node_Id
:= Next
(Cond
);
5238 Elsex
: constant Node_Id
:= Next
(Thenx
);
5239 Typ
: constant Entity_Id
:= Etype
(N
);
5247 Ptr_Typ
: Entity_Id
;
5249 -- Start of processing for Expand_N_If_Expression
5252 -- Check for MINIMIZED/ELIMINATED overflow mode
5254 if Minimized_Eliminated_Overflow_Check
(N
) then
5255 Apply_Arithmetic_Overflow_Check
(N
);
5259 -- Fold at compile time if condition known. We have already folded
5260 -- static if expressions, but it is possible to fold any case in which
5261 -- the condition is known at compile time, even though the result is
5264 -- Note that we don't do the fold of such cases in Sem_Elab because
5265 -- it can cause infinite loops with the expander adding a conditional
5266 -- expression, and Sem_Elab circuitry removing it repeatedly.
5268 if Compile_Time_Known_Value
(Cond
) then
5269 if Is_True
(Expr_Value
(Cond
)) then
5271 Actions
:= Then_Actions
(N
);
5274 Actions
:= Else_Actions
(N
);
5279 if Present
(Actions
) then
5281 Make_Expression_With_Actions
(Loc
,
5282 Expression
=> Relocate_Node
(Expr
),
5283 Actions
=> Actions
));
5284 Analyze_And_Resolve
(N
, Typ
);
5286 Rewrite
(N
, Relocate_Node
(Expr
));
5289 -- Note that the result is never static (legitimate cases of static
5290 -- if expressions were folded in Sem_Eval).
5292 Set_Is_Static_Expression
(N
, False);
5296 -- If the type is limited, and the back end does not handle limited
5297 -- types, then we expand as follows to avoid the possibility of
5298 -- improper copying.
5300 -- type Ptr is access all Typ;
5304 -- Cnn := then-expr'Unrestricted_Access;
5307 -- Cnn := else-expr'Unrestricted_Access;
5310 -- and replace the if expression by a reference to Cnn.all.
5312 -- This special case can be skipped if the back end handles limited
5313 -- types properly and ensures that no incorrect copies are made.
5315 if Is_By_Reference_Type
(Typ
)
5316 and then not Back_End_Handles_Limited_Types
5318 -- When the "then" or "else" expressions involve controlled function
5319 -- calls, generated temporaries are chained on the corresponding list
5320 -- of actions. These temporaries need to be finalized after the if
5321 -- expression is evaluated.
5323 Process_Actions
(Then_Actions
(N
));
5324 Process_Actions
(Else_Actions
(N
));
5327 -- type Ann is access all Typ;
5329 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
5332 Make_Full_Type_Declaration
(Loc
,
5333 Defining_Identifier
=> Ptr_Typ
,
5335 Make_Access_To_Object_Definition
(Loc
,
5336 All_Present
=> True,
5337 Subtype_Indication
=> New_Occurrence_Of
(Typ
, Loc
))));
5342 Cnn
:= Make_Temporary
(Loc
, 'C', N
);
5345 Make_Object_Declaration
(Loc
,
5346 Defining_Identifier
=> Cnn
,
5347 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
));
5351 -- Cnn := <Thenx>'Unrestricted_Access;
5353 -- Cnn := <Elsex>'Unrestricted_Access;
5357 Make_Implicit_If_Statement
(N
,
5358 Condition
=> Relocate_Node
(Cond
),
5359 Then_Statements
=> New_List
(
5360 Make_Assignment_Statement
(Sloc
(Thenx
),
5361 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Thenx
)),
5363 Make_Attribute_Reference
(Loc
,
5364 Prefix
=> Relocate_Node
(Thenx
),
5365 Attribute_Name
=> Name_Unrestricted_Access
))),
5367 Else_Statements
=> New_List
(
5368 Make_Assignment_Statement
(Sloc
(Elsex
),
5369 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Elsex
)),
5371 Make_Attribute_Reference
(Loc
,
5372 Prefix
=> Relocate_Node
(Elsex
),
5373 Attribute_Name
=> Name_Unrestricted_Access
))));
5375 -- Preserve the original context for which the if statement is being
5376 -- generated. This is needed by the finalization machinery to prevent
5377 -- the premature finalization of controlled objects found within the
5380 Set_From_Conditional_Expression
(New_If
);
5383 Make_Explicit_Dereference
(Loc
,
5384 Prefix
=> New_Occurrence_Of
(Cnn
, Loc
));
5386 -- If the result is an unconstrained array and the if expression is in a
5387 -- context other than the initializing expression of the declaration of
5388 -- an object, then we pull out the if expression as follows:
5390 -- Cnn : constant typ := if-expression
5392 -- and then replace the if expression with an occurrence of Cnn. This
5393 -- avoids the need in the back end to create on-the-fly variable length
5394 -- temporaries (which it cannot do!)
5396 -- Note that the test for being in an object declaration avoids doing an
5397 -- unnecessary expansion, and also avoids infinite recursion.
5399 elsif Is_Array_Type
(Typ
) and then not Is_Constrained
(Typ
)
5400 and then (Nkind
(Parent
(N
)) /= N_Object_Declaration
5401 or else Expression
(Parent
(N
)) /= N
)
5404 Cnn
: constant Node_Id
:= Make_Temporary
(Loc
, 'C', N
);
5407 Make_Object_Declaration
(Loc
,
5408 Defining_Identifier
=> Cnn
,
5409 Constant_Present
=> True,
5410 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
5411 Expression
=> Relocate_Node
(N
),
5412 Has_Init_Expression
=> True));
5414 Rewrite
(N
, New_Occurrence_Of
(Cnn
, Loc
));
5418 -- For other types, we only need to expand if there are other actions
5419 -- associated with either branch.
5421 elsif Present
(Then_Actions
(N
)) or else Present
(Else_Actions
(N
)) then
5423 -- We now wrap the actions into the appropriate expression
5425 if Present
(Then_Actions
(N
)) then
5427 Make_Expression_With_Actions
(Sloc
(Thenx
),
5428 Actions
=> Then_Actions
(N
),
5429 Expression
=> Relocate_Node
(Thenx
)));
5431 Set_Then_Actions
(N
, No_List
);
5432 Analyze_And_Resolve
(Thenx
, Typ
);
5435 if Present
(Else_Actions
(N
)) then
5437 Make_Expression_With_Actions
(Sloc
(Elsex
),
5438 Actions
=> Else_Actions
(N
),
5439 Expression
=> Relocate_Node
(Elsex
)));
5441 Set_Else_Actions
(N
, No_List
);
5442 Analyze_And_Resolve
(Elsex
, Typ
);
5447 -- If no actions then no expansion needed, gigi will handle it using the
5448 -- same approach as a C conditional expression.
5454 -- Fall through here for either the limited expansion, or the case of
5455 -- inserting actions for non-limited types. In both these cases, we must
5456 -- move the SLOC of the parent If statement to the newly created one and
5457 -- change it to the SLOC of the expression which, after expansion, will
5458 -- correspond to what is being evaluated.
5460 if Present
(Parent
(N
)) and then Nkind
(Parent
(N
)) = N_If_Statement
then
5461 Set_Sloc
(New_If
, Sloc
(Parent
(N
)));
5462 Set_Sloc
(Parent
(N
), Loc
);
5465 -- Make sure Then_Actions and Else_Actions are appropriately moved
5466 -- to the new if statement.
5468 if Present
(Then_Actions
(N
)) then
5470 (First
(Then_Statements
(New_If
)), Then_Actions
(N
));
5473 if Present
(Else_Actions
(N
)) then
5475 (First
(Else_Statements
(New_If
)), Else_Actions
(N
));
5478 Insert_Action
(N
, Decl
);
5479 Insert_Action
(N
, New_If
);
5481 Analyze_And_Resolve
(N
, Typ
);
5482 end Expand_N_If_Expression
;
5488 procedure Expand_N_In
(N
: Node_Id
) is
5489 Loc
: constant Source_Ptr
:= Sloc
(N
);
5490 Restyp
: constant Entity_Id
:= Etype
(N
);
5491 Lop
: constant Node_Id
:= Left_Opnd
(N
);
5492 Rop
: constant Node_Id
:= Right_Opnd
(N
);
5493 Static
: constant Boolean := Is_OK_Static_Expression
(N
);
5498 procedure Substitute_Valid_Check
;
5499 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5500 -- test for the left operand being in range of its subtype.
5502 ----------------------------
5503 -- Substitute_Valid_Check --
5504 ----------------------------
5506 procedure Substitute_Valid_Check
is
5509 Make_Attribute_Reference
(Loc
,
5510 Prefix
=> Relocate_Node
(Lop
),
5511 Attribute_Name
=> Name_Valid
));
5513 Analyze_And_Resolve
(N
, Restyp
);
5515 -- Give warning unless overflow checking is MINIMIZED or ELIMINATED,
5516 -- in which case, this usage makes sense, and in any case, we have
5517 -- actually eliminated the danger of optimization above.
5519 if Overflow_Check_Mode
not in Minimized_Or_Eliminated
then
5521 ("??explicit membership test may be optimized away", N
);
5522 Error_Msg_N
-- CODEFIX
5523 ("\??use ''Valid attribute instead", N
);
5527 end Substitute_Valid_Check
;
5529 -- Start of processing for Expand_N_In
5532 -- If set membership case, expand with separate procedure
5534 if Present
(Alternatives
(N
)) then
5535 Expand_Set_Membership
(N
);
5539 -- Not set membership, proceed with expansion
5541 Ltyp
:= Etype
(Left_Opnd
(N
));
5542 Rtyp
:= Etype
(Right_Opnd
(N
));
5544 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
5545 -- type, then expand with a separate procedure. Note the use of the
5546 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5548 if Overflow_Check_Mode
in Minimized_Or_Eliminated
5549 and then Is_Signed_Integer_Type
(Ltyp
)
5550 and then not No_Minimize_Eliminate
(N
)
5552 Expand_Membership_Minimize_Eliminate_Overflow
(N
);
5556 -- Check case of explicit test for an expression in range of its
5557 -- subtype. This is suspicious usage and we replace it with a 'Valid
5558 -- test and give a warning for scalar types.
5560 if Is_Scalar_Type
(Ltyp
)
5562 -- Only relevant for source comparisons
5564 and then Comes_From_Source
(N
)
5566 -- In floating-point this is a standard way to check for finite values
5567 -- and using 'Valid would typically be a pessimization.
5569 and then not Is_Floating_Point_Type
(Ltyp
)
5571 -- Don't give the message unless right operand is a type entity and
5572 -- the type of the left operand matches this type. Note that this
5573 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
5574 -- checks have changed the type of the left operand.
5576 and then Nkind
(Rop
) in N_Has_Entity
5577 and then Ltyp
= Entity
(Rop
)
5579 -- Skip in VM mode, where we have no sense of invalid values. The
5580 -- warning still seems relevant, but not important enough to worry.
5582 and then VM_Target
= No_VM
5584 -- Skip this for predicated types, where such expressions are a
5585 -- reasonable way of testing if something meets the predicate.
5587 and then not Present
(Predicate_Function
(Ltyp
))
5589 Substitute_Valid_Check
;
5593 -- Do validity check on operands
5595 if Validity_Checks_On
and Validity_Check_Operands
then
5596 Ensure_Valid
(Left_Opnd
(N
));
5597 Validity_Check_Range
(Right_Opnd
(N
));
5600 -- Case of explicit range
5602 if Nkind
(Rop
) = N_Range
then
5604 Lo
: constant Node_Id
:= Low_Bound
(Rop
);
5605 Hi
: constant Node_Id
:= High_Bound
(Rop
);
5607 Lo_Orig
: constant Node_Id
:= Original_Node
(Lo
);
5608 Hi_Orig
: constant Node_Id
:= Original_Node
(Hi
);
5610 Lcheck
: Compare_Result
;
5611 Ucheck
: Compare_Result
;
5613 Warn1
: constant Boolean :=
5614 Constant_Condition_Warnings
5615 and then Comes_From_Source
(N
)
5616 and then not In_Instance
;
5617 -- This must be true for any of the optimization warnings, we
5618 -- clearly want to give them only for source with the flag on. We
5619 -- also skip these warnings in an instance since it may be the
5620 -- case that different instantiations have different ranges.
5622 Warn2
: constant Boolean :=
5624 and then Nkind
(Original_Node
(Rop
)) = N_Range
5625 and then Is_Integer_Type
(Etype
(Lo
));
5626 -- For the case where only one bound warning is elided, we also
5627 -- insist on an explicit range and an integer type. The reason is
5628 -- that the use of enumeration ranges including an end point is
5629 -- common, as is the use of a subtype name, one of whose bounds is
5630 -- the same as the type of the expression.
5633 -- If test is explicit x'First .. x'Last, replace by valid check
5635 -- Could use some individual comments for this complex test ???
5637 if Is_Scalar_Type
(Ltyp
)
5639 -- And left operand is X'First where X matches left operand
5640 -- type (this eliminates cases of type mismatch, including
5641 -- the cases where ELIMINATED/MINIMIZED mode has changed the
5642 -- type of the left operand.
5644 and then Nkind
(Lo_Orig
) = N_Attribute_Reference
5645 and then Attribute_Name
(Lo_Orig
) = Name_First
5646 and then Nkind
(Prefix
(Lo_Orig
)) in N_Has_Entity
5647 and then Entity
(Prefix
(Lo_Orig
)) = Ltyp
5649 -- Same tests for right operand
5651 and then Nkind
(Hi_Orig
) = N_Attribute_Reference
5652 and then Attribute_Name
(Hi_Orig
) = Name_Last
5653 and then Nkind
(Prefix
(Hi_Orig
)) in N_Has_Entity
5654 and then Entity
(Prefix
(Hi_Orig
)) = Ltyp
5656 -- Relevant only for source cases
5658 and then Comes_From_Source
(N
)
5660 -- Omit for VM cases, where we don't have invalid values
5662 and then VM_Target
= No_VM
5664 Substitute_Valid_Check
;
5668 -- If bounds of type are known at compile time, and the end points
5669 -- are known at compile time and identical, this is another case
5670 -- for substituting a valid test. We only do this for discrete
5671 -- types, since it won't arise in practice for float types.
5673 if Comes_From_Source
(N
)
5674 and then Is_Discrete_Type
(Ltyp
)
5675 and then Compile_Time_Known_Value
(Type_High_Bound
(Ltyp
))
5676 and then Compile_Time_Known_Value
(Type_Low_Bound
(Ltyp
))
5677 and then Compile_Time_Known_Value
(Lo
)
5678 and then Compile_Time_Known_Value
(Hi
)
5679 and then Expr_Value
(Type_High_Bound
(Ltyp
)) = Expr_Value
(Hi
)
5680 and then Expr_Value
(Type_Low_Bound
(Ltyp
)) = Expr_Value
(Lo
)
5682 -- Kill warnings in instances, since they may be cases where we
5683 -- have a test in the generic that makes sense with some types
5684 -- and not with other types.
5686 and then not In_Instance
5688 Substitute_Valid_Check
;
5692 -- If we have an explicit range, do a bit of optimization based on
5693 -- range analysis (we may be able to kill one or both checks).
5695 Lcheck
:= Compile_Time_Compare
(Lop
, Lo
, Assume_Valid
=> False);
5696 Ucheck
:= Compile_Time_Compare
(Lop
, Hi
, Assume_Valid
=> False);
5698 -- If either check is known to fail, replace result by False since
5699 -- the other check does not matter. Preserve the static flag for
5700 -- legality checks, because we are constant-folding beyond RM 4.9.
5702 if Lcheck
= LT
or else Ucheck
= GT
then
5704 Error_Msg_N
("?c?range test optimized away", N
);
5705 Error_Msg_N
("\?c?value is known to be out of range", N
);
5708 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
5709 Analyze_And_Resolve
(N
, Restyp
);
5710 Set_Is_Static_Expression
(N
, Static
);
5713 -- If both checks are known to succeed, replace result by True,
5714 -- since we know we are in range.
5716 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
5718 Error_Msg_N
("?c?range test optimized away", N
);
5719 Error_Msg_N
("\?c?value is known to be in range", N
);
5722 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
5723 Analyze_And_Resolve
(N
, Restyp
);
5724 Set_Is_Static_Expression
(N
, Static
);
5727 -- If lower bound check succeeds and upper bound check is not
5728 -- known to succeed or fail, then replace the range check with
5729 -- a comparison against the upper bound.
5731 elsif Lcheck
in Compare_GE
then
5732 if Warn2
and then not In_Instance
then
5733 Error_Msg_N
("??lower bound test optimized away", Lo
);
5734 Error_Msg_N
("\??value is known to be in range", Lo
);
5740 Right_Opnd
=> High_Bound
(Rop
)));
5741 Analyze_And_Resolve
(N
, Restyp
);
5744 -- If upper bound check succeeds and lower bound check is not
5745 -- known to succeed or fail, then replace the range check with
5746 -- a comparison against the lower bound.
5748 elsif Ucheck
in Compare_LE
then
5749 if Warn2
and then not In_Instance
then
5750 Error_Msg_N
("??upper bound test optimized away", Hi
);
5751 Error_Msg_N
("\??value is known to be in range", Hi
);
5757 Right_Opnd
=> Low_Bound
(Rop
)));
5758 Analyze_And_Resolve
(N
, Restyp
);
5762 -- We couldn't optimize away the range check, but there is one
5763 -- more issue. If we are checking constant conditionals, then we
5764 -- see if we can determine the outcome assuming everything is
5765 -- valid, and if so give an appropriate warning.
5767 if Warn1
and then not Assume_No_Invalid_Values
then
5768 Lcheck
:= Compile_Time_Compare
(Lop
, Lo
, Assume_Valid
=> True);
5769 Ucheck
:= Compile_Time_Compare
(Lop
, Hi
, Assume_Valid
=> True);
5771 -- Result is out of range for valid value
5773 if Lcheck
= LT
or else Ucheck
= GT
then
5775 ("?c?value can only be in range if it is invalid", N
);
5777 -- Result is in range for valid value
5779 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
5781 ("?c?value can only be out of range if it is invalid", N
);
5783 -- Lower bound check succeeds if value is valid
5785 elsif Warn2
and then Lcheck
in Compare_GE
then
5787 ("?c?lower bound check only fails if it is invalid", Lo
);
5789 -- Upper bound check succeeds if value is valid
5791 elsif Warn2
and then Ucheck
in Compare_LE
then
5793 ("?c?upper bound check only fails for invalid values", Hi
);
5798 -- For all other cases of an explicit range, nothing to be done
5802 -- Here right operand is a subtype mark
5806 Typ
: Entity_Id
:= Etype
(Rop
);
5807 Is_Acc
: constant Boolean := Is_Access_Type
(Typ
);
5808 Cond
: Node_Id
:= Empty
;
5810 Obj
: Node_Id
:= Lop
;
5811 SCIL_Node
: Node_Id
;
5814 Remove_Side_Effects
(Obj
);
5816 -- For tagged type, do tagged membership operation
5818 if Is_Tagged_Type
(Typ
) then
5820 -- No expansion will be performed when VM_Target, as the VM
5821 -- back-ends will handle the membership tests directly (tags
5822 -- are not explicitly represented in Java objects, so the
5823 -- normal tagged membership expansion is not what we want).
5825 if Tagged_Type_Expansion
then
5826 Tagged_Membership
(N
, SCIL_Node
, New_N
);
5828 Analyze_And_Resolve
(N
, Restyp
);
5830 -- Update decoration of relocated node referenced by the
5833 if Generate_SCIL
and then Present
(SCIL_Node
) then
5834 Set_SCIL_Node
(N
, SCIL_Node
);
5840 -- If type is scalar type, rewrite as x in t'First .. t'Last.
5841 -- This reason we do this is that the bounds may have the wrong
5842 -- type if they come from the original type definition. Also this
5843 -- way we get all the processing above for an explicit range.
5845 -- Don't do this for predicated types, since in this case we
5846 -- want to check the predicate.
5848 elsif Is_Scalar_Type
(Typ
) then
5849 if No
(Predicate_Function
(Typ
)) then
5853 Make_Attribute_Reference
(Loc
,
5854 Attribute_Name
=> Name_First
,
5855 Prefix
=> New_Occurrence_Of
(Typ
, Loc
)),
5858 Make_Attribute_Reference
(Loc
,
5859 Attribute_Name
=> Name_Last
,
5860 Prefix
=> New_Occurrence_Of
(Typ
, Loc
))));
5861 Analyze_And_Resolve
(N
, Restyp
);
5866 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5867 -- a membership test if the subtype mark denotes a constrained
5868 -- Unchecked_Union subtype and the expression lacks inferable
5871 elsif Is_Unchecked_Union
(Base_Type
(Typ
))
5872 and then Is_Constrained
(Typ
)
5873 and then not Has_Inferable_Discriminants
(Lop
)
5876 Make_Raise_Program_Error
(Loc
,
5877 Reason
=> PE_Unchecked_Union_Restriction
));
5879 -- Prevent Gigi from generating incorrect code by rewriting the
5880 -- test as False. What is this undocumented thing about ???
5882 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
5886 -- Here we have a non-scalar type
5889 Typ
:= Designated_Type
(Typ
);
5892 if not Is_Constrained
(Typ
) then
5893 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
5894 Analyze_And_Resolve
(N
, Restyp
);
5896 -- For the constrained array case, we have to check the subscripts
5897 -- for an exact match if the lengths are non-zero (the lengths
5898 -- must match in any case).
5900 elsif Is_Array_Type
(Typ
) then
5901 Check_Subscripts
: declare
5902 function Build_Attribute_Reference
5905 Dim
: Nat
) return Node_Id
;
5906 -- Build attribute reference E'Nam (Dim)
5908 -------------------------------
5909 -- Build_Attribute_Reference --
5910 -------------------------------
5912 function Build_Attribute_Reference
5915 Dim
: Nat
) return Node_Id
5919 Make_Attribute_Reference
(Loc
,
5921 Attribute_Name
=> Nam
,
5922 Expressions
=> New_List
(
5923 Make_Integer_Literal
(Loc
, Dim
)));
5924 end Build_Attribute_Reference
;
5926 -- Start of processing for Check_Subscripts
5929 for J
in 1 .. Number_Dimensions
(Typ
) loop
5930 Evolve_And_Then
(Cond
,
5933 Build_Attribute_Reference
5934 (Duplicate_Subexpr_No_Checks
(Obj
),
5937 Build_Attribute_Reference
5938 (New_Occurrence_Of
(Typ
, Loc
), Name_First
, J
)));
5940 Evolve_And_Then
(Cond
,
5943 Build_Attribute_Reference
5944 (Duplicate_Subexpr_No_Checks
(Obj
),
5947 Build_Attribute_Reference
5948 (New_Occurrence_Of
(Typ
, Loc
), Name_Last
, J
)));
5957 Right_Opnd
=> Make_Null
(Loc
)),
5958 Right_Opnd
=> Cond
);
5962 Analyze_And_Resolve
(N
, Restyp
);
5963 end Check_Subscripts
;
5965 -- These are the cases where constraint checks may be required,
5966 -- e.g. records with possible discriminants
5969 -- Expand the test into a series of discriminant comparisons.
5970 -- The expression that is built is the negation of the one that
5971 -- is used for checking discriminant constraints.
5973 Obj
:= Relocate_Node
(Left_Opnd
(N
));
5975 if Has_Discriminants
(Typ
) then
5976 Cond
:= Make_Op_Not
(Loc
,
5977 Right_Opnd
=> Build_Discriminant_Checks
(Obj
, Typ
));
5980 Cond
:= Make_Or_Else
(Loc
,
5984 Right_Opnd
=> Make_Null
(Loc
)),
5985 Right_Opnd
=> Cond
);
5989 Cond
:= New_Occurrence_Of
(Standard_True
, Loc
);
5993 Analyze_And_Resolve
(N
, Restyp
);
5996 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
5997 -- expression of an anonymous access type. This can involve an
5998 -- accessibility test and a tagged type membership test in the
5999 -- case of tagged designated types.
6001 if Ada_Version
>= Ada_2012
6003 and then Ekind
(Ltyp
) = E_Anonymous_Access_Type
6006 Expr_Entity
: Entity_Id
:= Empty
;
6008 Param_Level
: Node_Id
;
6009 Type_Level
: Node_Id
;
6012 if Is_Entity_Name
(Lop
) then
6013 Expr_Entity
:= Param_Entity
(Lop
);
6015 if not Present
(Expr_Entity
) then
6016 Expr_Entity
:= Entity
(Lop
);
6020 -- If a conversion of the anonymous access value to the
6021 -- tested type would be illegal, then the result is False.
6023 if not Valid_Conversion
6024 (Lop
, Rtyp
, Lop
, Report_Errs
=> False)
6026 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
6027 Analyze_And_Resolve
(N
, Restyp
);
6029 -- Apply an accessibility check if the access object has an
6030 -- associated access level and when the level of the type is
6031 -- less deep than the level of the access parameter. This
6032 -- only occur for access parameters and stand-alone objects
6033 -- of an anonymous access type.
6036 if Present
(Expr_Entity
)
6039 (Effective_Extra_Accessibility
(Expr_Entity
))
6040 and then UI_Gt
(Object_Access_Level
(Lop
),
6041 Type_Access_Level
(Rtyp
))
6045 (Effective_Extra_Accessibility
(Expr_Entity
), Loc
);
6048 Make_Integer_Literal
(Loc
, Type_Access_Level
(Rtyp
));
6050 -- Return True only if the accessibility level of the
6051 -- expression entity is not deeper than the level of
6052 -- the tested access type.
6056 Left_Opnd
=> Relocate_Node
(N
),
6057 Right_Opnd
=> Make_Op_Le
(Loc
,
6058 Left_Opnd
=> Param_Level
,
6059 Right_Opnd
=> Type_Level
)));
6061 Analyze_And_Resolve
(N
);
6064 -- If the designated type is tagged, do tagged membership
6067 -- *** NOTE: we have to check not null before doing the
6068 -- tagged membership test (but maybe that can be done
6069 -- inside Tagged_Membership?).
6071 if Is_Tagged_Type
(Typ
) then
6074 Left_Opnd
=> Relocate_Node
(N
),
6078 Right_Opnd
=> Make_Null
(Loc
))));
6080 -- No expansion will be performed when VM_Target, as
6081 -- the VM back-ends will handle the membership tests
6082 -- directly (tags are not explicitly represented in
6083 -- Java objects, so the normal tagged membership
6084 -- expansion is not what we want).
6086 if Tagged_Type_Expansion
then
6088 -- Note that we have to pass Original_Node, because
6089 -- the membership test might already have been
6090 -- rewritten by earlier parts of membership test.
6093 (Original_Node
(N
), SCIL_Node
, New_N
);
6095 -- Update decoration of relocated node referenced
6096 -- by the SCIL node.
6098 if Generate_SCIL
and then Present
(SCIL_Node
) then
6099 Set_SCIL_Node
(New_N
, SCIL_Node
);
6104 Left_Opnd
=> Relocate_Node
(N
),
6105 Right_Opnd
=> New_N
));
6107 Analyze_And_Resolve
(N
, Restyp
);
6116 -- At this point, we have done the processing required for the basic
6117 -- membership test, but not yet dealt with the predicate.
6121 -- If a predicate is present, then we do the predicate test, but we
6122 -- most certainly want to omit this if we are within the predicate
6123 -- function itself, since otherwise we have an infinite recursion.
6124 -- The check should also not be emitted when testing against a range
6125 -- (the check is only done when the right operand is a subtype; see
6126 -- RM12-4.5.2 (28.1/3-30/3)).
6129 PFunc
: constant Entity_Id
:= Predicate_Function
(Rtyp
);
6133 and then Current_Scope
/= PFunc
6134 and then Nkind
(Rop
) /= N_Range
6138 Left_Opnd
=> Relocate_Node
(N
),
6139 Right_Opnd
=> Make_Predicate_Call
(Rtyp
, Lop
, Mem
=> True)));
6141 -- Analyze new expression, mark left operand as analyzed to
6142 -- avoid infinite recursion adding predicate calls. Similarly,
6143 -- suppress further range checks on the call.
6145 Set_Analyzed
(Left_Opnd
(N
));
6146 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
6148 -- All done, skip attempt at compile time determination of result
6155 --------------------------------
6156 -- Expand_N_Indexed_Component --
6157 --------------------------------
6159 procedure Expand_N_Indexed_Component
(N
: Node_Id
) is
6160 Loc
: constant Source_Ptr
:= Sloc
(N
);
6161 Typ
: constant Entity_Id
:= Etype
(N
);
6162 P
: constant Node_Id
:= Prefix
(N
);
6163 T
: constant Entity_Id
:= Etype
(P
);
6167 -- A special optimization, if we have an indexed component that is
6168 -- selecting from a slice, then we can eliminate the slice, since, for
6169 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6170 -- the range check required by the slice. The range check for the slice
6171 -- itself has already been generated. The range check for the
6172 -- subscripting operation is ensured by converting the subject to
6173 -- the subtype of the slice.
6175 -- This optimization not only generates better code, avoiding slice
6176 -- messing especially in the packed case, but more importantly bypasses
6177 -- some problems in handling this peculiar case, for example, the issue
6178 -- of dealing specially with object renamings.
6180 if Nkind
(P
) = N_Slice
6182 -- This optimization is disabled for CodePeer because it can transform
6183 -- an index-check constraint_error into a range-check constraint_error
6184 -- and CodePeer cares about that distinction.
6186 and then not CodePeer_Mode
6189 Make_Indexed_Component
(Loc
,
6190 Prefix
=> Prefix
(P
),
6191 Expressions
=> New_List
(
6193 (Etype
(First_Index
(Etype
(P
))),
6194 First
(Expressions
(N
))))));
6195 Analyze_And_Resolve
(N
, Typ
);
6199 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6200 -- function, then additional actuals must be passed.
6202 if Ada_Version
>= Ada_2005
6203 and then Is_Build_In_Place_Function_Call
(P
)
6205 Make_Build_In_Place_Call_In_Anonymous_Context
(P
);
6208 -- If the prefix is an access type, then we unconditionally rewrite if
6209 -- as an explicit dereference. This simplifies processing for several
6210 -- cases, including packed array cases and certain cases in which checks
6211 -- must be generated. We used to try to do this only when it was
6212 -- necessary, but it cleans up the code to do it all the time.
6214 if Is_Access_Type
(T
) then
6215 Insert_Explicit_Dereference
(P
);
6216 Analyze_And_Resolve
(P
, Designated_Type
(T
));
6217 Atp
:= Designated_Type
(T
);
6222 -- Generate index and validity checks
6224 Generate_Index_Checks
(N
);
6226 if Validity_Checks_On
and then Validity_Check_Subscripts
then
6227 Apply_Subscript_Validity_Checks
(N
);
6230 -- If selecting from an array with atomic components, and atomic sync
6231 -- is not suppressed for this array type, set atomic sync flag.
6233 if (Has_Atomic_Components
(Atp
)
6234 and then not Atomic_Synchronization_Disabled
(Atp
))
6235 or else (Is_Atomic
(Typ
)
6236 and then not Atomic_Synchronization_Disabled
(Typ
))
6238 Activate_Atomic_Synchronization
(N
);
6241 -- All done for the non-packed case
6243 if not Is_Packed
(Etype
(Prefix
(N
))) then
6247 -- For packed arrays that are not bit-packed (i.e. the case of an array
6248 -- with one or more index types with a non-contiguous enumeration type),
6249 -- we can always use the normal packed element get circuit.
6251 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
6252 Expand_Packed_Element_Reference
(N
);
6256 -- For a reference to a component of a bit packed array, we convert it
6257 -- to a reference to the corresponding Packed_Array_Impl_Type. We only
6258 -- want to do this for simple references, and not for:
6260 -- Left side of assignment, or prefix of left side of assignment, or
6261 -- prefix of the prefix, to handle packed arrays of packed arrays,
6262 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6264 -- Renaming objects in renaming associations
6265 -- This case is handled when a use of the renamed variable occurs
6267 -- Actual parameters for a procedure call
6268 -- This case is handled in Exp_Ch6.Expand_Actuals
6270 -- The second expression in a 'Read attribute reference
6272 -- The prefix of an address or bit or size attribute reference
6274 -- The following circuit detects these exceptions
6277 Child
: Node_Id
:= N
;
6278 Parnt
: Node_Id
:= Parent
(N
);
6282 if Nkind
(Parnt
) = N_Unchecked_Expression
then
6285 elsif Nkind_In
(Parnt
, N_Object_Renaming_Declaration
,
6286 N_Procedure_Call_Statement
)
6287 or else (Nkind
(Parnt
) = N_Parameter_Association
6289 Nkind
(Parent
(Parnt
)) = N_Procedure_Call_Statement
)
6293 elsif Nkind
(Parnt
) = N_Attribute_Reference
6294 and then Nam_In
(Attribute_Name
(Parnt
), Name_Address
,
6297 and then Prefix
(Parnt
) = Child
6301 elsif Nkind
(Parnt
) = N_Assignment_Statement
6302 and then Name
(Parnt
) = Child
6306 -- If the expression is an index of an indexed component, it must
6307 -- be expanded regardless of context.
6309 elsif Nkind
(Parnt
) = N_Indexed_Component
6310 and then Child
/= Prefix
(Parnt
)
6312 Expand_Packed_Element_Reference
(N
);
6315 elsif Nkind
(Parent
(Parnt
)) = N_Assignment_Statement
6316 and then Name
(Parent
(Parnt
)) = Parnt
6320 elsif Nkind
(Parnt
) = N_Attribute_Reference
6321 and then Attribute_Name
(Parnt
) = Name_Read
6322 and then Next
(First
(Expressions
(Parnt
))) = Child
6326 elsif Nkind_In
(Parnt
, N_Indexed_Component
, N_Selected_Component
)
6327 and then Prefix
(Parnt
) = Child
6332 Expand_Packed_Element_Reference
(N
);
6336 -- Keep looking up tree for unchecked expression, or if we are the
6337 -- prefix of a possible assignment left side.
6340 Parnt
:= Parent
(Child
);
6343 end Expand_N_Indexed_Component
;
6345 ---------------------
6346 -- Expand_N_Not_In --
6347 ---------------------
6349 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6350 -- can be done. This avoids needing to duplicate this expansion code.
6352 procedure Expand_N_Not_In
(N
: Node_Id
) is
6353 Loc
: constant Source_Ptr
:= Sloc
(N
);
6354 Typ
: constant Entity_Id
:= Etype
(N
);
6355 Cfs
: constant Boolean := Comes_From_Source
(N
);
6362 Left_Opnd
=> Left_Opnd
(N
),
6363 Right_Opnd
=> Right_Opnd
(N
))));
6365 -- If this is a set membership, preserve list of alternatives
6367 Set_Alternatives
(Right_Opnd
(N
), Alternatives
(Original_Node
(N
)));
6369 -- We want this to appear as coming from source if original does (see
6370 -- transformations in Expand_N_In).
6372 Set_Comes_From_Source
(N
, Cfs
);
6373 Set_Comes_From_Source
(Right_Opnd
(N
), Cfs
);
6375 -- Now analyze transformed node
6377 Analyze_And_Resolve
(N
, Typ
);
6378 end Expand_N_Not_In
;
6384 -- The only replacement required is for the case of a null of a type that
6385 -- is an access to protected subprogram, or a subtype thereof. We represent
6386 -- such access values as a record, and so we must replace the occurrence of
6387 -- null by the equivalent record (with a null address and a null pointer in
6388 -- it), so that the backend creates the proper value.
6390 procedure Expand_N_Null
(N
: Node_Id
) is
6391 Loc
: constant Source_Ptr
:= Sloc
(N
);
6392 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
6396 if Is_Access_Protected_Subprogram_Type
(Typ
) then
6398 Make_Aggregate
(Loc
,
6399 Expressions
=> New_List
(
6400 New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
),
6404 Analyze_And_Resolve
(N
, Equivalent_Type
(Typ
));
6406 -- For subsequent semantic analysis, the node must retain its type.
6407 -- Gigi in any case replaces this type by the corresponding record
6408 -- type before processing the node.
6414 when RE_Not_Available
=>
6418 ---------------------
6419 -- Expand_N_Op_Abs --
6420 ---------------------
6422 procedure Expand_N_Op_Abs
(N
: Node_Id
) is
6423 Loc
: constant Source_Ptr
:= Sloc
(N
);
6424 Expr
: constant Node_Id
:= Right_Opnd
(N
);
6427 Unary_Op_Validity_Checks
(N
);
6429 -- Check for MINIMIZED/ELIMINATED overflow mode
6431 if Minimized_Eliminated_Overflow_Check
(N
) then
6432 Apply_Arithmetic_Overflow_Check
(N
);
6436 -- Deal with software overflow checking
6438 if not Backend_Overflow_Checks_On_Target
6439 and then Is_Signed_Integer_Type
(Etype
(N
))
6440 and then Do_Overflow_Check
(N
)
6442 -- The only case to worry about is when the argument is equal to the
6443 -- largest negative number, so what we do is to insert the check:
6445 -- [constraint_error when Expr = typ'Base'First]
6447 -- with the usual Duplicate_Subexpr use coding for expr
6450 Make_Raise_Constraint_Error
(Loc
,
6453 Left_Opnd
=> Duplicate_Subexpr
(Expr
),
6455 Make_Attribute_Reference
(Loc
,
6457 New_Occurrence_Of
(Base_Type
(Etype
(Expr
)), Loc
),
6458 Attribute_Name
=> Name_First
)),
6459 Reason
=> CE_Overflow_Check_Failed
));
6461 end Expand_N_Op_Abs
;
6463 ---------------------
6464 -- Expand_N_Op_Add --
6465 ---------------------
6467 procedure Expand_N_Op_Add
(N
: Node_Id
) is
6468 Typ
: constant Entity_Id
:= Etype
(N
);
6471 Binary_Op_Validity_Checks
(N
);
6473 -- Check for MINIMIZED/ELIMINATED overflow mode
6475 if Minimized_Eliminated_Overflow_Check
(N
) then
6476 Apply_Arithmetic_Overflow_Check
(N
);
6480 -- N + 0 = 0 + N = N for integer types
6482 if Is_Integer_Type
(Typ
) then
6483 if Compile_Time_Known_Value
(Right_Opnd
(N
))
6484 and then Expr_Value
(Right_Opnd
(N
)) = Uint_0
6486 Rewrite
(N
, Left_Opnd
(N
));
6489 elsif Compile_Time_Known_Value
(Left_Opnd
(N
))
6490 and then Expr_Value
(Left_Opnd
(N
)) = Uint_0
6492 Rewrite
(N
, Right_Opnd
(N
));
6497 -- Arithmetic overflow checks for signed integer/fixed point types
6499 if Is_Signed_Integer_Type
(Typ
) or else Is_Fixed_Point_Type
(Typ
) then
6500 Apply_Arithmetic_Overflow_Check
(N
);
6504 -- Overflow checks for floating-point if -gnateF mode active
6506 Check_Float_Op_Overflow
(N
);
6507 end Expand_N_Op_Add
;
6509 ---------------------
6510 -- Expand_N_Op_And --
6511 ---------------------
6513 procedure Expand_N_Op_And
(N
: Node_Id
) is
6514 Typ
: constant Entity_Id
:= Etype
(N
);
6517 Binary_Op_Validity_Checks
(N
);
6519 if Is_Array_Type
(Etype
(N
)) then
6520 Expand_Boolean_Operator
(N
);
6522 elsif Is_Boolean_Type
(Etype
(N
)) then
6523 Adjust_Condition
(Left_Opnd
(N
));
6524 Adjust_Condition
(Right_Opnd
(N
));
6525 Set_Etype
(N
, Standard_Boolean
);
6526 Adjust_Result_Type
(N
, Typ
);
6528 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
6529 Expand_Intrinsic_Call
(N
, Entity
(N
));
6532 end Expand_N_Op_And
;
6534 ------------------------
6535 -- Expand_N_Op_Concat --
6536 ------------------------
6538 procedure Expand_N_Op_Concat
(N
: Node_Id
) is
6540 -- List of operands to be concatenated
6543 -- Node which is to be replaced by the result of concatenating the nodes
6544 -- in the list Opnds.
6547 -- Ensure validity of both operands
6549 Binary_Op_Validity_Checks
(N
);
6551 -- If we are the left operand of a concatenation higher up the tree,
6552 -- then do nothing for now, since we want to deal with a series of
6553 -- concatenations as a unit.
6555 if Nkind
(Parent
(N
)) = N_Op_Concat
6556 and then N
= Left_Opnd
(Parent
(N
))
6561 -- We get here with a concatenation whose left operand may be a
6562 -- concatenation itself with a consistent type. We need to process
6563 -- these concatenation operands from left to right, which means
6564 -- from the deepest node in the tree to the highest node.
6567 while Nkind
(Left_Opnd
(Cnode
)) = N_Op_Concat
loop
6568 Cnode
:= Left_Opnd
(Cnode
);
6571 -- Now Cnode is the deepest concatenation, and its parents are the
6572 -- concatenation nodes above, so now we process bottom up, doing the
6575 -- The outer loop runs more than once if more than one concatenation
6576 -- type is involved.
6579 Opnds
:= New_List
(Left_Opnd
(Cnode
), Right_Opnd
(Cnode
));
6580 Set_Parent
(Opnds
, N
);
6582 -- The inner loop gathers concatenation operands
6584 Inner
: while Cnode
/= N
6585 and then Base_Type
(Etype
(Cnode
)) =
6586 Base_Type
(Etype
(Parent
(Cnode
)))
6588 Cnode
:= Parent
(Cnode
);
6589 Append
(Right_Opnd
(Cnode
), Opnds
);
6592 -- Note: The following code is a temporary workaround for N731-034
6593 -- and N829-028 and will be kept until the general issue of internal
6594 -- symbol serialization is addressed. The workaround is kept under a
6595 -- debug switch to avoid permiating into the general case.
6597 -- Wrap the node to concatenate into an expression actions node to
6598 -- keep it nicely packaged. This is useful in the case of an assert
6599 -- pragma with a concatenation where we want to be able to delete
6600 -- the concatenation and all its expansion stuff.
6602 if Debug_Flag_Dot_H
then
6604 Cnod
: constant Node_Id
:= Relocate_Node
(Cnode
);
6605 Typ
: constant Entity_Id
:= Base_Type
(Etype
(Cnode
));
6608 -- Note: use Rewrite rather than Replace here, so that for
6609 -- example Why_Not_Static can find the original concatenation
6613 Make_Expression_With_Actions
(Sloc
(Cnode
),
6614 Actions
=> New_List
(Make_Null_Statement
(Sloc
(Cnode
))),
6615 Expression
=> Cnod
));
6617 Expand_Concatenate
(Cnod
, Opnds
);
6618 Analyze_And_Resolve
(Cnode
, Typ
);
6624 Expand_Concatenate
(Cnode
, Opnds
);
6627 exit Outer
when Cnode
= N
;
6628 Cnode
:= Parent
(Cnode
);
6630 end Expand_N_Op_Concat
;
6632 ------------------------
6633 -- Expand_N_Op_Divide --
6634 ------------------------
6636 procedure Expand_N_Op_Divide
(N
: Node_Id
) is
6637 Loc
: constant Source_Ptr
:= Sloc
(N
);
6638 Lopnd
: constant Node_Id
:= Left_Opnd
(N
);
6639 Ropnd
: constant Node_Id
:= Right_Opnd
(N
);
6640 Ltyp
: constant Entity_Id
:= Etype
(Lopnd
);
6641 Rtyp
: constant Entity_Id
:= Etype
(Ropnd
);
6642 Typ
: Entity_Id
:= Etype
(N
);
6643 Rknow
: constant Boolean := Is_Integer_Type
(Typ
)
6645 Compile_Time_Known_Value
(Ropnd
);
6649 Binary_Op_Validity_Checks
(N
);
6651 -- Check for MINIMIZED/ELIMINATED overflow mode
6653 if Minimized_Eliminated_Overflow_Check
(N
) then
6654 Apply_Arithmetic_Overflow_Check
(N
);
6658 -- Otherwise proceed with expansion of division
6661 Rval
:= Expr_Value
(Ropnd
);
6664 -- N / 1 = N for integer types
6666 if Rknow
and then Rval
= Uint_1
then
6671 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
6672 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6673 -- operand is an unsigned integer, as required for this to work.
6675 if Nkind
(Ropnd
) = N_Op_Expon
6676 and then Is_Power_Of_2_For_Shift
(Ropnd
)
6678 -- We cannot do this transformation in configurable run time mode if we
6679 -- have 64-bit integers and long shifts are not available.
6681 and then (Esize
(Ltyp
) <= 32 or else Support_Long_Shifts_On_Target
)
6684 Make_Op_Shift_Right
(Loc
,
6687 Convert_To
(Standard_Natural
, Right_Opnd
(Ropnd
))));
6688 Analyze_And_Resolve
(N
, Typ
);
6692 -- Do required fixup of universal fixed operation
6694 if Typ
= Universal_Fixed
then
6695 Fixup_Universal_Fixed_Operation
(N
);
6699 -- Divisions with fixed-point results
6701 if Is_Fixed_Point_Type
(Typ
) then
6703 -- No special processing if Treat_Fixed_As_Integer is set, since
6704 -- from a semantic point of view such operations are simply integer
6705 -- operations and will be treated that way.
6707 if not Treat_Fixed_As_Integer
(N
) then
6708 if Is_Integer_Type
(Rtyp
) then
6709 Expand_Divide_Fixed_By_Integer_Giving_Fixed
(N
);
6711 Expand_Divide_Fixed_By_Fixed_Giving_Fixed
(N
);
6715 -- Other cases of division of fixed-point operands. Again we exclude the
6716 -- case where Treat_Fixed_As_Integer is set.
6718 elsif (Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
))
6719 and then not Treat_Fixed_As_Integer
(N
)
6721 if Is_Integer_Type
(Typ
) then
6722 Expand_Divide_Fixed_By_Fixed_Giving_Integer
(N
);
6724 pragma Assert
(Is_Floating_Point_Type
(Typ
));
6725 Expand_Divide_Fixed_By_Fixed_Giving_Float
(N
);
6728 -- Mixed-mode operations can appear in a non-static universal context,
6729 -- in which case the integer argument must be converted explicitly.
6731 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Rtyp
) then
6733 Convert_To
(Universal_Real
, Relocate_Node
(Ropnd
)));
6735 Analyze_And_Resolve
(Ropnd
, Universal_Real
);
6737 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Ltyp
) then
6739 Convert_To
(Universal_Real
, Relocate_Node
(Lopnd
)));
6741 Analyze_And_Resolve
(Lopnd
, Universal_Real
);
6743 -- Non-fixed point cases, do integer zero divide and overflow checks
6745 elsif Is_Integer_Type
(Typ
) then
6746 Apply_Divide_Checks
(N
);
6749 -- Overflow checks for floating-point if -gnateF mode active
6751 Check_Float_Op_Overflow
(N
);
6752 end Expand_N_Op_Divide
;
6754 --------------------
6755 -- Expand_N_Op_Eq --
6756 --------------------
6758 procedure Expand_N_Op_Eq
(N
: Node_Id
) is
6759 Loc
: constant Source_Ptr
:= Sloc
(N
);
6760 Typ
: constant Entity_Id
:= Etype
(N
);
6761 Lhs
: constant Node_Id
:= Left_Opnd
(N
);
6762 Rhs
: constant Node_Id
:= Right_Opnd
(N
);
6763 Bodies
: constant List_Id
:= New_List
;
6764 A_Typ
: constant Entity_Id
:= Etype
(Lhs
);
6766 Typl
: Entity_Id
:= A_Typ
;
6767 Op_Name
: Entity_Id
;
6770 procedure Build_Equality_Call
(Eq
: Entity_Id
);
6771 -- If a constructed equality exists for the type or for its parent,
6772 -- build and analyze call, adding conversions if the operation is
6775 function Has_Unconstrained_UU_Component
(Typ
: Node_Id
) return Boolean;
6776 -- Determines whether a type has a subcomponent of an unconstrained
6777 -- Unchecked_Union subtype. Typ is a record type.
6779 -------------------------
6780 -- Build_Equality_Call --
6781 -------------------------
6783 procedure Build_Equality_Call
(Eq
: Entity_Id
) is
6784 Op_Type
: constant Entity_Id
:= Etype
(First_Formal
(Eq
));
6785 L_Exp
: Node_Id
:= Relocate_Node
(Lhs
);
6786 R_Exp
: Node_Id
:= Relocate_Node
(Rhs
);
6789 -- Adjust operands if necessary to comparison type
6791 if Base_Type
(Op_Type
) /= Base_Type
(A_Typ
)
6792 and then not Is_Class_Wide_Type
(A_Typ
)
6794 L_Exp
:= OK_Convert_To
(Op_Type
, L_Exp
);
6795 R_Exp
:= OK_Convert_To
(Op_Type
, R_Exp
);
6798 -- If we have an Unchecked_Union, we need to add the inferred
6799 -- discriminant values as actuals in the function call. At this
6800 -- point, the expansion has determined that both operands have
6801 -- inferable discriminants.
6803 if Is_Unchecked_Union
(Op_Type
) then
6805 Lhs_Type
: constant Node_Id
:= Etype
(L_Exp
);
6806 Rhs_Type
: constant Node_Id
:= Etype
(R_Exp
);
6808 Lhs_Discr_Vals
: Elist_Id
;
6809 -- List of inferred discriminant values for left operand.
6811 Rhs_Discr_Vals
: Elist_Id
;
6812 -- List of inferred discriminant values for right operand.
6817 Lhs_Discr_Vals
:= New_Elmt_List
;
6818 Rhs_Discr_Vals
:= New_Elmt_List
;
6820 -- Per-object constrained selected components require special
6821 -- attention. If the enclosing scope of the component is an
6822 -- Unchecked_Union, we cannot reference its discriminants
6823 -- directly. This is why we use the extra parameters of the
6824 -- equality function of the enclosing Unchecked_Union.
6826 -- type UU_Type (Discr : Integer := 0) is
6829 -- pragma Unchecked_Union (UU_Type);
6831 -- 1. Unchecked_Union enclosing record:
6833 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
6835 -- Comp : UU_Type (Discr);
6837 -- end Enclosing_UU_Type;
6838 -- pragma Unchecked_Union (Enclosing_UU_Type);
6840 -- Obj1 : Enclosing_UU_Type;
6841 -- Obj2 : Enclosing_UU_Type (1);
6843 -- [. . .] Obj1 = Obj2 [. . .]
6847 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
6849 -- A and B are the formal parameters of the equality function
6850 -- of Enclosing_UU_Type. The function always has two extra
6851 -- formals to capture the inferred discriminant values for
6852 -- each discriminant of the type.
6854 -- 2. Non-Unchecked_Union enclosing record:
6857 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
6860 -- Comp : UU_Type (Discr);
6862 -- end Enclosing_Non_UU_Type;
6864 -- Obj1 : Enclosing_Non_UU_Type;
6865 -- Obj2 : Enclosing_Non_UU_Type (1);
6867 -- ... Obj1 = Obj2 ...
6871 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
6872 -- obj1.discr, obj2.discr)) then
6874 -- In this case we can directly reference the discriminants of
6875 -- the enclosing record.
6877 -- Process left operand of equality
6879 if Nkind
(Lhs
) = N_Selected_Component
6881 Has_Per_Object_Constraint
(Entity
(Selector_Name
(Lhs
)))
6883 -- If enclosing record is an Unchecked_Union, use formals
6884 -- corresponding to each discriminant. The name of the
6885 -- formal is that of the discriminant, with added suffix,
6886 -- see Exp_Ch3.Build_Record_Equality for details.
6888 if Is_Unchecked_Union
(Scope
(Entity
(Selector_Name
(Lhs
))))
6892 (Scope
(Entity
(Selector_Name
(Lhs
))));
6893 while Present
(Discr
) loop
6895 (Make_Identifier
(Loc
,
6896 Chars
=> New_External_Name
(Chars
(Discr
), 'A')),
6897 To
=> Lhs_Discr_Vals
);
6898 Next_Discriminant
(Discr
);
6901 -- If enclosing record is of a non-Unchecked_Union type, it
6902 -- is possible to reference its discriminants directly.
6905 Discr
:= First_Discriminant
(Lhs_Type
);
6906 while Present
(Discr
) loop
6908 (Make_Selected_Component
(Loc
,
6909 Prefix
=> Prefix
(Lhs
),
6912 (Get_Discriminant_Value
(Discr
,
6914 Stored_Constraint
(Lhs_Type
)))),
6915 To
=> Lhs_Discr_Vals
);
6916 Next_Discriminant
(Discr
);
6920 -- Otherwise operand is on object with a constrained type.
6921 -- Infer the discriminant values from the constraint.
6925 Discr
:= First_Discriminant
(Lhs_Type
);
6926 while Present
(Discr
) loop
6929 (Get_Discriminant_Value
(Discr
,
6931 Stored_Constraint
(Lhs_Type
))),
6932 To
=> Lhs_Discr_Vals
);
6933 Next_Discriminant
(Discr
);
6937 -- Similar processing for right operand of equality
6939 if Nkind
(Rhs
) = N_Selected_Component
6941 Has_Per_Object_Constraint
(Entity
(Selector_Name
(Rhs
)))
6943 if Is_Unchecked_Union
6944 (Scope
(Entity
(Selector_Name
(Rhs
))))
6948 (Scope
(Entity
(Selector_Name
(Rhs
))));
6949 while Present
(Discr
) loop
6951 (Make_Identifier
(Loc
,
6952 Chars
=> New_External_Name
(Chars
(Discr
), 'B')),
6953 To
=> Rhs_Discr_Vals
);
6954 Next_Discriminant
(Discr
);
6958 Discr
:= First_Discriminant
(Rhs_Type
);
6959 while Present
(Discr
) loop
6961 (Make_Selected_Component
(Loc
,
6962 Prefix
=> Prefix
(Rhs
),
6964 New_Copy
(Get_Discriminant_Value
6967 Stored_Constraint
(Rhs_Type
)))),
6968 To
=> Rhs_Discr_Vals
);
6969 Next_Discriminant
(Discr
);
6974 Discr
:= First_Discriminant
(Rhs_Type
);
6975 while Present
(Discr
) loop
6977 (New_Copy
(Get_Discriminant_Value
6980 Stored_Constraint
(Rhs_Type
))),
6981 To
=> Rhs_Discr_Vals
);
6982 Next_Discriminant
(Discr
);
6986 -- Now merge the list of discriminant values so that values
6987 -- of corresponding discriminants are adjacent.
6995 Params
:= New_List
(L_Exp
, R_Exp
);
6996 L_Elmt
:= First_Elmt
(Lhs_Discr_Vals
);
6997 R_Elmt
:= First_Elmt
(Rhs_Discr_Vals
);
6998 while Present
(L_Elmt
) loop
6999 Append_To
(Params
, Node
(L_Elmt
));
7000 Append_To
(Params
, Node
(R_Elmt
));
7006 Make_Function_Call
(Loc
,
7007 Name
=> New_Occurrence_Of
(Eq
, Loc
),
7008 Parameter_Associations
=> Params
));
7012 -- Normal case, not an unchecked union
7016 Make_Function_Call
(Loc
,
7017 Name
=> New_Occurrence_Of
(Eq
, Loc
),
7018 Parameter_Associations
=> New_List
(L_Exp
, R_Exp
)));
7021 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7022 end Build_Equality_Call
;
7024 ------------------------------------
7025 -- Has_Unconstrained_UU_Component --
7026 ------------------------------------
7028 function Has_Unconstrained_UU_Component
7029 (Typ
: Node_Id
) return Boolean
7031 Tdef
: constant Node_Id
:=
7032 Type_Definition
(Declaration_Node
(Base_Type
(Typ
)));
7036 function Component_Is_Unconstrained_UU
7037 (Comp
: Node_Id
) return Boolean;
7038 -- Determines whether the subtype of the component is an
7039 -- unconstrained Unchecked_Union.
7041 function Variant_Is_Unconstrained_UU
7042 (Variant
: Node_Id
) return Boolean;
7043 -- Determines whether a component of the variant has an unconstrained
7044 -- Unchecked_Union subtype.
7046 -----------------------------------
7047 -- Component_Is_Unconstrained_UU --
7048 -----------------------------------
7050 function Component_Is_Unconstrained_UU
7051 (Comp
: Node_Id
) return Boolean
7054 if Nkind
(Comp
) /= N_Component_Declaration
then
7059 Sindic
: constant Node_Id
:=
7060 Subtype_Indication
(Component_Definition
(Comp
));
7063 -- Unconstrained nominal type. In the case of a constraint
7064 -- present, the node kind would have been N_Subtype_Indication.
7066 if Nkind
(Sindic
) = N_Identifier
then
7067 return Is_Unchecked_Union
(Base_Type
(Etype
(Sindic
)));
7072 end Component_Is_Unconstrained_UU
;
7074 ---------------------------------
7075 -- Variant_Is_Unconstrained_UU --
7076 ---------------------------------
7078 function Variant_Is_Unconstrained_UU
7079 (Variant
: Node_Id
) return Boolean
7081 Clist
: constant Node_Id
:= Component_List
(Variant
);
7084 if Is_Empty_List
(Component_Items
(Clist
)) then
7088 -- We only need to test one component
7091 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
7094 while Present
(Comp
) loop
7095 if Component_Is_Unconstrained_UU
(Comp
) then
7103 -- None of the components withing the variant were of
7104 -- unconstrained Unchecked_Union type.
7107 end Variant_Is_Unconstrained_UU
;
7109 -- Start of processing for Has_Unconstrained_UU_Component
7112 if Null_Present
(Tdef
) then
7116 Clist
:= Component_List
(Tdef
);
7117 Vpart
:= Variant_Part
(Clist
);
7119 -- Inspect available components
7121 if Present
(Component_Items
(Clist
)) then
7123 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
7126 while Present
(Comp
) loop
7128 -- One component is sufficient
7130 if Component_Is_Unconstrained_UU
(Comp
) then
7139 -- Inspect available components withing variants
7141 if Present
(Vpart
) then
7143 Variant
: Node_Id
:= First
(Variants
(Vpart
));
7146 while Present
(Variant
) loop
7148 -- One component within a variant is sufficient
7150 if Variant_Is_Unconstrained_UU
(Variant
) then
7159 -- Neither the available components, nor the components inside the
7160 -- variant parts were of an unconstrained Unchecked_Union subtype.
7163 end Has_Unconstrained_UU_Component
;
7165 -- Start of processing for Expand_N_Op_Eq
7168 Binary_Op_Validity_Checks
(N
);
7170 -- Deal with private types
7172 if Ekind
(Typl
) = E_Private_Type
then
7173 Typl
:= Underlying_Type
(Typl
);
7174 elsif Ekind
(Typl
) = E_Private_Subtype
then
7175 Typl
:= Underlying_Type
(Base_Type
(Typl
));
7180 -- It may happen in error situations that the underlying type is not
7181 -- set. The error will be detected later, here we just defend the
7188 -- Now get the implementation base type (note that plain Base_Type here
7189 -- might lead us back to the private type, which is not what we want!)
7191 Typl
:= Implementation_Base_Type
(Typl
);
7193 -- Equality between variant records results in a call to a routine
7194 -- that has conditional tests of the discriminant value(s), and hence
7195 -- violates the No_Implicit_Conditionals restriction.
7197 if Has_Variant_Part
(Typl
) then
7202 Check_Restriction
(Msg
, No_Implicit_Conditionals
, N
);
7206 ("\comparison of variant records tests discriminants", N
);
7212 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7213 -- means we no longer have a comparison operation, we are all done.
7215 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
7217 if Nkind
(N
) /= N_Op_Eq
then
7221 -- Boolean types (requiring handling of non-standard case)
7223 if Is_Boolean_Type
(Typl
) then
7224 Adjust_Condition
(Left_Opnd
(N
));
7225 Adjust_Condition
(Right_Opnd
(N
));
7226 Set_Etype
(N
, Standard_Boolean
);
7227 Adjust_Result_Type
(N
, Typ
);
7231 elsif Is_Array_Type
(Typl
) then
7233 -- If we are doing full validity checking, and it is possible for the
7234 -- array elements to be invalid then expand out array comparisons to
7235 -- make sure that we check the array elements.
7237 if Validity_Check_Operands
7238 and then not Is_Known_Valid
(Component_Type
(Typl
))
7241 Save_Force_Validity_Checks
: constant Boolean :=
7242 Force_Validity_Checks
;
7244 Force_Validity_Checks
:= True;
7246 Expand_Array_Equality
7248 Relocate_Node
(Lhs
),
7249 Relocate_Node
(Rhs
),
7252 Insert_Actions
(N
, Bodies
);
7253 Analyze_And_Resolve
(N
, Standard_Boolean
);
7254 Force_Validity_Checks
:= Save_Force_Validity_Checks
;
7257 -- Packed case where both operands are known aligned
7259 elsif Is_Bit_Packed_Array
(Typl
)
7260 and then not Is_Possibly_Unaligned_Object
(Lhs
)
7261 and then not Is_Possibly_Unaligned_Object
(Rhs
)
7263 Expand_Packed_Eq
(N
);
7265 -- Where the component type is elementary we can use a block bit
7266 -- comparison (if supported on the target) exception in the case
7267 -- of floating-point (negative zero issues require element by
7268 -- element comparison), and atomic types (where we must be sure
7269 -- to load elements independently) and possibly unaligned arrays.
7271 elsif Is_Elementary_Type
(Component_Type
(Typl
))
7272 and then not Is_Floating_Point_Type
(Component_Type
(Typl
))
7273 and then not Is_Atomic
(Component_Type
(Typl
))
7274 and then not Is_Possibly_Unaligned_Object
(Lhs
)
7275 and then not Is_Possibly_Unaligned_Object
(Rhs
)
7276 and then Support_Composite_Compare_On_Target
7280 -- For composite and floating-point cases, expand equality loop to
7281 -- make sure of using proper comparisons for tagged types, and
7282 -- correctly handling the floating-point case.
7286 Expand_Array_Equality
7288 Relocate_Node
(Lhs
),
7289 Relocate_Node
(Rhs
),
7292 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
7293 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7298 elsif Is_Record_Type
(Typl
) then
7300 -- For tagged types, use the primitive "="
7302 if Is_Tagged_Type
(Typl
) then
7304 -- No need to do anything else compiling under restriction
7305 -- No_Dispatching_Calls. During the semantic analysis we
7306 -- already notified such violation.
7308 if Restriction_Active
(No_Dispatching_Calls
) then
7312 -- If this is derived from an untagged private type completed with
7313 -- a tagged type, it does not have a full view, so we use the
7314 -- primitive operations of the private type. This check should no
7315 -- longer be necessary when these types get their full views???
7317 if Is_Private_Type
(A_Typ
)
7318 and then not Is_Tagged_Type
(A_Typ
)
7319 and then Is_Derived_Type
(A_Typ
)
7320 and then No
(Full_View
(A_Typ
))
7322 -- Search for equality operation, checking that the operands
7323 -- have the same type. Note that we must find a matching entry,
7324 -- or something is very wrong.
7326 Prim
:= First_Elmt
(Collect_Primitive_Operations
(A_Typ
));
7328 while Present
(Prim
) loop
7329 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7330 and then Etype
(First_Formal
(Node
(Prim
))) =
7331 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7333 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7338 pragma Assert
(Present
(Prim
));
7339 Op_Name
:= Node
(Prim
);
7341 -- Find the type's predefined equality or an overriding
7342 -- user-defined equality. The reason for not simply calling
7343 -- Find_Prim_Op here is that there may be a user-defined
7344 -- overloaded equality op that precedes the equality that we
7345 -- want, so we have to explicitly search (e.g., there could be
7346 -- an equality with two different parameter types).
7349 if Is_Class_Wide_Type
(Typl
) then
7350 Typl
:= Find_Specific_Type
(Typl
);
7353 Prim
:= First_Elmt
(Primitive_Operations
(Typl
));
7354 while Present
(Prim
) loop
7355 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7356 and then Etype
(First_Formal
(Node
(Prim
))) =
7357 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7359 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7364 pragma Assert
(Present
(Prim
));
7365 Op_Name
:= Node
(Prim
);
7368 Build_Equality_Call
(Op_Name
);
7370 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7371 -- predefined equality operator for a type which has a subcomponent
7372 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7374 elsif Has_Unconstrained_UU_Component
(Typl
) then
7376 Make_Raise_Program_Error
(Loc
,
7377 Reason
=> PE_Unchecked_Union_Restriction
));
7379 -- Prevent Gigi from generating incorrect code by rewriting the
7380 -- equality as a standard False. (is this documented somewhere???)
7383 New_Occurrence_Of
(Standard_False
, Loc
));
7385 elsif Is_Unchecked_Union
(Typl
) then
7387 -- If we can infer the discriminants of the operands, we make a
7388 -- call to the TSS equality function.
7390 if Has_Inferable_Discriminants
(Lhs
)
7392 Has_Inferable_Discriminants
(Rhs
)
7395 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
7398 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7399 -- the predefined equality operator for an Unchecked_Union type
7400 -- if either of the operands lack inferable discriminants.
7403 Make_Raise_Program_Error
(Loc
,
7404 Reason
=> PE_Unchecked_Union_Restriction
));
7406 -- Emit a warning on source equalities only, otherwise the
7407 -- message may appear out of place due to internal use. The
7408 -- warning is unconditional because it is required by the
7411 if Comes_From_Source
(N
) then
7413 ("Unchecked_Union discriminants cannot be determined??",
7416 ("\Program_Error will be raised for equality operation??",
7420 -- Prevent Gigi from generating incorrect code by rewriting
7421 -- the equality as a standard False (documented where???).
7424 New_Occurrence_Of
(Standard_False
, Loc
));
7427 -- If a type support function is present (for complex cases), use it
7429 elsif Present
(TSS
(Root_Type
(Typl
), TSS_Composite_Equality
)) then
7431 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
7433 -- When comparing two Bounded_Strings, use the primitive equality of
7434 -- the root Super_String type.
7436 elsif Is_Bounded_String
(Typl
) then
7438 First_Elmt
(Collect_Primitive_Operations
(Root_Type
(Typl
)));
7440 while Present
(Prim
) loop
7441 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7442 and then Etype
(First_Formal
(Node
(Prim
))) =
7443 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7444 and then Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7449 -- A Super_String type should always have a primitive equality
7451 pragma Assert
(Present
(Prim
));
7452 Build_Equality_Call
(Node
(Prim
));
7454 -- Otherwise expand the component by component equality. Note that
7455 -- we never use block-bit comparisons for records, because of the
7456 -- problems with gaps. The backend will often be able to recombine
7457 -- the separate comparisons that we generate here.
7460 Remove_Side_Effects
(Lhs
);
7461 Remove_Side_Effects
(Rhs
);
7463 Expand_Record_Equality
(N
, Typl
, Lhs
, Rhs
, Bodies
));
7465 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
7466 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7470 -- Test if result is known at compile time
7472 Rewrite_Comparison
(N
);
7474 Optimize_Length_Comparison
(N
);
7477 -----------------------
7478 -- Expand_N_Op_Expon --
7479 -----------------------
7481 procedure Expand_N_Op_Expon
(N
: Node_Id
) is
7482 Loc
: constant Source_Ptr
:= Sloc
(N
);
7483 Typ
: constant Entity_Id
:= Etype
(N
);
7484 Rtyp
: constant Entity_Id
:= Root_Type
(Typ
);
7485 Base
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
7486 Bastyp
: constant Node_Id
:= Etype
(Base
);
7487 Exp
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
7488 Exptyp
: constant Entity_Id
:= Etype
(Exp
);
7489 Ovflo
: constant Boolean := Do_Overflow_Check
(N
);
7498 Binary_Op_Validity_Checks
(N
);
7500 -- CodePeer wants to see the unexpanded N_Op_Expon node
7502 if CodePeer_Mode
then
7506 -- If either operand is of a private type, then we have the use of an
7507 -- intrinsic operator, and we get rid of the privateness, by using root
7508 -- types of underlying types for the actual operation. Otherwise the
7509 -- private types will cause trouble if we expand multiplications or
7510 -- shifts etc. We also do this transformation if the result type is
7511 -- different from the base type.
7513 if Is_Private_Type
(Etype
(Base
))
7514 or else Is_Private_Type
(Typ
)
7515 or else Is_Private_Type
(Exptyp
)
7516 or else Rtyp
/= Root_Type
(Bastyp
)
7519 Bt
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Bastyp
));
7520 Et
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Exptyp
));
7523 Unchecked_Convert_To
(Typ
,
7525 Left_Opnd
=> Unchecked_Convert_To
(Bt
, Base
),
7526 Right_Opnd
=> Unchecked_Convert_To
(Et
, Exp
))));
7527 Analyze_And_Resolve
(N
, Typ
);
7532 -- Check for MINIMIZED/ELIMINATED overflow mode
7534 if Minimized_Eliminated_Overflow_Check
(N
) then
7535 Apply_Arithmetic_Overflow_Check
(N
);
7539 -- Test for case of known right argument where we can replace the
7540 -- exponentiation by an equivalent expression using multiplication.
7542 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
7543 -- configurable run-time mode, we may not have the exponentiation
7544 -- routine available, and we don't want the legality of the program
7545 -- to depend on how clever the compiler is in knowing values.
7547 if CRT_Safe_Compile_Time_Known_Value
(Exp
) then
7548 Expv
:= Expr_Value
(Exp
);
7550 -- We only fold small non-negative exponents. You might think we
7551 -- could fold small negative exponents for the real case, but we
7552 -- can't because we are required to raise Constraint_Error for
7553 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
7554 -- See ACVC test C4A012B.
7556 if Expv
>= 0 and then Expv
<= 4 then
7558 -- X ** 0 = 1 (or 1.0)
7562 -- Call Remove_Side_Effects to ensure that any side effects
7563 -- in the ignored left operand (in particular function calls
7564 -- to user defined functions) are properly executed.
7566 Remove_Side_Effects
(Base
);
7568 if Ekind
(Typ
) in Integer_Kind
then
7569 Xnode
:= Make_Integer_Literal
(Loc
, Intval
=> 1);
7571 Xnode
:= Make_Real_Literal
(Loc
, Ureal_1
);
7583 Make_Op_Multiply
(Loc
,
7584 Left_Opnd
=> Duplicate_Subexpr
(Base
),
7585 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
));
7587 -- X ** 3 = X * X * X
7591 Make_Op_Multiply
(Loc
,
7593 Make_Op_Multiply
(Loc
,
7594 Left_Opnd
=> Duplicate_Subexpr
(Base
),
7595 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)),
7596 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
));
7601 -- En : constant base'type := base * base;
7606 pragma Assert
(Expv
= 4);
7607 Temp
:= Make_Temporary
(Loc
, 'E', Base
);
7610 Make_Expression_With_Actions
(Loc
,
7611 Actions
=> New_List
(
7612 Make_Object_Declaration
(Loc
,
7613 Defining_Identifier
=> Temp
,
7614 Constant_Present
=> True,
7615 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
7617 Make_Op_Multiply
(Loc
,
7619 Duplicate_Subexpr
(Base
),
7621 Duplicate_Subexpr_No_Checks
(Base
)))),
7624 Make_Op_Multiply
(Loc
,
7625 Left_Opnd
=> New_Occurrence_Of
(Temp
, Loc
),
7626 Right_Opnd
=> New_Occurrence_Of
(Temp
, Loc
)));
7630 Analyze_And_Resolve
(N
, Typ
);
7635 -- Case of (2 ** expression) appearing as an argument of an integer
7636 -- multiplication, or as the right argument of a division of a non-
7637 -- negative integer. In such cases we leave the node untouched, setting
7638 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
7639 -- of the higher level node converts it into a shift.
7641 -- Another case is 2 ** N in any other context. We simply convert
7642 -- this to 1 * 2 ** N, and then the above transformation applies.
7644 -- Note: this transformation is not applicable for a modular type with
7645 -- a non-binary modulus in the multiplication case, since we get a wrong
7646 -- result if the shift causes an overflow before the modular reduction.
7648 -- Note: we used to check that Exptyp was an unsigned type. But that is
7649 -- an unnecessary check, since if Exp is negative, we have a run-time
7650 -- error that is either caught (so we get the right result) or we have
7651 -- suppressed the check, in which case the code is erroneous anyway.
7653 if Nkind
(Base
) = N_Integer_Literal
7654 and then CRT_Safe_Compile_Time_Known_Value
(Base
)
7655 and then Expr_Value
(Base
) = Uint_2
7656 and then Is_Integer_Type
(Root_Type
(Exptyp
))
7657 and then Esize
(Root_Type
(Exptyp
)) <= Esize
(Standard_Integer
)
7660 -- First the multiply and divide cases
7662 if Nkind_In
(Parent
(N
), N_Op_Divide
, N_Op_Multiply
) then
7664 P
: constant Node_Id
:= Parent
(N
);
7665 L
: constant Node_Id
:= Left_Opnd
(P
);
7666 R
: constant Node_Id
:= Right_Opnd
(P
);
7669 if (Nkind
(P
) = N_Op_Multiply
7670 and then not Non_Binary_Modulus
(Typ
)
7672 ((Is_Integer_Type
(Etype
(L
)) and then R
= N
)
7674 (Is_Integer_Type
(Etype
(R
)) and then L
= N
))
7675 and then not Do_Overflow_Check
(P
))
7677 (Nkind
(P
) = N_Op_Divide
7678 and then Is_Integer_Type
(Etype
(L
))
7679 and then Is_Unsigned_Type
(Etype
(L
))
7681 and then not Do_Overflow_Check
(P
))
7683 Set_Is_Power_Of_2_For_Shift
(N
);
7688 -- Now the other cases
7690 elsif not Non_Binary_Modulus
(Typ
) then
7692 Make_Op_Multiply
(Loc
,
7693 Left_Opnd
=> Make_Integer_Literal
(Loc
, 1),
7694 Right_Opnd
=> Relocate_Node
(N
)));
7695 Analyze_And_Resolve
(N
, Typ
);
7700 -- Fall through if exponentiation must be done using a runtime routine
7702 -- First deal with modular case
7704 if Is_Modular_Integer_Type
(Rtyp
) then
7706 -- Non-binary case, we call the special exponentiation routine for
7707 -- the non-binary case, converting the argument to Long_Long_Integer
7708 -- and passing the modulus value. Then the result is converted back
7709 -- to the base type.
7711 if Non_Binary_Modulus
(Rtyp
) then
7714 Make_Function_Call
(Loc
,
7716 New_Occurrence_Of
(RTE
(RE_Exp_Modular
), Loc
),
7717 Parameter_Associations
=> New_List
(
7718 Convert_To
(RTE
(RE_Unsigned
), Base
),
7719 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
)),
7722 -- Binary case, in this case, we call one of two routines, either the
7723 -- unsigned integer case, or the unsigned long long integer case,
7724 -- with a final "and" operation to do the required mod.
7727 if UI_To_Int
(Esize
(Rtyp
)) <= Standard_Integer_Size
then
7728 Ent
:= RTE
(RE_Exp_Unsigned
);
7730 Ent
:= RTE
(RE_Exp_Long_Long_Unsigned
);
7737 Make_Function_Call
(Loc
,
7738 Name
=> New_Occurrence_Of
(Ent
, Loc
),
7739 Parameter_Associations
=> New_List
(
7740 Convert_To
(Etype
(First_Formal
(Ent
)), Base
),
7743 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
) - 1))));
7747 -- Common exit point for modular type case
7749 Analyze_And_Resolve
(N
, Typ
);
7752 -- Signed integer cases, done using either Integer or Long_Long_Integer.
7753 -- It is not worth having routines for Short_[Short_]Integer, since for
7754 -- most machines it would not help, and it would generate more code that
7755 -- might need certification when a certified run time is required.
7757 -- In the integer cases, we have two routines, one for when overflow
7758 -- checks are required, and one when they are not required, since there
7759 -- is a real gain in omitting checks on many machines.
7761 elsif Rtyp
= Base_Type
(Standard_Long_Long_Integer
)
7762 or else (Rtyp
= Base_Type
(Standard_Long_Integer
)
7764 Esize
(Standard_Long_Integer
) > Esize
(Standard_Integer
))
7765 or else Rtyp
= Universal_Integer
7767 Etyp
:= Standard_Long_Long_Integer
;
7769 -- Overflow checking is the only choice on the AAMP target, where
7770 -- arithmetic instructions check overflow automatically, so only
7771 -- one version of the exponentiation unit is needed.
7773 if Ovflo
or AAMP_On_Target
then
7774 Rent
:= RE_Exp_Long_Long_Integer
;
7776 Rent
:= RE_Exn_Long_Long_Integer
;
7779 elsif Is_Signed_Integer_Type
(Rtyp
) then
7780 Etyp
:= Standard_Integer
;
7782 -- Overflow checking is the only choice on the AAMP target, where
7783 -- arithmetic instructions check overflow automatically, so only
7784 -- one version of the exponentiation unit is needed.
7786 if Ovflo
or AAMP_On_Target
then
7787 Rent
:= RE_Exp_Integer
;
7789 Rent
:= RE_Exn_Integer
;
7792 -- Floating-point cases, always done using Long_Long_Float. We do not
7793 -- need separate routines for the overflow case here, since in the case
7794 -- of floating-point, we generate infinities anyway as a rule (either
7795 -- that or we automatically trap overflow), and if there is an infinity
7796 -- generated and a range check is required, the check will fail anyway.
7799 pragma Assert
(Is_Floating_Point_Type
(Rtyp
));
7800 Etyp
:= Standard_Long_Long_Float
;
7801 Rent
:= RE_Exn_Long_Long_Float
;
7804 -- Common processing for integer cases and floating-point cases.
7805 -- If we are in the right type, we can call runtime routine directly
7808 and then Rtyp
/= Universal_Integer
7809 and then Rtyp
/= Universal_Real
7812 Make_Function_Call
(Loc
,
7813 Name
=> New_Occurrence_Of
(RTE
(Rent
), Loc
),
7814 Parameter_Associations
=> New_List
(Base
, Exp
)));
7816 -- Otherwise we have to introduce conversions (conversions are also
7817 -- required in the universal cases, since the runtime routine is
7818 -- typed using one of the standard types).
7823 Make_Function_Call
(Loc
,
7824 Name
=> New_Occurrence_Of
(RTE
(Rent
), Loc
),
7825 Parameter_Associations
=> New_List
(
7826 Convert_To
(Etyp
, Base
),
7830 Analyze_And_Resolve
(N
, Typ
);
7834 when RE_Not_Available
=>
7836 end Expand_N_Op_Expon
;
7838 --------------------
7839 -- Expand_N_Op_Ge --
7840 --------------------
7842 procedure Expand_N_Op_Ge
(N
: Node_Id
) is
7843 Typ
: constant Entity_Id
:= Etype
(N
);
7844 Op1
: constant Node_Id
:= Left_Opnd
(N
);
7845 Op2
: constant Node_Id
:= Right_Opnd
(N
);
7846 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
7849 Binary_Op_Validity_Checks
(N
);
7851 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7852 -- means we no longer have a comparison operation, we are all done.
7854 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
7856 if Nkind
(N
) /= N_Op_Ge
then
7862 if Is_Array_Type
(Typ1
) then
7863 Expand_Array_Comparison
(N
);
7867 -- Deal with boolean operands
7869 if Is_Boolean_Type
(Typ1
) then
7870 Adjust_Condition
(Op1
);
7871 Adjust_Condition
(Op2
);
7872 Set_Etype
(N
, Standard_Boolean
);
7873 Adjust_Result_Type
(N
, Typ
);
7876 Rewrite_Comparison
(N
);
7878 Optimize_Length_Comparison
(N
);
7881 --------------------
7882 -- Expand_N_Op_Gt --
7883 --------------------
7885 procedure Expand_N_Op_Gt
(N
: Node_Id
) is
7886 Typ
: constant Entity_Id
:= Etype
(N
);
7887 Op1
: constant Node_Id
:= Left_Opnd
(N
);
7888 Op2
: constant Node_Id
:= Right_Opnd
(N
);
7889 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
7892 Binary_Op_Validity_Checks
(N
);
7894 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7895 -- means we no longer have a comparison operation, we are all done.
7897 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
7899 if Nkind
(N
) /= N_Op_Gt
then
7903 -- Deal with array type operands
7905 if Is_Array_Type
(Typ1
) then
7906 Expand_Array_Comparison
(N
);
7910 -- Deal with boolean type operands
7912 if Is_Boolean_Type
(Typ1
) then
7913 Adjust_Condition
(Op1
);
7914 Adjust_Condition
(Op2
);
7915 Set_Etype
(N
, Standard_Boolean
);
7916 Adjust_Result_Type
(N
, Typ
);
7919 Rewrite_Comparison
(N
);
7921 Optimize_Length_Comparison
(N
);
7924 --------------------
7925 -- Expand_N_Op_Le --
7926 --------------------
7928 procedure Expand_N_Op_Le
(N
: Node_Id
) is
7929 Typ
: constant Entity_Id
:= Etype
(N
);
7930 Op1
: constant Node_Id
:= Left_Opnd
(N
);
7931 Op2
: constant Node_Id
:= Right_Opnd
(N
);
7932 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
7935 Binary_Op_Validity_Checks
(N
);
7937 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7938 -- means we no longer have a comparison operation, we are all done.
7940 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
7942 if Nkind
(N
) /= N_Op_Le
then
7946 -- Deal with array type operands
7948 if Is_Array_Type
(Typ1
) then
7949 Expand_Array_Comparison
(N
);
7953 -- Deal with Boolean type operands
7955 if Is_Boolean_Type
(Typ1
) then
7956 Adjust_Condition
(Op1
);
7957 Adjust_Condition
(Op2
);
7958 Set_Etype
(N
, Standard_Boolean
);
7959 Adjust_Result_Type
(N
, Typ
);
7962 Rewrite_Comparison
(N
);
7964 Optimize_Length_Comparison
(N
);
7967 --------------------
7968 -- Expand_N_Op_Lt --
7969 --------------------
7971 procedure Expand_N_Op_Lt
(N
: Node_Id
) is
7972 Typ
: constant Entity_Id
:= Etype
(N
);
7973 Op1
: constant Node_Id
:= Left_Opnd
(N
);
7974 Op2
: constant Node_Id
:= Right_Opnd
(N
);
7975 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
7978 Binary_Op_Validity_Checks
(N
);
7980 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7981 -- means we no longer have a comparison operation, we are all done.
7983 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
7985 if Nkind
(N
) /= N_Op_Lt
then
7989 -- Deal with array type operands
7991 if Is_Array_Type
(Typ1
) then
7992 Expand_Array_Comparison
(N
);
7996 -- Deal with Boolean type operands
7998 if Is_Boolean_Type
(Typ1
) then
7999 Adjust_Condition
(Op1
);
8000 Adjust_Condition
(Op2
);
8001 Set_Etype
(N
, Standard_Boolean
);
8002 Adjust_Result_Type
(N
, Typ
);
8005 Rewrite_Comparison
(N
);
8007 Optimize_Length_Comparison
(N
);
8010 -----------------------
8011 -- Expand_N_Op_Minus --
8012 -----------------------
8014 procedure Expand_N_Op_Minus
(N
: Node_Id
) is
8015 Loc
: constant Source_Ptr
:= Sloc
(N
);
8016 Typ
: constant Entity_Id
:= Etype
(N
);
8019 Unary_Op_Validity_Checks
(N
);
8021 -- Check for MINIMIZED/ELIMINATED overflow mode
8023 if Minimized_Eliminated_Overflow_Check
(N
) then
8024 Apply_Arithmetic_Overflow_Check
(N
);
8028 if not Backend_Overflow_Checks_On_Target
8029 and then Is_Signed_Integer_Type
(Etype
(N
))
8030 and then Do_Overflow_Check
(N
)
8032 -- Software overflow checking expands -expr into (0 - expr)
8035 Make_Op_Subtract
(Loc
,
8036 Left_Opnd
=> Make_Integer_Literal
(Loc
, 0),
8037 Right_Opnd
=> Right_Opnd
(N
)));
8039 Analyze_And_Resolve
(N
, Typ
);
8041 end Expand_N_Op_Minus
;
8043 ---------------------
8044 -- Expand_N_Op_Mod --
8045 ---------------------
8047 procedure Expand_N_Op_Mod
(N
: Node_Id
) is
8048 Loc
: constant Source_Ptr
:= Sloc
(N
);
8049 Typ
: constant Entity_Id
:= Etype
(N
);
8050 DDC
: constant Boolean := Do_Division_Check
(N
);
8063 pragma Warnings
(Off
, Lhi
);
8066 Binary_Op_Validity_Checks
(N
);
8068 -- Check for MINIMIZED/ELIMINATED overflow mode
8070 if Minimized_Eliminated_Overflow_Check
(N
) then
8071 Apply_Arithmetic_Overflow_Check
(N
);
8075 if Is_Integer_Type
(Etype
(N
)) then
8076 Apply_Divide_Checks
(N
);
8078 -- All done if we don't have a MOD any more, which can happen as a
8079 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8081 if Nkind
(N
) /= N_Op_Mod
then
8086 -- Proceed with expansion of mod operator
8088 Left
:= Left_Opnd
(N
);
8089 Right
:= Right_Opnd
(N
);
8091 Determine_Range
(Right
, ROK
, Rlo
, Rhi
, Assume_Valid
=> True);
8092 Determine_Range
(Left
, LOK
, Llo
, Lhi
, Assume_Valid
=> True);
8094 -- Convert mod to rem if operands are both known to be non-negative, or
8095 -- both known to be non-positive (these are the cases in which rem and
8096 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
8097 -- likely that this will improve the quality of code, (the operation now
8098 -- corresponds to the hardware remainder), and it does not seem likely
8099 -- that it could be harmful. It also avoids some cases of the elaborate
8100 -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %).
8103 and then ((Llo
>= 0 and then Rlo
>= 0)
8105 (Lhi
<= 0 and then Rhi
<= 0))
8108 Make_Op_Rem
(Sloc
(N
),
8109 Left_Opnd
=> Left_Opnd
(N
),
8110 Right_Opnd
=> Right_Opnd
(N
)));
8112 -- Instead of reanalyzing the node we do the analysis manually. This
8113 -- avoids anomalies when the replacement is done in an instance and
8114 -- is epsilon more efficient.
8116 Set_Entity
(N
, Standard_Entity
(S_Op_Rem
));
8118 Set_Do_Division_Check
(N
, DDC
);
8119 Expand_N_Op_Rem
(N
);
8123 -- Otherwise, normal mod processing
8126 -- Apply optimization x mod 1 = 0. We don't really need that with
8127 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
8128 -- certainly harmless.
8130 if Is_Integer_Type
(Etype
(N
))
8131 and then Compile_Time_Known_Value
(Right
)
8132 and then Expr_Value
(Right
) = Uint_1
8134 -- Call Remove_Side_Effects to ensure that any side effects in
8135 -- the ignored left operand (in particular function calls to
8136 -- user defined functions) are properly executed.
8138 Remove_Side_Effects
(Left
);
8140 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
8141 Analyze_And_Resolve
(N
, Typ
);
8145 -- If we still have a mod operator and we are in Modify_Tree_For_C
8146 -- mode, and we have a signed integer type, then here is where we do
8147 -- the rewrite in terms of Rem. Note this rewrite bypasses the need
8148 -- for the special handling of the annoying case of largest negative
8149 -- number mod minus one.
8151 if Nkind
(N
) = N_Op_Mod
8152 and then Is_Signed_Integer_Type
(Typ
)
8153 and then Modify_Tree_For_C
8155 -- In the general case, we expand A mod B as
8157 -- Tnn : constant typ := A rem B;
8159 -- (if (A >= 0) = (B >= 0) then Tnn
8160 -- elsif Tnn = 0 then 0
8163 -- The comparison can be written simply as A >= 0 if we know that
8164 -- B >= 0 which is a very common case.
8166 -- An important optimization is when B is known at compile time
8167 -- to be 2**K for some constant. In this case we can simply AND
8168 -- the left operand with the bit string 2**K-1 (i.e. K 1-bits)
8169 -- and that works for both the positive and negative cases.
8172 P2
: constant Nat
:= Power_Of_Two
(Right
);
8177 Unchecked_Convert_To
(Typ
,
8180 Unchecked_Convert_To
8181 (Corresponding_Unsigned_Type
(Typ
), Left
),
8183 Make_Integer_Literal
(Loc
, 2 ** P2
- 1))));
8184 Analyze_And_Resolve
(N
, Typ
);
8189 -- Here for the full rewrite
8192 Tnn
: constant Entity_Id
:= Make_Temporary
(Sloc
(N
), 'T', N
);
8198 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left
),
8199 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
8201 if not LOK
or else Rlo
< 0 then
8207 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Right
),
8208 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)));
8212 Make_Object_Declaration
(Loc
,
8213 Defining_Identifier
=> Tnn
,
8214 Constant_Present
=> True,
8215 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
8219 Right_Opnd
=> Right
)));
8222 Make_If_Expression
(Loc
,
8223 Expressions
=> New_List
(
8225 New_Occurrence_Of
(Tnn
, Loc
),
8226 Make_If_Expression
(Loc
,
8228 Expressions
=> New_List
(
8230 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
8231 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
8232 Make_Integer_Literal
(Loc
, 0),
8234 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
8236 Duplicate_Subexpr_No_Checks
(Right
)))))));
8238 Analyze_And_Resolve
(N
, Typ
);
8243 -- Deal with annoying case of largest negative number mod minus one.
8244 -- Gigi may not handle this case correctly, because on some targets,
8245 -- the mod value is computed using a divide instruction which gives
8246 -- an overflow trap for this case.
8248 -- It would be a bit more efficient to figure out which targets
8249 -- this is really needed for, but in practice it is reasonable
8250 -- to do the following special check in all cases, since it means
8251 -- we get a clearer message, and also the overhead is minimal given
8252 -- that division is expensive in any case.
8254 -- In fact the check is quite easy, if the right operand is -1, then
8255 -- the mod value is always 0, and we can just ignore the left operand
8256 -- completely in this case.
8258 -- This only applies if we still have a mod operator. Skip if we
8259 -- have already rewritten this (e.g. in the case of eliminated
8260 -- overflow checks which have driven us into bignum mode).
8262 if Nkind
(N
) = N_Op_Mod
then
8264 -- The operand type may be private (e.g. in the expansion of an
8265 -- intrinsic operation) so we must use the underlying type to get
8266 -- the bounds, and convert the literals explicitly.
8270 (Type_Low_Bound
(Base_Type
(Underlying_Type
(Etype
(Left
)))));
8272 if ((not ROK
) or else (Rlo
<= (-1) and then (-1) <= Rhi
))
8273 and then ((not LOK
) or else (Llo
= LLB
))
8276 Make_If_Expression
(Loc
,
8277 Expressions
=> New_List
(
8279 Left_Opnd
=> Duplicate_Subexpr
(Right
),
8281 Unchecked_Convert_To
(Typ
,
8282 Make_Integer_Literal
(Loc
, -1))),
8283 Unchecked_Convert_To
(Typ
,
8284 Make_Integer_Literal
(Loc
, Uint_0
)),
8285 Relocate_Node
(N
))));
8287 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
8288 Analyze_And_Resolve
(N
, Typ
);
8292 end Expand_N_Op_Mod
;
8294 --------------------------
8295 -- Expand_N_Op_Multiply --
8296 --------------------------
8298 procedure Expand_N_Op_Multiply
(N
: Node_Id
) is
8299 Loc
: constant Source_Ptr
:= Sloc
(N
);
8300 Lop
: constant Node_Id
:= Left_Opnd
(N
);
8301 Rop
: constant Node_Id
:= Right_Opnd
(N
);
8303 Lp2
: constant Boolean :=
8304 Nkind
(Lop
) = N_Op_Expon
and then Is_Power_Of_2_For_Shift
(Lop
);
8305 Rp2
: constant Boolean :=
8306 Nkind
(Rop
) = N_Op_Expon
and then Is_Power_Of_2_For_Shift
(Rop
);
8308 Ltyp
: constant Entity_Id
:= Etype
(Lop
);
8309 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
8310 Typ
: Entity_Id
:= Etype
(N
);
8313 Binary_Op_Validity_Checks
(N
);
8315 -- Check for MINIMIZED/ELIMINATED overflow mode
8317 if Minimized_Eliminated_Overflow_Check
(N
) then
8318 Apply_Arithmetic_Overflow_Check
(N
);
8322 -- Special optimizations for integer types
8324 if Is_Integer_Type
(Typ
) then
8326 -- N * 0 = 0 for integer types
8328 if Compile_Time_Known_Value
(Rop
)
8329 and then Expr_Value
(Rop
) = Uint_0
8331 -- Call Remove_Side_Effects to ensure that any side effects in
8332 -- the ignored left operand (in particular function calls to
8333 -- user defined functions) are properly executed.
8335 Remove_Side_Effects
(Lop
);
8337 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
8338 Analyze_And_Resolve
(N
, Typ
);
8342 -- Similar handling for 0 * N = 0
8344 if Compile_Time_Known_Value
(Lop
)
8345 and then Expr_Value
(Lop
) = Uint_0
8347 Remove_Side_Effects
(Rop
);
8348 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
8349 Analyze_And_Resolve
(N
, Typ
);
8353 -- N * 1 = 1 * N = N for integer types
8355 -- This optimisation is not done if we are going to
8356 -- rewrite the product 1 * 2 ** N to a shift.
8358 if Compile_Time_Known_Value
(Rop
)
8359 and then Expr_Value
(Rop
) = Uint_1
8365 elsif Compile_Time_Known_Value
(Lop
)
8366 and then Expr_Value
(Lop
) = Uint_1
8374 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
8375 -- Is_Power_Of_2_For_Shift is set means that we know that our left
8376 -- operand is an integer, as required for this to work.
8381 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
8385 Left_Opnd
=> Make_Integer_Literal
(Loc
, 2),
8388 Left_Opnd
=> Right_Opnd
(Lop
),
8389 Right_Opnd
=> Right_Opnd
(Rop
))));
8390 Analyze_And_Resolve
(N
, Typ
);
8394 -- If the result is modular, perform the reduction of the result
8397 if Is_Modular_Integer_Type
(Typ
)
8398 and then not Non_Binary_Modulus
(Typ
)
8403 Make_Op_Shift_Left
(Loc
,
8406 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))),
8408 Make_Integer_Literal
(Loc
, Modulus
(Typ
) - 1)));
8412 Make_Op_Shift_Left
(Loc
,
8415 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))));
8418 Analyze_And_Resolve
(N
, Typ
);
8422 -- Same processing for the operands the other way round
8425 if Is_Modular_Integer_Type
(Typ
)
8426 and then not Non_Binary_Modulus
(Typ
)
8431 Make_Op_Shift_Left
(Loc
,
8434 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))),
8436 Make_Integer_Literal
(Loc
, Modulus
(Typ
) - 1)));
8440 Make_Op_Shift_Left
(Loc
,
8443 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))));
8446 Analyze_And_Resolve
(N
, Typ
);
8450 -- Do required fixup of universal fixed operation
8452 if Typ
= Universal_Fixed
then
8453 Fixup_Universal_Fixed_Operation
(N
);
8457 -- Multiplications with fixed-point results
8459 if Is_Fixed_Point_Type
(Typ
) then
8461 -- No special processing if Treat_Fixed_As_Integer is set, since from
8462 -- a semantic point of view such operations are simply integer
8463 -- operations and will be treated that way.
8465 if not Treat_Fixed_As_Integer
(N
) then
8467 -- Case of fixed * integer => fixed
8469 if Is_Integer_Type
(Rtyp
) then
8470 Expand_Multiply_Fixed_By_Integer_Giving_Fixed
(N
);
8472 -- Case of integer * fixed => fixed
8474 elsif Is_Integer_Type
(Ltyp
) then
8475 Expand_Multiply_Integer_By_Fixed_Giving_Fixed
(N
);
8477 -- Case of fixed * fixed => fixed
8480 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed
(N
);
8484 -- Other cases of multiplication of fixed-point operands. Again we
8485 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
8487 elsif (Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
))
8488 and then not Treat_Fixed_As_Integer
(N
)
8490 if Is_Integer_Type
(Typ
) then
8491 Expand_Multiply_Fixed_By_Fixed_Giving_Integer
(N
);
8493 pragma Assert
(Is_Floating_Point_Type
(Typ
));
8494 Expand_Multiply_Fixed_By_Fixed_Giving_Float
(N
);
8497 -- Mixed-mode operations can appear in a non-static universal context,
8498 -- in which case the integer argument must be converted explicitly.
8500 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Rtyp
) then
8501 Rewrite
(Rop
, Convert_To
(Universal_Real
, Relocate_Node
(Rop
)));
8502 Analyze_And_Resolve
(Rop
, Universal_Real
);
8504 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Ltyp
) then
8505 Rewrite
(Lop
, Convert_To
(Universal_Real
, Relocate_Node
(Lop
)));
8506 Analyze_And_Resolve
(Lop
, Universal_Real
);
8508 -- Non-fixed point cases, check software overflow checking required
8510 elsif Is_Signed_Integer_Type
(Etype
(N
)) then
8511 Apply_Arithmetic_Overflow_Check
(N
);
8514 -- Overflow checks for floating-point if -gnateF mode active
8516 Check_Float_Op_Overflow
(N
);
8517 end Expand_N_Op_Multiply
;
8519 --------------------
8520 -- Expand_N_Op_Ne --
8521 --------------------
8523 procedure Expand_N_Op_Ne
(N
: Node_Id
) is
8524 Typ
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
8527 -- Case of elementary type with standard operator
8529 if Is_Elementary_Type
(Typ
)
8530 and then Sloc
(Entity
(N
)) = Standard_Location
8532 Binary_Op_Validity_Checks
(N
);
8534 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
8535 -- means we no longer have a /= operation, we are all done.
8537 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8539 if Nkind
(N
) /= N_Op_Ne
then
8543 -- Boolean types (requiring handling of non-standard case)
8545 if Is_Boolean_Type
(Typ
) then
8546 Adjust_Condition
(Left_Opnd
(N
));
8547 Adjust_Condition
(Right_Opnd
(N
));
8548 Set_Etype
(N
, Standard_Boolean
);
8549 Adjust_Result_Type
(N
, Typ
);
8552 Rewrite_Comparison
(N
);
8554 -- For all cases other than elementary types, we rewrite node as the
8555 -- negation of an equality operation, and reanalyze. The equality to be
8556 -- used is defined in the same scope and has the same signature. This
8557 -- signature must be set explicitly since in an instance it may not have
8558 -- the same visibility as in the generic unit. This avoids duplicating
8559 -- or factoring the complex code for record/array equality tests etc.
8563 Loc
: constant Source_Ptr
:= Sloc
(N
);
8565 Ne
: constant Entity_Id
:= Entity
(N
);
8568 Binary_Op_Validity_Checks
(N
);
8574 Left_Opnd
=> Left_Opnd
(N
),
8575 Right_Opnd
=> Right_Opnd
(N
)));
8576 Set_Paren_Count
(Right_Opnd
(Neg
), 1);
8578 if Scope
(Ne
) /= Standard_Standard
then
8579 Set_Entity
(Right_Opnd
(Neg
), Corresponding_Equality
(Ne
));
8582 -- For navigation purposes, we want to treat the inequality as an
8583 -- implicit reference to the corresponding equality. Preserve the
8584 -- Comes_From_ source flag to generate proper Xref entries.
8586 Preserve_Comes_From_Source
(Neg
, N
);
8587 Preserve_Comes_From_Source
(Right_Opnd
(Neg
), N
);
8589 Analyze_And_Resolve
(N
, Standard_Boolean
);
8593 Optimize_Length_Comparison
(N
);
8596 ---------------------
8597 -- Expand_N_Op_Not --
8598 ---------------------
8600 -- If the argument is other than a Boolean array type, there is no special
8601 -- expansion required, except for dealing with validity checks, and non-
8602 -- standard boolean representations.
8604 -- For the packed array case, we call the special routine in Exp_Pakd,
8605 -- except that if the component size is greater than one, we use the
8606 -- standard routine generating a gruesome loop (it is so peculiar to have
8607 -- packed arrays with non-standard Boolean representations anyway, so it
8608 -- does not matter that we do not handle this case efficiently).
8610 -- For the unpacked array case (and for the special packed case where we
8611 -- have non standard Booleans, as discussed above), we generate and insert
8612 -- into the tree the following function definition:
8614 -- function Nnnn (A : arr) is
8617 -- for J in a'range loop
8618 -- B (J) := not A (J);
8623 -- Here arr is the actual subtype of the parameter (and hence always
8624 -- constrained). Then we replace the not with a call to this function.
8626 procedure Expand_N_Op_Not
(N
: Node_Id
) is
8627 Loc
: constant Source_Ptr
:= Sloc
(N
);
8628 Typ
: constant Entity_Id
:= Etype
(N
);
8637 Func_Name
: Entity_Id
;
8638 Loop_Statement
: Node_Id
;
8641 Unary_Op_Validity_Checks
(N
);
8643 -- For boolean operand, deal with non-standard booleans
8645 if Is_Boolean_Type
(Typ
) then
8646 Adjust_Condition
(Right_Opnd
(N
));
8647 Set_Etype
(N
, Standard_Boolean
);
8648 Adjust_Result_Type
(N
, Typ
);
8652 -- Only array types need any other processing
8654 if not Is_Array_Type
(Typ
) then
8658 -- Case of array operand. If bit packed with a component size of 1,
8659 -- handle it in Exp_Pakd if the operand is known to be aligned.
8661 if Is_Bit_Packed_Array
(Typ
)
8662 and then Component_Size
(Typ
) = 1
8663 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
8665 Expand_Packed_Not
(N
);
8669 -- Case of array operand which is not bit-packed. If the context is
8670 -- a safe assignment, call in-place operation, If context is a larger
8671 -- boolean expression in the context of a safe assignment, expansion is
8672 -- done by enclosing operation.
8674 Opnd
:= Relocate_Node
(Right_Opnd
(N
));
8675 Convert_To_Actual_Subtype
(Opnd
);
8676 Arr
:= Etype
(Opnd
);
8677 Ensure_Defined
(Arr
, N
);
8678 Silly_Boolean_Array_Not_Test
(N
, Arr
);
8680 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
8681 if Safe_In_Place_Array_Op
(Name
(Parent
(N
)), N
, Empty
) then
8682 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
8685 -- Special case the negation of a binary operation
8687 elsif Nkind_In
(Opnd
, N_Op_And
, N_Op_Or
, N_Op_Xor
)
8688 and then Safe_In_Place_Array_Op
8689 (Name
(Parent
(N
)), Left_Opnd
(Opnd
), Right_Opnd
(Opnd
))
8691 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
8695 elsif Nkind
(Parent
(N
)) in N_Binary_Op
8696 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
8699 Op1
: constant Node_Id
:= Left_Opnd
(Parent
(N
));
8700 Op2
: constant Node_Id
:= Right_Opnd
(Parent
(N
));
8701 Lhs
: constant Node_Id
:= Name
(Parent
(Parent
(N
)));
8704 if Safe_In_Place_Array_Op
(Lhs
, Op1
, Op2
) then
8706 -- (not A) op (not B) can be reduced to a single call
8708 if N
= Op1
and then Nkind
(Op2
) = N_Op_Not
then
8711 elsif N
= Op2
and then Nkind
(Op1
) = N_Op_Not
then
8714 -- A xor (not B) can also be special-cased
8716 elsif N
= Op2
and then Nkind
(Parent
(N
)) = N_Op_Xor
then
8723 A
:= Make_Defining_Identifier
(Loc
, Name_uA
);
8724 B
:= Make_Defining_Identifier
(Loc
, Name_uB
);
8725 J
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
8728 Make_Indexed_Component
(Loc
,
8729 Prefix
=> New_Occurrence_Of
(A
, Loc
),
8730 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
8733 Make_Indexed_Component
(Loc
,
8734 Prefix
=> New_Occurrence_Of
(B
, Loc
),
8735 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
8738 Make_Implicit_Loop_Statement
(N
,
8739 Identifier
=> Empty
,
8742 Make_Iteration_Scheme
(Loc
,
8743 Loop_Parameter_Specification
=>
8744 Make_Loop_Parameter_Specification
(Loc
,
8745 Defining_Identifier
=> J
,
8746 Discrete_Subtype_Definition
=>
8747 Make_Attribute_Reference
(Loc
,
8748 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
8749 Attribute_Name
=> Name_Range
))),
8751 Statements
=> New_List
(
8752 Make_Assignment_Statement
(Loc
,
8754 Expression
=> Make_Op_Not
(Loc
, A_J
))));
8756 Func_Name
:= Make_Temporary
(Loc
, 'N');
8757 Set_Is_Inlined
(Func_Name
);
8760 Make_Subprogram_Body
(Loc
,
8762 Make_Function_Specification
(Loc
,
8763 Defining_Unit_Name
=> Func_Name
,
8764 Parameter_Specifications
=> New_List
(
8765 Make_Parameter_Specification
(Loc
,
8766 Defining_Identifier
=> A
,
8767 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
))),
8768 Result_Definition
=> New_Occurrence_Of
(Typ
, Loc
)),
8770 Declarations
=> New_List
(
8771 Make_Object_Declaration
(Loc
,
8772 Defining_Identifier
=> B
,
8773 Object_Definition
=> New_Occurrence_Of
(Arr
, Loc
))),
8775 Handled_Statement_Sequence
=>
8776 Make_Handled_Sequence_Of_Statements
(Loc
,
8777 Statements
=> New_List
(
8779 Make_Simple_Return_Statement
(Loc
,
8780 Expression
=> Make_Identifier
(Loc
, Chars
(B
)))))));
8783 Make_Function_Call
(Loc
,
8784 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
8785 Parameter_Associations
=> New_List
(Opnd
)));
8787 Analyze_And_Resolve
(N
, Typ
);
8788 end Expand_N_Op_Not
;
8790 --------------------
8791 -- Expand_N_Op_Or --
8792 --------------------
8794 procedure Expand_N_Op_Or
(N
: Node_Id
) is
8795 Typ
: constant Entity_Id
:= Etype
(N
);
8798 Binary_Op_Validity_Checks
(N
);
8800 if Is_Array_Type
(Etype
(N
)) then
8801 Expand_Boolean_Operator
(N
);
8803 elsif Is_Boolean_Type
(Etype
(N
)) then
8804 Adjust_Condition
(Left_Opnd
(N
));
8805 Adjust_Condition
(Right_Opnd
(N
));
8806 Set_Etype
(N
, Standard_Boolean
);
8807 Adjust_Result_Type
(N
, Typ
);
8809 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
8810 Expand_Intrinsic_Call
(N
, Entity
(N
));
8815 ----------------------
8816 -- Expand_N_Op_Plus --
8817 ----------------------
8819 procedure Expand_N_Op_Plus
(N
: Node_Id
) is
8821 Unary_Op_Validity_Checks
(N
);
8823 -- Check for MINIMIZED/ELIMINATED overflow mode
8825 if Minimized_Eliminated_Overflow_Check
(N
) then
8826 Apply_Arithmetic_Overflow_Check
(N
);
8829 end Expand_N_Op_Plus
;
8831 ---------------------
8832 -- Expand_N_Op_Rem --
8833 ---------------------
8835 procedure Expand_N_Op_Rem
(N
: Node_Id
) is
8836 Loc
: constant Source_Ptr
:= Sloc
(N
);
8837 Typ
: constant Entity_Id
:= Etype
(N
);
8848 -- Set if corresponding operand can be negative
8850 pragma Unreferenced
(Hi
);
8853 Binary_Op_Validity_Checks
(N
);
8855 -- Check for MINIMIZED/ELIMINATED overflow mode
8857 if Minimized_Eliminated_Overflow_Check
(N
) then
8858 Apply_Arithmetic_Overflow_Check
(N
);
8862 if Is_Integer_Type
(Etype
(N
)) then
8863 Apply_Divide_Checks
(N
);
8865 -- All done if we don't have a REM any more, which can happen as a
8866 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8868 if Nkind
(N
) /= N_Op_Rem
then
8873 -- Proceed with expansion of REM
8875 Left
:= Left_Opnd
(N
);
8876 Right
:= Right_Opnd
(N
);
8878 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
8879 -- but it is useful with other back ends (e.g. AAMP), and is certainly
8882 if Is_Integer_Type
(Etype
(N
))
8883 and then Compile_Time_Known_Value
(Right
)
8884 and then Expr_Value
(Right
) = Uint_1
8886 -- Call Remove_Side_Effects to ensure that any side effects in the
8887 -- ignored left operand (in particular function calls to user defined
8888 -- functions) are properly executed.
8890 Remove_Side_Effects
(Left
);
8892 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
8893 Analyze_And_Resolve
(N
, Typ
);
8897 -- Deal with annoying case of largest negative number remainder minus
8898 -- one. Gigi may not handle this case correctly, because on some
8899 -- targets, the mod value is computed using a divide instruction
8900 -- which gives an overflow trap for this case.
8902 -- It would be a bit more efficient to figure out which targets this
8903 -- is really needed for, but in practice it is reasonable to do the
8904 -- following special check in all cases, since it means we get a clearer
8905 -- message, and also the overhead is minimal given that division is
8906 -- expensive in any case.
8908 -- In fact the check is quite easy, if the right operand is -1, then
8909 -- the remainder is always 0, and we can just ignore the left operand
8910 -- completely in this case.
8912 Determine_Range
(Right
, OK
, Lo
, Hi
, Assume_Valid
=> True);
8913 Lneg
:= (not OK
) or else Lo
< 0;
8915 Determine_Range
(Left
, OK
, Lo
, Hi
, Assume_Valid
=> True);
8916 Rneg
:= (not OK
) or else Lo
< 0;
8918 -- We won't mess with trying to find out if the left operand can really
8919 -- be the largest negative number (that's a pain in the case of private
8920 -- types and this is really marginal). We will just assume that we need
8921 -- the test if the left operand can be negative at all.
8923 if Lneg
and Rneg
then
8925 Make_If_Expression
(Loc
,
8926 Expressions
=> New_List
(
8928 Left_Opnd
=> Duplicate_Subexpr
(Right
),
8930 Unchecked_Convert_To
(Typ
, Make_Integer_Literal
(Loc
, -1))),
8932 Unchecked_Convert_To
(Typ
,
8933 Make_Integer_Literal
(Loc
, Uint_0
)),
8935 Relocate_Node
(N
))));
8937 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
8938 Analyze_And_Resolve
(N
, Typ
);
8940 end Expand_N_Op_Rem
;
8942 -----------------------------
8943 -- Expand_N_Op_Rotate_Left --
8944 -----------------------------
8946 procedure Expand_N_Op_Rotate_Left
(N
: Node_Id
) is
8948 Binary_Op_Validity_Checks
(N
);
8950 -- If we are in Modify_Tree_For_C mode, there is no rotate left in C,
8951 -- so we rewrite in terms of logical shifts
8953 -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits)
8955 -- where Bits is the shift count mod Esize (the mod operation here
8956 -- deals with ludicrous large shift counts, which are apparently OK).
8958 -- What about non-binary modulus ???
8961 Loc
: constant Source_Ptr
:= Sloc
(N
);
8962 Rtp
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
8963 Typ
: constant Entity_Id
:= Etype
(N
);
8966 if Modify_Tree_For_C
then
8967 Rewrite
(Right_Opnd
(N
),
8969 Left_Opnd
=> Relocate_Node
(Right_Opnd
(N
)),
8970 Right_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
))));
8972 Analyze_And_Resolve
(Right_Opnd
(N
), Rtp
);
8977 Make_Op_Shift_Left
(Loc
,
8978 Left_Opnd
=> Left_Opnd
(N
),
8979 Right_Opnd
=> Right_Opnd
(N
)),
8982 Make_Op_Shift_Right
(Loc
,
8983 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left_Opnd
(N
)),
8985 Make_Op_Subtract
(Loc
,
8986 Left_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
)),
8988 Duplicate_Subexpr_No_Checks
(Right_Opnd
(N
))))));
8990 Analyze_And_Resolve
(N
, Typ
);
8993 end Expand_N_Op_Rotate_Left
;
8995 ------------------------------
8996 -- Expand_N_Op_Rotate_Right --
8997 ------------------------------
8999 procedure Expand_N_Op_Rotate_Right
(N
: Node_Id
) is
9001 Binary_Op_Validity_Checks
(N
);
9003 -- If we are in Modify_Tree_For_C mode, there is no rotate right in C,
9004 -- so we rewrite in terms of logical shifts
9006 -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits)
9008 -- where Bits is the shift count mod Esize (the mod operation here
9009 -- deals with ludicrous large shift counts, which are apparently OK).
9011 -- What about non-binary modulus ???
9014 Loc
: constant Source_Ptr
:= Sloc
(N
);
9015 Rtp
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
9016 Typ
: constant Entity_Id
:= Etype
(N
);
9019 Rewrite
(Right_Opnd
(N
),
9021 Left_Opnd
=> Relocate_Node
(Right_Opnd
(N
)),
9022 Right_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
))));
9024 Analyze_And_Resolve
(Right_Opnd
(N
), Rtp
);
9026 if Modify_Tree_For_C
then
9030 Make_Op_Shift_Right
(Loc
,
9031 Left_Opnd
=> Left_Opnd
(N
),
9032 Right_Opnd
=> Right_Opnd
(N
)),
9035 Make_Op_Shift_Left
(Loc
,
9036 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left_Opnd
(N
)),
9038 Make_Op_Subtract
(Loc
,
9039 Left_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
)),
9041 Duplicate_Subexpr_No_Checks
(Right_Opnd
(N
))))));
9043 Analyze_And_Resolve
(N
, Typ
);
9046 end Expand_N_Op_Rotate_Right
;
9048 ----------------------------
9049 -- Expand_N_Op_Shift_Left --
9050 ----------------------------
9052 -- Note: nothing in this routine depends on left as opposed to right shifts
9053 -- so we share the routine for expanding shift right operations.
9055 procedure Expand_N_Op_Shift_Left
(N
: Node_Id
) is
9057 Binary_Op_Validity_Checks
(N
);
9059 -- If we are in Modify_Tree_For_C mode, then ensure that the right
9060 -- operand is not greater than the word size (since that would not
9061 -- be defined properly by the corresponding C shift operator).
9063 if Modify_Tree_For_C
then
9065 Right
: constant Node_Id
:= Right_Opnd
(N
);
9066 Loc
: constant Source_Ptr
:= Sloc
(Right
);
9067 Typ
: constant Entity_Id
:= Etype
(N
);
9068 Siz
: constant Uint
:= Esize
(Typ
);
9075 if Compile_Time_Known_Value
(Right
) then
9076 if Expr_Value
(Right
) >= Siz
then
9077 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
9078 Analyze_And_Resolve
(N
, Typ
);
9081 -- Not compile time known, find range
9084 Determine_Range
(Right
, OK
, Lo
, Hi
, Assume_Valid
=> True);
9086 -- Nothing to do if known to be OK range, otherwise expand
9088 if not OK
or else Hi
>= Siz
then
9090 -- Prevent recursion on copy of shift node
9092 Orig
:= Relocate_Node
(N
);
9093 Set_Analyzed
(Orig
);
9095 -- Now do the rewrite
9098 Make_If_Expression
(Loc
,
9099 Expressions
=> New_List
(
9101 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Right
),
9102 Right_Opnd
=> Make_Integer_Literal
(Loc
, Siz
)),
9103 Make_Integer_Literal
(Loc
, 0),
9105 Analyze_And_Resolve
(N
, Typ
);
9110 end Expand_N_Op_Shift_Left
;
9112 -----------------------------
9113 -- Expand_N_Op_Shift_Right --
9114 -----------------------------
9116 procedure Expand_N_Op_Shift_Right
(N
: Node_Id
) is
9118 -- Share shift left circuit
9120 Expand_N_Op_Shift_Left
(N
);
9121 end Expand_N_Op_Shift_Right
;
9123 ----------------------------------------
9124 -- Expand_N_Op_Shift_Right_Arithmetic --
9125 ----------------------------------------
9127 procedure Expand_N_Op_Shift_Right_Arithmetic
(N
: Node_Id
) is
9129 Binary_Op_Validity_Checks
(N
);
9131 -- If we are in Modify_Tree_For_C mode, there is no shift right
9132 -- arithmetic in C, so we rewrite in terms of logical shifts.
9134 -- Shift_Right (Num, Bits) or
9136 -- then not (Shift_Right (Mask, bits))
9139 -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1)
9141 -- Note: in almost all C compilers it would work to just shift a
9142 -- signed integer right, but it's undefined and we cannot rely on it.
9144 -- Note: the above works fine for shift counts greater than or equal
9145 -- to the word size, since in this case (not (Shift_Right (Mask, bits)))
9146 -- generates all 1'bits.
9148 -- What about non-binary modulus ???
9151 Loc
: constant Source_Ptr
:= Sloc
(N
);
9152 Typ
: constant Entity_Id
:= Etype
(N
);
9153 Sign
: constant Uint
:= 2 ** (Esize
(Typ
) - 1);
9154 Mask
: constant Uint
:= (2 ** Esize
(Typ
)) - 1;
9155 Left
: constant Node_Id
:= Left_Opnd
(N
);
9156 Right
: constant Node_Id
:= Right_Opnd
(N
);
9160 if Modify_Tree_For_C
then
9162 -- Here if not (Shift_Right (Mask, bits)) can be computed at
9163 -- compile time as a single constant.
9165 if Compile_Time_Known_Value
(Right
) then
9167 Val
: constant Uint
:= Expr_Value
(Right
);
9170 if Val
>= Esize
(Typ
) then
9171 Maskx
:= Make_Integer_Literal
(Loc
, Mask
);
9175 Make_Integer_Literal
(Loc
,
9176 Intval
=> Mask
- (Mask
/ (2 ** Expr_Value
(Right
))));
9184 Make_Op_Shift_Right
(Loc
,
9185 Left_Opnd
=> Make_Integer_Literal
(Loc
, Mask
),
9186 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Right
)));
9189 -- Now do the rewrite
9194 Make_Op_Shift_Right
(Loc
,
9196 Right_Opnd
=> Right
),
9198 Make_If_Expression
(Loc
,
9199 Expressions
=> New_List
(
9201 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left
),
9202 Right_Opnd
=> Make_Integer_Literal
(Loc
, Sign
)),
9204 Make_Integer_Literal
(Loc
, 0)))));
9205 Analyze_And_Resolve
(N
, Typ
);
9208 end Expand_N_Op_Shift_Right_Arithmetic
;
9210 --------------------------
9211 -- Expand_N_Op_Subtract --
9212 --------------------------
9214 procedure Expand_N_Op_Subtract
(N
: Node_Id
) is
9215 Typ
: constant Entity_Id
:= Etype
(N
);
9218 Binary_Op_Validity_Checks
(N
);
9220 -- Check for MINIMIZED/ELIMINATED overflow mode
9222 if Minimized_Eliminated_Overflow_Check
(N
) then
9223 Apply_Arithmetic_Overflow_Check
(N
);
9227 -- N - 0 = N for integer types
9229 if Is_Integer_Type
(Typ
)
9230 and then Compile_Time_Known_Value
(Right_Opnd
(N
))
9231 and then Expr_Value
(Right_Opnd
(N
)) = 0
9233 Rewrite
(N
, Left_Opnd
(N
));
9237 -- Arithmetic overflow checks for signed integer/fixed point types
9239 if Is_Signed_Integer_Type
(Typ
) or else Is_Fixed_Point_Type
(Typ
) then
9240 Apply_Arithmetic_Overflow_Check
(N
);
9243 -- Overflow checks for floating-point if -gnateF mode active
9245 Check_Float_Op_Overflow
(N
);
9246 end Expand_N_Op_Subtract
;
9248 ---------------------
9249 -- Expand_N_Op_Xor --
9250 ---------------------
9252 procedure Expand_N_Op_Xor
(N
: Node_Id
) is
9253 Typ
: constant Entity_Id
:= Etype
(N
);
9256 Binary_Op_Validity_Checks
(N
);
9258 if Is_Array_Type
(Etype
(N
)) then
9259 Expand_Boolean_Operator
(N
);
9261 elsif Is_Boolean_Type
(Etype
(N
)) then
9262 Adjust_Condition
(Left_Opnd
(N
));
9263 Adjust_Condition
(Right_Opnd
(N
));
9264 Set_Etype
(N
, Standard_Boolean
);
9265 Adjust_Result_Type
(N
, Typ
);
9267 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
9268 Expand_Intrinsic_Call
(N
, Entity
(N
));
9271 end Expand_N_Op_Xor
;
9273 ----------------------
9274 -- Expand_N_Or_Else --
9275 ----------------------
9277 procedure Expand_N_Or_Else
(N
: Node_Id
)
9278 renames Expand_Short_Circuit_Operator
;
9280 -----------------------------------
9281 -- Expand_N_Qualified_Expression --
9282 -----------------------------------
9284 procedure Expand_N_Qualified_Expression
(N
: Node_Id
) is
9285 Operand
: constant Node_Id
:= Expression
(N
);
9286 Target_Type
: constant Entity_Id
:= Entity
(Subtype_Mark
(N
));
9289 -- Do validity check if validity checking operands
9291 if Validity_Checks_On
and Validity_Check_Operands
then
9292 Ensure_Valid
(Operand
);
9295 -- Apply possible constraint check
9297 Apply_Constraint_Check
(Operand
, Target_Type
, No_Sliding
=> True);
9299 if Do_Range_Check
(Operand
) then
9300 Set_Do_Range_Check
(Operand
, False);
9301 Generate_Range_Check
(Operand
, Target_Type
, CE_Range_Check_Failed
);
9303 end Expand_N_Qualified_Expression
;
9305 ------------------------------------
9306 -- Expand_N_Quantified_Expression --
9307 ------------------------------------
9311 -- for all X in range => Cond
9316 -- for X in range loop
9323 -- Similarly, an existentially quantified expression:
9325 -- for some X in range => Cond
9330 -- for X in range loop
9337 -- In both cases, the iteration may be over a container in which case it is
9338 -- given by an iterator specification, not a loop parameter specification.
9340 procedure Expand_N_Quantified_Expression
(N
: Node_Id
) is
9341 Actions
: constant List_Id
:= New_List
;
9342 For_All
: constant Boolean := All_Present
(N
);
9343 Iter_Spec
: constant Node_Id
:= Iterator_Specification
(N
);
9344 Loc
: constant Source_Ptr
:= Sloc
(N
);
9345 Loop_Spec
: constant Node_Id
:= Loop_Parameter_Specification
(N
);
9352 -- Create the declaration of the flag which tracks the status of the
9353 -- quantified expression. Generate:
9355 -- Flag : Boolean := (True | False);
9357 Flag
:= Make_Temporary
(Loc
, 'T', N
);
9360 Make_Object_Declaration
(Loc
,
9361 Defining_Identifier
=> Flag
,
9362 Object_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
),
9364 New_Occurrence_Of
(Boolean_Literals
(For_All
), Loc
)));
9366 -- Construct the circuitry which tracks the status of the quantified
9367 -- expression. Generate:
9369 -- if [not] Cond then
9370 -- Flag := (False | True);
9374 Cond
:= Relocate_Node
(Condition
(N
));
9377 Cond
:= Make_Op_Not
(Loc
, Cond
);
9381 Make_Implicit_If_Statement
(N
,
9383 Then_Statements
=> New_List
(
9384 Make_Assignment_Statement
(Loc
,
9385 Name
=> New_Occurrence_Of
(Flag
, Loc
),
9387 New_Occurrence_Of
(Boolean_Literals
(not For_All
), Loc
)),
9388 Make_Exit_Statement
(Loc
))));
9390 -- Build the loop equivalent of the quantified expression
9392 if Present
(Iter_Spec
) then
9394 Make_Iteration_Scheme
(Loc
,
9395 Iterator_Specification
=> Iter_Spec
);
9398 Make_Iteration_Scheme
(Loc
,
9399 Loop_Parameter_Specification
=> Loop_Spec
);
9403 Make_Loop_Statement
(Loc
,
9404 Iteration_Scheme
=> Scheme
,
9405 Statements
=> Stmts
,
9406 End_Label
=> Empty
));
9408 -- Transform the quantified expression
9411 Make_Expression_With_Actions
(Loc
,
9412 Expression
=> New_Occurrence_Of
(Flag
, Loc
),
9413 Actions
=> Actions
));
9414 Analyze_And_Resolve
(N
, Standard_Boolean
);
9415 end Expand_N_Quantified_Expression
;
9417 ---------------------------------
9418 -- Expand_N_Selected_Component --
9419 ---------------------------------
9421 procedure Expand_N_Selected_Component
(N
: Node_Id
) is
9422 Loc
: constant Source_Ptr
:= Sloc
(N
);
9423 Par
: constant Node_Id
:= Parent
(N
);
9424 P
: constant Node_Id
:= Prefix
(N
);
9425 S
: constant Node_Id
:= Selector_Name
(N
);
9426 Ptyp
: Entity_Id
:= Underlying_Type
(Etype
(P
));
9432 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean;
9433 -- Gigi needs a temporary for prefixes that depend on a discriminant,
9434 -- unless the context of an assignment can provide size information.
9435 -- Don't we have a general routine that does this???
9437 function Is_Subtype_Declaration
return Boolean;
9438 -- The replacement of a discriminant reference by its value is required
9439 -- if this is part of the initialization of an temporary generated by a
9440 -- change of representation. This shows up as the construction of a
9441 -- discriminant constraint for a subtype declared at the same point as
9442 -- the entity in the prefix of the selected component. We recognize this
9443 -- case when the context of the reference is:
9444 -- subtype ST is T(Obj.D);
9445 -- where the entity for Obj comes from source, and ST has the same sloc.
9447 -----------------------
9448 -- In_Left_Hand_Side --
9449 -----------------------
9451 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean is
9453 return (Nkind
(Parent
(Comp
)) = N_Assignment_Statement
9454 and then Comp
= Name
(Parent
(Comp
)))
9455 or else (Present
(Parent
(Comp
))
9456 and then Nkind
(Parent
(Comp
)) in N_Subexpr
9457 and then In_Left_Hand_Side
(Parent
(Comp
)));
9458 end In_Left_Hand_Side
;
9460 -----------------------------
9461 -- Is_Subtype_Declaration --
9462 -----------------------------
9464 function Is_Subtype_Declaration
return Boolean is
9465 Par
: constant Node_Id
:= Parent
(N
);
9468 Nkind
(Par
) = N_Index_Or_Discriminant_Constraint
9469 and then Nkind
(Parent
(Parent
(Par
))) = N_Subtype_Declaration
9470 and then Comes_From_Source
(Entity
(Prefix
(N
)))
9471 and then Sloc
(Par
) = Sloc
(Entity
(Prefix
(N
)));
9472 end Is_Subtype_Declaration
;
9474 -- Start of processing for Expand_N_Selected_Component
9477 -- Insert explicit dereference if required
9479 if Is_Access_Type
(Ptyp
) then
9481 -- First set prefix type to proper access type, in case it currently
9482 -- has a private (non-access) view of this type.
9484 Set_Etype
(P
, Ptyp
);
9486 Insert_Explicit_Dereference
(P
);
9487 Analyze_And_Resolve
(P
, Designated_Type
(Ptyp
));
9489 if Ekind
(Etype
(P
)) = E_Private_Subtype
9490 and then Is_For_Access_Subtype
(Etype
(P
))
9492 Set_Etype
(P
, Base_Type
(Etype
(P
)));
9498 -- Deal with discriminant check required
9500 if Do_Discriminant_Check
(N
) then
9501 if Present
(Discriminant_Checking_Func
9502 (Original_Record_Component
(Entity
(S
))))
9504 -- Present the discriminant checking function to the backend, so
9505 -- that it can inline the call to the function.
9508 (Discriminant_Checking_Func
9509 (Original_Record_Component
(Entity
(S
))));
9511 -- Now reset the flag and generate the call
9513 Set_Do_Discriminant_Check
(N
, False);
9514 Generate_Discriminant_Check
(N
);
9516 -- In the case of Unchecked_Union, no discriminant checking is
9517 -- actually performed.
9520 Set_Do_Discriminant_Check
(N
, False);
9524 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9525 -- function, then additional actuals must be passed.
9527 if Ada_Version
>= Ada_2005
9528 and then Is_Build_In_Place_Function_Call
(P
)
9530 Make_Build_In_Place_Call_In_Anonymous_Context
(P
);
9533 -- Gigi cannot handle unchecked conversions that are the prefix of a
9534 -- selected component with discriminants. This must be checked during
9535 -- expansion, because during analysis the type of the selector is not
9536 -- known at the point the prefix is analyzed. If the conversion is the
9537 -- target of an assignment, then we cannot force the evaluation.
9539 if Nkind
(Prefix
(N
)) = N_Unchecked_Type_Conversion
9540 and then Has_Discriminants
(Etype
(N
))
9541 and then not In_Left_Hand_Side
(N
)
9543 Force_Evaluation
(Prefix
(N
));
9546 -- Remaining processing applies only if selector is a discriminant
9548 if Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
then
9550 -- If the selector is a discriminant of a constrained record type,
9551 -- we may be able to rewrite the expression with the actual value
9552 -- of the discriminant, a useful optimization in some cases.
9554 if Is_Record_Type
(Ptyp
)
9555 and then Has_Discriminants
(Ptyp
)
9556 and then Is_Constrained
(Ptyp
)
9558 -- Do this optimization for discrete types only, and not for
9559 -- access types (access discriminants get us into trouble).
9561 if not Is_Discrete_Type
(Etype
(N
)) then
9564 -- Don't do this on the left hand of an assignment statement.
9565 -- Normally one would think that references like this would not
9566 -- occur, but they do in generated code, and mean that we really
9567 -- do want to assign the discriminant.
9569 elsif Nkind
(Par
) = N_Assignment_Statement
9570 and then Name
(Par
) = N
9574 -- Don't do this optimization for the prefix of an attribute or
9575 -- the name of an object renaming declaration since these are
9576 -- contexts where we do not want the value anyway.
9578 elsif (Nkind
(Par
) = N_Attribute_Reference
9579 and then Prefix
(Par
) = N
)
9580 or else Is_Renamed_Object
(N
)
9584 -- Don't do this optimization if we are within the code for a
9585 -- discriminant check, since the whole point of such a check may
9586 -- be to verify the condition on which the code below depends.
9588 elsif Is_In_Discriminant_Check
(N
) then
9591 -- Green light to see if we can do the optimization. There is
9592 -- still one condition that inhibits the optimization below but
9593 -- now is the time to check the particular discriminant.
9596 -- Loop through discriminants to find the matching discriminant
9597 -- constraint to see if we can copy it.
9599 Disc
:= First_Discriminant
(Ptyp
);
9600 Dcon
:= First_Elmt
(Discriminant_Constraint
(Ptyp
));
9601 Discr_Loop
: while Present
(Dcon
) loop
9602 Dval
:= Node
(Dcon
);
9604 -- Check if this is the matching discriminant and if the
9605 -- discriminant value is simple enough to make sense to
9606 -- copy. We don't want to copy complex expressions, and
9607 -- indeed to do so can cause trouble (before we put in
9608 -- this guard, a discriminant expression containing an
9609 -- AND THEN was copied, causing problems for coverage
9612 -- However, if the reference is part of the initialization
9613 -- code generated for an object declaration, we must use
9614 -- the discriminant value from the subtype constraint,
9615 -- because the selected component may be a reference to the
9616 -- object being initialized, whose discriminant is not yet
9617 -- set. This only happens in complex cases involving changes
9618 -- or representation.
9620 if Disc
= Entity
(Selector_Name
(N
))
9621 and then (Is_Entity_Name
(Dval
)
9622 or else Compile_Time_Known_Value
(Dval
)
9623 or else Is_Subtype_Declaration
)
9625 -- Here we have the matching discriminant. Check for
9626 -- the case of a discriminant of a component that is
9627 -- constrained by an outer discriminant, which cannot
9628 -- be optimized away.
9630 if Denotes_Discriminant
9631 (Dval
, Check_Concurrent
=> True)
9635 elsif Nkind
(Original_Node
(Dval
)) = N_Selected_Component
9637 Denotes_Discriminant
9638 (Selector_Name
(Original_Node
(Dval
)), True)
9642 -- Do not retrieve value if constraint is not static. It
9643 -- is generally not useful, and the constraint may be a
9644 -- rewritten outer discriminant in which case it is in
9647 elsif Is_Entity_Name
(Dval
)
9649 Nkind
(Parent
(Entity
(Dval
))) = N_Object_Declaration
9650 and then Present
(Expression
(Parent
(Entity
(Dval
))))
9652 Is_OK_Static_Expression
9653 (Expression
(Parent
(Entity
(Dval
))))
9657 -- In the context of a case statement, the expression may
9658 -- have the base type of the discriminant, and we need to
9659 -- preserve the constraint to avoid spurious errors on
9662 elsif Nkind
(Parent
(N
)) = N_Case_Statement
9663 and then Etype
(Dval
) /= Etype
(Disc
)
9666 Make_Qualified_Expression
(Loc
,
9668 New_Occurrence_Of
(Etype
(Disc
), Loc
),
9670 New_Copy_Tree
(Dval
)));
9671 Analyze_And_Resolve
(N
, Etype
(Disc
));
9673 -- In case that comes out as a static expression,
9674 -- reset it (a selected component is never static).
9676 Set_Is_Static_Expression
(N
, False);
9679 -- Otherwise we can just copy the constraint, but the
9680 -- result is certainly not static. In some cases the
9681 -- discriminant constraint has been analyzed in the
9682 -- context of the original subtype indication, but for
9683 -- itypes the constraint might not have been analyzed
9684 -- yet, and this must be done now.
9687 Rewrite
(N
, New_Copy_Tree
(Dval
));
9688 Analyze_And_Resolve
(N
);
9689 Set_Is_Static_Expression
(N
, False);
9695 Next_Discriminant
(Disc
);
9696 end loop Discr_Loop
;
9698 -- Note: the above loop should always find a matching
9699 -- discriminant, but if it does not, we just missed an
9700 -- optimization due to some glitch (perhaps a previous
9701 -- error), so ignore.
9706 -- The only remaining processing is in the case of a discriminant of
9707 -- a concurrent object, where we rewrite the prefix to denote the
9708 -- corresponding record type. If the type is derived and has renamed
9709 -- discriminants, use corresponding discriminant, which is the one
9710 -- that appears in the corresponding record.
9712 if not Is_Concurrent_Type
(Ptyp
) then
9716 Disc
:= Entity
(Selector_Name
(N
));
9718 if Is_Derived_Type
(Ptyp
)
9719 and then Present
(Corresponding_Discriminant
(Disc
))
9721 Disc
:= Corresponding_Discriminant
(Disc
);
9725 Make_Selected_Component
(Loc
,
9727 Unchecked_Convert_To
(Corresponding_Record_Type
(Ptyp
),
9729 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Disc
)));
9735 -- Set Atomic_Sync_Required if necessary for atomic component
9737 if Nkind
(N
) = N_Selected_Component
then
9739 E
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
9743 -- If component is atomic, but type is not, setting depends on
9744 -- disable/enable state for the component.
9746 if Is_Atomic
(E
) and then not Is_Atomic
(Etype
(E
)) then
9747 Set
:= not Atomic_Synchronization_Disabled
(E
);
9749 -- If component is not atomic, but its type is atomic, setting
9750 -- depends on disable/enable state for the type.
9752 elsif not Is_Atomic
(E
) and then Is_Atomic
(Etype
(E
)) then
9753 Set
:= not Atomic_Synchronization_Disabled
(Etype
(E
));
9755 -- If both component and type are atomic, we disable if either
9756 -- component or its type have sync disabled.
9758 elsif Is_Atomic
(E
) and then Is_Atomic
(Etype
(E
)) then
9759 Set
:= (not Atomic_Synchronization_Disabled
(E
))
9761 (not Atomic_Synchronization_Disabled
(Etype
(E
)));
9767 -- Set flag if required
9770 Activate_Atomic_Synchronization
(N
);
9774 end Expand_N_Selected_Component
;
9776 --------------------
9777 -- Expand_N_Slice --
9778 --------------------
9780 procedure Expand_N_Slice
(N
: Node_Id
) is
9781 Loc
: constant Source_Ptr
:= Sloc
(N
);
9782 Typ
: constant Entity_Id
:= Etype
(N
);
9784 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean;
9785 -- Check whether the argument is an actual for a procedure call, in
9786 -- which case the expansion of a bit-packed slice is deferred until the
9787 -- call itself is expanded. The reason this is required is that we might
9788 -- have an IN OUT or OUT parameter, and the copy out is essential, and
9789 -- that copy out would be missed if we created a temporary here in
9790 -- Expand_N_Slice. Note that we don't bother to test specifically for an
9791 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
9792 -- is harmless to defer expansion in the IN case, since the call
9793 -- processing will still generate the appropriate copy in operation,
9794 -- which will take care of the slice.
9796 procedure Make_Temporary_For_Slice
;
9797 -- Create a named variable for the value of the slice, in cases where
9798 -- the back-end cannot handle it properly, e.g. when packed types or
9799 -- unaligned slices are involved.
9801 -------------------------
9802 -- Is_Procedure_Actual --
9803 -------------------------
9805 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean is
9806 Par
: Node_Id
:= Parent
(N
);
9810 -- If our parent is a procedure call we can return
9812 if Nkind
(Par
) = N_Procedure_Call_Statement
then
9815 -- If our parent is a type conversion, keep climbing the tree,
9816 -- since a type conversion can be a procedure actual. Also keep
9817 -- climbing if parameter association or a qualified expression,
9818 -- since these are additional cases that do can appear on
9819 -- procedure actuals.
9821 elsif Nkind_In
(Par
, N_Type_Conversion
,
9822 N_Parameter_Association
,
9823 N_Qualified_Expression
)
9825 Par
:= Parent
(Par
);
9827 -- Any other case is not what we are looking for
9833 end Is_Procedure_Actual
;
9835 ------------------------------
9836 -- Make_Temporary_For_Slice --
9837 ------------------------------
9839 procedure Make_Temporary_For_Slice
is
9840 Ent
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', N
);
9845 Make_Object_Declaration
(Loc
,
9846 Defining_Identifier
=> Ent
,
9847 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
9849 Set_No_Initialization
(Decl
);
9851 Insert_Actions
(N
, New_List
(
9853 Make_Assignment_Statement
(Loc
,
9854 Name
=> New_Occurrence_Of
(Ent
, Loc
),
9855 Expression
=> Relocate_Node
(N
))));
9857 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
9858 Analyze_And_Resolve
(N
, Typ
);
9859 end Make_Temporary_For_Slice
;
9863 Pref
: constant Node_Id
:= Prefix
(N
);
9864 Pref_Typ
: Entity_Id
:= Etype
(Pref
);
9866 -- Start of processing for Expand_N_Slice
9869 -- Special handling for access types
9871 if Is_Access_Type
(Pref_Typ
) then
9872 Pref_Typ
:= Designated_Type
(Pref_Typ
);
9875 Make_Explicit_Dereference
(Sloc
(N
),
9876 Prefix
=> Relocate_Node
(Pref
)));
9878 Analyze_And_Resolve
(Pref
, Pref_Typ
);
9881 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9882 -- function, then additional actuals must be passed.
9884 if Ada_Version
>= Ada_2005
9885 and then Is_Build_In_Place_Function_Call
(Pref
)
9887 Make_Build_In_Place_Call_In_Anonymous_Context
(Pref
);
9890 -- The remaining case to be handled is packed slices. We can leave
9891 -- packed slices as they are in the following situations:
9893 -- 1. Right or left side of an assignment (we can handle this
9894 -- situation correctly in the assignment statement expansion).
9896 -- 2. Prefix of indexed component (the slide is optimized away in this
9897 -- case, see the start of Expand_N_Slice.)
9899 -- 3. Object renaming declaration, since we want the name of the
9900 -- slice, not the value.
9902 -- 4. Argument to procedure call, since copy-in/copy-out handling may
9903 -- be required, and this is handled in the expansion of call
9906 -- 5. Prefix of an address attribute (this is an error which is caught
9907 -- elsewhere, and the expansion would interfere with generating the
9910 if not Is_Packed
(Typ
) then
9912 -- Apply transformation for actuals of a function call, where
9913 -- Expand_Actuals is not used.
9915 if Nkind
(Parent
(N
)) = N_Function_Call
9916 and then Is_Possibly_Unaligned_Slice
(N
)
9918 Make_Temporary_For_Slice
;
9921 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
9922 or else (Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
9923 and then Parent
(N
) = Name
(Parent
(Parent
(N
))))
9927 elsif Nkind
(Parent
(N
)) = N_Indexed_Component
9928 or else Is_Renamed_Object
(N
)
9929 or else Is_Procedure_Actual
(N
)
9933 elsif Nkind
(Parent
(N
)) = N_Attribute_Reference
9934 and then Attribute_Name
(Parent
(N
)) = Name_Address
9939 Make_Temporary_For_Slice
;
9943 ------------------------------
9944 -- Expand_N_Type_Conversion --
9945 ------------------------------
9947 procedure Expand_N_Type_Conversion
(N
: Node_Id
) is
9948 Loc
: constant Source_Ptr
:= Sloc
(N
);
9949 Operand
: constant Node_Id
:= Expression
(N
);
9950 Target_Type
: constant Entity_Id
:= Etype
(N
);
9951 Operand_Type
: Entity_Id
:= Etype
(Operand
);
9953 procedure Handle_Changed_Representation
;
9954 -- This is called in the case of record and array type conversions to
9955 -- see if there is a change of representation to be handled. Change of
9956 -- representation is actually handled at the assignment statement level,
9957 -- and what this procedure does is rewrite node N conversion as an
9958 -- assignment to temporary. If there is no change of representation,
9959 -- then the conversion node is unchanged.
9961 procedure Raise_Accessibility_Error
;
9962 -- Called when we know that an accessibility check will fail. Rewrites
9963 -- node N to an appropriate raise statement and outputs warning msgs.
9964 -- The Etype of the raise node is set to Target_Type.
9966 procedure Real_Range_Check
;
9967 -- Handles generation of range check for real target value
9969 function Has_Extra_Accessibility
(Id
: Entity_Id
) return Boolean;
9970 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
9971 -- evaluates to True.
9973 -----------------------------------
9974 -- Handle_Changed_Representation --
9975 -----------------------------------
9977 procedure Handle_Changed_Representation
is
9986 -- Nothing else to do if no change of representation
9988 if Same_Representation
(Operand_Type
, Target_Type
) then
9991 -- The real change of representation work is done by the assignment
9992 -- statement processing. So if this type conversion is appearing as
9993 -- the expression of an assignment statement, nothing needs to be
9994 -- done to the conversion.
9996 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
then
9999 -- Otherwise we need to generate a temporary variable, and do the
10000 -- change of representation assignment into that temporary variable.
10001 -- The conversion is then replaced by a reference to this variable.
10006 -- If type is unconstrained we have to add a constraint, copied
10007 -- from the actual value of the left hand side.
10009 if not Is_Constrained
(Target_Type
) then
10010 if Has_Discriminants
(Operand_Type
) then
10011 Disc
:= First_Discriminant
(Operand_Type
);
10013 if Disc
/= First_Stored_Discriminant
(Operand_Type
) then
10014 Disc
:= First_Stored_Discriminant
(Operand_Type
);
10018 while Present
(Disc
) loop
10020 Make_Selected_Component
(Loc
,
10022 Duplicate_Subexpr_Move_Checks
(Operand
),
10024 Make_Identifier
(Loc
, Chars
(Disc
))));
10025 Next_Discriminant
(Disc
);
10028 elsif Is_Array_Type
(Operand_Type
) then
10029 N_Ix
:= First_Index
(Target_Type
);
10032 for J
in 1 .. Number_Dimensions
(Operand_Type
) loop
10034 -- We convert the bounds explicitly. We use an unchecked
10035 -- conversion because bounds checks are done elsewhere.
10040 Unchecked_Convert_To
(Etype
(N_Ix
),
10041 Make_Attribute_Reference
(Loc
,
10043 Duplicate_Subexpr_No_Checks
10044 (Operand
, Name_Req
=> True),
10045 Attribute_Name
=> Name_First
,
10046 Expressions
=> New_List
(
10047 Make_Integer_Literal
(Loc
, J
)))),
10050 Unchecked_Convert_To
(Etype
(N_Ix
),
10051 Make_Attribute_Reference
(Loc
,
10053 Duplicate_Subexpr_No_Checks
10054 (Operand
, Name_Req
=> True),
10055 Attribute_Name
=> Name_Last
,
10056 Expressions
=> New_List
(
10057 Make_Integer_Literal
(Loc
, J
))))));
10064 Odef
:= New_Occurrence_Of
(Target_Type
, Loc
);
10066 if Present
(Cons
) then
10068 Make_Subtype_Indication
(Loc
,
10069 Subtype_Mark
=> Odef
,
10071 Make_Index_Or_Discriminant_Constraint
(Loc
,
10072 Constraints
=> Cons
));
10075 Temp
:= Make_Temporary
(Loc
, 'C');
10077 Make_Object_Declaration
(Loc
,
10078 Defining_Identifier
=> Temp
,
10079 Object_Definition
=> Odef
);
10081 Set_No_Initialization
(Decl
, True);
10083 -- Insert required actions. It is essential to suppress checks
10084 -- since we have suppressed default initialization, which means
10085 -- that the variable we create may have no discriminants.
10090 Make_Assignment_Statement
(Loc
,
10091 Name
=> New_Occurrence_Of
(Temp
, Loc
),
10092 Expression
=> Relocate_Node
(N
))),
10093 Suppress
=> All_Checks
);
10095 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
10098 end Handle_Changed_Representation
;
10100 -------------------------------
10101 -- Raise_Accessibility_Error --
10102 -------------------------------
10104 procedure Raise_Accessibility_Error
is
10106 Error_Msg_Warn
:= SPARK_Mode
/= On
;
10108 Make_Raise_Program_Error
(Sloc
(N
),
10109 Reason
=> PE_Accessibility_Check_Failed
));
10110 Set_Etype
(N
, Target_Type
);
10112 Error_Msg_N
("<<accessibility check failure", N
);
10113 Error_Msg_NE
("\<<& [", N
, Standard_Program_Error
);
10114 end Raise_Accessibility_Error
;
10116 ----------------------
10117 -- Real_Range_Check --
10118 ----------------------
10120 -- Case of conversions to floating-point or fixed-point. If range checks
10121 -- are enabled and the target type has a range constraint, we convert:
10127 -- Tnn : typ'Base := typ'Base (x);
10128 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
10131 -- This is necessary when there is a conversion of integer to float or
10132 -- to fixed-point to ensure that the correct checks are made. It is not
10133 -- necessary for float to float where it is enough to simply set the
10134 -- Do_Range_Check flag.
10136 procedure Real_Range_Check
is
10137 Btyp
: constant Entity_Id
:= Base_Type
(Target_Type
);
10138 Lo
: constant Node_Id
:= Type_Low_Bound
(Target_Type
);
10139 Hi
: constant Node_Id
:= Type_High_Bound
(Target_Type
);
10140 Xtyp
: constant Entity_Id
:= Etype
(Operand
);
10145 -- Nothing to do if conversion was rewritten
10147 if Nkind
(N
) /= N_Type_Conversion
then
10151 -- Nothing to do if range checks suppressed, or target has the same
10152 -- range as the base type (or is the base type).
10154 if Range_Checks_Suppressed
(Target_Type
)
10155 or else (Lo
= Type_Low_Bound
(Btyp
)
10157 Hi
= Type_High_Bound
(Btyp
))
10162 -- Nothing to do if expression is an entity on which checks have been
10165 if Is_Entity_Name
(Operand
)
10166 and then Range_Checks_Suppressed
(Entity
(Operand
))
10171 -- Nothing to do if bounds are all static and we can tell that the
10172 -- expression is within the bounds of the target. Note that if the
10173 -- operand is of an unconstrained floating-point type, then we do
10174 -- not trust it to be in range (might be infinite)
10177 S_Lo
: constant Node_Id
:= Type_Low_Bound
(Xtyp
);
10178 S_Hi
: constant Node_Id
:= Type_High_Bound
(Xtyp
);
10181 if (not Is_Floating_Point_Type
(Xtyp
)
10182 or else Is_Constrained
(Xtyp
))
10183 and then Compile_Time_Known_Value
(S_Lo
)
10184 and then Compile_Time_Known_Value
(S_Hi
)
10185 and then Compile_Time_Known_Value
(Hi
)
10186 and then Compile_Time_Known_Value
(Lo
)
10189 D_Lov
: constant Ureal
:= Expr_Value_R
(Lo
);
10190 D_Hiv
: constant Ureal
:= Expr_Value_R
(Hi
);
10195 if Is_Real_Type
(Xtyp
) then
10196 S_Lov
:= Expr_Value_R
(S_Lo
);
10197 S_Hiv
:= Expr_Value_R
(S_Hi
);
10199 S_Lov
:= UR_From_Uint
(Expr_Value
(S_Lo
));
10200 S_Hiv
:= UR_From_Uint
(Expr_Value
(S_Hi
));
10204 and then S_Lov
>= D_Lov
10205 and then S_Hiv
<= D_Hiv
10207 -- Unset the range check flag on the current value of
10208 -- Expression (N), since the captured Operand may have
10209 -- been rewritten (such as for the case of a conversion
10210 -- to a fixed-point type).
10212 Set_Do_Range_Check
(Expression
(N
), False);
10220 -- For float to float conversions, we are done
10222 if Is_Floating_Point_Type
(Xtyp
)
10224 Is_Floating_Point_Type
(Btyp
)
10229 -- Otherwise rewrite the conversion as described above
10231 Conv
:= Relocate_Node
(N
);
10232 Rewrite
(Subtype_Mark
(Conv
), New_Occurrence_Of
(Btyp
, Loc
));
10233 Set_Etype
(Conv
, Btyp
);
10235 -- Enable overflow except for case of integer to float conversions,
10236 -- where it is never required, since we can never have overflow in
10239 if not Is_Integer_Type
(Etype
(Operand
)) then
10240 Enable_Overflow_Check
(Conv
);
10243 Tnn
:= Make_Temporary
(Loc
, 'T', Conv
);
10245 Insert_Actions
(N
, New_List
(
10246 Make_Object_Declaration
(Loc
,
10247 Defining_Identifier
=> Tnn
,
10248 Object_Definition
=> New_Occurrence_Of
(Btyp
, Loc
),
10249 Constant_Present
=> True,
10250 Expression
=> Conv
),
10252 Make_Raise_Constraint_Error
(Loc
,
10257 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
10259 Make_Attribute_Reference
(Loc
,
10260 Attribute_Name
=> Name_First
,
10262 New_Occurrence_Of
(Target_Type
, Loc
))),
10266 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
10268 Make_Attribute_Reference
(Loc
,
10269 Attribute_Name
=> Name_Last
,
10271 New_Occurrence_Of
(Target_Type
, Loc
)))),
10272 Reason
=> CE_Range_Check_Failed
)));
10274 Rewrite
(N
, New_Occurrence_Of
(Tnn
, Loc
));
10275 Analyze_And_Resolve
(N
, Btyp
);
10276 end Real_Range_Check
;
10278 -----------------------------
10279 -- Has_Extra_Accessibility --
10280 -----------------------------
10282 -- Returns true for a formal of an anonymous access type or for
10283 -- an Ada 2012-style stand-alone object of an anonymous access type.
10285 function Has_Extra_Accessibility
(Id
: Entity_Id
) return Boolean is
10287 if Is_Formal
(Id
) or else Ekind_In
(Id
, E_Constant
, E_Variable
) then
10288 return Present
(Effective_Extra_Accessibility
(Id
));
10292 end Has_Extra_Accessibility
;
10294 -- Start of processing for Expand_N_Type_Conversion
10297 -- First remove check marks put by the semantic analysis on the type
10298 -- conversion between array types. We need these checks, and they will
10299 -- be generated by this expansion routine, but we do not depend on these
10300 -- flags being set, and since we do intend to expand the checks in the
10301 -- front end, we don't want them on the tree passed to the back end.
10303 if Is_Array_Type
(Target_Type
) then
10304 if Is_Constrained
(Target_Type
) then
10305 Set_Do_Length_Check
(N
, False);
10307 Set_Do_Range_Check
(Operand
, False);
10311 -- Nothing at all to do if conversion is to the identical type so remove
10312 -- the conversion completely, it is useless, except that it may carry
10313 -- an Assignment_OK attribute, which must be propagated to the operand.
10315 if Operand_Type
= Target_Type
then
10316 if Assignment_OK
(N
) then
10317 Set_Assignment_OK
(Operand
);
10320 Rewrite
(N
, Relocate_Node
(Operand
));
10324 -- Nothing to do if this is the second argument of read. This is a
10325 -- "backwards" conversion that will be handled by the specialized code
10326 -- in attribute processing.
10328 if Nkind
(Parent
(N
)) = N_Attribute_Reference
10329 and then Attribute_Name
(Parent
(N
)) = Name_Read
10330 and then Next
(First
(Expressions
(Parent
(N
)))) = N
10335 -- Check for case of converting to a type that has an invariant
10336 -- associated with it. This required an invariant check. We convert
10342 -- do invariant_check (typ (expr)) in typ (expr);
10344 -- using Duplicate_Subexpr to avoid multiple side effects
10346 -- Note: the Comes_From_Source check, and then the resetting of this
10347 -- flag prevents what would otherwise be an infinite recursion.
10349 if Has_Invariants
(Target_Type
)
10350 and then Present
(Invariant_Procedure
(Target_Type
))
10351 and then Comes_From_Source
(N
)
10353 Set_Comes_From_Source
(N
, False);
10355 Make_Expression_With_Actions
(Loc
,
10356 Actions
=> New_List
(
10357 Make_Invariant_Call
(Duplicate_Subexpr
(N
))),
10358 Expression
=> Duplicate_Subexpr_No_Checks
(N
)));
10359 Analyze_And_Resolve
(N
, Target_Type
);
10363 -- Here if we may need to expand conversion
10365 -- If the operand of the type conversion is an arithmetic operation on
10366 -- signed integers, and the based type of the signed integer type in
10367 -- question is smaller than Standard.Integer, we promote both of the
10368 -- operands to type Integer.
10370 -- For example, if we have
10372 -- target-type (opnd1 + opnd2)
10374 -- and opnd1 and opnd2 are of type short integer, then we rewrite
10377 -- target-type (integer(opnd1) + integer(opnd2))
10379 -- We do this because we are always allowed to compute in a larger type
10380 -- if we do the right thing with the result, and in this case we are
10381 -- going to do a conversion which will do an appropriate check to make
10382 -- sure that things are in range of the target type in any case. This
10383 -- avoids some unnecessary intermediate overflows.
10385 -- We might consider a similar transformation in the case where the
10386 -- target is a real type or a 64-bit integer type, and the operand
10387 -- is an arithmetic operation using a 32-bit integer type. However,
10388 -- we do not bother with this case, because it could cause significant
10389 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
10390 -- much cheaper, but we don't want different behavior on 32-bit and
10391 -- 64-bit machines. Note that the exclusion of the 64-bit case also
10392 -- handles the configurable run-time cases where 64-bit arithmetic
10393 -- may simply be unavailable.
10395 -- Note: this circuit is partially redundant with respect to the circuit
10396 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
10397 -- the processing here. Also we still need the Checks circuit, since we
10398 -- have to be sure not to generate junk overflow checks in the first
10399 -- place, since it would be trick to remove them here.
10401 if Integer_Promotion_Possible
(N
) then
10403 -- All conditions met, go ahead with transformation
10411 Make_Type_Conversion
(Loc
,
10412 Subtype_Mark
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
10413 Expression
=> Relocate_Node
(Right_Opnd
(Operand
)));
10415 Opnd
:= New_Op_Node
(Nkind
(Operand
), Loc
);
10416 Set_Right_Opnd
(Opnd
, R
);
10418 if Nkind
(Operand
) in N_Binary_Op
then
10420 Make_Type_Conversion
(Loc
,
10421 Subtype_Mark
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
10422 Expression
=> Relocate_Node
(Left_Opnd
(Operand
)));
10424 Set_Left_Opnd
(Opnd
, L
);
10428 Make_Type_Conversion
(Loc
,
10429 Subtype_Mark
=> Relocate_Node
(Subtype_Mark
(N
)),
10430 Expression
=> Opnd
));
10432 Analyze_And_Resolve
(N
, Target_Type
);
10437 -- Do validity check if validity checking operands
10439 if Validity_Checks_On
and Validity_Check_Operands
then
10440 Ensure_Valid
(Operand
);
10443 -- Special case of converting from non-standard boolean type
10445 if Is_Boolean_Type
(Operand_Type
)
10446 and then (Nonzero_Is_True
(Operand_Type
))
10448 Adjust_Condition
(Operand
);
10449 Set_Etype
(Operand
, Standard_Boolean
);
10450 Operand_Type
:= Standard_Boolean
;
10453 -- Case of converting to an access type
10455 if Is_Access_Type
(Target_Type
) then
10457 -- Apply an accessibility check when the conversion operand is an
10458 -- access parameter (or a renaming thereof), unless conversion was
10459 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
10460 -- Note that other checks may still need to be applied below (such
10461 -- as tagged type checks).
10463 if Is_Entity_Name
(Operand
)
10464 and then Has_Extra_Accessibility
(Entity
(Operand
))
10465 and then Ekind
(Etype
(Operand
)) = E_Anonymous_Access_Type
10466 and then (Nkind
(Original_Node
(N
)) /= N_Attribute_Reference
10467 or else Attribute_Name
(Original_Node
(N
)) = Name_Access
)
10469 Apply_Accessibility_Check
10470 (Operand
, Target_Type
, Insert_Node
=> Operand
);
10472 -- If the level of the operand type is statically deeper than the
10473 -- level of the target type, then force Program_Error. Note that this
10474 -- can only occur for cases where the attribute is within the body of
10475 -- an instantiation, otherwise the conversion will already have been
10476 -- rejected as illegal.
10478 -- Note: warnings are issued by the analyzer for the instance cases
10480 elsif In_Instance_Body
10482 -- The case where the target type is an anonymous access type of
10483 -- a discriminant is excluded, because the level of such a type
10484 -- depends on the context and currently the level returned for such
10485 -- types is zero, resulting in warnings about about check failures
10486 -- in certain legal cases involving class-wide interfaces as the
10487 -- designated type (some cases, such as return statements, are
10488 -- checked at run time, but not clear if these are handled right
10489 -- in general, see 3.10.2(12/2-12.5/3) ???).
10492 not (Ekind
(Target_Type
) = E_Anonymous_Access_Type
10493 and then Present
(Associated_Node_For_Itype
(Target_Type
))
10494 and then Nkind
(Associated_Node_For_Itype
(Target_Type
)) =
10495 N_Discriminant_Specification
)
10497 Type_Access_Level
(Operand_Type
) > Type_Access_Level
(Target_Type
)
10499 Raise_Accessibility_Error
;
10501 -- When the operand is a selected access discriminant the check needs
10502 -- to be made against the level of the object denoted by the prefix
10503 -- of the selected name. Force Program_Error for this case as well
10504 -- (this accessibility violation can only happen if within the body
10505 -- of an instantiation).
10507 elsif In_Instance_Body
10508 and then Ekind
(Operand_Type
) = E_Anonymous_Access_Type
10509 and then Nkind
(Operand
) = N_Selected_Component
10510 and then Object_Access_Level
(Operand
) >
10511 Type_Access_Level
(Target_Type
)
10513 Raise_Accessibility_Error
;
10518 -- Case of conversions of tagged types and access to tagged types
10520 -- When needed, that is to say when the expression is class-wide, Add
10521 -- runtime a tag check for (strict) downward conversion by using the
10522 -- membership test, generating:
10524 -- [constraint_error when Operand not in Target_Type'Class]
10526 -- or in the access type case
10528 -- [constraint_error
10529 -- when Operand /= null
10530 -- and then Operand.all not in
10531 -- Designated_Type (Target_Type)'Class]
10533 if (Is_Access_Type
(Target_Type
)
10534 and then Is_Tagged_Type
(Designated_Type
(Target_Type
)))
10535 or else Is_Tagged_Type
(Target_Type
)
10537 -- Do not do any expansion in the access type case if the parent is a
10538 -- renaming, since this is an error situation which will be caught by
10539 -- Sem_Ch8, and the expansion can interfere with this error check.
10541 if Is_Access_Type
(Target_Type
) and then Is_Renamed_Object
(N
) then
10545 -- Otherwise, proceed with processing tagged conversion
10547 Tagged_Conversion
: declare
10548 Actual_Op_Typ
: Entity_Id
;
10549 Actual_Targ_Typ
: Entity_Id
;
10550 Make_Conversion
: Boolean := False;
10551 Root_Op_Typ
: Entity_Id
;
10553 procedure Make_Tag_Check
(Targ_Typ
: Entity_Id
);
10554 -- Create a membership check to test whether Operand is a member
10555 -- of Targ_Typ. If the original Target_Type is an access, include
10556 -- a test for null value. The check is inserted at N.
10558 --------------------
10559 -- Make_Tag_Check --
10560 --------------------
10562 procedure Make_Tag_Check
(Targ_Typ
: Entity_Id
) is
10567 -- [Constraint_Error
10568 -- when Operand /= null
10569 -- and then Operand.all not in Targ_Typ]
10571 if Is_Access_Type
(Target_Type
) then
10573 Make_And_Then
(Loc
,
10576 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
10577 Right_Opnd
=> Make_Null
(Loc
)),
10582 Make_Explicit_Dereference
(Loc
,
10583 Prefix
=> Duplicate_Subexpr_No_Checks
(Operand
)),
10584 Right_Opnd
=> New_Occurrence_Of
(Targ_Typ
, Loc
)));
10587 -- [Constraint_Error when Operand not in Targ_Typ]
10592 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
10593 Right_Opnd
=> New_Occurrence_Of
(Targ_Typ
, Loc
));
10597 Make_Raise_Constraint_Error
(Loc
,
10599 Reason
=> CE_Tag_Check_Failed
));
10600 end Make_Tag_Check
;
10602 -- Start of processing for Tagged_Conversion
10605 -- Handle entities from the limited view
10607 if Is_Access_Type
(Operand_Type
) then
10609 Available_View
(Designated_Type
(Operand_Type
));
10611 Actual_Op_Typ
:= Operand_Type
;
10614 if Is_Access_Type
(Target_Type
) then
10616 Available_View
(Designated_Type
(Target_Type
));
10618 Actual_Targ_Typ
:= Target_Type
;
10621 Root_Op_Typ
:= Root_Type
(Actual_Op_Typ
);
10623 -- Ada 2005 (AI-251): Handle interface type conversion
10625 if Is_Interface
(Actual_Op_Typ
) then
10626 Expand_Interface_Conversion
(N
);
10630 if not Tag_Checks_Suppressed
(Actual_Targ_Typ
) then
10632 -- Create a runtime tag check for a downward class-wide type
10635 if Is_Class_Wide_Type
(Actual_Op_Typ
)
10636 and then Actual_Op_Typ
/= Actual_Targ_Typ
10637 and then Root_Op_Typ
/= Actual_Targ_Typ
10638 and then Is_Ancestor
(Root_Op_Typ
, Actual_Targ_Typ
,
10639 Use_Full_View
=> True)
10641 Make_Tag_Check
(Class_Wide_Type
(Actual_Targ_Typ
));
10642 Make_Conversion
:= True;
10645 -- AI05-0073: If the result subtype of the function is defined
10646 -- by an access_definition designating a specific tagged type
10647 -- T, a check is made that the result value is null or the tag
10648 -- of the object designated by the result value identifies T.
10649 -- Constraint_Error is raised if this check fails.
10651 if Nkind
(Parent
(N
)) = N_Simple_Return_Statement
then
10654 Func_Typ
: Entity_Id
;
10657 -- Climb scope stack looking for the enclosing function
10659 Func
:= Current_Scope
;
10660 while Present
(Func
)
10661 and then Ekind
(Func
) /= E_Function
10663 Func
:= Scope
(Func
);
10666 -- The function's return subtype must be defined using
10667 -- an access definition.
10669 if Nkind
(Result_Definition
(Parent
(Func
))) =
10670 N_Access_Definition
10672 Func_Typ
:= Directly_Designated_Type
(Etype
(Func
));
10674 -- The return subtype denotes a specific tagged type,
10675 -- in other words, a non class-wide type.
10677 if Is_Tagged_Type
(Func_Typ
)
10678 and then not Is_Class_Wide_Type
(Func_Typ
)
10680 Make_Tag_Check
(Actual_Targ_Typ
);
10681 Make_Conversion
:= True;
10687 -- We have generated a tag check for either a class-wide type
10688 -- conversion or for AI05-0073.
10690 if Make_Conversion
then
10695 Make_Unchecked_Type_Conversion
(Loc
,
10696 Subtype_Mark
=> New_Occurrence_Of
(Target_Type
, Loc
),
10697 Expression
=> Relocate_Node
(Expression
(N
)));
10699 Analyze_And_Resolve
(N
, Target_Type
);
10703 end Tagged_Conversion
;
10705 -- Case of other access type conversions
10707 elsif Is_Access_Type
(Target_Type
) then
10708 Apply_Constraint_Check
(Operand
, Target_Type
);
10710 -- Case of conversions from a fixed-point type
10712 -- These conversions require special expansion and processing, found in
10713 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
10714 -- since from a semantic point of view, these are simple integer
10715 -- conversions, which do not need further processing.
10717 elsif Is_Fixed_Point_Type
(Operand_Type
)
10718 and then not Conversion_OK
(N
)
10720 -- We should never see universal fixed at this case, since the
10721 -- expansion of the constituent divide or multiply should have
10722 -- eliminated the explicit mention of universal fixed.
10724 pragma Assert
(Operand_Type
/= Universal_Fixed
);
10726 -- Check for special case of the conversion to universal real that
10727 -- occurs as a result of the use of a round attribute. In this case,
10728 -- the real type for the conversion is taken from the target type of
10729 -- the Round attribute and the result must be marked as rounded.
10731 if Target_Type
= Universal_Real
10732 and then Nkind
(Parent
(N
)) = N_Attribute_Reference
10733 and then Attribute_Name
(Parent
(N
)) = Name_Round
10735 Set_Rounded_Result
(N
);
10736 Set_Etype
(N
, Etype
(Parent
(N
)));
10739 -- Otherwise do correct fixed-conversion, but skip these if the
10740 -- Conversion_OK flag is set, because from a semantic point of view
10741 -- these are simple integer conversions needing no further processing
10742 -- (the backend will simply treat them as integers).
10744 if not Conversion_OK
(N
) then
10745 if Is_Fixed_Point_Type
(Etype
(N
)) then
10746 Expand_Convert_Fixed_To_Fixed
(N
);
10749 elsif Is_Integer_Type
(Etype
(N
)) then
10750 Expand_Convert_Fixed_To_Integer
(N
);
10753 pragma Assert
(Is_Floating_Point_Type
(Etype
(N
)));
10754 Expand_Convert_Fixed_To_Float
(N
);
10759 -- Case of conversions to a fixed-point type
10761 -- These conversions require special expansion and processing, found in
10762 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
10763 -- since from a semantic point of view, these are simple integer
10764 -- conversions, which do not need further processing.
10766 elsif Is_Fixed_Point_Type
(Target_Type
)
10767 and then not Conversion_OK
(N
)
10769 if Is_Integer_Type
(Operand_Type
) then
10770 Expand_Convert_Integer_To_Fixed
(N
);
10773 pragma Assert
(Is_Floating_Point_Type
(Operand_Type
));
10774 Expand_Convert_Float_To_Fixed
(N
);
10778 -- Case of float-to-integer conversions
10780 -- We also handle float-to-fixed conversions with Conversion_OK set
10781 -- since semantically the fixed-point target is treated as though it
10782 -- were an integer in such cases.
10784 elsif Is_Floating_Point_Type
(Operand_Type
)
10786 (Is_Integer_Type
(Target_Type
)
10788 (Is_Fixed_Point_Type
(Target_Type
) and then Conversion_OK
(N
)))
10790 -- One more check here, gcc is still not able to do conversions of
10791 -- this type with proper overflow checking, and so gigi is doing an
10792 -- approximation of what is required by doing floating-point compares
10793 -- with the end-point. But that can lose precision in some cases, and
10794 -- give a wrong result. Converting the operand to Universal_Real is
10795 -- helpful, but still does not catch all cases with 64-bit integers
10796 -- on targets with only 64-bit floats.
10798 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
10799 -- Can this code be removed ???
10801 if Do_Range_Check
(Operand
) then
10803 Make_Type_Conversion
(Loc
,
10805 New_Occurrence_Of
(Universal_Real
, Loc
),
10807 Relocate_Node
(Operand
)));
10809 Set_Etype
(Operand
, Universal_Real
);
10810 Enable_Range_Check
(Operand
);
10811 Set_Do_Range_Check
(Expression
(Operand
), False);
10814 -- Case of array conversions
10816 -- Expansion of array conversions, add required length/range checks but
10817 -- only do this if there is no change of representation. For handling of
10818 -- this case, see Handle_Changed_Representation.
10820 elsif Is_Array_Type
(Target_Type
) then
10821 if Is_Constrained
(Target_Type
) then
10822 Apply_Length_Check
(Operand
, Target_Type
);
10824 Apply_Range_Check
(Operand
, Target_Type
);
10827 Handle_Changed_Representation
;
10829 -- Case of conversions of discriminated types
10831 -- Add required discriminant checks if target is constrained. Again this
10832 -- change is skipped if we have a change of representation.
10834 elsif Has_Discriminants
(Target_Type
)
10835 and then Is_Constrained
(Target_Type
)
10837 Apply_Discriminant_Check
(Operand
, Target_Type
);
10838 Handle_Changed_Representation
;
10840 -- Case of all other record conversions. The only processing required
10841 -- is to check for a change of representation requiring the special
10842 -- assignment processing.
10844 elsif Is_Record_Type
(Target_Type
) then
10846 -- Ada 2005 (AI-216): Program_Error is raised when converting from
10847 -- a derived Unchecked_Union type to an unconstrained type that is
10848 -- not Unchecked_Union if the operand lacks inferable discriminants.
10850 if Is_Derived_Type
(Operand_Type
)
10851 and then Is_Unchecked_Union
(Base_Type
(Operand_Type
))
10852 and then not Is_Constrained
(Target_Type
)
10853 and then not Is_Unchecked_Union
(Base_Type
(Target_Type
))
10854 and then not Has_Inferable_Discriminants
(Operand
)
10856 -- To prevent Gigi from generating illegal code, we generate a
10857 -- Program_Error node, but we give it the target type of the
10858 -- conversion (is this requirement documented somewhere ???)
10861 PE
: constant Node_Id
:= Make_Raise_Program_Error
(Loc
,
10862 Reason
=> PE_Unchecked_Union_Restriction
);
10865 Set_Etype
(PE
, Target_Type
);
10870 Handle_Changed_Representation
;
10873 -- Case of conversions of enumeration types
10875 elsif Is_Enumeration_Type
(Target_Type
) then
10877 -- Special processing is required if there is a change of
10878 -- representation (from enumeration representation clauses).
10880 if not Same_Representation
(Target_Type
, Operand_Type
) then
10882 -- Convert: x(y) to x'val (ytyp'val (y))
10885 Make_Attribute_Reference
(Loc
,
10886 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
10887 Attribute_Name
=> Name_Val
,
10888 Expressions
=> New_List
(
10889 Make_Attribute_Reference
(Loc
,
10890 Prefix
=> New_Occurrence_Of
(Operand_Type
, Loc
),
10891 Attribute_Name
=> Name_Pos
,
10892 Expressions
=> New_List
(Operand
)))));
10894 Analyze_And_Resolve
(N
, Target_Type
);
10897 -- Case of conversions to floating-point
10899 elsif Is_Floating_Point_Type
(Target_Type
) then
10903 -- At this stage, either the conversion node has been transformed into
10904 -- some other equivalent expression, or left as a conversion that can be
10905 -- handled by Gigi, in the following cases:
10907 -- Conversions with no change of representation or type
10909 -- Numeric conversions involving integer, floating- and fixed-point
10910 -- values. Fixed-point values are allowed only if Conversion_OK is
10911 -- set, i.e. if the fixed-point values are to be treated as integers.
10913 -- No other conversions should be passed to Gigi
10915 -- Check: are these rules stated in sinfo??? if so, why restate here???
10917 -- The only remaining step is to generate a range check if we still have
10918 -- a type conversion at this stage and Do_Range_Check is set. For now we
10919 -- do this only for conversions of discrete types and for float-to-float
10922 if Nkind
(N
) = N_Type_Conversion
then
10924 -- For now we only support floating-point cases where both source
10925 -- and target are floating-point types. Conversions where the source
10926 -- and target involve integer or fixed-point types are still TBD,
10927 -- though not clear whether those can even happen at this point, due
10928 -- to transformations above. ???
10930 if Is_Floating_Point_Type
(Etype
(N
))
10931 and then Is_Floating_Point_Type
(Etype
(Expression
(N
)))
10933 if Do_Range_Check
(Expression
(N
))
10934 and then Is_Floating_Point_Type
(Target_Type
)
10936 Generate_Range_Check
10937 (Expression
(N
), Target_Type
, CE_Range_Check_Failed
);
10940 -- Discrete-to-discrete conversions
10942 elsif Is_Discrete_Type
(Etype
(N
)) then
10944 Expr
: constant Node_Id
:= Expression
(N
);
10949 if Do_Range_Check
(Expr
)
10950 and then Is_Discrete_Type
(Etype
(Expr
))
10952 Set_Do_Range_Check
(Expr
, False);
10954 -- Before we do a range check, we have to deal with treating
10955 -- a fixed-point operand as an integer. The way we do this
10956 -- is simply to do an unchecked conversion to an appropriate
10957 -- integer type large enough to hold the result.
10959 -- This code is not active yet, because we are only dealing
10960 -- with discrete types so far ???
10962 if Nkind
(Expr
) in N_Has_Treat_Fixed_As_Integer
10963 and then Treat_Fixed_As_Integer
(Expr
)
10965 Ftyp
:= Base_Type
(Etype
(Expr
));
10967 if Esize
(Ftyp
) >= Esize
(Standard_Integer
) then
10968 Ityp
:= Standard_Long_Long_Integer
;
10970 Ityp
:= Standard_Integer
;
10973 Rewrite
(Expr
, Unchecked_Convert_To
(Ityp
, Expr
));
10976 -- Reset overflow flag, since the range check will include
10977 -- dealing with possible overflow, and generate the check.
10978 -- If Address is either a source type or target type,
10979 -- suppress range check to avoid typing anomalies when
10980 -- it is a visible integer type.
10982 Set_Do_Overflow_Check
(N
, False);
10984 if not Is_Descendent_Of_Address
(Etype
(Expr
))
10985 and then not Is_Descendent_Of_Address
(Target_Type
)
10987 Generate_Range_Check
10988 (Expr
, Target_Type
, CE_Range_Check_Failed
);
10995 -- Here at end of processing
10998 -- Apply predicate check if required. Note that we can't just call
10999 -- Apply_Predicate_Check here, because the type looks right after
11000 -- the conversion and it would omit the check. The Comes_From_Source
11001 -- guard is necessary to prevent infinite recursions when we generate
11002 -- internal conversions for the purpose of checking predicates.
11004 if Present
(Predicate_Function
(Target_Type
))
11005 and then Target_Type
/= Operand_Type
11006 and then Comes_From_Source
(N
)
11009 New_Expr
: constant Node_Id
:= Duplicate_Subexpr
(N
);
11012 -- Avoid infinite recursion on the subsequent expansion of
11013 -- of the copy of the original type conversion.
11015 Set_Comes_From_Source
(New_Expr
, False);
11016 Insert_Action
(N
, Make_Predicate_Check
(Target_Type
, New_Expr
));
11019 end Expand_N_Type_Conversion
;
11021 -----------------------------------
11022 -- Expand_N_Unchecked_Expression --
11023 -----------------------------------
11025 -- Remove the unchecked expression node from the tree. Its job was simply
11026 -- to make sure that its constituent expression was handled with checks
11027 -- off, and now that that is done, we can remove it from the tree, and
11028 -- indeed must, since Gigi does not expect to see these nodes.
11030 procedure Expand_N_Unchecked_Expression
(N
: Node_Id
) is
11031 Exp
: constant Node_Id
:= Expression
(N
);
11033 Set_Assignment_OK
(Exp
, Assignment_OK
(N
) or else Assignment_OK
(Exp
));
11035 end Expand_N_Unchecked_Expression
;
11037 ----------------------------------------
11038 -- Expand_N_Unchecked_Type_Conversion --
11039 ----------------------------------------
11041 -- If this cannot be handled by Gigi and we haven't already made a
11042 -- temporary for it, do it now.
11044 procedure Expand_N_Unchecked_Type_Conversion
(N
: Node_Id
) is
11045 Target_Type
: constant Entity_Id
:= Etype
(N
);
11046 Operand
: constant Node_Id
:= Expression
(N
);
11047 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
11050 -- Nothing at all to do if conversion is to the identical type so remove
11051 -- the conversion completely, it is useless, except that it may carry
11052 -- an Assignment_OK indication which must be propagated to the operand.
11054 if Operand_Type
= Target_Type
then
11056 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
11058 if Assignment_OK
(N
) then
11059 Set_Assignment_OK
(Operand
);
11062 Rewrite
(N
, Relocate_Node
(Operand
));
11066 -- If we have a conversion of a compile time known value to a target
11067 -- type and the value is in range of the target type, then we can simply
11068 -- replace the construct by an integer literal of the correct type. We
11069 -- only apply this to integer types being converted. Possibly it may
11070 -- apply in other cases, but it is too much trouble to worry about.
11072 -- Note that we do not do this transformation if the Kill_Range_Check
11073 -- flag is set, since then the value may be outside the expected range.
11074 -- This happens in the Normalize_Scalars case.
11076 -- We also skip this if either the target or operand type is biased
11077 -- because in this case, the unchecked conversion is supposed to
11078 -- preserve the bit pattern, not the integer value.
11080 if Is_Integer_Type
(Target_Type
)
11081 and then not Has_Biased_Representation
(Target_Type
)
11082 and then Is_Integer_Type
(Operand_Type
)
11083 and then not Has_Biased_Representation
(Operand_Type
)
11084 and then Compile_Time_Known_Value
(Operand
)
11085 and then not Kill_Range_Check
(N
)
11088 Val
: constant Uint
:= Expr_Value
(Operand
);
11091 if Compile_Time_Known_Value
(Type_Low_Bound
(Target_Type
))
11093 Compile_Time_Known_Value
(Type_High_Bound
(Target_Type
))
11095 Val
>= Expr_Value
(Type_Low_Bound
(Target_Type
))
11097 Val
<= Expr_Value
(Type_High_Bound
(Target_Type
))
11099 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Val
));
11101 -- If Address is the target type, just set the type to avoid a
11102 -- spurious type error on the literal when Address is a visible
11105 if Is_Descendent_Of_Address
(Target_Type
) then
11106 Set_Etype
(N
, Target_Type
);
11108 Analyze_And_Resolve
(N
, Target_Type
);
11116 -- Nothing to do if conversion is safe
11118 if Safe_Unchecked_Type_Conversion
(N
) then
11122 -- Otherwise force evaluation unless Assignment_OK flag is set (this
11123 -- flag indicates ??? More comments needed here)
11125 if Assignment_OK
(N
) then
11128 Force_Evaluation
(N
);
11130 end Expand_N_Unchecked_Type_Conversion
;
11132 ----------------------------
11133 -- Expand_Record_Equality --
11134 ----------------------------
11136 -- For non-variant records, Equality is expanded when needed into:
11138 -- and then Lhs.Discr1 = Rhs.Discr1
11140 -- and then Lhs.Discrn = Rhs.Discrn
11141 -- and then Lhs.Cmp1 = Rhs.Cmp1
11143 -- and then Lhs.Cmpn = Rhs.Cmpn
11145 -- The expression is folded by the back-end for adjacent fields. This
11146 -- function is called for tagged record in only one occasion: for imple-
11147 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
11148 -- otherwise the primitive "=" is used directly.
11150 function Expand_Record_Equality
11155 Bodies
: List_Id
) return Node_Id
11157 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
11162 First_Time
: Boolean := True;
11164 function Element_To_Compare
(C
: Entity_Id
) return Entity_Id
;
11165 -- Return the next discriminant or component to compare, starting with
11166 -- C, skipping inherited components.
11168 ------------------------
11169 -- Element_To_Compare --
11170 ------------------------
11172 function Element_To_Compare
(C
: Entity_Id
) return Entity_Id
is
11178 -- Exit loop when the next element to be compared is found, or
11179 -- there is no more such element.
11181 exit when No
(Comp
);
11183 exit when Ekind_In
(Comp
, E_Discriminant
, E_Component
)
11186 -- Skip inherited components
11188 -- Note: for a tagged type, we always generate the "=" primitive
11189 -- for the base type (not on the first subtype), so the test for
11190 -- Comp /= Original_Record_Component (Comp) is True for
11191 -- inherited components only.
11193 (Is_Tagged_Type
(Typ
)
11194 and then Comp
/= Original_Record_Component
(Comp
))
11198 or else Chars
(Comp
) = Name_uTag
11200 -- The .NET/JVM version of type Root_Controlled contains two
11201 -- fields which should not be considered part of the object. To
11202 -- achieve proper equiality between two controlled objects on
11203 -- .NET/JVM, skip _Parent whenever it has type Root_Controlled.
11205 or else (Chars
(Comp
) = Name_uParent
11206 and then VM_Target
/= No_VM
11207 and then Etype
(Comp
) = RTE
(RE_Root_Controlled
))
11209 -- Skip interface elements (secondary tags???)
11211 or else Is_Interface
(Etype
(Comp
)));
11213 Next_Entity
(Comp
);
11217 end Element_To_Compare
;
11219 -- Start of processing for Expand_Record_Equality
11222 -- Generates the following code: (assuming that Typ has one Discr and
11223 -- component C2 is also a record)
11226 -- and then Lhs.Discr1 = Rhs.Discr1
11227 -- and then Lhs.C1 = Rhs.C1
11228 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
11230 -- and then Lhs.Cmpn = Rhs.Cmpn
11232 Result
:= New_Occurrence_Of
(Standard_True
, Loc
);
11233 C
:= Element_To_Compare
(First_Entity
(Typ
));
11234 while Present
(C
) loop
11242 First_Time
:= False;
11246 New_Lhs
:= New_Copy_Tree
(Lhs
);
11247 New_Rhs
:= New_Copy_Tree
(Rhs
);
11251 Expand_Composite_Equality
(Nod
, Etype
(C
),
11253 Make_Selected_Component
(Loc
,
11255 Selector_Name
=> New_Occurrence_Of
(C
, Loc
)),
11257 Make_Selected_Component
(Loc
,
11259 Selector_Name
=> New_Occurrence_Of
(C
, Loc
)),
11262 -- If some (sub)component is an unchecked_union, the whole
11263 -- operation will raise program error.
11265 if Nkind
(Check
) = N_Raise_Program_Error
then
11267 Set_Etype
(Result
, Standard_Boolean
);
11271 Make_And_Then
(Loc
,
11272 Left_Opnd
=> Result
,
11273 Right_Opnd
=> Check
);
11277 C
:= Element_To_Compare
(Next_Entity
(C
));
11281 end Expand_Record_Equality
;
11283 ---------------------------
11284 -- Expand_Set_Membership --
11285 ---------------------------
11287 procedure Expand_Set_Membership
(N
: Node_Id
) is
11288 Lop
: constant Node_Id
:= Left_Opnd
(N
);
11292 function Make_Cond
(Alt
: Node_Id
) return Node_Id
;
11293 -- If the alternative is a subtype mark, create a simple membership
11294 -- test. Otherwise create an equality test for it.
11300 function Make_Cond
(Alt
: Node_Id
) return Node_Id
is
11302 L
: constant Node_Id
:= New_Copy
(Lop
);
11303 R
: constant Node_Id
:= Relocate_Node
(Alt
);
11306 if (Is_Entity_Name
(Alt
) and then Is_Type
(Entity
(Alt
)))
11307 or else Nkind
(Alt
) = N_Range
11310 Make_In
(Sloc
(Alt
),
11315 Make_Op_Eq
(Sloc
(Alt
),
11323 -- Start of processing for Expand_Set_Membership
11326 Remove_Side_Effects
(Lop
);
11328 Alt
:= Last
(Alternatives
(N
));
11329 Res
:= Make_Cond
(Alt
);
11332 while Present
(Alt
) loop
11334 Make_Or_Else
(Sloc
(Alt
),
11335 Left_Opnd
=> Make_Cond
(Alt
),
11336 Right_Opnd
=> Res
);
11341 Analyze_And_Resolve
(N
, Standard_Boolean
);
11342 end Expand_Set_Membership
;
11344 -----------------------------------
11345 -- Expand_Short_Circuit_Operator --
11346 -----------------------------------
11348 -- Deal with special expansion if actions are present for the right operand
11349 -- and deal with optimizing case of arguments being True or False. We also
11350 -- deal with the special case of non-standard boolean values.
11352 procedure Expand_Short_Circuit_Operator
(N
: Node_Id
) is
11353 Loc
: constant Source_Ptr
:= Sloc
(N
);
11354 Typ
: constant Entity_Id
:= Etype
(N
);
11355 Left
: constant Node_Id
:= Left_Opnd
(N
);
11356 Right
: constant Node_Id
:= Right_Opnd
(N
);
11357 LocR
: constant Source_Ptr
:= Sloc
(Right
);
11360 Shortcut_Value
: constant Boolean := Nkind
(N
) = N_Or_Else
;
11361 Shortcut_Ent
: constant Entity_Id
:= Boolean_Literals
(Shortcut_Value
);
11362 -- If Left = Shortcut_Value then Right need not be evaluated
11365 -- Deal with non-standard booleans
11367 if Is_Boolean_Type
(Typ
) then
11368 Adjust_Condition
(Left
);
11369 Adjust_Condition
(Right
);
11370 Set_Etype
(N
, Standard_Boolean
);
11373 -- Check for cases where left argument is known to be True or False
11375 if Compile_Time_Known_Value
(Left
) then
11377 -- Mark SCO for left condition as compile time known
11379 if Generate_SCO
and then Comes_From_Source
(Left
) then
11380 Set_SCO_Condition
(Left
, Expr_Value_E
(Left
) = Standard_True
);
11383 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
11384 -- Any actions associated with Right will be executed unconditionally
11385 -- and can thus be inserted into the tree unconditionally.
11387 if Expr_Value_E
(Left
) /= Shortcut_Ent
then
11388 if Present
(Actions
(N
)) then
11389 Insert_Actions
(N
, Actions
(N
));
11392 Rewrite
(N
, Right
);
11394 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
11395 -- In this case we can forget the actions associated with Right,
11396 -- since they will never be executed.
11399 Kill_Dead_Code
(Right
);
11400 Kill_Dead_Code
(Actions
(N
));
11401 Rewrite
(N
, New_Occurrence_Of
(Shortcut_Ent
, Loc
));
11404 Adjust_Result_Type
(N
, Typ
);
11408 -- If Actions are present for the right operand, we have to do some
11409 -- special processing. We can't just let these actions filter back into
11410 -- code preceding the short circuit (which is what would have happened
11411 -- if we had not trapped them in the short-circuit form), since they
11412 -- must only be executed if the right operand of the short circuit is
11413 -- executed and not otherwise.
11415 if Present
(Actions
(N
)) then
11416 Actlist
:= Actions
(N
);
11418 -- We now use an Expression_With_Actions node for the right operand
11419 -- of the short-circuit form. Note that this solves the traceability
11420 -- problems for coverage analysis.
11423 Make_Expression_With_Actions
(LocR
,
11424 Expression
=> Relocate_Node
(Right
),
11425 Actions
=> Actlist
));
11426 Set_Actions
(N
, No_List
);
11427 Analyze_And_Resolve
(Right
, Standard_Boolean
);
11429 Adjust_Result_Type
(N
, Typ
);
11433 -- No actions present, check for cases of right argument True/False
11435 if Compile_Time_Known_Value
(Right
) then
11437 -- Mark SCO for left condition as compile time known
11439 if Generate_SCO
and then Comes_From_Source
(Right
) then
11440 Set_SCO_Condition
(Right
, Expr_Value_E
(Right
) = Standard_True
);
11443 -- Change (Left and then True), (Left or else False) to Left.
11444 -- Note that we know there are no actions associated with the right
11445 -- operand, since we just checked for this case above.
11447 if Expr_Value_E
(Right
) /= Shortcut_Ent
then
11450 -- Change (Left and then False), (Left or else True) to Right,
11451 -- making sure to preserve any side effects associated with the Left
11455 Remove_Side_Effects
(Left
);
11456 Rewrite
(N
, New_Occurrence_Of
(Shortcut_Ent
, Loc
));
11460 Adjust_Result_Type
(N
, Typ
);
11461 end Expand_Short_Circuit_Operator
;
11463 -------------------------------------
11464 -- Fixup_Universal_Fixed_Operation --
11465 -------------------------------------
11467 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
) is
11468 Conv
: constant Node_Id
:= Parent
(N
);
11471 -- We must have a type conversion immediately above us
11473 pragma Assert
(Nkind
(Conv
) = N_Type_Conversion
);
11475 -- Normally the type conversion gives our target type. The exception
11476 -- occurs in the case of the Round attribute, where the conversion
11477 -- will be to universal real, and our real type comes from the Round
11478 -- attribute (as well as an indication that we must round the result)
11480 if Nkind
(Parent
(Conv
)) = N_Attribute_Reference
11481 and then Attribute_Name
(Parent
(Conv
)) = Name_Round
11483 Set_Etype
(N
, Etype
(Parent
(Conv
)));
11484 Set_Rounded_Result
(N
);
11486 -- Normal case where type comes from conversion above us
11489 Set_Etype
(N
, Etype
(Conv
));
11491 end Fixup_Universal_Fixed_Operation
;
11493 ---------------------------------
11494 -- Has_Inferable_Discriminants --
11495 ---------------------------------
11497 function Has_Inferable_Discriminants
(N
: Node_Id
) return Boolean is
11499 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean;
11500 -- Determines whether the left-most prefix of a selected component is a
11501 -- formal parameter in a subprogram. Assumes N is a selected component.
11503 --------------------------------
11504 -- Prefix_Is_Formal_Parameter --
11505 --------------------------------
11507 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean is
11508 Sel_Comp
: Node_Id
;
11511 -- Move to the left-most prefix by climbing up the tree
11514 while Present
(Parent
(Sel_Comp
))
11515 and then Nkind
(Parent
(Sel_Comp
)) = N_Selected_Component
11517 Sel_Comp
:= Parent
(Sel_Comp
);
11520 return Ekind
(Entity
(Prefix
(Sel_Comp
))) in Formal_Kind
;
11521 end Prefix_Is_Formal_Parameter
;
11523 -- Start of processing for Has_Inferable_Discriminants
11526 -- For selected components, the subtype of the selector must be a
11527 -- constrained Unchecked_Union. If the component is subject to a
11528 -- per-object constraint, then the enclosing object must have inferable
11531 if Nkind
(N
) = N_Selected_Component
then
11532 if Has_Per_Object_Constraint
(Entity
(Selector_Name
(N
))) then
11534 -- A small hack. If we have a per-object constrained selected
11535 -- component of a formal parameter, return True since we do not
11536 -- know the actual parameter association yet.
11538 if Prefix_Is_Formal_Parameter
(N
) then
11541 -- Otherwise, check the enclosing object and the selector
11544 return Has_Inferable_Discriminants
(Prefix
(N
))
11545 and then Has_Inferable_Discriminants
(Selector_Name
(N
));
11548 -- The call to Has_Inferable_Discriminants will determine whether
11549 -- the selector has a constrained Unchecked_Union nominal type.
11552 return Has_Inferable_Discriminants
(Selector_Name
(N
));
11555 -- A qualified expression has inferable discriminants if its subtype
11556 -- mark is a constrained Unchecked_Union subtype.
11558 elsif Nkind
(N
) = N_Qualified_Expression
then
11559 return Is_Unchecked_Union
(Etype
(Subtype_Mark
(N
)))
11560 and then Is_Constrained
(Etype
(Subtype_Mark
(N
)));
11562 -- For all other names, it is sufficient to have a constrained
11563 -- Unchecked_Union nominal subtype.
11566 return Is_Unchecked_Union
(Base_Type
(Etype
(N
)))
11567 and then Is_Constrained
(Etype
(N
));
11569 end Has_Inferable_Discriminants
;
11571 -------------------------------
11572 -- Insert_Dereference_Action --
11573 -------------------------------
11575 procedure Insert_Dereference_Action
(N
: Node_Id
) is
11577 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean;
11578 -- Return true if type of P is derived from Checked_Pool;
11580 -----------------------------
11581 -- Is_Checked_Storage_Pool --
11582 -----------------------------
11584 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean is
11593 while T
/= Etype
(T
) loop
11594 if Is_RTE
(T
, RE_Checked_Pool
) then
11602 end Is_Checked_Storage_Pool
;
11606 Typ
: constant Entity_Id
:= Etype
(N
);
11607 Desig
: constant Entity_Id
:= Available_View
(Designated_Type
(Typ
));
11608 Loc
: constant Source_Ptr
:= Sloc
(N
);
11609 Pool
: constant Entity_Id
:= Associated_Storage_Pool
(Typ
);
11610 Pnod
: constant Node_Id
:= Parent
(N
);
11616 Size_Bits
: Node_Id
;
11619 -- Start of processing for Insert_Dereference_Action
11622 pragma Assert
(Nkind
(Pnod
) = N_Explicit_Dereference
);
11624 -- Do not re-expand a dereference which has already been processed by
11627 if Has_Dereference_Action
(Pnod
) then
11630 -- Do not perform this type of expansion for internally-generated
11633 elsif not Comes_From_Source
(Original_Node
(Pnod
)) then
11636 -- A dereference action is only applicable to objects which have been
11637 -- allocated on a checked pool.
11639 elsif not Is_Checked_Storage_Pool
(Pool
) then
11643 -- Extract the address of the dereferenced object. Generate:
11645 -- Addr : System.Address := <N>'Pool_Address;
11647 Addr
:= Make_Temporary
(Loc
, 'P');
11650 Make_Object_Declaration
(Loc
,
11651 Defining_Identifier
=> Addr
,
11652 Object_Definition
=>
11653 New_Occurrence_Of
(RTE
(RE_Address
), Loc
),
11655 Make_Attribute_Reference
(Loc
,
11656 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
),
11657 Attribute_Name
=> Name_Pool_Address
)));
11659 -- Calculate the size of the dereferenced object. Generate:
11661 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
11664 Make_Explicit_Dereference
(Loc
,
11665 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
11666 Set_Has_Dereference_Action
(Deref
);
11669 Make_Attribute_Reference
(Loc
,
11671 Attribute_Name
=> Name_Size
);
11673 -- Special case of an unconstrained array: need to add descriptor size
11675 if Is_Array_Type
(Desig
)
11676 and then not Is_Constrained
(First_Subtype
(Desig
))
11681 Make_Attribute_Reference
(Loc
,
11683 New_Occurrence_Of
(First_Subtype
(Desig
), Loc
),
11684 Attribute_Name
=> Name_Descriptor_Size
),
11685 Right_Opnd
=> Size_Bits
);
11688 Size
:= Make_Temporary
(Loc
, 'S');
11690 Make_Object_Declaration
(Loc
,
11691 Defining_Identifier
=> Size
,
11692 Object_Definition
=>
11693 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
),
11695 Make_Op_Divide
(Loc
,
11696 Left_Opnd
=> Size_Bits
,
11697 Right_Opnd
=> Make_Integer_Literal
(Loc
, System_Storage_Unit
))));
11699 -- Calculate the alignment of the dereferenced object. Generate:
11700 -- Alig : constant Storage_Count := <N>.all'Alignment;
11703 Make_Explicit_Dereference
(Loc
,
11704 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
11705 Set_Has_Dereference_Action
(Deref
);
11707 Alig
:= Make_Temporary
(Loc
, 'A');
11709 Make_Object_Declaration
(Loc
,
11710 Defining_Identifier
=> Alig
,
11711 Object_Definition
=>
11712 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
),
11714 Make_Attribute_Reference
(Loc
,
11716 Attribute_Name
=> Name_Alignment
)));
11718 -- A dereference of a controlled object requires special processing. The
11719 -- finalization machinery requests additional space from the underlying
11720 -- pool to allocate and hide two pointers. As a result, a checked pool
11721 -- may mark the wrong memory as valid. Since checked pools do not have
11722 -- knowledge of hidden pointers, we have to bring the two pointers back
11723 -- in view in order to restore the original state of the object.
11725 if Needs_Finalization
(Desig
) then
11727 -- Adjust the address and size of the dereferenced object. Generate:
11728 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
11731 Make_Procedure_Call_Statement
(Loc
,
11733 New_Occurrence_Of
(RTE
(RE_Adjust_Controlled_Dereference
), Loc
),
11734 Parameter_Associations
=> New_List
(
11735 New_Occurrence_Of
(Addr
, Loc
),
11736 New_Occurrence_Of
(Size
, Loc
),
11737 New_Occurrence_Of
(Alig
, Loc
)));
11739 -- Class-wide types complicate things because we cannot determine
11740 -- statically whether the actual object is truly controlled. We must
11741 -- generate a runtime check to detect this property. Generate:
11743 -- if Needs_Finalization (<N>.all'Tag) then
11747 if Is_Class_Wide_Type
(Desig
) then
11749 Make_Explicit_Dereference
(Loc
,
11750 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
11751 Set_Has_Dereference_Action
(Deref
);
11754 Make_Implicit_If_Statement
(N
,
11756 Make_Function_Call
(Loc
,
11758 New_Occurrence_Of
(RTE
(RE_Needs_Finalization
), Loc
),
11759 Parameter_Associations
=> New_List
(
11760 Make_Attribute_Reference
(Loc
,
11762 Attribute_Name
=> Name_Tag
))),
11763 Then_Statements
=> New_List
(Stmt
));
11766 Insert_Action
(N
, Stmt
);
11770 -- Dereference (Pool, Addr, Size, Alig);
11773 Make_Procedure_Call_Statement
(Loc
,
11776 (Find_Prim_Op
(Etype
(Pool
), Name_Dereference
), Loc
),
11777 Parameter_Associations
=> New_List
(
11778 New_Occurrence_Of
(Pool
, Loc
),
11779 New_Occurrence_Of
(Addr
, Loc
),
11780 New_Occurrence_Of
(Size
, Loc
),
11781 New_Occurrence_Of
(Alig
, Loc
))));
11783 -- Mark the explicit dereference as processed to avoid potential
11784 -- infinite expansion.
11786 Set_Has_Dereference_Action
(Pnod
);
11789 when RE_Not_Available
=>
11791 end Insert_Dereference_Action
;
11793 --------------------------------
11794 -- Integer_Promotion_Possible --
11795 --------------------------------
11797 function Integer_Promotion_Possible
(N
: Node_Id
) return Boolean is
11798 Operand
: constant Node_Id
:= Expression
(N
);
11799 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
11800 Root_Operand_Type
: constant Entity_Id
:= Root_Type
(Operand_Type
);
11803 pragma Assert
(Nkind
(N
) = N_Type_Conversion
);
11807 -- We only do the transformation for source constructs. We assume
11808 -- that the expander knows what it is doing when it generates code.
11810 Comes_From_Source
(N
)
11812 -- If the operand type is Short_Integer or Short_Short_Integer,
11813 -- then we will promote to Integer, which is available on all
11814 -- targets, and is sufficient to ensure no intermediate overflow.
11815 -- Furthermore it is likely to be as efficient or more efficient
11816 -- than using the smaller type for the computation so we do this
11817 -- unconditionally.
11820 (Root_Operand_Type
= Base_Type
(Standard_Short_Integer
)
11822 Root_Operand_Type
= Base_Type
(Standard_Short_Short_Integer
))
11824 -- Test for interesting operation, which includes addition,
11825 -- division, exponentiation, multiplication, subtraction, absolute
11826 -- value and unary negation. Unary "+" is omitted since it is a
11827 -- no-op and thus can't overflow.
11829 and then Nkind_In
(Operand
, N_Op_Abs
,
11836 end Integer_Promotion_Possible
;
11838 ------------------------------
11839 -- Make_Array_Comparison_Op --
11840 ------------------------------
11842 -- This is a hand-coded expansion of the following generic function:
11845 -- type elem is (<>);
11846 -- type index is (<>);
11847 -- type a is array (index range <>) of elem;
11849 -- function Gnnn (X : a; Y: a) return boolean is
11850 -- J : index := Y'first;
11853 -- if X'length = 0 then
11856 -- elsif Y'length = 0 then
11860 -- for I in X'range loop
11861 -- if X (I) = Y (J) then
11862 -- if J = Y'last then
11865 -- J := index'succ (J);
11869 -- return X (I) > Y (J);
11873 -- return X'length > Y'length;
11877 -- Note that since we are essentially doing this expansion by hand, we
11878 -- do not need to generate an actual or formal generic part, just the
11879 -- instantiated function itself.
11881 -- Perhaps we could have the actual generic available in the run-time,
11882 -- obtained by rtsfind, and actually expand a real instantiation ???
11884 function Make_Array_Comparison_Op
11886 Nod
: Node_Id
) return Node_Id
11888 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
11890 X
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uX
);
11891 Y
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uY
);
11892 I
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uI
);
11893 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
11895 Index
: constant Entity_Id
:= Base_Type
(Etype
(First_Index
(Typ
)));
11897 Loop_Statement
: Node_Id
;
11898 Loop_Body
: Node_Id
;
11900 Inner_If
: Node_Id
;
11901 Final_Expr
: Node_Id
;
11902 Func_Body
: Node_Id
;
11903 Func_Name
: Entity_Id
;
11909 -- if J = Y'last then
11912 -- J := index'succ (J);
11916 Make_Implicit_If_Statement
(Nod
,
11919 Left_Opnd
=> New_Occurrence_Of
(J
, Loc
),
11921 Make_Attribute_Reference
(Loc
,
11922 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
11923 Attribute_Name
=> Name_Last
)),
11925 Then_Statements
=> New_List
(
11926 Make_Exit_Statement
(Loc
)),
11930 Make_Assignment_Statement
(Loc
,
11931 Name
=> New_Occurrence_Of
(J
, Loc
),
11933 Make_Attribute_Reference
(Loc
,
11934 Prefix
=> New_Occurrence_Of
(Index
, Loc
),
11935 Attribute_Name
=> Name_Succ
,
11936 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
))))));
11938 -- if X (I) = Y (J) then
11941 -- return X (I) > Y (J);
11945 Make_Implicit_If_Statement
(Nod
,
11949 Make_Indexed_Component
(Loc
,
11950 Prefix
=> New_Occurrence_Of
(X
, Loc
),
11951 Expressions
=> New_List
(New_Occurrence_Of
(I
, Loc
))),
11954 Make_Indexed_Component
(Loc
,
11955 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
11956 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)))),
11958 Then_Statements
=> New_List
(Inner_If
),
11960 Else_Statements
=> New_List
(
11961 Make_Simple_Return_Statement
(Loc
,
11965 Make_Indexed_Component
(Loc
,
11966 Prefix
=> New_Occurrence_Of
(X
, Loc
),
11967 Expressions
=> New_List
(New_Occurrence_Of
(I
, Loc
))),
11970 Make_Indexed_Component
(Loc
,
11971 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
11972 Expressions
=> New_List
(
11973 New_Occurrence_Of
(J
, Loc
)))))));
11975 -- for I in X'range loop
11980 Make_Implicit_Loop_Statement
(Nod
,
11981 Identifier
=> Empty
,
11983 Iteration_Scheme
=>
11984 Make_Iteration_Scheme
(Loc
,
11985 Loop_Parameter_Specification
=>
11986 Make_Loop_Parameter_Specification
(Loc
,
11987 Defining_Identifier
=> I
,
11988 Discrete_Subtype_Definition
=>
11989 Make_Attribute_Reference
(Loc
,
11990 Prefix
=> New_Occurrence_Of
(X
, Loc
),
11991 Attribute_Name
=> Name_Range
))),
11993 Statements
=> New_List
(Loop_Body
));
11995 -- if X'length = 0 then
11997 -- elsif Y'length = 0 then
12000 -- for ... loop ... end loop;
12001 -- return X'length > Y'length;
12005 Make_Attribute_Reference
(Loc
,
12006 Prefix
=> New_Occurrence_Of
(X
, Loc
),
12007 Attribute_Name
=> Name_Length
);
12010 Make_Attribute_Reference
(Loc
,
12011 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12012 Attribute_Name
=> Name_Length
);
12016 Left_Opnd
=> Length1
,
12017 Right_Opnd
=> Length2
);
12020 Make_Implicit_If_Statement
(Nod
,
12024 Make_Attribute_Reference
(Loc
,
12025 Prefix
=> New_Occurrence_Of
(X
, Loc
),
12026 Attribute_Name
=> Name_Length
),
12028 Make_Integer_Literal
(Loc
, 0)),
12032 Make_Simple_Return_Statement
(Loc
,
12033 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))),
12035 Elsif_Parts
=> New_List
(
12036 Make_Elsif_Part
(Loc
,
12040 Make_Attribute_Reference
(Loc
,
12041 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12042 Attribute_Name
=> Name_Length
),
12044 Make_Integer_Literal
(Loc
, 0)),
12048 Make_Simple_Return_Statement
(Loc
,
12049 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
))))),
12051 Else_Statements
=> New_List
(
12053 Make_Simple_Return_Statement
(Loc
,
12054 Expression
=> Final_Expr
)));
12058 Formals
:= New_List
(
12059 Make_Parameter_Specification
(Loc
,
12060 Defining_Identifier
=> X
,
12061 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)),
12063 Make_Parameter_Specification
(Loc
,
12064 Defining_Identifier
=> Y
,
12065 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)));
12067 -- function Gnnn (...) return boolean is
12068 -- J : index := Y'first;
12073 Func_Name
:= Make_Temporary
(Loc
, 'G');
12076 Make_Subprogram_Body
(Loc
,
12078 Make_Function_Specification
(Loc
,
12079 Defining_Unit_Name
=> Func_Name
,
12080 Parameter_Specifications
=> Formals
,
12081 Result_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
)),
12083 Declarations
=> New_List
(
12084 Make_Object_Declaration
(Loc
,
12085 Defining_Identifier
=> J
,
12086 Object_Definition
=> New_Occurrence_Of
(Index
, Loc
),
12088 Make_Attribute_Reference
(Loc
,
12089 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12090 Attribute_Name
=> Name_First
))),
12092 Handled_Statement_Sequence
=>
12093 Make_Handled_Sequence_Of_Statements
(Loc
,
12094 Statements
=> New_List
(If_Stat
)));
12097 end Make_Array_Comparison_Op
;
12099 ---------------------------
12100 -- Make_Boolean_Array_Op --
12101 ---------------------------
12103 -- For logical operations on boolean arrays, expand in line the following,
12104 -- replacing 'and' with 'or' or 'xor' where needed:
12106 -- function Annn (A : typ; B: typ) return typ is
12109 -- for J in A'range loop
12110 -- C (J) := A (J) op B (J);
12115 -- Here typ is the boolean array type
12117 function Make_Boolean_Array_Op
12119 N
: Node_Id
) return Node_Id
12121 Loc
: constant Source_Ptr
:= Sloc
(N
);
12123 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
12124 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
12125 C
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uC
);
12126 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
12134 Func_Name
: Entity_Id
;
12135 Func_Body
: Node_Id
;
12136 Loop_Statement
: Node_Id
;
12140 Make_Indexed_Component
(Loc
,
12141 Prefix
=> New_Occurrence_Of
(A
, Loc
),
12142 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
12145 Make_Indexed_Component
(Loc
,
12146 Prefix
=> New_Occurrence_Of
(B
, Loc
),
12147 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
12150 Make_Indexed_Component
(Loc
,
12151 Prefix
=> New_Occurrence_Of
(C
, Loc
),
12152 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
12154 if Nkind
(N
) = N_Op_And
then
12158 Right_Opnd
=> B_J
);
12160 elsif Nkind
(N
) = N_Op_Or
then
12164 Right_Opnd
=> B_J
);
12170 Right_Opnd
=> B_J
);
12174 Make_Implicit_Loop_Statement
(N
,
12175 Identifier
=> Empty
,
12177 Iteration_Scheme
=>
12178 Make_Iteration_Scheme
(Loc
,
12179 Loop_Parameter_Specification
=>
12180 Make_Loop_Parameter_Specification
(Loc
,
12181 Defining_Identifier
=> J
,
12182 Discrete_Subtype_Definition
=>
12183 Make_Attribute_Reference
(Loc
,
12184 Prefix
=> New_Occurrence_Of
(A
, Loc
),
12185 Attribute_Name
=> Name_Range
))),
12187 Statements
=> New_List
(
12188 Make_Assignment_Statement
(Loc
,
12190 Expression
=> Op
)));
12192 Formals
:= New_List
(
12193 Make_Parameter_Specification
(Loc
,
12194 Defining_Identifier
=> A
,
12195 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)),
12197 Make_Parameter_Specification
(Loc
,
12198 Defining_Identifier
=> B
,
12199 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)));
12201 Func_Name
:= Make_Temporary
(Loc
, 'A');
12202 Set_Is_Inlined
(Func_Name
);
12205 Make_Subprogram_Body
(Loc
,
12207 Make_Function_Specification
(Loc
,
12208 Defining_Unit_Name
=> Func_Name
,
12209 Parameter_Specifications
=> Formals
,
12210 Result_Definition
=> New_Occurrence_Of
(Typ
, Loc
)),
12212 Declarations
=> New_List
(
12213 Make_Object_Declaration
(Loc
,
12214 Defining_Identifier
=> C
,
12215 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
))),
12217 Handled_Statement_Sequence
=>
12218 Make_Handled_Sequence_Of_Statements
(Loc
,
12219 Statements
=> New_List
(
12221 Make_Simple_Return_Statement
(Loc
,
12222 Expression
=> New_Occurrence_Of
(C
, Loc
)))));
12225 end Make_Boolean_Array_Op
;
12227 -----------------------------------------
12228 -- Minimized_Eliminated_Overflow_Check --
12229 -----------------------------------------
12231 function Minimized_Eliminated_Overflow_Check
(N
: Node_Id
) return Boolean is
12234 Is_Signed_Integer_Type
(Etype
(N
))
12235 and then Overflow_Check_Mode
in Minimized_Or_Eliminated
;
12236 end Minimized_Eliminated_Overflow_Check
;
12238 --------------------------------
12239 -- Optimize_Length_Comparison --
12240 --------------------------------
12242 procedure Optimize_Length_Comparison
(N
: Node_Id
) is
12243 Loc
: constant Source_Ptr
:= Sloc
(N
);
12244 Typ
: constant Entity_Id
:= Etype
(N
);
12249 -- First and Last attribute reference nodes, which end up as left and
12250 -- right operands of the optimized result.
12253 -- True for comparison operand of zero
12256 -- Comparison operand, set only if Is_Zero is false
12259 -- Entity whose length is being compared
12262 -- Integer_Literal node for length attribute expression, or Empty
12263 -- if there is no such expression present.
12266 -- Type of array index to which 'Length is applied
12268 Op
: Node_Kind
:= Nkind
(N
);
12269 -- Kind of comparison operator, gets flipped if operands backwards
12271 function Is_Optimizable
(N
: Node_Id
) return Boolean;
12272 -- Tests N to see if it is an optimizable comparison value (defined as
12273 -- constant zero or one, or something else where the value is known to
12274 -- be positive and in the range of 32-bits, and where the corresponding
12275 -- Length value is also known to be 32-bits. If result is true, sets
12276 -- Is_Zero, Ityp, and Comp accordingly.
12278 function Is_Entity_Length
(N
: Node_Id
) return Boolean;
12279 -- Tests if N is a length attribute applied to a simple entity. If so,
12280 -- returns True, and sets Ent to the entity, and Index to the integer
12281 -- literal provided as an attribute expression, or to Empty if none.
12282 -- Also returns True if the expression is a generated type conversion
12283 -- whose expression is of the desired form. This latter case arises
12284 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
12285 -- to check for being in range, which is not needed in this context.
12286 -- Returns False if neither condition holds.
12288 function Prepare_64
(N
: Node_Id
) return Node_Id
;
12289 -- Given a discrete expression, returns a Long_Long_Integer typed
12290 -- expression representing the underlying value of the expression.
12291 -- This is done with an unchecked conversion to the result type. We
12292 -- use unchecked conversion to handle the enumeration type case.
12294 ----------------------
12295 -- Is_Entity_Length --
12296 ----------------------
12298 function Is_Entity_Length
(N
: Node_Id
) return Boolean is
12300 if Nkind
(N
) = N_Attribute_Reference
12301 and then Attribute_Name
(N
) = Name_Length
12302 and then Is_Entity_Name
(Prefix
(N
))
12304 Ent
:= Entity
(Prefix
(N
));
12306 if Present
(Expressions
(N
)) then
12307 Index
:= First
(Expressions
(N
));
12314 elsif Nkind
(N
) = N_Type_Conversion
12315 and then not Comes_From_Source
(N
)
12317 return Is_Entity_Length
(Expression
(N
));
12322 end Is_Entity_Length
;
12324 --------------------
12325 -- Is_Optimizable --
12326 --------------------
12328 function Is_Optimizable
(N
: Node_Id
) return Boolean is
12336 if Compile_Time_Known_Value
(N
) then
12337 Val
:= Expr_Value
(N
);
12339 if Val
= Uint_0
then
12344 elsif Val
= Uint_1
then
12351 -- Here we have to make sure of being within 32-bits
12353 Determine_Range
(N
, OK
, Lo
, Hi
, Assume_Valid
=> True);
12356 or else Lo
< Uint_1
12357 or else Hi
> UI_From_Int
(Int
'Last)
12362 -- Comparison value was within range, so now we must check the index
12363 -- value to make sure it is also within 32-bits.
12365 Indx
:= First_Index
(Etype
(Ent
));
12367 if Present
(Index
) then
12368 for J
in 2 .. UI_To_Int
(Intval
(Index
)) loop
12373 Ityp
:= Etype
(Indx
);
12375 if Esize
(Ityp
) > 32 then
12382 end Is_Optimizable
;
12388 function Prepare_64
(N
: Node_Id
) return Node_Id
is
12390 return Unchecked_Convert_To
(Standard_Long_Long_Integer
, N
);
12393 -- Start of processing for Optimize_Length_Comparison
12396 -- Nothing to do if not a comparison
12398 if Op
not in N_Op_Compare
then
12402 -- Nothing to do if special -gnatd.P debug flag set
12404 if Debug_Flag_Dot_PP
then
12408 -- Ent'Length op 0/1
12410 if Is_Entity_Length
(Left_Opnd
(N
))
12411 and then Is_Optimizable
(Right_Opnd
(N
))
12415 -- 0/1 op Ent'Length
12417 elsif Is_Entity_Length
(Right_Opnd
(N
))
12418 and then Is_Optimizable
(Left_Opnd
(N
))
12420 -- Flip comparison to opposite sense
12423 when N_Op_Lt
=> Op
:= N_Op_Gt
;
12424 when N_Op_Le
=> Op
:= N_Op_Ge
;
12425 when N_Op_Gt
=> Op
:= N_Op_Lt
;
12426 when N_Op_Ge
=> Op
:= N_Op_Le
;
12427 when others => null;
12430 -- Else optimization not possible
12436 -- Fall through if we will do the optimization
12438 -- Cases to handle:
12440 -- X'Length = 0 => X'First > X'Last
12441 -- X'Length = 1 => X'First = X'Last
12442 -- X'Length = n => X'First + (n - 1) = X'Last
12444 -- X'Length /= 0 => X'First <= X'Last
12445 -- X'Length /= 1 => X'First /= X'Last
12446 -- X'Length /= n => X'First + (n - 1) /= X'Last
12448 -- X'Length >= 0 => always true, warn
12449 -- X'Length >= 1 => X'First <= X'Last
12450 -- X'Length >= n => X'First + (n - 1) <= X'Last
12452 -- X'Length > 0 => X'First <= X'Last
12453 -- X'Length > 1 => X'First < X'Last
12454 -- X'Length > n => X'First + (n - 1) < X'Last
12456 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
12457 -- X'Length <= 1 => X'First >= X'Last
12458 -- X'Length <= n => X'First + (n - 1) >= X'Last
12460 -- X'Length < 0 => always false (warn)
12461 -- X'Length < 1 => X'First > X'Last
12462 -- X'Length < n => X'First + (n - 1) > X'Last
12464 -- Note: for the cases of n (not constant 0,1), we require that the
12465 -- corresponding index type be integer or shorter (i.e. not 64-bit),
12466 -- and the same for the comparison value. Then we do the comparison
12467 -- using 64-bit arithmetic (actually long long integer), so that we
12468 -- cannot have overflow intefering with the result.
12470 -- First deal with warning cases
12479 Convert_To
(Typ
, New_Occurrence_Of
(Standard_True
, Loc
)));
12480 Analyze_And_Resolve
(N
, Typ
);
12481 Warn_On_Known_Condition
(N
);
12488 Convert_To
(Typ
, New_Occurrence_Of
(Standard_False
, Loc
)));
12489 Analyze_And_Resolve
(N
, Typ
);
12490 Warn_On_Known_Condition
(N
);
12494 if Constant_Condition_Warnings
12495 and then Comes_From_Source
(Original_Node
(N
))
12497 Error_Msg_N
("could replace by ""'=""?c?", N
);
12507 -- Build the First reference we will use
12510 Make_Attribute_Reference
(Loc
,
12511 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
12512 Attribute_Name
=> Name_First
);
12514 if Present
(Index
) then
12515 Set_Expressions
(Left
, New_List
(New_Copy
(Index
)));
12518 -- If general value case, then do the addition of (n - 1), and
12519 -- also add the needed conversions to type Long_Long_Integer.
12521 if Present
(Comp
) then
12524 Left_Opnd
=> Prepare_64
(Left
),
12526 Make_Op_Subtract
(Loc
,
12527 Left_Opnd
=> Prepare_64
(Comp
),
12528 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)));
12531 -- Build the Last reference we will use
12534 Make_Attribute_Reference
(Loc
,
12535 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
12536 Attribute_Name
=> Name_Last
);
12538 if Present
(Index
) then
12539 Set_Expressions
(Right
, New_List
(New_Copy
(Index
)));
12542 -- If general operand, convert Last reference to Long_Long_Integer
12544 if Present
(Comp
) then
12545 Right
:= Prepare_64
(Right
);
12548 -- Check for cases to optimize
12550 -- X'Length = 0 => X'First > X'Last
12551 -- X'Length < 1 => X'First > X'Last
12552 -- X'Length < n => X'First + (n - 1) > X'Last
12554 if (Is_Zero
and then Op
= N_Op_Eq
)
12555 or else (not Is_Zero
and then Op
= N_Op_Lt
)
12560 Right_Opnd
=> Right
);
12562 -- X'Length = 1 => X'First = X'Last
12563 -- X'Length = n => X'First + (n - 1) = X'Last
12565 elsif not Is_Zero
and then Op
= N_Op_Eq
then
12569 Right_Opnd
=> Right
);
12571 -- X'Length /= 0 => X'First <= X'Last
12572 -- X'Length > 0 => X'First <= X'Last
12574 elsif Is_Zero
and (Op
= N_Op_Ne
or else Op
= N_Op_Gt
) then
12578 Right_Opnd
=> Right
);
12580 -- X'Length /= 1 => X'First /= X'Last
12581 -- X'Length /= n => X'First + (n - 1) /= X'Last
12583 elsif not Is_Zero
and then Op
= N_Op_Ne
then
12587 Right_Opnd
=> Right
);
12589 -- X'Length >= 1 => X'First <= X'Last
12590 -- X'Length >= n => X'First + (n - 1) <= X'Last
12592 elsif not Is_Zero
and then Op
= N_Op_Ge
then
12596 Right_Opnd
=> Right
);
12598 -- X'Length > 1 => X'First < X'Last
12599 -- X'Length > n => X'First + (n = 1) < X'Last
12601 elsif not Is_Zero
and then Op
= N_Op_Gt
then
12605 Right_Opnd
=> Right
);
12607 -- X'Length <= 1 => X'First >= X'Last
12608 -- X'Length <= n => X'First + (n - 1) >= X'Last
12610 elsif not Is_Zero
and then Op
= N_Op_Le
then
12614 Right_Opnd
=> Right
);
12616 -- Should not happen at this stage
12619 raise Program_Error
;
12622 -- Rewrite and finish up
12624 Rewrite
(N
, Result
);
12625 Analyze_And_Resolve
(N
, Typ
);
12627 end Optimize_Length_Comparison
;
12629 ------------------------------
12630 -- Process_Transient_Object --
12631 ------------------------------
12633 procedure Process_Transient_Object
12635 Rel_Node
: Node_Id
)
12637 Loc
: constant Source_Ptr
:= Sloc
(Decl
);
12638 Obj_Id
: constant Entity_Id
:= Defining_Identifier
(Decl
);
12639 Obj_Typ
: constant Node_Id
:= Etype
(Obj_Id
);
12640 Desig_Typ
: Entity_Id
;
12642 Fin_Stmts
: List_Id
;
12643 Ptr_Id
: Entity_Id
;
12644 Temp_Id
: Entity_Id
;
12645 Temp_Ins
: Node_Id
;
12647 Hook_Context
: constant Node_Id
:= Find_Hook_Context
(Rel_Node
);
12648 -- Node on which to insert the hook pointer (as an action): the
12649 -- innermost enclosing non-transient scope.
12651 Finalization_Context
: Node_Id
;
12652 -- Node after which to insert finalization actions
12654 Finalize_Always
: Boolean;
12655 -- If False, call to finalizer includes a test of whether the hook
12656 -- pointer is null.
12659 -- Step 0: determine where to attach finalization actions in the tree
12661 -- Special case for Boolean EWAs: capture expression in a temporary,
12662 -- whose declaration will serve as the context around which to insert
12663 -- finalization code. The finalization thus remains local to the
12664 -- specific condition being evaluated.
12666 if Is_Boolean_Type
(Etype
(Rel_Node
)) then
12668 -- In this case, the finalization context is chosen so that we know
12669 -- at finalization point that the hook pointer is never null, so no
12670 -- need for a test, we can call the finalizer unconditionally, except
12671 -- in the case where the object is created in a specific branch of a
12672 -- conditional expression.
12675 not Within_Case_Or_If_Expression
(Rel_Node
)
12676 and then not Nkind_In
12677 (Original_Node
(Rel_Node
), N_Case_Expression
,
12681 Loc
: constant Source_Ptr
:= Sloc
(Rel_Node
);
12682 Temp
: constant Entity_Id
:= Make_Temporary
(Loc
, 'E', Rel_Node
);
12685 Append_To
(Actions
(Rel_Node
),
12686 Make_Object_Declaration
(Loc
,
12687 Defining_Identifier
=> Temp
,
12688 Constant_Present
=> True,
12689 Object_Definition
=>
12690 New_Occurrence_Of
(Etype
(Rel_Node
), Loc
),
12691 Expression
=> Expression
(Rel_Node
)));
12692 Finalization_Context
:= Last
(Actions
(Rel_Node
));
12694 Analyze
(Last
(Actions
(Rel_Node
)));
12696 Set_Expression
(Rel_Node
, New_Occurrence_Of
(Temp
, Loc
));
12697 Analyze
(Expression
(Rel_Node
));
12701 Finalize_Always
:= False;
12702 Finalization_Context
:= Hook_Context
;
12705 -- Step 1: Create the access type which provides a reference to the
12706 -- transient controlled object.
12708 if Is_Access_Type
(Obj_Typ
) then
12709 Desig_Typ
:= Directly_Designated_Type
(Obj_Typ
);
12711 Desig_Typ
:= Obj_Typ
;
12714 Desig_Typ
:= Base_Type
(Desig_Typ
);
12717 -- Ann : access [all] <Desig_Typ>;
12719 Ptr_Id
:= Make_Temporary
(Loc
, 'A');
12721 Insert_Action
(Hook_Context
,
12722 Make_Full_Type_Declaration
(Loc
,
12723 Defining_Identifier
=> Ptr_Id
,
12725 Make_Access_To_Object_Definition
(Loc
,
12726 All_Present
=> Ekind
(Obj_Typ
) = E_General_Access_Type
,
12727 Subtype_Indication
=> New_Occurrence_Of
(Desig_Typ
, Loc
))));
12729 -- Step 2: Create a temporary which acts as a hook to the transient
12730 -- controlled object. Generate:
12732 -- Temp : Ptr_Id := null;
12734 Temp_Id
:= Make_Temporary
(Loc
, 'T');
12736 Insert_Action
(Hook_Context
,
12737 Make_Object_Declaration
(Loc
,
12738 Defining_Identifier
=> Temp_Id
,
12739 Object_Definition
=> New_Occurrence_Of
(Ptr_Id
, Loc
)));
12741 -- Mark the temporary as created for the purposes of exporting the
12742 -- transient controlled object out of the expression_with_action or if
12743 -- expression. This signals the machinery in Build_Finalizer to treat
12744 -- this case specially.
12746 Set_Status_Flag_Or_Transient_Decl
(Temp_Id
, Decl
);
12748 -- Step 3: Hook the transient object to the temporary
12750 -- This must be inserted right after the object declaration, so that
12751 -- the assignment is executed if, and only if, the object is actually
12752 -- created (whereas the declaration of the hook pointer, and the
12753 -- finalization call, may be inserted at an outer level, and may
12754 -- remain unused for some executions, if the actual creation of
12755 -- the object is conditional).
12757 -- The use of unchecked conversion / unrestricted access is needed to
12758 -- avoid an accessibility violation. Note that the finalization code is
12759 -- structured in such a way that the "hook" is processed only when it
12760 -- points to an existing object.
12762 if Is_Access_Type
(Obj_Typ
) then
12764 Unchecked_Convert_To
(Ptr_Id
, New_Occurrence_Of
(Obj_Id
, Loc
));
12767 Make_Attribute_Reference
(Loc
,
12768 Prefix
=> New_Occurrence_Of
(Obj_Id
, Loc
),
12769 Attribute_Name
=> Name_Unrestricted_Access
);
12773 -- Temp := Ptr_Id (Obj_Id);
12775 -- Temp := Obj_Id'Unrestricted_Access;
12777 -- When the transient object is initialized by an aggregate, the hook
12778 -- must capture the object after the last component assignment takes
12779 -- place. Only then is the object fully initialized.
12781 if Ekind
(Obj_Id
) = E_Variable
12782 and then Present
(Last_Aggregate_Assignment
(Obj_Id
))
12784 Temp_Ins
:= Last_Aggregate_Assignment
(Obj_Id
);
12786 -- Otherwise the hook seizes the related object immediately
12792 Insert_After_And_Analyze
(Temp_Ins
,
12793 Make_Assignment_Statement
(Loc
,
12794 Name
=> New_Occurrence_Of
(Temp_Id
, Loc
),
12795 Expression
=> Expr
));
12797 -- Step 4: Finalize the transient controlled object after the context
12798 -- has been evaluated/elaborated. Generate:
12800 -- if Temp /= null then
12801 -- [Deep_]Finalize (Temp.all);
12805 -- When the node is part of a return statement, there is no need to
12806 -- insert a finalization call, as the general finalization mechanism
12807 -- (see Build_Finalizer) would take care of the transient controlled
12808 -- object on subprogram exit. Note that it would also be impossible to
12809 -- insert the finalization code after the return statement as this will
12810 -- render it unreachable.
12812 if Nkind
(Finalization_Context
) /= N_Simple_Return_Statement
then
12813 Fin_Stmts
:= New_List
(
12816 Make_Explicit_Dereference
(Loc
,
12817 Prefix
=> New_Occurrence_Of
(Temp_Id
, Loc
)),
12820 Make_Assignment_Statement
(Loc
,
12821 Name
=> New_Occurrence_Of
(Temp_Id
, Loc
),
12822 Expression
=> Make_Null
(Loc
)));
12824 if not Finalize_Always
then
12825 Fin_Stmts
:= New_List
(
12826 Make_Implicit_If_Statement
(Decl
,
12829 Left_Opnd
=> New_Occurrence_Of
(Temp_Id
, Loc
),
12830 Right_Opnd
=> Make_Null
(Loc
)),
12831 Then_Statements
=> Fin_Stmts
));
12834 Insert_Actions_After
(Finalization_Context
, Fin_Stmts
);
12836 end Process_Transient_Object
;
12838 ------------------------
12839 -- Rewrite_Comparison --
12840 ------------------------
12842 procedure Rewrite_Comparison
(N
: Node_Id
) is
12843 Warning_Generated
: Boolean := False;
12844 -- Set to True if first pass with Assume_Valid generates a warning in
12845 -- which case we skip the second pass to avoid warning overloaded.
12848 -- Set to Standard_True or Standard_False
12851 if Nkind
(N
) = N_Type_Conversion
then
12852 Rewrite_Comparison
(Expression
(N
));
12855 elsif Nkind
(N
) not in N_Op_Compare
then
12859 -- Now start looking at the comparison in detail. We potentially go
12860 -- through this loop twice. The first time, Assume_Valid is set False
12861 -- in the call to Compile_Time_Compare. If this call results in a
12862 -- clear result of always True or Always False, that's decisive and
12863 -- we are done. Otherwise we repeat the processing with Assume_Valid
12864 -- set to True to generate additional warnings. We can skip that step
12865 -- if Constant_Condition_Warnings is False.
12867 for AV
in False .. True loop
12869 Typ
: constant Entity_Id
:= Etype
(N
);
12870 Op1
: constant Node_Id
:= Left_Opnd
(N
);
12871 Op2
: constant Node_Id
:= Right_Opnd
(N
);
12873 Res
: constant Compare_Result
:=
12874 Compile_Time_Compare
(Op1
, Op2
, Assume_Valid
=> AV
);
12875 -- Res indicates if compare outcome can be compile time determined
12877 True_Result
: Boolean;
12878 False_Result
: Boolean;
12881 case N_Op_Compare
(Nkind
(N
)) is
12883 True_Result
:= Res
= EQ
;
12884 False_Result
:= Res
= LT
or else Res
= GT
or else Res
= NE
;
12887 True_Result
:= Res
in Compare_GE
;
12888 False_Result
:= Res
= LT
;
12891 and then Constant_Condition_Warnings
12892 and then Comes_From_Source
(Original_Node
(N
))
12893 and then Nkind
(Original_Node
(N
)) = N_Op_Ge
12894 and then not In_Instance
12895 and then Is_Integer_Type
(Etype
(Left_Opnd
(N
)))
12896 and then not Has_Warnings_Off
(Etype
(Left_Opnd
(N
)))
12899 ("can never be greater than, could replace by ""'=""?c?",
12901 Warning_Generated
:= True;
12905 True_Result
:= Res
= GT
;
12906 False_Result
:= Res
in Compare_LE
;
12909 True_Result
:= Res
= LT
;
12910 False_Result
:= Res
in Compare_GE
;
12913 True_Result
:= Res
in Compare_LE
;
12914 False_Result
:= Res
= GT
;
12917 and then Constant_Condition_Warnings
12918 and then Comes_From_Source
(Original_Node
(N
))
12919 and then Nkind
(Original_Node
(N
)) = N_Op_Le
12920 and then not In_Instance
12921 and then Is_Integer_Type
(Etype
(Left_Opnd
(N
)))
12922 and then not Has_Warnings_Off
(Etype
(Left_Opnd
(N
)))
12925 ("can never be less than, could replace by ""'=""?c?", N
);
12926 Warning_Generated
:= True;
12930 True_Result
:= Res
= NE
or else Res
= GT
or else Res
= LT
;
12931 False_Result
:= Res
= EQ
;
12934 -- If this is the first iteration, then we actually convert the
12935 -- comparison into True or False, if the result is certain.
12938 if True_Result
or False_Result
then
12939 Result
:= Boolean_Literals
(True_Result
);
12942 New_Occurrence_Of
(Result
, Sloc
(N
))));
12943 Analyze_And_Resolve
(N
, Typ
);
12944 Warn_On_Known_Condition
(N
);
12948 -- If this is the second iteration (AV = True), and the original
12949 -- node comes from source and we are not in an instance, then give
12950 -- a warning if we know result would be True or False. Note: we
12951 -- know Constant_Condition_Warnings is set if we get here.
12953 elsif Comes_From_Source
(Original_Node
(N
))
12954 and then not In_Instance
12956 if True_Result
then
12958 ("condition can only be False if invalid values present??",
12960 elsif False_Result
then
12962 ("condition can only be True if invalid values present??",
12968 -- Skip second iteration if not warning on constant conditions or
12969 -- if the first iteration already generated a warning of some kind or
12970 -- if we are in any case assuming all values are valid (so that the
12971 -- first iteration took care of the valid case).
12973 exit when not Constant_Condition_Warnings
;
12974 exit when Warning_Generated
;
12975 exit when Assume_No_Invalid_Values
;
12977 end Rewrite_Comparison
;
12979 ----------------------------
12980 -- Safe_In_Place_Array_Op --
12981 ----------------------------
12983 function Safe_In_Place_Array_Op
12986 Op2
: Node_Id
) return Boolean
12988 Target
: Entity_Id
;
12990 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean;
12991 -- Operand is safe if it cannot overlap part of the target of the
12992 -- operation. If the operand and the target are identical, the operand
12993 -- is safe. The operand can be empty in the case of negation.
12995 function Is_Unaliased
(N
: Node_Id
) return Boolean;
12996 -- Check that N is a stand-alone entity
13002 function Is_Unaliased
(N
: Node_Id
) return Boolean is
13006 and then No
(Address_Clause
(Entity
(N
)))
13007 and then No
(Renamed_Object
(Entity
(N
)));
13010 ---------------------
13011 -- Is_Safe_Operand --
13012 ---------------------
13014 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean is
13019 elsif Is_Entity_Name
(Op
) then
13020 return Is_Unaliased
(Op
);
13022 elsif Nkind_In
(Op
, N_Indexed_Component
, N_Selected_Component
) then
13023 return Is_Unaliased
(Prefix
(Op
));
13025 elsif Nkind
(Op
) = N_Slice
then
13027 Is_Unaliased
(Prefix
(Op
))
13028 and then Entity
(Prefix
(Op
)) /= Target
;
13030 elsif Nkind
(Op
) = N_Op_Not
then
13031 return Is_Safe_Operand
(Right_Opnd
(Op
));
13036 end Is_Safe_Operand
;
13038 -- Start of processing for Safe_In_Place_Array_Op
13041 -- Skip this processing if the component size is different from system
13042 -- storage unit (since at least for NOT this would cause problems).
13044 if Component_Size
(Etype
(Lhs
)) /= System_Storage_Unit
then
13047 -- Cannot do in place stuff on VM_Target since cannot pass addresses
13049 elsif VM_Target
/= No_VM
then
13052 -- Cannot do in place stuff if non-standard Boolean representation
13054 elsif Has_Non_Standard_Rep
(Component_Type
(Etype
(Lhs
))) then
13057 elsif not Is_Unaliased
(Lhs
) then
13061 Target
:= Entity
(Lhs
);
13062 return Is_Safe_Operand
(Op1
) and then Is_Safe_Operand
(Op2
);
13064 end Safe_In_Place_Array_Op
;
13066 -----------------------
13067 -- Tagged_Membership --
13068 -----------------------
13070 -- There are two different cases to consider depending on whether the right
13071 -- operand is a class-wide type or not. If not we just compare the actual
13072 -- tag of the left expr to the target type tag:
13074 -- Left_Expr.Tag = Right_Type'Tag;
13076 -- If it is a class-wide type we use the RT function CW_Membership which is
13077 -- usually implemented by looking in the ancestor tables contained in the
13078 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
13080 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
13081 -- function IW_Membership which is usually implemented by looking in the
13082 -- table of abstract interface types plus the ancestor table contained in
13083 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
13085 procedure Tagged_Membership
13087 SCIL_Node
: out Node_Id
;
13088 Result
: out Node_Id
)
13090 Left
: constant Node_Id
:= Left_Opnd
(N
);
13091 Right
: constant Node_Id
:= Right_Opnd
(N
);
13092 Loc
: constant Source_Ptr
:= Sloc
(N
);
13094 Full_R_Typ
: Entity_Id
;
13095 Left_Type
: Entity_Id
;
13096 New_Node
: Node_Id
;
13097 Right_Type
: Entity_Id
;
13101 SCIL_Node
:= Empty
;
13103 -- Handle entities from the limited view
13105 Left_Type
:= Available_View
(Etype
(Left
));
13106 Right_Type
:= Available_View
(Etype
(Right
));
13108 -- In the case where the type is an access type, the test is applied
13109 -- using the designated types (needed in Ada 2012 for implicit anonymous
13110 -- access conversions, for AI05-0149).
13112 if Is_Access_Type
(Right_Type
) then
13113 Left_Type
:= Designated_Type
(Left_Type
);
13114 Right_Type
:= Designated_Type
(Right_Type
);
13117 if Is_Class_Wide_Type
(Left_Type
) then
13118 Left_Type
:= Root_Type
(Left_Type
);
13121 if Is_Class_Wide_Type
(Right_Type
) then
13122 Full_R_Typ
:= Underlying_Type
(Root_Type
(Right_Type
));
13124 Full_R_Typ
:= Underlying_Type
(Right_Type
);
13128 Make_Selected_Component
(Loc
,
13129 Prefix
=> Relocate_Node
(Left
),
13131 New_Occurrence_Of
(First_Tag_Component
(Left_Type
), Loc
));
13133 if Is_Class_Wide_Type
(Right_Type
) then
13135 -- No need to issue a run-time check if we statically know that the
13136 -- result of this membership test is always true. For example,
13137 -- considering the following declarations:
13139 -- type Iface is interface;
13140 -- type T is tagged null record;
13141 -- type DT is new T and Iface with null record;
13146 -- These membership tests are always true:
13149 -- Obj2 in T'Class;
13150 -- Obj2 in Iface'Class;
13152 -- We do not need to handle cases where the membership is illegal.
13155 -- Obj1 in DT'Class; -- Compile time error
13156 -- Obj1 in Iface'Class; -- Compile time error
13158 if not Is_Class_Wide_Type
(Left_Type
)
13159 and then (Is_Ancestor
(Etype
(Right_Type
), Left_Type
,
13160 Use_Full_View
=> True)
13161 or else (Is_Interface
(Etype
(Right_Type
))
13162 and then Interface_Present_In_Ancestor
13164 Iface
=> Etype
(Right_Type
))))
13166 Result
:= New_Occurrence_Of
(Standard_True
, Loc
);
13170 -- Ada 2005 (AI-251): Class-wide applied to interfaces
13172 if Is_Interface
(Etype
(Class_Wide_Type
(Right_Type
)))
13174 -- Support to: "Iface_CW_Typ in Typ'Class"
13176 or else Is_Interface
(Left_Type
)
13178 -- Issue error if IW_Membership operation not available in a
13179 -- configurable run time setting.
13181 if not RTE_Available
(RE_IW_Membership
) then
13183 ("dynamic membership test on interface types", N
);
13189 Make_Function_Call
(Loc
,
13190 Name
=> New_Occurrence_Of
(RTE
(RE_IW_Membership
), Loc
),
13191 Parameter_Associations
=> New_List
(
13192 Make_Attribute_Reference
(Loc
,
13194 Attribute_Name
=> Name_Address
),
13195 New_Occurrence_Of
(
13196 Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))),
13199 -- Ada 95: Normal case
13202 Build_CW_Membership
(Loc
,
13203 Obj_Tag_Node
=> Obj_Tag
,
13205 New_Occurrence_Of
(
13206 Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))), Loc
),
13208 New_Node
=> New_Node
);
13210 -- Generate the SCIL node for this class-wide membership test.
13211 -- Done here because the previous call to Build_CW_Membership
13212 -- relocates Obj_Tag.
13214 if Generate_SCIL
then
13215 SCIL_Node
:= Make_SCIL_Membership_Test
(Sloc
(N
));
13216 Set_SCIL_Entity
(SCIL_Node
, Etype
(Right_Type
));
13217 Set_SCIL_Tag_Value
(SCIL_Node
, Obj_Tag
);
13220 Result
:= New_Node
;
13223 -- Right_Type is not a class-wide type
13226 -- No need to check the tag of the object if Right_Typ is abstract
13228 if Is_Abstract_Type
(Right_Type
) then
13229 Result
:= New_Occurrence_Of
(Standard_False
, Loc
);
13234 Left_Opnd
=> Obj_Tag
,
13237 (Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))), Loc
));
13240 end Tagged_Membership
;
13242 ------------------------------
13243 -- Unary_Op_Validity_Checks --
13244 ------------------------------
13246 procedure Unary_Op_Validity_Checks
(N
: Node_Id
) is
13248 if Validity_Checks_On
and Validity_Check_Operands
then
13249 Ensure_Valid
(Right_Opnd
(N
));
13251 end Unary_Op_Validity_Checks
;