1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Debug
; use Debug
;
29 with Einfo
; use Einfo
;
30 with Elists
; use Elists
;
31 with Errout
; use Errout
;
32 with Exp_Aggr
; use Exp_Aggr
;
33 with Exp_Atag
; use Exp_Atag
;
34 with Exp_Ch2
; use Exp_Ch2
;
35 with Exp_Ch3
; use Exp_Ch3
;
36 with Exp_Ch6
; use Exp_Ch6
;
37 with Exp_Ch7
; use Exp_Ch7
;
38 with Exp_Ch9
; use Exp_Ch9
;
39 with Exp_Disp
; use Exp_Disp
;
40 with Exp_Fixd
; use Exp_Fixd
;
41 with Exp_Intr
; use Exp_Intr
;
42 with Exp_Pakd
; use Exp_Pakd
;
43 with Exp_Tss
; use Exp_Tss
;
44 with Exp_Util
; use Exp_Util
;
45 with Freeze
; use Freeze
;
46 with Inline
; use Inline
;
48 with Namet
; use Namet
;
49 with Nlists
; use Nlists
;
50 with Nmake
; use Nmake
;
52 with Par_SCO
; use Par_SCO
;
53 with Restrict
; use Restrict
;
54 with Rident
; use Rident
;
55 with Rtsfind
; use Rtsfind
;
57 with Sem_Aux
; use Sem_Aux
;
58 with Sem_Cat
; use Sem_Cat
;
59 with Sem_Ch3
; use Sem_Ch3
;
60 with Sem_Ch8
; use Sem_Ch8
;
61 with Sem_Ch13
; use Sem_Ch13
;
62 with Sem_Eval
; use Sem_Eval
;
63 with Sem_Res
; use Sem_Res
;
64 with Sem_Type
; use Sem_Type
;
65 with Sem_Util
; use Sem_Util
;
66 with Sem_Warn
; use Sem_Warn
;
67 with Sinfo
; use Sinfo
;
68 with Snames
; use Snames
;
69 with Stand
; use Stand
;
70 with SCIL_LL
; use SCIL_LL
;
71 with Targparm
; use Targparm
;
72 with Tbuild
; use Tbuild
;
73 with Ttypes
; use Ttypes
;
74 with Uintp
; use Uintp
;
75 with Urealp
; use Urealp
;
76 with Validsw
; use Validsw
;
78 package body Exp_Ch4
is
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 procedure Binary_Op_Validity_Checks
(N
: Node_Id
);
85 pragma Inline
(Binary_Op_Validity_Checks
);
86 -- Performs validity checks for a binary operator
88 procedure Build_Boolean_Array_Proc_Call
92 -- If a boolean array assignment can be done in place, build call to
93 -- corresponding library procedure.
95 function Current_Anonymous_Master
return Entity_Id
;
96 -- Return the entity of the heterogeneous finalization master belonging to
97 -- the current unit (either function, package or procedure). This master
98 -- services all anonymous access-to-controlled types. If the current unit
99 -- does not have such master, create one.
101 procedure Displace_Allocator_Pointer
(N
: Node_Id
);
102 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
103 -- Expand_Allocator_Expression. Allocating class-wide interface objects
104 -- this routine displaces the pointer to the allocated object to reference
105 -- the component referencing the corresponding secondary dispatch table.
107 procedure Expand_Allocator_Expression
(N
: Node_Id
);
108 -- Subsidiary to Expand_N_Allocator, for the case when the expression
109 -- is a qualified expression or an aggregate.
111 procedure Expand_Array_Comparison
(N
: Node_Id
);
112 -- This routine handles expansion of the comparison operators (N_Op_Lt,
113 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
114 -- code for these operators is similar, differing only in the details of
115 -- the actual comparison call that is made. Special processing (call a
118 function Expand_Array_Equality
123 Typ
: Entity_Id
) return Node_Id
;
124 -- Expand an array equality into a call to a function implementing this
125 -- equality, and a call to it. Loc is the location for the generated nodes.
126 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
127 -- on which to attach bodies of local functions that are created in the
128 -- process. It is the responsibility of the caller to insert those bodies
129 -- at the right place. Nod provides the Sloc value for the generated code.
130 -- Normally the types used for the generated equality routine are taken
131 -- from Lhs and Rhs. However, in some situations of generated code, the
132 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
133 -- the type to be used for the formal parameters.
135 procedure Expand_Boolean_Operator
(N
: Node_Id
);
136 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
137 -- case of array type arguments.
139 procedure Expand_Short_Circuit_Operator
(N
: Node_Id
);
140 -- Common expansion processing for short-circuit boolean operators
142 procedure Expand_Compare_Minimize_Eliminate_Overflow
(N
: Node_Id
);
143 -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is
144 -- where we allow comparison of "out of range" values.
146 function Expand_Composite_Equality
151 Bodies
: List_Id
) return Node_Id
;
152 -- Local recursive function used to expand equality for nested composite
153 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
154 -- to attach bodies of local functions that are created in the process. It
155 -- is the responsibility of the caller to insert those bodies at the right
156 -- place. Nod provides the Sloc value for generated code. Lhs and Rhs are
157 -- the left and right sides for the comparison, and Typ is the type of the
158 -- objects to compare.
160 procedure Expand_Concatenate
(Cnode
: Node_Id
; Opnds
: List_Id
);
161 -- Routine to expand concatenation of a sequence of two or more operands
162 -- (in the list Operands) and replace node Cnode with the result of the
163 -- concatenation. The operands can be of any appropriate type, and can
164 -- include both arrays and singleton elements.
166 procedure Expand_Membership_Minimize_Eliminate_Overflow
(N
: Node_Id
);
167 -- N is an N_In membership test mode, with the overflow check mode set to
168 -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed
169 -- integer type. This is a case where top level processing is required to
170 -- handle overflow checks in subtrees.
172 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
);
173 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
174 -- fixed. We do not have such a type at runtime, so the purpose of this
175 -- routine is to find the real type by looking up the tree. We also
176 -- determine if the operation must be rounded.
178 function Has_Inferable_Discriminants
(N
: Node_Id
) return Boolean;
179 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
180 -- discriminants if it has a constrained nominal type, unless the object
181 -- is a component of an enclosing Unchecked_Union object that is subject
182 -- to a per-object constraint and the enclosing object lacks inferable
185 -- An expression of an Unchecked_Union type has inferable discriminants
186 -- if it is either a name of an object with inferable discriminants or a
187 -- qualified expression whose subtype mark denotes a constrained subtype.
189 procedure Insert_Dereference_Action
(N
: Node_Id
);
190 -- N is an expression whose type is an access. When the type of the
191 -- associated storage pool is derived from Checked_Pool, generate a
192 -- call to the 'Dereference' primitive operation.
194 function Make_Array_Comparison_Op
196 Nod
: Node_Id
) return Node_Id
;
197 -- Comparisons between arrays are expanded in line. This function produces
198 -- the body of the implementation of (a > b), where a and b are one-
199 -- dimensional arrays of some discrete type. The original node is then
200 -- expanded into the appropriate call to this function. Nod provides the
201 -- Sloc value for the generated code.
203 function Make_Boolean_Array_Op
205 N
: Node_Id
) return Node_Id
;
206 -- Boolean operations on boolean arrays are expanded in line. This function
207 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
208 -- b). It is used only the normal case and not the packed case. The type
209 -- involved, Typ, is the Boolean array type, and the logical operations in
210 -- the body are simple boolean operations. Note that Typ is always a
211 -- constrained type (the caller has ensured this by using
212 -- Convert_To_Actual_Subtype if necessary).
214 function Minimized_Eliminated_Overflow_Check
(N
: Node_Id
) return Boolean;
215 -- For signed arithmetic operations when the current overflow mode is
216 -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks
217 -- as the first thing we do. We then return. We count on the recursive
218 -- apparatus for overflow checks to call us back with an equivalent
219 -- operation that is in CHECKED mode, avoiding a recursive entry into this
220 -- routine, and that is when we will proceed with the expansion of the
221 -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do
222 -- these optimizations without first making this check, since there may be
223 -- operands further down the tree that are relying on the recursive calls
224 -- triggered by the top level nodes to properly process overflow checking
225 -- and remaining expansion on these nodes. Note that this call back may be
226 -- skipped if the operation is done in Bignum mode but that's fine, since
227 -- the Bignum call takes care of everything.
229 procedure Optimize_Length_Comparison
(N
: Node_Id
);
230 -- Given an expression, if it is of the form X'Length op N (or the other
231 -- way round), where N is known at compile time to be 0 or 1, and X is a
232 -- simple entity, and op is a comparison operator, optimizes it into a
233 -- comparison of First and Last.
235 procedure Process_Transient_Object
238 -- Subsidiary routine to the expansion of expression_with_actions and if
239 -- expressions. Generate all the necessary code to finalize a transient
240 -- controlled object when the enclosing context is elaborated or evaluated.
241 -- Decl denotes the declaration of the transient controlled object which is
242 -- usually the result of a controlled function call. Rel_Node denotes the
243 -- context, either an expression_with_actions or an if expression.
245 procedure Rewrite_Comparison
(N
: Node_Id
);
246 -- If N is the node for a comparison whose outcome can be determined at
247 -- compile time, then the node N can be rewritten with True or False. If
248 -- the outcome cannot be determined at compile time, the call has no
249 -- effect. If N is a type conversion, then this processing is applied to
250 -- its expression. If N is neither comparison nor a type conversion, the
251 -- call has no effect.
253 procedure Tagged_Membership
255 SCIL_Node
: out Node_Id
;
256 Result
: out Node_Id
);
257 -- Construct the expression corresponding to the tagged membership test.
258 -- Deals with a second operand being (or not) a class-wide type.
260 function Safe_In_Place_Array_Op
263 Op2
: Node_Id
) return Boolean;
264 -- In the context of an assignment, where the right-hand side is a boolean
265 -- operation on arrays, check whether operation can be performed in place.
267 procedure Unary_Op_Validity_Checks
(N
: Node_Id
);
268 pragma Inline
(Unary_Op_Validity_Checks
);
269 -- Performs validity checks for a unary operator
271 -------------------------------
272 -- Binary_Op_Validity_Checks --
273 -------------------------------
275 procedure Binary_Op_Validity_Checks
(N
: Node_Id
) is
277 if Validity_Checks_On
and Validity_Check_Operands
then
278 Ensure_Valid
(Left_Opnd
(N
));
279 Ensure_Valid
(Right_Opnd
(N
));
281 end Binary_Op_Validity_Checks
;
283 ------------------------------------
284 -- Build_Boolean_Array_Proc_Call --
285 ------------------------------------
287 procedure Build_Boolean_Array_Proc_Call
292 Loc
: constant Source_Ptr
:= Sloc
(N
);
293 Kind
: constant Node_Kind
:= Nkind
(Expression
(N
));
294 Target
: constant Node_Id
:=
295 Make_Attribute_Reference
(Loc
,
297 Attribute_Name
=> Name_Address
);
299 Arg1
: Node_Id
:= Op1
;
300 Arg2
: Node_Id
:= Op2
;
302 Proc_Name
: Entity_Id
;
305 if Kind
= N_Op_Not
then
306 if Nkind
(Op1
) in N_Binary_Op
then
308 -- Use negated version of the binary operators
310 if Nkind
(Op1
) = N_Op_And
then
311 Proc_Name
:= RTE
(RE_Vector_Nand
);
313 elsif Nkind
(Op1
) = N_Op_Or
then
314 Proc_Name
:= RTE
(RE_Vector_Nor
);
316 else pragma Assert
(Nkind
(Op1
) = N_Op_Xor
);
317 Proc_Name
:= RTE
(RE_Vector_Xor
);
321 Make_Procedure_Call_Statement
(Loc
,
322 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
324 Parameter_Associations
=> New_List
(
326 Make_Attribute_Reference
(Loc
,
327 Prefix
=> Left_Opnd
(Op1
),
328 Attribute_Name
=> Name_Address
),
330 Make_Attribute_Reference
(Loc
,
331 Prefix
=> Right_Opnd
(Op1
),
332 Attribute_Name
=> Name_Address
),
334 Make_Attribute_Reference
(Loc
,
335 Prefix
=> Left_Opnd
(Op1
),
336 Attribute_Name
=> Name_Length
)));
339 Proc_Name
:= RTE
(RE_Vector_Not
);
342 Make_Procedure_Call_Statement
(Loc
,
343 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
344 Parameter_Associations
=> New_List
(
347 Make_Attribute_Reference
(Loc
,
349 Attribute_Name
=> Name_Address
),
351 Make_Attribute_Reference
(Loc
,
353 Attribute_Name
=> Name_Length
)));
357 -- We use the following equivalences:
359 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
360 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
361 -- (not X) xor (not Y) = X xor Y
362 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
364 if Nkind
(Op1
) = N_Op_Not
then
365 Arg1
:= Right_Opnd
(Op1
);
366 Arg2
:= Right_Opnd
(Op2
);
368 if Kind
= N_Op_And
then
369 Proc_Name
:= RTE
(RE_Vector_Nor
);
370 elsif Kind
= N_Op_Or
then
371 Proc_Name
:= RTE
(RE_Vector_Nand
);
373 Proc_Name
:= RTE
(RE_Vector_Xor
);
377 if Kind
= N_Op_And
then
378 Proc_Name
:= RTE
(RE_Vector_And
);
379 elsif Kind
= N_Op_Or
then
380 Proc_Name
:= RTE
(RE_Vector_Or
);
381 elsif Nkind
(Op2
) = N_Op_Not
then
382 Proc_Name
:= RTE
(RE_Vector_Nxor
);
383 Arg2
:= Right_Opnd
(Op2
);
385 Proc_Name
:= RTE
(RE_Vector_Xor
);
390 Make_Procedure_Call_Statement
(Loc
,
391 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
392 Parameter_Associations
=> New_List
(
394 Make_Attribute_Reference
(Loc
,
396 Attribute_Name
=> Name_Address
),
397 Make_Attribute_Reference
(Loc
,
399 Attribute_Name
=> Name_Address
),
400 Make_Attribute_Reference
(Loc
,
402 Attribute_Name
=> Name_Length
)));
405 Rewrite
(N
, Call_Node
);
409 when RE_Not_Available
=>
411 end Build_Boolean_Array_Proc_Call
;
413 ------------------------------
414 -- Current_Anonymous_Master --
415 ------------------------------
417 function Current_Anonymous_Master
return Entity_Id
is
418 function Create_Anonymous_Master
419 (Unit_Id
: Entity_Id
;
420 Unit_Decl
: Node_Id
) return Entity_Id
;
421 -- Create a new anonymous master for a compilation unit denoted by its
422 -- entity Unit_Id and declaration Unit_Decl. The declaration of the new
423 -- master along with any specialized initialization is inserted at the
424 -- top of the unit's declarations (see body for special cases). Return
425 -- the entity of the anonymous master.
427 -----------------------------
428 -- Create_Anonymous_Master --
429 -----------------------------
431 function Create_Anonymous_Master
432 (Unit_Id
: Entity_Id
;
433 Unit_Decl
: Node_Id
) return Entity_Id
435 Insert_Nod
: Node_Id
:= Empty
;
436 -- The point of insertion into the declarative list of the unit. All
437 -- nodes are inserted before Insert_Nod.
439 procedure Insert_And_Analyze
(Decls
: List_Id
; N
: Node_Id
);
440 -- Insert arbitrary node N in declarative list Decls and analyze it
442 ------------------------
443 -- Insert_And_Analyze --
444 ------------------------
446 procedure Insert_And_Analyze
(Decls
: List_Id
; N
: Node_Id
) is
448 -- The declarative list is already populated, the nodes are
449 -- inserted at the top of the list, preserving their order.
451 if Present
(Insert_Nod
) then
452 Insert_Before
(Insert_Nod
, N
);
454 -- Otherwise append to the declarations to preserve order
457 Append_To
(Decls
, N
);
461 end Insert_And_Analyze
;
465 Loc
: constant Source_Ptr
:= Sloc
(Unit_Id
);
466 Spec_Id
: constant Entity_Id
:= Corresponding_Spec_Of
(Unit_Decl
);
472 -- Start of processing for Create_Anonymous_Master
475 -- Find the declarative list of the unit
477 if Nkind
(Unit_Decl
) = N_Package_Declaration
then
478 Unit_Spec
:= Specification
(Unit_Decl
);
479 Decls
:= Visible_Declarations
(Unit_Spec
);
482 Decls
:= New_List
(Make_Null_Statement
(Loc
));
483 Set_Visible_Declarations
(Unit_Spec
, Decls
);
486 -- Package or subprogram body
488 -- ??? A subprogram declaration that acts as a compilation unit may
489 -- contain a formal parameter of an anonymous access-to-controlled
490 -- type initialized by an allocator.
492 -- procedure Comp_Unit_Proc (Param : access Ctrl := new Ctrl);
494 -- There is no suitable place to create the anonymous master as the
495 -- subprogram is not in a declarative list.
498 Decls
:= Declarations
(Unit_Decl
);
501 Decls
:= New_List
(Make_Null_Statement
(Loc
));
502 Set_Declarations
(Unit_Decl
, Decls
);
506 -- The anonymous master and all initialization actions are inserted
507 -- before the first declaration (if any).
509 Insert_Nod
:= First
(Decls
);
511 -- Since the anonymous master and all its initialization actions are
512 -- inserted at top level, use the scope of the unit when analyzing.
514 Push_Scope
(Spec_Id
);
516 -- Step 1: Anonymous master creation
518 -- Use a unique prefix in case the same unit requires two anonymous
519 -- masters, one for the spec (S) and one for the body (B).
521 if Ekind_In
(Unit_Id
, E_Function
, E_Package
, E_Procedure
) then
528 Make_Defining_Identifier
(Loc
,
530 (Related_Id
=> Chars
(Unit_Id
),
534 Set_Anonymous_Master
(Unit_Id
, FM_Id
);
537 -- <FM_Id> : Finalization_Master;
539 Insert_And_Analyze
(Decls
,
540 Make_Object_Declaration
(Loc
,
541 Defining_Identifier
=> FM_Id
,
543 New_Occurrence_Of
(RTE
(RE_Finalization_Master
), Loc
)));
545 -- Step 2: Initialization actions
547 -- Do not set the base pool and mode of operation on .NET/JVM since
548 -- those targets do not support pools and all VM masters defaulted to
551 if VM_Target
= No_VM
then
555 -- (<FM_Id>, Global_Pool_Object'Unrestricted_Access);
557 Insert_And_Analyze
(Decls
,
558 Make_Procedure_Call_Statement
(Loc
,
560 New_Occurrence_Of
(RTE
(RE_Set_Base_Pool
), Loc
),
561 Parameter_Associations
=> New_List
(
562 New_Occurrence_Of
(FM_Id
, Loc
),
563 Make_Attribute_Reference
(Loc
,
565 New_Occurrence_Of
(RTE
(RE_Global_Pool_Object
), Loc
),
566 Attribute_Name
=> Name_Unrestricted_Access
))));
569 -- Set_Is_Heterogeneous (<FM_Id>);
571 Insert_And_Analyze
(Decls
,
572 Make_Procedure_Call_Statement
(Loc
,
574 New_Occurrence_Of
(RTE
(RE_Set_Is_Heterogeneous
), Loc
),
575 Parameter_Associations
=> New_List
(
576 New_Occurrence_Of
(FM_Id
, Loc
))));
581 end Create_Anonymous_Master
;
583 -- Local declarations
588 -- Start of processing for Current_Anonymous_Master
591 Unit_Decl
:= Unit
(Cunit
(Current_Sem_Unit
));
592 Unit_Id
:= Defining_Entity
(Unit_Decl
);
594 -- The compilation unit is a package instantiation. In this case the
595 -- anonymous master is associated with the package spec as both the
596 -- spec and body appear at the same level.
598 if Nkind
(Unit_Decl
) = N_Package_Body
599 and then Nkind
(Original_Node
(Unit_Decl
)) = N_Package_Instantiation
601 Unit_Id
:= Corresponding_Spec
(Unit_Decl
);
602 Unit_Decl
:= Unit_Declaration_Node
(Unit_Id
);
605 if Present
(Anonymous_Master
(Unit_Id
)) then
606 return Anonymous_Master
(Unit_Id
);
608 -- Create a new anonymous master when allocating an object of anonymous
609 -- access-to-controlled type for the first time.
612 return Create_Anonymous_Master
(Unit_Id
, Unit_Decl
);
614 end Current_Anonymous_Master
;
616 --------------------------------
617 -- Displace_Allocator_Pointer --
618 --------------------------------
620 procedure Displace_Allocator_Pointer
(N
: Node_Id
) is
621 Loc
: constant Source_Ptr
:= Sloc
(N
);
622 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
628 -- Do nothing in case of VM targets: the virtual machine will handle
629 -- interfaces directly.
631 if not Tagged_Type_Expansion
then
635 pragma Assert
(Nkind
(N
) = N_Identifier
636 and then Nkind
(Orig_Node
) = N_Allocator
);
638 PtrT
:= Etype
(Orig_Node
);
639 Dtyp
:= Available_View
(Designated_Type
(PtrT
));
640 Etyp
:= Etype
(Expression
(Orig_Node
));
642 if Is_Class_Wide_Type
(Dtyp
) and then Is_Interface
(Dtyp
) then
644 -- If the type of the allocator expression is not an interface type
645 -- we can generate code to reference the record component containing
646 -- the pointer to the secondary dispatch table.
648 if not Is_Interface
(Etyp
) then
650 Saved_Typ
: constant Entity_Id
:= Etype
(Orig_Node
);
653 -- 1) Get access to the allocated object
656 Make_Explicit_Dereference
(Loc
, Relocate_Node
(N
)));
660 -- 2) Add the conversion to displace the pointer to reference
661 -- the secondary dispatch table.
663 Rewrite
(N
, Convert_To
(Dtyp
, Relocate_Node
(N
)));
664 Analyze_And_Resolve
(N
, Dtyp
);
666 -- 3) The 'access to the secondary dispatch table will be used
667 -- as the value returned by the allocator.
670 Make_Attribute_Reference
(Loc
,
671 Prefix
=> Relocate_Node
(N
),
672 Attribute_Name
=> Name_Access
));
673 Set_Etype
(N
, Saved_Typ
);
677 -- If the type of the allocator expression is an interface type we
678 -- generate a run-time call to displace "this" to reference the
679 -- component containing the pointer to the secondary dispatch table
680 -- or else raise Constraint_Error if the actual object does not
681 -- implement the target interface. This case corresponds to the
682 -- following example:
684 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
686 -- return new Iface_2'Class'(Obj);
691 Unchecked_Convert_To
(PtrT
,
692 Make_Function_Call
(Loc
,
693 Name
=> New_Occurrence_Of
(RTE
(RE_Displace
), Loc
),
694 Parameter_Associations
=> New_List
(
695 Unchecked_Convert_To
(RTE
(RE_Address
),
701 (Access_Disp_Table
(Etype
(Base_Type
(Dtyp
))))),
703 Analyze_And_Resolve
(N
, PtrT
);
706 end Displace_Allocator_Pointer
;
708 ---------------------------------
709 -- Expand_Allocator_Expression --
710 ---------------------------------
712 procedure Expand_Allocator_Expression
(N
: Node_Id
) is
713 Loc
: constant Source_Ptr
:= Sloc
(N
);
714 Exp
: constant Node_Id
:= Expression
(Expression
(N
));
715 PtrT
: constant Entity_Id
:= Etype
(N
);
716 DesigT
: constant Entity_Id
:= Designated_Type
(PtrT
);
718 procedure Apply_Accessibility_Check
720 Built_In_Place
: Boolean := False);
721 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
722 -- type, generate an accessibility check to verify that the level of the
723 -- type of the created object is not deeper than the level of the access
724 -- type. If the type of the qualified expression is class-wide, then
725 -- always generate the check (except in the case where it is known to be
726 -- unnecessary, see comment below). Otherwise, only generate the check
727 -- if the level of the qualified expression type is statically deeper
728 -- than the access type.
730 -- Although the static accessibility will generally have been performed
731 -- as a legality check, it won't have been done in cases where the
732 -- allocator appears in generic body, so a run-time check is needed in
733 -- general. One special case is when the access type is declared in the
734 -- same scope as the class-wide allocator, in which case the check can
735 -- never fail, so it need not be generated.
737 -- As an open issue, there seem to be cases where the static level
738 -- associated with the class-wide object's underlying type is not
739 -- sufficient to perform the proper accessibility check, such as for
740 -- allocators in nested subprograms or accept statements initialized by
741 -- class-wide formals when the actual originates outside at a deeper
742 -- static level. The nested subprogram case might require passing
743 -- accessibility levels along with class-wide parameters, and the task
744 -- case seems to be an actual gap in the language rules that needs to
745 -- be fixed by the ARG. ???
747 -------------------------------
748 -- Apply_Accessibility_Check --
749 -------------------------------
751 procedure Apply_Accessibility_Check
753 Built_In_Place
: Boolean := False)
755 Pool_Id
: constant Entity_Id
:= Associated_Storage_Pool
(PtrT
);
763 if Ada_Version
>= Ada_2005
764 and then Is_Class_Wide_Type
(DesigT
)
765 and then (Tagged_Type_Expansion
or else VM_Target
/= No_VM
)
766 and then not Scope_Suppress
.Suppress
(Accessibility_Check
)
768 (Type_Access_Level
(Etype
(Exp
)) > Type_Access_Level
(PtrT
)
770 (Is_Class_Wide_Type
(Etype
(Exp
))
771 and then Scope
(PtrT
) /= Current_Scope
))
773 -- If the allocator was built in place, Ref is already a reference
774 -- to the access object initialized to the result of the allocator
775 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
776 -- Remove_Side_Effects for cases where the build-in-place call may
777 -- still be the prefix of the reference (to avoid generating
778 -- duplicate calls). Otherwise, it is the entity associated with
779 -- the object containing the address of the allocated object.
781 if Built_In_Place
then
782 Remove_Side_Effects
(Ref
);
783 Obj_Ref
:= New_Copy_Tree
(Ref
);
785 Obj_Ref
:= New_Occurrence_Of
(Ref
, Loc
);
788 -- For access to interface types we must generate code to displace
789 -- the pointer to the base of the object since the subsequent code
790 -- references components located in the TSD of the object (which
791 -- is associated with the primary dispatch table --see a-tags.ads)
792 -- and also generates code invoking Free, which requires also a
793 -- reference to the base of the unallocated object.
795 if Is_Interface
(DesigT
) and then Tagged_Type_Expansion
then
797 Unchecked_Convert_To
(Etype
(Obj_Ref
),
798 Make_Function_Call
(Loc
,
800 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
801 Parameter_Associations
=> New_List
(
802 Unchecked_Convert_To
(RTE
(RE_Address
),
803 New_Copy_Tree
(Obj_Ref
)))));
806 -- Step 1: Create the object clean up code
810 -- Deallocate the object if the accessibility check fails. This
811 -- is done only on targets or profiles that support deallocation.
815 if RTE_Available
(RE_Free
) then
816 Free_Stmt
:= Make_Free_Statement
(Loc
, New_Copy_Tree
(Obj_Ref
));
817 Set_Storage_Pool
(Free_Stmt
, Pool_Id
);
819 Append_To
(Stmts
, Free_Stmt
);
821 -- The target or profile cannot deallocate objects
827 -- Finalize the object if applicable. Generate:
829 -- [Deep_]Finalize (Obj_Ref.all);
831 if Needs_Finalization
(DesigT
) then
835 Make_Explicit_Dereference
(Loc
, New_Copy
(Obj_Ref
)),
838 -- When the target or profile supports deallocation, wrap the
839 -- finalization call in a block to ensure proper deallocation
840 -- even if finalization fails. Generate:
850 if Present
(Free_Stmt
) then
852 Make_Block_Statement
(Loc
,
853 Handled_Statement_Sequence
=>
854 Make_Handled_Sequence_Of_Statements
(Loc
,
855 Statements
=> New_List
(Fin_Call
),
857 Exception_Handlers
=> New_List
(
858 Make_Exception_Handler
(Loc
,
859 Exception_Choices
=> New_List
(
860 Make_Others_Choice
(Loc
)),
861 Statements
=> New_List
(
862 New_Copy_Tree
(Free_Stmt
),
863 Make_Raise_Statement
(Loc
))))));
866 Prepend_To
(Stmts
, Fin_Call
);
869 -- Signal the accessibility failure through a Program_Error
872 Make_Raise_Program_Error
(Loc
,
873 Condition
=> New_Occurrence_Of
(Standard_True
, Loc
),
874 Reason
=> PE_Accessibility_Check_Failed
));
876 -- Step 2: Create the accessibility comparison
882 Make_Attribute_Reference
(Loc
,
884 Attribute_Name
=> Name_Tag
);
886 -- For tagged types, determine the accessibility level by looking
887 -- at the type specific data of the dispatch table. Generate:
889 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
891 if Tagged_Type_Expansion
then
892 Cond
:= Build_Get_Access_Level
(Loc
, Obj_Ref
);
894 -- Use a runtime call to determine the accessibility level when
895 -- compiling on virtual machine targets. Generate:
897 -- Get_Access_Level (Ref'Tag)
901 Make_Function_Call
(Loc
,
903 New_Occurrence_Of
(RTE
(RE_Get_Access_Level
), Loc
),
904 Parameter_Associations
=> New_List
(Obj_Ref
));
911 Make_Integer_Literal
(Loc
, Type_Access_Level
(PtrT
)));
913 -- Due to the complexity and side effects of the check, utilize an
914 -- if statement instead of the regular Program_Error circuitry.
917 Make_Implicit_If_Statement
(N
,
919 Then_Statements
=> Stmts
));
921 end Apply_Accessibility_Check
;
925 Aggr_In_Place
: constant Boolean := Is_Delayed_Aggregate
(Exp
);
926 Indic
: constant Node_Id
:= Subtype_Mark
(Expression
(N
));
927 T
: constant Entity_Id
:= Entity
(Indic
);
929 Tag_Assign
: Node_Id
;
933 TagT
: Entity_Id
:= Empty
;
934 -- Type used as source for tag assignment
936 TagR
: Node_Id
:= Empty
;
937 -- Target reference for tag assignment
939 -- Start of processing for Expand_Allocator_Expression
942 -- Handle call to C++ constructor
944 if Is_CPP_Constructor_Call
(Exp
) then
945 Make_CPP_Constructor_Call_In_Allocator
947 Function_Call
=> Exp
);
951 -- In the case of an Ada 2012 allocator whose initial value comes from a
952 -- function call, pass "the accessibility level determined by the point
953 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
954 -- Expand_Call but it couldn't be done there (because the Etype of the
955 -- allocator wasn't set then) so we generate the parameter here. See
956 -- the Boolean variable Defer in (a block within) Expand_Call.
958 if Ada_Version
>= Ada_2012
and then Nkind
(Exp
) = N_Function_Call
then
963 if Nkind
(Name
(Exp
)) = N_Explicit_Dereference
then
964 Subp
:= Designated_Type
(Etype
(Prefix
(Name
(Exp
))));
966 Subp
:= Entity
(Name
(Exp
));
969 Subp
:= Ultimate_Alias
(Subp
);
971 if Present
(Extra_Accessibility_Of_Result
(Subp
)) then
972 Add_Extra_Actual_To_Call
973 (Subprogram_Call
=> Exp
,
974 Extra_Formal
=> Extra_Accessibility_Of_Result
(Subp
),
975 Extra_Actual
=> Dynamic_Accessibility_Level
(PtrT
));
980 -- Case of tagged type or type requiring finalization
982 if Is_Tagged_Type
(T
) or else Needs_Finalization
(T
) then
984 -- Ada 2005 (AI-318-02): If the initialization expression is a call
985 -- to a build-in-place function, then access to the allocated object
986 -- must be passed to the function. Currently we limit such functions
987 -- to those with constrained limited result subtypes, but eventually
988 -- we plan to expand the allowed forms of functions that are treated
989 -- as build-in-place.
991 if Ada_Version
>= Ada_2005
992 and then Is_Build_In_Place_Function_Call
(Exp
)
994 Make_Build_In_Place_Call_In_Allocator
(N
, Exp
);
995 Apply_Accessibility_Check
(N
, Built_In_Place
=> True);
999 -- Actions inserted before:
1000 -- Temp : constant ptr_T := new T'(Expression);
1001 -- Temp._tag = T'tag; -- when not class-wide
1002 -- [Deep_]Adjust (Temp.all);
1004 -- We analyze by hand the new internal allocator to avoid any
1005 -- recursion and inappropriate call to Initialize.
1007 -- We don't want to remove side effects when the expression must be
1008 -- built in place. In the case of a build-in-place function call,
1009 -- that could lead to a duplication of the call, which was already
1010 -- substituted for the allocator.
1012 if not Aggr_In_Place
then
1013 Remove_Side_Effects
(Exp
);
1016 Temp
:= Make_Temporary
(Loc
, 'P', N
);
1018 -- For a class wide allocation generate the following code:
1020 -- type Equiv_Record is record ... end record;
1021 -- implicit subtype CW is <Class_Wide_Subytpe>;
1022 -- temp : PtrT := new CW'(CW!(expr));
1024 if Is_Class_Wide_Type
(T
) then
1025 Expand_Subtype_From_Expr
(Empty
, T
, Indic
, Exp
);
1027 -- Ada 2005 (AI-251): If the expression is a class-wide interface
1028 -- object we generate code to move up "this" to reference the
1029 -- base of the object before allocating the new object.
1031 -- Note that Exp'Address is recursively expanded into a call
1032 -- to Base_Address (Exp.Tag)
1034 if Is_Class_Wide_Type
(Etype
(Exp
))
1035 and then Is_Interface
(Etype
(Exp
))
1036 and then Tagged_Type_Expansion
1040 Unchecked_Convert_To
(Entity
(Indic
),
1041 Make_Explicit_Dereference
(Loc
,
1042 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
1043 Make_Attribute_Reference
(Loc
,
1045 Attribute_Name
=> Name_Address
)))));
1049 Unchecked_Convert_To
(Entity
(Indic
), Exp
));
1052 Analyze_And_Resolve
(Expression
(N
), Entity
(Indic
));
1055 -- Processing for allocators returning non-interface types
1057 if not Is_Interface
(Directly_Designated_Type
(PtrT
)) then
1058 if Aggr_In_Place
then
1060 Make_Object_Declaration
(Loc
,
1061 Defining_Identifier
=> Temp
,
1062 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
1064 Make_Allocator
(Loc
,
1066 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
1068 -- Copy the Comes_From_Source flag for the allocator we just
1069 -- built, since logically this allocator is a replacement of
1070 -- the original allocator node. This is for proper handling of
1071 -- restriction No_Implicit_Heap_Allocations.
1073 Set_Comes_From_Source
1074 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
1076 Set_No_Initialization
(Expression
(Temp_Decl
));
1077 Insert_Action
(N
, Temp_Decl
);
1079 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1080 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
1082 -- Attach the object to the associated finalization master.
1083 -- This is done manually on .NET/JVM since those compilers do
1084 -- no support pools and can't benefit from internally generated
1085 -- Allocate / Deallocate procedures.
1087 if VM_Target
/= No_VM
1088 and then Is_Controlled
(DesigT
)
1089 and then Present
(Finalization_Master
(PtrT
))
1093 (Obj_Ref
=> New_Occurrence_Of
(Temp
, Loc
),
1098 Node
:= Relocate_Node
(N
);
1099 Set_Analyzed
(Node
);
1102 Make_Object_Declaration
(Loc
,
1103 Defining_Identifier
=> Temp
,
1104 Constant_Present
=> True,
1105 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
1106 Expression
=> Node
);
1108 Insert_Action
(N
, Temp_Decl
);
1109 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1111 -- Attach the object to the associated finalization master.
1112 -- This is done manually on .NET/JVM since those compilers do
1113 -- no support pools and can't benefit from internally generated
1114 -- Allocate / Deallocate procedures.
1116 if VM_Target
/= No_VM
1117 and then Is_Controlled
(DesigT
)
1118 and then Present
(Finalization_Master
(PtrT
))
1122 (Obj_Ref
=> New_Occurrence_Of
(Temp
, Loc
),
1127 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
1128 -- interface type. In this case we use the type of the qualified
1129 -- expression to allocate the object.
1133 Def_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
1138 Make_Full_Type_Declaration
(Loc
,
1139 Defining_Identifier
=> Def_Id
,
1141 Make_Access_To_Object_Definition
(Loc
,
1142 All_Present
=> True,
1143 Null_Exclusion_Present
=> False,
1145 Is_Access_Constant
(Etype
(N
)),
1146 Subtype_Indication
=>
1147 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
1149 Insert_Action
(N
, New_Decl
);
1151 -- Inherit the allocation-related attributes from the original
1154 Set_Finalization_Master
1155 (Def_Id
, Finalization_Master
(PtrT
));
1157 Set_Associated_Storage_Pool
1158 (Def_Id
, Associated_Storage_Pool
(PtrT
));
1160 -- Declare the object using the previous type declaration
1162 if Aggr_In_Place
then
1164 Make_Object_Declaration
(Loc
,
1165 Defining_Identifier
=> Temp
,
1166 Object_Definition
=> New_Occurrence_Of
(Def_Id
, Loc
),
1168 Make_Allocator
(Loc
,
1169 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
1171 -- Copy the Comes_From_Source flag for the allocator we just
1172 -- built, since logically this allocator is a replacement of
1173 -- the original allocator node. This is for proper handling
1174 -- of restriction No_Implicit_Heap_Allocations.
1176 Set_Comes_From_Source
1177 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
1179 Set_No_Initialization
(Expression
(Temp_Decl
));
1180 Insert_Action
(N
, Temp_Decl
);
1182 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1183 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
1186 Node
:= Relocate_Node
(N
);
1187 Set_Analyzed
(Node
);
1190 Make_Object_Declaration
(Loc
,
1191 Defining_Identifier
=> Temp
,
1192 Constant_Present
=> True,
1193 Object_Definition
=> New_Occurrence_Of
(Def_Id
, Loc
),
1194 Expression
=> Node
);
1196 Insert_Action
(N
, Temp_Decl
);
1197 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1200 -- Generate an additional object containing the address of the
1201 -- returned object. The type of this second object declaration
1202 -- is the correct type required for the common processing that
1203 -- is still performed by this subprogram. The displacement of
1204 -- this pointer to reference the component associated with the
1205 -- interface type will be done at the end of common processing.
1208 Make_Object_Declaration
(Loc
,
1209 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
1210 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
1212 Unchecked_Convert_To
(PtrT
,
1213 New_Occurrence_Of
(Temp
, Loc
)));
1215 Insert_Action
(N
, New_Decl
);
1217 Temp_Decl
:= New_Decl
;
1218 Temp
:= Defining_Identifier
(New_Decl
);
1222 Apply_Accessibility_Check
(Temp
);
1224 -- Generate the tag assignment
1226 -- Suppress the tag assignment when VM_Target because VM tags are
1227 -- represented implicitly in objects.
1229 if not Tagged_Type_Expansion
then
1232 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1233 -- interface objects because in this case the tag does not change.
1235 elsif Is_Interface
(Directly_Designated_Type
(Etype
(N
))) then
1236 pragma Assert
(Is_Class_Wide_Type
1237 (Directly_Designated_Type
(Etype
(N
))));
1240 elsif Is_Tagged_Type
(T
) and then not Is_Class_Wide_Type
(T
) then
1242 TagR
:= New_Occurrence_Of
(Temp
, Loc
);
1244 elsif Is_Private_Type
(T
)
1245 and then Is_Tagged_Type
(Underlying_Type
(T
))
1247 TagT
:= Underlying_Type
(T
);
1249 Unchecked_Convert_To
(Underlying_Type
(T
),
1250 Make_Explicit_Dereference
(Loc
,
1251 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
1254 if Present
(TagT
) then
1256 Full_T
: constant Entity_Id
:= Underlying_Type
(TagT
);
1260 Make_Assignment_Statement
(Loc
,
1262 Make_Selected_Component
(Loc
,
1266 (First_Tag_Component
(Full_T
), Loc
)),
1269 Unchecked_Convert_To
(RTE
(RE_Tag
),
1272 (First_Elmt
(Access_Disp_Table
(Full_T
))), Loc
)));
1275 -- The previous assignment has to be done in any case
1277 Set_Assignment_OK
(Name
(Tag_Assign
));
1278 Insert_Action
(N
, Tag_Assign
);
1281 if Needs_Finalization
(DesigT
) and then Needs_Finalization
(T
) then
1283 -- Generate an Adjust call if the object will be moved. In Ada
1284 -- 2005, the object may be inherently limited, in which case
1285 -- there is no Adjust procedure, and the object is built in
1286 -- place. In Ada 95, the object can be limited but not
1287 -- inherently limited if this allocator came from a return
1288 -- statement (we're allocating the result on the secondary
1289 -- stack). In that case, the object will be moved, so we _do_
1292 if not Aggr_In_Place
1293 and then not Is_Limited_View
(T
)
1297 -- An unchecked conversion is needed in the classwide case
1298 -- because the designated type can be an ancestor of the
1299 -- subtype mark of the allocator.
1303 Unchecked_Convert_To
(T
,
1304 Make_Explicit_Dereference
(Loc
,
1305 Prefix
=> New_Occurrence_Of
(Temp
, Loc
))),
1310 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
1311 Analyze_And_Resolve
(N
, PtrT
);
1313 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1314 -- component containing the secondary dispatch table of the interface
1317 if Is_Interface
(Directly_Designated_Type
(PtrT
)) then
1318 Displace_Allocator_Pointer
(N
);
1321 elsif Aggr_In_Place
then
1322 Temp
:= Make_Temporary
(Loc
, 'P', N
);
1324 Make_Object_Declaration
(Loc
,
1325 Defining_Identifier
=> Temp
,
1326 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
1328 Make_Allocator
(Loc
,
1329 Expression
=> New_Occurrence_Of
(Etype
(Exp
), Loc
)));
1331 -- Copy the Comes_From_Source flag for the allocator we just built,
1332 -- since logically this allocator is a replacement of the original
1333 -- allocator node. This is for proper handling of restriction
1334 -- No_Implicit_Heap_Allocations.
1336 Set_Comes_From_Source
1337 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
1339 Set_No_Initialization
(Expression
(Temp_Decl
));
1340 Insert_Action
(N
, Temp_Decl
);
1342 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1343 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
1345 -- Attach the object to the associated finalization master. Thisis
1346 -- done manually on .NET/JVM since those compilers do no support
1347 -- pools and cannot benefit from internally generated Allocate and
1348 -- Deallocate procedures.
1350 if VM_Target
/= No_VM
1351 and then Is_Controlled
(DesigT
)
1352 and then Present
(Finalization_Master
(PtrT
))
1356 (Obj_Ref
=> New_Occurrence_Of
(Temp
, Loc
),
1360 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
1361 Analyze_And_Resolve
(N
, PtrT
);
1363 elsif Is_Access_Type
(T
) and then Can_Never_Be_Null
(T
) then
1364 Install_Null_Excluding_Check
(Exp
);
1366 elsif Is_Access_Type
(DesigT
)
1367 and then Nkind
(Exp
) = N_Allocator
1368 and then Nkind
(Expression
(Exp
)) /= N_Qualified_Expression
1370 -- Apply constraint to designated subtype indication
1372 Apply_Constraint_Check
1373 (Expression
(Exp
), Designated_Type
(DesigT
), No_Sliding
=> True);
1375 if Nkind
(Expression
(Exp
)) = N_Raise_Constraint_Error
then
1377 -- Propagate constraint_error to enclosing allocator
1379 Rewrite
(Exp
, New_Copy
(Expression
(Exp
)));
1383 Build_Allocate_Deallocate_Proc
(N
, True);
1386 -- type A is access T1;
1387 -- X : A := new T2'(...);
1388 -- T1 and T2 can be different subtypes, and we might need to check
1389 -- both constraints. First check against the type of the qualified
1392 Apply_Constraint_Check
(Exp
, T
, No_Sliding
=> True);
1394 if Do_Range_Check
(Exp
) then
1395 Generate_Range_Check
(Exp
, DesigT
, CE_Range_Check_Failed
);
1398 -- A check is also needed in cases where the designated subtype is
1399 -- constrained and differs from the subtype given in the qualified
1400 -- expression. Note that the check on the qualified expression does
1401 -- not allow sliding, but this check does (a relaxation from Ada 83).
1403 if Is_Constrained
(DesigT
)
1404 and then not Subtypes_Statically_Match
(T
, DesigT
)
1406 Apply_Constraint_Check
1407 (Exp
, DesigT
, No_Sliding
=> False);
1409 if Do_Range_Check
(Exp
) then
1410 Generate_Range_Check
(Exp
, DesigT
, CE_Range_Check_Failed
);
1414 -- For an access to unconstrained packed array, GIGI needs to see an
1415 -- expression with a constrained subtype in order to compute the
1416 -- proper size for the allocator.
1418 if Is_Array_Type
(T
)
1419 and then not Is_Constrained
(T
)
1420 and then Is_Packed
(T
)
1423 ConstrT
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
1424 Internal_Exp
: constant Node_Id
:= Relocate_Node
(Exp
);
1427 Make_Subtype_Declaration
(Loc
,
1428 Defining_Identifier
=> ConstrT
,
1429 Subtype_Indication
=>
1430 Make_Subtype_From_Expr
(Internal_Exp
, T
)));
1431 Freeze_Itype
(ConstrT
, Exp
);
1432 Rewrite
(Exp
, OK_Convert_To
(ConstrT
, Internal_Exp
));
1436 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1437 -- to a build-in-place function, then access to the allocated object
1438 -- must be passed to the function. Currently we limit such functions
1439 -- to those with constrained limited result subtypes, but eventually
1440 -- we plan to expand the allowed forms of functions that are treated
1441 -- as build-in-place.
1443 if Ada_Version
>= Ada_2005
1444 and then Is_Build_In_Place_Function_Call
(Exp
)
1446 Make_Build_In_Place_Call_In_Allocator
(N
, Exp
);
1451 when RE_Not_Available
=>
1453 end Expand_Allocator_Expression
;
1455 -----------------------------
1456 -- Expand_Array_Comparison --
1457 -----------------------------
1459 -- Expansion is only required in the case of array types. For the unpacked
1460 -- case, an appropriate runtime routine is called. For packed cases, and
1461 -- also in some other cases where a runtime routine cannot be called, the
1462 -- form of the expansion is:
1464 -- [body for greater_nn; boolean_expression]
1466 -- The body is built by Make_Array_Comparison_Op, and the form of the
1467 -- Boolean expression depends on the operator involved.
1469 procedure Expand_Array_Comparison
(N
: Node_Id
) is
1470 Loc
: constant Source_Ptr
:= Sloc
(N
);
1471 Op1
: Node_Id
:= Left_Opnd
(N
);
1472 Op2
: Node_Id
:= Right_Opnd
(N
);
1473 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
1474 Ctyp
: constant Entity_Id
:= Component_Type
(Typ1
);
1477 Func_Body
: Node_Id
;
1478 Func_Name
: Entity_Id
;
1482 Byte_Addressable
: constant Boolean := System_Storage_Unit
= Byte
'Size;
1483 -- True for byte addressable target
1485 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean;
1486 -- Returns True if the length of the given operand is known to be less
1487 -- than 4. Returns False if this length is known to be four or greater
1488 -- or is not known at compile time.
1490 ------------------------
1491 -- Length_Less_Than_4 --
1492 ------------------------
1494 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean is
1495 Otyp
: constant Entity_Id
:= Etype
(Opnd
);
1498 if Ekind
(Otyp
) = E_String_Literal_Subtype
then
1499 return String_Literal_Length
(Otyp
) < 4;
1503 Ityp
: constant Entity_Id
:= Etype
(First_Index
(Otyp
));
1504 Lo
: constant Node_Id
:= Type_Low_Bound
(Ityp
);
1505 Hi
: constant Node_Id
:= Type_High_Bound
(Ityp
);
1510 if Compile_Time_Known_Value
(Lo
) then
1511 Lov
:= Expr_Value
(Lo
);
1516 if Compile_Time_Known_Value
(Hi
) then
1517 Hiv
:= Expr_Value
(Hi
);
1522 return Hiv
< Lov
+ 3;
1525 end Length_Less_Than_4
;
1527 -- Start of processing for Expand_Array_Comparison
1530 -- Deal first with unpacked case, where we can call a runtime routine
1531 -- except that we avoid this for targets for which are not addressable
1532 -- by bytes, and for the JVM/CIL, since they do not support direct
1533 -- addressing of array components.
1535 if not Is_Bit_Packed_Array
(Typ1
)
1536 and then Byte_Addressable
1537 and then VM_Target
= No_VM
1539 -- The call we generate is:
1541 -- Compare_Array_xn[_Unaligned]
1542 -- (left'address, right'address, left'length, right'length) <op> 0
1544 -- x = U for unsigned, S for signed
1545 -- n = 8,16,32,64 for component size
1546 -- Add _Unaligned if length < 4 and component size is 8.
1547 -- <op> is the standard comparison operator
1549 if Component_Size
(Typ1
) = 8 then
1550 if Length_Less_Than_4
(Op1
)
1552 Length_Less_Than_4
(Op2
)
1554 if Is_Unsigned_Type
(Ctyp
) then
1555 Comp
:= RE_Compare_Array_U8_Unaligned
;
1557 Comp
:= RE_Compare_Array_S8_Unaligned
;
1561 if Is_Unsigned_Type
(Ctyp
) then
1562 Comp
:= RE_Compare_Array_U8
;
1564 Comp
:= RE_Compare_Array_S8
;
1568 elsif Component_Size
(Typ1
) = 16 then
1569 if Is_Unsigned_Type
(Ctyp
) then
1570 Comp
:= RE_Compare_Array_U16
;
1572 Comp
:= RE_Compare_Array_S16
;
1575 elsif Component_Size
(Typ1
) = 32 then
1576 if Is_Unsigned_Type
(Ctyp
) then
1577 Comp
:= RE_Compare_Array_U32
;
1579 Comp
:= RE_Compare_Array_S32
;
1582 else pragma Assert
(Component_Size
(Typ1
) = 64);
1583 if Is_Unsigned_Type
(Ctyp
) then
1584 Comp
:= RE_Compare_Array_U64
;
1586 Comp
:= RE_Compare_Array_S64
;
1590 Remove_Side_Effects
(Op1
, Name_Req
=> True);
1591 Remove_Side_Effects
(Op2
, Name_Req
=> True);
1594 Make_Function_Call
(Sloc
(Op1
),
1595 Name
=> New_Occurrence_Of
(RTE
(Comp
), Loc
),
1597 Parameter_Associations
=> New_List
(
1598 Make_Attribute_Reference
(Loc
,
1599 Prefix
=> Relocate_Node
(Op1
),
1600 Attribute_Name
=> Name_Address
),
1602 Make_Attribute_Reference
(Loc
,
1603 Prefix
=> Relocate_Node
(Op2
),
1604 Attribute_Name
=> Name_Address
),
1606 Make_Attribute_Reference
(Loc
,
1607 Prefix
=> Relocate_Node
(Op1
),
1608 Attribute_Name
=> Name_Length
),
1610 Make_Attribute_Reference
(Loc
,
1611 Prefix
=> Relocate_Node
(Op2
),
1612 Attribute_Name
=> Name_Length
))));
1615 Make_Integer_Literal
(Sloc
(Op2
),
1618 Analyze_And_Resolve
(Op1
, Standard_Integer
);
1619 Analyze_And_Resolve
(Op2
, Standard_Integer
);
1623 -- Cases where we cannot make runtime call
1625 -- For (a <= b) we convert to not (a > b)
1627 if Chars
(N
) = Name_Op_Le
then
1633 Right_Opnd
=> Op2
)));
1634 Analyze_And_Resolve
(N
, Standard_Boolean
);
1637 -- For < the Boolean expression is
1638 -- greater__nn (op2, op1)
1640 elsif Chars
(N
) = Name_Op_Lt
then
1641 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
1645 Op1
:= Right_Opnd
(N
);
1646 Op2
:= Left_Opnd
(N
);
1648 -- For (a >= b) we convert to not (a < b)
1650 elsif Chars
(N
) = Name_Op_Ge
then
1656 Right_Opnd
=> Op2
)));
1657 Analyze_And_Resolve
(N
, Standard_Boolean
);
1660 -- For > the Boolean expression is
1661 -- greater__nn (op1, op2)
1664 pragma Assert
(Chars
(N
) = Name_Op_Gt
);
1665 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
1668 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
1670 Make_Function_Call
(Loc
,
1671 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
1672 Parameter_Associations
=> New_List
(Op1
, Op2
));
1674 Insert_Action
(N
, Func_Body
);
1676 Analyze_And_Resolve
(N
, Standard_Boolean
);
1679 when RE_Not_Available
=>
1681 end Expand_Array_Comparison
;
1683 ---------------------------
1684 -- Expand_Array_Equality --
1685 ---------------------------
1687 -- Expand an equality function for multi-dimensional arrays. Here is an
1688 -- example of such a function for Nb_Dimension = 2
1690 -- function Enn (A : atyp; B : btyp) return boolean is
1692 -- if (A'length (1) = 0 or else A'length (2) = 0)
1694 -- (B'length (1) = 0 or else B'length (2) = 0)
1696 -- return True; -- RM 4.5.2(22)
1699 -- if A'length (1) /= B'length (1)
1701 -- A'length (2) /= B'length (2)
1703 -- return False; -- RM 4.5.2(23)
1707 -- A1 : Index_T1 := A'first (1);
1708 -- B1 : Index_T1 := B'first (1);
1712 -- A2 : Index_T2 := A'first (2);
1713 -- B2 : Index_T2 := B'first (2);
1716 -- if A (A1, A2) /= B (B1, B2) then
1720 -- exit when A2 = A'last (2);
1721 -- A2 := Index_T2'succ (A2);
1722 -- B2 := Index_T2'succ (B2);
1726 -- exit when A1 = A'last (1);
1727 -- A1 := Index_T1'succ (A1);
1728 -- B1 := Index_T1'succ (B1);
1735 -- Note on the formal types used (atyp and btyp). If either of the arrays
1736 -- is of a private type, we use the underlying type, and do an unchecked
1737 -- conversion of the actual. If either of the arrays has a bound depending
1738 -- on a discriminant, then we use the base type since otherwise we have an
1739 -- escaped discriminant in the function.
1741 -- If both arrays are constrained and have the same bounds, we can generate
1742 -- a loop with an explicit iteration scheme using a 'Range attribute over
1745 function Expand_Array_Equality
1750 Typ
: Entity_Id
) return Node_Id
1752 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
1753 Decls
: constant List_Id
:= New_List
;
1754 Index_List1
: constant List_Id
:= New_List
;
1755 Index_List2
: constant List_Id
:= New_List
;
1759 Func_Name
: Entity_Id
;
1760 Func_Body
: Node_Id
;
1762 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
1763 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
1767 -- The parameter types to be used for the formals
1772 Num
: Int
) return Node_Id
;
1773 -- This builds the attribute reference Arr'Nam (Expr)
1775 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
;
1776 -- Create one statement to compare corresponding components, designated
1777 -- by a full set of indexes.
1779 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
;
1780 -- Given one of the arguments, computes the appropriate type to be used
1781 -- for that argument in the corresponding function formal
1783 function Handle_One_Dimension
1785 Index
: Node_Id
) return Node_Id
;
1786 -- This procedure returns the following code
1789 -- Bn : Index_T := B'First (N);
1793 -- exit when An = A'Last (N);
1794 -- An := Index_T'Succ (An)
1795 -- Bn := Index_T'Succ (Bn)
1799 -- If both indexes are constrained and identical, the procedure
1800 -- returns a simpler loop:
1802 -- for An in A'Range (N) loop
1806 -- N is the dimension for which we are generating a loop. Index is the
1807 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1808 -- xxx statement is either the loop or declare for the next dimension
1809 -- or if this is the last dimension the comparison of corresponding
1810 -- components of the arrays.
1812 -- The actual way the code works is to return the comparison of
1813 -- corresponding components for the N+1 call. That's neater.
1815 function Test_Empty_Arrays
return Node_Id
;
1816 -- This function constructs the test for both arrays being empty
1817 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1819 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1821 function Test_Lengths_Correspond
return Node_Id
;
1822 -- This function constructs the test for arrays having different lengths
1823 -- in at least one index position, in which case the resulting code is:
1825 -- A'length (1) /= B'length (1)
1827 -- A'length (2) /= B'length (2)
1838 Num
: Int
) return Node_Id
1842 Make_Attribute_Reference
(Loc
,
1843 Attribute_Name
=> Nam
,
1844 Prefix
=> New_Occurrence_Of
(Arr
, Loc
),
1845 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, Num
)));
1848 ------------------------
1849 -- Component_Equality --
1850 ------------------------
1852 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
is
1857 -- if a(i1...) /= b(j1...) then return false; end if;
1860 Make_Indexed_Component
(Loc
,
1861 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
1862 Expressions
=> Index_List1
);
1865 Make_Indexed_Component
(Loc
,
1866 Prefix
=> Make_Identifier
(Loc
, Chars
(B
)),
1867 Expressions
=> Index_List2
);
1869 Test
:= Expand_Composite_Equality
1870 (Nod
, Component_Type
(Typ
), L
, R
, Decls
);
1872 -- If some (sub)component is an unchecked_union, the whole operation
1873 -- will raise program error.
1875 if Nkind
(Test
) = N_Raise_Program_Error
then
1877 -- This node is going to be inserted at a location where a
1878 -- statement is expected: clear its Etype so analysis will set
1879 -- it to the expected Standard_Void_Type.
1881 Set_Etype
(Test
, Empty
);
1886 Make_Implicit_If_Statement
(Nod
,
1887 Condition
=> Make_Op_Not
(Loc
, Right_Opnd
=> Test
),
1888 Then_Statements
=> New_List
(
1889 Make_Simple_Return_Statement
(Loc
,
1890 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))));
1892 end Component_Equality
;
1898 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
is
1909 T
:= Underlying_Type
(T
);
1911 X
:= First_Index
(T
);
1912 while Present
(X
) loop
1913 if Denotes_Discriminant
(Type_Low_Bound
(Etype
(X
)))
1915 Denotes_Discriminant
(Type_High_Bound
(Etype
(X
)))
1928 --------------------------
1929 -- Handle_One_Dimension --
1930 ---------------------------
1932 function Handle_One_Dimension
1934 Index
: Node_Id
) return Node_Id
1936 Need_Separate_Indexes
: constant Boolean :=
1937 Ltyp
/= Rtyp
or else not Is_Constrained
(Ltyp
);
1938 -- If the index types are identical, and we are working with
1939 -- constrained types, then we can use the same index for both
1942 An
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
1945 Index_T
: Entity_Id
;
1950 if N
> Number_Dimensions
(Ltyp
) then
1951 return Component_Equality
(Ltyp
);
1954 -- Case where we generate a loop
1956 Index_T
:= Base_Type
(Etype
(Index
));
1958 if Need_Separate_Indexes
then
1959 Bn
:= Make_Temporary
(Loc
, 'B');
1964 Append
(New_Occurrence_Of
(An
, Loc
), Index_List1
);
1965 Append
(New_Occurrence_Of
(Bn
, Loc
), Index_List2
);
1967 Stm_List
:= New_List
(
1968 Handle_One_Dimension
(N
+ 1, Next_Index
(Index
)));
1970 if Need_Separate_Indexes
then
1972 -- Generate guard for loop, followed by increments of indexes
1974 Append_To
(Stm_List
,
1975 Make_Exit_Statement
(Loc
,
1978 Left_Opnd
=> New_Occurrence_Of
(An
, Loc
),
1979 Right_Opnd
=> Arr_Attr
(A
, Name_Last
, N
))));
1981 Append_To
(Stm_List
,
1982 Make_Assignment_Statement
(Loc
,
1983 Name
=> New_Occurrence_Of
(An
, Loc
),
1985 Make_Attribute_Reference
(Loc
,
1986 Prefix
=> New_Occurrence_Of
(Index_T
, Loc
),
1987 Attribute_Name
=> Name_Succ
,
1988 Expressions
=> New_List
(
1989 New_Occurrence_Of
(An
, Loc
)))));
1991 Append_To
(Stm_List
,
1992 Make_Assignment_Statement
(Loc
,
1993 Name
=> New_Occurrence_Of
(Bn
, Loc
),
1995 Make_Attribute_Reference
(Loc
,
1996 Prefix
=> New_Occurrence_Of
(Index_T
, Loc
),
1997 Attribute_Name
=> Name_Succ
,
1998 Expressions
=> New_List
(
1999 New_Occurrence_Of
(Bn
, Loc
)))));
2002 -- If separate indexes, we need a declare block for An and Bn, and a
2003 -- loop without an iteration scheme.
2005 if Need_Separate_Indexes
then
2007 Make_Implicit_Loop_Statement
(Nod
, Statements
=> Stm_List
);
2010 Make_Block_Statement
(Loc
,
2011 Declarations
=> New_List
(
2012 Make_Object_Declaration
(Loc
,
2013 Defining_Identifier
=> An
,
2014 Object_Definition
=> New_Occurrence_Of
(Index_T
, Loc
),
2015 Expression
=> Arr_Attr
(A
, Name_First
, N
)),
2017 Make_Object_Declaration
(Loc
,
2018 Defining_Identifier
=> Bn
,
2019 Object_Definition
=> New_Occurrence_Of
(Index_T
, Loc
),
2020 Expression
=> Arr_Attr
(B
, Name_First
, N
))),
2022 Handled_Statement_Sequence
=>
2023 Make_Handled_Sequence_Of_Statements
(Loc
,
2024 Statements
=> New_List
(Loop_Stm
)));
2026 -- If no separate indexes, return loop statement with explicit
2027 -- iteration scheme on its own
2031 Make_Implicit_Loop_Statement
(Nod
,
2032 Statements
=> Stm_List
,
2034 Make_Iteration_Scheme
(Loc
,
2035 Loop_Parameter_Specification
=>
2036 Make_Loop_Parameter_Specification
(Loc
,
2037 Defining_Identifier
=> An
,
2038 Discrete_Subtype_Definition
=>
2039 Arr_Attr
(A
, Name_Range
, N
))));
2042 end Handle_One_Dimension
;
2044 -----------------------
2045 -- Test_Empty_Arrays --
2046 -----------------------
2048 function Test_Empty_Arrays
return Node_Id
is
2058 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
2061 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
2062 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
2066 Left_Opnd
=> Arr_Attr
(B
, Name_Length
, J
),
2067 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
2076 Left_Opnd
=> Relocate_Node
(Alist
),
2077 Right_Opnd
=> Atest
);
2081 Left_Opnd
=> Relocate_Node
(Blist
),
2082 Right_Opnd
=> Btest
);
2089 Right_Opnd
=> Blist
);
2090 end Test_Empty_Arrays
;
2092 -----------------------------
2093 -- Test_Lengths_Correspond --
2094 -----------------------------
2096 function Test_Lengths_Correspond
return Node_Id
is
2102 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
2105 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
2106 Right_Opnd
=> Arr_Attr
(B
, Name_Length
, J
));
2113 Left_Opnd
=> Relocate_Node
(Result
),
2114 Right_Opnd
=> Rtest
);
2119 end Test_Lengths_Correspond
;
2121 -- Start of processing for Expand_Array_Equality
2124 Ltyp
:= Get_Arg_Type
(Lhs
);
2125 Rtyp
:= Get_Arg_Type
(Rhs
);
2127 -- For now, if the argument types are not the same, go to the base type,
2128 -- since the code assumes that the formals have the same type. This is
2129 -- fixable in future ???
2131 if Ltyp
/= Rtyp
then
2132 Ltyp
:= Base_Type
(Ltyp
);
2133 Rtyp
:= Base_Type
(Rtyp
);
2134 pragma Assert
(Ltyp
= Rtyp
);
2137 -- Build list of formals for function
2139 Formals
:= New_List
(
2140 Make_Parameter_Specification
(Loc
,
2141 Defining_Identifier
=> A
,
2142 Parameter_Type
=> New_Occurrence_Of
(Ltyp
, Loc
)),
2144 Make_Parameter_Specification
(Loc
,
2145 Defining_Identifier
=> B
,
2146 Parameter_Type
=> New_Occurrence_Of
(Rtyp
, Loc
)));
2148 Func_Name
:= Make_Temporary
(Loc
, 'E');
2150 -- Build statement sequence for function
2153 Make_Subprogram_Body
(Loc
,
2155 Make_Function_Specification
(Loc
,
2156 Defining_Unit_Name
=> Func_Name
,
2157 Parameter_Specifications
=> Formals
,
2158 Result_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
)),
2160 Declarations
=> Decls
,
2162 Handled_Statement_Sequence
=>
2163 Make_Handled_Sequence_Of_Statements
(Loc
,
2164 Statements
=> New_List
(
2166 Make_Implicit_If_Statement
(Nod
,
2167 Condition
=> Test_Empty_Arrays
,
2168 Then_Statements
=> New_List
(
2169 Make_Simple_Return_Statement
(Loc
,
2171 New_Occurrence_Of
(Standard_True
, Loc
)))),
2173 Make_Implicit_If_Statement
(Nod
,
2174 Condition
=> Test_Lengths_Correspond
,
2175 Then_Statements
=> New_List
(
2176 Make_Simple_Return_Statement
(Loc
,
2177 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
)))),
2179 Handle_One_Dimension
(1, First_Index
(Ltyp
)),
2181 Make_Simple_Return_Statement
(Loc
,
2182 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)))));
2184 Set_Has_Completion
(Func_Name
, True);
2185 Set_Is_Inlined
(Func_Name
);
2187 -- If the array type is distinct from the type of the arguments, it
2188 -- is the full view of a private type. Apply an unchecked conversion
2189 -- to insure that analysis of the call succeeds.
2199 or else Base_Type
(Etype
(Lhs
)) /= Base_Type
(Ltyp
)
2201 L
:= OK_Convert_To
(Ltyp
, Lhs
);
2205 or else Base_Type
(Etype
(Rhs
)) /= Base_Type
(Rtyp
)
2207 R
:= OK_Convert_To
(Rtyp
, Rhs
);
2210 Actuals
:= New_List
(L
, R
);
2213 Append_To
(Bodies
, Func_Body
);
2216 Make_Function_Call
(Loc
,
2217 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
2218 Parameter_Associations
=> Actuals
);
2219 end Expand_Array_Equality
;
2221 -----------------------------
2222 -- Expand_Boolean_Operator --
2223 -----------------------------
2225 -- Note that we first get the actual subtypes of the operands, since we
2226 -- always want to deal with types that have bounds.
2228 procedure Expand_Boolean_Operator
(N
: Node_Id
) is
2229 Typ
: constant Entity_Id
:= Etype
(N
);
2232 -- Special case of bit packed array where both operands are known to be
2233 -- properly aligned. In this case we use an efficient run time routine
2234 -- to carry out the operation (see System.Bit_Ops).
2236 if Is_Bit_Packed_Array
(Typ
)
2237 and then not Is_Possibly_Unaligned_Object
(Left_Opnd
(N
))
2238 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
2240 Expand_Packed_Boolean_Operator
(N
);
2244 -- For the normal non-packed case, the general expansion is to build
2245 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2246 -- and then inserting it into the tree. The original operator node is
2247 -- then rewritten as a call to this function. We also use this in the
2248 -- packed case if either operand is a possibly unaligned object.
2251 Loc
: constant Source_Ptr
:= Sloc
(N
);
2252 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
2253 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
2254 Func_Body
: Node_Id
;
2255 Func_Name
: Entity_Id
;
2258 Convert_To_Actual_Subtype
(L
);
2259 Convert_To_Actual_Subtype
(R
);
2260 Ensure_Defined
(Etype
(L
), N
);
2261 Ensure_Defined
(Etype
(R
), N
);
2262 Apply_Length_Check
(R
, Etype
(L
));
2264 if Nkind
(N
) = N_Op_Xor
then
2265 Silly_Boolean_Array_Xor_Test
(N
, Etype
(L
));
2268 if Nkind
(Parent
(N
)) = N_Assignment_Statement
2269 and then Safe_In_Place_Array_Op
(Name
(Parent
(N
)), L
, R
)
2271 Build_Boolean_Array_Proc_Call
(Parent
(N
), L
, R
);
2273 elsif Nkind
(Parent
(N
)) = N_Op_Not
2274 and then Nkind
(N
) = N_Op_And
2275 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
2276 and then Safe_In_Place_Array_Op
(Name
(Parent
(Parent
(N
))), L
, R
)
2281 Func_Body
:= Make_Boolean_Array_Op
(Etype
(L
), N
);
2282 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
2283 Insert_Action
(N
, Func_Body
);
2285 -- Now rewrite the expression with a call
2288 Make_Function_Call
(Loc
,
2289 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
2290 Parameter_Associations
=>
2293 Make_Type_Conversion
2294 (Loc
, New_Occurrence_Of
(Etype
(L
), Loc
), R
))));
2296 Analyze_And_Resolve
(N
, Typ
);
2299 end Expand_Boolean_Operator
;
2301 ------------------------------------------------
2302 -- Expand_Compare_Minimize_Eliminate_Overflow --
2303 ------------------------------------------------
2305 procedure Expand_Compare_Minimize_Eliminate_Overflow
(N
: Node_Id
) is
2306 Loc
: constant Source_Ptr
:= Sloc
(N
);
2308 Result_Type
: constant Entity_Id
:= Etype
(N
);
2309 -- Capture result type (could be a derived boolean type)
2314 LLIB
: constant Entity_Id
:= Base_Type
(Standard_Long_Long_Integer
);
2315 -- Entity for Long_Long_Integer'Base
2317 Check
: constant Overflow_Mode_Type
:= Overflow_Check_Mode
;
2318 -- Current overflow checking mode
2321 procedure Set_False
;
2322 -- These procedures rewrite N with an occurrence of Standard_True or
2323 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2329 procedure Set_False
is
2331 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
2332 Warn_On_Known_Condition
(N
);
2339 procedure Set_True
is
2341 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
2342 Warn_On_Known_Condition
(N
);
2345 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2348 -- Nothing to do unless we have a comparison operator with operands
2349 -- that are signed integer types, and we are operating in either
2350 -- MINIMIZED or ELIMINATED overflow checking mode.
2352 if Nkind
(N
) not in N_Op_Compare
2353 or else Check
not in Minimized_Or_Eliminated
2354 or else not Is_Signed_Integer_Type
(Etype
(Left_Opnd
(N
)))
2359 -- OK, this is the case we are interested in. First step is to process
2360 -- our operands using the Minimize_Eliminate circuitry which applies
2361 -- this processing to the two operand subtrees.
2363 Minimize_Eliminate_Overflows
2364 (Left_Opnd
(N
), Llo
, Lhi
, Top_Level
=> False);
2365 Minimize_Eliminate_Overflows
2366 (Right_Opnd
(N
), Rlo
, Rhi
, Top_Level
=> False);
2368 -- See if the range information decides the result of the comparison.
2369 -- We can only do this if we in fact have full range information (which
2370 -- won't be the case if either operand is bignum at this stage).
2372 if Llo
/= No_Uint
and then Rlo
/= No_Uint
then
2373 case N_Op_Compare
(Nkind
(N
)) is
2375 if Llo
= Lhi
and then Rlo
= Rhi
and then Llo
= Rlo
then
2377 elsif Llo
> Rhi
or else Lhi
< Rlo
then
2384 elsif Lhi
< Rlo
then
2391 elsif Lhi
<= Rlo
then
2398 elsif Lhi
<= Rlo
then
2405 elsif Lhi
< Rlo
then
2410 if Llo
= Lhi
and then Rlo
= Rhi
and then Llo
= Rlo
then
2412 elsif Llo
> Rhi
or else Lhi
< Rlo
then
2417 -- All done if we did the rewrite
2419 if Nkind
(N
) not in N_Op_Compare
then
2424 -- Otherwise, time to do the comparison
2427 Ltype
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
2428 Rtype
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
2431 -- If the two operands have the same signed integer type we are
2432 -- all set, nothing more to do. This is the case where either
2433 -- both operands were unchanged, or we rewrote both of them to
2434 -- be Long_Long_Integer.
2436 -- Note: Entity for the comparison may be wrong, but it's not worth
2437 -- the effort to change it, since the back end does not use it.
2439 if Is_Signed_Integer_Type
(Ltype
)
2440 and then Base_Type
(Ltype
) = Base_Type
(Rtype
)
2444 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2446 elsif Is_RTE
(Ltype
, RE_Bignum
) or else Is_RTE
(Rtype
, RE_Bignum
) then
2448 Left
: Node_Id
:= Left_Opnd
(N
);
2449 Right
: Node_Id
:= Right_Opnd
(N
);
2450 -- Bignum references for left and right operands
2453 if not Is_RTE
(Ltype
, RE_Bignum
) then
2454 Left
:= Convert_To_Bignum
(Left
);
2455 elsif not Is_RTE
(Rtype
, RE_Bignum
) then
2456 Right
:= Convert_To_Bignum
(Right
);
2459 -- We rewrite our node with:
2462 -- Bnn : Result_Type;
2464 -- M : Mark_Id := SS_Mark;
2466 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2474 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
2475 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
2479 case N_Op_Compare
(Nkind
(N
)) is
2480 when N_Op_Eq
=> Ent
:= RE_Big_EQ
;
2481 when N_Op_Ge
=> Ent
:= RE_Big_GE
;
2482 when N_Op_Gt
=> Ent
:= RE_Big_GT
;
2483 when N_Op_Le
=> Ent
:= RE_Big_LE
;
2484 when N_Op_Lt
=> Ent
:= RE_Big_LT
;
2485 when N_Op_Ne
=> Ent
:= RE_Big_NE
;
2488 -- Insert assignment to Bnn into the bignum block
2491 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
2492 Make_Assignment_Statement
(Loc
,
2493 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
2495 Make_Function_Call
(Loc
,
2497 New_Occurrence_Of
(RTE
(Ent
), Loc
),
2498 Parameter_Associations
=> New_List
(Left
, Right
))));
2500 -- Now do the rewrite with expression actions
2503 Make_Expression_With_Actions
(Loc
,
2504 Actions
=> New_List
(
2505 Make_Object_Declaration
(Loc
,
2506 Defining_Identifier
=> Bnn
,
2507 Object_Definition
=>
2508 New_Occurrence_Of
(Result_Type
, Loc
)),
2510 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
2511 Analyze_And_Resolve
(N
, Result_Type
);
2515 -- No bignums involved, but types are different, so we must have
2516 -- rewritten one of the operands as a Long_Long_Integer but not
2519 -- If left operand is Long_Long_Integer, convert right operand
2520 -- and we are done (with a comparison of two Long_Long_Integers).
2522 elsif Ltype
= LLIB
then
2523 Convert_To_And_Rewrite
(LLIB
, Right_Opnd
(N
));
2524 Analyze_And_Resolve
(Right_Opnd
(N
), LLIB
, Suppress
=> All_Checks
);
2527 -- If right operand is Long_Long_Integer, convert left operand
2528 -- and we are done (with a comparison of two Long_Long_Integers).
2530 -- This is the only remaining possibility
2532 else pragma Assert
(Rtype
= LLIB
);
2533 Convert_To_And_Rewrite
(LLIB
, Left_Opnd
(N
));
2534 Analyze_And_Resolve
(Left_Opnd
(N
), LLIB
, Suppress
=> All_Checks
);
2538 end Expand_Compare_Minimize_Eliminate_Overflow
;
2540 -------------------------------
2541 -- Expand_Composite_Equality --
2542 -------------------------------
2544 -- This function is only called for comparing internal fields of composite
2545 -- types when these fields are themselves composites. This is a special
2546 -- case because it is not possible to respect normal Ada visibility rules.
2548 function Expand_Composite_Equality
2553 Bodies
: List_Id
) return Node_Id
2555 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
2556 Full_Type
: Entity_Id
;
2560 function Find_Primitive_Eq
return Node_Id
;
2561 -- AI05-0123: Locate primitive equality for type if it exists, and
2562 -- build the corresponding call. If operation is abstract, replace
2563 -- call with an explicit raise. Return Empty if there is no primitive.
2565 -----------------------
2566 -- Find_Primitive_Eq --
2567 -----------------------
2569 function Find_Primitive_Eq
return Node_Id
is
2574 Prim_E
:= First_Elmt
(Collect_Primitive_Operations
(Typ
));
2575 while Present
(Prim_E
) loop
2576 Prim
:= Node
(Prim_E
);
2578 -- Locate primitive equality with the right signature
2580 if Chars
(Prim
) = Name_Op_Eq
2581 and then Etype
(First_Formal
(Prim
)) =
2582 Etype
(Next_Formal
(First_Formal
(Prim
)))
2583 and then Etype
(Prim
) = Standard_Boolean
2585 if Is_Abstract_Subprogram
(Prim
) then
2587 Make_Raise_Program_Error
(Loc
,
2588 Reason
=> PE_Explicit_Raise
);
2592 Make_Function_Call
(Loc
,
2593 Name
=> New_Occurrence_Of
(Prim
, Loc
),
2594 Parameter_Associations
=> New_List
(Lhs
, Rhs
));
2601 -- If not found, predefined operation will be used
2604 end Find_Primitive_Eq
;
2606 -- Start of processing for Expand_Composite_Equality
2609 if Is_Private_Type
(Typ
) then
2610 Full_Type
:= Underlying_Type
(Typ
);
2615 -- If the private type has no completion the context may be the
2616 -- expansion of a composite equality for a composite type with some
2617 -- still incomplete components. The expression will not be analyzed
2618 -- until the enclosing type is completed, at which point this will be
2619 -- properly expanded, unless there is a bona fide completion error.
2621 if No
(Full_Type
) then
2622 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2625 Full_Type
:= Base_Type
(Full_Type
);
2627 -- When the base type itself is private, use the full view to expand
2628 -- the composite equality.
2630 if Is_Private_Type
(Full_Type
) then
2631 Full_Type
:= Underlying_Type
(Full_Type
);
2634 -- Case of array types
2636 if Is_Array_Type
(Full_Type
) then
2638 -- If the operand is an elementary type other than a floating-point
2639 -- type, then we can simply use the built-in block bitwise equality,
2640 -- since the predefined equality operators always apply and bitwise
2641 -- equality is fine for all these cases.
2643 if Is_Elementary_Type
(Component_Type
(Full_Type
))
2644 and then not Is_Floating_Point_Type
(Component_Type
(Full_Type
))
2646 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2648 -- For composite component types, and floating-point types, use the
2649 -- expansion. This deals with tagged component types (where we use
2650 -- the applicable equality routine) and floating-point, (where we
2651 -- need to worry about negative zeroes), and also the case of any
2652 -- composite type recursively containing such fields.
2655 return Expand_Array_Equality
(Nod
, Lhs
, Rhs
, Bodies
, Full_Type
);
2658 -- Case of tagged record types
2660 elsif Is_Tagged_Type
(Full_Type
) then
2662 -- Call the primitive operation "=" of this type
2664 if Is_Class_Wide_Type
(Full_Type
) then
2665 Full_Type
:= Root_Type
(Full_Type
);
2668 -- If this is derived from an untagged private type completed with a
2669 -- tagged type, it does not have a full view, so we use the primitive
2670 -- operations of the private type. This check should no longer be
2671 -- necessary when these types receive their full views ???
2673 if Is_Private_Type
(Typ
)
2674 and then not Is_Tagged_Type
(Typ
)
2675 and then not Is_Controlled
(Typ
)
2676 and then Is_Derived_Type
(Typ
)
2677 and then No
(Full_View
(Typ
))
2679 Prim
:= First_Elmt
(Collect_Primitive_Operations
(Typ
));
2681 Prim
:= First_Elmt
(Primitive_Operations
(Full_Type
));
2685 Eq_Op
:= Node
(Prim
);
2686 exit when Chars
(Eq_Op
) = Name_Op_Eq
2687 and then Etype
(First_Formal
(Eq_Op
)) =
2688 Etype
(Next_Formal
(First_Formal
(Eq_Op
)))
2689 and then Base_Type
(Etype
(Eq_Op
)) = Standard_Boolean
;
2691 pragma Assert
(Present
(Prim
));
2694 Eq_Op
:= Node
(Prim
);
2697 Make_Function_Call
(Loc
,
2698 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2699 Parameter_Associations
=>
2701 (Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Lhs
),
2702 Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Rhs
)));
2704 -- Case of untagged record types
2706 elsif Is_Record_Type
(Full_Type
) then
2707 Eq_Op
:= TSS
(Full_Type
, TSS_Composite_Equality
);
2709 if Present
(Eq_Op
) then
2710 if Etype
(First_Formal
(Eq_Op
)) /= Full_Type
then
2712 -- Inherited equality from parent type. Convert the actuals to
2713 -- match signature of operation.
2716 T
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
2720 Make_Function_Call
(Loc
,
2721 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2722 Parameter_Associations
=> New_List
(
2723 OK_Convert_To
(T
, Lhs
),
2724 OK_Convert_To
(T
, Rhs
)));
2728 -- Comparison between Unchecked_Union components
2730 if Is_Unchecked_Union
(Full_Type
) then
2732 Lhs_Type
: Node_Id
:= Full_Type
;
2733 Rhs_Type
: Node_Id
:= Full_Type
;
2734 Lhs_Discr_Val
: Node_Id
;
2735 Rhs_Discr_Val
: Node_Id
;
2740 if Nkind
(Lhs
) = N_Selected_Component
then
2741 Lhs_Type
:= Etype
(Entity
(Selector_Name
(Lhs
)));
2746 if Nkind
(Rhs
) = N_Selected_Component
then
2747 Rhs_Type
:= Etype
(Entity
(Selector_Name
(Rhs
)));
2750 -- Lhs of the composite equality
2752 if Is_Constrained
(Lhs_Type
) then
2754 -- Since the enclosing record type can never be an
2755 -- Unchecked_Union (this code is executed for records
2756 -- that do not have variants), we may reference its
2759 if Nkind
(Lhs
) = N_Selected_Component
2760 and then Has_Per_Object_Constraint
2761 (Entity
(Selector_Name
(Lhs
)))
2764 Make_Selected_Component
(Loc
,
2765 Prefix
=> Prefix
(Lhs
),
2768 (Get_Discriminant_Value
2769 (First_Discriminant
(Lhs_Type
),
2771 Stored_Constraint
(Lhs_Type
))));
2776 (Get_Discriminant_Value
2777 (First_Discriminant
(Lhs_Type
),
2779 Stored_Constraint
(Lhs_Type
)));
2783 -- It is not possible to infer the discriminant since
2784 -- the subtype is not constrained.
2787 Make_Raise_Program_Error
(Loc
,
2788 Reason
=> PE_Unchecked_Union_Restriction
);
2791 -- Rhs of the composite equality
2793 if Is_Constrained
(Rhs_Type
) then
2794 if Nkind
(Rhs
) = N_Selected_Component
2795 and then Has_Per_Object_Constraint
2796 (Entity
(Selector_Name
(Rhs
)))
2799 Make_Selected_Component
(Loc
,
2800 Prefix
=> Prefix
(Rhs
),
2803 (Get_Discriminant_Value
2804 (First_Discriminant
(Rhs_Type
),
2806 Stored_Constraint
(Rhs_Type
))));
2811 (Get_Discriminant_Value
2812 (First_Discriminant
(Rhs_Type
),
2814 Stored_Constraint
(Rhs_Type
)));
2819 Make_Raise_Program_Error
(Loc
,
2820 Reason
=> PE_Unchecked_Union_Restriction
);
2823 -- Call the TSS equality function with the inferred
2824 -- discriminant values.
2827 Make_Function_Call
(Loc
,
2828 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2829 Parameter_Associations
=> New_List
(
2836 -- All cases other than comparing Unchecked_Union types
2840 T
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
2843 Make_Function_Call
(Loc
,
2845 New_Occurrence_Of
(Eq_Op
, Loc
),
2846 Parameter_Associations
=> New_List
(
2847 OK_Convert_To
(T
, Lhs
),
2848 OK_Convert_To
(T
, Rhs
)));
2853 -- Equality composes in Ada 2012 for untagged record types. It also
2854 -- composes for bounded strings, because they are part of the
2855 -- predefined environment. We could make it compose for bounded
2856 -- strings by making them tagged, or by making sure all subcomponents
2857 -- are set to the same value, even when not used. Instead, we have
2858 -- this special case in the compiler, because it's more efficient.
2860 elsif Ada_Version
>= Ada_2012
or else Is_Bounded_String
(Typ
) then
2862 -- If no TSS has been created for the type, check whether there is
2863 -- a primitive equality declared for it.
2866 Op
: constant Node_Id
:= Find_Primitive_Eq
;
2869 -- Use user-defined primitive if it exists, otherwise use
2870 -- predefined equality.
2872 if Present
(Op
) then
2875 return Make_Op_Eq
(Loc
, Lhs
, Rhs
);
2880 return Expand_Record_Equality
(Nod
, Full_Type
, Lhs
, Rhs
, Bodies
);
2883 -- Non-composite types (always use predefined equality)
2886 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2888 end Expand_Composite_Equality
;
2890 ------------------------
2891 -- Expand_Concatenate --
2892 ------------------------
2894 procedure Expand_Concatenate
(Cnode
: Node_Id
; Opnds
: List_Id
) is
2895 Loc
: constant Source_Ptr
:= Sloc
(Cnode
);
2897 Atyp
: constant Entity_Id
:= Base_Type
(Etype
(Cnode
));
2898 -- Result type of concatenation
2900 Ctyp
: constant Entity_Id
:= Base_Type
(Component_Type
(Etype
(Cnode
)));
2901 -- Component type. Elements of this component type can appear as one
2902 -- of the operands of concatenation as well as arrays.
2904 Istyp
: constant Entity_Id
:= Etype
(First_Index
(Atyp
));
2907 Ityp
: constant Entity_Id
:= Base_Type
(Istyp
);
2908 -- Index type. This is the base type of the index subtype, and is used
2909 -- for all computed bounds (which may be out of range of Istyp in the
2910 -- case of null ranges).
2913 -- This is the type we use to do arithmetic to compute the bounds and
2914 -- lengths of operands. The choice of this type is a little subtle and
2915 -- is discussed in a separate section at the start of the body code.
2917 Concatenation_Error
: exception;
2918 -- Raised if concatenation is sure to raise a CE
2920 Result_May_Be_Null
: Boolean := True;
2921 -- Reset to False if at least one operand is encountered which is known
2922 -- at compile time to be non-null. Used for handling the special case
2923 -- of setting the high bound to the last operand high bound for a null
2924 -- result, thus ensuring a proper high bound in the super-flat case.
2926 N
: constant Nat
:= List_Length
(Opnds
);
2927 -- Number of concatenation operands including possibly null operands
2930 -- Number of operands excluding any known to be null, except that the
2931 -- last operand is always retained, in case it provides the bounds for
2935 -- Current operand being processed in the loop through operands. After
2936 -- this loop is complete, always contains the last operand (which is not
2937 -- the same as Operands (NN), since null operands are skipped).
2939 -- Arrays describing the operands, only the first NN entries of each
2940 -- array are set (NN < N when we exclude known null operands).
2942 Is_Fixed_Length
: array (1 .. N
) of Boolean;
2943 -- True if length of corresponding operand known at compile time
2945 Operands
: array (1 .. N
) of Node_Id
;
2946 -- Set to the corresponding entry in the Opnds list (but note that null
2947 -- operands are excluded, so not all entries in the list are stored).
2949 Fixed_Length
: array (1 .. N
) of Uint
;
2950 -- Set to length of operand. Entries in this array are set only if the
2951 -- corresponding entry in Is_Fixed_Length is True.
2953 Opnd_Low_Bound
: array (1 .. N
) of Node_Id
;
2954 -- Set to lower bound of operand. Either an integer literal in the case
2955 -- where the bound is known at compile time, else actual lower bound.
2956 -- The operand low bound is of type Ityp.
2958 Var_Length
: array (1 .. N
) of Entity_Id
;
2959 -- Set to an entity of type Natural that contains the length of an
2960 -- operand whose length is not known at compile time. Entries in this
2961 -- array are set only if the corresponding entry in Is_Fixed_Length
2962 -- is False. The entity is of type Artyp.
2964 Aggr_Length
: array (0 .. N
) of Node_Id
;
2965 -- The J'th entry in an expression node that represents the total length
2966 -- of operands 1 through J. It is either an integer literal node, or a
2967 -- reference to a constant entity with the right value, so it is fine
2968 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2969 -- entry always is set to zero. The length is of type Artyp.
2971 Low_Bound
: Node_Id
;
2972 -- A tree node representing the low bound of the result (of type Ityp).
2973 -- This is either an integer literal node, or an identifier reference to
2974 -- a constant entity initialized to the appropriate value.
2976 Last_Opnd_Low_Bound
: Node_Id
;
2977 -- A tree node representing the low bound of the last operand. This
2978 -- need only be set if the result could be null. It is used for the
2979 -- special case of setting the right low bound for a null result.
2980 -- This is of type Ityp.
2982 Last_Opnd_High_Bound
: Node_Id
;
2983 -- A tree node representing the high bound of the last operand. This
2984 -- need only be set if the result could be null. It is used for the
2985 -- special case of setting the right high bound for a null result.
2986 -- This is of type Ityp.
2988 High_Bound
: Node_Id
;
2989 -- A tree node representing the high bound of the result (of type Ityp)
2992 -- Result of the concatenation (of type Ityp)
2994 Actions
: constant List_Id
:= New_List
;
2995 -- Collect actions to be inserted
2997 Known_Non_Null_Operand_Seen
: Boolean;
2998 -- Set True during generation of the assignments of operands into
2999 -- result once an operand known to be non-null has been seen.
3001 function Make_Artyp_Literal
(Val
: Nat
) return Node_Id
;
3002 -- This function makes an N_Integer_Literal node that is returned in
3003 -- analyzed form with the type set to Artyp. Importantly this literal
3004 -- is not flagged as static, so that if we do computations with it that
3005 -- result in statically detected out of range conditions, we will not
3006 -- generate error messages but instead warning messages.
3008 function To_Artyp
(X
: Node_Id
) return Node_Id
;
3009 -- Given a node of type Ityp, returns the corresponding value of type
3010 -- Artyp. For non-enumeration types, this is a plain integer conversion.
3011 -- For enum types, the Pos of the value is returned.
3013 function To_Ityp
(X
: Node_Id
) return Node_Id
;
3014 -- The inverse function (uses Val in the case of enumeration types)
3016 ------------------------
3017 -- Make_Artyp_Literal --
3018 ------------------------
3020 function Make_Artyp_Literal
(Val
: Nat
) return Node_Id
is
3021 Result
: constant Node_Id
:= Make_Integer_Literal
(Loc
, Val
);
3023 Set_Etype
(Result
, Artyp
);
3024 Set_Analyzed
(Result
, True);
3025 Set_Is_Static_Expression
(Result
, False);
3027 end Make_Artyp_Literal
;
3033 function To_Artyp
(X
: Node_Id
) return Node_Id
is
3035 if Ityp
= Base_Type
(Artyp
) then
3038 elsif Is_Enumeration_Type
(Ityp
) then
3040 Make_Attribute_Reference
(Loc
,
3041 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
3042 Attribute_Name
=> Name_Pos
,
3043 Expressions
=> New_List
(X
));
3046 return Convert_To
(Artyp
, X
);
3054 function To_Ityp
(X
: Node_Id
) return Node_Id
is
3056 if Is_Enumeration_Type
(Ityp
) then
3058 Make_Attribute_Reference
(Loc
,
3059 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
3060 Attribute_Name
=> Name_Val
,
3061 Expressions
=> New_List
(X
));
3063 -- Case where we will do a type conversion
3066 if Ityp
= Base_Type
(Artyp
) then
3069 return Convert_To
(Ityp
, X
);
3074 -- Local Declarations
3076 Lib_Level_Target
: constant Boolean :=
3077 Nkind
(Parent
(Cnode
)) = N_Object_Declaration
3079 Is_Library_Level_Entity
(Defining_Identifier
(Parent
(Cnode
)));
3081 -- If the concatenation declares a library level entity, we call the
3082 -- built-in concatenation routines to prevent code bloat, regardless
3083 -- of optimization level. This is space-efficient, and prevent linking
3084 -- problems when units are compiled with different optimizations.
3086 Opnd_Typ
: Entity_Id
;
3093 -- Start of processing for Expand_Concatenate
3096 -- Choose an appropriate computational type
3098 -- We will be doing calculations of lengths and bounds in this routine
3099 -- and computing one from the other in some cases, e.g. getting the high
3100 -- bound by adding the length-1 to the low bound.
3102 -- We can't just use the index type, or even its base type for this
3103 -- purpose for two reasons. First it might be an enumeration type which
3104 -- is not suitable for computations of any kind, and second it may
3105 -- simply not have enough range. For example if the index type is
3106 -- -128..+127 then lengths can be up to 256, which is out of range of
3109 -- For enumeration types, we can simply use Standard_Integer, this is
3110 -- sufficient since the actual number of enumeration literals cannot
3111 -- possibly exceed the range of integer (remember we will be doing the
3112 -- arithmetic with POS values, not representation values).
3114 if Is_Enumeration_Type
(Ityp
) then
3115 Artyp
:= Standard_Integer
;
3117 -- If index type is Positive, we use the standard unsigned type, to give
3118 -- more room on the top of the range, obviating the need for an overflow
3119 -- check when creating the upper bound. This is needed to avoid junk
3120 -- overflow checks in the common case of String types.
3122 -- ??? Disabled for now
3124 -- elsif Istyp = Standard_Positive then
3125 -- Artyp := Standard_Unsigned;
3127 -- For modular types, we use a 32-bit modular type for types whose size
3128 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
3129 -- identity type, and for larger unsigned types we use 64-bits.
3131 elsif Is_Modular_Integer_Type
(Ityp
) then
3132 if RM_Size
(Ityp
) < RM_Size
(Standard_Unsigned
) then
3133 Artyp
:= Standard_Unsigned
;
3134 elsif RM_Size
(Ityp
) = RM_Size
(Standard_Unsigned
) then
3137 Artyp
:= RTE
(RE_Long_Long_Unsigned
);
3140 -- Similar treatment for signed types
3143 if RM_Size
(Ityp
) < RM_Size
(Standard_Integer
) then
3144 Artyp
:= Standard_Integer
;
3145 elsif RM_Size
(Ityp
) = RM_Size
(Standard_Integer
) then
3148 Artyp
:= Standard_Long_Long_Integer
;
3152 -- Supply dummy entry at start of length array
3154 Aggr_Length
(0) := Make_Artyp_Literal
(0);
3156 -- Go through operands setting up the above arrays
3160 Opnd
:= Remove_Head
(Opnds
);
3161 Opnd_Typ
:= Etype
(Opnd
);
3163 -- The parent got messed up when we put the operands in a list,
3164 -- so now put back the proper parent for the saved operand, that
3165 -- is to say the concatenation node, to make sure that each operand
3166 -- is seen as a subexpression, e.g. if actions must be inserted.
3168 Set_Parent
(Opnd
, Cnode
);
3170 -- Set will be True when we have setup one entry in the array
3174 -- Singleton element (or character literal) case
3176 if Base_Type
(Opnd_Typ
) = Ctyp
then
3178 Operands
(NN
) := Opnd
;
3179 Is_Fixed_Length
(NN
) := True;
3180 Fixed_Length
(NN
) := Uint_1
;
3181 Result_May_Be_Null
:= False;
3183 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
3184 -- since we know that the result cannot be null).
3186 Opnd_Low_Bound
(NN
) :=
3187 Make_Attribute_Reference
(Loc
,
3188 Prefix
=> New_Occurrence_Of
(Istyp
, Loc
),
3189 Attribute_Name
=> Name_First
);
3193 -- String literal case (can only occur for strings of course)
3195 elsif Nkind
(Opnd
) = N_String_Literal
then
3196 Len
:= String_Literal_Length
(Opnd_Typ
);
3199 Result_May_Be_Null
:= False;
3202 -- Capture last operand low and high bound if result could be null
3204 if J
= N
and then Result_May_Be_Null
then
3205 Last_Opnd_Low_Bound
:=
3206 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
));
3208 Last_Opnd_High_Bound
:=
3209 Make_Op_Subtract
(Loc
,
3211 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
)),
3212 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
3215 -- Skip null string literal
3217 if J
< N
and then Len
= 0 then
3222 Operands
(NN
) := Opnd
;
3223 Is_Fixed_Length
(NN
) := True;
3225 -- Set length and bounds
3227 Fixed_Length
(NN
) := Len
;
3229 Opnd_Low_Bound
(NN
) :=
3230 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
));
3237 -- Check constrained case with known bounds
3239 if Is_Constrained
(Opnd_Typ
) then
3241 Index
: constant Node_Id
:= First_Index
(Opnd_Typ
);
3242 Indx_Typ
: constant Entity_Id
:= Etype
(Index
);
3243 Lo
: constant Node_Id
:= Type_Low_Bound
(Indx_Typ
);
3244 Hi
: constant Node_Id
:= Type_High_Bound
(Indx_Typ
);
3247 -- Fixed length constrained array type with known at compile
3248 -- time bounds is last case of fixed length operand.
3250 if Compile_Time_Known_Value
(Lo
)
3252 Compile_Time_Known_Value
(Hi
)
3255 Loval
: constant Uint
:= Expr_Value
(Lo
);
3256 Hival
: constant Uint
:= Expr_Value
(Hi
);
3257 Len
: constant Uint
:=
3258 UI_Max
(Hival
- Loval
+ 1, Uint_0
);
3262 Result_May_Be_Null
:= False;
3265 -- Capture last operand bounds if result could be null
3267 if J
= N
and then Result_May_Be_Null
then
3268 Last_Opnd_Low_Bound
:=
3270 Make_Integer_Literal
(Loc
, Expr_Value
(Lo
)));
3272 Last_Opnd_High_Bound
:=
3274 Make_Integer_Literal
(Loc
, Expr_Value
(Hi
)));
3277 -- Exclude null length case unless last operand
3279 if J
< N
and then Len
= 0 then
3284 Operands
(NN
) := Opnd
;
3285 Is_Fixed_Length
(NN
) := True;
3286 Fixed_Length
(NN
) := Len
;
3288 Opnd_Low_Bound
(NN
) :=
3290 (Make_Integer_Literal
(Loc
, Expr_Value
(Lo
)));
3297 -- All cases where the length is not known at compile time, or the
3298 -- special case of an operand which is known to be null but has a
3299 -- lower bound other than 1 or is other than a string type.
3304 -- Capture operand bounds
3306 Opnd_Low_Bound
(NN
) :=
3307 Make_Attribute_Reference
(Loc
,
3309 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3310 Attribute_Name
=> Name_First
);
3312 -- Capture last operand bounds if result could be null
3314 if J
= N
and Result_May_Be_Null
then
3315 Last_Opnd_Low_Bound
:=
3317 Make_Attribute_Reference
(Loc
,
3319 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3320 Attribute_Name
=> Name_First
));
3322 Last_Opnd_High_Bound
:=
3324 Make_Attribute_Reference
(Loc
,
3326 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3327 Attribute_Name
=> Name_Last
));
3330 -- Capture length of operand in entity
3332 Operands
(NN
) := Opnd
;
3333 Is_Fixed_Length
(NN
) := False;
3335 Var_Length
(NN
) := Make_Temporary
(Loc
, 'L');
3338 Make_Object_Declaration
(Loc
,
3339 Defining_Identifier
=> Var_Length
(NN
),
3340 Constant_Present
=> True,
3341 Object_Definition
=> New_Occurrence_Of
(Artyp
, Loc
),
3343 Make_Attribute_Reference
(Loc
,
3345 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3346 Attribute_Name
=> Name_Length
)));
3350 -- Set next entry in aggregate length array
3352 -- For first entry, make either integer literal for fixed length
3353 -- or a reference to the saved length for variable length.
3356 if Is_Fixed_Length
(1) then
3357 Aggr_Length
(1) := Make_Integer_Literal
(Loc
, Fixed_Length
(1));
3359 Aggr_Length
(1) := New_Occurrence_Of
(Var_Length
(1), Loc
);
3362 -- If entry is fixed length and only fixed lengths so far, make
3363 -- appropriate new integer literal adding new length.
3365 elsif Is_Fixed_Length
(NN
)
3366 and then Nkind
(Aggr_Length
(NN
- 1)) = N_Integer_Literal
3369 Make_Integer_Literal
(Loc
,
3370 Intval
=> Fixed_Length
(NN
) + Intval
(Aggr_Length
(NN
- 1)));
3372 -- All other cases, construct an addition node for the length and
3373 -- create an entity initialized to this length.
3376 Ent
:= Make_Temporary
(Loc
, 'L');
3378 if Is_Fixed_Length
(NN
) then
3379 Clen
:= Make_Integer_Literal
(Loc
, Fixed_Length
(NN
));
3381 Clen
:= New_Occurrence_Of
(Var_Length
(NN
), Loc
);
3385 Make_Object_Declaration
(Loc
,
3386 Defining_Identifier
=> Ent
,
3387 Constant_Present
=> True,
3388 Object_Definition
=> New_Occurrence_Of
(Artyp
, Loc
),
3391 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
- 1)),
3392 Right_Opnd
=> Clen
)));
3394 Aggr_Length
(NN
) := Make_Identifier
(Loc
, Chars
=> Chars
(Ent
));
3401 -- If we have only skipped null operands, return the last operand
3408 -- If we have only one non-null operand, return it and we are done.
3409 -- There is one case in which this cannot be done, and that is when
3410 -- the sole operand is of the element type, in which case it must be
3411 -- converted to an array, and the easiest way of doing that is to go
3412 -- through the normal general circuit.
3414 if NN
= 1 and then Base_Type
(Etype
(Operands
(1))) /= Ctyp
then
3415 Result
:= Operands
(1);
3419 -- Cases where we have a real concatenation
3421 -- Next step is to find the low bound for the result array that we
3422 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3424 -- If the ultimate ancestor of the index subtype is a constrained array
3425 -- definition, then the lower bound is that of the index subtype as
3426 -- specified by (RM 4.5.3(6)).
3428 -- The right test here is to go to the root type, and then the ultimate
3429 -- ancestor is the first subtype of this root type.
3431 if Is_Constrained
(First_Subtype
(Root_Type
(Atyp
))) then
3433 Make_Attribute_Reference
(Loc
,
3435 New_Occurrence_Of
(First_Subtype
(Root_Type
(Atyp
)), Loc
),
3436 Attribute_Name
=> Name_First
);
3438 -- If the first operand in the list has known length we know that
3439 -- the lower bound of the result is the lower bound of this operand.
3441 elsif Is_Fixed_Length
(1) then
3442 Low_Bound
:= Opnd_Low_Bound
(1);
3444 -- OK, we don't know the lower bound, we have to build a horrible
3445 -- if expression node of the form
3447 -- if Cond1'Length /= 0 then
3450 -- if Opnd2'Length /= 0 then
3455 -- The nesting ends either when we hit an operand whose length is known
3456 -- at compile time, or on reaching the last operand, whose low bound we
3457 -- take unconditionally whether or not it is null. It's easiest to do
3458 -- this with a recursive procedure:
3462 function Get_Known_Bound
(J
: Nat
) return Node_Id
;
3463 -- Returns the lower bound determined by operands J .. NN
3465 ---------------------
3466 -- Get_Known_Bound --
3467 ---------------------
3469 function Get_Known_Bound
(J
: Nat
) return Node_Id
is
3471 if Is_Fixed_Length
(J
) or else J
= NN
then
3472 return New_Copy
(Opnd_Low_Bound
(J
));
3476 Make_If_Expression
(Loc
,
3477 Expressions
=> New_List
(
3481 New_Occurrence_Of
(Var_Length
(J
), Loc
),
3483 Make_Integer_Literal
(Loc
, 0)),
3485 New_Copy
(Opnd_Low_Bound
(J
)),
3486 Get_Known_Bound
(J
+ 1)));
3488 end Get_Known_Bound
;
3491 Ent
:= Make_Temporary
(Loc
, 'L');
3494 Make_Object_Declaration
(Loc
,
3495 Defining_Identifier
=> Ent
,
3496 Constant_Present
=> True,
3497 Object_Definition
=> New_Occurrence_Of
(Ityp
, Loc
),
3498 Expression
=> Get_Known_Bound
(1)));
3500 Low_Bound
:= New_Occurrence_Of
(Ent
, Loc
);
3504 -- Now we can safely compute the upper bound, normally
3505 -- Low_Bound + Length - 1.
3510 Left_Opnd
=> To_Artyp
(New_Copy
(Low_Bound
)),
3512 Make_Op_Subtract
(Loc
,
3513 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
)),
3514 Right_Opnd
=> Make_Artyp_Literal
(1))));
3516 -- Note that calculation of the high bound may cause overflow in some
3517 -- very weird cases, so in the general case we need an overflow check on
3518 -- the high bound. We can avoid this for the common case of string types
3519 -- and other types whose index is Positive, since we chose a wider range
3520 -- for the arithmetic type.
3522 if Istyp
/= Standard_Positive
then
3523 Activate_Overflow_Check
(High_Bound
);
3526 -- Handle the exceptional case where the result is null, in which case
3527 -- case the bounds come from the last operand (so that we get the proper
3528 -- bounds if the last operand is super-flat).
3530 if Result_May_Be_Null
then
3532 Make_If_Expression
(Loc
,
3533 Expressions
=> New_List
(
3535 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
)),
3536 Right_Opnd
=> Make_Artyp_Literal
(0)),
3537 Last_Opnd_Low_Bound
,
3541 Make_If_Expression
(Loc
,
3542 Expressions
=> New_List
(
3544 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
)),
3545 Right_Opnd
=> Make_Artyp_Literal
(0)),
3546 Last_Opnd_High_Bound
,
3550 -- Here is where we insert the saved up actions
3552 Insert_Actions
(Cnode
, Actions
, Suppress
=> All_Checks
);
3554 -- Now we construct an array object with appropriate bounds. We mark
3555 -- the target as internal to prevent useless initialization when
3556 -- Initialize_Scalars is enabled. Also since this is the actual result
3557 -- entity, we make sure we have debug information for the result.
3559 Ent
:= Make_Temporary
(Loc
, 'S');
3560 Set_Is_Internal
(Ent
);
3561 Set_Needs_Debug_Info
(Ent
);
3563 -- If the bound is statically known to be out of range, we do not want
3564 -- to abort, we want a warning and a runtime constraint error. Note that
3565 -- we have arranged that the result will not be treated as a static
3566 -- constant, so we won't get an illegality during this insertion.
3568 Insert_Action
(Cnode
,
3569 Make_Object_Declaration
(Loc
,
3570 Defining_Identifier
=> Ent
,
3571 Object_Definition
=>
3572 Make_Subtype_Indication
(Loc
,
3573 Subtype_Mark
=> New_Occurrence_Of
(Atyp
, Loc
),
3575 Make_Index_Or_Discriminant_Constraint
(Loc
,
3576 Constraints
=> New_List
(
3578 Low_Bound
=> Low_Bound
,
3579 High_Bound
=> High_Bound
))))),
3580 Suppress
=> All_Checks
);
3582 -- If the result of the concatenation appears as the initializing
3583 -- expression of an object declaration, we can just rename the
3584 -- result, rather than copying it.
3586 Set_OK_To_Rename
(Ent
);
3588 -- Catch the static out of range case now
3590 if Raises_Constraint_Error
(High_Bound
) then
3591 raise Concatenation_Error
;
3594 -- Now we will generate the assignments to do the actual concatenation
3596 -- There is one case in which we will not do this, namely when all the
3597 -- following conditions are met:
3599 -- The result type is Standard.String
3601 -- There are nine or fewer retained (non-null) operands
3603 -- The optimization level is -O0
3605 -- The corresponding System.Concat_n.Str_Concat_n routine is
3606 -- available in the run time.
3608 -- The debug flag gnatd.c is not set
3610 -- If all these conditions are met then we generate a call to the
3611 -- relevant concatenation routine. The purpose of this is to avoid
3612 -- undesirable code bloat at -O0.
3614 if Atyp
= Standard_String
3615 and then NN
in 2 .. 9
3616 and then (Lib_Level_Target
3617 or else ((Optimization_Level
= 0 or else Debug_Flag_Dot_CC
)
3618 and then not Debug_Flag_Dot_C
))
3621 RR
: constant array (Nat
range 2 .. 9) of RE_Id
:=
3632 if RTE_Available
(RR
(NN
)) then
3634 Opnds
: constant List_Id
:=
3635 New_List
(New_Occurrence_Of
(Ent
, Loc
));
3638 for J
in 1 .. NN
loop
3639 if Is_List_Member
(Operands
(J
)) then
3640 Remove
(Operands
(J
));
3643 if Base_Type
(Etype
(Operands
(J
))) = Ctyp
then
3645 Make_Aggregate
(Loc
,
3646 Component_Associations
=> New_List
(
3647 Make_Component_Association
(Loc
,
3648 Choices
=> New_List
(
3649 Make_Integer_Literal
(Loc
, 1)),
3650 Expression
=> Operands
(J
)))));
3653 Append_To
(Opnds
, Operands
(J
));
3657 Insert_Action
(Cnode
,
3658 Make_Procedure_Call_Statement
(Loc
,
3659 Name
=> New_Occurrence_Of
(RTE
(RR
(NN
)), Loc
),
3660 Parameter_Associations
=> Opnds
));
3662 Result
:= New_Occurrence_Of
(Ent
, Loc
);
3669 -- Not special case so generate the assignments
3671 Known_Non_Null_Operand_Seen
:= False;
3673 for J
in 1 .. NN
loop
3675 Lo
: constant Node_Id
:=
3677 Left_Opnd
=> To_Artyp
(New_Copy
(Low_Bound
)),
3678 Right_Opnd
=> Aggr_Length
(J
- 1));
3680 Hi
: constant Node_Id
:=
3682 Left_Opnd
=> To_Artyp
(New_Copy
(Low_Bound
)),
3684 Make_Op_Subtract
(Loc
,
3685 Left_Opnd
=> Aggr_Length
(J
),
3686 Right_Opnd
=> Make_Artyp_Literal
(1)));
3689 -- Singleton case, simple assignment
3691 if Base_Type
(Etype
(Operands
(J
))) = Ctyp
then
3692 Known_Non_Null_Operand_Seen
:= True;
3693 Insert_Action
(Cnode
,
3694 Make_Assignment_Statement
(Loc
,
3696 Make_Indexed_Component
(Loc
,
3697 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
3698 Expressions
=> New_List
(To_Ityp
(Lo
))),
3699 Expression
=> Operands
(J
)),
3700 Suppress
=> All_Checks
);
3702 -- Array case, slice assignment, skipped when argument is fixed
3703 -- length and known to be null.
3705 elsif (not Is_Fixed_Length
(J
)) or else (Fixed_Length
(J
) > 0) then
3708 Make_Assignment_Statement
(Loc
,
3712 New_Occurrence_Of
(Ent
, Loc
),
3715 Low_Bound
=> To_Ityp
(Lo
),
3716 High_Bound
=> To_Ityp
(Hi
))),
3717 Expression
=> Operands
(J
));
3719 if Is_Fixed_Length
(J
) then
3720 Known_Non_Null_Operand_Seen
:= True;
3722 elsif not Known_Non_Null_Operand_Seen
then
3724 -- Here if operand length is not statically known and no
3725 -- operand known to be non-null has been processed yet.
3726 -- If operand length is 0, we do not need to perform the
3727 -- assignment, and we must avoid the evaluation of the
3728 -- high bound of the slice, since it may underflow if the
3729 -- low bound is Ityp'First.
3732 Make_Implicit_If_Statement
(Cnode
,
3736 New_Occurrence_Of
(Var_Length
(J
), Loc
),
3737 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
3738 Then_Statements
=> New_List
(Assign
));
3741 Insert_Action
(Cnode
, Assign
, Suppress
=> All_Checks
);
3747 -- Finally we build the result, which is a reference to the array object
3749 Result
:= New_Occurrence_Of
(Ent
, Loc
);
3752 Rewrite
(Cnode
, Result
);
3753 Analyze_And_Resolve
(Cnode
, Atyp
);
3756 when Concatenation_Error
=>
3758 -- Kill warning generated for the declaration of the static out of
3759 -- range high bound, and instead generate a Constraint_Error with
3760 -- an appropriate specific message.
3762 Kill_Dead_Code
(Declaration_Node
(Entity
(High_Bound
)));
3763 Apply_Compile_Time_Constraint_Error
3765 Msg
=> "concatenation result upper bound out of range??",
3766 Reason
=> CE_Range_Check_Failed
);
3767 end Expand_Concatenate
;
3769 ---------------------------------------------------
3770 -- Expand_Membership_Minimize_Eliminate_Overflow --
3771 ---------------------------------------------------
3773 procedure Expand_Membership_Minimize_Eliminate_Overflow
(N
: Node_Id
) is
3774 pragma Assert
(Nkind
(N
) = N_In
);
3775 -- Despite the name, this routine applies only to N_In, not to
3776 -- N_Not_In. The latter is always rewritten as not (X in Y).
3778 Result_Type
: constant Entity_Id
:= Etype
(N
);
3779 -- Capture result type, may be a derived boolean type
3781 Loc
: constant Source_Ptr
:= Sloc
(N
);
3782 Lop
: constant Node_Id
:= Left_Opnd
(N
);
3783 Rop
: constant Node_Id
:= Right_Opnd
(N
);
3785 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3786 -- is thus tempting to capture these values, but due to the rewrites
3787 -- that occur as a result of overflow checking, these values change
3788 -- as we go along, and it is safe just to always use Etype explicitly.
3790 Restype
: constant Entity_Id
:= Etype
(N
);
3794 -- Bounds in Minimize calls, not used currently
3796 LLIB
: constant Entity_Id
:= Base_Type
(Standard_Long_Long_Integer
);
3797 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3800 Minimize_Eliminate_Overflows
(Lop
, Lo
, Hi
, Top_Level
=> False);
3802 -- If right operand is a subtype name, and the subtype name has no
3803 -- predicate, then we can just replace the right operand with an
3804 -- explicit range T'First .. T'Last, and use the explicit range code.
3806 if Nkind
(Rop
) /= N_Range
3807 and then No
(Predicate_Function
(Etype
(Rop
)))
3810 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
3815 Make_Attribute_Reference
(Loc
,
3816 Attribute_Name
=> Name_First
,
3817 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
)),
3819 Make_Attribute_Reference
(Loc
,
3820 Attribute_Name
=> Name_Last
,
3821 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
))));
3822 Analyze_And_Resolve
(Rop
, Rtyp
, Suppress
=> All_Checks
);
3826 -- Here for the explicit range case. Note that the bounds of the range
3827 -- have not been processed for minimized or eliminated checks.
3829 if Nkind
(Rop
) = N_Range
then
3830 Minimize_Eliminate_Overflows
3831 (Low_Bound
(Rop
), Lo
, Hi
, Top_Level
=> False);
3832 Minimize_Eliminate_Overflows
3833 (High_Bound
(Rop
), Lo
, Hi
, Top_Level
=> False);
3835 -- We have A in B .. C, treated as A >= B and then A <= C
3839 if Is_RTE
(Etype
(Lop
), RE_Bignum
)
3840 or else Is_RTE
(Etype
(Low_Bound
(Rop
)), RE_Bignum
)
3841 or else Is_RTE
(Etype
(High_Bound
(Rop
)), RE_Bignum
)
3844 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
3845 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
3846 L
: constant Entity_Id
:=
3847 Make_Defining_Identifier
(Loc
, Name_uL
);
3848 Lopnd
: constant Node_Id
:= Convert_To_Bignum
(Lop
);
3849 Lbound
: constant Node_Id
:=
3850 Convert_To_Bignum
(Low_Bound
(Rop
));
3851 Hbound
: constant Node_Id
:=
3852 Convert_To_Bignum
(High_Bound
(Rop
));
3854 -- Now we rewrite the membership test node to look like
3857 -- Bnn : Result_Type;
3859 -- M : Mark_Id := SS_Mark;
3860 -- L : Bignum := Lopnd;
3862 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3870 -- Insert declaration of L into declarations of bignum block
3873 (Last
(Declarations
(Blk
)),
3874 Make_Object_Declaration
(Loc
,
3875 Defining_Identifier
=> L
,
3876 Object_Definition
=>
3877 New_Occurrence_Of
(RTE
(RE_Bignum
), Loc
),
3878 Expression
=> Lopnd
));
3880 -- Insert assignment to Bnn into expressions of bignum block
3883 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
3884 Make_Assignment_Statement
(Loc
,
3885 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
3889 Make_Function_Call
(Loc
,
3891 New_Occurrence_Of
(RTE
(RE_Big_GE
), Loc
),
3892 Parameter_Associations
=> New_List
(
3893 New_Occurrence_Of
(L
, Loc
),
3897 Make_Function_Call
(Loc
,
3899 New_Occurrence_Of
(RTE
(RE_Big_LE
), Loc
),
3900 Parameter_Associations
=> New_List
(
3901 New_Occurrence_Of
(L
, Loc
),
3904 -- Now rewrite the node
3907 Make_Expression_With_Actions
(Loc
,
3908 Actions
=> New_List
(
3909 Make_Object_Declaration
(Loc
,
3910 Defining_Identifier
=> Bnn
,
3911 Object_Definition
=>
3912 New_Occurrence_Of
(Result_Type
, Loc
)),
3914 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
3915 Analyze_And_Resolve
(N
, Result_Type
);
3919 -- Here if no bignums around
3922 -- Case where types are all the same
3924 if Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(Low_Bound
(Rop
)))
3926 Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(High_Bound
(Rop
)))
3930 -- If types are not all the same, it means that we have rewritten
3931 -- at least one of them to be of type Long_Long_Integer, and we
3932 -- will convert the other operands to Long_Long_Integer.
3935 Convert_To_And_Rewrite
(LLIB
, Lop
);
3936 Set_Analyzed
(Lop
, False);
3937 Analyze_And_Resolve
(Lop
, LLIB
);
3939 -- For the right operand, avoid unnecessary recursion into
3940 -- this routine, we know that overflow is not possible.
3942 Convert_To_And_Rewrite
(LLIB
, Low_Bound
(Rop
));
3943 Convert_To_And_Rewrite
(LLIB
, High_Bound
(Rop
));
3944 Set_Analyzed
(Rop
, False);
3945 Analyze_And_Resolve
(Rop
, LLIB
, Suppress
=> Overflow_Check
);
3948 -- Now the three operands are of the same signed integer type,
3949 -- so we can use the normal expansion routine for membership,
3950 -- setting the flag to prevent recursion into this procedure.
3952 Set_No_Minimize_Eliminate
(N
);
3956 -- Right operand is a subtype name and the subtype has a predicate. We
3957 -- have to make sure the predicate is checked, and for that we need to
3958 -- use the standard N_In circuitry with appropriate types.
3961 pragma Assert
(Present
(Predicate_Function
(Etype
(Rop
))));
3963 -- If types are "right", just call Expand_N_In preventing recursion
3965 if Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(Rop
)) then
3966 Set_No_Minimize_Eliminate
(N
);
3971 elsif Is_RTE
(Etype
(Lop
), RE_Bignum
) then
3973 -- For X in T, we want to rewrite our node as
3976 -- Bnn : Result_Type;
3979 -- M : Mark_Id := SS_Mark;
3980 -- Lnn : Long_Long_Integer'Base
3986 -- if not Bignum_In_LLI_Range (Nnn) then
3989 -- Lnn := From_Bignum (Nnn);
3991 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3992 -- and then T'Base (Lnn) in T;
4001 -- A bit gruesome, but there doesn't seem to be a simpler way
4004 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
4005 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
4006 Lnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'L', N
);
4007 Nnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'N', N
);
4008 T
: constant Entity_Id
:= Etype
(Rop
);
4009 TB
: constant Entity_Id
:= Base_Type
(T
);
4013 -- Mark the last membership operation to prevent recursion
4017 Left_Opnd
=> Convert_To
(TB
, New_Occurrence_Of
(Lnn
, Loc
)),
4018 Right_Opnd
=> New_Occurrence_Of
(T
, Loc
));
4019 Set_No_Minimize_Eliminate
(Nin
);
4021 -- Now decorate the block
4024 (Last
(Declarations
(Blk
)),
4025 Make_Object_Declaration
(Loc
,
4026 Defining_Identifier
=> Lnn
,
4027 Object_Definition
=> New_Occurrence_Of
(LLIB
, Loc
)));
4030 (Last
(Declarations
(Blk
)),
4031 Make_Object_Declaration
(Loc
,
4032 Defining_Identifier
=> Nnn
,
4033 Object_Definition
=>
4034 New_Occurrence_Of
(RTE
(RE_Bignum
), Loc
)));
4037 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
4039 Make_Assignment_Statement
(Loc
,
4040 Name
=> New_Occurrence_Of
(Nnn
, Loc
),
4041 Expression
=> Relocate_Node
(Lop
)),
4043 Make_Implicit_If_Statement
(N
,
4047 Make_Function_Call
(Loc
,
4050 (RTE
(RE_Bignum_In_LLI_Range
), Loc
),
4051 Parameter_Associations
=> New_List
(
4052 New_Occurrence_Of
(Nnn
, Loc
)))),
4054 Then_Statements
=> New_List
(
4055 Make_Assignment_Statement
(Loc
,
4056 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
4058 New_Occurrence_Of
(Standard_False
, Loc
))),
4060 Else_Statements
=> New_List
(
4061 Make_Assignment_Statement
(Loc
,
4062 Name
=> New_Occurrence_Of
(Lnn
, Loc
),
4064 Make_Function_Call
(Loc
,
4066 New_Occurrence_Of
(RTE
(RE_From_Bignum
), Loc
),
4067 Parameter_Associations
=> New_List
(
4068 New_Occurrence_Of
(Nnn
, Loc
)))),
4070 Make_Assignment_Statement
(Loc
,
4071 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
4076 Left_Opnd
=> New_Occurrence_Of
(Lnn
, Loc
),
4081 Make_Attribute_Reference
(Loc
,
4082 Attribute_Name
=> Name_First
,
4084 New_Occurrence_Of
(TB
, Loc
))),
4088 Make_Attribute_Reference
(Loc
,
4089 Attribute_Name
=> Name_Last
,
4091 New_Occurrence_Of
(TB
, Loc
))))),
4093 Right_Opnd
=> Nin
))))));
4095 -- Now we can do the rewrite
4098 Make_Expression_With_Actions
(Loc
,
4099 Actions
=> New_List
(
4100 Make_Object_Declaration
(Loc
,
4101 Defining_Identifier
=> Bnn
,
4102 Object_Definition
=>
4103 New_Occurrence_Of
(Result_Type
, Loc
)),
4105 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
4106 Analyze_And_Resolve
(N
, Result_Type
);
4110 -- Not bignum case, but types don't match (this means we rewrote the
4111 -- left operand to be Long_Long_Integer).
4114 pragma Assert
(Base_Type
(Etype
(Lop
)) = LLIB
);
4116 -- We rewrite the membership test as (where T is the type with
4117 -- the predicate, i.e. the type of the right operand)
4119 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
4120 -- and then T'Base (Lop) in T
4123 T
: constant Entity_Id
:= Etype
(Rop
);
4124 TB
: constant Entity_Id
:= Base_Type
(T
);
4128 -- The last membership test is marked to prevent recursion
4132 Left_Opnd
=> Convert_To
(TB
, Duplicate_Subexpr
(Lop
)),
4133 Right_Opnd
=> New_Occurrence_Of
(T
, Loc
));
4134 Set_No_Minimize_Eliminate
(Nin
);
4136 -- Now do the rewrite
4147 Make_Attribute_Reference
(Loc
,
4148 Attribute_Name
=> Name_First
,
4150 New_Occurrence_Of
(TB
, Loc
))),
4153 Make_Attribute_Reference
(Loc
,
4154 Attribute_Name
=> Name_Last
,
4156 New_Occurrence_Of
(TB
, Loc
))))),
4157 Right_Opnd
=> Nin
));
4158 Set_Analyzed
(N
, False);
4159 Analyze_And_Resolve
(N
, Restype
);
4163 end Expand_Membership_Minimize_Eliminate_Overflow
;
4165 ------------------------
4166 -- Expand_N_Allocator --
4167 ------------------------
4169 procedure Expand_N_Allocator
(N
: Node_Id
) is
4170 Etyp
: constant Entity_Id
:= Etype
(Expression
(N
));
4171 Loc
: constant Source_Ptr
:= Sloc
(N
);
4172 PtrT
: constant Entity_Id
:= Etype
(N
);
4174 procedure Rewrite_Coextension
(N
: Node_Id
);
4175 -- Static coextensions have the same lifetime as the entity they
4176 -- constrain. Such occurrences can be rewritten as aliased objects
4177 -- and their unrestricted access used instead of the coextension.
4179 function Size_In_Storage_Elements
(E
: Entity_Id
) return Node_Id
;
4180 -- Given a constrained array type E, returns a node representing the
4181 -- code to compute the size in storage elements for the given type.
4182 -- This is done without using the attribute (which malfunctions for
4185 -------------------------
4186 -- Rewrite_Coextension --
4187 -------------------------
4189 procedure Rewrite_Coextension
(N
: Node_Id
) is
4190 Temp_Id
: constant Node_Id
:= Make_Temporary
(Loc
, 'C');
4191 Temp_Decl
: Node_Id
;
4195 -- Cnn : aliased Etyp;
4198 Make_Object_Declaration
(Loc
,
4199 Defining_Identifier
=> Temp_Id
,
4200 Aliased_Present
=> True,
4201 Object_Definition
=> New_Occurrence_Of
(Etyp
, Loc
));
4203 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
4204 Set_Expression
(Temp_Decl
, Expression
(Expression
(N
)));
4207 Insert_Action
(N
, Temp_Decl
);
4209 Make_Attribute_Reference
(Loc
,
4210 Prefix
=> New_Occurrence_Of
(Temp_Id
, Loc
),
4211 Attribute_Name
=> Name_Unrestricted_Access
));
4213 Analyze_And_Resolve
(N
, PtrT
);
4214 end Rewrite_Coextension
;
4216 ------------------------------
4217 -- Size_In_Storage_Elements --
4218 ------------------------------
4220 function Size_In_Storage_Elements
(E
: Entity_Id
) return Node_Id
is
4222 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4223 -- However, the reason for the existence of this function is
4224 -- to construct a test for sizes too large, which means near the
4225 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4226 -- is that we get overflows when sizes are greater than 2**31.
4228 -- So what we end up doing for array types is to use the expression:
4230 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4232 -- which avoids this problem. All this is a bit bogus, but it does
4233 -- mean we catch common cases of trying to allocate arrays that
4234 -- are too large, and which in the absence of a check results in
4235 -- undetected chaos ???
4237 -- Note in particular that this is a pessimistic estimate in the
4238 -- case of packed array types, where an array element might occupy
4239 -- just a fraction of a storage element???
4246 for J
in 1 .. Number_Dimensions
(E
) loop
4248 Make_Attribute_Reference
(Loc
,
4249 Prefix
=> New_Occurrence_Of
(E
, Loc
),
4250 Attribute_Name
=> Name_Length
,
4251 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, J
)));
4258 Make_Op_Multiply
(Loc
,
4265 Make_Op_Multiply
(Loc
,
4268 Make_Attribute_Reference
(Loc
,
4269 Prefix
=> New_Occurrence_Of
(Component_Type
(E
), Loc
),
4270 Attribute_Name
=> Name_Max_Size_In_Storage_Elements
));
4272 end Size_In_Storage_Elements
;
4276 Dtyp
: constant Entity_Id
:= Available_View
(Designated_Type
(PtrT
));
4280 Rel_Typ
: Entity_Id
;
4283 -- Start of processing for Expand_N_Allocator
4286 -- RM E.2.3(22). We enforce that the expected type of an allocator
4287 -- shall not be a remote access-to-class-wide-limited-private type
4289 -- Why is this being done at expansion time, seems clearly wrong ???
4291 Validate_Remote_Access_To_Class_Wide_Type
(N
);
4293 -- Processing for anonymous access-to-controlled types. These access
4294 -- types receive a special finalization master which appears in the
4295 -- declarations of the enclosing semantic unit. This expansion is done
4296 -- now to ensure that any additional types generated by this routine or
4297 -- Expand_Allocator_Expression inherit the proper type attributes.
4299 if (Ekind
(PtrT
) = E_Anonymous_Access_Type
4300 or else (Is_Itype
(PtrT
) and then No
(Finalization_Master
(PtrT
))))
4301 and then Needs_Finalization
(Dtyp
)
4303 -- Detect the allocation of an anonymous controlled object where the
4304 -- type of the context is named. For example:
4306 -- procedure Proc (Ptr : Named_Access_Typ);
4307 -- Proc (new Designated_Typ);
4309 -- Regardless of the anonymous-to-named access type conversion, the
4310 -- lifetime of the object must be associated with the named access
4311 -- type. Use the finalization-related attributes of this type.
4313 if Nkind_In
(Parent
(N
), N_Type_Conversion
,
4314 N_Unchecked_Type_Conversion
)
4315 and then Ekind_In
(Etype
(Parent
(N
)), E_Access_Subtype
,
4317 E_General_Access_Type
)
4319 Rel_Typ
:= Etype
(Parent
(N
));
4324 -- Anonymous access-to-controlled types allocate on the global pool.
4325 -- Do not set this attribute on .NET/JVM since those targets do not
4326 -- support pools. Note that this is a "root type only" attribute.
4328 if No
(Associated_Storage_Pool
(PtrT
)) and then VM_Target
= No_VM
then
4329 if Present
(Rel_Typ
) then
4330 Set_Associated_Storage_Pool
4331 (Root_Type
(PtrT
), Associated_Storage_Pool
(Rel_Typ
));
4333 Set_Associated_Storage_Pool
4334 (Root_Type
(PtrT
), RTE
(RE_Global_Pool_Object
));
4338 -- The finalization master must be inserted and analyzed as part of
4339 -- the current semantic unit. Note that the master is updated when
4340 -- analysis changes current units. Note that this is a "root type
4343 if Present
(Rel_Typ
) then
4344 Set_Finalization_Master
4345 (Root_Type
(PtrT
), Finalization_Master
(Rel_Typ
));
4347 Set_Finalization_Master
4348 (Root_Type
(PtrT
), Current_Anonymous_Master
);
4352 -- Set the storage pool and find the appropriate version of Allocate to
4353 -- call. Do not overwrite the storage pool if it is already set, which
4354 -- can happen for build-in-place function returns (see
4355 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4357 if No
(Storage_Pool
(N
)) then
4358 Pool
:= Associated_Storage_Pool
(Root_Type
(PtrT
));
4360 if Present
(Pool
) then
4361 Set_Storage_Pool
(N
, Pool
);
4363 if Is_RTE
(Pool
, RE_SS_Pool
) then
4364 if VM_Target
= No_VM
then
4365 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
4368 -- In the case of an allocator for a simple storage pool, locate
4369 -- and save a reference to the pool type's Allocate routine.
4371 elsif Present
(Get_Rep_Pragma
4372 (Etype
(Pool
), Name_Simple_Storage_Pool_Type
))
4375 Pool_Type
: constant Entity_Id
:= Base_Type
(Etype
(Pool
));
4376 Alloc_Op
: Entity_Id
;
4378 Alloc_Op
:= Get_Name_Entity_Id
(Name_Allocate
);
4379 while Present
(Alloc_Op
) loop
4380 if Scope
(Alloc_Op
) = Scope
(Pool_Type
)
4381 and then Present
(First_Formal
(Alloc_Op
))
4382 and then Etype
(First_Formal
(Alloc_Op
)) = Pool_Type
4384 Set_Procedure_To_Call
(N
, Alloc_Op
);
4387 Alloc_Op
:= Homonym
(Alloc_Op
);
4392 elsif Is_Class_Wide_Type
(Etype
(Pool
)) then
4393 Set_Procedure_To_Call
(N
, RTE
(RE_Allocate_Any
));
4396 Set_Procedure_To_Call
(N
,
4397 Find_Prim_Op
(Etype
(Pool
), Name_Allocate
));
4402 -- Under certain circumstances we can replace an allocator by an access
4403 -- to statically allocated storage. The conditions, as noted in AARM
4404 -- 3.10 (10c) are as follows:
4406 -- Size and initial value is known at compile time
4407 -- Access type is access-to-constant
4409 -- The allocator is not part of a constraint on a record component,
4410 -- because in that case the inserted actions are delayed until the
4411 -- record declaration is fully analyzed, which is too late for the
4412 -- analysis of the rewritten allocator.
4414 if Is_Access_Constant
(PtrT
)
4415 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
4416 and then Compile_Time_Known_Value
(Expression
(Expression
(N
)))
4417 and then Size_Known_At_Compile_Time
4418 (Etype
(Expression
(Expression
(N
))))
4419 and then not Is_Record_Type
(Current_Scope
)
4421 -- Here we can do the optimization. For the allocator
4425 -- We insert an object declaration
4427 -- Tnn : aliased x := y;
4429 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4430 -- marked as requiring static allocation.
4432 Temp
:= Make_Temporary
(Loc
, 'T', Expression
(Expression
(N
)));
4433 Desig
:= Subtype_Mark
(Expression
(N
));
4435 -- If context is constrained, use constrained subtype directly,
4436 -- so that the constant is not labelled as having a nominally
4437 -- unconstrained subtype.
4439 if Entity
(Desig
) = Base_Type
(Dtyp
) then
4440 Desig
:= New_Occurrence_Of
(Dtyp
, Loc
);
4444 Make_Object_Declaration
(Loc
,
4445 Defining_Identifier
=> Temp
,
4446 Aliased_Present
=> True,
4447 Constant_Present
=> Is_Access_Constant
(PtrT
),
4448 Object_Definition
=> Desig
,
4449 Expression
=> Expression
(Expression
(N
))));
4452 Make_Attribute_Reference
(Loc
,
4453 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
4454 Attribute_Name
=> Name_Unrestricted_Access
));
4456 Analyze_And_Resolve
(N
, PtrT
);
4458 -- We set the variable as statically allocated, since we don't want
4459 -- it going on the stack of the current procedure.
4461 Set_Is_Statically_Allocated
(Temp
);
4465 -- Same if the allocator is an access discriminant for a local object:
4466 -- instead of an allocator we create a local value and constrain the
4467 -- enclosing object with the corresponding access attribute.
4469 if Is_Static_Coextension
(N
) then
4470 Rewrite_Coextension
(N
);
4474 -- Check for size too large, we do this because the back end misses
4475 -- proper checks here and can generate rubbish allocation calls when
4476 -- we are near the limit. We only do this for the 32-bit address case
4477 -- since that is from a practical point of view where we see a problem.
4479 if System_Address_Size
= 32
4480 and then not Storage_Checks_Suppressed
(PtrT
)
4481 and then not Storage_Checks_Suppressed
(Dtyp
)
4482 and then not Storage_Checks_Suppressed
(Etyp
)
4484 -- The check we want to generate should look like
4486 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4487 -- raise Storage_Error;
4490 -- where 3.5 gigabytes is a constant large enough to accommodate any
4491 -- reasonable request for. But we can't do it this way because at
4492 -- least at the moment we don't compute this attribute right, and
4493 -- can silently give wrong results when the result gets large. Since
4494 -- this is all about large results, that's bad, so instead we only
4495 -- apply the check for constrained arrays, and manually compute the
4496 -- value of the attribute ???
4498 if Is_Array_Type
(Etyp
) and then Is_Constrained
(Etyp
) then
4500 Make_Raise_Storage_Error
(Loc
,
4503 Left_Opnd
=> Size_In_Storage_Elements
(Etyp
),
4505 Make_Integer_Literal
(Loc
, Uint_7
* (Uint_2
** 29))),
4506 Reason
=> SE_Object_Too_Large
));
4510 -- If no storage pool has been specified and we have the restriction
4511 -- No_Standard_Allocators_After_Elaboration is present, then generate
4512 -- a call to Elaboration_Allocators.Check_Standard_Allocator.
4514 if Nkind
(N
) = N_Allocator
4515 and then No
(Storage_Pool
(N
))
4516 and then Restriction_Active
(No_Standard_Allocators_After_Elaboration
)
4519 Make_Procedure_Call_Statement
(Loc
,
4521 New_Occurrence_Of
(RTE
(RE_Check_Standard_Allocator
), Loc
)));
4524 -- Handle case of qualified expression (other than optimization above)
4525 -- First apply constraint checks, because the bounds or discriminants
4526 -- in the aggregate might not match the subtype mark in the allocator.
4528 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
4529 Apply_Constraint_Check
4530 (Expression
(Expression
(N
)), Etype
(Expression
(N
)));
4532 Expand_Allocator_Expression
(N
);
4536 -- If the allocator is for a type which requires initialization, and
4537 -- there is no initial value (i.e. operand is a subtype indication
4538 -- rather than a qualified expression), then we must generate a call to
4539 -- the initialization routine using an expressions action node:
4541 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4543 -- Here ptr_T is the pointer type for the allocator, and T is the
4544 -- subtype of the allocator. A special case arises if the designated
4545 -- type of the access type is a task or contains tasks. In this case
4546 -- the call to Init (Temp.all ...) is replaced by code that ensures
4547 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4548 -- for details). In addition, if the type T is a task type, then the
4549 -- first argument to Init must be converted to the task record type.
4552 T
: constant Entity_Id
:= Entity
(Expression
(N
));
4558 Init_Arg1
: Node_Id
;
4559 Temp_Decl
: Node_Id
;
4560 Temp_Type
: Entity_Id
;
4563 if No_Initialization
(N
) then
4565 -- Even though this might be a simple allocation, create a custom
4566 -- Allocate if the context requires it. Since .NET/JVM compilers
4567 -- do not support pools, this step is skipped.
4569 if VM_Target
= No_VM
4570 and then Present
(Finalization_Master
(PtrT
))
4572 Build_Allocate_Deallocate_Proc
4574 Is_Allocate
=> True);
4577 -- Case of no initialization procedure present
4579 elsif not Has_Non_Null_Base_Init_Proc
(T
) then
4581 -- Case of simple initialization required
4583 if Needs_Simple_Initialization
(T
) then
4584 Check_Restriction
(No_Default_Initialization
, N
);
4585 Rewrite
(Expression
(N
),
4586 Make_Qualified_Expression
(Loc
,
4587 Subtype_Mark
=> New_Occurrence_Of
(T
, Loc
),
4588 Expression
=> Get_Simple_Init_Val
(T
, N
)));
4590 Analyze_And_Resolve
(Expression
(Expression
(N
)), T
);
4591 Analyze_And_Resolve
(Expression
(N
), T
);
4592 Set_Paren_Count
(Expression
(Expression
(N
)), 1);
4593 Expand_N_Allocator
(N
);
4595 -- No initialization required
4601 -- Case of initialization procedure present, must be called
4604 Check_Restriction
(No_Default_Initialization
, N
);
4606 if not Restriction_Active
(No_Default_Initialization
) then
4607 Init
:= Base_Init_Proc
(T
);
4609 Temp
:= Make_Temporary
(Loc
, 'P');
4611 -- Construct argument list for the initialization routine call
4614 Make_Explicit_Dereference
(Loc
,
4616 New_Occurrence_Of
(Temp
, Loc
));
4618 Set_Assignment_OK
(Init_Arg1
);
4621 -- The initialization procedure expects a specific type. if the
4622 -- context is access to class wide, indicate that the object
4623 -- being allocated has the right specific type.
4625 if Is_Class_Wide_Type
(Dtyp
) then
4626 Init_Arg1
:= Unchecked_Convert_To
(T
, Init_Arg1
);
4629 -- If designated type is a concurrent type or if it is private
4630 -- type whose definition is a concurrent type, the first
4631 -- argument in the Init routine has to be unchecked conversion
4632 -- to the corresponding record type. If the designated type is
4633 -- a derived type, also convert the argument to its root type.
4635 if Is_Concurrent_Type
(T
) then
4637 Unchecked_Convert_To
(
4638 Corresponding_Record_Type
(T
), Init_Arg1
);
4640 elsif Is_Private_Type
(T
)
4641 and then Present
(Full_View
(T
))
4642 and then Is_Concurrent_Type
(Full_View
(T
))
4645 Unchecked_Convert_To
4646 (Corresponding_Record_Type
(Full_View
(T
)), Init_Arg1
);
4648 elsif Etype
(First_Formal
(Init
)) /= Base_Type
(T
) then
4650 Ftyp
: constant Entity_Id
:= Etype
(First_Formal
(Init
));
4653 Init_Arg1
:= OK_Convert_To
(Etype
(Ftyp
), Init_Arg1
);
4654 Set_Etype
(Init_Arg1
, Ftyp
);
4658 Args
:= New_List
(Init_Arg1
);
4660 -- For the task case, pass the Master_Id of the access type as
4661 -- the value of the _Master parameter, and _Chain as the value
4662 -- of the _Chain parameter (_Chain will be defined as part of
4663 -- the generated code for the allocator).
4665 -- In Ada 2005, the context may be a function that returns an
4666 -- anonymous access type. In that case the Master_Id has been
4667 -- created when expanding the function declaration.
4669 if Has_Task
(T
) then
4670 if No
(Master_Id
(Base_Type
(PtrT
))) then
4672 -- The designated type was an incomplete type, and the
4673 -- access type did not get expanded. Salvage it now.
4675 if not Restriction_Active
(No_Task_Hierarchy
) then
4676 if Present
(Parent
(Base_Type
(PtrT
))) then
4677 Expand_N_Full_Type_Declaration
4678 (Parent
(Base_Type
(PtrT
)));
4680 -- The only other possibility is an itype. For this
4681 -- case, the master must exist in the context. This is
4682 -- the case when the allocator initializes an access
4683 -- component in an init-proc.
4686 pragma Assert
(Is_Itype
(PtrT
));
4687 Build_Master_Renaming
(PtrT
, N
);
4692 -- If the context of the allocator is a declaration or an
4693 -- assignment, we can generate a meaningful image for it,
4694 -- even though subsequent assignments might remove the
4695 -- connection between task and entity. We build this image
4696 -- when the left-hand side is a simple variable, a simple
4697 -- indexed assignment or a simple selected component.
4699 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
4701 Nam
: constant Node_Id
:= Name
(Parent
(N
));
4704 if Is_Entity_Name
(Nam
) then
4706 Build_Task_Image_Decls
4709 (Entity
(Nam
), Sloc
(Nam
)), T
);
4711 elsif Nkind_In
(Nam
, N_Indexed_Component
,
4712 N_Selected_Component
)
4713 and then Is_Entity_Name
(Prefix
(Nam
))
4716 Build_Task_Image_Decls
4717 (Loc
, Nam
, Etype
(Prefix
(Nam
)));
4719 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
4723 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
4725 Build_Task_Image_Decls
4726 (Loc
, Defining_Identifier
(Parent
(N
)), T
);
4729 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
4732 if Restriction_Active
(No_Task_Hierarchy
) then
4734 New_Occurrence_Of
(RTE
(RE_Library_Task_Level
), Loc
));
4738 (Master_Id
(Base_Type
(Root_Type
(PtrT
))), Loc
));
4741 Append_To
(Args
, Make_Identifier
(Loc
, Name_uChain
));
4743 Decl
:= Last
(Decls
);
4745 New_Occurrence_Of
(Defining_Identifier
(Decl
), Loc
));
4747 -- Has_Task is false, Decls not used
4753 -- Add discriminants if discriminated type
4756 Dis
: Boolean := False;
4760 if Has_Discriminants
(T
) then
4764 elsif Is_Private_Type
(T
)
4765 and then Present
(Full_View
(T
))
4766 and then Has_Discriminants
(Full_View
(T
))
4769 Typ
:= Full_View
(T
);
4774 -- If the allocated object will be constrained by the
4775 -- default values for discriminants, then build a subtype
4776 -- with those defaults, and change the allocated subtype
4777 -- to that. Note that this happens in fewer cases in Ada
4780 if not Is_Constrained
(Typ
)
4781 and then Present
(Discriminant_Default_Value
4782 (First_Discriminant
(Typ
)))
4783 and then (Ada_Version
< Ada_2005
4785 Object_Type_Has_Constrained_Partial_View
4786 (Typ
, Current_Scope
))
4788 Typ
:= Build_Default_Subtype
(Typ
, N
);
4789 Set_Expression
(N
, New_Occurrence_Of
(Typ
, Loc
));
4792 Discr
:= First_Elmt
(Discriminant_Constraint
(Typ
));
4793 while Present
(Discr
) loop
4794 Nod
:= Node
(Discr
);
4795 Append
(New_Copy_Tree
(Node
(Discr
)), Args
);
4797 -- AI-416: when the discriminant constraint is an
4798 -- anonymous access type make sure an accessibility
4799 -- check is inserted if necessary (3.10.2(22.q/2))
4801 if Ada_Version
>= Ada_2005
4803 Ekind
(Etype
(Nod
)) = E_Anonymous_Access_Type
4805 Apply_Accessibility_Check
4806 (Nod
, Typ
, Insert_Node
=> Nod
);
4814 -- We set the allocator as analyzed so that when we analyze
4815 -- the if expression node, we do not get an unwanted recursive
4816 -- expansion of the allocator expression.
4818 Set_Analyzed
(N
, True);
4819 Nod
:= Relocate_Node
(N
);
4821 -- Here is the transformation:
4822 -- input: new Ctrl_Typ
4823 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4824 -- Ctrl_TypIP (Temp.all, ...);
4825 -- [Deep_]Initialize (Temp.all);
4827 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4828 -- is the subtype of the allocator.
4831 Make_Object_Declaration
(Loc
,
4832 Defining_Identifier
=> Temp
,
4833 Constant_Present
=> True,
4834 Object_Definition
=> New_Occurrence_Of
(Temp_Type
, Loc
),
4837 Set_Assignment_OK
(Temp_Decl
);
4838 Insert_Action
(N
, Temp_Decl
, Suppress
=> All_Checks
);
4840 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
4842 -- If the designated type is a task type or contains tasks,
4843 -- create block to activate created tasks, and insert
4844 -- declaration for Task_Image variable ahead of call.
4846 if Has_Task
(T
) then
4848 L
: constant List_Id
:= New_List
;
4851 Build_Task_Allocate_Block
(L
, Nod
, Args
);
4853 Insert_List_Before
(First
(Declarations
(Blk
)), Decls
);
4854 Insert_Actions
(N
, L
);
4859 Make_Procedure_Call_Statement
(Loc
,
4860 Name
=> New_Occurrence_Of
(Init
, Loc
),
4861 Parameter_Associations
=> Args
));
4864 if Needs_Finalization
(T
) then
4867 -- [Deep_]Initialize (Init_Arg1);
4871 (Obj_Ref
=> New_Copy_Tree
(Init_Arg1
),
4874 -- Special processing for .NET/JVM, the allocated object is
4875 -- attached to the finalization master. Generate:
4877 -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1));
4879 -- Types derived from [Limited_]Controlled are the only ones
4880 -- considered since they have fields Prev and Next.
4882 if VM_Target
/= No_VM
4883 and then Is_Controlled
(T
)
4884 and then Present
(Finalization_Master
(PtrT
))
4888 (Obj_Ref
=> New_Copy_Tree
(Init_Arg1
),
4893 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
4894 Analyze_And_Resolve
(N
, PtrT
);
4899 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
4900 -- object that has been rewritten as a reference, we displace "this"
4901 -- to reference properly its secondary dispatch table.
4903 if Nkind
(N
) = N_Identifier
and then Is_Interface
(Dtyp
) then
4904 Displace_Allocator_Pointer
(N
);
4908 when RE_Not_Available
=>
4910 end Expand_N_Allocator
;
4912 -----------------------
4913 -- Expand_N_And_Then --
4914 -----------------------
4916 procedure Expand_N_And_Then
(N
: Node_Id
)
4917 renames Expand_Short_Circuit_Operator
;
4919 ------------------------------
4920 -- Expand_N_Case_Expression --
4921 ------------------------------
4923 procedure Expand_N_Case_Expression
(N
: Node_Id
) is
4924 Loc
: constant Source_Ptr
:= Sloc
(N
);
4925 Typ
: constant Entity_Id
:= Etype
(N
);
4936 -- Check for MINIMIZED/ELIMINATED overflow mode
4938 if Minimized_Eliminated_Overflow_Check
(N
) then
4939 Apply_Arithmetic_Overflow_Check
(N
);
4943 -- If the case expression is a predicate specification, do not
4944 -- expand, because it will be converted to the proper predicate
4945 -- form when building the predicate function.
4947 if Ekind_In
(Current_Scope
, E_Function
, E_Procedure
)
4948 and then Is_Predicate_Function
(Current_Scope
)
4955 -- case X is when A => AX, when B => BX ...
4970 -- However, this expansion is wrong for limited types, and also
4971 -- wrong for unconstrained types (since the bounds may not be the
4972 -- same in all branches). Furthermore it involves an extra copy
4973 -- for large objects. So we take care of this by using the following
4974 -- modified expansion for non-elementary types:
4977 -- type Pnn is access all typ;
4981 -- T := AX'Unrestricted_Access;
4983 -- T := BX'Unrestricted_Access;
4989 Make_Case_Statement
(Loc
,
4990 Expression
=> Expression
(N
),
4991 Alternatives
=> New_List
);
4993 -- Preserve the original context for which the case statement is being
4994 -- generated. This is needed by the finalization machinery to prevent
4995 -- the premature finalization of controlled objects found within the
4998 Set_From_Conditional_Expression
(Cstmt
);
5000 Actions
:= New_List
;
5004 if Is_Elementary_Type
(Typ
) then
5008 Pnn
:= Make_Temporary
(Loc
, 'P');
5010 Make_Full_Type_Declaration
(Loc
,
5011 Defining_Identifier
=> Pnn
,
5013 Make_Access_To_Object_Definition
(Loc
,
5014 All_Present
=> True,
5015 Subtype_Indication
=> New_Occurrence_Of
(Typ
, Loc
))));
5019 Tnn
:= Make_Temporary
(Loc
, 'T');
5021 -- Create declaration for target of expression, and indicate that it
5022 -- does not require initialization.
5025 Make_Object_Declaration
(Loc
,
5026 Defining_Identifier
=> Tnn
,
5027 Object_Definition
=> New_Occurrence_Of
(Ttyp
, Loc
));
5028 Set_No_Initialization
(Decl
);
5029 Append_To
(Actions
, Decl
);
5031 -- Now process the alternatives
5033 Alt
:= First
(Alternatives
(N
));
5034 while Present
(Alt
) loop
5036 Aexp
: Node_Id
:= Expression
(Alt
);
5037 Aloc
: constant Source_Ptr
:= Sloc
(Aexp
);
5041 -- As described above, take Unrestricted_Access for case of non-
5042 -- scalar types, to avoid big copies, and special cases.
5044 if not Is_Elementary_Type
(Typ
) then
5046 Make_Attribute_Reference
(Aloc
,
5047 Prefix
=> Relocate_Node
(Aexp
),
5048 Attribute_Name
=> Name_Unrestricted_Access
);
5052 Make_Assignment_Statement
(Aloc
,
5053 Name
=> New_Occurrence_Of
(Tnn
, Loc
),
5054 Expression
=> Aexp
));
5056 -- Propagate declarations inserted in the node by Insert_Actions
5057 -- (for example, temporaries generated to remove side effects).
5058 -- These actions must remain attached to the alternative, given
5059 -- that they are generated by the corresponding expression.
5061 if Present
(Sinfo
.Actions
(Alt
)) then
5062 Prepend_List
(Sinfo
.Actions
(Alt
), Stats
);
5066 (Alternatives
(Cstmt
),
5067 Make_Case_Statement_Alternative
(Sloc
(Alt
),
5068 Discrete_Choices
=> Discrete_Choices
(Alt
),
5069 Statements
=> Stats
));
5075 Append_To
(Actions
, Cstmt
);
5077 -- Construct and return final expression with actions
5079 if Is_Elementary_Type
(Typ
) then
5080 Fexp
:= New_Occurrence_Of
(Tnn
, Loc
);
5083 Make_Explicit_Dereference
(Loc
,
5084 Prefix
=> New_Occurrence_Of
(Tnn
, Loc
));
5088 Make_Expression_With_Actions
(Loc
,
5090 Actions
=> Actions
));
5092 Analyze_And_Resolve
(N
, Typ
);
5093 end Expand_N_Case_Expression
;
5095 -----------------------------------
5096 -- Expand_N_Explicit_Dereference --
5097 -----------------------------------
5099 procedure Expand_N_Explicit_Dereference
(N
: Node_Id
) is
5101 -- Insert explicit dereference call for the checked storage pool case
5103 Insert_Dereference_Action
(Prefix
(N
));
5105 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5106 -- we set the atomic sync flag.
5108 if Is_Atomic
(Etype
(N
))
5109 and then not Atomic_Synchronization_Disabled
(Etype
(N
))
5111 Activate_Atomic_Synchronization
(N
);
5113 end Expand_N_Explicit_Dereference
;
5115 --------------------------------------
5116 -- Expand_N_Expression_With_Actions --
5117 --------------------------------------
5119 procedure Expand_N_Expression_With_Actions
(N
: Node_Id
) is
5120 function Process_Action
(Act
: Node_Id
) return Traverse_Result
;
5121 -- Inspect and process a single action of an expression_with_actions for
5122 -- transient controlled objects. If such objects are found, the routine
5123 -- generates code to clean them up when the context of the expression is
5124 -- evaluated or elaborated.
5126 --------------------
5127 -- Process_Action --
5128 --------------------
5130 function Process_Action
(Act
: Node_Id
) return Traverse_Result
is
5132 if Nkind
(Act
) = N_Object_Declaration
5133 and then Is_Finalizable_Transient
(Act
, N
)
5135 Process_Transient_Object
(Act
, N
);
5138 -- Avoid processing temporary function results multiple times when
5139 -- dealing with nested expression_with_actions.
5141 elsif Nkind
(Act
) = N_Expression_With_Actions
then
5144 -- Do not process temporary function results in loops. This is done
5145 -- by Expand_N_Loop_Statement and Build_Finalizer.
5147 elsif Nkind
(Act
) = N_Loop_Statement
then
5154 procedure Process_Single_Action
is new Traverse_Proc
(Process_Action
);
5158 Acts
: constant List_Id
:= Actions
(N
);
5159 Expr
: constant Node_Id
:= Expression
(N
);
5162 -- Start of processing for Expand_N_Expression_With_Actions
5165 -- Do not evaluate the expression when it denotes an entity because the
5166 -- expression_with_actions node will be replaced by the reference.
5168 if Is_Entity_Name
(Expr
) then
5171 -- Do not evaluate the expression when there are no actions because the
5172 -- expression_with_actions node will be replaced by the expression.
5174 elsif No
(Acts
) or else Is_Empty_List
(Acts
) then
5177 -- Force the evaluation of the expression by capturing its value in a
5178 -- temporary. This ensures that aliases of transient controlled objects
5179 -- do not leak to the expression of the expression_with_actions node:
5182 -- Trans_Id : Ctrl_Typ : ...;
5183 -- Alias : ... := Trans_Id;
5184 -- in ... Alias ... end;
5186 -- In the example above, Trans_Id cannot be finalized at the end of the
5187 -- actions list because this may affect the alias and the final value of
5188 -- the expression_with_actions. Forcing the evaluation encapsulates the
5189 -- reference to the Alias within the actions list:
5192 -- Trans_Id : Ctrl_Typ : ...;
5193 -- Alias : ... := Trans_Id;
5194 -- Val : constant Boolean := ... Alias ...;
5195 -- <finalize Trans_Id>
5198 -- It is now safe to finalize the transient controlled object at the end
5199 -- of the actions list.
5202 Force_Evaluation
(Expr
);
5205 -- Process all transient controlled objects found within the actions of
5208 Act
:= First
(Acts
);
5209 while Present
(Act
) loop
5210 Process_Single_Action
(Act
);
5214 -- Deal with case where there are no actions. In this case we simply
5215 -- rewrite the node with its expression since we don't need the actions
5216 -- and the specification of this node does not allow a null action list.
5218 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5219 -- the expanded tree and relying on being able to retrieve the original
5220 -- tree in cases like this. This raises a whole lot of issues of whether
5221 -- we have problems elsewhere, which will be addressed in the future???
5223 if Is_Empty_List
(Acts
) then
5224 Rewrite
(N
, Relocate_Node
(Expression
(N
)));
5226 end Expand_N_Expression_With_Actions
;
5228 ----------------------------
5229 -- Expand_N_If_Expression --
5230 ----------------------------
5232 -- Deal with limited types and condition actions
5234 procedure Expand_N_If_Expression
(N
: Node_Id
) is
5235 procedure Process_Actions
(Actions
: List_Id
);
5236 -- Inspect and process a single action list of an if expression for
5237 -- transient controlled objects. If such objects are found, the routine
5238 -- generates code to clean them up when the context of the expression is
5239 -- evaluated or elaborated.
5241 ---------------------
5242 -- Process_Actions --
5243 ---------------------
5245 procedure Process_Actions
(Actions
: List_Id
) is
5249 Act
:= First
(Actions
);
5250 while Present
(Act
) loop
5251 if Nkind
(Act
) = N_Object_Declaration
5252 and then Is_Finalizable_Transient
(Act
, N
)
5254 Process_Transient_Object
(Act
, N
);
5259 end Process_Actions
;
5263 Loc
: constant Source_Ptr
:= Sloc
(N
);
5264 Cond
: constant Node_Id
:= First
(Expressions
(N
));
5265 Thenx
: constant Node_Id
:= Next
(Cond
);
5266 Elsex
: constant Node_Id
:= Next
(Thenx
);
5267 Typ
: constant Entity_Id
:= Etype
(N
);
5275 Ptr_Typ
: Entity_Id
;
5277 -- Start of processing for Expand_N_If_Expression
5280 -- Check for MINIMIZED/ELIMINATED overflow mode
5282 if Minimized_Eliminated_Overflow_Check
(N
) then
5283 Apply_Arithmetic_Overflow_Check
(N
);
5287 -- Fold at compile time if condition known. We have already folded
5288 -- static if expressions, but it is possible to fold any case in which
5289 -- the condition is known at compile time, even though the result is
5292 -- Note that we don't do the fold of such cases in Sem_Elab because
5293 -- it can cause infinite loops with the expander adding a conditional
5294 -- expression, and Sem_Elab circuitry removing it repeatedly.
5296 if Compile_Time_Known_Value
(Cond
) then
5297 if Is_True
(Expr_Value
(Cond
)) then
5299 Actions
:= Then_Actions
(N
);
5302 Actions
:= Else_Actions
(N
);
5307 if Present
(Actions
) then
5309 Make_Expression_With_Actions
(Loc
,
5310 Expression
=> Relocate_Node
(Expr
),
5311 Actions
=> Actions
));
5312 Analyze_And_Resolve
(N
, Typ
);
5314 Rewrite
(N
, Relocate_Node
(Expr
));
5317 -- Note that the result is never static (legitimate cases of static
5318 -- if expressions were folded in Sem_Eval).
5320 Set_Is_Static_Expression
(N
, False);
5324 -- If the type is limited, and the back end does not handle limited
5325 -- types, then we expand as follows to avoid the possibility of
5326 -- improper copying.
5328 -- type Ptr is access all Typ;
5332 -- Cnn := then-expr'Unrestricted_Access;
5335 -- Cnn := else-expr'Unrestricted_Access;
5338 -- and replace the if expression by a reference to Cnn.all.
5340 -- This special case can be skipped if the back end handles limited
5341 -- types properly and ensures that no incorrect copies are made.
5343 if Is_By_Reference_Type
(Typ
)
5344 and then not Back_End_Handles_Limited_Types
5346 -- When the "then" or "else" expressions involve controlled function
5347 -- calls, generated temporaries are chained on the corresponding list
5348 -- of actions. These temporaries need to be finalized after the if
5349 -- expression is evaluated.
5351 Process_Actions
(Then_Actions
(N
));
5352 Process_Actions
(Else_Actions
(N
));
5355 -- type Ann is access all Typ;
5357 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
5360 Make_Full_Type_Declaration
(Loc
,
5361 Defining_Identifier
=> Ptr_Typ
,
5363 Make_Access_To_Object_Definition
(Loc
,
5364 All_Present
=> True,
5365 Subtype_Indication
=> New_Occurrence_Of
(Typ
, Loc
))));
5370 Cnn
:= Make_Temporary
(Loc
, 'C', N
);
5373 Make_Object_Declaration
(Loc
,
5374 Defining_Identifier
=> Cnn
,
5375 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
));
5379 -- Cnn := <Thenx>'Unrestricted_Access;
5381 -- Cnn := <Elsex>'Unrestricted_Access;
5385 Make_Implicit_If_Statement
(N
,
5386 Condition
=> Relocate_Node
(Cond
),
5387 Then_Statements
=> New_List
(
5388 Make_Assignment_Statement
(Sloc
(Thenx
),
5389 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Thenx
)),
5391 Make_Attribute_Reference
(Loc
,
5392 Prefix
=> Relocate_Node
(Thenx
),
5393 Attribute_Name
=> Name_Unrestricted_Access
))),
5395 Else_Statements
=> New_List
(
5396 Make_Assignment_Statement
(Sloc
(Elsex
),
5397 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Elsex
)),
5399 Make_Attribute_Reference
(Loc
,
5400 Prefix
=> Relocate_Node
(Elsex
),
5401 Attribute_Name
=> Name_Unrestricted_Access
))));
5403 -- Preserve the original context for which the if statement is being
5404 -- generated. This is needed by the finalization machinery to prevent
5405 -- the premature finalization of controlled objects found within the
5408 Set_From_Conditional_Expression
(New_If
);
5411 Make_Explicit_Dereference
(Loc
,
5412 Prefix
=> New_Occurrence_Of
(Cnn
, Loc
));
5414 -- If the result is an unconstrained array and the if expression is in a
5415 -- context other than the initializing expression of the declaration of
5416 -- an object, then we pull out the if expression as follows:
5418 -- Cnn : constant typ := if-expression
5420 -- and then replace the if expression with an occurrence of Cnn. This
5421 -- avoids the need in the back end to create on-the-fly variable length
5422 -- temporaries (which it cannot do!)
5424 -- Note that the test for being in an object declaration avoids doing an
5425 -- unnecessary expansion, and also avoids infinite recursion.
5427 elsif Is_Array_Type
(Typ
) and then not Is_Constrained
(Typ
)
5428 and then (Nkind
(Parent
(N
)) /= N_Object_Declaration
5429 or else Expression
(Parent
(N
)) /= N
)
5432 Cnn
: constant Node_Id
:= Make_Temporary
(Loc
, 'C', N
);
5435 Make_Object_Declaration
(Loc
,
5436 Defining_Identifier
=> Cnn
,
5437 Constant_Present
=> True,
5438 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
5439 Expression
=> Relocate_Node
(N
),
5440 Has_Init_Expression
=> True));
5442 Rewrite
(N
, New_Occurrence_Of
(Cnn
, Loc
));
5446 -- For other types, we only need to expand if there are other actions
5447 -- associated with either branch.
5449 elsif Present
(Then_Actions
(N
)) or else Present
(Else_Actions
(N
)) then
5451 -- We now wrap the actions into the appropriate expression
5453 if Present
(Then_Actions
(N
)) then
5455 Make_Expression_With_Actions
(Sloc
(Thenx
),
5456 Actions
=> Then_Actions
(N
),
5457 Expression
=> Relocate_Node
(Thenx
)));
5459 Set_Then_Actions
(N
, No_List
);
5460 Analyze_And_Resolve
(Thenx
, Typ
);
5463 if Present
(Else_Actions
(N
)) then
5465 Make_Expression_With_Actions
(Sloc
(Elsex
),
5466 Actions
=> Else_Actions
(N
),
5467 Expression
=> Relocate_Node
(Elsex
)));
5469 Set_Else_Actions
(N
, No_List
);
5470 Analyze_And_Resolve
(Elsex
, Typ
);
5475 -- If no actions then no expansion needed, gigi will handle it using the
5476 -- same approach as a C conditional expression.
5482 -- Fall through here for either the limited expansion, or the case of
5483 -- inserting actions for non-limited types. In both these cases, we must
5484 -- move the SLOC of the parent If statement to the newly created one and
5485 -- change it to the SLOC of the expression which, after expansion, will
5486 -- correspond to what is being evaluated.
5488 if Present
(Parent
(N
)) and then Nkind
(Parent
(N
)) = N_If_Statement
then
5489 Set_Sloc
(New_If
, Sloc
(Parent
(N
)));
5490 Set_Sloc
(Parent
(N
), Loc
);
5493 -- Make sure Then_Actions and Else_Actions are appropriately moved
5494 -- to the new if statement.
5496 if Present
(Then_Actions
(N
)) then
5498 (First
(Then_Statements
(New_If
)), Then_Actions
(N
));
5501 if Present
(Else_Actions
(N
)) then
5503 (First
(Else_Statements
(New_If
)), Else_Actions
(N
));
5506 Insert_Action
(N
, Decl
);
5507 Insert_Action
(N
, New_If
);
5509 Analyze_And_Resolve
(N
, Typ
);
5510 end Expand_N_If_Expression
;
5516 procedure Expand_N_In
(N
: Node_Id
) is
5517 Loc
: constant Source_Ptr
:= Sloc
(N
);
5518 Restyp
: constant Entity_Id
:= Etype
(N
);
5519 Lop
: constant Node_Id
:= Left_Opnd
(N
);
5520 Rop
: constant Node_Id
:= Right_Opnd
(N
);
5521 Static
: constant Boolean := Is_OK_Static_Expression
(N
);
5526 procedure Substitute_Valid_Check
;
5527 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5528 -- test for the left operand being in range of its subtype.
5530 ----------------------------
5531 -- Substitute_Valid_Check --
5532 ----------------------------
5534 procedure Substitute_Valid_Check
is
5537 Make_Attribute_Reference
(Loc
,
5538 Prefix
=> Relocate_Node
(Lop
),
5539 Attribute_Name
=> Name_Valid
));
5541 Analyze_And_Resolve
(N
, Restyp
);
5543 -- Give warning unless overflow checking is MINIMIZED or ELIMINATED,
5544 -- in which case, this usage makes sense, and in any case, we have
5545 -- actually eliminated the danger of optimization above.
5547 if Overflow_Check_Mode
not in Minimized_Or_Eliminated
then
5549 ("??explicit membership test may be optimized away", N
);
5550 Error_Msg_N
-- CODEFIX
5551 ("\??use ''Valid attribute instead", N
);
5555 end Substitute_Valid_Check
;
5557 -- Start of processing for Expand_N_In
5560 -- If set membership case, expand with separate procedure
5562 if Present
(Alternatives
(N
)) then
5563 Expand_Set_Membership
(N
);
5567 -- Not set membership, proceed with expansion
5569 Ltyp
:= Etype
(Left_Opnd
(N
));
5570 Rtyp
:= Etype
(Right_Opnd
(N
));
5572 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
5573 -- type, then expand with a separate procedure. Note the use of the
5574 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5576 if Overflow_Check_Mode
in Minimized_Or_Eliminated
5577 and then Is_Signed_Integer_Type
(Ltyp
)
5578 and then not No_Minimize_Eliminate
(N
)
5580 Expand_Membership_Minimize_Eliminate_Overflow
(N
);
5584 -- Check case of explicit test for an expression in range of its
5585 -- subtype. This is suspicious usage and we replace it with a 'Valid
5586 -- test and give a warning for scalar types.
5588 if Is_Scalar_Type
(Ltyp
)
5590 -- Only relevant for source comparisons
5592 and then Comes_From_Source
(N
)
5594 -- In floating-point this is a standard way to check for finite values
5595 -- and using 'Valid would typically be a pessimization.
5597 and then not Is_Floating_Point_Type
(Ltyp
)
5599 -- Don't give the message unless right operand is a type entity and
5600 -- the type of the left operand matches this type. Note that this
5601 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
5602 -- checks have changed the type of the left operand.
5604 and then Nkind
(Rop
) in N_Has_Entity
5605 and then Ltyp
= Entity
(Rop
)
5607 -- Skip in VM mode, where we have no sense of invalid values. The
5608 -- warning still seems relevant, but not important enough to worry.
5610 and then VM_Target
= No_VM
5612 -- Skip this for predicated types, where such expressions are a
5613 -- reasonable way of testing if something meets the predicate.
5615 and then not Present
(Predicate_Function
(Ltyp
))
5617 Substitute_Valid_Check
;
5621 -- Do validity check on operands
5623 if Validity_Checks_On
and Validity_Check_Operands
then
5624 Ensure_Valid
(Left_Opnd
(N
));
5625 Validity_Check_Range
(Right_Opnd
(N
));
5628 -- Case of explicit range
5630 if Nkind
(Rop
) = N_Range
then
5632 Lo
: constant Node_Id
:= Low_Bound
(Rop
);
5633 Hi
: constant Node_Id
:= High_Bound
(Rop
);
5635 Lo_Orig
: constant Node_Id
:= Original_Node
(Lo
);
5636 Hi_Orig
: constant Node_Id
:= Original_Node
(Hi
);
5638 Lcheck
: Compare_Result
;
5639 Ucheck
: Compare_Result
;
5641 Warn1
: constant Boolean :=
5642 Constant_Condition_Warnings
5643 and then Comes_From_Source
(N
)
5644 and then not In_Instance
;
5645 -- This must be true for any of the optimization warnings, we
5646 -- clearly want to give them only for source with the flag on. We
5647 -- also skip these warnings in an instance since it may be the
5648 -- case that different instantiations have different ranges.
5650 Warn2
: constant Boolean :=
5652 and then Nkind
(Original_Node
(Rop
)) = N_Range
5653 and then Is_Integer_Type
(Etype
(Lo
));
5654 -- For the case where only one bound warning is elided, we also
5655 -- insist on an explicit range and an integer type. The reason is
5656 -- that the use of enumeration ranges including an end point is
5657 -- common, as is the use of a subtype name, one of whose bounds is
5658 -- the same as the type of the expression.
5661 -- If test is explicit x'First .. x'Last, replace by valid check
5663 -- Could use some individual comments for this complex test ???
5665 if Is_Scalar_Type
(Ltyp
)
5667 -- And left operand is X'First where X matches left operand
5668 -- type (this eliminates cases of type mismatch, including
5669 -- the cases where ELIMINATED/MINIMIZED mode has changed the
5670 -- type of the left operand.
5672 and then Nkind
(Lo_Orig
) = N_Attribute_Reference
5673 and then Attribute_Name
(Lo_Orig
) = Name_First
5674 and then Nkind
(Prefix
(Lo_Orig
)) in N_Has_Entity
5675 and then Entity
(Prefix
(Lo_Orig
)) = Ltyp
5677 -- Same tests for right operand
5679 and then Nkind
(Hi_Orig
) = N_Attribute_Reference
5680 and then Attribute_Name
(Hi_Orig
) = Name_Last
5681 and then Nkind
(Prefix
(Hi_Orig
)) in N_Has_Entity
5682 and then Entity
(Prefix
(Hi_Orig
)) = Ltyp
5684 -- Relevant only for source cases
5686 and then Comes_From_Source
(N
)
5688 -- Omit for VM cases, where we don't have invalid values
5690 and then VM_Target
= No_VM
5692 Substitute_Valid_Check
;
5696 -- If bounds of type are known at compile time, and the end points
5697 -- are known at compile time and identical, this is another case
5698 -- for substituting a valid test. We only do this for discrete
5699 -- types, since it won't arise in practice for float types.
5701 if Comes_From_Source
(N
)
5702 and then Is_Discrete_Type
(Ltyp
)
5703 and then Compile_Time_Known_Value
(Type_High_Bound
(Ltyp
))
5704 and then Compile_Time_Known_Value
(Type_Low_Bound
(Ltyp
))
5705 and then Compile_Time_Known_Value
(Lo
)
5706 and then Compile_Time_Known_Value
(Hi
)
5707 and then Expr_Value
(Type_High_Bound
(Ltyp
)) = Expr_Value
(Hi
)
5708 and then Expr_Value
(Type_Low_Bound
(Ltyp
)) = Expr_Value
(Lo
)
5710 -- Kill warnings in instances, since they may be cases where we
5711 -- have a test in the generic that makes sense with some types
5712 -- and not with other types.
5714 and then not In_Instance
5716 Substitute_Valid_Check
;
5720 -- If we have an explicit range, do a bit of optimization based on
5721 -- range analysis (we may be able to kill one or both checks).
5723 Lcheck
:= Compile_Time_Compare
(Lop
, Lo
, Assume_Valid
=> False);
5724 Ucheck
:= Compile_Time_Compare
(Lop
, Hi
, Assume_Valid
=> False);
5726 -- If either check is known to fail, replace result by False since
5727 -- the other check does not matter. Preserve the static flag for
5728 -- legality checks, because we are constant-folding beyond RM 4.9.
5730 if Lcheck
= LT
or else Ucheck
= GT
then
5732 Error_Msg_N
("?c?range test optimized away", N
);
5733 Error_Msg_N
("\?c?value is known to be out of range", N
);
5736 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
5737 Analyze_And_Resolve
(N
, Restyp
);
5738 Set_Is_Static_Expression
(N
, Static
);
5741 -- If both checks are known to succeed, replace result by True,
5742 -- since we know we are in range.
5744 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
5746 Error_Msg_N
("?c?range test optimized away", N
);
5747 Error_Msg_N
("\?c?value is known to be in range", N
);
5750 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
5751 Analyze_And_Resolve
(N
, Restyp
);
5752 Set_Is_Static_Expression
(N
, Static
);
5755 -- If lower bound check succeeds and upper bound check is not
5756 -- known to succeed or fail, then replace the range check with
5757 -- a comparison against the upper bound.
5759 elsif Lcheck
in Compare_GE
then
5760 if Warn2
and then not In_Instance
then
5761 Error_Msg_N
("??lower bound test optimized away", Lo
);
5762 Error_Msg_N
("\??value is known to be in range", Lo
);
5768 Right_Opnd
=> High_Bound
(Rop
)));
5769 Analyze_And_Resolve
(N
, Restyp
);
5772 -- If upper bound check succeeds and lower bound check is not
5773 -- known to succeed or fail, then replace the range check with
5774 -- a comparison against the lower bound.
5776 elsif Ucheck
in Compare_LE
then
5777 if Warn2
and then not In_Instance
then
5778 Error_Msg_N
("??upper bound test optimized away", Hi
);
5779 Error_Msg_N
("\??value is known to be in range", Hi
);
5785 Right_Opnd
=> Low_Bound
(Rop
)));
5786 Analyze_And_Resolve
(N
, Restyp
);
5790 -- We couldn't optimize away the range check, but there is one
5791 -- more issue. If we are checking constant conditionals, then we
5792 -- see if we can determine the outcome assuming everything is
5793 -- valid, and if so give an appropriate warning.
5795 if Warn1
and then not Assume_No_Invalid_Values
then
5796 Lcheck
:= Compile_Time_Compare
(Lop
, Lo
, Assume_Valid
=> True);
5797 Ucheck
:= Compile_Time_Compare
(Lop
, Hi
, Assume_Valid
=> True);
5799 -- Result is out of range for valid value
5801 if Lcheck
= LT
or else Ucheck
= GT
then
5803 ("?c?value can only be in range if it is invalid", N
);
5805 -- Result is in range for valid value
5807 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
5809 ("?c?value can only be out of range if it is invalid", N
);
5811 -- Lower bound check succeeds if value is valid
5813 elsif Warn2
and then Lcheck
in Compare_GE
then
5815 ("?c?lower bound check only fails if it is invalid", Lo
);
5817 -- Upper bound check succeeds if value is valid
5819 elsif Warn2
and then Ucheck
in Compare_LE
then
5821 ("?c?upper bound check only fails for invalid values", Hi
);
5826 -- For all other cases of an explicit range, nothing to be done
5830 -- Here right operand is a subtype mark
5834 Typ
: Entity_Id
:= Etype
(Rop
);
5835 Is_Acc
: constant Boolean := Is_Access_Type
(Typ
);
5836 Cond
: Node_Id
:= Empty
;
5838 Obj
: Node_Id
:= Lop
;
5839 SCIL_Node
: Node_Id
;
5842 Remove_Side_Effects
(Obj
);
5844 -- For tagged type, do tagged membership operation
5846 if Is_Tagged_Type
(Typ
) then
5848 -- No expansion will be performed when VM_Target, as the VM
5849 -- back-ends will handle the membership tests directly (tags
5850 -- are not explicitly represented in Java objects, so the
5851 -- normal tagged membership expansion is not what we want).
5853 if Tagged_Type_Expansion
then
5854 Tagged_Membership
(N
, SCIL_Node
, New_N
);
5856 Analyze_And_Resolve
(N
, Restyp
);
5858 -- Update decoration of relocated node referenced by the
5861 if Generate_SCIL
and then Present
(SCIL_Node
) then
5862 Set_SCIL_Node
(N
, SCIL_Node
);
5868 -- If type is scalar type, rewrite as x in t'First .. t'Last.
5869 -- This reason we do this is that the bounds may have the wrong
5870 -- type if they come from the original type definition. Also this
5871 -- way we get all the processing above for an explicit range.
5873 -- Don't do this for predicated types, since in this case we
5874 -- want to check the predicate.
5876 elsif Is_Scalar_Type
(Typ
) then
5877 if No
(Predicate_Function
(Typ
)) then
5881 Make_Attribute_Reference
(Loc
,
5882 Attribute_Name
=> Name_First
,
5883 Prefix
=> New_Occurrence_Of
(Typ
, Loc
)),
5886 Make_Attribute_Reference
(Loc
,
5887 Attribute_Name
=> Name_Last
,
5888 Prefix
=> New_Occurrence_Of
(Typ
, Loc
))));
5889 Analyze_And_Resolve
(N
, Restyp
);
5894 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5895 -- a membership test if the subtype mark denotes a constrained
5896 -- Unchecked_Union subtype and the expression lacks inferable
5899 elsif Is_Unchecked_Union
(Base_Type
(Typ
))
5900 and then Is_Constrained
(Typ
)
5901 and then not Has_Inferable_Discriminants
(Lop
)
5904 Make_Raise_Program_Error
(Loc
,
5905 Reason
=> PE_Unchecked_Union_Restriction
));
5907 -- Prevent Gigi from generating incorrect code by rewriting the
5908 -- test as False. What is this undocumented thing about ???
5910 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
5914 -- Here we have a non-scalar type
5917 Typ
:= Designated_Type
(Typ
);
5920 if not Is_Constrained
(Typ
) then
5921 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
5922 Analyze_And_Resolve
(N
, Restyp
);
5924 -- For the constrained array case, we have to check the subscripts
5925 -- for an exact match if the lengths are non-zero (the lengths
5926 -- must match in any case).
5928 elsif Is_Array_Type
(Typ
) then
5929 Check_Subscripts
: declare
5930 function Build_Attribute_Reference
5933 Dim
: Nat
) return Node_Id
;
5934 -- Build attribute reference E'Nam (Dim)
5936 -------------------------------
5937 -- Build_Attribute_Reference --
5938 -------------------------------
5940 function Build_Attribute_Reference
5943 Dim
: Nat
) return Node_Id
5947 Make_Attribute_Reference
(Loc
,
5949 Attribute_Name
=> Nam
,
5950 Expressions
=> New_List
(
5951 Make_Integer_Literal
(Loc
, Dim
)));
5952 end Build_Attribute_Reference
;
5954 -- Start of processing for Check_Subscripts
5957 for J
in 1 .. Number_Dimensions
(Typ
) loop
5958 Evolve_And_Then
(Cond
,
5961 Build_Attribute_Reference
5962 (Duplicate_Subexpr_No_Checks
(Obj
),
5965 Build_Attribute_Reference
5966 (New_Occurrence_Of
(Typ
, Loc
), Name_First
, J
)));
5968 Evolve_And_Then
(Cond
,
5971 Build_Attribute_Reference
5972 (Duplicate_Subexpr_No_Checks
(Obj
),
5975 Build_Attribute_Reference
5976 (New_Occurrence_Of
(Typ
, Loc
), Name_Last
, J
)));
5985 Right_Opnd
=> Make_Null
(Loc
)),
5986 Right_Opnd
=> Cond
);
5990 Analyze_And_Resolve
(N
, Restyp
);
5991 end Check_Subscripts
;
5993 -- These are the cases where constraint checks may be required,
5994 -- e.g. records with possible discriminants
5997 -- Expand the test into a series of discriminant comparisons.
5998 -- The expression that is built is the negation of the one that
5999 -- is used for checking discriminant constraints.
6001 Obj
:= Relocate_Node
(Left_Opnd
(N
));
6003 if Has_Discriminants
(Typ
) then
6004 Cond
:= Make_Op_Not
(Loc
,
6005 Right_Opnd
=> Build_Discriminant_Checks
(Obj
, Typ
));
6008 Cond
:= Make_Or_Else
(Loc
,
6012 Right_Opnd
=> Make_Null
(Loc
)),
6013 Right_Opnd
=> Cond
);
6017 Cond
:= New_Occurrence_Of
(Standard_True
, Loc
);
6021 Analyze_And_Resolve
(N
, Restyp
);
6024 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
6025 -- expression of an anonymous access type. This can involve an
6026 -- accessibility test and a tagged type membership test in the
6027 -- case of tagged designated types.
6029 if Ada_Version
>= Ada_2012
6031 and then Ekind
(Ltyp
) = E_Anonymous_Access_Type
6034 Expr_Entity
: Entity_Id
:= Empty
;
6036 Param_Level
: Node_Id
;
6037 Type_Level
: Node_Id
;
6040 if Is_Entity_Name
(Lop
) then
6041 Expr_Entity
:= Param_Entity
(Lop
);
6043 if not Present
(Expr_Entity
) then
6044 Expr_Entity
:= Entity
(Lop
);
6048 -- If a conversion of the anonymous access value to the
6049 -- tested type would be illegal, then the result is False.
6051 if not Valid_Conversion
6052 (Lop
, Rtyp
, Lop
, Report_Errs
=> False)
6054 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
6055 Analyze_And_Resolve
(N
, Restyp
);
6057 -- Apply an accessibility check if the access object has an
6058 -- associated access level and when the level of the type is
6059 -- less deep than the level of the access parameter. This
6060 -- only occur for access parameters and stand-alone objects
6061 -- of an anonymous access type.
6064 if Present
(Expr_Entity
)
6067 (Effective_Extra_Accessibility
(Expr_Entity
))
6068 and then UI_Gt
(Object_Access_Level
(Lop
),
6069 Type_Access_Level
(Rtyp
))
6073 (Effective_Extra_Accessibility
(Expr_Entity
), Loc
);
6076 Make_Integer_Literal
(Loc
, Type_Access_Level
(Rtyp
));
6078 -- Return True only if the accessibility level of the
6079 -- expression entity is not deeper than the level of
6080 -- the tested access type.
6084 Left_Opnd
=> Relocate_Node
(N
),
6085 Right_Opnd
=> Make_Op_Le
(Loc
,
6086 Left_Opnd
=> Param_Level
,
6087 Right_Opnd
=> Type_Level
)));
6089 Analyze_And_Resolve
(N
);
6092 -- If the designated type is tagged, do tagged membership
6095 -- *** NOTE: we have to check not null before doing the
6096 -- tagged membership test (but maybe that can be done
6097 -- inside Tagged_Membership?).
6099 if Is_Tagged_Type
(Typ
) then
6102 Left_Opnd
=> Relocate_Node
(N
),
6106 Right_Opnd
=> Make_Null
(Loc
))));
6108 -- No expansion will be performed when VM_Target, as
6109 -- the VM back-ends will handle the membership tests
6110 -- directly (tags are not explicitly represented in
6111 -- Java objects, so the normal tagged membership
6112 -- expansion is not what we want).
6114 if Tagged_Type_Expansion
then
6116 -- Note that we have to pass Original_Node, because
6117 -- the membership test might already have been
6118 -- rewritten by earlier parts of membership test.
6121 (Original_Node
(N
), SCIL_Node
, New_N
);
6123 -- Update decoration of relocated node referenced
6124 -- by the SCIL node.
6126 if Generate_SCIL
and then Present
(SCIL_Node
) then
6127 Set_SCIL_Node
(New_N
, SCIL_Node
);
6132 Left_Opnd
=> Relocate_Node
(N
),
6133 Right_Opnd
=> New_N
));
6135 Analyze_And_Resolve
(N
, Restyp
);
6144 -- At this point, we have done the processing required for the basic
6145 -- membership test, but not yet dealt with the predicate.
6149 -- If a predicate is present, then we do the predicate test, but we
6150 -- most certainly want to omit this if we are within the predicate
6151 -- function itself, since otherwise we have an infinite recursion.
6152 -- The check should also not be emitted when testing against a range
6153 -- (the check is only done when the right operand is a subtype; see
6154 -- RM12-4.5.2 (28.1/3-30/3)).
6157 PFunc
: constant Entity_Id
:= Predicate_Function
(Rtyp
);
6161 and then Current_Scope
/= PFunc
6162 and then Nkind
(Rop
) /= N_Range
6166 Left_Opnd
=> Relocate_Node
(N
),
6167 Right_Opnd
=> Make_Predicate_Call
(Rtyp
, Lop
, Mem
=> True)));
6169 -- Analyze new expression, mark left operand as analyzed to
6170 -- avoid infinite recursion adding predicate calls. Similarly,
6171 -- suppress further range checks on the call.
6173 Set_Analyzed
(Left_Opnd
(N
));
6174 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
6176 -- All done, skip attempt at compile time determination of result
6183 --------------------------------
6184 -- Expand_N_Indexed_Component --
6185 --------------------------------
6187 procedure Expand_N_Indexed_Component
(N
: Node_Id
) is
6188 Loc
: constant Source_Ptr
:= Sloc
(N
);
6189 Typ
: constant Entity_Id
:= Etype
(N
);
6190 P
: constant Node_Id
:= Prefix
(N
);
6191 T
: constant Entity_Id
:= Etype
(P
);
6195 -- A special optimization, if we have an indexed component that is
6196 -- selecting from a slice, then we can eliminate the slice, since, for
6197 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6198 -- the range check required by the slice. The range check for the slice
6199 -- itself has already been generated. The range check for the
6200 -- subscripting operation is ensured by converting the subject to
6201 -- the subtype of the slice.
6203 -- This optimization not only generates better code, avoiding slice
6204 -- messing especially in the packed case, but more importantly bypasses
6205 -- some problems in handling this peculiar case, for example, the issue
6206 -- of dealing specially with object renamings.
6208 if Nkind
(P
) = N_Slice
6210 -- This optimization is disabled for CodePeer because it can transform
6211 -- an index-check constraint_error into a range-check constraint_error
6212 -- and CodePeer cares about that distinction.
6214 and then not CodePeer_Mode
6217 Make_Indexed_Component
(Loc
,
6218 Prefix
=> Prefix
(P
),
6219 Expressions
=> New_List
(
6221 (Etype
(First_Index
(Etype
(P
))),
6222 First
(Expressions
(N
))))));
6223 Analyze_And_Resolve
(N
, Typ
);
6227 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6228 -- function, then additional actuals must be passed.
6230 if Ada_Version
>= Ada_2005
6231 and then Is_Build_In_Place_Function_Call
(P
)
6233 Make_Build_In_Place_Call_In_Anonymous_Context
(P
);
6236 -- If the prefix is an access type, then we unconditionally rewrite if
6237 -- as an explicit dereference. This simplifies processing for several
6238 -- cases, including packed array cases and certain cases in which checks
6239 -- must be generated. We used to try to do this only when it was
6240 -- necessary, but it cleans up the code to do it all the time.
6242 if Is_Access_Type
(T
) then
6243 Insert_Explicit_Dereference
(P
);
6244 Analyze_And_Resolve
(P
, Designated_Type
(T
));
6245 Atp
:= Designated_Type
(T
);
6250 -- Generate index and validity checks
6252 Generate_Index_Checks
(N
);
6254 if Validity_Checks_On
and then Validity_Check_Subscripts
then
6255 Apply_Subscript_Validity_Checks
(N
);
6258 -- If selecting from an array with atomic components, and atomic sync
6259 -- is not suppressed for this array type, set atomic sync flag.
6261 if (Has_Atomic_Components
(Atp
)
6262 and then not Atomic_Synchronization_Disabled
(Atp
))
6263 or else (Is_Atomic
(Typ
)
6264 and then not Atomic_Synchronization_Disabled
(Typ
))
6266 Activate_Atomic_Synchronization
(N
);
6269 -- All done for the non-packed case
6271 if not Is_Packed
(Etype
(Prefix
(N
))) then
6275 -- For packed arrays that are not bit-packed (i.e. the case of an array
6276 -- with one or more index types with a non-contiguous enumeration type),
6277 -- we can always use the normal packed element get circuit.
6279 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
6280 Expand_Packed_Element_Reference
(N
);
6284 -- For a reference to a component of a bit packed array, we convert it
6285 -- to a reference to the corresponding Packed_Array_Impl_Type. We only
6286 -- want to do this for simple references, and not for:
6288 -- Left side of assignment, or prefix of left side of assignment, or
6289 -- prefix of the prefix, to handle packed arrays of packed arrays,
6290 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6292 -- Renaming objects in renaming associations
6293 -- This case is handled when a use of the renamed variable occurs
6295 -- Actual parameters for a procedure call
6296 -- This case is handled in Exp_Ch6.Expand_Actuals
6298 -- The second expression in a 'Read attribute reference
6300 -- The prefix of an address or bit or size attribute reference
6302 -- The following circuit detects these exceptions
6305 Child
: Node_Id
:= N
;
6306 Parnt
: Node_Id
:= Parent
(N
);
6310 if Nkind
(Parnt
) = N_Unchecked_Expression
then
6313 elsif Nkind_In
(Parnt
, N_Object_Renaming_Declaration
,
6314 N_Procedure_Call_Statement
)
6315 or else (Nkind
(Parnt
) = N_Parameter_Association
6317 Nkind
(Parent
(Parnt
)) = N_Procedure_Call_Statement
)
6321 elsif Nkind
(Parnt
) = N_Attribute_Reference
6322 and then Nam_In
(Attribute_Name
(Parnt
), Name_Address
,
6325 and then Prefix
(Parnt
) = Child
6329 elsif Nkind
(Parnt
) = N_Assignment_Statement
6330 and then Name
(Parnt
) = Child
6334 -- If the expression is an index of an indexed component, it must
6335 -- be expanded regardless of context.
6337 elsif Nkind
(Parnt
) = N_Indexed_Component
6338 and then Child
/= Prefix
(Parnt
)
6340 Expand_Packed_Element_Reference
(N
);
6343 elsif Nkind
(Parent
(Parnt
)) = N_Assignment_Statement
6344 and then Name
(Parent
(Parnt
)) = Parnt
6348 elsif Nkind
(Parnt
) = N_Attribute_Reference
6349 and then Attribute_Name
(Parnt
) = Name_Read
6350 and then Next
(First
(Expressions
(Parnt
))) = Child
6354 elsif Nkind_In
(Parnt
, N_Indexed_Component
, N_Selected_Component
)
6355 and then Prefix
(Parnt
) = Child
6360 Expand_Packed_Element_Reference
(N
);
6364 -- Keep looking up tree for unchecked expression, or if we are the
6365 -- prefix of a possible assignment left side.
6368 Parnt
:= Parent
(Child
);
6371 end Expand_N_Indexed_Component
;
6373 ---------------------
6374 -- Expand_N_Not_In --
6375 ---------------------
6377 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6378 -- can be done. This avoids needing to duplicate this expansion code.
6380 procedure Expand_N_Not_In
(N
: Node_Id
) is
6381 Loc
: constant Source_Ptr
:= Sloc
(N
);
6382 Typ
: constant Entity_Id
:= Etype
(N
);
6383 Cfs
: constant Boolean := Comes_From_Source
(N
);
6390 Left_Opnd
=> Left_Opnd
(N
),
6391 Right_Opnd
=> Right_Opnd
(N
))));
6393 -- If this is a set membership, preserve list of alternatives
6395 Set_Alternatives
(Right_Opnd
(N
), Alternatives
(Original_Node
(N
)));
6397 -- We want this to appear as coming from source if original does (see
6398 -- transformations in Expand_N_In).
6400 Set_Comes_From_Source
(N
, Cfs
);
6401 Set_Comes_From_Source
(Right_Opnd
(N
), Cfs
);
6403 -- Now analyze transformed node
6405 Analyze_And_Resolve
(N
, Typ
);
6406 end Expand_N_Not_In
;
6412 -- The only replacement required is for the case of a null of a type that
6413 -- is an access to protected subprogram, or a subtype thereof. We represent
6414 -- such access values as a record, and so we must replace the occurrence of
6415 -- null by the equivalent record (with a null address and a null pointer in
6416 -- it), so that the backend creates the proper value.
6418 procedure Expand_N_Null
(N
: Node_Id
) is
6419 Loc
: constant Source_Ptr
:= Sloc
(N
);
6420 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
6424 if Is_Access_Protected_Subprogram_Type
(Typ
) then
6426 Make_Aggregate
(Loc
,
6427 Expressions
=> New_List
(
6428 New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
),
6432 Analyze_And_Resolve
(N
, Equivalent_Type
(Typ
));
6434 -- For subsequent semantic analysis, the node must retain its type.
6435 -- Gigi in any case replaces this type by the corresponding record
6436 -- type before processing the node.
6442 when RE_Not_Available
=>
6446 ---------------------
6447 -- Expand_N_Op_Abs --
6448 ---------------------
6450 procedure Expand_N_Op_Abs
(N
: Node_Id
) is
6451 Loc
: constant Source_Ptr
:= Sloc
(N
);
6452 Expr
: constant Node_Id
:= Right_Opnd
(N
);
6455 Unary_Op_Validity_Checks
(N
);
6457 -- Check for MINIMIZED/ELIMINATED overflow mode
6459 if Minimized_Eliminated_Overflow_Check
(N
) then
6460 Apply_Arithmetic_Overflow_Check
(N
);
6464 -- Deal with software overflow checking
6466 if not Backend_Overflow_Checks_On_Target
6467 and then Is_Signed_Integer_Type
(Etype
(N
))
6468 and then Do_Overflow_Check
(N
)
6470 -- The only case to worry about is when the argument is equal to the
6471 -- largest negative number, so what we do is to insert the check:
6473 -- [constraint_error when Expr = typ'Base'First]
6475 -- with the usual Duplicate_Subexpr use coding for expr
6478 Make_Raise_Constraint_Error
(Loc
,
6481 Left_Opnd
=> Duplicate_Subexpr
(Expr
),
6483 Make_Attribute_Reference
(Loc
,
6485 New_Occurrence_Of
(Base_Type
(Etype
(Expr
)), Loc
),
6486 Attribute_Name
=> Name_First
)),
6487 Reason
=> CE_Overflow_Check_Failed
));
6489 end Expand_N_Op_Abs
;
6491 ---------------------
6492 -- Expand_N_Op_Add --
6493 ---------------------
6495 procedure Expand_N_Op_Add
(N
: Node_Id
) is
6496 Typ
: constant Entity_Id
:= Etype
(N
);
6499 Binary_Op_Validity_Checks
(N
);
6501 -- Check for MINIMIZED/ELIMINATED overflow mode
6503 if Minimized_Eliminated_Overflow_Check
(N
) then
6504 Apply_Arithmetic_Overflow_Check
(N
);
6508 -- N + 0 = 0 + N = N for integer types
6510 if Is_Integer_Type
(Typ
) then
6511 if Compile_Time_Known_Value
(Right_Opnd
(N
))
6512 and then Expr_Value
(Right_Opnd
(N
)) = Uint_0
6514 Rewrite
(N
, Left_Opnd
(N
));
6517 elsif Compile_Time_Known_Value
(Left_Opnd
(N
))
6518 and then Expr_Value
(Left_Opnd
(N
)) = Uint_0
6520 Rewrite
(N
, Right_Opnd
(N
));
6525 -- Arithmetic overflow checks for signed integer/fixed point types
6527 if Is_Signed_Integer_Type
(Typ
) or else Is_Fixed_Point_Type
(Typ
) then
6528 Apply_Arithmetic_Overflow_Check
(N
);
6532 -- Overflow checks for floating-point if -gnateF mode active
6534 Check_Float_Op_Overflow
(N
);
6535 end Expand_N_Op_Add
;
6537 ---------------------
6538 -- Expand_N_Op_And --
6539 ---------------------
6541 procedure Expand_N_Op_And
(N
: Node_Id
) is
6542 Typ
: constant Entity_Id
:= Etype
(N
);
6545 Binary_Op_Validity_Checks
(N
);
6547 if Is_Array_Type
(Etype
(N
)) then
6548 Expand_Boolean_Operator
(N
);
6550 elsif Is_Boolean_Type
(Etype
(N
)) then
6551 Adjust_Condition
(Left_Opnd
(N
));
6552 Adjust_Condition
(Right_Opnd
(N
));
6553 Set_Etype
(N
, Standard_Boolean
);
6554 Adjust_Result_Type
(N
, Typ
);
6556 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
6557 Expand_Intrinsic_Call
(N
, Entity
(N
));
6560 end Expand_N_Op_And
;
6562 ------------------------
6563 -- Expand_N_Op_Concat --
6564 ------------------------
6566 procedure Expand_N_Op_Concat
(N
: Node_Id
) is
6568 -- List of operands to be concatenated
6571 -- Node which is to be replaced by the result of concatenating the nodes
6572 -- in the list Opnds.
6575 -- Ensure validity of both operands
6577 Binary_Op_Validity_Checks
(N
);
6579 -- If we are the left operand of a concatenation higher up the tree,
6580 -- then do nothing for now, since we want to deal with a series of
6581 -- concatenations as a unit.
6583 if Nkind
(Parent
(N
)) = N_Op_Concat
6584 and then N
= Left_Opnd
(Parent
(N
))
6589 -- We get here with a concatenation whose left operand may be a
6590 -- concatenation itself with a consistent type. We need to process
6591 -- these concatenation operands from left to right, which means
6592 -- from the deepest node in the tree to the highest node.
6595 while Nkind
(Left_Opnd
(Cnode
)) = N_Op_Concat
loop
6596 Cnode
:= Left_Opnd
(Cnode
);
6599 -- Now Cnode is the deepest concatenation, and its parents are the
6600 -- concatenation nodes above, so now we process bottom up, doing the
6603 -- The outer loop runs more than once if more than one concatenation
6604 -- type is involved.
6607 Opnds
:= New_List
(Left_Opnd
(Cnode
), Right_Opnd
(Cnode
));
6608 Set_Parent
(Opnds
, N
);
6610 -- The inner loop gathers concatenation operands
6612 Inner
: while Cnode
/= N
6613 and then Base_Type
(Etype
(Cnode
)) =
6614 Base_Type
(Etype
(Parent
(Cnode
)))
6616 Cnode
:= Parent
(Cnode
);
6617 Append
(Right_Opnd
(Cnode
), Opnds
);
6620 -- Note: The following code is a temporary workaround for N731-034
6621 -- and N829-028 and will be kept until the general issue of internal
6622 -- symbol serialization is addressed. The workaround is kept under a
6623 -- debug switch to avoid permiating into the general case.
6625 -- Wrap the node to concatenate into an expression actions node to
6626 -- keep it nicely packaged. This is useful in the case of an assert
6627 -- pragma with a concatenation where we want to be able to delete
6628 -- the concatenation and all its expansion stuff.
6630 if Debug_Flag_Dot_H
then
6632 Cnod
: constant Node_Id
:= Relocate_Node
(Cnode
);
6633 Typ
: constant Entity_Id
:= Base_Type
(Etype
(Cnode
));
6636 -- Note: use Rewrite rather than Replace here, so that for
6637 -- example Why_Not_Static can find the original concatenation
6641 Make_Expression_With_Actions
(Sloc
(Cnode
),
6642 Actions
=> New_List
(Make_Null_Statement
(Sloc
(Cnode
))),
6643 Expression
=> Cnod
));
6645 Expand_Concatenate
(Cnod
, Opnds
);
6646 Analyze_And_Resolve
(Cnode
, Typ
);
6652 Expand_Concatenate
(Cnode
, Opnds
);
6655 exit Outer
when Cnode
= N
;
6656 Cnode
:= Parent
(Cnode
);
6658 end Expand_N_Op_Concat
;
6660 ------------------------
6661 -- Expand_N_Op_Divide --
6662 ------------------------
6664 procedure Expand_N_Op_Divide
(N
: Node_Id
) is
6665 Loc
: constant Source_Ptr
:= Sloc
(N
);
6666 Lopnd
: constant Node_Id
:= Left_Opnd
(N
);
6667 Ropnd
: constant Node_Id
:= Right_Opnd
(N
);
6668 Ltyp
: constant Entity_Id
:= Etype
(Lopnd
);
6669 Rtyp
: constant Entity_Id
:= Etype
(Ropnd
);
6670 Typ
: Entity_Id
:= Etype
(N
);
6671 Rknow
: constant Boolean := Is_Integer_Type
(Typ
)
6673 Compile_Time_Known_Value
(Ropnd
);
6677 Binary_Op_Validity_Checks
(N
);
6679 -- Check for MINIMIZED/ELIMINATED overflow mode
6681 if Minimized_Eliminated_Overflow_Check
(N
) then
6682 Apply_Arithmetic_Overflow_Check
(N
);
6686 -- Otherwise proceed with expansion of division
6689 Rval
:= Expr_Value
(Ropnd
);
6692 -- N / 1 = N for integer types
6694 if Rknow
and then Rval
= Uint_1
then
6699 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
6700 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6701 -- operand is an unsigned integer, as required for this to work.
6703 if Nkind
(Ropnd
) = N_Op_Expon
6704 and then Is_Power_Of_2_For_Shift
(Ropnd
)
6706 -- We cannot do this transformation in configurable run time mode if we
6707 -- have 64-bit integers and long shifts are not available.
6709 and then (Esize
(Ltyp
) <= 32 or else Support_Long_Shifts_On_Target
)
6712 Make_Op_Shift_Right
(Loc
,
6715 Convert_To
(Standard_Natural
, Right_Opnd
(Ropnd
))));
6716 Analyze_And_Resolve
(N
, Typ
);
6720 -- Do required fixup of universal fixed operation
6722 if Typ
= Universal_Fixed
then
6723 Fixup_Universal_Fixed_Operation
(N
);
6727 -- Divisions with fixed-point results
6729 if Is_Fixed_Point_Type
(Typ
) then
6731 -- Deal with divide-by-zero check if back end cannot handle them
6732 -- and the flag is set indicating that we need such a check. Note
6733 -- that we don't need to bother here with the case of mixed-mode
6734 -- (Right operand an integer type), since these will be rewritten
6735 -- with conversions to a divide with a fixed-point right operand.
6737 if Do_Division_Check
(N
)
6738 and then not Backend_Divide_Checks_On_Target
6739 and then not Is_Integer_Type
(Rtyp
)
6741 Set_Do_Division_Check
(N
, False);
6743 Make_Raise_Constraint_Error
(Loc
,
6746 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ropnd
),
6747 Right_Opnd
=> Make_Real_Literal
(Loc
, Ureal_0
)),
6748 Reason
=> CE_Divide_By_Zero
));
6751 -- No special processing if Treat_Fixed_As_Integer is set, since
6752 -- from a semantic point of view such operations are simply integer
6753 -- operations and will be treated that way.
6755 if not Treat_Fixed_As_Integer
(N
) then
6756 if Is_Integer_Type
(Rtyp
) then
6757 Expand_Divide_Fixed_By_Integer_Giving_Fixed
(N
);
6759 Expand_Divide_Fixed_By_Fixed_Giving_Fixed
(N
);
6763 -- Other cases of division of fixed-point operands. Again we exclude the
6764 -- case where Treat_Fixed_As_Integer is set.
6766 elsif (Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
))
6767 and then not Treat_Fixed_As_Integer
(N
)
6769 if Is_Integer_Type
(Typ
) then
6770 Expand_Divide_Fixed_By_Fixed_Giving_Integer
(N
);
6772 pragma Assert
(Is_Floating_Point_Type
(Typ
));
6773 Expand_Divide_Fixed_By_Fixed_Giving_Float
(N
);
6776 -- Mixed-mode operations can appear in a non-static universal context,
6777 -- in which case the integer argument must be converted explicitly.
6779 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Rtyp
) then
6781 Convert_To
(Universal_Real
, Relocate_Node
(Ropnd
)));
6783 Analyze_And_Resolve
(Ropnd
, Universal_Real
);
6785 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Ltyp
) then
6787 Convert_To
(Universal_Real
, Relocate_Node
(Lopnd
)));
6789 Analyze_And_Resolve
(Lopnd
, Universal_Real
);
6791 -- Non-fixed point cases, do integer zero divide and overflow checks
6793 elsif Is_Integer_Type
(Typ
) then
6794 Apply_Divide_Checks
(N
);
6797 -- Overflow checks for floating-point if -gnateF mode active
6799 Check_Float_Op_Overflow
(N
);
6800 end Expand_N_Op_Divide
;
6802 --------------------
6803 -- Expand_N_Op_Eq --
6804 --------------------
6806 procedure Expand_N_Op_Eq
(N
: Node_Id
) is
6807 Loc
: constant Source_Ptr
:= Sloc
(N
);
6808 Typ
: constant Entity_Id
:= Etype
(N
);
6809 Lhs
: constant Node_Id
:= Left_Opnd
(N
);
6810 Rhs
: constant Node_Id
:= Right_Opnd
(N
);
6811 Bodies
: constant List_Id
:= New_List
;
6812 A_Typ
: constant Entity_Id
:= Etype
(Lhs
);
6814 Typl
: Entity_Id
:= A_Typ
;
6815 Op_Name
: Entity_Id
;
6818 procedure Build_Equality_Call
(Eq
: Entity_Id
);
6819 -- If a constructed equality exists for the type or for its parent,
6820 -- build and analyze call, adding conversions if the operation is
6823 function Has_Unconstrained_UU_Component
(Typ
: Node_Id
) return Boolean;
6824 -- Determines whether a type has a subcomponent of an unconstrained
6825 -- Unchecked_Union subtype. Typ is a record type.
6827 -------------------------
6828 -- Build_Equality_Call --
6829 -------------------------
6831 procedure Build_Equality_Call
(Eq
: Entity_Id
) is
6832 Op_Type
: constant Entity_Id
:= Etype
(First_Formal
(Eq
));
6833 L_Exp
: Node_Id
:= Relocate_Node
(Lhs
);
6834 R_Exp
: Node_Id
:= Relocate_Node
(Rhs
);
6837 -- Adjust operands if necessary to comparison type
6839 if Base_Type
(Op_Type
) /= Base_Type
(A_Typ
)
6840 and then not Is_Class_Wide_Type
(A_Typ
)
6842 L_Exp
:= OK_Convert_To
(Op_Type
, L_Exp
);
6843 R_Exp
:= OK_Convert_To
(Op_Type
, R_Exp
);
6846 -- If we have an Unchecked_Union, we need to add the inferred
6847 -- discriminant values as actuals in the function call. At this
6848 -- point, the expansion has determined that both operands have
6849 -- inferable discriminants.
6851 if Is_Unchecked_Union
(Op_Type
) then
6853 Lhs_Type
: constant Node_Id
:= Etype
(L_Exp
);
6854 Rhs_Type
: constant Node_Id
:= Etype
(R_Exp
);
6856 Lhs_Discr_Vals
: Elist_Id
;
6857 -- List of inferred discriminant values for left operand.
6859 Rhs_Discr_Vals
: Elist_Id
;
6860 -- List of inferred discriminant values for right operand.
6865 Lhs_Discr_Vals
:= New_Elmt_List
;
6866 Rhs_Discr_Vals
:= New_Elmt_List
;
6868 -- Per-object constrained selected components require special
6869 -- attention. If the enclosing scope of the component is an
6870 -- Unchecked_Union, we cannot reference its discriminants
6871 -- directly. This is why we use the extra parameters of the
6872 -- equality function of the enclosing Unchecked_Union.
6874 -- type UU_Type (Discr : Integer := 0) is
6877 -- pragma Unchecked_Union (UU_Type);
6879 -- 1. Unchecked_Union enclosing record:
6881 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
6883 -- Comp : UU_Type (Discr);
6885 -- end Enclosing_UU_Type;
6886 -- pragma Unchecked_Union (Enclosing_UU_Type);
6888 -- Obj1 : Enclosing_UU_Type;
6889 -- Obj2 : Enclosing_UU_Type (1);
6891 -- [. . .] Obj1 = Obj2 [. . .]
6895 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
6897 -- A and B are the formal parameters of the equality function
6898 -- of Enclosing_UU_Type. The function always has two extra
6899 -- formals to capture the inferred discriminant values for
6900 -- each discriminant of the type.
6902 -- 2. Non-Unchecked_Union enclosing record:
6905 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
6908 -- Comp : UU_Type (Discr);
6910 -- end Enclosing_Non_UU_Type;
6912 -- Obj1 : Enclosing_Non_UU_Type;
6913 -- Obj2 : Enclosing_Non_UU_Type (1);
6915 -- ... Obj1 = Obj2 ...
6919 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
6920 -- obj1.discr, obj2.discr)) then
6922 -- In this case we can directly reference the discriminants of
6923 -- the enclosing record.
6925 -- Process left operand of equality
6927 if Nkind
(Lhs
) = N_Selected_Component
6929 Has_Per_Object_Constraint
(Entity
(Selector_Name
(Lhs
)))
6931 -- If enclosing record is an Unchecked_Union, use formals
6932 -- corresponding to each discriminant. The name of the
6933 -- formal is that of the discriminant, with added suffix,
6934 -- see Exp_Ch3.Build_Record_Equality for details.
6936 if Is_Unchecked_Union
(Scope
(Entity
(Selector_Name
(Lhs
))))
6940 (Scope
(Entity
(Selector_Name
(Lhs
))));
6941 while Present
(Discr
) loop
6943 (Make_Identifier
(Loc
,
6944 Chars
=> New_External_Name
(Chars
(Discr
), 'A')),
6945 To
=> Lhs_Discr_Vals
);
6946 Next_Discriminant
(Discr
);
6949 -- If enclosing record is of a non-Unchecked_Union type, it
6950 -- is possible to reference its discriminants directly.
6953 Discr
:= First_Discriminant
(Lhs_Type
);
6954 while Present
(Discr
) loop
6956 (Make_Selected_Component
(Loc
,
6957 Prefix
=> Prefix
(Lhs
),
6960 (Get_Discriminant_Value
(Discr
,
6962 Stored_Constraint
(Lhs_Type
)))),
6963 To
=> Lhs_Discr_Vals
);
6964 Next_Discriminant
(Discr
);
6968 -- Otherwise operand is on object with a constrained type.
6969 -- Infer the discriminant values from the constraint.
6973 Discr
:= First_Discriminant
(Lhs_Type
);
6974 while Present
(Discr
) loop
6977 (Get_Discriminant_Value
(Discr
,
6979 Stored_Constraint
(Lhs_Type
))),
6980 To
=> Lhs_Discr_Vals
);
6981 Next_Discriminant
(Discr
);
6985 -- Similar processing for right operand of equality
6987 if Nkind
(Rhs
) = N_Selected_Component
6989 Has_Per_Object_Constraint
(Entity
(Selector_Name
(Rhs
)))
6991 if Is_Unchecked_Union
6992 (Scope
(Entity
(Selector_Name
(Rhs
))))
6996 (Scope
(Entity
(Selector_Name
(Rhs
))));
6997 while Present
(Discr
) loop
6999 (Make_Identifier
(Loc
,
7000 Chars
=> New_External_Name
(Chars
(Discr
), 'B')),
7001 To
=> Rhs_Discr_Vals
);
7002 Next_Discriminant
(Discr
);
7006 Discr
:= First_Discriminant
(Rhs_Type
);
7007 while Present
(Discr
) loop
7009 (Make_Selected_Component
(Loc
,
7010 Prefix
=> Prefix
(Rhs
),
7012 New_Copy
(Get_Discriminant_Value
7015 Stored_Constraint
(Rhs_Type
)))),
7016 To
=> Rhs_Discr_Vals
);
7017 Next_Discriminant
(Discr
);
7022 Discr
:= First_Discriminant
(Rhs_Type
);
7023 while Present
(Discr
) loop
7025 (New_Copy
(Get_Discriminant_Value
7028 Stored_Constraint
(Rhs_Type
))),
7029 To
=> Rhs_Discr_Vals
);
7030 Next_Discriminant
(Discr
);
7034 -- Now merge the list of discriminant values so that values
7035 -- of corresponding discriminants are adjacent.
7043 Params
:= New_List
(L_Exp
, R_Exp
);
7044 L_Elmt
:= First_Elmt
(Lhs_Discr_Vals
);
7045 R_Elmt
:= First_Elmt
(Rhs_Discr_Vals
);
7046 while Present
(L_Elmt
) loop
7047 Append_To
(Params
, Node
(L_Elmt
));
7048 Append_To
(Params
, Node
(R_Elmt
));
7054 Make_Function_Call
(Loc
,
7055 Name
=> New_Occurrence_Of
(Eq
, Loc
),
7056 Parameter_Associations
=> Params
));
7060 -- Normal case, not an unchecked union
7064 Make_Function_Call
(Loc
,
7065 Name
=> New_Occurrence_Of
(Eq
, Loc
),
7066 Parameter_Associations
=> New_List
(L_Exp
, R_Exp
)));
7069 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7070 end Build_Equality_Call
;
7072 ------------------------------------
7073 -- Has_Unconstrained_UU_Component --
7074 ------------------------------------
7076 function Has_Unconstrained_UU_Component
7077 (Typ
: Node_Id
) return Boolean
7079 Tdef
: constant Node_Id
:=
7080 Type_Definition
(Declaration_Node
(Base_Type
(Typ
)));
7084 function Component_Is_Unconstrained_UU
7085 (Comp
: Node_Id
) return Boolean;
7086 -- Determines whether the subtype of the component is an
7087 -- unconstrained Unchecked_Union.
7089 function Variant_Is_Unconstrained_UU
7090 (Variant
: Node_Id
) return Boolean;
7091 -- Determines whether a component of the variant has an unconstrained
7092 -- Unchecked_Union subtype.
7094 -----------------------------------
7095 -- Component_Is_Unconstrained_UU --
7096 -----------------------------------
7098 function Component_Is_Unconstrained_UU
7099 (Comp
: Node_Id
) return Boolean
7102 if Nkind
(Comp
) /= N_Component_Declaration
then
7107 Sindic
: constant Node_Id
:=
7108 Subtype_Indication
(Component_Definition
(Comp
));
7111 -- Unconstrained nominal type. In the case of a constraint
7112 -- present, the node kind would have been N_Subtype_Indication.
7114 if Nkind
(Sindic
) = N_Identifier
then
7115 return Is_Unchecked_Union
(Base_Type
(Etype
(Sindic
)));
7120 end Component_Is_Unconstrained_UU
;
7122 ---------------------------------
7123 -- Variant_Is_Unconstrained_UU --
7124 ---------------------------------
7126 function Variant_Is_Unconstrained_UU
7127 (Variant
: Node_Id
) return Boolean
7129 Clist
: constant Node_Id
:= Component_List
(Variant
);
7132 if Is_Empty_List
(Component_Items
(Clist
)) then
7136 -- We only need to test one component
7139 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
7142 while Present
(Comp
) loop
7143 if Component_Is_Unconstrained_UU
(Comp
) then
7151 -- None of the components withing the variant were of
7152 -- unconstrained Unchecked_Union type.
7155 end Variant_Is_Unconstrained_UU
;
7157 -- Start of processing for Has_Unconstrained_UU_Component
7160 if Null_Present
(Tdef
) then
7164 Clist
:= Component_List
(Tdef
);
7165 Vpart
:= Variant_Part
(Clist
);
7167 -- Inspect available components
7169 if Present
(Component_Items
(Clist
)) then
7171 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
7174 while Present
(Comp
) loop
7176 -- One component is sufficient
7178 if Component_Is_Unconstrained_UU
(Comp
) then
7187 -- Inspect available components withing variants
7189 if Present
(Vpart
) then
7191 Variant
: Node_Id
:= First
(Variants
(Vpart
));
7194 while Present
(Variant
) loop
7196 -- One component within a variant is sufficient
7198 if Variant_Is_Unconstrained_UU
(Variant
) then
7207 -- Neither the available components, nor the components inside the
7208 -- variant parts were of an unconstrained Unchecked_Union subtype.
7211 end Has_Unconstrained_UU_Component
;
7213 -- Start of processing for Expand_N_Op_Eq
7216 Binary_Op_Validity_Checks
(N
);
7218 -- Deal with private types
7220 if Ekind
(Typl
) = E_Private_Type
then
7221 Typl
:= Underlying_Type
(Typl
);
7222 elsif Ekind
(Typl
) = E_Private_Subtype
then
7223 Typl
:= Underlying_Type
(Base_Type
(Typl
));
7228 -- It may happen in error situations that the underlying type is not
7229 -- set. The error will be detected later, here we just defend the
7236 -- Now get the implementation base type (note that plain Base_Type here
7237 -- might lead us back to the private type, which is not what we want!)
7239 Typl
:= Implementation_Base_Type
(Typl
);
7241 -- Equality between variant records results in a call to a routine
7242 -- that has conditional tests of the discriminant value(s), and hence
7243 -- violates the No_Implicit_Conditionals restriction.
7245 if Has_Variant_Part
(Typl
) then
7250 Check_Restriction
(Msg
, No_Implicit_Conditionals
, N
);
7254 ("\comparison of variant records tests discriminants", N
);
7260 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7261 -- means we no longer have a comparison operation, we are all done.
7263 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
7265 if Nkind
(N
) /= N_Op_Eq
then
7269 -- Boolean types (requiring handling of non-standard case)
7271 if Is_Boolean_Type
(Typl
) then
7272 Adjust_Condition
(Left_Opnd
(N
));
7273 Adjust_Condition
(Right_Opnd
(N
));
7274 Set_Etype
(N
, Standard_Boolean
);
7275 Adjust_Result_Type
(N
, Typ
);
7279 elsif Is_Array_Type
(Typl
) then
7281 -- If we are doing full validity checking, and it is possible for the
7282 -- array elements to be invalid then expand out array comparisons to
7283 -- make sure that we check the array elements.
7285 if Validity_Check_Operands
7286 and then not Is_Known_Valid
(Component_Type
(Typl
))
7289 Save_Force_Validity_Checks
: constant Boolean :=
7290 Force_Validity_Checks
;
7292 Force_Validity_Checks
:= True;
7294 Expand_Array_Equality
7296 Relocate_Node
(Lhs
),
7297 Relocate_Node
(Rhs
),
7300 Insert_Actions
(N
, Bodies
);
7301 Analyze_And_Resolve
(N
, Standard_Boolean
);
7302 Force_Validity_Checks
:= Save_Force_Validity_Checks
;
7305 -- Packed case where both operands are known aligned
7307 elsif Is_Bit_Packed_Array
(Typl
)
7308 and then not Is_Possibly_Unaligned_Object
(Lhs
)
7309 and then not Is_Possibly_Unaligned_Object
(Rhs
)
7311 Expand_Packed_Eq
(N
);
7313 -- Where the component type is elementary we can use a block bit
7314 -- comparison (if supported on the target) exception in the case
7315 -- of floating-point (negative zero issues require element by
7316 -- element comparison), and atomic/VFA types (where we must be sure
7317 -- to load elements independently) and possibly unaligned arrays.
7319 elsif Is_Elementary_Type
(Component_Type
(Typl
))
7320 and then not Is_Floating_Point_Type
(Component_Type
(Typl
))
7321 and then not Is_Atomic_Or_VFA
(Component_Type
(Typl
))
7322 and then not Is_Possibly_Unaligned_Object
(Lhs
)
7323 and then not Is_Possibly_Unaligned_Object
(Rhs
)
7324 and then Support_Composite_Compare_On_Target
7328 -- For composite and floating-point cases, expand equality loop to
7329 -- make sure of using proper comparisons for tagged types, and
7330 -- correctly handling the floating-point case.
7334 Expand_Array_Equality
7336 Relocate_Node
(Lhs
),
7337 Relocate_Node
(Rhs
),
7340 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
7341 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7346 elsif Is_Record_Type
(Typl
) then
7348 -- For tagged types, use the primitive "="
7350 if Is_Tagged_Type
(Typl
) then
7352 -- No need to do anything else compiling under restriction
7353 -- No_Dispatching_Calls. During the semantic analysis we
7354 -- already notified such violation.
7356 if Restriction_Active
(No_Dispatching_Calls
) then
7360 -- If this is derived from an untagged private type completed with
7361 -- a tagged type, it does not have a full view, so we use the
7362 -- primitive operations of the private type. This check should no
7363 -- longer be necessary when these types get their full views???
7365 if Is_Private_Type
(A_Typ
)
7366 and then not Is_Tagged_Type
(A_Typ
)
7367 and then Is_Derived_Type
(A_Typ
)
7368 and then No
(Full_View
(A_Typ
))
7370 -- Search for equality operation, checking that the operands
7371 -- have the same type. Note that we must find a matching entry,
7372 -- or something is very wrong.
7374 Prim
:= First_Elmt
(Collect_Primitive_Operations
(A_Typ
));
7376 while Present
(Prim
) loop
7377 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7378 and then Etype
(First_Formal
(Node
(Prim
))) =
7379 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7381 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7386 pragma Assert
(Present
(Prim
));
7387 Op_Name
:= Node
(Prim
);
7389 -- Find the type's predefined equality or an overriding
7390 -- user-defined equality. The reason for not simply calling
7391 -- Find_Prim_Op here is that there may be a user-defined
7392 -- overloaded equality op that precedes the equality that we
7393 -- want, so we have to explicitly search (e.g., there could be
7394 -- an equality with two different parameter types).
7397 if Is_Class_Wide_Type
(Typl
) then
7398 Typl
:= Find_Specific_Type
(Typl
);
7401 Prim
:= First_Elmt
(Primitive_Operations
(Typl
));
7402 while Present
(Prim
) loop
7403 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7404 and then Etype
(First_Formal
(Node
(Prim
))) =
7405 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7407 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7412 pragma Assert
(Present
(Prim
));
7413 Op_Name
:= Node
(Prim
);
7416 Build_Equality_Call
(Op_Name
);
7418 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7419 -- predefined equality operator for a type which has a subcomponent
7420 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7422 elsif Has_Unconstrained_UU_Component
(Typl
) then
7424 Make_Raise_Program_Error
(Loc
,
7425 Reason
=> PE_Unchecked_Union_Restriction
));
7427 -- Prevent Gigi from generating incorrect code by rewriting the
7428 -- equality as a standard False. (is this documented somewhere???)
7431 New_Occurrence_Of
(Standard_False
, Loc
));
7433 elsif Is_Unchecked_Union
(Typl
) then
7435 -- If we can infer the discriminants of the operands, we make a
7436 -- call to the TSS equality function.
7438 if Has_Inferable_Discriminants
(Lhs
)
7440 Has_Inferable_Discriminants
(Rhs
)
7443 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
7446 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7447 -- the predefined equality operator for an Unchecked_Union type
7448 -- if either of the operands lack inferable discriminants.
7451 Make_Raise_Program_Error
(Loc
,
7452 Reason
=> PE_Unchecked_Union_Restriction
));
7454 -- Emit a warning on source equalities only, otherwise the
7455 -- message may appear out of place due to internal use. The
7456 -- warning is unconditional because it is required by the
7459 if Comes_From_Source
(N
) then
7461 ("Unchecked_Union discriminants cannot be determined??",
7464 ("\Program_Error will be raised for equality operation??",
7468 -- Prevent Gigi from generating incorrect code by rewriting
7469 -- the equality as a standard False (documented where???).
7472 New_Occurrence_Of
(Standard_False
, Loc
));
7475 -- If a type support function is present (for complex cases), use it
7477 elsif Present
(TSS
(Root_Type
(Typl
), TSS_Composite_Equality
)) then
7479 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
7481 -- When comparing two Bounded_Strings, use the primitive equality of
7482 -- the root Super_String type.
7484 elsif Is_Bounded_String
(Typl
) then
7486 First_Elmt
(Collect_Primitive_Operations
(Root_Type
(Typl
)));
7488 while Present
(Prim
) loop
7489 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7490 and then Etype
(First_Formal
(Node
(Prim
))) =
7491 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7492 and then Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7497 -- A Super_String type should always have a primitive equality
7499 pragma Assert
(Present
(Prim
));
7500 Build_Equality_Call
(Node
(Prim
));
7502 -- Otherwise expand the component by component equality. Note that
7503 -- we never use block-bit comparisons for records, because of the
7504 -- problems with gaps. The backend will often be able to recombine
7505 -- the separate comparisons that we generate here.
7508 Remove_Side_Effects
(Lhs
);
7509 Remove_Side_Effects
(Rhs
);
7511 Expand_Record_Equality
(N
, Typl
, Lhs
, Rhs
, Bodies
));
7513 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
7514 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7518 -- Test if result is known at compile time
7520 Rewrite_Comparison
(N
);
7522 -- Special optimization of length comparison
7524 Optimize_Length_Comparison
(N
);
7526 -- One more special case, if we have a comparison of X'Result = expr
7527 -- in floating-point, then if not already there, change expr to be
7528 -- f'Machine (expr) to eliminate suprise from extra precision.
7530 if Is_Floating_Point_Type
(Typl
)
7531 and then Nkind
(Original_Node
(Lhs
)) = N_Attribute_Reference
7532 and then Attribute_Name
(Original_Node
(Lhs
)) = Name_Result
7534 -- Stick in the Typ'Machine call if not already there
7536 if Nkind
(Rhs
) /= N_Attribute_Reference
7537 or else Attribute_Name
(Rhs
) /= Name_Machine
7540 Make_Attribute_Reference
(Loc
,
7541 Prefix
=> New_Occurrence_Of
(Typl
, Loc
),
7542 Attribute_Name
=> Name_Machine
,
7543 Expressions
=> New_List
(Relocate_Node
(Rhs
))));
7544 Analyze_And_Resolve
(Rhs
, Typl
);
7549 -----------------------
7550 -- Expand_N_Op_Expon --
7551 -----------------------
7553 procedure Expand_N_Op_Expon
(N
: Node_Id
) is
7554 Loc
: constant Source_Ptr
:= Sloc
(N
);
7555 Typ
: constant Entity_Id
:= Etype
(N
);
7556 Rtyp
: constant Entity_Id
:= Root_Type
(Typ
);
7557 Base
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
7558 Bastyp
: constant Node_Id
:= Etype
(Base
);
7559 Exp
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
7560 Exptyp
: constant Entity_Id
:= Etype
(Exp
);
7561 Ovflo
: constant Boolean := Do_Overflow_Check
(N
);
7570 Binary_Op_Validity_Checks
(N
);
7572 -- CodePeer wants to see the unexpanded N_Op_Expon node
7574 if CodePeer_Mode
then
7578 -- If either operand is of a private type, then we have the use of an
7579 -- intrinsic operator, and we get rid of the privateness, by using root
7580 -- types of underlying types for the actual operation. Otherwise the
7581 -- private types will cause trouble if we expand multiplications or
7582 -- shifts etc. We also do this transformation if the result type is
7583 -- different from the base type.
7585 if Is_Private_Type
(Etype
(Base
))
7586 or else Is_Private_Type
(Typ
)
7587 or else Is_Private_Type
(Exptyp
)
7588 or else Rtyp
/= Root_Type
(Bastyp
)
7591 Bt
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Bastyp
));
7592 Et
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Exptyp
));
7595 Unchecked_Convert_To
(Typ
,
7597 Left_Opnd
=> Unchecked_Convert_To
(Bt
, Base
),
7598 Right_Opnd
=> Unchecked_Convert_To
(Et
, Exp
))));
7599 Analyze_And_Resolve
(N
, Typ
);
7604 -- Check for MINIMIZED/ELIMINATED overflow mode
7606 if Minimized_Eliminated_Overflow_Check
(N
) then
7607 Apply_Arithmetic_Overflow_Check
(N
);
7611 -- Test for case of known right argument where we can replace the
7612 -- exponentiation by an equivalent expression using multiplication.
7614 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
7615 -- configurable run-time mode, we may not have the exponentiation
7616 -- routine available, and we don't want the legality of the program
7617 -- to depend on how clever the compiler is in knowing values.
7619 if CRT_Safe_Compile_Time_Known_Value
(Exp
) then
7620 Expv
:= Expr_Value
(Exp
);
7622 -- We only fold small non-negative exponents. You might think we
7623 -- could fold small negative exponents for the real case, but we
7624 -- can't because we are required to raise Constraint_Error for
7625 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
7626 -- See ACVC test C4A012B.
7628 if Expv
>= 0 and then Expv
<= 4 then
7630 -- X ** 0 = 1 (or 1.0)
7634 -- Call Remove_Side_Effects to ensure that any side effects
7635 -- in the ignored left operand (in particular function calls
7636 -- to user defined functions) are properly executed.
7638 Remove_Side_Effects
(Base
);
7640 if Ekind
(Typ
) in Integer_Kind
then
7641 Xnode
:= Make_Integer_Literal
(Loc
, Intval
=> 1);
7643 Xnode
:= Make_Real_Literal
(Loc
, Ureal_1
);
7655 Make_Op_Multiply
(Loc
,
7656 Left_Opnd
=> Duplicate_Subexpr
(Base
),
7657 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
));
7659 -- X ** 3 = X * X * X
7663 Make_Op_Multiply
(Loc
,
7665 Make_Op_Multiply
(Loc
,
7666 Left_Opnd
=> Duplicate_Subexpr
(Base
),
7667 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)),
7668 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
));
7673 -- En : constant base'type := base * base;
7678 pragma Assert
(Expv
= 4);
7679 Temp
:= Make_Temporary
(Loc
, 'E', Base
);
7682 Make_Expression_With_Actions
(Loc
,
7683 Actions
=> New_List
(
7684 Make_Object_Declaration
(Loc
,
7685 Defining_Identifier
=> Temp
,
7686 Constant_Present
=> True,
7687 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
7689 Make_Op_Multiply
(Loc
,
7691 Duplicate_Subexpr
(Base
),
7693 Duplicate_Subexpr_No_Checks
(Base
)))),
7696 Make_Op_Multiply
(Loc
,
7697 Left_Opnd
=> New_Occurrence_Of
(Temp
, Loc
),
7698 Right_Opnd
=> New_Occurrence_Of
(Temp
, Loc
)));
7702 Analyze_And_Resolve
(N
, Typ
);
7707 -- Deal with optimizing 2 ** expression to shift where possible
7709 -- Note: we used to check that Exptyp was an unsigned type. But that is
7710 -- an unnecessary check, since if Exp is negative, we have a run-time
7711 -- error that is either caught (so we get the right result) or we have
7712 -- suppressed the check, in which case the code is erroneous anyway.
7714 if Is_Integer_Type
(Rtyp
)
7716 -- The base value must be "safe compile-time known", and exactly 2
7718 and then Nkind
(Base
) = N_Integer_Literal
7719 and then CRT_Safe_Compile_Time_Known_Value
(Base
)
7720 and then Expr_Value
(Base
) = Uint_2
7722 -- We only handle cases where the right type is a integer
7724 and then Is_Integer_Type
(Root_Type
(Exptyp
))
7725 and then Esize
(Root_Type
(Exptyp
)) <= Esize
(Standard_Integer
)
7727 -- This transformation is not applicable for a modular type with a
7728 -- nonbinary modulus because we do not handle modular reduction in
7729 -- a correct manner if we attempt this transformation in this case.
7731 and then not Non_Binary_Modulus
(Typ
)
7733 -- Handle the cases where our parent is a division or multiplication
7734 -- specially. In these cases we can convert to using a shift at the
7735 -- parent level if we are not doing overflow checking, since it is
7736 -- too tricky to combine the overflow check at the parent level.
7739 and then Nkind_In
(Parent
(N
), N_Op_Divide
, N_Op_Multiply
)
7742 P
: constant Node_Id
:= Parent
(N
);
7743 L
: constant Node_Id
:= Left_Opnd
(P
);
7744 R
: constant Node_Id
:= Right_Opnd
(P
);
7747 if (Nkind
(P
) = N_Op_Multiply
7749 ((Is_Integer_Type
(Etype
(L
)) and then R
= N
)
7751 (Is_Integer_Type
(Etype
(R
)) and then L
= N
))
7752 and then not Do_Overflow_Check
(P
))
7755 (Nkind
(P
) = N_Op_Divide
7756 and then Is_Integer_Type
(Etype
(L
))
7757 and then Is_Unsigned_Type
(Etype
(L
))
7759 and then not Do_Overflow_Check
(P
))
7761 Set_Is_Power_Of_2_For_Shift
(N
);
7766 -- Here we just have 2 ** N on its own, so we can convert this to a
7767 -- shift node. We are prepared to deal with overflow here, and we
7768 -- also have to handle proper modular reduction for binary modular.
7777 -- Maximum shift count with no overflow
7780 -- Set True if we must test the shift count
7783 -- Compute maximum shift based on the underlying size. For a
7784 -- modular type this is one less than the size.
7786 if Is_Modular_Integer_Type
(Typ
) then
7788 -- For modular integer types, this is the size of the value
7789 -- being shifted minus one. Any larger values will cause
7790 -- modular reduction to a result of zero. Note that we do
7791 -- want the RM_Size here (e.g. mod 2 ** 7, we want a result
7792 -- of 6, since 2**7 should be reduced to zero).
7794 MaxS
:= RM_Size
(Rtyp
) - 1;
7796 -- For signed integer types, we use the size of the value
7797 -- being shifted minus 2. Larger values cause overflow.
7800 MaxS
:= Esize
(Rtyp
) - 2;
7803 -- Determine range to see if it can be larger than MaxS
7806 (Right_Opnd
(N
), OK
, Lo
, Hi
, Assume_Valid
=> True);
7807 TestS
:= (not OK
) or else Hi
> MaxS
;
7809 -- Signed integer case
7811 if Is_Signed_Integer_Type
(Typ
) then
7813 -- Generate overflow check if overflow is active. Note that
7814 -- we can simply ignore the possibility of overflow if the
7815 -- flag is not set (means that overflow cannot happen or
7816 -- that overflow checks are suppressed).
7818 if Ovflo
and TestS
then
7820 Make_Raise_Constraint_Error
(Loc
,
7823 Left_Opnd
=> Duplicate_Subexpr
(Right_Opnd
(N
)),
7824 Right_Opnd
=> Make_Integer_Literal
(Loc
, MaxS
)),
7825 Reason
=> CE_Overflow_Check_Failed
));
7828 -- Now rewrite node as Shift_Left (1, right-operand)
7831 Make_Op_Shift_Left
(Loc
,
7832 Left_Opnd
=> Make_Integer_Literal
(Loc
, Uint_1
),
7833 Right_Opnd
=> Right_Opnd
(N
)));
7835 -- Modular integer case
7837 else pragma Assert
(Is_Modular_Integer_Type
(Typ
));
7839 -- If shift count can be greater than MaxS, we need to wrap
7840 -- the shift in a test that will reduce the result value to
7841 -- zero if this shift count is exceeded.
7845 Make_If_Expression
(Loc
,
7846 Expressions
=> New_List
(
7848 Left_Opnd
=> Duplicate_Subexpr
(Right_Opnd
(N
)),
7849 Right_Opnd
=> Make_Integer_Literal
(Loc
, MaxS
)),
7851 Make_Integer_Literal
(Loc
, Uint_0
),
7853 Make_Op_Shift_Left
(Loc
,
7854 Left_Opnd
=> Make_Integer_Literal
(Loc
, Uint_1
),
7855 Right_Opnd
=> Right_Opnd
(N
)))));
7857 -- If we know shift count cannot be greater than MaxS, then
7858 -- it is safe to just rewrite as a shift with no test.
7862 Make_Op_Shift_Left
(Loc
,
7863 Left_Opnd
=> Make_Integer_Literal
(Loc
, Uint_1
),
7864 Right_Opnd
=> Right_Opnd
(N
)));
7868 Analyze_And_Resolve
(N
, Typ
);
7874 -- Fall through if exponentiation must be done using a runtime routine
7876 -- First deal with modular case
7878 if Is_Modular_Integer_Type
(Rtyp
) then
7880 -- Nonbinary case, we call the special exponentiation routine for
7881 -- the nonbinary case, converting the argument to Long_Long_Integer
7882 -- and passing the modulus value. Then the result is converted back
7883 -- to the base type.
7885 if Non_Binary_Modulus
(Rtyp
) then
7888 Make_Function_Call
(Loc
,
7890 New_Occurrence_Of
(RTE
(RE_Exp_Modular
), Loc
),
7891 Parameter_Associations
=> New_List
(
7892 Convert_To
(RTE
(RE_Unsigned
), Base
),
7893 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
)),
7896 -- Binary case, in this case, we call one of two routines, either the
7897 -- unsigned integer case, or the unsigned long long integer case,
7898 -- with a final "and" operation to do the required mod.
7901 if UI_To_Int
(Esize
(Rtyp
)) <= Standard_Integer_Size
then
7902 Ent
:= RTE
(RE_Exp_Unsigned
);
7904 Ent
:= RTE
(RE_Exp_Long_Long_Unsigned
);
7911 Make_Function_Call
(Loc
,
7912 Name
=> New_Occurrence_Of
(Ent
, Loc
),
7913 Parameter_Associations
=> New_List
(
7914 Convert_To
(Etype
(First_Formal
(Ent
)), Base
),
7917 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
) - 1))));
7921 -- Common exit point for modular type case
7923 Analyze_And_Resolve
(N
, Typ
);
7926 -- Signed integer cases, done using either Integer or Long_Long_Integer.
7927 -- It is not worth having routines for Short_[Short_]Integer, since for
7928 -- most machines it would not help, and it would generate more code that
7929 -- might need certification when a certified run time is required.
7931 -- In the integer cases, we have two routines, one for when overflow
7932 -- checks are required, and one when they are not required, since there
7933 -- is a real gain in omitting checks on many machines.
7935 elsif Rtyp
= Base_Type
(Standard_Long_Long_Integer
)
7936 or else (Rtyp
= Base_Type
(Standard_Long_Integer
)
7938 Esize
(Standard_Long_Integer
) > Esize
(Standard_Integer
))
7939 or else Rtyp
= Universal_Integer
7941 Etyp
:= Standard_Long_Long_Integer
;
7943 -- Overflow checking is the only choice on the AAMP target, where
7944 -- arithmetic instructions check overflow automatically, so only
7945 -- one version of the exponentiation unit is needed.
7947 if Ovflo
or AAMP_On_Target
then
7948 Rent
:= RE_Exp_Long_Long_Integer
;
7950 Rent
:= RE_Exn_Long_Long_Integer
;
7953 elsif Is_Signed_Integer_Type
(Rtyp
) then
7954 Etyp
:= Standard_Integer
;
7956 -- Overflow checking is the only choice on the AAMP target, where
7957 -- arithmetic instructions check overflow automatically, so only
7958 -- one version of the exponentiation unit is needed.
7960 if Ovflo
or AAMP_On_Target
then
7961 Rent
:= RE_Exp_Integer
;
7963 Rent
:= RE_Exn_Integer
;
7966 -- Floating-point cases, always done using Long_Long_Float. We do not
7967 -- need separate routines for the overflow case here, since in the case
7968 -- of floating-point, we generate infinities anyway as a rule (either
7969 -- that or we automatically trap overflow), and if there is an infinity
7970 -- generated and a range check is required, the check will fail anyway.
7973 pragma Assert
(Is_Floating_Point_Type
(Rtyp
));
7974 Etyp
:= Standard_Long_Long_Float
;
7975 Rent
:= RE_Exn_Long_Long_Float
;
7978 -- Common processing for integer cases and floating-point cases.
7979 -- If we are in the right type, we can call runtime routine directly
7982 and then Rtyp
/= Universal_Integer
7983 and then Rtyp
/= Universal_Real
7986 Make_Function_Call
(Loc
,
7987 Name
=> New_Occurrence_Of
(RTE
(Rent
), Loc
),
7988 Parameter_Associations
=> New_List
(Base
, Exp
)));
7990 -- Otherwise we have to introduce conversions (conversions are also
7991 -- required in the universal cases, since the runtime routine is
7992 -- typed using one of the standard types).
7997 Make_Function_Call
(Loc
,
7998 Name
=> New_Occurrence_Of
(RTE
(Rent
), Loc
),
7999 Parameter_Associations
=> New_List
(
8000 Convert_To
(Etyp
, Base
),
8004 Analyze_And_Resolve
(N
, Typ
);
8008 when RE_Not_Available
=>
8010 end Expand_N_Op_Expon
;
8012 --------------------
8013 -- Expand_N_Op_Ge --
8014 --------------------
8016 procedure Expand_N_Op_Ge
(N
: Node_Id
) is
8017 Typ
: constant Entity_Id
:= Etype
(N
);
8018 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8019 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8020 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8023 Binary_Op_Validity_Checks
(N
);
8025 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8026 -- means we no longer have a comparison operation, we are all done.
8028 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8030 if Nkind
(N
) /= N_Op_Ge
then
8036 if Is_Array_Type
(Typ1
) then
8037 Expand_Array_Comparison
(N
);
8041 -- Deal with boolean operands
8043 if Is_Boolean_Type
(Typ1
) then
8044 Adjust_Condition
(Op1
);
8045 Adjust_Condition
(Op2
);
8046 Set_Etype
(N
, Standard_Boolean
);
8047 Adjust_Result_Type
(N
, Typ
);
8050 Rewrite_Comparison
(N
);
8052 Optimize_Length_Comparison
(N
);
8055 --------------------
8056 -- Expand_N_Op_Gt --
8057 --------------------
8059 procedure Expand_N_Op_Gt
(N
: Node_Id
) is
8060 Typ
: constant Entity_Id
:= Etype
(N
);
8061 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8062 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8063 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8066 Binary_Op_Validity_Checks
(N
);
8068 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8069 -- means we no longer have a comparison operation, we are all done.
8071 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8073 if Nkind
(N
) /= N_Op_Gt
then
8077 -- Deal with array type operands
8079 if Is_Array_Type
(Typ1
) then
8080 Expand_Array_Comparison
(N
);
8084 -- Deal with boolean type operands
8086 if Is_Boolean_Type
(Typ1
) then
8087 Adjust_Condition
(Op1
);
8088 Adjust_Condition
(Op2
);
8089 Set_Etype
(N
, Standard_Boolean
);
8090 Adjust_Result_Type
(N
, Typ
);
8093 Rewrite_Comparison
(N
);
8095 Optimize_Length_Comparison
(N
);
8098 --------------------
8099 -- Expand_N_Op_Le --
8100 --------------------
8102 procedure Expand_N_Op_Le
(N
: Node_Id
) is
8103 Typ
: constant Entity_Id
:= Etype
(N
);
8104 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8105 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8106 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8109 Binary_Op_Validity_Checks
(N
);
8111 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8112 -- means we no longer have a comparison operation, we are all done.
8114 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8116 if Nkind
(N
) /= N_Op_Le
then
8120 -- Deal with array type operands
8122 if Is_Array_Type
(Typ1
) then
8123 Expand_Array_Comparison
(N
);
8127 -- Deal with Boolean type operands
8129 if Is_Boolean_Type
(Typ1
) then
8130 Adjust_Condition
(Op1
);
8131 Adjust_Condition
(Op2
);
8132 Set_Etype
(N
, Standard_Boolean
);
8133 Adjust_Result_Type
(N
, Typ
);
8136 Rewrite_Comparison
(N
);
8138 Optimize_Length_Comparison
(N
);
8141 --------------------
8142 -- Expand_N_Op_Lt --
8143 --------------------
8145 procedure Expand_N_Op_Lt
(N
: Node_Id
) is
8146 Typ
: constant Entity_Id
:= Etype
(N
);
8147 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8148 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8149 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8152 Binary_Op_Validity_Checks
(N
);
8154 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8155 -- means we no longer have a comparison operation, we are all done.
8157 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8159 if Nkind
(N
) /= N_Op_Lt
then
8163 -- Deal with array type operands
8165 if Is_Array_Type
(Typ1
) then
8166 Expand_Array_Comparison
(N
);
8170 -- Deal with Boolean type operands
8172 if Is_Boolean_Type
(Typ1
) then
8173 Adjust_Condition
(Op1
);
8174 Adjust_Condition
(Op2
);
8175 Set_Etype
(N
, Standard_Boolean
);
8176 Adjust_Result_Type
(N
, Typ
);
8179 Rewrite_Comparison
(N
);
8181 Optimize_Length_Comparison
(N
);
8184 -----------------------
8185 -- Expand_N_Op_Minus --
8186 -----------------------
8188 procedure Expand_N_Op_Minus
(N
: Node_Id
) is
8189 Loc
: constant Source_Ptr
:= Sloc
(N
);
8190 Typ
: constant Entity_Id
:= Etype
(N
);
8193 Unary_Op_Validity_Checks
(N
);
8195 -- Check for MINIMIZED/ELIMINATED overflow mode
8197 if Minimized_Eliminated_Overflow_Check
(N
) then
8198 Apply_Arithmetic_Overflow_Check
(N
);
8202 if not Backend_Overflow_Checks_On_Target
8203 and then Is_Signed_Integer_Type
(Etype
(N
))
8204 and then Do_Overflow_Check
(N
)
8206 -- Software overflow checking expands -expr into (0 - expr)
8209 Make_Op_Subtract
(Loc
,
8210 Left_Opnd
=> Make_Integer_Literal
(Loc
, 0),
8211 Right_Opnd
=> Right_Opnd
(N
)));
8213 Analyze_And_Resolve
(N
, Typ
);
8215 end Expand_N_Op_Minus
;
8217 ---------------------
8218 -- Expand_N_Op_Mod --
8219 ---------------------
8221 procedure Expand_N_Op_Mod
(N
: Node_Id
) is
8222 Loc
: constant Source_Ptr
:= Sloc
(N
);
8223 Typ
: constant Entity_Id
:= Etype
(N
);
8224 DDC
: constant Boolean := Do_Division_Check
(N
);
8237 pragma Warnings
(Off
, Lhi
);
8240 Binary_Op_Validity_Checks
(N
);
8242 -- Check for MINIMIZED/ELIMINATED overflow mode
8244 if Minimized_Eliminated_Overflow_Check
(N
) then
8245 Apply_Arithmetic_Overflow_Check
(N
);
8249 if Is_Integer_Type
(Etype
(N
)) then
8250 Apply_Divide_Checks
(N
);
8252 -- All done if we don't have a MOD any more, which can happen as a
8253 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8255 if Nkind
(N
) /= N_Op_Mod
then
8260 -- Proceed with expansion of mod operator
8262 Left
:= Left_Opnd
(N
);
8263 Right
:= Right_Opnd
(N
);
8265 Determine_Range
(Right
, ROK
, Rlo
, Rhi
, Assume_Valid
=> True);
8266 Determine_Range
(Left
, LOK
, Llo
, Lhi
, Assume_Valid
=> True);
8268 -- Convert mod to rem if operands are both known to be non-negative, or
8269 -- both known to be non-positive (these are the cases in which rem and
8270 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
8271 -- likely that this will improve the quality of code, (the operation now
8272 -- corresponds to the hardware remainder), and it does not seem likely
8273 -- that it could be harmful. It also avoids some cases of the elaborate
8274 -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %).
8277 and then ((Llo
>= 0 and then Rlo
>= 0)
8279 (Lhi
<= 0 and then Rhi
<= 0))
8282 Make_Op_Rem
(Sloc
(N
),
8283 Left_Opnd
=> Left_Opnd
(N
),
8284 Right_Opnd
=> Right_Opnd
(N
)));
8286 -- Instead of reanalyzing the node we do the analysis manually. This
8287 -- avoids anomalies when the replacement is done in an instance and
8288 -- is epsilon more efficient.
8290 Set_Entity
(N
, Standard_Entity
(S_Op_Rem
));
8292 Set_Do_Division_Check
(N
, DDC
);
8293 Expand_N_Op_Rem
(N
);
8297 -- Otherwise, normal mod processing
8300 -- Apply optimization x mod 1 = 0. We don't really need that with
8301 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
8302 -- certainly harmless.
8304 if Is_Integer_Type
(Etype
(N
))
8305 and then Compile_Time_Known_Value
(Right
)
8306 and then Expr_Value
(Right
) = Uint_1
8308 -- Call Remove_Side_Effects to ensure that any side effects in
8309 -- the ignored left operand (in particular function calls to
8310 -- user defined functions) are properly executed.
8312 Remove_Side_Effects
(Left
);
8314 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
8315 Analyze_And_Resolve
(N
, Typ
);
8319 -- If we still have a mod operator and we are in Modify_Tree_For_C
8320 -- mode, and we have a signed integer type, then here is where we do
8321 -- the rewrite in terms of Rem. Note this rewrite bypasses the need
8322 -- for the special handling of the annoying case of largest negative
8323 -- number mod minus one.
8325 if Nkind
(N
) = N_Op_Mod
8326 and then Is_Signed_Integer_Type
(Typ
)
8327 and then Modify_Tree_For_C
8329 -- In the general case, we expand A mod B as
8331 -- Tnn : constant typ := A rem B;
8333 -- (if (A >= 0) = (B >= 0) then Tnn
8334 -- elsif Tnn = 0 then 0
8337 -- The comparison can be written simply as A >= 0 if we know that
8338 -- B >= 0 which is a very common case.
8340 -- An important optimization is when B is known at compile time
8341 -- to be 2**K for some constant. In this case we can simply AND
8342 -- the left operand with the bit string 2**K-1 (i.e. K 1-bits)
8343 -- and that works for both the positive and negative cases.
8346 P2
: constant Nat
:= Power_Of_Two
(Right
);
8351 Unchecked_Convert_To
(Typ
,
8354 Unchecked_Convert_To
8355 (Corresponding_Unsigned_Type
(Typ
), Left
),
8357 Make_Integer_Literal
(Loc
, 2 ** P2
- 1))));
8358 Analyze_And_Resolve
(N
, Typ
);
8363 -- Here for the full rewrite
8366 Tnn
: constant Entity_Id
:= Make_Temporary
(Sloc
(N
), 'T', N
);
8372 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left
),
8373 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
8375 if not LOK
or else Rlo
< 0 then
8381 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Right
),
8382 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)));
8386 Make_Object_Declaration
(Loc
,
8387 Defining_Identifier
=> Tnn
,
8388 Constant_Present
=> True,
8389 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
8393 Right_Opnd
=> Right
)));
8396 Make_If_Expression
(Loc
,
8397 Expressions
=> New_List
(
8399 New_Occurrence_Of
(Tnn
, Loc
),
8400 Make_If_Expression
(Loc
,
8402 Expressions
=> New_List
(
8404 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
8405 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
8406 Make_Integer_Literal
(Loc
, 0),
8408 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
8410 Duplicate_Subexpr_No_Checks
(Right
)))))));
8412 Analyze_And_Resolve
(N
, Typ
);
8417 -- Deal with annoying case of largest negative number mod minus one.
8418 -- Gigi may not handle this case correctly, because on some targets,
8419 -- the mod value is computed using a divide instruction which gives
8420 -- an overflow trap for this case.
8422 -- It would be a bit more efficient to figure out which targets
8423 -- this is really needed for, but in practice it is reasonable
8424 -- to do the following special check in all cases, since it means
8425 -- we get a clearer message, and also the overhead is minimal given
8426 -- that division is expensive in any case.
8428 -- In fact the check is quite easy, if the right operand is -1, then
8429 -- the mod value is always 0, and we can just ignore the left operand
8430 -- completely in this case.
8432 -- This only applies if we still have a mod operator. Skip if we
8433 -- have already rewritten this (e.g. in the case of eliminated
8434 -- overflow checks which have driven us into bignum mode).
8436 if Nkind
(N
) = N_Op_Mod
then
8438 -- The operand type may be private (e.g. in the expansion of an
8439 -- intrinsic operation) so we must use the underlying type to get
8440 -- the bounds, and convert the literals explicitly.
8444 (Type_Low_Bound
(Base_Type
(Underlying_Type
(Etype
(Left
)))));
8446 if ((not ROK
) or else (Rlo
<= (-1) and then (-1) <= Rhi
))
8447 and then ((not LOK
) or else (Llo
= LLB
))
8450 Make_If_Expression
(Loc
,
8451 Expressions
=> New_List
(
8453 Left_Opnd
=> Duplicate_Subexpr
(Right
),
8455 Unchecked_Convert_To
(Typ
,
8456 Make_Integer_Literal
(Loc
, -1))),
8457 Unchecked_Convert_To
(Typ
,
8458 Make_Integer_Literal
(Loc
, Uint_0
)),
8459 Relocate_Node
(N
))));
8461 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
8462 Analyze_And_Resolve
(N
, Typ
);
8466 end Expand_N_Op_Mod
;
8468 --------------------------
8469 -- Expand_N_Op_Multiply --
8470 --------------------------
8472 procedure Expand_N_Op_Multiply
(N
: Node_Id
) is
8473 Loc
: constant Source_Ptr
:= Sloc
(N
);
8474 Lop
: constant Node_Id
:= Left_Opnd
(N
);
8475 Rop
: constant Node_Id
:= Right_Opnd
(N
);
8477 Lp2
: constant Boolean :=
8478 Nkind
(Lop
) = N_Op_Expon
and then Is_Power_Of_2_For_Shift
(Lop
);
8479 Rp2
: constant Boolean :=
8480 Nkind
(Rop
) = N_Op_Expon
and then Is_Power_Of_2_For_Shift
(Rop
);
8482 Ltyp
: constant Entity_Id
:= Etype
(Lop
);
8483 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
8484 Typ
: Entity_Id
:= Etype
(N
);
8487 Binary_Op_Validity_Checks
(N
);
8489 -- Check for MINIMIZED/ELIMINATED overflow mode
8491 if Minimized_Eliminated_Overflow_Check
(N
) then
8492 Apply_Arithmetic_Overflow_Check
(N
);
8496 -- Special optimizations for integer types
8498 if Is_Integer_Type
(Typ
) then
8500 -- N * 0 = 0 for integer types
8502 if Compile_Time_Known_Value
(Rop
)
8503 and then Expr_Value
(Rop
) = Uint_0
8505 -- Call Remove_Side_Effects to ensure that any side effects in
8506 -- the ignored left operand (in particular function calls to
8507 -- user defined functions) are properly executed.
8509 Remove_Side_Effects
(Lop
);
8511 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
8512 Analyze_And_Resolve
(N
, Typ
);
8516 -- Similar handling for 0 * N = 0
8518 if Compile_Time_Known_Value
(Lop
)
8519 and then Expr_Value
(Lop
) = Uint_0
8521 Remove_Side_Effects
(Rop
);
8522 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
8523 Analyze_And_Resolve
(N
, Typ
);
8527 -- N * 1 = 1 * N = N for integer types
8529 -- This optimisation is not done if we are going to
8530 -- rewrite the product 1 * 2 ** N to a shift.
8532 if Compile_Time_Known_Value
(Rop
)
8533 and then Expr_Value
(Rop
) = Uint_1
8539 elsif Compile_Time_Known_Value
(Lop
)
8540 and then Expr_Value
(Lop
) = Uint_1
8548 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
8549 -- Is_Power_Of_2_For_Shift is set means that we know that our left
8550 -- operand is an integer, as required for this to work.
8555 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
8559 Left_Opnd
=> Make_Integer_Literal
(Loc
, 2),
8562 Left_Opnd
=> Right_Opnd
(Lop
),
8563 Right_Opnd
=> Right_Opnd
(Rop
))));
8564 Analyze_And_Resolve
(N
, Typ
);
8568 -- If the result is modular, perform the reduction of the result
8571 if Is_Modular_Integer_Type
(Typ
)
8572 and then not Non_Binary_Modulus
(Typ
)
8577 Make_Op_Shift_Left
(Loc
,
8580 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))),
8582 Make_Integer_Literal
(Loc
, Modulus
(Typ
) - 1)));
8586 Make_Op_Shift_Left
(Loc
,
8589 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))));
8592 Analyze_And_Resolve
(N
, Typ
);
8596 -- Same processing for the operands the other way round
8599 if Is_Modular_Integer_Type
(Typ
)
8600 and then not Non_Binary_Modulus
(Typ
)
8605 Make_Op_Shift_Left
(Loc
,
8608 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))),
8610 Make_Integer_Literal
(Loc
, Modulus
(Typ
) - 1)));
8614 Make_Op_Shift_Left
(Loc
,
8617 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))));
8620 Analyze_And_Resolve
(N
, Typ
);
8624 -- Do required fixup of universal fixed operation
8626 if Typ
= Universal_Fixed
then
8627 Fixup_Universal_Fixed_Operation
(N
);
8631 -- Multiplications with fixed-point results
8633 if Is_Fixed_Point_Type
(Typ
) then
8635 -- No special processing if Treat_Fixed_As_Integer is set, since from
8636 -- a semantic point of view such operations are simply integer
8637 -- operations and will be treated that way.
8639 if not Treat_Fixed_As_Integer
(N
) then
8641 -- Case of fixed * integer => fixed
8643 if Is_Integer_Type
(Rtyp
) then
8644 Expand_Multiply_Fixed_By_Integer_Giving_Fixed
(N
);
8646 -- Case of integer * fixed => fixed
8648 elsif Is_Integer_Type
(Ltyp
) then
8649 Expand_Multiply_Integer_By_Fixed_Giving_Fixed
(N
);
8651 -- Case of fixed * fixed => fixed
8654 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed
(N
);
8658 -- Other cases of multiplication of fixed-point operands. Again we
8659 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
8661 elsif (Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
))
8662 and then not Treat_Fixed_As_Integer
(N
)
8664 if Is_Integer_Type
(Typ
) then
8665 Expand_Multiply_Fixed_By_Fixed_Giving_Integer
(N
);
8667 pragma Assert
(Is_Floating_Point_Type
(Typ
));
8668 Expand_Multiply_Fixed_By_Fixed_Giving_Float
(N
);
8671 -- Mixed-mode operations can appear in a non-static universal context,
8672 -- in which case the integer argument must be converted explicitly.
8674 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Rtyp
) then
8675 Rewrite
(Rop
, Convert_To
(Universal_Real
, Relocate_Node
(Rop
)));
8676 Analyze_And_Resolve
(Rop
, Universal_Real
);
8678 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Ltyp
) then
8679 Rewrite
(Lop
, Convert_To
(Universal_Real
, Relocate_Node
(Lop
)));
8680 Analyze_And_Resolve
(Lop
, Universal_Real
);
8682 -- Non-fixed point cases, check software overflow checking required
8684 elsif Is_Signed_Integer_Type
(Etype
(N
)) then
8685 Apply_Arithmetic_Overflow_Check
(N
);
8688 -- Overflow checks for floating-point if -gnateF mode active
8690 Check_Float_Op_Overflow
(N
);
8691 end Expand_N_Op_Multiply
;
8693 --------------------
8694 -- Expand_N_Op_Ne --
8695 --------------------
8697 procedure Expand_N_Op_Ne
(N
: Node_Id
) is
8698 Typ
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
8701 -- Case of elementary type with standard operator
8703 if Is_Elementary_Type
(Typ
)
8704 and then Sloc
(Entity
(N
)) = Standard_Location
8706 Binary_Op_Validity_Checks
(N
);
8708 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
8709 -- means we no longer have a /= operation, we are all done.
8711 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8713 if Nkind
(N
) /= N_Op_Ne
then
8717 -- Boolean types (requiring handling of non-standard case)
8719 if Is_Boolean_Type
(Typ
) then
8720 Adjust_Condition
(Left_Opnd
(N
));
8721 Adjust_Condition
(Right_Opnd
(N
));
8722 Set_Etype
(N
, Standard_Boolean
);
8723 Adjust_Result_Type
(N
, Typ
);
8726 Rewrite_Comparison
(N
);
8728 -- For all cases other than elementary types, we rewrite node as the
8729 -- negation of an equality operation, and reanalyze. The equality to be
8730 -- used is defined in the same scope and has the same signature. This
8731 -- signature must be set explicitly since in an instance it may not have
8732 -- the same visibility as in the generic unit. This avoids duplicating
8733 -- or factoring the complex code for record/array equality tests etc.
8737 Loc
: constant Source_Ptr
:= Sloc
(N
);
8739 Ne
: constant Entity_Id
:= Entity
(N
);
8742 Binary_Op_Validity_Checks
(N
);
8748 Left_Opnd
=> Left_Opnd
(N
),
8749 Right_Opnd
=> Right_Opnd
(N
)));
8750 Set_Paren_Count
(Right_Opnd
(Neg
), 1);
8752 if Scope
(Ne
) /= Standard_Standard
then
8753 Set_Entity
(Right_Opnd
(Neg
), Corresponding_Equality
(Ne
));
8756 -- For navigation purposes, we want to treat the inequality as an
8757 -- implicit reference to the corresponding equality. Preserve the
8758 -- Comes_From_ source flag to generate proper Xref entries.
8760 Preserve_Comes_From_Source
(Neg
, N
);
8761 Preserve_Comes_From_Source
(Right_Opnd
(Neg
), N
);
8763 Analyze_And_Resolve
(N
, Standard_Boolean
);
8767 Optimize_Length_Comparison
(N
);
8770 ---------------------
8771 -- Expand_N_Op_Not --
8772 ---------------------
8774 -- If the argument is other than a Boolean array type, there is no special
8775 -- expansion required, except for dealing with validity checks, and non-
8776 -- standard boolean representations.
8778 -- For the packed array case, we call the special routine in Exp_Pakd,
8779 -- except that if the component size is greater than one, we use the
8780 -- standard routine generating a gruesome loop (it is so peculiar to have
8781 -- packed arrays with non-standard Boolean representations anyway, so it
8782 -- does not matter that we do not handle this case efficiently).
8784 -- For the unpacked array case (and for the special packed case where we
8785 -- have non standard Booleans, as discussed above), we generate and insert
8786 -- into the tree the following function definition:
8788 -- function Nnnn (A : arr) is
8791 -- for J in a'range loop
8792 -- B (J) := not A (J);
8797 -- Here arr is the actual subtype of the parameter (and hence always
8798 -- constrained). Then we replace the not with a call to this function.
8800 procedure Expand_N_Op_Not
(N
: Node_Id
) is
8801 Loc
: constant Source_Ptr
:= Sloc
(N
);
8802 Typ
: constant Entity_Id
:= Etype
(N
);
8811 Func_Name
: Entity_Id
;
8812 Loop_Statement
: Node_Id
;
8815 Unary_Op_Validity_Checks
(N
);
8817 -- For boolean operand, deal with non-standard booleans
8819 if Is_Boolean_Type
(Typ
) then
8820 Adjust_Condition
(Right_Opnd
(N
));
8821 Set_Etype
(N
, Standard_Boolean
);
8822 Adjust_Result_Type
(N
, Typ
);
8826 -- Only array types need any other processing
8828 if not Is_Array_Type
(Typ
) then
8832 -- Case of array operand. If bit packed with a component size of 1,
8833 -- handle it in Exp_Pakd if the operand is known to be aligned.
8835 if Is_Bit_Packed_Array
(Typ
)
8836 and then Component_Size
(Typ
) = 1
8837 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
8839 Expand_Packed_Not
(N
);
8843 -- Case of array operand which is not bit-packed. If the context is
8844 -- a safe assignment, call in-place operation, If context is a larger
8845 -- boolean expression in the context of a safe assignment, expansion is
8846 -- done by enclosing operation.
8848 Opnd
:= Relocate_Node
(Right_Opnd
(N
));
8849 Convert_To_Actual_Subtype
(Opnd
);
8850 Arr
:= Etype
(Opnd
);
8851 Ensure_Defined
(Arr
, N
);
8852 Silly_Boolean_Array_Not_Test
(N
, Arr
);
8854 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
8855 if Safe_In_Place_Array_Op
(Name
(Parent
(N
)), N
, Empty
) then
8856 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
8859 -- Special case the negation of a binary operation
8861 elsif Nkind_In
(Opnd
, N_Op_And
, N_Op_Or
, N_Op_Xor
)
8862 and then Safe_In_Place_Array_Op
8863 (Name
(Parent
(N
)), Left_Opnd
(Opnd
), Right_Opnd
(Opnd
))
8865 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
8869 elsif Nkind
(Parent
(N
)) in N_Binary_Op
8870 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
8873 Op1
: constant Node_Id
:= Left_Opnd
(Parent
(N
));
8874 Op2
: constant Node_Id
:= Right_Opnd
(Parent
(N
));
8875 Lhs
: constant Node_Id
:= Name
(Parent
(Parent
(N
)));
8878 if Safe_In_Place_Array_Op
(Lhs
, Op1
, Op2
) then
8880 -- (not A) op (not B) can be reduced to a single call
8882 if N
= Op1
and then Nkind
(Op2
) = N_Op_Not
then
8885 elsif N
= Op2
and then Nkind
(Op1
) = N_Op_Not
then
8888 -- A xor (not B) can also be special-cased
8890 elsif N
= Op2
and then Nkind
(Parent
(N
)) = N_Op_Xor
then
8897 A
:= Make_Defining_Identifier
(Loc
, Name_uA
);
8898 B
:= Make_Defining_Identifier
(Loc
, Name_uB
);
8899 J
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
8902 Make_Indexed_Component
(Loc
,
8903 Prefix
=> New_Occurrence_Of
(A
, Loc
),
8904 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
8907 Make_Indexed_Component
(Loc
,
8908 Prefix
=> New_Occurrence_Of
(B
, Loc
),
8909 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
8912 Make_Implicit_Loop_Statement
(N
,
8913 Identifier
=> Empty
,
8916 Make_Iteration_Scheme
(Loc
,
8917 Loop_Parameter_Specification
=>
8918 Make_Loop_Parameter_Specification
(Loc
,
8919 Defining_Identifier
=> J
,
8920 Discrete_Subtype_Definition
=>
8921 Make_Attribute_Reference
(Loc
,
8922 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
8923 Attribute_Name
=> Name_Range
))),
8925 Statements
=> New_List
(
8926 Make_Assignment_Statement
(Loc
,
8928 Expression
=> Make_Op_Not
(Loc
, A_J
))));
8930 Func_Name
:= Make_Temporary
(Loc
, 'N');
8931 Set_Is_Inlined
(Func_Name
);
8934 Make_Subprogram_Body
(Loc
,
8936 Make_Function_Specification
(Loc
,
8937 Defining_Unit_Name
=> Func_Name
,
8938 Parameter_Specifications
=> New_List
(
8939 Make_Parameter_Specification
(Loc
,
8940 Defining_Identifier
=> A
,
8941 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
))),
8942 Result_Definition
=> New_Occurrence_Of
(Typ
, Loc
)),
8944 Declarations
=> New_List
(
8945 Make_Object_Declaration
(Loc
,
8946 Defining_Identifier
=> B
,
8947 Object_Definition
=> New_Occurrence_Of
(Arr
, Loc
))),
8949 Handled_Statement_Sequence
=>
8950 Make_Handled_Sequence_Of_Statements
(Loc
,
8951 Statements
=> New_List
(
8953 Make_Simple_Return_Statement
(Loc
,
8954 Expression
=> Make_Identifier
(Loc
, Chars
(B
)))))));
8957 Make_Function_Call
(Loc
,
8958 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
8959 Parameter_Associations
=> New_List
(Opnd
)));
8961 Analyze_And_Resolve
(N
, Typ
);
8962 end Expand_N_Op_Not
;
8964 --------------------
8965 -- Expand_N_Op_Or --
8966 --------------------
8968 procedure Expand_N_Op_Or
(N
: Node_Id
) is
8969 Typ
: constant Entity_Id
:= Etype
(N
);
8972 Binary_Op_Validity_Checks
(N
);
8974 if Is_Array_Type
(Etype
(N
)) then
8975 Expand_Boolean_Operator
(N
);
8977 elsif Is_Boolean_Type
(Etype
(N
)) then
8978 Adjust_Condition
(Left_Opnd
(N
));
8979 Adjust_Condition
(Right_Opnd
(N
));
8980 Set_Etype
(N
, Standard_Boolean
);
8981 Adjust_Result_Type
(N
, Typ
);
8983 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
8984 Expand_Intrinsic_Call
(N
, Entity
(N
));
8989 ----------------------
8990 -- Expand_N_Op_Plus --
8991 ----------------------
8993 procedure Expand_N_Op_Plus
(N
: Node_Id
) is
8995 Unary_Op_Validity_Checks
(N
);
8997 -- Check for MINIMIZED/ELIMINATED overflow mode
8999 if Minimized_Eliminated_Overflow_Check
(N
) then
9000 Apply_Arithmetic_Overflow_Check
(N
);
9003 end Expand_N_Op_Plus
;
9005 ---------------------
9006 -- Expand_N_Op_Rem --
9007 ---------------------
9009 procedure Expand_N_Op_Rem
(N
: Node_Id
) is
9010 Loc
: constant Source_Ptr
:= Sloc
(N
);
9011 Typ
: constant Entity_Id
:= Etype
(N
);
9022 -- Set if corresponding operand can be negative
9024 pragma Unreferenced
(Hi
);
9027 Binary_Op_Validity_Checks
(N
);
9029 -- Check for MINIMIZED/ELIMINATED overflow mode
9031 if Minimized_Eliminated_Overflow_Check
(N
) then
9032 Apply_Arithmetic_Overflow_Check
(N
);
9036 if Is_Integer_Type
(Etype
(N
)) then
9037 Apply_Divide_Checks
(N
);
9039 -- All done if we don't have a REM any more, which can happen as a
9040 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
9042 if Nkind
(N
) /= N_Op_Rem
then
9047 -- Proceed with expansion of REM
9049 Left
:= Left_Opnd
(N
);
9050 Right
:= Right_Opnd
(N
);
9052 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
9053 -- but it is useful with other back ends (e.g. AAMP), and is certainly
9056 if Is_Integer_Type
(Etype
(N
))
9057 and then Compile_Time_Known_Value
(Right
)
9058 and then Expr_Value
(Right
) = Uint_1
9060 -- Call Remove_Side_Effects to ensure that any side effects in the
9061 -- ignored left operand (in particular function calls to user defined
9062 -- functions) are properly executed.
9064 Remove_Side_Effects
(Left
);
9066 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
9067 Analyze_And_Resolve
(N
, Typ
);
9071 -- Deal with annoying case of largest negative number remainder minus
9072 -- one. Gigi may not handle this case correctly, because on some
9073 -- targets, the mod value is computed using a divide instruction
9074 -- which gives an overflow trap for this case.
9076 -- It would be a bit more efficient to figure out which targets this
9077 -- is really needed for, but in practice it is reasonable to do the
9078 -- following special check in all cases, since it means we get a clearer
9079 -- message, and also the overhead is minimal given that division is
9080 -- expensive in any case.
9082 -- In fact the check is quite easy, if the right operand is -1, then
9083 -- the remainder is always 0, and we can just ignore the left operand
9084 -- completely in this case.
9086 Determine_Range
(Right
, OK
, Lo
, Hi
, Assume_Valid
=> True);
9087 Lneg
:= (not OK
) or else Lo
< 0;
9089 Determine_Range
(Left
, OK
, Lo
, Hi
, Assume_Valid
=> True);
9090 Rneg
:= (not OK
) or else Lo
< 0;
9092 -- We won't mess with trying to find out if the left operand can really
9093 -- be the largest negative number (that's a pain in the case of private
9094 -- types and this is really marginal). We will just assume that we need
9095 -- the test if the left operand can be negative at all.
9097 if Lneg
and Rneg
then
9099 Make_If_Expression
(Loc
,
9100 Expressions
=> New_List
(
9102 Left_Opnd
=> Duplicate_Subexpr
(Right
),
9104 Unchecked_Convert_To
(Typ
, Make_Integer_Literal
(Loc
, -1))),
9106 Unchecked_Convert_To
(Typ
,
9107 Make_Integer_Literal
(Loc
, Uint_0
)),
9109 Relocate_Node
(N
))));
9111 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
9112 Analyze_And_Resolve
(N
, Typ
);
9114 end Expand_N_Op_Rem
;
9116 -----------------------------
9117 -- Expand_N_Op_Rotate_Left --
9118 -----------------------------
9120 procedure Expand_N_Op_Rotate_Left
(N
: Node_Id
) is
9122 Binary_Op_Validity_Checks
(N
);
9124 -- If we are in Modify_Tree_For_C mode, there is no rotate left in C,
9125 -- so we rewrite in terms of logical shifts
9127 -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits)
9129 -- where Bits is the shift count mod Esize (the mod operation here
9130 -- deals with ludicrous large shift counts, which are apparently OK).
9132 -- What about nonbinary modulus ???
9135 Loc
: constant Source_Ptr
:= Sloc
(N
);
9136 Rtp
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
9137 Typ
: constant Entity_Id
:= Etype
(N
);
9140 if Modify_Tree_For_C
then
9141 Rewrite
(Right_Opnd
(N
),
9143 Left_Opnd
=> Relocate_Node
(Right_Opnd
(N
)),
9144 Right_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
))));
9146 Analyze_And_Resolve
(Right_Opnd
(N
), Rtp
);
9151 Make_Op_Shift_Left
(Loc
,
9152 Left_Opnd
=> Left_Opnd
(N
),
9153 Right_Opnd
=> Right_Opnd
(N
)),
9156 Make_Op_Shift_Right
(Loc
,
9157 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left_Opnd
(N
)),
9159 Make_Op_Subtract
(Loc
,
9160 Left_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
)),
9162 Duplicate_Subexpr_No_Checks
(Right_Opnd
(N
))))));
9164 Analyze_And_Resolve
(N
, Typ
);
9167 end Expand_N_Op_Rotate_Left
;
9169 ------------------------------
9170 -- Expand_N_Op_Rotate_Right --
9171 ------------------------------
9173 procedure Expand_N_Op_Rotate_Right
(N
: Node_Id
) is
9175 Binary_Op_Validity_Checks
(N
);
9177 -- If we are in Modify_Tree_For_C mode, there is no rotate right in C,
9178 -- so we rewrite in terms of logical shifts
9180 -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits)
9182 -- where Bits is the shift count mod Esize (the mod operation here
9183 -- deals with ludicrous large shift counts, which are apparently OK).
9185 -- What about nonbinary modulus ???
9188 Loc
: constant Source_Ptr
:= Sloc
(N
);
9189 Rtp
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
9190 Typ
: constant Entity_Id
:= Etype
(N
);
9193 Rewrite
(Right_Opnd
(N
),
9195 Left_Opnd
=> Relocate_Node
(Right_Opnd
(N
)),
9196 Right_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
))));
9198 Analyze_And_Resolve
(Right_Opnd
(N
), Rtp
);
9200 if Modify_Tree_For_C
then
9204 Make_Op_Shift_Right
(Loc
,
9205 Left_Opnd
=> Left_Opnd
(N
),
9206 Right_Opnd
=> Right_Opnd
(N
)),
9209 Make_Op_Shift_Left
(Loc
,
9210 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left_Opnd
(N
)),
9212 Make_Op_Subtract
(Loc
,
9213 Left_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
)),
9215 Duplicate_Subexpr_No_Checks
(Right_Opnd
(N
))))));
9217 Analyze_And_Resolve
(N
, Typ
);
9220 end Expand_N_Op_Rotate_Right
;
9222 ----------------------------
9223 -- Expand_N_Op_Shift_Left --
9224 ----------------------------
9226 -- Note: nothing in this routine depends on left as opposed to right shifts
9227 -- so we share the routine for expanding shift right operations.
9229 procedure Expand_N_Op_Shift_Left
(N
: Node_Id
) is
9231 Binary_Op_Validity_Checks
(N
);
9233 -- If we are in Modify_Tree_For_C mode, then ensure that the right
9234 -- operand is not greater than the word size (since that would not
9235 -- be defined properly by the corresponding C shift operator).
9237 if Modify_Tree_For_C
then
9239 Right
: constant Node_Id
:= Right_Opnd
(N
);
9240 Loc
: constant Source_Ptr
:= Sloc
(Right
);
9241 Typ
: constant Entity_Id
:= Etype
(N
);
9242 Siz
: constant Uint
:= Esize
(Typ
);
9249 if Compile_Time_Known_Value
(Right
) then
9250 if Expr_Value
(Right
) >= Siz
then
9251 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
9252 Analyze_And_Resolve
(N
, Typ
);
9255 -- Not compile time known, find range
9258 Determine_Range
(Right
, OK
, Lo
, Hi
, Assume_Valid
=> True);
9260 -- Nothing to do if known to be OK range, otherwise expand
9262 if not OK
or else Hi
>= Siz
then
9264 -- Prevent recursion on copy of shift node
9266 Orig
:= Relocate_Node
(N
);
9267 Set_Analyzed
(Orig
);
9269 -- Now do the rewrite
9272 Make_If_Expression
(Loc
,
9273 Expressions
=> New_List
(
9275 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Right
),
9276 Right_Opnd
=> Make_Integer_Literal
(Loc
, Siz
)),
9277 Make_Integer_Literal
(Loc
, 0),
9279 Analyze_And_Resolve
(N
, Typ
);
9284 end Expand_N_Op_Shift_Left
;
9286 -----------------------------
9287 -- Expand_N_Op_Shift_Right --
9288 -----------------------------
9290 procedure Expand_N_Op_Shift_Right
(N
: Node_Id
) is
9292 -- Share shift left circuit
9294 Expand_N_Op_Shift_Left
(N
);
9295 end Expand_N_Op_Shift_Right
;
9297 ----------------------------------------
9298 -- Expand_N_Op_Shift_Right_Arithmetic --
9299 ----------------------------------------
9301 procedure Expand_N_Op_Shift_Right_Arithmetic
(N
: Node_Id
) is
9303 Binary_Op_Validity_Checks
(N
);
9305 -- If we are in Modify_Tree_For_C mode, there is no shift right
9306 -- arithmetic in C, so we rewrite in terms of logical shifts.
9308 -- Shift_Right (Num, Bits) or
9310 -- then not (Shift_Right (Mask, bits))
9313 -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1)
9315 -- Note: in almost all C compilers it would work to just shift a
9316 -- signed integer right, but it's undefined and we cannot rely on it.
9318 -- Note: the above works fine for shift counts greater than or equal
9319 -- to the word size, since in this case (not (Shift_Right (Mask, bits)))
9320 -- generates all 1'bits.
9322 -- What about nonbinary modulus ???
9325 Loc
: constant Source_Ptr
:= Sloc
(N
);
9326 Typ
: constant Entity_Id
:= Etype
(N
);
9327 Sign
: constant Uint
:= 2 ** (Esize
(Typ
) - 1);
9328 Mask
: constant Uint
:= (2 ** Esize
(Typ
)) - 1;
9329 Left
: constant Node_Id
:= Left_Opnd
(N
);
9330 Right
: constant Node_Id
:= Right_Opnd
(N
);
9334 if Modify_Tree_For_C
then
9336 -- Here if not (Shift_Right (Mask, bits)) can be computed at
9337 -- compile time as a single constant.
9339 if Compile_Time_Known_Value
(Right
) then
9341 Val
: constant Uint
:= Expr_Value
(Right
);
9344 if Val
>= Esize
(Typ
) then
9345 Maskx
:= Make_Integer_Literal
(Loc
, Mask
);
9349 Make_Integer_Literal
(Loc
,
9350 Intval
=> Mask
- (Mask
/ (2 ** Expr_Value
(Right
))));
9358 Make_Op_Shift_Right
(Loc
,
9359 Left_Opnd
=> Make_Integer_Literal
(Loc
, Mask
),
9360 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Right
)));
9363 -- Now do the rewrite
9368 Make_Op_Shift_Right
(Loc
,
9370 Right_Opnd
=> Right
),
9372 Make_If_Expression
(Loc
,
9373 Expressions
=> New_List
(
9375 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left
),
9376 Right_Opnd
=> Make_Integer_Literal
(Loc
, Sign
)),
9378 Make_Integer_Literal
(Loc
, 0)))));
9379 Analyze_And_Resolve
(N
, Typ
);
9382 end Expand_N_Op_Shift_Right_Arithmetic
;
9384 --------------------------
9385 -- Expand_N_Op_Subtract --
9386 --------------------------
9388 procedure Expand_N_Op_Subtract
(N
: Node_Id
) is
9389 Typ
: constant Entity_Id
:= Etype
(N
);
9392 Binary_Op_Validity_Checks
(N
);
9394 -- Check for MINIMIZED/ELIMINATED overflow mode
9396 if Minimized_Eliminated_Overflow_Check
(N
) then
9397 Apply_Arithmetic_Overflow_Check
(N
);
9401 -- N - 0 = N for integer types
9403 if Is_Integer_Type
(Typ
)
9404 and then Compile_Time_Known_Value
(Right_Opnd
(N
))
9405 and then Expr_Value
(Right_Opnd
(N
)) = 0
9407 Rewrite
(N
, Left_Opnd
(N
));
9411 -- Arithmetic overflow checks for signed integer/fixed point types
9413 if Is_Signed_Integer_Type
(Typ
) or else Is_Fixed_Point_Type
(Typ
) then
9414 Apply_Arithmetic_Overflow_Check
(N
);
9417 -- Overflow checks for floating-point if -gnateF mode active
9419 Check_Float_Op_Overflow
(N
);
9420 end Expand_N_Op_Subtract
;
9422 ---------------------
9423 -- Expand_N_Op_Xor --
9424 ---------------------
9426 procedure Expand_N_Op_Xor
(N
: Node_Id
) is
9427 Typ
: constant Entity_Id
:= Etype
(N
);
9430 Binary_Op_Validity_Checks
(N
);
9432 if Is_Array_Type
(Etype
(N
)) then
9433 Expand_Boolean_Operator
(N
);
9435 elsif Is_Boolean_Type
(Etype
(N
)) then
9436 Adjust_Condition
(Left_Opnd
(N
));
9437 Adjust_Condition
(Right_Opnd
(N
));
9438 Set_Etype
(N
, Standard_Boolean
);
9439 Adjust_Result_Type
(N
, Typ
);
9441 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
9442 Expand_Intrinsic_Call
(N
, Entity
(N
));
9445 end Expand_N_Op_Xor
;
9447 ----------------------
9448 -- Expand_N_Or_Else --
9449 ----------------------
9451 procedure Expand_N_Or_Else
(N
: Node_Id
)
9452 renames Expand_Short_Circuit_Operator
;
9454 -----------------------------------
9455 -- Expand_N_Qualified_Expression --
9456 -----------------------------------
9458 procedure Expand_N_Qualified_Expression
(N
: Node_Id
) is
9459 Operand
: constant Node_Id
:= Expression
(N
);
9460 Target_Type
: constant Entity_Id
:= Entity
(Subtype_Mark
(N
));
9463 -- Do validity check if validity checking operands
9465 if Validity_Checks_On
and Validity_Check_Operands
then
9466 Ensure_Valid
(Operand
);
9469 -- Apply possible constraint check
9471 Apply_Constraint_Check
(Operand
, Target_Type
, No_Sliding
=> True);
9473 if Do_Range_Check
(Operand
) then
9474 Set_Do_Range_Check
(Operand
, False);
9475 Generate_Range_Check
(Operand
, Target_Type
, CE_Range_Check_Failed
);
9477 end Expand_N_Qualified_Expression
;
9479 ------------------------------------
9480 -- Expand_N_Quantified_Expression --
9481 ------------------------------------
9485 -- for all X in range => Cond
9490 -- for X in range loop
9497 -- Similarly, an existentially quantified expression:
9499 -- for some X in range => Cond
9504 -- for X in range loop
9511 -- In both cases, the iteration may be over a container in which case it is
9512 -- given by an iterator specification, not a loop parameter specification.
9514 procedure Expand_N_Quantified_Expression
(N
: Node_Id
) is
9515 Actions
: constant List_Id
:= New_List
;
9516 For_All
: constant Boolean := All_Present
(N
);
9517 Iter_Spec
: constant Node_Id
:= Iterator_Specification
(N
);
9518 Loc
: constant Source_Ptr
:= Sloc
(N
);
9519 Loop_Spec
: constant Node_Id
:= Loop_Parameter_Specification
(N
);
9526 -- Create the declaration of the flag which tracks the status of the
9527 -- quantified expression. Generate:
9529 -- Flag : Boolean := (True | False);
9531 Flag
:= Make_Temporary
(Loc
, 'T', N
);
9534 Make_Object_Declaration
(Loc
,
9535 Defining_Identifier
=> Flag
,
9536 Object_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
),
9538 New_Occurrence_Of
(Boolean_Literals
(For_All
), Loc
)));
9540 -- Construct the circuitry which tracks the status of the quantified
9541 -- expression. Generate:
9543 -- if [not] Cond then
9544 -- Flag := (False | True);
9548 Cond
:= Relocate_Node
(Condition
(N
));
9551 Cond
:= Make_Op_Not
(Loc
, Cond
);
9555 Make_Implicit_If_Statement
(N
,
9557 Then_Statements
=> New_List
(
9558 Make_Assignment_Statement
(Loc
,
9559 Name
=> New_Occurrence_Of
(Flag
, Loc
),
9561 New_Occurrence_Of
(Boolean_Literals
(not For_All
), Loc
)),
9562 Make_Exit_Statement
(Loc
))));
9564 -- Build the loop equivalent of the quantified expression
9566 if Present
(Iter_Spec
) then
9568 Make_Iteration_Scheme
(Loc
,
9569 Iterator_Specification
=> Iter_Spec
);
9572 Make_Iteration_Scheme
(Loc
,
9573 Loop_Parameter_Specification
=> Loop_Spec
);
9577 Make_Loop_Statement
(Loc
,
9578 Iteration_Scheme
=> Scheme
,
9579 Statements
=> Stmts
,
9580 End_Label
=> Empty
));
9582 -- Transform the quantified expression
9585 Make_Expression_With_Actions
(Loc
,
9586 Expression
=> New_Occurrence_Of
(Flag
, Loc
),
9587 Actions
=> Actions
));
9588 Analyze_And_Resolve
(N
, Standard_Boolean
);
9589 end Expand_N_Quantified_Expression
;
9591 ---------------------------------
9592 -- Expand_N_Selected_Component --
9593 ---------------------------------
9595 procedure Expand_N_Selected_Component
(N
: Node_Id
) is
9596 Loc
: constant Source_Ptr
:= Sloc
(N
);
9597 Par
: constant Node_Id
:= Parent
(N
);
9598 P
: constant Node_Id
:= Prefix
(N
);
9599 S
: constant Node_Id
:= Selector_Name
(N
);
9600 Ptyp
: Entity_Id
:= Underlying_Type
(Etype
(P
));
9606 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean;
9607 -- Gigi needs a temporary for prefixes that depend on a discriminant,
9608 -- unless the context of an assignment can provide size information.
9609 -- Don't we have a general routine that does this???
9611 function Is_Subtype_Declaration
return Boolean;
9612 -- The replacement of a discriminant reference by its value is required
9613 -- if this is part of the initialization of an temporary generated by a
9614 -- change of representation. This shows up as the construction of a
9615 -- discriminant constraint for a subtype declared at the same point as
9616 -- the entity in the prefix of the selected component. We recognize this
9617 -- case when the context of the reference is:
9618 -- subtype ST is T(Obj.D);
9619 -- where the entity for Obj comes from source, and ST has the same sloc.
9621 -----------------------
9622 -- In_Left_Hand_Side --
9623 -----------------------
9625 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean is
9627 return (Nkind
(Parent
(Comp
)) = N_Assignment_Statement
9628 and then Comp
= Name
(Parent
(Comp
)))
9629 or else (Present
(Parent
(Comp
))
9630 and then Nkind
(Parent
(Comp
)) in N_Subexpr
9631 and then In_Left_Hand_Side
(Parent
(Comp
)));
9632 end In_Left_Hand_Side
;
9634 -----------------------------
9635 -- Is_Subtype_Declaration --
9636 -----------------------------
9638 function Is_Subtype_Declaration
return Boolean is
9639 Par
: constant Node_Id
:= Parent
(N
);
9642 Nkind
(Par
) = N_Index_Or_Discriminant_Constraint
9643 and then Nkind
(Parent
(Parent
(Par
))) = N_Subtype_Declaration
9644 and then Comes_From_Source
(Entity
(Prefix
(N
)))
9645 and then Sloc
(Par
) = Sloc
(Entity
(Prefix
(N
)));
9646 end Is_Subtype_Declaration
;
9648 -- Start of processing for Expand_N_Selected_Component
9651 -- Insert explicit dereference if required
9653 if Is_Access_Type
(Ptyp
) then
9655 -- First set prefix type to proper access type, in case it currently
9656 -- has a private (non-access) view of this type.
9658 Set_Etype
(P
, Ptyp
);
9660 Insert_Explicit_Dereference
(P
);
9661 Analyze_And_Resolve
(P
, Designated_Type
(Ptyp
));
9663 if Ekind
(Etype
(P
)) = E_Private_Subtype
9664 and then Is_For_Access_Subtype
(Etype
(P
))
9666 Set_Etype
(P
, Base_Type
(Etype
(P
)));
9672 -- Deal with discriminant check required
9674 if Do_Discriminant_Check
(N
) then
9675 if Present
(Discriminant_Checking_Func
9676 (Original_Record_Component
(Entity
(S
))))
9678 -- Present the discriminant checking function to the backend, so
9679 -- that it can inline the call to the function.
9682 (Discriminant_Checking_Func
9683 (Original_Record_Component
(Entity
(S
))),
9686 -- Now reset the flag and generate the call
9688 Set_Do_Discriminant_Check
(N
, False);
9689 Generate_Discriminant_Check
(N
);
9691 -- In the case of Unchecked_Union, no discriminant checking is
9692 -- actually performed.
9695 Set_Do_Discriminant_Check
(N
, False);
9699 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9700 -- function, then additional actuals must be passed.
9702 if Ada_Version
>= Ada_2005
9703 and then Is_Build_In_Place_Function_Call
(P
)
9705 Make_Build_In_Place_Call_In_Anonymous_Context
(P
);
9708 -- Gigi cannot handle unchecked conversions that are the prefix of a
9709 -- selected component with discriminants. This must be checked during
9710 -- expansion, because during analysis the type of the selector is not
9711 -- known at the point the prefix is analyzed. If the conversion is the
9712 -- target of an assignment, then we cannot force the evaluation.
9714 if Nkind
(Prefix
(N
)) = N_Unchecked_Type_Conversion
9715 and then Has_Discriminants
(Etype
(N
))
9716 and then not In_Left_Hand_Side
(N
)
9718 Force_Evaluation
(Prefix
(N
));
9721 -- Remaining processing applies only if selector is a discriminant
9723 if Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
then
9725 -- If the selector is a discriminant of a constrained record type,
9726 -- we may be able to rewrite the expression with the actual value
9727 -- of the discriminant, a useful optimization in some cases.
9729 if Is_Record_Type
(Ptyp
)
9730 and then Has_Discriminants
(Ptyp
)
9731 and then Is_Constrained
(Ptyp
)
9733 -- Do this optimization for discrete types only, and not for
9734 -- access types (access discriminants get us into trouble).
9736 if not Is_Discrete_Type
(Etype
(N
)) then
9739 -- Don't do this on the left hand of an assignment statement.
9740 -- Normally one would think that references like this would not
9741 -- occur, but they do in generated code, and mean that we really
9742 -- do want to assign the discriminant.
9744 elsif Nkind
(Par
) = N_Assignment_Statement
9745 and then Name
(Par
) = N
9749 -- Don't do this optimization for the prefix of an attribute or
9750 -- the name of an object renaming declaration since these are
9751 -- contexts where we do not want the value anyway.
9753 elsif (Nkind
(Par
) = N_Attribute_Reference
9754 and then Prefix
(Par
) = N
)
9755 or else Is_Renamed_Object
(N
)
9759 -- Don't do this optimization if we are within the code for a
9760 -- discriminant check, since the whole point of such a check may
9761 -- be to verify the condition on which the code below depends.
9763 elsif Is_In_Discriminant_Check
(N
) then
9766 -- Green light to see if we can do the optimization. There is
9767 -- still one condition that inhibits the optimization below but
9768 -- now is the time to check the particular discriminant.
9771 -- Loop through discriminants to find the matching discriminant
9772 -- constraint to see if we can copy it.
9774 Disc
:= First_Discriminant
(Ptyp
);
9775 Dcon
:= First_Elmt
(Discriminant_Constraint
(Ptyp
));
9776 Discr_Loop
: while Present
(Dcon
) loop
9777 Dval
:= Node
(Dcon
);
9779 -- Check if this is the matching discriminant and if the
9780 -- discriminant value is simple enough to make sense to
9781 -- copy. We don't want to copy complex expressions, and
9782 -- indeed to do so can cause trouble (before we put in
9783 -- this guard, a discriminant expression containing an
9784 -- AND THEN was copied, causing problems for coverage
9787 -- However, if the reference is part of the initialization
9788 -- code generated for an object declaration, we must use
9789 -- the discriminant value from the subtype constraint,
9790 -- because the selected component may be a reference to the
9791 -- object being initialized, whose discriminant is not yet
9792 -- set. This only happens in complex cases involving changes
9793 -- or representation.
9795 if Disc
= Entity
(Selector_Name
(N
))
9796 and then (Is_Entity_Name
(Dval
)
9797 or else Compile_Time_Known_Value
(Dval
)
9798 or else Is_Subtype_Declaration
)
9800 -- Here we have the matching discriminant. Check for
9801 -- the case of a discriminant of a component that is
9802 -- constrained by an outer discriminant, which cannot
9803 -- be optimized away.
9805 if Denotes_Discriminant
9806 (Dval
, Check_Concurrent
=> True)
9810 elsif Nkind
(Original_Node
(Dval
)) = N_Selected_Component
9812 Denotes_Discriminant
9813 (Selector_Name
(Original_Node
(Dval
)), True)
9817 -- Do not retrieve value if constraint is not static. It
9818 -- is generally not useful, and the constraint may be a
9819 -- rewritten outer discriminant in which case it is in
9822 elsif Is_Entity_Name
(Dval
)
9824 Nkind
(Parent
(Entity
(Dval
))) = N_Object_Declaration
9825 and then Present
(Expression
(Parent
(Entity
(Dval
))))
9827 Is_OK_Static_Expression
9828 (Expression
(Parent
(Entity
(Dval
))))
9832 -- In the context of a case statement, the expression may
9833 -- have the base type of the discriminant, and we need to
9834 -- preserve the constraint to avoid spurious errors on
9837 elsif Nkind
(Parent
(N
)) = N_Case_Statement
9838 and then Etype
(Dval
) /= Etype
(Disc
)
9841 Make_Qualified_Expression
(Loc
,
9843 New_Occurrence_Of
(Etype
(Disc
), Loc
),
9845 New_Copy_Tree
(Dval
)));
9846 Analyze_And_Resolve
(N
, Etype
(Disc
));
9848 -- In case that comes out as a static expression,
9849 -- reset it (a selected component is never static).
9851 Set_Is_Static_Expression
(N
, False);
9854 -- Otherwise we can just copy the constraint, but the
9855 -- result is certainly not static. In some cases the
9856 -- discriminant constraint has been analyzed in the
9857 -- context of the original subtype indication, but for
9858 -- itypes the constraint might not have been analyzed
9859 -- yet, and this must be done now.
9862 Rewrite
(N
, New_Copy_Tree
(Dval
));
9863 Analyze_And_Resolve
(N
);
9864 Set_Is_Static_Expression
(N
, False);
9870 Next_Discriminant
(Disc
);
9871 end loop Discr_Loop
;
9873 -- Note: the above loop should always find a matching
9874 -- discriminant, but if it does not, we just missed an
9875 -- optimization due to some glitch (perhaps a previous
9876 -- error), so ignore.
9881 -- The only remaining processing is in the case of a discriminant of
9882 -- a concurrent object, where we rewrite the prefix to denote the
9883 -- corresponding record type. If the type is derived and has renamed
9884 -- discriminants, use corresponding discriminant, which is the one
9885 -- that appears in the corresponding record.
9887 if not Is_Concurrent_Type
(Ptyp
) then
9891 Disc
:= Entity
(Selector_Name
(N
));
9893 if Is_Derived_Type
(Ptyp
)
9894 and then Present
(Corresponding_Discriminant
(Disc
))
9896 Disc
:= Corresponding_Discriminant
(Disc
);
9900 Make_Selected_Component
(Loc
,
9902 Unchecked_Convert_To
(Corresponding_Record_Type
(Ptyp
),
9904 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Disc
)));
9910 -- Set Atomic_Sync_Required if necessary for atomic component
9912 if Nkind
(N
) = N_Selected_Component
then
9914 E
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
9918 -- If component is atomic, but type is not, setting depends on
9919 -- disable/enable state for the component.
9921 if Is_Atomic
(E
) and then not Is_Atomic
(Etype
(E
)) then
9922 Set
:= not Atomic_Synchronization_Disabled
(E
);
9924 -- If component is not atomic, but its type is atomic, setting
9925 -- depends on disable/enable state for the type.
9927 elsif not Is_Atomic
(E
) and then Is_Atomic
(Etype
(E
)) then
9928 Set
:= not Atomic_Synchronization_Disabled
(Etype
(E
));
9930 -- If both component and type are atomic, we disable if either
9931 -- component or its type have sync disabled.
9933 elsif Is_Atomic
(E
) and then Is_Atomic
(Etype
(E
)) then
9934 Set
:= (not Atomic_Synchronization_Disabled
(E
))
9936 (not Atomic_Synchronization_Disabled
(Etype
(E
)));
9942 -- Set flag if required
9945 Activate_Atomic_Synchronization
(N
);
9949 end Expand_N_Selected_Component
;
9951 --------------------
9952 -- Expand_N_Slice --
9953 --------------------
9955 procedure Expand_N_Slice
(N
: Node_Id
) is
9956 Loc
: constant Source_Ptr
:= Sloc
(N
);
9957 Typ
: constant Entity_Id
:= Etype
(N
);
9959 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean;
9960 -- Check whether the argument is an actual for a procedure call, in
9961 -- which case the expansion of a bit-packed slice is deferred until the
9962 -- call itself is expanded. The reason this is required is that we might
9963 -- have an IN OUT or OUT parameter, and the copy out is essential, and
9964 -- that copy out would be missed if we created a temporary here in
9965 -- Expand_N_Slice. Note that we don't bother to test specifically for an
9966 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
9967 -- is harmless to defer expansion in the IN case, since the call
9968 -- processing will still generate the appropriate copy in operation,
9969 -- which will take care of the slice.
9971 procedure Make_Temporary_For_Slice
;
9972 -- Create a named variable for the value of the slice, in cases where
9973 -- the back-end cannot handle it properly, e.g. when packed types or
9974 -- unaligned slices are involved.
9976 -------------------------
9977 -- Is_Procedure_Actual --
9978 -------------------------
9980 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean is
9981 Par
: Node_Id
:= Parent
(N
);
9985 -- If our parent is a procedure call we can return
9987 if Nkind
(Par
) = N_Procedure_Call_Statement
then
9990 -- If our parent is a type conversion, keep climbing the tree,
9991 -- since a type conversion can be a procedure actual. Also keep
9992 -- climbing if parameter association or a qualified expression,
9993 -- since these are additional cases that do can appear on
9994 -- procedure actuals.
9996 elsif Nkind_In
(Par
, N_Type_Conversion
,
9997 N_Parameter_Association
,
9998 N_Qualified_Expression
)
10000 Par
:= Parent
(Par
);
10002 -- Any other case is not what we are looking for
10008 end Is_Procedure_Actual
;
10010 ------------------------------
10011 -- Make_Temporary_For_Slice --
10012 ------------------------------
10014 procedure Make_Temporary_For_Slice
is
10015 Ent
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', N
);
10020 Make_Object_Declaration
(Loc
,
10021 Defining_Identifier
=> Ent
,
10022 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
10024 Set_No_Initialization
(Decl
);
10026 Insert_Actions
(N
, New_List
(
10028 Make_Assignment_Statement
(Loc
,
10029 Name
=> New_Occurrence_Of
(Ent
, Loc
),
10030 Expression
=> Relocate_Node
(N
))));
10032 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
10033 Analyze_And_Resolve
(N
, Typ
);
10034 end Make_Temporary_For_Slice
;
10038 Pref
: constant Node_Id
:= Prefix
(N
);
10039 Pref_Typ
: Entity_Id
:= Etype
(Pref
);
10041 -- Start of processing for Expand_N_Slice
10044 -- Special handling for access types
10046 if Is_Access_Type
(Pref_Typ
) then
10047 Pref_Typ
:= Designated_Type
(Pref_Typ
);
10050 Make_Explicit_Dereference
(Sloc
(N
),
10051 Prefix
=> Relocate_Node
(Pref
)));
10053 Analyze_And_Resolve
(Pref
, Pref_Typ
);
10056 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
10057 -- function, then additional actuals must be passed.
10059 if Ada_Version
>= Ada_2005
10060 and then Is_Build_In_Place_Function_Call
(Pref
)
10062 Make_Build_In_Place_Call_In_Anonymous_Context
(Pref
);
10065 -- The remaining case to be handled is packed slices. We can leave
10066 -- packed slices as they are in the following situations:
10068 -- 1. Right or left side of an assignment (we can handle this
10069 -- situation correctly in the assignment statement expansion).
10071 -- 2. Prefix of indexed component (the slide is optimized away in this
10072 -- case, see the start of Expand_N_Slice.)
10074 -- 3. Object renaming declaration, since we want the name of the
10075 -- slice, not the value.
10077 -- 4. Argument to procedure call, since copy-in/copy-out handling may
10078 -- be required, and this is handled in the expansion of call
10081 -- 5. Prefix of an address attribute (this is an error which is caught
10082 -- elsewhere, and the expansion would interfere with generating the
10085 if not Is_Packed
(Typ
) then
10087 -- Apply transformation for actuals of a function call, where
10088 -- Expand_Actuals is not used.
10090 if Nkind
(Parent
(N
)) = N_Function_Call
10091 and then Is_Possibly_Unaligned_Slice
(N
)
10093 Make_Temporary_For_Slice
;
10096 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
10097 or else (Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
10098 and then Parent
(N
) = Name
(Parent
(Parent
(N
))))
10102 elsif Nkind
(Parent
(N
)) = N_Indexed_Component
10103 or else Is_Renamed_Object
(N
)
10104 or else Is_Procedure_Actual
(N
)
10108 elsif Nkind
(Parent
(N
)) = N_Attribute_Reference
10109 and then Attribute_Name
(Parent
(N
)) = Name_Address
10114 Make_Temporary_For_Slice
;
10116 end Expand_N_Slice
;
10118 ------------------------------
10119 -- Expand_N_Type_Conversion --
10120 ------------------------------
10122 procedure Expand_N_Type_Conversion
(N
: Node_Id
) is
10123 Loc
: constant Source_Ptr
:= Sloc
(N
);
10124 Operand
: constant Node_Id
:= Expression
(N
);
10125 Target_Type
: constant Entity_Id
:= Etype
(N
);
10126 Operand_Type
: Entity_Id
:= Etype
(Operand
);
10128 procedure Handle_Changed_Representation
;
10129 -- This is called in the case of record and array type conversions to
10130 -- see if there is a change of representation to be handled. Change of
10131 -- representation is actually handled at the assignment statement level,
10132 -- and what this procedure does is rewrite node N conversion as an
10133 -- assignment to temporary. If there is no change of representation,
10134 -- then the conversion node is unchanged.
10136 procedure Raise_Accessibility_Error
;
10137 -- Called when we know that an accessibility check will fail. Rewrites
10138 -- node N to an appropriate raise statement and outputs warning msgs.
10139 -- The Etype of the raise node is set to Target_Type. Note that in this
10140 -- case the rest of the processing should be skipped (i.e. the call to
10141 -- this procedure will be followed by "goto Done").
10143 procedure Real_Range_Check
;
10144 -- Handles generation of range check for real target value
10146 function Has_Extra_Accessibility
(Id
: Entity_Id
) return Boolean;
10147 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
10148 -- evaluates to True.
10150 -----------------------------------
10151 -- Handle_Changed_Representation --
10152 -----------------------------------
10154 procedure Handle_Changed_Representation
is
10163 -- Nothing else to do if no change of representation
10165 if Same_Representation
(Operand_Type
, Target_Type
) then
10168 -- The real change of representation work is done by the assignment
10169 -- statement processing. So if this type conversion is appearing as
10170 -- the expression of an assignment statement, nothing needs to be
10171 -- done to the conversion.
10173 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
then
10176 -- Otherwise we need to generate a temporary variable, and do the
10177 -- change of representation assignment into that temporary variable.
10178 -- The conversion is then replaced by a reference to this variable.
10183 -- If type is unconstrained we have to add a constraint, copied
10184 -- from the actual value of the left hand side.
10186 if not Is_Constrained
(Target_Type
) then
10187 if Has_Discriminants
(Operand_Type
) then
10188 Disc
:= First_Discriminant
(Operand_Type
);
10190 if Disc
/= First_Stored_Discriminant
(Operand_Type
) then
10191 Disc
:= First_Stored_Discriminant
(Operand_Type
);
10195 while Present
(Disc
) loop
10197 Make_Selected_Component
(Loc
,
10199 Duplicate_Subexpr_Move_Checks
(Operand
),
10201 Make_Identifier
(Loc
, Chars
(Disc
))));
10202 Next_Discriminant
(Disc
);
10205 elsif Is_Array_Type
(Operand_Type
) then
10206 N_Ix
:= First_Index
(Target_Type
);
10209 for J
in 1 .. Number_Dimensions
(Operand_Type
) loop
10211 -- We convert the bounds explicitly. We use an unchecked
10212 -- conversion because bounds checks are done elsewhere.
10217 Unchecked_Convert_To
(Etype
(N_Ix
),
10218 Make_Attribute_Reference
(Loc
,
10220 Duplicate_Subexpr_No_Checks
10221 (Operand
, Name_Req
=> True),
10222 Attribute_Name
=> Name_First
,
10223 Expressions
=> New_List
(
10224 Make_Integer_Literal
(Loc
, J
)))),
10227 Unchecked_Convert_To
(Etype
(N_Ix
),
10228 Make_Attribute_Reference
(Loc
,
10230 Duplicate_Subexpr_No_Checks
10231 (Operand
, Name_Req
=> True),
10232 Attribute_Name
=> Name_Last
,
10233 Expressions
=> New_List
(
10234 Make_Integer_Literal
(Loc
, J
))))));
10241 Odef
:= New_Occurrence_Of
(Target_Type
, Loc
);
10243 if Present
(Cons
) then
10245 Make_Subtype_Indication
(Loc
,
10246 Subtype_Mark
=> Odef
,
10248 Make_Index_Or_Discriminant_Constraint
(Loc
,
10249 Constraints
=> Cons
));
10252 Temp
:= Make_Temporary
(Loc
, 'C');
10254 Make_Object_Declaration
(Loc
,
10255 Defining_Identifier
=> Temp
,
10256 Object_Definition
=> Odef
);
10258 Set_No_Initialization
(Decl
, True);
10260 -- Insert required actions. It is essential to suppress checks
10261 -- since we have suppressed default initialization, which means
10262 -- that the variable we create may have no discriminants.
10267 Make_Assignment_Statement
(Loc
,
10268 Name
=> New_Occurrence_Of
(Temp
, Loc
),
10269 Expression
=> Relocate_Node
(N
))),
10270 Suppress
=> All_Checks
);
10272 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
10275 end Handle_Changed_Representation
;
10277 -------------------------------
10278 -- Raise_Accessibility_Error --
10279 -------------------------------
10281 procedure Raise_Accessibility_Error
is
10283 Error_Msg_Warn
:= SPARK_Mode
/= On
;
10285 Make_Raise_Program_Error
(Sloc
(N
),
10286 Reason
=> PE_Accessibility_Check_Failed
));
10287 Set_Etype
(N
, Target_Type
);
10289 Error_Msg_N
("<<accessibility check failure", N
);
10290 Error_Msg_NE
("\<<& [", N
, Standard_Program_Error
);
10291 end Raise_Accessibility_Error
;
10293 ----------------------
10294 -- Real_Range_Check --
10295 ----------------------
10297 -- Case of conversions to floating-point or fixed-point. If range checks
10298 -- are enabled and the target type has a range constraint, we convert:
10304 -- Tnn : typ'Base := typ'Base (x);
10305 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
10308 -- This is necessary when there is a conversion of integer to float or
10309 -- to fixed-point to ensure that the correct checks are made. It is not
10310 -- necessary for float to float where it is enough to simply set the
10311 -- Do_Range_Check flag.
10313 procedure Real_Range_Check
is
10314 Btyp
: constant Entity_Id
:= Base_Type
(Target_Type
);
10315 Lo
: constant Node_Id
:= Type_Low_Bound
(Target_Type
);
10316 Hi
: constant Node_Id
:= Type_High_Bound
(Target_Type
);
10317 Xtyp
: constant Entity_Id
:= Etype
(Operand
);
10322 -- Nothing to do if conversion was rewritten
10324 if Nkind
(N
) /= N_Type_Conversion
then
10328 -- Nothing to do if range checks suppressed, or target has the same
10329 -- range as the base type (or is the base type).
10331 if Range_Checks_Suppressed
(Target_Type
)
10332 or else (Lo
= Type_Low_Bound
(Btyp
)
10334 Hi
= Type_High_Bound
(Btyp
))
10339 -- Nothing to do if expression is an entity on which checks have been
10342 if Is_Entity_Name
(Operand
)
10343 and then Range_Checks_Suppressed
(Entity
(Operand
))
10348 -- Nothing to do if bounds are all static and we can tell that the
10349 -- expression is within the bounds of the target. Note that if the
10350 -- operand is of an unconstrained floating-point type, then we do
10351 -- not trust it to be in range (might be infinite)
10354 S_Lo
: constant Node_Id
:= Type_Low_Bound
(Xtyp
);
10355 S_Hi
: constant Node_Id
:= Type_High_Bound
(Xtyp
);
10358 if (not Is_Floating_Point_Type
(Xtyp
)
10359 or else Is_Constrained
(Xtyp
))
10360 and then Compile_Time_Known_Value
(S_Lo
)
10361 and then Compile_Time_Known_Value
(S_Hi
)
10362 and then Compile_Time_Known_Value
(Hi
)
10363 and then Compile_Time_Known_Value
(Lo
)
10366 D_Lov
: constant Ureal
:= Expr_Value_R
(Lo
);
10367 D_Hiv
: constant Ureal
:= Expr_Value_R
(Hi
);
10372 if Is_Real_Type
(Xtyp
) then
10373 S_Lov
:= Expr_Value_R
(S_Lo
);
10374 S_Hiv
:= Expr_Value_R
(S_Hi
);
10376 S_Lov
:= UR_From_Uint
(Expr_Value
(S_Lo
));
10377 S_Hiv
:= UR_From_Uint
(Expr_Value
(S_Hi
));
10381 and then S_Lov
>= D_Lov
10382 and then S_Hiv
<= D_Hiv
10384 -- Unset the range check flag on the current value of
10385 -- Expression (N), since the captured Operand may have
10386 -- been rewritten (such as for the case of a conversion
10387 -- to a fixed-point type).
10389 Set_Do_Range_Check
(Expression
(N
), False);
10397 -- For float to float conversions, we are done
10399 if Is_Floating_Point_Type
(Xtyp
)
10401 Is_Floating_Point_Type
(Btyp
)
10406 -- Otherwise rewrite the conversion as described above
10408 Conv
:= Relocate_Node
(N
);
10409 Rewrite
(Subtype_Mark
(Conv
), New_Occurrence_Of
(Btyp
, Loc
));
10410 Set_Etype
(Conv
, Btyp
);
10412 -- Enable overflow except for case of integer to float conversions,
10413 -- where it is never required, since we can never have overflow in
10416 if not Is_Integer_Type
(Etype
(Operand
)) then
10417 Enable_Overflow_Check
(Conv
);
10420 Tnn
:= Make_Temporary
(Loc
, 'T', Conv
);
10422 Insert_Actions
(N
, New_List
(
10423 Make_Object_Declaration
(Loc
,
10424 Defining_Identifier
=> Tnn
,
10425 Object_Definition
=> New_Occurrence_Of
(Btyp
, Loc
),
10426 Constant_Present
=> True,
10427 Expression
=> Conv
),
10429 Make_Raise_Constraint_Error
(Loc
,
10434 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
10436 Make_Attribute_Reference
(Loc
,
10437 Attribute_Name
=> Name_First
,
10439 New_Occurrence_Of
(Target_Type
, Loc
))),
10443 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
10445 Make_Attribute_Reference
(Loc
,
10446 Attribute_Name
=> Name_Last
,
10448 New_Occurrence_Of
(Target_Type
, Loc
)))),
10449 Reason
=> CE_Range_Check_Failed
)));
10451 Rewrite
(N
, New_Occurrence_Of
(Tnn
, Loc
));
10452 Analyze_And_Resolve
(N
, Btyp
);
10453 end Real_Range_Check
;
10455 -----------------------------
10456 -- Has_Extra_Accessibility --
10457 -----------------------------
10459 -- Returns true for a formal of an anonymous access type or for
10460 -- an Ada 2012-style stand-alone object of an anonymous access type.
10462 function Has_Extra_Accessibility
(Id
: Entity_Id
) return Boolean is
10464 if Is_Formal
(Id
) or else Ekind_In
(Id
, E_Constant
, E_Variable
) then
10465 return Present
(Effective_Extra_Accessibility
(Id
));
10469 end Has_Extra_Accessibility
;
10471 -- Start of processing for Expand_N_Type_Conversion
10474 -- First remove check marks put by the semantic analysis on the type
10475 -- conversion between array types. We need these checks, and they will
10476 -- be generated by this expansion routine, but we do not depend on these
10477 -- flags being set, and since we do intend to expand the checks in the
10478 -- front end, we don't want them on the tree passed to the back end.
10480 if Is_Array_Type
(Target_Type
) then
10481 if Is_Constrained
(Target_Type
) then
10482 Set_Do_Length_Check
(N
, False);
10484 Set_Do_Range_Check
(Operand
, False);
10488 -- Nothing at all to do if conversion is to the identical type so remove
10489 -- the conversion completely, it is useless, except that it may carry
10490 -- an Assignment_OK attribute, which must be propagated to the operand.
10492 if Operand_Type
= Target_Type
then
10493 if Assignment_OK
(N
) then
10494 Set_Assignment_OK
(Operand
);
10497 Rewrite
(N
, Relocate_Node
(Operand
));
10501 -- Nothing to do if this is the second argument of read. This is a
10502 -- "backwards" conversion that will be handled by the specialized code
10503 -- in attribute processing.
10505 if Nkind
(Parent
(N
)) = N_Attribute_Reference
10506 and then Attribute_Name
(Parent
(N
)) = Name_Read
10507 and then Next
(First
(Expressions
(Parent
(N
)))) = N
10512 -- Check for case of converting to a type that has an invariant
10513 -- associated with it. This required an invariant check. We convert
10519 -- do invariant_check (typ (expr)) in typ (expr);
10521 -- using Duplicate_Subexpr to avoid multiple side effects
10523 -- Note: the Comes_From_Source check, and then the resetting of this
10524 -- flag prevents what would otherwise be an infinite recursion.
10526 if Has_Invariants
(Target_Type
)
10527 and then Present
(Invariant_Procedure
(Target_Type
))
10528 and then Comes_From_Source
(N
)
10530 Set_Comes_From_Source
(N
, False);
10532 Make_Expression_With_Actions
(Loc
,
10533 Actions
=> New_List
(
10534 Make_Invariant_Call
(Duplicate_Subexpr
(N
))),
10535 Expression
=> Duplicate_Subexpr_No_Checks
(N
)));
10536 Analyze_And_Resolve
(N
, Target_Type
);
10540 -- Here if we may need to expand conversion
10542 -- If the operand of the type conversion is an arithmetic operation on
10543 -- signed integers, and the based type of the signed integer type in
10544 -- question is smaller than Standard.Integer, we promote both of the
10545 -- operands to type Integer.
10547 -- For example, if we have
10549 -- target-type (opnd1 + opnd2)
10551 -- and opnd1 and opnd2 are of type short integer, then we rewrite
10554 -- target-type (integer(opnd1) + integer(opnd2))
10556 -- We do this because we are always allowed to compute in a larger type
10557 -- if we do the right thing with the result, and in this case we are
10558 -- going to do a conversion which will do an appropriate check to make
10559 -- sure that things are in range of the target type in any case. This
10560 -- avoids some unnecessary intermediate overflows.
10562 -- We might consider a similar transformation in the case where the
10563 -- target is a real type or a 64-bit integer type, and the operand
10564 -- is an arithmetic operation using a 32-bit integer type. However,
10565 -- we do not bother with this case, because it could cause significant
10566 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
10567 -- much cheaper, but we don't want different behavior on 32-bit and
10568 -- 64-bit machines. Note that the exclusion of the 64-bit case also
10569 -- handles the configurable run-time cases where 64-bit arithmetic
10570 -- may simply be unavailable.
10572 -- Note: this circuit is partially redundant with respect to the circuit
10573 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
10574 -- the processing here. Also we still need the Checks circuit, since we
10575 -- have to be sure not to generate junk overflow checks in the first
10576 -- place, since it would be trick to remove them here.
10578 if Integer_Promotion_Possible
(N
) then
10580 -- All conditions met, go ahead with transformation
10588 Make_Type_Conversion
(Loc
,
10589 Subtype_Mark
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
10590 Expression
=> Relocate_Node
(Right_Opnd
(Operand
)));
10592 Opnd
:= New_Op_Node
(Nkind
(Operand
), Loc
);
10593 Set_Right_Opnd
(Opnd
, R
);
10595 if Nkind
(Operand
) in N_Binary_Op
then
10597 Make_Type_Conversion
(Loc
,
10598 Subtype_Mark
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
10599 Expression
=> Relocate_Node
(Left_Opnd
(Operand
)));
10601 Set_Left_Opnd
(Opnd
, L
);
10605 Make_Type_Conversion
(Loc
,
10606 Subtype_Mark
=> Relocate_Node
(Subtype_Mark
(N
)),
10607 Expression
=> Opnd
));
10609 Analyze_And_Resolve
(N
, Target_Type
);
10614 -- Do validity check if validity checking operands
10616 if Validity_Checks_On
and Validity_Check_Operands
then
10617 Ensure_Valid
(Operand
);
10620 -- Special case of converting from non-standard boolean type
10622 if Is_Boolean_Type
(Operand_Type
)
10623 and then (Nonzero_Is_True
(Operand_Type
))
10625 Adjust_Condition
(Operand
);
10626 Set_Etype
(Operand
, Standard_Boolean
);
10627 Operand_Type
:= Standard_Boolean
;
10630 -- Case of converting to an access type
10632 if Is_Access_Type
(Target_Type
) then
10634 -- Apply an accessibility check when the conversion operand is an
10635 -- access parameter (or a renaming thereof), unless conversion was
10636 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
10637 -- Note that other checks may still need to be applied below (such
10638 -- as tagged type checks).
10640 if Is_Entity_Name
(Operand
)
10641 and then Has_Extra_Accessibility
(Entity
(Operand
))
10642 and then Ekind
(Etype
(Operand
)) = E_Anonymous_Access_Type
10643 and then (Nkind
(Original_Node
(N
)) /= N_Attribute_Reference
10644 or else Attribute_Name
(Original_Node
(N
)) = Name_Access
)
10646 Apply_Accessibility_Check
10647 (Operand
, Target_Type
, Insert_Node
=> Operand
);
10649 -- If the level of the operand type is statically deeper than the
10650 -- level of the target type, then force Program_Error. Note that this
10651 -- can only occur for cases where the attribute is within the body of
10652 -- an instantiation, otherwise the conversion will already have been
10653 -- rejected as illegal.
10655 -- Note: warnings are issued by the analyzer for the instance cases
10657 elsif In_Instance_Body
10659 -- The case where the target type is an anonymous access type of
10660 -- a discriminant is excluded, because the level of such a type
10661 -- depends on the context and currently the level returned for such
10662 -- types is zero, resulting in warnings about about check failures
10663 -- in certain legal cases involving class-wide interfaces as the
10664 -- designated type (some cases, such as return statements, are
10665 -- checked at run time, but not clear if these are handled right
10666 -- in general, see 3.10.2(12/2-12.5/3) ???).
10669 not (Ekind
(Target_Type
) = E_Anonymous_Access_Type
10670 and then Present
(Associated_Node_For_Itype
(Target_Type
))
10671 and then Nkind
(Associated_Node_For_Itype
(Target_Type
)) =
10672 N_Discriminant_Specification
)
10674 Type_Access_Level
(Operand_Type
) > Type_Access_Level
(Target_Type
)
10676 Raise_Accessibility_Error
;
10679 -- When the operand is a selected access discriminant the check needs
10680 -- to be made against the level of the object denoted by the prefix
10681 -- of the selected name. Force Program_Error for this case as well
10682 -- (this accessibility violation can only happen if within the body
10683 -- of an instantiation).
10685 elsif In_Instance_Body
10686 and then Ekind
(Operand_Type
) = E_Anonymous_Access_Type
10687 and then Nkind
(Operand
) = N_Selected_Component
10688 and then Object_Access_Level
(Operand
) >
10689 Type_Access_Level
(Target_Type
)
10691 Raise_Accessibility_Error
;
10696 -- Case of conversions of tagged types and access to tagged types
10698 -- When needed, that is to say when the expression is class-wide, Add
10699 -- runtime a tag check for (strict) downward conversion by using the
10700 -- membership test, generating:
10702 -- [constraint_error when Operand not in Target_Type'Class]
10704 -- or in the access type case
10706 -- [constraint_error
10707 -- when Operand /= null
10708 -- and then Operand.all not in
10709 -- Designated_Type (Target_Type)'Class]
10711 if (Is_Access_Type
(Target_Type
)
10712 and then Is_Tagged_Type
(Designated_Type
(Target_Type
)))
10713 or else Is_Tagged_Type
(Target_Type
)
10715 -- Do not do any expansion in the access type case if the parent is a
10716 -- renaming, since this is an error situation which will be caught by
10717 -- Sem_Ch8, and the expansion can interfere with this error check.
10719 if Is_Access_Type
(Target_Type
) and then Is_Renamed_Object
(N
) then
10723 -- Otherwise, proceed with processing tagged conversion
10725 Tagged_Conversion
: declare
10726 Actual_Op_Typ
: Entity_Id
;
10727 Actual_Targ_Typ
: Entity_Id
;
10728 Make_Conversion
: Boolean := False;
10729 Root_Op_Typ
: Entity_Id
;
10731 procedure Make_Tag_Check
(Targ_Typ
: Entity_Id
);
10732 -- Create a membership check to test whether Operand is a member
10733 -- of Targ_Typ. If the original Target_Type is an access, include
10734 -- a test for null value. The check is inserted at N.
10736 --------------------
10737 -- Make_Tag_Check --
10738 --------------------
10740 procedure Make_Tag_Check
(Targ_Typ
: Entity_Id
) is
10745 -- [Constraint_Error
10746 -- when Operand /= null
10747 -- and then Operand.all not in Targ_Typ]
10749 if Is_Access_Type
(Target_Type
) then
10751 Make_And_Then
(Loc
,
10754 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
10755 Right_Opnd
=> Make_Null
(Loc
)),
10760 Make_Explicit_Dereference
(Loc
,
10761 Prefix
=> Duplicate_Subexpr_No_Checks
(Operand
)),
10762 Right_Opnd
=> New_Occurrence_Of
(Targ_Typ
, Loc
)));
10765 -- [Constraint_Error when Operand not in Targ_Typ]
10770 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
10771 Right_Opnd
=> New_Occurrence_Of
(Targ_Typ
, Loc
));
10775 Make_Raise_Constraint_Error
(Loc
,
10777 Reason
=> CE_Tag_Check_Failed
));
10778 end Make_Tag_Check
;
10780 -- Start of processing for Tagged_Conversion
10783 -- Handle entities from the limited view
10785 if Is_Access_Type
(Operand_Type
) then
10787 Available_View
(Designated_Type
(Operand_Type
));
10789 Actual_Op_Typ
:= Operand_Type
;
10792 if Is_Access_Type
(Target_Type
) then
10794 Available_View
(Designated_Type
(Target_Type
));
10796 Actual_Targ_Typ
:= Target_Type
;
10799 Root_Op_Typ
:= Root_Type
(Actual_Op_Typ
);
10801 -- Ada 2005 (AI-251): Handle interface type conversion
10803 if Is_Interface
(Actual_Op_Typ
)
10805 Is_Interface
(Actual_Targ_Typ
)
10807 Expand_Interface_Conversion
(N
);
10811 if not Tag_Checks_Suppressed
(Actual_Targ_Typ
) then
10813 -- Create a runtime tag check for a downward class-wide type
10816 if Is_Class_Wide_Type
(Actual_Op_Typ
)
10817 and then Actual_Op_Typ
/= Actual_Targ_Typ
10818 and then Root_Op_Typ
/= Actual_Targ_Typ
10819 and then Is_Ancestor
(Root_Op_Typ
, Actual_Targ_Typ
,
10820 Use_Full_View
=> True)
10822 Make_Tag_Check
(Class_Wide_Type
(Actual_Targ_Typ
));
10823 Make_Conversion
:= True;
10826 -- AI05-0073: If the result subtype of the function is defined
10827 -- by an access_definition designating a specific tagged type
10828 -- T, a check is made that the result value is null or the tag
10829 -- of the object designated by the result value identifies T.
10830 -- Constraint_Error is raised if this check fails.
10832 if Nkind
(Parent
(N
)) = N_Simple_Return_Statement
then
10835 Func_Typ
: Entity_Id
;
10838 -- Climb scope stack looking for the enclosing function
10840 Func
:= Current_Scope
;
10841 while Present
(Func
)
10842 and then Ekind
(Func
) /= E_Function
10844 Func
:= Scope
(Func
);
10847 -- The function's return subtype must be defined using
10848 -- an access definition.
10850 if Nkind
(Result_Definition
(Parent
(Func
))) =
10851 N_Access_Definition
10853 Func_Typ
:= Directly_Designated_Type
(Etype
(Func
));
10855 -- The return subtype denotes a specific tagged type,
10856 -- in other words, a non class-wide type.
10858 if Is_Tagged_Type
(Func_Typ
)
10859 and then not Is_Class_Wide_Type
(Func_Typ
)
10861 Make_Tag_Check
(Actual_Targ_Typ
);
10862 Make_Conversion
:= True;
10868 -- We have generated a tag check for either a class-wide type
10869 -- conversion or for AI05-0073.
10871 if Make_Conversion
then
10876 Make_Unchecked_Type_Conversion
(Loc
,
10877 Subtype_Mark
=> New_Occurrence_Of
(Target_Type
, Loc
),
10878 Expression
=> Relocate_Node
(Expression
(N
)));
10880 Analyze_And_Resolve
(N
, Target_Type
);
10884 end Tagged_Conversion
;
10886 -- Case of other access type conversions
10888 elsif Is_Access_Type
(Target_Type
) then
10889 Apply_Constraint_Check
(Operand
, Target_Type
);
10891 -- Case of conversions from a fixed-point type
10893 -- These conversions require special expansion and processing, found in
10894 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
10895 -- since from a semantic point of view, these are simple integer
10896 -- conversions, which do not need further processing.
10898 elsif Is_Fixed_Point_Type
(Operand_Type
)
10899 and then not Conversion_OK
(N
)
10901 -- We should never see universal fixed at this case, since the
10902 -- expansion of the constituent divide or multiply should have
10903 -- eliminated the explicit mention of universal fixed.
10905 pragma Assert
(Operand_Type
/= Universal_Fixed
);
10907 -- Check for special case of the conversion to universal real that
10908 -- occurs as a result of the use of a round attribute. In this case,
10909 -- the real type for the conversion is taken from the target type of
10910 -- the Round attribute and the result must be marked as rounded.
10912 if Target_Type
= Universal_Real
10913 and then Nkind
(Parent
(N
)) = N_Attribute_Reference
10914 and then Attribute_Name
(Parent
(N
)) = Name_Round
10916 Set_Rounded_Result
(N
);
10917 Set_Etype
(N
, Etype
(Parent
(N
)));
10920 -- Otherwise do correct fixed-conversion, but skip these if the
10921 -- Conversion_OK flag is set, because from a semantic point of view
10922 -- these are simple integer conversions needing no further processing
10923 -- (the backend will simply treat them as integers).
10925 if not Conversion_OK
(N
) then
10926 if Is_Fixed_Point_Type
(Etype
(N
)) then
10927 Expand_Convert_Fixed_To_Fixed
(N
);
10930 elsif Is_Integer_Type
(Etype
(N
)) then
10931 Expand_Convert_Fixed_To_Integer
(N
);
10934 pragma Assert
(Is_Floating_Point_Type
(Etype
(N
)));
10935 Expand_Convert_Fixed_To_Float
(N
);
10940 -- Case of conversions to a fixed-point type
10942 -- These conversions require special expansion and processing, found in
10943 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
10944 -- since from a semantic point of view, these are simple integer
10945 -- conversions, which do not need further processing.
10947 elsif Is_Fixed_Point_Type
(Target_Type
)
10948 and then not Conversion_OK
(N
)
10950 if Is_Integer_Type
(Operand_Type
) then
10951 Expand_Convert_Integer_To_Fixed
(N
);
10954 pragma Assert
(Is_Floating_Point_Type
(Operand_Type
));
10955 Expand_Convert_Float_To_Fixed
(N
);
10959 -- Case of float-to-integer conversions
10961 -- We also handle float-to-fixed conversions with Conversion_OK set
10962 -- since semantically the fixed-point target is treated as though it
10963 -- were an integer in such cases.
10965 elsif Is_Floating_Point_Type
(Operand_Type
)
10967 (Is_Integer_Type
(Target_Type
)
10969 (Is_Fixed_Point_Type
(Target_Type
) and then Conversion_OK
(N
)))
10971 -- One more check here, gcc is still not able to do conversions of
10972 -- this type with proper overflow checking, and so gigi is doing an
10973 -- approximation of what is required by doing floating-point compares
10974 -- with the end-point. But that can lose precision in some cases, and
10975 -- give a wrong result. Converting the operand to Universal_Real is
10976 -- helpful, but still does not catch all cases with 64-bit integers
10977 -- on targets with only 64-bit floats.
10979 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
10980 -- Can this code be removed ???
10982 if Do_Range_Check
(Operand
) then
10984 Make_Type_Conversion
(Loc
,
10986 New_Occurrence_Of
(Universal_Real
, Loc
),
10988 Relocate_Node
(Operand
)));
10990 Set_Etype
(Operand
, Universal_Real
);
10991 Enable_Range_Check
(Operand
);
10992 Set_Do_Range_Check
(Expression
(Operand
), False);
10995 -- Case of array conversions
10997 -- Expansion of array conversions, add required length/range checks but
10998 -- only do this if there is no change of representation. For handling of
10999 -- this case, see Handle_Changed_Representation.
11001 elsif Is_Array_Type
(Target_Type
) then
11002 if Is_Constrained
(Target_Type
) then
11003 Apply_Length_Check
(Operand
, Target_Type
);
11005 Apply_Range_Check
(Operand
, Target_Type
);
11008 Handle_Changed_Representation
;
11010 -- Case of conversions of discriminated types
11012 -- Add required discriminant checks if target is constrained. Again this
11013 -- change is skipped if we have a change of representation.
11015 elsif Has_Discriminants
(Target_Type
)
11016 and then Is_Constrained
(Target_Type
)
11018 Apply_Discriminant_Check
(Operand
, Target_Type
);
11019 Handle_Changed_Representation
;
11021 -- Case of all other record conversions. The only processing required
11022 -- is to check for a change of representation requiring the special
11023 -- assignment processing.
11025 elsif Is_Record_Type
(Target_Type
) then
11027 -- Ada 2005 (AI-216): Program_Error is raised when converting from
11028 -- a derived Unchecked_Union type to an unconstrained type that is
11029 -- not Unchecked_Union if the operand lacks inferable discriminants.
11031 if Is_Derived_Type
(Operand_Type
)
11032 and then Is_Unchecked_Union
(Base_Type
(Operand_Type
))
11033 and then not Is_Constrained
(Target_Type
)
11034 and then not Is_Unchecked_Union
(Base_Type
(Target_Type
))
11035 and then not Has_Inferable_Discriminants
(Operand
)
11037 -- To prevent Gigi from generating illegal code, we generate a
11038 -- Program_Error node, but we give it the target type of the
11039 -- conversion (is this requirement documented somewhere ???)
11042 PE
: constant Node_Id
:= Make_Raise_Program_Error
(Loc
,
11043 Reason
=> PE_Unchecked_Union_Restriction
);
11046 Set_Etype
(PE
, Target_Type
);
11051 Handle_Changed_Representation
;
11054 -- Case of conversions of enumeration types
11056 elsif Is_Enumeration_Type
(Target_Type
) then
11058 -- Special processing is required if there is a change of
11059 -- representation (from enumeration representation clauses).
11061 if not Same_Representation
(Target_Type
, Operand_Type
) then
11063 -- Convert: x(y) to x'val (ytyp'val (y))
11066 Make_Attribute_Reference
(Loc
,
11067 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
11068 Attribute_Name
=> Name_Val
,
11069 Expressions
=> New_List
(
11070 Make_Attribute_Reference
(Loc
,
11071 Prefix
=> New_Occurrence_Of
(Operand_Type
, Loc
),
11072 Attribute_Name
=> Name_Pos
,
11073 Expressions
=> New_List
(Operand
)))));
11075 Analyze_And_Resolve
(N
, Target_Type
);
11078 -- Case of conversions to floating-point
11080 elsif Is_Floating_Point_Type
(Target_Type
) then
11084 -- At this stage, either the conversion node has been transformed into
11085 -- some other equivalent expression, or left as a conversion that can be
11086 -- handled by Gigi, in the following cases:
11088 -- Conversions with no change of representation or type
11090 -- Numeric conversions involving integer, floating- and fixed-point
11091 -- values. Fixed-point values are allowed only if Conversion_OK is
11092 -- set, i.e. if the fixed-point values are to be treated as integers.
11094 -- No other conversions should be passed to Gigi
11096 -- Check: are these rules stated in sinfo??? if so, why restate here???
11098 -- The only remaining step is to generate a range check if we still have
11099 -- a type conversion at this stage and Do_Range_Check is set. For now we
11100 -- do this only for conversions of discrete types and for float-to-float
11103 if Nkind
(N
) = N_Type_Conversion
then
11105 -- For now we only support floating-point cases where both source
11106 -- and target are floating-point types. Conversions where the source
11107 -- and target involve integer or fixed-point types are still TBD,
11108 -- though not clear whether those can even happen at this point, due
11109 -- to transformations above. ???
11111 if Is_Floating_Point_Type
(Etype
(N
))
11112 and then Is_Floating_Point_Type
(Etype
(Expression
(N
)))
11114 if Do_Range_Check
(Expression
(N
))
11115 and then Is_Floating_Point_Type
(Target_Type
)
11117 Generate_Range_Check
11118 (Expression
(N
), Target_Type
, CE_Range_Check_Failed
);
11121 -- Discrete-to-discrete conversions
11123 elsif Is_Discrete_Type
(Etype
(N
)) then
11125 Expr
: constant Node_Id
:= Expression
(N
);
11130 if Do_Range_Check
(Expr
)
11131 and then Is_Discrete_Type
(Etype
(Expr
))
11133 Set_Do_Range_Check
(Expr
, False);
11135 -- Before we do a range check, we have to deal with treating
11136 -- a fixed-point operand as an integer. The way we do this
11137 -- is simply to do an unchecked conversion to an appropriate
11138 -- integer type large enough to hold the result.
11140 -- This code is not active yet, because we are only dealing
11141 -- with discrete types so far ???
11143 if Nkind
(Expr
) in N_Has_Treat_Fixed_As_Integer
11144 and then Treat_Fixed_As_Integer
(Expr
)
11146 Ftyp
:= Base_Type
(Etype
(Expr
));
11148 if Esize
(Ftyp
) >= Esize
(Standard_Integer
) then
11149 Ityp
:= Standard_Long_Long_Integer
;
11151 Ityp
:= Standard_Integer
;
11154 Rewrite
(Expr
, Unchecked_Convert_To
(Ityp
, Expr
));
11157 -- Reset overflow flag, since the range check will include
11158 -- dealing with possible overflow, and generate the check.
11159 -- If Address is either a source type or target type,
11160 -- suppress range check to avoid typing anomalies when
11161 -- it is a visible integer type.
11163 Set_Do_Overflow_Check
(N
, False);
11165 if not Is_Descendent_Of_Address
(Etype
(Expr
))
11166 and then not Is_Descendent_Of_Address
(Target_Type
)
11168 Generate_Range_Check
11169 (Expr
, Target_Type
, CE_Range_Check_Failed
);
11176 -- Here at end of processing
11179 -- Apply predicate check if required. Note that we can't just call
11180 -- Apply_Predicate_Check here, because the type looks right after
11181 -- the conversion and it would omit the check. The Comes_From_Source
11182 -- guard is necessary to prevent infinite recursions when we generate
11183 -- internal conversions for the purpose of checking predicates.
11185 if Present
(Predicate_Function
(Target_Type
))
11186 and then Target_Type
/= Operand_Type
11187 and then Comes_From_Source
(N
)
11190 New_Expr
: constant Node_Id
:= Duplicate_Subexpr
(N
);
11193 -- Avoid infinite recursion on the subsequent expansion of
11194 -- of the copy of the original type conversion.
11196 Set_Comes_From_Source
(New_Expr
, False);
11197 Insert_Action
(N
, Make_Predicate_Check
(Target_Type
, New_Expr
));
11200 end Expand_N_Type_Conversion
;
11202 -----------------------------------
11203 -- Expand_N_Unchecked_Expression --
11204 -----------------------------------
11206 -- Remove the unchecked expression node from the tree. Its job was simply
11207 -- to make sure that its constituent expression was handled with checks
11208 -- off, and now that that is done, we can remove it from the tree, and
11209 -- indeed must, since Gigi does not expect to see these nodes.
11211 procedure Expand_N_Unchecked_Expression
(N
: Node_Id
) is
11212 Exp
: constant Node_Id
:= Expression
(N
);
11214 Set_Assignment_OK
(Exp
, Assignment_OK
(N
) or else Assignment_OK
(Exp
));
11216 end Expand_N_Unchecked_Expression
;
11218 ----------------------------------------
11219 -- Expand_N_Unchecked_Type_Conversion --
11220 ----------------------------------------
11222 -- If this cannot be handled by Gigi and we haven't already made a
11223 -- temporary for it, do it now.
11225 procedure Expand_N_Unchecked_Type_Conversion
(N
: Node_Id
) is
11226 Target_Type
: constant Entity_Id
:= Etype
(N
);
11227 Operand
: constant Node_Id
:= Expression
(N
);
11228 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
11231 -- Nothing at all to do if conversion is to the identical type so remove
11232 -- the conversion completely, it is useless, except that it may carry
11233 -- an Assignment_OK indication which must be propagated to the operand.
11235 if Operand_Type
= Target_Type
then
11237 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
11239 if Assignment_OK
(N
) then
11240 Set_Assignment_OK
(Operand
);
11243 Rewrite
(N
, Relocate_Node
(Operand
));
11247 -- If we have a conversion of a compile time known value to a target
11248 -- type and the value is in range of the target type, then we can simply
11249 -- replace the construct by an integer literal of the correct type. We
11250 -- only apply this to integer types being converted. Possibly it may
11251 -- apply in other cases, but it is too much trouble to worry about.
11253 -- Note that we do not do this transformation if the Kill_Range_Check
11254 -- flag is set, since then the value may be outside the expected range.
11255 -- This happens in the Normalize_Scalars case.
11257 -- We also skip this if either the target or operand type is biased
11258 -- because in this case, the unchecked conversion is supposed to
11259 -- preserve the bit pattern, not the integer value.
11261 if Is_Integer_Type
(Target_Type
)
11262 and then not Has_Biased_Representation
(Target_Type
)
11263 and then Is_Integer_Type
(Operand_Type
)
11264 and then not Has_Biased_Representation
(Operand_Type
)
11265 and then Compile_Time_Known_Value
(Operand
)
11266 and then not Kill_Range_Check
(N
)
11269 Val
: constant Uint
:= Expr_Value
(Operand
);
11272 if Compile_Time_Known_Value
(Type_Low_Bound
(Target_Type
))
11274 Compile_Time_Known_Value
(Type_High_Bound
(Target_Type
))
11276 Val
>= Expr_Value
(Type_Low_Bound
(Target_Type
))
11278 Val
<= Expr_Value
(Type_High_Bound
(Target_Type
))
11280 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Val
));
11282 -- If Address is the target type, just set the type to avoid a
11283 -- spurious type error on the literal when Address is a visible
11286 if Is_Descendent_Of_Address
(Target_Type
) then
11287 Set_Etype
(N
, Target_Type
);
11289 Analyze_And_Resolve
(N
, Target_Type
);
11297 -- Nothing to do if conversion is safe
11299 if Safe_Unchecked_Type_Conversion
(N
) then
11303 -- Otherwise force evaluation unless Assignment_OK flag is set (this
11304 -- flag indicates ??? More comments needed here)
11306 if Assignment_OK
(N
) then
11309 Force_Evaluation
(N
);
11311 end Expand_N_Unchecked_Type_Conversion
;
11313 ----------------------------
11314 -- Expand_Record_Equality --
11315 ----------------------------
11317 -- For non-variant records, Equality is expanded when needed into:
11319 -- and then Lhs.Discr1 = Rhs.Discr1
11321 -- and then Lhs.Discrn = Rhs.Discrn
11322 -- and then Lhs.Cmp1 = Rhs.Cmp1
11324 -- and then Lhs.Cmpn = Rhs.Cmpn
11326 -- The expression is folded by the back-end for adjacent fields. This
11327 -- function is called for tagged record in only one occasion: for imple-
11328 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
11329 -- otherwise the primitive "=" is used directly.
11331 function Expand_Record_Equality
11336 Bodies
: List_Id
) return Node_Id
11338 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
11343 First_Time
: Boolean := True;
11345 function Element_To_Compare
(C
: Entity_Id
) return Entity_Id
;
11346 -- Return the next discriminant or component to compare, starting with
11347 -- C, skipping inherited components.
11349 ------------------------
11350 -- Element_To_Compare --
11351 ------------------------
11353 function Element_To_Compare
(C
: Entity_Id
) return Entity_Id
is
11359 -- Exit loop when the next element to be compared is found, or
11360 -- there is no more such element.
11362 exit when No
(Comp
);
11364 exit when Ekind_In
(Comp
, E_Discriminant
, E_Component
)
11367 -- Skip inherited components
11369 -- Note: for a tagged type, we always generate the "=" primitive
11370 -- for the base type (not on the first subtype), so the test for
11371 -- Comp /= Original_Record_Component (Comp) is True for
11372 -- inherited components only.
11374 (Is_Tagged_Type
(Typ
)
11375 and then Comp
/= Original_Record_Component
(Comp
))
11379 or else Chars
(Comp
) = Name_uTag
11381 -- The .NET/JVM version of type Root_Controlled contains two
11382 -- fields which should not be considered part of the object. To
11383 -- achieve proper equiality between two controlled objects on
11384 -- .NET/JVM, skip _Parent whenever it has type Root_Controlled.
11386 or else (Chars
(Comp
) = Name_uParent
11387 and then VM_Target
/= No_VM
11388 and then Etype
(Comp
) = RTE
(RE_Root_Controlled
))
11390 -- Skip interface elements (secondary tags???)
11392 or else Is_Interface
(Etype
(Comp
)));
11394 Next_Entity
(Comp
);
11398 end Element_To_Compare
;
11400 -- Start of processing for Expand_Record_Equality
11403 -- Generates the following code: (assuming that Typ has one Discr and
11404 -- component C2 is also a record)
11407 -- and then Lhs.Discr1 = Rhs.Discr1
11408 -- and then Lhs.C1 = Rhs.C1
11409 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
11411 -- and then Lhs.Cmpn = Rhs.Cmpn
11413 Result
:= New_Occurrence_Of
(Standard_True
, Loc
);
11414 C
:= Element_To_Compare
(First_Entity
(Typ
));
11415 while Present
(C
) loop
11423 First_Time
:= False;
11427 New_Lhs
:= New_Copy_Tree
(Lhs
);
11428 New_Rhs
:= New_Copy_Tree
(Rhs
);
11432 Expand_Composite_Equality
(Nod
, Etype
(C
),
11434 Make_Selected_Component
(Loc
,
11436 Selector_Name
=> New_Occurrence_Of
(C
, Loc
)),
11438 Make_Selected_Component
(Loc
,
11440 Selector_Name
=> New_Occurrence_Of
(C
, Loc
)),
11443 -- If some (sub)component is an unchecked_union, the whole
11444 -- operation will raise program error.
11446 if Nkind
(Check
) = N_Raise_Program_Error
then
11448 Set_Etype
(Result
, Standard_Boolean
);
11452 Make_And_Then
(Loc
,
11453 Left_Opnd
=> Result
,
11454 Right_Opnd
=> Check
);
11458 C
:= Element_To_Compare
(Next_Entity
(C
));
11462 end Expand_Record_Equality
;
11464 ---------------------------
11465 -- Expand_Set_Membership --
11466 ---------------------------
11468 procedure Expand_Set_Membership
(N
: Node_Id
) is
11469 Lop
: constant Node_Id
:= Left_Opnd
(N
);
11473 function Make_Cond
(Alt
: Node_Id
) return Node_Id
;
11474 -- If the alternative is a subtype mark, create a simple membership
11475 -- test. Otherwise create an equality test for it.
11481 function Make_Cond
(Alt
: Node_Id
) return Node_Id
is
11483 L
: constant Node_Id
:= New_Copy
(Lop
);
11484 R
: constant Node_Id
:= Relocate_Node
(Alt
);
11487 if (Is_Entity_Name
(Alt
) and then Is_Type
(Entity
(Alt
)))
11488 or else Nkind
(Alt
) = N_Range
11491 Make_In
(Sloc
(Alt
),
11496 Make_Op_Eq
(Sloc
(Alt
),
11504 -- Start of processing for Expand_Set_Membership
11507 Remove_Side_Effects
(Lop
);
11509 Alt
:= Last
(Alternatives
(N
));
11510 Res
:= Make_Cond
(Alt
);
11513 while Present
(Alt
) loop
11515 Make_Or_Else
(Sloc
(Alt
),
11516 Left_Opnd
=> Make_Cond
(Alt
),
11517 Right_Opnd
=> Res
);
11522 Analyze_And_Resolve
(N
, Standard_Boolean
);
11523 end Expand_Set_Membership
;
11525 -----------------------------------
11526 -- Expand_Short_Circuit_Operator --
11527 -----------------------------------
11529 -- Deal with special expansion if actions are present for the right operand
11530 -- and deal with optimizing case of arguments being True or False. We also
11531 -- deal with the special case of non-standard boolean values.
11533 procedure Expand_Short_Circuit_Operator
(N
: Node_Id
) is
11534 Loc
: constant Source_Ptr
:= Sloc
(N
);
11535 Typ
: constant Entity_Id
:= Etype
(N
);
11536 Left
: constant Node_Id
:= Left_Opnd
(N
);
11537 Right
: constant Node_Id
:= Right_Opnd
(N
);
11538 LocR
: constant Source_Ptr
:= Sloc
(Right
);
11541 Shortcut_Value
: constant Boolean := Nkind
(N
) = N_Or_Else
;
11542 Shortcut_Ent
: constant Entity_Id
:= Boolean_Literals
(Shortcut_Value
);
11543 -- If Left = Shortcut_Value then Right need not be evaluated
11546 -- Deal with non-standard booleans
11548 if Is_Boolean_Type
(Typ
) then
11549 Adjust_Condition
(Left
);
11550 Adjust_Condition
(Right
);
11551 Set_Etype
(N
, Standard_Boolean
);
11554 -- Check for cases where left argument is known to be True or False
11556 if Compile_Time_Known_Value
(Left
) then
11558 -- Mark SCO for left condition as compile time known
11560 if Generate_SCO
and then Comes_From_Source
(Left
) then
11561 Set_SCO_Condition
(Left
, Expr_Value_E
(Left
) = Standard_True
);
11564 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
11565 -- Any actions associated with Right will be executed unconditionally
11566 -- and can thus be inserted into the tree unconditionally.
11568 if Expr_Value_E
(Left
) /= Shortcut_Ent
then
11569 if Present
(Actions
(N
)) then
11570 Insert_Actions
(N
, Actions
(N
));
11573 Rewrite
(N
, Right
);
11575 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
11576 -- In this case we can forget the actions associated with Right,
11577 -- since they will never be executed.
11580 Kill_Dead_Code
(Right
);
11581 Kill_Dead_Code
(Actions
(N
));
11582 Rewrite
(N
, New_Occurrence_Of
(Shortcut_Ent
, Loc
));
11585 Adjust_Result_Type
(N
, Typ
);
11589 -- If Actions are present for the right operand, we have to do some
11590 -- special processing. We can't just let these actions filter back into
11591 -- code preceding the short circuit (which is what would have happened
11592 -- if we had not trapped them in the short-circuit form), since they
11593 -- must only be executed if the right operand of the short circuit is
11594 -- executed and not otherwise.
11596 if Present
(Actions
(N
)) then
11597 Actlist
:= Actions
(N
);
11599 -- We now use an Expression_With_Actions node for the right operand
11600 -- of the short-circuit form. Note that this solves the traceability
11601 -- problems for coverage analysis.
11604 Make_Expression_With_Actions
(LocR
,
11605 Expression
=> Relocate_Node
(Right
),
11606 Actions
=> Actlist
));
11608 Set_Actions
(N
, No_List
);
11609 Analyze_And_Resolve
(Right
, Standard_Boolean
);
11611 Adjust_Result_Type
(N
, Typ
);
11615 -- No actions present, check for cases of right argument True/False
11617 if Compile_Time_Known_Value
(Right
) then
11619 -- Mark SCO for left condition as compile time known
11621 if Generate_SCO
and then Comes_From_Source
(Right
) then
11622 Set_SCO_Condition
(Right
, Expr_Value_E
(Right
) = Standard_True
);
11625 -- Change (Left and then True), (Left or else False) to Left.
11626 -- Note that we know there are no actions associated with the right
11627 -- operand, since we just checked for this case above.
11629 if Expr_Value_E
(Right
) /= Shortcut_Ent
then
11632 -- Change (Left and then False), (Left or else True) to Right,
11633 -- making sure to preserve any side effects associated with the Left
11637 Remove_Side_Effects
(Left
);
11638 Rewrite
(N
, New_Occurrence_Of
(Shortcut_Ent
, Loc
));
11642 Adjust_Result_Type
(N
, Typ
);
11643 end Expand_Short_Circuit_Operator
;
11645 -------------------------------------
11646 -- Fixup_Universal_Fixed_Operation --
11647 -------------------------------------
11649 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
) is
11650 Conv
: constant Node_Id
:= Parent
(N
);
11653 -- We must have a type conversion immediately above us
11655 pragma Assert
(Nkind
(Conv
) = N_Type_Conversion
);
11657 -- Normally the type conversion gives our target type. The exception
11658 -- occurs in the case of the Round attribute, where the conversion
11659 -- will be to universal real, and our real type comes from the Round
11660 -- attribute (as well as an indication that we must round the result)
11662 if Nkind
(Parent
(Conv
)) = N_Attribute_Reference
11663 and then Attribute_Name
(Parent
(Conv
)) = Name_Round
11665 Set_Etype
(N
, Etype
(Parent
(Conv
)));
11666 Set_Rounded_Result
(N
);
11668 -- Normal case where type comes from conversion above us
11671 Set_Etype
(N
, Etype
(Conv
));
11673 end Fixup_Universal_Fixed_Operation
;
11675 ---------------------------------
11676 -- Has_Inferable_Discriminants --
11677 ---------------------------------
11679 function Has_Inferable_Discriminants
(N
: Node_Id
) return Boolean is
11681 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean;
11682 -- Determines whether the left-most prefix of a selected component is a
11683 -- formal parameter in a subprogram. Assumes N is a selected component.
11685 --------------------------------
11686 -- Prefix_Is_Formal_Parameter --
11687 --------------------------------
11689 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean is
11690 Sel_Comp
: Node_Id
;
11693 -- Move to the left-most prefix by climbing up the tree
11696 while Present
(Parent
(Sel_Comp
))
11697 and then Nkind
(Parent
(Sel_Comp
)) = N_Selected_Component
11699 Sel_Comp
:= Parent
(Sel_Comp
);
11702 return Ekind
(Entity
(Prefix
(Sel_Comp
))) in Formal_Kind
;
11703 end Prefix_Is_Formal_Parameter
;
11705 -- Start of processing for Has_Inferable_Discriminants
11708 -- For selected components, the subtype of the selector must be a
11709 -- constrained Unchecked_Union. If the component is subject to a
11710 -- per-object constraint, then the enclosing object must have inferable
11713 if Nkind
(N
) = N_Selected_Component
then
11714 if Has_Per_Object_Constraint
(Entity
(Selector_Name
(N
))) then
11716 -- A small hack. If we have a per-object constrained selected
11717 -- component of a formal parameter, return True since we do not
11718 -- know the actual parameter association yet.
11720 if Prefix_Is_Formal_Parameter
(N
) then
11723 -- Otherwise, check the enclosing object and the selector
11726 return Has_Inferable_Discriminants
(Prefix
(N
))
11727 and then Has_Inferable_Discriminants
(Selector_Name
(N
));
11730 -- The call to Has_Inferable_Discriminants will determine whether
11731 -- the selector has a constrained Unchecked_Union nominal type.
11734 return Has_Inferable_Discriminants
(Selector_Name
(N
));
11737 -- A qualified expression has inferable discriminants if its subtype
11738 -- mark is a constrained Unchecked_Union subtype.
11740 elsif Nkind
(N
) = N_Qualified_Expression
then
11741 return Is_Unchecked_Union
(Etype
(Subtype_Mark
(N
)))
11742 and then Is_Constrained
(Etype
(Subtype_Mark
(N
)));
11744 -- For all other names, it is sufficient to have a constrained
11745 -- Unchecked_Union nominal subtype.
11748 return Is_Unchecked_Union
(Base_Type
(Etype
(N
)))
11749 and then Is_Constrained
(Etype
(N
));
11751 end Has_Inferable_Discriminants
;
11753 -------------------------------
11754 -- Insert_Dereference_Action --
11755 -------------------------------
11757 procedure Insert_Dereference_Action
(N
: Node_Id
) is
11759 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean;
11760 -- Return true if type of P is derived from Checked_Pool;
11762 -----------------------------
11763 -- Is_Checked_Storage_Pool --
11764 -----------------------------
11766 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean is
11775 while T
/= Etype
(T
) loop
11776 if Is_RTE
(T
, RE_Checked_Pool
) then
11784 end Is_Checked_Storage_Pool
;
11788 Typ
: constant Entity_Id
:= Etype
(N
);
11789 Desig
: constant Entity_Id
:= Available_View
(Designated_Type
(Typ
));
11790 Loc
: constant Source_Ptr
:= Sloc
(N
);
11791 Pool
: constant Entity_Id
:= Associated_Storage_Pool
(Typ
);
11792 Pnod
: constant Node_Id
:= Parent
(N
);
11798 Size_Bits
: Node_Id
;
11801 -- Start of processing for Insert_Dereference_Action
11804 pragma Assert
(Nkind
(Pnod
) = N_Explicit_Dereference
);
11806 -- Do not re-expand a dereference which has already been processed by
11809 if Has_Dereference_Action
(Pnod
) then
11812 -- Do not perform this type of expansion for internally-generated
11815 elsif not Comes_From_Source
(Original_Node
(Pnod
)) then
11818 -- A dereference action is only applicable to objects which have been
11819 -- allocated on a checked pool.
11821 elsif not Is_Checked_Storage_Pool
(Pool
) then
11825 -- Extract the address of the dereferenced object. Generate:
11827 -- Addr : System.Address := <N>'Pool_Address;
11829 Addr
:= Make_Temporary
(Loc
, 'P');
11832 Make_Object_Declaration
(Loc
,
11833 Defining_Identifier
=> Addr
,
11834 Object_Definition
=>
11835 New_Occurrence_Of
(RTE
(RE_Address
), Loc
),
11837 Make_Attribute_Reference
(Loc
,
11838 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
),
11839 Attribute_Name
=> Name_Pool_Address
)));
11841 -- Calculate the size of the dereferenced object. Generate:
11843 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
11846 Make_Explicit_Dereference
(Loc
,
11847 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
11848 Set_Has_Dereference_Action
(Deref
);
11851 Make_Attribute_Reference
(Loc
,
11853 Attribute_Name
=> Name_Size
);
11855 -- Special case of an unconstrained array: need to add descriptor size
11857 if Is_Array_Type
(Desig
)
11858 and then not Is_Constrained
(First_Subtype
(Desig
))
11863 Make_Attribute_Reference
(Loc
,
11865 New_Occurrence_Of
(First_Subtype
(Desig
), Loc
),
11866 Attribute_Name
=> Name_Descriptor_Size
),
11867 Right_Opnd
=> Size_Bits
);
11870 Size
:= Make_Temporary
(Loc
, 'S');
11872 Make_Object_Declaration
(Loc
,
11873 Defining_Identifier
=> Size
,
11874 Object_Definition
=>
11875 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
),
11877 Make_Op_Divide
(Loc
,
11878 Left_Opnd
=> Size_Bits
,
11879 Right_Opnd
=> Make_Integer_Literal
(Loc
, System_Storage_Unit
))));
11881 -- Calculate the alignment of the dereferenced object. Generate:
11882 -- Alig : constant Storage_Count := <N>.all'Alignment;
11885 Make_Explicit_Dereference
(Loc
,
11886 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
11887 Set_Has_Dereference_Action
(Deref
);
11889 Alig
:= Make_Temporary
(Loc
, 'A');
11891 Make_Object_Declaration
(Loc
,
11892 Defining_Identifier
=> Alig
,
11893 Object_Definition
=>
11894 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
),
11896 Make_Attribute_Reference
(Loc
,
11898 Attribute_Name
=> Name_Alignment
)));
11900 -- A dereference of a controlled object requires special processing. The
11901 -- finalization machinery requests additional space from the underlying
11902 -- pool to allocate and hide two pointers. As a result, a checked pool
11903 -- may mark the wrong memory as valid. Since checked pools do not have
11904 -- knowledge of hidden pointers, we have to bring the two pointers back
11905 -- in view in order to restore the original state of the object.
11907 if Needs_Finalization
(Desig
) then
11909 -- Adjust the address and size of the dereferenced object. Generate:
11910 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
11913 Make_Procedure_Call_Statement
(Loc
,
11915 New_Occurrence_Of
(RTE
(RE_Adjust_Controlled_Dereference
), Loc
),
11916 Parameter_Associations
=> New_List
(
11917 New_Occurrence_Of
(Addr
, Loc
),
11918 New_Occurrence_Of
(Size
, Loc
),
11919 New_Occurrence_Of
(Alig
, Loc
)));
11921 -- Class-wide types complicate things because we cannot determine
11922 -- statically whether the actual object is truly controlled. We must
11923 -- generate a runtime check to detect this property. Generate:
11925 -- if Needs_Finalization (<N>.all'Tag) then
11929 if Is_Class_Wide_Type
(Desig
) then
11931 Make_Explicit_Dereference
(Loc
,
11932 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
11933 Set_Has_Dereference_Action
(Deref
);
11936 Make_Implicit_If_Statement
(N
,
11938 Make_Function_Call
(Loc
,
11940 New_Occurrence_Of
(RTE
(RE_Needs_Finalization
), Loc
),
11941 Parameter_Associations
=> New_List
(
11942 Make_Attribute_Reference
(Loc
,
11944 Attribute_Name
=> Name_Tag
))),
11945 Then_Statements
=> New_List
(Stmt
));
11948 Insert_Action
(N
, Stmt
);
11952 -- Dereference (Pool, Addr, Size, Alig);
11955 Make_Procedure_Call_Statement
(Loc
,
11958 (Find_Prim_Op
(Etype
(Pool
), Name_Dereference
), Loc
),
11959 Parameter_Associations
=> New_List
(
11960 New_Occurrence_Of
(Pool
, Loc
),
11961 New_Occurrence_Of
(Addr
, Loc
),
11962 New_Occurrence_Of
(Size
, Loc
),
11963 New_Occurrence_Of
(Alig
, Loc
))));
11965 -- Mark the explicit dereference as processed to avoid potential
11966 -- infinite expansion.
11968 Set_Has_Dereference_Action
(Pnod
);
11971 when RE_Not_Available
=>
11973 end Insert_Dereference_Action
;
11975 --------------------------------
11976 -- Integer_Promotion_Possible --
11977 --------------------------------
11979 function Integer_Promotion_Possible
(N
: Node_Id
) return Boolean is
11980 Operand
: constant Node_Id
:= Expression
(N
);
11981 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
11982 Root_Operand_Type
: constant Entity_Id
:= Root_Type
(Operand_Type
);
11985 pragma Assert
(Nkind
(N
) = N_Type_Conversion
);
11989 -- We only do the transformation for source constructs. We assume
11990 -- that the expander knows what it is doing when it generates code.
11992 Comes_From_Source
(N
)
11994 -- If the operand type is Short_Integer or Short_Short_Integer,
11995 -- then we will promote to Integer, which is available on all
11996 -- targets, and is sufficient to ensure no intermediate overflow.
11997 -- Furthermore it is likely to be as efficient or more efficient
11998 -- than using the smaller type for the computation so we do this
11999 -- unconditionally.
12002 (Root_Operand_Type
= Base_Type
(Standard_Short_Integer
)
12004 Root_Operand_Type
= Base_Type
(Standard_Short_Short_Integer
))
12006 -- Test for interesting operation, which includes addition,
12007 -- division, exponentiation, multiplication, subtraction, absolute
12008 -- value and unary negation. Unary "+" is omitted since it is a
12009 -- no-op and thus can't overflow.
12011 and then Nkind_In
(Operand
, N_Op_Abs
,
12018 end Integer_Promotion_Possible
;
12020 ------------------------------
12021 -- Make_Array_Comparison_Op --
12022 ------------------------------
12024 -- This is a hand-coded expansion of the following generic function:
12027 -- type elem is (<>);
12028 -- type index is (<>);
12029 -- type a is array (index range <>) of elem;
12031 -- function Gnnn (X : a; Y: a) return boolean is
12032 -- J : index := Y'first;
12035 -- if X'length = 0 then
12038 -- elsif Y'length = 0 then
12042 -- for I in X'range loop
12043 -- if X (I) = Y (J) then
12044 -- if J = Y'last then
12047 -- J := index'succ (J);
12051 -- return X (I) > Y (J);
12055 -- return X'length > Y'length;
12059 -- Note that since we are essentially doing this expansion by hand, we
12060 -- do not need to generate an actual or formal generic part, just the
12061 -- instantiated function itself.
12063 -- Perhaps we could have the actual generic available in the run-time,
12064 -- obtained by rtsfind, and actually expand a real instantiation ???
12066 function Make_Array_Comparison_Op
12068 Nod
: Node_Id
) return Node_Id
12070 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
12072 X
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uX
);
12073 Y
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uY
);
12074 I
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uI
);
12075 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
12077 Index
: constant Entity_Id
:= Base_Type
(Etype
(First_Index
(Typ
)));
12079 Loop_Statement
: Node_Id
;
12080 Loop_Body
: Node_Id
;
12082 Inner_If
: Node_Id
;
12083 Final_Expr
: Node_Id
;
12084 Func_Body
: Node_Id
;
12085 Func_Name
: Entity_Id
;
12091 -- if J = Y'last then
12094 -- J := index'succ (J);
12098 Make_Implicit_If_Statement
(Nod
,
12101 Left_Opnd
=> New_Occurrence_Of
(J
, Loc
),
12103 Make_Attribute_Reference
(Loc
,
12104 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12105 Attribute_Name
=> Name_Last
)),
12107 Then_Statements
=> New_List
(
12108 Make_Exit_Statement
(Loc
)),
12112 Make_Assignment_Statement
(Loc
,
12113 Name
=> New_Occurrence_Of
(J
, Loc
),
12115 Make_Attribute_Reference
(Loc
,
12116 Prefix
=> New_Occurrence_Of
(Index
, Loc
),
12117 Attribute_Name
=> Name_Succ
,
12118 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
))))));
12120 -- if X (I) = Y (J) then
12123 -- return X (I) > Y (J);
12127 Make_Implicit_If_Statement
(Nod
,
12131 Make_Indexed_Component
(Loc
,
12132 Prefix
=> New_Occurrence_Of
(X
, Loc
),
12133 Expressions
=> New_List
(New_Occurrence_Of
(I
, Loc
))),
12136 Make_Indexed_Component
(Loc
,
12137 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12138 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)))),
12140 Then_Statements
=> New_List
(Inner_If
),
12142 Else_Statements
=> New_List
(
12143 Make_Simple_Return_Statement
(Loc
,
12147 Make_Indexed_Component
(Loc
,
12148 Prefix
=> New_Occurrence_Of
(X
, Loc
),
12149 Expressions
=> New_List
(New_Occurrence_Of
(I
, Loc
))),
12152 Make_Indexed_Component
(Loc
,
12153 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12154 Expressions
=> New_List
(
12155 New_Occurrence_Of
(J
, Loc
)))))));
12157 -- for I in X'range loop
12162 Make_Implicit_Loop_Statement
(Nod
,
12163 Identifier
=> Empty
,
12165 Iteration_Scheme
=>
12166 Make_Iteration_Scheme
(Loc
,
12167 Loop_Parameter_Specification
=>
12168 Make_Loop_Parameter_Specification
(Loc
,
12169 Defining_Identifier
=> I
,
12170 Discrete_Subtype_Definition
=>
12171 Make_Attribute_Reference
(Loc
,
12172 Prefix
=> New_Occurrence_Of
(X
, Loc
),
12173 Attribute_Name
=> Name_Range
))),
12175 Statements
=> New_List
(Loop_Body
));
12177 -- if X'length = 0 then
12179 -- elsif Y'length = 0 then
12182 -- for ... loop ... end loop;
12183 -- return X'length > Y'length;
12187 Make_Attribute_Reference
(Loc
,
12188 Prefix
=> New_Occurrence_Of
(X
, Loc
),
12189 Attribute_Name
=> Name_Length
);
12192 Make_Attribute_Reference
(Loc
,
12193 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12194 Attribute_Name
=> Name_Length
);
12198 Left_Opnd
=> Length1
,
12199 Right_Opnd
=> Length2
);
12202 Make_Implicit_If_Statement
(Nod
,
12206 Make_Attribute_Reference
(Loc
,
12207 Prefix
=> New_Occurrence_Of
(X
, Loc
),
12208 Attribute_Name
=> Name_Length
),
12210 Make_Integer_Literal
(Loc
, 0)),
12214 Make_Simple_Return_Statement
(Loc
,
12215 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))),
12217 Elsif_Parts
=> New_List
(
12218 Make_Elsif_Part
(Loc
,
12222 Make_Attribute_Reference
(Loc
,
12223 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12224 Attribute_Name
=> Name_Length
),
12226 Make_Integer_Literal
(Loc
, 0)),
12230 Make_Simple_Return_Statement
(Loc
,
12231 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
))))),
12233 Else_Statements
=> New_List
(
12235 Make_Simple_Return_Statement
(Loc
,
12236 Expression
=> Final_Expr
)));
12240 Formals
:= New_List
(
12241 Make_Parameter_Specification
(Loc
,
12242 Defining_Identifier
=> X
,
12243 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)),
12245 Make_Parameter_Specification
(Loc
,
12246 Defining_Identifier
=> Y
,
12247 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)));
12249 -- function Gnnn (...) return boolean is
12250 -- J : index := Y'first;
12255 Func_Name
:= Make_Temporary
(Loc
, 'G');
12258 Make_Subprogram_Body
(Loc
,
12260 Make_Function_Specification
(Loc
,
12261 Defining_Unit_Name
=> Func_Name
,
12262 Parameter_Specifications
=> Formals
,
12263 Result_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
)),
12265 Declarations
=> New_List
(
12266 Make_Object_Declaration
(Loc
,
12267 Defining_Identifier
=> J
,
12268 Object_Definition
=> New_Occurrence_Of
(Index
, Loc
),
12270 Make_Attribute_Reference
(Loc
,
12271 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12272 Attribute_Name
=> Name_First
))),
12274 Handled_Statement_Sequence
=>
12275 Make_Handled_Sequence_Of_Statements
(Loc
,
12276 Statements
=> New_List
(If_Stat
)));
12279 end Make_Array_Comparison_Op
;
12281 ---------------------------
12282 -- Make_Boolean_Array_Op --
12283 ---------------------------
12285 -- For logical operations on boolean arrays, expand in line the following,
12286 -- replacing 'and' with 'or' or 'xor' where needed:
12288 -- function Annn (A : typ; B: typ) return typ is
12291 -- for J in A'range loop
12292 -- C (J) := A (J) op B (J);
12297 -- Here typ is the boolean array type
12299 function Make_Boolean_Array_Op
12301 N
: Node_Id
) return Node_Id
12303 Loc
: constant Source_Ptr
:= Sloc
(N
);
12305 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
12306 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
12307 C
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uC
);
12308 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
12316 Func_Name
: Entity_Id
;
12317 Func_Body
: Node_Id
;
12318 Loop_Statement
: Node_Id
;
12322 Make_Indexed_Component
(Loc
,
12323 Prefix
=> New_Occurrence_Of
(A
, Loc
),
12324 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
12327 Make_Indexed_Component
(Loc
,
12328 Prefix
=> New_Occurrence_Of
(B
, Loc
),
12329 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
12332 Make_Indexed_Component
(Loc
,
12333 Prefix
=> New_Occurrence_Of
(C
, Loc
),
12334 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
12336 if Nkind
(N
) = N_Op_And
then
12340 Right_Opnd
=> B_J
);
12342 elsif Nkind
(N
) = N_Op_Or
then
12346 Right_Opnd
=> B_J
);
12352 Right_Opnd
=> B_J
);
12356 Make_Implicit_Loop_Statement
(N
,
12357 Identifier
=> Empty
,
12359 Iteration_Scheme
=>
12360 Make_Iteration_Scheme
(Loc
,
12361 Loop_Parameter_Specification
=>
12362 Make_Loop_Parameter_Specification
(Loc
,
12363 Defining_Identifier
=> J
,
12364 Discrete_Subtype_Definition
=>
12365 Make_Attribute_Reference
(Loc
,
12366 Prefix
=> New_Occurrence_Of
(A
, Loc
),
12367 Attribute_Name
=> Name_Range
))),
12369 Statements
=> New_List
(
12370 Make_Assignment_Statement
(Loc
,
12372 Expression
=> Op
)));
12374 Formals
:= New_List
(
12375 Make_Parameter_Specification
(Loc
,
12376 Defining_Identifier
=> A
,
12377 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)),
12379 Make_Parameter_Specification
(Loc
,
12380 Defining_Identifier
=> B
,
12381 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)));
12383 Func_Name
:= Make_Temporary
(Loc
, 'A');
12384 Set_Is_Inlined
(Func_Name
);
12387 Make_Subprogram_Body
(Loc
,
12389 Make_Function_Specification
(Loc
,
12390 Defining_Unit_Name
=> Func_Name
,
12391 Parameter_Specifications
=> Formals
,
12392 Result_Definition
=> New_Occurrence_Of
(Typ
, Loc
)),
12394 Declarations
=> New_List
(
12395 Make_Object_Declaration
(Loc
,
12396 Defining_Identifier
=> C
,
12397 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
))),
12399 Handled_Statement_Sequence
=>
12400 Make_Handled_Sequence_Of_Statements
(Loc
,
12401 Statements
=> New_List
(
12403 Make_Simple_Return_Statement
(Loc
,
12404 Expression
=> New_Occurrence_Of
(C
, Loc
)))));
12407 end Make_Boolean_Array_Op
;
12409 -----------------------------------------
12410 -- Minimized_Eliminated_Overflow_Check --
12411 -----------------------------------------
12413 function Minimized_Eliminated_Overflow_Check
(N
: Node_Id
) return Boolean is
12416 Is_Signed_Integer_Type
(Etype
(N
))
12417 and then Overflow_Check_Mode
in Minimized_Or_Eliminated
;
12418 end Minimized_Eliminated_Overflow_Check
;
12420 --------------------------------
12421 -- Optimize_Length_Comparison --
12422 --------------------------------
12424 procedure Optimize_Length_Comparison
(N
: Node_Id
) is
12425 Loc
: constant Source_Ptr
:= Sloc
(N
);
12426 Typ
: constant Entity_Id
:= Etype
(N
);
12431 -- First and Last attribute reference nodes, which end up as left and
12432 -- right operands of the optimized result.
12435 -- True for comparison operand of zero
12438 -- Comparison operand, set only if Is_Zero is false
12441 -- Entity whose length is being compared
12444 -- Integer_Literal node for length attribute expression, or Empty
12445 -- if there is no such expression present.
12448 -- Type of array index to which 'Length is applied
12450 Op
: Node_Kind
:= Nkind
(N
);
12451 -- Kind of comparison operator, gets flipped if operands backwards
12453 function Is_Optimizable
(N
: Node_Id
) return Boolean;
12454 -- Tests N to see if it is an optimizable comparison value (defined as
12455 -- constant zero or one, or something else where the value is known to
12456 -- be positive and in the range of 32-bits, and where the corresponding
12457 -- Length value is also known to be 32-bits. If result is true, sets
12458 -- Is_Zero, Ityp, and Comp accordingly.
12460 function Is_Entity_Length
(N
: Node_Id
) return Boolean;
12461 -- Tests if N is a length attribute applied to a simple entity. If so,
12462 -- returns True, and sets Ent to the entity, and Index to the integer
12463 -- literal provided as an attribute expression, or to Empty if none.
12464 -- Also returns True if the expression is a generated type conversion
12465 -- whose expression is of the desired form. This latter case arises
12466 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
12467 -- to check for being in range, which is not needed in this context.
12468 -- Returns False if neither condition holds.
12470 function Prepare_64
(N
: Node_Id
) return Node_Id
;
12471 -- Given a discrete expression, returns a Long_Long_Integer typed
12472 -- expression representing the underlying value of the expression.
12473 -- This is done with an unchecked conversion to the result type. We
12474 -- use unchecked conversion to handle the enumeration type case.
12476 ----------------------
12477 -- Is_Entity_Length --
12478 ----------------------
12480 function Is_Entity_Length
(N
: Node_Id
) return Boolean is
12482 if Nkind
(N
) = N_Attribute_Reference
12483 and then Attribute_Name
(N
) = Name_Length
12484 and then Is_Entity_Name
(Prefix
(N
))
12486 Ent
:= Entity
(Prefix
(N
));
12488 if Present
(Expressions
(N
)) then
12489 Index
:= First
(Expressions
(N
));
12496 elsif Nkind
(N
) = N_Type_Conversion
12497 and then not Comes_From_Source
(N
)
12499 return Is_Entity_Length
(Expression
(N
));
12504 end Is_Entity_Length
;
12506 --------------------
12507 -- Is_Optimizable --
12508 --------------------
12510 function Is_Optimizable
(N
: Node_Id
) return Boolean is
12518 if Compile_Time_Known_Value
(N
) then
12519 Val
:= Expr_Value
(N
);
12521 if Val
= Uint_0
then
12526 elsif Val
= Uint_1
then
12533 -- Here we have to make sure of being within 32-bits
12535 Determine_Range
(N
, OK
, Lo
, Hi
, Assume_Valid
=> True);
12538 or else Lo
< Uint_1
12539 or else Hi
> UI_From_Int
(Int
'Last)
12544 -- Comparison value was within range, so now we must check the index
12545 -- value to make sure it is also within 32-bits.
12547 Indx
:= First_Index
(Etype
(Ent
));
12549 if Present
(Index
) then
12550 for J
in 2 .. UI_To_Int
(Intval
(Index
)) loop
12555 Ityp
:= Etype
(Indx
);
12557 if Esize
(Ityp
) > 32 then
12564 end Is_Optimizable
;
12570 function Prepare_64
(N
: Node_Id
) return Node_Id
is
12572 return Unchecked_Convert_To
(Standard_Long_Long_Integer
, N
);
12575 -- Start of processing for Optimize_Length_Comparison
12578 -- Nothing to do if not a comparison
12580 if Op
not in N_Op_Compare
then
12584 -- Nothing to do if special -gnatd.P debug flag set
12586 if Debug_Flag_Dot_PP
then
12590 -- Ent'Length op 0/1
12592 if Is_Entity_Length
(Left_Opnd
(N
))
12593 and then Is_Optimizable
(Right_Opnd
(N
))
12597 -- 0/1 op Ent'Length
12599 elsif Is_Entity_Length
(Right_Opnd
(N
))
12600 and then Is_Optimizable
(Left_Opnd
(N
))
12602 -- Flip comparison to opposite sense
12605 when N_Op_Lt
=> Op
:= N_Op_Gt
;
12606 when N_Op_Le
=> Op
:= N_Op_Ge
;
12607 when N_Op_Gt
=> Op
:= N_Op_Lt
;
12608 when N_Op_Ge
=> Op
:= N_Op_Le
;
12609 when others => null;
12612 -- Else optimization not possible
12618 -- Fall through if we will do the optimization
12620 -- Cases to handle:
12622 -- X'Length = 0 => X'First > X'Last
12623 -- X'Length = 1 => X'First = X'Last
12624 -- X'Length = n => X'First + (n - 1) = X'Last
12626 -- X'Length /= 0 => X'First <= X'Last
12627 -- X'Length /= 1 => X'First /= X'Last
12628 -- X'Length /= n => X'First + (n - 1) /= X'Last
12630 -- X'Length >= 0 => always true, warn
12631 -- X'Length >= 1 => X'First <= X'Last
12632 -- X'Length >= n => X'First + (n - 1) <= X'Last
12634 -- X'Length > 0 => X'First <= X'Last
12635 -- X'Length > 1 => X'First < X'Last
12636 -- X'Length > n => X'First + (n - 1) < X'Last
12638 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
12639 -- X'Length <= 1 => X'First >= X'Last
12640 -- X'Length <= n => X'First + (n - 1) >= X'Last
12642 -- X'Length < 0 => always false (warn)
12643 -- X'Length < 1 => X'First > X'Last
12644 -- X'Length < n => X'First + (n - 1) > X'Last
12646 -- Note: for the cases of n (not constant 0,1), we require that the
12647 -- corresponding index type be integer or shorter (i.e. not 64-bit),
12648 -- and the same for the comparison value. Then we do the comparison
12649 -- using 64-bit arithmetic (actually long long integer), so that we
12650 -- cannot have overflow intefering with the result.
12652 -- First deal with warning cases
12661 Convert_To
(Typ
, New_Occurrence_Of
(Standard_True
, Loc
)));
12662 Analyze_And_Resolve
(N
, Typ
);
12663 Warn_On_Known_Condition
(N
);
12670 Convert_To
(Typ
, New_Occurrence_Of
(Standard_False
, Loc
)));
12671 Analyze_And_Resolve
(N
, Typ
);
12672 Warn_On_Known_Condition
(N
);
12676 if Constant_Condition_Warnings
12677 and then Comes_From_Source
(Original_Node
(N
))
12679 Error_Msg_N
("could replace by ""'=""?c?", N
);
12689 -- Build the First reference we will use
12692 Make_Attribute_Reference
(Loc
,
12693 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
12694 Attribute_Name
=> Name_First
);
12696 if Present
(Index
) then
12697 Set_Expressions
(Left
, New_List
(New_Copy
(Index
)));
12700 -- If general value case, then do the addition of (n - 1), and
12701 -- also add the needed conversions to type Long_Long_Integer.
12703 if Present
(Comp
) then
12706 Left_Opnd
=> Prepare_64
(Left
),
12708 Make_Op_Subtract
(Loc
,
12709 Left_Opnd
=> Prepare_64
(Comp
),
12710 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)));
12713 -- Build the Last reference we will use
12716 Make_Attribute_Reference
(Loc
,
12717 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
12718 Attribute_Name
=> Name_Last
);
12720 if Present
(Index
) then
12721 Set_Expressions
(Right
, New_List
(New_Copy
(Index
)));
12724 -- If general operand, convert Last reference to Long_Long_Integer
12726 if Present
(Comp
) then
12727 Right
:= Prepare_64
(Right
);
12730 -- Check for cases to optimize
12732 -- X'Length = 0 => X'First > X'Last
12733 -- X'Length < 1 => X'First > X'Last
12734 -- X'Length < n => X'First + (n - 1) > X'Last
12736 if (Is_Zero
and then Op
= N_Op_Eq
)
12737 or else (not Is_Zero
and then Op
= N_Op_Lt
)
12742 Right_Opnd
=> Right
);
12744 -- X'Length = 1 => X'First = X'Last
12745 -- X'Length = n => X'First + (n - 1) = X'Last
12747 elsif not Is_Zero
and then Op
= N_Op_Eq
then
12751 Right_Opnd
=> Right
);
12753 -- X'Length /= 0 => X'First <= X'Last
12754 -- X'Length > 0 => X'First <= X'Last
12756 elsif Is_Zero
and (Op
= N_Op_Ne
or else Op
= N_Op_Gt
) then
12760 Right_Opnd
=> Right
);
12762 -- X'Length /= 1 => X'First /= X'Last
12763 -- X'Length /= n => X'First + (n - 1) /= X'Last
12765 elsif not Is_Zero
and then Op
= N_Op_Ne
then
12769 Right_Opnd
=> Right
);
12771 -- X'Length >= 1 => X'First <= X'Last
12772 -- X'Length >= n => X'First + (n - 1) <= X'Last
12774 elsif not Is_Zero
and then Op
= N_Op_Ge
then
12778 Right_Opnd
=> Right
);
12780 -- X'Length > 1 => X'First < X'Last
12781 -- X'Length > n => X'First + (n = 1) < X'Last
12783 elsif not Is_Zero
and then Op
= N_Op_Gt
then
12787 Right_Opnd
=> Right
);
12789 -- X'Length <= 1 => X'First >= X'Last
12790 -- X'Length <= n => X'First + (n - 1) >= X'Last
12792 elsif not Is_Zero
and then Op
= N_Op_Le
then
12796 Right_Opnd
=> Right
);
12798 -- Should not happen at this stage
12801 raise Program_Error
;
12804 -- Rewrite and finish up
12806 Rewrite
(N
, Result
);
12807 Analyze_And_Resolve
(N
, Typ
);
12809 end Optimize_Length_Comparison
;
12811 ------------------------------
12812 -- Process_Transient_Object --
12813 ------------------------------
12815 procedure Process_Transient_Object
12817 Rel_Node
: Node_Id
)
12819 Loc
: constant Source_Ptr
:= Sloc
(Decl
);
12820 Obj_Id
: constant Entity_Id
:= Defining_Identifier
(Decl
);
12821 Obj_Typ
: constant Node_Id
:= Etype
(Obj_Id
);
12822 Desig_Typ
: Entity_Id
;
12824 Hook_Id
: Entity_Id
;
12825 Hook_Insert
: Node_Id
;
12826 Ptr_Id
: Entity_Id
;
12828 Hook_Context
: constant Node_Id
:= Find_Hook_Context
(Rel_Node
);
12829 -- The node on which to insert the hook as an action. This is usually
12830 -- the innermost enclosing non-transient construct.
12832 Fin_Context
: Node_Id
;
12833 -- The node after which to insert the finalization actions of the
12834 -- transient controlled object.
12837 if Is_Boolean_Type
(Etype
(Rel_Node
)) then
12838 Fin_Context
:= Last
(Actions
(Rel_Node
));
12840 Fin_Context
:= Hook_Context
;
12843 -- Step 1: Create the access type which provides a reference to the
12844 -- transient controlled object.
12846 if Is_Access_Type
(Obj_Typ
) then
12847 Desig_Typ
:= Directly_Designated_Type
(Obj_Typ
);
12849 Desig_Typ
:= Obj_Typ
;
12852 Desig_Typ
:= Base_Type
(Desig_Typ
);
12855 -- Ann : access [all] <Desig_Typ>;
12857 Ptr_Id
:= Make_Temporary
(Loc
, 'A');
12859 Insert_Action
(Hook_Context
,
12860 Make_Full_Type_Declaration
(Loc
,
12861 Defining_Identifier
=> Ptr_Id
,
12863 Make_Access_To_Object_Definition
(Loc
,
12864 All_Present
=> Ekind
(Obj_Typ
) = E_General_Access_Type
,
12865 Subtype_Indication
=> New_Occurrence_Of
(Desig_Typ
, Loc
))));
12867 -- Step 2: Create a temporary which acts as a hook to the transient
12868 -- controlled object. Generate:
12870 -- Hook : Ptr_Id := null;
12872 Hook_Id
:= Make_Temporary
(Loc
, 'T');
12874 Insert_Action
(Hook_Context
,
12875 Make_Object_Declaration
(Loc
,
12876 Defining_Identifier
=> Hook_Id
,
12877 Object_Definition
=> New_Occurrence_Of
(Ptr_Id
, Loc
)));
12879 -- Mark the hook as created for the purposes of exporting the transient
12880 -- controlled object out of the expression_with_action or if expression.
12881 -- This signals the machinery in Build_Finalizer to treat this case in
12882 -- a special manner.
12884 Set_Status_Flag_Or_Transient_Decl
(Hook_Id
, Decl
);
12886 -- Step 3: Associate the transient object to the hook
12888 -- This must be inserted right after the object declaration, so that
12889 -- the assignment is executed if, and only if, the object is actually
12890 -- created (whereas the declaration of the hook pointer, and the
12891 -- finalization call, may be inserted at an outer level, and may
12892 -- remain unused for some executions, if the actual creation of
12893 -- the object is conditional).
12895 -- The use of unchecked conversion / unrestricted access is needed to
12896 -- avoid an accessibility violation. Note that the finalization code is
12897 -- structured in such a way that the "hook" is processed only when it
12898 -- points to an existing object.
12900 if Is_Access_Type
(Obj_Typ
) then
12902 Unchecked_Convert_To
12904 Expr
=> New_Occurrence_Of
(Obj_Id
, Loc
));
12907 Make_Attribute_Reference
(Loc
,
12908 Prefix
=> New_Occurrence_Of
(Obj_Id
, Loc
),
12909 Attribute_Name
=> Name_Unrestricted_Access
);
12913 -- Hook := Ptr_Id (Obj_Id);
12915 -- Hook := Obj_Id'Unrestricted_Access;
12917 -- When the transient object is initialized by an aggregate, the hook
12918 -- must capture the object after the last component assignment takes
12919 -- place. Only then is the object fully initialized.
12921 if Ekind
(Obj_Id
) = E_Variable
12922 and then Present
(Last_Aggregate_Assignment
(Obj_Id
))
12924 Hook_Insert
:= Last_Aggregate_Assignment
(Obj_Id
);
12926 -- Otherwise the hook seizes the related object immediately
12929 Hook_Insert
:= Decl
;
12932 Insert_After_And_Analyze
(Hook_Insert
,
12933 Make_Assignment_Statement
(Loc
,
12934 Name
=> New_Occurrence_Of
(Hook_Id
, Loc
),
12935 Expression
=> Expr
));
12937 -- Step 4: Finalize the hook after the context has been evaluated or
12938 -- elaborated. Generate:
12940 -- if Hook /= null then
12941 -- [Deep_]Finalize (Hook.all);
12945 -- When the node is part of a return statement, there is no need to
12946 -- insert a finalization call, as the general finalization mechanism
12947 -- (see Build_Finalizer) would take care of the transient controlled
12948 -- object on subprogram exit. Note that it would also be impossible to
12949 -- insert the finalization code after the return statement as this will
12950 -- render it unreachable.
12952 if Nkind
(Fin_Context
) = N_Simple_Return_Statement
then
12955 -- Otherwise finalize the hook
12958 Insert_Action_After
(Fin_Context
,
12959 Make_Implicit_If_Statement
(Decl
,
12962 Left_Opnd
=> New_Occurrence_Of
(Hook_Id
, Loc
),
12963 Right_Opnd
=> Make_Null
(Loc
)),
12965 Then_Statements
=> New_List
(
12968 Make_Explicit_Dereference
(Loc
,
12969 Prefix
=> New_Occurrence_Of
(Hook_Id
, Loc
)),
12972 Make_Assignment_Statement
(Loc
,
12973 Name
=> New_Occurrence_Of
(Hook_Id
, Loc
),
12974 Expression
=> Make_Null
(Loc
)))));
12976 end Process_Transient_Object
;
12978 ------------------------
12979 -- Rewrite_Comparison --
12980 ------------------------
12982 procedure Rewrite_Comparison
(N
: Node_Id
) is
12983 Warning_Generated
: Boolean := False;
12984 -- Set to True if first pass with Assume_Valid generates a warning in
12985 -- which case we skip the second pass to avoid warning overloaded.
12988 -- Set to Standard_True or Standard_False
12991 if Nkind
(N
) = N_Type_Conversion
then
12992 Rewrite_Comparison
(Expression
(N
));
12995 elsif Nkind
(N
) not in N_Op_Compare
then
12999 -- Now start looking at the comparison in detail. We potentially go
13000 -- through this loop twice. The first time, Assume_Valid is set False
13001 -- in the call to Compile_Time_Compare. If this call results in a
13002 -- clear result of always True or Always False, that's decisive and
13003 -- we are done. Otherwise we repeat the processing with Assume_Valid
13004 -- set to True to generate additional warnings. We can skip that step
13005 -- if Constant_Condition_Warnings is False.
13007 for AV
in False .. True loop
13009 Typ
: constant Entity_Id
:= Etype
(N
);
13010 Op1
: constant Node_Id
:= Left_Opnd
(N
);
13011 Op2
: constant Node_Id
:= Right_Opnd
(N
);
13013 Res
: constant Compare_Result
:=
13014 Compile_Time_Compare
(Op1
, Op2
, Assume_Valid
=> AV
);
13015 -- Res indicates if compare outcome can be compile time determined
13017 True_Result
: Boolean;
13018 False_Result
: Boolean;
13021 case N_Op_Compare
(Nkind
(N
)) is
13023 True_Result
:= Res
= EQ
;
13024 False_Result
:= Res
= LT
or else Res
= GT
or else Res
= NE
;
13027 True_Result
:= Res
in Compare_GE
;
13028 False_Result
:= Res
= LT
;
13031 and then Constant_Condition_Warnings
13032 and then Comes_From_Source
(Original_Node
(N
))
13033 and then Nkind
(Original_Node
(N
)) = N_Op_Ge
13034 and then not In_Instance
13035 and then Is_Integer_Type
(Etype
(Left_Opnd
(N
)))
13036 and then not Has_Warnings_Off
(Etype
(Left_Opnd
(N
)))
13039 ("can never be greater than, could replace by ""'=""?c?",
13041 Warning_Generated
:= True;
13045 True_Result
:= Res
= GT
;
13046 False_Result
:= Res
in Compare_LE
;
13049 True_Result
:= Res
= LT
;
13050 False_Result
:= Res
in Compare_GE
;
13053 True_Result
:= Res
in Compare_LE
;
13054 False_Result
:= Res
= GT
;
13057 and then Constant_Condition_Warnings
13058 and then Comes_From_Source
(Original_Node
(N
))
13059 and then Nkind
(Original_Node
(N
)) = N_Op_Le
13060 and then not In_Instance
13061 and then Is_Integer_Type
(Etype
(Left_Opnd
(N
)))
13062 and then not Has_Warnings_Off
(Etype
(Left_Opnd
(N
)))
13065 ("can never be less than, could replace by ""'=""?c?", N
);
13066 Warning_Generated
:= True;
13070 True_Result
:= Res
= NE
or else Res
= GT
or else Res
= LT
;
13071 False_Result
:= Res
= EQ
;
13074 -- If this is the first iteration, then we actually convert the
13075 -- comparison into True or False, if the result is certain.
13078 if True_Result
or False_Result
then
13079 Result
:= Boolean_Literals
(True_Result
);
13082 New_Occurrence_Of
(Result
, Sloc
(N
))));
13083 Analyze_And_Resolve
(N
, Typ
);
13084 Warn_On_Known_Condition
(N
);
13088 -- If this is the second iteration (AV = True), and the original
13089 -- node comes from source and we are not in an instance, then give
13090 -- a warning if we know result would be True or False. Note: we
13091 -- know Constant_Condition_Warnings is set if we get here.
13093 elsif Comes_From_Source
(Original_Node
(N
))
13094 and then not In_Instance
13096 if True_Result
then
13098 ("condition can only be False if invalid values present??",
13100 elsif False_Result
then
13102 ("condition can only be True if invalid values present??",
13108 -- Skip second iteration if not warning on constant conditions or
13109 -- if the first iteration already generated a warning of some kind or
13110 -- if we are in any case assuming all values are valid (so that the
13111 -- first iteration took care of the valid case).
13113 exit when not Constant_Condition_Warnings
;
13114 exit when Warning_Generated
;
13115 exit when Assume_No_Invalid_Values
;
13117 end Rewrite_Comparison
;
13119 ----------------------------
13120 -- Safe_In_Place_Array_Op --
13121 ----------------------------
13123 function Safe_In_Place_Array_Op
13126 Op2
: Node_Id
) return Boolean
13128 Target
: Entity_Id
;
13130 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean;
13131 -- Operand is safe if it cannot overlap part of the target of the
13132 -- operation. If the operand and the target are identical, the operand
13133 -- is safe. The operand can be empty in the case of negation.
13135 function Is_Unaliased
(N
: Node_Id
) return Boolean;
13136 -- Check that N is a stand-alone entity
13142 function Is_Unaliased
(N
: Node_Id
) return Boolean is
13146 and then No
(Address_Clause
(Entity
(N
)))
13147 and then No
(Renamed_Object
(Entity
(N
)));
13150 ---------------------
13151 -- Is_Safe_Operand --
13152 ---------------------
13154 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean is
13159 elsif Is_Entity_Name
(Op
) then
13160 return Is_Unaliased
(Op
);
13162 elsif Nkind_In
(Op
, N_Indexed_Component
, N_Selected_Component
) then
13163 return Is_Unaliased
(Prefix
(Op
));
13165 elsif Nkind
(Op
) = N_Slice
then
13167 Is_Unaliased
(Prefix
(Op
))
13168 and then Entity
(Prefix
(Op
)) /= Target
;
13170 elsif Nkind
(Op
) = N_Op_Not
then
13171 return Is_Safe_Operand
(Right_Opnd
(Op
));
13176 end Is_Safe_Operand
;
13178 -- Start of processing for Safe_In_Place_Array_Op
13181 -- Skip this processing if the component size is different from system
13182 -- storage unit (since at least for NOT this would cause problems).
13184 if Component_Size
(Etype
(Lhs
)) /= System_Storage_Unit
then
13187 -- Cannot do in place stuff on VM_Target since cannot pass addresses
13189 elsif VM_Target
/= No_VM
then
13192 -- Cannot do in place stuff if non-standard Boolean representation
13194 elsif Has_Non_Standard_Rep
(Component_Type
(Etype
(Lhs
))) then
13197 elsif not Is_Unaliased
(Lhs
) then
13201 Target
:= Entity
(Lhs
);
13202 return Is_Safe_Operand
(Op1
) and then Is_Safe_Operand
(Op2
);
13204 end Safe_In_Place_Array_Op
;
13206 -----------------------
13207 -- Tagged_Membership --
13208 -----------------------
13210 -- There are two different cases to consider depending on whether the right
13211 -- operand is a class-wide type or not. If not we just compare the actual
13212 -- tag of the left expr to the target type tag:
13214 -- Left_Expr.Tag = Right_Type'Tag;
13216 -- If it is a class-wide type we use the RT function CW_Membership which is
13217 -- usually implemented by looking in the ancestor tables contained in the
13218 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
13220 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
13221 -- function IW_Membership which is usually implemented by looking in the
13222 -- table of abstract interface types plus the ancestor table contained in
13223 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
13225 procedure Tagged_Membership
13227 SCIL_Node
: out Node_Id
;
13228 Result
: out Node_Id
)
13230 Left
: constant Node_Id
:= Left_Opnd
(N
);
13231 Right
: constant Node_Id
:= Right_Opnd
(N
);
13232 Loc
: constant Source_Ptr
:= Sloc
(N
);
13234 Full_R_Typ
: Entity_Id
;
13235 Left_Type
: Entity_Id
;
13236 New_Node
: Node_Id
;
13237 Right_Type
: Entity_Id
;
13241 SCIL_Node
:= Empty
;
13243 -- Handle entities from the limited view
13245 Left_Type
:= Available_View
(Etype
(Left
));
13246 Right_Type
:= Available_View
(Etype
(Right
));
13248 -- In the case where the type is an access type, the test is applied
13249 -- using the designated types (needed in Ada 2012 for implicit anonymous
13250 -- access conversions, for AI05-0149).
13252 if Is_Access_Type
(Right_Type
) then
13253 Left_Type
:= Designated_Type
(Left_Type
);
13254 Right_Type
:= Designated_Type
(Right_Type
);
13257 if Is_Class_Wide_Type
(Left_Type
) then
13258 Left_Type
:= Root_Type
(Left_Type
);
13261 if Is_Class_Wide_Type
(Right_Type
) then
13262 Full_R_Typ
:= Underlying_Type
(Root_Type
(Right_Type
));
13264 Full_R_Typ
:= Underlying_Type
(Right_Type
);
13268 Make_Selected_Component
(Loc
,
13269 Prefix
=> Relocate_Node
(Left
),
13271 New_Occurrence_Of
(First_Tag_Component
(Left_Type
), Loc
));
13273 if Is_Class_Wide_Type
(Right_Type
) then
13275 -- No need to issue a run-time check if we statically know that the
13276 -- result of this membership test is always true. For example,
13277 -- considering the following declarations:
13279 -- type Iface is interface;
13280 -- type T is tagged null record;
13281 -- type DT is new T and Iface with null record;
13286 -- These membership tests are always true:
13289 -- Obj2 in T'Class;
13290 -- Obj2 in Iface'Class;
13292 -- We do not need to handle cases where the membership is illegal.
13295 -- Obj1 in DT'Class; -- Compile time error
13296 -- Obj1 in Iface'Class; -- Compile time error
13298 if not Is_Class_Wide_Type
(Left_Type
)
13299 and then (Is_Ancestor
(Etype
(Right_Type
), Left_Type
,
13300 Use_Full_View
=> True)
13301 or else (Is_Interface
(Etype
(Right_Type
))
13302 and then Interface_Present_In_Ancestor
13304 Iface
=> Etype
(Right_Type
))))
13306 Result
:= New_Occurrence_Of
(Standard_True
, Loc
);
13310 -- Ada 2005 (AI-251): Class-wide applied to interfaces
13312 if Is_Interface
(Etype
(Class_Wide_Type
(Right_Type
)))
13314 -- Support to: "Iface_CW_Typ in Typ'Class"
13316 or else Is_Interface
(Left_Type
)
13318 -- Issue error if IW_Membership operation not available in a
13319 -- configurable run time setting.
13321 if not RTE_Available
(RE_IW_Membership
) then
13323 ("dynamic membership test on interface types", N
);
13329 Make_Function_Call
(Loc
,
13330 Name
=> New_Occurrence_Of
(RTE
(RE_IW_Membership
), Loc
),
13331 Parameter_Associations
=> New_List
(
13332 Make_Attribute_Reference
(Loc
,
13334 Attribute_Name
=> Name_Address
),
13335 New_Occurrence_Of
(
13336 Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))),
13339 -- Ada 95: Normal case
13342 Build_CW_Membership
(Loc
,
13343 Obj_Tag_Node
=> Obj_Tag
,
13345 New_Occurrence_Of
(
13346 Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))), Loc
),
13348 New_Node
=> New_Node
);
13350 -- Generate the SCIL node for this class-wide membership test.
13351 -- Done here because the previous call to Build_CW_Membership
13352 -- relocates Obj_Tag.
13354 if Generate_SCIL
then
13355 SCIL_Node
:= Make_SCIL_Membership_Test
(Sloc
(N
));
13356 Set_SCIL_Entity
(SCIL_Node
, Etype
(Right_Type
));
13357 Set_SCIL_Tag_Value
(SCIL_Node
, Obj_Tag
);
13360 Result
:= New_Node
;
13363 -- Right_Type is not a class-wide type
13366 -- No need to check the tag of the object if Right_Typ is abstract
13368 if Is_Abstract_Type
(Right_Type
) then
13369 Result
:= New_Occurrence_Of
(Standard_False
, Loc
);
13374 Left_Opnd
=> Obj_Tag
,
13377 (Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))), Loc
));
13380 end Tagged_Membership
;
13382 ------------------------------
13383 -- Unary_Op_Validity_Checks --
13384 ------------------------------
13386 procedure Unary_Op_Validity_Checks
(N
: Node_Id
) is
13388 if Validity_Checks_On
and Validity_Check_Operands
then
13389 Ensure_Valid
(Right_Opnd
(N
));
13391 end Unary_Op_Validity_Checks
;