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
, N_Op_Xor
) 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, or the storage pool
4565 -- is System.Pool_Global.Global_Pool_Object, and the restriction
4566 -- No_Standard_Allocators_After_Elaboration is present, then generate
4567 -- a call to Elaboration_Allocators.Check_Standard_Allocator.
4569 if Nkind
(N
) = N_Allocator
4570 and then (No
(Storage_Pool
(N
))
4571 or else Is_RTE
(Storage_Pool
(N
), RE_Global_Pool_Object
))
4572 and then Restriction_Active
(No_Standard_Allocators_After_Elaboration
)
4575 Make_Procedure_Call_Statement
(Loc
,
4577 New_Occurrence_Of
(RTE
(RE_Check_Standard_Allocator
), Loc
)));
4580 -- Handle case of qualified expression (other than optimization above)
4581 -- First apply constraint checks, because the bounds or discriminants
4582 -- in the aggregate might not match the subtype mark in the allocator.
4584 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
4586 Exp
: constant Node_Id
:= Expression
(Expression
(N
));
4587 Typ
: constant Entity_Id
:= Etype
(Expression
(N
));
4590 Apply_Constraint_Check
(Exp
, Typ
);
4591 Apply_Predicate_Check
(Exp
, Typ
);
4594 Expand_Allocator_Expression
(N
);
4598 -- If the allocator is for a type which requires initialization, and
4599 -- there is no initial value (i.e. operand is a subtype indication
4600 -- rather than a qualified expression), then we must generate a call to
4601 -- the initialization routine using an expressions action node:
4603 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4605 -- Here ptr_T is the pointer type for the allocator, and T is the
4606 -- subtype of the allocator. A special case arises if the designated
4607 -- type of the access type is a task or contains tasks. In this case
4608 -- the call to Init (Temp.all ...) is replaced by code that ensures
4609 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4610 -- for details). In addition, if the type T is a task type, then the
4611 -- first argument to Init must be converted to the task record type.
4614 T
: constant Entity_Id
:= Etype
(Expression
(N
));
4620 Init_Arg1
: Node_Id
;
4621 Init_Call
: Node_Id
;
4622 Temp_Decl
: Node_Id
;
4623 Temp_Type
: Entity_Id
;
4626 if No_Initialization
(N
) then
4628 -- Even though this might be a simple allocation, create a custom
4629 -- Allocate if the context requires it.
4631 if Present
(Finalization_Master
(PtrT
)) then
4632 Build_Allocate_Deallocate_Proc
4634 Is_Allocate
=> True);
4637 -- Optimize the default allocation of an array object when pragma
4638 -- Initialize_Scalars or Normalize_Scalars is in effect. Construct an
4639 -- in-place initialization aggregate which may be convert into a fast
4640 -- memset by the backend.
4642 elsif Init_Or_Norm_Scalars
4643 and then Is_Array_Type
(T
)
4645 -- The array must lack atomic components because they are treated
4646 -- as non-static, and as a result the backend will not initialize
4647 -- the memory in one go.
4649 and then not Has_Atomic_Components
(T
)
4651 -- The array must not be packed because the invalid values in
4652 -- System.Scalar_Values are multiples of Storage_Unit.
4654 and then not Is_Packed
(T
)
4656 -- The array must have static non-empty ranges, otherwise the
4657 -- backend cannot initialize the memory in one go.
4659 and then Has_Static_Non_Empty_Array_Bounds
(T
)
4661 -- The optimization is only relevant for arrays of scalar types
4663 and then Is_Scalar_Type
(Component_Type
(T
))
4665 -- Similar to regular array initialization using a type init proc,
4666 -- predicate checks are not performed because the initialization
4667 -- values are intentionally invalid, and may violate the predicate.
4669 and then not Has_Predicates
(Component_Type
(T
))
4671 -- The component type must have a single initialization value
4673 and then Needs_Simple_Initialization
4674 (Typ
=> Component_Type
(T
),
4675 Consider_IS
=> True)
4678 Temp
:= Make_Temporary
(Loc
, 'P');
4681 -- Temp : Ptr_Typ := new ...;
4686 Make_Object_Declaration
(Loc
,
4687 Defining_Identifier
=> Temp
,
4688 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
4689 Expression
=> Relocate_Node
(N
)),
4690 Suppress
=> All_Checks
);
4693 -- Temp.all := (others => ...);
4698 Make_Assignment_Statement
(Loc
,
4700 Make_Explicit_Dereference
(Loc
,
4701 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)),
4706 Size
=> Esize
(Component_Type
(T
)))),
4707 Suppress
=> All_Checks
);
4709 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
4710 Analyze_And_Resolve
(N
, PtrT
);
4712 -- Case of no initialization procedure present
4714 elsif not Has_Non_Null_Base_Init_Proc
(T
) then
4716 -- Case of simple initialization required
4718 if Needs_Simple_Initialization
(T
) then
4719 Check_Restriction
(No_Default_Initialization
, N
);
4720 Rewrite
(Expression
(N
),
4721 Make_Qualified_Expression
(Loc
,
4722 Subtype_Mark
=> New_Occurrence_Of
(T
, Loc
),
4723 Expression
=> Get_Simple_Init_Val
(T
, N
)));
4725 Analyze_And_Resolve
(Expression
(Expression
(N
)), T
);
4726 Analyze_And_Resolve
(Expression
(N
), T
);
4727 Set_Paren_Count
(Expression
(Expression
(N
)), 1);
4728 Expand_N_Allocator
(N
);
4730 -- No initialization required
4733 Build_Allocate_Deallocate_Proc
4735 Is_Allocate
=> True);
4738 -- Case of initialization procedure present, must be called
4741 Check_Restriction
(No_Default_Initialization
, N
);
4743 if not Restriction_Active
(No_Default_Initialization
) then
4744 Init
:= Base_Init_Proc
(T
);
4746 Temp
:= Make_Temporary
(Loc
, 'P');
4748 -- Construct argument list for the initialization routine call
4751 Make_Explicit_Dereference
(Loc
,
4753 New_Occurrence_Of
(Temp
, Loc
));
4755 Set_Assignment_OK
(Init_Arg1
);
4758 -- The initialization procedure expects a specific type. if the
4759 -- context is access to class wide, indicate that the object
4760 -- being allocated has the right specific type.
4762 if Is_Class_Wide_Type
(Dtyp
) then
4763 Init_Arg1
:= Unchecked_Convert_To
(T
, Init_Arg1
);
4766 -- If designated type is a concurrent type or if it is private
4767 -- type whose definition is a concurrent type, the first
4768 -- argument in the Init routine has to be unchecked conversion
4769 -- to the corresponding record type. If the designated type is
4770 -- a derived type, also convert the argument to its root type.
4772 if Is_Concurrent_Type
(T
) then
4774 Unchecked_Convert_To
(
4775 Corresponding_Record_Type
(T
), Init_Arg1
);
4777 elsif Is_Private_Type
(T
)
4778 and then Present
(Full_View
(T
))
4779 and then Is_Concurrent_Type
(Full_View
(T
))
4782 Unchecked_Convert_To
4783 (Corresponding_Record_Type
(Full_View
(T
)), Init_Arg1
);
4785 elsif Etype
(First_Formal
(Init
)) /= Base_Type
(T
) then
4787 Ftyp
: constant Entity_Id
:= Etype
(First_Formal
(Init
));
4790 Init_Arg1
:= OK_Convert_To
(Etype
(Ftyp
), Init_Arg1
);
4791 Set_Etype
(Init_Arg1
, Ftyp
);
4795 Args
:= New_List
(Init_Arg1
);
4797 -- For the task case, pass the Master_Id of the access type as
4798 -- the value of the _Master parameter, and _Chain as the value
4799 -- of the _Chain parameter (_Chain will be defined as part of
4800 -- the generated code for the allocator).
4802 -- In Ada 2005, the context may be a function that returns an
4803 -- anonymous access type. In that case the Master_Id has been
4804 -- created when expanding the function declaration.
4806 if Has_Task
(T
) then
4807 if No
(Master_Id
(Base_Type
(PtrT
))) then
4809 -- The designated type was an incomplete type, and the
4810 -- access type did not get expanded. Salvage it now.
4812 if not Restriction_Active
(No_Task_Hierarchy
) then
4813 if Present
(Parent
(Base_Type
(PtrT
))) then
4814 Expand_N_Full_Type_Declaration
4815 (Parent
(Base_Type
(PtrT
)));
4817 -- The only other possibility is an itype. For this
4818 -- case, the master must exist in the context. This is
4819 -- the case when the allocator initializes an access
4820 -- component in an init-proc.
4823 pragma Assert
(Is_Itype
(PtrT
));
4824 Build_Master_Renaming
(PtrT
, N
);
4829 -- If the context of the allocator is a declaration or an
4830 -- assignment, we can generate a meaningful image for it,
4831 -- even though subsequent assignments might remove the
4832 -- connection between task and entity. We build this image
4833 -- when the left-hand side is a simple variable, a simple
4834 -- indexed assignment or a simple selected component.
4836 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
4838 Nam
: constant Node_Id
:= Name
(Parent
(N
));
4841 if Is_Entity_Name
(Nam
) then
4843 Build_Task_Image_Decls
4846 (Entity
(Nam
), Sloc
(Nam
)), T
);
4848 elsif Nkind_In
(Nam
, N_Indexed_Component
,
4849 N_Selected_Component
)
4850 and then Is_Entity_Name
(Prefix
(Nam
))
4853 Build_Task_Image_Decls
4854 (Loc
, Nam
, Etype
(Prefix
(Nam
)));
4856 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
4860 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
4862 Build_Task_Image_Decls
4863 (Loc
, Defining_Identifier
(Parent
(N
)), T
);
4866 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
4869 if Restriction_Active
(No_Task_Hierarchy
) then
4871 New_Occurrence_Of
(RTE
(RE_Library_Task_Level
), Loc
));
4875 (Master_Id
(Base_Type
(Root_Type
(PtrT
))), Loc
));
4878 Append_To
(Args
, Make_Identifier
(Loc
, Name_uChain
));
4880 Decl
:= Last
(Decls
);
4882 New_Occurrence_Of
(Defining_Identifier
(Decl
), Loc
));
4884 -- Has_Task is false, Decls not used
4890 -- Add discriminants if discriminated type
4893 Dis
: Boolean := False;
4894 Typ
: Entity_Id
:= Empty
;
4897 if Has_Discriminants
(T
) then
4901 -- Type may be a private type with no visible discriminants
4902 -- in which case check full view if in scope, or the
4903 -- underlying_full_view if dealing with a type whose full
4904 -- view may be derived from a private type whose own full
4905 -- view has discriminants.
4907 elsif Is_Private_Type
(T
) then
4908 if Present
(Full_View
(T
))
4909 and then Has_Discriminants
(Full_View
(T
))
4912 Typ
:= Full_View
(T
);
4914 elsif Present
(Underlying_Full_View
(T
))
4915 and then Has_Discriminants
(Underlying_Full_View
(T
))
4918 Typ
:= Underlying_Full_View
(T
);
4924 -- If the allocated object will be constrained by the
4925 -- default values for discriminants, then build a subtype
4926 -- with those defaults, and change the allocated subtype
4927 -- to that. Note that this happens in fewer cases in Ada
4930 if not Is_Constrained
(Typ
)
4931 and then Present
(Discriminant_Default_Value
4932 (First_Discriminant
(Typ
)))
4933 and then (Ada_Version
< Ada_2005
4935 Object_Type_Has_Constrained_Partial_View
4936 (Typ
, Current_Scope
))
4938 Typ
:= Build_Default_Subtype
(Typ
, N
);
4939 Set_Expression
(N
, New_Occurrence_Of
(Typ
, Loc
));
4942 Discr
:= First_Elmt
(Discriminant_Constraint
(Typ
));
4943 while Present
(Discr
) loop
4944 Nod
:= Node
(Discr
);
4945 Append
(New_Copy_Tree
(Node
(Discr
)), Args
);
4947 -- AI-416: when the discriminant constraint is an
4948 -- anonymous access type make sure an accessibility
4949 -- check is inserted if necessary (3.10.2(22.q/2))
4951 if Ada_Version
>= Ada_2005
4953 Ekind
(Etype
(Nod
)) = E_Anonymous_Access_Type
4955 Apply_Accessibility_Check
4956 (Nod
, Typ
, Insert_Node
=> Nod
);
4964 -- We set the allocator as analyzed so that when we analyze
4965 -- the if expression node, we do not get an unwanted recursive
4966 -- expansion of the allocator expression.
4968 Set_Analyzed
(N
, True);
4969 Nod
:= Relocate_Node
(N
);
4971 -- Here is the transformation:
4972 -- input: new Ctrl_Typ
4973 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4974 -- Ctrl_TypIP (Temp.all, ...);
4975 -- [Deep_]Initialize (Temp.all);
4977 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4978 -- is the subtype of the allocator.
4981 Make_Object_Declaration
(Loc
,
4982 Defining_Identifier
=> Temp
,
4983 Constant_Present
=> True,
4984 Object_Definition
=> New_Occurrence_Of
(Temp_Type
, Loc
),
4987 Set_Assignment_OK
(Temp_Decl
);
4988 Insert_Action
(N
, Temp_Decl
, Suppress
=> All_Checks
);
4990 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
4992 -- If the designated type is a task type or contains tasks,
4993 -- create block to activate created tasks, and insert
4994 -- declaration for Task_Image variable ahead of call.
4996 if Has_Task
(T
) then
4998 L
: constant List_Id
:= New_List
;
5001 Build_Task_Allocate_Block
(L
, Nod
, Args
);
5003 Insert_List_Before
(First
(Declarations
(Blk
)), Decls
);
5004 Insert_Actions
(N
, L
);
5009 Make_Procedure_Call_Statement
(Loc
,
5010 Name
=> New_Occurrence_Of
(Init
, Loc
),
5011 Parameter_Associations
=> Args
));
5014 if Needs_Finalization
(T
) then
5017 -- [Deep_]Initialize (Init_Arg1);
5021 (Obj_Ref
=> New_Copy_Tree
(Init_Arg1
),
5024 -- Guard against a missing [Deep_]Initialize when the
5025 -- designated type was not properly frozen.
5027 if Present
(Init_Call
) then
5028 Insert_Action
(N
, Init_Call
);
5032 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
5033 Analyze_And_Resolve
(N
, PtrT
);
5038 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
5039 -- object that has been rewritten as a reference, we displace "this"
5040 -- to reference properly its secondary dispatch table.
5042 if Nkind
(N
) = N_Identifier
and then Is_Interface
(Dtyp
) then
5043 Displace_Allocator_Pointer
(N
);
5047 when RE_Not_Available
=>
5049 end Expand_N_Allocator
;
5051 -----------------------
5052 -- Expand_N_And_Then --
5053 -----------------------
5055 procedure Expand_N_And_Then
(N
: Node_Id
)
5056 renames Expand_Short_Circuit_Operator
;
5058 ------------------------------
5059 -- Expand_N_Case_Expression --
5060 ------------------------------
5062 procedure Expand_N_Case_Expression
(N
: Node_Id
) is
5064 function Is_Copy_Type
(Typ
: Entity_Id
) return Boolean;
5065 -- Return True if we can copy objects of this type when expanding a case
5072 function Is_Copy_Type
(Typ
: Entity_Id
) return Boolean is
5074 -- If Minimize_Expression_With_Actions is True, we can afford to copy
5075 -- large objects, as long as they are constrained and not limited.
5078 Is_Elementary_Type
(Underlying_Type
(Typ
))
5080 (Minimize_Expression_With_Actions
5081 and then Is_Constrained
(Underlying_Type
(Typ
))
5082 and then not Is_Limited_View
(Underlying_Type
(Typ
)));
5087 Loc
: constant Source_Ptr
:= Sloc
(N
);
5088 Par
: constant Node_Id
:= Parent
(N
);
5089 Typ
: constant Entity_Id
:= Etype
(N
);
5093 Case_Stmt
: Node_Id
;
5097 Target_Typ
: Entity_Id
;
5099 In_Predicate
: Boolean := False;
5100 -- Flag set when the case expression appears within a predicate
5102 Optimize_Return_Stmt
: Boolean := False;
5103 -- Flag set when the case expression can be optimized in the context of
5104 -- a simple return statement.
5106 -- Start of processing for Expand_N_Case_Expression
5109 -- Check for MINIMIZED/ELIMINATED overflow mode
5111 if Minimized_Eliminated_Overflow_Check
(N
) then
5112 Apply_Arithmetic_Overflow_Check
(N
);
5116 -- If the case expression is a predicate specification, and the type
5117 -- to which it applies has a static predicate aspect, do not expand,
5118 -- because it will be converted to the proper predicate form later.
5120 if Ekind_In
(Current_Scope
, E_Function
, E_Procedure
)
5121 and then Is_Predicate_Function
(Current_Scope
)
5123 In_Predicate
:= True;
5125 if Has_Static_Predicate_Aspect
(Etype
(First_Entity
(Current_Scope
)))
5131 -- When the type of the case expression is elementary, expand
5133 -- (case X is when A => AX, when B => BX ...)
5148 -- In all other cases expand into
5151 -- type Ptr_Typ is access all Typ;
5152 -- Target : Ptr_Typ;
5155 -- Target := AX'Unrestricted_Access;
5157 -- Target := BX'Unrestricted_Access;
5160 -- in Target.all end;
5162 -- This approach avoids extra copies of potentially large objects. It
5163 -- also allows handling of values of limited or unconstrained types.
5164 -- Note that we do the copy also for constrained, nonlimited types
5165 -- when minimizing expressions with actions (e.g. when generating C
5166 -- code) since it allows us to do the optimization below in more cases.
5168 -- Small optimization: when the case expression appears in the context
5169 -- of a simple return statement, expand into
5180 Make_Case_Statement
(Loc
,
5181 Expression
=> Expression
(N
),
5182 Alternatives
=> New_List
);
5184 -- Preserve the original context for which the case statement is being
5185 -- generated. This is needed by the finalization machinery to prevent
5186 -- the premature finalization of controlled objects found within the
5189 Set_From_Conditional_Expression
(Case_Stmt
);
5194 if Is_Copy_Type
(Typ
) then
5197 -- ??? Do not perform the optimization when the return statement is
5198 -- within a predicate function, as this causes spurious errors. Could
5199 -- this be a possible mismatch in handling this case somewhere else
5200 -- in semantic analysis?
5202 Optimize_Return_Stmt
:=
5203 Nkind
(Par
) = N_Simple_Return_Statement
and then not In_Predicate
;
5205 -- Otherwise create an access type to handle the general case using
5206 -- 'Unrestricted_Access.
5209 -- type Ptr_Typ is access all Typ;
5212 if Generate_C_Code
then
5214 -- We cannot ensure that correct C code will be generated if any
5215 -- temporary is created down the line (to e.g. handle checks or
5216 -- capture values) since we might end up with dangling references
5217 -- to local variables, so better be safe and reject the construct.
5220 ("case expression too complex, use case statement instead", N
);
5223 Target_Typ
:= Make_Temporary
(Loc
, 'P');
5226 Make_Full_Type_Declaration
(Loc
,
5227 Defining_Identifier
=> Target_Typ
,
5229 Make_Access_To_Object_Definition
(Loc
,
5230 All_Present
=> True,
5231 Subtype_Indication
=> New_Occurrence_Of
(Typ
, Loc
))));
5234 -- Create the declaration of the target which captures the value of the
5238 -- Target : [Ptr_]Typ;
5240 if not Optimize_Return_Stmt
then
5241 Target
:= Make_Temporary
(Loc
, 'T');
5244 Make_Object_Declaration
(Loc
,
5245 Defining_Identifier
=> Target
,
5246 Object_Definition
=> New_Occurrence_Of
(Target_Typ
, Loc
));
5247 Set_No_Initialization
(Decl
);
5249 Append_To
(Acts
, Decl
);
5252 -- Process the alternatives
5254 Alt
:= First
(Alternatives
(N
));
5255 while Present
(Alt
) loop
5257 Alt_Expr
: Node_Id
:= Expression
(Alt
);
5258 Alt_Loc
: constant Source_Ptr
:= Sloc
(Alt_Expr
);
5262 -- Take the unrestricted access of the expression value for non-
5263 -- scalar types. This approach avoids big copies and covers the
5264 -- limited and unconstrained cases.
5267 -- AX'Unrestricted_Access
5269 if not Is_Copy_Type
(Typ
) then
5271 Make_Attribute_Reference
(Alt_Loc
,
5272 Prefix
=> Relocate_Node
(Alt_Expr
),
5273 Attribute_Name
=> Name_Unrestricted_Access
);
5277 -- return AX['Unrestricted_Access];
5279 if Optimize_Return_Stmt
then
5281 Make_Simple_Return_Statement
(Alt_Loc
,
5282 Expression
=> Alt_Expr
));
5285 -- Target := AX['Unrestricted_Access];
5289 Make_Assignment_Statement
(Alt_Loc
,
5290 Name
=> New_Occurrence_Of
(Target
, Loc
),
5291 Expression
=> Alt_Expr
));
5294 -- Propagate declarations inserted in the node by Insert_Actions
5295 -- (for example, temporaries generated to remove side effects).
5296 -- These actions must remain attached to the alternative, given
5297 -- that they are generated by the corresponding expression.
5299 if Present
(Actions
(Alt
)) then
5300 Prepend_List
(Actions
(Alt
), Stmts
);
5303 -- Finalize any transient objects on exit from the alternative.
5304 -- This is done only in the return optimization case because
5305 -- otherwise the case expression is converted into an expression
5306 -- with actions which already contains this form of processing.
5308 if Optimize_Return_Stmt
then
5309 Process_If_Case_Statements
(N
, Stmts
);
5313 (Alternatives
(Case_Stmt
),
5314 Make_Case_Statement_Alternative
(Sloc
(Alt
),
5315 Discrete_Choices
=> Discrete_Choices
(Alt
),
5316 Statements
=> Stmts
));
5322 -- Rewrite the parent return statement as a case statement
5324 if Optimize_Return_Stmt
then
5325 Rewrite
(Par
, Case_Stmt
);
5328 -- Otherwise convert the case expression into an expression with actions
5331 Append_To
(Acts
, Case_Stmt
);
5333 if Is_Copy_Type
(Typ
) then
5334 Expr
:= New_Occurrence_Of
(Target
, Loc
);
5338 Make_Explicit_Dereference
(Loc
,
5339 Prefix
=> New_Occurrence_Of
(Target
, Loc
));
5345 -- in Target[.all] end;
5348 Make_Expression_With_Actions
(Loc
,
5352 Analyze_And_Resolve
(N
, Typ
);
5354 end Expand_N_Case_Expression
;
5356 -----------------------------------
5357 -- Expand_N_Explicit_Dereference --
5358 -----------------------------------
5360 procedure Expand_N_Explicit_Dereference
(N
: Node_Id
) is
5362 -- Insert explicit dereference call for the checked storage pool case
5364 Insert_Dereference_Action
(Prefix
(N
));
5366 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5367 -- we set the atomic sync flag.
5369 if Is_Atomic
(Etype
(N
))
5370 and then not Atomic_Synchronization_Disabled
(Etype
(N
))
5372 Activate_Atomic_Synchronization
(N
);
5374 end Expand_N_Explicit_Dereference
;
5376 --------------------------------------
5377 -- Expand_N_Expression_With_Actions --
5378 --------------------------------------
5380 procedure Expand_N_Expression_With_Actions
(N
: Node_Id
) is
5381 Acts
: constant List_Id
:= Actions
(N
);
5383 procedure Force_Boolean_Evaluation
(Expr
: Node_Id
);
5384 -- Force the evaluation of Boolean expression Expr
5386 function Process_Action
(Act
: Node_Id
) return Traverse_Result
;
5387 -- Inspect and process a single action of an expression_with_actions for
5388 -- transient objects. If such objects are found, the routine generates
5389 -- code to clean them up when the context of the expression is evaluated
5392 ------------------------------
5393 -- Force_Boolean_Evaluation --
5394 ------------------------------
5396 procedure Force_Boolean_Evaluation
(Expr
: Node_Id
) is
5397 Loc
: constant Source_Ptr
:= Sloc
(N
);
5398 Flag_Decl
: Node_Id
;
5399 Flag_Id
: Entity_Id
;
5402 -- Relocate the expression to the actions list by capturing its value
5403 -- in a Boolean flag. Generate:
5404 -- Flag : constant Boolean := Expr;
5406 Flag_Id
:= Make_Temporary
(Loc
, 'F');
5409 Make_Object_Declaration
(Loc
,
5410 Defining_Identifier
=> Flag_Id
,
5411 Constant_Present
=> True,
5412 Object_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
),
5413 Expression
=> Relocate_Node
(Expr
));
5415 Append
(Flag_Decl
, Acts
);
5416 Analyze
(Flag_Decl
);
5418 -- Replace the expression with a reference to the flag
5420 Rewrite
(Expression
(N
), New_Occurrence_Of
(Flag_Id
, Loc
));
5421 Analyze
(Expression
(N
));
5422 end Force_Boolean_Evaluation
;
5424 --------------------
5425 -- Process_Action --
5426 --------------------
5428 function Process_Action
(Act
: Node_Id
) return Traverse_Result
is
5430 if Nkind
(Act
) = N_Object_Declaration
5431 and then Is_Finalizable_Transient
(Act
, N
)
5433 Process_Transient_In_Expression
(Act
, N
, Acts
);
5436 -- Avoid processing temporary function results multiple times when
5437 -- dealing with nested expression_with_actions.
5439 elsif Nkind
(Act
) = N_Expression_With_Actions
then
5442 -- Do not process temporary function results in loops. This is done
5443 -- by Expand_N_Loop_Statement and Build_Finalizer.
5445 elsif Nkind
(Act
) = N_Loop_Statement
then
5452 procedure Process_Single_Action
is new Traverse_Proc
(Process_Action
);
5458 -- Start of processing for Expand_N_Expression_With_Actions
5461 -- Do not evaluate the expression when it denotes an entity because the
5462 -- expression_with_actions node will be replaced by the reference.
5464 if Is_Entity_Name
(Expression
(N
)) then
5467 -- Do not evaluate the expression when there are no actions because the
5468 -- expression_with_actions node will be replaced by the expression.
5470 elsif No
(Acts
) or else Is_Empty_List
(Acts
) then
5473 -- Force the evaluation of the expression by capturing its value in a
5474 -- temporary. This ensures that aliases of transient objects do not leak
5475 -- to the expression of the expression_with_actions node:
5478 -- Trans_Id : Ctrl_Typ := ...;
5479 -- Alias : ... := Trans_Id;
5480 -- in ... Alias ... end;
5482 -- In the example above, Trans_Id cannot be finalized at the end of the
5483 -- actions list because this may affect the alias and the final value of
5484 -- the expression_with_actions. Forcing the evaluation encapsulates the
5485 -- reference to the Alias within the actions list:
5488 -- Trans_Id : Ctrl_Typ := ...;
5489 -- Alias : ... := Trans_Id;
5490 -- Val : constant Boolean := ... Alias ...;
5491 -- <finalize Trans_Id>
5494 -- Once this transformation is performed, it is safe to finalize the
5495 -- transient object at the end of the actions list.
5497 -- Note that Force_Evaluation does not remove side effects in operators
5498 -- because it assumes that all operands are evaluated and side effect
5499 -- free. This is not the case when an operand depends implicitly on the
5500 -- transient object through the use of access types.
5502 elsif Is_Boolean_Type
(Etype
(Expression
(N
))) then
5503 Force_Boolean_Evaluation
(Expression
(N
));
5505 -- The expression of an expression_with_actions node may not necessarily
5506 -- be Boolean when the node appears in an if expression. In this case do
5507 -- the usual forced evaluation to encapsulate potential aliasing.
5510 Force_Evaluation
(Expression
(N
));
5513 -- Process all transient objects found within the actions of the EWA
5516 Act
:= First
(Acts
);
5517 while Present
(Act
) loop
5518 Process_Single_Action
(Act
);
5522 -- Deal with case where there are no actions. In this case we simply
5523 -- rewrite the node with its expression since we don't need the actions
5524 -- and the specification of this node does not allow a null action list.
5526 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5527 -- the expanded tree and relying on being able to retrieve the original
5528 -- tree in cases like this. This raises a whole lot of issues of whether
5529 -- we have problems elsewhere, which will be addressed in the future???
5531 if Is_Empty_List
(Acts
) then
5532 Rewrite
(N
, Relocate_Node
(Expression
(N
)));
5534 end Expand_N_Expression_With_Actions
;
5536 ----------------------------
5537 -- Expand_N_If_Expression --
5538 ----------------------------
5540 -- Deal with limited types and condition actions
5542 procedure Expand_N_If_Expression
(N
: Node_Id
) is
5543 Cond
: constant Node_Id
:= First
(Expressions
(N
));
5544 Loc
: constant Source_Ptr
:= Sloc
(N
);
5545 Thenx
: constant Node_Id
:= Next
(Cond
);
5546 Elsex
: constant Node_Id
:= Next
(Thenx
);
5547 Typ
: constant Entity_Id
:= Etype
(N
);
5556 -- Check for MINIMIZED/ELIMINATED overflow mode
5558 if Minimized_Eliminated_Overflow_Check
(N
) then
5559 Apply_Arithmetic_Overflow_Check
(N
);
5563 -- Fold at compile time if condition known. We have already folded
5564 -- static if expressions, but it is possible to fold any case in which
5565 -- the condition is known at compile time, even though the result is
5568 -- Note that we don't do the fold of such cases in Sem_Elab because
5569 -- it can cause infinite loops with the expander adding a conditional
5570 -- expression, and Sem_Elab circuitry removing it repeatedly.
5572 if Compile_Time_Known_Value
(Cond
) then
5574 function Fold_Known_Value
(Cond
: Node_Id
) return Boolean;
5575 -- Fold at compile time. Assumes condition known. Return True if
5576 -- folding occurred, meaning we're done.
5578 ----------------------
5579 -- Fold_Known_Value --
5580 ----------------------
5582 function Fold_Known_Value
(Cond
: Node_Id
) return Boolean is
5584 if Is_True
(Expr_Value
(Cond
)) then
5586 Actions
:= Then_Actions
(N
);
5589 Actions
:= Else_Actions
(N
);
5594 if Present
(Actions
) then
5596 -- To minimize the use of Expression_With_Actions, just skip
5597 -- the optimization as it is not critical for correctness.
5599 if Minimize_Expression_With_Actions
then
5604 Make_Expression_With_Actions
(Loc
,
5605 Expression
=> Relocate_Node
(Expr
),
5606 Actions
=> Actions
));
5607 Analyze_And_Resolve
(N
, Typ
);
5610 Rewrite
(N
, Relocate_Node
(Expr
));
5613 -- Note that the result is never static (legitimate cases of
5614 -- static if expressions were folded in Sem_Eval).
5616 Set_Is_Static_Expression
(N
, False);
5618 end Fold_Known_Value
;
5621 if Fold_Known_Value
(Cond
) then
5627 -- If the type is limited, and the back end does not handle limited
5628 -- types, then we expand as follows to avoid the possibility of
5629 -- improper copying.
5631 -- type Ptr is access all Typ;
5635 -- Cnn := then-expr'Unrestricted_Access;
5638 -- Cnn := else-expr'Unrestricted_Access;
5641 -- and replace the if expression by a reference to Cnn.all.
5643 -- This special case can be skipped if the back end handles limited
5644 -- types properly and ensures that no incorrect copies are made.
5646 if Is_By_Reference_Type
(Typ
)
5647 and then not Back_End_Handles_Limited_Types
5649 -- When the "then" or "else" expressions involve controlled function
5650 -- calls, generated temporaries are chained on the corresponding list
5651 -- of actions. These temporaries need to be finalized after the if
5652 -- expression is evaluated.
5654 Process_If_Case_Statements
(N
, Then_Actions
(N
));
5655 Process_If_Case_Statements
(N
, Else_Actions
(N
));
5658 Cnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'C', N
);
5659 Ptr_Typ
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
5663 -- type Ann is access all Typ;
5666 Make_Full_Type_Declaration
(Loc
,
5667 Defining_Identifier
=> Ptr_Typ
,
5669 Make_Access_To_Object_Definition
(Loc
,
5670 All_Present
=> True,
5671 Subtype_Indication
=> New_Occurrence_Of
(Typ
, Loc
))));
5677 Make_Object_Declaration
(Loc
,
5678 Defining_Identifier
=> Cnn
,
5679 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
));
5683 -- Cnn := <Thenx>'Unrestricted_Access;
5685 -- Cnn := <Elsex>'Unrestricted_Access;
5689 Make_Implicit_If_Statement
(N
,
5690 Condition
=> Relocate_Node
(Cond
),
5691 Then_Statements
=> New_List
(
5692 Make_Assignment_Statement
(Sloc
(Thenx
),
5693 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Thenx
)),
5695 Make_Attribute_Reference
(Loc
,
5696 Prefix
=> Relocate_Node
(Thenx
),
5697 Attribute_Name
=> Name_Unrestricted_Access
))),
5699 Else_Statements
=> New_List
(
5700 Make_Assignment_Statement
(Sloc
(Elsex
),
5701 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Elsex
)),
5703 Make_Attribute_Reference
(Loc
,
5704 Prefix
=> Relocate_Node
(Elsex
),
5705 Attribute_Name
=> Name_Unrestricted_Access
))));
5707 -- Preserve the original context for which the if statement is
5708 -- being generated. This is needed by the finalization machinery
5709 -- to prevent the premature finalization of controlled objects
5710 -- found within the if statement.
5712 Set_From_Conditional_Expression
(New_If
);
5715 Make_Explicit_Dereference
(Loc
,
5716 Prefix
=> New_Occurrence_Of
(Cnn
, Loc
));
5719 -- If the result is an unconstrained array and the if expression is in a
5720 -- context other than the initializing expression of the declaration of
5721 -- an object, then we pull out the if expression as follows:
5723 -- Cnn : constant typ := if-expression
5725 -- and then replace the if expression with an occurrence of Cnn. This
5726 -- avoids the need in the back end to create on-the-fly variable length
5727 -- temporaries (which it cannot do!)
5729 -- Note that the test for being in an object declaration avoids doing an
5730 -- unnecessary expansion, and also avoids infinite recursion.
5732 elsif Is_Array_Type
(Typ
) and then not Is_Constrained
(Typ
)
5733 and then (Nkind
(Parent
(N
)) /= N_Object_Declaration
5734 or else Expression
(Parent
(N
)) /= N
)
5737 Cnn
: constant Node_Id
:= Make_Temporary
(Loc
, 'C', N
);
5741 Make_Object_Declaration
(Loc
,
5742 Defining_Identifier
=> Cnn
,
5743 Constant_Present
=> True,
5744 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
5745 Expression
=> Relocate_Node
(N
),
5746 Has_Init_Expression
=> True));
5748 Rewrite
(N
, New_Occurrence_Of
(Cnn
, Loc
));
5752 -- For other types, we only need to expand if there are other actions
5753 -- associated with either branch.
5755 elsif Present
(Then_Actions
(N
)) or else Present
(Else_Actions
(N
)) then
5757 -- We now wrap the actions into the appropriate expression
5759 if Minimize_Expression_With_Actions
5760 and then (Is_Elementary_Type
(Underlying_Type
(Typ
))
5761 or else Is_Constrained
(Underlying_Type
(Typ
)))
5763 -- If we can't use N_Expression_With_Actions nodes, then we insert
5764 -- the following sequence of actions (using Insert_Actions):
5769 -- Cnn := then-expr;
5775 -- and replace the if expression by a reference to Cnn
5778 Cnn
: constant Node_Id
:= Make_Temporary
(Loc
, 'C', N
);
5782 Make_Object_Declaration
(Loc
,
5783 Defining_Identifier
=> Cnn
,
5784 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
5787 Make_Implicit_If_Statement
(N
,
5788 Condition
=> Relocate_Node
(Cond
),
5790 Then_Statements
=> New_List
(
5791 Make_Assignment_Statement
(Sloc
(Thenx
),
5792 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Thenx
)),
5793 Expression
=> Relocate_Node
(Thenx
))),
5795 Else_Statements
=> New_List
(
5796 Make_Assignment_Statement
(Sloc
(Elsex
),
5797 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Elsex
)),
5798 Expression
=> Relocate_Node
(Elsex
))));
5800 Set_Assignment_OK
(Name
(First
(Then_Statements
(New_If
))));
5801 Set_Assignment_OK
(Name
(First
(Else_Statements
(New_If
))));
5803 New_N
:= New_Occurrence_Of
(Cnn
, Loc
);
5806 -- Regular path using Expression_With_Actions
5809 if Present
(Then_Actions
(N
)) then
5811 Make_Expression_With_Actions
(Sloc
(Thenx
),
5812 Actions
=> Then_Actions
(N
),
5813 Expression
=> Relocate_Node
(Thenx
)));
5815 Set_Then_Actions
(N
, No_List
);
5816 Analyze_And_Resolve
(Thenx
, Typ
);
5819 if Present
(Else_Actions
(N
)) then
5821 Make_Expression_With_Actions
(Sloc
(Elsex
),
5822 Actions
=> Else_Actions
(N
),
5823 Expression
=> Relocate_Node
(Elsex
)));
5825 Set_Else_Actions
(N
, No_List
);
5826 Analyze_And_Resolve
(Elsex
, Typ
);
5832 -- If no actions then no expansion needed, gigi will handle it using the
5833 -- same approach as a C conditional expression.
5839 -- Fall through here for either the limited expansion, or the case of
5840 -- inserting actions for nonlimited types. In both these cases, we must
5841 -- move the SLOC of the parent If statement to the newly created one and
5842 -- change it to the SLOC of the expression which, after expansion, will
5843 -- correspond to what is being evaluated.
5845 if Present
(Parent
(N
)) and then Nkind
(Parent
(N
)) = N_If_Statement
then
5846 Set_Sloc
(New_If
, Sloc
(Parent
(N
)));
5847 Set_Sloc
(Parent
(N
), Loc
);
5850 -- Make sure Then_Actions and Else_Actions are appropriately moved
5851 -- to the new if statement.
5853 if Present
(Then_Actions
(N
)) then
5855 (First
(Then_Statements
(New_If
)), Then_Actions
(N
));
5858 if Present
(Else_Actions
(N
)) then
5860 (First
(Else_Statements
(New_If
)), Else_Actions
(N
));
5863 Insert_Action
(N
, Decl
);
5864 Insert_Action
(N
, New_If
);
5866 Analyze_And_Resolve
(N
, Typ
);
5867 end Expand_N_If_Expression
;
5873 procedure Expand_N_In
(N
: Node_Id
) is
5874 Loc
: constant Source_Ptr
:= Sloc
(N
);
5875 Restyp
: constant Entity_Id
:= Etype
(N
);
5876 Lop
: constant Node_Id
:= Left_Opnd
(N
);
5877 Rop
: constant Node_Id
:= Right_Opnd
(N
);
5878 Static
: constant Boolean := Is_OK_Static_Expression
(N
);
5880 procedure Substitute_Valid_Check
;
5881 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5882 -- test for the left operand being in range of its subtype.
5884 ----------------------------
5885 -- Substitute_Valid_Check --
5886 ----------------------------
5888 procedure Substitute_Valid_Check
is
5889 function Is_OK_Object_Reference
(Nod
: Node_Id
) return Boolean;
5890 -- Determine whether arbitrary node Nod denotes a source object that
5891 -- may safely act as prefix of attribute 'Valid.
5893 ----------------------------
5894 -- Is_OK_Object_Reference --
5895 ----------------------------
5897 function Is_OK_Object_Reference
(Nod
: Node_Id
) return Boolean is
5901 -- Inspect the original operand
5903 Obj_Ref
:= Original_Node
(Nod
);
5905 -- The object reference must be a source construct, otherwise the
5906 -- codefix suggestion may refer to nonexistent code from a user
5909 if Comes_From_Source
(Obj_Ref
) then
5911 -- Recover the actual object reference. There may be more cases
5915 if Nkind_In
(Obj_Ref
, N_Type_Conversion
,
5916 N_Unchecked_Type_Conversion
)
5918 Obj_Ref
:= Expression
(Obj_Ref
);
5924 return Is_Object_Reference
(Obj_Ref
);
5928 end Is_OK_Object_Reference
;
5930 -- Start of processing for Substitute_Valid_Check
5934 Make_Attribute_Reference
(Loc
,
5935 Prefix
=> Relocate_Node
(Lop
),
5936 Attribute_Name
=> Name_Valid
));
5938 Analyze_And_Resolve
(N
, Restyp
);
5940 -- Emit a warning when the left-hand operand of the membership test
5941 -- is a source object, otherwise the use of attribute 'Valid would be
5942 -- illegal. The warning is not given when overflow checking is either
5943 -- MINIMIZED or ELIMINATED, as the danger of optimization has been
5944 -- eliminated above.
5946 if Is_OK_Object_Reference
(Lop
)
5947 and then Overflow_Check_Mode
not in Minimized_Or_Eliminated
5950 ("??explicit membership test may be optimized away", N
);
5951 Error_Msg_N
-- CODEFIX
5952 ("\??use ''Valid attribute instead", N
);
5954 end Substitute_Valid_Check
;
5961 -- Start of processing for Expand_N_In
5964 -- If set membership case, expand with separate procedure
5966 if Present
(Alternatives
(N
)) then
5967 Expand_Set_Membership
(N
);
5971 -- Not set membership, proceed with expansion
5973 Ltyp
:= Etype
(Left_Opnd
(N
));
5974 Rtyp
:= Etype
(Right_Opnd
(N
));
5976 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
5977 -- type, then expand with a separate procedure. Note the use of the
5978 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5980 if Overflow_Check_Mode
in Minimized_Or_Eliminated
5981 and then Is_Signed_Integer_Type
(Ltyp
)
5982 and then not No_Minimize_Eliminate
(N
)
5984 Expand_Membership_Minimize_Eliminate_Overflow
(N
);
5988 -- Check case of explicit test for an expression in range of its
5989 -- subtype. This is suspicious usage and we replace it with a 'Valid
5990 -- test and give a warning for scalar types.
5992 if Is_Scalar_Type
(Ltyp
)
5994 -- Only relevant for source comparisons
5996 and then Comes_From_Source
(N
)
5998 -- In floating-point this is a standard way to check for finite values
5999 -- and using 'Valid would typically be a pessimization.
6001 and then not Is_Floating_Point_Type
(Ltyp
)
6003 -- Don't give the message unless right operand is a type entity and
6004 -- the type of the left operand matches this type. Note that this
6005 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
6006 -- checks have changed the type of the left operand.
6008 and then Nkind
(Rop
) in N_Has_Entity
6009 and then Ltyp
= Entity
(Rop
)
6011 -- Skip this for predicated types, where such expressions are a
6012 -- reasonable way of testing if something meets the predicate.
6014 and then not Present
(Predicate_Function
(Ltyp
))
6016 Substitute_Valid_Check
;
6020 -- Do validity check on operands
6022 if Validity_Checks_On
and Validity_Check_Operands
then
6023 Ensure_Valid
(Left_Opnd
(N
));
6024 Validity_Check_Range
(Right_Opnd
(N
));
6027 -- Case of explicit range
6029 if Nkind
(Rop
) = N_Range
then
6031 Lo
: constant Node_Id
:= Low_Bound
(Rop
);
6032 Hi
: constant Node_Id
:= High_Bound
(Rop
);
6034 Lo_Orig
: constant Node_Id
:= Original_Node
(Lo
);
6035 Hi_Orig
: constant Node_Id
:= Original_Node
(Hi
);
6037 Lcheck
: Compare_Result
;
6038 Ucheck
: Compare_Result
;
6040 Warn1
: constant Boolean :=
6041 Constant_Condition_Warnings
6042 and then Comes_From_Source
(N
)
6043 and then not In_Instance
;
6044 -- This must be true for any of the optimization warnings, we
6045 -- clearly want to give them only for source with the flag on. We
6046 -- also skip these warnings in an instance since it may be the
6047 -- case that different instantiations have different ranges.
6049 Warn2
: constant Boolean :=
6051 and then Nkind
(Original_Node
(Rop
)) = N_Range
6052 and then Is_Integer_Type
(Etype
(Lo
));
6053 -- For the case where only one bound warning is elided, we also
6054 -- insist on an explicit range and an integer type. The reason is
6055 -- that the use of enumeration ranges including an end point is
6056 -- common, as is the use of a subtype name, one of whose bounds is
6057 -- the same as the type of the expression.
6060 -- If test is explicit x'First .. x'Last, replace by valid check
6062 -- Could use some individual comments for this complex test ???
6064 if Is_Scalar_Type
(Ltyp
)
6066 -- And left operand is X'First where X matches left operand
6067 -- type (this eliminates cases of type mismatch, including
6068 -- the cases where ELIMINATED/MINIMIZED mode has changed the
6069 -- type of the left operand.
6071 and then Nkind
(Lo_Orig
) = N_Attribute_Reference
6072 and then Attribute_Name
(Lo_Orig
) = Name_First
6073 and then Nkind
(Prefix
(Lo_Orig
)) in N_Has_Entity
6074 and then Entity
(Prefix
(Lo_Orig
)) = Ltyp
6076 -- Same tests for right operand
6078 and then Nkind
(Hi_Orig
) = N_Attribute_Reference
6079 and then Attribute_Name
(Hi_Orig
) = Name_Last
6080 and then Nkind
(Prefix
(Hi_Orig
)) in N_Has_Entity
6081 and then Entity
(Prefix
(Hi_Orig
)) = Ltyp
6083 -- Relevant only for source cases
6085 and then Comes_From_Source
(N
)
6087 Substitute_Valid_Check
;
6091 -- If bounds of type are known at compile time, and the end points
6092 -- are known at compile time and identical, this is another case
6093 -- for substituting a valid test. We only do this for discrete
6094 -- types, since it won't arise in practice for float types.
6096 if Comes_From_Source
(N
)
6097 and then Is_Discrete_Type
(Ltyp
)
6098 and then Compile_Time_Known_Value
(Type_High_Bound
(Ltyp
))
6099 and then Compile_Time_Known_Value
(Type_Low_Bound
(Ltyp
))
6100 and then Compile_Time_Known_Value
(Lo
)
6101 and then Compile_Time_Known_Value
(Hi
)
6102 and then Expr_Value
(Type_High_Bound
(Ltyp
)) = Expr_Value
(Hi
)
6103 and then Expr_Value
(Type_Low_Bound
(Ltyp
)) = Expr_Value
(Lo
)
6105 -- Kill warnings in instances, since they may be cases where we
6106 -- have a test in the generic that makes sense with some types
6107 -- and not with other types.
6109 -- Similarly, do not rewrite membership as a validity check if
6110 -- within the predicate function for the type.
6114 or else (Ekind
(Current_Scope
) = E_Function
6115 and then Is_Predicate_Function
(Current_Scope
))
6120 Substitute_Valid_Check
;
6125 -- If we have an explicit range, do a bit of optimization based on
6126 -- range analysis (we may be able to kill one or both checks).
6128 Lcheck
:= Compile_Time_Compare
(Lop
, Lo
, Assume_Valid
=> False);
6129 Ucheck
:= Compile_Time_Compare
(Lop
, Hi
, Assume_Valid
=> False);
6131 -- If either check is known to fail, replace result by False since
6132 -- the other check does not matter. Preserve the static flag for
6133 -- legality checks, because we are constant-folding beyond RM 4.9.
6135 if Lcheck
= LT
or else Ucheck
= GT
then
6137 Error_Msg_N
("?c?range test optimized away", N
);
6138 Error_Msg_N
("\?c?value is known to be out of range", N
);
6141 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
6142 Analyze_And_Resolve
(N
, Restyp
);
6143 Set_Is_Static_Expression
(N
, Static
);
6146 -- If both checks are known to succeed, replace result by True,
6147 -- since we know we are in range.
6149 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
6151 Error_Msg_N
("?c?range test optimized away", N
);
6152 Error_Msg_N
("\?c?value is known to be in range", N
);
6155 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
6156 Analyze_And_Resolve
(N
, Restyp
);
6157 Set_Is_Static_Expression
(N
, Static
);
6160 -- If lower bound check succeeds and upper bound check is not
6161 -- known to succeed or fail, then replace the range check with
6162 -- a comparison against the upper bound.
6164 elsif Lcheck
in Compare_GE
then
6165 if Warn2
and then not In_Instance
then
6166 Error_Msg_N
("??lower bound test optimized away", Lo
);
6167 Error_Msg_N
("\??value is known to be in range", Lo
);
6173 Right_Opnd
=> High_Bound
(Rop
)));
6174 Analyze_And_Resolve
(N
, Restyp
);
6177 -- If upper bound check succeeds and lower bound check is not
6178 -- known to succeed or fail, then replace the range check with
6179 -- a comparison against the lower bound.
6181 elsif Ucheck
in Compare_LE
then
6182 if Warn2
and then not In_Instance
then
6183 Error_Msg_N
("??upper bound test optimized away", Hi
);
6184 Error_Msg_N
("\??value is known to be in range", Hi
);
6190 Right_Opnd
=> Low_Bound
(Rop
)));
6191 Analyze_And_Resolve
(N
, Restyp
);
6195 -- We couldn't optimize away the range check, but there is one
6196 -- more issue. If we are checking constant conditionals, then we
6197 -- see if we can determine the outcome assuming everything is
6198 -- valid, and if so give an appropriate warning.
6200 if Warn1
and then not Assume_No_Invalid_Values
then
6201 Lcheck
:= Compile_Time_Compare
(Lop
, Lo
, Assume_Valid
=> True);
6202 Ucheck
:= Compile_Time_Compare
(Lop
, Hi
, Assume_Valid
=> True);
6204 -- Result is out of range for valid value
6206 if Lcheck
= LT
or else Ucheck
= GT
then
6208 ("?c?value can only be in range if it is invalid", N
);
6210 -- Result is in range for valid value
6212 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
6214 ("?c?value can only be out of range if it is invalid", N
);
6216 -- Lower bound check succeeds if value is valid
6218 elsif Warn2
and then Lcheck
in Compare_GE
then
6220 ("?c?lower bound check only fails if it is invalid", Lo
);
6222 -- Upper bound check succeeds if value is valid
6224 elsif Warn2
and then Ucheck
in Compare_LE
then
6226 ("?c?upper bound check only fails for invalid values", Hi
);
6231 -- For all other cases of an explicit range, nothing to be done
6235 -- Here right operand is a subtype mark
6239 Typ
: Entity_Id
:= Etype
(Rop
);
6240 Is_Acc
: constant Boolean := Is_Access_Type
(Typ
);
6241 Cond
: Node_Id
:= Empty
;
6243 Obj
: Node_Id
:= Lop
;
6244 SCIL_Node
: Node_Id
;
6247 Remove_Side_Effects
(Obj
);
6249 -- For tagged type, do tagged membership operation
6251 if Is_Tagged_Type
(Typ
) then
6253 -- No expansion will be performed for VM targets, as the VM
6254 -- back ends will handle the membership tests directly.
6256 if Tagged_Type_Expansion
then
6257 Tagged_Membership
(N
, SCIL_Node
, New_N
);
6259 Analyze_And_Resolve
(N
, Restyp
, Suppress
=> All_Checks
);
6261 -- Update decoration of relocated node referenced by the
6264 if Generate_SCIL
and then Present
(SCIL_Node
) then
6265 Set_SCIL_Node
(N
, SCIL_Node
);
6271 -- If type is scalar type, rewrite as x in t'First .. t'Last.
6272 -- This reason we do this is that the bounds may have the wrong
6273 -- type if they come from the original type definition. Also this
6274 -- way we get all the processing above for an explicit range.
6276 -- Don't do this for predicated types, since in this case we
6277 -- want to check the predicate.
6279 elsif Is_Scalar_Type
(Typ
) then
6280 if No
(Predicate_Function
(Typ
)) then
6284 Make_Attribute_Reference
(Loc
,
6285 Attribute_Name
=> Name_First
,
6286 Prefix
=> New_Occurrence_Of
(Typ
, Loc
)),
6289 Make_Attribute_Reference
(Loc
,
6290 Attribute_Name
=> Name_Last
,
6291 Prefix
=> New_Occurrence_Of
(Typ
, Loc
))));
6292 Analyze_And_Resolve
(N
, Restyp
);
6297 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
6298 -- a membership test if the subtype mark denotes a constrained
6299 -- Unchecked_Union subtype and the expression lacks inferable
6302 elsif Is_Unchecked_Union
(Base_Type
(Typ
))
6303 and then Is_Constrained
(Typ
)
6304 and then not Has_Inferable_Discriminants
(Lop
)
6307 Make_Raise_Program_Error
(Loc
,
6308 Reason
=> PE_Unchecked_Union_Restriction
));
6310 -- Prevent Gigi from generating incorrect code by rewriting the
6311 -- test as False. What is this undocumented thing about ???
6313 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
6317 -- Here we have a non-scalar type
6320 Typ
:= Designated_Type
(Typ
);
6323 if not Is_Constrained
(Typ
) then
6324 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
6325 Analyze_And_Resolve
(N
, Restyp
);
6327 -- For the constrained array case, we have to check the subscripts
6328 -- for an exact match if the lengths are non-zero (the lengths
6329 -- must match in any case).
6331 elsif Is_Array_Type
(Typ
) then
6332 Check_Subscripts
: declare
6333 function Build_Attribute_Reference
6336 Dim
: Nat
) return Node_Id
;
6337 -- Build attribute reference E'Nam (Dim)
6339 -------------------------------
6340 -- Build_Attribute_Reference --
6341 -------------------------------
6343 function Build_Attribute_Reference
6346 Dim
: Nat
) return Node_Id
6350 Make_Attribute_Reference
(Loc
,
6352 Attribute_Name
=> Nam
,
6353 Expressions
=> New_List
(
6354 Make_Integer_Literal
(Loc
, Dim
)));
6355 end Build_Attribute_Reference
;
6357 -- Start of processing for Check_Subscripts
6360 for J
in 1 .. Number_Dimensions
(Typ
) loop
6361 Evolve_And_Then
(Cond
,
6364 Build_Attribute_Reference
6365 (Duplicate_Subexpr_No_Checks
(Obj
),
6368 Build_Attribute_Reference
6369 (New_Occurrence_Of
(Typ
, Loc
), Name_First
, J
)));
6371 Evolve_And_Then
(Cond
,
6374 Build_Attribute_Reference
6375 (Duplicate_Subexpr_No_Checks
(Obj
),
6378 Build_Attribute_Reference
6379 (New_Occurrence_Of
(Typ
, Loc
), Name_Last
, J
)));
6388 Right_Opnd
=> Make_Null
(Loc
)),
6389 Right_Opnd
=> Cond
);
6393 Analyze_And_Resolve
(N
, Restyp
);
6394 end Check_Subscripts
;
6396 -- These are the cases where constraint checks may be required,
6397 -- e.g. records with possible discriminants
6400 -- Expand the test into a series of discriminant comparisons.
6401 -- The expression that is built is the negation of the one that
6402 -- is used for checking discriminant constraints.
6404 Obj
:= Relocate_Node
(Left_Opnd
(N
));
6406 if Has_Discriminants
(Typ
) then
6407 Cond
:= Make_Op_Not
(Loc
,
6408 Right_Opnd
=> Build_Discriminant_Checks
(Obj
, Typ
));
6411 Cond
:= Make_Or_Else
(Loc
,
6415 Right_Opnd
=> Make_Null
(Loc
)),
6416 Right_Opnd
=> Cond
);
6420 Cond
:= New_Occurrence_Of
(Standard_True
, Loc
);
6424 Analyze_And_Resolve
(N
, Restyp
);
6427 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
6428 -- expression of an anonymous access type. This can involve an
6429 -- accessibility test and a tagged type membership test in the
6430 -- case of tagged designated types.
6432 if Ada_Version
>= Ada_2012
6434 and then Ekind
(Ltyp
) = E_Anonymous_Access_Type
6437 Expr_Entity
: Entity_Id
:= Empty
;
6439 Param_Level
: Node_Id
;
6440 Type_Level
: Node_Id
;
6443 if Is_Entity_Name
(Lop
) then
6444 Expr_Entity
:= Param_Entity
(Lop
);
6446 if not Present
(Expr_Entity
) then
6447 Expr_Entity
:= Entity
(Lop
);
6451 -- If a conversion of the anonymous access value to the
6452 -- tested type would be illegal, then the result is False.
6454 if not Valid_Conversion
6455 (Lop
, Rtyp
, Lop
, Report_Errs
=> False)
6457 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
6458 Analyze_And_Resolve
(N
, Restyp
);
6460 -- Apply an accessibility check if the access object has an
6461 -- associated access level and when the level of the type is
6462 -- less deep than the level of the access parameter. This
6463 -- only occur for access parameters and stand-alone objects
6464 -- of an anonymous access type.
6467 if Present
(Expr_Entity
)
6470 (Effective_Extra_Accessibility
(Expr_Entity
))
6471 and then UI_Gt
(Object_Access_Level
(Lop
),
6472 Type_Access_Level
(Rtyp
))
6476 (Effective_Extra_Accessibility
(Expr_Entity
), Loc
);
6479 Make_Integer_Literal
(Loc
, Type_Access_Level
(Rtyp
));
6481 -- Return True only if the accessibility level of the
6482 -- expression entity is not deeper than the level of
6483 -- the tested access type.
6487 Left_Opnd
=> Relocate_Node
(N
),
6488 Right_Opnd
=> Make_Op_Le
(Loc
,
6489 Left_Opnd
=> Param_Level
,
6490 Right_Opnd
=> Type_Level
)));
6492 Analyze_And_Resolve
(N
);
6495 -- If the designated type is tagged, do tagged membership
6498 -- *** NOTE: we have to check not null before doing the
6499 -- tagged membership test (but maybe that can be done
6500 -- inside Tagged_Membership?).
6502 if Is_Tagged_Type
(Typ
) then
6505 Left_Opnd
=> Relocate_Node
(N
),
6509 Right_Opnd
=> Make_Null
(Loc
))));
6511 -- No expansion will be performed for VM targets, as
6512 -- the VM back ends will handle the membership tests
6515 if Tagged_Type_Expansion
then
6517 -- Note that we have to pass Original_Node, because
6518 -- the membership test might already have been
6519 -- rewritten by earlier parts of membership test.
6522 (Original_Node
(N
), SCIL_Node
, New_N
);
6524 -- Update decoration of relocated node referenced
6525 -- by the SCIL node.
6527 if Generate_SCIL
and then Present
(SCIL_Node
) then
6528 Set_SCIL_Node
(New_N
, SCIL_Node
);
6533 Left_Opnd
=> Relocate_Node
(N
),
6534 Right_Opnd
=> New_N
));
6536 Analyze_And_Resolve
(N
, Restyp
);
6545 -- At this point, we have done the processing required for the basic
6546 -- membership test, but not yet dealt with the predicate.
6550 -- If a predicate is present, then we do the predicate test, but we
6551 -- most certainly want to omit this if we are within the predicate
6552 -- function itself, since otherwise we have an infinite recursion.
6553 -- The check should also not be emitted when testing against a range
6554 -- (the check is only done when the right operand is a subtype; see
6555 -- RM12-4.5.2 (28.1/3-30/3)).
6557 Predicate_Check
: declare
6558 function In_Range_Check
return Boolean;
6559 -- Within an expanded range check that may raise Constraint_Error do
6560 -- not generate a predicate check as well. It is redundant because
6561 -- the context will add an explicit predicate check, and it will
6562 -- raise the wrong exception if it fails.
6564 --------------------
6565 -- In_Range_Check --
6566 --------------------
6568 function In_Range_Check
return Boolean is
6572 while Present
(P
) loop
6573 if Nkind
(P
) = N_Raise_Constraint_Error
then
6576 elsif Nkind
(P
) in N_Statement_Other_Than_Procedure_Call
6577 or else Nkind
(P
) = N_Procedure_Call_Statement
6578 or else Nkind
(P
) in N_Declaration
6591 PFunc
: constant Entity_Id
:= Predicate_Function
(Rtyp
);
6594 -- Start of processing for Predicate_Check
6598 and then Current_Scope
/= PFunc
6599 and then Nkind
(Rop
) /= N_Range
6601 if not In_Range_Check
then
6602 R_Op
:= Make_Predicate_Call
(Rtyp
, Lop
, Mem
=> True);
6604 R_Op
:= New_Occurrence_Of
(Standard_True
, Loc
);
6609 Left_Opnd
=> Relocate_Node
(N
),
6610 Right_Opnd
=> R_Op
));
6612 -- Analyze new expression, mark left operand as analyzed to
6613 -- avoid infinite recursion adding predicate calls. Similarly,
6614 -- suppress further range checks on the call.
6616 Set_Analyzed
(Left_Opnd
(N
));
6617 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
6619 -- All done, skip attempt at compile time determination of result
6623 end Predicate_Check
;
6626 --------------------------------
6627 -- Expand_N_Indexed_Component --
6628 --------------------------------
6630 procedure Expand_N_Indexed_Component
(N
: Node_Id
) is
6631 Loc
: constant Source_Ptr
:= Sloc
(N
);
6632 Typ
: constant Entity_Id
:= Etype
(N
);
6633 P
: constant Node_Id
:= Prefix
(N
);
6634 T
: constant Entity_Id
:= Etype
(P
);
6638 -- A special optimization, if we have an indexed component that is
6639 -- selecting from a slice, then we can eliminate the slice, since, for
6640 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6641 -- the range check required by the slice. The range check for the slice
6642 -- itself has already been generated. The range check for the
6643 -- subscripting operation is ensured by converting the subject to
6644 -- the subtype of the slice.
6646 -- This optimization not only generates better code, avoiding slice
6647 -- messing especially in the packed case, but more importantly bypasses
6648 -- some problems in handling this peculiar case, for example, the issue
6649 -- of dealing specially with object renamings.
6651 if Nkind
(P
) = N_Slice
6653 -- This optimization is disabled for CodePeer because it can transform
6654 -- an index-check constraint_error into a range-check constraint_error
6655 -- and CodePeer cares about that distinction.
6657 and then not CodePeer_Mode
6660 Make_Indexed_Component
(Loc
,
6661 Prefix
=> Prefix
(P
),
6662 Expressions
=> New_List
(
6664 (Etype
(First_Index
(Etype
(P
))),
6665 First
(Expressions
(N
))))));
6666 Analyze_And_Resolve
(N
, Typ
);
6670 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6671 -- function, then additional actuals must be passed.
6673 if Is_Build_In_Place_Function_Call
(P
) then
6674 Make_Build_In_Place_Call_In_Anonymous_Context
(P
);
6676 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
6677 -- containing build-in-place function calls whose returned object covers
6680 elsif Present
(Unqual_BIP_Iface_Function_Call
(P
)) then
6681 Make_Build_In_Place_Iface_Call_In_Anonymous_Context
(P
);
6684 -- If the prefix is an access type, then we unconditionally rewrite if
6685 -- as an explicit dereference. This simplifies processing for several
6686 -- cases, including packed array cases and certain cases in which checks
6687 -- must be generated. We used to try to do this only when it was
6688 -- necessary, but it cleans up the code to do it all the time.
6690 if Is_Access_Type
(T
) then
6691 Insert_Explicit_Dereference
(P
);
6692 Analyze_And_Resolve
(P
, Designated_Type
(T
));
6693 Atp
:= Designated_Type
(T
);
6698 -- Generate index and validity checks
6700 Generate_Index_Checks
(N
);
6702 if Validity_Checks_On
and then Validity_Check_Subscripts
then
6703 Apply_Subscript_Validity_Checks
(N
);
6706 -- If selecting from an array with atomic components, and atomic sync
6707 -- is not suppressed for this array type, set atomic sync flag.
6709 if (Has_Atomic_Components
(Atp
)
6710 and then not Atomic_Synchronization_Disabled
(Atp
))
6711 or else (Is_Atomic
(Typ
)
6712 and then not Atomic_Synchronization_Disabled
(Typ
))
6713 or else (Is_Entity_Name
(P
)
6714 and then Has_Atomic_Components
(Entity
(P
))
6715 and then not Atomic_Synchronization_Disabled
(Entity
(P
)))
6717 Activate_Atomic_Synchronization
(N
);
6720 -- All done if the prefix is not a packed array implemented specially
6722 if not (Is_Packed
(Etype
(Prefix
(N
)))
6723 and then Present
(Packed_Array_Impl_Type
(Etype
(Prefix
(N
)))))
6728 -- For packed arrays that are not bit-packed (i.e. the case of an array
6729 -- with one or more index types with a non-contiguous enumeration type),
6730 -- we can always use the normal packed element get circuit.
6732 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
6733 Expand_Packed_Element_Reference
(N
);
6737 -- For a reference to a component of a bit packed array, we convert it
6738 -- to a reference to the corresponding Packed_Array_Impl_Type. We only
6739 -- want to do this for simple references, and not for:
6741 -- Left side of assignment, or prefix of left side of assignment, or
6742 -- prefix of the prefix, to handle packed arrays of packed arrays,
6743 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6745 -- Renaming objects in renaming associations
6746 -- This case is handled when a use of the renamed variable occurs
6748 -- Actual parameters for a procedure call
6749 -- This case is handled in Exp_Ch6.Expand_Actuals
6751 -- The second expression in a 'Read attribute reference
6753 -- The prefix of an address or bit or size attribute reference
6755 -- The following circuit detects these exceptions. Note that we need to
6756 -- deal with implicit dereferences when climbing up the parent chain,
6757 -- with the additional difficulty that the type of parents may have yet
6758 -- to be resolved since prefixes are usually resolved first.
6761 Child
: Node_Id
:= N
;
6762 Parnt
: Node_Id
:= Parent
(N
);
6766 if Nkind
(Parnt
) = N_Unchecked_Expression
then
6769 elsif Nkind_In
(Parnt
, N_Object_Renaming_Declaration
,
6770 N_Procedure_Call_Statement
)
6771 or else (Nkind
(Parnt
) = N_Parameter_Association
6773 Nkind
(Parent
(Parnt
)) = N_Procedure_Call_Statement
)
6777 elsif Nkind
(Parnt
) = N_Attribute_Reference
6778 and then Nam_In
(Attribute_Name
(Parnt
), Name_Address
,
6781 and then Prefix
(Parnt
) = Child
6785 elsif Nkind
(Parnt
) = N_Assignment_Statement
6786 and then Name
(Parnt
) = Child
6790 -- If the expression is an index of an indexed component, it must
6791 -- be expanded regardless of context.
6793 elsif Nkind
(Parnt
) = N_Indexed_Component
6794 and then Child
/= Prefix
(Parnt
)
6796 Expand_Packed_Element_Reference
(N
);
6799 elsif Nkind
(Parent
(Parnt
)) = N_Assignment_Statement
6800 and then Name
(Parent
(Parnt
)) = Parnt
6804 elsif Nkind
(Parnt
) = N_Attribute_Reference
6805 and then Attribute_Name
(Parnt
) = Name_Read
6806 and then Next
(First
(Expressions
(Parnt
))) = Child
6810 elsif Nkind
(Parnt
) = N_Indexed_Component
6811 and then Prefix
(Parnt
) = Child
6815 elsif Nkind
(Parnt
) = N_Selected_Component
6816 and then Prefix
(Parnt
) = Child
6817 and then not (Present
(Etype
(Selector_Name
(Parnt
)))
6819 Is_Access_Type
(Etype
(Selector_Name
(Parnt
))))
6823 -- If the parent is a dereference, either implicit or explicit,
6824 -- then the packed reference needs to be expanded.
6827 Expand_Packed_Element_Reference
(N
);
6831 -- Keep looking up tree for unchecked expression, or if we are the
6832 -- prefix of a possible assignment left side.
6835 Parnt
:= Parent
(Child
);
6838 end Expand_N_Indexed_Component
;
6840 ---------------------
6841 -- Expand_N_Not_In --
6842 ---------------------
6844 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6845 -- can be done. This avoids needing to duplicate this expansion code.
6847 procedure Expand_N_Not_In
(N
: Node_Id
) is
6848 Loc
: constant Source_Ptr
:= Sloc
(N
);
6849 Typ
: constant Entity_Id
:= Etype
(N
);
6850 Cfs
: constant Boolean := Comes_From_Source
(N
);
6857 Left_Opnd
=> Left_Opnd
(N
),
6858 Right_Opnd
=> Right_Opnd
(N
))));
6860 -- If this is a set membership, preserve list of alternatives
6862 Set_Alternatives
(Right_Opnd
(N
), Alternatives
(Original_Node
(N
)));
6864 -- We want this to appear as coming from source if original does (see
6865 -- transformations in Expand_N_In).
6867 Set_Comes_From_Source
(N
, Cfs
);
6868 Set_Comes_From_Source
(Right_Opnd
(N
), Cfs
);
6870 -- Now analyze transformed node
6872 Analyze_And_Resolve
(N
, Typ
);
6873 end Expand_N_Not_In
;
6879 -- The only replacement required is for the case of a null of a type that
6880 -- is an access to protected subprogram, or a subtype thereof. We represent
6881 -- such access values as a record, and so we must replace the occurrence of
6882 -- null by the equivalent record (with a null address and a null pointer in
6883 -- it), so that the back end creates the proper value.
6885 procedure Expand_N_Null
(N
: Node_Id
) is
6886 Loc
: constant Source_Ptr
:= Sloc
(N
);
6887 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
6891 if Is_Access_Protected_Subprogram_Type
(Typ
) then
6893 Make_Aggregate
(Loc
,
6894 Expressions
=> New_List
(
6895 New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
),
6899 Analyze_And_Resolve
(N
, Equivalent_Type
(Typ
));
6901 -- For subsequent semantic analysis, the node must retain its type.
6902 -- Gigi in any case replaces this type by the corresponding record
6903 -- type before processing the node.
6909 when RE_Not_Available
=>
6913 ---------------------
6914 -- Expand_N_Op_Abs --
6915 ---------------------
6917 procedure Expand_N_Op_Abs
(N
: Node_Id
) is
6918 Loc
: constant Source_Ptr
:= Sloc
(N
);
6919 Expr
: constant Node_Id
:= Right_Opnd
(N
);
6922 Unary_Op_Validity_Checks
(N
);
6924 -- Check for MINIMIZED/ELIMINATED overflow mode
6926 if Minimized_Eliminated_Overflow_Check
(N
) then
6927 Apply_Arithmetic_Overflow_Check
(N
);
6931 -- Deal with software overflow checking
6933 if Is_Signed_Integer_Type
(Etype
(N
))
6934 and then Do_Overflow_Check
(N
)
6936 -- The only case to worry about is when the argument is equal to the
6937 -- largest negative number, so what we do is to insert the check:
6939 -- [constraint_error when Expr = typ'Base'First]
6941 -- with the usual Duplicate_Subexpr use coding for expr
6944 Make_Raise_Constraint_Error
(Loc
,
6947 Left_Opnd
=> Duplicate_Subexpr
(Expr
),
6949 Make_Attribute_Reference
(Loc
,
6951 New_Occurrence_Of
(Base_Type
(Etype
(Expr
)), Loc
),
6952 Attribute_Name
=> Name_First
)),
6953 Reason
=> CE_Overflow_Check_Failed
));
6955 Set_Do_Overflow_Check
(N
, False);
6957 end Expand_N_Op_Abs
;
6959 ---------------------
6960 -- Expand_N_Op_Add --
6961 ---------------------
6963 procedure Expand_N_Op_Add
(N
: Node_Id
) is
6964 Typ
: constant Entity_Id
:= Etype
(N
);
6967 Binary_Op_Validity_Checks
(N
);
6969 -- Check for MINIMIZED/ELIMINATED overflow mode
6971 if Minimized_Eliminated_Overflow_Check
(N
) then
6972 Apply_Arithmetic_Overflow_Check
(N
);
6976 -- N + 0 = 0 + N = N for integer types
6978 if Is_Integer_Type
(Typ
) then
6979 if Compile_Time_Known_Value
(Right_Opnd
(N
))
6980 and then Expr_Value
(Right_Opnd
(N
)) = Uint_0
6982 Rewrite
(N
, Left_Opnd
(N
));
6985 elsif Compile_Time_Known_Value
(Left_Opnd
(N
))
6986 and then Expr_Value
(Left_Opnd
(N
)) = Uint_0
6988 Rewrite
(N
, Right_Opnd
(N
));
6993 -- Arithmetic overflow checks for signed integer/fixed point types
6995 if Is_Signed_Integer_Type
(Typ
) or else Is_Fixed_Point_Type
(Typ
) then
6996 Apply_Arithmetic_Overflow_Check
(N
);
7000 -- Overflow checks for floating-point if -gnateF mode active
7002 Check_Float_Op_Overflow
(N
);
7004 Expand_Nonbinary_Modular_Op
(N
);
7005 end Expand_N_Op_Add
;
7007 ---------------------
7008 -- Expand_N_Op_And --
7009 ---------------------
7011 procedure Expand_N_Op_And
(N
: Node_Id
) is
7012 Typ
: constant Entity_Id
:= Etype
(N
);
7015 Binary_Op_Validity_Checks
(N
);
7017 if Is_Array_Type
(Etype
(N
)) then
7018 Expand_Boolean_Operator
(N
);
7020 elsif Is_Boolean_Type
(Etype
(N
)) then
7021 Adjust_Condition
(Left_Opnd
(N
));
7022 Adjust_Condition
(Right_Opnd
(N
));
7023 Set_Etype
(N
, Standard_Boolean
);
7024 Adjust_Result_Type
(N
, Typ
);
7026 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
7027 Expand_Intrinsic_Call
(N
, Entity
(N
));
7030 Expand_Nonbinary_Modular_Op
(N
);
7031 end Expand_N_Op_And
;
7033 ------------------------
7034 -- Expand_N_Op_Concat --
7035 ------------------------
7037 procedure Expand_N_Op_Concat
(N
: Node_Id
) is
7039 -- List of operands to be concatenated
7042 -- Node which is to be replaced by the result of concatenating the nodes
7043 -- in the list Opnds.
7046 -- Ensure validity of both operands
7048 Binary_Op_Validity_Checks
(N
);
7050 -- If we are the left operand of a concatenation higher up the tree,
7051 -- then do nothing for now, since we want to deal with a series of
7052 -- concatenations as a unit.
7054 if Nkind
(Parent
(N
)) = N_Op_Concat
7055 and then N
= Left_Opnd
(Parent
(N
))
7060 -- We get here with a concatenation whose left operand may be a
7061 -- concatenation itself with a consistent type. We need to process
7062 -- these concatenation operands from left to right, which means
7063 -- from the deepest node in the tree to the highest node.
7066 while Nkind
(Left_Opnd
(Cnode
)) = N_Op_Concat
loop
7067 Cnode
:= Left_Opnd
(Cnode
);
7070 -- Now Cnode is the deepest concatenation, and its parents are the
7071 -- concatenation nodes above, so now we process bottom up, doing the
7074 -- The outer loop runs more than once if more than one concatenation
7075 -- type is involved.
7078 Opnds
:= New_List
(Left_Opnd
(Cnode
), Right_Opnd
(Cnode
));
7079 Set_Parent
(Opnds
, N
);
7081 -- The inner loop gathers concatenation operands
7083 Inner
: while Cnode
/= N
7084 and then Base_Type
(Etype
(Cnode
)) =
7085 Base_Type
(Etype
(Parent
(Cnode
)))
7087 Cnode
:= Parent
(Cnode
);
7088 Append
(Right_Opnd
(Cnode
), Opnds
);
7091 -- Note: The following code is a temporary workaround for N731-034
7092 -- and N829-028 and will be kept until the general issue of internal
7093 -- symbol serialization is addressed. The workaround is kept under a
7094 -- debug switch to avoid permiating into the general case.
7096 -- Wrap the node to concatenate into an expression actions node to
7097 -- keep it nicely packaged. This is useful in the case of an assert
7098 -- pragma with a concatenation where we want to be able to delete
7099 -- the concatenation and all its expansion stuff.
7101 if Debug_Flag_Dot_H
then
7103 Cnod
: constant Node_Id
:= New_Copy_Tree
(Cnode
);
7104 Typ
: constant Entity_Id
:= Base_Type
(Etype
(Cnode
));
7107 -- Note: use Rewrite rather than Replace here, so that for
7108 -- example Why_Not_Static can find the original concatenation
7112 Make_Expression_With_Actions
(Sloc
(Cnode
),
7113 Actions
=> New_List
(Make_Null_Statement
(Sloc
(Cnode
))),
7114 Expression
=> Cnod
));
7116 Expand_Concatenate
(Cnod
, Opnds
);
7117 Analyze_And_Resolve
(Cnode
, Typ
);
7123 Expand_Concatenate
(Cnode
, Opnds
);
7126 exit Outer
when Cnode
= N
;
7127 Cnode
:= Parent
(Cnode
);
7129 end Expand_N_Op_Concat
;
7131 ------------------------
7132 -- Expand_N_Op_Divide --
7133 ------------------------
7135 procedure Expand_N_Op_Divide
(N
: Node_Id
) is
7136 Loc
: constant Source_Ptr
:= Sloc
(N
);
7137 Lopnd
: constant Node_Id
:= Left_Opnd
(N
);
7138 Ropnd
: constant Node_Id
:= Right_Opnd
(N
);
7139 Ltyp
: constant Entity_Id
:= Etype
(Lopnd
);
7140 Rtyp
: constant Entity_Id
:= Etype
(Ropnd
);
7141 Typ
: Entity_Id
:= Etype
(N
);
7142 Rknow
: constant Boolean := Is_Integer_Type
(Typ
)
7144 Compile_Time_Known_Value
(Ropnd
);
7148 Binary_Op_Validity_Checks
(N
);
7150 -- Check for MINIMIZED/ELIMINATED overflow mode
7152 if Minimized_Eliminated_Overflow_Check
(N
) then
7153 Apply_Arithmetic_Overflow_Check
(N
);
7157 -- Otherwise proceed with expansion of division
7160 Rval
:= Expr_Value
(Ropnd
);
7163 -- N / 1 = N for integer types
7165 if Rknow
and then Rval
= Uint_1
then
7170 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
7171 -- Is_Power_Of_2_For_Shift is set means that we know that our left
7172 -- operand is an unsigned integer, as required for this to work.
7174 if Nkind
(Ropnd
) = N_Op_Expon
7175 and then Is_Power_Of_2_For_Shift
(Ropnd
)
7177 -- We cannot do this transformation in configurable run time mode if we
7178 -- have 64-bit integers and long shifts are not available.
7180 and then (Esize
(Ltyp
) <= 32 or else Support_Long_Shifts_On_Target
)
7183 Make_Op_Shift_Right
(Loc
,
7186 Convert_To
(Standard_Natural
, Right_Opnd
(Ropnd
))));
7187 Analyze_And_Resolve
(N
, Typ
);
7191 -- Do required fixup of universal fixed operation
7193 if Typ
= Universal_Fixed
then
7194 Fixup_Universal_Fixed_Operation
(N
);
7198 -- Divisions with fixed-point results
7200 if Is_Fixed_Point_Type
(Typ
) then
7202 -- No special processing if Treat_Fixed_As_Integer is set, since
7203 -- from a semantic point of view such operations are simply integer
7204 -- operations and will be treated that way.
7206 if not Treat_Fixed_As_Integer
(N
) then
7207 if Is_Integer_Type
(Rtyp
) then
7208 Expand_Divide_Fixed_By_Integer_Giving_Fixed
(N
);
7210 Expand_Divide_Fixed_By_Fixed_Giving_Fixed
(N
);
7214 -- Deal with divide-by-zero check if back end cannot handle them
7215 -- and the flag is set indicating that we need such a check. Note
7216 -- that we don't need to bother here with the case of mixed-mode
7217 -- (Right operand an integer type), since these will be rewritten
7218 -- with conversions to a divide with a fixed-point right operand.
7220 if Nkind
(N
) = N_Op_Divide
7221 and then Do_Division_Check
(N
)
7222 and then not Backend_Divide_Checks_On_Target
7223 and then not Is_Integer_Type
(Rtyp
)
7225 Set_Do_Division_Check
(N
, False);
7227 Make_Raise_Constraint_Error
(Loc
,
7230 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ropnd
),
7231 Right_Opnd
=> Make_Real_Literal
(Loc
, Ureal_0
)),
7232 Reason
=> CE_Divide_By_Zero
));
7235 -- Other cases of division of fixed-point operands. Again we exclude the
7236 -- case where Treat_Fixed_As_Integer is set.
7238 elsif (Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
))
7239 and then not Treat_Fixed_As_Integer
(N
)
7241 if Is_Integer_Type
(Typ
) then
7242 Expand_Divide_Fixed_By_Fixed_Giving_Integer
(N
);
7244 pragma Assert
(Is_Floating_Point_Type
(Typ
));
7245 Expand_Divide_Fixed_By_Fixed_Giving_Float
(N
);
7248 -- Mixed-mode operations can appear in a non-static universal context,
7249 -- in which case the integer argument must be converted explicitly.
7251 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Rtyp
) then
7253 Convert_To
(Universal_Real
, Relocate_Node
(Ropnd
)));
7255 Analyze_And_Resolve
(Ropnd
, Universal_Real
);
7257 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Ltyp
) then
7259 Convert_To
(Universal_Real
, Relocate_Node
(Lopnd
)));
7261 Analyze_And_Resolve
(Lopnd
, Universal_Real
);
7263 -- Non-fixed point cases, do integer zero divide and overflow checks
7265 elsif Is_Integer_Type
(Typ
) then
7266 Apply_Divide_Checks
(N
);
7269 -- Overflow checks for floating-point if -gnateF mode active
7271 Check_Float_Op_Overflow
(N
);
7273 Expand_Nonbinary_Modular_Op
(N
);
7274 end Expand_N_Op_Divide
;
7276 --------------------
7277 -- Expand_N_Op_Eq --
7278 --------------------
7280 procedure Expand_N_Op_Eq
(N
: Node_Id
) is
7281 Loc
: constant Source_Ptr
:= Sloc
(N
);
7282 Typ
: constant Entity_Id
:= Etype
(N
);
7283 Lhs
: constant Node_Id
:= Left_Opnd
(N
);
7284 Rhs
: constant Node_Id
:= Right_Opnd
(N
);
7285 Bodies
: constant List_Id
:= New_List
;
7286 A_Typ
: constant Entity_Id
:= Etype
(Lhs
);
7288 Typl
: Entity_Id
:= A_Typ
;
7289 Op_Name
: Entity_Id
;
7292 procedure Build_Equality_Call
(Eq
: Entity_Id
);
7293 -- If a constructed equality exists for the type or for its parent,
7294 -- build and analyze call, adding conversions if the operation is
7297 function Has_Unconstrained_UU_Component
(Typ
: Node_Id
) return Boolean;
7298 -- Determines whether a type has a subcomponent of an unconstrained
7299 -- Unchecked_Union subtype. Typ is a record type.
7301 -------------------------
7302 -- Build_Equality_Call --
7303 -------------------------
7305 procedure Build_Equality_Call
(Eq
: Entity_Id
) is
7306 Op_Type
: constant Entity_Id
:= Etype
(First_Formal
(Eq
));
7307 L_Exp
: Node_Id
:= Relocate_Node
(Lhs
);
7308 R_Exp
: Node_Id
:= Relocate_Node
(Rhs
);
7311 -- Adjust operands if necessary to comparison type
7313 if Base_Type
(Op_Type
) /= Base_Type
(A_Typ
)
7314 and then not Is_Class_Wide_Type
(A_Typ
)
7316 L_Exp
:= OK_Convert_To
(Op_Type
, L_Exp
);
7317 R_Exp
:= OK_Convert_To
(Op_Type
, R_Exp
);
7320 -- If we have an Unchecked_Union, we need to add the inferred
7321 -- discriminant values as actuals in the function call. At this
7322 -- point, the expansion has determined that both operands have
7323 -- inferable discriminants.
7325 if Is_Unchecked_Union
(Op_Type
) then
7327 Lhs_Type
: constant Node_Id
:= Etype
(L_Exp
);
7328 Rhs_Type
: constant Node_Id
:= Etype
(R_Exp
);
7330 Lhs_Discr_Vals
: Elist_Id
;
7331 -- List of inferred discriminant values for left operand.
7333 Rhs_Discr_Vals
: Elist_Id
;
7334 -- List of inferred discriminant values for right operand.
7339 Lhs_Discr_Vals
:= New_Elmt_List
;
7340 Rhs_Discr_Vals
:= New_Elmt_List
;
7342 -- Per-object constrained selected components require special
7343 -- attention. If the enclosing scope of the component is an
7344 -- Unchecked_Union, we cannot reference its discriminants
7345 -- directly. This is why we use the extra parameters of the
7346 -- equality function of the enclosing Unchecked_Union.
7348 -- type UU_Type (Discr : Integer := 0) is
7351 -- pragma Unchecked_Union (UU_Type);
7353 -- 1. Unchecked_Union enclosing record:
7355 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
7357 -- Comp : UU_Type (Discr);
7359 -- end Enclosing_UU_Type;
7360 -- pragma Unchecked_Union (Enclosing_UU_Type);
7362 -- Obj1 : Enclosing_UU_Type;
7363 -- Obj2 : Enclosing_UU_Type (1);
7365 -- [. . .] Obj1 = Obj2 [. . .]
7369 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
7371 -- A and B are the formal parameters of the equality function
7372 -- of Enclosing_UU_Type. The function always has two extra
7373 -- formals to capture the inferred discriminant values for
7374 -- each discriminant of the type.
7376 -- 2. Non-Unchecked_Union enclosing record:
7379 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
7382 -- Comp : UU_Type (Discr);
7384 -- end Enclosing_Non_UU_Type;
7386 -- Obj1 : Enclosing_Non_UU_Type;
7387 -- Obj2 : Enclosing_Non_UU_Type (1);
7389 -- ... Obj1 = Obj2 ...
7393 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
7394 -- obj1.discr, obj2.discr)) then
7396 -- In this case we can directly reference the discriminants of
7397 -- the enclosing record.
7399 -- Process left operand of equality
7401 if Nkind
(Lhs
) = N_Selected_Component
7403 Has_Per_Object_Constraint
(Entity
(Selector_Name
(Lhs
)))
7405 -- If enclosing record is an Unchecked_Union, use formals
7406 -- corresponding to each discriminant. The name of the
7407 -- formal is that of the discriminant, with added suffix,
7408 -- see Exp_Ch3.Build_Record_Equality for details.
7410 if Is_Unchecked_Union
(Scope
(Entity
(Selector_Name
(Lhs
))))
7414 (Scope
(Entity
(Selector_Name
(Lhs
))));
7415 while Present
(Discr
) loop
7417 (Make_Identifier
(Loc
,
7418 Chars
=> New_External_Name
(Chars
(Discr
), 'A')),
7419 To
=> Lhs_Discr_Vals
);
7420 Next_Discriminant
(Discr
);
7423 -- If enclosing record is of a non-Unchecked_Union type, it
7424 -- is possible to reference its discriminants directly.
7427 Discr
:= First_Discriminant
(Lhs_Type
);
7428 while Present
(Discr
) loop
7430 (Make_Selected_Component
(Loc
,
7431 Prefix
=> Prefix
(Lhs
),
7434 (Get_Discriminant_Value
(Discr
,
7436 Stored_Constraint
(Lhs_Type
)))),
7437 To
=> Lhs_Discr_Vals
);
7438 Next_Discriminant
(Discr
);
7442 -- Otherwise operand is on object with a constrained type.
7443 -- Infer the discriminant values from the constraint.
7447 Discr
:= First_Discriminant
(Lhs_Type
);
7448 while Present
(Discr
) loop
7451 (Get_Discriminant_Value
(Discr
,
7453 Stored_Constraint
(Lhs_Type
))),
7454 To
=> Lhs_Discr_Vals
);
7455 Next_Discriminant
(Discr
);
7459 -- Similar processing for right operand of equality
7461 if Nkind
(Rhs
) = N_Selected_Component
7463 Has_Per_Object_Constraint
(Entity
(Selector_Name
(Rhs
)))
7465 if Is_Unchecked_Union
7466 (Scope
(Entity
(Selector_Name
(Rhs
))))
7470 (Scope
(Entity
(Selector_Name
(Rhs
))));
7471 while Present
(Discr
) loop
7473 (Make_Identifier
(Loc
,
7474 Chars
=> New_External_Name
(Chars
(Discr
), 'B')),
7475 To
=> Rhs_Discr_Vals
);
7476 Next_Discriminant
(Discr
);
7480 Discr
:= First_Discriminant
(Rhs_Type
);
7481 while Present
(Discr
) loop
7483 (Make_Selected_Component
(Loc
,
7484 Prefix
=> Prefix
(Rhs
),
7486 New_Copy
(Get_Discriminant_Value
7489 Stored_Constraint
(Rhs_Type
)))),
7490 To
=> Rhs_Discr_Vals
);
7491 Next_Discriminant
(Discr
);
7496 Discr
:= First_Discriminant
(Rhs_Type
);
7497 while Present
(Discr
) loop
7499 (New_Copy
(Get_Discriminant_Value
7502 Stored_Constraint
(Rhs_Type
))),
7503 To
=> Rhs_Discr_Vals
);
7504 Next_Discriminant
(Discr
);
7508 -- Now merge the list of discriminant values so that values
7509 -- of corresponding discriminants are adjacent.
7517 Params
:= New_List
(L_Exp
, R_Exp
);
7518 L_Elmt
:= First_Elmt
(Lhs_Discr_Vals
);
7519 R_Elmt
:= First_Elmt
(Rhs_Discr_Vals
);
7520 while Present
(L_Elmt
) loop
7521 Append_To
(Params
, Node
(L_Elmt
));
7522 Append_To
(Params
, Node
(R_Elmt
));
7528 Make_Function_Call
(Loc
,
7529 Name
=> New_Occurrence_Of
(Eq
, Loc
),
7530 Parameter_Associations
=> Params
));
7534 -- Normal case, not an unchecked union
7538 Make_Function_Call
(Loc
,
7539 Name
=> New_Occurrence_Of
(Eq
, Loc
),
7540 Parameter_Associations
=> New_List
(L_Exp
, R_Exp
)));
7543 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7544 end Build_Equality_Call
;
7546 ------------------------------------
7547 -- Has_Unconstrained_UU_Component --
7548 ------------------------------------
7550 function Has_Unconstrained_UU_Component
7551 (Typ
: Node_Id
) return Boolean
7553 Tdef
: constant Node_Id
:=
7554 Type_Definition
(Declaration_Node
(Base_Type
(Typ
)));
7558 function Component_Is_Unconstrained_UU
7559 (Comp
: Node_Id
) return Boolean;
7560 -- Determines whether the subtype of the component is an
7561 -- unconstrained Unchecked_Union.
7563 function Variant_Is_Unconstrained_UU
7564 (Variant
: Node_Id
) return Boolean;
7565 -- Determines whether a component of the variant has an unconstrained
7566 -- Unchecked_Union subtype.
7568 -----------------------------------
7569 -- Component_Is_Unconstrained_UU --
7570 -----------------------------------
7572 function Component_Is_Unconstrained_UU
7573 (Comp
: Node_Id
) return Boolean
7576 if Nkind
(Comp
) /= N_Component_Declaration
then
7581 Sindic
: constant Node_Id
:=
7582 Subtype_Indication
(Component_Definition
(Comp
));
7585 -- Unconstrained nominal type. In the case of a constraint
7586 -- present, the node kind would have been N_Subtype_Indication.
7588 if Nkind
(Sindic
) = N_Identifier
then
7589 return Is_Unchecked_Union
(Base_Type
(Etype
(Sindic
)));
7594 end Component_Is_Unconstrained_UU
;
7596 ---------------------------------
7597 -- Variant_Is_Unconstrained_UU --
7598 ---------------------------------
7600 function Variant_Is_Unconstrained_UU
7601 (Variant
: Node_Id
) return Boolean
7603 Clist
: constant Node_Id
:= Component_List
(Variant
);
7606 if Is_Empty_List
(Component_Items
(Clist
)) then
7610 -- We only need to test one component
7613 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
7616 while Present
(Comp
) loop
7617 if Component_Is_Unconstrained_UU
(Comp
) then
7625 -- None of the components withing the variant were of
7626 -- unconstrained Unchecked_Union type.
7629 end Variant_Is_Unconstrained_UU
;
7631 -- Start of processing for Has_Unconstrained_UU_Component
7634 if Null_Present
(Tdef
) then
7638 Clist
:= Component_List
(Tdef
);
7639 Vpart
:= Variant_Part
(Clist
);
7641 -- Inspect available components
7643 if Present
(Component_Items
(Clist
)) then
7645 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
7648 while Present
(Comp
) loop
7650 -- One component is sufficient
7652 if Component_Is_Unconstrained_UU
(Comp
) then
7661 -- Inspect available components withing variants
7663 if Present
(Vpart
) then
7665 Variant
: Node_Id
:= First
(Variants
(Vpart
));
7668 while Present
(Variant
) loop
7670 -- One component within a variant is sufficient
7672 if Variant_Is_Unconstrained_UU
(Variant
) then
7681 -- Neither the available components, nor the components inside the
7682 -- variant parts were of an unconstrained Unchecked_Union subtype.
7685 end Has_Unconstrained_UU_Component
;
7687 -- Start of processing for Expand_N_Op_Eq
7690 Binary_Op_Validity_Checks
(N
);
7692 -- Deal with private types
7694 if Ekind
(Typl
) = E_Private_Type
then
7695 Typl
:= Underlying_Type
(Typl
);
7696 elsif Ekind
(Typl
) = E_Private_Subtype
then
7697 Typl
:= Underlying_Type
(Base_Type
(Typl
));
7702 -- It may happen in error situations that the underlying type is not
7703 -- set. The error will be detected later, here we just defend the
7710 -- Now get the implementation base type (note that plain Base_Type here
7711 -- might lead us back to the private type, which is not what we want!)
7713 Typl
:= Implementation_Base_Type
(Typl
);
7715 -- Equality between variant records results in a call to a routine
7716 -- that has conditional tests of the discriminant value(s), and hence
7717 -- violates the No_Implicit_Conditionals restriction.
7719 if Has_Variant_Part
(Typl
) then
7724 Check_Restriction
(Msg
, No_Implicit_Conditionals
, N
);
7728 ("\comparison of variant records tests discriminants", N
);
7734 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7735 -- means we no longer have a comparison operation, we are all done.
7737 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
7739 if Nkind
(N
) /= N_Op_Eq
then
7743 -- Boolean types (requiring handling of non-standard case)
7745 if Is_Boolean_Type
(Typl
) then
7746 Adjust_Condition
(Left_Opnd
(N
));
7747 Adjust_Condition
(Right_Opnd
(N
));
7748 Set_Etype
(N
, Standard_Boolean
);
7749 Adjust_Result_Type
(N
, Typ
);
7753 elsif Is_Array_Type
(Typl
) then
7755 -- If we are doing full validity checking, and it is possible for the
7756 -- array elements to be invalid then expand out array comparisons to
7757 -- make sure that we check the array elements.
7759 if Validity_Check_Operands
7760 and then not Is_Known_Valid
(Component_Type
(Typl
))
7763 Save_Force_Validity_Checks
: constant Boolean :=
7764 Force_Validity_Checks
;
7766 Force_Validity_Checks
:= True;
7768 Expand_Array_Equality
7770 Relocate_Node
(Lhs
),
7771 Relocate_Node
(Rhs
),
7774 Insert_Actions
(N
, Bodies
);
7775 Analyze_And_Resolve
(N
, Standard_Boolean
);
7776 Force_Validity_Checks
:= Save_Force_Validity_Checks
;
7779 -- Packed case where both operands are known aligned
7781 elsif Is_Bit_Packed_Array
(Typl
)
7782 and then not Is_Possibly_Unaligned_Object
(Lhs
)
7783 and then not Is_Possibly_Unaligned_Object
(Rhs
)
7785 Expand_Packed_Eq
(N
);
7787 -- Where the component type is elementary we can use a block bit
7788 -- comparison (if supported on the target) exception in the case
7789 -- of floating-point (negative zero issues require element by
7790 -- element comparison), and atomic/VFA types (where we must be sure
7791 -- to load elements independently) and possibly unaligned arrays.
7793 elsif Is_Elementary_Type
(Component_Type
(Typl
))
7794 and then not Is_Floating_Point_Type
(Component_Type
(Typl
))
7795 and then not Is_Atomic_Or_VFA
(Component_Type
(Typl
))
7796 and then not Is_Possibly_Unaligned_Object
(Lhs
)
7797 and then not Is_Possibly_Unaligned_Object
(Rhs
)
7798 and then Support_Composite_Compare_On_Target
7802 -- For composite and floating-point cases, expand equality loop to
7803 -- make sure of using proper comparisons for tagged types, and
7804 -- correctly handling the floating-point case.
7808 Expand_Array_Equality
7810 Relocate_Node
(Lhs
),
7811 Relocate_Node
(Rhs
),
7814 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
7815 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7820 elsif Is_Record_Type
(Typl
) then
7822 -- For tagged types, use the primitive "="
7824 if Is_Tagged_Type
(Typl
) then
7826 -- No need to do anything else compiling under restriction
7827 -- No_Dispatching_Calls. During the semantic analysis we
7828 -- already notified such violation.
7830 if Restriction_Active
(No_Dispatching_Calls
) then
7834 -- If this is an untagged private type completed with a derivation
7835 -- of an untagged private type whose full view is a tagged type,
7836 -- we use the primitive operations of the private type (since it
7837 -- does not have a full view, and also because its equality
7838 -- primitive may have been overridden in its untagged full view).
7840 if Inherits_From_Tagged_Full_View
(A_Typ
) then
7842 -- Search for equality operation, checking that the operands
7843 -- have the same type. Note that we must find a matching entry,
7844 -- or something is very wrong.
7846 Prim
:= First_Elmt
(Collect_Primitive_Operations
(A_Typ
));
7848 while Present
(Prim
) loop
7849 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7850 and then Etype
(First_Formal
(Node
(Prim
))) =
7851 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7853 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7858 pragma Assert
(Present
(Prim
));
7859 Op_Name
:= Node
(Prim
);
7861 -- Find the type's predefined equality or an overriding
7862 -- user-defined equality. The reason for not simply calling
7863 -- Find_Prim_Op here is that there may be a user-defined
7864 -- overloaded equality op that precedes the equality that we
7865 -- want, so we have to explicitly search (e.g., there could be
7866 -- an equality with two different parameter types).
7869 if Is_Class_Wide_Type
(Typl
) then
7870 Typl
:= Find_Specific_Type
(Typl
);
7873 Prim
:= First_Elmt
(Primitive_Operations
(Typl
));
7874 while Present
(Prim
) loop
7875 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7876 and then Etype
(First_Formal
(Node
(Prim
))) =
7877 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7879 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7884 pragma Assert
(Present
(Prim
));
7885 Op_Name
:= Node
(Prim
);
7888 Build_Equality_Call
(Op_Name
);
7890 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7891 -- predefined equality operator for a type which has a subcomponent
7892 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7894 elsif Has_Unconstrained_UU_Component
(Typl
) then
7896 Make_Raise_Program_Error
(Loc
,
7897 Reason
=> PE_Unchecked_Union_Restriction
));
7899 -- Prevent Gigi from generating incorrect code by rewriting the
7900 -- equality as a standard False. (is this documented somewhere???)
7903 New_Occurrence_Of
(Standard_False
, Loc
));
7905 elsif Is_Unchecked_Union
(Typl
) then
7907 -- If we can infer the discriminants of the operands, we make a
7908 -- call to the TSS equality function.
7910 if Has_Inferable_Discriminants
(Lhs
)
7912 Has_Inferable_Discriminants
(Rhs
)
7915 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
7918 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7919 -- the predefined equality operator for an Unchecked_Union type
7920 -- if either of the operands lack inferable discriminants.
7923 Make_Raise_Program_Error
(Loc
,
7924 Reason
=> PE_Unchecked_Union_Restriction
));
7926 -- Emit a warning on source equalities only, otherwise the
7927 -- message may appear out of place due to internal use. The
7928 -- warning is unconditional because it is required by the
7931 if Comes_From_Source
(N
) then
7933 ("Unchecked_Union discriminants cannot be determined??",
7936 ("\Program_Error will be raised for equality operation??",
7940 -- Prevent Gigi from generating incorrect code by rewriting
7941 -- the equality as a standard False (documented where???).
7944 New_Occurrence_Of
(Standard_False
, Loc
));
7947 -- If a type support function is present (for complex cases), use it
7949 elsif Present
(TSS
(Root_Type
(Typl
), TSS_Composite_Equality
)) then
7951 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
7953 -- When comparing two Bounded_Strings, use the primitive equality of
7954 -- the root Super_String type.
7956 elsif Is_Bounded_String
(Typl
) then
7958 First_Elmt
(Collect_Primitive_Operations
(Root_Type
(Typl
)));
7960 while Present
(Prim
) loop
7961 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7962 and then Etype
(First_Formal
(Node
(Prim
))) =
7963 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7964 and then Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7969 -- A Super_String type should always have a primitive equality
7971 pragma Assert
(Present
(Prim
));
7972 Build_Equality_Call
(Node
(Prim
));
7974 -- Otherwise expand the component by component equality. Note that
7975 -- we never use block-bit comparisons for records, because of the
7976 -- problems with gaps. The back end will often be able to recombine
7977 -- the separate comparisons that we generate here.
7980 Remove_Side_Effects
(Lhs
);
7981 Remove_Side_Effects
(Rhs
);
7983 Expand_Record_Equality
(N
, Typl
, Lhs
, Rhs
, Bodies
));
7985 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
7986 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7990 -- Test if result is known at compile time
7992 Rewrite_Comparison
(N
);
7994 -- Special optimization of length comparison
7996 Optimize_Length_Comparison
(N
);
7998 -- One more special case: if we have a comparison of X'Result = expr
7999 -- in floating-point, then if not already there, change expr to be
8000 -- f'Machine (expr) to eliminate surprise from extra precision.
8002 if Is_Floating_Point_Type
(Typl
)
8003 and then Nkind
(Original_Node
(Lhs
)) = N_Attribute_Reference
8004 and then Attribute_Name
(Original_Node
(Lhs
)) = Name_Result
8006 -- Stick in the Typ'Machine call if not already there
8008 if Nkind
(Rhs
) /= N_Attribute_Reference
8009 or else Attribute_Name
(Rhs
) /= Name_Machine
8012 Make_Attribute_Reference
(Loc
,
8013 Prefix
=> New_Occurrence_Of
(Typl
, Loc
),
8014 Attribute_Name
=> Name_Machine
,
8015 Expressions
=> New_List
(Relocate_Node
(Rhs
))));
8016 Analyze_And_Resolve
(Rhs
, Typl
);
8021 -----------------------
8022 -- Expand_N_Op_Expon --
8023 -----------------------
8025 procedure Expand_N_Op_Expon
(N
: Node_Id
) is
8026 Loc
: constant Source_Ptr
:= Sloc
(N
);
8027 Ovflo
: constant Boolean := Do_Overflow_Check
(N
);
8028 Typ
: constant Entity_Id
:= Etype
(N
);
8029 Rtyp
: constant Entity_Id
:= Root_Type
(Typ
);
8033 function Wrap_MA
(Exp
: Node_Id
) return Node_Id
;
8034 -- Given an expression Exp, if the root type is Float or Long_Float,
8035 -- then wrap the expression in a call of Bastyp'Machine, to stop any
8036 -- extra precision. This is done to ensure that X**A = X**B when A is
8037 -- a static constant and B is a variable with the same value. For any
8038 -- other type, the node Exp is returned unchanged.
8044 function Wrap_MA
(Exp
: Node_Id
) return Node_Id
is
8045 Loc
: constant Source_Ptr
:= Sloc
(Exp
);
8048 if Rtyp
= Standard_Float
or else Rtyp
= Standard_Long_Float
then
8050 Make_Attribute_Reference
(Loc
,
8051 Attribute_Name
=> Name_Machine
,
8052 Prefix
=> New_Occurrence_Of
(Bastyp
, Loc
),
8053 Expressions
=> New_List
(Relocate_Node
(Exp
)));
8071 -- Start of processing for Expand_N_Op_Expon
8074 Binary_Op_Validity_Checks
(N
);
8076 -- CodePeer wants to see the unexpanded N_Op_Expon node
8078 if CodePeer_Mode
then
8082 -- Relocation of left and right operands must be done after performing
8083 -- the validity checks since the generation of validation checks may
8084 -- remove side effects.
8086 Base
:= Relocate_Node
(Left_Opnd
(N
));
8087 Bastyp
:= Etype
(Base
);
8088 Exp
:= Relocate_Node
(Right_Opnd
(N
));
8089 Exptyp
:= Etype
(Exp
);
8091 -- If either operand is of a private type, then we have the use of an
8092 -- intrinsic operator, and we get rid of the privateness, by using root
8093 -- types of underlying types for the actual operation. Otherwise the
8094 -- private types will cause trouble if we expand multiplications or
8095 -- shifts etc. We also do this transformation if the result type is
8096 -- different from the base type.
8098 if Is_Private_Type
(Etype
(Base
))
8099 or else Is_Private_Type
(Typ
)
8100 or else Is_Private_Type
(Exptyp
)
8101 or else Rtyp
/= Root_Type
(Bastyp
)
8104 Bt
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Bastyp
));
8105 Et
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Exptyp
));
8108 Unchecked_Convert_To
(Typ
,
8110 Left_Opnd
=> Unchecked_Convert_To
(Bt
, Base
),
8111 Right_Opnd
=> Unchecked_Convert_To
(Et
, Exp
))));
8112 Analyze_And_Resolve
(N
, Typ
);
8117 -- Check for MINIMIZED/ELIMINATED overflow mode
8119 if Minimized_Eliminated_Overflow_Check
(N
) then
8120 Apply_Arithmetic_Overflow_Check
(N
);
8124 -- Test for case of known right argument where we can replace the
8125 -- exponentiation by an equivalent expression using multiplication.
8127 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
8128 -- configurable run-time mode, we may not have the exponentiation
8129 -- routine available, and we don't want the legality of the program
8130 -- to depend on how clever the compiler is in knowing values.
8132 if CRT_Safe_Compile_Time_Known_Value
(Exp
) then
8133 Expv
:= Expr_Value
(Exp
);
8135 -- We only fold small non-negative exponents. You might think we
8136 -- could fold small negative exponents for the real case, but we
8137 -- can't because we are required to raise Constraint_Error for
8138 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
8139 -- See ACVC test C4A012B, and it is not worth generating the test.
8141 -- For small negative exponents, we return the reciprocal of
8142 -- the folding of the exponentiation for the opposite (positive)
8143 -- exponent, as required by Ada RM 4.5.6(11/3).
8145 if abs Expv
<= 4 then
8147 -- X ** 0 = 1 (or 1.0)
8151 -- Call Remove_Side_Effects to ensure that any side effects
8152 -- in the ignored left operand (in particular function calls
8153 -- to user defined functions) are properly executed.
8155 Remove_Side_Effects
(Base
);
8157 if Ekind
(Typ
) in Integer_Kind
then
8158 Xnode
:= Make_Integer_Literal
(Loc
, Intval
=> 1);
8160 Xnode
:= Make_Real_Literal
(Loc
, Ureal_1
);
8173 Make_Op_Multiply
(Loc
,
8174 Left_Opnd
=> Duplicate_Subexpr
(Base
),
8175 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)));
8177 -- X ** 3 = X * X * X
8182 Make_Op_Multiply
(Loc
,
8184 Make_Op_Multiply
(Loc
,
8185 Left_Opnd
=> Duplicate_Subexpr
(Base
),
8186 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)),
8187 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)));
8192 -- En : constant base'type := base * base;
8197 Temp
:= Make_Temporary
(Loc
, 'E', Base
);
8200 Make_Expression_With_Actions
(Loc
,
8201 Actions
=> New_List
(
8202 Make_Object_Declaration
(Loc
,
8203 Defining_Identifier
=> Temp
,
8204 Constant_Present
=> True,
8205 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
8208 Make_Op_Multiply
(Loc
,
8210 Duplicate_Subexpr
(Base
),
8212 Duplicate_Subexpr_No_Checks
(Base
))))),
8216 Make_Op_Multiply
(Loc
,
8217 Left_Opnd
=> New_Occurrence_Of
(Temp
, Loc
),
8218 Right_Opnd
=> New_Occurrence_Of
(Temp
, Loc
))));
8220 -- X ** N = 1.0 / X ** (-N)
8225 (Expv
= -1 or Expv
= -2 or Expv
= -3 or Expv
= -4);
8228 Make_Op_Divide
(Loc
,
8230 Make_Float_Literal
(Loc
,
8232 Significand
=> Uint_1
,
8233 Exponent
=> Uint_0
),
8236 Left_Opnd
=> Duplicate_Subexpr
(Base
),
8238 Make_Integer_Literal
(Loc
,
8243 Analyze_And_Resolve
(N
, Typ
);
8248 -- Deal with optimizing 2 ** expression to shift where possible
8250 -- Note: we used to check that Exptyp was an unsigned type. But that is
8251 -- an unnecessary check, since if Exp is negative, we have a run-time
8252 -- error that is either caught (so we get the right result) or we have
8253 -- suppressed the check, in which case the code is erroneous anyway.
8255 if Is_Integer_Type
(Rtyp
)
8257 -- The base value must be "safe compile-time known", and exactly 2
8259 and then Nkind
(Base
) = N_Integer_Literal
8260 and then CRT_Safe_Compile_Time_Known_Value
(Base
)
8261 and then Expr_Value
(Base
) = Uint_2
8263 -- We only handle cases where the right type is a integer
8265 and then Is_Integer_Type
(Root_Type
(Exptyp
))
8266 and then Esize
(Root_Type
(Exptyp
)) <= Esize
(Standard_Integer
)
8268 -- This transformation is not applicable for a modular type with a
8269 -- nonbinary modulus because we do not handle modular reduction in
8270 -- a correct manner if we attempt this transformation in this case.
8272 and then not Non_Binary_Modulus
(Typ
)
8274 -- Handle the cases where our parent is a division or multiplication
8275 -- specially. In these cases we can convert to using a shift at the
8276 -- parent level if we are not doing overflow checking, since it is
8277 -- too tricky to combine the overflow check at the parent level.
8280 and then Nkind_In
(Parent
(N
), N_Op_Divide
, N_Op_Multiply
)
8283 P
: constant Node_Id
:= Parent
(N
);
8284 L
: constant Node_Id
:= Left_Opnd
(P
);
8285 R
: constant Node_Id
:= Right_Opnd
(P
);
8288 if (Nkind
(P
) = N_Op_Multiply
8290 ((Is_Integer_Type
(Etype
(L
)) and then R
= N
)
8292 (Is_Integer_Type
(Etype
(R
)) and then L
= N
))
8293 and then not Do_Overflow_Check
(P
))
8296 (Nkind
(P
) = N_Op_Divide
8297 and then Is_Integer_Type
(Etype
(L
))
8298 and then Is_Unsigned_Type
(Etype
(L
))
8300 and then not Do_Overflow_Check
(P
))
8302 Set_Is_Power_Of_2_For_Shift
(N
);
8307 -- Here we just have 2 ** N on its own, so we can convert this to a
8308 -- shift node. We are prepared to deal with overflow here, and we
8309 -- also have to handle proper modular reduction for binary modular.
8318 -- Maximum shift count with no overflow
8321 -- Set True if we must test the shift count
8324 -- Node for test against TestS
8327 -- Compute maximum shift based on the underlying size. For a
8328 -- modular type this is one less than the size.
8330 if Is_Modular_Integer_Type
(Typ
) then
8332 -- For modular integer types, this is the size of the value
8333 -- being shifted minus one. Any larger values will cause
8334 -- modular reduction to a result of zero. Note that we do
8335 -- want the RM_Size here (e.g. mod 2 ** 7, we want a result
8336 -- of 6, since 2**7 should be reduced to zero).
8338 MaxS
:= RM_Size
(Rtyp
) - 1;
8340 -- For signed integer types, we use the size of the value
8341 -- being shifted minus 2. Larger values cause overflow.
8344 MaxS
:= Esize
(Rtyp
) - 2;
8347 -- Determine range to see if it can be larger than MaxS
8350 (Right_Opnd
(N
), OK
, Lo
, Hi
, Assume_Valid
=> True);
8351 TestS
:= (not OK
) or else Hi
> MaxS
;
8353 -- Signed integer case
8355 if Is_Signed_Integer_Type
(Typ
) then
8357 -- Generate overflow check if overflow is active. Note that
8358 -- we can simply ignore the possibility of overflow if the
8359 -- flag is not set (means that overflow cannot happen or
8360 -- that overflow checks are suppressed).
8362 if Ovflo
and TestS
then
8364 Make_Raise_Constraint_Error
(Loc
,
8367 Left_Opnd
=> Duplicate_Subexpr
(Right_Opnd
(N
)),
8368 Right_Opnd
=> Make_Integer_Literal
(Loc
, MaxS
)),
8369 Reason
=> CE_Overflow_Check_Failed
));
8372 -- Now rewrite node as Shift_Left (1, right-operand)
8375 Make_Op_Shift_Left
(Loc
,
8376 Left_Opnd
=> Make_Integer_Literal
(Loc
, Uint_1
),
8377 Right_Opnd
=> Right_Opnd
(N
)));
8379 -- Modular integer case
8381 else pragma Assert
(Is_Modular_Integer_Type
(Typ
));
8383 -- If shift count can be greater than MaxS, we need to wrap
8384 -- the shift in a test that will reduce the result value to
8385 -- zero if this shift count is exceeded.
8389 -- Note: build node for the comparison first, before we
8390 -- reuse the Right_Opnd, so that we have proper parents
8391 -- in place for the Duplicate_Subexpr call.
8395 Left_Opnd
=> Duplicate_Subexpr
(Right_Opnd
(N
)),
8396 Right_Opnd
=> Make_Integer_Literal
(Loc
, MaxS
));
8399 Make_If_Expression
(Loc
,
8400 Expressions
=> New_List
(
8402 Make_Integer_Literal
(Loc
, Uint_0
),
8403 Make_Op_Shift_Left
(Loc
,
8404 Left_Opnd
=> Make_Integer_Literal
(Loc
, Uint_1
),
8405 Right_Opnd
=> Right_Opnd
(N
)))));
8407 -- If we know shift count cannot be greater than MaxS, then
8408 -- it is safe to just rewrite as a shift with no test.
8412 Make_Op_Shift_Left
(Loc
,
8413 Left_Opnd
=> Make_Integer_Literal
(Loc
, Uint_1
),
8414 Right_Opnd
=> Right_Opnd
(N
)));
8418 Analyze_And_Resolve
(N
, Typ
);
8424 -- Fall through if exponentiation must be done using a runtime routine
8426 -- First deal with modular case
8428 if Is_Modular_Integer_Type
(Rtyp
) then
8430 -- Nonbinary modular case, we call the special exponentiation
8431 -- routine for the nonbinary case, converting the argument to
8432 -- Long_Long_Integer and passing the modulus value. Then the
8433 -- result is converted back to the base type.
8435 if Non_Binary_Modulus
(Rtyp
) then
8438 Make_Function_Call
(Loc
,
8440 New_Occurrence_Of
(RTE
(RE_Exp_Modular
), Loc
),
8441 Parameter_Associations
=> New_List
(
8442 Convert_To
(RTE
(RE_Unsigned
), Base
),
8443 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
)),
8446 -- Binary modular case, in this case, we call one of two routines,
8447 -- either the unsigned integer case, or the unsigned long long
8448 -- integer case, with a final "and" operation to do the required mod.
8451 if UI_To_Int
(Esize
(Rtyp
)) <= Standard_Integer_Size
then
8452 Ent
:= RTE
(RE_Exp_Unsigned
);
8454 Ent
:= RTE
(RE_Exp_Long_Long_Unsigned
);
8461 Make_Function_Call
(Loc
,
8462 Name
=> New_Occurrence_Of
(Ent
, Loc
),
8463 Parameter_Associations
=> New_List
(
8464 Convert_To
(Etype
(First_Formal
(Ent
)), Base
),
8467 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
) - 1))));
8471 -- Common exit point for modular type case
8473 Analyze_And_Resolve
(N
, Typ
);
8476 -- Signed integer cases, done using either Integer or Long_Long_Integer.
8477 -- It is not worth having routines for Short_[Short_]Integer, since for
8478 -- most machines it would not help, and it would generate more code that
8479 -- might need certification when a certified run time is required.
8481 -- In the integer cases, we have two routines, one for when overflow
8482 -- checks are required, and one when they are not required, since there
8483 -- is a real gain in omitting checks on many machines.
8485 elsif Rtyp
= Base_Type
(Standard_Long_Long_Integer
)
8486 or else (Rtyp
= Base_Type
(Standard_Long_Integer
)
8488 Esize
(Standard_Long_Integer
) > Esize
(Standard_Integer
))
8489 or else Rtyp
= Universal_Integer
8491 Etyp
:= Standard_Long_Long_Integer
;
8494 Rent
:= RE_Exp_Long_Long_Integer
;
8496 Rent
:= RE_Exn_Long_Long_Integer
;
8499 elsif Is_Signed_Integer_Type
(Rtyp
) then
8500 Etyp
:= Standard_Integer
;
8503 Rent
:= RE_Exp_Integer
;
8505 Rent
:= RE_Exn_Integer
;
8508 -- Floating-point cases. We do not need separate routines for the
8509 -- overflow case here, since in the case of floating-point, we generate
8510 -- infinities anyway as a rule (either that or we automatically trap
8511 -- overflow), and if there is an infinity generated and a range check
8512 -- is required, the check will fail anyway.
8514 -- Historical note: we used to convert everything to Long_Long_Float
8515 -- and call a single common routine, but this had the undesirable effect
8516 -- of giving different results for small static exponent values and the
8517 -- same dynamic values.
8520 pragma Assert
(Is_Floating_Point_Type
(Rtyp
));
8522 if Rtyp
= Standard_Float
then
8523 Etyp
:= Standard_Float
;
8524 Rent
:= RE_Exn_Float
;
8526 elsif Rtyp
= Standard_Long_Float
then
8527 Etyp
:= Standard_Long_Float
;
8528 Rent
:= RE_Exn_Long_Float
;
8531 Etyp
:= Standard_Long_Long_Float
;
8532 Rent
:= RE_Exn_Long_Long_Float
;
8536 -- Common processing for integer cases and floating-point cases.
8537 -- If we are in the right type, we can call runtime routine directly
8540 and then Rtyp
/= Universal_Integer
8541 and then Rtyp
/= Universal_Real
8545 Make_Function_Call
(Loc
,
8546 Name
=> New_Occurrence_Of
(RTE
(Rent
), Loc
),
8547 Parameter_Associations
=> New_List
(Base
, Exp
))));
8549 -- Otherwise we have to introduce conversions (conversions are also
8550 -- required in the universal cases, since the runtime routine is
8551 -- typed using one of the standard types).
8556 Make_Function_Call
(Loc
,
8557 Name
=> New_Occurrence_Of
(RTE
(Rent
), Loc
),
8558 Parameter_Associations
=> New_List
(
8559 Convert_To
(Etyp
, Base
),
8563 Analyze_And_Resolve
(N
, Typ
);
8567 when RE_Not_Available
=>
8569 end Expand_N_Op_Expon
;
8571 --------------------
8572 -- Expand_N_Op_Ge --
8573 --------------------
8575 procedure Expand_N_Op_Ge
(N
: Node_Id
) is
8576 Typ
: constant Entity_Id
:= Etype
(N
);
8577 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8578 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8579 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8582 Binary_Op_Validity_Checks
(N
);
8584 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8585 -- means we no longer have a comparison operation, we are all done.
8587 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8589 if Nkind
(N
) /= N_Op_Ge
then
8595 if Is_Array_Type
(Typ1
) then
8596 Expand_Array_Comparison
(N
);
8600 -- Deal with boolean operands
8602 if Is_Boolean_Type
(Typ1
) then
8603 Adjust_Condition
(Op1
);
8604 Adjust_Condition
(Op2
);
8605 Set_Etype
(N
, Standard_Boolean
);
8606 Adjust_Result_Type
(N
, Typ
);
8609 Rewrite_Comparison
(N
);
8611 Optimize_Length_Comparison
(N
);
8614 --------------------
8615 -- Expand_N_Op_Gt --
8616 --------------------
8618 procedure Expand_N_Op_Gt
(N
: Node_Id
) is
8619 Typ
: constant Entity_Id
:= Etype
(N
);
8620 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8621 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8622 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8625 Binary_Op_Validity_Checks
(N
);
8627 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8628 -- means we no longer have a comparison operation, we are all done.
8630 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8632 if Nkind
(N
) /= N_Op_Gt
then
8636 -- Deal with array type operands
8638 if Is_Array_Type
(Typ1
) then
8639 Expand_Array_Comparison
(N
);
8643 -- Deal with boolean type operands
8645 if Is_Boolean_Type
(Typ1
) then
8646 Adjust_Condition
(Op1
);
8647 Adjust_Condition
(Op2
);
8648 Set_Etype
(N
, Standard_Boolean
);
8649 Adjust_Result_Type
(N
, Typ
);
8652 Rewrite_Comparison
(N
);
8654 Optimize_Length_Comparison
(N
);
8657 --------------------
8658 -- Expand_N_Op_Le --
8659 --------------------
8661 procedure Expand_N_Op_Le
(N
: Node_Id
) is
8662 Typ
: constant Entity_Id
:= Etype
(N
);
8663 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8664 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8665 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8668 Binary_Op_Validity_Checks
(N
);
8670 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8671 -- means we no longer have a comparison operation, we are all done.
8673 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8675 if Nkind
(N
) /= N_Op_Le
then
8679 -- Deal with array type operands
8681 if Is_Array_Type
(Typ1
) then
8682 Expand_Array_Comparison
(N
);
8686 -- Deal with Boolean type operands
8688 if Is_Boolean_Type
(Typ1
) then
8689 Adjust_Condition
(Op1
);
8690 Adjust_Condition
(Op2
);
8691 Set_Etype
(N
, Standard_Boolean
);
8692 Adjust_Result_Type
(N
, Typ
);
8695 Rewrite_Comparison
(N
);
8697 Optimize_Length_Comparison
(N
);
8700 --------------------
8701 -- Expand_N_Op_Lt --
8702 --------------------
8704 procedure Expand_N_Op_Lt
(N
: Node_Id
) is
8705 Typ
: constant Entity_Id
:= Etype
(N
);
8706 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8707 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8708 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8711 Binary_Op_Validity_Checks
(N
);
8713 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8714 -- means we no longer have a comparison operation, we are all done.
8716 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8718 if Nkind
(N
) /= N_Op_Lt
then
8722 -- Deal with array type operands
8724 if Is_Array_Type
(Typ1
) then
8725 Expand_Array_Comparison
(N
);
8729 -- Deal with Boolean type operands
8731 if Is_Boolean_Type
(Typ1
) then
8732 Adjust_Condition
(Op1
);
8733 Adjust_Condition
(Op2
);
8734 Set_Etype
(N
, Standard_Boolean
);
8735 Adjust_Result_Type
(N
, Typ
);
8738 Rewrite_Comparison
(N
);
8740 Optimize_Length_Comparison
(N
);
8743 -----------------------
8744 -- Expand_N_Op_Minus --
8745 -----------------------
8747 procedure Expand_N_Op_Minus
(N
: Node_Id
) is
8748 Loc
: constant Source_Ptr
:= Sloc
(N
);
8749 Typ
: constant Entity_Id
:= Etype
(N
);
8752 Unary_Op_Validity_Checks
(N
);
8754 -- Check for MINIMIZED/ELIMINATED overflow mode
8756 if Minimized_Eliminated_Overflow_Check
(N
) then
8757 Apply_Arithmetic_Overflow_Check
(N
);
8761 if not Backend_Overflow_Checks_On_Target
8762 and then Is_Signed_Integer_Type
(Etype
(N
))
8763 and then Do_Overflow_Check
(N
)
8765 -- Software overflow checking expands -expr into (0 - expr)
8768 Make_Op_Subtract
(Loc
,
8769 Left_Opnd
=> Make_Integer_Literal
(Loc
, 0),
8770 Right_Opnd
=> Right_Opnd
(N
)));
8772 Analyze_And_Resolve
(N
, Typ
);
8775 Expand_Nonbinary_Modular_Op
(N
);
8776 end Expand_N_Op_Minus
;
8778 ---------------------
8779 -- Expand_N_Op_Mod --
8780 ---------------------
8782 procedure Expand_N_Op_Mod
(N
: Node_Id
) is
8783 Loc
: constant Source_Ptr
:= Sloc
(N
);
8784 Typ
: constant Entity_Id
:= Etype
(N
);
8785 DDC
: constant Boolean := Do_Division_Check
(N
);
8798 pragma Warnings
(Off
, Lhi
);
8801 Binary_Op_Validity_Checks
(N
);
8803 -- Check for MINIMIZED/ELIMINATED overflow mode
8805 if Minimized_Eliminated_Overflow_Check
(N
) then
8806 Apply_Arithmetic_Overflow_Check
(N
);
8810 if Is_Integer_Type
(Etype
(N
)) then
8811 Apply_Divide_Checks
(N
);
8813 -- All done if we don't have a MOD any more, which can happen as a
8814 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8816 if Nkind
(N
) /= N_Op_Mod
then
8821 -- Proceed with expansion of mod operator
8823 Left
:= Left_Opnd
(N
);
8824 Right
:= Right_Opnd
(N
);
8826 Determine_Range
(Right
, ROK
, Rlo
, Rhi
, Assume_Valid
=> True);
8827 Determine_Range
(Left
, LOK
, Llo
, Lhi
, Assume_Valid
=> True);
8829 -- Convert mod to rem if operands are both known to be non-negative, or
8830 -- both known to be non-positive (these are the cases in which rem and
8831 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
8832 -- likely that this will improve the quality of code, (the operation now
8833 -- corresponds to the hardware remainder), and it does not seem likely
8834 -- that it could be harmful. It also avoids some cases of the elaborate
8835 -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %).
8838 and then ((Llo
>= 0 and then Rlo
>= 0)
8840 (Lhi
<= 0 and then Rhi
<= 0))
8843 Make_Op_Rem
(Sloc
(N
),
8844 Left_Opnd
=> Left_Opnd
(N
),
8845 Right_Opnd
=> Right_Opnd
(N
)));
8847 -- Instead of reanalyzing the node we do the analysis manually. This
8848 -- avoids anomalies when the replacement is done in an instance and
8849 -- is epsilon more efficient.
8851 Set_Entity
(N
, Standard_Entity
(S_Op_Rem
));
8853 Set_Do_Division_Check
(N
, DDC
);
8854 Expand_N_Op_Rem
(N
);
8858 -- Otherwise, normal mod processing
8861 -- Apply optimization x mod 1 = 0. We don't really need that with
8862 -- gcc, but it is useful with other back ends and is certainly
8865 if Is_Integer_Type
(Etype
(N
))
8866 and then Compile_Time_Known_Value
(Right
)
8867 and then Expr_Value
(Right
) = Uint_1
8869 -- Call Remove_Side_Effects to ensure that any side effects in
8870 -- the ignored left operand (in particular function calls to
8871 -- user defined functions) are properly executed.
8873 Remove_Side_Effects
(Left
);
8875 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
8876 Analyze_And_Resolve
(N
, Typ
);
8880 -- If we still have a mod operator and we are in Modify_Tree_For_C
8881 -- mode, and we have a signed integer type, then here is where we do
8882 -- the rewrite in terms of Rem. Note this rewrite bypasses the need
8883 -- for the special handling of the annoying case of largest negative
8884 -- number mod minus one.
8886 if Nkind
(N
) = N_Op_Mod
8887 and then Is_Signed_Integer_Type
(Typ
)
8888 and then Modify_Tree_For_C
8890 -- In the general case, we expand A mod B as
8892 -- Tnn : constant typ := A rem B;
8894 -- (if (A >= 0) = (B >= 0) then Tnn
8895 -- elsif Tnn = 0 then 0
8898 -- The comparison can be written simply as A >= 0 if we know that
8899 -- B >= 0 which is a very common case.
8901 -- An important optimization is when B is known at compile time
8902 -- to be 2**K for some constant. In this case we can simply AND
8903 -- the left operand with the bit string 2**K-1 (i.e. K 1-bits)
8904 -- and that works for both the positive and negative cases.
8907 P2
: constant Nat
:= Power_Of_Two
(Right
);
8912 Unchecked_Convert_To
(Typ
,
8915 Unchecked_Convert_To
8916 (Corresponding_Unsigned_Type
(Typ
), Left
),
8918 Make_Integer_Literal
(Loc
, 2 ** P2
- 1))));
8919 Analyze_And_Resolve
(N
, Typ
);
8924 -- Here for the full rewrite
8927 Tnn
: constant Entity_Id
:= Make_Temporary
(Sloc
(N
), 'T', N
);
8933 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left
),
8934 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
8936 if not LOK
or else Rlo
< 0 then
8942 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Right
),
8943 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)));
8947 Make_Object_Declaration
(Loc
,
8948 Defining_Identifier
=> Tnn
,
8949 Constant_Present
=> True,
8950 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
8954 Right_Opnd
=> Right
)));
8957 Make_If_Expression
(Loc
,
8958 Expressions
=> New_List
(
8960 New_Occurrence_Of
(Tnn
, Loc
),
8961 Make_If_Expression
(Loc
,
8963 Expressions
=> New_List
(
8965 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
8966 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
8967 Make_Integer_Literal
(Loc
, 0),
8969 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
8971 Duplicate_Subexpr_No_Checks
(Right
)))))));
8973 Analyze_And_Resolve
(N
, Typ
);
8978 -- Deal with annoying case of largest negative number mod minus one.
8979 -- Gigi may not handle this case correctly, because on some targets,
8980 -- the mod value is computed using a divide instruction which gives
8981 -- an overflow trap for this case.
8983 -- It would be a bit more efficient to figure out which targets
8984 -- this is really needed for, but in practice it is reasonable
8985 -- to do the following special check in all cases, since it means
8986 -- we get a clearer message, and also the overhead is minimal given
8987 -- that division is expensive in any case.
8989 -- In fact the check is quite easy, if the right operand is -1, then
8990 -- the mod value is always 0, and we can just ignore the left operand
8991 -- completely in this case.
8993 -- This only applies if we still have a mod operator. Skip if we
8994 -- have already rewritten this (e.g. in the case of eliminated
8995 -- overflow checks which have driven us into bignum mode).
8997 if Nkind
(N
) = N_Op_Mod
then
8999 -- The operand type may be private (e.g. in the expansion of an
9000 -- intrinsic operation) so we must use the underlying type to get
9001 -- the bounds, and convert the literals explicitly.
9005 (Type_Low_Bound
(Base_Type
(Underlying_Type
(Etype
(Left
)))));
9007 if ((not ROK
) or else (Rlo
<= (-1) and then (-1) <= Rhi
))
9008 and then ((not LOK
) or else (Llo
= LLB
))
9011 Make_If_Expression
(Loc
,
9012 Expressions
=> New_List
(
9014 Left_Opnd
=> Duplicate_Subexpr
(Right
),
9016 Unchecked_Convert_To
(Typ
,
9017 Make_Integer_Literal
(Loc
, -1))),
9018 Unchecked_Convert_To
(Typ
,
9019 Make_Integer_Literal
(Loc
, Uint_0
)),
9020 Relocate_Node
(N
))));
9022 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
9023 Analyze_And_Resolve
(N
, Typ
);
9027 end Expand_N_Op_Mod
;
9029 --------------------------
9030 -- Expand_N_Op_Multiply --
9031 --------------------------
9033 procedure Expand_N_Op_Multiply
(N
: Node_Id
) is
9034 Loc
: constant Source_Ptr
:= Sloc
(N
);
9035 Lop
: constant Node_Id
:= Left_Opnd
(N
);
9036 Rop
: constant Node_Id
:= Right_Opnd
(N
);
9038 Lp2
: constant Boolean :=
9039 Nkind
(Lop
) = N_Op_Expon
and then Is_Power_Of_2_For_Shift
(Lop
);
9040 Rp2
: constant Boolean :=
9041 Nkind
(Rop
) = N_Op_Expon
and then Is_Power_Of_2_For_Shift
(Rop
);
9043 Ltyp
: constant Entity_Id
:= Etype
(Lop
);
9044 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
9045 Typ
: Entity_Id
:= Etype
(N
);
9048 Binary_Op_Validity_Checks
(N
);
9050 -- Check for MINIMIZED/ELIMINATED overflow mode
9052 if Minimized_Eliminated_Overflow_Check
(N
) then
9053 Apply_Arithmetic_Overflow_Check
(N
);
9057 -- Special optimizations for integer types
9059 if Is_Integer_Type
(Typ
) then
9061 -- N * 0 = 0 for integer types
9063 if Compile_Time_Known_Value
(Rop
)
9064 and then Expr_Value
(Rop
) = Uint_0
9066 -- Call Remove_Side_Effects to ensure that any side effects in
9067 -- the ignored left operand (in particular function calls to
9068 -- user defined functions) are properly executed.
9070 Remove_Side_Effects
(Lop
);
9072 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
9073 Analyze_And_Resolve
(N
, Typ
);
9077 -- Similar handling for 0 * N = 0
9079 if Compile_Time_Known_Value
(Lop
)
9080 and then Expr_Value
(Lop
) = Uint_0
9082 Remove_Side_Effects
(Rop
);
9083 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
9084 Analyze_And_Resolve
(N
, Typ
);
9088 -- N * 1 = 1 * N = N for integer types
9090 -- This optimisation is not done if we are going to
9091 -- rewrite the product 1 * 2 ** N to a shift.
9093 if Compile_Time_Known_Value
(Rop
)
9094 and then Expr_Value
(Rop
) = Uint_1
9100 elsif Compile_Time_Known_Value
(Lop
)
9101 and then Expr_Value
(Lop
) = Uint_1
9109 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
9110 -- Is_Power_Of_2_For_Shift is set means that we know that our left
9111 -- operand is an integer, as required for this to work.
9116 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
9120 Left_Opnd
=> Make_Integer_Literal
(Loc
, 2),
9123 Left_Opnd
=> Right_Opnd
(Lop
),
9124 Right_Opnd
=> Right_Opnd
(Rop
))));
9125 Analyze_And_Resolve
(N
, Typ
);
9129 -- If the result is modular, perform the reduction of the result
9132 if Is_Modular_Integer_Type
(Typ
)
9133 and then not Non_Binary_Modulus
(Typ
)
9138 Make_Op_Shift_Left
(Loc
,
9141 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))),
9143 Make_Integer_Literal
(Loc
, Modulus
(Typ
) - 1)));
9147 Make_Op_Shift_Left
(Loc
,
9150 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))));
9153 Analyze_And_Resolve
(N
, Typ
);
9157 -- Same processing for the operands the other way round
9160 if Is_Modular_Integer_Type
(Typ
)
9161 and then not Non_Binary_Modulus
(Typ
)
9166 Make_Op_Shift_Left
(Loc
,
9169 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))),
9171 Make_Integer_Literal
(Loc
, Modulus
(Typ
) - 1)));
9175 Make_Op_Shift_Left
(Loc
,
9178 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))));
9181 Analyze_And_Resolve
(N
, Typ
);
9185 -- Do required fixup of universal fixed operation
9187 if Typ
= Universal_Fixed
then
9188 Fixup_Universal_Fixed_Operation
(N
);
9192 -- Multiplications with fixed-point results
9194 if Is_Fixed_Point_Type
(Typ
) then
9196 -- No special processing if Treat_Fixed_As_Integer is set, since from
9197 -- a semantic point of view such operations are simply integer
9198 -- operations and will be treated that way.
9200 if not Treat_Fixed_As_Integer
(N
) then
9202 -- Case of fixed * integer => fixed
9204 if Is_Integer_Type
(Rtyp
) then
9205 Expand_Multiply_Fixed_By_Integer_Giving_Fixed
(N
);
9207 -- Case of integer * fixed => fixed
9209 elsif Is_Integer_Type
(Ltyp
) then
9210 Expand_Multiply_Integer_By_Fixed_Giving_Fixed
(N
);
9212 -- Case of fixed * fixed => fixed
9215 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed
(N
);
9219 -- Other cases of multiplication of fixed-point operands. Again we
9220 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
9222 elsif (Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
))
9223 and then not Treat_Fixed_As_Integer
(N
)
9225 if Is_Integer_Type
(Typ
) then
9226 Expand_Multiply_Fixed_By_Fixed_Giving_Integer
(N
);
9228 pragma Assert
(Is_Floating_Point_Type
(Typ
));
9229 Expand_Multiply_Fixed_By_Fixed_Giving_Float
(N
);
9232 -- Mixed-mode operations can appear in a non-static universal context,
9233 -- in which case the integer argument must be converted explicitly.
9235 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Rtyp
) then
9236 Rewrite
(Rop
, Convert_To
(Universal_Real
, Relocate_Node
(Rop
)));
9237 Analyze_And_Resolve
(Rop
, Universal_Real
);
9239 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Ltyp
) then
9240 Rewrite
(Lop
, Convert_To
(Universal_Real
, Relocate_Node
(Lop
)));
9241 Analyze_And_Resolve
(Lop
, Universal_Real
);
9243 -- Non-fixed point cases, check software overflow checking required
9245 elsif Is_Signed_Integer_Type
(Etype
(N
)) then
9246 Apply_Arithmetic_Overflow_Check
(N
);
9249 -- Overflow checks for floating-point if -gnateF mode active
9251 Check_Float_Op_Overflow
(N
);
9253 Expand_Nonbinary_Modular_Op
(N
);
9254 end Expand_N_Op_Multiply
;
9256 --------------------
9257 -- Expand_N_Op_Ne --
9258 --------------------
9260 procedure Expand_N_Op_Ne
(N
: Node_Id
) is
9261 Typ
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
9264 -- Case of elementary type with standard operator
9266 if Is_Elementary_Type
(Typ
)
9267 and then Sloc
(Entity
(N
)) = Standard_Location
9269 Binary_Op_Validity_Checks
(N
);
9271 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
9272 -- means we no longer have a /= operation, we are all done.
9274 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
9276 if Nkind
(N
) /= N_Op_Ne
then
9280 -- Boolean types (requiring handling of non-standard case)
9282 if Is_Boolean_Type
(Typ
) then
9283 Adjust_Condition
(Left_Opnd
(N
));
9284 Adjust_Condition
(Right_Opnd
(N
));
9285 Set_Etype
(N
, Standard_Boolean
);
9286 Adjust_Result_Type
(N
, Typ
);
9289 Rewrite_Comparison
(N
);
9291 -- For all cases other than elementary types, we rewrite node as the
9292 -- negation of an equality operation, and reanalyze. The equality to be
9293 -- used is defined in the same scope and has the same signature. This
9294 -- signature must be set explicitly since in an instance it may not have
9295 -- the same visibility as in the generic unit. This avoids duplicating
9296 -- or factoring the complex code for record/array equality tests etc.
9298 -- This case is also used for the minimal expansion performed in
9303 Loc
: constant Source_Ptr
:= Sloc
(N
);
9305 Ne
: constant Entity_Id
:= Entity
(N
);
9308 Binary_Op_Validity_Checks
(N
);
9314 Left_Opnd
=> Left_Opnd
(N
),
9315 Right_Opnd
=> Right_Opnd
(N
)));
9317 -- The level of parentheses is useless in GNATprove mode, and
9318 -- bumping its level here leads to wrong columns being used in
9319 -- check messages, hence skip it in this mode.
9321 if not GNATprove_Mode
then
9322 Set_Paren_Count
(Right_Opnd
(Neg
), 1);
9325 if Scope
(Ne
) /= Standard_Standard
then
9326 Set_Entity
(Right_Opnd
(Neg
), Corresponding_Equality
(Ne
));
9329 -- For navigation purposes, we want to treat the inequality as an
9330 -- implicit reference to the corresponding equality. Preserve the
9331 -- Comes_From_ source flag to generate proper Xref entries.
9333 Preserve_Comes_From_Source
(Neg
, N
);
9334 Preserve_Comes_From_Source
(Right_Opnd
(Neg
), N
);
9336 Analyze_And_Resolve
(N
, Standard_Boolean
);
9340 -- No need for optimization in GNATprove mode, where we would rather see
9341 -- the original source expression.
9343 if not GNATprove_Mode
then
9344 Optimize_Length_Comparison
(N
);
9348 ---------------------
9349 -- Expand_N_Op_Not --
9350 ---------------------
9352 -- If the argument is other than a Boolean array type, there is no special
9353 -- expansion required, except for dealing with validity checks, and non-
9354 -- standard boolean representations.
9356 -- For the packed array case, we call the special routine in Exp_Pakd,
9357 -- except that if the component size is greater than one, we use the
9358 -- standard routine generating a gruesome loop (it is so peculiar to have
9359 -- packed arrays with non-standard Boolean representations anyway, so it
9360 -- does not matter that we do not handle this case efficiently).
9362 -- For the unpacked array case (and for the special packed case where we
9363 -- have non standard Booleans, as discussed above), we generate and insert
9364 -- into the tree the following function definition:
9366 -- function Nnnn (A : arr) is
9369 -- for J in a'range loop
9370 -- B (J) := not A (J);
9375 -- Here arr is the actual subtype of the parameter (and hence always
9376 -- constrained). Then we replace the not with a call to this function.
9378 procedure Expand_N_Op_Not
(N
: Node_Id
) is
9379 Loc
: constant Source_Ptr
:= Sloc
(N
);
9380 Typ
: constant Entity_Id
:= Etype
(N
);
9389 Func_Name
: Entity_Id
;
9390 Loop_Statement
: Node_Id
;
9393 Unary_Op_Validity_Checks
(N
);
9395 -- For boolean operand, deal with non-standard booleans
9397 if Is_Boolean_Type
(Typ
) then
9398 Adjust_Condition
(Right_Opnd
(N
));
9399 Set_Etype
(N
, Standard_Boolean
);
9400 Adjust_Result_Type
(N
, Typ
);
9404 -- Only array types need any other processing
9406 if not Is_Array_Type
(Typ
) then
9410 -- Case of array operand. If bit packed with a component size of 1,
9411 -- handle it in Exp_Pakd if the operand is known to be aligned.
9413 if Is_Bit_Packed_Array
(Typ
)
9414 and then Component_Size
(Typ
) = 1
9415 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
9417 Expand_Packed_Not
(N
);
9421 -- Case of array operand which is not bit-packed. If the context is
9422 -- a safe assignment, call in-place operation, If context is a larger
9423 -- boolean expression in the context of a safe assignment, expansion is
9424 -- done by enclosing operation.
9426 Opnd
:= Relocate_Node
(Right_Opnd
(N
));
9427 Convert_To_Actual_Subtype
(Opnd
);
9428 Arr
:= Etype
(Opnd
);
9429 Ensure_Defined
(Arr
, N
);
9430 Silly_Boolean_Array_Not_Test
(N
, Arr
);
9432 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
9433 if Safe_In_Place_Array_Op
(Name
(Parent
(N
)), N
, Empty
) then
9434 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
9437 -- Special case the negation of a binary operation
9439 elsif Nkind_In
(Opnd
, N_Op_And
, N_Op_Or
, N_Op_Xor
)
9440 and then Safe_In_Place_Array_Op
9441 (Name
(Parent
(N
)), Left_Opnd
(Opnd
), Right_Opnd
(Opnd
))
9443 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
9447 elsif Nkind
(Parent
(N
)) in N_Binary_Op
9448 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
9451 Op1
: constant Node_Id
:= Left_Opnd
(Parent
(N
));
9452 Op2
: constant Node_Id
:= Right_Opnd
(Parent
(N
));
9453 Lhs
: constant Node_Id
:= Name
(Parent
(Parent
(N
)));
9456 if Safe_In_Place_Array_Op
(Lhs
, Op1
, Op2
) then
9458 -- (not A) op (not B) can be reduced to a single call
9460 if N
= Op1
and then Nkind
(Op2
) = N_Op_Not
then
9463 elsif N
= Op2
and then Nkind
(Op1
) = N_Op_Not
then
9466 -- A xor (not B) can also be special-cased
9468 elsif N
= Op2
and then Nkind
(Parent
(N
)) = N_Op_Xor
then
9475 A
:= Make_Defining_Identifier
(Loc
, Name_uA
);
9476 B
:= Make_Defining_Identifier
(Loc
, Name_uB
);
9477 J
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
9480 Make_Indexed_Component
(Loc
,
9481 Prefix
=> New_Occurrence_Of
(A
, Loc
),
9482 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
9485 Make_Indexed_Component
(Loc
,
9486 Prefix
=> New_Occurrence_Of
(B
, Loc
),
9487 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
9490 Make_Implicit_Loop_Statement
(N
,
9491 Identifier
=> Empty
,
9494 Make_Iteration_Scheme
(Loc
,
9495 Loop_Parameter_Specification
=>
9496 Make_Loop_Parameter_Specification
(Loc
,
9497 Defining_Identifier
=> J
,
9498 Discrete_Subtype_Definition
=>
9499 Make_Attribute_Reference
(Loc
,
9500 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
9501 Attribute_Name
=> Name_Range
))),
9503 Statements
=> New_List
(
9504 Make_Assignment_Statement
(Loc
,
9506 Expression
=> Make_Op_Not
(Loc
, A_J
))));
9508 Func_Name
:= Make_Temporary
(Loc
, 'N');
9509 Set_Is_Inlined
(Func_Name
);
9512 Make_Subprogram_Body
(Loc
,
9514 Make_Function_Specification
(Loc
,
9515 Defining_Unit_Name
=> Func_Name
,
9516 Parameter_Specifications
=> New_List
(
9517 Make_Parameter_Specification
(Loc
,
9518 Defining_Identifier
=> A
,
9519 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
))),
9520 Result_Definition
=> New_Occurrence_Of
(Typ
, Loc
)),
9522 Declarations
=> New_List
(
9523 Make_Object_Declaration
(Loc
,
9524 Defining_Identifier
=> B
,
9525 Object_Definition
=> New_Occurrence_Of
(Arr
, Loc
))),
9527 Handled_Statement_Sequence
=>
9528 Make_Handled_Sequence_Of_Statements
(Loc
,
9529 Statements
=> New_List
(
9531 Make_Simple_Return_Statement
(Loc
,
9532 Expression
=> Make_Identifier
(Loc
, Chars
(B
)))))));
9535 Make_Function_Call
(Loc
,
9536 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
9537 Parameter_Associations
=> New_List
(Opnd
)));
9539 Analyze_And_Resolve
(N
, Typ
);
9540 end Expand_N_Op_Not
;
9542 --------------------
9543 -- Expand_N_Op_Or --
9544 --------------------
9546 procedure Expand_N_Op_Or
(N
: Node_Id
) is
9547 Typ
: constant Entity_Id
:= Etype
(N
);
9550 Binary_Op_Validity_Checks
(N
);
9552 if Is_Array_Type
(Etype
(N
)) then
9553 Expand_Boolean_Operator
(N
);
9555 elsif Is_Boolean_Type
(Etype
(N
)) then
9556 Adjust_Condition
(Left_Opnd
(N
));
9557 Adjust_Condition
(Right_Opnd
(N
));
9558 Set_Etype
(N
, Standard_Boolean
);
9559 Adjust_Result_Type
(N
, Typ
);
9561 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
9562 Expand_Intrinsic_Call
(N
, Entity
(N
));
9565 Expand_Nonbinary_Modular_Op
(N
);
9568 ----------------------
9569 -- Expand_N_Op_Plus --
9570 ----------------------
9572 procedure Expand_N_Op_Plus
(N
: Node_Id
) is
9574 Unary_Op_Validity_Checks
(N
);
9576 -- Check for MINIMIZED/ELIMINATED overflow mode
9578 if Minimized_Eliminated_Overflow_Check
(N
) then
9579 Apply_Arithmetic_Overflow_Check
(N
);
9582 end Expand_N_Op_Plus
;
9584 ---------------------
9585 -- Expand_N_Op_Rem --
9586 ---------------------
9588 procedure Expand_N_Op_Rem
(N
: Node_Id
) is
9589 Loc
: constant Source_Ptr
:= Sloc
(N
);
9590 Typ
: constant Entity_Id
:= Etype
(N
);
9601 -- Set if corresponding operand can be negative
9603 pragma Unreferenced
(Hi
);
9606 Binary_Op_Validity_Checks
(N
);
9608 -- Check for MINIMIZED/ELIMINATED overflow mode
9610 if Minimized_Eliminated_Overflow_Check
(N
) then
9611 Apply_Arithmetic_Overflow_Check
(N
);
9615 if Is_Integer_Type
(Etype
(N
)) then
9616 Apply_Divide_Checks
(N
);
9618 -- All done if we don't have a REM any more, which can happen as a
9619 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
9621 if Nkind
(N
) /= N_Op_Rem
then
9626 -- Proceed with expansion of REM
9628 Left
:= Left_Opnd
(N
);
9629 Right
:= Right_Opnd
(N
);
9631 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
9632 -- but it is useful with other back ends, and is certainly harmless.
9634 if Is_Integer_Type
(Etype
(N
))
9635 and then Compile_Time_Known_Value
(Right
)
9636 and then Expr_Value
(Right
) = Uint_1
9638 -- Call Remove_Side_Effects to ensure that any side effects in the
9639 -- ignored left operand (in particular function calls to user defined
9640 -- functions) are properly executed.
9642 Remove_Side_Effects
(Left
);
9644 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
9645 Analyze_And_Resolve
(N
, Typ
);
9649 -- Deal with annoying case of largest negative number remainder minus
9650 -- one. Gigi may not handle this case correctly, because on some
9651 -- targets, the mod value is computed using a divide instruction
9652 -- which gives an overflow trap for this case.
9654 -- It would be a bit more efficient to figure out which targets this
9655 -- is really needed for, but in practice it is reasonable to do the
9656 -- following special check in all cases, since it means we get a clearer
9657 -- message, and also the overhead is minimal given that division is
9658 -- expensive in any case.
9660 -- In fact the check is quite easy, if the right operand is -1, then
9661 -- the remainder is always 0, and we can just ignore the left operand
9662 -- completely in this case.
9664 Determine_Range
(Right
, OK
, Lo
, Hi
, Assume_Valid
=> True);
9665 Lneg
:= (not OK
) or else Lo
< 0;
9667 Determine_Range
(Left
, OK
, Lo
, Hi
, Assume_Valid
=> True);
9668 Rneg
:= (not OK
) or else Lo
< 0;
9670 -- We won't mess with trying to find out if the left operand can really
9671 -- be the largest negative number (that's a pain in the case of private
9672 -- types and this is really marginal). We will just assume that we need
9673 -- the test if the left operand can be negative at all.
9675 if Lneg
and Rneg
then
9677 Make_If_Expression
(Loc
,
9678 Expressions
=> New_List
(
9680 Left_Opnd
=> Duplicate_Subexpr
(Right
),
9682 Unchecked_Convert_To
(Typ
, Make_Integer_Literal
(Loc
, -1))),
9684 Unchecked_Convert_To
(Typ
,
9685 Make_Integer_Literal
(Loc
, Uint_0
)),
9687 Relocate_Node
(N
))));
9689 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
9690 Analyze_And_Resolve
(N
, Typ
);
9692 end Expand_N_Op_Rem
;
9694 -----------------------------
9695 -- Expand_N_Op_Rotate_Left --
9696 -----------------------------
9698 procedure Expand_N_Op_Rotate_Left
(N
: Node_Id
) is
9700 Binary_Op_Validity_Checks
(N
);
9702 -- If we are in Modify_Tree_For_C mode, there is no rotate left in C,
9703 -- so we rewrite in terms of logical shifts
9705 -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits)
9707 -- where Bits is the shift count mod Esize (the mod operation here
9708 -- deals with ludicrous large shift counts, which are apparently OK).
9710 -- What about nonbinary modulus ???
9713 Loc
: constant Source_Ptr
:= Sloc
(N
);
9714 Rtp
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
9715 Typ
: constant Entity_Id
:= Etype
(N
);
9718 if Modify_Tree_For_C
then
9719 Rewrite
(Right_Opnd
(N
),
9721 Left_Opnd
=> Relocate_Node
(Right_Opnd
(N
)),
9722 Right_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
))));
9724 Analyze_And_Resolve
(Right_Opnd
(N
), Rtp
);
9729 Make_Op_Shift_Left
(Loc
,
9730 Left_Opnd
=> Left_Opnd
(N
),
9731 Right_Opnd
=> Right_Opnd
(N
)),
9734 Make_Op_Shift_Right
(Loc
,
9735 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left_Opnd
(N
)),
9737 Make_Op_Subtract
(Loc
,
9738 Left_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
)),
9740 Duplicate_Subexpr_No_Checks
(Right_Opnd
(N
))))));
9742 Analyze_And_Resolve
(N
, Typ
);
9745 end Expand_N_Op_Rotate_Left
;
9747 ------------------------------
9748 -- Expand_N_Op_Rotate_Right --
9749 ------------------------------
9751 procedure Expand_N_Op_Rotate_Right
(N
: Node_Id
) is
9753 Binary_Op_Validity_Checks
(N
);
9755 -- If we are in Modify_Tree_For_C mode, there is no rotate right in C,
9756 -- so we rewrite in terms of logical shifts
9758 -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits)
9760 -- where Bits is the shift count mod Esize (the mod operation here
9761 -- deals with ludicrous large shift counts, which are apparently OK).
9763 -- What about nonbinary modulus ???
9766 Loc
: constant Source_Ptr
:= Sloc
(N
);
9767 Rtp
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
9768 Typ
: constant Entity_Id
:= Etype
(N
);
9771 Rewrite
(Right_Opnd
(N
),
9773 Left_Opnd
=> Relocate_Node
(Right_Opnd
(N
)),
9774 Right_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
))));
9776 Analyze_And_Resolve
(Right_Opnd
(N
), Rtp
);
9778 if Modify_Tree_For_C
then
9782 Make_Op_Shift_Right
(Loc
,
9783 Left_Opnd
=> Left_Opnd
(N
),
9784 Right_Opnd
=> Right_Opnd
(N
)),
9787 Make_Op_Shift_Left
(Loc
,
9788 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left_Opnd
(N
)),
9790 Make_Op_Subtract
(Loc
,
9791 Left_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
)),
9793 Duplicate_Subexpr_No_Checks
(Right_Opnd
(N
))))));
9795 Analyze_And_Resolve
(N
, Typ
);
9798 end Expand_N_Op_Rotate_Right
;
9800 ----------------------------
9801 -- Expand_N_Op_Shift_Left --
9802 ----------------------------
9804 -- Note: nothing in this routine depends on left as opposed to right shifts
9805 -- so we share the routine for expanding shift right operations.
9807 procedure Expand_N_Op_Shift_Left
(N
: Node_Id
) is
9809 Binary_Op_Validity_Checks
(N
);
9811 -- If we are in Modify_Tree_For_C mode, then ensure that the right
9812 -- operand is not greater than the word size (since that would not
9813 -- be defined properly by the corresponding C shift operator).
9815 if Modify_Tree_For_C
then
9817 Right
: constant Node_Id
:= Right_Opnd
(N
);
9818 Loc
: constant Source_Ptr
:= Sloc
(Right
);
9819 Typ
: constant Entity_Id
:= Etype
(N
);
9820 Siz
: constant Uint
:= Esize
(Typ
);
9827 if Compile_Time_Known_Value
(Right
) then
9828 if Expr_Value
(Right
) >= Siz
then
9829 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
9830 Analyze_And_Resolve
(N
, Typ
);
9833 -- Not compile time known, find range
9836 Determine_Range
(Right
, OK
, Lo
, Hi
, Assume_Valid
=> True);
9838 -- Nothing to do if known to be OK range, otherwise expand
9840 if not OK
or else Hi
>= Siz
then
9842 -- Prevent recursion on copy of shift node
9844 Orig
:= Relocate_Node
(N
);
9845 Set_Analyzed
(Orig
);
9847 -- Now do the rewrite
9850 Make_If_Expression
(Loc
,
9851 Expressions
=> New_List
(
9853 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Right
),
9854 Right_Opnd
=> Make_Integer_Literal
(Loc
, Siz
)),
9855 Make_Integer_Literal
(Loc
, 0),
9857 Analyze_And_Resolve
(N
, Typ
);
9862 end Expand_N_Op_Shift_Left
;
9864 -----------------------------
9865 -- Expand_N_Op_Shift_Right --
9866 -----------------------------
9868 procedure Expand_N_Op_Shift_Right
(N
: Node_Id
) is
9870 -- Share shift left circuit
9872 Expand_N_Op_Shift_Left
(N
);
9873 end Expand_N_Op_Shift_Right
;
9875 ----------------------------------------
9876 -- Expand_N_Op_Shift_Right_Arithmetic --
9877 ----------------------------------------
9879 procedure Expand_N_Op_Shift_Right_Arithmetic
(N
: Node_Id
) is
9881 Binary_Op_Validity_Checks
(N
);
9883 -- If we are in Modify_Tree_For_C mode, there is no shift right
9884 -- arithmetic in C, so we rewrite in terms of logical shifts.
9886 -- Shift_Right (Num, Bits) or
9888 -- then not (Shift_Right (Mask, bits))
9891 -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1)
9893 -- Note: in almost all C compilers it would work to just shift a
9894 -- signed integer right, but it's undefined and we cannot rely on it.
9896 -- Note: the above works fine for shift counts greater than or equal
9897 -- to the word size, since in this case (not (Shift_Right (Mask, bits)))
9898 -- generates all 1'bits.
9900 -- What about nonbinary modulus ???
9903 Loc
: constant Source_Ptr
:= Sloc
(N
);
9904 Typ
: constant Entity_Id
:= Etype
(N
);
9905 Sign
: constant Uint
:= 2 ** (Esize
(Typ
) - 1);
9906 Mask
: constant Uint
:= (2 ** Esize
(Typ
)) - 1;
9907 Left
: constant Node_Id
:= Left_Opnd
(N
);
9908 Right
: constant Node_Id
:= Right_Opnd
(N
);
9912 if Modify_Tree_For_C
then
9914 -- Here if not (Shift_Right (Mask, bits)) can be computed at
9915 -- compile time as a single constant.
9917 if Compile_Time_Known_Value
(Right
) then
9919 Val
: constant Uint
:= Expr_Value
(Right
);
9922 if Val
>= Esize
(Typ
) then
9923 Maskx
:= Make_Integer_Literal
(Loc
, Mask
);
9927 Make_Integer_Literal
(Loc
,
9928 Intval
=> Mask
- (Mask
/ (2 ** Expr_Value
(Right
))));
9936 Make_Op_Shift_Right
(Loc
,
9937 Left_Opnd
=> Make_Integer_Literal
(Loc
, Mask
),
9938 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Right
)));
9941 -- Now do the rewrite
9946 Make_Op_Shift_Right
(Loc
,
9948 Right_Opnd
=> Right
),
9950 Make_If_Expression
(Loc
,
9951 Expressions
=> New_List
(
9953 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left
),
9954 Right_Opnd
=> Make_Integer_Literal
(Loc
, Sign
)),
9956 Make_Integer_Literal
(Loc
, 0)))));
9957 Analyze_And_Resolve
(N
, Typ
);
9960 end Expand_N_Op_Shift_Right_Arithmetic
;
9962 --------------------------
9963 -- Expand_N_Op_Subtract --
9964 --------------------------
9966 procedure Expand_N_Op_Subtract
(N
: Node_Id
) is
9967 Typ
: constant Entity_Id
:= Etype
(N
);
9970 Binary_Op_Validity_Checks
(N
);
9972 -- Check for MINIMIZED/ELIMINATED overflow mode
9974 if Minimized_Eliminated_Overflow_Check
(N
) then
9975 Apply_Arithmetic_Overflow_Check
(N
);
9979 -- N - 0 = N for integer types
9981 if Is_Integer_Type
(Typ
)
9982 and then Compile_Time_Known_Value
(Right_Opnd
(N
))
9983 and then Expr_Value
(Right_Opnd
(N
)) = 0
9985 Rewrite
(N
, Left_Opnd
(N
));
9989 -- Arithmetic overflow checks for signed integer/fixed point types
9991 if Is_Signed_Integer_Type
(Typ
) or else Is_Fixed_Point_Type
(Typ
) then
9992 Apply_Arithmetic_Overflow_Check
(N
);
9995 -- Overflow checks for floating-point if -gnateF mode active
9997 Check_Float_Op_Overflow
(N
);
9999 Expand_Nonbinary_Modular_Op
(N
);
10000 end Expand_N_Op_Subtract
;
10002 ---------------------
10003 -- Expand_N_Op_Xor --
10004 ---------------------
10006 procedure Expand_N_Op_Xor
(N
: Node_Id
) is
10007 Typ
: constant Entity_Id
:= Etype
(N
);
10010 Binary_Op_Validity_Checks
(N
);
10012 if Is_Array_Type
(Etype
(N
)) then
10013 Expand_Boolean_Operator
(N
);
10015 elsif Is_Boolean_Type
(Etype
(N
)) then
10016 Adjust_Condition
(Left_Opnd
(N
));
10017 Adjust_Condition
(Right_Opnd
(N
));
10018 Set_Etype
(N
, Standard_Boolean
);
10019 Adjust_Result_Type
(N
, Typ
);
10021 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
10022 Expand_Intrinsic_Call
(N
, Entity
(N
));
10025 Expand_Nonbinary_Modular_Op
(N
);
10026 end Expand_N_Op_Xor
;
10028 ----------------------
10029 -- Expand_N_Or_Else --
10030 ----------------------
10032 procedure Expand_N_Or_Else
(N
: Node_Id
)
10033 renames Expand_Short_Circuit_Operator
;
10035 -----------------------------------
10036 -- Expand_N_Qualified_Expression --
10037 -----------------------------------
10039 procedure Expand_N_Qualified_Expression
(N
: Node_Id
) is
10040 Operand
: constant Node_Id
:= Expression
(N
);
10041 Target_Type
: constant Entity_Id
:= Entity
(Subtype_Mark
(N
));
10044 -- Do validity check if validity checking operands
10046 if Validity_Checks_On
and Validity_Check_Operands
then
10047 Ensure_Valid
(Operand
);
10050 -- Apply possible constraint check
10052 Apply_Constraint_Check
(Operand
, Target_Type
, No_Sliding
=> True);
10054 if Do_Range_Check
(Operand
) then
10055 Set_Do_Range_Check
(Operand
, False);
10056 Generate_Range_Check
(Operand
, Target_Type
, CE_Range_Check_Failed
);
10058 end Expand_N_Qualified_Expression
;
10060 ------------------------------------
10061 -- Expand_N_Quantified_Expression --
10062 ------------------------------------
10066 -- for all X in range => Cond
10071 -- for X in range loop
10072 -- if not Cond then
10078 -- Similarly, an existentially quantified expression:
10080 -- for some X in range => Cond
10085 -- for X in range loop
10092 -- In both cases, the iteration may be over a container in which case it is
10093 -- given by an iterator specification, not a loop parameter specification.
10095 procedure Expand_N_Quantified_Expression
(N
: Node_Id
) is
10096 Actions
: constant List_Id
:= New_List
;
10097 For_All
: constant Boolean := All_Present
(N
);
10098 Iter_Spec
: constant Node_Id
:= Iterator_Specification
(N
);
10099 Loc
: constant Source_Ptr
:= Sloc
(N
);
10100 Loop_Spec
: constant Node_Id
:= Loop_Parameter_Specification
(N
);
10107 -- Create the declaration of the flag which tracks the status of the
10108 -- quantified expression. Generate:
10110 -- Flag : Boolean := (True | False);
10112 Flag
:= Make_Temporary
(Loc
, 'T', N
);
10114 Append_To
(Actions
,
10115 Make_Object_Declaration
(Loc
,
10116 Defining_Identifier
=> Flag
,
10117 Object_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
),
10119 New_Occurrence_Of
(Boolean_Literals
(For_All
), Loc
)));
10121 -- Construct the circuitry which tracks the status of the quantified
10122 -- expression. Generate:
10124 -- if [not] Cond then
10125 -- Flag := (False | True);
10129 Cond
:= Relocate_Node
(Condition
(N
));
10132 Cond
:= Make_Op_Not
(Loc
, Cond
);
10135 Stmts
:= New_List
(
10136 Make_Implicit_If_Statement
(N
,
10138 Then_Statements
=> New_List
(
10139 Make_Assignment_Statement
(Loc
,
10140 Name
=> New_Occurrence_Of
(Flag
, Loc
),
10142 New_Occurrence_Of
(Boolean_Literals
(not For_All
), Loc
)),
10143 Make_Exit_Statement
(Loc
))));
10145 -- Build the loop equivalent of the quantified expression
10147 if Present
(Iter_Spec
) then
10149 Make_Iteration_Scheme
(Loc
,
10150 Iterator_Specification
=> Iter_Spec
);
10153 Make_Iteration_Scheme
(Loc
,
10154 Loop_Parameter_Specification
=> Loop_Spec
);
10157 Append_To
(Actions
,
10158 Make_Loop_Statement
(Loc
,
10159 Iteration_Scheme
=> Scheme
,
10160 Statements
=> Stmts
,
10161 End_Label
=> Empty
));
10163 -- Transform the quantified expression
10166 Make_Expression_With_Actions
(Loc
,
10167 Expression
=> New_Occurrence_Of
(Flag
, Loc
),
10168 Actions
=> Actions
));
10169 Analyze_And_Resolve
(N
, Standard_Boolean
);
10170 end Expand_N_Quantified_Expression
;
10172 ---------------------------------
10173 -- Expand_N_Selected_Component --
10174 ---------------------------------
10176 procedure Expand_N_Selected_Component
(N
: Node_Id
) is
10177 Loc
: constant Source_Ptr
:= Sloc
(N
);
10178 Par
: constant Node_Id
:= Parent
(N
);
10179 P
: constant Node_Id
:= Prefix
(N
);
10180 S
: constant Node_Id
:= Selector_Name
(N
);
10181 Ptyp
: Entity_Id
:= Underlying_Type
(Etype
(P
));
10187 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean;
10188 -- Gigi needs a temporary for prefixes that depend on a discriminant,
10189 -- unless the context of an assignment can provide size information.
10190 -- Don't we have a general routine that does this???
10192 function Is_Subtype_Declaration
return Boolean;
10193 -- The replacement of a discriminant reference by its value is required
10194 -- if this is part of the initialization of an temporary generated by a
10195 -- change of representation. This shows up as the construction of a
10196 -- discriminant constraint for a subtype declared at the same point as
10197 -- the entity in the prefix of the selected component. We recognize this
10198 -- case when the context of the reference is:
10199 -- subtype ST is T(Obj.D);
10200 -- where the entity for Obj comes from source, and ST has the same sloc.
10202 -----------------------
10203 -- In_Left_Hand_Side --
10204 -----------------------
10206 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean is
10208 return (Nkind
(Parent
(Comp
)) = N_Assignment_Statement
10209 and then Comp
= Name
(Parent
(Comp
)))
10210 or else (Present
(Parent
(Comp
))
10211 and then Nkind
(Parent
(Comp
)) in N_Subexpr
10212 and then In_Left_Hand_Side
(Parent
(Comp
)));
10213 end In_Left_Hand_Side
;
10215 -----------------------------
10216 -- Is_Subtype_Declaration --
10217 -----------------------------
10219 function Is_Subtype_Declaration
return Boolean is
10220 Par
: constant Node_Id
:= Parent
(N
);
10223 Nkind
(Par
) = N_Index_Or_Discriminant_Constraint
10224 and then Nkind
(Parent
(Parent
(Par
))) = N_Subtype_Declaration
10225 and then Comes_From_Source
(Entity
(Prefix
(N
)))
10226 and then Sloc
(Par
) = Sloc
(Entity
(Prefix
(N
)));
10227 end Is_Subtype_Declaration
;
10229 -- Start of processing for Expand_N_Selected_Component
10232 -- Insert explicit dereference if required
10234 if Is_Access_Type
(Ptyp
) then
10236 -- First set prefix type to proper access type, in case it currently
10237 -- has a private (non-access) view of this type.
10239 Set_Etype
(P
, Ptyp
);
10241 Insert_Explicit_Dereference
(P
);
10242 Analyze_And_Resolve
(P
, Designated_Type
(Ptyp
));
10244 if Ekind
(Etype
(P
)) = E_Private_Subtype
10245 and then Is_For_Access_Subtype
(Etype
(P
))
10247 Set_Etype
(P
, Base_Type
(Etype
(P
)));
10253 -- Deal with discriminant check required
10255 if Do_Discriminant_Check
(N
) then
10256 if Present
(Discriminant_Checking_Func
10257 (Original_Record_Component
(Entity
(S
))))
10259 -- Present the discriminant checking function to the backend, so
10260 -- that it can inline the call to the function.
10263 (Discriminant_Checking_Func
10264 (Original_Record_Component
(Entity
(S
))),
10267 -- Now reset the flag and generate the call
10269 Set_Do_Discriminant_Check
(N
, False);
10270 Generate_Discriminant_Check
(N
);
10272 -- In the case of Unchecked_Union, no discriminant checking is
10273 -- actually performed.
10276 Set_Do_Discriminant_Check
(N
, False);
10280 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
10281 -- function, then additional actuals must be passed.
10283 if Is_Build_In_Place_Function_Call
(P
) then
10284 Make_Build_In_Place_Call_In_Anonymous_Context
(P
);
10286 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
10287 -- containing build-in-place function calls whose returned object covers
10288 -- interface types.
10290 elsif Present
(Unqual_BIP_Iface_Function_Call
(P
)) then
10291 Make_Build_In_Place_Iface_Call_In_Anonymous_Context
(P
);
10294 -- Gigi cannot handle unchecked conversions that are the prefix of a
10295 -- selected component with discriminants. This must be checked during
10296 -- expansion, because during analysis the type of the selector is not
10297 -- known at the point the prefix is analyzed. If the conversion is the
10298 -- target of an assignment, then we cannot force the evaluation.
10300 if Nkind
(Prefix
(N
)) = N_Unchecked_Type_Conversion
10301 and then Has_Discriminants
(Etype
(N
))
10302 and then not In_Left_Hand_Side
(N
)
10304 Force_Evaluation
(Prefix
(N
));
10307 -- Remaining processing applies only if selector is a discriminant
10309 if Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
then
10311 -- If the selector is a discriminant of a constrained record type,
10312 -- we may be able to rewrite the expression with the actual value
10313 -- of the discriminant, a useful optimization in some cases.
10315 if Is_Record_Type
(Ptyp
)
10316 and then Has_Discriminants
(Ptyp
)
10317 and then Is_Constrained
(Ptyp
)
10319 -- Do this optimization for discrete types only, and not for
10320 -- access types (access discriminants get us into trouble).
10322 if not Is_Discrete_Type
(Etype
(N
)) then
10325 -- Don't do this on the left-hand side of an assignment statement.
10326 -- Normally one would think that references like this would not
10327 -- occur, but they do in generated code, and mean that we really
10328 -- do want to assign the discriminant.
10330 elsif Nkind
(Par
) = N_Assignment_Statement
10331 and then Name
(Par
) = N
10335 -- Don't do this optimization for the prefix of an attribute or
10336 -- the name of an object renaming declaration since these are
10337 -- contexts where we do not want the value anyway.
10339 elsif (Nkind
(Par
) = N_Attribute_Reference
10340 and then Prefix
(Par
) = N
)
10341 or else Is_Renamed_Object
(N
)
10345 -- Don't do this optimization if we are within the code for a
10346 -- discriminant check, since the whole point of such a check may
10347 -- be to verify the condition on which the code below depends.
10349 elsif Is_In_Discriminant_Check
(N
) then
10352 -- Green light to see if we can do the optimization. There is
10353 -- still one condition that inhibits the optimization below but
10354 -- now is the time to check the particular discriminant.
10357 -- Loop through discriminants to find the matching discriminant
10358 -- constraint to see if we can copy it.
10360 Disc
:= First_Discriminant
(Ptyp
);
10361 Dcon
:= First_Elmt
(Discriminant_Constraint
(Ptyp
));
10362 Discr_Loop
: while Present
(Dcon
) loop
10363 Dval
:= Node
(Dcon
);
10365 -- Check if this is the matching discriminant and if the
10366 -- discriminant value is simple enough to make sense to
10367 -- copy. We don't want to copy complex expressions, and
10368 -- indeed to do so can cause trouble (before we put in
10369 -- this guard, a discriminant expression containing an
10370 -- AND THEN was copied, causing problems for coverage
10371 -- analysis tools).
10373 -- However, if the reference is part of the initialization
10374 -- code generated for an object declaration, we must use
10375 -- the discriminant value from the subtype constraint,
10376 -- because the selected component may be a reference to the
10377 -- object being initialized, whose discriminant is not yet
10378 -- set. This only happens in complex cases involving changes
10379 -- or representation.
10381 if Disc
= Entity
(Selector_Name
(N
))
10382 and then (Is_Entity_Name
(Dval
)
10383 or else Compile_Time_Known_Value
(Dval
)
10384 or else Is_Subtype_Declaration
)
10386 -- Here we have the matching discriminant. Check for
10387 -- the case of a discriminant of a component that is
10388 -- constrained by an outer discriminant, which cannot
10389 -- be optimized away.
10391 if Denotes_Discriminant
10392 (Dval
, Check_Concurrent
=> True)
10396 elsif Nkind
(Original_Node
(Dval
)) = N_Selected_Component
10398 Denotes_Discriminant
10399 (Selector_Name
(Original_Node
(Dval
)), True)
10403 -- Do not retrieve value if constraint is not static. It
10404 -- is generally not useful, and the constraint may be a
10405 -- rewritten outer discriminant in which case it is in
10408 elsif Is_Entity_Name
(Dval
)
10410 Nkind
(Parent
(Entity
(Dval
))) = N_Object_Declaration
10411 and then Present
(Expression
(Parent
(Entity
(Dval
))))
10413 Is_OK_Static_Expression
10414 (Expression
(Parent
(Entity
(Dval
))))
10418 -- In the context of a case statement, the expression may
10419 -- have the base type of the discriminant, and we need to
10420 -- preserve the constraint to avoid spurious errors on
10423 elsif Nkind
(Parent
(N
)) = N_Case_Statement
10424 and then Etype
(Dval
) /= Etype
(Disc
)
10427 Make_Qualified_Expression
(Loc
,
10429 New_Occurrence_Of
(Etype
(Disc
), Loc
),
10431 New_Copy_Tree
(Dval
)));
10432 Analyze_And_Resolve
(N
, Etype
(Disc
));
10434 -- In case that comes out as a static expression,
10435 -- reset it (a selected component is never static).
10437 Set_Is_Static_Expression
(N
, False);
10440 -- Otherwise we can just copy the constraint, but the
10441 -- result is certainly not static. In some cases the
10442 -- discriminant constraint has been analyzed in the
10443 -- context of the original subtype indication, but for
10444 -- itypes the constraint might not have been analyzed
10445 -- yet, and this must be done now.
10448 Rewrite
(N
, New_Copy_Tree
(Dval
));
10449 Analyze_And_Resolve
(N
);
10450 Set_Is_Static_Expression
(N
, False);
10456 Next_Discriminant
(Disc
);
10457 end loop Discr_Loop
;
10459 -- Note: the above loop should always find a matching
10460 -- discriminant, but if it does not, we just missed an
10461 -- optimization due to some glitch (perhaps a previous
10462 -- error), so ignore.
10467 -- The only remaining processing is in the case of a discriminant of
10468 -- a concurrent object, where we rewrite the prefix to denote the
10469 -- corresponding record type. If the type is derived and has renamed
10470 -- discriminants, use corresponding discriminant, which is the one
10471 -- that appears in the corresponding record.
10473 if not Is_Concurrent_Type
(Ptyp
) then
10477 Disc
:= Entity
(Selector_Name
(N
));
10479 if Is_Derived_Type
(Ptyp
)
10480 and then Present
(Corresponding_Discriminant
(Disc
))
10482 Disc
:= Corresponding_Discriminant
(Disc
);
10486 Make_Selected_Component
(Loc
,
10488 Unchecked_Convert_To
(Corresponding_Record_Type
(Ptyp
),
10489 New_Copy_Tree
(P
)),
10490 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Disc
)));
10492 Rewrite
(N
, New_N
);
10496 -- Set Atomic_Sync_Required if necessary for atomic component
10498 if Nkind
(N
) = N_Selected_Component
then
10500 E
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
10504 -- If component is atomic, but type is not, setting depends on
10505 -- disable/enable state for the component.
10507 if Is_Atomic
(E
) and then not Is_Atomic
(Etype
(E
)) then
10508 Set
:= not Atomic_Synchronization_Disabled
(E
);
10510 -- If component is not atomic, but its type is atomic, setting
10511 -- depends on disable/enable state for the type.
10513 elsif not Is_Atomic
(E
) and then Is_Atomic
(Etype
(E
)) then
10514 Set
:= not Atomic_Synchronization_Disabled
(Etype
(E
));
10516 -- If both component and type are atomic, we disable if either
10517 -- component or its type have sync disabled.
10519 elsif Is_Atomic
(E
) and then Is_Atomic
(Etype
(E
)) then
10520 Set
:= (not Atomic_Synchronization_Disabled
(E
))
10522 (not Atomic_Synchronization_Disabled
(Etype
(E
)));
10528 -- Set flag if required
10531 Activate_Atomic_Synchronization
(N
);
10535 end Expand_N_Selected_Component
;
10537 --------------------
10538 -- Expand_N_Slice --
10539 --------------------
10541 procedure Expand_N_Slice
(N
: Node_Id
) is
10542 Loc
: constant Source_Ptr
:= Sloc
(N
);
10543 Typ
: constant Entity_Id
:= Etype
(N
);
10545 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean;
10546 -- Check whether the argument is an actual for a procedure call, in
10547 -- which case the expansion of a bit-packed slice is deferred until the
10548 -- call itself is expanded. The reason this is required is that we might
10549 -- have an IN OUT or OUT parameter, and the copy out is essential, and
10550 -- that copy out would be missed if we created a temporary here in
10551 -- Expand_N_Slice. Note that we don't bother to test specifically for an
10552 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
10553 -- is harmless to defer expansion in the IN case, since the call
10554 -- processing will still generate the appropriate copy in operation,
10555 -- which will take care of the slice.
10557 procedure Make_Temporary_For_Slice
;
10558 -- Create a named variable for the value of the slice, in cases where
10559 -- the back end cannot handle it properly, e.g. when packed types or
10560 -- unaligned slices are involved.
10562 -------------------------
10563 -- Is_Procedure_Actual --
10564 -------------------------
10566 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean is
10567 Par
: Node_Id
:= Parent
(N
);
10571 -- If our parent is a procedure call we can return
10573 if Nkind
(Par
) = N_Procedure_Call_Statement
then
10576 -- If our parent is a type conversion, keep climbing the tree,
10577 -- since a type conversion can be a procedure actual. Also keep
10578 -- climbing if parameter association or a qualified expression,
10579 -- since these are additional cases that do can appear on
10580 -- procedure actuals.
10582 elsif Nkind_In
(Par
, N_Type_Conversion
,
10583 N_Parameter_Association
,
10584 N_Qualified_Expression
)
10586 Par
:= Parent
(Par
);
10588 -- Any other case is not what we are looking for
10594 end Is_Procedure_Actual
;
10596 ------------------------------
10597 -- Make_Temporary_For_Slice --
10598 ------------------------------
10600 procedure Make_Temporary_For_Slice
is
10601 Ent
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', N
);
10606 Make_Object_Declaration
(Loc
,
10607 Defining_Identifier
=> Ent
,
10608 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
10610 Set_No_Initialization
(Decl
);
10612 Insert_Actions
(N
, New_List
(
10614 Make_Assignment_Statement
(Loc
,
10615 Name
=> New_Occurrence_Of
(Ent
, Loc
),
10616 Expression
=> Relocate_Node
(N
))));
10618 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
10619 Analyze_And_Resolve
(N
, Typ
);
10620 end Make_Temporary_For_Slice
;
10624 Pref
: constant Node_Id
:= Prefix
(N
);
10625 Pref_Typ
: Entity_Id
:= Etype
(Pref
);
10627 -- Start of processing for Expand_N_Slice
10630 -- Special handling for access types
10632 if Is_Access_Type
(Pref_Typ
) then
10633 Pref_Typ
:= Designated_Type
(Pref_Typ
);
10636 Make_Explicit_Dereference
(Sloc
(N
),
10637 Prefix
=> Relocate_Node
(Pref
)));
10639 Analyze_And_Resolve
(Pref
, Pref_Typ
);
10642 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
10643 -- function, then additional actuals must be passed.
10645 if Is_Build_In_Place_Function_Call
(Pref
) then
10646 Make_Build_In_Place_Call_In_Anonymous_Context
(Pref
);
10648 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
10649 -- containing build-in-place function calls whose returned object covers
10650 -- interface types.
10652 elsif Present
(Unqual_BIP_Iface_Function_Call
(Pref
)) then
10653 Make_Build_In_Place_Iface_Call_In_Anonymous_Context
(Pref
);
10656 -- The remaining case to be handled is packed slices. We can leave
10657 -- packed slices as they are in the following situations:
10659 -- 1. Right or left side of an assignment (we can handle this
10660 -- situation correctly in the assignment statement expansion).
10662 -- 2. Prefix of indexed component (the slide is optimized away in this
10663 -- case, see the start of Expand_N_Slice.)
10665 -- 3. Object renaming declaration, since we want the name of the
10666 -- slice, not the value.
10668 -- 4. Argument to procedure call, since copy-in/copy-out handling may
10669 -- be required, and this is handled in the expansion of call
10672 -- 5. Prefix of an address attribute (this is an error which is caught
10673 -- elsewhere, and the expansion would interfere with generating the
10676 if not Is_Packed
(Typ
) then
10678 -- Apply transformation for actuals of a function call, where
10679 -- Expand_Actuals is not used.
10681 if Nkind
(Parent
(N
)) = N_Function_Call
10682 and then Is_Possibly_Unaligned_Slice
(N
)
10684 Make_Temporary_For_Slice
;
10687 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
10688 or else (Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
10689 and then Parent
(N
) = Name
(Parent
(Parent
(N
))))
10693 elsif Nkind
(Parent
(N
)) = N_Indexed_Component
10694 or else Is_Renamed_Object
(N
)
10695 or else Is_Procedure_Actual
(N
)
10699 elsif Nkind
(Parent
(N
)) = N_Attribute_Reference
10700 and then Attribute_Name
(Parent
(N
)) = Name_Address
10705 Make_Temporary_For_Slice
;
10707 end Expand_N_Slice
;
10709 ------------------------------
10710 -- Expand_N_Type_Conversion --
10711 ------------------------------
10713 procedure Expand_N_Type_Conversion
(N
: Node_Id
) is
10714 Loc
: constant Source_Ptr
:= Sloc
(N
);
10715 Operand
: constant Node_Id
:= Expression
(N
);
10716 Target_Type
: constant Entity_Id
:= Etype
(N
);
10717 Operand_Type
: Entity_Id
:= Etype
(Operand
);
10719 procedure Handle_Changed_Representation
;
10720 -- This is called in the case of record and array type conversions to
10721 -- see if there is a change of representation to be handled. Change of
10722 -- representation is actually handled at the assignment statement level,
10723 -- and what this procedure does is rewrite node N conversion as an
10724 -- assignment to temporary. If there is no change of representation,
10725 -- then the conversion node is unchanged.
10727 procedure Raise_Accessibility_Error
;
10728 -- Called when we know that an accessibility check will fail. Rewrites
10729 -- node N to an appropriate raise statement and outputs warning msgs.
10730 -- The Etype of the raise node is set to Target_Type. Note that in this
10731 -- case the rest of the processing should be skipped (i.e. the call to
10732 -- this procedure will be followed by "goto Done").
10734 procedure Real_Range_Check
;
10735 -- Handles generation of range check for real target value
10737 function Has_Extra_Accessibility
(Id
: Entity_Id
) return Boolean;
10738 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
10739 -- evaluates to True.
10741 -----------------------------------
10742 -- Handle_Changed_Representation --
10743 -----------------------------------
10745 procedure Handle_Changed_Representation
is
10753 -- Nothing else to do if no change of representation
10755 if Same_Representation
(Operand_Type
, Target_Type
) then
10758 -- The real change of representation work is done by the assignment
10759 -- statement processing. So if this type conversion is appearing as
10760 -- the expression of an assignment statement, nothing needs to be
10761 -- done to the conversion.
10763 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
then
10766 -- Otherwise we need to generate a temporary variable, and do the
10767 -- change of representation assignment into that temporary variable.
10768 -- The conversion is then replaced by a reference to this variable.
10773 -- If type is unconstrained we have to add a constraint, copied
10774 -- from the actual value of the left-hand side.
10776 if not Is_Constrained
(Target_Type
) then
10777 if Has_Discriminants
(Operand_Type
) then
10779 -- A change of representation can only apply to untagged
10780 -- types. We need to build the constraint that applies to
10781 -- the target type, using the constraints of the operand.
10782 -- The analysis is complicated if there are both inherited
10783 -- discriminants and constrained discriminants.
10784 -- We iterate over the discriminants of the target, and
10785 -- find the discriminant of the same name:
10787 -- a) If there is a corresponding discriminant in the object
10788 -- then the value is a selected component of the operand.
10790 -- b) Otherwise the value of a constrained discriminant is
10791 -- found in the stored constraint of the operand.
10794 Stored
: constant Elist_Id
:=
10795 Stored_Constraint
(Operand_Type
);
10799 Disc_O
: Entity_Id
;
10800 -- Discriminant of the operand type. Its value in the
10801 -- object is captured in a selected component.
10803 Disc_S
: Entity_Id
;
10804 -- Stored discriminant of the operand. If present, it
10805 -- corresponds to a constrained discriminant of the
10808 Disc_T
: Entity_Id
;
10809 -- Discriminant of the target type
10812 Disc_T
:= First_Discriminant
(Target_Type
);
10813 Disc_O
:= First_Discriminant
(Operand_Type
);
10814 Disc_S
:= First_Stored_Discriminant
(Operand_Type
);
10816 if Present
(Stored
) then
10817 Elmt
:= First_Elmt
(Stored
);
10819 Elmt
:= No_Elmt
; -- init to avoid warning
10823 while Present
(Disc_T
) loop
10824 if Present
(Disc_O
)
10825 and then Chars
(Disc_T
) = Chars
(Disc_O
)
10828 Make_Selected_Component
(Loc
,
10830 Duplicate_Subexpr_Move_Checks
(Operand
),
10832 Make_Identifier
(Loc
, Chars
(Disc_O
))));
10833 Next_Discriminant
(Disc_O
);
10835 elsif Present
(Disc_S
) then
10836 Append_To
(Cons
, New_Copy_Tree
(Node
(Elmt
)));
10840 Next_Discriminant
(Disc_T
);
10844 elsif Is_Array_Type
(Operand_Type
) then
10845 N_Ix
:= First_Index
(Target_Type
);
10848 for J
in 1 .. Number_Dimensions
(Operand_Type
) loop
10850 -- We convert the bounds explicitly. We use an unchecked
10851 -- conversion because bounds checks are done elsewhere.
10856 Unchecked_Convert_To
(Etype
(N_Ix
),
10857 Make_Attribute_Reference
(Loc
,
10859 Duplicate_Subexpr_No_Checks
10860 (Operand
, Name_Req
=> True),
10861 Attribute_Name
=> Name_First
,
10862 Expressions
=> New_List
(
10863 Make_Integer_Literal
(Loc
, J
)))),
10866 Unchecked_Convert_To
(Etype
(N_Ix
),
10867 Make_Attribute_Reference
(Loc
,
10869 Duplicate_Subexpr_No_Checks
10870 (Operand
, Name_Req
=> True),
10871 Attribute_Name
=> Name_Last
,
10872 Expressions
=> New_List
(
10873 Make_Integer_Literal
(Loc
, J
))))));
10880 Odef
:= New_Occurrence_Of
(Target_Type
, Loc
);
10882 if Present
(Cons
) then
10884 Make_Subtype_Indication
(Loc
,
10885 Subtype_Mark
=> Odef
,
10887 Make_Index_Or_Discriminant_Constraint
(Loc
,
10888 Constraints
=> Cons
));
10891 Temp
:= Make_Temporary
(Loc
, 'C');
10893 Make_Object_Declaration
(Loc
,
10894 Defining_Identifier
=> Temp
,
10895 Object_Definition
=> Odef
);
10897 Set_No_Initialization
(Decl
, True);
10899 -- Insert required actions. It is essential to suppress checks
10900 -- since we have suppressed default initialization, which means
10901 -- that the variable we create may have no discriminants.
10906 Make_Assignment_Statement
(Loc
,
10907 Name
=> New_Occurrence_Of
(Temp
, Loc
),
10908 Expression
=> Relocate_Node
(N
))),
10909 Suppress
=> All_Checks
);
10911 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
10914 end Handle_Changed_Representation
;
10916 -------------------------------
10917 -- Raise_Accessibility_Error --
10918 -------------------------------
10920 procedure Raise_Accessibility_Error
is
10922 Error_Msg_Warn
:= SPARK_Mode
/= On
;
10924 Make_Raise_Program_Error
(Sloc
(N
),
10925 Reason
=> PE_Accessibility_Check_Failed
));
10926 Set_Etype
(N
, Target_Type
);
10928 Error_Msg_N
("<<accessibility check failure", N
);
10929 Error_Msg_NE
("\<<& [", N
, Standard_Program_Error
);
10930 end Raise_Accessibility_Error
;
10932 ----------------------
10933 -- Real_Range_Check --
10934 ----------------------
10936 -- Case of conversions to floating-point or fixed-point. If range checks
10937 -- are enabled and the target type has a range constraint, we convert:
10943 -- Tnn : typ'Base := typ'Base (x);
10944 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
10947 -- This is necessary when there is a conversion of integer to float or
10948 -- to fixed-point to ensure that the correct checks are made. It is not
10949 -- necessary for float to float where it is enough to simply set the
10950 -- Do_Range_Check flag.
10952 procedure Real_Range_Check
is
10953 Btyp
: constant Entity_Id
:= Base_Type
(Target_Type
);
10954 Lo
: constant Node_Id
:= Type_Low_Bound
(Target_Type
);
10955 Hi
: constant Node_Id
:= Type_High_Bound
(Target_Type
);
10956 Xtyp
: constant Entity_Id
:= Etype
(Operand
);
10966 -- Nothing to do if conversion was rewritten
10968 if Nkind
(N
) /= N_Type_Conversion
then
10972 -- Nothing to do if range checks suppressed, or target has the same
10973 -- range as the base type (or is the base type).
10975 if Range_Checks_Suppressed
(Target_Type
)
10976 or else (Lo
= Type_Low_Bound
(Btyp
)
10978 Hi
= Type_High_Bound
(Btyp
))
10983 -- Nothing to do if expression is an entity on which checks have been
10986 if Is_Entity_Name
(Operand
)
10987 and then Range_Checks_Suppressed
(Entity
(Operand
))
10992 -- Nothing to do if bounds are all static and we can tell that the
10993 -- expression is within the bounds of the target. Note that if the
10994 -- operand is of an unconstrained floating-point type, then we do
10995 -- not trust it to be in range (might be infinite)
10998 S_Lo
: constant Node_Id
:= Type_Low_Bound
(Xtyp
);
10999 S_Hi
: constant Node_Id
:= Type_High_Bound
(Xtyp
);
11002 if (not Is_Floating_Point_Type
(Xtyp
)
11003 or else Is_Constrained
(Xtyp
))
11004 and then Compile_Time_Known_Value
(S_Lo
)
11005 and then Compile_Time_Known_Value
(S_Hi
)
11006 and then Compile_Time_Known_Value
(Hi
)
11007 and then Compile_Time_Known_Value
(Lo
)
11010 D_Lov
: constant Ureal
:= Expr_Value_R
(Lo
);
11011 D_Hiv
: constant Ureal
:= Expr_Value_R
(Hi
);
11016 if Is_Real_Type
(Xtyp
) then
11017 S_Lov
:= Expr_Value_R
(S_Lo
);
11018 S_Hiv
:= Expr_Value_R
(S_Hi
);
11020 S_Lov
:= UR_From_Uint
(Expr_Value
(S_Lo
));
11021 S_Hiv
:= UR_From_Uint
(Expr_Value
(S_Hi
));
11025 and then S_Lov
>= D_Lov
11026 and then S_Hiv
<= D_Hiv
11028 -- Unset the range check flag on the current value of
11029 -- Expression (N), since the captured Operand may have
11030 -- been rewritten (such as for the case of a conversion
11031 -- to a fixed-point type).
11033 Set_Do_Range_Check
(Expression
(N
), False);
11041 -- For float to float conversions, we are done
11043 if Is_Floating_Point_Type
(Xtyp
)
11045 Is_Floating_Point_Type
(Btyp
)
11050 -- Otherwise rewrite the conversion as described above
11052 Conv
:= Relocate_Node
(N
);
11053 Rewrite
(Subtype_Mark
(Conv
), New_Occurrence_Of
(Btyp
, Loc
));
11054 Set_Etype
(Conv
, Btyp
);
11056 -- Enable overflow except for case of integer to float conversions,
11057 -- where it is never required, since we can never have overflow in
11060 if not Is_Integer_Type
(Etype
(Operand
)) then
11061 Enable_Overflow_Check
(Conv
);
11064 Tnn
:= Make_Temporary
(Loc
, 'T', Conv
);
11066 -- For a conversion from Float to Fixed where the bounds of the
11067 -- fixed-point type are static, we can obtain a more accurate
11068 -- fixed-point value by converting the result of the floating-
11069 -- point expression to an appropriate integer type, and then
11070 -- performing an unchecked conversion to the target fixed-point
11071 -- type. The range check can then use the corresponding integer
11072 -- value of the bounds instead of requiring further conversions.
11073 -- This preserves the identity:
11075 -- Fix_Val = Fixed_Type (Float_Type (Fix_Val))
11077 -- which used to fail when Fix_Val was a bound of the type and
11078 -- the 'Small was not a representable number.
11079 -- This transformation requires an integer type large enough to
11080 -- accommodate a fixed-point value. This will not be the case
11081 -- in systems where Duration is larger than Long_Integer.
11083 if Is_Ordinary_Fixed_Point_Type
(Target_Type
)
11084 and then Is_Floating_Point_Type
(Operand_Type
)
11085 and then RM_Size
(Base_Type
(Target_Type
)) <=
11086 RM_Size
(Standard_Long_Integer
)
11087 and then Nkind
(Lo
) = N_Real_Literal
11088 and then Nkind
(Hi
) = N_Real_Literal
11090 -- Find the integer type of the right size to perform an unchecked
11091 -- conversion to the target fixed-point type.
11094 Bfx_Type
: constant Entity_Id
:= Base_Type
(Target_Type
);
11095 Expr_Id
: constant Entity_Id
:=
11096 Make_Temporary
(Loc
, 'T', Conv
);
11097 Int_Type
: Entity_Id
;
11100 if RM_Size
(Bfx_Type
) > RM_Size
(Standard_Integer
) then
11101 Int_Type
:= Standard_Long_Integer
;
11103 elsif RM_Size
(Bfx_Type
) > RM_Size
(Standard_Short_Integer
) then
11104 Int_Type
:= Standard_Integer
;
11107 Int_Type
:= Standard_Short_Integer
;
11110 -- Generate a temporary with the integer value. Required in the
11111 -- CCG compiler to ensure that runtime checks reference this
11112 -- integer expression (instead of the resulting fixed-point
11113 -- value) because fixed-point values are handled by means of
11114 -- unsigned integer types).
11117 Make_Object_Declaration
(Loc
,
11118 Defining_Identifier
=> Expr_Id
,
11119 Object_Definition
=> New_Occurrence_Of
(Int_Type
, Loc
),
11120 Constant_Present
=> True,
11122 Convert_To
(Int_Type
, Expression
(Conv
))));
11124 -- Create integer objects for range checking of result.
11127 Unchecked_Convert_To
11128 (Int_Type
, New_Occurrence_Of
(Expr_Id
, Loc
));
11131 Make_Integer_Literal
(Loc
, Corresponding_Integer_Value
(Lo
));
11134 Unchecked_Convert_To
11135 (Int_Type
, New_Occurrence_Of
(Expr_Id
, Loc
));
11138 Make_Integer_Literal
(Loc
, Corresponding_Integer_Value
(Hi
));
11140 -- Rewrite conversion as an integer conversion of the
11141 -- original floating-point expression, followed by an
11142 -- unchecked conversion to the target fixed-point type.
11145 Make_Unchecked_Type_Conversion
(Loc
,
11146 Subtype_Mark
=> New_Occurrence_Of
(Target_Type
, Loc
),
11147 Expression
=> New_Occurrence_Of
(Expr_Id
, Loc
));
11150 -- All other conversions
11153 Lo_Arg
:= New_Occurrence_Of
(Tnn
, Loc
);
11155 Make_Attribute_Reference
(Loc
,
11156 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
11157 Attribute_Name
=> Name_First
);
11159 Hi_Arg
:= New_Occurrence_Of
(Tnn
, Loc
);
11161 Make_Attribute_Reference
(Loc
,
11162 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
11163 Attribute_Name
=> Name_Last
);
11166 -- Build code for range checking
11168 Insert_Actions
(N
, New_List
(
11169 Make_Object_Declaration
(Loc
,
11170 Defining_Identifier
=> Tnn
,
11171 Object_Definition
=> New_Occurrence_Of
(Btyp
, Loc
),
11172 Constant_Present
=> True,
11173 Expression
=> Conv
),
11175 Make_Raise_Constraint_Error
(Loc
,
11180 Left_Opnd
=> Lo_Arg
,
11181 Right_Opnd
=> Lo_Val
),
11185 Left_Opnd
=> Hi_Arg
,
11186 Right_Opnd
=> Hi_Val
)),
11187 Reason
=> CE_Range_Check_Failed
)));
11189 Rewrite
(N
, New_Occurrence_Of
(Tnn
, Loc
));
11190 Analyze_And_Resolve
(N
, Btyp
);
11191 end Real_Range_Check
;
11193 -----------------------------
11194 -- Has_Extra_Accessibility --
11195 -----------------------------
11197 -- Returns true for a formal of an anonymous access type or for an Ada
11198 -- 2012-style stand-alone object of an anonymous access type.
11200 function Has_Extra_Accessibility
(Id
: Entity_Id
) return Boolean is
11202 if Is_Formal
(Id
) or else Ekind_In
(Id
, E_Constant
, E_Variable
) then
11203 return Present
(Effective_Extra_Accessibility
(Id
));
11207 end Has_Extra_Accessibility
;
11209 -- Start of processing for Expand_N_Type_Conversion
11212 -- First remove check marks put by the semantic analysis on the type
11213 -- conversion between array types. We need these checks, and they will
11214 -- be generated by this expansion routine, but we do not depend on these
11215 -- flags being set, and since we do intend to expand the checks in the
11216 -- front end, we don't want them on the tree passed to the back end.
11218 if Is_Array_Type
(Target_Type
) then
11219 if Is_Constrained
(Target_Type
) then
11220 Set_Do_Length_Check
(N
, False);
11222 Set_Do_Range_Check
(Operand
, False);
11226 -- Nothing at all to do if conversion is to the identical type so remove
11227 -- the conversion completely, it is useless, except that it may carry
11228 -- an Assignment_OK attribute, which must be propagated to the operand.
11230 if Operand_Type
= Target_Type
then
11231 if Assignment_OK
(N
) then
11232 Set_Assignment_OK
(Operand
);
11235 Rewrite
(N
, Relocate_Node
(Operand
));
11239 -- Nothing to do if this is the second argument of read. This is a
11240 -- "backwards" conversion that will be handled by the specialized code
11241 -- in attribute processing.
11243 if Nkind
(Parent
(N
)) = N_Attribute_Reference
11244 and then Attribute_Name
(Parent
(N
)) = Name_Read
11245 and then Next
(First
(Expressions
(Parent
(N
)))) = N
11250 -- Check for case of converting to a type that has an invariant
11251 -- associated with it. This requires an invariant check. We insert
11254 -- invariant_check (typ (expr))
11256 -- in the code, after removing side effects from the expression.
11257 -- This is clearer than replacing the conversion into an expression
11258 -- with actions, because the context may impose additional actions
11259 -- (tag checks, membership tests, etc.) that conflict with this
11260 -- rewriting (used previously).
11262 -- Note: the Comes_From_Source check, and then the resetting of this
11263 -- flag prevents what would otherwise be an infinite recursion.
11265 if Has_Invariants
(Target_Type
)
11266 and then Present
(Invariant_Procedure
(Target_Type
))
11267 and then Comes_From_Source
(N
)
11269 Set_Comes_From_Source
(N
, False);
11270 Remove_Side_Effects
(N
);
11271 Insert_Action
(N
, Make_Invariant_Call
(Duplicate_Subexpr
(N
)));
11275 -- Here if we may need to expand conversion
11277 -- If the operand of the type conversion is an arithmetic operation on
11278 -- signed integers, and the based type of the signed integer type in
11279 -- question is smaller than Standard.Integer, we promote both of the
11280 -- operands to type Integer.
11282 -- For example, if we have
11284 -- target-type (opnd1 + opnd2)
11286 -- and opnd1 and opnd2 are of type short integer, then we rewrite
11289 -- target-type (integer(opnd1) + integer(opnd2))
11291 -- We do this because we are always allowed to compute in a larger type
11292 -- if we do the right thing with the result, and in this case we are
11293 -- going to do a conversion which will do an appropriate check to make
11294 -- sure that things are in range of the target type in any case. This
11295 -- avoids some unnecessary intermediate overflows.
11297 -- We might consider a similar transformation in the case where the
11298 -- target is a real type or a 64-bit integer type, and the operand
11299 -- is an arithmetic operation using a 32-bit integer type. However,
11300 -- we do not bother with this case, because it could cause significant
11301 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
11302 -- much cheaper, but we don't want different behavior on 32-bit and
11303 -- 64-bit machines. Note that the exclusion of the 64-bit case also
11304 -- handles the configurable run-time cases where 64-bit arithmetic
11305 -- may simply be unavailable.
11307 -- Note: this circuit is partially redundant with respect to the circuit
11308 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
11309 -- the processing here. Also we still need the Checks circuit, since we
11310 -- have to be sure not to generate junk overflow checks in the first
11311 -- place, since it would be trick to remove them here.
11313 if Integer_Promotion_Possible
(N
) then
11315 -- All conditions met, go ahead with transformation
11323 Make_Type_Conversion
(Loc
,
11324 Subtype_Mark
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
11325 Expression
=> Relocate_Node
(Right_Opnd
(Operand
)));
11327 Opnd
:= New_Op_Node
(Nkind
(Operand
), Loc
);
11328 Set_Right_Opnd
(Opnd
, R
);
11330 if Nkind
(Operand
) in N_Binary_Op
then
11332 Make_Type_Conversion
(Loc
,
11333 Subtype_Mark
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
11334 Expression
=> Relocate_Node
(Left_Opnd
(Operand
)));
11336 Set_Left_Opnd
(Opnd
, L
);
11340 Make_Type_Conversion
(Loc
,
11341 Subtype_Mark
=> Relocate_Node
(Subtype_Mark
(N
)),
11342 Expression
=> Opnd
));
11344 Analyze_And_Resolve
(N
, Target_Type
);
11349 -- Do validity check if validity checking operands
11351 if Validity_Checks_On
and Validity_Check_Operands
then
11352 Ensure_Valid
(Operand
);
11355 -- Special case of converting from non-standard boolean type
11357 if Is_Boolean_Type
(Operand_Type
)
11358 and then (Nonzero_Is_True
(Operand_Type
))
11360 Adjust_Condition
(Operand
);
11361 Set_Etype
(Operand
, Standard_Boolean
);
11362 Operand_Type
:= Standard_Boolean
;
11365 -- Case of converting to an access type
11367 if Is_Access_Type
(Target_Type
) then
11369 -- If this type conversion was internally generated by the front end
11370 -- to displace the pointer to the object to reference an interface
11371 -- type and the original node was an Unrestricted_Access attribute,
11372 -- then skip applying accessibility checks (because, according to the
11373 -- GNAT Reference Manual, this attribute is similar to 'Access except
11374 -- that all accessibility and aliased view checks are omitted).
11376 if not Comes_From_Source
(N
)
11377 and then Is_Interface
(Designated_Type
(Target_Type
))
11378 and then Nkind
(Original_Node
(N
)) = N_Attribute_Reference
11379 and then Attribute_Name
(Original_Node
(N
)) =
11380 Name_Unrestricted_Access
11384 -- Apply an accessibility check when the conversion operand is an
11385 -- access parameter (or a renaming thereof), unless conversion was
11386 -- expanded from an Unchecked_ or Unrestricted_Access attribute,
11387 -- or for the actual of a class-wide interface parameter. Note that
11388 -- other checks may still need to be applied below (such as tagged
11391 elsif Is_Entity_Name
(Operand
)
11392 and then Has_Extra_Accessibility
(Entity
(Operand
))
11393 and then Ekind
(Etype
(Operand
)) = E_Anonymous_Access_Type
11394 and then (Nkind
(Original_Node
(N
)) /= N_Attribute_Reference
11395 or else Attribute_Name
(Original_Node
(N
)) = Name_Access
)
11397 if not Comes_From_Source
(N
)
11398 and then Nkind_In
(Parent
(N
), N_Function_Call
,
11399 N_Procedure_Call_Statement
)
11400 and then Is_Interface
(Designated_Type
(Target_Type
))
11401 and then Is_Class_Wide_Type
(Designated_Type
(Target_Type
))
11406 Apply_Accessibility_Check
11407 (Operand
, Target_Type
, Insert_Node
=> Operand
);
11410 -- If the level of the operand type is statically deeper than the
11411 -- level of the target type, then force Program_Error. Note that this
11412 -- can only occur for cases where the attribute is within the body of
11413 -- an instantiation, otherwise the conversion will already have been
11414 -- rejected as illegal.
11416 -- Note: warnings are issued by the analyzer for the instance cases
11418 elsif In_Instance_Body
11420 -- The case where the target type is an anonymous access type of
11421 -- a discriminant is excluded, because the level of such a type
11422 -- depends on the context and currently the level returned for such
11423 -- types is zero, resulting in warnings about about check failures
11424 -- in certain legal cases involving class-wide interfaces as the
11425 -- designated type (some cases, such as return statements, are
11426 -- checked at run time, but not clear if these are handled right
11427 -- in general, see 3.10.2(12/2-12.5/3) ???).
11430 not (Ekind
(Target_Type
) = E_Anonymous_Access_Type
11431 and then Present
(Associated_Node_For_Itype
(Target_Type
))
11432 and then Nkind
(Associated_Node_For_Itype
(Target_Type
)) =
11433 N_Discriminant_Specification
)
11435 Type_Access_Level
(Operand_Type
) > Type_Access_Level
(Target_Type
)
11437 Raise_Accessibility_Error
;
11440 -- When the operand is a selected access discriminant the check needs
11441 -- to be made against the level of the object denoted by the prefix
11442 -- of the selected name. Force Program_Error for this case as well
11443 -- (this accessibility violation can only happen if within the body
11444 -- of an instantiation).
11446 elsif In_Instance_Body
11447 and then Ekind
(Operand_Type
) = E_Anonymous_Access_Type
11448 and then Nkind
(Operand
) = N_Selected_Component
11449 and then Ekind
(Entity
(Selector_Name
(Operand
))) = E_Discriminant
11450 and then Object_Access_Level
(Operand
) >
11451 Type_Access_Level
(Target_Type
)
11453 Raise_Accessibility_Error
;
11458 -- Case of conversions of tagged types and access to tagged types
11460 -- When needed, that is to say when the expression is class-wide, Add
11461 -- runtime a tag check for (strict) downward conversion by using the
11462 -- membership test, generating:
11464 -- [constraint_error when Operand not in Target_Type'Class]
11466 -- or in the access type case
11468 -- [constraint_error
11469 -- when Operand /= null
11470 -- and then Operand.all not in
11471 -- Designated_Type (Target_Type)'Class]
11473 if (Is_Access_Type
(Target_Type
)
11474 and then Is_Tagged_Type
(Designated_Type
(Target_Type
)))
11475 or else Is_Tagged_Type
(Target_Type
)
11477 -- Do not do any expansion in the access type case if the parent is a
11478 -- renaming, since this is an error situation which will be caught by
11479 -- Sem_Ch8, and the expansion can interfere with this error check.
11481 if Is_Access_Type
(Target_Type
) and then Is_Renamed_Object
(N
) then
11485 -- Otherwise, proceed with processing tagged conversion
11487 Tagged_Conversion
: declare
11488 Actual_Op_Typ
: Entity_Id
;
11489 Actual_Targ_Typ
: Entity_Id
;
11490 Make_Conversion
: Boolean := False;
11491 Root_Op_Typ
: Entity_Id
;
11493 procedure Make_Tag_Check
(Targ_Typ
: Entity_Id
);
11494 -- Create a membership check to test whether Operand is a member
11495 -- of Targ_Typ. If the original Target_Type is an access, include
11496 -- a test for null value. The check is inserted at N.
11498 --------------------
11499 -- Make_Tag_Check --
11500 --------------------
11502 procedure Make_Tag_Check
(Targ_Typ
: Entity_Id
) is
11507 -- [Constraint_Error
11508 -- when Operand /= null
11509 -- and then Operand.all not in Targ_Typ]
11511 if Is_Access_Type
(Target_Type
) then
11513 Make_And_Then
(Loc
,
11516 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
11517 Right_Opnd
=> Make_Null
(Loc
)),
11522 Make_Explicit_Dereference
(Loc
,
11523 Prefix
=> Duplicate_Subexpr_No_Checks
(Operand
)),
11524 Right_Opnd
=> New_Occurrence_Of
(Targ_Typ
, Loc
)));
11527 -- [Constraint_Error when Operand not in Targ_Typ]
11532 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
11533 Right_Opnd
=> New_Occurrence_Of
(Targ_Typ
, Loc
));
11537 Make_Raise_Constraint_Error
(Loc
,
11539 Reason
=> CE_Tag_Check_Failed
),
11540 Suppress
=> All_Checks
);
11541 end Make_Tag_Check
;
11543 -- Start of processing for Tagged_Conversion
11546 -- Handle entities from the limited view
11548 if Is_Access_Type
(Operand_Type
) then
11550 Available_View
(Designated_Type
(Operand_Type
));
11552 Actual_Op_Typ
:= Operand_Type
;
11555 if Is_Access_Type
(Target_Type
) then
11557 Available_View
(Designated_Type
(Target_Type
));
11559 Actual_Targ_Typ
:= Target_Type
;
11562 Root_Op_Typ
:= Root_Type
(Actual_Op_Typ
);
11564 -- Ada 2005 (AI-251): Handle interface type conversion
11566 if Is_Interface
(Actual_Op_Typ
)
11568 Is_Interface
(Actual_Targ_Typ
)
11570 Expand_Interface_Conversion
(N
);
11574 if not Tag_Checks_Suppressed
(Actual_Targ_Typ
) then
11576 -- Create a runtime tag check for a downward class-wide type
11579 if Is_Class_Wide_Type
(Actual_Op_Typ
)
11580 and then Actual_Op_Typ
/= Actual_Targ_Typ
11581 and then Root_Op_Typ
/= Actual_Targ_Typ
11582 and then Is_Ancestor
(Root_Op_Typ
, Actual_Targ_Typ
,
11583 Use_Full_View
=> True)
11585 Make_Tag_Check
(Class_Wide_Type
(Actual_Targ_Typ
));
11586 Make_Conversion
:= True;
11589 -- AI05-0073: If the result subtype of the function is defined
11590 -- by an access_definition designating a specific tagged type
11591 -- T, a check is made that the result value is null or the tag
11592 -- of the object designated by the result value identifies T.
11593 -- Constraint_Error is raised if this check fails.
11595 if Nkind
(Parent
(N
)) = N_Simple_Return_Statement
then
11598 Func_Typ
: Entity_Id
;
11601 -- Climb scope stack looking for the enclosing function
11603 Func
:= Current_Scope
;
11604 while Present
(Func
)
11605 and then Ekind
(Func
) /= E_Function
11607 Func
:= Scope
(Func
);
11610 -- The function's return subtype must be defined using
11611 -- an access definition.
11613 if Nkind
(Result_Definition
(Parent
(Func
))) =
11614 N_Access_Definition
11616 Func_Typ
:= Directly_Designated_Type
(Etype
(Func
));
11618 -- The return subtype denotes a specific tagged type,
11619 -- in other words, a non class-wide type.
11621 if Is_Tagged_Type
(Func_Typ
)
11622 and then not Is_Class_Wide_Type
(Func_Typ
)
11624 Make_Tag_Check
(Actual_Targ_Typ
);
11625 Make_Conversion
:= True;
11631 -- We have generated a tag check for either a class-wide type
11632 -- conversion or for AI05-0073.
11634 if Make_Conversion
then
11639 Make_Unchecked_Type_Conversion
(Loc
,
11640 Subtype_Mark
=> New_Occurrence_Of
(Target_Type
, Loc
),
11641 Expression
=> Relocate_Node
(Expression
(N
)));
11643 Analyze_And_Resolve
(N
, Target_Type
);
11647 end Tagged_Conversion
;
11649 -- Case of other access type conversions
11651 elsif Is_Access_Type
(Target_Type
) then
11652 Apply_Constraint_Check
(Operand
, Target_Type
);
11654 -- Case of conversions from a fixed-point type
11656 -- These conversions require special expansion and processing, found in
11657 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
11658 -- since from a semantic point of view, these are simple integer
11659 -- conversions, which do not need further processing.
11661 elsif Is_Fixed_Point_Type
(Operand_Type
)
11662 and then not Conversion_OK
(N
)
11664 -- We should never see universal fixed at this case, since the
11665 -- expansion of the constituent divide or multiply should have
11666 -- eliminated the explicit mention of universal fixed.
11668 pragma Assert
(Operand_Type
/= Universal_Fixed
);
11670 -- Check for special case of the conversion to universal real that
11671 -- occurs as a result of the use of a round attribute. In this case,
11672 -- the real type for the conversion is taken from the target type of
11673 -- the Round attribute and the result must be marked as rounded.
11675 if Target_Type
= Universal_Real
11676 and then Nkind
(Parent
(N
)) = N_Attribute_Reference
11677 and then Attribute_Name
(Parent
(N
)) = Name_Round
11679 Set_Rounded_Result
(N
);
11680 Set_Etype
(N
, Etype
(Parent
(N
)));
11683 -- Otherwise do correct fixed-conversion, but skip these if the
11684 -- Conversion_OK flag is set, because from a semantic point of view
11685 -- these are simple integer conversions needing no further processing
11686 -- (the backend will simply treat them as integers).
11688 if not Conversion_OK
(N
) then
11689 if Is_Fixed_Point_Type
(Etype
(N
)) then
11690 Expand_Convert_Fixed_To_Fixed
(N
);
11693 elsif Is_Integer_Type
(Etype
(N
)) then
11694 Expand_Convert_Fixed_To_Integer
(N
);
11697 pragma Assert
(Is_Floating_Point_Type
(Etype
(N
)));
11698 Expand_Convert_Fixed_To_Float
(N
);
11703 -- Case of conversions to a fixed-point type
11705 -- These conversions require special expansion and processing, found in
11706 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
11707 -- since from a semantic point of view, these are simple integer
11708 -- conversions, which do not need further processing.
11710 elsif Is_Fixed_Point_Type
(Target_Type
)
11711 and then not Conversion_OK
(N
)
11713 if Is_Integer_Type
(Operand_Type
) then
11714 Expand_Convert_Integer_To_Fixed
(N
);
11717 pragma Assert
(Is_Floating_Point_Type
(Operand_Type
));
11718 Expand_Convert_Float_To_Fixed
(N
);
11722 -- Case of float-to-integer conversions
11724 -- We also handle float-to-fixed conversions with Conversion_OK set
11725 -- since semantically the fixed-point target is treated as though it
11726 -- were an integer in such cases.
11728 elsif Is_Floating_Point_Type
(Operand_Type
)
11730 (Is_Integer_Type
(Target_Type
)
11732 (Is_Fixed_Point_Type
(Target_Type
) and then Conversion_OK
(N
)))
11734 -- One more check here, gcc is still not able to do conversions of
11735 -- this type with proper overflow checking, and so gigi is doing an
11736 -- approximation of what is required by doing floating-point compares
11737 -- with the end-point. But that can lose precision in some cases, and
11738 -- give a wrong result. Converting the operand to Universal_Real is
11739 -- helpful, but still does not catch all cases with 64-bit integers
11740 -- on targets with only 64-bit floats.
11742 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
11743 -- Can this code be removed ???
11745 if Do_Range_Check
(Operand
) then
11747 Make_Type_Conversion
(Loc
,
11749 New_Occurrence_Of
(Universal_Real
, Loc
),
11751 Relocate_Node
(Operand
)));
11753 Set_Etype
(Operand
, Universal_Real
);
11754 Enable_Range_Check
(Operand
);
11755 Set_Do_Range_Check
(Expression
(Operand
), False);
11758 -- Case of array conversions
11760 -- Expansion of array conversions, add required length/range checks but
11761 -- only do this if there is no change of representation. For handling of
11762 -- this case, see Handle_Changed_Representation.
11764 elsif Is_Array_Type
(Target_Type
) then
11765 if Is_Constrained
(Target_Type
) then
11766 Apply_Length_Check
(Operand
, Target_Type
);
11768 Apply_Range_Check
(Operand
, Target_Type
);
11771 Handle_Changed_Representation
;
11773 -- Case of conversions of discriminated types
11775 -- Add required discriminant checks if target is constrained. Again this
11776 -- change is skipped if we have a change of representation.
11778 elsif Has_Discriminants
(Target_Type
)
11779 and then Is_Constrained
(Target_Type
)
11781 Apply_Discriminant_Check
(Operand
, Target_Type
);
11782 Handle_Changed_Representation
;
11784 -- Case of all other record conversions. The only processing required
11785 -- is to check for a change of representation requiring the special
11786 -- assignment processing.
11788 elsif Is_Record_Type
(Target_Type
) then
11790 -- Ada 2005 (AI-216): Program_Error is raised when converting from
11791 -- a derived Unchecked_Union type to an unconstrained type that is
11792 -- not Unchecked_Union if the operand lacks inferable discriminants.
11794 if Is_Derived_Type
(Operand_Type
)
11795 and then Is_Unchecked_Union
(Base_Type
(Operand_Type
))
11796 and then not Is_Constrained
(Target_Type
)
11797 and then not Is_Unchecked_Union
(Base_Type
(Target_Type
))
11798 and then not Has_Inferable_Discriminants
(Operand
)
11800 -- To prevent Gigi from generating illegal code, we generate a
11801 -- Program_Error node, but we give it the target type of the
11802 -- conversion (is this requirement documented somewhere ???)
11805 PE
: constant Node_Id
:= Make_Raise_Program_Error
(Loc
,
11806 Reason
=> PE_Unchecked_Union_Restriction
);
11809 Set_Etype
(PE
, Target_Type
);
11814 Handle_Changed_Representation
;
11817 -- Case of conversions of enumeration types
11819 elsif Is_Enumeration_Type
(Target_Type
) then
11821 -- Special processing is required if there is a change of
11822 -- representation (from enumeration representation clauses).
11824 if not Same_Representation
(Target_Type
, Operand_Type
) then
11826 -- Convert: x(y) to x'val (ytyp'val (y))
11829 Make_Attribute_Reference
(Loc
,
11830 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
11831 Attribute_Name
=> Name_Val
,
11832 Expressions
=> New_List
(
11833 Make_Attribute_Reference
(Loc
,
11834 Prefix
=> New_Occurrence_Of
(Operand_Type
, Loc
),
11835 Attribute_Name
=> Name_Pos
,
11836 Expressions
=> New_List
(Operand
)))));
11838 Analyze_And_Resolve
(N
, Target_Type
);
11841 -- Case of conversions to floating-point
11843 elsif Is_Floating_Point_Type
(Target_Type
) then
11847 -- At this stage, either the conversion node has been transformed into
11848 -- some other equivalent expression, or left as a conversion that can be
11849 -- handled by Gigi, in the following cases:
11851 -- Conversions with no change of representation or type
11853 -- Numeric conversions involving integer, floating- and fixed-point
11854 -- values. Fixed-point values are allowed only if Conversion_OK is
11855 -- set, i.e. if the fixed-point values are to be treated as integers.
11857 -- No other conversions should be passed to Gigi
11859 -- Check: are these rules stated in sinfo??? if so, why restate here???
11861 -- The only remaining step is to generate a range check if we still have
11862 -- a type conversion at this stage and Do_Range_Check is set. For now we
11863 -- do this only for conversions of discrete types and for float-to-float
11866 if Nkind
(N
) = N_Type_Conversion
then
11868 -- For now we only support floating-point cases where both source
11869 -- and target are floating-point types. Conversions where the source
11870 -- and target involve integer or fixed-point types are still TBD,
11871 -- though not clear whether those can even happen at this point, due
11872 -- to transformations above. ???
11874 if Is_Floating_Point_Type
(Etype
(N
))
11875 and then Is_Floating_Point_Type
(Etype
(Expression
(N
)))
11877 if Do_Range_Check
(Expression
(N
))
11878 and then Is_Floating_Point_Type
(Target_Type
)
11880 Generate_Range_Check
11881 (Expression
(N
), Target_Type
, CE_Range_Check_Failed
);
11884 -- Discrete-to-discrete conversions
11886 elsif Is_Discrete_Type
(Etype
(N
)) then
11888 Expr
: constant Node_Id
:= Expression
(N
);
11893 if Do_Range_Check
(Expr
)
11894 and then Is_Discrete_Type
(Etype
(Expr
))
11896 Set_Do_Range_Check
(Expr
, False);
11898 -- Before we do a range check, we have to deal with treating
11899 -- a fixed-point operand as an integer. The way we do this
11900 -- is simply to do an unchecked conversion to an appropriate
11901 -- integer type large enough to hold the result.
11903 -- This code is not active yet, because we are only dealing
11904 -- with discrete types so far ???
11906 if Nkind
(Expr
) in N_Has_Treat_Fixed_As_Integer
11907 and then Treat_Fixed_As_Integer
(Expr
)
11909 Ftyp
:= Base_Type
(Etype
(Expr
));
11911 if Esize
(Ftyp
) >= Esize
(Standard_Integer
) then
11912 Ityp
:= Standard_Long_Long_Integer
;
11914 Ityp
:= Standard_Integer
;
11917 Rewrite
(Expr
, Unchecked_Convert_To
(Ityp
, Expr
));
11920 -- Reset overflow flag, since the range check will include
11921 -- dealing with possible overflow, and generate the check.
11922 -- If Address is either a source type or target type,
11923 -- suppress range check to avoid typing anomalies when
11924 -- it is a visible integer type.
11926 Set_Do_Overflow_Check
(N
, False);
11928 if not Is_Descendant_Of_Address
(Etype
(Expr
))
11929 and then not Is_Descendant_Of_Address
(Target_Type
)
11931 Generate_Range_Check
11932 (Expr
, Target_Type
, CE_Range_Check_Failed
);
11939 -- Here at end of processing
11942 -- Apply predicate check if required. Note that we can't just call
11943 -- Apply_Predicate_Check here, because the type looks right after
11944 -- the conversion and it would omit the check. The Comes_From_Source
11945 -- guard is necessary to prevent infinite recursions when we generate
11946 -- internal conversions for the purpose of checking predicates.
11948 if Present
(Predicate_Function
(Target_Type
))
11949 and then not Predicates_Ignored
(Target_Type
)
11950 and then Target_Type
/= Operand_Type
11951 and then Comes_From_Source
(N
)
11954 New_Expr
: constant Node_Id
:= Duplicate_Subexpr
(N
);
11957 -- Avoid infinite recursion on the subsequent expansion of
11958 -- of the copy of the original type conversion.
11960 Set_Comes_From_Source
(New_Expr
, False);
11961 Insert_Action
(N
, Make_Predicate_Check
(Target_Type
, New_Expr
));
11964 end Expand_N_Type_Conversion
;
11966 -----------------------------------
11967 -- Expand_N_Unchecked_Expression --
11968 -----------------------------------
11970 -- Remove the unchecked expression node from the tree. Its job was simply
11971 -- to make sure that its constituent expression was handled with checks
11972 -- off, and now that that is done, we can remove it from the tree, and
11973 -- indeed must, since Gigi does not expect to see these nodes.
11975 procedure Expand_N_Unchecked_Expression
(N
: Node_Id
) is
11976 Exp
: constant Node_Id
:= Expression
(N
);
11978 Set_Assignment_OK
(Exp
, Assignment_OK
(N
) or else Assignment_OK
(Exp
));
11980 end Expand_N_Unchecked_Expression
;
11982 ----------------------------------------
11983 -- Expand_N_Unchecked_Type_Conversion --
11984 ----------------------------------------
11986 -- If this cannot be handled by Gigi and we haven't already made a
11987 -- temporary for it, do it now.
11989 procedure Expand_N_Unchecked_Type_Conversion
(N
: Node_Id
) is
11990 Target_Type
: constant Entity_Id
:= Etype
(N
);
11991 Operand
: constant Node_Id
:= Expression
(N
);
11992 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
11995 -- Nothing at all to do if conversion is to the identical type so remove
11996 -- the conversion completely, it is useless, except that it may carry
11997 -- an Assignment_OK indication which must be propagated to the operand.
11999 if Operand_Type
= Target_Type
then
12001 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
12003 if Assignment_OK
(N
) then
12004 Set_Assignment_OK
(Operand
);
12007 Rewrite
(N
, Relocate_Node
(Operand
));
12011 -- If we have a conversion of a compile time known value to a target
12012 -- type and the value is in range of the target type, then we can simply
12013 -- replace the construct by an integer literal of the correct type. We
12014 -- only apply this to integer types being converted. Possibly it may
12015 -- apply in other cases, but it is too much trouble to worry about.
12017 -- Note that we do not do this transformation if the Kill_Range_Check
12018 -- flag is set, since then the value may be outside the expected range.
12019 -- This happens in the Normalize_Scalars case.
12021 -- We also skip this if either the target or operand type is biased
12022 -- because in this case, the unchecked conversion is supposed to
12023 -- preserve the bit pattern, not the integer value.
12025 if Is_Integer_Type
(Target_Type
)
12026 and then not Has_Biased_Representation
(Target_Type
)
12027 and then Is_Integer_Type
(Operand_Type
)
12028 and then not Has_Biased_Representation
(Operand_Type
)
12029 and then Compile_Time_Known_Value
(Operand
)
12030 and then not Kill_Range_Check
(N
)
12033 Val
: constant Uint
:= Expr_Value
(Operand
);
12036 if Compile_Time_Known_Value
(Type_Low_Bound
(Target_Type
))
12038 Compile_Time_Known_Value
(Type_High_Bound
(Target_Type
))
12040 Val
>= Expr_Value
(Type_Low_Bound
(Target_Type
))
12042 Val
<= Expr_Value
(Type_High_Bound
(Target_Type
))
12044 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Val
));
12046 -- If Address is the target type, just set the type to avoid a
12047 -- spurious type error on the literal when Address is a visible
12050 if Is_Descendant_Of_Address
(Target_Type
) then
12051 Set_Etype
(N
, Target_Type
);
12053 Analyze_And_Resolve
(N
, Target_Type
);
12061 -- Nothing to do if conversion is safe
12063 if Safe_Unchecked_Type_Conversion
(N
) then
12067 -- Otherwise force evaluation unless Assignment_OK flag is set (this
12068 -- flag indicates ??? More comments needed here)
12070 if Assignment_OK
(N
) then
12073 Force_Evaluation
(N
);
12075 end Expand_N_Unchecked_Type_Conversion
;
12077 ----------------------------
12078 -- Expand_Record_Equality --
12079 ----------------------------
12081 -- For non-variant records, Equality is expanded when needed into:
12083 -- and then Lhs.Discr1 = Rhs.Discr1
12085 -- and then Lhs.Discrn = Rhs.Discrn
12086 -- and then Lhs.Cmp1 = Rhs.Cmp1
12088 -- and then Lhs.Cmpn = Rhs.Cmpn
12090 -- The expression is folded by the back end for adjacent fields. This
12091 -- function is called for tagged record in only one occasion: for imple-
12092 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
12093 -- otherwise the primitive "=" is used directly.
12095 function Expand_Record_Equality
12100 Bodies
: List_Id
) return Node_Id
12102 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
12107 First_Time
: Boolean := True;
12109 function Element_To_Compare
(C
: Entity_Id
) return Entity_Id
;
12110 -- Return the next discriminant or component to compare, starting with
12111 -- C, skipping inherited components.
12113 ------------------------
12114 -- Element_To_Compare --
12115 ------------------------
12117 function Element_To_Compare
(C
: Entity_Id
) return Entity_Id
is
12123 -- Exit loop when the next element to be compared is found, or
12124 -- there is no more such element.
12126 exit when No
(Comp
);
12128 exit when Ekind_In
(Comp
, E_Discriminant
, E_Component
)
12131 -- Skip inherited components
12133 -- Note: for a tagged type, we always generate the "=" primitive
12134 -- for the base type (not on the first subtype), so the test for
12135 -- Comp /= Original_Record_Component (Comp) is True for
12136 -- inherited components only.
12138 (Is_Tagged_Type
(Typ
)
12139 and then Comp
/= Original_Record_Component
(Comp
))
12143 or else Chars
(Comp
) = Name_uTag
12145 -- Skip interface elements (secondary tags???)
12147 or else Is_Interface
(Etype
(Comp
)));
12149 Next_Entity
(Comp
);
12153 end Element_To_Compare
;
12155 -- Start of processing for Expand_Record_Equality
12158 -- Generates the following code: (assuming that Typ has one Discr and
12159 -- component C2 is also a record)
12161 -- Lhs.Discr1 = Rhs.Discr1
12162 -- and then Lhs.C1 = Rhs.C1
12163 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
12165 -- and then Lhs.Cmpn = Rhs.Cmpn
12167 Result
:= New_Occurrence_Of
(Standard_True
, Loc
);
12168 C
:= Element_To_Compare
(First_Entity
(Typ
));
12169 while Present
(C
) loop
12180 New_Lhs
:= New_Copy_Tree
(Lhs
);
12181 New_Rhs
:= New_Copy_Tree
(Rhs
);
12185 Expand_Composite_Equality
(Nod
, Etype
(C
),
12187 Make_Selected_Component
(Loc
,
12189 Selector_Name
=> New_Occurrence_Of
(C
, Loc
)),
12191 Make_Selected_Component
(Loc
,
12193 Selector_Name
=> New_Occurrence_Of
(C
, Loc
)),
12196 -- If some (sub)component is an unchecked_union, the whole
12197 -- operation will raise program error.
12199 if Nkind
(Check
) = N_Raise_Program_Error
then
12201 Set_Etype
(Result
, Standard_Boolean
);
12207 -- Generate logical "and" for CodePeer to simplify the
12208 -- generated code and analysis.
12210 elsif CodePeer_Mode
then
12213 Left_Opnd
=> Result
,
12214 Right_Opnd
=> Check
);
12218 Make_And_Then
(Loc
,
12219 Left_Opnd
=> Result
,
12220 Right_Opnd
=> Check
);
12225 First_Time
:= False;
12226 C
:= Element_To_Compare
(Next_Entity
(C
));
12230 end Expand_Record_Equality
;
12232 ---------------------------
12233 -- Expand_Set_Membership --
12234 ---------------------------
12236 procedure Expand_Set_Membership
(N
: Node_Id
) is
12237 Lop
: constant Node_Id
:= Left_Opnd
(N
);
12241 function Make_Cond
(Alt
: Node_Id
) return Node_Id
;
12242 -- If the alternative is a subtype mark, create a simple membership
12243 -- test. Otherwise create an equality test for it.
12249 function Make_Cond
(Alt
: Node_Id
) return Node_Id
is
12251 L
: constant Node_Id
:= New_Copy_Tree
(Lop
);
12252 R
: constant Node_Id
:= Relocate_Node
(Alt
);
12255 if (Is_Entity_Name
(Alt
) and then Is_Type
(Entity
(Alt
)))
12256 or else Nkind
(Alt
) = N_Range
12259 Make_In
(Sloc
(Alt
),
12264 Make_Op_Eq
(Sloc
(Alt
),
12272 -- Start of processing for Expand_Set_Membership
12275 Remove_Side_Effects
(Lop
);
12277 Alt
:= Last
(Alternatives
(N
));
12278 Res
:= Make_Cond
(Alt
);
12281 while Present
(Alt
) loop
12283 Make_Or_Else
(Sloc
(Alt
),
12284 Left_Opnd
=> Make_Cond
(Alt
),
12285 Right_Opnd
=> Res
);
12290 Analyze_And_Resolve
(N
, Standard_Boolean
);
12291 end Expand_Set_Membership
;
12293 -----------------------------------
12294 -- Expand_Short_Circuit_Operator --
12295 -----------------------------------
12297 -- Deal with special expansion if actions are present for the right operand
12298 -- and deal with optimizing case of arguments being True or False. We also
12299 -- deal with the special case of non-standard boolean values.
12301 procedure Expand_Short_Circuit_Operator
(N
: Node_Id
) is
12302 Loc
: constant Source_Ptr
:= Sloc
(N
);
12303 Typ
: constant Entity_Id
:= Etype
(N
);
12304 Left
: constant Node_Id
:= Left_Opnd
(N
);
12305 Right
: constant Node_Id
:= Right_Opnd
(N
);
12306 LocR
: constant Source_Ptr
:= Sloc
(Right
);
12309 Shortcut_Value
: constant Boolean := Nkind
(N
) = N_Or_Else
;
12310 Shortcut_Ent
: constant Entity_Id
:= Boolean_Literals
(Shortcut_Value
);
12311 -- If Left = Shortcut_Value then Right need not be evaluated
12313 function Make_Test_Expr
(Opnd
: Node_Id
) return Node_Id
;
12314 -- For Opnd a boolean expression, return a Boolean expression equivalent
12315 -- to Opnd /= Shortcut_Value.
12317 --------------------
12318 -- Make_Test_Expr --
12319 --------------------
12321 function Make_Test_Expr
(Opnd
: Node_Id
) return Node_Id
is
12323 if Shortcut_Value
then
12324 return Make_Op_Not
(Sloc
(Opnd
), Opnd
);
12328 end Make_Test_Expr
;
12332 Op_Var
: Entity_Id
;
12333 -- Entity for a temporary variable holding the value of the operator,
12334 -- used for expansion in the case where actions are present.
12336 -- Start of processing for Expand_Short_Circuit_Operator
12339 -- Deal with non-standard booleans
12341 if Is_Boolean_Type
(Typ
) then
12342 Adjust_Condition
(Left
);
12343 Adjust_Condition
(Right
);
12344 Set_Etype
(N
, Standard_Boolean
);
12347 -- Check for cases where left argument is known to be True or False
12349 if Compile_Time_Known_Value
(Left
) then
12351 -- Mark SCO for left condition as compile time known
12353 if Generate_SCO
and then Comes_From_Source
(Left
) then
12354 Set_SCO_Condition
(Left
, Expr_Value_E
(Left
) = Standard_True
);
12357 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
12358 -- Any actions associated with Right will be executed unconditionally
12359 -- and can thus be inserted into the tree unconditionally.
12361 if Expr_Value_E
(Left
) /= Shortcut_Ent
then
12362 if Present
(Actions
(N
)) then
12363 Insert_Actions
(N
, Actions
(N
));
12366 Rewrite
(N
, Right
);
12368 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
12369 -- In this case we can forget the actions associated with Right,
12370 -- since they will never be executed.
12373 Kill_Dead_Code
(Right
);
12374 Kill_Dead_Code
(Actions
(N
));
12375 Rewrite
(N
, New_Occurrence_Of
(Shortcut_Ent
, Loc
));
12378 Adjust_Result_Type
(N
, Typ
);
12382 -- If Actions are present for the right operand, we have to do some
12383 -- special processing. We can't just let these actions filter back into
12384 -- code preceding the short circuit (which is what would have happened
12385 -- if we had not trapped them in the short-circuit form), since they
12386 -- must only be executed if the right operand of the short circuit is
12387 -- executed and not otherwise.
12389 if Present
(Actions
(N
)) then
12390 Actlist
:= Actions
(N
);
12392 -- The old approach is to expand:
12394 -- left AND THEN right
12398 -- C : Boolean := False;
12406 -- and finally rewrite the operator into a reference to C. Similarly
12407 -- for left OR ELSE right, with negated values. Note that this
12408 -- rewrite causes some difficulties for coverage analysis because
12409 -- of the introduction of the new variable C, which obscures the
12410 -- structure of the test.
12412 -- We use this "old approach" if Minimize_Expression_With_Actions
12415 if Minimize_Expression_With_Actions
then
12416 Op_Var
:= Make_Temporary
(Loc
, 'C', Related_Node
=> N
);
12419 Make_Object_Declaration
(Loc
,
12420 Defining_Identifier
=> Op_Var
,
12421 Object_Definition
=>
12422 New_Occurrence_Of
(Standard_Boolean
, Loc
),
12424 New_Occurrence_Of
(Shortcut_Ent
, Loc
)));
12426 Append_To
(Actlist
,
12427 Make_Implicit_If_Statement
(Right
,
12428 Condition
=> Make_Test_Expr
(Right
),
12429 Then_Statements
=> New_List
(
12430 Make_Assignment_Statement
(LocR
,
12431 Name
=> New_Occurrence_Of
(Op_Var
, LocR
),
12434 (Boolean_Literals
(not Shortcut_Value
), LocR
)))));
12437 Make_Implicit_If_Statement
(Left
,
12438 Condition
=> Make_Test_Expr
(Left
),
12439 Then_Statements
=> Actlist
));
12441 Rewrite
(N
, New_Occurrence_Of
(Op_Var
, Loc
));
12442 Analyze_And_Resolve
(N
, Standard_Boolean
);
12444 -- The new approach (the default) is to use an
12445 -- Expression_With_Actions node for the right operand of the
12446 -- short-circuit form. Note that this solves the traceability
12447 -- problems for coverage analysis.
12451 Make_Expression_With_Actions
(LocR
,
12452 Expression
=> Relocate_Node
(Right
),
12453 Actions
=> Actlist
));
12455 Set_Actions
(N
, No_List
);
12456 Analyze_And_Resolve
(Right
, Standard_Boolean
);
12459 Adjust_Result_Type
(N
, Typ
);
12463 -- No actions present, check for cases of right argument True/False
12465 if Compile_Time_Known_Value
(Right
) then
12467 -- Mark SCO for left condition as compile time known
12469 if Generate_SCO
and then Comes_From_Source
(Right
) then
12470 Set_SCO_Condition
(Right
, Expr_Value_E
(Right
) = Standard_True
);
12473 -- Change (Left and then True), (Left or else False) to Left. Note
12474 -- that we know there are no actions associated with the right
12475 -- operand, since we just checked for this case above.
12477 if Expr_Value_E
(Right
) /= Shortcut_Ent
then
12480 -- Change (Left and then False), (Left or else True) to Right,
12481 -- making sure to preserve any side effects associated with the Left
12485 Remove_Side_Effects
(Left
);
12486 Rewrite
(N
, New_Occurrence_Of
(Shortcut_Ent
, Loc
));
12490 Adjust_Result_Type
(N
, Typ
);
12491 end Expand_Short_Circuit_Operator
;
12493 -------------------------------------
12494 -- Fixup_Universal_Fixed_Operation --
12495 -------------------------------------
12497 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
) is
12498 Conv
: constant Node_Id
:= Parent
(N
);
12501 -- We must have a type conversion immediately above us
12503 pragma Assert
(Nkind
(Conv
) = N_Type_Conversion
);
12505 -- Normally the type conversion gives our target type. The exception
12506 -- occurs in the case of the Round attribute, where the conversion
12507 -- will be to universal real, and our real type comes from the Round
12508 -- attribute (as well as an indication that we must round the result)
12510 if Nkind
(Parent
(Conv
)) = N_Attribute_Reference
12511 and then Attribute_Name
(Parent
(Conv
)) = Name_Round
12513 Set_Etype
(N
, Etype
(Parent
(Conv
)));
12514 Set_Rounded_Result
(N
);
12516 -- Normal case where type comes from conversion above us
12519 Set_Etype
(N
, Etype
(Conv
));
12521 end Fixup_Universal_Fixed_Operation
;
12523 ---------------------------------
12524 -- Has_Inferable_Discriminants --
12525 ---------------------------------
12527 function Has_Inferable_Discriminants
(N
: Node_Id
) return Boolean is
12529 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean;
12530 -- Determines whether the left-most prefix of a selected component is a
12531 -- formal parameter in a subprogram. Assumes N is a selected component.
12533 --------------------------------
12534 -- Prefix_Is_Formal_Parameter --
12535 --------------------------------
12537 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean is
12538 Sel_Comp
: Node_Id
;
12541 -- Move to the left-most prefix by climbing up the tree
12544 while Present
(Parent
(Sel_Comp
))
12545 and then Nkind
(Parent
(Sel_Comp
)) = N_Selected_Component
12547 Sel_Comp
:= Parent
(Sel_Comp
);
12550 return Ekind
(Entity
(Prefix
(Sel_Comp
))) in Formal_Kind
;
12551 end Prefix_Is_Formal_Parameter
;
12553 -- Start of processing for Has_Inferable_Discriminants
12556 -- For selected components, the subtype of the selector must be a
12557 -- constrained Unchecked_Union. If the component is subject to a
12558 -- per-object constraint, then the enclosing object must have inferable
12561 if Nkind
(N
) = N_Selected_Component
then
12562 if Has_Per_Object_Constraint
(Entity
(Selector_Name
(N
))) then
12564 -- A small hack. If we have a per-object constrained selected
12565 -- component of a formal parameter, return True since we do not
12566 -- know the actual parameter association yet.
12568 if Prefix_Is_Formal_Parameter
(N
) then
12571 -- Otherwise, check the enclosing object and the selector
12574 return Has_Inferable_Discriminants
(Prefix
(N
))
12575 and then Has_Inferable_Discriminants
(Selector_Name
(N
));
12578 -- The call to Has_Inferable_Discriminants will determine whether
12579 -- the selector has a constrained Unchecked_Union nominal type.
12582 return Has_Inferable_Discriminants
(Selector_Name
(N
));
12585 -- A qualified expression has inferable discriminants if its subtype
12586 -- mark is a constrained Unchecked_Union subtype.
12588 elsif Nkind
(N
) = N_Qualified_Expression
then
12589 return Is_Unchecked_Union
(Etype
(Subtype_Mark
(N
)))
12590 and then Is_Constrained
(Etype
(Subtype_Mark
(N
)));
12592 -- For all other names, it is sufficient to have a constrained
12593 -- Unchecked_Union nominal subtype.
12596 return Is_Unchecked_Union
(Base_Type
(Etype
(N
)))
12597 and then Is_Constrained
(Etype
(N
));
12599 end Has_Inferable_Discriminants
;
12601 -------------------------------
12602 -- Insert_Dereference_Action --
12603 -------------------------------
12605 procedure Insert_Dereference_Action
(N
: Node_Id
) is
12606 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean;
12607 -- Return true if type of P is derived from Checked_Pool;
12609 -----------------------------
12610 -- Is_Checked_Storage_Pool --
12611 -----------------------------
12613 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean is
12622 while T
/= Etype
(T
) loop
12623 if Is_RTE
(T
, RE_Checked_Pool
) then
12631 end Is_Checked_Storage_Pool
;
12635 Context
: constant Node_Id
:= Parent
(N
);
12636 Ptr_Typ
: constant Entity_Id
:= Etype
(N
);
12637 Desig_Typ
: constant Entity_Id
:=
12638 Available_View
(Designated_Type
(Ptr_Typ
));
12639 Loc
: constant Source_Ptr
:= Sloc
(N
);
12640 Pool
: constant Entity_Id
:= Associated_Storage_Pool
(Ptr_Typ
);
12646 Size_Bits
: Node_Id
;
12649 -- Start of processing for Insert_Dereference_Action
12652 pragma Assert
(Nkind
(Context
) = N_Explicit_Dereference
);
12654 -- Do not re-expand a dereference which has already been processed by
12657 if Has_Dereference_Action
(Context
) then
12660 -- Do not perform this type of expansion for internally-generated
12663 elsif not Comes_From_Source
(Original_Node
(Context
)) then
12666 -- A dereference action is only applicable to objects which have been
12667 -- allocated on a checked pool.
12669 elsif not Is_Checked_Storage_Pool
(Pool
) then
12673 -- Extract the address of the dereferenced object. Generate:
12675 -- Addr : System.Address := <N>'Pool_Address;
12677 Addr
:= Make_Temporary
(Loc
, 'P');
12680 Make_Object_Declaration
(Loc
,
12681 Defining_Identifier
=> Addr
,
12682 Object_Definition
=>
12683 New_Occurrence_Of
(RTE
(RE_Address
), Loc
),
12685 Make_Attribute_Reference
(Loc
,
12686 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
),
12687 Attribute_Name
=> Name_Pool_Address
)));
12689 -- Calculate the size of the dereferenced object. Generate:
12691 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
12694 Make_Explicit_Dereference
(Loc
,
12695 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
12696 Set_Has_Dereference_Action
(Deref
);
12699 Make_Attribute_Reference
(Loc
,
12701 Attribute_Name
=> Name_Size
);
12703 -- Special case of an unconstrained array: need to add descriptor size
12705 if Is_Array_Type
(Desig_Typ
)
12706 and then not Is_Constrained
(First_Subtype
(Desig_Typ
))
12711 Make_Attribute_Reference
(Loc
,
12713 New_Occurrence_Of
(First_Subtype
(Desig_Typ
), Loc
),
12714 Attribute_Name
=> Name_Descriptor_Size
),
12715 Right_Opnd
=> Size_Bits
);
12718 Size
:= Make_Temporary
(Loc
, 'S');
12720 Make_Object_Declaration
(Loc
,
12721 Defining_Identifier
=> Size
,
12722 Object_Definition
=>
12723 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
),
12725 Make_Op_Divide
(Loc
,
12726 Left_Opnd
=> Size_Bits
,
12727 Right_Opnd
=> Make_Integer_Literal
(Loc
, System_Storage_Unit
))));
12729 -- Calculate the alignment of the dereferenced object. Generate:
12730 -- Alig : constant Storage_Count := <N>.all'Alignment;
12733 Make_Explicit_Dereference
(Loc
,
12734 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
12735 Set_Has_Dereference_Action
(Deref
);
12737 Alig
:= Make_Temporary
(Loc
, 'A');
12739 Make_Object_Declaration
(Loc
,
12740 Defining_Identifier
=> Alig
,
12741 Object_Definition
=>
12742 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
),
12744 Make_Attribute_Reference
(Loc
,
12746 Attribute_Name
=> Name_Alignment
)));
12748 -- A dereference of a controlled object requires special processing. The
12749 -- finalization machinery requests additional space from the underlying
12750 -- pool to allocate and hide two pointers. As a result, a checked pool
12751 -- may mark the wrong memory as valid. Since checked pools do not have
12752 -- knowledge of hidden pointers, we have to bring the two pointers back
12753 -- in view in order to restore the original state of the object.
12755 -- The address manipulation is not performed for access types that are
12756 -- subject to pragma No_Heap_Finalization because the two pointers do
12757 -- not exist in the first place.
12759 if No_Heap_Finalization
(Ptr_Typ
) then
12762 elsif Needs_Finalization
(Desig_Typ
) then
12764 -- Adjust the address and size of the dereferenced object. Generate:
12765 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
12768 Make_Procedure_Call_Statement
(Loc
,
12770 New_Occurrence_Of
(RTE
(RE_Adjust_Controlled_Dereference
), Loc
),
12771 Parameter_Associations
=> New_List
(
12772 New_Occurrence_Of
(Addr
, Loc
),
12773 New_Occurrence_Of
(Size
, Loc
),
12774 New_Occurrence_Of
(Alig
, Loc
)));
12776 -- Class-wide types complicate things because we cannot determine
12777 -- statically whether the actual object is truly controlled. We must
12778 -- generate a runtime check to detect this property. Generate:
12780 -- if Needs_Finalization (<N>.all'Tag) then
12784 if Is_Class_Wide_Type
(Desig_Typ
) then
12786 Make_Explicit_Dereference
(Loc
,
12787 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
12788 Set_Has_Dereference_Action
(Deref
);
12791 Make_Implicit_If_Statement
(N
,
12793 Make_Function_Call
(Loc
,
12795 New_Occurrence_Of
(RTE
(RE_Needs_Finalization
), Loc
),
12796 Parameter_Associations
=> New_List
(
12797 Make_Attribute_Reference
(Loc
,
12799 Attribute_Name
=> Name_Tag
))),
12800 Then_Statements
=> New_List
(Stmt
));
12803 Insert_Action
(N
, Stmt
);
12807 -- Dereference (Pool, Addr, Size, Alig);
12810 Make_Procedure_Call_Statement
(Loc
,
12813 (Find_Prim_Op
(Etype
(Pool
), Name_Dereference
), Loc
),
12814 Parameter_Associations
=> New_List
(
12815 New_Occurrence_Of
(Pool
, Loc
),
12816 New_Occurrence_Of
(Addr
, Loc
),
12817 New_Occurrence_Of
(Size
, Loc
),
12818 New_Occurrence_Of
(Alig
, Loc
))));
12820 -- Mark the explicit dereference as processed to avoid potential
12821 -- infinite expansion.
12823 Set_Has_Dereference_Action
(Context
);
12826 when RE_Not_Available
=>
12828 end Insert_Dereference_Action
;
12830 --------------------------------
12831 -- Integer_Promotion_Possible --
12832 --------------------------------
12834 function Integer_Promotion_Possible
(N
: Node_Id
) return Boolean is
12835 Operand
: constant Node_Id
:= Expression
(N
);
12836 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
12837 Root_Operand_Type
: constant Entity_Id
:= Root_Type
(Operand_Type
);
12840 pragma Assert
(Nkind
(N
) = N_Type_Conversion
);
12844 -- We only do the transformation for source constructs. We assume
12845 -- that the expander knows what it is doing when it generates code.
12847 Comes_From_Source
(N
)
12849 -- If the operand type is Short_Integer or Short_Short_Integer,
12850 -- then we will promote to Integer, which is available on all
12851 -- targets, and is sufficient to ensure no intermediate overflow.
12852 -- Furthermore it is likely to be as efficient or more efficient
12853 -- than using the smaller type for the computation so we do this
12854 -- unconditionally.
12857 (Root_Operand_Type
= Base_Type
(Standard_Short_Integer
)
12859 Root_Operand_Type
= Base_Type
(Standard_Short_Short_Integer
))
12861 -- Test for interesting operation, which includes addition,
12862 -- division, exponentiation, multiplication, subtraction, absolute
12863 -- value and unary negation. Unary "+" is omitted since it is a
12864 -- no-op and thus can't overflow.
12866 and then Nkind_In
(Operand
, N_Op_Abs
,
12873 end Integer_Promotion_Possible
;
12875 ------------------------------
12876 -- Make_Array_Comparison_Op --
12877 ------------------------------
12879 -- This is a hand-coded expansion of the following generic function:
12882 -- type elem is (<>);
12883 -- type index is (<>);
12884 -- type a is array (index range <>) of elem;
12886 -- function Gnnn (X : a; Y: a) return boolean is
12887 -- J : index := Y'first;
12890 -- if X'length = 0 then
12893 -- elsif Y'length = 0 then
12897 -- for I in X'range loop
12898 -- if X (I) = Y (J) then
12899 -- if J = Y'last then
12902 -- J := index'succ (J);
12906 -- return X (I) > Y (J);
12910 -- return X'length > Y'length;
12914 -- Note that since we are essentially doing this expansion by hand, we
12915 -- do not need to generate an actual or formal generic part, just the
12916 -- instantiated function itself.
12918 -- Perhaps we could have the actual generic available in the run-time,
12919 -- obtained by rtsfind, and actually expand a real instantiation ???
12921 function Make_Array_Comparison_Op
12923 Nod
: Node_Id
) return Node_Id
12925 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
12927 X
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uX
);
12928 Y
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uY
);
12929 I
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uI
);
12930 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
12932 Index
: constant Entity_Id
:= Base_Type
(Etype
(First_Index
(Typ
)));
12934 Loop_Statement
: Node_Id
;
12935 Loop_Body
: Node_Id
;
12937 Inner_If
: Node_Id
;
12938 Final_Expr
: Node_Id
;
12939 Func_Body
: Node_Id
;
12940 Func_Name
: Entity_Id
;
12946 -- if J = Y'last then
12949 -- J := index'succ (J);
12953 Make_Implicit_If_Statement
(Nod
,
12956 Left_Opnd
=> New_Occurrence_Of
(J
, Loc
),
12958 Make_Attribute_Reference
(Loc
,
12959 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12960 Attribute_Name
=> Name_Last
)),
12962 Then_Statements
=> New_List
(
12963 Make_Exit_Statement
(Loc
)),
12967 Make_Assignment_Statement
(Loc
,
12968 Name
=> New_Occurrence_Of
(J
, Loc
),
12970 Make_Attribute_Reference
(Loc
,
12971 Prefix
=> New_Occurrence_Of
(Index
, Loc
),
12972 Attribute_Name
=> Name_Succ
,
12973 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
))))));
12975 -- if X (I) = Y (J) then
12978 -- return X (I) > Y (J);
12982 Make_Implicit_If_Statement
(Nod
,
12986 Make_Indexed_Component
(Loc
,
12987 Prefix
=> New_Occurrence_Of
(X
, Loc
),
12988 Expressions
=> New_List
(New_Occurrence_Of
(I
, Loc
))),
12991 Make_Indexed_Component
(Loc
,
12992 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12993 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)))),
12995 Then_Statements
=> New_List
(Inner_If
),
12997 Else_Statements
=> New_List
(
12998 Make_Simple_Return_Statement
(Loc
,
13002 Make_Indexed_Component
(Loc
,
13003 Prefix
=> New_Occurrence_Of
(X
, Loc
),
13004 Expressions
=> New_List
(New_Occurrence_Of
(I
, Loc
))),
13007 Make_Indexed_Component
(Loc
,
13008 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
13009 Expressions
=> New_List
(
13010 New_Occurrence_Of
(J
, Loc
)))))));
13012 -- for I in X'range loop
13017 Make_Implicit_Loop_Statement
(Nod
,
13018 Identifier
=> Empty
,
13020 Iteration_Scheme
=>
13021 Make_Iteration_Scheme
(Loc
,
13022 Loop_Parameter_Specification
=>
13023 Make_Loop_Parameter_Specification
(Loc
,
13024 Defining_Identifier
=> I
,
13025 Discrete_Subtype_Definition
=>
13026 Make_Attribute_Reference
(Loc
,
13027 Prefix
=> New_Occurrence_Of
(X
, Loc
),
13028 Attribute_Name
=> Name_Range
))),
13030 Statements
=> New_List
(Loop_Body
));
13032 -- if X'length = 0 then
13034 -- elsif Y'length = 0 then
13037 -- for ... loop ... end loop;
13038 -- return X'length > Y'length;
13042 Make_Attribute_Reference
(Loc
,
13043 Prefix
=> New_Occurrence_Of
(X
, Loc
),
13044 Attribute_Name
=> Name_Length
);
13047 Make_Attribute_Reference
(Loc
,
13048 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
13049 Attribute_Name
=> Name_Length
);
13053 Left_Opnd
=> Length1
,
13054 Right_Opnd
=> Length2
);
13057 Make_Implicit_If_Statement
(Nod
,
13061 Make_Attribute_Reference
(Loc
,
13062 Prefix
=> New_Occurrence_Of
(X
, Loc
),
13063 Attribute_Name
=> Name_Length
),
13065 Make_Integer_Literal
(Loc
, 0)),
13069 Make_Simple_Return_Statement
(Loc
,
13070 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))),
13072 Elsif_Parts
=> New_List
(
13073 Make_Elsif_Part
(Loc
,
13077 Make_Attribute_Reference
(Loc
,
13078 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
13079 Attribute_Name
=> Name_Length
),
13081 Make_Integer_Literal
(Loc
, 0)),
13085 Make_Simple_Return_Statement
(Loc
,
13086 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
))))),
13088 Else_Statements
=> New_List
(
13090 Make_Simple_Return_Statement
(Loc
,
13091 Expression
=> Final_Expr
)));
13095 Formals
:= New_List
(
13096 Make_Parameter_Specification
(Loc
,
13097 Defining_Identifier
=> X
,
13098 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)),
13100 Make_Parameter_Specification
(Loc
,
13101 Defining_Identifier
=> Y
,
13102 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)));
13104 -- function Gnnn (...) return boolean is
13105 -- J : index := Y'first;
13110 Func_Name
:= Make_Temporary
(Loc
, 'G');
13113 Make_Subprogram_Body
(Loc
,
13115 Make_Function_Specification
(Loc
,
13116 Defining_Unit_Name
=> Func_Name
,
13117 Parameter_Specifications
=> Formals
,
13118 Result_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
)),
13120 Declarations
=> New_List
(
13121 Make_Object_Declaration
(Loc
,
13122 Defining_Identifier
=> J
,
13123 Object_Definition
=> New_Occurrence_Of
(Index
, Loc
),
13125 Make_Attribute_Reference
(Loc
,
13126 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
13127 Attribute_Name
=> Name_First
))),
13129 Handled_Statement_Sequence
=>
13130 Make_Handled_Sequence_Of_Statements
(Loc
,
13131 Statements
=> New_List
(If_Stat
)));
13134 end Make_Array_Comparison_Op
;
13136 ---------------------------
13137 -- Make_Boolean_Array_Op --
13138 ---------------------------
13140 -- For logical operations on boolean arrays, expand in line the following,
13141 -- replacing 'and' with 'or' or 'xor' where needed:
13143 -- function Annn (A : typ; B: typ) return typ is
13146 -- for J in A'range loop
13147 -- C (J) := A (J) op B (J);
13152 -- Here typ is the boolean array type
13154 function Make_Boolean_Array_Op
13156 N
: Node_Id
) return Node_Id
13158 Loc
: constant Source_Ptr
:= Sloc
(N
);
13160 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
13161 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
13162 C
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uC
);
13163 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
13171 Func_Name
: Entity_Id
;
13172 Func_Body
: Node_Id
;
13173 Loop_Statement
: Node_Id
;
13177 Make_Indexed_Component
(Loc
,
13178 Prefix
=> New_Occurrence_Of
(A
, Loc
),
13179 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
13182 Make_Indexed_Component
(Loc
,
13183 Prefix
=> New_Occurrence_Of
(B
, Loc
),
13184 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
13187 Make_Indexed_Component
(Loc
,
13188 Prefix
=> New_Occurrence_Of
(C
, Loc
),
13189 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
13191 if Nkind
(N
) = N_Op_And
then
13195 Right_Opnd
=> B_J
);
13197 elsif Nkind
(N
) = N_Op_Or
then
13201 Right_Opnd
=> B_J
);
13207 Right_Opnd
=> B_J
);
13211 Make_Implicit_Loop_Statement
(N
,
13212 Identifier
=> Empty
,
13214 Iteration_Scheme
=>
13215 Make_Iteration_Scheme
(Loc
,
13216 Loop_Parameter_Specification
=>
13217 Make_Loop_Parameter_Specification
(Loc
,
13218 Defining_Identifier
=> J
,
13219 Discrete_Subtype_Definition
=>
13220 Make_Attribute_Reference
(Loc
,
13221 Prefix
=> New_Occurrence_Of
(A
, Loc
),
13222 Attribute_Name
=> Name_Range
))),
13224 Statements
=> New_List
(
13225 Make_Assignment_Statement
(Loc
,
13227 Expression
=> Op
)));
13229 Formals
:= New_List
(
13230 Make_Parameter_Specification
(Loc
,
13231 Defining_Identifier
=> A
,
13232 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)),
13234 Make_Parameter_Specification
(Loc
,
13235 Defining_Identifier
=> B
,
13236 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)));
13238 Func_Name
:= Make_Temporary
(Loc
, 'A');
13239 Set_Is_Inlined
(Func_Name
);
13242 Make_Subprogram_Body
(Loc
,
13244 Make_Function_Specification
(Loc
,
13245 Defining_Unit_Name
=> Func_Name
,
13246 Parameter_Specifications
=> Formals
,
13247 Result_Definition
=> New_Occurrence_Of
(Typ
, Loc
)),
13249 Declarations
=> New_List
(
13250 Make_Object_Declaration
(Loc
,
13251 Defining_Identifier
=> C
,
13252 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
))),
13254 Handled_Statement_Sequence
=>
13255 Make_Handled_Sequence_Of_Statements
(Loc
,
13256 Statements
=> New_List
(
13258 Make_Simple_Return_Statement
(Loc
,
13259 Expression
=> New_Occurrence_Of
(C
, Loc
)))));
13262 end Make_Boolean_Array_Op
;
13264 -----------------------------------------
13265 -- Minimized_Eliminated_Overflow_Check --
13266 -----------------------------------------
13268 function Minimized_Eliminated_Overflow_Check
(N
: Node_Id
) return Boolean is
13271 Is_Signed_Integer_Type
(Etype
(N
))
13272 and then Overflow_Check_Mode
in Minimized_Or_Eliminated
;
13273 end Minimized_Eliminated_Overflow_Check
;
13275 --------------------------------
13276 -- Optimize_Length_Comparison --
13277 --------------------------------
13279 procedure Optimize_Length_Comparison
(N
: Node_Id
) is
13280 Loc
: constant Source_Ptr
:= Sloc
(N
);
13281 Typ
: constant Entity_Id
:= Etype
(N
);
13286 -- First and Last attribute reference nodes, which end up as left and
13287 -- right operands of the optimized result.
13290 -- True for comparison operand of zero
13293 -- Comparison operand, set only if Is_Zero is false
13295 Ent
: Entity_Id
:= Empty
;
13296 -- Entity whose length is being compared
13298 Index
: Node_Id
:= Empty
;
13299 -- Integer_Literal node for length attribute expression, or Empty
13300 -- if there is no such expression present.
13303 -- Type of array index to which 'Length is applied
13305 Op
: Node_Kind
:= Nkind
(N
);
13306 -- Kind of comparison operator, gets flipped if operands backwards
13308 function Is_Optimizable
(N
: Node_Id
) return Boolean;
13309 -- Tests N to see if it is an optimizable comparison value (defined as
13310 -- constant zero or one, or something else where the value is known to
13311 -- be positive and in the range of 32-bits, and where the corresponding
13312 -- Length value is also known to be 32-bits. If result is true, sets
13313 -- Is_Zero, Ityp, and Comp accordingly.
13315 function Is_Entity_Length
(N
: Node_Id
) return Boolean;
13316 -- Tests if N is a length attribute applied to a simple entity. If so,
13317 -- returns True, and sets Ent to the entity, and Index to the integer
13318 -- literal provided as an attribute expression, or to Empty if none.
13319 -- Also returns True if the expression is a generated type conversion
13320 -- whose expression is of the desired form. This latter case arises
13321 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
13322 -- to check for being in range, which is not needed in this context.
13323 -- Returns False if neither condition holds.
13325 function Prepare_64
(N
: Node_Id
) return Node_Id
;
13326 -- Given a discrete expression, returns a Long_Long_Integer typed
13327 -- expression representing the underlying value of the expression.
13328 -- This is done with an unchecked conversion to the result type. We
13329 -- use unchecked conversion to handle the enumeration type case.
13331 ----------------------
13332 -- Is_Entity_Length --
13333 ----------------------
13335 function Is_Entity_Length
(N
: Node_Id
) return Boolean is
13337 if Nkind
(N
) = N_Attribute_Reference
13338 and then Attribute_Name
(N
) = Name_Length
13339 and then Is_Entity_Name
(Prefix
(N
))
13341 Ent
:= Entity
(Prefix
(N
));
13343 if Present
(Expressions
(N
)) then
13344 Index
:= First
(Expressions
(N
));
13351 elsif Nkind
(N
) = N_Type_Conversion
13352 and then not Comes_From_Source
(N
)
13354 return Is_Entity_Length
(Expression
(N
));
13359 end Is_Entity_Length
;
13361 --------------------
13362 -- Is_Optimizable --
13363 --------------------
13365 function Is_Optimizable
(N
: Node_Id
) return Boolean is
13373 if Compile_Time_Known_Value
(N
) then
13374 Val
:= Expr_Value
(N
);
13376 if Val
= Uint_0
then
13381 elsif Val
= Uint_1
then
13388 -- Here we have to make sure of being within 32-bits
13390 Determine_Range
(N
, OK
, Lo
, Hi
, Assume_Valid
=> True);
13393 or else Lo
< Uint_1
13394 or else Hi
> UI_From_Int
(Int
'Last)
13399 -- Comparison value was within range, so now we must check the index
13400 -- value to make sure it is also within 32-bits.
13402 Indx
:= First_Index
(Etype
(Ent
));
13404 if Present
(Index
) then
13405 for J
in 2 .. UI_To_Int
(Intval
(Index
)) loop
13410 Ityp
:= Etype
(Indx
);
13412 if Esize
(Ityp
) > 32 then
13419 end Is_Optimizable
;
13425 function Prepare_64
(N
: Node_Id
) return Node_Id
is
13427 return Unchecked_Convert_To
(Standard_Long_Long_Integer
, N
);
13430 -- Start of processing for Optimize_Length_Comparison
13433 -- Nothing to do if not a comparison
13435 if Op
not in N_Op_Compare
then
13439 -- Nothing to do if special -gnatd.P debug flag set.
13441 if Debug_Flag_Dot_PP
then
13445 -- Ent'Length op 0/1
13447 if Is_Entity_Length
(Left_Opnd
(N
))
13448 and then Is_Optimizable
(Right_Opnd
(N
))
13452 -- 0/1 op Ent'Length
13454 elsif Is_Entity_Length
(Right_Opnd
(N
))
13455 and then Is_Optimizable
(Left_Opnd
(N
))
13457 -- Flip comparison to opposite sense
13460 when N_Op_Lt
=> Op
:= N_Op_Gt
;
13461 when N_Op_Le
=> Op
:= N_Op_Ge
;
13462 when N_Op_Gt
=> Op
:= N_Op_Lt
;
13463 when N_Op_Ge
=> Op
:= N_Op_Le
;
13464 when others => null;
13467 -- Else optimization not possible
13473 -- Fall through if we will do the optimization
13475 -- Cases to handle:
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 => X'First <= X'Last
13482 -- X'Length /= 1 => X'First /= X'Last
13483 -- X'Length /= n => X'First + (n - 1) /= X'Last
13485 -- X'Length >= 0 => always true, warn
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
13490 -- X'Length > 1 => X'First < X'Last
13491 -- X'Length > n => X'First + (n - 1) < X'Last
13493 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
13494 -- X'Length <= 1 => X'First >= X'Last
13495 -- X'Length <= n => X'First + (n - 1) >= X'Last
13497 -- X'Length < 0 => always false (warn)
13498 -- X'Length < 1 => X'First > X'Last
13499 -- X'Length < n => X'First + (n - 1) > X'Last
13501 -- Note: for the cases of n (not constant 0,1), we require that the
13502 -- corresponding index type be integer or shorter (i.e. not 64-bit),
13503 -- and the same for the comparison value. Then we do the comparison
13504 -- using 64-bit arithmetic (actually long long integer), so that we
13505 -- cannot have overflow intefering with the result.
13507 -- First deal with warning cases
13516 Convert_To
(Typ
, New_Occurrence_Of
(Standard_True
, Loc
)));
13517 Analyze_And_Resolve
(N
, Typ
);
13518 Warn_On_Known_Condition
(N
);
13525 Convert_To
(Typ
, New_Occurrence_Of
(Standard_False
, Loc
)));
13526 Analyze_And_Resolve
(N
, Typ
);
13527 Warn_On_Known_Condition
(N
);
13531 if Constant_Condition_Warnings
13532 and then Comes_From_Source
(Original_Node
(N
))
13534 Error_Msg_N
("could replace by ""'=""?c?", N
);
13544 -- Build the First reference we will use
13547 Make_Attribute_Reference
(Loc
,
13548 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
13549 Attribute_Name
=> Name_First
);
13551 if Present
(Index
) then
13552 Set_Expressions
(Left
, New_List
(New_Copy
(Index
)));
13555 -- If general value case, then do the addition of (n - 1), and
13556 -- also add the needed conversions to type Long_Long_Integer.
13558 if Present
(Comp
) then
13561 Left_Opnd
=> Prepare_64
(Left
),
13563 Make_Op_Subtract
(Loc
,
13564 Left_Opnd
=> Prepare_64
(Comp
),
13565 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)));
13568 -- Build the Last reference we will use
13571 Make_Attribute_Reference
(Loc
,
13572 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
13573 Attribute_Name
=> Name_Last
);
13575 if Present
(Index
) then
13576 Set_Expressions
(Right
, New_List
(New_Copy
(Index
)));
13579 -- If general operand, convert Last reference to Long_Long_Integer
13581 if Present
(Comp
) then
13582 Right
:= Prepare_64
(Right
);
13585 -- Check for cases to optimize
13587 -- X'Length = 0 => X'First > X'Last
13588 -- X'Length < 1 => X'First > X'Last
13589 -- X'Length < n => X'First + (n - 1) > X'Last
13591 if (Is_Zero
and then Op
= N_Op_Eq
)
13592 or else (not Is_Zero
and then Op
= N_Op_Lt
)
13597 Right_Opnd
=> Right
);
13599 -- X'Length = 1 => X'First = X'Last
13600 -- X'Length = n => X'First + (n - 1) = X'Last
13602 elsif not Is_Zero
and then Op
= N_Op_Eq
then
13606 Right_Opnd
=> Right
);
13608 -- X'Length /= 0 => X'First <= X'Last
13609 -- X'Length > 0 => X'First <= X'Last
13611 elsif Is_Zero
and (Op
= N_Op_Ne
or else Op
= N_Op_Gt
) then
13615 Right_Opnd
=> Right
);
13617 -- X'Length /= 1 => X'First /= X'Last
13618 -- X'Length /= n => X'First + (n - 1) /= X'Last
13620 elsif not Is_Zero
and then Op
= N_Op_Ne
then
13624 Right_Opnd
=> Right
);
13626 -- X'Length >= 1 => X'First <= X'Last
13627 -- X'Length >= n => X'First + (n - 1) <= X'Last
13629 elsif not Is_Zero
and then Op
= N_Op_Ge
then
13633 Right_Opnd
=> Right
);
13635 -- X'Length > 1 => X'First < X'Last
13636 -- X'Length > n => X'First + (n = 1) < X'Last
13638 elsif not Is_Zero
and then Op
= N_Op_Gt
then
13642 Right_Opnd
=> Right
);
13644 -- X'Length <= 1 => X'First >= X'Last
13645 -- X'Length <= n => X'First + (n - 1) >= X'Last
13647 elsif not Is_Zero
and then Op
= N_Op_Le
then
13651 Right_Opnd
=> Right
);
13653 -- Should not happen at this stage
13656 raise Program_Error
;
13659 -- Rewrite and finish up
13661 Rewrite
(N
, Result
);
13662 Analyze_And_Resolve
(N
, Typ
);
13664 end Optimize_Length_Comparison
;
13666 --------------------------------
13667 -- Process_If_Case_Statements --
13668 --------------------------------
13670 procedure Process_If_Case_Statements
(N
: Node_Id
; Stmts
: List_Id
) is
13674 Decl
:= First
(Stmts
);
13675 while Present
(Decl
) loop
13676 if Nkind
(Decl
) = N_Object_Declaration
13677 and then Is_Finalizable_Transient
(Decl
, N
)
13679 Process_Transient_In_Expression
(Decl
, N
, Stmts
);
13684 end Process_If_Case_Statements
;
13686 -------------------------------------
13687 -- Process_Transient_In_Expression --
13688 -------------------------------------
13690 procedure Process_Transient_In_Expression
13691 (Obj_Decl
: Node_Id
;
13695 Loc
: constant Source_Ptr
:= Sloc
(Obj_Decl
);
13696 Obj_Id
: constant Entity_Id
:= Defining_Identifier
(Obj_Decl
);
13698 Hook_Context
: constant Node_Id
:= Find_Hook_Context
(Expr
);
13699 -- The node on which to insert the hook as an action. This is usually
13700 -- the innermost enclosing non-transient construct.
13702 Fin_Call
: Node_Id
;
13703 Hook_Assign
: Node_Id
;
13704 Hook_Clear
: Node_Id
;
13705 Hook_Decl
: Node_Id
;
13706 Hook_Insert
: Node_Id
;
13707 Ptr_Decl
: Node_Id
;
13709 Fin_Context
: Node_Id
;
13710 -- The node after which to insert the finalization actions of the
13711 -- transient object.
13714 pragma Assert
(Nkind_In
(Expr
, N_Case_Expression
,
13715 N_Expression_With_Actions
,
13718 -- When the context is a Boolean evaluation, all three nodes capture the
13719 -- result of their computation in a local temporary:
13722 -- Trans_Id : Ctrl_Typ := ...;
13723 -- Result : constant Boolean := ... Trans_Id ...;
13724 -- <finalize Trans_Id>
13727 -- As a result, the finalization of any transient objects can safely
13728 -- take place after the result capture.
13730 -- ??? could this be extended to elementary types?
13732 if Is_Boolean_Type
(Etype
(Expr
)) then
13733 Fin_Context
:= Last
(Stmts
);
13735 -- Otherwise the immediate context may not be safe enough to carry
13736 -- out transient object finalization due to aliasing and nesting of
13737 -- constructs. Insert calls to [Deep_]Finalize after the innermost
13738 -- enclosing non-transient construct.
13741 Fin_Context
:= Hook_Context
;
13744 -- Mark the transient object as successfully processed to avoid double
13747 Set_Is_Finalized_Transient
(Obj_Id
);
13749 -- Construct all the pieces necessary to hook and finalize a transient
13752 Build_Transient_Object_Statements
13753 (Obj_Decl
=> Obj_Decl
,
13754 Fin_Call
=> Fin_Call
,
13755 Hook_Assign
=> Hook_Assign
,
13756 Hook_Clear
=> Hook_Clear
,
13757 Hook_Decl
=> Hook_Decl
,
13758 Ptr_Decl
=> Ptr_Decl
,
13759 Finalize_Obj
=> False);
13761 -- Add the access type which provides a reference to the transient
13762 -- object. Generate:
13764 -- type Ptr_Typ is access all Desig_Typ;
13766 Insert_Action
(Hook_Context
, Ptr_Decl
);
13768 -- Add the temporary which acts as a hook to the transient object.
13771 -- Hook : Ptr_Id := null;
13773 Insert_Action
(Hook_Context
, Hook_Decl
);
13775 -- When the transient object is initialized by an aggregate, the hook
13776 -- must capture the object after the last aggregate assignment takes
13777 -- place. Only then is the object considered initialized. Generate:
13779 -- Hook := Ptr_Typ (Obj_Id);
13781 -- Hook := Obj_Id'Unrestricted_Access;
13783 if Ekind_In
(Obj_Id
, E_Constant
, E_Variable
)
13784 and then Present
(Last_Aggregate_Assignment
(Obj_Id
))
13786 Hook_Insert
:= Last_Aggregate_Assignment
(Obj_Id
);
13788 -- Otherwise the hook seizes the related object immediately
13791 Hook_Insert
:= Obj_Decl
;
13794 Insert_After_And_Analyze
(Hook_Insert
, Hook_Assign
);
13796 -- When the node is part of a return statement, there is no need to
13797 -- insert a finalization call, as the general finalization mechanism
13798 -- (see Build_Finalizer) would take care of the transient object on
13799 -- subprogram exit. Note that it would also be impossible to insert the
13800 -- finalization code after the return statement as this will render it
13803 if Nkind
(Fin_Context
) = N_Simple_Return_Statement
then
13806 -- Finalize the hook after the context has been evaluated. Generate:
13808 -- if Hook /= null then
13809 -- [Deep_]Finalize (Hook.all);
13814 Insert_Action_After
(Fin_Context
,
13815 Make_Implicit_If_Statement
(Obj_Decl
,
13819 New_Occurrence_Of
(Defining_Entity
(Hook_Decl
), Loc
),
13820 Right_Opnd
=> Make_Null
(Loc
)),
13822 Then_Statements
=> New_List
(
13826 end Process_Transient_In_Expression
;
13828 ------------------------
13829 -- Rewrite_Comparison --
13830 ------------------------
13832 procedure Rewrite_Comparison
(N
: Node_Id
) is
13833 Typ
: constant Entity_Id
:= Etype
(N
);
13835 False_Result
: Boolean;
13836 True_Result
: Boolean;
13839 if Nkind
(N
) = N_Type_Conversion
then
13840 Rewrite_Comparison
(Expression
(N
));
13843 elsif Nkind
(N
) not in N_Op_Compare
then
13847 -- Determine the potential outcome of the comparison assuming that the
13848 -- operands are valid and emit a warning when the comparison evaluates
13849 -- to True or False only in the presence of invalid values.
13851 Warn_On_Constant_Valid_Condition
(N
);
13853 -- Determine the potential outcome of the comparison assuming that the
13854 -- operands are not valid.
13858 Assume_Valid
=> False,
13859 True_Result
=> True_Result
,
13860 False_Result
=> False_Result
);
13862 -- The outcome is a decisive False or True, rewrite the operator
13864 if False_Result
or True_Result
then
13867 New_Occurrence_Of
(Boolean_Literals
(True_Result
), Sloc
(N
))));
13869 Analyze_And_Resolve
(N
, Typ
);
13870 Warn_On_Known_Condition
(N
);
13872 end Rewrite_Comparison
;
13874 ----------------------------
13875 -- Safe_In_Place_Array_Op --
13876 ----------------------------
13878 function Safe_In_Place_Array_Op
13881 Op2
: Node_Id
) return Boolean
13883 Target
: Entity_Id
;
13885 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean;
13886 -- Operand is safe if it cannot overlap part of the target of the
13887 -- operation. If the operand and the target are identical, the operand
13888 -- is safe. The operand can be empty in the case of negation.
13890 function Is_Unaliased
(N
: Node_Id
) return Boolean;
13891 -- Check that N is a stand-alone entity
13897 function Is_Unaliased
(N
: Node_Id
) return Boolean is
13901 and then No
(Address_Clause
(Entity
(N
)))
13902 and then No
(Renamed_Object
(Entity
(N
)));
13905 ---------------------
13906 -- Is_Safe_Operand --
13907 ---------------------
13909 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean is
13914 elsif Is_Entity_Name
(Op
) then
13915 return Is_Unaliased
(Op
);
13917 elsif Nkind_In
(Op
, N_Indexed_Component
, N_Selected_Component
) then
13918 return Is_Unaliased
(Prefix
(Op
));
13920 elsif Nkind
(Op
) = N_Slice
then
13922 Is_Unaliased
(Prefix
(Op
))
13923 and then Entity
(Prefix
(Op
)) /= Target
;
13925 elsif Nkind
(Op
) = N_Op_Not
then
13926 return Is_Safe_Operand
(Right_Opnd
(Op
));
13931 end Is_Safe_Operand
;
13933 -- Start of processing for Safe_In_Place_Array_Op
13936 -- Skip this processing if the component size is different from system
13937 -- storage unit (since at least for NOT this would cause problems).
13939 if Component_Size
(Etype
(Lhs
)) /= System_Storage_Unit
then
13942 -- Cannot do in place stuff if non-standard Boolean representation
13944 elsif Has_Non_Standard_Rep
(Component_Type
(Etype
(Lhs
))) then
13947 elsif not Is_Unaliased
(Lhs
) then
13951 Target
:= Entity
(Lhs
);
13952 return Is_Safe_Operand
(Op1
) and then Is_Safe_Operand
(Op2
);
13954 end Safe_In_Place_Array_Op
;
13956 -----------------------
13957 -- Tagged_Membership --
13958 -----------------------
13960 -- There are two different cases to consider depending on whether the right
13961 -- operand is a class-wide type or not. If not we just compare the actual
13962 -- tag of the left expr to the target type tag:
13964 -- Left_Expr.Tag = Right_Type'Tag;
13966 -- If it is a class-wide type we use the RT function CW_Membership which is
13967 -- usually implemented by looking in the ancestor tables contained in the
13968 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
13970 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
13971 -- function IW_Membership which is usually implemented by looking in the
13972 -- table of abstract interface types plus the ancestor table contained in
13973 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
13975 procedure Tagged_Membership
13977 SCIL_Node
: out Node_Id
;
13978 Result
: out Node_Id
)
13980 Left
: constant Node_Id
:= Left_Opnd
(N
);
13981 Right
: constant Node_Id
:= Right_Opnd
(N
);
13982 Loc
: constant Source_Ptr
:= Sloc
(N
);
13984 Full_R_Typ
: Entity_Id
;
13985 Left_Type
: Entity_Id
;
13986 New_Node
: Node_Id
;
13987 Right_Type
: Entity_Id
;
13991 SCIL_Node
:= Empty
;
13993 -- Handle entities from the limited view
13995 Left_Type
:= Available_View
(Etype
(Left
));
13996 Right_Type
:= Available_View
(Etype
(Right
));
13998 -- In the case where the type is an access type, the test is applied
13999 -- using the designated types (needed in Ada 2012 for implicit anonymous
14000 -- access conversions, for AI05-0149).
14002 if Is_Access_Type
(Right_Type
) then
14003 Left_Type
:= Designated_Type
(Left_Type
);
14004 Right_Type
:= Designated_Type
(Right_Type
);
14007 if Is_Class_Wide_Type
(Left_Type
) then
14008 Left_Type
:= Root_Type
(Left_Type
);
14011 if Is_Class_Wide_Type
(Right_Type
) then
14012 Full_R_Typ
:= Underlying_Type
(Root_Type
(Right_Type
));
14014 Full_R_Typ
:= Underlying_Type
(Right_Type
);
14018 Make_Selected_Component
(Loc
,
14019 Prefix
=> Relocate_Node
(Left
),
14021 New_Occurrence_Of
(First_Tag_Component
(Left_Type
), Loc
));
14023 if Is_Class_Wide_Type
(Right_Type
) or else Is_Interface
(Left_Type
) then
14025 -- No need to issue a run-time check if we statically know that the
14026 -- result of this membership test is always true. For example,
14027 -- considering the following declarations:
14029 -- type Iface is interface;
14030 -- type T is tagged null record;
14031 -- type DT is new T and Iface with null record;
14036 -- These membership tests are always true:
14039 -- Obj2 in T'Class;
14040 -- Obj2 in Iface'Class;
14042 -- We do not need to handle cases where the membership is illegal.
14045 -- Obj1 in DT'Class; -- Compile time error
14046 -- Obj1 in Iface'Class; -- Compile time error
14048 if not Is_Class_Wide_Type
(Left_Type
)
14049 and then (Is_Ancestor
(Etype
(Right_Type
), Left_Type
,
14050 Use_Full_View
=> True)
14051 or else (Is_Interface
(Etype
(Right_Type
))
14052 and then Interface_Present_In_Ancestor
14054 Iface
=> Etype
(Right_Type
))))
14056 Result
:= New_Occurrence_Of
(Standard_True
, Loc
);
14060 -- Ada 2005 (AI-251): Class-wide applied to interfaces
14062 if Is_Interface
(Etype
(Class_Wide_Type
(Right_Type
)))
14064 -- Support to: "Iface_CW_Typ in Typ'Class"
14066 or else Is_Interface
(Left_Type
)
14068 -- Issue error if IW_Membership operation not available in a
14069 -- configurable run time setting.
14071 if not RTE_Available
(RE_IW_Membership
) then
14073 ("dynamic membership test on interface types", N
);
14079 Make_Function_Call
(Loc
,
14080 Name
=> New_Occurrence_Of
(RTE
(RE_IW_Membership
), Loc
),
14081 Parameter_Associations
=> New_List
(
14082 Make_Attribute_Reference
(Loc
,
14084 Attribute_Name
=> Name_Address
),
14085 New_Occurrence_Of
(
14086 Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))),
14089 -- Ada 95: Normal case
14092 Build_CW_Membership
(Loc
,
14093 Obj_Tag_Node
=> Obj_Tag
,
14095 New_Occurrence_Of
(
14096 Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))), Loc
),
14098 New_Node
=> New_Node
);
14100 -- Generate the SCIL node for this class-wide membership test.
14101 -- Done here because the previous call to Build_CW_Membership
14102 -- relocates Obj_Tag.
14104 if Generate_SCIL
then
14105 SCIL_Node
:= Make_SCIL_Membership_Test
(Sloc
(N
));
14106 Set_SCIL_Entity
(SCIL_Node
, Etype
(Right_Type
));
14107 Set_SCIL_Tag_Value
(SCIL_Node
, Obj_Tag
);
14110 Result
:= New_Node
;
14113 -- Right_Type is not a class-wide type
14116 -- No need to check the tag of the object if Right_Typ is abstract
14118 if Is_Abstract_Type
(Right_Type
) then
14119 Result
:= New_Occurrence_Of
(Standard_False
, Loc
);
14124 Left_Opnd
=> Obj_Tag
,
14127 (Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))), Loc
));
14130 end Tagged_Membership
;
14132 ------------------------------
14133 -- Unary_Op_Validity_Checks --
14134 ------------------------------
14136 procedure Unary_Op_Validity_Checks
(N
: Node_Id
) is
14138 if Validity_Checks_On
and Validity_Check_Operands
then
14139 Ensure_Valid
(Right_Opnd
(N
));
14141 end Unary_Op_Validity_Checks
;