1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, 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_Short_Circuit_Operator
(N
: Node_Id
);
132 -- Common expansion processing for short-circuit boolean operators
134 procedure Expand_Compare_Minimize_Eliminate_Overflow
(N
: Node_Id
);
135 -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is
136 -- where we allow comparison of "out of range" values.
138 function Expand_Composite_Equality
143 Bodies
: List_Id
) return Node_Id
;
144 -- Local recursive function used to expand equality for nested composite
145 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
146 -- to attach bodies of local functions that are created in the process. It
147 -- is the responsibility of the caller to insert those bodies at the right
148 -- place. Nod provides the Sloc value for generated code. Lhs and Rhs are
149 -- the left and right sides for the comparison, and Typ is the type of the
150 -- objects to compare.
152 procedure Expand_Concatenate
(Cnode
: Node_Id
; Opnds
: List_Id
);
153 -- Routine to expand concatenation of a sequence of two or more operands
154 -- (in the list Operands) and replace node Cnode with the result of the
155 -- concatenation. The operands can be of any appropriate type, and can
156 -- include both arrays and singleton elements.
158 procedure Expand_Membership_Minimize_Eliminate_Overflow
(N
: Node_Id
);
159 -- N is an N_In membership test mode, with the overflow check mode set to
160 -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed
161 -- integer type. This is a case where top level processing is required to
162 -- handle overflow checks in subtrees.
164 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
);
165 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
166 -- fixed. We do not have such a type at runtime, so the purpose of this
167 -- routine is to find the real type by looking up the tree. We also
168 -- determine if the operation must be rounded.
170 function Has_Inferable_Discriminants
(N
: Node_Id
) return Boolean;
171 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
172 -- discriminants if it has a constrained nominal type, unless the object
173 -- is a component of an enclosing Unchecked_Union object that is subject
174 -- to a per-object constraint and the enclosing object lacks inferable
177 -- An expression of an Unchecked_Union type has inferable discriminants
178 -- if it is either a name of an object with inferable discriminants or a
179 -- qualified expression whose subtype mark denotes a constrained subtype.
181 procedure Insert_Dereference_Action
(N
: Node_Id
);
182 -- N is an expression whose type is an access. When the type of the
183 -- associated storage pool is derived from Checked_Pool, generate a
184 -- call to the 'Dereference' primitive operation.
186 function Make_Array_Comparison_Op
188 Nod
: Node_Id
) return Node_Id
;
189 -- Comparisons between arrays are expanded in line. This function produces
190 -- the body of the implementation of (a > b), where a and b are one-
191 -- dimensional arrays of some discrete type. The original node is then
192 -- expanded into the appropriate call to this function. Nod provides the
193 -- Sloc value for the generated code.
195 function Make_Boolean_Array_Op
197 N
: Node_Id
) return Node_Id
;
198 -- Boolean operations on boolean arrays are expanded in line. This function
199 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
200 -- b). It is used only the normal case and not the packed case. The type
201 -- involved, Typ, is the Boolean array type, and the logical operations in
202 -- the body are simple boolean operations. Note that Typ is always a
203 -- constrained type (the caller has ensured this by using
204 -- Convert_To_Actual_Subtype if necessary).
206 function Minimized_Eliminated_Overflow_Check
(N
: Node_Id
) return Boolean;
207 -- For signed arithmetic operations when the current overflow mode is
208 -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks
209 -- as the first thing we do. We then return. We count on the recursive
210 -- apparatus for overflow checks to call us back with an equivalent
211 -- operation that is in CHECKED mode, avoiding a recursive entry into this
212 -- routine, and that is when we will proceed with the expansion of the
213 -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do
214 -- these optimizations without first making this check, since there may be
215 -- operands further down the tree that are relying on the recursive calls
216 -- triggered by the top level nodes to properly process overflow checking
217 -- and remaining expansion on these nodes. Note that this call back may be
218 -- skipped if the operation is done in Bignum mode but that's fine, since
219 -- the Bignum call takes care of everything.
221 procedure Optimize_Length_Comparison
(N
: Node_Id
);
222 -- Given an expression, if it is of the form X'Length op N (or the other
223 -- way round), where N is known at compile time to be 0 or 1, and X is a
224 -- simple entity, and op is a comparison operator, optimizes it into a
225 -- comparison of First and Last.
227 procedure Process_If_Case_Statements
(N
: Node_Id
; Stmts
: List_Id
);
228 -- Inspect and process statement list Stmt of if or case expression N for
229 -- transient objects. If such objects are found, the routine generates code
230 -- to clean them up when the context of the expression is evaluated.
232 procedure Process_Transient_In_Expression
236 -- Subsidiary routine to the expansion of expression_with_actions, if and
237 -- case expressions. Generate all necessary code to finalize a transient
238 -- object when the enclosing context is elaborated or evaluated. Obj_Decl
239 -- denotes the declaration of the transient object, which is usually the
240 -- result of a controlled function call. Expr denotes the expression with
241 -- actions, if expression, or case expression node. Stmts denotes the
242 -- statement list which contains Decl, either at the top level or within a
245 procedure Rewrite_Comparison
(N
: Node_Id
);
246 -- If N is the node for a comparison whose outcome can be determined at
247 -- compile time, then the node N can be rewritten with True or False. If
248 -- the outcome cannot be determined at compile time, the call has no
249 -- effect. If N is a type conversion, then this processing is applied to
250 -- its expression. If N is neither comparison nor a type conversion, the
251 -- call has no effect.
253 procedure Tagged_Membership
255 SCIL_Node
: out Node_Id
;
256 Result
: out Node_Id
);
257 -- Construct the expression corresponding to the tagged membership test.
258 -- Deals with a second operand being (or not) a class-wide type.
260 function Safe_In_Place_Array_Op
263 Op2
: Node_Id
) return Boolean;
264 -- In the context of an assignment, where the right-hand side is a boolean
265 -- operation on arrays, check whether operation can be performed in place.
267 procedure Unary_Op_Validity_Checks
(N
: Node_Id
);
268 pragma Inline
(Unary_Op_Validity_Checks
);
269 -- Performs validity checks for a unary operator
271 -------------------------------
272 -- Binary_Op_Validity_Checks --
273 -------------------------------
275 procedure Binary_Op_Validity_Checks
(N
: Node_Id
) is
277 if Validity_Checks_On
and Validity_Check_Operands
then
278 Ensure_Valid
(Left_Opnd
(N
));
279 Ensure_Valid
(Right_Opnd
(N
));
281 end Binary_Op_Validity_Checks
;
283 ------------------------------------
284 -- Build_Boolean_Array_Proc_Call --
285 ------------------------------------
287 procedure Build_Boolean_Array_Proc_Call
292 Loc
: constant Source_Ptr
:= Sloc
(N
);
293 Kind
: constant Node_Kind
:= Nkind
(Expression
(N
));
294 Target
: constant Node_Id
:=
295 Make_Attribute_Reference
(Loc
,
297 Attribute_Name
=> Name_Address
);
299 Arg1
: Node_Id
:= Op1
;
300 Arg2
: Node_Id
:= Op2
;
302 Proc_Name
: Entity_Id
;
305 if Kind
= N_Op_Not
then
306 if Nkind
(Op1
) in N_Binary_Op
then
308 -- Use negated version of the binary operators
310 if Nkind
(Op1
) = N_Op_And
then
311 Proc_Name
:= RTE
(RE_Vector_Nand
);
313 elsif Nkind
(Op1
) = N_Op_Or
then
314 Proc_Name
:= RTE
(RE_Vector_Nor
);
316 else pragma Assert
(Nkind
(Op1
) = N_Op_Xor
);
317 Proc_Name
:= RTE
(RE_Vector_Xor
);
321 Make_Procedure_Call_Statement
(Loc
,
322 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
324 Parameter_Associations
=> New_List
(
326 Make_Attribute_Reference
(Loc
,
327 Prefix
=> Left_Opnd
(Op1
),
328 Attribute_Name
=> Name_Address
),
330 Make_Attribute_Reference
(Loc
,
331 Prefix
=> Right_Opnd
(Op1
),
332 Attribute_Name
=> Name_Address
),
334 Make_Attribute_Reference
(Loc
,
335 Prefix
=> Left_Opnd
(Op1
),
336 Attribute_Name
=> Name_Length
)));
339 Proc_Name
:= RTE
(RE_Vector_Not
);
342 Make_Procedure_Call_Statement
(Loc
,
343 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
344 Parameter_Associations
=> New_List
(
347 Make_Attribute_Reference
(Loc
,
349 Attribute_Name
=> Name_Address
),
351 Make_Attribute_Reference
(Loc
,
353 Attribute_Name
=> Name_Length
)));
357 -- We use the following equivalences:
359 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
360 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
361 -- (not X) xor (not Y) = X xor Y
362 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
364 if Nkind
(Op1
) = N_Op_Not
then
365 Arg1
:= Right_Opnd
(Op1
);
366 Arg2
:= Right_Opnd
(Op2
);
368 if Kind
= N_Op_And
then
369 Proc_Name
:= RTE
(RE_Vector_Nor
);
370 elsif Kind
= N_Op_Or
then
371 Proc_Name
:= RTE
(RE_Vector_Nand
);
373 Proc_Name
:= RTE
(RE_Vector_Xor
);
377 if Kind
= N_Op_And
then
378 Proc_Name
:= RTE
(RE_Vector_And
);
379 elsif Kind
= N_Op_Or
then
380 Proc_Name
:= RTE
(RE_Vector_Or
);
381 elsif Nkind
(Op2
) = N_Op_Not
then
382 Proc_Name
:= RTE
(RE_Vector_Nxor
);
383 Arg2
:= Right_Opnd
(Op2
);
385 Proc_Name
:= RTE
(RE_Vector_Xor
);
390 Make_Procedure_Call_Statement
(Loc
,
391 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
392 Parameter_Associations
=> New_List
(
394 Make_Attribute_Reference
(Loc
,
396 Attribute_Name
=> Name_Address
),
397 Make_Attribute_Reference
(Loc
,
399 Attribute_Name
=> Name_Address
),
400 Make_Attribute_Reference
(Loc
,
402 Attribute_Name
=> Name_Length
)));
405 Rewrite
(N
, Call_Node
);
409 when RE_Not_Available
=>
411 end Build_Boolean_Array_Proc_Call
;
413 --------------------------------
414 -- Displace_Allocator_Pointer --
415 --------------------------------
417 procedure Displace_Allocator_Pointer
(N
: Node_Id
) is
418 Loc
: constant Source_Ptr
:= Sloc
(N
);
419 Orig_Node
: constant Node_Id
:= Original_Node
(N
);
425 -- Do nothing in case of VM targets: the virtual machine will handle
426 -- interfaces directly.
428 if not Tagged_Type_Expansion
then
432 pragma Assert
(Nkind
(N
) = N_Identifier
433 and then Nkind
(Orig_Node
) = N_Allocator
);
435 PtrT
:= Etype
(Orig_Node
);
436 Dtyp
:= Available_View
(Designated_Type
(PtrT
));
437 Etyp
:= Etype
(Expression
(Orig_Node
));
439 if Is_Class_Wide_Type
(Dtyp
) and then Is_Interface
(Dtyp
) then
441 -- If the type of the allocator expression is not an interface type
442 -- we can generate code to reference the record component containing
443 -- the pointer to the secondary dispatch table.
445 if not Is_Interface
(Etyp
) then
447 Saved_Typ
: constant Entity_Id
:= Etype
(Orig_Node
);
450 -- 1) Get access to the allocated object
453 Make_Explicit_Dereference
(Loc
, Relocate_Node
(N
)));
457 -- 2) Add the conversion to displace the pointer to reference
458 -- the secondary dispatch table.
460 Rewrite
(N
, Convert_To
(Dtyp
, Relocate_Node
(N
)));
461 Analyze_And_Resolve
(N
, Dtyp
);
463 -- 3) The 'access to the secondary dispatch table will be used
464 -- as the value returned by the allocator.
467 Make_Attribute_Reference
(Loc
,
468 Prefix
=> Relocate_Node
(N
),
469 Attribute_Name
=> Name_Access
));
470 Set_Etype
(N
, Saved_Typ
);
474 -- If the type of the allocator expression is an interface type we
475 -- generate a run-time call to displace "this" to reference the
476 -- component containing the pointer to the secondary dispatch table
477 -- or else raise Constraint_Error if the actual object does not
478 -- implement the target interface. This case corresponds to the
479 -- following example:
481 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
483 -- return new Iface_2'Class'(Obj);
488 Unchecked_Convert_To
(PtrT
,
489 Make_Function_Call
(Loc
,
490 Name
=> New_Occurrence_Of
(RTE
(RE_Displace
), Loc
),
491 Parameter_Associations
=> New_List
(
492 Unchecked_Convert_To
(RTE
(RE_Address
),
498 (Access_Disp_Table
(Etype
(Base_Type
(Dtyp
))))),
500 Analyze_And_Resolve
(N
, PtrT
);
503 end Displace_Allocator_Pointer
;
505 ---------------------------------
506 -- Expand_Allocator_Expression --
507 ---------------------------------
509 procedure Expand_Allocator_Expression
(N
: Node_Id
) is
510 Loc
: constant Source_Ptr
:= Sloc
(N
);
511 Exp
: constant Node_Id
:= Expression
(Expression
(N
));
512 PtrT
: constant Entity_Id
:= Etype
(N
);
513 DesigT
: constant Entity_Id
:= Designated_Type
(PtrT
);
515 procedure Apply_Accessibility_Check
517 Built_In_Place
: Boolean := False);
518 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
519 -- type, generate an accessibility check to verify that the level of the
520 -- type of the created object is not deeper than the level of the access
521 -- type. If the type of the qualified expression is class-wide, then
522 -- always generate the check (except in the case where it is known to be
523 -- unnecessary, see comment below). Otherwise, only generate the check
524 -- if the level of the qualified expression type is statically deeper
525 -- than the access type.
527 -- Although the static accessibility will generally have been performed
528 -- as a legality check, it won't have been done in cases where the
529 -- allocator appears in generic body, so a run-time check is needed in
530 -- general. One special case is when the access type is declared in the
531 -- same scope as the class-wide allocator, in which case the check can
532 -- never fail, so it need not be generated.
534 -- As an open issue, there seem to be cases where the static level
535 -- associated with the class-wide object's underlying type is not
536 -- sufficient to perform the proper accessibility check, such as for
537 -- allocators in nested subprograms or accept statements initialized by
538 -- class-wide formals when the actual originates outside at a deeper
539 -- static level. The nested subprogram case might require passing
540 -- accessibility levels along with class-wide parameters, and the task
541 -- case seems to be an actual gap in the language rules that needs to
542 -- be fixed by the ARG. ???
544 -------------------------------
545 -- Apply_Accessibility_Check --
546 -------------------------------
548 procedure Apply_Accessibility_Check
550 Built_In_Place
: Boolean := False)
552 Pool_Id
: constant Entity_Id
:= Associated_Storage_Pool
(PtrT
);
560 if Ada_Version
>= Ada_2005
561 and then Is_Class_Wide_Type
(DesigT
)
562 and then Tagged_Type_Expansion
563 and then not Scope_Suppress
.Suppress
(Accessibility_Check
)
565 (Type_Access_Level
(Etype
(Exp
)) > Type_Access_Level
(PtrT
)
567 (Is_Class_Wide_Type
(Etype
(Exp
))
568 and then Scope
(PtrT
) /= Current_Scope
))
570 -- If the allocator was built in place, Ref is already a reference
571 -- to the access object initialized to the result of the allocator
572 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
573 -- Remove_Side_Effects for cases where the build-in-place call may
574 -- still be the prefix of the reference (to avoid generating
575 -- duplicate calls). Otherwise, it is the entity associated with
576 -- the object containing the address of the allocated object.
578 if Built_In_Place
then
579 Remove_Side_Effects
(Ref
);
580 Obj_Ref
:= New_Copy_Tree
(Ref
);
582 Obj_Ref
:= New_Occurrence_Of
(Ref
, Loc
);
585 -- For access to interface types we must generate code to displace
586 -- the pointer to the base of the object since the subsequent code
587 -- references components located in the TSD of the object (which
588 -- is associated with the primary dispatch table --see a-tags.ads)
589 -- and also generates code invoking Free, which requires also a
590 -- reference to the base of the unallocated object.
592 if Is_Interface
(DesigT
) and then Tagged_Type_Expansion
then
594 Unchecked_Convert_To
(Etype
(Obj_Ref
),
595 Make_Function_Call
(Loc
,
597 New_Occurrence_Of
(RTE
(RE_Base_Address
), Loc
),
598 Parameter_Associations
=> New_List
(
599 Unchecked_Convert_To
(RTE
(RE_Address
),
600 New_Copy_Tree
(Obj_Ref
)))));
603 -- Step 1: Create the object clean up code
607 -- Deallocate the object if the accessibility check fails. This
608 -- is done only on targets or profiles that support deallocation.
612 if RTE_Available
(RE_Free
) then
613 Free_Stmt
:= Make_Free_Statement
(Loc
, New_Copy_Tree
(Obj_Ref
));
614 Set_Storage_Pool
(Free_Stmt
, Pool_Id
);
616 Append_To
(Stmts
, Free_Stmt
);
618 -- The target or profile cannot deallocate objects
624 -- Finalize the object if applicable. Generate:
626 -- [Deep_]Finalize (Obj_Ref.all);
628 if Needs_Finalization
(DesigT
) then
632 Make_Explicit_Dereference
(Loc
, New_Copy
(Obj_Ref
)),
635 -- Guard against a missing [Deep_]Finalize when the designated
636 -- type was not properly frozen.
638 if No
(Fin_Call
) then
639 Fin_Call
:= Make_Null_Statement
(Loc
);
642 -- When the target or profile supports deallocation, wrap the
643 -- finalization call in a block to ensure proper deallocation
644 -- even if finalization fails. Generate:
654 if Present
(Free_Stmt
) then
656 Make_Block_Statement
(Loc
,
657 Handled_Statement_Sequence
=>
658 Make_Handled_Sequence_Of_Statements
(Loc
,
659 Statements
=> New_List
(Fin_Call
),
661 Exception_Handlers
=> New_List
(
662 Make_Exception_Handler
(Loc
,
663 Exception_Choices
=> New_List
(
664 Make_Others_Choice
(Loc
)),
665 Statements
=> New_List
(
666 New_Copy_Tree
(Free_Stmt
),
667 Make_Raise_Statement
(Loc
))))));
670 Prepend_To
(Stmts
, Fin_Call
);
673 -- Signal the accessibility failure through a Program_Error
676 Make_Raise_Program_Error
(Loc
,
677 Condition
=> New_Occurrence_Of
(Standard_True
, Loc
),
678 Reason
=> PE_Accessibility_Check_Failed
));
680 -- Step 2: Create the accessibility comparison
686 Make_Attribute_Reference
(Loc
,
688 Attribute_Name
=> Name_Tag
);
690 -- For tagged types, determine the accessibility level by looking
691 -- at the type specific data of the dispatch table. Generate:
693 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
695 if Tagged_Type_Expansion
then
696 Cond
:= Build_Get_Access_Level
(Loc
, Obj_Ref
);
698 -- Use a runtime call to determine the accessibility level when
699 -- compiling on virtual machine targets. Generate:
701 -- Get_Access_Level (Ref'Tag)
705 Make_Function_Call
(Loc
,
707 New_Occurrence_Of
(RTE
(RE_Get_Access_Level
), Loc
),
708 Parameter_Associations
=> New_List
(Obj_Ref
));
715 Make_Integer_Literal
(Loc
, Type_Access_Level
(PtrT
)));
717 -- Due to the complexity and side effects of the check, utilize an
718 -- if statement instead of the regular Program_Error circuitry.
721 Make_Implicit_If_Statement
(N
,
723 Then_Statements
=> Stmts
));
725 end Apply_Accessibility_Check
;
729 Aggr_In_Place
: constant Boolean := Is_Delayed_Aggregate
(Exp
);
730 Indic
: constant Node_Id
:= Subtype_Mark
(Expression
(N
));
731 T
: constant Entity_Id
:= Entity
(Indic
);
734 Tag_Assign
: Node_Id
;
738 TagT
: Entity_Id
:= Empty
;
739 -- Type used as source for tag assignment
741 TagR
: Node_Id
:= Empty
;
742 -- Target reference for tag assignment
744 -- Start of processing for Expand_Allocator_Expression
747 -- Handle call to C++ constructor
749 if Is_CPP_Constructor_Call
(Exp
) then
750 Make_CPP_Constructor_Call_In_Allocator
752 Function_Call
=> Exp
);
756 -- In the case of an Ada 2012 allocator whose initial value comes from a
757 -- function call, pass "the accessibility level determined by the point
758 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
759 -- Expand_Call but it couldn't be done there (because the Etype of the
760 -- allocator wasn't set then) so we generate the parameter here. See
761 -- the Boolean variable Defer in (a block within) Expand_Call.
763 if Ada_Version
>= Ada_2012
and then Nkind
(Exp
) = N_Function_Call
then
768 if Nkind
(Name
(Exp
)) = N_Explicit_Dereference
then
769 Subp
:= Designated_Type
(Etype
(Prefix
(Name
(Exp
))));
771 Subp
:= Entity
(Name
(Exp
));
774 Subp
:= Ultimate_Alias
(Subp
);
776 if Present
(Extra_Accessibility_Of_Result
(Subp
)) then
777 Add_Extra_Actual_To_Call
778 (Subprogram_Call
=> Exp
,
779 Extra_Formal
=> Extra_Accessibility_Of_Result
(Subp
),
780 Extra_Actual
=> Dynamic_Accessibility_Level
(PtrT
));
785 -- Case of tagged type or type requiring finalization
787 if Is_Tagged_Type
(T
) or else Needs_Finalization
(T
) then
789 -- Ada 2005 (AI-318-02): If the initialization expression is a call
790 -- to a build-in-place function, then access to the allocated object
791 -- must be passed to the function. Currently we limit such functions
792 -- to those with constrained limited result subtypes, but eventually
793 -- we plan to expand the allowed forms of functions that are treated
794 -- as build-in-place.
796 if Ada_Version
>= Ada_2005
797 and then Is_Build_In_Place_Function_Call
(Exp
)
799 Make_Build_In_Place_Call_In_Allocator
(N
, Exp
);
800 Apply_Accessibility_Check
(N
, Built_In_Place
=> True);
804 -- Actions inserted before:
805 -- Temp : constant ptr_T := new T'(Expression);
806 -- Temp._tag = T'tag; -- when not class-wide
807 -- [Deep_]Adjust (Temp.all);
809 -- We analyze by hand the new internal allocator to avoid any
810 -- recursion and inappropriate call to Initialize.
812 -- We don't want to remove side effects when the expression must be
813 -- built in place. In the case of a build-in-place function call,
814 -- that could lead to a duplication of the call, which was already
815 -- substituted for the allocator.
817 if not Aggr_In_Place
then
818 Remove_Side_Effects
(Exp
);
821 Temp
:= Make_Temporary
(Loc
, 'P', N
);
823 -- For a class wide allocation generate the following code:
825 -- type Equiv_Record is record ... end record;
826 -- implicit subtype CW is <Class_Wide_Subytpe>;
827 -- temp : PtrT := new CW'(CW!(expr));
829 if Is_Class_Wide_Type
(T
) then
830 Expand_Subtype_From_Expr
(Empty
, T
, Indic
, Exp
);
832 -- Ada 2005 (AI-251): If the expression is a class-wide interface
833 -- object we generate code to move up "this" to reference the
834 -- base of the object before allocating the new object.
836 -- Note that Exp'Address is recursively expanded into a call
837 -- to Base_Address (Exp.Tag)
839 if Is_Class_Wide_Type
(Etype
(Exp
))
840 and then Is_Interface
(Etype
(Exp
))
841 and then Tagged_Type_Expansion
845 Unchecked_Convert_To
(Entity
(Indic
),
846 Make_Explicit_Dereference
(Loc
,
847 Unchecked_Convert_To
(RTE
(RE_Tag_Ptr
),
848 Make_Attribute_Reference
(Loc
,
850 Attribute_Name
=> Name_Address
)))));
854 Unchecked_Convert_To
(Entity
(Indic
), Exp
));
857 Analyze_And_Resolve
(Expression
(N
), Entity
(Indic
));
860 -- Processing for allocators returning non-interface types
862 if not Is_Interface
(Directly_Designated_Type
(PtrT
)) then
863 if Aggr_In_Place
then
865 Make_Object_Declaration
(Loc
,
866 Defining_Identifier
=> Temp
,
867 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
871 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
873 -- Copy the Comes_From_Source flag for the allocator we just
874 -- built, since logically this allocator is a replacement of
875 -- the original allocator node. This is for proper handling of
876 -- restriction No_Implicit_Heap_Allocations.
878 Set_Comes_From_Source
879 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
881 Set_No_Initialization
(Expression
(Temp_Decl
));
882 Insert_Action
(N
, Temp_Decl
);
884 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
885 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
888 Node
:= Relocate_Node
(N
);
892 Make_Object_Declaration
(Loc
,
893 Defining_Identifier
=> Temp
,
894 Constant_Present
=> True,
895 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
898 Insert_Action
(N
, Temp_Decl
);
899 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
902 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
903 -- interface type. In this case we use the type of the qualified
904 -- expression to allocate the object.
908 Def_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
913 Make_Full_Type_Declaration
(Loc
,
914 Defining_Identifier
=> Def_Id
,
916 Make_Access_To_Object_Definition
(Loc
,
918 Null_Exclusion_Present
=> False,
920 Is_Access_Constant
(Etype
(N
)),
921 Subtype_Indication
=>
922 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
924 Insert_Action
(N
, New_Decl
);
926 -- Inherit the allocation-related attributes from the original
929 Set_Finalization_Master
930 (Def_Id
, Finalization_Master
(PtrT
));
932 Set_Associated_Storage_Pool
933 (Def_Id
, Associated_Storage_Pool
(PtrT
));
935 -- Declare the object using the previous type declaration
937 if Aggr_In_Place
then
939 Make_Object_Declaration
(Loc
,
940 Defining_Identifier
=> Temp
,
941 Object_Definition
=> New_Occurrence_Of
(Def_Id
, Loc
),
944 New_Occurrence_Of
(Etype
(Exp
), Loc
)));
946 -- Copy the Comes_From_Source flag for the allocator we just
947 -- built, since logically this allocator is a replacement of
948 -- the original allocator node. This is for proper handling
949 -- of restriction No_Implicit_Heap_Allocations.
951 Set_Comes_From_Source
952 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
954 Set_No_Initialization
(Expression
(Temp_Decl
));
955 Insert_Action
(N
, Temp_Decl
);
957 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
958 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
961 Node
:= Relocate_Node
(N
);
965 Make_Object_Declaration
(Loc
,
966 Defining_Identifier
=> Temp
,
967 Constant_Present
=> True,
968 Object_Definition
=> New_Occurrence_Of
(Def_Id
, Loc
),
971 Insert_Action
(N
, Temp_Decl
);
972 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
975 -- Generate an additional object containing the address of the
976 -- returned object. The type of this second object declaration
977 -- is the correct type required for the common processing that
978 -- is still performed by this subprogram. The displacement of
979 -- this pointer to reference the component associated with the
980 -- interface type will be done at the end of common processing.
983 Make_Object_Declaration
(Loc
,
984 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
985 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
987 Unchecked_Convert_To
(PtrT
,
988 New_Occurrence_Of
(Temp
, Loc
)));
990 Insert_Action
(N
, New_Decl
);
992 Temp_Decl
:= New_Decl
;
993 Temp
:= Defining_Identifier
(New_Decl
);
997 -- Generate the tag assignment
999 -- Suppress the tag assignment for VM targets because VM tags are
1000 -- represented implicitly in objects.
1002 if not Tagged_Type_Expansion
then
1005 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1006 -- interface objects because in this case the tag does not change.
1008 elsif Is_Interface
(Directly_Designated_Type
(Etype
(N
))) then
1009 pragma Assert
(Is_Class_Wide_Type
1010 (Directly_Designated_Type
(Etype
(N
))));
1013 elsif Is_Tagged_Type
(T
) and then not Is_Class_Wide_Type
(T
) then
1015 TagR
:= New_Occurrence_Of
(Temp
, Loc
);
1017 elsif Is_Private_Type
(T
)
1018 and then Is_Tagged_Type
(Underlying_Type
(T
))
1020 TagT
:= Underlying_Type
(T
);
1022 Unchecked_Convert_To
(Underlying_Type
(T
),
1023 Make_Explicit_Dereference
(Loc
,
1024 Prefix
=> New_Occurrence_Of
(Temp
, Loc
)));
1027 if Present
(TagT
) then
1029 Full_T
: constant Entity_Id
:= Underlying_Type
(TagT
);
1033 Make_Assignment_Statement
(Loc
,
1035 Make_Selected_Component
(Loc
,
1039 (First_Tag_Component
(Full_T
), Loc
)),
1042 Unchecked_Convert_To
(RTE
(RE_Tag
),
1045 (First_Elmt
(Access_Disp_Table
(Full_T
))), Loc
)));
1048 -- The previous assignment has to be done in any case
1050 Set_Assignment_OK
(Name
(Tag_Assign
));
1051 Insert_Action
(N
, Tag_Assign
);
1054 -- Generate an Adjust call if the object will be moved. In Ada 2005,
1055 -- the object may be inherently limited, in which case there is no
1056 -- Adjust procedure, and the object is built in place. In Ada 95, the
1057 -- object can be limited but not inherently limited if this allocator
1058 -- came from a return statement (we're allocating the result on the
1059 -- secondary stack). In that case, the object will be moved, so we do
1062 if Needs_Finalization
(DesigT
)
1063 and then Needs_Finalization
(T
)
1064 and then not Aggr_In_Place
1065 and then not Is_Limited_View
(T
)
1067 -- An unchecked conversion is needed in the classwide case because
1068 -- the designated type can be an ancestor of the subtype mark of
1074 Unchecked_Convert_To
(T
,
1075 Make_Explicit_Dereference
(Loc
,
1076 Prefix
=> New_Occurrence_Of
(Temp
, Loc
))),
1079 if Present
(Adj_Call
) then
1080 Insert_Action
(N
, Adj_Call
);
1084 -- Note: the accessibility check must be inserted after the call to
1085 -- [Deep_]Adjust to ensure proper completion of the assignment.
1087 Apply_Accessibility_Check
(Temp
);
1089 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
1090 Analyze_And_Resolve
(N
, PtrT
);
1092 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1093 -- component containing the secondary dispatch table of the interface
1096 if Is_Interface
(Directly_Designated_Type
(PtrT
)) then
1097 Displace_Allocator_Pointer
(N
);
1100 -- Always force the generation of a temporary for aggregates when
1101 -- generating C code, to simplify the work in the code generator.
1104 or else (Modify_Tree_For_C
and then Nkind
(Exp
) = N_Aggregate
)
1106 Temp
:= Make_Temporary
(Loc
, 'P', N
);
1108 Make_Object_Declaration
(Loc
,
1109 Defining_Identifier
=> Temp
,
1110 Object_Definition
=> New_Occurrence_Of
(PtrT
, Loc
),
1112 Make_Allocator
(Loc
,
1113 Expression
=> New_Occurrence_Of
(Etype
(Exp
), Loc
)));
1115 -- Copy the Comes_From_Source flag for the allocator we just built,
1116 -- since logically this allocator is a replacement of the original
1117 -- allocator node. This is for proper handling of restriction
1118 -- No_Implicit_Heap_Allocations.
1120 Set_Comes_From_Source
1121 (Expression
(Temp_Decl
), Comes_From_Source
(N
));
1123 Set_No_Initialization
(Expression
(Temp_Decl
));
1124 Insert_Action
(N
, Temp_Decl
);
1126 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
1127 Convert_Aggr_In_Allocator
(N
, Temp_Decl
, Exp
);
1129 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
1130 Analyze_And_Resolve
(N
, PtrT
);
1132 elsif Is_Access_Type
(T
) and then Can_Never_Be_Null
(T
) then
1133 Install_Null_Excluding_Check
(Exp
);
1135 elsif Is_Access_Type
(DesigT
)
1136 and then Nkind
(Exp
) = N_Allocator
1137 and then Nkind
(Expression
(Exp
)) /= N_Qualified_Expression
1139 -- Apply constraint to designated subtype indication
1141 Apply_Constraint_Check
1142 (Expression
(Exp
), Designated_Type
(DesigT
), No_Sliding
=> True);
1144 if Nkind
(Expression
(Exp
)) = N_Raise_Constraint_Error
then
1146 -- Propagate constraint_error to enclosing allocator
1148 Rewrite
(Exp
, New_Copy
(Expression
(Exp
)));
1152 Build_Allocate_Deallocate_Proc
(N
, True);
1155 -- type A is access T1;
1156 -- X : A := new T2'(...);
1157 -- T1 and T2 can be different subtypes, and we might need to check
1158 -- both constraints. First check against the type of the qualified
1161 Apply_Constraint_Check
(Exp
, T
, No_Sliding
=> True);
1163 if Do_Range_Check
(Exp
) then
1164 Generate_Range_Check
(Exp
, DesigT
, CE_Range_Check_Failed
);
1167 -- A check is also needed in cases where the designated subtype is
1168 -- constrained and differs from the subtype given in the qualified
1169 -- expression. Note that the check on the qualified expression does
1170 -- not allow sliding, but this check does (a relaxation from Ada 83).
1172 if Is_Constrained
(DesigT
)
1173 and then not Subtypes_Statically_Match
(T
, DesigT
)
1175 Apply_Constraint_Check
1176 (Exp
, DesigT
, No_Sliding
=> False);
1178 if Do_Range_Check
(Exp
) then
1179 Generate_Range_Check
(Exp
, DesigT
, CE_Range_Check_Failed
);
1183 -- For an access to unconstrained packed array, GIGI needs to see an
1184 -- expression with a constrained subtype in order to compute the
1185 -- proper size for the allocator.
1187 if Is_Array_Type
(T
)
1188 and then not Is_Constrained
(T
)
1189 and then Is_Packed
(T
)
1192 ConstrT
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
1193 Internal_Exp
: constant Node_Id
:= Relocate_Node
(Exp
);
1196 Make_Subtype_Declaration
(Loc
,
1197 Defining_Identifier
=> ConstrT
,
1198 Subtype_Indication
=>
1199 Make_Subtype_From_Expr
(Internal_Exp
, T
)));
1200 Freeze_Itype
(ConstrT
, Exp
);
1201 Rewrite
(Exp
, OK_Convert_To
(ConstrT
, Internal_Exp
));
1205 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1206 -- to a build-in-place function, then access to the allocated object
1207 -- must be passed to the function. Currently we limit such functions
1208 -- to those with constrained limited result subtypes, but eventually
1209 -- we plan to expand the allowed forms of functions that are treated
1210 -- as build-in-place.
1212 if Ada_Version
>= Ada_2005
1213 and then Is_Build_In_Place_Function_Call
(Exp
)
1215 Make_Build_In_Place_Call_In_Allocator
(N
, Exp
);
1220 when RE_Not_Available
=>
1222 end Expand_Allocator_Expression
;
1224 -----------------------------
1225 -- Expand_Array_Comparison --
1226 -----------------------------
1228 -- Expansion is only required in the case of array types. For the unpacked
1229 -- case, an appropriate runtime routine is called. For packed cases, and
1230 -- also in some other cases where a runtime routine cannot be called, the
1231 -- form of the expansion is:
1233 -- [body for greater_nn; boolean_expression]
1235 -- The body is built by Make_Array_Comparison_Op, and the form of the
1236 -- Boolean expression depends on the operator involved.
1238 procedure Expand_Array_Comparison
(N
: Node_Id
) is
1239 Loc
: constant Source_Ptr
:= Sloc
(N
);
1240 Op1
: Node_Id
:= Left_Opnd
(N
);
1241 Op2
: Node_Id
:= Right_Opnd
(N
);
1242 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
1243 Ctyp
: constant Entity_Id
:= Component_Type
(Typ1
);
1246 Func_Body
: Node_Id
;
1247 Func_Name
: Entity_Id
;
1251 Byte_Addressable
: constant Boolean := System_Storage_Unit
= Byte
'Size;
1252 -- True for byte addressable target
1254 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean;
1255 -- Returns True if the length of the given operand is known to be less
1256 -- than 4. Returns False if this length is known to be four or greater
1257 -- or is not known at compile time.
1259 ------------------------
1260 -- Length_Less_Than_4 --
1261 ------------------------
1263 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean is
1264 Otyp
: constant Entity_Id
:= Etype
(Opnd
);
1267 if Ekind
(Otyp
) = E_String_Literal_Subtype
then
1268 return String_Literal_Length
(Otyp
) < 4;
1272 Ityp
: constant Entity_Id
:= Etype
(First_Index
(Otyp
));
1273 Lo
: constant Node_Id
:= Type_Low_Bound
(Ityp
);
1274 Hi
: constant Node_Id
:= Type_High_Bound
(Ityp
);
1279 if Compile_Time_Known_Value
(Lo
) then
1280 Lov
:= Expr_Value
(Lo
);
1285 if Compile_Time_Known_Value
(Hi
) then
1286 Hiv
:= Expr_Value
(Hi
);
1291 return Hiv
< Lov
+ 3;
1294 end Length_Less_Than_4
;
1296 -- Start of processing for Expand_Array_Comparison
1299 -- Deal first with unpacked case, where we can call a runtime routine
1300 -- except that we avoid this for targets for which are not addressable
1303 if not Is_Bit_Packed_Array
(Typ1
)
1304 and then Byte_Addressable
1306 -- The call we generate is:
1308 -- Compare_Array_xn[_Unaligned]
1309 -- (left'address, right'address, left'length, right'length) <op> 0
1311 -- x = U for unsigned, S for signed
1312 -- n = 8,16,32,64 for component size
1313 -- Add _Unaligned if length < 4 and component size is 8.
1314 -- <op> is the standard comparison operator
1316 if Component_Size
(Typ1
) = 8 then
1317 if Length_Less_Than_4
(Op1
)
1319 Length_Less_Than_4
(Op2
)
1321 if Is_Unsigned_Type
(Ctyp
) then
1322 Comp
:= RE_Compare_Array_U8_Unaligned
;
1324 Comp
:= RE_Compare_Array_S8_Unaligned
;
1328 if Is_Unsigned_Type
(Ctyp
) then
1329 Comp
:= RE_Compare_Array_U8
;
1331 Comp
:= RE_Compare_Array_S8
;
1335 elsif Component_Size
(Typ1
) = 16 then
1336 if Is_Unsigned_Type
(Ctyp
) then
1337 Comp
:= RE_Compare_Array_U16
;
1339 Comp
:= RE_Compare_Array_S16
;
1342 elsif Component_Size
(Typ1
) = 32 then
1343 if Is_Unsigned_Type
(Ctyp
) then
1344 Comp
:= RE_Compare_Array_U32
;
1346 Comp
:= RE_Compare_Array_S32
;
1349 else pragma Assert
(Component_Size
(Typ1
) = 64);
1350 if Is_Unsigned_Type
(Ctyp
) then
1351 Comp
:= RE_Compare_Array_U64
;
1353 Comp
:= RE_Compare_Array_S64
;
1357 if RTE_Available
(Comp
) then
1359 -- Expand to a call only if the runtime function is available,
1360 -- otherwise fall back to inline code.
1362 Remove_Side_Effects
(Op1
, Name_Req
=> True);
1363 Remove_Side_Effects
(Op2
, Name_Req
=> True);
1366 Make_Function_Call
(Sloc
(Op1
),
1367 Name
=> New_Occurrence_Of
(RTE
(Comp
), Loc
),
1369 Parameter_Associations
=> New_List
(
1370 Make_Attribute_Reference
(Loc
,
1371 Prefix
=> Relocate_Node
(Op1
),
1372 Attribute_Name
=> Name_Address
),
1374 Make_Attribute_Reference
(Loc
,
1375 Prefix
=> Relocate_Node
(Op2
),
1376 Attribute_Name
=> Name_Address
),
1378 Make_Attribute_Reference
(Loc
,
1379 Prefix
=> Relocate_Node
(Op1
),
1380 Attribute_Name
=> Name_Length
),
1382 Make_Attribute_Reference
(Loc
,
1383 Prefix
=> Relocate_Node
(Op2
),
1384 Attribute_Name
=> Name_Length
))));
1387 Make_Integer_Literal
(Sloc
(Op2
),
1390 Analyze_And_Resolve
(Op1
, Standard_Integer
);
1391 Analyze_And_Resolve
(Op2
, Standard_Integer
);
1396 -- Cases where we cannot make runtime call
1398 -- For (a <= b) we convert to not (a > b)
1400 if Chars
(N
) = Name_Op_Le
then
1406 Right_Opnd
=> Op2
)));
1407 Analyze_And_Resolve
(N
, Standard_Boolean
);
1410 -- For < the Boolean expression is
1411 -- greater__nn (op2, op1)
1413 elsif Chars
(N
) = Name_Op_Lt
then
1414 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
1418 Op1
:= Right_Opnd
(N
);
1419 Op2
:= Left_Opnd
(N
);
1421 -- For (a >= b) we convert to not (a < b)
1423 elsif Chars
(N
) = Name_Op_Ge
then
1429 Right_Opnd
=> Op2
)));
1430 Analyze_And_Resolve
(N
, Standard_Boolean
);
1433 -- For > the Boolean expression is
1434 -- greater__nn (op1, op2)
1437 pragma Assert
(Chars
(N
) = Name_Op_Gt
);
1438 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
1441 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
1443 Make_Function_Call
(Loc
,
1444 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
1445 Parameter_Associations
=> New_List
(Op1
, Op2
));
1447 Insert_Action
(N
, Func_Body
);
1449 Analyze_And_Resolve
(N
, Standard_Boolean
);
1450 end Expand_Array_Comparison
;
1452 ---------------------------
1453 -- Expand_Array_Equality --
1454 ---------------------------
1456 -- Expand an equality function for multi-dimensional arrays. Here is an
1457 -- example of such a function for Nb_Dimension = 2
1459 -- function Enn (A : atyp; B : btyp) return boolean is
1461 -- if (A'length (1) = 0 or else A'length (2) = 0)
1463 -- (B'length (1) = 0 or else B'length (2) = 0)
1465 -- return True; -- RM 4.5.2(22)
1468 -- if A'length (1) /= B'length (1)
1470 -- A'length (2) /= B'length (2)
1472 -- return False; -- RM 4.5.2(23)
1476 -- A1 : Index_T1 := A'first (1);
1477 -- B1 : Index_T1 := B'first (1);
1481 -- A2 : Index_T2 := A'first (2);
1482 -- B2 : Index_T2 := B'first (2);
1485 -- if A (A1, A2) /= B (B1, B2) then
1489 -- exit when A2 = A'last (2);
1490 -- A2 := Index_T2'succ (A2);
1491 -- B2 := Index_T2'succ (B2);
1495 -- exit when A1 = A'last (1);
1496 -- A1 := Index_T1'succ (A1);
1497 -- B1 := Index_T1'succ (B1);
1504 -- Note on the formal types used (atyp and btyp). If either of the arrays
1505 -- is of a private type, we use the underlying type, and do an unchecked
1506 -- conversion of the actual. If either of the arrays has a bound depending
1507 -- on a discriminant, then we use the base type since otherwise we have an
1508 -- escaped discriminant in the function.
1510 -- If both arrays are constrained and have the same bounds, we can generate
1511 -- a loop with an explicit iteration scheme using a 'Range attribute over
1514 function Expand_Array_Equality
1519 Typ
: Entity_Id
) return Node_Id
1521 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
1522 Decls
: constant List_Id
:= New_List
;
1523 Index_List1
: constant List_Id
:= New_List
;
1524 Index_List2
: constant List_Id
:= New_List
;
1528 Func_Name
: Entity_Id
;
1529 Func_Body
: Node_Id
;
1531 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
1532 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
1536 -- The parameter types to be used for the formals
1541 Num
: Int
) return Node_Id
;
1542 -- This builds the attribute reference Arr'Nam (Expr)
1544 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
;
1545 -- Create one statement to compare corresponding components, designated
1546 -- by a full set of indexes.
1548 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
;
1549 -- Given one of the arguments, computes the appropriate type to be used
1550 -- for that argument in the corresponding function formal
1552 function Handle_One_Dimension
1554 Index
: Node_Id
) return Node_Id
;
1555 -- This procedure returns the following code
1558 -- Bn : Index_T := B'First (N);
1562 -- exit when An = A'Last (N);
1563 -- An := Index_T'Succ (An)
1564 -- Bn := Index_T'Succ (Bn)
1568 -- If both indexes are constrained and identical, the procedure
1569 -- returns a simpler loop:
1571 -- for An in A'Range (N) loop
1575 -- N is the dimension for which we are generating a loop. Index is the
1576 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1577 -- xxx statement is either the loop or declare for the next dimension
1578 -- or if this is the last dimension the comparison of corresponding
1579 -- components of the arrays.
1581 -- The actual way the code works is to return the comparison of
1582 -- corresponding components for the N+1 call. That's neater.
1584 function Test_Empty_Arrays
return Node_Id
;
1585 -- This function constructs the test for both arrays being empty
1586 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1588 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1590 function Test_Lengths_Correspond
return Node_Id
;
1591 -- This function constructs the test for arrays having different lengths
1592 -- in at least one index position, in which case the resulting code is:
1594 -- A'length (1) /= B'length (1)
1596 -- A'length (2) /= B'length (2)
1607 Num
: Int
) return Node_Id
1611 Make_Attribute_Reference
(Loc
,
1612 Attribute_Name
=> Nam
,
1613 Prefix
=> New_Occurrence_Of
(Arr
, Loc
),
1614 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, Num
)));
1617 ------------------------
1618 -- Component_Equality --
1619 ------------------------
1621 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
is
1626 -- if a(i1...) /= b(j1...) then return false; end if;
1629 Make_Indexed_Component
(Loc
,
1630 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
1631 Expressions
=> Index_List1
);
1634 Make_Indexed_Component
(Loc
,
1635 Prefix
=> Make_Identifier
(Loc
, Chars
(B
)),
1636 Expressions
=> Index_List2
);
1638 Test
:= Expand_Composite_Equality
1639 (Nod
, Component_Type
(Typ
), L
, R
, Decls
);
1641 -- If some (sub)component is an unchecked_union, the whole operation
1642 -- will raise program error.
1644 if Nkind
(Test
) = N_Raise_Program_Error
then
1646 -- This node is going to be inserted at a location where a
1647 -- statement is expected: clear its Etype so analysis will set
1648 -- it to the expected Standard_Void_Type.
1650 Set_Etype
(Test
, Empty
);
1655 Make_Implicit_If_Statement
(Nod
,
1656 Condition
=> Make_Op_Not
(Loc
, Right_Opnd
=> Test
),
1657 Then_Statements
=> New_List
(
1658 Make_Simple_Return_Statement
(Loc
,
1659 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))));
1661 end Component_Equality
;
1667 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
is
1678 T
:= Underlying_Type
(T
);
1680 X
:= First_Index
(T
);
1681 while Present
(X
) loop
1682 if Denotes_Discriminant
(Type_Low_Bound
(Etype
(X
)))
1684 Denotes_Discriminant
(Type_High_Bound
(Etype
(X
)))
1697 --------------------------
1698 -- Handle_One_Dimension --
1699 ---------------------------
1701 function Handle_One_Dimension
1703 Index
: Node_Id
) return Node_Id
1705 Need_Separate_Indexes
: constant Boolean :=
1706 Ltyp
/= Rtyp
or else not Is_Constrained
(Ltyp
);
1707 -- If the index types are identical, and we are working with
1708 -- constrained types, then we can use the same index for both
1711 An
: constant Entity_Id
:= Make_Temporary
(Loc
, 'A');
1714 Index_T
: Entity_Id
;
1719 if N
> Number_Dimensions
(Ltyp
) then
1720 return Component_Equality
(Ltyp
);
1723 -- Case where we generate a loop
1725 Index_T
:= Base_Type
(Etype
(Index
));
1727 if Need_Separate_Indexes
then
1728 Bn
:= Make_Temporary
(Loc
, 'B');
1733 Append
(New_Occurrence_Of
(An
, Loc
), Index_List1
);
1734 Append
(New_Occurrence_Of
(Bn
, Loc
), Index_List2
);
1736 Stm_List
:= New_List
(
1737 Handle_One_Dimension
(N
+ 1, Next_Index
(Index
)));
1739 if Need_Separate_Indexes
then
1741 -- Generate guard for loop, followed by increments of indexes
1743 Append_To
(Stm_List
,
1744 Make_Exit_Statement
(Loc
,
1747 Left_Opnd
=> New_Occurrence_Of
(An
, Loc
),
1748 Right_Opnd
=> Arr_Attr
(A
, Name_Last
, N
))));
1750 Append_To
(Stm_List
,
1751 Make_Assignment_Statement
(Loc
,
1752 Name
=> New_Occurrence_Of
(An
, Loc
),
1754 Make_Attribute_Reference
(Loc
,
1755 Prefix
=> New_Occurrence_Of
(Index_T
, Loc
),
1756 Attribute_Name
=> Name_Succ
,
1757 Expressions
=> New_List
(
1758 New_Occurrence_Of
(An
, Loc
)))));
1760 Append_To
(Stm_List
,
1761 Make_Assignment_Statement
(Loc
,
1762 Name
=> New_Occurrence_Of
(Bn
, Loc
),
1764 Make_Attribute_Reference
(Loc
,
1765 Prefix
=> New_Occurrence_Of
(Index_T
, Loc
),
1766 Attribute_Name
=> Name_Succ
,
1767 Expressions
=> New_List
(
1768 New_Occurrence_Of
(Bn
, Loc
)))));
1771 -- If separate indexes, we need a declare block for An and Bn, and a
1772 -- loop without an iteration scheme.
1774 if Need_Separate_Indexes
then
1776 Make_Implicit_Loop_Statement
(Nod
, Statements
=> Stm_List
);
1779 Make_Block_Statement
(Loc
,
1780 Declarations
=> New_List
(
1781 Make_Object_Declaration
(Loc
,
1782 Defining_Identifier
=> An
,
1783 Object_Definition
=> New_Occurrence_Of
(Index_T
, Loc
),
1784 Expression
=> Arr_Attr
(A
, Name_First
, N
)),
1786 Make_Object_Declaration
(Loc
,
1787 Defining_Identifier
=> Bn
,
1788 Object_Definition
=> New_Occurrence_Of
(Index_T
, Loc
),
1789 Expression
=> Arr_Attr
(B
, Name_First
, N
))),
1791 Handled_Statement_Sequence
=>
1792 Make_Handled_Sequence_Of_Statements
(Loc
,
1793 Statements
=> New_List
(Loop_Stm
)));
1795 -- If no separate indexes, return loop statement with explicit
1796 -- iteration scheme on its own
1800 Make_Implicit_Loop_Statement
(Nod
,
1801 Statements
=> Stm_List
,
1803 Make_Iteration_Scheme
(Loc
,
1804 Loop_Parameter_Specification
=>
1805 Make_Loop_Parameter_Specification
(Loc
,
1806 Defining_Identifier
=> An
,
1807 Discrete_Subtype_Definition
=>
1808 Arr_Attr
(A
, Name_Range
, N
))));
1811 end Handle_One_Dimension
;
1813 -----------------------
1814 -- Test_Empty_Arrays --
1815 -----------------------
1817 function Test_Empty_Arrays
return Node_Id
is
1827 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
1830 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
1831 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
1835 Left_Opnd
=> Arr_Attr
(B
, Name_Length
, J
),
1836 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
1845 Left_Opnd
=> Relocate_Node
(Alist
),
1846 Right_Opnd
=> Atest
);
1850 Left_Opnd
=> Relocate_Node
(Blist
),
1851 Right_Opnd
=> Btest
);
1858 Right_Opnd
=> Blist
);
1859 end Test_Empty_Arrays
;
1861 -----------------------------
1862 -- Test_Lengths_Correspond --
1863 -----------------------------
1865 function Test_Lengths_Correspond
return Node_Id
is
1871 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
1874 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
1875 Right_Opnd
=> Arr_Attr
(B
, Name_Length
, J
));
1882 Left_Opnd
=> Relocate_Node
(Result
),
1883 Right_Opnd
=> Rtest
);
1888 end Test_Lengths_Correspond
;
1890 -- Start of processing for Expand_Array_Equality
1893 Ltyp
:= Get_Arg_Type
(Lhs
);
1894 Rtyp
:= Get_Arg_Type
(Rhs
);
1896 -- For now, if the argument types are not the same, go to the base type,
1897 -- since the code assumes that the formals have the same type. This is
1898 -- fixable in future ???
1900 if Ltyp
/= Rtyp
then
1901 Ltyp
:= Base_Type
(Ltyp
);
1902 Rtyp
:= Base_Type
(Rtyp
);
1903 pragma Assert
(Ltyp
= Rtyp
);
1906 -- Build list of formals for function
1908 Formals
:= New_List
(
1909 Make_Parameter_Specification
(Loc
,
1910 Defining_Identifier
=> A
,
1911 Parameter_Type
=> New_Occurrence_Of
(Ltyp
, Loc
)),
1913 Make_Parameter_Specification
(Loc
,
1914 Defining_Identifier
=> B
,
1915 Parameter_Type
=> New_Occurrence_Of
(Rtyp
, Loc
)));
1917 Func_Name
:= Make_Temporary
(Loc
, 'E');
1919 -- Build statement sequence for function
1922 Make_Subprogram_Body
(Loc
,
1924 Make_Function_Specification
(Loc
,
1925 Defining_Unit_Name
=> Func_Name
,
1926 Parameter_Specifications
=> Formals
,
1927 Result_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
)),
1929 Declarations
=> Decls
,
1931 Handled_Statement_Sequence
=>
1932 Make_Handled_Sequence_Of_Statements
(Loc
,
1933 Statements
=> New_List
(
1935 Make_Implicit_If_Statement
(Nod
,
1936 Condition
=> Test_Empty_Arrays
,
1937 Then_Statements
=> New_List
(
1938 Make_Simple_Return_Statement
(Loc
,
1940 New_Occurrence_Of
(Standard_True
, Loc
)))),
1942 Make_Implicit_If_Statement
(Nod
,
1943 Condition
=> Test_Lengths_Correspond
,
1944 Then_Statements
=> New_List
(
1945 Make_Simple_Return_Statement
(Loc
,
1946 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
)))),
1948 Handle_One_Dimension
(1, First_Index
(Ltyp
)),
1950 Make_Simple_Return_Statement
(Loc
,
1951 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)))));
1953 Set_Has_Completion
(Func_Name
, True);
1954 Set_Is_Inlined
(Func_Name
);
1956 -- If the array type is distinct from the type of the arguments, it
1957 -- is the full view of a private type. Apply an unchecked conversion
1958 -- to insure that analysis of the call succeeds.
1968 or else Base_Type
(Etype
(Lhs
)) /= Base_Type
(Ltyp
)
1970 L
:= OK_Convert_To
(Ltyp
, Lhs
);
1974 or else Base_Type
(Etype
(Rhs
)) /= Base_Type
(Rtyp
)
1976 R
:= OK_Convert_To
(Rtyp
, Rhs
);
1979 Actuals
:= New_List
(L
, R
);
1982 Append_To
(Bodies
, Func_Body
);
1985 Make_Function_Call
(Loc
,
1986 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
1987 Parameter_Associations
=> Actuals
);
1988 end Expand_Array_Equality
;
1990 -----------------------------
1991 -- Expand_Boolean_Operator --
1992 -----------------------------
1994 -- Note that we first get the actual subtypes of the operands, since we
1995 -- always want to deal with types that have bounds.
1997 procedure Expand_Boolean_Operator
(N
: Node_Id
) is
1998 Typ
: constant Entity_Id
:= Etype
(N
);
2001 -- Special case of bit packed array where both operands are known to be
2002 -- properly aligned. In this case we use an efficient run time routine
2003 -- to carry out the operation (see System.Bit_Ops).
2005 if Is_Bit_Packed_Array
(Typ
)
2006 and then not Is_Possibly_Unaligned_Object
(Left_Opnd
(N
))
2007 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
2009 Expand_Packed_Boolean_Operator
(N
);
2013 -- For the normal non-packed case, the general expansion is to build
2014 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2015 -- and then inserting it into the tree. The original operator node is
2016 -- then rewritten as a call to this function. We also use this in the
2017 -- packed case if either operand is a possibly unaligned object.
2020 Loc
: constant Source_Ptr
:= Sloc
(N
);
2021 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
2022 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
2023 Func_Body
: Node_Id
;
2024 Func_Name
: Entity_Id
;
2027 Convert_To_Actual_Subtype
(L
);
2028 Convert_To_Actual_Subtype
(R
);
2029 Ensure_Defined
(Etype
(L
), N
);
2030 Ensure_Defined
(Etype
(R
), N
);
2031 Apply_Length_Check
(R
, Etype
(L
));
2033 if Nkind
(N
) = N_Op_Xor
then
2034 Silly_Boolean_Array_Xor_Test
(N
, Etype
(L
));
2037 if Nkind
(Parent
(N
)) = N_Assignment_Statement
2038 and then Safe_In_Place_Array_Op
(Name
(Parent
(N
)), L
, R
)
2040 Build_Boolean_Array_Proc_Call
(Parent
(N
), L
, R
);
2042 elsif Nkind
(Parent
(N
)) = N_Op_Not
2043 and then Nkind
(N
) = N_Op_And
2044 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
2045 and then Safe_In_Place_Array_Op
(Name
(Parent
(Parent
(N
))), L
, R
)
2050 Func_Body
:= Make_Boolean_Array_Op
(Etype
(L
), N
);
2051 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
2052 Insert_Action
(N
, Func_Body
);
2054 -- Now rewrite the expression with a call
2057 Make_Function_Call
(Loc
,
2058 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
2059 Parameter_Associations
=>
2062 Make_Type_Conversion
2063 (Loc
, New_Occurrence_Of
(Etype
(L
), Loc
), R
))));
2065 Analyze_And_Resolve
(N
, Typ
);
2068 end Expand_Boolean_Operator
;
2070 ------------------------------------------------
2071 -- Expand_Compare_Minimize_Eliminate_Overflow --
2072 ------------------------------------------------
2074 procedure Expand_Compare_Minimize_Eliminate_Overflow
(N
: Node_Id
) is
2075 Loc
: constant Source_Ptr
:= Sloc
(N
);
2077 Result_Type
: constant Entity_Id
:= Etype
(N
);
2078 -- Capture result type (could be a derived boolean type)
2083 LLIB
: constant Entity_Id
:= Base_Type
(Standard_Long_Long_Integer
);
2084 -- Entity for Long_Long_Integer'Base
2086 Check
: constant Overflow_Mode_Type
:= Overflow_Check_Mode
;
2087 -- Current overflow checking mode
2090 procedure Set_False
;
2091 -- These procedures rewrite N with an occurrence of Standard_True or
2092 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2098 procedure Set_False
is
2100 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
2101 Warn_On_Known_Condition
(N
);
2108 procedure Set_True
is
2110 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
2111 Warn_On_Known_Condition
(N
);
2114 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2117 -- Nothing to do unless we have a comparison operator with operands
2118 -- that are signed integer types, and we are operating in either
2119 -- MINIMIZED or ELIMINATED overflow checking mode.
2121 if Nkind
(N
) not in N_Op_Compare
2122 or else Check
not in Minimized_Or_Eliminated
2123 or else not Is_Signed_Integer_Type
(Etype
(Left_Opnd
(N
)))
2128 -- OK, this is the case we are interested in. First step is to process
2129 -- our operands using the Minimize_Eliminate circuitry which applies
2130 -- this processing to the two operand subtrees.
2132 Minimize_Eliminate_Overflows
2133 (Left_Opnd
(N
), Llo
, Lhi
, Top_Level
=> False);
2134 Minimize_Eliminate_Overflows
2135 (Right_Opnd
(N
), Rlo
, Rhi
, Top_Level
=> False);
2137 -- See if the range information decides the result of the comparison.
2138 -- We can only do this if we in fact have full range information (which
2139 -- won't be the case if either operand is bignum at this stage).
2141 if Llo
/= No_Uint
and then Rlo
/= No_Uint
then
2142 case N_Op_Compare
(Nkind
(N
)) is
2144 if Llo
= Lhi
and then Rlo
= Rhi
and then Llo
= Rlo
then
2146 elsif Llo
> Rhi
or else Lhi
< Rlo
then
2153 elsif Lhi
< Rlo
then
2160 elsif Lhi
<= Rlo
then
2167 elsif Lhi
<= Rlo
then
2174 elsif Lhi
< Rlo
then
2179 if Llo
= Lhi
and then Rlo
= Rhi
and then Llo
= Rlo
then
2181 elsif Llo
> Rhi
or else Lhi
< Rlo
then
2186 -- All done if we did the rewrite
2188 if Nkind
(N
) not in N_Op_Compare
then
2193 -- Otherwise, time to do the comparison
2196 Ltype
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
2197 Rtype
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
2200 -- If the two operands have the same signed integer type we are
2201 -- all set, nothing more to do. This is the case where either
2202 -- both operands were unchanged, or we rewrote both of them to
2203 -- be Long_Long_Integer.
2205 -- Note: Entity for the comparison may be wrong, but it's not worth
2206 -- the effort to change it, since the back end does not use it.
2208 if Is_Signed_Integer_Type
(Ltype
)
2209 and then Base_Type
(Ltype
) = Base_Type
(Rtype
)
2213 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2215 elsif Is_RTE
(Ltype
, RE_Bignum
) or else Is_RTE
(Rtype
, RE_Bignum
) then
2217 Left
: Node_Id
:= Left_Opnd
(N
);
2218 Right
: Node_Id
:= Right_Opnd
(N
);
2219 -- Bignum references for left and right operands
2222 if not Is_RTE
(Ltype
, RE_Bignum
) then
2223 Left
:= Convert_To_Bignum
(Left
);
2224 elsif not Is_RTE
(Rtype
, RE_Bignum
) then
2225 Right
:= Convert_To_Bignum
(Right
);
2228 -- We rewrite our node with:
2231 -- Bnn : Result_Type;
2233 -- M : Mark_Id := SS_Mark;
2235 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2243 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
2244 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
2248 case N_Op_Compare
(Nkind
(N
)) is
2249 when N_Op_Eq
=> Ent
:= RE_Big_EQ
;
2250 when N_Op_Ge
=> Ent
:= RE_Big_GE
;
2251 when N_Op_Gt
=> Ent
:= RE_Big_GT
;
2252 when N_Op_Le
=> Ent
:= RE_Big_LE
;
2253 when N_Op_Lt
=> Ent
:= RE_Big_LT
;
2254 when N_Op_Ne
=> Ent
:= RE_Big_NE
;
2257 -- Insert assignment to Bnn into the bignum block
2260 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
2261 Make_Assignment_Statement
(Loc
,
2262 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
2264 Make_Function_Call
(Loc
,
2266 New_Occurrence_Of
(RTE
(Ent
), Loc
),
2267 Parameter_Associations
=> New_List
(Left
, Right
))));
2269 -- Now do the rewrite with expression actions
2272 Make_Expression_With_Actions
(Loc
,
2273 Actions
=> New_List
(
2274 Make_Object_Declaration
(Loc
,
2275 Defining_Identifier
=> Bnn
,
2276 Object_Definition
=>
2277 New_Occurrence_Of
(Result_Type
, Loc
)),
2279 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
2280 Analyze_And_Resolve
(N
, Result_Type
);
2284 -- No bignums involved, but types are different, so we must have
2285 -- rewritten one of the operands as a Long_Long_Integer but not
2288 -- If left operand is Long_Long_Integer, convert right operand
2289 -- and we are done (with a comparison of two Long_Long_Integers).
2291 elsif Ltype
= LLIB
then
2292 Convert_To_And_Rewrite
(LLIB
, Right_Opnd
(N
));
2293 Analyze_And_Resolve
(Right_Opnd
(N
), LLIB
, Suppress
=> All_Checks
);
2296 -- If right operand is Long_Long_Integer, convert left operand
2297 -- and we are done (with a comparison of two Long_Long_Integers).
2299 -- This is the only remaining possibility
2301 else pragma Assert
(Rtype
= LLIB
);
2302 Convert_To_And_Rewrite
(LLIB
, Left_Opnd
(N
));
2303 Analyze_And_Resolve
(Left_Opnd
(N
), LLIB
, Suppress
=> All_Checks
);
2307 end Expand_Compare_Minimize_Eliminate_Overflow
;
2309 -------------------------------
2310 -- Expand_Composite_Equality --
2311 -------------------------------
2313 -- This function is only called for comparing internal fields of composite
2314 -- types when these fields are themselves composites. This is a special
2315 -- case because it is not possible to respect normal Ada visibility rules.
2317 function Expand_Composite_Equality
2322 Bodies
: List_Id
) return Node_Id
2324 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
2325 Full_Type
: Entity_Id
;
2329 function Find_Primitive_Eq
return Node_Id
;
2330 -- AI05-0123: Locate primitive equality for type if it exists, and
2331 -- build the corresponding call. If operation is abstract, replace
2332 -- call with an explicit raise. Return Empty if there is no primitive.
2334 -----------------------
2335 -- Find_Primitive_Eq --
2336 -----------------------
2338 function Find_Primitive_Eq
return Node_Id
is
2343 Prim_E
:= First_Elmt
(Collect_Primitive_Operations
(Typ
));
2344 while Present
(Prim_E
) loop
2345 Prim
:= Node
(Prim_E
);
2347 -- Locate primitive equality with the right signature
2349 if Chars
(Prim
) = Name_Op_Eq
2350 and then Etype
(First_Formal
(Prim
)) =
2351 Etype
(Next_Formal
(First_Formal
(Prim
)))
2352 and then Etype
(Prim
) = Standard_Boolean
2354 if Is_Abstract_Subprogram
(Prim
) then
2356 Make_Raise_Program_Error
(Loc
,
2357 Reason
=> PE_Explicit_Raise
);
2361 Make_Function_Call
(Loc
,
2362 Name
=> New_Occurrence_Of
(Prim
, Loc
),
2363 Parameter_Associations
=> New_List
(Lhs
, Rhs
));
2370 -- If not found, predefined operation will be used
2373 end Find_Primitive_Eq
;
2375 -- Start of processing for Expand_Composite_Equality
2378 if Is_Private_Type
(Typ
) then
2379 Full_Type
:= Underlying_Type
(Typ
);
2384 -- If the private type has no completion the context may be the
2385 -- expansion of a composite equality for a composite type with some
2386 -- still incomplete components. The expression will not be analyzed
2387 -- until the enclosing type is completed, at which point this will be
2388 -- properly expanded, unless there is a bona fide completion error.
2390 if No
(Full_Type
) then
2391 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2394 Full_Type
:= Base_Type
(Full_Type
);
2396 -- When the base type itself is private, use the full view to expand
2397 -- the composite equality.
2399 if Is_Private_Type
(Full_Type
) then
2400 Full_Type
:= Underlying_Type
(Full_Type
);
2403 -- Case of array types
2405 if Is_Array_Type
(Full_Type
) then
2407 -- If the operand is an elementary type other than a floating-point
2408 -- type, then we can simply use the built-in block bitwise equality,
2409 -- since the predefined equality operators always apply and bitwise
2410 -- equality is fine for all these cases.
2412 if Is_Elementary_Type
(Component_Type
(Full_Type
))
2413 and then not Is_Floating_Point_Type
(Component_Type
(Full_Type
))
2415 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2417 -- For composite component types, and floating-point types, use the
2418 -- expansion. This deals with tagged component types (where we use
2419 -- the applicable equality routine) and floating-point, (where we
2420 -- need to worry about negative zeroes), and also the case of any
2421 -- composite type recursively containing such fields.
2424 return Expand_Array_Equality
(Nod
, Lhs
, Rhs
, Bodies
, Full_Type
);
2427 -- Case of tagged record types
2429 elsif Is_Tagged_Type
(Full_Type
) then
2431 -- Call the primitive operation "=" of this type
2433 if Is_Class_Wide_Type
(Full_Type
) then
2434 Full_Type
:= Root_Type
(Full_Type
);
2437 -- If this is derived from an untagged private type completed with a
2438 -- tagged type, it does not have a full view, so we use the primitive
2439 -- operations of the private type. This check should no longer be
2440 -- necessary when these types receive their full views ???
2442 if Is_Private_Type
(Typ
)
2443 and then not Is_Tagged_Type
(Typ
)
2444 and then not Is_Controlled
(Typ
)
2445 and then Is_Derived_Type
(Typ
)
2446 and then No
(Full_View
(Typ
))
2448 Prim
:= First_Elmt
(Collect_Primitive_Operations
(Typ
));
2450 Prim
:= First_Elmt
(Primitive_Operations
(Full_Type
));
2454 Eq_Op
:= Node
(Prim
);
2455 exit when Chars
(Eq_Op
) = Name_Op_Eq
2456 and then Etype
(First_Formal
(Eq_Op
)) =
2457 Etype
(Next_Formal
(First_Formal
(Eq_Op
)))
2458 and then Base_Type
(Etype
(Eq_Op
)) = Standard_Boolean
;
2460 pragma Assert
(Present
(Prim
));
2463 Eq_Op
:= Node
(Prim
);
2466 Make_Function_Call
(Loc
,
2467 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2468 Parameter_Associations
=>
2470 (Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Lhs
),
2471 Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Rhs
)));
2473 -- Case of untagged record types
2475 elsif Is_Record_Type
(Full_Type
) then
2476 Eq_Op
:= TSS
(Full_Type
, TSS_Composite_Equality
);
2478 if Present
(Eq_Op
) then
2479 if Etype
(First_Formal
(Eq_Op
)) /= Full_Type
then
2481 -- Inherited equality from parent type. Convert the actuals to
2482 -- match signature of operation.
2485 T
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
2489 Make_Function_Call
(Loc
,
2490 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2491 Parameter_Associations
=> New_List
(
2492 OK_Convert_To
(T
, Lhs
),
2493 OK_Convert_To
(T
, Rhs
)));
2497 -- Comparison between Unchecked_Union components
2499 if Is_Unchecked_Union
(Full_Type
) then
2501 Lhs_Type
: Node_Id
:= Full_Type
;
2502 Rhs_Type
: Node_Id
:= Full_Type
;
2503 Lhs_Discr_Val
: Node_Id
;
2504 Rhs_Discr_Val
: Node_Id
;
2509 if Nkind
(Lhs
) = N_Selected_Component
then
2510 Lhs_Type
:= Etype
(Entity
(Selector_Name
(Lhs
)));
2515 if Nkind
(Rhs
) = N_Selected_Component
then
2516 Rhs_Type
:= Etype
(Entity
(Selector_Name
(Rhs
)));
2519 -- Lhs of the composite equality
2521 if Is_Constrained
(Lhs_Type
) then
2523 -- Since the enclosing record type can never be an
2524 -- Unchecked_Union (this code is executed for records
2525 -- that do not have variants), we may reference its
2528 if Nkind
(Lhs
) = N_Selected_Component
2529 and then Has_Per_Object_Constraint
2530 (Entity
(Selector_Name
(Lhs
)))
2533 Make_Selected_Component
(Loc
,
2534 Prefix
=> Prefix
(Lhs
),
2537 (Get_Discriminant_Value
2538 (First_Discriminant
(Lhs_Type
),
2540 Stored_Constraint
(Lhs_Type
))));
2545 (Get_Discriminant_Value
2546 (First_Discriminant
(Lhs_Type
),
2548 Stored_Constraint
(Lhs_Type
)));
2552 -- It is not possible to infer the discriminant since
2553 -- the subtype is not constrained.
2556 Make_Raise_Program_Error
(Loc
,
2557 Reason
=> PE_Unchecked_Union_Restriction
);
2560 -- Rhs of the composite equality
2562 if Is_Constrained
(Rhs_Type
) then
2563 if Nkind
(Rhs
) = N_Selected_Component
2564 and then Has_Per_Object_Constraint
2565 (Entity
(Selector_Name
(Rhs
)))
2568 Make_Selected_Component
(Loc
,
2569 Prefix
=> Prefix
(Rhs
),
2572 (Get_Discriminant_Value
2573 (First_Discriminant
(Rhs_Type
),
2575 Stored_Constraint
(Rhs_Type
))));
2580 (Get_Discriminant_Value
2581 (First_Discriminant
(Rhs_Type
),
2583 Stored_Constraint
(Rhs_Type
)));
2588 Make_Raise_Program_Error
(Loc
,
2589 Reason
=> PE_Unchecked_Union_Restriction
);
2592 -- Call the TSS equality function with the inferred
2593 -- discriminant values.
2596 Make_Function_Call
(Loc
,
2597 Name
=> New_Occurrence_Of
(Eq_Op
, Loc
),
2598 Parameter_Associations
=> New_List
(
2605 -- All cases other than comparing Unchecked_Union types
2609 T
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
2612 Make_Function_Call
(Loc
,
2614 New_Occurrence_Of
(Eq_Op
, Loc
),
2615 Parameter_Associations
=> New_List
(
2616 OK_Convert_To
(T
, Lhs
),
2617 OK_Convert_To
(T
, Rhs
)));
2622 -- Equality composes in Ada 2012 for untagged record types. It also
2623 -- composes for bounded strings, because they are part of the
2624 -- predefined environment. We could make it compose for bounded
2625 -- strings by making them tagged, or by making sure all subcomponents
2626 -- are set to the same value, even when not used. Instead, we have
2627 -- this special case in the compiler, because it's more efficient.
2629 elsif Ada_Version
>= Ada_2012
or else Is_Bounded_String
(Typ
) then
2631 -- If no TSS has been created for the type, check whether there is
2632 -- a primitive equality declared for it.
2635 Op
: constant Node_Id
:= Find_Primitive_Eq
;
2638 -- Use user-defined primitive if it exists, otherwise use
2639 -- predefined equality.
2641 if Present
(Op
) then
2644 return Make_Op_Eq
(Loc
, Lhs
, Rhs
);
2649 return Expand_Record_Equality
(Nod
, Full_Type
, Lhs
, Rhs
, Bodies
);
2652 -- Non-composite types (always use predefined equality)
2655 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
2657 end Expand_Composite_Equality
;
2659 ------------------------
2660 -- Expand_Concatenate --
2661 ------------------------
2663 procedure Expand_Concatenate
(Cnode
: Node_Id
; Opnds
: List_Id
) is
2664 Loc
: constant Source_Ptr
:= Sloc
(Cnode
);
2666 Atyp
: constant Entity_Id
:= Base_Type
(Etype
(Cnode
));
2667 -- Result type of concatenation
2669 Ctyp
: constant Entity_Id
:= Base_Type
(Component_Type
(Etype
(Cnode
)));
2670 -- Component type. Elements of this component type can appear as one
2671 -- of the operands of concatenation as well as arrays.
2673 Istyp
: constant Entity_Id
:= Etype
(First_Index
(Atyp
));
2676 Ityp
: constant Entity_Id
:= Base_Type
(Istyp
);
2677 -- Index type. This is the base type of the index subtype, and is used
2678 -- for all computed bounds (which may be out of range of Istyp in the
2679 -- case of null ranges).
2682 -- This is the type we use to do arithmetic to compute the bounds and
2683 -- lengths of operands. The choice of this type is a little subtle and
2684 -- is discussed in a separate section at the start of the body code.
2686 Concatenation_Error
: exception;
2687 -- Raised if concatenation is sure to raise a CE
2689 Result_May_Be_Null
: Boolean := True;
2690 -- Reset to False if at least one operand is encountered which is known
2691 -- at compile time to be non-null. Used for handling the special case
2692 -- of setting the high bound to the last operand high bound for a null
2693 -- result, thus ensuring a proper high bound in the super-flat case.
2695 N
: constant Nat
:= List_Length
(Opnds
);
2696 -- Number of concatenation operands including possibly null operands
2699 -- Number of operands excluding any known to be null, except that the
2700 -- last operand is always retained, in case it provides the bounds for
2704 -- Current operand being processed in the loop through operands. After
2705 -- this loop is complete, always contains the last operand (which is not
2706 -- the same as Operands (NN), since null operands are skipped).
2708 -- Arrays describing the operands, only the first NN entries of each
2709 -- array are set (NN < N when we exclude known null operands).
2711 Is_Fixed_Length
: array (1 .. N
) of Boolean;
2712 -- True if length of corresponding operand known at compile time
2714 Operands
: array (1 .. N
) of Node_Id
;
2715 -- Set to the corresponding entry in the Opnds list (but note that null
2716 -- operands are excluded, so not all entries in the list are stored).
2718 Fixed_Length
: array (1 .. N
) of Uint
;
2719 -- Set to length of operand. Entries in this array are set only if the
2720 -- corresponding entry in Is_Fixed_Length is True.
2722 Opnd_Low_Bound
: array (1 .. N
) of Node_Id
;
2723 -- Set to lower bound of operand. Either an integer literal in the case
2724 -- where the bound is known at compile time, else actual lower bound.
2725 -- The operand low bound is of type Ityp.
2727 Var_Length
: array (1 .. N
) of Entity_Id
;
2728 -- Set to an entity of type Natural that contains the length of an
2729 -- operand whose length is not known at compile time. Entries in this
2730 -- array are set only if the corresponding entry in Is_Fixed_Length
2731 -- is False. The entity is of type Artyp.
2733 Aggr_Length
: array (0 .. N
) of Node_Id
;
2734 -- The J'th entry in an expression node that represents the total length
2735 -- of operands 1 through J. It is either an integer literal node, or a
2736 -- reference to a constant entity with the right value, so it is fine
2737 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2738 -- entry always is set to zero. The length is of type Artyp.
2740 Low_Bound
: Node_Id
;
2741 -- A tree node representing the low bound of the result (of type Ityp).
2742 -- This is either an integer literal node, or an identifier reference to
2743 -- a constant entity initialized to the appropriate value.
2745 Last_Opnd_Low_Bound
: Node_Id
;
2746 -- A tree node representing the low bound of the last operand. This
2747 -- need only be set if the result could be null. It is used for the
2748 -- special case of setting the right low bound for a null result.
2749 -- This is of type Ityp.
2751 Last_Opnd_High_Bound
: Node_Id
;
2752 -- A tree node representing the high bound of the last operand. This
2753 -- need only be set if the result could be null. It is used for the
2754 -- special case of setting the right high bound for a null result.
2755 -- This is of type Ityp.
2757 High_Bound
: Node_Id
;
2758 -- A tree node representing the high bound of the result (of type Ityp)
2761 -- Result of the concatenation (of type Ityp)
2763 Actions
: constant List_Id
:= New_List
;
2764 -- Collect actions to be inserted
2766 Known_Non_Null_Operand_Seen
: Boolean;
2767 -- Set True during generation of the assignments of operands into
2768 -- result once an operand known to be non-null has been seen.
2770 function Make_Artyp_Literal
(Val
: Nat
) return Node_Id
;
2771 -- This function makes an N_Integer_Literal node that is returned in
2772 -- analyzed form with the type set to Artyp. Importantly this literal
2773 -- is not flagged as static, so that if we do computations with it that
2774 -- result in statically detected out of range conditions, we will not
2775 -- generate error messages but instead warning messages.
2777 function To_Artyp
(X
: Node_Id
) return Node_Id
;
2778 -- Given a node of type Ityp, returns the corresponding value of type
2779 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2780 -- For enum types, the Pos of the value is returned.
2782 function To_Ityp
(X
: Node_Id
) return Node_Id
;
2783 -- The inverse function (uses Val in the case of enumeration types)
2785 ------------------------
2786 -- Make_Artyp_Literal --
2787 ------------------------
2789 function Make_Artyp_Literal
(Val
: Nat
) return Node_Id
is
2790 Result
: constant Node_Id
:= Make_Integer_Literal
(Loc
, Val
);
2792 Set_Etype
(Result
, Artyp
);
2793 Set_Analyzed
(Result
, True);
2794 Set_Is_Static_Expression
(Result
, False);
2796 end Make_Artyp_Literal
;
2802 function To_Artyp
(X
: Node_Id
) return Node_Id
is
2804 if Ityp
= Base_Type
(Artyp
) then
2807 elsif Is_Enumeration_Type
(Ityp
) then
2809 Make_Attribute_Reference
(Loc
,
2810 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
2811 Attribute_Name
=> Name_Pos
,
2812 Expressions
=> New_List
(X
));
2815 return Convert_To
(Artyp
, X
);
2823 function To_Ityp
(X
: Node_Id
) return Node_Id
is
2825 if Is_Enumeration_Type
(Ityp
) then
2827 Make_Attribute_Reference
(Loc
,
2828 Prefix
=> New_Occurrence_Of
(Ityp
, Loc
),
2829 Attribute_Name
=> Name_Val
,
2830 Expressions
=> New_List
(X
));
2832 -- Case where we will do a type conversion
2835 if Ityp
= Base_Type
(Artyp
) then
2838 return Convert_To
(Ityp
, X
);
2843 -- Local Declarations
2845 Lib_Level_Target
: constant Boolean :=
2846 Nkind
(Parent
(Cnode
)) = N_Object_Declaration
2848 Is_Library_Level_Entity
(Defining_Identifier
(Parent
(Cnode
)));
2850 -- If the concatenation declares a library level entity, we call the
2851 -- built-in concatenation routines to prevent code bloat, regardless
2852 -- of optimization level. This is space-efficient, and prevent linking
2853 -- problems when units are compiled with different optimizations.
2855 Opnd_Typ
: Entity_Id
;
2862 -- Start of processing for Expand_Concatenate
2865 -- Choose an appropriate computational type
2867 -- We will be doing calculations of lengths and bounds in this routine
2868 -- and computing one from the other in some cases, e.g. getting the high
2869 -- bound by adding the length-1 to the low bound.
2871 -- We can't just use the index type, or even its base type for this
2872 -- purpose for two reasons. First it might be an enumeration type which
2873 -- is not suitable for computations of any kind, and second it may
2874 -- simply not have enough range. For example if the index type is
2875 -- -128..+127 then lengths can be up to 256, which is out of range of
2878 -- For enumeration types, we can simply use Standard_Integer, this is
2879 -- sufficient since the actual number of enumeration literals cannot
2880 -- possibly exceed the range of integer (remember we will be doing the
2881 -- arithmetic with POS values, not representation values).
2883 if Is_Enumeration_Type
(Ityp
) then
2884 Artyp
:= Standard_Integer
;
2886 -- If index type is Positive, we use the standard unsigned type, to give
2887 -- more room on the top of the range, obviating the need for an overflow
2888 -- check when creating the upper bound. This is needed to avoid junk
2889 -- overflow checks in the common case of String types.
2891 -- ??? Disabled for now
2893 -- elsif Istyp = Standard_Positive then
2894 -- Artyp := Standard_Unsigned;
2896 -- For modular types, we use a 32-bit modular type for types whose size
2897 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2898 -- identity type, and for larger unsigned types we use 64-bits.
2900 elsif Is_Modular_Integer_Type
(Ityp
) then
2901 if RM_Size
(Ityp
) < RM_Size
(Standard_Unsigned
) then
2902 Artyp
:= Standard_Unsigned
;
2903 elsif RM_Size
(Ityp
) = RM_Size
(Standard_Unsigned
) then
2906 Artyp
:= RTE
(RE_Long_Long_Unsigned
);
2909 -- Similar treatment for signed types
2912 if RM_Size
(Ityp
) < RM_Size
(Standard_Integer
) then
2913 Artyp
:= Standard_Integer
;
2914 elsif RM_Size
(Ityp
) = RM_Size
(Standard_Integer
) then
2917 Artyp
:= Standard_Long_Long_Integer
;
2921 -- Supply dummy entry at start of length array
2923 Aggr_Length
(0) := Make_Artyp_Literal
(0);
2925 -- Go through operands setting up the above arrays
2929 Opnd
:= Remove_Head
(Opnds
);
2930 Opnd_Typ
:= Etype
(Opnd
);
2932 -- The parent got messed up when we put the operands in a list,
2933 -- so now put back the proper parent for the saved operand, that
2934 -- is to say the concatenation node, to make sure that each operand
2935 -- is seen as a subexpression, e.g. if actions must be inserted.
2937 Set_Parent
(Opnd
, Cnode
);
2939 -- Set will be True when we have setup one entry in the array
2943 -- Singleton element (or character literal) case
2945 if Base_Type
(Opnd_Typ
) = Ctyp
then
2947 Operands
(NN
) := Opnd
;
2948 Is_Fixed_Length
(NN
) := True;
2949 Fixed_Length
(NN
) := Uint_1
;
2950 Result_May_Be_Null
:= False;
2952 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2953 -- since we know that the result cannot be null).
2955 Opnd_Low_Bound
(NN
) :=
2956 Make_Attribute_Reference
(Loc
,
2957 Prefix
=> New_Occurrence_Of
(Istyp
, Loc
),
2958 Attribute_Name
=> Name_First
);
2962 -- String literal case (can only occur for strings of course)
2964 elsif Nkind
(Opnd
) = N_String_Literal
then
2965 Len
:= String_Literal_Length
(Opnd_Typ
);
2968 Result_May_Be_Null
:= False;
2971 -- Capture last operand low and high bound if result could be null
2973 if J
= N
and then Result_May_Be_Null
then
2974 Last_Opnd_Low_Bound
:=
2975 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
));
2977 Last_Opnd_High_Bound
:=
2978 Make_Op_Subtract
(Loc
,
2980 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
)),
2981 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
2984 -- Skip null string literal
2986 if J
< N
and then Len
= 0 then
2991 Operands
(NN
) := Opnd
;
2992 Is_Fixed_Length
(NN
) := True;
2994 -- Set length and bounds
2996 Fixed_Length
(NN
) := Len
;
2998 Opnd_Low_Bound
(NN
) :=
2999 New_Copy_Tree
(String_Literal_Low_Bound
(Opnd_Typ
));
3006 -- Check constrained case with known bounds
3008 if Is_Constrained
(Opnd_Typ
) then
3010 Index
: constant Node_Id
:= First_Index
(Opnd_Typ
);
3011 Indx_Typ
: constant Entity_Id
:= Etype
(Index
);
3012 Lo
: constant Node_Id
:= Type_Low_Bound
(Indx_Typ
);
3013 Hi
: constant Node_Id
:= Type_High_Bound
(Indx_Typ
);
3016 -- Fixed length constrained array type with known at compile
3017 -- time bounds is last case of fixed length operand.
3019 if Compile_Time_Known_Value
(Lo
)
3021 Compile_Time_Known_Value
(Hi
)
3024 Loval
: constant Uint
:= Expr_Value
(Lo
);
3025 Hival
: constant Uint
:= Expr_Value
(Hi
);
3026 Len
: constant Uint
:=
3027 UI_Max
(Hival
- Loval
+ 1, Uint_0
);
3031 Result_May_Be_Null
:= False;
3034 -- Capture last operand bounds if result could be null
3036 if J
= N
and then Result_May_Be_Null
then
3037 Last_Opnd_Low_Bound
:=
3039 Make_Integer_Literal
(Loc
, Expr_Value
(Lo
)));
3041 Last_Opnd_High_Bound
:=
3043 Make_Integer_Literal
(Loc
, Expr_Value
(Hi
)));
3046 -- Exclude null length case unless last operand
3048 if J
< N
and then Len
= 0 then
3053 Operands
(NN
) := Opnd
;
3054 Is_Fixed_Length
(NN
) := True;
3055 Fixed_Length
(NN
) := Len
;
3057 Opnd_Low_Bound
(NN
) :=
3059 (Make_Integer_Literal
(Loc
, Expr_Value
(Lo
)));
3066 -- All cases where the length is not known at compile time, or the
3067 -- special case of an operand which is known to be null but has a
3068 -- lower bound other than 1 or is other than a string type.
3073 -- Capture operand bounds
3075 Opnd_Low_Bound
(NN
) :=
3076 Make_Attribute_Reference
(Loc
,
3078 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3079 Attribute_Name
=> Name_First
);
3081 -- Capture last operand bounds if result could be null
3083 if J
= N
and Result_May_Be_Null
then
3084 Last_Opnd_Low_Bound
:=
3086 Make_Attribute_Reference
(Loc
,
3088 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3089 Attribute_Name
=> Name_First
));
3091 Last_Opnd_High_Bound
:=
3093 Make_Attribute_Reference
(Loc
,
3095 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3096 Attribute_Name
=> Name_Last
));
3099 -- Capture length of operand in entity
3101 Operands
(NN
) := Opnd
;
3102 Is_Fixed_Length
(NN
) := False;
3104 Var_Length
(NN
) := Make_Temporary
(Loc
, 'L');
3107 Make_Object_Declaration
(Loc
,
3108 Defining_Identifier
=> Var_Length
(NN
),
3109 Constant_Present
=> True,
3110 Object_Definition
=> New_Occurrence_Of
(Artyp
, Loc
),
3112 Make_Attribute_Reference
(Loc
,
3114 Duplicate_Subexpr
(Opnd
, Name_Req
=> True),
3115 Attribute_Name
=> Name_Length
)));
3119 -- Set next entry in aggregate length array
3121 -- For first entry, make either integer literal for fixed length
3122 -- or a reference to the saved length for variable length.
3125 if Is_Fixed_Length
(1) then
3126 Aggr_Length
(1) := Make_Integer_Literal
(Loc
, Fixed_Length
(1));
3128 Aggr_Length
(1) := New_Occurrence_Of
(Var_Length
(1), Loc
);
3131 -- If entry is fixed length and only fixed lengths so far, make
3132 -- appropriate new integer literal adding new length.
3134 elsif Is_Fixed_Length
(NN
)
3135 and then Nkind
(Aggr_Length
(NN
- 1)) = N_Integer_Literal
3138 Make_Integer_Literal
(Loc
,
3139 Intval
=> Fixed_Length
(NN
) + Intval
(Aggr_Length
(NN
- 1)));
3141 -- All other cases, construct an addition node for the length and
3142 -- create an entity initialized to this length.
3145 Ent
:= Make_Temporary
(Loc
, 'L');
3147 if Is_Fixed_Length
(NN
) then
3148 Clen
:= Make_Integer_Literal
(Loc
, Fixed_Length
(NN
));
3150 Clen
:= New_Occurrence_Of
(Var_Length
(NN
), Loc
);
3154 Make_Object_Declaration
(Loc
,
3155 Defining_Identifier
=> Ent
,
3156 Constant_Present
=> True,
3157 Object_Definition
=> New_Occurrence_Of
(Artyp
, Loc
),
3160 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
- 1)),
3161 Right_Opnd
=> Clen
)));
3163 Aggr_Length
(NN
) := Make_Identifier
(Loc
, Chars
=> Chars
(Ent
));
3170 -- If we have only skipped null operands, return the last operand
3177 -- If we have only one non-null operand, return it and we are done.
3178 -- There is one case in which this cannot be done, and that is when
3179 -- the sole operand is of the element type, in which case it must be
3180 -- converted to an array, and the easiest way of doing that is to go
3181 -- through the normal general circuit.
3183 if NN
= 1 and then Base_Type
(Etype
(Operands
(1))) /= Ctyp
then
3184 Result
:= Operands
(1);
3188 -- Cases where we have a real concatenation
3190 -- Next step is to find the low bound for the result array that we
3191 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3193 -- If the ultimate ancestor of the index subtype is a constrained array
3194 -- definition, then the lower bound is that of the index subtype as
3195 -- specified by (RM 4.5.3(6)).
3197 -- The right test here is to go to the root type, and then the ultimate
3198 -- ancestor is the first subtype of this root type.
3200 if Is_Constrained
(First_Subtype
(Root_Type
(Atyp
))) then
3202 Make_Attribute_Reference
(Loc
,
3204 New_Occurrence_Of
(First_Subtype
(Root_Type
(Atyp
)), Loc
),
3205 Attribute_Name
=> Name_First
);
3207 -- If the first operand in the list has known length we know that
3208 -- the lower bound of the result is the lower bound of this operand.
3210 elsif Is_Fixed_Length
(1) then
3211 Low_Bound
:= Opnd_Low_Bound
(1);
3213 -- OK, we don't know the lower bound, we have to build a horrible
3214 -- if expression node of the form
3216 -- if Cond1'Length /= 0 then
3219 -- if Opnd2'Length /= 0 then
3224 -- The nesting ends either when we hit an operand whose length is known
3225 -- at compile time, or on reaching the last operand, whose low bound we
3226 -- take unconditionally whether or not it is null. It's easiest to do
3227 -- this with a recursive procedure:
3231 function Get_Known_Bound
(J
: Nat
) return Node_Id
;
3232 -- Returns the lower bound determined by operands J .. NN
3234 ---------------------
3235 -- Get_Known_Bound --
3236 ---------------------
3238 function Get_Known_Bound
(J
: Nat
) return Node_Id
is
3240 if Is_Fixed_Length
(J
) or else J
= NN
then
3241 return New_Copy
(Opnd_Low_Bound
(J
));
3245 Make_If_Expression
(Loc
,
3246 Expressions
=> New_List
(
3250 New_Occurrence_Of
(Var_Length
(J
), Loc
),
3252 Make_Integer_Literal
(Loc
, 0)),
3254 New_Copy
(Opnd_Low_Bound
(J
)),
3255 Get_Known_Bound
(J
+ 1)));
3257 end Get_Known_Bound
;
3260 Ent
:= Make_Temporary
(Loc
, 'L');
3263 Make_Object_Declaration
(Loc
,
3264 Defining_Identifier
=> Ent
,
3265 Constant_Present
=> True,
3266 Object_Definition
=> New_Occurrence_Of
(Ityp
, Loc
),
3267 Expression
=> Get_Known_Bound
(1)));
3269 Low_Bound
:= New_Occurrence_Of
(Ent
, Loc
);
3273 -- Now we can safely compute the upper bound, normally
3274 -- Low_Bound + Length - 1.
3279 Left_Opnd
=> To_Artyp
(New_Copy
(Low_Bound
)),
3281 Make_Op_Subtract
(Loc
,
3282 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
)),
3283 Right_Opnd
=> Make_Artyp_Literal
(1))));
3285 -- Note that calculation of the high bound may cause overflow in some
3286 -- very weird cases, so in the general case we need an overflow check on
3287 -- the high bound. We can avoid this for the common case of string types
3288 -- and other types whose index is Positive, since we chose a wider range
3289 -- for the arithmetic type. If checks are suppressed we do not set the
3290 -- flag, and possibly superfluous warnings will be omitted.
3292 if Istyp
/= Standard_Positive
3293 and then not Overflow_Checks_Suppressed
(Istyp
)
3295 Activate_Overflow_Check
(High_Bound
);
3298 -- Handle the exceptional case where the result is null, in which case
3299 -- case the bounds come from the last operand (so that we get the proper
3300 -- bounds if the last operand is super-flat).
3302 if Result_May_Be_Null
then
3304 Make_If_Expression
(Loc
,
3305 Expressions
=> New_List
(
3307 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
)),
3308 Right_Opnd
=> Make_Artyp_Literal
(0)),
3309 Last_Opnd_Low_Bound
,
3313 Make_If_Expression
(Loc
,
3314 Expressions
=> New_List
(
3316 Left_Opnd
=> New_Copy
(Aggr_Length
(NN
)),
3317 Right_Opnd
=> Make_Artyp_Literal
(0)),
3318 Last_Opnd_High_Bound
,
3322 -- Here is where we insert the saved up actions
3324 Insert_Actions
(Cnode
, Actions
, Suppress
=> All_Checks
);
3326 -- Now we construct an array object with appropriate bounds. We mark
3327 -- the target as internal to prevent useless initialization when
3328 -- Initialize_Scalars is enabled. Also since this is the actual result
3329 -- entity, we make sure we have debug information for the result.
3331 Ent
:= Make_Temporary
(Loc
, 'S');
3332 Set_Is_Internal
(Ent
);
3333 Set_Needs_Debug_Info
(Ent
);
3335 -- If the bound is statically known to be out of range, we do not want
3336 -- to abort, we want a warning and a runtime constraint error. Note that
3337 -- we have arranged that the result will not be treated as a static
3338 -- constant, so we won't get an illegality during this insertion.
3340 Insert_Action
(Cnode
,
3341 Make_Object_Declaration
(Loc
,
3342 Defining_Identifier
=> Ent
,
3343 Object_Definition
=>
3344 Make_Subtype_Indication
(Loc
,
3345 Subtype_Mark
=> New_Occurrence_Of
(Atyp
, Loc
),
3347 Make_Index_Or_Discriminant_Constraint
(Loc
,
3348 Constraints
=> New_List
(
3350 Low_Bound
=> Low_Bound
,
3351 High_Bound
=> High_Bound
))))),
3352 Suppress
=> All_Checks
);
3354 -- If the result of the concatenation appears as the initializing
3355 -- expression of an object declaration, we can just rename the
3356 -- result, rather than copying it.
3358 Set_OK_To_Rename
(Ent
);
3360 -- Catch the static out of range case now
3362 if Raises_Constraint_Error
(High_Bound
) then
3363 raise Concatenation_Error
;
3366 -- Now we will generate the assignments to do the actual concatenation
3368 -- There is one case in which we will not do this, namely when all the
3369 -- following conditions are met:
3371 -- The result type is Standard.String
3373 -- There are nine or fewer retained (non-null) operands
3375 -- The optimization level is -O0
3377 -- The corresponding System.Concat_n.Str_Concat_n routine is
3378 -- available in the run time.
3380 -- The debug flag gnatd.c is not set
3382 -- If all these conditions are met then we generate a call to the
3383 -- relevant concatenation routine. The purpose of this is to avoid
3384 -- undesirable code bloat at -O0.
3386 if Atyp
= Standard_String
3387 and then NN
in 2 .. 9
3388 and then (Lib_Level_Target
3389 or else ((Optimization_Level
= 0 or else Debug_Flag_Dot_CC
)
3390 and then not Debug_Flag_Dot_C
))
3393 RR
: constant array (Nat
range 2 .. 9) of RE_Id
:=
3404 if RTE_Available
(RR
(NN
)) then
3406 Opnds
: constant List_Id
:=
3407 New_List
(New_Occurrence_Of
(Ent
, Loc
));
3410 for J
in 1 .. NN
loop
3411 if Is_List_Member
(Operands
(J
)) then
3412 Remove
(Operands
(J
));
3415 if Base_Type
(Etype
(Operands
(J
))) = Ctyp
then
3417 Make_Aggregate
(Loc
,
3418 Component_Associations
=> New_List
(
3419 Make_Component_Association
(Loc
,
3420 Choices
=> New_List
(
3421 Make_Integer_Literal
(Loc
, 1)),
3422 Expression
=> Operands
(J
)))));
3425 Append_To
(Opnds
, Operands
(J
));
3429 Insert_Action
(Cnode
,
3430 Make_Procedure_Call_Statement
(Loc
,
3431 Name
=> New_Occurrence_Of
(RTE
(RR
(NN
)), Loc
),
3432 Parameter_Associations
=> Opnds
));
3434 Result
:= New_Occurrence_Of
(Ent
, Loc
);
3441 -- Not special case so generate the assignments
3443 Known_Non_Null_Operand_Seen
:= False;
3445 for J
in 1 .. NN
loop
3447 Lo
: constant Node_Id
:=
3449 Left_Opnd
=> To_Artyp
(New_Copy
(Low_Bound
)),
3450 Right_Opnd
=> Aggr_Length
(J
- 1));
3452 Hi
: constant Node_Id
:=
3454 Left_Opnd
=> To_Artyp
(New_Copy
(Low_Bound
)),
3456 Make_Op_Subtract
(Loc
,
3457 Left_Opnd
=> Aggr_Length
(J
),
3458 Right_Opnd
=> Make_Artyp_Literal
(1)));
3461 -- Singleton case, simple assignment
3463 if Base_Type
(Etype
(Operands
(J
))) = Ctyp
then
3464 Known_Non_Null_Operand_Seen
:= True;
3465 Insert_Action
(Cnode
,
3466 Make_Assignment_Statement
(Loc
,
3468 Make_Indexed_Component
(Loc
,
3469 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
3470 Expressions
=> New_List
(To_Ityp
(Lo
))),
3471 Expression
=> Operands
(J
)),
3472 Suppress
=> All_Checks
);
3474 -- Array case, slice assignment, skipped when argument is fixed
3475 -- length and known to be null.
3477 elsif (not Is_Fixed_Length
(J
)) or else (Fixed_Length
(J
) > 0) then
3480 Make_Assignment_Statement
(Loc
,
3484 New_Occurrence_Of
(Ent
, Loc
),
3487 Low_Bound
=> To_Ityp
(Lo
),
3488 High_Bound
=> To_Ityp
(Hi
))),
3489 Expression
=> Operands
(J
));
3491 if Is_Fixed_Length
(J
) then
3492 Known_Non_Null_Operand_Seen
:= True;
3494 elsif not Known_Non_Null_Operand_Seen
then
3496 -- Here if operand length is not statically known and no
3497 -- operand known to be non-null has been processed yet.
3498 -- If operand length is 0, we do not need to perform the
3499 -- assignment, and we must avoid the evaluation of the
3500 -- high bound of the slice, since it may underflow if the
3501 -- low bound is Ityp'First.
3504 Make_Implicit_If_Statement
(Cnode
,
3508 New_Occurrence_Of
(Var_Length
(J
), Loc
),
3509 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
3510 Then_Statements
=> New_List
(Assign
));
3513 Insert_Action
(Cnode
, Assign
, Suppress
=> All_Checks
);
3519 -- Finally we build the result, which is a reference to the array object
3521 Result
:= New_Occurrence_Of
(Ent
, Loc
);
3524 Rewrite
(Cnode
, Result
);
3525 Analyze_And_Resolve
(Cnode
, Atyp
);
3528 when Concatenation_Error
=>
3530 -- Kill warning generated for the declaration of the static out of
3531 -- range high bound, and instead generate a Constraint_Error with
3532 -- an appropriate specific message.
3534 Kill_Dead_Code
(Declaration_Node
(Entity
(High_Bound
)));
3535 Apply_Compile_Time_Constraint_Error
3537 Msg
=> "concatenation result upper bound out of range??",
3538 Reason
=> CE_Range_Check_Failed
);
3539 end Expand_Concatenate
;
3541 ---------------------------------------------------
3542 -- Expand_Membership_Minimize_Eliminate_Overflow --
3543 ---------------------------------------------------
3545 procedure Expand_Membership_Minimize_Eliminate_Overflow
(N
: Node_Id
) is
3546 pragma Assert
(Nkind
(N
) = N_In
);
3547 -- Despite the name, this routine applies only to N_In, not to
3548 -- N_Not_In. The latter is always rewritten as not (X in Y).
3550 Result_Type
: constant Entity_Id
:= Etype
(N
);
3551 -- Capture result type, may be a derived boolean type
3553 Loc
: constant Source_Ptr
:= Sloc
(N
);
3554 Lop
: constant Node_Id
:= Left_Opnd
(N
);
3555 Rop
: constant Node_Id
:= Right_Opnd
(N
);
3557 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3558 -- is thus tempting to capture these values, but due to the rewrites
3559 -- that occur as a result of overflow checking, these values change
3560 -- as we go along, and it is safe just to always use Etype explicitly.
3562 Restype
: constant Entity_Id
:= Etype
(N
);
3566 -- Bounds in Minimize calls, not used currently
3568 LLIB
: constant Entity_Id
:= Base_Type
(Standard_Long_Long_Integer
);
3569 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3572 Minimize_Eliminate_Overflows
(Lop
, Lo
, Hi
, Top_Level
=> False);
3574 -- If right operand is a subtype name, and the subtype name has no
3575 -- predicate, then we can just replace the right operand with an
3576 -- explicit range T'First .. T'Last, and use the explicit range code.
3578 if Nkind
(Rop
) /= N_Range
3579 and then No
(Predicate_Function
(Etype
(Rop
)))
3582 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
3587 Make_Attribute_Reference
(Loc
,
3588 Attribute_Name
=> Name_First
,
3589 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
)),
3591 Make_Attribute_Reference
(Loc
,
3592 Attribute_Name
=> Name_Last
,
3593 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
))));
3594 Analyze_And_Resolve
(Rop
, Rtyp
, Suppress
=> All_Checks
);
3598 -- Here for the explicit range case. Note that the bounds of the range
3599 -- have not been processed for minimized or eliminated checks.
3601 if Nkind
(Rop
) = N_Range
then
3602 Minimize_Eliminate_Overflows
3603 (Low_Bound
(Rop
), Lo
, Hi
, Top_Level
=> False);
3604 Minimize_Eliminate_Overflows
3605 (High_Bound
(Rop
), Lo
, Hi
, Top_Level
=> False);
3607 -- We have A in B .. C, treated as A >= B and then A <= C
3611 if Is_RTE
(Etype
(Lop
), RE_Bignum
)
3612 or else Is_RTE
(Etype
(Low_Bound
(Rop
)), RE_Bignum
)
3613 or else Is_RTE
(Etype
(High_Bound
(Rop
)), RE_Bignum
)
3616 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
3617 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
3618 L
: constant Entity_Id
:=
3619 Make_Defining_Identifier
(Loc
, Name_uL
);
3620 Lopnd
: constant Node_Id
:= Convert_To_Bignum
(Lop
);
3621 Lbound
: constant Node_Id
:=
3622 Convert_To_Bignum
(Low_Bound
(Rop
));
3623 Hbound
: constant Node_Id
:=
3624 Convert_To_Bignum
(High_Bound
(Rop
));
3626 -- Now we rewrite the membership test node to look like
3629 -- Bnn : Result_Type;
3631 -- M : Mark_Id := SS_Mark;
3632 -- L : Bignum := Lopnd;
3634 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3642 -- Insert declaration of L into declarations of bignum block
3645 (Last
(Declarations
(Blk
)),
3646 Make_Object_Declaration
(Loc
,
3647 Defining_Identifier
=> L
,
3648 Object_Definition
=>
3649 New_Occurrence_Of
(RTE
(RE_Bignum
), Loc
),
3650 Expression
=> Lopnd
));
3652 -- Insert assignment to Bnn into expressions of bignum block
3655 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
3656 Make_Assignment_Statement
(Loc
,
3657 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
3661 Make_Function_Call
(Loc
,
3663 New_Occurrence_Of
(RTE
(RE_Big_GE
), Loc
),
3664 Parameter_Associations
=> New_List
(
3665 New_Occurrence_Of
(L
, Loc
),
3669 Make_Function_Call
(Loc
,
3671 New_Occurrence_Of
(RTE
(RE_Big_LE
), Loc
),
3672 Parameter_Associations
=> New_List
(
3673 New_Occurrence_Of
(L
, Loc
),
3676 -- Now rewrite the node
3679 Make_Expression_With_Actions
(Loc
,
3680 Actions
=> New_List
(
3681 Make_Object_Declaration
(Loc
,
3682 Defining_Identifier
=> Bnn
,
3683 Object_Definition
=>
3684 New_Occurrence_Of
(Result_Type
, Loc
)),
3686 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
3687 Analyze_And_Resolve
(N
, Result_Type
);
3691 -- Here if no bignums around
3694 -- Case where types are all the same
3696 if Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(Low_Bound
(Rop
)))
3698 Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(High_Bound
(Rop
)))
3702 -- If types are not all the same, it means that we have rewritten
3703 -- at least one of them to be of type Long_Long_Integer, and we
3704 -- will convert the other operands to Long_Long_Integer.
3707 Convert_To_And_Rewrite
(LLIB
, Lop
);
3708 Set_Analyzed
(Lop
, False);
3709 Analyze_And_Resolve
(Lop
, LLIB
);
3711 -- For the right operand, avoid unnecessary recursion into
3712 -- this routine, we know that overflow is not possible.
3714 Convert_To_And_Rewrite
(LLIB
, Low_Bound
(Rop
));
3715 Convert_To_And_Rewrite
(LLIB
, High_Bound
(Rop
));
3716 Set_Analyzed
(Rop
, False);
3717 Analyze_And_Resolve
(Rop
, LLIB
, Suppress
=> Overflow_Check
);
3720 -- Now the three operands are of the same signed integer type,
3721 -- so we can use the normal expansion routine for membership,
3722 -- setting the flag to prevent recursion into this procedure.
3724 Set_No_Minimize_Eliminate
(N
);
3728 -- Right operand is a subtype name and the subtype has a predicate. We
3729 -- have to make sure the predicate is checked, and for that we need to
3730 -- use the standard N_In circuitry with appropriate types.
3733 pragma Assert
(Present
(Predicate_Function
(Etype
(Rop
))));
3735 -- If types are "right", just call Expand_N_In preventing recursion
3737 if Base_Type
(Etype
(Lop
)) = Base_Type
(Etype
(Rop
)) then
3738 Set_No_Minimize_Eliminate
(N
);
3743 elsif Is_RTE
(Etype
(Lop
), RE_Bignum
) then
3745 -- For X in T, we want to rewrite our node as
3748 -- Bnn : Result_Type;
3751 -- M : Mark_Id := SS_Mark;
3752 -- Lnn : Long_Long_Integer'Base
3758 -- if not Bignum_In_LLI_Range (Nnn) then
3761 -- Lnn := From_Bignum (Nnn);
3763 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3764 -- and then T'Base (Lnn) in T;
3773 -- A bit gruesome, but there doesn't seem to be a simpler way
3776 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
3777 Bnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'B', N
);
3778 Lnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'L', N
);
3779 Nnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'N', N
);
3780 T
: constant Entity_Id
:= Etype
(Rop
);
3781 TB
: constant Entity_Id
:= Base_Type
(T
);
3785 -- Mark the last membership operation to prevent recursion
3789 Left_Opnd
=> Convert_To
(TB
, New_Occurrence_Of
(Lnn
, Loc
)),
3790 Right_Opnd
=> New_Occurrence_Of
(T
, Loc
));
3791 Set_No_Minimize_Eliminate
(Nin
);
3793 -- Now decorate the block
3796 (Last
(Declarations
(Blk
)),
3797 Make_Object_Declaration
(Loc
,
3798 Defining_Identifier
=> Lnn
,
3799 Object_Definition
=> New_Occurrence_Of
(LLIB
, Loc
)));
3802 (Last
(Declarations
(Blk
)),
3803 Make_Object_Declaration
(Loc
,
3804 Defining_Identifier
=> Nnn
,
3805 Object_Definition
=>
3806 New_Occurrence_Of
(RTE
(RE_Bignum
), Loc
)));
3809 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
3811 Make_Assignment_Statement
(Loc
,
3812 Name
=> New_Occurrence_Of
(Nnn
, Loc
),
3813 Expression
=> Relocate_Node
(Lop
)),
3815 Make_Implicit_If_Statement
(N
,
3819 Make_Function_Call
(Loc
,
3822 (RTE
(RE_Bignum_In_LLI_Range
), Loc
),
3823 Parameter_Associations
=> New_List
(
3824 New_Occurrence_Of
(Nnn
, Loc
)))),
3826 Then_Statements
=> New_List
(
3827 Make_Assignment_Statement
(Loc
,
3828 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
3830 New_Occurrence_Of
(Standard_False
, Loc
))),
3832 Else_Statements
=> New_List
(
3833 Make_Assignment_Statement
(Loc
,
3834 Name
=> New_Occurrence_Of
(Lnn
, Loc
),
3836 Make_Function_Call
(Loc
,
3838 New_Occurrence_Of
(RTE
(RE_From_Bignum
), Loc
),
3839 Parameter_Associations
=> New_List
(
3840 New_Occurrence_Of
(Nnn
, Loc
)))),
3842 Make_Assignment_Statement
(Loc
,
3843 Name
=> New_Occurrence_Of
(Bnn
, Loc
),
3848 Left_Opnd
=> New_Occurrence_Of
(Lnn
, Loc
),
3853 Make_Attribute_Reference
(Loc
,
3854 Attribute_Name
=> Name_First
,
3856 New_Occurrence_Of
(TB
, Loc
))),
3860 Make_Attribute_Reference
(Loc
,
3861 Attribute_Name
=> Name_Last
,
3863 New_Occurrence_Of
(TB
, Loc
))))),
3865 Right_Opnd
=> Nin
))))));
3867 -- Now we can do the rewrite
3870 Make_Expression_With_Actions
(Loc
,
3871 Actions
=> New_List
(
3872 Make_Object_Declaration
(Loc
,
3873 Defining_Identifier
=> Bnn
,
3874 Object_Definition
=>
3875 New_Occurrence_Of
(Result_Type
, Loc
)),
3877 Expression
=> New_Occurrence_Of
(Bnn
, Loc
)));
3878 Analyze_And_Resolve
(N
, Result_Type
);
3882 -- Not bignum case, but types don't match (this means we rewrote the
3883 -- left operand to be Long_Long_Integer).
3886 pragma Assert
(Base_Type
(Etype
(Lop
)) = LLIB
);
3888 -- We rewrite the membership test as (where T is the type with
3889 -- the predicate, i.e. the type of the right operand)
3891 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3892 -- and then T'Base (Lop) in T
3895 T
: constant Entity_Id
:= Etype
(Rop
);
3896 TB
: constant Entity_Id
:= Base_Type
(T
);
3900 -- The last membership test is marked to prevent recursion
3904 Left_Opnd
=> Convert_To
(TB
, Duplicate_Subexpr
(Lop
)),
3905 Right_Opnd
=> New_Occurrence_Of
(T
, Loc
));
3906 Set_No_Minimize_Eliminate
(Nin
);
3908 -- Now do the rewrite
3919 Make_Attribute_Reference
(Loc
,
3920 Attribute_Name
=> Name_First
,
3922 New_Occurrence_Of
(TB
, Loc
))),
3925 Make_Attribute_Reference
(Loc
,
3926 Attribute_Name
=> Name_Last
,
3928 New_Occurrence_Of
(TB
, Loc
))))),
3929 Right_Opnd
=> Nin
));
3930 Set_Analyzed
(N
, False);
3931 Analyze_And_Resolve
(N
, Restype
);
3935 end Expand_Membership_Minimize_Eliminate_Overflow
;
3937 ------------------------
3938 -- Expand_N_Allocator --
3939 ------------------------
3941 procedure Expand_N_Allocator
(N
: Node_Id
) is
3942 Etyp
: constant Entity_Id
:= Etype
(Expression
(N
));
3943 Loc
: constant Source_Ptr
:= Sloc
(N
);
3944 PtrT
: constant Entity_Id
:= Etype
(N
);
3946 procedure Rewrite_Coextension
(N
: Node_Id
);
3947 -- Static coextensions have the same lifetime as the entity they
3948 -- constrain. Such occurrences can be rewritten as aliased objects
3949 -- and their unrestricted access used instead of the coextension.
3951 function Size_In_Storage_Elements
(E
: Entity_Id
) return Node_Id
;
3952 -- Given a constrained array type E, returns a node representing the
3953 -- code to compute the size in storage elements for the given type.
3954 -- This is done without using the attribute (which malfunctions for
3957 -------------------------
3958 -- Rewrite_Coextension --
3959 -------------------------
3961 procedure Rewrite_Coextension
(N
: Node_Id
) is
3962 Temp_Id
: constant Node_Id
:= Make_Temporary
(Loc
, 'C');
3963 Temp_Decl
: Node_Id
;
3967 -- Cnn : aliased Etyp;
3970 Make_Object_Declaration
(Loc
,
3971 Defining_Identifier
=> Temp_Id
,
3972 Aliased_Present
=> True,
3973 Object_Definition
=> New_Occurrence_Of
(Etyp
, Loc
));
3975 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
3976 Set_Expression
(Temp_Decl
, Expression
(Expression
(N
)));
3979 Insert_Action
(N
, Temp_Decl
);
3981 Make_Attribute_Reference
(Loc
,
3982 Prefix
=> New_Occurrence_Of
(Temp_Id
, Loc
),
3983 Attribute_Name
=> Name_Unrestricted_Access
));
3985 Analyze_And_Resolve
(N
, PtrT
);
3986 end Rewrite_Coextension
;
3988 ------------------------------
3989 -- Size_In_Storage_Elements --
3990 ------------------------------
3992 function Size_In_Storage_Elements
(E
: Entity_Id
) return Node_Id
is
3994 -- Logically this just returns E'Max_Size_In_Storage_Elements.
3995 -- However, the reason for the existence of this function is
3996 -- to construct a test for sizes too large, which means near the
3997 -- 32-bit limit on a 32-bit machine, and precisely the trouble
3998 -- is that we get overflows when sizes are greater than 2**31.
4000 -- So what we end up doing for array types is to use the expression:
4002 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4004 -- which avoids this problem. All this is a bit bogus, but it does
4005 -- mean we catch common cases of trying to allocate arrays that
4006 -- are too large, and which in the absence of a check results in
4007 -- undetected chaos ???
4009 -- Note in particular that this is a pessimistic estimate in the
4010 -- case of packed array types, where an array element might occupy
4011 -- just a fraction of a storage element???
4018 for J
in 1 .. Number_Dimensions
(E
) loop
4020 Make_Attribute_Reference
(Loc
,
4021 Prefix
=> New_Occurrence_Of
(E
, Loc
),
4022 Attribute_Name
=> Name_Length
,
4023 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, J
)));
4030 Make_Op_Multiply
(Loc
,
4037 Make_Op_Multiply
(Loc
,
4040 Make_Attribute_Reference
(Loc
,
4041 Prefix
=> New_Occurrence_Of
(Component_Type
(E
), Loc
),
4042 Attribute_Name
=> Name_Max_Size_In_Storage_Elements
));
4044 end Size_In_Storage_Elements
;
4048 Dtyp
: constant Entity_Id
:= Available_View
(Designated_Type
(PtrT
));
4052 Rel_Typ
: Entity_Id
;
4055 -- Start of processing for Expand_N_Allocator
4058 -- RM E.2.3(22). We enforce that the expected type of an allocator
4059 -- shall not be a remote access-to-class-wide-limited-private type
4061 -- Why is this being done at expansion time, seems clearly wrong ???
4063 Validate_Remote_Access_To_Class_Wide_Type
(N
);
4065 -- Processing for anonymous access-to-controlled types. These access
4066 -- types receive a special finalization master which appears in the
4067 -- declarations of the enclosing semantic unit. This expansion is done
4068 -- now to ensure that any additional types generated by this routine or
4069 -- Expand_Allocator_Expression inherit the proper type attributes.
4071 if (Ekind
(PtrT
) = E_Anonymous_Access_Type
4072 or else (Is_Itype
(PtrT
) and then No
(Finalization_Master
(PtrT
))))
4073 and then Needs_Finalization
(Dtyp
)
4075 -- Detect the allocation of an anonymous controlled object where the
4076 -- type of the context is named. For example:
4078 -- procedure Proc (Ptr : Named_Access_Typ);
4079 -- Proc (new Designated_Typ);
4081 -- Regardless of the anonymous-to-named access type conversion, the
4082 -- lifetime of the object must be associated with the named access
4083 -- type. Use the finalization-related attributes of this type.
4085 if Nkind_In
(Parent
(N
), N_Type_Conversion
,
4086 N_Unchecked_Type_Conversion
)
4087 and then Ekind_In
(Etype
(Parent
(N
)), E_Access_Subtype
,
4089 E_General_Access_Type
)
4091 Rel_Typ
:= Etype
(Parent
(N
));
4096 -- Anonymous access-to-controlled types allocate on the global pool.
4097 -- Note that this is a "root type only" attribute.
4099 if No
(Associated_Storage_Pool
(PtrT
)) then
4100 if Present
(Rel_Typ
) then
4101 Set_Associated_Storage_Pool
4102 (Root_Type
(PtrT
), Associated_Storage_Pool
(Rel_Typ
));
4104 Set_Associated_Storage_Pool
4105 (Root_Type
(PtrT
), RTE
(RE_Global_Pool_Object
));
4109 -- The finalization master must be inserted and analyzed as part of
4110 -- the current semantic unit. Note that the master is updated when
4111 -- analysis changes current units. Note that this is a "root type
4114 if Present
(Rel_Typ
) then
4115 Set_Finalization_Master
4116 (Root_Type
(PtrT
), Finalization_Master
(Rel_Typ
));
4118 Build_Anonymous_Master
(Root_Type
(PtrT
));
4122 -- Set the storage pool and find the appropriate version of Allocate to
4123 -- call. Do not overwrite the storage pool if it is already set, which
4124 -- can happen for build-in-place function returns (see
4125 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4127 if No
(Storage_Pool
(N
)) then
4128 Pool
:= Associated_Storage_Pool
(Root_Type
(PtrT
));
4130 if Present
(Pool
) then
4131 Set_Storage_Pool
(N
, Pool
);
4133 if Is_RTE
(Pool
, RE_SS_Pool
) then
4134 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
4136 -- In the case of an allocator for a simple storage pool, locate
4137 -- and save a reference to the pool type's Allocate routine.
4139 elsif Present
(Get_Rep_Pragma
4140 (Etype
(Pool
), Name_Simple_Storage_Pool_Type
))
4143 Pool_Type
: constant Entity_Id
:= Base_Type
(Etype
(Pool
));
4144 Alloc_Op
: Entity_Id
;
4146 Alloc_Op
:= Get_Name_Entity_Id
(Name_Allocate
);
4147 while Present
(Alloc_Op
) loop
4148 if Scope
(Alloc_Op
) = Scope
(Pool_Type
)
4149 and then Present
(First_Formal
(Alloc_Op
))
4150 and then Etype
(First_Formal
(Alloc_Op
)) = Pool_Type
4152 Set_Procedure_To_Call
(N
, Alloc_Op
);
4155 Alloc_Op
:= Homonym
(Alloc_Op
);
4160 elsif Is_Class_Wide_Type
(Etype
(Pool
)) then
4161 Set_Procedure_To_Call
(N
, RTE
(RE_Allocate_Any
));
4164 Set_Procedure_To_Call
(N
,
4165 Find_Prim_Op
(Etype
(Pool
), Name_Allocate
));
4170 -- Under certain circumstances we can replace an allocator by an access
4171 -- to statically allocated storage. The conditions, as noted in AARM
4172 -- 3.10 (10c) are as follows:
4174 -- Size and initial value is known at compile time
4175 -- Access type is access-to-constant
4177 -- The allocator is not part of a constraint on a record component,
4178 -- because in that case the inserted actions are delayed until the
4179 -- record declaration is fully analyzed, which is too late for the
4180 -- analysis of the rewritten allocator.
4182 if Is_Access_Constant
(PtrT
)
4183 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
4184 and then Compile_Time_Known_Value
(Expression
(Expression
(N
)))
4185 and then Size_Known_At_Compile_Time
4186 (Etype
(Expression
(Expression
(N
))))
4187 and then not Is_Record_Type
(Current_Scope
)
4189 -- Here we can do the optimization. For the allocator
4193 -- We insert an object declaration
4195 -- Tnn : aliased x := y;
4197 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4198 -- marked as requiring static allocation.
4200 Temp
:= Make_Temporary
(Loc
, 'T', Expression
(Expression
(N
)));
4201 Desig
:= Subtype_Mark
(Expression
(N
));
4203 -- If context is constrained, use constrained subtype directly,
4204 -- so that the constant is not labelled as having a nominally
4205 -- unconstrained subtype.
4207 if Entity
(Desig
) = Base_Type
(Dtyp
) then
4208 Desig
:= New_Occurrence_Of
(Dtyp
, Loc
);
4212 Make_Object_Declaration
(Loc
,
4213 Defining_Identifier
=> Temp
,
4214 Aliased_Present
=> True,
4215 Constant_Present
=> Is_Access_Constant
(PtrT
),
4216 Object_Definition
=> Desig
,
4217 Expression
=> Expression
(Expression
(N
))));
4220 Make_Attribute_Reference
(Loc
,
4221 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
4222 Attribute_Name
=> Name_Unrestricted_Access
));
4224 Analyze_And_Resolve
(N
, PtrT
);
4226 -- We set the variable as statically allocated, since we don't want
4227 -- it going on the stack of the current procedure.
4229 Set_Is_Statically_Allocated
(Temp
);
4233 -- Same if the allocator is an access discriminant for a local object:
4234 -- instead of an allocator we create a local value and constrain the
4235 -- enclosing object with the corresponding access attribute.
4237 if Is_Static_Coextension
(N
) then
4238 Rewrite_Coextension
(N
);
4242 -- Check for size too large, we do this because the back end misses
4243 -- proper checks here and can generate rubbish allocation calls when
4244 -- we are near the limit. We only do this for the 32-bit address case
4245 -- since that is from a practical point of view where we see a problem.
4247 if System_Address_Size
= 32
4248 and then not Storage_Checks_Suppressed
(PtrT
)
4249 and then not Storage_Checks_Suppressed
(Dtyp
)
4250 and then not Storage_Checks_Suppressed
(Etyp
)
4252 -- The check we want to generate should look like
4254 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4255 -- raise Storage_Error;
4258 -- where 3.5 gigabytes is a constant large enough to accommodate any
4259 -- reasonable request for. But we can't do it this way because at
4260 -- least at the moment we don't compute this attribute right, and
4261 -- can silently give wrong results when the result gets large. Since
4262 -- this is all about large results, that's bad, so instead we only
4263 -- apply the check for constrained arrays, and manually compute the
4264 -- value of the attribute ???
4266 if Is_Array_Type
(Etyp
) and then Is_Constrained
(Etyp
) then
4268 Make_Raise_Storage_Error
(Loc
,
4271 Left_Opnd
=> Size_In_Storage_Elements
(Etyp
),
4273 Make_Integer_Literal
(Loc
, Uint_7
* (Uint_2
** 29))),
4274 Reason
=> SE_Object_Too_Large
));
4278 -- If no storage pool has been specified and we have the restriction
4279 -- No_Standard_Allocators_After_Elaboration is present, then generate
4280 -- a call to Elaboration_Allocators.Check_Standard_Allocator.
4282 if Nkind
(N
) = N_Allocator
4283 and then No
(Storage_Pool
(N
))
4284 and then Restriction_Active
(No_Standard_Allocators_After_Elaboration
)
4287 Make_Procedure_Call_Statement
(Loc
,
4289 New_Occurrence_Of
(RTE
(RE_Check_Standard_Allocator
), Loc
)));
4292 -- Handle case of qualified expression (other than optimization above)
4293 -- First apply constraint checks, because the bounds or discriminants
4294 -- in the aggregate might not match the subtype mark in the allocator.
4296 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
4298 Exp
: constant Node_Id
:= Expression
(Expression
(N
));
4299 Typ
: constant Entity_Id
:= Etype
(Expression
(N
));
4302 Apply_Constraint_Check
(Exp
, Typ
);
4303 Apply_Predicate_Check
(Exp
, Typ
);
4306 Expand_Allocator_Expression
(N
);
4310 -- If the allocator is for a type which requires initialization, and
4311 -- there is no initial value (i.e. operand is a subtype indication
4312 -- rather than a qualified expression), then we must generate a call to
4313 -- the initialization routine using an expressions action node:
4315 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4317 -- Here ptr_T is the pointer type for the allocator, and T is the
4318 -- subtype of the allocator. A special case arises if the designated
4319 -- type of the access type is a task or contains tasks. In this case
4320 -- the call to Init (Temp.all ...) is replaced by code that ensures
4321 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4322 -- for details). In addition, if the type T is a task type, then the
4323 -- first argument to Init must be converted to the task record type.
4326 T
: constant Entity_Id
:= Entity
(Expression
(N
));
4332 Init_Arg1
: Node_Id
;
4333 Init_Call
: Node_Id
;
4334 Temp_Decl
: Node_Id
;
4335 Temp_Type
: Entity_Id
;
4338 if No_Initialization
(N
) then
4340 -- Even though this might be a simple allocation, create a custom
4341 -- Allocate if the context requires it.
4343 if Present
(Finalization_Master
(PtrT
)) then
4344 Build_Allocate_Deallocate_Proc
4346 Is_Allocate
=> True);
4349 -- Case of no initialization procedure present
4351 elsif not Has_Non_Null_Base_Init_Proc
(T
) then
4353 -- Case of simple initialization required
4355 if Needs_Simple_Initialization
(T
) then
4356 Check_Restriction
(No_Default_Initialization
, N
);
4357 Rewrite
(Expression
(N
),
4358 Make_Qualified_Expression
(Loc
,
4359 Subtype_Mark
=> New_Occurrence_Of
(T
, Loc
),
4360 Expression
=> Get_Simple_Init_Val
(T
, N
)));
4362 Analyze_And_Resolve
(Expression
(Expression
(N
)), T
);
4363 Analyze_And_Resolve
(Expression
(N
), T
);
4364 Set_Paren_Count
(Expression
(Expression
(N
)), 1);
4365 Expand_N_Allocator
(N
);
4367 -- No initialization required
4373 -- Case of initialization procedure present, must be called
4376 Check_Restriction
(No_Default_Initialization
, N
);
4378 if not Restriction_Active
(No_Default_Initialization
) then
4379 Init
:= Base_Init_Proc
(T
);
4381 Temp
:= Make_Temporary
(Loc
, 'P');
4383 -- Construct argument list for the initialization routine call
4386 Make_Explicit_Dereference
(Loc
,
4388 New_Occurrence_Of
(Temp
, Loc
));
4390 Set_Assignment_OK
(Init_Arg1
);
4393 -- The initialization procedure expects a specific type. if the
4394 -- context is access to class wide, indicate that the object
4395 -- being allocated has the right specific type.
4397 if Is_Class_Wide_Type
(Dtyp
) then
4398 Init_Arg1
:= Unchecked_Convert_To
(T
, Init_Arg1
);
4401 -- If designated type is a concurrent type or if it is private
4402 -- type whose definition is a concurrent type, the first
4403 -- argument in the Init routine has to be unchecked conversion
4404 -- to the corresponding record type. If the designated type is
4405 -- a derived type, also convert the argument to its root type.
4407 if Is_Concurrent_Type
(T
) then
4409 Unchecked_Convert_To
(
4410 Corresponding_Record_Type
(T
), Init_Arg1
);
4412 elsif Is_Private_Type
(T
)
4413 and then Present
(Full_View
(T
))
4414 and then Is_Concurrent_Type
(Full_View
(T
))
4417 Unchecked_Convert_To
4418 (Corresponding_Record_Type
(Full_View
(T
)), Init_Arg1
);
4420 elsif Etype
(First_Formal
(Init
)) /= Base_Type
(T
) then
4422 Ftyp
: constant Entity_Id
:= Etype
(First_Formal
(Init
));
4425 Init_Arg1
:= OK_Convert_To
(Etype
(Ftyp
), Init_Arg1
);
4426 Set_Etype
(Init_Arg1
, Ftyp
);
4430 Args
:= New_List
(Init_Arg1
);
4432 -- For the task case, pass the Master_Id of the access type as
4433 -- the value of the _Master parameter, and _Chain as the value
4434 -- of the _Chain parameter (_Chain will be defined as part of
4435 -- the generated code for the allocator).
4437 -- In Ada 2005, the context may be a function that returns an
4438 -- anonymous access type. In that case the Master_Id has been
4439 -- created when expanding the function declaration.
4441 if Has_Task
(T
) then
4442 if No
(Master_Id
(Base_Type
(PtrT
))) then
4444 -- The designated type was an incomplete type, and the
4445 -- access type did not get expanded. Salvage it now.
4447 if not Restriction_Active
(No_Task_Hierarchy
) then
4448 if Present
(Parent
(Base_Type
(PtrT
))) then
4449 Expand_N_Full_Type_Declaration
4450 (Parent
(Base_Type
(PtrT
)));
4452 -- The only other possibility is an itype. For this
4453 -- case, the master must exist in the context. This is
4454 -- the case when the allocator initializes an access
4455 -- component in an init-proc.
4458 pragma Assert
(Is_Itype
(PtrT
));
4459 Build_Master_Renaming
(PtrT
, N
);
4464 -- If the context of the allocator is a declaration or an
4465 -- assignment, we can generate a meaningful image for it,
4466 -- even though subsequent assignments might remove the
4467 -- connection between task and entity. We build this image
4468 -- when the left-hand side is a simple variable, a simple
4469 -- indexed assignment or a simple selected component.
4471 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
4473 Nam
: constant Node_Id
:= Name
(Parent
(N
));
4476 if Is_Entity_Name
(Nam
) then
4478 Build_Task_Image_Decls
4481 (Entity
(Nam
), Sloc
(Nam
)), T
);
4483 elsif Nkind_In
(Nam
, N_Indexed_Component
,
4484 N_Selected_Component
)
4485 and then Is_Entity_Name
(Prefix
(Nam
))
4488 Build_Task_Image_Decls
4489 (Loc
, Nam
, Etype
(Prefix
(Nam
)));
4491 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
4495 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
4497 Build_Task_Image_Decls
4498 (Loc
, Defining_Identifier
(Parent
(N
)), T
);
4501 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
4504 if Restriction_Active
(No_Task_Hierarchy
) then
4506 New_Occurrence_Of
(RTE
(RE_Library_Task_Level
), Loc
));
4510 (Master_Id
(Base_Type
(Root_Type
(PtrT
))), Loc
));
4513 Append_To
(Args
, Make_Identifier
(Loc
, Name_uChain
));
4515 Decl
:= Last
(Decls
);
4517 New_Occurrence_Of
(Defining_Identifier
(Decl
), Loc
));
4519 -- Has_Task is false, Decls not used
4525 -- Add discriminants if discriminated type
4528 Dis
: Boolean := False;
4532 if Has_Discriminants
(T
) then
4536 -- Type may be a private type with no visible discriminants
4537 -- in which case check full view if in scope, or the
4538 -- underlying_full_view if dealing with a type whose full
4539 -- view may be derived from a private type whose own full
4540 -- view has discriminants.
4542 elsif Is_Private_Type
(T
) then
4543 if Present
(Full_View
(T
))
4544 and then Has_Discriminants
(Full_View
(T
))
4547 Typ
:= Full_View
(T
);
4549 elsif Present
(Underlying_Full_View
(T
))
4550 and then Has_Discriminants
(Underlying_Full_View
(T
))
4553 Typ
:= Underlying_Full_View
(T
);
4559 -- If the allocated object will be constrained by the
4560 -- default values for discriminants, then build a subtype
4561 -- with those defaults, and change the allocated subtype
4562 -- to that. Note that this happens in fewer cases in Ada
4565 if not Is_Constrained
(Typ
)
4566 and then Present
(Discriminant_Default_Value
4567 (First_Discriminant
(Typ
)))
4568 and then (Ada_Version
< Ada_2005
4570 Object_Type_Has_Constrained_Partial_View
4571 (Typ
, Current_Scope
))
4573 Typ
:= Build_Default_Subtype
(Typ
, N
);
4574 Set_Expression
(N
, New_Occurrence_Of
(Typ
, Loc
));
4577 Discr
:= First_Elmt
(Discriminant_Constraint
(Typ
));
4578 while Present
(Discr
) loop
4579 Nod
:= Node
(Discr
);
4580 Append
(New_Copy_Tree
(Node
(Discr
)), Args
);
4582 -- AI-416: when the discriminant constraint is an
4583 -- anonymous access type make sure an accessibility
4584 -- check is inserted if necessary (3.10.2(22.q/2))
4586 if Ada_Version
>= Ada_2005
4588 Ekind
(Etype
(Nod
)) = E_Anonymous_Access_Type
4590 Apply_Accessibility_Check
4591 (Nod
, Typ
, Insert_Node
=> Nod
);
4599 -- We set the allocator as analyzed so that when we analyze
4600 -- the if expression node, we do not get an unwanted recursive
4601 -- expansion of the allocator expression.
4603 Set_Analyzed
(N
, True);
4604 Nod
:= Relocate_Node
(N
);
4606 -- Here is the transformation:
4607 -- input: new Ctrl_Typ
4608 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4609 -- Ctrl_TypIP (Temp.all, ...);
4610 -- [Deep_]Initialize (Temp.all);
4612 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4613 -- is the subtype of the allocator.
4616 Make_Object_Declaration
(Loc
,
4617 Defining_Identifier
=> Temp
,
4618 Constant_Present
=> True,
4619 Object_Definition
=> New_Occurrence_Of
(Temp_Type
, Loc
),
4622 Set_Assignment_OK
(Temp_Decl
);
4623 Insert_Action
(N
, Temp_Decl
, Suppress
=> All_Checks
);
4625 Build_Allocate_Deallocate_Proc
(Temp_Decl
, True);
4627 -- If the designated type is a task type or contains tasks,
4628 -- create block to activate created tasks, and insert
4629 -- declaration for Task_Image variable ahead of call.
4631 if Has_Task
(T
) then
4633 L
: constant List_Id
:= New_List
;
4636 Build_Task_Allocate_Block
(L
, Nod
, Args
);
4638 Insert_List_Before
(First
(Declarations
(Blk
)), Decls
);
4639 Insert_Actions
(N
, L
);
4644 Make_Procedure_Call_Statement
(Loc
,
4645 Name
=> New_Occurrence_Of
(Init
, Loc
),
4646 Parameter_Associations
=> Args
));
4649 if Needs_Finalization
(T
) then
4652 -- [Deep_]Initialize (Init_Arg1);
4656 (Obj_Ref
=> New_Copy_Tree
(Init_Arg1
),
4659 -- Guard against a missing [Deep_]Initialize when the
4660 -- designated type was not properly frozen.
4662 if Present
(Init_Call
) then
4663 Insert_Action
(N
, Init_Call
);
4667 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
4668 Analyze_And_Resolve
(N
, PtrT
);
4673 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
4674 -- object that has been rewritten as a reference, we displace "this"
4675 -- to reference properly its secondary dispatch table.
4677 if Nkind
(N
) = N_Identifier
and then Is_Interface
(Dtyp
) then
4678 Displace_Allocator_Pointer
(N
);
4682 when RE_Not_Available
=>
4684 end Expand_N_Allocator
;
4686 -----------------------
4687 -- Expand_N_And_Then --
4688 -----------------------
4690 procedure Expand_N_And_Then
(N
: Node_Id
)
4691 renames Expand_Short_Circuit_Operator
;
4693 ------------------------------
4694 -- Expand_N_Case_Expression --
4695 ------------------------------
4697 procedure Expand_N_Case_Expression
(N
: Node_Id
) is
4698 Loc
: constant Source_Ptr
:= Sloc
(N
);
4699 Par
: constant Node_Id
:= Parent
(N
);
4700 Typ
: constant Entity_Id
:= Etype
(N
);
4703 Case_Stmt
: Node_Id
;
4707 Target_Typ
: Entity_Id
;
4709 In_Predicate
: Boolean := False;
4710 -- Flag set when the case expression appears within a predicate
4712 Optimize_Return_Stmt
: Boolean := False;
4713 -- Flag set when the case expression can be optimized in the context of
4714 -- a simple return statement.
4717 -- Check for MINIMIZED/ELIMINATED overflow mode
4719 if Minimized_Eliminated_Overflow_Check
(N
) then
4720 Apply_Arithmetic_Overflow_Check
(N
);
4724 -- If the case expression is a predicate specification, and the type
4725 -- to which it applies has a static predicate aspect, do not expand,
4726 -- because it will be converted to the proper predicate form later.
4728 if Ekind_In
(Current_Scope
, E_Function
, E_Procedure
)
4729 and then Is_Predicate_Function
(Current_Scope
)
4731 In_Predicate
:= True;
4733 if Has_Static_Predicate_Aspect
(Etype
(First_Entity
(Current_Scope
)))
4739 -- When the type of the case expression is elementary, expand
4741 -- (case X is when A => AX, when B => BX ...)
4756 -- In all other cases expand into
4759 -- type Ptr_Typ is access all Typ;
4760 -- Target : Ptr_Typ;
4763 -- Target := AX'Unrestricted_Access;
4765 -- Target := BX'Unrestricted_Access;
4768 -- in Target.all end;
4770 -- This approach avoids extra copies of potentially large objects. It
4771 -- also allows handling of values of limited or unconstrained types.
4773 -- Small optimization: when the case expression appears in the context
4774 -- of a simple return statement, expand into
4785 Make_Case_Statement
(Loc
,
4786 Expression
=> Expression
(N
),
4787 Alternatives
=> New_List
);
4789 -- Preserve the original context for which the case statement is being
4790 -- generated. This is needed by the finalization machinery to prevent
4791 -- the premature finalization of controlled objects found within the
4794 Set_From_Conditional_Expression
(Case_Stmt
);
4799 if Is_Elementary_Type
(Typ
) then
4802 -- ??? Do not perform the optimization when the return statement is
4803 -- within a predicate function as this causes supurious errors. Could
4804 -- this be a possible mismatch in handling this case somewhere else
4805 -- in semantic analysis?
4807 Optimize_Return_Stmt
:=
4808 Nkind
(Par
) = N_Simple_Return_Statement
and then not In_Predicate
;
4810 -- Otherwise create an access type to handle the general case using
4811 -- 'Unrestricted_Access.
4814 -- type Ptr_Typ is access all Typ;
4817 Target_Typ
:= Make_Temporary
(Loc
, 'P');
4820 Make_Full_Type_Declaration
(Loc
,
4821 Defining_Identifier
=> Target_Typ
,
4823 Make_Access_To_Object_Definition
(Loc
,
4824 All_Present
=> True,
4825 Subtype_Indication
=> New_Occurrence_Of
(Typ
, Loc
))));
4828 -- Create the declaration of the target which captures the value of the
4832 -- Target : [Ptr_]Typ;
4834 if not Optimize_Return_Stmt
then
4835 Target
:= Make_Temporary
(Loc
, 'T');
4838 Make_Object_Declaration
(Loc
,
4839 Defining_Identifier
=> Target
,
4840 Object_Definition
=> New_Occurrence_Of
(Target_Typ
, Loc
));
4841 Set_No_Initialization
(Decl
);
4843 Append_To
(Acts
, Decl
);
4846 -- Process the alternatives
4848 Alt
:= First
(Alternatives
(N
));
4849 while Present
(Alt
) loop
4851 Alt_Expr
: Node_Id
:= Expression
(Alt
);
4852 Alt_Loc
: constant Source_Ptr
:= Sloc
(Alt_Expr
);
4856 -- Take the unrestricted access of the expression value for non-
4857 -- scalar types. This approach avoids big copies and covers the
4858 -- limited and unconstrained cases.
4861 -- AX'Unrestricted_Access
4863 if not Is_Elementary_Type
(Typ
) then
4865 Make_Attribute_Reference
(Alt_Loc
,
4866 Prefix
=> Relocate_Node
(Alt_Expr
),
4867 Attribute_Name
=> Name_Unrestricted_Access
);
4871 -- return AX['Unrestricted_Access];
4873 if Optimize_Return_Stmt
then
4875 Make_Simple_Return_Statement
(Alt_Loc
,
4876 Expression
=> Alt_Expr
));
4879 -- Target := AX['Unrestricted_Access];
4883 Make_Assignment_Statement
(Alt_Loc
,
4884 Name
=> New_Occurrence_Of
(Target
, Loc
),
4885 Expression
=> Alt_Expr
));
4888 -- Propagate declarations inserted in the node by Insert_Actions
4889 -- (for example, temporaries generated to remove side effects).
4890 -- These actions must remain attached to the alternative, given
4891 -- that they are generated by the corresponding expression.
4893 if Present
(Actions
(Alt
)) then
4894 Prepend_List
(Actions
(Alt
), Stmts
);
4897 -- Finalize any transient objects on exit from the alternative.
4898 -- This is done only in the return optimization case because
4899 -- otherwise the case expression is converted into an expression
4900 -- with actions which already contains this form of processing.
4902 if Optimize_Return_Stmt
then
4903 Process_If_Case_Statements
(N
, Stmts
);
4907 (Alternatives
(Case_Stmt
),
4908 Make_Case_Statement_Alternative
(Sloc
(Alt
),
4909 Discrete_Choices
=> Discrete_Choices
(Alt
),
4910 Statements
=> Stmts
));
4916 -- Rewrite the parent return statement as a case statement
4918 if Optimize_Return_Stmt
then
4919 Rewrite
(Par
, Case_Stmt
);
4922 -- Otherwise convert the case expression into an expression with actions
4925 Append_To
(Acts
, Case_Stmt
);
4927 if Is_Elementary_Type
(Typ
) then
4928 Expr
:= New_Occurrence_Of
(Target
, Loc
);
4932 Make_Explicit_Dereference
(Loc
,
4933 Prefix
=> New_Occurrence_Of
(Target
, Loc
));
4939 -- in Target[.all] end;
4942 Make_Expression_With_Actions
(Loc
,
4946 Analyze_And_Resolve
(N
, Typ
);
4948 end Expand_N_Case_Expression
;
4950 -----------------------------------
4951 -- Expand_N_Explicit_Dereference --
4952 -----------------------------------
4954 procedure Expand_N_Explicit_Dereference
(N
: Node_Id
) is
4956 -- Insert explicit dereference call for the checked storage pool case
4958 Insert_Dereference_Action
(Prefix
(N
));
4960 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
4961 -- we set the atomic sync flag.
4963 if Is_Atomic
(Etype
(N
))
4964 and then not Atomic_Synchronization_Disabled
(Etype
(N
))
4966 Activate_Atomic_Synchronization
(N
);
4968 end Expand_N_Explicit_Dereference
;
4970 --------------------------------------
4971 -- Expand_N_Expression_With_Actions --
4972 --------------------------------------
4974 procedure Expand_N_Expression_With_Actions
(N
: Node_Id
) is
4975 Acts
: constant List_Id
:= Actions
(N
);
4977 procedure Force_Boolean_Evaluation
(Expr
: Node_Id
);
4978 -- Force the evaluation of Boolean expression Expr
4980 function Process_Action
(Act
: Node_Id
) return Traverse_Result
;
4981 -- Inspect and process a single action of an expression_with_actions for
4982 -- transient objects. If such objects are found, the routine generates
4983 -- code to clean them up when the context of the expression is evaluated
4986 ------------------------------
4987 -- Force_Boolean_Evaluation --
4988 ------------------------------
4990 procedure Force_Boolean_Evaluation
(Expr
: Node_Id
) is
4991 Loc
: constant Source_Ptr
:= Sloc
(N
);
4992 Flag_Decl
: Node_Id
;
4993 Flag_Id
: Entity_Id
;
4996 -- Relocate the expression to the actions list by capturing its value
4997 -- in a Boolean flag. Generate:
4998 -- Flag : constant Boolean := Expr;
5000 Flag_Id
:= Make_Temporary
(Loc
, 'F');
5003 Make_Object_Declaration
(Loc
,
5004 Defining_Identifier
=> Flag_Id
,
5005 Constant_Present
=> True,
5006 Object_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
),
5007 Expression
=> Relocate_Node
(Expr
));
5009 Append
(Flag_Decl
, Acts
);
5010 Analyze
(Flag_Decl
);
5012 -- Replace the expression with a reference to the flag
5014 Rewrite
(Expression
(N
), New_Occurrence_Of
(Flag_Id
, Loc
));
5015 Analyze
(Expression
(N
));
5016 end Force_Boolean_Evaluation
;
5018 --------------------
5019 -- Process_Action --
5020 --------------------
5022 function Process_Action
(Act
: Node_Id
) return Traverse_Result
is
5024 if Nkind
(Act
) = N_Object_Declaration
5025 and then Is_Finalizable_Transient
(Act
, N
)
5027 Process_Transient_In_Expression
(Act
, N
, Acts
);
5030 -- Avoid processing temporary function results multiple times when
5031 -- dealing with nested expression_with_actions.
5033 elsif Nkind
(Act
) = N_Expression_With_Actions
then
5036 -- Do not process temporary function results in loops. This is done
5037 -- by Expand_N_Loop_Statement and Build_Finalizer.
5039 elsif Nkind
(Act
) = N_Loop_Statement
then
5046 procedure Process_Single_Action
is new Traverse_Proc
(Process_Action
);
5052 -- Start of processing for Expand_N_Expression_With_Actions
5055 -- Do not evaluate the expression when it denotes an entity because the
5056 -- expression_with_actions node will be replaced by the reference.
5058 if Is_Entity_Name
(Expression
(N
)) then
5061 -- Do not evaluate the expression when there are no actions because the
5062 -- expression_with_actions node will be replaced by the expression.
5064 elsif No
(Acts
) or else Is_Empty_List
(Acts
) then
5067 -- Force the evaluation of the expression by capturing its value in a
5068 -- temporary. This ensures that aliases of transient objects do not leak
5069 -- to the expression of the expression_with_actions node:
5072 -- Trans_Id : Ctrl_Typ := ...;
5073 -- Alias : ... := Trans_Id;
5074 -- in ... Alias ... end;
5076 -- In the example above, Trans_Id cannot be finalized at the end of the
5077 -- actions list because this may affect the alias and the final value of
5078 -- the expression_with_actions. Forcing the evaluation encapsulates the
5079 -- reference to the Alias within the actions list:
5082 -- Trans_Id : Ctrl_Typ := ...;
5083 -- Alias : ... := Trans_Id;
5084 -- Val : constant Boolean := ... Alias ...;
5085 -- <finalize Trans_Id>
5088 -- Once this transformation is performed, it is safe to finalize the
5089 -- transient object at the end of the actions list.
5091 -- Note that Force_Evaluation does not remove side effects in operators
5092 -- because it assumes that all operands are evaluated and side effect
5093 -- free. This is not the case when an operand depends implicitly on the
5094 -- transient object through the use of access types.
5096 elsif Is_Boolean_Type
(Etype
(Expression
(N
))) then
5097 Force_Boolean_Evaluation
(Expression
(N
));
5099 -- The expression of an expression_with_actions node may not necessarily
5100 -- be Boolean when the node appears in an if expression. In this case do
5101 -- the usual forced evaluation to encapsulate potential aliasing.
5104 Force_Evaluation
(Expression
(N
));
5107 -- Process all transient objects found within the actions of the EWA
5110 Act
:= First
(Acts
);
5111 while Present
(Act
) loop
5112 Process_Single_Action
(Act
);
5116 -- Deal with case where there are no actions. In this case we simply
5117 -- rewrite the node with its expression since we don't need the actions
5118 -- and the specification of this node does not allow a null action list.
5120 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5121 -- the expanded tree and relying on being able to retrieve the original
5122 -- tree in cases like this. This raises a whole lot of issues of whether
5123 -- we have problems elsewhere, which will be addressed in the future???
5125 if Is_Empty_List
(Acts
) then
5126 Rewrite
(N
, Relocate_Node
(Expression
(N
)));
5128 end Expand_N_Expression_With_Actions
;
5130 ----------------------------
5131 -- Expand_N_If_Expression --
5132 ----------------------------
5134 -- Deal with limited types and condition actions
5136 procedure Expand_N_If_Expression
(N
: Node_Id
) is
5137 Cond
: constant Node_Id
:= First
(Expressions
(N
));
5138 Loc
: constant Source_Ptr
:= Sloc
(N
);
5139 Thenx
: constant Node_Id
:= Next
(Cond
);
5140 Elsex
: constant Node_Id
:= Next
(Thenx
);
5141 Typ
: constant Entity_Id
:= Etype
(N
);
5149 Ptr_Typ
: Entity_Id
;
5152 -- Check for MINIMIZED/ELIMINATED overflow mode
5154 if Minimized_Eliminated_Overflow_Check
(N
) then
5155 Apply_Arithmetic_Overflow_Check
(N
);
5159 -- Fold at compile time if condition known. We have already folded
5160 -- static if expressions, but it is possible to fold any case in which
5161 -- the condition is known at compile time, even though the result is
5164 -- Note that we don't do the fold of such cases in Sem_Elab because
5165 -- it can cause infinite loops with the expander adding a conditional
5166 -- expression, and Sem_Elab circuitry removing it repeatedly.
5168 if Compile_Time_Known_Value
(Cond
) then
5170 function Fold_Known_Value
(Cond
: Node_Id
) return Boolean;
5171 -- Fold at compile time. Assumes condition known. Return True if
5172 -- folding occurred, meaning we're done.
5174 ----------------------
5175 -- Fold_Known_Value --
5176 ----------------------
5178 function Fold_Known_Value
(Cond
: Node_Id
) return Boolean is
5180 if Is_True
(Expr_Value
(Cond
)) then
5182 Actions
:= Then_Actions
(N
);
5185 Actions
:= Else_Actions
(N
);
5190 if Present
(Actions
) then
5192 -- To minimize the use of Expression_With_Actions, just skip
5193 -- the optimization as it is not critical for correctness.
5195 if Minimize_Expression_With_Actions
then
5200 Make_Expression_With_Actions
(Loc
,
5201 Expression
=> Relocate_Node
(Expr
),
5202 Actions
=> Actions
));
5203 Analyze_And_Resolve
(N
, Typ
);
5206 Rewrite
(N
, Relocate_Node
(Expr
));
5209 -- Note that the result is never static (legitimate cases of
5210 -- static if expressions were folded in Sem_Eval).
5212 Set_Is_Static_Expression
(N
, False);
5214 end Fold_Known_Value
;
5217 if Fold_Known_Value
(Cond
) then
5223 -- If the type is limited, and the back end does not handle limited
5224 -- types, then we expand as follows to avoid the possibility of
5225 -- improper copying.
5227 -- type Ptr is access all Typ;
5231 -- Cnn := then-expr'Unrestricted_Access;
5234 -- Cnn := else-expr'Unrestricted_Access;
5237 -- and replace the if expression by a reference to Cnn.all.
5239 -- This special case can be skipped if the back end handles limited
5240 -- types properly and ensures that no incorrect copies are made.
5242 if Is_By_Reference_Type
(Typ
)
5243 and then not Back_End_Handles_Limited_Types
5245 -- When the "then" or "else" expressions involve controlled function
5246 -- calls, generated temporaries are chained on the corresponding list
5247 -- of actions. These temporaries need to be finalized after the if
5248 -- expression is evaluated.
5250 Process_If_Case_Statements
(N
, Then_Actions
(N
));
5251 Process_If_Case_Statements
(N
, Else_Actions
(N
));
5254 -- type Ann is access all Typ;
5256 Ptr_Typ
:= Make_Temporary
(Loc
, 'A');
5259 Make_Full_Type_Declaration
(Loc
,
5260 Defining_Identifier
=> Ptr_Typ
,
5262 Make_Access_To_Object_Definition
(Loc
,
5263 All_Present
=> True,
5264 Subtype_Indication
=> New_Occurrence_Of
(Typ
, Loc
))));
5269 Cnn
:= Make_Temporary
(Loc
, 'C', N
);
5272 Make_Object_Declaration
(Loc
,
5273 Defining_Identifier
=> Cnn
,
5274 Object_Definition
=> New_Occurrence_Of
(Ptr_Typ
, Loc
));
5278 -- Cnn := <Thenx>'Unrestricted_Access;
5280 -- Cnn := <Elsex>'Unrestricted_Access;
5284 Make_Implicit_If_Statement
(N
,
5285 Condition
=> Relocate_Node
(Cond
),
5286 Then_Statements
=> New_List
(
5287 Make_Assignment_Statement
(Sloc
(Thenx
),
5288 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Thenx
)),
5290 Make_Attribute_Reference
(Loc
,
5291 Prefix
=> Relocate_Node
(Thenx
),
5292 Attribute_Name
=> Name_Unrestricted_Access
))),
5294 Else_Statements
=> New_List
(
5295 Make_Assignment_Statement
(Sloc
(Elsex
),
5296 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Elsex
)),
5298 Make_Attribute_Reference
(Loc
,
5299 Prefix
=> Relocate_Node
(Elsex
),
5300 Attribute_Name
=> Name_Unrestricted_Access
))));
5302 -- Preserve the original context for which the if statement is being
5303 -- generated. This is needed by the finalization machinery to prevent
5304 -- the premature finalization of controlled objects found within the
5307 Set_From_Conditional_Expression
(New_If
);
5310 Make_Explicit_Dereference
(Loc
,
5311 Prefix
=> New_Occurrence_Of
(Cnn
, Loc
));
5313 -- If the result is an unconstrained array and the if expression is in a
5314 -- context other than the initializing expression of the declaration of
5315 -- an object, then we pull out the if expression as follows:
5317 -- Cnn : constant typ := if-expression
5319 -- and then replace the if expression with an occurrence of Cnn. This
5320 -- avoids the need in the back end to create on-the-fly variable length
5321 -- temporaries (which it cannot do!)
5323 -- Note that the test for being in an object declaration avoids doing an
5324 -- unnecessary expansion, and also avoids infinite recursion.
5326 elsif Is_Array_Type
(Typ
) and then not Is_Constrained
(Typ
)
5327 and then (Nkind
(Parent
(N
)) /= N_Object_Declaration
5328 or else Expression
(Parent
(N
)) /= N
)
5331 Cnn
: constant Node_Id
:= Make_Temporary
(Loc
, 'C', N
);
5334 Make_Object_Declaration
(Loc
,
5335 Defining_Identifier
=> Cnn
,
5336 Constant_Present
=> True,
5337 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
5338 Expression
=> Relocate_Node
(N
),
5339 Has_Init_Expression
=> True));
5341 Rewrite
(N
, New_Occurrence_Of
(Cnn
, Loc
));
5345 -- For other types, we only need to expand if there are other actions
5346 -- associated with either branch.
5348 elsif Present
(Then_Actions
(N
)) or else Present
(Else_Actions
(N
)) then
5350 -- We now wrap the actions into the appropriate expression
5352 if Minimize_Expression_With_Actions
5353 and then (Is_Elementary_Type
(Underlying_Type
(Typ
))
5354 or else Is_Constrained
(Underlying_Type
(Typ
)))
5356 -- If we can't use N_Expression_With_Actions nodes, then we insert
5357 -- the following sequence of actions (using Insert_Actions):
5362 -- Cnn := then-expr;
5368 -- and replace the if expression by a reference to Cnn
5370 Cnn
:= Make_Temporary
(Loc
, 'C', N
);
5373 Make_Object_Declaration
(Loc
,
5374 Defining_Identifier
=> Cnn
,
5375 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
5378 Make_Implicit_If_Statement
(N
,
5379 Condition
=> Relocate_Node
(Cond
),
5381 Then_Statements
=> New_List
(
5382 Make_Assignment_Statement
(Sloc
(Thenx
),
5383 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Thenx
)),
5384 Expression
=> Relocate_Node
(Thenx
))),
5386 Else_Statements
=> New_List
(
5387 Make_Assignment_Statement
(Sloc
(Elsex
),
5388 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Elsex
)),
5389 Expression
=> Relocate_Node
(Elsex
))));
5391 Set_Assignment_OK
(Name
(First
(Then_Statements
(New_If
))));
5392 Set_Assignment_OK
(Name
(First
(Else_Statements
(New_If
))));
5394 New_N
:= New_Occurrence_Of
(Cnn
, Loc
);
5396 -- Regular path using Expression_With_Actions
5399 if Present
(Then_Actions
(N
)) then
5401 Make_Expression_With_Actions
(Sloc
(Thenx
),
5402 Actions
=> Then_Actions
(N
),
5403 Expression
=> Relocate_Node
(Thenx
)));
5405 Set_Then_Actions
(N
, No_List
);
5406 Analyze_And_Resolve
(Thenx
, Typ
);
5409 if Present
(Else_Actions
(N
)) then
5411 Make_Expression_With_Actions
(Sloc
(Elsex
),
5412 Actions
=> Else_Actions
(N
),
5413 Expression
=> Relocate_Node
(Elsex
)));
5415 Set_Else_Actions
(N
, No_List
);
5416 Analyze_And_Resolve
(Elsex
, Typ
);
5422 -- If no actions then no expansion needed, gigi will handle it using the
5423 -- same approach as a C conditional expression.
5429 -- Fall through here for either the limited expansion, or the case of
5430 -- inserting actions for non-limited types. In both these cases, we must
5431 -- move the SLOC of the parent If statement to the newly created one and
5432 -- change it to the SLOC of the expression which, after expansion, will
5433 -- correspond to what is being evaluated.
5435 if Present
(Parent
(N
)) and then Nkind
(Parent
(N
)) = N_If_Statement
then
5436 Set_Sloc
(New_If
, Sloc
(Parent
(N
)));
5437 Set_Sloc
(Parent
(N
), Loc
);
5440 -- Make sure Then_Actions and Else_Actions are appropriately moved
5441 -- to the new if statement.
5443 if Present
(Then_Actions
(N
)) then
5445 (First
(Then_Statements
(New_If
)), Then_Actions
(N
));
5448 if Present
(Else_Actions
(N
)) then
5450 (First
(Else_Statements
(New_If
)), Else_Actions
(N
));
5453 Insert_Action
(N
, Decl
);
5454 Insert_Action
(N
, New_If
);
5456 Analyze_And_Resolve
(N
, Typ
);
5457 end Expand_N_If_Expression
;
5463 procedure Expand_N_In
(N
: Node_Id
) is
5464 Loc
: constant Source_Ptr
:= Sloc
(N
);
5465 Restyp
: constant Entity_Id
:= Etype
(N
);
5466 Lop
: constant Node_Id
:= Left_Opnd
(N
);
5467 Rop
: constant Node_Id
:= Right_Opnd
(N
);
5468 Static
: constant Boolean := Is_OK_Static_Expression
(N
);
5470 procedure Substitute_Valid_Check
;
5471 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5472 -- test for the left operand being in range of its subtype.
5474 ----------------------------
5475 -- Substitute_Valid_Check --
5476 ----------------------------
5478 procedure Substitute_Valid_Check
is
5479 function Is_OK_Object_Reference
(Nod
: Node_Id
) return Boolean;
5480 -- Determine whether arbitrary node Nod denotes a source object that
5481 -- may safely act as prefix of attribute 'Valid.
5483 ----------------------------
5484 -- Is_OK_Object_Reference --
5485 ----------------------------
5487 function Is_OK_Object_Reference
(Nod
: Node_Id
) return Boolean is
5491 -- Inspect the original operand
5493 Obj_Ref
:= Original_Node
(Nod
);
5495 -- The object reference must be a source construct, otherwise the
5496 -- codefix suggestion may refer to nonexistent code from a user
5499 if Comes_From_Source
(Obj_Ref
) then
5501 -- Recover the actual object reference. There may be more cases
5505 if Nkind_In
(Obj_Ref
, N_Type_Conversion
,
5506 N_Unchecked_Type_Conversion
)
5508 Obj_Ref
:= Expression
(Obj_Ref
);
5514 return Is_Object_Reference
(Obj_Ref
);
5518 end Is_OK_Object_Reference
;
5520 -- Start of processing for Substitute_Valid_Check
5524 Make_Attribute_Reference
(Loc
,
5525 Prefix
=> Relocate_Node
(Lop
),
5526 Attribute_Name
=> Name_Valid
));
5528 Analyze_And_Resolve
(N
, Restyp
);
5530 -- Emit a warning when the left-hand operand of the membership test
5531 -- is a source object, otherwise the use of attribute 'Valid would be
5532 -- illegal. The warning is not given when overflow checking is either
5533 -- MINIMIZED or ELIMINATED, as the danger of optimization has been
5534 -- eliminated above.
5536 if Is_OK_Object_Reference
(Lop
)
5537 and then Overflow_Check_Mode
not in Minimized_Or_Eliminated
5540 ("??explicit membership test may be optimized away", N
);
5541 Error_Msg_N
-- CODEFIX
5542 ("\??use ''Valid attribute instead", N
);
5544 end Substitute_Valid_Check
;
5551 -- Start of processing for Expand_N_In
5554 -- If set membership case, expand with separate procedure
5556 if Present
(Alternatives
(N
)) then
5557 Expand_Set_Membership
(N
);
5561 -- Not set membership, proceed with expansion
5563 Ltyp
:= Etype
(Left_Opnd
(N
));
5564 Rtyp
:= Etype
(Right_Opnd
(N
));
5566 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
5567 -- type, then expand with a separate procedure. Note the use of the
5568 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5570 if Overflow_Check_Mode
in Minimized_Or_Eliminated
5571 and then Is_Signed_Integer_Type
(Ltyp
)
5572 and then not No_Minimize_Eliminate
(N
)
5574 Expand_Membership_Minimize_Eliminate_Overflow
(N
);
5578 -- Check case of explicit test for an expression in range of its
5579 -- subtype. This is suspicious usage and we replace it with a 'Valid
5580 -- test and give a warning for scalar types.
5582 if Is_Scalar_Type
(Ltyp
)
5584 -- Only relevant for source comparisons
5586 and then Comes_From_Source
(N
)
5588 -- In floating-point this is a standard way to check for finite values
5589 -- and using 'Valid would typically be a pessimization.
5591 and then not Is_Floating_Point_Type
(Ltyp
)
5593 -- Don't give the message unless right operand is a type entity and
5594 -- the type of the left operand matches this type. Note that this
5595 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
5596 -- checks have changed the type of the left operand.
5598 and then Nkind
(Rop
) in N_Has_Entity
5599 and then Ltyp
= Entity
(Rop
)
5601 -- Skip this for predicated types, where such expressions are a
5602 -- reasonable way of testing if something meets the predicate.
5604 and then not Present
(Predicate_Function
(Ltyp
))
5606 Substitute_Valid_Check
;
5610 -- Do validity check on operands
5612 if Validity_Checks_On
and Validity_Check_Operands
then
5613 Ensure_Valid
(Left_Opnd
(N
));
5614 Validity_Check_Range
(Right_Opnd
(N
));
5617 -- Case of explicit range
5619 if Nkind
(Rop
) = N_Range
then
5621 Lo
: constant Node_Id
:= Low_Bound
(Rop
);
5622 Hi
: constant Node_Id
:= High_Bound
(Rop
);
5624 Lo_Orig
: constant Node_Id
:= Original_Node
(Lo
);
5625 Hi_Orig
: constant Node_Id
:= Original_Node
(Hi
);
5627 Lcheck
: Compare_Result
;
5628 Ucheck
: Compare_Result
;
5630 Warn1
: constant Boolean :=
5631 Constant_Condition_Warnings
5632 and then Comes_From_Source
(N
)
5633 and then not In_Instance
;
5634 -- This must be true for any of the optimization warnings, we
5635 -- clearly want to give them only for source with the flag on. We
5636 -- also skip these warnings in an instance since it may be the
5637 -- case that different instantiations have different ranges.
5639 Warn2
: constant Boolean :=
5641 and then Nkind
(Original_Node
(Rop
)) = N_Range
5642 and then Is_Integer_Type
(Etype
(Lo
));
5643 -- For the case where only one bound warning is elided, we also
5644 -- insist on an explicit range and an integer type. The reason is
5645 -- that the use of enumeration ranges including an end point is
5646 -- common, as is the use of a subtype name, one of whose bounds is
5647 -- the same as the type of the expression.
5650 -- If test is explicit x'First .. x'Last, replace by valid check
5652 -- Could use some individual comments for this complex test ???
5654 if Is_Scalar_Type
(Ltyp
)
5656 -- And left operand is X'First where X matches left operand
5657 -- type (this eliminates cases of type mismatch, including
5658 -- the cases where ELIMINATED/MINIMIZED mode has changed the
5659 -- type of the left operand.
5661 and then Nkind
(Lo_Orig
) = N_Attribute_Reference
5662 and then Attribute_Name
(Lo_Orig
) = Name_First
5663 and then Nkind
(Prefix
(Lo_Orig
)) in N_Has_Entity
5664 and then Entity
(Prefix
(Lo_Orig
)) = Ltyp
5666 -- Same tests for right operand
5668 and then Nkind
(Hi_Orig
) = N_Attribute_Reference
5669 and then Attribute_Name
(Hi_Orig
) = Name_Last
5670 and then Nkind
(Prefix
(Hi_Orig
)) in N_Has_Entity
5671 and then Entity
(Prefix
(Hi_Orig
)) = Ltyp
5673 -- Relevant only for source cases
5675 and then Comes_From_Source
(N
)
5677 Substitute_Valid_Check
;
5681 -- If bounds of type are known at compile time, and the end points
5682 -- are known at compile time and identical, this is another case
5683 -- for substituting a valid test. We only do this for discrete
5684 -- types, since it won't arise in practice for float types.
5686 if Comes_From_Source
(N
)
5687 and then Is_Discrete_Type
(Ltyp
)
5688 and then Compile_Time_Known_Value
(Type_High_Bound
(Ltyp
))
5689 and then Compile_Time_Known_Value
(Type_Low_Bound
(Ltyp
))
5690 and then Compile_Time_Known_Value
(Lo
)
5691 and then Compile_Time_Known_Value
(Hi
)
5692 and then Expr_Value
(Type_High_Bound
(Ltyp
)) = Expr_Value
(Hi
)
5693 and then Expr_Value
(Type_Low_Bound
(Ltyp
)) = Expr_Value
(Lo
)
5695 -- Kill warnings in instances, since they may be cases where we
5696 -- have a test in the generic that makes sense with some types
5697 -- and not with other types.
5699 and then not In_Instance
5701 Substitute_Valid_Check
;
5705 -- If we have an explicit range, do a bit of optimization based on
5706 -- range analysis (we may be able to kill one or both checks).
5708 Lcheck
:= Compile_Time_Compare
(Lop
, Lo
, Assume_Valid
=> False);
5709 Ucheck
:= Compile_Time_Compare
(Lop
, Hi
, Assume_Valid
=> False);
5711 -- If either check is known to fail, replace result by False since
5712 -- the other check does not matter. Preserve the static flag for
5713 -- legality checks, because we are constant-folding beyond RM 4.9.
5715 if Lcheck
= LT
or else Ucheck
= GT
then
5717 Error_Msg_N
("?c?range test optimized away", N
);
5718 Error_Msg_N
("\?c?value is known to be out of range", N
);
5721 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
5722 Analyze_And_Resolve
(N
, Restyp
);
5723 Set_Is_Static_Expression
(N
, Static
);
5726 -- If both checks are known to succeed, replace result by True,
5727 -- since we know we are in range.
5729 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
5731 Error_Msg_N
("?c?range test optimized away", N
);
5732 Error_Msg_N
("\?c?value is known to be in range", N
);
5735 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
5736 Analyze_And_Resolve
(N
, Restyp
);
5737 Set_Is_Static_Expression
(N
, Static
);
5740 -- If lower bound check succeeds and upper bound check is not
5741 -- known to succeed or fail, then replace the range check with
5742 -- a comparison against the upper bound.
5744 elsif Lcheck
in Compare_GE
then
5745 if Warn2
and then not In_Instance
then
5746 Error_Msg_N
("??lower bound test optimized away", Lo
);
5747 Error_Msg_N
("\??value is known to be in range", Lo
);
5753 Right_Opnd
=> High_Bound
(Rop
)));
5754 Analyze_And_Resolve
(N
, Restyp
);
5757 -- If upper bound check succeeds and lower bound check is not
5758 -- known to succeed or fail, then replace the range check with
5759 -- a comparison against the lower bound.
5761 elsif Ucheck
in Compare_LE
then
5762 if Warn2
and then not In_Instance
then
5763 Error_Msg_N
("??upper bound test optimized away", Hi
);
5764 Error_Msg_N
("\??value is known to be in range", Hi
);
5770 Right_Opnd
=> Low_Bound
(Rop
)));
5771 Analyze_And_Resolve
(N
, Restyp
);
5775 -- We couldn't optimize away the range check, but there is one
5776 -- more issue. If we are checking constant conditionals, then we
5777 -- see if we can determine the outcome assuming everything is
5778 -- valid, and if so give an appropriate warning.
5780 if Warn1
and then not Assume_No_Invalid_Values
then
5781 Lcheck
:= Compile_Time_Compare
(Lop
, Lo
, Assume_Valid
=> True);
5782 Ucheck
:= Compile_Time_Compare
(Lop
, Hi
, Assume_Valid
=> True);
5784 -- Result is out of range for valid value
5786 if Lcheck
= LT
or else Ucheck
= GT
then
5788 ("?c?value can only be in range if it is invalid", N
);
5790 -- Result is in range for valid value
5792 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
5794 ("?c?value can only be out of range if it is invalid", N
);
5796 -- Lower bound check succeeds if value is valid
5798 elsif Warn2
and then Lcheck
in Compare_GE
then
5800 ("?c?lower bound check only fails if it is invalid", Lo
);
5802 -- Upper bound check succeeds if value is valid
5804 elsif Warn2
and then Ucheck
in Compare_LE
then
5806 ("?c?upper bound check only fails for invalid values", Hi
);
5811 -- For all other cases of an explicit range, nothing to be done
5815 -- Here right operand is a subtype mark
5819 Typ
: Entity_Id
:= Etype
(Rop
);
5820 Is_Acc
: constant Boolean := Is_Access_Type
(Typ
);
5821 Cond
: Node_Id
:= Empty
;
5823 Obj
: Node_Id
:= Lop
;
5824 SCIL_Node
: Node_Id
;
5827 Remove_Side_Effects
(Obj
);
5829 -- For tagged type, do tagged membership operation
5831 if Is_Tagged_Type
(Typ
) then
5833 -- No expansion will be performed for VM targets, as the VM
5834 -- back-ends will handle the membership tests directly.
5836 if Tagged_Type_Expansion
then
5837 Tagged_Membership
(N
, SCIL_Node
, New_N
);
5839 Analyze_And_Resolve
(N
, Restyp
);
5841 -- Update decoration of relocated node referenced by the
5844 if Generate_SCIL
and then Present
(SCIL_Node
) then
5845 Set_SCIL_Node
(N
, SCIL_Node
);
5851 -- If type is scalar type, rewrite as x in t'First .. t'Last.
5852 -- This reason we do this is that the bounds may have the wrong
5853 -- type if they come from the original type definition. Also this
5854 -- way we get all the processing above for an explicit range.
5856 -- Don't do this for predicated types, since in this case we
5857 -- want to check the predicate.
5859 elsif Is_Scalar_Type
(Typ
) then
5860 if No
(Predicate_Function
(Typ
)) then
5864 Make_Attribute_Reference
(Loc
,
5865 Attribute_Name
=> Name_First
,
5866 Prefix
=> New_Occurrence_Of
(Typ
, Loc
)),
5869 Make_Attribute_Reference
(Loc
,
5870 Attribute_Name
=> Name_Last
,
5871 Prefix
=> New_Occurrence_Of
(Typ
, Loc
))));
5872 Analyze_And_Resolve
(N
, Restyp
);
5877 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5878 -- a membership test if the subtype mark denotes a constrained
5879 -- Unchecked_Union subtype and the expression lacks inferable
5882 elsif Is_Unchecked_Union
(Base_Type
(Typ
))
5883 and then Is_Constrained
(Typ
)
5884 and then not Has_Inferable_Discriminants
(Lop
)
5887 Make_Raise_Program_Error
(Loc
,
5888 Reason
=> PE_Unchecked_Union_Restriction
));
5890 -- Prevent Gigi from generating incorrect code by rewriting the
5891 -- test as False. What is this undocumented thing about ???
5893 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
5897 -- Here we have a non-scalar type
5900 Typ
:= Designated_Type
(Typ
);
5903 if not Is_Constrained
(Typ
) then
5904 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
5905 Analyze_And_Resolve
(N
, Restyp
);
5907 -- For the constrained array case, we have to check the subscripts
5908 -- for an exact match if the lengths are non-zero (the lengths
5909 -- must match in any case).
5911 elsif Is_Array_Type
(Typ
) then
5912 Check_Subscripts
: declare
5913 function Build_Attribute_Reference
5916 Dim
: Nat
) return Node_Id
;
5917 -- Build attribute reference E'Nam (Dim)
5919 -------------------------------
5920 -- Build_Attribute_Reference --
5921 -------------------------------
5923 function Build_Attribute_Reference
5926 Dim
: Nat
) return Node_Id
5930 Make_Attribute_Reference
(Loc
,
5932 Attribute_Name
=> Nam
,
5933 Expressions
=> New_List
(
5934 Make_Integer_Literal
(Loc
, Dim
)));
5935 end Build_Attribute_Reference
;
5937 -- Start of processing for Check_Subscripts
5940 for J
in 1 .. Number_Dimensions
(Typ
) loop
5941 Evolve_And_Then
(Cond
,
5944 Build_Attribute_Reference
5945 (Duplicate_Subexpr_No_Checks
(Obj
),
5948 Build_Attribute_Reference
5949 (New_Occurrence_Of
(Typ
, Loc
), Name_First
, J
)));
5951 Evolve_And_Then
(Cond
,
5954 Build_Attribute_Reference
5955 (Duplicate_Subexpr_No_Checks
(Obj
),
5958 Build_Attribute_Reference
5959 (New_Occurrence_Of
(Typ
, Loc
), Name_Last
, J
)));
5968 Right_Opnd
=> Make_Null
(Loc
)),
5969 Right_Opnd
=> Cond
);
5973 Analyze_And_Resolve
(N
, Restyp
);
5974 end Check_Subscripts
;
5976 -- These are the cases where constraint checks may be required,
5977 -- e.g. records with possible discriminants
5980 -- Expand the test into a series of discriminant comparisons.
5981 -- The expression that is built is the negation of the one that
5982 -- is used for checking discriminant constraints.
5984 Obj
:= Relocate_Node
(Left_Opnd
(N
));
5986 if Has_Discriminants
(Typ
) then
5987 Cond
:= Make_Op_Not
(Loc
,
5988 Right_Opnd
=> Build_Discriminant_Checks
(Obj
, Typ
));
5991 Cond
:= Make_Or_Else
(Loc
,
5995 Right_Opnd
=> Make_Null
(Loc
)),
5996 Right_Opnd
=> Cond
);
6000 Cond
:= New_Occurrence_Of
(Standard_True
, Loc
);
6004 Analyze_And_Resolve
(N
, Restyp
);
6007 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
6008 -- expression of an anonymous access type. This can involve an
6009 -- accessibility test and a tagged type membership test in the
6010 -- case of tagged designated types.
6012 if Ada_Version
>= Ada_2012
6014 and then Ekind
(Ltyp
) = E_Anonymous_Access_Type
6017 Expr_Entity
: Entity_Id
:= Empty
;
6019 Param_Level
: Node_Id
;
6020 Type_Level
: Node_Id
;
6023 if Is_Entity_Name
(Lop
) then
6024 Expr_Entity
:= Param_Entity
(Lop
);
6026 if not Present
(Expr_Entity
) then
6027 Expr_Entity
:= Entity
(Lop
);
6031 -- If a conversion of the anonymous access value to the
6032 -- tested type would be illegal, then the result is False.
6034 if not Valid_Conversion
6035 (Lop
, Rtyp
, Lop
, Report_Errs
=> False)
6037 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
6038 Analyze_And_Resolve
(N
, Restyp
);
6040 -- Apply an accessibility check if the access object has an
6041 -- associated access level and when the level of the type is
6042 -- less deep than the level of the access parameter. This
6043 -- only occur for access parameters and stand-alone objects
6044 -- of an anonymous access type.
6047 if Present
(Expr_Entity
)
6050 (Effective_Extra_Accessibility
(Expr_Entity
))
6051 and then UI_Gt
(Object_Access_Level
(Lop
),
6052 Type_Access_Level
(Rtyp
))
6056 (Effective_Extra_Accessibility
(Expr_Entity
), Loc
);
6059 Make_Integer_Literal
(Loc
, Type_Access_Level
(Rtyp
));
6061 -- Return True only if the accessibility level of the
6062 -- expression entity is not deeper than the level of
6063 -- the tested access type.
6067 Left_Opnd
=> Relocate_Node
(N
),
6068 Right_Opnd
=> Make_Op_Le
(Loc
,
6069 Left_Opnd
=> Param_Level
,
6070 Right_Opnd
=> Type_Level
)));
6072 Analyze_And_Resolve
(N
);
6075 -- If the designated type is tagged, do tagged membership
6078 -- *** NOTE: we have to check not null before doing the
6079 -- tagged membership test (but maybe that can be done
6080 -- inside Tagged_Membership?).
6082 if Is_Tagged_Type
(Typ
) then
6085 Left_Opnd
=> Relocate_Node
(N
),
6089 Right_Opnd
=> Make_Null
(Loc
))));
6091 -- No expansion will be performed for VM targets, as
6092 -- the VM back-ends will handle the membership tests
6095 if Tagged_Type_Expansion
then
6097 -- Note that we have to pass Original_Node, because
6098 -- the membership test might already have been
6099 -- rewritten by earlier parts of membership test.
6102 (Original_Node
(N
), SCIL_Node
, New_N
);
6104 -- Update decoration of relocated node referenced
6105 -- by the SCIL node.
6107 if Generate_SCIL
and then Present
(SCIL_Node
) then
6108 Set_SCIL_Node
(New_N
, SCIL_Node
);
6113 Left_Opnd
=> Relocate_Node
(N
),
6114 Right_Opnd
=> New_N
));
6116 Analyze_And_Resolve
(N
, Restyp
);
6125 -- At this point, we have done the processing required for the basic
6126 -- membership test, but not yet dealt with the predicate.
6130 -- If a predicate is present, then we do the predicate test, but we
6131 -- most certainly want to omit this if we are within the predicate
6132 -- function itself, since otherwise we have an infinite recursion.
6133 -- The check should also not be emitted when testing against a range
6134 -- (the check is only done when the right operand is a subtype; see
6135 -- RM12-4.5.2 (28.1/3-30/3)).
6137 Predicate_Check
: declare
6138 function In_Range_Check
return Boolean;
6139 -- Within an expanded range check that may raise Constraint_Error do
6140 -- not generate a predicate check as well. It is redundant because
6141 -- the context will add an explicit predicate check, and it will
6142 -- raise the wrong exception if it fails.
6144 --------------------
6145 -- In_Range_Check --
6146 --------------------
6148 function In_Range_Check
return Boolean is
6152 while Present
(P
) loop
6153 if Nkind
(P
) = N_Raise_Constraint_Error
then
6156 elsif Nkind
(P
) in N_Statement_Other_Than_Procedure_Call
6157 or else Nkind
(P
) = N_Procedure_Call_Statement
6158 or else Nkind
(P
) in N_Declaration
6171 PFunc
: constant Entity_Id
:= Predicate_Function
(Rtyp
);
6174 -- Start of processing for Predicate_Check
6178 and then Current_Scope
/= PFunc
6179 and then Nkind
(Rop
) /= N_Range
6181 if not In_Range_Check
then
6182 R_Op
:= Make_Predicate_Call
(Rtyp
, Lop
, Mem
=> True);
6184 R_Op
:= New_Occurrence_Of
(Standard_True
, Loc
);
6189 Left_Opnd
=> Relocate_Node
(N
),
6190 Right_Opnd
=> R_Op
));
6192 -- Analyze new expression, mark left operand as analyzed to
6193 -- avoid infinite recursion adding predicate calls. Similarly,
6194 -- suppress further range checks on the call.
6196 Set_Analyzed
(Left_Opnd
(N
));
6197 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
6199 -- All done, skip attempt at compile time determination of result
6203 end Predicate_Check
;
6206 --------------------------------
6207 -- Expand_N_Indexed_Component --
6208 --------------------------------
6210 procedure Expand_N_Indexed_Component
(N
: Node_Id
) is
6211 Loc
: constant Source_Ptr
:= Sloc
(N
);
6212 Typ
: constant Entity_Id
:= Etype
(N
);
6213 P
: constant Node_Id
:= Prefix
(N
);
6214 T
: constant Entity_Id
:= Etype
(P
);
6218 -- A special optimization, if we have an indexed component that is
6219 -- selecting from a slice, then we can eliminate the slice, since, for
6220 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6221 -- the range check required by the slice. The range check for the slice
6222 -- itself has already been generated. The range check for the
6223 -- subscripting operation is ensured by converting the subject to
6224 -- the subtype of the slice.
6226 -- This optimization not only generates better code, avoiding slice
6227 -- messing especially in the packed case, but more importantly bypasses
6228 -- some problems in handling this peculiar case, for example, the issue
6229 -- of dealing specially with object renamings.
6231 if Nkind
(P
) = N_Slice
6233 -- This optimization is disabled for CodePeer because it can transform
6234 -- an index-check constraint_error into a range-check constraint_error
6235 -- and CodePeer cares about that distinction.
6237 and then not CodePeer_Mode
6240 Make_Indexed_Component
(Loc
,
6241 Prefix
=> Prefix
(P
),
6242 Expressions
=> New_List
(
6244 (Etype
(First_Index
(Etype
(P
))),
6245 First
(Expressions
(N
))))));
6246 Analyze_And_Resolve
(N
, Typ
);
6250 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6251 -- function, then additional actuals must be passed.
6253 if Ada_Version
>= Ada_2005
6254 and then Is_Build_In_Place_Function_Call
(P
)
6256 Make_Build_In_Place_Call_In_Anonymous_Context
(P
);
6259 -- If the prefix is an access type, then we unconditionally rewrite if
6260 -- as an explicit dereference. This simplifies processing for several
6261 -- cases, including packed array cases and certain cases in which checks
6262 -- must be generated. We used to try to do this only when it was
6263 -- necessary, but it cleans up the code to do it all the time.
6265 if Is_Access_Type
(T
) then
6266 Insert_Explicit_Dereference
(P
);
6267 Analyze_And_Resolve
(P
, Designated_Type
(T
));
6268 Atp
:= Designated_Type
(T
);
6273 -- Generate index and validity checks
6275 Generate_Index_Checks
(N
);
6277 if Validity_Checks_On
and then Validity_Check_Subscripts
then
6278 Apply_Subscript_Validity_Checks
(N
);
6281 -- If selecting from an array with atomic components, and atomic sync
6282 -- is not suppressed for this array type, set atomic sync flag.
6284 if (Has_Atomic_Components
(Atp
)
6285 and then not Atomic_Synchronization_Disabled
(Atp
))
6286 or else (Is_Atomic
(Typ
)
6287 and then not Atomic_Synchronization_Disabled
(Typ
))
6288 or else (Is_Entity_Name
(P
)
6289 and then Has_Atomic_Components
(Entity
(P
))
6290 and then not Atomic_Synchronization_Disabled
(Entity
(P
)))
6292 Activate_Atomic_Synchronization
(N
);
6295 -- All done if the prefix is not a packed array implemented specially
6297 if not (Is_Packed
(Etype
(Prefix
(N
)))
6298 and then Present
(Packed_Array_Impl_Type
(Etype
(Prefix
(N
)))))
6303 -- For packed arrays that are not bit-packed (i.e. the case of an array
6304 -- with one or more index types with a non-contiguous enumeration type),
6305 -- we can always use the normal packed element get circuit.
6307 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
6308 Expand_Packed_Element_Reference
(N
);
6312 -- For a reference to a component of a bit packed array, we convert it
6313 -- to a reference to the corresponding Packed_Array_Impl_Type. We only
6314 -- want to do this for simple references, and not for:
6316 -- Left side of assignment, or prefix of left side of assignment, or
6317 -- prefix of the prefix, to handle packed arrays of packed arrays,
6318 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6320 -- Renaming objects in renaming associations
6321 -- This case is handled when a use of the renamed variable occurs
6323 -- Actual parameters for a procedure call
6324 -- This case is handled in Exp_Ch6.Expand_Actuals
6326 -- The second expression in a 'Read attribute reference
6328 -- The prefix of an address or bit or size attribute reference
6330 -- The following circuit detects these exceptions. Note that we need to
6331 -- deal with implicit dereferences when climbing up the parent chain,
6332 -- with the additional difficulty that the type of parents may have yet
6333 -- to be resolved since prefixes are usually resolved first.
6336 Child
: Node_Id
:= N
;
6337 Parnt
: Node_Id
:= Parent
(N
);
6341 if Nkind
(Parnt
) = N_Unchecked_Expression
then
6344 elsif Nkind_In
(Parnt
, N_Object_Renaming_Declaration
,
6345 N_Procedure_Call_Statement
)
6346 or else (Nkind
(Parnt
) = N_Parameter_Association
6348 Nkind
(Parent
(Parnt
)) = N_Procedure_Call_Statement
)
6352 elsif Nkind
(Parnt
) = N_Attribute_Reference
6353 and then Nam_In
(Attribute_Name
(Parnt
), Name_Address
,
6356 and then Prefix
(Parnt
) = Child
6360 elsif Nkind
(Parnt
) = N_Assignment_Statement
6361 and then Name
(Parnt
) = Child
6365 -- If the expression is an index of an indexed component, it must
6366 -- be expanded regardless of context.
6368 elsif Nkind
(Parnt
) = N_Indexed_Component
6369 and then Child
/= Prefix
(Parnt
)
6371 Expand_Packed_Element_Reference
(N
);
6374 elsif Nkind
(Parent
(Parnt
)) = N_Assignment_Statement
6375 and then Name
(Parent
(Parnt
)) = Parnt
6379 elsif Nkind
(Parnt
) = N_Attribute_Reference
6380 and then Attribute_Name
(Parnt
) = Name_Read
6381 and then Next
(First
(Expressions
(Parnt
))) = Child
6385 elsif Nkind
(Parnt
) = N_Indexed_Component
6386 and then Prefix
(Parnt
) = Child
6390 elsif Nkind
(Parnt
) = N_Selected_Component
6391 and then Prefix
(Parnt
) = Child
6392 and then not (Present
(Etype
(Selector_Name
(Parnt
)))
6394 Is_Access_Type
(Etype
(Selector_Name
(Parnt
))))
6398 -- If the parent is a dereference, either implicit or explicit,
6399 -- then the packed reference needs to be expanded.
6402 Expand_Packed_Element_Reference
(N
);
6406 -- Keep looking up tree for unchecked expression, or if we are the
6407 -- prefix of a possible assignment left side.
6410 Parnt
:= Parent
(Child
);
6413 end Expand_N_Indexed_Component
;
6415 ---------------------
6416 -- Expand_N_Not_In --
6417 ---------------------
6419 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6420 -- can be done. This avoids needing to duplicate this expansion code.
6422 procedure Expand_N_Not_In
(N
: Node_Id
) is
6423 Loc
: constant Source_Ptr
:= Sloc
(N
);
6424 Typ
: constant Entity_Id
:= Etype
(N
);
6425 Cfs
: constant Boolean := Comes_From_Source
(N
);
6432 Left_Opnd
=> Left_Opnd
(N
),
6433 Right_Opnd
=> Right_Opnd
(N
))));
6435 -- If this is a set membership, preserve list of alternatives
6437 Set_Alternatives
(Right_Opnd
(N
), Alternatives
(Original_Node
(N
)));
6439 -- We want this to appear as coming from source if original does (see
6440 -- transformations in Expand_N_In).
6442 Set_Comes_From_Source
(N
, Cfs
);
6443 Set_Comes_From_Source
(Right_Opnd
(N
), Cfs
);
6445 -- Now analyze transformed node
6447 Analyze_And_Resolve
(N
, Typ
);
6448 end Expand_N_Not_In
;
6454 -- The only replacement required is for the case of a null of a type that
6455 -- is an access to protected subprogram, or a subtype thereof. We represent
6456 -- such access values as a record, and so we must replace the occurrence of
6457 -- null by the equivalent record (with a null address and a null pointer in
6458 -- it), so that the backend creates the proper value.
6460 procedure Expand_N_Null
(N
: Node_Id
) is
6461 Loc
: constant Source_Ptr
:= Sloc
(N
);
6462 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
6466 if Is_Access_Protected_Subprogram_Type
(Typ
) then
6468 Make_Aggregate
(Loc
,
6469 Expressions
=> New_List
(
6470 New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
),
6474 Analyze_And_Resolve
(N
, Equivalent_Type
(Typ
));
6476 -- For subsequent semantic analysis, the node must retain its type.
6477 -- Gigi in any case replaces this type by the corresponding record
6478 -- type before processing the node.
6484 when RE_Not_Available
=>
6488 ---------------------
6489 -- Expand_N_Op_Abs --
6490 ---------------------
6492 procedure Expand_N_Op_Abs
(N
: Node_Id
) is
6493 Loc
: constant Source_Ptr
:= Sloc
(N
);
6494 Expr
: constant Node_Id
:= Right_Opnd
(N
);
6497 Unary_Op_Validity_Checks
(N
);
6499 -- Check for MINIMIZED/ELIMINATED overflow mode
6501 if Minimized_Eliminated_Overflow_Check
(N
) then
6502 Apply_Arithmetic_Overflow_Check
(N
);
6506 -- Deal with software overflow checking
6508 if not Backend_Overflow_Checks_On_Target
6509 and then Is_Signed_Integer_Type
(Etype
(N
))
6510 and then Do_Overflow_Check
(N
)
6512 -- The only case to worry about is when the argument is equal to the
6513 -- largest negative number, so what we do is to insert the check:
6515 -- [constraint_error when Expr = typ'Base'First]
6517 -- with the usual Duplicate_Subexpr use coding for expr
6520 Make_Raise_Constraint_Error
(Loc
,
6523 Left_Opnd
=> Duplicate_Subexpr
(Expr
),
6525 Make_Attribute_Reference
(Loc
,
6527 New_Occurrence_Of
(Base_Type
(Etype
(Expr
)), Loc
),
6528 Attribute_Name
=> Name_First
)),
6529 Reason
=> CE_Overflow_Check_Failed
));
6531 end Expand_N_Op_Abs
;
6533 ---------------------
6534 -- Expand_N_Op_Add --
6535 ---------------------
6537 procedure Expand_N_Op_Add
(N
: Node_Id
) is
6538 Typ
: constant Entity_Id
:= Etype
(N
);
6541 Binary_Op_Validity_Checks
(N
);
6543 -- Check for MINIMIZED/ELIMINATED overflow mode
6545 if Minimized_Eliminated_Overflow_Check
(N
) then
6546 Apply_Arithmetic_Overflow_Check
(N
);
6550 -- N + 0 = 0 + N = N for integer types
6552 if Is_Integer_Type
(Typ
) then
6553 if Compile_Time_Known_Value
(Right_Opnd
(N
))
6554 and then Expr_Value
(Right_Opnd
(N
)) = Uint_0
6556 Rewrite
(N
, Left_Opnd
(N
));
6559 elsif Compile_Time_Known_Value
(Left_Opnd
(N
))
6560 and then Expr_Value
(Left_Opnd
(N
)) = Uint_0
6562 Rewrite
(N
, Right_Opnd
(N
));
6567 -- Arithmetic overflow checks for signed integer/fixed point types
6569 if Is_Signed_Integer_Type
(Typ
) or else Is_Fixed_Point_Type
(Typ
) then
6570 Apply_Arithmetic_Overflow_Check
(N
);
6574 -- Overflow checks for floating-point if -gnateF mode active
6576 Check_Float_Op_Overflow
(N
);
6577 end Expand_N_Op_Add
;
6579 ---------------------
6580 -- Expand_N_Op_And --
6581 ---------------------
6583 procedure Expand_N_Op_And
(N
: Node_Id
) is
6584 Typ
: constant Entity_Id
:= Etype
(N
);
6587 Binary_Op_Validity_Checks
(N
);
6589 if Is_Array_Type
(Etype
(N
)) then
6590 Expand_Boolean_Operator
(N
);
6592 elsif Is_Boolean_Type
(Etype
(N
)) then
6593 Adjust_Condition
(Left_Opnd
(N
));
6594 Adjust_Condition
(Right_Opnd
(N
));
6595 Set_Etype
(N
, Standard_Boolean
);
6596 Adjust_Result_Type
(N
, Typ
);
6598 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
6599 Expand_Intrinsic_Call
(N
, Entity
(N
));
6602 end Expand_N_Op_And
;
6604 ------------------------
6605 -- Expand_N_Op_Concat --
6606 ------------------------
6608 procedure Expand_N_Op_Concat
(N
: Node_Id
) is
6610 -- List of operands to be concatenated
6613 -- Node which is to be replaced by the result of concatenating the nodes
6614 -- in the list Opnds.
6617 -- Ensure validity of both operands
6619 Binary_Op_Validity_Checks
(N
);
6621 -- If we are the left operand of a concatenation higher up the tree,
6622 -- then do nothing for now, since we want to deal with a series of
6623 -- concatenations as a unit.
6625 if Nkind
(Parent
(N
)) = N_Op_Concat
6626 and then N
= Left_Opnd
(Parent
(N
))
6631 -- We get here with a concatenation whose left operand may be a
6632 -- concatenation itself with a consistent type. We need to process
6633 -- these concatenation operands from left to right, which means
6634 -- from the deepest node in the tree to the highest node.
6637 while Nkind
(Left_Opnd
(Cnode
)) = N_Op_Concat
loop
6638 Cnode
:= Left_Opnd
(Cnode
);
6641 -- Now Cnode is the deepest concatenation, and its parents are the
6642 -- concatenation nodes above, so now we process bottom up, doing the
6645 -- The outer loop runs more than once if more than one concatenation
6646 -- type is involved.
6649 Opnds
:= New_List
(Left_Opnd
(Cnode
), Right_Opnd
(Cnode
));
6650 Set_Parent
(Opnds
, N
);
6652 -- The inner loop gathers concatenation operands
6654 Inner
: while Cnode
/= N
6655 and then Base_Type
(Etype
(Cnode
)) =
6656 Base_Type
(Etype
(Parent
(Cnode
)))
6658 Cnode
:= Parent
(Cnode
);
6659 Append
(Right_Opnd
(Cnode
), Opnds
);
6662 -- Note: The following code is a temporary workaround for N731-034
6663 -- and N829-028 and will be kept until the general issue of internal
6664 -- symbol serialization is addressed. The workaround is kept under a
6665 -- debug switch to avoid permiating into the general case.
6667 -- Wrap the node to concatenate into an expression actions node to
6668 -- keep it nicely packaged. This is useful in the case of an assert
6669 -- pragma with a concatenation where we want to be able to delete
6670 -- the concatenation and all its expansion stuff.
6672 if Debug_Flag_Dot_H
then
6674 Cnod
: constant Node_Id
:= Relocate_Node
(Cnode
);
6675 Typ
: constant Entity_Id
:= Base_Type
(Etype
(Cnode
));
6678 -- Note: use Rewrite rather than Replace here, so that for
6679 -- example Why_Not_Static can find the original concatenation
6683 Make_Expression_With_Actions
(Sloc
(Cnode
),
6684 Actions
=> New_List
(Make_Null_Statement
(Sloc
(Cnode
))),
6685 Expression
=> Cnod
));
6687 Expand_Concatenate
(Cnod
, Opnds
);
6688 Analyze_And_Resolve
(Cnode
, Typ
);
6694 Expand_Concatenate
(Cnode
, Opnds
);
6697 exit Outer
when Cnode
= N
;
6698 Cnode
:= Parent
(Cnode
);
6700 end Expand_N_Op_Concat
;
6702 ------------------------
6703 -- Expand_N_Op_Divide --
6704 ------------------------
6706 procedure Expand_N_Op_Divide
(N
: Node_Id
) is
6707 Loc
: constant Source_Ptr
:= Sloc
(N
);
6708 Lopnd
: constant Node_Id
:= Left_Opnd
(N
);
6709 Ropnd
: constant Node_Id
:= Right_Opnd
(N
);
6710 Ltyp
: constant Entity_Id
:= Etype
(Lopnd
);
6711 Rtyp
: constant Entity_Id
:= Etype
(Ropnd
);
6712 Typ
: Entity_Id
:= Etype
(N
);
6713 Rknow
: constant Boolean := Is_Integer_Type
(Typ
)
6715 Compile_Time_Known_Value
(Ropnd
);
6719 Binary_Op_Validity_Checks
(N
);
6721 -- Check for MINIMIZED/ELIMINATED overflow mode
6723 if Minimized_Eliminated_Overflow_Check
(N
) then
6724 Apply_Arithmetic_Overflow_Check
(N
);
6728 -- Otherwise proceed with expansion of division
6731 Rval
:= Expr_Value
(Ropnd
);
6734 -- N / 1 = N for integer types
6736 if Rknow
and then Rval
= Uint_1
then
6741 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
6742 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6743 -- operand is an unsigned integer, as required for this to work.
6745 if Nkind
(Ropnd
) = N_Op_Expon
6746 and then Is_Power_Of_2_For_Shift
(Ropnd
)
6748 -- We cannot do this transformation in configurable run time mode if we
6749 -- have 64-bit integers and long shifts are not available.
6751 and then (Esize
(Ltyp
) <= 32 or else Support_Long_Shifts_On_Target
)
6754 Make_Op_Shift_Right
(Loc
,
6757 Convert_To
(Standard_Natural
, Right_Opnd
(Ropnd
))));
6758 Analyze_And_Resolve
(N
, Typ
);
6762 -- Do required fixup of universal fixed operation
6764 if Typ
= Universal_Fixed
then
6765 Fixup_Universal_Fixed_Operation
(N
);
6769 -- Divisions with fixed-point results
6771 if Is_Fixed_Point_Type
(Typ
) then
6773 -- Deal with divide-by-zero check if back end cannot handle them
6774 -- and the flag is set indicating that we need such a check. Note
6775 -- that we don't need to bother here with the case of mixed-mode
6776 -- (Right operand an integer type), since these will be rewritten
6777 -- with conversions to a divide with a fixed-point right operand.
6779 if Do_Division_Check
(N
)
6780 and then not Backend_Divide_Checks_On_Target
6781 and then not Is_Integer_Type
(Rtyp
)
6783 Set_Do_Division_Check
(N
, False);
6785 Make_Raise_Constraint_Error
(Loc
,
6788 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Ropnd
),
6789 Right_Opnd
=> Make_Real_Literal
(Loc
, Ureal_0
)),
6790 Reason
=> CE_Divide_By_Zero
));
6793 -- No special processing if Treat_Fixed_As_Integer is set, since
6794 -- from a semantic point of view such operations are simply integer
6795 -- operations and will be treated that way.
6797 if not Treat_Fixed_As_Integer
(N
) then
6798 if Is_Integer_Type
(Rtyp
) then
6799 Expand_Divide_Fixed_By_Integer_Giving_Fixed
(N
);
6801 Expand_Divide_Fixed_By_Fixed_Giving_Fixed
(N
);
6805 -- Other cases of division of fixed-point operands. Again we exclude the
6806 -- case where Treat_Fixed_As_Integer is set.
6808 elsif (Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
))
6809 and then not Treat_Fixed_As_Integer
(N
)
6811 if Is_Integer_Type
(Typ
) then
6812 Expand_Divide_Fixed_By_Fixed_Giving_Integer
(N
);
6814 pragma Assert
(Is_Floating_Point_Type
(Typ
));
6815 Expand_Divide_Fixed_By_Fixed_Giving_Float
(N
);
6818 -- Mixed-mode operations can appear in a non-static universal context,
6819 -- in which case the integer argument must be converted explicitly.
6821 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Rtyp
) then
6823 Convert_To
(Universal_Real
, Relocate_Node
(Ropnd
)));
6825 Analyze_And_Resolve
(Ropnd
, Universal_Real
);
6827 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Ltyp
) then
6829 Convert_To
(Universal_Real
, Relocate_Node
(Lopnd
)));
6831 Analyze_And_Resolve
(Lopnd
, Universal_Real
);
6833 -- Non-fixed point cases, do integer zero divide and overflow checks
6835 elsif Is_Integer_Type
(Typ
) then
6836 Apply_Divide_Checks
(N
);
6839 -- Overflow checks for floating-point if -gnateF mode active
6841 Check_Float_Op_Overflow
(N
);
6842 end Expand_N_Op_Divide
;
6844 --------------------
6845 -- Expand_N_Op_Eq --
6846 --------------------
6848 procedure Expand_N_Op_Eq
(N
: Node_Id
) is
6849 Loc
: constant Source_Ptr
:= Sloc
(N
);
6850 Typ
: constant Entity_Id
:= Etype
(N
);
6851 Lhs
: constant Node_Id
:= Left_Opnd
(N
);
6852 Rhs
: constant Node_Id
:= Right_Opnd
(N
);
6853 Bodies
: constant List_Id
:= New_List
;
6854 A_Typ
: constant Entity_Id
:= Etype
(Lhs
);
6856 Typl
: Entity_Id
:= A_Typ
;
6857 Op_Name
: Entity_Id
;
6860 procedure Build_Equality_Call
(Eq
: Entity_Id
);
6861 -- If a constructed equality exists for the type or for its parent,
6862 -- build and analyze call, adding conversions if the operation is
6865 function Has_Unconstrained_UU_Component
(Typ
: Node_Id
) return Boolean;
6866 -- Determines whether a type has a subcomponent of an unconstrained
6867 -- Unchecked_Union subtype. Typ is a record type.
6869 -------------------------
6870 -- Build_Equality_Call --
6871 -------------------------
6873 procedure Build_Equality_Call
(Eq
: Entity_Id
) is
6874 Op_Type
: constant Entity_Id
:= Etype
(First_Formal
(Eq
));
6875 L_Exp
: Node_Id
:= Relocate_Node
(Lhs
);
6876 R_Exp
: Node_Id
:= Relocate_Node
(Rhs
);
6879 -- Adjust operands if necessary to comparison type
6881 if Base_Type
(Op_Type
) /= Base_Type
(A_Typ
)
6882 and then not Is_Class_Wide_Type
(A_Typ
)
6884 L_Exp
:= OK_Convert_To
(Op_Type
, L_Exp
);
6885 R_Exp
:= OK_Convert_To
(Op_Type
, R_Exp
);
6888 -- If we have an Unchecked_Union, we need to add the inferred
6889 -- discriminant values as actuals in the function call. At this
6890 -- point, the expansion has determined that both operands have
6891 -- inferable discriminants.
6893 if Is_Unchecked_Union
(Op_Type
) then
6895 Lhs_Type
: constant Node_Id
:= Etype
(L_Exp
);
6896 Rhs_Type
: constant Node_Id
:= Etype
(R_Exp
);
6898 Lhs_Discr_Vals
: Elist_Id
;
6899 -- List of inferred discriminant values for left operand.
6901 Rhs_Discr_Vals
: Elist_Id
;
6902 -- List of inferred discriminant values for right operand.
6907 Lhs_Discr_Vals
:= New_Elmt_List
;
6908 Rhs_Discr_Vals
:= New_Elmt_List
;
6910 -- Per-object constrained selected components require special
6911 -- attention. If the enclosing scope of the component is an
6912 -- Unchecked_Union, we cannot reference its discriminants
6913 -- directly. This is why we use the extra parameters of the
6914 -- equality function of the enclosing Unchecked_Union.
6916 -- type UU_Type (Discr : Integer := 0) is
6919 -- pragma Unchecked_Union (UU_Type);
6921 -- 1. Unchecked_Union enclosing record:
6923 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
6925 -- Comp : UU_Type (Discr);
6927 -- end Enclosing_UU_Type;
6928 -- pragma Unchecked_Union (Enclosing_UU_Type);
6930 -- Obj1 : Enclosing_UU_Type;
6931 -- Obj2 : Enclosing_UU_Type (1);
6933 -- [. . .] Obj1 = Obj2 [. . .]
6937 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
6939 -- A and B are the formal parameters of the equality function
6940 -- of Enclosing_UU_Type. The function always has two extra
6941 -- formals to capture the inferred discriminant values for
6942 -- each discriminant of the type.
6944 -- 2. Non-Unchecked_Union enclosing record:
6947 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
6950 -- Comp : UU_Type (Discr);
6952 -- end Enclosing_Non_UU_Type;
6954 -- Obj1 : Enclosing_Non_UU_Type;
6955 -- Obj2 : Enclosing_Non_UU_Type (1);
6957 -- ... Obj1 = Obj2 ...
6961 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
6962 -- obj1.discr, obj2.discr)) then
6964 -- In this case we can directly reference the discriminants of
6965 -- the enclosing record.
6967 -- Process left operand of equality
6969 if Nkind
(Lhs
) = N_Selected_Component
6971 Has_Per_Object_Constraint
(Entity
(Selector_Name
(Lhs
)))
6973 -- If enclosing record is an Unchecked_Union, use formals
6974 -- corresponding to each discriminant. The name of the
6975 -- formal is that of the discriminant, with added suffix,
6976 -- see Exp_Ch3.Build_Record_Equality for details.
6978 if Is_Unchecked_Union
(Scope
(Entity
(Selector_Name
(Lhs
))))
6982 (Scope
(Entity
(Selector_Name
(Lhs
))));
6983 while Present
(Discr
) loop
6985 (Make_Identifier
(Loc
,
6986 Chars
=> New_External_Name
(Chars
(Discr
), 'A')),
6987 To
=> Lhs_Discr_Vals
);
6988 Next_Discriminant
(Discr
);
6991 -- If enclosing record is of a non-Unchecked_Union type, it
6992 -- is possible to reference its discriminants directly.
6995 Discr
:= First_Discriminant
(Lhs_Type
);
6996 while Present
(Discr
) loop
6998 (Make_Selected_Component
(Loc
,
6999 Prefix
=> Prefix
(Lhs
),
7002 (Get_Discriminant_Value
(Discr
,
7004 Stored_Constraint
(Lhs_Type
)))),
7005 To
=> Lhs_Discr_Vals
);
7006 Next_Discriminant
(Discr
);
7010 -- Otherwise operand is on object with a constrained type.
7011 -- Infer the discriminant values from the constraint.
7015 Discr
:= First_Discriminant
(Lhs_Type
);
7016 while Present
(Discr
) loop
7019 (Get_Discriminant_Value
(Discr
,
7021 Stored_Constraint
(Lhs_Type
))),
7022 To
=> Lhs_Discr_Vals
);
7023 Next_Discriminant
(Discr
);
7027 -- Similar processing for right operand of equality
7029 if Nkind
(Rhs
) = N_Selected_Component
7031 Has_Per_Object_Constraint
(Entity
(Selector_Name
(Rhs
)))
7033 if Is_Unchecked_Union
7034 (Scope
(Entity
(Selector_Name
(Rhs
))))
7038 (Scope
(Entity
(Selector_Name
(Rhs
))));
7039 while Present
(Discr
) loop
7041 (Make_Identifier
(Loc
,
7042 Chars
=> New_External_Name
(Chars
(Discr
), 'B')),
7043 To
=> Rhs_Discr_Vals
);
7044 Next_Discriminant
(Discr
);
7048 Discr
:= First_Discriminant
(Rhs_Type
);
7049 while Present
(Discr
) loop
7051 (Make_Selected_Component
(Loc
,
7052 Prefix
=> Prefix
(Rhs
),
7054 New_Copy
(Get_Discriminant_Value
7057 Stored_Constraint
(Rhs_Type
)))),
7058 To
=> Rhs_Discr_Vals
);
7059 Next_Discriminant
(Discr
);
7064 Discr
:= First_Discriminant
(Rhs_Type
);
7065 while Present
(Discr
) loop
7067 (New_Copy
(Get_Discriminant_Value
7070 Stored_Constraint
(Rhs_Type
))),
7071 To
=> Rhs_Discr_Vals
);
7072 Next_Discriminant
(Discr
);
7076 -- Now merge the list of discriminant values so that values
7077 -- of corresponding discriminants are adjacent.
7085 Params
:= New_List
(L_Exp
, R_Exp
);
7086 L_Elmt
:= First_Elmt
(Lhs_Discr_Vals
);
7087 R_Elmt
:= First_Elmt
(Rhs_Discr_Vals
);
7088 while Present
(L_Elmt
) loop
7089 Append_To
(Params
, Node
(L_Elmt
));
7090 Append_To
(Params
, Node
(R_Elmt
));
7096 Make_Function_Call
(Loc
,
7097 Name
=> New_Occurrence_Of
(Eq
, Loc
),
7098 Parameter_Associations
=> Params
));
7102 -- Normal case, not an unchecked union
7106 Make_Function_Call
(Loc
,
7107 Name
=> New_Occurrence_Of
(Eq
, Loc
),
7108 Parameter_Associations
=> New_List
(L_Exp
, R_Exp
)));
7111 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7112 end Build_Equality_Call
;
7114 ------------------------------------
7115 -- Has_Unconstrained_UU_Component --
7116 ------------------------------------
7118 function Has_Unconstrained_UU_Component
7119 (Typ
: Node_Id
) return Boolean
7121 Tdef
: constant Node_Id
:=
7122 Type_Definition
(Declaration_Node
(Base_Type
(Typ
)));
7126 function Component_Is_Unconstrained_UU
7127 (Comp
: Node_Id
) return Boolean;
7128 -- Determines whether the subtype of the component is an
7129 -- unconstrained Unchecked_Union.
7131 function Variant_Is_Unconstrained_UU
7132 (Variant
: Node_Id
) return Boolean;
7133 -- Determines whether a component of the variant has an unconstrained
7134 -- Unchecked_Union subtype.
7136 -----------------------------------
7137 -- Component_Is_Unconstrained_UU --
7138 -----------------------------------
7140 function Component_Is_Unconstrained_UU
7141 (Comp
: Node_Id
) return Boolean
7144 if Nkind
(Comp
) /= N_Component_Declaration
then
7149 Sindic
: constant Node_Id
:=
7150 Subtype_Indication
(Component_Definition
(Comp
));
7153 -- Unconstrained nominal type. In the case of a constraint
7154 -- present, the node kind would have been N_Subtype_Indication.
7156 if Nkind
(Sindic
) = N_Identifier
then
7157 return Is_Unchecked_Union
(Base_Type
(Etype
(Sindic
)));
7162 end Component_Is_Unconstrained_UU
;
7164 ---------------------------------
7165 -- Variant_Is_Unconstrained_UU --
7166 ---------------------------------
7168 function Variant_Is_Unconstrained_UU
7169 (Variant
: Node_Id
) return Boolean
7171 Clist
: constant Node_Id
:= Component_List
(Variant
);
7174 if Is_Empty_List
(Component_Items
(Clist
)) then
7178 -- We only need to test one component
7181 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
7184 while Present
(Comp
) loop
7185 if Component_Is_Unconstrained_UU
(Comp
) then
7193 -- None of the components withing the variant were of
7194 -- unconstrained Unchecked_Union type.
7197 end Variant_Is_Unconstrained_UU
;
7199 -- Start of processing for Has_Unconstrained_UU_Component
7202 if Null_Present
(Tdef
) then
7206 Clist
:= Component_List
(Tdef
);
7207 Vpart
:= Variant_Part
(Clist
);
7209 -- Inspect available components
7211 if Present
(Component_Items
(Clist
)) then
7213 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
7216 while Present
(Comp
) loop
7218 -- One component is sufficient
7220 if Component_Is_Unconstrained_UU
(Comp
) then
7229 -- Inspect available components withing variants
7231 if Present
(Vpart
) then
7233 Variant
: Node_Id
:= First
(Variants
(Vpart
));
7236 while Present
(Variant
) loop
7238 -- One component within a variant is sufficient
7240 if Variant_Is_Unconstrained_UU
(Variant
) then
7249 -- Neither the available components, nor the components inside the
7250 -- variant parts were of an unconstrained Unchecked_Union subtype.
7253 end Has_Unconstrained_UU_Component
;
7255 -- Start of processing for Expand_N_Op_Eq
7258 Binary_Op_Validity_Checks
(N
);
7260 -- Deal with private types
7262 if Ekind
(Typl
) = E_Private_Type
then
7263 Typl
:= Underlying_Type
(Typl
);
7264 elsif Ekind
(Typl
) = E_Private_Subtype
then
7265 Typl
:= Underlying_Type
(Base_Type
(Typl
));
7270 -- It may happen in error situations that the underlying type is not
7271 -- set. The error will be detected later, here we just defend the
7278 -- Now get the implementation base type (note that plain Base_Type here
7279 -- might lead us back to the private type, which is not what we want!)
7281 Typl
:= Implementation_Base_Type
(Typl
);
7283 -- Equality between variant records results in a call to a routine
7284 -- that has conditional tests of the discriminant value(s), and hence
7285 -- violates the No_Implicit_Conditionals restriction.
7287 if Has_Variant_Part
(Typl
) then
7292 Check_Restriction
(Msg
, No_Implicit_Conditionals
, N
);
7296 ("\comparison of variant records tests discriminants", N
);
7302 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7303 -- means we no longer have a comparison operation, we are all done.
7305 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
7307 if Nkind
(N
) /= N_Op_Eq
then
7311 -- Boolean types (requiring handling of non-standard case)
7313 if Is_Boolean_Type
(Typl
) then
7314 Adjust_Condition
(Left_Opnd
(N
));
7315 Adjust_Condition
(Right_Opnd
(N
));
7316 Set_Etype
(N
, Standard_Boolean
);
7317 Adjust_Result_Type
(N
, Typ
);
7321 elsif Is_Array_Type
(Typl
) then
7323 -- If we are doing full validity checking, and it is possible for the
7324 -- array elements to be invalid then expand out array comparisons to
7325 -- make sure that we check the array elements.
7327 if Validity_Check_Operands
7328 and then not Is_Known_Valid
(Component_Type
(Typl
))
7331 Save_Force_Validity_Checks
: constant Boolean :=
7332 Force_Validity_Checks
;
7334 Force_Validity_Checks
:= True;
7336 Expand_Array_Equality
7338 Relocate_Node
(Lhs
),
7339 Relocate_Node
(Rhs
),
7342 Insert_Actions
(N
, Bodies
);
7343 Analyze_And_Resolve
(N
, Standard_Boolean
);
7344 Force_Validity_Checks
:= Save_Force_Validity_Checks
;
7347 -- Packed case where both operands are known aligned
7349 elsif Is_Bit_Packed_Array
(Typl
)
7350 and then not Is_Possibly_Unaligned_Object
(Lhs
)
7351 and then not Is_Possibly_Unaligned_Object
(Rhs
)
7353 Expand_Packed_Eq
(N
);
7355 -- Where the component type is elementary we can use a block bit
7356 -- comparison (if supported on the target) exception in the case
7357 -- of floating-point (negative zero issues require element by
7358 -- element comparison), and atomic/VFA types (where we must be sure
7359 -- to load elements independently) and possibly unaligned arrays.
7361 elsif Is_Elementary_Type
(Component_Type
(Typl
))
7362 and then not Is_Floating_Point_Type
(Component_Type
(Typl
))
7363 and then not Is_Atomic_Or_VFA
(Component_Type
(Typl
))
7364 and then not Is_Possibly_Unaligned_Object
(Lhs
)
7365 and then not Is_Possibly_Unaligned_Object
(Rhs
)
7366 and then Support_Composite_Compare_On_Target
7370 -- For composite and floating-point cases, expand equality loop to
7371 -- make sure of using proper comparisons for tagged types, and
7372 -- correctly handling the floating-point case.
7376 Expand_Array_Equality
7378 Relocate_Node
(Lhs
),
7379 Relocate_Node
(Rhs
),
7382 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
7383 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7388 elsif Is_Record_Type
(Typl
) then
7390 -- For tagged types, use the primitive "="
7392 if Is_Tagged_Type
(Typl
) then
7394 -- No need to do anything else compiling under restriction
7395 -- No_Dispatching_Calls. During the semantic analysis we
7396 -- already notified such violation.
7398 if Restriction_Active
(No_Dispatching_Calls
) then
7402 -- If this is derived from an untagged private type completed with
7403 -- a tagged type, it does not have a full view, so we use the
7404 -- primitive operations of the private type. This check should no
7405 -- longer be necessary when these types get their full views???
7407 if Is_Private_Type
(A_Typ
)
7408 and then not Is_Tagged_Type
(A_Typ
)
7409 and then Is_Derived_Type
(A_Typ
)
7410 and then No
(Full_View
(A_Typ
))
7412 -- Search for equality operation, checking that the operands
7413 -- have the same type. Note that we must find a matching entry,
7414 -- or something is very wrong.
7416 Prim
:= First_Elmt
(Collect_Primitive_Operations
(A_Typ
));
7418 while Present
(Prim
) loop
7419 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7420 and then Etype
(First_Formal
(Node
(Prim
))) =
7421 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7423 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7428 pragma Assert
(Present
(Prim
));
7429 Op_Name
:= Node
(Prim
);
7431 -- Find the type's predefined equality or an overriding
7432 -- user-defined equality. The reason for not simply calling
7433 -- Find_Prim_Op here is that there may be a user-defined
7434 -- overloaded equality op that precedes the equality that we
7435 -- want, so we have to explicitly search (e.g., there could be
7436 -- an equality with two different parameter types).
7439 if Is_Class_Wide_Type
(Typl
) then
7440 Typl
:= Find_Specific_Type
(Typl
);
7443 Prim
:= First_Elmt
(Primitive_Operations
(Typl
));
7444 while Present
(Prim
) loop
7445 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7446 and then Etype
(First_Formal
(Node
(Prim
))) =
7447 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7449 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7454 pragma Assert
(Present
(Prim
));
7455 Op_Name
:= Node
(Prim
);
7458 Build_Equality_Call
(Op_Name
);
7460 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7461 -- predefined equality operator for a type which has a subcomponent
7462 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7464 elsif Has_Unconstrained_UU_Component
(Typl
) then
7466 Make_Raise_Program_Error
(Loc
,
7467 Reason
=> PE_Unchecked_Union_Restriction
));
7469 -- Prevent Gigi from generating incorrect code by rewriting the
7470 -- equality as a standard False. (is this documented somewhere???)
7473 New_Occurrence_Of
(Standard_False
, Loc
));
7475 elsif Is_Unchecked_Union
(Typl
) then
7477 -- If we can infer the discriminants of the operands, we make a
7478 -- call to the TSS equality function.
7480 if Has_Inferable_Discriminants
(Lhs
)
7482 Has_Inferable_Discriminants
(Rhs
)
7485 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
7488 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7489 -- the predefined equality operator for an Unchecked_Union type
7490 -- if either of the operands lack inferable discriminants.
7493 Make_Raise_Program_Error
(Loc
,
7494 Reason
=> PE_Unchecked_Union_Restriction
));
7496 -- Emit a warning on source equalities only, otherwise the
7497 -- message may appear out of place due to internal use. The
7498 -- warning is unconditional because it is required by the
7501 if Comes_From_Source
(N
) then
7503 ("Unchecked_Union discriminants cannot be determined??",
7506 ("\Program_Error will be raised for equality operation??",
7510 -- Prevent Gigi from generating incorrect code by rewriting
7511 -- the equality as a standard False (documented where???).
7514 New_Occurrence_Of
(Standard_False
, Loc
));
7517 -- If a type support function is present (for complex cases), use it
7519 elsif Present
(TSS
(Root_Type
(Typl
), TSS_Composite_Equality
)) then
7521 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
7523 -- When comparing two Bounded_Strings, use the primitive equality of
7524 -- the root Super_String type.
7526 elsif Is_Bounded_String
(Typl
) then
7528 First_Elmt
(Collect_Primitive_Operations
(Root_Type
(Typl
)));
7530 while Present
(Prim
) loop
7531 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
7532 and then Etype
(First_Formal
(Node
(Prim
))) =
7533 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
7534 and then Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
7539 -- A Super_String type should always have a primitive equality
7541 pragma Assert
(Present
(Prim
));
7542 Build_Equality_Call
(Node
(Prim
));
7544 -- Otherwise expand the component by component equality. Note that
7545 -- we never use block-bit comparisons for records, because of the
7546 -- problems with gaps. The backend will often be able to recombine
7547 -- the separate comparisons that we generate here.
7550 Remove_Side_Effects
(Lhs
);
7551 Remove_Side_Effects
(Rhs
);
7553 Expand_Record_Equality
(N
, Typl
, Lhs
, Rhs
, Bodies
));
7555 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
7556 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
7560 -- Test if result is known at compile time
7562 Rewrite_Comparison
(N
);
7564 -- Special optimization of length comparison
7566 Optimize_Length_Comparison
(N
);
7568 -- One more special case: if we have a comparison of X'Result = expr
7569 -- in floating-point, then if not already there, change expr to be
7570 -- f'Machine (expr) to eliminate surprise from extra precision.
7572 if Is_Floating_Point_Type
(Typl
)
7573 and then Nkind
(Original_Node
(Lhs
)) = N_Attribute_Reference
7574 and then Attribute_Name
(Original_Node
(Lhs
)) = Name_Result
7576 -- Stick in the Typ'Machine call if not already there
7578 if Nkind
(Rhs
) /= N_Attribute_Reference
7579 or else Attribute_Name
(Rhs
) /= Name_Machine
7582 Make_Attribute_Reference
(Loc
,
7583 Prefix
=> New_Occurrence_Of
(Typl
, Loc
),
7584 Attribute_Name
=> Name_Machine
,
7585 Expressions
=> New_List
(Relocate_Node
(Rhs
))));
7586 Analyze_And_Resolve
(Rhs
, Typl
);
7591 -----------------------
7592 -- Expand_N_Op_Expon --
7593 -----------------------
7595 procedure Expand_N_Op_Expon
(N
: Node_Id
) is
7596 Loc
: constant Source_Ptr
:= Sloc
(N
);
7597 Typ
: constant Entity_Id
:= Etype
(N
);
7598 Rtyp
: constant Entity_Id
:= Root_Type
(Typ
);
7599 Base
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
7600 Bastyp
: constant Node_Id
:= Etype
(Base
);
7601 Exp
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
7602 Exptyp
: constant Entity_Id
:= Etype
(Exp
);
7603 Ovflo
: constant Boolean := Do_Overflow_Check
(N
);
7611 function Wrap_MA
(Exp
: Node_Id
) return Node_Id
;
7612 -- Given an expression Exp, if the root type is Float or Long_Float,
7613 -- then wrap the expression in a call of Bastyp'Machine, to stop any
7614 -- extra precision. This is done to ensure that X**A = X**B when A is
7615 -- a static constant and B is a variable with the same value. For any
7616 -- other type, the node Exp is returned unchanged.
7622 function Wrap_MA
(Exp
: Node_Id
) return Node_Id
is
7623 Loc
: constant Source_Ptr
:= Sloc
(Exp
);
7625 if Rtyp
= Standard_Float
or else Rtyp
= Standard_Long_Float
then
7627 Make_Attribute_Reference
(Loc
,
7628 Attribute_Name
=> Name_Machine
,
7629 Prefix
=> New_Occurrence_Of
(Bastyp
, Loc
),
7630 Expressions
=> New_List
(Relocate_Node
(Exp
)));
7636 -- Start of processing for Expand_N_Op
7639 Binary_Op_Validity_Checks
(N
);
7641 -- CodePeer wants to see the unexpanded N_Op_Expon node
7643 if CodePeer_Mode
then
7647 -- If either operand is of a private type, then we have the use of an
7648 -- intrinsic operator, and we get rid of the privateness, by using root
7649 -- types of underlying types for the actual operation. Otherwise the
7650 -- private types will cause trouble if we expand multiplications or
7651 -- shifts etc. We also do this transformation if the result type is
7652 -- different from the base type.
7654 if Is_Private_Type
(Etype
(Base
))
7655 or else Is_Private_Type
(Typ
)
7656 or else Is_Private_Type
(Exptyp
)
7657 or else Rtyp
/= Root_Type
(Bastyp
)
7660 Bt
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Bastyp
));
7661 Et
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Exptyp
));
7664 Unchecked_Convert_To
(Typ
,
7666 Left_Opnd
=> Unchecked_Convert_To
(Bt
, Base
),
7667 Right_Opnd
=> Unchecked_Convert_To
(Et
, Exp
))));
7668 Analyze_And_Resolve
(N
, Typ
);
7673 -- Check for MINIMIZED/ELIMINATED overflow mode
7675 if Minimized_Eliminated_Overflow_Check
(N
) then
7676 Apply_Arithmetic_Overflow_Check
(N
);
7680 -- Test for case of known right argument where we can replace the
7681 -- exponentiation by an equivalent expression using multiplication.
7683 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
7684 -- configurable run-time mode, we may not have the exponentiation
7685 -- routine available, and we don't want the legality of the program
7686 -- to depend on how clever the compiler is in knowing values.
7688 if CRT_Safe_Compile_Time_Known_Value
(Exp
) then
7689 Expv
:= Expr_Value
(Exp
);
7691 -- We only fold small non-negative exponents. You might think we
7692 -- could fold small negative exponents for the real case, but we
7693 -- can't because we are required to raise Constraint_Error for
7694 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
7695 -- See ACVC test C4A012B, and it is not worth generating the test.
7697 -- For small negative exponents, we return the reciprocal of
7698 -- the folding of the exponentiation for the opposite (positive)
7699 -- exponent, as required by Ada RM 4.5.6(11/3).
7701 if abs Expv
<= 4 then
7703 -- X ** 0 = 1 (or 1.0)
7707 -- Call Remove_Side_Effects to ensure that any side effects
7708 -- in the ignored left operand (in particular function calls
7709 -- to user defined functions) are properly executed.
7711 Remove_Side_Effects
(Base
);
7713 if Ekind
(Typ
) in Integer_Kind
then
7714 Xnode
:= Make_Integer_Literal
(Loc
, Intval
=> 1);
7716 Xnode
:= Make_Real_Literal
(Loc
, Ureal_1
);
7729 Make_Op_Multiply
(Loc
,
7730 Left_Opnd
=> Duplicate_Subexpr
(Base
),
7731 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)));
7733 -- X ** 3 = X * X * X
7738 Make_Op_Multiply
(Loc
,
7740 Make_Op_Multiply
(Loc
,
7741 Left_Opnd
=> Duplicate_Subexpr
(Base
),
7742 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)),
7743 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)));
7748 -- En : constant base'type := base * base;
7753 Temp
:= Make_Temporary
(Loc
, 'E', Base
);
7756 Make_Expression_With_Actions
(Loc
,
7757 Actions
=> New_List
(
7758 Make_Object_Declaration
(Loc
,
7759 Defining_Identifier
=> Temp
,
7760 Constant_Present
=> True,
7761 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
7764 Make_Op_Multiply
(Loc
,
7766 Duplicate_Subexpr
(Base
),
7768 Duplicate_Subexpr_No_Checks
(Base
))))),
7772 Make_Op_Multiply
(Loc
,
7773 Left_Opnd
=> New_Occurrence_Of
(Temp
, Loc
),
7774 Right_Opnd
=> New_Occurrence_Of
(Temp
, Loc
))));
7776 -- X ** N = 1.0 / X ** (-N)
7781 (Expv
= -1 or Expv
= -2 or Expv
= -3 or Expv
= -4);
7784 Make_Op_Divide
(Loc
,
7786 Make_Float_Literal
(Loc
,
7788 Significand
=> Uint_1
,
7789 Exponent
=> Uint_0
),
7792 Left_Opnd
=> Duplicate_Subexpr
(Base
),
7794 Make_Integer_Literal
(Loc
,
7799 Analyze_And_Resolve
(N
, Typ
);
7804 -- Deal with optimizing 2 ** expression to shift where possible
7806 -- Note: we used to check that Exptyp was an unsigned type. But that is
7807 -- an unnecessary check, since if Exp is negative, we have a run-time
7808 -- error that is either caught (so we get the right result) or we have
7809 -- suppressed the check, in which case the code is erroneous anyway.
7811 if Is_Integer_Type
(Rtyp
)
7813 -- The base value must be "safe compile-time known", and exactly 2
7815 and then Nkind
(Base
) = N_Integer_Literal
7816 and then CRT_Safe_Compile_Time_Known_Value
(Base
)
7817 and then Expr_Value
(Base
) = Uint_2
7819 -- We only handle cases where the right type is a integer
7821 and then Is_Integer_Type
(Root_Type
(Exptyp
))
7822 and then Esize
(Root_Type
(Exptyp
)) <= Esize
(Standard_Integer
)
7824 -- This transformation is not applicable for a modular type with a
7825 -- nonbinary modulus because we do not handle modular reduction in
7826 -- a correct manner if we attempt this transformation in this case.
7828 and then not Non_Binary_Modulus
(Typ
)
7830 -- Handle the cases where our parent is a division or multiplication
7831 -- specially. In these cases we can convert to using a shift at the
7832 -- parent level if we are not doing overflow checking, since it is
7833 -- too tricky to combine the overflow check at the parent level.
7836 and then Nkind_In
(Parent
(N
), N_Op_Divide
, N_Op_Multiply
)
7839 P
: constant Node_Id
:= Parent
(N
);
7840 L
: constant Node_Id
:= Left_Opnd
(P
);
7841 R
: constant Node_Id
:= Right_Opnd
(P
);
7844 if (Nkind
(P
) = N_Op_Multiply
7846 ((Is_Integer_Type
(Etype
(L
)) and then R
= N
)
7848 (Is_Integer_Type
(Etype
(R
)) and then L
= N
))
7849 and then not Do_Overflow_Check
(P
))
7852 (Nkind
(P
) = N_Op_Divide
7853 and then Is_Integer_Type
(Etype
(L
))
7854 and then Is_Unsigned_Type
(Etype
(L
))
7856 and then not Do_Overflow_Check
(P
))
7858 Set_Is_Power_Of_2_For_Shift
(N
);
7863 -- Here we just have 2 ** N on its own, so we can convert this to a
7864 -- shift node. We are prepared to deal with overflow here, and we
7865 -- also have to handle proper modular reduction for binary modular.
7874 -- Maximum shift count with no overflow
7877 -- Set True if we must test the shift count
7880 -- Node for test against TestS
7883 -- Compute maximum shift based on the underlying size. For a
7884 -- modular type this is one less than the size.
7886 if Is_Modular_Integer_Type
(Typ
) then
7888 -- For modular integer types, this is the size of the value
7889 -- being shifted minus one. Any larger values will cause
7890 -- modular reduction to a result of zero. Note that we do
7891 -- want the RM_Size here (e.g. mod 2 ** 7, we want a result
7892 -- of 6, since 2**7 should be reduced to zero).
7894 MaxS
:= RM_Size
(Rtyp
) - 1;
7896 -- For signed integer types, we use the size of the value
7897 -- being shifted minus 2. Larger values cause overflow.
7900 MaxS
:= Esize
(Rtyp
) - 2;
7903 -- Determine range to see if it can be larger than MaxS
7906 (Right_Opnd
(N
), OK
, Lo
, Hi
, Assume_Valid
=> True);
7907 TestS
:= (not OK
) or else Hi
> MaxS
;
7909 -- Signed integer case
7911 if Is_Signed_Integer_Type
(Typ
) then
7913 -- Generate overflow check if overflow is active. Note that
7914 -- we can simply ignore the possibility of overflow if the
7915 -- flag is not set (means that overflow cannot happen or
7916 -- that overflow checks are suppressed).
7918 if Ovflo
and TestS
then
7920 Make_Raise_Constraint_Error
(Loc
,
7923 Left_Opnd
=> Duplicate_Subexpr
(Right_Opnd
(N
)),
7924 Right_Opnd
=> Make_Integer_Literal
(Loc
, MaxS
)),
7925 Reason
=> CE_Overflow_Check_Failed
));
7928 -- Now rewrite node as Shift_Left (1, right-operand)
7931 Make_Op_Shift_Left
(Loc
,
7932 Left_Opnd
=> Make_Integer_Literal
(Loc
, Uint_1
),
7933 Right_Opnd
=> Right_Opnd
(N
)));
7935 -- Modular integer case
7937 else pragma Assert
(Is_Modular_Integer_Type
(Typ
));
7939 -- If shift count can be greater than MaxS, we need to wrap
7940 -- the shift in a test that will reduce the result value to
7941 -- zero if this shift count is exceeded.
7945 -- Note: build node for the comparison first, before we
7946 -- reuse the Right_Opnd, so that we have proper parents
7947 -- in place for the Duplicate_Subexpr call.
7951 Left_Opnd
=> Duplicate_Subexpr
(Right_Opnd
(N
)),
7952 Right_Opnd
=> Make_Integer_Literal
(Loc
, MaxS
));
7955 Make_If_Expression
(Loc
,
7956 Expressions
=> New_List
(
7958 Make_Integer_Literal
(Loc
, Uint_0
),
7959 Make_Op_Shift_Left
(Loc
,
7960 Left_Opnd
=> Make_Integer_Literal
(Loc
, Uint_1
),
7961 Right_Opnd
=> Right_Opnd
(N
)))));
7963 -- If we know shift count cannot be greater than MaxS, then
7964 -- it is safe to just rewrite as a shift with no test.
7968 Make_Op_Shift_Left
(Loc
,
7969 Left_Opnd
=> Make_Integer_Literal
(Loc
, Uint_1
),
7970 Right_Opnd
=> Right_Opnd
(N
)));
7974 Analyze_And_Resolve
(N
, Typ
);
7980 -- Fall through if exponentiation must be done using a runtime routine
7982 -- First deal with modular case
7984 if Is_Modular_Integer_Type
(Rtyp
) then
7986 -- Nonbinary modular case, we call the special exponentiation
7987 -- routine for the nonbinary case, converting the argument to
7988 -- Long_Long_Integer and passing the modulus value. Then the
7989 -- result is converted back to the base type.
7991 if Non_Binary_Modulus
(Rtyp
) then
7994 Make_Function_Call
(Loc
,
7996 New_Occurrence_Of
(RTE
(RE_Exp_Modular
), Loc
),
7997 Parameter_Associations
=> New_List
(
7998 Convert_To
(RTE
(RE_Unsigned
), Base
),
7999 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
)),
8002 -- Binary modular case, in this case, we call one of two routines,
8003 -- either the unsigned integer case, or the unsigned long long
8004 -- integer case, with a final "and" operation to do the required mod.
8007 if UI_To_Int
(Esize
(Rtyp
)) <= Standard_Integer_Size
then
8008 Ent
:= RTE
(RE_Exp_Unsigned
);
8010 Ent
:= RTE
(RE_Exp_Long_Long_Unsigned
);
8017 Make_Function_Call
(Loc
,
8018 Name
=> New_Occurrence_Of
(Ent
, Loc
),
8019 Parameter_Associations
=> New_List
(
8020 Convert_To
(Etype
(First_Formal
(Ent
)), Base
),
8023 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
) - 1))));
8027 -- Common exit point for modular type case
8029 Analyze_And_Resolve
(N
, Typ
);
8032 -- Signed integer cases, done using either Integer or Long_Long_Integer.
8033 -- It is not worth having routines for Short_[Short_]Integer, since for
8034 -- most machines it would not help, and it would generate more code that
8035 -- might need certification when a certified run time is required.
8037 -- In the integer cases, we have two routines, one for when overflow
8038 -- checks are required, and one when they are not required, since there
8039 -- is a real gain in omitting checks on many machines.
8041 elsif Rtyp
= Base_Type
(Standard_Long_Long_Integer
)
8042 or else (Rtyp
= Base_Type
(Standard_Long_Integer
)
8044 Esize
(Standard_Long_Integer
) > Esize
(Standard_Integer
))
8045 or else Rtyp
= Universal_Integer
8047 Etyp
:= Standard_Long_Long_Integer
;
8050 Rent
:= RE_Exp_Long_Long_Integer
;
8052 Rent
:= RE_Exn_Long_Long_Integer
;
8055 elsif Is_Signed_Integer_Type
(Rtyp
) then
8056 Etyp
:= Standard_Integer
;
8059 Rent
:= RE_Exp_Integer
;
8061 Rent
:= RE_Exn_Integer
;
8064 -- Floating-point cases. We do not need separate routines for the
8065 -- overflow case here, since in the case of floating-point, we generate
8066 -- infinities anyway as a rule (either that or we automatically trap
8067 -- overflow), and if there is an infinity generated and a range check
8068 -- is required, the check will fail anyway.
8070 -- Historical note: we used to convert everything to Long_Long_Float
8071 -- and call a single common routine, but this had the undesirable effect
8072 -- of giving different results for small static exponent values and the
8073 -- same dynamic values.
8076 pragma Assert
(Is_Floating_Point_Type
(Rtyp
));
8078 if Rtyp
= Standard_Float
then
8079 Etyp
:= Standard_Float
;
8080 Rent
:= RE_Exn_Float
;
8082 elsif Rtyp
= Standard_Long_Float
then
8083 Etyp
:= Standard_Long_Float
;
8084 Rent
:= RE_Exn_Long_Float
;
8087 Etyp
:= Standard_Long_Long_Float
;
8088 Rent
:= RE_Exn_Long_Long_Float
;
8092 -- Common processing for integer cases and floating-point cases.
8093 -- If we are in the right type, we can call runtime routine directly
8096 and then Rtyp
/= Universal_Integer
8097 and then Rtyp
/= Universal_Real
8101 Make_Function_Call
(Loc
,
8102 Name
=> New_Occurrence_Of
(RTE
(Rent
), Loc
),
8103 Parameter_Associations
=> New_List
(Base
, Exp
))));
8105 -- Otherwise we have to introduce conversions (conversions are also
8106 -- required in the universal cases, since the runtime routine is
8107 -- typed using one of the standard types).
8112 Make_Function_Call
(Loc
,
8113 Name
=> New_Occurrence_Of
(RTE
(Rent
), Loc
),
8114 Parameter_Associations
=> New_List
(
8115 Convert_To
(Etyp
, Base
),
8119 Analyze_And_Resolve
(N
, Typ
);
8123 when RE_Not_Available
=>
8125 end Expand_N_Op_Expon
;
8127 --------------------
8128 -- Expand_N_Op_Ge --
8129 --------------------
8131 procedure Expand_N_Op_Ge
(N
: Node_Id
) is
8132 Typ
: constant Entity_Id
:= Etype
(N
);
8133 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8134 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8135 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8138 Binary_Op_Validity_Checks
(N
);
8140 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8141 -- means we no longer have a comparison operation, we are all done.
8143 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8145 if Nkind
(N
) /= N_Op_Ge
then
8151 if Is_Array_Type
(Typ1
) then
8152 Expand_Array_Comparison
(N
);
8156 -- Deal with boolean operands
8158 if Is_Boolean_Type
(Typ1
) then
8159 Adjust_Condition
(Op1
);
8160 Adjust_Condition
(Op2
);
8161 Set_Etype
(N
, Standard_Boolean
);
8162 Adjust_Result_Type
(N
, Typ
);
8165 Rewrite_Comparison
(N
);
8167 Optimize_Length_Comparison
(N
);
8170 --------------------
8171 -- Expand_N_Op_Gt --
8172 --------------------
8174 procedure Expand_N_Op_Gt
(N
: Node_Id
) is
8175 Typ
: constant Entity_Id
:= Etype
(N
);
8176 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8177 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8178 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8181 Binary_Op_Validity_Checks
(N
);
8183 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8184 -- means we no longer have a comparison operation, we are all done.
8186 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8188 if Nkind
(N
) /= N_Op_Gt
then
8192 -- Deal with array type operands
8194 if Is_Array_Type
(Typ1
) then
8195 Expand_Array_Comparison
(N
);
8199 -- Deal with boolean type operands
8201 if Is_Boolean_Type
(Typ1
) then
8202 Adjust_Condition
(Op1
);
8203 Adjust_Condition
(Op2
);
8204 Set_Etype
(N
, Standard_Boolean
);
8205 Adjust_Result_Type
(N
, Typ
);
8208 Rewrite_Comparison
(N
);
8210 Optimize_Length_Comparison
(N
);
8213 --------------------
8214 -- Expand_N_Op_Le --
8215 --------------------
8217 procedure Expand_N_Op_Le
(N
: Node_Id
) is
8218 Typ
: constant Entity_Id
:= Etype
(N
);
8219 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8220 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8221 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8224 Binary_Op_Validity_Checks
(N
);
8226 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8227 -- means we no longer have a comparison operation, we are all done.
8229 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8231 if Nkind
(N
) /= N_Op_Le
then
8235 -- Deal with array type operands
8237 if Is_Array_Type
(Typ1
) then
8238 Expand_Array_Comparison
(N
);
8242 -- Deal with Boolean type operands
8244 if Is_Boolean_Type
(Typ1
) then
8245 Adjust_Condition
(Op1
);
8246 Adjust_Condition
(Op2
);
8247 Set_Etype
(N
, Standard_Boolean
);
8248 Adjust_Result_Type
(N
, Typ
);
8251 Rewrite_Comparison
(N
);
8253 Optimize_Length_Comparison
(N
);
8256 --------------------
8257 -- Expand_N_Op_Lt --
8258 --------------------
8260 procedure Expand_N_Op_Lt
(N
: Node_Id
) is
8261 Typ
: constant Entity_Id
:= Etype
(N
);
8262 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8263 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8264 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
8267 Binary_Op_Validity_Checks
(N
);
8269 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8270 -- means we no longer have a comparison operation, we are all done.
8272 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8274 if Nkind
(N
) /= N_Op_Lt
then
8278 -- Deal with array type operands
8280 if Is_Array_Type
(Typ1
) then
8281 Expand_Array_Comparison
(N
);
8285 -- Deal with Boolean type operands
8287 if Is_Boolean_Type
(Typ1
) then
8288 Adjust_Condition
(Op1
);
8289 Adjust_Condition
(Op2
);
8290 Set_Etype
(N
, Standard_Boolean
);
8291 Adjust_Result_Type
(N
, Typ
);
8294 Rewrite_Comparison
(N
);
8296 Optimize_Length_Comparison
(N
);
8299 -----------------------
8300 -- Expand_N_Op_Minus --
8301 -----------------------
8303 procedure Expand_N_Op_Minus
(N
: Node_Id
) is
8304 Loc
: constant Source_Ptr
:= Sloc
(N
);
8305 Typ
: constant Entity_Id
:= Etype
(N
);
8308 Unary_Op_Validity_Checks
(N
);
8310 -- Check for MINIMIZED/ELIMINATED overflow mode
8312 if Minimized_Eliminated_Overflow_Check
(N
) then
8313 Apply_Arithmetic_Overflow_Check
(N
);
8317 if not Backend_Overflow_Checks_On_Target
8318 and then Is_Signed_Integer_Type
(Etype
(N
))
8319 and then Do_Overflow_Check
(N
)
8321 -- Software overflow checking expands -expr into (0 - expr)
8324 Make_Op_Subtract
(Loc
,
8325 Left_Opnd
=> Make_Integer_Literal
(Loc
, 0),
8326 Right_Opnd
=> Right_Opnd
(N
)));
8328 Analyze_And_Resolve
(N
, Typ
);
8330 end Expand_N_Op_Minus
;
8332 ---------------------
8333 -- Expand_N_Op_Mod --
8334 ---------------------
8336 procedure Expand_N_Op_Mod
(N
: Node_Id
) is
8337 Loc
: constant Source_Ptr
:= Sloc
(N
);
8338 Typ
: constant Entity_Id
:= Etype
(N
);
8339 DDC
: constant Boolean := Do_Division_Check
(N
);
8352 pragma Warnings
(Off
, Lhi
);
8355 Binary_Op_Validity_Checks
(N
);
8357 -- Check for MINIMIZED/ELIMINATED overflow mode
8359 if Minimized_Eliminated_Overflow_Check
(N
) then
8360 Apply_Arithmetic_Overflow_Check
(N
);
8364 if Is_Integer_Type
(Etype
(N
)) then
8365 Apply_Divide_Checks
(N
);
8367 -- All done if we don't have a MOD any more, which can happen as a
8368 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8370 if Nkind
(N
) /= N_Op_Mod
then
8375 -- Proceed with expansion of mod operator
8377 Left
:= Left_Opnd
(N
);
8378 Right
:= Right_Opnd
(N
);
8380 Determine_Range
(Right
, ROK
, Rlo
, Rhi
, Assume_Valid
=> True);
8381 Determine_Range
(Left
, LOK
, Llo
, Lhi
, Assume_Valid
=> True);
8383 -- Convert mod to rem if operands are both known to be non-negative, or
8384 -- both known to be non-positive (these are the cases in which rem and
8385 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
8386 -- likely that this will improve the quality of code, (the operation now
8387 -- corresponds to the hardware remainder), and it does not seem likely
8388 -- that it could be harmful. It also avoids some cases of the elaborate
8389 -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %).
8392 and then ((Llo
>= 0 and then Rlo
>= 0)
8394 (Lhi
<= 0 and then Rhi
<= 0))
8397 Make_Op_Rem
(Sloc
(N
),
8398 Left_Opnd
=> Left_Opnd
(N
),
8399 Right_Opnd
=> Right_Opnd
(N
)));
8401 -- Instead of reanalyzing the node we do the analysis manually. This
8402 -- avoids anomalies when the replacement is done in an instance and
8403 -- is epsilon more efficient.
8405 Set_Entity
(N
, Standard_Entity
(S_Op_Rem
));
8407 Set_Do_Division_Check
(N
, DDC
);
8408 Expand_N_Op_Rem
(N
);
8412 -- Otherwise, normal mod processing
8415 -- Apply optimization x mod 1 = 0. We don't really need that with
8416 -- gcc, but it is useful with other back ends and is certainly
8419 if Is_Integer_Type
(Etype
(N
))
8420 and then Compile_Time_Known_Value
(Right
)
8421 and then Expr_Value
(Right
) = Uint_1
8423 -- Call Remove_Side_Effects to ensure that any side effects in
8424 -- the ignored left operand (in particular function calls to
8425 -- user defined functions) are properly executed.
8427 Remove_Side_Effects
(Left
);
8429 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
8430 Analyze_And_Resolve
(N
, Typ
);
8434 -- If we still have a mod operator and we are in Modify_Tree_For_C
8435 -- mode, and we have a signed integer type, then here is where we do
8436 -- the rewrite in terms of Rem. Note this rewrite bypasses the need
8437 -- for the special handling of the annoying case of largest negative
8438 -- number mod minus one.
8440 if Nkind
(N
) = N_Op_Mod
8441 and then Is_Signed_Integer_Type
(Typ
)
8442 and then Modify_Tree_For_C
8444 -- In the general case, we expand A mod B as
8446 -- Tnn : constant typ := A rem B;
8448 -- (if (A >= 0) = (B >= 0) then Tnn
8449 -- elsif Tnn = 0 then 0
8452 -- The comparison can be written simply as A >= 0 if we know that
8453 -- B >= 0 which is a very common case.
8455 -- An important optimization is when B is known at compile time
8456 -- to be 2**K for some constant. In this case we can simply AND
8457 -- the left operand with the bit string 2**K-1 (i.e. K 1-bits)
8458 -- and that works for both the positive and negative cases.
8461 P2
: constant Nat
:= Power_Of_Two
(Right
);
8466 Unchecked_Convert_To
(Typ
,
8469 Unchecked_Convert_To
8470 (Corresponding_Unsigned_Type
(Typ
), Left
),
8472 Make_Integer_Literal
(Loc
, 2 ** P2
- 1))));
8473 Analyze_And_Resolve
(N
, Typ
);
8478 -- Here for the full rewrite
8481 Tnn
: constant Entity_Id
:= Make_Temporary
(Sloc
(N
), 'T', N
);
8487 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left
),
8488 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
8490 if not LOK
or else Rlo
< 0 then
8496 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Right
),
8497 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)));
8501 Make_Object_Declaration
(Loc
,
8502 Defining_Identifier
=> Tnn
,
8503 Constant_Present
=> True,
8504 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
),
8508 Right_Opnd
=> Right
)));
8511 Make_If_Expression
(Loc
,
8512 Expressions
=> New_List
(
8514 New_Occurrence_Of
(Tnn
, Loc
),
8515 Make_If_Expression
(Loc
,
8517 Expressions
=> New_List
(
8519 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
8520 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
8521 Make_Integer_Literal
(Loc
, 0),
8523 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
8525 Duplicate_Subexpr_No_Checks
(Right
)))))));
8527 Analyze_And_Resolve
(N
, Typ
);
8532 -- Deal with annoying case of largest negative number mod minus one.
8533 -- Gigi may not handle this case correctly, because on some targets,
8534 -- the mod value is computed using a divide instruction which gives
8535 -- an overflow trap for this case.
8537 -- It would be a bit more efficient to figure out which targets
8538 -- this is really needed for, but in practice it is reasonable
8539 -- to do the following special check in all cases, since it means
8540 -- we get a clearer message, and also the overhead is minimal given
8541 -- that division is expensive in any case.
8543 -- In fact the check is quite easy, if the right operand is -1, then
8544 -- the mod value is always 0, and we can just ignore the left operand
8545 -- completely in this case.
8547 -- This only applies if we still have a mod operator. Skip if we
8548 -- have already rewritten this (e.g. in the case of eliminated
8549 -- overflow checks which have driven us into bignum mode).
8551 if Nkind
(N
) = N_Op_Mod
then
8553 -- The operand type may be private (e.g. in the expansion of an
8554 -- intrinsic operation) so we must use the underlying type to get
8555 -- the bounds, and convert the literals explicitly.
8559 (Type_Low_Bound
(Base_Type
(Underlying_Type
(Etype
(Left
)))));
8561 if ((not ROK
) or else (Rlo
<= (-1) and then (-1) <= Rhi
))
8562 and then ((not LOK
) or else (Llo
= LLB
))
8565 Make_If_Expression
(Loc
,
8566 Expressions
=> New_List
(
8568 Left_Opnd
=> Duplicate_Subexpr
(Right
),
8570 Unchecked_Convert_To
(Typ
,
8571 Make_Integer_Literal
(Loc
, -1))),
8572 Unchecked_Convert_To
(Typ
,
8573 Make_Integer_Literal
(Loc
, Uint_0
)),
8574 Relocate_Node
(N
))));
8576 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
8577 Analyze_And_Resolve
(N
, Typ
);
8581 end Expand_N_Op_Mod
;
8583 --------------------------
8584 -- Expand_N_Op_Multiply --
8585 --------------------------
8587 procedure Expand_N_Op_Multiply
(N
: Node_Id
) is
8588 Loc
: constant Source_Ptr
:= Sloc
(N
);
8589 Lop
: constant Node_Id
:= Left_Opnd
(N
);
8590 Rop
: constant Node_Id
:= Right_Opnd
(N
);
8592 Lp2
: constant Boolean :=
8593 Nkind
(Lop
) = N_Op_Expon
and then Is_Power_Of_2_For_Shift
(Lop
);
8594 Rp2
: constant Boolean :=
8595 Nkind
(Rop
) = N_Op_Expon
and then Is_Power_Of_2_For_Shift
(Rop
);
8597 Ltyp
: constant Entity_Id
:= Etype
(Lop
);
8598 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
8599 Typ
: Entity_Id
:= Etype
(N
);
8602 Binary_Op_Validity_Checks
(N
);
8604 -- Check for MINIMIZED/ELIMINATED overflow mode
8606 if Minimized_Eliminated_Overflow_Check
(N
) then
8607 Apply_Arithmetic_Overflow_Check
(N
);
8611 -- Special optimizations for integer types
8613 if Is_Integer_Type
(Typ
) then
8615 -- N * 0 = 0 for integer types
8617 if Compile_Time_Known_Value
(Rop
)
8618 and then Expr_Value
(Rop
) = Uint_0
8620 -- Call Remove_Side_Effects to ensure that any side effects in
8621 -- the ignored left operand (in particular function calls to
8622 -- user defined functions) are properly executed.
8624 Remove_Side_Effects
(Lop
);
8626 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
8627 Analyze_And_Resolve
(N
, Typ
);
8631 -- Similar handling for 0 * N = 0
8633 if Compile_Time_Known_Value
(Lop
)
8634 and then Expr_Value
(Lop
) = Uint_0
8636 Remove_Side_Effects
(Rop
);
8637 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
8638 Analyze_And_Resolve
(N
, Typ
);
8642 -- N * 1 = 1 * N = N for integer types
8644 -- This optimisation is not done if we are going to
8645 -- rewrite the product 1 * 2 ** N to a shift.
8647 if Compile_Time_Known_Value
(Rop
)
8648 and then Expr_Value
(Rop
) = Uint_1
8654 elsif Compile_Time_Known_Value
(Lop
)
8655 and then Expr_Value
(Lop
) = Uint_1
8663 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
8664 -- Is_Power_Of_2_For_Shift is set means that we know that our left
8665 -- operand is an integer, as required for this to work.
8670 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
8674 Left_Opnd
=> Make_Integer_Literal
(Loc
, 2),
8677 Left_Opnd
=> Right_Opnd
(Lop
),
8678 Right_Opnd
=> Right_Opnd
(Rop
))));
8679 Analyze_And_Resolve
(N
, Typ
);
8683 -- If the result is modular, perform the reduction of the result
8686 if Is_Modular_Integer_Type
(Typ
)
8687 and then not Non_Binary_Modulus
(Typ
)
8692 Make_Op_Shift_Left
(Loc
,
8695 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))),
8697 Make_Integer_Literal
(Loc
, Modulus
(Typ
) - 1)));
8701 Make_Op_Shift_Left
(Loc
,
8704 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))));
8707 Analyze_And_Resolve
(N
, Typ
);
8711 -- Same processing for the operands the other way round
8714 if Is_Modular_Integer_Type
(Typ
)
8715 and then not Non_Binary_Modulus
(Typ
)
8720 Make_Op_Shift_Left
(Loc
,
8723 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))),
8725 Make_Integer_Literal
(Loc
, Modulus
(Typ
) - 1)));
8729 Make_Op_Shift_Left
(Loc
,
8732 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))));
8735 Analyze_And_Resolve
(N
, Typ
);
8739 -- Do required fixup of universal fixed operation
8741 if Typ
= Universal_Fixed
then
8742 Fixup_Universal_Fixed_Operation
(N
);
8746 -- Multiplications with fixed-point results
8748 if Is_Fixed_Point_Type
(Typ
) then
8750 -- No special processing if Treat_Fixed_As_Integer is set, since from
8751 -- a semantic point of view such operations are simply integer
8752 -- operations and will be treated that way.
8754 if not Treat_Fixed_As_Integer
(N
) then
8756 -- Case of fixed * integer => fixed
8758 if Is_Integer_Type
(Rtyp
) then
8759 Expand_Multiply_Fixed_By_Integer_Giving_Fixed
(N
);
8761 -- Case of integer * fixed => fixed
8763 elsif Is_Integer_Type
(Ltyp
) then
8764 Expand_Multiply_Integer_By_Fixed_Giving_Fixed
(N
);
8766 -- Case of fixed * fixed => fixed
8769 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed
(N
);
8773 -- Other cases of multiplication of fixed-point operands. Again we
8774 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
8776 elsif (Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
))
8777 and then not Treat_Fixed_As_Integer
(N
)
8779 if Is_Integer_Type
(Typ
) then
8780 Expand_Multiply_Fixed_By_Fixed_Giving_Integer
(N
);
8782 pragma Assert
(Is_Floating_Point_Type
(Typ
));
8783 Expand_Multiply_Fixed_By_Fixed_Giving_Float
(N
);
8786 -- Mixed-mode operations can appear in a non-static universal context,
8787 -- in which case the integer argument must be converted explicitly.
8789 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Rtyp
) then
8790 Rewrite
(Rop
, Convert_To
(Universal_Real
, Relocate_Node
(Rop
)));
8791 Analyze_And_Resolve
(Rop
, Universal_Real
);
8793 elsif Typ
= Universal_Real
and then Is_Integer_Type
(Ltyp
) then
8794 Rewrite
(Lop
, Convert_To
(Universal_Real
, Relocate_Node
(Lop
)));
8795 Analyze_And_Resolve
(Lop
, Universal_Real
);
8797 -- Non-fixed point cases, check software overflow checking required
8799 elsif Is_Signed_Integer_Type
(Etype
(N
)) then
8800 Apply_Arithmetic_Overflow_Check
(N
);
8803 -- Overflow checks for floating-point if -gnateF mode active
8805 Check_Float_Op_Overflow
(N
);
8806 end Expand_N_Op_Multiply
;
8808 --------------------
8809 -- Expand_N_Op_Ne --
8810 --------------------
8812 procedure Expand_N_Op_Ne
(N
: Node_Id
) is
8813 Typ
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
8816 -- Case of elementary type with standard operator
8818 if Is_Elementary_Type
(Typ
)
8819 and then Sloc
(Entity
(N
)) = Standard_Location
8821 Binary_Op_Validity_Checks
(N
);
8823 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
8824 -- means we no longer have a /= operation, we are all done.
8826 Expand_Compare_Minimize_Eliminate_Overflow
(N
);
8828 if Nkind
(N
) /= N_Op_Ne
then
8832 -- Boolean types (requiring handling of non-standard case)
8834 if Is_Boolean_Type
(Typ
) then
8835 Adjust_Condition
(Left_Opnd
(N
));
8836 Adjust_Condition
(Right_Opnd
(N
));
8837 Set_Etype
(N
, Standard_Boolean
);
8838 Adjust_Result_Type
(N
, Typ
);
8841 Rewrite_Comparison
(N
);
8843 -- For all cases other than elementary types, we rewrite node as the
8844 -- negation of an equality operation, and reanalyze. The equality to be
8845 -- used is defined in the same scope and has the same signature. This
8846 -- signature must be set explicitly since in an instance it may not have
8847 -- the same visibility as in the generic unit. This avoids duplicating
8848 -- or factoring the complex code for record/array equality tests etc.
8852 Loc
: constant Source_Ptr
:= Sloc
(N
);
8854 Ne
: constant Entity_Id
:= Entity
(N
);
8857 Binary_Op_Validity_Checks
(N
);
8863 Left_Opnd
=> Left_Opnd
(N
),
8864 Right_Opnd
=> Right_Opnd
(N
)));
8865 Set_Paren_Count
(Right_Opnd
(Neg
), 1);
8867 if Scope
(Ne
) /= Standard_Standard
then
8868 Set_Entity
(Right_Opnd
(Neg
), Corresponding_Equality
(Ne
));
8871 -- For navigation purposes, we want to treat the inequality as an
8872 -- implicit reference to the corresponding equality. Preserve the
8873 -- Comes_From_ source flag to generate proper Xref entries.
8875 Preserve_Comes_From_Source
(Neg
, N
);
8876 Preserve_Comes_From_Source
(Right_Opnd
(Neg
), N
);
8878 Analyze_And_Resolve
(N
, Standard_Boolean
);
8882 Optimize_Length_Comparison
(N
);
8885 ---------------------
8886 -- Expand_N_Op_Not --
8887 ---------------------
8889 -- If the argument is other than a Boolean array type, there is no special
8890 -- expansion required, except for dealing with validity checks, and non-
8891 -- standard boolean representations.
8893 -- For the packed array case, we call the special routine in Exp_Pakd,
8894 -- except that if the component size is greater than one, we use the
8895 -- standard routine generating a gruesome loop (it is so peculiar to have
8896 -- packed arrays with non-standard Boolean representations anyway, so it
8897 -- does not matter that we do not handle this case efficiently).
8899 -- For the unpacked array case (and for the special packed case where we
8900 -- have non standard Booleans, as discussed above), we generate and insert
8901 -- into the tree the following function definition:
8903 -- function Nnnn (A : arr) is
8906 -- for J in a'range loop
8907 -- B (J) := not A (J);
8912 -- Here arr is the actual subtype of the parameter (and hence always
8913 -- constrained). Then we replace the not with a call to this function.
8915 procedure Expand_N_Op_Not
(N
: Node_Id
) is
8916 Loc
: constant Source_Ptr
:= Sloc
(N
);
8917 Typ
: constant Entity_Id
:= Etype
(N
);
8926 Func_Name
: Entity_Id
;
8927 Loop_Statement
: Node_Id
;
8930 Unary_Op_Validity_Checks
(N
);
8932 -- For boolean operand, deal with non-standard booleans
8934 if Is_Boolean_Type
(Typ
) then
8935 Adjust_Condition
(Right_Opnd
(N
));
8936 Set_Etype
(N
, Standard_Boolean
);
8937 Adjust_Result_Type
(N
, Typ
);
8941 -- Only array types need any other processing
8943 if not Is_Array_Type
(Typ
) then
8947 -- Case of array operand. If bit packed with a component size of 1,
8948 -- handle it in Exp_Pakd if the operand is known to be aligned.
8950 if Is_Bit_Packed_Array
(Typ
)
8951 and then Component_Size
(Typ
) = 1
8952 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
8954 Expand_Packed_Not
(N
);
8958 -- Case of array operand which is not bit-packed. If the context is
8959 -- a safe assignment, call in-place operation, If context is a larger
8960 -- boolean expression in the context of a safe assignment, expansion is
8961 -- done by enclosing operation.
8963 Opnd
:= Relocate_Node
(Right_Opnd
(N
));
8964 Convert_To_Actual_Subtype
(Opnd
);
8965 Arr
:= Etype
(Opnd
);
8966 Ensure_Defined
(Arr
, N
);
8967 Silly_Boolean_Array_Not_Test
(N
, Arr
);
8969 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
8970 if Safe_In_Place_Array_Op
(Name
(Parent
(N
)), N
, Empty
) then
8971 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
8974 -- Special case the negation of a binary operation
8976 elsif Nkind_In
(Opnd
, N_Op_And
, N_Op_Or
, N_Op_Xor
)
8977 and then Safe_In_Place_Array_Op
8978 (Name
(Parent
(N
)), Left_Opnd
(Opnd
), Right_Opnd
(Opnd
))
8980 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
8984 elsif Nkind
(Parent
(N
)) in N_Binary_Op
8985 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
8988 Op1
: constant Node_Id
:= Left_Opnd
(Parent
(N
));
8989 Op2
: constant Node_Id
:= Right_Opnd
(Parent
(N
));
8990 Lhs
: constant Node_Id
:= Name
(Parent
(Parent
(N
)));
8993 if Safe_In_Place_Array_Op
(Lhs
, Op1
, Op2
) then
8995 -- (not A) op (not B) can be reduced to a single call
8997 if N
= Op1
and then Nkind
(Op2
) = N_Op_Not
then
9000 elsif N
= Op2
and then Nkind
(Op1
) = N_Op_Not
then
9003 -- A xor (not B) can also be special-cased
9005 elsif N
= Op2
and then Nkind
(Parent
(N
)) = N_Op_Xor
then
9012 A
:= Make_Defining_Identifier
(Loc
, Name_uA
);
9013 B
:= Make_Defining_Identifier
(Loc
, Name_uB
);
9014 J
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
9017 Make_Indexed_Component
(Loc
,
9018 Prefix
=> New_Occurrence_Of
(A
, Loc
),
9019 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
9022 Make_Indexed_Component
(Loc
,
9023 Prefix
=> New_Occurrence_Of
(B
, Loc
),
9024 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
9027 Make_Implicit_Loop_Statement
(N
,
9028 Identifier
=> Empty
,
9031 Make_Iteration_Scheme
(Loc
,
9032 Loop_Parameter_Specification
=>
9033 Make_Loop_Parameter_Specification
(Loc
,
9034 Defining_Identifier
=> J
,
9035 Discrete_Subtype_Definition
=>
9036 Make_Attribute_Reference
(Loc
,
9037 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
9038 Attribute_Name
=> Name_Range
))),
9040 Statements
=> New_List
(
9041 Make_Assignment_Statement
(Loc
,
9043 Expression
=> Make_Op_Not
(Loc
, A_J
))));
9045 Func_Name
:= Make_Temporary
(Loc
, 'N');
9046 Set_Is_Inlined
(Func_Name
);
9049 Make_Subprogram_Body
(Loc
,
9051 Make_Function_Specification
(Loc
,
9052 Defining_Unit_Name
=> Func_Name
,
9053 Parameter_Specifications
=> New_List
(
9054 Make_Parameter_Specification
(Loc
,
9055 Defining_Identifier
=> A
,
9056 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
))),
9057 Result_Definition
=> New_Occurrence_Of
(Typ
, Loc
)),
9059 Declarations
=> New_List
(
9060 Make_Object_Declaration
(Loc
,
9061 Defining_Identifier
=> B
,
9062 Object_Definition
=> New_Occurrence_Of
(Arr
, Loc
))),
9064 Handled_Statement_Sequence
=>
9065 Make_Handled_Sequence_Of_Statements
(Loc
,
9066 Statements
=> New_List
(
9068 Make_Simple_Return_Statement
(Loc
,
9069 Expression
=> Make_Identifier
(Loc
, Chars
(B
)))))));
9072 Make_Function_Call
(Loc
,
9073 Name
=> New_Occurrence_Of
(Func_Name
, Loc
),
9074 Parameter_Associations
=> New_List
(Opnd
)));
9076 Analyze_And_Resolve
(N
, Typ
);
9077 end Expand_N_Op_Not
;
9079 --------------------
9080 -- Expand_N_Op_Or --
9081 --------------------
9083 procedure Expand_N_Op_Or
(N
: Node_Id
) is
9084 Typ
: constant Entity_Id
:= Etype
(N
);
9087 Binary_Op_Validity_Checks
(N
);
9089 if Is_Array_Type
(Etype
(N
)) then
9090 Expand_Boolean_Operator
(N
);
9092 elsif Is_Boolean_Type
(Etype
(N
)) then
9093 Adjust_Condition
(Left_Opnd
(N
));
9094 Adjust_Condition
(Right_Opnd
(N
));
9095 Set_Etype
(N
, Standard_Boolean
);
9096 Adjust_Result_Type
(N
, Typ
);
9098 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
9099 Expand_Intrinsic_Call
(N
, Entity
(N
));
9104 ----------------------
9105 -- Expand_N_Op_Plus --
9106 ----------------------
9108 procedure Expand_N_Op_Plus
(N
: Node_Id
) is
9110 Unary_Op_Validity_Checks
(N
);
9112 -- Check for MINIMIZED/ELIMINATED overflow mode
9114 if Minimized_Eliminated_Overflow_Check
(N
) then
9115 Apply_Arithmetic_Overflow_Check
(N
);
9118 end Expand_N_Op_Plus
;
9120 ---------------------
9121 -- Expand_N_Op_Rem --
9122 ---------------------
9124 procedure Expand_N_Op_Rem
(N
: Node_Id
) is
9125 Loc
: constant Source_Ptr
:= Sloc
(N
);
9126 Typ
: constant Entity_Id
:= Etype
(N
);
9137 -- Set if corresponding operand can be negative
9139 pragma Unreferenced
(Hi
);
9142 Binary_Op_Validity_Checks
(N
);
9144 -- Check for MINIMIZED/ELIMINATED overflow mode
9146 if Minimized_Eliminated_Overflow_Check
(N
) then
9147 Apply_Arithmetic_Overflow_Check
(N
);
9151 if Is_Integer_Type
(Etype
(N
)) then
9152 Apply_Divide_Checks
(N
);
9154 -- All done if we don't have a REM any more, which can happen as a
9155 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
9157 if Nkind
(N
) /= N_Op_Rem
then
9162 -- Proceed with expansion of REM
9164 Left
:= Left_Opnd
(N
);
9165 Right
:= Right_Opnd
(N
);
9167 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
9168 -- but it is useful with other back ends, and is certainly harmless.
9170 if Is_Integer_Type
(Etype
(N
))
9171 and then Compile_Time_Known_Value
(Right
)
9172 and then Expr_Value
(Right
) = Uint_1
9174 -- Call Remove_Side_Effects to ensure that any side effects in the
9175 -- ignored left operand (in particular function calls to user defined
9176 -- functions) are properly executed.
9178 Remove_Side_Effects
(Left
);
9180 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
9181 Analyze_And_Resolve
(N
, Typ
);
9185 -- Deal with annoying case of largest negative number remainder minus
9186 -- one. Gigi may not handle this case correctly, because on some
9187 -- targets, the mod value is computed using a divide instruction
9188 -- which gives an overflow trap for this case.
9190 -- It would be a bit more efficient to figure out which targets this
9191 -- is really needed for, but in practice it is reasonable to do the
9192 -- following special check in all cases, since it means we get a clearer
9193 -- message, and also the overhead is minimal given that division is
9194 -- expensive in any case.
9196 -- In fact the check is quite easy, if the right operand is -1, then
9197 -- the remainder is always 0, and we can just ignore the left operand
9198 -- completely in this case.
9200 Determine_Range
(Right
, OK
, Lo
, Hi
, Assume_Valid
=> True);
9201 Lneg
:= (not OK
) or else Lo
< 0;
9203 Determine_Range
(Left
, OK
, Lo
, Hi
, Assume_Valid
=> True);
9204 Rneg
:= (not OK
) or else Lo
< 0;
9206 -- We won't mess with trying to find out if the left operand can really
9207 -- be the largest negative number (that's a pain in the case of private
9208 -- types and this is really marginal). We will just assume that we need
9209 -- the test if the left operand can be negative at all.
9211 if Lneg
and Rneg
then
9213 Make_If_Expression
(Loc
,
9214 Expressions
=> New_List
(
9216 Left_Opnd
=> Duplicate_Subexpr
(Right
),
9218 Unchecked_Convert_To
(Typ
, Make_Integer_Literal
(Loc
, -1))),
9220 Unchecked_Convert_To
(Typ
,
9221 Make_Integer_Literal
(Loc
, Uint_0
)),
9223 Relocate_Node
(N
))));
9225 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
9226 Analyze_And_Resolve
(N
, Typ
);
9228 end Expand_N_Op_Rem
;
9230 -----------------------------
9231 -- Expand_N_Op_Rotate_Left --
9232 -----------------------------
9234 procedure Expand_N_Op_Rotate_Left
(N
: Node_Id
) is
9236 Binary_Op_Validity_Checks
(N
);
9238 -- If we are in Modify_Tree_For_C mode, there is no rotate left in C,
9239 -- so we rewrite in terms of logical shifts
9241 -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits)
9243 -- where Bits is the shift count mod Esize (the mod operation here
9244 -- deals with ludicrous large shift counts, which are apparently OK).
9246 -- What about nonbinary modulus ???
9249 Loc
: constant Source_Ptr
:= Sloc
(N
);
9250 Rtp
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
9251 Typ
: constant Entity_Id
:= Etype
(N
);
9254 if Modify_Tree_For_C
then
9255 Rewrite
(Right_Opnd
(N
),
9257 Left_Opnd
=> Relocate_Node
(Right_Opnd
(N
)),
9258 Right_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
))));
9260 Analyze_And_Resolve
(Right_Opnd
(N
), Rtp
);
9265 Make_Op_Shift_Left
(Loc
,
9266 Left_Opnd
=> Left_Opnd
(N
),
9267 Right_Opnd
=> Right_Opnd
(N
)),
9270 Make_Op_Shift_Right
(Loc
,
9271 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left_Opnd
(N
)),
9273 Make_Op_Subtract
(Loc
,
9274 Left_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
)),
9276 Duplicate_Subexpr_No_Checks
(Right_Opnd
(N
))))));
9278 Analyze_And_Resolve
(N
, Typ
);
9281 end Expand_N_Op_Rotate_Left
;
9283 ------------------------------
9284 -- Expand_N_Op_Rotate_Right --
9285 ------------------------------
9287 procedure Expand_N_Op_Rotate_Right
(N
: Node_Id
) is
9289 Binary_Op_Validity_Checks
(N
);
9291 -- If we are in Modify_Tree_For_C mode, there is no rotate right in C,
9292 -- so we rewrite in terms of logical shifts
9294 -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits)
9296 -- where Bits is the shift count mod Esize (the mod operation here
9297 -- deals with ludicrous large shift counts, which are apparently OK).
9299 -- What about nonbinary modulus ???
9302 Loc
: constant Source_Ptr
:= Sloc
(N
);
9303 Rtp
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
9304 Typ
: constant Entity_Id
:= Etype
(N
);
9307 Rewrite
(Right_Opnd
(N
),
9309 Left_Opnd
=> Relocate_Node
(Right_Opnd
(N
)),
9310 Right_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
))));
9312 Analyze_And_Resolve
(Right_Opnd
(N
), Rtp
);
9314 if Modify_Tree_For_C
then
9318 Make_Op_Shift_Right
(Loc
,
9319 Left_Opnd
=> Left_Opnd
(N
),
9320 Right_Opnd
=> Right_Opnd
(N
)),
9323 Make_Op_Shift_Left
(Loc
,
9324 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left_Opnd
(N
)),
9326 Make_Op_Subtract
(Loc
,
9327 Left_Opnd
=> Make_Integer_Literal
(Loc
, Esize
(Typ
)),
9329 Duplicate_Subexpr_No_Checks
(Right_Opnd
(N
))))));
9331 Analyze_And_Resolve
(N
, Typ
);
9334 end Expand_N_Op_Rotate_Right
;
9336 ----------------------------
9337 -- Expand_N_Op_Shift_Left --
9338 ----------------------------
9340 -- Note: nothing in this routine depends on left as opposed to right shifts
9341 -- so we share the routine for expanding shift right operations.
9343 procedure Expand_N_Op_Shift_Left
(N
: Node_Id
) is
9345 Binary_Op_Validity_Checks
(N
);
9347 -- If we are in Modify_Tree_For_C mode, then ensure that the right
9348 -- operand is not greater than the word size (since that would not
9349 -- be defined properly by the corresponding C shift operator).
9351 if Modify_Tree_For_C
then
9353 Right
: constant Node_Id
:= Right_Opnd
(N
);
9354 Loc
: constant Source_Ptr
:= Sloc
(Right
);
9355 Typ
: constant Entity_Id
:= Etype
(N
);
9356 Siz
: constant Uint
:= Esize
(Typ
);
9363 if Compile_Time_Known_Value
(Right
) then
9364 if Expr_Value
(Right
) >= Siz
then
9365 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
9366 Analyze_And_Resolve
(N
, Typ
);
9369 -- Not compile time known, find range
9372 Determine_Range
(Right
, OK
, Lo
, Hi
, Assume_Valid
=> True);
9374 -- Nothing to do if known to be OK range, otherwise expand
9376 if not OK
or else Hi
>= Siz
then
9378 -- Prevent recursion on copy of shift node
9380 Orig
:= Relocate_Node
(N
);
9381 Set_Analyzed
(Orig
);
9383 -- Now do the rewrite
9386 Make_If_Expression
(Loc
,
9387 Expressions
=> New_List
(
9389 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Right
),
9390 Right_Opnd
=> Make_Integer_Literal
(Loc
, Siz
)),
9391 Make_Integer_Literal
(Loc
, 0),
9393 Analyze_And_Resolve
(N
, Typ
);
9398 end Expand_N_Op_Shift_Left
;
9400 -----------------------------
9401 -- Expand_N_Op_Shift_Right --
9402 -----------------------------
9404 procedure Expand_N_Op_Shift_Right
(N
: Node_Id
) is
9406 -- Share shift left circuit
9408 Expand_N_Op_Shift_Left
(N
);
9409 end Expand_N_Op_Shift_Right
;
9411 ----------------------------------------
9412 -- Expand_N_Op_Shift_Right_Arithmetic --
9413 ----------------------------------------
9415 procedure Expand_N_Op_Shift_Right_Arithmetic
(N
: Node_Id
) is
9417 Binary_Op_Validity_Checks
(N
);
9419 -- If we are in Modify_Tree_For_C mode, there is no shift right
9420 -- arithmetic in C, so we rewrite in terms of logical shifts.
9422 -- Shift_Right (Num, Bits) or
9424 -- then not (Shift_Right (Mask, bits))
9427 -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1)
9429 -- Note: in almost all C compilers it would work to just shift a
9430 -- signed integer right, but it's undefined and we cannot rely on it.
9432 -- Note: the above works fine for shift counts greater than or equal
9433 -- to the word size, since in this case (not (Shift_Right (Mask, bits)))
9434 -- generates all 1'bits.
9436 -- What about nonbinary modulus ???
9439 Loc
: constant Source_Ptr
:= Sloc
(N
);
9440 Typ
: constant Entity_Id
:= Etype
(N
);
9441 Sign
: constant Uint
:= 2 ** (Esize
(Typ
) - 1);
9442 Mask
: constant Uint
:= (2 ** Esize
(Typ
)) - 1;
9443 Left
: constant Node_Id
:= Left_Opnd
(N
);
9444 Right
: constant Node_Id
:= Right_Opnd
(N
);
9448 if Modify_Tree_For_C
then
9450 -- Here if not (Shift_Right (Mask, bits)) can be computed at
9451 -- compile time as a single constant.
9453 if Compile_Time_Known_Value
(Right
) then
9455 Val
: constant Uint
:= Expr_Value
(Right
);
9458 if Val
>= Esize
(Typ
) then
9459 Maskx
:= Make_Integer_Literal
(Loc
, Mask
);
9463 Make_Integer_Literal
(Loc
,
9464 Intval
=> Mask
- (Mask
/ (2 ** Expr_Value
(Right
))));
9472 Make_Op_Shift_Right
(Loc
,
9473 Left_Opnd
=> Make_Integer_Literal
(Loc
, Mask
),
9474 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Right
)));
9477 -- Now do the rewrite
9482 Make_Op_Shift_Right
(Loc
,
9484 Right_Opnd
=> Right
),
9486 Make_If_Expression
(Loc
,
9487 Expressions
=> New_List
(
9489 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Left
),
9490 Right_Opnd
=> Make_Integer_Literal
(Loc
, Sign
)),
9492 Make_Integer_Literal
(Loc
, 0)))));
9493 Analyze_And_Resolve
(N
, Typ
);
9496 end Expand_N_Op_Shift_Right_Arithmetic
;
9498 --------------------------
9499 -- Expand_N_Op_Subtract --
9500 --------------------------
9502 procedure Expand_N_Op_Subtract
(N
: Node_Id
) is
9503 Typ
: constant Entity_Id
:= Etype
(N
);
9506 Binary_Op_Validity_Checks
(N
);
9508 -- Check for MINIMIZED/ELIMINATED overflow mode
9510 if Minimized_Eliminated_Overflow_Check
(N
) then
9511 Apply_Arithmetic_Overflow_Check
(N
);
9515 -- N - 0 = N for integer types
9517 if Is_Integer_Type
(Typ
)
9518 and then Compile_Time_Known_Value
(Right_Opnd
(N
))
9519 and then Expr_Value
(Right_Opnd
(N
)) = 0
9521 Rewrite
(N
, Left_Opnd
(N
));
9525 -- Arithmetic overflow checks for signed integer/fixed point types
9527 if Is_Signed_Integer_Type
(Typ
) or else Is_Fixed_Point_Type
(Typ
) then
9528 Apply_Arithmetic_Overflow_Check
(N
);
9531 -- Overflow checks for floating-point if -gnateF mode active
9533 Check_Float_Op_Overflow
(N
);
9534 end Expand_N_Op_Subtract
;
9536 ---------------------
9537 -- Expand_N_Op_Xor --
9538 ---------------------
9540 procedure Expand_N_Op_Xor
(N
: Node_Id
) is
9541 Typ
: constant Entity_Id
:= Etype
(N
);
9544 Binary_Op_Validity_Checks
(N
);
9546 if Is_Array_Type
(Etype
(N
)) then
9547 Expand_Boolean_Operator
(N
);
9549 elsif Is_Boolean_Type
(Etype
(N
)) then
9550 Adjust_Condition
(Left_Opnd
(N
));
9551 Adjust_Condition
(Right_Opnd
(N
));
9552 Set_Etype
(N
, Standard_Boolean
);
9553 Adjust_Result_Type
(N
, Typ
);
9555 elsif Is_Intrinsic_Subprogram
(Entity
(N
)) then
9556 Expand_Intrinsic_Call
(N
, Entity
(N
));
9559 end Expand_N_Op_Xor
;
9561 ----------------------
9562 -- Expand_N_Or_Else --
9563 ----------------------
9565 procedure Expand_N_Or_Else
(N
: Node_Id
)
9566 renames Expand_Short_Circuit_Operator
;
9568 -----------------------------------
9569 -- Expand_N_Qualified_Expression --
9570 -----------------------------------
9572 procedure Expand_N_Qualified_Expression
(N
: Node_Id
) is
9573 Operand
: constant Node_Id
:= Expression
(N
);
9574 Target_Type
: constant Entity_Id
:= Entity
(Subtype_Mark
(N
));
9577 -- Do validity check if validity checking operands
9579 if Validity_Checks_On
and Validity_Check_Operands
then
9580 Ensure_Valid
(Operand
);
9583 -- Apply possible constraint check
9585 Apply_Constraint_Check
(Operand
, Target_Type
, No_Sliding
=> True);
9587 if Do_Range_Check
(Operand
) then
9588 Set_Do_Range_Check
(Operand
, False);
9589 Generate_Range_Check
(Operand
, Target_Type
, CE_Range_Check_Failed
);
9591 end Expand_N_Qualified_Expression
;
9593 ------------------------------------
9594 -- Expand_N_Quantified_Expression --
9595 ------------------------------------
9599 -- for all X in range => Cond
9604 -- for X in range loop
9611 -- Similarly, an existentially quantified expression:
9613 -- for some X in range => Cond
9618 -- for X in range loop
9625 -- In both cases, the iteration may be over a container in which case it is
9626 -- given by an iterator specification, not a loop parameter specification.
9628 procedure Expand_N_Quantified_Expression
(N
: Node_Id
) is
9629 Actions
: constant List_Id
:= New_List
;
9630 For_All
: constant Boolean := All_Present
(N
);
9631 Iter_Spec
: constant Node_Id
:= Iterator_Specification
(N
);
9632 Loc
: constant Source_Ptr
:= Sloc
(N
);
9633 Loop_Spec
: constant Node_Id
:= Loop_Parameter_Specification
(N
);
9640 -- Create the declaration of the flag which tracks the status of the
9641 -- quantified expression. Generate:
9643 -- Flag : Boolean := (True | False);
9645 Flag
:= Make_Temporary
(Loc
, 'T', N
);
9648 Make_Object_Declaration
(Loc
,
9649 Defining_Identifier
=> Flag
,
9650 Object_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
),
9652 New_Occurrence_Of
(Boolean_Literals
(For_All
), Loc
)));
9654 -- Construct the circuitry which tracks the status of the quantified
9655 -- expression. Generate:
9657 -- if [not] Cond then
9658 -- Flag := (False | True);
9662 Cond
:= Relocate_Node
(Condition
(N
));
9665 Cond
:= Make_Op_Not
(Loc
, Cond
);
9669 Make_Implicit_If_Statement
(N
,
9671 Then_Statements
=> New_List
(
9672 Make_Assignment_Statement
(Loc
,
9673 Name
=> New_Occurrence_Of
(Flag
, Loc
),
9675 New_Occurrence_Of
(Boolean_Literals
(not For_All
), Loc
)),
9676 Make_Exit_Statement
(Loc
))));
9678 -- Build the loop equivalent of the quantified expression
9680 if Present
(Iter_Spec
) then
9682 Make_Iteration_Scheme
(Loc
,
9683 Iterator_Specification
=> Iter_Spec
);
9686 Make_Iteration_Scheme
(Loc
,
9687 Loop_Parameter_Specification
=> Loop_Spec
);
9691 Make_Loop_Statement
(Loc
,
9692 Iteration_Scheme
=> Scheme
,
9693 Statements
=> Stmts
,
9694 End_Label
=> Empty
));
9696 -- Transform the quantified expression
9699 Make_Expression_With_Actions
(Loc
,
9700 Expression
=> New_Occurrence_Of
(Flag
, Loc
),
9701 Actions
=> Actions
));
9702 Analyze_And_Resolve
(N
, Standard_Boolean
);
9703 end Expand_N_Quantified_Expression
;
9705 ---------------------------------
9706 -- Expand_N_Selected_Component --
9707 ---------------------------------
9709 procedure Expand_N_Selected_Component
(N
: Node_Id
) is
9710 Loc
: constant Source_Ptr
:= Sloc
(N
);
9711 Par
: constant Node_Id
:= Parent
(N
);
9712 P
: constant Node_Id
:= Prefix
(N
);
9713 S
: constant Node_Id
:= Selector_Name
(N
);
9714 Ptyp
: Entity_Id
:= Underlying_Type
(Etype
(P
));
9720 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean;
9721 -- Gigi needs a temporary for prefixes that depend on a discriminant,
9722 -- unless the context of an assignment can provide size information.
9723 -- Don't we have a general routine that does this???
9725 function Is_Subtype_Declaration
return Boolean;
9726 -- The replacement of a discriminant reference by its value is required
9727 -- if this is part of the initialization of an temporary generated by a
9728 -- change of representation. This shows up as the construction of a
9729 -- discriminant constraint for a subtype declared at the same point as
9730 -- the entity in the prefix of the selected component. We recognize this
9731 -- case when the context of the reference is:
9732 -- subtype ST is T(Obj.D);
9733 -- where the entity for Obj comes from source, and ST has the same sloc.
9735 -----------------------
9736 -- In_Left_Hand_Side --
9737 -----------------------
9739 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean is
9741 return (Nkind
(Parent
(Comp
)) = N_Assignment_Statement
9742 and then Comp
= Name
(Parent
(Comp
)))
9743 or else (Present
(Parent
(Comp
))
9744 and then Nkind
(Parent
(Comp
)) in N_Subexpr
9745 and then In_Left_Hand_Side
(Parent
(Comp
)));
9746 end In_Left_Hand_Side
;
9748 -----------------------------
9749 -- Is_Subtype_Declaration --
9750 -----------------------------
9752 function Is_Subtype_Declaration
return Boolean is
9753 Par
: constant Node_Id
:= Parent
(N
);
9756 Nkind
(Par
) = N_Index_Or_Discriminant_Constraint
9757 and then Nkind
(Parent
(Parent
(Par
))) = N_Subtype_Declaration
9758 and then Comes_From_Source
(Entity
(Prefix
(N
)))
9759 and then Sloc
(Par
) = Sloc
(Entity
(Prefix
(N
)));
9760 end Is_Subtype_Declaration
;
9762 -- Start of processing for Expand_N_Selected_Component
9765 -- Insert explicit dereference if required
9767 if Is_Access_Type
(Ptyp
) then
9769 -- First set prefix type to proper access type, in case it currently
9770 -- has a private (non-access) view of this type.
9772 Set_Etype
(P
, Ptyp
);
9774 Insert_Explicit_Dereference
(P
);
9775 Analyze_And_Resolve
(P
, Designated_Type
(Ptyp
));
9777 if Ekind
(Etype
(P
)) = E_Private_Subtype
9778 and then Is_For_Access_Subtype
(Etype
(P
))
9780 Set_Etype
(P
, Base_Type
(Etype
(P
)));
9786 -- Deal with discriminant check required
9788 if Do_Discriminant_Check
(N
) then
9789 if Present
(Discriminant_Checking_Func
9790 (Original_Record_Component
(Entity
(S
))))
9792 -- Present the discriminant checking function to the backend, so
9793 -- that it can inline the call to the function.
9796 (Discriminant_Checking_Func
9797 (Original_Record_Component
(Entity
(S
))),
9800 -- Now reset the flag and generate the call
9802 Set_Do_Discriminant_Check
(N
, False);
9803 Generate_Discriminant_Check
(N
);
9805 -- In the case of Unchecked_Union, no discriminant checking is
9806 -- actually performed.
9809 Set_Do_Discriminant_Check
(N
, False);
9813 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9814 -- function, then additional actuals must be passed.
9816 if Ada_Version
>= Ada_2005
9817 and then Is_Build_In_Place_Function_Call
(P
)
9819 Make_Build_In_Place_Call_In_Anonymous_Context
(P
);
9822 -- Gigi cannot handle unchecked conversions that are the prefix of a
9823 -- selected component with discriminants. This must be checked during
9824 -- expansion, because during analysis the type of the selector is not
9825 -- known at the point the prefix is analyzed. If the conversion is the
9826 -- target of an assignment, then we cannot force the evaluation.
9828 if Nkind
(Prefix
(N
)) = N_Unchecked_Type_Conversion
9829 and then Has_Discriminants
(Etype
(N
))
9830 and then not In_Left_Hand_Side
(N
)
9832 Force_Evaluation
(Prefix
(N
));
9835 -- Remaining processing applies only if selector is a discriminant
9837 if Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
then
9839 -- If the selector is a discriminant of a constrained record type,
9840 -- we may be able to rewrite the expression with the actual value
9841 -- of the discriminant, a useful optimization in some cases.
9843 if Is_Record_Type
(Ptyp
)
9844 and then Has_Discriminants
(Ptyp
)
9845 and then Is_Constrained
(Ptyp
)
9847 -- Do this optimization for discrete types only, and not for
9848 -- access types (access discriminants get us into trouble).
9850 if not Is_Discrete_Type
(Etype
(N
)) then
9853 -- Don't do this on the left-hand side of an assignment statement.
9854 -- Normally one would think that references like this would not
9855 -- occur, but they do in generated code, and mean that we really
9856 -- do want to assign the discriminant.
9858 elsif Nkind
(Par
) = N_Assignment_Statement
9859 and then Name
(Par
) = N
9863 -- Don't do this optimization for the prefix of an attribute or
9864 -- the name of an object renaming declaration since these are
9865 -- contexts where we do not want the value anyway.
9867 elsif (Nkind
(Par
) = N_Attribute_Reference
9868 and then Prefix
(Par
) = N
)
9869 or else Is_Renamed_Object
(N
)
9873 -- Don't do this optimization if we are within the code for a
9874 -- discriminant check, since the whole point of such a check may
9875 -- be to verify the condition on which the code below depends.
9877 elsif Is_In_Discriminant_Check
(N
) then
9880 -- Green light to see if we can do the optimization. There is
9881 -- still one condition that inhibits the optimization below but
9882 -- now is the time to check the particular discriminant.
9885 -- Loop through discriminants to find the matching discriminant
9886 -- constraint to see if we can copy it.
9888 Disc
:= First_Discriminant
(Ptyp
);
9889 Dcon
:= First_Elmt
(Discriminant_Constraint
(Ptyp
));
9890 Discr_Loop
: while Present
(Dcon
) loop
9891 Dval
:= Node
(Dcon
);
9893 -- Check if this is the matching discriminant and if the
9894 -- discriminant value is simple enough to make sense to
9895 -- copy. We don't want to copy complex expressions, and
9896 -- indeed to do so can cause trouble (before we put in
9897 -- this guard, a discriminant expression containing an
9898 -- AND THEN was copied, causing problems for coverage
9901 -- However, if the reference is part of the initialization
9902 -- code generated for an object declaration, we must use
9903 -- the discriminant value from the subtype constraint,
9904 -- because the selected component may be a reference to the
9905 -- object being initialized, whose discriminant is not yet
9906 -- set. This only happens in complex cases involving changes
9907 -- or representation.
9909 if Disc
= Entity
(Selector_Name
(N
))
9910 and then (Is_Entity_Name
(Dval
)
9911 or else Compile_Time_Known_Value
(Dval
)
9912 or else Is_Subtype_Declaration
)
9914 -- Here we have the matching discriminant. Check for
9915 -- the case of a discriminant of a component that is
9916 -- constrained by an outer discriminant, which cannot
9917 -- be optimized away.
9919 if Denotes_Discriminant
9920 (Dval
, Check_Concurrent
=> True)
9924 elsif Nkind
(Original_Node
(Dval
)) = N_Selected_Component
9926 Denotes_Discriminant
9927 (Selector_Name
(Original_Node
(Dval
)), True)
9931 -- Do not retrieve value if constraint is not static. It
9932 -- is generally not useful, and the constraint may be a
9933 -- rewritten outer discriminant in which case it is in
9936 elsif Is_Entity_Name
(Dval
)
9938 Nkind
(Parent
(Entity
(Dval
))) = N_Object_Declaration
9939 and then Present
(Expression
(Parent
(Entity
(Dval
))))
9941 Is_OK_Static_Expression
9942 (Expression
(Parent
(Entity
(Dval
))))
9946 -- In the context of a case statement, the expression may
9947 -- have the base type of the discriminant, and we need to
9948 -- preserve the constraint to avoid spurious errors on
9951 elsif Nkind
(Parent
(N
)) = N_Case_Statement
9952 and then Etype
(Dval
) /= Etype
(Disc
)
9955 Make_Qualified_Expression
(Loc
,
9957 New_Occurrence_Of
(Etype
(Disc
), Loc
),
9959 New_Copy_Tree
(Dval
)));
9960 Analyze_And_Resolve
(N
, Etype
(Disc
));
9962 -- In case that comes out as a static expression,
9963 -- reset it (a selected component is never static).
9965 Set_Is_Static_Expression
(N
, False);
9968 -- Otherwise we can just copy the constraint, but the
9969 -- result is certainly not static. In some cases the
9970 -- discriminant constraint has been analyzed in the
9971 -- context of the original subtype indication, but for
9972 -- itypes the constraint might not have been analyzed
9973 -- yet, and this must be done now.
9976 Rewrite
(N
, New_Copy_Tree
(Dval
));
9977 Analyze_And_Resolve
(N
);
9978 Set_Is_Static_Expression
(N
, False);
9984 Next_Discriminant
(Disc
);
9985 end loop Discr_Loop
;
9987 -- Note: the above loop should always find a matching
9988 -- discriminant, but if it does not, we just missed an
9989 -- optimization due to some glitch (perhaps a previous
9990 -- error), so ignore.
9995 -- The only remaining processing is in the case of a discriminant of
9996 -- a concurrent object, where we rewrite the prefix to denote the
9997 -- corresponding record type. If the type is derived and has renamed
9998 -- discriminants, use corresponding discriminant, which is the one
9999 -- that appears in the corresponding record.
10001 if not Is_Concurrent_Type
(Ptyp
) then
10005 Disc
:= Entity
(Selector_Name
(N
));
10007 if Is_Derived_Type
(Ptyp
)
10008 and then Present
(Corresponding_Discriminant
(Disc
))
10010 Disc
:= Corresponding_Discriminant
(Disc
);
10014 Make_Selected_Component
(Loc
,
10016 Unchecked_Convert_To
(Corresponding_Record_Type
(Ptyp
),
10017 New_Copy_Tree
(P
)),
10018 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Disc
)));
10020 Rewrite
(N
, New_N
);
10024 -- Set Atomic_Sync_Required if necessary for atomic component
10026 if Nkind
(N
) = N_Selected_Component
then
10028 E
: constant Entity_Id
:= Entity
(Selector_Name
(N
));
10032 -- If component is atomic, but type is not, setting depends on
10033 -- disable/enable state for the component.
10035 if Is_Atomic
(E
) and then not Is_Atomic
(Etype
(E
)) then
10036 Set
:= not Atomic_Synchronization_Disabled
(E
);
10038 -- If component is not atomic, but its type is atomic, setting
10039 -- depends on disable/enable state for the type.
10041 elsif not Is_Atomic
(E
) and then Is_Atomic
(Etype
(E
)) then
10042 Set
:= not Atomic_Synchronization_Disabled
(Etype
(E
));
10044 -- If both component and type are atomic, we disable if either
10045 -- component or its type have sync disabled.
10047 elsif Is_Atomic
(E
) and then Is_Atomic
(Etype
(E
)) then
10048 Set
:= (not Atomic_Synchronization_Disabled
(E
))
10050 (not Atomic_Synchronization_Disabled
(Etype
(E
)));
10056 -- Set flag if required
10059 Activate_Atomic_Synchronization
(N
);
10063 end Expand_N_Selected_Component
;
10065 --------------------
10066 -- Expand_N_Slice --
10067 --------------------
10069 procedure Expand_N_Slice
(N
: Node_Id
) is
10070 Loc
: constant Source_Ptr
:= Sloc
(N
);
10071 Typ
: constant Entity_Id
:= Etype
(N
);
10073 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean;
10074 -- Check whether the argument is an actual for a procedure call, in
10075 -- which case the expansion of a bit-packed slice is deferred until the
10076 -- call itself is expanded. The reason this is required is that we might
10077 -- have an IN OUT or OUT parameter, and the copy out is essential, and
10078 -- that copy out would be missed if we created a temporary here in
10079 -- Expand_N_Slice. Note that we don't bother to test specifically for an
10080 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
10081 -- is harmless to defer expansion in the IN case, since the call
10082 -- processing will still generate the appropriate copy in operation,
10083 -- which will take care of the slice.
10085 procedure Make_Temporary_For_Slice
;
10086 -- Create a named variable for the value of the slice, in cases where
10087 -- the back-end cannot handle it properly, e.g. when packed types or
10088 -- unaligned slices are involved.
10090 -------------------------
10091 -- Is_Procedure_Actual --
10092 -------------------------
10094 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean is
10095 Par
: Node_Id
:= Parent
(N
);
10099 -- If our parent is a procedure call we can return
10101 if Nkind
(Par
) = N_Procedure_Call_Statement
then
10104 -- If our parent is a type conversion, keep climbing the tree,
10105 -- since a type conversion can be a procedure actual. Also keep
10106 -- climbing if parameter association or a qualified expression,
10107 -- since these are additional cases that do can appear on
10108 -- procedure actuals.
10110 elsif Nkind_In
(Par
, N_Type_Conversion
,
10111 N_Parameter_Association
,
10112 N_Qualified_Expression
)
10114 Par
:= Parent
(Par
);
10116 -- Any other case is not what we are looking for
10122 end Is_Procedure_Actual
;
10124 ------------------------------
10125 -- Make_Temporary_For_Slice --
10126 ------------------------------
10128 procedure Make_Temporary_For_Slice
is
10129 Ent
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', N
);
10134 Make_Object_Declaration
(Loc
,
10135 Defining_Identifier
=> Ent
,
10136 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
10138 Set_No_Initialization
(Decl
);
10140 Insert_Actions
(N
, New_List
(
10142 Make_Assignment_Statement
(Loc
,
10143 Name
=> New_Occurrence_Of
(Ent
, Loc
),
10144 Expression
=> Relocate_Node
(N
))));
10146 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
10147 Analyze_And_Resolve
(N
, Typ
);
10148 end Make_Temporary_For_Slice
;
10152 Pref
: constant Node_Id
:= Prefix
(N
);
10153 Pref_Typ
: Entity_Id
:= Etype
(Pref
);
10155 -- Start of processing for Expand_N_Slice
10158 -- Special handling for access types
10160 if Is_Access_Type
(Pref_Typ
) then
10161 Pref_Typ
:= Designated_Type
(Pref_Typ
);
10164 Make_Explicit_Dereference
(Sloc
(N
),
10165 Prefix
=> Relocate_Node
(Pref
)));
10167 Analyze_And_Resolve
(Pref
, Pref_Typ
);
10170 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
10171 -- function, then additional actuals must be passed.
10173 if Ada_Version
>= Ada_2005
10174 and then Is_Build_In_Place_Function_Call
(Pref
)
10176 Make_Build_In_Place_Call_In_Anonymous_Context
(Pref
);
10179 -- The remaining case to be handled is packed slices. We can leave
10180 -- packed slices as they are in the following situations:
10182 -- 1. Right or left side of an assignment (we can handle this
10183 -- situation correctly in the assignment statement expansion).
10185 -- 2. Prefix of indexed component (the slide is optimized away in this
10186 -- case, see the start of Expand_N_Slice.)
10188 -- 3. Object renaming declaration, since we want the name of the
10189 -- slice, not the value.
10191 -- 4. Argument to procedure call, since copy-in/copy-out handling may
10192 -- be required, and this is handled in the expansion of call
10195 -- 5. Prefix of an address attribute (this is an error which is caught
10196 -- elsewhere, and the expansion would interfere with generating the
10199 if not Is_Packed
(Typ
) then
10201 -- Apply transformation for actuals of a function call, where
10202 -- Expand_Actuals is not used.
10204 if Nkind
(Parent
(N
)) = N_Function_Call
10205 and then Is_Possibly_Unaligned_Slice
(N
)
10207 Make_Temporary_For_Slice
;
10210 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
10211 or else (Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
10212 and then Parent
(N
) = Name
(Parent
(Parent
(N
))))
10216 elsif Nkind
(Parent
(N
)) = N_Indexed_Component
10217 or else Is_Renamed_Object
(N
)
10218 or else Is_Procedure_Actual
(N
)
10222 elsif Nkind
(Parent
(N
)) = N_Attribute_Reference
10223 and then Attribute_Name
(Parent
(N
)) = Name_Address
10228 Make_Temporary_For_Slice
;
10230 end Expand_N_Slice
;
10232 ------------------------------
10233 -- Expand_N_Type_Conversion --
10234 ------------------------------
10236 procedure Expand_N_Type_Conversion
(N
: Node_Id
) is
10237 Loc
: constant Source_Ptr
:= Sloc
(N
);
10238 Operand
: constant Node_Id
:= Expression
(N
);
10239 Target_Type
: constant Entity_Id
:= Etype
(N
);
10240 Operand_Type
: Entity_Id
:= Etype
(Operand
);
10242 procedure Handle_Changed_Representation
;
10243 -- This is called in the case of record and array type conversions to
10244 -- see if there is a change of representation to be handled. Change of
10245 -- representation is actually handled at the assignment statement level,
10246 -- and what this procedure does is rewrite node N conversion as an
10247 -- assignment to temporary. If there is no change of representation,
10248 -- then the conversion node is unchanged.
10250 procedure Raise_Accessibility_Error
;
10251 -- Called when we know that an accessibility check will fail. Rewrites
10252 -- node N to an appropriate raise statement and outputs warning msgs.
10253 -- The Etype of the raise node is set to Target_Type. Note that in this
10254 -- case the rest of the processing should be skipped (i.e. the call to
10255 -- this procedure will be followed by "goto Done").
10257 procedure Real_Range_Check
;
10258 -- Handles generation of range check for real target value
10260 function Has_Extra_Accessibility
(Id
: Entity_Id
) return Boolean;
10261 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
10262 -- evaluates to True.
10264 -----------------------------------
10265 -- Handle_Changed_Representation --
10266 -----------------------------------
10268 procedure Handle_Changed_Representation
is
10277 -- Nothing else to do if no change of representation
10279 if Same_Representation
(Operand_Type
, Target_Type
) then
10282 -- The real change of representation work is done by the assignment
10283 -- statement processing. So if this type conversion is appearing as
10284 -- the expression of an assignment statement, nothing needs to be
10285 -- done to the conversion.
10287 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
then
10290 -- Otherwise we need to generate a temporary variable, and do the
10291 -- change of representation assignment into that temporary variable.
10292 -- The conversion is then replaced by a reference to this variable.
10297 -- If type is unconstrained we have to add a constraint, copied
10298 -- from the actual value of the left-hand side.
10300 if not Is_Constrained
(Target_Type
) then
10301 if Has_Discriminants
(Operand_Type
) then
10302 Disc
:= First_Discriminant
(Operand_Type
);
10304 if Disc
/= First_Stored_Discriminant
(Operand_Type
) then
10305 Disc
:= First_Stored_Discriminant
(Operand_Type
);
10309 while Present
(Disc
) loop
10311 Make_Selected_Component
(Loc
,
10313 Duplicate_Subexpr_Move_Checks
(Operand
),
10315 Make_Identifier
(Loc
, Chars
(Disc
))));
10316 Next_Discriminant
(Disc
);
10319 elsif Is_Array_Type
(Operand_Type
) then
10320 N_Ix
:= First_Index
(Target_Type
);
10323 for J
in 1 .. Number_Dimensions
(Operand_Type
) loop
10325 -- We convert the bounds explicitly. We use an unchecked
10326 -- conversion because bounds checks are done elsewhere.
10331 Unchecked_Convert_To
(Etype
(N_Ix
),
10332 Make_Attribute_Reference
(Loc
,
10334 Duplicate_Subexpr_No_Checks
10335 (Operand
, Name_Req
=> True),
10336 Attribute_Name
=> Name_First
,
10337 Expressions
=> New_List
(
10338 Make_Integer_Literal
(Loc
, J
)))),
10341 Unchecked_Convert_To
(Etype
(N_Ix
),
10342 Make_Attribute_Reference
(Loc
,
10344 Duplicate_Subexpr_No_Checks
10345 (Operand
, Name_Req
=> True),
10346 Attribute_Name
=> Name_Last
,
10347 Expressions
=> New_List
(
10348 Make_Integer_Literal
(Loc
, J
))))));
10355 Odef
:= New_Occurrence_Of
(Target_Type
, Loc
);
10357 if Present
(Cons
) then
10359 Make_Subtype_Indication
(Loc
,
10360 Subtype_Mark
=> Odef
,
10362 Make_Index_Or_Discriminant_Constraint
(Loc
,
10363 Constraints
=> Cons
));
10366 Temp
:= Make_Temporary
(Loc
, 'C');
10368 Make_Object_Declaration
(Loc
,
10369 Defining_Identifier
=> Temp
,
10370 Object_Definition
=> Odef
);
10372 Set_No_Initialization
(Decl
, True);
10374 -- Insert required actions. It is essential to suppress checks
10375 -- since we have suppressed default initialization, which means
10376 -- that the variable we create may have no discriminants.
10381 Make_Assignment_Statement
(Loc
,
10382 Name
=> New_Occurrence_Of
(Temp
, Loc
),
10383 Expression
=> Relocate_Node
(N
))),
10384 Suppress
=> All_Checks
);
10386 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
10389 end Handle_Changed_Representation
;
10391 -------------------------------
10392 -- Raise_Accessibility_Error --
10393 -------------------------------
10395 procedure Raise_Accessibility_Error
is
10397 Error_Msg_Warn
:= SPARK_Mode
/= On
;
10399 Make_Raise_Program_Error
(Sloc
(N
),
10400 Reason
=> PE_Accessibility_Check_Failed
));
10401 Set_Etype
(N
, Target_Type
);
10403 Error_Msg_N
("<<accessibility check failure", N
);
10404 Error_Msg_NE
("\<<& [", N
, Standard_Program_Error
);
10405 end Raise_Accessibility_Error
;
10407 ----------------------
10408 -- Real_Range_Check --
10409 ----------------------
10411 -- Case of conversions to floating-point or fixed-point. If range checks
10412 -- are enabled and the target type has a range constraint, we convert:
10418 -- Tnn : typ'Base := typ'Base (x);
10419 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
10422 -- This is necessary when there is a conversion of integer to float or
10423 -- to fixed-point to ensure that the correct checks are made. It is not
10424 -- necessary for float to float where it is enough to simply set the
10425 -- Do_Range_Check flag.
10427 procedure Real_Range_Check
is
10428 Btyp
: constant Entity_Id
:= Base_Type
(Target_Type
);
10429 Lo
: constant Node_Id
:= Type_Low_Bound
(Target_Type
);
10430 Hi
: constant Node_Id
:= Type_High_Bound
(Target_Type
);
10431 Xtyp
: constant Entity_Id
:= Etype
(Operand
);
10436 -- Nothing to do if conversion was rewritten
10438 if Nkind
(N
) /= N_Type_Conversion
then
10442 -- Nothing to do if range checks suppressed, or target has the same
10443 -- range as the base type (or is the base type).
10445 if Range_Checks_Suppressed
(Target_Type
)
10446 or else (Lo
= Type_Low_Bound
(Btyp
)
10448 Hi
= Type_High_Bound
(Btyp
))
10453 -- Nothing to do if expression is an entity on which checks have been
10456 if Is_Entity_Name
(Operand
)
10457 and then Range_Checks_Suppressed
(Entity
(Operand
))
10462 -- Nothing to do if bounds are all static and we can tell that the
10463 -- expression is within the bounds of the target. Note that if the
10464 -- operand is of an unconstrained floating-point type, then we do
10465 -- not trust it to be in range (might be infinite)
10468 S_Lo
: constant Node_Id
:= Type_Low_Bound
(Xtyp
);
10469 S_Hi
: constant Node_Id
:= Type_High_Bound
(Xtyp
);
10472 if (not Is_Floating_Point_Type
(Xtyp
)
10473 or else Is_Constrained
(Xtyp
))
10474 and then Compile_Time_Known_Value
(S_Lo
)
10475 and then Compile_Time_Known_Value
(S_Hi
)
10476 and then Compile_Time_Known_Value
(Hi
)
10477 and then Compile_Time_Known_Value
(Lo
)
10480 D_Lov
: constant Ureal
:= Expr_Value_R
(Lo
);
10481 D_Hiv
: constant Ureal
:= Expr_Value_R
(Hi
);
10486 if Is_Real_Type
(Xtyp
) then
10487 S_Lov
:= Expr_Value_R
(S_Lo
);
10488 S_Hiv
:= Expr_Value_R
(S_Hi
);
10490 S_Lov
:= UR_From_Uint
(Expr_Value
(S_Lo
));
10491 S_Hiv
:= UR_From_Uint
(Expr_Value
(S_Hi
));
10495 and then S_Lov
>= D_Lov
10496 and then S_Hiv
<= D_Hiv
10498 -- Unset the range check flag on the current value of
10499 -- Expression (N), since the captured Operand may have
10500 -- been rewritten (such as for the case of a conversion
10501 -- to a fixed-point type).
10503 Set_Do_Range_Check
(Expression
(N
), False);
10511 -- For float to float conversions, we are done
10513 if Is_Floating_Point_Type
(Xtyp
)
10515 Is_Floating_Point_Type
(Btyp
)
10520 -- Otherwise rewrite the conversion as described above
10522 Conv
:= Relocate_Node
(N
);
10523 Rewrite
(Subtype_Mark
(Conv
), New_Occurrence_Of
(Btyp
, Loc
));
10524 Set_Etype
(Conv
, Btyp
);
10526 -- Enable overflow except for case of integer to float conversions,
10527 -- where it is never required, since we can never have overflow in
10530 if not Is_Integer_Type
(Etype
(Operand
)) then
10531 Enable_Overflow_Check
(Conv
);
10534 Tnn
:= Make_Temporary
(Loc
, 'T', Conv
);
10536 Insert_Actions
(N
, New_List
(
10537 Make_Object_Declaration
(Loc
,
10538 Defining_Identifier
=> Tnn
,
10539 Object_Definition
=> New_Occurrence_Of
(Btyp
, Loc
),
10540 Constant_Present
=> True,
10541 Expression
=> Conv
),
10543 Make_Raise_Constraint_Error
(Loc
,
10548 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
10550 Make_Attribute_Reference
(Loc
,
10551 Attribute_Name
=> Name_First
,
10553 New_Occurrence_Of
(Target_Type
, Loc
))),
10557 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
10559 Make_Attribute_Reference
(Loc
,
10560 Attribute_Name
=> Name_Last
,
10562 New_Occurrence_Of
(Target_Type
, Loc
)))),
10563 Reason
=> CE_Range_Check_Failed
)));
10565 Rewrite
(N
, New_Occurrence_Of
(Tnn
, Loc
));
10566 Analyze_And_Resolve
(N
, Btyp
);
10567 end Real_Range_Check
;
10569 -----------------------------
10570 -- Has_Extra_Accessibility --
10571 -----------------------------
10573 -- Returns true for a formal of an anonymous access type or for
10574 -- an Ada 2012-style stand-alone object of an anonymous access type.
10576 function Has_Extra_Accessibility
(Id
: Entity_Id
) return Boolean is
10578 if Is_Formal
(Id
) or else Ekind_In
(Id
, E_Constant
, E_Variable
) then
10579 return Present
(Effective_Extra_Accessibility
(Id
));
10583 end Has_Extra_Accessibility
;
10585 -- Start of processing for Expand_N_Type_Conversion
10588 -- First remove check marks put by the semantic analysis on the type
10589 -- conversion between array types. We need these checks, and they will
10590 -- be generated by this expansion routine, but we do not depend on these
10591 -- flags being set, and since we do intend to expand the checks in the
10592 -- front end, we don't want them on the tree passed to the back end.
10594 if Is_Array_Type
(Target_Type
) then
10595 if Is_Constrained
(Target_Type
) then
10596 Set_Do_Length_Check
(N
, False);
10598 Set_Do_Range_Check
(Operand
, False);
10602 -- Nothing at all to do if conversion is to the identical type so remove
10603 -- the conversion completely, it is useless, except that it may carry
10604 -- an Assignment_OK attribute, which must be propagated to the operand.
10606 if Operand_Type
= Target_Type
then
10607 if Assignment_OK
(N
) then
10608 Set_Assignment_OK
(Operand
);
10611 Rewrite
(N
, Relocate_Node
(Operand
));
10615 -- Nothing to do if this is the second argument of read. This is a
10616 -- "backwards" conversion that will be handled by the specialized code
10617 -- in attribute processing.
10619 if Nkind
(Parent
(N
)) = N_Attribute_Reference
10620 and then Attribute_Name
(Parent
(N
)) = Name_Read
10621 and then Next
(First
(Expressions
(Parent
(N
)))) = N
10626 -- Check for case of converting to a type that has an invariant
10627 -- associated with it. This requires an invariant check. We insert
10630 -- invariant_check (typ (expr))
10632 -- in the code, after removing side effects from the expression.
10633 -- This is clearer than replacing the conversion into an expression
10634 -- with actions, because the context may impose additional actions
10635 -- (tag checks, membership tests, etc.) that conflict with this
10636 -- rewriting (used previously).
10638 -- Note: the Comes_From_Source check, and then the resetting of this
10639 -- flag prevents what would otherwise be an infinite recursion.
10641 if Has_Invariants
(Target_Type
)
10642 and then Present
(Invariant_Procedure
(Target_Type
))
10643 and then Comes_From_Source
(N
)
10645 Set_Comes_From_Source
(N
, False);
10646 Remove_Side_Effects
(N
);
10647 Insert_Action
(N
, Make_Invariant_Call
(Duplicate_Subexpr
(N
)));
10651 -- Here if we may need to expand conversion
10653 -- If the operand of the type conversion is an arithmetic operation on
10654 -- signed integers, and the based type of the signed integer type in
10655 -- question is smaller than Standard.Integer, we promote both of the
10656 -- operands to type Integer.
10658 -- For example, if we have
10660 -- target-type (opnd1 + opnd2)
10662 -- and opnd1 and opnd2 are of type short integer, then we rewrite
10665 -- target-type (integer(opnd1) + integer(opnd2))
10667 -- We do this because we are always allowed to compute in a larger type
10668 -- if we do the right thing with the result, and in this case we are
10669 -- going to do a conversion which will do an appropriate check to make
10670 -- sure that things are in range of the target type in any case. This
10671 -- avoids some unnecessary intermediate overflows.
10673 -- We might consider a similar transformation in the case where the
10674 -- target is a real type or a 64-bit integer type, and the operand
10675 -- is an arithmetic operation using a 32-bit integer type. However,
10676 -- we do not bother with this case, because it could cause significant
10677 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
10678 -- much cheaper, but we don't want different behavior on 32-bit and
10679 -- 64-bit machines. Note that the exclusion of the 64-bit case also
10680 -- handles the configurable run-time cases where 64-bit arithmetic
10681 -- may simply be unavailable.
10683 -- Note: this circuit is partially redundant with respect to the circuit
10684 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
10685 -- the processing here. Also we still need the Checks circuit, since we
10686 -- have to be sure not to generate junk overflow checks in the first
10687 -- place, since it would be trick to remove them here.
10689 if Integer_Promotion_Possible
(N
) then
10691 -- All conditions met, go ahead with transformation
10699 Make_Type_Conversion
(Loc
,
10700 Subtype_Mark
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
10701 Expression
=> Relocate_Node
(Right_Opnd
(Operand
)));
10703 Opnd
:= New_Op_Node
(Nkind
(Operand
), Loc
);
10704 Set_Right_Opnd
(Opnd
, R
);
10706 if Nkind
(Operand
) in N_Binary_Op
then
10708 Make_Type_Conversion
(Loc
,
10709 Subtype_Mark
=> New_Occurrence_Of
(Standard_Integer
, Loc
),
10710 Expression
=> Relocate_Node
(Left_Opnd
(Operand
)));
10712 Set_Left_Opnd
(Opnd
, L
);
10716 Make_Type_Conversion
(Loc
,
10717 Subtype_Mark
=> Relocate_Node
(Subtype_Mark
(N
)),
10718 Expression
=> Opnd
));
10720 Analyze_And_Resolve
(N
, Target_Type
);
10725 -- Do validity check if validity checking operands
10727 if Validity_Checks_On
and Validity_Check_Operands
then
10728 Ensure_Valid
(Operand
);
10731 -- Special case of converting from non-standard boolean type
10733 if Is_Boolean_Type
(Operand_Type
)
10734 and then (Nonzero_Is_True
(Operand_Type
))
10736 Adjust_Condition
(Operand
);
10737 Set_Etype
(Operand
, Standard_Boolean
);
10738 Operand_Type
:= Standard_Boolean
;
10741 -- Case of converting to an access type
10743 if Is_Access_Type
(Target_Type
) then
10745 -- Apply an accessibility check when the conversion operand is an
10746 -- access parameter (or a renaming thereof), unless conversion was
10747 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
10748 -- Note that other checks may still need to be applied below (such
10749 -- as tagged type checks).
10751 if Is_Entity_Name
(Operand
)
10752 and then Has_Extra_Accessibility
(Entity
(Operand
))
10753 and then Ekind
(Etype
(Operand
)) = E_Anonymous_Access_Type
10754 and then (Nkind
(Original_Node
(N
)) /= N_Attribute_Reference
10755 or else Attribute_Name
(Original_Node
(N
)) = Name_Access
)
10757 Apply_Accessibility_Check
10758 (Operand
, Target_Type
, Insert_Node
=> Operand
);
10760 -- If the level of the operand type is statically deeper than the
10761 -- level of the target type, then force Program_Error. Note that this
10762 -- can only occur for cases where the attribute is within the body of
10763 -- an instantiation, otherwise the conversion will already have been
10764 -- rejected as illegal.
10766 -- Note: warnings are issued by the analyzer for the instance cases
10768 elsif In_Instance_Body
10770 -- The case where the target type is an anonymous access type of
10771 -- a discriminant is excluded, because the level of such a type
10772 -- depends on the context and currently the level returned for such
10773 -- types is zero, resulting in warnings about about check failures
10774 -- in certain legal cases involving class-wide interfaces as the
10775 -- designated type (some cases, such as return statements, are
10776 -- checked at run time, but not clear if these are handled right
10777 -- in general, see 3.10.2(12/2-12.5/3) ???).
10780 not (Ekind
(Target_Type
) = E_Anonymous_Access_Type
10781 and then Present
(Associated_Node_For_Itype
(Target_Type
))
10782 and then Nkind
(Associated_Node_For_Itype
(Target_Type
)) =
10783 N_Discriminant_Specification
)
10785 Type_Access_Level
(Operand_Type
) > Type_Access_Level
(Target_Type
)
10787 Raise_Accessibility_Error
;
10790 -- When the operand is a selected access discriminant the check needs
10791 -- to be made against the level of the object denoted by the prefix
10792 -- of the selected name. Force Program_Error for this case as well
10793 -- (this accessibility violation can only happen if within the body
10794 -- of an instantiation).
10796 elsif In_Instance_Body
10797 and then Ekind
(Operand_Type
) = E_Anonymous_Access_Type
10798 and then Nkind
(Operand
) = N_Selected_Component
10799 and then Object_Access_Level
(Operand
) >
10800 Type_Access_Level
(Target_Type
)
10802 Raise_Accessibility_Error
;
10807 -- Case of conversions of tagged types and access to tagged types
10809 -- When needed, that is to say when the expression is class-wide, Add
10810 -- runtime a tag check for (strict) downward conversion by using the
10811 -- membership test, generating:
10813 -- [constraint_error when Operand not in Target_Type'Class]
10815 -- or in the access type case
10817 -- [constraint_error
10818 -- when Operand /= null
10819 -- and then Operand.all not in
10820 -- Designated_Type (Target_Type)'Class]
10822 if (Is_Access_Type
(Target_Type
)
10823 and then Is_Tagged_Type
(Designated_Type
(Target_Type
)))
10824 or else Is_Tagged_Type
(Target_Type
)
10826 -- Do not do any expansion in the access type case if the parent is a
10827 -- renaming, since this is an error situation which will be caught by
10828 -- Sem_Ch8, and the expansion can interfere with this error check.
10830 if Is_Access_Type
(Target_Type
) and then Is_Renamed_Object
(N
) then
10834 -- Otherwise, proceed with processing tagged conversion
10836 Tagged_Conversion
: declare
10837 Actual_Op_Typ
: Entity_Id
;
10838 Actual_Targ_Typ
: Entity_Id
;
10839 Make_Conversion
: Boolean := False;
10840 Root_Op_Typ
: Entity_Id
;
10842 procedure Make_Tag_Check
(Targ_Typ
: Entity_Id
);
10843 -- Create a membership check to test whether Operand is a member
10844 -- of Targ_Typ. If the original Target_Type is an access, include
10845 -- a test for null value. The check is inserted at N.
10847 --------------------
10848 -- Make_Tag_Check --
10849 --------------------
10851 procedure Make_Tag_Check
(Targ_Typ
: Entity_Id
) is
10856 -- [Constraint_Error
10857 -- when Operand /= null
10858 -- and then Operand.all not in Targ_Typ]
10860 if Is_Access_Type
(Target_Type
) then
10862 Make_And_Then
(Loc
,
10865 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
10866 Right_Opnd
=> Make_Null
(Loc
)),
10871 Make_Explicit_Dereference
(Loc
,
10872 Prefix
=> Duplicate_Subexpr_No_Checks
(Operand
)),
10873 Right_Opnd
=> New_Occurrence_Of
(Targ_Typ
, Loc
)));
10876 -- [Constraint_Error when Operand not in Targ_Typ]
10881 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
10882 Right_Opnd
=> New_Occurrence_Of
(Targ_Typ
, Loc
));
10886 Make_Raise_Constraint_Error
(Loc
,
10888 Reason
=> CE_Tag_Check_Failed
));
10889 end Make_Tag_Check
;
10891 -- Start of processing for Tagged_Conversion
10894 -- Handle entities from the limited view
10896 if Is_Access_Type
(Operand_Type
) then
10898 Available_View
(Designated_Type
(Operand_Type
));
10900 Actual_Op_Typ
:= Operand_Type
;
10903 if Is_Access_Type
(Target_Type
) then
10905 Available_View
(Designated_Type
(Target_Type
));
10907 Actual_Targ_Typ
:= Target_Type
;
10910 Root_Op_Typ
:= Root_Type
(Actual_Op_Typ
);
10912 -- Ada 2005 (AI-251): Handle interface type conversion
10914 if Is_Interface
(Actual_Op_Typ
)
10916 Is_Interface
(Actual_Targ_Typ
)
10918 Expand_Interface_Conversion
(N
);
10922 if not Tag_Checks_Suppressed
(Actual_Targ_Typ
) then
10924 -- Create a runtime tag check for a downward class-wide type
10927 if Is_Class_Wide_Type
(Actual_Op_Typ
)
10928 and then Actual_Op_Typ
/= Actual_Targ_Typ
10929 and then Root_Op_Typ
/= Actual_Targ_Typ
10930 and then Is_Ancestor
(Root_Op_Typ
, Actual_Targ_Typ
,
10931 Use_Full_View
=> True)
10933 Make_Tag_Check
(Class_Wide_Type
(Actual_Targ_Typ
));
10934 Make_Conversion
:= True;
10937 -- AI05-0073: If the result subtype of the function is defined
10938 -- by an access_definition designating a specific tagged type
10939 -- T, a check is made that the result value is null or the tag
10940 -- of the object designated by the result value identifies T.
10941 -- Constraint_Error is raised if this check fails.
10943 if Nkind
(Parent
(N
)) = N_Simple_Return_Statement
then
10946 Func_Typ
: Entity_Id
;
10949 -- Climb scope stack looking for the enclosing function
10951 Func
:= Current_Scope
;
10952 while Present
(Func
)
10953 and then Ekind
(Func
) /= E_Function
10955 Func
:= Scope
(Func
);
10958 -- The function's return subtype must be defined using
10959 -- an access definition.
10961 if Nkind
(Result_Definition
(Parent
(Func
))) =
10962 N_Access_Definition
10964 Func_Typ
:= Directly_Designated_Type
(Etype
(Func
));
10966 -- The return subtype denotes a specific tagged type,
10967 -- in other words, a non class-wide type.
10969 if Is_Tagged_Type
(Func_Typ
)
10970 and then not Is_Class_Wide_Type
(Func_Typ
)
10972 Make_Tag_Check
(Actual_Targ_Typ
);
10973 Make_Conversion
:= True;
10979 -- We have generated a tag check for either a class-wide type
10980 -- conversion or for AI05-0073.
10982 if Make_Conversion
then
10987 Make_Unchecked_Type_Conversion
(Loc
,
10988 Subtype_Mark
=> New_Occurrence_Of
(Target_Type
, Loc
),
10989 Expression
=> Relocate_Node
(Expression
(N
)));
10991 Analyze_And_Resolve
(N
, Target_Type
);
10995 end Tagged_Conversion
;
10997 -- Case of other access type conversions
10999 elsif Is_Access_Type
(Target_Type
) then
11000 Apply_Constraint_Check
(Operand
, Target_Type
);
11002 -- Case of conversions from a fixed-point type
11004 -- These conversions require special expansion and processing, found in
11005 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
11006 -- since from a semantic point of view, these are simple integer
11007 -- conversions, which do not need further processing.
11009 elsif Is_Fixed_Point_Type
(Operand_Type
)
11010 and then not Conversion_OK
(N
)
11012 -- We should never see universal fixed at this case, since the
11013 -- expansion of the constituent divide or multiply should have
11014 -- eliminated the explicit mention of universal fixed.
11016 pragma Assert
(Operand_Type
/= Universal_Fixed
);
11018 -- Check for special case of the conversion to universal real that
11019 -- occurs as a result of the use of a round attribute. In this case,
11020 -- the real type for the conversion is taken from the target type of
11021 -- the Round attribute and the result must be marked as rounded.
11023 if Target_Type
= Universal_Real
11024 and then Nkind
(Parent
(N
)) = N_Attribute_Reference
11025 and then Attribute_Name
(Parent
(N
)) = Name_Round
11027 Set_Rounded_Result
(N
);
11028 Set_Etype
(N
, Etype
(Parent
(N
)));
11031 -- Otherwise do correct fixed-conversion, but skip these if the
11032 -- Conversion_OK flag is set, because from a semantic point of view
11033 -- these are simple integer conversions needing no further processing
11034 -- (the backend will simply treat them as integers).
11036 if not Conversion_OK
(N
) then
11037 if Is_Fixed_Point_Type
(Etype
(N
)) then
11038 Expand_Convert_Fixed_To_Fixed
(N
);
11041 elsif Is_Integer_Type
(Etype
(N
)) then
11042 Expand_Convert_Fixed_To_Integer
(N
);
11045 pragma Assert
(Is_Floating_Point_Type
(Etype
(N
)));
11046 Expand_Convert_Fixed_To_Float
(N
);
11051 -- Case of conversions to a fixed-point type
11053 -- These conversions require special expansion and processing, found in
11054 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
11055 -- since from a semantic point of view, these are simple integer
11056 -- conversions, which do not need further processing.
11058 elsif Is_Fixed_Point_Type
(Target_Type
)
11059 and then not Conversion_OK
(N
)
11061 if Is_Integer_Type
(Operand_Type
) then
11062 Expand_Convert_Integer_To_Fixed
(N
);
11065 pragma Assert
(Is_Floating_Point_Type
(Operand_Type
));
11066 Expand_Convert_Float_To_Fixed
(N
);
11070 -- Case of float-to-integer conversions
11072 -- We also handle float-to-fixed conversions with Conversion_OK set
11073 -- since semantically the fixed-point target is treated as though it
11074 -- were an integer in such cases.
11076 elsif Is_Floating_Point_Type
(Operand_Type
)
11078 (Is_Integer_Type
(Target_Type
)
11080 (Is_Fixed_Point_Type
(Target_Type
) and then Conversion_OK
(N
)))
11082 -- One more check here, gcc is still not able to do conversions of
11083 -- this type with proper overflow checking, and so gigi is doing an
11084 -- approximation of what is required by doing floating-point compares
11085 -- with the end-point. But that can lose precision in some cases, and
11086 -- give a wrong result. Converting the operand to Universal_Real is
11087 -- helpful, but still does not catch all cases with 64-bit integers
11088 -- on targets with only 64-bit floats.
11090 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
11091 -- Can this code be removed ???
11093 if Do_Range_Check
(Operand
) then
11095 Make_Type_Conversion
(Loc
,
11097 New_Occurrence_Of
(Universal_Real
, Loc
),
11099 Relocate_Node
(Operand
)));
11101 Set_Etype
(Operand
, Universal_Real
);
11102 Enable_Range_Check
(Operand
);
11103 Set_Do_Range_Check
(Expression
(Operand
), False);
11106 -- Case of array conversions
11108 -- Expansion of array conversions, add required length/range checks but
11109 -- only do this if there is no change of representation. For handling of
11110 -- this case, see Handle_Changed_Representation.
11112 elsif Is_Array_Type
(Target_Type
) then
11113 if Is_Constrained
(Target_Type
) then
11114 Apply_Length_Check
(Operand
, Target_Type
);
11116 Apply_Range_Check
(Operand
, Target_Type
);
11119 Handle_Changed_Representation
;
11121 -- Case of conversions of discriminated types
11123 -- Add required discriminant checks if target is constrained. Again this
11124 -- change is skipped if we have a change of representation.
11126 elsif Has_Discriminants
(Target_Type
)
11127 and then Is_Constrained
(Target_Type
)
11129 Apply_Discriminant_Check
(Operand
, Target_Type
);
11130 Handle_Changed_Representation
;
11132 -- Case of all other record conversions. The only processing required
11133 -- is to check for a change of representation requiring the special
11134 -- assignment processing.
11136 elsif Is_Record_Type
(Target_Type
) then
11138 -- Ada 2005 (AI-216): Program_Error is raised when converting from
11139 -- a derived Unchecked_Union type to an unconstrained type that is
11140 -- not Unchecked_Union if the operand lacks inferable discriminants.
11142 if Is_Derived_Type
(Operand_Type
)
11143 and then Is_Unchecked_Union
(Base_Type
(Operand_Type
))
11144 and then not Is_Constrained
(Target_Type
)
11145 and then not Is_Unchecked_Union
(Base_Type
(Target_Type
))
11146 and then not Has_Inferable_Discriminants
(Operand
)
11148 -- To prevent Gigi from generating illegal code, we generate a
11149 -- Program_Error node, but we give it the target type of the
11150 -- conversion (is this requirement documented somewhere ???)
11153 PE
: constant Node_Id
:= Make_Raise_Program_Error
(Loc
,
11154 Reason
=> PE_Unchecked_Union_Restriction
);
11157 Set_Etype
(PE
, Target_Type
);
11162 Handle_Changed_Representation
;
11165 -- Case of conversions of enumeration types
11167 elsif Is_Enumeration_Type
(Target_Type
) then
11169 -- Special processing is required if there is a change of
11170 -- representation (from enumeration representation clauses).
11172 if not Same_Representation
(Target_Type
, Operand_Type
) then
11174 -- Convert: x(y) to x'val (ytyp'val (y))
11177 Make_Attribute_Reference
(Loc
,
11178 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
11179 Attribute_Name
=> Name_Val
,
11180 Expressions
=> New_List
(
11181 Make_Attribute_Reference
(Loc
,
11182 Prefix
=> New_Occurrence_Of
(Operand_Type
, Loc
),
11183 Attribute_Name
=> Name_Pos
,
11184 Expressions
=> New_List
(Operand
)))));
11186 Analyze_And_Resolve
(N
, Target_Type
);
11189 -- Case of conversions to floating-point
11191 elsif Is_Floating_Point_Type
(Target_Type
) then
11195 -- At this stage, either the conversion node has been transformed into
11196 -- some other equivalent expression, or left as a conversion that can be
11197 -- handled by Gigi, in the following cases:
11199 -- Conversions with no change of representation or type
11201 -- Numeric conversions involving integer, floating- and fixed-point
11202 -- values. Fixed-point values are allowed only if Conversion_OK is
11203 -- set, i.e. if the fixed-point values are to be treated as integers.
11205 -- No other conversions should be passed to Gigi
11207 -- Check: are these rules stated in sinfo??? if so, why restate here???
11209 -- The only remaining step is to generate a range check if we still have
11210 -- a type conversion at this stage and Do_Range_Check is set. For now we
11211 -- do this only for conversions of discrete types and for float-to-float
11214 if Nkind
(N
) = N_Type_Conversion
then
11216 -- For now we only support floating-point cases where both source
11217 -- and target are floating-point types. Conversions where the source
11218 -- and target involve integer or fixed-point types are still TBD,
11219 -- though not clear whether those can even happen at this point, due
11220 -- to transformations above. ???
11222 if Is_Floating_Point_Type
(Etype
(N
))
11223 and then Is_Floating_Point_Type
(Etype
(Expression
(N
)))
11225 if Do_Range_Check
(Expression
(N
))
11226 and then Is_Floating_Point_Type
(Target_Type
)
11228 Generate_Range_Check
11229 (Expression
(N
), Target_Type
, CE_Range_Check_Failed
);
11232 -- Discrete-to-discrete conversions
11234 elsif Is_Discrete_Type
(Etype
(N
)) then
11236 Expr
: constant Node_Id
:= Expression
(N
);
11241 if Do_Range_Check
(Expr
)
11242 and then Is_Discrete_Type
(Etype
(Expr
))
11244 Set_Do_Range_Check
(Expr
, False);
11246 -- Before we do a range check, we have to deal with treating
11247 -- a fixed-point operand as an integer. The way we do this
11248 -- is simply to do an unchecked conversion to an appropriate
11249 -- integer type large enough to hold the result.
11251 -- This code is not active yet, because we are only dealing
11252 -- with discrete types so far ???
11254 if Nkind
(Expr
) in N_Has_Treat_Fixed_As_Integer
11255 and then Treat_Fixed_As_Integer
(Expr
)
11257 Ftyp
:= Base_Type
(Etype
(Expr
));
11259 if Esize
(Ftyp
) >= Esize
(Standard_Integer
) then
11260 Ityp
:= Standard_Long_Long_Integer
;
11262 Ityp
:= Standard_Integer
;
11265 Rewrite
(Expr
, Unchecked_Convert_To
(Ityp
, Expr
));
11268 -- Reset overflow flag, since the range check will include
11269 -- dealing with possible overflow, and generate the check.
11270 -- If Address is either a source type or target type,
11271 -- suppress range check to avoid typing anomalies when
11272 -- it is a visible integer type.
11274 Set_Do_Overflow_Check
(N
, False);
11276 if not Is_Descendant_Of_Address
(Etype
(Expr
))
11277 and then not Is_Descendant_Of_Address
(Target_Type
)
11279 Generate_Range_Check
11280 (Expr
, Target_Type
, CE_Range_Check_Failed
);
11287 -- Here at end of processing
11290 -- Apply predicate check if required. Note that we can't just call
11291 -- Apply_Predicate_Check here, because the type looks right after
11292 -- the conversion and it would omit the check. The Comes_From_Source
11293 -- guard is necessary to prevent infinite recursions when we generate
11294 -- internal conversions for the purpose of checking predicates.
11296 if Present
(Predicate_Function
(Target_Type
))
11297 and then not Predicates_Ignored
(Target_Type
)
11298 and then Target_Type
/= Operand_Type
11299 and then Comes_From_Source
(N
)
11302 New_Expr
: constant Node_Id
:= Duplicate_Subexpr
(N
);
11305 -- Avoid infinite recursion on the subsequent expansion of
11306 -- of the copy of the original type conversion.
11308 Set_Comes_From_Source
(New_Expr
, False);
11309 Insert_Action
(N
, Make_Predicate_Check
(Target_Type
, New_Expr
));
11312 end Expand_N_Type_Conversion
;
11314 -----------------------------------
11315 -- Expand_N_Unchecked_Expression --
11316 -----------------------------------
11318 -- Remove the unchecked expression node from the tree. Its job was simply
11319 -- to make sure that its constituent expression was handled with checks
11320 -- off, and now that that is done, we can remove it from the tree, and
11321 -- indeed must, since Gigi does not expect to see these nodes.
11323 procedure Expand_N_Unchecked_Expression
(N
: Node_Id
) is
11324 Exp
: constant Node_Id
:= Expression
(N
);
11326 Set_Assignment_OK
(Exp
, Assignment_OK
(N
) or else Assignment_OK
(Exp
));
11328 end Expand_N_Unchecked_Expression
;
11330 ----------------------------------------
11331 -- Expand_N_Unchecked_Type_Conversion --
11332 ----------------------------------------
11334 -- If this cannot be handled by Gigi and we haven't already made a
11335 -- temporary for it, do it now.
11337 procedure Expand_N_Unchecked_Type_Conversion
(N
: Node_Id
) is
11338 Target_Type
: constant Entity_Id
:= Etype
(N
);
11339 Operand
: constant Node_Id
:= Expression
(N
);
11340 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
11343 -- Nothing at all to do if conversion is to the identical type so remove
11344 -- the conversion completely, it is useless, except that it may carry
11345 -- an Assignment_OK indication which must be propagated to the operand.
11347 if Operand_Type
= Target_Type
then
11349 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
11351 if Assignment_OK
(N
) then
11352 Set_Assignment_OK
(Operand
);
11355 Rewrite
(N
, Relocate_Node
(Operand
));
11359 -- If we have a conversion of a compile time known value to a target
11360 -- type and the value is in range of the target type, then we can simply
11361 -- replace the construct by an integer literal of the correct type. We
11362 -- only apply this to integer types being converted. Possibly it may
11363 -- apply in other cases, but it is too much trouble to worry about.
11365 -- Note that we do not do this transformation if the Kill_Range_Check
11366 -- flag is set, since then the value may be outside the expected range.
11367 -- This happens in the Normalize_Scalars case.
11369 -- We also skip this if either the target or operand type is biased
11370 -- because in this case, the unchecked conversion is supposed to
11371 -- preserve the bit pattern, not the integer value.
11373 if Is_Integer_Type
(Target_Type
)
11374 and then not Has_Biased_Representation
(Target_Type
)
11375 and then Is_Integer_Type
(Operand_Type
)
11376 and then not Has_Biased_Representation
(Operand_Type
)
11377 and then Compile_Time_Known_Value
(Operand
)
11378 and then not Kill_Range_Check
(N
)
11381 Val
: constant Uint
:= Expr_Value
(Operand
);
11384 if Compile_Time_Known_Value
(Type_Low_Bound
(Target_Type
))
11386 Compile_Time_Known_Value
(Type_High_Bound
(Target_Type
))
11388 Val
>= Expr_Value
(Type_Low_Bound
(Target_Type
))
11390 Val
<= Expr_Value
(Type_High_Bound
(Target_Type
))
11392 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Val
));
11394 -- If Address is the target type, just set the type to avoid a
11395 -- spurious type error on the literal when Address is a visible
11398 if Is_Descendant_Of_Address
(Target_Type
) then
11399 Set_Etype
(N
, Target_Type
);
11401 Analyze_And_Resolve
(N
, Target_Type
);
11409 -- Nothing to do if conversion is safe
11411 if Safe_Unchecked_Type_Conversion
(N
) then
11415 -- Otherwise force evaluation unless Assignment_OK flag is set (this
11416 -- flag indicates ??? More comments needed here)
11418 if Assignment_OK
(N
) then
11421 Force_Evaluation
(N
);
11423 end Expand_N_Unchecked_Type_Conversion
;
11425 ----------------------------
11426 -- Expand_Record_Equality --
11427 ----------------------------
11429 -- For non-variant records, Equality is expanded when needed into:
11431 -- and then Lhs.Discr1 = Rhs.Discr1
11433 -- and then Lhs.Discrn = Rhs.Discrn
11434 -- and then Lhs.Cmp1 = Rhs.Cmp1
11436 -- and then Lhs.Cmpn = Rhs.Cmpn
11438 -- The expression is folded by the back-end for adjacent fields. This
11439 -- function is called for tagged record in only one occasion: for imple-
11440 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
11441 -- otherwise the primitive "=" is used directly.
11443 function Expand_Record_Equality
11448 Bodies
: List_Id
) return Node_Id
11450 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
11455 First_Time
: Boolean := True;
11457 function Element_To_Compare
(C
: Entity_Id
) return Entity_Id
;
11458 -- Return the next discriminant or component to compare, starting with
11459 -- C, skipping inherited components.
11461 ------------------------
11462 -- Element_To_Compare --
11463 ------------------------
11465 function Element_To_Compare
(C
: Entity_Id
) return Entity_Id
is
11471 -- Exit loop when the next element to be compared is found, or
11472 -- there is no more such element.
11474 exit when No
(Comp
);
11476 exit when Ekind_In
(Comp
, E_Discriminant
, E_Component
)
11479 -- Skip inherited components
11481 -- Note: for a tagged type, we always generate the "=" primitive
11482 -- for the base type (not on the first subtype), so the test for
11483 -- Comp /= Original_Record_Component (Comp) is True for
11484 -- inherited components only.
11486 (Is_Tagged_Type
(Typ
)
11487 and then Comp
/= Original_Record_Component
(Comp
))
11491 or else Chars
(Comp
) = Name_uTag
11493 -- Skip interface elements (secondary tags???)
11495 or else Is_Interface
(Etype
(Comp
)));
11497 Next_Entity
(Comp
);
11501 end Element_To_Compare
;
11503 -- Start of processing for Expand_Record_Equality
11506 -- Generates the following code: (assuming that Typ has one Discr and
11507 -- component C2 is also a record)
11510 -- and then Lhs.Discr1 = Rhs.Discr1
11511 -- and then Lhs.C1 = Rhs.C1
11512 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
11514 -- and then Lhs.Cmpn = Rhs.Cmpn
11516 Result
:= New_Occurrence_Of
(Standard_True
, Loc
);
11517 C
:= Element_To_Compare
(First_Entity
(Typ
));
11518 while Present
(C
) loop
11526 First_Time
:= False;
11530 New_Lhs
:= New_Copy_Tree
(Lhs
);
11531 New_Rhs
:= New_Copy_Tree
(Rhs
);
11535 Expand_Composite_Equality
(Nod
, Etype
(C
),
11537 Make_Selected_Component
(Loc
,
11539 Selector_Name
=> New_Occurrence_Of
(C
, Loc
)),
11541 Make_Selected_Component
(Loc
,
11543 Selector_Name
=> New_Occurrence_Of
(C
, Loc
)),
11546 -- If some (sub)component is an unchecked_union, the whole
11547 -- operation will raise program error.
11549 if Nkind
(Check
) = N_Raise_Program_Error
then
11551 Set_Etype
(Result
, Standard_Boolean
);
11555 Make_And_Then
(Loc
,
11556 Left_Opnd
=> Result
,
11557 Right_Opnd
=> Check
);
11561 C
:= Element_To_Compare
(Next_Entity
(C
));
11565 end Expand_Record_Equality
;
11567 ---------------------------
11568 -- Expand_Set_Membership --
11569 ---------------------------
11571 procedure Expand_Set_Membership
(N
: Node_Id
) is
11572 Lop
: constant Node_Id
:= Left_Opnd
(N
);
11576 function Make_Cond
(Alt
: Node_Id
) return Node_Id
;
11577 -- If the alternative is a subtype mark, create a simple membership
11578 -- test. Otherwise create an equality test for it.
11584 function Make_Cond
(Alt
: Node_Id
) return Node_Id
is
11586 L
: constant Node_Id
:= New_Copy
(Lop
);
11587 R
: constant Node_Id
:= Relocate_Node
(Alt
);
11590 if (Is_Entity_Name
(Alt
) and then Is_Type
(Entity
(Alt
)))
11591 or else Nkind
(Alt
) = N_Range
11594 Make_In
(Sloc
(Alt
),
11599 Make_Op_Eq
(Sloc
(Alt
),
11607 -- Start of processing for Expand_Set_Membership
11610 Remove_Side_Effects
(Lop
);
11612 Alt
:= Last
(Alternatives
(N
));
11613 Res
:= Make_Cond
(Alt
);
11616 while Present
(Alt
) loop
11618 Make_Or_Else
(Sloc
(Alt
),
11619 Left_Opnd
=> Make_Cond
(Alt
),
11620 Right_Opnd
=> Res
);
11625 Analyze_And_Resolve
(N
, Standard_Boolean
);
11626 end Expand_Set_Membership
;
11628 -----------------------------------
11629 -- Expand_Short_Circuit_Operator --
11630 -----------------------------------
11632 -- Deal with special expansion if actions are present for the right operand
11633 -- and deal with optimizing case of arguments being True or False. We also
11634 -- deal with the special case of non-standard boolean values.
11636 procedure Expand_Short_Circuit_Operator
(N
: Node_Id
) is
11637 Loc
: constant Source_Ptr
:= Sloc
(N
);
11638 Typ
: constant Entity_Id
:= Etype
(N
);
11639 Left
: constant Node_Id
:= Left_Opnd
(N
);
11640 Right
: constant Node_Id
:= Right_Opnd
(N
);
11641 LocR
: constant Source_Ptr
:= Sloc
(Right
);
11644 Shortcut_Value
: constant Boolean := Nkind
(N
) = N_Or_Else
;
11645 Shortcut_Ent
: constant Entity_Id
:= Boolean_Literals
(Shortcut_Value
);
11646 -- If Left = Shortcut_Value then Right need not be evaluated
11648 function Make_Test_Expr
(Opnd
: Node_Id
) return Node_Id
;
11649 -- For Opnd a boolean expression, return a Boolean expression equivalent
11650 -- to Opnd /= Shortcut_Value.
11652 --------------------
11653 -- Make_Test_Expr --
11654 --------------------
11656 function Make_Test_Expr
(Opnd
: Node_Id
) return Node_Id
is
11658 if Shortcut_Value
then
11659 return Make_Op_Not
(Sloc
(Opnd
), Opnd
);
11663 end Make_Test_Expr
;
11667 Op_Var
: Entity_Id
;
11668 -- Entity for a temporary variable holding the value of the operator,
11669 -- used for expansion in the case where actions are present.
11671 -- Start of processing for Expand_Short_Circuit_Operator
11674 -- Deal with non-standard booleans
11676 if Is_Boolean_Type
(Typ
) then
11677 Adjust_Condition
(Left
);
11678 Adjust_Condition
(Right
);
11679 Set_Etype
(N
, Standard_Boolean
);
11682 -- Check for cases where left argument is known to be True or False
11684 if Compile_Time_Known_Value
(Left
) then
11686 -- Mark SCO for left condition as compile time known
11688 if Generate_SCO
and then Comes_From_Source
(Left
) then
11689 Set_SCO_Condition
(Left
, Expr_Value_E
(Left
) = Standard_True
);
11692 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
11693 -- Any actions associated with Right will be executed unconditionally
11694 -- and can thus be inserted into the tree unconditionally.
11696 if Expr_Value_E
(Left
) /= Shortcut_Ent
then
11697 if Present
(Actions
(N
)) then
11698 Insert_Actions
(N
, Actions
(N
));
11701 Rewrite
(N
, Right
);
11703 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
11704 -- In this case we can forget the actions associated with Right,
11705 -- since they will never be executed.
11708 Kill_Dead_Code
(Right
);
11709 Kill_Dead_Code
(Actions
(N
));
11710 Rewrite
(N
, New_Occurrence_Of
(Shortcut_Ent
, Loc
));
11713 Adjust_Result_Type
(N
, Typ
);
11717 -- If Actions are present for the right operand, we have to do some
11718 -- special processing. We can't just let these actions filter back into
11719 -- code preceding the short circuit (which is what would have happened
11720 -- if we had not trapped them in the short-circuit form), since they
11721 -- must only be executed if the right operand of the short circuit is
11722 -- executed and not otherwise.
11724 if Present
(Actions
(N
)) then
11725 Actlist
:= Actions
(N
);
11727 -- The old approach is to expand:
11729 -- left AND THEN right
11733 -- C : Boolean := False;
11741 -- and finally rewrite the operator into a reference to C. Similarly
11742 -- for left OR ELSE right, with negated values. Note that this
11743 -- rewrite causes some difficulties for coverage analysis because
11744 -- of the introduction of the new variable C, which obscures the
11745 -- structure of the test.
11747 -- We use this "old approach" if Minimize_Expression_With_Actions
11750 if Minimize_Expression_With_Actions
then
11751 Op_Var
:= Make_Temporary
(Loc
, 'C', Related_Node
=> N
);
11754 Make_Object_Declaration
(Loc
,
11755 Defining_Identifier
=> Op_Var
,
11756 Object_Definition
=>
11757 New_Occurrence_Of
(Standard_Boolean
, Loc
),
11759 New_Occurrence_Of
(Shortcut_Ent
, Loc
)));
11761 Append_To
(Actlist
,
11762 Make_Implicit_If_Statement
(Right
,
11763 Condition
=> Make_Test_Expr
(Right
),
11764 Then_Statements
=> New_List
(
11765 Make_Assignment_Statement
(LocR
,
11766 Name
=> New_Occurrence_Of
(Op_Var
, LocR
),
11769 (Boolean_Literals
(not Shortcut_Value
), LocR
)))));
11772 Make_Implicit_If_Statement
(Left
,
11773 Condition
=> Make_Test_Expr
(Left
),
11774 Then_Statements
=> Actlist
));
11776 Rewrite
(N
, New_Occurrence_Of
(Op_Var
, Loc
));
11777 Analyze_And_Resolve
(N
, Standard_Boolean
);
11779 -- The new approach (the default) is to use an
11780 -- Expression_With_Actions node for the right operand of the
11781 -- short-circuit form. Note that this solves the traceability
11782 -- problems for coverage analysis.
11786 Make_Expression_With_Actions
(LocR
,
11787 Expression
=> Relocate_Node
(Right
),
11788 Actions
=> Actlist
));
11790 Set_Actions
(N
, No_List
);
11791 Analyze_And_Resolve
(Right
, Standard_Boolean
);
11794 Adjust_Result_Type
(N
, Typ
);
11798 -- No actions present, check for cases of right argument True/False
11800 if Compile_Time_Known_Value
(Right
) then
11802 -- Mark SCO for left condition as compile time known
11804 if Generate_SCO
and then Comes_From_Source
(Right
) then
11805 Set_SCO_Condition
(Right
, Expr_Value_E
(Right
) = Standard_True
);
11808 -- Change (Left and then True), (Left or else False) to Left. Note
11809 -- that we know there are no actions associated with the right
11810 -- operand, since we just checked for this case above.
11812 if Expr_Value_E
(Right
) /= Shortcut_Ent
then
11815 -- Change (Left and then False), (Left or else True) to Right,
11816 -- making sure to preserve any side effects associated with the Left
11820 Remove_Side_Effects
(Left
);
11821 Rewrite
(N
, New_Occurrence_Of
(Shortcut_Ent
, Loc
));
11825 Adjust_Result_Type
(N
, Typ
);
11826 end Expand_Short_Circuit_Operator
;
11828 -------------------------------------
11829 -- Fixup_Universal_Fixed_Operation --
11830 -------------------------------------
11832 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
) is
11833 Conv
: constant Node_Id
:= Parent
(N
);
11836 -- We must have a type conversion immediately above us
11838 pragma Assert
(Nkind
(Conv
) = N_Type_Conversion
);
11840 -- Normally the type conversion gives our target type. The exception
11841 -- occurs in the case of the Round attribute, where the conversion
11842 -- will be to universal real, and our real type comes from the Round
11843 -- attribute (as well as an indication that we must round the result)
11845 if Nkind
(Parent
(Conv
)) = N_Attribute_Reference
11846 and then Attribute_Name
(Parent
(Conv
)) = Name_Round
11848 Set_Etype
(N
, Etype
(Parent
(Conv
)));
11849 Set_Rounded_Result
(N
);
11851 -- Normal case where type comes from conversion above us
11854 Set_Etype
(N
, Etype
(Conv
));
11856 end Fixup_Universal_Fixed_Operation
;
11858 ---------------------------------
11859 -- Has_Inferable_Discriminants --
11860 ---------------------------------
11862 function Has_Inferable_Discriminants
(N
: Node_Id
) return Boolean is
11864 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean;
11865 -- Determines whether the left-most prefix of a selected component is a
11866 -- formal parameter in a subprogram. Assumes N is a selected component.
11868 --------------------------------
11869 -- Prefix_Is_Formal_Parameter --
11870 --------------------------------
11872 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean is
11873 Sel_Comp
: Node_Id
;
11876 -- Move to the left-most prefix by climbing up the tree
11879 while Present
(Parent
(Sel_Comp
))
11880 and then Nkind
(Parent
(Sel_Comp
)) = N_Selected_Component
11882 Sel_Comp
:= Parent
(Sel_Comp
);
11885 return Ekind
(Entity
(Prefix
(Sel_Comp
))) in Formal_Kind
;
11886 end Prefix_Is_Formal_Parameter
;
11888 -- Start of processing for Has_Inferable_Discriminants
11891 -- For selected components, the subtype of the selector must be a
11892 -- constrained Unchecked_Union. If the component is subject to a
11893 -- per-object constraint, then the enclosing object must have inferable
11896 if Nkind
(N
) = N_Selected_Component
then
11897 if Has_Per_Object_Constraint
(Entity
(Selector_Name
(N
))) then
11899 -- A small hack. If we have a per-object constrained selected
11900 -- component of a formal parameter, return True since we do not
11901 -- know the actual parameter association yet.
11903 if Prefix_Is_Formal_Parameter
(N
) then
11906 -- Otherwise, check the enclosing object and the selector
11909 return Has_Inferable_Discriminants
(Prefix
(N
))
11910 and then Has_Inferable_Discriminants
(Selector_Name
(N
));
11913 -- The call to Has_Inferable_Discriminants will determine whether
11914 -- the selector has a constrained Unchecked_Union nominal type.
11917 return Has_Inferable_Discriminants
(Selector_Name
(N
));
11920 -- A qualified expression has inferable discriminants if its subtype
11921 -- mark is a constrained Unchecked_Union subtype.
11923 elsif Nkind
(N
) = N_Qualified_Expression
then
11924 return Is_Unchecked_Union
(Etype
(Subtype_Mark
(N
)))
11925 and then Is_Constrained
(Etype
(Subtype_Mark
(N
)));
11927 -- For all other names, it is sufficient to have a constrained
11928 -- Unchecked_Union nominal subtype.
11931 return Is_Unchecked_Union
(Base_Type
(Etype
(N
)))
11932 and then Is_Constrained
(Etype
(N
));
11934 end Has_Inferable_Discriminants
;
11936 -------------------------------
11937 -- Insert_Dereference_Action --
11938 -------------------------------
11940 procedure Insert_Dereference_Action
(N
: Node_Id
) is
11942 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean;
11943 -- Return true if type of P is derived from Checked_Pool;
11945 -----------------------------
11946 -- Is_Checked_Storage_Pool --
11947 -----------------------------
11949 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean is
11958 while T
/= Etype
(T
) loop
11959 if Is_RTE
(T
, RE_Checked_Pool
) then
11967 end Is_Checked_Storage_Pool
;
11971 Typ
: constant Entity_Id
:= Etype
(N
);
11972 Desig
: constant Entity_Id
:= Available_View
(Designated_Type
(Typ
));
11973 Loc
: constant Source_Ptr
:= Sloc
(N
);
11974 Pool
: constant Entity_Id
:= Associated_Storage_Pool
(Typ
);
11975 Pnod
: constant Node_Id
:= Parent
(N
);
11981 Size_Bits
: Node_Id
;
11984 -- Start of processing for Insert_Dereference_Action
11987 pragma Assert
(Nkind
(Pnod
) = N_Explicit_Dereference
);
11989 -- Do not re-expand a dereference which has already been processed by
11992 if Has_Dereference_Action
(Pnod
) then
11995 -- Do not perform this type of expansion for internally-generated
11998 elsif not Comes_From_Source
(Original_Node
(Pnod
)) then
12001 -- A dereference action is only applicable to objects which have been
12002 -- allocated on a checked pool.
12004 elsif not Is_Checked_Storage_Pool
(Pool
) then
12008 -- Extract the address of the dereferenced object. Generate:
12010 -- Addr : System.Address := <N>'Pool_Address;
12012 Addr
:= Make_Temporary
(Loc
, 'P');
12015 Make_Object_Declaration
(Loc
,
12016 Defining_Identifier
=> Addr
,
12017 Object_Definition
=>
12018 New_Occurrence_Of
(RTE
(RE_Address
), Loc
),
12020 Make_Attribute_Reference
(Loc
,
12021 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
),
12022 Attribute_Name
=> Name_Pool_Address
)));
12024 -- Calculate the size of the dereferenced object. Generate:
12026 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
12029 Make_Explicit_Dereference
(Loc
,
12030 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
12031 Set_Has_Dereference_Action
(Deref
);
12034 Make_Attribute_Reference
(Loc
,
12036 Attribute_Name
=> Name_Size
);
12038 -- Special case of an unconstrained array: need to add descriptor size
12040 if Is_Array_Type
(Desig
)
12041 and then not Is_Constrained
(First_Subtype
(Desig
))
12046 Make_Attribute_Reference
(Loc
,
12048 New_Occurrence_Of
(First_Subtype
(Desig
), Loc
),
12049 Attribute_Name
=> Name_Descriptor_Size
),
12050 Right_Opnd
=> Size_Bits
);
12053 Size
:= Make_Temporary
(Loc
, 'S');
12055 Make_Object_Declaration
(Loc
,
12056 Defining_Identifier
=> Size
,
12057 Object_Definition
=>
12058 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
),
12060 Make_Op_Divide
(Loc
,
12061 Left_Opnd
=> Size_Bits
,
12062 Right_Opnd
=> Make_Integer_Literal
(Loc
, System_Storage_Unit
))));
12064 -- Calculate the alignment of the dereferenced object. Generate:
12065 -- Alig : constant Storage_Count := <N>.all'Alignment;
12068 Make_Explicit_Dereference
(Loc
,
12069 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
12070 Set_Has_Dereference_Action
(Deref
);
12072 Alig
:= Make_Temporary
(Loc
, 'A');
12074 Make_Object_Declaration
(Loc
,
12075 Defining_Identifier
=> Alig
,
12076 Object_Definition
=>
12077 New_Occurrence_Of
(RTE
(RE_Storage_Count
), Loc
),
12079 Make_Attribute_Reference
(Loc
,
12081 Attribute_Name
=> Name_Alignment
)));
12083 -- A dereference of a controlled object requires special processing. The
12084 -- finalization machinery requests additional space from the underlying
12085 -- pool to allocate and hide two pointers. As a result, a checked pool
12086 -- may mark the wrong memory as valid. Since checked pools do not have
12087 -- knowledge of hidden pointers, we have to bring the two pointers back
12088 -- in view in order to restore the original state of the object.
12090 if Needs_Finalization
(Desig
) then
12092 -- Adjust the address and size of the dereferenced object. Generate:
12093 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
12096 Make_Procedure_Call_Statement
(Loc
,
12098 New_Occurrence_Of
(RTE
(RE_Adjust_Controlled_Dereference
), Loc
),
12099 Parameter_Associations
=> New_List
(
12100 New_Occurrence_Of
(Addr
, Loc
),
12101 New_Occurrence_Of
(Size
, Loc
),
12102 New_Occurrence_Of
(Alig
, Loc
)));
12104 -- Class-wide types complicate things because we cannot determine
12105 -- statically whether the actual object is truly controlled. We must
12106 -- generate a runtime check to detect this property. Generate:
12108 -- if Needs_Finalization (<N>.all'Tag) then
12112 if Is_Class_Wide_Type
(Desig
) then
12114 Make_Explicit_Dereference
(Loc
,
12115 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
));
12116 Set_Has_Dereference_Action
(Deref
);
12119 Make_Implicit_If_Statement
(N
,
12121 Make_Function_Call
(Loc
,
12123 New_Occurrence_Of
(RTE
(RE_Needs_Finalization
), Loc
),
12124 Parameter_Associations
=> New_List
(
12125 Make_Attribute_Reference
(Loc
,
12127 Attribute_Name
=> Name_Tag
))),
12128 Then_Statements
=> New_List
(Stmt
));
12131 Insert_Action
(N
, Stmt
);
12135 -- Dereference (Pool, Addr, Size, Alig);
12138 Make_Procedure_Call_Statement
(Loc
,
12141 (Find_Prim_Op
(Etype
(Pool
), Name_Dereference
), Loc
),
12142 Parameter_Associations
=> New_List
(
12143 New_Occurrence_Of
(Pool
, Loc
),
12144 New_Occurrence_Of
(Addr
, Loc
),
12145 New_Occurrence_Of
(Size
, Loc
),
12146 New_Occurrence_Of
(Alig
, Loc
))));
12148 -- Mark the explicit dereference as processed to avoid potential
12149 -- infinite expansion.
12151 Set_Has_Dereference_Action
(Pnod
);
12154 when RE_Not_Available
=>
12156 end Insert_Dereference_Action
;
12158 --------------------------------
12159 -- Integer_Promotion_Possible --
12160 --------------------------------
12162 function Integer_Promotion_Possible
(N
: Node_Id
) return Boolean is
12163 Operand
: constant Node_Id
:= Expression
(N
);
12164 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
12165 Root_Operand_Type
: constant Entity_Id
:= Root_Type
(Operand_Type
);
12168 pragma Assert
(Nkind
(N
) = N_Type_Conversion
);
12172 -- We only do the transformation for source constructs. We assume
12173 -- that the expander knows what it is doing when it generates code.
12175 Comes_From_Source
(N
)
12177 -- If the operand type is Short_Integer or Short_Short_Integer,
12178 -- then we will promote to Integer, which is available on all
12179 -- targets, and is sufficient to ensure no intermediate overflow.
12180 -- Furthermore it is likely to be as efficient or more efficient
12181 -- than using the smaller type for the computation so we do this
12182 -- unconditionally.
12185 (Root_Operand_Type
= Base_Type
(Standard_Short_Integer
)
12187 Root_Operand_Type
= Base_Type
(Standard_Short_Short_Integer
))
12189 -- Test for interesting operation, which includes addition,
12190 -- division, exponentiation, multiplication, subtraction, absolute
12191 -- value and unary negation. Unary "+" is omitted since it is a
12192 -- no-op and thus can't overflow.
12194 and then Nkind_In
(Operand
, N_Op_Abs
,
12201 end Integer_Promotion_Possible
;
12203 ------------------------------
12204 -- Make_Array_Comparison_Op --
12205 ------------------------------
12207 -- This is a hand-coded expansion of the following generic function:
12210 -- type elem is (<>);
12211 -- type index is (<>);
12212 -- type a is array (index range <>) of elem;
12214 -- function Gnnn (X : a; Y: a) return boolean is
12215 -- J : index := Y'first;
12218 -- if X'length = 0 then
12221 -- elsif Y'length = 0 then
12225 -- for I in X'range loop
12226 -- if X (I) = Y (J) then
12227 -- if J = Y'last then
12230 -- J := index'succ (J);
12234 -- return X (I) > Y (J);
12238 -- return X'length > Y'length;
12242 -- Note that since we are essentially doing this expansion by hand, we
12243 -- do not need to generate an actual or formal generic part, just the
12244 -- instantiated function itself.
12246 -- Perhaps we could have the actual generic available in the run-time,
12247 -- obtained by rtsfind, and actually expand a real instantiation ???
12249 function Make_Array_Comparison_Op
12251 Nod
: Node_Id
) return Node_Id
12253 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
12255 X
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uX
);
12256 Y
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uY
);
12257 I
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uI
);
12258 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
12260 Index
: constant Entity_Id
:= Base_Type
(Etype
(First_Index
(Typ
)));
12262 Loop_Statement
: Node_Id
;
12263 Loop_Body
: Node_Id
;
12265 Inner_If
: Node_Id
;
12266 Final_Expr
: Node_Id
;
12267 Func_Body
: Node_Id
;
12268 Func_Name
: Entity_Id
;
12274 -- if J = Y'last then
12277 -- J := index'succ (J);
12281 Make_Implicit_If_Statement
(Nod
,
12284 Left_Opnd
=> New_Occurrence_Of
(J
, Loc
),
12286 Make_Attribute_Reference
(Loc
,
12287 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12288 Attribute_Name
=> Name_Last
)),
12290 Then_Statements
=> New_List
(
12291 Make_Exit_Statement
(Loc
)),
12295 Make_Assignment_Statement
(Loc
,
12296 Name
=> New_Occurrence_Of
(J
, Loc
),
12298 Make_Attribute_Reference
(Loc
,
12299 Prefix
=> New_Occurrence_Of
(Index
, Loc
),
12300 Attribute_Name
=> Name_Succ
,
12301 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
))))));
12303 -- if X (I) = Y (J) then
12306 -- return X (I) > Y (J);
12310 Make_Implicit_If_Statement
(Nod
,
12314 Make_Indexed_Component
(Loc
,
12315 Prefix
=> New_Occurrence_Of
(X
, Loc
),
12316 Expressions
=> New_List
(New_Occurrence_Of
(I
, Loc
))),
12319 Make_Indexed_Component
(Loc
,
12320 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12321 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)))),
12323 Then_Statements
=> New_List
(Inner_If
),
12325 Else_Statements
=> New_List
(
12326 Make_Simple_Return_Statement
(Loc
,
12330 Make_Indexed_Component
(Loc
,
12331 Prefix
=> New_Occurrence_Of
(X
, Loc
),
12332 Expressions
=> New_List
(New_Occurrence_Of
(I
, Loc
))),
12335 Make_Indexed_Component
(Loc
,
12336 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12337 Expressions
=> New_List
(
12338 New_Occurrence_Of
(J
, Loc
)))))));
12340 -- for I in X'range loop
12345 Make_Implicit_Loop_Statement
(Nod
,
12346 Identifier
=> Empty
,
12348 Iteration_Scheme
=>
12349 Make_Iteration_Scheme
(Loc
,
12350 Loop_Parameter_Specification
=>
12351 Make_Loop_Parameter_Specification
(Loc
,
12352 Defining_Identifier
=> I
,
12353 Discrete_Subtype_Definition
=>
12354 Make_Attribute_Reference
(Loc
,
12355 Prefix
=> New_Occurrence_Of
(X
, Loc
),
12356 Attribute_Name
=> Name_Range
))),
12358 Statements
=> New_List
(Loop_Body
));
12360 -- if X'length = 0 then
12362 -- elsif Y'length = 0 then
12365 -- for ... loop ... end loop;
12366 -- return X'length > Y'length;
12370 Make_Attribute_Reference
(Loc
,
12371 Prefix
=> New_Occurrence_Of
(X
, Loc
),
12372 Attribute_Name
=> Name_Length
);
12375 Make_Attribute_Reference
(Loc
,
12376 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12377 Attribute_Name
=> Name_Length
);
12381 Left_Opnd
=> Length1
,
12382 Right_Opnd
=> Length2
);
12385 Make_Implicit_If_Statement
(Nod
,
12389 Make_Attribute_Reference
(Loc
,
12390 Prefix
=> New_Occurrence_Of
(X
, Loc
),
12391 Attribute_Name
=> Name_Length
),
12393 Make_Integer_Literal
(Loc
, 0)),
12397 Make_Simple_Return_Statement
(Loc
,
12398 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))),
12400 Elsif_Parts
=> New_List
(
12401 Make_Elsif_Part
(Loc
,
12405 Make_Attribute_Reference
(Loc
,
12406 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12407 Attribute_Name
=> Name_Length
),
12409 Make_Integer_Literal
(Loc
, 0)),
12413 Make_Simple_Return_Statement
(Loc
,
12414 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
))))),
12416 Else_Statements
=> New_List
(
12418 Make_Simple_Return_Statement
(Loc
,
12419 Expression
=> Final_Expr
)));
12423 Formals
:= New_List
(
12424 Make_Parameter_Specification
(Loc
,
12425 Defining_Identifier
=> X
,
12426 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)),
12428 Make_Parameter_Specification
(Loc
,
12429 Defining_Identifier
=> Y
,
12430 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)));
12432 -- function Gnnn (...) return boolean is
12433 -- J : index := Y'first;
12438 Func_Name
:= Make_Temporary
(Loc
, 'G');
12441 Make_Subprogram_Body
(Loc
,
12443 Make_Function_Specification
(Loc
,
12444 Defining_Unit_Name
=> Func_Name
,
12445 Parameter_Specifications
=> Formals
,
12446 Result_Definition
=> New_Occurrence_Of
(Standard_Boolean
, Loc
)),
12448 Declarations
=> New_List
(
12449 Make_Object_Declaration
(Loc
,
12450 Defining_Identifier
=> J
,
12451 Object_Definition
=> New_Occurrence_Of
(Index
, Loc
),
12453 Make_Attribute_Reference
(Loc
,
12454 Prefix
=> New_Occurrence_Of
(Y
, Loc
),
12455 Attribute_Name
=> Name_First
))),
12457 Handled_Statement_Sequence
=>
12458 Make_Handled_Sequence_Of_Statements
(Loc
,
12459 Statements
=> New_List
(If_Stat
)));
12462 end Make_Array_Comparison_Op
;
12464 ---------------------------
12465 -- Make_Boolean_Array_Op --
12466 ---------------------------
12468 -- For logical operations on boolean arrays, expand in line the following,
12469 -- replacing 'and' with 'or' or 'xor' where needed:
12471 -- function Annn (A : typ; B: typ) return typ is
12474 -- for J in A'range loop
12475 -- C (J) := A (J) op B (J);
12480 -- Here typ is the boolean array type
12482 function Make_Boolean_Array_Op
12484 N
: Node_Id
) return Node_Id
12486 Loc
: constant Source_Ptr
:= Sloc
(N
);
12488 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
12489 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
12490 C
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uC
);
12491 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
12499 Func_Name
: Entity_Id
;
12500 Func_Body
: Node_Id
;
12501 Loop_Statement
: Node_Id
;
12505 Make_Indexed_Component
(Loc
,
12506 Prefix
=> New_Occurrence_Of
(A
, Loc
),
12507 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
12510 Make_Indexed_Component
(Loc
,
12511 Prefix
=> New_Occurrence_Of
(B
, Loc
),
12512 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
12515 Make_Indexed_Component
(Loc
,
12516 Prefix
=> New_Occurrence_Of
(C
, Loc
),
12517 Expressions
=> New_List
(New_Occurrence_Of
(J
, Loc
)));
12519 if Nkind
(N
) = N_Op_And
then
12523 Right_Opnd
=> B_J
);
12525 elsif Nkind
(N
) = N_Op_Or
then
12529 Right_Opnd
=> B_J
);
12535 Right_Opnd
=> B_J
);
12539 Make_Implicit_Loop_Statement
(N
,
12540 Identifier
=> Empty
,
12542 Iteration_Scheme
=>
12543 Make_Iteration_Scheme
(Loc
,
12544 Loop_Parameter_Specification
=>
12545 Make_Loop_Parameter_Specification
(Loc
,
12546 Defining_Identifier
=> J
,
12547 Discrete_Subtype_Definition
=>
12548 Make_Attribute_Reference
(Loc
,
12549 Prefix
=> New_Occurrence_Of
(A
, Loc
),
12550 Attribute_Name
=> Name_Range
))),
12552 Statements
=> New_List
(
12553 Make_Assignment_Statement
(Loc
,
12555 Expression
=> Op
)));
12557 Formals
:= New_List
(
12558 Make_Parameter_Specification
(Loc
,
12559 Defining_Identifier
=> A
,
12560 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)),
12562 Make_Parameter_Specification
(Loc
,
12563 Defining_Identifier
=> B
,
12564 Parameter_Type
=> New_Occurrence_Of
(Typ
, Loc
)));
12566 Func_Name
:= Make_Temporary
(Loc
, 'A');
12567 Set_Is_Inlined
(Func_Name
);
12570 Make_Subprogram_Body
(Loc
,
12572 Make_Function_Specification
(Loc
,
12573 Defining_Unit_Name
=> Func_Name
,
12574 Parameter_Specifications
=> Formals
,
12575 Result_Definition
=> New_Occurrence_Of
(Typ
, Loc
)),
12577 Declarations
=> New_List
(
12578 Make_Object_Declaration
(Loc
,
12579 Defining_Identifier
=> C
,
12580 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
))),
12582 Handled_Statement_Sequence
=>
12583 Make_Handled_Sequence_Of_Statements
(Loc
,
12584 Statements
=> New_List
(
12586 Make_Simple_Return_Statement
(Loc
,
12587 Expression
=> New_Occurrence_Of
(C
, Loc
)))));
12590 end Make_Boolean_Array_Op
;
12592 -----------------------------------------
12593 -- Minimized_Eliminated_Overflow_Check --
12594 -----------------------------------------
12596 function Minimized_Eliminated_Overflow_Check
(N
: Node_Id
) return Boolean is
12599 Is_Signed_Integer_Type
(Etype
(N
))
12600 and then Overflow_Check_Mode
in Minimized_Or_Eliminated
;
12601 end Minimized_Eliminated_Overflow_Check
;
12603 --------------------------------
12604 -- Optimize_Length_Comparison --
12605 --------------------------------
12607 procedure Optimize_Length_Comparison
(N
: Node_Id
) is
12608 Loc
: constant Source_Ptr
:= Sloc
(N
);
12609 Typ
: constant Entity_Id
:= Etype
(N
);
12614 -- First and Last attribute reference nodes, which end up as left and
12615 -- right operands of the optimized result.
12618 -- True for comparison operand of zero
12621 -- Comparison operand, set only if Is_Zero is false
12624 -- Entity whose length is being compared
12627 -- Integer_Literal node for length attribute expression, or Empty
12628 -- if there is no such expression present.
12631 -- Type of array index to which 'Length is applied
12633 Op
: Node_Kind
:= Nkind
(N
);
12634 -- Kind of comparison operator, gets flipped if operands backwards
12636 function Is_Optimizable
(N
: Node_Id
) return Boolean;
12637 -- Tests N to see if it is an optimizable comparison value (defined as
12638 -- constant zero or one, or something else where the value is known to
12639 -- be positive and in the range of 32-bits, and where the corresponding
12640 -- Length value is also known to be 32-bits. If result is true, sets
12641 -- Is_Zero, Ityp, and Comp accordingly.
12643 function Is_Entity_Length
(N
: Node_Id
) return Boolean;
12644 -- Tests if N is a length attribute applied to a simple entity. If so,
12645 -- returns True, and sets Ent to the entity, and Index to the integer
12646 -- literal provided as an attribute expression, or to Empty if none.
12647 -- Also returns True if the expression is a generated type conversion
12648 -- whose expression is of the desired form. This latter case arises
12649 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
12650 -- to check for being in range, which is not needed in this context.
12651 -- Returns False if neither condition holds.
12653 function Prepare_64
(N
: Node_Id
) return Node_Id
;
12654 -- Given a discrete expression, returns a Long_Long_Integer typed
12655 -- expression representing the underlying value of the expression.
12656 -- This is done with an unchecked conversion to the result type. We
12657 -- use unchecked conversion to handle the enumeration type case.
12659 ----------------------
12660 -- Is_Entity_Length --
12661 ----------------------
12663 function Is_Entity_Length
(N
: Node_Id
) return Boolean is
12665 if Nkind
(N
) = N_Attribute_Reference
12666 and then Attribute_Name
(N
) = Name_Length
12667 and then Is_Entity_Name
(Prefix
(N
))
12669 Ent
:= Entity
(Prefix
(N
));
12671 if Present
(Expressions
(N
)) then
12672 Index
:= First
(Expressions
(N
));
12679 elsif Nkind
(N
) = N_Type_Conversion
12680 and then not Comes_From_Source
(N
)
12682 return Is_Entity_Length
(Expression
(N
));
12687 end Is_Entity_Length
;
12689 --------------------
12690 -- Is_Optimizable --
12691 --------------------
12693 function Is_Optimizable
(N
: Node_Id
) return Boolean is
12701 if Compile_Time_Known_Value
(N
) then
12702 Val
:= Expr_Value
(N
);
12704 if Val
= Uint_0
then
12709 elsif Val
= Uint_1
then
12716 -- Here we have to make sure of being within 32-bits
12718 Determine_Range
(N
, OK
, Lo
, Hi
, Assume_Valid
=> True);
12721 or else Lo
< Uint_1
12722 or else Hi
> UI_From_Int
(Int
'Last)
12727 -- Comparison value was within range, so now we must check the index
12728 -- value to make sure it is also within 32-bits.
12730 Indx
:= First_Index
(Etype
(Ent
));
12732 if Present
(Index
) then
12733 for J
in 2 .. UI_To_Int
(Intval
(Index
)) loop
12738 Ityp
:= Etype
(Indx
);
12740 if Esize
(Ityp
) > 32 then
12747 end Is_Optimizable
;
12753 function Prepare_64
(N
: Node_Id
) return Node_Id
is
12755 return Unchecked_Convert_To
(Standard_Long_Long_Integer
, N
);
12758 -- Start of processing for Optimize_Length_Comparison
12761 -- Nothing to do if not a comparison
12763 if Op
not in N_Op_Compare
then
12767 -- Nothing to do if special -gnatd.P debug flag set.
12769 if Debug_Flag_Dot_PP
then
12773 -- Ent'Length op 0/1
12775 if Is_Entity_Length
(Left_Opnd
(N
))
12776 and then Is_Optimizable
(Right_Opnd
(N
))
12780 -- 0/1 op Ent'Length
12782 elsif Is_Entity_Length
(Right_Opnd
(N
))
12783 and then Is_Optimizable
(Left_Opnd
(N
))
12785 -- Flip comparison to opposite sense
12788 when N_Op_Lt
=> Op
:= N_Op_Gt
;
12789 when N_Op_Le
=> Op
:= N_Op_Ge
;
12790 when N_Op_Gt
=> Op
:= N_Op_Lt
;
12791 when N_Op_Ge
=> Op
:= N_Op_Le
;
12792 when others => null;
12795 -- Else optimization not possible
12801 -- Fall through if we will do the optimization
12803 -- Cases to handle:
12805 -- X'Length = 0 => X'First > X'Last
12806 -- X'Length = 1 => X'First = X'Last
12807 -- X'Length = n => X'First + (n - 1) = X'Last
12809 -- X'Length /= 0 => X'First <= X'Last
12810 -- X'Length /= 1 => X'First /= X'Last
12811 -- X'Length /= n => X'First + (n - 1) /= X'Last
12813 -- X'Length >= 0 => always true, warn
12814 -- X'Length >= 1 => X'First <= X'Last
12815 -- X'Length >= n => X'First + (n - 1) <= X'Last
12817 -- X'Length > 0 => X'First <= X'Last
12818 -- X'Length > 1 => X'First < X'Last
12819 -- X'Length > n => X'First + (n - 1) < X'Last
12821 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
12822 -- X'Length <= 1 => X'First >= X'Last
12823 -- X'Length <= n => X'First + (n - 1) >= X'Last
12825 -- X'Length < 0 => always false (warn)
12826 -- X'Length < 1 => X'First > X'Last
12827 -- X'Length < n => X'First + (n - 1) > X'Last
12829 -- Note: for the cases of n (not constant 0,1), we require that the
12830 -- corresponding index type be integer or shorter (i.e. not 64-bit),
12831 -- and the same for the comparison value. Then we do the comparison
12832 -- using 64-bit arithmetic (actually long long integer), so that we
12833 -- cannot have overflow intefering with the result.
12835 -- First deal with warning cases
12844 Convert_To
(Typ
, New_Occurrence_Of
(Standard_True
, Loc
)));
12845 Analyze_And_Resolve
(N
, Typ
);
12846 Warn_On_Known_Condition
(N
);
12853 Convert_To
(Typ
, New_Occurrence_Of
(Standard_False
, Loc
)));
12854 Analyze_And_Resolve
(N
, Typ
);
12855 Warn_On_Known_Condition
(N
);
12859 if Constant_Condition_Warnings
12860 and then Comes_From_Source
(Original_Node
(N
))
12862 Error_Msg_N
("could replace by ""'=""?c?", N
);
12872 -- Build the First reference we will use
12875 Make_Attribute_Reference
(Loc
,
12876 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
12877 Attribute_Name
=> Name_First
);
12879 if Present
(Index
) then
12880 Set_Expressions
(Left
, New_List
(New_Copy
(Index
)));
12883 -- If general value case, then do the addition of (n - 1), and
12884 -- also add the needed conversions to type Long_Long_Integer.
12886 if Present
(Comp
) then
12889 Left_Opnd
=> Prepare_64
(Left
),
12891 Make_Op_Subtract
(Loc
,
12892 Left_Opnd
=> Prepare_64
(Comp
),
12893 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1)));
12896 -- Build the Last reference we will use
12899 Make_Attribute_Reference
(Loc
,
12900 Prefix
=> New_Occurrence_Of
(Ent
, Loc
),
12901 Attribute_Name
=> Name_Last
);
12903 if Present
(Index
) then
12904 Set_Expressions
(Right
, New_List
(New_Copy
(Index
)));
12907 -- If general operand, convert Last reference to Long_Long_Integer
12909 if Present
(Comp
) then
12910 Right
:= Prepare_64
(Right
);
12913 -- Check for cases to optimize
12915 -- X'Length = 0 => X'First > X'Last
12916 -- X'Length < 1 => X'First > X'Last
12917 -- X'Length < n => X'First + (n - 1) > X'Last
12919 if (Is_Zero
and then Op
= N_Op_Eq
)
12920 or else (not Is_Zero
and then Op
= N_Op_Lt
)
12925 Right_Opnd
=> Right
);
12927 -- X'Length = 1 => X'First = X'Last
12928 -- X'Length = n => X'First + (n - 1) = X'Last
12930 elsif not Is_Zero
and then Op
= N_Op_Eq
then
12934 Right_Opnd
=> Right
);
12936 -- X'Length /= 0 => X'First <= X'Last
12937 -- X'Length > 0 => X'First <= X'Last
12939 elsif Is_Zero
and (Op
= N_Op_Ne
or else Op
= N_Op_Gt
) then
12943 Right_Opnd
=> Right
);
12945 -- X'Length /= 1 => X'First /= X'Last
12946 -- X'Length /= n => X'First + (n - 1) /= X'Last
12948 elsif not Is_Zero
and then Op
= N_Op_Ne
then
12952 Right_Opnd
=> Right
);
12954 -- X'Length >= 1 => X'First <= X'Last
12955 -- X'Length >= n => X'First + (n - 1) <= X'Last
12957 elsif not Is_Zero
and then Op
= N_Op_Ge
then
12961 Right_Opnd
=> Right
);
12963 -- X'Length > 1 => X'First < X'Last
12964 -- X'Length > n => X'First + (n = 1) < X'Last
12966 elsif not Is_Zero
and then Op
= N_Op_Gt
then
12970 Right_Opnd
=> Right
);
12972 -- X'Length <= 1 => X'First >= X'Last
12973 -- X'Length <= n => X'First + (n - 1) >= X'Last
12975 elsif not Is_Zero
and then Op
= N_Op_Le
then
12979 Right_Opnd
=> Right
);
12981 -- Should not happen at this stage
12984 raise Program_Error
;
12987 -- Rewrite and finish up
12989 Rewrite
(N
, Result
);
12990 Analyze_And_Resolve
(N
, Typ
);
12992 end Optimize_Length_Comparison
;
12994 --------------------------------
12995 -- Process_If_Case_Statements --
12996 --------------------------------
12998 procedure Process_If_Case_Statements
(N
: Node_Id
; Stmts
: List_Id
) is
13002 Decl
:= First
(Stmts
);
13003 while Present
(Decl
) loop
13004 if Nkind
(Decl
) = N_Object_Declaration
13005 and then Is_Finalizable_Transient
(Decl
, N
)
13007 Process_Transient_In_Expression
(Decl
, N
, Stmts
);
13012 end Process_If_Case_Statements
;
13014 -------------------------------------
13015 -- Process_Transient_In_Expression --
13016 -------------------------------------
13018 procedure Process_Transient_In_Expression
13019 (Obj_Decl
: Node_Id
;
13023 Loc
: constant Source_Ptr
:= Sloc
(Obj_Decl
);
13024 Obj_Id
: constant Entity_Id
:= Defining_Identifier
(Obj_Decl
);
13026 Hook_Context
: constant Node_Id
:= Find_Hook_Context
(Expr
);
13027 -- The node on which to insert the hook as an action. This is usually
13028 -- the innermost enclosing non-transient construct.
13030 Fin_Call
: Node_Id
;
13031 Hook_Assign
: Node_Id
;
13032 Hook_Clear
: Node_Id
;
13033 Hook_Decl
: Node_Id
;
13034 Hook_Insert
: Node_Id
;
13035 Ptr_Decl
: Node_Id
;
13037 Fin_Context
: Node_Id
;
13038 -- The node after which to insert the finalization actions of the
13039 -- transient object.
13042 pragma Assert
(Nkind_In
(Expr
, N_Case_Expression
,
13043 N_Expression_With_Actions
,
13046 -- When the context is a Boolean evaluation, all three nodes capture the
13047 -- result of their computation in a local temporary:
13050 -- Trans_Id : Ctrl_Typ := ...;
13051 -- Result : constant Boolean := ... Trans_Id ...;
13052 -- <finalize Trans_Id>
13055 -- As a result, the finalization of any transient objects can safely
13056 -- take place after the result capture.
13058 -- ??? could this be extended to elementary types?
13060 if Is_Boolean_Type
(Etype
(Expr
)) then
13061 Fin_Context
:= Last
(Stmts
);
13063 -- Otherwise the immediate context may not be safe enough to carry
13064 -- out transient object finalization due to aliasing and nesting of
13065 -- constructs. Insert calls to [Deep_]Finalize after the innermost
13066 -- enclosing non-transient construct.
13069 Fin_Context
:= Hook_Context
;
13072 -- Mark the transient object as successfully processed to avoid double
13075 Set_Is_Finalized_Transient
(Obj_Id
);
13077 -- Construct all the pieces necessary to hook and finalize a transient
13080 Build_Transient_Object_Statements
13081 (Obj_Decl
=> Obj_Decl
,
13082 Fin_Call
=> Fin_Call
,
13083 Hook_Assign
=> Hook_Assign
,
13084 Hook_Clear
=> Hook_Clear
,
13085 Hook_Decl
=> Hook_Decl
,
13086 Ptr_Decl
=> Ptr_Decl
,
13087 Finalize_Obj
=> False);
13089 -- Add the access type which provides a reference to the transient
13090 -- object. Generate:
13092 -- type Ptr_Typ is access all Desig_Typ;
13094 Insert_Action
(Hook_Context
, Ptr_Decl
);
13096 -- Add the temporary which acts as a hook to the transient object.
13099 -- Hook : Ptr_Id := null;
13101 Insert_Action
(Hook_Context
, Hook_Decl
);
13103 -- When the transient object is initialized by an aggregate, the hook
13104 -- must capture the object after the last aggregate assignment takes
13105 -- place. Only then is the object considered initialized. Generate:
13107 -- Hook := Ptr_Typ (Obj_Id);
13109 -- Hook := Obj_Id'Unrestricted_Access;
13111 if Ekind_In
(Obj_Id
, E_Constant
, E_Variable
)
13112 and then Present
(Last_Aggregate_Assignment
(Obj_Id
))
13114 Hook_Insert
:= Last_Aggregate_Assignment
(Obj_Id
);
13116 -- Otherwise the hook seizes the related object immediately
13119 Hook_Insert
:= Obj_Decl
;
13122 Insert_After_And_Analyze
(Hook_Insert
, Hook_Assign
);
13124 -- When the node is part of a return statement, there is no need to
13125 -- insert a finalization call, as the general finalization mechanism
13126 -- (see Build_Finalizer) would take care of the transient object on
13127 -- subprogram exit. Note that it would also be impossible to insert the
13128 -- finalization code after the return statement as this will render it
13131 if Nkind
(Fin_Context
) = N_Simple_Return_Statement
then
13134 -- Finalize the hook after the context has been evaluated. Generate:
13136 -- if Hook /= null then
13137 -- [Deep_]Finalize (Hook.all);
13142 Insert_Action_After
(Fin_Context
,
13143 Make_Implicit_If_Statement
(Obj_Decl
,
13147 New_Occurrence_Of
(Defining_Entity
(Hook_Decl
), Loc
),
13148 Right_Opnd
=> Make_Null
(Loc
)),
13150 Then_Statements
=> New_List
(
13154 end Process_Transient_In_Expression
;
13156 ------------------------
13157 -- Rewrite_Comparison --
13158 ------------------------
13160 procedure Rewrite_Comparison
(N
: Node_Id
) is
13161 Warning_Generated
: Boolean := False;
13162 -- Set to True if first pass with Assume_Valid generates a warning in
13163 -- which case we skip the second pass to avoid warning overloaded.
13166 -- Set to Standard_True or Standard_False
13169 if Nkind
(N
) = N_Type_Conversion
then
13170 Rewrite_Comparison
(Expression
(N
));
13173 elsif Nkind
(N
) not in N_Op_Compare
then
13177 -- Now start looking at the comparison in detail. We potentially go
13178 -- through this loop twice. The first time, Assume_Valid is set False
13179 -- in the call to Compile_Time_Compare. If this call results in a
13180 -- clear result of always True or Always False, that's decisive and
13181 -- we are done. Otherwise we repeat the processing with Assume_Valid
13182 -- set to True to generate additional warnings. We can skip that step
13183 -- if Constant_Condition_Warnings is False.
13185 for AV
in False .. True loop
13187 Typ
: constant Entity_Id
:= Etype
(N
);
13188 Op1
: constant Node_Id
:= Left_Opnd
(N
);
13189 Op2
: constant Node_Id
:= Right_Opnd
(N
);
13191 Res
: constant Compare_Result
:=
13192 Compile_Time_Compare
(Op1
, Op2
, Assume_Valid
=> AV
);
13193 -- Res indicates if compare outcome can be compile time determined
13195 True_Result
: Boolean;
13196 False_Result
: Boolean;
13199 case N_Op_Compare
(Nkind
(N
)) is
13201 True_Result
:= Res
= EQ
;
13202 False_Result
:= Res
= LT
or else Res
= GT
or else Res
= NE
;
13205 True_Result
:= Res
in Compare_GE
;
13206 False_Result
:= Res
= LT
;
13209 and then Constant_Condition_Warnings
13210 and then Comes_From_Source
(Original_Node
(N
))
13211 and then Nkind
(Original_Node
(N
)) = N_Op_Ge
13212 and then not In_Instance
13213 and then Is_Integer_Type
(Etype
(Left_Opnd
(N
)))
13214 and then not Has_Warnings_Off
(Etype
(Left_Opnd
(N
)))
13217 ("can never be greater than, could replace by "
13219 Warning_Generated
:= True;
13223 True_Result
:= Res
= GT
;
13224 False_Result
:= Res
in Compare_LE
;
13227 True_Result
:= Res
= LT
;
13228 False_Result
:= Res
in Compare_GE
;
13231 True_Result
:= Res
in Compare_LE
;
13232 False_Result
:= Res
= GT
;
13235 and then Constant_Condition_Warnings
13236 and then Comes_From_Source
(Original_Node
(N
))
13237 and then Nkind
(Original_Node
(N
)) = N_Op_Le
13238 and then not In_Instance
13239 and then Is_Integer_Type
(Etype
(Left_Opnd
(N
)))
13240 and then not Has_Warnings_Off
(Etype
(Left_Opnd
(N
)))
13243 ("can never be less than, could replace by ""'=""?c?",
13245 Warning_Generated
:= True;
13249 True_Result
:= Res
= NE
or else Res
= GT
or else Res
= LT
;
13250 False_Result
:= Res
= EQ
;
13253 -- If this is the first iteration, then we actually convert the
13254 -- comparison into True or False, if the result is certain.
13257 if True_Result
or False_Result
then
13258 Result
:= Boolean_Literals
(True_Result
);
13261 New_Occurrence_Of
(Result
, Sloc
(N
))));
13262 Analyze_And_Resolve
(N
, Typ
);
13263 Warn_On_Known_Condition
(N
);
13267 -- If this is the second iteration (AV = True), and the original
13268 -- node comes from source and we are not in an instance, then give
13269 -- a warning if we know result would be True or False. Note: we
13270 -- know Constant_Condition_Warnings is set if we get here.
13272 elsif Comes_From_Source
(Original_Node
(N
))
13273 and then not In_Instance
13275 if True_Result
then
13277 ("condition can only be False if invalid values present??",
13279 elsif False_Result
then
13281 ("condition can only be True if invalid values present??",
13287 -- Skip second iteration if not warning on constant conditions or
13288 -- if the first iteration already generated a warning of some kind or
13289 -- if we are in any case assuming all values are valid (so that the
13290 -- first iteration took care of the valid case).
13292 exit when not Constant_Condition_Warnings
;
13293 exit when Warning_Generated
;
13294 exit when Assume_No_Invalid_Values
;
13296 end Rewrite_Comparison
;
13298 ----------------------------
13299 -- Safe_In_Place_Array_Op --
13300 ----------------------------
13302 function Safe_In_Place_Array_Op
13305 Op2
: Node_Id
) return Boolean
13307 Target
: Entity_Id
;
13309 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean;
13310 -- Operand is safe if it cannot overlap part of the target of the
13311 -- operation. If the operand and the target are identical, the operand
13312 -- is safe. The operand can be empty in the case of negation.
13314 function Is_Unaliased
(N
: Node_Id
) return Boolean;
13315 -- Check that N is a stand-alone entity
13321 function Is_Unaliased
(N
: Node_Id
) return Boolean is
13325 and then No
(Address_Clause
(Entity
(N
)))
13326 and then No
(Renamed_Object
(Entity
(N
)));
13329 ---------------------
13330 -- Is_Safe_Operand --
13331 ---------------------
13333 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean is
13338 elsif Is_Entity_Name
(Op
) then
13339 return Is_Unaliased
(Op
);
13341 elsif Nkind_In
(Op
, N_Indexed_Component
, N_Selected_Component
) then
13342 return Is_Unaliased
(Prefix
(Op
));
13344 elsif Nkind
(Op
) = N_Slice
then
13346 Is_Unaliased
(Prefix
(Op
))
13347 and then Entity
(Prefix
(Op
)) /= Target
;
13349 elsif Nkind
(Op
) = N_Op_Not
then
13350 return Is_Safe_Operand
(Right_Opnd
(Op
));
13355 end Is_Safe_Operand
;
13357 -- Start of processing for Safe_In_Place_Array_Op
13360 -- Skip this processing if the component size is different from system
13361 -- storage unit (since at least for NOT this would cause problems).
13363 if Component_Size
(Etype
(Lhs
)) /= System_Storage_Unit
then
13366 -- Cannot do in place stuff if non-standard Boolean representation
13368 elsif Has_Non_Standard_Rep
(Component_Type
(Etype
(Lhs
))) then
13371 elsif not Is_Unaliased
(Lhs
) then
13375 Target
:= Entity
(Lhs
);
13376 return Is_Safe_Operand
(Op1
) and then Is_Safe_Operand
(Op2
);
13378 end Safe_In_Place_Array_Op
;
13380 -----------------------
13381 -- Tagged_Membership --
13382 -----------------------
13384 -- There are two different cases to consider depending on whether the right
13385 -- operand is a class-wide type or not. If not we just compare the actual
13386 -- tag of the left expr to the target type tag:
13388 -- Left_Expr.Tag = Right_Type'Tag;
13390 -- If it is a class-wide type we use the RT function CW_Membership which is
13391 -- usually implemented by looking in the ancestor tables contained in the
13392 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
13394 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
13395 -- function IW_Membership which is usually implemented by looking in the
13396 -- table of abstract interface types plus the ancestor table contained in
13397 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
13399 procedure Tagged_Membership
13401 SCIL_Node
: out Node_Id
;
13402 Result
: out Node_Id
)
13404 Left
: constant Node_Id
:= Left_Opnd
(N
);
13405 Right
: constant Node_Id
:= Right_Opnd
(N
);
13406 Loc
: constant Source_Ptr
:= Sloc
(N
);
13408 Full_R_Typ
: Entity_Id
;
13409 Left_Type
: Entity_Id
;
13410 New_Node
: Node_Id
;
13411 Right_Type
: Entity_Id
;
13415 SCIL_Node
:= Empty
;
13417 -- Handle entities from the limited view
13419 Left_Type
:= Available_View
(Etype
(Left
));
13420 Right_Type
:= Available_View
(Etype
(Right
));
13422 -- In the case where the type is an access type, the test is applied
13423 -- using the designated types (needed in Ada 2012 for implicit anonymous
13424 -- access conversions, for AI05-0149).
13426 if Is_Access_Type
(Right_Type
) then
13427 Left_Type
:= Designated_Type
(Left_Type
);
13428 Right_Type
:= Designated_Type
(Right_Type
);
13431 if Is_Class_Wide_Type
(Left_Type
) then
13432 Left_Type
:= Root_Type
(Left_Type
);
13435 if Is_Class_Wide_Type
(Right_Type
) then
13436 Full_R_Typ
:= Underlying_Type
(Root_Type
(Right_Type
));
13438 Full_R_Typ
:= Underlying_Type
(Right_Type
);
13442 Make_Selected_Component
(Loc
,
13443 Prefix
=> Relocate_Node
(Left
),
13445 New_Occurrence_Of
(First_Tag_Component
(Left_Type
), Loc
));
13447 if Is_Class_Wide_Type
(Right_Type
) then
13449 -- No need to issue a run-time check if we statically know that the
13450 -- result of this membership test is always true. For example,
13451 -- considering the following declarations:
13453 -- type Iface is interface;
13454 -- type T is tagged null record;
13455 -- type DT is new T and Iface with null record;
13460 -- These membership tests are always true:
13463 -- Obj2 in T'Class;
13464 -- Obj2 in Iface'Class;
13466 -- We do not need to handle cases where the membership is illegal.
13469 -- Obj1 in DT'Class; -- Compile time error
13470 -- Obj1 in Iface'Class; -- Compile time error
13472 if not Is_Class_Wide_Type
(Left_Type
)
13473 and then (Is_Ancestor
(Etype
(Right_Type
), Left_Type
,
13474 Use_Full_View
=> True)
13475 or else (Is_Interface
(Etype
(Right_Type
))
13476 and then Interface_Present_In_Ancestor
13478 Iface
=> Etype
(Right_Type
))))
13480 Result
:= New_Occurrence_Of
(Standard_True
, Loc
);
13484 -- Ada 2005 (AI-251): Class-wide applied to interfaces
13486 if Is_Interface
(Etype
(Class_Wide_Type
(Right_Type
)))
13488 -- Support to: "Iface_CW_Typ in Typ'Class"
13490 or else Is_Interface
(Left_Type
)
13492 -- Issue error if IW_Membership operation not available in a
13493 -- configurable run time setting.
13495 if not RTE_Available
(RE_IW_Membership
) then
13497 ("dynamic membership test on interface types", N
);
13503 Make_Function_Call
(Loc
,
13504 Name
=> New_Occurrence_Of
(RTE
(RE_IW_Membership
), Loc
),
13505 Parameter_Associations
=> New_List
(
13506 Make_Attribute_Reference
(Loc
,
13508 Attribute_Name
=> Name_Address
),
13509 New_Occurrence_Of
(
13510 Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))),
13513 -- Ada 95: Normal case
13516 Build_CW_Membership
(Loc
,
13517 Obj_Tag_Node
=> Obj_Tag
,
13519 New_Occurrence_Of
(
13520 Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))), Loc
),
13522 New_Node
=> New_Node
);
13524 -- Generate the SCIL node for this class-wide membership test.
13525 -- Done here because the previous call to Build_CW_Membership
13526 -- relocates Obj_Tag.
13528 if Generate_SCIL
then
13529 SCIL_Node
:= Make_SCIL_Membership_Test
(Sloc
(N
));
13530 Set_SCIL_Entity
(SCIL_Node
, Etype
(Right_Type
));
13531 Set_SCIL_Tag_Value
(SCIL_Node
, Obj_Tag
);
13534 Result
:= New_Node
;
13537 -- Right_Type is not a class-wide type
13540 -- No need to check the tag of the object if Right_Typ is abstract
13542 if Is_Abstract_Type
(Right_Type
) then
13543 Result
:= New_Occurrence_Of
(Standard_False
, Loc
);
13548 Left_Opnd
=> Obj_Tag
,
13551 (Node
(First_Elmt
(Access_Disp_Table
(Full_R_Typ
))), Loc
));
13554 end Tagged_Membership
;
13556 ------------------------------
13557 -- Unary_Op_Validity_Checks --
13558 ------------------------------
13560 procedure Unary_Op_Validity_Checks
(N
: Node_Id
) is
13562 if Validity_Checks_On
and Validity_Check_Operands
then
13563 Ensure_Valid
(Right_Opnd
(N
));
13565 end Unary_Op_Validity_Checks
;