1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, 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 Accessibility
; use Accessibility
;
27 with Aspects
; use Aspects
;
28 with Atree
; use Atree
;
29 with Checks
; use Checks
;
30 with Debug
; use Debug
;
31 with Einfo
; use Einfo
;
32 with Einfo
.Entities
; use Einfo
.Entities
;
33 with Einfo
.Utils
; use Einfo
.Utils
;
34 with Elists
; use Elists
;
35 with Errout
; use Errout
;
36 with Exp_Aggr
; use Exp_Aggr
;
37 with Exp_Ch3
; use Exp_Ch3
;
38 with Exp_Ch6
; use Exp_Ch6
;
39 with Exp_Ch7
; use Exp_Ch7
;
40 with Exp_Ch9
; use Exp_Ch9
;
41 with Exp_Disp
; use Exp_Disp
;
42 with Exp_Fixd
; use Exp_Fixd
;
43 with Exp_Intr
; use Exp_Intr
;
44 with Exp_Pakd
; use Exp_Pakd
;
45 with Exp_Tss
; use Exp_Tss
;
46 with Exp_Util
; use Exp_Util
;
47 with Freeze
; use Freeze
;
48 with Inline
; use Inline
;
50 with Namet
; use Namet
;
51 with Nlists
; use Nlists
;
52 with Nmake
; use Nmake
;
54 with Par_SCO
; use Par_SCO
;
55 with Restrict
; use Restrict
;
56 with Rident
; use Rident
;
57 with Rtsfind
; use Rtsfind
;
59 with Sem_Aux
; use Sem_Aux
;
60 with Sem_Cat
; use Sem_Cat
;
61 with Sem_Ch3
; use Sem_Ch3
;
62 with Sem_Ch13
; use Sem_Ch13
;
63 with Sem_Eval
; use Sem_Eval
;
64 with Sem_Res
; use Sem_Res
;
65 with Sem_Type
; use Sem_Type
;
66 with Sem_Util
; use Sem_Util
;
67 with Sem_Warn
; use Sem_Warn
;
68 with Sinfo
; use Sinfo
;
69 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
70 with Sinfo
.Utils
; use Sinfo
.Utils
;
71 with Snames
; use Snames
;
72 with Stand
; use Stand
;
73 with SCIL_LL
; use SCIL_LL
;
74 with Targparm
; use Targparm
;
75 with Tbuild
; use Tbuild
;
76 with Ttypes
; use Ttypes
;
77 with Uintp
; use Uintp
;
78 with Urealp
; use Urealp
;
79 with Validsw
; use Validsw
;
80 with Warnsw
; use Warnsw
;
82 package body Exp_Ch4
is
84 Too_Large_Length_For_Array
: constant Unat
:= Uint_256
;
85 -- Threshold from which we do not try to create static array temporaries in
86 -- order to eliminate dynamic stack allocations.
88 -----------------------
89 -- Local Subprograms --
90 -----------------------
92 procedure Binary_Op_Validity_Checks
(N
: Node_Id
);
93 pragma Inline
(Binary_Op_Validity_Checks
);
94 -- Performs validity checks for a binary operator
96 procedure Build_Boolean_Array_Proc_Call
100 -- If a boolean array assignment can be done in place, build call to
101 -- corresponding library procedure.
103 procedure Displace_Allocator_Pointer
(N
: Node_Id
);
104 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
105 -- Expand_Allocator_Expression. Allocating class-wide interface objects
106 -- this routine displaces the pointer to the allocated object to reference
107 -- the component referencing the corresponding secondary dispatch table.
109 procedure Expand_Allocator_Expression
(N
: Node_Id
);
110 -- Subsidiary to Expand_N_Allocator, for the case when the expression
111 -- is a qualified expression.
113 procedure Expand_Array_Comparison
(N
: Node_Id
);
114 -- This routine handles expansion of the comparison operators (N_Op_Lt,
115 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
116 -- code for these operators is similar, differing only in the details of
117 -- the actual comparison call that is made. Special processing (call a
120 function Expand_Array_Equality
125 Typ
: Entity_Id
) return Node_Id
;
126 -- Expand an array equality into a call to a function implementing this
127 -- equality, and a call to it. Loc is the location for the generated nodes.
128 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
129 -- on which to attach bodies of local functions that are created in the
130 -- process. It is the responsibility of the caller to insert those bodies
131 -- at the right place. Nod provides the Sloc value for the generated code.
132 -- Normally the types used for the generated equality routine are taken
133 -- from Lhs and Rhs. However, in some situations of generated code, the
134 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
135 -- the type to be used for the formal parameters.
137 procedure Expand_Boolean_Operator
(N
: Node_Id
);
138 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
139 -- case of array type arguments.
141 procedure Expand_Nonbinary_Modular_Op
(N
: Node_Id
);
142 -- When generating C code, convert nonbinary modular arithmetic operations
143 -- into code that relies on the front-end expansion of operator Mod. No
144 -- expansion is performed if N is not a nonbinary modular operand.
146 procedure Expand_Short_Circuit_Operator
(N
: Node_Id
);
147 -- Common expansion processing for short-circuit boolean operators
149 procedure Expand_Compare_Minimize_Eliminate_Overflow
(N
: Node_Id
);
150 -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is
151 -- where we allow comparison of "out of range" values.
153 function Expand_Composite_Equality
154 (Outer_Type
: Entity_Id
;
156 Comp_Type
: Entity_Id
;
158 Rhs
: Node_Id
) return Node_Id
;
159 -- Local recursive function used to expand equality for nested composite
160 -- types. Used by Expand_Record/Array_Equality. Nod provides the Sloc value
161 -- for generated code. Lhs and Rhs are the left and right sides for the
162 -- comparison, and Comp_Typ is the type of the objects to compare.
163 -- Outer_Type is the composite type containing a component of type
164 -- Comp_Type -- used for printing messages.
166 procedure Expand_Concatenate
(Cnode
: Node_Id
; Opnds
: List_Id
);
167 -- Routine to expand concatenation of a sequence of two or more operands
168 -- (in the list Operands) and replace node Cnode with the result of the
169 -- concatenation. The operands can be of any appropriate type, and can
170 -- include both arrays and singleton elements.
172 procedure Expand_Membership_Minimize_Eliminate_Overflow
(N
: Node_Id
);
173 -- N is an N_In membership test mode, with the overflow check mode set to
174 -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed
175 -- integer type. This is a case where top level processing is required to
176 -- handle overflow checks in subtrees.
178 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
);
179 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
180 -- fixed. We do not have such a type at runtime, so the purpose of this
181 -- routine is to find the real type by looking up the tree. We also
182 -- determine if the operation must be rounded.
184 procedure Get_First_Index_Bounds
(T
: Entity_Id
; Lo
, Hi
: out Uint
);
185 -- T is an array whose index bounds are all known at compile time. Return
186 -- the value of the low and high bounds of the first index of T.
188 function Get_Size_For_Range
(Lo
, Hi
: Uint
) return Uint
;
189 -- Return the size of a small signed integer type covering Lo .. Hi, the
190 -- main goal being to return a size lower than that of standard types.
192 procedure Insert_Dereference_Action
(N
: Node_Id
);
193 -- N is an expression whose type is an access. When the type of the
194 -- associated storage pool is derived from Checked_Pool, generate a
195 -- call to the 'Dereference' primitive operation.
197 function Make_Array_Comparison_Op
199 Nod
: Node_Id
) return Node_Id
;
200 -- Comparisons between arrays are expanded in line. This function produces
201 -- the body of the implementation of (a > b), where a and b are one-
202 -- dimensional arrays of some discrete type. The original node is then
203 -- expanded into the appropriate call to this function. Nod provides the
204 -- Sloc value for the generated code.
206 function Make_Boolean_Array_Op
208 N
: Node_Id
) return Node_Id
;
209 -- Boolean operations on boolean arrays are expanded in line. This function
210 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
211 -- b). It is used only the normal case and not the packed case. The type
212 -- involved, Typ, is the Boolean array type, and the logical operations in
213 -- the body are simple boolean operations. Note that Typ is always a
214 -- constrained type (the caller has ensured this by using
215 -- Convert_To_Actual_Subtype if necessary).
217 function Minimized_Eliminated_Overflow_Check
(N
: Node_Id
) return Boolean;
218 -- For signed arithmetic operations when the current overflow mode is
219 -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks
220 -- as the first thing we do. We then return. We count on the recursive
221 -- apparatus for overflow checks to call us back with an equivalent
222 -- operation that is in CHECKED mode, avoiding a recursive entry into this
223 -- routine, and that is when we will proceed with the expansion of the
224 -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do
225 -- these optimizations without first making this check, since there may be
226 -- operands further down the tree that are relying on the recursive calls
227 -- triggered by the top level nodes to properly process overflow checking
228 -- and remaining expansion on these nodes. Note that this call back may be
229 -- skipped if the operation is done in Bignum mode but that's fine, since
230 -- the Bignum call takes care of everything.
232 procedure Narrow_Large_Operation
(N
: Node_Id
);
233 -- Try to compute the result of a large operation in a narrower type than
234 -- its nominal type. This is mainly aimed at getting rid of operations done
235 -- in Universal_Integer that can be generated for attributes.
237 procedure Optimize_Length_Comparison
(N
: Node_Id
);
238 -- Given an expression, if it is of the form X'Length op N (or the other
239 -- way round), where N is known at compile time to be 0 or 1, or something
240 -- else where the value is known to be nonnegative and in the 32-bit range,
241 -- and X is a simple entity, and op is a comparison operator, optimizes it
242 -- into a comparison of X'First and X'Last.
244 procedure Process_If_Case_Statements
(N
: Node_Id
; Stmts
: List_Id
);
245 -- Inspect and process statement list Stmt of if or case expression N for
246 -- transient objects. If such objects are found, the routine generates code
247 -- to clean them up when the context of the expression is evaluated.
249 procedure Process_Transient_In_Expression
253 -- Subsidiary routine to the expansion of expression_with_actions, if and
254 -- case expressions. Generate all necessary code to finalize a transient
255 -- object when the enclosing context is elaborated or evaluated. Obj_Decl
256 -- denotes the declaration of the transient object, which is usually the
257 -- result of a controlled function call. Expr denotes the expression with
258 -- actions, if expression, or case expression node. Stmts denotes the
259 -- statement list which contains Decl, either at the top level or within a
262 procedure Rewrite_Comparison
(N
: Node_Id
);
263 -- If N is the node for a comparison whose outcome can be determined at
264 -- compile time, then the node N can be rewritten with True or False. If
265 -- the outcome cannot be determined at compile time, the call has no
266 -- effect. If N is a type conversion, then this processing is applied to
267 -- its expression. If N is neither comparison nor a type conversion, the
268 -- call has no effect.
270 procedure Tagged_Membership
272 SCIL_Node
: out Node_Id
;
273 Result
: out Node_Id
);
274 -- Construct the expression corresponding to the tagged membership test.
275 -- Deals with a second operand being (or not) a class-wide type.
277 function Safe_In_Place_Array_Op
280 Op2
: Node_Id
) return Boolean;
281 -- In the context of an assignment, where the right-hand side is a boolean
282 -- operation on arrays, check whether operation can be performed in place.
284 procedure Unary_Op_Validity_Checks
(N
: Node_Id
);
285 pragma Inline
(Unary_Op_Validity_Checks
);
286 -- Performs validity checks for a unary operator
288 -------------------------------
289 -- Binary_Op_Validity_Checks --
290 -------------------------------
292 procedure Binary_Op_Validity_Checks
(N
: Node_Id
) is
294 if Validity_Checks_On
and Validity_Check_Operands
then
295 Ensure_Valid
(Left_Opnd
(N
));
296 Ensure_Valid
(Right_Opnd
(N
));
298 end Binary_Op_Validity_Checks
;
300 ------------------------------------
301 -- Build_Boolean_Array_Proc_Call --
302 ------------------------------------
304 procedure Build_Boolean_Array_Proc_Call
309 Loc
: constant Source_Ptr
:= Sloc
(N
);
310 Kind
: constant Node_Kind
:= Nkind
(Expression
(N
));
311 Target
: constant Node_Id
:=
312 Make_Attribute_Reference
(Loc
,
314 Attribute_Name
=> Name_Address
);
316 Arg1
: Node_Id
:= Op1
;
317 Arg2
: Node_Id
:= Op2
;
319 Proc_Name
: Entity_Id
;
322 if Kind
= N_Op_Not
then
323 if Nkind
(Op1
) in N_Binary_Op
then
325 -- Use negated version of the binary operators
327 if Nkind
(Op1
) = N_Op_And
then
328 Proc_Name
:= RTE
(RE_Vector_Nand
);
330 elsif Nkind
(Op1
) = N_Op_Or
then
331 Proc_Name
:= RTE
(RE_Vector_Nor
);
333 else pragma Assert
(Nkind
(Op1
) = N_Op_Xor
);
334 Proc_Name
:= RTE
(RE_Vector_Xor
);
338 Make_Procedure_Call_Statement
(Loc
,
339 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
341 Parameter_Associations
=> New_List
(
343 Make_Attribute_Reference
(Loc
,
344 Prefix
=> Left_Opnd
(Op1
),
345 Attribute_Name
=> Name_Address
),
347 Make_Attribute_Reference
(Loc
,
348 Prefix
=> Right_Opnd
(Op1
),
349 Attribute_Name
=> Name_Address
),
351 Make_Attribute_Reference
(Loc
,
352 Prefix
=> Left_Opnd
(Op1
),
353 Attribute_Name
=> Name_Length
)));
356 Proc_Name
:= RTE
(RE_Vector_Not
);
359 Make_Procedure_Call_Statement
(Loc
,
360 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
361 Parameter_Associations
=> New_List
(
364 Make_Attribute_Reference
(Loc
,
366 Attribute_Name
=> Name_Address
),
368 Make_Attribute_Reference
(Loc
,
370 Attribute_Name
=> Name_Length
)));
374 -- We use the following equivalences:
376 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
377 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
378 -- (not X) xor (not Y) = X xor Y
379 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
381 if Nkind
(Op1
) = N_Op_Not
then
382 Arg1
:= Right_Opnd
(Op1
);
383 Arg2
:= Right_Opnd
(Op2
);
385 if Kind
= N_Op_And
then
386 Proc_Name
:= RTE
(RE_Vector_Nor
);
387 elsif Kind
= N_Op_Or
then
388 Proc_Name
:= RTE
(RE_Vector_Nand
);
390 Proc_Name
:= RTE
(RE_Vector_Xor
);
394 if Kind
= N_Op_And
then
395 Proc_Name
:= RTE
(RE_Vector_And
);
396 elsif Kind
= N_Op_Or
then
397 Proc_Name
:= RTE
(RE_Vector_Or
);
398 elsif Nkind
(Op2
) = N_Op_Not
then
399 Proc_Name
:= RTE
(RE_Vector_Nxor
);
400 Arg2
:= Right_Opnd
(Op2
);
402 Proc_Name
:= RTE
(RE_Vector_Xor
);
407 Make_Procedure_Call_Statement
(Loc
,
408 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
409 Parameter_Associations
=> New_List
(
411 Make_Attribute_Reference
(Loc
,
413 Attribute_Name
=> Name_Address
),
414 Make_Attribute_Reference
(Loc
,
416 Attribute_Name
=> Name_Address
),
417 Make_Attribute_Reference
(Loc
,
419 Attribute_Name
=> Name_Length
)));
422 Rewrite
(N
, Call_Node
);
426 when RE_Not_Available
=>
428 end Build_Boolean_Array_Proc_Call
;
430 -----------------------
432 -----------------------
434 function Build_Eq_Call
438 Rhs
: Node_Id
) return Node_Id
440 Eq
: constant Entity_Id
:= Get_User_Defined_Equality
(Typ
);
444 if Is_Abstract_Subprogram
(Eq
) then
445 return Make_Raise_Program_Error
(Loc
,
446 Reason
=> PE_Explicit_Raise
);
450 Make_Function_Call
(Loc
,
451 Name
=> New_Occurrence_Of
(Eq
, Loc
),
452 Parameter_Associations
=> New_List
(Lhs
, Rhs
));
456 -- If not found, predefined operation will be used
461 --------------------------------
462 -- Displace_Allocator_Pointer --
463 --------------------------------
465 procedure Displace_Allocator_Pointer
(N
: Node_Id
) is
466 Loc
: constant Source_Ptr
:= Sloc
(N
);
467 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
473 -- Do nothing in case of VM targets: the virtual machine will handle
474 -- interfaces directly.
476 if not Tagged_Type_Expansion
then
480 pragma Assert
(Nkind
(N
) = N_Identifier
481 and then Nkind
(Orig_Node
) = N_Allocator
);
483 PtrT
:= Etype
(Orig_Node
);
484 Dtyp
:= Available_View
(Designated_Type
(PtrT
));
485 Etyp
:= Etype
(Expression
(Orig_Node
));
487 if Is_Class_Wide_Type
(Dtyp
) and then Is_Interface
(Dtyp
) then
489 -- If the type of the allocator expression is not an interface type
490 -- we can generate code to reference the record component containing
491 -- the pointer to the secondary dispatch table.
493 if not Is_Interface
(Etyp
) then
495 Saved_Typ
: constant Entity_Id
:= Etype
(Orig_Node
);
498 -- 1) Get access to the allocated object
501 Make_Explicit_Dereference
(Loc
, Relocate_Node
(N
)));
505 -- 2) Add the conversion to displace the pointer to reference
506 -- the secondary dispatch table.
508 Rewrite
(N
, Convert_To
(Dtyp
, Relocate_Node
(N
)));
509 Analyze_And_Resolve
(N
, Dtyp
);
511 -- 3) The 'access to the secondary dispatch table will be used
512 -- as the value returned by the allocator.
515 Make_Attribute_Reference
(Loc
,
516 Prefix
=> Relocate_Node
(N
),
517 Attribute_Name
=> Name_Access
));
518 Set_Etype
(N
, Saved_Typ
);
522 -- If the type of the allocator expression is an interface type we
523 -- generate a run-time call to displace "this" to reference the
524 -- component containing the pointer to the secondary dispatch table
525 -- or else raise Constraint_Error if the actual object does not
526 -- implement the target interface. This case corresponds to the
527 -- following example:
529 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
531 -- return new Iface_2'Class'(Obj);
536 Unchecked_Convert_To
(PtrT
,
537 Make_Function_Call
(Loc
,
538 Name
=> New_Occurrence_Of
(RTE
(RE_Displace
), Loc
),
539 Parameter_Associations
=> New_List
(
540 Unchecked_Convert_To
(RTE
(RE_Address
),
546 (Access_Disp_Table
(Etype
(Base_Type
(Dtyp
))))),
548 Analyze_And_Resolve
(N
, PtrT
);
551 end Displace_Allocator_Pointer
;
553 ---------------------------------
554 -- Expand_Allocator_Expression --
555 ---------------------------------
557 procedure Expand_Allocator_Expression
(N
: Node_Id
) is
558 Loc
: constant Source_Ptr
:= Sloc
(N
);
559 Exp
: constant Node_Id
:= Expression
(Expression
(N
));
560 PtrT
: constant Entity_Id
:= Etype
(N
);
561 DesigT
: constant Entity_Id
:= Designated_Type
(PtrT
);
565 Indic
: constant Node_Id
:= Subtype_Mark
(Expression
(N
));
566 T
: constant Entity_Id
:= Entity
(Indic
);
568 Aggr_In_Place
: Boolean;
570 Tag_Assign
: Node_Id
;
574 TagT
: Entity_Id
:= Empty
;
575 -- Type used as source for tag assignment
577 TagR
: Node_Id
:= Empty
;
578 -- Target reference for tag assignment
580 -- Start of processing for Expand_Allocator_Expression
583 -- Handle call to C++ constructor
585 if Is_CPP_Constructor_Call
(Exp
) then
586 Make_CPP_Constructor_Call_In_Allocator
588 Function_Call
=> Exp
);
593 -- type A is access T1;
594 -- X : A := new T2'(...);
595 -- T1 and T2 can be different subtypes, and we might need to check
596 -- both constraints. First check against the type of the qualified
599 Apply_Constraint_Check
(Exp
, T
, No_Sliding
=> True);
601 Apply_Predicate_Check
(Exp
, T
);
603 -- Check that any anonymous access discriminants are suitable
604 -- for use in an allocator.
606 -- Note: This check is performed here instead of during analysis so that
607 -- we can check against the fully resolved etype of Exp.
609 if Is_Entity_Name
(Exp
)
610 and then Has_Anonymous_Access_Discriminant
(Etype
(Exp
))
611 and then Static_Accessibility_Level
(Exp
, Object_Decl_Level
)
612 > Static_Accessibility_Level
(N
, Object_Decl_Level
)
614 -- A dynamic check and a warning are generated when we are within
619 Make_Raise_Program_Error
(Loc
,
620 Reason
=> PE_Accessibility_Check_Failed
));
622 Error_Msg_Warn
:= SPARK_Mode
/= On
;
623 Error_Msg_N
("anonymous access discriminant is too deep for use"
624 & " in allocator<<", N
);
625 Error_Msg_N
("\Program_Error [<<", N
);
627 -- Otherwise, make the error static
630 Error_Msg_N
("anonymous access discriminant is too deep for use"
631 & " in allocator", N
);
635 if Do_Range_Check
(Exp
) then
636 Generate_Range_Check
(Exp
, T
, CE_Range_Check_Failed
);
639 -- A check is also needed in cases where the designated subtype is
640 -- constrained and differs from the subtype given in the qualified
641 -- expression. Note that the check on the qualified expression does
642 -- not allow sliding, but this check does (a relaxation from Ada 83).
644 if Is_Constrained
(DesigT
)
645 and then not Subtypes_Statically_Match
(T
, DesigT
)
647 Apply_Constraint_Check
(Exp
, DesigT
, No_Sliding
=> False);
649 Apply_Predicate_Check
(Exp
, DesigT
);
651 if Do_Range_Check
(Exp
) then
652 Generate_Range_Check
(Exp
, DesigT
, CE_Range_Check_Failed
);
656 if Nkind
(Exp
) = N_Raise_Constraint_Error
then
657 Rewrite
(N
, New_Copy
(Exp
));
662 Aggr_In_Place
:= Is_Delayed_Aggregate
(Exp
);
664 -- Case of tagged type or type requiring finalization
666 if Is_Tagged_Type
(T
) or else Needs_Finalization
(T
) then
668 -- Ada 2005 (AI-318-02): If the initialization expression is a call
669 -- to a build-in-place function, then access to the allocated object
670 -- must be passed to the function.
672 if Is_Build_In_Place_Function_Call
(Exp
) then
673 Make_Build_In_Place_Call_In_Allocator
(N
, Exp
);
674 Apply_Accessibility_Check_For_Allocator
675 (N
, Exp
, N
, Built_In_Place
=> True);
678 -- Ada 2005 (AI-318-02): Specialization of the previous case for
679 -- expressions containing a build-in-place function call whose
680 -- returned object covers interface types, and Expr has calls to
681 -- Ada.Tags.Displace to displace the pointer to the returned build-
682 -- in-place object to reference the secondary dispatch table of a
683 -- covered interface type.
685 elsif Present
(Unqual_BIP_Iface_Function_Call
(Exp
)) then
686 Make_Build_In_Place_Iface_Call_In_Allocator
(N
, Exp
);
687 Apply_Accessibility_Check_For_Allocator
688 (N
, Exp
, N
, Built_In_Place
=> True);
692 -- Actions inserted before:
693 -- Temp : constant ptr_T := new T'(Expression);
694 -- Temp._tag = T'tag; -- when not class-wide
695 -- [Deep_]Adjust (Temp.all);
697 -- We analyze by hand the new internal allocator to avoid any
698 -- recursion and inappropriate call to Initialize.
700 -- We don't want to remove side effects when the expression must be
701 -- built in place and we don't need it when there is no storage pool
702 -- or this is a return/secondary stack allocation.
705 and then Present
(Storage_Pool
(N
))
706 and then not Is_RTE
(Storage_Pool
(N
), RE_RS_Pool
)
707 and then not Is_RTE
(Storage_Pool
(N
), RE_SS_Pool
)
709 Remove_Side_Effects
(Exp
);
712 Temp
:= Make_Temporary
(Loc
, 'P', N
);
714 -- For a class wide allocation generate the following code:
716 -- type Equiv_Record is record ... end record;
717 -- implicit subtype CW is <Class_Wide_Subytpe>;
718 -- temp : PtrT := new CW'(CW!(expr));
720 if Is_Class_Wide_Type
(T
) then
721 Expand_Subtype_From_Expr
(Empty
, T
, Indic
, Exp
);
723 -- Ada 2005 (AI-251): If the expression is a class-wide interface
724 -- object we generate code to move up "this" to reference the
725 -- base of the object before allocating the new object.
727 -- Note that Exp'Address is recursively expanded into a call
728 -- to Base_Address (Exp.Tag)
730 if Is_Class_Wide_Type
(Etype
(Exp
))
731 and then Is_Interface
(Etype
(Exp
))
732 and then Tagged_Type_Expansion
736 Unchecked_Convert_To
(Entity
(Indic
),
737 Make_Explicit_Dereference
(Loc
,
738 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
739 Make_Attribute_Reference
(Loc
,
741 Attribute_Name
=> Name_Address
)))));
745 Unchecked_Convert_To
(Entity
(Indic
), Exp
));
748 Analyze_And_Resolve
(Expression
(N
), Entity
(Indic
));
751 -- Processing for allocators returning non-interface types
753 if not Is_Interface
(DesigT
) then
754 if Aggr_In_Place
then
756 Make_Object_Declaration
(Loc
,
757 Defining_Identifier
=> Temp
,
758 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
762 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
764 -- Copy the Comes_From_Source flag for the allocator we just
765 -- built, since logically this allocator is a replacement of
766 -- the original allocator node. This is for proper handling of
767 -- restriction No_Implicit_Heap_Allocations.
769 Preserve_Comes_From_Source
770 (Expression
(Temp_Decl
), N
);
772 Set_No_Initialization
(Expression
(Temp_Decl
));
773 Insert_Action
(N
, Temp_Decl
);
775 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
776 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
779 Node
:= Relocate_Node
(N
);
783 Make_Object_Declaration
(Loc
,
784 Defining_Identifier
=> Temp
,
785 Constant_Present
=> True,
786 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
789 Insert_Action
(N
, Temp_Decl
);
790 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
793 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
794 -- interface type. In this case we use the type of the qualified
795 -- expression to allocate the object.
799 Def_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
804 Make_Full_Type_Declaration
(Loc
,
805 Defining_Identifier
=> Def_Id
,
807 Make_Access_To_Object_Definition
(Loc
,
809 Null_Exclusion_Present
=> False,
811 Is_Access_Constant
(Etype
(N
)),
812 Subtype_Indication
=>
813 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
815 Insert_Action
(N
, New_Decl
);
817 -- Inherit the allocation-related attributes from the original
820 Set_Finalization_Master
821 (Def_Id
, Finalization_Master
(PtrT
));
823 Set_Associated_Storage_Pool
824 (Def_Id
, Associated_Storage_Pool
(PtrT
));
826 -- Declare the object using the previous type declaration
828 if Aggr_In_Place
then
830 Make_Object_Declaration
(Loc
,
831 Defining_Identifier
=> Temp
,
832 Object_Definition
=> New_Occurrence_Of
(Def_Id
, Loc
),
835 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
837 -- Copy the Comes_From_Source flag for the allocator we just
838 -- built, since logically this allocator is a replacement of
839 -- the original allocator node. This is for proper handling
840 -- of restriction No_Implicit_Heap_Allocations.
842 Set_Comes_From_Source
843 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
845 Set_No_Initialization
(Expression
(Temp_Decl
));
846 Insert_Action
(N
, Temp_Decl
);
848 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
849 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
852 Node
:= Relocate_Node
(N
);
856 Make_Object_Declaration
(Loc
,
857 Defining_Identifier
=> Temp
,
858 Constant_Present
=> True,
859 Object_Definition
=> New_Occurrence_Of
(Def_Id
, Loc
),
862 Insert_Action
(N
, Temp_Decl
);
863 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
866 -- Generate an additional object containing the address of the
867 -- returned object. The type of this second object declaration
868 -- is the correct type required for the common processing that
869 -- is still performed by this subprogram. The displacement of
870 -- this pointer to reference the component associated with the
871 -- interface type will be done at the end of common processing.
874 Make_Object_Declaration
(Loc
,
875 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
876 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
878 Unchecked_Convert_To
(PtrT
,
879 New_Occurrence_Of
(Temp
, Loc
)));
881 Insert_Action
(N
, New_Decl
);
883 Temp_Decl
:= New_Decl
;
884 Temp
:= Defining_Identifier
(New_Decl
);
888 -- Generate the tag assignment
890 -- Suppress the tag assignment for VM targets because VM tags are
891 -- represented implicitly in objects.
893 if not Tagged_Type_Expansion
then
896 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
897 -- interface objects because in this case the tag does not change.
899 elsif Is_Interface
(Directly_Designated_Type
(Etype
(N
))) then
900 pragma Assert
(Is_Class_Wide_Type
901 (Directly_Designated_Type
(Etype
(N
))));
904 -- Likewise if the allocator is made for a special return object
906 elsif For_Special_Return_Object
(N
) then
909 elsif Is_Tagged_Type
(T
) and then not Is_Class_Wide_Type
(T
) then
912 Make_Explicit_Dereference
(Loc
,
913 Prefix
=> New_Occurrence_Of
(Temp
, Loc
));
915 elsif Is_Private_Type
(T
)
916 and then Is_Tagged_Type
(Underlying_Type
(T
))
918 TagT
:= Underlying_Type
(T
);
920 Unchecked_Convert_To
(Underlying_Type
(T
),
921 Make_Explicit_Dereference
(Loc
,
922 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
925 if Present
(TagT
) then
927 Full_T
: constant Entity_Id
:= Underlying_Type
(TagT
);
931 Make_Assignment_Statement
(Loc
,
933 Make_Selected_Component
(Loc
,
937 (First_Tag_Component
(Full_T
), Loc
)),
940 Unchecked_Convert_To
(RTE
(RE_Tag
),
943 (First_Elmt
(Access_Disp_Table
(Full_T
))), Loc
)));
946 -- The previous assignment has to be done in any case
948 Set_Assignment_OK
(Name
(Tag_Assign
));
949 Insert_Action
(N
, Tag_Assign
);
952 -- Generate an Adjust call if the object will be moved. In Ada 2005,
953 -- the object may be inherently limited, in which case there is no
954 -- Adjust procedure, and the object is built in place. In Ada 95, the
955 -- object can be limited but not inherently limited if this allocator
956 -- came from a return statement (we're allocating the result on the
957 -- secondary stack); in that case, the object will be moved, so we do
958 -- want to Adjust. But the call is always skipped if the allocator is
959 -- made for a special return object because it's generated elsewhere.
961 -- Needs_Finalization (DesigT) may differ from Needs_Finalization (T)
962 -- if one of the two types is class-wide, and the other is not.
964 if Needs_Finalization
(DesigT
)
965 and then Needs_Finalization
(T
)
966 and then not Is_Limited_View
(T
)
967 and then not Aggr_In_Place
968 and then Nkind
(Exp
) /= N_Function_Call
969 and then not For_Special_Return_Object
(N
)
971 -- An unchecked conversion is needed in the classwide case because
972 -- the designated type can be an ancestor of the subtype mark of
978 Unchecked_Convert_To
(T
,
979 Make_Explicit_Dereference
(Loc
,
980 Prefix
=> New_Occurrence_Of
(Temp
, Loc
))),
983 if Present
(Adj_Call
) then
984 Insert_Action
(N
, Adj_Call
);
988 -- Note: the accessibility check must be inserted after the call to
989 -- [Deep_]Adjust to ensure proper completion of the assignment.
991 Apply_Accessibility_Check_For_Allocator
(N
, Exp
, Temp
);
993 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
994 Analyze_And_Resolve
(N
, PtrT
);
996 -- Ada 2005 (AI-251): Displace the pointer to reference the record
997 -- component containing the secondary dispatch table of the interface
1000 if Is_Interface
(DesigT
) then
1001 Displace_Allocator_Pointer
(N
);
1004 -- Always force the generation of a temporary for aggregates when
1005 -- generating C code, to simplify the work in the code generator.
1008 or else (Modify_Tree_For_C
and then Nkind
(Exp
) = N_Aggregate
)
1010 Temp
:= Make_Temporary
(Loc
, 'P', N
);
1012 Make_Object_Declaration
(Loc
,
1013 Defining_Identifier
=> Temp
,
1014 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
1016 Make_Allocator
(Loc
,
1017 Expression
=> New_Occurrence_Of
(Etype
(Exp
), Loc
)));
1019 -- Copy the Comes_From_Source flag for the allocator we just built,
1020 -- since logically this allocator is a replacement of the original
1021 -- allocator node. This is for proper handling of restriction
1022 -- No_Implicit_Heap_Allocations.
1024 Set_Comes_From_Source
1025 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
1027 Set_No_Initialization
(Expression
(Temp_Decl
));
1028 Insert_Action
(N
, Temp_Decl
);
1030 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1031 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
1033 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
1034 Analyze_And_Resolve
(N
, PtrT
);
1036 elsif Is_Access_Type
(T
) and then Can_Never_Be_Null
(T
) then
1037 Install_Null_Excluding_Check
(Exp
);
1039 elsif Is_Access_Type
(DesigT
)
1040 and then Nkind
(Exp
) = N_Allocator
1041 and then Nkind
(Expression
(Exp
)) /= N_Qualified_Expression
1043 -- Apply constraint to designated subtype indication
1045 Apply_Constraint_Check
1046 (Expression
(Exp
), Designated_Type
(DesigT
), No_Sliding
=> True);
1048 if Nkind
(Expression
(Exp
)) = N_Raise_Constraint_Error
then
1050 -- Propagate constraint_error to enclosing allocator
1052 Rewrite
(Exp
, New_Copy
(Expression
(Exp
)));
1056 Build_Allocate_Deallocate_Proc
(N
, True);
1058 -- For an access to unconstrained packed array, GIGI needs to see an
1059 -- expression with a constrained subtype in order to compute the
1060 -- proper size for the allocator.
1062 if Is_Packed_Array
(T
)
1063 and then not Is_Constrained
(T
)
1066 ConstrT
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
1067 Internal_Exp
: constant Node_Id
:= Relocate_Node
(Exp
);
1070 Make_Subtype_Declaration
(Loc
,
1071 Defining_Identifier
=> ConstrT
,
1072 Subtype_Indication
=>
1073 Make_Subtype_From_Expr
(Internal_Exp
, T
)));
1074 Freeze_Itype
(ConstrT
, Exp
);
1075 Rewrite
(Exp
, OK_Convert_To
(ConstrT
, Internal_Exp
));
1079 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1080 -- to a build-in-place function, then access to the allocated object
1081 -- must be passed to the function.
1083 if Is_Build_In_Place_Function_Call
(Exp
) then
1084 Make_Build_In_Place_Call_In_Allocator
(N
, Exp
);
1089 when RE_Not_Available
=>
1091 end Expand_Allocator_Expression
;
1093 -----------------------------
1094 -- Expand_Array_Comparison --
1095 -----------------------------
1097 -- Expansion is only required in the case of array types. For the unpacked
1098 -- case, an appropriate runtime routine is called. For packed cases, and
1099 -- also in some other cases where a runtime routine cannot be called, the
1100 -- form of the expansion is:
1102 -- [body for greater_nn; boolean_expression]
1104 -- The body is built by Make_Array_Comparison_Op, and the form of the
1105 -- Boolean expression depends on the operator involved.
1107 procedure Expand_Array_Comparison
(N
: Node_Id
) is
1108 Loc
: constant Source_Ptr
:= Sloc
(N
);
1109 Op1
: Node_Id
:= Left_Opnd
(N
);
1110 Op2
: Node_Id
:= Right_Opnd
(N
);
1111 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
1112 Ctyp
: constant Entity_Id
:= Component_Type
(Typ1
);
1115 Func_Body
: Node_Id
;
1116 Func_Name
: Entity_Id
;
1120 Byte_Addressable
: constant Boolean := System_Storage_Unit
= Byte
'Size;
1121 -- True for byte addressable target
1123 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean;
1124 -- Returns True if the length of the given operand is known to be less
1125 -- than 4. Returns False if this length is known to be four or greater
1126 -- or is not known at compile time.
1128 ------------------------
1129 -- Length_Less_Than_4 --
1130 ------------------------
1132 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean is
1133 Otyp
: constant Entity_Id
:= Etype
(Opnd
);
1136 if Ekind
(Otyp
) = E_String_Literal_Subtype
then
1137 return String_Literal_Length
(Otyp
) < 4;
1139 elsif Compile_Time_Known_Bounds
(Otyp
) then
1144 Get_First_Index_Bounds
(Otyp
, Lo
, Hi
);
1151 end Length_Less_Than_4
;
1153 -- Start of processing for Expand_Array_Comparison
1156 -- Deal first with unpacked case, where we can call a runtime routine
1157 -- except that we avoid this for targets for which are not addressable
1160 if not Is_Bit_Packed_Array
(Typ1
) and then Byte_Addressable
then
1161 -- The call we generate is:
1163 -- Compare_Array_xn[_Unaligned]
1164 -- (left'address, right'address, left'length, right'length) <op> 0
1166 -- x = U for unsigned, S for signed
1167 -- n = 8,16,32,64,128 for component size
1168 -- Add _Unaligned if length < 4 and component size is 8.
1169 -- <op> is the standard comparison operator
1171 if Component_Size
(Typ1
) = 8 then
1172 if Length_Less_Than_4
(Op1
)
1174 Length_Less_Than_4
(Op2
)
1176 if Is_Unsigned_Type
(Ctyp
) then
1177 Comp
:= RE_Compare_Array_U8_Unaligned
;
1179 Comp
:= RE_Compare_Array_S8_Unaligned
;
1183 if Is_Unsigned_Type
(Ctyp
) then
1184 Comp
:= RE_Compare_Array_U8
;
1186 Comp
:= RE_Compare_Array_S8
;
1190 elsif Component_Size
(Typ1
) = 16 then
1191 if Is_Unsigned_Type
(Ctyp
) then
1192 Comp
:= RE_Compare_Array_U16
;
1194 Comp
:= RE_Compare_Array_S16
;
1197 elsif Component_Size
(Typ1
) = 32 then
1198 if Is_Unsigned_Type
(Ctyp
) then
1199 Comp
:= RE_Compare_Array_U32
;
1201 Comp
:= RE_Compare_Array_S32
;
1204 elsif Component_Size
(Typ1
) = 64 then
1205 if Is_Unsigned_Type
(Ctyp
) then
1206 Comp
:= RE_Compare_Array_U64
;
1208 Comp
:= RE_Compare_Array_S64
;
1211 else pragma Assert
(Component_Size
(Typ1
) = 128);
1212 if Is_Unsigned_Type
(Ctyp
) then
1213 Comp
:= RE_Compare_Array_U128
;
1215 Comp
:= RE_Compare_Array_S128
;
1219 if RTE_Available
(Comp
) then
1221 -- Expand to a call only if the runtime function is available,
1222 -- otherwise fall back to inline code.
1224 Remove_Side_Effects
(Op1
, Name_Req
=> True);
1225 Remove_Side_Effects
(Op2
, Name_Req
=> True);
1228 Comp_Call
: constant Node_Id
:=
1229 Make_Function_Call
(Loc
,
1230 Name
=> New_Occurrence_Of
(RTE
(Comp
), Loc
),
1232 Parameter_Associations
=> New_List
(
1233 Make_Attribute_Reference
(Loc
,
1234 Prefix
=> Relocate_Node
(Op1
),
1235 Attribute_Name
=> Name_Address
),
1237 Make_Attribute_Reference
(Loc
,
1238 Prefix
=> Relocate_Node
(Op2
),
1239 Attribute_Name
=> Name_Address
),
1241 Make_Attribute_Reference
(Loc
,
1242 Prefix
=> Relocate_Node
(Op1
),
1243 Attribute_Name
=> Name_Length
),
1245 Make_Attribute_Reference
(Loc
,
1246 Prefix
=> Relocate_Node
(Op2
),
1247 Attribute_Name
=> Name_Length
)));
1249 Zero
: constant Node_Id
:=
1250 Make_Integer_Literal
(Loc
,
1258 Comp_Op
:= Make_Op_Lt
(Loc
, Comp_Call
, Zero
);
1260 Comp_Op
:= Make_Op_Le
(Loc
, Comp_Call
, Zero
);
1262 Comp_Op
:= Make_Op_Gt
(Loc
, Comp_Call
, Zero
);
1264 Comp_Op
:= Make_Op_Ge
(Loc
, Comp_Call
, Zero
);
1266 raise Program_Error
;
1269 Rewrite
(N
, Comp_Op
);
1272 Analyze_And_Resolve
(N
, Standard_Boolean
);
1277 -- Cases where we cannot make runtime call
1279 -- For (a <= b) we convert to not (a > b)
1281 if Chars
(N
) = Name_Op_Le
then
1287 Right_Opnd
=> Op2
)));
1288 Analyze_And_Resolve
(N
, Standard_Boolean
);
1291 -- For < the Boolean expression is
1292 -- greater__nn (op2, op1)
1294 elsif Chars
(N
) = Name_Op_Lt
then
1295 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
1299 Op1
:= Right_Opnd
(N
);
1300 Op2
:= Left_Opnd
(N
);
1302 -- For (a >= b) we convert to not (a < b)
1304 elsif Chars
(N
) = Name_Op_Ge
then
1310 Right_Opnd
=> Op2
)));
1311 Analyze_And_Resolve
(N
, Standard_Boolean
);
1314 -- For > the Boolean expression is
1315 -- greater__nn (op1, op2)
1318 pragma Assert
(Chars
(N
) = Name_Op_Gt
);
1319 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
1322 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
1324 Make_Function_Call
(Loc
,
1325 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
1326 Parameter_Associations
=> New_List
(Op1
, Op2
));
1328 Insert_Action
(N
, Func_Body
);
1330 Analyze_And_Resolve
(N
, Standard_Boolean
);
1331 end Expand_Array_Comparison
;
1333 ---------------------------
1334 -- Expand_Array_Equality --
1335 ---------------------------
1337 -- Expand an equality function for multi-dimensional arrays. Here is an
1338 -- example of such a function for Nb_Dimension = 2
1340 -- function Enn (A : atyp; B : btyp) return boolean is
1342 -- if (A'length (1) = 0 or else A'length (2) = 0)
1344 -- (B'length (1) = 0 or else B'length (2) = 0)
1346 -- return true; -- RM 4.5.2(22)
1349 -- if A'length (1) /= B'length (1)
1351 -- A'length (2) /= B'length (2)
1353 -- return false; -- RM 4.5.2(23)
1357 -- A1 : Index_T1 := A'first (1);
1358 -- B1 : Index_T1 := B'first (1);
1362 -- A2 : Index_T2 := A'first (2);
1363 -- B2 : Index_T2 := B'first (2);
1366 -- if A (A1, A2) /= B (B1, B2) then
1370 -- exit when A2 = A'last (2);
1371 -- A2 := Index_T2'succ (A2);
1372 -- B2 := Index_T2'succ (B2);
1376 -- exit when A1 = A'last (1);
1377 -- A1 := Index_T1'succ (A1);
1378 -- B1 := Index_T1'succ (B1);
1385 -- Note on the formal types used (atyp and btyp). If either of the arrays
1386 -- is of a private type, we use the underlying type, and do an unchecked
1387 -- conversion of the actual. If either of the arrays has a bound depending
1388 -- on a discriminant, then we use the base type since otherwise we have an
1389 -- escaped discriminant in the function.
1391 -- If both arrays are constrained and have the same bounds, we can generate
1392 -- a loop with an explicit iteration scheme using a 'Range attribute over
1395 function Expand_Array_Equality
1400 Typ
: Entity_Id
) return Node_Id
1402 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
1403 Decls
: constant List_Id
:= New_List
;
1404 Index_List1
: constant List_Id
:= New_List
;
1405 Index_List2
: constant List_Id
:= New_List
;
1407 First_Idx
: Node_Id
;
1409 Func_Name
: Entity_Id
;
1410 Func_Body
: Node_Id
;
1412 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
1413 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
1417 -- The parameter types to be used for the formals
1421 -- The LHS and RHS converted to the parameter types
1426 Dim
: Pos
) return Node_Id
;
1427 -- This builds the attribute reference Arr'Nam (Dim)
1429 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
;
1430 -- Create one statement to compare corresponding components, designated
1431 -- by a full set of indexes.
1433 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
;
1434 -- Given one of the arguments, computes the appropriate type to be used
1435 -- for that argument in the corresponding function formal
1437 function Handle_One_Dimension
1439 Index
: Node_Id
) return Node_Id
;
1440 -- This procedure returns the following code
1443 -- An : Index_T := A'First (N);
1444 -- Bn : Index_T := B'First (N);
1448 -- exit when An = A'Last (N);
1449 -- An := Index_T'Succ (An)
1450 -- Bn := Index_T'Succ (Bn)
1454 -- If both indexes are constrained and identical, the procedure
1455 -- returns a simpler loop:
1457 -- for An in A'Range (N) loop
1461 -- N is the dimension for which we are generating a loop. Index is the
1462 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1463 -- xxx statement is either the loop or declare for the next dimension
1464 -- or if this is the last dimension the comparison of corresponding
1465 -- components of the arrays.
1467 -- The actual way the code works is to return the comparison of
1468 -- corresponding components for the N+1 call. That's neater.
1470 function Test_Empty_Arrays
return Node_Id
;
1471 -- This function constructs the test for both arrays being empty
1472 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1474 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1476 function Test_Lengths_Correspond
return Node_Id
;
1477 -- This function constructs the test for arrays having different lengths
1478 -- in at least one index position, in which case the resulting code is:
1480 -- A'length (1) /= B'length (1)
1482 -- A'length (2) /= B'length (2)
1493 Dim
: Pos
) return Node_Id
1497 Make_Attribute_Reference
(Loc
,
1498 Attribute_Name
=> Nam
,
1499 Prefix
=> New_Occurrence_Of
(Arr
, Loc
),
1500 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, Dim
)));
1503 ------------------------
1504 -- Component_Equality --
1505 ------------------------
1507 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
is
1512 -- if a(i1...) /= b(j1...) then return false; end if;
1515 Make_Indexed_Component
(Loc
,
1516 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
1517 Expressions
=> Index_List1
);
1520 Make_Indexed_Component
(Loc
,
1521 Prefix
=> Make_Identifier
(Loc
, Chars
(B
)),
1522 Expressions
=> Index_List2
);
1524 Test
:= Expand_Composite_Equality
1525 (Outer_Type
=> Typ
, Nod
=> Nod
, Comp_Type
=> Component_Type
(Typ
),
1526 Lhs
=> L
, Rhs
=> R
);
1528 -- If some (sub)component is an unchecked_union, the whole operation
1529 -- will raise program error.
1531 if Nkind
(Test
) = N_Raise_Program_Error
then
1533 -- This node is going to be inserted at a location where a
1534 -- statement is expected: clear its Etype so analysis will set
1535 -- it to the expected Standard_Void_Type.
1537 Set_Etype
(Test
, Empty
);
1542 Make_Implicit_If_Statement
(Nod
,
1543 Condition
=> Make_Op_Not
(Loc
, Right_Opnd
=> Test
),
1544 Then_Statements
=> New_List
(
1545 Make_Simple_Return_Statement
(Loc
,
1546 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))));
1548 end Component_Equality
;
1554 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
is
1565 T
:= Underlying_Type
(T
);
1567 X
:= First_Index
(T
);
1568 while Present
(X
) loop
1569 if Denotes_Discriminant
(Type_Low_Bound
(Etype
(X
)))
1571 Denotes_Discriminant
(Type_High_Bound
(Etype
(X
)))
1584 --------------------------
1585 -- Handle_One_Dimension --
1586 ---------------------------
1588 function Handle_One_Dimension
1590 Index
: Node_Id
) return Node_Id
1592 Need_Separate_Indexes
: constant Boolean :=
1593 Ltyp
/= Rtyp
or else not Is_Constrained
(Ltyp
);
1594 -- If the index types are identical, and we are working with
1595 -- constrained types, then we can use the same index for both
1598 An
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
1601 Index_T
: Entity_Id
;
1606 if N
> Number_Dimensions
(Ltyp
) then
1607 return Component_Equality
(Ltyp
);
1610 -- Case where we generate a loop
1612 Index_T
:= Base_Type
(Etype
(Index
));
1614 if Need_Separate_Indexes
then
1615 Bn
:= Make_Temporary
(Loc
, 'B');
1620 Append
(New_Occurrence_Of
(An
, Loc
), Index_List1
);
1621 Append
(New_Occurrence_Of
(Bn
, Loc
), Index_List2
);
1623 Stm_List
:= New_List
(
1624 Handle_One_Dimension
(N
+ 1, Next_Index
(Index
)));
1626 if Need_Separate_Indexes
then
1628 -- Generate guard for loop, followed by increments of indexes
1630 Append_To
(Stm_List
,
1631 Make_Exit_Statement
(Loc
,
1634 Left_Opnd
=> New_Occurrence_Of
(An
, Loc
),
1635 Right_Opnd
=> Arr_Attr
(A
, Name_Last
, N
))));
1637 Append_To
(Stm_List
,
1638 Make_Assignment_Statement
(Loc
,
1639 Name
=> New_Occurrence_Of
(An
, Loc
),
1641 Make_Attribute_Reference
(Loc
,
1642 Prefix
=> New_Occurrence_Of
(Index_T
, Loc
),
1643 Attribute_Name
=> Name_Succ
,
1644 Expressions
=> New_List
(
1645 New_Occurrence_Of
(An
, Loc
)))));
1647 Append_To
(Stm_List
,
1648 Make_Assignment_Statement
(Loc
,
1649 Name
=> New_Occurrence_Of
(Bn
, Loc
),
1651 Make_Attribute_Reference
(Loc
,
1652 Prefix
=> New_Occurrence_Of
(Index_T
, Loc
),
1653 Attribute_Name
=> Name_Succ
,
1654 Expressions
=> New_List
(
1655 New_Occurrence_Of
(Bn
, Loc
)))));
1658 -- If separate indexes, we need a declare block for An and Bn, and a
1659 -- loop without an iteration scheme.
1661 if Need_Separate_Indexes
then
1663 Make_Implicit_Loop_Statement
(Nod
, Statements
=> Stm_List
);
1666 Make_Block_Statement
(Loc
,
1667 Declarations
=> New_List
(
1668 Make_Object_Declaration
(Loc
,
1669 Defining_Identifier
=> An
,
1670 Object_Definition
=> New_Occurrence_Of
(Index_T
, Loc
),
1671 Expression
=> Arr_Attr
(A
, Name_First
, N
)),
1673 Make_Object_Declaration
(Loc
,
1674 Defining_Identifier
=> Bn
,
1675 Object_Definition
=> New_Occurrence_Of
(Index_T
, Loc
),
1676 Expression
=> Arr_Attr
(B
, Name_First
, N
))),
1678 Handled_Statement_Sequence
=>
1679 Make_Handled_Sequence_Of_Statements
(Loc
,
1680 Statements
=> New_List
(Loop_Stm
)));
1682 -- If no separate indexes, return loop statement with explicit
1683 -- iteration scheme on its own.
1687 Make_Implicit_Loop_Statement
(Nod
,
1688 Statements
=> Stm_List
,
1690 Make_Iteration_Scheme
(Loc
,
1691 Loop_Parameter_Specification
=>
1692 Make_Loop_Parameter_Specification
(Loc
,
1693 Defining_Identifier
=> An
,
1694 Discrete_Subtype_Definition
=>
1695 Arr_Attr
(A
, Name_Range
, N
))));
1698 end Handle_One_Dimension
;
1700 -----------------------
1701 -- Test_Empty_Arrays --
1702 -----------------------
1704 function Test_Empty_Arrays
return Node_Id
is
1705 Alist
: Node_Id
:= Empty
;
1706 Blist
: Node_Id
:= Empty
;
1709 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
1710 Evolve_Or_Else
(Alist
,
1712 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
1713 Right_Opnd
=> Make_Integer_Literal
(Loc
, Uint_0
)));
1715 Evolve_Or_Else
(Blist
,
1717 Left_Opnd
=> Arr_Attr
(B
, Name_Length
, J
),
1718 Right_Opnd
=> Make_Integer_Literal
(Loc
, Uint_0
)));
1724 Right_Opnd
=> Blist
);
1725 end Test_Empty_Arrays
;
1727 -----------------------------
1728 -- Test_Lengths_Correspond --
1729 -----------------------------
1731 function Test_Lengths_Correspond
return Node_Id
is
1732 Result
: Node_Id
:= Empty
;
1735 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
1736 Evolve_Or_Else
(Result
,
1738 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
1739 Right_Opnd
=> Arr_Attr
(B
, Name_Length
, J
)));
1743 end Test_Lengths_Correspond
;
1745 -- Start of processing for Expand_Array_Equality
1748 Ltyp
:= Get_Arg_Type
(Lhs
);
1749 Rtyp
:= Get_Arg_Type
(Rhs
);
1751 -- For now, if the argument types are not the same, go to the base type,
1752 -- since the code assumes that the formals have the same type. This is
1753 -- fixable in future ???
1755 if Ltyp
/= Rtyp
then
1756 Ltyp
:= Base_Type
(Ltyp
);
1757 Rtyp
:= Base_Type
(Rtyp
);
1760 -- If the array type is distinct from the type of the arguments, it
1761 -- is the full view of a private type. Apply an unchecked conversion
1762 -- to ensure that analysis of the code below succeeds.
1765 or else Base_Type
(Etype
(Lhs
)) /= Base_Type
(Ltyp
)
1767 New_Lhs
:= OK_Convert_To
(Ltyp
, Lhs
);
1773 or else Base_Type
(Etype
(Rhs
)) /= Base_Type
(Rtyp
)
1775 New_Rhs
:= OK_Convert_To
(Rtyp
, Rhs
);
1780 pragma Assert
(Ltyp
= Rtyp
);
1781 First_Idx
:= First_Index
(Ltyp
);
1783 -- If optimization is enabled and the array boils down to a couple of
1784 -- consecutive elements, generate a simple conjunction of comparisons
1785 -- which should be easier to optimize by the code generator.
1787 if Optimization_Level
> 0
1788 and then Is_Constrained
(Ltyp
)
1789 and then Number_Dimensions
(Ltyp
) = 1
1790 and then Compile_Time_Known_Bounds
(Ltyp
)
1791 and then Expr_Value
(Type_High_Bound
(Etype
(First_Idx
))) =
1792 Expr_Value
(Type_Low_Bound
(Etype
(First_Idx
))) + 1
1795 Ctyp
: constant Entity_Id
:= Component_Type
(Ltyp
);
1796 Low_B
: constant Node_Id
:=
1797 Type_Low_Bound
(Etype
(First_Idx
));
1798 High_B
: constant Node_Id
:=
1799 Type_High_Bound
(Etype
(First_Idx
));
1801 TestL
, TestH
: Node_Id
;
1805 Make_Indexed_Component
(Loc
,
1806 Prefix
=> New_Copy_Tree
(New_Lhs
),
1807 Expressions
=> New_List
(New_Copy_Tree
(Low_B
)));
1810 Make_Indexed_Component
(Loc
,
1811 Prefix
=> New_Copy_Tree
(New_Rhs
),
1812 Expressions
=> New_List
(New_Copy_Tree
(Low_B
)));
1814 TestL
:= Expand_Composite_Equality
1815 (Outer_Type
=> Ltyp
, Nod
=> Nod
, Comp_Type
=> Ctyp
,
1816 Lhs
=> L
, Rhs
=> R
);
1819 Make_Indexed_Component
(Loc
,
1821 Expressions
=> New_List
(New_Copy_Tree
(High_B
)));
1824 Make_Indexed_Component
(Loc
,
1826 Expressions
=> New_List
(New_Copy_Tree
(High_B
)));
1828 TestH
:= Expand_Composite_Equality
1829 (Outer_Type
=> Ltyp
, Nod
=> Nod
, Comp_Type
=> Ctyp
,
1830 Lhs
=> L
, Rhs
=> R
);
1833 Make_And_Then
(Loc
, Left_Opnd
=> TestL
, Right_Opnd
=> TestH
);
1837 -- Build list of formals for function
1839 Formals
:= New_List
(
1840 Make_Parameter_Specification
(Loc
,
1841 Defining_Identifier
=> A
,
1842 Parameter_Type
=> New_Occurrence_Of
(Ltyp
, Loc
)),
1844 Make_Parameter_Specification
(Loc
,
1845 Defining_Identifier
=> B
,
1846 Parameter_Type
=> New_Occurrence_Of
(Rtyp
, Loc
)));
1848 Func_Name
:= Make_Temporary
(Loc
, 'E');
1850 -- Build statement sequence for function
1853 Make_Subprogram_Body
(Loc
,
1855 Make_Function_Specification
(Loc
,
1856 Defining_Unit_Name
=> Func_Name
,
1857 Parameter_Specifications
=> Formals
,
1858 Result_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
)),
1860 Declarations
=> Decls
,
1862 Handled_Statement_Sequence
=>
1863 Make_Handled_Sequence_Of_Statements
(Loc
,
1864 Statements
=> New_List
(
1866 Make_Implicit_If_Statement
(Nod
,
1867 Condition
=> Test_Empty_Arrays
,
1868 Then_Statements
=> New_List
(
1869 Make_Simple_Return_Statement
(Loc
,
1871 New_Occurrence_Of
(Standard_True
, Loc
)))),
1873 Make_Implicit_If_Statement
(Nod
,
1874 Condition
=> Test_Lengths_Correspond
,
1875 Then_Statements
=> New_List
(
1876 Make_Simple_Return_Statement
(Loc
,
1877 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
)))),
1879 Handle_One_Dimension
(1, First_Idx
),
1881 Make_Simple_Return_Statement
(Loc
,
1882 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)))));
1884 Set_Has_Completion
(Func_Name
, True);
1885 Set_Is_Inlined
(Func_Name
);
1887 Append_To
(Bodies
, Func_Body
);
1890 Make_Function_Call
(Loc
,
1891 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
1892 Parameter_Associations
=> New_List
(New_Lhs
, New_Rhs
));
1893 end Expand_Array_Equality
;
1895 -----------------------------
1896 -- Expand_Boolean_Operator --
1897 -----------------------------
1899 -- Note that we first get the actual subtypes of the operands, since we
1900 -- always want to deal with types that have bounds.
1902 procedure Expand_Boolean_Operator
(N
: Node_Id
) is
1903 Typ
: constant Entity_Id
:= Etype
(N
);
1906 -- Special case of bit packed array where both operands are known to be
1907 -- properly aligned. In this case we use an efficient run time routine
1908 -- to carry out the operation (see System.Bit_Ops).
1910 if Is_Bit_Packed_Array
(Typ
)
1911 and then not Is_Possibly_Unaligned_Object
(Left_Opnd
(N
))
1912 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
1914 Expand_Packed_Boolean_Operator
(N
);
1918 -- For the normal non-packed case, the general expansion is to build
1919 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
1920 -- and then inserting it into the tree. The original operator node is
1921 -- then rewritten as a call to this function. We also use this in the
1922 -- packed case if either operand is a possibly unaligned object.
1925 Loc
: constant Source_Ptr
:= Sloc
(N
);
1926 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
1927 R
: Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
1928 Func_Body
: Node_Id
;
1929 Func_Name
: Entity_Id
;
1932 Convert_To_Actual_Subtype
(L
);
1933 Convert_To_Actual_Subtype
(R
);
1934 Ensure_Defined
(Etype
(L
), N
);
1935 Ensure_Defined
(Etype
(R
), N
);
1936 Apply_Length_Check
(R
, Etype
(L
));
1938 if Nkind
(N
) = N_Op_Xor
then
1939 R
:= Duplicate_Subexpr
(R
);
1940 Silly_Boolean_Array_Xor_Test
(N
, R
, Etype
(L
));
1943 if Nkind
(Parent
(N
)) = N_Assignment_Statement
1944 and then Safe_In_Place_Array_Op
(Name
(Parent
(N
)), L
, R
)
1946 Build_Boolean_Array_Proc_Call
(Parent
(N
), L
, R
);
1948 elsif Nkind
(Parent
(N
)) = N_Op_Not
1949 and then Nkind
(N
) = N_Op_And
1950 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
1951 and then Safe_In_Place_Array_Op
(Name
(Parent
(Parent
(N
))), L
, R
)
1955 Func_Body
:= Make_Boolean_Array_Op
(Etype
(L
), N
);
1956 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
1957 Insert_Action
(N
, Func_Body
);
1959 -- Now rewrite the expression with a call
1961 if Transform_Function_Array
then
1963 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
1972 Make_Object_Declaration
(Loc
,
1973 Defining_Identifier
=> Temp_Id
,
1974 Object_Definition
=>
1975 New_Occurrence_Of
(Etype
(L
), Loc
));
1978 -- Proc_Call (L, R, Temp);
1981 Make_Procedure_Call_Statement
(Loc
,
1982 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
1983 Parameter_Associations
=>
1986 Make_Type_Conversion
1987 (Loc
, New_Occurrence_Of
(Etype
(L
), Loc
), R
),
1988 New_Occurrence_Of
(Temp_Id
, Loc
)));
1990 Insert_Actions
(Parent
(N
), New_List
(Decl
, Call
));
1991 Rewrite
(N
, New_Occurrence_Of
(Temp_Id
, Loc
));
1995 Make_Function_Call
(Loc
,
1996 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
1997 Parameter_Associations
=>
2000 Make_Type_Conversion
2001 (Loc
, New_Occurrence_Of
(Etype
(L
), Loc
), R
))));
2004 Analyze_And_Resolve
(N
, Typ
);
2007 end Expand_Boolean_Operator
;
2009 ------------------------------------------------
2010 -- Expand_Compare_Minimize_Eliminate_Overflow --
2011 ------------------------------------------------
2013 procedure Expand_Compare_Minimize_Eliminate_Overflow
(N
: Node_Id
) is
2014 Loc
: constant Source_Ptr
:= Sloc
(N
);
2016 Result_Type
: constant Entity_Id
:= Etype
(N
);
2017 -- Capture result type (could be a derived boolean type)
2022 LLIB
: constant Entity_Id
:= Base_Type
(Standard_Long_Long_Integer
);
2023 -- Entity for Long_Long_Integer'Base
2026 procedure Set_False
;
2027 -- These procedures rewrite N with an occurrence of Standard_True or
2028 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2034 procedure Set_False
is
2036 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
2037 Warn_On_Known_Condition
(N
);
2044 procedure Set_True
is
2046 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
2047 Warn_On_Known_Condition
(N
);
2050 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2053 -- OK, this is the case we are interested in. First step is to process
2054 -- our operands using the Minimize_Eliminate circuitry which applies
2055 -- this processing to the two operand subtrees.
2057 Minimize_Eliminate_Overflows
2058 (Left_Opnd
(N
), Llo
, Lhi
, Top_Level
=> False);
2059 Minimize_Eliminate_Overflows
2060 (Right_Opnd
(N
), Rlo
, Rhi
, Top_Level
=> False);
2062 -- See if the range information decides the result of the comparison.
2063 -- We can only do this if we in fact have full range information (which
2064 -- won't be the case if either operand is bignum at this stage).
2066 if Present
(Llo
) and then Present
(Rlo
) then
2067 case N_Op_Compare
(Nkind
(N
)) is
2069 if Llo
= Lhi
and then Rlo
= Rhi
and then Llo
= Rlo
then
2071 elsif Llo
> Rhi
or else Lhi
< Rlo
then
2078 elsif Lhi
< Rlo
then
2085 elsif Lhi
<= Rlo
then
2092 elsif Lhi
<= Rlo
then
2099 elsif Lhi
< Rlo
then
2104 if Llo
= Lhi
and then Rlo
= Rhi
and then Llo
= Rlo
then
2106 elsif Llo
> Rhi
or else Lhi
< Rlo
then
2111 -- All done if we did the rewrite
2113 if Nkind
(N
) not in N_Op_Compare
then
2118 -- Otherwise, time to do the comparison
2121 Ltype
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
2122 Rtype
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
2125 -- If the two operands have the same signed integer type we are
2126 -- all set, nothing more to do. This is the case where either
2127 -- both operands were unchanged, or we rewrote both of them to
2128 -- be Long_Long_Integer.
2130 -- Note: Entity for the comparison may be wrong, but it's not worth
2131 -- the effort to change it, since the back end does not use it.
2133 if Is_Signed_Integer_Type
(Ltype
)
2134 and then Base_Type
(Ltype
) = Base_Type
(Rtype
)
2138 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2140 elsif Is_RTE
(Ltype
, RE_Bignum
) or else Is_RTE
(Rtype
, RE_Bignum
) then
2142 Left
: Node_Id
:= Left_Opnd
(N
);
2143 Right
: Node_Id
:= Right_Opnd
(N
);
2144 -- Bignum references for left and right operands
2147 if not Is_RTE
(Ltype
, RE_Bignum
) then
2148 Left
:= Convert_To_Bignum
(Left
);
2149 elsif not Is_RTE
(Rtype
, RE_Bignum
) then
2150 Right
:= Convert_To_Bignum
(Right
);
2153 -- We rewrite our node with:
2156 -- Bnn : Result_Type;
2158 -- M : Mark_Id := SS_Mark;
2160 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2168 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
2169 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
2173 case N_Op_Compare
(Nkind
(N
)) is
2174 when N_Op_Eq
=> Ent
:= RE_Big_EQ
;
2175 when N_Op_Ge
=> Ent
:= RE_Big_GE
;
2176 when N_Op_Gt
=> Ent
:= RE_Big_GT
;
2177 when N_Op_Le
=> Ent
:= RE_Big_LE
;
2178 when N_Op_Lt
=> Ent
:= RE_Big_LT
;
2179 when N_Op_Ne
=> Ent
:= RE_Big_NE
;
2182 -- Insert assignment to Bnn into the bignum block
2185 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
2186 Make_Assignment_Statement
(Loc
,
2187 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
2189 Make_Function_Call
(Loc
,
2191 New_Occurrence_Of
(RTE
(Ent
), Loc
),
2192 Parameter_Associations
=> New_List
(Left
, Right
))));
2194 -- Now do the rewrite with expression actions
2197 Make_Expression_With_Actions
(Loc
,
2198 Actions
=> New_List
(
2199 Make_Object_Declaration
(Loc
,
2200 Defining_Identifier
=> Bnn
,
2201 Object_Definition
=>
2202 New_Occurrence_Of
(Result_Type
, Loc
)),
2204 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
2205 Analyze_And_Resolve
(N
, Result_Type
);
2209 -- No bignums involved, but types are different, so we must have
2210 -- rewritten one of the operands as a Long_Long_Integer but not
2213 -- If left operand is Long_Long_Integer, convert right operand
2214 -- and we are done (with a comparison of two Long_Long_Integers).
2216 elsif Ltype
= LLIB
then
2217 Convert_To_And_Rewrite
(LLIB
, Right_Opnd
(N
));
2218 Analyze_And_Resolve
(Right_Opnd
(N
), LLIB
, Suppress
=> All_Checks
);
2221 -- If right operand is Long_Long_Integer, convert left operand
2222 -- and we are done (with a comparison of two Long_Long_Integers).
2224 -- This is the only remaining possibility
2226 else pragma Assert
(Rtype
= LLIB
);
2227 Convert_To_And_Rewrite
(LLIB
, Left_Opnd
(N
));
2228 Analyze_And_Resolve
(Left_Opnd
(N
), LLIB
, Suppress
=> All_Checks
);
2232 end Expand_Compare_Minimize_Eliminate_Overflow
;
2234 -------------------------------
2235 -- Expand_Composite_Equality --
2236 -------------------------------
2238 -- This function is only called for comparing internal fields of composite
2239 -- types when these fields are themselves composites. This is a special
2240 -- case because it is not possible to respect normal Ada visibility rules.
2242 function Expand_Composite_Equality
2243 (Outer_Type
: Entity_Id
;
2245 Comp_Type
: Entity_Id
;
2247 Rhs
: Node_Id
) return Node_Id
2249 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
2250 Full_Type
: Entity_Id
;
2254 if Is_Private_Type
(Comp_Type
) then
2255 Full_Type
:= Underlying_Type
(Comp_Type
);
2257 Full_Type
:= Comp_Type
;
2260 -- If the private type has no completion the context may be the
2261 -- expansion of a composite equality for a composite type with some
2262 -- still incomplete components. The expression will not be analyzed
2263 -- until the enclosing type is completed, at which point this will be
2264 -- properly expanded, unless there is a bona fide completion error.
2266 if No
(Full_Type
) then
2267 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2270 Full_Type
:= Base_Type
(Full_Type
);
2272 -- When the base type itself is private, use the full view to expand
2273 -- the composite equality.
2275 if Is_Private_Type
(Full_Type
) then
2276 Full_Type
:= Underlying_Type
(Full_Type
);
2279 -- Case of tagged record types
2281 if Is_Tagged_Type
(Full_Type
) then
2282 Eq_Op
:= Find_Primitive_Eq
(Comp_Type
);
2283 pragma Assert
(Present
(Eq_Op
));
2286 Make_Function_Call
(Loc
,
2287 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2288 Parameter_Associations
=>
2290 (Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Lhs
),
2291 Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Rhs
)));
2293 -- Case of untagged record types
2295 elsif Is_Record_Type
(Full_Type
) then
2296 Eq_Op
:= TSS
(Full_Type
, TSS_Composite_Equality
);
2298 if Present
(Eq_Op
) then
2299 if Etype
(First_Formal
(Eq_Op
)) /= Full_Type
then
2301 -- Inherited equality from parent type. Convert the actuals to
2302 -- match signature of operation.
2305 T
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
2309 Make_Function_Call
(Loc
,
2310 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2311 Parameter_Associations
=> New_List
(
2312 OK_Convert_To
(T
, Lhs
),
2313 OK_Convert_To
(T
, Rhs
)));
2317 -- Comparison between Unchecked_Union components
2319 if Is_Unchecked_Union
(Full_Type
) then
2321 Lhs_Type
: Node_Id
:= Full_Type
;
2322 Rhs_Type
: Node_Id
:= Full_Type
;
2323 Lhs_Discr_Val
: Node_Id
;
2324 Rhs_Discr_Val
: Node_Id
;
2329 if Nkind
(Lhs
) = N_Selected_Component
then
2330 Lhs_Type
:= Etype
(Entity
(Selector_Name
(Lhs
)));
2335 if Nkind
(Rhs
) = N_Selected_Component
then
2336 Rhs_Type
:= Etype
(Entity
(Selector_Name
(Rhs
)));
2339 -- Lhs of the composite equality
2341 if Is_Constrained
(Lhs_Type
) then
2343 -- Since the enclosing record type can never be an
2344 -- Unchecked_Union (this code is executed for records
2345 -- that do not have variants), we may reference its
2348 if Nkind
(Lhs
) = N_Selected_Component
2349 and then Has_Per_Object_Constraint
2350 (Entity
(Selector_Name
(Lhs
)))
2353 Make_Selected_Component
(Loc
,
2354 Prefix
=> Prefix
(Lhs
),
2357 (Get_Discriminant_Value
2358 (First_Discriminant
(Lhs_Type
),
2360 Stored_Constraint
(Lhs_Type
))));
2365 (Get_Discriminant_Value
2366 (First_Discriminant
(Lhs_Type
),
2368 Stored_Constraint
(Lhs_Type
)));
2372 -- It is not possible to infer the discriminant since
2373 -- the subtype is not constrained.
2376 Make_Raise_Program_Error
(Loc
,
2377 Reason
=> PE_Unchecked_Union_Restriction
);
2380 -- Rhs of the composite equality
2382 if Is_Constrained
(Rhs_Type
) then
2383 if Nkind
(Rhs
) = N_Selected_Component
2384 and then Has_Per_Object_Constraint
2385 (Entity
(Selector_Name
(Rhs
)))
2388 Make_Selected_Component
(Loc
,
2389 Prefix
=> Prefix
(Rhs
),
2392 (Get_Discriminant_Value
2393 (First_Discriminant
(Rhs_Type
),
2395 Stored_Constraint
(Rhs_Type
))));
2400 (Get_Discriminant_Value
2401 (First_Discriminant
(Rhs_Type
),
2403 Stored_Constraint
(Rhs_Type
)));
2408 Make_Raise_Program_Error
(Loc
,
2409 Reason
=> PE_Unchecked_Union_Restriction
);
2412 -- Call the TSS equality function with the inferred
2413 -- discriminant values.
2416 Make_Function_Call
(Loc
,
2417 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2418 Parameter_Associations
=> New_List
(
2425 -- All cases other than comparing Unchecked_Union types
2429 T
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
2432 Make_Function_Call
(Loc
,
2434 New_Occurrence_Of
(Eq_Op
, Loc
),
2435 Parameter_Associations
=> New_List
(
2436 OK_Convert_To
(T
, Lhs
),
2437 OK_Convert_To
(T
, Rhs
)));
2442 -- Equality composes in Ada 2012 for untagged record types. It also
2443 -- composes for bounded strings, because they are part of the
2444 -- predefined environment (see 4.5.2(32.1/1)). We could make it
2445 -- compose for bounded strings by making them tagged, or by making
2446 -- sure all subcomponents are set to the same value, even when not
2447 -- used. Instead, we have this special case in the compiler, because
2448 -- it's more efficient.
2450 elsif Ada_Version
>= Ada_2012
or else Is_Bounded_String
(Comp_Type
)
2452 -- If no TSS has been created for the type, check whether there is
2453 -- a primitive equality declared for it.
2456 Op
: constant Node_Id
:=
2457 Build_Eq_Call
(Comp_Type
, Loc
, Lhs
, Rhs
);
2460 -- Use user-defined primitive if it exists, otherwise use
2461 -- predefined equality.
2463 if Present
(Op
) then
2466 return Make_Op_Eq
(Loc
, Lhs
, Rhs
);
2471 return Expand_Record_Equality
(Nod
, Full_Type
, Lhs
, Rhs
);
2474 -- Case of non-record types (always use predefined equality)
2477 -- Print a warning if there is a user-defined "=", because it can be
2478 -- surprising that the predefined "=" takes precedence over it.
2480 -- Suppress the warning if the "user-defined" one is in the
2481 -- predefined library, because those are defined to compose
2482 -- properly by RM-4.5.2(32.1/1). Intrinsics also compose.
2485 Op
: constant Entity_Id
:= Find_Primitive_Eq
(Comp_Type
);
2487 if Warn_On_Ignored_Equality
2488 and then Present
(Op
)
2489 and then not In_Predefined_Unit
(Base_Type
(Comp_Type
))
2490 and then not Is_Intrinsic_Subprogram
(Op
)
2493 (Is_First_Subtype
(Outer_Type
)
2494 or else Is_Generic_Actual_Type
(Outer_Type
));
2495 Error_Msg_Node_1
:= Outer_Type
;
2496 Error_Msg_Node_2
:= Comp_Type
;
2498 ("?_q?""="" for type & uses predefined ""="" for }", Loc
);
2499 Error_Msg_Sloc
:= Sloc
(Op
);
2500 Error_Msg
("\?_q?""="" # is ignored here", Loc
);
2504 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2506 end Expand_Composite_Equality
;
2508 ------------------------
2509 -- Expand_Concatenate --
2510 ------------------------
2512 procedure Expand_Concatenate
(Cnode
: Node_Id
; Opnds
: List_Id
) is
2513 Loc
: constant Source_Ptr
:= Sloc
(Cnode
);
2515 Atyp
: constant Entity_Id
:= Base_Type
(Etype
(Cnode
));
2516 -- Result type of concatenation
2518 Ctyp
: constant Entity_Id
:= Base_Type
(Component_Type
(Etype
(Cnode
)));
2519 -- Component type. Elements of this component type can appear as one
2520 -- of the operands of concatenation as well as arrays.
2522 Istyp
: constant Entity_Id
:= Etype
(First_Index
(Atyp
));
2525 Ityp
: constant Entity_Id
:= Base_Type
(Istyp
);
2526 -- Index type. This is the base type of the index subtype, and is used
2527 -- for all computed bounds (which may be out of range of Istyp in the
2528 -- case of null ranges).
2531 -- This is the type we use to do arithmetic to compute the bounds and
2532 -- lengths of operands. The choice of this type is a little subtle and
2533 -- is discussed in a separate section at the start of the body code.
2535 Result_May_Be_Null
: Boolean := True;
2536 -- Reset to False if at least one operand is encountered which is known
2537 -- at compile time to be non-null. Used for handling the special case
2538 -- of setting the high bound to the last operand high bound for a null
2539 -- result, thus ensuring a proper high bound in the superflat case.
2541 N
: constant Nat
:= List_Length
(Opnds
);
2542 -- Number of concatenation operands including possibly null operands
2545 -- Number of operands excluding any known to be null, except that the
2546 -- last operand is always retained, in case it provides the bounds for
2549 Opnd
: Node_Id
:= Empty
;
2550 -- Current operand being processed in the loop through operands. After
2551 -- this loop is complete, always contains the last operand (which is not
2552 -- the same as Operands (NN), since null operands are skipped).
2554 -- Arrays describing the operands, only the first NN entries of each
2555 -- array are set (NN < N when we exclude known null operands).
2557 Is_Fixed_Length
: array (1 .. N
) of Boolean;
2558 -- True if length of corresponding operand known at compile time
2560 Operands
: array (1 .. N
) of Node_Id
;
2561 -- Set to the corresponding entry in the Opnds list (but note that null
2562 -- operands are excluded, so not all entries in the list are stored).
2564 Fixed_Length
: array (1 .. N
) of Unat
;
2565 -- Set to length of operand. Entries in this array are set only if the
2566 -- corresponding entry in Is_Fixed_Length is True.
2568 Max_Length
: array (1 .. N
) of Unat
;
2569 -- Set to the maximum length of operand, or Too_Large_Length_For_Array
2570 -- if it is not known. Entries in this array are set only if the
2571 -- corresponding entry in Is_Fixed_Length is False;
2573 Opnd_Low_Bound
: array (1 .. N
) of Node_Id
;
2574 -- Set to lower bound of operand. Either an integer literal in the case
2575 -- where the bound is known at compile time, else actual lower bound.
2576 -- The operand low bound is of type Ityp.
2578 Var_Length
: array (1 .. N
) of Entity_Id
;
2579 -- Set to an entity of type Natural that contains the length of an
2580 -- operand whose length is not known at compile time. Entries in this
2581 -- array are set only if the corresponding entry in Is_Fixed_Length
2582 -- is False. The entity is of type Artyp.
2584 Aggr_Length
: array (0 .. N
) of Node_Id
;
2585 -- The J'th entry is an expression node that represents the total length
2586 -- of operands 1 through J. It is either an integer literal node, or a
2587 -- reference to a constant entity with the right value, so it is fine
2588 -- to just do a Copy_Node to get an appropriate copy. The extra zeroth
2589 -- entry always is set to zero. The length is of type Artyp.
2591 Max_Aggr_Length
: Unat
:= Too_Large_Length_For_Array
;
2592 -- Set to the maximum total length, or Too_Large_Length_For_Array at
2593 -- least if it is not known.
2595 Low_Bound
: Node_Id
:= Empty
;
2596 -- A tree node representing the low bound of the result (of type Ityp).
2597 -- This is either an integer literal node, or an identifier reference to
2598 -- a constant entity initialized to the appropriate value.
2600 High_Bound
: Node_Id
:= Empty
;
2601 -- A tree node representing the high bound of the result (of type Ityp)
2603 Last_Opnd_Low_Bound
: Node_Id
:= Empty
;
2604 -- A tree node representing the low bound of the last operand. This
2605 -- need only be set if the result could be null. It is used for the
2606 -- special case of setting the right low bound for a null result.
2607 -- This is of type Ityp.
2609 Last_Opnd_High_Bound
: Node_Id
:= Empty
;
2610 -- A tree node representing the high bound of the last operand. This
2611 -- need only be set if the result could be null. It is used for the
2612 -- special case of setting the right high bound for a null result.
2613 -- This is of type Ityp.
2615 Result
: Node_Id
:= Empty
;
2616 -- Result of the concatenation (of type Ityp)
2618 Actions
: constant List_Id
:= New_List
;
2619 -- Collect actions to be inserted
2621 Known_Non_Null_Operand_Seen
: Boolean;
2622 -- Set True during generation of the assignments of operands into
2623 -- result once an operand known to be non-null has been seen.
2625 function Library_Level_Target
return Boolean;
2626 -- Return True if the concatenation is within the expression of the
2627 -- declaration of a library-level object.
2629 function Make_Artyp_Literal
(Val
: Uint
) return Node_Id
;
2630 -- This function makes an N_Integer_Literal node that is returned in
2631 -- analyzed form with the type set to Artyp. Importantly this literal
2632 -- is not flagged as static, so that if we do computations with it that
2633 -- result in statically detected out of range conditions, we will not
2634 -- generate error messages but instead warning messages.
2636 function To_Artyp
(X
: Node_Id
) return Node_Id
;
2637 -- Given a node of type Ityp, returns the corresponding value of type
2638 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2639 -- For enum types, the Pos of the value is returned.
2641 function To_Ityp
(X
: Node_Id
) return Node_Id
;
2642 -- The inverse function (uses Val in the case of enumeration types)
2644 --------------------------
2645 -- Library_Level_Target --
2646 --------------------------
2648 function Library_Level_Target
return Boolean is
2649 P
: Node_Id
:= Parent
(Cnode
);
2652 while Present
(P
) loop
2653 if Nkind
(P
) = N_Object_Declaration
then
2654 return Is_Library_Level_Entity
(Defining_Identifier
(P
));
2656 -- Prevent the search from going too far
2658 elsif Is_Body_Or_Package_Declaration
(P
) then
2666 end Library_Level_Target
;
2668 ------------------------
2669 -- Make_Artyp_Literal --
2670 ------------------------
2672 function Make_Artyp_Literal
(Val
: Uint
) return Node_Id
is
2673 Result
: constant Node_Id
:= Make_Integer_Literal
(Loc
, Val
);
2675 Set_Etype
(Result
, Artyp
);
2676 Set_Analyzed
(Result
, True);
2677 Set_Is_Static_Expression
(Result
, False);
2679 end Make_Artyp_Literal
;
2685 function To_Artyp
(X
: Node_Id
) return Node_Id
is
2687 if Ityp
= Base_Type
(Artyp
) then
2690 elsif Is_Enumeration_Type
(Ityp
) then
2692 Make_Attribute_Reference
(Loc
,
2693 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
2694 Attribute_Name
=> Name_Pos
,
2695 Expressions
=> New_List
(X
));
2698 return Convert_To
(Artyp
, X
);
2706 function To_Ityp
(X
: Node_Id
) return Node_Id
is
2708 if Is_Enumeration_Type
(Ityp
) then
2710 Make_Attribute_Reference
(Loc
,
2711 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
2712 Attribute_Name
=> Name_Val
,
2713 Expressions
=> New_List
(X
));
2715 -- Case where we will do a type conversion
2718 if Ityp
= Base_Type
(Artyp
) then
2721 return Convert_To
(Ityp
, X
);
2726 -- Local Declarations
2728 Opnd_Typ
: Entity_Id
;
2729 Slice_Rng
: Node_Id
;
2730 Subtyp_Ind
: Node_Id
;
2731 Subtyp_Rng
: Node_Id
;
2738 -- Start of processing for Expand_Concatenate
2741 -- Choose an appropriate computational type
2743 -- We will be doing calculations of lengths and bounds in this routine
2744 -- and computing one from the other in some cases, e.g. getting the high
2745 -- bound by adding the length-1 to the low bound.
2747 -- We can't just use the index type, or even its base type for this
2748 -- purpose for two reasons. First it might be an enumeration type which
2749 -- is not suitable for computations of any kind, and second it may
2750 -- simply not have enough range. For example if the index type is
2751 -- -128..+127 then lengths can be up to 256, which is out of range of
2754 -- For enumeration types, we can simply use Standard_Integer, this is
2755 -- sufficient since the actual number of enumeration literals cannot
2756 -- possibly exceed the range of integer (remember we will be doing the
2757 -- arithmetic with POS values, not representation values).
2759 if Is_Enumeration_Type
(Ityp
) then
2760 Artyp
:= Standard_Integer
;
2762 -- For modular types, we use a 32-bit modular type for types whose size
2763 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2764 -- identity type, and for larger unsigned types we use a 64-bit type.
2766 elsif Is_Modular_Integer_Type
(Ityp
) then
2767 if RM_Size
(Ityp
) < Standard_Integer_Size
then
2768 Artyp
:= Standard_Unsigned
;
2769 elsif RM_Size
(Ityp
) = Standard_Integer_Size
then
2772 Artyp
:= Standard_Long_Long_Unsigned
;
2775 -- Similar treatment for signed types
2778 if RM_Size
(Ityp
) < Standard_Integer_Size
then
2779 Artyp
:= Standard_Integer
;
2780 elsif RM_Size
(Ityp
) = Standard_Integer_Size
then
2783 Artyp
:= Standard_Long_Long_Integer
;
2787 -- Supply dummy entry at start of length array
2789 Aggr_Length
(0) := Make_Artyp_Literal
(Uint_0
);
2791 -- Go through operands setting up the above arrays
2795 Opnd
:= Remove_Head
(Opnds
);
2796 Opnd_Typ
:= Etype
(Opnd
);
2798 -- The parent got messed up when we put the operands in a list,
2799 -- so now put back the proper parent for the saved operand, that
2800 -- is to say the concatenation node, to make sure that each operand
2801 -- is seen as a subexpression, e.g. if actions must be inserted.
2803 Set_Parent
(Opnd
, Cnode
);
2805 -- Set will be True when we have setup one entry in the array
2809 -- Singleton element (or character literal) case
2811 if Base_Type
(Opnd_Typ
) = Ctyp
then
2813 Operands
(NN
) := Opnd
;
2814 Is_Fixed_Length
(NN
) := True;
2815 Fixed_Length
(NN
) := Uint_1
;
2816 Result_May_Be_Null
:= False;
2818 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2819 -- since we know that the result cannot be null).
2821 Opnd_Low_Bound
(NN
) :=
2822 Make_Attribute_Reference
(Loc
,
2823 Prefix
=> New_Occurrence_Of
(Istyp
, Loc
),
2824 Attribute_Name
=> Name_First
);
2828 -- String literal case (can only occur for strings of course)
2830 elsif Nkind
(Opnd
) = N_String_Literal
then
2831 Len
:= String_Literal_Length
(Opnd_Typ
);
2834 Result_May_Be_Null
:= False;
2837 -- Capture last operand low and high bound if result could be null
2839 if J
= N
and then Result_May_Be_Null
then
2840 Last_Opnd_Low_Bound
:=
2841 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
));
2843 Last_Opnd_High_Bound
:=
2844 Make_Op_Subtract
(Loc
,
2846 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
)),
2847 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
2850 -- Skip null string literal
2852 if J
< N
and then Len
= 0 then
2857 Operands
(NN
) := Opnd
;
2858 Is_Fixed_Length
(NN
) := True;
2860 -- Set length and bounds
2862 Fixed_Length
(NN
) := Len
;
2864 Opnd_Low_Bound
(NN
) :=
2865 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
));
2872 -- Check constrained case with known bounds
2874 if Is_Constrained
(Opnd_Typ
)
2875 and then Compile_Time_Known_Bounds
(Opnd_Typ
)
2881 -- Fixed length constrained array type with known at compile
2882 -- time bounds is last case of fixed length operand.
2884 Get_First_Index_Bounds
(Opnd_Typ
, Lo
, Hi
);
2885 Len
:= UI_Max
(Hi
- Lo
+ 1, Uint_0
);
2888 Result_May_Be_Null
:= False;
2891 -- Capture last operand bounds if result could be null
2893 if J
= N
and then Result_May_Be_Null
then
2894 Last_Opnd_Low_Bound
:=
2895 To_Ityp
(Make_Integer_Literal
(Loc
, Lo
));
2897 Last_Opnd_High_Bound
:=
2898 To_Ityp
(Make_Integer_Literal
(Loc
, Hi
));
2901 -- Exclude null length case unless last operand
2903 if J
< N
and then Len
= 0 then
2908 Operands
(NN
) := Opnd
;
2909 Is_Fixed_Length
(NN
) := True;
2910 Fixed_Length
(NN
) := Len
;
2912 Opnd_Low_Bound
(NN
) :=
2913 To_Ityp
(Make_Integer_Literal
(Loc
, Lo
));
2918 -- All cases where the length is not known at compile time, or the
2919 -- special case of an operand which is known to be null but has a
2920 -- lower bound other than 1 or is other than a string type.
2925 -- Capture operand bounds
2927 Opnd_Low_Bound
(NN
) :=
2928 Make_Attribute_Reference
(Loc
,
2930 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
2931 Attribute_Name
=> Name_First
);
2933 -- Capture last operand bounds if result could be null
2935 if J
= N
and Result_May_Be_Null
then
2936 Last_Opnd_Low_Bound
:=
2938 Make_Attribute_Reference
(Loc
,
2940 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
2941 Attribute_Name
=> Name_First
));
2943 Last_Opnd_High_Bound
:=
2945 Make_Attribute_Reference
(Loc
,
2947 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
2948 Attribute_Name
=> Name_Last
));
2951 -- Capture length of operand in entity
2953 Operands
(NN
) := Opnd
;
2954 Is_Fixed_Length
(NN
) := False;
2956 Var_Length
(NN
) := Make_Temporary
(Loc
, 'L');
2958 -- If the operand is a slice, try to compute an upper bound for
2961 if Nkind
(Opnd
) = N_Slice
2962 and then Is_Constrained
(Etype
(Prefix
(Opnd
)))
2963 and then Compile_Time_Known_Bounds
(Etype
(Prefix
(Opnd
)))
2969 Get_First_Index_Bounds
(Etype
(Prefix
(Opnd
)), Lo
, Hi
);
2970 Max_Length
(NN
) := UI_Max
(Hi
- Lo
+ 1, Uint_0
);
2974 Max_Length
(NN
) := Too_Large_Length_For_Array
;
2978 Make_Object_Declaration
(Loc
,
2979 Defining_Identifier
=> Var_Length
(NN
),
2980 Constant_Present
=> True,
2981 Object_Definition
=> New_Occurrence_Of
(Artyp
, Loc
),
2983 Make_Attribute_Reference
(Loc
,
2985 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
2986 Attribute_Name
=> Name_Length
)));
2990 -- Set next entry in aggregate length array
2992 -- For first entry, make either integer literal for fixed length
2993 -- or a reference to the saved length for variable length.
2996 if Is_Fixed_Length
(1) then
2997 Aggr_Length
(1) := Make_Integer_Literal
(Loc
, Fixed_Length
(1));
2998 Max_Aggr_Length
:= Fixed_Length
(1);
3000 Aggr_Length
(1) := New_Occurrence_Of
(Var_Length
(1), Loc
);
3001 Max_Aggr_Length
:= Max_Length
(1);
3004 -- If entry is fixed length and only fixed lengths so far, make
3005 -- appropriate new integer literal adding new length.
3007 elsif Is_Fixed_Length
(NN
)
3008 and then Nkind
(Aggr_Length
(NN
- 1)) = N_Integer_Literal
3011 Make_Integer_Literal
(Loc
,
3012 Intval
=> Fixed_Length
(NN
) + Intval
(Aggr_Length
(NN
- 1)));
3013 Max_Aggr_Length
:= Intval
(Aggr_Length
(NN
));
3015 -- All other cases, construct an addition node for the length and
3016 -- create an entity initialized to this length.
3019 Ent
:= Make_Temporary
(Loc
, 'L');
3021 if Is_Fixed_Length
(NN
) then
3022 Clen
:= Make_Integer_Literal
(Loc
, Fixed_Length
(NN
));
3023 Max_Aggr_Length
:= Max_Aggr_Length
+ Fixed_Length
(NN
);
3026 Clen
:= New_Occurrence_Of
(Var_Length
(NN
), Loc
);
3027 Max_Aggr_Length
:= Max_Aggr_Length
+ Max_Length
(NN
);
3031 Make_Object_Declaration
(Loc
,
3032 Defining_Identifier
=> Ent
,
3033 Constant_Present
=> True,
3034 Object_Definition
=> New_Occurrence_Of
(Artyp
, Loc
),
3037 Left_Opnd
=> New_Copy_Tree
(Aggr_Length
(NN
- 1)),
3038 Right_Opnd
=> Clen
)));
3040 Aggr_Length
(NN
) := Make_Identifier
(Loc
, Chars
=> Chars
(Ent
));
3047 -- If we have only skipped null operands, return the last operand
3054 -- If we have only one non-null operand, return it and we are done.
3055 -- There is one case in which this cannot be done, and that is when
3056 -- the sole operand is of the element type, in which case it must be
3057 -- converted to an array, and the easiest way of doing that is to go
3058 -- through the normal general circuit.
3060 if NN
= 1 and then Base_Type
(Etype
(Operands
(1))) /= Ctyp
then
3061 Result
:= Operands
(1);
3065 -- Cases where we have a real concatenation
3067 -- Next step is to find the low bound for the result array that we
3068 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3070 -- If the ultimate ancestor of the index subtype is a constrained array
3071 -- definition, then the lower bound is that of the index subtype as
3072 -- specified by (RM 4.5.3(6)).
3074 -- The right test here is to go to the root type, and then the ultimate
3075 -- ancestor is the first subtype of this root type.
3077 if Is_Constrained
(First_Subtype
(Root_Type
(Atyp
))) then
3079 Make_Attribute_Reference
(Loc
,
3081 New_Occurrence_Of
(First_Subtype
(Root_Type
(Atyp
)), Loc
),
3082 Attribute_Name
=> Name_First
);
3084 -- If the first operand in the list has known length we know that
3085 -- the lower bound of the result is the lower bound of this operand.
3087 elsif Is_Fixed_Length
(1) then
3088 Low_Bound
:= Opnd_Low_Bound
(1);
3090 -- OK, we don't know the lower bound, we have to build a horrible
3091 -- if expression node of the form
3093 -- if Cond1'Length /= 0 then
3096 -- if Opnd2'Length /= 0 then
3101 -- The nesting ends either when we hit an operand whose length is known
3102 -- at compile time, or on reaching the last operand, whose low bound we
3103 -- take unconditionally whether or not it is null. It's easiest to do
3104 -- this with a recursive procedure:
3108 function Get_Known_Bound
(J
: Nat
) return Node_Id
;
3109 -- Returns the lower bound determined by operands J .. NN
3111 ---------------------
3112 -- Get_Known_Bound --
3113 ---------------------
3115 function Get_Known_Bound
(J
: Nat
) return Node_Id
is
3117 if Is_Fixed_Length
(J
) or else J
= NN
then
3118 return New_Copy_Tree
(Opnd_Low_Bound
(J
));
3122 Make_If_Expression
(Loc
,
3123 Expressions
=> New_List
(
3127 New_Occurrence_Of
(Var_Length
(J
), Loc
),
3129 Make_Integer_Literal
(Loc
, 0)),
3131 New_Copy_Tree
(Opnd_Low_Bound
(J
)),
3132 Get_Known_Bound
(J
+ 1)));
3134 end Get_Known_Bound
;
3137 Ent
:= Make_Temporary
(Loc
, 'L');
3140 Make_Object_Declaration
(Loc
,
3141 Defining_Identifier
=> Ent
,
3142 Constant_Present
=> True,
3143 Object_Definition
=> New_Occurrence_Of
(Ityp
, Loc
),
3144 Expression
=> Get_Known_Bound
(1)));
3146 Low_Bound
:= New_Occurrence_Of
(Ent
, Loc
);
3150 pragma Assert
(Present
(Low_Bound
));
3152 -- Now we can compute the high bound as Low_Bound + Length - 1
3154 if Compile_Time_Known_Value
(Low_Bound
)
3155 and then Nkind
(Aggr_Length
(NN
)) = N_Integer_Literal
3160 (Expr_Value
(Low_Bound
) + Intval
(Aggr_Length
(NN
)) - 1));
3166 Left_Opnd
=> To_Artyp
(New_Copy_Tree
(Low_Bound
)),
3168 Make_Op_Subtract
(Loc
,
3169 Left_Opnd
=> New_Copy_Tree
(Aggr_Length
(NN
)),
3170 Right_Opnd
=> Make_Artyp_Literal
(Uint_1
))));
3172 -- Note that calculation of the high bound may cause overflow in some
3173 -- very weird cases, so in the general case we need an overflow check
3174 -- on the high bound. We can avoid this for the common case of string
3175 -- types and other types whose index is Positive, since we chose a
3176 -- wider range for the arithmetic type. If checks are suppressed, we
3177 -- do not set the flag so superfluous warnings may be omitted.
3179 if Istyp
/= Standard_Positive
3180 and then not Overflow_Checks_Suppressed
(Istyp
)
3182 Activate_Overflow_Check
(High_Bound
);
3186 -- Handle the exceptional case where the result is null, in which case
3187 -- case the bounds come from the last operand (so that we get the proper
3188 -- bounds if the last operand is superflat).
3190 if Result_May_Be_Null
then
3192 Make_If_Expression
(Loc
,
3193 Expressions
=> New_List
(
3195 Left_Opnd
=> New_Copy_Tree
(Aggr_Length
(NN
)),
3196 Right_Opnd
=> Make_Artyp_Literal
(Uint_0
)),
3197 Last_Opnd_Low_Bound
,
3201 Make_If_Expression
(Loc
,
3202 Expressions
=> New_List
(
3204 Left_Opnd
=> New_Copy_Tree
(Aggr_Length
(NN
)),
3205 Right_Opnd
=> Make_Artyp_Literal
(Uint_0
)),
3206 Last_Opnd_High_Bound
,
3210 -- Here is where we insert the saved up actions
3212 Insert_Actions
(Cnode
, Actions
, Suppress
=> All_Checks
);
3214 -- If the low bound is known at compile time and not the high bound, but
3215 -- we have computed a sensible upper bound for the length, then adjust
3216 -- the high bound for the subtype of the array. This will change it into
3217 -- a static subtype and thus help the code generator.
3219 if Compile_Time_Known_Value
(Low_Bound
)
3220 and then not Compile_Time_Known_Value
(High_Bound
)
3221 and then Max_Aggr_Length
< Too_Large_Length_For_Array
3224 Known_High_Bound
: constant Node_Id
:=
3227 (Expr_Value
(Low_Bound
) + Max_Aggr_Length
- 1));
3230 if not Is_Out_Of_Range
(Known_High_Bound
, Ityp
) then
3231 Slice_Rng
:= Make_Range
(Loc
, Low_Bound
, High_Bound
);
3232 High_Bound
:= Known_High_Bound
;
3243 Subtyp_Rng
:= Make_Range
(Loc
, Low_Bound
, High_Bound
);
3245 -- If the result cannot be null then the range cannot be superflat
3247 Set_Cannot_Be_Superflat
(Subtyp_Rng
, not Result_May_Be_Null
);
3249 -- Now we construct an array object with appropriate bounds. We mark
3250 -- the target as internal to prevent useless initialization when
3251 -- Initialize_Scalars is enabled. Also since this is the actual result
3252 -- entity, we make sure we have debug information for the result.
3255 Make_Subtype_Indication
(Loc
,
3256 Subtype_Mark
=> New_Occurrence_Of
(Atyp
, Loc
),
3258 Make_Index_Or_Discriminant_Constraint
(Loc
,
3259 Constraints
=> New_List
(Subtyp_Rng
)));
3261 Ent
:= Make_Temporary
(Loc
, 'S');
3262 Set_Is_Internal
(Ent
);
3263 Set_Debug_Info_Needed
(Ent
);
3265 -- If we are concatenating strings and the current scope already uses
3266 -- the secondary stack, allocate the result also on the secondary stack
3267 -- to avoid putting too much pressure on the primary stack.
3269 -- Don't do this if -gnatd.h is set, as this will break the wrapping of
3270 -- Cnode in an Expression_With_Actions, see Expand_N_Op_Concat.
3272 if Atyp
= Standard_String
3273 and then Uses_Sec_Stack
(Current_Scope
)
3274 and then RTE_Available
(RE_SS_Pool
)
3275 and then not Debug_Flag_Dot_H
3278 -- subtype Axx is String (<low-bound> .. <high-bound>)
3279 -- type Ayy is access Axx;
3280 -- Rxx : Ayy := new <Axx> [storage_pool = ss_pool];
3281 -- Sxx : Axx renames Rxx.all;
3284 ConstrT
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
3285 Acc_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
3291 Insert_Action
(Cnode
,
3292 Make_Subtype_Declaration
(Loc
,
3293 Defining_Identifier
=> ConstrT
,
3294 Subtype_Indication
=> Subtyp_Ind
),
3295 Suppress
=> All_Checks
);
3297 Freeze_Itype
(ConstrT
, Cnode
);
3299 Insert_Action
(Cnode
,
3300 Make_Full_Type_Declaration
(Loc
,
3301 Defining_Identifier
=> Acc_Typ
,
3303 Make_Access_To_Object_Definition
(Loc
,
3304 Subtype_Indication
=> New_Occurrence_Of
(ConstrT
, Loc
))),
3305 Suppress
=> All_Checks
);
3307 Mutate_Ekind
(Acc_Typ
, E_Access_Type
);
3308 Set_Associated_Storage_Pool
(Acc_Typ
, RTE
(RE_SS_Pool
));
3311 Make_Allocator
(Loc
,
3312 Expression
=> New_Occurrence_Of
(ConstrT
, Loc
));
3314 -- This is currently done only for type String, which normally
3315 -- doesn't have default initialization, but we need to set the
3316 -- No_Initialization flag in case of either Initialize_Scalars
3317 -- or Normalize_Scalars.
3319 Set_No_Initialization
(Alloc
);
3321 Temp
:= Make_Temporary
(Loc
, 'R', Alloc
);
3322 Insert_Action
(Cnode
,
3323 Make_Object_Declaration
(Loc
,
3324 Defining_Identifier
=> Temp
,
3325 Object_Definition
=> New_Occurrence_Of
(Acc_Typ
, Loc
),
3326 Expression
=> Alloc
),
3327 Suppress
=> All_Checks
);
3329 Insert_Action
(Cnode
,
3330 Make_Object_Renaming_Declaration
(Loc
,
3331 Defining_Identifier
=> Ent
,
3332 Subtype_Mark
=> New_Occurrence_Of
(ConstrT
, Loc
),
3334 Make_Explicit_Dereference
(Loc
,
3335 Prefix
=> New_Occurrence_Of
(Temp
, Loc
))),
3336 Suppress
=> All_Checks
);
3340 -- If the bound is statically known to be out of range, we do not
3341 -- want to abort, we want a warning and a runtime constraint error.
3342 -- Note that we have arranged that the result will not be treated
3343 -- as a static constant, so we won't get an illegality during this
3344 -- insertion. We also enable checks (in particular range checks) in
3345 -- case the bounds of Subtyp_Ind are out of range.
3347 Insert_Action
(Cnode
,
3348 Make_Object_Declaration
(Loc
,
3349 Defining_Identifier
=> Ent
,
3350 Object_Definition
=> Subtyp_Ind
));
3353 -- If the result of the concatenation appears as the initializing
3354 -- expression of an object declaration, we can just rename the
3355 -- result, rather than copying it.
3357 Set_OK_To_Rename
(Ent
);
3359 -- Catch the static out of range case now
3361 if Raises_Constraint_Error
(High_Bound
)
3362 or else Is_Out_Of_Range
(High_Bound
, Ityp
)
3364 -- Kill warning generated for the declaration of the static out of
3365 -- range high bound, and instead generate a Constraint_Error with
3366 -- an appropriate specific message.
3368 if Nkind
(High_Bound
) = N_Integer_Literal
then
3369 Kill_Dead_Code
(High_Bound
);
3370 Rewrite
(High_Bound
, New_Copy_Tree
(Low_Bound
));
3373 Kill_Dead_Code
(Declaration_Node
(Entity
(High_Bound
)));
3376 Apply_Compile_Time_Constraint_Error
3378 Msg
=> "concatenation result upper bound out of range??",
3379 Reason
=> CE_Range_Check_Failed
);
3384 -- Now we will generate the assignments to do the actual concatenation
3386 -- There is one case in which we will not do this, namely when all the
3387 -- following conditions are met:
3389 -- The result type is Standard.String
3391 -- There are nine or fewer retained (non-null) operands
3393 -- The optimization level is -O0 or the debug flag gnatd.C is set,
3394 -- and the debug flag gnatd.c is not set.
3396 -- The corresponding System.Concat_n.Str_Concat_n routine is
3397 -- available in the run time.
3399 -- If all these conditions are met then we generate a call to the
3400 -- relevant concatenation routine. The purpose of this is to avoid
3401 -- undesirable code bloat at -O0.
3403 -- If the concatenation is within the declaration of a library-level
3404 -- object, we call the built-in concatenation routines to prevent code
3405 -- bloat, regardless of the optimization level. This is space efficient
3406 -- and prevents linking problems when units are compiled with different
3407 -- optimization levels.
3409 if Atyp
= Standard_String
3410 and then NN
in 2 .. 9
3411 and then (((Optimization_Level
= 0 or else Debug_Flag_Dot_CC
)
3412 and then not Debug_Flag_Dot_C
)
3413 or else Library_Level_Target
)
3416 RR
: constant array (Nat
range 2 .. 9) of RE_Id
:=
3427 if RTE_Available
(RR
(NN
)) then
3429 Opnds
: constant List_Id
:=
3430 New_List
(New_Occurrence_Of
(Ent
, Loc
));
3433 for J
in 1 .. NN
loop
3434 if Is_List_Member
(Operands
(J
)) then
3435 Remove
(Operands
(J
));
3438 if Base_Type
(Etype
(Operands
(J
))) = Ctyp
then
3440 Make_Aggregate
(Loc
,
3441 Component_Associations
=> New_List
(
3442 Make_Component_Association
(Loc
,
3443 Choices
=> New_List
(
3444 Make_Integer_Literal
(Loc
, 1)),
3445 Expression
=> Operands
(J
)))));
3448 Append_To
(Opnds
, Operands
(J
));
3452 Insert_Action
(Cnode
,
3453 Make_Procedure_Call_Statement
(Loc
,
3454 Name
=> New_Occurrence_Of
(RTE
(RR
(NN
)), Loc
),
3455 Parameter_Associations
=> Opnds
));
3457 -- No assignments left to do below
3465 -- Not special case so generate the assignments
3467 Known_Non_Null_Operand_Seen
:= False;
3469 for J
in 1 .. NN
loop
3471 Lo
: constant Node_Id
:=
3473 Left_Opnd
=> To_Artyp
(New_Copy_Tree
(Low_Bound
)),
3474 Right_Opnd
=> Aggr_Length
(J
- 1));
3476 Hi
: constant Node_Id
:=
3478 Left_Opnd
=> To_Artyp
(New_Copy_Tree
(Low_Bound
)),
3480 Make_Op_Subtract
(Loc
,
3481 Left_Opnd
=> Aggr_Length
(J
),
3482 Right_Opnd
=> Make_Artyp_Literal
(Uint_1
)));
3485 -- Singleton case, simple assignment
3487 if Base_Type
(Etype
(Operands
(J
))) = Ctyp
then
3488 Known_Non_Null_Operand_Seen
:= True;
3489 Insert_Action
(Cnode
,
3490 Make_Assignment_Statement
(Loc
,
3492 Make_Indexed_Component
(Loc
,
3493 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
3494 Expressions
=> New_List
(To_Ityp
(Lo
))),
3495 Expression
=> Operands
(J
)),
3496 Suppress
=> All_Checks
);
3498 -- Array case, slice assignment, skipped when argument is fixed
3499 -- length and known to be null.
3501 elsif not Is_Fixed_Length
(J
) or else Fixed_Length
(J
) > 0 then
3504 Make_Assignment_Statement
(Loc
,
3508 New_Occurrence_Of
(Ent
, Loc
),
3511 Low_Bound
=> To_Ityp
(Lo
),
3512 High_Bound
=> To_Ityp
(Hi
))),
3513 Expression
=> Operands
(J
));
3515 if Is_Fixed_Length
(J
) then
3516 Known_Non_Null_Operand_Seen
:= True;
3518 elsif not Known_Non_Null_Operand_Seen
then
3520 -- Here if operand length is not statically known and no
3521 -- operand known to be non-null has been processed yet.
3522 -- If operand length is 0, we do not need to perform the
3523 -- assignment, and we must avoid the evaluation of the
3524 -- high bound of the slice, since it may underflow if the
3525 -- low bound is Ityp'First.
3528 Make_Implicit_If_Statement
(Cnode
,
3532 New_Occurrence_Of
(Var_Length
(J
), Loc
),
3533 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
3534 Then_Statements
=> New_List
(Assign
));
3537 Insert_Action
(Cnode
, Assign
, Suppress
=> All_Checks
);
3543 -- Finally we build the result, which is either a direct reference to
3544 -- the array object or a slice of it.
3546 Result
:= New_Occurrence_Of
(Ent
, Loc
);
3548 if Present
(Slice_Rng
) then
3549 Result
:= Make_Slice
(Loc
, Result
, Slice_Rng
);
3553 pragma Assert
(Present
(Result
));
3554 Rewrite
(Cnode
, Result
);
3555 Analyze_And_Resolve
(Cnode
, Atyp
);
3556 end Expand_Concatenate
;
3558 ---------------------------------------------------
3559 -- Expand_Membership_Minimize_Eliminate_Overflow --
3560 ---------------------------------------------------
3562 procedure Expand_Membership_Minimize_Eliminate_Overflow
(N
: Node_Id
) is
3563 pragma Assert
(Nkind
(N
) = N_In
);
3564 -- Despite the name, this routine applies only to N_In, not to
3565 -- N_Not_In. The latter is always rewritten as not (X in Y).
3567 Result_Type
: constant Entity_Id
:= Etype
(N
);
3568 -- Capture result type, may be a derived boolean type
3570 Loc
: constant Source_Ptr
:= Sloc
(N
);
3571 Lop
: constant Node_Id
:= Left_Opnd
(N
);
3572 Rop
: constant Node_Id
:= Right_Opnd
(N
);
3574 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3575 -- is thus tempting to capture these values, but due to the rewrites
3576 -- that occur as a result of overflow checking, these values change
3577 -- as we go along, and it is safe just to always use Etype explicitly.
3579 Restype
: constant Entity_Id
:= Etype
(N
);
3583 -- Bounds in Minimize calls, not used currently
3585 LLIB
: constant Entity_Id
:= Base_Type
(Standard_Long_Long_Integer
);
3586 -- Entity for Long_Long_Integer'Base
3589 Minimize_Eliminate_Overflows
(Lop
, Lo
, Hi
, Top_Level
=> False);
3591 -- If right operand is a subtype name, and the subtype name has no
3592 -- predicate, then we can just replace the right operand with an
3593 -- explicit range T'First .. T'Last, and use the explicit range code.
3595 if Nkind
(Rop
) /= N_Range
3596 and then No
(Predicate_Function
(Etype
(Rop
)))
3599 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
3604 Make_Attribute_Reference
(Loc
,
3605 Attribute_Name
=> Name_First
,
3606 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
)),
3608 Make_Attribute_Reference
(Loc
,
3609 Attribute_Name
=> Name_Last
,
3610 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
))));
3611 Analyze_And_Resolve
(Rop
, Rtyp
, Suppress
=> All_Checks
);
3615 -- Here for the explicit range case. Note that the bounds of the range
3616 -- have not been processed for minimized or eliminated checks.
3618 if Nkind
(Rop
) = N_Range
then
3619 Minimize_Eliminate_Overflows
3620 (Low_Bound
(Rop
), Lo
, Hi
, Top_Level
=> False);
3621 Minimize_Eliminate_Overflows
3622 (High_Bound
(Rop
), Lo
, Hi
, Top_Level
=> False);
3624 -- We have A in B .. C, treated as A >= B and then A <= C
3628 if Is_RTE
(Etype
(Lop
), RE_Bignum
)
3629 or else Is_RTE
(Etype
(Low_Bound
(Rop
)), RE_Bignum
)
3630 or else Is_RTE
(Etype
(High_Bound
(Rop
)), RE_Bignum
)
3633 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
3634 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
3635 L
: constant Entity_Id
:=
3636 Make_Defining_Identifier
(Loc
, Name_uL
);
3637 Lopnd
: constant Node_Id
:= Convert_To_Bignum
(Lop
);
3638 Lbound
: constant Node_Id
:=
3639 Convert_To_Bignum
(Low_Bound
(Rop
));
3640 Hbound
: constant Node_Id
:=
3641 Convert_To_Bignum
(High_Bound
(Rop
));
3643 -- Now we rewrite the membership test node to look like
3646 -- Bnn : Result_Type;
3648 -- M : Mark_Id := SS_Mark;
3649 -- L : Bignum := Lopnd;
3651 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3659 -- Insert declaration of L into declarations of bignum block
3662 (Last
(Declarations
(Blk
)),
3663 Make_Object_Declaration
(Loc
,
3664 Defining_Identifier
=> L
,
3665 Object_Definition
=>
3666 New_Occurrence_Of
(RTE
(RE_Bignum
), Loc
),
3667 Expression
=> Lopnd
));
3669 -- Insert assignment to Bnn into expressions of bignum block
3672 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
3673 Make_Assignment_Statement
(Loc
,
3674 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
3678 Make_Function_Call
(Loc
,
3680 New_Occurrence_Of
(RTE
(RE_Big_GE
), Loc
),
3681 Parameter_Associations
=> New_List
(
3682 New_Occurrence_Of
(L
, Loc
),
3686 Make_Function_Call
(Loc
,
3688 New_Occurrence_Of
(RTE
(RE_Big_LE
), Loc
),
3689 Parameter_Associations
=> New_List
(
3690 New_Occurrence_Of
(L
, Loc
),
3693 -- Now rewrite the node
3696 Make_Expression_With_Actions
(Loc
,
3697 Actions
=> New_List
(
3698 Make_Object_Declaration
(Loc
,
3699 Defining_Identifier
=> Bnn
,
3700 Object_Definition
=>
3701 New_Occurrence_Of
(Result_Type
, Loc
)),
3703 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
3704 Analyze_And_Resolve
(N
, Result_Type
);
3708 -- Here if no bignums around
3711 -- Case where types are all the same
3713 if Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(Low_Bound
(Rop
)))
3715 Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(High_Bound
(Rop
)))
3719 -- If types are not all the same, it means that we have rewritten
3720 -- at least one of them to be of type Long_Long_Integer, and we
3721 -- will convert the other operands to Long_Long_Integer.
3724 Convert_To_And_Rewrite
(LLIB
, Lop
);
3725 Set_Analyzed
(Lop
, False);
3726 Analyze_And_Resolve
(Lop
, LLIB
);
3728 -- For the right operand, avoid unnecessary recursion into
3729 -- this routine, we know that overflow is not possible.
3731 Convert_To_And_Rewrite
(LLIB
, Low_Bound
(Rop
));
3732 Convert_To_And_Rewrite
(LLIB
, High_Bound
(Rop
));
3733 Set_Analyzed
(Rop
, False);
3734 Analyze_And_Resolve
(Rop
, LLIB
, Suppress
=> Overflow_Check
);
3737 -- Now the three operands are of the same signed integer type,
3738 -- so we can use the normal expansion routine for membership,
3739 -- setting the flag to prevent recursion into this procedure.
3741 Set_No_Minimize_Eliminate
(N
);
3745 -- Right operand is a subtype name and the subtype has a predicate. We
3746 -- have to make sure the predicate is checked, and for that we need to
3747 -- use the standard N_In circuitry with appropriate types.
3750 pragma Assert
(Present
(Predicate_Function
(Etype
(Rop
))));
3752 -- If types are "right", just call Expand_N_In preventing recursion
3754 if Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(Rop
)) then
3755 Set_No_Minimize_Eliminate
(N
);
3760 elsif Is_RTE
(Etype
(Lop
), RE_Bignum
) then
3762 -- For X in T, we want to rewrite our node as
3765 -- Bnn : Result_Type;
3768 -- M : Mark_Id := SS_Mark;
3769 -- Lnn : Long_Long_Integer'Base
3775 -- if not Bignum_In_LLI_Range (Nnn) then
3778 -- Lnn := From_Bignum (Nnn);
3780 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3781 -- and then T'Base (Lnn) in T;
3790 -- A bit gruesome, but there doesn't seem to be a simpler way
3793 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
3794 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
3795 Lnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'L', N
);
3796 Nnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'N', N
);
3797 T
: constant Entity_Id
:= Etype
(Rop
);
3798 TB
: constant Entity_Id
:= Base_Type
(T
);
3802 -- Mark the last membership operation to prevent recursion
3806 Left_Opnd
=> Convert_To
(TB
, New_Occurrence_Of
(Lnn
, Loc
)),
3807 Right_Opnd
=> New_Occurrence_Of
(T
, Loc
));
3808 Set_No_Minimize_Eliminate
(Nin
);
3810 -- Now decorate the block
3813 (Last
(Declarations
(Blk
)),
3814 Make_Object_Declaration
(Loc
,
3815 Defining_Identifier
=> Lnn
,
3816 Object_Definition
=> New_Occurrence_Of
(LLIB
, Loc
)));
3819 (Last
(Declarations
(Blk
)),
3820 Make_Object_Declaration
(Loc
,
3821 Defining_Identifier
=> Nnn
,
3822 Object_Definition
=>
3823 New_Occurrence_Of
(RTE
(RE_Bignum
), Loc
)));
3826 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
3828 Make_Assignment_Statement
(Loc
,
3829 Name
=> New_Occurrence_Of
(Nnn
, Loc
),
3830 Expression
=> Relocate_Node
(Lop
)),
3832 Make_Implicit_If_Statement
(N
,
3836 Make_Function_Call
(Loc
,
3839 (RTE
(RE_Bignum_In_LLI_Range
), Loc
),
3840 Parameter_Associations
=> New_List
(
3841 New_Occurrence_Of
(Nnn
, Loc
)))),
3843 Then_Statements
=> New_List
(
3844 Make_Assignment_Statement
(Loc
,
3845 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
3847 New_Occurrence_Of
(Standard_False
, Loc
))),
3849 Else_Statements
=> New_List
(
3850 Make_Assignment_Statement
(Loc
,
3851 Name
=> New_Occurrence_Of
(Lnn
, Loc
),
3853 Make_Function_Call
(Loc
,
3855 New_Occurrence_Of
(RTE
(RE_From_Bignum
), Loc
),
3856 Parameter_Associations
=> New_List
(
3857 New_Occurrence_Of
(Nnn
, Loc
)))),
3859 Make_Assignment_Statement
(Loc
,
3860 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
3865 Left_Opnd
=> New_Occurrence_Of
(Lnn
, Loc
),
3870 Make_Attribute_Reference
(Loc
,
3871 Attribute_Name
=> Name_First
,
3873 New_Occurrence_Of
(TB
, Loc
))),
3877 Make_Attribute_Reference
(Loc
,
3878 Attribute_Name
=> Name_Last
,
3880 New_Occurrence_Of
(TB
, Loc
))))),
3882 Right_Opnd
=> Nin
))))));
3884 -- Now we can do the rewrite
3887 Make_Expression_With_Actions
(Loc
,
3888 Actions
=> New_List
(
3889 Make_Object_Declaration
(Loc
,
3890 Defining_Identifier
=> Bnn
,
3891 Object_Definition
=>
3892 New_Occurrence_Of
(Result_Type
, Loc
)),
3894 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
3895 Analyze_And_Resolve
(N
, Result_Type
);
3899 -- Not bignum case, but types don't match (this means we rewrote the
3900 -- left operand to be Long_Long_Integer).
3903 pragma Assert
(Base_Type
(Etype
(Lop
)) = LLIB
);
3905 -- We rewrite the membership test as (where T is the type with
3906 -- the predicate, i.e. the type of the right operand)
3908 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3909 -- and then T'Base (Lop) in T
3912 T
: constant Entity_Id
:= Etype
(Rop
);
3913 TB
: constant Entity_Id
:= Base_Type
(T
);
3917 -- The last membership test is marked to prevent recursion
3921 Left_Opnd
=> Convert_To
(TB
, Duplicate_Subexpr
(Lop
)),
3922 Right_Opnd
=> New_Occurrence_Of
(T
, Loc
));
3923 Set_No_Minimize_Eliminate
(Nin
);
3925 -- Now do the rewrite
3936 Make_Attribute_Reference
(Loc
,
3937 Attribute_Name
=> Name_First
,
3939 New_Occurrence_Of
(TB
, Loc
))),
3942 Make_Attribute_Reference
(Loc
,
3943 Attribute_Name
=> Name_Last
,
3945 New_Occurrence_Of
(TB
, Loc
))))),
3946 Right_Opnd
=> Nin
));
3947 Set_Analyzed
(N
, False);
3948 Analyze_And_Resolve
(N
, Restype
);
3952 end Expand_Membership_Minimize_Eliminate_Overflow
;
3954 ---------------------------------
3955 -- Expand_Nonbinary_Modular_Op --
3956 ---------------------------------
3958 procedure Expand_Nonbinary_Modular_Op
(N
: Node_Id
) is
3959 Loc
: constant Source_Ptr
:= Sloc
(N
);
3960 Typ
: constant Entity_Id
:= Etype
(N
);
3962 procedure Expand_Modular_Addition
;
3963 -- Expand the modular addition, handling the special case of adding a
3966 procedure Expand_Modular_Op
;
3967 -- Compute the general rule: (lhs OP rhs) mod Modulus
3969 procedure Expand_Modular_Subtraction
;
3970 -- Expand the modular addition, handling the special case of subtracting
3973 -----------------------------
3974 -- Expand_Modular_Addition --
3975 -----------------------------
3977 procedure Expand_Modular_Addition
is
3979 -- If this is not the addition of a constant then compute it using
3980 -- the general rule: (lhs + rhs) mod Modulus
3982 if Nkind
(Right_Opnd
(N
)) /= N_Integer_Literal
then
3985 -- If this is an addition of a constant, convert it to a subtraction
3986 -- plus a conditional expression since we can compute it faster than
3987 -- computing the modulus.
3989 -- modMinusRhs = Modulus - rhs
3990 -- if lhs < modMinusRhs then lhs + rhs
3991 -- else lhs - modMinusRhs
3995 Mod_Minus_Right
: constant Uint
:=
3996 Modulus
(Typ
) - Intval
(Right_Opnd
(N
));
3998 Cond_Expr
: Node_Id
;
3999 Then_Expr
: Node_Id
;
4000 Else_Expr
: Node_Id
;
4002 -- To prevent spurious visibility issues, convert all
4003 -- operands to Standard.Unsigned.
4008 Unchecked_Convert_To
(Standard_Unsigned
,
4009 New_Copy_Tree
(Left_Opnd
(N
))),
4011 Make_Integer_Literal
(Loc
, Mod_Minus_Right
));
4016 Unchecked_Convert_To
(Standard_Unsigned
,
4017 New_Copy_Tree
(Left_Opnd
(N
))),
4019 Make_Integer_Literal
(Loc
, Intval
(Right_Opnd
(N
))));
4022 Make_Op_Subtract
(Loc
,
4024 Unchecked_Convert_To
(Standard_Unsigned
,
4025 New_Copy_Tree
(Left_Opnd
(N
))),
4027 Make_Integer_Literal
(Loc
, Mod_Minus_Right
));
4030 Unchecked_Convert_To
(Typ
,
4031 Make_If_Expression
(Loc
,
4033 New_List
(Cond_Expr
, Then_Expr
, Else_Expr
))));
4036 end Expand_Modular_Addition
;
4038 -----------------------
4039 -- Expand_Modular_Op --
4040 -----------------------
4042 procedure Expand_Modular_Op
is
4043 -- We will convert to another type (not a nonbinary-modulus modular
4044 -- type), evaluate the op in that representation, reduce the result,
4045 -- and convert back to the original type. This means that the
4046 -- backend does not have to deal with nonbinary-modulus ops.
4048 Op_Expr
: constant Node_Id
:= New_Op_Node
(Nkind
(N
), Loc
);
4051 Target_Type
: Entity_Id
;
4053 -- Select a target type that is large enough to avoid spurious
4054 -- intermediate overflow on pre-reduction computation (for
4055 -- correctness) but is no larger than is needed (for performance).
4058 Required_Size
: Uint
:= RM_Size
(Etype
(N
));
4059 Use_Unsigned
: Boolean := True;
4063 -- For example, if modulus is 255 then RM_Size will be 8
4064 -- and the range of possible values (before reduction) will
4065 -- be 0 .. 508; that range requires 9 bits.
4066 Required_Size
:= Required_Size
+ 1;
4068 when N_Op_Subtract
=>
4069 -- For example, if modulus is 255 then RM_Size will be 8
4070 -- and the range of possible values (before reduction) will
4071 -- be -254 .. 254; that range requires 9 bits, signed.
4072 Use_Unsigned
:= False;
4073 Required_Size
:= Required_Size
+ 1;
4075 when N_Op_Multiply
=>
4076 -- For example, if modulus is 255 then RM_Size will be 8
4077 -- and the range of possible values (before reduction) will
4078 -- be 0 .. 64,516; that range requires 16 bits.
4079 Required_Size
:= Required_Size
* 2;
4085 if Use_Unsigned
then
4086 if Required_Size
<= Standard_Short_Short_Integer_Size
then
4087 Target_Type
:= Standard_Short_Short_Unsigned
;
4088 elsif Required_Size
<= Standard_Short_Integer_Size
then
4089 Target_Type
:= Standard_Short_Unsigned
;
4090 elsif Required_Size
<= Standard_Integer_Size
then
4091 Target_Type
:= Standard_Unsigned
;
4093 pragma Assert
(Required_Size
<= 64);
4094 Target_Type
:= Standard_Unsigned_64
;
4096 elsif Required_Size
<= 8 then
4097 Target_Type
:= Standard_Integer_8
;
4098 elsif Required_Size
<= 16 then
4099 Target_Type
:= Standard_Integer_16
;
4100 elsif Required_Size
<= 32 then
4101 Target_Type
:= Standard_Integer_32
;
4103 pragma Assert
(Required_Size
<= 64);
4104 Target_Type
:= Standard_Integer_64
;
4107 pragma Assert
(Present
(Target_Type
));
4110 Set_Left_Opnd
(Op_Expr
,
4111 Unchecked_Convert_To
(Target_Type
,
4112 New_Copy_Tree
(Left_Opnd
(N
))));
4113 Set_Right_Opnd
(Op_Expr
,
4114 Unchecked_Convert_To
(Target_Type
,
4115 New_Copy_Tree
(Right_Opnd
(N
))));
4117 -- ??? Why do this stuff for some ops and not others?
4118 if Nkind
(N
) not in N_Op_And | N_Op_Or | N_Op_Xor
then
4120 -- Link this node to the tree to analyze it
4122 -- If the parent node is an expression with actions we link it to
4123 -- N since otherwise Force_Evaluation cannot identify if this node
4124 -- comes from the Expression and rejects generating the temporary.
4126 if Nkind
(Parent
(N
)) = N_Expression_With_Actions
then
4127 Set_Parent
(Op_Expr
, N
);
4132 Set_Parent
(Op_Expr
, Parent
(N
));
4137 -- Force generating a temporary because in the expansion of this
4138 -- expression we may generate code that performs this computation
4141 Force_Evaluation
(Op_Expr
, Mode
=> Strict
);
4146 Left_Opnd
=> Op_Expr
,
4147 Right_Opnd
=> Make_Integer_Literal
(Loc
, Modulus
(Typ
)));
4150 Unchecked_Convert_To
(Typ
, Mod_Expr
));
4151 end Expand_Modular_Op
;
4153 --------------------------------
4154 -- Expand_Modular_Subtraction --
4155 --------------------------------
4157 procedure Expand_Modular_Subtraction
is
4159 -- If this is not the addition of a constant then compute it using
4160 -- the general rule: (lhs + rhs) mod Modulus
4162 if Nkind
(Right_Opnd
(N
)) /= N_Integer_Literal
then
4165 -- If this is an addition of a constant, convert it to a subtraction
4166 -- plus a conditional expression since we can compute it faster than
4167 -- computing the modulus.
4169 -- modMinusRhs = Modulus - rhs
4170 -- if lhs < rhs then lhs + modMinusRhs
4175 Mod_Minus_Right
: constant Uint
:=
4176 Modulus
(Typ
) - Intval
(Right_Opnd
(N
));
4178 Cond_Expr
: Node_Id
;
4179 Then_Expr
: Node_Id
;
4180 Else_Expr
: Node_Id
;
4185 Unchecked_Convert_To
(Standard_Unsigned
,
4186 New_Copy_Tree
(Left_Opnd
(N
))),
4188 Make_Integer_Literal
(Loc
, Intval
(Right_Opnd
(N
))));
4193 Unchecked_Convert_To
(Standard_Unsigned
,
4194 New_Copy_Tree
(Left_Opnd
(N
))),
4196 Make_Integer_Literal
(Loc
, Mod_Minus_Right
));
4199 Make_Op_Subtract
(Loc
,
4201 Unchecked_Convert_To
(Standard_Unsigned
,
4202 New_Copy_Tree
(Left_Opnd
(N
))),
4204 Unchecked_Convert_To
(Standard_Unsigned
,
4205 New_Copy_Tree
(Right_Opnd
(N
))));
4208 Unchecked_Convert_To
(Typ
,
4209 Make_If_Expression
(Loc
,
4211 New_List
(Cond_Expr
, Then_Expr
, Else_Expr
))));
4214 end Expand_Modular_Subtraction
;
4216 -- Start of processing for Expand_Nonbinary_Modular_Op
4219 -- No action needed if front-end expansion is not required or if we
4220 -- have a binary modular operand.
4222 if not Expand_Nonbinary_Modular_Ops
4223 or else not Non_Binary_Modulus
(Typ
)
4230 Expand_Modular_Addition
;
4232 when N_Op_Subtract
=>
4233 Expand_Modular_Subtraction
;
4237 -- Expand -expr into (0 - expr)
4240 Make_Op_Subtract
(Loc
,
4241 Left_Opnd
=> Make_Integer_Literal
(Loc
, 0),
4242 Right_Opnd
=> Right_Opnd
(N
)));
4243 Analyze_And_Resolve
(N
, Typ
);
4249 Analyze_And_Resolve
(N
, Typ
);
4250 end Expand_Nonbinary_Modular_Op
;
4252 ------------------------
4253 -- Expand_N_Allocator --
4254 ------------------------
4256 procedure Expand_N_Allocator
(N
: Node_Id
) is
4257 Etyp
: constant Entity_Id
:= Etype
(Expression
(N
));
4258 Loc
: constant Source_Ptr
:= Sloc
(N
);
4259 PtrT
: constant Entity_Id
:= Etype
(N
);
4261 procedure Rewrite_Coextension
(N
: Node_Id
);
4262 -- Static coextensions have the same lifetime as the entity they
4263 -- constrain. Such occurrences can be rewritten as aliased objects
4264 -- and their unrestricted access used instead of the coextension.
4266 function Size_In_Storage_Elements
(E
: Entity_Id
) return Node_Id
;
4267 -- Given a constrained array type E, returns a node representing the
4268 -- code to compute a close approximation of the size in storage elements
4269 -- for the given type; for indexes that are modular types we compute
4270 -- 'Last - First (instead of 'Length) because for large arrays computing
4271 -- 'Last -'First + 1 causes overflow. This is done without using the
4272 -- attribute 'Size_In_Storage_Elements (which malfunctions for large
4275 -------------------------
4276 -- Rewrite_Coextension --
4277 -------------------------
4279 procedure Rewrite_Coextension
(N
: Node_Id
) is
4280 Temp_Id
: constant Node_Id
:= Make_Temporary
(Loc
, 'C');
4281 Temp_Decl
: Node_Id
;
4285 -- Cnn : aliased Etyp;
4288 Make_Object_Declaration
(Loc
,
4289 Defining_Identifier
=> Temp_Id
,
4290 Aliased_Present
=> True,
4291 Object_Definition
=> New_Occurrence_Of
(Etyp
, Loc
));
4293 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
4294 Set_Expression
(Temp_Decl
, Expression
(Expression
(N
)));
4297 Insert_Action
(N
, Temp_Decl
);
4299 Make_Attribute_Reference
(Loc
,
4300 Prefix
=> New_Occurrence_Of
(Temp_Id
, Loc
),
4301 Attribute_Name
=> Name_Unrestricted_Access
));
4303 Analyze_And_Resolve
(N
, PtrT
);
4304 end Rewrite_Coextension
;
4306 ------------------------------
4307 -- Size_In_Storage_Elements --
4308 ------------------------------
4310 function Size_In_Storage_Elements
(E
: Entity_Id
) return Node_Id
is
4311 Idx
: Node_Id
:= First_Index
(E
);
4313 Res
: Node_Id
:= Empty
;
4316 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4317 -- However, the reason for the existence of this function is to
4318 -- construct a test for sizes too large, which means near the 32-bit
4319 -- limit on a 32-bit machine, and precisely the trouble is that we
4320 -- get overflows when sizes are greater than 2**31.
4322 -- So what we end up doing for array types is to use the expression:
4324 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4326 -- which avoids this problem. All this is a bit bogus, but it does
4327 -- mean we catch common cases of trying to allocate arrays that are
4328 -- too large, and which in the absence of a check results in
4329 -- undetected chaos ???
4331 for J
in 1 .. Number_Dimensions
(E
) loop
4333 if not Is_Modular_Integer_Type
(Etype
(Idx
)) then
4335 Make_Attribute_Reference
(Loc
,
4336 Prefix
=> New_Occurrence_Of
(E
, Loc
),
4337 Attribute_Name
=> Name_Length
,
4338 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, J
)));
4340 -- For indexes that are modular types we cannot generate code to
4341 -- compute 'Length since for large arrays 'Last -'First + 1 causes
4342 -- overflow; therefore we compute 'Last - 'First (which is not the
4343 -- exact number of components but it is valid for the purpose of
4344 -- this runtime check on 32-bit targets).
4348 Len_Minus_1_Expr
: Node_Id
;
4354 Make_Attribute_Reference
(Loc
,
4355 Prefix
=> New_Occurrence_Of
(E
, Loc
),
4356 Attribute_Name
=> Name_Last
,
4358 New_List
(Make_Integer_Literal
(Loc
, J
))),
4359 Make_Attribute_Reference
(Loc
,
4360 Prefix
=> New_Occurrence_Of
(E
, Loc
),
4361 Attribute_Name
=> Name_First
,
4363 New_List
(Make_Integer_Literal
(Loc
, J
))));
4366 Convert_To
(Standard_Unsigned
,
4367 Make_Op_Subtract
(Loc
,
4368 Make_Attribute_Reference
(Loc
,
4369 Prefix
=> New_Occurrence_Of
(E
, Loc
),
4370 Attribute_Name
=> Name_Last
,
4372 New_List
(Make_Integer_Literal
(Loc
, J
))),
4373 Make_Attribute_Reference
(Loc
,
4374 Prefix
=> New_Occurrence_Of
(E
, Loc
),
4375 Attribute_Name
=> Name_First
,
4377 New_List
(Make_Integer_Literal
(Loc
, J
)))));
4379 -- Handle superflat arrays, i.e. arrays with such bounds as
4380 -- 4 .. 2, to ensure that the result is correct.
4383 -- (if X'Last > X'First then X'Last - X'First else 0)
4386 Make_If_Expression
(Loc
,
4387 Expressions
=> New_List
(
4390 Make_Integer_Literal
(Loc
, Uint_0
)));
4398 pragma Assert
(Present
(Res
));
4400 Make_Op_Multiply
(Loc
,
4409 Make_Op_Multiply
(Loc
,
4412 Make_Attribute_Reference
(Loc
,
4413 Prefix
=> New_Occurrence_Of
(Component_Type
(E
), Loc
),
4414 Attribute_Name
=> Name_Max_Size_In_Storage_Elements
));
4415 end Size_In_Storage_Elements
;
4419 Dtyp
: constant Entity_Id
:= Available_View
(Designated_Type
(PtrT
));
4423 Rel_Typ
: Entity_Id
;
4426 -- Start of processing for Expand_N_Allocator
4429 -- Warn on the presence of an allocator of an anonymous access type when
4430 -- enabled, except when it's an object declaration at library level.
4432 if Warn_On_Anonymous_Allocators
4433 and then Ekind
(PtrT
) = E_Anonymous_Access_Type
4434 and then not (Is_Library_Level_Entity
(PtrT
)
4435 and then Nkind
(Associated_Node_For_Itype
(PtrT
)) =
4436 N_Object_Declaration
)
4438 Error_Msg_N
("?_a?use of an anonymous access type allocator", N
);
4441 -- RM E.2.2(17). We enforce that the expected type of an allocator
4442 -- shall not be a remote access-to-class-wide-limited-private type.
4443 -- We probably shouldn't be doing this legality check during expansion,
4444 -- but this is only an issue for Annex E users, and is unlikely to be a
4445 -- problem in practice.
4447 Validate_Remote_Access_To_Class_Wide_Type
(N
);
4449 -- Processing for anonymous access-to-controlled types. These access
4450 -- types receive a special finalization master which appears in the
4451 -- declarations of the enclosing semantic unit. This expansion is done
4452 -- now to ensure that any additional types generated by this routine or
4453 -- Expand_Allocator_Expression inherit the proper type attributes.
4455 if (Ekind
(PtrT
) = E_Anonymous_Access_Type
4456 or else (Is_Itype
(PtrT
) and then No
(Finalization_Master
(PtrT
))))
4457 and then Needs_Finalization
(Dtyp
)
4459 -- Detect the allocation of an anonymous controlled object where the
4460 -- type of the context is named. For example:
4462 -- procedure Proc (Ptr : Named_Access_Typ);
4463 -- Proc (new Designated_Typ);
4465 -- Regardless of the anonymous-to-named access type conversion, the
4466 -- lifetime of the object must be associated with the named access
4467 -- type. Use the finalization-related attributes of this type.
4469 if Nkind
(Parent
(N
)) in N_Type_Conversion
4470 | N_Unchecked_Type_Conversion
4471 and then Ekind
(Etype
(Parent
(N
))) in E_Access_Subtype
4473 | E_General_Access_Type
4475 Rel_Typ
:= Etype
(Parent
(N
));
4480 -- Anonymous access-to-controlled types allocate on the global pool.
4481 -- Note that this is a "root type only" attribute.
4483 if No
(Associated_Storage_Pool
(PtrT
)) then
4484 if Present
(Rel_Typ
) then
4485 Set_Associated_Storage_Pool
4486 (Root_Type
(PtrT
), Associated_Storage_Pool
(Rel_Typ
));
4488 Set_Associated_Storage_Pool
4489 (Root_Type
(PtrT
), RTE
(RE_Global_Pool_Object
));
4493 -- The finalization master must be inserted and analyzed as part of
4494 -- the current semantic unit. Note that the master is updated when
4495 -- analysis changes current units. Note that this is a "root type
4498 if Present
(Rel_Typ
) then
4499 Set_Finalization_Master
4500 (Root_Type
(PtrT
), Finalization_Master
(Rel_Typ
));
4502 Build_Anonymous_Master
(Root_Type
(PtrT
));
4506 -- Set the storage pool and find the appropriate version of Allocate to
4507 -- call. Do not overwrite the storage pool if it is already set, which
4508 -- can happen for build-in-place function returns (see
4509 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4511 if No
(Storage_Pool
(N
)) then
4512 Pool
:= Associated_Storage_Pool
(Root_Type
(PtrT
));
4514 if Present
(Pool
) then
4515 Set_Storage_Pool
(N
, Pool
);
4517 if Is_RTE
(Pool
, RE_RS_Pool
) then
4518 Set_Procedure_To_Call
(N
, RTE
(RE_RS_Allocate
));
4520 elsif Is_RTE
(Pool
, RE_SS_Pool
) then
4521 Check_Restriction
(No_Secondary_Stack
, N
);
4522 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
4524 -- In the case of an allocator for a simple storage pool, locate
4525 -- and save a reference to the pool type's Allocate routine.
4527 elsif Present
(Get_Rep_Pragma
4528 (Etype
(Pool
), Name_Simple_Storage_Pool_Type
))
4531 Pool_Type
: constant Entity_Id
:= Base_Type
(Etype
(Pool
));
4532 Alloc_Op
: Entity_Id
;
4534 Alloc_Op
:= Get_Name_Entity_Id
(Name_Allocate
);
4535 while Present
(Alloc_Op
) loop
4536 if Scope
(Alloc_Op
) = Scope
(Pool_Type
)
4537 and then Present
(First_Formal
(Alloc_Op
))
4538 and then Etype
(First_Formal
(Alloc_Op
)) = Pool_Type
4540 Set_Procedure_To_Call
(N
, Alloc_Op
);
4543 Alloc_Op
:= Homonym
(Alloc_Op
);
4548 elsif Is_Class_Wide_Type
(Etype
(Pool
)) then
4549 Set_Procedure_To_Call
(N
, RTE
(RE_Allocate_Any
));
4552 Set_Procedure_To_Call
(N
,
4553 Find_Storage_Op
(Etype
(Pool
), Name_Allocate
));
4558 -- Under certain circumstances we can replace an allocator by an access
4559 -- to statically allocated storage. The conditions, as noted in AARM
4560 -- 3.10 (10c) are as follows:
4562 -- Size and initial value is known at compile time
4563 -- Access type is access-to-constant
4565 -- The allocator is not part of a constraint on a record component,
4566 -- because in that case the inserted actions are delayed until the
4567 -- record declaration is fully analyzed, which is too late for the
4568 -- analysis of the rewritten allocator.
4570 if Is_Access_Constant
(PtrT
)
4571 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
4572 and then Compile_Time_Known_Value
(Expression
(Expression
(N
)))
4573 and then Size_Known_At_Compile_Time
4574 (Etype
(Expression
(Expression
(N
))))
4575 and then not Is_Record_Type
(Current_Scope
)
4577 -- Here we can do the optimization. For the allocator
4581 -- We insert an object declaration
4583 -- Tnn : aliased x := y;
4585 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4586 -- marked as requiring static allocation.
4588 Temp
:= Make_Temporary
(Loc
, 'T', Expression
(Expression
(N
)));
4589 Desig
:= Subtype_Mark
(Expression
(N
));
4591 -- If context is constrained, use constrained subtype directly,
4592 -- so that the constant is not labelled as having a nominally
4593 -- unconstrained subtype.
4595 if Entity
(Desig
) = Base_Type
(Dtyp
) then
4596 Desig
:= New_Occurrence_Of
(Dtyp
, Loc
);
4600 Make_Object_Declaration
(Loc
,
4601 Defining_Identifier
=> Temp
,
4602 Aliased_Present
=> True,
4603 Constant_Present
=> Is_Access_Constant
(PtrT
),
4604 Object_Definition
=> Desig
,
4605 Expression
=> Expression
(Expression
(N
))));
4608 Make_Attribute_Reference
(Loc
,
4609 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
4610 Attribute_Name
=> Name_Unrestricted_Access
));
4612 Analyze_And_Resolve
(N
, PtrT
);
4614 -- We set the variable as statically allocated, since we don't want
4615 -- it going on the stack of the current procedure.
4617 Set_Is_Statically_Allocated
(Temp
);
4621 -- Same if the allocator is an access discriminant for a local object:
4622 -- instead of an allocator we create a local value and constrain the
4623 -- enclosing object with the corresponding access attribute.
4625 if Is_Static_Coextension
(N
) then
4626 Rewrite_Coextension
(N
);
4630 -- Check for size too large, we do this because the back end misses
4631 -- proper checks here and can generate rubbish allocation calls when
4632 -- we are near the limit. We only do this for the 32-bit address case
4633 -- since that is from a practical point of view where we see a problem.
4635 if System_Address_Size
= 32
4636 and then not Storage_Checks_Suppressed
(PtrT
)
4637 and then not Storage_Checks_Suppressed
(Dtyp
)
4638 and then not Storage_Checks_Suppressed
(Etyp
)
4640 -- The check we want to generate should look like
4642 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4643 -- raise Storage_Error;
4646 -- where 3.5 gigabytes is a constant large enough to accommodate any
4647 -- reasonable request for. But we can't do it this way because at
4648 -- least at the moment we don't compute this attribute right, and
4649 -- can silently give wrong results when the result gets large. Since
4650 -- this is all about large results, that's bad, so instead we only
4651 -- apply the check for constrained arrays, and manually compute the
4652 -- value of the attribute ???
4654 -- The check on No_Initialization is used here to prevent generating
4655 -- this runtime check twice when the allocator is locally replaced by
4656 -- the expander with another one.
4658 if Is_Array_Type
(Etyp
) and then not No_Initialization
(N
) then
4661 Ins_Nod
: Node_Id
:= N
;
4662 Siz_Typ
: Entity_Id
:= Etyp
;
4666 -- For unconstrained array types initialized with a qualified
4667 -- expression we use its type to perform this check
4669 if not Is_Constrained
(Etyp
)
4670 and then not No_Initialization
(N
)
4671 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
4673 Expr
:= Expression
(Expression
(N
));
4674 Siz_Typ
:= Etype
(Expression
(Expression
(N
)));
4676 -- If the qualified expression has been moved to an internal
4677 -- temporary (to remove side effects) then we must insert
4678 -- the runtime check before its declaration to ensure that
4679 -- the check is performed before the execution of the code
4680 -- computing the qualified expression.
4682 if Nkind
(Expr
) = N_Identifier
4683 and then Is_Internal_Name
(Chars
(Expr
))
4685 Nkind
(Parent
(Entity
(Expr
))) = N_Object_Declaration
4687 Ins_Nod
:= Parent
(Entity
(Expr
));
4693 if Is_Constrained
(Siz_Typ
)
4694 and then Ekind
(Siz_Typ
) /= E_String_Literal_Subtype
4696 -- For CCG targets, the largest array may have up to 2**31-1
4697 -- components (i.e. 2 gigabytes if each array component is
4698 -- one byte). This ensures that fat pointer fields do not
4699 -- overflow, since they are 32-bit integer types, and also
4700 -- ensures that 'Length can be computed at run time.
4702 if Modify_Tree_For_C
then
4705 Left_Opnd
=> Size_In_Storage_Elements
(Siz_Typ
),
4706 Right_Opnd
=> Make_Integer_Literal
(Loc
,
4707 Uint_2
** 31 - Uint_1
));
4709 -- For native targets the largest object is 3.5 gigabytes
4714 Left_Opnd
=> Size_In_Storage_Elements
(Siz_Typ
),
4715 Right_Opnd
=> Make_Integer_Literal
(Loc
,
4716 Uint_7
* (Uint_2
** 29)));
4719 Insert_Action
(Ins_Nod
,
4720 Make_Raise_Storage_Error
(Loc
,
4722 Reason
=> SE_Object_Too_Large
));
4724 if Entity
(Cond
) = Standard_True
then
4726 ("object too large: Storage_Error will be raised at "
4734 -- If no storage pool has been specified, or the storage pool
4735 -- is System.Pool_Global.Global_Pool_Object, and the restriction
4736 -- No_Standard_Allocators_After_Elaboration is present, then generate
4737 -- a call to Elaboration_Allocators.Check_Standard_Allocator.
4739 if Nkind
(N
) = N_Allocator
4740 and then (No
(Storage_Pool
(N
))
4741 or else Is_RTE
(Storage_Pool
(N
), RE_Global_Pool_Object
))
4742 and then Restriction_Active
(No_Standard_Allocators_After_Elaboration
)
4745 Make_Procedure_Call_Statement
(Loc
,
4747 New_Occurrence_Of
(RTE
(RE_Check_Standard_Allocator
), Loc
)));
4750 -- Handle case of qualified expression (other than optimization above)
4752 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
4753 Expand_Allocator_Expression
(N
);
4757 -- If the allocator is for a type which requires initialization, and
4758 -- there is no initial value (i.e. operand is a subtype indication
4759 -- rather than a qualified expression), then we must generate a call to
4760 -- the initialization routine using an expressions action node:
4762 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4764 -- Here ptr_T is the pointer type for the allocator, and T is the
4765 -- subtype of the allocator. A special case arises if the designated
4766 -- type of the access type is a task or contains tasks. In this case
4767 -- the call to Init (Temp.all ...) is replaced by code that ensures
4768 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4769 -- for details). In addition, if the type T is a task type, then the
4770 -- first argument to Init must be converted to the task record type.
4773 T
: constant Entity_Id
:= Etype
(Expression
(N
));
4779 Init_Arg1
: Node_Id
;
4780 Init_Call
: Node_Id
;
4781 Temp_Decl
: Node_Id
;
4782 Temp_Type
: Entity_Id
;
4785 -- Apply constraint checks against designated subtype (RM 4.8(10/2))
4786 -- but ignore the expression if the No_Initialization flag is set.
4787 -- Discriminant checks will be generated by the expansion below.
4789 if Is_Array_Type
(Dtyp
) and then not No_Initialization
(N
) then
4790 Apply_Constraint_Check
(Expression
(N
), Dtyp
, No_Sliding
=> True);
4792 Apply_Predicate_Check
(Expression
(N
), Dtyp
);
4794 if Nkind
(Expression
(N
)) = N_Raise_Constraint_Error
then
4795 Rewrite
(N
, New_Copy
(Expression
(N
)));
4796 Set_Etype
(N
, PtrT
);
4801 if No_Initialization
(N
) then
4803 -- Even though this might be a simple allocation, create a custom
4804 -- Allocate if the context requires it.
4806 if Present
(Finalization_Master
(PtrT
)) then
4807 Build_Allocate_Deallocate_Proc
4809 Is_Allocate
=> True);
4812 -- Optimize the default allocation of an array object when pragma
4813 -- Initialize_Scalars or Normalize_Scalars is in effect. Construct an
4814 -- in-place initialization aggregate which may be convert into a fast
4815 -- memset by the backend.
4817 elsif Init_Or_Norm_Scalars
4818 and then Is_Array_Type
(T
)
4820 -- The array must lack atomic components because they are treated
4821 -- as non-static, and as a result the backend will not initialize
4822 -- the memory in one go.
4824 and then not Has_Atomic_Components
(T
)
4826 -- The array must not be packed because the invalid values in
4827 -- System.Scalar_Values are multiples of Storage_Unit.
4829 and then not Is_Packed
(T
)
4831 -- The array must have static non-empty ranges, otherwise the
4832 -- backend cannot initialize the memory in one go.
4834 and then Has_Static_Non_Empty_Array_Bounds
(T
)
4836 -- The optimization is only relevant for arrays of scalar types
4838 and then Is_Scalar_Type
(Component_Type
(T
))
4840 -- Similar to regular array initialization using a type init proc,
4841 -- predicate checks are not performed because the initialization
4842 -- values are intentionally invalid, and may violate the predicate.
4844 and then not Has_Predicates
(Component_Type
(T
))
4846 -- The component type must have a single initialization value
4848 and then Needs_Simple_Initialization
4849 (Typ
=> Component_Type
(T
),
4850 Consider_IS
=> True)
4853 Temp
:= Make_Temporary
(Loc
, 'P');
4856 -- Temp : Ptr_Typ := new ...;
4861 Make_Object_Declaration
(Loc
,
4862 Defining_Identifier
=> Temp
,
4863 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
4864 Expression
=> Relocate_Node
(N
)),
4865 Suppress
=> All_Checks
);
4868 -- Temp.all := (others => ...);
4873 Make_Assignment_Statement
(Loc
,
4875 Make_Explicit_Dereference
(Loc
,
4876 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)),
4881 Size
=> Esize
(Component_Type
(T
)))),
4882 Suppress
=> All_Checks
);
4884 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
4885 Analyze_And_Resolve
(N
, PtrT
);
4887 -- Case of no initialization procedure present
4889 elsif not Has_Non_Null_Base_Init_Proc
(T
) then
4891 -- Case of simple initialization required
4893 if Needs_Simple_Initialization
(T
) then
4894 Check_Restriction
(No_Default_Initialization
, N
);
4895 Rewrite
(Expression
(N
),
4896 Make_Qualified_Expression
(Loc
,
4897 Subtype_Mark
=> New_Occurrence_Of
(T
, Loc
),
4898 Expression
=> Get_Simple_Init_Val
(T
, N
)));
4900 Analyze_And_Resolve
(Expression
(Expression
(N
)), T
);
4901 Analyze_And_Resolve
(Expression
(N
), T
);
4902 Set_Paren_Count
(Expression
(Expression
(N
)), 1);
4903 Expand_N_Allocator
(N
);
4905 -- No initialization required
4908 Build_Allocate_Deallocate_Proc
4910 Is_Allocate
=> True);
4913 -- Case of initialization procedure present, must be called
4915 -- NOTE: There is a *huge* amount of code duplication here from
4916 -- Build_Initialization_Call. We should probably refactor???
4919 Check_Restriction
(No_Default_Initialization
, N
);
4921 if not Restriction_Active
(No_Default_Initialization
) then
4922 Init
:= Base_Init_Proc
(T
);
4924 Temp
:= Make_Temporary
(Loc
, 'P');
4926 -- Construct argument list for the initialization routine call
4929 Make_Explicit_Dereference
(Loc
,
4931 New_Occurrence_Of
(Temp
, Loc
));
4933 Set_Assignment_OK
(Init_Arg1
);
4936 -- The initialization procedure expects a specific type. if the
4937 -- context is access to class wide, indicate that the object
4938 -- being allocated has the right specific type.
4940 if Is_Class_Wide_Type
(Dtyp
) then
4941 Init_Arg1
:= Unchecked_Convert_To
(T
, Init_Arg1
);
4944 -- If designated type is a concurrent type or if it is private
4945 -- type whose definition is a concurrent type, the first
4946 -- argument in the Init routine has to be unchecked conversion
4947 -- to the corresponding record type. If the designated type is
4948 -- a derived type, also convert the argument to its root type.
4950 if Is_Concurrent_Type
(T
) then
4952 Unchecked_Convert_To
(
4953 Corresponding_Record_Type
(T
), Init_Arg1
);
4955 elsif Is_Private_Type
(T
)
4956 and then Present
(Full_View
(T
))
4957 and then Is_Concurrent_Type
(Full_View
(T
))
4960 Unchecked_Convert_To
4961 (Corresponding_Record_Type
(Full_View
(T
)), Init_Arg1
);
4963 elsif Etype
(First_Formal
(Init
)) /= Base_Type
(T
) then
4965 Ftyp
: constant Entity_Id
:= Etype
(First_Formal
(Init
));
4968 Init_Arg1
:= OK_Convert_To
(Etype
(Ftyp
), Init_Arg1
);
4969 Set_Etype
(Init_Arg1
, Ftyp
);
4973 Args
:= New_List
(Init_Arg1
);
4975 -- For the task case, pass the Master_Id of the access type as
4976 -- the value of the _Master parameter, and _Chain as the value
4977 -- of the _Chain parameter (_Chain will be defined as part of
4978 -- the generated code for the allocator).
4980 -- In Ada 2005, the context may be a function that returns an
4981 -- anonymous access type. In that case the Master_Id has been
4982 -- created when expanding the function declaration.
4984 if Has_Task
(T
) then
4985 if No
(Master_Id
(Base_Type
(PtrT
))) then
4987 -- The designated type was an incomplete type, and the
4988 -- access type did not get expanded. Salvage it now.
4990 if Present
(Parent
(Base_Type
(PtrT
))) then
4991 Expand_N_Full_Type_Declaration
4992 (Parent
(Base_Type
(PtrT
)));
4994 -- The only other possibility is an itype. For this
4995 -- case, the master must exist in the context. This is
4996 -- the case when the allocator initializes an access
4997 -- component in an init-proc.
5000 pragma Assert
(Is_Itype
(PtrT
));
5001 Build_Master_Renaming
(PtrT
, N
);
5005 -- If the context of the allocator is a declaration or an
5006 -- assignment, we can generate a meaningful image for it,
5007 -- even though subsequent assignments might remove the
5008 -- connection between task and entity. We build this image
5009 -- when the left-hand side is a simple variable, a simple
5010 -- indexed assignment or a simple selected component.
5012 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
5014 Nam
: constant Node_Id
:= Name
(Parent
(N
));
5017 if Is_Entity_Name
(Nam
) then
5019 Build_Task_Image_Decls
5022 (Entity
(Nam
), Sloc
(Nam
)), T
);
5024 elsif Nkind
(Nam
) in N_Indexed_Component
5025 | N_Selected_Component
5026 and then Is_Entity_Name
(Prefix
(Nam
))
5029 Build_Task_Image_Decls
5030 (Loc
, Nam
, Etype
(Prefix
(Nam
)));
5032 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
5036 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
5038 Build_Task_Image_Decls
5039 (Loc
, Defining_Identifier
(Parent
(N
)), T
);
5042 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
5045 if Restriction_Active
(No_Task_Hierarchy
) then
5047 (Args
, Make_Integer_Literal
(Loc
, Library_Task_Level
));
5051 (Master_Id
(Base_Type
(Root_Type
(PtrT
))), Loc
));
5054 Append_To
(Args
, Make_Identifier
(Loc
, Name_uChain
));
5056 Decl
:= Last
(Decls
);
5058 New_Occurrence_Of
(Defining_Identifier
(Decl
), Loc
));
5060 -- Has_Task is false, Decls not used
5066 -- Add discriminants if discriminated type
5069 Dis
: Boolean := False;
5070 Typ
: Entity_Id
:= T
;
5073 if Has_Discriminants
(T
) then
5076 -- Type may be a private type with no visible discriminants
5077 -- in which case check full view if in scope, or the
5078 -- underlying_full_view if dealing with a type whose full
5079 -- view may be derived from a private type whose own full
5080 -- view has discriminants.
5082 elsif Is_Private_Type
(T
) then
5083 if Present
(Full_View
(T
))
5084 and then Has_Discriminants
(Full_View
(T
))
5087 Typ
:= Full_View
(T
);
5089 elsif Present
(Underlying_Full_View
(T
))
5090 and then Has_Discriminants
(Underlying_Full_View
(T
))
5093 Typ
:= Underlying_Full_View
(T
);
5099 -- If the allocated object will be constrained by the
5100 -- default values for discriminants, then build a subtype
5101 -- with those defaults, and change the allocated subtype
5102 -- to that. Note that this happens in fewer cases in Ada
5105 if not Is_Constrained
(Typ
)
5106 and then Present
(Discriminant_Default_Value
5107 (First_Discriminant
(Typ
)))
5108 and then (Ada_Version
< Ada_2005
5110 Object_Type_Has_Constrained_Partial_View
5111 (Typ
, Current_Scope
))
5113 Typ
:= Build_Default_Subtype
(Typ
, N
);
5114 Set_Expression
(N
, New_Occurrence_Of
(Typ
, Loc
));
5117 Discr
:= First_Elmt
(Discriminant_Constraint
(Typ
));
5118 while Present
(Discr
) loop
5119 Nod
:= Node
(Discr
);
5120 Append
(New_Copy_Tree
(Node
(Discr
)), Args
);
5122 -- AI-416: when the discriminant constraint is an
5123 -- anonymous access type make sure an accessibility
5124 -- check is inserted if necessary (3.10.2(22.q/2))
5126 if Ada_Version
>= Ada_2005
5128 Ekind
(Etype
(Nod
)) = E_Anonymous_Access_Type
5130 No_Dynamic_Accessibility_Checks_Enabled
(Nod
)
5132 Apply_Accessibility_Check
5133 (Nod
, Typ
, Insert_Node
=> Nod
);
5140 -- When the designated subtype is unconstrained and
5141 -- the allocator specifies a constrained subtype (or
5142 -- such a subtype has been created, such as above by
5143 -- Build_Default_Subtype), associate that subtype with
5144 -- the dereference of the allocator's access value.
5145 -- This is needed by the expander for cases where the
5146 -- access type has a Designated_Storage_Model in order
5147 -- to support allocation of a host object of the right
5148 -- size for passing to the initialization procedure.
5150 if not Is_Constrained
(Dtyp
)
5151 and then Is_Constrained
(Typ
)
5154 Deref
: constant Node_Id
:= Unqual_Conv
(Init_Arg1
);
5157 pragma Assert
(Nkind
(Deref
) = N_Explicit_Dereference
);
5159 Set_Actual_Designated_Subtype
(Deref
, Typ
);
5164 -- We set the allocator as analyzed so that when we analyze
5165 -- the if expression node, we do not get an unwanted recursive
5166 -- expansion of the allocator expression.
5168 Set_Analyzed
(N
, True);
5169 Nod
:= Relocate_Node
(N
);
5171 -- Here is the transformation:
5172 -- input: new Ctrl_Typ
5173 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
5174 -- Ctrl_TypIP (Temp.all, ...);
5175 -- [Deep_]Initialize (Temp.all);
5177 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
5178 -- is the subtype of the allocator.
5181 Make_Object_Declaration
(Loc
,
5182 Defining_Identifier
=> Temp
,
5183 Constant_Present
=> True,
5184 Object_Definition
=> New_Occurrence_Of
(Temp_Type
, Loc
),
5187 Set_Assignment_OK
(Temp_Decl
);
5188 Insert_Action
(N
, Temp_Decl
, Suppress
=> All_Checks
);
5190 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
5192 -- If the designated type is a task type or contains tasks,
5193 -- create block to activate created tasks, and insert
5194 -- declaration for Task_Image variable ahead of call.
5196 if Has_Task
(T
) then
5198 L
: constant List_Id
:= New_List
;
5201 Build_Task_Allocate_Block
(L
, Nod
, Args
);
5203 Insert_List_Before
(First
(Declarations
(Blk
)), Decls
);
5204 Insert_Actions
(N
, L
);
5209 Make_Procedure_Call_Statement
(Loc
,
5210 Name
=> New_Occurrence_Of
(Init
, Loc
),
5211 Parameter_Associations
=> Args
));
5214 if Needs_Finalization
(T
) then
5217 -- [Deep_]Initialize (Init_Arg1);
5221 (Obj_Ref
=> New_Copy_Tree
(Init_Arg1
),
5224 -- Guard against a missing [Deep_]Initialize when the
5225 -- designated type was not properly frozen.
5227 if Present
(Init_Call
) then
5228 Insert_Action
(N
, Init_Call
);
5232 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
5233 Analyze_And_Resolve
(N
, PtrT
);
5235 -- When designated type has Default_Initial_Condition aspects,
5236 -- make a call to the type's DIC procedure to perform the
5237 -- checks. Theoretically this might also be needed for cases
5238 -- where the type doesn't have an init proc, but those should
5239 -- be very uncommon, and for now we only support the init proc
5243 and then Present
(DIC_Procedure
(Dtyp
))
5244 and then not Has_Null_Body
(DIC_Procedure
(Dtyp
))
5247 Build_DIC_Call
(Loc
,
5248 Make_Explicit_Dereference
(Loc
,
5249 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)),
5256 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
5257 -- object that has been rewritten as a reference, we displace "this"
5258 -- to reference properly its secondary dispatch table.
5260 if Nkind
(N
) = N_Identifier
and then Is_Interface
(Dtyp
) then
5261 Displace_Allocator_Pointer
(N
);
5265 when RE_Not_Available
=>
5267 end Expand_N_Allocator
;
5269 -----------------------
5270 -- Expand_N_And_Then --
5271 -----------------------
5273 procedure Expand_N_And_Then
(N
: Node_Id
)
5274 renames Expand_Short_Circuit_Operator
;
5276 ------------------------------
5277 -- Expand_N_Case_Expression --
5278 ------------------------------
5280 procedure Expand_N_Case_Expression
(N
: Node_Id
) is
5281 function Is_Copy_Type
(Typ
: Entity_Id
) return Boolean;
5282 -- Return True if we can copy objects of this type when expanding a case
5289 function Is_Copy_Type
(Typ
: Entity_Id
) return Boolean is
5291 -- If Minimize_Expression_With_Actions is True, we can afford to copy
5292 -- large objects, as long as they are constrained and not limited.
5295 Is_Elementary_Type
(Underlying_Type
(Typ
))
5297 (Minimize_Expression_With_Actions
5298 and then Is_Constrained
(Underlying_Type
(Typ
))
5299 and then not Is_Limited_Type
(Underlying_Type
(Typ
)));
5304 Loc
: constant Source_Ptr
:= Sloc
(N
);
5305 Par
: constant Node_Id
:= Parent
(N
);
5306 Typ
: constant Entity_Id
:= Etype
(N
);
5310 Case_Stmt
: Node_Id
;
5313 Target
: Entity_Id
:= Empty
;
5314 Target_Typ
: Entity_Id
;
5316 In_Predicate
: Boolean := False;
5317 -- Flag set when the case expression appears within a predicate
5319 Optimize_Return_Stmt
: Boolean := False;
5320 -- Flag set when the case expression can be optimized in the context of
5321 -- a simple return statement.
5323 -- Start of processing for Expand_N_Case_Expression
5326 -- Check for MINIMIZED/ELIMINATED overflow mode
5328 if Minimized_Eliminated_Overflow_Check
(N
) then
5329 Apply_Arithmetic_Overflow_Check
(N
);
5333 -- If the case expression is a predicate specification, and the type
5334 -- to which it applies has a static predicate aspect, do not expand,
5335 -- because it will be converted to the proper predicate form later.
5337 if Ekind
(Current_Scope
) in E_Function | E_Procedure
5338 and then Is_Predicate_Function
(Current_Scope
)
5340 In_Predicate
:= True;
5342 if Has_Static_Predicate_Aspect
(Etype
(First_Entity
(Current_Scope
)))
5348 -- When the type of the case expression is elementary, expand
5350 -- (case X is when A => AX, when B => BX ...)
5365 -- In all other cases expand into
5368 -- type Ptr_Typ is access all Typ;
5369 -- Target : Ptr_Typ;
5372 -- Target := AX'Unrestricted_Access;
5374 -- Target := BX'Unrestricted_Access;
5377 -- in Target.all end;
5379 -- This approach avoids extra copies of potentially large objects. It
5380 -- also allows handling of values of limited or unconstrained types.
5381 -- Note that we do the copy also for constrained, nonlimited types
5382 -- when minimizing expressions with actions (e.g. when generating C
5383 -- code) since it allows us to do the optimization below in more cases.
5385 -- Small optimization: when the case expression appears in the context
5386 -- of a simple return statement, expand into
5397 Make_Case_Statement
(Loc
,
5398 Expression
=> Expression
(N
),
5399 Alternatives
=> New_List
);
5401 -- Preserve the original context for which the case statement is being
5402 -- generated. This is needed by the finalization machinery to prevent
5403 -- the premature finalization of controlled objects found within the
5406 Set_From_Conditional_Expression
(Case_Stmt
);
5411 if Is_Copy_Type
(Typ
) then
5414 -- Do not perform the optimization when the return statement is
5415 -- within a predicate function, as this causes spurious errors.
5417 Optimize_Return_Stmt
:=
5418 Nkind
(Par
) = N_Simple_Return_Statement
and then not In_Predicate
;
5420 -- Otherwise create an access type to handle the general case using
5421 -- 'Unrestricted_Access.
5424 -- type Ptr_Typ is access all Typ;
5427 if Generate_C_Code
then
5429 -- We cannot ensure that correct C code will be generated if any
5430 -- temporary is created down the line (to e.g. handle checks or
5431 -- capture values) since we might end up with dangling references
5432 -- to local variables, so better be safe and reject the construct.
5435 ("case expression too complex, use case statement instead", N
);
5438 Target_Typ
:= Make_Temporary
(Loc
, 'P');
5441 Make_Full_Type_Declaration
(Loc
,
5442 Defining_Identifier
=> Target_Typ
,
5444 Make_Access_To_Object_Definition
(Loc
,
5445 All_Present
=> True,
5446 Subtype_Indication
=> New_Occurrence_Of
(Typ
, Loc
))));
5449 -- Create the declaration of the target which captures the value of the
5453 -- Target : [Ptr_]Typ;
5455 if not Optimize_Return_Stmt
then
5456 Target
:= Make_Temporary
(Loc
, 'T');
5459 Make_Object_Declaration
(Loc
,
5460 Defining_Identifier
=> Target
,
5461 Object_Definition
=> New_Occurrence_Of
(Target_Typ
, Loc
));
5462 Set_No_Initialization
(Decl
);
5464 Append_To
(Acts
, Decl
);
5467 -- Process the alternatives
5469 Alt
:= First
(Alternatives
(N
));
5470 while Present
(Alt
) loop
5472 Alt_Expr
: Node_Id
:= Expression
(Alt
);
5473 Alt_Loc
: constant Source_Ptr
:= Sloc
(Alt_Expr
);
5478 -- Take the unrestricted access of the expression value for non-
5479 -- scalar types. This approach avoids big copies and covers the
5480 -- limited and unconstrained cases.
5483 -- AX'Unrestricted_Access
5485 if not Is_Copy_Type
(Typ
) then
5487 Make_Attribute_Reference
(Alt_Loc
,
5488 Prefix
=> Relocate_Node
(Alt_Expr
),
5489 Attribute_Name
=> Name_Unrestricted_Access
);
5493 -- return AX['Unrestricted_Access];
5495 if Optimize_Return_Stmt
then
5497 Make_Simple_Return_Statement
(Alt_Loc
,
5498 Expression
=> Alt_Expr
));
5501 -- Target := AX['Unrestricted_Access];
5504 LHS
:= New_Occurrence_Of
(Target
, Loc
);
5505 Set_Assignment_OK
(LHS
);
5508 Make_Assignment_Statement
(Alt_Loc
,
5510 Expression
=> Alt_Expr
));
5513 -- Propagate declarations inserted in the node by Insert_Actions
5514 -- (for example, temporaries generated to remove side effects).
5515 -- These actions must remain attached to the alternative, given
5516 -- that they are generated by the corresponding expression.
5518 if Present
(Actions
(Alt
)) then
5519 Prepend_List
(Actions
(Alt
), Stmts
);
5522 -- Finalize any transient objects on exit from the alternative.
5523 -- This is done only in the return optimization case because
5524 -- otherwise the case expression is converted into an expression
5525 -- with actions which already contains this form of processing.
5527 if Optimize_Return_Stmt
then
5528 Process_If_Case_Statements
(N
, Stmts
);
5532 (Alternatives
(Case_Stmt
),
5533 Make_Case_Statement_Alternative
(Sloc
(Alt
),
5534 Discrete_Choices
=> Discrete_Choices
(Alt
),
5535 Statements
=> Stmts
));
5541 -- Rewrite the parent return statement as a case statement
5543 if Optimize_Return_Stmt
then
5544 Rewrite
(Par
, Case_Stmt
);
5547 -- Otherwise convert the case expression into an expression with actions
5550 Append_To
(Acts
, Case_Stmt
);
5552 if Is_Copy_Type
(Typ
) then
5553 Expr
:= New_Occurrence_Of
(Target
, Loc
);
5557 Make_Explicit_Dereference
(Loc
,
5558 Prefix
=> New_Occurrence_Of
(Target
, Loc
));
5564 -- in Target[.all] end;
5567 Make_Expression_With_Actions
(Loc
,
5571 Analyze_And_Resolve
(N
, Typ
);
5573 end Expand_N_Case_Expression
;
5575 -----------------------------------
5576 -- Expand_N_Explicit_Dereference --
5577 -----------------------------------
5579 procedure Expand_N_Explicit_Dereference
(N
: Node_Id
) is
5581 -- Insert explicit dereference call for the checked storage pool case
5583 Insert_Dereference_Action
(Prefix
(N
));
5585 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5586 -- we set the atomic sync flag.
5588 if Is_Atomic
(Etype
(N
))
5589 and then not Atomic_Synchronization_Disabled
(Etype
(N
))
5591 Activate_Atomic_Synchronization
(N
);
5593 end Expand_N_Explicit_Dereference
;
5595 --------------------------------------
5596 -- Expand_N_Expression_With_Actions --
5597 --------------------------------------
5599 procedure Expand_N_Expression_With_Actions
(N
: Node_Id
) is
5600 Acts
: constant List_Id
:= Actions
(N
);
5602 procedure Force_Boolean_Evaluation
(Expr
: Node_Id
);
5603 -- Force the evaluation of Boolean expression Expr
5605 function Process_Action
(Act
: Node_Id
) return Traverse_Result
;
5606 -- Inspect and process a single action of an expression_with_actions for
5607 -- transient objects. If such objects are found, the routine generates
5608 -- code to clean them up when the context of the expression is evaluated
5611 ------------------------------
5612 -- Force_Boolean_Evaluation --
5613 ------------------------------
5615 procedure Force_Boolean_Evaluation
(Expr
: Node_Id
) is
5616 Loc
: constant Source_Ptr
:= Sloc
(N
);
5617 Flag_Decl
: Node_Id
;
5618 Flag_Id
: Entity_Id
;
5621 -- Relocate the expression to the actions list by capturing its value
5622 -- in a Boolean flag. Generate:
5623 -- Flag : constant Boolean := Expr;
5625 Flag_Id
:= Make_Temporary
(Loc
, 'F');
5628 Make_Object_Declaration
(Loc
,
5629 Defining_Identifier
=> Flag_Id
,
5630 Constant_Present
=> True,
5631 Object_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
),
5632 Expression
=> Relocate_Node
(Expr
));
5634 Append
(Flag_Decl
, Acts
);
5635 Analyze
(Flag_Decl
);
5637 -- Replace the expression with a reference to the flag
5639 Rewrite
(Expression
(N
), New_Occurrence_Of
(Flag_Id
, Loc
));
5640 Analyze
(Expression
(N
));
5641 end Force_Boolean_Evaluation
;
5643 --------------------
5644 -- Process_Action --
5645 --------------------
5647 function Process_Action
(Act
: Node_Id
) return Traverse_Result
is
5649 if Nkind
(Act
) = N_Object_Declaration
5650 and then Is_Finalizable_Transient
(Act
, N
)
5652 Process_Transient_In_Expression
(Act
, N
, Acts
);
5655 -- Avoid processing temporary function results multiple times when
5656 -- dealing with nested expression_with_actions or nested blocks.
5657 -- Similarly, do not process temporary function results in loops.
5658 -- This is done by Expand_N_Loop_Statement and Build_Finalizer.
5659 -- Note that we used to wrongly return Abandon instead of Skip here:
5660 -- this is wrong since it means that we were ignoring lots of
5661 -- relevant subsequent statements.
5663 elsif Nkind
(Act
) in N_Expression_With_Actions
5673 procedure Process_Single_Action
is new Traverse_Proc
(Process_Action
);
5679 -- Start of processing for Expand_N_Expression_With_Actions
5682 -- Do not evaluate the expression when it denotes an entity because the
5683 -- expression_with_actions node will be replaced by the reference.
5685 if Is_Entity_Name
(Expression
(N
)) then
5688 -- Do not evaluate the expression when there are no actions because the
5689 -- expression_with_actions node will be replaced by the expression.
5691 elsif Is_Empty_List
(Acts
) then
5694 -- Force the evaluation of the expression by capturing its value in a
5695 -- temporary. This ensures that aliases of transient objects do not leak
5696 -- to the expression of the expression_with_actions node:
5699 -- Trans_Id : Ctrl_Typ := ...;
5700 -- Alias : ... := Trans_Id;
5701 -- in ... Alias ... end;
5703 -- In the example above, Trans_Id cannot be finalized at the end of the
5704 -- actions list because this may affect the alias and the final value of
5705 -- the expression_with_actions. Forcing the evaluation encapsulates the
5706 -- reference to the Alias within the actions list:
5709 -- Trans_Id : Ctrl_Typ := ...;
5710 -- Alias : ... := Trans_Id;
5711 -- Val : constant Boolean := ... Alias ...;
5712 -- <finalize Trans_Id>
5715 -- Once this transformation is performed, it is safe to finalize the
5716 -- transient object at the end of the actions list.
5718 -- Note that Force_Evaluation does not remove side effects in operators
5719 -- because it assumes that all operands are evaluated and side effect
5720 -- free. This is not the case when an operand depends implicitly on the
5721 -- transient object through the use of access types.
5723 elsif Is_Boolean_Type
(Etype
(Expression
(N
))) then
5724 Force_Boolean_Evaluation
(Expression
(N
));
5726 -- The expression of an expression_with_actions node may not necessarily
5727 -- be Boolean when the node appears in an if expression. In this case do
5728 -- the usual forced evaluation to encapsulate potential aliasing.
5731 Force_Evaluation
(Expression
(N
));
5734 -- Process all transient objects found within the actions of the EWA
5737 Act
:= First
(Acts
);
5738 while Present
(Act
) loop
5739 Process_Single_Action
(Act
);
5743 -- Deal with case where there are no actions. In this case we simply
5744 -- rewrite the node with its expression since we don't need the actions
5745 -- and the specification of this node does not allow a null action list.
5747 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5748 -- the expanded tree and relying on being able to retrieve the original
5749 -- tree in cases like this. This raises a whole lot of issues of whether
5750 -- we have problems elsewhere, which will be addressed in the future???
5752 if Is_Empty_List
(Acts
) then
5753 Rewrite
(N
, Relocate_Node
(Expression
(N
)));
5755 end Expand_N_Expression_With_Actions
;
5757 ----------------------------
5758 -- Expand_N_If_Expression --
5759 ----------------------------
5761 -- Deal with limited types and condition actions
5763 procedure Expand_N_If_Expression
(N
: Node_Id
) is
5764 Cond
: constant Node_Id
:= First
(Expressions
(N
));
5765 Loc
: constant Source_Ptr
:= Sloc
(N
);
5766 Thenx
: constant Node_Id
:= Next
(Cond
);
5767 Elsex
: constant Node_Id
:= Next
(Thenx
);
5768 Typ
: constant Entity_Id
:= Etype
(N
);
5770 Force_Expand
: constant Boolean := Is_Anonymous_Access_Actual
(N
);
5771 -- Determine if we are dealing with a special case of a conditional
5772 -- expression used as an actual for an anonymous access type which
5773 -- forces us to transform the if expression into an expression with
5774 -- actions in order to create a temporary to capture the level of the
5775 -- expression in each branch.
5777 function OK_For_Single_Subtype
(T1
, T2
: Entity_Id
) return Boolean;
5778 -- Return true if it is acceptable to use a single subtype for two
5779 -- dependent expressions of subtype T1 and T2 respectively, which are
5780 -- unidimensional arrays whose index bounds are known at compile time.
5782 ---------------------------
5783 -- OK_For_Single_Subtype --
5784 ---------------------------
5786 function OK_For_Single_Subtype
(T1
, T2
: Entity_Id
) return Boolean is
5791 Get_First_Index_Bounds
(T1
, Lo1
, Hi1
);
5792 Get_First_Index_Bounds
(T2
, Lo2
, Hi2
);
5794 -- Return true if the length of the covering subtype is not too large
5797 UI_Max
(Hi1
, Hi2
) - UI_Min
(Lo1
, Lo2
) < Too_Large_Length_For_Array
;
5798 end OK_For_Single_Subtype
;
5808 -- Start of processing for Expand_N_If_Expression
5811 -- Deal with non-standard booleans
5813 Adjust_Condition
(Cond
);
5815 -- Check for MINIMIZED/ELIMINATED overflow mode.
5816 -- Apply_Arithmetic_Overflow_Check will not deal with Then/Else_Actions
5817 -- so skip this step if any actions are present.
5819 if Minimized_Eliminated_Overflow_Check
(N
)
5820 and then No
(Then_Actions
(N
))
5821 and then No
(Else_Actions
(N
))
5823 Apply_Arithmetic_Overflow_Check
(N
);
5827 -- Fold at compile time if condition known. We have already folded
5828 -- static if expressions, but it is possible to fold any case in which
5829 -- the condition is known at compile time, even though the result is
5832 -- Note that we don't do the fold of such cases in Sem_Elab because
5833 -- it can cause infinite loops with the expander adding a conditional
5834 -- expression, and Sem_Elab circuitry removing it repeatedly.
5836 if Compile_Time_Known_Value
(Cond
) then
5838 function Fold_Known_Value
(Cond
: Node_Id
) return Boolean;
5839 -- Fold at compile time. Assumes condition known. Return True if
5840 -- folding occurred, meaning we're done.
5842 ----------------------
5843 -- Fold_Known_Value --
5844 ----------------------
5846 function Fold_Known_Value
(Cond
: Node_Id
) return Boolean is
5848 if Is_True
(Expr_Value
(Cond
)) then
5850 Actions
:= Then_Actions
(N
);
5853 Actions
:= Else_Actions
(N
);
5858 if Present
(Actions
) then
5860 -- To minimize the use of Expression_With_Actions, just skip
5861 -- the optimization as it is not critical for correctness.
5863 if Minimize_Expression_With_Actions
then
5868 Make_Expression_With_Actions
(Loc
,
5869 Expression
=> Relocate_Node
(Expr
),
5870 Actions
=> Actions
));
5871 Analyze_And_Resolve
(N
, Typ
);
5874 Rewrite
(N
, Relocate_Node
(Expr
));
5877 -- Note that the result is never static (legitimate cases of
5878 -- static if expressions were folded in Sem_Eval).
5880 Set_Is_Static_Expression
(N
, False);
5882 end Fold_Known_Value
;
5885 if Fold_Known_Value
(Cond
) then
5891 -- If the type is limited, and the back end does not handle limited
5892 -- types, then we expand as follows to avoid the possibility of
5893 -- improper copying.
5895 -- type Ptr is access all Typ;
5899 -- Cnn := then-expr'Unrestricted_Access;
5902 -- Cnn := else-expr'Unrestricted_Access;
5905 -- and replace the if expression by a reference to Cnn.all.
5907 -- This special case can be skipped if the back end handles limited
5908 -- types properly and ensures that no incorrect copies are made.
5910 if Is_By_Reference_Type
(Typ
)
5911 and then not Back_End_Handles_Limited_Types
5913 -- When the "then" or "else" expressions involve controlled function
5914 -- calls, generated temporaries are chained on the corresponding list
5915 -- of actions. These temporaries need to be finalized after the if
5916 -- expression is evaluated.
5918 Process_If_Case_Statements
(N
, Then_Actions
(N
));
5919 Process_If_Case_Statements
(N
, Else_Actions
(N
));
5922 Cnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'C', N
);
5923 Ptr_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
5927 -- type Ann is access all Typ;
5930 Make_Full_Type_Declaration
(Loc
,
5931 Defining_Identifier
=> Ptr_Typ
,
5933 Make_Access_To_Object_Definition
(Loc
,
5934 All_Present
=> True,
5935 Subtype_Indication
=> New_Occurrence_Of
(Typ
, Loc
))));
5941 Make_Object_Declaration
(Loc
,
5942 Defining_Identifier
=> Cnn
,
5943 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
));
5947 -- Cnn := <Thenx>'Unrestricted_Access;
5949 -- Cnn := <Elsex>'Unrestricted_Access;
5953 Make_Implicit_If_Statement
(N
,
5954 Condition
=> Relocate_Node
(Cond
),
5955 Then_Statements
=> New_List
(
5956 Make_Assignment_Statement
(Sloc
(Thenx
),
5957 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Thenx
)),
5959 Make_Attribute_Reference
(Loc
,
5960 Prefix
=> Relocate_Node
(Thenx
),
5961 Attribute_Name
=> Name_Unrestricted_Access
))),
5963 Else_Statements
=> New_List
(
5964 Make_Assignment_Statement
(Sloc
(Elsex
),
5965 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Elsex
)),
5967 Make_Attribute_Reference
(Loc
,
5968 Prefix
=> Relocate_Node
(Elsex
),
5969 Attribute_Name
=> Name_Unrestricted_Access
))));
5971 -- Preserve the original context for which the if statement is
5972 -- being generated. This is needed by the finalization machinery
5973 -- to prevent the premature finalization of controlled objects
5974 -- found within the if statement.
5976 Set_From_Conditional_Expression
(New_If
);
5979 Make_Explicit_Dereference
(Loc
,
5980 Prefix
=> New_Occurrence_Of
(Cnn
, Loc
));
5983 -- If the result is a unidimensional unconstrained array but the two
5984 -- dependent expressions have constrained subtypes with known bounds,
5985 -- then we expand as follows:
5987 -- subtype Txx is Typ (<static low-bound> .. <static high-bound>);
5991 -- Cnn (<then low-bound .. then high-bound>) := then-expr;
5994 -- Cnn (<else low bound .. else high-bound>) := else-expr;
5997 -- and replace the if expression by a slice of Cnn, provided that Txx
5998 -- is not too large. This will create a static temporary instead of the
5999 -- dynamic one of the next case and thus help the code generator.
6001 -- Note that we need to deal with the case where the else expression is
6002 -- itself such a slice, in order to catch if expressions with more than
6003 -- two dependent expressions in the source code.
6005 -- Also note that this creates variables on branches without an explicit
6006 -- scope, causing troubles with e.g. the LLVM IR, so disable this
6007 -- optimization when Unnest_Subprogram_Mode (enabled for LLVM).
6009 elsif Is_Array_Type
(Typ
)
6010 and then Number_Dimensions
(Typ
) = 1
6011 and then not Is_Constrained
(Typ
)
6012 and then Is_Constrained
(Etype
(Thenx
))
6013 and then Compile_Time_Known_Bounds
(Etype
(Thenx
))
6015 ((Is_Constrained
(Etype
(Elsex
))
6016 and then Compile_Time_Known_Bounds
(Etype
(Elsex
))
6017 and then OK_For_Single_Subtype
(Etype
(Thenx
), Etype
(Elsex
)))
6019 (Nkind
(Elsex
) = N_Slice
6020 and then Is_Constrained
(Etype
(Prefix
(Elsex
)))
6021 and then Compile_Time_Known_Bounds
(Etype
(Prefix
(Elsex
)))
6023 OK_For_Single_Subtype
(Etype
(Thenx
), Etype
(Prefix
(Elsex
)))))
6024 and then not Generate_C_Code
6025 and then not Unnest_Subprogram_Mode
6028 Ityp
: constant Entity_Id
:= Base_Type
(Etype
(First_Index
(Typ
)));
6030 function Build_New_Bound
6033 Slice_Bnd
: Node_Id
) return Node_Id
;
6034 -- Build a new bound from the bounds of the if expression
6036 function To_Ityp
(V
: Uint
) return Node_Id
;
6037 -- Convert V to an index value in Ityp
6039 ---------------------
6040 -- Build_New_Bound --
6041 ---------------------
6043 function Build_New_Bound
6046 Slice_Bnd
: Node_Id
) return Node_Id
is
6049 -- We need to use the special processing for slices only if
6050 -- they do not have compile-time known bounds; if they do, they
6051 -- can be treated like any other expressions.
6053 if Nkind
(Elsex
) = N_Slice
6054 and then not Compile_Time_Known_Bounds
(Etype
(Elsex
))
6056 if Compile_Time_Known_Value
(Slice_Bnd
)
6057 and then Expr_Value
(Slice_Bnd
) = Then_Bnd
6059 return To_Ityp
(Then_Bnd
);
6062 return Make_If_Expression
(Loc
,
6063 Expressions
=> New_List
(
6064 Duplicate_Subexpr
(Cond
),
6066 New_Copy_Tree
(Slice_Bnd
)));
6069 elsif Then_Bnd
= Else_Bnd
then
6070 return To_Ityp
(Then_Bnd
);
6073 return Make_If_Expression
(Loc
,
6074 Expressions
=> New_List
(
6075 Duplicate_Subexpr
(Cond
),
6077 To_Ityp
(Else_Bnd
)));
6079 end Build_New_Bound
;
6085 function To_Ityp
(V
: Uint
) return Node_Id
is
6086 Result
: constant Node_Id
:= Make_Integer_Literal
(Loc
, V
);
6089 if Is_Enumeration_Type
(Ityp
) then
6091 Make_Attribute_Reference
(Loc
,
6092 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
6093 Attribute_Name
=> Name_Val
,
6094 Expressions
=> New_List
(Result
));
6101 Slice_Lo
, Slice_Hi
: Node_Id
;
6102 Subtyp_Ind
: Node_Id
;
6103 Else_Lo
, Else_Hi
: Uint
;
6104 Min_Lo
, Max_Hi
: Uint
;
6105 Then_Lo
, Then_Hi
: Uint
;
6106 Then_List
, Else_List
: List_Id
;
6109 Get_First_Index_Bounds
(Etype
(Thenx
), Then_Lo
, Then_Hi
);
6111 -- See the rationale in Build_New_Bound
6113 if Nkind
(Elsex
) = N_Slice
6114 and then not Compile_Time_Known_Bounds
(Etype
(Elsex
))
6116 Slice_Lo
:= Low_Bound
(Discrete_Range
(Elsex
));
6117 Slice_Hi
:= High_Bound
(Discrete_Range
(Elsex
));
6118 Get_First_Index_Bounds
6119 (Etype
(Prefix
(Elsex
)), Else_Lo
, Else_Hi
);
6124 Get_First_Index_Bounds
(Etype
(Elsex
), Else_Lo
, Else_Hi
);
6127 Min_Lo
:= UI_Min
(Then_Lo
, Else_Lo
);
6128 Max_Hi
:= UI_Max
(Then_Hi
, Else_Hi
);
6130 -- Now we construct an array object with appropriate bounds and
6131 -- mark it as internal to prevent useless initialization when
6132 -- Initialize_Scalars is enabled. Also since this is the actual
6133 -- result entity, we make sure we have debug information for it.
6136 Make_Subtype_Indication
(Loc
,
6137 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Loc
),
6139 Make_Index_Or_Discriminant_Constraint
(Loc
,
6140 Constraints
=> New_List
(
6142 Low_Bound
=> To_Ityp
(Min_Lo
),
6143 High_Bound
=> To_Ityp
(Max_Hi
)))));
6145 Ent
:= Make_Temporary
(Loc
, 'C');
6146 Set_Is_Internal
(Ent
);
6147 Set_Debug_Info_Needed
(Ent
);
6150 Make_Object_Declaration
(Loc
,
6151 Defining_Identifier
=> Ent
,
6152 Object_Definition
=> Subtyp_Ind
);
6154 -- If the result of the expression appears as the initializing
6155 -- expression of an object declaration, we can just rename the
6156 -- result, rather than copying it.
6158 Mutate_Ekind
(Ent
, E_Variable
);
6159 Set_OK_To_Rename
(Ent
);
6161 Then_List
:= New_List
(
6162 Make_Assignment_Statement
(Loc
,
6165 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
6168 Low_Bound
=> To_Ityp
(Then_Lo
),
6169 High_Bound
=> To_Ityp
(Then_Hi
))),
6170 Expression
=> Relocate_Node
(Thenx
)));
6172 Set_Suppress_Assignment_Checks
(Last
(Then_List
));
6174 -- See the rationale in Build_New_Bound
6176 if Nkind
(Elsex
) = N_Slice
6177 and then not Compile_Time_Known_Bounds
(Etype
(Elsex
))
6179 Else_List
:= New_List
(
6180 Make_Assignment_Statement
(Loc
,
6183 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
6186 Low_Bound
=> New_Copy_Tree
(Slice_Lo
),
6187 High_Bound
=> New_Copy_Tree
(Slice_Hi
))),
6188 Expression
=> Relocate_Node
(Elsex
)));
6191 Else_List
:= New_List
(
6192 Make_Assignment_Statement
(Loc
,
6195 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
6198 Low_Bound
=> To_Ityp
(Else_Lo
),
6199 High_Bound
=> To_Ityp
(Else_Hi
))),
6200 Expression
=> Relocate_Node
(Elsex
)));
6203 Set_Suppress_Assignment_Checks
(Last
(Else_List
));
6206 Make_Implicit_If_Statement
(N
,
6207 Condition
=> Duplicate_Subexpr
(Cond
),
6208 Then_Statements
=> Then_List
,
6209 Else_Statements
=> Else_List
);
6213 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
6214 Discrete_Range
=> Make_Range
(Loc
,
6215 Low_Bound
=> Build_New_Bound
(Then_Lo
, Else_Lo
, Slice_Lo
),
6216 High_Bound
=> Build_New_Bound
(Then_Hi
, Else_Hi
, Slice_Hi
)));
6219 -- If the result is an unconstrained array and the if expression is in a
6220 -- context other than the initializing expression of the declaration of
6221 -- an object, then we pull out the if expression as follows:
6223 -- Cnn : constant typ := if-expression
6225 -- and then replace the if expression with an occurrence of Cnn. This
6226 -- avoids the need in the back end to create on-the-fly variable length
6227 -- temporaries (which it cannot do!)
6229 -- Note that the test for being in an object declaration avoids doing an
6230 -- unnecessary expansion, and also avoids infinite recursion.
6232 elsif Is_Array_Type
(Typ
) and then not Is_Constrained
(Typ
)
6233 and then (Nkind
(Parent
(N
)) /= N_Object_Declaration
6234 or else Expression
(Parent
(N
)) /= N
)
6237 Cnn
: constant Node_Id
:= Make_Temporary
(Loc
, 'C', N
);
6241 Make_Object_Declaration
(Loc
,
6242 Defining_Identifier
=> Cnn
,
6243 Constant_Present
=> True,
6244 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
6245 Expression
=> Relocate_Node
(N
),
6246 Has_Init_Expression
=> True));
6248 Rewrite
(N
, New_Occurrence_Of
(Cnn
, Loc
));
6252 -- For other types, we only need to expand if there are other actions
6253 -- associated with either branch or we need to force expansion to deal
6254 -- with if expressions used as an actual of an anonymous access type.
6256 elsif Present
(Then_Actions
(N
))
6257 or else Present
(Else_Actions
(N
))
6258 or else Force_Expand
6261 -- We now wrap the actions into the appropriate expression
6263 if Minimize_Expression_With_Actions
6264 and then (Is_Elementary_Type
(Underlying_Type
(Typ
))
6265 or else Is_Constrained
(Underlying_Type
(Typ
)))
6267 -- If we can't use N_Expression_With_Actions nodes, then we insert
6268 -- the following sequence of actions (using Insert_Actions):
6273 -- Cnn := then-expr;
6279 -- and replace the if expression by a reference to Cnn
6282 Cnn
: constant Node_Id
:= Make_Temporary
(Loc
, 'C', N
);
6286 Make_Object_Declaration
(Loc
,
6287 Defining_Identifier
=> Cnn
,
6288 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
6291 Make_Implicit_If_Statement
(N
,
6292 Condition
=> Relocate_Node
(Cond
),
6294 Then_Statements
=> New_List
(
6295 Make_Assignment_Statement
(Sloc
(Thenx
),
6296 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Thenx
)),
6297 Expression
=> Relocate_Node
(Thenx
))),
6299 Else_Statements
=> New_List
(
6300 Make_Assignment_Statement
(Sloc
(Elsex
),
6301 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Elsex
)),
6302 Expression
=> Relocate_Node
(Elsex
))));
6304 Set_Assignment_OK
(Name
(First
(Then_Statements
(New_If
))));
6305 Set_Assignment_OK
(Name
(First
(Else_Statements
(New_If
))));
6307 New_N
:= New_Occurrence_Of
(Cnn
, Loc
);
6310 -- Regular path using Expression_With_Actions
6313 if Present
(Then_Actions
(N
)) then
6315 Make_Expression_With_Actions
(Sloc
(Thenx
),
6316 Actions
=> Then_Actions
(N
),
6317 Expression
=> Relocate_Node
(Thenx
)));
6319 Set_Then_Actions
(N
, No_List
);
6320 Analyze_And_Resolve
(Thenx
, Typ
);
6323 if Present
(Else_Actions
(N
)) then
6325 Make_Expression_With_Actions
(Sloc
(Elsex
),
6326 Actions
=> Else_Actions
(N
),
6327 Expression
=> Relocate_Node
(Elsex
)));
6329 Set_Else_Actions
(N
, No_List
);
6330 Analyze_And_Resolve
(Elsex
, Typ
);
6333 -- We must force expansion into an expression with actions when
6334 -- an if expression gets used directly as an actual for an
6335 -- anonymous access type.
6337 if Force_Expand
then
6339 Cnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'C');
6348 Make_Object_Declaration
(Loc
,
6349 Defining_Identifier
=> Cnn
,
6350 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
6351 Append_To
(Acts
, Decl
);
6353 Set_No_Initialization
(Decl
);
6363 Make_Implicit_If_Statement
(N
,
6364 Condition
=> Relocate_Node
(Cond
),
6365 Then_Statements
=> New_List
(
6366 Make_Assignment_Statement
(Sloc
(Thenx
),
6367 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Thenx
)),
6368 Expression
=> Relocate_Node
(Thenx
))),
6370 Else_Statements
=> New_List
(
6371 Make_Assignment_Statement
(Sloc
(Elsex
),
6372 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Elsex
)),
6373 Expression
=> Relocate_Node
(Elsex
))));
6374 Append_To
(Acts
, New_If
);
6382 Make_Expression_With_Actions
(Loc
,
6383 Expression
=> New_Occurrence_Of
(Cnn
, Loc
),
6385 Analyze_And_Resolve
(N
, Typ
);
6392 -- For the sake of GNATcoverage, generate an intermediate temporary in
6393 -- the case where the if expression is a condition in an outer decision,
6394 -- in order to make sure that no branch is shared between the decisions.
6396 elsif Opt
.Suppress_Control_Flow_Optimizations
6397 and then Nkind
(Original_Node
(Parent
(N
))) in N_Case_Expression
6401 | N_Goto_When_Statement
6403 | N_Return_When_Statement
6407 Cnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'C');
6413 -- Cnn : constant Typ := N;
6417 Make_Object_Declaration
(Loc
,
6418 Defining_Identifier
=> Cnn
,
6419 Constant_Present
=> True,
6420 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
6421 Expression
=> Relocate_Node
(N
)));
6424 Make_Expression_With_Actions
(Loc
,
6425 Expression
=> New_Occurrence_Of
(Cnn
, Loc
),
6428 Analyze_And_Resolve
(N
, Typ
);
6432 -- If no actions then no expansion needed, gigi will handle it using the
6433 -- same approach as a C conditional expression.
6439 -- Fall through here for either the limited expansion, or the case of
6440 -- inserting actions for nonlimited types. In both these cases, we must
6441 -- move the SLOC of the parent If statement to the newly created one and
6442 -- change it to the SLOC of the expression which, after expansion, will
6443 -- correspond to what is being evaluated.
6445 if Present
(Parent
(N
)) and then Nkind
(Parent
(N
)) = N_If_Statement
then
6446 Set_Sloc
(New_If
, Sloc
(Parent
(N
)));
6447 Set_Sloc
(Parent
(N
), Loc
);
6450 -- Move Then_Actions and Else_Actions, if any, to the new if statement
6452 Insert_List_Before
(First
(Then_Statements
(New_If
)), Then_Actions
(N
));
6453 Insert_List_Before
(First
(Else_Statements
(New_If
)), Else_Actions
(N
));
6455 Insert_Action
(N
, Decl
);
6456 Insert_Action
(N
, New_If
);
6458 Analyze_And_Resolve
(N
, Typ
);
6459 end Expand_N_If_Expression
;
6465 procedure Expand_N_In
(N
: Node_Id
) is
6466 Loc
: constant Source_Ptr
:= Sloc
(N
);
6467 Restyp
: constant Entity_Id
:= Etype
(N
);
6468 Lop
: constant Node_Id
:= Left_Opnd
(N
);
6469 Rop
: constant Node_Id
:= Right_Opnd
(N
);
6470 Static
: constant Boolean := Is_OK_Static_Expression
(N
);
6472 procedure Substitute_Valid_Test
;
6473 -- Replaces node N by Lop'Valid. This is done when we have an explicit
6474 -- test for the left operand being in range of its subtype.
6476 ---------------------------
6477 -- Substitute_Valid_Test --
6478 ---------------------------
6480 procedure Substitute_Valid_Test
is
6481 function Is_OK_Object_Reference
(Nod
: Node_Id
) return Boolean;
6482 -- Determine whether arbitrary node Nod denotes a source object that
6483 -- may safely act as prefix of attribute 'Valid.
6485 ----------------------------
6486 -- Is_OK_Object_Reference --
6487 ----------------------------
6489 function Is_OK_Object_Reference
(Nod
: Node_Id
) return Boolean is
6493 -- Inspect the original operand
6495 Obj_Ref
:= Original_Node
(Nod
);
6497 -- The object reference must be a source construct, otherwise the
6498 -- codefix suggestion may refer to nonexistent code from a user
6501 if Comes_From_Source
(Obj_Ref
) then
6503 if Nkind
(Obj_Ref
) in
6505 N_Unchecked_Type_Conversion |
6506 N_Qualified_Expression
6508 Obj_Ref
:= Expression
(Obj_Ref
);
6514 return Is_Object_Reference
(Obj_Ref
);
6518 end Is_OK_Object_Reference
;
6520 -- Start of processing for Substitute_Valid_Test
6524 Make_Attribute_Reference
(Loc
,
6525 Prefix
=> Relocate_Node
(Lop
),
6526 Attribute_Name
=> Name_Valid
));
6528 Analyze_And_Resolve
(N
, Restyp
);
6530 -- Emit a warning when the left-hand operand of the membership test
6531 -- is a source object, otherwise the use of attribute 'Valid would be
6532 -- illegal. The warning is not given when overflow checking is either
6533 -- MINIMIZED or ELIMINATED, as the danger of optimization has been
6534 -- eliminated above.
6536 if Is_OK_Object_Reference
(Lop
)
6537 and then Overflow_Check_Mode
not in Minimized_Or_Eliminated
6540 ("??explicit membership test may be optimized away", N
);
6541 Error_Msg_N
-- CODEFIX
6542 ("\??use ''Valid attribute instead", N
);
6544 end Substitute_Valid_Test
;
6551 -- Start of processing for Expand_N_In
6554 -- If set membership case, expand with separate procedure
6556 if Present
(Alternatives
(N
)) then
6557 Expand_Set_Membership
(N
);
6561 -- Not set membership, proceed with expansion
6563 Ltyp
:= Etype
(Left_Opnd
(N
));
6564 Rtyp
:= Etype
(Right_Opnd
(N
));
6566 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
6567 -- type, then expand with a separate procedure. Note the use of the
6568 -- flag No_Minimize_Eliminate to prevent infinite recursion.
6570 if Minimized_Eliminated_Overflow_Check
(Left_Opnd
(N
))
6571 and then not No_Minimize_Eliminate
(N
)
6573 Expand_Membership_Minimize_Eliminate_Overflow
(N
);
6577 -- Check case of explicit test for an expression in range of its
6578 -- subtype. This is suspicious usage and we replace it with a 'Valid
6579 -- test and give a warning for scalar types.
6581 if Is_Scalar_Type
(Ltyp
)
6583 -- Only relevant for source comparisons
6585 and then Comes_From_Source
(N
)
6587 -- In floating-point this is a standard way to check for finite values
6588 -- and using 'Valid would typically be a pessimization.
6590 and then not Is_Floating_Point_Type
(Ltyp
)
6592 -- Don't give the message unless right operand is a type entity and
6593 -- the type of the left operand matches this type. Note that this
6594 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
6595 -- checks have changed the type of the left operand.
6597 and then Is_Entity_Name
(Rop
)
6598 and then Ltyp
= Entity
(Rop
)
6600 -- Skip this for predicated types, where such expressions are a
6601 -- reasonable way of testing if something meets the predicate.
6603 and then No
(Predicate_Function
(Ltyp
))
6605 Substitute_Valid_Test
;
6609 -- Do validity check on operands
6611 if Validity_Checks_On
and Validity_Check_Operands
then
6612 Ensure_Valid
(Left_Opnd
(N
));
6613 Validity_Check_Range
(Right_Opnd
(N
));
6616 -- Case of explicit range
6618 if Nkind
(Rop
) = N_Range
then
6620 Lo
: constant Node_Id
:= Low_Bound
(Rop
);
6621 Hi
: constant Node_Id
:= High_Bound
(Rop
);
6623 Lo_Orig
: constant Node_Id
:= Original_Node
(Lo
);
6624 Hi_Orig
: constant Node_Id
:= Original_Node
(Hi
);
6625 Rop_Orig
: constant Node_Id
:= Original_Node
(Rop
);
6627 Comes_From_Simple_Range_In_Source
: constant Boolean :=
6628 Comes_From_Source
(N
)
6630 (Is_Entity_Name
(Rop_Orig
)
6631 and then Is_Type
(Entity
(Rop_Orig
))
6632 and then Present
(Predicate_Function
(Entity
(Rop_Orig
))));
6633 -- This is true for a membership test present in the source with a
6634 -- range or mark for a subtype that is not predicated. As already
6635 -- explained a few lines above, we do not want to give warnings on
6636 -- a test with a mark for a subtype that is predicated.
6638 Warn
: constant Boolean :=
6639 Constant_Condition_Warnings
6640 and then Comes_From_Simple_Range_In_Source
6641 and then not In_Instance
;
6642 -- This must be true for any of the optimization warnings, we
6643 -- clearly want to give them only for source with the flag on. We
6644 -- also skip these warnings in an instance since it may be the
6645 -- case that different instantiations have different ranges.
6647 Lcheck
: Compare_Result
;
6648 Ucheck
: Compare_Result
;
6651 -- If test is explicit x'First .. x'Last, replace by 'Valid test
6653 if Is_Scalar_Type
(Ltyp
)
6655 -- Only relevant for source comparisons
6657 and then Comes_From_Simple_Range_In_Source
6659 -- And left operand is X'First where X matches left operand
6660 -- type (this eliminates cases of type mismatch, including
6661 -- the cases where ELIMINATED/MINIMIZED mode has changed the
6662 -- type of the left operand.
6664 and then Nkind
(Lo_Orig
) = N_Attribute_Reference
6665 and then Attribute_Name
(Lo_Orig
) = Name_First
6666 and then Is_Entity_Name
(Prefix
(Lo_Orig
))
6667 and then Entity
(Prefix
(Lo_Orig
)) = Ltyp
6669 -- Same tests for right operand
6671 and then Nkind
(Hi_Orig
) = N_Attribute_Reference
6672 and then Attribute_Name
(Hi_Orig
) = Name_Last
6673 and then Is_Entity_Name
(Prefix
(Hi_Orig
))
6674 and then Entity
(Prefix
(Hi_Orig
)) = Ltyp
6676 Substitute_Valid_Test
;
6680 -- If bounds of type are known at compile time, and the end points
6681 -- are known at compile time and identical, this is another case
6682 -- for substituting a valid test. We only do this for discrete
6683 -- types, since it won't arise in practice for float types.
6685 if Comes_From_Simple_Range_In_Source
6686 and then Is_Discrete_Type
(Ltyp
)
6687 and then Compile_Time_Known_Value
(Type_High_Bound
(Ltyp
))
6688 and then Compile_Time_Known_Value
(Type_Low_Bound
(Ltyp
))
6689 and then Compile_Time_Known_Value
(Lo
)
6690 and then Compile_Time_Known_Value
(Hi
)
6691 and then Expr_Value
(Type_High_Bound
(Ltyp
)) = Expr_Value
(Hi
)
6692 and then Expr_Value
(Type_Low_Bound
(Ltyp
)) = Expr_Value
(Lo
)
6694 -- Kill warnings in instances, since they may be cases where we
6695 -- have a test in the generic that makes sense with some types
6696 -- and not with other types.
6698 -- Similarly, do not rewrite membership as a 'Valid test if
6699 -- within the predicate function for the type.
6701 -- Finally, if the original bounds are type conversions, even
6702 -- if they have been folded into constants, there are different
6703 -- types involved and 'Valid is not appropriate.
6707 or else (Ekind
(Current_Scope
) = E_Function
6708 and then Is_Predicate_Function
(Current_Scope
))
6712 elsif Nkind
(Lo_Orig
) = N_Type_Conversion
6713 or else Nkind
(Hi_Orig
) = N_Type_Conversion
6718 Substitute_Valid_Test
;
6723 -- If we have an explicit range, do a bit of optimization based on
6724 -- range analysis (we may be able to kill one or both checks).
6726 Lcheck
:= Compile_Time_Compare
(Lop
, Lo
, Assume_Valid
=> False);
6727 Ucheck
:= Compile_Time_Compare
(Lop
, Hi
, Assume_Valid
=> False);
6729 -- If either check is known to fail, replace result by False since
6730 -- the other check does not matter. Preserve the static flag for
6731 -- legality checks, because we are constant-folding beyond RM 4.9.
6733 if Lcheck
= LT
or else Ucheck
= GT
then
6735 Error_Msg_N
("?c?range test optimized away", N
);
6736 Error_Msg_N
("\?c?value is known to be out of range", N
);
6739 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
6740 Analyze_And_Resolve
(N
, Restyp
);
6741 Set_Is_Static_Expression
(N
, Static
);
6744 -- If both checks are known to succeed, replace result by True,
6745 -- since we know we are in range.
6747 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
6749 Error_Msg_N
("?c?range test optimized away", N
);
6750 Error_Msg_N
("\?c?value is known to be in range", N
);
6753 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
6754 Analyze_And_Resolve
(N
, Restyp
);
6755 Set_Is_Static_Expression
(N
, Static
);
6758 -- If lower bound check succeeds and upper bound check is not
6759 -- known to succeed or fail, then replace the range check with
6760 -- a comparison against the upper bound.
6762 elsif Lcheck
in Compare_GE
then
6766 Right_Opnd
=> High_Bound
(Rop
)));
6767 Analyze_And_Resolve
(N
, Restyp
);
6770 -- Inverse of previous case.
6772 elsif Ucheck
in Compare_LE
then
6776 Right_Opnd
=> Low_Bound
(Rop
)));
6777 Analyze_And_Resolve
(N
, Restyp
);
6781 -- We couldn't optimize away the range check, but there is one
6782 -- more issue. If we are checking constant conditionals, then we
6783 -- see if we can determine the outcome assuming everything is
6784 -- valid, and if so give an appropriate warning.
6786 if Warn
and then not Assume_No_Invalid_Values
then
6787 Lcheck
:= Compile_Time_Compare
(Lop
, Lo
, Assume_Valid
=> True);
6788 Ucheck
:= Compile_Time_Compare
(Lop
, Hi
, Assume_Valid
=> True);
6790 -- Result is out of range for valid value
6792 if Lcheck
= LT
or else Ucheck
= GT
then
6794 ("?c?value can only be in range if it is invalid", N
);
6796 -- Result is in range for valid value
6798 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
6800 ("?c?value can only be out of range if it is invalid", N
);
6805 -- Try to narrow the operation
6807 if Ltyp
= Universal_Integer
and then Nkind
(N
) = N_In
then
6808 Narrow_Large_Operation
(N
);
6811 -- For all other cases of an explicit range, nothing to be done
6815 -- Here right operand is a subtype mark
6819 Typ
: Entity_Id
:= Etype
(Rop
);
6820 Is_Acc
: constant Boolean := Is_Access_Type
(Typ
);
6821 Check_Null_Exclusion
: Boolean;
6822 Cond
: Node_Id
:= Empty
;
6824 Obj
: Node_Id
:= Lop
;
6825 SCIL_Node
: Node_Id
;
6828 Remove_Side_Effects
(Obj
);
6830 -- For tagged type, do tagged membership operation
6832 if Is_Tagged_Type
(Typ
) then
6834 -- No expansion will be performed for VM targets, as the VM
6835 -- back ends will handle the membership tests directly.
6837 if Tagged_Type_Expansion
then
6838 Tagged_Membership
(N
, SCIL_Node
, New_N
);
6840 Analyze_And_Resolve
(N
, Restyp
, Suppress
=> All_Checks
);
6842 -- Update decoration of relocated node referenced by the
6845 if Generate_SCIL
and then Present
(SCIL_Node
) then
6846 Set_SCIL_Node
(N
, SCIL_Node
);
6852 -- If type is scalar type, rewrite as x in t'First .. t'Last.
6853 -- The reason we do this is that the bounds may have the wrong
6854 -- type if they come from the original type definition. Also this
6855 -- way we get all the processing above for an explicit range.
6857 -- Don't do this for predicated types, since in this case we want
6858 -- to generate the predicate check at the end of the function.
6860 elsif Is_Scalar_Type
(Typ
) then
6861 if No
(Predicate_Function
(Typ
)) then
6865 Make_Attribute_Reference
(Loc
,
6866 Attribute_Name
=> Name_First
,
6867 Prefix
=> New_Occurrence_Of
(Typ
, Loc
)),
6870 Make_Attribute_Reference
(Loc
,
6871 Attribute_Name
=> Name_Last
,
6872 Prefix
=> New_Occurrence_Of
(Typ
, Loc
))));
6874 Analyze_And_Resolve
(N
, Restyp
);
6879 -- Ada 2005 (AI95-0216 amended by AI12-0162): Program_Error is
6880 -- raised when evaluating an individual membership test if the
6881 -- subtype mark denotes a constrained Unchecked_Union subtype
6882 -- and the expression lacks inferable discriminants.
6884 elsif Is_Unchecked_Union
(Base_Type
(Typ
))
6885 and then Is_Constrained
(Typ
)
6886 and then not Has_Inferable_Discriminants
(Lop
)
6889 Make_Expression_With_Actions
(Loc
,
6891 New_List
(Make_Raise_Program_Error
(Loc
,
6892 Reason
=> PE_Unchecked_Union_Restriction
)),
6894 New_Occurrence_Of
(Standard_False
, Loc
)));
6895 Analyze_And_Resolve
(N
, Restyp
);
6900 -- Here we have a non-scalar type
6904 -- If the null exclusion checks are not compatible, need to
6905 -- perform further checks. In other words, we cannot have
6906 -- Ltyp including null and Typ excluding null. All other cases
6909 Check_Null_Exclusion
:=
6910 Can_Never_Be_Null
(Typ
) and then not Can_Never_Be_Null
(Ltyp
);
6911 Typ
:= Designated_Type
(Typ
);
6914 if not Is_Constrained
(Typ
) then
6915 Cond
:= New_Occurrence_Of
(Standard_True
, Loc
);
6917 -- For the constrained array case, we have to check the subscripts
6918 -- for an exact match if the lengths are non-zero (the lengths
6919 -- must match in any case).
6921 elsif Is_Array_Type
(Typ
) then
6922 Check_Subscripts
: declare
6923 function Build_Attribute_Reference
6926 Dim
: Nat
) return Node_Id
;
6927 -- Build attribute reference E'Nam (Dim)
6929 -------------------------------
6930 -- Build_Attribute_Reference --
6931 -------------------------------
6933 function Build_Attribute_Reference
6936 Dim
: Nat
) return Node_Id
6940 Make_Attribute_Reference
(Loc
,
6942 Attribute_Name
=> Nam
,
6943 Expressions
=> New_List
(
6944 Make_Integer_Literal
(Loc
, Dim
)));
6945 end Build_Attribute_Reference
;
6947 -- Start of processing for Check_Subscripts
6950 for J
in 1 .. Number_Dimensions
(Typ
) loop
6951 Evolve_And_Then
(Cond
,
6954 Build_Attribute_Reference
6955 (Duplicate_Subexpr_No_Checks
(Obj
),
6958 Build_Attribute_Reference
6959 (New_Occurrence_Of
(Typ
, Loc
), Name_First
, J
)));
6961 Evolve_And_Then
(Cond
,
6964 Build_Attribute_Reference
6965 (Duplicate_Subexpr_No_Checks
(Obj
),
6968 Build_Attribute_Reference
6969 (New_Occurrence_Of
(Typ
, Loc
), Name_Last
, J
)));
6971 end Check_Subscripts
;
6973 -- These are the cases where constraint checks may be required,
6974 -- e.g. records with possible discriminants
6977 -- Expand the test into a series of discriminant comparisons.
6978 -- The expression that is built is the negation of the one that
6979 -- is used for checking discriminant constraints.
6981 Obj
:= Relocate_Node
(Left_Opnd
(N
));
6983 if Has_Discriminants
(Typ
) then
6984 Cond
:= Make_Op_Not
(Loc
,
6985 Right_Opnd
=> Build_Discriminant_Checks
(Obj
, Typ
));
6987 Cond
:= New_Occurrence_Of
(Standard_True
, Loc
);
6992 if Check_Null_Exclusion
then
6993 Cond
:= Make_And_Then
(Loc
,
6997 Right_Opnd
=> Make_Null
(Loc
)),
6998 Right_Opnd
=> Cond
);
7000 Cond
:= Make_Or_Else
(Loc
,
7004 Right_Opnd
=> Make_Null
(Loc
)),
7005 Right_Opnd
=> Cond
);
7010 Analyze_And_Resolve
(N
, Restyp
);
7012 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
7013 -- expression of an anonymous access type. This can involve an
7014 -- accessibility test and a tagged type membership test in the
7015 -- case of tagged designated types.
7017 if Ada_Version
>= Ada_2012
7019 and then Ekind
(Ltyp
) = E_Anonymous_Access_Type
7022 Expr_Entity
: Entity_Id
:= Empty
;
7024 Param_Level
: Node_Id
;
7025 Type_Level
: Node_Id
;
7028 if Is_Entity_Name
(Lop
) then
7029 Expr_Entity
:= Param_Entity
(Lop
);
7031 if No
(Expr_Entity
) then
7032 Expr_Entity
:= Entity
(Lop
);
7036 -- When restriction No_Dynamic_Accessibility_Checks is in
7037 -- effect, expand the membership test to a static value
7038 -- since we cannot rely on dynamic levels.
7040 if No_Dynamic_Accessibility_Checks_Enabled
(Lop
) then
7041 if Static_Accessibility_Level
7042 (Lop
, Object_Decl_Level
)
7043 > Type_Access_Level
(Rtyp
)
7045 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
7047 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
7049 Analyze_And_Resolve
(N
, Restyp
);
7051 -- If a conversion of the anonymous access value to the
7052 -- tested type would be illegal, then the result is False.
7054 elsif not Valid_Conversion
7055 (Lop
, Rtyp
, Lop
, Report_Errs
=> False)
7057 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
7058 Analyze_And_Resolve
(N
, Restyp
);
7060 -- Apply an accessibility check if the access object has an
7061 -- associated access level and when the level of the type is
7062 -- less deep than the level of the access parameter. This
7063 -- can only occur for access parameters and stand-alone
7064 -- objects of an anonymous access type.
7067 Param_Level
:= Accessibility_Level
7068 (Expr_Entity
, Dynamic_Level
);
7071 Make_Integer_Literal
(Loc
, Type_Access_Level
(Rtyp
));
7073 -- Return True only if the accessibility level of the
7074 -- expression entity is not deeper than the level of
7075 -- the tested access type.
7079 Left_Opnd
=> Relocate_Node
(N
),
7080 Right_Opnd
=> Make_Op_Le
(Loc
,
7081 Left_Opnd
=> Param_Level
,
7082 Right_Opnd
=> Type_Level
)));
7084 Analyze_And_Resolve
(N
);
7086 -- If the designated type is tagged, do tagged membership
7089 if Is_Tagged_Type
(Typ
) then
7091 -- No expansion will be performed for VM targets, as
7092 -- the VM back ends will handle the membership tests
7095 if Tagged_Type_Expansion
then
7097 -- Note that we have to pass Original_Node, because
7098 -- the membership test might already have been
7099 -- rewritten by earlier parts of membership test.
7102 (Original_Node
(N
), SCIL_Node
, New_N
);
7104 -- Update decoration of relocated node referenced
7105 -- by the SCIL node.
7107 if Generate_SCIL
and then Present
(SCIL_Node
) then
7108 Set_SCIL_Node
(New_N
, SCIL_Node
);
7113 Left_Opnd
=> Relocate_Node
(N
),
7114 Right_Opnd
=> New_N
));
7116 Analyze_And_Resolve
(N
, Restyp
);
7125 -- At this point, we have done the processing required for the basic
7126 -- membership test, but not yet dealt with the predicate.
7130 -- If a predicate is present, then we do the predicate test, but we
7131 -- most certainly want to omit this if we are within the predicate
7132 -- function itself, since otherwise we have an infinite recursion.
7133 -- The check should also not be emitted when testing against a range
7134 -- (the check is only done when the right operand is a subtype; see
7135 -- RM12-4.5.2 (28.1/3-30/3)).
7137 Predicate_Check
: declare
7138 function In_Range_Check
return Boolean;
7139 -- Within an expanded range check that may raise Constraint_Error do
7140 -- not generate a predicate check as well. It is redundant because
7141 -- the context will add an explicit predicate check, and it will
7142 -- raise the wrong exception if it fails.
7144 --------------------
7145 -- In_Range_Check --
7146 --------------------
7148 function In_Range_Check
return Boolean is
7152 while Present
(P
) loop
7153 if Nkind
(P
) = N_Raise_Constraint_Error
then
7156 elsif Nkind
(P
) in N_Statement_Other_Than_Procedure_Call
7157 or else Nkind
(P
) = N_Procedure_Call_Statement
7158 or else Nkind
(P
) in N_Declaration
7171 PFunc
: constant Entity_Id
:= Predicate_Function
(Rtyp
);
7174 -- Start of processing for Predicate_Check
7178 and then Current_Scope
/= PFunc
7179 and then Nkind
(Rop
) /= N_Range
7181 -- First apply the transformation that was skipped above
7183 if Is_Scalar_Type
(Rtyp
) then
7187 Make_Attribute_Reference
(Loc
,
7188 Attribute_Name
=> Name_First
,
7189 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
)),
7192 Make_Attribute_Reference
(Loc
,
7193 Attribute_Name
=> Name_Last
,
7194 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
))));
7196 Analyze_And_Resolve
(N
, Restyp
);
7199 if not In_Range_Check
then
7200 -- Indicate via Static_Mem parameter that this predicate
7201 -- evaluation is for a membership test.
7202 R_Op
:= Make_Predicate_Call
(Rtyp
, Lop
, Static_Mem
=> True);
7204 R_Op
:= New_Occurrence_Of
(Standard_True
, Loc
);
7209 Left_Opnd
=> Relocate_Node
(N
),
7210 Right_Opnd
=> R_Op
));
7212 -- Analyze new expression, mark left operand as analyzed to
7213 -- avoid infinite recursion adding predicate calls. Similarly,
7214 -- suppress further range checks on the call.
7216 Set_Analyzed
(Left_Opnd
(N
));
7217 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7219 end Predicate_Check
;
7222 --------------------------------
7223 -- Expand_N_Indexed_Component --
7224 --------------------------------
7226 procedure Expand_N_Indexed_Component
(N
: Node_Id
) is
7228 Wild_Reads_May_Have_Bad_Side_Effects
: Boolean
7229 renames Validity_Check_Subscripts
;
7230 -- This Boolean needs to be True if reading from a bad address can
7231 -- have a bad side effect (e.g., a segmentation fault that is not
7232 -- transformed into a Storage_Error exception, or interactions with
7233 -- memory-mapped I/O) that needs to be prevented. This refers to the
7234 -- act of reading itself, not to any damage that might be caused later
7235 -- by making use of whatever value was read. We assume here that
7236 -- Validity_Check_Subscripts meets this requirement, but introduce
7237 -- this declaration in order to document this assumption.
7239 function Is_Renamed_Variable_Name
(N
: Node_Id
) return Boolean;
7240 -- Returns True if the given name occurs as part of the renaming
7241 -- of a variable. In this case, the indexing operation should be
7242 -- treated as a write, rather than a read, with respect to validity
7243 -- checking. This is because the renamed variable can later be
7246 function Type_Requires_Subscript_Validity_Checks_For_Reads
7247 (Typ
: Entity_Id
) return Boolean;
7248 -- If Wild_Reads_May_Have_Bad_Side_Effects is False and we are indexing
7249 -- into an array of characters in order to read an element, it is ok
7250 -- if an invalid index value goes undetected. But if it is an array of
7251 -- pointers or an array of tasks, the consequences of such a read are
7252 -- potentially more severe and so we want to detect an invalid index
7253 -- value. This function captures that distinction; this is intended to
7254 -- be consistent with the "but does not by itself lead to erroneous
7255 -- ... execution" rule of RM 13.9.1(11).
7257 ------------------------------
7258 -- Is_Renamed_Variable_Name --
7259 ------------------------------
7261 function Is_Renamed_Variable_Name
(N
: Node_Id
) return Boolean is
7262 Rover
: Node_Id
:= N
;
7264 if Is_Variable
(N
) then
7267 Rover_Parent
: constant Node_Id
:= Parent
(Rover
);
7269 case Nkind
(Rover_Parent
) is
7270 when N_Object_Renaming_Declaration
=>
7271 return Rover
= Name
(Rover_Parent
);
7273 when N_Indexed_Component
7275 | N_Selected_Component
7277 exit when Rover
/= Prefix
(Rover_Parent
);
7278 Rover
:= Rover_Parent
;
7280 -- No need to check for qualified expressions or type
7281 -- conversions here, mostly because of the Is_Variable
7282 -- test. It is possible to have a view conversion for
7283 -- which Is_Variable yields True and which occurs as
7284 -- part of an object renaming, but only if the type is
7285 -- tagged; in that case this function will not be called.
7294 end Is_Renamed_Variable_Name
;
7296 -------------------------------------------------------
7297 -- Type_Requires_Subscript_Validity_Checks_For_Reads --
7298 -------------------------------------------------------
7300 function Type_Requires_Subscript_Validity_Checks_For_Reads
7301 (Typ
: Entity_Id
) return Boolean
7303 -- a shorter name for recursive calls
7304 function Needs_Check
(Typ
: Entity_Id
) return Boolean renames
7305 Type_Requires_Subscript_Validity_Checks_For_Reads
;
7307 if Is_Access_Type
(Typ
)
7308 or else Is_Tagged_Type
(Typ
)
7309 or else Is_Concurrent_Type
(Typ
)
7310 or else (Is_Array_Type
(Typ
)
7311 and then Needs_Check
(Component_Type
(Typ
)))
7312 or else (Is_Scalar_Type
(Typ
)
7313 and then Has_Aspect
(Typ
, Aspect_Default_Value
))
7318 if Is_Record_Type
(Typ
) then
7320 Comp
: Entity_Id
:= First_Component_Or_Discriminant
(Typ
);
7322 while Present
(Comp
) loop
7323 if Needs_Check
(Etype
(Comp
)) then
7327 Next_Component_Or_Discriminant
(Comp
);
7333 end Type_Requires_Subscript_Validity_Checks_For_Reads
;
7337 Loc
: constant Source_Ptr
:= Sloc
(N
);
7338 Typ
: constant Entity_Id
:= Etype
(N
);
7339 P
: constant Node_Id
:= Prefix
(N
);
7340 T
: constant Entity_Id
:= Etype
(P
);
7342 -- Start of processing for Expand_N_Indexed_Component
7345 -- A special optimization, if we have an indexed component that is
7346 -- selecting from a slice, then we can eliminate the slice, since, for
7347 -- example, x (i .. j)(k) is identical to x(k). The only difference is
7348 -- the range check required by the slice. The range check for the slice
7349 -- itself has already been generated. The range check for the
7350 -- subscripting operation is ensured by converting the subject to
7351 -- the subtype of the slice.
7353 -- This optimization not only generates better code, avoiding slice
7354 -- messing especially in the packed case, but more importantly bypasses
7355 -- some problems in handling this peculiar case, for example, the issue
7356 -- of dealing specially with object renamings.
7358 if Nkind
(P
) = N_Slice
7360 -- This optimization is disabled for CodePeer because it can transform
7361 -- an index-check constraint_error into a range-check constraint_error
7362 -- and CodePeer cares about that distinction.
7364 and then not CodePeer_Mode
7367 Make_Indexed_Component
(Loc
,
7368 Prefix
=> Prefix
(P
),
7369 Expressions
=> New_List
(
7371 (Etype
(First_Index
(Etype
(P
))),
7372 First
(Expressions
(N
))))));
7373 Analyze_And_Resolve
(N
, Typ
);
7377 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7378 -- function, then additional actuals must be passed.
7380 if Is_Build_In_Place_Function_Call
(P
) then
7381 Make_Build_In_Place_Call_In_Anonymous_Context
(P
);
7383 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
7384 -- containing build-in-place function calls whose returned object covers
7387 elsif Present
(Unqual_BIP_Iface_Function_Call
(P
)) then
7388 Make_Build_In_Place_Iface_Call_In_Anonymous_Context
(P
);
7391 -- Generate index and validity checks
7394 Dims_Checked
: Dimension_Set
(Dimensions
=>
7395 (if Is_Array_Type
(T
)
7396 then Number_Dimensions
(T
)
7398 -- Dims_Checked is used to avoid generating two checks (one in
7399 -- Generate_Index_Checks, one in Apply_Subscript_Validity_Checks)
7400 -- for the same index value in cases where the index check eliminates
7401 -- the need for the validity check. The Is_Array_Type test avoids
7402 -- cascading errors.
7405 Generate_Index_Checks
(N
, Checks_Generated
=> Dims_Checked
);
7407 if Validity_Checks_On
7408 and then (Validity_Check_Subscripts
7409 or else Wild_Reads_May_Have_Bad_Side_Effects
7410 or else Type_Requires_Subscript_Validity_Checks_For_Reads
7412 or else Is_Renamed_Variable_Name
(N
))
7414 if Validity_Check_Subscripts
then
7415 -- If we index into an array with an uninitialized variable
7416 -- and we generate an index check that passes at run time,
7417 -- passing that check does not ensure that the variable is
7418 -- valid (although it does in the common case where the
7419 -- object's subtype matches the index subtype).
7420 -- Consider an uninitialized variable with subtype 1 .. 10
7421 -- used to index into an array with bounds 1 .. 20 when the
7422 -- value of the uninitialized variable happens to be 15.
7423 -- The index check will succeed but the variable is invalid.
7424 -- If Validity_Check_Subscripts is True then we need to
7425 -- ensure validity, so we adjust Dims_Checked accordingly.
7426 Dims_Checked
.Elements
:= (others => False);
7428 elsif Is_Array_Type
(T
) then
7429 -- We are only adding extra validity checks here to
7430 -- deal with uninitialized variables (but this includes
7431 -- assigning one uninitialized variable to another). Other
7432 -- ways of producing invalid objects imply erroneousness, so
7433 -- the compiler can do whatever it wants for those cases.
7434 -- If an index type has the Default_Value aspect specified,
7435 -- then we don't have to worry about the possibility of an
7436 -- uninitialized variable, so no need for these extra
7440 Idx
: Node_Id
:= First_Index
(T
);
7442 for No_Check_Needed
of Dims_Checked
.Elements
loop
7443 No_Check_Needed
:= No_Check_Needed
7444 or else Has_Aspect
(Etype
(Idx
), Aspect_Default_Value
);
7450 Apply_Subscript_Validity_Checks
7451 (N
, No_Check_Needed
=> Dims_Checked
);
7455 -- If selecting from an array with atomic components, and atomic sync
7456 -- is not suppressed for this array type, set atomic sync flag.
7458 if (Has_Atomic_Components
(T
)
7459 and then not Atomic_Synchronization_Disabled
(T
))
7460 or else (Is_Atomic
(Typ
)
7461 and then not Atomic_Synchronization_Disabled
(Typ
))
7462 or else (Is_Entity_Name
(P
)
7463 and then Has_Atomic_Components
(Entity
(P
))
7464 and then not Atomic_Synchronization_Disabled
(Entity
(P
)))
7466 Activate_Atomic_Synchronization
(N
);
7469 -- All done if the prefix is not a packed array implemented specially
7471 if not (Is_Packed
(Etype
(Prefix
(N
)))
7472 and then Present
(Packed_Array_Impl_Type
(Etype
(Prefix
(N
)))))
7477 -- For packed arrays that are not bit-packed (i.e. the case of an array
7478 -- with one or more index types with a non-contiguous enumeration type),
7479 -- we can always use the normal packed element get circuit.
7481 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
7482 Expand_Packed_Element_Reference
(N
);
7486 -- For a reference to a component of a bit packed array, we convert it
7487 -- to a reference to the corresponding Packed_Array_Impl_Type. We only
7488 -- want to do this for simple references, and not for:
7490 -- Left side of assignment, or prefix of left side of assignment, or
7491 -- prefix of the prefix, to handle packed arrays of packed arrays,
7492 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
7494 -- Renaming objects in renaming associations
7495 -- This case is handled when a use of the renamed variable occurs
7497 -- Actual parameters for a subprogram call
7498 -- This case is handled in Exp_Ch6.Expand_Actuals
7500 -- The second expression in a 'Read attribute reference
7502 -- The prefix of an address or bit or size attribute reference
7504 -- The following circuit detects these exceptions. Note that we need to
7505 -- deal with implicit dereferences when climbing up the parent chain,
7506 -- with the additional difficulty that the type of parents may have yet
7507 -- to be resolved since prefixes are usually resolved first.
7510 Child
: Node_Id
:= N
;
7511 Parnt
: Node_Id
:= Parent
(N
);
7515 if Nkind
(Parnt
) = N_Unchecked_Expression
then
7518 elsif Nkind
(Parnt
) = N_Object_Renaming_Declaration
then
7521 elsif Nkind
(Parnt
) in N_Subprogram_Call
7522 or else (Nkind
(Parnt
) = N_Parameter_Association
7523 and then Nkind
(Parent
(Parnt
)) in N_Subprogram_Call
)
7527 elsif Nkind
(Parnt
) = N_Attribute_Reference
7528 and then Attribute_Name
(Parnt
) in Name_Address
7531 and then Prefix
(Parnt
) = Child
7535 elsif Nkind
(Parnt
) = N_Assignment_Statement
7536 and then Name
(Parnt
) = Child
7540 -- If the expression is an index of an indexed component, it must
7541 -- be expanded regardless of context.
7543 elsif Nkind
(Parnt
) = N_Indexed_Component
7544 and then Child
/= Prefix
(Parnt
)
7546 Expand_Packed_Element_Reference
(N
);
7549 elsif Nkind
(Parent
(Parnt
)) = N_Assignment_Statement
7550 and then Name
(Parent
(Parnt
)) = Parnt
7554 elsif Nkind
(Parnt
) = N_Attribute_Reference
7555 and then Attribute_Name
(Parnt
) = Name_Read
7556 and then Next
(First
(Expressions
(Parnt
))) = Child
7560 elsif Nkind
(Parnt
) = N_Indexed_Component
7561 and then Prefix
(Parnt
) = Child
7565 elsif Nkind
(Parnt
) = N_Selected_Component
7566 and then Prefix
(Parnt
) = Child
7567 and then not (Present
(Etype
(Selector_Name
(Parnt
)))
7569 Is_Access_Type
(Etype
(Selector_Name
(Parnt
))))
7573 -- If the parent is a dereference, either implicit or explicit,
7574 -- then the packed reference needs to be expanded.
7577 Expand_Packed_Element_Reference
(N
);
7581 -- Keep looking up tree for unchecked expression, or if we are the
7582 -- prefix of a possible assignment left side.
7585 Parnt
:= Parent
(Child
);
7588 end Expand_N_Indexed_Component
;
7590 ---------------------
7591 -- Expand_N_Not_In --
7592 ---------------------
7594 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
7595 -- can be done. This avoids needing to duplicate this expansion code.
7597 procedure Expand_N_Not_In
(N
: Node_Id
) is
7598 Loc
: constant Source_Ptr
:= Sloc
(N
);
7599 Typ
: constant Entity_Id
:= Etype
(N
);
7600 Cfs
: constant Boolean := Comes_From_Source
(N
);
7607 Left_Opnd
=> Left_Opnd
(N
),
7608 Right_Opnd
=> Right_Opnd
(N
))));
7610 -- If this is a set membership, preserve list of alternatives
7612 Set_Alternatives
(Right_Opnd
(N
), Alternatives
(Original_Node
(N
)));
7614 -- We want this to appear as coming from source if original does (see
7615 -- transformations in Expand_N_In).
7617 Set_Comes_From_Source
(N
, Cfs
);
7618 Set_Comes_From_Source
(Right_Opnd
(N
), Cfs
);
7620 -- Now analyze transformed node
7622 Analyze_And_Resolve
(N
, Typ
);
7623 end Expand_N_Not_In
;
7629 -- The only replacement required is for the case of a null of a type that
7630 -- is an access to protected subprogram, or a subtype thereof. We represent
7631 -- such access values as a record, and so we must replace the occurrence of
7632 -- null by the equivalent record (with a null address and a null pointer in
7633 -- it), so that the back end creates the proper value.
7635 procedure Expand_N_Null
(N
: Node_Id
) is
7636 Loc
: constant Source_Ptr
:= Sloc
(N
);
7637 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
7641 if Is_Access_Protected_Subprogram_Type
(Typ
) then
7643 Make_Aggregate
(Loc
,
7644 Expressions
=> New_List
(
7645 New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
),
7649 Analyze_And_Resolve
(N
, Equivalent_Type
(Typ
));
7651 -- For subsequent semantic analysis, the node must retain its type.
7652 -- Gigi in any case replaces this type by the corresponding record
7653 -- type before processing the node.
7659 when RE_Not_Available
=>
7663 ---------------------
7664 -- Expand_N_Op_Abs --
7665 ---------------------
7667 procedure Expand_N_Op_Abs
(N
: Node_Id
) is
7668 Loc
: constant Source_Ptr
:= Sloc
(N
);
7669 Expr
: constant Node_Id
:= Right_Opnd
(N
);
7670 Typ
: constant Entity_Id
:= Etype
(N
);
7673 Unary_Op_Validity_Checks
(N
);
7675 -- Check for MINIMIZED/ELIMINATED overflow mode
7677 if Minimized_Eliminated_Overflow_Check
(N
) then
7678 Apply_Arithmetic_Overflow_Check
(N
);
7682 -- Try to narrow the operation
7684 if Typ
= Universal_Integer
then
7685 Narrow_Large_Operation
(N
);
7687 if Nkind
(N
) /= N_Op_Abs
then
7692 -- Deal with software overflow checking
7694 if Is_Signed_Integer_Type
(Typ
)
7695 and then Do_Overflow_Check
(N
)
7697 -- The only case to worry about is when the argument is equal to the
7698 -- largest negative number, so what we do is to insert the check:
7700 -- [constraint_error when Expr = typ'Base'First]
7702 -- with the usual Duplicate_Subexpr use coding for expr
7705 Make_Raise_Constraint_Error
(Loc
,
7708 Left_Opnd
=> Duplicate_Subexpr
(Expr
),
7710 Make_Attribute_Reference
(Loc
,
7712 New_Occurrence_Of
(Base_Type
(Etype
(Expr
)), Loc
),
7713 Attribute_Name
=> Name_First
)),
7714 Reason
=> CE_Overflow_Check_Failed
));
7716 Set_Do_Overflow_Check
(N
, False);
7718 end Expand_N_Op_Abs
;
7720 ---------------------
7721 -- Expand_N_Op_Add --
7722 ---------------------
7724 procedure Expand_N_Op_Add
(N
: Node_Id
) is
7725 Typ
: constant Entity_Id
:= Etype
(N
);
7728 Binary_Op_Validity_Checks
(N
);
7730 -- Check for MINIMIZED/ELIMINATED overflow mode
7732 if Minimized_Eliminated_Overflow_Check
(N
) then
7733 Apply_Arithmetic_Overflow_Check
(N
);
7737 -- N + 0 = 0 + N = N for integer types
7739 if Is_Integer_Type
(Typ
) then
7740 if Compile_Time_Known_Value
(Right_Opnd
(N
))
7741 and then Expr_Value
(Right_Opnd
(N
)) = Uint_0
7743 Rewrite
(N
, Left_Opnd
(N
));
7746 elsif Compile_Time_Known_Value
(Left_Opnd
(N
))
7747 and then Expr_Value
(Left_Opnd
(N
)) = Uint_0
7749 Rewrite
(N
, Right_Opnd
(N
));
7754 -- Try to narrow the operation
7756 if Typ
= Universal_Integer
then
7757 Narrow_Large_Operation
(N
);
7759 if Nkind
(N
) /= N_Op_Add
then
7764 -- Arithmetic overflow checks for signed integer/fixed point types
7766 if Is_Signed_Integer_Type
(Typ
) or else Is_Fixed_Point_Type
(Typ
) then
7767 Apply_Arithmetic_Overflow_Check
(N
);
7771 -- Overflow checks for floating-point if -gnateF mode active
7773 Check_Float_Op_Overflow
(N
);
7775 Expand_Nonbinary_Modular_Op
(N
);
7776 end Expand_N_Op_Add
;
7778 ---------------------
7779 -- Expand_N_Op_And --
7780 ---------------------
7782 procedure Expand_N_Op_And
(N
: Node_Id
) is
7783 Typ
: constant Entity_Id
:= Etype
(N
);
7786 Binary_Op_Validity_Checks
(N
);
7788 if Is_Array_Type
(Etype
(N
)) then
7789 Expand_Boolean_Operator
(N
);
7791 elsif Is_Boolean_Type
(Etype
(N
)) then
7792 Adjust_Condition
(Left_Opnd
(N
));
7793 Adjust_Condition
(Right_Opnd
(N
));
7794 Set_Etype
(N
, Standard_Boolean
);
7795 Adjust_Result_Type
(N
, Typ
);
7797 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
7798 Expand_Intrinsic_Call
(N
, Entity
(N
));
7801 Expand_Nonbinary_Modular_Op
(N
);
7802 end Expand_N_Op_And
;
7804 ------------------------
7805 -- Expand_N_Op_Concat --
7806 ------------------------
7808 procedure Expand_N_Op_Concat
(N
: Node_Id
) is
7810 -- List of operands to be concatenated
7813 -- Node which is to be replaced by the result of concatenating the nodes
7814 -- in the list Opnds.
7817 -- Ensure validity of both operands
7819 Binary_Op_Validity_Checks
(N
);
7821 -- If we are the left operand of a concatenation higher up the tree,
7822 -- then do nothing for now, since we want to deal with a series of
7823 -- concatenations as a unit.
7825 if Nkind
(Parent
(N
)) = N_Op_Concat
7826 and then N
= Left_Opnd
(Parent
(N
))
7831 -- We get here with a concatenation whose left operand may be a
7832 -- concatenation itself with a consistent type. We need to process
7833 -- these concatenation operands from left to right, which means
7834 -- from the deepest node in the tree to the highest node.
7837 while Nkind
(Left_Opnd
(Cnode
)) = N_Op_Concat
loop
7838 Cnode
:= Left_Opnd
(Cnode
);
7841 -- Now Cnode is the deepest concatenation, and its parents are the
7842 -- concatenation nodes above, so now we process bottom up, doing the
7845 -- The outer loop runs more than once if more than one concatenation
7846 -- type is involved.
7849 Opnds
:= New_List
(Left_Opnd
(Cnode
), Right_Opnd
(Cnode
));
7850 Set_Parent
(Opnds
, N
);
7852 -- The inner loop gathers concatenation operands
7854 Inner
: while Cnode
/= N
7855 and then Base_Type
(Etype
(Cnode
)) =
7856 Base_Type
(Etype
(Parent
(Cnode
)))
7858 Cnode
:= Parent
(Cnode
);
7859 Append
(Right_Opnd
(Cnode
), Opnds
);
7862 -- Note: The following code is a temporary workaround for N731-034
7863 -- and N829-028 and will be kept until the general issue of internal
7864 -- symbol serialization is addressed. The workaround is kept under a
7865 -- debug switch to avoid permiating into the general case.
7867 -- Wrap the node to concatenate into an expression actions node to
7868 -- keep it nicely packaged. This is useful in the case of an assert
7869 -- pragma with a concatenation where we want to be able to delete
7870 -- the concatenation and all its expansion stuff.
7872 if Debug_Flag_Dot_H
then
7874 Cnod
: constant Node_Id
:= New_Copy_Tree
(Cnode
);
7875 Typ
: constant Entity_Id
:= Base_Type
(Etype
(Cnode
));
7878 -- Note: use Rewrite rather than Replace here, so that for
7879 -- example Why_Not_Static can find the original concatenation
7883 Make_Expression_With_Actions
(Sloc
(Cnode
),
7884 Actions
=> New_List
(Make_Null_Statement
(Sloc
(Cnode
))),
7885 Expression
=> Cnod
));
7887 Expand_Concatenate
(Cnod
, Opnds
);
7888 Analyze_And_Resolve
(Cnode
, Typ
);
7894 Expand_Concatenate
(Cnode
, Opnds
);
7897 exit Outer
when Cnode
= N
;
7898 Cnode
:= Parent
(Cnode
);
7900 end Expand_N_Op_Concat
;
7902 ------------------------
7903 -- Expand_N_Op_Divide --
7904 ------------------------
7906 procedure Expand_N_Op_Divide
(N
: Node_Id
) is
7907 Loc
: constant Source_Ptr
:= Sloc
(N
);
7908 Lopnd
: constant Node_Id
:= Left_Opnd
(N
);
7909 Ropnd
: constant Node_Id
:= Right_Opnd
(N
);
7910 Ltyp
: constant Entity_Id
:= Etype
(Lopnd
);
7911 Rtyp
: constant Entity_Id
:= Etype
(Ropnd
);
7912 Typ
: Entity_Id
:= Etype
(N
);
7913 Rknow
: constant Boolean := Is_Integer_Type
(Typ
)
7915 Compile_Time_Known_Value
(Ropnd
);
7919 Binary_Op_Validity_Checks
(N
);
7921 -- Check for MINIMIZED/ELIMINATED overflow mode
7923 if Minimized_Eliminated_Overflow_Check
(N
) then
7924 Apply_Arithmetic_Overflow_Check
(N
);
7928 -- Otherwise proceed with expansion of division
7931 Rval
:= Expr_Value
(Ropnd
);
7934 -- N / 1 = N for integer types
7936 if Rknow
and then Rval
= Uint_1
then
7941 -- Try to narrow the operation
7943 if Typ
= Universal_Integer
then
7944 Narrow_Large_Operation
(N
);
7946 if Nkind
(N
) /= N_Op_Divide
then
7951 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
7952 -- Is_Power_Of_2_For_Shift is set means that we know that our left
7953 -- operand is an unsigned integer, as required for this to work.
7955 if Nkind
(Ropnd
) = N_Op_Expon
7956 and then Is_Power_Of_2_For_Shift
(Ropnd
)
7958 -- We cannot do this transformation in configurable run time mode if we
7959 -- have 64-bit integers and long shifts are not available.
7961 and then (Esize
(Ltyp
) <= 32 or else Support_Long_Shifts_On_Target
)
7964 Make_Op_Shift_Right
(Loc
,
7967 Convert_To
(Standard_Natural
, Right_Opnd
(Ropnd
))));
7968 Analyze_And_Resolve
(N
, Typ
);
7972 -- Do required fixup of universal fixed operation
7974 if Typ
= Universal_Fixed
then
7975 Fixup_Universal_Fixed_Operation
(N
);
7979 -- Divisions with fixed-point results
7981 if Is_Fixed_Point_Type
(Typ
) then
7983 if Is_Integer_Type
(Rtyp
) then
7984 Expand_Divide_Fixed_By_Integer_Giving_Fixed
(N
);
7986 Expand_Divide_Fixed_By_Fixed_Giving_Fixed
(N
);
7989 -- Deal with divide-by-zero check if back end cannot handle them
7990 -- and the flag is set indicating that we need such a check. Note
7991 -- that we don't need to bother here with the case of mixed-mode
7992 -- (Right operand an integer type), since these will be rewritten
7993 -- with conversions to a divide with a fixed-point right operand.
7995 if Nkind
(N
) = N_Op_Divide
7996 and then Do_Division_Check
(N
)
7997 and then not Backend_Divide_Checks_On_Target
7998 and then not Is_Integer_Type
(Rtyp
)
8000 Set_Do_Division_Check
(N
, False);
8002 Make_Raise_Constraint_Error
(Loc
,
8005 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ropnd
),
8006 Right_Opnd
=> Make_Real_Literal
(Loc
, Ureal_0
)),
8007 Reason
=> CE_Divide_By_Zero
));
8010 -- Other cases of division of fixed-point operands
8012 elsif Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
) then
8013 if Is_Integer_Type
(Typ
) then
8014 Expand_Divide_Fixed_By_Fixed_Giving_Integer
(N
);
8016 pragma Assert
(Is_Floating_Point_Type
(Typ
));
8017 Expand_Divide_Fixed_By_Fixed_Giving_Float
(N
);
8020 -- Mixed-mode operations can appear in a non-static universal context,
8021 -- in which case the integer argument must be converted explicitly.
8023 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Rtyp
) then
8025 Convert_To
(Universal_Real
, Relocate_Node
(Ropnd
)));
8027 Analyze_And_Resolve
(Ropnd
, Universal_Real
);
8029 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Ltyp
) then
8031 Convert_To
(Universal_Real
, Relocate_Node
(Lopnd
)));
8033 Analyze_And_Resolve
(Lopnd
, Universal_Real
);
8035 -- Non-fixed point cases, do integer zero divide and overflow checks
8037 elsif Is_Integer_Type
(Typ
) then
8038 Apply_Divide_Checks
(N
);
8041 -- Overflow checks for floating-point if -gnateF mode active
8043 Check_Float_Op_Overflow
(N
);
8045 Expand_Nonbinary_Modular_Op
(N
);
8046 end Expand_N_Op_Divide
;
8048 --------------------
8049 -- Expand_N_Op_Eq --
8050 --------------------
8052 procedure Expand_N_Op_Eq
(N
: Node_Id
) is
8053 Loc
: constant Source_Ptr
:= Sloc
(N
);
8054 Typ
: constant Entity_Id
:= Etype
(N
);
8055 Lhs
: constant Node_Id
:= Left_Opnd
(N
);
8056 Rhs
: constant Node_Id
:= Right_Opnd
(N
);
8057 Bodies
: constant List_Id
:= New_List
;
8058 A_Typ
: constant Entity_Id
:= Etype
(Lhs
);
8060 procedure Build_Equality_Call
(Eq
: Entity_Id
);
8061 -- If a constructed equality exists for the type or for its parent,
8062 -- build and analyze call, adding conversions if the operation is
8065 function Find_Equality
(Prims
: Elist_Id
) return Entity_Id
;
8066 -- Find a primitive equality function within primitive operation list
8069 function Has_Unconstrained_UU_Component
(Typ
: Entity_Id
) return Boolean;
8070 -- Determines whether a type has a subcomponent of an unconstrained
8071 -- Unchecked_Union subtype. Typ is a record type.
8073 -------------------------
8074 -- Build_Equality_Call --
8075 -------------------------
8077 procedure Build_Equality_Call
(Eq
: Entity_Id
) is
8078 Op_Type
: constant Entity_Id
:= Etype
(First_Formal
(Eq
));
8079 L_Exp
: Node_Id
:= Relocate_Node
(Lhs
);
8080 R_Exp
: Node_Id
:= Relocate_Node
(Rhs
);
8083 -- Adjust operands if necessary to comparison type
8085 if Base_Type
(Op_Type
) /= Base_Type
(A_Typ
)
8086 and then not Is_Class_Wide_Type
(A_Typ
)
8088 L_Exp
:= OK_Convert_To
(Op_Type
, L_Exp
);
8089 R_Exp
:= OK_Convert_To
(Op_Type
, R_Exp
);
8092 -- If we have an Unchecked_Union, we need to add the inferred
8093 -- discriminant values as actuals in the function call. At this
8094 -- point, the expansion has determined that both operands have
8095 -- inferable discriminants.
8097 if Is_Unchecked_Union
(Op_Type
) then
8099 Lhs_Type
: constant Entity_Id
:= Etype
(L_Exp
);
8100 Rhs_Type
: constant Entity_Id
:= Etype
(R_Exp
);
8102 Lhs_Discr_Vals
: Elist_Id
;
8103 -- List of inferred discriminant values for left operand.
8105 Rhs_Discr_Vals
: Elist_Id
;
8106 -- List of inferred discriminant values for right operand.
8111 Lhs_Discr_Vals
:= New_Elmt_List
;
8112 Rhs_Discr_Vals
:= New_Elmt_List
;
8114 -- Per-object constrained selected components require special
8115 -- attention. If the enclosing scope of the component is an
8116 -- Unchecked_Union, we cannot reference its discriminants
8117 -- directly. This is why we use the extra parameters of the
8118 -- equality function of the enclosing Unchecked_Union.
8120 -- type UU_Type (Discr : Integer := 0) is
8123 -- pragma Unchecked_Union (UU_Type);
8125 -- 1. Unchecked_Union enclosing record:
8127 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
8129 -- Comp : UU_Type (Discr);
8131 -- end Enclosing_UU_Type;
8132 -- pragma Unchecked_Union (Enclosing_UU_Type);
8134 -- Obj1 : Enclosing_UU_Type;
8135 -- Obj2 : Enclosing_UU_Type (1);
8137 -- [. . .] Obj1 = Obj2 [. . .]
8141 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
8143 -- A and B are the formal parameters of the equality function
8144 -- of Enclosing_UU_Type. The function always has two extra
8145 -- formals to capture the inferred discriminant values for
8146 -- each discriminant of the type.
8148 -- 2. Non-Unchecked_Union enclosing record:
8151 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
8154 -- Comp : UU_Type (Discr);
8156 -- end Enclosing_Non_UU_Type;
8158 -- Obj1 : Enclosing_Non_UU_Type;
8159 -- Obj2 : Enclosing_Non_UU_Type (1);
8161 -- ... Obj1 = Obj2 ...
8165 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
8166 -- obj1.discr, obj2.discr)) then
8168 -- In this case we can directly reference the discriminants of
8169 -- the enclosing record.
8171 -- Process left operand of equality
8173 if Nkind
(Lhs
) = N_Selected_Component
8175 Has_Per_Object_Constraint
(Entity
(Selector_Name
(Lhs
)))
8177 -- If enclosing record is an Unchecked_Union, use formals
8178 -- corresponding to each discriminant. The name of the
8179 -- formal is that of the discriminant, with added suffix,
8180 -- see Exp_Ch3.Build_Record_Equality for details.
8182 if Is_Unchecked_Union
(Scope
(Entity
(Selector_Name
(Lhs
))))
8186 (Scope
(Entity
(Selector_Name
(Lhs
))));
8187 while Present
(Discr
) loop
8189 (Make_Identifier
(Loc
,
8190 Chars
=> New_External_Name
(Chars
(Discr
), 'A')),
8191 To
=> Lhs_Discr_Vals
);
8192 Next_Discriminant
(Discr
);
8195 -- If enclosing record is of a non-Unchecked_Union type, it
8196 -- is possible to reference its discriminants directly.
8199 Discr
:= First_Discriminant
(Lhs_Type
);
8200 while Present
(Discr
) loop
8202 (Make_Selected_Component
(Loc
,
8203 Prefix
=> Prefix
(Lhs
),
8206 (Get_Discriminant_Value
(Discr
,
8208 Stored_Constraint
(Lhs_Type
)))),
8209 To
=> Lhs_Discr_Vals
);
8210 Next_Discriminant
(Discr
);
8214 -- Otherwise operand is on object with a constrained type.
8215 -- Infer the discriminant values from the constraint.
8218 Discr
:= First_Discriminant
(Lhs_Type
);
8219 while Present
(Discr
) loop
8222 (Get_Discriminant_Value
(Discr
,
8224 Stored_Constraint
(Lhs_Type
))),
8225 To
=> Lhs_Discr_Vals
);
8226 Next_Discriminant
(Discr
);
8230 -- Similar processing for right operand of equality
8232 if Nkind
(Rhs
) = N_Selected_Component
8234 Has_Per_Object_Constraint
(Entity
(Selector_Name
(Rhs
)))
8236 if Is_Unchecked_Union
8237 (Scope
(Entity
(Selector_Name
(Rhs
))))
8241 (Scope
(Entity
(Selector_Name
(Rhs
))));
8242 while Present
(Discr
) loop
8244 (Make_Identifier
(Loc
,
8245 Chars
=> New_External_Name
(Chars
(Discr
), 'B')),
8246 To
=> Rhs_Discr_Vals
);
8247 Next_Discriminant
(Discr
);
8251 Discr
:= First_Discriminant
(Rhs_Type
);
8252 while Present
(Discr
) loop
8254 (Make_Selected_Component
(Loc
,
8255 Prefix
=> Prefix
(Rhs
),
8257 New_Copy
(Get_Discriminant_Value
8260 Stored_Constraint
(Rhs_Type
)))),
8261 To
=> Rhs_Discr_Vals
);
8262 Next_Discriminant
(Discr
);
8267 Discr
:= First_Discriminant
(Rhs_Type
);
8268 while Present
(Discr
) loop
8270 (New_Copy
(Get_Discriminant_Value
8273 Stored_Constraint
(Rhs_Type
))),
8274 To
=> Rhs_Discr_Vals
);
8275 Next_Discriminant
(Discr
);
8279 -- Now merge the list of discriminant values so that values
8280 -- of corresponding discriminants are adjacent.
8288 Params
:= New_List
(L_Exp
, R_Exp
);
8289 L_Elmt
:= First_Elmt
(Lhs_Discr_Vals
);
8290 R_Elmt
:= First_Elmt
(Rhs_Discr_Vals
);
8291 while Present
(L_Elmt
) loop
8292 Append_To
(Params
, Node
(L_Elmt
));
8293 Append_To
(Params
, Node
(R_Elmt
));
8299 Make_Function_Call
(Loc
,
8300 Name
=> New_Occurrence_Of
(Eq
, Loc
),
8301 Parameter_Associations
=> Params
));
8305 -- Normal case, not an unchecked union
8309 Make_Function_Call
(Loc
,
8310 Name
=> New_Occurrence_Of
(Eq
, Loc
),
8311 Parameter_Associations
=> New_List
(L_Exp
, R_Exp
)));
8314 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
8315 end Build_Equality_Call
;
8321 function Find_Equality
(Prims
: Elist_Id
) return Entity_Id
is
8322 function Find_Aliased_Equality
(Prim
: Entity_Id
) return Entity_Id
;
8323 -- Find an equality in a possible alias chain starting from primitive
8326 ---------------------------
8327 -- Find_Aliased_Equality --
8328 ---------------------------
8330 function Find_Aliased_Equality
(Prim
: Entity_Id
) return Entity_Id
is
8334 -- Inspect each candidate in the alias chain, checking whether it
8335 -- denotes an equality.
8338 while Present
(Candid
) loop
8339 if Is_User_Defined_Equality
(Candid
) then
8343 Candid
:= Alias
(Candid
);
8347 end Find_Aliased_Equality
;
8351 Eq_Prim
: Entity_Id
;
8352 Prim_Elmt
: Elmt_Id
;
8354 -- Start of processing for Find_Equality
8357 -- Assume that the tagged type lacks an equality
8361 -- Inspect the list of primitives looking for a suitable equality
8362 -- within a possible chain of aliases.
8364 Prim_Elmt
:= First_Elmt
(Prims
);
8365 while Present
(Prim_Elmt
) and then No
(Eq_Prim
) loop
8366 Eq_Prim
:= Find_Aliased_Equality
(Node
(Prim_Elmt
));
8368 Next_Elmt
(Prim_Elmt
);
8371 -- A tagged type should always have an equality
8373 pragma Assert
(Present
(Eq_Prim
));
8378 ------------------------------------
8379 -- Has_Unconstrained_UU_Component --
8380 ------------------------------------
8382 function Has_Unconstrained_UU_Component
8383 (Typ
: Entity_Id
) return Boolean
8385 function Unconstrained_UU_In_Component_Declaration
8386 (N
: Node_Id
) return Boolean;
8388 function Unconstrained_UU_In_Component_Items
8389 (L
: List_Id
) return Boolean;
8391 function Unconstrained_UU_In_Component_List
8392 (N
: Node_Id
) return Boolean;
8394 function Unconstrained_UU_In_Variant_Part
8395 (N
: Node_Id
) return Boolean;
8396 -- A family of routines that determine whether a particular construct
8397 -- of a record type definition contains a subcomponent of an
8398 -- unchecked union type whose nominal subtype is unconstrained.
8400 -- Individual routines correspond to the production rules of the Ada
8401 -- grammar, as described in the Ada RM (P).
8403 -----------------------------------------------
8404 -- Unconstrained_UU_In_Component_Declaration --
8405 -----------------------------------------------
8407 function Unconstrained_UU_In_Component_Declaration
8408 (N
: Node_Id
) return Boolean
8410 pragma Assert
(Nkind
(N
) = N_Component_Declaration
);
8412 Sindic
: constant Node_Id
:=
8413 Subtype_Indication
(Component_Definition
(N
));
8415 -- If the component declaration includes a subtype indication
8416 -- it is not an unchecked_union. Otherwise verify that it carries
8417 -- the Unchecked_Union flag and is either a record or a private
8418 -- type. A Record_Subtype declared elsewhere does not qualify,
8419 -- even if its parent type carries the flag.
8421 return Nkind
(Sindic
) in N_Expanded_Name | N_Identifier
8422 and then Is_Unchecked_Union
(Base_Type
(Etype
(Sindic
)))
8423 and then Ekind
(Entity
(Sindic
)) in
8424 E_Private_Type | E_Record_Type
;
8425 end Unconstrained_UU_In_Component_Declaration
;
8427 -----------------------------------------
8428 -- Unconstrained_UU_In_Component_Items --
8429 -----------------------------------------
8431 function Unconstrained_UU_In_Component_Items
8432 (L
: List_Id
) return Boolean
8434 N
: Node_Id
:= First
(L
);
8436 while Present
(N
) loop
8437 if Nkind
(N
) = N_Component_Declaration
8438 and then Unconstrained_UU_In_Component_Declaration
(N
)
8447 end Unconstrained_UU_In_Component_Items
;
8449 ----------------------------------------
8450 -- Unconstrained_UU_In_Component_List --
8451 ----------------------------------------
8453 function Unconstrained_UU_In_Component_List
8454 (N
: Node_Id
) return Boolean
8456 pragma Assert
(Nkind
(N
) = N_Component_List
);
8458 Optional_Variant_Part
: Node_Id
;
8460 if Unconstrained_UU_In_Component_Items
(Component_Items
(N
)) then
8464 Optional_Variant_Part
:= Variant_Part
(N
);
8467 Present
(Optional_Variant_Part
)
8469 Unconstrained_UU_In_Variant_Part
(Optional_Variant_Part
);
8470 end Unconstrained_UU_In_Component_List
;
8472 --------------------------------------
8473 -- Unconstrained_UU_In_Variant_Part --
8474 --------------------------------------
8476 function Unconstrained_UU_In_Variant_Part
8477 (N
: Node_Id
) return Boolean
8479 pragma Assert
(Nkind
(N
) = N_Variant_Part
);
8481 Variant
: Node_Id
:= First
(Variants
(N
));
8484 if Unconstrained_UU_In_Component_List
(Component_List
(Variant
))
8490 exit when No
(Variant
);
8494 end Unconstrained_UU_In_Variant_Part
;
8496 Typ_Def
: constant Node_Id
:=
8497 Type_Definition
(Declaration_Node
(Base_Type
(Typ
)));
8499 Optional_Component_List
: constant Node_Id
:=
8500 Component_List
(Typ_Def
);
8502 -- Start of processing for Has_Unconstrained_UU_Component
8505 return Present
(Optional_Component_List
)
8507 Unconstrained_UU_In_Component_List
(Optional_Component_List
);
8508 end Has_Unconstrained_UU_Component
;
8514 -- Start of processing for Expand_N_Op_Eq
8517 Binary_Op_Validity_Checks
(N
);
8519 -- Deal with private types
8521 Typl
:= Underlying_Type
(A_Typ
);
8523 -- It may happen in error situations that the underlying type is not
8524 -- set. The error will be detected later, here we just defend the
8531 -- Now get the implementation base type (note that plain Base_Type here
8532 -- might lead us back to the private type, which is not what we want!)
8534 Typl
:= Implementation_Base_Type
(Typl
);
8536 -- Equality between variant records results in a call to a routine
8537 -- that has conditional tests of the discriminant value(s), and hence
8538 -- violates the No_Implicit_Conditionals restriction.
8540 if Has_Variant_Part
(Typl
) then
8545 Check_Restriction
(Msg
, No_Implicit_Conditionals
, N
);
8549 ("\comparison of variant records tests discriminants", N
);
8555 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8556 -- means we no longer have a comparison operation, we are all done.
8558 if Minimized_Eliminated_Overflow_Check
(Left_Opnd
(N
)) then
8559 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8562 if Nkind
(N
) /= N_Op_Eq
then
8566 -- Boolean types (requiring handling of non-standard case)
8568 if Is_Boolean_Type
(Typl
) then
8569 Adjust_Condition
(Left_Opnd
(N
));
8570 Adjust_Condition
(Right_Opnd
(N
));
8571 Set_Etype
(N
, Standard_Boolean
);
8572 Adjust_Result_Type
(N
, Typ
);
8576 elsif Is_Array_Type
(Typl
) then
8578 -- If we are doing full validity checking, and it is possible for the
8579 -- array elements to be invalid then expand out array comparisons to
8580 -- make sure that we check the array elements.
8582 if Validity_Check_Operands
8583 and then not Is_Known_Valid
(Component_Type
(Typl
))
8586 Save_Force_Validity_Checks
: constant Boolean :=
8587 Force_Validity_Checks
;
8589 Force_Validity_Checks
:= True;
8591 Expand_Array_Equality
8593 Relocate_Node
(Lhs
),
8594 Relocate_Node
(Rhs
),
8597 Insert_Actions
(N
, Bodies
);
8598 Analyze_And_Resolve
(N
, Standard_Boolean
);
8599 Force_Validity_Checks
:= Save_Force_Validity_Checks
;
8602 -- Packed case where both operands are known aligned
8604 elsif Is_Bit_Packed_Array
(Typl
)
8605 and then not Is_Possibly_Unaligned_Object
(Lhs
)
8606 and then not Is_Possibly_Unaligned_Object
(Rhs
)
8608 Expand_Packed_Eq
(N
);
8610 -- Where the component type is elementary we can use a block bit
8611 -- comparison (if supported on the target) exception in the case
8612 -- of floating-point (negative zero issues require element by
8613 -- element comparison), and full access types (where we must be sure
8614 -- to load elements independently) and possibly unaligned arrays.
8616 elsif Is_Elementary_Type
(Component_Type
(Typl
))
8617 and then not Is_Floating_Point_Type
(Component_Type
(Typl
))
8618 and then not Is_Full_Access
(Component_Type
(Typl
))
8619 and then not Is_Possibly_Unaligned_Object
(Lhs
)
8620 and then not Is_Possibly_Unaligned_Slice
(Lhs
)
8621 and then not Is_Possibly_Unaligned_Object
(Rhs
)
8622 and then not Is_Possibly_Unaligned_Slice
(Rhs
)
8623 and then Support_Composite_Compare_On_Target
8627 -- For composite and floating-point cases, expand equality loop to
8628 -- make sure of using proper comparisons for tagged types, and
8629 -- correctly handling the floating-point case.
8633 Expand_Array_Equality
8635 Relocate_Node
(Lhs
),
8636 Relocate_Node
(Rhs
),
8639 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
8640 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
8645 elsif Is_Record_Type
(Typl
) then
8647 -- For tagged types, use the primitive "="
8649 if Is_Tagged_Type
(Typl
) then
8651 -- No need to do anything else compiling under restriction
8652 -- No_Dispatching_Calls. During the semantic analysis we
8653 -- already notified such violation.
8655 if Restriction_Active
(No_Dispatching_Calls
) then
8659 -- If this is an untagged private type completed with a derivation
8660 -- of an untagged private type whose full view is a tagged type,
8661 -- we use the primitive operations of the private type (since it
8662 -- does not have a full view, and also because its equality
8663 -- primitive may have been overridden in its untagged full view).
8665 if Inherits_From_Tagged_Full_View
(A_Typ
) then
8667 (Find_Equality
(Collect_Primitive_Operations
(A_Typ
)));
8669 -- Find the type's predefined equality or an overriding
8670 -- user-defined equality. The reason for not simply calling
8671 -- Find_Prim_Op here is that there may be a user-defined
8672 -- overloaded equality op that precedes the equality that we
8673 -- want, so we have to explicitly search (e.g., there could be
8674 -- an equality with two different parameter types).
8677 if Is_Class_Wide_Type
(Typl
) then
8678 Typl
:= Find_Specific_Type
(Typl
);
8682 (Find_Equality
(Primitive_Operations
(Typl
)));
8685 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
8686 -- predefined equality operator for a type which has a subcomponent
8687 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
8689 elsif Has_Unconstrained_UU_Component
(Typl
) then
8691 Make_Raise_Program_Error
(Loc
,
8692 Reason
=> PE_Unchecked_Union_Restriction
));
8694 -- Prevent Gigi from generating incorrect code by rewriting the
8695 -- equality as a standard False. (is this documented somewhere???)
8698 New_Occurrence_Of
(Standard_False
, Loc
));
8700 elsif Is_Unchecked_Union
(Typl
) then
8702 -- If we can infer the discriminants of the operands, we make a
8703 -- call to the TSS equality function.
8705 if Has_Inferable_Discriminants
(Lhs
)
8707 Has_Inferable_Discriminants
(Rhs
)
8710 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
8713 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
8714 -- the predefined equality operator for an Unchecked_Union type
8715 -- if either of the operands lack inferable discriminants.
8718 Make_Raise_Program_Error
(Loc
,
8719 Reason
=> PE_Unchecked_Union_Restriction
));
8721 -- Emit a warning on source equalities only, otherwise the
8722 -- message may appear out of place due to internal use. The
8723 -- warning is unconditional because it is required by the
8726 if Comes_From_Source
(N
) then
8728 ("Unchecked_Union discriminants cannot be determined??",
8731 ("\Program_Error will be raised for equality operation??",
8735 -- Prevent Gigi from generating incorrect code by rewriting
8736 -- the equality as a standard False (documented where???).
8739 New_Occurrence_Of
(Standard_False
, Loc
));
8742 -- If a type support function is present (for complex cases), use it
8744 elsif Present
(TSS
(Root_Type
(Typl
), TSS_Composite_Equality
)) then
8746 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
8748 -- When comparing two Bounded_Strings, use the primitive equality of
8749 -- the root Super_String type.
8751 elsif Is_Bounded_String
(Typl
) then
8754 (Collect_Primitive_Operations
(Root_Type
(Typl
))));
8756 -- Otherwise expand the component by component equality. Note that
8757 -- we never use block-bit comparisons for records, because of the
8758 -- problems with gaps. The back end will often be able to recombine
8759 -- the separate comparisons that we generate here.
8762 Remove_Side_Effects
(Lhs
);
8763 Remove_Side_Effects
(Rhs
);
8764 Rewrite
(N
, Expand_Record_Equality
(N
, Typl
, Lhs
, Rhs
));
8766 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
8769 -- If unnesting, handle elementary types whose Equivalent_Types are
8770 -- records because there may be padding or undefined fields.
8772 elsif Unnest_Subprogram_Mode
8773 and then Ekind
(Typl
) in E_Class_Wide_Type
8774 | E_Class_Wide_Subtype
8775 | E_Access_Subprogram_Type
8776 | E_Access_Protected_Subprogram_Type
8777 | E_Anonymous_Access_Protected_Subprogram_Type
8779 and then Present
(Equivalent_Type
(Typl
))
8780 and then Is_Record_Type
(Equivalent_Type
(Typl
))
8782 Typl
:= Equivalent_Type
(Typl
);
8783 Remove_Side_Effects
(Lhs
);
8784 Remove_Side_Effects
(Rhs
);
8786 Expand_Record_Equality
(N
, Typl
,
8787 Unchecked_Convert_To
(Typl
, Lhs
),
8788 Unchecked_Convert_To
(Typl
, Rhs
)));
8790 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
8793 -- Test if result is known at compile time
8795 Rewrite_Comparison
(N
);
8797 -- Try to narrow the operation
8799 if Typl
= Universal_Integer
and then Nkind
(N
) = N_Op_Eq
then
8800 Narrow_Large_Operation
(N
);
8803 -- Special optimization of length comparison
8805 Optimize_Length_Comparison
(N
);
8807 -- One more special case: if we have a comparison of X'Result = expr
8808 -- in floating-point, then if not already there, change expr to be
8809 -- f'Machine (expr) to eliminate surprise from extra precision.
8811 if Is_Floating_Point_Type
(Typl
)
8812 and then Is_Attribute_Result
(Original_Node
(Lhs
))
8814 -- Stick in the Typ'Machine call if not already there
8816 if Nkind
(Rhs
) /= N_Attribute_Reference
8817 or else Attribute_Name
(Rhs
) /= Name_Machine
8820 Make_Attribute_Reference
(Loc
,
8821 Prefix
=> New_Occurrence_Of
(Typl
, Loc
),
8822 Attribute_Name
=> Name_Machine
,
8823 Expressions
=> New_List
(Relocate_Node
(Rhs
))));
8824 Analyze_And_Resolve
(Rhs
, Typl
);
8829 -----------------------
8830 -- Expand_N_Op_Expon --
8831 -----------------------
8833 procedure Expand_N_Op_Expon
(N
: Node_Id
) is
8834 Loc
: constant Source_Ptr
:= Sloc
(N
);
8835 Ovflo
: constant Boolean := Do_Overflow_Check
(N
);
8836 Typ
: constant Entity_Id
:= Etype
(N
);
8837 Rtyp
: constant Entity_Id
:= Root_Type
(Typ
);
8841 function Wrap_MA
(Exp
: Node_Id
) return Node_Id
;
8842 -- Given an expression Exp, if the root type is Float or Long_Float,
8843 -- then wrap the expression in a call of Bastyp'Machine, to stop any
8844 -- extra precision. This is done to ensure that X**A = X**B when A is
8845 -- a static constant and B is a variable with the same value. For any
8846 -- other type, the node Exp is returned unchanged.
8852 function Wrap_MA
(Exp
: Node_Id
) return Node_Id
is
8853 Loc
: constant Source_Ptr
:= Sloc
(Exp
);
8856 if Rtyp
= Standard_Float
or else Rtyp
= Standard_Long_Float
then
8858 Make_Attribute_Reference
(Loc
,
8859 Attribute_Name
=> Name_Machine
,
8860 Prefix
=> New_Occurrence_Of
(Bastyp
, Loc
),
8861 Expressions
=> New_List
(Relocate_Node
(Exp
)));
8879 -- Start of processing for Expand_N_Op_Expon
8882 Binary_Op_Validity_Checks
(N
);
8884 -- CodePeer wants to see the unexpanded N_Op_Expon node
8886 if CodePeer_Mode
then
8890 -- Relocation of left and right operands must be done after performing
8891 -- the validity checks since the generation of validation checks may
8892 -- remove side effects.
8894 Base
:= Relocate_Node
(Left_Opnd
(N
));
8895 Bastyp
:= Etype
(Base
);
8896 Exp
:= Relocate_Node
(Right_Opnd
(N
));
8897 Exptyp
:= Etype
(Exp
);
8899 -- If either operand is of a private type, then we have the use of an
8900 -- intrinsic operator, and we get rid of the privateness, by using root
8901 -- types of underlying types for the actual operation. Otherwise the
8902 -- private types will cause trouble if we expand multiplications or
8903 -- shifts etc. We also do this transformation if the result type is
8904 -- different from the base type.
8906 if Is_Private_Type
(Etype
(Base
))
8907 or else Is_Private_Type
(Typ
)
8908 or else Is_Private_Type
(Exptyp
)
8909 or else Rtyp
/= Root_Type
(Bastyp
)
8912 Bt
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Bastyp
));
8913 Et
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Exptyp
));
8916 Unchecked_Convert_To
(Typ
,
8918 Left_Opnd
=> Unchecked_Convert_To
(Bt
, Base
),
8919 Right_Opnd
=> Unchecked_Convert_To
(Et
, Exp
))));
8920 Analyze_And_Resolve
(N
, Typ
);
8925 -- Check for MINIMIZED/ELIMINATED overflow mode
8927 if Minimized_Eliminated_Overflow_Check
(N
) then
8928 Apply_Arithmetic_Overflow_Check
(N
);
8932 -- Test for case of known right argument where we can replace the
8933 -- exponentiation by an equivalent expression using multiplication.
8935 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
8936 -- configurable run-time mode, we may not have the exponentiation
8937 -- routine available, and we don't want the legality of the program
8938 -- to depend on how clever the compiler is in knowing values.
8940 if CRT_Safe_Compile_Time_Known_Value
(Exp
) then
8941 Expv
:= Expr_Value
(Exp
);
8943 -- We only fold small non-negative exponents. You might think we
8944 -- could fold small negative exponents for the real case, but we
8945 -- can't because we are required to raise Constraint_Error for
8946 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
8947 -- See ACVC test C4A012B, and it is not worth generating the test.
8949 -- For small negative exponents, we return the reciprocal of
8950 -- the folding of the exponentiation for the opposite (positive)
8951 -- exponent, as required by Ada RM 4.5.6(11/3).
8953 if abs Expv
<= 4 then
8955 -- X ** 0 = 1 (or 1.0)
8959 -- Call Remove_Side_Effects to ensure that any side effects
8960 -- in the ignored left operand (in particular function calls
8961 -- to user defined functions) are properly executed.
8963 Remove_Side_Effects
(Base
);
8965 if Ekind
(Typ
) in Integer_Kind
then
8966 Xnode
:= Make_Integer_Literal
(Loc
, Intval
=> 1);
8968 Xnode
:= Make_Real_Literal
(Loc
, Ureal_1
);
8981 Make_Op_Multiply
(Loc
,
8982 Left_Opnd
=> Duplicate_Subexpr
(Base
),
8983 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)));
8985 -- X ** 3 = X * X * X
8990 Make_Op_Multiply
(Loc
,
8992 Make_Op_Multiply
(Loc
,
8993 Left_Opnd
=> Duplicate_Subexpr
(Base
),
8994 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)),
8995 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)));
9000 -- En : constant base'type := base * base;
9005 Temp
:= Make_Temporary
(Loc
, 'E', Base
);
9008 Make_Expression_With_Actions
(Loc
,
9009 Actions
=> New_List
(
9010 Make_Object_Declaration
(Loc
,
9011 Defining_Identifier
=> Temp
,
9012 Constant_Present
=> True,
9013 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
9016 Make_Op_Multiply
(Loc
,
9018 Duplicate_Subexpr
(Base
),
9020 Duplicate_Subexpr_No_Checks
(Base
))))),
9024 Make_Op_Multiply
(Loc
,
9025 Left_Opnd
=> New_Occurrence_Of
(Temp
, Loc
),
9026 Right_Opnd
=> New_Occurrence_Of
(Temp
, Loc
))));
9028 -- X ** N = 1.0 / X ** (-N)
9033 (Expv
= -1 or Expv
= -2 or Expv
= -3 or Expv
= -4);
9036 Make_Op_Divide
(Loc
,
9038 Make_Float_Literal
(Loc
,
9040 Significand
=> Uint_1
,
9041 Exponent
=> Uint_0
),
9044 Left_Opnd
=> Duplicate_Subexpr
(Base
),
9046 Make_Integer_Literal
(Loc
,
9051 Analyze_And_Resolve
(N
, Typ
);
9056 -- Optimize 2 ** expression to shift where possible
9058 -- Note: we used to check that Exptyp was an unsigned type. But that is
9059 -- an unnecessary check, since if Exp is negative, we have a run-time
9060 -- error that is either caught (so we get the right result) or we have
9061 -- suppressed the check, in which case the code is erroneous anyway.
9063 if Is_Integer_Type
(Rtyp
)
9065 -- The base value must be "safe compile-time known", and exactly 2
9067 and then Nkind
(Base
) = N_Integer_Literal
9068 and then CRT_Safe_Compile_Time_Known_Value
(Base
)
9069 and then Expr_Value
(Base
) = Uint_2
9071 -- This transformation is not applicable for a modular type with a
9072 -- nonbinary modulus because shifting makes no sense in that case.
9074 and then not Non_Binary_Modulus
(Typ
)
9076 -- Handle the cases where our parent is a division or multiplication
9077 -- specially. In these cases we can convert to using a shift at the
9078 -- parent level if we are not doing overflow checking, since it is
9079 -- too tricky to combine the overflow check at the parent level.
9082 and then Nkind
(Parent
(N
)) in N_Op_Divide | N_Op_Multiply
9085 P
: constant Node_Id
:= Parent
(N
);
9086 L
: constant Node_Id
:= Left_Opnd
(P
);
9087 R
: constant Node_Id
:= Right_Opnd
(P
);
9090 if (Nkind
(P
) = N_Op_Multiply
9092 ((Is_Integer_Type
(Etype
(L
)) and then R
= N
)
9094 (Is_Integer_Type
(Etype
(R
)) and then L
= N
))
9095 and then not Do_Overflow_Check
(P
))
9098 (Nkind
(P
) = N_Op_Divide
9099 and then Is_Integer_Type
(Etype
(L
))
9100 and then Is_Unsigned_Type
(Etype
(L
))
9102 and then not Do_Overflow_Check
(P
))
9104 Set_Is_Power_Of_2_For_Shift
(N
);
9109 -- Here we have 2 ** N on its own, so we can convert this into a
9113 -- Op_Shift_Left (generated below) has modular-shift semantics;
9114 -- therefore we might need to generate an overflow check here
9115 -- if the type is signed.
9117 if Is_Signed_Integer_Type
(Typ
) and then Ovflo
then
9123 MaxS
: constant Uint
:= Esize
(Rtyp
) - 2;
9124 -- Maximum shift count with no overflow
9126 Determine_Range
(Exp
, OK
, Lo
, Hi
, Assume_Valid
=> True);
9128 if not OK
or else Hi
> MaxS
then
9130 Make_Raise_Constraint_Error
(Loc
,
9133 Left_Opnd
=> Duplicate_Subexpr
(Exp
),
9134 Right_Opnd
=> Make_Integer_Literal
(Loc
, MaxS
)),
9135 Reason
=> CE_Overflow_Check_Failed
));
9140 -- Generate Shift_Left (1, Exp)
9143 Make_Op_Shift_Left
(Loc
,
9144 Left_Opnd
=> Make_Integer_Literal
(Loc
, Uint_1
),
9145 Right_Opnd
=> Exp
));
9147 Analyze_And_Resolve
(N
, Typ
);
9152 -- Fall through if exponentiation must be done using a runtime routine
9154 -- First deal with modular case
9156 if Is_Modular_Integer_Type
(Rtyp
) then
9158 -- Nonbinary modular case, we call the special exponentiation
9159 -- routine for the nonbinary case, converting the argument to
9160 -- Long_Long_Integer and passing the modulus value. Then the
9161 -- result is converted back to the base type.
9163 if Non_Binary_Modulus
(Rtyp
) then
9166 Make_Function_Call
(Loc
,
9168 New_Occurrence_Of
(RTE
(RE_Exp_Modular
), Loc
),
9169 Parameter_Associations
=> New_List
(
9170 Convert_To
(RTE
(RE_Unsigned
), Base
),
9171 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
)),
9174 -- Binary modular case, in this case, we call one of three routines,
9175 -- either the unsigned integer case, or the unsigned long long
9176 -- integer case, or the unsigned long long long integer case, with a
9177 -- final "and" operation to do the required mod.
9180 if Esize
(Rtyp
) <= Standard_Integer_Size
then
9181 Ent
:= RTE
(RE_Exp_Unsigned
);
9182 elsif Esize
(Rtyp
) <= Standard_Long_Long_Integer_Size
then
9183 Ent
:= RTE
(RE_Exp_Long_Long_Unsigned
);
9185 Ent
:= RTE
(RE_Exp_Long_Long_Long_Unsigned
);
9192 Make_Function_Call
(Loc
,
9193 Name
=> New_Occurrence_Of
(Ent
, Loc
),
9194 Parameter_Associations
=> New_List
(
9195 Convert_To
(Etype
(First_Formal
(Ent
)), Base
),
9198 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
) - 1))));
9202 -- Common exit point for modular type case
9204 Analyze_And_Resolve
(N
, Typ
);
9207 -- Signed integer cases, using either Integer, Long_Long_Integer or
9208 -- Long_Long_Long_Integer. It is not worth also having routines for
9209 -- Short_[Short_]Integer, since for most machines it would not help,
9210 -- and it would generate more code that might need certification when
9211 -- a certified run time is required.
9213 -- In the integer cases, we have two routines, one for when overflow
9214 -- checks are required, and one when they are not required, since there
9215 -- is a real gain in omitting checks on many machines.
9217 elsif Is_Signed_Integer_Type
(Rtyp
) then
9218 if Esize
(Rtyp
) <= Standard_Integer_Size
then
9219 Etyp
:= Standard_Integer
;
9222 Rent
:= RE_Exp_Integer
;
9224 Rent
:= RE_Exn_Integer
;
9227 elsif Esize
(Rtyp
) <= Standard_Long_Long_Integer_Size
then
9228 Etyp
:= Standard_Long_Long_Integer
;
9231 Rent
:= RE_Exp_Long_Long_Integer
;
9233 Rent
:= RE_Exn_Long_Long_Integer
;
9237 Etyp
:= Standard_Long_Long_Long_Integer
;
9240 Rent
:= RE_Exp_Long_Long_Long_Integer
;
9242 Rent
:= RE_Exn_Long_Long_Long_Integer
;
9246 -- Floating-point cases. We do not need separate routines for the
9247 -- overflow case here, since in the case of floating-point, we generate
9248 -- infinities anyway as a rule (either that or we automatically trap
9249 -- overflow), and if there is an infinity generated and a range check
9250 -- is required, the check will fail anyway.
9253 pragma Assert
(Is_Floating_Point_Type
(Rtyp
));
9255 -- Short_Float and Float are the same type for GNAT
9257 if Rtyp
= Standard_Short_Float
or else Rtyp
= Standard_Float
then
9258 Etyp
:= Standard_Float
;
9259 Rent
:= RE_Exn_Float
;
9261 elsif Rtyp
= Standard_Long_Float
then
9262 Etyp
:= Standard_Long_Float
;
9263 Rent
:= RE_Exn_Long_Float
;
9266 Etyp
:= Standard_Long_Long_Float
;
9267 Rent
:= RE_Exn_Long_Long_Float
;
9271 -- Common processing for integer cases and floating-point cases.
9272 -- If we are in the right type, we can call runtime routine directly
9275 and then not Is_Universal_Numeric_Type
(Rtyp
)
9279 Make_Function_Call
(Loc
,
9280 Name
=> New_Occurrence_Of
(RTE
(Rent
), Loc
),
9281 Parameter_Associations
=> New_List
(Base
, Exp
))));
9283 -- Otherwise we have to introduce conversions (conversions are also
9284 -- required in the universal cases, since the runtime routine is
9285 -- typed using one of the standard types).
9290 Make_Function_Call
(Loc
,
9291 Name
=> New_Occurrence_Of
(RTE
(Rent
), Loc
),
9292 Parameter_Associations
=> New_List
(
9293 Convert_To
(Etyp
, Base
),
9297 Analyze_And_Resolve
(N
, Typ
);
9301 when RE_Not_Available
=>
9303 end Expand_N_Op_Expon
;
9305 --------------------
9306 -- Expand_N_Op_Ge --
9307 --------------------
9309 procedure Expand_N_Op_Ge
(N
: Node_Id
) is
9310 Typ
: constant Entity_Id
:= Etype
(N
);
9311 Op1
: constant Node_Id
:= Left_Opnd
(N
);
9312 Op2
: constant Node_Id
:= Right_Opnd
(N
);
9313 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
9316 Binary_Op_Validity_Checks
(N
);
9318 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
9319 -- means we no longer have a comparison operation, we are all done.
9321 if Minimized_Eliminated_Overflow_Check
(Op1
) then
9322 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
9325 if Nkind
(N
) /= N_Op_Ge
then
9331 if Is_Array_Type
(Typ1
) then
9332 Expand_Array_Comparison
(N
);
9336 -- Deal with boolean operands
9338 if Is_Boolean_Type
(Typ1
) then
9339 Adjust_Condition
(Op1
);
9340 Adjust_Condition
(Op2
);
9341 Set_Etype
(N
, Standard_Boolean
);
9342 Adjust_Result_Type
(N
, Typ
);
9345 Rewrite_Comparison
(N
);
9347 -- Try to narrow the operation
9349 if Typ1
= Universal_Integer
and then Nkind
(N
) = N_Op_Ge
then
9350 Narrow_Large_Operation
(N
);
9353 Optimize_Length_Comparison
(N
);
9356 --------------------
9357 -- Expand_N_Op_Gt --
9358 --------------------
9360 procedure Expand_N_Op_Gt
(N
: Node_Id
) is
9361 Typ
: constant Entity_Id
:= Etype
(N
);
9362 Op1
: constant Node_Id
:= Left_Opnd
(N
);
9363 Op2
: constant Node_Id
:= Right_Opnd
(N
);
9364 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
9367 Binary_Op_Validity_Checks
(N
);
9369 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
9370 -- means we no longer have a comparison operation, we are all done.
9372 if Minimized_Eliminated_Overflow_Check
(Op1
) then
9373 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
9376 if Nkind
(N
) /= N_Op_Gt
then
9380 -- Deal with array type operands
9382 if Is_Array_Type
(Typ1
) then
9383 Expand_Array_Comparison
(N
);
9387 -- Deal with boolean type operands
9389 if Is_Boolean_Type
(Typ1
) then
9390 Adjust_Condition
(Op1
);
9391 Adjust_Condition
(Op2
);
9392 Set_Etype
(N
, Standard_Boolean
);
9393 Adjust_Result_Type
(N
, Typ
);
9396 Rewrite_Comparison
(N
);
9398 -- Try to narrow the operation
9400 if Typ1
= Universal_Integer
and then Nkind
(N
) = N_Op_Gt
then
9401 Narrow_Large_Operation
(N
);
9404 Optimize_Length_Comparison
(N
);
9407 --------------------
9408 -- Expand_N_Op_Le --
9409 --------------------
9411 procedure Expand_N_Op_Le
(N
: Node_Id
) is
9412 Typ
: constant Entity_Id
:= Etype
(N
);
9413 Op1
: constant Node_Id
:= Left_Opnd
(N
);
9414 Op2
: constant Node_Id
:= Right_Opnd
(N
);
9415 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
9418 Binary_Op_Validity_Checks
(N
);
9420 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
9421 -- means we no longer have a comparison operation, we are all done.
9423 if Minimized_Eliminated_Overflow_Check
(Op1
) then
9424 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
9427 if Nkind
(N
) /= N_Op_Le
then
9431 -- Deal with array type operands
9433 if Is_Array_Type
(Typ1
) then
9434 Expand_Array_Comparison
(N
);
9438 -- Deal with Boolean type operands
9440 if Is_Boolean_Type
(Typ1
) then
9441 Adjust_Condition
(Op1
);
9442 Adjust_Condition
(Op2
);
9443 Set_Etype
(N
, Standard_Boolean
);
9444 Adjust_Result_Type
(N
, Typ
);
9447 Rewrite_Comparison
(N
);
9449 -- Try to narrow the operation
9451 if Typ1
= Universal_Integer
and then Nkind
(N
) = N_Op_Le
then
9452 Narrow_Large_Operation
(N
);
9455 Optimize_Length_Comparison
(N
);
9458 --------------------
9459 -- Expand_N_Op_Lt --
9460 --------------------
9462 procedure Expand_N_Op_Lt
(N
: Node_Id
) is
9463 Typ
: constant Entity_Id
:= Etype
(N
);
9464 Op1
: constant Node_Id
:= Left_Opnd
(N
);
9465 Op2
: constant Node_Id
:= Right_Opnd
(N
);
9466 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
9469 Binary_Op_Validity_Checks
(N
);
9471 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
9472 -- means we no longer have a comparison operation, we are all done.
9474 if Minimized_Eliminated_Overflow_Check
(Op1
) then
9475 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
9478 if Nkind
(N
) /= N_Op_Lt
then
9482 -- Deal with array type operands
9484 if Is_Array_Type
(Typ1
) then
9485 Expand_Array_Comparison
(N
);
9489 -- Deal with Boolean type operands
9491 if Is_Boolean_Type
(Typ1
) then
9492 Adjust_Condition
(Op1
);
9493 Adjust_Condition
(Op2
);
9494 Set_Etype
(N
, Standard_Boolean
);
9495 Adjust_Result_Type
(N
, Typ
);
9498 Rewrite_Comparison
(N
);
9500 -- Try to narrow the operation
9502 if Typ1
= Universal_Integer
and then Nkind
(N
) = N_Op_Lt
then
9503 Narrow_Large_Operation
(N
);
9506 Optimize_Length_Comparison
(N
);
9509 -----------------------
9510 -- Expand_N_Op_Minus --
9511 -----------------------
9513 procedure Expand_N_Op_Minus
(N
: Node_Id
) is
9514 Loc
: constant Source_Ptr
:= Sloc
(N
);
9515 Typ
: constant Entity_Id
:= Etype
(N
);
9518 Unary_Op_Validity_Checks
(N
);
9520 -- Check for MINIMIZED/ELIMINATED overflow mode
9522 if Minimized_Eliminated_Overflow_Check
(N
) then
9523 Apply_Arithmetic_Overflow_Check
(N
);
9527 -- Try to narrow the operation
9529 if Typ
= Universal_Integer
then
9530 Narrow_Large_Operation
(N
);
9532 if Nkind
(N
) /= N_Op_Minus
then
9537 if not Backend_Overflow_Checks_On_Target
9538 and then Is_Signed_Integer_Type
(Typ
)
9539 and then Do_Overflow_Check
(N
)
9541 -- Software overflow checking expands -expr into (0 - expr)
9544 Make_Op_Subtract
(Loc
,
9545 Left_Opnd
=> Make_Integer_Literal
(Loc
, 0),
9546 Right_Opnd
=> Right_Opnd
(N
)));
9548 Analyze_And_Resolve
(N
, Typ
);
9551 Expand_Nonbinary_Modular_Op
(N
);
9552 end Expand_N_Op_Minus
;
9554 ---------------------
9555 -- Expand_N_Op_Mod --
9556 ---------------------
9558 procedure Expand_N_Op_Mod
(N
: Node_Id
) is
9559 Loc
: constant Source_Ptr
:= Sloc
(N
);
9560 Typ
: constant Entity_Id
:= Etype
(N
);
9561 DDC
: constant Boolean := Do_Division_Check
(N
);
9563 Is_Stoele_Mod
: constant Boolean :=
9564 Is_RTE
(Typ
, RE_Address
)
9565 and then Nkind
(Right_Opnd
(N
)) = N_Unchecked_Type_Conversion
9567 Is_RTE
(Etype
(Expression
(Right_Opnd
(N
))), RE_Storage_Offset
);
9568 -- True if this is the special mod operator of System.Storage_Elements
9581 pragma Warnings
(Off
, Lhi
);
9584 Binary_Op_Validity_Checks
(N
);
9586 -- Check for MINIMIZED/ELIMINATED overflow mode
9588 if Minimized_Eliminated_Overflow_Check
(N
) then
9589 Apply_Arithmetic_Overflow_Check
(N
);
9593 -- Try to narrow the operation
9595 if Typ
= Universal_Integer
then
9596 Narrow_Large_Operation
(N
);
9598 if Nkind
(N
) /= N_Op_Mod
then
9603 -- For the special mod operator of System.Storage_Elements, the checks
9604 -- are subsumed into the handling of the negative case below.
9606 if Is_Integer_Type
(Typ
) and then not Is_Stoele_Mod
then
9607 Apply_Divide_Checks
(N
);
9609 -- All done if we don't have a MOD any more, which can happen as a
9610 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
9612 if Nkind
(N
) /= N_Op_Mod
then
9617 -- Proceed with expansion of mod operator
9619 Left
:= Left_Opnd
(N
);
9620 Right
:= Right_Opnd
(N
);
9622 Determine_Range
(Right
, ROK
, Rlo
, Rhi
, Assume_Valid
=> True);
9623 Determine_Range
(Left
, LOK
, Llo
, Lhi
, Assume_Valid
=> True);
9625 -- Convert mod to rem if operands are both known to be non-negative, or
9626 -- both known to be non-positive (these are the cases in which rem and
9627 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
9628 -- likely that this will improve the quality of code, (the operation now
9629 -- corresponds to the hardware remainder), and it does not seem likely
9630 -- that it could be harmful. It also avoids some cases of the elaborate
9631 -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %).
9634 and then ((Llo
>= 0 and then Rlo
>= 0)
9636 (Lhi
<= 0 and then Rhi
<= 0))
9637 and then not Is_Stoele_Mod
9640 Make_Op_Rem
(Sloc
(N
),
9641 Left_Opnd
=> Left_Opnd
(N
),
9642 Right_Opnd
=> Right_Opnd
(N
)));
9644 -- Instead of reanalyzing the node we do the analysis manually. This
9645 -- avoids anomalies when the replacement is done in an instance and
9646 -- is epsilon more efficient.
9648 pragma Assert
(Entity
(N
) = Standard_Op_Rem
);
9650 Set_Do_Division_Check
(N
, DDC
);
9651 Expand_N_Op_Rem
(N
);
9655 -- Otherwise, normal mod processing
9658 -- Apply optimization x mod 1 = 0. We don't really need that with
9659 -- gcc, but it is useful with other back ends and is certainly
9662 if Is_Integer_Type
(Etype
(N
))
9663 and then Compile_Time_Known_Value
(Right
)
9664 and then Expr_Value
(Right
) = Uint_1
9666 -- Call Remove_Side_Effects to ensure that any side effects in
9667 -- the ignored left operand (in particular function calls to
9668 -- user defined functions) are properly executed.
9670 Remove_Side_Effects
(Left
);
9672 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
9673 Analyze_And_Resolve
(N
, Typ
);
9677 -- The negative case makes no sense since it is a case of a mod where
9678 -- the left argument is unsigned and the right argument is signed. In
9679 -- accordance with the (spirit of the) permission of RM 13.7.1(16),
9680 -- we raise CE, and also include the zero case here. Yes, the RM says
9681 -- PE, but this really is so obviously more like a constraint error.
9683 if Is_Stoele_Mod
and then (not ROK
or else Rlo
<= 0) then
9685 Make_Raise_Constraint_Error
(Loc
,
9689 Duplicate_Subexpr_No_Checks
(Expression
(Right
)),
9690 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
9691 Reason
=> CE_Overflow_Check_Failed
));
9695 -- If we still have a mod operator and we are in Modify_Tree_For_C
9696 -- mode, and we have a signed integer type, then here is where we do
9697 -- the rewrite in terms of Rem. Note this rewrite bypasses the need
9698 -- for the special handling of the annoying case of largest negative
9699 -- number mod minus one.
9701 if Nkind
(N
) = N_Op_Mod
9702 and then Is_Signed_Integer_Type
(Typ
)
9703 and then Modify_Tree_For_C
9705 -- In the general case, we expand A mod B as
9707 -- Tnn : constant typ := A rem B;
9709 -- (if (A >= 0) = (B >= 0) then Tnn
9710 -- elsif Tnn = 0 then 0
9713 -- The comparison can be written simply as A >= 0 if we know that
9714 -- B >= 0 which is a very common case.
9716 -- An important optimization is when B is known at compile time
9717 -- to be 2**K for some constant. In this case we can simply AND
9718 -- the left operand with the bit string 2**K-1 (i.e. K 1-bits)
9719 -- and that works for both the positive and negative cases.
9722 P2
: constant Nat
:= Power_Of_Two
(Right
);
9727 Unchecked_Convert_To
(Typ
,
9730 Unchecked_Convert_To
9731 (Corresponding_Unsigned_Type
(Typ
), Left
),
9733 Make_Integer_Literal
(Loc
, 2 ** P2
- 1))));
9734 Analyze_And_Resolve
(N
, Typ
);
9739 -- Here for the full rewrite
9742 Tnn
: constant Entity_Id
:= Make_Temporary
(Sloc
(N
), 'T', N
);
9748 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left
),
9749 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
9751 if not LOK
or else Rlo
< 0 then
9757 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Right
),
9758 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)));
9762 Make_Object_Declaration
(Loc
,
9763 Defining_Identifier
=> Tnn
,
9764 Constant_Present
=> True,
9765 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
9769 Right_Opnd
=> Right
)));
9772 Make_If_Expression
(Loc
,
9773 Expressions
=> New_List
(
9775 New_Occurrence_Of
(Tnn
, Loc
),
9776 Make_If_Expression
(Loc
,
9778 Expressions
=> New_List
(
9780 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
9781 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
9782 Make_Integer_Literal
(Loc
, 0),
9784 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
9786 Duplicate_Subexpr_No_Checks
(Right
)))))));
9788 Analyze_And_Resolve
(N
, Typ
);
9793 -- Deal with annoying case of largest negative number mod minus one.
9794 -- Gigi may not handle this case correctly, because on some targets,
9795 -- the mod value is computed using a divide instruction which gives
9796 -- an overflow trap for this case.
9798 -- It would be a bit more efficient to figure out which targets
9799 -- this is really needed for, but in practice it is reasonable
9800 -- to do the following special check in all cases, since it means
9801 -- we get a clearer message, and also the overhead is minimal given
9802 -- that division is expensive in any case.
9804 -- In fact the check is quite easy, if the right operand is -1, then
9805 -- the mod value is always 0, and we can just ignore the left operand
9806 -- completely in this case.
9808 -- This only applies if we still have a mod operator. Skip if we
9809 -- have already rewritten this (e.g. in the case of eliminated
9810 -- overflow checks which have driven us into bignum mode).
9812 if Nkind
(N
) = N_Op_Mod
then
9814 -- The operand type may be private (e.g. in the expansion of an
9815 -- intrinsic operation) so we must use the underlying type to get
9816 -- the bounds, and convert the literals explicitly.
9820 (Type_Low_Bound
(Base_Type
(Underlying_Type
(Etype
(Left
)))));
9822 if (not ROK
or else (Rlo
<= (-1) and then (-1) <= Rhi
))
9823 and then (not LOK
or else Llo
= LLB
)
9824 and then not CodePeer_Mode
9827 Make_If_Expression
(Loc
,
9828 Expressions
=> New_List
(
9830 Left_Opnd
=> Duplicate_Subexpr
(Right
),
9832 Unchecked_Convert_To
(Typ
,
9833 Make_Integer_Literal
(Loc
, -1))),
9834 Unchecked_Convert_To
(Typ
,
9835 Make_Integer_Literal
(Loc
, Uint_0
)),
9836 Relocate_Node
(N
))));
9838 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
9839 Analyze_And_Resolve
(N
, Typ
);
9843 end Expand_N_Op_Mod
;
9845 --------------------------
9846 -- Expand_N_Op_Multiply --
9847 --------------------------
9849 procedure Expand_N_Op_Multiply
(N
: Node_Id
) is
9850 Loc
: constant Source_Ptr
:= Sloc
(N
);
9851 Lop
: constant Node_Id
:= Left_Opnd
(N
);
9852 Rop
: constant Node_Id
:= Right_Opnd
(N
);
9854 Lp2
: constant Boolean :=
9855 Nkind
(Lop
) = N_Op_Expon
and then Is_Power_Of_2_For_Shift
(Lop
);
9856 Rp2
: constant Boolean :=
9857 Nkind
(Rop
) = N_Op_Expon
and then Is_Power_Of_2_For_Shift
(Rop
);
9859 Ltyp
: constant Entity_Id
:= Etype
(Lop
);
9860 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
9861 Typ
: Entity_Id
:= Etype
(N
);
9864 Binary_Op_Validity_Checks
(N
);
9866 -- Check for MINIMIZED/ELIMINATED overflow mode
9868 if Minimized_Eliminated_Overflow_Check
(N
) then
9869 Apply_Arithmetic_Overflow_Check
(N
);
9873 -- Special optimizations for integer types
9875 if Is_Integer_Type
(Typ
) then
9877 -- N * 0 = 0 for integer types
9879 if Compile_Time_Known_Value
(Rop
)
9880 and then Expr_Value
(Rop
) = Uint_0
9882 -- Call Remove_Side_Effects to ensure that any side effects in
9883 -- the ignored left operand (in particular function calls to
9884 -- user defined functions) are properly executed.
9886 Remove_Side_Effects
(Lop
);
9888 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
9889 Analyze_And_Resolve
(N
, Typ
);
9893 -- Similar handling for 0 * N = 0
9895 if Compile_Time_Known_Value
(Lop
)
9896 and then Expr_Value
(Lop
) = Uint_0
9898 Remove_Side_Effects
(Rop
);
9899 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
9900 Analyze_And_Resolve
(N
, Typ
);
9904 -- N * 1 = 1 * N = N for integer types
9906 -- This optimisation is not done if we are going to
9907 -- rewrite the product 1 * 2 ** N to a shift.
9909 if Compile_Time_Known_Value
(Rop
)
9910 and then Expr_Value
(Rop
) = Uint_1
9916 elsif Compile_Time_Known_Value
(Lop
)
9917 and then Expr_Value
(Lop
) = Uint_1
9925 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
9926 -- Is_Power_Of_2_For_Shift is set means that we know that our left
9927 -- operand is an integer, as required for this to work.
9932 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
9936 Left_Opnd
=> Make_Integer_Literal
(Loc
, 2),
9939 Left_Opnd
=> Right_Opnd
(Lop
),
9940 Right_Opnd
=> Right_Opnd
(Rop
))));
9941 Analyze_And_Resolve
(N
, Typ
);
9945 -- If the result is modular, perform the reduction of the result
9948 if Is_Modular_Integer_Type
(Typ
)
9949 and then not Non_Binary_Modulus
(Typ
)
9954 Make_Op_Shift_Left
(Loc
,
9957 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))),
9959 Make_Integer_Literal
(Loc
, Modulus
(Typ
) - 1)));
9963 Make_Op_Shift_Left
(Loc
,
9966 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))));
9969 Analyze_And_Resolve
(N
, Typ
);
9973 -- Same processing for the operands the other way round
9976 if Is_Modular_Integer_Type
(Typ
)
9977 and then not Non_Binary_Modulus
(Typ
)
9982 Make_Op_Shift_Left
(Loc
,
9985 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))),
9987 Make_Integer_Literal
(Loc
, Modulus
(Typ
) - 1)));
9991 Make_Op_Shift_Left
(Loc
,
9994 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))));
9997 Analyze_And_Resolve
(N
, Typ
);
10001 -- Try to narrow the operation
10003 if Typ
= Universal_Integer
then
10004 Narrow_Large_Operation
(N
);
10006 if Nkind
(N
) /= N_Op_Multiply
then
10011 -- Do required fixup of universal fixed operation
10013 if Typ
= Universal_Fixed
then
10014 Fixup_Universal_Fixed_Operation
(N
);
10018 -- Multiplications with fixed-point results
10020 if Is_Fixed_Point_Type
(Typ
) then
10022 -- Case of fixed * integer => fixed
10024 if Is_Integer_Type
(Rtyp
) then
10025 Expand_Multiply_Fixed_By_Integer_Giving_Fixed
(N
);
10027 -- Case of integer * fixed => fixed
10029 elsif Is_Integer_Type
(Ltyp
) then
10030 Expand_Multiply_Integer_By_Fixed_Giving_Fixed
(N
);
10032 -- Case of fixed * fixed => fixed
10035 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed
(N
);
10038 -- Other cases of multiplication of fixed-point operands
10040 elsif Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
) then
10041 if Is_Integer_Type
(Typ
) then
10042 Expand_Multiply_Fixed_By_Fixed_Giving_Integer
(N
);
10044 pragma Assert
(Is_Floating_Point_Type
(Typ
));
10045 Expand_Multiply_Fixed_By_Fixed_Giving_Float
(N
);
10048 -- Mixed-mode operations can appear in a non-static universal context,
10049 -- in which case the integer argument must be converted explicitly.
10051 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Rtyp
) then
10052 Rewrite
(Rop
, Convert_To
(Universal_Real
, Relocate_Node
(Rop
)));
10053 Analyze_And_Resolve
(Rop
, Universal_Real
);
10055 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Ltyp
) then
10056 Rewrite
(Lop
, Convert_To
(Universal_Real
, Relocate_Node
(Lop
)));
10057 Analyze_And_Resolve
(Lop
, Universal_Real
);
10059 -- Non-fixed point cases, check software overflow checking required
10061 elsif Is_Signed_Integer_Type
(Etype
(N
)) then
10062 Apply_Arithmetic_Overflow_Check
(N
);
10065 -- Overflow checks for floating-point if -gnateF mode active
10067 Check_Float_Op_Overflow
(N
);
10069 Expand_Nonbinary_Modular_Op
(N
);
10070 end Expand_N_Op_Multiply
;
10072 --------------------
10073 -- Expand_N_Op_Ne --
10074 --------------------
10076 procedure Expand_N_Op_Ne
(N
: Node_Id
) is
10077 Typ
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
10080 -- Case of elementary type with standard operator. But if unnesting,
10081 -- handle elementary types whose Equivalent_Types are records because
10082 -- there may be padding or undefined fields.
10084 if Is_Elementary_Type
(Typ
)
10085 and then Sloc
(Entity
(N
)) = Standard_Location
10086 and then not (Ekind
(Typ
) in E_Class_Wide_Type
10087 | E_Class_Wide_Subtype
10088 | E_Access_Subprogram_Type
10089 | E_Access_Protected_Subprogram_Type
10090 | E_Anonymous_Access_Protected_Subprogram_Type
10092 and then Present
(Equivalent_Type
(Typ
))
10093 and then Is_Record_Type
(Equivalent_Type
(Typ
)))
10095 Binary_Op_Validity_Checks
(N
);
10097 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
10098 -- means we no longer have a /= operation, we are all done.
10100 if Minimized_Eliminated_Overflow_Check
(Left_Opnd
(N
)) then
10101 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
10104 if Nkind
(N
) /= N_Op_Ne
then
10108 -- Boolean types (requiring handling of non-standard case)
10110 if Is_Boolean_Type
(Typ
) then
10111 Adjust_Condition
(Left_Opnd
(N
));
10112 Adjust_Condition
(Right_Opnd
(N
));
10113 Set_Etype
(N
, Standard_Boolean
);
10114 Adjust_Result_Type
(N
, Typ
);
10117 Rewrite_Comparison
(N
);
10119 -- Try to narrow the operation
10121 if Typ
= Universal_Integer
and then Nkind
(N
) = N_Op_Ne
then
10122 Narrow_Large_Operation
(N
);
10125 -- For all cases other than elementary types, we rewrite node as the
10126 -- negation of an equality operation, and reanalyze. The equality to be
10127 -- used is defined in the same scope and has the same signature. This
10128 -- signature must be set explicitly since in an instance it may not have
10129 -- the same visibility as in the generic unit. This avoids duplicating
10130 -- or factoring the complex code for record/array equality tests etc.
10132 -- This case is also used for the minimal expansion performed in
10137 Loc
: constant Source_Ptr
:= Sloc
(N
);
10139 Ne
: constant Entity_Id
:= Entity
(N
);
10142 Binary_Op_Validity_Checks
(N
);
10148 Left_Opnd
=> Left_Opnd
(N
),
10149 Right_Opnd
=> Right_Opnd
(N
)));
10151 if Scope
(Ne
) /= Standard_Standard
then
10152 Set_Entity
(Right_Opnd
(Neg
), Corresponding_Equality
(Ne
));
10155 -- For navigation purposes, we want to treat the inequality as an
10156 -- implicit reference to the corresponding equality. Preserve the
10157 -- Comes_From_ source flag to generate proper Xref entries.
10159 Preserve_Comes_From_Source
(Neg
, N
);
10160 Preserve_Comes_From_Source
(Right_Opnd
(Neg
), N
);
10162 Analyze_And_Resolve
(N
, Standard_Boolean
);
10166 -- No need for optimization in GNATprove mode, where we would rather see
10167 -- the original source expression.
10169 if not GNATprove_Mode
then
10170 Optimize_Length_Comparison
(N
);
10172 end Expand_N_Op_Ne
;
10174 ---------------------
10175 -- Expand_N_Op_Not --
10176 ---------------------
10178 -- If the argument is other than a Boolean array type, there is no special
10179 -- expansion required, except for dealing with validity checks, and non-
10180 -- standard boolean representations.
10182 -- For the packed array case, we call the special routine in Exp_Pakd,
10183 -- except that if the component size is greater than one, we use the
10184 -- standard routine generating a gruesome loop (it is so peculiar to have
10185 -- packed arrays with non-standard Boolean representations anyway, so it
10186 -- does not matter that we do not handle this case efficiently).
10188 -- For the unpacked array case (and for the special packed case where we
10189 -- have non standard Booleans, as discussed above), we generate and insert
10190 -- into the tree the following function definition:
10192 -- function Nnnn (A : arr) is
10195 -- for J in a'range loop
10196 -- B (J) := not A (J);
10201 -- or in the case of Transform_Function_Array:
10203 -- procedure Nnnn (A : arr; RESULT : out arr) is
10205 -- for J in a'range loop
10206 -- RESULT (J) := not A (J);
10210 -- Here arr is the actual subtype of the parameter (and hence always
10211 -- constrained). Then we replace the not with a call to this subprogram.
10213 procedure Expand_N_Op_Not
(N
: Node_Id
) is
10214 Loc
: constant Source_Ptr
:= Sloc
(N
);
10215 Typ
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
10224 Func_Name
: Entity_Id
;
10225 Loop_Statement
: Node_Id
;
10228 Unary_Op_Validity_Checks
(N
);
10230 -- For boolean operand, deal with non-standard booleans
10232 if Is_Boolean_Type
(Typ
) then
10233 Adjust_Condition
(Right_Opnd
(N
));
10234 Set_Etype
(N
, Standard_Boolean
);
10235 Adjust_Result_Type
(N
, Typ
);
10239 -- Only array types need any other processing
10241 if not Is_Array_Type
(Typ
) then
10245 -- Case of array operand. If bit packed with a component size of 1,
10246 -- handle it in Exp_Pakd if the operand is known to be aligned.
10248 if Is_Bit_Packed_Array
(Typ
)
10249 and then Component_Size
(Typ
) = 1
10250 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
10252 Expand_Packed_Not
(N
);
10256 -- Case of array operand which is not bit-packed. If the context is
10257 -- a safe assignment, call in-place operation, If context is a larger
10258 -- boolean expression in the context of a safe assignment, expansion is
10259 -- done by enclosing operation.
10261 Opnd
:= Relocate_Node
(Right_Opnd
(N
));
10262 Convert_To_Actual_Subtype
(Opnd
);
10263 Arr
:= Etype
(Opnd
);
10264 Ensure_Defined
(Arr
, N
);
10265 Silly_Boolean_Array_Not_Test
(N
, Arr
);
10267 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
10268 if Safe_In_Place_Array_Op
(Name
(Parent
(N
)), N
, Empty
) then
10269 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
10272 -- Special case the negation of a binary operation
10274 elsif Nkind
(Opnd
) in N_Op_And | N_Op_Or | N_Op_Xor
10275 and then Safe_In_Place_Array_Op
10276 (Name
(Parent
(N
)), Left_Opnd
(Opnd
), Right_Opnd
(Opnd
))
10278 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
10282 elsif Nkind
(Parent
(N
)) in N_Binary_Op
10283 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
10286 Op1
: constant Node_Id
:= Left_Opnd
(Parent
(N
));
10287 Op2
: constant Node_Id
:= Right_Opnd
(Parent
(N
));
10288 Lhs
: constant Node_Id
:= Name
(Parent
(Parent
(N
)));
10291 if Safe_In_Place_Array_Op
(Lhs
, Op1
, Op2
) then
10293 -- (not A) op (not B) can be reduced to a single call
10295 if N
= Op1
and then Nkind
(Op2
) = N_Op_Not
then
10298 elsif N
= Op2
and then Nkind
(Op1
) = N_Op_Not
then
10301 -- A xor (not B) can also be special-cased
10303 elsif N
= Op2
and then Nkind
(Parent
(N
)) = N_Op_Xor
then
10310 A
:= Make_Defining_Identifier
(Loc
, Name_uA
);
10312 if Transform_Function_Array
then
10313 B
:= Make_Defining_Identifier
(Loc
, Name_UP_RESULT
);
10315 B
:= Make_Defining_Identifier
(Loc
, Name_uB
);
10318 J
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
10321 Make_Indexed_Component
(Loc
,
10322 Prefix
=> New_Occurrence_Of
(A
, Loc
),
10323 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
10326 Make_Indexed_Component
(Loc
,
10327 Prefix
=> New_Occurrence_Of
(B
, Loc
),
10328 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
10331 Make_Implicit_Loop_Statement
(N
,
10332 Identifier
=> Empty
,
10334 Iteration_Scheme
=>
10335 Make_Iteration_Scheme
(Loc
,
10336 Loop_Parameter_Specification
=>
10337 Make_Loop_Parameter_Specification
(Loc
,
10338 Defining_Identifier
=> J
,
10339 Discrete_Subtype_Definition
=>
10340 Make_Attribute_Reference
(Loc
,
10341 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
10342 Attribute_Name
=> Name_Range
))),
10344 Statements
=> New_List
(
10345 Make_Assignment_Statement
(Loc
,
10347 Expression
=> Make_Op_Not
(Loc
, A_J
))));
10349 Func_Name
:= Make_Temporary
(Loc
, 'N');
10350 Set_Is_Inlined
(Func_Name
);
10352 if Transform_Function_Array
then
10354 Make_Subprogram_Body
(Loc
,
10356 Make_Procedure_Specification
(Loc
,
10357 Defining_Unit_Name
=> Func_Name
,
10358 Parameter_Specifications
=> New_List
(
10359 Make_Parameter_Specification
(Loc
,
10360 Defining_Identifier
=> A
,
10361 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)),
10362 Make_Parameter_Specification
(Loc
,
10363 Defining_Identifier
=> B
,
10364 Out_Present
=> True,
10365 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)))),
10367 Declarations
=> New_List
,
10369 Handled_Statement_Sequence
=>
10370 Make_Handled_Sequence_Of_Statements
(Loc
,
10371 Statements
=> New_List
(Loop_Statement
))));
10374 Temp_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
10383 Make_Object_Declaration
(Loc
,
10384 Defining_Identifier
=> Temp_Id
,
10385 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
10388 -- Proc_Call (Opnd, Temp);
10391 Make_Procedure_Call_Statement
(Loc
,
10392 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
10393 Parameter_Associations
=>
10394 New_List
(Opnd
, New_Occurrence_Of
(Temp_Id
, Loc
)));
10396 Insert_Actions
(Parent
(N
), New_List
(Decl
, Call
));
10397 Rewrite
(N
, New_Occurrence_Of
(Temp_Id
, Loc
));
10401 Make_Subprogram_Body
(Loc
,
10403 Make_Function_Specification
(Loc
,
10404 Defining_Unit_Name
=> Func_Name
,
10405 Parameter_Specifications
=> New_List
(
10406 Make_Parameter_Specification
(Loc
,
10407 Defining_Identifier
=> A
,
10408 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
))),
10409 Result_Definition
=> New_Occurrence_Of
(Typ
, Loc
)),
10411 Declarations
=> New_List
(
10412 Make_Object_Declaration
(Loc
,
10413 Defining_Identifier
=> B
,
10414 Object_Definition
=> New_Occurrence_Of
(Arr
, Loc
))),
10416 Handled_Statement_Sequence
=>
10417 Make_Handled_Sequence_Of_Statements
(Loc
,
10418 Statements
=> New_List
(
10420 Make_Simple_Return_Statement
(Loc
,
10421 Expression
=> Make_Identifier
(Loc
, Chars
(B
)))))));
10424 Make_Function_Call
(Loc
,
10425 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
10426 Parameter_Associations
=> New_List
(Opnd
)));
10429 Analyze_And_Resolve
(N
, Typ
);
10430 end Expand_N_Op_Not
;
10432 --------------------
10433 -- Expand_N_Op_Or --
10434 --------------------
10436 procedure Expand_N_Op_Or
(N
: Node_Id
) is
10437 Typ
: constant Entity_Id
:= Etype
(N
);
10440 Binary_Op_Validity_Checks
(N
);
10442 if Is_Array_Type
(Etype
(N
)) then
10443 Expand_Boolean_Operator
(N
);
10445 elsif Is_Boolean_Type
(Etype
(N
)) then
10446 Adjust_Condition
(Left_Opnd
(N
));
10447 Adjust_Condition
(Right_Opnd
(N
));
10448 Set_Etype
(N
, Standard_Boolean
);
10449 Adjust_Result_Type
(N
, Typ
);
10451 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
10452 Expand_Intrinsic_Call
(N
, Entity
(N
));
10455 Expand_Nonbinary_Modular_Op
(N
);
10456 end Expand_N_Op_Or
;
10458 ----------------------
10459 -- Expand_N_Op_Plus --
10460 ----------------------
10462 procedure Expand_N_Op_Plus
(N
: Node_Id
) is
10463 Typ
: constant Entity_Id
:= Etype
(N
);
10466 Unary_Op_Validity_Checks
(N
);
10468 -- Check for MINIMIZED/ELIMINATED overflow mode
10470 if Minimized_Eliminated_Overflow_Check
(N
) then
10471 Apply_Arithmetic_Overflow_Check
(N
);
10475 -- Try to narrow the operation
10477 if Typ
= Universal_Integer
then
10478 Narrow_Large_Operation
(N
);
10480 end Expand_N_Op_Plus
;
10482 ---------------------
10483 -- Expand_N_Op_Rem --
10484 ---------------------
10486 procedure Expand_N_Op_Rem
(N
: Node_Id
) is
10487 Loc
: constant Source_Ptr
:= Sloc
(N
);
10488 Typ
: constant Entity_Id
:= Etype
(N
);
10499 -- Set if corresponding operand can be negative
10502 Binary_Op_Validity_Checks
(N
);
10504 -- Check for MINIMIZED/ELIMINATED overflow mode
10506 if Minimized_Eliminated_Overflow_Check
(N
) then
10507 Apply_Arithmetic_Overflow_Check
(N
);
10511 -- Try to narrow the operation
10513 if Typ
= Universal_Integer
then
10514 Narrow_Large_Operation
(N
);
10516 if Nkind
(N
) /= N_Op_Rem
then
10521 if Is_Integer_Type
(Etype
(N
)) then
10522 Apply_Divide_Checks
(N
);
10524 -- All done if we don't have a REM any more, which can happen as a
10525 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
10527 if Nkind
(N
) /= N_Op_Rem
then
10532 -- Proceed with expansion of REM
10534 Left
:= Left_Opnd
(N
);
10535 Right
:= Right_Opnd
(N
);
10537 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
10538 -- but it is useful with other back ends, and is certainly harmless.
10540 if Is_Integer_Type
(Etype
(N
))
10541 and then Compile_Time_Known_Value
(Right
)
10542 and then Expr_Value
(Right
) = Uint_1
10544 -- Call Remove_Side_Effects to ensure that any side effects in the
10545 -- ignored left operand (in particular function calls to user defined
10546 -- functions) are properly executed.
10548 Remove_Side_Effects
(Left
);
10550 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
10551 Analyze_And_Resolve
(N
, Typ
);
10555 -- Deal with annoying case of largest negative number remainder minus
10556 -- one. Gigi may not handle this case correctly, because on some
10557 -- targets, the mod value is computed using a divide instruction
10558 -- which gives an overflow trap for this case.
10560 -- It would be a bit more efficient to figure out which targets this
10561 -- is really needed for, but in practice it is reasonable to do the
10562 -- following special check in all cases, since it means we get a clearer
10563 -- message, and also the overhead is minimal given that division is
10564 -- expensive in any case.
10566 -- In fact the check is quite easy, if the right operand is -1, then
10567 -- the remainder is always 0, and we can just ignore the left operand
10568 -- completely in this case.
10570 Determine_Range
(Right
, OK
, Lo
, Hi
, Assume_Valid
=> True);
10571 Lneg
:= not OK
or else Lo
< 0;
10573 Determine_Range
(Left
, OK
, Lo
, Hi
, Assume_Valid
=> True);
10574 Rneg
:= not OK
or else Lo
< 0;
10576 -- We won't mess with trying to find out if the left operand can really
10577 -- be the largest negative number (that's a pain in the case of private
10578 -- types and this is really marginal). We will just assume that we need
10579 -- the test if the left operand can be negative at all.
10582 and then not CodePeer_Mode
10585 Make_If_Expression
(Loc
,
10586 Expressions
=> New_List
(
10588 Left_Opnd
=> Duplicate_Subexpr
(Right
),
10590 Unchecked_Convert_To
(Typ
, Make_Integer_Literal
(Loc
, -1))),
10592 Unchecked_Convert_To
(Typ
,
10593 Make_Integer_Literal
(Loc
, Uint_0
)),
10595 Relocate_Node
(N
))));
10597 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
10598 Analyze_And_Resolve
(N
, Typ
);
10600 end Expand_N_Op_Rem
;
10602 -----------------------------
10603 -- Expand_N_Op_Rotate_Left --
10604 -----------------------------
10606 procedure Expand_N_Op_Rotate_Left
(N
: Node_Id
) is
10608 Binary_Op_Validity_Checks
(N
);
10610 -- If we are in Modify_Tree_For_C mode, there is no rotate left in C,
10611 -- so we rewrite in terms of logical shifts
10613 -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits)
10615 -- where Bits is the shift count mod Esize (the mod operation here
10616 -- deals with ludicrous large shift counts, which are apparently OK).
10618 if Modify_Tree_For_C
then
10620 Loc
: constant Source_Ptr
:= Sloc
(N
);
10621 Rtp
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
10622 Typ
: constant Entity_Id
:= Etype
(N
);
10625 -- Sem_Intr should prevent getting there with a non binary modulus
10627 pragma Assert
(not Non_Binary_Modulus
(Typ
));
10629 Rewrite
(Right_Opnd
(N
),
10631 Left_Opnd
=> Relocate_Node
(Right_Opnd
(N
)),
10632 Right_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
))));
10634 Analyze_And_Resolve
(Right_Opnd
(N
), Rtp
);
10639 Make_Op_Shift_Left
(Loc
,
10640 Left_Opnd
=> Left_Opnd
(N
),
10641 Right_Opnd
=> Right_Opnd
(N
)),
10644 Make_Op_Shift_Right
(Loc
,
10645 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left_Opnd
(N
)),
10647 Make_Op_Subtract
(Loc
,
10648 Left_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
)),
10650 Duplicate_Subexpr_No_Checks
(Right_Opnd
(N
))))));
10652 Analyze_And_Resolve
(N
, Typ
);
10655 end Expand_N_Op_Rotate_Left
;
10657 ------------------------------
10658 -- Expand_N_Op_Rotate_Right --
10659 ------------------------------
10661 procedure Expand_N_Op_Rotate_Right
(N
: Node_Id
) is
10663 Binary_Op_Validity_Checks
(N
);
10665 -- If we are in Modify_Tree_For_C mode, there is no rotate right in C,
10666 -- so we rewrite in terms of logical shifts
10668 -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits)
10670 -- where Bits is the shift count mod Esize (the mod operation here
10671 -- deals with ludicrous large shift counts, which are apparently OK).
10673 if Modify_Tree_For_C
then
10675 Loc
: constant Source_Ptr
:= Sloc
(N
);
10676 Rtp
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
10677 Typ
: constant Entity_Id
:= Etype
(N
);
10680 -- Sem_Intr should prevent getting there with a non binary modulus
10682 pragma Assert
(not Non_Binary_Modulus
(Typ
));
10684 Rewrite
(Right_Opnd
(N
),
10686 Left_Opnd
=> Relocate_Node
(Right_Opnd
(N
)),
10687 Right_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
))));
10689 Analyze_And_Resolve
(Right_Opnd
(N
), Rtp
);
10694 Make_Op_Shift_Right
(Loc
,
10695 Left_Opnd
=> Left_Opnd
(N
),
10696 Right_Opnd
=> Right_Opnd
(N
)),
10699 Make_Op_Shift_Left
(Loc
,
10700 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left_Opnd
(N
)),
10702 Make_Op_Subtract
(Loc
,
10703 Left_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
)),
10705 Duplicate_Subexpr_No_Checks
(Right_Opnd
(N
))))));
10707 Analyze_And_Resolve
(N
, Typ
);
10710 end Expand_N_Op_Rotate_Right
;
10712 ----------------------------
10713 -- Expand_N_Op_Shift_Left --
10714 ----------------------------
10716 -- Note: nothing in this routine depends on left as opposed to right shifts
10717 -- so we share the routine for expanding shift right operations.
10719 procedure Expand_N_Op_Shift_Left
(N
: Node_Id
) is
10721 Binary_Op_Validity_Checks
(N
);
10723 -- If we are in Modify_Tree_For_C mode, then ensure that the right
10724 -- operand is not greater than the word size (since that would not
10725 -- be defined properly by the corresponding C shift operator).
10727 if Modify_Tree_For_C
then
10729 Right
: constant Node_Id
:= Right_Opnd
(N
);
10730 Loc
: constant Source_Ptr
:= Sloc
(Right
);
10731 Typ
: constant Entity_Id
:= Etype
(N
);
10732 Siz
: constant Uint
:= Esize
(Typ
);
10739 -- Sem_Intr should prevent getting there with a non binary modulus
10741 pragma Assert
(not Non_Binary_Modulus
(Typ
));
10743 if Compile_Time_Known_Value
(Right
) then
10744 if Expr_Value
(Right
) >= Siz
then
10745 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
10746 Analyze_And_Resolve
(N
, Typ
);
10749 -- Not compile time known, find range
10752 Determine_Range
(Right
, OK
, Lo
, Hi
, Assume_Valid
=> True);
10754 -- Nothing to do if known to be OK range, otherwise expand
10756 if not OK
or else Hi
>= Siz
then
10758 -- Prevent recursion on copy of shift node
10760 Orig
:= Relocate_Node
(N
);
10761 Set_Analyzed
(Orig
);
10763 -- Now do the rewrite
10766 Make_If_Expression
(Loc
,
10767 Expressions
=> New_List
(
10769 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Right
),
10770 Right_Opnd
=> Make_Integer_Literal
(Loc
, Siz
)),
10771 Make_Integer_Literal
(Loc
, 0),
10773 Analyze_And_Resolve
(N
, Typ
);
10778 end Expand_N_Op_Shift_Left
;
10780 -----------------------------
10781 -- Expand_N_Op_Shift_Right --
10782 -----------------------------
10784 procedure Expand_N_Op_Shift_Right
(N
: Node_Id
) is
10786 -- Share shift left circuit
10788 Expand_N_Op_Shift_Left
(N
);
10789 end Expand_N_Op_Shift_Right
;
10791 ----------------------------------------
10792 -- Expand_N_Op_Shift_Right_Arithmetic --
10793 ----------------------------------------
10795 procedure Expand_N_Op_Shift_Right_Arithmetic
(N
: Node_Id
) is
10797 Binary_Op_Validity_Checks
(N
);
10799 -- If we are in Modify_Tree_For_C mode, there is no shift right
10800 -- arithmetic in C, so we rewrite in terms of logical shifts for
10801 -- modular integers, and keep the Shift_Right intrinsic for signed
10802 -- integers: even though doing a shift on a signed integer is not
10803 -- fully guaranteed by the C standard, this is what C compilers
10804 -- implement in practice.
10805 -- Consider also taking advantage of this for modular integers by first
10806 -- performing an unchecked conversion of the modular integer to a signed
10807 -- integer of the same sign, and then convert back.
10809 -- Shift_Right (Num, Bits) or
10811 -- then not (Shift_Right (Mask, bits))
10814 -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1)
10816 -- Note: the above works fine for shift counts greater than or equal
10817 -- to the word size, since in this case (not (Shift_Right (Mask, bits)))
10818 -- generates all 1'bits.
10820 if Modify_Tree_For_C
and then Is_Modular_Integer_Type
(Etype
(N
)) then
10822 Loc
: constant Source_Ptr
:= Sloc
(N
);
10823 Typ
: constant Entity_Id
:= Etype
(N
);
10824 Sign
: constant Uint
:= 2 ** (Esize
(Typ
) - 1);
10825 Mask
: constant Uint
:= (2 ** Esize
(Typ
)) - 1;
10826 Left
: constant Node_Id
:= Left_Opnd
(N
);
10827 Right
: constant Node_Id
:= Right_Opnd
(N
);
10831 -- Sem_Intr should prevent getting there with a non binary modulus
10833 pragma Assert
(not Non_Binary_Modulus
(Typ
));
10835 -- Here if not (Shift_Right (Mask, bits)) can be computed at
10836 -- compile time as a single constant.
10838 if Compile_Time_Known_Value
(Right
) then
10840 Val
: constant Uint
:= Expr_Value
(Right
);
10843 if Val
>= Esize
(Typ
) then
10844 Maskx
:= Make_Integer_Literal
(Loc
, Mask
);
10848 Make_Integer_Literal
(Loc
,
10849 Intval
=> Mask
- (Mask
/ (2 ** Expr_Value
(Right
))));
10857 Make_Op_Shift_Right
(Loc
,
10858 Left_Opnd
=> Make_Integer_Literal
(Loc
, Mask
),
10859 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Right
)));
10862 -- Now do the rewrite
10867 Make_Op_Shift_Right
(Loc
,
10869 Right_Opnd
=> Right
),
10871 Make_If_Expression
(Loc
,
10872 Expressions
=> New_List
(
10874 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left
),
10875 Right_Opnd
=> Make_Integer_Literal
(Loc
, Sign
)),
10877 Make_Integer_Literal
(Loc
, 0)))));
10878 Analyze_And_Resolve
(N
, Typ
);
10881 end Expand_N_Op_Shift_Right_Arithmetic
;
10883 --------------------------
10884 -- Expand_N_Op_Subtract --
10885 --------------------------
10887 procedure Expand_N_Op_Subtract
(N
: Node_Id
) is
10888 Typ
: constant Entity_Id
:= Etype
(N
);
10891 Binary_Op_Validity_Checks
(N
);
10893 -- Check for MINIMIZED/ELIMINATED overflow mode
10895 if Minimized_Eliminated_Overflow_Check
(N
) then
10896 Apply_Arithmetic_Overflow_Check
(N
);
10900 -- Try to narrow the operation
10902 if Typ
= Universal_Integer
then
10903 Narrow_Large_Operation
(N
);
10905 if Nkind
(N
) /= N_Op_Subtract
then
10910 -- N - 0 = N for integer types
10912 if Is_Integer_Type
(Typ
)
10913 and then Compile_Time_Known_Value
(Right_Opnd
(N
))
10914 and then Expr_Value
(Right_Opnd
(N
)) = 0
10916 Rewrite
(N
, Left_Opnd
(N
));
10920 -- Arithmetic overflow checks for signed integer/fixed point types
10922 if Is_Signed_Integer_Type
(Typ
) or else Is_Fixed_Point_Type
(Typ
) then
10923 Apply_Arithmetic_Overflow_Check
(N
);
10926 -- Overflow checks for floating-point if -gnateF mode active
10928 Check_Float_Op_Overflow
(N
);
10930 Expand_Nonbinary_Modular_Op
(N
);
10931 end Expand_N_Op_Subtract
;
10933 ---------------------
10934 -- Expand_N_Op_Xor --
10935 ---------------------
10937 procedure Expand_N_Op_Xor
(N
: Node_Id
) is
10938 Typ
: constant Entity_Id
:= Etype
(N
);
10941 Binary_Op_Validity_Checks
(N
);
10943 if Is_Array_Type
(Etype
(N
)) then
10944 Expand_Boolean_Operator
(N
);
10946 elsif Is_Boolean_Type
(Etype
(N
)) then
10947 Adjust_Condition
(Left_Opnd
(N
));
10948 Adjust_Condition
(Right_Opnd
(N
));
10949 Set_Etype
(N
, Standard_Boolean
);
10950 Adjust_Result_Type
(N
, Typ
);
10952 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
10953 Expand_Intrinsic_Call
(N
, Entity
(N
));
10956 Expand_Nonbinary_Modular_Op
(N
);
10957 end Expand_N_Op_Xor
;
10959 ----------------------
10960 -- Expand_N_Or_Else --
10961 ----------------------
10963 procedure Expand_N_Or_Else
(N
: Node_Id
)
10964 renames Expand_Short_Circuit_Operator
;
10966 -----------------------------------
10967 -- Expand_N_Qualified_Expression --
10968 -----------------------------------
10970 procedure Expand_N_Qualified_Expression
(N
: Node_Id
) is
10971 Operand
: constant Node_Id
:= Expression
(N
);
10972 Target_Type
: constant Entity_Id
:= Entity
(Subtype_Mark
(N
));
10975 -- Do validity check if validity checking operands
10977 if Validity_Checks_On
and Validity_Check_Operands
then
10978 Ensure_Valid
(Operand
);
10981 Freeze_Before
(Operand
, Target_Type
);
10983 -- Apply possible constraint check
10985 Apply_Constraint_Check
(Operand
, Target_Type
, No_Sliding
=> True);
10987 -- Apply possible predicate check
10989 Apply_Predicate_Check
(Operand
, Target_Type
);
10991 if Do_Range_Check
(Operand
) then
10992 Generate_Range_Check
(Operand
, Target_Type
, CE_Range_Check_Failed
);
10994 end Expand_N_Qualified_Expression
;
10996 ------------------------------------
10997 -- Expand_N_Quantified_Expression --
10998 ------------------------------------
11002 -- for all X in range => Cond
11007 -- for X in range loop
11008 -- if not Cond then
11014 -- Similarly, an existentially quantified expression:
11016 -- for some X in range => Cond
11021 -- for X in range loop
11028 -- In both cases, the iteration may be over a container in which case it is
11029 -- given by an iterator specification, not a loop parameter specification.
11031 procedure Expand_N_Quantified_Expression
(N
: Node_Id
) is
11032 Actions
: constant List_Id
:= New_List
;
11033 For_All
: constant Boolean := All_Present
(N
);
11034 Iter_Spec
: constant Node_Id
:= Iterator_Specification
(N
);
11035 Loc
: constant Source_Ptr
:= Sloc
(N
);
11036 Loop_Spec
: constant Node_Id
:= Loop_Parameter_Specification
(N
);
11044 -- Ensure that the bound variable as well as the type of Name of the
11045 -- Iter_Spec if present are properly frozen. We must do this before
11046 -- expansion because the expression is about to be converted into a
11047 -- loop, and resulting freeze nodes may end up in the wrong place in the
11050 if Present
(Iter_Spec
) then
11051 Var
:= Defining_Identifier
(Iter_Spec
);
11053 Var
:= Defining_Identifier
(Loop_Spec
);
11057 P
: Node_Id
:= Parent
(N
);
11059 while Nkind
(P
) in N_Subexpr
loop
11063 if Present
(Iter_Spec
) then
11064 Freeze_Before
(P
, Etype
(Name
(Iter_Spec
)));
11067 Freeze_Before
(P
, Etype
(Var
));
11070 -- Create the declaration of the flag which tracks the status of the
11071 -- quantified expression. Generate:
11073 -- Flag : Boolean := (True | False);
11075 Flag
:= Make_Temporary
(Loc
, 'T', N
);
11077 Append_To
(Actions
,
11078 Make_Object_Declaration
(Loc
,
11079 Defining_Identifier
=> Flag
,
11080 Object_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
),
11082 New_Occurrence_Of
(Boolean_Literals
(For_All
), Loc
)));
11084 -- Construct the circuitry which tracks the status of the quantified
11085 -- expression. Generate:
11087 -- if [not] Cond then
11088 -- Flag := (False | True);
11092 Cond
:= Relocate_Node
(Condition
(N
));
11095 Cond
:= Make_Op_Not
(Loc
, Cond
);
11098 Stmts
:= New_List
(
11099 Make_Implicit_If_Statement
(N
,
11101 Then_Statements
=> New_List
(
11102 Make_Assignment_Statement
(Loc
,
11103 Name
=> New_Occurrence_Of
(Flag
, Loc
),
11105 New_Occurrence_Of
(Boolean_Literals
(not For_All
), Loc
)),
11106 Make_Exit_Statement
(Loc
))));
11108 -- Build the loop equivalent of the quantified expression
11110 if Present
(Iter_Spec
) then
11112 Make_Iteration_Scheme
(Loc
,
11113 Iterator_Specification
=> Iter_Spec
);
11116 Make_Iteration_Scheme
(Loc
,
11117 Loop_Parameter_Specification
=> Loop_Spec
);
11120 Append_To
(Actions
,
11121 Make_Loop_Statement
(Loc
,
11122 Iteration_Scheme
=> Scheme
,
11123 Statements
=> Stmts
,
11124 End_Label
=> Empty
));
11126 -- Transform the quantified expression
11129 Make_Expression_With_Actions
(Loc
,
11130 Expression
=> New_Occurrence_Of
(Flag
, Loc
),
11131 Actions
=> Actions
));
11132 Analyze_And_Resolve
(N
, Standard_Boolean
);
11133 end Expand_N_Quantified_Expression
;
11135 ---------------------------------
11136 -- Expand_N_Selected_Component --
11137 ---------------------------------
11139 procedure Expand_N_Selected_Component
(N
: Node_Id
) is
11140 Loc
: constant Source_Ptr
:= Sloc
(N
);
11141 Par
: constant Node_Id
:= Parent
(N
);
11142 P
: constant Node_Id
:= Prefix
(N
);
11143 S
: constant Node_Id
:= Selector_Name
(N
);
11144 Ptyp
: constant Entity_Id
:= Underlying_Type
(Etype
(P
));
11150 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean;
11151 -- Gigi needs a temporary for prefixes that depend on a discriminant,
11152 -- unless the context of an assignment can provide size information.
11153 -- Don't we have a general routine that does this???
11155 function Is_Subtype_Declaration
return Boolean;
11156 -- The replacement of a discriminant reference by its value is required
11157 -- if this is part of the initialization of an temporary generated by a
11158 -- change of representation. This shows up as the construction of a
11159 -- discriminant constraint for a subtype declared at the same point as
11160 -- the entity in the prefix of the selected component. We recognize this
11161 -- case when the context of the reference is:
11162 -- subtype ST is T(Obj.D);
11163 -- where the entity for Obj comes from source, and ST has the same sloc.
11165 -----------------------
11166 -- In_Left_Hand_Side --
11167 -----------------------
11169 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean is
11171 return (Nkind
(Parent
(Comp
)) = N_Assignment_Statement
11172 and then Comp
= Name
(Parent
(Comp
)))
11173 or else (Present
(Parent
(Comp
))
11174 and then Nkind
(Parent
(Comp
)) in N_Subexpr
11175 and then In_Left_Hand_Side
(Parent
(Comp
)));
11176 end In_Left_Hand_Side
;
11178 -----------------------------
11179 -- Is_Subtype_Declaration --
11180 -----------------------------
11182 function Is_Subtype_Declaration
return Boolean is
11183 Par
: constant Node_Id
:= Parent
(N
);
11186 Nkind
(Par
) = N_Index_Or_Discriminant_Constraint
11187 and then Nkind
(Parent
(Parent
(Par
))) = N_Subtype_Declaration
11188 and then Comes_From_Source
(Entity
(Prefix
(N
)))
11189 and then Sloc
(Par
) = Sloc
(Entity
(Prefix
(N
)));
11190 end Is_Subtype_Declaration
;
11192 -- Start of processing for Expand_N_Selected_Component
11195 -- Deal with discriminant check required
11197 if Do_Discriminant_Check
(N
) then
11198 if Present
(Discriminant_Checking_Func
11199 (Original_Record_Component
(Entity
(S
))))
11201 -- Present the discriminant checking function to the backend, so
11202 -- that it can inline the call to the function.
11205 (Discriminant_Checking_Func
11206 (Original_Record_Component
(Entity
(S
))),
11209 -- Now reset the flag and generate the call
11211 Set_Do_Discriminant_Check
(N
, False);
11212 Generate_Discriminant_Check
(N
);
11214 -- In the case of Unchecked_Union, no discriminant checking is
11215 -- actually performed.
11218 if not Is_Unchecked_Union
11219 (Implementation_Base_Type
(Etype
(Prefix
(N
))))
11220 and then not Is_Predefined_Unit
(Get_Source_Unit
(N
))
11223 ("sorry - unable to generate discriminant check for" &
11224 " reference to variant component &",
11225 Selector_Name
(N
));
11228 Set_Do_Discriminant_Check
(N
, False);
11232 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
11233 -- function, then additional actuals must be passed.
11235 if Is_Build_In_Place_Function_Call
(P
) then
11236 Make_Build_In_Place_Call_In_Anonymous_Context
(P
);
11238 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
11239 -- containing build-in-place function calls whose returned object covers
11240 -- interface types.
11242 elsif Present
(Unqual_BIP_Iface_Function_Call
(P
)) then
11243 Make_Build_In_Place_Iface_Call_In_Anonymous_Context
(P
);
11246 -- Gigi cannot handle unchecked conversions that are the prefix of a
11247 -- selected component with discriminants. This must be checked during
11248 -- expansion, because during analysis the type of the selector is not
11249 -- known at the point the prefix is analyzed. If the conversion is the
11250 -- target of an assignment, then we cannot force the evaluation.
11252 if Nkind
(Prefix
(N
)) = N_Unchecked_Type_Conversion
11253 and then Has_Discriminants
(Etype
(N
))
11254 and then not In_Left_Hand_Side
(N
)
11256 Force_Evaluation
(Prefix
(N
));
11259 -- Remaining processing applies only if selector is a discriminant
11261 if Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
then
11263 -- If the selector is a discriminant of a constrained record type,
11264 -- we may be able to rewrite the expression with the actual value
11265 -- of the discriminant, a useful optimization in some cases.
11267 if Is_Record_Type
(Ptyp
)
11268 and then Has_Discriminants
(Ptyp
)
11269 and then Is_Constrained
(Ptyp
)
11271 -- Do this optimization for discrete types only, and not for
11272 -- access types (access discriminants get us into trouble).
11274 if not Is_Discrete_Type
(Etype
(N
)) then
11277 -- Don't do this on the left-hand side of an assignment statement.
11278 -- Normally one would think that references like this would not
11279 -- occur, but they do in generated code, and mean that we really
11280 -- do want to assign the discriminant.
11282 elsif Nkind
(Par
) = N_Assignment_Statement
11283 and then Name
(Par
) = N
11287 -- Don't do this optimization for the prefix of an attribute or
11288 -- the name of an object renaming declaration since these are
11289 -- contexts where we do not want the value anyway.
11291 elsif (Nkind
(Par
) = N_Attribute_Reference
11292 and then Prefix
(Par
) = N
)
11293 or else Is_Renamed_Object
(N
)
11297 -- Don't do this optimization if we are within the code for a
11298 -- discriminant check, since the whole point of such a check may
11299 -- be to verify the condition on which the code below depends.
11301 elsif Is_In_Discriminant_Check
(N
) then
11304 -- Green light to see if we can do the optimization. There is
11305 -- still one condition that inhibits the optimization below but
11306 -- now is the time to check the particular discriminant.
11309 -- Loop through discriminants to find the matching discriminant
11310 -- constraint to see if we can copy it.
11312 Disc
:= First_Discriminant
(Ptyp
);
11313 Dcon
:= First_Elmt
(Discriminant_Constraint
(Ptyp
));
11314 Discr_Loop
: while Present
(Dcon
) loop
11315 Dval
:= Node
(Dcon
);
11317 -- Check if this is the matching discriminant and if the
11318 -- discriminant value is simple enough to make sense to
11319 -- copy. We don't want to copy complex expressions, and
11320 -- indeed to do so can cause trouble (before we put in
11321 -- this guard, a discriminant expression containing an
11322 -- AND THEN was copied, causing problems for coverage
11323 -- analysis tools).
11325 -- However, if the reference is part of the initialization
11326 -- code generated for an object declaration, we must use
11327 -- the discriminant value from the subtype constraint,
11328 -- because the selected component may be a reference to the
11329 -- object being initialized, whose discriminant is not yet
11330 -- set. This only happens in complex cases involving changes
11331 -- of representation.
11333 if Disc
= Entity
(Selector_Name
(N
))
11334 and then (Is_Entity_Name
(Dval
)
11335 or else Compile_Time_Known_Value
(Dval
)
11336 or else Is_Subtype_Declaration
)
11338 -- Here we have the matching discriminant. Check for
11339 -- the case of a discriminant of a component that is
11340 -- constrained by an outer discriminant, which cannot
11341 -- be optimized away.
11343 if Denotes_Discriminant
(Dval
, Check_Concurrent
=> True)
11347 -- Do not retrieve value if constraint is not static. It
11348 -- is generally not useful, and the constraint may be a
11349 -- rewritten outer discriminant in which case it is in
11352 elsif Is_Entity_Name
(Dval
)
11354 Nkind
(Parent
(Entity
(Dval
))) = N_Object_Declaration
11355 and then Present
(Expression
(Parent
(Entity
(Dval
))))
11357 Is_OK_Static_Expression
11358 (Expression
(Parent
(Entity
(Dval
))))
11362 -- In the context of a case statement, the expression may
11363 -- have the base type of the discriminant, and we need to
11364 -- preserve the constraint to avoid spurious errors on
11367 elsif Nkind
(Parent
(N
)) = N_Case_Statement
11368 and then Etype
(Dval
) /= Etype
(Disc
)
11371 Make_Qualified_Expression
(Loc
,
11373 New_Occurrence_Of
(Etype
(Disc
), Loc
),
11375 New_Copy_Tree
(Dval
)));
11376 Analyze_And_Resolve
(N
, Etype
(Disc
));
11378 -- In case that comes out as a static expression,
11379 -- reset it (a selected component is never static).
11381 Set_Is_Static_Expression
(N
, False);
11384 -- Otherwise we can just copy the constraint, but the
11385 -- result is certainly not static. In some cases the
11386 -- discriminant constraint has been analyzed in the
11387 -- context of the original subtype indication, but for
11388 -- itypes the constraint might not have been analyzed
11389 -- yet, and this must be done now.
11392 Rewrite
(N
, New_Copy_Tree
(Dval
));
11393 Analyze_And_Resolve
(N
);
11394 Set_Is_Static_Expression
(N
, False);
11400 Next_Discriminant
(Disc
);
11401 end loop Discr_Loop
;
11403 -- Note: the above loop should always find a matching
11404 -- discriminant, but if it does not, we just missed an
11405 -- optimization due to some glitch (perhaps a previous
11406 -- error), so ignore.
11411 -- The only remaining processing is in the case of a discriminant of
11412 -- a concurrent object, where we rewrite the prefix to denote the
11413 -- corresponding record type. If the type is derived and has renamed
11414 -- discriminants, use corresponding discriminant, which is the one
11415 -- that appears in the corresponding record.
11417 if not Is_Concurrent_Type
(Ptyp
) then
11421 Disc
:= Entity
(Selector_Name
(N
));
11423 if Is_Derived_Type
(Ptyp
)
11424 and then Present
(Corresponding_Discriminant
(Disc
))
11426 Disc
:= Corresponding_Discriminant
(Disc
);
11430 Make_Selected_Component
(Loc
,
11432 Unchecked_Convert_To
(Corresponding_Record_Type
(Ptyp
),
11433 New_Copy_Tree
(P
)),
11434 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Disc
)));
11436 Rewrite
(N
, New_N
);
11440 -- Set Atomic_Sync_Required if necessary for atomic component
11442 if Nkind
(N
) = N_Selected_Component
then
11444 E
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
11448 -- If component is atomic, but type is not, setting depends on
11449 -- disable/enable state for the component.
11451 if Is_Atomic
(E
) and then not Is_Atomic
(Etype
(E
)) then
11452 Set
:= not Atomic_Synchronization_Disabled
(E
);
11454 -- If component is not atomic, but its type is atomic, setting
11455 -- depends on disable/enable state for the type.
11457 elsif not Is_Atomic
(E
) and then Is_Atomic
(Etype
(E
)) then
11458 Set
:= not Atomic_Synchronization_Disabled
(Etype
(E
));
11460 -- If both component and type are atomic, we disable if either
11461 -- component or its type have sync disabled.
11463 elsif Is_Atomic
(E
) and then Is_Atomic
(Etype
(E
)) then
11464 Set
:= not Atomic_Synchronization_Disabled
(E
)
11466 not Atomic_Synchronization_Disabled
(Etype
(E
));
11472 -- Set flag if required
11475 Activate_Atomic_Synchronization
(N
);
11479 end Expand_N_Selected_Component
;
11481 --------------------
11482 -- Expand_N_Slice --
11483 --------------------
11485 procedure Expand_N_Slice
(N
: Node_Id
) is
11486 Loc
: constant Source_Ptr
:= Sloc
(N
);
11487 Typ
: constant Entity_Id
:= Etype
(N
);
11489 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean;
11490 -- Check whether the argument is an actual for a procedure call, in
11491 -- which case the expansion of a bit-packed slice is deferred until the
11492 -- call itself is expanded. The reason this is required is that we might
11493 -- have an IN OUT or OUT parameter, and the copy out is essential, and
11494 -- that copy out would be missed if we created a temporary here in
11495 -- Expand_N_Slice. Note that we don't bother to test specifically for an
11496 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
11497 -- is harmless to defer expansion in the IN case, since the call
11498 -- processing will still generate the appropriate copy in operation,
11499 -- which will take care of the slice.
11501 procedure Make_Temporary_For_Slice
;
11502 -- Create a named variable for the value of the slice, in cases where
11503 -- the back end cannot handle it properly, e.g. when packed types or
11504 -- unaligned slices are involved.
11506 -------------------------
11507 -- Is_Procedure_Actual --
11508 -------------------------
11510 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean is
11511 Par
: Node_Id
:= Parent
(N
);
11515 -- If our parent is a procedure call we can return
11517 if Nkind
(Par
) = N_Procedure_Call_Statement
then
11520 -- If our parent is a type conversion, keep climbing the tree,
11521 -- since a type conversion can be a procedure actual. Also keep
11522 -- climbing if parameter association or a qualified expression,
11523 -- since these are additional cases that do can appear on
11524 -- procedure actuals.
11526 elsif Nkind
(Par
) in N_Type_Conversion
11527 | N_Parameter_Association
11528 | N_Qualified_Expression
11530 Par
:= Parent
(Par
);
11532 -- Any other case is not what we are looking for
11538 end Is_Procedure_Actual
;
11540 ------------------------------
11541 -- Make_Temporary_For_Slice --
11542 ------------------------------
11544 procedure Make_Temporary_For_Slice
is
11545 Ent
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', N
);
11550 Make_Object_Declaration
(Loc
,
11551 Defining_Identifier
=> Ent
,
11552 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
11554 Set_No_Initialization
(Decl
);
11556 Insert_Actions
(N
, New_List
(
11558 Make_Assignment_Statement
(Loc
,
11559 Name
=> New_Occurrence_Of
(Ent
, Loc
),
11560 Expression
=> Relocate_Node
(N
))));
11562 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
11563 Analyze_And_Resolve
(N
, Typ
);
11564 end Make_Temporary_For_Slice
;
11568 Pref
: constant Node_Id
:= Prefix
(N
);
11570 -- Start of processing for Expand_N_Slice
11573 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
11574 -- function, then additional actuals must be passed.
11576 if Is_Build_In_Place_Function_Call
(Pref
) then
11577 Make_Build_In_Place_Call_In_Anonymous_Context
(Pref
);
11579 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
11580 -- containing build-in-place function calls whose returned object covers
11581 -- interface types.
11583 elsif Present
(Unqual_BIP_Iface_Function_Call
(Pref
)) then
11584 Make_Build_In_Place_Iface_Call_In_Anonymous_Context
(Pref
);
11587 -- The remaining case to be handled is packed slices. We can leave
11588 -- packed slices as they are in the following situations:
11590 -- 1. Right or left side of an assignment (we can handle this
11591 -- situation correctly in the assignment statement expansion).
11593 -- 2. Prefix of indexed component (the slide is optimized away in this
11594 -- case, see the start of Expand_N_Indexed_Component.)
11596 -- 3. Object renaming declaration, since we want the name of the
11597 -- slice, not the value.
11599 -- 4. Argument to procedure call, since copy-in/copy-out handling may
11600 -- be required, and this is handled in the expansion of call
11603 -- 5. Prefix of an address attribute (this is an error which is caught
11604 -- elsewhere, and the expansion would interfere with generating the
11605 -- error message) or of a size attribute (because 'Size may change
11606 -- when applied to the temporary instead of the slice directly).
11608 if not Is_Packed
(Typ
) then
11610 -- Apply transformation for actuals of a function call, where
11611 -- Expand_Actuals is not used.
11613 if Nkind
(Parent
(N
)) = N_Function_Call
11614 and then Is_Possibly_Unaligned_Slice
(N
)
11616 Make_Temporary_For_Slice
;
11619 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
11620 or else (Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
11621 and then Parent
(N
) = Name
(Parent
(Parent
(N
))))
11625 elsif Nkind
(Parent
(N
)) = N_Indexed_Component
11626 or else Is_Renamed_Object
(N
)
11627 or else Is_Procedure_Actual
(N
)
11631 elsif Nkind
(Parent
(N
)) = N_Attribute_Reference
11632 and then (Attribute_Name
(Parent
(N
)) = Name_Address
11633 or else Attribute_Name
(Parent
(N
)) = Name_Size
)
11638 Make_Temporary_For_Slice
;
11640 end Expand_N_Slice
;
11642 ------------------------------
11643 -- Expand_N_Type_Conversion --
11644 ------------------------------
11646 procedure Expand_N_Type_Conversion
(N
: Node_Id
) is
11647 Loc
: constant Source_Ptr
:= Sloc
(N
);
11648 Operand
: constant Node_Id
:= Expression
(N
);
11649 Operand_Acc
: Node_Id
:= Operand
;
11650 Target_Type
: Entity_Id
:= Etype
(N
);
11651 Operand_Type
: Entity_Id
:= Etype
(Operand
);
11653 procedure Discrete_Range_Check
;
11654 -- Handles generation of range check for discrete target value
11656 procedure Handle_Changed_Representation
;
11657 -- This is called in the case of record and array type conversions to
11658 -- see if there is a change of representation to be handled. Change of
11659 -- representation is actually handled at the assignment statement level,
11660 -- and what this procedure does is rewrite node N conversion as an
11661 -- assignment to temporary. If there is no change of representation,
11662 -- then the conversion node is unchanged.
11664 procedure Raise_Accessibility_Error
;
11665 -- Called when we know that an accessibility check will fail. Rewrites
11666 -- node N to an appropriate raise statement and outputs warning msgs.
11667 -- The Etype of the raise node is set to Target_Type. Note that in this
11668 -- case the rest of the processing should be skipped (i.e. the call to
11669 -- this procedure will be followed by "goto Done").
11671 procedure Real_Range_Check
;
11672 -- Handles generation of range check for real target value
11674 function Has_Extra_Accessibility
(Id
: Entity_Id
) return Boolean;
11675 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
11676 -- evaluates to True.
11678 function Statically_Deeper_Relation_Applies
(Targ_Typ
: Entity_Id
)
11680 -- Given a target type for a conversion, determine whether the
11681 -- statically deeper accessibility rules apply to it.
11683 --------------------------
11684 -- Discrete_Range_Check --
11685 --------------------------
11687 -- Case of conversions to a discrete type. We let Generate_Range_Check
11688 -- do the heavy lifting, after converting a fixed-point operand to an
11689 -- appropriate integer type.
11691 procedure Discrete_Range_Check
is
11695 procedure Generate_Temporary
;
11696 -- Generate a temporary to facilitate in the C backend the code
11697 -- generation of the unchecked conversion since the size of the
11698 -- source type may differ from the size of the target type.
11700 ------------------------
11701 -- Generate_Temporary --
11702 ------------------------
11704 procedure Generate_Temporary
is
11706 if Esize
(Etype
(Expr
)) < Esize
(Etype
(Ityp
)) then
11708 Exp_Type
: constant Entity_Id
:= Ityp
;
11709 Def_Id
: constant Entity_Id
:=
11710 Make_Temporary
(Loc
, 'R', Expr
);
11715 Set_Is_Internal
(Def_Id
);
11716 Set_Etype
(Def_Id
, Exp_Type
);
11717 Res
:= New_Occurrence_Of
(Def_Id
, Loc
);
11720 Make_Object_Declaration
(Loc
,
11721 Defining_Identifier
=> Def_Id
,
11722 Object_Definition
=> New_Occurrence_Of
11724 Constant_Present
=> True,
11725 Expression
=> Relocate_Node
(Expr
));
11727 Set_Assignment_OK
(E
);
11728 Insert_Action
(Expr
, E
);
11730 Set_Assignment_OK
(Res
, Assignment_OK
(Expr
));
11732 Rewrite
(Expr
, Res
);
11733 Analyze_And_Resolve
(Expr
, Exp_Type
);
11736 end Generate_Temporary
;
11738 -- Start of processing for Discrete_Range_Check
11741 -- Nothing more to do if conversion was rewritten
11743 if Nkind
(N
) /= N_Type_Conversion
then
11747 Expr
:= Expression
(N
);
11749 -- Clear the Do_Range_Check flag on Expr
11751 Set_Do_Range_Check
(Expr
, False);
11753 -- Nothing to do if range checks suppressed
11755 if Range_Checks_Suppressed
(Target_Type
) then
11759 -- Nothing to do if expression is an entity on which checks have been
11762 if Is_Entity_Name
(Expr
)
11763 and then Range_Checks_Suppressed
(Entity
(Expr
))
11768 -- Before we do a range check, we have to deal with treating
11769 -- a fixed-point operand as an integer. The way we do this
11770 -- is simply to do an unchecked conversion to an appropriate
11771 -- integer type with the smallest size, so that we can suppress
11774 if Is_Fixed_Point_Type
(Etype
(Expr
)) then
11775 Ityp
:= Small_Integer_Type_For
11776 (Esize
(Base_Type
(Etype
(Expr
))), Uns
=> False);
11778 -- Generate a temporary with the integer type to facilitate in the
11779 -- C backend the code generation for the unchecked conversion.
11781 if Modify_Tree_For_C
then
11782 Generate_Temporary
;
11785 Rewrite
(Expr
, Unchecked_Convert_To
(Ityp
, Expr
));
11788 -- Reset overflow flag, since the range check will include
11789 -- dealing with possible overflow, and generate the check.
11791 Set_Do_Overflow_Check
(N
, False);
11793 Generate_Range_Check
(Expr
, Target_Type
, CE_Range_Check_Failed
);
11794 end Discrete_Range_Check
;
11796 -----------------------------------
11797 -- Handle_Changed_Representation --
11798 -----------------------------------
11800 procedure Handle_Changed_Representation
is
11808 -- Nothing else to do if no change of representation
11810 if Has_Compatible_Representation
(Target_Type
, Operand_Type
) then
11813 -- The real change of representation work is done by the assignment
11814 -- statement processing. So if this type conversion is appearing as
11815 -- the expression of an assignment statement, nothing needs to be
11816 -- done to the conversion.
11818 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
then
11821 -- Otherwise we need to generate a temporary variable, and do the
11822 -- change of representation assignment into that temporary variable.
11823 -- The conversion is then replaced by a reference to this variable.
11828 -- If type is unconstrained we have to add a constraint, copied
11829 -- from the actual value of the left-hand side.
11831 if not Is_Constrained
(Target_Type
) then
11832 if Has_Discriminants
(Operand_Type
) then
11834 -- A change of representation can only apply to untagged
11835 -- types. We need to build the constraint that applies to
11836 -- the target type, using the constraints of the operand.
11837 -- The analysis is complicated if there are both inherited
11838 -- discriminants and constrained discriminants.
11839 -- We iterate over the discriminants of the target, and
11840 -- find the discriminant of the same name:
11842 -- a) If there is a corresponding discriminant in the object
11843 -- then the value is a selected component of the operand.
11845 -- b) Otherwise the value of a constrained discriminant is
11846 -- found in the stored constraint of the operand.
11849 Stored
: constant Elist_Id
:=
11850 Stored_Constraint
(Operand_Type
);
11851 -- Stored constraints of the operand. If present, they
11852 -- correspond to the discriminants of the parent type.
11854 Disc_O
: Entity_Id
;
11855 -- Discriminant of the operand type. Its value in the
11856 -- object is captured in a selected component.
11858 Disc_T
: Entity_Id
;
11859 -- Discriminant of the target type
11864 Disc_O
:= First_Discriminant
(Operand_Type
);
11865 Disc_T
:= First_Discriminant
(Target_Type
);
11866 Elmt
:= (if Present
(Stored
)
11867 then First_Elmt
(Stored
)
11871 while Present
(Disc_T
) loop
11872 if Present
(Disc_O
)
11873 and then Chars
(Disc_T
) = Chars
(Disc_O
)
11876 Make_Selected_Component
(Loc
,
11878 Duplicate_Subexpr_Move_Checks
(Operand
),
11880 Make_Identifier
(Loc
, Chars
(Disc_O
))));
11881 Next_Discriminant
(Disc_O
);
11883 elsif Present
(Elmt
) then
11884 Append_To
(Cons
, New_Copy_Tree
(Node
(Elmt
)));
11887 if Present
(Elmt
) then
11891 Next_Discriminant
(Disc_T
);
11895 elsif Is_Array_Type
(Operand_Type
) then
11896 N_Ix
:= First_Index
(Target_Type
);
11899 for J
in 1 .. Number_Dimensions
(Operand_Type
) loop
11901 -- We convert the bounds explicitly. We use an unchecked
11902 -- conversion because bounds checks are done elsewhere.
11907 Unchecked_Convert_To
(Etype
(N_Ix
),
11908 Make_Attribute_Reference
(Loc
,
11910 Duplicate_Subexpr_No_Checks
11911 (Operand
, Name_Req
=> True),
11912 Attribute_Name
=> Name_First
,
11913 Expressions
=> New_List
(
11914 Make_Integer_Literal
(Loc
, J
)))),
11917 Unchecked_Convert_To
(Etype
(N_Ix
),
11918 Make_Attribute_Reference
(Loc
,
11920 Duplicate_Subexpr_No_Checks
11921 (Operand
, Name_Req
=> True),
11922 Attribute_Name
=> Name_Last
,
11923 Expressions
=> New_List
(
11924 Make_Integer_Literal
(Loc
, J
))))));
11931 Odef
:= New_Occurrence_Of
(Target_Type
, Loc
);
11933 if Present
(Cons
) then
11935 Make_Subtype_Indication
(Loc
,
11936 Subtype_Mark
=> Odef
,
11938 Make_Index_Or_Discriminant_Constraint
(Loc
,
11939 Constraints
=> Cons
));
11942 Temp
:= Make_Temporary
(Loc
, 'C');
11944 Make_Object_Declaration
(Loc
,
11945 Defining_Identifier
=> Temp
,
11946 Object_Definition
=> Odef
);
11948 Set_No_Initialization
(Decl
, True);
11950 -- Insert required actions. It is essential to suppress checks
11951 -- since we have suppressed default initialization, which means
11952 -- that the variable we create may have no discriminants.
11957 Make_Assignment_Statement
(Loc
,
11958 Name
=> New_Occurrence_Of
(Temp
, Loc
),
11959 Expression
=> Relocate_Node
(N
))),
11960 Suppress
=> All_Checks
);
11962 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
11965 end Handle_Changed_Representation
;
11967 -------------------------------
11968 -- Raise_Accessibility_Error --
11969 -------------------------------
11971 procedure Raise_Accessibility_Error
is
11973 Error_Msg_Warn
:= SPARK_Mode
/= On
;
11975 Make_Raise_Program_Error
(Sloc
(N
),
11976 Reason
=> PE_Accessibility_Check_Failed
));
11977 Set_Etype
(N
, Target_Type
);
11979 Error_Msg_N
("accessibility check failure<<", N
);
11980 Error_Msg_N
("\Program_Error [<<", N
);
11981 end Raise_Accessibility_Error
;
11983 ----------------------
11984 -- Real_Range_Check --
11985 ----------------------
11987 -- Case of conversions to floating-point or fixed-point. If range checks
11988 -- are enabled and the target type has a range constraint, we convert:
11994 -- Tnn : typ'Base := typ'Base (x);
11995 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
11998 -- This is necessary when there is a conversion of integer to float or
11999 -- to fixed-point to ensure that the correct checks are made. It is not
12000 -- necessary for the float-to-float case where it is enough to just set
12001 -- the Do_Range_Check flag on the expression.
12003 procedure Real_Range_Check
is
12004 Btyp
: constant Entity_Id
:= Base_Type
(Target_Type
);
12005 Lo
: constant Node_Id
:= Type_Low_Bound
(Target_Type
);
12006 Hi
: constant Node_Id
:= Type_High_Bound
(Target_Type
);
12017 -- Nothing more to do if conversion was rewritten
12019 if Nkind
(N
) /= N_Type_Conversion
then
12023 Expr
:= Expression
(N
);
12025 -- Clear the Do_Range_Check flag on Expr
12027 Set_Do_Range_Check
(Expr
, False);
12029 -- Nothing to do if range checks suppressed, or target has the same
12030 -- range as the base type (or is the base type).
12032 if Range_Checks_Suppressed
(Target_Type
)
12033 or else (Lo
= Type_Low_Bound
(Btyp
)
12035 Hi
= Type_High_Bound
(Btyp
))
12040 -- Nothing to do if expression is an entity on which checks have been
12043 if Is_Entity_Name
(Expr
)
12044 and then Range_Checks_Suppressed
(Entity
(Expr
))
12049 -- Nothing to do if expression was rewritten into a float-to-float
12050 -- conversion, since this kind of conversion is handled elsewhere.
12052 if Is_Floating_Point_Type
(Etype
(Expr
))
12053 and then Is_Floating_Point_Type
(Target_Type
)
12058 -- Nothing to do if bounds are all static and we can tell that the
12059 -- expression is within the bounds of the target. Note that if the
12060 -- operand is of an unconstrained floating-point type, then we do
12061 -- not trust it to be in range (might be infinite)
12064 S_Lo
: constant Node_Id
:= Type_Low_Bound
(Etype
(Expr
));
12065 S_Hi
: constant Node_Id
:= Type_High_Bound
(Etype
(Expr
));
12068 if (not Is_Floating_Point_Type
(Etype
(Expr
))
12069 or else Is_Constrained
(Etype
(Expr
)))
12070 and then Compile_Time_Known_Value
(S_Lo
)
12071 and then Compile_Time_Known_Value
(S_Hi
)
12072 and then Compile_Time_Known_Value
(Hi
)
12073 and then Compile_Time_Known_Value
(Lo
)
12076 D_Lov
: constant Ureal
:= Expr_Value_R
(Lo
);
12077 D_Hiv
: constant Ureal
:= Expr_Value_R
(Hi
);
12082 if Is_Real_Type
(Etype
(Expr
)) then
12083 S_Lov
:= Expr_Value_R
(S_Lo
);
12084 S_Hiv
:= Expr_Value_R
(S_Hi
);
12086 S_Lov
:= UR_From_Uint
(Expr_Value
(S_Lo
));
12087 S_Hiv
:= UR_From_Uint
(Expr_Value
(S_Hi
));
12091 and then S_Lov
>= D_Lov
12092 and then S_Hiv
<= D_Hiv
12100 -- Otherwise rewrite the conversion as described above
12102 Conv
:= Convert_To
(Btyp
, Expr
);
12104 -- If a conversion is necessary, then copy the specific flags from
12105 -- the original one and also move the Do_Overflow_Check flag since
12106 -- this new conversion is to the base type.
12108 if Nkind
(Conv
) = N_Type_Conversion
then
12109 Set_Conversion_OK
(Conv
, Conversion_OK
(N
));
12110 Set_Float_Truncate
(Conv
, Float_Truncate
(N
));
12111 Set_Rounded_Result
(Conv
, Rounded_Result
(N
));
12113 if Do_Overflow_Check
(N
) then
12114 Set_Do_Overflow_Check
(Conv
);
12115 Set_Do_Overflow_Check
(N
, False);
12119 Tnn
:= Make_Temporary
(Loc
, 'T', Conv
);
12121 -- For a conversion from Float to Fixed where the bounds of the
12122 -- fixed-point type are static, we can obtain a more accurate
12123 -- fixed-point value by converting the result of the floating-
12124 -- point expression to an appropriate integer type, and then
12125 -- performing an unchecked conversion to the target fixed-point
12126 -- type. The range check can then use the corresponding integer
12127 -- value of the bounds instead of requiring further conversions.
12128 -- This preserves the identity:
12130 -- Fix_Val = Fixed_Type (Float_Type (Fix_Val))
12132 -- which used to fail when Fix_Val was a bound of the type and
12133 -- the 'Small was not a representable number.
12134 -- This transformation requires an integer type large enough to
12135 -- accommodate a fixed-point value.
12137 if Is_Ordinary_Fixed_Point_Type
(Target_Type
)
12138 and then Is_Floating_Point_Type
(Etype
(Expr
))
12139 and then RM_Size
(Btyp
) <= System_Max_Integer_Size
12140 and then Nkind
(Lo
) = N_Real_Literal
12141 and then Nkind
(Hi
) = N_Real_Literal
12144 Expr_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', Conv
);
12145 Int_Typ
: constant Entity_Id
:=
12146 Small_Integer_Type_For
(RM_Size
(Btyp
), Uns
=> False);
12149 -- Generate a temporary with the integer value. Required in the
12150 -- CCG compiler to ensure that run-time checks reference this
12151 -- integer expression (instead of the resulting fixed-point
12152 -- value because fixed-point values are handled by means of
12153 -- unsigned integer types).
12156 Make_Object_Declaration
(Loc
,
12157 Defining_Identifier
=> Expr_Id
,
12158 Object_Definition
=> New_Occurrence_Of
(Int_Typ
, Loc
),
12159 Constant_Present
=> True,
12161 Convert_To
(Int_Typ
, Expression
(Conv
))));
12163 -- Create integer objects for range checking of result.
12166 Unchecked_Convert_To
12167 (Int_Typ
, New_Occurrence_Of
(Expr_Id
, Loc
));
12170 Make_Integer_Literal
(Loc
, Corresponding_Integer_Value
(Lo
));
12173 Unchecked_Convert_To
12174 (Int_Typ
, New_Occurrence_Of
(Expr_Id
, Loc
));
12177 Make_Integer_Literal
(Loc
, Corresponding_Integer_Value
(Hi
));
12179 -- Rewrite conversion as an integer conversion of the
12180 -- original floating-point expression, followed by an
12181 -- unchecked conversion to the target fixed-point type.
12184 Unchecked_Convert_To
12185 (Target_Type
, New_Occurrence_Of
(Expr_Id
, Loc
));
12188 -- All other conversions
12191 Lo_Arg
:= New_Occurrence_Of
(Tnn
, Loc
);
12193 Make_Attribute_Reference
(Loc
,
12194 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
12195 Attribute_Name
=> Name_First
);
12197 Hi_Arg
:= New_Occurrence_Of
(Tnn
, Loc
);
12199 Make_Attribute_Reference
(Loc
,
12200 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
12201 Attribute_Name
=> Name_Last
);
12204 -- Build code for range checking. Note that checks are suppressed
12205 -- here since we don't want a recursive range check popping up.
12207 Insert_Actions
(N
, New_List
(
12208 Make_Object_Declaration
(Loc
,
12209 Defining_Identifier
=> Tnn
,
12210 Object_Definition
=> New_Occurrence_Of
(Btyp
, Loc
),
12211 Constant_Present
=> True,
12212 Expression
=> Conv
),
12214 Make_Raise_Constraint_Error
(Loc
,
12219 Left_Opnd
=> Lo_Arg
,
12220 Right_Opnd
=> Lo_Val
),
12224 Left_Opnd
=> Hi_Arg
,
12225 Right_Opnd
=> Hi_Val
)),
12226 Reason
=> CE_Range_Check_Failed
)),
12227 Suppress
=> All_Checks
);
12229 Rewrite
(Expr
, New_Occurrence_Of
(Tnn
, Loc
));
12230 end Real_Range_Check
;
12232 -----------------------------
12233 -- Has_Extra_Accessibility --
12234 -----------------------------
12236 -- Returns true for a formal of an anonymous access type or for an Ada
12237 -- 2012-style stand-alone object of an anonymous access type.
12239 function Has_Extra_Accessibility
(Id
: Entity_Id
) return Boolean is
12241 if Is_Formal
(Id
) or else Ekind
(Id
) in E_Constant | E_Variable
then
12242 return Present
(Effective_Extra_Accessibility
(Id
));
12246 end Has_Extra_Accessibility
;
12248 ----------------------------------------
12249 -- Statically_Deeper_Relation_Applies --
12250 ----------------------------------------
12252 function Statically_Deeper_Relation_Applies
(Targ_Typ
: Entity_Id
)
12256 -- The case where the target type is an anonymous access type is
12257 -- ignored since they have different semantics and get covered by
12258 -- various runtime checks depending on context.
12260 -- Note, the current implementation of this predicate is incomplete
12261 -- and doesn't fully reflect the rules given in RM 3.10.2 (19) and
12264 return Ekind
(Targ_Typ
) /= E_Anonymous_Access_Type
;
12265 end Statically_Deeper_Relation_Applies
;
12267 -- Start of processing for Expand_N_Type_Conversion
12270 -- First remove check marks put by the semantic analysis on the type
12271 -- conversion between array types. We need these checks, and they will
12272 -- be generated by this expansion routine, but we do not depend on these
12273 -- flags being set, and since we do intend to expand the checks in the
12274 -- front end, we don't want them on the tree passed to the back end.
12276 if Is_Array_Type
(Target_Type
) then
12277 if Is_Constrained
(Target_Type
) then
12278 Set_Do_Length_Check
(N
, False);
12280 Set_Do_Range_Check
(Operand
, False);
12284 -- Nothing at all to do if conversion is to the identical type so remove
12285 -- the conversion completely, it is useless, except that it may carry
12286 -- an Assignment_OK attribute, which must be propagated to the operand
12287 -- and the Do_Range_Check flag on the operand must be cleared, if any.
12289 if Operand_Type
= Target_Type
then
12290 if Assignment_OK
(N
) then
12291 Set_Assignment_OK
(Operand
);
12294 Set_Do_Range_Check
(Operand
, False);
12296 Rewrite
(N
, Relocate_Node
(Operand
));
12301 -- Nothing to do if this is the second argument of read. This is a
12302 -- "backwards" conversion that will be handled by the specialized code
12303 -- in attribute processing.
12305 if Nkind
(Parent
(N
)) = N_Attribute_Reference
12306 and then Attribute_Name
(Parent
(N
)) = Name_Read
12307 and then Next
(First
(Expressions
(Parent
(N
)))) = N
12312 -- Check for case of converting to a type that has an invariant
12313 -- associated with it. This requires an invariant check. We insert
12316 -- invariant_check (typ (expr))
12318 -- in the code, after removing side effects from the expression.
12319 -- This is clearer than replacing the conversion into an expression
12320 -- with actions, because the context may impose additional actions
12321 -- (tag checks, membership tests, etc.) that conflict with this
12322 -- rewriting (used previously).
12324 -- Note: the Comes_From_Source check, and then the resetting of this
12325 -- flag prevents what would otherwise be an infinite recursion.
12327 if Has_Invariants
(Target_Type
)
12328 and then Present
(Invariant_Procedure
(Target_Type
))
12329 and then Comes_From_Source
(N
)
12331 Set_Comes_From_Source
(N
, False);
12332 Remove_Side_Effects
(N
);
12333 Insert_Action
(N
, Make_Invariant_Call
(Duplicate_Subexpr
(N
)));
12336 -- AI12-0042: For a view conversion to a class-wide type occurring
12337 -- within the immediate scope of T, from a specific type that is
12338 -- a descendant of T (including T itself), an invariant check is
12339 -- performed on the part of the object that is of type T. (We don't
12340 -- need to explicitly check for the operand type being a descendant,
12341 -- just that it's a specific type, because the conversion would be
12342 -- illegal if it's specific and not a descendant -- downward conversion
12343 -- is not allowed).
12345 elsif Is_Class_Wide_Type
(Target_Type
)
12346 and then not Is_Class_Wide_Type
(Etype
(Expression
(N
)))
12347 and then Present
(Invariant_Procedure
(Root_Type
(Target_Type
)))
12348 and then Comes_From_Source
(N
)
12349 and then Within_Scope
(Find_Enclosing_Scope
(N
), Scope
(Target_Type
))
12351 Remove_Side_Effects
(N
);
12353 -- Perform the invariant check on a conversion to the class-wide
12354 -- type's root type.
12357 Root_Conv
: constant Node_Id
:=
12358 Make_Type_Conversion
(Loc
,
12360 New_Occurrence_Of
(Root_Type
(Target_Type
), Loc
),
12361 Expression
=> Duplicate_Subexpr
(Expression
(N
)));
12363 Set_Etype
(Root_Conv
, Root_Type
(Target_Type
));
12365 Insert_Action
(N
, Make_Invariant_Call
(Root_Conv
));
12370 -- Here if we may need to expand conversion
12372 -- If the operand of the type conversion is an arithmetic operation on
12373 -- signed integers, and the based type of the signed integer type in
12374 -- question is smaller than Standard.Integer, we promote both of the
12375 -- operands to type Integer.
12377 -- For example, if we have
12379 -- target-type (opnd1 + opnd2)
12381 -- and opnd1 and opnd2 are of type short integer, then we rewrite
12384 -- target-type (integer(opnd1) + integer(opnd2))
12386 -- We do this because we are always allowed to compute in a larger type
12387 -- if we do the right thing with the result, and in this case we are
12388 -- going to do a conversion which will do an appropriate check to make
12389 -- sure that things are in range of the target type in any case. This
12390 -- avoids some unnecessary intermediate overflows.
12392 -- We might consider a similar transformation in the case where the
12393 -- target is a real type or a 64-bit integer type, and the operand
12394 -- is an arithmetic operation using a 32-bit integer type. However,
12395 -- we do not bother with this case, because it could cause significant
12396 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
12397 -- much cheaper, but we don't want different behavior on 32-bit and
12398 -- 64-bit machines. Note that the exclusion of the 64-bit case also
12399 -- handles the configurable run-time cases where 64-bit arithmetic
12400 -- may simply be unavailable.
12402 -- Note: this circuit is partially redundant with respect to the circuit
12403 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
12404 -- the processing here. Also we still need the Checks circuit, since we
12405 -- have to be sure not to generate junk overflow checks in the first
12406 -- place, since it would be tricky to remove them here.
12408 if Integer_Promotion_Possible
(N
) then
12410 -- All conditions met, go ahead with transformation
12417 Opnd
:= New_Op_Node
(Nkind
(Operand
), Loc
);
12419 R
:= Convert_To
(Standard_Integer
, Right_Opnd
(Operand
));
12420 Set_Right_Opnd
(Opnd
, R
);
12422 if Nkind
(Operand
) in N_Binary_Op
then
12423 L
:= Convert_To
(Standard_Integer
, Left_Opnd
(Operand
));
12424 Set_Left_Opnd
(Opnd
, L
);
12428 Make_Type_Conversion
(Loc
,
12429 Subtype_Mark
=> Relocate_Node
(Subtype_Mark
(N
)),
12430 Expression
=> Opnd
));
12432 Analyze_And_Resolve
(N
, Target_Type
);
12437 -- If the conversion is from Universal_Integer and requires an overflow
12438 -- check, try to do an intermediate conversion to a narrower type first
12439 -- without overflow check, in order to avoid doing the overflow check
12440 -- in Universal_Integer, which can be a very large type.
12442 if Operand_Type
= Universal_Integer
and then Do_Overflow_Check
(N
) then
12444 Lo
, Hi
, Siz
: Uint
;
12449 Determine_Range
(Operand
, OK
, Lo
, Hi
, Assume_Valid
=> True);
12452 Siz
:= Get_Size_For_Range
(Lo
, Hi
);
12454 -- We use the base type instead of the first subtype because
12455 -- overflow checks are done in the base type, so this avoids
12456 -- the need for useless conversions.
12458 if Siz
< System_Max_Integer_Size
then
12459 Typ
:= Etype
(Integer_Type_For
(Siz
, Uns
=> False));
12461 Convert_To_And_Rewrite
(Typ
, Operand
);
12462 Analyze_And_Resolve
12463 (Operand
, Typ
, Suppress
=> Overflow_Check
);
12465 Analyze_And_Resolve
(N
, Target_Type
);
12472 -- Do validity check if validity checking operands
12474 if Validity_Checks_On
and Validity_Check_Operands
then
12475 Ensure_Valid
(Operand
);
12478 -- Special case of converting from non-standard boolean type
12480 if Is_Boolean_Type
(Operand_Type
)
12481 and then Nonzero_Is_True
(Operand_Type
)
12483 Adjust_Condition
(Operand
);
12484 Set_Etype
(Operand
, Standard_Boolean
);
12485 Operand_Type
:= Standard_Boolean
;
12488 -- Case of converting to an access type
12490 if Is_Access_Type
(Target_Type
) then
12491 -- In terms of accessibility rules, an anonymous access discriminant
12492 -- is not considered separate from its parent object.
12494 if Nkind
(Operand
) = N_Selected_Component
12495 and then Ekind
(Entity
(Selector_Name
(Operand
))) = E_Discriminant
12496 and then Ekind
(Operand_Type
) = E_Anonymous_Access_Type
12498 Operand_Acc
:= Original_Node
(Prefix
(Operand
));
12501 -- If this type conversion was internally generated by the front end
12502 -- to displace the pointer to the object to reference an interface
12503 -- type and the original node was an Unrestricted_Access attribute,
12504 -- then skip applying accessibility checks (because, according to the
12505 -- GNAT Reference Manual, this attribute is similar to 'Access except
12506 -- that all accessibility and aliased view checks are omitted).
12508 if not Comes_From_Source
(N
)
12509 and then Is_Interface
(Designated_Type
(Target_Type
))
12510 and then Nkind
(Original_Node
(N
)) = N_Attribute_Reference
12511 and then Attribute_Name
(Original_Node
(N
)) =
12512 Name_Unrestricted_Access
12516 -- Apply an accessibility check when the conversion operand is an
12517 -- access parameter (or a renaming thereof), unless conversion was
12518 -- expanded from an Unchecked_ or Unrestricted_Access attribute,
12519 -- or for the actual of a class-wide interface parameter. Note that
12520 -- other checks may still need to be applied below (such as tagged
12523 elsif Is_Entity_Name
(Operand_Acc
)
12524 and then Has_Extra_Accessibility
(Entity
(Operand_Acc
))
12525 and then Ekind
(Etype
(Operand_Acc
)) = E_Anonymous_Access_Type
12526 and then (Nkind
(Original_Node
(N
)) /= N_Attribute_Reference
12527 or else Attribute_Name
(Original_Node
(N
)) = Name_Access
)
12528 and then not No_Dynamic_Accessibility_Checks_Enabled
(N
)
12530 if not Comes_From_Source
(N
)
12531 and then Nkind
(Parent
(N
)) in N_Function_Call
12532 | N_Parameter_Association
12533 | N_Procedure_Call_Statement
12534 and then Is_Interface
(Designated_Type
(Target_Type
))
12535 and then Is_Class_Wide_Type
(Designated_Type
(Target_Type
))
12540 Apply_Accessibility_Check
12541 (Operand
, Target_Type
, Insert_Node
=> Operand
);
12544 -- If the level of the operand type is statically deeper than the
12545 -- level of the target type, then force Program_Error. Note that this
12546 -- can only occur for cases where the attribute is within the body of
12547 -- an instantiation, otherwise the conversion will already have been
12548 -- rejected as illegal.
12550 -- Note: warnings are issued by the analyzer for the instance cases,
12551 -- and, since we are late in expansion, a check is performed to
12552 -- verify that neither the target type nor the operand type are
12553 -- internally generated - as this can lead to spurious errors when,
12554 -- for example, the operand type is a result of BIP expansion.
12556 elsif In_Instance_Body
12557 and then Statically_Deeper_Relation_Applies
(Target_Type
)
12558 and then not Is_Internal
(Target_Type
)
12559 and then not Is_Internal
(Operand_Type
)
12561 Type_Access_Level
(Operand_Type
) > Type_Access_Level
(Target_Type
)
12563 Raise_Accessibility_Error
;
12566 -- When the operand is a selected access discriminant the check needs
12567 -- to be made against the level of the object denoted by the prefix
12568 -- of the selected name. Force Program_Error for this case as well
12569 -- (this accessibility violation can only happen if within the body
12570 -- of an instantiation).
12572 elsif In_Instance_Body
12573 and then Ekind
(Operand_Type
) = E_Anonymous_Access_Type
12574 and then Nkind
(Operand
) = N_Selected_Component
12575 and then Ekind
(Entity
(Selector_Name
(Operand
))) = E_Discriminant
12576 and then Static_Accessibility_Level
(Operand
, Zero_On_Dynamic_Level
)
12577 > Type_Access_Level
(Target_Type
)
12579 Raise_Accessibility_Error
;
12584 -- Case of conversions of tagged types and access to tagged types
12586 -- When needed, that is to say when the expression is class-wide, Add
12587 -- runtime a tag check for (strict) downward conversion by using the
12588 -- membership test, generating:
12590 -- [constraint_error when Operand not in Target_Type'Class]
12592 -- or in the access type case
12594 -- [constraint_error
12595 -- when Operand /= null
12596 -- and then Operand.all not in
12597 -- Designated_Type (Target_Type)'Class]
12599 if (Is_Access_Type
(Target_Type
)
12600 and then Is_Tagged_Type
(Designated_Type
(Target_Type
)))
12601 or else Is_Tagged_Type
(Target_Type
)
12603 -- Do not do any expansion in the access type case if the parent is a
12604 -- renaming, since this is an error situation which will be caught by
12605 -- Sem_Ch8, and the expansion can interfere with this error check.
12607 if Is_Access_Type
(Target_Type
) and then Is_Renamed_Object
(N
) then
12611 -- Otherwise, proceed with processing tagged conversion
12613 Tagged_Conversion
: declare
12614 Actual_Op_Typ
: Entity_Id
;
12615 Actual_Targ_Typ
: Entity_Id
;
12616 Root_Op_Typ
: Entity_Id
;
12618 procedure Make_Tag_Check
(Targ_Typ
: Entity_Id
);
12619 -- Create a membership check to test whether Operand is a member
12620 -- of Targ_Typ. If the original Target_Type is an access, include
12621 -- a test for null value. The check is inserted at N.
12623 --------------------
12624 -- Make_Tag_Check --
12625 --------------------
12627 procedure Make_Tag_Check
(Targ_Typ
: Entity_Id
) is
12632 -- [Constraint_Error
12633 -- when Operand /= null
12634 -- and then Operand.all not in Targ_Typ]
12636 if Is_Access_Type
(Target_Type
) then
12638 Make_And_Then
(Loc
,
12641 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
12642 Right_Opnd
=> Make_Null
(Loc
)),
12647 Make_Explicit_Dereference
(Loc
,
12648 Prefix
=> Duplicate_Subexpr_No_Checks
(Operand
)),
12649 Right_Opnd
=> New_Occurrence_Of
(Targ_Typ
, Loc
)));
12652 -- [Constraint_Error when Operand not in Targ_Typ]
12657 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
12658 Right_Opnd
=> New_Occurrence_Of
(Targ_Typ
, Loc
));
12662 Make_Raise_Constraint_Error
(Loc
,
12664 Reason
=> CE_Tag_Check_Failed
),
12665 Suppress
=> All_Checks
);
12666 end Make_Tag_Check
;
12668 -- Start of processing for Tagged_Conversion
12671 -- Handle entities from the limited view
12673 if Is_Access_Type
(Operand_Type
) then
12675 Available_View
(Designated_Type
(Operand_Type
));
12677 Actual_Op_Typ
:= Operand_Type
;
12680 if Is_Access_Type
(Target_Type
) then
12682 Available_View
(Designated_Type
(Target_Type
));
12684 Actual_Targ_Typ
:= Target_Type
;
12687 Root_Op_Typ
:= Root_Type
(Actual_Op_Typ
);
12689 -- Ada 2005 (AI-251): Handle interface type conversion
12691 if Is_Interface
(Actual_Op_Typ
)
12693 Is_Interface
(Actual_Targ_Typ
)
12695 Expand_Interface_Conversion
(N
);
12699 -- Create a runtime tag check for a downward CW type conversion
12701 if Is_Class_Wide_Type
(Actual_Op_Typ
)
12702 and then Actual_Op_Typ
/= Actual_Targ_Typ
12703 and then Root_Op_Typ
/= Actual_Targ_Typ
12704 and then Is_Ancestor
12705 (Root_Op_Typ
, Actual_Targ_Typ
, Use_Full_View
=> True)
12706 and then not Tag_Checks_Suppressed
(Actual_Targ_Typ
)
12711 Make_Tag_Check
(Class_Wide_Type
(Actual_Targ_Typ
));
12712 Conv
:= Unchecked_Convert_To
(Target_Type
, Expression
(N
));
12714 Analyze_And_Resolve
(N
, Target_Type
);
12717 end Tagged_Conversion
;
12719 -- Case of other access type conversions
12721 elsif Is_Access_Type
(Target_Type
) then
12722 Apply_Constraint_Check
(Operand
, Target_Type
);
12724 -- Case of conversions from a fixed-point type
12726 -- These conversions require special expansion and processing, found in
12727 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
12728 -- since from a semantic point of view, these are simple integer
12729 -- conversions, which do not need further processing except for the
12730 -- generation of range checks, which is performed at the end of this
12733 elsif Is_Fixed_Point_Type
(Operand_Type
)
12734 and then not Conversion_OK
(N
)
12736 -- We should never see universal fixed at this case, since the
12737 -- expansion of the constituent divide or multiply should have
12738 -- eliminated the explicit mention of universal fixed.
12740 pragma Assert
(Operand_Type
/= Universal_Fixed
);
12742 -- Check for special case of the conversion to universal real that
12743 -- occurs as a result of the use of a round attribute. In this case,
12744 -- the real type for the conversion is taken from the target type of
12745 -- the Round attribute and the result must be marked as rounded.
12747 if Target_Type
= Universal_Real
12748 and then Nkind
(Parent
(N
)) = N_Attribute_Reference
12749 and then Attribute_Name
(Parent
(N
)) = Name_Round
12751 Set_Etype
(N
, Etype
(Parent
(N
)));
12752 Target_Type
:= Etype
(N
);
12753 Set_Rounded_Result
(N
);
12756 if Is_Fixed_Point_Type
(Target_Type
) then
12757 Expand_Convert_Fixed_To_Fixed
(N
);
12758 elsif Is_Integer_Type
(Target_Type
) then
12759 Expand_Convert_Fixed_To_Integer
(N
);
12761 pragma Assert
(Is_Floating_Point_Type
(Target_Type
));
12762 Expand_Convert_Fixed_To_Float
(N
);
12765 -- Case of conversions to a fixed-point type
12767 -- These conversions require special expansion and processing, found in
12768 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
12769 -- since from a semantic point of view, these are simple integer
12770 -- conversions, which do not need further processing.
12772 elsif Is_Fixed_Point_Type
(Target_Type
)
12773 and then not Conversion_OK
(N
)
12775 if Is_Integer_Type
(Operand_Type
) then
12776 Expand_Convert_Integer_To_Fixed
(N
);
12778 pragma Assert
(Is_Floating_Point_Type
(Operand_Type
));
12779 Expand_Convert_Float_To_Fixed
(N
);
12782 -- Case of array conversions
12784 -- Expansion of array conversions, add required length/range checks but
12785 -- only do this if there is no change of representation. For handling of
12786 -- this case, see Handle_Changed_Representation.
12788 elsif Is_Array_Type
(Target_Type
) then
12789 if Is_Constrained
(Target_Type
) then
12790 Apply_Length_Check
(Operand
, Target_Type
);
12792 -- If the object has an unconstrained array subtype with fixed
12793 -- lower bound, then sliding to that bound may be needed.
12795 if Is_Fixed_Lower_Bound_Array_Subtype
(Target_Type
) then
12796 Expand_Sliding_Conversion
(Operand
, Target_Type
);
12799 Apply_Range_Check
(Operand
, Target_Type
);
12802 Handle_Changed_Representation
;
12804 -- Case of conversions of discriminated types
12806 -- Add required discriminant checks if target is constrained. Again this
12807 -- change is skipped if we have a change of representation.
12809 elsif Has_Discriminants
(Target_Type
)
12810 and then Is_Constrained
(Target_Type
)
12812 Apply_Discriminant_Check
(Operand
, Target_Type
);
12813 Handle_Changed_Representation
;
12815 -- Case of all other record conversions. The only processing required
12816 -- is to check for a change of representation requiring the special
12817 -- assignment processing.
12819 elsif Is_Record_Type
(Target_Type
) then
12821 -- Ada 2005 (AI-216): Program_Error is raised when converting from
12822 -- a derived Unchecked_Union type to an unconstrained type that is
12823 -- not Unchecked_Union if the operand lacks inferable discriminants.
12825 if Is_Derived_Type
(Operand_Type
)
12826 and then Is_Unchecked_Union
(Base_Type
(Operand_Type
))
12827 and then not Is_Constrained
(Target_Type
)
12828 and then not Is_Unchecked_Union
(Base_Type
(Target_Type
))
12829 and then not Has_Inferable_Discriminants
(Operand
)
12831 -- To prevent Gigi from generating illegal code, we generate a
12832 -- Program_Error node, but we give it the target type of the
12833 -- conversion (is this requirement documented somewhere ???)
12836 PE
: constant Node_Id
:= Make_Raise_Program_Error
(Loc
,
12837 Reason
=> PE_Unchecked_Union_Restriction
);
12840 Set_Etype
(PE
, Target_Type
);
12845 Handle_Changed_Representation
;
12848 -- Case of conversions of enumeration types
12850 elsif Is_Enumeration_Type
(Target_Type
) then
12852 -- Special processing is required if there is a change of
12853 -- representation (from enumeration representation clauses).
12855 if not Has_Compatible_Representation
(Target_Type
, Operand_Type
)
12856 and then not Conversion_OK
(N
)
12858 if Optimization_Level
> 0
12859 and then Is_Boolean_Type
(Target_Type
)
12861 -- Convert x(y) to (if y then x'(True) else x'(False)).
12862 -- Use literals, instead of indexing x'val, to enable
12863 -- further optimizations in the middle-end.
12866 Make_If_Expression
(Loc
,
12867 Expressions
=> New_List
(
12869 Convert_To
(Target_Type
,
12870 New_Occurrence_Of
(Standard_True
, Loc
)),
12871 Convert_To
(Target_Type
,
12872 New_Occurrence_Of
(Standard_False
, Loc
)))));
12875 -- Convert: x(y) to x'val (ytyp'pos (y))
12878 Make_Attribute_Reference
(Loc
,
12879 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
12880 Attribute_Name
=> Name_Val
,
12881 Expressions
=> New_List
(
12882 Make_Attribute_Reference
(Loc
,
12883 Prefix
=> New_Occurrence_Of
(Operand_Type
, Loc
),
12884 Attribute_Name
=> Name_Pos
,
12885 Expressions
=> New_List
(Operand
)))));
12888 Analyze_And_Resolve
(N
, Target_Type
);
12892 -- At this stage, either the conversion node has been transformed into
12893 -- some other equivalent expression, or left as a conversion that can be
12894 -- handled by Gigi.
12896 -- The only remaining step is to generate a range check if we still have
12897 -- a type conversion at this stage and Do_Range_Check is set. Note that
12898 -- we need to deal with at most 8 out of the 9 possible cases of numeric
12899 -- conversions here, because the float-to-integer case is entirely dealt
12900 -- with by Apply_Float_Conversion_Check.
12902 if Nkind
(N
) = N_Type_Conversion
12903 and then Do_Range_Check
(Expression
(N
))
12905 -- Float-to-float conversions
12907 if Is_Floating_Point_Type
(Target_Type
)
12908 and then Is_Floating_Point_Type
(Etype
(Expression
(N
)))
12910 -- Reset overflow flag, since the range check will include
12911 -- dealing with possible overflow, and generate the check.
12913 Set_Do_Overflow_Check
(N
, False);
12915 Generate_Range_Check
12916 (Expression
(N
), Target_Type
, CE_Range_Check_Failed
);
12918 -- Discrete-to-discrete conversions or fixed-point-to-discrete
12919 -- conversions when Conversion_OK is set.
12921 elsif Is_Discrete_Type
(Target_Type
)
12922 and then (Is_Discrete_Type
(Etype
(Expression
(N
)))
12923 or else (Is_Fixed_Point_Type
(Etype
(Expression
(N
)))
12924 and then Conversion_OK
(N
)))
12926 -- If Address is either a source type or target type,
12927 -- suppress range check to avoid typing anomalies when
12928 -- it is a visible integer type.
12930 if Is_Descendant_Of_Address
(Etype
(Expression
(N
)))
12931 or else Is_Descendant_Of_Address
(Target_Type
)
12933 Set_Do_Range_Check
(Expression
(N
), False);
12935 Discrete_Range_Check
;
12938 -- Conversions to floating- or fixed-point when Conversion_OK is set
12940 elsif Is_Floating_Point_Type
(Target_Type
)
12941 or else (Is_Fixed_Point_Type
(Target_Type
)
12942 and then Conversion_OK
(N
))
12947 pragma Assert
(not Do_Range_Check
(Expression
(N
)));
12950 -- Here at end of processing
12953 -- Apply predicate check if required. Note that we can't just call
12954 -- Apply_Predicate_Check here, because the type looks right after
12955 -- the conversion and it would omit the check. The Comes_From_Source
12956 -- guard is necessary to prevent infinite recursions when we generate
12957 -- internal conversions for the purpose of checking predicates.
12959 -- A view conversion of a tagged object is an object and can appear
12960 -- in an assignment context, in which case no predicate check applies
12961 -- to the now-dead value.
12963 if Nkind
(Parent
(N
)) = N_Assignment_Statement
12964 and then N
= Name
(Parent
(N
))
12968 elsif Predicate_Enabled
(Target_Type
)
12969 and then Target_Type
/= Operand_Type
12970 and then Comes_From_Source
(N
)
12973 New_Expr
: constant Node_Id
:= Duplicate_Subexpr
(N
);
12976 -- Avoid infinite recursion on the subsequent expansion of the
12977 -- copy of the original type conversion. When needed, a range
12978 -- check has already been applied to the expression.
12980 Set_Comes_From_Source
(New_Expr
, False);
12982 Make_Predicate_Check
(Target_Type
, New_Expr
),
12983 Suppress
=> Range_Check
);
12986 end Expand_N_Type_Conversion
;
12988 -----------------------------------
12989 -- Expand_N_Unchecked_Expression --
12990 -----------------------------------
12992 -- Remove the unchecked expression node from the tree. Its job was simply
12993 -- to make sure that its constituent expression was handled with checks
12994 -- off, and now that is done, we can remove it from the tree, and indeed
12995 -- must, since Gigi does not expect to see these nodes.
12997 procedure Expand_N_Unchecked_Expression
(N
: Node_Id
) is
12998 Exp
: constant Node_Id
:= Expression
(N
);
13000 Set_Assignment_OK
(Exp
, Assignment_OK
(N
) or else Assignment_OK
(Exp
));
13002 end Expand_N_Unchecked_Expression
;
13004 ----------------------------------------
13005 -- Expand_N_Unchecked_Type_Conversion --
13006 ----------------------------------------
13008 -- If this cannot be handled by Gigi and we haven't already made a
13009 -- temporary for it, do it now.
13011 procedure Expand_N_Unchecked_Type_Conversion
(N
: Node_Id
) is
13012 Target_Type
: constant Entity_Id
:= Etype
(N
);
13013 Operand
: constant Node_Id
:= Expression
(N
);
13014 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
13017 -- Nothing at all to do if conversion is to the identical type so remove
13018 -- the conversion completely, it is useless, except that it may carry
13019 -- an Assignment_OK indication which must be propagated to the operand.
13021 if Operand_Type
= Target_Type
then
13022 Expand_N_Unchecked_Expression
(N
);
13026 -- Generate an extra temporary for cases unsupported by the C backend
13028 if Modify_Tree_For_C
then
13030 Source
: constant Node_Id
:= Unqual_Conv
(Expression
(N
));
13031 Source_Typ
: Entity_Id
:= Get_Full_View
(Etype
(Source
));
13034 if Is_Packed_Array
(Source_Typ
) then
13035 Source_Typ
:= Packed_Array_Impl_Type
(Source_Typ
);
13038 if Nkind
(Source
) = N_Function_Call
13039 and then (Is_Composite_Type
(Etype
(Source
))
13040 or else Is_Composite_Type
(Target_Type
))
13042 Force_Evaluation
(Source
);
13047 -- Nothing to do if conversion is safe
13049 if Safe_Unchecked_Type_Conversion
(N
) then
13053 if Assignment_OK
(N
) then
13056 Force_Evaluation
(N
);
13058 end Expand_N_Unchecked_Type_Conversion
;
13060 ----------------------------
13061 -- Expand_Record_Equality --
13062 ----------------------------
13064 -- For non-variant records, Equality is expanded when needed into:
13066 -- and then Lhs.Discr1 = Rhs.Discr1
13068 -- and then Lhs.Discrn = Rhs.Discrn
13069 -- and then Lhs.Cmp1 = Rhs.Cmp1
13071 -- and then Lhs.Cmpn = Rhs.Cmpn
13073 -- The expression is folded by the back end for adjacent fields. This
13074 -- function is called for tagged record in only one occasion: for imple-
13075 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
13076 -- otherwise the primitive "=" is used directly.
13078 function Expand_Record_Equality
13082 Rhs
: Node_Id
) return Node_Id
13084 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
13089 First_Time
: Boolean := True;
13091 function Element_To_Compare
(C
: Entity_Id
) return Entity_Id
;
13092 -- Return the next discriminant or component to compare, starting with
13093 -- C, skipping inherited components.
13095 ------------------------
13096 -- Element_To_Compare --
13097 ------------------------
13099 function Element_To_Compare
(C
: Entity_Id
) return Entity_Id
is
13100 Comp
: Entity_Id
:= C
;
13103 while Present
(Comp
) loop
13104 -- Skip inherited components
13106 -- Note: for a tagged type, we always generate the "=" primitive
13107 -- for the base type (not on the first subtype), so the test for
13108 -- Comp /= Original_Record_Component (Comp) is True for inherited
13109 -- components only.
13111 if (Is_Tagged_Type
(Typ
)
13112 and then Comp
/= Original_Record_Component
(Comp
))
13116 or else Chars
(Comp
) = Name_uTag
13118 -- Skip interface elements (secondary tags???)
13120 or else Is_Interface
(Etype
(Comp
))
13122 Next_Component_Or_Discriminant
(Comp
);
13129 end Element_To_Compare
;
13131 -- Start of processing for Expand_Record_Equality
13134 -- Generates the following code: (assuming that Typ has one Discr and
13135 -- component C2 is also a record)
13137 -- Lhs.Discr1 = Rhs.Discr1
13138 -- and then Lhs.C1 = Rhs.C1
13139 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
13141 -- and then Lhs.Cmpn = Rhs.Cmpn
13143 Result
:= New_Occurrence_Of
(Standard_True
, Loc
);
13144 C
:= Element_To_Compare
(First_Component_Or_Discriminant
(Typ
));
13145 while Present
(C
) loop
13156 New_Lhs
:= New_Copy_Tree
(Lhs
);
13157 New_Rhs
:= New_Copy_Tree
(Rhs
);
13161 Expand_Composite_Equality
13162 (Outer_Type
=> Typ
, Nod
=> Nod
, Comp_Type
=> Etype
(C
),
13164 Make_Selected_Component
(Loc
,
13166 Selector_Name
=> New_Occurrence_Of
(C
, Loc
)),
13168 Make_Selected_Component
(Loc
,
13170 Selector_Name
=> New_Occurrence_Of
(C
, Loc
)));
13172 -- If some (sub)component is an unchecked_union, the whole
13173 -- operation will raise program error.
13175 if Nkind
(Check
) = N_Raise_Program_Error
then
13177 Set_Etype
(Result
, Standard_Boolean
);
13183 -- Generate logical "and" for CodePeer to simplify the
13184 -- generated code and analysis.
13186 elsif CodePeer_Mode
then
13189 Left_Opnd
=> Result
,
13190 Right_Opnd
=> Check
);
13194 Make_And_Then
(Loc
,
13195 Left_Opnd
=> Result
,
13196 Right_Opnd
=> Check
);
13201 First_Time
:= False;
13202 C
:= Element_To_Compare
(Next_Component_Or_Discriminant
(C
));
13206 end Expand_Record_Equality
;
13208 ---------------------------
13209 -- Expand_Set_Membership --
13210 ---------------------------
13212 procedure Expand_Set_Membership
(N
: Node_Id
) is
13213 Lop
: constant Node_Id
:= Left_Opnd
(N
);
13217 function Make_Cond
(Alt
: Node_Id
) return Node_Id
;
13218 -- If the alternative is a subtype mark, create a simple membership
13219 -- test. Otherwise create an equality test for it.
13225 function Make_Cond
(Alt
: Node_Id
) return Node_Id
is
13227 L
: constant Node_Id
:= New_Copy_Tree
(Lop
);
13228 R
: constant Node_Id
:= Relocate_Node
(Alt
);
13231 if (Is_Entity_Name
(Alt
) and then Is_Type
(Entity
(Alt
)))
13232 or else Nkind
(Alt
) = N_Range
13234 Cond
:= Make_In
(Sloc
(Alt
), Left_Opnd
=> L
, Right_Opnd
=> R
);
13237 Cond
:= Make_Op_Eq
(Sloc
(Alt
), Left_Opnd
=> L
, Right_Opnd
=> R
);
13238 Resolve_Membership_Equality
(Cond
, Etype
(Alt
));
13244 -- Start of processing for Expand_Set_Membership
13247 Remove_Side_Effects
(Lop
);
13249 Alt
:= First
(Alternatives
(N
));
13250 Res
:= Make_Cond
(Alt
);
13253 -- We use left associativity as in the equivalent boolean case. This
13254 -- kind of canonicalization helps the optimizer of the code generator.
13256 while Present
(Alt
) loop
13258 Make_Or_Else
(Sloc
(Alt
),
13260 Right_Opnd
=> Make_Cond
(Alt
));
13265 Analyze_And_Resolve
(N
, Standard_Boolean
);
13266 end Expand_Set_Membership
;
13268 -----------------------------------
13269 -- Expand_Short_Circuit_Operator --
13270 -----------------------------------
13272 -- Deal with special expansion if actions are present for the right operand
13273 -- and deal with optimizing case of arguments being True or False. We also
13274 -- deal with the special case of non-standard boolean values.
13276 procedure Expand_Short_Circuit_Operator
(N
: Node_Id
) is
13277 Loc
: constant Source_Ptr
:= Sloc
(N
);
13278 Typ
: constant Entity_Id
:= Etype
(N
);
13279 Left
: constant Node_Id
:= Left_Opnd
(N
);
13280 Right
: constant Node_Id
:= Right_Opnd
(N
);
13281 LocR
: constant Source_Ptr
:= Sloc
(Right
);
13284 Shortcut_Value
: constant Boolean := Nkind
(N
) = N_Or_Else
;
13285 Shortcut_Ent
: constant Entity_Id
:= Boolean_Literals
(Shortcut_Value
);
13286 -- If Left = Shortcut_Value then Right need not be evaluated
13288 function Make_Test_Expr
(Opnd
: Node_Id
) return Node_Id
;
13289 -- For Opnd a boolean expression, return a Boolean expression equivalent
13290 -- to Opnd /= Shortcut_Value.
13292 function Useful
(Actions
: List_Id
) return Boolean;
13293 -- Return True if Actions is not empty and contains useful nodes to
13296 --------------------
13297 -- Make_Test_Expr --
13298 --------------------
13300 function Make_Test_Expr
(Opnd
: Node_Id
) return Node_Id
is
13302 if Shortcut_Value
then
13303 return Make_Op_Not
(Sloc
(Opnd
), Opnd
);
13307 end Make_Test_Expr
;
13313 function Useful
(Actions
: List_Id
) return Boolean is
13316 if Present
(Actions
) then
13317 L
:= First
(Actions
);
13319 -- For now "useful" means not N_Variable_Reference_Marker.
13320 -- Consider stripping other nodes in the future.
13322 while Present
(L
) loop
13323 if Nkind
(L
) /= N_Variable_Reference_Marker
then
13336 Op_Var
: Entity_Id
;
13337 -- Entity for a temporary variable holding the value of the operator,
13338 -- used for expansion in the case where actions are present.
13340 -- Start of processing for Expand_Short_Circuit_Operator
13343 -- Deal with non-standard booleans
13345 if Is_Boolean_Type
(Typ
) then
13346 Adjust_Condition
(Left
);
13347 Adjust_Condition
(Right
);
13348 Set_Etype
(N
, Standard_Boolean
);
13351 -- Check for cases where left argument is known to be True or False
13353 if Compile_Time_Known_Value
(Left
) then
13355 -- Mark SCO for left condition as compile time known
13357 if Generate_SCO
and then Comes_From_Source
(Left
) then
13358 Set_SCO_Condition
(Left
, Expr_Value_E
(Left
) = Standard_True
);
13361 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
13362 -- Any actions associated with Right will be executed unconditionally
13363 -- and can thus be inserted into the tree unconditionally.
13365 if Expr_Value_E
(Left
) /= Shortcut_Ent
then
13366 if Present
(Actions
(N
)) then
13367 Insert_Actions
(N
, Actions
(N
));
13370 Rewrite
(N
, Right
);
13372 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
13373 -- In this case we can forget the actions associated with Right,
13374 -- since they will never be executed.
13377 Kill_Dead_Code
(Right
);
13378 Kill_Dead_Code
(Actions
(N
));
13379 Rewrite
(N
, New_Occurrence_Of
(Shortcut_Ent
, Loc
));
13382 Adjust_Result_Type
(N
, Typ
);
13386 -- If Actions are present for the right operand, we have to do some
13387 -- special processing. We can't just let these actions filter back into
13388 -- code preceding the short circuit (which is what would have happened
13389 -- if we had not trapped them in the short-circuit form), since they
13390 -- must only be executed if the right operand of the short circuit is
13391 -- executed and not otherwise.
13393 if Useful
(Actions
(N
)) then
13394 Actlist
:= Actions
(N
);
13396 -- The old approach is to expand:
13398 -- left AND THEN right
13402 -- C : Boolean := False;
13410 -- and finally rewrite the operator into a reference to C. Similarly
13411 -- for left OR ELSE right, with negated values. Note that this
13412 -- rewrite causes some difficulties for coverage analysis because
13413 -- of the introduction of the new variable C, which obscures the
13414 -- structure of the test.
13416 -- We use this "old approach" if Minimize_Expression_With_Actions
13419 if Minimize_Expression_With_Actions
then
13420 Op_Var
:= Make_Temporary
(Loc
, 'C', Related_Node
=> N
);
13423 Make_Object_Declaration
(Loc
,
13424 Defining_Identifier
=> Op_Var
,
13425 Object_Definition
=>
13426 New_Occurrence_Of
(Standard_Boolean
, Loc
),
13428 New_Occurrence_Of
(Shortcut_Ent
, Loc
)));
13430 Append_To
(Actlist
,
13431 Make_Implicit_If_Statement
(Right
,
13432 Condition
=> Make_Test_Expr
(Right
),
13433 Then_Statements
=> New_List
(
13434 Make_Assignment_Statement
(LocR
,
13435 Name
=> New_Occurrence_Of
(Op_Var
, LocR
),
13438 (Boolean_Literals
(not Shortcut_Value
), LocR
)))));
13441 Make_Implicit_If_Statement
(Left
,
13442 Condition
=> Make_Test_Expr
(Left
),
13443 Then_Statements
=> Actlist
));
13445 Rewrite
(N
, New_Occurrence_Of
(Op_Var
, Loc
));
13446 Analyze_And_Resolve
(N
, Standard_Boolean
);
13448 -- The new approach (the default) is to use an
13449 -- Expression_With_Actions node for the right operand of the
13450 -- short-circuit form. Note that this solves the traceability
13451 -- problems for coverage analysis.
13455 Make_Expression_With_Actions
(LocR
,
13456 Expression
=> Relocate_Node
(Right
),
13457 Actions
=> Actlist
));
13459 Set_Actions
(N
, No_List
);
13460 Analyze_And_Resolve
(Right
, Standard_Boolean
);
13463 Adjust_Result_Type
(N
, Typ
);
13467 -- No actions present, check for cases of right argument True/False
13469 if Compile_Time_Known_Value
(Right
) then
13471 -- Mark SCO for left condition as compile time known
13473 if Generate_SCO
and then Comes_From_Source
(Right
) then
13474 Set_SCO_Condition
(Right
, Expr_Value_E
(Right
) = Standard_True
);
13477 -- Change (Left and then True), (Left or else False) to Left. Note
13478 -- that we know there are no actions associated with the right
13479 -- operand, since we just checked for this case above.
13481 if Expr_Value_E
(Right
) /= Shortcut_Ent
then
13484 -- Change (Left and then False), (Left or else True) to Right,
13485 -- making sure to preserve any side effects associated with the Left
13489 Remove_Side_Effects
(Left
);
13490 Rewrite
(N
, New_Occurrence_Of
(Shortcut_Ent
, Loc
));
13494 Adjust_Result_Type
(N
, Typ
);
13495 end Expand_Short_Circuit_Operator
;
13497 ------------------------------------
13498 -- Fixup_Universal_Fixed_Operation --
13499 -------------------------------------
13501 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
) is
13502 Conv
: constant Node_Id
:= Parent
(N
);
13505 -- We must have a type conversion immediately above us
13507 pragma Assert
(Nkind
(Conv
) = N_Type_Conversion
);
13509 -- Normally the type conversion gives our target type. The exception
13510 -- occurs in the case of the Round attribute, where the conversion
13511 -- will be to universal real, and our real type comes from the Round
13512 -- attribute (as well as an indication that we must round the result)
13514 if Etype
(Conv
) = Universal_Real
13515 and then Nkind
(Parent
(Conv
)) = N_Attribute_Reference
13516 and then Attribute_Name
(Parent
(Conv
)) = Name_Round
13518 Set_Etype
(N
, Base_Type
(Etype
(Parent
(Conv
))));
13519 Set_Rounded_Result
(N
);
13521 -- Normal case where type comes from conversion above us
13524 Set_Etype
(N
, Base_Type
(Etype
(Conv
)));
13526 end Fixup_Universal_Fixed_Operation
;
13528 ----------------------------
13529 -- Get_First_Index_Bounds --
13530 ----------------------------
13532 procedure Get_First_Index_Bounds
(T
: Entity_Id
; Lo
, Hi
: out Uint
) is
13536 pragma Assert
(Is_Array_Type
(T
));
13538 -- This follows Sem_Eval.Compile_Time_Known_Bounds
13540 if Ekind
(T
) = E_String_Literal_Subtype
then
13541 Lo
:= Expr_Value
(String_Literal_Low_Bound
(T
));
13542 Hi
:= Lo
+ String_Literal_Length
(T
) - 1;
13545 Typ
:= Underlying_Type
(Etype
(First_Index
(T
)));
13547 Lo
:= Expr_Value
(Type_Low_Bound
(Typ
));
13548 Hi
:= Expr_Value
(Type_High_Bound
(Typ
));
13550 end Get_First_Index_Bounds
;
13552 ------------------------
13553 -- Get_Size_For_Range --
13554 ------------------------
13556 function Get_Size_For_Range
(Lo
, Hi
: Uint
) return Uint
is
13558 function Is_OK_For_Range
(Siz
: Uint
) return Boolean;
13559 -- Return True if a signed integer with given size can cover Lo .. Hi
13561 --------------------------
13562 -- Is_OK_For_Range --
13563 --------------------------
13565 function Is_OK_For_Range
(Siz
: Uint
) return Boolean is
13566 B
: constant Uint
:= Uint_2
** (Siz
- 1);
13569 -- Test B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
13571 return Lo
>= -B
and then Hi
>= -B
and then Lo
< B
and then Hi
< B
;
13572 end Is_OK_For_Range
;
13575 -- This is (almost always) the size of Integer
13577 if Is_OK_For_Range
(Uint_32
) then
13582 elsif Is_OK_For_Range
(Uint_63
) then
13585 -- This is (almost always) the size of Long_Long_Integer
13587 elsif Is_OK_For_Range
(Uint_64
) then
13592 elsif Is_OK_For_Range
(Uint_127
) then
13598 end Get_Size_For_Range
;
13600 -------------------------------
13601 -- Insert_Dereference_Action --
13602 -------------------------------
13604 procedure Insert_Dereference_Action
(N
: Node_Id
) is
13605 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean;
13606 -- Return true if type of P is derived from Checked_Pool;
13608 -----------------------------
13609 -- Is_Checked_Storage_Pool --
13610 -----------------------------
13612 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean is
13621 while T
/= Etype
(T
) loop
13622 if Is_RTE
(T
, RE_Checked_Pool
) then
13630 end Is_Checked_Storage_Pool
;
13634 Context
: constant Node_Id
:= Parent
(N
);
13635 Ptr_Typ
: constant Entity_Id
:= Etype
(N
);
13636 Desig_Typ
: constant Entity_Id
:=
13637 Available_View
(Designated_Type
(Ptr_Typ
));
13638 Loc
: constant Source_Ptr
:= Sloc
(N
);
13639 Pool
: constant Entity_Id
:= Associated_Storage_Pool
(Ptr_Typ
);
13645 Size_Bits
: Node_Id
;
13648 -- Start of processing for Insert_Dereference_Action
13651 pragma Assert
(Nkind
(Context
) = N_Explicit_Dereference
);
13653 -- Do not re-expand a dereference which has already been processed by
13656 if Has_Dereference_Action
(Context
) then
13659 -- Do not perform this type of expansion for internally-generated
13662 elsif not Comes_From_Source
(Original_Node
(Context
)) then
13665 -- A dereference action is only applicable to objects which have been
13666 -- allocated on a checked pool.
13668 elsif not Is_Checked_Storage_Pool
(Pool
) then
13672 -- Extract the address of the dereferenced object. Generate:
13674 -- Addr : System.Address := <N>'Pool_Address;
13676 Addr
:= Make_Temporary
(Loc
, 'P');
13679 Make_Object_Declaration
(Loc
,
13680 Defining_Identifier
=> Addr
,
13681 Object_Definition
=>
13682 New_Occurrence_Of
(RTE
(RE_Address
), Loc
),
13684 Make_Attribute_Reference
(Loc
,
13685 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
),
13686 Attribute_Name
=> Name_Pool_Address
)));
13688 -- Calculate the size of the dereferenced object. Generate:
13690 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
13693 Make_Explicit_Dereference
(Loc
,
13694 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
13695 Set_Has_Dereference_Action
(Deref
);
13698 Make_Attribute_Reference
(Loc
,
13700 Attribute_Name
=> Name_Size
);
13702 -- Special case of an unconstrained array: need to add descriptor size
13704 if Is_Array_Type
(Desig_Typ
)
13705 and then not Is_Constrained
(First_Subtype
(Desig_Typ
))
13710 Make_Attribute_Reference
(Loc
,
13712 New_Occurrence_Of
(First_Subtype
(Desig_Typ
), Loc
),
13713 Attribute_Name
=> Name_Descriptor_Size
),
13714 Right_Opnd
=> Size_Bits
);
13717 Size
:= Make_Temporary
(Loc
, 'S');
13719 Make_Object_Declaration
(Loc
,
13720 Defining_Identifier
=> Size
,
13721 Object_Definition
=>
13722 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
),
13724 Make_Op_Divide
(Loc
,
13725 Left_Opnd
=> Size_Bits
,
13726 Right_Opnd
=> Make_Integer_Literal
(Loc
, System_Storage_Unit
))));
13728 -- Calculate the alignment of the dereferenced object. Generate:
13729 -- Alig : constant Storage_Count := <N>.all'Alignment;
13732 Make_Explicit_Dereference
(Loc
,
13733 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
13734 Set_Has_Dereference_Action
(Deref
);
13736 Alig
:= Make_Temporary
(Loc
, 'A');
13738 Make_Object_Declaration
(Loc
,
13739 Defining_Identifier
=> Alig
,
13740 Object_Definition
=>
13741 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
),
13743 Make_Attribute_Reference
(Loc
,
13745 Attribute_Name
=> Name_Alignment
)));
13747 -- A dereference of a controlled object requires special processing. The
13748 -- finalization machinery requests additional space from the underlying
13749 -- pool to allocate and hide two pointers. As a result, a checked pool
13750 -- may mark the wrong memory as valid. Since checked pools do not have
13751 -- knowledge of hidden pointers, we have to bring the two pointers back
13752 -- in view in order to restore the original state of the object.
13754 -- The address manipulation is not performed for access types that are
13755 -- subject to pragma No_Heap_Finalization because the two pointers do
13756 -- not exist in the first place.
13758 if No_Heap_Finalization
(Ptr_Typ
) then
13761 elsif Needs_Finalization
(Desig_Typ
) then
13763 -- Adjust the address and size of the dereferenced object. Generate:
13764 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
13767 Make_Procedure_Call_Statement
(Loc
,
13769 New_Occurrence_Of
(RTE
(RE_Adjust_Controlled_Dereference
), Loc
),
13770 Parameter_Associations
=> New_List
(
13771 New_Occurrence_Of
(Addr
, Loc
),
13772 New_Occurrence_Of
(Size
, Loc
),
13773 New_Occurrence_Of
(Alig
, Loc
)));
13775 -- Class-wide types complicate things because we cannot determine
13776 -- statically whether the actual object is truly controlled. We must
13777 -- generate a runtime check to detect this property. Generate:
13779 -- if Needs_Finalization (<N>.all'Tag) then
13783 if Is_Class_Wide_Type
(Desig_Typ
) then
13785 Make_Explicit_Dereference
(Loc
,
13786 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
13787 Set_Has_Dereference_Action
(Deref
);
13790 Make_Implicit_If_Statement
(N
,
13792 Make_Function_Call
(Loc
,
13794 New_Occurrence_Of
(RTE
(RE_Needs_Finalization
), Loc
),
13795 Parameter_Associations
=> New_List
(
13796 Make_Attribute_Reference
(Loc
,
13798 Attribute_Name
=> Name_Tag
))),
13799 Then_Statements
=> New_List
(Stmt
));
13802 Insert_Action
(N
, Stmt
);
13806 -- Dereference (Pool, Addr, Size, Alig);
13809 Make_Procedure_Call_Statement
(Loc
,
13812 (Find_Prim_Op
(Etype
(Pool
), Name_Dereference
), Loc
),
13813 Parameter_Associations
=> New_List
(
13814 New_Occurrence_Of
(Pool
, Loc
),
13815 New_Occurrence_Of
(Addr
, Loc
),
13816 New_Occurrence_Of
(Size
, Loc
),
13817 New_Occurrence_Of
(Alig
, Loc
))));
13819 -- Mark the explicit dereference as processed to avoid potential
13820 -- infinite expansion.
13822 Set_Has_Dereference_Action
(Context
);
13825 when RE_Not_Available
=>
13827 end Insert_Dereference_Action
;
13829 --------------------------------
13830 -- Integer_Promotion_Possible --
13831 --------------------------------
13833 function Integer_Promotion_Possible
(N
: Node_Id
) return Boolean is
13834 Operand
: constant Node_Id
:= Expression
(N
);
13835 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
13836 Root_Operand_Type
: constant Entity_Id
:= Root_Type
(Operand_Type
);
13839 pragma Assert
(Nkind
(N
) = N_Type_Conversion
);
13843 -- We only do the transformation for source constructs. We assume
13844 -- that the expander knows what it is doing when it generates code.
13846 Comes_From_Source
(N
)
13848 -- If the operand type is Short_Integer or Short_Short_Integer,
13849 -- then we will promote to Integer, which is available on all
13850 -- targets, and is sufficient to ensure no intermediate overflow.
13851 -- Furthermore it is likely to be as efficient or more efficient
13852 -- than using the smaller type for the computation so we do this
13853 -- unconditionally.
13856 (Root_Operand_Type
= Base_Type
(Standard_Short_Integer
)
13858 Root_Operand_Type
= Base_Type
(Standard_Short_Short_Integer
))
13860 -- Test for interesting operation, which includes addition,
13861 -- division, exponentiation, multiplication, subtraction, absolute
13862 -- value and unary negation. Unary "+" is omitted since it is a
13863 -- no-op and thus can't overflow.
13865 and then Nkind
(Operand
) in
13866 N_Op_Abs | N_Op_Add | N_Op_Divide | N_Op_Expon |
13867 N_Op_Minus | N_Op_Multiply | N_Op_Subtract
;
13868 end Integer_Promotion_Possible
;
13870 ------------------------------
13871 -- Make_Array_Comparison_Op --
13872 ------------------------------
13874 -- This is a hand-coded expansion of the following generic function:
13877 -- type elem is (<>);
13878 -- type index is (<>);
13879 -- type a is array (index range <>) of elem;
13881 -- function Gnnn (X : a; Y: a) return boolean is
13882 -- J : index := Y'first;
13885 -- if X'length = 0 then
13888 -- elsif Y'length = 0 then
13892 -- for I in X'range loop
13893 -- if X (I) = Y (J) then
13894 -- if J = Y'last then
13897 -- J := index'succ (J);
13901 -- return X (I) > Y (J);
13905 -- return X'length > Y'length;
13909 -- Note that since we are essentially doing this expansion by hand, we
13910 -- do not need to generate an actual or formal generic part, just the
13911 -- instantiated function itself.
13913 function Make_Array_Comparison_Op
13915 Nod
: Node_Id
) return Node_Id
13917 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
13919 X
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uX
);
13920 Y
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uY
);
13921 I
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uI
);
13922 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
13924 Index
: constant Entity_Id
:= Base_Type
(Etype
(First_Index
(Typ
)));
13926 Loop_Statement
: Node_Id
;
13927 Loop_Body
: Node_Id
;
13929 Inner_If
: Node_Id
;
13930 Final_Expr
: Node_Id
;
13931 Func_Body
: Node_Id
;
13932 Func_Name
: Entity_Id
;
13938 -- if J = Y'last then
13941 -- J := index'succ (J);
13945 Make_Implicit_If_Statement
(Nod
,
13948 Left_Opnd
=> New_Occurrence_Of
(J
, Loc
),
13950 Make_Attribute_Reference
(Loc
,
13951 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
13952 Attribute_Name
=> Name_Last
)),
13954 Then_Statements
=> New_List
(
13955 Make_Exit_Statement
(Loc
)),
13959 Make_Assignment_Statement
(Loc
,
13960 Name
=> New_Occurrence_Of
(J
, Loc
),
13962 Make_Attribute_Reference
(Loc
,
13963 Prefix
=> New_Occurrence_Of
(Index
, Loc
),
13964 Attribute_Name
=> Name_Succ
,
13965 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
))))));
13967 -- if X (I) = Y (J) then
13970 -- return X (I) > Y (J);
13974 Make_Implicit_If_Statement
(Nod
,
13978 Make_Indexed_Component
(Loc
,
13979 Prefix
=> New_Occurrence_Of
(X
, Loc
),
13980 Expressions
=> New_List
(New_Occurrence_Of
(I
, Loc
))),
13983 Make_Indexed_Component
(Loc
,
13984 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
13985 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)))),
13987 Then_Statements
=> New_List
(Inner_If
),
13989 Else_Statements
=> New_List
(
13990 Make_Simple_Return_Statement
(Loc
,
13994 Make_Indexed_Component
(Loc
,
13995 Prefix
=> New_Occurrence_Of
(X
, Loc
),
13996 Expressions
=> New_List
(New_Occurrence_Of
(I
, Loc
))),
13999 Make_Indexed_Component
(Loc
,
14000 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
14001 Expressions
=> New_List
(
14002 New_Occurrence_Of
(J
, Loc
)))))));
14004 -- for I in X'range loop
14009 Make_Implicit_Loop_Statement
(Nod
,
14010 Identifier
=> Empty
,
14012 Iteration_Scheme
=>
14013 Make_Iteration_Scheme
(Loc
,
14014 Loop_Parameter_Specification
=>
14015 Make_Loop_Parameter_Specification
(Loc
,
14016 Defining_Identifier
=> I
,
14017 Discrete_Subtype_Definition
=>
14018 Make_Attribute_Reference
(Loc
,
14019 Prefix
=> New_Occurrence_Of
(X
, Loc
),
14020 Attribute_Name
=> Name_Range
))),
14022 Statements
=> New_List
(Loop_Body
));
14024 -- if X'length = 0 then
14026 -- elsif Y'length = 0 then
14029 -- for ... loop ... end loop;
14030 -- return X'length > Y'length;
14034 Make_Attribute_Reference
(Loc
,
14035 Prefix
=> New_Occurrence_Of
(X
, Loc
),
14036 Attribute_Name
=> Name_Length
);
14039 Make_Attribute_Reference
(Loc
,
14040 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
14041 Attribute_Name
=> Name_Length
);
14045 Left_Opnd
=> Length1
,
14046 Right_Opnd
=> Length2
);
14049 Make_Implicit_If_Statement
(Nod
,
14053 Make_Attribute_Reference
(Loc
,
14054 Prefix
=> New_Occurrence_Of
(X
, Loc
),
14055 Attribute_Name
=> Name_Length
),
14057 Make_Integer_Literal
(Loc
, 0)),
14061 Make_Simple_Return_Statement
(Loc
,
14062 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))),
14064 Elsif_Parts
=> New_List
(
14065 Make_Elsif_Part
(Loc
,
14069 Make_Attribute_Reference
(Loc
,
14070 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
14071 Attribute_Name
=> Name_Length
),
14073 Make_Integer_Literal
(Loc
, 0)),
14077 Make_Simple_Return_Statement
(Loc
,
14078 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
))))),
14080 Else_Statements
=> New_List
(
14082 Make_Simple_Return_Statement
(Loc
,
14083 Expression
=> Final_Expr
)));
14087 Formals
:= New_List
(
14088 Make_Parameter_Specification
(Loc
,
14089 Defining_Identifier
=> X
,
14090 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)),
14092 Make_Parameter_Specification
(Loc
,
14093 Defining_Identifier
=> Y
,
14094 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)));
14096 -- function Gnnn (...) return boolean is
14097 -- J : index := Y'first;
14102 Func_Name
:= Make_Temporary
(Loc
, 'G');
14105 Make_Subprogram_Body
(Loc
,
14107 Make_Function_Specification
(Loc
,
14108 Defining_Unit_Name
=> Func_Name
,
14109 Parameter_Specifications
=> Formals
,
14110 Result_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
)),
14112 Declarations
=> New_List
(
14113 Make_Object_Declaration
(Loc
,
14114 Defining_Identifier
=> J
,
14115 Object_Definition
=> New_Occurrence_Of
(Index
, Loc
),
14117 Make_Attribute_Reference
(Loc
,
14118 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
14119 Attribute_Name
=> Name_First
))),
14121 Handled_Statement_Sequence
=>
14122 Make_Handled_Sequence_Of_Statements
(Loc
,
14123 Statements
=> New_List
(If_Stat
)));
14126 end Make_Array_Comparison_Op
;
14128 ---------------------------
14129 -- Make_Boolean_Array_Op --
14130 ---------------------------
14132 -- For logical operations on boolean arrays, expand in line the following,
14133 -- replacing 'and' with 'or' or 'xor' where needed:
14135 -- function Annn (A : typ; B: typ) return typ is
14138 -- for J in A'range loop
14139 -- C (J) := A (J) op B (J);
14144 -- or in the case of Transform_Function_Array:
14146 -- procedure Annn (A : typ; B: typ; RESULT: out typ) is
14148 -- for J in A'range loop
14149 -- RESULT (J) := A (J) op B (J);
14153 -- Here typ is the boolean array type
14155 function Make_Boolean_Array_Op
14157 N
: Node_Id
) return Node_Id
14159 Loc
: constant Source_Ptr
:= Sloc
(N
);
14161 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
14162 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
14163 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
14173 Func_Name
: Entity_Id
;
14174 Func_Body
: Node_Id
;
14175 Loop_Statement
: Node_Id
;
14178 if Transform_Function_Array
then
14179 C
:= Make_Defining_Identifier
(Loc
, Name_UP_RESULT
);
14181 C
:= Make_Defining_Identifier
(Loc
, Name_uC
);
14185 Make_Indexed_Component
(Loc
,
14186 Prefix
=> New_Occurrence_Of
(A
, Loc
),
14187 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
14190 Make_Indexed_Component
(Loc
,
14191 Prefix
=> New_Occurrence_Of
(B
, Loc
),
14192 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
14195 Make_Indexed_Component
(Loc
,
14196 Prefix
=> New_Occurrence_Of
(C
, Loc
),
14197 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
14199 if Nkind
(N
) = N_Op_And
then
14203 Right_Opnd
=> B_J
);
14205 elsif Nkind
(N
) = N_Op_Or
then
14209 Right_Opnd
=> B_J
);
14215 Right_Opnd
=> B_J
);
14219 Make_Implicit_Loop_Statement
(N
,
14220 Identifier
=> Empty
,
14222 Iteration_Scheme
=>
14223 Make_Iteration_Scheme
(Loc
,
14224 Loop_Parameter_Specification
=>
14225 Make_Loop_Parameter_Specification
(Loc
,
14226 Defining_Identifier
=> J
,
14227 Discrete_Subtype_Definition
=>
14228 Make_Attribute_Reference
(Loc
,
14229 Prefix
=> New_Occurrence_Of
(A
, Loc
),
14230 Attribute_Name
=> Name_Range
))),
14232 Statements
=> New_List
(
14233 Make_Assignment_Statement
(Loc
,
14235 Expression
=> Op
)));
14237 Formals
:= New_List
(
14238 Make_Parameter_Specification
(Loc
,
14239 Defining_Identifier
=> A
,
14240 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)),
14242 Make_Parameter_Specification
(Loc
,
14243 Defining_Identifier
=> B
,
14244 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)));
14246 if Transform_Function_Array
then
14247 Append_To
(Formals
,
14248 Make_Parameter_Specification
(Loc
,
14249 Defining_Identifier
=> C
,
14250 Out_Present
=> True,
14251 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)));
14254 Func_Name
:= Make_Temporary
(Loc
, 'A');
14255 Set_Is_Inlined
(Func_Name
);
14257 if Transform_Function_Array
then
14259 Make_Subprogram_Body
(Loc
,
14261 Make_Procedure_Specification
(Loc
,
14262 Defining_Unit_Name
=> Func_Name
,
14263 Parameter_Specifications
=> Formals
),
14265 Declarations
=> New_List
,
14267 Handled_Statement_Sequence
=>
14268 Make_Handled_Sequence_Of_Statements
(Loc
,
14269 Statements
=> New_List
(Loop_Statement
)));
14273 Make_Subprogram_Body
(Loc
,
14275 Make_Function_Specification
(Loc
,
14276 Defining_Unit_Name
=> Func_Name
,
14277 Parameter_Specifications
=> Formals
,
14278 Result_Definition
=> New_Occurrence_Of
(Typ
, Loc
)),
14280 Declarations
=> New_List
(
14281 Make_Object_Declaration
(Loc
,
14282 Defining_Identifier
=> C
,
14283 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
))),
14285 Handled_Statement_Sequence
=>
14286 Make_Handled_Sequence_Of_Statements
(Loc
,
14287 Statements
=> New_List
(
14289 Make_Simple_Return_Statement
(Loc
,
14290 Expression
=> New_Occurrence_Of
(C
, Loc
)))));
14294 end Make_Boolean_Array_Op
;
14296 -----------------------------------------
14297 -- Minimized_Eliminated_Overflow_Check --
14298 -----------------------------------------
14300 function Minimized_Eliminated_Overflow_Check
(N
: Node_Id
) return Boolean is
14302 -- The MINIMIZED mode operates in Long_Long_Integer so we cannot use it
14303 -- if the type of the expression is already larger.
14306 Is_Signed_Integer_Type
(Etype
(N
))
14307 and then Overflow_Check_Mode
in Minimized_Or_Eliminated
14308 and then not (Overflow_Check_Mode
= Minimized
14310 Esize
(Etype
(N
)) > Standard_Long_Long_Integer_Size
);
14311 end Minimized_Eliminated_Overflow_Check
;
14313 ----------------------------
14314 -- Narrow_Large_Operation --
14315 ----------------------------
14317 procedure Narrow_Large_Operation
(N
: Node_Id
) is
14318 Kind
: constant Node_Kind
:= Nkind
(N
);
14319 Otyp
: constant Entity_Id
:= Etype
(N
);
14320 In_Rng
: constant Boolean := Kind
= N_In
;
14321 Binary
: constant Boolean := Kind
in N_Binary_Op
or else In_Rng
;
14322 Compar
: constant Boolean := Kind
in N_Op_Compare
or else In_Rng
;
14323 R
: constant Node_Id
:= Right_Opnd
(N
);
14324 Typ
: constant Entity_Id
:= Etype
(R
);
14325 Tsiz
: constant Uint
:= RM_Size
(Typ
);
14339 -- Start of processing for Narrow_Large_Operation
14342 -- First, determine the range of the left operand, if any
14345 L
:= Left_Opnd
(N
);
14346 Determine_Range
(L
, OK
, Llo
, Lhi
, Assume_Valid
=> True);
14357 -- Second, determine the range of the right operand, which can itself
14358 -- be a range, in which case we take the lower bound of the low bound
14359 -- and the upper bound of the high bound.
14367 (Low_Bound
(R
), OK
, Rlo
, Zhi
, Assume_Valid
=> True);
14373 (High_Bound
(R
), OK
, Zlo
, Rhi
, Assume_Valid
=> True);
14380 Determine_Range
(R
, OK
, Rlo
, Rhi
, Assume_Valid
=> True);
14386 -- Then compute a size suitable for each range
14389 Lsiz
:= Get_Size_For_Range
(Llo
, Lhi
);
14394 Rsiz
:= Get_Size_For_Range
(Rlo
, Rhi
);
14396 -- Now compute the size of the narrower type
14399 -- The type must be able to accommodate the operands
14401 Nsiz
:= UI_Max
(Lsiz
, Rsiz
);
14404 -- The type must be able to accommodate the operand(s) and result.
14406 -- Note that Determine_Range typically does not report the bounds of
14407 -- the value as being larger than those of the base type, which means
14408 -- that it does not report overflow (see also Enable_Overflow_Check).
14410 Determine_Range
(N
, OK
, Nlo
, Nhi
, Assume_Valid
=> True);
14415 -- Therefore, if Nsiz is not lower than the size of the original type
14416 -- here, we cannot be sure that the operation does not overflow.
14418 Nsiz
:= Get_Size_For_Range
(Nlo
, Nhi
);
14419 Nsiz
:= UI_Max
(Nsiz
, Lsiz
);
14420 Nsiz
:= UI_Max
(Nsiz
, Rsiz
);
14423 -- If the size is not lower than the size of the original type, then
14424 -- there is no point in changing the type, except in the case where
14425 -- we can remove a conversion to the original type from an operand.
14428 and then not (Binary
14429 and then Nkind
(L
) = N_Type_Conversion
14430 and then Entity
(Subtype_Mark
(L
)) = Typ
)
14431 and then not (Nkind
(R
) = N_Type_Conversion
14432 and then Entity
(Subtype_Mark
(R
)) = Typ
)
14437 -- Now pick the narrower type according to the size. We use the base
14438 -- type instead of the first subtype because operations are done in
14439 -- the base type, so this avoids the need for useless conversions.
14441 if Nsiz
<= System_Max_Integer_Size
then
14442 Ntyp
:= Etype
(Integer_Type_For
(Nsiz
, Uns
=> False));
14447 -- Finally, rewrite the operation in the narrower type, but make sure
14448 -- not to perform name resolution for the operator again.
14450 Nop
:= New_Op_Node
(Kind
, Sloc
(N
));
14451 if Nkind
(N
) in N_Has_Entity
then
14452 Set_Entity
(Nop
, Entity
(N
));
14456 Set_Left_Opnd
(Nop
, Convert_To
(Ntyp
, L
));
14460 Set_Right_Opnd
(Nop
,
14461 Make_Range
(Sloc
(N
),
14462 Convert_To
(Ntyp
, Low_Bound
(R
)),
14463 Convert_To
(Ntyp
, High_Bound
(R
))));
14465 Set_Right_Opnd
(Nop
, Convert_To
(Ntyp
, R
));
14471 -- Analyze it with the comparison type and checks suppressed since
14472 -- the conversions of the operands cannot overflow.
14474 Analyze_And_Resolve
(N
, Otyp
, Suppress
=> Overflow_Check
);
14477 -- Analyze it with the narrower type and checks suppressed, but only
14478 -- when we are sure that the operation does not overflow, see above.
14480 if Nsiz
< Tsiz
then
14481 Analyze_And_Resolve
(N
, Ntyp
, Suppress
=> Overflow_Check
);
14483 Analyze_And_Resolve
(N
, Ntyp
);
14486 -- Put back a conversion to the original type
14488 Convert_To_And_Rewrite
(Typ
, N
);
14490 end Narrow_Large_Operation
;
14492 --------------------------------
14493 -- Optimize_Length_Comparison --
14494 --------------------------------
14496 procedure Optimize_Length_Comparison
(N
: Node_Id
) is
14497 Loc
: constant Source_Ptr
:= Sloc
(N
);
14498 Typ
: constant Entity_Id
:= Etype
(N
);
14503 -- First and Last attribute reference nodes, which end up as left and
14504 -- right operands of the optimized result.
14507 -- True for comparison operand of zero
14509 Maybe_Superflat
: Boolean;
14510 -- True if we may be in the dynamic superflat case, i.e. Is_Zero is set
14511 -- to false but the comparison operand can be zero at run time. In this
14512 -- case, we normally cannot do anything because the canonical formula of
14513 -- the length is not valid, but there is one exception: when the operand
14514 -- is itself the length of an array with the same bounds as the array on
14515 -- the LHS, we can entirely optimize away the comparison.
14518 -- Comparison operand, set only if Is_Zero is false
14520 Ent
: array (Pos
range 1 .. 2) of Entity_Id
:= (Empty
, Empty
);
14521 -- Entities whose length is being compared
14523 Index
: array (Pos
range 1 .. 2) of Node_Id
:= (Empty
, Empty
);
14524 -- Integer_Literal nodes for length attribute expressions, or Empty
14525 -- if there is no such expression present.
14527 Op
: Node_Kind
:= Nkind
(N
);
14528 -- Kind of comparison operator, gets flipped if operands backwards
14530 function Convert_To_Long_Long_Integer
(N
: Node_Id
) return Node_Id
;
14531 -- Given a discrete expression, returns a Long_Long_Integer typed
14532 -- expression representing the underlying value of the expression.
14533 -- This is done with an unchecked conversion to Long_Long_Integer.
14534 -- We use unchecked conversion to handle the enumeration type case.
14536 function Is_Entity_Length
(N
: Node_Id
; Num
: Pos
) return Boolean;
14537 -- Tests if N is a length attribute applied to a simple entity. If so,
14538 -- returns True, and sets Ent to the entity, and Index to the integer
14539 -- literal provided as an attribute expression, or to Empty if none.
14540 -- Num is the index designating the relevant slot in Ent and Index.
14541 -- Also returns True if the expression is a generated type conversion
14542 -- whose expression is of the desired form. This latter case arises
14543 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
14544 -- to check for being in range, which is not needed in this context.
14545 -- Returns False if neither condition holds.
14547 function Is_Optimizable
(N
: Node_Id
) return Boolean;
14548 -- Tests N to see if it is an optimizable comparison value (defined as
14549 -- constant zero or one, or something else where the value is known to
14550 -- be nonnegative and in the 32-bit range and where the corresponding
14551 -- Length value is also known to be 32 bits). If result is true, sets
14552 -- Is_Zero, Maybe_Superflat and Comp accordingly.
14554 procedure Rewrite_For_Equal_Lengths
;
14555 -- Rewrite the comparison of two equal lengths into either True or False
14557 ----------------------------------
14558 -- Convert_To_Long_Long_Integer --
14559 ----------------------------------
14561 function Convert_To_Long_Long_Integer
(N
: Node_Id
) return Node_Id
is
14563 return Unchecked_Convert_To
(Standard_Long_Long_Integer
, N
);
14564 end Convert_To_Long_Long_Integer
;
14566 ----------------------
14567 -- Is_Entity_Length --
14568 ----------------------
14570 function Is_Entity_Length
(N
: Node_Id
; Num
: Pos
) return Boolean is
14572 if Nkind
(N
) = N_Attribute_Reference
14573 and then Attribute_Name
(N
) = Name_Length
14574 and then Is_Entity_Name
(Prefix
(N
))
14576 Ent
(Num
) := Entity
(Prefix
(N
));
14578 if Present
(Expressions
(N
)) then
14579 Index
(Num
) := First
(Expressions
(N
));
14581 Index
(Num
) := Empty
;
14586 elsif Nkind
(N
) = N_Type_Conversion
14587 and then not Comes_From_Source
(N
)
14589 return Is_Entity_Length
(Expression
(N
), Num
);
14594 end Is_Entity_Length
;
14596 --------------------
14597 -- Is_Optimizable --
14598 --------------------
14600 function Is_Optimizable
(N
: Node_Id
) return Boolean is
14610 if Compile_Time_Known_Value
(N
) then
14611 Val
:= Expr_Value
(N
);
14613 if Val
= Uint_0
then
14615 Maybe_Superflat
:= False;
14619 elsif Val
= Uint_1
then
14621 Maybe_Superflat
:= False;
14627 -- Here we have to make sure of being within a 32-bit range (take the
14628 -- full unsigned range so the length of 32-bit arrays is accepted).
14630 Determine_Range
(N
, OK
, Lo
, Hi
, Assume_Valid
=> True);
14633 or else Lo
< Uint_0
14634 or else Hi
> Uint_2
** 32
14639 Maybe_Superflat
:= (Lo
= Uint_0
);
14641 -- Tests if N is also a length attribute applied to a simple entity
14643 Dbl
:= Is_Entity_Length
(N
, 2);
14645 -- We can deal with the superflat case only if N is also a length
14647 if Maybe_Superflat
and then not Dbl
then
14651 -- Comparison value was within range, so now we must check the index
14652 -- value to make sure it is also within 32 bits.
14654 for K
in Pos
range 1 .. 2 loop
14655 Indx
:= First_Index
(Etype
(Ent
(K
)));
14657 if Present
(Index
(K
)) then
14658 for J
in 2 .. UI_To_Int
(Intval
(Index
(K
))) loop
14663 Ityp
:= Etype
(Indx
);
14665 if Esize
(Ityp
) > 32 then
14675 end Is_Optimizable
;
14677 -------------------------------
14678 -- Rewrite_For_Equal_Lengths --
14679 -------------------------------
14681 procedure Rewrite_For_Equal_Lengths
is
14690 New_Occurrence_Of
(Standard_True
, Sloc
(N
))));
14698 New_Occurrence_Of
(Standard_False
, Sloc
(N
))));
14701 raise Program_Error
;
14704 Analyze_And_Resolve
(N
, Typ
);
14705 end Rewrite_For_Equal_Lengths
;
14707 -- Start of processing for Optimize_Length_Comparison
14710 -- Nothing to do if not a comparison
14712 if Op
not in N_Op_Compare
then
14716 -- Nothing to do if special -gnatd.P debug flag set.
14718 if Debug_Flag_Dot_PP
then
14722 -- Ent'Length op 0/1
14724 if Is_Entity_Length
(Left_Opnd
(N
), 1)
14725 and then Is_Optimizable
(Right_Opnd
(N
))
14729 -- 0/1 op Ent'Length
14731 elsif Is_Entity_Length
(Right_Opnd
(N
), 1)
14732 and then Is_Optimizable
(Left_Opnd
(N
))
14734 -- Flip comparison to opposite sense
14737 when N_Op_Lt
=> Op
:= N_Op_Gt
;
14738 when N_Op_Le
=> Op
:= N_Op_Ge
;
14739 when N_Op_Gt
=> Op
:= N_Op_Lt
;
14740 when N_Op_Ge
=> Op
:= N_Op_Le
;
14741 when others => null;
14744 -- Else optimization not possible
14750 -- Fall through if we will do the optimization
14752 -- Cases to handle:
14754 -- X'Length = 0 => X'First > X'Last
14755 -- X'Length = 1 => X'First = X'Last
14756 -- X'Length = n => X'First + (n - 1) = X'Last
14758 -- X'Length /= 0 => X'First <= X'Last
14759 -- X'Length /= 1 => X'First /= X'Last
14760 -- X'Length /= n => X'First + (n - 1) /= X'Last
14762 -- X'Length >= 0 => always true, warn
14763 -- X'Length >= 1 => X'First <= X'Last
14764 -- X'Length >= n => X'First + (n - 1) <= X'Last
14766 -- X'Length > 0 => X'First <= X'Last
14767 -- X'Length > 1 => X'First < X'Last
14768 -- X'Length > n => X'First + (n - 1) < X'Last
14770 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
14771 -- X'Length <= 1 => X'First >= X'Last
14772 -- X'Length <= n => X'First + (n - 1) >= X'Last
14774 -- X'Length < 0 => always false (warn)
14775 -- X'Length < 1 => X'First > X'Last
14776 -- X'Length < n => X'First + (n - 1) > X'Last
14778 -- Note: for the cases of n (not constant 0,1), we require that the
14779 -- corresponding index type be integer or shorter (i.e. not 64-bit),
14780 -- and the same for the comparison value. Then we do the comparison
14781 -- using 64-bit arithmetic (actually long long integer), so that we
14782 -- cannot have overflow intefering with the result.
14784 -- First deal with warning cases
14793 Convert_To
(Typ
, New_Occurrence_Of
(Standard_True
, Loc
)));
14794 Analyze_And_Resolve
(N
, Typ
);
14795 Warn_On_Known_Condition
(N
);
14802 Convert_To
(Typ
, New_Occurrence_Of
(Standard_False
, Loc
)));
14803 Analyze_And_Resolve
(N
, Typ
);
14804 Warn_On_Known_Condition
(N
);
14808 if Constant_Condition_Warnings
14809 and then Comes_From_Source
(Original_Node
(N
))
14811 Error_Msg_N
("could replace by ""'=""?c?", N
);
14821 -- Build the First reference we will use
14824 Make_Attribute_Reference
(Loc
,
14825 Prefix
=> New_Occurrence_Of
(Ent
(1), Loc
),
14826 Attribute_Name
=> Name_First
);
14828 if Present
(Index
(1)) then
14829 Set_Expressions
(Left
, New_List
(New_Copy
(Index
(1))));
14832 -- Build the Last reference we will use
14835 Make_Attribute_Reference
(Loc
,
14836 Prefix
=> New_Occurrence_Of
(Ent
(1), Loc
),
14837 Attribute_Name
=> Name_Last
);
14839 if Present
(Index
(1)) then
14840 Set_Expressions
(Right
, New_List
(New_Copy
(Index
(1))));
14843 -- If general value case, then do the addition of (n - 1), and
14844 -- also add the needed conversions to type Long_Long_Integer.
14846 -- If n = Y'Length, we rewrite X'First + (n - 1) op X'Last into:
14848 -- Y'Last + (X'First - Y'First) op X'Last
14850 -- in the hope that X'First - Y'First can be computed statically.
14852 if Present
(Comp
) then
14853 if Present
(Ent
(2)) then
14855 Y_First
: constant Node_Id
:=
14856 Make_Attribute_Reference
(Loc
,
14857 Prefix
=> New_Occurrence_Of
(Ent
(2), Loc
),
14858 Attribute_Name
=> Name_First
);
14859 Y_Last
: constant Node_Id
:=
14860 Make_Attribute_Reference
(Loc
,
14861 Prefix
=> New_Occurrence_Of
(Ent
(2), Loc
),
14862 Attribute_Name
=> Name_Last
);
14863 R
: Compare_Result
;
14866 if Present
(Index
(2)) then
14867 Set_Expressions
(Y_First
, New_List
(New_Copy
(Index
(2))));
14868 Set_Expressions
(Y_Last
, New_List
(New_Copy
(Index
(2))));
14874 -- If X'First = Y'First, simplify the above formula into a
14875 -- direct comparison of Y'Last and X'Last.
14877 R
:= Compile_Time_Compare
(Left
, Y_First
, Assume_Valid
=> True);
14883 R
:= Compile_Time_Compare
14884 (Right
, Y_Last
, Assume_Valid
=> True);
14886 -- If the pairs of attributes are equal, we are done
14889 Rewrite_For_Equal_Lengths
;
14893 -- If the base types are different, convert both operands to
14894 -- Long_Long_Integer, else compare them directly.
14896 if Base_Type
(Etype
(Right
)) /= Base_Type
(Etype
(Y_Last
))
14898 Left
:= Convert_To_Long_Long_Integer
(Y_Last
);
14904 -- Otherwise, use the above formula as-is
14910 Convert_To_Long_Long_Integer
(Y_Last
),
14912 Make_Op_Subtract
(Loc
,
14914 Convert_To_Long_Long_Integer
(Left
),
14916 Convert_To_Long_Long_Integer
(Y_First
)));
14920 -- General value case
14925 Left_Opnd
=> Convert_To_Long_Long_Integer
(Left
),
14927 Make_Op_Subtract
(Loc
,
14928 Left_Opnd
=> Convert_To_Long_Long_Integer
(Comp
),
14929 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)));
14933 -- We cannot do anything in the superflat case past this point
14935 if Maybe_Superflat
then
14939 -- If general operand, convert Last reference to Long_Long_Integer
14941 if Present
(Comp
) then
14942 Right
:= Convert_To_Long_Long_Integer
(Right
);
14945 -- Check for cases to optimize
14947 -- X'Length = 0 => X'First > X'Last
14948 -- X'Length < 1 => X'First > X'Last
14949 -- X'Length < n => X'First + (n - 1) > X'Last
14951 if (Is_Zero
and then Op
= N_Op_Eq
)
14952 or else (not Is_Zero
and then Op
= N_Op_Lt
)
14957 Right_Opnd
=> Right
);
14959 -- X'Length = 1 => X'First = X'Last
14960 -- X'Length = n => X'First + (n - 1) = X'Last
14962 elsif not Is_Zero
and then Op
= N_Op_Eq
then
14966 Right_Opnd
=> Right
);
14968 -- X'Length /= 0 => X'First <= X'Last
14969 -- X'Length > 0 => X'First <= X'Last
14971 elsif Is_Zero
and (Op
= N_Op_Ne
or else Op
= N_Op_Gt
) then
14975 Right_Opnd
=> Right
);
14977 -- X'Length /= 1 => X'First /= X'Last
14978 -- X'Length /= n => X'First + (n - 1) /= X'Last
14980 elsif not Is_Zero
and then Op
= N_Op_Ne
then
14984 Right_Opnd
=> Right
);
14986 -- X'Length >= 1 => X'First <= X'Last
14987 -- X'Length >= n => X'First + (n - 1) <= X'Last
14989 elsif not Is_Zero
and then Op
= N_Op_Ge
then
14993 Right_Opnd
=> Right
);
14995 -- X'Length > 1 => X'First < X'Last
14996 -- X'Length > n => X'First + (n = 1) < X'Last
14998 elsif not Is_Zero
and then Op
= N_Op_Gt
then
15002 Right_Opnd
=> Right
);
15004 -- X'Length <= 1 => X'First >= X'Last
15005 -- X'Length <= n => X'First + (n - 1) >= X'Last
15007 elsif not Is_Zero
and then Op
= N_Op_Le
then
15011 Right_Opnd
=> Right
);
15013 -- Should not happen at this stage
15016 raise Program_Error
;
15019 -- Rewrite and finish up (we can suppress overflow checks, see above)
15021 Rewrite
(N
, Result
);
15022 Analyze_And_Resolve
(N
, Typ
, Suppress
=> Overflow_Check
);
15023 end Optimize_Length_Comparison
;
15025 --------------------------------
15026 -- Process_If_Case_Statements --
15027 --------------------------------
15029 procedure Process_If_Case_Statements
(N
: Node_Id
; Stmts
: List_Id
) is
15033 Decl
:= First
(Stmts
);
15034 while Present
(Decl
) loop
15035 if Nkind
(Decl
) = N_Object_Declaration
15036 and then Is_Finalizable_Transient
(Decl
, N
)
15038 Process_Transient_In_Expression
(Decl
, N
, Stmts
);
15043 end Process_If_Case_Statements
;
15045 -------------------------------------
15046 -- Process_Transient_In_Expression --
15047 -------------------------------------
15049 procedure Process_Transient_In_Expression
15050 (Obj_Decl
: Node_Id
;
15054 Loc
: constant Source_Ptr
:= Sloc
(Obj_Decl
);
15055 Obj_Id
: constant Entity_Id
:= Defining_Identifier
(Obj_Decl
);
15057 Hook_Context
: constant Node_Id
:= Find_Hook_Context
(Expr
);
15058 -- The node on which to insert the hook as an action. This is usually
15059 -- the innermost enclosing non-transient construct.
15061 Fin_Call
: Node_Id
;
15062 Hook_Assign
: Node_Id
;
15063 Hook_Clear
: Node_Id
;
15064 Hook_Decl
: Node_Id
;
15065 Hook_Insert
: Node_Id
;
15066 Ptr_Decl
: Node_Id
;
15068 Fin_Context
: Node_Id
;
15069 -- The node after which to insert the finalization actions of the
15070 -- transient object.
15073 pragma Assert
(Nkind
(Expr
) in N_Case_Expression
15074 | N_Expression_With_Actions
15075 | N_If_Expression
);
15077 -- When the context is a Boolean evaluation, all three nodes capture the
15078 -- result of their computation in a local temporary:
15081 -- Trans_Id : Ctrl_Typ := ...;
15082 -- Result : constant Boolean := ... Trans_Id ...;
15083 -- <finalize Trans_Id>
15086 -- As a result, the finalization of any transient objects can safely
15087 -- take place after the result capture.
15089 -- ??? could this be extended to elementary types?
15091 if Is_Boolean_Type
(Etype
(Expr
)) then
15092 Fin_Context
:= Last
(Stmts
);
15094 -- Otherwise the immediate context may not be safe enough to carry
15095 -- out transient object finalization due to aliasing and nesting of
15096 -- constructs. Insert calls to [Deep_]Finalize after the innermost
15097 -- enclosing non-transient construct.
15100 Fin_Context
:= Hook_Context
;
15103 -- Mark the transient object as successfully processed to avoid double
15106 Set_Is_Finalized_Transient
(Obj_Id
);
15108 -- Construct all the pieces necessary to hook and finalize a transient
15111 Build_Transient_Object_Statements
15112 (Obj_Decl
=> Obj_Decl
,
15113 Fin_Call
=> Fin_Call
,
15114 Hook_Assign
=> Hook_Assign
,
15115 Hook_Clear
=> Hook_Clear
,
15116 Hook_Decl
=> Hook_Decl
,
15117 Ptr_Decl
=> Ptr_Decl
,
15118 Finalize_Obj
=> False);
15120 -- Add the access type which provides a reference to the transient
15121 -- object. Generate:
15123 -- type Ptr_Typ is access all Desig_Typ;
15125 Insert_Action
(Hook_Context
, Ptr_Decl
);
15127 -- Add the temporary which acts as a hook to the transient object.
15130 -- Hook : Ptr_Id := null;
15132 Insert_Action
(Hook_Context
, Hook_Decl
);
15134 -- When the transient object is initialized by an aggregate, the hook
15135 -- must capture the object after the last aggregate assignment takes
15136 -- place. Only then is the object considered initialized. Generate:
15138 -- Hook := Ptr_Typ (Obj_Id);
15140 -- Hook := Obj_Id'Unrestricted_Access;
15142 if Ekind
(Obj_Id
) in E_Constant | E_Variable
15143 and then Present
(Last_Aggregate_Assignment
(Obj_Id
))
15145 Hook_Insert
:= Last_Aggregate_Assignment
(Obj_Id
);
15147 -- Otherwise the hook seizes the related object immediately
15150 Hook_Insert
:= Obj_Decl
;
15153 Insert_After_And_Analyze
(Hook_Insert
, Hook_Assign
);
15155 -- When the node is part of a return statement, there is no need to
15156 -- insert a finalization call, as the general finalization mechanism
15157 -- (see Build_Finalizer) would take care of the transient object on
15158 -- subprogram exit. Note that it would also be impossible to insert the
15159 -- finalization code after the return statement as this will render it
15162 if Nkind
(Fin_Context
) = N_Simple_Return_Statement
then
15165 -- Finalize the hook after the context has been evaluated. Generate:
15167 -- if Hook /= null then
15168 -- [Deep_]Finalize (Hook.all);
15172 -- Note that the value returned by Find_Hook_Context may be an operator
15173 -- node, which is not a list member. We must locate the proper node in
15174 -- in the tree after which to insert the finalization code.
15177 while not Is_List_Member
(Fin_Context
) loop
15178 Fin_Context
:= Parent
(Fin_Context
);
15181 pragma Assert
(Present
(Fin_Context
));
15183 Insert_Action_After
(Fin_Context
,
15184 Make_Implicit_If_Statement
(Obj_Decl
,
15188 New_Occurrence_Of
(Defining_Entity
(Hook_Decl
), Loc
),
15189 Right_Opnd
=> Make_Null
(Loc
)),
15191 Then_Statements
=> New_List
(
15195 end Process_Transient_In_Expression
;
15197 ------------------------
15198 -- Rewrite_Comparison --
15199 ------------------------
15201 procedure Rewrite_Comparison
(N
: Node_Id
) is
15202 Typ
: constant Entity_Id
:= Etype
(N
);
15204 False_Result
: Boolean;
15205 True_Result
: Boolean;
15208 if Nkind
(N
) = N_Type_Conversion
then
15209 Rewrite_Comparison
(Expression
(N
));
15212 elsif Nkind
(N
) not in N_Op_Compare
then
15216 -- If both operands are static, then the comparison has been already
15217 -- folded in evaluation.
15220 (not Is_Static_Expression
(Left_Opnd
(N
))
15222 not Is_Static_Expression
(Right_Opnd
(N
)));
15224 -- Determine the potential outcome of the comparison assuming that the
15225 -- operands are valid and emit a warning when the comparison evaluates
15226 -- to True or False only in the presence of invalid values.
15228 Warn_On_Constant_Valid_Condition
(N
);
15230 -- Determine the potential outcome of the comparison assuming that the
15231 -- operands are not valid.
15235 Assume_Valid
=> False,
15236 True_Result
=> True_Result
,
15237 False_Result
=> False_Result
);
15239 -- The outcome is a decisive False or True, rewrite the operator into a
15240 -- non-static literal.
15242 if False_Result
or True_Result
then
15245 New_Occurrence_Of
(Boolean_Literals
(True_Result
), Sloc
(N
))));
15247 Analyze_And_Resolve
(N
, Typ
);
15248 Set_Is_Static_Expression
(N
, False);
15249 Warn_On_Known_Condition
(N
);
15251 end Rewrite_Comparison
;
15253 ----------------------------
15254 -- Safe_In_Place_Array_Op --
15255 ----------------------------
15257 function Safe_In_Place_Array_Op
15260 Op2
: Node_Id
) return Boolean
15262 Target
: Entity_Id
;
15264 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean;
15265 -- Operand is safe if it cannot overlap part of the target of the
15266 -- operation. If the operand and the target are identical, the operand
15267 -- is safe. The operand can be empty in the case of negation.
15269 function Is_Unaliased
(N
: Node_Id
) return Boolean;
15270 -- Check that N is a stand-alone entity
15276 function Is_Unaliased
(N
: Node_Id
) return Boolean is
15280 and then No
(Address_Clause
(Entity
(N
)))
15281 and then No
(Renamed_Object
(Entity
(N
)));
15284 ---------------------
15285 -- Is_Safe_Operand --
15286 ---------------------
15288 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean is
15293 elsif Is_Entity_Name
(Op
) then
15294 return Is_Unaliased
(Op
);
15296 elsif Nkind
(Op
) in N_Indexed_Component | N_Selected_Component
then
15297 return Is_Unaliased
(Prefix
(Op
));
15299 elsif Nkind
(Op
) = N_Slice
then
15301 Is_Unaliased
(Prefix
(Op
))
15302 and then Entity
(Prefix
(Op
)) /= Target
;
15304 elsif Nkind
(Op
) = N_Op_Not
then
15305 return Is_Safe_Operand
(Right_Opnd
(Op
));
15310 end Is_Safe_Operand
;
15312 -- Start of processing for Safe_In_Place_Array_Op
15315 -- Skip this processing if the component size is different from system
15316 -- storage unit (since at least for NOT this would cause problems).
15318 if Component_Size
(Etype
(Lhs
)) /= System_Storage_Unit
then
15321 -- Cannot do in place stuff if non-standard Boolean representation
15323 elsif Has_Non_Standard_Rep
(Component_Type
(Etype
(Lhs
))) then
15326 elsif not Is_Unaliased
(Lhs
) then
15330 Target
:= Entity
(Lhs
);
15331 return Is_Safe_Operand
(Op1
) and then Is_Safe_Operand
(Op2
);
15333 end Safe_In_Place_Array_Op
;
15335 -----------------------
15336 -- Tagged_Membership --
15337 -----------------------
15339 -- There are two different cases to consider depending on whether the right
15340 -- operand is a class-wide type or not. If not we just compare the actual
15341 -- tag of the left expr to the target type tag:
15343 -- Left_Expr.Tag = Right_Type'Tag;
15345 -- If it is a class-wide type we use the RT function CW_Membership which is
15346 -- usually implemented by looking in the ancestor tables contained in the
15347 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
15349 -- In both cases if Left_Expr is an access type, we first check whether it
15352 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
15353 -- function IW_Membership which is usually implemented by looking in the
15354 -- table of abstract interface types plus the ancestor table contained in
15355 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
15357 procedure Tagged_Membership
15359 SCIL_Node
: out Node_Id
;
15360 Result
: out Node_Id
)
15362 Left
: constant Node_Id
:= Left_Opnd
(N
);
15363 Right
: constant Node_Id
:= Right_Opnd
(N
);
15364 Loc
: constant Source_Ptr
:= Sloc
(N
);
15366 -- Handle entities from the limited view
15368 Orig_Right_Type
: constant Entity_Id
:= Available_View
(Etype
(Right
));
15370 Full_R_Typ
: Entity_Id
;
15371 Left_Type
: Entity_Id
:= Available_View
(Etype
(Left
));
15372 Right_Type
: Entity_Id
:= Orig_Right_Type
;
15376 SCIL_Node
:= Empty
;
15378 -- We have to examine the corresponding record type when dealing with
15379 -- protected types instead of the original, unexpanded, type.
15381 if Ekind
(Right_Type
) = E_Protected_Type
then
15382 Right_Type
:= Corresponding_Record_Type
(Right_Type
);
15385 if Ekind
(Left_Type
) = E_Protected_Type
then
15386 Left_Type
:= Corresponding_Record_Type
(Left_Type
);
15389 -- In the case where the type is an access type, the test is applied
15390 -- using the designated types (needed in Ada 2012 for implicit anonymous
15391 -- access conversions, for AI05-0149).
15393 if Is_Access_Type
(Right_Type
) then
15394 Left_Type
:= Designated_Type
(Left_Type
);
15395 Right_Type
:= Designated_Type
(Right_Type
);
15398 if Is_Class_Wide_Type
(Left_Type
) then
15399 Left_Type
:= Root_Type
(Left_Type
);
15402 if Is_Class_Wide_Type
(Right_Type
) then
15403 Full_R_Typ
:= Underlying_Type
(Root_Type
(Right_Type
));
15405 Full_R_Typ
:= Underlying_Type
(Right_Type
);
15409 Make_Selected_Component
(Loc
,
15410 Prefix
=> Relocate_Node
(Left
),
15412 New_Occurrence_Of
(First_Tag_Component
(Left_Type
), Loc
));
15414 if Is_Class_Wide_Type
(Right_Type
) then
15416 -- No need to issue a run-time check if we statically know that the
15417 -- result of this membership test is always true. For example,
15418 -- considering the following declarations:
15420 -- type Iface is interface;
15421 -- type T is tagged null record;
15422 -- type DT is new T and Iface with null record;
15427 -- These membership tests are always true:
15430 -- Obj2 in T'Class;
15431 -- Obj2 in Iface'Class;
15433 -- We do not need to handle cases where the membership is illegal.
15436 -- Obj1 in DT'Class; -- Compile time error
15437 -- Obj1 in Iface'Class; -- Compile time error
15439 if not Is_Interface
(Left_Type
)
15440 and then not Is_Class_Wide_Type
(Left_Type
)
15441 and then (Is_Ancestor
(Etype
(Right_Type
), Left_Type
,
15442 Use_Full_View
=> True)
15443 or else (Is_Interface
(Etype
(Right_Type
))
15444 and then Interface_Present_In_Ancestor
15446 Iface
=> Etype
(Right_Type
))))
15448 Result
:= New_Occurrence_Of
(Standard_True
, Loc
);
15452 -- Ada 2005 (AI-251): Class-wide applied to interfaces
15454 if Is_Interface
(Etype
(Class_Wide_Type
(Right_Type
)))
15456 -- Support to: "Iface_CW_Typ in Typ'Class"
15458 or else Is_Interface
(Left_Type
)
15460 -- Issue error if IW_Membership operation not available in a
15461 -- configurable run-time setting.
15463 if not RTE_Available
(RE_IW_Membership
) then
15465 ("dynamic membership test on interface types", N
);
15471 Make_Function_Call
(Loc
,
15472 Name
=> New_Occurrence_Of
(RTE
(RE_IW_Membership
), Loc
),
15473 Parameter_Associations
=> New_List
(
15474 Make_Attribute_Reference
(Loc
,
15476 Attribute_Name
=> Name_Address
),
15477 New_Occurrence_Of
(
15478 Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))),
15481 -- Ada 95: Normal case
15484 -- Issue error if CW_Membership operation not available in a
15485 -- configurable run-time setting.
15487 if not RTE_Available
(RE_CW_Membership
) then
15489 ("dynamic membership test on tagged types", N
);
15495 Make_Function_Call
(Loc
,
15496 Name
=> New_Occurrence_Of
(RTE
(RE_CW_Membership
), Loc
),
15497 Parameter_Associations
=> New_List
(
15499 New_Occurrence_Of
(
15500 Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))),
15503 -- Generate the SCIL node for this class-wide membership test.
15505 if Generate_SCIL
then
15506 SCIL_Node
:= Make_SCIL_Membership_Test
(Sloc
(N
));
15507 Set_SCIL_Entity
(SCIL_Node
, Etype
(Right_Type
));
15508 Set_SCIL_Tag_Value
(SCIL_Node
, Obj_Tag
);
15512 -- Right_Type is not a class-wide type
15515 -- No need to check the tag of the object if Right_Typ is abstract
15517 if Is_Abstract_Type
(Right_Type
) then
15518 Result
:= New_Occurrence_Of
(Standard_False
, Loc
);
15523 Left_Opnd
=> Obj_Tag
,
15526 (Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))), Loc
));
15530 -- if Left is an access object then generate test of the form:
15531 -- * if Right_Type excludes null: Left /= null and then ...
15532 -- * if Right_Type includes null: Left = null or else ...
15534 if Is_Access_Type
(Orig_Right_Type
) then
15535 if Can_Never_Be_Null
(Orig_Right_Type
) then
15536 Result
:= Make_And_Then
(Loc
,
15540 Right_Opnd
=> Make_Null
(Loc
)),
15541 Right_Opnd
=> Result
);
15544 Result
:= Make_Or_Else
(Loc
,
15548 Right_Opnd
=> Make_Null
(Loc
)),
15549 Right_Opnd
=> Result
);
15552 end Tagged_Membership
;
15554 ------------------------------
15555 -- Unary_Op_Validity_Checks --
15556 ------------------------------
15558 procedure Unary_Op_Validity_Checks
(N
: Node_Id
) is
15560 if Validity_Checks_On
and Validity_Check_Operands
then
15561 Ensure_Valid
(Right_Opnd
(N
));
15563 end Unary_Op_Validity_Checks
;