1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2012, 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 Exp_VFpt
; use Exp_VFpt
;
46 with Freeze
; use Freeze
;
47 with Inline
; use Inline
;
49 with Namet
; use Namet
;
50 with Nlists
; use Nlists
;
51 with Nmake
; use Nmake
;
53 with Par_SCO
; use Par_SCO
;
54 with Restrict
; use Restrict
;
55 with Rident
; use Rident
;
56 with Rtsfind
; use Rtsfind
;
58 with Sem_Aux
; use Sem_Aux
;
59 with Sem_Cat
; use Sem_Cat
;
60 with Sem_Ch3
; use Sem_Ch3
;
61 with Sem_Ch8
; use Sem_Ch8
;
62 with Sem_Ch13
; use Sem_Ch13
;
63 with Sem_Eval
; use Sem_Eval
;
64 with Sem_Res
; use Sem_Res
;
65 with Sem_Type
; use Sem_Type
;
66 with Sem_Util
; use Sem_Util
;
67 with Sem_Warn
; use Sem_Warn
;
68 with Sinfo
; use Sinfo
;
69 with Snames
; use Snames
;
70 with Stand
; use Stand
;
71 with SCIL_LL
; use SCIL_LL
;
72 with Targparm
; use Targparm
;
73 with Tbuild
; use Tbuild
;
74 with Ttypes
; use Ttypes
;
75 with Uintp
; use Uintp
;
76 with Urealp
; use Urealp
;
77 with Validsw
; use Validsw
;
79 package body Exp_Ch4
is
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 procedure Binary_Op_Validity_Checks
(N
: Node_Id
);
86 pragma Inline
(Binary_Op_Validity_Checks
);
87 -- Performs validity checks for a binary operator
89 procedure Build_Boolean_Array_Proc_Call
93 -- If a boolean array assignment can be done in place, build call to
94 -- corresponding library procedure.
96 function Current_Anonymous_Master
return Entity_Id
;
97 -- Return the entity of the heterogeneous finalization master belonging to
98 -- the current unit (either function, package or procedure). This master
99 -- services all anonymous access-to-controlled types. If the current unit
100 -- does not have such master, create one.
102 procedure Displace_Allocator_Pointer
(N
: Node_Id
);
103 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
104 -- Expand_Allocator_Expression. Allocating class-wide interface objects
105 -- this routine displaces the pointer to the allocated object to reference
106 -- the component referencing the corresponding secondary dispatch table.
108 procedure Expand_Allocator_Expression
(N
: Node_Id
);
109 -- Subsidiary to Expand_N_Allocator, for the case when the expression
110 -- is a qualified expression or an aggregate.
112 procedure Expand_Array_Comparison
(N
: Node_Id
);
113 -- This routine handles expansion of the comparison operators (N_Op_Lt,
114 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
115 -- code for these operators is similar, differing only in the details of
116 -- the actual comparison call that is made. Special processing (call a
119 function Expand_Array_Equality
124 Typ
: Entity_Id
) return Node_Id
;
125 -- Expand an array equality into a call to a function implementing this
126 -- equality, and a call to it. Loc is the location for the generated nodes.
127 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
128 -- on which to attach bodies of local functions that are created in the
129 -- process. It is the responsibility of the caller to insert those bodies
130 -- at the right place. Nod provides the Sloc value for the generated code.
131 -- Normally the types used for the generated equality routine are taken
132 -- from Lhs and Rhs. However, in some situations of generated code, the
133 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
134 -- the type to be used for the formal parameters.
136 procedure Expand_Boolean_Operator
(N
: Node_Id
);
137 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
138 -- case of array type arguments.
140 procedure Expand_Short_Circuit_Operator
(N
: Node_Id
);
141 -- Common expansion processing for short-circuit boolean operators
143 procedure Expand_Compare_Minimize_Eliminate_Overflow
(N
: Node_Id
);
144 -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is
145 -- where we allow comparison of "out of range" values.
147 function Expand_Composite_Equality
152 Bodies
: List_Id
) return Node_Id
;
153 -- Local recursive function used to expand equality for nested composite
154 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
155 -- to attach bodies of local functions that are created in the process.
156 -- It is the responsibility of the caller to insert those bodies at the
157 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
158 -- are the left and right sides for the comparison, and Typ is the type of
159 -- the objects to compare.
161 procedure Expand_Concatenate
(Cnode
: Node_Id
; Opnds
: List_Id
);
162 -- Routine to expand concatenation of a sequence of two or more operands
163 -- (in the list Operands) and replace node Cnode with the result of the
164 -- concatenation. The operands can be of any appropriate type, and can
165 -- include both arrays and singleton elements.
167 procedure Expand_Membership_Minimize_Eliminate_Overflow
(N
: Node_Id
);
168 -- N is an N_In membership test mode, with the overflow check mode set to
169 -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed
170 -- integer type. This is a case where top level processing is required to
171 -- handle overflow checks in subtrees.
173 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
);
174 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
175 -- fixed. We do not have such a type at runtime, so the purpose of this
176 -- routine is to find the real type by looking up the tree. We also
177 -- determine if the operation must be rounded.
179 function Has_Inferable_Discriminants
(N
: Node_Id
) return Boolean;
180 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
181 -- discriminants if it has a constrained nominal type, unless the object
182 -- is a component of an enclosing Unchecked_Union object that is subject
183 -- to a per-object constraint and the enclosing object lacks inferable
186 -- An expression of an Unchecked_Union type has inferable discriminants
187 -- if it is either a name of an object with inferable discriminants or a
188 -- qualified expression whose subtype mark denotes a constrained subtype.
190 procedure Insert_Dereference_Action
(N
: Node_Id
);
191 -- N is an expression whose type is an access. When the type of the
192 -- associated storage pool is derived from Checked_Pool, generate a
193 -- call to the 'Dereference' primitive operation.
195 function Make_Array_Comparison_Op
197 Nod
: Node_Id
) return Node_Id
;
198 -- Comparisons between arrays are expanded in line. This function produces
199 -- the body of the implementation of (a > b), where a and b are one-
200 -- dimensional arrays of some discrete type. The original node is then
201 -- expanded into the appropriate call to this function. Nod provides the
202 -- Sloc value for the generated code.
204 function Make_Boolean_Array_Op
206 N
: Node_Id
) return Node_Id
;
207 -- Boolean operations on boolean arrays are expanded in line. This function
208 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
209 -- b). It is used only the normal case and not the packed case. The type
210 -- involved, Typ, is the Boolean array type, and the logical operations in
211 -- the body are simple boolean operations. Note that Typ is always a
212 -- constrained type (the caller has ensured this by using
213 -- Convert_To_Actual_Subtype if necessary).
215 function Minimized_Eliminated_Overflow_Check
(N
: Node_Id
) return Boolean;
216 -- For signed arithmetic operations when the current overflow mode is
217 -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks
218 -- as the first thing we do. We then return. We count on the recursive
219 -- apparatus for overflow checks to call us back with an equivalent
220 -- operation that is in CHECKED mode, avoiding a recursive entry into this
221 -- routine, and that is when we will proceed with the expansion of the
222 -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do
223 -- these optimizations without first making this check, since there may be
224 -- operands further down the tree that are relying on the recursive calls
225 -- triggered by the top level nodes to properly process overflow checking
226 -- and remaining expansion on these nodes. Note that this call back may be
227 -- skipped if the operation is done in Bignum mode but that's fine, since
228 -- the Bignum call takes care of everything.
230 procedure Optimize_Length_Comparison
(N
: Node_Id
);
231 -- Given an expression, if it is of the form X'Length op N (or the other
232 -- way round), where N is known at compile time to be 0 or 1, and X is a
233 -- simple entity, and op is a comparison operator, optimizes it into a
234 -- comparison of First and Last.
236 procedure Rewrite_Comparison
(N
: Node_Id
);
237 -- If N is the node for a comparison whose outcome can be determined at
238 -- compile time, then the node N can be rewritten with True or False. If
239 -- the outcome cannot be determined at compile time, the call has no
240 -- effect. If N is a type conversion, then this processing is applied to
241 -- its expression. If N is neither comparison nor a type conversion, the
242 -- call has no effect.
244 procedure Tagged_Membership
246 SCIL_Node
: out Node_Id
;
247 Result
: out Node_Id
);
248 -- Construct the expression corresponding to the tagged membership test.
249 -- Deals with a second operand being (or not) a class-wide type.
251 function Safe_In_Place_Array_Op
254 Op2
: Node_Id
) return Boolean;
255 -- In the context of an assignment, where the right-hand side is a boolean
256 -- operation on arrays, check whether operation can be performed in place.
258 procedure Unary_Op_Validity_Checks
(N
: Node_Id
);
259 pragma Inline
(Unary_Op_Validity_Checks
);
260 -- Performs validity checks for a unary operator
262 -------------------------------
263 -- Binary_Op_Validity_Checks --
264 -------------------------------
266 procedure Binary_Op_Validity_Checks
(N
: Node_Id
) is
268 if Validity_Checks_On
and Validity_Check_Operands
then
269 Ensure_Valid
(Left_Opnd
(N
));
270 Ensure_Valid
(Right_Opnd
(N
));
272 end Binary_Op_Validity_Checks
;
274 ------------------------------------
275 -- Build_Boolean_Array_Proc_Call --
276 ------------------------------------
278 procedure Build_Boolean_Array_Proc_Call
283 Loc
: constant Source_Ptr
:= Sloc
(N
);
284 Kind
: constant Node_Kind
:= Nkind
(Expression
(N
));
285 Target
: constant Node_Id
:=
286 Make_Attribute_Reference
(Loc
,
288 Attribute_Name
=> Name_Address
);
290 Arg1
: Node_Id
:= Op1
;
291 Arg2
: Node_Id
:= Op2
;
293 Proc_Name
: Entity_Id
;
296 if Kind
= N_Op_Not
then
297 if Nkind
(Op1
) in N_Binary_Op
then
299 -- Use negated version of the binary operators
301 if Nkind
(Op1
) = N_Op_And
then
302 Proc_Name
:= RTE
(RE_Vector_Nand
);
304 elsif Nkind
(Op1
) = N_Op_Or
then
305 Proc_Name
:= RTE
(RE_Vector_Nor
);
307 else pragma Assert
(Nkind
(Op1
) = N_Op_Xor
);
308 Proc_Name
:= RTE
(RE_Vector_Xor
);
312 Make_Procedure_Call_Statement
(Loc
,
313 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
315 Parameter_Associations
=> New_List
(
317 Make_Attribute_Reference
(Loc
,
318 Prefix
=> Left_Opnd
(Op1
),
319 Attribute_Name
=> Name_Address
),
321 Make_Attribute_Reference
(Loc
,
322 Prefix
=> Right_Opnd
(Op1
),
323 Attribute_Name
=> Name_Address
),
325 Make_Attribute_Reference
(Loc
,
326 Prefix
=> Left_Opnd
(Op1
),
327 Attribute_Name
=> Name_Length
)));
330 Proc_Name
:= RTE
(RE_Vector_Not
);
333 Make_Procedure_Call_Statement
(Loc
,
334 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
335 Parameter_Associations
=> New_List
(
338 Make_Attribute_Reference
(Loc
,
340 Attribute_Name
=> Name_Address
),
342 Make_Attribute_Reference
(Loc
,
344 Attribute_Name
=> Name_Length
)));
348 -- We use the following equivalences:
350 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
351 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
352 -- (not X) xor (not Y) = X xor Y
353 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
355 if Nkind
(Op1
) = N_Op_Not
then
356 Arg1
:= Right_Opnd
(Op1
);
357 Arg2
:= Right_Opnd
(Op2
);
358 if Kind
= N_Op_And
then
359 Proc_Name
:= RTE
(RE_Vector_Nor
);
360 elsif Kind
= N_Op_Or
then
361 Proc_Name
:= RTE
(RE_Vector_Nand
);
363 Proc_Name
:= RTE
(RE_Vector_Xor
);
367 if Kind
= N_Op_And
then
368 Proc_Name
:= RTE
(RE_Vector_And
);
369 elsif Kind
= N_Op_Or
then
370 Proc_Name
:= RTE
(RE_Vector_Or
);
371 elsif Nkind
(Op2
) = N_Op_Not
then
372 Proc_Name
:= RTE
(RE_Vector_Nxor
);
373 Arg2
:= Right_Opnd
(Op2
);
375 Proc_Name
:= RTE
(RE_Vector_Xor
);
380 Make_Procedure_Call_Statement
(Loc
,
381 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
382 Parameter_Associations
=> New_List
(
384 Make_Attribute_Reference
(Loc
,
386 Attribute_Name
=> Name_Address
),
387 Make_Attribute_Reference
(Loc
,
389 Attribute_Name
=> Name_Address
),
390 Make_Attribute_Reference
(Loc
,
392 Attribute_Name
=> Name_Length
)));
395 Rewrite
(N
, Call_Node
);
399 when RE_Not_Available
=>
401 end Build_Boolean_Array_Proc_Call
;
403 ------------------------------
404 -- Current_Anonymous_Master --
405 ------------------------------
407 function Current_Anonymous_Master
return Entity_Id
is
415 Unit_Id
:= Cunit_Entity
(Current_Sem_Unit
);
417 -- Find the entity of the current unit
419 if Ekind
(Unit_Id
) = E_Subprogram_Body
then
421 -- When processing subprogram bodies, the proper scope is always that
424 Subp_Body
:= Unit_Id
;
425 while Present
(Subp_Body
)
426 and then Nkind
(Subp_Body
) /= N_Subprogram_Body
428 Subp_Body
:= Parent
(Subp_Body
);
431 Unit_Id
:= Corresponding_Spec
(Subp_Body
);
434 Loc
:= Sloc
(Unit_Id
);
435 Unit_Decl
:= Unit
(Cunit
(Current_Sem_Unit
));
437 -- Find the declarations list of the current unit
439 if Nkind
(Unit_Decl
) = N_Package_Declaration
then
440 Unit_Decl
:= Specification
(Unit_Decl
);
441 Decls
:= Visible_Declarations
(Unit_Decl
);
444 Decls
:= New_List
(Make_Null_Statement
(Loc
));
445 Set_Visible_Declarations
(Unit_Decl
, Decls
);
447 elsif Is_Empty_List
(Decls
) then
448 Append_To
(Decls
, Make_Null_Statement
(Loc
));
452 Decls
:= Declarations
(Unit_Decl
);
455 Decls
:= New_List
(Make_Null_Statement
(Loc
));
456 Set_Declarations
(Unit_Decl
, Decls
);
458 elsif Is_Empty_List
(Decls
) then
459 Append_To
(Decls
, Make_Null_Statement
(Loc
));
463 -- The current unit has an existing anonymous master, traverse its
464 -- declarations and locate the entity.
466 if Has_Anonymous_Master
(Unit_Id
) then
469 Fin_Mas_Id
: Entity_Id
;
472 Decl
:= First
(Decls
);
473 while Present
(Decl
) loop
475 -- Look for the first variable in the declarations whole type
476 -- is Finalization_Master.
478 if Nkind
(Decl
) = N_Object_Declaration
then
479 Fin_Mas_Id
:= Defining_Identifier
(Decl
);
481 if Ekind
(Fin_Mas_Id
) = E_Variable
482 and then Etype
(Fin_Mas_Id
) = RTE
(RE_Finalization_Master
)
491 -- The master was not found even though the unit was labeled as
497 -- Create a new anonymous master
501 First_Decl
: constant Node_Id
:= First
(Decls
);
503 Fin_Mas_Id
: Entity_Id
;
506 -- Since the master and its associated initialization is inserted
507 -- at top level, use the scope of the unit when analyzing.
509 Push_Scope
(Unit_Id
);
511 -- Create the finalization master
514 Make_Defining_Identifier
(Loc
,
515 Chars
=> New_External_Name
(Chars
(Unit_Id
), "AM"));
518 -- <Fin_Mas_Id> : Finalization_Master;
521 Make_Object_Declaration
(Loc
,
522 Defining_Identifier
=> Fin_Mas_Id
,
524 New_Reference_To
(RTE
(RE_Finalization_Master
), Loc
));
526 Insert_Before_And_Analyze
(First_Decl
, Action
);
528 -- Mark the unit to prevent the generation of multiple masters
530 Set_Has_Anonymous_Master
(Unit_Id
);
532 -- Do not set the base pool and mode of operation on .NET/JVM
533 -- since those targets do not support pools and all VM masters
534 -- are heterogeneous by default.
536 if VM_Target
= No_VM
then
540 -- (<Fin_Mas_Id>, Global_Pool_Object'Unrestricted_Access);
543 Make_Procedure_Call_Statement
(Loc
,
545 New_Reference_To
(RTE
(RE_Set_Base_Pool
), Loc
),
547 Parameter_Associations
=> New_List
(
548 New_Reference_To
(Fin_Mas_Id
, Loc
),
549 Make_Attribute_Reference
(Loc
,
551 New_Reference_To
(RTE
(RE_Global_Pool_Object
), Loc
),
552 Attribute_Name
=> Name_Unrestricted_Access
)));
554 Insert_Before_And_Analyze
(First_Decl
, Action
);
557 -- Set_Is_Heterogeneous (<Fin_Mas_Id>);
560 Make_Procedure_Call_Statement
(Loc
,
562 New_Reference_To
(RTE
(RE_Set_Is_Heterogeneous
), Loc
),
563 Parameter_Associations
=> New_List
(
564 New_Reference_To
(Fin_Mas_Id
, Loc
)));
566 Insert_Before_And_Analyze
(First_Decl
, Action
);
569 -- Restore the original state of the scope stack
576 end Current_Anonymous_Master
;
578 --------------------------------
579 -- Displace_Allocator_Pointer --
580 --------------------------------
582 procedure Displace_Allocator_Pointer
(N
: Node_Id
) is
583 Loc
: constant Source_Ptr
:= Sloc
(N
);
584 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
590 -- Do nothing in case of VM targets: the virtual machine will handle
591 -- interfaces directly.
593 if not Tagged_Type_Expansion
then
597 pragma Assert
(Nkind
(N
) = N_Identifier
598 and then Nkind
(Orig_Node
) = N_Allocator
);
600 PtrT
:= Etype
(Orig_Node
);
601 Dtyp
:= Available_View
(Designated_Type
(PtrT
));
602 Etyp
:= Etype
(Expression
(Orig_Node
));
604 if Is_Class_Wide_Type
(Dtyp
)
605 and then Is_Interface
(Dtyp
)
607 -- If the type of the allocator expression is not an interface type
608 -- we can generate code to reference the record component containing
609 -- the pointer to the secondary dispatch table.
611 if not Is_Interface
(Etyp
) then
613 Saved_Typ
: constant Entity_Id
:= Etype
(Orig_Node
);
616 -- 1) Get access to the allocated object
619 Make_Explicit_Dereference
(Loc
, Relocate_Node
(N
)));
623 -- 2) Add the conversion to displace the pointer to reference
624 -- the secondary dispatch table.
626 Rewrite
(N
, Convert_To
(Dtyp
, Relocate_Node
(N
)));
627 Analyze_And_Resolve
(N
, Dtyp
);
629 -- 3) The 'access to the secondary dispatch table will be used
630 -- as the value returned by the allocator.
633 Make_Attribute_Reference
(Loc
,
634 Prefix
=> Relocate_Node
(N
),
635 Attribute_Name
=> Name_Access
));
636 Set_Etype
(N
, Saved_Typ
);
640 -- If the type of the allocator expression is an interface type we
641 -- generate a run-time call to displace "this" to reference the
642 -- component containing the pointer to the secondary dispatch table
643 -- or else raise Constraint_Error if the actual object does not
644 -- implement the target interface. This case corresponds with the
645 -- following example:
647 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
649 -- return new Iface_2'Class'(Obj);
654 Unchecked_Convert_To
(PtrT
,
655 Make_Function_Call
(Loc
,
656 Name
=> New_Reference_To
(RTE
(RE_Displace
), Loc
),
657 Parameter_Associations
=> New_List
(
658 Unchecked_Convert_To
(RTE
(RE_Address
),
664 (Access_Disp_Table
(Etype
(Base_Type
(Dtyp
))))),
666 Analyze_And_Resolve
(N
, PtrT
);
669 end Displace_Allocator_Pointer
;
671 ---------------------------------
672 -- Expand_Allocator_Expression --
673 ---------------------------------
675 procedure Expand_Allocator_Expression
(N
: Node_Id
) is
676 Loc
: constant Source_Ptr
:= Sloc
(N
);
677 Exp
: constant Node_Id
:= Expression
(Expression
(N
));
678 PtrT
: constant Entity_Id
:= Etype
(N
);
679 DesigT
: constant Entity_Id
:= Designated_Type
(PtrT
);
681 procedure Apply_Accessibility_Check
683 Built_In_Place
: Boolean := False);
684 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
685 -- type, generate an accessibility check to verify that the level of the
686 -- type of the created object is not deeper than the level of the access
687 -- type. If the type of the qualified expression is class-wide, then
688 -- always generate the check (except in the case where it is known to be
689 -- unnecessary, see comment below). Otherwise, only generate the check
690 -- if the level of the qualified expression type is statically deeper
691 -- than the access type.
693 -- Although the static accessibility will generally have been performed
694 -- as a legality check, it won't have been done in cases where the
695 -- allocator appears in generic body, so a run-time check is needed in
696 -- general. One special case is when the access type is declared in the
697 -- same scope as the class-wide allocator, in which case the check can
698 -- never fail, so it need not be generated.
700 -- As an open issue, there seem to be cases where the static level
701 -- associated with the class-wide object's underlying type is not
702 -- sufficient to perform the proper accessibility check, such as for
703 -- allocators in nested subprograms or accept statements initialized by
704 -- class-wide formals when the actual originates outside at a deeper
705 -- static level. The nested subprogram case might require passing
706 -- accessibility levels along with class-wide parameters, and the task
707 -- case seems to be an actual gap in the language rules that needs to
708 -- be fixed by the ARG. ???
710 -------------------------------
711 -- Apply_Accessibility_Check --
712 -------------------------------
714 procedure Apply_Accessibility_Check
716 Built_In_Place
: Boolean := False)
718 Pool_Id
: constant Entity_Id
:= Associated_Storage_Pool
(PtrT
);
725 if Ada_Version
>= Ada_2005
726 and then Is_Class_Wide_Type
(DesigT
)
727 and then not Scope_Suppress
.Suppress
(Accessibility_Check
)
729 (Type_Access_Level
(Etype
(Exp
)) > Type_Access_Level
(PtrT
)
731 (Is_Class_Wide_Type
(Etype
(Exp
))
732 and then Scope
(PtrT
) /= Current_Scope
))
733 and then (Tagged_Type_Expansion
or else VM_Target
/= No_VM
)
735 -- If the allocator was built in place, Ref is already a reference
736 -- to the access object initialized to the result of the allocator
737 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
738 -- Remove_Side_Effects for cases where the build-in-place call may
739 -- still be the prefix of the reference (to avoid generating
740 -- duplicate calls). Otherwise, it is the entity associated with
741 -- the object containing the address of the allocated object.
743 if Built_In_Place
then
744 Remove_Side_Effects
(Ref
);
745 Obj_Ref
:= New_Copy
(Ref
);
747 Obj_Ref
:= New_Reference_To
(Ref
, Loc
);
750 -- Step 1: Create the object clean up code
754 -- Create an explicit free statement to clean up the allocated
755 -- object in case the accessibility check fails. Generate:
759 Free_Stmt
:= Make_Free_Statement
(Loc
, New_Copy
(Obj_Ref
));
760 Set_Storage_Pool
(Free_Stmt
, Pool_Id
);
762 Append_To
(Stmts
, Free_Stmt
);
764 -- Finalize the object (if applicable), but wrap the call inside
765 -- a block to ensure that the object would still be deallocated in
766 -- case the finalization fails. Generate:
769 -- [Deep_]Finalize (Obj_Ref.all);
776 if Needs_Finalization
(DesigT
) then
778 Make_Block_Statement
(Loc
,
779 Handled_Statement_Sequence
=>
780 Make_Handled_Sequence_Of_Statements
(Loc
,
781 Statements
=> New_List
(
784 Make_Explicit_Dereference
(Loc
,
785 Prefix
=> New_Copy
(Obj_Ref
)),
788 Exception_Handlers
=> New_List
(
789 Make_Exception_Handler
(Loc
,
790 Exception_Choices
=> New_List
(
791 Make_Others_Choice
(Loc
)),
792 Statements
=> New_List
(
793 New_Copy_Tree
(Free_Stmt
),
794 Make_Raise_Statement
(Loc
)))))));
797 -- Signal the accessibility failure through a Program_Error
800 Make_Raise_Program_Error
(Loc
,
801 Condition
=> New_Reference_To
(Standard_True
, Loc
),
802 Reason
=> PE_Accessibility_Check_Failed
));
804 -- Step 2: Create the accessibility comparison
810 Make_Attribute_Reference
(Loc
,
812 Attribute_Name
=> Name_Tag
);
814 -- For tagged types, determine the accessibility level by looking
815 -- at the type specific data of the dispatch table. Generate:
817 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
819 if Tagged_Type_Expansion
then
820 Cond
:= Build_Get_Access_Level
(Loc
, Obj_Ref
);
822 -- Use a runtime call to determine the accessibility level when
823 -- compiling on virtual machine targets. Generate:
825 -- Get_Access_Level (Ref'Tag)
829 Make_Function_Call
(Loc
,
831 New_Reference_To
(RTE
(RE_Get_Access_Level
), Loc
),
832 Parameter_Associations
=> New_List
(Obj_Ref
));
839 Make_Integer_Literal
(Loc
, Type_Access_Level
(PtrT
)));
841 -- Due to the complexity and side effects of the check, utilize an
842 -- if statement instead of the regular Program_Error circuitry.
845 Make_If_Statement
(Loc
,
847 Then_Statements
=> Stmts
));
849 end Apply_Accessibility_Check
;
853 Aggr_In_Place
: constant Boolean := Is_Delayed_Aggregate
(Exp
);
854 Indic
: constant Node_Id
:= Subtype_Mark
(Expression
(N
));
855 T
: constant Entity_Id
:= Entity
(Indic
);
857 Tag_Assign
: Node_Id
;
861 TagT
: Entity_Id
:= Empty
;
862 -- Type used as source for tag assignment
864 TagR
: Node_Id
:= Empty
;
865 -- Target reference for tag assignment
867 -- Start of processing for Expand_Allocator_Expression
870 -- Handle call to C++ constructor
872 if Is_CPP_Constructor_Call
(Exp
) then
873 Make_CPP_Constructor_Call_In_Allocator
875 Function_Call
=> Exp
);
879 -- In the case of an Ada 2012 allocator whose initial value comes from a
880 -- function call, pass "the accessibility level determined by the point
881 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
882 -- Expand_Call but it couldn't be done there (because the Etype of the
883 -- allocator wasn't set then) so we generate the parameter here. See
884 -- the Boolean variable Defer in (a block within) Expand_Call.
886 if Ada_Version
>= Ada_2012
and then Nkind
(Exp
) = N_Function_Call
then
891 if Nkind
(Name
(Exp
)) = N_Explicit_Dereference
then
892 Subp
:= Designated_Type
(Etype
(Prefix
(Name
(Exp
))));
894 Subp
:= Entity
(Name
(Exp
));
897 Subp
:= Ultimate_Alias
(Subp
);
899 if Present
(Extra_Accessibility_Of_Result
(Subp
)) then
900 Add_Extra_Actual_To_Call
901 (Subprogram_Call
=> Exp
,
902 Extra_Formal
=> Extra_Accessibility_Of_Result
(Subp
),
903 Extra_Actual
=> Dynamic_Accessibility_Level
(PtrT
));
908 -- Case of tagged type or type requiring finalization
910 if Is_Tagged_Type
(T
) or else Needs_Finalization
(T
) then
912 -- Ada 2005 (AI-318-02): If the initialization expression is a call
913 -- to a build-in-place function, then access to the allocated object
914 -- must be passed to the function. Currently we limit such functions
915 -- to those with constrained limited result subtypes, but eventually
916 -- we plan to expand the allowed forms of functions that are treated
917 -- as build-in-place.
919 if Ada_Version
>= Ada_2005
920 and then Is_Build_In_Place_Function_Call
(Exp
)
922 Make_Build_In_Place_Call_In_Allocator
(N
, Exp
);
923 Apply_Accessibility_Check
(N
, Built_In_Place
=> True);
927 -- Actions inserted before:
928 -- Temp : constant ptr_T := new T'(Expression);
929 -- Temp._tag = T'tag; -- when not class-wide
930 -- [Deep_]Adjust (Temp.all);
932 -- We analyze by hand the new internal allocator to avoid any
933 -- recursion and inappropriate call to Initialize
935 -- We don't want to remove side effects when the expression must be
936 -- built in place. In the case of a build-in-place function call,
937 -- that could lead to a duplication of the call, which was already
938 -- substituted for the allocator.
940 if not Aggr_In_Place
then
941 Remove_Side_Effects
(Exp
);
944 Temp
:= Make_Temporary
(Loc
, 'P', N
);
946 -- For a class wide allocation generate the following code:
948 -- type Equiv_Record is record ... end record;
949 -- implicit subtype CW is <Class_Wide_Subytpe>;
950 -- temp : PtrT := new CW'(CW!(expr));
952 if Is_Class_Wide_Type
(T
) then
953 Expand_Subtype_From_Expr
(Empty
, T
, Indic
, Exp
);
955 -- Ada 2005 (AI-251): If the expression is a class-wide interface
956 -- object we generate code to move up "this" to reference the
957 -- base of the object before allocating the new object.
959 -- Note that Exp'Address is recursively expanded into a call
960 -- to Base_Address (Exp.Tag)
962 if Is_Class_Wide_Type
(Etype
(Exp
))
963 and then Is_Interface
(Etype
(Exp
))
964 and then Tagged_Type_Expansion
968 Unchecked_Convert_To
(Entity
(Indic
),
969 Make_Explicit_Dereference
(Loc
,
970 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
971 Make_Attribute_Reference
(Loc
,
973 Attribute_Name
=> Name_Address
)))));
977 Unchecked_Convert_To
(Entity
(Indic
), Exp
));
980 Analyze_And_Resolve
(Expression
(N
), Entity
(Indic
));
983 -- Processing for allocators returning non-interface types
985 if not Is_Interface
(Directly_Designated_Type
(PtrT
)) then
986 if Aggr_In_Place
then
988 Make_Object_Declaration
(Loc
,
989 Defining_Identifier
=> Temp
,
990 Object_Definition
=> New_Reference_To
(PtrT
, Loc
),
994 New_Reference_To
(Etype
(Exp
), Loc
)));
996 -- Copy the Comes_From_Source flag for the allocator we just
997 -- built, since logically this allocator is a replacement of
998 -- the original allocator node. This is for proper handling of
999 -- restriction No_Implicit_Heap_Allocations.
1001 Set_Comes_From_Source
1002 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
1004 Set_No_Initialization
(Expression
(Temp_Decl
));
1005 Insert_Action
(N
, Temp_Decl
);
1007 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1008 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
1010 -- Attach the object to the associated finalization master.
1011 -- This is done manually on .NET/JVM since those compilers do
1012 -- no support pools and can't benefit from internally generated
1013 -- Allocate / Deallocate procedures.
1015 if VM_Target
/= No_VM
1016 and then Is_Controlled
(DesigT
)
1017 and then Present
(Finalization_Master
(PtrT
))
1022 New_Reference_To
(Temp
, Loc
),
1027 Node
:= Relocate_Node
(N
);
1028 Set_Analyzed
(Node
);
1031 Make_Object_Declaration
(Loc
,
1032 Defining_Identifier
=> Temp
,
1033 Constant_Present
=> True,
1034 Object_Definition
=> New_Reference_To
(PtrT
, Loc
),
1035 Expression
=> Node
);
1037 Insert_Action
(N
, Temp_Decl
);
1038 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1040 -- Attach the object to the associated finalization master.
1041 -- This is done manually on .NET/JVM since those compilers do
1042 -- no support pools and can't benefit from internally generated
1043 -- Allocate / Deallocate procedures.
1045 if VM_Target
/= No_VM
1046 and then Is_Controlled
(DesigT
)
1047 and then Present
(Finalization_Master
(PtrT
))
1052 New_Reference_To
(Temp
, Loc
),
1057 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
1058 -- interface type. In this case we use the type of the qualified
1059 -- expression to allocate the object.
1063 Def_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
1068 Make_Full_Type_Declaration
(Loc
,
1069 Defining_Identifier
=> Def_Id
,
1071 Make_Access_To_Object_Definition
(Loc
,
1072 All_Present
=> True,
1073 Null_Exclusion_Present
=> False,
1075 Is_Access_Constant
(Etype
(N
)),
1076 Subtype_Indication
=>
1077 New_Reference_To
(Etype
(Exp
), Loc
)));
1079 Insert_Action
(N
, New_Decl
);
1081 -- Inherit the allocation-related attributes from the original
1084 Set_Finalization_Master
(Def_Id
, Finalization_Master
(PtrT
));
1086 Set_Associated_Storage_Pool
(Def_Id
,
1087 Associated_Storage_Pool
(PtrT
));
1089 -- Declare the object using the previous type declaration
1091 if Aggr_In_Place
then
1093 Make_Object_Declaration
(Loc
,
1094 Defining_Identifier
=> Temp
,
1095 Object_Definition
=> New_Reference_To
(Def_Id
, Loc
),
1097 Make_Allocator
(Loc
,
1098 New_Reference_To
(Etype
(Exp
), Loc
)));
1100 -- Copy the Comes_From_Source flag for the allocator we just
1101 -- built, since logically this allocator is a replacement of
1102 -- the original allocator node. This is for proper handling
1103 -- of restriction No_Implicit_Heap_Allocations.
1105 Set_Comes_From_Source
1106 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
1108 Set_No_Initialization
(Expression
(Temp_Decl
));
1109 Insert_Action
(N
, Temp_Decl
);
1111 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1112 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
1115 Node
:= Relocate_Node
(N
);
1116 Set_Analyzed
(Node
);
1119 Make_Object_Declaration
(Loc
,
1120 Defining_Identifier
=> Temp
,
1121 Constant_Present
=> True,
1122 Object_Definition
=> New_Reference_To
(Def_Id
, Loc
),
1123 Expression
=> Node
);
1125 Insert_Action
(N
, Temp_Decl
);
1126 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1129 -- Generate an additional object containing the address of the
1130 -- returned object. The type of this second object declaration
1131 -- is the correct type required for the common processing that
1132 -- is still performed by this subprogram. The displacement of
1133 -- this pointer to reference the component associated with the
1134 -- interface type will be done at the end of common processing.
1137 Make_Object_Declaration
(Loc
,
1138 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
1139 Object_Definition
=> New_Reference_To
(PtrT
, Loc
),
1141 Unchecked_Convert_To
(PtrT
,
1142 New_Reference_To
(Temp
, Loc
)));
1144 Insert_Action
(N
, New_Decl
);
1146 Temp_Decl
:= New_Decl
;
1147 Temp
:= Defining_Identifier
(New_Decl
);
1151 Apply_Accessibility_Check
(Temp
);
1153 -- Generate the tag assignment
1155 -- Suppress the tag assignment when VM_Target because VM tags are
1156 -- represented implicitly in objects.
1158 if not Tagged_Type_Expansion
then
1161 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1162 -- interface objects because in this case the tag does not change.
1164 elsif Is_Interface
(Directly_Designated_Type
(Etype
(N
))) then
1165 pragma Assert
(Is_Class_Wide_Type
1166 (Directly_Designated_Type
(Etype
(N
))));
1169 elsif Is_Tagged_Type
(T
) and then not Is_Class_Wide_Type
(T
) then
1171 TagR
:= New_Reference_To
(Temp
, Loc
);
1173 elsif Is_Private_Type
(T
)
1174 and then Is_Tagged_Type
(Underlying_Type
(T
))
1176 TagT
:= Underlying_Type
(T
);
1178 Unchecked_Convert_To
(Underlying_Type
(T
),
1179 Make_Explicit_Dereference
(Loc
,
1180 Prefix
=> New_Reference_To
(Temp
, Loc
)));
1183 if Present
(TagT
) then
1185 Full_T
: constant Entity_Id
:= Underlying_Type
(TagT
);
1188 Make_Assignment_Statement
(Loc
,
1190 Make_Selected_Component
(Loc
,
1193 New_Reference_To
(First_Tag_Component
(Full_T
), Loc
)),
1195 Unchecked_Convert_To
(RTE
(RE_Tag
),
1198 (First_Elmt
(Access_Disp_Table
(Full_T
))), Loc
)));
1201 -- The previous assignment has to be done in any case
1203 Set_Assignment_OK
(Name
(Tag_Assign
));
1204 Insert_Action
(N
, Tag_Assign
);
1207 if Needs_Finalization
(DesigT
)
1208 and then Needs_Finalization
(T
)
1210 -- Generate an Adjust call if the object will be moved. In Ada
1211 -- 2005, the object may be inherently limited, in which case
1212 -- there is no Adjust procedure, and the object is built in
1213 -- place. In Ada 95, the object can be limited but not
1214 -- inherently limited if this allocator came from a return
1215 -- statement (we're allocating the result on the secondary
1216 -- stack). In that case, the object will be moved, so we _do_
1219 if not Aggr_In_Place
1220 and then not Is_Immutably_Limited_Type
(T
)
1226 -- An unchecked conversion is needed in the classwide
1227 -- case because the designated type can be an ancestor
1228 -- of the subtype mark of the allocator.
1230 Unchecked_Convert_To
(T
,
1231 Make_Explicit_Dereference
(Loc
,
1232 Prefix
=> New_Reference_To
(Temp
, Loc
))),
1237 -- Set_Finalize_Address (<PtrT>FM, <T>FD'Unrestricted_Access);
1239 -- Do not generate this call in the following cases:
1241 -- * .NET/JVM - these targets do not support address arithmetic
1242 -- and unchecked conversion, key elements of Finalize_Address.
1244 -- * Alfa mode - the call is useless and results in unwanted
1247 -- * CodePeer mode - TSS primitive Finalize_Address is not
1248 -- created in this mode.
1250 if VM_Target
= No_VM
1251 and then not Alfa_Mode
1252 and then not CodePeer_Mode
1253 and then Present
(Finalization_Master
(PtrT
))
1254 and then Present
(Temp_Decl
)
1255 and then Nkind
(Expression
(Temp_Decl
)) = N_Allocator
1258 Make_Set_Finalize_Address_Call
1265 Rewrite
(N
, New_Reference_To
(Temp
, Loc
));
1266 Analyze_And_Resolve
(N
, PtrT
);
1268 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1269 -- component containing the secondary dispatch table of the interface
1272 if Is_Interface
(Directly_Designated_Type
(PtrT
)) then
1273 Displace_Allocator_Pointer
(N
);
1276 elsif Aggr_In_Place
then
1277 Temp
:= Make_Temporary
(Loc
, 'P', N
);
1279 Make_Object_Declaration
(Loc
,
1280 Defining_Identifier
=> Temp
,
1281 Object_Definition
=> New_Reference_To
(PtrT
, Loc
),
1283 Make_Allocator
(Loc
,
1284 Expression
=> New_Reference_To
(Etype
(Exp
), Loc
)));
1286 -- Copy the Comes_From_Source flag for the allocator we just built,
1287 -- since logically this allocator is a replacement of the original
1288 -- allocator node. This is for proper handling of restriction
1289 -- No_Implicit_Heap_Allocations.
1291 Set_Comes_From_Source
1292 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
1294 Set_No_Initialization
(Expression
(Temp_Decl
));
1295 Insert_Action
(N
, Temp_Decl
);
1297 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1298 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
1300 -- Attach the object to the associated finalization master. Thisis
1301 -- done manually on .NET/JVM since those compilers do no support
1302 -- pools and cannot benefit from internally generated Allocate and
1303 -- Deallocate procedures.
1305 if VM_Target
/= No_VM
1306 and then Is_Controlled
(DesigT
)
1307 and then Present
(Finalization_Master
(PtrT
))
1311 (Obj_Ref
=> New_Reference_To
(Temp
, Loc
),
1315 Rewrite
(N
, New_Reference_To
(Temp
, Loc
));
1316 Analyze_And_Resolve
(N
, PtrT
);
1318 elsif Is_Access_Type
(T
)
1319 and then Can_Never_Be_Null
(T
)
1321 Install_Null_Excluding_Check
(Exp
);
1323 elsif Is_Access_Type
(DesigT
)
1324 and then Nkind
(Exp
) = N_Allocator
1325 and then Nkind
(Expression
(Exp
)) /= N_Qualified_Expression
1327 -- Apply constraint to designated subtype indication
1329 Apply_Constraint_Check
(Expression
(Exp
),
1330 Designated_Type
(DesigT
),
1331 No_Sliding
=> True);
1333 if Nkind
(Expression
(Exp
)) = N_Raise_Constraint_Error
then
1335 -- Propagate constraint_error to enclosing allocator
1337 Rewrite
(Exp
, New_Copy
(Expression
(Exp
)));
1341 Build_Allocate_Deallocate_Proc
(N
, True);
1344 -- type A is access T1;
1345 -- X : A := new T2'(...);
1346 -- T1 and T2 can be different subtypes, and we might need to check
1347 -- both constraints. First check against the type of the qualified
1350 Apply_Constraint_Check
(Exp
, T
, No_Sliding
=> True);
1352 if Do_Range_Check
(Exp
) then
1353 Set_Do_Range_Check
(Exp
, False);
1354 Generate_Range_Check
(Exp
, DesigT
, CE_Range_Check_Failed
);
1357 -- A check is also needed in cases where the designated subtype is
1358 -- constrained and differs from the subtype given in the qualified
1359 -- expression. Note that the check on the qualified expression does
1360 -- not allow sliding, but this check does (a relaxation from Ada 83).
1362 if Is_Constrained
(DesigT
)
1363 and then not Subtypes_Statically_Match
(T
, DesigT
)
1365 Apply_Constraint_Check
1366 (Exp
, DesigT
, No_Sliding
=> False);
1368 if Do_Range_Check
(Exp
) then
1369 Set_Do_Range_Check
(Exp
, False);
1370 Generate_Range_Check
(Exp
, DesigT
, CE_Range_Check_Failed
);
1374 -- For an access to unconstrained packed array, GIGI needs to see an
1375 -- expression with a constrained subtype in order to compute the
1376 -- proper size for the allocator.
1378 if Is_Array_Type
(T
)
1379 and then not Is_Constrained
(T
)
1380 and then Is_Packed
(T
)
1383 ConstrT
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
1384 Internal_Exp
: constant Node_Id
:= Relocate_Node
(Exp
);
1387 Make_Subtype_Declaration
(Loc
,
1388 Defining_Identifier
=> ConstrT
,
1389 Subtype_Indication
=>
1390 Make_Subtype_From_Expr
(Internal_Exp
, T
)));
1391 Freeze_Itype
(ConstrT
, Exp
);
1392 Rewrite
(Exp
, OK_Convert_To
(ConstrT
, Internal_Exp
));
1396 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1397 -- to a build-in-place function, then access to the allocated object
1398 -- must be passed to the function. Currently we limit such functions
1399 -- to those with constrained limited result subtypes, but eventually
1400 -- we plan to expand the allowed forms of functions that are treated
1401 -- as build-in-place.
1403 if Ada_Version
>= Ada_2005
1404 and then Is_Build_In_Place_Function_Call
(Exp
)
1406 Make_Build_In_Place_Call_In_Allocator
(N
, Exp
);
1411 when RE_Not_Available
=>
1413 end Expand_Allocator_Expression
;
1415 -----------------------------
1416 -- Expand_Array_Comparison --
1417 -----------------------------
1419 -- Expansion is only required in the case of array types. For the unpacked
1420 -- case, an appropriate runtime routine is called. For packed cases, and
1421 -- also in some other cases where a runtime routine cannot be called, the
1422 -- form of the expansion is:
1424 -- [body for greater_nn; boolean_expression]
1426 -- The body is built by Make_Array_Comparison_Op, and the form of the
1427 -- Boolean expression depends on the operator involved.
1429 procedure Expand_Array_Comparison
(N
: Node_Id
) is
1430 Loc
: constant Source_Ptr
:= Sloc
(N
);
1431 Op1
: Node_Id
:= Left_Opnd
(N
);
1432 Op2
: Node_Id
:= Right_Opnd
(N
);
1433 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
1434 Ctyp
: constant Entity_Id
:= Component_Type
(Typ1
);
1437 Func_Body
: Node_Id
;
1438 Func_Name
: Entity_Id
;
1442 Byte_Addressable
: constant Boolean := System_Storage_Unit
= Byte
'Size;
1443 -- True for byte addressable target
1445 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean;
1446 -- Returns True if the length of the given operand is known to be less
1447 -- than 4. Returns False if this length is known to be four or greater
1448 -- or is not known at compile time.
1450 ------------------------
1451 -- Length_Less_Than_4 --
1452 ------------------------
1454 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean is
1455 Otyp
: constant Entity_Id
:= Etype
(Opnd
);
1458 if Ekind
(Otyp
) = E_String_Literal_Subtype
then
1459 return String_Literal_Length
(Otyp
) < 4;
1463 Ityp
: constant Entity_Id
:= Etype
(First_Index
(Otyp
));
1464 Lo
: constant Node_Id
:= Type_Low_Bound
(Ityp
);
1465 Hi
: constant Node_Id
:= Type_High_Bound
(Ityp
);
1470 if Compile_Time_Known_Value
(Lo
) then
1471 Lov
:= Expr_Value
(Lo
);
1476 if Compile_Time_Known_Value
(Hi
) then
1477 Hiv
:= Expr_Value
(Hi
);
1482 return Hiv
< Lov
+ 3;
1485 end Length_Less_Than_4
;
1487 -- Start of processing for Expand_Array_Comparison
1490 -- Deal first with unpacked case, where we can call a runtime routine
1491 -- except that we avoid this for targets for which are not addressable
1492 -- by bytes, and for the JVM/CIL, since they do not support direct
1493 -- addressing of array components.
1495 if not Is_Bit_Packed_Array
(Typ1
)
1496 and then Byte_Addressable
1497 and then VM_Target
= No_VM
1499 -- The call we generate is:
1501 -- Compare_Array_xn[_Unaligned]
1502 -- (left'address, right'address, left'length, right'length) <op> 0
1504 -- x = U for unsigned, S for signed
1505 -- n = 8,16,32,64 for component size
1506 -- Add _Unaligned if length < 4 and component size is 8.
1507 -- <op> is the standard comparison operator
1509 if Component_Size
(Typ1
) = 8 then
1510 if Length_Less_Than_4
(Op1
)
1512 Length_Less_Than_4
(Op2
)
1514 if Is_Unsigned_Type
(Ctyp
) then
1515 Comp
:= RE_Compare_Array_U8_Unaligned
;
1517 Comp
:= RE_Compare_Array_S8_Unaligned
;
1521 if Is_Unsigned_Type
(Ctyp
) then
1522 Comp
:= RE_Compare_Array_U8
;
1524 Comp
:= RE_Compare_Array_S8
;
1528 elsif Component_Size
(Typ1
) = 16 then
1529 if Is_Unsigned_Type
(Ctyp
) then
1530 Comp
:= RE_Compare_Array_U16
;
1532 Comp
:= RE_Compare_Array_S16
;
1535 elsif Component_Size
(Typ1
) = 32 then
1536 if Is_Unsigned_Type
(Ctyp
) then
1537 Comp
:= RE_Compare_Array_U32
;
1539 Comp
:= RE_Compare_Array_S32
;
1542 else pragma Assert
(Component_Size
(Typ1
) = 64);
1543 if Is_Unsigned_Type
(Ctyp
) then
1544 Comp
:= RE_Compare_Array_U64
;
1546 Comp
:= RE_Compare_Array_S64
;
1550 Remove_Side_Effects
(Op1
, Name_Req
=> True);
1551 Remove_Side_Effects
(Op2
, Name_Req
=> True);
1554 Make_Function_Call
(Sloc
(Op1
),
1555 Name
=> New_Occurrence_Of
(RTE
(Comp
), Loc
),
1557 Parameter_Associations
=> New_List
(
1558 Make_Attribute_Reference
(Loc
,
1559 Prefix
=> Relocate_Node
(Op1
),
1560 Attribute_Name
=> Name_Address
),
1562 Make_Attribute_Reference
(Loc
,
1563 Prefix
=> Relocate_Node
(Op2
),
1564 Attribute_Name
=> Name_Address
),
1566 Make_Attribute_Reference
(Loc
,
1567 Prefix
=> Relocate_Node
(Op1
),
1568 Attribute_Name
=> Name_Length
),
1570 Make_Attribute_Reference
(Loc
,
1571 Prefix
=> Relocate_Node
(Op2
),
1572 Attribute_Name
=> Name_Length
))));
1575 Make_Integer_Literal
(Sloc
(Op2
),
1578 Analyze_And_Resolve
(Op1
, Standard_Integer
);
1579 Analyze_And_Resolve
(Op2
, Standard_Integer
);
1583 -- Cases where we cannot make runtime call
1585 -- For (a <= b) we convert to not (a > b)
1587 if Chars
(N
) = Name_Op_Le
then
1593 Right_Opnd
=> Op2
)));
1594 Analyze_And_Resolve
(N
, Standard_Boolean
);
1597 -- For < the Boolean expression is
1598 -- greater__nn (op2, op1)
1600 elsif Chars
(N
) = Name_Op_Lt
then
1601 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
1605 Op1
:= Right_Opnd
(N
);
1606 Op2
:= Left_Opnd
(N
);
1608 -- For (a >= b) we convert to not (a < b)
1610 elsif Chars
(N
) = Name_Op_Ge
then
1616 Right_Opnd
=> Op2
)));
1617 Analyze_And_Resolve
(N
, Standard_Boolean
);
1620 -- For > the Boolean expression is
1621 -- greater__nn (op1, op2)
1624 pragma Assert
(Chars
(N
) = Name_Op_Gt
);
1625 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
1628 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
1630 Make_Function_Call
(Loc
,
1631 Name
=> New_Reference_To
(Func_Name
, Loc
),
1632 Parameter_Associations
=> New_List
(Op1
, Op2
));
1634 Insert_Action
(N
, Func_Body
);
1636 Analyze_And_Resolve
(N
, Standard_Boolean
);
1639 when RE_Not_Available
=>
1641 end Expand_Array_Comparison
;
1643 ---------------------------
1644 -- Expand_Array_Equality --
1645 ---------------------------
1647 -- Expand an equality function for multi-dimensional arrays. Here is an
1648 -- example of such a function for Nb_Dimension = 2
1650 -- function Enn (A : atyp; B : btyp) return boolean is
1652 -- if (A'length (1) = 0 or else A'length (2) = 0)
1654 -- (B'length (1) = 0 or else B'length (2) = 0)
1656 -- return True; -- RM 4.5.2(22)
1659 -- if A'length (1) /= B'length (1)
1661 -- A'length (2) /= B'length (2)
1663 -- return False; -- RM 4.5.2(23)
1667 -- A1 : Index_T1 := A'first (1);
1668 -- B1 : Index_T1 := B'first (1);
1672 -- A2 : Index_T2 := A'first (2);
1673 -- B2 : Index_T2 := B'first (2);
1676 -- if A (A1, A2) /= B (B1, B2) then
1680 -- exit when A2 = A'last (2);
1681 -- A2 := Index_T2'succ (A2);
1682 -- B2 := Index_T2'succ (B2);
1686 -- exit when A1 = A'last (1);
1687 -- A1 := Index_T1'succ (A1);
1688 -- B1 := Index_T1'succ (B1);
1695 -- Note on the formal types used (atyp and btyp). If either of the arrays
1696 -- is of a private type, we use the underlying type, and do an unchecked
1697 -- conversion of the actual. If either of the arrays has a bound depending
1698 -- on a discriminant, then we use the base type since otherwise we have an
1699 -- escaped discriminant in the function.
1701 -- If both arrays are constrained and have the same bounds, we can generate
1702 -- a loop with an explicit iteration scheme using a 'Range attribute over
1705 function Expand_Array_Equality
1710 Typ
: Entity_Id
) return Node_Id
1712 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
1713 Decls
: constant List_Id
:= New_List
;
1714 Index_List1
: constant List_Id
:= New_List
;
1715 Index_List2
: constant List_Id
:= New_List
;
1719 Func_Name
: Entity_Id
;
1720 Func_Body
: Node_Id
;
1722 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
1723 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
1727 -- The parameter types to be used for the formals
1732 Num
: Int
) return Node_Id
;
1733 -- This builds the attribute reference Arr'Nam (Expr)
1735 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
;
1736 -- Create one statement to compare corresponding components, designated
1737 -- by a full set of indexes.
1739 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
;
1740 -- Given one of the arguments, computes the appropriate type to be used
1741 -- for that argument in the corresponding function formal
1743 function Handle_One_Dimension
1745 Index
: Node_Id
) return Node_Id
;
1746 -- This procedure returns the following code
1749 -- Bn : Index_T := B'First (N);
1753 -- exit when An = A'Last (N);
1754 -- An := Index_T'Succ (An)
1755 -- Bn := Index_T'Succ (Bn)
1759 -- If both indexes are constrained and identical, the procedure
1760 -- returns a simpler loop:
1762 -- for An in A'Range (N) loop
1766 -- N is the dimension for which we are generating a loop. Index is the
1767 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1768 -- xxx statement is either the loop or declare for the next dimension
1769 -- or if this is the last dimension the comparison of corresponding
1770 -- components of the arrays.
1772 -- The actual way the code works is to return the comparison of
1773 -- corresponding components for the N+1 call. That's neater!
1775 function Test_Empty_Arrays
return Node_Id
;
1776 -- This function constructs the test for both arrays being empty
1777 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1779 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1781 function Test_Lengths_Correspond
return Node_Id
;
1782 -- This function constructs the test for arrays having different lengths
1783 -- in at least one index position, in which case the resulting code is:
1785 -- A'length (1) /= B'length (1)
1787 -- A'length (2) /= B'length (2)
1798 Num
: Int
) return Node_Id
1802 Make_Attribute_Reference
(Loc
,
1803 Attribute_Name
=> Nam
,
1804 Prefix
=> New_Reference_To
(Arr
, Loc
),
1805 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, Num
)));
1808 ------------------------
1809 -- Component_Equality --
1810 ------------------------
1812 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
is
1817 -- if a(i1...) /= b(j1...) then return false; end if;
1820 Make_Indexed_Component
(Loc
,
1821 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
1822 Expressions
=> Index_List1
);
1825 Make_Indexed_Component
(Loc
,
1826 Prefix
=> Make_Identifier
(Loc
, Chars
(B
)),
1827 Expressions
=> Index_List2
);
1829 Test
:= Expand_Composite_Equality
1830 (Nod
, Component_Type
(Typ
), L
, R
, Decls
);
1832 -- If some (sub)component is an unchecked_union, the whole operation
1833 -- will raise program error.
1835 if Nkind
(Test
) = N_Raise_Program_Error
then
1837 -- This node is going to be inserted at a location where a
1838 -- statement is expected: clear its Etype so analysis will set
1839 -- it to the expected Standard_Void_Type.
1841 Set_Etype
(Test
, Empty
);
1846 Make_Implicit_If_Statement
(Nod
,
1847 Condition
=> Make_Op_Not
(Loc
, Right_Opnd
=> Test
),
1848 Then_Statements
=> New_List
(
1849 Make_Simple_Return_Statement
(Loc
,
1850 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))));
1852 end Component_Equality
;
1858 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
is
1869 T
:= Underlying_Type
(T
);
1871 X
:= First_Index
(T
);
1872 while Present
(X
) loop
1873 if Denotes_Discriminant
(Type_Low_Bound
(Etype
(X
)))
1875 Denotes_Discriminant
(Type_High_Bound
(Etype
(X
)))
1888 --------------------------
1889 -- Handle_One_Dimension --
1890 ---------------------------
1892 function Handle_One_Dimension
1894 Index
: Node_Id
) return Node_Id
1896 Need_Separate_Indexes
: constant Boolean :=
1898 or else not Is_Constrained
(Ltyp
);
1899 -- If the index types are identical, and we are working with
1900 -- constrained types, then we can use the same index for both
1903 An
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
1906 Index_T
: Entity_Id
;
1911 if N
> Number_Dimensions
(Ltyp
) then
1912 return Component_Equality
(Ltyp
);
1915 -- Case where we generate a loop
1917 Index_T
:= Base_Type
(Etype
(Index
));
1919 if Need_Separate_Indexes
then
1920 Bn
:= Make_Temporary
(Loc
, 'B');
1925 Append
(New_Reference_To
(An
, Loc
), Index_List1
);
1926 Append
(New_Reference_To
(Bn
, Loc
), Index_List2
);
1928 Stm_List
:= New_List
(
1929 Handle_One_Dimension
(N
+ 1, Next_Index
(Index
)));
1931 if Need_Separate_Indexes
then
1933 -- Generate guard for loop, followed by increments of indexes
1935 Append_To
(Stm_List
,
1936 Make_Exit_Statement
(Loc
,
1939 Left_Opnd
=> New_Reference_To
(An
, Loc
),
1940 Right_Opnd
=> Arr_Attr
(A
, Name_Last
, N
))));
1942 Append_To
(Stm_List
,
1943 Make_Assignment_Statement
(Loc
,
1944 Name
=> New_Reference_To
(An
, Loc
),
1946 Make_Attribute_Reference
(Loc
,
1947 Prefix
=> New_Reference_To
(Index_T
, Loc
),
1948 Attribute_Name
=> Name_Succ
,
1949 Expressions
=> New_List
(New_Reference_To
(An
, Loc
)))));
1951 Append_To
(Stm_List
,
1952 Make_Assignment_Statement
(Loc
,
1953 Name
=> New_Reference_To
(Bn
, Loc
),
1955 Make_Attribute_Reference
(Loc
,
1956 Prefix
=> New_Reference_To
(Index_T
, Loc
),
1957 Attribute_Name
=> Name_Succ
,
1958 Expressions
=> New_List
(New_Reference_To
(Bn
, Loc
)))));
1961 -- If separate indexes, we need a declare block for An and Bn, and a
1962 -- loop without an iteration scheme.
1964 if Need_Separate_Indexes
then
1966 Make_Implicit_Loop_Statement
(Nod
, Statements
=> Stm_List
);
1969 Make_Block_Statement
(Loc
,
1970 Declarations
=> New_List
(
1971 Make_Object_Declaration
(Loc
,
1972 Defining_Identifier
=> An
,
1973 Object_Definition
=> New_Reference_To
(Index_T
, Loc
),
1974 Expression
=> Arr_Attr
(A
, Name_First
, N
)),
1976 Make_Object_Declaration
(Loc
,
1977 Defining_Identifier
=> Bn
,
1978 Object_Definition
=> New_Reference_To
(Index_T
, Loc
),
1979 Expression
=> Arr_Attr
(B
, Name_First
, N
))),
1981 Handled_Statement_Sequence
=>
1982 Make_Handled_Sequence_Of_Statements
(Loc
,
1983 Statements
=> New_List
(Loop_Stm
)));
1985 -- If no separate indexes, return loop statement with explicit
1986 -- iteration scheme on its own
1990 Make_Implicit_Loop_Statement
(Nod
,
1991 Statements
=> Stm_List
,
1993 Make_Iteration_Scheme
(Loc
,
1994 Loop_Parameter_Specification
=>
1995 Make_Loop_Parameter_Specification
(Loc
,
1996 Defining_Identifier
=> An
,
1997 Discrete_Subtype_Definition
=>
1998 Arr_Attr
(A
, Name_Range
, N
))));
2001 end Handle_One_Dimension
;
2003 -----------------------
2004 -- Test_Empty_Arrays --
2005 -----------------------
2007 function Test_Empty_Arrays
return Node_Id
is
2017 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
2020 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
2021 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
2025 Left_Opnd
=> Arr_Attr
(B
, Name_Length
, J
),
2026 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
2035 Left_Opnd
=> Relocate_Node
(Alist
),
2036 Right_Opnd
=> Atest
);
2040 Left_Opnd
=> Relocate_Node
(Blist
),
2041 Right_Opnd
=> Btest
);
2048 Right_Opnd
=> Blist
);
2049 end Test_Empty_Arrays
;
2051 -----------------------------
2052 -- Test_Lengths_Correspond --
2053 -----------------------------
2055 function Test_Lengths_Correspond
return Node_Id
is
2061 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
2064 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
2065 Right_Opnd
=> Arr_Attr
(B
, Name_Length
, J
));
2072 Left_Opnd
=> Relocate_Node
(Result
),
2073 Right_Opnd
=> Rtest
);
2078 end Test_Lengths_Correspond
;
2080 -- Start of processing for Expand_Array_Equality
2083 Ltyp
:= Get_Arg_Type
(Lhs
);
2084 Rtyp
:= Get_Arg_Type
(Rhs
);
2086 -- For now, if the argument types are not the same, go to the base type,
2087 -- since the code assumes that the formals have the same type. This is
2088 -- fixable in future ???
2090 if Ltyp
/= Rtyp
then
2091 Ltyp
:= Base_Type
(Ltyp
);
2092 Rtyp
:= Base_Type
(Rtyp
);
2093 pragma Assert
(Ltyp
= Rtyp
);
2096 -- Build list of formals for function
2098 Formals
:= New_List
(
2099 Make_Parameter_Specification
(Loc
,
2100 Defining_Identifier
=> A
,
2101 Parameter_Type
=> New_Reference_To
(Ltyp
, Loc
)),
2103 Make_Parameter_Specification
(Loc
,
2104 Defining_Identifier
=> B
,
2105 Parameter_Type
=> New_Reference_To
(Rtyp
, Loc
)));
2107 Func_Name
:= Make_Temporary
(Loc
, 'E');
2109 -- Build statement sequence for function
2112 Make_Subprogram_Body
(Loc
,
2114 Make_Function_Specification
(Loc
,
2115 Defining_Unit_Name
=> Func_Name
,
2116 Parameter_Specifications
=> Formals
,
2117 Result_Definition
=> New_Reference_To
(Standard_Boolean
, Loc
)),
2119 Declarations
=> Decls
,
2121 Handled_Statement_Sequence
=>
2122 Make_Handled_Sequence_Of_Statements
(Loc
,
2123 Statements
=> New_List
(
2125 Make_Implicit_If_Statement
(Nod
,
2126 Condition
=> Test_Empty_Arrays
,
2127 Then_Statements
=> New_List
(
2128 Make_Simple_Return_Statement
(Loc
,
2130 New_Occurrence_Of
(Standard_True
, Loc
)))),
2132 Make_Implicit_If_Statement
(Nod
,
2133 Condition
=> Test_Lengths_Correspond
,
2134 Then_Statements
=> New_List
(
2135 Make_Simple_Return_Statement
(Loc
,
2137 New_Occurrence_Of
(Standard_False
, Loc
)))),
2139 Handle_One_Dimension
(1, First_Index
(Ltyp
)),
2141 Make_Simple_Return_Statement
(Loc
,
2142 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)))));
2144 Set_Has_Completion
(Func_Name
, True);
2145 Set_Is_Inlined
(Func_Name
);
2147 -- If the array type is distinct from the type of the arguments, it
2148 -- is the full view of a private type. Apply an unchecked conversion
2149 -- to insure that analysis of the call succeeds.
2159 or else Base_Type
(Etype
(Lhs
)) /= Base_Type
(Ltyp
)
2161 L
:= OK_Convert_To
(Ltyp
, Lhs
);
2165 or else Base_Type
(Etype
(Rhs
)) /= Base_Type
(Rtyp
)
2167 R
:= OK_Convert_To
(Rtyp
, Rhs
);
2170 Actuals
:= New_List
(L
, R
);
2173 Append_To
(Bodies
, Func_Body
);
2176 Make_Function_Call
(Loc
,
2177 Name
=> New_Reference_To
(Func_Name
, Loc
),
2178 Parameter_Associations
=> Actuals
);
2179 end Expand_Array_Equality
;
2181 -----------------------------
2182 -- Expand_Boolean_Operator --
2183 -----------------------------
2185 -- Note that we first get the actual subtypes of the operands, since we
2186 -- always want to deal with types that have bounds.
2188 procedure Expand_Boolean_Operator
(N
: Node_Id
) is
2189 Typ
: constant Entity_Id
:= Etype
(N
);
2192 -- Special case of bit packed array where both operands are known to be
2193 -- properly aligned. In this case we use an efficient run time routine
2194 -- to carry out the operation (see System.Bit_Ops).
2196 if Is_Bit_Packed_Array
(Typ
)
2197 and then not Is_Possibly_Unaligned_Object
(Left_Opnd
(N
))
2198 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
2200 Expand_Packed_Boolean_Operator
(N
);
2204 -- For the normal non-packed case, the general expansion is to build
2205 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2206 -- and then inserting it into the tree. The original operator node is
2207 -- then rewritten as a call to this function. We also use this in the
2208 -- packed case if either operand is a possibly unaligned object.
2211 Loc
: constant Source_Ptr
:= Sloc
(N
);
2212 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
2213 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
2214 Func_Body
: Node_Id
;
2215 Func_Name
: Entity_Id
;
2218 Convert_To_Actual_Subtype
(L
);
2219 Convert_To_Actual_Subtype
(R
);
2220 Ensure_Defined
(Etype
(L
), N
);
2221 Ensure_Defined
(Etype
(R
), N
);
2222 Apply_Length_Check
(R
, Etype
(L
));
2224 if Nkind
(N
) = N_Op_Xor
then
2225 Silly_Boolean_Array_Xor_Test
(N
, Etype
(L
));
2228 if Nkind
(Parent
(N
)) = N_Assignment_Statement
2229 and then Safe_In_Place_Array_Op
(Name
(Parent
(N
)), L
, R
)
2231 Build_Boolean_Array_Proc_Call
(Parent
(N
), L
, R
);
2233 elsif Nkind
(Parent
(N
)) = N_Op_Not
2234 and then Nkind
(N
) = N_Op_And
2236 Safe_In_Place_Array_Op
(Name
(Parent
(Parent
(N
))), L
, R
)
2241 Func_Body
:= Make_Boolean_Array_Op
(Etype
(L
), N
);
2242 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
2243 Insert_Action
(N
, Func_Body
);
2245 -- Now rewrite the expression with a call
2248 Make_Function_Call
(Loc
,
2249 Name
=> New_Reference_To
(Func_Name
, Loc
),
2250 Parameter_Associations
=>
2253 Make_Type_Conversion
2254 (Loc
, New_Reference_To
(Etype
(L
), Loc
), R
))));
2256 Analyze_And_Resolve
(N
, Typ
);
2259 end Expand_Boolean_Operator
;
2261 ------------------------------------------------
2262 -- Expand_Compare_Minimize_Eliminate_Overflow --
2263 ------------------------------------------------
2265 procedure Expand_Compare_Minimize_Eliminate_Overflow
(N
: Node_Id
) is
2266 Loc
: constant Source_Ptr
:= Sloc
(N
);
2268 Result_Type
: constant Entity_Id
:= Etype
(N
);
2269 -- Capture result type (could be a derived boolean type)
2274 LLIB
: constant Entity_Id
:= Base_Type
(Standard_Long_Long_Integer
);
2275 -- Entity for Long_Long_Integer'Base
2277 Check
: constant Overflow_Check_Type
:= Overflow_Check_Mode
;
2278 -- Current overflow checking mode
2281 procedure Set_False
;
2282 -- These procedures rewrite N with an occurrence of Standard_True or
2283 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2289 procedure Set_False
is
2291 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
2292 Warn_On_Known_Condition
(N
);
2299 procedure Set_True
is
2301 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
2302 Warn_On_Known_Condition
(N
);
2305 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2308 -- Nothing to do unless we have a comparison operator with operands
2309 -- that are signed integer types, and we are operating in either
2310 -- MINIMIZED or ELIMINATED overflow checking mode.
2312 if Nkind
(N
) not in N_Op_Compare
2313 or else Check
not in Minimized_Or_Eliminated
2314 or else not Is_Signed_Integer_Type
(Etype
(Left_Opnd
(N
)))
2319 -- OK, this is the case we are interested in. First step is to process
2320 -- our operands using the Minimize_Eliminate circuitry which applies
2321 -- this processing to the two operand subtrees.
2323 Minimize_Eliminate_Overflows
2324 (Left_Opnd
(N
), Llo
, Lhi
, Top_Level
=> False);
2325 Minimize_Eliminate_Overflows
2326 (Right_Opnd
(N
), Rlo
, Rhi
, Top_Level
=> False);
2328 -- See if the range information decides the result of the comparison.
2329 -- We can only do this if we in fact have full range information (which
2330 -- won't be the case if either operand is bignum at this stage).
2332 if Llo
/= No_Uint
and then Rlo
/= No_Uint
then
2333 case N_Op_Compare
(Nkind
(N
)) is
2335 if Llo
= Lhi
and then Rlo
= Rhi
and then Llo
= Rlo
then
2337 elsif Llo
> Rhi
or else Lhi
< Rlo
then
2344 elsif Lhi
< Rlo
then
2351 elsif Lhi
<= Rlo
then
2358 elsif Lhi
<= Rlo
then
2365 elsif Lhi
< Rlo
then
2370 if Llo
= Lhi
and then Rlo
= Rhi
and then Llo
= Rlo
then
2372 elsif Llo
> Rhi
or else Lhi
< Rlo
then
2377 -- All done if we did the rewrite
2379 if Nkind
(N
) not in N_Op_Compare
then
2384 -- Otherwise, time to do the comparison
2387 Ltype
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
2388 Rtype
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
2391 -- If the two operands have the same signed integer type we are
2392 -- all set, nothing more to do. This is the case where either
2393 -- both operands were unchanged, or we rewrote both of them to
2394 -- be Long_Long_Integer.
2396 -- Note: Entity for the comparison may be wrong, but it's not worth
2397 -- the effort to change it, since the back end does not use it.
2399 if Is_Signed_Integer_Type
(Ltype
)
2400 and then Base_Type
(Ltype
) = Base_Type
(Rtype
)
2404 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2406 elsif Is_RTE
(Ltype
, RE_Bignum
) or else Is_RTE
(Rtype
, RE_Bignum
) then
2408 Left
: Node_Id
:= Left_Opnd
(N
);
2409 Right
: Node_Id
:= Right_Opnd
(N
);
2410 -- Bignum references for left and right operands
2413 if not Is_RTE
(Ltype
, RE_Bignum
) then
2414 Left
:= Convert_To_Bignum
(Left
);
2415 elsif not Is_RTE
(Rtype
, RE_Bignum
) then
2416 Right
:= Convert_To_Bignum
(Right
);
2419 -- We rewrite our node with:
2422 -- Bnn : Result_Type;
2424 -- M : Mark_Id := SS_Mark;
2426 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2434 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
2435 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
2439 case N_Op_Compare
(Nkind
(N
)) is
2440 when N_Op_Eq
=> Ent
:= RE_Big_EQ
;
2441 when N_Op_Ge
=> Ent
:= RE_Big_GE
;
2442 when N_Op_Gt
=> Ent
:= RE_Big_GT
;
2443 when N_Op_Le
=> Ent
:= RE_Big_LE
;
2444 when N_Op_Lt
=> Ent
:= RE_Big_LT
;
2445 when N_Op_Ne
=> Ent
:= RE_Big_NE
;
2448 -- Insert assignment to Bnn into the bignum block
2451 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
2452 Make_Assignment_Statement
(Loc
,
2453 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
2455 Make_Function_Call
(Loc
,
2457 New_Occurrence_Of
(RTE
(Ent
), Loc
),
2458 Parameter_Associations
=> New_List
(Left
, Right
))));
2460 -- Now do the rewrite with expression actions
2463 Make_Expression_With_Actions
(Loc
,
2464 Actions
=> New_List
(
2465 Make_Object_Declaration
(Loc
,
2466 Defining_Identifier
=> Bnn
,
2467 Object_Definition
=>
2468 New_Occurrence_Of
(Result_Type
, Loc
)),
2470 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
2471 Analyze_And_Resolve
(N
, Result_Type
);
2475 -- No bignums involved, but types are different, so we must have
2476 -- rewritten one of the operands as a Long_Long_Integer but not
2479 -- If left operand is Long_Long_Integer, convert right operand
2480 -- and we are done (with a comparison of two Long_Long_Integers).
2482 elsif Ltype
= LLIB
then
2483 Convert_To_And_Rewrite
(LLIB
, Right_Opnd
(N
));
2484 Analyze_And_Resolve
(Right_Opnd
(N
), LLIB
, Suppress
=> All_Checks
);
2487 -- If right operand is Long_Long_Integer, convert left operand
2488 -- and we are done (with a comparison of two Long_Long_Integers).
2490 -- This is the only remaining possibility
2492 else pragma Assert
(Rtype
= LLIB
);
2493 Convert_To_And_Rewrite
(LLIB
, Left_Opnd
(N
));
2494 Analyze_And_Resolve
(Left_Opnd
(N
), LLIB
, Suppress
=> All_Checks
);
2498 end Expand_Compare_Minimize_Eliminate_Overflow
;
2500 -------------------------------
2501 -- Expand_Composite_Equality --
2502 -------------------------------
2504 -- This function is only called for comparing internal fields of composite
2505 -- types when these fields are themselves composites. This is a special
2506 -- case because it is not possible to respect normal Ada visibility rules.
2508 function Expand_Composite_Equality
2513 Bodies
: List_Id
) return Node_Id
2515 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
2516 Full_Type
: Entity_Id
;
2520 function Find_Primitive_Eq
return Node_Id
;
2521 -- AI05-0123: Locate primitive equality for type if it exists, and
2522 -- build the corresponding call. If operation is abstract, replace
2523 -- call with an explicit raise. Return Empty if there is no primitive.
2525 -----------------------
2526 -- Find_Primitive_Eq --
2527 -----------------------
2529 function Find_Primitive_Eq
return Node_Id
is
2534 Prim_E
:= First_Elmt
(Collect_Primitive_Operations
(Typ
));
2535 while Present
(Prim_E
) loop
2536 Prim
:= Node
(Prim_E
);
2538 -- Locate primitive equality with the right signature
2540 if Chars
(Prim
) = Name_Op_Eq
2541 and then Etype
(First_Formal
(Prim
)) =
2542 Etype
(Next_Formal
(First_Formal
(Prim
)))
2543 and then Etype
(Prim
) = Standard_Boolean
2545 if Is_Abstract_Subprogram
(Prim
) then
2547 Make_Raise_Program_Error
(Loc
,
2548 Reason
=> PE_Explicit_Raise
);
2552 Make_Function_Call
(Loc
,
2553 Name
=> New_Reference_To
(Prim
, Loc
),
2554 Parameter_Associations
=> New_List
(Lhs
, Rhs
));
2561 -- If not found, predefined operation will be used
2564 end Find_Primitive_Eq
;
2566 -- Start of processing for Expand_Composite_Equality
2569 if Is_Private_Type
(Typ
) then
2570 Full_Type
:= Underlying_Type
(Typ
);
2575 -- Defense against malformed private types with no completion the error
2576 -- will be diagnosed later by check_completion
2578 if No
(Full_Type
) then
2579 return New_Reference_To
(Standard_False
, Loc
);
2582 Full_Type
:= Base_Type
(Full_Type
);
2584 if Is_Array_Type
(Full_Type
) then
2586 -- If the operand is an elementary type other than a floating-point
2587 -- type, then we can simply use the built-in block bitwise equality,
2588 -- since the predefined equality operators always apply and bitwise
2589 -- equality is fine for all these cases.
2591 if Is_Elementary_Type
(Component_Type
(Full_Type
))
2592 and then not Is_Floating_Point_Type
(Component_Type
(Full_Type
))
2594 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2596 -- For composite component types, and floating-point types, use the
2597 -- expansion. This deals with tagged component types (where we use
2598 -- the applicable equality routine) and floating-point, (where we
2599 -- need to worry about negative zeroes), and also the case of any
2600 -- composite type recursively containing such fields.
2603 return Expand_Array_Equality
(Nod
, Lhs
, Rhs
, Bodies
, Full_Type
);
2606 elsif Is_Tagged_Type
(Full_Type
) then
2608 -- Call the primitive operation "=" of this type
2610 if Is_Class_Wide_Type
(Full_Type
) then
2611 Full_Type
:= Root_Type
(Full_Type
);
2614 -- If this is derived from an untagged private type completed with a
2615 -- tagged type, it does not have a full view, so we use the primitive
2616 -- operations of the private type. This check should no longer be
2617 -- necessary when these types receive their full views ???
2619 if Is_Private_Type
(Typ
)
2620 and then not Is_Tagged_Type
(Typ
)
2621 and then not Is_Controlled
(Typ
)
2622 and then Is_Derived_Type
(Typ
)
2623 and then No
(Full_View
(Typ
))
2625 Prim
:= First_Elmt
(Collect_Primitive_Operations
(Typ
));
2627 Prim
:= First_Elmt
(Primitive_Operations
(Full_Type
));
2631 Eq_Op
:= Node
(Prim
);
2632 exit when Chars
(Eq_Op
) = Name_Op_Eq
2633 and then Etype
(First_Formal
(Eq_Op
)) =
2634 Etype
(Next_Formal
(First_Formal
(Eq_Op
)))
2635 and then Base_Type
(Etype
(Eq_Op
)) = Standard_Boolean
;
2637 pragma Assert
(Present
(Prim
));
2640 Eq_Op
:= Node
(Prim
);
2643 Make_Function_Call
(Loc
,
2644 Name
=> New_Reference_To
(Eq_Op
, Loc
),
2645 Parameter_Associations
=>
2647 (Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Lhs
),
2648 Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Rhs
)));
2650 elsif Is_Record_Type
(Full_Type
) then
2651 Eq_Op
:= TSS
(Full_Type
, TSS_Composite_Equality
);
2653 if Present
(Eq_Op
) then
2654 if Etype
(First_Formal
(Eq_Op
)) /= Full_Type
then
2656 -- Inherited equality from parent type. Convert the actuals to
2657 -- match signature of operation.
2660 T
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
2664 Make_Function_Call
(Loc
,
2665 Name
=> New_Reference_To
(Eq_Op
, Loc
),
2666 Parameter_Associations
=> New_List
(
2667 OK_Convert_To
(T
, Lhs
),
2668 OK_Convert_To
(T
, Rhs
)));
2672 -- Comparison between Unchecked_Union components
2674 if Is_Unchecked_Union
(Full_Type
) then
2676 Lhs_Type
: Node_Id
:= Full_Type
;
2677 Rhs_Type
: Node_Id
:= Full_Type
;
2678 Lhs_Discr_Val
: Node_Id
;
2679 Rhs_Discr_Val
: Node_Id
;
2684 if Nkind
(Lhs
) = N_Selected_Component
then
2685 Lhs_Type
:= Etype
(Entity
(Selector_Name
(Lhs
)));
2690 if Nkind
(Rhs
) = N_Selected_Component
then
2691 Rhs_Type
:= Etype
(Entity
(Selector_Name
(Rhs
)));
2694 -- Lhs of the composite equality
2696 if Is_Constrained
(Lhs_Type
) then
2698 -- Since the enclosing record type can never be an
2699 -- Unchecked_Union (this code is executed for records
2700 -- that do not have variants), we may reference its
2703 if Nkind
(Lhs
) = N_Selected_Component
2704 and then Has_Per_Object_Constraint
(
2705 Entity
(Selector_Name
(Lhs
)))
2708 Make_Selected_Component
(Loc
,
2709 Prefix
=> Prefix
(Lhs
),
2712 (Get_Discriminant_Value
2713 (First_Discriminant
(Lhs_Type
),
2715 Stored_Constraint
(Lhs_Type
))));
2720 (Get_Discriminant_Value
2721 (First_Discriminant
(Lhs_Type
),
2723 Stored_Constraint
(Lhs_Type
)));
2727 -- It is not possible to infer the discriminant since
2728 -- the subtype is not constrained.
2731 Make_Raise_Program_Error
(Loc
,
2732 Reason
=> PE_Unchecked_Union_Restriction
);
2735 -- Rhs of the composite equality
2737 if Is_Constrained
(Rhs_Type
) then
2738 if Nkind
(Rhs
) = N_Selected_Component
2739 and then Has_Per_Object_Constraint
2740 (Entity
(Selector_Name
(Rhs
)))
2743 Make_Selected_Component
(Loc
,
2744 Prefix
=> Prefix
(Rhs
),
2747 (Get_Discriminant_Value
2748 (First_Discriminant
(Rhs_Type
),
2750 Stored_Constraint
(Rhs_Type
))));
2755 (Get_Discriminant_Value
2756 (First_Discriminant
(Rhs_Type
),
2758 Stored_Constraint
(Rhs_Type
)));
2763 Make_Raise_Program_Error
(Loc
,
2764 Reason
=> PE_Unchecked_Union_Restriction
);
2767 -- Call the TSS equality function with the inferred
2768 -- discriminant values.
2771 Make_Function_Call
(Loc
,
2772 Name
=> New_Reference_To
(Eq_Op
, Loc
),
2773 Parameter_Associations
=> New_List
(
2782 Make_Function_Call
(Loc
,
2783 Name
=> New_Reference_To
(Eq_Op
, Loc
),
2784 Parameter_Associations
=> New_List
(Lhs
, Rhs
));
2788 -- Equality composes in Ada 2012 for untagged record types. It also
2789 -- composes for bounded strings, because they are part of the
2790 -- predefined environment. We could make it compose for bounded
2791 -- strings by making them tagged, or by making sure all subcomponents
2792 -- are set to the same value, even when not used. Instead, we have
2793 -- this special case in the compiler, because it's more efficient.
2795 elsif Ada_Version
>= Ada_2012
or else Is_Bounded_String
(Typ
) then
2797 -- if no TSS has been created for the type, check whether there is
2798 -- a primitive equality declared for it.
2801 Op
: constant Node_Id
:= Find_Primitive_Eq
;
2804 -- Use user-defined primitive if it exists, otherwise use
2805 -- predefined equality.
2807 if Present
(Op
) then
2810 return Make_Op_Eq
(Loc
, Lhs
, Rhs
);
2815 return Expand_Record_Equality
(Nod
, Full_Type
, Lhs
, Rhs
, Bodies
);
2819 -- If not array or record type, it is predefined equality.
2821 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2823 end Expand_Composite_Equality
;
2825 ------------------------
2826 -- Expand_Concatenate --
2827 ------------------------
2829 procedure Expand_Concatenate
(Cnode
: Node_Id
; Opnds
: List_Id
) is
2830 Loc
: constant Source_Ptr
:= Sloc
(Cnode
);
2832 Atyp
: constant Entity_Id
:= Base_Type
(Etype
(Cnode
));
2833 -- Result type of concatenation
2835 Ctyp
: constant Entity_Id
:= Base_Type
(Component_Type
(Etype
(Cnode
)));
2836 -- Component type. Elements of this component type can appear as one
2837 -- of the operands of concatenation as well as arrays.
2839 Istyp
: constant Entity_Id
:= Etype
(First_Index
(Atyp
));
2842 Ityp
: constant Entity_Id
:= Base_Type
(Istyp
);
2843 -- Index type. This is the base type of the index subtype, and is used
2844 -- for all computed bounds (which may be out of range of Istyp in the
2845 -- case of null ranges).
2848 -- This is the type we use to do arithmetic to compute the bounds and
2849 -- lengths of operands. The choice of this type is a little subtle and
2850 -- is discussed in a separate section at the start of the body code.
2852 Concatenation_Error
: exception;
2853 -- Raised if concatenation is sure to raise a CE
2855 Result_May_Be_Null
: Boolean := True;
2856 -- Reset to False if at least one operand is encountered which is known
2857 -- at compile time to be non-null. Used for handling the special case
2858 -- of setting the high bound to the last operand high bound for a null
2859 -- result, thus ensuring a proper high bound in the super-flat case.
2861 N
: constant Nat
:= List_Length
(Opnds
);
2862 -- Number of concatenation operands including possibly null operands
2865 -- Number of operands excluding any known to be null, except that the
2866 -- last operand is always retained, in case it provides the bounds for
2870 -- Current operand being processed in the loop through operands. After
2871 -- this loop is complete, always contains the last operand (which is not
2872 -- the same as Operands (NN), since null operands are skipped).
2874 -- Arrays describing the operands, only the first NN entries of each
2875 -- array are set (NN < N when we exclude known null operands).
2877 Is_Fixed_Length
: array (1 .. N
) of Boolean;
2878 -- True if length of corresponding operand known at compile time
2880 Operands
: array (1 .. N
) of Node_Id
;
2881 -- Set to the corresponding entry in the Opnds list (but note that null
2882 -- operands are excluded, so not all entries in the list are stored).
2884 Fixed_Length
: array (1 .. N
) of Uint
;
2885 -- Set to length of operand. Entries in this array are set only if the
2886 -- corresponding entry in Is_Fixed_Length is True.
2888 Opnd_Low_Bound
: array (1 .. N
) of Node_Id
;
2889 -- Set to lower bound of operand. Either an integer literal in the case
2890 -- where the bound is known at compile time, else actual lower bound.
2891 -- The operand low bound is of type Ityp.
2893 Var_Length
: array (1 .. N
) of Entity_Id
;
2894 -- Set to an entity of type Natural that contains the length of an
2895 -- operand whose length is not known at compile time. Entries in this
2896 -- array are set only if the corresponding entry in Is_Fixed_Length
2897 -- is False. The entity is of type Artyp.
2899 Aggr_Length
: array (0 .. N
) of Node_Id
;
2900 -- The J'th entry in an expression node that represents the total length
2901 -- of operands 1 through J. It is either an integer literal node, or a
2902 -- reference to a constant entity with the right value, so it is fine
2903 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2904 -- entry always is set to zero. The length is of type Artyp.
2906 Low_Bound
: Node_Id
;
2907 -- A tree node representing the low bound of the result (of type Ityp).
2908 -- This is either an integer literal node, or an identifier reference to
2909 -- a constant entity initialized to the appropriate value.
2911 Last_Opnd_Low_Bound
: Node_Id
;
2912 -- A tree node representing the low bound of the last operand. This
2913 -- need only be set if the result could be null. It is used for the
2914 -- special case of setting the right low bound for a null result.
2915 -- This is of type Ityp.
2917 Last_Opnd_High_Bound
: Node_Id
;
2918 -- A tree node representing the high bound of the last operand. This
2919 -- need only be set if the result could be null. It is used for the
2920 -- special case of setting the right high bound for a null result.
2921 -- This is of type Ityp.
2923 High_Bound
: Node_Id
;
2924 -- A tree node representing the high bound of the result (of type Ityp)
2927 -- Result of the concatenation (of type Ityp)
2929 Actions
: constant List_Id
:= New_List
;
2930 -- Collect actions to be inserted
2932 Known_Non_Null_Operand_Seen
: Boolean;
2933 -- Set True during generation of the assignments of operands into
2934 -- result once an operand known to be non-null has been seen.
2936 function Make_Artyp_Literal
(Val
: Nat
) return Node_Id
;
2937 -- This function makes an N_Integer_Literal node that is returned in
2938 -- analyzed form with the type set to Artyp. Importantly this literal
2939 -- is not flagged as static, so that if we do computations with it that
2940 -- result in statically detected out of range conditions, we will not
2941 -- generate error messages but instead warning messages.
2943 function To_Artyp
(X
: Node_Id
) return Node_Id
;
2944 -- Given a node of type Ityp, returns the corresponding value of type
2945 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2946 -- For enum types, the Pos of the value is returned.
2948 function To_Ityp
(X
: Node_Id
) return Node_Id
;
2949 -- The inverse function (uses Val in the case of enumeration types)
2951 ------------------------
2952 -- Make_Artyp_Literal --
2953 ------------------------
2955 function Make_Artyp_Literal
(Val
: Nat
) return Node_Id
is
2956 Result
: constant Node_Id
:= Make_Integer_Literal
(Loc
, Val
);
2958 Set_Etype
(Result
, Artyp
);
2959 Set_Analyzed
(Result
, True);
2960 Set_Is_Static_Expression
(Result
, False);
2962 end Make_Artyp_Literal
;
2968 function To_Artyp
(X
: Node_Id
) return Node_Id
is
2970 if Ityp
= Base_Type
(Artyp
) then
2973 elsif Is_Enumeration_Type
(Ityp
) then
2975 Make_Attribute_Reference
(Loc
,
2976 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
2977 Attribute_Name
=> Name_Pos
,
2978 Expressions
=> New_List
(X
));
2981 return Convert_To
(Artyp
, X
);
2989 function To_Ityp
(X
: Node_Id
) return Node_Id
is
2991 if Is_Enumeration_Type
(Ityp
) then
2993 Make_Attribute_Reference
(Loc
,
2994 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
2995 Attribute_Name
=> Name_Val
,
2996 Expressions
=> New_List
(X
));
2998 -- Case where we will do a type conversion
3001 if Ityp
= Base_Type
(Artyp
) then
3004 return Convert_To
(Ityp
, X
);
3009 -- Local Declarations
3011 Opnd_Typ
: Entity_Id
;
3018 -- Start of processing for Expand_Concatenate
3021 -- Choose an appropriate computational type
3023 -- We will be doing calculations of lengths and bounds in this routine
3024 -- and computing one from the other in some cases, e.g. getting the high
3025 -- bound by adding the length-1 to the low bound.
3027 -- We can't just use the index type, or even its base type for this
3028 -- purpose for two reasons. First it might be an enumeration type which
3029 -- is not suitable for computations of any kind, and second it may
3030 -- simply not have enough range. For example if the index type is
3031 -- -128..+127 then lengths can be up to 256, which is out of range of
3034 -- For enumeration types, we can simply use Standard_Integer, this is
3035 -- sufficient since the actual number of enumeration literals cannot
3036 -- possibly exceed the range of integer (remember we will be doing the
3037 -- arithmetic with POS values, not representation values).
3039 if Is_Enumeration_Type
(Ityp
) then
3040 Artyp
:= Standard_Integer
;
3042 -- If index type is Positive, we use the standard unsigned type, to give
3043 -- more room on the top of the range, obviating the need for an overflow
3044 -- check when creating the upper bound. This is needed to avoid junk
3045 -- overflow checks in the common case of String types.
3047 -- ??? Disabled for now
3049 -- elsif Istyp = Standard_Positive then
3050 -- Artyp := Standard_Unsigned;
3052 -- For modular types, we use a 32-bit modular type for types whose size
3053 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
3054 -- identity type, and for larger unsigned types we use 64-bits.
3056 elsif Is_Modular_Integer_Type
(Ityp
) then
3057 if RM_Size
(Ityp
) < RM_Size
(Standard_Unsigned
) then
3058 Artyp
:= Standard_Unsigned
;
3059 elsif RM_Size
(Ityp
) = RM_Size
(Standard_Unsigned
) then
3062 Artyp
:= RTE
(RE_Long_Long_Unsigned
);
3065 -- Similar treatment for signed types
3068 if RM_Size
(Ityp
) < RM_Size
(Standard_Integer
) then
3069 Artyp
:= Standard_Integer
;
3070 elsif RM_Size
(Ityp
) = RM_Size
(Standard_Integer
) then
3073 Artyp
:= Standard_Long_Long_Integer
;
3077 -- Supply dummy entry at start of length array
3079 Aggr_Length
(0) := Make_Artyp_Literal
(0);
3081 -- Go through operands setting up the above arrays
3085 Opnd
:= Remove_Head
(Opnds
);
3086 Opnd_Typ
:= Etype
(Opnd
);
3088 -- The parent got messed up when we put the operands in a list,
3089 -- so now put back the proper parent for the saved operand, that
3090 -- is to say the concatenation node, to make sure that each operand
3091 -- is seen as a subexpression, e.g. if actions must be inserted.
3093 Set_Parent
(Opnd
, Cnode
);
3095 -- Set will be True when we have setup one entry in the array
3099 -- Singleton element (or character literal) case
3101 if Base_Type
(Opnd_Typ
) = Ctyp
then
3103 Operands
(NN
) := Opnd
;
3104 Is_Fixed_Length
(NN
) := True;
3105 Fixed_Length
(NN
) := Uint_1
;
3106 Result_May_Be_Null
:= False;
3108 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
3109 -- since we know that the result cannot be null).
3111 Opnd_Low_Bound
(NN
) :=
3112 Make_Attribute_Reference
(Loc
,
3113 Prefix
=> New_Reference_To
(Istyp
, Loc
),
3114 Attribute_Name
=> Name_First
);
3118 -- String literal case (can only occur for strings of course)
3120 elsif Nkind
(Opnd
) = N_String_Literal
then
3121 Len
:= String_Literal_Length
(Opnd_Typ
);
3124 Result_May_Be_Null
:= False;
3127 -- Capture last operand low and high bound if result could be null
3129 if J
= N
and then Result_May_Be_Null
then
3130 Last_Opnd_Low_Bound
:=
3131 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
));
3133 Last_Opnd_High_Bound
:=
3134 Make_Op_Subtract
(Loc
,
3136 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
)),
3137 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
3140 -- Skip null string literal
3142 if J
< N
and then Len
= 0 then
3147 Operands
(NN
) := Opnd
;
3148 Is_Fixed_Length
(NN
) := True;
3150 -- Set length and bounds
3152 Fixed_Length
(NN
) := Len
;
3154 Opnd_Low_Bound
(NN
) :=
3155 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
));
3162 -- Check constrained case with known bounds
3164 if Is_Constrained
(Opnd_Typ
) then
3166 Index
: constant Node_Id
:= First_Index
(Opnd_Typ
);
3167 Indx_Typ
: constant Entity_Id
:= Etype
(Index
);
3168 Lo
: constant Node_Id
:= Type_Low_Bound
(Indx_Typ
);
3169 Hi
: constant Node_Id
:= Type_High_Bound
(Indx_Typ
);
3172 -- Fixed length constrained array type with known at compile
3173 -- time bounds is last case of fixed length operand.
3175 if Compile_Time_Known_Value
(Lo
)
3177 Compile_Time_Known_Value
(Hi
)
3180 Loval
: constant Uint
:= Expr_Value
(Lo
);
3181 Hival
: constant Uint
:= Expr_Value
(Hi
);
3182 Len
: constant Uint
:=
3183 UI_Max
(Hival
- Loval
+ 1, Uint_0
);
3187 Result_May_Be_Null
:= False;
3190 -- Capture last operand bounds if result could be null
3192 if J
= N
and then Result_May_Be_Null
then
3193 Last_Opnd_Low_Bound
:=
3195 Make_Integer_Literal
(Loc
, Expr_Value
(Lo
)));
3197 Last_Opnd_High_Bound
:=
3199 Make_Integer_Literal
(Loc
, Expr_Value
(Hi
)));
3202 -- Exclude null length case unless last operand
3204 if J
< N
and then Len
= 0 then
3209 Operands
(NN
) := Opnd
;
3210 Is_Fixed_Length
(NN
) := True;
3211 Fixed_Length
(NN
) := Len
;
3213 Opnd_Low_Bound
(NN
) :=
3215 (Make_Integer_Literal
(Loc
, Expr_Value
(Lo
)));
3222 -- All cases where the length is not known at compile time, or the
3223 -- special case of an operand which is known to be null but has a
3224 -- lower bound other than 1 or is other than a string type.
3229 -- Capture operand bounds
3231 Opnd_Low_Bound
(NN
) :=
3232 Make_Attribute_Reference
(Loc
,
3234 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3235 Attribute_Name
=> Name_First
);
3237 -- Capture last operand bounds if result could be null
3239 if J
= N
and Result_May_Be_Null
then
3240 Last_Opnd_Low_Bound
:=
3242 Make_Attribute_Reference
(Loc
,
3244 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3245 Attribute_Name
=> Name_First
));
3247 Last_Opnd_High_Bound
:=
3249 Make_Attribute_Reference
(Loc
,
3251 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3252 Attribute_Name
=> Name_Last
));
3255 -- Capture length of operand in entity
3257 Operands
(NN
) := Opnd
;
3258 Is_Fixed_Length
(NN
) := False;
3260 Var_Length
(NN
) := Make_Temporary
(Loc
, 'L');
3263 Make_Object_Declaration
(Loc
,
3264 Defining_Identifier
=> Var_Length
(NN
),
3265 Constant_Present
=> True,
3266 Object_Definition
=> New_Occurrence_Of
(Artyp
, Loc
),
3268 Make_Attribute_Reference
(Loc
,
3270 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3271 Attribute_Name
=> Name_Length
)));
3275 -- Set next entry in aggregate length array
3277 -- For first entry, make either integer literal for fixed length
3278 -- or a reference to the saved length for variable length.
3281 if Is_Fixed_Length
(1) then
3282 Aggr_Length
(1) := Make_Integer_Literal
(Loc
, Fixed_Length
(1));
3284 Aggr_Length
(1) := New_Reference_To
(Var_Length
(1), Loc
);
3287 -- If entry is fixed length and only fixed lengths so far, make
3288 -- appropriate new integer literal adding new length.
3290 elsif Is_Fixed_Length
(NN
)
3291 and then Nkind
(Aggr_Length
(NN
- 1)) = N_Integer_Literal
3294 Make_Integer_Literal
(Loc
,
3295 Intval
=> Fixed_Length
(NN
) + Intval
(Aggr_Length
(NN
- 1)));
3297 -- All other cases, construct an addition node for the length and
3298 -- create an entity initialized to this length.
3301 Ent
:= Make_Temporary
(Loc
, 'L');
3303 if Is_Fixed_Length
(NN
) then
3304 Clen
:= Make_Integer_Literal
(Loc
, Fixed_Length
(NN
));
3306 Clen
:= New_Reference_To
(Var_Length
(NN
), Loc
);
3310 Make_Object_Declaration
(Loc
,
3311 Defining_Identifier
=> Ent
,
3312 Constant_Present
=> True,
3313 Object_Definition
=> New_Occurrence_Of
(Artyp
, Loc
),
3316 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
- 1)),
3317 Right_Opnd
=> Clen
)));
3319 Aggr_Length
(NN
) := Make_Identifier
(Loc
, Chars
=> Chars
(Ent
));
3326 -- If we have only skipped null operands, return the last operand
3333 -- If we have only one non-null operand, return it and we are done.
3334 -- There is one case in which this cannot be done, and that is when
3335 -- the sole operand is of the element type, in which case it must be
3336 -- converted to an array, and the easiest way of doing that is to go
3337 -- through the normal general circuit.
3340 and then Base_Type
(Etype
(Operands
(1))) /= Ctyp
3342 Result
:= Operands
(1);
3346 -- Cases where we have a real concatenation
3348 -- Next step is to find the low bound for the result array that we
3349 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3351 -- If the ultimate ancestor of the index subtype is a constrained array
3352 -- definition, then the lower bound is that of the index subtype as
3353 -- specified by (RM 4.5.3(6)).
3355 -- The right test here is to go to the root type, and then the ultimate
3356 -- ancestor is the first subtype of this root type.
3358 if Is_Constrained
(First_Subtype
(Root_Type
(Atyp
))) then
3360 Make_Attribute_Reference
(Loc
,
3362 New_Occurrence_Of
(First_Subtype
(Root_Type
(Atyp
)), Loc
),
3363 Attribute_Name
=> Name_First
);
3365 -- If the first operand in the list has known length we know that
3366 -- the lower bound of the result is the lower bound of this operand.
3368 elsif Is_Fixed_Length
(1) then
3369 Low_Bound
:= Opnd_Low_Bound
(1);
3371 -- OK, we don't know the lower bound, we have to build a horrible
3372 -- if expression node of the form
3374 -- if Cond1'Length /= 0 then
3377 -- if Opnd2'Length /= 0 then
3382 -- The nesting ends either when we hit an operand whose length is known
3383 -- at compile time, or on reaching the last operand, whose low bound we
3384 -- take unconditionally whether or not it is null. It's easiest to do
3385 -- this with a recursive procedure:
3389 function Get_Known_Bound
(J
: Nat
) return Node_Id
;
3390 -- Returns the lower bound determined by operands J .. NN
3392 ---------------------
3393 -- Get_Known_Bound --
3394 ---------------------
3396 function Get_Known_Bound
(J
: Nat
) return Node_Id
is
3398 if Is_Fixed_Length
(J
) or else J
= NN
then
3399 return New_Copy
(Opnd_Low_Bound
(J
));
3403 Make_If_Expression
(Loc
,
3404 Expressions
=> New_List
(
3407 Left_Opnd
=> New_Reference_To
(Var_Length
(J
), Loc
),
3408 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
3410 New_Copy
(Opnd_Low_Bound
(J
)),
3411 Get_Known_Bound
(J
+ 1)));
3413 end Get_Known_Bound
;
3416 Ent
:= Make_Temporary
(Loc
, 'L');
3419 Make_Object_Declaration
(Loc
,
3420 Defining_Identifier
=> Ent
,
3421 Constant_Present
=> True,
3422 Object_Definition
=> New_Occurrence_Of
(Ityp
, Loc
),
3423 Expression
=> Get_Known_Bound
(1)));
3425 Low_Bound
:= New_Reference_To
(Ent
, Loc
);
3429 -- Now we can safely compute the upper bound, normally
3430 -- Low_Bound + Length - 1.
3435 Left_Opnd
=> To_Artyp
(New_Copy
(Low_Bound
)),
3437 Make_Op_Subtract
(Loc
,
3438 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
)),
3439 Right_Opnd
=> Make_Artyp_Literal
(1))));
3441 -- Note that calculation of the high bound may cause overflow in some
3442 -- very weird cases, so in the general case we need an overflow check on
3443 -- the high bound. We can avoid this for the common case of string types
3444 -- and other types whose index is Positive, since we chose a wider range
3445 -- for the arithmetic type.
3447 if Istyp
/= Standard_Positive
then
3448 Activate_Overflow_Check
(High_Bound
);
3451 -- Handle the exceptional case where the result is null, in which case
3452 -- case the bounds come from the last operand (so that we get the proper
3453 -- bounds if the last operand is super-flat).
3455 if Result_May_Be_Null
then
3457 Make_If_Expression
(Loc
,
3458 Expressions
=> New_List
(
3460 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
)),
3461 Right_Opnd
=> Make_Artyp_Literal
(0)),
3462 Last_Opnd_Low_Bound
,
3466 Make_If_Expression
(Loc
,
3467 Expressions
=> New_List
(
3469 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
)),
3470 Right_Opnd
=> Make_Artyp_Literal
(0)),
3471 Last_Opnd_High_Bound
,
3475 -- Here is where we insert the saved up actions
3477 Insert_Actions
(Cnode
, Actions
, Suppress
=> All_Checks
);
3479 -- Now we construct an array object with appropriate bounds. We mark
3480 -- the target as internal to prevent useless initialization when
3481 -- Initialize_Scalars is enabled. Also since this is the actual result
3482 -- entity, we make sure we have debug information for the result.
3484 Ent
:= Make_Temporary
(Loc
, 'S');
3485 Set_Is_Internal
(Ent
);
3486 Set_Needs_Debug_Info
(Ent
);
3488 -- If the bound is statically known to be out of range, we do not want
3489 -- to abort, we want a warning and a runtime constraint error. Note that
3490 -- we have arranged that the result will not be treated as a static
3491 -- constant, so we won't get an illegality during this insertion.
3493 Insert_Action
(Cnode
,
3494 Make_Object_Declaration
(Loc
,
3495 Defining_Identifier
=> Ent
,
3496 Object_Definition
=>
3497 Make_Subtype_Indication
(Loc
,
3498 Subtype_Mark
=> New_Occurrence_Of
(Atyp
, Loc
),
3500 Make_Index_Or_Discriminant_Constraint
(Loc
,
3501 Constraints
=> New_List
(
3503 Low_Bound
=> Low_Bound
,
3504 High_Bound
=> High_Bound
))))),
3505 Suppress
=> All_Checks
);
3507 -- If the result of the concatenation appears as the initializing
3508 -- expression of an object declaration, we can just rename the
3509 -- result, rather than copying it.
3511 Set_OK_To_Rename
(Ent
);
3513 -- Catch the static out of range case now
3515 if Raises_Constraint_Error
(High_Bound
) then
3516 raise Concatenation_Error
;
3519 -- Now we will generate the assignments to do the actual concatenation
3521 -- There is one case in which we will not do this, namely when all the
3522 -- following conditions are met:
3524 -- The result type is Standard.String
3526 -- There are nine or fewer retained (non-null) operands
3528 -- The optimization level is -O0
3530 -- The corresponding System.Concat_n.Str_Concat_n routine is
3531 -- available in the run time.
3533 -- The debug flag gnatd.c is not set
3535 -- If all these conditions are met then we generate a call to the
3536 -- relevant concatenation routine. The purpose of this is to avoid
3537 -- undesirable code bloat at -O0.
3539 if Atyp
= Standard_String
3540 and then NN
in 2 .. 9
3541 and then (Opt
.Optimization_Level
= 0 or else Debug_Flag_Dot_CC
)
3542 and then not Debug_Flag_Dot_C
3545 RR
: constant array (Nat
range 2 .. 9) of RE_Id
:=
3556 if RTE_Available
(RR
(NN
)) then
3558 Opnds
: constant List_Id
:=
3559 New_List
(New_Occurrence_Of
(Ent
, Loc
));
3562 for J
in 1 .. NN
loop
3563 if Is_List_Member
(Operands
(J
)) then
3564 Remove
(Operands
(J
));
3567 if Base_Type
(Etype
(Operands
(J
))) = Ctyp
then
3569 Make_Aggregate
(Loc
,
3570 Component_Associations
=> New_List
(
3571 Make_Component_Association
(Loc
,
3572 Choices
=> New_List
(
3573 Make_Integer_Literal
(Loc
, 1)),
3574 Expression
=> Operands
(J
)))));
3577 Append_To
(Opnds
, Operands
(J
));
3581 Insert_Action
(Cnode
,
3582 Make_Procedure_Call_Statement
(Loc
,
3583 Name
=> New_Reference_To
(RTE
(RR
(NN
)), Loc
),
3584 Parameter_Associations
=> Opnds
));
3586 Result
:= New_Reference_To
(Ent
, Loc
);
3593 -- Not special case so generate the assignments
3595 Known_Non_Null_Operand_Seen
:= False;
3597 for J
in 1 .. NN
loop
3599 Lo
: constant Node_Id
:=
3601 Left_Opnd
=> To_Artyp
(New_Copy
(Low_Bound
)),
3602 Right_Opnd
=> Aggr_Length
(J
- 1));
3604 Hi
: constant Node_Id
:=
3606 Left_Opnd
=> To_Artyp
(New_Copy
(Low_Bound
)),
3608 Make_Op_Subtract
(Loc
,
3609 Left_Opnd
=> Aggr_Length
(J
),
3610 Right_Opnd
=> Make_Artyp_Literal
(1)));
3613 -- Singleton case, simple assignment
3615 if Base_Type
(Etype
(Operands
(J
))) = Ctyp
then
3616 Known_Non_Null_Operand_Seen
:= True;
3617 Insert_Action
(Cnode
,
3618 Make_Assignment_Statement
(Loc
,
3620 Make_Indexed_Component
(Loc
,
3621 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
3622 Expressions
=> New_List
(To_Ityp
(Lo
))),
3623 Expression
=> Operands
(J
)),
3624 Suppress
=> All_Checks
);
3626 -- Array case, slice assignment, skipped when argument is fixed
3627 -- length and known to be null.
3629 elsif (not Is_Fixed_Length
(J
)) or else (Fixed_Length
(J
) > 0) then
3632 Make_Assignment_Statement
(Loc
,
3636 New_Occurrence_Of
(Ent
, Loc
),
3639 Low_Bound
=> To_Ityp
(Lo
),
3640 High_Bound
=> To_Ityp
(Hi
))),
3641 Expression
=> Operands
(J
));
3643 if Is_Fixed_Length
(J
) then
3644 Known_Non_Null_Operand_Seen
:= True;
3646 elsif not Known_Non_Null_Operand_Seen
then
3648 -- Here if operand length is not statically known and no
3649 -- operand known to be non-null has been processed yet.
3650 -- If operand length is 0, we do not need to perform the
3651 -- assignment, and we must avoid the evaluation of the
3652 -- high bound of the slice, since it may underflow if the
3653 -- low bound is Ityp'First.
3656 Make_Implicit_If_Statement
(Cnode
,
3660 New_Occurrence_Of
(Var_Length
(J
), Loc
),
3661 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
3662 Then_Statements
=> New_List
(Assign
));
3665 Insert_Action
(Cnode
, Assign
, Suppress
=> All_Checks
);
3671 -- Finally we build the result, which is a reference to the array object
3673 Result
:= New_Reference_To
(Ent
, Loc
);
3676 Rewrite
(Cnode
, Result
);
3677 Analyze_And_Resolve
(Cnode
, Atyp
);
3680 when Concatenation_Error
=>
3682 -- Kill warning generated for the declaration of the static out of
3683 -- range high bound, and instead generate a Constraint_Error with
3684 -- an appropriate specific message.
3686 Kill_Dead_Code
(Declaration_Node
(Entity
(High_Bound
)));
3687 Apply_Compile_Time_Constraint_Error
3689 Msg
=> "concatenation result upper bound out of range?",
3690 Reason
=> CE_Range_Check_Failed
);
3691 end Expand_Concatenate
;
3693 ---------------------------------------------------
3694 -- Expand_Membership_Minimize_Eliminate_Overflow --
3695 ---------------------------------------------------
3697 procedure Expand_Membership_Minimize_Eliminate_Overflow
(N
: Node_Id
) is
3698 pragma Assert
(Nkind
(N
) = N_In
);
3699 -- Despite the name, this routine applies only to N_In, not to
3700 -- N_Not_In. The latter is always rewritten as not (X in Y).
3702 Result_Type
: constant Entity_Id
:= Etype
(N
);
3703 -- Capture result type, may be a derived boolean type
3705 Loc
: constant Source_Ptr
:= Sloc
(N
);
3706 Lop
: constant Node_Id
:= Left_Opnd
(N
);
3707 Rop
: constant Node_Id
:= Right_Opnd
(N
);
3709 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3710 -- is thus tempting to capture these values, but due to the rewrites
3711 -- that occur as a result of overflow checking, these values change
3712 -- as we go along, and it is safe just to always use Etype explicitly.
3714 Restype
: constant Entity_Id
:= Etype
(N
);
3718 -- Bounds in Minimize calls, not used currently
3720 LLIB
: constant Entity_Id
:= Base_Type
(Standard_Long_Long_Integer
);
3721 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3724 Minimize_Eliminate_Overflows
(Lop
, Lo
, Hi
, Top_Level
=> False);
3726 -- If right operand is a subtype name, and the subtype name has no
3727 -- predicate, then we can just replace the right operand with an
3728 -- explicit range T'First .. T'Last, and use the explicit range code.
3730 if Nkind
(Rop
) /= N_Range
3731 and then No
(Predicate_Function
(Etype
(Rop
)))
3734 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
3739 Make_Attribute_Reference
(Loc
,
3740 Attribute_Name
=> Name_First
,
3741 Prefix
=> New_Reference_To
(Rtyp
, Loc
)),
3743 Make_Attribute_Reference
(Loc
,
3744 Attribute_Name
=> Name_Last
,
3745 Prefix
=> New_Reference_To
(Rtyp
, Loc
))));
3746 Analyze_And_Resolve
(Rop
, Rtyp
, Suppress
=> All_Checks
);
3750 -- Here for the explicit range case. Note that the bounds of the range
3751 -- have not been processed for minimized or eliminated checks.
3753 if Nkind
(Rop
) = N_Range
then
3754 Minimize_Eliminate_Overflows
3755 (Low_Bound
(Rop
), Lo
, Hi
, Top_Level
=> False);
3756 Minimize_Eliminate_Overflows
3757 (High_Bound
(Rop
), Lo
, Hi
, Top_Level
=> False);
3759 -- We have A in B .. C, treated as A >= B and then A <= C
3763 if Is_RTE
(Etype
(Lop
), RE_Bignum
)
3764 or else Is_RTE
(Etype
(Low_Bound
(Rop
)), RE_Bignum
)
3765 or else Is_RTE
(Etype
(High_Bound
(Rop
)), RE_Bignum
)
3768 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
3769 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
3770 L
: constant Entity_Id
:=
3771 Make_Defining_Identifier
(Loc
, Name_uL
);
3772 Lopnd
: constant Node_Id
:= Convert_To_Bignum
(Lop
);
3773 Lbound
: constant Node_Id
:=
3774 Convert_To_Bignum
(Low_Bound
(Rop
));
3775 Hbound
: constant Node_Id
:=
3776 Convert_To_Bignum
(High_Bound
(Rop
));
3778 -- Now we rewrite the membership test node to look like
3781 -- Bnn : Result_Type;
3783 -- M : Mark_Id := SS_Mark;
3784 -- L : Bignum := Lopnd;
3786 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3794 -- Insert declaration of L into declarations of bignum block
3797 (Last
(Declarations
(Blk
)),
3798 Make_Object_Declaration
(Loc
,
3799 Defining_Identifier
=> L
,
3800 Object_Definition
=>
3801 New_Occurrence_Of
(RTE
(RE_Bignum
), Loc
),
3802 Expression
=> Lopnd
));
3804 -- Insert assignment to Bnn into expressions of bignum block
3807 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
3808 Make_Assignment_Statement
(Loc
,
3809 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
3813 Make_Function_Call
(Loc
,
3815 New_Occurrence_Of
(RTE
(RE_Big_GE
), Loc
),
3816 Parameter_Associations
=> New_List
(
3817 New_Occurrence_Of
(L
, Loc
),
3820 Make_Function_Call
(Loc
,
3822 New_Occurrence_Of
(RTE
(RE_Big_LE
), Loc
),
3823 Parameter_Associations
=> New_List
(
3824 New_Occurrence_Of
(L
, Loc
),
3827 -- Now rewrite the node
3830 Make_Expression_With_Actions
(Loc
,
3831 Actions
=> New_List
(
3832 Make_Object_Declaration
(Loc
,
3833 Defining_Identifier
=> Bnn
,
3834 Object_Definition
=>
3835 New_Occurrence_Of
(Result_Type
, Loc
)),
3837 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
3838 Analyze_And_Resolve
(N
, Result_Type
);
3842 -- Here if no bignums around
3845 -- Case where types are all the same
3847 if Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(Low_Bound
(Rop
)))
3849 Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(High_Bound
(Rop
)))
3853 -- If types are not all the same, it means that we have rewritten
3854 -- at least one of them to be of type Long_Long_Integer, and we
3855 -- will convert the other operands to Long_Long_Integer.
3858 Convert_To_And_Rewrite
(LLIB
, Lop
);
3859 Set_Analyzed
(Lop
, False);
3860 Analyze_And_Resolve
(Lop
, LLIB
);
3862 -- For the right operand, avoid unnecessary recursion into
3863 -- this routine, we know that overflow is not possible.
3865 Convert_To_And_Rewrite
(LLIB
, Low_Bound
(Rop
));
3866 Convert_To_And_Rewrite
(LLIB
, High_Bound
(Rop
));
3867 Set_Analyzed
(Rop
, False);
3868 Analyze_And_Resolve
(Rop
, LLIB
, Suppress
=> Overflow_Check
);
3871 -- Now the three operands are of the same signed integer type,
3872 -- so we can use the normal expansion routine for membership,
3873 -- setting the flag to prevent recursion into this procedure.
3875 Set_No_Minimize_Eliminate
(N
);
3879 -- Right operand is a subtype name and the subtype has a predicate. We
3880 -- have to make sure the predicate is checked, and for that we need to
3881 -- use the standard N_In circuitry with appropriate types.
3884 pragma Assert
(Present
(Predicate_Function
(Etype
(Rop
))));
3886 -- If types are "right", just call Expand_N_In preventing recursion
3888 if Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(Rop
)) then
3889 Set_No_Minimize_Eliminate
(N
);
3894 elsif Is_RTE
(Etype
(Lop
), RE_Bignum
) then
3896 -- For X in T, we want to rewrite our node as
3899 -- Bnn : Result_Type;
3902 -- M : Mark_Id := SS_Mark;
3903 -- Lnn : Long_Long_Integer'Base
3909 -- if not Bignum_In_LLI_Range (Nnn) then
3912 -- Lnn := From_Bignum (Nnn);
3914 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3915 -- and then T'Base (Lnn) in T;
3924 -- A bit gruesome, but there doesn't seem to be a simpler way
3927 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
3928 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
3929 Lnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'L', N
);
3930 Nnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'N', N
);
3931 T
: constant Entity_Id
:= Etype
(Rop
);
3932 TB
: constant Entity_Id
:= Base_Type
(T
);
3936 -- Mark the last membership operation to prevent recursion
3940 Left_Opnd
=> Convert_To
(TB
, New_Occurrence_Of
(Lnn
, Loc
)),
3941 Right_Opnd
=> New_Occurrence_Of
(T
, Loc
));
3942 Set_No_Minimize_Eliminate
(Nin
);
3944 -- Now decorate the block
3947 (Last
(Declarations
(Blk
)),
3948 Make_Object_Declaration
(Loc
,
3949 Defining_Identifier
=> Lnn
,
3950 Object_Definition
=> New_Occurrence_Of
(LLIB
, Loc
)));
3953 (Last
(Declarations
(Blk
)),
3954 Make_Object_Declaration
(Loc
,
3955 Defining_Identifier
=> Nnn
,
3956 Object_Definition
=>
3957 New_Occurrence_Of
(RTE
(RE_Bignum
), Loc
)));
3960 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
3962 Make_Assignment_Statement
(Loc
,
3963 Name
=> New_Occurrence_Of
(Nnn
, Loc
),
3964 Expression
=> Relocate_Node
(Lop
)),
3966 Make_If_Statement
(Loc
,
3970 Make_Function_Call
(Loc
,
3973 (RTE
(RE_Bignum_In_LLI_Range
), Loc
),
3974 Parameter_Associations
=> New_List
(
3975 New_Occurrence_Of
(Nnn
, Loc
)))),
3977 Then_Statements
=> New_List
(
3978 Make_Assignment_Statement
(Loc
,
3979 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
3981 New_Occurrence_Of
(Standard_False
, Loc
))),
3983 Else_Statements
=> New_List
(
3984 Make_Assignment_Statement
(Loc
,
3985 Name
=> New_Occurrence_Of
(Lnn
, Loc
),
3987 Make_Function_Call
(Loc
,
3989 New_Occurrence_Of
(RTE
(RE_From_Bignum
), Loc
),
3990 Parameter_Associations
=> New_List
(
3991 New_Occurrence_Of
(Nnn
, Loc
)))),
3993 Make_Assignment_Statement
(Loc
,
3994 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
3999 Left_Opnd
=> New_Occurrence_Of
(Lnn
, Loc
),
4004 Make_Attribute_Reference
(Loc
,
4005 Attribute_Name
=> Name_First
,
4007 New_Occurrence_Of
(TB
, Loc
))),
4011 Make_Attribute_Reference
(Loc
,
4012 Attribute_Name
=> Name_Last
,
4014 New_Occurrence_Of
(TB
, Loc
))))),
4016 Right_Opnd
=> Nin
))))));
4018 -- Now we can do the rewrite
4021 Make_Expression_With_Actions
(Loc
,
4022 Actions
=> New_List
(
4023 Make_Object_Declaration
(Loc
,
4024 Defining_Identifier
=> Bnn
,
4025 Object_Definition
=>
4026 New_Occurrence_Of
(Result_Type
, Loc
)),
4028 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
4029 Analyze_And_Resolve
(N
, Result_Type
);
4033 -- Not bignum case, but types don't match (this means we rewrote the
4034 -- left operand to be Long_Long_Integer).
4037 pragma Assert
(Base_Type
(Etype
(Lop
)) = LLIB
);
4039 -- We rewrite the membership test as (where T is the type with
4040 -- the predicate, i.e. the type of the right operand)
4042 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
4043 -- and then T'Base (Lop) in T
4046 T
: constant Entity_Id
:= Etype
(Rop
);
4047 TB
: constant Entity_Id
:= Base_Type
(T
);
4051 -- The last membership test is marked to prevent recursion
4055 Left_Opnd
=> Convert_To
(TB
, Duplicate_Subexpr
(Lop
)),
4056 Right_Opnd
=> New_Occurrence_Of
(T
, Loc
));
4057 Set_No_Minimize_Eliminate
(Nin
);
4059 -- Now do the rewrite
4070 Make_Attribute_Reference
(Loc
,
4071 Attribute_Name
=> Name_First
,
4072 Prefix
=> New_Occurrence_Of
(TB
, Loc
))),
4075 Make_Attribute_Reference
(Loc
,
4076 Attribute_Name
=> Name_Last
,
4077 Prefix
=> New_Occurrence_Of
(TB
, Loc
))))),
4078 Right_Opnd
=> Nin
));
4079 Set_Analyzed
(N
, False);
4080 Analyze_And_Resolve
(N
, Restype
);
4084 end Expand_Membership_Minimize_Eliminate_Overflow
;
4086 ------------------------
4087 -- Expand_N_Allocator --
4088 ------------------------
4090 procedure Expand_N_Allocator
(N
: Node_Id
) is
4091 PtrT
: constant Entity_Id
:= Etype
(N
);
4092 Dtyp
: constant Entity_Id
:= Available_View
(Designated_Type
(PtrT
));
4093 Etyp
: constant Entity_Id
:= Etype
(Expression
(N
));
4094 Loc
: constant Source_Ptr
:= Sloc
(N
);
4100 procedure Rewrite_Coextension
(N
: Node_Id
);
4101 -- Static coextensions have the same lifetime as the entity they
4102 -- constrain. Such occurrences can be rewritten as aliased objects
4103 -- and their unrestricted access used instead of the coextension.
4105 function Size_In_Storage_Elements
(E
: Entity_Id
) return Node_Id
;
4106 -- Given a constrained array type E, returns a node representing the
4107 -- code to compute the size in storage elements for the given type.
4108 -- This is done without using the attribute (which malfunctions for
4111 -------------------------
4112 -- Rewrite_Coextension --
4113 -------------------------
4115 procedure Rewrite_Coextension
(N
: Node_Id
) is
4116 Temp_Id
: constant Node_Id
:= Make_Temporary
(Loc
, 'C');
4117 Temp_Decl
: Node_Id
;
4121 -- Cnn : aliased Etyp;
4124 Make_Object_Declaration
(Loc
,
4125 Defining_Identifier
=> Temp_Id
,
4126 Aliased_Present
=> True,
4127 Object_Definition
=> New_Occurrence_Of
(Etyp
, Loc
));
4129 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
4130 Set_Expression
(Temp_Decl
, Expression
(Expression
(N
)));
4133 Insert_Action
(N
, Temp_Decl
);
4135 Make_Attribute_Reference
(Loc
,
4136 Prefix
=> New_Occurrence_Of
(Temp_Id
, Loc
),
4137 Attribute_Name
=> Name_Unrestricted_Access
));
4139 Analyze_And_Resolve
(N
, PtrT
);
4140 end Rewrite_Coextension
;
4142 ------------------------------
4143 -- Size_In_Storage_Elements --
4144 ------------------------------
4146 function Size_In_Storage_Elements
(E
: Entity_Id
) return Node_Id
is
4148 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4149 -- However, the reason for the existence of this function is
4150 -- to construct a test for sizes too large, which means near the
4151 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4152 -- is that we get overflows when sizes are greater than 2**31.
4154 -- So what we end up doing for array types is to use the expression:
4156 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4158 -- which avoids this problem. All this is a bit bogus, but it does
4159 -- mean we catch common cases of trying to allocate arrays that
4160 -- are too large, and which in the absence of a check results in
4161 -- undetected chaos ???
4168 for J
in 1 .. Number_Dimensions
(E
) loop
4170 Make_Attribute_Reference
(Loc
,
4171 Prefix
=> New_Occurrence_Of
(E
, Loc
),
4172 Attribute_Name
=> Name_Length
,
4173 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, J
)));
4180 Make_Op_Multiply
(Loc
,
4187 Make_Op_Multiply
(Loc
,
4190 Make_Attribute_Reference
(Loc
,
4191 Prefix
=> New_Occurrence_Of
(Component_Type
(E
), Loc
),
4192 Attribute_Name
=> Name_Max_Size_In_Storage_Elements
));
4194 end Size_In_Storage_Elements
;
4196 -- Start of processing for Expand_N_Allocator
4199 -- RM E.2.3(22). We enforce that the expected type of an allocator
4200 -- shall not be a remote access-to-class-wide-limited-private type
4202 -- Why is this being done at expansion time, seems clearly wrong ???
4204 Validate_Remote_Access_To_Class_Wide_Type
(N
);
4206 -- Processing for anonymous access-to-controlled types. These access
4207 -- types receive a special finalization master which appears in the
4208 -- declarations of the enclosing semantic unit. This expansion is done
4209 -- now to ensure that any additional types generated by this routine or
4210 -- Expand_Allocator_Expression inherit the proper type attributes.
4212 if (Ekind
(PtrT
) = E_Anonymous_Access_Type
4214 (Is_Itype
(PtrT
) and then No
(Finalization_Master
(PtrT
))))
4215 and then Needs_Finalization
(Dtyp
)
4217 -- Anonymous access-to-controlled types allocate on the global pool.
4218 -- Do not set this attribute on .NET/JVM since those targets do not
4221 if No
(Associated_Storage_Pool
(PtrT
)) and then VM_Target
= No_VM
then
4222 Set_Associated_Storage_Pool
4223 (PtrT
, Get_Global_Pool_For_Access_Type
(PtrT
));
4226 -- The finalization master must be inserted and analyzed as part of
4227 -- the current semantic unit. This form of expansion is not carried
4228 -- out in Alfa mode because it is useless. Note that the master is
4229 -- updated when analysis changes current units.
4231 if not Alfa_Mode
then
4232 Set_Finalization_Master
(PtrT
, Current_Anonymous_Master
);
4236 -- Set the storage pool and find the appropriate version of Allocate to
4237 -- call. Do not overwrite the storage pool if it is already set, which
4238 -- can happen for build-in-place function returns (see
4239 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4241 if No
(Storage_Pool
(N
)) then
4242 Pool
:= Associated_Storage_Pool
(Root_Type
(PtrT
));
4244 if Present
(Pool
) then
4245 Set_Storage_Pool
(N
, Pool
);
4247 if Is_RTE
(Pool
, RE_SS_Pool
) then
4248 if VM_Target
= No_VM
then
4249 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
4252 -- In the case of an allocator for a simple storage pool, locate
4253 -- and save a reference to the pool type's Allocate routine.
4255 elsif Present
(Get_Rep_Pragma
4256 (Etype
(Pool
), Name_Simple_Storage_Pool_Type
))
4259 Pool_Type
: constant Entity_Id
:= Base_Type
(Etype
(Pool
));
4260 Alloc_Op
: Entity_Id
;
4262 Alloc_Op
:= Get_Name_Entity_Id
(Name_Allocate
);
4263 while Present
(Alloc_Op
) loop
4264 if Scope
(Alloc_Op
) = Scope
(Pool_Type
)
4265 and then Present
(First_Formal
(Alloc_Op
))
4266 and then Etype
(First_Formal
(Alloc_Op
)) = Pool_Type
4268 Set_Procedure_To_Call
(N
, Alloc_Op
);
4271 Alloc_Op
:= Homonym
(Alloc_Op
);
4276 elsif Is_Class_Wide_Type
(Etype
(Pool
)) then
4277 Set_Procedure_To_Call
(N
, RTE
(RE_Allocate_Any
));
4280 Set_Procedure_To_Call
(N
,
4281 Find_Prim_Op
(Etype
(Pool
), Name_Allocate
));
4286 -- Under certain circumstances we can replace an allocator by an access
4287 -- to statically allocated storage. The conditions, as noted in AARM
4288 -- 3.10 (10c) are as follows:
4290 -- Size and initial value is known at compile time
4291 -- Access type is access-to-constant
4293 -- The allocator is not part of a constraint on a record component,
4294 -- because in that case the inserted actions are delayed until the
4295 -- record declaration is fully analyzed, which is too late for the
4296 -- analysis of the rewritten allocator.
4298 if Is_Access_Constant
(PtrT
)
4299 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
4300 and then Compile_Time_Known_Value
(Expression
(Expression
(N
)))
4301 and then Size_Known_At_Compile_Time
4302 (Etype
(Expression
(Expression
(N
))))
4303 and then not Is_Record_Type
(Current_Scope
)
4305 -- Here we can do the optimization. For the allocator
4309 -- We insert an object declaration
4311 -- Tnn : aliased x := y;
4313 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4314 -- marked as requiring static allocation.
4316 Temp
:= Make_Temporary
(Loc
, 'T', Expression
(Expression
(N
)));
4317 Desig
:= Subtype_Mark
(Expression
(N
));
4319 -- If context is constrained, use constrained subtype directly,
4320 -- so that the constant is not labelled as having a nominally
4321 -- unconstrained subtype.
4323 if Entity
(Desig
) = Base_Type
(Dtyp
) then
4324 Desig
:= New_Occurrence_Of
(Dtyp
, Loc
);
4328 Make_Object_Declaration
(Loc
,
4329 Defining_Identifier
=> Temp
,
4330 Aliased_Present
=> True,
4331 Constant_Present
=> Is_Access_Constant
(PtrT
),
4332 Object_Definition
=> Desig
,
4333 Expression
=> Expression
(Expression
(N
))));
4336 Make_Attribute_Reference
(Loc
,
4337 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
4338 Attribute_Name
=> Name_Unrestricted_Access
));
4340 Analyze_And_Resolve
(N
, PtrT
);
4342 -- We set the variable as statically allocated, since we don't want
4343 -- it going on the stack of the current procedure!
4345 Set_Is_Statically_Allocated
(Temp
);
4349 -- Same if the allocator is an access discriminant for a local object:
4350 -- instead of an allocator we create a local value and constrain the
4351 -- enclosing object with the corresponding access attribute.
4353 if Is_Static_Coextension
(N
) then
4354 Rewrite_Coextension
(N
);
4358 -- Check for size too large, we do this because the back end misses
4359 -- proper checks here and can generate rubbish allocation calls when
4360 -- we are near the limit. We only do this for the 32-bit address case
4361 -- since that is from a practical point of view where we see a problem.
4363 if System_Address_Size
= 32
4364 and then not Storage_Checks_Suppressed
(PtrT
)
4365 and then not Storage_Checks_Suppressed
(Dtyp
)
4366 and then not Storage_Checks_Suppressed
(Etyp
)
4368 -- The check we want to generate should look like
4370 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4371 -- raise Storage_Error;
4374 -- where 3.5 gigabytes is a constant large enough to accommodate any
4375 -- reasonable request for. But we can't do it this way because at
4376 -- least at the moment we don't compute this attribute right, and
4377 -- can silently give wrong results when the result gets large. Since
4378 -- this is all about large results, that's bad, so instead we only
4379 -- apply the check for constrained arrays, and manually compute the
4380 -- value of the attribute ???
4382 if Is_Array_Type
(Etyp
) and then Is_Constrained
(Etyp
) then
4384 Make_Raise_Storage_Error
(Loc
,
4387 Left_Opnd
=> Size_In_Storage_Elements
(Etyp
),
4389 Make_Integer_Literal
(Loc
, Uint_7
* (Uint_2
** 29))),
4390 Reason
=> SE_Object_Too_Large
));
4394 -- Handle case of qualified expression (other than optimization above)
4395 -- First apply constraint checks, because the bounds or discriminants
4396 -- in the aggregate might not match the subtype mark in the allocator.
4398 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
4399 Apply_Constraint_Check
4400 (Expression
(Expression
(N
)), Etype
(Expression
(N
)));
4402 Expand_Allocator_Expression
(N
);
4406 -- If the allocator is for a type which requires initialization, and
4407 -- there is no initial value (i.e. operand is a subtype indication
4408 -- rather than a qualified expression), then we must generate a call to
4409 -- the initialization routine using an expressions action node:
4411 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4413 -- Here ptr_T is the pointer type for the allocator, and T is the
4414 -- subtype of the allocator. A special case arises if the designated
4415 -- type of the access type is a task or contains tasks. In this case
4416 -- the call to Init (Temp.all ...) is replaced by code that ensures
4417 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4418 -- for details). In addition, if the type T is a task T, then the
4419 -- first argument to Init must be converted to the task record type.
4422 T
: constant Entity_Id
:= Entity
(Expression
(N
));
4428 Init_Arg1
: Node_Id
;
4429 Temp_Decl
: Node_Id
;
4430 Temp_Type
: Entity_Id
;
4433 if No_Initialization
(N
) then
4435 -- Even though this might be a simple allocation, create a custom
4436 -- Allocate if the context requires it. Since .NET/JVM compilers
4437 -- do not support pools, this step is skipped.
4439 if VM_Target
= No_VM
4440 and then Present
(Finalization_Master
(PtrT
))
4442 Build_Allocate_Deallocate_Proc
4444 Is_Allocate
=> True);
4447 -- Case of no initialization procedure present
4449 elsif not Has_Non_Null_Base_Init_Proc
(T
) then
4451 -- Case of simple initialization required
4453 if Needs_Simple_Initialization
(T
) then
4454 Check_Restriction
(No_Default_Initialization
, N
);
4455 Rewrite
(Expression
(N
),
4456 Make_Qualified_Expression
(Loc
,
4457 Subtype_Mark
=> New_Occurrence_Of
(T
, Loc
),
4458 Expression
=> Get_Simple_Init_Val
(T
, N
)));
4460 Analyze_And_Resolve
(Expression
(Expression
(N
)), T
);
4461 Analyze_And_Resolve
(Expression
(N
), T
);
4462 Set_Paren_Count
(Expression
(Expression
(N
)), 1);
4463 Expand_N_Allocator
(N
);
4465 -- No initialization required
4471 -- Case of initialization procedure present, must be called
4474 Check_Restriction
(No_Default_Initialization
, N
);
4476 if not Restriction_Active
(No_Default_Initialization
) then
4477 Init
:= Base_Init_Proc
(T
);
4479 Temp
:= Make_Temporary
(Loc
, 'P');
4481 -- Construct argument list for the initialization routine call
4484 Make_Explicit_Dereference
(Loc
,
4486 New_Reference_To
(Temp
, Loc
));
4488 Set_Assignment_OK
(Init_Arg1
);
4491 -- The initialization procedure expects a specific type. if the
4492 -- context is access to class wide, indicate that the object
4493 -- being allocated has the right specific type.
4495 if Is_Class_Wide_Type
(Dtyp
) then
4496 Init_Arg1
:= Unchecked_Convert_To
(T
, Init_Arg1
);
4499 -- If designated type is a concurrent type or if it is private
4500 -- type whose definition is a concurrent type, the first
4501 -- argument in the Init routine has to be unchecked conversion
4502 -- to the corresponding record type. If the designated type is
4503 -- a derived type, also convert the argument to its root type.
4505 if Is_Concurrent_Type
(T
) then
4507 Unchecked_Convert_To
(
4508 Corresponding_Record_Type
(T
), Init_Arg1
);
4510 elsif Is_Private_Type
(T
)
4511 and then Present
(Full_View
(T
))
4512 and then Is_Concurrent_Type
(Full_View
(T
))
4515 Unchecked_Convert_To
4516 (Corresponding_Record_Type
(Full_View
(T
)), Init_Arg1
);
4518 elsif Etype
(First_Formal
(Init
)) /= Base_Type
(T
) then
4520 Ftyp
: constant Entity_Id
:= Etype
(First_Formal
(Init
));
4523 Init_Arg1
:= OK_Convert_To
(Etype
(Ftyp
), Init_Arg1
);
4524 Set_Etype
(Init_Arg1
, Ftyp
);
4528 Args
:= New_List
(Init_Arg1
);
4530 -- For the task case, pass the Master_Id of the access type as
4531 -- the value of the _Master parameter, and _Chain as the value
4532 -- of the _Chain parameter (_Chain will be defined as part of
4533 -- the generated code for the allocator).
4535 -- In Ada 2005, the context may be a function that returns an
4536 -- anonymous access type. In that case the Master_Id has been
4537 -- created when expanding the function declaration.
4539 if Has_Task
(T
) then
4540 if No
(Master_Id
(Base_Type
(PtrT
))) then
4542 -- The designated type was an incomplete type, and the
4543 -- access type did not get expanded. Salvage it now.
4545 if not Restriction_Active
(No_Task_Hierarchy
) then
4546 pragma Assert
(Present
(Parent
(Base_Type
(PtrT
))));
4547 Expand_N_Full_Type_Declaration
4548 (Parent
(Base_Type
(PtrT
)));
4552 -- If the context of the allocator is a declaration or an
4553 -- assignment, we can generate a meaningful image for it,
4554 -- even though subsequent assignments might remove the
4555 -- connection between task and entity. We build this image
4556 -- when the left-hand side is a simple variable, a simple
4557 -- indexed assignment or a simple selected component.
4559 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
4561 Nam
: constant Node_Id
:= Name
(Parent
(N
));
4564 if Is_Entity_Name
(Nam
) then
4566 Build_Task_Image_Decls
4569 (Entity
(Nam
), Sloc
(Nam
)), T
);
4571 elsif Nkind_In
(Nam
, N_Indexed_Component
,
4572 N_Selected_Component
)
4573 and then Is_Entity_Name
(Prefix
(Nam
))
4576 Build_Task_Image_Decls
4577 (Loc
, Nam
, Etype
(Prefix
(Nam
)));
4579 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
4583 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
4585 Build_Task_Image_Decls
4586 (Loc
, Defining_Identifier
(Parent
(N
)), T
);
4589 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
4592 if Restriction_Active
(No_Task_Hierarchy
) then
4594 New_Occurrence_Of
(RTE
(RE_Library_Task_Level
), Loc
));
4598 (Master_Id
(Base_Type
(Root_Type
(PtrT
))), Loc
));
4601 Append_To
(Args
, Make_Identifier
(Loc
, Name_uChain
));
4603 Decl
:= Last
(Decls
);
4605 New_Occurrence_Of
(Defining_Identifier
(Decl
), Loc
));
4607 -- Has_Task is false, Decls not used
4613 -- Add discriminants if discriminated type
4616 Dis
: Boolean := False;
4620 if Has_Discriminants
(T
) then
4624 elsif Is_Private_Type
(T
)
4625 and then Present
(Full_View
(T
))
4626 and then Has_Discriminants
(Full_View
(T
))
4629 Typ
:= Full_View
(T
);
4634 -- If the allocated object will be constrained by the
4635 -- default values for discriminants, then build a subtype
4636 -- with those defaults, and change the allocated subtype
4637 -- to that. Note that this happens in fewer cases in Ada
4640 if not Is_Constrained
(Typ
)
4641 and then Present
(Discriminant_Default_Value
4642 (First_Discriminant
(Typ
)))
4643 and then (Ada_Version
< Ada_2005
4645 Effectively_Has_Constrained_Partial_View
4647 Scop
=> Current_Scope
))
4649 Typ
:= Build_Default_Subtype
(Typ
, N
);
4650 Set_Expression
(N
, New_Reference_To
(Typ
, Loc
));
4653 Discr
:= First_Elmt
(Discriminant_Constraint
(Typ
));
4654 while Present
(Discr
) loop
4655 Nod
:= Node
(Discr
);
4656 Append
(New_Copy_Tree
(Node
(Discr
)), Args
);
4658 -- AI-416: when the discriminant constraint is an
4659 -- anonymous access type make sure an accessibility
4660 -- check is inserted if necessary (3.10.2(22.q/2))
4662 if Ada_Version
>= Ada_2005
4664 Ekind
(Etype
(Nod
)) = E_Anonymous_Access_Type
4666 Apply_Accessibility_Check
4667 (Nod
, Typ
, Insert_Node
=> Nod
);
4675 -- We set the allocator as analyzed so that when we analyze
4676 -- the if expression node, we do not get an unwanted recursive
4677 -- expansion of the allocator expression.
4679 Set_Analyzed
(N
, True);
4680 Nod
:= Relocate_Node
(N
);
4682 -- Here is the transformation:
4683 -- input: new Ctrl_Typ
4684 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4685 -- Ctrl_TypIP (Temp.all, ...);
4686 -- [Deep_]Initialize (Temp.all);
4688 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4689 -- is the subtype of the allocator.
4692 Make_Object_Declaration
(Loc
,
4693 Defining_Identifier
=> Temp
,
4694 Constant_Present
=> True,
4695 Object_Definition
=> New_Reference_To
(Temp_Type
, Loc
),
4698 Set_Assignment_OK
(Temp_Decl
);
4699 Insert_Action
(N
, Temp_Decl
, Suppress
=> All_Checks
);
4701 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
4703 -- If the designated type is a task type or contains tasks,
4704 -- create block to activate created tasks, and insert
4705 -- declaration for Task_Image variable ahead of call.
4707 if Has_Task
(T
) then
4709 L
: constant List_Id
:= New_List
;
4712 Build_Task_Allocate_Block
(L
, Nod
, Args
);
4714 Insert_List_Before
(First
(Declarations
(Blk
)), Decls
);
4715 Insert_Actions
(N
, L
);
4720 Make_Procedure_Call_Statement
(Loc
,
4721 Name
=> New_Reference_To
(Init
, Loc
),
4722 Parameter_Associations
=> Args
));
4725 if Needs_Finalization
(T
) then
4728 -- [Deep_]Initialize (Init_Arg1);
4732 (Obj_Ref
=> New_Copy_Tree
(Init_Arg1
),
4735 if Present
(Finalization_Master
(PtrT
)) then
4737 -- Special processing for .NET/JVM, the allocated object
4738 -- is attached to the finalization master. Generate:
4740 -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1));
4742 -- Types derived from [Limited_]Controlled are the only
4743 -- ones considered since they have fields Prev and Next.
4745 if VM_Target
/= No_VM
then
4746 if Is_Controlled
(T
) then
4749 (Obj_Ref
=> New_Copy_Tree
(Init_Arg1
),
4753 -- Default case, generate:
4755 -- Set_Finalize_Address
4756 -- (<PtrT>FM, <T>FD'Unrestricted_Access);
4758 -- Do not generate this call in the following cases:
4760 -- * Alfa mode - the call is useless and results in
4761 -- unwanted expansion.
4763 -- * CodePeer mode - TSS primitive Finalize_Address is
4764 -- not created in this mode.
4767 and then not CodePeer_Mode
4770 Make_Set_Finalize_Address_Call
4778 Rewrite
(N
, New_Reference_To
(Temp
, Loc
));
4779 Analyze_And_Resolve
(N
, PtrT
);
4784 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
4785 -- object that has been rewritten as a reference, we displace "this"
4786 -- to reference properly its secondary dispatch table.
4788 if Nkind
(N
) = N_Identifier
4789 and then Is_Interface
(Dtyp
)
4791 Displace_Allocator_Pointer
(N
);
4795 when RE_Not_Available
=>
4797 end Expand_N_Allocator
;
4799 -----------------------
4800 -- Expand_N_And_Then --
4801 -----------------------
4803 procedure Expand_N_And_Then
(N
: Node_Id
)
4804 renames Expand_Short_Circuit_Operator
;
4806 ------------------------------
4807 -- Expand_N_Case_Expression --
4808 ------------------------------
4810 procedure Expand_N_Case_Expression
(N
: Node_Id
) is
4811 Loc
: constant Source_Ptr
:= Sloc
(N
);
4812 Typ
: constant Entity_Id
:= Etype
(N
);
4822 -- Check for MINIMIZED/ELIMINATED overflow mode
4824 if Minimized_Eliminated_Overflow_Check
(N
) then
4825 Apply_Arithmetic_Overflow_Check
(N
);
4831 -- case X is when A => AX, when B => BX ...
4846 -- However, this expansion is wrong for limited types, and also
4847 -- wrong for unconstrained types (since the bounds may not be the
4848 -- same in all branches). Furthermore it involves an extra copy
4849 -- for large objects. So we take care of this by using the following
4850 -- modified expansion for non-elementary types:
4853 -- type Pnn is access all typ;
4857 -- T := AX'Unrestricted_Access;
4859 -- T := BX'Unrestricted_Access;
4865 Make_Case_Statement
(Loc
,
4866 Expression
=> Expression
(N
),
4867 Alternatives
=> New_List
);
4869 Actions
:= New_List
;
4873 if Is_Elementary_Type
(Typ
) then
4877 Pnn
:= Make_Temporary
(Loc
, 'P');
4879 Make_Full_Type_Declaration
(Loc
,
4880 Defining_Identifier
=> Pnn
,
4882 Make_Access_To_Object_Definition
(Loc
,
4883 All_Present
=> True,
4884 Subtype_Indication
=>
4885 New_Reference_To
(Typ
, Loc
))));
4889 Tnn
:= Make_Temporary
(Loc
, 'T');
4891 Make_Object_Declaration
(Loc
,
4892 Defining_Identifier
=> Tnn
,
4893 Object_Definition
=> New_Occurrence_Of
(Ttyp
, Loc
)));
4895 -- Now process the alternatives
4897 Alt
:= First
(Alternatives
(N
));
4898 while Present
(Alt
) loop
4900 Aexp
: Node_Id
:= Expression
(Alt
);
4901 Aloc
: constant Source_Ptr
:= Sloc
(Aexp
);
4905 -- As described above, take Unrestricted_Access for case of non-
4906 -- scalar types, to avoid big copies, and special cases.
4908 if not Is_Elementary_Type
(Typ
) then
4910 Make_Attribute_Reference
(Aloc
,
4911 Prefix
=> Relocate_Node
(Aexp
),
4912 Attribute_Name
=> Name_Unrestricted_Access
);
4916 Make_Assignment_Statement
(Aloc
,
4917 Name
=> New_Occurrence_Of
(Tnn
, Loc
),
4918 Expression
=> Aexp
));
4920 -- Propagate declarations inserted in the node by Insert_Actions
4921 -- (for example, temporaries generated to remove side effects).
4922 -- These actions must remain attached to the alternative, given
4923 -- that they are generated by the corresponding expression.
4925 if Present
(Sinfo
.Actions
(Alt
)) then
4926 Prepend_List
(Sinfo
.Actions
(Alt
), Stats
);
4930 (Alternatives
(Cstmt
),
4931 Make_Case_Statement_Alternative
(Sloc
(Alt
),
4932 Discrete_Choices
=> Discrete_Choices
(Alt
),
4933 Statements
=> Stats
));
4939 Append_To
(Actions
, Cstmt
);
4941 -- Construct and return final expression with actions
4943 if Is_Elementary_Type
(Typ
) then
4944 Fexp
:= New_Occurrence_Of
(Tnn
, Loc
);
4947 Make_Explicit_Dereference
(Loc
,
4948 Prefix
=> New_Occurrence_Of
(Tnn
, Loc
));
4952 Make_Expression_With_Actions
(Loc
,
4954 Actions
=> Actions
));
4956 Analyze_And_Resolve
(N
, Typ
);
4957 end Expand_N_Case_Expression
;
4959 -----------------------------------
4960 -- Expand_N_Explicit_Dereference --
4961 -----------------------------------
4963 procedure Expand_N_Explicit_Dereference
(N
: Node_Id
) is
4965 -- Insert explicit dereference call for the checked storage pool case
4967 Insert_Dereference_Action
(Prefix
(N
));
4969 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
4970 -- we set the atomic sync flag.
4972 if Is_Atomic
(Etype
(N
))
4973 and then not Atomic_Synchronization_Disabled
(Etype
(N
))
4975 Activate_Atomic_Synchronization
(N
);
4977 end Expand_N_Explicit_Dereference
;
4979 --------------------------------------
4980 -- Expand_N_Expression_With_Actions --
4981 --------------------------------------
4983 procedure Expand_N_Expression_With_Actions
(N
: Node_Id
) is
4985 procedure Process_Transient_Object
(Decl
: Node_Id
);
4986 -- Given the declaration of a controlled transient declared inside the
4987 -- Actions list of an Expression_With_Actions, generate all necessary
4988 -- types and hooks in order to properly finalize the transient. This
4989 -- mechanism works in conjunction with Build_Finalizer.
4991 ------------------------------
4992 -- Process_Transient_Object --
4993 ------------------------------
4995 procedure Process_Transient_Object
(Decl
: Node_Id
) is
4997 function Find_Insertion_Node
return Node_Id
;
4998 -- Complex conditions in if statements may be converted into nested
4999 -- EWAs. In this case, any generated code must be inserted before the
5000 -- if statement to ensure proper visibility of the hook objects. This
5001 -- routine returns the top most short circuit operator or the parent
5002 -- of the EWA if no nesting was detected.
5004 -------------------------
5005 -- Find_Insertion_Node --
5006 -------------------------
5008 function Find_Insertion_Node
return Node_Id
is
5012 -- Climb up the branches of a complex condition
5015 while Nkind_In
(Parent
(Par
), N_And_Then
, N_Op_Not
, N_Or_Else
) loop
5016 Par
:= Parent
(Par
);
5020 end Find_Insertion_Node
;
5024 Ins_Node
: constant Node_Id
:= Find_Insertion_Node
;
5025 Loc
: constant Source_Ptr
:= Sloc
(Decl
);
5026 Obj_Id
: constant Entity_Id
:= Defining_Identifier
(Decl
);
5027 Obj_Typ
: constant Entity_Id
:= Etype
(Obj_Id
);
5028 Desig_Typ
: Entity_Id
;
5032 Temp_Decl
: Node_Id
;
5035 -- Start of processing for Process_Transient_Object
5038 -- Step 1: Create the access type which provides a reference to the
5039 -- transient object.
5041 if Is_Access_Type
(Obj_Typ
) then
5042 Desig_Typ
:= Directly_Designated_Type
(Obj_Typ
);
5044 Desig_Typ
:= Obj_Typ
;
5048 -- Ann : access [all] <Desig_Typ>;
5050 Ptr_Id
:= Make_Temporary
(Loc
, 'A');
5053 Make_Full_Type_Declaration
(Loc
,
5054 Defining_Identifier
=> Ptr_Id
,
5056 Make_Access_To_Object_Definition
(Loc
,
5058 Ekind
(Obj_Typ
) = E_General_Access_Type
,
5059 Subtype_Indication
=> New_Reference_To
(Desig_Typ
, Loc
)));
5061 Insert_Action
(Ins_Node
, Ptr_Decl
);
5064 -- Step 2: Create a temporary which acts as a hook to the transient
5065 -- object. Generate:
5067 -- Temp : Ptr_Id := null;
5069 Temp_Id
:= Make_Temporary
(Loc
, 'T');
5072 Make_Object_Declaration
(Loc
,
5073 Defining_Identifier
=> Temp_Id
,
5074 Object_Definition
=> New_Reference_To
(Ptr_Id
, Loc
));
5076 Insert_Action
(Ins_Node
, Temp_Decl
);
5077 Analyze
(Temp_Decl
);
5079 -- Mark this temporary as created for the purposes of exporting the
5080 -- transient declaration out of the Actions list. This signals the
5081 -- machinery in Build_Finalizer to recognize this special case.
5083 Set_Status_Flag_Or_Transient_Decl
(Temp_Id
, Decl
);
5085 -- Step 3: Hook the transient object to the temporary
5087 if Is_Access_Type
(Obj_Typ
) then
5088 Expr
:= Convert_To
(Ptr_Id
, New_Reference_To
(Obj_Id
, Loc
));
5091 Make_Attribute_Reference
(Loc
,
5092 Prefix
=> New_Reference_To
(Obj_Id
, Loc
),
5093 Attribute_Name
=> Name_Unrestricted_Access
);
5097 -- Temp := Ptr_Id (Obj_Id);
5099 -- Temp := Obj_Id'Unrestricted_Access;
5101 Insert_After_And_Analyze
(Decl
,
5102 Make_Assignment_Statement
(Loc
,
5103 Name
=> New_Reference_To
(Temp_Id
, Loc
),
5104 Expression
=> Expr
));
5105 end Process_Transient_Object
;
5111 -- Start of processing for Expand_N_Expression_With_Actions
5114 Decl
:= First
(Actions
(N
));
5115 while Present
(Decl
) loop
5116 if Nkind
(Decl
) = N_Object_Declaration
5117 and then Is_Finalizable_Transient
(Decl
, N
)
5119 Process_Transient_Object
(Decl
);
5124 end Expand_N_Expression_With_Actions
;
5126 ----------------------------
5127 -- Expand_N_If_Expression --
5128 ----------------------------
5130 -- Deal with limited types and condition actions
5132 procedure Expand_N_If_Expression
(N
: Node_Id
) is
5133 function Create_Alternative
5135 Temp_Id
: Entity_Id
;
5136 Flag_Id
: Entity_Id
;
5137 Expr
: Node_Id
) return List_Id
;
5138 -- Build the statements of a "then" or "else" dependent expression
5139 -- alternative. Temp_Id is the if expression result, Flag_Id is a
5140 -- finalization flag created to service expression Expr.
5142 function Is_Controlled_Function_Call
(Expr
: Node_Id
) return Boolean;
5143 -- Determine if expression Expr is a rewritten controlled function call
5145 ------------------------
5146 -- Create_Alternative --
5147 ------------------------
5149 function Create_Alternative
5151 Temp_Id
: Entity_Id
;
5152 Flag_Id
: Entity_Id
;
5153 Expr
: Node_Id
) return List_Id
5155 Result
: constant List_Id
:= New_List
;
5161 if Present
(Flag_Id
)
5162 and then not Is_Controlled_Function_Call
(Expr
)
5165 Make_Assignment_Statement
(Loc
,
5166 Name
=> New_Reference_To
(Flag_Id
, Loc
),
5167 Expression
=> New_Reference_To
(Standard_True
, Loc
)));
5171 -- Cnn := <expr>'Unrestricted_Access;
5174 Make_Assignment_Statement
(Loc
,
5175 Name
=> New_Reference_To
(Temp_Id
, Loc
),
5177 Make_Attribute_Reference
(Loc
,
5178 Prefix
=> Relocate_Node
(Expr
),
5179 Attribute_Name
=> Name_Unrestricted_Access
)));
5182 end Create_Alternative
;
5184 ---------------------------------
5185 -- Is_Controlled_Function_Call --
5186 ---------------------------------
5188 function Is_Controlled_Function_Call
(Expr
: Node_Id
) return Boolean is
5191 Nkind
(Original_Node
(Expr
)) = N_Function_Call
5192 and then Needs_Finalization
(Etype
(Expr
));
5193 end Is_Controlled_Function_Call
;
5197 Loc
: constant Source_Ptr
:= Sloc
(N
);
5198 Cond
: constant Node_Id
:= First
(Expressions
(N
));
5199 Thenx
: constant Node_Id
:= Next
(Cond
);
5200 Elsex
: constant Node_Id
:= Next
(Thenx
);
5201 Typ
: constant Entity_Id
:= Etype
(N
);
5211 -- Check for MINIMIZED/ELIMINATED overflow mode
5213 if Minimized_Eliminated_Overflow_Check
(N
) then
5214 Apply_Arithmetic_Overflow_Check
(N
);
5218 -- Fold at compile time if condition known. We have already folded
5219 -- static if expressions, but it is possible to fold any case in which
5220 -- the condition is known at compile time, even though the result is
5223 -- Note that we don't do the fold of such cases in Sem_Elab because
5224 -- it can cause infinite loops with the expander adding a conditional
5225 -- expression, and Sem_Elab circuitry removing it repeatedly.
5227 if Compile_Time_Known_Value
(Cond
) then
5228 if Is_True
(Expr_Value
(Cond
)) then
5230 Actions
:= Then_Actions
(N
);
5233 Actions
:= Else_Actions
(N
);
5238 if Present
(Actions
) then
5240 -- If we are not allowed to use Expression_With_Actions, just skip
5241 -- the optimization, it is not critical for correctness.
5243 if not Use_Expression_With_Actions
then
5244 goto Skip_Optimization
;
5248 Make_Expression_With_Actions
(Loc
,
5249 Expression
=> Relocate_Node
(Expr
),
5250 Actions
=> Actions
));
5251 Analyze_And_Resolve
(N
, Typ
);
5254 Rewrite
(N
, Relocate_Node
(Expr
));
5257 -- Note that the result is never static (legitimate cases of static
5258 -- if expressions were folded in Sem_Eval).
5260 Set_Is_Static_Expression
(N
, False);
5264 <<Skip_Optimization
>>
5266 -- If the type is limited or unconstrained, we expand as follows to
5267 -- avoid any possibility of improper copies.
5269 -- Note: it may be possible to avoid this special processing if the
5270 -- back end uses its own mechanisms for handling by-reference types ???
5272 -- type Ptr is access all Typ;
5276 -- Cnn := then-expr'Unrestricted_Access;
5279 -- Cnn := else-expr'Unrestricted_Access;
5282 -- and replace the if expression by a reference to Cnn.all.
5284 -- This special case can be skipped if the back end handles limited
5285 -- types properly and ensures that no incorrect copies are made.
5287 if Is_By_Reference_Type
(Typ
)
5288 and then not Back_End_Handles_Limited_Types
5291 Flag_Id
: Entity_Id
;
5292 Ptr_Typ
: Entity_Id
;
5297 -- At least one of the if expression dependent expressions uses a
5298 -- controlled function to provide the result. Create a status flag
5299 -- to signal the finalization machinery that Cnn needs special
5302 if Is_Controlled_Function_Call
(Thenx
)
5304 Is_Controlled_Function_Call
(Elsex
)
5306 Flag_Id
:= Make_Temporary
(Loc
, 'F');
5309 Make_Object_Declaration
(Loc
,
5310 Defining_Identifier
=> Flag_Id
,
5311 Object_Definition
=>
5312 New_Reference_To
(Standard_Boolean
, Loc
),
5314 New_Reference_To
(Standard_False
, Loc
)));
5318 -- type Ann is access all Typ;
5320 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
5323 Make_Full_Type_Declaration
(Loc
,
5324 Defining_Identifier
=> Ptr_Typ
,
5326 Make_Access_To_Object_Definition
(Loc
,
5327 All_Present
=> True,
5328 Subtype_Indication
=> New_Reference_To
(Typ
, Loc
))));
5333 Cnn
:= Make_Temporary
(Loc
, 'C', N
);
5334 Set_Ekind
(Cnn
, E_Variable
);
5335 Set_Status_Flag_Or_Transient_Decl
(Cnn
, Flag_Id
);
5338 Make_Object_Declaration
(Loc
,
5339 Defining_Identifier
=> Cnn
,
5340 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
));
5343 Make_Implicit_If_Statement
(N
,
5344 Condition
=> Relocate_Node
(Cond
),
5346 Create_Alternative
(Sloc
(Thenx
), Cnn
, Flag_Id
, Thenx
),
5348 Create_Alternative
(Sloc
(Elsex
), Cnn
, Flag_Id
, Elsex
));
5351 Make_Explicit_Dereference
(Loc
,
5352 Prefix
=> New_Occurrence_Of
(Cnn
, Loc
));
5355 -- For other types, we only need to expand if there are other actions
5356 -- associated with either branch.
5358 elsif Present
(Then_Actions
(N
)) or else Present
(Else_Actions
(N
)) then
5360 -- We have two approaches to handling this. If we are allowed to use
5361 -- N_Expression_With_Actions, then we can just wrap the actions into
5362 -- the appropriate expression.
5364 if Use_Expression_With_Actions
then
5365 if Present
(Then_Actions
(N
)) then
5367 Make_Expression_With_Actions
(Sloc
(Thenx
),
5368 Actions
=> Then_Actions
(N
),
5369 Expression
=> Relocate_Node
(Thenx
)));
5370 Set_Then_Actions
(N
, No_List
);
5371 Analyze_And_Resolve
(Thenx
, Typ
);
5374 if Present
(Else_Actions
(N
)) then
5376 Make_Expression_With_Actions
(Sloc
(Elsex
),
5377 Actions
=> Else_Actions
(N
),
5378 Expression
=> Relocate_Node
(Elsex
)));
5379 Set_Else_Actions
(N
, No_List
);
5380 Analyze_And_Resolve
(Elsex
, Typ
);
5385 -- if we can't use N_Expression_With_Actions nodes, then we insert
5386 -- the following sequence of actions (using Insert_Actions):
5391 -- Cnn := then-expr;
5397 -- and replace the if expression by a reference to Cnn
5400 Cnn
:= Make_Temporary
(Loc
, 'C', N
);
5403 Make_Object_Declaration
(Loc
,
5404 Defining_Identifier
=> Cnn
,
5405 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
5408 Make_Implicit_If_Statement
(N
,
5409 Condition
=> Relocate_Node
(Cond
),
5411 Then_Statements
=> New_List
(
5412 Make_Assignment_Statement
(Sloc
(Thenx
),
5413 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Thenx
)),
5414 Expression
=> Relocate_Node
(Thenx
))),
5416 Else_Statements
=> New_List
(
5417 Make_Assignment_Statement
(Sloc
(Elsex
),
5418 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Elsex
)),
5419 Expression
=> Relocate_Node
(Elsex
))));
5421 Set_Assignment_OK
(Name
(First
(Then_Statements
(New_If
))));
5422 Set_Assignment_OK
(Name
(First
(Else_Statements
(New_If
))));
5424 New_N
:= New_Occurrence_Of
(Cnn
, Loc
);
5427 -- If no actions then no expansion needed, gigi will handle it using
5428 -- the same approach as a C conditional expression.
5434 -- Fall through here for either the limited expansion, or the case of
5435 -- inserting actions for non-limited types. In both these cases, we must
5436 -- move the SLOC of the parent If statement to the newly created one and
5437 -- change it to the SLOC of the expression which, after expansion, will
5438 -- correspond to what is being evaluated.
5440 if Present
(Parent
(N
))
5441 and then Nkind
(Parent
(N
)) = N_If_Statement
5443 Set_Sloc
(New_If
, Sloc
(Parent
(N
)));
5444 Set_Sloc
(Parent
(N
), Loc
);
5447 -- Make sure Then_Actions and Else_Actions are appropriately moved
5448 -- to the new if statement.
5450 if Present
(Then_Actions
(N
)) then
5452 (First
(Then_Statements
(New_If
)), Then_Actions
(N
));
5455 if Present
(Else_Actions
(N
)) then
5457 (First
(Else_Statements
(New_If
)), Else_Actions
(N
));
5460 Insert_Action
(N
, Decl
);
5461 Insert_Action
(N
, New_If
);
5463 Analyze_And_Resolve
(N
, Typ
);
5464 end Expand_N_If_Expression
;
5470 procedure Expand_N_In
(N
: Node_Id
) is
5471 Loc
: constant Source_Ptr
:= Sloc
(N
);
5472 Restyp
: constant Entity_Id
:= Etype
(N
);
5473 Lop
: constant Node_Id
:= Left_Opnd
(N
);
5474 Rop
: constant Node_Id
:= Right_Opnd
(N
);
5475 Static
: constant Boolean := Is_OK_Static_Expression
(N
);
5480 procedure Substitute_Valid_Check
;
5481 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5482 -- test for the left operand being in range of its subtype.
5484 ----------------------------
5485 -- Substitute_Valid_Check --
5486 ----------------------------
5488 procedure Substitute_Valid_Check
is
5491 Make_Attribute_Reference
(Loc
,
5492 Prefix
=> Relocate_Node
(Lop
),
5493 Attribute_Name
=> Name_Valid
));
5495 Analyze_And_Resolve
(N
, Restyp
);
5497 -- Give warning unless overflow checking is MINIMIZED or ELIMINATED,
5498 -- in which case, this usage makes sense, and in any case, we have
5499 -- actually eliminated the danger of optimization above.
5501 if Overflow_Check_Mode
not in Minimized_Or_Eliminated
then
5502 Error_Msg_N
("?explicit membership test may be optimized away", N
);
5503 Error_Msg_N
-- CODEFIX
5504 ("\?use ''Valid attribute instead", N
);
5508 end Substitute_Valid_Check
;
5510 -- Start of processing for Expand_N_In
5513 -- If set membership case, expand with separate procedure
5515 if Present
(Alternatives
(N
)) then
5516 Expand_Set_Membership
(N
);
5520 -- Not set membership, proceed with expansion
5522 Ltyp
:= Etype
(Left_Opnd
(N
));
5523 Rtyp
:= Etype
(Right_Opnd
(N
));
5525 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
5526 -- type, then expand with a separate procedure. Note the use of the
5527 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5529 if Overflow_Check_Mode
in Minimized_Or_Eliminated
5530 and then Is_Signed_Integer_Type
(Ltyp
)
5531 and then not No_Minimize_Eliminate
(N
)
5533 Expand_Membership_Minimize_Eliminate_Overflow
(N
);
5537 -- Check case of explicit test for an expression in range of its
5538 -- subtype. This is suspicious usage and we replace it with a 'Valid
5539 -- test and give a warning for scalar types.
5541 if Is_Scalar_Type
(Ltyp
)
5543 -- Only relevant for source comparisons
5545 and then Comes_From_Source
(N
)
5547 -- In floating-point this is a standard way to check for finite values
5548 -- and using 'Valid would typically be a pessimization.
5550 and then not Is_Floating_Point_Type
(Ltyp
)
5552 -- Don't give the message unless right operand is a type entity and
5553 -- the type of the left operand matches this type. Note that this
5554 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
5555 -- checks have changed the type of the left operand.
5557 and then Nkind
(Rop
) in N_Has_Entity
5558 and then Ltyp
= Entity
(Rop
)
5560 -- Skip in VM mode, where we have no sense of invalid values. The
5561 -- warning still seems relevant, but not important enough to worry.
5563 and then VM_Target
= No_VM
5565 -- Skip this for predicated types, where such expressions are a
5566 -- reasonable way of testing if something meets the predicate.
5568 and then not Present
(Predicate_Function
(Ltyp
))
5570 Substitute_Valid_Check
;
5574 -- Do validity check on operands
5576 if Validity_Checks_On
and Validity_Check_Operands
then
5577 Ensure_Valid
(Left_Opnd
(N
));
5578 Validity_Check_Range
(Right_Opnd
(N
));
5581 -- Case of explicit range
5583 if Nkind
(Rop
) = N_Range
then
5585 Lo
: constant Node_Id
:= Low_Bound
(Rop
);
5586 Hi
: constant Node_Id
:= High_Bound
(Rop
);
5588 Lo_Orig
: constant Node_Id
:= Original_Node
(Lo
);
5589 Hi_Orig
: constant Node_Id
:= Original_Node
(Hi
);
5591 Lcheck
: Compare_Result
;
5592 Ucheck
: Compare_Result
;
5594 Warn1
: constant Boolean :=
5595 Constant_Condition_Warnings
5596 and then Comes_From_Source
(N
)
5597 and then not In_Instance
;
5598 -- This must be true for any of the optimization warnings, we
5599 -- clearly want to give them only for source with the flag on. We
5600 -- also skip these warnings in an instance since it may be the
5601 -- case that different instantiations have different ranges.
5603 Warn2
: constant Boolean :=
5605 and then Nkind
(Original_Node
(Rop
)) = N_Range
5606 and then Is_Integer_Type
(Etype
(Lo
));
5607 -- For the case where only one bound warning is elided, we also
5608 -- insist on an explicit range and an integer type. The reason is
5609 -- that the use of enumeration ranges including an end point is
5610 -- common, as is the use of a subtype name, one of whose bounds is
5611 -- the same as the type of the expression.
5614 -- If test is explicit x'First .. x'Last, replace by valid check
5616 -- Could use some individual comments for this complex test ???
5618 if Is_Scalar_Type
(Ltyp
)
5620 -- And left operand is X'First where X matches left operand
5621 -- type (this eliminates cases of type mismatch, including
5622 -- the cases where ELIMINATED/MINIMIZED mode has changed the
5623 -- type of the left operand.
5625 and then Nkind
(Lo_Orig
) = N_Attribute_Reference
5626 and then Attribute_Name
(Lo_Orig
) = Name_First
5627 and then Nkind
(Prefix
(Lo_Orig
)) in N_Has_Entity
5628 and then Entity
(Prefix
(Lo_Orig
)) = Ltyp
5630 -- Same tests for right operand
5632 and then Nkind
(Hi_Orig
) = N_Attribute_Reference
5633 and then Attribute_Name
(Hi_Orig
) = Name_Last
5634 and then Nkind
(Prefix
(Hi_Orig
)) in N_Has_Entity
5635 and then Entity
(Prefix
(Hi_Orig
)) = Ltyp
5637 -- Relevant only for source cases
5639 and then Comes_From_Source
(N
)
5641 -- Omit for VM cases, where we don't have invalid values
5643 and then VM_Target
= No_VM
5645 Substitute_Valid_Check
;
5649 -- If bounds of type are known at compile time, and the end points
5650 -- are known at compile time and identical, this is another case
5651 -- for substituting a valid test. We only do this for discrete
5652 -- types, since it won't arise in practice for float types.
5654 if Comes_From_Source
(N
)
5655 and then Is_Discrete_Type
(Ltyp
)
5656 and then Compile_Time_Known_Value
(Type_High_Bound
(Ltyp
))
5657 and then Compile_Time_Known_Value
(Type_Low_Bound
(Ltyp
))
5658 and then Compile_Time_Known_Value
(Lo
)
5659 and then Compile_Time_Known_Value
(Hi
)
5660 and then Expr_Value
(Type_High_Bound
(Ltyp
)) = Expr_Value
(Hi
)
5661 and then Expr_Value
(Type_Low_Bound
(Ltyp
)) = Expr_Value
(Lo
)
5663 -- Kill warnings in instances, since they may be cases where we
5664 -- have a test in the generic that makes sense with some types
5665 -- and not with other types.
5667 and then not In_Instance
5669 Substitute_Valid_Check
;
5673 -- If we have an explicit range, do a bit of optimization based on
5674 -- range analysis (we may be able to kill one or both checks).
5676 Lcheck
:= Compile_Time_Compare
(Lop
, Lo
, Assume_Valid
=> False);
5677 Ucheck
:= Compile_Time_Compare
(Lop
, Hi
, Assume_Valid
=> False);
5679 -- If either check is known to fail, replace result by False since
5680 -- the other check does not matter. Preserve the static flag for
5681 -- legality checks, because we are constant-folding beyond RM 4.9.
5683 if Lcheck
= LT
or else Ucheck
= GT
then
5685 Error_Msg_N
("?range test optimized away", N
);
5686 Error_Msg_N
("\?value is known to be out of range", N
);
5689 Rewrite
(N
, New_Reference_To
(Standard_False
, Loc
));
5690 Analyze_And_Resolve
(N
, Restyp
);
5691 Set_Is_Static_Expression
(N
, Static
);
5694 -- If both checks are known to succeed, replace result by True,
5695 -- since we know we are in range.
5697 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
5699 Error_Msg_N
("?range test optimized away", N
);
5700 Error_Msg_N
("\?value is known to be in range", N
);
5703 Rewrite
(N
, New_Reference_To
(Standard_True
, Loc
));
5704 Analyze_And_Resolve
(N
, Restyp
);
5705 Set_Is_Static_Expression
(N
, Static
);
5708 -- If lower bound check succeeds and upper bound check is not
5709 -- known to succeed or fail, then replace the range check with
5710 -- a comparison against the upper bound.
5712 elsif Lcheck
in Compare_GE
then
5713 if Warn2
and then not In_Instance
then
5714 Error_Msg_N
("?lower bound test optimized away", Lo
);
5715 Error_Msg_N
("\?value is known to be in range", Lo
);
5721 Right_Opnd
=> High_Bound
(Rop
)));
5722 Analyze_And_Resolve
(N
, Restyp
);
5725 -- If upper bound check succeeds and lower bound check is not
5726 -- known to succeed or fail, then replace the range check with
5727 -- a comparison against the lower bound.
5729 elsif Ucheck
in Compare_LE
then
5730 if Warn2
and then not In_Instance
then
5731 Error_Msg_N
("?upper bound test optimized away", Hi
);
5732 Error_Msg_N
("\?value is known to be in range", Hi
);
5738 Right_Opnd
=> Low_Bound
(Rop
)));
5739 Analyze_And_Resolve
(N
, Restyp
);
5743 -- We couldn't optimize away the range check, but there is one
5744 -- more issue. If we are checking constant conditionals, then we
5745 -- see if we can determine the outcome assuming everything is
5746 -- valid, and if so give an appropriate warning.
5748 if Warn1
and then not Assume_No_Invalid_Values
then
5749 Lcheck
:= Compile_Time_Compare
(Lop
, Lo
, Assume_Valid
=> True);
5750 Ucheck
:= Compile_Time_Compare
(Lop
, Hi
, Assume_Valid
=> True);
5752 -- Result is out of range for valid value
5754 if Lcheck
= LT
or else Ucheck
= GT
then
5756 ("?value can only be in range if it is invalid", N
);
5758 -- Result is in range for valid value
5760 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
5762 ("?value can only be out of range if it is invalid", N
);
5764 -- Lower bound check succeeds if value is valid
5766 elsif Warn2
and then Lcheck
in Compare_GE
then
5768 ("?lower bound check only fails if it is invalid", Lo
);
5770 -- Upper bound check succeeds if value is valid
5772 elsif Warn2
and then Ucheck
in Compare_LE
then
5774 ("?upper bound check only fails for invalid values", Hi
);
5779 -- For all other cases of an explicit range, nothing to be done
5783 -- Here right operand is a subtype mark
5787 Typ
: Entity_Id
:= Etype
(Rop
);
5788 Is_Acc
: constant Boolean := Is_Access_Type
(Typ
);
5789 Cond
: Node_Id
:= Empty
;
5791 Obj
: Node_Id
:= Lop
;
5792 SCIL_Node
: Node_Id
;
5795 Remove_Side_Effects
(Obj
);
5797 -- For tagged type, do tagged membership operation
5799 if Is_Tagged_Type
(Typ
) then
5801 -- No expansion will be performed when VM_Target, as the VM
5802 -- back-ends will handle the membership tests directly (tags
5803 -- are not explicitly represented in Java objects, so the
5804 -- normal tagged membership expansion is not what we want).
5806 if Tagged_Type_Expansion
then
5807 Tagged_Membership
(N
, SCIL_Node
, New_N
);
5809 Analyze_And_Resolve
(N
, Restyp
);
5811 -- Update decoration of relocated node referenced by the
5814 if Generate_SCIL
and then Present
(SCIL_Node
) then
5815 Set_SCIL_Node
(N
, SCIL_Node
);
5821 -- If type is scalar type, rewrite as x in t'First .. t'Last.
5822 -- This reason we do this is that the bounds may have the wrong
5823 -- type if they come from the original type definition. Also this
5824 -- way we get all the processing above for an explicit range.
5826 -- Don't do this for predicated types, since in this case we
5827 -- want to check the predicate!
5829 elsif Is_Scalar_Type
(Typ
) then
5830 if No
(Predicate_Function
(Typ
)) then
5834 Make_Attribute_Reference
(Loc
,
5835 Attribute_Name
=> Name_First
,
5836 Prefix
=> New_Reference_To
(Typ
, Loc
)),
5839 Make_Attribute_Reference
(Loc
,
5840 Attribute_Name
=> Name_Last
,
5841 Prefix
=> New_Reference_To
(Typ
, Loc
))));
5842 Analyze_And_Resolve
(N
, Restyp
);
5847 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5848 -- a membership test if the subtype mark denotes a constrained
5849 -- Unchecked_Union subtype and the expression lacks inferable
5852 elsif Is_Unchecked_Union
(Base_Type
(Typ
))
5853 and then Is_Constrained
(Typ
)
5854 and then not Has_Inferable_Discriminants
(Lop
)
5857 Make_Raise_Program_Error
(Loc
,
5858 Reason
=> PE_Unchecked_Union_Restriction
));
5860 -- Prevent Gigi from generating incorrect code by rewriting the
5861 -- test as False. What is this undocumented thing about ???
5863 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
5867 -- Here we have a non-scalar type
5870 Typ
:= Designated_Type
(Typ
);
5873 if not Is_Constrained
(Typ
) then
5874 Rewrite
(N
, New_Reference_To
(Standard_True
, Loc
));
5875 Analyze_And_Resolve
(N
, Restyp
);
5877 -- For the constrained array case, we have to check the subscripts
5878 -- for an exact match if the lengths are non-zero (the lengths
5879 -- must match in any case).
5881 elsif Is_Array_Type
(Typ
) then
5882 Check_Subscripts
: declare
5883 function Build_Attribute_Reference
5886 Dim
: Nat
) return Node_Id
;
5887 -- Build attribute reference E'Nam (Dim)
5889 -------------------------------
5890 -- Build_Attribute_Reference --
5891 -------------------------------
5893 function Build_Attribute_Reference
5896 Dim
: Nat
) return Node_Id
5900 Make_Attribute_Reference
(Loc
,
5902 Attribute_Name
=> Nam
,
5903 Expressions
=> New_List
(
5904 Make_Integer_Literal
(Loc
, Dim
)));
5905 end Build_Attribute_Reference
;
5907 -- Start of processing for Check_Subscripts
5910 for J
in 1 .. Number_Dimensions
(Typ
) loop
5911 Evolve_And_Then
(Cond
,
5914 Build_Attribute_Reference
5915 (Duplicate_Subexpr_No_Checks
(Obj
),
5918 Build_Attribute_Reference
5919 (New_Occurrence_Of
(Typ
, Loc
), Name_First
, J
)));
5921 Evolve_And_Then
(Cond
,
5924 Build_Attribute_Reference
5925 (Duplicate_Subexpr_No_Checks
(Obj
),
5928 Build_Attribute_Reference
5929 (New_Occurrence_Of
(Typ
, Loc
), Name_Last
, J
)));
5938 Right_Opnd
=> Make_Null
(Loc
)),
5939 Right_Opnd
=> Cond
);
5943 Analyze_And_Resolve
(N
, Restyp
);
5944 end Check_Subscripts
;
5946 -- These are the cases where constraint checks may be required,
5947 -- e.g. records with possible discriminants
5950 -- Expand the test into a series of discriminant comparisons.
5951 -- The expression that is built is the negation of the one that
5952 -- is used for checking discriminant constraints.
5954 Obj
:= Relocate_Node
(Left_Opnd
(N
));
5956 if Has_Discriminants
(Typ
) then
5957 Cond
:= Make_Op_Not
(Loc
,
5958 Right_Opnd
=> Build_Discriminant_Checks
(Obj
, Typ
));
5961 Cond
:= Make_Or_Else
(Loc
,
5965 Right_Opnd
=> Make_Null
(Loc
)),
5966 Right_Opnd
=> Cond
);
5970 Cond
:= New_Occurrence_Of
(Standard_True
, Loc
);
5974 Analyze_And_Resolve
(N
, Restyp
);
5977 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
5978 -- expression of an anonymous access type. This can involve an
5979 -- accessibility test and a tagged type membership test in the
5980 -- case of tagged designated types.
5982 if Ada_Version
>= Ada_2012
5984 and then Ekind
(Ltyp
) = E_Anonymous_Access_Type
5987 Expr_Entity
: Entity_Id
:= Empty
;
5989 Param_Level
: Node_Id
;
5990 Type_Level
: Node_Id
;
5993 if Is_Entity_Name
(Lop
) then
5994 Expr_Entity
:= Param_Entity
(Lop
);
5996 if not Present
(Expr_Entity
) then
5997 Expr_Entity
:= Entity
(Lop
);
6001 -- If a conversion of the anonymous access value to the
6002 -- tested type would be illegal, then the result is False.
6004 if not Valid_Conversion
6005 (Lop
, Rtyp
, Lop
, Report_Errs
=> False)
6007 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
6008 Analyze_And_Resolve
(N
, Restyp
);
6010 -- Apply an accessibility check if the access object has an
6011 -- associated access level and when the level of the type is
6012 -- less deep than the level of the access parameter. This
6013 -- only occur for access parameters and stand-alone objects
6014 -- of an anonymous access type.
6017 if Present
(Expr_Entity
)
6020 (Effective_Extra_Accessibility
(Expr_Entity
))
6021 and then UI_Gt
(Object_Access_Level
(Lop
),
6022 Type_Access_Level
(Rtyp
))
6026 (Effective_Extra_Accessibility
(Expr_Entity
), Loc
);
6029 Make_Integer_Literal
(Loc
, Type_Access_Level
(Rtyp
));
6031 -- Return True only if the accessibility level of the
6032 -- expression entity is not deeper than the level of
6033 -- the tested access type.
6037 Left_Opnd
=> Relocate_Node
(N
),
6038 Right_Opnd
=> Make_Op_Le
(Loc
,
6039 Left_Opnd
=> Param_Level
,
6040 Right_Opnd
=> Type_Level
)));
6042 Analyze_And_Resolve
(N
);
6045 -- If the designated type is tagged, do tagged membership
6048 -- *** NOTE: we have to check not null before doing the
6049 -- tagged membership test (but maybe that can be done
6050 -- inside Tagged_Membership?).
6052 if Is_Tagged_Type
(Typ
) then
6055 Left_Opnd
=> Relocate_Node
(N
),
6059 Right_Opnd
=> Make_Null
(Loc
))));
6061 -- No expansion will be performed when VM_Target, as
6062 -- the VM back-ends will handle the membership tests
6063 -- directly (tags are not explicitly represented in
6064 -- Java objects, so the normal tagged membership
6065 -- expansion is not what we want).
6067 if Tagged_Type_Expansion
then
6069 -- Note that we have to pass Original_Node, because
6070 -- the membership test might already have been
6071 -- rewritten by earlier parts of membership test.
6074 (Original_Node
(N
), SCIL_Node
, New_N
);
6076 -- Update decoration of relocated node referenced
6077 -- by the SCIL node.
6079 if Generate_SCIL
and then Present
(SCIL_Node
) then
6080 Set_SCIL_Node
(New_N
, SCIL_Node
);
6085 Left_Opnd
=> Relocate_Node
(N
),
6086 Right_Opnd
=> New_N
));
6088 Analyze_And_Resolve
(N
, Restyp
);
6097 -- At this point, we have done the processing required for the basic
6098 -- membership test, but not yet dealt with the predicate.
6102 -- If a predicate is present, then we do the predicate test, but we
6103 -- most certainly want to omit this if we are within the predicate
6104 -- function itself, since otherwise we have an infinite recursion!
6105 -- The check should also not be emitted when testing against a range
6106 -- (the check is only done when the right operand is a subtype; see
6107 -- RM12-4.5.2 (28.1/3-30/3)).
6110 PFunc
: constant Entity_Id
:= Predicate_Function
(Rtyp
);
6114 and then Current_Scope
/= PFunc
6115 and then Nkind
(Rop
) /= N_Range
6119 Left_Opnd
=> Relocate_Node
(N
),
6120 Right_Opnd
=> Make_Predicate_Call
(Rtyp
, Lop
)));
6122 -- Analyze new expression, mark left operand as analyzed to
6123 -- avoid infinite recursion adding predicate calls. Similarly,
6124 -- suppress further range checks on the call.
6126 Set_Analyzed
(Left_Opnd
(N
));
6127 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
6129 -- All done, skip attempt at compile time determination of result
6136 --------------------------------
6137 -- Expand_N_Indexed_Component --
6138 --------------------------------
6140 procedure Expand_N_Indexed_Component
(N
: Node_Id
) is
6141 Loc
: constant Source_Ptr
:= Sloc
(N
);
6142 Typ
: constant Entity_Id
:= Etype
(N
);
6143 P
: constant Node_Id
:= Prefix
(N
);
6144 T
: constant Entity_Id
:= Etype
(P
);
6148 -- A special optimization, if we have an indexed component that is
6149 -- selecting from a slice, then we can eliminate the slice, since, for
6150 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6151 -- the range check required by the slice. The range check for the slice
6152 -- itself has already been generated. The range check for the
6153 -- subscripting operation is ensured by converting the subject to
6154 -- the subtype of the slice.
6156 -- This optimization not only generates better code, avoiding slice
6157 -- messing especially in the packed case, but more importantly bypasses
6158 -- some problems in handling this peculiar case, for example, the issue
6159 -- of dealing specially with object renamings.
6161 if Nkind
(P
) = N_Slice
then
6163 Make_Indexed_Component
(Loc
,
6164 Prefix
=> Prefix
(P
),
6165 Expressions
=> New_List
(
6167 (Etype
(First_Index
(Etype
(P
))),
6168 First
(Expressions
(N
))))));
6169 Analyze_And_Resolve
(N
, Typ
);
6173 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6174 -- function, then additional actuals must be passed.
6176 if Ada_Version
>= Ada_2005
6177 and then Is_Build_In_Place_Function_Call
(P
)
6179 Make_Build_In_Place_Call_In_Anonymous_Context
(P
);
6182 -- If the prefix is an access type, then we unconditionally rewrite if
6183 -- as an explicit dereference. This simplifies processing for several
6184 -- cases, including packed array cases and certain cases in which checks
6185 -- must be generated. We used to try to do this only when it was
6186 -- necessary, but it cleans up the code to do it all the time.
6188 if Is_Access_Type
(T
) then
6189 Insert_Explicit_Dereference
(P
);
6190 Analyze_And_Resolve
(P
, Designated_Type
(T
));
6191 Atp
:= Designated_Type
(T
);
6196 -- Generate index and validity checks
6198 Generate_Index_Checks
(N
);
6200 if Validity_Checks_On
and then Validity_Check_Subscripts
then
6201 Apply_Subscript_Validity_Checks
(N
);
6204 -- If selecting from an array with atomic components, and atomic sync
6205 -- is not suppressed for this array type, set atomic sync flag.
6207 if (Has_Atomic_Components
(Atp
)
6208 and then not Atomic_Synchronization_Disabled
(Atp
))
6209 or else (Is_Atomic
(Typ
)
6210 and then not Atomic_Synchronization_Disabled
(Typ
))
6212 Activate_Atomic_Synchronization
(N
);
6215 -- All done for the non-packed case
6217 if not Is_Packed
(Etype
(Prefix
(N
))) then
6221 -- For packed arrays that are not bit-packed (i.e. the case of an array
6222 -- with one or more index types with a non-contiguous enumeration type),
6223 -- we can always use the normal packed element get circuit.
6225 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
6226 Expand_Packed_Element_Reference
(N
);
6230 -- For a reference to a component of a bit packed array, we have to
6231 -- convert it to a reference to the corresponding Packed_Array_Type.
6232 -- We only want to do this for simple references, and not for:
6234 -- Left side of assignment, or prefix of left side of assignment, or
6235 -- prefix of the prefix, to handle packed arrays of packed arrays,
6236 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6238 -- Renaming objects in renaming associations
6239 -- This case is handled when a use of the renamed variable occurs
6241 -- Actual parameters for a procedure call
6242 -- This case is handled in Exp_Ch6.Expand_Actuals
6244 -- The second expression in a 'Read attribute reference
6246 -- The prefix of an address or bit or size attribute reference
6248 -- The following circuit detects these exceptions
6251 Child
: Node_Id
:= N
;
6252 Parnt
: Node_Id
:= Parent
(N
);
6256 if Nkind
(Parnt
) = N_Unchecked_Expression
then
6259 elsif Nkind_In
(Parnt
, N_Object_Renaming_Declaration
,
6260 N_Procedure_Call_Statement
)
6261 or else (Nkind
(Parnt
) = N_Parameter_Association
6263 Nkind
(Parent
(Parnt
)) = N_Procedure_Call_Statement
)
6267 elsif Nkind
(Parnt
) = N_Attribute_Reference
6268 and then (Attribute_Name
(Parnt
) = Name_Address
6270 Attribute_Name
(Parnt
) = Name_Bit
6272 Attribute_Name
(Parnt
) = Name_Size
)
6273 and then Prefix
(Parnt
) = Child
6277 elsif Nkind
(Parnt
) = N_Assignment_Statement
6278 and then Name
(Parnt
) = Child
6282 -- If the expression is an index of an indexed component, it must
6283 -- be expanded regardless of context.
6285 elsif Nkind
(Parnt
) = N_Indexed_Component
6286 and then Child
/= Prefix
(Parnt
)
6288 Expand_Packed_Element_Reference
(N
);
6291 elsif Nkind
(Parent
(Parnt
)) = N_Assignment_Statement
6292 and then Name
(Parent
(Parnt
)) = Parnt
6296 elsif Nkind
(Parnt
) = N_Attribute_Reference
6297 and then Attribute_Name
(Parnt
) = Name_Read
6298 and then Next
(First
(Expressions
(Parnt
))) = Child
6302 elsif Nkind_In
(Parnt
, N_Indexed_Component
, N_Selected_Component
)
6303 and then Prefix
(Parnt
) = Child
6308 Expand_Packed_Element_Reference
(N
);
6312 -- Keep looking up tree for unchecked expression, or if we are the
6313 -- prefix of a possible assignment left side.
6316 Parnt
:= Parent
(Child
);
6319 end Expand_N_Indexed_Component
;
6321 ---------------------
6322 -- Expand_N_Not_In --
6323 ---------------------
6325 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6326 -- can be done. This avoids needing to duplicate this expansion code.
6328 procedure Expand_N_Not_In
(N
: Node_Id
) is
6329 Loc
: constant Source_Ptr
:= Sloc
(N
);
6330 Typ
: constant Entity_Id
:= Etype
(N
);
6331 Cfs
: constant Boolean := Comes_From_Source
(N
);
6338 Left_Opnd
=> Left_Opnd
(N
),
6339 Right_Opnd
=> Right_Opnd
(N
))));
6341 -- If this is a set membership, preserve list of alternatives
6343 Set_Alternatives
(Right_Opnd
(N
), Alternatives
(Original_Node
(N
)));
6345 -- We want this to appear as coming from source if original does (see
6346 -- transformations in Expand_N_In).
6348 Set_Comes_From_Source
(N
, Cfs
);
6349 Set_Comes_From_Source
(Right_Opnd
(N
), Cfs
);
6351 -- Now analyze transformed node
6353 Analyze_And_Resolve
(N
, Typ
);
6354 end Expand_N_Not_In
;
6360 -- The only replacement required is for the case of a null of a type that
6361 -- is an access to protected subprogram, or a subtype thereof. We represent
6362 -- such access values as a record, and so we must replace the occurrence of
6363 -- null by the equivalent record (with a null address and a null pointer in
6364 -- it), so that the backend creates the proper value.
6366 procedure Expand_N_Null
(N
: Node_Id
) is
6367 Loc
: constant Source_Ptr
:= Sloc
(N
);
6368 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
6372 if Is_Access_Protected_Subprogram_Type
(Typ
) then
6374 Make_Aggregate
(Loc
,
6375 Expressions
=> New_List
(
6376 New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
),
6380 Analyze_And_Resolve
(N
, Equivalent_Type
(Typ
));
6382 -- For subsequent semantic analysis, the node must retain its type.
6383 -- Gigi in any case replaces this type by the corresponding record
6384 -- type before processing the node.
6390 when RE_Not_Available
=>
6394 ---------------------
6395 -- Expand_N_Op_Abs --
6396 ---------------------
6398 procedure Expand_N_Op_Abs
(N
: Node_Id
) is
6399 Loc
: constant Source_Ptr
:= Sloc
(N
);
6400 Expr
: constant Node_Id
:= Right_Opnd
(N
);
6403 Unary_Op_Validity_Checks
(N
);
6405 -- Check for MINIMIZED/ELIMINATED overflow mode
6407 if Minimized_Eliminated_Overflow_Check
(N
) then
6408 Apply_Arithmetic_Overflow_Check
(N
);
6412 -- Deal with software overflow checking
6414 if not Backend_Overflow_Checks_On_Target
6415 and then Is_Signed_Integer_Type
(Etype
(N
))
6416 and then Do_Overflow_Check
(N
)
6418 -- The only case to worry about is when the argument is equal to the
6419 -- largest negative number, so what we do is to insert the check:
6421 -- [constraint_error when Expr = typ'Base'First]
6423 -- with the usual Duplicate_Subexpr use coding for expr
6426 Make_Raise_Constraint_Error
(Loc
,
6429 Left_Opnd
=> Duplicate_Subexpr
(Expr
),
6431 Make_Attribute_Reference
(Loc
,
6433 New_Occurrence_Of
(Base_Type
(Etype
(Expr
)), Loc
),
6434 Attribute_Name
=> Name_First
)),
6435 Reason
=> CE_Overflow_Check_Failed
));
6438 -- Vax floating-point types case
6440 if Vax_Float
(Etype
(N
)) then
6441 Expand_Vax_Arith
(N
);
6443 end Expand_N_Op_Abs
;
6445 ---------------------
6446 -- Expand_N_Op_Add --
6447 ---------------------
6449 procedure Expand_N_Op_Add
(N
: Node_Id
) is
6450 Typ
: constant Entity_Id
:= Etype
(N
);
6453 Binary_Op_Validity_Checks
(N
);
6455 -- Check for MINIMIZED/ELIMINATED overflow mode
6457 if Minimized_Eliminated_Overflow_Check
(N
) then
6458 Apply_Arithmetic_Overflow_Check
(N
);
6462 -- N + 0 = 0 + N = N for integer types
6464 if Is_Integer_Type
(Typ
) then
6465 if Compile_Time_Known_Value
(Right_Opnd
(N
))
6466 and then Expr_Value
(Right_Opnd
(N
)) = Uint_0
6468 Rewrite
(N
, Left_Opnd
(N
));
6471 elsif Compile_Time_Known_Value
(Left_Opnd
(N
))
6472 and then Expr_Value
(Left_Opnd
(N
)) = Uint_0
6474 Rewrite
(N
, Right_Opnd
(N
));
6479 -- Arithmetic overflow checks for signed integer/fixed point types
6481 if Is_Signed_Integer_Type
(Typ
)
6482 or else Is_Fixed_Point_Type
(Typ
)
6484 Apply_Arithmetic_Overflow_Check
(N
);
6487 -- Vax floating-point types case
6489 elsif Vax_Float
(Typ
) then
6490 Expand_Vax_Arith
(N
);
6492 end Expand_N_Op_Add
;
6494 ---------------------
6495 -- Expand_N_Op_And --
6496 ---------------------
6498 procedure Expand_N_Op_And
(N
: Node_Id
) is
6499 Typ
: constant Entity_Id
:= Etype
(N
);
6502 Binary_Op_Validity_Checks
(N
);
6504 if Is_Array_Type
(Etype
(N
)) then
6505 Expand_Boolean_Operator
(N
);
6507 elsif Is_Boolean_Type
(Etype
(N
)) then
6508 Adjust_Condition
(Left_Opnd
(N
));
6509 Adjust_Condition
(Right_Opnd
(N
));
6510 Set_Etype
(N
, Standard_Boolean
);
6511 Adjust_Result_Type
(N
, Typ
);
6513 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
6514 Expand_Intrinsic_Call
(N
, Entity
(N
));
6517 end Expand_N_Op_And
;
6519 ------------------------
6520 -- Expand_N_Op_Concat --
6521 ------------------------
6523 procedure Expand_N_Op_Concat
(N
: Node_Id
) is
6525 -- List of operands to be concatenated
6528 -- Node which is to be replaced by the result of concatenating the nodes
6529 -- in the list Opnds.
6532 -- Ensure validity of both operands
6534 Binary_Op_Validity_Checks
(N
);
6536 -- If we are the left operand of a concatenation higher up the tree,
6537 -- then do nothing for now, since we want to deal with a series of
6538 -- concatenations as a unit.
6540 if Nkind
(Parent
(N
)) = N_Op_Concat
6541 and then N
= Left_Opnd
(Parent
(N
))
6546 -- We get here with a concatenation whose left operand may be a
6547 -- concatenation itself with a consistent type. We need to process
6548 -- these concatenation operands from left to right, which means
6549 -- from the deepest node in the tree to the highest node.
6552 while Nkind
(Left_Opnd
(Cnode
)) = N_Op_Concat
loop
6553 Cnode
:= Left_Opnd
(Cnode
);
6556 -- Now Cnode is the deepest concatenation, and its parents are the
6557 -- concatenation nodes above, so now we process bottom up, doing the
6558 -- operations. We gather a string that is as long as possible up to five
6561 -- The outer loop runs more than once if more than one concatenation
6562 -- type is involved.
6565 Opnds
:= New_List
(Left_Opnd
(Cnode
), Right_Opnd
(Cnode
));
6566 Set_Parent
(Opnds
, N
);
6568 -- The inner loop gathers concatenation operands
6570 Inner
: while Cnode
/= N
6571 and then Base_Type
(Etype
(Cnode
)) =
6572 Base_Type
(Etype
(Parent
(Cnode
)))
6574 Cnode
:= Parent
(Cnode
);
6575 Append
(Right_Opnd
(Cnode
), Opnds
);
6578 Expand_Concatenate
(Cnode
, Opnds
);
6580 exit Outer
when Cnode
= N
;
6581 Cnode
:= Parent
(Cnode
);
6583 end Expand_N_Op_Concat
;
6585 ------------------------
6586 -- Expand_N_Op_Divide --
6587 ------------------------
6589 procedure Expand_N_Op_Divide
(N
: Node_Id
) is
6590 Loc
: constant Source_Ptr
:= Sloc
(N
);
6591 Lopnd
: constant Node_Id
:= Left_Opnd
(N
);
6592 Ropnd
: constant Node_Id
:= Right_Opnd
(N
);
6593 Ltyp
: constant Entity_Id
:= Etype
(Lopnd
);
6594 Rtyp
: constant Entity_Id
:= Etype
(Ropnd
);
6595 Typ
: Entity_Id
:= Etype
(N
);
6596 Rknow
: constant Boolean := Is_Integer_Type
(Typ
)
6598 Compile_Time_Known_Value
(Ropnd
);
6602 Binary_Op_Validity_Checks
(N
);
6604 -- Check for MINIMIZED/ELIMINATED overflow mode
6606 if Minimized_Eliminated_Overflow_Check
(N
) then
6607 Apply_Arithmetic_Overflow_Check
(N
);
6611 -- Otherwise proceed with expansion of division
6614 Rval
:= Expr_Value
(Ropnd
);
6617 -- N / 1 = N for integer types
6619 if Rknow
and then Rval
= Uint_1
then
6624 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
6625 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6626 -- operand is an unsigned integer, as required for this to work.
6628 if Nkind
(Ropnd
) = N_Op_Expon
6629 and then Is_Power_Of_2_For_Shift
(Ropnd
)
6631 -- We cannot do this transformation in configurable run time mode if we
6632 -- have 64-bit integers and long shifts are not available.
6636 or else Support_Long_Shifts_On_Target
)
6639 Make_Op_Shift_Right
(Loc
,
6642 Convert_To
(Standard_Natural
, Right_Opnd
(Ropnd
))));
6643 Analyze_And_Resolve
(N
, Typ
);
6647 -- Do required fixup of universal fixed operation
6649 if Typ
= Universal_Fixed
then
6650 Fixup_Universal_Fixed_Operation
(N
);
6654 -- Divisions with fixed-point results
6656 if Is_Fixed_Point_Type
(Typ
) then
6658 -- No special processing if Treat_Fixed_As_Integer is set, since
6659 -- from a semantic point of view such operations are simply integer
6660 -- operations and will be treated that way.
6662 if not Treat_Fixed_As_Integer
(N
) then
6663 if Is_Integer_Type
(Rtyp
) then
6664 Expand_Divide_Fixed_By_Integer_Giving_Fixed
(N
);
6666 Expand_Divide_Fixed_By_Fixed_Giving_Fixed
(N
);
6670 -- Other cases of division of fixed-point operands. Again we exclude the
6671 -- case where Treat_Fixed_As_Integer is set.
6673 elsif (Is_Fixed_Point_Type
(Ltyp
) or else
6674 Is_Fixed_Point_Type
(Rtyp
))
6675 and then not Treat_Fixed_As_Integer
(N
)
6677 if Is_Integer_Type
(Typ
) then
6678 Expand_Divide_Fixed_By_Fixed_Giving_Integer
(N
);
6680 pragma Assert
(Is_Floating_Point_Type
(Typ
));
6681 Expand_Divide_Fixed_By_Fixed_Giving_Float
(N
);
6684 -- Mixed-mode operations can appear in a non-static universal context,
6685 -- in which case the integer argument must be converted explicitly.
6687 elsif Typ
= Universal_Real
6688 and then Is_Integer_Type
(Rtyp
)
6691 Convert_To
(Universal_Real
, Relocate_Node
(Ropnd
)));
6693 Analyze_And_Resolve
(Ropnd
, Universal_Real
);
6695 elsif Typ
= Universal_Real
6696 and then Is_Integer_Type
(Ltyp
)
6699 Convert_To
(Universal_Real
, Relocate_Node
(Lopnd
)));
6701 Analyze_And_Resolve
(Lopnd
, Universal_Real
);
6703 -- Non-fixed point cases, do integer zero divide and overflow checks
6705 elsif Is_Integer_Type
(Typ
) then
6706 Apply_Divide_Checks
(N
);
6708 -- Deal with Vax_Float
6710 elsif Vax_Float
(Typ
) then
6711 Expand_Vax_Arith
(N
);
6714 end Expand_N_Op_Divide
;
6716 --------------------
6717 -- Expand_N_Op_Eq --
6718 --------------------
6720 procedure Expand_N_Op_Eq
(N
: Node_Id
) is
6721 Loc
: constant Source_Ptr
:= Sloc
(N
);
6722 Typ
: constant Entity_Id
:= Etype
(N
);
6723 Lhs
: constant Node_Id
:= Left_Opnd
(N
);
6724 Rhs
: constant Node_Id
:= Right_Opnd
(N
);
6725 Bodies
: constant List_Id
:= New_List
;
6726 A_Typ
: constant Entity_Id
:= Etype
(Lhs
);
6728 Typl
: Entity_Id
:= A_Typ
;
6729 Op_Name
: Entity_Id
;
6732 procedure Build_Equality_Call
(Eq
: Entity_Id
);
6733 -- If a constructed equality exists for the type or for its parent,
6734 -- build and analyze call, adding conversions if the operation is
6737 function Has_Unconstrained_UU_Component
(Typ
: Node_Id
) return Boolean;
6738 -- Determines whether a type has a subcomponent of an unconstrained
6739 -- Unchecked_Union subtype. Typ is a record type.
6741 -------------------------
6742 -- Build_Equality_Call --
6743 -------------------------
6745 procedure Build_Equality_Call
(Eq
: Entity_Id
) is
6746 Op_Type
: constant Entity_Id
:= Etype
(First_Formal
(Eq
));
6747 L_Exp
: Node_Id
:= Relocate_Node
(Lhs
);
6748 R_Exp
: Node_Id
:= Relocate_Node
(Rhs
);
6751 if Base_Type
(Op_Type
) /= Base_Type
(A_Typ
)
6752 and then not Is_Class_Wide_Type
(A_Typ
)
6754 L_Exp
:= OK_Convert_To
(Op_Type
, L_Exp
);
6755 R_Exp
:= OK_Convert_To
(Op_Type
, R_Exp
);
6758 -- If we have an Unchecked_Union, we need to add the inferred
6759 -- discriminant values as actuals in the function call. At this
6760 -- point, the expansion has determined that both operands have
6761 -- inferable discriminants.
6763 if Is_Unchecked_Union
(Op_Type
) then
6765 Lhs_Type
: constant Node_Id
:= Etype
(L_Exp
);
6766 Rhs_Type
: constant Node_Id
:= Etype
(R_Exp
);
6767 Lhs_Discr_Val
: Node_Id
;
6768 Rhs_Discr_Val
: Node_Id
;
6771 -- Per-object constrained selected components require special
6772 -- attention. If the enclosing scope of the component is an
6773 -- Unchecked_Union, we cannot reference its discriminants
6774 -- directly. This is why we use the two extra parameters of
6775 -- the equality function of the enclosing Unchecked_Union.
6777 -- type UU_Type (Discr : Integer := 0) is
6780 -- pragma Unchecked_Union (UU_Type);
6782 -- 1. Unchecked_Union enclosing record:
6784 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
6786 -- Comp : UU_Type (Discr);
6788 -- end Enclosing_UU_Type;
6789 -- pragma Unchecked_Union (Enclosing_UU_Type);
6791 -- Obj1 : Enclosing_UU_Type;
6792 -- Obj2 : Enclosing_UU_Type (1);
6794 -- [. . .] Obj1 = Obj2 [. . .]
6798 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
6800 -- A and B are the formal parameters of the equality function
6801 -- of Enclosing_UU_Type. The function always has two extra
6802 -- formals to capture the inferred discriminant values.
6804 -- 2. Non-Unchecked_Union enclosing record:
6807 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
6810 -- Comp : UU_Type (Discr);
6812 -- end Enclosing_Non_UU_Type;
6814 -- Obj1 : Enclosing_Non_UU_Type;
6815 -- Obj2 : Enclosing_Non_UU_Type (1);
6817 -- ... Obj1 = Obj2 ...
6821 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
6822 -- obj1.discr, obj2.discr)) then
6824 -- In this case we can directly reference the discriminants of
6825 -- the enclosing record.
6829 if Nkind
(Lhs
) = N_Selected_Component
6830 and then Has_Per_Object_Constraint
6831 (Entity
(Selector_Name
(Lhs
)))
6833 -- Enclosing record is an Unchecked_Union, use formal A
6835 if Is_Unchecked_Union
6836 (Scope
(Entity
(Selector_Name
(Lhs
))))
6838 Lhs_Discr_Val
:= Make_Identifier
(Loc
, Name_A
);
6840 -- Enclosing record is of a non-Unchecked_Union type, it is
6841 -- possible to reference the discriminant.
6845 Make_Selected_Component
(Loc
,
6846 Prefix
=> Prefix
(Lhs
),
6849 (Get_Discriminant_Value
6850 (First_Discriminant
(Lhs_Type
),
6852 Stored_Constraint
(Lhs_Type
))));
6855 -- Comment needed here ???
6858 -- Infer the discriminant value
6862 (Get_Discriminant_Value
6863 (First_Discriminant
(Lhs_Type
),
6865 Stored_Constraint
(Lhs_Type
)));
6870 if Nkind
(Rhs
) = N_Selected_Component
6871 and then Has_Per_Object_Constraint
6872 (Entity
(Selector_Name
(Rhs
)))
6874 if Is_Unchecked_Union
6875 (Scope
(Entity
(Selector_Name
(Rhs
))))
6877 Rhs_Discr_Val
:= Make_Identifier
(Loc
, Name_B
);
6881 Make_Selected_Component
(Loc
,
6882 Prefix
=> Prefix
(Rhs
),
6884 New_Copy
(Get_Discriminant_Value
(
6885 First_Discriminant
(Rhs_Type
),
6887 Stored_Constraint
(Rhs_Type
))));
6892 New_Copy
(Get_Discriminant_Value
(
6893 First_Discriminant
(Rhs_Type
),
6895 Stored_Constraint
(Rhs_Type
)));
6900 Make_Function_Call
(Loc
,
6901 Name
=> New_Reference_To
(Eq
, Loc
),
6902 Parameter_Associations
=> New_List
(
6909 -- Normal case, not an unchecked union
6913 Make_Function_Call
(Loc
,
6914 Name
=> New_Reference_To
(Eq
, Loc
),
6915 Parameter_Associations
=> New_List
(L_Exp
, R_Exp
)));
6918 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
6919 end Build_Equality_Call
;
6921 ------------------------------------
6922 -- Has_Unconstrained_UU_Component --
6923 ------------------------------------
6925 function Has_Unconstrained_UU_Component
6926 (Typ
: Node_Id
) return Boolean
6928 Tdef
: constant Node_Id
:=
6929 Type_Definition
(Declaration_Node
(Base_Type
(Typ
)));
6933 function Component_Is_Unconstrained_UU
6934 (Comp
: Node_Id
) return Boolean;
6935 -- Determines whether the subtype of the component is an
6936 -- unconstrained Unchecked_Union.
6938 function Variant_Is_Unconstrained_UU
6939 (Variant
: Node_Id
) return Boolean;
6940 -- Determines whether a component of the variant has an unconstrained
6941 -- Unchecked_Union subtype.
6943 -----------------------------------
6944 -- Component_Is_Unconstrained_UU --
6945 -----------------------------------
6947 function Component_Is_Unconstrained_UU
6948 (Comp
: Node_Id
) return Boolean
6951 if Nkind
(Comp
) /= N_Component_Declaration
then
6956 Sindic
: constant Node_Id
:=
6957 Subtype_Indication
(Component_Definition
(Comp
));
6960 -- Unconstrained nominal type. In the case of a constraint
6961 -- present, the node kind would have been N_Subtype_Indication.
6963 if Nkind
(Sindic
) = N_Identifier
then
6964 return Is_Unchecked_Union
(Base_Type
(Etype
(Sindic
)));
6969 end Component_Is_Unconstrained_UU
;
6971 ---------------------------------
6972 -- Variant_Is_Unconstrained_UU --
6973 ---------------------------------
6975 function Variant_Is_Unconstrained_UU
6976 (Variant
: Node_Id
) return Boolean
6978 Clist
: constant Node_Id
:= Component_List
(Variant
);
6981 if Is_Empty_List
(Component_Items
(Clist
)) then
6985 -- We only need to test one component
6988 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
6991 while Present
(Comp
) loop
6992 if Component_Is_Unconstrained_UU
(Comp
) then
7000 -- None of the components withing the variant were of
7001 -- unconstrained Unchecked_Union type.
7004 end Variant_Is_Unconstrained_UU
;
7006 -- Start of processing for Has_Unconstrained_UU_Component
7009 if Null_Present
(Tdef
) then
7013 Clist
:= Component_List
(Tdef
);
7014 Vpart
:= Variant_Part
(Clist
);
7016 -- Inspect available components
7018 if Present
(Component_Items
(Clist
)) then
7020 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
7023 while Present
(Comp
) loop
7025 -- One component is sufficient
7027 if Component_Is_Unconstrained_UU
(Comp
) then
7036 -- Inspect available components withing variants
7038 if Present
(Vpart
) then
7040 Variant
: Node_Id
:= First
(Variants
(Vpart
));
7043 while Present
(Variant
) loop
7045 -- One component within a variant is sufficient
7047 if Variant_Is_Unconstrained_UU
(Variant
) then
7056 -- Neither the available components, nor the components inside the
7057 -- variant parts were of an unconstrained Unchecked_Union subtype.
7060 end Has_Unconstrained_UU_Component
;
7062 -- Start of processing for Expand_N_Op_Eq
7065 Binary_Op_Validity_Checks
(N
);
7067 -- Deal with private types
7069 if Ekind
(Typl
) = E_Private_Type
then
7070 Typl
:= Underlying_Type
(Typl
);
7071 elsif Ekind
(Typl
) = E_Private_Subtype
then
7072 Typl
:= Underlying_Type
(Base_Type
(Typl
));
7077 -- It may happen in error situations that the underlying type is not
7078 -- set. The error will be detected later, here we just defend the
7085 Typl
:= Base_Type
(Typl
);
7087 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7088 -- means we no longer have a comparison operation, we are all done.
7090 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
7092 if Nkind
(N
) /= N_Op_Eq
then
7096 -- Boolean types (requiring handling of non-standard case)
7098 if Is_Boolean_Type
(Typl
) then
7099 Adjust_Condition
(Left_Opnd
(N
));
7100 Adjust_Condition
(Right_Opnd
(N
));
7101 Set_Etype
(N
, Standard_Boolean
);
7102 Adjust_Result_Type
(N
, Typ
);
7106 elsif Is_Array_Type
(Typl
) then
7108 -- If we are doing full validity checking, and it is possible for the
7109 -- array elements to be invalid then expand out array comparisons to
7110 -- make sure that we check the array elements.
7112 if Validity_Check_Operands
7113 and then not Is_Known_Valid
(Component_Type
(Typl
))
7116 Save_Force_Validity_Checks
: constant Boolean :=
7117 Force_Validity_Checks
;
7119 Force_Validity_Checks
:= True;
7121 Expand_Array_Equality
7123 Relocate_Node
(Lhs
),
7124 Relocate_Node
(Rhs
),
7127 Insert_Actions
(N
, Bodies
);
7128 Analyze_And_Resolve
(N
, Standard_Boolean
);
7129 Force_Validity_Checks
:= Save_Force_Validity_Checks
;
7132 -- Packed case where both operands are known aligned
7134 elsif Is_Bit_Packed_Array
(Typl
)
7135 and then not Is_Possibly_Unaligned_Object
(Lhs
)
7136 and then not Is_Possibly_Unaligned_Object
(Rhs
)
7138 Expand_Packed_Eq
(N
);
7140 -- Where the component type is elementary we can use a block bit
7141 -- comparison (if supported on the target) exception in the case
7142 -- of floating-point (negative zero issues require element by
7143 -- element comparison), and atomic types (where we must be sure
7144 -- to load elements independently) and possibly unaligned arrays.
7146 elsif Is_Elementary_Type
(Component_Type
(Typl
))
7147 and then not Is_Floating_Point_Type
(Component_Type
(Typl
))
7148 and then not Is_Atomic
(Component_Type
(Typl
))
7149 and then not Is_Possibly_Unaligned_Object
(Lhs
)
7150 and then not Is_Possibly_Unaligned_Object
(Rhs
)
7151 and then Support_Composite_Compare_On_Target
7155 -- For composite and floating-point cases, expand equality loop to
7156 -- make sure of using proper comparisons for tagged types, and
7157 -- correctly handling the floating-point case.
7161 Expand_Array_Equality
7163 Relocate_Node
(Lhs
),
7164 Relocate_Node
(Rhs
),
7167 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
7168 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7173 elsif Is_Record_Type
(Typl
) then
7175 -- For tagged types, use the primitive "="
7177 if Is_Tagged_Type
(Typl
) then
7179 -- No need to do anything else compiling under restriction
7180 -- No_Dispatching_Calls. During the semantic analysis we
7181 -- already notified such violation.
7183 if Restriction_Active
(No_Dispatching_Calls
) then
7187 -- If this is derived from an untagged private type completed with
7188 -- a tagged type, it does not have a full view, so we use the
7189 -- primitive operations of the private type. This check should no
7190 -- longer be necessary when these types get their full views???
7192 if Is_Private_Type
(A_Typ
)
7193 and then not Is_Tagged_Type
(A_Typ
)
7194 and then Is_Derived_Type
(A_Typ
)
7195 and then No
(Full_View
(A_Typ
))
7197 -- Search for equality operation, checking that the operands
7198 -- have the same type. Note that we must find a matching entry,
7199 -- or something is very wrong!
7201 Prim
:= First_Elmt
(Collect_Primitive_Operations
(A_Typ
));
7203 while Present
(Prim
) loop
7204 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7205 and then Etype
(First_Formal
(Node
(Prim
))) =
7206 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7208 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7213 pragma Assert
(Present
(Prim
));
7214 Op_Name
:= Node
(Prim
);
7216 -- Find the type's predefined equality or an overriding
7217 -- user- defined equality. The reason for not simply calling
7218 -- Find_Prim_Op here is that there may be a user-defined
7219 -- overloaded equality op that precedes the equality that we want,
7220 -- so we have to explicitly search (e.g., there could be an
7221 -- equality with two different parameter types).
7224 if Is_Class_Wide_Type
(Typl
) then
7225 Typl
:= Root_Type
(Typl
);
7228 Prim
:= First_Elmt
(Primitive_Operations
(Typl
));
7229 while Present
(Prim
) loop
7230 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7231 and then Etype
(First_Formal
(Node
(Prim
))) =
7232 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7234 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7239 pragma Assert
(Present
(Prim
));
7240 Op_Name
:= Node
(Prim
);
7243 Build_Equality_Call
(Op_Name
);
7245 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7246 -- predefined equality operator for a type which has a subcomponent
7247 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7249 elsif Has_Unconstrained_UU_Component
(Typl
) then
7251 Make_Raise_Program_Error
(Loc
,
7252 Reason
=> PE_Unchecked_Union_Restriction
));
7254 -- Prevent Gigi from generating incorrect code by rewriting the
7255 -- equality as a standard False. (is this documented somewhere???)
7258 New_Occurrence_Of
(Standard_False
, Loc
));
7260 elsif Is_Unchecked_Union
(Typl
) then
7262 -- If we can infer the discriminants of the operands, we make a
7263 -- call to the TSS equality function.
7265 if Has_Inferable_Discriminants
(Lhs
)
7267 Has_Inferable_Discriminants
(Rhs
)
7270 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
7273 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7274 -- the predefined equality operator for an Unchecked_Union type
7275 -- if either of the operands lack inferable discriminants.
7278 Make_Raise_Program_Error
(Loc
,
7279 Reason
=> PE_Unchecked_Union_Restriction
));
7281 -- Prevent Gigi from generating incorrect code by rewriting
7282 -- the equality as a standard False (documented where???).
7285 New_Occurrence_Of
(Standard_False
, Loc
));
7289 -- If a type support function is present (for complex cases), use it
7291 elsif Present
(TSS
(Root_Type
(Typl
), TSS_Composite_Equality
)) then
7293 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
7295 -- Otherwise expand the component by component equality. Note that
7296 -- we never use block-bit comparisons for records, because of the
7297 -- problems with gaps. The backend will often be able to recombine
7298 -- the separate comparisons that we generate here.
7301 Remove_Side_Effects
(Lhs
);
7302 Remove_Side_Effects
(Rhs
);
7304 Expand_Record_Equality
(N
, Typl
, Lhs
, Rhs
, Bodies
));
7306 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
7307 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7311 -- Test if result is known at compile time
7313 Rewrite_Comparison
(N
);
7315 -- If we still have comparison for Vax_Float, process it
7317 if Vax_Float
(Typl
) and then Nkind
(N
) in N_Op_Compare
then
7318 Expand_Vax_Comparison
(N
);
7322 Optimize_Length_Comparison
(N
);
7325 -----------------------
7326 -- Expand_N_Op_Expon --
7327 -----------------------
7329 procedure Expand_N_Op_Expon
(N
: Node_Id
) is
7330 Loc
: constant Source_Ptr
:= Sloc
(N
);
7331 Typ
: constant Entity_Id
:= Etype
(N
);
7332 Rtyp
: constant Entity_Id
:= Root_Type
(Typ
);
7333 Base
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
7334 Bastyp
: constant Node_Id
:= Etype
(Base
);
7335 Exp
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
7336 Exptyp
: constant Entity_Id
:= Etype
(Exp
);
7337 Ovflo
: constant Boolean := Do_Overflow_Check
(N
);
7346 Binary_Op_Validity_Checks
(N
);
7348 -- CodePeer and GNATprove want to see the unexpanded N_Op_Expon node
7350 if CodePeer_Mode
or Alfa_Mode
then
7354 -- If either operand is of a private type, then we have the use of an
7355 -- intrinsic operator, and we get rid of the privateness, by using root
7356 -- types of underlying types for the actual operation. Otherwise the
7357 -- private types will cause trouble if we expand multiplications or
7358 -- shifts etc. We also do this transformation if the result type is
7359 -- different from the base type.
7361 if Is_Private_Type
(Etype
(Base
))
7362 or else Is_Private_Type
(Typ
)
7363 or else Is_Private_Type
(Exptyp
)
7364 or else Rtyp
/= Root_Type
(Bastyp
)
7367 Bt
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Bastyp
));
7368 Et
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Exptyp
));
7372 Unchecked_Convert_To
(Typ
,
7374 Left_Opnd
=> Unchecked_Convert_To
(Bt
, Base
),
7375 Right_Opnd
=> Unchecked_Convert_To
(Et
, Exp
))));
7376 Analyze_And_Resolve
(N
, Typ
);
7381 -- Check for MINIMIZED/ELIMINATED overflow mode
7383 if Minimized_Eliminated_Overflow_Check
(N
) then
7384 Apply_Arithmetic_Overflow_Check
(N
);
7388 -- Test for case of known right argument where we can replace the
7389 -- exponentiation by an equivalent expression using multiplication.
7391 if Compile_Time_Known_Value
(Exp
) then
7392 Expv
:= Expr_Value
(Exp
);
7394 -- We only fold small non-negative exponents. You might think we
7395 -- could fold small negative exponents for the real case, but we
7396 -- can't because we are required to raise Constraint_Error for
7397 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
7398 -- See ACVC test C4A012B.
7400 if Expv
>= 0 and then Expv
<= 4 then
7402 -- X ** 0 = 1 (or 1.0)
7406 -- Call Remove_Side_Effects to ensure that any side effects
7407 -- in the ignored left operand (in particular function calls
7408 -- to user defined functions) are properly executed.
7410 Remove_Side_Effects
(Base
);
7412 if Ekind
(Typ
) in Integer_Kind
then
7413 Xnode
:= Make_Integer_Literal
(Loc
, Intval
=> 1);
7415 Xnode
:= Make_Real_Literal
(Loc
, Ureal_1
);
7427 Make_Op_Multiply
(Loc
,
7428 Left_Opnd
=> Duplicate_Subexpr
(Base
),
7429 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
));
7431 -- X ** 3 = X * X * X
7435 Make_Op_Multiply
(Loc
,
7437 Make_Op_Multiply
(Loc
,
7438 Left_Opnd
=> Duplicate_Subexpr
(Base
),
7439 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)),
7440 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
));
7445 -- En : constant base'type := base * base;
7450 pragma Assert
(Expv
= 4);
7451 Temp
:= Make_Temporary
(Loc
, 'E', Base
);
7454 Make_Expression_With_Actions
(Loc
,
7455 Actions
=> New_List
(
7456 Make_Object_Declaration
(Loc
,
7457 Defining_Identifier
=> Temp
,
7458 Constant_Present
=> True,
7459 Object_Definition
=> New_Reference_To
(Typ
, Loc
),
7461 Make_Op_Multiply
(Loc
,
7463 Duplicate_Subexpr
(Base
),
7465 Duplicate_Subexpr_No_Checks
(Base
)))),
7468 Make_Op_Multiply
(Loc
,
7469 Left_Opnd
=> New_Reference_To
(Temp
, Loc
),
7470 Right_Opnd
=> New_Reference_To
(Temp
, Loc
)));
7474 Analyze_And_Resolve
(N
, Typ
);
7479 -- Case of (2 ** expression) appearing as an argument of an integer
7480 -- multiplication, or as the right argument of a division of a non-
7481 -- negative integer. In such cases we leave the node untouched, setting
7482 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
7483 -- of the higher level node converts it into a shift.
7485 -- Another case is 2 ** N in any other context. We simply convert
7486 -- this to 1 * 2 ** N, and then the above transformation applies.
7488 -- Note: this transformation is not applicable for a modular type with
7489 -- a non-binary modulus in the multiplication case, since we get a wrong
7490 -- result if the shift causes an overflow before the modular reduction.
7492 if Nkind
(Base
) = N_Integer_Literal
7493 and then Intval
(Base
) = 2
7494 and then Is_Integer_Type
(Root_Type
(Exptyp
))
7495 and then Esize
(Root_Type
(Exptyp
)) <= Esize
(Standard_Integer
)
7496 and then Is_Unsigned_Type
(Exptyp
)
7499 -- First the multiply and divide cases
7501 if Nkind_In
(Parent
(N
), N_Op_Divide
, N_Op_Multiply
) then
7503 P
: constant Node_Id
:= Parent
(N
);
7504 L
: constant Node_Id
:= Left_Opnd
(P
);
7505 R
: constant Node_Id
:= Right_Opnd
(P
);
7508 if (Nkind
(P
) = N_Op_Multiply
7509 and then not Non_Binary_Modulus
(Typ
)
7511 ((Is_Integer_Type
(Etype
(L
)) and then R
= N
)
7513 (Is_Integer_Type
(Etype
(R
)) and then L
= N
))
7514 and then not Do_Overflow_Check
(P
))
7516 (Nkind
(P
) = N_Op_Divide
7517 and then Is_Integer_Type
(Etype
(L
))
7518 and then Is_Unsigned_Type
(Etype
(L
))
7520 and then not Do_Overflow_Check
(P
))
7522 Set_Is_Power_Of_2_For_Shift
(N
);
7527 -- Now the other cases
7529 elsif not Non_Binary_Modulus
(Typ
) then
7531 Make_Op_Multiply
(Loc
,
7532 Left_Opnd
=> Make_Integer_Literal
(Loc
, 1),
7533 Right_Opnd
=> Relocate_Node
(N
)));
7534 Analyze_And_Resolve
(N
, Typ
);
7539 -- Fall through if exponentiation must be done using a runtime routine
7541 -- First deal with modular case
7543 if Is_Modular_Integer_Type
(Rtyp
) then
7545 -- Non-binary case, we call the special exponentiation routine for
7546 -- the non-binary case, converting the argument to Long_Long_Integer
7547 -- and passing the modulus value. Then the result is converted back
7548 -- to the base type.
7550 if Non_Binary_Modulus
(Rtyp
) then
7553 Make_Function_Call
(Loc
,
7554 Name
=> New_Reference_To
(RTE
(RE_Exp_Modular
), Loc
),
7555 Parameter_Associations
=> New_List
(
7556 Convert_To
(Standard_Integer
, Base
),
7557 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
)),
7560 -- Binary case, in this case, we call one of two routines, either the
7561 -- unsigned integer case, or the unsigned long long integer case,
7562 -- with a final "and" operation to do the required mod.
7565 if UI_To_Int
(Esize
(Rtyp
)) <= Standard_Integer_Size
then
7566 Ent
:= RTE
(RE_Exp_Unsigned
);
7568 Ent
:= RTE
(RE_Exp_Long_Long_Unsigned
);
7575 Make_Function_Call
(Loc
,
7576 Name
=> New_Reference_To
(Ent
, Loc
),
7577 Parameter_Associations
=> New_List
(
7578 Convert_To
(Etype
(First_Formal
(Ent
)), Base
),
7581 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
) - 1))));
7585 -- Common exit point for modular type case
7587 Analyze_And_Resolve
(N
, Typ
);
7590 -- Signed integer cases, done using either Integer or Long_Long_Integer.
7591 -- It is not worth having routines for Short_[Short_]Integer, since for
7592 -- most machines it would not help, and it would generate more code that
7593 -- might need certification when a certified run time is required.
7595 -- In the integer cases, we have two routines, one for when overflow
7596 -- checks are required, and one when they are not required, since there
7597 -- is a real gain in omitting checks on many machines.
7599 elsif Rtyp
= Base_Type
(Standard_Long_Long_Integer
)
7600 or else (Rtyp
= Base_Type
(Standard_Long_Integer
)
7602 Esize
(Standard_Long_Integer
) > Esize
(Standard_Integer
))
7603 or else (Rtyp
= Universal_Integer
)
7605 Etyp
:= Standard_Long_Long_Integer
;
7608 Rent
:= RE_Exp_Long_Long_Integer
;
7610 Rent
:= RE_Exn_Long_Long_Integer
;
7613 elsif Is_Signed_Integer_Type
(Rtyp
) then
7614 Etyp
:= Standard_Integer
;
7617 Rent
:= RE_Exp_Integer
;
7619 Rent
:= RE_Exn_Integer
;
7622 -- Floating-point cases, always done using Long_Long_Float. We do not
7623 -- need separate routines for the overflow case here, since in the case
7624 -- of floating-point, we generate infinities anyway as a rule (either
7625 -- that or we automatically trap overflow), and if there is an infinity
7626 -- generated and a range check is required, the check will fail anyway.
7629 pragma Assert
(Is_Floating_Point_Type
(Rtyp
));
7630 Etyp
:= Standard_Long_Long_Float
;
7631 Rent
:= RE_Exn_Long_Long_Float
;
7634 -- Common processing for integer cases and floating-point cases.
7635 -- If we are in the right type, we can call runtime routine directly
7638 and then Rtyp
/= Universal_Integer
7639 and then Rtyp
/= Universal_Real
7642 Make_Function_Call
(Loc
,
7643 Name
=> New_Reference_To
(RTE
(Rent
), Loc
),
7644 Parameter_Associations
=> New_List
(Base
, Exp
)));
7646 -- Otherwise we have to introduce conversions (conversions are also
7647 -- required in the universal cases, since the runtime routine is
7648 -- typed using one of the standard types).
7653 Make_Function_Call
(Loc
,
7654 Name
=> New_Reference_To
(RTE
(Rent
), Loc
),
7655 Parameter_Associations
=> New_List
(
7656 Convert_To
(Etyp
, Base
),
7660 Analyze_And_Resolve
(N
, Typ
);
7664 when RE_Not_Available
=>
7666 end Expand_N_Op_Expon
;
7668 --------------------
7669 -- Expand_N_Op_Ge --
7670 --------------------
7672 procedure Expand_N_Op_Ge
(N
: Node_Id
) is
7673 Typ
: constant Entity_Id
:= Etype
(N
);
7674 Op1
: constant Node_Id
:= Left_Opnd
(N
);
7675 Op2
: constant Node_Id
:= Right_Opnd
(N
);
7676 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
7679 Binary_Op_Validity_Checks
(N
);
7681 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7682 -- means we no longer have a comparison operation, we are all done.
7684 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
7686 if Nkind
(N
) /= N_Op_Ge
then
7692 if Is_Array_Type
(Typ1
) then
7693 Expand_Array_Comparison
(N
);
7697 -- Deal with boolean operands
7699 if Is_Boolean_Type
(Typ1
) then
7700 Adjust_Condition
(Op1
);
7701 Adjust_Condition
(Op2
);
7702 Set_Etype
(N
, Standard_Boolean
);
7703 Adjust_Result_Type
(N
, Typ
);
7706 Rewrite_Comparison
(N
);
7708 -- If we still have comparison, and Vax_Float type, process it
7710 if Vax_Float
(Typ1
) and then Nkind
(N
) in N_Op_Compare
then
7711 Expand_Vax_Comparison
(N
);
7715 Optimize_Length_Comparison
(N
);
7718 --------------------
7719 -- Expand_N_Op_Gt --
7720 --------------------
7722 procedure Expand_N_Op_Gt
(N
: Node_Id
) is
7723 Typ
: constant Entity_Id
:= Etype
(N
);
7724 Op1
: constant Node_Id
:= Left_Opnd
(N
);
7725 Op2
: constant Node_Id
:= Right_Opnd
(N
);
7726 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
7729 Binary_Op_Validity_Checks
(N
);
7731 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7732 -- means we no longer have a comparison operation, we are all done.
7734 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
7736 if Nkind
(N
) /= N_Op_Gt
then
7740 -- Deal with array type operands
7742 if Is_Array_Type
(Typ1
) then
7743 Expand_Array_Comparison
(N
);
7747 -- Deal with boolean type operands
7749 if Is_Boolean_Type
(Typ1
) then
7750 Adjust_Condition
(Op1
);
7751 Adjust_Condition
(Op2
);
7752 Set_Etype
(N
, Standard_Boolean
);
7753 Adjust_Result_Type
(N
, Typ
);
7756 Rewrite_Comparison
(N
);
7758 -- If we still have comparison, and Vax_Float type, process it
7760 if Vax_Float
(Typ1
) and then Nkind
(N
) in N_Op_Compare
then
7761 Expand_Vax_Comparison
(N
);
7765 Optimize_Length_Comparison
(N
);
7768 --------------------
7769 -- Expand_N_Op_Le --
7770 --------------------
7772 procedure Expand_N_Op_Le
(N
: Node_Id
) is
7773 Typ
: constant Entity_Id
:= Etype
(N
);
7774 Op1
: constant Node_Id
:= Left_Opnd
(N
);
7775 Op2
: constant Node_Id
:= Right_Opnd
(N
);
7776 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
7779 Binary_Op_Validity_Checks
(N
);
7781 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7782 -- means we no longer have a comparison operation, we are all done.
7784 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
7786 if Nkind
(N
) /= N_Op_Le
then
7790 -- Deal with array type operands
7792 if Is_Array_Type
(Typ1
) then
7793 Expand_Array_Comparison
(N
);
7797 -- Deal with Boolean type operands
7799 if Is_Boolean_Type
(Typ1
) then
7800 Adjust_Condition
(Op1
);
7801 Adjust_Condition
(Op2
);
7802 Set_Etype
(N
, Standard_Boolean
);
7803 Adjust_Result_Type
(N
, Typ
);
7806 Rewrite_Comparison
(N
);
7808 -- If we still have comparison, and Vax_Float type, process it
7810 if Vax_Float
(Typ1
) and then Nkind
(N
) in N_Op_Compare
then
7811 Expand_Vax_Comparison
(N
);
7815 Optimize_Length_Comparison
(N
);
7818 --------------------
7819 -- Expand_N_Op_Lt --
7820 --------------------
7822 procedure Expand_N_Op_Lt
(N
: Node_Id
) is
7823 Typ
: constant Entity_Id
:= Etype
(N
);
7824 Op1
: constant Node_Id
:= Left_Opnd
(N
);
7825 Op2
: constant Node_Id
:= Right_Opnd
(N
);
7826 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
7829 Binary_Op_Validity_Checks
(N
);
7831 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7832 -- means we no longer have a comparison operation, we are all done.
7834 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
7836 if Nkind
(N
) /= N_Op_Lt
then
7840 -- Deal with array type operands
7842 if Is_Array_Type
(Typ1
) then
7843 Expand_Array_Comparison
(N
);
7847 -- Deal with Boolean type operands
7849 if Is_Boolean_Type
(Typ1
) then
7850 Adjust_Condition
(Op1
);
7851 Adjust_Condition
(Op2
);
7852 Set_Etype
(N
, Standard_Boolean
);
7853 Adjust_Result_Type
(N
, Typ
);
7856 Rewrite_Comparison
(N
);
7858 -- If we still have comparison, and Vax_Float type, process it
7860 if Vax_Float
(Typ1
) and then Nkind
(N
) in N_Op_Compare
then
7861 Expand_Vax_Comparison
(N
);
7865 Optimize_Length_Comparison
(N
);
7868 -----------------------
7869 -- Expand_N_Op_Minus --
7870 -----------------------
7872 procedure Expand_N_Op_Minus
(N
: Node_Id
) is
7873 Loc
: constant Source_Ptr
:= Sloc
(N
);
7874 Typ
: constant Entity_Id
:= Etype
(N
);
7877 Unary_Op_Validity_Checks
(N
);
7879 -- Check for MINIMIZED/ELIMINATED overflow mode
7881 if Minimized_Eliminated_Overflow_Check
(N
) then
7882 Apply_Arithmetic_Overflow_Check
(N
);
7886 if not Backend_Overflow_Checks_On_Target
7887 and then Is_Signed_Integer_Type
(Etype
(N
))
7888 and then Do_Overflow_Check
(N
)
7890 -- Software overflow checking expands -expr into (0 - expr)
7893 Make_Op_Subtract
(Loc
,
7894 Left_Opnd
=> Make_Integer_Literal
(Loc
, 0),
7895 Right_Opnd
=> Right_Opnd
(N
)));
7897 Analyze_And_Resolve
(N
, Typ
);
7899 -- Vax floating-point types case
7901 elsif Vax_Float
(Etype
(N
)) then
7902 Expand_Vax_Arith
(N
);
7904 end Expand_N_Op_Minus
;
7906 ---------------------
7907 -- Expand_N_Op_Mod --
7908 ---------------------
7910 procedure Expand_N_Op_Mod
(N
: Node_Id
) is
7911 Loc
: constant Source_Ptr
:= Sloc
(N
);
7912 Typ
: constant Entity_Id
:= Etype
(N
);
7913 DOC
: constant Boolean := Do_Overflow_Check
(N
);
7914 DDC
: constant Boolean := Do_Division_Check
(N
);
7927 pragma Warnings
(Off
, Lhi
);
7930 Binary_Op_Validity_Checks
(N
);
7932 -- Check for MINIMIZED/ELIMINATED overflow mode
7934 if Minimized_Eliminated_Overflow_Check
(N
) then
7935 Apply_Arithmetic_Overflow_Check
(N
);
7939 if Is_Integer_Type
(Etype
(N
)) then
7940 Apply_Divide_Checks
(N
);
7942 -- All done if we don't have a MOD any more, which can happen as a
7943 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
7945 if Nkind
(N
) /= N_Op_Mod
then
7950 -- Proceed with expansion of mod operator
7952 Left
:= Left_Opnd
(N
);
7953 Right
:= Right_Opnd
(N
);
7955 Determine_Range
(Right
, ROK
, Rlo
, Rhi
, Assume_Valid
=> True);
7956 Determine_Range
(Left
, LOK
, Llo
, Lhi
, Assume_Valid
=> True);
7958 -- Convert mod to rem if operands are known non-negative. We do this
7959 -- since it is quite likely that this will improve the quality of code,
7960 -- (the operation now corresponds to the hardware remainder), and it
7961 -- does not seem likely that it could be harmful.
7963 if LOK
and then Llo
>= 0
7965 ROK
and then Rlo
>= 0
7968 Make_Op_Rem
(Sloc
(N
),
7969 Left_Opnd
=> Left_Opnd
(N
),
7970 Right_Opnd
=> Right_Opnd
(N
)));
7972 -- Instead of reanalyzing the node we do the analysis manually. This
7973 -- avoids anomalies when the replacement is done in an instance and
7974 -- is epsilon more efficient.
7976 Set_Entity
(N
, Standard_Entity
(S_Op_Rem
));
7978 Set_Do_Overflow_Check
(N
, DOC
);
7979 Set_Do_Division_Check
(N
, DDC
);
7980 Expand_N_Op_Rem
(N
);
7983 -- Otherwise, normal mod processing
7986 -- Apply optimization x mod 1 = 0. We don't really need that with
7987 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
7988 -- certainly harmless.
7990 if Is_Integer_Type
(Etype
(N
))
7991 and then Compile_Time_Known_Value
(Right
)
7992 and then Expr_Value
(Right
) = Uint_1
7994 -- Call Remove_Side_Effects to ensure that any side effects in
7995 -- the ignored left operand (in particular function calls to
7996 -- user defined functions) are properly executed.
7998 Remove_Side_Effects
(Left
);
8000 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
8001 Analyze_And_Resolve
(N
, Typ
);
8005 -- Deal with annoying case of largest negative number remainder
8006 -- minus one. Gigi does not handle this case correctly, because
8007 -- it generates a divide instruction which may trap in this case.
8009 -- In fact the check is quite easy, if the right operand is -1, then
8010 -- the mod value is always 0, and we can just ignore the left operand
8011 -- completely in this case.
8013 -- This only applies if we still have a mod operator. Skip if we
8014 -- have already rewritten this (e.g. in the case of eliminated
8015 -- overflow checks which have driven us into bignum mode).
8017 if Nkind
(N
) = N_Op_Mod
then
8019 -- The operand type may be private (e.g. in the expansion of an
8020 -- intrinsic operation) so we must use the underlying type to get
8021 -- the bounds, and convert the literals explicitly.
8025 (Type_Low_Bound
(Base_Type
(Underlying_Type
(Etype
(Left
)))));
8027 if ((not ROK
) or else (Rlo
<= (-1) and then (-1) <= Rhi
))
8029 ((not LOK
) or else (Llo
= LLB
))
8032 Make_If_Expression
(Loc
,
8033 Expressions
=> New_List
(
8035 Left_Opnd
=> Duplicate_Subexpr
(Right
),
8037 Unchecked_Convert_To
(Typ
,
8038 Make_Integer_Literal
(Loc
, -1))),
8039 Unchecked_Convert_To
(Typ
,
8040 Make_Integer_Literal
(Loc
, Uint_0
)),
8041 Relocate_Node
(N
))));
8043 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
8044 Analyze_And_Resolve
(N
, Typ
);
8048 end Expand_N_Op_Mod
;
8050 --------------------------
8051 -- Expand_N_Op_Multiply --
8052 --------------------------
8054 procedure Expand_N_Op_Multiply
(N
: Node_Id
) is
8055 Loc
: constant Source_Ptr
:= Sloc
(N
);
8056 Lop
: constant Node_Id
:= Left_Opnd
(N
);
8057 Rop
: constant Node_Id
:= Right_Opnd
(N
);
8059 Lp2
: constant Boolean :=
8060 Nkind
(Lop
) = N_Op_Expon
8061 and then Is_Power_Of_2_For_Shift
(Lop
);
8063 Rp2
: constant Boolean :=
8064 Nkind
(Rop
) = N_Op_Expon
8065 and then Is_Power_Of_2_For_Shift
(Rop
);
8067 Ltyp
: constant Entity_Id
:= Etype
(Lop
);
8068 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
8069 Typ
: Entity_Id
:= Etype
(N
);
8072 Binary_Op_Validity_Checks
(N
);
8074 -- Check for MINIMIZED/ELIMINATED overflow mode
8076 if Minimized_Eliminated_Overflow_Check
(N
) then
8077 Apply_Arithmetic_Overflow_Check
(N
);
8081 -- Special optimizations for integer types
8083 if Is_Integer_Type
(Typ
) then
8085 -- N * 0 = 0 for integer types
8087 if Compile_Time_Known_Value
(Rop
)
8088 and then Expr_Value
(Rop
) = Uint_0
8090 -- Call Remove_Side_Effects to ensure that any side effects in
8091 -- the ignored left operand (in particular function calls to
8092 -- user defined functions) are properly executed.
8094 Remove_Side_Effects
(Lop
);
8096 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
8097 Analyze_And_Resolve
(N
, Typ
);
8101 -- Similar handling for 0 * N = 0
8103 if Compile_Time_Known_Value
(Lop
)
8104 and then Expr_Value
(Lop
) = Uint_0
8106 Remove_Side_Effects
(Rop
);
8107 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
8108 Analyze_And_Resolve
(N
, Typ
);
8112 -- N * 1 = 1 * N = N for integer types
8114 -- This optimisation is not done if we are going to
8115 -- rewrite the product 1 * 2 ** N to a shift.
8117 if Compile_Time_Known_Value
(Rop
)
8118 and then Expr_Value
(Rop
) = Uint_1
8124 elsif Compile_Time_Known_Value
(Lop
)
8125 and then Expr_Value
(Lop
) = Uint_1
8133 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
8134 -- Is_Power_Of_2_For_Shift is set means that we know that our left
8135 -- operand is an integer, as required for this to work.
8140 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
8144 Left_Opnd
=> Make_Integer_Literal
(Loc
, 2),
8147 Left_Opnd
=> Right_Opnd
(Lop
),
8148 Right_Opnd
=> Right_Opnd
(Rop
))));
8149 Analyze_And_Resolve
(N
, Typ
);
8154 Make_Op_Shift_Left
(Loc
,
8157 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))));
8158 Analyze_And_Resolve
(N
, Typ
);
8162 -- Same processing for the operands the other way round
8166 Make_Op_Shift_Left
(Loc
,
8169 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))));
8170 Analyze_And_Resolve
(N
, Typ
);
8174 -- Do required fixup of universal fixed operation
8176 if Typ
= Universal_Fixed
then
8177 Fixup_Universal_Fixed_Operation
(N
);
8181 -- Multiplications with fixed-point results
8183 if Is_Fixed_Point_Type
(Typ
) then
8185 -- No special processing if Treat_Fixed_As_Integer is set, since from
8186 -- a semantic point of view such operations are simply integer
8187 -- operations and will be treated that way.
8189 if not Treat_Fixed_As_Integer
(N
) then
8191 -- Case of fixed * integer => fixed
8193 if Is_Integer_Type
(Rtyp
) then
8194 Expand_Multiply_Fixed_By_Integer_Giving_Fixed
(N
);
8196 -- Case of integer * fixed => fixed
8198 elsif Is_Integer_Type
(Ltyp
) then
8199 Expand_Multiply_Integer_By_Fixed_Giving_Fixed
(N
);
8201 -- Case of fixed * fixed => fixed
8204 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed
(N
);
8208 -- Other cases of multiplication of fixed-point operands. Again we
8209 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
8211 elsif (Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
))
8212 and then not Treat_Fixed_As_Integer
(N
)
8214 if Is_Integer_Type
(Typ
) then
8215 Expand_Multiply_Fixed_By_Fixed_Giving_Integer
(N
);
8217 pragma Assert
(Is_Floating_Point_Type
(Typ
));
8218 Expand_Multiply_Fixed_By_Fixed_Giving_Float
(N
);
8221 -- Mixed-mode operations can appear in a non-static universal context,
8222 -- in which case the integer argument must be converted explicitly.
8224 elsif Typ
= Universal_Real
8225 and then Is_Integer_Type
(Rtyp
)
8227 Rewrite
(Rop
, Convert_To
(Universal_Real
, Relocate_Node
(Rop
)));
8229 Analyze_And_Resolve
(Rop
, Universal_Real
);
8231 elsif Typ
= Universal_Real
8232 and then Is_Integer_Type
(Ltyp
)
8234 Rewrite
(Lop
, Convert_To
(Universal_Real
, Relocate_Node
(Lop
)));
8236 Analyze_And_Resolve
(Lop
, Universal_Real
);
8238 -- Non-fixed point cases, check software overflow checking required
8240 elsif Is_Signed_Integer_Type
(Etype
(N
)) then
8241 Apply_Arithmetic_Overflow_Check
(N
);
8243 -- Deal with VAX float case
8245 elsif Vax_Float
(Typ
) then
8246 Expand_Vax_Arith
(N
);
8249 end Expand_N_Op_Multiply
;
8251 --------------------
8252 -- Expand_N_Op_Ne --
8253 --------------------
8255 procedure Expand_N_Op_Ne
(N
: Node_Id
) is
8256 Typ
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
8259 -- Case of elementary type with standard operator
8261 if Is_Elementary_Type
(Typ
)
8262 and then Sloc
(Entity
(N
)) = Standard_Location
8264 Binary_Op_Validity_Checks
(N
);
8266 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
8267 -- means we no longer have a /= operation, we are all done.
8269 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8271 if Nkind
(N
) /= N_Op_Ne
then
8275 -- Boolean types (requiring handling of non-standard case)
8277 if Is_Boolean_Type
(Typ
) then
8278 Adjust_Condition
(Left_Opnd
(N
));
8279 Adjust_Condition
(Right_Opnd
(N
));
8280 Set_Etype
(N
, Standard_Boolean
);
8281 Adjust_Result_Type
(N
, Typ
);
8284 Rewrite_Comparison
(N
);
8286 -- If we still have comparison for Vax_Float, process it
8288 if Vax_Float
(Typ
) and then Nkind
(N
) in N_Op_Compare
then
8289 Expand_Vax_Comparison
(N
);
8293 -- For all cases other than elementary types, we rewrite node as the
8294 -- negation of an equality operation, and reanalyze. The equality to be
8295 -- used is defined in the same scope and has the same signature. This
8296 -- signature must be set explicitly since in an instance it may not have
8297 -- the same visibility as in the generic unit. This avoids duplicating
8298 -- or factoring the complex code for record/array equality tests etc.
8302 Loc
: constant Source_Ptr
:= Sloc
(N
);
8304 Ne
: constant Entity_Id
:= Entity
(N
);
8307 Binary_Op_Validity_Checks
(N
);
8313 Left_Opnd
=> Left_Opnd
(N
),
8314 Right_Opnd
=> Right_Opnd
(N
)));
8315 Set_Paren_Count
(Right_Opnd
(Neg
), 1);
8317 if Scope
(Ne
) /= Standard_Standard
then
8318 Set_Entity
(Right_Opnd
(Neg
), Corresponding_Equality
(Ne
));
8321 -- For navigation purposes, we want to treat the inequality as an
8322 -- implicit reference to the corresponding equality. Preserve the
8323 -- Comes_From_ source flag to generate proper Xref entries.
8325 Preserve_Comes_From_Source
(Neg
, N
);
8326 Preserve_Comes_From_Source
(Right_Opnd
(Neg
), N
);
8328 Analyze_And_Resolve
(N
, Standard_Boolean
);
8332 Optimize_Length_Comparison
(N
);
8335 ---------------------
8336 -- Expand_N_Op_Not --
8337 ---------------------
8339 -- If the argument is other than a Boolean array type, there is no special
8340 -- expansion required, except for VMS operations on signed integers.
8342 -- For the packed case, we call the special routine in Exp_Pakd, except
8343 -- that if the component size is greater than one, we use the standard
8344 -- routine generating a gruesome loop (it is so peculiar to have packed
8345 -- arrays with non-standard Boolean representations anyway, so it does not
8346 -- matter that we do not handle this case efficiently).
8348 -- For the unpacked case (and for the special packed case where we have non
8349 -- standard Booleans, as discussed above), we generate and insert into the
8350 -- tree the following function definition:
8352 -- function Nnnn (A : arr) is
8355 -- for J in a'range loop
8356 -- B (J) := not A (J);
8361 -- Here arr is the actual subtype of the parameter (and hence always
8362 -- constrained). Then we replace the not with a call to this function.
8364 procedure Expand_N_Op_Not
(N
: Node_Id
) is
8365 Loc
: constant Source_Ptr
:= Sloc
(N
);
8366 Typ
: constant Entity_Id
:= Etype
(N
);
8375 Func_Name
: Entity_Id
;
8376 Loop_Statement
: Node_Id
;
8379 Unary_Op_Validity_Checks
(N
);
8381 -- For boolean operand, deal with non-standard booleans
8383 if Is_Boolean_Type
(Typ
) then
8384 Adjust_Condition
(Right_Opnd
(N
));
8385 Set_Etype
(N
, Standard_Boolean
);
8386 Adjust_Result_Type
(N
, Typ
);
8390 -- For the VMS "not" on signed integer types, use conversion to and from
8391 -- a predefined modular type.
8393 if Is_VMS_Operator
(Entity
(N
)) then
8399 -- If this is a derived type, retrieve original VMS type so that
8400 -- the proper sized type is used for intermediate values.
8402 if Is_Derived_Type
(Typ
) then
8403 Rtyp
:= First_Subtype
(Etype
(Typ
));
8408 -- The proper unsigned type must have a size compatible with the
8409 -- operand, to prevent misalignment.
8411 if RM_Size
(Rtyp
) <= 8 then
8412 Utyp
:= RTE
(RE_Unsigned_8
);
8414 elsif RM_Size
(Rtyp
) <= 16 then
8415 Utyp
:= RTE
(RE_Unsigned_16
);
8417 elsif RM_Size
(Rtyp
) = RM_Size
(Standard_Unsigned
) then
8418 Utyp
:= RTE
(RE_Unsigned_32
);
8421 Utyp
:= RTE
(RE_Long_Long_Unsigned
);
8425 Unchecked_Convert_To
(Typ
,
8427 Unchecked_Convert_To
(Utyp
, Right_Opnd
(N
)))));
8428 Analyze_And_Resolve
(N
, Typ
);
8433 -- Only array types need any other processing
8435 if not Is_Array_Type
(Typ
) then
8439 -- Case of array operand. If bit packed with a component size of 1,
8440 -- handle it in Exp_Pakd if the operand is known to be aligned.
8442 if Is_Bit_Packed_Array
(Typ
)
8443 and then Component_Size
(Typ
) = 1
8444 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
8446 Expand_Packed_Not
(N
);
8450 -- Case of array operand which is not bit-packed. If the context is
8451 -- a safe assignment, call in-place operation, If context is a larger
8452 -- boolean expression in the context of a safe assignment, expansion is
8453 -- done by enclosing operation.
8455 Opnd
:= Relocate_Node
(Right_Opnd
(N
));
8456 Convert_To_Actual_Subtype
(Opnd
);
8457 Arr
:= Etype
(Opnd
);
8458 Ensure_Defined
(Arr
, N
);
8459 Silly_Boolean_Array_Not_Test
(N
, Arr
);
8461 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
8462 if Safe_In_Place_Array_Op
(Name
(Parent
(N
)), N
, Empty
) then
8463 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
8466 -- Special case the negation of a binary operation
8468 elsif Nkind_In
(Opnd
, N_Op_And
, N_Op_Or
, N_Op_Xor
)
8469 and then Safe_In_Place_Array_Op
8470 (Name
(Parent
(N
)), Left_Opnd
(Opnd
), Right_Opnd
(Opnd
))
8472 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
8476 elsif Nkind
(Parent
(N
)) in N_Binary_Op
8477 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
8480 Op1
: constant Node_Id
:= Left_Opnd
(Parent
(N
));
8481 Op2
: constant Node_Id
:= Right_Opnd
(Parent
(N
));
8482 Lhs
: constant Node_Id
:= Name
(Parent
(Parent
(N
)));
8485 if Safe_In_Place_Array_Op
(Lhs
, Op1
, Op2
) then
8487 -- (not A) op (not B) can be reduced to a single call
8489 if N
= Op1
and then Nkind
(Op2
) = N_Op_Not
then
8492 elsif N
= Op2
and then Nkind
(Op1
) = N_Op_Not
then
8495 -- A xor (not B) can also be special-cased
8497 elsif N
= Op2
and then Nkind
(Parent
(N
)) = N_Op_Xor
then
8504 A
:= Make_Defining_Identifier
(Loc
, Name_uA
);
8505 B
:= Make_Defining_Identifier
(Loc
, Name_uB
);
8506 J
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
8509 Make_Indexed_Component
(Loc
,
8510 Prefix
=> New_Reference_To
(A
, Loc
),
8511 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
8514 Make_Indexed_Component
(Loc
,
8515 Prefix
=> New_Reference_To
(B
, Loc
),
8516 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
8519 Make_Implicit_Loop_Statement
(N
,
8520 Identifier
=> Empty
,
8523 Make_Iteration_Scheme
(Loc
,
8524 Loop_Parameter_Specification
=>
8525 Make_Loop_Parameter_Specification
(Loc
,
8526 Defining_Identifier
=> J
,
8527 Discrete_Subtype_Definition
=>
8528 Make_Attribute_Reference
(Loc
,
8529 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
8530 Attribute_Name
=> Name_Range
))),
8532 Statements
=> New_List
(
8533 Make_Assignment_Statement
(Loc
,
8535 Expression
=> Make_Op_Not
(Loc
, A_J
))));
8537 Func_Name
:= Make_Temporary
(Loc
, 'N');
8538 Set_Is_Inlined
(Func_Name
);
8541 Make_Subprogram_Body
(Loc
,
8543 Make_Function_Specification
(Loc
,
8544 Defining_Unit_Name
=> Func_Name
,
8545 Parameter_Specifications
=> New_List
(
8546 Make_Parameter_Specification
(Loc
,
8547 Defining_Identifier
=> A
,
8548 Parameter_Type
=> New_Reference_To
(Typ
, Loc
))),
8549 Result_Definition
=> New_Reference_To
(Typ
, Loc
)),
8551 Declarations
=> New_List
(
8552 Make_Object_Declaration
(Loc
,
8553 Defining_Identifier
=> B
,
8554 Object_Definition
=> New_Reference_To
(Arr
, Loc
))),
8556 Handled_Statement_Sequence
=>
8557 Make_Handled_Sequence_Of_Statements
(Loc
,
8558 Statements
=> New_List
(
8560 Make_Simple_Return_Statement
(Loc
,
8561 Expression
=> Make_Identifier
(Loc
, Chars
(B
)))))));
8564 Make_Function_Call
(Loc
,
8565 Name
=> New_Reference_To
(Func_Name
, Loc
),
8566 Parameter_Associations
=> New_List
(Opnd
)));
8568 Analyze_And_Resolve
(N
, Typ
);
8569 end Expand_N_Op_Not
;
8571 --------------------
8572 -- Expand_N_Op_Or --
8573 --------------------
8575 procedure Expand_N_Op_Or
(N
: Node_Id
) is
8576 Typ
: constant Entity_Id
:= Etype
(N
);
8579 Binary_Op_Validity_Checks
(N
);
8581 if Is_Array_Type
(Etype
(N
)) then
8582 Expand_Boolean_Operator
(N
);
8584 elsif Is_Boolean_Type
(Etype
(N
)) then
8585 Adjust_Condition
(Left_Opnd
(N
));
8586 Adjust_Condition
(Right_Opnd
(N
));
8587 Set_Etype
(N
, Standard_Boolean
);
8588 Adjust_Result_Type
(N
, Typ
);
8590 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
8591 Expand_Intrinsic_Call
(N
, Entity
(N
));
8596 ----------------------
8597 -- Expand_N_Op_Plus --
8598 ----------------------
8600 procedure Expand_N_Op_Plus
(N
: Node_Id
) is
8602 Unary_Op_Validity_Checks
(N
);
8604 -- Check for MINIMIZED/ELIMINATED overflow mode
8606 if Minimized_Eliminated_Overflow_Check
(N
) then
8607 Apply_Arithmetic_Overflow_Check
(N
);
8610 end Expand_N_Op_Plus
;
8612 ---------------------
8613 -- Expand_N_Op_Rem --
8614 ---------------------
8616 procedure Expand_N_Op_Rem
(N
: Node_Id
) is
8617 Loc
: constant Source_Ptr
:= Sloc
(N
);
8618 Typ
: constant Entity_Id
:= Etype
(N
);
8629 -- Set if corresponding operand can be negative
8631 pragma Unreferenced
(Hi
);
8634 Binary_Op_Validity_Checks
(N
);
8636 -- Check for MINIMIZED/ELIMINATED overflow mode
8638 if Minimized_Eliminated_Overflow_Check
(N
) then
8639 Apply_Arithmetic_Overflow_Check
(N
);
8643 if Is_Integer_Type
(Etype
(N
)) then
8644 Apply_Divide_Checks
(N
);
8646 -- All done if we don't have a REM any more, which can happen as a
8647 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8649 if Nkind
(N
) /= N_Op_Rem
then
8654 -- Proceed with expansion of REM
8656 Left
:= Left_Opnd
(N
);
8657 Right
:= Right_Opnd
(N
);
8659 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
8660 -- but it is useful with other back ends (e.g. AAMP), and is certainly
8663 if Is_Integer_Type
(Etype
(N
))
8664 and then Compile_Time_Known_Value
(Right
)
8665 and then Expr_Value
(Right
) = Uint_1
8667 -- Call Remove_Side_Effects to ensure that any side effects in the
8668 -- ignored left operand (in particular function calls to user defined
8669 -- functions) are properly executed.
8671 Remove_Side_Effects
(Left
);
8673 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
8674 Analyze_And_Resolve
(N
, Typ
);
8678 -- Deal with annoying case of largest negative number remainder minus
8679 -- one. Gigi does not handle this case correctly, because it generates
8680 -- a divide instruction which may trap in this case.
8682 -- In fact the check is quite easy, if the right operand is -1, then
8683 -- the remainder is always 0, and we can just ignore the left operand
8684 -- completely in this case.
8686 Determine_Range
(Right
, OK
, Lo
, Hi
, Assume_Valid
=> True);
8687 Lneg
:= (not OK
) or else Lo
< 0;
8689 Determine_Range
(Left
, OK
, Lo
, Hi
, Assume_Valid
=> True);
8690 Rneg
:= (not OK
) or else Lo
< 0;
8692 -- We won't mess with trying to find out if the left operand can really
8693 -- be the largest negative number (that's a pain in the case of private
8694 -- types and this is really marginal). We will just assume that we need
8695 -- the test if the left operand can be negative at all.
8697 if Lneg
and Rneg
then
8699 Make_If_Expression
(Loc
,
8700 Expressions
=> New_List
(
8702 Left_Opnd
=> Duplicate_Subexpr
(Right
),
8704 Unchecked_Convert_To
(Typ
, Make_Integer_Literal
(Loc
, -1))),
8706 Unchecked_Convert_To
(Typ
,
8707 Make_Integer_Literal
(Loc
, Uint_0
)),
8709 Relocate_Node
(N
))));
8711 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
8712 Analyze_And_Resolve
(N
, Typ
);
8714 end Expand_N_Op_Rem
;
8716 -----------------------------
8717 -- Expand_N_Op_Rotate_Left --
8718 -----------------------------
8720 procedure Expand_N_Op_Rotate_Left
(N
: Node_Id
) is
8722 Binary_Op_Validity_Checks
(N
);
8723 end Expand_N_Op_Rotate_Left
;
8725 ------------------------------
8726 -- Expand_N_Op_Rotate_Right --
8727 ------------------------------
8729 procedure Expand_N_Op_Rotate_Right
(N
: Node_Id
) is
8731 Binary_Op_Validity_Checks
(N
);
8732 end Expand_N_Op_Rotate_Right
;
8734 ----------------------------
8735 -- Expand_N_Op_Shift_Left --
8736 ----------------------------
8738 procedure Expand_N_Op_Shift_Left
(N
: Node_Id
) is
8740 Binary_Op_Validity_Checks
(N
);
8741 end Expand_N_Op_Shift_Left
;
8743 -----------------------------
8744 -- Expand_N_Op_Shift_Right --
8745 -----------------------------
8747 procedure Expand_N_Op_Shift_Right
(N
: Node_Id
) is
8749 Binary_Op_Validity_Checks
(N
);
8750 end Expand_N_Op_Shift_Right
;
8752 ----------------------------------------
8753 -- Expand_N_Op_Shift_Right_Arithmetic --
8754 ----------------------------------------
8756 procedure Expand_N_Op_Shift_Right_Arithmetic
(N
: Node_Id
) is
8758 Binary_Op_Validity_Checks
(N
);
8759 end Expand_N_Op_Shift_Right_Arithmetic
;
8761 --------------------------
8762 -- Expand_N_Op_Subtract --
8763 --------------------------
8765 procedure Expand_N_Op_Subtract
(N
: Node_Id
) is
8766 Typ
: constant Entity_Id
:= Etype
(N
);
8769 Binary_Op_Validity_Checks
(N
);
8771 -- Check for MINIMIZED/ELIMINATED overflow mode
8773 if Minimized_Eliminated_Overflow_Check
(N
) then
8774 Apply_Arithmetic_Overflow_Check
(N
);
8778 -- N - 0 = N for integer types
8780 if Is_Integer_Type
(Typ
)
8781 and then Compile_Time_Known_Value
(Right_Opnd
(N
))
8782 and then Expr_Value
(Right_Opnd
(N
)) = 0
8784 Rewrite
(N
, Left_Opnd
(N
));
8788 -- Arithmetic overflow checks for signed integer/fixed point types
8790 if Is_Signed_Integer_Type
(Typ
)
8792 Is_Fixed_Point_Type
(Typ
)
8794 Apply_Arithmetic_Overflow_Check
(N
);
8796 -- VAX floating-point types case
8798 elsif Vax_Float
(Typ
) then
8799 Expand_Vax_Arith
(N
);
8801 end Expand_N_Op_Subtract
;
8803 ---------------------
8804 -- Expand_N_Op_Xor --
8805 ---------------------
8807 procedure Expand_N_Op_Xor
(N
: Node_Id
) is
8808 Typ
: constant Entity_Id
:= Etype
(N
);
8811 Binary_Op_Validity_Checks
(N
);
8813 if Is_Array_Type
(Etype
(N
)) then
8814 Expand_Boolean_Operator
(N
);
8816 elsif Is_Boolean_Type
(Etype
(N
)) then
8817 Adjust_Condition
(Left_Opnd
(N
));
8818 Adjust_Condition
(Right_Opnd
(N
));
8819 Set_Etype
(N
, Standard_Boolean
);
8820 Adjust_Result_Type
(N
, Typ
);
8822 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
8823 Expand_Intrinsic_Call
(N
, Entity
(N
));
8826 end Expand_N_Op_Xor
;
8828 ----------------------
8829 -- Expand_N_Or_Else --
8830 ----------------------
8832 procedure Expand_N_Or_Else
(N
: Node_Id
)
8833 renames Expand_Short_Circuit_Operator
;
8835 -----------------------------------
8836 -- Expand_N_Qualified_Expression --
8837 -----------------------------------
8839 procedure Expand_N_Qualified_Expression
(N
: Node_Id
) is
8840 Operand
: constant Node_Id
:= Expression
(N
);
8841 Target_Type
: constant Entity_Id
:= Entity
(Subtype_Mark
(N
));
8844 -- Do validity check if validity checking operands
8846 if Validity_Checks_On
and then Validity_Check_Operands
then
8847 Ensure_Valid
(Operand
);
8850 -- Apply possible constraint check
8852 Apply_Constraint_Check
(Operand
, Target_Type
, No_Sliding
=> True);
8854 if Do_Range_Check
(Operand
) then
8855 Set_Do_Range_Check
(Operand
, False);
8856 Generate_Range_Check
(Operand
, Target_Type
, CE_Range_Check_Failed
);
8858 end Expand_N_Qualified_Expression
;
8860 ------------------------------------
8861 -- Expand_N_Quantified_Expression --
8862 ------------------------------------
8866 -- for all X in range => Cond
8871 -- for X in range loop
8878 -- Similarly, an existentially quantified expression:
8880 -- for some X in range => Cond
8885 -- for X in range loop
8892 -- In both cases, the iteration may be over a container in which case it is
8893 -- given by an iterator specification, not a loop parameter specification.
8895 procedure Expand_N_Quantified_Expression
(N
: Node_Id
) is
8896 Actions
: constant List_Id
:= New_List
;
8897 For_All
: constant Boolean := All_Present
(N
);
8898 Iter_Spec
: constant Node_Id
:= Iterator_Specification
(N
);
8899 Loc
: constant Source_Ptr
:= Sloc
(N
);
8900 Loop_Spec
: constant Node_Id
:= Loop_Parameter_Specification
(N
);
8907 -- Create the declaration of the flag which tracks the status of the
8908 -- quantified expression. Generate:
8910 -- Flag : Boolean := (True | False);
8912 Flag
:= Make_Temporary
(Loc
, 'T', N
);
8915 Make_Object_Declaration
(Loc
,
8916 Defining_Identifier
=> Flag
,
8917 Object_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
),
8919 New_Occurrence_Of
(Boolean_Literals
(For_All
), Loc
)));
8921 -- Construct the circuitry which tracks the status of the quantified
8922 -- expression. Generate:
8924 -- if [not] Cond then
8925 -- Flag := (False | True);
8929 Cond
:= Relocate_Node
(Condition
(N
));
8932 Cond
:= Make_Op_Not
(Loc
, Cond
);
8936 Make_Implicit_If_Statement
(N
,
8938 Then_Statements
=> New_List
(
8939 Make_Assignment_Statement
(Loc
,
8940 Name
=> New_Occurrence_Of
(Flag
, Loc
),
8942 New_Occurrence_Of
(Boolean_Literals
(not For_All
), Loc
)),
8943 Make_Exit_Statement
(Loc
))));
8945 -- Build the loop equivalent of the quantified expression
8947 if Present
(Iter_Spec
) then
8949 Make_Iteration_Scheme
(Loc
,
8950 Iterator_Specification
=> Iter_Spec
);
8953 Make_Iteration_Scheme
(Loc
,
8954 Loop_Parameter_Specification
=> Loop_Spec
);
8958 Make_Loop_Statement
(Loc
,
8959 Iteration_Scheme
=> Scheme
,
8960 Statements
=> Stmts
,
8961 End_Label
=> Empty
));
8963 -- Transform the quantified expression
8966 Make_Expression_With_Actions
(Loc
,
8967 Expression
=> New_Occurrence_Of
(Flag
, Loc
),
8968 Actions
=> Actions
));
8969 Analyze_And_Resolve
(N
, Standard_Boolean
);
8970 end Expand_N_Quantified_Expression
;
8972 ---------------------------------
8973 -- Expand_N_Selected_Component --
8974 ---------------------------------
8976 procedure Expand_N_Selected_Component
(N
: Node_Id
) is
8977 Loc
: constant Source_Ptr
:= Sloc
(N
);
8978 Par
: constant Node_Id
:= Parent
(N
);
8979 P
: constant Node_Id
:= Prefix
(N
);
8980 Ptyp
: Entity_Id
:= Underlying_Type
(Etype
(P
));
8986 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean;
8987 -- Gigi needs a temporary for prefixes that depend on a discriminant,
8988 -- unless the context of an assignment can provide size information.
8989 -- Don't we have a general routine that does this???
8991 function Is_Subtype_Declaration
return Boolean;
8992 -- The replacement of a discriminant reference by its value is required
8993 -- if this is part of the initialization of an temporary generated by a
8994 -- change of representation. This shows up as the construction of a
8995 -- discriminant constraint for a subtype declared at the same point as
8996 -- the entity in the prefix of the selected component. We recognize this
8997 -- case when the context of the reference is:
8998 -- subtype ST is T(Obj.D);
8999 -- where the entity for Obj comes from source, and ST has the same sloc.
9001 -----------------------
9002 -- In_Left_Hand_Side --
9003 -----------------------
9005 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean is
9007 return (Nkind
(Parent
(Comp
)) = N_Assignment_Statement
9008 and then Comp
= Name
(Parent
(Comp
)))
9009 or else (Present
(Parent
(Comp
))
9010 and then Nkind
(Parent
(Comp
)) in N_Subexpr
9011 and then In_Left_Hand_Side
(Parent
(Comp
)));
9012 end In_Left_Hand_Side
;
9014 -----------------------------
9015 -- Is_Subtype_Declaration --
9016 -----------------------------
9018 function Is_Subtype_Declaration
return Boolean is
9019 Par
: constant Node_Id
:= Parent
(N
);
9022 Nkind
(Par
) = N_Index_Or_Discriminant_Constraint
9023 and then Nkind
(Parent
(Parent
(Par
))) = N_Subtype_Declaration
9024 and then Comes_From_Source
(Entity
(Prefix
(N
)))
9025 and then Sloc
(Par
) = Sloc
(Entity
(Prefix
(N
)));
9026 end Is_Subtype_Declaration
;
9028 -- Start of processing for Expand_N_Selected_Component
9031 -- Insert explicit dereference if required
9033 if Is_Access_Type
(Ptyp
) then
9035 -- First set prefix type to proper access type, in case it currently
9036 -- has a private (non-access) view of this type.
9038 Set_Etype
(P
, Ptyp
);
9040 Insert_Explicit_Dereference
(P
);
9041 Analyze_And_Resolve
(P
, Designated_Type
(Ptyp
));
9043 if Ekind
(Etype
(P
)) = E_Private_Subtype
9044 and then Is_For_Access_Subtype
(Etype
(P
))
9046 Set_Etype
(P
, Base_Type
(Etype
(P
)));
9052 -- Deal with discriminant check required
9054 if Do_Discriminant_Check
(N
) then
9056 -- Present the discriminant checking function to the backend, so that
9057 -- it can inline the call to the function.
9060 (Discriminant_Checking_Func
9061 (Original_Record_Component
(Entity
(Selector_Name
(N
)))));
9063 -- Now reset the flag and generate the call
9065 Set_Do_Discriminant_Check
(N
, False);
9066 Generate_Discriminant_Check
(N
);
9069 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9070 -- function, then additional actuals must be passed.
9072 if Ada_Version
>= Ada_2005
9073 and then Is_Build_In_Place_Function_Call
(P
)
9075 Make_Build_In_Place_Call_In_Anonymous_Context
(P
);
9078 -- Gigi cannot handle unchecked conversions that are the prefix of a
9079 -- selected component with discriminants. This must be checked during
9080 -- expansion, because during analysis the type of the selector is not
9081 -- known at the point the prefix is analyzed. If the conversion is the
9082 -- target of an assignment, then we cannot force the evaluation.
9084 if Nkind
(Prefix
(N
)) = N_Unchecked_Type_Conversion
9085 and then Has_Discriminants
(Etype
(N
))
9086 and then not In_Left_Hand_Side
(N
)
9088 Force_Evaluation
(Prefix
(N
));
9091 -- Remaining processing applies only if selector is a discriminant
9093 if Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
then
9095 -- If the selector is a discriminant of a constrained record type,
9096 -- we may be able to rewrite the expression with the actual value
9097 -- of the discriminant, a useful optimization in some cases.
9099 if Is_Record_Type
(Ptyp
)
9100 and then Has_Discriminants
(Ptyp
)
9101 and then Is_Constrained
(Ptyp
)
9103 -- Do this optimization for discrete types only, and not for
9104 -- access types (access discriminants get us into trouble!)
9106 if not Is_Discrete_Type
(Etype
(N
)) then
9109 -- Don't do this on the left hand of an assignment statement.
9110 -- Normally one would think that references like this would not
9111 -- occur, but they do in generated code, and mean that we really
9112 -- do want to assign the discriminant!
9114 elsif Nkind
(Par
) = N_Assignment_Statement
9115 and then Name
(Par
) = N
9119 -- Don't do this optimization for the prefix of an attribute or
9120 -- the name of an object renaming declaration since these are
9121 -- contexts where we do not want the value anyway.
9123 elsif (Nkind
(Par
) = N_Attribute_Reference
9124 and then Prefix
(Par
) = N
)
9125 or else Is_Renamed_Object
(N
)
9129 -- Don't do this optimization if we are within the code for a
9130 -- discriminant check, since the whole point of such a check may
9131 -- be to verify the condition on which the code below depends!
9133 elsif Is_In_Discriminant_Check
(N
) then
9136 -- Green light to see if we can do the optimization. There is
9137 -- still one condition that inhibits the optimization below but
9138 -- now is the time to check the particular discriminant.
9141 -- Loop through discriminants to find the matching discriminant
9142 -- constraint to see if we can copy it.
9144 Disc
:= First_Discriminant
(Ptyp
);
9145 Dcon
:= First_Elmt
(Discriminant_Constraint
(Ptyp
));
9146 Discr_Loop
: while Present
(Dcon
) loop
9147 Dval
:= Node
(Dcon
);
9149 -- Check if this is the matching discriminant and if the
9150 -- discriminant value is simple enough to make sense to
9151 -- copy. We don't want to copy complex expressions, and
9152 -- indeed to do so can cause trouble (before we put in
9153 -- this guard, a discriminant expression containing an
9154 -- AND THEN was copied, causing problems for coverage
9157 -- However, if the reference is part of the initialization
9158 -- code generated for an object declaration, we must use
9159 -- the discriminant value from the subtype constraint,
9160 -- because the selected component may be a reference to the
9161 -- object being initialized, whose discriminant is not yet
9162 -- set. This only happens in complex cases involving changes
9163 -- or representation.
9165 if Disc
= Entity
(Selector_Name
(N
))
9166 and then (Is_Entity_Name
(Dval
)
9167 or else Compile_Time_Known_Value
(Dval
)
9168 or else Is_Subtype_Declaration
)
9170 -- Here we have the matching discriminant. Check for
9171 -- the case of a discriminant of a component that is
9172 -- constrained by an outer discriminant, which cannot
9173 -- be optimized away.
9175 if Denotes_Discriminant
9176 (Dval
, Check_Concurrent
=> True)
9180 elsif Nkind
(Original_Node
(Dval
)) = N_Selected_Component
9182 Denotes_Discriminant
9183 (Selector_Name
(Original_Node
(Dval
)), True)
9187 -- Do not retrieve value if constraint is not static. It
9188 -- is generally not useful, and the constraint may be a
9189 -- rewritten outer discriminant in which case it is in
9192 elsif Is_Entity_Name
(Dval
)
9193 and then Nkind
(Parent
(Entity
(Dval
))) =
9194 N_Object_Declaration
9195 and then Present
(Expression
(Parent
(Entity
(Dval
))))
9197 not Is_Static_Expression
9198 (Expression
(Parent
(Entity
(Dval
))))
9202 -- In the context of a case statement, the expression may
9203 -- have the base type of the discriminant, and we need to
9204 -- preserve the constraint to avoid spurious errors on
9207 elsif Nkind
(Parent
(N
)) = N_Case_Statement
9208 and then Etype
(Dval
) /= Etype
(Disc
)
9211 Make_Qualified_Expression
(Loc
,
9213 New_Occurrence_Of
(Etype
(Disc
), Loc
),
9215 New_Copy_Tree
(Dval
)));
9216 Analyze_And_Resolve
(N
, Etype
(Disc
));
9218 -- In case that comes out as a static expression,
9219 -- reset it (a selected component is never static).
9221 Set_Is_Static_Expression
(N
, False);
9224 -- Otherwise we can just copy the constraint, but the
9225 -- result is certainly not static! In some cases the
9226 -- discriminant constraint has been analyzed in the
9227 -- context of the original subtype indication, but for
9228 -- itypes the constraint might not have been analyzed
9229 -- yet, and this must be done now.
9232 Rewrite
(N
, New_Copy_Tree
(Dval
));
9233 Analyze_And_Resolve
(N
);
9234 Set_Is_Static_Expression
(N
, False);
9240 Next_Discriminant
(Disc
);
9241 end loop Discr_Loop
;
9243 -- Note: the above loop should always find a matching
9244 -- discriminant, but if it does not, we just missed an
9245 -- optimization due to some glitch (perhaps a previous
9246 -- error), so ignore.
9251 -- The only remaining processing is in the case of a discriminant of
9252 -- a concurrent object, where we rewrite the prefix to denote the
9253 -- corresponding record type. If the type is derived and has renamed
9254 -- discriminants, use corresponding discriminant, which is the one
9255 -- that appears in the corresponding record.
9257 if not Is_Concurrent_Type
(Ptyp
) then
9261 Disc
:= Entity
(Selector_Name
(N
));
9263 if Is_Derived_Type
(Ptyp
)
9264 and then Present
(Corresponding_Discriminant
(Disc
))
9266 Disc
:= Corresponding_Discriminant
(Disc
);
9270 Make_Selected_Component
(Loc
,
9272 Unchecked_Convert_To
(Corresponding_Record_Type
(Ptyp
),
9274 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Disc
)));
9280 -- Set Atomic_Sync_Required if necessary for atomic component
9282 if Nkind
(N
) = N_Selected_Component
then
9284 E
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
9288 -- If component is atomic, but type is not, setting depends on
9289 -- disable/enable state for the component.
9291 if Is_Atomic
(E
) and then not Is_Atomic
(Etype
(E
)) then
9292 Set
:= not Atomic_Synchronization_Disabled
(E
);
9294 -- If component is not atomic, but its type is atomic, setting
9295 -- depends on disable/enable state for the type.
9297 elsif not Is_Atomic
(E
) and then Is_Atomic
(Etype
(E
)) then
9298 Set
:= not Atomic_Synchronization_Disabled
(Etype
(E
));
9300 -- If both component and type are atomic, we disable if either
9301 -- component or its type have sync disabled.
9303 elsif Is_Atomic
(E
) and then Is_Atomic
(Etype
(E
)) then
9304 Set
:= (not Atomic_Synchronization_Disabled
(E
))
9306 (not Atomic_Synchronization_Disabled
(Etype
(E
)));
9312 -- Set flag if required
9315 Activate_Atomic_Synchronization
(N
);
9319 end Expand_N_Selected_Component
;
9321 --------------------
9322 -- Expand_N_Slice --
9323 --------------------
9325 procedure Expand_N_Slice
(N
: Node_Id
) is
9326 Loc
: constant Source_Ptr
:= Sloc
(N
);
9327 Typ
: constant Entity_Id
:= Etype
(N
);
9328 Pfx
: constant Node_Id
:= Prefix
(N
);
9329 Ptp
: Entity_Id
:= Etype
(Pfx
);
9331 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean;
9332 -- Check whether the argument is an actual for a procedure call, in
9333 -- which case the expansion of a bit-packed slice is deferred until the
9334 -- call itself is expanded. The reason this is required is that we might
9335 -- have an IN OUT or OUT parameter, and the copy out is essential, and
9336 -- that copy out would be missed if we created a temporary here in
9337 -- Expand_N_Slice. Note that we don't bother to test specifically for an
9338 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
9339 -- is harmless to defer expansion in the IN case, since the call
9340 -- processing will still generate the appropriate copy in operation,
9341 -- which will take care of the slice.
9343 procedure Make_Temporary_For_Slice
;
9344 -- Create a named variable for the value of the slice, in cases where
9345 -- the back-end cannot handle it properly, e.g. when packed types or
9346 -- unaligned slices are involved.
9348 -------------------------
9349 -- Is_Procedure_Actual --
9350 -------------------------
9352 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean is
9353 Par
: Node_Id
:= Parent
(N
);
9357 -- If our parent is a procedure call we can return
9359 if Nkind
(Par
) = N_Procedure_Call_Statement
then
9362 -- If our parent is a type conversion, keep climbing the tree,
9363 -- since a type conversion can be a procedure actual. Also keep
9364 -- climbing if parameter association or a qualified expression,
9365 -- since these are additional cases that do can appear on
9366 -- procedure actuals.
9368 elsif Nkind_In
(Par
, N_Type_Conversion
,
9369 N_Parameter_Association
,
9370 N_Qualified_Expression
)
9372 Par
:= Parent
(Par
);
9374 -- Any other case is not what we are looking for
9380 end Is_Procedure_Actual
;
9382 ------------------------------
9383 -- Make_Temporary_For_Slice --
9384 ------------------------------
9386 procedure Make_Temporary_For_Slice
is
9388 Ent
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', N
);
9392 Make_Object_Declaration
(Loc
,
9393 Defining_Identifier
=> Ent
,
9394 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
9396 Set_No_Initialization
(Decl
);
9398 Insert_Actions
(N
, New_List
(
9400 Make_Assignment_Statement
(Loc
,
9401 Name
=> New_Occurrence_Of
(Ent
, Loc
),
9402 Expression
=> Relocate_Node
(N
))));
9404 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
9405 Analyze_And_Resolve
(N
, Typ
);
9406 end Make_Temporary_For_Slice
;
9408 -- Start of processing for Expand_N_Slice
9411 -- Special handling for access types
9413 if Is_Access_Type
(Ptp
) then
9415 Ptp
:= Designated_Type
(Ptp
);
9418 Make_Explicit_Dereference
(Sloc
(N
),
9419 Prefix
=> Relocate_Node
(Pfx
)));
9421 Analyze_And_Resolve
(Pfx
, Ptp
);
9424 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9425 -- function, then additional actuals must be passed.
9427 if Ada_Version
>= Ada_2005
9428 and then Is_Build_In_Place_Function_Call
(Pfx
)
9430 Make_Build_In_Place_Call_In_Anonymous_Context
(Pfx
);
9433 -- The remaining case to be handled is packed slices. We can leave
9434 -- packed slices as they are in the following situations:
9436 -- 1. Right or left side of an assignment (we can handle this
9437 -- situation correctly in the assignment statement expansion).
9439 -- 2. Prefix of indexed component (the slide is optimized away in this
9440 -- case, see the start of Expand_N_Slice.)
9442 -- 3. Object renaming declaration, since we want the name of the
9443 -- slice, not the value.
9445 -- 4. Argument to procedure call, since copy-in/copy-out handling may
9446 -- be required, and this is handled in the expansion of call
9449 -- 5. Prefix of an address attribute (this is an error which is caught
9450 -- elsewhere, and the expansion would interfere with generating the
9453 if not Is_Packed
(Typ
) then
9455 -- Apply transformation for actuals of a function call, where
9456 -- Expand_Actuals is not used.
9458 if Nkind
(Parent
(N
)) = N_Function_Call
9459 and then Is_Possibly_Unaligned_Slice
(N
)
9461 Make_Temporary_For_Slice
;
9464 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
9465 or else (Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
9466 and then Parent
(N
) = Name
(Parent
(Parent
(N
))))
9470 elsif Nkind
(Parent
(N
)) = N_Indexed_Component
9471 or else Is_Renamed_Object
(N
)
9472 or else Is_Procedure_Actual
(N
)
9476 elsif Nkind
(Parent
(N
)) = N_Attribute_Reference
9477 and then Attribute_Name
(Parent
(N
)) = Name_Address
9482 Make_Temporary_For_Slice
;
9486 ------------------------------
9487 -- Expand_N_Type_Conversion --
9488 ------------------------------
9490 procedure Expand_N_Type_Conversion
(N
: Node_Id
) is
9491 Loc
: constant Source_Ptr
:= Sloc
(N
);
9492 Operand
: constant Node_Id
:= Expression
(N
);
9493 Target_Type
: constant Entity_Id
:= Etype
(N
);
9494 Operand_Type
: Entity_Id
:= Etype
(Operand
);
9496 procedure Handle_Changed_Representation
;
9497 -- This is called in the case of record and array type conversions to
9498 -- see if there is a change of representation to be handled. Change of
9499 -- representation is actually handled at the assignment statement level,
9500 -- and what this procedure does is rewrite node N conversion as an
9501 -- assignment to temporary. If there is no change of representation,
9502 -- then the conversion node is unchanged.
9504 procedure Raise_Accessibility_Error
;
9505 -- Called when we know that an accessibility check will fail. Rewrites
9506 -- node N to an appropriate raise statement and outputs warning msgs.
9507 -- The Etype of the raise node is set to Target_Type.
9509 procedure Real_Range_Check
;
9510 -- Handles generation of range check for real target value
9512 function Has_Extra_Accessibility
(Id
: Entity_Id
) return Boolean;
9513 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
9514 -- evaluates to True.
9516 -----------------------------------
9517 -- Handle_Changed_Representation --
9518 -----------------------------------
9520 procedure Handle_Changed_Representation
is
9529 -- Nothing else to do if no change of representation
9531 if Same_Representation
(Operand_Type
, Target_Type
) then
9534 -- The real change of representation work is done by the assignment
9535 -- statement processing. So if this type conversion is appearing as
9536 -- the expression of an assignment statement, nothing needs to be
9537 -- done to the conversion.
9539 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
then
9542 -- Otherwise we need to generate a temporary variable, and do the
9543 -- change of representation assignment into that temporary variable.
9544 -- The conversion is then replaced by a reference to this variable.
9549 -- If type is unconstrained we have to add a constraint, copied
9550 -- from the actual value of the left hand side.
9552 if not Is_Constrained
(Target_Type
) then
9553 if Has_Discriminants
(Operand_Type
) then
9554 Disc
:= First_Discriminant
(Operand_Type
);
9556 if Disc
/= First_Stored_Discriminant
(Operand_Type
) then
9557 Disc
:= First_Stored_Discriminant
(Operand_Type
);
9561 while Present
(Disc
) loop
9563 Make_Selected_Component
(Loc
,
9565 Duplicate_Subexpr_Move_Checks
(Operand
),
9567 Make_Identifier
(Loc
, Chars
(Disc
))));
9568 Next_Discriminant
(Disc
);
9571 elsif Is_Array_Type
(Operand_Type
) then
9572 N_Ix
:= First_Index
(Target_Type
);
9575 for J
in 1 .. Number_Dimensions
(Operand_Type
) loop
9577 -- We convert the bounds explicitly. We use an unchecked
9578 -- conversion because bounds checks are done elsewhere.
9583 Unchecked_Convert_To
(Etype
(N_Ix
),
9584 Make_Attribute_Reference
(Loc
,
9586 Duplicate_Subexpr_No_Checks
9587 (Operand
, Name_Req
=> True),
9588 Attribute_Name
=> Name_First
,
9589 Expressions
=> New_List
(
9590 Make_Integer_Literal
(Loc
, J
)))),
9593 Unchecked_Convert_To
(Etype
(N_Ix
),
9594 Make_Attribute_Reference
(Loc
,
9596 Duplicate_Subexpr_No_Checks
9597 (Operand
, Name_Req
=> True),
9598 Attribute_Name
=> Name_Last
,
9599 Expressions
=> New_List
(
9600 Make_Integer_Literal
(Loc
, J
))))));
9607 Odef
:= New_Occurrence_Of
(Target_Type
, Loc
);
9609 if Present
(Cons
) then
9611 Make_Subtype_Indication
(Loc
,
9612 Subtype_Mark
=> Odef
,
9614 Make_Index_Or_Discriminant_Constraint
(Loc
,
9615 Constraints
=> Cons
));
9618 Temp
:= Make_Temporary
(Loc
, 'C');
9620 Make_Object_Declaration
(Loc
,
9621 Defining_Identifier
=> Temp
,
9622 Object_Definition
=> Odef
);
9624 Set_No_Initialization
(Decl
, True);
9626 -- Insert required actions. It is essential to suppress checks
9627 -- since we have suppressed default initialization, which means
9628 -- that the variable we create may have no discriminants.
9633 Make_Assignment_Statement
(Loc
,
9634 Name
=> New_Occurrence_Of
(Temp
, Loc
),
9635 Expression
=> Relocate_Node
(N
))),
9636 Suppress
=> All_Checks
);
9638 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
9641 end Handle_Changed_Representation
;
9643 -------------------------------
9644 -- Raise_Accessibility_Error --
9645 -------------------------------
9647 procedure Raise_Accessibility_Error
is
9650 Make_Raise_Program_Error
(Sloc
(N
),
9651 Reason
=> PE_Accessibility_Check_Failed
));
9652 Set_Etype
(N
, Target_Type
);
9654 Error_Msg_N
("?accessibility check failure", N
);
9656 ("\?& will be raised at run time", N
, Standard_Program_Error
);
9657 end Raise_Accessibility_Error
;
9659 ----------------------
9660 -- Real_Range_Check --
9661 ----------------------
9663 -- Case of conversions to floating-point or fixed-point. If range checks
9664 -- are enabled and the target type has a range constraint, we convert:
9670 -- Tnn : typ'Base := typ'Base (x);
9671 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
9674 -- This is necessary when there is a conversion of integer to float or
9675 -- to fixed-point to ensure that the correct checks are made. It is not
9676 -- necessary for float to float where it is enough to simply set the
9677 -- Do_Range_Check flag.
9679 procedure Real_Range_Check
is
9680 Btyp
: constant Entity_Id
:= Base_Type
(Target_Type
);
9681 Lo
: constant Node_Id
:= Type_Low_Bound
(Target_Type
);
9682 Hi
: constant Node_Id
:= Type_High_Bound
(Target_Type
);
9683 Xtyp
: constant Entity_Id
:= Etype
(Operand
);
9688 -- Nothing to do if conversion was rewritten
9690 if Nkind
(N
) /= N_Type_Conversion
then
9694 -- Nothing to do if range checks suppressed, or target has the same
9695 -- range as the base type (or is the base type).
9697 if Range_Checks_Suppressed
(Target_Type
)
9698 or else (Lo
= Type_Low_Bound
(Btyp
)
9700 Hi
= Type_High_Bound
(Btyp
))
9705 -- Nothing to do if expression is an entity on which checks have been
9708 if Is_Entity_Name
(Operand
)
9709 and then Range_Checks_Suppressed
(Entity
(Operand
))
9714 -- Nothing to do if bounds are all static and we can tell that the
9715 -- expression is within the bounds of the target. Note that if the
9716 -- operand is of an unconstrained floating-point type, then we do
9717 -- not trust it to be in range (might be infinite)
9720 S_Lo
: constant Node_Id
:= Type_Low_Bound
(Xtyp
);
9721 S_Hi
: constant Node_Id
:= Type_High_Bound
(Xtyp
);
9724 if (not Is_Floating_Point_Type
(Xtyp
)
9725 or else Is_Constrained
(Xtyp
))
9726 and then Compile_Time_Known_Value
(S_Lo
)
9727 and then Compile_Time_Known_Value
(S_Hi
)
9728 and then Compile_Time_Known_Value
(Hi
)
9729 and then Compile_Time_Known_Value
(Lo
)
9732 D_Lov
: constant Ureal
:= Expr_Value_R
(Lo
);
9733 D_Hiv
: constant Ureal
:= Expr_Value_R
(Hi
);
9738 if Is_Real_Type
(Xtyp
) then
9739 S_Lov
:= Expr_Value_R
(S_Lo
);
9740 S_Hiv
:= Expr_Value_R
(S_Hi
);
9742 S_Lov
:= UR_From_Uint
(Expr_Value
(S_Lo
));
9743 S_Hiv
:= UR_From_Uint
(Expr_Value
(S_Hi
));
9747 and then S_Lov
>= D_Lov
9748 and then S_Hiv
<= D_Hiv
9750 Set_Do_Range_Check
(Operand
, False);
9757 -- For float to float conversions, we are done
9759 if Is_Floating_Point_Type
(Xtyp
)
9761 Is_Floating_Point_Type
(Btyp
)
9766 -- Otherwise rewrite the conversion as described above
9768 Conv
:= Relocate_Node
(N
);
9769 Rewrite
(Subtype_Mark
(Conv
), New_Occurrence_Of
(Btyp
, Loc
));
9770 Set_Etype
(Conv
, Btyp
);
9772 -- Enable overflow except for case of integer to float conversions,
9773 -- where it is never required, since we can never have overflow in
9776 if not Is_Integer_Type
(Etype
(Operand
)) then
9777 Enable_Overflow_Check
(Conv
);
9780 Tnn
:= Make_Temporary
(Loc
, 'T', Conv
);
9782 Insert_Actions
(N
, New_List
(
9783 Make_Object_Declaration
(Loc
,
9784 Defining_Identifier
=> Tnn
,
9785 Object_Definition
=> New_Occurrence_Of
(Btyp
, Loc
),
9786 Constant_Present
=> True,
9787 Expression
=> Conv
),
9789 Make_Raise_Constraint_Error
(Loc
,
9794 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
9796 Make_Attribute_Reference
(Loc
,
9797 Attribute_Name
=> Name_First
,
9799 New_Occurrence_Of
(Target_Type
, Loc
))),
9803 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
9805 Make_Attribute_Reference
(Loc
,
9806 Attribute_Name
=> Name_Last
,
9808 New_Occurrence_Of
(Target_Type
, Loc
)))),
9809 Reason
=> CE_Range_Check_Failed
)));
9811 Rewrite
(N
, New_Occurrence_Of
(Tnn
, Loc
));
9812 Analyze_And_Resolve
(N
, Btyp
);
9813 end Real_Range_Check
;
9815 -----------------------------
9816 -- Has_Extra_Accessibility --
9817 -----------------------------
9819 -- Returns true for a formal of an anonymous access type or for
9820 -- an Ada 2012-style stand-alone object of an anonymous access type.
9822 function Has_Extra_Accessibility
(Id
: Entity_Id
) return Boolean is
9824 if Is_Formal
(Id
) or else Ekind_In
(Id
, E_Constant
, E_Variable
) then
9825 return Present
(Effective_Extra_Accessibility
(Id
));
9829 end Has_Extra_Accessibility
;
9831 -- Start of processing for Expand_N_Type_Conversion
9834 -- Nothing at all to do if conversion is to the identical type so remove
9835 -- the conversion completely, it is useless, except that it may carry
9836 -- an Assignment_OK attribute, which must be propagated to the operand.
9838 if Operand_Type
= Target_Type
then
9839 if Assignment_OK
(N
) then
9840 Set_Assignment_OK
(Operand
);
9843 Rewrite
(N
, Relocate_Node
(Operand
));
9847 -- Nothing to do if this is the second argument of read. This is a
9848 -- "backwards" conversion that will be handled by the specialized code
9849 -- in attribute processing.
9851 if Nkind
(Parent
(N
)) = N_Attribute_Reference
9852 and then Attribute_Name
(Parent
(N
)) = Name_Read
9853 and then Next
(First
(Expressions
(Parent
(N
)))) = N
9858 -- Check for case of converting to a type that has an invariant
9859 -- associated with it. This required an invariant check. We convert
9865 -- do invariant_check (typ (expr)) in typ (expr);
9867 -- using Duplicate_Subexpr to avoid multiple side effects
9869 -- Note: the Comes_From_Source check, and then the resetting of this
9870 -- flag prevents what would otherwise be an infinite recursion.
9872 if Has_Invariants
(Target_Type
)
9873 and then Present
(Invariant_Procedure
(Target_Type
))
9874 and then Comes_From_Source
(N
)
9876 Set_Comes_From_Source
(N
, False);
9878 Make_Expression_With_Actions
(Loc
,
9879 Actions
=> New_List
(
9880 Make_Invariant_Call
(Duplicate_Subexpr
(N
))),
9881 Expression
=> Duplicate_Subexpr_No_Checks
(N
)));
9882 Analyze_And_Resolve
(N
, Target_Type
);
9886 -- Here if we may need to expand conversion
9888 -- If the operand of the type conversion is an arithmetic operation on
9889 -- signed integers, and the based type of the signed integer type in
9890 -- question is smaller than Standard.Integer, we promote both of the
9891 -- operands to type Integer.
9893 -- For example, if we have
9895 -- target-type (opnd1 + opnd2)
9897 -- and opnd1 and opnd2 are of type short integer, then we rewrite
9900 -- target-type (integer(opnd1) + integer(opnd2))
9902 -- We do this because we are always allowed to compute in a larger type
9903 -- if we do the right thing with the result, and in this case we are
9904 -- going to do a conversion which will do an appropriate check to make
9905 -- sure that things are in range of the target type in any case. This
9906 -- avoids some unnecessary intermediate overflows.
9908 -- We might consider a similar transformation in the case where the
9909 -- target is a real type or a 64-bit integer type, and the operand
9910 -- is an arithmetic operation using a 32-bit integer type. However,
9911 -- we do not bother with this case, because it could cause significant
9912 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
9913 -- much cheaper, but we don't want different behavior on 32-bit and
9914 -- 64-bit machines. Note that the exclusion of the 64-bit case also
9915 -- handles the configurable run-time cases where 64-bit arithmetic
9916 -- may simply be unavailable.
9918 -- Note: this circuit is partially redundant with respect to the circuit
9919 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
9920 -- the processing here. Also we still need the Checks circuit, since we
9921 -- have to be sure not to generate junk overflow checks in the first
9922 -- place, since it would be trick to remove them here!
9924 if Integer_Promotion_Possible
(N
) then
9926 -- All conditions met, go ahead with transformation
9934 Make_Type_Conversion
(Loc
,
9935 Subtype_Mark
=> New_Reference_To
(Standard_Integer
, Loc
),
9936 Expression
=> Relocate_Node
(Right_Opnd
(Operand
)));
9938 Opnd
:= New_Op_Node
(Nkind
(Operand
), Loc
);
9939 Set_Right_Opnd
(Opnd
, R
);
9941 if Nkind
(Operand
) in N_Binary_Op
then
9943 Make_Type_Conversion
(Loc
,
9944 Subtype_Mark
=> New_Reference_To
(Standard_Integer
, Loc
),
9945 Expression
=> Relocate_Node
(Left_Opnd
(Operand
)));
9947 Set_Left_Opnd
(Opnd
, L
);
9951 Make_Type_Conversion
(Loc
,
9952 Subtype_Mark
=> Relocate_Node
(Subtype_Mark
(N
)),
9953 Expression
=> Opnd
));
9955 Analyze_And_Resolve
(N
, Target_Type
);
9960 -- Do validity check if validity checking operands
9962 if Validity_Checks_On
9963 and then Validity_Check_Operands
9965 Ensure_Valid
(Operand
);
9968 -- Special case of converting from non-standard boolean type
9970 if Is_Boolean_Type
(Operand_Type
)
9971 and then (Nonzero_Is_True
(Operand_Type
))
9973 Adjust_Condition
(Operand
);
9974 Set_Etype
(Operand
, Standard_Boolean
);
9975 Operand_Type
:= Standard_Boolean
;
9978 -- Case of converting to an access type
9980 if Is_Access_Type
(Target_Type
) then
9982 -- Apply an accessibility check when the conversion operand is an
9983 -- access parameter (or a renaming thereof), unless conversion was
9984 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
9985 -- Note that other checks may still need to be applied below (such
9986 -- as tagged type checks).
9988 if Is_Entity_Name
(Operand
)
9989 and then Has_Extra_Accessibility
(Entity
(Operand
))
9990 and then Ekind
(Etype
(Operand
)) = E_Anonymous_Access_Type
9991 and then (Nkind
(Original_Node
(N
)) /= N_Attribute_Reference
9992 or else Attribute_Name
(Original_Node
(N
)) = Name_Access
)
9994 Apply_Accessibility_Check
9995 (Operand
, Target_Type
, Insert_Node
=> Operand
);
9997 -- If the level of the operand type is statically deeper than the
9998 -- level of the target type, then force Program_Error. Note that this
9999 -- can only occur for cases where the attribute is within the body of
10000 -- an instantiation (otherwise the conversion will already have been
10001 -- rejected as illegal). Note: warnings are issued by the analyzer
10002 -- for the instance cases.
10004 elsif In_Instance_Body
10005 and then Type_Access_Level
(Operand_Type
) >
10006 Type_Access_Level
(Target_Type
)
10008 Raise_Accessibility_Error
;
10010 -- When the operand is a selected access discriminant the check needs
10011 -- to be made against the level of the object denoted by the prefix
10012 -- of the selected name. Force Program_Error for this case as well
10013 -- (this accessibility violation can only happen if within the body
10014 -- of an instantiation).
10016 elsif In_Instance_Body
10017 and then Ekind
(Operand_Type
) = E_Anonymous_Access_Type
10018 and then Nkind
(Operand
) = N_Selected_Component
10019 and then Object_Access_Level
(Operand
) >
10020 Type_Access_Level
(Target_Type
)
10022 Raise_Accessibility_Error
;
10027 -- Case of conversions of tagged types and access to tagged types
10029 -- When needed, that is to say when the expression is class-wide, Add
10030 -- runtime a tag check for (strict) downward conversion by using the
10031 -- membership test, generating:
10033 -- [constraint_error when Operand not in Target_Type'Class]
10035 -- or in the access type case
10037 -- [constraint_error
10038 -- when Operand /= null
10039 -- and then Operand.all not in
10040 -- Designated_Type (Target_Type)'Class]
10042 if (Is_Access_Type
(Target_Type
)
10043 and then Is_Tagged_Type
(Designated_Type
(Target_Type
)))
10044 or else Is_Tagged_Type
(Target_Type
)
10046 -- Do not do any expansion in the access type case if the parent is a
10047 -- renaming, since this is an error situation which will be caught by
10048 -- Sem_Ch8, and the expansion can interfere with this error check.
10050 if Is_Access_Type
(Target_Type
) and then Is_Renamed_Object
(N
) then
10054 -- Otherwise, proceed with processing tagged conversion
10056 Tagged_Conversion
: declare
10057 Actual_Op_Typ
: Entity_Id
;
10058 Actual_Targ_Typ
: Entity_Id
;
10059 Make_Conversion
: Boolean := False;
10060 Root_Op_Typ
: Entity_Id
;
10062 procedure Make_Tag_Check
(Targ_Typ
: Entity_Id
);
10063 -- Create a membership check to test whether Operand is a member
10064 -- of Targ_Typ. If the original Target_Type is an access, include
10065 -- a test for null value. The check is inserted at N.
10067 --------------------
10068 -- Make_Tag_Check --
10069 --------------------
10071 procedure Make_Tag_Check
(Targ_Typ
: Entity_Id
) is
10076 -- [Constraint_Error
10077 -- when Operand /= null
10078 -- and then Operand.all not in Targ_Typ]
10080 if Is_Access_Type
(Target_Type
) then
10082 Make_And_Then
(Loc
,
10085 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
10086 Right_Opnd
=> Make_Null
(Loc
)),
10091 Make_Explicit_Dereference
(Loc
,
10092 Prefix
=> Duplicate_Subexpr_No_Checks
(Operand
)),
10093 Right_Opnd
=> New_Reference_To
(Targ_Typ
, Loc
)));
10096 -- [Constraint_Error when Operand not in Targ_Typ]
10101 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
10102 Right_Opnd
=> New_Reference_To
(Targ_Typ
, Loc
));
10106 Make_Raise_Constraint_Error
(Loc
,
10108 Reason
=> CE_Tag_Check_Failed
));
10109 end Make_Tag_Check
;
10111 -- Start of processing for Tagged_Conversion
10114 -- Handle entities from the limited view
10116 if Is_Access_Type
(Operand_Type
) then
10118 Available_View
(Designated_Type
(Operand_Type
));
10120 Actual_Op_Typ
:= Operand_Type
;
10123 if Is_Access_Type
(Target_Type
) then
10125 Available_View
(Designated_Type
(Target_Type
));
10127 Actual_Targ_Typ
:= Target_Type
;
10130 Root_Op_Typ
:= Root_Type
(Actual_Op_Typ
);
10132 -- Ada 2005 (AI-251): Handle interface type conversion
10134 if Is_Interface
(Actual_Op_Typ
) then
10135 Expand_Interface_Conversion
(N
, Is_Static
=> False);
10139 if not Tag_Checks_Suppressed
(Actual_Targ_Typ
) then
10141 -- Create a runtime tag check for a downward class-wide type
10144 if Is_Class_Wide_Type
(Actual_Op_Typ
)
10145 and then Actual_Op_Typ
/= Actual_Targ_Typ
10146 and then Root_Op_Typ
/= Actual_Targ_Typ
10147 and then Is_Ancestor
(Root_Op_Typ
, Actual_Targ_Typ
,
10148 Use_Full_View
=> True)
10150 Make_Tag_Check
(Class_Wide_Type
(Actual_Targ_Typ
));
10151 Make_Conversion
:= True;
10154 -- AI05-0073: If the result subtype of the function is defined
10155 -- by an access_definition designating a specific tagged type
10156 -- T, a check is made that the result value is null or the tag
10157 -- of the object designated by the result value identifies T.
10158 -- Constraint_Error is raised if this check fails.
10160 if Nkind
(Parent
(N
)) = N_Simple_Return_Statement
then
10163 Func_Typ
: Entity_Id
;
10166 -- Climb scope stack looking for the enclosing function
10168 Func
:= Current_Scope
;
10169 while Present
(Func
)
10170 and then Ekind
(Func
) /= E_Function
10172 Func
:= Scope
(Func
);
10175 -- The function's return subtype must be defined using
10176 -- an access definition.
10178 if Nkind
(Result_Definition
(Parent
(Func
))) =
10179 N_Access_Definition
10181 Func_Typ
:= Directly_Designated_Type
(Etype
(Func
));
10183 -- The return subtype denotes a specific tagged type,
10184 -- in other words, a non class-wide type.
10186 if Is_Tagged_Type
(Func_Typ
)
10187 and then not Is_Class_Wide_Type
(Func_Typ
)
10189 Make_Tag_Check
(Actual_Targ_Typ
);
10190 Make_Conversion
:= True;
10196 -- We have generated a tag check for either a class-wide type
10197 -- conversion or for AI05-0073.
10199 if Make_Conversion
then
10204 Make_Unchecked_Type_Conversion
(Loc
,
10205 Subtype_Mark
=> New_Occurrence_Of
(Target_Type
, Loc
),
10206 Expression
=> Relocate_Node
(Expression
(N
)));
10208 Analyze_And_Resolve
(N
, Target_Type
);
10212 end Tagged_Conversion
;
10214 -- Case of other access type conversions
10216 elsif Is_Access_Type
(Target_Type
) then
10217 Apply_Constraint_Check
(Operand
, Target_Type
);
10219 -- Case of conversions from a fixed-point type
10221 -- These conversions require special expansion and processing, found in
10222 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
10223 -- since from a semantic point of view, these are simple integer
10224 -- conversions, which do not need further processing.
10226 elsif Is_Fixed_Point_Type
(Operand_Type
)
10227 and then not Conversion_OK
(N
)
10229 -- We should never see universal fixed at this case, since the
10230 -- expansion of the constituent divide or multiply should have
10231 -- eliminated the explicit mention of universal fixed.
10233 pragma Assert
(Operand_Type
/= Universal_Fixed
);
10235 -- Check for special case of the conversion to universal real that
10236 -- occurs as a result of the use of a round attribute. In this case,
10237 -- the real type for the conversion is taken from the target type of
10238 -- the Round attribute and the result must be marked as rounded.
10240 if Target_Type
= Universal_Real
10241 and then Nkind
(Parent
(N
)) = N_Attribute_Reference
10242 and then Attribute_Name
(Parent
(N
)) = Name_Round
10244 Set_Rounded_Result
(N
);
10245 Set_Etype
(N
, Etype
(Parent
(N
)));
10248 -- Otherwise do correct fixed-conversion, but skip these if the
10249 -- Conversion_OK flag is set, because from a semantic point of view
10250 -- these are simple integer conversions needing no further processing
10251 -- (the backend will simply treat them as integers).
10253 if not Conversion_OK
(N
) then
10254 if Is_Fixed_Point_Type
(Etype
(N
)) then
10255 Expand_Convert_Fixed_To_Fixed
(N
);
10258 elsif Is_Integer_Type
(Etype
(N
)) then
10259 Expand_Convert_Fixed_To_Integer
(N
);
10262 pragma Assert
(Is_Floating_Point_Type
(Etype
(N
)));
10263 Expand_Convert_Fixed_To_Float
(N
);
10268 -- Case of conversions to a fixed-point type
10270 -- These conversions require special expansion and processing, found in
10271 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
10272 -- since from a semantic point of view, these are simple integer
10273 -- conversions, which do not need further processing.
10275 elsif Is_Fixed_Point_Type
(Target_Type
)
10276 and then not Conversion_OK
(N
)
10278 if Is_Integer_Type
(Operand_Type
) then
10279 Expand_Convert_Integer_To_Fixed
(N
);
10282 pragma Assert
(Is_Floating_Point_Type
(Operand_Type
));
10283 Expand_Convert_Float_To_Fixed
(N
);
10287 -- Case of float-to-integer conversions
10289 -- We also handle float-to-fixed conversions with Conversion_OK set
10290 -- since semantically the fixed-point target is treated as though it
10291 -- were an integer in such cases.
10293 elsif Is_Floating_Point_Type
(Operand_Type
)
10295 (Is_Integer_Type
(Target_Type
)
10297 (Is_Fixed_Point_Type
(Target_Type
) and then Conversion_OK
(N
)))
10299 -- One more check here, gcc is still not able to do conversions of
10300 -- this type with proper overflow checking, and so gigi is doing an
10301 -- approximation of what is required by doing floating-point compares
10302 -- with the end-point. But that can lose precision in some cases, and
10303 -- give a wrong result. Converting the operand to Universal_Real is
10304 -- helpful, but still does not catch all cases with 64-bit integers
10305 -- on targets with only 64-bit floats.
10307 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
10308 -- Can this code be removed ???
10310 if Do_Range_Check
(Operand
) then
10312 Make_Type_Conversion
(Loc
,
10314 New_Occurrence_Of
(Universal_Real
, Loc
),
10316 Relocate_Node
(Operand
)));
10318 Set_Etype
(Operand
, Universal_Real
);
10319 Enable_Range_Check
(Operand
);
10320 Set_Do_Range_Check
(Expression
(Operand
), False);
10323 -- Case of array conversions
10325 -- Expansion of array conversions, add required length/range checks but
10326 -- only do this if there is no change of representation. For handling of
10327 -- this case, see Handle_Changed_Representation.
10329 elsif Is_Array_Type
(Target_Type
) then
10330 if Is_Constrained
(Target_Type
) then
10331 Apply_Length_Check
(Operand
, Target_Type
);
10333 Apply_Range_Check
(Operand
, Target_Type
);
10336 Handle_Changed_Representation
;
10338 -- Case of conversions of discriminated types
10340 -- Add required discriminant checks if target is constrained. Again this
10341 -- change is skipped if we have a change of representation.
10343 elsif Has_Discriminants
(Target_Type
)
10344 and then Is_Constrained
(Target_Type
)
10346 Apply_Discriminant_Check
(Operand
, Target_Type
);
10347 Handle_Changed_Representation
;
10349 -- Case of all other record conversions. The only processing required
10350 -- is to check for a change of representation requiring the special
10351 -- assignment processing.
10353 elsif Is_Record_Type
(Target_Type
) then
10355 -- Ada 2005 (AI-216): Program_Error is raised when converting from
10356 -- a derived Unchecked_Union type to an unconstrained type that is
10357 -- not Unchecked_Union if the operand lacks inferable discriminants.
10359 if Is_Derived_Type
(Operand_Type
)
10360 and then Is_Unchecked_Union
(Base_Type
(Operand_Type
))
10361 and then not Is_Constrained
(Target_Type
)
10362 and then not Is_Unchecked_Union
(Base_Type
(Target_Type
))
10363 and then not Has_Inferable_Discriminants
(Operand
)
10365 -- To prevent Gigi from generating illegal code, we generate a
10366 -- Program_Error node, but we give it the target type of the
10367 -- conversion (is this requirement documented somewhere ???)
10370 PE
: constant Node_Id
:= Make_Raise_Program_Error
(Loc
,
10371 Reason
=> PE_Unchecked_Union_Restriction
);
10374 Set_Etype
(PE
, Target_Type
);
10379 Handle_Changed_Representation
;
10382 -- Case of conversions of enumeration types
10384 elsif Is_Enumeration_Type
(Target_Type
) then
10386 -- Special processing is required if there is a change of
10387 -- representation (from enumeration representation clauses).
10389 if not Same_Representation
(Target_Type
, Operand_Type
) then
10391 -- Convert: x(y) to x'val (ytyp'val (y))
10394 Make_Attribute_Reference
(Loc
,
10395 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
10396 Attribute_Name
=> Name_Val
,
10397 Expressions
=> New_List
(
10398 Make_Attribute_Reference
(Loc
,
10399 Prefix
=> New_Occurrence_Of
(Operand_Type
, Loc
),
10400 Attribute_Name
=> Name_Pos
,
10401 Expressions
=> New_List
(Operand
)))));
10403 Analyze_And_Resolve
(N
, Target_Type
);
10406 -- Case of conversions to floating-point
10408 elsif Is_Floating_Point_Type
(Target_Type
) then
10412 -- At this stage, either the conversion node has been transformed into
10413 -- some other equivalent expression, or left as a conversion that can be
10414 -- handled by Gigi, in the following cases:
10416 -- Conversions with no change of representation or type
10418 -- Numeric conversions involving integer, floating- and fixed-point
10419 -- values. Fixed-point values are allowed only if Conversion_OK is
10420 -- set, i.e. if the fixed-point values are to be treated as integers.
10422 -- No other conversions should be passed to Gigi
10424 -- Check: are these rules stated in sinfo??? if so, why restate here???
10426 -- The only remaining step is to generate a range check if we still have
10427 -- a type conversion at this stage and Do_Range_Check is set. For now we
10428 -- do this only for conversions of discrete types.
10430 if Nkind
(N
) = N_Type_Conversion
10431 and then Is_Discrete_Type
(Etype
(N
))
10434 Expr
: constant Node_Id
:= Expression
(N
);
10439 if Do_Range_Check
(Expr
)
10440 and then Is_Discrete_Type
(Etype
(Expr
))
10442 Set_Do_Range_Check
(Expr
, False);
10444 -- Before we do a range check, we have to deal with treating a
10445 -- fixed-point operand as an integer. The way we do this is
10446 -- simply to do an unchecked conversion to an appropriate
10447 -- integer type large enough to hold the result.
10449 -- This code is not active yet, because we are only dealing
10450 -- with discrete types so far ???
10452 if Nkind
(Expr
) in N_Has_Treat_Fixed_As_Integer
10453 and then Treat_Fixed_As_Integer
(Expr
)
10455 Ftyp
:= Base_Type
(Etype
(Expr
));
10457 if Esize
(Ftyp
) >= Esize
(Standard_Integer
) then
10458 Ityp
:= Standard_Long_Long_Integer
;
10460 Ityp
:= Standard_Integer
;
10463 Rewrite
(Expr
, Unchecked_Convert_To
(Ityp
, Expr
));
10466 -- Reset overflow flag, since the range check will include
10467 -- dealing with possible overflow, and generate the check. If
10468 -- Address is either a source type or target type, suppress
10469 -- range check to avoid typing anomalies when it is a visible
10472 Set_Do_Overflow_Check
(N
, False);
10473 if not Is_Descendent_Of_Address
(Etype
(Expr
))
10474 and then not Is_Descendent_Of_Address
(Target_Type
)
10476 Generate_Range_Check
10477 (Expr
, Target_Type
, CE_Range_Check_Failed
);
10483 -- Final step, if the result is a type conversion involving Vax_Float
10484 -- types, then it is subject for further special processing.
10486 if Nkind
(N
) = N_Type_Conversion
10487 and then (Vax_Float
(Operand_Type
) or else Vax_Float
(Target_Type
))
10489 Expand_Vax_Conversion
(N
);
10493 -- Here at end of processing
10496 -- Apply predicate check if required. Note that we can't just call
10497 -- Apply_Predicate_Check here, because the type looks right after
10498 -- the conversion and it would omit the check. The Comes_From_Source
10499 -- guard is necessary to prevent infinite recursions when we generate
10500 -- internal conversions for the purpose of checking predicates.
10502 if Present
(Predicate_Function
(Target_Type
))
10503 and then Target_Type
/= Operand_Type
10504 and then Comes_From_Source
(N
)
10507 New_Expr
: constant Node_Id
:= Duplicate_Subexpr
(N
);
10510 -- Avoid infinite recursion on the subsequent expansion of
10511 -- of the copy of the original type conversion.
10513 Set_Comes_From_Source
(New_Expr
, False);
10514 Insert_Action
(N
, Make_Predicate_Check
(Target_Type
, New_Expr
));
10517 end Expand_N_Type_Conversion
;
10519 -----------------------------------
10520 -- Expand_N_Unchecked_Expression --
10521 -----------------------------------
10523 -- Remove the unchecked expression node from the tree. Its job was simply
10524 -- to make sure that its constituent expression was handled with checks
10525 -- off, and now that that is done, we can remove it from the tree, and
10526 -- indeed must, since Gigi does not expect to see these nodes.
10528 procedure Expand_N_Unchecked_Expression
(N
: Node_Id
) is
10529 Exp
: constant Node_Id
:= Expression
(N
);
10531 Set_Assignment_OK
(Exp
, Assignment_OK
(N
) or else Assignment_OK
(Exp
));
10533 end Expand_N_Unchecked_Expression
;
10535 ----------------------------------------
10536 -- Expand_N_Unchecked_Type_Conversion --
10537 ----------------------------------------
10539 -- If this cannot be handled by Gigi and we haven't already made a
10540 -- temporary for it, do it now.
10542 procedure Expand_N_Unchecked_Type_Conversion
(N
: Node_Id
) is
10543 Target_Type
: constant Entity_Id
:= Etype
(N
);
10544 Operand
: constant Node_Id
:= Expression
(N
);
10545 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
10548 -- Nothing at all to do if conversion is to the identical type so remove
10549 -- the conversion completely, it is useless, except that it may carry
10550 -- an Assignment_OK indication which must be propagated to the operand.
10552 if Operand_Type
= Target_Type
then
10554 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
10556 if Assignment_OK
(N
) then
10557 Set_Assignment_OK
(Operand
);
10560 Rewrite
(N
, Relocate_Node
(Operand
));
10564 -- If we have a conversion of a compile time known value to a target
10565 -- type and the value is in range of the target type, then we can simply
10566 -- replace the construct by an integer literal of the correct type. We
10567 -- only apply this to integer types being converted. Possibly it may
10568 -- apply in other cases, but it is too much trouble to worry about.
10570 -- Note that we do not do this transformation if the Kill_Range_Check
10571 -- flag is set, since then the value may be outside the expected range.
10572 -- This happens in the Normalize_Scalars case.
10574 -- We also skip this if either the target or operand type is biased
10575 -- because in this case, the unchecked conversion is supposed to
10576 -- preserve the bit pattern, not the integer value.
10578 if Is_Integer_Type
(Target_Type
)
10579 and then not Has_Biased_Representation
(Target_Type
)
10580 and then Is_Integer_Type
(Operand_Type
)
10581 and then not Has_Biased_Representation
(Operand_Type
)
10582 and then Compile_Time_Known_Value
(Operand
)
10583 and then not Kill_Range_Check
(N
)
10586 Val
: constant Uint
:= Expr_Value
(Operand
);
10589 if Compile_Time_Known_Value
(Type_Low_Bound
(Target_Type
))
10591 Compile_Time_Known_Value
(Type_High_Bound
(Target_Type
))
10593 Val
>= Expr_Value
(Type_Low_Bound
(Target_Type
))
10595 Val
<= Expr_Value
(Type_High_Bound
(Target_Type
))
10597 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Val
));
10599 -- If Address is the target type, just set the type to avoid a
10600 -- spurious type error on the literal when Address is a visible
10603 if Is_Descendent_Of_Address
(Target_Type
) then
10604 Set_Etype
(N
, Target_Type
);
10606 Analyze_And_Resolve
(N
, Target_Type
);
10614 -- Nothing to do if conversion is safe
10616 if Safe_Unchecked_Type_Conversion
(N
) then
10620 -- Otherwise force evaluation unless Assignment_OK flag is set (this
10621 -- flag indicates ??? -- more comments needed here)
10623 if Assignment_OK
(N
) then
10626 Force_Evaluation
(N
);
10628 end Expand_N_Unchecked_Type_Conversion
;
10630 ----------------------------
10631 -- Expand_Record_Equality --
10632 ----------------------------
10634 -- For non-variant records, Equality is expanded when needed into:
10636 -- and then Lhs.Discr1 = Rhs.Discr1
10638 -- and then Lhs.Discrn = Rhs.Discrn
10639 -- and then Lhs.Cmp1 = Rhs.Cmp1
10641 -- and then Lhs.Cmpn = Rhs.Cmpn
10643 -- The expression is folded by the back-end for adjacent fields. This
10644 -- function is called for tagged record in only one occasion: for imple-
10645 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
10646 -- otherwise the primitive "=" is used directly.
10648 function Expand_Record_Equality
10653 Bodies
: List_Id
) return Node_Id
10655 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
10660 First_Time
: Boolean := True;
10662 function Suitable_Element
(C
: Entity_Id
) return Entity_Id
;
10663 -- Return the first field to compare beginning with C, skipping the
10664 -- inherited components.
10666 ----------------------
10667 -- Suitable_Element --
10668 ----------------------
10670 function Suitable_Element
(C
: Entity_Id
) return Entity_Id
is
10675 elsif Ekind
(C
) /= E_Discriminant
10676 and then Ekind
(C
) /= E_Component
10678 return Suitable_Element
(Next_Entity
(C
));
10680 elsif Is_Tagged_Type
(Typ
)
10681 and then C
/= Original_Record_Component
(C
)
10683 return Suitable_Element
(Next_Entity
(C
));
10685 elsif Chars
(C
) = Name_uTag
then
10686 return Suitable_Element
(Next_Entity
(C
));
10688 -- The .NET/JVM version of type Root_Controlled contains two fields
10689 -- which should not be considered part of the object. To achieve
10690 -- proper equiality between two controlled objects on .NET/JVM, skip
10691 -- field _parent whenever it is of type Root_Controlled.
10693 elsif Chars
(C
) = Name_uParent
10694 and then VM_Target
/= No_VM
10695 and then Etype
(C
) = RTE
(RE_Root_Controlled
)
10697 return Suitable_Element
(Next_Entity
(C
));
10699 elsif Is_Interface
(Etype
(C
)) then
10700 return Suitable_Element
(Next_Entity
(C
));
10705 end Suitable_Element
;
10707 -- Start of processing for Expand_Record_Equality
10710 -- Generates the following code: (assuming that Typ has one Discr and
10711 -- component C2 is also a record)
10714 -- and then Lhs.Discr1 = Rhs.Discr1
10715 -- and then Lhs.C1 = Rhs.C1
10716 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
10718 -- and then Lhs.Cmpn = Rhs.Cmpn
10720 Result
:= New_Reference_To
(Standard_True
, Loc
);
10721 C
:= Suitable_Element
(First_Entity
(Typ
));
10722 while Present
(C
) loop
10730 First_Time
:= False;
10734 New_Lhs
:= New_Copy_Tree
(Lhs
);
10735 New_Rhs
:= New_Copy_Tree
(Rhs
);
10739 Expand_Composite_Equality
(Nod
, Etype
(C
),
10741 Make_Selected_Component
(Loc
,
10743 Selector_Name
=> New_Reference_To
(C
, Loc
)),
10745 Make_Selected_Component
(Loc
,
10747 Selector_Name
=> New_Reference_To
(C
, Loc
)),
10750 -- If some (sub)component is an unchecked_union, the whole
10751 -- operation will raise program error.
10753 if Nkind
(Check
) = N_Raise_Program_Error
then
10755 Set_Etype
(Result
, Standard_Boolean
);
10759 Make_And_Then
(Loc
,
10760 Left_Opnd
=> Result
,
10761 Right_Opnd
=> Check
);
10765 C
:= Suitable_Element
(Next_Entity
(C
));
10769 end Expand_Record_Equality
;
10771 ---------------------------
10772 -- Expand_Set_Membership --
10773 ---------------------------
10775 procedure Expand_Set_Membership
(N
: Node_Id
) is
10776 Lop
: constant Node_Id
:= Left_Opnd
(N
);
10780 function Make_Cond
(Alt
: Node_Id
) return Node_Id
;
10781 -- If the alternative is a subtype mark, create a simple membership
10782 -- test. Otherwise create an equality test for it.
10788 function Make_Cond
(Alt
: Node_Id
) return Node_Id
is
10790 L
: constant Node_Id
:= New_Copy
(Lop
);
10791 R
: constant Node_Id
:= Relocate_Node
(Alt
);
10794 if (Is_Entity_Name
(Alt
) and then Is_Type
(Entity
(Alt
)))
10795 or else Nkind
(Alt
) = N_Range
10798 Make_In
(Sloc
(Alt
),
10803 Make_Op_Eq
(Sloc
(Alt
),
10811 -- Start of processing for Expand_Set_Membership
10814 Remove_Side_Effects
(Lop
);
10816 Alt
:= Last
(Alternatives
(N
));
10817 Res
:= Make_Cond
(Alt
);
10820 while Present
(Alt
) loop
10822 Make_Or_Else
(Sloc
(Alt
),
10823 Left_Opnd
=> Make_Cond
(Alt
),
10824 Right_Opnd
=> Res
);
10829 Analyze_And_Resolve
(N
, Standard_Boolean
);
10830 end Expand_Set_Membership
;
10832 -----------------------------------
10833 -- Expand_Short_Circuit_Operator --
10834 -----------------------------------
10836 -- Deal with special expansion if actions are present for the right operand
10837 -- and deal with optimizing case of arguments being True or False. We also
10838 -- deal with the special case of non-standard boolean values.
10840 procedure Expand_Short_Circuit_Operator
(N
: Node_Id
) is
10841 Loc
: constant Source_Ptr
:= Sloc
(N
);
10842 Typ
: constant Entity_Id
:= Etype
(N
);
10843 Left
: constant Node_Id
:= Left_Opnd
(N
);
10844 Right
: constant Node_Id
:= Right_Opnd
(N
);
10845 LocR
: constant Source_Ptr
:= Sloc
(Right
);
10848 Shortcut_Value
: constant Boolean := Nkind
(N
) = N_Or_Else
;
10849 Shortcut_Ent
: constant Entity_Id
:= Boolean_Literals
(Shortcut_Value
);
10850 -- If Left = Shortcut_Value then Right need not be evaluated
10852 function Make_Test_Expr
(Opnd
: Node_Id
) return Node_Id
;
10853 -- For Opnd a boolean expression, return a Boolean expression equivalent
10854 -- to Opnd /= Shortcut_Value.
10856 --------------------
10857 -- Make_Test_Expr --
10858 --------------------
10860 function Make_Test_Expr
(Opnd
: Node_Id
) return Node_Id
is
10862 if Shortcut_Value
then
10863 return Make_Op_Not
(Sloc
(Opnd
), Opnd
);
10867 end Make_Test_Expr
;
10869 Op_Var
: Entity_Id
;
10870 -- Entity for a temporary variable holding the value of the operator,
10871 -- used for expansion in the case where actions are present.
10873 -- Start of processing for Expand_Short_Circuit_Operator
10876 -- Deal with non-standard booleans
10878 if Is_Boolean_Type
(Typ
) then
10879 Adjust_Condition
(Left
);
10880 Adjust_Condition
(Right
);
10881 Set_Etype
(N
, Standard_Boolean
);
10884 -- Check for cases where left argument is known to be True or False
10886 if Compile_Time_Known_Value
(Left
) then
10888 -- Mark SCO for left condition as compile time known
10890 if Generate_SCO
and then Comes_From_Source
(Left
) then
10891 Set_SCO_Condition
(Left
, Expr_Value_E
(Left
) = Standard_True
);
10894 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
10895 -- Any actions associated with Right will be executed unconditionally
10896 -- and can thus be inserted into the tree unconditionally.
10898 if Expr_Value_E
(Left
) /= Shortcut_Ent
then
10899 if Present
(Actions
(N
)) then
10900 Insert_Actions
(N
, Actions
(N
));
10903 Rewrite
(N
, Right
);
10905 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
10906 -- In this case we can forget the actions associated with Right,
10907 -- since they will never be executed.
10910 Kill_Dead_Code
(Right
);
10911 Kill_Dead_Code
(Actions
(N
));
10912 Rewrite
(N
, New_Occurrence_Of
(Shortcut_Ent
, Loc
));
10915 Adjust_Result_Type
(N
, Typ
);
10919 -- If Actions are present for the right operand, we have to do some
10920 -- special processing. We can't just let these actions filter back into
10921 -- code preceding the short circuit (which is what would have happened
10922 -- if we had not trapped them in the short-circuit form), since they
10923 -- must only be executed if the right operand of the short circuit is
10924 -- executed and not otherwise.
10926 -- the temporary variable C.
10928 if Present
(Actions
(N
)) then
10929 Actlist
:= Actions
(N
);
10931 -- The old approach is to expand:
10933 -- left AND THEN right
10937 -- C : Boolean := False;
10945 -- and finally rewrite the operator into a reference to C. Similarly
10946 -- for left OR ELSE right, with negated values. Note that this
10947 -- rewrite causes some difficulties for coverage analysis because
10948 -- of the introduction of the new variable C, which obscures the
10949 -- structure of the test.
10951 -- We use this "old approach" if use of N_Expression_With_Actions
10952 -- is False (see description in Opt of when this is or is not set).
10954 if not Use_Expression_With_Actions
then
10955 Op_Var
:= Make_Temporary
(Loc
, 'C', Related_Node
=> N
);
10958 Make_Object_Declaration
(Loc
,
10959 Defining_Identifier
=>
10961 Object_Definition
=>
10962 New_Occurrence_Of
(Standard_Boolean
, Loc
),
10964 New_Occurrence_Of
(Shortcut_Ent
, Loc
)));
10966 Append_To
(Actlist
,
10967 Make_Implicit_If_Statement
(Right
,
10968 Condition
=> Make_Test_Expr
(Right
),
10969 Then_Statements
=> New_List
(
10970 Make_Assignment_Statement
(LocR
,
10971 Name
=> New_Occurrence_Of
(Op_Var
, LocR
),
10974 (Boolean_Literals
(not Shortcut_Value
), LocR
)))));
10977 Make_Implicit_If_Statement
(Left
,
10978 Condition
=> Make_Test_Expr
(Left
),
10979 Then_Statements
=> Actlist
));
10981 Rewrite
(N
, New_Occurrence_Of
(Op_Var
, Loc
));
10982 Analyze_And_Resolve
(N
, Standard_Boolean
);
10984 -- The new approach, activated for now by the use of debug flag
10985 -- -gnatd.X is to use the new Expression_With_Actions node for the
10986 -- right operand of the short-circuit form. This should solve the
10987 -- traceability problems for coverage analysis.
10991 Make_Expression_With_Actions
(LocR
,
10992 Expression
=> Relocate_Node
(Right
),
10993 Actions
=> Actlist
));
10994 Set_Actions
(N
, No_List
);
10995 Analyze_And_Resolve
(Right
, Standard_Boolean
);
10998 Adjust_Result_Type
(N
, Typ
);
11002 -- No actions present, check for cases of right argument True/False
11004 if Compile_Time_Known_Value
(Right
) then
11006 -- Mark SCO for left condition as compile time known
11008 if Generate_SCO
and then Comes_From_Source
(Right
) then
11009 Set_SCO_Condition
(Right
, Expr_Value_E
(Right
) = Standard_True
);
11012 -- Change (Left and then True), (Left or else False) to Left.
11013 -- Note that we know there are no actions associated with the right
11014 -- operand, since we just checked for this case above.
11016 if Expr_Value_E
(Right
) /= Shortcut_Ent
then
11019 -- Change (Left and then False), (Left or else True) to Right,
11020 -- making sure to preserve any side effects associated with the Left
11024 Remove_Side_Effects
(Left
);
11025 Rewrite
(N
, New_Occurrence_Of
(Shortcut_Ent
, Loc
));
11029 Adjust_Result_Type
(N
, Typ
);
11030 end Expand_Short_Circuit_Operator
;
11032 -------------------------------------
11033 -- Fixup_Universal_Fixed_Operation --
11034 -------------------------------------
11036 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
) is
11037 Conv
: constant Node_Id
:= Parent
(N
);
11040 -- We must have a type conversion immediately above us
11042 pragma Assert
(Nkind
(Conv
) = N_Type_Conversion
);
11044 -- Normally the type conversion gives our target type. The exception
11045 -- occurs in the case of the Round attribute, where the conversion
11046 -- will be to universal real, and our real type comes from the Round
11047 -- attribute (as well as an indication that we must round the result)
11049 if Nkind
(Parent
(Conv
)) = N_Attribute_Reference
11050 and then Attribute_Name
(Parent
(Conv
)) = Name_Round
11052 Set_Etype
(N
, Etype
(Parent
(Conv
)));
11053 Set_Rounded_Result
(N
);
11055 -- Normal case where type comes from conversion above us
11058 Set_Etype
(N
, Etype
(Conv
));
11060 end Fixup_Universal_Fixed_Operation
;
11062 ---------------------------------
11063 -- Has_Inferable_Discriminants --
11064 ---------------------------------
11066 function Has_Inferable_Discriminants
(N
: Node_Id
) return Boolean is
11068 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean;
11069 -- Determines whether the left-most prefix of a selected component is a
11070 -- formal parameter in a subprogram. Assumes N is a selected component.
11072 --------------------------------
11073 -- Prefix_Is_Formal_Parameter --
11074 --------------------------------
11076 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean is
11077 Sel_Comp
: Node_Id
;
11080 -- Move to the left-most prefix by climbing up the tree
11083 while Present
(Parent
(Sel_Comp
))
11084 and then Nkind
(Parent
(Sel_Comp
)) = N_Selected_Component
11086 Sel_Comp
:= Parent
(Sel_Comp
);
11089 return Ekind
(Entity
(Prefix
(Sel_Comp
))) in Formal_Kind
;
11090 end Prefix_Is_Formal_Parameter
;
11092 -- Start of processing for Has_Inferable_Discriminants
11095 -- For selected components, the subtype of the selector must be a
11096 -- constrained Unchecked_Union. If the component is subject to a
11097 -- per-object constraint, then the enclosing object must have inferable
11100 if Nkind
(N
) = N_Selected_Component
then
11101 if Has_Per_Object_Constraint
(Entity
(Selector_Name
(N
))) then
11103 -- A small hack. If we have a per-object constrained selected
11104 -- component of a formal parameter, return True since we do not
11105 -- know the actual parameter association yet.
11107 if Prefix_Is_Formal_Parameter
(N
) then
11110 -- Otherwise, check the enclosing object and the selector
11113 return Has_Inferable_Discriminants
(Prefix
(N
))
11114 and then Has_Inferable_Discriminants
(Selector_Name
(N
));
11117 -- The call to Has_Inferable_Discriminants will determine whether
11118 -- the selector has a constrained Unchecked_Union nominal type.
11121 return Has_Inferable_Discriminants
(Selector_Name
(N
));
11124 -- A qualified expression has inferable discriminants if its subtype
11125 -- mark is a constrained Unchecked_Union subtype.
11127 elsif Nkind
(N
) = N_Qualified_Expression
then
11128 return Is_Unchecked_Union
(Etype
(Subtype_Mark
(N
)))
11129 and then Is_Constrained
(Etype
(Subtype_Mark
(N
)));
11131 -- For all other names, it is sufficient to have a constrained
11132 -- Unchecked_Union nominal subtype.
11135 return Is_Unchecked_Union
(Base_Type
(Etype
(N
)))
11136 and then Is_Constrained
(Etype
(N
));
11138 end Has_Inferable_Discriminants
;
11140 -------------------------------
11141 -- Insert_Dereference_Action --
11142 -------------------------------
11144 procedure Insert_Dereference_Action
(N
: Node_Id
) is
11146 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean;
11147 -- Return true if type of P is derived from Checked_Pool;
11149 -----------------------------
11150 -- Is_Checked_Storage_Pool --
11151 -----------------------------
11153 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean is
11162 while T
/= Etype
(T
) loop
11163 if Is_RTE
(T
, RE_Checked_Pool
) then
11171 end Is_Checked_Storage_Pool
;
11175 Typ
: constant Entity_Id
:= Etype
(N
);
11176 Desig
: constant Entity_Id
:= Available_View
(Designated_Type
(Typ
));
11177 Loc
: constant Source_Ptr
:= Sloc
(N
);
11178 Pool
: constant Entity_Id
:= Associated_Storage_Pool
(Typ
);
11179 Pnod
: constant Node_Id
:= Parent
(N
);
11187 -- Start of processing for Insert_Dereference_Action
11190 pragma Assert
(Nkind
(Pnod
) = N_Explicit_Dereference
);
11192 -- Do not re-expand a dereference which has already been processed by
11195 if Has_Dereference_Action
(Pnod
) then
11198 -- Do not perform this type of expansion for internally-generated
11201 elsif not Comes_From_Source
(Original_Node
(Pnod
)) then
11204 -- A dereference action is only applicable to objects which have been
11205 -- allocated on a checked pool.
11207 elsif not Is_Checked_Storage_Pool
(Pool
) then
11211 -- Extract the address of the dereferenced object. Generate:
11213 -- Addr : System.Address := <N>'Pool_Address;
11215 Addr
:= Make_Temporary
(Loc
, 'P');
11218 Make_Object_Declaration
(Loc
,
11219 Defining_Identifier
=> Addr
,
11220 Object_Definition
=>
11221 New_Reference_To
(RTE
(RE_Address
), Loc
),
11223 Make_Attribute_Reference
(Loc
,
11224 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
),
11225 Attribute_Name
=> Name_Pool_Address
)));
11227 -- Calculate the size of the dereferenced object. Generate:
11229 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
11232 Make_Explicit_Dereference
(Loc
,
11233 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
11234 Set_Has_Dereference_Action
(Deref
);
11236 Size
:= Make_Temporary
(Loc
, 'S');
11239 Make_Object_Declaration
(Loc
,
11240 Defining_Identifier
=> Size
,
11242 Object_Definition
=>
11243 New_Reference_To
(RTE
(RE_Storage_Count
), Loc
),
11246 Make_Op_Divide
(Loc
,
11248 Make_Attribute_Reference
(Loc
,
11250 Attribute_Name
=> Name_Size
),
11252 Make_Integer_Literal
(Loc
, System_Storage_Unit
))));
11254 -- Calculate the alignment of the dereferenced object. Generate:
11255 -- Alig : constant Storage_Count := <N>.all'Alignment;
11258 Make_Explicit_Dereference
(Loc
,
11259 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
11260 Set_Has_Dereference_Action
(Deref
);
11262 Alig
:= Make_Temporary
(Loc
, 'A');
11265 Make_Object_Declaration
(Loc
,
11266 Defining_Identifier
=> Alig
,
11267 Object_Definition
=>
11268 New_Reference_To
(RTE
(RE_Storage_Count
), Loc
),
11270 Make_Attribute_Reference
(Loc
,
11272 Attribute_Name
=> Name_Alignment
)));
11274 -- A dereference of a controlled object requires special processing. The
11275 -- finalization machinery requests additional space from the underlying
11276 -- pool to allocate and hide two pointers. As a result, a checked pool
11277 -- may mark the wrong memory as valid. Since checked pools do not have
11278 -- knowledge of hidden pointers, we have to bring the two pointers back
11279 -- in view in order to restore the original state of the object.
11281 if Needs_Finalization
(Desig
) then
11283 -- Adjust the address and size of the dereferenced object. Generate:
11284 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
11287 Make_Procedure_Call_Statement
(Loc
,
11289 New_Reference_To
(RTE
(RE_Adjust_Controlled_Dereference
), Loc
),
11290 Parameter_Associations
=> New_List
(
11291 New_Reference_To
(Addr
, Loc
),
11292 New_Reference_To
(Size
, Loc
),
11293 New_Reference_To
(Alig
, Loc
)));
11295 -- Class-wide types complicate things because we cannot determine
11296 -- statically whether the actual object is truly controlled. We must
11297 -- generate a runtime check to detect this property. Generate:
11299 -- if Needs_Finalization (<N>.all'Tag) then
11303 if Is_Class_Wide_Type
(Desig
) then
11305 Make_Explicit_Dereference
(Loc
,
11306 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
11307 Set_Has_Dereference_Action
(Deref
);
11310 Make_If_Statement
(Loc
,
11312 Make_Function_Call
(Loc
,
11314 New_Reference_To
(RTE
(RE_Needs_Finalization
), Loc
),
11315 Parameter_Associations
=> New_List
(
11316 Make_Attribute_Reference
(Loc
,
11318 Attribute_Name
=> Name_Tag
))),
11319 Then_Statements
=> New_List
(Stmt
));
11322 Insert_Action
(N
, Stmt
);
11326 -- Dereference (Pool, Addr, Size, Alig);
11329 Make_Procedure_Call_Statement
(Loc
,
11332 (Find_Prim_Op
(Etype
(Pool
), Name_Dereference
), Loc
),
11333 Parameter_Associations
=> New_List
(
11334 New_Reference_To
(Pool
, Loc
),
11335 New_Reference_To
(Addr
, Loc
),
11336 New_Reference_To
(Size
, Loc
),
11337 New_Reference_To
(Alig
, Loc
))));
11339 -- Mark the explicit dereference as processed to avoid potential
11340 -- infinite expansion.
11342 Set_Has_Dereference_Action
(Pnod
);
11345 when RE_Not_Available
=>
11347 end Insert_Dereference_Action
;
11349 --------------------------------
11350 -- Integer_Promotion_Possible --
11351 --------------------------------
11353 function Integer_Promotion_Possible
(N
: Node_Id
) return Boolean is
11354 Operand
: constant Node_Id
:= Expression
(N
);
11355 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
11356 Root_Operand_Type
: constant Entity_Id
:= Root_Type
(Operand_Type
);
11359 pragma Assert
(Nkind
(N
) = N_Type_Conversion
);
11363 -- We only do the transformation for source constructs. We assume
11364 -- that the expander knows what it is doing when it generates code.
11366 Comes_From_Source
(N
)
11368 -- If the operand type is Short_Integer or Short_Short_Integer,
11369 -- then we will promote to Integer, which is available on all
11370 -- targets, and is sufficient to ensure no intermediate overflow.
11371 -- Furthermore it is likely to be as efficient or more efficient
11372 -- than using the smaller type for the computation so we do this
11373 -- unconditionally.
11376 (Root_Operand_Type
= Base_Type
(Standard_Short_Integer
)
11378 Root_Operand_Type
= Base_Type
(Standard_Short_Short_Integer
))
11380 -- Test for interesting operation, which includes addition,
11381 -- division, exponentiation, multiplication, subtraction, absolute
11382 -- value and unary negation. Unary "+" is omitted since it is a
11383 -- no-op and thus can't overflow.
11385 and then Nkind_In
(Operand
, N_Op_Abs
,
11392 end Integer_Promotion_Possible
;
11394 ------------------------------
11395 -- Make_Array_Comparison_Op --
11396 ------------------------------
11398 -- This is a hand-coded expansion of the following generic function:
11401 -- type elem is (<>);
11402 -- type index is (<>);
11403 -- type a is array (index range <>) of elem;
11405 -- function Gnnn (X : a; Y: a) return boolean is
11406 -- J : index := Y'first;
11409 -- if X'length = 0 then
11412 -- elsif Y'length = 0 then
11416 -- for I in X'range loop
11417 -- if X (I) = Y (J) then
11418 -- if J = Y'last then
11421 -- J := index'succ (J);
11425 -- return X (I) > Y (J);
11429 -- return X'length > Y'length;
11433 -- Note that since we are essentially doing this expansion by hand, we
11434 -- do not need to generate an actual or formal generic part, just the
11435 -- instantiated function itself.
11437 function Make_Array_Comparison_Op
11439 Nod
: Node_Id
) return Node_Id
11441 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
11443 X
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uX
);
11444 Y
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uY
);
11445 I
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uI
);
11446 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
11448 Index
: constant Entity_Id
:= Base_Type
(Etype
(First_Index
(Typ
)));
11450 Loop_Statement
: Node_Id
;
11451 Loop_Body
: Node_Id
;
11453 Inner_If
: Node_Id
;
11454 Final_Expr
: Node_Id
;
11455 Func_Body
: Node_Id
;
11456 Func_Name
: Entity_Id
;
11462 -- if J = Y'last then
11465 -- J := index'succ (J);
11469 Make_Implicit_If_Statement
(Nod
,
11472 Left_Opnd
=> New_Reference_To
(J
, Loc
),
11474 Make_Attribute_Reference
(Loc
,
11475 Prefix
=> New_Reference_To
(Y
, Loc
),
11476 Attribute_Name
=> Name_Last
)),
11478 Then_Statements
=> New_List
(
11479 Make_Exit_Statement
(Loc
)),
11483 Make_Assignment_Statement
(Loc
,
11484 Name
=> New_Reference_To
(J
, Loc
),
11486 Make_Attribute_Reference
(Loc
,
11487 Prefix
=> New_Reference_To
(Index
, Loc
),
11488 Attribute_Name
=> Name_Succ
,
11489 Expressions
=> New_List
(New_Reference_To
(J
, Loc
))))));
11491 -- if X (I) = Y (J) then
11494 -- return X (I) > Y (J);
11498 Make_Implicit_If_Statement
(Nod
,
11502 Make_Indexed_Component
(Loc
,
11503 Prefix
=> New_Reference_To
(X
, Loc
),
11504 Expressions
=> New_List
(New_Reference_To
(I
, Loc
))),
11507 Make_Indexed_Component
(Loc
,
11508 Prefix
=> New_Reference_To
(Y
, Loc
),
11509 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)))),
11511 Then_Statements
=> New_List
(Inner_If
),
11513 Else_Statements
=> New_List
(
11514 Make_Simple_Return_Statement
(Loc
,
11518 Make_Indexed_Component
(Loc
,
11519 Prefix
=> New_Reference_To
(X
, Loc
),
11520 Expressions
=> New_List
(New_Reference_To
(I
, Loc
))),
11523 Make_Indexed_Component
(Loc
,
11524 Prefix
=> New_Reference_To
(Y
, Loc
),
11525 Expressions
=> New_List
(
11526 New_Reference_To
(J
, Loc
)))))));
11528 -- for I in X'range loop
11533 Make_Implicit_Loop_Statement
(Nod
,
11534 Identifier
=> Empty
,
11536 Iteration_Scheme
=>
11537 Make_Iteration_Scheme
(Loc
,
11538 Loop_Parameter_Specification
=>
11539 Make_Loop_Parameter_Specification
(Loc
,
11540 Defining_Identifier
=> I
,
11541 Discrete_Subtype_Definition
=>
11542 Make_Attribute_Reference
(Loc
,
11543 Prefix
=> New_Reference_To
(X
, Loc
),
11544 Attribute_Name
=> Name_Range
))),
11546 Statements
=> New_List
(Loop_Body
));
11548 -- if X'length = 0 then
11550 -- elsif Y'length = 0 then
11553 -- for ... loop ... end loop;
11554 -- return X'length > Y'length;
11558 Make_Attribute_Reference
(Loc
,
11559 Prefix
=> New_Reference_To
(X
, Loc
),
11560 Attribute_Name
=> Name_Length
);
11563 Make_Attribute_Reference
(Loc
,
11564 Prefix
=> New_Reference_To
(Y
, Loc
),
11565 Attribute_Name
=> Name_Length
);
11569 Left_Opnd
=> Length1
,
11570 Right_Opnd
=> Length2
);
11573 Make_Implicit_If_Statement
(Nod
,
11577 Make_Attribute_Reference
(Loc
,
11578 Prefix
=> New_Reference_To
(X
, Loc
),
11579 Attribute_Name
=> Name_Length
),
11581 Make_Integer_Literal
(Loc
, 0)),
11585 Make_Simple_Return_Statement
(Loc
,
11586 Expression
=> New_Reference_To
(Standard_False
, Loc
))),
11588 Elsif_Parts
=> New_List
(
11589 Make_Elsif_Part
(Loc
,
11593 Make_Attribute_Reference
(Loc
,
11594 Prefix
=> New_Reference_To
(Y
, Loc
),
11595 Attribute_Name
=> Name_Length
),
11597 Make_Integer_Literal
(Loc
, 0)),
11601 Make_Simple_Return_Statement
(Loc
,
11602 Expression
=> New_Reference_To
(Standard_True
, Loc
))))),
11604 Else_Statements
=> New_List
(
11606 Make_Simple_Return_Statement
(Loc
,
11607 Expression
=> Final_Expr
)));
11611 Formals
:= New_List
(
11612 Make_Parameter_Specification
(Loc
,
11613 Defining_Identifier
=> X
,
11614 Parameter_Type
=> New_Reference_To
(Typ
, Loc
)),
11616 Make_Parameter_Specification
(Loc
,
11617 Defining_Identifier
=> Y
,
11618 Parameter_Type
=> New_Reference_To
(Typ
, Loc
)));
11620 -- function Gnnn (...) return boolean is
11621 -- J : index := Y'first;
11626 Func_Name
:= Make_Temporary
(Loc
, 'G');
11629 Make_Subprogram_Body
(Loc
,
11631 Make_Function_Specification
(Loc
,
11632 Defining_Unit_Name
=> Func_Name
,
11633 Parameter_Specifications
=> Formals
,
11634 Result_Definition
=> New_Reference_To
(Standard_Boolean
, Loc
)),
11636 Declarations
=> New_List
(
11637 Make_Object_Declaration
(Loc
,
11638 Defining_Identifier
=> J
,
11639 Object_Definition
=> New_Reference_To
(Index
, Loc
),
11641 Make_Attribute_Reference
(Loc
,
11642 Prefix
=> New_Reference_To
(Y
, Loc
),
11643 Attribute_Name
=> Name_First
))),
11645 Handled_Statement_Sequence
=>
11646 Make_Handled_Sequence_Of_Statements
(Loc
,
11647 Statements
=> New_List
(If_Stat
)));
11650 end Make_Array_Comparison_Op
;
11652 ---------------------------
11653 -- Make_Boolean_Array_Op --
11654 ---------------------------
11656 -- For logical operations on boolean arrays, expand in line the following,
11657 -- replacing 'and' with 'or' or 'xor' where needed:
11659 -- function Annn (A : typ; B: typ) return typ is
11662 -- for J in A'range loop
11663 -- C (J) := A (J) op B (J);
11668 -- Here typ is the boolean array type
11670 function Make_Boolean_Array_Op
11672 N
: Node_Id
) return Node_Id
11674 Loc
: constant Source_Ptr
:= Sloc
(N
);
11676 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
11677 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
11678 C
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uC
);
11679 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
11687 Func_Name
: Entity_Id
;
11688 Func_Body
: Node_Id
;
11689 Loop_Statement
: Node_Id
;
11693 Make_Indexed_Component
(Loc
,
11694 Prefix
=> New_Reference_To
(A
, Loc
),
11695 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
11698 Make_Indexed_Component
(Loc
,
11699 Prefix
=> New_Reference_To
(B
, Loc
),
11700 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
11703 Make_Indexed_Component
(Loc
,
11704 Prefix
=> New_Reference_To
(C
, Loc
),
11705 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
11707 if Nkind
(N
) = N_Op_And
then
11711 Right_Opnd
=> B_J
);
11713 elsif Nkind
(N
) = N_Op_Or
then
11717 Right_Opnd
=> B_J
);
11723 Right_Opnd
=> B_J
);
11727 Make_Implicit_Loop_Statement
(N
,
11728 Identifier
=> Empty
,
11730 Iteration_Scheme
=>
11731 Make_Iteration_Scheme
(Loc
,
11732 Loop_Parameter_Specification
=>
11733 Make_Loop_Parameter_Specification
(Loc
,
11734 Defining_Identifier
=> J
,
11735 Discrete_Subtype_Definition
=>
11736 Make_Attribute_Reference
(Loc
,
11737 Prefix
=> New_Reference_To
(A
, Loc
),
11738 Attribute_Name
=> Name_Range
))),
11740 Statements
=> New_List
(
11741 Make_Assignment_Statement
(Loc
,
11743 Expression
=> Op
)));
11745 Formals
:= New_List
(
11746 Make_Parameter_Specification
(Loc
,
11747 Defining_Identifier
=> A
,
11748 Parameter_Type
=> New_Reference_To
(Typ
, Loc
)),
11750 Make_Parameter_Specification
(Loc
,
11751 Defining_Identifier
=> B
,
11752 Parameter_Type
=> New_Reference_To
(Typ
, Loc
)));
11754 Func_Name
:= Make_Temporary
(Loc
, 'A');
11755 Set_Is_Inlined
(Func_Name
);
11758 Make_Subprogram_Body
(Loc
,
11760 Make_Function_Specification
(Loc
,
11761 Defining_Unit_Name
=> Func_Name
,
11762 Parameter_Specifications
=> Formals
,
11763 Result_Definition
=> New_Reference_To
(Typ
, Loc
)),
11765 Declarations
=> New_List
(
11766 Make_Object_Declaration
(Loc
,
11767 Defining_Identifier
=> C
,
11768 Object_Definition
=> New_Reference_To
(Typ
, Loc
))),
11770 Handled_Statement_Sequence
=>
11771 Make_Handled_Sequence_Of_Statements
(Loc
,
11772 Statements
=> New_List
(
11774 Make_Simple_Return_Statement
(Loc
,
11775 Expression
=> New_Reference_To
(C
, Loc
)))));
11778 end Make_Boolean_Array_Op
;
11780 -----------------------------------------
11781 -- Minimized_Eliminated_Overflow_Check --
11782 -----------------------------------------
11784 function Minimized_Eliminated_Overflow_Check
(N
: Node_Id
) return Boolean is
11787 Is_Signed_Integer_Type
(Etype
(N
))
11788 and then Overflow_Check_Mode
in Minimized_Or_Eliminated
;
11789 end Minimized_Eliminated_Overflow_Check
;
11791 --------------------------------
11792 -- Optimize_Length_Comparison --
11793 --------------------------------
11795 procedure Optimize_Length_Comparison
(N
: Node_Id
) is
11796 Loc
: constant Source_Ptr
:= Sloc
(N
);
11797 Typ
: constant Entity_Id
:= Etype
(N
);
11802 -- First and Last attribute reference nodes, which end up as left and
11803 -- right operands of the optimized result.
11806 -- True for comparison operand of zero
11809 -- Comparison operand, set only if Is_Zero is false
11812 -- Entity whose length is being compared
11815 -- Integer_Literal node for length attribute expression, or Empty
11816 -- if there is no such expression present.
11819 -- Type of array index to which 'Length is applied
11821 Op
: Node_Kind
:= Nkind
(N
);
11822 -- Kind of comparison operator, gets flipped if operands backwards
11824 function Is_Optimizable
(N
: Node_Id
) return Boolean;
11825 -- Tests N to see if it is an optimizable comparison value (defined as
11826 -- constant zero or one, or something else where the value is known to
11827 -- be positive and in the range of 32-bits, and where the corresponding
11828 -- Length value is also known to be 32-bits. If result is true, sets
11829 -- Is_Zero, Ityp, and Comp accordingly.
11831 function Is_Entity_Length
(N
: Node_Id
) return Boolean;
11832 -- Tests if N is a length attribute applied to a simple entity. If so,
11833 -- returns True, and sets Ent to the entity, and Index to the integer
11834 -- literal provided as an attribute expression, or to Empty if none.
11835 -- Also returns True if the expression is a generated type conversion
11836 -- whose expression is of the desired form. This latter case arises
11837 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
11838 -- to check for being in range, which is not needed in this context.
11839 -- Returns False if neither condition holds.
11841 function Prepare_64
(N
: Node_Id
) return Node_Id
;
11842 -- Given a discrete expression, returns a Long_Long_Integer typed
11843 -- expression representing the underlying value of the expression.
11844 -- This is done with an unchecked conversion to the result type. We
11845 -- use unchecked conversion to handle the enumeration type case.
11847 ----------------------
11848 -- Is_Entity_Length --
11849 ----------------------
11851 function Is_Entity_Length
(N
: Node_Id
) return Boolean is
11853 if Nkind
(N
) = N_Attribute_Reference
11854 and then Attribute_Name
(N
) = Name_Length
11855 and then Is_Entity_Name
(Prefix
(N
))
11857 Ent
:= Entity
(Prefix
(N
));
11859 if Present
(Expressions
(N
)) then
11860 Index
:= First
(Expressions
(N
));
11867 elsif Nkind
(N
) = N_Type_Conversion
11868 and then not Comes_From_Source
(N
)
11870 return Is_Entity_Length
(Expression
(N
));
11875 end Is_Entity_Length
;
11877 --------------------
11878 -- Is_Optimizable --
11879 --------------------
11881 function Is_Optimizable
(N
: Node_Id
) return Boolean is
11889 if Compile_Time_Known_Value
(N
) then
11890 Val
:= Expr_Value
(N
);
11892 if Val
= Uint_0
then
11897 elsif Val
= Uint_1
then
11904 -- Here we have to make sure of being within 32-bits
11906 Determine_Range
(N
, OK
, Lo
, Hi
, Assume_Valid
=> True);
11909 or else Lo
< Uint_1
11910 or else Hi
> UI_From_Int
(Int
'Last)
11915 -- Comparison value was within range, so now we must check the index
11916 -- value to make sure it is also within 32-bits.
11918 Indx
:= First_Index
(Etype
(Ent
));
11920 if Present
(Index
) then
11921 for J
in 2 .. UI_To_Int
(Intval
(Index
)) loop
11926 Ityp
:= Etype
(Indx
);
11928 if Esize
(Ityp
) > 32 then
11935 end Is_Optimizable
;
11941 function Prepare_64
(N
: Node_Id
) return Node_Id
is
11943 return Unchecked_Convert_To
(Standard_Long_Long_Integer
, N
);
11946 -- Start of processing for Optimize_Length_Comparison
11949 -- Nothing to do if not a comparison
11951 if Op
not in N_Op_Compare
then
11955 -- Nothing to do if special -gnatd.P debug flag set
11957 if Debug_Flag_Dot_PP
then
11961 -- Ent'Length op 0/1
11963 if Is_Entity_Length
(Left_Opnd
(N
))
11964 and then Is_Optimizable
(Right_Opnd
(N
))
11968 -- 0/1 op Ent'Length
11970 elsif Is_Entity_Length
(Right_Opnd
(N
))
11971 and then Is_Optimizable
(Left_Opnd
(N
))
11973 -- Flip comparison to opposite sense
11976 when N_Op_Lt
=> Op
:= N_Op_Gt
;
11977 when N_Op_Le
=> Op
:= N_Op_Ge
;
11978 when N_Op_Gt
=> Op
:= N_Op_Lt
;
11979 when N_Op_Ge
=> Op
:= N_Op_Le
;
11980 when others => null;
11983 -- Else optimization not possible
11989 -- Fall through if we will do the optimization
11991 -- Cases to handle:
11993 -- X'Length = 0 => X'First > X'Last
11994 -- X'Length = 1 => X'First = X'Last
11995 -- X'Length = n => X'First + (n - 1) = X'Last
11997 -- X'Length /= 0 => X'First <= X'Last
11998 -- X'Length /= 1 => X'First /= X'Last
11999 -- X'Length /= n => X'First + (n - 1) /= X'Last
12001 -- X'Length >= 0 => always true, warn
12002 -- X'Length >= 1 => X'First <= X'Last
12003 -- X'Length >= n => X'First + (n - 1) <= X'Last
12005 -- X'Length > 0 => X'First <= X'Last
12006 -- X'Length > 1 => X'First < X'Last
12007 -- X'Length > n => X'First + (n - 1) < X'Last
12009 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
12010 -- X'Length <= 1 => X'First >= X'Last
12011 -- X'Length <= n => X'First + (n - 1) >= X'Last
12013 -- X'Length < 0 => always false (warn)
12014 -- X'Length < 1 => X'First > X'Last
12015 -- X'Length < n => X'First + (n - 1) > X'Last
12017 -- Note: for the cases of n (not constant 0,1), we require that the
12018 -- corresponding index type be integer or shorter (i.e. not 64-bit),
12019 -- and the same for the comparison value. Then we do the comparison
12020 -- using 64-bit arithmetic (actually long long integer), so that we
12021 -- cannot have overflow intefering with the result.
12023 -- First deal with warning cases
12032 Convert_To
(Typ
, New_Occurrence_Of
(Standard_True
, Loc
)));
12033 Analyze_And_Resolve
(N
, Typ
);
12034 Warn_On_Known_Condition
(N
);
12041 Convert_To
(Typ
, New_Occurrence_Of
(Standard_False
, Loc
)));
12042 Analyze_And_Resolve
(N
, Typ
);
12043 Warn_On_Known_Condition
(N
);
12047 if Constant_Condition_Warnings
12048 and then Comes_From_Source
(Original_Node
(N
))
12050 Error_Msg_N
("could replace by ""'=""?", N
);
12060 -- Build the First reference we will use
12063 Make_Attribute_Reference
(Loc
,
12064 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
12065 Attribute_Name
=> Name_First
);
12067 if Present
(Index
) then
12068 Set_Expressions
(Left
, New_List
(New_Copy
(Index
)));
12071 -- If general value case, then do the addition of (n - 1), and
12072 -- also add the needed conversions to type Long_Long_Integer.
12074 if Present
(Comp
) then
12077 Left_Opnd
=> Prepare_64
(Left
),
12079 Make_Op_Subtract
(Loc
,
12080 Left_Opnd
=> Prepare_64
(Comp
),
12081 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)));
12084 -- Build the Last reference we will use
12087 Make_Attribute_Reference
(Loc
,
12088 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
12089 Attribute_Name
=> Name_Last
);
12091 if Present
(Index
) then
12092 Set_Expressions
(Right
, New_List
(New_Copy
(Index
)));
12095 -- If general operand, convert Last reference to Long_Long_Integer
12097 if Present
(Comp
) then
12098 Right
:= Prepare_64
(Right
);
12101 -- Check for cases to optimize
12103 -- X'Length = 0 => X'First > X'Last
12104 -- X'Length < 1 => X'First > X'Last
12105 -- X'Length < n => X'First + (n - 1) > X'Last
12107 if (Is_Zero
and then Op
= N_Op_Eq
)
12108 or else (not Is_Zero
and then Op
= N_Op_Lt
)
12113 Right_Opnd
=> Right
);
12115 -- X'Length = 1 => X'First = X'Last
12116 -- X'Length = n => X'First + (n - 1) = X'Last
12118 elsif not Is_Zero
and then Op
= N_Op_Eq
then
12122 Right_Opnd
=> Right
);
12124 -- X'Length /= 0 => X'First <= X'Last
12125 -- X'Length > 0 => X'First <= X'Last
12127 elsif Is_Zero
and (Op
= N_Op_Ne
or else Op
= N_Op_Gt
) then
12131 Right_Opnd
=> Right
);
12133 -- X'Length /= 1 => X'First /= X'Last
12134 -- X'Length /= n => X'First + (n - 1) /= X'Last
12136 elsif not Is_Zero
and then Op
= N_Op_Ne
then
12140 Right_Opnd
=> Right
);
12142 -- X'Length >= 1 => X'First <= X'Last
12143 -- X'Length >= n => X'First + (n - 1) <= X'Last
12145 elsif not Is_Zero
and then Op
= N_Op_Ge
then
12149 Right_Opnd
=> Right
);
12151 -- X'Length > 1 => X'First < X'Last
12152 -- X'Length > n => X'First + (n = 1) < X'Last
12154 elsif not Is_Zero
and then Op
= N_Op_Gt
then
12158 Right_Opnd
=> Right
);
12160 -- X'Length <= 1 => X'First >= X'Last
12161 -- X'Length <= n => X'First + (n - 1) >= X'Last
12163 elsif not Is_Zero
and then Op
= N_Op_Le
then
12167 Right_Opnd
=> Right
);
12169 -- Should not happen at this stage
12172 raise Program_Error
;
12175 -- Rewrite and finish up
12177 Rewrite
(N
, Result
);
12178 Analyze_And_Resolve
(N
, Typ
);
12180 end Optimize_Length_Comparison
;
12182 ------------------------
12183 -- Rewrite_Comparison --
12184 ------------------------
12186 procedure Rewrite_Comparison
(N
: Node_Id
) is
12187 Warning_Generated
: Boolean := False;
12188 -- Set to True if first pass with Assume_Valid generates a warning in
12189 -- which case we skip the second pass to avoid warning overloaded.
12192 -- Set to Standard_True or Standard_False
12195 if Nkind
(N
) = N_Type_Conversion
then
12196 Rewrite_Comparison
(Expression
(N
));
12199 elsif Nkind
(N
) not in N_Op_Compare
then
12203 -- Now start looking at the comparison in detail. We potentially go
12204 -- through this loop twice. The first time, Assume_Valid is set False
12205 -- in the call to Compile_Time_Compare. If this call results in a
12206 -- clear result of always True or Always False, that's decisive and
12207 -- we are done. Otherwise we repeat the processing with Assume_Valid
12208 -- set to True to generate additional warnings. We can skip that step
12209 -- if Constant_Condition_Warnings is False.
12211 for AV
in False .. True loop
12213 Typ
: constant Entity_Id
:= Etype
(N
);
12214 Op1
: constant Node_Id
:= Left_Opnd
(N
);
12215 Op2
: constant Node_Id
:= Right_Opnd
(N
);
12217 Res
: constant Compare_Result
:=
12218 Compile_Time_Compare
(Op1
, Op2
, Assume_Valid
=> AV
);
12219 -- Res indicates if compare outcome can be compile time determined
12221 True_Result
: Boolean;
12222 False_Result
: Boolean;
12225 case N_Op_Compare
(Nkind
(N
)) is
12227 True_Result
:= Res
= EQ
;
12228 False_Result
:= Res
= LT
or else Res
= GT
or else Res
= NE
;
12231 True_Result
:= Res
in Compare_GE
;
12232 False_Result
:= Res
= LT
;
12235 and then Constant_Condition_Warnings
12236 and then Comes_From_Source
(Original_Node
(N
))
12237 and then Nkind
(Original_Node
(N
)) = N_Op_Ge
12238 and then not In_Instance
12239 and then Is_Integer_Type
(Etype
(Left_Opnd
(N
)))
12240 and then not Has_Warnings_Off
(Etype
(Left_Opnd
(N
)))
12243 ("can never be greater than, could replace by ""'=""?", N
);
12244 Warning_Generated
:= True;
12248 True_Result
:= Res
= GT
;
12249 False_Result
:= Res
in Compare_LE
;
12252 True_Result
:= Res
= LT
;
12253 False_Result
:= Res
in Compare_GE
;
12256 True_Result
:= Res
in Compare_LE
;
12257 False_Result
:= Res
= GT
;
12260 and then Constant_Condition_Warnings
12261 and then Comes_From_Source
(Original_Node
(N
))
12262 and then Nkind
(Original_Node
(N
)) = N_Op_Le
12263 and then not In_Instance
12264 and then Is_Integer_Type
(Etype
(Left_Opnd
(N
)))
12265 and then not Has_Warnings_Off
(Etype
(Left_Opnd
(N
)))
12268 ("can never be less than, could replace by ""'=""?", N
);
12269 Warning_Generated
:= True;
12273 True_Result
:= Res
= NE
or else Res
= GT
or else Res
= LT
;
12274 False_Result
:= Res
= EQ
;
12277 -- If this is the first iteration, then we actually convert the
12278 -- comparison into True or False, if the result is certain.
12281 if True_Result
or False_Result
then
12282 Result
:= Boolean_Literals
(True_Result
);
12285 New_Occurrence_Of
(Result
, Sloc
(N
))));
12286 Analyze_And_Resolve
(N
, Typ
);
12287 Warn_On_Known_Condition
(N
);
12291 -- If this is the second iteration (AV = True), and the original
12292 -- node comes from source and we are not in an instance, then give
12293 -- a warning if we know result would be True or False. Note: we
12294 -- know Constant_Condition_Warnings is set if we get here.
12296 elsif Comes_From_Source
(Original_Node
(N
))
12297 and then not In_Instance
12299 if True_Result
then
12301 ("condition can only be False if invalid values present?",
12303 elsif False_Result
then
12305 ("condition can only be True if invalid values present?",
12311 -- Skip second iteration if not warning on constant conditions or
12312 -- if the first iteration already generated a warning of some kind or
12313 -- if we are in any case assuming all values are valid (so that the
12314 -- first iteration took care of the valid case).
12316 exit when not Constant_Condition_Warnings
;
12317 exit when Warning_Generated
;
12318 exit when Assume_No_Invalid_Values
;
12320 end Rewrite_Comparison
;
12322 ----------------------------
12323 -- Safe_In_Place_Array_Op --
12324 ----------------------------
12326 function Safe_In_Place_Array_Op
12329 Op2
: Node_Id
) return Boolean
12331 Target
: Entity_Id
;
12333 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean;
12334 -- Operand is safe if it cannot overlap part of the target of the
12335 -- operation. If the operand and the target are identical, the operand
12336 -- is safe. The operand can be empty in the case of negation.
12338 function Is_Unaliased
(N
: Node_Id
) return Boolean;
12339 -- Check that N is a stand-alone entity
12345 function Is_Unaliased
(N
: Node_Id
) return Boolean is
12349 and then No
(Address_Clause
(Entity
(N
)))
12350 and then No
(Renamed_Object
(Entity
(N
)));
12353 ---------------------
12354 -- Is_Safe_Operand --
12355 ---------------------
12357 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean is
12362 elsif Is_Entity_Name
(Op
) then
12363 return Is_Unaliased
(Op
);
12365 elsif Nkind_In
(Op
, N_Indexed_Component
, N_Selected_Component
) then
12366 return Is_Unaliased
(Prefix
(Op
));
12368 elsif Nkind
(Op
) = N_Slice
then
12370 Is_Unaliased
(Prefix
(Op
))
12371 and then Entity
(Prefix
(Op
)) /= Target
;
12373 elsif Nkind
(Op
) = N_Op_Not
then
12374 return Is_Safe_Operand
(Right_Opnd
(Op
));
12379 end Is_Safe_Operand
;
12381 -- Start of processing for Safe_In_Place_Array_Op
12384 -- Skip this processing if the component size is different from system
12385 -- storage unit (since at least for NOT this would cause problems).
12387 if Component_Size
(Etype
(Lhs
)) /= System_Storage_Unit
then
12390 -- Cannot do in place stuff on VM_Target since cannot pass addresses
12392 elsif VM_Target
/= No_VM
then
12395 -- Cannot do in place stuff if non-standard Boolean representation
12397 elsif Has_Non_Standard_Rep
(Component_Type
(Etype
(Lhs
))) then
12400 elsif not Is_Unaliased
(Lhs
) then
12404 Target
:= Entity
(Lhs
);
12405 return Is_Safe_Operand
(Op1
) and then Is_Safe_Operand
(Op2
);
12407 end Safe_In_Place_Array_Op
;
12409 -----------------------
12410 -- Tagged_Membership --
12411 -----------------------
12413 -- There are two different cases to consider depending on whether the right
12414 -- operand is a class-wide type or not. If not we just compare the actual
12415 -- tag of the left expr to the target type tag:
12417 -- Left_Expr.Tag = Right_Type'Tag;
12419 -- If it is a class-wide type we use the RT function CW_Membership which is
12420 -- usually implemented by looking in the ancestor tables contained in the
12421 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
12423 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
12424 -- function IW_Membership which is usually implemented by looking in the
12425 -- table of abstract interface types plus the ancestor table contained in
12426 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
12428 procedure Tagged_Membership
12430 SCIL_Node
: out Node_Id
;
12431 Result
: out Node_Id
)
12433 Left
: constant Node_Id
:= Left_Opnd
(N
);
12434 Right
: constant Node_Id
:= Right_Opnd
(N
);
12435 Loc
: constant Source_Ptr
:= Sloc
(N
);
12437 Full_R_Typ
: Entity_Id
;
12438 Left_Type
: Entity_Id
;
12439 New_Node
: Node_Id
;
12440 Right_Type
: Entity_Id
;
12444 SCIL_Node
:= Empty
;
12446 -- Handle entities from the limited view
12448 Left_Type
:= Available_View
(Etype
(Left
));
12449 Right_Type
:= Available_View
(Etype
(Right
));
12451 -- In the case where the type is an access type, the test is applied
12452 -- using the designated types (needed in Ada 2012 for implicit anonymous
12453 -- access conversions, for AI05-0149).
12455 if Is_Access_Type
(Right_Type
) then
12456 Left_Type
:= Designated_Type
(Left_Type
);
12457 Right_Type
:= Designated_Type
(Right_Type
);
12460 if Is_Class_Wide_Type
(Left_Type
) then
12461 Left_Type
:= Root_Type
(Left_Type
);
12464 if Is_Class_Wide_Type
(Right_Type
) then
12465 Full_R_Typ
:= Underlying_Type
(Root_Type
(Right_Type
));
12467 Full_R_Typ
:= Underlying_Type
(Right_Type
);
12471 Make_Selected_Component
(Loc
,
12472 Prefix
=> Relocate_Node
(Left
),
12474 New_Reference_To
(First_Tag_Component
(Left_Type
), Loc
));
12476 if Is_Class_Wide_Type
(Right_Type
) then
12478 -- No need to issue a run-time check if we statically know that the
12479 -- result of this membership test is always true. For example,
12480 -- considering the following declarations:
12482 -- type Iface is interface;
12483 -- type T is tagged null record;
12484 -- type DT is new T and Iface with null record;
12489 -- These membership tests are always true:
12492 -- Obj2 in T'Class;
12493 -- Obj2 in Iface'Class;
12495 -- We do not need to handle cases where the membership is illegal.
12498 -- Obj1 in DT'Class; -- Compile time error
12499 -- Obj1 in Iface'Class; -- Compile time error
12501 if not Is_Class_Wide_Type
(Left_Type
)
12502 and then (Is_Ancestor
(Etype
(Right_Type
), Left_Type
,
12503 Use_Full_View
=> True)
12504 or else (Is_Interface
(Etype
(Right_Type
))
12505 and then Interface_Present_In_Ancestor
12507 Iface
=> Etype
(Right_Type
))))
12509 Result
:= New_Reference_To
(Standard_True
, Loc
);
12513 -- Ada 2005 (AI-251): Class-wide applied to interfaces
12515 if Is_Interface
(Etype
(Class_Wide_Type
(Right_Type
)))
12517 -- Support to: "Iface_CW_Typ in Typ'Class"
12519 or else Is_Interface
(Left_Type
)
12521 -- Issue error if IW_Membership operation not available in a
12522 -- configurable run time setting.
12524 if not RTE_Available
(RE_IW_Membership
) then
12526 ("dynamic membership test on interface types", N
);
12532 Make_Function_Call
(Loc
,
12533 Name
=> New_Occurrence_Of
(RTE
(RE_IW_Membership
), Loc
),
12534 Parameter_Associations
=> New_List
(
12535 Make_Attribute_Reference
(Loc
,
12537 Attribute_Name
=> Name_Address
),
12539 Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))),
12542 -- Ada 95: Normal case
12545 Build_CW_Membership
(Loc
,
12546 Obj_Tag_Node
=> Obj_Tag
,
12549 Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))), Loc
),
12551 New_Node
=> New_Node
);
12553 -- Generate the SCIL node for this class-wide membership test.
12554 -- Done here because the previous call to Build_CW_Membership
12555 -- relocates Obj_Tag.
12557 if Generate_SCIL
then
12558 SCIL_Node
:= Make_SCIL_Membership_Test
(Sloc
(N
));
12559 Set_SCIL_Entity
(SCIL_Node
, Etype
(Right_Type
));
12560 Set_SCIL_Tag_Value
(SCIL_Node
, Obj_Tag
);
12563 Result
:= New_Node
;
12566 -- Right_Type is not a class-wide type
12569 -- No need to check the tag of the object if Right_Typ is abstract
12571 if Is_Abstract_Type
(Right_Type
) then
12572 Result
:= New_Reference_To
(Standard_False
, Loc
);
12577 Left_Opnd
=> Obj_Tag
,
12580 (Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))), Loc
));
12583 end Tagged_Membership
;
12585 ------------------------------
12586 -- Unary_Op_Validity_Checks --
12587 ------------------------------
12589 procedure Unary_Op_Validity_Checks
(N
: Node_Id
) is
12591 if Validity_Checks_On
and Validity_Check_Operands
then
12592 Ensure_Valid
(Right_Opnd
(N
));
12594 end Unary_Op_Validity_Checks
;