1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Debug
; use Debug
;
29 with Einfo
; use Einfo
;
30 with Elists
; use Elists
;
31 with Errout
; use Errout
;
32 with Exp_Aggr
; use Exp_Aggr
;
33 with Exp_Atag
; use Exp_Atag
;
34 with Exp_Ch2
; use Exp_Ch2
;
35 with Exp_Ch3
; use Exp_Ch3
;
36 with Exp_Ch6
; use Exp_Ch6
;
37 with Exp_Ch7
; use Exp_Ch7
;
38 with Exp_Ch9
; use Exp_Ch9
;
39 with Exp_Disp
; use Exp_Disp
;
40 with Exp_Fixd
; use Exp_Fixd
;
41 with Exp_Intr
; use Exp_Intr
;
42 with Exp_Pakd
; use Exp_Pakd
;
43 with Exp_Tss
; use Exp_Tss
;
44 with Exp_Util
; use Exp_Util
;
45 with Freeze
; use Freeze
;
46 with Inline
; use Inline
;
47 with Namet
; use Namet
;
48 with Nlists
; use Nlists
;
49 with Nmake
; use Nmake
;
51 with Par_SCO
; use Par_SCO
;
52 with Restrict
; use Restrict
;
53 with Rident
; use Rident
;
54 with Rtsfind
; use Rtsfind
;
56 with Sem_Aux
; use Sem_Aux
;
57 with Sem_Cat
; use Sem_Cat
;
58 with Sem_Ch3
; use Sem_Ch3
;
59 with Sem_Ch13
; use Sem_Ch13
;
60 with Sem_Eval
; use Sem_Eval
;
61 with Sem_Res
; use Sem_Res
;
62 with Sem_Type
; use Sem_Type
;
63 with Sem_Util
; use Sem_Util
;
64 with Sem_Warn
; use Sem_Warn
;
65 with Sinfo
; use Sinfo
;
66 with Snames
; use Snames
;
67 with Stand
; use Stand
;
68 with SCIL_LL
; use SCIL_LL
;
69 with Targparm
; use Targparm
;
70 with Tbuild
; use Tbuild
;
71 with Ttypes
; use Ttypes
;
72 with Uintp
; use Uintp
;
73 with Urealp
; use Urealp
;
74 with Validsw
; use Validsw
;
76 package body Exp_Ch4
is
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Binary_Op_Validity_Checks
(N
: Node_Id
);
83 pragma Inline
(Binary_Op_Validity_Checks
);
84 -- Performs validity checks for a binary operator
86 procedure Build_Boolean_Array_Proc_Call
90 -- If a boolean array assignment can be done in place, build call to
91 -- corresponding library procedure.
93 procedure Displace_Allocator_Pointer
(N
: Node_Id
);
94 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
95 -- Expand_Allocator_Expression. Allocating class-wide interface objects
96 -- this routine displaces the pointer to the allocated object to reference
97 -- the component referencing the corresponding secondary dispatch table.
99 procedure Expand_Allocator_Expression
(N
: Node_Id
);
100 -- Subsidiary to Expand_N_Allocator, for the case when the expression
101 -- is a qualified expression or an aggregate.
103 procedure Expand_Array_Comparison
(N
: Node_Id
);
104 -- This routine handles expansion of the comparison operators (N_Op_Lt,
105 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
106 -- code for these operators is similar, differing only in the details of
107 -- the actual comparison call that is made. Special processing (call a
110 function Expand_Array_Equality
115 Typ
: Entity_Id
) return Node_Id
;
116 -- Expand an array equality into a call to a function implementing this
117 -- equality, and a call to it. Loc is the location for the generated nodes.
118 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
119 -- on which to attach bodies of local functions that are created in the
120 -- process. It is the responsibility of the caller to insert those bodies
121 -- at the right place. Nod provides the Sloc value for the generated code.
122 -- Normally the types used for the generated equality routine are taken
123 -- from Lhs and Rhs. However, in some situations of generated code, the
124 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
125 -- the type to be used for the formal parameters.
127 procedure Expand_Boolean_Operator
(N
: Node_Id
);
128 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
129 -- case of array type arguments.
131 procedure Expand_Nonbinary_Modular_Op
(N
: Node_Id
);
132 -- When generating C code, convert nonbinary modular arithmetic operations
133 -- into code that relies on the front-end expansion of operator Mod. No
134 -- expansion is performed if N is not a nonbinary modular operand.
136 procedure Expand_Short_Circuit_Operator
(N
: Node_Id
);
137 -- Common expansion processing for short-circuit boolean operators
139 procedure Expand_Compare_Minimize_Eliminate_Overflow
(N
: Node_Id
);
140 -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is
141 -- where we allow comparison of "out of range" values.
143 function Expand_Composite_Equality
148 Bodies
: List_Id
) return Node_Id
;
149 -- Local recursive function used to expand equality for nested composite
150 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
151 -- to attach bodies of local functions that are created in the process. It
152 -- is the responsibility of the caller to insert those bodies at the right
153 -- place. Nod provides the Sloc value for generated code. Lhs and Rhs are
154 -- the left and right sides for the comparison, and Typ is the type of the
155 -- objects to compare.
157 procedure Expand_Concatenate
(Cnode
: Node_Id
; Opnds
: List_Id
);
158 -- Routine to expand concatenation of a sequence of two or more operands
159 -- (in the list Operands) and replace node Cnode with the result of the
160 -- concatenation. The operands can be of any appropriate type, and can
161 -- include both arrays and singleton elements.
163 procedure Expand_Membership_Minimize_Eliminate_Overflow
(N
: Node_Id
);
164 -- N is an N_In membership test mode, with the overflow check mode set to
165 -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed
166 -- integer type. This is a case where top level processing is required to
167 -- handle overflow checks in subtrees.
169 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
);
170 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
171 -- fixed. We do not have such a type at runtime, so the purpose of this
172 -- routine is to find the real type by looking up the tree. We also
173 -- determine if the operation must be rounded.
175 function Has_Inferable_Discriminants
(N
: Node_Id
) return Boolean;
176 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
177 -- discriminants if it has a constrained nominal type, unless the object
178 -- is a component of an enclosing Unchecked_Union object that is subject
179 -- to a per-object constraint and the enclosing object lacks inferable
182 -- An expression of an Unchecked_Union type has inferable discriminants
183 -- if it is either a name of an object with inferable discriminants or a
184 -- qualified expression whose subtype mark denotes a constrained subtype.
186 procedure Insert_Dereference_Action
(N
: Node_Id
);
187 -- N is an expression whose type is an access. When the type of the
188 -- associated storage pool is derived from Checked_Pool, generate a
189 -- call to the 'Dereference' primitive operation.
191 function Make_Array_Comparison_Op
193 Nod
: Node_Id
) return Node_Id
;
194 -- Comparisons between arrays are expanded in line. This function produces
195 -- the body of the implementation of (a > b), where a and b are one-
196 -- dimensional arrays of some discrete type. The original node is then
197 -- expanded into the appropriate call to this function. Nod provides the
198 -- Sloc value for the generated code.
200 function Make_Boolean_Array_Op
202 N
: Node_Id
) return Node_Id
;
203 -- Boolean operations on boolean arrays are expanded in line. This function
204 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
205 -- b). It is used only the normal case and not the packed case. The type
206 -- involved, Typ, is the Boolean array type, and the logical operations in
207 -- the body are simple boolean operations. Note that Typ is always a
208 -- constrained type (the caller has ensured this by using
209 -- Convert_To_Actual_Subtype if necessary).
211 function Minimized_Eliminated_Overflow_Check
(N
: Node_Id
) return Boolean;
212 -- For signed arithmetic operations when the current overflow mode is
213 -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks
214 -- as the first thing we do. We then return. We count on the recursive
215 -- apparatus for overflow checks to call us back with an equivalent
216 -- operation that is in CHECKED mode, avoiding a recursive entry into this
217 -- routine, and that is when we will proceed with the expansion of the
218 -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do
219 -- these optimizations without first making this check, since there may be
220 -- operands further down the tree that are relying on the recursive calls
221 -- triggered by the top level nodes to properly process overflow checking
222 -- and remaining expansion on these nodes. Note that this call back may be
223 -- skipped if the operation is done in Bignum mode but that's fine, since
224 -- the Bignum call takes care of everything.
226 procedure Optimize_Length_Comparison
(N
: Node_Id
);
227 -- Given an expression, if it is of the form X'Length op N (or the other
228 -- way round), where N is known at compile time to be 0 or 1, and X is a
229 -- simple entity, and op is a comparison operator, optimizes it into a
230 -- comparison of First and Last.
232 procedure Process_If_Case_Statements
(N
: Node_Id
; Stmts
: List_Id
);
233 -- Inspect and process statement list Stmt of if or case expression N for
234 -- transient objects. If such objects are found, the routine generates code
235 -- to clean them up when the context of the expression is evaluated.
237 procedure Process_Transient_In_Expression
241 -- Subsidiary routine to the expansion of expression_with_actions, if and
242 -- case expressions. Generate all necessary code to finalize a transient
243 -- object when the enclosing context is elaborated or evaluated. Obj_Decl
244 -- denotes the declaration of the transient object, which is usually the
245 -- result of a controlled function call. Expr denotes the expression with
246 -- actions, if expression, or case expression node. Stmts denotes the
247 -- statement list which contains Decl, either at the top level or within a
250 procedure Rewrite_Comparison
(N
: Node_Id
);
251 -- If N is the node for a comparison whose outcome can be determined at
252 -- compile time, then the node N can be rewritten with True or False. If
253 -- the outcome cannot be determined at compile time, the call has no
254 -- effect. If N is a type conversion, then this processing is applied to
255 -- its expression. If N is neither comparison nor a type conversion, the
256 -- call has no effect.
258 procedure Tagged_Membership
260 SCIL_Node
: out Node_Id
;
261 Result
: out Node_Id
);
262 -- Construct the expression corresponding to the tagged membership test.
263 -- Deals with a second operand being (or not) a class-wide type.
265 function Safe_In_Place_Array_Op
268 Op2
: Node_Id
) return Boolean;
269 -- In the context of an assignment, where the right-hand side is a boolean
270 -- operation on arrays, check whether operation can be performed in place.
272 procedure Unary_Op_Validity_Checks
(N
: Node_Id
);
273 pragma Inline
(Unary_Op_Validity_Checks
);
274 -- Performs validity checks for a unary operator
276 -------------------------------
277 -- Binary_Op_Validity_Checks --
278 -------------------------------
280 procedure Binary_Op_Validity_Checks
(N
: Node_Id
) is
282 if Validity_Checks_On
and Validity_Check_Operands
then
283 Ensure_Valid
(Left_Opnd
(N
));
284 Ensure_Valid
(Right_Opnd
(N
));
286 end Binary_Op_Validity_Checks
;
288 ------------------------------------
289 -- Build_Boolean_Array_Proc_Call --
290 ------------------------------------
292 procedure Build_Boolean_Array_Proc_Call
297 Loc
: constant Source_Ptr
:= Sloc
(N
);
298 Kind
: constant Node_Kind
:= Nkind
(Expression
(N
));
299 Target
: constant Node_Id
:=
300 Make_Attribute_Reference
(Loc
,
302 Attribute_Name
=> Name_Address
);
304 Arg1
: Node_Id
:= Op1
;
305 Arg2
: Node_Id
:= Op2
;
307 Proc_Name
: Entity_Id
;
310 if Kind
= N_Op_Not
then
311 if Nkind
(Op1
) in N_Binary_Op
then
313 -- Use negated version of the binary operators
315 if Nkind
(Op1
) = N_Op_And
then
316 Proc_Name
:= RTE
(RE_Vector_Nand
);
318 elsif Nkind
(Op1
) = N_Op_Or
then
319 Proc_Name
:= RTE
(RE_Vector_Nor
);
321 else pragma Assert
(Nkind
(Op1
) = N_Op_Xor
);
322 Proc_Name
:= RTE
(RE_Vector_Xor
);
326 Make_Procedure_Call_Statement
(Loc
,
327 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
329 Parameter_Associations
=> New_List
(
331 Make_Attribute_Reference
(Loc
,
332 Prefix
=> Left_Opnd
(Op1
),
333 Attribute_Name
=> Name_Address
),
335 Make_Attribute_Reference
(Loc
,
336 Prefix
=> Right_Opnd
(Op1
),
337 Attribute_Name
=> Name_Address
),
339 Make_Attribute_Reference
(Loc
,
340 Prefix
=> Left_Opnd
(Op1
),
341 Attribute_Name
=> Name_Length
)));
344 Proc_Name
:= RTE
(RE_Vector_Not
);
347 Make_Procedure_Call_Statement
(Loc
,
348 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
349 Parameter_Associations
=> New_List
(
352 Make_Attribute_Reference
(Loc
,
354 Attribute_Name
=> Name_Address
),
356 Make_Attribute_Reference
(Loc
,
358 Attribute_Name
=> Name_Length
)));
362 -- We use the following equivalences:
364 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
365 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
366 -- (not X) xor (not Y) = X xor Y
367 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
369 if Nkind
(Op1
) = N_Op_Not
then
370 Arg1
:= Right_Opnd
(Op1
);
371 Arg2
:= Right_Opnd
(Op2
);
373 if Kind
= N_Op_And
then
374 Proc_Name
:= RTE
(RE_Vector_Nor
);
375 elsif Kind
= N_Op_Or
then
376 Proc_Name
:= RTE
(RE_Vector_Nand
);
378 Proc_Name
:= RTE
(RE_Vector_Xor
);
382 if Kind
= N_Op_And
then
383 Proc_Name
:= RTE
(RE_Vector_And
);
384 elsif Kind
= N_Op_Or
then
385 Proc_Name
:= RTE
(RE_Vector_Or
);
386 elsif Nkind
(Op2
) = N_Op_Not
then
387 Proc_Name
:= RTE
(RE_Vector_Nxor
);
388 Arg2
:= Right_Opnd
(Op2
);
390 Proc_Name
:= RTE
(RE_Vector_Xor
);
395 Make_Procedure_Call_Statement
(Loc
,
396 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
397 Parameter_Associations
=> New_List
(
399 Make_Attribute_Reference
(Loc
,
401 Attribute_Name
=> Name_Address
),
402 Make_Attribute_Reference
(Loc
,
404 Attribute_Name
=> Name_Address
),
405 Make_Attribute_Reference
(Loc
,
407 Attribute_Name
=> Name_Length
)));
410 Rewrite
(N
, Call_Node
);
414 when RE_Not_Available
=>
416 end Build_Boolean_Array_Proc_Call
;
418 --------------------------------
419 -- Displace_Allocator_Pointer --
420 --------------------------------
422 procedure Displace_Allocator_Pointer
(N
: Node_Id
) is
423 Loc
: constant Source_Ptr
:= Sloc
(N
);
424 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
430 -- Do nothing in case of VM targets: the virtual machine will handle
431 -- interfaces directly.
433 if not Tagged_Type_Expansion
then
437 pragma Assert
(Nkind
(N
) = N_Identifier
438 and then Nkind
(Orig_Node
) = N_Allocator
);
440 PtrT
:= Etype
(Orig_Node
);
441 Dtyp
:= Available_View
(Designated_Type
(PtrT
));
442 Etyp
:= Etype
(Expression
(Orig_Node
));
444 if Is_Class_Wide_Type
(Dtyp
) and then Is_Interface
(Dtyp
) then
446 -- If the type of the allocator expression is not an interface type
447 -- we can generate code to reference the record component containing
448 -- the pointer to the secondary dispatch table.
450 if not Is_Interface
(Etyp
) then
452 Saved_Typ
: constant Entity_Id
:= Etype
(Orig_Node
);
455 -- 1) Get access to the allocated object
458 Make_Explicit_Dereference
(Loc
, Relocate_Node
(N
)));
462 -- 2) Add the conversion to displace the pointer to reference
463 -- the secondary dispatch table.
465 Rewrite
(N
, Convert_To
(Dtyp
, Relocate_Node
(N
)));
466 Analyze_And_Resolve
(N
, Dtyp
);
468 -- 3) The 'access to the secondary dispatch table will be used
469 -- as the value returned by the allocator.
472 Make_Attribute_Reference
(Loc
,
473 Prefix
=> Relocate_Node
(N
),
474 Attribute_Name
=> Name_Access
));
475 Set_Etype
(N
, Saved_Typ
);
479 -- If the type of the allocator expression is an interface type we
480 -- generate a run-time call to displace "this" to reference the
481 -- component containing the pointer to the secondary dispatch table
482 -- or else raise Constraint_Error if the actual object does not
483 -- implement the target interface. This case corresponds to the
484 -- following example:
486 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
488 -- return new Iface_2'Class'(Obj);
493 Unchecked_Convert_To
(PtrT
,
494 Make_Function_Call
(Loc
,
495 Name
=> New_Occurrence_Of
(RTE
(RE_Displace
), Loc
),
496 Parameter_Associations
=> New_List
(
497 Unchecked_Convert_To
(RTE
(RE_Address
),
503 (Access_Disp_Table
(Etype
(Base_Type
(Dtyp
))))),
505 Analyze_And_Resolve
(N
, PtrT
);
508 end Displace_Allocator_Pointer
;
510 ---------------------------------
511 -- Expand_Allocator_Expression --
512 ---------------------------------
514 procedure Expand_Allocator_Expression
(N
: Node_Id
) is
515 Loc
: constant Source_Ptr
:= Sloc
(N
);
516 Exp
: constant Node_Id
:= Expression
(Expression
(N
));
517 PtrT
: constant Entity_Id
:= Etype
(N
);
518 DesigT
: constant Entity_Id
:= Designated_Type
(PtrT
);
520 procedure Apply_Accessibility_Check
522 Built_In_Place
: Boolean := False);
523 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
524 -- type, generate an accessibility check to verify that the level of the
525 -- type of the created object is not deeper than the level of the access
526 -- type. If the type of the qualified expression is class-wide, then
527 -- always generate the check (except in the case where it is known to be
528 -- unnecessary, see comment below). Otherwise, only generate the check
529 -- if the level of the qualified expression type is statically deeper
530 -- than the access type.
532 -- Although the static accessibility will generally have been performed
533 -- as a legality check, it won't have been done in cases where the
534 -- allocator appears in generic body, so a run-time check is needed in
535 -- general. One special case is when the access type is declared in the
536 -- same scope as the class-wide allocator, in which case the check can
537 -- never fail, so it need not be generated.
539 -- As an open issue, there seem to be cases where the static level
540 -- associated with the class-wide object's underlying type is not
541 -- sufficient to perform the proper accessibility check, such as for
542 -- allocators in nested subprograms or accept statements initialized by
543 -- class-wide formals when the actual originates outside at a deeper
544 -- static level. The nested subprogram case might require passing
545 -- accessibility levels along with class-wide parameters, and the task
546 -- case seems to be an actual gap in the language rules that needs to
547 -- be fixed by the ARG. ???
549 -------------------------------
550 -- Apply_Accessibility_Check --
551 -------------------------------
553 procedure Apply_Accessibility_Check
555 Built_In_Place
: Boolean := False)
557 Pool_Id
: constant Entity_Id
:= Associated_Storage_Pool
(PtrT
);
565 if Ada_Version
>= Ada_2005
566 and then Is_Class_Wide_Type
(DesigT
)
567 and then Tagged_Type_Expansion
568 and then not Scope_Suppress
.Suppress
(Accessibility_Check
)
570 (Type_Access_Level
(Etype
(Exp
)) > Type_Access_Level
(PtrT
)
572 (Is_Class_Wide_Type
(Etype
(Exp
))
573 and then Scope
(PtrT
) /= Current_Scope
))
575 -- If the allocator was built in place, Ref is already a reference
576 -- to the access object initialized to the result of the allocator
577 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
578 -- Remove_Side_Effects for cases where the build-in-place call may
579 -- still be the prefix of the reference (to avoid generating
580 -- duplicate calls). Otherwise, it is the entity associated with
581 -- the object containing the address of the allocated object.
583 if Built_In_Place
then
584 Remove_Side_Effects
(Ref
);
585 Obj_Ref
:= New_Copy_Tree
(Ref
);
587 Obj_Ref
:= New_Occurrence_Of
(Ref
, Loc
);
590 -- For access to interface types we must generate code to displace
591 -- the pointer to the base of the object since the subsequent code
592 -- references components located in the TSD of the object (which
593 -- is associated with the primary dispatch table --see a-tags.ads)
594 -- and also generates code invoking Free, which requires also a
595 -- reference to the base of the unallocated object.
597 if Is_Interface
(DesigT
) and then Tagged_Type_Expansion
then
599 Unchecked_Convert_To
(Etype
(Obj_Ref
),
600 Make_Function_Call
(Loc
,
602 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
603 Parameter_Associations
=> New_List
(
604 Unchecked_Convert_To
(RTE
(RE_Address
),
605 New_Copy_Tree
(Obj_Ref
)))));
608 -- Step 1: Create the object clean up code
612 -- Deallocate the object if the accessibility check fails. This
613 -- is done only on targets or profiles that support deallocation.
617 if RTE_Available
(RE_Free
) then
618 Free_Stmt
:= Make_Free_Statement
(Loc
, New_Copy_Tree
(Obj_Ref
));
619 Set_Storage_Pool
(Free_Stmt
, Pool_Id
);
621 Append_To
(Stmts
, Free_Stmt
);
623 -- The target or profile cannot deallocate objects
629 -- Finalize the object if applicable. Generate:
631 -- [Deep_]Finalize (Obj_Ref.all);
633 if Needs_Finalization
(DesigT
)
634 and then not No_Heap_Finalization
(PtrT
)
639 Make_Explicit_Dereference
(Loc
, New_Copy
(Obj_Ref
)),
642 -- Guard against a missing [Deep_]Finalize when the designated
643 -- type was not properly frozen.
645 if No
(Fin_Call
) then
646 Fin_Call
:= Make_Null_Statement
(Loc
);
649 -- When the target or profile supports deallocation, wrap the
650 -- finalization call in a block to ensure proper deallocation
651 -- even if finalization fails. Generate:
661 if Present
(Free_Stmt
) then
663 Make_Block_Statement
(Loc
,
664 Handled_Statement_Sequence
=>
665 Make_Handled_Sequence_Of_Statements
(Loc
,
666 Statements
=> New_List
(Fin_Call
),
668 Exception_Handlers
=> New_List
(
669 Make_Exception_Handler
(Loc
,
670 Exception_Choices
=> New_List
(
671 Make_Others_Choice
(Loc
)),
672 Statements
=> New_List
(
673 New_Copy_Tree
(Free_Stmt
),
674 Make_Raise_Statement
(Loc
))))));
677 Prepend_To
(Stmts
, Fin_Call
);
680 -- Signal the accessibility failure through a Program_Error
683 Make_Raise_Program_Error
(Loc
,
684 Condition
=> New_Occurrence_Of
(Standard_True
, Loc
),
685 Reason
=> PE_Accessibility_Check_Failed
));
687 -- Step 2: Create the accessibility comparison
693 Make_Attribute_Reference
(Loc
,
695 Attribute_Name
=> Name_Tag
);
697 -- For tagged types, determine the accessibility level by looking
698 -- at the type specific data of the dispatch table. Generate:
700 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
702 if Tagged_Type_Expansion
then
703 Cond
:= Build_Get_Access_Level
(Loc
, Obj_Ref
);
705 -- Use a runtime call to determine the accessibility level when
706 -- compiling on virtual machine targets. Generate:
708 -- Get_Access_Level (Ref'Tag)
712 Make_Function_Call
(Loc
,
714 New_Occurrence_Of
(RTE
(RE_Get_Access_Level
), Loc
),
715 Parameter_Associations
=> New_List
(Obj_Ref
));
722 Make_Integer_Literal
(Loc
, Type_Access_Level
(PtrT
)));
724 -- Due to the complexity and side effects of the check, utilize an
725 -- if statement instead of the regular Program_Error circuitry.
728 Make_Implicit_If_Statement
(N
,
730 Then_Statements
=> Stmts
));
732 end Apply_Accessibility_Check
;
736 Aggr_In_Place
: constant Boolean := Is_Delayed_Aggregate
(Exp
);
737 Indic
: constant Node_Id
:= Subtype_Mark
(Expression
(N
));
738 T
: constant Entity_Id
:= Entity
(Indic
);
741 Tag_Assign
: Node_Id
;
745 TagT
: Entity_Id
:= Empty
;
746 -- Type used as source for tag assignment
748 TagR
: Node_Id
:= Empty
;
749 -- Target reference for tag assignment
751 -- Start of processing for Expand_Allocator_Expression
754 -- Handle call to C++ constructor
756 if Is_CPP_Constructor_Call
(Exp
) then
757 Make_CPP_Constructor_Call_In_Allocator
759 Function_Call
=> Exp
);
763 -- In the case of an Ada 2012 allocator whose initial value comes from a
764 -- function call, pass "the accessibility level determined by the point
765 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
766 -- Expand_Call but it couldn't be done there (because the Etype of the
767 -- allocator wasn't set then) so we generate the parameter here. See
768 -- the Boolean variable Defer in (a block within) Expand_Call.
770 if Ada_Version
>= Ada_2012
and then Nkind
(Exp
) = N_Function_Call
then
775 if Nkind
(Name
(Exp
)) = N_Explicit_Dereference
then
776 Subp
:= Designated_Type
(Etype
(Prefix
(Name
(Exp
))));
778 Subp
:= Entity
(Name
(Exp
));
781 Subp
:= Ultimate_Alias
(Subp
);
783 if Present
(Extra_Accessibility_Of_Result
(Subp
)) then
784 Add_Extra_Actual_To_Call
785 (Subprogram_Call
=> Exp
,
786 Extra_Formal
=> Extra_Accessibility_Of_Result
(Subp
),
787 Extra_Actual
=> Dynamic_Accessibility_Level
(PtrT
));
792 -- Case of tagged type or type requiring finalization
794 if Is_Tagged_Type
(T
) or else Needs_Finalization
(T
) then
796 -- Ada 2005 (AI-318-02): If the initialization expression is a call
797 -- to a build-in-place function, then access to the allocated object
798 -- must be passed to the function.
800 if Is_Build_In_Place_Function_Call
(Exp
) then
801 Make_Build_In_Place_Call_In_Allocator
(N
, Exp
);
802 Apply_Accessibility_Check
(N
, Built_In_Place
=> True);
805 -- Ada 2005 (AI-318-02): Specialization of the previous case for
806 -- expressions containing a build-in-place function call whose
807 -- returned object covers interface types, and Expr has calls to
808 -- Ada.Tags.Displace to displace the pointer to the returned build-
809 -- in-place object to reference the secondary dispatch table of a
810 -- covered interface type.
812 elsif Present
(Unqual_BIP_Iface_Function_Call
(Exp
)) then
813 Make_Build_In_Place_Iface_Call_In_Allocator
(N
, Exp
);
814 Apply_Accessibility_Check
(N
, Built_In_Place
=> True);
818 -- Actions inserted before:
819 -- Temp : constant ptr_T := new T'(Expression);
820 -- Temp._tag = T'tag; -- when not class-wide
821 -- [Deep_]Adjust (Temp.all);
823 -- We analyze by hand the new internal allocator to avoid any
824 -- recursion and inappropriate call to Initialize.
826 -- We don't want to remove side effects when the expression must be
827 -- built in place. In the case of a build-in-place function call,
828 -- that could lead to a duplication of the call, which was already
829 -- substituted for the allocator.
831 if not Aggr_In_Place
then
832 Remove_Side_Effects
(Exp
);
835 Temp
:= Make_Temporary
(Loc
, 'P', N
);
837 -- For a class wide allocation generate the following code:
839 -- type Equiv_Record is record ... end record;
840 -- implicit subtype CW is <Class_Wide_Subytpe>;
841 -- temp : PtrT := new CW'(CW!(expr));
843 if Is_Class_Wide_Type
(T
) then
844 Expand_Subtype_From_Expr
(Empty
, T
, Indic
, Exp
);
846 -- Ada 2005 (AI-251): If the expression is a class-wide interface
847 -- object we generate code to move up "this" to reference the
848 -- base of the object before allocating the new object.
850 -- Note that Exp'Address is recursively expanded into a call
851 -- to Base_Address (Exp.Tag)
853 if Is_Class_Wide_Type
(Etype
(Exp
))
854 and then Is_Interface
(Etype
(Exp
))
855 and then Tagged_Type_Expansion
859 Unchecked_Convert_To
(Entity
(Indic
),
860 Make_Explicit_Dereference
(Loc
,
861 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
862 Make_Attribute_Reference
(Loc
,
864 Attribute_Name
=> Name_Address
)))));
868 Unchecked_Convert_To
(Entity
(Indic
), Exp
));
871 Analyze_And_Resolve
(Expression
(N
), Entity
(Indic
));
874 -- Processing for allocators returning non-interface types
876 if not Is_Interface
(Directly_Designated_Type
(PtrT
)) then
877 if Aggr_In_Place
then
879 Make_Object_Declaration
(Loc
,
880 Defining_Identifier
=> Temp
,
881 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
885 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
887 -- Copy the Comes_From_Source flag for the allocator we just
888 -- built, since logically this allocator is a replacement of
889 -- the original allocator node. This is for proper handling of
890 -- restriction No_Implicit_Heap_Allocations.
892 Set_Comes_From_Source
893 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
895 Set_No_Initialization
(Expression
(Temp_Decl
));
896 Insert_Action
(N
, Temp_Decl
);
898 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
899 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
902 Node
:= Relocate_Node
(N
);
906 Make_Object_Declaration
(Loc
,
907 Defining_Identifier
=> Temp
,
908 Constant_Present
=> True,
909 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
912 Insert_Action
(N
, Temp_Decl
);
913 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
916 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
917 -- interface type. In this case we use the type of the qualified
918 -- expression to allocate the object.
922 Def_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
927 Make_Full_Type_Declaration
(Loc
,
928 Defining_Identifier
=> Def_Id
,
930 Make_Access_To_Object_Definition
(Loc
,
932 Null_Exclusion_Present
=> False,
934 Is_Access_Constant
(Etype
(N
)),
935 Subtype_Indication
=>
936 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
938 Insert_Action
(N
, New_Decl
);
940 -- Inherit the allocation-related attributes from the original
943 Set_Finalization_Master
944 (Def_Id
, Finalization_Master
(PtrT
));
946 Set_Associated_Storage_Pool
947 (Def_Id
, Associated_Storage_Pool
(PtrT
));
949 -- Declare the object using the previous type declaration
951 if Aggr_In_Place
then
953 Make_Object_Declaration
(Loc
,
954 Defining_Identifier
=> Temp
,
955 Object_Definition
=> New_Occurrence_Of
(Def_Id
, Loc
),
958 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
960 -- Copy the Comes_From_Source flag for the allocator we just
961 -- built, since logically this allocator is a replacement of
962 -- the original allocator node. This is for proper handling
963 -- of restriction No_Implicit_Heap_Allocations.
965 Set_Comes_From_Source
966 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
968 Set_No_Initialization
(Expression
(Temp_Decl
));
969 Insert_Action
(N
, Temp_Decl
);
971 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
972 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
975 Node
:= Relocate_Node
(N
);
979 Make_Object_Declaration
(Loc
,
980 Defining_Identifier
=> Temp
,
981 Constant_Present
=> True,
982 Object_Definition
=> New_Occurrence_Of
(Def_Id
, Loc
),
985 Insert_Action
(N
, Temp_Decl
);
986 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
989 -- Generate an additional object containing the address of the
990 -- returned object. The type of this second object declaration
991 -- is the correct type required for the common processing that
992 -- is still performed by this subprogram. The displacement of
993 -- this pointer to reference the component associated with the
994 -- interface type will be done at the end of common processing.
997 Make_Object_Declaration
(Loc
,
998 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
999 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
1001 Unchecked_Convert_To
(PtrT
,
1002 New_Occurrence_Of
(Temp
, Loc
)));
1004 Insert_Action
(N
, New_Decl
);
1006 Temp_Decl
:= New_Decl
;
1007 Temp
:= Defining_Identifier
(New_Decl
);
1011 -- Generate the tag assignment
1013 -- Suppress the tag assignment for VM targets because VM tags are
1014 -- represented implicitly in objects.
1016 if not Tagged_Type_Expansion
then
1019 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1020 -- interface objects because in this case the tag does not change.
1022 elsif Is_Interface
(Directly_Designated_Type
(Etype
(N
))) then
1023 pragma Assert
(Is_Class_Wide_Type
1024 (Directly_Designated_Type
(Etype
(N
))));
1027 elsif Is_Tagged_Type
(T
) and then not Is_Class_Wide_Type
(T
) then
1029 TagR
:= New_Occurrence_Of
(Temp
, Loc
);
1031 elsif Is_Private_Type
(T
)
1032 and then Is_Tagged_Type
(Underlying_Type
(T
))
1034 TagT
:= Underlying_Type
(T
);
1036 Unchecked_Convert_To
(Underlying_Type
(T
),
1037 Make_Explicit_Dereference
(Loc
,
1038 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
1041 if Present
(TagT
) then
1043 Full_T
: constant Entity_Id
:= Underlying_Type
(TagT
);
1047 Make_Assignment_Statement
(Loc
,
1049 Make_Selected_Component
(Loc
,
1053 (First_Tag_Component
(Full_T
), Loc
)),
1056 Unchecked_Convert_To
(RTE
(RE_Tag
),
1059 (First_Elmt
(Access_Disp_Table
(Full_T
))), Loc
)));
1062 -- The previous assignment has to be done in any case
1064 Set_Assignment_OK
(Name
(Tag_Assign
));
1065 Insert_Action
(N
, Tag_Assign
);
1068 -- Generate an Adjust call if the object will be moved. In Ada 2005,
1069 -- the object may be inherently limited, in which case there is no
1070 -- Adjust procedure, and the object is built in place. In Ada 95, the
1071 -- object can be limited but not inherently limited if this allocator
1072 -- came from a return statement (we're allocating the result on the
1073 -- secondary stack). In that case, the object will be moved, so we do
1074 -- want to Adjust. However, if it's a nonlimited build-in-place
1075 -- function call, Adjust is not wanted.
1077 if Needs_Finalization
(DesigT
)
1078 and then Needs_Finalization
(T
)
1079 and then not Aggr_In_Place
1080 and then not Is_Limited_View
(T
)
1081 and then not Alloc_For_BIP_Return
(N
)
1082 and then not Is_Build_In_Place_Function_Call
(Expression
(N
))
1084 -- An unchecked conversion is needed in the classwide case because
1085 -- the designated type can be an ancestor of the subtype mark of
1091 Unchecked_Convert_To
(T
,
1092 Make_Explicit_Dereference
(Loc
,
1093 Prefix
=> New_Occurrence_Of
(Temp
, Loc
))),
1096 if Present
(Adj_Call
) then
1097 Insert_Action
(N
, Adj_Call
);
1101 -- Note: the accessibility check must be inserted after the call to
1102 -- [Deep_]Adjust to ensure proper completion of the assignment.
1104 Apply_Accessibility_Check
(Temp
);
1106 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
1107 Analyze_And_Resolve
(N
, PtrT
);
1109 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1110 -- component containing the secondary dispatch table of the interface
1113 if Is_Interface
(Directly_Designated_Type
(PtrT
)) then
1114 Displace_Allocator_Pointer
(N
);
1117 -- Always force the generation of a temporary for aggregates when
1118 -- generating C code, to simplify the work in the code generator.
1121 or else (Modify_Tree_For_C
and then Nkind
(Exp
) = N_Aggregate
)
1123 Temp
:= Make_Temporary
(Loc
, 'P', N
);
1125 Make_Object_Declaration
(Loc
,
1126 Defining_Identifier
=> Temp
,
1127 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
1129 Make_Allocator
(Loc
,
1130 Expression
=> New_Occurrence_Of
(Etype
(Exp
), Loc
)));
1132 -- Copy the Comes_From_Source flag for the allocator we just built,
1133 -- since logically this allocator is a replacement of the original
1134 -- allocator node. This is for proper handling of restriction
1135 -- No_Implicit_Heap_Allocations.
1137 Set_Comes_From_Source
1138 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
1140 Set_No_Initialization
(Expression
(Temp_Decl
));
1141 Insert_Action
(N
, Temp_Decl
);
1143 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1144 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
1146 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
1147 Analyze_And_Resolve
(N
, PtrT
);
1149 elsif Is_Access_Type
(T
) and then Can_Never_Be_Null
(T
) then
1150 Install_Null_Excluding_Check
(Exp
);
1152 elsif Is_Access_Type
(DesigT
)
1153 and then Nkind
(Exp
) = N_Allocator
1154 and then Nkind
(Expression
(Exp
)) /= N_Qualified_Expression
1156 -- Apply constraint to designated subtype indication
1158 Apply_Constraint_Check
1159 (Expression
(Exp
), Designated_Type
(DesigT
), No_Sliding
=> True);
1161 if Nkind
(Expression
(Exp
)) = N_Raise_Constraint_Error
then
1163 -- Propagate constraint_error to enclosing allocator
1165 Rewrite
(Exp
, New_Copy
(Expression
(Exp
)));
1169 Build_Allocate_Deallocate_Proc
(N
, True);
1172 -- type A is access T1;
1173 -- X : A := new T2'(...);
1174 -- T1 and T2 can be different subtypes, and we might need to check
1175 -- both constraints. First check against the type of the qualified
1178 Apply_Constraint_Check
(Exp
, T
, No_Sliding
=> True);
1180 if Do_Range_Check
(Exp
) then
1181 Generate_Range_Check
(Exp
, DesigT
, CE_Range_Check_Failed
);
1184 -- A check is also needed in cases where the designated subtype is
1185 -- constrained and differs from the subtype given in the qualified
1186 -- expression. Note that the check on the qualified expression does
1187 -- not allow sliding, but this check does (a relaxation from Ada 83).
1189 if Is_Constrained
(DesigT
)
1190 and then not Subtypes_Statically_Match
(T
, DesigT
)
1192 Apply_Constraint_Check
1193 (Exp
, DesigT
, No_Sliding
=> False);
1195 if Do_Range_Check
(Exp
) then
1196 Generate_Range_Check
(Exp
, DesigT
, CE_Range_Check_Failed
);
1200 -- For an access to unconstrained packed array, GIGI needs to see an
1201 -- expression with a constrained subtype in order to compute the
1202 -- proper size for the allocator.
1204 if Is_Array_Type
(T
)
1205 and then not Is_Constrained
(T
)
1206 and then Is_Packed
(T
)
1209 ConstrT
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
1210 Internal_Exp
: constant Node_Id
:= Relocate_Node
(Exp
);
1213 Make_Subtype_Declaration
(Loc
,
1214 Defining_Identifier
=> ConstrT
,
1215 Subtype_Indication
=>
1216 Make_Subtype_From_Expr
(Internal_Exp
, T
)));
1217 Freeze_Itype
(ConstrT
, Exp
);
1218 Rewrite
(Exp
, OK_Convert_To
(ConstrT
, Internal_Exp
));
1222 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1223 -- to a build-in-place function, then access to the allocated object
1224 -- must be passed to the function.
1226 if Is_Build_In_Place_Function_Call
(Exp
) then
1227 Make_Build_In_Place_Call_In_Allocator
(N
, Exp
);
1232 when RE_Not_Available
=>
1234 end Expand_Allocator_Expression
;
1236 -----------------------------
1237 -- Expand_Array_Comparison --
1238 -----------------------------
1240 -- Expansion is only required in the case of array types. For the unpacked
1241 -- case, an appropriate runtime routine is called. For packed cases, and
1242 -- also in some other cases where a runtime routine cannot be called, the
1243 -- form of the expansion is:
1245 -- [body for greater_nn; boolean_expression]
1247 -- The body is built by Make_Array_Comparison_Op, and the form of the
1248 -- Boolean expression depends on the operator involved.
1250 procedure Expand_Array_Comparison
(N
: Node_Id
) is
1251 Loc
: constant Source_Ptr
:= Sloc
(N
);
1252 Op1
: Node_Id
:= Left_Opnd
(N
);
1253 Op2
: Node_Id
:= Right_Opnd
(N
);
1254 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
1255 Ctyp
: constant Entity_Id
:= Component_Type
(Typ1
);
1258 Func_Body
: Node_Id
;
1259 Func_Name
: Entity_Id
;
1263 Byte_Addressable
: constant Boolean := System_Storage_Unit
= Byte
'Size;
1264 -- True for byte addressable target
1266 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean;
1267 -- Returns True if the length of the given operand is known to be less
1268 -- than 4. Returns False if this length is known to be four or greater
1269 -- or is not known at compile time.
1271 ------------------------
1272 -- Length_Less_Than_4 --
1273 ------------------------
1275 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean is
1276 Otyp
: constant Entity_Id
:= Etype
(Opnd
);
1279 if Ekind
(Otyp
) = E_String_Literal_Subtype
then
1280 return String_Literal_Length
(Otyp
) < 4;
1284 Ityp
: constant Entity_Id
:= Etype
(First_Index
(Otyp
));
1285 Lo
: constant Node_Id
:= Type_Low_Bound
(Ityp
);
1286 Hi
: constant Node_Id
:= Type_High_Bound
(Ityp
);
1291 if Compile_Time_Known_Value
(Lo
) then
1292 Lov
:= Expr_Value
(Lo
);
1297 if Compile_Time_Known_Value
(Hi
) then
1298 Hiv
:= Expr_Value
(Hi
);
1303 return Hiv
< Lov
+ 3;
1306 end Length_Less_Than_4
;
1308 -- Start of processing for Expand_Array_Comparison
1311 -- Deal first with unpacked case, where we can call a runtime routine
1312 -- except that we avoid this for targets for which are not addressable
1315 if not Is_Bit_Packed_Array
(Typ1
)
1316 and then Byte_Addressable
1318 -- The call we generate is:
1320 -- Compare_Array_xn[_Unaligned]
1321 -- (left'address, right'address, left'length, right'length) <op> 0
1323 -- x = U for unsigned, S for signed
1324 -- n = 8,16,32,64 for component size
1325 -- Add _Unaligned if length < 4 and component size is 8.
1326 -- <op> is the standard comparison operator
1328 if Component_Size
(Typ1
) = 8 then
1329 if Length_Less_Than_4
(Op1
)
1331 Length_Less_Than_4
(Op2
)
1333 if Is_Unsigned_Type
(Ctyp
) then
1334 Comp
:= RE_Compare_Array_U8_Unaligned
;
1336 Comp
:= RE_Compare_Array_S8_Unaligned
;
1340 if Is_Unsigned_Type
(Ctyp
) then
1341 Comp
:= RE_Compare_Array_U8
;
1343 Comp
:= RE_Compare_Array_S8
;
1347 elsif Component_Size
(Typ1
) = 16 then
1348 if Is_Unsigned_Type
(Ctyp
) then
1349 Comp
:= RE_Compare_Array_U16
;
1351 Comp
:= RE_Compare_Array_S16
;
1354 elsif Component_Size
(Typ1
) = 32 then
1355 if Is_Unsigned_Type
(Ctyp
) then
1356 Comp
:= RE_Compare_Array_U32
;
1358 Comp
:= RE_Compare_Array_S32
;
1361 else pragma Assert
(Component_Size
(Typ1
) = 64);
1362 if Is_Unsigned_Type
(Ctyp
) then
1363 Comp
:= RE_Compare_Array_U64
;
1365 Comp
:= RE_Compare_Array_S64
;
1369 if RTE_Available
(Comp
) then
1371 -- Expand to a call only if the runtime function is available,
1372 -- otherwise fall back to inline code.
1374 Remove_Side_Effects
(Op1
, Name_Req
=> True);
1375 Remove_Side_Effects
(Op2
, Name_Req
=> True);
1378 Make_Function_Call
(Sloc
(Op1
),
1379 Name
=> New_Occurrence_Of
(RTE
(Comp
), Loc
),
1381 Parameter_Associations
=> New_List
(
1382 Make_Attribute_Reference
(Loc
,
1383 Prefix
=> Relocate_Node
(Op1
),
1384 Attribute_Name
=> Name_Address
),
1386 Make_Attribute_Reference
(Loc
,
1387 Prefix
=> Relocate_Node
(Op2
),
1388 Attribute_Name
=> Name_Address
),
1390 Make_Attribute_Reference
(Loc
,
1391 Prefix
=> Relocate_Node
(Op1
),
1392 Attribute_Name
=> Name_Length
),
1394 Make_Attribute_Reference
(Loc
,
1395 Prefix
=> Relocate_Node
(Op2
),
1396 Attribute_Name
=> Name_Length
))));
1399 Make_Integer_Literal
(Sloc
(Op2
),
1402 Analyze_And_Resolve
(Op1
, Standard_Integer
);
1403 Analyze_And_Resolve
(Op2
, Standard_Integer
);
1408 -- Cases where we cannot make runtime call
1410 -- For (a <= b) we convert to not (a > b)
1412 if Chars
(N
) = Name_Op_Le
then
1418 Right_Opnd
=> Op2
)));
1419 Analyze_And_Resolve
(N
, Standard_Boolean
);
1422 -- For < the Boolean expression is
1423 -- greater__nn (op2, op1)
1425 elsif Chars
(N
) = Name_Op_Lt
then
1426 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
1430 Op1
:= Right_Opnd
(N
);
1431 Op2
:= Left_Opnd
(N
);
1433 -- For (a >= b) we convert to not (a < b)
1435 elsif Chars
(N
) = Name_Op_Ge
then
1441 Right_Opnd
=> Op2
)));
1442 Analyze_And_Resolve
(N
, Standard_Boolean
);
1445 -- For > the Boolean expression is
1446 -- greater__nn (op1, op2)
1449 pragma Assert
(Chars
(N
) = Name_Op_Gt
);
1450 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
1453 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
1455 Make_Function_Call
(Loc
,
1456 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
1457 Parameter_Associations
=> New_List
(Op1
, Op2
));
1459 Insert_Action
(N
, Func_Body
);
1461 Analyze_And_Resolve
(N
, Standard_Boolean
);
1462 end Expand_Array_Comparison
;
1464 ---------------------------
1465 -- Expand_Array_Equality --
1466 ---------------------------
1468 -- Expand an equality function for multi-dimensional arrays. Here is an
1469 -- example of such a function for Nb_Dimension = 2
1471 -- function Enn (A : atyp; B : btyp) return boolean is
1473 -- if (A'length (1) = 0 or else A'length (2) = 0)
1475 -- (B'length (1) = 0 or else B'length (2) = 0)
1477 -- return True; -- RM 4.5.2(22)
1480 -- if A'length (1) /= B'length (1)
1482 -- A'length (2) /= B'length (2)
1484 -- return False; -- RM 4.5.2(23)
1488 -- A1 : Index_T1 := A'first (1);
1489 -- B1 : Index_T1 := B'first (1);
1493 -- A2 : Index_T2 := A'first (2);
1494 -- B2 : Index_T2 := B'first (2);
1497 -- if A (A1, A2) /= B (B1, B2) then
1501 -- exit when A2 = A'last (2);
1502 -- A2 := Index_T2'succ (A2);
1503 -- B2 := Index_T2'succ (B2);
1507 -- exit when A1 = A'last (1);
1508 -- A1 := Index_T1'succ (A1);
1509 -- B1 := Index_T1'succ (B1);
1516 -- Note on the formal types used (atyp and btyp). If either of the arrays
1517 -- is of a private type, we use the underlying type, and do an unchecked
1518 -- conversion of the actual. If either of the arrays has a bound depending
1519 -- on a discriminant, then we use the base type since otherwise we have an
1520 -- escaped discriminant in the function.
1522 -- If both arrays are constrained and have the same bounds, we can generate
1523 -- a loop with an explicit iteration scheme using a 'Range attribute over
1526 function Expand_Array_Equality
1531 Typ
: Entity_Id
) return Node_Id
1533 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
1534 Decls
: constant List_Id
:= New_List
;
1535 Index_List1
: constant List_Id
:= New_List
;
1536 Index_List2
: constant List_Id
:= New_List
;
1540 Func_Name
: Entity_Id
;
1541 Func_Body
: Node_Id
;
1543 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
1544 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
1548 -- The parameter types to be used for the formals
1553 Num
: Int
) return Node_Id
;
1554 -- This builds the attribute reference Arr'Nam (Expr)
1556 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
;
1557 -- Create one statement to compare corresponding components, designated
1558 -- by a full set of indexes.
1560 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
;
1561 -- Given one of the arguments, computes the appropriate type to be used
1562 -- for that argument in the corresponding function formal
1564 function Handle_One_Dimension
1566 Index
: Node_Id
) return Node_Id
;
1567 -- This procedure returns the following code
1570 -- Bn : Index_T := B'First (N);
1574 -- exit when An = A'Last (N);
1575 -- An := Index_T'Succ (An)
1576 -- Bn := Index_T'Succ (Bn)
1580 -- If both indexes are constrained and identical, the procedure
1581 -- returns a simpler loop:
1583 -- for An in A'Range (N) loop
1587 -- N is the dimension for which we are generating a loop. Index is the
1588 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1589 -- xxx statement is either the loop or declare for the next dimension
1590 -- or if this is the last dimension the comparison of corresponding
1591 -- components of the arrays.
1593 -- The actual way the code works is to return the comparison of
1594 -- corresponding components for the N+1 call. That's neater.
1596 function Test_Empty_Arrays
return Node_Id
;
1597 -- This function constructs the test for both arrays being empty
1598 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1600 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1602 function Test_Lengths_Correspond
return Node_Id
;
1603 -- This function constructs the test for arrays having different lengths
1604 -- in at least one index position, in which case the resulting code is:
1606 -- A'length (1) /= B'length (1)
1608 -- A'length (2) /= B'length (2)
1619 Num
: Int
) return Node_Id
1623 Make_Attribute_Reference
(Loc
,
1624 Attribute_Name
=> Nam
,
1625 Prefix
=> New_Occurrence_Of
(Arr
, Loc
),
1626 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, Num
)));
1629 ------------------------
1630 -- Component_Equality --
1631 ------------------------
1633 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
is
1638 -- if a(i1...) /= b(j1...) then return false; end if;
1641 Make_Indexed_Component
(Loc
,
1642 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
1643 Expressions
=> Index_List1
);
1646 Make_Indexed_Component
(Loc
,
1647 Prefix
=> Make_Identifier
(Loc
, Chars
(B
)),
1648 Expressions
=> Index_List2
);
1650 Test
:= Expand_Composite_Equality
1651 (Nod
, Component_Type
(Typ
), L
, R
, Decls
);
1653 -- If some (sub)component is an unchecked_union, the whole operation
1654 -- will raise program error.
1656 if Nkind
(Test
) = N_Raise_Program_Error
then
1658 -- This node is going to be inserted at a location where a
1659 -- statement is expected: clear its Etype so analysis will set
1660 -- it to the expected Standard_Void_Type.
1662 Set_Etype
(Test
, Empty
);
1667 Make_Implicit_If_Statement
(Nod
,
1668 Condition
=> Make_Op_Not
(Loc
, Right_Opnd
=> Test
),
1669 Then_Statements
=> New_List
(
1670 Make_Simple_Return_Statement
(Loc
,
1671 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))));
1673 end Component_Equality
;
1679 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
is
1690 T
:= Underlying_Type
(T
);
1692 X
:= First_Index
(T
);
1693 while Present
(X
) loop
1694 if Denotes_Discriminant
(Type_Low_Bound
(Etype
(X
)))
1696 Denotes_Discriminant
(Type_High_Bound
(Etype
(X
)))
1709 --------------------------
1710 -- Handle_One_Dimension --
1711 ---------------------------
1713 function Handle_One_Dimension
1715 Index
: Node_Id
) return Node_Id
1717 Need_Separate_Indexes
: constant Boolean :=
1718 Ltyp
/= Rtyp
or else not Is_Constrained
(Ltyp
);
1719 -- If the index types are identical, and we are working with
1720 -- constrained types, then we can use the same index for both
1723 An
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
1726 Index_T
: Entity_Id
;
1731 if N
> Number_Dimensions
(Ltyp
) then
1732 return Component_Equality
(Ltyp
);
1735 -- Case where we generate a loop
1737 Index_T
:= Base_Type
(Etype
(Index
));
1739 if Need_Separate_Indexes
then
1740 Bn
:= Make_Temporary
(Loc
, 'B');
1745 Append
(New_Occurrence_Of
(An
, Loc
), Index_List1
);
1746 Append
(New_Occurrence_Of
(Bn
, Loc
), Index_List2
);
1748 Stm_List
:= New_List
(
1749 Handle_One_Dimension
(N
+ 1, Next_Index
(Index
)));
1751 if Need_Separate_Indexes
then
1753 -- Generate guard for loop, followed by increments of indexes
1755 Append_To
(Stm_List
,
1756 Make_Exit_Statement
(Loc
,
1759 Left_Opnd
=> New_Occurrence_Of
(An
, Loc
),
1760 Right_Opnd
=> Arr_Attr
(A
, Name_Last
, N
))));
1762 Append_To
(Stm_List
,
1763 Make_Assignment_Statement
(Loc
,
1764 Name
=> New_Occurrence_Of
(An
, Loc
),
1766 Make_Attribute_Reference
(Loc
,
1767 Prefix
=> New_Occurrence_Of
(Index_T
, Loc
),
1768 Attribute_Name
=> Name_Succ
,
1769 Expressions
=> New_List
(
1770 New_Occurrence_Of
(An
, Loc
)))));
1772 Append_To
(Stm_List
,
1773 Make_Assignment_Statement
(Loc
,
1774 Name
=> New_Occurrence_Of
(Bn
, Loc
),
1776 Make_Attribute_Reference
(Loc
,
1777 Prefix
=> New_Occurrence_Of
(Index_T
, Loc
),
1778 Attribute_Name
=> Name_Succ
,
1779 Expressions
=> New_List
(
1780 New_Occurrence_Of
(Bn
, Loc
)))));
1783 -- If separate indexes, we need a declare block for An and Bn, and a
1784 -- loop without an iteration scheme.
1786 if Need_Separate_Indexes
then
1788 Make_Implicit_Loop_Statement
(Nod
, Statements
=> Stm_List
);
1791 Make_Block_Statement
(Loc
,
1792 Declarations
=> New_List
(
1793 Make_Object_Declaration
(Loc
,
1794 Defining_Identifier
=> An
,
1795 Object_Definition
=> New_Occurrence_Of
(Index_T
, Loc
),
1796 Expression
=> Arr_Attr
(A
, Name_First
, N
)),
1798 Make_Object_Declaration
(Loc
,
1799 Defining_Identifier
=> Bn
,
1800 Object_Definition
=> New_Occurrence_Of
(Index_T
, Loc
),
1801 Expression
=> Arr_Attr
(B
, Name_First
, N
))),
1803 Handled_Statement_Sequence
=>
1804 Make_Handled_Sequence_Of_Statements
(Loc
,
1805 Statements
=> New_List
(Loop_Stm
)));
1807 -- If no separate indexes, return loop statement with explicit
1808 -- iteration scheme on its own
1812 Make_Implicit_Loop_Statement
(Nod
,
1813 Statements
=> Stm_List
,
1815 Make_Iteration_Scheme
(Loc
,
1816 Loop_Parameter_Specification
=>
1817 Make_Loop_Parameter_Specification
(Loc
,
1818 Defining_Identifier
=> An
,
1819 Discrete_Subtype_Definition
=>
1820 Arr_Attr
(A
, Name_Range
, N
))));
1823 end Handle_One_Dimension
;
1825 -----------------------
1826 -- Test_Empty_Arrays --
1827 -----------------------
1829 function Test_Empty_Arrays
return Node_Id
is
1839 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
1842 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
1843 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
1847 Left_Opnd
=> Arr_Attr
(B
, Name_Length
, J
),
1848 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
1857 Left_Opnd
=> Relocate_Node
(Alist
),
1858 Right_Opnd
=> Atest
);
1862 Left_Opnd
=> Relocate_Node
(Blist
),
1863 Right_Opnd
=> Btest
);
1870 Right_Opnd
=> Blist
);
1871 end Test_Empty_Arrays
;
1873 -----------------------------
1874 -- Test_Lengths_Correspond --
1875 -----------------------------
1877 function Test_Lengths_Correspond
return Node_Id
is
1883 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
1886 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
1887 Right_Opnd
=> Arr_Attr
(B
, Name_Length
, J
));
1894 Left_Opnd
=> Relocate_Node
(Result
),
1895 Right_Opnd
=> Rtest
);
1900 end Test_Lengths_Correspond
;
1902 -- Start of processing for Expand_Array_Equality
1905 Ltyp
:= Get_Arg_Type
(Lhs
);
1906 Rtyp
:= Get_Arg_Type
(Rhs
);
1908 -- For now, if the argument types are not the same, go to the base type,
1909 -- since the code assumes that the formals have the same type. This is
1910 -- fixable in future ???
1912 if Ltyp
/= Rtyp
then
1913 Ltyp
:= Base_Type
(Ltyp
);
1914 Rtyp
:= Base_Type
(Rtyp
);
1915 pragma Assert
(Ltyp
= Rtyp
);
1918 -- Build list of formals for function
1920 Formals
:= New_List
(
1921 Make_Parameter_Specification
(Loc
,
1922 Defining_Identifier
=> A
,
1923 Parameter_Type
=> New_Occurrence_Of
(Ltyp
, Loc
)),
1925 Make_Parameter_Specification
(Loc
,
1926 Defining_Identifier
=> B
,
1927 Parameter_Type
=> New_Occurrence_Of
(Rtyp
, Loc
)));
1929 Func_Name
:= Make_Temporary
(Loc
, 'E');
1931 -- Build statement sequence for function
1934 Make_Subprogram_Body
(Loc
,
1936 Make_Function_Specification
(Loc
,
1937 Defining_Unit_Name
=> Func_Name
,
1938 Parameter_Specifications
=> Formals
,
1939 Result_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
)),
1941 Declarations
=> Decls
,
1943 Handled_Statement_Sequence
=>
1944 Make_Handled_Sequence_Of_Statements
(Loc
,
1945 Statements
=> New_List
(
1947 Make_Implicit_If_Statement
(Nod
,
1948 Condition
=> Test_Empty_Arrays
,
1949 Then_Statements
=> New_List
(
1950 Make_Simple_Return_Statement
(Loc
,
1952 New_Occurrence_Of
(Standard_True
, Loc
)))),
1954 Make_Implicit_If_Statement
(Nod
,
1955 Condition
=> Test_Lengths_Correspond
,
1956 Then_Statements
=> New_List
(
1957 Make_Simple_Return_Statement
(Loc
,
1958 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
)))),
1960 Handle_One_Dimension
(1, First_Index
(Ltyp
)),
1962 Make_Simple_Return_Statement
(Loc
,
1963 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)))));
1965 Set_Has_Completion
(Func_Name
, True);
1966 Set_Is_Inlined
(Func_Name
);
1968 -- If the array type is distinct from the type of the arguments, it
1969 -- is the full view of a private type. Apply an unchecked conversion
1970 -- to insure that analysis of the call succeeds.
1980 or else Base_Type
(Etype
(Lhs
)) /= Base_Type
(Ltyp
)
1982 L
:= OK_Convert_To
(Ltyp
, Lhs
);
1986 or else Base_Type
(Etype
(Rhs
)) /= Base_Type
(Rtyp
)
1988 R
:= OK_Convert_To
(Rtyp
, Rhs
);
1991 Actuals
:= New_List
(L
, R
);
1994 Append_To
(Bodies
, Func_Body
);
1997 Make_Function_Call
(Loc
,
1998 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
1999 Parameter_Associations
=> Actuals
);
2000 end Expand_Array_Equality
;
2002 -----------------------------
2003 -- Expand_Boolean_Operator --
2004 -----------------------------
2006 -- Note that we first get the actual subtypes of the operands, since we
2007 -- always want to deal with types that have bounds.
2009 procedure Expand_Boolean_Operator
(N
: Node_Id
) is
2010 Typ
: constant Entity_Id
:= Etype
(N
);
2013 -- Special case of bit packed array where both operands are known to be
2014 -- properly aligned. In this case we use an efficient run time routine
2015 -- to carry out the operation (see System.Bit_Ops).
2017 if Is_Bit_Packed_Array
(Typ
)
2018 and then not Is_Possibly_Unaligned_Object
(Left_Opnd
(N
))
2019 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
2021 Expand_Packed_Boolean_Operator
(N
);
2025 -- For the normal non-packed case, the general expansion is to build
2026 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2027 -- and then inserting it into the tree. The original operator node is
2028 -- then rewritten as a call to this function. We also use this in the
2029 -- packed case if either operand is a possibly unaligned object.
2032 Loc
: constant Source_Ptr
:= Sloc
(N
);
2033 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
2034 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
2035 Func_Body
: Node_Id
;
2036 Func_Name
: Entity_Id
;
2039 Convert_To_Actual_Subtype
(L
);
2040 Convert_To_Actual_Subtype
(R
);
2041 Ensure_Defined
(Etype
(L
), N
);
2042 Ensure_Defined
(Etype
(R
), N
);
2043 Apply_Length_Check
(R
, Etype
(L
));
2045 if Nkind
(N
) = N_Op_Xor
then
2046 Silly_Boolean_Array_Xor_Test
(N
, Etype
(L
));
2049 if Nkind
(Parent
(N
)) = N_Assignment_Statement
2050 and then Safe_In_Place_Array_Op
(Name
(Parent
(N
)), L
, R
)
2052 Build_Boolean_Array_Proc_Call
(Parent
(N
), L
, R
);
2054 elsif Nkind
(Parent
(N
)) = N_Op_Not
2055 and then Nkind
(N
) = N_Op_And
2056 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
2057 and then Safe_In_Place_Array_Op
(Name
(Parent
(Parent
(N
))), L
, R
)
2062 Func_Body
:= Make_Boolean_Array_Op
(Etype
(L
), N
);
2063 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
2064 Insert_Action
(N
, Func_Body
);
2066 -- Now rewrite the expression with a call
2069 Make_Function_Call
(Loc
,
2070 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
2071 Parameter_Associations
=>
2074 Make_Type_Conversion
2075 (Loc
, New_Occurrence_Of
(Etype
(L
), Loc
), R
))));
2077 Analyze_And_Resolve
(N
, Typ
);
2080 end Expand_Boolean_Operator
;
2082 ------------------------------------------------
2083 -- Expand_Compare_Minimize_Eliminate_Overflow --
2084 ------------------------------------------------
2086 procedure Expand_Compare_Minimize_Eliminate_Overflow
(N
: Node_Id
) is
2087 Loc
: constant Source_Ptr
:= Sloc
(N
);
2089 Result_Type
: constant Entity_Id
:= Etype
(N
);
2090 -- Capture result type (could be a derived boolean type)
2095 LLIB
: constant Entity_Id
:= Base_Type
(Standard_Long_Long_Integer
);
2096 -- Entity for Long_Long_Integer'Base
2098 Check
: constant Overflow_Mode_Type
:= Overflow_Check_Mode
;
2099 -- Current overflow checking mode
2102 procedure Set_False
;
2103 -- These procedures rewrite N with an occurrence of Standard_True or
2104 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2110 procedure Set_False
is
2112 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
2113 Warn_On_Known_Condition
(N
);
2120 procedure Set_True
is
2122 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
2123 Warn_On_Known_Condition
(N
);
2126 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2129 -- Nothing to do unless we have a comparison operator with operands
2130 -- that are signed integer types, and we are operating in either
2131 -- MINIMIZED or ELIMINATED overflow checking mode.
2133 if Nkind
(N
) not in N_Op_Compare
2134 or else Check
not in Minimized_Or_Eliminated
2135 or else not Is_Signed_Integer_Type
(Etype
(Left_Opnd
(N
)))
2140 -- OK, this is the case we are interested in. First step is to process
2141 -- our operands using the Minimize_Eliminate circuitry which applies
2142 -- this processing to the two operand subtrees.
2144 Minimize_Eliminate_Overflows
2145 (Left_Opnd
(N
), Llo
, Lhi
, Top_Level
=> False);
2146 Minimize_Eliminate_Overflows
2147 (Right_Opnd
(N
), Rlo
, Rhi
, Top_Level
=> False);
2149 -- See if the range information decides the result of the comparison.
2150 -- We can only do this if we in fact have full range information (which
2151 -- won't be the case if either operand is bignum at this stage).
2153 if Llo
/= No_Uint
and then Rlo
/= No_Uint
then
2154 case N_Op_Compare
(Nkind
(N
)) is
2156 if Llo
= Lhi
and then Rlo
= Rhi
and then Llo
= Rlo
then
2158 elsif Llo
> Rhi
or else Lhi
< Rlo
then
2165 elsif Lhi
< Rlo
then
2172 elsif Lhi
<= Rlo
then
2179 elsif Lhi
<= Rlo
then
2186 elsif Lhi
< Rlo
then
2191 if Llo
= Lhi
and then Rlo
= Rhi
and then Llo
= Rlo
then
2193 elsif Llo
> Rhi
or else Lhi
< Rlo
then
2198 -- All done if we did the rewrite
2200 if Nkind
(N
) not in N_Op_Compare
then
2205 -- Otherwise, time to do the comparison
2208 Ltype
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
2209 Rtype
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
2212 -- If the two operands have the same signed integer type we are
2213 -- all set, nothing more to do. This is the case where either
2214 -- both operands were unchanged, or we rewrote both of them to
2215 -- be Long_Long_Integer.
2217 -- Note: Entity for the comparison may be wrong, but it's not worth
2218 -- the effort to change it, since the back end does not use it.
2220 if Is_Signed_Integer_Type
(Ltype
)
2221 and then Base_Type
(Ltype
) = Base_Type
(Rtype
)
2225 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2227 elsif Is_RTE
(Ltype
, RE_Bignum
) or else Is_RTE
(Rtype
, RE_Bignum
) then
2229 Left
: Node_Id
:= Left_Opnd
(N
);
2230 Right
: Node_Id
:= Right_Opnd
(N
);
2231 -- Bignum references for left and right operands
2234 if not Is_RTE
(Ltype
, RE_Bignum
) then
2235 Left
:= Convert_To_Bignum
(Left
);
2236 elsif not Is_RTE
(Rtype
, RE_Bignum
) then
2237 Right
:= Convert_To_Bignum
(Right
);
2240 -- We rewrite our node with:
2243 -- Bnn : Result_Type;
2245 -- M : Mark_Id := SS_Mark;
2247 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2255 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
2256 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
2260 case N_Op_Compare
(Nkind
(N
)) is
2261 when N_Op_Eq
=> Ent
:= RE_Big_EQ
;
2262 when N_Op_Ge
=> Ent
:= RE_Big_GE
;
2263 when N_Op_Gt
=> Ent
:= RE_Big_GT
;
2264 when N_Op_Le
=> Ent
:= RE_Big_LE
;
2265 when N_Op_Lt
=> Ent
:= RE_Big_LT
;
2266 when N_Op_Ne
=> Ent
:= RE_Big_NE
;
2269 -- Insert assignment to Bnn into the bignum block
2272 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
2273 Make_Assignment_Statement
(Loc
,
2274 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
2276 Make_Function_Call
(Loc
,
2278 New_Occurrence_Of
(RTE
(Ent
), Loc
),
2279 Parameter_Associations
=> New_List
(Left
, Right
))));
2281 -- Now do the rewrite with expression actions
2284 Make_Expression_With_Actions
(Loc
,
2285 Actions
=> New_List
(
2286 Make_Object_Declaration
(Loc
,
2287 Defining_Identifier
=> Bnn
,
2288 Object_Definition
=>
2289 New_Occurrence_Of
(Result_Type
, Loc
)),
2291 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
2292 Analyze_And_Resolve
(N
, Result_Type
);
2296 -- No bignums involved, but types are different, so we must have
2297 -- rewritten one of the operands as a Long_Long_Integer but not
2300 -- If left operand is Long_Long_Integer, convert right operand
2301 -- and we are done (with a comparison of two Long_Long_Integers).
2303 elsif Ltype
= LLIB
then
2304 Convert_To_And_Rewrite
(LLIB
, Right_Opnd
(N
));
2305 Analyze_And_Resolve
(Right_Opnd
(N
), LLIB
, Suppress
=> All_Checks
);
2308 -- If right operand is Long_Long_Integer, convert left operand
2309 -- and we are done (with a comparison of two Long_Long_Integers).
2311 -- This is the only remaining possibility
2313 else pragma Assert
(Rtype
= LLIB
);
2314 Convert_To_And_Rewrite
(LLIB
, Left_Opnd
(N
));
2315 Analyze_And_Resolve
(Left_Opnd
(N
), LLIB
, Suppress
=> All_Checks
);
2319 end Expand_Compare_Minimize_Eliminate_Overflow
;
2321 -------------------------------
2322 -- Expand_Composite_Equality --
2323 -------------------------------
2325 -- This function is only called for comparing internal fields of composite
2326 -- types when these fields are themselves composites. This is a special
2327 -- case because it is not possible to respect normal Ada visibility rules.
2329 function Expand_Composite_Equality
2334 Bodies
: List_Id
) return Node_Id
2336 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
2337 Full_Type
: Entity_Id
;
2340 function Find_Primitive_Eq
return Node_Id
;
2341 -- AI05-0123: Locate primitive equality for type if it exists, and
2342 -- build the corresponding call. If operation is abstract, replace
2343 -- call with an explicit raise. Return Empty if there is no primitive.
2345 -----------------------
2346 -- Find_Primitive_Eq --
2347 -----------------------
2349 function Find_Primitive_Eq
return Node_Id
is
2354 Prim_E
:= First_Elmt
(Collect_Primitive_Operations
(Typ
));
2355 while Present
(Prim_E
) loop
2356 Prim
:= Node
(Prim_E
);
2358 -- Locate primitive equality with the right signature
2360 if Chars
(Prim
) = Name_Op_Eq
2361 and then Etype
(First_Formal
(Prim
)) =
2362 Etype
(Next_Formal
(First_Formal
(Prim
)))
2363 and then Etype
(Prim
) = Standard_Boolean
2365 if Is_Abstract_Subprogram
(Prim
) then
2367 Make_Raise_Program_Error
(Loc
,
2368 Reason
=> PE_Explicit_Raise
);
2372 Make_Function_Call
(Loc
,
2373 Name
=> New_Occurrence_Of
(Prim
, Loc
),
2374 Parameter_Associations
=> New_List
(Lhs
, Rhs
));
2381 -- If not found, predefined operation will be used
2384 end Find_Primitive_Eq
;
2386 -- Start of processing for Expand_Composite_Equality
2389 if Is_Private_Type
(Typ
) then
2390 Full_Type
:= Underlying_Type
(Typ
);
2395 -- If the private type has no completion the context may be the
2396 -- expansion of a composite equality for a composite type with some
2397 -- still incomplete components. The expression will not be analyzed
2398 -- until the enclosing type is completed, at which point this will be
2399 -- properly expanded, unless there is a bona fide completion error.
2401 if No
(Full_Type
) then
2402 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2405 Full_Type
:= Base_Type
(Full_Type
);
2407 -- When the base type itself is private, use the full view to expand
2408 -- the composite equality.
2410 if Is_Private_Type
(Full_Type
) then
2411 Full_Type
:= Underlying_Type
(Full_Type
);
2414 -- Case of array types
2416 if Is_Array_Type
(Full_Type
) then
2418 -- If the operand is an elementary type other than a floating-point
2419 -- type, then we can simply use the built-in block bitwise equality,
2420 -- since the predefined equality operators always apply and bitwise
2421 -- equality is fine for all these cases.
2423 if Is_Elementary_Type
(Component_Type
(Full_Type
))
2424 and then not Is_Floating_Point_Type
(Component_Type
(Full_Type
))
2426 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2428 -- For composite component types, and floating-point types, use the
2429 -- expansion. This deals with tagged component types (where we use
2430 -- the applicable equality routine) and floating-point (where we
2431 -- need to worry about negative zeroes), and also the case of any
2432 -- composite type recursively containing such fields.
2436 Comp_Typ
: Entity_Id
;
2443 -- Do the comparison in the type (or its full view) and not in
2444 -- its unconstrained base type, because the latter operation is
2445 -- more complex and would also require an unchecked conversion.
2447 if Is_Private_Type
(Typ
) then
2448 Comp_Typ
:= Underlying_Type
(Typ
);
2453 -- Except for the case where the bounds of the type depend on a
2454 -- discriminant, or else we would run into scoping issues.
2456 Indx
:= First_Index
(Comp_Typ
);
2457 while Present
(Indx
) loop
2458 Ityp
:= Etype
(Indx
);
2460 Lo
:= Type_Low_Bound
(Ityp
);
2461 Hi
:= Type_High_Bound
(Ityp
);
2463 if (Nkind
(Lo
) = N_Identifier
2464 and then Ekind
(Entity
(Lo
)) = E_Discriminant
)
2466 (Nkind
(Hi
) = N_Identifier
2467 and then Ekind
(Entity
(Hi
)) = E_Discriminant
)
2469 Comp_Typ
:= Full_Type
;
2476 return Expand_Array_Equality
(Nod
, Lhs
, Rhs
, Bodies
, Comp_Typ
);
2480 -- Case of tagged record types
2482 elsif Is_Tagged_Type
(Full_Type
) then
2483 Eq_Op
:= Find_Primitive_Eq
(Typ
);
2484 pragma Assert
(Present
(Eq_Op
));
2487 Make_Function_Call
(Loc
,
2488 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2489 Parameter_Associations
=>
2491 (Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Lhs
),
2492 Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Rhs
)));
2494 -- Case of untagged record types
2496 elsif Is_Record_Type
(Full_Type
) then
2497 Eq_Op
:= TSS
(Full_Type
, TSS_Composite_Equality
);
2499 if Present
(Eq_Op
) then
2500 if Etype
(First_Formal
(Eq_Op
)) /= Full_Type
then
2502 -- Inherited equality from parent type. Convert the actuals to
2503 -- match signature of operation.
2506 T
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
2510 Make_Function_Call
(Loc
,
2511 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2512 Parameter_Associations
=> New_List
(
2513 OK_Convert_To
(T
, Lhs
),
2514 OK_Convert_To
(T
, Rhs
)));
2518 -- Comparison between Unchecked_Union components
2520 if Is_Unchecked_Union
(Full_Type
) then
2522 Lhs_Type
: Node_Id
:= Full_Type
;
2523 Rhs_Type
: Node_Id
:= Full_Type
;
2524 Lhs_Discr_Val
: Node_Id
;
2525 Rhs_Discr_Val
: Node_Id
;
2530 if Nkind
(Lhs
) = N_Selected_Component
then
2531 Lhs_Type
:= Etype
(Entity
(Selector_Name
(Lhs
)));
2536 if Nkind
(Rhs
) = N_Selected_Component
then
2537 Rhs_Type
:= Etype
(Entity
(Selector_Name
(Rhs
)));
2540 -- Lhs of the composite equality
2542 if Is_Constrained
(Lhs_Type
) then
2544 -- Since the enclosing record type can never be an
2545 -- Unchecked_Union (this code is executed for records
2546 -- that do not have variants), we may reference its
2549 if Nkind
(Lhs
) = N_Selected_Component
2550 and then Has_Per_Object_Constraint
2551 (Entity
(Selector_Name
(Lhs
)))
2554 Make_Selected_Component
(Loc
,
2555 Prefix
=> Prefix
(Lhs
),
2558 (Get_Discriminant_Value
2559 (First_Discriminant
(Lhs_Type
),
2561 Stored_Constraint
(Lhs_Type
))));
2566 (Get_Discriminant_Value
2567 (First_Discriminant
(Lhs_Type
),
2569 Stored_Constraint
(Lhs_Type
)));
2573 -- It is not possible to infer the discriminant since
2574 -- the subtype is not constrained.
2577 Make_Raise_Program_Error
(Loc
,
2578 Reason
=> PE_Unchecked_Union_Restriction
);
2581 -- Rhs of the composite equality
2583 if Is_Constrained
(Rhs_Type
) then
2584 if Nkind
(Rhs
) = N_Selected_Component
2585 and then Has_Per_Object_Constraint
2586 (Entity
(Selector_Name
(Rhs
)))
2589 Make_Selected_Component
(Loc
,
2590 Prefix
=> Prefix
(Rhs
),
2593 (Get_Discriminant_Value
2594 (First_Discriminant
(Rhs_Type
),
2596 Stored_Constraint
(Rhs_Type
))));
2601 (Get_Discriminant_Value
2602 (First_Discriminant
(Rhs_Type
),
2604 Stored_Constraint
(Rhs_Type
)));
2609 Make_Raise_Program_Error
(Loc
,
2610 Reason
=> PE_Unchecked_Union_Restriction
);
2613 -- Call the TSS equality function with the inferred
2614 -- discriminant values.
2617 Make_Function_Call
(Loc
,
2618 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2619 Parameter_Associations
=> New_List
(
2626 -- All cases other than comparing Unchecked_Union types
2630 T
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
2633 Make_Function_Call
(Loc
,
2635 New_Occurrence_Of
(Eq_Op
, Loc
),
2636 Parameter_Associations
=> New_List
(
2637 OK_Convert_To
(T
, Lhs
),
2638 OK_Convert_To
(T
, Rhs
)));
2643 -- Equality composes in Ada 2012 for untagged record types. It also
2644 -- composes for bounded strings, because they are part of the
2645 -- predefined environment. We could make it compose for bounded
2646 -- strings by making them tagged, or by making sure all subcomponents
2647 -- are set to the same value, even when not used. Instead, we have
2648 -- this special case in the compiler, because it's more efficient.
2650 elsif Ada_Version
>= Ada_2012
or else Is_Bounded_String
(Typ
) then
2652 -- If no TSS has been created for the type, check whether there is
2653 -- a primitive equality declared for it.
2656 Op
: constant Node_Id
:= Find_Primitive_Eq
;
2659 -- Use user-defined primitive if it exists, otherwise use
2660 -- predefined equality.
2662 if Present
(Op
) then
2665 return Make_Op_Eq
(Loc
, Lhs
, Rhs
);
2670 return Expand_Record_Equality
(Nod
, Full_Type
, Lhs
, Rhs
, Bodies
);
2673 -- Non-composite types (always use predefined equality)
2676 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2678 end Expand_Composite_Equality
;
2680 ------------------------
2681 -- Expand_Concatenate --
2682 ------------------------
2684 procedure Expand_Concatenate
(Cnode
: Node_Id
; Opnds
: List_Id
) is
2685 Loc
: constant Source_Ptr
:= Sloc
(Cnode
);
2687 Atyp
: constant Entity_Id
:= Base_Type
(Etype
(Cnode
));
2688 -- Result type of concatenation
2690 Ctyp
: constant Entity_Id
:= Base_Type
(Component_Type
(Etype
(Cnode
)));
2691 -- Component type. Elements of this component type can appear as one
2692 -- of the operands of concatenation as well as arrays.
2694 Istyp
: constant Entity_Id
:= Etype
(First_Index
(Atyp
));
2697 Ityp
: constant Entity_Id
:= Base_Type
(Istyp
);
2698 -- Index type. This is the base type of the index subtype, and is used
2699 -- for all computed bounds (which may be out of range of Istyp in the
2700 -- case of null ranges).
2703 -- This is the type we use to do arithmetic to compute the bounds and
2704 -- lengths of operands. The choice of this type is a little subtle and
2705 -- is discussed in a separate section at the start of the body code.
2707 Concatenation_Error
: exception;
2708 -- Raised if concatenation is sure to raise a CE
2710 Result_May_Be_Null
: Boolean := True;
2711 -- Reset to False if at least one operand is encountered which is known
2712 -- at compile time to be non-null. Used for handling the special case
2713 -- of setting the high bound to the last operand high bound for a null
2714 -- result, thus ensuring a proper high bound in the super-flat case.
2716 N
: constant Nat
:= List_Length
(Opnds
);
2717 -- Number of concatenation operands including possibly null operands
2720 -- Number of operands excluding any known to be null, except that the
2721 -- last operand is always retained, in case it provides the bounds for
2724 Opnd
: Node_Id
:= Empty
;
2725 -- Current operand being processed in the loop through operands. After
2726 -- this loop is complete, always contains the last operand (which is not
2727 -- the same as Operands (NN), since null operands are skipped).
2729 -- Arrays describing the operands, only the first NN entries of each
2730 -- array are set (NN < N when we exclude known null operands).
2732 Is_Fixed_Length
: array (1 .. N
) of Boolean;
2733 -- True if length of corresponding operand known at compile time
2735 Operands
: array (1 .. N
) of Node_Id
;
2736 -- Set to the corresponding entry in the Opnds list (but note that null
2737 -- operands are excluded, so not all entries in the list are stored).
2739 Fixed_Length
: array (1 .. N
) of Uint
;
2740 -- Set to length of operand. Entries in this array are set only if the
2741 -- corresponding entry in Is_Fixed_Length is True.
2743 Opnd_Low_Bound
: array (1 .. N
) of Node_Id
;
2744 -- Set to lower bound of operand. Either an integer literal in the case
2745 -- where the bound is known at compile time, else actual lower bound.
2746 -- The operand low bound is of type Ityp.
2748 Var_Length
: array (1 .. N
) of Entity_Id
;
2749 -- Set to an entity of type Natural that contains the length of an
2750 -- operand whose length is not known at compile time. Entries in this
2751 -- array are set only if the corresponding entry in Is_Fixed_Length
2752 -- is False. The entity is of type Artyp.
2754 Aggr_Length
: array (0 .. N
) of Node_Id
;
2755 -- The J'th entry in an expression node that represents the total length
2756 -- of operands 1 through J. It is either an integer literal node, or a
2757 -- reference to a constant entity with the right value, so it is fine
2758 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2759 -- entry always is set to zero. The length is of type Artyp.
2761 Low_Bound
: Node_Id
;
2762 -- A tree node representing the low bound of the result (of type Ityp).
2763 -- This is either an integer literal node, or an identifier reference to
2764 -- a constant entity initialized to the appropriate value.
2766 Last_Opnd_Low_Bound
: Node_Id
:= Empty
;
2767 -- A tree node representing the low bound of the last operand. This
2768 -- need only be set if the result could be null. It is used for the
2769 -- special case of setting the right low bound for a null result.
2770 -- This is of type Ityp.
2772 Last_Opnd_High_Bound
: Node_Id
:= Empty
;
2773 -- A tree node representing the high bound of the last operand. This
2774 -- need only be set if the result could be null. It is used for the
2775 -- special case of setting the right high bound for a null result.
2776 -- This is of type Ityp.
2778 High_Bound
: Node_Id
:= Empty
;
2779 -- A tree node representing the high bound of the result (of type Ityp)
2782 -- Result of the concatenation (of type Ityp)
2784 Actions
: constant List_Id
:= New_List
;
2785 -- Collect actions to be inserted
2787 Known_Non_Null_Operand_Seen
: Boolean;
2788 -- Set True during generation of the assignments of operands into
2789 -- result once an operand known to be non-null has been seen.
2791 function Library_Level_Target
return Boolean;
2792 -- Return True if the concatenation is within the expression of the
2793 -- declaration of a library-level object.
2795 function Make_Artyp_Literal
(Val
: Nat
) return Node_Id
;
2796 -- This function makes an N_Integer_Literal node that is returned in
2797 -- analyzed form with the type set to Artyp. Importantly this literal
2798 -- is not flagged as static, so that if we do computations with it that
2799 -- result in statically detected out of range conditions, we will not
2800 -- generate error messages but instead warning messages.
2802 function To_Artyp
(X
: Node_Id
) return Node_Id
;
2803 -- Given a node of type Ityp, returns the corresponding value of type
2804 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2805 -- For enum types, the Pos of the value is returned.
2807 function To_Ityp
(X
: Node_Id
) return Node_Id
;
2808 -- The inverse function (uses Val in the case of enumeration types)
2810 --------------------------
2811 -- Library_Level_Target --
2812 --------------------------
2814 function Library_Level_Target
return Boolean is
2815 P
: Node_Id
:= Parent
(Cnode
);
2818 while Present
(P
) loop
2819 if Nkind
(P
) = N_Object_Declaration
then
2820 return Is_Library_Level_Entity
(Defining_Identifier
(P
));
2822 -- Prevent the search from going too far
2824 elsif Is_Body_Or_Package_Declaration
(P
) then
2832 end Library_Level_Target
;
2834 ------------------------
2835 -- Make_Artyp_Literal --
2836 ------------------------
2838 function Make_Artyp_Literal
(Val
: Nat
) return Node_Id
is
2839 Result
: constant Node_Id
:= Make_Integer_Literal
(Loc
, Val
);
2841 Set_Etype
(Result
, Artyp
);
2842 Set_Analyzed
(Result
, True);
2843 Set_Is_Static_Expression
(Result
, False);
2845 end Make_Artyp_Literal
;
2851 function To_Artyp
(X
: Node_Id
) return Node_Id
is
2853 if Ityp
= Base_Type
(Artyp
) then
2856 elsif Is_Enumeration_Type
(Ityp
) then
2858 Make_Attribute_Reference
(Loc
,
2859 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
2860 Attribute_Name
=> Name_Pos
,
2861 Expressions
=> New_List
(X
));
2864 return Convert_To
(Artyp
, X
);
2872 function To_Ityp
(X
: Node_Id
) return Node_Id
is
2874 if Is_Enumeration_Type
(Ityp
) then
2876 Make_Attribute_Reference
(Loc
,
2877 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
2878 Attribute_Name
=> Name_Val
,
2879 Expressions
=> New_List
(X
));
2881 -- Case where we will do a type conversion
2884 if Ityp
= Base_Type
(Artyp
) then
2887 return Convert_To
(Ityp
, X
);
2892 -- Local Declarations
2894 Opnd_Typ
: Entity_Id
;
2901 -- Start of processing for Expand_Concatenate
2904 -- Choose an appropriate computational type
2906 -- We will be doing calculations of lengths and bounds in this routine
2907 -- and computing one from the other in some cases, e.g. getting the high
2908 -- bound by adding the length-1 to the low bound.
2910 -- We can't just use the index type, or even its base type for this
2911 -- purpose for two reasons. First it might be an enumeration type which
2912 -- is not suitable for computations of any kind, and second it may
2913 -- simply not have enough range. For example if the index type is
2914 -- -128..+127 then lengths can be up to 256, which is out of range of
2917 -- For enumeration types, we can simply use Standard_Integer, this is
2918 -- sufficient since the actual number of enumeration literals cannot
2919 -- possibly exceed the range of integer (remember we will be doing the
2920 -- arithmetic with POS values, not representation values).
2922 if Is_Enumeration_Type
(Ityp
) then
2923 Artyp
:= Standard_Integer
;
2925 -- If index type is Positive, we use the standard unsigned type, to give
2926 -- more room on the top of the range, obviating the need for an overflow
2927 -- check when creating the upper bound. This is needed to avoid junk
2928 -- overflow checks in the common case of String types.
2930 -- ??? Disabled for now
2932 -- elsif Istyp = Standard_Positive then
2933 -- Artyp := Standard_Unsigned;
2935 -- For modular types, we use a 32-bit modular type for types whose size
2936 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2937 -- identity type, and for larger unsigned types we use 64-bits.
2939 elsif Is_Modular_Integer_Type
(Ityp
) then
2940 if RM_Size
(Ityp
) < RM_Size
(Standard_Unsigned
) then
2941 Artyp
:= Standard_Unsigned
;
2942 elsif RM_Size
(Ityp
) = RM_Size
(Standard_Unsigned
) then
2945 Artyp
:= RTE
(RE_Long_Long_Unsigned
);
2948 -- Similar treatment for signed types
2951 if RM_Size
(Ityp
) < RM_Size
(Standard_Integer
) then
2952 Artyp
:= Standard_Integer
;
2953 elsif RM_Size
(Ityp
) = RM_Size
(Standard_Integer
) then
2956 Artyp
:= Standard_Long_Long_Integer
;
2960 -- Supply dummy entry at start of length array
2962 Aggr_Length
(0) := Make_Artyp_Literal
(0);
2964 -- Go through operands setting up the above arrays
2968 Opnd
:= Remove_Head
(Opnds
);
2969 Opnd_Typ
:= Etype
(Opnd
);
2971 -- The parent got messed up when we put the operands in a list,
2972 -- so now put back the proper parent for the saved operand, that
2973 -- is to say the concatenation node, to make sure that each operand
2974 -- is seen as a subexpression, e.g. if actions must be inserted.
2976 Set_Parent
(Opnd
, Cnode
);
2978 -- Set will be True when we have setup one entry in the array
2982 -- Singleton element (or character literal) case
2984 if Base_Type
(Opnd_Typ
) = Ctyp
then
2986 Operands
(NN
) := Opnd
;
2987 Is_Fixed_Length
(NN
) := True;
2988 Fixed_Length
(NN
) := Uint_1
;
2989 Result_May_Be_Null
:= False;
2991 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2992 -- since we know that the result cannot be null).
2994 Opnd_Low_Bound
(NN
) :=
2995 Make_Attribute_Reference
(Loc
,
2996 Prefix
=> New_Occurrence_Of
(Istyp
, Loc
),
2997 Attribute_Name
=> Name_First
);
3001 -- String literal case (can only occur for strings of course)
3003 elsif Nkind
(Opnd
) = N_String_Literal
then
3004 Len
:= String_Literal_Length
(Opnd_Typ
);
3007 Result_May_Be_Null
:= False;
3010 -- Capture last operand low and high bound if result could be null
3012 if J
= N
and then Result_May_Be_Null
then
3013 Last_Opnd_Low_Bound
:=
3014 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
));
3016 Last_Opnd_High_Bound
:=
3017 Make_Op_Subtract
(Loc
,
3019 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
)),
3020 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
3023 -- Skip null string literal
3025 if J
< N
and then Len
= 0 then
3030 Operands
(NN
) := Opnd
;
3031 Is_Fixed_Length
(NN
) := True;
3033 -- Set length and bounds
3035 Fixed_Length
(NN
) := Len
;
3037 Opnd_Low_Bound
(NN
) :=
3038 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
));
3045 -- Check constrained case with known bounds
3047 if Is_Constrained
(Opnd_Typ
) then
3049 Index
: constant Node_Id
:= First_Index
(Opnd_Typ
);
3050 Indx_Typ
: constant Entity_Id
:= Etype
(Index
);
3051 Lo
: constant Node_Id
:= Type_Low_Bound
(Indx_Typ
);
3052 Hi
: constant Node_Id
:= Type_High_Bound
(Indx_Typ
);
3055 -- Fixed length constrained array type with known at compile
3056 -- time bounds is last case of fixed length operand.
3058 if Compile_Time_Known_Value
(Lo
)
3060 Compile_Time_Known_Value
(Hi
)
3063 Loval
: constant Uint
:= Expr_Value
(Lo
);
3064 Hival
: constant Uint
:= Expr_Value
(Hi
);
3065 Len
: constant Uint
:=
3066 UI_Max
(Hival
- Loval
+ 1, Uint_0
);
3070 Result_May_Be_Null
:= False;
3073 -- Capture last operand bounds if result could be null
3075 if J
= N
and then Result_May_Be_Null
then
3076 Last_Opnd_Low_Bound
:=
3078 Make_Integer_Literal
(Loc
, Expr_Value
(Lo
)));
3080 Last_Opnd_High_Bound
:=
3082 Make_Integer_Literal
(Loc
, Expr_Value
(Hi
)));
3085 -- Exclude null length case unless last operand
3087 if J
< N
and then Len
= 0 then
3092 Operands
(NN
) := Opnd
;
3093 Is_Fixed_Length
(NN
) := True;
3094 Fixed_Length
(NN
) := Len
;
3096 Opnd_Low_Bound
(NN
) :=
3098 (Make_Integer_Literal
(Loc
, Expr_Value
(Lo
)));
3105 -- All cases where the length is not known at compile time, or the
3106 -- special case of an operand which is known to be null but has a
3107 -- lower bound other than 1 or is other than a string type.
3112 -- Capture operand bounds
3114 Opnd_Low_Bound
(NN
) :=
3115 Make_Attribute_Reference
(Loc
,
3117 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3118 Attribute_Name
=> Name_First
);
3120 -- Capture last operand bounds if result could be null
3122 if J
= N
and Result_May_Be_Null
then
3123 Last_Opnd_Low_Bound
:=
3125 Make_Attribute_Reference
(Loc
,
3127 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3128 Attribute_Name
=> Name_First
));
3130 Last_Opnd_High_Bound
:=
3132 Make_Attribute_Reference
(Loc
,
3134 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3135 Attribute_Name
=> Name_Last
));
3138 -- Capture length of operand in entity
3140 Operands
(NN
) := Opnd
;
3141 Is_Fixed_Length
(NN
) := False;
3143 Var_Length
(NN
) := Make_Temporary
(Loc
, 'L');
3146 Make_Object_Declaration
(Loc
,
3147 Defining_Identifier
=> Var_Length
(NN
),
3148 Constant_Present
=> True,
3149 Object_Definition
=> New_Occurrence_Of
(Artyp
, Loc
),
3151 Make_Attribute_Reference
(Loc
,
3153 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3154 Attribute_Name
=> Name_Length
)));
3158 -- Set next entry in aggregate length array
3160 -- For first entry, make either integer literal for fixed length
3161 -- or a reference to the saved length for variable length.
3164 if Is_Fixed_Length
(1) then
3165 Aggr_Length
(1) := Make_Integer_Literal
(Loc
, Fixed_Length
(1));
3167 Aggr_Length
(1) := New_Occurrence_Of
(Var_Length
(1), Loc
);
3170 -- If entry is fixed length and only fixed lengths so far, make
3171 -- appropriate new integer literal adding new length.
3173 elsif Is_Fixed_Length
(NN
)
3174 and then Nkind
(Aggr_Length
(NN
- 1)) = N_Integer_Literal
3177 Make_Integer_Literal
(Loc
,
3178 Intval
=> Fixed_Length
(NN
) + Intval
(Aggr_Length
(NN
- 1)));
3180 -- All other cases, construct an addition node for the length and
3181 -- create an entity initialized to this length.
3184 Ent
:= Make_Temporary
(Loc
, 'L');
3186 if Is_Fixed_Length
(NN
) then
3187 Clen
:= Make_Integer_Literal
(Loc
, Fixed_Length
(NN
));
3189 Clen
:= New_Occurrence_Of
(Var_Length
(NN
), Loc
);
3193 Make_Object_Declaration
(Loc
,
3194 Defining_Identifier
=> Ent
,
3195 Constant_Present
=> True,
3196 Object_Definition
=> New_Occurrence_Of
(Artyp
, Loc
),
3199 Left_Opnd
=> New_Copy_Tree
(Aggr_Length
(NN
- 1)),
3200 Right_Opnd
=> Clen
)));
3202 Aggr_Length
(NN
) := Make_Identifier
(Loc
, Chars
=> Chars
(Ent
));
3209 -- If we have only skipped null operands, return the last operand
3216 -- If we have only one non-null operand, return it and we are done.
3217 -- There is one case in which this cannot be done, and that is when
3218 -- the sole operand is of the element type, in which case it must be
3219 -- converted to an array, and the easiest way of doing that is to go
3220 -- through the normal general circuit.
3222 if NN
= 1 and then Base_Type
(Etype
(Operands
(1))) /= Ctyp
then
3223 Result
:= Operands
(1);
3227 -- Cases where we have a real concatenation
3229 -- Next step is to find the low bound for the result array that we
3230 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3232 -- If the ultimate ancestor of the index subtype is a constrained array
3233 -- definition, then the lower bound is that of the index subtype as
3234 -- specified by (RM 4.5.3(6)).
3236 -- The right test here is to go to the root type, and then the ultimate
3237 -- ancestor is the first subtype of this root type.
3239 if Is_Constrained
(First_Subtype
(Root_Type
(Atyp
))) then
3241 Make_Attribute_Reference
(Loc
,
3243 New_Occurrence_Of
(First_Subtype
(Root_Type
(Atyp
)), Loc
),
3244 Attribute_Name
=> Name_First
);
3246 -- If the first operand in the list has known length we know that
3247 -- the lower bound of the result is the lower bound of this operand.
3249 elsif Is_Fixed_Length
(1) then
3250 Low_Bound
:= Opnd_Low_Bound
(1);
3252 -- OK, we don't know the lower bound, we have to build a horrible
3253 -- if expression node of the form
3255 -- if Cond1'Length /= 0 then
3258 -- if Opnd2'Length /= 0 then
3263 -- The nesting ends either when we hit an operand whose length is known
3264 -- at compile time, or on reaching the last operand, whose low bound we
3265 -- take unconditionally whether or not it is null. It's easiest to do
3266 -- this with a recursive procedure:
3270 function Get_Known_Bound
(J
: Nat
) return Node_Id
;
3271 -- Returns the lower bound determined by operands J .. NN
3273 ---------------------
3274 -- Get_Known_Bound --
3275 ---------------------
3277 function Get_Known_Bound
(J
: Nat
) return Node_Id
is
3279 if Is_Fixed_Length
(J
) or else J
= NN
then
3280 return New_Copy_Tree
(Opnd_Low_Bound
(J
));
3284 Make_If_Expression
(Loc
,
3285 Expressions
=> New_List
(
3289 New_Occurrence_Of
(Var_Length
(J
), Loc
),
3291 Make_Integer_Literal
(Loc
, 0)),
3293 New_Copy_Tree
(Opnd_Low_Bound
(J
)),
3294 Get_Known_Bound
(J
+ 1)));
3296 end Get_Known_Bound
;
3299 Ent
:= Make_Temporary
(Loc
, 'L');
3302 Make_Object_Declaration
(Loc
,
3303 Defining_Identifier
=> Ent
,
3304 Constant_Present
=> True,
3305 Object_Definition
=> New_Occurrence_Of
(Ityp
, Loc
),
3306 Expression
=> Get_Known_Bound
(1)));
3308 Low_Bound
:= New_Occurrence_Of
(Ent
, Loc
);
3312 -- Now we can safely compute the upper bound, normally
3313 -- Low_Bound + Length - 1.
3318 Left_Opnd
=> To_Artyp
(New_Copy_Tree
(Low_Bound
)),
3320 Make_Op_Subtract
(Loc
,
3321 Left_Opnd
=> New_Copy_Tree
(Aggr_Length
(NN
)),
3322 Right_Opnd
=> Make_Artyp_Literal
(1))));
3324 -- Note that calculation of the high bound may cause overflow in some
3325 -- very weird cases, so in the general case we need an overflow check on
3326 -- the high bound. We can avoid this for the common case of string types
3327 -- and other types whose index is Positive, since we chose a wider range
3328 -- for the arithmetic type. If checks are suppressed we do not set the
3329 -- flag, and possibly superfluous warnings will be omitted.
3331 if Istyp
/= Standard_Positive
3332 and then not Overflow_Checks_Suppressed
(Istyp
)
3334 Activate_Overflow_Check
(High_Bound
);
3337 -- Handle the exceptional case where the result is null, in which case
3338 -- case the bounds come from the last operand (so that we get the proper
3339 -- bounds if the last operand is super-flat).
3341 if Result_May_Be_Null
then
3343 Make_If_Expression
(Loc
,
3344 Expressions
=> New_List
(
3346 Left_Opnd
=> New_Copy_Tree
(Aggr_Length
(NN
)),
3347 Right_Opnd
=> Make_Artyp_Literal
(0)),
3348 Last_Opnd_Low_Bound
,
3352 Make_If_Expression
(Loc
,
3353 Expressions
=> New_List
(
3355 Left_Opnd
=> New_Copy_Tree
(Aggr_Length
(NN
)),
3356 Right_Opnd
=> Make_Artyp_Literal
(0)),
3357 Last_Opnd_High_Bound
,
3361 -- Here is where we insert the saved up actions
3363 Insert_Actions
(Cnode
, Actions
, Suppress
=> All_Checks
);
3365 -- Now we construct an array object with appropriate bounds. We mark
3366 -- the target as internal to prevent useless initialization when
3367 -- Initialize_Scalars is enabled. Also since this is the actual result
3368 -- entity, we make sure we have debug information for the result.
3370 Ent
:= Make_Temporary
(Loc
, 'S');
3371 Set_Is_Internal
(Ent
);
3372 Set_Needs_Debug_Info
(Ent
);
3374 -- If the bound is statically known to be out of range, we do not want
3375 -- to abort, we want a warning and a runtime constraint error. Note that
3376 -- we have arranged that the result will not be treated as a static
3377 -- constant, so we won't get an illegality during this insertion.
3379 Insert_Action
(Cnode
,
3380 Make_Object_Declaration
(Loc
,
3381 Defining_Identifier
=> Ent
,
3382 Object_Definition
=>
3383 Make_Subtype_Indication
(Loc
,
3384 Subtype_Mark
=> New_Occurrence_Of
(Atyp
, Loc
),
3386 Make_Index_Or_Discriminant_Constraint
(Loc
,
3387 Constraints
=> New_List
(
3389 Low_Bound
=> Low_Bound
,
3390 High_Bound
=> High_Bound
))))),
3391 Suppress
=> All_Checks
);
3393 -- If the result of the concatenation appears as the initializing
3394 -- expression of an object declaration, we can just rename the
3395 -- result, rather than copying it.
3397 Set_OK_To_Rename
(Ent
);
3399 -- Catch the static out of range case now
3401 if Raises_Constraint_Error
(High_Bound
) then
3402 raise Concatenation_Error
;
3405 -- Now we will generate the assignments to do the actual concatenation
3407 -- There is one case in which we will not do this, namely when all the
3408 -- following conditions are met:
3410 -- The result type is Standard.String
3412 -- There are nine or fewer retained (non-null) operands
3414 -- The optimization level is -O0 or the debug flag gnatd.C is set,
3415 -- and the debug flag gnatd.c is not set.
3417 -- The corresponding System.Concat_n.Str_Concat_n routine is
3418 -- available in the run time.
3420 -- If all these conditions are met then we generate a call to the
3421 -- relevant concatenation routine. The purpose of this is to avoid
3422 -- undesirable code bloat at -O0.
3424 -- If the concatenation is within the declaration of a library-level
3425 -- object, we call the built-in concatenation routines to prevent code
3426 -- bloat, regardless of the optimization level. This is space efficient
3427 -- and prevents linking problems when units are compiled with different
3428 -- optimization levels.
3430 if Atyp
= Standard_String
3431 and then NN
in 2 .. 9
3432 and then (((Optimization_Level
= 0 or else Debug_Flag_Dot_CC
)
3433 and then not Debug_Flag_Dot_C
)
3434 or else Library_Level_Target
)
3437 RR
: constant array (Nat
range 2 .. 9) of RE_Id
:=
3448 if RTE_Available
(RR
(NN
)) then
3450 Opnds
: constant List_Id
:=
3451 New_List
(New_Occurrence_Of
(Ent
, Loc
));
3454 for J
in 1 .. NN
loop
3455 if Is_List_Member
(Operands
(J
)) then
3456 Remove
(Operands
(J
));
3459 if Base_Type
(Etype
(Operands
(J
))) = Ctyp
then
3461 Make_Aggregate
(Loc
,
3462 Component_Associations
=> New_List
(
3463 Make_Component_Association
(Loc
,
3464 Choices
=> New_List
(
3465 Make_Integer_Literal
(Loc
, 1)),
3466 Expression
=> Operands
(J
)))));
3469 Append_To
(Opnds
, Operands
(J
));
3473 Insert_Action
(Cnode
,
3474 Make_Procedure_Call_Statement
(Loc
,
3475 Name
=> New_Occurrence_Of
(RTE
(RR
(NN
)), Loc
),
3476 Parameter_Associations
=> Opnds
));
3478 Result
:= New_Occurrence_Of
(Ent
, Loc
);
3485 -- Not special case so generate the assignments
3487 Known_Non_Null_Operand_Seen
:= False;
3489 for J
in 1 .. NN
loop
3491 Lo
: constant Node_Id
:=
3493 Left_Opnd
=> To_Artyp
(New_Copy_Tree
(Low_Bound
)),
3494 Right_Opnd
=> Aggr_Length
(J
- 1));
3496 Hi
: constant Node_Id
:=
3498 Left_Opnd
=> To_Artyp
(New_Copy_Tree
(Low_Bound
)),
3500 Make_Op_Subtract
(Loc
,
3501 Left_Opnd
=> Aggr_Length
(J
),
3502 Right_Opnd
=> Make_Artyp_Literal
(1)));
3505 -- Singleton case, simple assignment
3507 if Base_Type
(Etype
(Operands
(J
))) = Ctyp
then
3508 Known_Non_Null_Operand_Seen
:= True;
3509 Insert_Action
(Cnode
,
3510 Make_Assignment_Statement
(Loc
,
3512 Make_Indexed_Component
(Loc
,
3513 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
3514 Expressions
=> New_List
(To_Ityp
(Lo
))),
3515 Expression
=> Operands
(J
)),
3516 Suppress
=> All_Checks
);
3518 -- Array case, slice assignment, skipped when argument is fixed
3519 -- length and known to be null.
3521 elsif (not Is_Fixed_Length
(J
)) or else (Fixed_Length
(J
) > 0) then
3524 Make_Assignment_Statement
(Loc
,
3528 New_Occurrence_Of
(Ent
, Loc
),
3531 Low_Bound
=> To_Ityp
(Lo
),
3532 High_Bound
=> To_Ityp
(Hi
))),
3533 Expression
=> Operands
(J
));
3535 if Is_Fixed_Length
(J
) then
3536 Known_Non_Null_Operand_Seen
:= True;
3538 elsif not Known_Non_Null_Operand_Seen
then
3540 -- Here if operand length is not statically known and no
3541 -- operand known to be non-null has been processed yet.
3542 -- If operand length is 0, we do not need to perform the
3543 -- assignment, and we must avoid the evaluation of the
3544 -- high bound of the slice, since it may underflow if the
3545 -- low bound is Ityp'First.
3548 Make_Implicit_If_Statement
(Cnode
,
3552 New_Occurrence_Of
(Var_Length
(J
), Loc
),
3553 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
3554 Then_Statements
=> New_List
(Assign
));
3557 Insert_Action
(Cnode
, Assign
, Suppress
=> All_Checks
);
3563 -- Finally we build the result, which is a reference to the array object
3565 Result
:= New_Occurrence_Of
(Ent
, Loc
);
3568 Rewrite
(Cnode
, Result
);
3569 Analyze_And_Resolve
(Cnode
, Atyp
);
3572 when Concatenation_Error
=>
3574 -- Kill warning generated for the declaration of the static out of
3575 -- range high bound, and instead generate a Constraint_Error with
3576 -- an appropriate specific message.
3578 Kill_Dead_Code
(Declaration_Node
(Entity
(High_Bound
)));
3579 Apply_Compile_Time_Constraint_Error
3581 Msg
=> "concatenation result upper bound out of range??",
3582 Reason
=> CE_Range_Check_Failed
);
3583 end Expand_Concatenate
;
3585 ---------------------------------------------------
3586 -- Expand_Membership_Minimize_Eliminate_Overflow --
3587 ---------------------------------------------------
3589 procedure Expand_Membership_Minimize_Eliminate_Overflow
(N
: Node_Id
) is
3590 pragma Assert
(Nkind
(N
) = N_In
);
3591 -- Despite the name, this routine applies only to N_In, not to
3592 -- N_Not_In. The latter is always rewritten as not (X in Y).
3594 Result_Type
: constant Entity_Id
:= Etype
(N
);
3595 -- Capture result type, may be a derived boolean type
3597 Loc
: constant Source_Ptr
:= Sloc
(N
);
3598 Lop
: constant Node_Id
:= Left_Opnd
(N
);
3599 Rop
: constant Node_Id
:= Right_Opnd
(N
);
3601 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3602 -- is thus tempting to capture these values, but due to the rewrites
3603 -- that occur as a result of overflow checking, these values change
3604 -- as we go along, and it is safe just to always use Etype explicitly.
3606 Restype
: constant Entity_Id
:= Etype
(N
);
3610 -- Bounds in Minimize calls, not used currently
3612 LLIB
: constant Entity_Id
:= Base_Type
(Standard_Long_Long_Integer
);
3613 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3616 Minimize_Eliminate_Overflows
(Lop
, Lo
, Hi
, Top_Level
=> False);
3618 -- If right operand is a subtype name, and the subtype name has no
3619 -- predicate, then we can just replace the right operand with an
3620 -- explicit range T'First .. T'Last, and use the explicit range code.
3622 if Nkind
(Rop
) /= N_Range
3623 and then No
(Predicate_Function
(Etype
(Rop
)))
3626 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
3631 Make_Attribute_Reference
(Loc
,
3632 Attribute_Name
=> Name_First
,
3633 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
)),
3635 Make_Attribute_Reference
(Loc
,
3636 Attribute_Name
=> Name_Last
,
3637 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
))));
3638 Analyze_And_Resolve
(Rop
, Rtyp
, Suppress
=> All_Checks
);
3642 -- Here for the explicit range case. Note that the bounds of the range
3643 -- have not been processed for minimized or eliminated checks.
3645 if Nkind
(Rop
) = N_Range
then
3646 Minimize_Eliminate_Overflows
3647 (Low_Bound
(Rop
), Lo
, Hi
, Top_Level
=> False);
3648 Minimize_Eliminate_Overflows
3649 (High_Bound
(Rop
), Lo
, Hi
, Top_Level
=> False);
3651 -- We have A in B .. C, treated as A >= B and then A <= C
3655 if Is_RTE
(Etype
(Lop
), RE_Bignum
)
3656 or else Is_RTE
(Etype
(Low_Bound
(Rop
)), RE_Bignum
)
3657 or else Is_RTE
(Etype
(High_Bound
(Rop
)), RE_Bignum
)
3660 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
3661 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
3662 L
: constant Entity_Id
:=
3663 Make_Defining_Identifier
(Loc
, Name_uL
);
3664 Lopnd
: constant Node_Id
:= Convert_To_Bignum
(Lop
);
3665 Lbound
: constant Node_Id
:=
3666 Convert_To_Bignum
(Low_Bound
(Rop
));
3667 Hbound
: constant Node_Id
:=
3668 Convert_To_Bignum
(High_Bound
(Rop
));
3670 -- Now we rewrite the membership test node to look like
3673 -- Bnn : Result_Type;
3675 -- M : Mark_Id := SS_Mark;
3676 -- L : Bignum := Lopnd;
3678 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3686 -- Insert declaration of L into declarations of bignum block
3689 (Last
(Declarations
(Blk
)),
3690 Make_Object_Declaration
(Loc
,
3691 Defining_Identifier
=> L
,
3692 Object_Definition
=>
3693 New_Occurrence_Of
(RTE
(RE_Bignum
), Loc
),
3694 Expression
=> Lopnd
));
3696 -- Insert assignment to Bnn into expressions of bignum block
3699 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
3700 Make_Assignment_Statement
(Loc
,
3701 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
3705 Make_Function_Call
(Loc
,
3707 New_Occurrence_Of
(RTE
(RE_Big_GE
), Loc
),
3708 Parameter_Associations
=> New_List
(
3709 New_Occurrence_Of
(L
, Loc
),
3713 Make_Function_Call
(Loc
,
3715 New_Occurrence_Of
(RTE
(RE_Big_LE
), Loc
),
3716 Parameter_Associations
=> New_List
(
3717 New_Occurrence_Of
(L
, Loc
),
3720 -- Now rewrite the node
3723 Make_Expression_With_Actions
(Loc
,
3724 Actions
=> New_List
(
3725 Make_Object_Declaration
(Loc
,
3726 Defining_Identifier
=> Bnn
,
3727 Object_Definition
=>
3728 New_Occurrence_Of
(Result_Type
, Loc
)),
3730 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
3731 Analyze_And_Resolve
(N
, Result_Type
);
3735 -- Here if no bignums around
3738 -- Case where types are all the same
3740 if Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(Low_Bound
(Rop
)))
3742 Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(High_Bound
(Rop
)))
3746 -- If types are not all the same, it means that we have rewritten
3747 -- at least one of them to be of type Long_Long_Integer, and we
3748 -- will convert the other operands to Long_Long_Integer.
3751 Convert_To_And_Rewrite
(LLIB
, Lop
);
3752 Set_Analyzed
(Lop
, False);
3753 Analyze_And_Resolve
(Lop
, LLIB
);
3755 -- For the right operand, avoid unnecessary recursion into
3756 -- this routine, we know that overflow is not possible.
3758 Convert_To_And_Rewrite
(LLIB
, Low_Bound
(Rop
));
3759 Convert_To_And_Rewrite
(LLIB
, High_Bound
(Rop
));
3760 Set_Analyzed
(Rop
, False);
3761 Analyze_And_Resolve
(Rop
, LLIB
, Suppress
=> Overflow_Check
);
3764 -- Now the three operands are of the same signed integer type,
3765 -- so we can use the normal expansion routine for membership,
3766 -- setting the flag to prevent recursion into this procedure.
3768 Set_No_Minimize_Eliminate
(N
);
3772 -- Right operand is a subtype name and the subtype has a predicate. We
3773 -- have to make sure the predicate is checked, and for that we need to
3774 -- use the standard N_In circuitry with appropriate types.
3777 pragma Assert
(Present
(Predicate_Function
(Etype
(Rop
))));
3779 -- If types are "right", just call Expand_N_In preventing recursion
3781 if Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(Rop
)) then
3782 Set_No_Minimize_Eliminate
(N
);
3787 elsif Is_RTE
(Etype
(Lop
), RE_Bignum
) then
3789 -- For X in T, we want to rewrite our node as
3792 -- Bnn : Result_Type;
3795 -- M : Mark_Id := SS_Mark;
3796 -- Lnn : Long_Long_Integer'Base
3802 -- if not Bignum_In_LLI_Range (Nnn) then
3805 -- Lnn := From_Bignum (Nnn);
3807 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3808 -- and then T'Base (Lnn) in T;
3817 -- A bit gruesome, but there doesn't seem to be a simpler way
3820 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
3821 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
3822 Lnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'L', N
);
3823 Nnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'N', N
);
3824 T
: constant Entity_Id
:= Etype
(Rop
);
3825 TB
: constant Entity_Id
:= Base_Type
(T
);
3829 -- Mark the last membership operation to prevent recursion
3833 Left_Opnd
=> Convert_To
(TB
, New_Occurrence_Of
(Lnn
, Loc
)),
3834 Right_Opnd
=> New_Occurrence_Of
(T
, Loc
));
3835 Set_No_Minimize_Eliminate
(Nin
);
3837 -- Now decorate the block
3840 (Last
(Declarations
(Blk
)),
3841 Make_Object_Declaration
(Loc
,
3842 Defining_Identifier
=> Lnn
,
3843 Object_Definition
=> New_Occurrence_Of
(LLIB
, Loc
)));
3846 (Last
(Declarations
(Blk
)),
3847 Make_Object_Declaration
(Loc
,
3848 Defining_Identifier
=> Nnn
,
3849 Object_Definition
=>
3850 New_Occurrence_Of
(RTE
(RE_Bignum
), Loc
)));
3853 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
3855 Make_Assignment_Statement
(Loc
,
3856 Name
=> New_Occurrence_Of
(Nnn
, Loc
),
3857 Expression
=> Relocate_Node
(Lop
)),
3859 Make_Implicit_If_Statement
(N
,
3863 Make_Function_Call
(Loc
,
3866 (RTE
(RE_Bignum_In_LLI_Range
), Loc
),
3867 Parameter_Associations
=> New_List
(
3868 New_Occurrence_Of
(Nnn
, Loc
)))),
3870 Then_Statements
=> New_List
(
3871 Make_Assignment_Statement
(Loc
,
3872 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
3874 New_Occurrence_Of
(Standard_False
, Loc
))),
3876 Else_Statements
=> New_List
(
3877 Make_Assignment_Statement
(Loc
,
3878 Name
=> New_Occurrence_Of
(Lnn
, Loc
),
3880 Make_Function_Call
(Loc
,
3882 New_Occurrence_Of
(RTE
(RE_From_Bignum
), Loc
),
3883 Parameter_Associations
=> New_List
(
3884 New_Occurrence_Of
(Nnn
, Loc
)))),
3886 Make_Assignment_Statement
(Loc
,
3887 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
3892 Left_Opnd
=> New_Occurrence_Of
(Lnn
, Loc
),
3897 Make_Attribute_Reference
(Loc
,
3898 Attribute_Name
=> Name_First
,
3900 New_Occurrence_Of
(TB
, Loc
))),
3904 Make_Attribute_Reference
(Loc
,
3905 Attribute_Name
=> Name_Last
,
3907 New_Occurrence_Of
(TB
, Loc
))))),
3909 Right_Opnd
=> Nin
))))));
3911 -- Now we can do the rewrite
3914 Make_Expression_With_Actions
(Loc
,
3915 Actions
=> New_List
(
3916 Make_Object_Declaration
(Loc
,
3917 Defining_Identifier
=> Bnn
,
3918 Object_Definition
=>
3919 New_Occurrence_Of
(Result_Type
, Loc
)),
3921 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
3922 Analyze_And_Resolve
(N
, Result_Type
);
3926 -- Not bignum case, but types don't match (this means we rewrote the
3927 -- left operand to be Long_Long_Integer).
3930 pragma Assert
(Base_Type
(Etype
(Lop
)) = LLIB
);
3932 -- We rewrite the membership test as (where T is the type with
3933 -- the predicate, i.e. the type of the right operand)
3935 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3936 -- and then T'Base (Lop) in T
3939 T
: constant Entity_Id
:= Etype
(Rop
);
3940 TB
: constant Entity_Id
:= Base_Type
(T
);
3944 -- The last membership test is marked to prevent recursion
3948 Left_Opnd
=> Convert_To
(TB
, Duplicate_Subexpr
(Lop
)),
3949 Right_Opnd
=> New_Occurrence_Of
(T
, Loc
));
3950 Set_No_Minimize_Eliminate
(Nin
);
3952 -- Now do the rewrite
3963 Make_Attribute_Reference
(Loc
,
3964 Attribute_Name
=> Name_First
,
3966 New_Occurrence_Of
(TB
, Loc
))),
3969 Make_Attribute_Reference
(Loc
,
3970 Attribute_Name
=> Name_Last
,
3972 New_Occurrence_Of
(TB
, Loc
))))),
3973 Right_Opnd
=> Nin
));
3974 Set_Analyzed
(N
, False);
3975 Analyze_And_Resolve
(N
, Restype
);
3979 end Expand_Membership_Minimize_Eliminate_Overflow
;
3981 ---------------------------------
3982 -- Expand_Nonbinary_Modular_Op --
3983 ---------------------------------
3985 procedure Expand_Nonbinary_Modular_Op
(N
: Node_Id
) is
3986 Loc
: constant Source_Ptr
:= Sloc
(N
);
3987 Typ
: constant Entity_Id
:= Etype
(N
);
3989 procedure Expand_Modular_Addition
;
3990 -- Expand the modular addition, handling the special case of adding a
3993 procedure Expand_Modular_Op
;
3994 -- Compute the general rule: (lhs OP rhs) mod Modulus
3996 procedure Expand_Modular_Subtraction
;
3997 -- Expand the modular addition, handling the special case of subtracting
4000 -----------------------------
4001 -- Expand_Modular_Addition --
4002 -----------------------------
4004 procedure Expand_Modular_Addition
is
4006 -- If this is not the addition of a constant then compute it using
4007 -- the general rule: (lhs + rhs) mod Modulus
4009 if Nkind
(Right_Opnd
(N
)) /= N_Integer_Literal
then
4012 -- If this is an addition of a constant, convert it to a subtraction
4013 -- plus a conditional expression since we can compute it faster than
4014 -- computing the modulus.
4016 -- modMinusRhs = Modulus - rhs
4017 -- if lhs < modMinusRhs then lhs + rhs
4018 -- else lhs - modMinusRhs
4022 Mod_Minus_Right
: constant Uint
:=
4023 Modulus
(Typ
) - Intval
(Right_Opnd
(N
));
4025 Exprs
: constant List_Id
:= New_List
;
4026 Cond_Expr
: constant Node_Id
:= New_Op_Node
(N_Op_Lt
, Loc
);
4027 Then_Expr
: constant Node_Id
:= New_Op_Node
(N_Op_Add
, Loc
);
4028 Else_Expr
: constant Node_Id
:= New_Op_Node
(N_Op_Subtract
,
4031 -- To prevent spurious visibility issues, convert all
4032 -- operands to Standard.Unsigned.
4034 Set_Left_Opnd
(Cond_Expr
,
4035 Unchecked_Convert_To
(Standard_Unsigned
,
4036 New_Copy_Tree
(Left_Opnd
(N
))));
4037 Set_Right_Opnd
(Cond_Expr
,
4038 Make_Integer_Literal
(Loc
, Mod_Minus_Right
));
4039 Append_To
(Exprs
, Cond_Expr
);
4041 Set_Left_Opnd
(Then_Expr
,
4042 Unchecked_Convert_To
(Standard_Unsigned
,
4043 New_Copy_Tree
(Left_Opnd
(N
))));
4044 Set_Right_Opnd
(Then_Expr
,
4045 Make_Integer_Literal
(Loc
, Intval
(Right_Opnd
(N
))));
4046 Append_To
(Exprs
, Then_Expr
);
4048 Set_Left_Opnd
(Else_Expr
,
4049 Unchecked_Convert_To
(Standard_Unsigned
,
4050 New_Copy_Tree
(Left_Opnd
(N
))));
4051 Set_Right_Opnd
(Else_Expr
,
4052 Make_Integer_Literal
(Loc
, Mod_Minus_Right
));
4053 Append_To
(Exprs
, Else_Expr
);
4056 Unchecked_Convert_To
(Typ
,
4057 Make_If_Expression
(Loc
, Expressions
=> Exprs
)));
4060 end Expand_Modular_Addition
;
4062 -----------------------
4063 -- Expand_Modular_Op --
4064 -----------------------
4066 procedure Expand_Modular_Op
is
4067 Op_Expr
: constant Node_Id
:= New_Op_Node
(Nkind
(N
), Loc
);
4068 Mod_Expr
: constant Node_Id
:= New_Op_Node
(N_Op_Mod
, Loc
);
4071 -- Convert nonbinary modular type operands into integer values. Thus
4072 -- we avoid never-ending loops expanding them, and we also ensure
4073 -- the back end never receives nonbinary modular type expressions.
4075 if Nkind_In
(Nkind
(N
), N_Op_And
, N_Op_Or
) then
4076 Set_Left_Opnd
(Op_Expr
,
4077 Unchecked_Convert_To
(Standard_Unsigned
,
4078 New_Copy_Tree
(Left_Opnd
(N
))));
4079 Set_Right_Opnd
(Op_Expr
,
4080 Unchecked_Convert_To
(Standard_Unsigned
,
4081 New_Copy_Tree
(Right_Opnd
(N
))));
4082 Set_Left_Opnd
(Mod_Expr
,
4083 Unchecked_Convert_To
(Standard_Integer
, Op_Expr
));
4086 Set_Left_Opnd
(Op_Expr
,
4087 Unchecked_Convert_To
(Standard_Integer
,
4088 New_Copy_Tree
(Left_Opnd
(N
))));
4089 Set_Right_Opnd
(Op_Expr
,
4090 Unchecked_Convert_To
(Standard_Integer
,
4091 New_Copy_Tree
(Right_Opnd
(N
))));
4093 -- Link this node to the tree to analyze it
4095 -- If the parent node is an expression with actions we link it to
4096 -- N since otherwise Force_Evaluation cannot identify if this node
4097 -- comes from the Expression and rejects generating the temporary.
4099 if Nkind
(Parent
(N
)) = N_Expression_With_Actions
then
4100 Set_Parent
(Op_Expr
, N
);
4105 Set_Parent
(Op_Expr
, Parent
(N
));
4110 -- Force generating a temporary because in the expansion of this
4111 -- expression we may generate code that performs this computation
4114 Force_Evaluation
(Op_Expr
, Mode
=> Strict
);
4116 Set_Left_Opnd
(Mod_Expr
, Op_Expr
);
4119 Set_Right_Opnd
(Mod_Expr
,
4120 Make_Integer_Literal
(Loc
, Modulus
(Typ
)));
4123 Unchecked_Convert_To
(Typ
, Mod_Expr
));
4124 end Expand_Modular_Op
;
4126 --------------------------------
4127 -- Expand_Modular_Subtraction --
4128 --------------------------------
4130 procedure Expand_Modular_Subtraction
is
4132 -- If this is not the addition of a constant then compute it using
4133 -- the general rule: (lhs + rhs) mod Modulus
4135 if Nkind
(Right_Opnd
(N
)) /= N_Integer_Literal
then
4138 -- If this is an addition of a constant, convert it to a subtraction
4139 -- plus a conditional expression since we can compute it faster than
4140 -- computing the modulus.
4142 -- modMinusRhs = Modulus - rhs
4143 -- if lhs < rhs then lhs + modMinusRhs
4148 Mod_Minus_Right
: constant Uint
:=
4149 Modulus
(Typ
) - Intval
(Right_Opnd
(N
));
4151 Exprs
: constant List_Id
:= New_List
;
4152 Cond_Expr
: constant Node_Id
:= New_Op_Node
(N_Op_Lt
, Loc
);
4153 Then_Expr
: constant Node_Id
:= New_Op_Node
(N_Op_Add
, Loc
);
4154 Else_Expr
: constant Node_Id
:= New_Op_Node
(N_Op_Subtract
,
4157 Set_Left_Opnd
(Cond_Expr
,
4158 Unchecked_Convert_To
(Standard_Unsigned
,
4159 New_Copy_Tree
(Left_Opnd
(N
))));
4160 Set_Right_Opnd
(Cond_Expr
,
4161 Make_Integer_Literal
(Loc
, Intval
(Right_Opnd
(N
))));
4162 Append_To
(Exprs
, Cond_Expr
);
4164 Set_Left_Opnd
(Then_Expr
,
4165 Unchecked_Convert_To
(Standard_Unsigned
,
4166 New_Copy_Tree
(Left_Opnd
(N
))));
4167 Set_Right_Opnd
(Then_Expr
,
4168 Make_Integer_Literal
(Loc
, Mod_Minus_Right
));
4169 Append_To
(Exprs
, Then_Expr
);
4171 Set_Left_Opnd
(Else_Expr
,
4172 Unchecked_Convert_To
(Standard_Unsigned
,
4173 New_Copy_Tree
(Left_Opnd
(N
))));
4174 Set_Right_Opnd
(Else_Expr
,
4175 Unchecked_Convert_To
(Standard_Unsigned
,
4176 New_Copy_Tree
(Right_Opnd
(N
))));
4177 Append_To
(Exprs
, Else_Expr
);
4180 Unchecked_Convert_To
(Typ
,
4181 Make_If_Expression
(Loc
, Expressions
=> Exprs
)));
4184 end Expand_Modular_Subtraction
;
4186 -- Start of processing for Expand_Nonbinary_Modular_Op
4189 -- No action needed if front-end expansion is not required or if we
4190 -- have a binary modular operand.
4192 if not Expand_Nonbinary_Modular_Ops
4193 or else not Non_Binary_Modulus
(Typ
)
4200 Expand_Modular_Addition
;
4202 when N_Op_Subtract
=>
4203 Expand_Modular_Subtraction
;
4207 -- Expand -expr into (0 - expr)
4210 Make_Op_Subtract
(Loc
,
4211 Left_Opnd
=> Make_Integer_Literal
(Loc
, 0),
4212 Right_Opnd
=> Right_Opnd
(N
)));
4213 Analyze_And_Resolve
(N
, Typ
);
4219 Analyze_And_Resolve
(N
, Typ
);
4220 end Expand_Nonbinary_Modular_Op
;
4222 ------------------------
4223 -- Expand_N_Allocator --
4224 ------------------------
4226 procedure Expand_N_Allocator
(N
: Node_Id
) is
4227 Etyp
: constant Entity_Id
:= Etype
(Expression
(N
));
4228 Loc
: constant Source_Ptr
:= Sloc
(N
);
4229 PtrT
: constant Entity_Id
:= Etype
(N
);
4231 procedure Rewrite_Coextension
(N
: Node_Id
);
4232 -- Static coextensions have the same lifetime as the entity they
4233 -- constrain. Such occurrences can be rewritten as aliased objects
4234 -- and their unrestricted access used instead of the coextension.
4236 function Size_In_Storage_Elements
(E
: Entity_Id
) return Node_Id
;
4237 -- Given a constrained array type E, returns a node representing the
4238 -- code to compute the size in storage elements for the given type.
4239 -- This is done without using the attribute (which malfunctions for
4242 -------------------------
4243 -- Rewrite_Coextension --
4244 -------------------------
4246 procedure Rewrite_Coextension
(N
: Node_Id
) is
4247 Temp_Id
: constant Node_Id
:= Make_Temporary
(Loc
, 'C');
4248 Temp_Decl
: Node_Id
;
4252 -- Cnn : aliased Etyp;
4255 Make_Object_Declaration
(Loc
,
4256 Defining_Identifier
=> Temp_Id
,
4257 Aliased_Present
=> True,
4258 Object_Definition
=> New_Occurrence_Of
(Etyp
, Loc
));
4260 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
4261 Set_Expression
(Temp_Decl
, Expression
(Expression
(N
)));
4264 Insert_Action
(N
, Temp_Decl
);
4266 Make_Attribute_Reference
(Loc
,
4267 Prefix
=> New_Occurrence_Of
(Temp_Id
, Loc
),
4268 Attribute_Name
=> Name_Unrestricted_Access
));
4270 Analyze_And_Resolve
(N
, PtrT
);
4271 end Rewrite_Coextension
;
4273 ------------------------------
4274 -- Size_In_Storage_Elements --
4275 ------------------------------
4277 function Size_In_Storage_Elements
(E
: Entity_Id
) return Node_Id
is
4279 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4280 -- However, the reason for the existence of this function is
4281 -- to construct a test for sizes too large, which means near the
4282 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4283 -- is that we get overflows when sizes are greater than 2**31.
4285 -- So what we end up doing for array types is to use the expression:
4287 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4289 -- which avoids this problem. All this is a bit bogus, but it does
4290 -- mean we catch common cases of trying to allocate arrays that
4291 -- are too large, and which in the absence of a check results in
4292 -- undetected chaos ???
4294 -- Note in particular that this is a pessimistic estimate in the
4295 -- case of packed array types, where an array element might occupy
4296 -- just a fraction of a storage element???
4301 pragma Warnings
(Off
, Res
);
4304 for J
in 1 .. Number_Dimensions
(E
) loop
4306 Make_Attribute_Reference
(Loc
,
4307 Prefix
=> New_Occurrence_Of
(E
, Loc
),
4308 Attribute_Name
=> Name_Length
,
4309 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, J
)));
4316 Make_Op_Multiply
(Loc
,
4323 Make_Op_Multiply
(Loc
,
4326 Make_Attribute_Reference
(Loc
,
4327 Prefix
=> New_Occurrence_Of
(Component_Type
(E
), Loc
),
4328 Attribute_Name
=> Name_Max_Size_In_Storage_Elements
));
4330 end Size_In_Storage_Elements
;
4334 Dtyp
: constant Entity_Id
:= Available_View
(Designated_Type
(PtrT
));
4338 Rel_Typ
: Entity_Id
;
4341 -- Start of processing for Expand_N_Allocator
4344 -- RM E.2.3(22). We enforce that the expected type of an allocator
4345 -- shall not be a remote access-to-class-wide-limited-private type
4347 -- Why is this being done at expansion time, seems clearly wrong ???
4349 Validate_Remote_Access_To_Class_Wide_Type
(N
);
4351 -- Processing for anonymous access-to-controlled types. These access
4352 -- types receive a special finalization master which appears in the
4353 -- declarations of the enclosing semantic unit. This expansion is done
4354 -- now to ensure that any additional types generated by this routine or
4355 -- Expand_Allocator_Expression inherit the proper type attributes.
4357 if (Ekind
(PtrT
) = E_Anonymous_Access_Type
4358 or else (Is_Itype
(PtrT
) and then No
(Finalization_Master
(PtrT
))))
4359 and then Needs_Finalization
(Dtyp
)
4361 -- Detect the allocation of an anonymous controlled object where the
4362 -- type of the context is named. For example:
4364 -- procedure Proc (Ptr : Named_Access_Typ);
4365 -- Proc (new Designated_Typ);
4367 -- Regardless of the anonymous-to-named access type conversion, the
4368 -- lifetime of the object must be associated with the named access
4369 -- type. Use the finalization-related attributes of this type.
4371 if Nkind_In
(Parent
(N
), N_Type_Conversion
,
4372 N_Unchecked_Type_Conversion
)
4373 and then Ekind_In
(Etype
(Parent
(N
)), E_Access_Subtype
,
4375 E_General_Access_Type
)
4377 Rel_Typ
:= Etype
(Parent
(N
));
4382 -- Anonymous access-to-controlled types allocate on the global pool.
4383 -- Note that this is a "root type only" attribute.
4385 if No
(Associated_Storage_Pool
(PtrT
)) then
4386 if Present
(Rel_Typ
) then
4387 Set_Associated_Storage_Pool
4388 (Root_Type
(PtrT
), Associated_Storage_Pool
(Rel_Typ
));
4390 Set_Associated_Storage_Pool
4391 (Root_Type
(PtrT
), RTE
(RE_Global_Pool_Object
));
4395 -- The finalization master must be inserted and analyzed as part of
4396 -- the current semantic unit. Note that the master is updated when
4397 -- analysis changes current units. Note that this is a "root type
4400 if Present
(Rel_Typ
) then
4401 Set_Finalization_Master
4402 (Root_Type
(PtrT
), Finalization_Master
(Rel_Typ
));
4404 Build_Anonymous_Master
(Root_Type
(PtrT
));
4408 -- Set the storage pool and find the appropriate version of Allocate to
4409 -- call. Do not overwrite the storage pool if it is already set, which
4410 -- can happen for build-in-place function returns (see
4411 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4413 if No
(Storage_Pool
(N
)) then
4414 Pool
:= Associated_Storage_Pool
(Root_Type
(PtrT
));
4416 if Present
(Pool
) then
4417 Set_Storage_Pool
(N
, Pool
);
4419 if Is_RTE
(Pool
, RE_SS_Pool
) then
4420 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
4422 -- In the case of an allocator for a simple storage pool, locate
4423 -- and save a reference to the pool type's Allocate routine.
4425 elsif Present
(Get_Rep_Pragma
4426 (Etype
(Pool
), Name_Simple_Storage_Pool_Type
))
4429 Pool_Type
: constant Entity_Id
:= Base_Type
(Etype
(Pool
));
4430 Alloc_Op
: Entity_Id
;
4432 Alloc_Op
:= Get_Name_Entity_Id
(Name_Allocate
);
4433 while Present
(Alloc_Op
) loop
4434 if Scope
(Alloc_Op
) = Scope
(Pool_Type
)
4435 and then Present
(First_Formal
(Alloc_Op
))
4436 and then Etype
(First_Formal
(Alloc_Op
)) = Pool_Type
4438 Set_Procedure_To_Call
(N
, Alloc_Op
);
4441 Alloc_Op
:= Homonym
(Alloc_Op
);
4446 elsif Is_Class_Wide_Type
(Etype
(Pool
)) then
4447 Set_Procedure_To_Call
(N
, RTE
(RE_Allocate_Any
));
4450 Set_Procedure_To_Call
(N
,
4451 Find_Prim_Op
(Etype
(Pool
), Name_Allocate
));
4456 -- Under certain circumstances we can replace an allocator by an access
4457 -- to statically allocated storage. The conditions, as noted in AARM
4458 -- 3.10 (10c) are as follows:
4460 -- Size and initial value is known at compile time
4461 -- Access type is access-to-constant
4463 -- The allocator is not part of a constraint on a record component,
4464 -- because in that case the inserted actions are delayed until the
4465 -- record declaration is fully analyzed, which is too late for the
4466 -- analysis of the rewritten allocator.
4468 if Is_Access_Constant
(PtrT
)
4469 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
4470 and then Compile_Time_Known_Value
(Expression
(Expression
(N
)))
4471 and then Size_Known_At_Compile_Time
4472 (Etype
(Expression
(Expression
(N
))))
4473 and then not Is_Record_Type
(Current_Scope
)
4475 -- Here we can do the optimization. For the allocator
4479 -- We insert an object declaration
4481 -- Tnn : aliased x := y;
4483 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4484 -- marked as requiring static allocation.
4486 Temp
:= Make_Temporary
(Loc
, 'T', Expression
(Expression
(N
)));
4487 Desig
:= Subtype_Mark
(Expression
(N
));
4489 -- If context is constrained, use constrained subtype directly,
4490 -- so that the constant is not labelled as having a nominally
4491 -- unconstrained subtype.
4493 if Entity
(Desig
) = Base_Type
(Dtyp
) then
4494 Desig
:= New_Occurrence_Of
(Dtyp
, Loc
);
4498 Make_Object_Declaration
(Loc
,
4499 Defining_Identifier
=> Temp
,
4500 Aliased_Present
=> True,
4501 Constant_Present
=> Is_Access_Constant
(PtrT
),
4502 Object_Definition
=> Desig
,
4503 Expression
=> Expression
(Expression
(N
))));
4506 Make_Attribute_Reference
(Loc
,
4507 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
4508 Attribute_Name
=> Name_Unrestricted_Access
));
4510 Analyze_And_Resolve
(N
, PtrT
);
4512 -- We set the variable as statically allocated, since we don't want
4513 -- it going on the stack of the current procedure.
4515 Set_Is_Statically_Allocated
(Temp
);
4519 -- Same if the allocator is an access discriminant for a local object:
4520 -- instead of an allocator we create a local value and constrain the
4521 -- enclosing object with the corresponding access attribute.
4523 if Is_Static_Coextension
(N
) then
4524 Rewrite_Coextension
(N
);
4528 -- Check for size too large, we do this because the back end misses
4529 -- proper checks here and can generate rubbish allocation calls when
4530 -- we are near the limit. We only do this for the 32-bit address case
4531 -- since that is from a practical point of view where we see a problem.
4533 if System_Address_Size
= 32
4534 and then not Storage_Checks_Suppressed
(PtrT
)
4535 and then not Storage_Checks_Suppressed
(Dtyp
)
4536 and then not Storage_Checks_Suppressed
(Etyp
)
4538 -- The check we want to generate should look like
4540 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4541 -- raise Storage_Error;
4544 -- where 3.5 gigabytes is a constant large enough to accommodate any
4545 -- reasonable request for. But we can't do it this way because at
4546 -- least at the moment we don't compute this attribute right, and
4547 -- can silently give wrong results when the result gets large. Since
4548 -- this is all about large results, that's bad, so instead we only
4549 -- apply the check for constrained arrays, and manually compute the
4550 -- value of the attribute ???
4552 if Is_Array_Type
(Etyp
) and then Is_Constrained
(Etyp
) then
4554 Make_Raise_Storage_Error
(Loc
,
4557 Left_Opnd
=> Size_In_Storage_Elements
(Etyp
),
4559 Make_Integer_Literal
(Loc
, Uint_7
* (Uint_2
** 29))),
4560 Reason
=> SE_Object_Too_Large
));
4564 -- If no storage pool has been specified and we have the restriction
4565 -- No_Standard_Allocators_After_Elaboration is present, then generate
4566 -- a call to Elaboration_Allocators.Check_Standard_Allocator.
4568 if Nkind
(N
) = N_Allocator
4569 and then No
(Storage_Pool
(N
))
4570 and then Restriction_Active
(No_Standard_Allocators_After_Elaboration
)
4573 Make_Procedure_Call_Statement
(Loc
,
4575 New_Occurrence_Of
(RTE
(RE_Check_Standard_Allocator
), Loc
)));
4578 -- Handle case of qualified expression (other than optimization above)
4579 -- First apply constraint checks, because the bounds or discriminants
4580 -- in the aggregate might not match the subtype mark in the allocator.
4582 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
4584 Exp
: constant Node_Id
:= Expression
(Expression
(N
));
4585 Typ
: constant Entity_Id
:= Etype
(Expression
(N
));
4588 Apply_Constraint_Check
(Exp
, Typ
);
4589 Apply_Predicate_Check
(Exp
, Typ
);
4592 Expand_Allocator_Expression
(N
);
4596 -- If the allocator is for a type which requires initialization, and
4597 -- there is no initial value (i.e. operand is a subtype indication
4598 -- rather than a qualified expression), then we must generate a call to
4599 -- the initialization routine using an expressions action node:
4601 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4603 -- Here ptr_T is the pointer type for the allocator, and T is the
4604 -- subtype of the allocator. A special case arises if the designated
4605 -- type of the access type is a task or contains tasks. In this case
4606 -- the call to Init (Temp.all ...) is replaced by code that ensures
4607 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4608 -- for details). In addition, if the type T is a task type, then the
4609 -- first argument to Init must be converted to the task record type.
4612 T
: constant Entity_Id
:= Etype
(Expression
(N
));
4618 Init_Arg1
: Node_Id
;
4619 Init_Call
: Node_Id
;
4620 Temp_Decl
: Node_Id
;
4621 Temp_Type
: Entity_Id
;
4624 if No_Initialization
(N
) then
4626 -- Even though this might be a simple allocation, create a custom
4627 -- Allocate if the context requires it.
4629 if Present
(Finalization_Master
(PtrT
)) then
4630 Build_Allocate_Deallocate_Proc
4632 Is_Allocate
=> True);
4635 -- Optimize the default allocation of an array object when pragma
4636 -- Initialize_Scalars or Normalize_Scalars is in effect. Construct an
4637 -- in-place initialization aggregate which may be convert into a fast
4638 -- memset by the backend.
4640 elsif Init_Or_Norm_Scalars
4641 and then Is_Array_Type
(T
)
4643 -- The array must lack atomic components because they are treated
4644 -- as non-static, and as a result the backend will not initialize
4645 -- the memory in one go.
4647 and then not Has_Atomic_Components
(T
)
4649 -- The array must not be packed because the invalid values in
4650 -- System.Scalar_Values are multiples of Storage_Unit.
4652 and then not Is_Packed
(T
)
4654 -- The array must have static non-empty ranges, otherwise the
4655 -- backend cannot initialize the memory in one go.
4657 and then Has_Static_Non_Empty_Array_Bounds
(T
)
4659 -- The optimization is only relevant for arrays of scalar types
4661 and then Is_Scalar_Type
(Component_Type
(T
))
4663 -- Similar to regular array initialization using a type init proc,
4664 -- predicate checks are not performed because the initialization
4665 -- values are intentionally invalid, and may violate the predicate.
4667 and then not Has_Predicates
(Component_Type
(T
))
4669 -- The component type must have a single initialization value
4671 and then Needs_Simple_Initialization
4672 (Typ
=> Component_Type
(T
),
4673 Consider_IS
=> True)
4676 Temp
:= Make_Temporary
(Loc
, 'P');
4679 -- Temp : Ptr_Typ := new ...;
4684 Make_Object_Declaration
(Loc
,
4685 Defining_Identifier
=> Temp
,
4686 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
4687 Expression
=> Relocate_Node
(N
)),
4688 Suppress
=> All_Checks
);
4691 -- Temp.all := (others => ...);
4696 Make_Assignment_Statement
(Loc
,
4698 Make_Explicit_Dereference
(Loc
,
4699 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)),
4704 Size
=> Esize
(Component_Type
(T
)))),
4705 Suppress
=> All_Checks
);
4707 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
4708 Analyze_And_Resolve
(N
, PtrT
);
4710 -- Case of no initialization procedure present
4712 elsif not Has_Non_Null_Base_Init_Proc
(T
) then
4714 -- Case of simple initialization required
4716 if Needs_Simple_Initialization
(T
) then
4717 Check_Restriction
(No_Default_Initialization
, N
);
4718 Rewrite
(Expression
(N
),
4719 Make_Qualified_Expression
(Loc
,
4720 Subtype_Mark
=> New_Occurrence_Of
(T
, Loc
),
4721 Expression
=> Get_Simple_Init_Val
(T
, N
)));
4723 Analyze_And_Resolve
(Expression
(Expression
(N
)), T
);
4724 Analyze_And_Resolve
(Expression
(N
), T
);
4725 Set_Paren_Count
(Expression
(Expression
(N
)), 1);
4726 Expand_N_Allocator
(N
);
4728 -- No initialization required
4731 Build_Allocate_Deallocate_Proc
4733 Is_Allocate
=> True);
4736 -- Case of initialization procedure present, must be called
4739 Check_Restriction
(No_Default_Initialization
, N
);
4741 if not Restriction_Active
(No_Default_Initialization
) then
4742 Init
:= Base_Init_Proc
(T
);
4744 Temp
:= Make_Temporary
(Loc
, 'P');
4746 -- Construct argument list for the initialization routine call
4749 Make_Explicit_Dereference
(Loc
,
4751 New_Occurrence_Of
(Temp
, Loc
));
4753 Set_Assignment_OK
(Init_Arg1
);
4756 -- The initialization procedure expects a specific type. if the
4757 -- context is access to class wide, indicate that the object
4758 -- being allocated has the right specific type.
4760 if Is_Class_Wide_Type
(Dtyp
) then
4761 Init_Arg1
:= Unchecked_Convert_To
(T
, Init_Arg1
);
4764 -- If designated type is a concurrent type or if it is private
4765 -- type whose definition is a concurrent type, the first
4766 -- argument in the Init routine has to be unchecked conversion
4767 -- to the corresponding record type. If the designated type is
4768 -- a derived type, also convert the argument to its root type.
4770 if Is_Concurrent_Type
(T
) then
4772 Unchecked_Convert_To
(
4773 Corresponding_Record_Type
(T
), Init_Arg1
);
4775 elsif Is_Private_Type
(T
)
4776 and then Present
(Full_View
(T
))
4777 and then Is_Concurrent_Type
(Full_View
(T
))
4780 Unchecked_Convert_To
4781 (Corresponding_Record_Type
(Full_View
(T
)), Init_Arg1
);
4783 elsif Etype
(First_Formal
(Init
)) /= Base_Type
(T
) then
4785 Ftyp
: constant Entity_Id
:= Etype
(First_Formal
(Init
));
4788 Init_Arg1
:= OK_Convert_To
(Etype
(Ftyp
), Init_Arg1
);
4789 Set_Etype
(Init_Arg1
, Ftyp
);
4793 Args
:= New_List
(Init_Arg1
);
4795 -- For the task case, pass the Master_Id of the access type as
4796 -- the value of the _Master parameter, and _Chain as the value
4797 -- of the _Chain parameter (_Chain will be defined as part of
4798 -- the generated code for the allocator).
4800 -- In Ada 2005, the context may be a function that returns an
4801 -- anonymous access type. In that case the Master_Id has been
4802 -- created when expanding the function declaration.
4804 if Has_Task
(T
) then
4805 if No
(Master_Id
(Base_Type
(PtrT
))) then
4807 -- The designated type was an incomplete type, and the
4808 -- access type did not get expanded. Salvage it now.
4810 if not Restriction_Active
(No_Task_Hierarchy
) then
4811 if Present
(Parent
(Base_Type
(PtrT
))) then
4812 Expand_N_Full_Type_Declaration
4813 (Parent
(Base_Type
(PtrT
)));
4815 -- The only other possibility is an itype. For this
4816 -- case, the master must exist in the context. This is
4817 -- the case when the allocator initializes an access
4818 -- component in an init-proc.
4821 pragma Assert
(Is_Itype
(PtrT
));
4822 Build_Master_Renaming
(PtrT
, N
);
4827 -- If the context of the allocator is a declaration or an
4828 -- assignment, we can generate a meaningful image for it,
4829 -- even though subsequent assignments might remove the
4830 -- connection between task and entity. We build this image
4831 -- when the left-hand side is a simple variable, a simple
4832 -- indexed assignment or a simple selected component.
4834 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
4836 Nam
: constant Node_Id
:= Name
(Parent
(N
));
4839 if Is_Entity_Name
(Nam
) then
4841 Build_Task_Image_Decls
4844 (Entity
(Nam
), Sloc
(Nam
)), T
);
4846 elsif Nkind_In
(Nam
, N_Indexed_Component
,
4847 N_Selected_Component
)
4848 and then Is_Entity_Name
(Prefix
(Nam
))
4851 Build_Task_Image_Decls
4852 (Loc
, Nam
, Etype
(Prefix
(Nam
)));
4854 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
4858 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
4860 Build_Task_Image_Decls
4861 (Loc
, Defining_Identifier
(Parent
(N
)), T
);
4864 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
4867 if Restriction_Active
(No_Task_Hierarchy
) then
4869 New_Occurrence_Of
(RTE
(RE_Library_Task_Level
), Loc
));
4873 (Master_Id
(Base_Type
(Root_Type
(PtrT
))), Loc
));
4876 Append_To
(Args
, Make_Identifier
(Loc
, Name_uChain
));
4878 Decl
:= Last
(Decls
);
4880 New_Occurrence_Of
(Defining_Identifier
(Decl
), Loc
));
4882 -- Has_Task is false, Decls not used
4888 -- Add discriminants if discriminated type
4891 Dis
: Boolean := False;
4892 Typ
: Entity_Id
:= Empty
;
4895 if Has_Discriminants
(T
) then
4899 -- Type may be a private type with no visible discriminants
4900 -- in which case check full view if in scope, or the
4901 -- underlying_full_view if dealing with a type whose full
4902 -- view may be derived from a private type whose own full
4903 -- view has discriminants.
4905 elsif Is_Private_Type
(T
) then
4906 if Present
(Full_View
(T
))
4907 and then Has_Discriminants
(Full_View
(T
))
4910 Typ
:= Full_View
(T
);
4912 elsif Present
(Underlying_Full_View
(T
))
4913 and then Has_Discriminants
(Underlying_Full_View
(T
))
4916 Typ
:= Underlying_Full_View
(T
);
4922 -- If the allocated object will be constrained by the
4923 -- default values for discriminants, then build a subtype
4924 -- with those defaults, and change the allocated subtype
4925 -- to that. Note that this happens in fewer cases in Ada
4928 if not Is_Constrained
(Typ
)
4929 and then Present
(Discriminant_Default_Value
4930 (First_Discriminant
(Typ
)))
4931 and then (Ada_Version
< Ada_2005
4933 Object_Type_Has_Constrained_Partial_View
4934 (Typ
, Current_Scope
))
4936 Typ
:= Build_Default_Subtype
(Typ
, N
);
4937 Set_Expression
(N
, New_Occurrence_Of
(Typ
, Loc
));
4940 Discr
:= First_Elmt
(Discriminant_Constraint
(Typ
));
4941 while Present
(Discr
) loop
4942 Nod
:= Node
(Discr
);
4943 Append
(New_Copy_Tree
(Node
(Discr
)), Args
);
4945 -- AI-416: when the discriminant constraint is an
4946 -- anonymous access type make sure an accessibility
4947 -- check is inserted if necessary (3.10.2(22.q/2))
4949 if Ada_Version
>= Ada_2005
4951 Ekind
(Etype
(Nod
)) = E_Anonymous_Access_Type
4953 Apply_Accessibility_Check
4954 (Nod
, Typ
, Insert_Node
=> Nod
);
4962 -- We set the allocator as analyzed so that when we analyze
4963 -- the if expression node, we do not get an unwanted recursive
4964 -- expansion of the allocator expression.
4966 Set_Analyzed
(N
, True);
4967 Nod
:= Relocate_Node
(N
);
4969 -- Here is the transformation:
4970 -- input: new Ctrl_Typ
4971 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4972 -- Ctrl_TypIP (Temp.all, ...);
4973 -- [Deep_]Initialize (Temp.all);
4975 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4976 -- is the subtype of the allocator.
4979 Make_Object_Declaration
(Loc
,
4980 Defining_Identifier
=> Temp
,
4981 Constant_Present
=> True,
4982 Object_Definition
=> New_Occurrence_Of
(Temp_Type
, Loc
),
4985 Set_Assignment_OK
(Temp_Decl
);
4986 Insert_Action
(N
, Temp_Decl
, Suppress
=> All_Checks
);
4988 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
4990 -- If the designated type is a task type or contains tasks,
4991 -- create block to activate created tasks, and insert
4992 -- declaration for Task_Image variable ahead of call.
4994 if Has_Task
(T
) then
4996 L
: constant List_Id
:= New_List
;
4999 Build_Task_Allocate_Block
(L
, Nod
, Args
);
5001 Insert_List_Before
(First
(Declarations
(Blk
)), Decls
);
5002 Insert_Actions
(N
, L
);
5007 Make_Procedure_Call_Statement
(Loc
,
5008 Name
=> New_Occurrence_Of
(Init
, Loc
),
5009 Parameter_Associations
=> Args
));
5012 if Needs_Finalization
(T
) then
5015 -- [Deep_]Initialize (Init_Arg1);
5019 (Obj_Ref
=> New_Copy_Tree
(Init_Arg1
),
5022 -- Guard against a missing [Deep_]Initialize when the
5023 -- designated type was not properly frozen.
5025 if Present
(Init_Call
) then
5026 Insert_Action
(N
, Init_Call
);
5030 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
5031 Analyze_And_Resolve
(N
, PtrT
);
5036 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
5037 -- object that has been rewritten as a reference, we displace "this"
5038 -- to reference properly its secondary dispatch table.
5040 if Nkind
(N
) = N_Identifier
and then Is_Interface
(Dtyp
) then
5041 Displace_Allocator_Pointer
(N
);
5045 when RE_Not_Available
=>
5047 end Expand_N_Allocator
;
5049 -----------------------
5050 -- Expand_N_And_Then --
5051 -----------------------
5053 procedure Expand_N_And_Then
(N
: Node_Id
)
5054 renames Expand_Short_Circuit_Operator
;
5056 ------------------------------
5057 -- Expand_N_Case_Expression --
5058 ------------------------------
5060 procedure Expand_N_Case_Expression
(N
: Node_Id
) is
5062 function Is_Copy_Type
(Typ
: Entity_Id
) return Boolean;
5063 -- Return True if we can copy objects of this type when expanding a case
5070 function Is_Copy_Type
(Typ
: Entity_Id
) return Boolean is
5072 -- If Minimize_Expression_With_Actions is True, we can afford to copy
5073 -- large objects, as long as they are constrained and not limited.
5076 Is_Elementary_Type
(Underlying_Type
(Typ
))
5078 (Minimize_Expression_With_Actions
5079 and then Is_Constrained
(Underlying_Type
(Typ
))
5080 and then not Is_Limited_View
(Underlying_Type
(Typ
)));
5085 Loc
: constant Source_Ptr
:= Sloc
(N
);
5086 Par
: constant Node_Id
:= Parent
(N
);
5087 Typ
: constant Entity_Id
:= Etype
(N
);
5091 Case_Stmt
: Node_Id
;
5095 Target_Typ
: Entity_Id
;
5097 In_Predicate
: Boolean := False;
5098 -- Flag set when the case expression appears within a predicate
5100 Optimize_Return_Stmt
: Boolean := False;
5101 -- Flag set when the case expression can be optimized in the context of
5102 -- a simple return statement.
5104 -- Start of processing for Expand_N_Case_Expression
5107 -- Check for MINIMIZED/ELIMINATED overflow mode
5109 if Minimized_Eliminated_Overflow_Check
(N
) then
5110 Apply_Arithmetic_Overflow_Check
(N
);
5114 -- If the case expression is a predicate specification, and the type
5115 -- to which it applies has a static predicate aspect, do not expand,
5116 -- because it will be converted to the proper predicate form later.
5118 if Ekind_In
(Current_Scope
, E_Function
, E_Procedure
)
5119 and then Is_Predicate_Function
(Current_Scope
)
5121 In_Predicate
:= True;
5123 if Has_Static_Predicate_Aspect
(Etype
(First_Entity
(Current_Scope
)))
5129 -- When the type of the case expression is elementary, expand
5131 -- (case X is when A => AX, when B => BX ...)
5146 -- In all other cases expand into
5149 -- type Ptr_Typ is access all Typ;
5150 -- Target : Ptr_Typ;
5153 -- Target := AX'Unrestricted_Access;
5155 -- Target := BX'Unrestricted_Access;
5158 -- in Target.all end;
5160 -- This approach avoids extra copies of potentially large objects. It
5161 -- also allows handling of values of limited or unconstrained types.
5162 -- Note that we do the copy also for constrained, nonlimited types
5163 -- when minimizing expressions with actions (e.g. when generating C
5164 -- code) since it allows us to do the optimization below in more cases.
5166 -- Small optimization: when the case expression appears in the context
5167 -- of a simple return statement, expand into
5178 Make_Case_Statement
(Loc
,
5179 Expression
=> Expression
(N
),
5180 Alternatives
=> New_List
);
5182 -- Preserve the original context for which the case statement is being
5183 -- generated. This is needed by the finalization machinery to prevent
5184 -- the premature finalization of controlled objects found within the
5187 Set_From_Conditional_Expression
(Case_Stmt
);
5192 if Is_Copy_Type
(Typ
) then
5195 -- ??? Do not perform the optimization when the return statement is
5196 -- within a predicate function, as this causes spurious errors. Could
5197 -- this be a possible mismatch in handling this case somewhere else
5198 -- in semantic analysis?
5200 Optimize_Return_Stmt
:=
5201 Nkind
(Par
) = N_Simple_Return_Statement
and then not In_Predicate
;
5203 -- Otherwise create an access type to handle the general case using
5204 -- 'Unrestricted_Access.
5207 -- type Ptr_Typ is access all Typ;
5210 if Generate_C_Code
then
5212 -- We cannot ensure that correct C code will be generated if any
5213 -- temporary is created down the line (to e.g. handle checks or
5214 -- capture values) since we might end up with dangling references
5215 -- to local variables, so better be safe and reject the construct.
5218 ("case expression too complex, use case statement instead", N
);
5221 Target_Typ
:= Make_Temporary
(Loc
, 'P');
5224 Make_Full_Type_Declaration
(Loc
,
5225 Defining_Identifier
=> Target_Typ
,
5227 Make_Access_To_Object_Definition
(Loc
,
5228 All_Present
=> True,
5229 Subtype_Indication
=> New_Occurrence_Of
(Typ
, Loc
))));
5232 -- Create the declaration of the target which captures the value of the
5236 -- Target : [Ptr_]Typ;
5238 if not Optimize_Return_Stmt
then
5239 Target
:= Make_Temporary
(Loc
, 'T');
5242 Make_Object_Declaration
(Loc
,
5243 Defining_Identifier
=> Target
,
5244 Object_Definition
=> New_Occurrence_Of
(Target_Typ
, Loc
));
5245 Set_No_Initialization
(Decl
);
5247 Append_To
(Acts
, Decl
);
5250 -- Process the alternatives
5252 Alt
:= First
(Alternatives
(N
));
5253 while Present
(Alt
) loop
5255 Alt_Expr
: Node_Id
:= Expression
(Alt
);
5256 Alt_Loc
: constant Source_Ptr
:= Sloc
(Alt_Expr
);
5260 -- Take the unrestricted access of the expression value for non-
5261 -- scalar types. This approach avoids big copies and covers the
5262 -- limited and unconstrained cases.
5265 -- AX'Unrestricted_Access
5267 if not Is_Copy_Type
(Typ
) then
5269 Make_Attribute_Reference
(Alt_Loc
,
5270 Prefix
=> Relocate_Node
(Alt_Expr
),
5271 Attribute_Name
=> Name_Unrestricted_Access
);
5275 -- return AX['Unrestricted_Access];
5277 if Optimize_Return_Stmt
then
5279 Make_Simple_Return_Statement
(Alt_Loc
,
5280 Expression
=> Alt_Expr
));
5283 -- Target := AX['Unrestricted_Access];
5287 Make_Assignment_Statement
(Alt_Loc
,
5288 Name
=> New_Occurrence_Of
(Target
, Loc
),
5289 Expression
=> Alt_Expr
));
5292 -- Propagate declarations inserted in the node by Insert_Actions
5293 -- (for example, temporaries generated to remove side effects).
5294 -- These actions must remain attached to the alternative, given
5295 -- that they are generated by the corresponding expression.
5297 if Present
(Actions
(Alt
)) then
5298 Prepend_List
(Actions
(Alt
), Stmts
);
5301 -- Finalize any transient objects on exit from the alternative.
5302 -- This is done only in the return optimization case because
5303 -- otherwise the case expression is converted into an expression
5304 -- with actions which already contains this form of processing.
5306 if Optimize_Return_Stmt
then
5307 Process_If_Case_Statements
(N
, Stmts
);
5311 (Alternatives
(Case_Stmt
),
5312 Make_Case_Statement_Alternative
(Sloc
(Alt
),
5313 Discrete_Choices
=> Discrete_Choices
(Alt
),
5314 Statements
=> Stmts
));
5320 -- Rewrite the parent return statement as a case statement
5322 if Optimize_Return_Stmt
then
5323 Rewrite
(Par
, Case_Stmt
);
5326 -- Otherwise convert the case expression into an expression with actions
5329 Append_To
(Acts
, Case_Stmt
);
5331 if Is_Copy_Type
(Typ
) then
5332 Expr
:= New_Occurrence_Of
(Target
, Loc
);
5336 Make_Explicit_Dereference
(Loc
,
5337 Prefix
=> New_Occurrence_Of
(Target
, Loc
));
5343 -- in Target[.all] end;
5346 Make_Expression_With_Actions
(Loc
,
5350 Analyze_And_Resolve
(N
, Typ
);
5352 end Expand_N_Case_Expression
;
5354 -----------------------------------
5355 -- Expand_N_Explicit_Dereference --
5356 -----------------------------------
5358 procedure Expand_N_Explicit_Dereference
(N
: Node_Id
) is
5360 -- Insert explicit dereference call for the checked storage pool case
5362 Insert_Dereference_Action
(Prefix
(N
));
5364 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5365 -- we set the atomic sync flag.
5367 if Is_Atomic
(Etype
(N
))
5368 and then not Atomic_Synchronization_Disabled
(Etype
(N
))
5370 Activate_Atomic_Synchronization
(N
);
5372 end Expand_N_Explicit_Dereference
;
5374 --------------------------------------
5375 -- Expand_N_Expression_With_Actions --
5376 --------------------------------------
5378 procedure Expand_N_Expression_With_Actions
(N
: Node_Id
) is
5379 Acts
: constant List_Id
:= Actions
(N
);
5381 procedure Force_Boolean_Evaluation
(Expr
: Node_Id
);
5382 -- Force the evaluation of Boolean expression Expr
5384 function Process_Action
(Act
: Node_Id
) return Traverse_Result
;
5385 -- Inspect and process a single action of an expression_with_actions for
5386 -- transient objects. If such objects are found, the routine generates
5387 -- code to clean them up when the context of the expression is evaluated
5390 ------------------------------
5391 -- Force_Boolean_Evaluation --
5392 ------------------------------
5394 procedure Force_Boolean_Evaluation
(Expr
: Node_Id
) is
5395 Loc
: constant Source_Ptr
:= Sloc
(N
);
5396 Flag_Decl
: Node_Id
;
5397 Flag_Id
: Entity_Id
;
5400 -- Relocate the expression to the actions list by capturing its value
5401 -- in a Boolean flag. Generate:
5402 -- Flag : constant Boolean := Expr;
5404 Flag_Id
:= Make_Temporary
(Loc
, 'F');
5407 Make_Object_Declaration
(Loc
,
5408 Defining_Identifier
=> Flag_Id
,
5409 Constant_Present
=> True,
5410 Object_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
),
5411 Expression
=> Relocate_Node
(Expr
));
5413 Append
(Flag_Decl
, Acts
);
5414 Analyze
(Flag_Decl
);
5416 -- Replace the expression with a reference to the flag
5418 Rewrite
(Expression
(N
), New_Occurrence_Of
(Flag_Id
, Loc
));
5419 Analyze
(Expression
(N
));
5420 end Force_Boolean_Evaluation
;
5422 --------------------
5423 -- Process_Action --
5424 --------------------
5426 function Process_Action
(Act
: Node_Id
) return Traverse_Result
is
5428 if Nkind
(Act
) = N_Object_Declaration
5429 and then Is_Finalizable_Transient
(Act
, N
)
5431 Process_Transient_In_Expression
(Act
, N
, Acts
);
5434 -- Avoid processing temporary function results multiple times when
5435 -- dealing with nested expression_with_actions.
5437 elsif Nkind
(Act
) = N_Expression_With_Actions
then
5440 -- Do not process temporary function results in loops. This is done
5441 -- by Expand_N_Loop_Statement and Build_Finalizer.
5443 elsif Nkind
(Act
) = N_Loop_Statement
then
5450 procedure Process_Single_Action
is new Traverse_Proc
(Process_Action
);
5456 -- Start of processing for Expand_N_Expression_With_Actions
5459 -- Do not evaluate the expression when it denotes an entity because the
5460 -- expression_with_actions node will be replaced by the reference.
5462 if Is_Entity_Name
(Expression
(N
)) then
5465 -- Do not evaluate the expression when there are no actions because the
5466 -- expression_with_actions node will be replaced by the expression.
5468 elsif No
(Acts
) or else Is_Empty_List
(Acts
) then
5471 -- Force the evaluation of the expression by capturing its value in a
5472 -- temporary. This ensures that aliases of transient objects do not leak
5473 -- to the expression of the expression_with_actions node:
5476 -- Trans_Id : Ctrl_Typ := ...;
5477 -- Alias : ... := Trans_Id;
5478 -- in ... Alias ... end;
5480 -- In the example above, Trans_Id cannot be finalized at the end of the
5481 -- actions list because this may affect the alias and the final value of
5482 -- the expression_with_actions. Forcing the evaluation encapsulates the
5483 -- reference to the Alias within the actions list:
5486 -- Trans_Id : Ctrl_Typ := ...;
5487 -- Alias : ... := Trans_Id;
5488 -- Val : constant Boolean := ... Alias ...;
5489 -- <finalize Trans_Id>
5492 -- Once this transformation is performed, it is safe to finalize the
5493 -- transient object at the end of the actions list.
5495 -- Note that Force_Evaluation does not remove side effects in operators
5496 -- because it assumes that all operands are evaluated and side effect
5497 -- free. This is not the case when an operand depends implicitly on the
5498 -- transient object through the use of access types.
5500 elsif Is_Boolean_Type
(Etype
(Expression
(N
))) then
5501 Force_Boolean_Evaluation
(Expression
(N
));
5503 -- The expression of an expression_with_actions node may not necessarily
5504 -- be Boolean when the node appears in an if expression. In this case do
5505 -- the usual forced evaluation to encapsulate potential aliasing.
5508 Force_Evaluation
(Expression
(N
));
5511 -- Process all transient objects found within the actions of the EWA
5514 Act
:= First
(Acts
);
5515 while Present
(Act
) loop
5516 Process_Single_Action
(Act
);
5520 -- Deal with case where there are no actions. In this case we simply
5521 -- rewrite the node with its expression since we don't need the actions
5522 -- and the specification of this node does not allow a null action list.
5524 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5525 -- the expanded tree and relying on being able to retrieve the original
5526 -- tree in cases like this. This raises a whole lot of issues of whether
5527 -- we have problems elsewhere, which will be addressed in the future???
5529 if Is_Empty_List
(Acts
) then
5530 Rewrite
(N
, Relocate_Node
(Expression
(N
)));
5532 end Expand_N_Expression_With_Actions
;
5534 ----------------------------
5535 -- Expand_N_If_Expression --
5536 ----------------------------
5538 -- Deal with limited types and condition actions
5540 procedure Expand_N_If_Expression
(N
: Node_Id
) is
5541 Cond
: constant Node_Id
:= First
(Expressions
(N
));
5542 Loc
: constant Source_Ptr
:= Sloc
(N
);
5543 Thenx
: constant Node_Id
:= Next
(Cond
);
5544 Elsex
: constant Node_Id
:= Next
(Thenx
);
5545 Typ
: constant Entity_Id
:= Etype
(N
);
5554 -- Check for MINIMIZED/ELIMINATED overflow mode
5556 if Minimized_Eliminated_Overflow_Check
(N
) then
5557 Apply_Arithmetic_Overflow_Check
(N
);
5561 -- Fold at compile time if condition known. We have already folded
5562 -- static if expressions, but it is possible to fold any case in which
5563 -- the condition is known at compile time, even though the result is
5566 -- Note that we don't do the fold of such cases in Sem_Elab because
5567 -- it can cause infinite loops with the expander adding a conditional
5568 -- expression, and Sem_Elab circuitry removing it repeatedly.
5570 if Compile_Time_Known_Value
(Cond
) then
5572 function Fold_Known_Value
(Cond
: Node_Id
) return Boolean;
5573 -- Fold at compile time. Assumes condition known. Return True if
5574 -- folding occurred, meaning we're done.
5576 ----------------------
5577 -- Fold_Known_Value --
5578 ----------------------
5580 function Fold_Known_Value
(Cond
: Node_Id
) return Boolean is
5582 if Is_True
(Expr_Value
(Cond
)) then
5584 Actions
:= Then_Actions
(N
);
5587 Actions
:= Else_Actions
(N
);
5592 if Present
(Actions
) then
5594 -- To minimize the use of Expression_With_Actions, just skip
5595 -- the optimization as it is not critical for correctness.
5597 if Minimize_Expression_With_Actions
then
5602 Make_Expression_With_Actions
(Loc
,
5603 Expression
=> Relocate_Node
(Expr
),
5604 Actions
=> Actions
));
5605 Analyze_And_Resolve
(N
, Typ
);
5608 Rewrite
(N
, Relocate_Node
(Expr
));
5611 -- Note that the result is never static (legitimate cases of
5612 -- static if expressions were folded in Sem_Eval).
5614 Set_Is_Static_Expression
(N
, False);
5616 end Fold_Known_Value
;
5619 if Fold_Known_Value
(Cond
) then
5625 -- If the type is limited, and the back end does not handle limited
5626 -- types, then we expand as follows to avoid the possibility of
5627 -- improper copying.
5629 -- type Ptr is access all Typ;
5633 -- Cnn := then-expr'Unrestricted_Access;
5636 -- Cnn := else-expr'Unrestricted_Access;
5639 -- and replace the if expression by a reference to Cnn.all.
5641 -- This special case can be skipped if the back end handles limited
5642 -- types properly and ensures that no incorrect copies are made.
5644 if Is_By_Reference_Type
(Typ
)
5645 and then not Back_End_Handles_Limited_Types
5647 -- When the "then" or "else" expressions involve controlled function
5648 -- calls, generated temporaries are chained on the corresponding list
5649 -- of actions. These temporaries need to be finalized after the if
5650 -- expression is evaluated.
5652 Process_If_Case_Statements
(N
, Then_Actions
(N
));
5653 Process_If_Case_Statements
(N
, Else_Actions
(N
));
5656 Cnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'C', N
);
5657 Ptr_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
5661 -- type Ann is access all Typ;
5664 Make_Full_Type_Declaration
(Loc
,
5665 Defining_Identifier
=> Ptr_Typ
,
5667 Make_Access_To_Object_Definition
(Loc
,
5668 All_Present
=> True,
5669 Subtype_Indication
=> New_Occurrence_Of
(Typ
, Loc
))));
5675 Make_Object_Declaration
(Loc
,
5676 Defining_Identifier
=> Cnn
,
5677 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
));
5681 -- Cnn := <Thenx>'Unrestricted_Access;
5683 -- Cnn := <Elsex>'Unrestricted_Access;
5687 Make_Implicit_If_Statement
(N
,
5688 Condition
=> Relocate_Node
(Cond
),
5689 Then_Statements
=> New_List
(
5690 Make_Assignment_Statement
(Sloc
(Thenx
),
5691 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Thenx
)),
5693 Make_Attribute_Reference
(Loc
,
5694 Prefix
=> Relocate_Node
(Thenx
),
5695 Attribute_Name
=> Name_Unrestricted_Access
))),
5697 Else_Statements
=> New_List
(
5698 Make_Assignment_Statement
(Sloc
(Elsex
),
5699 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Elsex
)),
5701 Make_Attribute_Reference
(Loc
,
5702 Prefix
=> Relocate_Node
(Elsex
),
5703 Attribute_Name
=> Name_Unrestricted_Access
))));
5705 -- Preserve the original context for which the if statement is
5706 -- being generated. This is needed by the finalization machinery
5707 -- to prevent the premature finalization of controlled objects
5708 -- found within the if statement.
5710 Set_From_Conditional_Expression
(New_If
);
5713 Make_Explicit_Dereference
(Loc
,
5714 Prefix
=> New_Occurrence_Of
(Cnn
, Loc
));
5717 -- If the result is an unconstrained array and the if expression is in a
5718 -- context other than the initializing expression of the declaration of
5719 -- an object, then we pull out the if expression as follows:
5721 -- Cnn : constant typ := if-expression
5723 -- and then replace the if expression with an occurrence of Cnn. This
5724 -- avoids the need in the back end to create on-the-fly variable length
5725 -- temporaries (which it cannot do!)
5727 -- Note that the test for being in an object declaration avoids doing an
5728 -- unnecessary expansion, and also avoids infinite recursion.
5730 elsif Is_Array_Type
(Typ
) and then not Is_Constrained
(Typ
)
5731 and then (Nkind
(Parent
(N
)) /= N_Object_Declaration
5732 or else Expression
(Parent
(N
)) /= N
)
5735 Cnn
: constant Node_Id
:= Make_Temporary
(Loc
, 'C', N
);
5739 Make_Object_Declaration
(Loc
,
5740 Defining_Identifier
=> Cnn
,
5741 Constant_Present
=> True,
5742 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
5743 Expression
=> Relocate_Node
(N
),
5744 Has_Init_Expression
=> True));
5746 Rewrite
(N
, New_Occurrence_Of
(Cnn
, Loc
));
5750 -- For other types, we only need to expand if there are other actions
5751 -- associated with either branch.
5753 elsif Present
(Then_Actions
(N
)) or else Present
(Else_Actions
(N
)) then
5755 -- We now wrap the actions into the appropriate expression
5757 if Minimize_Expression_With_Actions
5758 and then (Is_Elementary_Type
(Underlying_Type
(Typ
))
5759 or else Is_Constrained
(Underlying_Type
(Typ
)))
5761 -- If we can't use N_Expression_With_Actions nodes, then we insert
5762 -- the following sequence of actions (using Insert_Actions):
5767 -- Cnn := then-expr;
5773 -- and replace the if expression by a reference to Cnn
5776 Cnn
: constant Node_Id
:= Make_Temporary
(Loc
, 'C', N
);
5780 Make_Object_Declaration
(Loc
,
5781 Defining_Identifier
=> Cnn
,
5782 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
5785 Make_Implicit_If_Statement
(N
,
5786 Condition
=> Relocate_Node
(Cond
),
5788 Then_Statements
=> New_List
(
5789 Make_Assignment_Statement
(Sloc
(Thenx
),
5790 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Thenx
)),
5791 Expression
=> Relocate_Node
(Thenx
))),
5793 Else_Statements
=> New_List
(
5794 Make_Assignment_Statement
(Sloc
(Elsex
),
5795 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Elsex
)),
5796 Expression
=> Relocate_Node
(Elsex
))));
5798 Set_Assignment_OK
(Name
(First
(Then_Statements
(New_If
))));
5799 Set_Assignment_OK
(Name
(First
(Else_Statements
(New_If
))));
5801 New_N
:= New_Occurrence_Of
(Cnn
, Loc
);
5804 -- Regular path using Expression_With_Actions
5807 if Present
(Then_Actions
(N
)) then
5809 Make_Expression_With_Actions
(Sloc
(Thenx
),
5810 Actions
=> Then_Actions
(N
),
5811 Expression
=> Relocate_Node
(Thenx
)));
5813 Set_Then_Actions
(N
, No_List
);
5814 Analyze_And_Resolve
(Thenx
, Typ
);
5817 if Present
(Else_Actions
(N
)) then
5819 Make_Expression_With_Actions
(Sloc
(Elsex
),
5820 Actions
=> Else_Actions
(N
),
5821 Expression
=> Relocate_Node
(Elsex
)));
5823 Set_Else_Actions
(N
, No_List
);
5824 Analyze_And_Resolve
(Elsex
, Typ
);
5830 -- If no actions then no expansion needed, gigi will handle it using the
5831 -- same approach as a C conditional expression.
5837 -- Fall through here for either the limited expansion, or the case of
5838 -- inserting actions for nonlimited types. In both these cases, we must
5839 -- move the SLOC of the parent If statement to the newly created one and
5840 -- change it to the SLOC of the expression which, after expansion, will
5841 -- correspond to what is being evaluated.
5843 if Present
(Parent
(N
)) and then Nkind
(Parent
(N
)) = N_If_Statement
then
5844 Set_Sloc
(New_If
, Sloc
(Parent
(N
)));
5845 Set_Sloc
(Parent
(N
), Loc
);
5848 -- Make sure Then_Actions and Else_Actions are appropriately moved
5849 -- to the new if statement.
5851 if Present
(Then_Actions
(N
)) then
5853 (First
(Then_Statements
(New_If
)), Then_Actions
(N
));
5856 if Present
(Else_Actions
(N
)) then
5858 (First
(Else_Statements
(New_If
)), Else_Actions
(N
));
5861 Insert_Action
(N
, Decl
);
5862 Insert_Action
(N
, New_If
);
5864 Analyze_And_Resolve
(N
, Typ
);
5865 end Expand_N_If_Expression
;
5871 procedure Expand_N_In
(N
: Node_Id
) is
5872 Loc
: constant Source_Ptr
:= Sloc
(N
);
5873 Restyp
: constant Entity_Id
:= Etype
(N
);
5874 Lop
: constant Node_Id
:= Left_Opnd
(N
);
5875 Rop
: constant Node_Id
:= Right_Opnd
(N
);
5876 Static
: constant Boolean := Is_OK_Static_Expression
(N
);
5878 procedure Substitute_Valid_Check
;
5879 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5880 -- test for the left operand being in range of its subtype.
5882 ----------------------------
5883 -- Substitute_Valid_Check --
5884 ----------------------------
5886 procedure Substitute_Valid_Check
is
5887 function Is_OK_Object_Reference
(Nod
: Node_Id
) return Boolean;
5888 -- Determine whether arbitrary node Nod denotes a source object that
5889 -- may safely act as prefix of attribute 'Valid.
5891 ----------------------------
5892 -- Is_OK_Object_Reference --
5893 ----------------------------
5895 function Is_OK_Object_Reference
(Nod
: Node_Id
) return Boolean is
5899 -- Inspect the original operand
5901 Obj_Ref
:= Original_Node
(Nod
);
5903 -- The object reference must be a source construct, otherwise the
5904 -- codefix suggestion may refer to nonexistent code from a user
5907 if Comes_From_Source
(Obj_Ref
) then
5909 -- Recover the actual object reference. There may be more cases
5913 if Nkind_In
(Obj_Ref
, N_Type_Conversion
,
5914 N_Unchecked_Type_Conversion
)
5916 Obj_Ref
:= Expression
(Obj_Ref
);
5922 return Is_Object_Reference
(Obj_Ref
);
5926 end Is_OK_Object_Reference
;
5928 -- Start of processing for Substitute_Valid_Check
5932 Make_Attribute_Reference
(Loc
,
5933 Prefix
=> Relocate_Node
(Lop
),
5934 Attribute_Name
=> Name_Valid
));
5936 Analyze_And_Resolve
(N
, Restyp
);
5938 -- Emit a warning when the left-hand operand of the membership test
5939 -- is a source object, otherwise the use of attribute 'Valid would be
5940 -- illegal. The warning is not given when overflow checking is either
5941 -- MINIMIZED or ELIMINATED, as the danger of optimization has been
5942 -- eliminated above.
5944 if Is_OK_Object_Reference
(Lop
)
5945 and then Overflow_Check_Mode
not in Minimized_Or_Eliminated
5948 ("??explicit membership test may be optimized away", N
);
5949 Error_Msg_N
-- CODEFIX
5950 ("\??use ''Valid attribute instead", N
);
5952 end Substitute_Valid_Check
;
5959 -- Start of processing for Expand_N_In
5962 -- If set membership case, expand with separate procedure
5964 if Present
(Alternatives
(N
)) then
5965 Expand_Set_Membership
(N
);
5969 -- Not set membership, proceed with expansion
5971 Ltyp
:= Etype
(Left_Opnd
(N
));
5972 Rtyp
:= Etype
(Right_Opnd
(N
));
5974 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
5975 -- type, then expand with a separate procedure. Note the use of the
5976 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5978 if Overflow_Check_Mode
in Minimized_Or_Eliminated
5979 and then Is_Signed_Integer_Type
(Ltyp
)
5980 and then not No_Minimize_Eliminate
(N
)
5982 Expand_Membership_Minimize_Eliminate_Overflow
(N
);
5986 -- Check case of explicit test for an expression in range of its
5987 -- subtype. This is suspicious usage and we replace it with a 'Valid
5988 -- test and give a warning for scalar types.
5990 if Is_Scalar_Type
(Ltyp
)
5992 -- Only relevant for source comparisons
5994 and then Comes_From_Source
(N
)
5996 -- In floating-point this is a standard way to check for finite values
5997 -- and using 'Valid would typically be a pessimization.
5999 and then not Is_Floating_Point_Type
(Ltyp
)
6001 -- Don't give the message unless right operand is a type entity and
6002 -- the type of the left operand matches this type. Note that this
6003 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
6004 -- checks have changed the type of the left operand.
6006 and then Nkind
(Rop
) in N_Has_Entity
6007 and then Ltyp
= Entity
(Rop
)
6009 -- Skip this for predicated types, where such expressions are a
6010 -- reasonable way of testing if something meets the predicate.
6012 and then not Present
(Predicate_Function
(Ltyp
))
6014 Substitute_Valid_Check
;
6018 -- Do validity check on operands
6020 if Validity_Checks_On
and Validity_Check_Operands
then
6021 Ensure_Valid
(Left_Opnd
(N
));
6022 Validity_Check_Range
(Right_Opnd
(N
));
6025 -- Case of explicit range
6027 if Nkind
(Rop
) = N_Range
then
6029 Lo
: constant Node_Id
:= Low_Bound
(Rop
);
6030 Hi
: constant Node_Id
:= High_Bound
(Rop
);
6032 Lo_Orig
: constant Node_Id
:= Original_Node
(Lo
);
6033 Hi_Orig
: constant Node_Id
:= Original_Node
(Hi
);
6035 Lcheck
: Compare_Result
;
6036 Ucheck
: Compare_Result
;
6038 Warn1
: constant Boolean :=
6039 Constant_Condition_Warnings
6040 and then Comes_From_Source
(N
)
6041 and then not In_Instance
;
6042 -- This must be true for any of the optimization warnings, we
6043 -- clearly want to give them only for source with the flag on. We
6044 -- also skip these warnings in an instance since it may be the
6045 -- case that different instantiations have different ranges.
6047 Warn2
: constant Boolean :=
6049 and then Nkind
(Original_Node
(Rop
)) = N_Range
6050 and then Is_Integer_Type
(Etype
(Lo
));
6051 -- For the case where only one bound warning is elided, we also
6052 -- insist on an explicit range and an integer type. The reason is
6053 -- that the use of enumeration ranges including an end point is
6054 -- common, as is the use of a subtype name, one of whose bounds is
6055 -- the same as the type of the expression.
6058 -- If test is explicit x'First .. x'Last, replace by valid check
6060 -- Could use some individual comments for this complex test ???
6062 if Is_Scalar_Type
(Ltyp
)
6064 -- And left operand is X'First where X matches left operand
6065 -- type (this eliminates cases of type mismatch, including
6066 -- the cases where ELIMINATED/MINIMIZED mode has changed the
6067 -- type of the left operand.
6069 and then Nkind
(Lo_Orig
) = N_Attribute_Reference
6070 and then Attribute_Name
(Lo_Orig
) = Name_First
6071 and then Nkind
(Prefix
(Lo_Orig
)) in N_Has_Entity
6072 and then Entity
(Prefix
(Lo_Orig
)) = Ltyp
6074 -- Same tests for right operand
6076 and then Nkind
(Hi_Orig
) = N_Attribute_Reference
6077 and then Attribute_Name
(Hi_Orig
) = Name_Last
6078 and then Nkind
(Prefix
(Hi_Orig
)) in N_Has_Entity
6079 and then Entity
(Prefix
(Hi_Orig
)) = Ltyp
6081 -- Relevant only for source cases
6083 and then Comes_From_Source
(N
)
6085 Substitute_Valid_Check
;
6089 -- If bounds of type are known at compile time, and the end points
6090 -- are known at compile time and identical, this is another case
6091 -- for substituting a valid test. We only do this for discrete
6092 -- types, since it won't arise in practice for float types.
6094 if Comes_From_Source
(N
)
6095 and then Is_Discrete_Type
(Ltyp
)
6096 and then Compile_Time_Known_Value
(Type_High_Bound
(Ltyp
))
6097 and then Compile_Time_Known_Value
(Type_Low_Bound
(Ltyp
))
6098 and then Compile_Time_Known_Value
(Lo
)
6099 and then Compile_Time_Known_Value
(Hi
)
6100 and then Expr_Value
(Type_High_Bound
(Ltyp
)) = Expr_Value
(Hi
)
6101 and then Expr_Value
(Type_Low_Bound
(Ltyp
)) = Expr_Value
(Lo
)
6103 -- Kill warnings in instances, since they may be cases where we
6104 -- have a test in the generic that makes sense with some types
6105 -- and not with other types.
6107 -- Similarly, do not rewrite membership as a validity check if
6108 -- within the predicate function for the type.
6112 or else (Ekind
(Current_Scope
) = E_Function
6113 and then Is_Predicate_Function
(Current_Scope
))
6118 Substitute_Valid_Check
;
6123 -- If we have an explicit range, do a bit of optimization based on
6124 -- range analysis (we may be able to kill one or both checks).
6126 Lcheck
:= Compile_Time_Compare
(Lop
, Lo
, Assume_Valid
=> False);
6127 Ucheck
:= Compile_Time_Compare
(Lop
, Hi
, Assume_Valid
=> False);
6129 -- If either check is known to fail, replace result by False since
6130 -- the other check does not matter. Preserve the static flag for
6131 -- legality checks, because we are constant-folding beyond RM 4.9.
6133 if Lcheck
= LT
or else Ucheck
= GT
then
6135 Error_Msg_N
("?c?range test optimized away", N
);
6136 Error_Msg_N
("\?c?value is known to be out of range", N
);
6139 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
6140 Analyze_And_Resolve
(N
, Restyp
);
6141 Set_Is_Static_Expression
(N
, Static
);
6144 -- If both checks are known to succeed, replace result by True,
6145 -- since we know we are in range.
6147 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
6149 Error_Msg_N
("?c?range test optimized away", N
);
6150 Error_Msg_N
("\?c?value is known to be in range", N
);
6153 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
6154 Analyze_And_Resolve
(N
, Restyp
);
6155 Set_Is_Static_Expression
(N
, Static
);
6158 -- If lower bound check succeeds and upper bound check is not
6159 -- known to succeed or fail, then replace the range check with
6160 -- a comparison against the upper bound.
6162 elsif Lcheck
in Compare_GE
then
6163 if Warn2
and then not In_Instance
then
6164 Error_Msg_N
("??lower bound test optimized away", Lo
);
6165 Error_Msg_N
("\??value is known to be in range", Lo
);
6171 Right_Opnd
=> High_Bound
(Rop
)));
6172 Analyze_And_Resolve
(N
, Restyp
);
6175 -- If upper bound check succeeds and lower bound check is not
6176 -- known to succeed or fail, then replace the range check with
6177 -- a comparison against the lower bound.
6179 elsif Ucheck
in Compare_LE
then
6180 if Warn2
and then not In_Instance
then
6181 Error_Msg_N
("??upper bound test optimized away", Hi
);
6182 Error_Msg_N
("\??value is known to be in range", Hi
);
6188 Right_Opnd
=> Low_Bound
(Rop
)));
6189 Analyze_And_Resolve
(N
, Restyp
);
6193 -- We couldn't optimize away the range check, but there is one
6194 -- more issue. If we are checking constant conditionals, then we
6195 -- see if we can determine the outcome assuming everything is
6196 -- valid, and if so give an appropriate warning.
6198 if Warn1
and then not Assume_No_Invalid_Values
then
6199 Lcheck
:= Compile_Time_Compare
(Lop
, Lo
, Assume_Valid
=> True);
6200 Ucheck
:= Compile_Time_Compare
(Lop
, Hi
, Assume_Valid
=> True);
6202 -- Result is out of range for valid value
6204 if Lcheck
= LT
or else Ucheck
= GT
then
6206 ("?c?value can only be in range if it is invalid", N
);
6208 -- Result is in range for valid value
6210 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
6212 ("?c?value can only be out of range if it is invalid", N
);
6214 -- Lower bound check succeeds if value is valid
6216 elsif Warn2
and then Lcheck
in Compare_GE
then
6218 ("?c?lower bound check only fails if it is invalid", Lo
);
6220 -- Upper bound check succeeds if value is valid
6222 elsif Warn2
and then Ucheck
in Compare_LE
then
6224 ("?c?upper bound check only fails for invalid values", Hi
);
6229 -- For all other cases of an explicit range, nothing to be done
6233 -- Here right operand is a subtype mark
6237 Typ
: Entity_Id
:= Etype
(Rop
);
6238 Is_Acc
: constant Boolean := Is_Access_Type
(Typ
);
6239 Cond
: Node_Id
:= Empty
;
6241 Obj
: Node_Id
:= Lop
;
6242 SCIL_Node
: Node_Id
;
6245 Remove_Side_Effects
(Obj
);
6247 -- For tagged type, do tagged membership operation
6249 if Is_Tagged_Type
(Typ
) then
6251 -- No expansion will be performed for VM targets, as the VM
6252 -- back ends will handle the membership tests directly.
6254 if Tagged_Type_Expansion
then
6255 Tagged_Membership
(N
, SCIL_Node
, New_N
);
6257 Analyze_And_Resolve
(N
, Restyp
, Suppress
=> All_Checks
);
6259 -- Update decoration of relocated node referenced by the
6262 if Generate_SCIL
and then Present
(SCIL_Node
) then
6263 Set_SCIL_Node
(N
, SCIL_Node
);
6269 -- If type is scalar type, rewrite as x in t'First .. t'Last.
6270 -- This reason we do this is that the bounds may have the wrong
6271 -- type if they come from the original type definition. Also this
6272 -- way we get all the processing above for an explicit range.
6274 -- Don't do this for predicated types, since in this case we
6275 -- want to check the predicate.
6277 elsif Is_Scalar_Type
(Typ
) then
6278 if No
(Predicate_Function
(Typ
)) then
6282 Make_Attribute_Reference
(Loc
,
6283 Attribute_Name
=> Name_First
,
6284 Prefix
=> New_Occurrence_Of
(Typ
, Loc
)),
6287 Make_Attribute_Reference
(Loc
,
6288 Attribute_Name
=> Name_Last
,
6289 Prefix
=> New_Occurrence_Of
(Typ
, Loc
))));
6290 Analyze_And_Resolve
(N
, Restyp
);
6295 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
6296 -- a membership test if the subtype mark denotes a constrained
6297 -- Unchecked_Union subtype and the expression lacks inferable
6300 elsif Is_Unchecked_Union
(Base_Type
(Typ
))
6301 and then Is_Constrained
(Typ
)
6302 and then not Has_Inferable_Discriminants
(Lop
)
6305 Make_Raise_Program_Error
(Loc
,
6306 Reason
=> PE_Unchecked_Union_Restriction
));
6308 -- Prevent Gigi from generating incorrect code by rewriting the
6309 -- test as False. What is this undocumented thing about ???
6311 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
6315 -- Here we have a non-scalar type
6318 Typ
:= Designated_Type
(Typ
);
6321 if not Is_Constrained
(Typ
) then
6322 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
6323 Analyze_And_Resolve
(N
, Restyp
);
6325 -- For the constrained array case, we have to check the subscripts
6326 -- for an exact match if the lengths are non-zero (the lengths
6327 -- must match in any case).
6329 elsif Is_Array_Type
(Typ
) then
6330 Check_Subscripts
: declare
6331 function Build_Attribute_Reference
6334 Dim
: Nat
) return Node_Id
;
6335 -- Build attribute reference E'Nam (Dim)
6337 -------------------------------
6338 -- Build_Attribute_Reference --
6339 -------------------------------
6341 function Build_Attribute_Reference
6344 Dim
: Nat
) return Node_Id
6348 Make_Attribute_Reference
(Loc
,
6350 Attribute_Name
=> Nam
,
6351 Expressions
=> New_List
(
6352 Make_Integer_Literal
(Loc
, Dim
)));
6353 end Build_Attribute_Reference
;
6355 -- Start of processing for Check_Subscripts
6358 for J
in 1 .. Number_Dimensions
(Typ
) loop
6359 Evolve_And_Then
(Cond
,
6362 Build_Attribute_Reference
6363 (Duplicate_Subexpr_No_Checks
(Obj
),
6366 Build_Attribute_Reference
6367 (New_Occurrence_Of
(Typ
, Loc
), Name_First
, J
)));
6369 Evolve_And_Then
(Cond
,
6372 Build_Attribute_Reference
6373 (Duplicate_Subexpr_No_Checks
(Obj
),
6376 Build_Attribute_Reference
6377 (New_Occurrence_Of
(Typ
, Loc
), Name_Last
, J
)));
6386 Right_Opnd
=> Make_Null
(Loc
)),
6387 Right_Opnd
=> Cond
);
6391 Analyze_And_Resolve
(N
, Restyp
);
6392 end Check_Subscripts
;
6394 -- These are the cases where constraint checks may be required,
6395 -- e.g. records with possible discriminants
6398 -- Expand the test into a series of discriminant comparisons.
6399 -- The expression that is built is the negation of the one that
6400 -- is used for checking discriminant constraints.
6402 Obj
:= Relocate_Node
(Left_Opnd
(N
));
6404 if Has_Discriminants
(Typ
) then
6405 Cond
:= Make_Op_Not
(Loc
,
6406 Right_Opnd
=> Build_Discriminant_Checks
(Obj
, Typ
));
6409 Cond
:= Make_Or_Else
(Loc
,
6413 Right_Opnd
=> Make_Null
(Loc
)),
6414 Right_Opnd
=> Cond
);
6418 Cond
:= New_Occurrence_Of
(Standard_True
, Loc
);
6422 Analyze_And_Resolve
(N
, Restyp
);
6425 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
6426 -- expression of an anonymous access type. This can involve an
6427 -- accessibility test and a tagged type membership test in the
6428 -- case of tagged designated types.
6430 if Ada_Version
>= Ada_2012
6432 and then Ekind
(Ltyp
) = E_Anonymous_Access_Type
6435 Expr_Entity
: Entity_Id
:= Empty
;
6437 Param_Level
: Node_Id
;
6438 Type_Level
: Node_Id
;
6441 if Is_Entity_Name
(Lop
) then
6442 Expr_Entity
:= Param_Entity
(Lop
);
6444 if not Present
(Expr_Entity
) then
6445 Expr_Entity
:= Entity
(Lop
);
6449 -- If a conversion of the anonymous access value to the
6450 -- tested type would be illegal, then the result is False.
6452 if not Valid_Conversion
6453 (Lop
, Rtyp
, Lop
, Report_Errs
=> False)
6455 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
6456 Analyze_And_Resolve
(N
, Restyp
);
6458 -- Apply an accessibility check if the access object has an
6459 -- associated access level and when the level of the type is
6460 -- less deep than the level of the access parameter. This
6461 -- only occur for access parameters and stand-alone objects
6462 -- of an anonymous access type.
6465 if Present
(Expr_Entity
)
6468 (Effective_Extra_Accessibility
(Expr_Entity
))
6469 and then UI_Gt
(Object_Access_Level
(Lop
),
6470 Type_Access_Level
(Rtyp
))
6474 (Effective_Extra_Accessibility
(Expr_Entity
), Loc
);
6477 Make_Integer_Literal
(Loc
, Type_Access_Level
(Rtyp
));
6479 -- Return True only if the accessibility level of the
6480 -- expression entity is not deeper than the level of
6481 -- the tested access type.
6485 Left_Opnd
=> Relocate_Node
(N
),
6486 Right_Opnd
=> Make_Op_Le
(Loc
,
6487 Left_Opnd
=> Param_Level
,
6488 Right_Opnd
=> Type_Level
)));
6490 Analyze_And_Resolve
(N
);
6493 -- If the designated type is tagged, do tagged membership
6496 -- *** NOTE: we have to check not null before doing the
6497 -- tagged membership test (but maybe that can be done
6498 -- inside Tagged_Membership?).
6500 if Is_Tagged_Type
(Typ
) then
6503 Left_Opnd
=> Relocate_Node
(N
),
6507 Right_Opnd
=> Make_Null
(Loc
))));
6509 -- No expansion will be performed for VM targets, as
6510 -- the VM back ends will handle the membership tests
6513 if Tagged_Type_Expansion
then
6515 -- Note that we have to pass Original_Node, because
6516 -- the membership test might already have been
6517 -- rewritten by earlier parts of membership test.
6520 (Original_Node
(N
), SCIL_Node
, New_N
);
6522 -- Update decoration of relocated node referenced
6523 -- by the SCIL node.
6525 if Generate_SCIL
and then Present
(SCIL_Node
) then
6526 Set_SCIL_Node
(New_N
, SCIL_Node
);
6531 Left_Opnd
=> Relocate_Node
(N
),
6532 Right_Opnd
=> New_N
));
6534 Analyze_And_Resolve
(N
, Restyp
);
6543 -- At this point, we have done the processing required for the basic
6544 -- membership test, but not yet dealt with the predicate.
6548 -- If a predicate is present, then we do the predicate test, but we
6549 -- most certainly want to omit this if we are within the predicate
6550 -- function itself, since otherwise we have an infinite recursion.
6551 -- The check should also not be emitted when testing against a range
6552 -- (the check is only done when the right operand is a subtype; see
6553 -- RM12-4.5.2 (28.1/3-30/3)).
6555 Predicate_Check
: declare
6556 function In_Range_Check
return Boolean;
6557 -- Within an expanded range check that may raise Constraint_Error do
6558 -- not generate a predicate check as well. It is redundant because
6559 -- the context will add an explicit predicate check, and it will
6560 -- raise the wrong exception if it fails.
6562 --------------------
6563 -- In_Range_Check --
6564 --------------------
6566 function In_Range_Check
return Boolean is
6570 while Present
(P
) loop
6571 if Nkind
(P
) = N_Raise_Constraint_Error
then
6574 elsif Nkind
(P
) in N_Statement_Other_Than_Procedure_Call
6575 or else Nkind
(P
) = N_Procedure_Call_Statement
6576 or else Nkind
(P
) in N_Declaration
6589 PFunc
: constant Entity_Id
:= Predicate_Function
(Rtyp
);
6592 -- Start of processing for Predicate_Check
6596 and then Current_Scope
/= PFunc
6597 and then Nkind
(Rop
) /= N_Range
6599 if not In_Range_Check
then
6600 R_Op
:= Make_Predicate_Call
(Rtyp
, Lop
, Mem
=> True);
6602 R_Op
:= New_Occurrence_Of
(Standard_True
, Loc
);
6607 Left_Opnd
=> Relocate_Node
(N
),
6608 Right_Opnd
=> R_Op
));
6610 -- Analyze new expression, mark left operand as analyzed to
6611 -- avoid infinite recursion adding predicate calls. Similarly,
6612 -- suppress further range checks on the call.
6614 Set_Analyzed
(Left_Opnd
(N
));
6615 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
6617 -- All done, skip attempt at compile time determination of result
6621 end Predicate_Check
;
6624 --------------------------------
6625 -- Expand_N_Indexed_Component --
6626 --------------------------------
6628 procedure Expand_N_Indexed_Component
(N
: Node_Id
) is
6629 Loc
: constant Source_Ptr
:= Sloc
(N
);
6630 Typ
: constant Entity_Id
:= Etype
(N
);
6631 P
: constant Node_Id
:= Prefix
(N
);
6632 T
: constant Entity_Id
:= Etype
(P
);
6636 -- A special optimization, if we have an indexed component that is
6637 -- selecting from a slice, then we can eliminate the slice, since, for
6638 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6639 -- the range check required by the slice. The range check for the slice
6640 -- itself has already been generated. The range check for the
6641 -- subscripting operation is ensured by converting the subject to
6642 -- the subtype of the slice.
6644 -- This optimization not only generates better code, avoiding slice
6645 -- messing especially in the packed case, but more importantly bypasses
6646 -- some problems in handling this peculiar case, for example, the issue
6647 -- of dealing specially with object renamings.
6649 if Nkind
(P
) = N_Slice
6651 -- This optimization is disabled for CodePeer because it can transform
6652 -- an index-check constraint_error into a range-check constraint_error
6653 -- and CodePeer cares about that distinction.
6655 and then not CodePeer_Mode
6658 Make_Indexed_Component
(Loc
,
6659 Prefix
=> Prefix
(P
),
6660 Expressions
=> New_List
(
6662 (Etype
(First_Index
(Etype
(P
))),
6663 First
(Expressions
(N
))))));
6664 Analyze_And_Resolve
(N
, Typ
);
6668 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6669 -- function, then additional actuals must be passed.
6671 if Is_Build_In_Place_Function_Call
(P
) then
6672 Make_Build_In_Place_Call_In_Anonymous_Context
(P
);
6674 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
6675 -- containing build-in-place function calls whose returned object covers
6678 elsif Present
(Unqual_BIP_Iface_Function_Call
(P
)) then
6679 Make_Build_In_Place_Iface_Call_In_Anonymous_Context
(P
);
6682 -- If the prefix is an access type, then we unconditionally rewrite if
6683 -- as an explicit dereference. This simplifies processing for several
6684 -- cases, including packed array cases and certain cases in which checks
6685 -- must be generated. We used to try to do this only when it was
6686 -- necessary, but it cleans up the code to do it all the time.
6688 if Is_Access_Type
(T
) then
6689 Insert_Explicit_Dereference
(P
);
6690 Analyze_And_Resolve
(P
, Designated_Type
(T
));
6691 Atp
:= Designated_Type
(T
);
6696 -- Generate index and validity checks
6698 Generate_Index_Checks
(N
);
6700 if Validity_Checks_On
and then Validity_Check_Subscripts
then
6701 Apply_Subscript_Validity_Checks
(N
);
6704 -- If selecting from an array with atomic components, and atomic sync
6705 -- is not suppressed for this array type, set atomic sync flag.
6707 if (Has_Atomic_Components
(Atp
)
6708 and then not Atomic_Synchronization_Disabled
(Atp
))
6709 or else (Is_Atomic
(Typ
)
6710 and then not Atomic_Synchronization_Disabled
(Typ
))
6711 or else (Is_Entity_Name
(P
)
6712 and then Has_Atomic_Components
(Entity
(P
))
6713 and then not Atomic_Synchronization_Disabled
(Entity
(P
)))
6715 Activate_Atomic_Synchronization
(N
);
6718 -- All done if the prefix is not a packed array implemented specially
6720 if not (Is_Packed
(Etype
(Prefix
(N
)))
6721 and then Present
(Packed_Array_Impl_Type
(Etype
(Prefix
(N
)))))
6726 -- For packed arrays that are not bit-packed (i.e. the case of an array
6727 -- with one or more index types with a non-contiguous enumeration type),
6728 -- we can always use the normal packed element get circuit.
6730 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
6731 Expand_Packed_Element_Reference
(N
);
6735 -- For a reference to a component of a bit packed array, we convert it
6736 -- to a reference to the corresponding Packed_Array_Impl_Type. We only
6737 -- want to do this for simple references, and not for:
6739 -- Left side of assignment, or prefix of left side of assignment, or
6740 -- prefix of the prefix, to handle packed arrays of packed arrays,
6741 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6743 -- Renaming objects in renaming associations
6744 -- This case is handled when a use of the renamed variable occurs
6746 -- Actual parameters for a procedure call
6747 -- This case is handled in Exp_Ch6.Expand_Actuals
6749 -- The second expression in a 'Read attribute reference
6751 -- The prefix of an address or bit or size attribute reference
6753 -- The following circuit detects these exceptions. Note that we need to
6754 -- deal with implicit dereferences when climbing up the parent chain,
6755 -- with the additional difficulty that the type of parents may have yet
6756 -- to be resolved since prefixes are usually resolved first.
6759 Child
: Node_Id
:= N
;
6760 Parnt
: Node_Id
:= Parent
(N
);
6764 if Nkind
(Parnt
) = N_Unchecked_Expression
then
6767 elsif Nkind_In
(Parnt
, N_Object_Renaming_Declaration
,
6768 N_Procedure_Call_Statement
)
6769 or else (Nkind
(Parnt
) = N_Parameter_Association
6771 Nkind
(Parent
(Parnt
)) = N_Procedure_Call_Statement
)
6775 elsif Nkind
(Parnt
) = N_Attribute_Reference
6776 and then Nam_In
(Attribute_Name
(Parnt
), Name_Address
,
6779 and then Prefix
(Parnt
) = Child
6783 elsif Nkind
(Parnt
) = N_Assignment_Statement
6784 and then Name
(Parnt
) = Child
6788 -- If the expression is an index of an indexed component, it must
6789 -- be expanded regardless of context.
6791 elsif Nkind
(Parnt
) = N_Indexed_Component
6792 and then Child
/= Prefix
(Parnt
)
6794 Expand_Packed_Element_Reference
(N
);
6797 elsif Nkind
(Parent
(Parnt
)) = N_Assignment_Statement
6798 and then Name
(Parent
(Parnt
)) = Parnt
6802 elsif Nkind
(Parnt
) = N_Attribute_Reference
6803 and then Attribute_Name
(Parnt
) = Name_Read
6804 and then Next
(First
(Expressions
(Parnt
))) = Child
6808 elsif Nkind
(Parnt
) = N_Indexed_Component
6809 and then Prefix
(Parnt
) = Child
6813 elsif Nkind
(Parnt
) = N_Selected_Component
6814 and then Prefix
(Parnt
) = Child
6815 and then not (Present
(Etype
(Selector_Name
(Parnt
)))
6817 Is_Access_Type
(Etype
(Selector_Name
(Parnt
))))
6821 -- If the parent is a dereference, either implicit or explicit,
6822 -- then the packed reference needs to be expanded.
6825 Expand_Packed_Element_Reference
(N
);
6829 -- Keep looking up tree for unchecked expression, or if we are the
6830 -- prefix of a possible assignment left side.
6833 Parnt
:= Parent
(Child
);
6836 end Expand_N_Indexed_Component
;
6838 ---------------------
6839 -- Expand_N_Not_In --
6840 ---------------------
6842 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6843 -- can be done. This avoids needing to duplicate this expansion code.
6845 procedure Expand_N_Not_In
(N
: Node_Id
) is
6846 Loc
: constant Source_Ptr
:= Sloc
(N
);
6847 Typ
: constant Entity_Id
:= Etype
(N
);
6848 Cfs
: constant Boolean := Comes_From_Source
(N
);
6855 Left_Opnd
=> Left_Opnd
(N
),
6856 Right_Opnd
=> Right_Opnd
(N
))));
6858 -- If this is a set membership, preserve list of alternatives
6860 Set_Alternatives
(Right_Opnd
(N
), Alternatives
(Original_Node
(N
)));
6862 -- We want this to appear as coming from source if original does (see
6863 -- transformations in Expand_N_In).
6865 Set_Comes_From_Source
(N
, Cfs
);
6866 Set_Comes_From_Source
(Right_Opnd
(N
), Cfs
);
6868 -- Now analyze transformed node
6870 Analyze_And_Resolve
(N
, Typ
);
6871 end Expand_N_Not_In
;
6877 -- The only replacement required is for the case of a null of a type that
6878 -- is an access to protected subprogram, or a subtype thereof. We represent
6879 -- such access values as a record, and so we must replace the occurrence of
6880 -- null by the equivalent record (with a null address and a null pointer in
6881 -- it), so that the back end creates the proper value.
6883 procedure Expand_N_Null
(N
: Node_Id
) is
6884 Loc
: constant Source_Ptr
:= Sloc
(N
);
6885 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
6889 if Is_Access_Protected_Subprogram_Type
(Typ
) then
6891 Make_Aggregate
(Loc
,
6892 Expressions
=> New_List
(
6893 New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
),
6897 Analyze_And_Resolve
(N
, Equivalent_Type
(Typ
));
6899 -- For subsequent semantic analysis, the node must retain its type.
6900 -- Gigi in any case replaces this type by the corresponding record
6901 -- type before processing the node.
6907 when RE_Not_Available
=>
6911 ---------------------
6912 -- Expand_N_Op_Abs --
6913 ---------------------
6915 procedure Expand_N_Op_Abs
(N
: Node_Id
) is
6916 Loc
: constant Source_Ptr
:= Sloc
(N
);
6917 Expr
: constant Node_Id
:= Right_Opnd
(N
);
6920 Unary_Op_Validity_Checks
(N
);
6922 -- Check for MINIMIZED/ELIMINATED overflow mode
6924 if Minimized_Eliminated_Overflow_Check
(N
) then
6925 Apply_Arithmetic_Overflow_Check
(N
);
6929 -- Deal with software overflow checking
6931 if Is_Signed_Integer_Type
(Etype
(N
))
6932 and then Do_Overflow_Check
(N
)
6934 -- The only case to worry about is when the argument is equal to the
6935 -- largest negative number, so what we do is to insert the check:
6937 -- [constraint_error when Expr = typ'Base'First]
6939 -- with the usual Duplicate_Subexpr use coding for expr
6942 Make_Raise_Constraint_Error
(Loc
,
6945 Left_Opnd
=> Duplicate_Subexpr
(Expr
),
6947 Make_Attribute_Reference
(Loc
,
6949 New_Occurrence_Of
(Base_Type
(Etype
(Expr
)), Loc
),
6950 Attribute_Name
=> Name_First
)),
6951 Reason
=> CE_Overflow_Check_Failed
));
6953 Set_Do_Overflow_Check
(N
, False);
6955 end Expand_N_Op_Abs
;
6957 ---------------------
6958 -- Expand_N_Op_Add --
6959 ---------------------
6961 procedure Expand_N_Op_Add
(N
: Node_Id
) is
6962 Typ
: constant Entity_Id
:= Etype
(N
);
6965 Binary_Op_Validity_Checks
(N
);
6967 -- Check for MINIMIZED/ELIMINATED overflow mode
6969 if Minimized_Eliminated_Overflow_Check
(N
) then
6970 Apply_Arithmetic_Overflow_Check
(N
);
6974 -- N + 0 = 0 + N = N for integer types
6976 if Is_Integer_Type
(Typ
) then
6977 if Compile_Time_Known_Value
(Right_Opnd
(N
))
6978 and then Expr_Value
(Right_Opnd
(N
)) = Uint_0
6980 Rewrite
(N
, Left_Opnd
(N
));
6983 elsif Compile_Time_Known_Value
(Left_Opnd
(N
))
6984 and then Expr_Value
(Left_Opnd
(N
)) = Uint_0
6986 Rewrite
(N
, Right_Opnd
(N
));
6991 -- Arithmetic overflow checks for signed integer/fixed point types
6993 if Is_Signed_Integer_Type
(Typ
) or else Is_Fixed_Point_Type
(Typ
) then
6994 Apply_Arithmetic_Overflow_Check
(N
);
6998 -- Overflow checks for floating-point if -gnateF mode active
7000 Check_Float_Op_Overflow
(N
);
7002 Expand_Nonbinary_Modular_Op
(N
);
7003 end Expand_N_Op_Add
;
7005 ---------------------
7006 -- Expand_N_Op_And --
7007 ---------------------
7009 procedure Expand_N_Op_And
(N
: Node_Id
) is
7010 Typ
: constant Entity_Id
:= Etype
(N
);
7013 Binary_Op_Validity_Checks
(N
);
7015 if Is_Array_Type
(Etype
(N
)) then
7016 Expand_Boolean_Operator
(N
);
7018 elsif Is_Boolean_Type
(Etype
(N
)) then
7019 Adjust_Condition
(Left_Opnd
(N
));
7020 Adjust_Condition
(Right_Opnd
(N
));
7021 Set_Etype
(N
, Standard_Boolean
);
7022 Adjust_Result_Type
(N
, Typ
);
7024 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
7025 Expand_Intrinsic_Call
(N
, Entity
(N
));
7028 Expand_Nonbinary_Modular_Op
(N
);
7029 end Expand_N_Op_And
;
7031 ------------------------
7032 -- Expand_N_Op_Concat --
7033 ------------------------
7035 procedure Expand_N_Op_Concat
(N
: Node_Id
) is
7037 -- List of operands to be concatenated
7040 -- Node which is to be replaced by the result of concatenating the nodes
7041 -- in the list Opnds.
7044 -- Ensure validity of both operands
7046 Binary_Op_Validity_Checks
(N
);
7048 -- If we are the left operand of a concatenation higher up the tree,
7049 -- then do nothing for now, since we want to deal with a series of
7050 -- concatenations as a unit.
7052 if Nkind
(Parent
(N
)) = N_Op_Concat
7053 and then N
= Left_Opnd
(Parent
(N
))
7058 -- We get here with a concatenation whose left operand may be a
7059 -- concatenation itself with a consistent type. We need to process
7060 -- these concatenation operands from left to right, which means
7061 -- from the deepest node in the tree to the highest node.
7064 while Nkind
(Left_Opnd
(Cnode
)) = N_Op_Concat
loop
7065 Cnode
:= Left_Opnd
(Cnode
);
7068 -- Now Cnode is the deepest concatenation, and its parents are the
7069 -- concatenation nodes above, so now we process bottom up, doing the
7072 -- The outer loop runs more than once if more than one concatenation
7073 -- type is involved.
7076 Opnds
:= New_List
(Left_Opnd
(Cnode
), Right_Opnd
(Cnode
));
7077 Set_Parent
(Opnds
, N
);
7079 -- The inner loop gathers concatenation operands
7081 Inner
: while Cnode
/= N
7082 and then Base_Type
(Etype
(Cnode
)) =
7083 Base_Type
(Etype
(Parent
(Cnode
)))
7085 Cnode
:= Parent
(Cnode
);
7086 Append
(Right_Opnd
(Cnode
), Opnds
);
7089 -- Note: The following code is a temporary workaround for N731-034
7090 -- and N829-028 and will be kept until the general issue of internal
7091 -- symbol serialization is addressed. The workaround is kept under a
7092 -- debug switch to avoid permiating into the general case.
7094 -- Wrap the node to concatenate into an expression actions node to
7095 -- keep it nicely packaged. This is useful in the case of an assert
7096 -- pragma with a concatenation where we want to be able to delete
7097 -- the concatenation and all its expansion stuff.
7099 if Debug_Flag_Dot_H
then
7101 Cnod
: constant Node_Id
:= New_Copy_Tree
(Cnode
);
7102 Typ
: constant Entity_Id
:= Base_Type
(Etype
(Cnode
));
7105 -- Note: use Rewrite rather than Replace here, so that for
7106 -- example Why_Not_Static can find the original concatenation
7110 Make_Expression_With_Actions
(Sloc
(Cnode
),
7111 Actions
=> New_List
(Make_Null_Statement
(Sloc
(Cnode
))),
7112 Expression
=> Cnod
));
7114 Expand_Concatenate
(Cnod
, Opnds
);
7115 Analyze_And_Resolve
(Cnode
, Typ
);
7121 Expand_Concatenate
(Cnode
, Opnds
);
7124 exit Outer
when Cnode
= N
;
7125 Cnode
:= Parent
(Cnode
);
7127 end Expand_N_Op_Concat
;
7129 ------------------------
7130 -- Expand_N_Op_Divide --
7131 ------------------------
7133 procedure Expand_N_Op_Divide
(N
: Node_Id
) is
7134 Loc
: constant Source_Ptr
:= Sloc
(N
);
7135 Lopnd
: constant Node_Id
:= Left_Opnd
(N
);
7136 Ropnd
: constant Node_Id
:= Right_Opnd
(N
);
7137 Ltyp
: constant Entity_Id
:= Etype
(Lopnd
);
7138 Rtyp
: constant Entity_Id
:= Etype
(Ropnd
);
7139 Typ
: Entity_Id
:= Etype
(N
);
7140 Rknow
: constant Boolean := Is_Integer_Type
(Typ
)
7142 Compile_Time_Known_Value
(Ropnd
);
7146 Binary_Op_Validity_Checks
(N
);
7148 -- Check for MINIMIZED/ELIMINATED overflow mode
7150 if Minimized_Eliminated_Overflow_Check
(N
) then
7151 Apply_Arithmetic_Overflow_Check
(N
);
7155 -- Otherwise proceed with expansion of division
7158 Rval
:= Expr_Value
(Ropnd
);
7161 -- N / 1 = N for integer types
7163 if Rknow
and then Rval
= Uint_1
then
7168 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
7169 -- Is_Power_Of_2_For_Shift is set means that we know that our left
7170 -- operand is an unsigned integer, as required for this to work.
7172 if Nkind
(Ropnd
) = N_Op_Expon
7173 and then Is_Power_Of_2_For_Shift
(Ropnd
)
7175 -- We cannot do this transformation in configurable run time mode if we
7176 -- have 64-bit integers and long shifts are not available.
7178 and then (Esize
(Ltyp
) <= 32 or else Support_Long_Shifts_On_Target
)
7181 Make_Op_Shift_Right
(Loc
,
7184 Convert_To
(Standard_Natural
, Right_Opnd
(Ropnd
))));
7185 Analyze_And_Resolve
(N
, Typ
);
7189 -- Do required fixup of universal fixed operation
7191 if Typ
= Universal_Fixed
then
7192 Fixup_Universal_Fixed_Operation
(N
);
7196 -- Divisions with fixed-point results
7198 if Is_Fixed_Point_Type
(Typ
) then
7200 -- No special processing if Treat_Fixed_As_Integer is set, since
7201 -- from a semantic point of view such operations are simply integer
7202 -- operations and will be treated that way.
7204 if not Treat_Fixed_As_Integer
(N
) then
7205 if Is_Integer_Type
(Rtyp
) then
7206 Expand_Divide_Fixed_By_Integer_Giving_Fixed
(N
);
7208 Expand_Divide_Fixed_By_Fixed_Giving_Fixed
(N
);
7212 -- Deal with divide-by-zero check if back end cannot handle them
7213 -- and the flag is set indicating that we need such a check. Note
7214 -- that we don't need to bother here with the case of mixed-mode
7215 -- (Right operand an integer type), since these will be rewritten
7216 -- with conversions to a divide with a fixed-point right operand.
7218 if Nkind
(N
) = N_Op_Divide
7219 and then Do_Division_Check
(N
)
7220 and then not Backend_Divide_Checks_On_Target
7221 and then not Is_Integer_Type
(Rtyp
)
7223 Set_Do_Division_Check
(N
, False);
7225 Make_Raise_Constraint_Error
(Loc
,
7228 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ropnd
),
7229 Right_Opnd
=> Make_Real_Literal
(Loc
, Ureal_0
)),
7230 Reason
=> CE_Divide_By_Zero
));
7233 -- Other cases of division of fixed-point operands. Again we exclude the
7234 -- case where Treat_Fixed_As_Integer is set.
7236 elsif (Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
))
7237 and then not Treat_Fixed_As_Integer
(N
)
7239 if Is_Integer_Type
(Typ
) then
7240 Expand_Divide_Fixed_By_Fixed_Giving_Integer
(N
);
7242 pragma Assert
(Is_Floating_Point_Type
(Typ
));
7243 Expand_Divide_Fixed_By_Fixed_Giving_Float
(N
);
7246 -- Mixed-mode operations can appear in a non-static universal context,
7247 -- in which case the integer argument must be converted explicitly.
7249 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Rtyp
) then
7251 Convert_To
(Universal_Real
, Relocate_Node
(Ropnd
)));
7253 Analyze_And_Resolve
(Ropnd
, Universal_Real
);
7255 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Ltyp
) then
7257 Convert_To
(Universal_Real
, Relocate_Node
(Lopnd
)));
7259 Analyze_And_Resolve
(Lopnd
, Universal_Real
);
7261 -- Non-fixed point cases, do integer zero divide and overflow checks
7263 elsif Is_Integer_Type
(Typ
) then
7264 Apply_Divide_Checks
(N
);
7267 -- Overflow checks for floating-point if -gnateF mode active
7269 Check_Float_Op_Overflow
(N
);
7271 Expand_Nonbinary_Modular_Op
(N
);
7272 end Expand_N_Op_Divide
;
7274 --------------------
7275 -- Expand_N_Op_Eq --
7276 --------------------
7278 procedure Expand_N_Op_Eq
(N
: Node_Id
) is
7279 Loc
: constant Source_Ptr
:= Sloc
(N
);
7280 Typ
: constant Entity_Id
:= Etype
(N
);
7281 Lhs
: constant Node_Id
:= Left_Opnd
(N
);
7282 Rhs
: constant Node_Id
:= Right_Opnd
(N
);
7283 Bodies
: constant List_Id
:= New_List
;
7284 A_Typ
: constant Entity_Id
:= Etype
(Lhs
);
7286 Typl
: Entity_Id
:= A_Typ
;
7287 Op_Name
: Entity_Id
;
7290 procedure Build_Equality_Call
(Eq
: Entity_Id
);
7291 -- If a constructed equality exists for the type or for its parent,
7292 -- build and analyze call, adding conversions if the operation is
7295 function Has_Unconstrained_UU_Component
(Typ
: Node_Id
) return Boolean;
7296 -- Determines whether a type has a subcomponent of an unconstrained
7297 -- Unchecked_Union subtype. Typ is a record type.
7299 -------------------------
7300 -- Build_Equality_Call --
7301 -------------------------
7303 procedure Build_Equality_Call
(Eq
: Entity_Id
) is
7304 Op_Type
: constant Entity_Id
:= Etype
(First_Formal
(Eq
));
7305 L_Exp
: Node_Id
:= Relocate_Node
(Lhs
);
7306 R_Exp
: Node_Id
:= Relocate_Node
(Rhs
);
7309 -- Adjust operands if necessary to comparison type
7311 if Base_Type
(Op_Type
) /= Base_Type
(A_Typ
)
7312 and then not Is_Class_Wide_Type
(A_Typ
)
7314 L_Exp
:= OK_Convert_To
(Op_Type
, L_Exp
);
7315 R_Exp
:= OK_Convert_To
(Op_Type
, R_Exp
);
7318 -- If we have an Unchecked_Union, we need to add the inferred
7319 -- discriminant values as actuals in the function call. At this
7320 -- point, the expansion has determined that both operands have
7321 -- inferable discriminants.
7323 if Is_Unchecked_Union
(Op_Type
) then
7325 Lhs_Type
: constant Node_Id
:= Etype
(L_Exp
);
7326 Rhs_Type
: constant Node_Id
:= Etype
(R_Exp
);
7328 Lhs_Discr_Vals
: Elist_Id
;
7329 -- List of inferred discriminant values for left operand.
7331 Rhs_Discr_Vals
: Elist_Id
;
7332 -- List of inferred discriminant values for right operand.
7337 Lhs_Discr_Vals
:= New_Elmt_List
;
7338 Rhs_Discr_Vals
:= New_Elmt_List
;
7340 -- Per-object constrained selected components require special
7341 -- attention. If the enclosing scope of the component is an
7342 -- Unchecked_Union, we cannot reference its discriminants
7343 -- directly. This is why we use the extra parameters of the
7344 -- equality function of the enclosing Unchecked_Union.
7346 -- type UU_Type (Discr : Integer := 0) is
7349 -- pragma Unchecked_Union (UU_Type);
7351 -- 1. Unchecked_Union enclosing record:
7353 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
7355 -- Comp : UU_Type (Discr);
7357 -- end Enclosing_UU_Type;
7358 -- pragma Unchecked_Union (Enclosing_UU_Type);
7360 -- Obj1 : Enclosing_UU_Type;
7361 -- Obj2 : Enclosing_UU_Type (1);
7363 -- [. . .] Obj1 = Obj2 [. . .]
7367 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
7369 -- A and B are the formal parameters of the equality function
7370 -- of Enclosing_UU_Type. The function always has two extra
7371 -- formals to capture the inferred discriminant values for
7372 -- each discriminant of the type.
7374 -- 2. Non-Unchecked_Union enclosing record:
7377 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
7380 -- Comp : UU_Type (Discr);
7382 -- end Enclosing_Non_UU_Type;
7384 -- Obj1 : Enclosing_Non_UU_Type;
7385 -- Obj2 : Enclosing_Non_UU_Type (1);
7387 -- ... Obj1 = Obj2 ...
7391 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
7392 -- obj1.discr, obj2.discr)) then
7394 -- In this case we can directly reference the discriminants of
7395 -- the enclosing record.
7397 -- Process left operand of equality
7399 if Nkind
(Lhs
) = N_Selected_Component
7401 Has_Per_Object_Constraint
(Entity
(Selector_Name
(Lhs
)))
7403 -- If enclosing record is an Unchecked_Union, use formals
7404 -- corresponding to each discriminant. The name of the
7405 -- formal is that of the discriminant, with added suffix,
7406 -- see Exp_Ch3.Build_Record_Equality for details.
7408 if Is_Unchecked_Union
(Scope
(Entity
(Selector_Name
(Lhs
))))
7412 (Scope
(Entity
(Selector_Name
(Lhs
))));
7413 while Present
(Discr
) loop
7415 (Make_Identifier
(Loc
,
7416 Chars
=> New_External_Name
(Chars
(Discr
), 'A')),
7417 To
=> Lhs_Discr_Vals
);
7418 Next_Discriminant
(Discr
);
7421 -- If enclosing record is of a non-Unchecked_Union type, it
7422 -- is possible to reference its discriminants directly.
7425 Discr
:= First_Discriminant
(Lhs_Type
);
7426 while Present
(Discr
) loop
7428 (Make_Selected_Component
(Loc
,
7429 Prefix
=> Prefix
(Lhs
),
7432 (Get_Discriminant_Value
(Discr
,
7434 Stored_Constraint
(Lhs_Type
)))),
7435 To
=> Lhs_Discr_Vals
);
7436 Next_Discriminant
(Discr
);
7440 -- Otherwise operand is on object with a constrained type.
7441 -- Infer the discriminant values from the constraint.
7445 Discr
:= First_Discriminant
(Lhs_Type
);
7446 while Present
(Discr
) loop
7449 (Get_Discriminant_Value
(Discr
,
7451 Stored_Constraint
(Lhs_Type
))),
7452 To
=> Lhs_Discr_Vals
);
7453 Next_Discriminant
(Discr
);
7457 -- Similar processing for right operand of equality
7459 if Nkind
(Rhs
) = N_Selected_Component
7461 Has_Per_Object_Constraint
(Entity
(Selector_Name
(Rhs
)))
7463 if Is_Unchecked_Union
7464 (Scope
(Entity
(Selector_Name
(Rhs
))))
7468 (Scope
(Entity
(Selector_Name
(Rhs
))));
7469 while Present
(Discr
) loop
7471 (Make_Identifier
(Loc
,
7472 Chars
=> New_External_Name
(Chars
(Discr
), 'B')),
7473 To
=> Rhs_Discr_Vals
);
7474 Next_Discriminant
(Discr
);
7478 Discr
:= First_Discriminant
(Rhs_Type
);
7479 while Present
(Discr
) loop
7481 (Make_Selected_Component
(Loc
,
7482 Prefix
=> Prefix
(Rhs
),
7484 New_Copy
(Get_Discriminant_Value
7487 Stored_Constraint
(Rhs_Type
)))),
7488 To
=> Rhs_Discr_Vals
);
7489 Next_Discriminant
(Discr
);
7494 Discr
:= First_Discriminant
(Rhs_Type
);
7495 while Present
(Discr
) loop
7497 (New_Copy
(Get_Discriminant_Value
7500 Stored_Constraint
(Rhs_Type
))),
7501 To
=> Rhs_Discr_Vals
);
7502 Next_Discriminant
(Discr
);
7506 -- Now merge the list of discriminant values so that values
7507 -- of corresponding discriminants are adjacent.
7515 Params
:= New_List
(L_Exp
, R_Exp
);
7516 L_Elmt
:= First_Elmt
(Lhs_Discr_Vals
);
7517 R_Elmt
:= First_Elmt
(Rhs_Discr_Vals
);
7518 while Present
(L_Elmt
) loop
7519 Append_To
(Params
, Node
(L_Elmt
));
7520 Append_To
(Params
, Node
(R_Elmt
));
7526 Make_Function_Call
(Loc
,
7527 Name
=> New_Occurrence_Of
(Eq
, Loc
),
7528 Parameter_Associations
=> Params
));
7532 -- Normal case, not an unchecked union
7536 Make_Function_Call
(Loc
,
7537 Name
=> New_Occurrence_Of
(Eq
, Loc
),
7538 Parameter_Associations
=> New_List
(L_Exp
, R_Exp
)));
7541 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7542 end Build_Equality_Call
;
7544 ------------------------------------
7545 -- Has_Unconstrained_UU_Component --
7546 ------------------------------------
7548 function Has_Unconstrained_UU_Component
7549 (Typ
: Node_Id
) return Boolean
7551 Tdef
: constant Node_Id
:=
7552 Type_Definition
(Declaration_Node
(Base_Type
(Typ
)));
7556 function Component_Is_Unconstrained_UU
7557 (Comp
: Node_Id
) return Boolean;
7558 -- Determines whether the subtype of the component is an
7559 -- unconstrained Unchecked_Union.
7561 function Variant_Is_Unconstrained_UU
7562 (Variant
: Node_Id
) return Boolean;
7563 -- Determines whether a component of the variant has an unconstrained
7564 -- Unchecked_Union subtype.
7566 -----------------------------------
7567 -- Component_Is_Unconstrained_UU --
7568 -----------------------------------
7570 function Component_Is_Unconstrained_UU
7571 (Comp
: Node_Id
) return Boolean
7574 if Nkind
(Comp
) /= N_Component_Declaration
then
7579 Sindic
: constant Node_Id
:=
7580 Subtype_Indication
(Component_Definition
(Comp
));
7583 -- Unconstrained nominal type. In the case of a constraint
7584 -- present, the node kind would have been N_Subtype_Indication.
7586 if Nkind
(Sindic
) = N_Identifier
then
7587 return Is_Unchecked_Union
(Base_Type
(Etype
(Sindic
)));
7592 end Component_Is_Unconstrained_UU
;
7594 ---------------------------------
7595 -- Variant_Is_Unconstrained_UU --
7596 ---------------------------------
7598 function Variant_Is_Unconstrained_UU
7599 (Variant
: Node_Id
) return Boolean
7601 Clist
: constant Node_Id
:= Component_List
(Variant
);
7604 if Is_Empty_List
(Component_Items
(Clist
)) then
7608 -- We only need to test one component
7611 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
7614 while Present
(Comp
) loop
7615 if Component_Is_Unconstrained_UU
(Comp
) then
7623 -- None of the components withing the variant were of
7624 -- unconstrained Unchecked_Union type.
7627 end Variant_Is_Unconstrained_UU
;
7629 -- Start of processing for Has_Unconstrained_UU_Component
7632 if Null_Present
(Tdef
) then
7636 Clist
:= Component_List
(Tdef
);
7637 Vpart
:= Variant_Part
(Clist
);
7639 -- Inspect available components
7641 if Present
(Component_Items
(Clist
)) then
7643 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
7646 while Present
(Comp
) loop
7648 -- One component is sufficient
7650 if Component_Is_Unconstrained_UU
(Comp
) then
7659 -- Inspect available components withing variants
7661 if Present
(Vpart
) then
7663 Variant
: Node_Id
:= First
(Variants
(Vpart
));
7666 while Present
(Variant
) loop
7668 -- One component within a variant is sufficient
7670 if Variant_Is_Unconstrained_UU
(Variant
) then
7679 -- Neither the available components, nor the components inside the
7680 -- variant parts were of an unconstrained Unchecked_Union subtype.
7683 end Has_Unconstrained_UU_Component
;
7685 -- Start of processing for Expand_N_Op_Eq
7688 Binary_Op_Validity_Checks
(N
);
7690 -- Deal with private types
7692 if Ekind
(Typl
) = E_Private_Type
then
7693 Typl
:= Underlying_Type
(Typl
);
7694 elsif Ekind
(Typl
) = E_Private_Subtype
then
7695 Typl
:= Underlying_Type
(Base_Type
(Typl
));
7700 -- It may happen in error situations that the underlying type is not
7701 -- set. The error will be detected later, here we just defend the
7708 -- Now get the implementation base type (note that plain Base_Type here
7709 -- might lead us back to the private type, which is not what we want!)
7711 Typl
:= Implementation_Base_Type
(Typl
);
7713 -- Equality between variant records results in a call to a routine
7714 -- that has conditional tests of the discriminant value(s), and hence
7715 -- violates the No_Implicit_Conditionals restriction.
7717 if Has_Variant_Part
(Typl
) then
7722 Check_Restriction
(Msg
, No_Implicit_Conditionals
, N
);
7726 ("\comparison of variant records tests discriminants", N
);
7732 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7733 -- means we no longer have a comparison operation, we are all done.
7735 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
7737 if Nkind
(N
) /= N_Op_Eq
then
7741 -- Boolean types (requiring handling of non-standard case)
7743 if Is_Boolean_Type
(Typl
) then
7744 Adjust_Condition
(Left_Opnd
(N
));
7745 Adjust_Condition
(Right_Opnd
(N
));
7746 Set_Etype
(N
, Standard_Boolean
);
7747 Adjust_Result_Type
(N
, Typ
);
7751 elsif Is_Array_Type
(Typl
) then
7753 -- If we are doing full validity checking, and it is possible for the
7754 -- array elements to be invalid then expand out array comparisons to
7755 -- make sure that we check the array elements.
7757 if Validity_Check_Operands
7758 and then not Is_Known_Valid
(Component_Type
(Typl
))
7761 Save_Force_Validity_Checks
: constant Boolean :=
7762 Force_Validity_Checks
;
7764 Force_Validity_Checks
:= True;
7766 Expand_Array_Equality
7768 Relocate_Node
(Lhs
),
7769 Relocate_Node
(Rhs
),
7772 Insert_Actions
(N
, Bodies
);
7773 Analyze_And_Resolve
(N
, Standard_Boolean
);
7774 Force_Validity_Checks
:= Save_Force_Validity_Checks
;
7777 -- Packed case where both operands are known aligned
7779 elsif Is_Bit_Packed_Array
(Typl
)
7780 and then not Is_Possibly_Unaligned_Object
(Lhs
)
7781 and then not Is_Possibly_Unaligned_Object
(Rhs
)
7783 Expand_Packed_Eq
(N
);
7785 -- Where the component type is elementary we can use a block bit
7786 -- comparison (if supported on the target) exception in the case
7787 -- of floating-point (negative zero issues require element by
7788 -- element comparison), and atomic/VFA types (where we must be sure
7789 -- to load elements independently) and possibly unaligned arrays.
7791 elsif Is_Elementary_Type
(Component_Type
(Typl
))
7792 and then not Is_Floating_Point_Type
(Component_Type
(Typl
))
7793 and then not Is_Atomic_Or_VFA
(Component_Type
(Typl
))
7794 and then not Is_Possibly_Unaligned_Object
(Lhs
)
7795 and then not Is_Possibly_Unaligned_Object
(Rhs
)
7796 and then Support_Composite_Compare_On_Target
7800 -- For composite and floating-point cases, expand equality loop to
7801 -- make sure of using proper comparisons for tagged types, and
7802 -- correctly handling the floating-point case.
7806 Expand_Array_Equality
7808 Relocate_Node
(Lhs
),
7809 Relocate_Node
(Rhs
),
7812 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
7813 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7818 elsif Is_Record_Type
(Typl
) then
7820 -- For tagged types, use the primitive "="
7822 if Is_Tagged_Type
(Typl
) then
7824 -- No need to do anything else compiling under restriction
7825 -- No_Dispatching_Calls. During the semantic analysis we
7826 -- already notified such violation.
7828 if Restriction_Active
(No_Dispatching_Calls
) then
7832 -- If this is an untagged private type completed with a derivation
7833 -- of an untagged private type whose full view is a tagged type,
7834 -- we use the primitive operations of the private type (since it
7835 -- does not have a full view, and also because its equality
7836 -- primitive may have been overridden in its untagged full view).
7838 if Inherits_From_Tagged_Full_View
(A_Typ
) then
7840 -- Search for equality operation, checking that the operands
7841 -- have the same type. Note that we must find a matching entry,
7842 -- or something is very wrong.
7844 Prim
:= First_Elmt
(Collect_Primitive_Operations
(A_Typ
));
7846 while Present
(Prim
) loop
7847 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7848 and then Etype
(First_Formal
(Node
(Prim
))) =
7849 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7851 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7856 pragma Assert
(Present
(Prim
));
7857 Op_Name
:= Node
(Prim
);
7859 -- Find the type's predefined equality or an overriding
7860 -- user-defined equality. The reason for not simply calling
7861 -- Find_Prim_Op here is that there may be a user-defined
7862 -- overloaded equality op that precedes the equality that we
7863 -- want, so we have to explicitly search (e.g., there could be
7864 -- an equality with two different parameter types).
7867 if Is_Class_Wide_Type
(Typl
) then
7868 Typl
:= Find_Specific_Type
(Typl
);
7871 Prim
:= First_Elmt
(Primitive_Operations
(Typl
));
7872 while Present
(Prim
) loop
7873 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7874 and then Etype
(First_Formal
(Node
(Prim
))) =
7875 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7877 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7882 pragma Assert
(Present
(Prim
));
7883 Op_Name
:= Node
(Prim
);
7886 Build_Equality_Call
(Op_Name
);
7888 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7889 -- predefined equality operator for a type which has a subcomponent
7890 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7892 elsif Has_Unconstrained_UU_Component
(Typl
) then
7894 Make_Raise_Program_Error
(Loc
,
7895 Reason
=> PE_Unchecked_Union_Restriction
));
7897 -- Prevent Gigi from generating incorrect code by rewriting the
7898 -- equality as a standard False. (is this documented somewhere???)
7901 New_Occurrence_Of
(Standard_False
, Loc
));
7903 elsif Is_Unchecked_Union
(Typl
) then
7905 -- If we can infer the discriminants of the operands, we make a
7906 -- call to the TSS equality function.
7908 if Has_Inferable_Discriminants
(Lhs
)
7910 Has_Inferable_Discriminants
(Rhs
)
7913 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
7916 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7917 -- the predefined equality operator for an Unchecked_Union type
7918 -- if either of the operands lack inferable discriminants.
7921 Make_Raise_Program_Error
(Loc
,
7922 Reason
=> PE_Unchecked_Union_Restriction
));
7924 -- Emit a warning on source equalities only, otherwise the
7925 -- message may appear out of place due to internal use. The
7926 -- warning is unconditional because it is required by the
7929 if Comes_From_Source
(N
) then
7931 ("Unchecked_Union discriminants cannot be determined??",
7934 ("\Program_Error will be raised for equality operation??",
7938 -- Prevent Gigi from generating incorrect code by rewriting
7939 -- the equality as a standard False (documented where???).
7942 New_Occurrence_Of
(Standard_False
, Loc
));
7945 -- If a type support function is present (for complex cases), use it
7947 elsif Present
(TSS
(Root_Type
(Typl
), TSS_Composite_Equality
)) then
7949 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
7951 -- When comparing two Bounded_Strings, use the primitive equality of
7952 -- the root Super_String type.
7954 elsif Is_Bounded_String
(Typl
) then
7956 First_Elmt
(Collect_Primitive_Operations
(Root_Type
(Typl
)));
7958 while Present
(Prim
) loop
7959 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7960 and then Etype
(First_Formal
(Node
(Prim
))) =
7961 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7962 and then Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7967 -- A Super_String type should always have a primitive equality
7969 pragma Assert
(Present
(Prim
));
7970 Build_Equality_Call
(Node
(Prim
));
7972 -- Otherwise expand the component by component equality. Note that
7973 -- we never use block-bit comparisons for records, because of the
7974 -- problems with gaps. The back end will often be able to recombine
7975 -- the separate comparisons that we generate here.
7978 Remove_Side_Effects
(Lhs
);
7979 Remove_Side_Effects
(Rhs
);
7981 Expand_Record_Equality
(N
, Typl
, Lhs
, Rhs
, Bodies
));
7983 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
7984 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7988 -- Test if result is known at compile time
7990 Rewrite_Comparison
(N
);
7992 -- Special optimization of length comparison
7994 Optimize_Length_Comparison
(N
);
7996 -- One more special case: if we have a comparison of X'Result = expr
7997 -- in floating-point, then if not already there, change expr to be
7998 -- f'Machine (expr) to eliminate surprise from extra precision.
8000 if Is_Floating_Point_Type
(Typl
)
8001 and then Nkind
(Original_Node
(Lhs
)) = N_Attribute_Reference
8002 and then Attribute_Name
(Original_Node
(Lhs
)) = Name_Result
8004 -- Stick in the Typ'Machine call if not already there
8006 if Nkind
(Rhs
) /= N_Attribute_Reference
8007 or else Attribute_Name
(Rhs
) /= Name_Machine
8010 Make_Attribute_Reference
(Loc
,
8011 Prefix
=> New_Occurrence_Of
(Typl
, Loc
),
8012 Attribute_Name
=> Name_Machine
,
8013 Expressions
=> New_List
(Relocate_Node
(Rhs
))));
8014 Analyze_And_Resolve
(Rhs
, Typl
);
8019 -----------------------
8020 -- Expand_N_Op_Expon --
8021 -----------------------
8023 procedure Expand_N_Op_Expon
(N
: Node_Id
) is
8024 Loc
: constant Source_Ptr
:= Sloc
(N
);
8025 Ovflo
: constant Boolean := Do_Overflow_Check
(N
);
8026 Typ
: constant Entity_Id
:= Etype
(N
);
8027 Rtyp
: constant Entity_Id
:= Root_Type
(Typ
);
8031 function Wrap_MA
(Exp
: Node_Id
) return Node_Id
;
8032 -- Given an expression Exp, if the root type is Float or Long_Float,
8033 -- then wrap the expression in a call of Bastyp'Machine, to stop any
8034 -- extra precision. This is done to ensure that X**A = X**B when A is
8035 -- a static constant and B is a variable with the same value. For any
8036 -- other type, the node Exp is returned unchanged.
8042 function Wrap_MA
(Exp
: Node_Id
) return Node_Id
is
8043 Loc
: constant Source_Ptr
:= Sloc
(Exp
);
8046 if Rtyp
= Standard_Float
or else Rtyp
= Standard_Long_Float
then
8048 Make_Attribute_Reference
(Loc
,
8049 Attribute_Name
=> Name_Machine
,
8050 Prefix
=> New_Occurrence_Of
(Bastyp
, Loc
),
8051 Expressions
=> New_List
(Relocate_Node
(Exp
)));
8069 -- Start of processing for Expand_N_Op_Expon
8072 Binary_Op_Validity_Checks
(N
);
8074 -- CodePeer wants to see the unexpanded N_Op_Expon node
8076 if CodePeer_Mode
then
8080 -- Relocation of left and right operands must be done after performing
8081 -- the validity checks since the generation of validation checks may
8082 -- remove side effects.
8084 Base
:= Relocate_Node
(Left_Opnd
(N
));
8085 Bastyp
:= Etype
(Base
);
8086 Exp
:= Relocate_Node
(Right_Opnd
(N
));
8087 Exptyp
:= Etype
(Exp
);
8089 -- If either operand is of a private type, then we have the use of an
8090 -- intrinsic operator, and we get rid of the privateness, by using root
8091 -- types of underlying types for the actual operation. Otherwise the
8092 -- private types will cause trouble if we expand multiplications or
8093 -- shifts etc. We also do this transformation if the result type is
8094 -- different from the base type.
8096 if Is_Private_Type
(Etype
(Base
))
8097 or else Is_Private_Type
(Typ
)
8098 or else Is_Private_Type
(Exptyp
)
8099 or else Rtyp
/= Root_Type
(Bastyp
)
8102 Bt
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Bastyp
));
8103 Et
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Exptyp
));
8106 Unchecked_Convert_To
(Typ
,
8108 Left_Opnd
=> Unchecked_Convert_To
(Bt
, Base
),
8109 Right_Opnd
=> Unchecked_Convert_To
(Et
, Exp
))));
8110 Analyze_And_Resolve
(N
, Typ
);
8115 -- Check for MINIMIZED/ELIMINATED overflow mode
8117 if Minimized_Eliminated_Overflow_Check
(N
) then
8118 Apply_Arithmetic_Overflow_Check
(N
);
8122 -- Test for case of known right argument where we can replace the
8123 -- exponentiation by an equivalent expression using multiplication.
8125 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
8126 -- configurable run-time mode, we may not have the exponentiation
8127 -- routine available, and we don't want the legality of the program
8128 -- to depend on how clever the compiler is in knowing values.
8130 if CRT_Safe_Compile_Time_Known_Value
(Exp
) then
8131 Expv
:= Expr_Value
(Exp
);
8133 -- We only fold small non-negative exponents. You might think we
8134 -- could fold small negative exponents for the real case, but we
8135 -- can't because we are required to raise Constraint_Error for
8136 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
8137 -- See ACVC test C4A012B, and it is not worth generating the test.
8139 -- For small negative exponents, we return the reciprocal of
8140 -- the folding of the exponentiation for the opposite (positive)
8141 -- exponent, as required by Ada RM 4.5.6(11/3).
8143 if abs Expv
<= 4 then
8145 -- X ** 0 = 1 (or 1.0)
8149 -- Call Remove_Side_Effects to ensure that any side effects
8150 -- in the ignored left operand (in particular function calls
8151 -- to user defined functions) are properly executed.
8153 Remove_Side_Effects
(Base
);
8155 if Ekind
(Typ
) in Integer_Kind
then
8156 Xnode
:= Make_Integer_Literal
(Loc
, Intval
=> 1);
8158 Xnode
:= Make_Real_Literal
(Loc
, Ureal_1
);
8171 Make_Op_Multiply
(Loc
,
8172 Left_Opnd
=> Duplicate_Subexpr
(Base
),
8173 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)));
8175 -- X ** 3 = X * X * X
8180 Make_Op_Multiply
(Loc
,
8182 Make_Op_Multiply
(Loc
,
8183 Left_Opnd
=> Duplicate_Subexpr
(Base
),
8184 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)),
8185 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)));
8190 -- En : constant base'type := base * base;
8195 Temp
:= Make_Temporary
(Loc
, 'E', Base
);
8198 Make_Expression_With_Actions
(Loc
,
8199 Actions
=> New_List
(
8200 Make_Object_Declaration
(Loc
,
8201 Defining_Identifier
=> Temp
,
8202 Constant_Present
=> True,
8203 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
8206 Make_Op_Multiply
(Loc
,
8208 Duplicate_Subexpr
(Base
),
8210 Duplicate_Subexpr_No_Checks
(Base
))))),
8214 Make_Op_Multiply
(Loc
,
8215 Left_Opnd
=> New_Occurrence_Of
(Temp
, Loc
),
8216 Right_Opnd
=> New_Occurrence_Of
(Temp
, Loc
))));
8218 -- X ** N = 1.0 / X ** (-N)
8223 (Expv
= -1 or Expv
= -2 or Expv
= -3 or Expv
= -4);
8226 Make_Op_Divide
(Loc
,
8228 Make_Float_Literal
(Loc
,
8230 Significand
=> Uint_1
,
8231 Exponent
=> Uint_0
),
8234 Left_Opnd
=> Duplicate_Subexpr
(Base
),
8236 Make_Integer_Literal
(Loc
,
8241 Analyze_And_Resolve
(N
, Typ
);
8246 -- Deal with optimizing 2 ** expression to shift where possible
8248 -- Note: we used to check that Exptyp was an unsigned type. But that is
8249 -- an unnecessary check, since if Exp is negative, we have a run-time
8250 -- error that is either caught (so we get the right result) or we have
8251 -- suppressed the check, in which case the code is erroneous anyway.
8253 if Is_Integer_Type
(Rtyp
)
8255 -- The base value must be "safe compile-time known", and exactly 2
8257 and then Nkind
(Base
) = N_Integer_Literal
8258 and then CRT_Safe_Compile_Time_Known_Value
(Base
)
8259 and then Expr_Value
(Base
) = Uint_2
8261 -- We only handle cases where the right type is a integer
8263 and then Is_Integer_Type
(Root_Type
(Exptyp
))
8264 and then Esize
(Root_Type
(Exptyp
)) <= Esize
(Standard_Integer
)
8266 -- This transformation is not applicable for a modular type with a
8267 -- nonbinary modulus because we do not handle modular reduction in
8268 -- a correct manner if we attempt this transformation in this case.
8270 and then not Non_Binary_Modulus
(Typ
)
8272 -- Handle the cases where our parent is a division or multiplication
8273 -- specially. In these cases we can convert to using a shift at the
8274 -- parent level if we are not doing overflow checking, since it is
8275 -- too tricky to combine the overflow check at the parent level.
8278 and then Nkind_In
(Parent
(N
), N_Op_Divide
, N_Op_Multiply
)
8281 P
: constant Node_Id
:= Parent
(N
);
8282 L
: constant Node_Id
:= Left_Opnd
(P
);
8283 R
: constant Node_Id
:= Right_Opnd
(P
);
8286 if (Nkind
(P
) = N_Op_Multiply
8288 ((Is_Integer_Type
(Etype
(L
)) and then R
= N
)
8290 (Is_Integer_Type
(Etype
(R
)) and then L
= N
))
8291 and then not Do_Overflow_Check
(P
))
8294 (Nkind
(P
) = N_Op_Divide
8295 and then Is_Integer_Type
(Etype
(L
))
8296 and then Is_Unsigned_Type
(Etype
(L
))
8298 and then not Do_Overflow_Check
(P
))
8300 Set_Is_Power_Of_2_For_Shift
(N
);
8305 -- Here we just have 2 ** N on its own, so we can convert this to a
8306 -- shift node. We are prepared to deal with overflow here, and we
8307 -- also have to handle proper modular reduction for binary modular.
8316 -- Maximum shift count with no overflow
8319 -- Set True if we must test the shift count
8322 -- Node for test against TestS
8325 -- Compute maximum shift based on the underlying size. For a
8326 -- modular type this is one less than the size.
8328 if Is_Modular_Integer_Type
(Typ
) then
8330 -- For modular integer types, this is the size of the value
8331 -- being shifted minus one. Any larger values will cause
8332 -- modular reduction to a result of zero. Note that we do
8333 -- want the RM_Size here (e.g. mod 2 ** 7, we want a result
8334 -- of 6, since 2**7 should be reduced to zero).
8336 MaxS
:= RM_Size
(Rtyp
) - 1;
8338 -- For signed integer types, we use the size of the value
8339 -- being shifted minus 2. Larger values cause overflow.
8342 MaxS
:= Esize
(Rtyp
) - 2;
8345 -- Determine range to see if it can be larger than MaxS
8348 (Right_Opnd
(N
), OK
, Lo
, Hi
, Assume_Valid
=> True);
8349 TestS
:= (not OK
) or else Hi
> MaxS
;
8351 -- Signed integer case
8353 if Is_Signed_Integer_Type
(Typ
) then
8355 -- Generate overflow check if overflow is active. Note that
8356 -- we can simply ignore the possibility of overflow if the
8357 -- flag is not set (means that overflow cannot happen or
8358 -- that overflow checks are suppressed).
8360 if Ovflo
and TestS
then
8362 Make_Raise_Constraint_Error
(Loc
,
8365 Left_Opnd
=> Duplicate_Subexpr
(Right_Opnd
(N
)),
8366 Right_Opnd
=> Make_Integer_Literal
(Loc
, MaxS
)),
8367 Reason
=> CE_Overflow_Check_Failed
));
8370 -- Now rewrite node as Shift_Left (1, right-operand)
8373 Make_Op_Shift_Left
(Loc
,
8374 Left_Opnd
=> Make_Integer_Literal
(Loc
, Uint_1
),
8375 Right_Opnd
=> Right_Opnd
(N
)));
8377 -- Modular integer case
8379 else pragma Assert
(Is_Modular_Integer_Type
(Typ
));
8381 -- If shift count can be greater than MaxS, we need to wrap
8382 -- the shift in a test that will reduce the result value to
8383 -- zero if this shift count is exceeded.
8387 -- Note: build node for the comparison first, before we
8388 -- reuse the Right_Opnd, so that we have proper parents
8389 -- in place for the Duplicate_Subexpr call.
8393 Left_Opnd
=> Duplicate_Subexpr
(Right_Opnd
(N
)),
8394 Right_Opnd
=> Make_Integer_Literal
(Loc
, MaxS
));
8397 Make_If_Expression
(Loc
,
8398 Expressions
=> New_List
(
8400 Make_Integer_Literal
(Loc
, Uint_0
),
8401 Make_Op_Shift_Left
(Loc
,
8402 Left_Opnd
=> Make_Integer_Literal
(Loc
, Uint_1
),
8403 Right_Opnd
=> Right_Opnd
(N
)))));
8405 -- If we know shift count cannot be greater than MaxS, then
8406 -- it is safe to just rewrite as a shift with no test.
8410 Make_Op_Shift_Left
(Loc
,
8411 Left_Opnd
=> Make_Integer_Literal
(Loc
, Uint_1
),
8412 Right_Opnd
=> Right_Opnd
(N
)));
8416 Analyze_And_Resolve
(N
, Typ
);
8422 -- Fall through if exponentiation must be done using a runtime routine
8424 -- First deal with modular case
8426 if Is_Modular_Integer_Type
(Rtyp
) then
8428 -- Nonbinary modular case, we call the special exponentiation
8429 -- routine for the nonbinary case, converting the argument to
8430 -- Long_Long_Integer and passing the modulus value. Then the
8431 -- result is converted back to the base type.
8433 if Non_Binary_Modulus
(Rtyp
) then
8436 Make_Function_Call
(Loc
,
8438 New_Occurrence_Of
(RTE
(RE_Exp_Modular
), Loc
),
8439 Parameter_Associations
=> New_List
(
8440 Convert_To
(RTE
(RE_Unsigned
), Base
),
8441 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
)),
8444 -- Binary modular case, in this case, we call one of two routines,
8445 -- either the unsigned integer case, or the unsigned long long
8446 -- integer case, with a final "and" operation to do the required mod.
8449 if UI_To_Int
(Esize
(Rtyp
)) <= Standard_Integer_Size
then
8450 Ent
:= RTE
(RE_Exp_Unsigned
);
8452 Ent
:= RTE
(RE_Exp_Long_Long_Unsigned
);
8459 Make_Function_Call
(Loc
,
8460 Name
=> New_Occurrence_Of
(Ent
, Loc
),
8461 Parameter_Associations
=> New_List
(
8462 Convert_To
(Etype
(First_Formal
(Ent
)), Base
),
8465 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
) - 1))));
8469 -- Common exit point for modular type case
8471 Analyze_And_Resolve
(N
, Typ
);
8474 -- Signed integer cases, done using either Integer or Long_Long_Integer.
8475 -- It is not worth having routines for Short_[Short_]Integer, since for
8476 -- most machines it would not help, and it would generate more code that
8477 -- might need certification when a certified run time is required.
8479 -- In the integer cases, we have two routines, one for when overflow
8480 -- checks are required, and one when they are not required, since there
8481 -- is a real gain in omitting checks on many machines.
8483 elsif Rtyp
= Base_Type
(Standard_Long_Long_Integer
)
8484 or else (Rtyp
= Base_Type
(Standard_Long_Integer
)
8486 Esize
(Standard_Long_Integer
) > Esize
(Standard_Integer
))
8487 or else Rtyp
= Universal_Integer
8489 Etyp
:= Standard_Long_Long_Integer
;
8492 Rent
:= RE_Exp_Long_Long_Integer
;
8494 Rent
:= RE_Exn_Long_Long_Integer
;
8497 elsif Is_Signed_Integer_Type
(Rtyp
) then
8498 Etyp
:= Standard_Integer
;
8501 Rent
:= RE_Exp_Integer
;
8503 Rent
:= RE_Exn_Integer
;
8506 -- Floating-point cases. We do not need separate routines for the
8507 -- overflow case here, since in the case of floating-point, we generate
8508 -- infinities anyway as a rule (either that or we automatically trap
8509 -- overflow), and if there is an infinity generated and a range check
8510 -- is required, the check will fail anyway.
8512 -- Historical note: we used to convert everything to Long_Long_Float
8513 -- and call a single common routine, but this had the undesirable effect
8514 -- of giving different results for small static exponent values and the
8515 -- same dynamic values.
8518 pragma Assert
(Is_Floating_Point_Type
(Rtyp
));
8520 if Rtyp
= Standard_Float
then
8521 Etyp
:= Standard_Float
;
8522 Rent
:= RE_Exn_Float
;
8524 elsif Rtyp
= Standard_Long_Float
then
8525 Etyp
:= Standard_Long_Float
;
8526 Rent
:= RE_Exn_Long_Float
;
8529 Etyp
:= Standard_Long_Long_Float
;
8530 Rent
:= RE_Exn_Long_Long_Float
;
8534 -- Common processing for integer cases and floating-point cases.
8535 -- If we are in the right type, we can call runtime routine directly
8538 and then Rtyp
/= Universal_Integer
8539 and then Rtyp
/= Universal_Real
8543 Make_Function_Call
(Loc
,
8544 Name
=> New_Occurrence_Of
(RTE
(Rent
), Loc
),
8545 Parameter_Associations
=> New_List
(Base
, Exp
))));
8547 -- Otherwise we have to introduce conversions (conversions are also
8548 -- required in the universal cases, since the runtime routine is
8549 -- typed using one of the standard types).
8554 Make_Function_Call
(Loc
,
8555 Name
=> New_Occurrence_Of
(RTE
(Rent
), Loc
),
8556 Parameter_Associations
=> New_List
(
8557 Convert_To
(Etyp
, Base
),
8561 Analyze_And_Resolve
(N
, Typ
);
8565 when RE_Not_Available
=>
8567 end Expand_N_Op_Expon
;
8569 --------------------
8570 -- Expand_N_Op_Ge --
8571 --------------------
8573 procedure Expand_N_Op_Ge
(N
: Node_Id
) is
8574 Typ
: constant Entity_Id
:= Etype
(N
);
8575 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8576 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8577 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8580 Binary_Op_Validity_Checks
(N
);
8582 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8583 -- means we no longer have a comparison operation, we are all done.
8585 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8587 if Nkind
(N
) /= N_Op_Ge
then
8593 if Is_Array_Type
(Typ1
) then
8594 Expand_Array_Comparison
(N
);
8598 -- Deal with boolean operands
8600 if Is_Boolean_Type
(Typ1
) then
8601 Adjust_Condition
(Op1
);
8602 Adjust_Condition
(Op2
);
8603 Set_Etype
(N
, Standard_Boolean
);
8604 Adjust_Result_Type
(N
, Typ
);
8607 Rewrite_Comparison
(N
);
8609 Optimize_Length_Comparison
(N
);
8612 --------------------
8613 -- Expand_N_Op_Gt --
8614 --------------------
8616 procedure Expand_N_Op_Gt
(N
: Node_Id
) is
8617 Typ
: constant Entity_Id
:= Etype
(N
);
8618 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8619 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8620 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8623 Binary_Op_Validity_Checks
(N
);
8625 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8626 -- means we no longer have a comparison operation, we are all done.
8628 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8630 if Nkind
(N
) /= N_Op_Gt
then
8634 -- Deal with array type operands
8636 if Is_Array_Type
(Typ1
) then
8637 Expand_Array_Comparison
(N
);
8641 -- Deal with boolean type operands
8643 if Is_Boolean_Type
(Typ1
) then
8644 Adjust_Condition
(Op1
);
8645 Adjust_Condition
(Op2
);
8646 Set_Etype
(N
, Standard_Boolean
);
8647 Adjust_Result_Type
(N
, Typ
);
8650 Rewrite_Comparison
(N
);
8652 Optimize_Length_Comparison
(N
);
8655 --------------------
8656 -- Expand_N_Op_Le --
8657 --------------------
8659 procedure Expand_N_Op_Le
(N
: Node_Id
) is
8660 Typ
: constant Entity_Id
:= Etype
(N
);
8661 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8662 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8663 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8666 Binary_Op_Validity_Checks
(N
);
8668 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8669 -- means we no longer have a comparison operation, we are all done.
8671 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8673 if Nkind
(N
) /= N_Op_Le
then
8677 -- Deal with array type operands
8679 if Is_Array_Type
(Typ1
) then
8680 Expand_Array_Comparison
(N
);
8684 -- Deal with Boolean type operands
8686 if Is_Boolean_Type
(Typ1
) then
8687 Adjust_Condition
(Op1
);
8688 Adjust_Condition
(Op2
);
8689 Set_Etype
(N
, Standard_Boolean
);
8690 Adjust_Result_Type
(N
, Typ
);
8693 Rewrite_Comparison
(N
);
8695 Optimize_Length_Comparison
(N
);
8698 --------------------
8699 -- Expand_N_Op_Lt --
8700 --------------------
8702 procedure Expand_N_Op_Lt
(N
: Node_Id
) is
8703 Typ
: constant Entity_Id
:= Etype
(N
);
8704 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8705 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8706 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8709 Binary_Op_Validity_Checks
(N
);
8711 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8712 -- means we no longer have a comparison operation, we are all done.
8714 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8716 if Nkind
(N
) /= N_Op_Lt
then
8720 -- Deal with array type operands
8722 if Is_Array_Type
(Typ1
) then
8723 Expand_Array_Comparison
(N
);
8727 -- Deal with Boolean type operands
8729 if Is_Boolean_Type
(Typ1
) then
8730 Adjust_Condition
(Op1
);
8731 Adjust_Condition
(Op2
);
8732 Set_Etype
(N
, Standard_Boolean
);
8733 Adjust_Result_Type
(N
, Typ
);
8736 Rewrite_Comparison
(N
);
8738 Optimize_Length_Comparison
(N
);
8741 -----------------------
8742 -- Expand_N_Op_Minus --
8743 -----------------------
8745 procedure Expand_N_Op_Minus
(N
: Node_Id
) is
8746 Loc
: constant Source_Ptr
:= Sloc
(N
);
8747 Typ
: constant Entity_Id
:= Etype
(N
);
8750 Unary_Op_Validity_Checks
(N
);
8752 -- Check for MINIMIZED/ELIMINATED overflow mode
8754 if Minimized_Eliminated_Overflow_Check
(N
) then
8755 Apply_Arithmetic_Overflow_Check
(N
);
8759 if not Backend_Overflow_Checks_On_Target
8760 and then Is_Signed_Integer_Type
(Etype
(N
))
8761 and then Do_Overflow_Check
(N
)
8763 -- Software overflow checking expands -expr into (0 - expr)
8766 Make_Op_Subtract
(Loc
,
8767 Left_Opnd
=> Make_Integer_Literal
(Loc
, 0),
8768 Right_Opnd
=> Right_Opnd
(N
)));
8770 Analyze_And_Resolve
(N
, Typ
);
8773 Expand_Nonbinary_Modular_Op
(N
);
8774 end Expand_N_Op_Minus
;
8776 ---------------------
8777 -- Expand_N_Op_Mod --
8778 ---------------------
8780 procedure Expand_N_Op_Mod
(N
: Node_Id
) is
8781 Loc
: constant Source_Ptr
:= Sloc
(N
);
8782 Typ
: constant Entity_Id
:= Etype
(N
);
8783 DDC
: constant Boolean := Do_Division_Check
(N
);
8796 pragma Warnings
(Off
, Lhi
);
8799 Binary_Op_Validity_Checks
(N
);
8801 -- Check for MINIMIZED/ELIMINATED overflow mode
8803 if Minimized_Eliminated_Overflow_Check
(N
) then
8804 Apply_Arithmetic_Overflow_Check
(N
);
8808 if Is_Integer_Type
(Etype
(N
)) then
8809 Apply_Divide_Checks
(N
);
8811 -- All done if we don't have a MOD any more, which can happen as a
8812 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8814 if Nkind
(N
) /= N_Op_Mod
then
8819 -- Proceed with expansion of mod operator
8821 Left
:= Left_Opnd
(N
);
8822 Right
:= Right_Opnd
(N
);
8824 Determine_Range
(Right
, ROK
, Rlo
, Rhi
, Assume_Valid
=> True);
8825 Determine_Range
(Left
, LOK
, Llo
, Lhi
, Assume_Valid
=> True);
8827 -- Convert mod to rem if operands are both known to be non-negative, or
8828 -- both known to be non-positive (these are the cases in which rem and
8829 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
8830 -- likely that this will improve the quality of code, (the operation now
8831 -- corresponds to the hardware remainder), and it does not seem likely
8832 -- that it could be harmful. It also avoids some cases of the elaborate
8833 -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %).
8836 and then ((Llo
>= 0 and then Rlo
>= 0)
8838 (Lhi
<= 0 and then Rhi
<= 0))
8841 Make_Op_Rem
(Sloc
(N
),
8842 Left_Opnd
=> Left_Opnd
(N
),
8843 Right_Opnd
=> Right_Opnd
(N
)));
8845 -- Instead of reanalyzing the node we do the analysis manually. This
8846 -- avoids anomalies when the replacement is done in an instance and
8847 -- is epsilon more efficient.
8849 Set_Entity
(N
, Standard_Entity
(S_Op_Rem
));
8851 Set_Do_Division_Check
(N
, DDC
);
8852 Expand_N_Op_Rem
(N
);
8856 -- Otherwise, normal mod processing
8859 -- Apply optimization x mod 1 = 0. We don't really need that with
8860 -- gcc, but it is useful with other back ends and is certainly
8863 if Is_Integer_Type
(Etype
(N
))
8864 and then Compile_Time_Known_Value
(Right
)
8865 and then Expr_Value
(Right
) = Uint_1
8867 -- Call Remove_Side_Effects to ensure that any side effects in
8868 -- the ignored left operand (in particular function calls to
8869 -- user defined functions) are properly executed.
8871 Remove_Side_Effects
(Left
);
8873 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
8874 Analyze_And_Resolve
(N
, Typ
);
8878 -- If we still have a mod operator and we are in Modify_Tree_For_C
8879 -- mode, and we have a signed integer type, then here is where we do
8880 -- the rewrite in terms of Rem. Note this rewrite bypasses the need
8881 -- for the special handling of the annoying case of largest negative
8882 -- number mod minus one.
8884 if Nkind
(N
) = N_Op_Mod
8885 and then Is_Signed_Integer_Type
(Typ
)
8886 and then Modify_Tree_For_C
8888 -- In the general case, we expand A mod B as
8890 -- Tnn : constant typ := A rem B;
8892 -- (if (A >= 0) = (B >= 0) then Tnn
8893 -- elsif Tnn = 0 then 0
8896 -- The comparison can be written simply as A >= 0 if we know that
8897 -- B >= 0 which is a very common case.
8899 -- An important optimization is when B is known at compile time
8900 -- to be 2**K for some constant. In this case we can simply AND
8901 -- the left operand with the bit string 2**K-1 (i.e. K 1-bits)
8902 -- and that works for both the positive and negative cases.
8905 P2
: constant Nat
:= Power_Of_Two
(Right
);
8910 Unchecked_Convert_To
(Typ
,
8913 Unchecked_Convert_To
8914 (Corresponding_Unsigned_Type
(Typ
), Left
),
8916 Make_Integer_Literal
(Loc
, 2 ** P2
- 1))));
8917 Analyze_And_Resolve
(N
, Typ
);
8922 -- Here for the full rewrite
8925 Tnn
: constant Entity_Id
:= Make_Temporary
(Sloc
(N
), 'T', N
);
8931 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left
),
8932 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
8934 if not LOK
or else Rlo
< 0 then
8940 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Right
),
8941 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)));
8945 Make_Object_Declaration
(Loc
,
8946 Defining_Identifier
=> Tnn
,
8947 Constant_Present
=> True,
8948 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
8952 Right_Opnd
=> Right
)));
8955 Make_If_Expression
(Loc
,
8956 Expressions
=> New_List
(
8958 New_Occurrence_Of
(Tnn
, Loc
),
8959 Make_If_Expression
(Loc
,
8961 Expressions
=> New_List
(
8963 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
8964 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
8965 Make_Integer_Literal
(Loc
, 0),
8967 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
8969 Duplicate_Subexpr_No_Checks
(Right
)))))));
8971 Analyze_And_Resolve
(N
, Typ
);
8976 -- Deal with annoying case of largest negative number mod minus one.
8977 -- Gigi may not handle this case correctly, because on some targets,
8978 -- the mod value is computed using a divide instruction which gives
8979 -- an overflow trap for this case.
8981 -- It would be a bit more efficient to figure out which targets
8982 -- this is really needed for, but in practice it is reasonable
8983 -- to do the following special check in all cases, since it means
8984 -- we get a clearer message, and also the overhead is minimal given
8985 -- that division is expensive in any case.
8987 -- In fact the check is quite easy, if the right operand is -1, then
8988 -- the mod value is always 0, and we can just ignore the left operand
8989 -- completely in this case.
8991 -- This only applies if we still have a mod operator. Skip if we
8992 -- have already rewritten this (e.g. in the case of eliminated
8993 -- overflow checks which have driven us into bignum mode).
8995 if Nkind
(N
) = N_Op_Mod
then
8997 -- The operand type may be private (e.g. in the expansion of an
8998 -- intrinsic operation) so we must use the underlying type to get
8999 -- the bounds, and convert the literals explicitly.
9003 (Type_Low_Bound
(Base_Type
(Underlying_Type
(Etype
(Left
)))));
9005 if ((not ROK
) or else (Rlo
<= (-1) and then (-1) <= Rhi
))
9006 and then ((not LOK
) or else (Llo
= LLB
))
9009 Make_If_Expression
(Loc
,
9010 Expressions
=> New_List
(
9012 Left_Opnd
=> Duplicate_Subexpr
(Right
),
9014 Unchecked_Convert_To
(Typ
,
9015 Make_Integer_Literal
(Loc
, -1))),
9016 Unchecked_Convert_To
(Typ
,
9017 Make_Integer_Literal
(Loc
, Uint_0
)),
9018 Relocate_Node
(N
))));
9020 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
9021 Analyze_And_Resolve
(N
, Typ
);
9025 end Expand_N_Op_Mod
;
9027 --------------------------
9028 -- Expand_N_Op_Multiply --
9029 --------------------------
9031 procedure Expand_N_Op_Multiply
(N
: Node_Id
) is
9032 Loc
: constant Source_Ptr
:= Sloc
(N
);
9033 Lop
: constant Node_Id
:= Left_Opnd
(N
);
9034 Rop
: constant Node_Id
:= Right_Opnd
(N
);
9036 Lp2
: constant Boolean :=
9037 Nkind
(Lop
) = N_Op_Expon
and then Is_Power_Of_2_For_Shift
(Lop
);
9038 Rp2
: constant Boolean :=
9039 Nkind
(Rop
) = N_Op_Expon
and then Is_Power_Of_2_For_Shift
(Rop
);
9041 Ltyp
: constant Entity_Id
:= Etype
(Lop
);
9042 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
9043 Typ
: Entity_Id
:= Etype
(N
);
9046 Binary_Op_Validity_Checks
(N
);
9048 -- Check for MINIMIZED/ELIMINATED overflow mode
9050 if Minimized_Eliminated_Overflow_Check
(N
) then
9051 Apply_Arithmetic_Overflow_Check
(N
);
9055 -- Special optimizations for integer types
9057 if Is_Integer_Type
(Typ
) then
9059 -- N * 0 = 0 for integer types
9061 if Compile_Time_Known_Value
(Rop
)
9062 and then Expr_Value
(Rop
) = Uint_0
9064 -- Call Remove_Side_Effects to ensure that any side effects in
9065 -- the ignored left operand (in particular function calls to
9066 -- user defined functions) are properly executed.
9068 Remove_Side_Effects
(Lop
);
9070 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
9071 Analyze_And_Resolve
(N
, Typ
);
9075 -- Similar handling for 0 * N = 0
9077 if Compile_Time_Known_Value
(Lop
)
9078 and then Expr_Value
(Lop
) = Uint_0
9080 Remove_Side_Effects
(Rop
);
9081 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
9082 Analyze_And_Resolve
(N
, Typ
);
9086 -- N * 1 = 1 * N = N for integer types
9088 -- This optimisation is not done if we are going to
9089 -- rewrite the product 1 * 2 ** N to a shift.
9091 if Compile_Time_Known_Value
(Rop
)
9092 and then Expr_Value
(Rop
) = Uint_1
9098 elsif Compile_Time_Known_Value
(Lop
)
9099 and then Expr_Value
(Lop
) = Uint_1
9107 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
9108 -- Is_Power_Of_2_For_Shift is set means that we know that our left
9109 -- operand is an integer, as required for this to work.
9114 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
9118 Left_Opnd
=> Make_Integer_Literal
(Loc
, 2),
9121 Left_Opnd
=> Right_Opnd
(Lop
),
9122 Right_Opnd
=> Right_Opnd
(Rop
))));
9123 Analyze_And_Resolve
(N
, Typ
);
9127 -- If the result is modular, perform the reduction of the result
9130 if Is_Modular_Integer_Type
(Typ
)
9131 and then not Non_Binary_Modulus
(Typ
)
9136 Make_Op_Shift_Left
(Loc
,
9139 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))),
9141 Make_Integer_Literal
(Loc
, Modulus
(Typ
) - 1)));
9145 Make_Op_Shift_Left
(Loc
,
9148 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))));
9151 Analyze_And_Resolve
(N
, Typ
);
9155 -- Same processing for the operands the other way round
9158 if Is_Modular_Integer_Type
(Typ
)
9159 and then not Non_Binary_Modulus
(Typ
)
9164 Make_Op_Shift_Left
(Loc
,
9167 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))),
9169 Make_Integer_Literal
(Loc
, Modulus
(Typ
) - 1)));
9173 Make_Op_Shift_Left
(Loc
,
9176 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))));
9179 Analyze_And_Resolve
(N
, Typ
);
9183 -- Do required fixup of universal fixed operation
9185 if Typ
= Universal_Fixed
then
9186 Fixup_Universal_Fixed_Operation
(N
);
9190 -- Multiplications with fixed-point results
9192 if Is_Fixed_Point_Type
(Typ
) then
9194 -- No special processing if Treat_Fixed_As_Integer is set, since from
9195 -- a semantic point of view such operations are simply integer
9196 -- operations and will be treated that way.
9198 if not Treat_Fixed_As_Integer
(N
) then
9200 -- Case of fixed * integer => fixed
9202 if Is_Integer_Type
(Rtyp
) then
9203 Expand_Multiply_Fixed_By_Integer_Giving_Fixed
(N
);
9205 -- Case of integer * fixed => fixed
9207 elsif Is_Integer_Type
(Ltyp
) then
9208 Expand_Multiply_Integer_By_Fixed_Giving_Fixed
(N
);
9210 -- Case of fixed * fixed => fixed
9213 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed
(N
);
9217 -- Other cases of multiplication of fixed-point operands. Again we
9218 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
9220 elsif (Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
))
9221 and then not Treat_Fixed_As_Integer
(N
)
9223 if Is_Integer_Type
(Typ
) then
9224 Expand_Multiply_Fixed_By_Fixed_Giving_Integer
(N
);
9226 pragma Assert
(Is_Floating_Point_Type
(Typ
));
9227 Expand_Multiply_Fixed_By_Fixed_Giving_Float
(N
);
9230 -- Mixed-mode operations can appear in a non-static universal context,
9231 -- in which case the integer argument must be converted explicitly.
9233 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Rtyp
) then
9234 Rewrite
(Rop
, Convert_To
(Universal_Real
, Relocate_Node
(Rop
)));
9235 Analyze_And_Resolve
(Rop
, Universal_Real
);
9237 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Ltyp
) then
9238 Rewrite
(Lop
, Convert_To
(Universal_Real
, Relocate_Node
(Lop
)));
9239 Analyze_And_Resolve
(Lop
, Universal_Real
);
9241 -- Non-fixed point cases, check software overflow checking required
9243 elsif Is_Signed_Integer_Type
(Etype
(N
)) then
9244 Apply_Arithmetic_Overflow_Check
(N
);
9247 -- Overflow checks for floating-point if -gnateF mode active
9249 Check_Float_Op_Overflow
(N
);
9251 Expand_Nonbinary_Modular_Op
(N
);
9252 end Expand_N_Op_Multiply
;
9254 --------------------
9255 -- Expand_N_Op_Ne --
9256 --------------------
9258 procedure Expand_N_Op_Ne
(N
: Node_Id
) is
9259 Typ
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
9262 -- Case of elementary type with standard operator
9264 if Is_Elementary_Type
(Typ
)
9265 and then Sloc
(Entity
(N
)) = Standard_Location
9267 Binary_Op_Validity_Checks
(N
);
9269 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
9270 -- means we no longer have a /= operation, we are all done.
9272 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
9274 if Nkind
(N
) /= N_Op_Ne
then
9278 -- Boolean types (requiring handling of non-standard case)
9280 if Is_Boolean_Type
(Typ
) then
9281 Adjust_Condition
(Left_Opnd
(N
));
9282 Adjust_Condition
(Right_Opnd
(N
));
9283 Set_Etype
(N
, Standard_Boolean
);
9284 Adjust_Result_Type
(N
, Typ
);
9287 Rewrite_Comparison
(N
);
9289 -- For all cases other than elementary types, we rewrite node as the
9290 -- negation of an equality operation, and reanalyze. The equality to be
9291 -- used is defined in the same scope and has the same signature. This
9292 -- signature must be set explicitly since in an instance it may not have
9293 -- the same visibility as in the generic unit. This avoids duplicating
9294 -- or factoring the complex code for record/array equality tests etc.
9296 -- This case is also used for the minimal expansion performed in
9301 Loc
: constant Source_Ptr
:= Sloc
(N
);
9303 Ne
: constant Entity_Id
:= Entity
(N
);
9306 Binary_Op_Validity_Checks
(N
);
9312 Left_Opnd
=> Left_Opnd
(N
),
9313 Right_Opnd
=> Right_Opnd
(N
)));
9315 -- The level of parentheses is useless in GNATprove mode, and
9316 -- bumping its level here leads to wrong columns being used in
9317 -- check messages, hence skip it in this mode.
9319 if not GNATprove_Mode
then
9320 Set_Paren_Count
(Right_Opnd
(Neg
), 1);
9323 if Scope
(Ne
) /= Standard_Standard
then
9324 Set_Entity
(Right_Opnd
(Neg
), Corresponding_Equality
(Ne
));
9327 -- For navigation purposes, we want to treat the inequality as an
9328 -- implicit reference to the corresponding equality. Preserve the
9329 -- Comes_From_ source flag to generate proper Xref entries.
9331 Preserve_Comes_From_Source
(Neg
, N
);
9332 Preserve_Comes_From_Source
(Right_Opnd
(Neg
), N
);
9334 Analyze_And_Resolve
(N
, Standard_Boolean
);
9338 -- No need for optimization in GNATprove mode, where we would rather see
9339 -- the original source expression.
9341 if not GNATprove_Mode
then
9342 Optimize_Length_Comparison
(N
);
9346 ---------------------
9347 -- Expand_N_Op_Not --
9348 ---------------------
9350 -- If the argument is other than a Boolean array type, there is no special
9351 -- expansion required, except for dealing with validity checks, and non-
9352 -- standard boolean representations.
9354 -- For the packed array case, we call the special routine in Exp_Pakd,
9355 -- except that if the component size is greater than one, we use the
9356 -- standard routine generating a gruesome loop (it is so peculiar to have
9357 -- packed arrays with non-standard Boolean representations anyway, so it
9358 -- does not matter that we do not handle this case efficiently).
9360 -- For the unpacked array case (and for the special packed case where we
9361 -- have non standard Booleans, as discussed above), we generate and insert
9362 -- into the tree the following function definition:
9364 -- function Nnnn (A : arr) is
9367 -- for J in a'range loop
9368 -- B (J) := not A (J);
9373 -- Here arr is the actual subtype of the parameter (and hence always
9374 -- constrained). Then we replace the not with a call to this function.
9376 procedure Expand_N_Op_Not
(N
: Node_Id
) is
9377 Loc
: constant Source_Ptr
:= Sloc
(N
);
9378 Typ
: constant Entity_Id
:= Etype
(N
);
9387 Func_Name
: Entity_Id
;
9388 Loop_Statement
: Node_Id
;
9391 Unary_Op_Validity_Checks
(N
);
9393 -- For boolean operand, deal with non-standard booleans
9395 if Is_Boolean_Type
(Typ
) then
9396 Adjust_Condition
(Right_Opnd
(N
));
9397 Set_Etype
(N
, Standard_Boolean
);
9398 Adjust_Result_Type
(N
, Typ
);
9402 -- Only array types need any other processing
9404 if not Is_Array_Type
(Typ
) then
9408 -- Case of array operand. If bit packed with a component size of 1,
9409 -- handle it in Exp_Pakd if the operand is known to be aligned.
9411 if Is_Bit_Packed_Array
(Typ
)
9412 and then Component_Size
(Typ
) = 1
9413 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
9415 Expand_Packed_Not
(N
);
9419 -- Case of array operand which is not bit-packed. If the context is
9420 -- a safe assignment, call in-place operation, If context is a larger
9421 -- boolean expression in the context of a safe assignment, expansion is
9422 -- done by enclosing operation.
9424 Opnd
:= Relocate_Node
(Right_Opnd
(N
));
9425 Convert_To_Actual_Subtype
(Opnd
);
9426 Arr
:= Etype
(Opnd
);
9427 Ensure_Defined
(Arr
, N
);
9428 Silly_Boolean_Array_Not_Test
(N
, Arr
);
9430 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
9431 if Safe_In_Place_Array_Op
(Name
(Parent
(N
)), N
, Empty
) then
9432 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
9435 -- Special case the negation of a binary operation
9437 elsif Nkind_In
(Opnd
, N_Op_And
, N_Op_Or
, N_Op_Xor
)
9438 and then Safe_In_Place_Array_Op
9439 (Name
(Parent
(N
)), Left_Opnd
(Opnd
), Right_Opnd
(Opnd
))
9441 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
9445 elsif Nkind
(Parent
(N
)) in N_Binary_Op
9446 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
9449 Op1
: constant Node_Id
:= Left_Opnd
(Parent
(N
));
9450 Op2
: constant Node_Id
:= Right_Opnd
(Parent
(N
));
9451 Lhs
: constant Node_Id
:= Name
(Parent
(Parent
(N
)));
9454 if Safe_In_Place_Array_Op
(Lhs
, Op1
, Op2
) then
9456 -- (not A) op (not B) can be reduced to a single call
9458 if N
= Op1
and then Nkind
(Op2
) = N_Op_Not
then
9461 elsif N
= Op2
and then Nkind
(Op1
) = N_Op_Not
then
9464 -- A xor (not B) can also be special-cased
9466 elsif N
= Op2
and then Nkind
(Parent
(N
)) = N_Op_Xor
then
9473 A
:= Make_Defining_Identifier
(Loc
, Name_uA
);
9474 B
:= Make_Defining_Identifier
(Loc
, Name_uB
);
9475 J
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
9478 Make_Indexed_Component
(Loc
,
9479 Prefix
=> New_Occurrence_Of
(A
, Loc
),
9480 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
9483 Make_Indexed_Component
(Loc
,
9484 Prefix
=> New_Occurrence_Of
(B
, Loc
),
9485 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
9488 Make_Implicit_Loop_Statement
(N
,
9489 Identifier
=> Empty
,
9492 Make_Iteration_Scheme
(Loc
,
9493 Loop_Parameter_Specification
=>
9494 Make_Loop_Parameter_Specification
(Loc
,
9495 Defining_Identifier
=> J
,
9496 Discrete_Subtype_Definition
=>
9497 Make_Attribute_Reference
(Loc
,
9498 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
9499 Attribute_Name
=> Name_Range
))),
9501 Statements
=> New_List
(
9502 Make_Assignment_Statement
(Loc
,
9504 Expression
=> Make_Op_Not
(Loc
, A_J
))));
9506 Func_Name
:= Make_Temporary
(Loc
, 'N');
9507 Set_Is_Inlined
(Func_Name
);
9510 Make_Subprogram_Body
(Loc
,
9512 Make_Function_Specification
(Loc
,
9513 Defining_Unit_Name
=> Func_Name
,
9514 Parameter_Specifications
=> New_List
(
9515 Make_Parameter_Specification
(Loc
,
9516 Defining_Identifier
=> A
,
9517 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
))),
9518 Result_Definition
=> New_Occurrence_Of
(Typ
, Loc
)),
9520 Declarations
=> New_List
(
9521 Make_Object_Declaration
(Loc
,
9522 Defining_Identifier
=> B
,
9523 Object_Definition
=> New_Occurrence_Of
(Arr
, Loc
))),
9525 Handled_Statement_Sequence
=>
9526 Make_Handled_Sequence_Of_Statements
(Loc
,
9527 Statements
=> New_List
(
9529 Make_Simple_Return_Statement
(Loc
,
9530 Expression
=> Make_Identifier
(Loc
, Chars
(B
)))))));
9533 Make_Function_Call
(Loc
,
9534 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
9535 Parameter_Associations
=> New_List
(Opnd
)));
9537 Analyze_And_Resolve
(N
, Typ
);
9538 end Expand_N_Op_Not
;
9540 --------------------
9541 -- Expand_N_Op_Or --
9542 --------------------
9544 procedure Expand_N_Op_Or
(N
: Node_Id
) is
9545 Typ
: constant Entity_Id
:= Etype
(N
);
9548 Binary_Op_Validity_Checks
(N
);
9550 if Is_Array_Type
(Etype
(N
)) then
9551 Expand_Boolean_Operator
(N
);
9553 elsif Is_Boolean_Type
(Etype
(N
)) then
9554 Adjust_Condition
(Left_Opnd
(N
));
9555 Adjust_Condition
(Right_Opnd
(N
));
9556 Set_Etype
(N
, Standard_Boolean
);
9557 Adjust_Result_Type
(N
, Typ
);
9559 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
9560 Expand_Intrinsic_Call
(N
, Entity
(N
));
9563 Expand_Nonbinary_Modular_Op
(N
);
9566 ----------------------
9567 -- Expand_N_Op_Plus --
9568 ----------------------
9570 procedure Expand_N_Op_Plus
(N
: Node_Id
) is
9572 Unary_Op_Validity_Checks
(N
);
9574 -- Check for MINIMIZED/ELIMINATED overflow mode
9576 if Minimized_Eliminated_Overflow_Check
(N
) then
9577 Apply_Arithmetic_Overflow_Check
(N
);
9580 end Expand_N_Op_Plus
;
9582 ---------------------
9583 -- Expand_N_Op_Rem --
9584 ---------------------
9586 procedure Expand_N_Op_Rem
(N
: Node_Id
) is
9587 Loc
: constant Source_Ptr
:= Sloc
(N
);
9588 Typ
: constant Entity_Id
:= Etype
(N
);
9599 -- Set if corresponding operand can be negative
9601 pragma Unreferenced
(Hi
);
9604 Binary_Op_Validity_Checks
(N
);
9606 -- Check for MINIMIZED/ELIMINATED overflow mode
9608 if Minimized_Eliminated_Overflow_Check
(N
) then
9609 Apply_Arithmetic_Overflow_Check
(N
);
9613 if Is_Integer_Type
(Etype
(N
)) then
9614 Apply_Divide_Checks
(N
);
9616 -- All done if we don't have a REM any more, which can happen as a
9617 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
9619 if Nkind
(N
) /= N_Op_Rem
then
9624 -- Proceed with expansion of REM
9626 Left
:= Left_Opnd
(N
);
9627 Right
:= Right_Opnd
(N
);
9629 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
9630 -- but it is useful with other back ends, and is certainly harmless.
9632 if Is_Integer_Type
(Etype
(N
))
9633 and then Compile_Time_Known_Value
(Right
)
9634 and then Expr_Value
(Right
) = Uint_1
9636 -- Call Remove_Side_Effects to ensure that any side effects in the
9637 -- ignored left operand (in particular function calls to user defined
9638 -- functions) are properly executed.
9640 Remove_Side_Effects
(Left
);
9642 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
9643 Analyze_And_Resolve
(N
, Typ
);
9647 -- Deal with annoying case of largest negative number remainder minus
9648 -- one. Gigi may not handle this case correctly, because on some
9649 -- targets, the mod value is computed using a divide instruction
9650 -- which gives an overflow trap for this case.
9652 -- It would be a bit more efficient to figure out which targets this
9653 -- is really needed for, but in practice it is reasonable to do the
9654 -- following special check in all cases, since it means we get a clearer
9655 -- message, and also the overhead is minimal given that division is
9656 -- expensive in any case.
9658 -- In fact the check is quite easy, if the right operand is -1, then
9659 -- the remainder is always 0, and we can just ignore the left operand
9660 -- completely in this case.
9662 Determine_Range
(Right
, OK
, Lo
, Hi
, Assume_Valid
=> True);
9663 Lneg
:= (not OK
) or else Lo
< 0;
9665 Determine_Range
(Left
, OK
, Lo
, Hi
, Assume_Valid
=> True);
9666 Rneg
:= (not OK
) or else Lo
< 0;
9668 -- We won't mess with trying to find out if the left operand can really
9669 -- be the largest negative number (that's a pain in the case of private
9670 -- types and this is really marginal). We will just assume that we need
9671 -- the test if the left operand can be negative at all.
9673 if Lneg
and Rneg
then
9675 Make_If_Expression
(Loc
,
9676 Expressions
=> New_List
(
9678 Left_Opnd
=> Duplicate_Subexpr
(Right
),
9680 Unchecked_Convert_To
(Typ
, Make_Integer_Literal
(Loc
, -1))),
9682 Unchecked_Convert_To
(Typ
,
9683 Make_Integer_Literal
(Loc
, Uint_0
)),
9685 Relocate_Node
(N
))));
9687 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
9688 Analyze_And_Resolve
(N
, Typ
);
9690 end Expand_N_Op_Rem
;
9692 -----------------------------
9693 -- Expand_N_Op_Rotate_Left --
9694 -----------------------------
9696 procedure Expand_N_Op_Rotate_Left
(N
: Node_Id
) is
9698 Binary_Op_Validity_Checks
(N
);
9700 -- If we are in Modify_Tree_For_C mode, there is no rotate left in C,
9701 -- so we rewrite in terms of logical shifts
9703 -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits)
9705 -- where Bits is the shift count mod Esize (the mod operation here
9706 -- deals with ludicrous large shift counts, which are apparently OK).
9708 -- What about nonbinary modulus ???
9711 Loc
: constant Source_Ptr
:= Sloc
(N
);
9712 Rtp
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
9713 Typ
: constant Entity_Id
:= Etype
(N
);
9716 if Modify_Tree_For_C
then
9717 Rewrite
(Right_Opnd
(N
),
9719 Left_Opnd
=> Relocate_Node
(Right_Opnd
(N
)),
9720 Right_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
))));
9722 Analyze_And_Resolve
(Right_Opnd
(N
), Rtp
);
9727 Make_Op_Shift_Left
(Loc
,
9728 Left_Opnd
=> Left_Opnd
(N
),
9729 Right_Opnd
=> Right_Opnd
(N
)),
9732 Make_Op_Shift_Right
(Loc
,
9733 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left_Opnd
(N
)),
9735 Make_Op_Subtract
(Loc
,
9736 Left_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
)),
9738 Duplicate_Subexpr_No_Checks
(Right_Opnd
(N
))))));
9740 Analyze_And_Resolve
(N
, Typ
);
9743 end Expand_N_Op_Rotate_Left
;
9745 ------------------------------
9746 -- Expand_N_Op_Rotate_Right --
9747 ------------------------------
9749 procedure Expand_N_Op_Rotate_Right
(N
: Node_Id
) is
9751 Binary_Op_Validity_Checks
(N
);
9753 -- If we are in Modify_Tree_For_C mode, there is no rotate right in C,
9754 -- so we rewrite in terms of logical shifts
9756 -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits)
9758 -- where Bits is the shift count mod Esize (the mod operation here
9759 -- deals with ludicrous large shift counts, which are apparently OK).
9761 -- What about nonbinary modulus ???
9764 Loc
: constant Source_Ptr
:= Sloc
(N
);
9765 Rtp
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
9766 Typ
: constant Entity_Id
:= Etype
(N
);
9769 Rewrite
(Right_Opnd
(N
),
9771 Left_Opnd
=> Relocate_Node
(Right_Opnd
(N
)),
9772 Right_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
))));
9774 Analyze_And_Resolve
(Right_Opnd
(N
), Rtp
);
9776 if Modify_Tree_For_C
then
9780 Make_Op_Shift_Right
(Loc
,
9781 Left_Opnd
=> Left_Opnd
(N
),
9782 Right_Opnd
=> Right_Opnd
(N
)),
9785 Make_Op_Shift_Left
(Loc
,
9786 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left_Opnd
(N
)),
9788 Make_Op_Subtract
(Loc
,
9789 Left_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
)),
9791 Duplicate_Subexpr_No_Checks
(Right_Opnd
(N
))))));
9793 Analyze_And_Resolve
(N
, Typ
);
9796 end Expand_N_Op_Rotate_Right
;
9798 ----------------------------
9799 -- Expand_N_Op_Shift_Left --
9800 ----------------------------
9802 -- Note: nothing in this routine depends on left as opposed to right shifts
9803 -- so we share the routine for expanding shift right operations.
9805 procedure Expand_N_Op_Shift_Left
(N
: Node_Id
) is
9807 Binary_Op_Validity_Checks
(N
);
9809 -- If we are in Modify_Tree_For_C mode, then ensure that the right
9810 -- operand is not greater than the word size (since that would not
9811 -- be defined properly by the corresponding C shift operator).
9813 if Modify_Tree_For_C
then
9815 Right
: constant Node_Id
:= Right_Opnd
(N
);
9816 Loc
: constant Source_Ptr
:= Sloc
(Right
);
9817 Typ
: constant Entity_Id
:= Etype
(N
);
9818 Siz
: constant Uint
:= Esize
(Typ
);
9825 if Compile_Time_Known_Value
(Right
) then
9826 if Expr_Value
(Right
) >= Siz
then
9827 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
9828 Analyze_And_Resolve
(N
, Typ
);
9831 -- Not compile time known, find range
9834 Determine_Range
(Right
, OK
, Lo
, Hi
, Assume_Valid
=> True);
9836 -- Nothing to do if known to be OK range, otherwise expand
9838 if not OK
or else Hi
>= Siz
then
9840 -- Prevent recursion on copy of shift node
9842 Orig
:= Relocate_Node
(N
);
9843 Set_Analyzed
(Orig
);
9845 -- Now do the rewrite
9848 Make_If_Expression
(Loc
,
9849 Expressions
=> New_List
(
9851 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Right
),
9852 Right_Opnd
=> Make_Integer_Literal
(Loc
, Siz
)),
9853 Make_Integer_Literal
(Loc
, 0),
9855 Analyze_And_Resolve
(N
, Typ
);
9860 end Expand_N_Op_Shift_Left
;
9862 -----------------------------
9863 -- Expand_N_Op_Shift_Right --
9864 -----------------------------
9866 procedure Expand_N_Op_Shift_Right
(N
: Node_Id
) is
9868 -- Share shift left circuit
9870 Expand_N_Op_Shift_Left
(N
);
9871 end Expand_N_Op_Shift_Right
;
9873 ----------------------------------------
9874 -- Expand_N_Op_Shift_Right_Arithmetic --
9875 ----------------------------------------
9877 procedure Expand_N_Op_Shift_Right_Arithmetic
(N
: Node_Id
) is
9879 Binary_Op_Validity_Checks
(N
);
9881 -- If we are in Modify_Tree_For_C mode, there is no shift right
9882 -- arithmetic in C, so we rewrite in terms of logical shifts.
9884 -- Shift_Right (Num, Bits) or
9886 -- then not (Shift_Right (Mask, bits))
9889 -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1)
9891 -- Note: in almost all C compilers it would work to just shift a
9892 -- signed integer right, but it's undefined and we cannot rely on it.
9894 -- Note: the above works fine for shift counts greater than or equal
9895 -- to the word size, since in this case (not (Shift_Right (Mask, bits)))
9896 -- generates all 1'bits.
9898 -- What about nonbinary modulus ???
9901 Loc
: constant Source_Ptr
:= Sloc
(N
);
9902 Typ
: constant Entity_Id
:= Etype
(N
);
9903 Sign
: constant Uint
:= 2 ** (Esize
(Typ
) - 1);
9904 Mask
: constant Uint
:= (2 ** Esize
(Typ
)) - 1;
9905 Left
: constant Node_Id
:= Left_Opnd
(N
);
9906 Right
: constant Node_Id
:= Right_Opnd
(N
);
9910 if Modify_Tree_For_C
then
9912 -- Here if not (Shift_Right (Mask, bits)) can be computed at
9913 -- compile time as a single constant.
9915 if Compile_Time_Known_Value
(Right
) then
9917 Val
: constant Uint
:= Expr_Value
(Right
);
9920 if Val
>= Esize
(Typ
) then
9921 Maskx
:= Make_Integer_Literal
(Loc
, Mask
);
9925 Make_Integer_Literal
(Loc
,
9926 Intval
=> Mask
- (Mask
/ (2 ** Expr_Value
(Right
))));
9934 Make_Op_Shift_Right
(Loc
,
9935 Left_Opnd
=> Make_Integer_Literal
(Loc
, Mask
),
9936 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Right
)));
9939 -- Now do the rewrite
9944 Make_Op_Shift_Right
(Loc
,
9946 Right_Opnd
=> Right
),
9948 Make_If_Expression
(Loc
,
9949 Expressions
=> New_List
(
9951 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left
),
9952 Right_Opnd
=> Make_Integer_Literal
(Loc
, Sign
)),
9954 Make_Integer_Literal
(Loc
, 0)))));
9955 Analyze_And_Resolve
(N
, Typ
);
9958 end Expand_N_Op_Shift_Right_Arithmetic
;
9960 --------------------------
9961 -- Expand_N_Op_Subtract --
9962 --------------------------
9964 procedure Expand_N_Op_Subtract
(N
: Node_Id
) is
9965 Typ
: constant Entity_Id
:= Etype
(N
);
9968 Binary_Op_Validity_Checks
(N
);
9970 -- Check for MINIMIZED/ELIMINATED overflow mode
9972 if Minimized_Eliminated_Overflow_Check
(N
) then
9973 Apply_Arithmetic_Overflow_Check
(N
);
9977 -- N - 0 = N for integer types
9979 if Is_Integer_Type
(Typ
)
9980 and then Compile_Time_Known_Value
(Right_Opnd
(N
))
9981 and then Expr_Value
(Right_Opnd
(N
)) = 0
9983 Rewrite
(N
, Left_Opnd
(N
));
9987 -- Arithmetic overflow checks for signed integer/fixed point types
9989 if Is_Signed_Integer_Type
(Typ
) or else Is_Fixed_Point_Type
(Typ
) then
9990 Apply_Arithmetic_Overflow_Check
(N
);
9993 -- Overflow checks for floating-point if -gnateF mode active
9995 Check_Float_Op_Overflow
(N
);
9997 Expand_Nonbinary_Modular_Op
(N
);
9998 end Expand_N_Op_Subtract
;
10000 ---------------------
10001 -- Expand_N_Op_Xor --
10002 ---------------------
10004 procedure Expand_N_Op_Xor
(N
: Node_Id
) is
10005 Typ
: constant Entity_Id
:= Etype
(N
);
10008 Binary_Op_Validity_Checks
(N
);
10010 if Is_Array_Type
(Etype
(N
)) then
10011 Expand_Boolean_Operator
(N
);
10013 elsif Is_Boolean_Type
(Etype
(N
)) then
10014 Adjust_Condition
(Left_Opnd
(N
));
10015 Adjust_Condition
(Right_Opnd
(N
));
10016 Set_Etype
(N
, Standard_Boolean
);
10017 Adjust_Result_Type
(N
, Typ
);
10019 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
10020 Expand_Intrinsic_Call
(N
, Entity
(N
));
10022 end Expand_N_Op_Xor
;
10024 ----------------------
10025 -- Expand_N_Or_Else --
10026 ----------------------
10028 procedure Expand_N_Or_Else
(N
: Node_Id
)
10029 renames Expand_Short_Circuit_Operator
;
10031 -----------------------------------
10032 -- Expand_N_Qualified_Expression --
10033 -----------------------------------
10035 procedure Expand_N_Qualified_Expression
(N
: Node_Id
) is
10036 Operand
: constant Node_Id
:= Expression
(N
);
10037 Target_Type
: constant Entity_Id
:= Entity
(Subtype_Mark
(N
));
10040 -- Do validity check if validity checking operands
10042 if Validity_Checks_On
and Validity_Check_Operands
then
10043 Ensure_Valid
(Operand
);
10046 -- Apply possible constraint check
10048 Apply_Constraint_Check
(Operand
, Target_Type
, No_Sliding
=> True);
10050 if Do_Range_Check
(Operand
) then
10051 Set_Do_Range_Check
(Operand
, False);
10052 Generate_Range_Check
(Operand
, Target_Type
, CE_Range_Check_Failed
);
10054 end Expand_N_Qualified_Expression
;
10056 ------------------------------------
10057 -- Expand_N_Quantified_Expression --
10058 ------------------------------------
10062 -- for all X in range => Cond
10067 -- for X in range loop
10068 -- if not Cond then
10074 -- Similarly, an existentially quantified expression:
10076 -- for some X in range => Cond
10081 -- for X in range loop
10088 -- In both cases, the iteration may be over a container in which case it is
10089 -- given by an iterator specification, not a loop parameter specification.
10091 procedure Expand_N_Quantified_Expression
(N
: Node_Id
) is
10092 Actions
: constant List_Id
:= New_List
;
10093 For_All
: constant Boolean := All_Present
(N
);
10094 Iter_Spec
: constant Node_Id
:= Iterator_Specification
(N
);
10095 Loc
: constant Source_Ptr
:= Sloc
(N
);
10096 Loop_Spec
: constant Node_Id
:= Loop_Parameter_Specification
(N
);
10103 -- Create the declaration of the flag which tracks the status of the
10104 -- quantified expression. Generate:
10106 -- Flag : Boolean := (True | False);
10108 Flag
:= Make_Temporary
(Loc
, 'T', N
);
10110 Append_To
(Actions
,
10111 Make_Object_Declaration
(Loc
,
10112 Defining_Identifier
=> Flag
,
10113 Object_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
),
10115 New_Occurrence_Of
(Boolean_Literals
(For_All
), Loc
)));
10117 -- Construct the circuitry which tracks the status of the quantified
10118 -- expression. Generate:
10120 -- if [not] Cond then
10121 -- Flag := (False | True);
10125 Cond
:= Relocate_Node
(Condition
(N
));
10128 Cond
:= Make_Op_Not
(Loc
, Cond
);
10131 Stmts
:= New_List
(
10132 Make_Implicit_If_Statement
(N
,
10134 Then_Statements
=> New_List
(
10135 Make_Assignment_Statement
(Loc
,
10136 Name
=> New_Occurrence_Of
(Flag
, Loc
),
10138 New_Occurrence_Of
(Boolean_Literals
(not For_All
), Loc
)),
10139 Make_Exit_Statement
(Loc
))));
10141 -- Build the loop equivalent of the quantified expression
10143 if Present
(Iter_Spec
) then
10145 Make_Iteration_Scheme
(Loc
,
10146 Iterator_Specification
=> Iter_Spec
);
10149 Make_Iteration_Scheme
(Loc
,
10150 Loop_Parameter_Specification
=> Loop_Spec
);
10153 Append_To
(Actions
,
10154 Make_Loop_Statement
(Loc
,
10155 Iteration_Scheme
=> Scheme
,
10156 Statements
=> Stmts
,
10157 End_Label
=> Empty
));
10159 -- Transform the quantified expression
10162 Make_Expression_With_Actions
(Loc
,
10163 Expression
=> New_Occurrence_Of
(Flag
, Loc
),
10164 Actions
=> Actions
));
10165 Analyze_And_Resolve
(N
, Standard_Boolean
);
10166 end Expand_N_Quantified_Expression
;
10168 ---------------------------------
10169 -- Expand_N_Selected_Component --
10170 ---------------------------------
10172 procedure Expand_N_Selected_Component
(N
: Node_Id
) is
10173 Loc
: constant Source_Ptr
:= Sloc
(N
);
10174 Par
: constant Node_Id
:= Parent
(N
);
10175 P
: constant Node_Id
:= Prefix
(N
);
10176 S
: constant Node_Id
:= Selector_Name
(N
);
10177 Ptyp
: Entity_Id
:= Underlying_Type
(Etype
(P
));
10183 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean;
10184 -- Gigi needs a temporary for prefixes that depend on a discriminant,
10185 -- unless the context of an assignment can provide size information.
10186 -- Don't we have a general routine that does this???
10188 function Is_Subtype_Declaration
return Boolean;
10189 -- The replacement of a discriminant reference by its value is required
10190 -- if this is part of the initialization of an temporary generated by a
10191 -- change of representation. This shows up as the construction of a
10192 -- discriminant constraint for a subtype declared at the same point as
10193 -- the entity in the prefix of the selected component. We recognize this
10194 -- case when the context of the reference is:
10195 -- subtype ST is T(Obj.D);
10196 -- where the entity for Obj comes from source, and ST has the same sloc.
10198 -----------------------
10199 -- In_Left_Hand_Side --
10200 -----------------------
10202 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean is
10204 return (Nkind
(Parent
(Comp
)) = N_Assignment_Statement
10205 and then Comp
= Name
(Parent
(Comp
)))
10206 or else (Present
(Parent
(Comp
))
10207 and then Nkind
(Parent
(Comp
)) in N_Subexpr
10208 and then In_Left_Hand_Side
(Parent
(Comp
)));
10209 end In_Left_Hand_Side
;
10211 -----------------------------
10212 -- Is_Subtype_Declaration --
10213 -----------------------------
10215 function Is_Subtype_Declaration
return Boolean is
10216 Par
: constant Node_Id
:= Parent
(N
);
10219 Nkind
(Par
) = N_Index_Or_Discriminant_Constraint
10220 and then Nkind
(Parent
(Parent
(Par
))) = N_Subtype_Declaration
10221 and then Comes_From_Source
(Entity
(Prefix
(N
)))
10222 and then Sloc
(Par
) = Sloc
(Entity
(Prefix
(N
)));
10223 end Is_Subtype_Declaration
;
10225 -- Start of processing for Expand_N_Selected_Component
10228 -- Insert explicit dereference if required
10230 if Is_Access_Type
(Ptyp
) then
10232 -- First set prefix type to proper access type, in case it currently
10233 -- has a private (non-access) view of this type.
10235 Set_Etype
(P
, Ptyp
);
10237 Insert_Explicit_Dereference
(P
);
10238 Analyze_And_Resolve
(P
, Designated_Type
(Ptyp
));
10240 if Ekind
(Etype
(P
)) = E_Private_Subtype
10241 and then Is_For_Access_Subtype
(Etype
(P
))
10243 Set_Etype
(P
, Base_Type
(Etype
(P
)));
10249 -- Deal with discriminant check required
10251 if Do_Discriminant_Check
(N
) then
10252 if Present
(Discriminant_Checking_Func
10253 (Original_Record_Component
(Entity
(S
))))
10255 -- Present the discriminant checking function to the backend, so
10256 -- that it can inline the call to the function.
10259 (Discriminant_Checking_Func
10260 (Original_Record_Component
(Entity
(S
))),
10263 -- Now reset the flag and generate the call
10265 Set_Do_Discriminant_Check
(N
, False);
10266 Generate_Discriminant_Check
(N
);
10268 -- In the case of Unchecked_Union, no discriminant checking is
10269 -- actually performed.
10272 Set_Do_Discriminant_Check
(N
, False);
10276 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
10277 -- function, then additional actuals must be passed.
10279 if Is_Build_In_Place_Function_Call
(P
) then
10280 Make_Build_In_Place_Call_In_Anonymous_Context
(P
);
10282 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
10283 -- containing build-in-place function calls whose returned object covers
10284 -- interface types.
10286 elsif Present
(Unqual_BIP_Iface_Function_Call
(P
)) then
10287 Make_Build_In_Place_Iface_Call_In_Anonymous_Context
(P
);
10290 -- Gigi cannot handle unchecked conversions that are the prefix of a
10291 -- selected component with discriminants. This must be checked during
10292 -- expansion, because during analysis the type of the selector is not
10293 -- known at the point the prefix is analyzed. If the conversion is the
10294 -- target of an assignment, then we cannot force the evaluation.
10296 if Nkind
(Prefix
(N
)) = N_Unchecked_Type_Conversion
10297 and then Has_Discriminants
(Etype
(N
))
10298 and then not In_Left_Hand_Side
(N
)
10300 Force_Evaluation
(Prefix
(N
));
10303 -- Remaining processing applies only if selector is a discriminant
10305 if Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
then
10307 -- If the selector is a discriminant of a constrained record type,
10308 -- we may be able to rewrite the expression with the actual value
10309 -- of the discriminant, a useful optimization in some cases.
10311 if Is_Record_Type
(Ptyp
)
10312 and then Has_Discriminants
(Ptyp
)
10313 and then Is_Constrained
(Ptyp
)
10315 -- Do this optimization for discrete types only, and not for
10316 -- access types (access discriminants get us into trouble).
10318 if not Is_Discrete_Type
(Etype
(N
)) then
10321 -- Don't do this on the left-hand side of an assignment statement.
10322 -- Normally one would think that references like this would not
10323 -- occur, but they do in generated code, and mean that we really
10324 -- do want to assign the discriminant.
10326 elsif Nkind
(Par
) = N_Assignment_Statement
10327 and then Name
(Par
) = N
10331 -- Don't do this optimization for the prefix of an attribute or
10332 -- the name of an object renaming declaration since these are
10333 -- contexts where we do not want the value anyway.
10335 elsif (Nkind
(Par
) = N_Attribute_Reference
10336 and then Prefix
(Par
) = N
)
10337 or else Is_Renamed_Object
(N
)
10341 -- Don't do this optimization if we are within the code for a
10342 -- discriminant check, since the whole point of such a check may
10343 -- be to verify the condition on which the code below depends.
10345 elsif Is_In_Discriminant_Check
(N
) then
10348 -- Green light to see if we can do the optimization. There is
10349 -- still one condition that inhibits the optimization below but
10350 -- now is the time to check the particular discriminant.
10353 -- Loop through discriminants to find the matching discriminant
10354 -- constraint to see if we can copy it.
10356 Disc
:= First_Discriminant
(Ptyp
);
10357 Dcon
:= First_Elmt
(Discriminant_Constraint
(Ptyp
));
10358 Discr_Loop
: while Present
(Dcon
) loop
10359 Dval
:= Node
(Dcon
);
10361 -- Check if this is the matching discriminant and if the
10362 -- discriminant value is simple enough to make sense to
10363 -- copy. We don't want to copy complex expressions, and
10364 -- indeed to do so can cause trouble (before we put in
10365 -- this guard, a discriminant expression containing an
10366 -- AND THEN was copied, causing problems for coverage
10367 -- analysis tools).
10369 -- However, if the reference is part of the initialization
10370 -- code generated for an object declaration, we must use
10371 -- the discriminant value from the subtype constraint,
10372 -- because the selected component may be a reference to the
10373 -- object being initialized, whose discriminant is not yet
10374 -- set. This only happens in complex cases involving changes
10375 -- or representation.
10377 if Disc
= Entity
(Selector_Name
(N
))
10378 and then (Is_Entity_Name
(Dval
)
10379 or else Compile_Time_Known_Value
(Dval
)
10380 or else Is_Subtype_Declaration
)
10382 -- Here we have the matching discriminant. Check for
10383 -- the case of a discriminant of a component that is
10384 -- constrained by an outer discriminant, which cannot
10385 -- be optimized away.
10387 if Denotes_Discriminant
10388 (Dval
, Check_Concurrent
=> True)
10392 elsif Nkind
(Original_Node
(Dval
)) = N_Selected_Component
10394 Denotes_Discriminant
10395 (Selector_Name
(Original_Node
(Dval
)), True)
10399 -- Do not retrieve value if constraint is not static. It
10400 -- is generally not useful, and the constraint may be a
10401 -- rewritten outer discriminant in which case it is in
10404 elsif Is_Entity_Name
(Dval
)
10406 Nkind
(Parent
(Entity
(Dval
))) = N_Object_Declaration
10407 and then Present
(Expression
(Parent
(Entity
(Dval
))))
10409 Is_OK_Static_Expression
10410 (Expression
(Parent
(Entity
(Dval
))))
10414 -- In the context of a case statement, the expression may
10415 -- have the base type of the discriminant, and we need to
10416 -- preserve the constraint to avoid spurious errors on
10419 elsif Nkind
(Parent
(N
)) = N_Case_Statement
10420 and then Etype
(Dval
) /= Etype
(Disc
)
10423 Make_Qualified_Expression
(Loc
,
10425 New_Occurrence_Of
(Etype
(Disc
), Loc
),
10427 New_Copy_Tree
(Dval
)));
10428 Analyze_And_Resolve
(N
, Etype
(Disc
));
10430 -- In case that comes out as a static expression,
10431 -- reset it (a selected component is never static).
10433 Set_Is_Static_Expression
(N
, False);
10436 -- Otherwise we can just copy the constraint, but the
10437 -- result is certainly not static. In some cases the
10438 -- discriminant constraint has been analyzed in the
10439 -- context of the original subtype indication, but for
10440 -- itypes the constraint might not have been analyzed
10441 -- yet, and this must be done now.
10444 Rewrite
(N
, New_Copy_Tree
(Dval
));
10445 Analyze_And_Resolve
(N
);
10446 Set_Is_Static_Expression
(N
, False);
10452 Next_Discriminant
(Disc
);
10453 end loop Discr_Loop
;
10455 -- Note: the above loop should always find a matching
10456 -- discriminant, but if it does not, we just missed an
10457 -- optimization due to some glitch (perhaps a previous
10458 -- error), so ignore.
10463 -- The only remaining processing is in the case of a discriminant of
10464 -- a concurrent object, where we rewrite the prefix to denote the
10465 -- corresponding record type. If the type is derived and has renamed
10466 -- discriminants, use corresponding discriminant, which is the one
10467 -- that appears in the corresponding record.
10469 if not Is_Concurrent_Type
(Ptyp
) then
10473 Disc
:= Entity
(Selector_Name
(N
));
10475 if Is_Derived_Type
(Ptyp
)
10476 and then Present
(Corresponding_Discriminant
(Disc
))
10478 Disc
:= Corresponding_Discriminant
(Disc
);
10482 Make_Selected_Component
(Loc
,
10484 Unchecked_Convert_To
(Corresponding_Record_Type
(Ptyp
),
10485 New_Copy_Tree
(P
)),
10486 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Disc
)));
10488 Rewrite
(N
, New_N
);
10492 -- Set Atomic_Sync_Required if necessary for atomic component
10494 if Nkind
(N
) = N_Selected_Component
then
10496 E
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
10500 -- If component is atomic, but type is not, setting depends on
10501 -- disable/enable state for the component.
10503 if Is_Atomic
(E
) and then not Is_Atomic
(Etype
(E
)) then
10504 Set
:= not Atomic_Synchronization_Disabled
(E
);
10506 -- If component is not atomic, but its type is atomic, setting
10507 -- depends on disable/enable state for the type.
10509 elsif not Is_Atomic
(E
) and then Is_Atomic
(Etype
(E
)) then
10510 Set
:= not Atomic_Synchronization_Disabled
(Etype
(E
));
10512 -- If both component and type are atomic, we disable if either
10513 -- component or its type have sync disabled.
10515 elsif Is_Atomic
(E
) and then Is_Atomic
(Etype
(E
)) then
10516 Set
:= (not Atomic_Synchronization_Disabled
(E
))
10518 (not Atomic_Synchronization_Disabled
(Etype
(E
)));
10524 -- Set flag if required
10527 Activate_Atomic_Synchronization
(N
);
10531 end Expand_N_Selected_Component
;
10533 --------------------
10534 -- Expand_N_Slice --
10535 --------------------
10537 procedure Expand_N_Slice
(N
: Node_Id
) is
10538 Loc
: constant Source_Ptr
:= Sloc
(N
);
10539 Typ
: constant Entity_Id
:= Etype
(N
);
10541 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean;
10542 -- Check whether the argument is an actual for a procedure call, in
10543 -- which case the expansion of a bit-packed slice is deferred until the
10544 -- call itself is expanded. The reason this is required is that we might
10545 -- have an IN OUT or OUT parameter, and the copy out is essential, and
10546 -- that copy out would be missed if we created a temporary here in
10547 -- Expand_N_Slice. Note that we don't bother to test specifically for an
10548 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
10549 -- is harmless to defer expansion in the IN case, since the call
10550 -- processing will still generate the appropriate copy in operation,
10551 -- which will take care of the slice.
10553 procedure Make_Temporary_For_Slice
;
10554 -- Create a named variable for the value of the slice, in cases where
10555 -- the back end cannot handle it properly, e.g. when packed types or
10556 -- unaligned slices are involved.
10558 -------------------------
10559 -- Is_Procedure_Actual --
10560 -------------------------
10562 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean is
10563 Par
: Node_Id
:= Parent
(N
);
10567 -- If our parent is a procedure call we can return
10569 if Nkind
(Par
) = N_Procedure_Call_Statement
then
10572 -- If our parent is a type conversion, keep climbing the tree,
10573 -- since a type conversion can be a procedure actual. Also keep
10574 -- climbing if parameter association or a qualified expression,
10575 -- since these are additional cases that do can appear on
10576 -- procedure actuals.
10578 elsif Nkind_In
(Par
, N_Type_Conversion
,
10579 N_Parameter_Association
,
10580 N_Qualified_Expression
)
10582 Par
:= Parent
(Par
);
10584 -- Any other case is not what we are looking for
10590 end Is_Procedure_Actual
;
10592 ------------------------------
10593 -- Make_Temporary_For_Slice --
10594 ------------------------------
10596 procedure Make_Temporary_For_Slice
is
10597 Ent
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', N
);
10602 Make_Object_Declaration
(Loc
,
10603 Defining_Identifier
=> Ent
,
10604 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
10606 Set_No_Initialization
(Decl
);
10608 Insert_Actions
(N
, New_List
(
10610 Make_Assignment_Statement
(Loc
,
10611 Name
=> New_Occurrence_Of
(Ent
, Loc
),
10612 Expression
=> Relocate_Node
(N
))));
10614 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
10615 Analyze_And_Resolve
(N
, Typ
);
10616 end Make_Temporary_For_Slice
;
10620 Pref
: constant Node_Id
:= Prefix
(N
);
10621 Pref_Typ
: Entity_Id
:= Etype
(Pref
);
10623 -- Start of processing for Expand_N_Slice
10626 -- Special handling for access types
10628 if Is_Access_Type
(Pref_Typ
) then
10629 Pref_Typ
:= Designated_Type
(Pref_Typ
);
10632 Make_Explicit_Dereference
(Sloc
(N
),
10633 Prefix
=> Relocate_Node
(Pref
)));
10635 Analyze_And_Resolve
(Pref
, Pref_Typ
);
10638 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
10639 -- function, then additional actuals must be passed.
10641 if Is_Build_In_Place_Function_Call
(Pref
) then
10642 Make_Build_In_Place_Call_In_Anonymous_Context
(Pref
);
10644 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
10645 -- containing build-in-place function calls whose returned object covers
10646 -- interface types.
10648 elsif Present
(Unqual_BIP_Iface_Function_Call
(Pref
)) then
10649 Make_Build_In_Place_Iface_Call_In_Anonymous_Context
(Pref
);
10652 -- The remaining case to be handled is packed slices. We can leave
10653 -- packed slices as they are in the following situations:
10655 -- 1. Right or left side of an assignment (we can handle this
10656 -- situation correctly in the assignment statement expansion).
10658 -- 2. Prefix of indexed component (the slide is optimized away in this
10659 -- case, see the start of Expand_N_Slice.)
10661 -- 3. Object renaming declaration, since we want the name of the
10662 -- slice, not the value.
10664 -- 4. Argument to procedure call, since copy-in/copy-out handling may
10665 -- be required, and this is handled in the expansion of call
10668 -- 5. Prefix of an address attribute (this is an error which is caught
10669 -- elsewhere, and the expansion would interfere with generating the
10672 if not Is_Packed
(Typ
) then
10674 -- Apply transformation for actuals of a function call, where
10675 -- Expand_Actuals is not used.
10677 if Nkind
(Parent
(N
)) = N_Function_Call
10678 and then Is_Possibly_Unaligned_Slice
(N
)
10680 Make_Temporary_For_Slice
;
10683 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
10684 or else (Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
10685 and then Parent
(N
) = Name
(Parent
(Parent
(N
))))
10689 elsif Nkind
(Parent
(N
)) = N_Indexed_Component
10690 or else Is_Renamed_Object
(N
)
10691 or else Is_Procedure_Actual
(N
)
10695 elsif Nkind
(Parent
(N
)) = N_Attribute_Reference
10696 and then Attribute_Name
(Parent
(N
)) = Name_Address
10701 Make_Temporary_For_Slice
;
10703 end Expand_N_Slice
;
10705 ------------------------------
10706 -- Expand_N_Type_Conversion --
10707 ------------------------------
10709 procedure Expand_N_Type_Conversion
(N
: Node_Id
) is
10710 Loc
: constant Source_Ptr
:= Sloc
(N
);
10711 Operand
: constant Node_Id
:= Expression
(N
);
10712 Target_Type
: constant Entity_Id
:= Etype
(N
);
10713 Operand_Type
: Entity_Id
:= Etype
(Operand
);
10715 procedure Handle_Changed_Representation
;
10716 -- This is called in the case of record and array type conversions to
10717 -- see if there is a change of representation to be handled. Change of
10718 -- representation is actually handled at the assignment statement level,
10719 -- and what this procedure does is rewrite node N conversion as an
10720 -- assignment to temporary. If there is no change of representation,
10721 -- then the conversion node is unchanged.
10723 procedure Raise_Accessibility_Error
;
10724 -- Called when we know that an accessibility check will fail. Rewrites
10725 -- node N to an appropriate raise statement and outputs warning msgs.
10726 -- The Etype of the raise node is set to Target_Type. Note that in this
10727 -- case the rest of the processing should be skipped (i.e. the call to
10728 -- this procedure will be followed by "goto Done").
10730 procedure Real_Range_Check
;
10731 -- Handles generation of range check for real target value
10733 function Has_Extra_Accessibility
(Id
: Entity_Id
) return Boolean;
10734 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
10735 -- evaluates to True.
10737 -----------------------------------
10738 -- Handle_Changed_Representation --
10739 -----------------------------------
10741 procedure Handle_Changed_Representation
is
10749 -- Nothing else to do if no change of representation
10751 if Same_Representation
(Operand_Type
, Target_Type
) then
10754 -- The real change of representation work is done by the assignment
10755 -- statement processing. So if this type conversion is appearing as
10756 -- the expression of an assignment statement, nothing needs to be
10757 -- done to the conversion.
10759 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
then
10762 -- Otherwise we need to generate a temporary variable, and do the
10763 -- change of representation assignment into that temporary variable.
10764 -- The conversion is then replaced by a reference to this variable.
10769 -- If type is unconstrained we have to add a constraint, copied
10770 -- from the actual value of the left-hand side.
10772 if not Is_Constrained
(Target_Type
) then
10773 if Has_Discriminants
(Operand_Type
) then
10775 -- A change of representation can only apply to untagged
10776 -- types. We need to build the constraint that applies to
10777 -- the target type, using the constraints of the operand.
10778 -- The analysis is complicated if there are both inherited
10779 -- discriminants and constrained discriminants.
10780 -- We iterate over the discriminants of the target, and
10781 -- find the discriminant of the same name:
10783 -- a) If there is a corresponding discriminant in the object
10784 -- then the value is a selected component of the operand.
10786 -- b) Otherwise the value of a constrained discriminant is
10787 -- found in the stored constraint of the operand.
10790 Stored
: constant Elist_Id
:=
10791 Stored_Constraint
(Operand_Type
);
10795 Disc_O
: Entity_Id
;
10796 -- Discriminant of the operand type. Its value in the
10797 -- object is captured in a selected component.
10799 Disc_S
: Entity_Id
;
10800 -- Stored discriminant of the operand. If present, it
10801 -- corresponds to a constrained discriminant of the
10804 Disc_T
: Entity_Id
;
10805 -- Discriminant of the target type
10808 Disc_T
:= First_Discriminant
(Target_Type
);
10809 Disc_O
:= First_Discriminant
(Operand_Type
);
10810 Disc_S
:= First_Stored_Discriminant
(Operand_Type
);
10812 if Present
(Stored
) then
10813 Elmt
:= First_Elmt
(Stored
);
10815 Elmt
:= No_Elmt
; -- init to avoid warning
10819 while Present
(Disc_T
) loop
10820 if Present
(Disc_O
)
10821 and then Chars
(Disc_T
) = Chars
(Disc_O
)
10824 Make_Selected_Component
(Loc
,
10826 Duplicate_Subexpr_Move_Checks
(Operand
),
10828 Make_Identifier
(Loc
, Chars
(Disc_O
))));
10829 Next_Discriminant
(Disc_O
);
10831 elsif Present
(Disc_S
) then
10832 Append_To
(Cons
, New_Copy_Tree
(Node
(Elmt
)));
10836 Next_Discriminant
(Disc_T
);
10840 elsif Is_Array_Type
(Operand_Type
) then
10841 N_Ix
:= First_Index
(Target_Type
);
10844 for J
in 1 .. Number_Dimensions
(Operand_Type
) loop
10846 -- We convert the bounds explicitly. We use an unchecked
10847 -- conversion because bounds checks are done elsewhere.
10852 Unchecked_Convert_To
(Etype
(N_Ix
),
10853 Make_Attribute_Reference
(Loc
,
10855 Duplicate_Subexpr_No_Checks
10856 (Operand
, Name_Req
=> True),
10857 Attribute_Name
=> Name_First
,
10858 Expressions
=> New_List
(
10859 Make_Integer_Literal
(Loc
, J
)))),
10862 Unchecked_Convert_To
(Etype
(N_Ix
),
10863 Make_Attribute_Reference
(Loc
,
10865 Duplicate_Subexpr_No_Checks
10866 (Operand
, Name_Req
=> True),
10867 Attribute_Name
=> Name_Last
,
10868 Expressions
=> New_List
(
10869 Make_Integer_Literal
(Loc
, J
))))));
10876 Odef
:= New_Occurrence_Of
(Target_Type
, Loc
);
10878 if Present
(Cons
) then
10880 Make_Subtype_Indication
(Loc
,
10881 Subtype_Mark
=> Odef
,
10883 Make_Index_Or_Discriminant_Constraint
(Loc
,
10884 Constraints
=> Cons
));
10887 Temp
:= Make_Temporary
(Loc
, 'C');
10889 Make_Object_Declaration
(Loc
,
10890 Defining_Identifier
=> Temp
,
10891 Object_Definition
=> Odef
);
10893 Set_No_Initialization
(Decl
, True);
10895 -- Insert required actions. It is essential to suppress checks
10896 -- since we have suppressed default initialization, which means
10897 -- that the variable we create may have no discriminants.
10902 Make_Assignment_Statement
(Loc
,
10903 Name
=> New_Occurrence_Of
(Temp
, Loc
),
10904 Expression
=> Relocate_Node
(N
))),
10905 Suppress
=> All_Checks
);
10907 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
10910 end Handle_Changed_Representation
;
10912 -------------------------------
10913 -- Raise_Accessibility_Error --
10914 -------------------------------
10916 procedure Raise_Accessibility_Error
is
10918 Error_Msg_Warn
:= SPARK_Mode
/= On
;
10920 Make_Raise_Program_Error
(Sloc
(N
),
10921 Reason
=> PE_Accessibility_Check_Failed
));
10922 Set_Etype
(N
, Target_Type
);
10924 Error_Msg_N
("<<accessibility check failure", N
);
10925 Error_Msg_NE
("\<<& [", N
, Standard_Program_Error
);
10926 end Raise_Accessibility_Error
;
10928 ----------------------
10929 -- Real_Range_Check --
10930 ----------------------
10932 -- Case of conversions to floating-point or fixed-point. If range checks
10933 -- are enabled and the target type has a range constraint, we convert:
10939 -- Tnn : typ'Base := typ'Base (x);
10940 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
10943 -- This is necessary when there is a conversion of integer to float or
10944 -- to fixed-point to ensure that the correct checks are made. It is not
10945 -- necessary for float to float where it is enough to simply set the
10946 -- Do_Range_Check flag.
10948 procedure Real_Range_Check
is
10949 Btyp
: constant Entity_Id
:= Base_Type
(Target_Type
);
10950 Lo
: constant Node_Id
:= Type_Low_Bound
(Target_Type
);
10951 Hi
: constant Node_Id
:= Type_High_Bound
(Target_Type
);
10952 Xtyp
: constant Entity_Id
:= Etype
(Operand
);
10962 -- Nothing to do if conversion was rewritten
10964 if Nkind
(N
) /= N_Type_Conversion
then
10968 -- Nothing to do if range checks suppressed, or target has the same
10969 -- range as the base type (or is the base type).
10971 if Range_Checks_Suppressed
(Target_Type
)
10972 or else (Lo
= Type_Low_Bound
(Btyp
)
10974 Hi
= Type_High_Bound
(Btyp
))
10979 -- Nothing to do if expression is an entity on which checks have been
10982 if Is_Entity_Name
(Operand
)
10983 and then Range_Checks_Suppressed
(Entity
(Operand
))
10988 -- Nothing to do if bounds are all static and we can tell that the
10989 -- expression is within the bounds of the target. Note that if the
10990 -- operand is of an unconstrained floating-point type, then we do
10991 -- not trust it to be in range (might be infinite)
10994 S_Lo
: constant Node_Id
:= Type_Low_Bound
(Xtyp
);
10995 S_Hi
: constant Node_Id
:= Type_High_Bound
(Xtyp
);
10998 if (not Is_Floating_Point_Type
(Xtyp
)
10999 or else Is_Constrained
(Xtyp
))
11000 and then Compile_Time_Known_Value
(S_Lo
)
11001 and then Compile_Time_Known_Value
(S_Hi
)
11002 and then Compile_Time_Known_Value
(Hi
)
11003 and then Compile_Time_Known_Value
(Lo
)
11006 D_Lov
: constant Ureal
:= Expr_Value_R
(Lo
);
11007 D_Hiv
: constant Ureal
:= Expr_Value_R
(Hi
);
11012 if Is_Real_Type
(Xtyp
) then
11013 S_Lov
:= Expr_Value_R
(S_Lo
);
11014 S_Hiv
:= Expr_Value_R
(S_Hi
);
11016 S_Lov
:= UR_From_Uint
(Expr_Value
(S_Lo
));
11017 S_Hiv
:= UR_From_Uint
(Expr_Value
(S_Hi
));
11021 and then S_Lov
>= D_Lov
11022 and then S_Hiv
<= D_Hiv
11024 -- Unset the range check flag on the current value of
11025 -- Expression (N), since the captured Operand may have
11026 -- been rewritten (such as for the case of a conversion
11027 -- to a fixed-point type).
11029 Set_Do_Range_Check
(Expression
(N
), False);
11037 -- For float to float conversions, we are done
11039 if Is_Floating_Point_Type
(Xtyp
)
11041 Is_Floating_Point_Type
(Btyp
)
11046 -- Otherwise rewrite the conversion as described above
11048 Conv
:= Relocate_Node
(N
);
11049 Rewrite
(Subtype_Mark
(Conv
), New_Occurrence_Of
(Btyp
, Loc
));
11050 Set_Etype
(Conv
, Btyp
);
11052 -- Enable overflow except for case of integer to float conversions,
11053 -- where it is never required, since we can never have overflow in
11056 if not Is_Integer_Type
(Etype
(Operand
)) then
11057 Enable_Overflow_Check
(Conv
);
11060 Tnn
:= Make_Temporary
(Loc
, 'T', Conv
);
11062 -- For a conversion from Float to Fixed where the bounds of the
11063 -- fixed-point type are static, we can obtain a more accurate
11064 -- fixed-point value by converting the result of the floating-
11065 -- point expression to an appropriate integer type, and then
11066 -- performing an unchecked conversion to the target fixed-point
11067 -- type. The range check can then use the corresponding integer
11068 -- value of the bounds instead of requiring further conversions.
11069 -- This preserves the identity:
11071 -- Fix_Val = Fixed_Type (Float_Type (Fix_Val))
11073 -- which used to fail when Fix_Val was a bound of the type and
11074 -- the 'Small was not a representable number.
11075 -- This transformation requires an integer type large enough to
11076 -- accommodate a fixed-point value. This will not be the case
11077 -- in systems where Duration is larger than Long_Integer.
11079 if Is_Ordinary_Fixed_Point_Type
(Target_Type
)
11080 and then Is_Floating_Point_Type
(Operand_Type
)
11081 and then RM_Size
(Base_Type
(Target_Type
)) <=
11082 RM_Size
(Standard_Long_Integer
)
11083 and then Nkind
(Lo
) = N_Real_Literal
11084 and then Nkind
(Hi
) = N_Real_Literal
11086 -- Find the integer type of the right size to perform an unchecked
11087 -- conversion to the target fixed-point type.
11090 Bfx_Type
: constant Entity_Id
:= Base_Type
(Target_Type
);
11091 Expr_Id
: constant Entity_Id
:=
11092 Make_Temporary
(Loc
, 'T', Conv
);
11093 Int_Type
: Entity_Id
;
11096 if RM_Size
(Bfx_Type
) > RM_Size
(Standard_Integer
) then
11097 Int_Type
:= Standard_Long_Integer
;
11099 elsif RM_Size
(Bfx_Type
) > RM_Size
(Standard_Short_Integer
) then
11100 Int_Type
:= Standard_Integer
;
11103 Int_Type
:= Standard_Short_Integer
;
11106 -- Generate a temporary with the integer value. Required in the
11107 -- CCG compiler to ensure that runtime checks reference this
11108 -- integer expression (instead of the resulting fixed-point
11109 -- value) because fixed-point values are handled by means of
11110 -- unsigned integer types).
11113 Make_Object_Declaration
(Loc
,
11114 Defining_Identifier
=> Expr_Id
,
11115 Object_Definition
=> New_Occurrence_Of
(Int_Type
, Loc
),
11116 Constant_Present
=> True,
11118 Convert_To
(Int_Type
, Expression
(Conv
))));
11120 -- Create integer objects for range checking of result.
11123 Unchecked_Convert_To
11124 (Int_Type
, New_Occurrence_Of
(Expr_Id
, Loc
));
11127 Make_Integer_Literal
(Loc
, Corresponding_Integer_Value
(Lo
));
11130 Unchecked_Convert_To
11131 (Int_Type
, New_Occurrence_Of
(Expr_Id
, Loc
));
11134 Make_Integer_Literal
(Loc
, Corresponding_Integer_Value
(Hi
));
11136 -- Rewrite conversion as an integer conversion of the
11137 -- original floating-point expression, followed by an
11138 -- unchecked conversion to the target fixed-point type.
11141 Make_Unchecked_Type_Conversion
(Loc
,
11142 Subtype_Mark
=> New_Occurrence_Of
(Target_Type
, Loc
),
11143 Expression
=> New_Occurrence_Of
(Expr_Id
, Loc
));
11146 -- All other conversions
11149 Lo_Arg
:= New_Occurrence_Of
(Tnn
, Loc
);
11151 Make_Attribute_Reference
(Loc
,
11152 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
11153 Attribute_Name
=> Name_First
);
11155 Hi_Arg
:= New_Occurrence_Of
(Tnn
, Loc
);
11157 Make_Attribute_Reference
(Loc
,
11158 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
11159 Attribute_Name
=> Name_Last
);
11162 -- Build code for range checking
11164 Insert_Actions
(N
, New_List
(
11165 Make_Object_Declaration
(Loc
,
11166 Defining_Identifier
=> Tnn
,
11167 Object_Definition
=> New_Occurrence_Of
(Btyp
, Loc
),
11168 Constant_Present
=> True,
11169 Expression
=> Conv
),
11171 Make_Raise_Constraint_Error
(Loc
,
11176 Left_Opnd
=> Lo_Arg
,
11177 Right_Opnd
=> Lo_Val
),
11181 Left_Opnd
=> Hi_Arg
,
11182 Right_Opnd
=> Hi_Val
)),
11183 Reason
=> CE_Range_Check_Failed
)));
11185 Rewrite
(N
, New_Occurrence_Of
(Tnn
, Loc
));
11186 Analyze_And_Resolve
(N
, Btyp
);
11187 end Real_Range_Check
;
11189 -----------------------------
11190 -- Has_Extra_Accessibility --
11191 -----------------------------
11193 -- Returns true for a formal of an anonymous access type or for an Ada
11194 -- 2012-style stand-alone object of an anonymous access type.
11196 function Has_Extra_Accessibility
(Id
: Entity_Id
) return Boolean is
11198 if Is_Formal
(Id
) or else Ekind_In
(Id
, E_Constant
, E_Variable
) then
11199 return Present
(Effective_Extra_Accessibility
(Id
));
11203 end Has_Extra_Accessibility
;
11205 -- Start of processing for Expand_N_Type_Conversion
11208 -- First remove check marks put by the semantic analysis on the type
11209 -- conversion between array types. We need these checks, and they will
11210 -- be generated by this expansion routine, but we do not depend on these
11211 -- flags being set, and since we do intend to expand the checks in the
11212 -- front end, we don't want them on the tree passed to the back end.
11214 if Is_Array_Type
(Target_Type
) then
11215 if Is_Constrained
(Target_Type
) then
11216 Set_Do_Length_Check
(N
, False);
11218 Set_Do_Range_Check
(Operand
, False);
11222 -- Nothing at all to do if conversion is to the identical type so remove
11223 -- the conversion completely, it is useless, except that it may carry
11224 -- an Assignment_OK attribute, which must be propagated to the operand.
11226 if Operand_Type
= Target_Type
then
11227 if Assignment_OK
(N
) then
11228 Set_Assignment_OK
(Operand
);
11231 Rewrite
(N
, Relocate_Node
(Operand
));
11235 -- Nothing to do if this is the second argument of read. This is a
11236 -- "backwards" conversion that will be handled by the specialized code
11237 -- in attribute processing.
11239 if Nkind
(Parent
(N
)) = N_Attribute_Reference
11240 and then Attribute_Name
(Parent
(N
)) = Name_Read
11241 and then Next
(First
(Expressions
(Parent
(N
)))) = N
11246 -- Check for case of converting to a type that has an invariant
11247 -- associated with it. This requires an invariant check. We insert
11250 -- invariant_check (typ (expr))
11252 -- in the code, after removing side effects from the expression.
11253 -- This is clearer than replacing the conversion into an expression
11254 -- with actions, because the context may impose additional actions
11255 -- (tag checks, membership tests, etc.) that conflict with this
11256 -- rewriting (used previously).
11258 -- Note: the Comes_From_Source check, and then the resetting of this
11259 -- flag prevents what would otherwise be an infinite recursion.
11261 if Has_Invariants
(Target_Type
)
11262 and then Present
(Invariant_Procedure
(Target_Type
))
11263 and then Comes_From_Source
(N
)
11265 Set_Comes_From_Source
(N
, False);
11266 Remove_Side_Effects
(N
);
11267 Insert_Action
(N
, Make_Invariant_Call
(Duplicate_Subexpr
(N
)));
11271 -- Here if we may need to expand conversion
11273 -- If the operand of the type conversion is an arithmetic operation on
11274 -- signed integers, and the based type of the signed integer type in
11275 -- question is smaller than Standard.Integer, we promote both of the
11276 -- operands to type Integer.
11278 -- For example, if we have
11280 -- target-type (opnd1 + opnd2)
11282 -- and opnd1 and opnd2 are of type short integer, then we rewrite
11285 -- target-type (integer(opnd1) + integer(opnd2))
11287 -- We do this because we are always allowed to compute in a larger type
11288 -- if we do the right thing with the result, and in this case we are
11289 -- going to do a conversion which will do an appropriate check to make
11290 -- sure that things are in range of the target type in any case. This
11291 -- avoids some unnecessary intermediate overflows.
11293 -- We might consider a similar transformation in the case where the
11294 -- target is a real type or a 64-bit integer type, and the operand
11295 -- is an arithmetic operation using a 32-bit integer type. However,
11296 -- we do not bother with this case, because it could cause significant
11297 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
11298 -- much cheaper, but we don't want different behavior on 32-bit and
11299 -- 64-bit machines. Note that the exclusion of the 64-bit case also
11300 -- handles the configurable run-time cases where 64-bit arithmetic
11301 -- may simply be unavailable.
11303 -- Note: this circuit is partially redundant with respect to the circuit
11304 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
11305 -- the processing here. Also we still need the Checks circuit, since we
11306 -- have to be sure not to generate junk overflow checks in the first
11307 -- place, since it would be trick to remove them here.
11309 if Integer_Promotion_Possible
(N
) then
11311 -- All conditions met, go ahead with transformation
11319 Make_Type_Conversion
(Loc
,
11320 Subtype_Mark
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
11321 Expression
=> Relocate_Node
(Right_Opnd
(Operand
)));
11323 Opnd
:= New_Op_Node
(Nkind
(Operand
), Loc
);
11324 Set_Right_Opnd
(Opnd
, R
);
11326 if Nkind
(Operand
) in N_Binary_Op
then
11328 Make_Type_Conversion
(Loc
,
11329 Subtype_Mark
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
11330 Expression
=> Relocate_Node
(Left_Opnd
(Operand
)));
11332 Set_Left_Opnd
(Opnd
, L
);
11336 Make_Type_Conversion
(Loc
,
11337 Subtype_Mark
=> Relocate_Node
(Subtype_Mark
(N
)),
11338 Expression
=> Opnd
));
11340 Analyze_And_Resolve
(N
, Target_Type
);
11345 -- Do validity check if validity checking operands
11347 if Validity_Checks_On
and Validity_Check_Operands
then
11348 Ensure_Valid
(Operand
);
11351 -- Special case of converting from non-standard boolean type
11353 if Is_Boolean_Type
(Operand_Type
)
11354 and then (Nonzero_Is_True
(Operand_Type
))
11356 Adjust_Condition
(Operand
);
11357 Set_Etype
(Operand
, Standard_Boolean
);
11358 Operand_Type
:= Standard_Boolean
;
11361 -- Case of converting to an access type
11363 if Is_Access_Type
(Target_Type
) then
11365 -- If this type conversion was internally generated by the front end
11366 -- to displace the pointer to the object to reference an interface
11367 -- type and the original node was an Unrestricted_Access attribute,
11368 -- then skip applying accessibility checks (because, according to the
11369 -- GNAT Reference Manual, this attribute is similar to 'Access except
11370 -- that all accessibility and aliased view checks are omitted).
11372 if not Comes_From_Source
(N
)
11373 and then Is_Interface
(Designated_Type
(Target_Type
))
11374 and then Nkind
(Original_Node
(N
)) = N_Attribute_Reference
11375 and then Attribute_Name
(Original_Node
(N
)) =
11376 Name_Unrestricted_Access
11380 -- Apply an accessibility check when the conversion operand is an
11381 -- access parameter (or a renaming thereof), unless conversion was
11382 -- expanded from an Unchecked_ or Unrestricted_Access attribute,
11383 -- or for the actual of a class-wide interface parameter. Note that
11384 -- other checks may still need to be applied below (such as tagged
11387 elsif Is_Entity_Name
(Operand
)
11388 and then Has_Extra_Accessibility
(Entity
(Operand
))
11389 and then Ekind
(Etype
(Operand
)) = E_Anonymous_Access_Type
11390 and then (Nkind
(Original_Node
(N
)) /= N_Attribute_Reference
11391 or else Attribute_Name
(Original_Node
(N
)) = Name_Access
)
11393 if not Comes_From_Source
(N
)
11394 and then Nkind_In
(Parent
(N
), N_Function_Call
,
11395 N_Procedure_Call_Statement
)
11396 and then Is_Interface
(Designated_Type
(Target_Type
))
11397 and then Is_Class_Wide_Type
(Designated_Type
(Target_Type
))
11402 Apply_Accessibility_Check
11403 (Operand
, Target_Type
, Insert_Node
=> Operand
);
11406 -- If the level of the operand type is statically deeper than the
11407 -- level of the target type, then force Program_Error. Note that this
11408 -- can only occur for cases where the attribute is within the body of
11409 -- an instantiation, otherwise the conversion will already have been
11410 -- rejected as illegal.
11412 -- Note: warnings are issued by the analyzer for the instance cases
11414 elsif In_Instance_Body
11416 -- The case where the target type is an anonymous access type of
11417 -- a discriminant is excluded, because the level of such a type
11418 -- depends on the context and currently the level returned for such
11419 -- types is zero, resulting in warnings about about check failures
11420 -- in certain legal cases involving class-wide interfaces as the
11421 -- designated type (some cases, such as return statements, are
11422 -- checked at run time, but not clear if these are handled right
11423 -- in general, see 3.10.2(12/2-12.5/3) ???).
11426 not (Ekind
(Target_Type
) = E_Anonymous_Access_Type
11427 and then Present
(Associated_Node_For_Itype
(Target_Type
))
11428 and then Nkind
(Associated_Node_For_Itype
(Target_Type
)) =
11429 N_Discriminant_Specification
)
11431 Type_Access_Level
(Operand_Type
) > Type_Access_Level
(Target_Type
)
11433 Raise_Accessibility_Error
;
11436 -- When the operand is a selected access discriminant the check needs
11437 -- to be made against the level of the object denoted by the prefix
11438 -- of the selected name. Force Program_Error for this case as well
11439 -- (this accessibility violation can only happen if within the body
11440 -- of an instantiation).
11442 elsif In_Instance_Body
11443 and then Ekind
(Operand_Type
) = E_Anonymous_Access_Type
11444 and then Nkind
(Operand
) = N_Selected_Component
11445 and then Ekind
(Entity
(Selector_Name
(Operand
))) = E_Discriminant
11446 and then Object_Access_Level
(Operand
) >
11447 Type_Access_Level
(Target_Type
)
11449 Raise_Accessibility_Error
;
11454 -- Case of conversions of tagged types and access to tagged types
11456 -- When needed, that is to say when the expression is class-wide, Add
11457 -- runtime a tag check for (strict) downward conversion by using the
11458 -- membership test, generating:
11460 -- [constraint_error when Operand not in Target_Type'Class]
11462 -- or in the access type case
11464 -- [constraint_error
11465 -- when Operand /= null
11466 -- and then Operand.all not in
11467 -- Designated_Type (Target_Type)'Class]
11469 if (Is_Access_Type
(Target_Type
)
11470 and then Is_Tagged_Type
(Designated_Type
(Target_Type
)))
11471 or else Is_Tagged_Type
(Target_Type
)
11473 -- Do not do any expansion in the access type case if the parent is a
11474 -- renaming, since this is an error situation which will be caught by
11475 -- Sem_Ch8, and the expansion can interfere with this error check.
11477 if Is_Access_Type
(Target_Type
) and then Is_Renamed_Object
(N
) then
11481 -- Otherwise, proceed with processing tagged conversion
11483 Tagged_Conversion
: declare
11484 Actual_Op_Typ
: Entity_Id
;
11485 Actual_Targ_Typ
: Entity_Id
;
11486 Make_Conversion
: Boolean := False;
11487 Root_Op_Typ
: Entity_Id
;
11489 procedure Make_Tag_Check
(Targ_Typ
: Entity_Id
);
11490 -- Create a membership check to test whether Operand is a member
11491 -- of Targ_Typ. If the original Target_Type is an access, include
11492 -- a test for null value. The check is inserted at N.
11494 --------------------
11495 -- Make_Tag_Check --
11496 --------------------
11498 procedure Make_Tag_Check
(Targ_Typ
: Entity_Id
) is
11503 -- [Constraint_Error
11504 -- when Operand /= null
11505 -- and then Operand.all not in Targ_Typ]
11507 if Is_Access_Type
(Target_Type
) then
11509 Make_And_Then
(Loc
,
11512 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
11513 Right_Opnd
=> Make_Null
(Loc
)),
11518 Make_Explicit_Dereference
(Loc
,
11519 Prefix
=> Duplicate_Subexpr_No_Checks
(Operand
)),
11520 Right_Opnd
=> New_Occurrence_Of
(Targ_Typ
, Loc
)));
11523 -- [Constraint_Error when Operand not in Targ_Typ]
11528 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
11529 Right_Opnd
=> New_Occurrence_Of
(Targ_Typ
, Loc
));
11533 Make_Raise_Constraint_Error
(Loc
,
11535 Reason
=> CE_Tag_Check_Failed
),
11536 Suppress
=> All_Checks
);
11537 end Make_Tag_Check
;
11539 -- Start of processing for Tagged_Conversion
11542 -- Handle entities from the limited view
11544 if Is_Access_Type
(Operand_Type
) then
11546 Available_View
(Designated_Type
(Operand_Type
));
11548 Actual_Op_Typ
:= Operand_Type
;
11551 if Is_Access_Type
(Target_Type
) then
11553 Available_View
(Designated_Type
(Target_Type
));
11555 Actual_Targ_Typ
:= Target_Type
;
11558 Root_Op_Typ
:= Root_Type
(Actual_Op_Typ
);
11560 -- Ada 2005 (AI-251): Handle interface type conversion
11562 if Is_Interface
(Actual_Op_Typ
)
11564 Is_Interface
(Actual_Targ_Typ
)
11566 Expand_Interface_Conversion
(N
);
11570 if not Tag_Checks_Suppressed
(Actual_Targ_Typ
) then
11572 -- Create a runtime tag check for a downward class-wide type
11575 if Is_Class_Wide_Type
(Actual_Op_Typ
)
11576 and then Actual_Op_Typ
/= Actual_Targ_Typ
11577 and then Root_Op_Typ
/= Actual_Targ_Typ
11578 and then Is_Ancestor
(Root_Op_Typ
, Actual_Targ_Typ
,
11579 Use_Full_View
=> True)
11581 Make_Tag_Check
(Class_Wide_Type
(Actual_Targ_Typ
));
11582 Make_Conversion
:= True;
11585 -- AI05-0073: If the result subtype of the function is defined
11586 -- by an access_definition designating a specific tagged type
11587 -- T, a check is made that the result value is null or the tag
11588 -- of the object designated by the result value identifies T.
11589 -- Constraint_Error is raised if this check fails.
11591 if Nkind
(Parent
(N
)) = N_Simple_Return_Statement
then
11594 Func_Typ
: Entity_Id
;
11597 -- Climb scope stack looking for the enclosing function
11599 Func
:= Current_Scope
;
11600 while Present
(Func
)
11601 and then Ekind
(Func
) /= E_Function
11603 Func
:= Scope
(Func
);
11606 -- The function's return subtype must be defined using
11607 -- an access definition.
11609 if Nkind
(Result_Definition
(Parent
(Func
))) =
11610 N_Access_Definition
11612 Func_Typ
:= Directly_Designated_Type
(Etype
(Func
));
11614 -- The return subtype denotes a specific tagged type,
11615 -- in other words, a non class-wide type.
11617 if Is_Tagged_Type
(Func_Typ
)
11618 and then not Is_Class_Wide_Type
(Func_Typ
)
11620 Make_Tag_Check
(Actual_Targ_Typ
);
11621 Make_Conversion
:= True;
11627 -- We have generated a tag check for either a class-wide type
11628 -- conversion or for AI05-0073.
11630 if Make_Conversion
then
11635 Make_Unchecked_Type_Conversion
(Loc
,
11636 Subtype_Mark
=> New_Occurrence_Of
(Target_Type
, Loc
),
11637 Expression
=> Relocate_Node
(Expression
(N
)));
11639 Analyze_And_Resolve
(N
, Target_Type
);
11643 end Tagged_Conversion
;
11645 -- Case of other access type conversions
11647 elsif Is_Access_Type
(Target_Type
) then
11648 Apply_Constraint_Check
(Operand
, Target_Type
);
11650 -- Case of conversions from a fixed-point type
11652 -- These conversions require special expansion and processing, found in
11653 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
11654 -- since from a semantic point of view, these are simple integer
11655 -- conversions, which do not need further processing.
11657 elsif Is_Fixed_Point_Type
(Operand_Type
)
11658 and then not Conversion_OK
(N
)
11660 -- We should never see universal fixed at this case, since the
11661 -- expansion of the constituent divide or multiply should have
11662 -- eliminated the explicit mention of universal fixed.
11664 pragma Assert
(Operand_Type
/= Universal_Fixed
);
11666 -- Check for special case of the conversion to universal real that
11667 -- occurs as a result of the use of a round attribute. In this case,
11668 -- the real type for the conversion is taken from the target type of
11669 -- the Round attribute and the result must be marked as rounded.
11671 if Target_Type
= Universal_Real
11672 and then Nkind
(Parent
(N
)) = N_Attribute_Reference
11673 and then Attribute_Name
(Parent
(N
)) = Name_Round
11675 Set_Rounded_Result
(N
);
11676 Set_Etype
(N
, Etype
(Parent
(N
)));
11679 -- Otherwise do correct fixed-conversion, but skip these if the
11680 -- Conversion_OK flag is set, because from a semantic point of view
11681 -- these are simple integer conversions needing no further processing
11682 -- (the backend will simply treat them as integers).
11684 if not Conversion_OK
(N
) then
11685 if Is_Fixed_Point_Type
(Etype
(N
)) then
11686 Expand_Convert_Fixed_To_Fixed
(N
);
11689 elsif Is_Integer_Type
(Etype
(N
)) then
11690 Expand_Convert_Fixed_To_Integer
(N
);
11693 pragma Assert
(Is_Floating_Point_Type
(Etype
(N
)));
11694 Expand_Convert_Fixed_To_Float
(N
);
11699 -- Case of conversions to a fixed-point type
11701 -- These conversions require special expansion and processing, found in
11702 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
11703 -- since from a semantic point of view, these are simple integer
11704 -- conversions, which do not need further processing.
11706 elsif Is_Fixed_Point_Type
(Target_Type
)
11707 and then not Conversion_OK
(N
)
11709 if Is_Integer_Type
(Operand_Type
) then
11710 Expand_Convert_Integer_To_Fixed
(N
);
11713 pragma Assert
(Is_Floating_Point_Type
(Operand_Type
));
11714 Expand_Convert_Float_To_Fixed
(N
);
11718 -- Case of float-to-integer conversions
11720 -- We also handle float-to-fixed conversions with Conversion_OK set
11721 -- since semantically the fixed-point target is treated as though it
11722 -- were an integer in such cases.
11724 elsif Is_Floating_Point_Type
(Operand_Type
)
11726 (Is_Integer_Type
(Target_Type
)
11728 (Is_Fixed_Point_Type
(Target_Type
) and then Conversion_OK
(N
)))
11730 -- One more check here, gcc is still not able to do conversions of
11731 -- this type with proper overflow checking, and so gigi is doing an
11732 -- approximation of what is required by doing floating-point compares
11733 -- with the end-point. But that can lose precision in some cases, and
11734 -- give a wrong result. Converting the operand to Universal_Real is
11735 -- helpful, but still does not catch all cases with 64-bit integers
11736 -- on targets with only 64-bit floats.
11738 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
11739 -- Can this code be removed ???
11741 if Do_Range_Check
(Operand
) then
11743 Make_Type_Conversion
(Loc
,
11745 New_Occurrence_Of
(Universal_Real
, Loc
),
11747 Relocate_Node
(Operand
)));
11749 Set_Etype
(Operand
, Universal_Real
);
11750 Enable_Range_Check
(Operand
);
11751 Set_Do_Range_Check
(Expression
(Operand
), False);
11754 -- Case of array conversions
11756 -- Expansion of array conversions, add required length/range checks but
11757 -- only do this if there is no change of representation. For handling of
11758 -- this case, see Handle_Changed_Representation.
11760 elsif Is_Array_Type
(Target_Type
) then
11761 if Is_Constrained
(Target_Type
) then
11762 Apply_Length_Check
(Operand
, Target_Type
);
11764 Apply_Range_Check
(Operand
, Target_Type
);
11767 Handle_Changed_Representation
;
11769 -- Case of conversions of discriminated types
11771 -- Add required discriminant checks if target is constrained. Again this
11772 -- change is skipped if we have a change of representation.
11774 elsif Has_Discriminants
(Target_Type
)
11775 and then Is_Constrained
(Target_Type
)
11777 Apply_Discriminant_Check
(Operand
, Target_Type
);
11778 Handle_Changed_Representation
;
11780 -- Case of all other record conversions. The only processing required
11781 -- is to check for a change of representation requiring the special
11782 -- assignment processing.
11784 elsif Is_Record_Type
(Target_Type
) then
11786 -- Ada 2005 (AI-216): Program_Error is raised when converting from
11787 -- a derived Unchecked_Union type to an unconstrained type that is
11788 -- not Unchecked_Union if the operand lacks inferable discriminants.
11790 if Is_Derived_Type
(Operand_Type
)
11791 and then Is_Unchecked_Union
(Base_Type
(Operand_Type
))
11792 and then not Is_Constrained
(Target_Type
)
11793 and then not Is_Unchecked_Union
(Base_Type
(Target_Type
))
11794 and then not Has_Inferable_Discriminants
(Operand
)
11796 -- To prevent Gigi from generating illegal code, we generate a
11797 -- Program_Error node, but we give it the target type of the
11798 -- conversion (is this requirement documented somewhere ???)
11801 PE
: constant Node_Id
:= Make_Raise_Program_Error
(Loc
,
11802 Reason
=> PE_Unchecked_Union_Restriction
);
11805 Set_Etype
(PE
, Target_Type
);
11810 Handle_Changed_Representation
;
11813 -- Case of conversions of enumeration types
11815 elsif Is_Enumeration_Type
(Target_Type
) then
11817 -- Special processing is required if there is a change of
11818 -- representation (from enumeration representation clauses).
11820 if not Same_Representation
(Target_Type
, Operand_Type
) then
11822 -- Convert: x(y) to x'val (ytyp'val (y))
11825 Make_Attribute_Reference
(Loc
,
11826 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
11827 Attribute_Name
=> Name_Val
,
11828 Expressions
=> New_List
(
11829 Make_Attribute_Reference
(Loc
,
11830 Prefix
=> New_Occurrence_Of
(Operand_Type
, Loc
),
11831 Attribute_Name
=> Name_Pos
,
11832 Expressions
=> New_List
(Operand
)))));
11834 Analyze_And_Resolve
(N
, Target_Type
);
11837 -- Case of conversions to floating-point
11839 elsif Is_Floating_Point_Type
(Target_Type
) then
11843 -- At this stage, either the conversion node has been transformed into
11844 -- some other equivalent expression, or left as a conversion that can be
11845 -- handled by Gigi, in the following cases:
11847 -- Conversions with no change of representation or type
11849 -- Numeric conversions involving integer, floating- and fixed-point
11850 -- values. Fixed-point values are allowed only if Conversion_OK is
11851 -- set, i.e. if the fixed-point values are to be treated as integers.
11853 -- No other conversions should be passed to Gigi
11855 -- Check: are these rules stated in sinfo??? if so, why restate here???
11857 -- The only remaining step is to generate a range check if we still have
11858 -- a type conversion at this stage and Do_Range_Check is set. For now we
11859 -- do this only for conversions of discrete types and for float-to-float
11862 if Nkind
(N
) = N_Type_Conversion
then
11864 -- For now we only support floating-point cases where both source
11865 -- and target are floating-point types. Conversions where the source
11866 -- and target involve integer or fixed-point types are still TBD,
11867 -- though not clear whether those can even happen at this point, due
11868 -- to transformations above. ???
11870 if Is_Floating_Point_Type
(Etype
(N
))
11871 and then Is_Floating_Point_Type
(Etype
(Expression
(N
)))
11873 if Do_Range_Check
(Expression
(N
))
11874 and then Is_Floating_Point_Type
(Target_Type
)
11876 Generate_Range_Check
11877 (Expression
(N
), Target_Type
, CE_Range_Check_Failed
);
11880 -- Discrete-to-discrete conversions
11882 elsif Is_Discrete_Type
(Etype
(N
)) then
11884 Expr
: constant Node_Id
:= Expression
(N
);
11889 if Do_Range_Check
(Expr
)
11890 and then Is_Discrete_Type
(Etype
(Expr
))
11892 Set_Do_Range_Check
(Expr
, False);
11894 -- Before we do a range check, we have to deal with treating
11895 -- a fixed-point operand as an integer. The way we do this
11896 -- is simply to do an unchecked conversion to an appropriate
11897 -- integer type large enough to hold the result.
11899 -- This code is not active yet, because we are only dealing
11900 -- with discrete types so far ???
11902 if Nkind
(Expr
) in N_Has_Treat_Fixed_As_Integer
11903 and then Treat_Fixed_As_Integer
(Expr
)
11905 Ftyp
:= Base_Type
(Etype
(Expr
));
11907 if Esize
(Ftyp
) >= Esize
(Standard_Integer
) then
11908 Ityp
:= Standard_Long_Long_Integer
;
11910 Ityp
:= Standard_Integer
;
11913 Rewrite
(Expr
, Unchecked_Convert_To
(Ityp
, Expr
));
11916 -- Reset overflow flag, since the range check will include
11917 -- dealing with possible overflow, and generate the check.
11918 -- If Address is either a source type or target type,
11919 -- suppress range check to avoid typing anomalies when
11920 -- it is a visible integer type.
11922 Set_Do_Overflow_Check
(N
, False);
11924 if not Is_Descendant_Of_Address
(Etype
(Expr
))
11925 and then not Is_Descendant_Of_Address
(Target_Type
)
11927 Generate_Range_Check
11928 (Expr
, Target_Type
, CE_Range_Check_Failed
);
11935 -- Here at end of processing
11938 -- Apply predicate check if required. Note that we can't just call
11939 -- Apply_Predicate_Check here, because the type looks right after
11940 -- the conversion and it would omit the check. The Comes_From_Source
11941 -- guard is necessary to prevent infinite recursions when we generate
11942 -- internal conversions for the purpose of checking predicates.
11944 if Present
(Predicate_Function
(Target_Type
))
11945 and then not Predicates_Ignored
(Target_Type
)
11946 and then Target_Type
/= Operand_Type
11947 and then Comes_From_Source
(N
)
11950 New_Expr
: constant Node_Id
:= Duplicate_Subexpr
(N
);
11953 -- Avoid infinite recursion on the subsequent expansion of
11954 -- of the copy of the original type conversion.
11956 Set_Comes_From_Source
(New_Expr
, False);
11957 Insert_Action
(N
, Make_Predicate_Check
(Target_Type
, New_Expr
));
11960 end Expand_N_Type_Conversion
;
11962 -----------------------------------
11963 -- Expand_N_Unchecked_Expression --
11964 -----------------------------------
11966 -- Remove the unchecked expression node from the tree. Its job was simply
11967 -- to make sure that its constituent expression was handled with checks
11968 -- off, and now that that is done, we can remove it from the tree, and
11969 -- indeed must, since Gigi does not expect to see these nodes.
11971 procedure Expand_N_Unchecked_Expression
(N
: Node_Id
) is
11972 Exp
: constant Node_Id
:= Expression
(N
);
11974 Set_Assignment_OK
(Exp
, Assignment_OK
(N
) or else Assignment_OK
(Exp
));
11976 end Expand_N_Unchecked_Expression
;
11978 ----------------------------------------
11979 -- Expand_N_Unchecked_Type_Conversion --
11980 ----------------------------------------
11982 -- If this cannot be handled by Gigi and we haven't already made a
11983 -- temporary for it, do it now.
11985 procedure Expand_N_Unchecked_Type_Conversion
(N
: Node_Id
) is
11986 Target_Type
: constant Entity_Id
:= Etype
(N
);
11987 Operand
: constant Node_Id
:= Expression
(N
);
11988 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
11991 -- Nothing at all to do if conversion is to the identical type so remove
11992 -- the conversion completely, it is useless, except that it may carry
11993 -- an Assignment_OK indication which must be propagated to the operand.
11995 if Operand_Type
= Target_Type
then
11997 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
11999 if Assignment_OK
(N
) then
12000 Set_Assignment_OK
(Operand
);
12003 Rewrite
(N
, Relocate_Node
(Operand
));
12007 -- If we have a conversion of a compile time known value to a target
12008 -- type and the value is in range of the target type, then we can simply
12009 -- replace the construct by an integer literal of the correct type. We
12010 -- only apply this to integer types being converted. Possibly it may
12011 -- apply in other cases, but it is too much trouble to worry about.
12013 -- Note that we do not do this transformation if the Kill_Range_Check
12014 -- flag is set, since then the value may be outside the expected range.
12015 -- This happens in the Normalize_Scalars case.
12017 -- We also skip this if either the target or operand type is biased
12018 -- because in this case, the unchecked conversion is supposed to
12019 -- preserve the bit pattern, not the integer value.
12021 if Is_Integer_Type
(Target_Type
)
12022 and then not Has_Biased_Representation
(Target_Type
)
12023 and then Is_Integer_Type
(Operand_Type
)
12024 and then not Has_Biased_Representation
(Operand_Type
)
12025 and then Compile_Time_Known_Value
(Operand
)
12026 and then not Kill_Range_Check
(N
)
12029 Val
: constant Uint
:= Expr_Value
(Operand
);
12032 if Compile_Time_Known_Value
(Type_Low_Bound
(Target_Type
))
12034 Compile_Time_Known_Value
(Type_High_Bound
(Target_Type
))
12036 Val
>= Expr_Value
(Type_Low_Bound
(Target_Type
))
12038 Val
<= Expr_Value
(Type_High_Bound
(Target_Type
))
12040 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Val
));
12042 -- If Address is the target type, just set the type to avoid a
12043 -- spurious type error on the literal when Address is a visible
12046 if Is_Descendant_Of_Address
(Target_Type
) then
12047 Set_Etype
(N
, Target_Type
);
12049 Analyze_And_Resolve
(N
, Target_Type
);
12057 -- Nothing to do if conversion is safe
12059 if Safe_Unchecked_Type_Conversion
(N
) then
12063 -- Otherwise force evaluation unless Assignment_OK flag is set (this
12064 -- flag indicates ??? More comments needed here)
12066 if Assignment_OK
(N
) then
12069 Force_Evaluation
(N
);
12071 end Expand_N_Unchecked_Type_Conversion
;
12073 ----------------------------
12074 -- Expand_Record_Equality --
12075 ----------------------------
12077 -- For non-variant records, Equality is expanded when needed into:
12079 -- and then Lhs.Discr1 = Rhs.Discr1
12081 -- and then Lhs.Discrn = Rhs.Discrn
12082 -- and then Lhs.Cmp1 = Rhs.Cmp1
12084 -- and then Lhs.Cmpn = Rhs.Cmpn
12086 -- The expression is folded by the back end for adjacent fields. This
12087 -- function is called for tagged record in only one occasion: for imple-
12088 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
12089 -- otherwise the primitive "=" is used directly.
12091 function Expand_Record_Equality
12096 Bodies
: List_Id
) return Node_Id
12098 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
12103 First_Time
: Boolean := True;
12105 function Element_To_Compare
(C
: Entity_Id
) return Entity_Id
;
12106 -- Return the next discriminant or component to compare, starting with
12107 -- C, skipping inherited components.
12109 ------------------------
12110 -- Element_To_Compare --
12111 ------------------------
12113 function Element_To_Compare
(C
: Entity_Id
) return Entity_Id
is
12119 -- Exit loop when the next element to be compared is found, or
12120 -- there is no more such element.
12122 exit when No
(Comp
);
12124 exit when Ekind_In
(Comp
, E_Discriminant
, E_Component
)
12127 -- Skip inherited components
12129 -- Note: for a tagged type, we always generate the "=" primitive
12130 -- for the base type (not on the first subtype), so the test for
12131 -- Comp /= Original_Record_Component (Comp) is True for
12132 -- inherited components only.
12134 (Is_Tagged_Type
(Typ
)
12135 and then Comp
/= Original_Record_Component
(Comp
))
12139 or else Chars
(Comp
) = Name_uTag
12141 -- Skip interface elements (secondary tags???)
12143 or else Is_Interface
(Etype
(Comp
)));
12145 Next_Entity
(Comp
);
12149 end Element_To_Compare
;
12151 -- Start of processing for Expand_Record_Equality
12154 -- Generates the following code: (assuming that Typ has one Discr and
12155 -- component C2 is also a record)
12157 -- Lhs.Discr1 = Rhs.Discr1
12158 -- and then Lhs.C1 = Rhs.C1
12159 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
12161 -- and then Lhs.Cmpn = Rhs.Cmpn
12163 Result
:= New_Occurrence_Of
(Standard_True
, Loc
);
12164 C
:= Element_To_Compare
(First_Entity
(Typ
));
12165 while Present
(C
) loop
12176 New_Lhs
:= New_Copy_Tree
(Lhs
);
12177 New_Rhs
:= New_Copy_Tree
(Rhs
);
12181 Expand_Composite_Equality
(Nod
, Etype
(C
),
12183 Make_Selected_Component
(Loc
,
12185 Selector_Name
=> New_Occurrence_Of
(C
, Loc
)),
12187 Make_Selected_Component
(Loc
,
12189 Selector_Name
=> New_Occurrence_Of
(C
, Loc
)),
12192 -- If some (sub)component is an unchecked_union, the whole
12193 -- operation will raise program error.
12195 if Nkind
(Check
) = N_Raise_Program_Error
then
12197 Set_Etype
(Result
, Standard_Boolean
);
12203 -- Generate logical "and" for CodePeer to simplify the
12204 -- generated code and analysis.
12206 elsif CodePeer_Mode
then
12209 Left_Opnd
=> Result
,
12210 Right_Opnd
=> Check
);
12214 Make_And_Then
(Loc
,
12215 Left_Opnd
=> Result
,
12216 Right_Opnd
=> Check
);
12221 First_Time
:= False;
12222 C
:= Element_To_Compare
(Next_Entity
(C
));
12226 end Expand_Record_Equality
;
12228 ---------------------------
12229 -- Expand_Set_Membership --
12230 ---------------------------
12232 procedure Expand_Set_Membership
(N
: Node_Id
) is
12233 Lop
: constant Node_Id
:= Left_Opnd
(N
);
12237 function Make_Cond
(Alt
: Node_Id
) return Node_Id
;
12238 -- If the alternative is a subtype mark, create a simple membership
12239 -- test. Otherwise create an equality test for it.
12245 function Make_Cond
(Alt
: Node_Id
) return Node_Id
is
12247 L
: constant Node_Id
:= New_Copy
(Lop
);
12248 R
: constant Node_Id
:= Relocate_Node
(Alt
);
12251 if (Is_Entity_Name
(Alt
) and then Is_Type
(Entity
(Alt
)))
12252 or else Nkind
(Alt
) = N_Range
12255 Make_In
(Sloc
(Alt
),
12260 Make_Op_Eq
(Sloc
(Alt
),
12268 -- Start of processing for Expand_Set_Membership
12271 Remove_Side_Effects
(Lop
);
12273 Alt
:= Last
(Alternatives
(N
));
12274 Res
:= Make_Cond
(Alt
);
12277 while Present
(Alt
) loop
12279 Make_Or_Else
(Sloc
(Alt
),
12280 Left_Opnd
=> Make_Cond
(Alt
),
12281 Right_Opnd
=> Res
);
12286 Analyze_And_Resolve
(N
, Standard_Boolean
);
12287 end Expand_Set_Membership
;
12289 -----------------------------------
12290 -- Expand_Short_Circuit_Operator --
12291 -----------------------------------
12293 -- Deal with special expansion if actions are present for the right operand
12294 -- and deal with optimizing case of arguments being True or False. We also
12295 -- deal with the special case of non-standard boolean values.
12297 procedure Expand_Short_Circuit_Operator
(N
: Node_Id
) is
12298 Loc
: constant Source_Ptr
:= Sloc
(N
);
12299 Typ
: constant Entity_Id
:= Etype
(N
);
12300 Left
: constant Node_Id
:= Left_Opnd
(N
);
12301 Right
: constant Node_Id
:= Right_Opnd
(N
);
12302 LocR
: constant Source_Ptr
:= Sloc
(Right
);
12305 Shortcut_Value
: constant Boolean := Nkind
(N
) = N_Or_Else
;
12306 Shortcut_Ent
: constant Entity_Id
:= Boolean_Literals
(Shortcut_Value
);
12307 -- If Left = Shortcut_Value then Right need not be evaluated
12309 function Make_Test_Expr
(Opnd
: Node_Id
) return Node_Id
;
12310 -- For Opnd a boolean expression, return a Boolean expression equivalent
12311 -- to Opnd /= Shortcut_Value.
12313 --------------------
12314 -- Make_Test_Expr --
12315 --------------------
12317 function Make_Test_Expr
(Opnd
: Node_Id
) return Node_Id
is
12319 if Shortcut_Value
then
12320 return Make_Op_Not
(Sloc
(Opnd
), Opnd
);
12324 end Make_Test_Expr
;
12328 Op_Var
: Entity_Id
;
12329 -- Entity for a temporary variable holding the value of the operator,
12330 -- used for expansion in the case where actions are present.
12332 -- Start of processing for Expand_Short_Circuit_Operator
12335 -- Deal with non-standard booleans
12337 if Is_Boolean_Type
(Typ
) then
12338 Adjust_Condition
(Left
);
12339 Adjust_Condition
(Right
);
12340 Set_Etype
(N
, Standard_Boolean
);
12343 -- Check for cases where left argument is known to be True or False
12345 if Compile_Time_Known_Value
(Left
) then
12347 -- Mark SCO for left condition as compile time known
12349 if Generate_SCO
and then Comes_From_Source
(Left
) then
12350 Set_SCO_Condition
(Left
, Expr_Value_E
(Left
) = Standard_True
);
12353 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
12354 -- Any actions associated with Right will be executed unconditionally
12355 -- and can thus be inserted into the tree unconditionally.
12357 if Expr_Value_E
(Left
) /= Shortcut_Ent
then
12358 if Present
(Actions
(N
)) then
12359 Insert_Actions
(N
, Actions
(N
));
12362 Rewrite
(N
, Right
);
12364 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
12365 -- In this case we can forget the actions associated with Right,
12366 -- since they will never be executed.
12369 Kill_Dead_Code
(Right
);
12370 Kill_Dead_Code
(Actions
(N
));
12371 Rewrite
(N
, New_Occurrence_Of
(Shortcut_Ent
, Loc
));
12374 Adjust_Result_Type
(N
, Typ
);
12378 -- If Actions are present for the right operand, we have to do some
12379 -- special processing. We can't just let these actions filter back into
12380 -- code preceding the short circuit (which is what would have happened
12381 -- if we had not trapped them in the short-circuit form), since they
12382 -- must only be executed if the right operand of the short circuit is
12383 -- executed and not otherwise.
12385 if Present
(Actions
(N
)) then
12386 Actlist
:= Actions
(N
);
12388 -- The old approach is to expand:
12390 -- left AND THEN right
12394 -- C : Boolean := False;
12402 -- and finally rewrite the operator into a reference to C. Similarly
12403 -- for left OR ELSE right, with negated values. Note that this
12404 -- rewrite causes some difficulties for coverage analysis because
12405 -- of the introduction of the new variable C, which obscures the
12406 -- structure of the test.
12408 -- We use this "old approach" if Minimize_Expression_With_Actions
12411 if Minimize_Expression_With_Actions
then
12412 Op_Var
:= Make_Temporary
(Loc
, 'C', Related_Node
=> N
);
12415 Make_Object_Declaration
(Loc
,
12416 Defining_Identifier
=> Op_Var
,
12417 Object_Definition
=>
12418 New_Occurrence_Of
(Standard_Boolean
, Loc
),
12420 New_Occurrence_Of
(Shortcut_Ent
, Loc
)));
12422 Append_To
(Actlist
,
12423 Make_Implicit_If_Statement
(Right
,
12424 Condition
=> Make_Test_Expr
(Right
),
12425 Then_Statements
=> New_List
(
12426 Make_Assignment_Statement
(LocR
,
12427 Name
=> New_Occurrence_Of
(Op_Var
, LocR
),
12430 (Boolean_Literals
(not Shortcut_Value
), LocR
)))));
12433 Make_Implicit_If_Statement
(Left
,
12434 Condition
=> Make_Test_Expr
(Left
),
12435 Then_Statements
=> Actlist
));
12437 Rewrite
(N
, New_Occurrence_Of
(Op_Var
, Loc
));
12438 Analyze_And_Resolve
(N
, Standard_Boolean
);
12440 -- The new approach (the default) is to use an
12441 -- Expression_With_Actions node for the right operand of the
12442 -- short-circuit form. Note that this solves the traceability
12443 -- problems for coverage analysis.
12447 Make_Expression_With_Actions
(LocR
,
12448 Expression
=> Relocate_Node
(Right
),
12449 Actions
=> Actlist
));
12451 Set_Actions
(N
, No_List
);
12452 Analyze_And_Resolve
(Right
, Standard_Boolean
);
12455 Adjust_Result_Type
(N
, Typ
);
12459 -- No actions present, check for cases of right argument True/False
12461 if Compile_Time_Known_Value
(Right
) then
12463 -- Mark SCO for left condition as compile time known
12465 if Generate_SCO
and then Comes_From_Source
(Right
) then
12466 Set_SCO_Condition
(Right
, Expr_Value_E
(Right
) = Standard_True
);
12469 -- Change (Left and then True), (Left or else False) to Left. Note
12470 -- that we know there are no actions associated with the right
12471 -- operand, since we just checked for this case above.
12473 if Expr_Value_E
(Right
) /= Shortcut_Ent
then
12476 -- Change (Left and then False), (Left or else True) to Right,
12477 -- making sure to preserve any side effects associated with the Left
12481 Remove_Side_Effects
(Left
);
12482 Rewrite
(N
, New_Occurrence_Of
(Shortcut_Ent
, Loc
));
12486 Adjust_Result_Type
(N
, Typ
);
12487 end Expand_Short_Circuit_Operator
;
12489 -------------------------------------
12490 -- Fixup_Universal_Fixed_Operation --
12491 -------------------------------------
12493 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
) is
12494 Conv
: constant Node_Id
:= Parent
(N
);
12497 -- We must have a type conversion immediately above us
12499 pragma Assert
(Nkind
(Conv
) = N_Type_Conversion
);
12501 -- Normally the type conversion gives our target type. The exception
12502 -- occurs in the case of the Round attribute, where the conversion
12503 -- will be to universal real, and our real type comes from the Round
12504 -- attribute (as well as an indication that we must round the result)
12506 if Nkind
(Parent
(Conv
)) = N_Attribute_Reference
12507 and then Attribute_Name
(Parent
(Conv
)) = Name_Round
12509 Set_Etype
(N
, Etype
(Parent
(Conv
)));
12510 Set_Rounded_Result
(N
);
12512 -- Normal case where type comes from conversion above us
12515 Set_Etype
(N
, Etype
(Conv
));
12517 end Fixup_Universal_Fixed_Operation
;
12519 ---------------------------------
12520 -- Has_Inferable_Discriminants --
12521 ---------------------------------
12523 function Has_Inferable_Discriminants
(N
: Node_Id
) return Boolean is
12525 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean;
12526 -- Determines whether the left-most prefix of a selected component is a
12527 -- formal parameter in a subprogram. Assumes N is a selected component.
12529 --------------------------------
12530 -- Prefix_Is_Formal_Parameter --
12531 --------------------------------
12533 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean is
12534 Sel_Comp
: Node_Id
;
12537 -- Move to the left-most prefix by climbing up the tree
12540 while Present
(Parent
(Sel_Comp
))
12541 and then Nkind
(Parent
(Sel_Comp
)) = N_Selected_Component
12543 Sel_Comp
:= Parent
(Sel_Comp
);
12546 return Ekind
(Entity
(Prefix
(Sel_Comp
))) in Formal_Kind
;
12547 end Prefix_Is_Formal_Parameter
;
12549 -- Start of processing for Has_Inferable_Discriminants
12552 -- For selected components, the subtype of the selector must be a
12553 -- constrained Unchecked_Union. If the component is subject to a
12554 -- per-object constraint, then the enclosing object must have inferable
12557 if Nkind
(N
) = N_Selected_Component
then
12558 if Has_Per_Object_Constraint
(Entity
(Selector_Name
(N
))) then
12560 -- A small hack. If we have a per-object constrained selected
12561 -- component of a formal parameter, return True since we do not
12562 -- know the actual parameter association yet.
12564 if Prefix_Is_Formal_Parameter
(N
) then
12567 -- Otherwise, check the enclosing object and the selector
12570 return Has_Inferable_Discriminants
(Prefix
(N
))
12571 and then Has_Inferable_Discriminants
(Selector_Name
(N
));
12574 -- The call to Has_Inferable_Discriminants will determine whether
12575 -- the selector has a constrained Unchecked_Union nominal type.
12578 return Has_Inferable_Discriminants
(Selector_Name
(N
));
12581 -- A qualified expression has inferable discriminants if its subtype
12582 -- mark is a constrained Unchecked_Union subtype.
12584 elsif Nkind
(N
) = N_Qualified_Expression
then
12585 return Is_Unchecked_Union
(Etype
(Subtype_Mark
(N
)))
12586 and then Is_Constrained
(Etype
(Subtype_Mark
(N
)));
12588 -- For all other names, it is sufficient to have a constrained
12589 -- Unchecked_Union nominal subtype.
12592 return Is_Unchecked_Union
(Base_Type
(Etype
(N
)))
12593 and then Is_Constrained
(Etype
(N
));
12595 end Has_Inferable_Discriminants
;
12597 -------------------------------
12598 -- Insert_Dereference_Action --
12599 -------------------------------
12601 procedure Insert_Dereference_Action
(N
: Node_Id
) is
12602 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean;
12603 -- Return true if type of P is derived from Checked_Pool;
12605 -----------------------------
12606 -- Is_Checked_Storage_Pool --
12607 -----------------------------
12609 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean is
12618 while T
/= Etype
(T
) loop
12619 if Is_RTE
(T
, RE_Checked_Pool
) then
12627 end Is_Checked_Storage_Pool
;
12631 Context
: constant Node_Id
:= Parent
(N
);
12632 Ptr_Typ
: constant Entity_Id
:= Etype
(N
);
12633 Desig_Typ
: constant Entity_Id
:=
12634 Available_View
(Designated_Type
(Ptr_Typ
));
12635 Loc
: constant Source_Ptr
:= Sloc
(N
);
12636 Pool
: constant Entity_Id
:= Associated_Storage_Pool
(Ptr_Typ
);
12642 Size_Bits
: Node_Id
;
12645 -- Start of processing for Insert_Dereference_Action
12648 pragma Assert
(Nkind
(Context
) = N_Explicit_Dereference
);
12650 -- Do not re-expand a dereference which has already been processed by
12653 if Has_Dereference_Action
(Context
) then
12656 -- Do not perform this type of expansion for internally-generated
12659 elsif not Comes_From_Source
(Original_Node
(Context
)) then
12662 -- A dereference action is only applicable to objects which have been
12663 -- allocated on a checked pool.
12665 elsif not Is_Checked_Storage_Pool
(Pool
) then
12669 -- Extract the address of the dereferenced object. Generate:
12671 -- Addr : System.Address := <N>'Pool_Address;
12673 Addr
:= Make_Temporary
(Loc
, 'P');
12676 Make_Object_Declaration
(Loc
,
12677 Defining_Identifier
=> Addr
,
12678 Object_Definition
=>
12679 New_Occurrence_Of
(RTE
(RE_Address
), Loc
),
12681 Make_Attribute_Reference
(Loc
,
12682 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
),
12683 Attribute_Name
=> Name_Pool_Address
)));
12685 -- Calculate the size of the dereferenced object. Generate:
12687 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
12690 Make_Explicit_Dereference
(Loc
,
12691 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
12692 Set_Has_Dereference_Action
(Deref
);
12695 Make_Attribute_Reference
(Loc
,
12697 Attribute_Name
=> Name_Size
);
12699 -- Special case of an unconstrained array: need to add descriptor size
12701 if Is_Array_Type
(Desig_Typ
)
12702 and then not Is_Constrained
(First_Subtype
(Desig_Typ
))
12707 Make_Attribute_Reference
(Loc
,
12709 New_Occurrence_Of
(First_Subtype
(Desig_Typ
), Loc
),
12710 Attribute_Name
=> Name_Descriptor_Size
),
12711 Right_Opnd
=> Size_Bits
);
12714 Size
:= Make_Temporary
(Loc
, 'S');
12716 Make_Object_Declaration
(Loc
,
12717 Defining_Identifier
=> Size
,
12718 Object_Definition
=>
12719 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
),
12721 Make_Op_Divide
(Loc
,
12722 Left_Opnd
=> Size_Bits
,
12723 Right_Opnd
=> Make_Integer_Literal
(Loc
, System_Storage_Unit
))));
12725 -- Calculate the alignment of the dereferenced object. Generate:
12726 -- Alig : constant Storage_Count := <N>.all'Alignment;
12729 Make_Explicit_Dereference
(Loc
,
12730 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
12731 Set_Has_Dereference_Action
(Deref
);
12733 Alig
:= Make_Temporary
(Loc
, 'A');
12735 Make_Object_Declaration
(Loc
,
12736 Defining_Identifier
=> Alig
,
12737 Object_Definition
=>
12738 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
),
12740 Make_Attribute_Reference
(Loc
,
12742 Attribute_Name
=> Name_Alignment
)));
12744 -- A dereference of a controlled object requires special processing. The
12745 -- finalization machinery requests additional space from the underlying
12746 -- pool to allocate and hide two pointers. As a result, a checked pool
12747 -- may mark the wrong memory as valid. Since checked pools do not have
12748 -- knowledge of hidden pointers, we have to bring the two pointers back
12749 -- in view in order to restore the original state of the object.
12751 -- The address manipulation is not performed for access types that are
12752 -- subject to pragma No_Heap_Finalization because the two pointers do
12753 -- not exist in the first place.
12755 if No_Heap_Finalization
(Ptr_Typ
) then
12758 elsif Needs_Finalization
(Desig_Typ
) then
12760 -- Adjust the address and size of the dereferenced object. Generate:
12761 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
12764 Make_Procedure_Call_Statement
(Loc
,
12766 New_Occurrence_Of
(RTE
(RE_Adjust_Controlled_Dereference
), Loc
),
12767 Parameter_Associations
=> New_List
(
12768 New_Occurrence_Of
(Addr
, Loc
),
12769 New_Occurrence_Of
(Size
, Loc
),
12770 New_Occurrence_Of
(Alig
, Loc
)));
12772 -- Class-wide types complicate things because we cannot determine
12773 -- statically whether the actual object is truly controlled. We must
12774 -- generate a runtime check to detect this property. Generate:
12776 -- if Needs_Finalization (<N>.all'Tag) then
12780 if Is_Class_Wide_Type
(Desig_Typ
) then
12782 Make_Explicit_Dereference
(Loc
,
12783 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
12784 Set_Has_Dereference_Action
(Deref
);
12787 Make_Implicit_If_Statement
(N
,
12789 Make_Function_Call
(Loc
,
12791 New_Occurrence_Of
(RTE
(RE_Needs_Finalization
), Loc
),
12792 Parameter_Associations
=> New_List
(
12793 Make_Attribute_Reference
(Loc
,
12795 Attribute_Name
=> Name_Tag
))),
12796 Then_Statements
=> New_List
(Stmt
));
12799 Insert_Action
(N
, Stmt
);
12803 -- Dereference (Pool, Addr, Size, Alig);
12806 Make_Procedure_Call_Statement
(Loc
,
12809 (Find_Prim_Op
(Etype
(Pool
), Name_Dereference
), Loc
),
12810 Parameter_Associations
=> New_List
(
12811 New_Occurrence_Of
(Pool
, Loc
),
12812 New_Occurrence_Of
(Addr
, Loc
),
12813 New_Occurrence_Of
(Size
, Loc
),
12814 New_Occurrence_Of
(Alig
, Loc
))));
12816 -- Mark the explicit dereference as processed to avoid potential
12817 -- infinite expansion.
12819 Set_Has_Dereference_Action
(Context
);
12822 when RE_Not_Available
=>
12824 end Insert_Dereference_Action
;
12826 --------------------------------
12827 -- Integer_Promotion_Possible --
12828 --------------------------------
12830 function Integer_Promotion_Possible
(N
: Node_Id
) return Boolean is
12831 Operand
: constant Node_Id
:= Expression
(N
);
12832 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
12833 Root_Operand_Type
: constant Entity_Id
:= Root_Type
(Operand_Type
);
12836 pragma Assert
(Nkind
(N
) = N_Type_Conversion
);
12840 -- We only do the transformation for source constructs. We assume
12841 -- that the expander knows what it is doing when it generates code.
12843 Comes_From_Source
(N
)
12845 -- If the operand type is Short_Integer or Short_Short_Integer,
12846 -- then we will promote to Integer, which is available on all
12847 -- targets, and is sufficient to ensure no intermediate overflow.
12848 -- Furthermore it is likely to be as efficient or more efficient
12849 -- than using the smaller type for the computation so we do this
12850 -- unconditionally.
12853 (Root_Operand_Type
= Base_Type
(Standard_Short_Integer
)
12855 Root_Operand_Type
= Base_Type
(Standard_Short_Short_Integer
))
12857 -- Test for interesting operation, which includes addition,
12858 -- division, exponentiation, multiplication, subtraction, absolute
12859 -- value and unary negation. Unary "+" is omitted since it is a
12860 -- no-op and thus can't overflow.
12862 and then Nkind_In
(Operand
, N_Op_Abs
,
12869 end Integer_Promotion_Possible
;
12871 ------------------------------
12872 -- Make_Array_Comparison_Op --
12873 ------------------------------
12875 -- This is a hand-coded expansion of the following generic function:
12878 -- type elem is (<>);
12879 -- type index is (<>);
12880 -- type a is array (index range <>) of elem;
12882 -- function Gnnn (X : a; Y: a) return boolean is
12883 -- J : index := Y'first;
12886 -- if X'length = 0 then
12889 -- elsif Y'length = 0 then
12893 -- for I in X'range loop
12894 -- if X (I) = Y (J) then
12895 -- if J = Y'last then
12898 -- J := index'succ (J);
12902 -- return X (I) > Y (J);
12906 -- return X'length > Y'length;
12910 -- Note that since we are essentially doing this expansion by hand, we
12911 -- do not need to generate an actual or formal generic part, just the
12912 -- instantiated function itself.
12914 -- Perhaps we could have the actual generic available in the run-time,
12915 -- obtained by rtsfind, and actually expand a real instantiation ???
12917 function Make_Array_Comparison_Op
12919 Nod
: Node_Id
) return Node_Id
12921 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
12923 X
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uX
);
12924 Y
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uY
);
12925 I
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uI
);
12926 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
12928 Index
: constant Entity_Id
:= Base_Type
(Etype
(First_Index
(Typ
)));
12930 Loop_Statement
: Node_Id
;
12931 Loop_Body
: Node_Id
;
12933 Inner_If
: Node_Id
;
12934 Final_Expr
: Node_Id
;
12935 Func_Body
: Node_Id
;
12936 Func_Name
: Entity_Id
;
12942 -- if J = Y'last then
12945 -- J := index'succ (J);
12949 Make_Implicit_If_Statement
(Nod
,
12952 Left_Opnd
=> New_Occurrence_Of
(J
, Loc
),
12954 Make_Attribute_Reference
(Loc
,
12955 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12956 Attribute_Name
=> Name_Last
)),
12958 Then_Statements
=> New_List
(
12959 Make_Exit_Statement
(Loc
)),
12963 Make_Assignment_Statement
(Loc
,
12964 Name
=> New_Occurrence_Of
(J
, Loc
),
12966 Make_Attribute_Reference
(Loc
,
12967 Prefix
=> New_Occurrence_Of
(Index
, Loc
),
12968 Attribute_Name
=> Name_Succ
,
12969 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
))))));
12971 -- if X (I) = Y (J) then
12974 -- return X (I) > Y (J);
12978 Make_Implicit_If_Statement
(Nod
,
12982 Make_Indexed_Component
(Loc
,
12983 Prefix
=> New_Occurrence_Of
(X
, Loc
),
12984 Expressions
=> New_List
(New_Occurrence_Of
(I
, Loc
))),
12987 Make_Indexed_Component
(Loc
,
12988 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12989 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)))),
12991 Then_Statements
=> New_List
(Inner_If
),
12993 Else_Statements
=> New_List
(
12994 Make_Simple_Return_Statement
(Loc
,
12998 Make_Indexed_Component
(Loc
,
12999 Prefix
=> New_Occurrence_Of
(X
, Loc
),
13000 Expressions
=> New_List
(New_Occurrence_Of
(I
, Loc
))),
13003 Make_Indexed_Component
(Loc
,
13004 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
13005 Expressions
=> New_List
(
13006 New_Occurrence_Of
(J
, Loc
)))))));
13008 -- for I in X'range loop
13013 Make_Implicit_Loop_Statement
(Nod
,
13014 Identifier
=> Empty
,
13016 Iteration_Scheme
=>
13017 Make_Iteration_Scheme
(Loc
,
13018 Loop_Parameter_Specification
=>
13019 Make_Loop_Parameter_Specification
(Loc
,
13020 Defining_Identifier
=> I
,
13021 Discrete_Subtype_Definition
=>
13022 Make_Attribute_Reference
(Loc
,
13023 Prefix
=> New_Occurrence_Of
(X
, Loc
),
13024 Attribute_Name
=> Name_Range
))),
13026 Statements
=> New_List
(Loop_Body
));
13028 -- if X'length = 0 then
13030 -- elsif Y'length = 0 then
13033 -- for ... loop ... end loop;
13034 -- return X'length > Y'length;
13038 Make_Attribute_Reference
(Loc
,
13039 Prefix
=> New_Occurrence_Of
(X
, Loc
),
13040 Attribute_Name
=> Name_Length
);
13043 Make_Attribute_Reference
(Loc
,
13044 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
13045 Attribute_Name
=> Name_Length
);
13049 Left_Opnd
=> Length1
,
13050 Right_Opnd
=> Length2
);
13053 Make_Implicit_If_Statement
(Nod
,
13057 Make_Attribute_Reference
(Loc
,
13058 Prefix
=> New_Occurrence_Of
(X
, Loc
),
13059 Attribute_Name
=> Name_Length
),
13061 Make_Integer_Literal
(Loc
, 0)),
13065 Make_Simple_Return_Statement
(Loc
,
13066 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))),
13068 Elsif_Parts
=> New_List
(
13069 Make_Elsif_Part
(Loc
,
13073 Make_Attribute_Reference
(Loc
,
13074 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
13075 Attribute_Name
=> Name_Length
),
13077 Make_Integer_Literal
(Loc
, 0)),
13081 Make_Simple_Return_Statement
(Loc
,
13082 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
))))),
13084 Else_Statements
=> New_List
(
13086 Make_Simple_Return_Statement
(Loc
,
13087 Expression
=> Final_Expr
)));
13091 Formals
:= New_List
(
13092 Make_Parameter_Specification
(Loc
,
13093 Defining_Identifier
=> X
,
13094 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)),
13096 Make_Parameter_Specification
(Loc
,
13097 Defining_Identifier
=> Y
,
13098 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)));
13100 -- function Gnnn (...) return boolean is
13101 -- J : index := Y'first;
13106 Func_Name
:= Make_Temporary
(Loc
, 'G');
13109 Make_Subprogram_Body
(Loc
,
13111 Make_Function_Specification
(Loc
,
13112 Defining_Unit_Name
=> Func_Name
,
13113 Parameter_Specifications
=> Formals
,
13114 Result_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
)),
13116 Declarations
=> New_List
(
13117 Make_Object_Declaration
(Loc
,
13118 Defining_Identifier
=> J
,
13119 Object_Definition
=> New_Occurrence_Of
(Index
, Loc
),
13121 Make_Attribute_Reference
(Loc
,
13122 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
13123 Attribute_Name
=> Name_First
))),
13125 Handled_Statement_Sequence
=>
13126 Make_Handled_Sequence_Of_Statements
(Loc
,
13127 Statements
=> New_List
(If_Stat
)));
13130 end Make_Array_Comparison_Op
;
13132 ---------------------------
13133 -- Make_Boolean_Array_Op --
13134 ---------------------------
13136 -- For logical operations on boolean arrays, expand in line the following,
13137 -- replacing 'and' with 'or' or 'xor' where needed:
13139 -- function Annn (A : typ; B: typ) return typ is
13142 -- for J in A'range loop
13143 -- C (J) := A (J) op B (J);
13148 -- Here typ is the boolean array type
13150 function Make_Boolean_Array_Op
13152 N
: Node_Id
) return Node_Id
13154 Loc
: constant Source_Ptr
:= Sloc
(N
);
13156 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
13157 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
13158 C
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uC
);
13159 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
13167 Func_Name
: Entity_Id
;
13168 Func_Body
: Node_Id
;
13169 Loop_Statement
: Node_Id
;
13173 Make_Indexed_Component
(Loc
,
13174 Prefix
=> New_Occurrence_Of
(A
, Loc
),
13175 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
13178 Make_Indexed_Component
(Loc
,
13179 Prefix
=> New_Occurrence_Of
(B
, Loc
),
13180 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
13183 Make_Indexed_Component
(Loc
,
13184 Prefix
=> New_Occurrence_Of
(C
, Loc
),
13185 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
13187 if Nkind
(N
) = N_Op_And
then
13191 Right_Opnd
=> B_J
);
13193 elsif Nkind
(N
) = N_Op_Or
then
13197 Right_Opnd
=> B_J
);
13203 Right_Opnd
=> B_J
);
13207 Make_Implicit_Loop_Statement
(N
,
13208 Identifier
=> Empty
,
13210 Iteration_Scheme
=>
13211 Make_Iteration_Scheme
(Loc
,
13212 Loop_Parameter_Specification
=>
13213 Make_Loop_Parameter_Specification
(Loc
,
13214 Defining_Identifier
=> J
,
13215 Discrete_Subtype_Definition
=>
13216 Make_Attribute_Reference
(Loc
,
13217 Prefix
=> New_Occurrence_Of
(A
, Loc
),
13218 Attribute_Name
=> Name_Range
))),
13220 Statements
=> New_List
(
13221 Make_Assignment_Statement
(Loc
,
13223 Expression
=> Op
)));
13225 Formals
:= New_List
(
13226 Make_Parameter_Specification
(Loc
,
13227 Defining_Identifier
=> A
,
13228 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)),
13230 Make_Parameter_Specification
(Loc
,
13231 Defining_Identifier
=> B
,
13232 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)));
13234 Func_Name
:= Make_Temporary
(Loc
, 'A');
13235 Set_Is_Inlined
(Func_Name
);
13238 Make_Subprogram_Body
(Loc
,
13240 Make_Function_Specification
(Loc
,
13241 Defining_Unit_Name
=> Func_Name
,
13242 Parameter_Specifications
=> Formals
,
13243 Result_Definition
=> New_Occurrence_Of
(Typ
, Loc
)),
13245 Declarations
=> New_List
(
13246 Make_Object_Declaration
(Loc
,
13247 Defining_Identifier
=> C
,
13248 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
))),
13250 Handled_Statement_Sequence
=>
13251 Make_Handled_Sequence_Of_Statements
(Loc
,
13252 Statements
=> New_List
(
13254 Make_Simple_Return_Statement
(Loc
,
13255 Expression
=> New_Occurrence_Of
(C
, Loc
)))));
13258 end Make_Boolean_Array_Op
;
13260 -----------------------------------------
13261 -- Minimized_Eliminated_Overflow_Check --
13262 -----------------------------------------
13264 function Minimized_Eliminated_Overflow_Check
(N
: Node_Id
) return Boolean is
13267 Is_Signed_Integer_Type
(Etype
(N
))
13268 and then Overflow_Check_Mode
in Minimized_Or_Eliminated
;
13269 end Minimized_Eliminated_Overflow_Check
;
13271 --------------------------------
13272 -- Optimize_Length_Comparison --
13273 --------------------------------
13275 procedure Optimize_Length_Comparison
(N
: Node_Id
) is
13276 Loc
: constant Source_Ptr
:= Sloc
(N
);
13277 Typ
: constant Entity_Id
:= Etype
(N
);
13282 -- First and Last attribute reference nodes, which end up as left and
13283 -- right operands of the optimized result.
13286 -- True for comparison operand of zero
13289 -- Comparison operand, set only if Is_Zero is false
13291 Ent
: Entity_Id
:= Empty
;
13292 -- Entity whose length is being compared
13294 Index
: Node_Id
:= Empty
;
13295 -- Integer_Literal node for length attribute expression, or Empty
13296 -- if there is no such expression present.
13299 -- Type of array index to which 'Length is applied
13301 Op
: Node_Kind
:= Nkind
(N
);
13302 -- Kind of comparison operator, gets flipped if operands backwards
13304 function Is_Optimizable
(N
: Node_Id
) return Boolean;
13305 -- Tests N to see if it is an optimizable comparison value (defined as
13306 -- constant zero or one, or something else where the value is known to
13307 -- be positive and in the range of 32-bits, and where the corresponding
13308 -- Length value is also known to be 32-bits. If result is true, sets
13309 -- Is_Zero, Ityp, and Comp accordingly.
13311 function Is_Entity_Length
(N
: Node_Id
) return Boolean;
13312 -- Tests if N is a length attribute applied to a simple entity. If so,
13313 -- returns True, and sets Ent to the entity, and Index to the integer
13314 -- literal provided as an attribute expression, or to Empty if none.
13315 -- Also returns True if the expression is a generated type conversion
13316 -- whose expression is of the desired form. This latter case arises
13317 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
13318 -- to check for being in range, which is not needed in this context.
13319 -- Returns False if neither condition holds.
13321 function Prepare_64
(N
: Node_Id
) return Node_Id
;
13322 -- Given a discrete expression, returns a Long_Long_Integer typed
13323 -- expression representing the underlying value of the expression.
13324 -- This is done with an unchecked conversion to the result type. We
13325 -- use unchecked conversion to handle the enumeration type case.
13327 ----------------------
13328 -- Is_Entity_Length --
13329 ----------------------
13331 function Is_Entity_Length
(N
: Node_Id
) return Boolean is
13333 if Nkind
(N
) = N_Attribute_Reference
13334 and then Attribute_Name
(N
) = Name_Length
13335 and then Is_Entity_Name
(Prefix
(N
))
13337 Ent
:= Entity
(Prefix
(N
));
13339 if Present
(Expressions
(N
)) then
13340 Index
:= First
(Expressions
(N
));
13347 elsif Nkind
(N
) = N_Type_Conversion
13348 and then not Comes_From_Source
(N
)
13350 return Is_Entity_Length
(Expression
(N
));
13355 end Is_Entity_Length
;
13357 --------------------
13358 -- Is_Optimizable --
13359 --------------------
13361 function Is_Optimizable
(N
: Node_Id
) return Boolean is
13369 if Compile_Time_Known_Value
(N
) then
13370 Val
:= Expr_Value
(N
);
13372 if Val
= Uint_0
then
13377 elsif Val
= Uint_1
then
13384 -- Here we have to make sure of being within 32-bits
13386 Determine_Range
(N
, OK
, Lo
, Hi
, Assume_Valid
=> True);
13389 or else Lo
< Uint_1
13390 or else Hi
> UI_From_Int
(Int
'Last)
13395 -- Comparison value was within range, so now we must check the index
13396 -- value to make sure it is also within 32-bits.
13398 Indx
:= First_Index
(Etype
(Ent
));
13400 if Present
(Index
) then
13401 for J
in 2 .. UI_To_Int
(Intval
(Index
)) loop
13406 Ityp
:= Etype
(Indx
);
13408 if Esize
(Ityp
) > 32 then
13415 end Is_Optimizable
;
13421 function Prepare_64
(N
: Node_Id
) return Node_Id
is
13423 return Unchecked_Convert_To
(Standard_Long_Long_Integer
, N
);
13426 -- Start of processing for Optimize_Length_Comparison
13429 -- Nothing to do if not a comparison
13431 if Op
not in N_Op_Compare
then
13435 -- Nothing to do if special -gnatd.P debug flag set.
13437 if Debug_Flag_Dot_PP
then
13441 -- Ent'Length op 0/1
13443 if Is_Entity_Length
(Left_Opnd
(N
))
13444 and then Is_Optimizable
(Right_Opnd
(N
))
13448 -- 0/1 op Ent'Length
13450 elsif Is_Entity_Length
(Right_Opnd
(N
))
13451 and then Is_Optimizable
(Left_Opnd
(N
))
13453 -- Flip comparison to opposite sense
13456 when N_Op_Lt
=> Op
:= N_Op_Gt
;
13457 when N_Op_Le
=> Op
:= N_Op_Ge
;
13458 when N_Op_Gt
=> Op
:= N_Op_Lt
;
13459 when N_Op_Ge
=> Op
:= N_Op_Le
;
13460 when others => null;
13463 -- Else optimization not possible
13469 -- Fall through if we will do the optimization
13471 -- Cases to handle:
13473 -- X'Length = 0 => X'First > X'Last
13474 -- X'Length = 1 => X'First = X'Last
13475 -- X'Length = n => X'First + (n - 1) = X'Last
13477 -- X'Length /= 0 => X'First <= X'Last
13478 -- X'Length /= 1 => X'First /= X'Last
13479 -- X'Length /= n => X'First + (n - 1) /= X'Last
13481 -- X'Length >= 0 => always true, warn
13482 -- X'Length >= 1 => X'First <= X'Last
13483 -- X'Length >= n => X'First + (n - 1) <= X'Last
13485 -- X'Length > 0 => X'First <= X'Last
13486 -- X'Length > 1 => X'First < X'Last
13487 -- X'Length > n => X'First + (n - 1) < X'Last
13489 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
13490 -- X'Length <= 1 => X'First >= X'Last
13491 -- X'Length <= n => X'First + (n - 1) >= X'Last
13493 -- X'Length < 0 => always false (warn)
13494 -- X'Length < 1 => X'First > X'Last
13495 -- X'Length < n => X'First + (n - 1) > X'Last
13497 -- Note: for the cases of n (not constant 0,1), we require that the
13498 -- corresponding index type be integer or shorter (i.e. not 64-bit),
13499 -- and the same for the comparison value. Then we do the comparison
13500 -- using 64-bit arithmetic (actually long long integer), so that we
13501 -- cannot have overflow intefering with the result.
13503 -- First deal with warning cases
13512 Convert_To
(Typ
, New_Occurrence_Of
(Standard_True
, Loc
)));
13513 Analyze_And_Resolve
(N
, Typ
);
13514 Warn_On_Known_Condition
(N
);
13521 Convert_To
(Typ
, New_Occurrence_Of
(Standard_False
, Loc
)));
13522 Analyze_And_Resolve
(N
, Typ
);
13523 Warn_On_Known_Condition
(N
);
13527 if Constant_Condition_Warnings
13528 and then Comes_From_Source
(Original_Node
(N
))
13530 Error_Msg_N
("could replace by ""'=""?c?", N
);
13540 -- Build the First reference we will use
13543 Make_Attribute_Reference
(Loc
,
13544 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
13545 Attribute_Name
=> Name_First
);
13547 if Present
(Index
) then
13548 Set_Expressions
(Left
, New_List
(New_Copy
(Index
)));
13551 -- If general value case, then do the addition of (n - 1), and
13552 -- also add the needed conversions to type Long_Long_Integer.
13554 if Present
(Comp
) then
13557 Left_Opnd
=> Prepare_64
(Left
),
13559 Make_Op_Subtract
(Loc
,
13560 Left_Opnd
=> Prepare_64
(Comp
),
13561 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)));
13564 -- Build the Last reference we will use
13567 Make_Attribute_Reference
(Loc
,
13568 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
13569 Attribute_Name
=> Name_Last
);
13571 if Present
(Index
) then
13572 Set_Expressions
(Right
, New_List
(New_Copy
(Index
)));
13575 -- If general operand, convert Last reference to Long_Long_Integer
13577 if Present
(Comp
) then
13578 Right
:= Prepare_64
(Right
);
13581 -- Check for cases to optimize
13583 -- X'Length = 0 => X'First > X'Last
13584 -- X'Length < 1 => X'First > X'Last
13585 -- X'Length < n => X'First + (n - 1) > X'Last
13587 if (Is_Zero
and then Op
= N_Op_Eq
)
13588 or else (not Is_Zero
and then Op
= N_Op_Lt
)
13593 Right_Opnd
=> Right
);
13595 -- X'Length = 1 => X'First = X'Last
13596 -- X'Length = n => X'First + (n - 1) = X'Last
13598 elsif not Is_Zero
and then Op
= N_Op_Eq
then
13602 Right_Opnd
=> Right
);
13604 -- X'Length /= 0 => X'First <= X'Last
13605 -- X'Length > 0 => X'First <= X'Last
13607 elsif Is_Zero
and (Op
= N_Op_Ne
or else Op
= N_Op_Gt
) then
13611 Right_Opnd
=> Right
);
13613 -- X'Length /= 1 => X'First /= X'Last
13614 -- X'Length /= n => X'First + (n - 1) /= X'Last
13616 elsif not Is_Zero
and then Op
= N_Op_Ne
then
13620 Right_Opnd
=> Right
);
13622 -- X'Length >= 1 => X'First <= X'Last
13623 -- X'Length >= n => X'First + (n - 1) <= X'Last
13625 elsif not Is_Zero
and then Op
= N_Op_Ge
then
13629 Right_Opnd
=> Right
);
13631 -- X'Length > 1 => X'First < X'Last
13632 -- X'Length > n => X'First + (n = 1) < X'Last
13634 elsif not Is_Zero
and then Op
= N_Op_Gt
then
13638 Right_Opnd
=> Right
);
13640 -- X'Length <= 1 => X'First >= X'Last
13641 -- X'Length <= n => X'First + (n - 1) >= X'Last
13643 elsif not Is_Zero
and then Op
= N_Op_Le
then
13647 Right_Opnd
=> Right
);
13649 -- Should not happen at this stage
13652 raise Program_Error
;
13655 -- Rewrite and finish up
13657 Rewrite
(N
, Result
);
13658 Analyze_And_Resolve
(N
, Typ
);
13660 end Optimize_Length_Comparison
;
13662 --------------------------------
13663 -- Process_If_Case_Statements --
13664 --------------------------------
13666 procedure Process_If_Case_Statements
(N
: Node_Id
; Stmts
: List_Id
) is
13670 Decl
:= First
(Stmts
);
13671 while Present
(Decl
) loop
13672 if Nkind
(Decl
) = N_Object_Declaration
13673 and then Is_Finalizable_Transient
(Decl
, N
)
13675 Process_Transient_In_Expression
(Decl
, N
, Stmts
);
13680 end Process_If_Case_Statements
;
13682 -------------------------------------
13683 -- Process_Transient_In_Expression --
13684 -------------------------------------
13686 procedure Process_Transient_In_Expression
13687 (Obj_Decl
: Node_Id
;
13691 Loc
: constant Source_Ptr
:= Sloc
(Obj_Decl
);
13692 Obj_Id
: constant Entity_Id
:= Defining_Identifier
(Obj_Decl
);
13694 Hook_Context
: constant Node_Id
:= Find_Hook_Context
(Expr
);
13695 -- The node on which to insert the hook as an action. This is usually
13696 -- the innermost enclosing non-transient construct.
13698 Fin_Call
: Node_Id
;
13699 Hook_Assign
: Node_Id
;
13700 Hook_Clear
: Node_Id
;
13701 Hook_Decl
: Node_Id
;
13702 Hook_Insert
: Node_Id
;
13703 Ptr_Decl
: Node_Id
;
13705 Fin_Context
: Node_Id
;
13706 -- The node after which to insert the finalization actions of the
13707 -- transient object.
13710 pragma Assert
(Nkind_In
(Expr
, N_Case_Expression
,
13711 N_Expression_With_Actions
,
13714 -- When the context is a Boolean evaluation, all three nodes capture the
13715 -- result of their computation in a local temporary:
13718 -- Trans_Id : Ctrl_Typ := ...;
13719 -- Result : constant Boolean := ... Trans_Id ...;
13720 -- <finalize Trans_Id>
13723 -- As a result, the finalization of any transient objects can safely
13724 -- take place after the result capture.
13726 -- ??? could this be extended to elementary types?
13728 if Is_Boolean_Type
(Etype
(Expr
)) then
13729 Fin_Context
:= Last
(Stmts
);
13731 -- Otherwise the immediate context may not be safe enough to carry
13732 -- out transient object finalization due to aliasing and nesting of
13733 -- constructs. Insert calls to [Deep_]Finalize after the innermost
13734 -- enclosing non-transient construct.
13737 Fin_Context
:= Hook_Context
;
13740 -- Mark the transient object as successfully processed to avoid double
13743 Set_Is_Finalized_Transient
(Obj_Id
);
13745 -- Construct all the pieces necessary to hook and finalize a transient
13748 Build_Transient_Object_Statements
13749 (Obj_Decl
=> Obj_Decl
,
13750 Fin_Call
=> Fin_Call
,
13751 Hook_Assign
=> Hook_Assign
,
13752 Hook_Clear
=> Hook_Clear
,
13753 Hook_Decl
=> Hook_Decl
,
13754 Ptr_Decl
=> Ptr_Decl
,
13755 Finalize_Obj
=> False);
13757 -- Add the access type which provides a reference to the transient
13758 -- object. Generate:
13760 -- type Ptr_Typ is access all Desig_Typ;
13762 Insert_Action
(Hook_Context
, Ptr_Decl
);
13764 -- Add the temporary which acts as a hook to the transient object.
13767 -- Hook : Ptr_Id := null;
13769 Insert_Action
(Hook_Context
, Hook_Decl
);
13771 -- When the transient object is initialized by an aggregate, the hook
13772 -- must capture the object after the last aggregate assignment takes
13773 -- place. Only then is the object considered initialized. Generate:
13775 -- Hook := Ptr_Typ (Obj_Id);
13777 -- Hook := Obj_Id'Unrestricted_Access;
13779 if Ekind_In
(Obj_Id
, E_Constant
, E_Variable
)
13780 and then Present
(Last_Aggregate_Assignment
(Obj_Id
))
13782 Hook_Insert
:= Last_Aggregate_Assignment
(Obj_Id
);
13784 -- Otherwise the hook seizes the related object immediately
13787 Hook_Insert
:= Obj_Decl
;
13790 Insert_After_And_Analyze
(Hook_Insert
, Hook_Assign
);
13792 -- When the node is part of a return statement, there is no need to
13793 -- insert a finalization call, as the general finalization mechanism
13794 -- (see Build_Finalizer) would take care of the transient object on
13795 -- subprogram exit. Note that it would also be impossible to insert the
13796 -- finalization code after the return statement as this will render it
13799 if Nkind
(Fin_Context
) = N_Simple_Return_Statement
then
13802 -- Finalize the hook after the context has been evaluated. Generate:
13804 -- if Hook /= null then
13805 -- [Deep_]Finalize (Hook.all);
13810 Insert_Action_After
(Fin_Context
,
13811 Make_Implicit_If_Statement
(Obj_Decl
,
13815 New_Occurrence_Of
(Defining_Entity
(Hook_Decl
), Loc
),
13816 Right_Opnd
=> Make_Null
(Loc
)),
13818 Then_Statements
=> New_List
(
13822 end Process_Transient_In_Expression
;
13824 ------------------------
13825 -- Rewrite_Comparison --
13826 ------------------------
13828 procedure Rewrite_Comparison
(N
: Node_Id
) is
13829 Typ
: constant Entity_Id
:= Etype
(N
);
13831 False_Result
: Boolean;
13832 True_Result
: Boolean;
13835 if Nkind
(N
) = N_Type_Conversion
then
13836 Rewrite_Comparison
(Expression
(N
));
13839 elsif Nkind
(N
) not in N_Op_Compare
then
13843 -- Determine the potential outcome of the comparison assuming that the
13844 -- operands are valid and emit a warning when the comparison evaluates
13845 -- to True or False only in the presence of invalid values.
13847 Warn_On_Constant_Valid_Condition
(N
);
13849 -- Determine the potential outcome of the comparison assuming that the
13850 -- operands are not valid.
13854 Assume_Valid
=> False,
13855 True_Result
=> True_Result
,
13856 False_Result
=> False_Result
);
13858 -- The outcome is a decisive False or True, rewrite the operator
13860 if False_Result
or True_Result
then
13863 New_Occurrence_Of
(Boolean_Literals
(True_Result
), Sloc
(N
))));
13865 Analyze_And_Resolve
(N
, Typ
);
13866 Warn_On_Known_Condition
(N
);
13868 end Rewrite_Comparison
;
13870 ----------------------------
13871 -- Safe_In_Place_Array_Op --
13872 ----------------------------
13874 function Safe_In_Place_Array_Op
13877 Op2
: Node_Id
) return Boolean
13879 Target
: Entity_Id
;
13881 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean;
13882 -- Operand is safe if it cannot overlap part of the target of the
13883 -- operation. If the operand and the target are identical, the operand
13884 -- is safe. The operand can be empty in the case of negation.
13886 function Is_Unaliased
(N
: Node_Id
) return Boolean;
13887 -- Check that N is a stand-alone entity
13893 function Is_Unaliased
(N
: Node_Id
) return Boolean is
13897 and then No
(Address_Clause
(Entity
(N
)))
13898 and then No
(Renamed_Object
(Entity
(N
)));
13901 ---------------------
13902 -- Is_Safe_Operand --
13903 ---------------------
13905 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean is
13910 elsif Is_Entity_Name
(Op
) then
13911 return Is_Unaliased
(Op
);
13913 elsif Nkind_In
(Op
, N_Indexed_Component
, N_Selected_Component
) then
13914 return Is_Unaliased
(Prefix
(Op
));
13916 elsif Nkind
(Op
) = N_Slice
then
13918 Is_Unaliased
(Prefix
(Op
))
13919 and then Entity
(Prefix
(Op
)) /= Target
;
13921 elsif Nkind
(Op
) = N_Op_Not
then
13922 return Is_Safe_Operand
(Right_Opnd
(Op
));
13927 end Is_Safe_Operand
;
13929 -- Start of processing for Safe_In_Place_Array_Op
13932 -- Skip this processing if the component size is different from system
13933 -- storage unit (since at least for NOT this would cause problems).
13935 if Component_Size
(Etype
(Lhs
)) /= System_Storage_Unit
then
13938 -- Cannot do in place stuff if non-standard Boolean representation
13940 elsif Has_Non_Standard_Rep
(Component_Type
(Etype
(Lhs
))) then
13943 elsif not Is_Unaliased
(Lhs
) then
13947 Target
:= Entity
(Lhs
);
13948 return Is_Safe_Operand
(Op1
) and then Is_Safe_Operand
(Op2
);
13950 end Safe_In_Place_Array_Op
;
13952 -----------------------
13953 -- Tagged_Membership --
13954 -----------------------
13956 -- There are two different cases to consider depending on whether the right
13957 -- operand is a class-wide type or not. If not we just compare the actual
13958 -- tag of the left expr to the target type tag:
13960 -- Left_Expr.Tag = Right_Type'Tag;
13962 -- If it is a class-wide type we use the RT function CW_Membership which is
13963 -- usually implemented by looking in the ancestor tables contained in the
13964 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
13966 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
13967 -- function IW_Membership which is usually implemented by looking in the
13968 -- table of abstract interface types plus the ancestor table contained in
13969 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
13971 procedure Tagged_Membership
13973 SCIL_Node
: out Node_Id
;
13974 Result
: out Node_Id
)
13976 Left
: constant Node_Id
:= Left_Opnd
(N
);
13977 Right
: constant Node_Id
:= Right_Opnd
(N
);
13978 Loc
: constant Source_Ptr
:= Sloc
(N
);
13980 Full_R_Typ
: Entity_Id
;
13981 Left_Type
: Entity_Id
;
13982 New_Node
: Node_Id
;
13983 Right_Type
: Entity_Id
;
13987 SCIL_Node
:= Empty
;
13989 -- Handle entities from the limited view
13991 Left_Type
:= Available_View
(Etype
(Left
));
13992 Right_Type
:= Available_View
(Etype
(Right
));
13994 -- In the case where the type is an access type, the test is applied
13995 -- using the designated types (needed in Ada 2012 for implicit anonymous
13996 -- access conversions, for AI05-0149).
13998 if Is_Access_Type
(Right_Type
) then
13999 Left_Type
:= Designated_Type
(Left_Type
);
14000 Right_Type
:= Designated_Type
(Right_Type
);
14003 if Is_Class_Wide_Type
(Left_Type
) then
14004 Left_Type
:= Root_Type
(Left_Type
);
14007 if Is_Class_Wide_Type
(Right_Type
) then
14008 Full_R_Typ
:= Underlying_Type
(Root_Type
(Right_Type
));
14010 Full_R_Typ
:= Underlying_Type
(Right_Type
);
14014 Make_Selected_Component
(Loc
,
14015 Prefix
=> Relocate_Node
(Left
),
14017 New_Occurrence_Of
(First_Tag_Component
(Left_Type
), Loc
));
14019 if Is_Class_Wide_Type
(Right_Type
) or else Is_Interface
(Left_Type
) then
14021 -- No need to issue a run-time check if we statically know that the
14022 -- result of this membership test is always true. For example,
14023 -- considering the following declarations:
14025 -- type Iface is interface;
14026 -- type T is tagged null record;
14027 -- type DT is new T and Iface with null record;
14032 -- These membership tests are always true:
14035 -- Obj2 in T'Class;
14036 -- Obj2 in Iface'Class;
14038 -- We do not need to handle cases where the membership is illegal.
14041 -- Obj1 in DT'Class; -- Compile time error
14042 -- Obj1 in Iface'Class; -- Compile time error
14044 if not Is_Class_Wide_Type
(Left_Type
)
14045 and then (Is_Ancestor
(Etype
(Right_Type
), Left_Type
,
14046 Use_Full_View
=> True)
14047 or else (Is_Interface
(Etype
(Right_Type
))
14048 and then Interface_Present_In_Ancestor
14050 Iface
=> Etype
(Right_Type
))))
14052 Result
:= New_Occurrence_Of
(Standard_True
, Loc
);
14056 -- Ada 2005 (AI-251): Class-wide applied to interfaces
14058 if Is_Interface
(Etype
(Class_Wide_Type
(Right_Type
)))
14060 -- Support to: "Iface_CW_Typ in Typ'Class"
14062 or else Is_Interface
(Left_Type
)
14064 -- Issue error if IW_Membership operation not available in a
14065 -- configurable run time setting.
14067 if not RTE_Available
(RE_IW_Membership
) then
14069 ("dynamic membership test on interface types", N
);
14075 Make_Function_Call
(Loc
,
14076 Name
=> New_Occurrence_Of
(RTE
(RE_IW_Membership
), Loc
),
14077 Parameter_Associations
=> New_List
(
14078 Make_Attribute_Reference
(Loc
,
14080 Attribute_Name
=> Name_Address
),
14081 New_Occurrence_Of
(
14082 Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))),
14085 -- Ada 95: Normal case
14088 Build_CW_Membership
(Loc
,
14089 Obj_Tag_Node
=> Obj_Tag
,
14091 New_Occurrence_Of
(
14092 Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))), Loc
),
14094 New_Node
=> New_Node
);
14096 -- Generate the SCIL node for this class-wide membership test.
14097 -- Done here because the previous call to Build_CW_Membership
14098 -- relocates Obj_Tag.
14100 if Generate_SCIL
then
14101 SCIL_Node
:= Make_SCIL_Membership_Test
(Sloc
(N
));
14102 Set_SCIL_Entity
(SCIL_Node
, Etype
(Right_Type
));
14103 Set_SCIL_Tag_Value
(SCIL_Node
, Obj_Tag
);
14106 Result
:= New_Node
;
14109 -- Right_Type is not a class-wide type
14112 -- No need to check the tag of the object if Right_Typ is abstract
14114 if Is_Abstract_Type
(Right_Type
) then
14115 Result
:= New_Occurrence_Of
(Standard_False
, Loc
);
14120 Left_Opnd
=> Obj_Tag
,
14123 (Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))), Loc
));
14126 end Tagged_Membership
;
14128 ------------------------------
14129 -- Unary_Op_Validity_Checks --
14130 ------------------------------
14132 procedure Unary_Op_Validity_Checks
(N
: Node_Id
) is
14134 if Validity_Checks_On
and Validity_Check_Operands
then
14135 Ensure_Valid
(Right_Opnd
(N
));
14137 end Unary_Op_Validity_Checks
;