1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree
; use Atree
;
28 with Checks
; use Checks
;
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_Ch3
; use Exp_Ch3
;
34 with Exp_Ch7
; use Exp_Ch7
;
35 with Exp_Ch9
; use Exp_Ch9
;
36 with Exp_Fixd
; use Exp_Fixd
;
37 with Exp_Pakd
; use Exp_Pakd
;
38 with Exp_Tss
; use Exp_Tss
;
39 with Exp_Util
; use Exp_Util
;
40 with Exp_VFpt
; use Exp_VFpt
;
41 with Freeze
; use Freeze
;
42 with Hostparm
; use Hostparm
;
43 with Inline
; use Inline
;
44 with Nlists
; use Nlists
;
45 with Nmake
; use Nmake
;
47 with Rtsfind
; use Rtsfind
;
49 with Sem_Cat
; use Sem_Cat
;
50 with Sem_Ch3
; use Sem_Ch3
;
51 with Sem_Ch13
; use Sem_Ch13
;
52 with Sem_Eval
; use Sem_Eval
;
53 with Sem_Res
; use Sem_Res
;
54 with Sem_Type
; use Sem_Type
;
55 with Sem_Util
; use Sem_Util
;
56 with Sem_Warn
; use Sem_Warn
;
57 with Sinfo
; use Sinfo
;
58 with Snames
; use Snames
;
59 with Stand
; use Stand
;
60 with Targparm
; use Targparm
;
61 with Tbuild
; use Tbuild
;
62 with Ttypes
; use Ttypes
;
63 with Uintp
; use Uintp
;
64 with Urealp
; use Urealp
;
65 with Validsw
; use Validsw
;
67 package body Exp_Ch4
is
69 -----------------------
70 -- Local Subprograms --
71 -----------------------
73 procedure Binary_Op_Validity_Checks
(N
: Node_Id
);
74 pragma Inline
(Binary_Op_Validity_Checks
);
75 -- Performs validity checks for a binary operator
77 procedure Build_Boolean_Array_Proc_Call
81 -- If an boolean array assignment can be done in place, build call to
82 -- corresponding library procedure.
84 procedure Expand_Allocator_Expression
(N
: Node_Id
);
85 -- Subsidiary to Expand_N_Allocator, for the case when the expression
86 -- is a qualified expression or an aggregate.
88 procedure Expand_Array_Comparison
(N
: Node_Id
);
89 -- This routine handles expansion of the comparison operators (N_Op_Lt,
90 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
91 -- code for these operators is similar, differing only in the details of
92 -- the actual comparison call that is made. Special processing (call a
95 function Expand_Array_Equality
100 Typ
: Entity_Id
) return Node_Id
;
101 -- Expand an array equality into a call to a function implementing this
102 -- equality, and a call to it. Loc is the location for the generated
103 -- nodes. Lhs and Rhs are the array expressions to be compared.
104 -- Bodies is a list on which to attach bodies of local functions that
105 -- are created in the process. It is the responsibility of the
106 -- caller to insert those bodies at the right place. Nod provides
107 -- the Sloc value for the generated code. Normally the types used
108 -- for the generated equality routine are taken from Lhs and Rhs.
109 -- However, in some situations of generated code, the Etype fields
110 -- of Lhs and Rhs are not set yet. In such cases, Typ supplies the
111 -- type to be used for the formal parameters.
113 procedure Expand_Boolean_Operator
(N
: Node_Id
);
114 -- Common expansion processing for Boolean operators (And, Or, Xor)
115 -- for the case of array type arguments.
117 function Expand_Composite_Equality
122 Bodies
: List_Id
) return Node_Id
;
123 -- Local recursive function used to expand equality for nested
124 -- composite types. Used by Expand_Record/Array_Equality, Bodies
125 -- is a list on which to attach bodies of local functions that are
126 -- created in the process. This is the responsability of the caller
127 -- to insert those bodies at the right place. Nod provides the Sloc
128 -- value for generated code. Lhs and Rhs are the left and right sides
129 -- for the comparison, and Typ is the type of the arrays to compare.
131 procedure Expand_Concatenate_Other
(Cnode
: Node_Id
; Opnds
: List_Id
);
132 -- This routine handles expansion of concatenation operations, where
133 -- N is the N_Op_Concat node being expanded and Operands is the list
134 -- of operands (at least two are present). The caller has dealt with
135 -- converting any singleton operands into singleton aggregates.
137 procedure Expand_Concatenate_String
(Cnode
: Node_Id
; Opnds
: List_Id
);
138 -- Routine to expand concatenation of 2-5 operands (in the list Operands)
139 -- and replace node Cnode with the result of the contatenation. If there
140 -- are two operands, they can be string or character. If there are more
141 -- than two operands, then are always of type string (i.e. the caller has
142 -- already converted character operands to strings in this case).
144 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
);
145 -- N is either an N_Op_Divide or N_Op_Multiply node whose result is
146 -- universal fixed. We do not have such a type at runtime, so the
147 -- purpose of this routine is to find the real type by looking up
148 -- the tree. We also determine if the operation must be rounded.
150 function Get_Allocator_Final_List
153 PtrT
: Entity_Id
) return Entity_Id
;
154 -- If the designated type is controlled, build final_list expression
155 -- for created object. If context is an access parameter, create a
156 -- local access type to have a usable finalization list.
158 function Has_Inferable_Discriminants
(N
: Node_Id
) return Boolean;
159 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
160 -- discriminants if it has a constrained nominal type, unless the object
161 -- is a component of an enclosing Unchecked_Union object that is subject
162 -- to a per-object constraint and the enclosing object lacks inferable
165 -- An expression of an Unchecked_Union type has inferable discriminants
166 -- if it is either a name of an object with inferable discriminants or a
167 -- qualified expression whose subtype mark denotes a constrained subtype.
169 procedure Insert_Dereference_Action
(N
: Node_Id
);
170 -- N is an expression whose type is an access. When the type of the
171 -- associated storage pool is derived from Checked_Pool, generate a
172 -- call to the 'Dereference' primitive operation.
174 function Make_Array_Comparison_Op
176 Nod
: Node_Id
) return Node_Id
;
177 -- Comparisons between arrays are expanded in line. This function
178 -- produces the body of the implementation of (a > b), where a and b
179 -- are one-dimensional arrays of some discrete type. The original
180 -- node is then expanded into the appropriate call to this function.
181 -- Nod provides the Sloc value for the generated code.
183 function Make_Boolean_Array_Op
185 N
: Node_Id
) return Node_Id
;
186 -- Boolean operations on boolean arrays are expanded in line. This
187 -- function produce the body for the node N, which is (a and b),
188 -- (a or b), or (a xor b). It is used only the normal case and not
189 -- the packed case. The type involved, Typ, is the Boolean array type,
190 -- and the logical operations in the body are simple boolean operations.
191 -- Note that Typ is always a constrained type (the caller has ensured
192 -- this by using Convert_To_Actual_Subtype if necessary).
194 procedure Rewrite_Comparison
(N
: Node_Id
);
195 -- if N is the node for a comparison whose outcome can be determined at
196 -- compile time, then the node N can be rewritten with True or False. If
197 -- the outcome cannot be determined at compile time, the call has no
198 -- effect. If N is a type conversion, then this processing is applied to
199 -- its expression. If N is neither comparison nor a type conversion, the
200 -- call has no effect.
202 function Tagged_Membership
(N
: Node_Id
) return Node_Id
;
203 -- Construct the expression corresponding to the tagged membership test.
204 -- Deals with a second operand being (or not) a class-wide type.
206 function Safe_In_Place_Array_Op
209 Op2
: Node_Id
) return Boolean;
210 -- In the context of an assignment, where the right-hand side is a
211 -- boolean operation on arrays, check whether operation can be performed
214 procedure Unary_Op_Validity_Checks
(N
: Node_Id
);
215 pragma Inline
(Unary_Op_Validity_Checks
);
216 -- Performs validity checks for a unary operator
218 -------------------------------
219 -- Binary_Op_Validity_Checks --
220 -------------------------------
222 procedure Binary_Op_Validity_Checks
(N
: Node_Id
) is
224 if Validity_Checks_On
and Validity_Check_Operands
then
225 Ensure_Valid
(Left_Opnd
(N
));
226 Ensure_Valid
(Right_Opnd
(N
));
228 end Binary_Op_Validity_Checks
;
230 ------------------------------------
231 -- Build_Boolean_Array_Proc_Call --
232 ------------------------------------
234 procedure Build_Boolean_Array_Proc_Call
239 Loc
: constant Source_Ptr
:= Sloc
(N
);
240 Kind
: constant Node_Kind
:= Nkind
(Expression
(N
));
241 Target
: constant Node_Id
:=
242 Make_Attribute_Reference
(Loc
,
244 Attribute_Name
=> Name_Address
);
246 Arg1
: constant Node_Id
:= Op1
;
247 Arg2
: Node_Id
:= Op2
;
249 Proc_Name
: Entity_Id
;
252 if Kind
= N_Op_Not
then
253 if Nkind
(Op1
) in N_Binary_Op
then
255 -- Use negated version of the binary operators
257 if Nkind
(Op1
) = N_Op_And
then
258 Proc_Name
:= RTE
(RE_Vector_Nand
);
260 elsif Nkind
(Op1
) = N_Op_Or
then
261 Proc_Name
:= RTE
(RE_Vector_Nor
);
263 else pragma Assert
(Nkind
(Op1
) = N_Op_Xor
);
264 Proc_Name
:= RTE
(RE_Vector_Xor
);
268 Make_Procedure_Call_Statement
(Loc
,
269 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
271 Parameter_Associations
=> New_List
(
273 Make_Attribute_Reference
(Loc
,
274 Prefix
=> Left_Opnd
(Op1
),
275 Attribute_Name
=> Name_Address
),
277 Make_Attribute_Reference
(Loc
,
278 Prefix
=> Right_Opnd
(Op1
),
279 Attribute_Name
=> Name_Address
),
281 Make_Attribute_Reference
(Loc
,
282 Prefix
=> Left_Opnd
(Op1
),
283 Attribute_Name
=> Name_Length
)));
286 Proc_Name
:= RTE
(RE_Vector_Not
);
289 Make_Procedure_Call_Statement
(Loc
,
290 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
291 Parameter_Associations
=> New_List
(
294 Make_Attribute_Reference
(Loc
,
296 Attribute_Name
=> Name_Address
),
298 Make_Attribute_Reference
(Loc
,
300 Attribute_Name
=> Name_Length
)));
304 -- We use the following equivalences:
306 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
307 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
308 -- (not X) xor (not Y) = X xor Y
309 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
311 if Nkind
(Op1
) = N_Op_Not
then
312 if Kind
= N_Op_And
then
313 Proc_Name
:= RTE
(RE_Vector_Nor
);
315 elsif Kind
= N_Op_Or
then
316 Proc_Name
:= RTE
(RE_Vector_Nand
);
319 Proc_Name
:= RTE
(RE_Vector_Xor
);
323 if Kind
= N_Op_And
then
324 Proc_Name
:= RTE
(RE_Vector_And
);
326 elsif Kind
= N_Op_Or
then
327 Proc_Name
:= RTE
(RE_Vector_Or
);
329 elsif Nkind
(Op2
) = N_Op_Not
then
330 Proc_Name
:= RTE
(RE_Vector_Nxor
);
331 Arg2
:= Right_Opnd
(Op2
);
334 Proc_Name
:= RTE
(RE_Vector_Xor
);
339 Make_Procedure_Call_Statement
(Loc
,
340 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
341 Parameter_Associations
=> New_List
(
343 Make_Attribute_Reference
(Loc
,
345 Attribute_Name
=> Name_Address
),
346 Make_Attribute_Reference
(Loc
,
348 Attribute_Name
=> Name_Address
),
349 Make_Attribute_Reference
(Loc
,
351 Attribute_Name
=> Name_Length
)));
354 Rewrite
(N
, Call_Node
);
358 when RE_Not_Available
=>
360 end Build_Boolean_Array_Proc_Call
;
362 ---------------------------------
363 -- Expand_Allocator_Expression --
364 ---------------------------------
366 procedure Expand_Allocator_Expression
(N
: Node_Id
) is
367 Loc
: constant Source_Ptr
:= Sloc
(N
);
368 Exp
: constant Node_Id
:= Expression
(Expression
(N
));
369 Indic
: constant Node_Id
:= Subtype_Mark
(Expression
(N
));
370 PtrT
: constant Entity_Id
:= Etype
(N
);
371 DesigT
: constant Entity_Id
:= Designated_Type
(PtrT
);
372 T
: constant Entity_Id
:= Entity
(Indic
);
377 TagT
: Entity_Id
:= Empty
;
378 -- Type used as source for tag assignment
380 TagR
: Node_Id
:= Empty
;
381 -- Target reference for tag assignment
383 Aggr_In_Place
: constant Boolean := Is_Delayed_Aggregate
(Exp
);
385 Tag_Assign
: Node_Id
;
389 if Is_Tagged_Type
(T
) or else Controlled_Type
(T
) then
391 -- Actions inserted before:
392 -- Temp : constant ptr_T := new T'(Expression);
393 -- <no CW> Temp._tag := T'tag;
394 -- <CTRL> Adjust (Finalizable (Temp.all));
395 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
397 -- We analyze by hand the new internal allocator to avoid
398 -- any recursion and inappropriate call to Initialize
400 if not Aggr_In_Place
then
401 Remove_Side_Effects
(Exp
);
405 Make_Defining_Identifier
(Loc
, New_Internal_Name
('P'));
407 -- For a class wide allocation generate the following code:
409 -- type Equiv_Record is record ... end record;
410 -- implicit subtype CW is <Class_Wide_Subytpe>;
411 -- temp : PtrT := new CW'(CW!(expr));
413 if Is_Class_Wide_Type
(T
) then
414 Expand_Subtype_From_Expr
(Empty
, T
, Indic
, Exp
);
416 Set_Expression
(Expression
(N
),
417 Unchecked_Convert_To
(Entity
(Indic
), Exp
));
419 Analyze_And_Resolve
(Expression
(N
), Entity
(Indic
));
422 if Aggr_In_Place
then
424 Make_Object_Declaration
(Loc
,
425 Defining_Identifier
=> Temp
,
426 Object_Definition
=> New_Reference_To
(PtrT
, Loc
),
429 New_Reference_To
(Etype
(Exp
), Loc
)));
431 Set_Comes_From_Source
432 (Expression
(Tmp_Node
), Comes_From_Source
(N
));
434 Set_No_Initialization
(Expression
(Tmp_Node
));
435 Insert_Action
(N
, Tmp_Node
);
437 if Controlled_Type
(T
)
438 and then Ekind
(PtrT
) = E_Anonymous_Access_Type
440 -- Create local finalization list for access parameter
442 Flist
:= Get_Allocator_Final_List
(N
, Base_Type
(T
), PtrT
);
445 Convert_Aggr_In_Allocator
(Tmp_Node
, Exp
);
447 Node
:= Relocate_Node
(N
);
450 Make_Object_Declaration
(Loc
,
451 Defining_Identifier
=> Temp
,
452 Constant_Present
=> True,
453 Object_Definition
=> New_Reference_To
(PtrT
, Loc
),
454 Expression
=> Node
));
457 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
458 -- type, generate an accessibility check to verify that the level of
459 -- the type of the created object is not deeper than the level of the
460 -- access type. If the type of the qualified expression is class-
461 -- wide, then always generate the check. Otherwise, only generate the
462 -- check if the level of the qualified expression type is statically
463 -- deeper than the access type. Although the static accessibility
464 -- will generally have been performed as a legality check, it won't
465 -- have been done in cases where the allocator appears in generic
466 -- body, so a run-time check is needed in general.
468 if Ada_Version
>= Ada_05
469 and then Is_Class_Wide_Type
(DesigT
)
470 and then not Scope_Suppress
(Accessibility_Check
)
472 (Is_Class_Wide_Type
(Etype
(Exp
))
474 Type_Access_Level
(Etype
(Exp
)) > Type_Access_Level
(PtrT
))
477 Make_Raise_Program_Error
(Loc
,
481 Make_Function_Call
(Loc
,
483 New_Reference_To
(RTE
(RE_Get_Access_Level
), Loc
),
484 Parameter_Associations
=>
485 New_List
(Make_Attribute_Reference
(Loc
,
487 New_Reference_To
(Temp
, Loc
),
491 Make_Integer_Literal
(Loc
, Type_Access_Level
(PtrT
))),
492 Reason
=> PE_Accessibility_Check_Failed
));
497 -- Suppress the tag assignment when Java_VM because JVM tags are
498 -- represented implicitly in objects.
502 elsif Is_Tagged_Type
(T
) and then not Is_Class_Wide_Type
(T
) then
504 TagR
:= New_Reference_To
(Temp
, Loc
);
506 elsif Is_Private_Type
(T
)
507 and then Is_Tagged_Type
(Underlying_Type
(T
))
509 TagT
:= Underlying_Type
(T
);
511 Unchecked_Convert_To
(Underlying_Type
(T
),
512 Make_Explicit_Dereference
(Loc
,
513 Prefix
=> New_Reference_To
(Temp
, Loc
)));
516 if Present
(TagT
) then
518 Make_Assignment_Statement
(Loc
,
520 Make_Selected_Component
(Loc
,
523 New_Reference_To
(First_Tag_Component
(TagT
), Loc
)),
526 Unchecked_Convert_To
(RTE
(RE_Tag
),
528 (Elists
.Node
(First_Elmt
(Access_Disp_Table
(TagT
))),
531 -- The previous assignment has to be done in any case
533 Set_Assignment_OK
(Name
(Tag_Assign
));
534 Insert_Action
(N
, Tag_Assign
);
537 if Controlled_Type
(DesigT
)
538 and then Controlled_Type
(T
)
542 Apool
: constant Entity_Id
:=
543 Associated_Storage_Pool
(PtrT
);
546 -- If it is an allocation on the secondary stack
547 -- (i.e. a value returned from a function), the object
548 -- is attached on the caller side as soon as the call
549 -- is completed (see Expand_Ctrl_Function_Call)
551 if Is_RTE
(Apool
, RE_SS_Pool
) then
553 F
: constant Entity_Id
:=
554 Make_Defining_Identifier
(Loc
,
555 New_Internal_Name
('F'));
558 Make_Object_Declaration
(Loc
,
559 Defining_Identifier
=> F
,
560 Object_Definition
=> New_Reference_To
(RTE
561 (RE_Finalizable_Ptr
), Loc
)));
563 Flist
:= New_Reference_To
(F
, Loc
);
564 Attach
:= Make_Integer_Literal
(Loc
, 1);
567 -- Normal case, not a secondary stack allocation
570 if Controlled_Type
(T
)
571 and then Ekind
(PtrT
) = E_Anonymous_Access_Type
573 -- Create local finalization list for access parameter
576 Get_Allocator_Final_List
(N
, Base_Type
(T
), PtrT
);
578 Flist
:= Find_Final_List
(PtrT
);
581 Attach
:= Make_Integer_Literal
(Loc
, 2);
584 if not Aggr_In_Place
then
589 -- An unchecked conversion is needed in the
590 -- classwide case because the designated type
591 -- can be an ancestor of the subtype mark of
594 Unchecked_Convert_To
(T
,
595 Make_Explicit_Dereference
(Loc
,
596 Prefix
=> New_Reference_To
(Temp
, Loc
))),
600 With_Attach
=> Attach
,
606 Rewrite
(N
, New_Reference_To
(Temp
, Loc
));
607 Analyze_And_Resolve
(N
, PtrT
);
609 elsif Aggr_In_Place
then
611 Make_Defining_Identifier
(Loc
, New_Internal_Name
('P'));
613 Make_Object_Declaration
(Loc
,
614 Defining_Identifier
=> Temp
,
615 Object_Definition
=> New_Reference_To
(PtrT
, Loc
),
616 Expression
=> Make_Allocator
(Loc
,
617 New_Reference_To
(Etype
(Exp
), Loc
)));
619 Set_Comes_From_Source
620 (Expression
(Tmp_Node
), Comes_From_Source
(N
));
622 Set_No_Initialization
(Expression
(Tmp_Node
));
623 Insert_Action
(N
, Tmp_Node
);
624 Convert_Aggr_In_Allocator
(Tmp_Node
, Exp
);
625 Rewrite
(N
, New_Reference_To
(Temp
, Loc
));
626 Analyze_And_Resolve
(N
, PtrT
);
628 elsif Is_Access_Type
(DesigT
)
629 and then Nkind
(Exp
) = N_Allocator
630 and then Nkind
(Expression
(Exp
)) /= N_Qualified_Expression
632 -- Apply constraint to designated subtype indication
634 Apply_Constraint_Check
(Expression
(Exp
),
635 Designated_Type
(DesigT
),
638 if Nkind
(Expression
(Exp
)) = N_Raise_Constraint_Error
then
640 -- Propagate constraint_error to enclosing allocator
642 Rewrite
(Exp
, New_Copy
(Expression
(Exp
)));
645 -- First check against the type of the qualified expression
647 -- NOTE: The commented call should be correct, but for
648 -- some reason causes the compiler to bomb (sigsegv) on
649 -- ACVC test c34007g, so for now we just perform the old
650 -- (incorrect) test against the designated subtype with
651 -- no sliding in the else part of the if statement below.
654 -- Apply_Constraint_Check (Exp, T, No_Sliding => True);
656 -- A check is also needed in cases where the designated
657 -- subtype is constrained and differs from the subtype
658 -- given in the qualified expression. Note that the check
659 -- on the qualified expression does not allow sliding,
660 -- but this check does (a relaxation from Ada 83).
662 if Is_Constrained
(DesigT
)
663 and then not Subtypes_Statically_Match
666 Apply_Constraint_Check
667 (Exp
, DesigT
, No_Sliding
=> False);
669 -- The nonsliding check should really be performed
670 -- (unconditionally) against the subtype of the
671 -- qualified expression, but that causes a problem
672 -- with c34007g (see above), so for now we retain this.
675 Apply_Constraint_Check
676 (Exp
, DesigT
, No_Sliding
=> True);
679 -- For an access to unconstrained packed array, GIGI needs
680 -- to see an expression with a constrained subtype in order
681 -- to compute the proper size for the allocator.
684 and then not Is_Constrained
(T
)
685 and then Is_Packed
(T
)
688 ConstrT
: constant Entity_Id
:=
689 Make_Defining_Identifier
(Loc
,
690 Chars
=> New_Internal_Name
('A'));
691 Internal_Exp
: constant Node_Id
:= Relocate_Node
(Exp
);
694 Make_Subtype_Declaration
(Loc
,
695 Defining_Identifier
=> ConstrT
,
696 Subtype_Indication
=>
697 Make_Subtype_From_Expr
(Exp
, T
)));
698 Freeze_Itype
(ConstrT
, Exp
);
699 Rewrite
(Exp
, OK_Convert_To
(ConstrT
, Internal_Exp
));
706 when RE_Not_Available
=>
708 end Expand_Allocator_Expression
;
710 -----------------------------
711 -- Expand_Array_Comparison --
712 -----------------------------
714 -- Expansion is only required in the case of array types. For the
715 -- unpacked case, an appropriate runtime routine is called. For
716 -- packed cases, and also in some other cases where a runtime
717 -- routine cannot be called, the form of the expansion is:
719 -- [body for greater_nn; boolean_expression]
721 -- The body is built by Make_Array_Comparison_Op, and the form of the
722 -- Boolean expression depends on the operator involved.
724 procedure Expand_Array_Comparison
(N
: Node_Id
) is
725 Loc
: constant Source_Ptr
:= Sloc
(N
);
726 Op1
: Node_Id
:= Left_Opnd
(N
);
727 Op2
: Node_Id
:= Right_Opnd
(N
);
728 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
729 Ctyp
: constant Entity_Id
:= Component_Type
(Typ1
);
733 Func_Name
: Entity_Id
;
737 Byte_Addressable
: constant Boolean := System_Storage_Unit
= Byte
'Size;
738 -- True for byte addressable target
740 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean;
741 -- Returns True if the length of the given operand is known to be
742 -- less than 4. Returns False if this length is known to be four
743 -- or greater or is not known at compile time.
745 ------------------------
746 -- Length_Less_Than_4 --
747 ------------------------
749 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean is
750 Otyp
: constant Entity_Id
:= Etype
(Opnd
);
753 if Ekind
(Otyp
) = E_String_Literal_Subtype
then
754 return String_Literal_Length
(Otyp
) < 4;
758 Ityp
: constant Entity_Id
:= Etype
(First_Index
(Otyp
));
759 Lo
: constant Node_Id
:= Type_Low_Bound
(Ityp
);
760 Hi
: constant Node_Id
:= Type_High_Bound
(Ityp
);
765 if Compile_Time_Known_Value
(Lo
) then
766 Lov
:= Expr_Value
(Lo
);
771 if Compile_Time_Known_Value
(Hi
) then
772 Hiv
:= Expr_Value
(Hi
);
777 return Hiv
< Lov
+ 3;
780 end Length_Less_Than_4
;
782 -- Start of processing for Expand_Array_Comparison
785 -- Deal first with unpacked case, where we can call a runtime routine
786 -- except that we avoid this for targets for which are not addressable
787 -- by bytes, and for the JVM, since the JVM does not support direct
788 -- addressing of array components.
790 if not Is_Bit_Packed_Array
(Typ1
)
791 and then Byte_Addressable
794 -- The call we generate is:
796 -- Compare_Array_xn[_Unaligned]
797 -- (left'address, right'address, left'length, right'length) <op> 0
799 -- x = U for unsigned, S for signed
800 -- n = 8,16,32,64 for component size
801 -- Add _Unaligned if length < 4 and component size is 8.
802 -- <op> is the standard comparison operator
804 if Component_Size
(Typ1
) = 8 then
805 if Length_Less_Than_4
(Op1
)
807 Length_Less_Than_4
(Op2
)
809 if Is_Unsigned_Type
(Ctyp
) then
810 Comp
:= RE_Compare_Array_U8_Unaligned
;
812 Comp
:= RE_Compare_Array_S8_Unaligned
;
816 if Is_Unsigned_Type
(Ctyp
) then
817 Comp
:= RE_Compare_Array_U8
;
819 Comp
:= RE_Compare_Array_S8
;
823 elsif Component_Size
(Typ1
) = 16 then
824 if Is_Unsigned_Type
(Ctyp
) then
825 Comp
:= RE_Compare_Array_U16
;
827 Comp
:= RE_Compare_Array_S16
;
830 elsif Component_Size
(Typ1
) = 32 then
831 if Is_Unsigned_Type
(Ctyp
) then
832 Comp
:= RE_Compare_Array_U32
;
834 Comp
:= RE_Compare_Array_S32
;
837 else pragma Assert
(Component_Size
(Typ1
) = 64);
838 if Is_Unsigned_Type
(Ctyp
) then
839 Comp
:= RE_Compare_Array_U64
;
841 Comp
:= RE_Compare_Array_S64
;
845 Remove_Side_Effects
(Op1
, Name_Req
=> True);
846 Remove_Side_Effects
(Op2
, Name_Req
=> True);
849 Make_Function_Call
(Sloc
(Op1
),
850 Name
=> New_Occurrence_Of
(RTE
(Comp
), Loc
),
852 Parameter_Associations
=> New_List
(
853 Make_Attribute_Reference
(Loc
,
854 Prefix
=> Relocate_Node
(Op1
),
855 Attribute_Name
=> Name_Address
),
857 Make_Attribute_Reference
(Loc
,
858 Prefix
=> Relocate_Node
(Op2
),
859 Attribute_Name
=> Name_Address
),
861 Make_Attribute_Reference
(Loc
,
862 Prefix
=> Relocate_Node
(Op1
),
863 Attribute_Name
=> Name_Length
),
865 Make_Attribute_Reference
(Loc
,
866 Prefix
=> Relocate_Node
(Op2
),
867 Attribute_Name
=> Name_Length
))));
870 Make_Integer_Literal
(Sloc
(Op2
),
873 Analyze_And_Resolve
(Op1
, Standard_Integer
);
874 Analyze_And_Resolve
(Op2
, Standard_Integer
);
878 -- Cases where we cannot make runtime call
880 -- For (a <= b) we convert to not (a > b)
882 if Chars
(N
) = Name_Op_Le
then
888 Right_Opnd
=> Op2
)));
889 Analyze_And_Resolve
(N
, Standard_Boolean
);
892 -- For < the Boolean expression is
893 -- greater__nn (op2, op1)
895 elsif Chars
(N
) = Name_Op_Lt
then
896 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
900 Op1
:= Right_Opnd
(N
);
901 Op2
:= Left_Opnd
(N
);
903 -- For (a >= b) we convert to not (a < b)
905 elsif Chars
(N
) = Name_Op_Ge
then
911 Right_Opnd
=> Op2
)));
912 Analyze_And_Resolve
(N
, Standard_Boolean
);
915 -- For > the Boolean expression is
916 -- greater__nn (op1, op2)
919 pragma Assert
(Chars
(N
) = Name_Op_Gt
);
920 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
923 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
925 Make_Function_Call
(Loc
,
926 Name
=> New_Reference_To
(Func_Name
, Loc
),
927 Parameter_Associations
=> New_List
(Op1
, Op2
));
929 Insert_Action
(N
, Func_Body
);
931 Analyze_And_Resolve
(N
, Standard_Boolean
);
934 when RE_Not_Available
=>
936 end Expand_Array_Comparison
;
938 ---------------------------
939 -- Expand_Array_Equality --
940 ---------------------------
942 -- Expand an equality function for multi-dimensional arrays. Here is
943 -- an example of such a function for Nb_Dimension = 2
945 -- function Enn (A : atyp; B : btyp) return boolean is
947 -- if (A'length (1) = 0 or else A'length (2) = 0)
949 -- (B'length (1) = 0 or else B'length (2) = 0)
951 -- return True; -- RM 4.5.2(22)
954 -- if A'length (1) /= B'length (1)
956 -- A'length (2) /= B'length (2)
958 -- return False; -- RM 4.5.2(23)
962 -- A1 : Index_T1 := A'first (1);
963 -- B1 : Index_T1 := B'first (1);
967 -- A2 : Index_T2 := A'first (2);
968 -- B2 : Index_T2 := B'first (2);
971 -- if A (A1, A2) /= B (B1, B2) then
975 -- exit when A2 = A'last (2);
976 -- A2 := Index_T2'succ (A2);
977 -- B2 := Index_T2'succ (B2);
981 -- exit when A1 = A'last (1);
982 -- A1 := Index_T1'succ (A1);
983 -- B1 := Index_T1'succ (B1);
990 -- Note on the formal types used (atyp and btyp). If either of the
991 -- arrays is of a private type, we use the underlying type, and
992 -- do an unchecked conversion of the actual. If either of the arrays
993 -- has a bound depending on a discriminant, then we use the base type
994 -- since otherwise we have an escaped discriminant in the function.
996 -- If both arrays are constrained and have the same bounds, we can
997 -- generate a loop with an explicit iteration scheme using a 'Range
998 -- attribute over the first array.
1000 function Expand_Array_Equality
1005 Typ
: Entity_Id
) return Node_Id
1007 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
1008 Decls
: constant List_Id
:= New_List
;
1009 Index_List1
: constant List_Id
:= New_List
;
1010 Index_List2
: constant List_Id
:= New_List
;
1014 Func_Name
: Entity_Id
;
1015 Func_Body
: Node_Id
;
1017 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
1018 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
1022 -- The parameter types to be used for the formals
1027 Num
: Int
) return Node_Id
;
1028 -- This builds the attribute reference Arr'Nam (Expr)
1030 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
;
1031 -- Create one statement to compare corresponding components,
1032 -- designated by a full set of indices.
1034 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
;
1035 -- Given one of the arguments, computes the appropriate type to
1036 -- be used for that argument in the corresponding function formal
1038 function Handle_One_Dimension
1040 Index
: Node_Id
) return Node_Id
;
1041 -- This procedure returns the following code
1044 -- Bn : Index_T := B'First (N);
1048 -- exit when An = A'Last (N);
1049 -- An := Index_T'Succ (An)
1050 -- Bn := Index_T'Succ (Bn)
1054 -- If both indices are constrained and identical, the procedure
1055 -- returns a simpler loop:
1057 -- for An in A'Range (N) loop
1061 -- N is the dimension for which we are generating a loop. Index is the
1062 -- N'th index node, whose Etype is Index_Type_n in the above code.
1063 -- The xxx statement is either the loop or declare for the next
1064 -- dimension or if this is the last dimension the comparison
1065 -- of corresponding components of the arrays.
1067 -- The actual way the code works is to return the comparison
1068 -- of corresponding components for the N+1 call. That's neater!
1070 function Test_Empty_Arrays
return Node_Id
;
1071 -- This function constructs the test for both arrays being empty
1072 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1074 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1076 function Test_Lengths_Correspond
return Node_Id
;
1077 -- This function constructs the test for arrays having different
1078 -- lengths in at least one index position, in which case resull
1080 -- A'length (1) /= B'length (1)
1082 -- A'length (2) /= B'length (2)
1093 Num
: Int
) return Node_Id
1097 Make_Attribute_Reference
(Loc
,
1098 Attribute_Name
=> Nam
,
1099 Prefix
=> New_Reference_To
(Arr
, Loc
),
1100 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, Num
)));
1103 ------------------------
1104 -- Component_Equality --
1105 ------------------------
1107 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
is
1112 -- if a(i1...) /= b(j1...) then return false; end if;
1115 Make_Indexed_Component
(Loc
,
1116 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
1117 Expressions
=> Index_List1
);
1120 Make_Indexed_Component
(Loc
,
1121 Prefix
=> Make_Identifier
(Loc
, Chars
(B
)),
1122 Expressions
=> Index_List2
);
1124 Test
:= Expand_Composite_Equality
1125 (Nod
, Component_Type
(Typ
), L
, R
, Decls
);
1127 -- If some (sub)component is an unchecked_union, the whole operation
1128 -- will raise program error.
1130 if Nkind
(Test
) = N_Raise_Program_Error
then
1132 -- This node is going to be inserted at a location where a
1133 -- statement is expected: clear its Etype so analysis will
1134 -- set it to the expected Standard_Void_Type.
1136 Set_Etype
(Test
, Empty
);
1141 Make_Implicit_If_Statement
(Nod
,
1142 Condition
=> Make_Op_Not
(Loc
, Right_Opnd
=> Test
),
1143 Then_Statements
=> New_List
(
1144 Make_Return_Statement
(Loc
,
1145 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))));
1147 end Component_Equality
;
1153 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
is
1164 T
:= Underlying_Type
(T
);
1166 X
:= First_Index
(T
);
1167 while Present
(X
) loop
1168 if Denotes_Discriminant
(Type_Low_Bound
(Etype
(X
)))
1170 Denotes_Discriminant
(Type_High_Bound
(Etype
(X
)))
1183 --------------------------
1184 -- Handle_One_Dimension --
1185 ---------------------------
1187 function Handle_One_Dimension
1189 Index
: Node_Id
) return Node_Id
1191 Need_Separate_Indexes
: constant Boolean :=
1193 or else not Is_Constrained
(Ltyp
);
1194 -- If the index types are identical, and we are working with
1195 -- constrained types, then we can use the same index for both of
1198 An
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
,
1199 Chars
=> New_Internal_Name
('A'));
1202 Index_T
: Entity_Id
;
1207 if N
> Number_Dimensions
(Ltyp
) then
1208 return Component_Equality
(Ltyp
);
1211 -- Case where we generate a loop
1213 Index_T
:= Base_Type
(Etype
(Index
));
1215 if Need_Separate_Indexes
then
1217 Make_Defining_Identifier
(Loc
,
1218 Chars
=> New_Internal_Name
('B'));
1223 Append
(New_Reference_To
(An
, Loc
), Index_List1
);
1224 Append
(New_Reference_To
(Bn
, Loc
), Index_List2
);
1226 Stm_List
:= New_List
(
1227 Handle_One_Dimension
(N
+ 1, Next_Index
(Index
)));
1229 if Need_Separate_Indexes
then
1231 -- Generate guard for loop, followed by increments of indices
1233 Append_To
(Stm_List
,
1234 Make_Exit_Statement
(Loc
,
1237 Left_Opnd
=> New_Reference_To
(An
, Loc
),
1238 Right_Opnd
=> Arr_Attr
(A
, Name_Last
, N
))));
1240 Append_To
(Stm_List
,
1241 Make_Assignment_Statement
(Loc
,
1242 Name
=> New_Reference_To
(An
, Loc
),
1244 Make_Attribute_Reference
(Loc
,
1245 Prefix
=> New_Reference_To
(Index_T
, Loc
),
1246 Attribute_Name
=> Name_Succ
,
1247 Expressions
=> New_List
(New_Reference_To
(An
, Loc
)))));
1249 Append_To
(Stm_List
,
1250 Make_Assignment_Statement
(Loc
,
1251 Name
=> New_Reference_To
(Bn
, Loc
),
1253 Make_Attribute_Reference
(Loc
,
1254 Prefix
=> New_Reference_To
(Index_T
, Loc
),
1255 Attribute_Name
=> Name_Succ
,
1256 Expressions
=> New_List
(New_Reference_To
(Bn
, Loc
)))));
1259 -- If separate indexes, we need a declare block for An and Bn, and a
1260 -- loop without an iteration scheme.
1262 if Need_Separate_Indexes
then
1264 Make_Implicit_Loop_Statement
(Nod
, Statements
=> Stm_List
);
1267 Make_Block_Statement
(Loc
,
1268 Declarations
=> New_List
(
1269 Make_Object_Declaration
(Loc
,
1270 Defining_Identifier
=> An
,
1271 Object_Definition
=> New_Reference_To
(Index_T
, Loc
),
1272 Expression
=> Arr_Attr
(A
, Name_First
, N
)),
1274 Make_Object_Declaration
(Loc
,
1275 Defining_Identifier
=> Bn
,
1276 Object_Definition
=> New_Reference_To
(Index_T
, Loc
),
1277 Expression
=> Arr_Attr
(B
, Name_First
, N
))),
1279 Handled_Statement_Sequence
=>
1280 Make_Handled_Sequence_Of_Statements
(Loc
,
1281 Statements
=> New_List
(Loop_Stm
)));
1283 -- If no separate indexes, return loop statement with explicit
1284 -- iteration scheme on its own
1288 Make_Implicit_Loop_Statement
(Nod
,
1289 Statements
=> Stm_List
,
1291 Make_Iteration_Scheme
(Loc
,
1292 Loop_Parameter_Specification
=>
1293 Make_Loop_Parameter_Specification
(Loc
,
1294 Defining_Identifier
=> An
,
1295 Discrete_Subtype_Definition
=>
1296 Arr_Attr
(A
, Name_Range
, N
))));
1299 end Handle_One_Dimension
;
1301 -----------------------
1302 -- Test_Empty_Arrays --
1303 -----------------------
1305 function Test_Empty_Arrays
return Node_Id
is
1315 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
1318 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
1319 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
1323 Left_Opnd
=> Arr_Attr
(B
, Name_Length
, J
),
1324 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
1333 Left_Opnd
=> Relocate_Node
(Alist
),
1334 Right_Opnd
=> Atest
);
1338 Left_Opnd
=> Relocate_Node
(Blist
),
1339 Right_Opnd
=> Btest
);
1346 Right_Opnd
=> Blist
);
1347 end Test_Empty_Arrays
;
1349 -----------------------------
1350 -- Test_Lengths_Correspond --
1351 -----------------------------
1353 function Test_Lengths_Correspond
return Node_Id
is
1359 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
1362 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
1363 Right_Opnd
=> Arr_Attr
(B
, Name_Length
, J
));
1370 Left_Opnd
=> Relocate_Node
(Result
),
1371 Right_Opnd
=> Rtest
);
1376 end Test_Lengths_Correspond
;
1378 -- Start of processing for Expand_Array_Equality
1381 Ltyp
:= Get_Arg_Type
(Lhs
);
1382 Rtyp
:= Get_Arg_Type
(Rhs
);
1384 -- For now, if the argument types are not the same, go to the
1385 -- base type, since the code assumes that the formals have the
1386 -- same type. This is fixable in future ???
1388 if Ltyp
/= Rtyp
then
1389 Ltyp
:= Base_Type
(Ltyp
);
1390 Rtyp
:= Base_Type
(Rtyp
);
1391 pragma Assert
(Ltyp
= Rtyp
);
1394 -- Build list of formals for function
1396 Formals
:= New_List
(
1397 Make_Parameter_Specification
(Loc
,
1398 Defining_Identifier
=> A
,
1399 Parameter_Type
=> New_Reference_To
(Ltyp
, Loc
)),
1401 Make_Parameter_Specification
(Loc
,
1402 Defining_Identifier
=> B
,
1403 Parameter_Type
=> New_Reference_To
(Rtyp
, Loc
)));
1405 Func_Name
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('E'));
1407 -- Build statement sequence for function
1410 Make_Subprogram_Body
(Loc
,
1412 Make_Function_Specification
(Loc
,
1413 Defining_Unit_Name
=> Func_Name
,
1414 Parameter_Specifications
=> Formals
,
1415 Result_Definition
=> New_Reference_To
(Standard_Boolean
, Loc
)),
1417 Declarations
=> Decls
,
1419 Handled_Statement_Sequence
=>
1420 Make_Handled_Sequence_Of_Statements
(Loc
,
1421 Statements
=> New_List
(
1423 Make_Implicit_If_Statement
(Nod
,
1424 Condition
=> Test_Empty_Arrays
,
1425 Then_Statements
=> New_List
(
1426 Make_Return_Statement
(Loc
,
1428 New_Occurrence_Of
(Standard_True
, Loc
)))),
1430 Make_Implicit_If_Statement
(Nod
,
1431 Condition
=> Test_Lengths_Correspond
,
1432 Then_Statements
=> New_List
(
1433 Make_Return_Statement
(Loc
,
1435 New_Occurrence_Of
(Standard_False
, Loc
)))),
1437 Handle_One_Dimension
(1, First_Index
(Ltyp
)),
1439 Make_Return_Statement
(Loc
,
1440 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)))));
1442 Set_Has_Completion
(Func_Name
, True);
1443 Set_Is_Inlined
(Func_Name
);
1445 -- If the array type is distinct from the type of the arguments,
1446 -- it is the full view of a private type. Apply an unchecked
1447 -- conversion to insure that analysis of the call succeeds.
1457 or else Base_Type
(Etype
(Lhs
)) /= Base_Type
(Ltyp
)
1459 L
:= OK_Convert_To
(Ltyp
, Lhs
);
1463 or else Base_Type
(Etype
(Rhs
)) /= Base_Type
(Rtyp
)
1465 R
:= OK_Convert_To
(Rtyp
, Rhs
);
1468 Actuals
:= New_List
(L
, R
);
1471 Append_To
(Bodies
, Func_Body
);
1474 Make_Function_Call
(Loc
,
1475 Name
=> New_Reference_To
(Func_Name
, Loc
),
1476 Parameter_Associations
=> Actuals
);
1477 end Expand_Array_Equality
;
1479 -----------------------------
1480 -- Expand_Boolean_Operator --
1481 -----------------------------
1483 -- Note that we first get the actual subtypes of the operands,
1484 -- since we always want to deal with types that have bounds.
1486 procedure Expand_Boolean_Operator
(N
: Node_Id
) is
1487 Typ
: constant Entity_Id
:= Etype
(N
);
1490 -- Special case of bit packed array where both operands are known
1491 -- to be properly aligned. In this case we use an efficient run time
1492 -- routine to carry out the operation (see System.Bit_Ops).
1494 if Is_Bit_Packed_Array
(Typ
)
1495 and then not Is_Possibly_Unaligned_Object
(Left_Opnd
(N
))
1496 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
1498 Expand_Packed_Boolean_Operator
(N
);
1502 -- For the normal non-packed case, the general expansion is to build
1503 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
1504 -- and then inserting it into the tree. The original operator node is
1505 -- then rewritten as a call to this function. We also use this in the
1506 -- packed case if either operand is a possibly unaligned object.
1509 Loc
: constant Source_Ptr
:= Sloc
(N
);
1510 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
1511 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
1512 Func_Body
: Node_Id
;
1513 Func_Name
: Entity_Id
;
1516 Convert_To_Actual_Subtype
(L
);
1517 Convert_To_Actual_Subtype
(R
);
1518 Ensure_Defined
(Etype
(L
), N
);
1519 Ensure_Defined
(Etype
(R
), N
);
1520 Apply_Length_Check
(R
, Etype
(L
));
1522 if Nkind
(Parent
(N
)) = N_Assignment_Statement
1523 and then Safe_In_Place_Array_Op
(Name
(Parent
(N
)), L
, R
)
1525 Build_Boolean_Array_Proc_Call
(Parent
(N
), L
, R
);
1527 elsif Nkind
(Parent
(N
)) = N_Op_Not
1528 and then Nkind
(N
) = N_Op_And
1530 Safe_In_Place_Array_Op
(Name
(Parent
(Parent
(N
))), L
, R
)
1535 Func_Body
:= Make_Boolean_Array_Op
(Etype
(L
), N
);
1536 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
1537 Insert_Action
(N
, Func_Body
);
1539 -- Now rewrite the expression with a call
1542 Make_Function_Call
(Loc
,
1543 Name
=> New_Reference_To
(Func_Name
, Loc
),
1544 Parameter_Associations
=>
1547 Make_Type_Conversion
1548 (Loc
, New_Reference_To
(Etype
(L
), Loc
), R
))));
1550 Analyze_And_Resolve
(N
, Typ
);
1553 end Expand_Boolean_Operator
;
1555 -------------------------------
1556 -- Expand_Composite_Equality --
1557 -------------------------------
1559 -- This function is only called for comparing internal fields of composite
1560 -- types when these fields are themselves composites. This is a special
1561 -- case because it is not possible to respect normal Ada visibility rules.
1563 function Expand_Composite_Equality
1568 Bodies
: List_Id
) return Node_Id
1570 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
1571 Full_Type
: Entity_Id
;
1576 if Is_Private_Type
(Typ
) then
1577 Full_Type
:= Underlying_Type
(Typ
);
1582 -- Defense against malformed private types with no completion
1583 -- the error will be diagnosed later by check_completion
1585 if No
(Full_Type
) then
1586 return New_Reference_To
(Standard_False
, Loc
);
1589 Full_Type
:= Base_Type
(Full_Type
);
1591 if Is_Array_Type
(Full_Type
) then
1593 -- If the operand is an elementary type other than a floating-point
1594 -- type, then we can simply use the built-in block bitwise equality,
1595 -- since the predefined equality operators always apply and bitwise
1596 -- equality is fine for all these cases.
1598 if Is_Elementary_Type
(Component_Type
(Full_Type
))
1599 and then not Is_Floating_Point_Type
(Component_Type
(Full_Type
))
1601 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
1603 -- For composite component types, and floating-point types, use
1604 -- the expansion. This deals with tagged component types (where
1605 -- we use the applicable equality routine) and floating-point,
1606 -- (where we need to worry about negative zeroes), and also the
1607 -- case of any composite type recursively containing such fields.
1610 return Expand_Array_Equality
(Nod
, Lhs
, Rhs
, Bodies
, Full_Type
);
1613 elsif Is_Tagged_Type
(Full_Type
) then
1615 -- Call the primitive operation "=" of this type
1617 if Is_Class_Wide_Type
(Full_Type
) then
1618 Full_Type
:= Root_Type
(Full_Type
);
1621 -- If this is derived from an untagged private type completed
1622 -- with a tagged type, it does not have a full view, so we
1623 -- use the primitive operations of the private type.
1624 -- This check should no longer be necessary when these
1625 -- types receive their full views ???
1627 if Is_Private_Type
(Typ
)
1628 and then not Is_Tagged_Type
(Typ
)
1629 and then not Is_Controlled
(Typ
)
1630 and then Is_Derived_Type
(Typ
)
1631 and then No
(Full_View
(Typ
))
1633 Prim
:= First_Elmt
(Collect_Primitive_Operations
(Typ
));
1635 Prim
:= First_Elmt
(Primitive_Operations
(Full_Type
));
1639 Eq_Op
:= Node
(Prim
);
1640 exit when Chars
(Eq_Op
) = Name_Op_Eq
1641 and then Etype
(First_Formal
(Eq_Op
)) =
1642 Etype
(Next_Formal
(First_Formal
(Eq_Op
)))
1643 and then Base_Type
(Etype
(Eq_Op
)) = Standard_Boolean
;
1645 pragma Assert
(Present
(Prim
));
1648 Eq_Op
:= Node
(Prim
);
1651 Make_Function_Call
(Loc
,
1652 Name
=> New_Reference_To
(Eq_Op
, Loc
),
1653 Parameter_Associations
=>
1655 (Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Lhs
),
1656 Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Rhs
)));
1658 elsif Is_Record_Type
(Full_Type
) then
1659 Eq_Op
:= TSS
(Full_Type
, TSS_Composite_Equality
);
1661 if Present
(Eq_Op
) then
1662 if Etype
(First_Formal
(Eq_Op
)) /= Full_Type
then
1664 -- Inherited equality from parent type. Convert the actuals
1665 -- to match signature of operation.
1668 T
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
1672 Make_Function_Call
(Loc
,
1673 Name
=> New_Reference_To
(Eq_Op
, Loc
),
1674 Parameter_Associations
=>
1675 New_List
(OK_Convert_To
(T
, Lhs
),
1676 OK_Convert_To
(T
, Rhs
)));
1680 -- Comparison between Unchecked_Union components
1682 if Is_Unchecked_Union
(Full_Type
) then
1684 Lhs_Type
: Node_Id
:= Full_Type
;
1685 Rhs_Type
: Node_Id
:= Full_Type
;
1686 Lhs_Discr_Val
: Node_Id
;
1687 Rhs_Discr_Val
: Node_Id
;
1692 if Nkind
(Lhs
) = N_Selected_Component
then
1693 Lhs_Type
:= Etype
(Entity
(Selector_Name
(Lhs
)));
1698 if Nkind
(Rhs
) = N_Selected_Component
then
1699 Rhs_Type
:= Etype
(Entity
(Selector_Name
(Rhs
)));
1702 -- Lhs of the composite equality
1704 if Is_Constrained
(Lhs_Type
) then
1706 -- Since the enclosing record can never be an
1707 -- Unchecked_Union (this code is executed for records
1708 -- that do not have variants), we may reference its
1711 if Nkind
(Lhs
) = N_Selected_Component
1712 and then Has_Per_Object_Constraint
(
1713 Entity
(Selector_Name
(Lhs
)))
1716 Make_Selected_Component
(Loc
,
1717 Prefix
=> Prefix
(Lhs
),
1720 Get_Discriminant_Value
(
1721 First_Discriminant
(Lhs_Type
),
1723 Stored_Constraint
(Lhs_Type
))));
1726 Lhs_Discr_Val
:= New_Copy
(
1727 Get_Discriminant_Value
(
1728 First_Discriminant
(Lhs_Type
),
1730 Stored_Constraint
(Lhs_Type
)));
1734 -- It is not possible to infer the discriminant since
1735 -- the subtype is not constrained.
1738 Make_Raise_Program_Error
(Loc
,
1739 Reason
=> PE_Unchecked_Union_Restriction
);
1742 -- Rhs of the composite equality
1744 if Is_Constrained
(Rhs_Type
) then
1745 if Nkind
(Rhs
) = N_Selected_Component
1746 and then Has_Per_Object_Constraint
(
1747 Entity
(Selector_Name
(Rhs
)))
1750 Make_Selected_Component
(Loc
,
1751 Prefix
=> Prefix
(Rhs
),
1754 Get_Discriminant_Value
(
1755 First_Discriminant
(Rhs_Type
),
1757 Stored_Constraint
(Rhs_Type
))));
1760 Rhs_Discr_Val
:= New_Copy
(
1761 Get_Discriminant_Value
(
1762 First_Discriminant
(Rhs_Type
),
1764 Stored_Constraint
(Rhs_Type
)));
1769 Make_Raise_Program_Error
(Loc
,
1770 Reason
=> PE_Unchecked_Union_Restriction
);
1773 -- Call the TSS equality function with the inferred
1774 -- discriminant values.
1777 Make_Function_Call
(Loc
,
1778 Name
=> New_Reference_To
(Eq_Op
, Loc
),
1779 Parameter_Associations
=> New_List
(
1787 -- Shouldn't this be an else, we can't fall through
1788 -- the above IF, right???
1791 Make_Function_Call
(Loc
,
1792 Name
=> New_Reference_To
(Eq_Op
, Loc
),
1793 Parameter_Associations
=> New_List
(Lhs
, Rhs
));
1797 return Expand_Record_Equality
(Nod
, Full_Type
, Lhs
, Rhs
, Bodies
);
1801 -- It can be a simple record or the full view of a scalar private
1803 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
1805 end Expand_Composite_Equality
;
1807 ------------------------------
1808 -- Expand_Concatenate_Other --
1809 ------------------------------
1811 -- Let n be the number of array operands to be concatenated, Base_Typ
1812 -- their base type, Ind_Typ their index type, and Arr_Typ the original
1813 -- array type to which the concatenantion operator applies, then the
1814 -- following subprogram is constructed:
1816 -- [function Cnn (S1 : Base_Typ; ...; Sn : Base_Typ) return Base_Typ is
1819 -- if S1'Length /= 0 then
1820 -- L := XXX; --> XXX = S1'First if Arr_Typ is unconstrained
1821 -- XXX = Arr_Typ'First otherwise
1822 -- elsif S2'Length /= 0 then
1823 -- L := YYY; --> YYY = S2'First if Arr_Typ is unconstrained
1824 -- YYY = Arr_Typ'First otherwise
1826 -- elsif Sn-1'Length /= 0 then
1827 -- L := ZZZ; --> ZZZ = Sn-1'First if Arr_Typ is unconstrained
1828 -- ZZZ = Arr_Typ'First otherwise
1836 -- Ind_Typ'Val ((((S1'Length - 1) + S2'Length) + ... + Sn'Length)
1837 -- + Ind_Typ'Pos (L));
1838 -- R : Base_Typ (L .. H);
1840 -- if S1'Length /= 0 then
1844 -- L := Ind_Typ'Succ (L);
1845 -- exit when P = S1'Last;
1846 -- P := Ind_Typ'Succ (P);
1850 -- if S2'Length /= 0 then
1851 -- L := Ind_Typ'Succ (L);
1854 -- L := Ind_Typ'Succ (L);
1855 -- exit when P = S2'Last;
1856 -- P := Ind_Typ'Succ (P);
1862 -- if Sn'Length /= 0 then
1866 -- L := Ind_Typ'Succ (L);
1867 -- exit when P = Sn'Last;
1868 -- P := Ind_Typ'Succ (P);
1876 procedure Expand_Concatenate_Other
(Cnode
: Node_Id
; Opnds
: List_Id
) is
1877 Loc
: constant Source_Ptr
:= Sloc
(Cnode
);
1878 Nb_Opnds
: constant Nat
:= List_Length
(Opnds
);
1880 Arr_Typ
: constant Entity_Id
:= Etype
(Entity
(Cnode
));
1881 Base_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Cnode
));
1882 Ind_Typ
: constant Entity_Id
:= Etype
(First_Index
(Base_Typ
));
1885 Func_Spec
: Node_Id
;
1886 Param_Specs
: List_Id
;
1888 Func_Body
: Node_Id
;
1889 Func_Decls
: List_Id
;
1890 Func_Stmts
: List_Id
;
1895 Elsif_List
: List_Id
;
1897 Declare_Block
: Node_Id
;
1898 Declare_Decls
: List_Id
;
1899 Declare_Stmts
: List_Id
;
1911 function Copy_Into_R_S
(I
: Nat
; Last
: Boolean) return List_Id
;
1912 -- Builds the sequence of statement:
1916 -- L := Ind_Typ'Succ (L);
1917 -- exit when P = Si'Last;
1918 -- P := Ind_Typ'Succ (P);
1921 -- where i is the input parameter I given.
1922 -- If the flag Last is true, the exit statement is emitted before
1923 -- incrementing the lower bound, to prevent the creation out of
1926 function Init_L
(I
: Nat
) return Node_Id
;
1927 -- Builds the statement:
1928 -- L := Arr_Typ'First; If Arr_Typ is constrained
1929 -- L := Si'First; otherwise (where I is the input param given)
1931 function H
return Node_Id
;
1932 -- Builds reference to identifier H
1934 function Ind_Val
(E
: Node_Id
) return Node_Id
;
1935 -- Builds expression Ind_Typ'Val (E);
1937 function L
return Node_Id
;
1938 -- Builds reference to identifier L
1940 function L_Pos
return Node_Id
;
1941 -- Builds expression Integer_Type'(Ind_Typ'Pos (L)). We qualify the
1942 -- expression to avoid universal_integer computations whenever possible,
1943 -- in the expression for the upper bound H.
1945 function L_Succ
return Node_Id
;
1946 -- Builds expression Ind_Typ'Succ (L)
1948 function One
return Node_Id
;
1949 -- Builds integer literal one
1951 function P
return Node_Id
;
1952 -- Builds reference to identifier P
1954 function P_Succ
return Node_Id
;
1955 -- Builds expression Ind_Typ'Succ (P)
1957 function R
return Node_Id
;
1958 -- Builds reference to identifier R
1960 function S
(I
: Nat
) return Node_Id
;
1961 -- Builds reference to identifier Si, where I is the value given
1963 function S_First
(I
: Nat
) return Node_Id
;
1964 -- Builds expression Si'First, where I is the value given
1966 function S_Last
(I
: Nat
) return Node_Id
;
1967 -- Builds expression Si'Last, where I is the value given
1969 function S_Length
(I
: Nat
) return Node_Id
;
1970 -- Builds expression Si'Length, where I is the value given
1972 function S_Length_Test
(I
: Nat
) return Node_Id
;
1973 -- Builds expression Si'Length /= 0, where I is the value given
1979 function Copy_Into_R_S
(I
: Nat
; Last
: Boolean) return List_Id
is
1980 Stmts
: constant List_Id
:= New_List
;
1982 Loop_Stmt
: Node_Id
;
1984 Exit_Stmt
: Node_Id
;
1989 -- First construct the initializations
1991 P_Start
:= Make_Assignment_Statement
(Loc
,
1993 Expression
=> S_First
(I
));
1994 Append_To
(Stmts
, P_Start
);
1996 -- Then build the loop
1998 R_Copy
:= Make_Assignment_Statement
(Loc
,
1999 Name
=> Make_Indexed_Component
(Loc
,
2001 Expressions
=> New_List
(L
)),
2002 Expression
=> Make_Indexed_Component
(Loc
,
2004 Expressions
=> New_List
(P
)));
2006 L_Inc
:= Make_Assignment_Statement
(Loc
,
2008 Expression
=> L_Succ
);
2010 Exit_Stmt
:= Make_Exit_Statement
(Loc
,
2011 Condition
=> Make_Op_Eq
(Loc
, P
, S_Last
(I
)));
2013 P_Inc
:= Make_Assignment_Statement
(Loc
,
2015 Expression
=> P_Succ
);
2019 Make_Implicit_Loop_Statement
(Cnode
,
2020 Statements
=> New_List
(R_Copy
, Exit_Stmt
, L_Inc
, P_Inc
));
2023 Make_Implicit_Loop_Statement
(Cnode
,
2024 Statements
=> New_List
(R_Copy
, L_Inc
, Exit_Stmt
, P_Inc
));
2027 Append_To
(Stmts
, Loop_Stmt
);
2036 function H
return Node_Id
is
2038 return Make_Identifier
(Loc
, Name_uH
);
2045 function Ind_Val
(E
: Node_Id
) return Node_Id
is
2048 Make_Attribute_Reference
(Loc
,
2049 Prefix
=> New_Reference_To
(Ind_Typ
, Loc
),
2050 Attribute_Name
=> Name_Val
,
2051 Expressions
=> New_List
(E
));
2058 function Init_L
(I
: Nat
) return Node_Id
is
2062 if Is_Constrained
(Arr_Typ
) then
2063 E
:= Make_Attribute_Reference
(Loc
,
2064 Prefix
=> New_Reference_To
(Arr_Typ
, Loc
),
2065 Attribute_Name
=> Name_First
);
2071 return Make_Assignment_Statement
(Loc
, Name
=> L
, Expression
=> E
);
2078 function L
return Node_Id
is
2080 return Make_Identifier
(Loc
, Name_uL
);
2087 function L_Pos
return Node_Id
is
2088 Target_Type
: Entity_Id
;
2091 -- If the index type is an enumeration type, the computation
2092 -- can be done in standard integer. Otherwise, choose a large
2093 -- enough integer type.
2095 if Is_Enumeration_Type
(Ind_Typ
)
2096 or else Root_Type
(Ind_Typ
) = Standard_Integer
2097 or else Root_Type
(Ind_Typ
) = Standard_Short_Integer
2098 or else Root_Type
(Ind_Typ
) = Standard_Short_Short_Integer
2100 Target_Type
:= Standard_Integer
;
2102 Target_Type
:= Root_Type
(Ind_Typ
);
2106 Make_Qualified_Expression
(Loc
,
2107 Subtype_Mark
=> New_Reference_To
(Target_Type
, Loc
),
2109 Make_Attribute_Reference
(Loc
,
2110 Prefix
=> New_Reference_To
(Ind_Typ
, Loc
),
2111 Attribute_Name
=> Name_Pos
,
2112 Expressions
=> New_List
(L
)));
2119 function L_Succ
return Node_Id
is
2122 Make_Attribute_Reference
(Loc
,
2123 Prefix
=> New_Reference_To
(Ind_Typ
, Loc
),
2124 Attribute_Name
=> Name_Succ
,
2125 Expressions
=> New_List
(L
));
2132 function One
return Node_Id
is
2134 return Make_Integer_Literal
(Loc
, 1);
2141 function P
return Node_Id
is
2143 return Make_Identifier
(Loc
, Name_uP
);
2150 function P_Succ
return Node_Id
is
2153 Make_Attribute_Reference
(Loc
,
2154 Prefix
=> New_Reference_To
(Ind_Typ
, Loc
),
2155 Attribute_Name
=> Name_Succ
,
2156 Expressions
=> New_List
(P
));
2163 function R
return Node_Id
is
2165 return Make_Identifier
(Loc
, Name_uR
);
2172 function S
(I
: Nat
) return Node_Id
is
2174 return Make_Identifier
(Loc
, New_External_Name
('S', I
));
2181 function S_First
(I
: Nat
) return Node_Id
is
2183 return Make_Attribute_Reference
(Loc
,
2185 Attribute_Name
=> Name_First
);
2192 function S_Last
(I
: Nat
) return Node_Id
is
2194 return Make_Attribute_Reference
(Loc
,
2196 Attribute_Name
=> Name_Last
);
2203 function S_Length
(I
: Nat
) return Node_Id
is
2205 return Make_Attribute_Reference
(Loc
,
2207 Attribute_Name
=> Name_Length
);
2214 function S_Length_Test
(I
: Nat
) return Node_Id
is
2218 Left_Opnd
=> S_Length
(I
),
2219 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
2222 -- Start of processing for Expand_Concatenate_Other
2225 -- Construct the parameter specs and the overall function spec
2227 Param_Specs
:= New_List
;
2228 for I
in 1 .. Nb_Opnds
loop
2231 Make_Parameter_Specification
(Loc
,
2232 Defining_Identifier
=>
2233 Make_Defining_Identifier
(Loc
, New_External_Name
('S', I
)),
2234 Parameter_Type
=> New_Reference_To
(Base_Typ
, Loc
)));
2237 Func_Id
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('C'));
2239 Make_Function_Specification
(Loc
,
2240 Defining_Unit_Name
=> Func_Id
,
2241 Parameter_Specifications
=> Param_Specs
,
2242 Result_Definition
=> New_Reference_To
(Base_Typ
, Loc
));
2244 -- Construct L's object declaration
2247 Make_Object_Declaration
(Loc
,
2248 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_uL
),
2249 Object_Definition
=> New_Reference_To
(Ind_Typ
, Loc
));
2251 Func_Decls
:= New_List
(L_Decl
);
2253 -- Construct the if-then-elsif statements
2255 Elsif_List
:= New_List
;
2256 for I
in 2 .. Nb_Opnds
- 1 loop
2257 Append_To
(Elsif_List
, Make_Elsif_Part
(Loc
,
2258 Condition
=> S_Length_Test
(I
),
2259 Then_Statements
=> New_List
(Init_L
(I
))));
2263 Make_Implicit_If_Statement
(Cnode
,
2264 Condition
=> S_Length_Test
(1),
2265 Then_Statements
=> New_List
(Init_L
(1)),
2266 Elsif_Parts
=> Elsif_List
,
2267 Else_Statements
=> New_List
(Make_Return_Statement
(Loc
,
2268 Expression
=> S
(Nb_Opnds
))));
2270 -- Construct the declaration for H
2273 Make_Object_Declaration
(Loc
,
2274 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_uP
),
2275 Object_Definition
=> New_Reference_To
(Ind_Typ
, Loc
));
2277 H_Init
:= Make_Op_Subtract
(Loc
, S_Length
(1), One
);
2278 for I
in 2 .. Nb_Opnds
loop
2279 H_Init
:= Make_Op_Add
(Loc
, H_Init
, S_Length
(I
));
2281 H_Init
:= Ind_Val
(Make_Op_Add
(Loc
, H_Init
, L_Pos
));
2284 Make_Object_Declaration
(Loc
,
2285 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_uH
),
2286 Object_Definition
=> New_Reference_To
(Ind_Typ
, Loc
),
2287 Expression
=> H_Init
);
2289 -- Construct the declaration for R
2291 R_Range
:= Make_Range
(Loc
, Low_Bound
=> L
, High_Bound
=> H
);
2293 Make_Index_Or_Discriminant_Constraint
(Loc
,
2294 Constraints
=> New_List
(R_Range
));
2297 Make_Object_Declaration
(Loc
,
2298 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_uR
),
2299 Object_Definition
=>
2300 Make_Subtype_Indication
(Loc
,
2301 Subtype_Mark
=> New_Reference_To
(Base_Typ
, Loc
),
2302 Constraint
=> R_Constr
));
2304 -- Construct the declarations for the declare block
2306 Declare_Decls
:= New_List
(P_Decl
, H_Decl
, R_Decl
);
2308 -- Construct list of statements for the declare block
2310 Declare_Stmts
:= New_List
;
2311 for I
in 1 .. Nb_Opnds
loop
2312 Append_To
(Declare_Stmts
,
2313 Make_Implicit_If_Statement
(Cnode
,
2314 Condition
=> S_Length_Test
(I
),
2315 Then_Statements
=> Copy_Into_R_S
(I
, I
= Nb_Opnds
)));
2318 Append_To
(Declare_Stmts
, Make_Return_Statement
(Loc
, Expression
=> R
));
2320 -- Construct the declare block
2322 Declare_Block
:= Make_Block_Statement
(Loc
,
2323 Declarations
=> Declare_Decls
,
2324 Handled_Statement_Sequence
=>
2325 Make_Handled_Sequence_Of_Statements
(Loc
, Declare_Stmts
));
2327 -- Construct the list of function statements
2329 Func_Stmts
:= New_List
(If_Stmt
, Declare_Block
);
2331 -- Construct the function body
2334 Make_Subprogram_Body
(Loc
,
2335 Specification
=> Func_Spec
,
2336 Declarations
=> Func_Decls
,
2337 Handled_Statement_Sequence
=>
2338 Make_Handled_Sequence_Of_Statements
(Loc
, Func_Stmts
));
2340 -- Insert the newly generated function in the code. This is analyzed
2341 -- with all checks off, since we have completed all the checks.
2343 -- Note that this does *not* fix the array concatenation bug when the
2344 -- low bound is Integer'first sibce that bug comes from the pointer
2345 -- dereferencing an unconstrained array. An there we need a constraint
2346 -- check to make sure the length of the concatenated array is ok. ???
2348 Insert_Action
(Cnode
, Func_Body
, Suppress
=> All_Checks
);
2350 -- Construct list of arguments for the function call
2353 Operand
:= First
(Opnds
);
2354 for I
in 1 .. Nb_Opnds
loop
2355 Append_To
(Params
, Relocate_Node
(Operand
));
2359 -- Insert the function call
2363 Make_Function_Call
(Loc
, New_Reference_To
(Func_Id
, Loc
), Params
));
2365 Analyze_And_Resolve
(Cnode
, Base_Typ
);
2366 Set_Is_Inlined
(Func_Id
);
2367 end Expand_Concatenate_Other
;
2369 -------------------------------
2370 -- Expand_Concatenate_String --
2371 -------------------------------
2373 procedure Expand_Concatenate_String
(Cnode
: Node_Id
; Opnds
: List_Id
) is
2374 Loc
: constant Source_Ptr
:= Sloc
(Cnode
);
2375 Opnd1
: constant Node_Id
:= First
(Opnds
);
2376 Opnd2
: constant Node_Id
:= Next
(Opnd1
);
2377 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Opnd1
));
2378 Typ2
: constant Entity_Id
:= Base_Type
(Etype
(Opnd2
));
2381 -- RE_Id value for function to be called
2384 -- In all cases, we build a call to a routine giving the list of
2385 -- arguments as the parameter list to the routine.
2387 case List_Length
(Opnds
) is
2389 if Typ1
= Standard_Character
then
2390 if Typ2
= Standard_Character
then
2391 R
:= RE_Str_Concat_CC
;
2394 pragma Assert
(Typ2
= Standard_String
);
2395 R
:= RE_Str_Concat_CS
;
2398 elsif Typ1
= Standard_String
then
2399 if Typ2
= Standard_Character
then
2400 R
:= RE_Str_Concat_SC
;
2403 pragma Assert
(Typ2
= Standard_String
);
2407 -- If we have anything other than Standard_Character or
2408 -- Standard_String, then we must have had a serious error
2409 -- earlier, so we just abandon the attempt at expansion.
2412 pragma Assert
(Serious_Errors_Detected
> 0);
2417 R
:= RE_Str_Concat_3
;
2420 R
:= RE_Str_Concat_4
;
2423 R
:= RE_Str_Concat_5
;
2427 raise Program_Error
;
2430 -- Now generate the appropriate call
2433 Make_Function_Call
(Sloc
(Cnode
),
2434 Name
=> New_Occurrence_Of
(RTE
(R
), Loc
),
2435 Parameter_Associations
=> Opnds
));
2437 Analyze_And_Resolve
(Cnode
, Standard_String
);
2440 when RE_Not_Available
=>
2442 end Expand_Concatenate_String
;
2444 ------------------------
2445 -- Expand_N_Allocator --
2446 ------------------------
2448 procedure Expand_N_Allocator
(N
: Node_Id
) is
2449 PtrT
: constant Entity_Id
:= Etype
(N
);
2450 Dtyp
: constant Entity_Id
:= Designated_Type
(PtrT
);
2452 Loc
: constant Source_Ptr
:= Sloc
(N
);
2457 -- RM E.2.3(22). We enforce that the expected type of an allocator
2458 -- shall not be a remote access-to-class-wide-limited-private type
2460 -- Why is this being done at expansion time, seems clearly wrong ???
2462 Validate_Remote_Access_To_Class_Wide_Type
(N
);
2464 -- Set the Storage Pool
2466 Set_Storage_Pool
(N
, Associated_Storage_Pool
(Root_Type
(PtrT
)));
2468 if Present
(Storage_Pool
(N
)) then
2469 if Is_RTE
(Storage_Pool
(N
), RE_SS_Pool
) then
2471 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
2474 elsif Is_Class_Wide_Type
(Etype
(Storage_Pool
(N
))) then
2475 Set_Procedure_To_Call
(N
, RTE
(RE_Allocate_Any
));
2478 Set_Procedure_To_Call
(N
,
2479 Find_Prim_Op
(Etype
(Storage_Pool
(N
)), Name_Allocate
));
2483 -- Under certain circumstances we can replace an allocator by an
2484 -- access to statically allocated storage. The conditions, as noted
2485 -- in AARM 3.10 (10c) are as follows:
2487 -- Size and initial value is known at compile time
2488 -- Access type is access-to-constant
2490 -- The allocator is not part of a constraint on a record component,
2491 -- because in that case the inserted actions are delayed until the
2492 -- record declaration is fully analyzed, which is too late for the
2493 -- analysis of the rewritten allocator.
2495 if Is_Access_Constant
(PtrT
)
2496 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
2497 and then Compile_Time_Known_Value
(Expression
(Expression
(N
)))
2498 and then Size_Known_At_Compile_Time
(Etype
(Expression
2500 and then not Is_Record_Type
(Current_Scope
)
2502 -- Here we can do the optimization. For the allocator
2506 -- We insert an object declaration
2508 -- Tnn : aliased x := y;
2510 -- and replace the allocator by Tnn'Unrestricted_Access.
2511 -- Tnn is marked as requiring static allocation.
2514 Make_Defining_Identifier
(Loc
, New_Internal_Name
('T'));
2516 Desig
:= Subtype_Mark
(Expression
(N
));
2518 -- If context is constrained, use constrained subtype directly,
2519 -- so that the constant is not labelled as having a nomimally
2520 -- unconstrained subtype.
2522 if Entity
(Desig
) = Base_Type
(Dtyp
) then
2523 Desig
:= New_Occurrence_Of
(Dtyp
, Loc
);
2527 Make_Object_Declaration
(Loc
,
2528 Defining_Identifier
=> Temp
,
2529 Aliased_Present
=> True,
2530 Constant_Present
=> Is_Access_Constant
(PtrT
),
2531 Object_Definition
=> Desig
,
2532 Expression
=> Expression
(Expression
(N
))));
2535 Make_Attribute_Reference
(Loc
,
2536 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
2537 Attribute_Name
=> Name_Unrestricted_Access
));
2539 Analyze_And_Resolve
(N
, PtrT
);
2541 -- We set the variable as statically allocated, since we don't
2542 -- want it going on the stack of the current procedure!
2544 Set_Is_Statically_Allocated
(Temp
);
2548 -- Handle case of qualified expression (other than optimization above)
2550 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
2551 Expand_Allocator_Expression
(N
);
2553 -- If the allocator is for a type which requires initialization, and
2554 -- there is no initial value (i.e. operand is a subtype indication
2555 -- rather than a qualifed expression), then we must generate a call
2556 -- to the initialization routine. This is done using an expression
2559 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
2561 -- Here ptr_T is the pointer type for the allocator, and T is the
2562 -- subtype of the allocator. A special case arises if the designated
2563 -- type of the access type is a task or contains tasks. In this case
2564 -- the call to Init (Temp.all ...) is replaced by code that ensures
2565 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
2566 -- for details). In addition, if the type T is a task T, then the
2567 -- first argument to Init must be converted to the task record type.
2571 T
: constant Entity_Id
:= Entity
(Expression
(N
));
2579 Temp_Decl
: Node_Id
;
2580 Temp_Type
: Entity_Id
;
2581 Attach_Level
: Uint
;
2584 if No_Initialization
(N
) then
2587 -- Case of no initialization procedure present
2589 elsif not Has_Non_Null_Base_Init_Proc
(T
) then
2591 -- Case of simple initialization required
2593 if Needs_Simple_Initialization
(T
) then
2594 Rewrite
(Expression
(N
),
2595 Make_Qualified_Expression
(Loc
,
2596 Subtype_Mark
=> New_Occurrence_Of
(T
, Loc
),
2597 Expression
=> Get_Simple_Init_Val
(T
, Loc
)));
2599 Analyze_And_Resolve
(Expression
(Expression
(N
)), T
);
2600 Analyze_And_Resolve
(Expression
(N
), T
);
2601 Set_Paren_Count
(Expression
(Expression
(N
)), 1);
2602 Expand_N_Allocator
(N
);
2604 -- No initialization required
2610 -- Case of initialization procedure present, must be called
2613 Init
:= Base_Init_Proc
(T
);
2616 Make_Defining_Identifier
(Loc
, New_Internal_Name
('P'));
2618 -- Construct argument list for the initialization routine call
2619 -- The CPP constructor needs the address directly
2621 if Is_CPP_Class
(T
) then
2622 Arg1
:= New_Reference_To
(Temp
, Loc
);
2627 Make_Explicit_Dereference
(Loc
,
2628 Prefix
=> New_Reference_To
(Temp
, Loc
));
2629 Set_Assignment_OK
(Arg1
);
2632 -- The initialization procedure expects a specific type.
2633 -- if the context is access to class wide, indicate that
2634 -- the object being allocated has the right specific type.
2636 if Is_Class_Wide_Type
(Dtyp
) then
2637 Arg1
:= Unchecked_Convert_To
(T
, Arg1
);
2641 -- If designated type is a concurrent type or if it is a
2642 -- private type whose definition is a concurrent type,
2643 -- the first argument in the Init routine has to be
2644 -- unchecked conversion to the corresponding record type.
2645 -- If the designated type is a derived type, we also
2646 -- convert the argument to its root type.
2648 if Is_Concurrent_Type
(T
) then
2650 Unchecked_Convert_To
(Corresponding_Record_Type
(T
), Arg1
);
2652 elsif Is_Private_Type
(T
)
2653 and then Present
(Full_View
(T
))
2654 and then Is_Concurrent_Type
(Full_View
(T
))
2657 Unchecked_Convert_To
2658 (Corresponding_Record_Type
(Full_View
(T
)), Arg1
);
2660 elsif Etype
(First_Formal
(Init
)) /= Base_Type
(T
) then
2663 Ftyp
: constant Entity_Id
:= Etype
(First_Formal
(Init
));
2666 Arg1
:= OK_Convert_To
(Etype
(Ftyp
), Arg1
);
2667 Set_Etype
(Arg1
, Ftyp
);
2671 Args
:= New_List
(Arg1
);
2673 -- For the task case, pass the Master_Id of the access type
2674 -- as the value of the _Master parameter, and _Chain as the
2675 -- value of the _Chain parameter (_Chain will be defined as
2676 -- part of the generated code for the allocator).
2678 if Has_Task
(T
) then
2679 if No
(Master_Id
(Base_Type
(PtrT
))) then
2681 -- The designated type was an incomplete type, and
2682 -- the access type did not get expanded. Salvage
2685 Expand_N_Full_Type_Declaration
2686 (Parent
(Base_Type
(PtrT
)));
2689 -- If the context of the allocator is a declaration or
2690 -- an assignment, we can generate a meaningful image for
2691 -- it, even though subsequent assignments might remove
2692 -- the connection between task and entity. We build this
2693 -- image when the left-hand side is a simple variable,
2694 -- a simple indexed assignment or a simple selected
2697 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
2699 Nam
: constant Node_Id
:= Name
(Parent
(N
));
2702 if Is_Entity_Name
(Nam
) then
2704 Build_Task_Image_Decls
(
2707 (Entity
(Nam
), Sloc
(Nam
)), T
);
2709 elsif (Nkind
(Nam
) = N_Indexed_Component
2710 or else Nkind
(Nam
) = N_Selected_Component
)
2711 and then Is_Entity_Name
(Prefix
(Nam
))
2714 Build_Task_Image_Decls
2715 (Loc
, Nam
, Etype
(Prefix
(Nam
)));
2717 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
2721 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
2723 Build_Task_Image_Decls
(
2724 Loc
, Defining_Identifier
(Parent
(N
)), T
);
2727 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
2732 (Master_Id
(Base_Type
(Root_Type
(PtrT
))), Loc
));
2733 Append_To
(Args
, Make_Identifier
(Loc
, Name_uChain
));
2735 Decl
:= Last
(Decls
);
2737 New_Occurrence_Of
(Defining_Identifier
(Decl
), Loc
));
2739 -- Has_Task is false, Decls not used
2745 -- Add discriminants if discriminated type
2747 if Has_Discriminants
(T
) then
2748 Discr
:= First_Elmt
(Discriminant_Constraint
(T
));
2750 while Present
(Discr
) loop
2751 Append
(New_Copy_Tree
(Elists
.Node
(Discr
)), Args
);
2755 elsif Is_Private_Type
(T
)
2756 and then Present
(Full_View
(T
))
2757 and then Has_Discriminants
(Full_View
(T
))
2760 First_Elmt
(Discriminant_Constraint
(Full_View
(T
)));
2762 while Present
(Discr
) loop
2763 Append
(New_Copy_Tree
(Elists
.Node
(Discr
)), Args
);
2768 -- We set the allocator as analyzed so that when we analyze the
2769 -- expression actions node, we do not get an unwanted recursive
2770 -- expansion of the allocator expression.
2772 Set_Analyzed
(N
, True);
2773 Node
:= Relocate_Node
(N
);
2775 -- Here is the transformation:
2777 -- output: Temp : constant ptr_T := new T;
2778 -- Init (Temp.all, ...);
2779 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
2780 -- <CTRL> Initialize (Finalizable (Temp.all));
2782 -- Here ptr_T is the pointer type for the allocator, and T
2783 -- is the subtype of the allocator.
2786 Make_Object_Declaration
(Loc
,
2787 Defining_Identifier
=> Temp
,
2788 Constant_Present
=> True,
2789 Object_Definition
=> New_Reference_To
(Temp_Type
, Loc
),
2790 Expression
=> Node
);
2792 Set_Assignment_OK
(Temp_Decl
);
2794 if Is_CPP_Class
(T
) then
2795 Set_Aliased_Present
(Temp_Decl
);
2798 Insert_Action
(N
, Temp_Decl
, Suppress
=> All_Checks
);
2800 -- If the designated type is task type or contains tasks,
2801 -- Create block to activate created tasks, and insert
2802 -- declaration for Task_Image variable ahead of call.
2804 if Has_Task
(T
) then
2806 L
: constant List_Id
:= New_List
;
2810 Build_Task_Allocate_Block
(L
, Node
, Args
);
2813 Insert_List_Before
(First
(Declarations
(Blk
)), Decls
);
2814 Insert_Actions
(N
, L
);
2819 Make_Procedure_Call_Statement
(Loc
,
2820 Name
=> New_Reference_To
(Init
, Loc
),
2821 Parameter_Associations
=> Args
));
2824 if Controlled_Type
(T
) then
2825 Flist
:= Get_Allocator_Final_List
(N
, Base_Type
(T
), PtrT
);
2826 if Ekind
(PtrT
) = E_Anonymous_Access_Type
then
2827 Attach_Level
:= Uint_1
;
2829 Attach_Level
:= Uint_2
;
2833 Ref
=> New_Copy_Tree
(Arg1
),
2836 With_Attach
=> Make_Integer_Literal
(Loc
,
2840 if Is_CPP_Class
(T
) then
2842 Make_Attribute_Reference
(Loc
,
2843 Prefix
=> New_Reference_To
(Temp
, Loc
),
2844 Attribute_Name
=> Name_Unchecked_Access
));
2846 Rewrite
(N
, New_Reference_To
(Temp
, Loc
));
2849 Analyze_And_Resolve
(N
, PtrT
);
2855 when RE_Not_Available
=>
2857 end Expand_N_Allocator
;
2859 -----------------------
2860 -- Expand_N_And_Then --
2861 -----------------------
2863 -- Expand into conditional expression if Actions present, and also
2864 -- deal with optimizing case of arguments being True or False.
2866 procedure Expand_N_And_Then
(N
: Node_Id
) is
2867 Loc
: constant Source_Ptr
:= Sloc
(N
);
2868 Typ
: constant Entity_Id
:= Etype
(N
);
2869 Left
: constant Node_Id
:= Left_Opnd
(N
);
2870 Right
: constant Node_Id
:= Right_Opnd
(N
);
2874 -- Deal with non-standard booleans
2876 if Is_Boolean_Type
(Typ
) then
2877 Adjust_Condition
(Left
);
2878 Adjust_Condition
(Right
);
2879 Set_Etype
(N
, Standard_Boolean
);
2882 -- Check for cases of left argument is True or False
2884 if Nkind
(Left
) = N_Identifier
then
2886 -- If left argument is True, change (True and then Right) to Right.
2887 -- Any actions associated with Right will be executed unconditionally
2888 -- and can thus be inserted into the tree unconditionally.
2890 if Entity
(Left
) = Standard_True
then
2891 if Present
(Actions
(N
)) then
2892 Insert_Actions
(N
, Actions
(N
));
2896 Adjust_Result_Type
(N
, Typ
);
2899 -- If left argument is False, change (False and then Right) to
2900 -- False. In this case we can forget the actions associated with
2901 -- Right, since they will never be executed.
2903 elsif Entity
(Left
) = Standard_False
then
2904 Kill_Dead_Code
(Right
);
2905 Kill_Dead_Code
(Actions
(N
));
2906 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
2907 Adjust_Result_Type
(N
, Typ
);
2912 -- If Actions are present, we expand
2914 -- left and then right
2918 -- if left then right else false end
2920 -- with the actions becoming the Then_Actions of the conditional
2921 -- expression. This conditional expression is then further expanded
2922 -- (and will eventually disappear)
2924 if Present
(Actions
(N
)) then
2925 Actlist
:= Actions
(N
);
2927 Make_Conditional_Expression
(Loc
,
2928 Expressions
=> New_List
(
2931 New_Occurrence_Of
(Standard_False
, Loc
))));
2933 Set_Then_Actions
(N
, Actlist
);
2934 Analyze_And_Resolve
(N
, Standard_Boolean
);
2935 Adjust_Result_Type
(N
, Typ
);
2939 -- No actions present, check for cases of right argument True/False
2941 if Nkind
(Right
) = N_Identifier
then
2943 -- Change (Left and then True) to Left. Note that we know there
2944 -- are no actions associated with the True operand, since we
2945 -- just checked for this case above.
2947 if Entity
(Right
) = Standard_True
then
2950 -- Change (Left and then False) to False, making sure to preserve
2951 -- any side effects associated with the Left operand.
2953 elsif Entity
(Right
) = Standard_False
then
2954 Remove_Side_Effects
(Left
);
2956 (N
, New_Occurrence_Of
(Standard_False
, Loc
));
2960 Adjust_Result_Type
(N
, Typ
);
2961 end Expand_N_And_Then
;
2963 -------------------------------------
2964 -- Expand_N_Conditional_Expression --
2965 -------------------------------------
2967 -- Expand into expression actions if then/else actions present
2969 procedure Expand_N_Conditional_Expression
(N
: Node_Id
) is
2970 Loc
: constant Source_Ptr
:= Sloc
(N
);
2971 Cond
: constant Node_Id
:= First
(Expressions
(N
));
2972 Thenx
: constant Node_Id
:= Next
(Cond
);
2973 Elsex
: constant Node_Id
:= Next
(Thenx
);
2974 Typ
: constant Entity_Id
:= Etype
(N
);
2979 -- If either then or else actions are present, then given:
2981 -- if cond then then-expr else else-expr end
2983 -- we insert the following sequence of actions (using Insert_Actions):
2988 -- Cnn := then-expr;
2994 -- and replace the conditional expression by a reference to Cnn
2996 if Present
(Then_Actions
(N
)) or else Present
(Else_Actions
(N
)) then
2997 Cnn
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('C'));
3000 Make_Implicit_If_Statement
(N
,
3001 Condition
=> Relocate_Node
(Cond
),
3003 Then_Statements
=> New_List
(
3004 Make_Assignment_Statement
(Sloc
(Thenx
),
3005 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Thenx
)),
3006 Expression
=> Relocate_Node
(Thenx
))),
3008 Else_Statements
=> New_List
(
3009 Make_Assignment_Statement
(Sloc
(Elsex
),
3010 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Elsex
)),
3011 Expression
=> Relocate_Node
(Elsex
))));
3013 Set_Assignment_OK
(Name
(First
(Then_Statements
(New_If
))));
3014 Set_Assignment_OK
(Name
(First
(Else_Statements
(New_If
))));
3016 if Present
(Then_Actions
(N
)) then
3018 (First
(Then_Statements
(New_If
)), Then_Actions
(N
));
3021 if Present
(Else_Actions
(N
)) then
3023 (First
(Else_Statements
(New_If
)), Else_Actions
(N
));
3026 Rewrite
(N
, New_Occurrence_Of
(Cnn
, Loc
));
3029 Make_Object_Declaration
(Loc
,
3030 Defining_Identifier
=> Cnn
,
3031 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
)));
3033 Insert_Action
(N
, New_If
);
3034 Analyze_And_Resolve
(N
, Typ
);
3036 end Expand_N_Conditional_Expression
;
3038 -----------------------------------
3039 -- Expand_N_Explicit_Dereference --
3040 -----------------------------------
3042 procedure Expand_N_Explicit_Dereference
(N
: Node_Id
) is
3044 -- Insert explicit dereference call for the checked storage pool case
3046 Insert_Dereference_Action
(Prefix
(N
));
3047 end Expand_N_Explicit_Dereference
;
3053 procedure Expand_N_In
(N
: Node_Id
) is
3054 Loc
: constant Source_Ptr
:= Sloc
(N
);
3055 Rtyp
: constant Entity_Id
:= Etype
(N
);
3056 Lop
: constant Node_Id
:= Left_Opnd
(N
);
3057 Rop
: constant Node_Id
:= Right_Opnd
(N
);
3058 Static
: constant Boolean := Is_OK_Static_Expression
(N
);
3060 procedure Substitute_Valid_Check
;
3061 -- Replaces node N by Lop'Valid. This is done when we have an explicit
3062 -- test for the left operand being in range of its subtype.
3064 ----------------------------
3065 -- Substitute_Valid_Check --
3066 ----------------------------
3068 procedure Substitute_Valid_Check
is
3071 Make_Attribute_Reference
(Loc
,
3072 Prefix
=> Relocate_Node
(Lop
),
3073 Attribute_Name
=> Name_Valid
));
3075 Analyze_And_Resolve
(N
, Rtyp
);
3077 Error_Msg_N
("?explicit membership test may be optimized away", N
);
3078 Error_Msg_N
("\?use ''Valid attribute instead", N
);
3080 end Substitute_Valid_Check
;
3082 -- Start of processing for Expand_N_In
3085 -- Check case of explicit test for an expression in range of its
3086 -- subtype. This is suspicious usage and we replace it with a 'Valid
3087 -- test and give a warning.
3089 if Is_Scalar_Type
(Etype
(Lop
))
3090 and then Nkind
(Rop
) in N_Has_Entity
3091 and then Etype
(Lop
) = Entity
(Rop
)
3092 and then Comes_From_Source
(N
)
3094 Substitute_Valid_Check
;
3098 -- Case of explicit range
3100 if Nkind
(Rop
) = N_Range
then
3102 Lo
: constant Node_Id
:= Low_Bound
(Rop
);
3103 Hi
: constant Node_Id
:= High_Bound
(Rop
);
3105 Lo_Orig
: constant Node_Id
:= Original_Node
(Lo
);
3106 Hi_Orig
: constant Node_Id
:= Original_Node
(Hi
);
3108 Lcheck
: constant Compare_Result
:= Compile_Time_Compare
(Lop
, Lo
);
3109 Ucheck
: constant Compare_Result
:= Compile_Time_Compare
(Lop
, Hi
);
3112 -- If test is explicit x'first .. x'last, replace by valid check
3114 if Is_Scalar_Type
(Etype
(Lop
))
3115 and then Nkind
(Lo_Orig
) = N_Attribute_Reference
3116 and then Attribute_Name
(Lo_Orig
) = Name_First
3117 and then Nkind
(Prefix
(Lo_Orig
)) in N_Has_Entity
3118 and then Entity
(Prefix
(Lo_Orig
)) = Etype
(Lop
)
3119 and then Nkind
(Hi_Orig
) = N_Attribute_Reference
3120 and then Attribute_Name
(Hi_Orig
) = Name_Last
3121 and then Nkind
(Prefix
(Hi_Orig
)) in N_Has_Entity
3122 and then Entity
(Prefix
(Hi_Orig
)) = Etype
(Lop
)
3123 and then Comes_From_Source
(N
)
3125 Substitute_Valid_Check
;
3129 -- If we have an explicit range, do a bit of optimization based
3130 -- on range analysis (we may be able to kill one or both checks).
3132 -- If either check is known to fail, replace result by False since
3133 -- the other check does not matter. Preserve the static flag for
3134 -- legality checks, because we are constant-folding beyond RM 4.9.
3136 if Lcheck
= LT
or else Ucheck
= GT
then
3138 New_Reference_To
(Standard_False
, Loc
));
3139 Analyze_And_Resolve
(N
, Rtyp
);
3140 Set_Is_Static_Expression
(N
, Static
);
3143 -- If both checks are known to succeed, replace result
3144 -- by True, since we know we are in range.
3146 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
3148 New_Reference_To
(Standard_True
, Loc
));
3149 Analyze_And_Resolve
(N
, Rtyp
);
3150 Set_Is_Static_Expression
(N
, Static
);
3153 -- If lower bound check succeeds and upper bound check is
3154 -- not known to succeed or fail, then replace the range check
3155 -- with a comparison against the upper bound.
3157 elsif Lcheck
in Compare_GE
then
3161 Right_Opnd
=> High_Bound
(Rop
)));
3162 Analyze_And_Resolve
(N
, Rtyp
);
3165 -- If upper bound check succeeds and lower bound check is
3166 -- not known to succeed or fail, then replace the range check
3167 -- with a comparison against the lower bound.
3169 elsif Ucheck
in Compare_LE
then
3173 Right_Opnd
=> Low_Bound
(Rop
)));
3174 Analyze_And_Resolve
(N
, Rtyp
);
3179 -- For all other cases of an explicit range, nothing to be done
3183 -- Here right operand is a subtype mark
3187 Typ
: Entity_Id
:= Etype
(Rop
);
3188 Is_Acc
: constant Boolean := Is_Access_Type
(Typ
);
3189 Obj
: Node_Id
:= Lop
;
3190 Cond
: Node_Id
:= Empty
;
3193 Remove_Side_Effects
(Obj
);
3195 -- For tagged type, do tagged membership operation
3197 if Is_Tagged_Type
(Typ
) then
3199 -- No expansion will be performed when Java_VM, as the
3200 -- JVM back end will handle the membership tests directly
3201 -- (tags are not explicitly represented in Java objects,
3202 -- so the normal tagged membership expansion is not what
3206 Rewrite
(N
, Tagged_Membership
(N
));
3207 Analyze_And_Resolve
(N
, Rtyp
);
3212 -- If type is scalar type, rewrite as x in t'first .. t'last
3213 -- This reason we do this is that the bounds may have the wrong
3214 -- type if they come from the original type definition.
3216 elsif Is_Scalar_Type
(Typ
) then
3220 Make_Attribute_Reference
(Loc
,
3221 Attribute_Name
=> Name_First
,
3222 Prefix
=> New_Reference_To
(Typ
, Loc
)),
3225 Make_Attribute_Reference
(Loc
,
3226 Attribute_Name
=> Name_Last
,
3227 Prefix
=> New_Reference_To
(Typ
, Loc
))));
3228 Analyze_And_Resolve
(N
, Rtyp
);
3231 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
3232 -- a membership test if the subtype mark denotes a constrained
3233 -- Unchecked_Union subtype and the expression lacks inferable
3236 elsif Is_Unchecked_Union
(Base_Type
(Typ
))
3237 and then Is_Constrained
(Typ
)
3238 and then not Has_Inferable_Discriminants
(Lop
)
3241 Make_Raise_Program_Error
(Loc
,
3242 Reason
=> PE_Unchecked_Union_Restriction
));
3244 -- Prevent Gigi from generating incorrect code by rewriting
3245 -- the test as a standard False.
3248 New_Occurrence_Of
(Standard_False
, Loc
));
3253 -- Here we have a non-scalar type
3256 Typ
:= Designated_Type
(Typ
);
3259 if not Is_Constrained
(Typ
) then
3261 New_Reference_To
(Standard_True
, Loc
));
3262 Analyze_And_Resolve
(N
, Rtyp
);
3264 -- For the constrained array case, we have to check the
3265 -- subscripts for an exact match if the lengths are
3266 -- non-zero (the lengths must match in any case).
3268 elsif Is_Array_Type
(Typ
) then
3270 Check_Subscripts
: declare
3271 function Construct_Attribute_Reference
3274 Dim
: Nat
) return Node_Id
;
3275 -- Build attribute reference E'Nam(Dim)
3277 -----------------------------------
3278 -- Construct_Attribute_Reference --
3279 -----------------------------------
3281 function Construct_Attribute_Reference
3284 Dim
: Nat
) return Node_Id
3288 Make_Attribute_Reference
(Loc
,
3290 Attribute_Name
=> Nam
,
3291 Expressions
=> New_List
(
3292 Make_Integer_Literal
(Loc
, Dim
)));
3293 end Construct_Attribute_Reference
;
3295 -- Start processing for Check_Subscripts
3298 for J
in 1 .. Number_Dimensions
(Typ
) loop
3299 Evolve_And_Then
(Cond
,
3302 Construct_Attribute_Reference
3303 (Duplicate_Subexpr_No_Checks
(Obj
),
3306 Construct_Attribute_Reference
3307 (New_Occurrence_Of
(Typ
, Loc
), Name_First
, J
)));
3309 Evolve_And_Then
(Cond
,
3312 Construct_Attribute_Reference
3313 (Duplicate_Subexpr_No_Checks
(Obj
),
3316 Construct_Attribute_Reference
3317 (New_Occurrence_Of
(Typ
, Loc
), Name_Last
, J
)));
3326 Right_Opnd
=> Make_Null
(Loc
)),
3327 Right_Opnd
=> Cond
);
3331 Analyze_And_Resolve
(N
, Rtyp
);
3332 end Check_Subscripts
;
3334 -- These are the cases where constraint checks may be
3335 -- required, e.g. records with possible discriminants
3338 -- Expand the test into a series of discriminant comparisons.
3339 -- The expression that is built is the negation of the one
3340 -- that is used for checking discriminant constraints.
3342 Obj
:= Relocate_Node
(Left_Opnd
(N
));
3344 if Has_Discriminants
(Typ
) then
3345 Cond
:= Make_Op_Not
(Loc
,
3346 Right_Opnd
=> Build_Discriminant_Checks
(Obj
, Typ
));
3349 Cond
:= Make_Or_Else
(Loc
,
3353 Right_Opnd
=> Make_Null
(Loc
)),
3354 Right_Opnd
=> Cond
);
3358 Cond
:= New_Occurrence_Of
(Standard_True
, Loc
);
3362 Analyze_And_Resolve
(N
, Rtyp
);
3368 --------------------------------
3369 -- Expand_N_Indexed_Component --
3370 --------------------------------
3372 procedure Expand_N_Indexed_Component
(N
: Node_Id
) is
3373 Loc
: constant Source_Ptr
:= Sloc
(N
);
3374 Typ
: constant Entity_Id
:= Etype
(N
);
3375 P
: constant Node_Id
:= Prefix
(N
);
3376 T
: constant Entity_Id
:= Etype
(P
);
3379 -- A special optimization, if we have an indexed component that
3380 -- is selecting from a slice, then we can eliminate the slice,
3381 -- since, for example, x (i .. j)(k) is identical to x(k). The
3382 -- only difference is the range check required by the slice. The
3383 -- range check for the slice itself has already been generated.
3384 -- The range check for the subscripting operation is ensured
3385 -- by converting the subject to the subtype of the slice.
3387 -- This optimization not only generates better code, avoiding
3388 -- slice messing especially in the packed case, but more importantly
3389 -- bypasses some problems in handling this peculiar case, for
3390 -- example, the issue of dealing specially with object renamings.
3392 if Nkind
(P
) = N_Slice
then
3394 Make_Indexed_Component
(Loc
,
3395 Prefix
=> Prefix
(P
),
3396 Expressions
=> New_List
(
3398 (Etype
(First_Index
(Etype
(P
))),
3399 First
(Expressions
(N
))))));
3400 Analyze_And_Resolve
(N
, Typ
);
3404 -- If the prefix is an access type, then we unconditionally rewrite
3405 -- if as an explicit deference. This simplifies processing for several
3406 -- cases, including packed array cases and certain cases in which
3407 -- checks must be generated. We used to try to do this only when it
3408 -- was necessary, but it cleans up the code to do it all the time.
3410 if Is_Access_Type
(T
) then
3411 Insert_Explicit_Dereference
(P
);
3412 Analyze_And_Resolve
(P
, Designated_Type
(T
));
3415 -- Generate index and validity checks
3417 Generate_Index_Checks
(N
);
3419 if Validity_Checks_On
and then Validity_Check_Subscripts
then
3420 Apply_Subscript_Validity_Checks
(N
);
3423 -- All done for the non-packed case
3425 if not Is_Packed
(Etype
(Prefix
(N
))) then
3429 -- For packed arrays that are not bit-packed (i.e. the case of an array
3430 -- with one or more index types with a non-coniguous enumeration type),
3431 -- we can always use the normal packed element get circuit.
3433 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
3434 Expand_Packed_Element_Reference
(N
);
3438 -- For a reference to a component of a bit packed array, we have to
3439 -- convert it to a reference to the corresponding Packed_Array_Type.
3440 -- We only want to do this for simple references, and not for:
3442 -- Left side of assignment, or prefix of left side of assignment,
3443 -- or prefix of the prefix, to handle packed arrays of packed arrays,
3444 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
3446 -- Renaming objects in renaming associations
3447 -- This case is handled when a use of the renamed variable occurs
3449 -- Actual parameters for a procedure call
3450 -- This case is handled in Exp_Ch6.Expand_Actuals
3452 -- The second expression in a 'Read attribute reference
3454 -- The prefix of an address or size attribute reference
3456 -- The following circuit detects these exceptions
3459 Child
: Node_Id
:= N
;
3460 Parnt
: Node_Id
:= Parent
(N
);
3464 if Nkind
(Parnt
) = N_Unchecked_Expression
then
3467 elsif Nkind
(Parnt
) = N_Object_Renaming_Declaration
3468 or else Nkind
(Parnt
) = N_Procedure_Call_Statement
3469 or else (Nkind
(Parnt
) = N_Parameter_Association
3471 Nkind
(Parent
(Parnt
)) = N_Procedure_Call_Statement
)
3475 elsif Nkind
(Parnt
) = N_Attribute_Reference
3476 and then (Attribute_Name
(Parnt
) = Name_Address
3478 Attribute_Name
(Parnt
) = Name_Size
)
3479 and then Prefix
(Parnt
) = Child
3483 elsif Nkind
(Parnt
) = N_Assignment_Statement
3484 and then Name
(Parnt
) = Child
3488 -- If the expression is an index of an indexed component,
3489 -- it must be expanded regardless of context.
3491 elsif Nkind
(Parnt
) = N_Indexed_Component
3492 and then Child
/= Prefix
(Parnt
)
3494 Expand_Packed_Element_Reference
(N
);
3497 elsif Nkind
(Parent
(Parnt
)) = N_Assignment_Statement
3498 and then Name
(Parent
(Parnt
)) = Parnt
3502 elsif Nkind
(Parnt
) = N_Attribute_Reference
3503 and then Attribute_Name
(Parnt
) = Name_Read
3504 and then Next
(First
(Expressions
(Parnt
))) = Child
3508 elsif (Nkind
(Parnt
) = N_Indexed_Component
3509 or else Nkind
(Parnt
) = N_Selected_Component
)
3510 and then Prefix
(Parnt
) = Child
3515 Expand_Packed_Element_Reference
(N
);
3519 -- Keep looking up tree for unchecked expression, or if we are
3520 -- the prefix of a possible assignment left side.
3523 Parnt
:= Parent
(Child
);
3526 end Expand_N_Indexed_Component
;
3528 ---------------------
3529 -- Expand_N_Not_In --
3530 ---------------------
3532 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
3533 -- can be done. This avoids needing to duplicate this expansion code.
3535 procedure Expand_N_Not_In
(N
: Node_Id
) is
3536 Loc
: constant Source_Ptr
:= Sloc
(N
);
3537 Typ
: constant Entity_Id
:= Etype
(N
);
3538 Cfs
: constant Boolean := Comes_From_Source
(N
);
3545 Left_Opnd
=> Left_Opnd
(N
),
3546 Right_Opnd
=> Right_Opnd
(N
))));
3548 -- We want this tp appear as coming from source if original does (see
3549 -- tranformations in Expand_N_In).
3551 Set_Comes_From_Source
(N
, Cfs
);
3552 Set_Comes_From_Source
(Right_Opnd
(N
), Cfs
);
3554 -- Now analyze tranformed node
3556 Analyze_And_Resolve
(N
, Typ
);
3557 end Expand_N_Not_In
;
3563 -- The only replacement required is for the case of a null of type
3564 -- that is an access to protected subprogram. We represent such
3565 -- access values as a record, and so we must replace the occurrence
3566 -- of null by the equivalent record (with a null address and a null
3567 -- pointer in it), so that the backend creates the proper value.
3569 procedure Expand_N_Null
(N
: Node_Id
) is
3570 Loc
: constant Source_Ptr
:= Sloc
(N
);
3571 Typ
: constant Entity_Id
:= Etype
(N
);
3575 if Ekind
(Typ
) = E_Access_Protected_Subprogram_Type
then
3577 Make_Aggregate
(Loc
,
3578 Expressions
=> New_List
(
3579 New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
),
3583 Analyze_And_Resolve
(N
, Equivalent_Type
(Typ
));
3585 -- For subsequent semantic analysis, the node must retain its
3586 -- type. Gigi in any case replaces this type by the corresponding
3587 -- record type before processing the node.
3593 when RE_Not_Available
=>
3597 ---------------------
3598 -- Expand_N_Op_Abs --
3599 ---------------------
3601 procedure Expand_N_Op_Abs
(N
: Node_Id
) is
3602 Loc
: constant Source_Ptr
:= Sloc
(N
);
3603 Expr
: constant Node_Id
:= Right_Opnd
(N
);
3606 Unary_Op_Validity_Checks
(N
);
3608 -- Deal with software overflow checking
3610 if not Backend_Overflow_Checks_On_Target
3611 and then Is_Signed_Integer_Type
(Etype
(N
))
3612 and then Do_Overflow_Check
(N
)
3614 -- The only case to worry about is when the argument is
3615 -- equal to the largest negative number, so what we do is
3616 -- to insert the check:
3618 -- [constraint_error when Expr = typ'Base'First]
3620 -- with the usual Duplicate_Subexpr use coding for expr
3623 Make_Raise_Constraint_Error
(Loc
,
3626 Left_Opnd
=> Duplicate_Subexpr
(Expr
),
3628 Make_Attribute_Reference
(Loc
,
3630 New_Occurrence_Of
(Base_Type
(Etype
(Expr
)), Loc
),
3631 Attribute_Name
=> Name_First
)),
3632 Reason
=> CE_Overflow_Check_Failed
));
3635 -- Vax floating-point types case
3637 if Vax_Float
(Etype
(N
)) then
3638 Expand_Vax_Arith
(N
);
3640 end Expand_N_Op_Abs
;
3642 ---------------------
3643 -- Expand_N_Op_Add --
3644 ---------------------
3646 procedure Expand_N_Op_Add
(N
: Node_Id
) is
3647 Typ
: constant Entity_Id
:= Etype
(N
);
3650 Binary_Op_Validity_Checks
(N
);
3652 -- N + 0 = 0 + N = N for integer types
3654 if Is_Integer_Type
(Typ
) then
3655 if Compile_Time_Known_Value
(Right_Opnd
(N
))
3656 and then Expr_Value
(Right_Opnd
(N
)) = Uint_0
3658 Rewrite
(N
, Left_Opnd
(N
));
3661 elsif Compile_Time_Known_Value
(Left_Opnd
(N
))
3662 and then Expr_Value
(Left_Opnd
(N
)) = Uint_0
3664 Rewrite
(N
, Right_Opnd
(N
));
3669 -- Arithmetic overflow checks for signed integer/fixed point types
3671 if Is_Signed_Integer_Type
(Typ
)
3672 or else Is_Fixed_Point_Type
(Typ
)
3674 Apply_Arithmetic_Overflow_Check
(N
);
3677 -- Vax floating-point types case
3679 elsif Vax_Float
(Typ
) then
3680 Expand_Vax_Arith
(N
);
3682 end Expand_N_Op_Add
;
3684 ---------------------
3685 -- Expand_N_Op_And --
3686 ---------------------
3688 procedure Expand_N_Op_And
(N
: Node_Id
) is
3689 Typ
: constant Entity_Id
:= Etype
(N
);
3692 Binary_Op_Validity_Checks
(N
);
3694 if Is_Array_Type
(Etype
(N
)) then
3695 Expand_Boolean_Operator
(N
);
3697 elsif Is_Boolean_Type
(Etype
(N
)) then
3698 Adjust_Condition
(Left_Opnd
(N
));
3699 Adjust_Condition
(Right_Opnd
(N
));
3700 Set_Etype
(N
, Standard_Boolean
);
3701 Adjust_Result_Type
(N
, Typ
);
3703 end Expand_N_Op_And
;
3705 ------------------------
3706 -- Expand_N_Op_Concat --
3707 ------------------------
3709 Max_Available_String_Operands
: Int
:= -1;
3710 -- This is initialized the first time this routine is called. It records
3711 -- a value of 0,2,3,4,5 depending on what Str_Concat_n procedures are
3712 -- available in the run-time:
3715 -- 2 RE_Str_Concat available, RE_Str_Concat_3 not available
3716 -- 3 RE_Str_Concat/Concat_2 available, RE_Str_Concat_4 not available
3717 -- 4 RE_Str_Concat/Concat_2/3 available, RE_Str_Concat_5 not available
3718 -- 5 All routines including RE_Str_Concat_5 available
3720 Char_Concat_Available
: Boolean;
3721 -- Records if the routines RE_Str_Concat_CC/CS/SC are available. True if
3722 -- all three are available, False if any one of these is unavailable.
3724 procedure Expand_N_Op_Concat
(N
: Node_Id
) is
3726 -- List of operands to be concatenated
3729 -- Single operand for concatenation
3732 -- Node which is to be replaced by the result of concatenating
3733 -- the nodes in the list Opnds.
3736 -- Array type of concatenation result type
3739 -- Component type of concatenation represented by Cnode
3742 -- Initialize global variables showing run-time status
3744 if Max_Available_String_Operands
< 1 then
3745 if not RTE_Available
(RE_Str_Concat
) then
3746 Max_Available_String_Operands
:= 0;
3747 elsif not RTE_Available
(RE_Str_Concat_3
) then
3748 Max_Available_String_Operands
:= 2;
3749 elsif not RTE_Available
(RE_Str_Concat_4
) then
3750 Max_Available_String_Operands
:= 3;
3751 elsif not RTE_Available
(RE_Str_Concat_5
) then
3752 Max_Available_String_Operands
:= 4;
3754 Max_Available_String_Operands
:= 5;
3757 Char_Concat_Available
:=
3758 RTE_Available
(RE_Str_Concat_CC
)
3760 RTE_Available
(RE_Str_Concat_CS
)
3762 RTE_Available
(RE_Str_Concat_SC
);
3765 -- Ensure validity of both operands
3767 Binary_Op_Validity_Checks
(N
);
3769 -- If we are the left operand of a concatenation higher up the
3770 -- tree, then do nothing for now, since we want to deal with a
3771 -- series of concatenations as a unit.
3773 if Nkind
(Parent
(N
)) = N_Op_Concat
3774 and then N
= Left_Opnd
(Parent
(N
))
3779 -- We get here with a concatenation whose left operand may be a
3780 -- concatenation itself with a consistent type. We need to process
3781 -- these concatenation operands from left to right, which means
3782 -- from the deepest node in the tree to the highest node.
3785 while Nkind
(Left_Opnd
(Cnode
)) = N_Op_Concat
loop
3786 Cnode
:= Left_Opnd
(Cnode
);
3789 -- Now Opnd is the deepest Opnd, and its parents are the concatenation
3790 -- nodes above, so now we process bottom up, doing the operations. We
3791 -- gather a string that is as long as possible up to five operands
3793 -- The outer loop runs more than once if there are more than five
3794 -- concatenations of type Standard.String, the most we handle for
3795 -- this case, or if more than one concatenation type is involved.
3798 Opnds
:= New_List
(Left_Opnd
(Cnode
), Right_Opnd
(Cnode
));
3799 Set_Parent
(Opnds
, N
);
3801 -- The inner loop gathers concatenation operands. We gather any
3802 -- number of these in the non-string case, or if no concatenation
3803 -- routines are available for string (since in that case we will
3804 -- treat string like any other non-string case). Otherwise we only
3805 -- gather as many operands as can be handled by the available
3806 -- procedures in the run-time library (normally 5, but may be
3807 -- less for the configurable run-time case).
3809 Inner
: while Cnode
/= N
3810 and then (Base_Type
(Etype
(Cnode
)) /= Standard_String
3812 Max_Available_String_Operands
= 0
3814 List_Length
(Opnds
) <
3815 Max_Available_String_Operands
)
3816 and then Base_Type
(Etype
(Cnode
)) =
3817 Base_Type
(Etype
(Parent
(Cnode
)))
3819 Cnode
:= Parent
(Cnode
);
3820 Append
(Right_Opnd
(Cnode
), Opnds
);
3823 -- Here we process the collected operands. First we convert
3824 -- singleton operands to singleton aggregates. This is skipped
3825 -- however for the case of two operands of type String, since
3826 -- we have special routines for these cases.
3828 Atyp
:= Base_Type
(Etype
(Cnode
));
3829 Ctyp
:= Base_Type
(Component_Type
(Etype
(Cnode
)));
3831 if (List_Length
(Opnds
) > 2 or else Atyp
/= Standard_String
)
3832 or else not Char_Concat_Available
3834 Opnd
:= First
(Opnds
);
3836 if Base_Type
(Etype
(Opnd
)) = Ctyp
then
3838 Make_Aggregate
(Sloc
(Cnode
),
3839 Expressions
=> New_List
(Relocate_Node
(Opnd
))));
3840 Analyze_And_Resolve
(Opnd
, Atyp
);
3844 exit when No
(Opnd
);
3848 -- Now call appropriate continuation routine
3850 if Atyp
= Standard_String
3851 and then Max_Available_String_Operands
> 0
3853 Expand_Concatenate_String
(Cnode
, Opnds
);
3855 Expand_Concatenate_Other
(Cnode
, Opnds
);
3858 exit Outer
when Cnode
= N
;
3859 Cnode
:= Parent
(Cnode
);
3861 end Expand_N_Op_Concat
;
3863 ------------------------
3864 -- Expand_N_Op_Divide --
3865 ------------------------
3867 procedure Expand_N_Op_Divide
(N
: Node_Id
) is
3868 Loc
: constant Source_Ptr
:= Sloc
(N
);
3869 Ltyp
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
3870 Rtyp
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
3871 Typ
: Entity_Id
:= Etype
(N
);
3874 Binary_Op_Validity_Checks
(N
);
3876 -- N / 1 = N for integer types
3878 if Is_Integer_Type
(Typ
)
3879 and then Compile_Time_Known_Value
(Right_Opnd
(N
))
3880 and then Expr_Value
(Right_Opnd
(N
)) = Uint_1
3882 Rewrite
(N
, Left_Opnd
(N
));
3886 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
3887 -- Is_Power_Of_2_For_Shift is set means that we know that our left
3888 -- operand is an unsigned integer, as required for this to work.
3890 if Nkind
(Right_Opnd
(N
)) = N_Op_Expon
3891 and then Is_Power_Of_2_For_Shift
(Right_Opnd
(N
))
3893 -- We cannot do this transformation in configurable run time mode if we
3894 -- have 64-bit -- integers and long shifts are not available.
3898 or else Support_Long_Shifts_On_Target
)
3901 Make_Op_Shift_Right
(Loc
,
3902 Left_Opnd
=> Left_Opnd
(N
),
3904 Convert_To
(Standard_Natural
, Right_Opnd
(Right_Opnd
(N
)))));
3905 Analyze_And_Resolve
(N
, Typ
);
3909 -- Do required fixup of universal fixed operation
3911 if Typ
= Universal_Fixed
then
3912 Fixup_Universal_Fixed_Operation
(N
);
3916 -- Divisions with fixed-point results
3918 if Is_Fixed_Point_Type
(Typ
) then
3920 -- No special processing if Treat_Fixed_As_Integer is set,
3921 -- since from a semantic point of view such operations are
3922 -- simply integer operations and will be treated that way.
3924 if not Treat_Fixed_As_Integer
(N
) then
3925 if Is_Integer_Type
(Rtyp
) then
3926 Expand_Divide_Fixed_By_Integer_Giving_Fixed
(N
);
3928 Expand_Divide_Fixed_By_Fixed_Giving_Fixed
(N
);
3932 -- Other cases of division of fixed-point operands. Again we
3933 -- exclude the case where Treat_Fixed_As_Integer is set.
3935 elsif (Is_Fixed_Point_Type
(Ltyp
) or else
3936 Is_Fixed_Point_Type
(Rtyp
))
3937 and then not Treat_Fixed_As_Integer
(N
)
3939 if Is_Integer_Type
(Typ
) then
3940 Expand_Divide_Fixed_By_Fixed_Giving_Integer
(N
);
3942 pragma Assert
(Is_Floating_Point_Type
(Typ
));
3943 Expand_Divide_Fixed_By_Fixed_Giving_Float
(N
);
3946 -- Mixed-mode operations can appear in a non-static universal
3947 -- context, in which case the integer argument must be converted
3950 elsif Typ
= Universal_Real
3951 and then Is_Integer_Type
(Rtyp
)
3953 Rewrite
(Right_Opnd
(N
),
3954 Convert_To
(Universal_Real
, Relocate_Node
(Right_Opnd
(N
))));
3956 Analyze_And_Resolve
(Right_Opnd
(N
), Universal_Real
);
3958 elsif Typ
= Universal_Real
3959 and then Is_Integer_Type
(Ltyp
)
3961 Rewrite
(Left_Opnd
(N
),
3962 Convert_To
(Universal_Real
, Relocate_Node
(Left_Opnd
(N
))));
3964 Analyze_And_Resolve
(Left_Opnd
(N
), Universal_Real
);
3966 -- Non-fixed point cases, do integer zero divide and overflow checks
3968 elsif Is_Integer_Type
(Typ
) then
3969 Apply_Divide_Check
(N
);
3971 -- Check for 64-bit division available
3973 if Esize
(Ltyp
) > 32
3974 and then not Support_64_Bit_Divides_On_Target
3976 Error_Msg_CRT
("64-bit division", N
);
3979 -- Deal with Vax_Float
3981 elsif Vax_Float
(Typ
) then
3982 Expand_Vax_Arith
(N
);
3985 end Expand_N_Op_Divide
;
3987 --------------------
3988 -- Expand_N_Op_Eq --
3989 --------------------
3991 procedure Expand_N_Op_Eq
(N
: Node_Id
) is
3992 Loc
: constant Source_Ptr
:= Sloc
(N
);
3993 Typ
: constant Entity_Id
:= Etype
(N
);
3994 Lhs
: constant Node_Id
:= Left_Opnd
(N
);
3995 Rhs
: constant Node_Id
:= Right_Opnd
(N
);
3996 Bodies
: constant List_Id
:= New_List
;
3997 A_Typ
: constant Entity_Id
:= Etype
(Lhs
);
3999 Typl
: Entity_Id
:= A_Typ
;
4000 Op_Name
: Entity_Id
;
4003 procedure Build_Equality_Call
(Eq
: Entity_Id
);
4004 -- If a constructed equality exists for the type or for its parent,
4005 -- build and analyze call, adding conversions if the operation is
4008 function Has_Unconstrained_UU_Component
(Typ
: Node_Id
) return Boolean;
4009 -- Determines whether a type has a subcompoment of an unconstrained
4010 -- Unchecked_Union subtype. Typ is a record type.
4012 -------------------------
4013 -- Build_Equality_Call --
4014 -------------------------
4016 procedure Build_Equality_Call
(Eq
: Entity_Id
) is
4017 Op_Type
: constant Entity_Id
:= Etype
(First_Formal
(Eq
));
4018 L_Exp
: Node_Id
:= Relocate_Node
(Lhs
);
4019 R_Exp
: Node_Id
:= Relocate_Node
(Rhs
);
4022 if Base_Type
(Op_Type
) /= Base_Type
(A_Typ
)
4023 and then not Is_Class_Wide_Type
(A_Typ
)
4025 L_Exp
:= OK_Convert_To
(Op_Type
, L_Exp
);
4026 R_Exp
:= OK_Convert_To
(Op_Type
, R_Exp
);
4029 -- If we have an Unchecked_Union, we need to add the inferred
4030 -- discriminant values as actuals in the function call. At this
4031 -- point, the expansion has determined that both operands have
4032 -- inferable discriminants.
4034 if Is_Unchecked_Union
(Op_Type
) then
4036 Lhs_Type
: constant Node_Id
:= Etype
(L_Exp
);
4037 Rhs_Type
: constant Node_Id
:= Etype
(R_Exp
);
4038 Lhs_Discr_Val
: Node_Id
;
4039 Rhs_Discr_Val
: Node_Id
;
4042 -- Per-object constrained selected components require special
4043 -- attention. If the enclosing scope of the component is an
4044 -- Unchecked_Union, we cannot reference its discriminants
4045 -- directly. This is why we use the two extra parameters of
4046 -- the equality function of the enclosing Unchecked_Union.
4048 -- type UU_Type (Discr : Integer := 0) is
4051 -- pragma Unchecked_Union (UU_Type);
4053 -- 1. Unchecked_Union enclosing record:
4055 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
4057 -- Comp : UU_Type (Discr);
4059 -- end Enclosing_UU_Type;
4060 -- pragma Unchecked_Union (Enclosing_UU_Type);
4062 -- Obj1 : Enclosing_UU_Type;
4063 -- Obj2 : Enclosing_UU_Type (1);
4065 -- [. . .] Obj1 = Obj2 [. . .]
4069 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
4071 -- A and B are the formal parameters of the equality function
4072 -- of Enclosing_UU_Type. The function always has two extra
4073 -- formals to capture the inferred discriminant values.
4075 -- 2. Non-Unchecked_Union enclosing record:
4078 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
4081 -- Comp : UU_Type (Discr);
4083 -- end Enclosing_Non_UU_Type;
4085 -- Obj1 : Enclosing_Non_UU_Type;
4086 -- Obj2 : Enclosing_Non_UU_Type (1);
4088 -- ... Obj1 = Obj2 ...
4092 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
4093 -- obj1.discr, obj2.discr)) then
4095 -- In this case we can directly reference the discriminants of
4096 -- the enclosing record.
4100 if Nkind
(Lhs
) = N_Selected_Component
4101 and then Has_Per_Object_Constraint
4102 (Entity
(Selector_Name
(Lhs
)))
4104 -- Enclosing record is an Unchecked_Union, use formal A
4106 if Is_Unchecked_Union
(Scope
4107 (Entity
(Selector_Name
(Lhs
))))
4110 Make_Identifier
(Loc
,
4113 -- Enclosing record is of a non-Unchecked_Union type, it is
4114 -- possible to reference the discriminant.
4118 Make_Selected_Component
(Loc
,
4119 Prefix
=> Prefix
(Lhs
),
4122 (Get_Discriminant_Value
4123 (First_Discriminant
(Lhs_Type
),
4125 Stored_Constraint
(Lhs_Type
))));
4128 -- Comment needed here ???
4131 -- Infer the discriminant value
4135 (Get_Discriminant_Value
4136 (First_Discriminant
(Lhs_Type
),
4138 Stored_Constraint
(Lhs_Type
)));
4143 if Nkind
(Rhs
) = N_Selected_Component
4144 and then Has_Per_Object_Constraint
4145 (Entity
(Selector_Name
(Rhs
)))
4147 if Is_Unchecked_Union
4148 (Scope
(Entity
(Selector_Name
(Rhs
))))
4151 Make_Identifier
(Loc
,
4156 Make_Selected_Component
(Loc
,
4157 Prefix
=> Prefix
(Rhs
),
4159 New_Copy
(Get_Discriminant_Value
(
4160 First_Discriminant
(Rhs_Type
),
4162 Stored_Constraint
(Rhs_Type
))));
4167 New_Copy
(Get_Discriminant_Value
(
4168 First_Discriminant
(Rhs_Type
),
4170 Stored_Constraint
(Rhs_Type
)));
4175 Make_Function_Call
(Loc
,
4176 Name
=> New_Reference_To
(Eq
, Loc
),
4177 Parameter_Associations
=> New_List
(
4184 -- Normal case, not an unchecked union
4188 Make_Function_Call
(Loc
,
4189 Name
=> New_Reference_To
(Eq
, Loc
),
4190 Parameter_Associations
=> New_List
(L_Exp
, R_Exp
)));
4193 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
4194 end Build_Equality_Call
;
4196 ------------------------------------
4197 -- Has_Unconstrained_UU_Component --
4198 ------------------------------------
4200 function Has_Unconstrained_UU_Component
4201 (Typ
: Node_Id
) return Boolean
4203 Tdef
: constant Node_Id
:=
4204 Type_Definition
(Declaration_Node
(Base_Type
(Typ
)));
4208 function Component_Is_Unconstrained_UU
4209 (Comp
: Node_Id
) return Boolean;
4210 -- Determines whether the subtype of the component is an
4211 -- unconstrained Unchecked_Union.
4213 function Variant_Is_Unconstrained_UU
4214 (Variant
: Node_Id
) return Boolean;
4215 -- Determines whether a component of the variant has an unconstrained
4216 -- Unchecked_Union subtype.
4218 -----------------------------------
4219 -- Component_Is_Unconstrained_UU --
4220 -----------------------------------
4222 function Component_Is_Unconstrained_UU
4223 (Comp
: Node_Id
) return Boolean
4226 if Nkind
(Comp
) /= N_Component_Declaration
then
4231 Sindic
: constant Node_Id
:=
4232 Subtype_Indication
(Component_Definition
(Comp
));
4235 -- Unconstrained nominal type. In the case of a constraint
4236 -- present, the node kind would have been N_Subtype_Indication.
4238 if Nkind
(Sindic
) = N_Identifier
then
4239 return Is_Unchecked_Union
(Base_Type
(Etype
(Sindic
)));
4244 end Component_Is_Unconstrained_UU
;
4246 ---------------------------------
4247 -- Variant_Is_Unconstrained_UU --
4248 ---------------------------------
4250 function Variant_Is_Unconstrained_UU
4251 (Variant
: Node_Id
) return Boolean
4253 Clist
: constant Node_Id
:= Component_List
(Variant
);
4256 if Is_Empty_List
(Component_Items
(Clist
)) then
4260 -- We only need to test one component
4263 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
4266 while Present
(Comp
) loop
4267 if Component_Is_Unconstrained_UU
(Comp
) then
4275 -- None of the components withing the variant were of
4276 -- unconstrained Unchecked_Union type.
4279 end Variant_Is_Unconstrained_UU
;
4281 -- Start of processing for Has_Unconstrained_UU_Component
4284 if Null_Present
(Tdef
) then
4288 Clist
:= Component_List
(Tdef
);
4289 Vpart
:= Variant_Part
(Clist
);
4291 -- Inspect available components
4293 if Present
(Component_Items
(Clist
)) then
4295 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
4298 while Present
(Comp
) loop
4300 -- One component is sufficent
4302 if Component_Is_Unconstrained_UU
(Comp
) then
4311 -- Inspect available components withing variants
4313 if Present
(Vpart
) then
4315 Variant
: Node_Id
:= First
(Variants
(Vpart
));
4318 while Present
(Variant
) loop
4320 -- One component within a variant is sufficent
4322 if Variant_Is_Unconstrained_UU
(Variant
) then
4331 -- Neither the available components, nor the components inside the
4332 -- variant parts were of an unconstrained Unchecked_Union subtype.
4335 end Has_Unconstrained_UU_Component
;
4337 -- Start of processing for Expand_N_Op_Eq
4340 Binary_Op_Validity_Checks
(N
);
4342 if Ekind
(Typl
) = E_Private_Type
then
4343 Typl
:= Underlying_Type
(Typl
);
4344 elsif Ekind
(Typl
) = E_Private_Subtype
then
4345 Typl
:= Underlying_Type
(Base_Type
(Typl
));
4350 -- It may happen in error situations that the underlying type is not
4351 -- set. The error will be detected later, here we just defend the
4358 Typl
:= Base_Type
(Typl
);
4360 -- Boolean types (requiring handling of non-standard case)
4362 if Is_Boolean_Type
(Typl
) then
4363 Adjust_Condition
(Left_Opnd
(N
));
4364 Adjust_Condition
(Right_Opnd
(N
));
4365 Set_Etype
(N
, Standard_Boolean
);
4366 Adjust_Result_Type
(N
, Typ
);
4370 elsif Is_Array_Type
(Typl
) then
4372 -- If we are doing full validity checking, then expand out array
4373 -- comparisons to make sure that we check the array elements.
4375 if Validity_Check_Operands
then
4377 Save_Force_Validity_Checks
: constant Boolean :=
4378 Force_Validity_Checks
;
4380 Force_Validity_Checks
:= True;
4382 Expand_Array_Equality
4384 Relocate_Node
(Lhs
),
4385 Relocate_Node
(Rhs
),
4388 Insert_Actions
(N
, Bodies
);
4389 Analyze_And_Resolve
(N
, Standard_Boolean
);
4390 Force_Validity_Checks
:= Save_Force_Validity_Checks
;
4393 -- Packed case where both operands are known aligned
4395 elsif Is_Bit_Packed_Array
(Typl
)
4396 and then not Is_Possibly_Unaligned_Object
(Lhs
)
4397 and then not Is_Possibly_Unaligned_Object
(Rhs
)
4399 Expand_Packed_Eq
(N
);
4401 -- Where the component type is elementary we can use a block bit
4402 -- comparison (if supported on the target) exception in the case
4403 -- of floating-point (negative zero issues require element by
4404 -- element comparison), and atomic types (where we must be sure
4405 -- to load elements independently) and possibly unaligned arrays.
4407 elsif Is_Elementary_Type
(Component_Type
(Typl
))
4408 and then not Is_Floating_Point_Type
(Component_Type
(Typl
))
4409 and then not Is_Atomic
(Component_Type
(Typl
))
4410 and then not Is_Possibly_Unaligned_Object
(Lhs
)
4411 and then not Is_Possibly_Unaligned_Object
(Rhs
)
4412 and then Support_Composite_Compare_On_Target
4416 -- For composite and floating-point cases, expand equality loop
4417 -- to make sure of using proper comparisons for tagged types,
4418 -- and correctly handling the floating-point case.
4422 Expand_Array_Equality
4424 Relocate_Node
(Lhs
),
4425 Relocate_Node
(Rhs
),
4428 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
4429 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
4434 elsif Is_Record_Type
(Typl
) then
4436 -- For tagged types, use the primitive "="
4438 if Is_Tagged_Type
(Typl
) then
4440 -- If this is derived from an untagged private type completed
4441 -- with a tagged type, it does not have a full view, so we
4442 -- use the primitive operations of the private type.
4443 -- This check should no longer be necessary when these
4444 -- types receive their full views ???
4446 if Is_Private_Type
(A_Typ
)
4447 and then not Is_Tagged_Type
(A_Typ
)
4448 and then Is_Derived_Type
(A_Typ
)
4449 and then No
(Full_View
(A_Typ
))
4451 -- Search for equality operation, checking that the
4452 -- operands have the same type. Note that we must find
4453 -- a matching entry, or something is very wrong!
4455 Prim
:= First_Elmt
(Collect_Primitive_Operations
(A_Typ
));
4457 while Present
(Prim
) loop
4458 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
4459 and then Etype
(First_Formal
(Node
(Prim
))) =
4460 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
4462 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
4467 pragma Assert
(Present
(Prim
));
4468 Op_Name
:= Node
(Prim
);
4470 -- Find the type's predefined equality or an overriding
4471 -- user-defined equality. The reason for not simply calling
4472 -- Find_Prim_Op here is that there may be a user-defined
4473 -- overloaded equality op that precedes the equality that
4474 -- we want, so we have to explicitly search (e.g., there
4475 -- could be an equality with two different parameter types).
4478 if Is_Class_Wide_Type
(Typl
) then
4479 Typl
:= Root_Type
(Typl
);
4482 Prim
:= First_Elmt
(Primitive_Operations
(Typl
));
4483 while Present
(Prim
) loop
4484 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
4485 and then Etype
(First_Formal
(Node
(Prim
))) =
4486 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
4488 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
4493 pragma Assert
(Present
(Prim
));
4494 Op_Name
:= Node
(Prim
);
4497 Build_Equality_Call
(Op_Name
);
4499 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
4500 -- predefined equality operator for a type which has a subcomponent
4501 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
4503 elsif Has_Unconstrained_UU_Component
(Typl
) then
4505 Make_Raise_Program_Error
(Loc
,
4506 Reason
=> PE_Unchecked_Union_Restriction
));
4508 -- Prevent Gigi from generating incorrect code by rewriting the
4509 -- equality as a standard False.
4512 New_Occurrence_Of
(Standard_False
, Loc
));
4514 elsif Is_Unchecked_Union
(Typl
) then
4516 -- If we can infer the discriminants of the operands, we make a
4517 -- call to the TSS equality function.
4519 if Has_Inferable_Discriminants
(Lhs
)
4521 Has_Inferable_Discriminants
(Rhs
)
4524 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
4527 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4528 -- the predefined equality operator for an Unchecked_Union type
4529 -- if either of the operands lack inferable discriminants.
4532 Make_Raise_Program_Error
(Loc
,
4533 Reason
=> PE_Unchecked_Union_Restriction
));
4535 -- Prevent Gigi from generating incorrect code by rewriting
4536 -- the equality as a standard False.
4539 New_Occurrence_Of
(Standard_False
, Loc
));
4543 -- If a type support function is present (for complex cases), use it
4545 elsif Present
(TSS
(Root_Type
(Typl
), TSS_Composite_Equality
)) then
4547 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
4549 -- Otherwise expand the component by component equality. Note that
4550 -- we never use block-bit coparisons for records, because of the
4551 -- problems with gaps. The backend will often be able to recombine
4552 -- the separate comparisons that we generate here.
4555 Remove_Side_Effects
(Lhs
);
4556 Remove_Side_Effects
(Rhs
);
4558 Expand_Record_Equality
(N
, Typl
, Lhs
, Rhs
, Bodies
));
4560 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
4561 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
4565 -- Test if result is known at compile time
4567 Rewrite_Comparison
(N
);
4569 -- If we still have comparison for Vax_Float, process it
4571 if Vax_Float
(Typl
) and then Nkind
(N
) in N_Op_Compare
then
4572 Expand_Vax_Comparison
(N
);
4577 -----------------------
4578 -- Expand_N_Op_Expon --
4579 -----------------------
4581 procedure Expand_N_Op_Expon
(N
: Node_Id
) is
4582 Loc
: constant Source_Ptr
:= Sloc
(N
);
4583 Typ
: constant Entity_Id
:= Etype
(N
);
4584 Rtyp
: constant Entity_Id
:= Root_Type
(Typ
);
4585 Base
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
4586 Bastyp
: constant Node_Id
:= Etype
(Base
);
4587 Exp
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
4588 Exptyp
: constant Entity_Id
:= Etype
(Exp
);
4589 Ovflo
: constant Boolean := Do_Overflow_Check
(N
);
4598 Binary_Op_Validity_Checks
(N
);
4600 -- If either operand is of a private type, then we have the use of
4601 -- an intrinsic operator, and we get rid of the privateness, by using
4602 -- root types of underlying types for the actual operation. Otherwise
4603 -- the private types will cause trouble if we expand multiplications
4604 -- or shifts etc. We also do this transformation if the result type
4605 -- is different from the base type.
4607 if Is_Private_Type
(Etype
(Base
))
4609 Is_Private_Type
(Typ
)
4611 Is_Private_Type
(Exptyp
)
4613 Rtyp
/= Root_Type
(Bastyp
)
4616 Bt
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Bastyp
));
4617 Et
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Exptyp
));
4621 Unchecked_Convert_To
(Typ
,
4623 Left_Opnd
=> Unchecked_Convert_To
(Bt
, Base
),
4624 Right_Opnd
=> Unchecked_Convert_To
(Et
, Exp
))));
4625 Analyze_And_Resolve
(N
, Typ
);
4630 -- Test for case of known right argument
4632 if Compile_Time_Known_Value
(Exp
) then
4633 Expv
:= Expr_Value
(Exp
);
4635 -- We only fold small non-negative exponents. You might think we
4636 -- could fold small negative exponents for the real case, but we
4637 -- can't because we are required to raise Constraint_Error for
4638 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
4639 -- See ACVC test C4A012B.
4641 if Expv
>= 0 and then Expv
<= 4 then
4643 -- X ** 0 = 1 (or 1.0)
4646 if Ekind
(Typ
) in Integer_Kind
then
4647 Xnode
:= Make_Integer_Literal
(Loc
, Intval
=> 1);
4649 Xnode
:= Make_Real_Literal
(Loc
, Ureal_1
);
4661 Make_Op_Multiply
(Loc
,
4662 Left_Opnd
=> Duplicate_Subexpr
(Base
),
4663 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
));
4665 -- X ** 3 = X * X * X
4669 Make_Op_Multiply
(Loc
,
4671 Make_Op_Multiply
(Loc
,
4672 Left_Opnd
=> Duplicate_Subexpr
(Base
),
4673 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)),
4674 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
));
4677 -- En : constant base'type := base * base;
4683 Make_Defining_Identifier
(Loc
, New_Internal_Name
('E'));
4685 Insert_Actions
(N
, New_List
(
4686 Make_Object_Declaration
(Loc
,
4687 Defining_Identifier
=> Temp
,
4688 Constant_Present
=> True,
4689 Object_Definition
=> New_Reference_To
(Typ
, Loc
),
4691 Make_Op_Multiply
(Loc
,
4692 Left_Opnd
=> Duplicate_Subexpr
(Base
),
4693 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)))));
4696 Make_Op_Multiply
(Loc
,
4697 Left_Opnd
=> New_Reference_To
(Temp
, Loc
),
4698 Right_Opnd
=> New_Reference_To
(Temp
, Loc
));
4702 Analyze_And_Resolve
(N
, Typ
);
4707 -- Case of (2 ** expression) appearing as an argument of an integer
4708 -- multiplication, or as the right argument of a division of a non-
4709 -- negative integer. In such cases we leave the node untouched, setting
4710 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
4711 -- of the higher level node converts it into a shift.
4713 if Nkind
(Base
) = N_Integer_Literal
4714 and then Intval
(Base
) = 2
4715 and then Is_Integer_Type
(Root_Type
(Exptyp
))
4716 and then Esize
(Root_Type
(Exptyp
)) <= Esize
(Standard_Integer
)
4717 and then Is_Unsigned_Type
(Exptyp
)
4719 and then Nkind
(Parent
(N
)) in N_Binary_Op
4722 P
: constant Node_Id
:= Parent
(N
);
4723 L
: constant Node_Id
:= Left_Opnd
(P
);
4724 R
: constant Node_Id
:= Right_Opnd
(P
);
4727 if (Nkind
(P
) = N_Op_Multiply
4729 ((Is_Integer_Type
(Etype
(L
)) and then R
= N
)
4731 (Is_Integer_Type
(Etype
(R
)) and then L
= N
))
4732 and then not Do_Overflow_Check
(P
))
4735 (Nkind
(P
) = N_Op_Divide
4736 and then Is_Integer_Type
(Etype
(L
))
4737 and then Is_Unsigned_Type
(Etype
(L
))
4739 and then not Do_Overflow_Check
(P
))
4741 Set_Is_Power_Of_2_For_Shift
(N
);
4747 -- Fall through if exponentiation must be done using a runtime routine
4749 -- First deal with modular case
4751 if Is_Modular_Integer_Type
(Rtyp
) then
4753 -- Non-binary case, we call the special exponentiation routine for
4754 -- the non-binary case, converting the argument to Long_Long_Integer
4755 -- and passing the modulus value. Then the result is converted back
4756 -- to the base type.
4758 if Non_Binary_Modulus
(Rtyp
) then
4761 Make_Function_Call
(Loc
,
4762 Name
=> New_Reference_To
(RTE
(RE_Exp_Modular
), Loc
),
4763 Parameter_Associations
=> New_List
(
4764 Convert_To
(Standard_Integer
, Base
),
4765 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
)),
4768 -- Binary case, in this case, we call one of two routines, either
4769 -- the unsigned integer case, or the unsigned long long integer
4770 -- case, with a final "and" operation to do the required mod.
4773 if UI_To_Int
(Esize
(Rtyp
)) <= Standard_Integer_Size
then
4774 Ent
:= RTE
(RE_Exp_Unsigned
);
4776 Ent
:= RTE
(RE_Exp_Long_Long_Unsigned
);
4783 Make_Function_Call
(Loc
,
4784 Name
=> New_Reference_To
(Ent
, Loc
),
4785 Parameter_Associations
=> New_List
(
4786 Convert_To
(Etype
(First_Formal
(Ent
)), Base
),
4789 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
) - 1))));
4793 -- Common exit point for modular type case
4795 Analyze_And_Resolve
(N
, Typ
);
4798 -- Signed integer cases, done using either Integer or Long_Long_Integer.
4799 -- It is not worth having routines for Short_[Short_]Integer, since for
4800 -- most machines it would not help, and it would generate more code that
4801 -- might need certification when a certified run time is required.
4803 -- In the integer cases, we have two routines, one for when overflow
4804 -- checks are required, and one when they are not required, since there
4805 -- is a real gain in omitting checks on many machines.
4807 elsif Rtyp
= Base_Type
(Standard_Long_Long_Integer
)
4808 or else (Rtyp
= Base_Type
(Standard_Long_Integer
)
4810 Esize
(Standard_Long_Integer
) > Esize
(Standard_Integer
))
4811 or else (Rtyp
= Universal_Integer
)
4813 Etyp
:= Standard_Long_Long_Integer
;
4816 Rent
:= RE_Exp_Long_Long_Integer
;
4818 Rent
:= RE_Exn_Long_Long_Integer
;
4821 elsif Is_Signed_Integer_Type
(Rtyp
) then
4822 Etyp
:= Standard_Integer
;
4825 Rent
:= RE_Exp_Integer
;
4827 Rent
:= RE_Exn_Integer
;
4830 -- Floating-point cases, always done using Long_Long_Float. We do not
4831 -- need separate routines for the overflow case here, since in the case
4832 -- of floating-point, we generate infinities anyway as a rule (either
4833 -- that or we automatically trap overflow), and if there is an infinity
4834 -- generated and a range check is required, the check will fail anyway.
4837 pragma Assert
(Is_Floating_Point_Type
(Rtyp
));
4838 Etyp
:= Standard_Long_Long_Float
;
4839 Rent
:= RE_Exn_Long_Long_Float
;
4842 -- Common processing for integer cases and floating-point cases.
4843 -- If we are in the right type, we can call runtime routine directly
4846 and then Rtyp
/= Universal_Integer
4847 and then Rtyp
/= Universal_Real
4850 Make_Function_Call
(Loc
,
4851 Name
=> New_Reference_To
(RTE
(Rent
), Loc
),
4852 Parameter_Associations
=> New_List
(Base
, Exp
)));
4854 -- Otherwise we have to introduce conversions (conversions are also
4855 -- required in the universal cases, since the runtime routine is
4856 -- typed using one of the standard types.
4861 Make_Function_Call
(Loc
,
4862 Name
=> New_Reference_To
(RTE
(Rent
), Loc
),
4863 Parameter_Associations
=> New_List
(
4864 Convert_To
(Etyp
, Base
),
4868 Analyze_And_Resolve
(N
, Typ
);
4872 when RE_Not_Available
=>
4874 end Expand_N_Op_Expon
;
4876 --------------------
4877 -- Expand_N_Op_Ge --
4878 --------------------
4880 procedure Expand_N_Op_Ge
(N
: Node_Id
) is
4881 Typ
: constant Entity_Id
:= Etype
(N
);
4882 Op1
: constant Node_Id
:= Left_Opnd
(N
);
4883 Op2
: constant Node_Id
:= Right_Opnd
(N
);
4884 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
4887 Binary_Op_Validity_Checks
(N
);
4889 if Is_Array_Type
(Typ1
) then
4890 Expand_Array_Comparison
(N
);
4894 if Is_Boolean_Type
(Typ1
) then
4895 Adjust_Condition
(Op1
);
4896 Adjust_Condition
(Op2
);
4897 Set_Etype
(N
, Standard_Boolean
);
4898 Adjust_Result_Type
(N
, Typ
);
4901 Rewrite_Comparison
(N
);
4903 -- If we still have comparison, and Vax_Float type, process it
4905 if Vax_Float
(Typ1
) and then Nkind
(N
) in N_Op_Compare
then
4906 Expand_Vax_Comparison
(N
);
4911 --------------------
4912 -- Expand_N_Op_Gt --
4913 --------------------
4915 procedure Expand_N_Op_Gt
(N
: Node_Id
) is
4916 Typ
: constant Entity_Id
:= Etype
(N
);
4917 Op1
: constant Node_Id
:= Left_Opnd
(N
);
4918 Op2
: constant Node_Id
:= Right_Opnd
(N
);
4919 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
4922 Binary_Op_Validity_Checks
(N
);
4924 if Is_Array_Type
(Typ1
) then
4925 Expand_Array_Comparison
(N
);
4929 if Is_Boolean_Type
(Typ1
) then
4930 Adjust_Condition
(Op1
);
4931 Adjust_Condition
(Op2
);
4932 Set_Etype
(N
, Standard_Boolean
);
4933 Adjust_Result_Type
(N
, Typ
);
4936 Rewrite_Comparison
(N
);
4938 -- If we still have comparison, and Vax_Float type, process it
4940 if Vax_Float
(Typ1
) and then Nkind
(N
) in N_Op_Compare
then
4941 Expand_Vax_Comparison
(N
);
4946 --------------------
4947 -- Expand_N_Op_Le --
4948 --------------------
4950 procedure Expand_N_Op_Le
(N
: Node_Id
) is
4951 Typ
: constant Entity_Id
:= Etype
(N
);
4952 Op1
: constant Node_Id
:= Left_Opnd
(N
);
4953 Op2
: constant Node_Id
:= Right_Opnd
(N
);
4954 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
4957 Binary_Op_Validity_Checks
(N
);
4959 if Is_Array_Type
(Typ1
) then
4960 Expand_Array_Comparison
(N
);
4964 if Is_Boolean_Type
(Typ1
) then
4965 Adjust_Condition
(Op1
);
4966 Adjust_Condition
(Op2
);
4967 Set_Etype
(N
, Standard_Boolean
);
4968 Adjust_Result_Type
(N
, Typ
);
4971 Rewrite_Comparison
(N
);
4973 -- If we still have comparison, and Vax_Float type, process it
4975 if Vax_Float
(Typ1
) and then Nkind
(N
) in N_Op_Compare
then
4976 Expand_Vax_Comparison
(N
);
4981 --------------------
4982 -- Expand_N_Op_Lt --
4983 --------------------
4985 procedure Expand_N_Op_Lt
(N
: Node_Id
) is
4986 Typ
: constant Entity_Id
:= Etype
(N
);
4987 Op1
: constant Node_Id
:= Left_Opnd
(N
);
4988 Op2
: constant Node_Id
:= Right_Opnd
(N
);
4989 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
4992 Binary_Op_Validity_Checks
(N
);
4994 if Is_Array_Type
(Typ1
) then
4995 Expand_Array_Comparison
(N
);
4999 if Is_Boolean_Type
(Typ1
) then
5000 Adjust_Condition
(Op1
);
5001 Adjust_Condition
(Op2
);
5002 Set_Etype
(N
, Standard_Boolean
);
5003 Adjust_Result_Type
(N
, Typ
);
5006 Rewrite_Comparison
(N
);
5008 -- If we still have comparison, and Vax_Float type, process it
5010 if Vax_Float
(Typ1
) and then Nkind
(N
) in N_Op_Compare
then
5011 Expand_Vax_Comparison
(N
);
5016 -----------------------
5017 -- Expand_N_Op_Minus --
5018 -----------------------
5020 procedure Expand_N_Op_Minus
(N
: Node_Id
) is
5021 Loc
: constant Source_Ptr
:= Sloc
(N
);
5022 Typ
: constant Entity_Id
:= Etype
(N
);
5025 Unary_Op_Validity_Checks
(N
);
5027 if not Backend_Overflow_Checks_On_Target
5028 and then Is_Signed_Integer_Type
(Etype
(N
))
5029 and then Do_Overflow_Check
(N
)
5031 -- Software overflow checking expands -expr into (0 - expr)
5034 Make_Op_Subtract
(Loc
,
5035 Left_Opnd
=> Make_Integer_Literal
(Loc
, 0),
5036 Right_Opnd
=> Right_Opnd
(N
)));
5038 Analyze_And_Resolve
(N
, Typ
);
5040 -- Vax floating-point types case
5042 elsif Vax_Float
(Etype
(N
)) then
5043 Expand_Vax_Arith
(N
);
5045 end Expand_N_Op_Minus
;
5047 ---------------------
5048 -- Expand_N_Op_Mod --
5049 ---------------------
5051 procedure Expand_N_Op_Mod
(N
: Node_Id
) is
5052 Loc
: constant Source_Ptr
:= Sloc
(N
);
5053 Typ
: constant Entity_Id
:= Etype
(N
);
5054 Left
: constant Node_Id
:= Left_Opnd
(N
);
5055 Right
: constant Node_Id
:= Right_Opnd
(N
);
5056 DOC
: constant Boolean := Do_Overflow_Check
(N
);
5057 DDC
: constant Boolean := Do_Division_Check
(N
);
5068 Binary_Op_Validity_Checks
(N
);
5070 Determine_Range
(Right
, ROK
, Rlo
, Rhi
);
5071 Determine_Range
(Left
, LOK
, Llo
, Lhi
);
5073 -- Convert mod to rem if operands are known non-negative. We do this
5074 -- since it is quite likely that this will improve the quality of code,
5075 -- (the operation now corresponds to the hardware remainder), and it
5076 -- does not seem likely that it could be harmful.
5078 if LOK
and then Llo
>= 0
5080 ROK
and then Rlo
>= 0
5083 Make_Op_Rem
(Sloc
(N
),
5084 Left_Opnd
=> Left_Opnd
(N
),
5085 Right_Opnd
=> Right_Opnd
(N
)));
5087 -- Instead of reanalyzing the node we do the analysis manually.
5088 -- This avoids anomalies when the replacement is done in an
5089 -- instance and is epsilon more efficient.
5091 Set_Entity
(N
, Standard_Entity
(S_Op_Rem
));
5093 Set_Do_Overflow_Check
(N
, DOC
);
5094 Set_Do_Division_Check
(N
, DDC
);
5095 Expand_N_Op_Rem
(N
);
5098 -- Otherwise, normal mod processing
5101 if Is_Integer_Type
(Etype
(N
)) then
5102 Apply_Divide_Check
(N
);
5105 -- Apply optimization x mod 1 = 0. We don't really need that with
5106 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
5107 -- certainly harmless.
5109 if Is_Integer_Type
(Etype
(N
))
5110 and then Compile_Time_Known_Value
(Right
)
5111 and then Expr_Value
(Right
) = Uint_1
5113 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
5114 Analyze_And_Resolve
(N
, Typ
);
5118 -- Deal with annoying case of largest negative number remainder
5119 -- minus one. Gigi does not handle this case correctly, because
5120 -- it generates a divide instruction which may trap in this case.
5122 -- In fact the check is quite easy, if the right operand is -1,
5123 -- then the mod value is always 0, and we can just ignore the
5124 -- left operand completely in this case.
5126 -- The operand type may be private (e.g. in the expansion of an
5127 -- an intrinsic operation) so we must use the underlying type to
5128 -- get the bounds, and convert the literals explicitly.
5132 (Type_Low_Bound
(Base_Type
(Underlying_Type
(Etype
(Left
)))));
5134 if ((not ROK
) or else (Rlo
<= (-1) and then (-1) <= Rhi
))
5136 ((not LOK
) or else (Llo
= LLB
))
5139 Make_Conditional_Expression
(Loc
,
5140 Expressions
=> New_List
(
5142 Left_Opnd
=> Duplicate_Subexpr
(Right
),
5144 Unchecked_Convert_To
(Typ
,
5145 Make_Integer_Literal
(Loc
, -1))),
5146 Unchecked_Convert_To
(Typ
,
5147 Make_Integer_Literal
(Loc
, Uint_0
)),
5148 Relocate_Node
(N
))));
5150 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
5151 Analyze_And_Resolve
(N
, Typ
);
5154 end Expand_N_Op_Mod
;
5156 --------------------------
5157 -- Expand_N_Op_Multiply --
5158 --------------------------
5160 procedure Expand_N_Op_Multiply
(N
: Node_Id
) is
5161 Loc
: constant Source_Ptr
:= Sloc
(N
);
5162 Lop
: constant Node_Id
:= Left_Opnd
(N
);
5163 Rop
: constant Node_Id
:= Right_Opnd
(N
);
5165 Lp2
: constant Boolean :=
5166 Nkind
(Lop
) = N_Op_Expon
5167 and then Is_Power_Of_2_For_Shift
(Lop
);
5169 Rp2
: constant Boolean :=
5170 Nkind
(Rop
) = N_Op_Expon
5171 and then Is_Power_Of_2_For_Shift
(Rop
);
5173 Ltyp
: constant Entity_Id
:= Etype
(Lop
);
5174 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
5175 Typ
: Entity_Id
:= Etype
(N
);
5178 Binary_Op_Validity_Checks
(N
);
5180 -- Special optimizations for integer types
5182 if Is_Integer_Type
(Typ
) then
5184 -- N * 0 = 0 * N = 0 for integer types
5186 if (Compile_Time_Known_Value
(Rop
)
5187 and then Expr_Value
(Rop
) = Uint_0
)
5189 (Compile_Time_Known_Value
(Lop
)
5190 and then Expr_Value
(Lop
) = Uint_0
)
5192 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
5193 Analyze_And_Resolve
(N
, Typ
);
5197 -- N * 1 = 1 * N = N for integer types
5199 -- This optimisation is not done if we are going to
5200 -- rewrite the product 1 * 2 ** N to a shift.
5202 if Compile_Time_Known_Value
(Rop
)
5203 and then Expr_Value
(Rop
) = Uint_1
5209 elsif Compile_Time_Known_Value
(Lop
)
5210 and then Expr_Value
(Lop
) = Uint_1
5218 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
5219 -- Is_Power_Of_2_For_Shift is set means that we know that our left
5220 -- operand is an integer, as required for this to work.
5225 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
5229 Left_Opnd
=> Make_Integer_Literal
(Loc
, 2),
5232 Left_Opnd
=> Right_Opnd
(Lop
),
5233 Right_Opnd
=> Right_Opnd
(Rop
))));
5234 Analyze_And_Resolve
(N
, Typ
);
5239 Make_Op_Shift_Left
(Loc
,
5242 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))));
5243 Analyze_And_Resolve
(N
, Typ
);
5247 -- Same processing for the operands the other way round
5251 Make_Op_Shift_Left
(Loc
,
5254 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))));
5255 Analyze_And_Resolve
(N
, Typ
);
5259 -- Do required fixup of universal fixed operation
5261 if Typ
= Universal_Fixed
then
5262 Fixup_Universal_Fixed_Operation
(N
);
5266 -- Multiplications with fixed-point results
5268 if Is_Fixed_Point_Type
(Typ
) then
5270 -- No special processing if Treat_Fixed_As_Integer is set,
5271 -- since from a semantic point of view such operations are
5272 -- simply integer operations and will be treated that way.
5274 if not Treat_Fixed_As_Integer
(N
) then
5276 -- Case of fixed * integer => fixed
5278 if Is_Integer_Type
(Rtyp
) then
5279 Expand_Multiply_Fixed_By_Integer_Giving_Fixed
(N
);
5281 -- Case of integer * fixed => fixed
5283 elsif Is_Integer_Type
(Ltyp
) then
5284 Expand_Multiply_Integer_By_Fixed_Giving_Fixed
(N
);
5286 -- Case of fixed * fixed => fixed
5289 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed
(N
);
5293 -- Other cases of multiplication of fixed-point operands. Again
5294 -- we exclude the cases where Treat_Fixed_As_Integer flag is set.
5296 elsif (Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
))
5297 and then not Treat_Fixed_As_Integer
(N
)
5299 if Is_Integer_Type
(Typ
) then
5300 Expand_Multiply_Fixed_By_Fixed_Giving_Integer
(N
);
5302 pragma Assert
(Is_Floating_Point_Type
(Typ
));
5303 Expand_Multiply_Fixed_By_Fixed_Giving_Float
(N
);
5306 -- Mixed-mode operations can appear in a non-static universal
5307 -- context, in which case the integer argument must be converted
5310 elsif Typ
= Universal_Real
5311 and then Is_Integer_Type
(Rtyp
)
5313 Rewrite
(Rop
, Convert_To
(Universal_Real
, Relocate_Node
(Rop
)));
5315 Analyze_And_Resolve
(Rop
, Universal_Real
);
5317 elsif Typ
= Universal_Real
5318 and then Is_Integer_Type
(Ltyp
)
5320 Rewrite
(Lop
, Convert_To
(Universal_Real
, Relocate_Node
(Lop
)));
5322 Analyze_And_Resolve
(Lop
, Universal_Real
);
5324 -- Non-fixed point cases, check software overflow checking required
5326 elsif Is_Signed_Integer_Type
(Etype
(N
)) then
5327 Apply_Arithmetic_Overflow_Check
(N
);
5329 -- Deal with VAX float case
5331 elsif Vax_Float
(Typ
) then
5332 Expand_Vax_Arith
(N
);
5335 end Expand_N_Op_Multiply
;
5337 --------------------
5338 -- Expand_N_Op_Ne --
5339 --------------------
5341 procedure Expand_N_Op_Ne
(N
: Node_Id
) is
5342 Typ
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
5345 -- Case of elementary type with standard operator
5347 if Is_Elementary_Type
(Typ
)
5348 and then Sloc
(Entity
(N
)) = Standard_Location
5350 Binary_Op_Validity_Checks
(N
);
5352 -- Boolean types (requiring handling of non-standard case)
5354 if Is_Boolean_Type
(Typ
) then
5355 Adjust_Condition
(Left_Opnd
(N
));
5356 Adjust_Condition
(Right_Opnd
(N
));
5357 Set_Etype
(N
, Standard_Boolean
);
5358 Adjust_Result_Type
(N
, Typ
);
5361 Rewrite_Comparison
(N
);
5363 -- If we still have comparison for Vax_Float, process it
5365 if Vax_Float
(Typ
) and then Nkind
(N
) in N_Op_Compare
then
5366 Expand_Vax_Comparison
(N
);
5370 -- For all cases other than elementary types, we rewrite node as the
5371 -- negation of an equality operation, and reanalyze. The equality to be
5372 -- used is defined in the same scope and has the same signature. This
5373 -- signature must be set explicitly since in an instance it may not have
5374 -- the same visibility as in the generic unit. This avoids duplicating
5375 -- or factoring the complex code for record/array equality tests etc.
5379 Loc
: constant Source_Ptr
:= Sloc
(N
);
5381 Ne
: constant Entity_Id
:= Entity
(N
);
5384 Binary_Op_Validity_Checks
(N
);
5390 Left_Opnd
=> Left_Opnd
(N
),
5391 Right_Opnd
=> Right_Opnd
(N
)));
5392 Set_Paren_Count
(Right_Opnd
(Neg
), 1);
5394 if Scope
(Ne
) /= Standard_Standard
then
5395 Set_Entity
(Right_Opnd
(Neg
), Corresponding_Equality
(Ne
));
5398 -- For navigation purposes, the inequality is treated as an
5399 -- implicit reference to the corresponding equality. Preserve the
5400 -- Comes_From_ source flag so that the proper Xref entry is
5403 Preserve_Comes_From_Source
(Neg
, N
);
5404 Preserve_Comes_From_Source
(Right_Opnd
(Neg
), N
);
5406 Analyze_And_Resolve
(N
, Standard_Boolean
);
5411 ---------------------
5412 -- Expand_N_Op_Not --
5413 ---------------------
5415 -- If the argument is other than a Boolean array type, there is no
5416 -- special expansion required.
5418 -- For the packed case, we call the special routine in Exp_Pakd, except
5419 -- that if the component size is greater than one, we use the standard
5420 -- routine generating a gruesome loop (it is so peculiar to have packed
5421 -- arrays with non-standard Boolean representations anyway, so it does
5422 -- not matter that we do not handle this case efficiently).
5424 -- For the unpacked case (and for the special packed case where we have
5425 -- non standard Booleans, as discussed above), we generate and insert
5426 -- into the tree the following function definition:
5428 -- function Nnnn (A : arr) is
5431 -- for J in a'range loop
5432 -- B (J) := not A (J);
5437 -- Here arr is the actual subtype of the parameter (and hence always
5438 -- constrained). Then we replace the not with a call to this function.
5440 procedure Expand_N_Op_Not
(N
: Node_Id
) is
5441 Loc
: constant Source_Ptr
:= Sloc
(N
);
5442 Typ
: constant Entity_Id
:= Etype
(N
);
5451 Func_Name
: Entity_Id
;
5452 Loop_Statement
: Node_Id
;
5455 Unary_Op_Validity_Checks
(N
);
5457 -- For boolean operand, deal with non-standard booleans
5459 if Is_Boolean_Type
(Typ
) then
5460 Adjust_Condition
(Right_Opnd
(N
));
5461 Set_Etype
(N
, Standard_Boolean
);
5462 Adjust_Result_Type
(N
, Typ
);
5466 -- Only array types need any other processing
5468 if not Is_Array_Type
(Typ
) then
5472 -- Case of array operand. If bit packed with a component size of 1,
5473 -- handle it in Exp_Pakd if the operand is known to be aligned.
5475 if Is_Bit_Packed_Array
(Typ
)
5476 and then Component_Size
(Typ
) = 1
5477 and then not Is_Possibly_Unaligned_Object
(Right_Opnd
(N
))
5479 Expand_Packed_Not
(N
);
5483 -- Case of array operand which is not bit-packed. If the context is
5484 -- a safe assignment, call in-place operation, If context is a larger
5485 -- boolean expression in the context of a safe assignment, expansion is
5486 -- done by enclosing operation.
5488 Opnd
:= Relocate_Node
(Right_Opnd
(N
));
5489 Convert_To_Actual_Subtype
(Opnd
);
5490 Arr
:= Etype
(Opnd
);
5491 Ensure_Defined
(Arr
, N
);
5493 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
5494 if Safe_In_Place_Array_Op
(Name
(Parent
(N
)), N
, Empty
) then
5495 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
5498 -- Special case the negation of a binary operation
5500 elsif (Nkind
(Opnd
) = N_Op_And
5501 or else Nkind
(Opnd
) = N_Op_Or
5502 or else Nkind
(Opnd
) = N_Op_Xor
)
5503 and then Safe_In_Place_Array_Op
5504 (Name
(Parent
(N
)), Left_Opnd
(Opnd
), Right_Opnd
(Opnd
))
5506 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
5510 elsif Nkind
(Parent
(N
)) in N_Binary_Op
5511 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
5514 Op1
: constant Node_Id
:= Left_Opnd
(Parent
(N
));
5515 Op2
: constant Node_Id
:= Right_Opnd
(Parent
(N
));
5516 Lhs
: constant Node_Id
:= Name
(Parent
(Parent
(N
)));
5519 if Safe_In_Place_Array_Op
(Lhs
, Op1
, Op2
) then
5521 and then Nkind
(Op2
) = N_Op_Not
5523 -- (not A) op (not B) can be reduced to a single call
5528 and then Nkind
(Parent
(N
)) = N_Op_Xor
5530 -- A xor (not B) can also be special-cased
5538 A
:= Make_Defining_Identifier
(Loc
, Name_uA
);
5539 B
:= Make_Defining_Identifier
(Loc
, Name_uB
);
5540 J
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
5543 Make_Indexed_Component
(Loc
,
5544 Prefix
=> New_Reference_To
(A
, Loc
),
5545 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
5548 Make_Indexed_Component
(Loc
,
5549 Prefix
=> New_Reference_To
(B
, Loc
),
5550 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
5553 Make_Implicit_Loop_Statement
(N
,
5554 Identifier
=> Empty
,
5557 Make_Iteration_Scheme
(Loc
,
5558 Loop_Parameter_Specification
=>
5559 Make_Loop_Parameter_Specification
(Loc
,
5560 Defining_Identifier
=> J
,
5561 Discrete_Subtype_Definition
=>
5562 Make_Attribute_Reference
(Loc
,
5563 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
5564 Attribute_Name
=> Name_Range
))),
5566 Statements
=> New_List
(
5567 Make_Assignment_Statement
(Loc
,
5569 Expression
=> Make_Op_Not
(Loc
, A_J
))));
5571 Func_Name
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('N'));
5572 Set_Is_Inlined
(Func_Name
);
5575 Make_Subprogram_Body
(Loc
,
5577 Make_Function_Specification
(Loc
,
5578 Defining_Unit_Name
=> Func_Name
,
5579 Parameter_Specifications
=> New_List
(
5580 Make_Parameter_Specification
(Loc
,
5581 Defining_Identifier
=> A
,
5582 Parameter_Type
=> New_Reference_To
(Typ
, Loc
))),
5583 Result_Definition
=> New_Reference_To
(Typ
, Loc
)),
5585 Declarations
=> New_List
(
5586 Make_Object_Declaration
(Loc
,
5587 Defining_Identifier
=> B
,
5588 Object_Definition
=> New_Reference_To
(Arr
, Loc
))),
5590 Handled_Statement_Sequence
=>
5591 Make_Handled_Sequence_Of_Statements
(Loc
,
5592 Statements
=> New_List
(
5594 Make_Return_Statement
(Loc
,
5596 Make_Identifier
(Loc
, Chars
(B
)))))));
5599 Make_Function_Call
(Loc
,
5600 Name
=> New_Reference_To
(Func_Name
, Loc
),
5601 Parameter_Associations
=> New_List
(Opnd
)));
5603 Analyze_And_Resolve
(N
, Typ
);
5604 end Expand_N_Op_Not
;
5606 --------------------
5607 -- Expand_N_Op_Or --
5608 --------------------
5610 procedure Expand_N_Op_Or
(N
: Node_Id
) is
5611 Typ
: constant Entity_Id
:= Etype
(N
);
5614 Binary_Op_Validity_Checks
(N
);
5616 if Is_Array_Type
(Etype
(N
)) then
5617 Expand_Boolean_Operator
(N
);
5619 elsif Is_Boolean_Type
(Etype
(N
)) then
5620 Adjust_Condition
(Left_Opnd
(N
));
5621 Adjust_Condition
(Right_Opnd
(N
));
5622 Set_Etype
(N
, Standard_Boolean
);
5623 Adjust_Result_Type
(N
, Typ
);
5627 ----------------------
5628 -- Expand_N_Op_Plus --
5629 ----------------------
5631 procedure Expand_N_Op_Plus
(N
: Node_Id
) is
5633 Unary_Op_Validity_Checks
(N
);
5634 end Expand_N_Op_Plus
;
5636 ---------------------
5637 -- Expand_N_Op_Rem --
5638 ---------------------
5640 procedure Expand_N_Op_Rem
(N
: Node_Id
) is
5641 Loc
: constant Source_Ptr
:= Sloc
(N
);
5642 Typ
: constant Entity_Id
:= Etype
(N
);
5644 Left
: constant Node_Id
:= Left_Opnd
(N
);
5645 Right
: constant Node_Id
:= Right_Opnd
(N
);
5656 Binary_Op_Validity_Checks
(N
);
5658 if Is_Integer_Type
(Etype
(N
)) then
5659 Apply_Divide_Check
(N
);
5662 -- Apply optimization x rem 1 = 0. We don't really need that with
5663 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
5664 -- certainly harmless.
5666 if Is_Integer_Type
(Etype
(N
))
5667 and then Compile_Time_Known_Value
(Right
)
5668 and then Expr_Value
(Right
) = Uint_1
5670 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
5671 Analyze_And_Resolve
(N
, Typ
);
5675 -- Deal with annoying case of largest negative number remainder
5676 -- minus one. Gigi does not handle this case correctly, because
5677 -- it generates a divide instruction which may trap in this case.
5679 -- In fact the check is quite easy, if the right operand is -1,
5680 -- then the remainder is always 0, and we can just ignore the
5681 -- left operand completely in this case.
5683 Determine_Range
(Right
, ROK
, Rlo
, Rhi
);
5684 Determine_Range
(Left
, LOK
, Llo
, Lhi
);
5686 -- The operand type may be private (e.g. in the expansion of an
5687 -- an intrinsic operation) so we must use the underlying type to
5688 -- get the bounds, and convert the literals explicitly.
5692 (Type_Low_Bound
(Base_Type
(Underlying_Type
(Etype
(Left
)))));
5694 -- Now perform the test, generating code only if needed
5696 if ((not ROK
) or else (Rlo
<= (-1) and then (-1) <= Rhi
))
5698 ((not LOK
) or else (Llo
= LLB
))
5701 Make_Conditional_Expression
(Loc
,
5702 Expressions
=> New_List
(
5704 Left_Opnd
=> Duplicate_Subexpr
(Right
),
5706 Unchecked_Convert_To
(Typ
,
5707 Make_Integer_Literal
(Loc
, -1))),
5709 Unchecked_Convert_To
(Typ
,
5710 Make_Integer_Literal
(Loc
, Uint_0
)),
5712 Relocate_Node
(N
))));
5714 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
5715 Analyze_And_Resolve
(N
, Typ
);
5717 end Expand_N_Op_Rem
;
5719 -----------------------------
5720 -- Expand_N_Op_Rotate_Left --
5721 -----------------------------
5723 procedure Expand_N_Op_Rotate_Left
(N
: Node_Id
) is
5725 Binary_Op_Validity_Checks
(N
);
5726 end Expand_N_Op_Rotate_Left
;
5728 ------------------------------
5729 -- Expand_N_Op_Rotate_Right --
5730 ------------------------------
5732 procedure Expand_N_Op_Rotate_Right
(N
: Node_Id
) is
5734 Binary_Op_Validity_Checks
(N
);
5735 end Expand_N_Op_Rotate_Right
;
5737 ----------------------------
5738 -- Expand_N_Op_Shift_Left --
5739 ----------------------------
5741 procedure Expand_N_Op_Shift_Left
(N
: Node_Id
) is
5743 Binary_Op_Validity_Checks
(N
);
5744 end Expand_N_Op_Shift_Left
;
5746 -----------------------------
5747 -- Expand_N_Op_Shift_Right --
5748 -----------------------------
5750 procedure Expand_N_Op_Shift_Right
(N
: Node_Id
) is
5752 Binary_Op_Validity_Checks
(N
);
5753 end Expand_N_Op_Shift_Right
;
5755 ----------------------------------------
5756 -- Expand_N_Op_Shift_Right_Arithmetic --
5757 ----------------------------------------
5759 procedure Expand_N_Op_Shift_Right_Arithmetic
(N
: Node_Id
) is
5761 Binary_Op_Validity_Checks
(N
);
5762 end Expand_N_Op_Shift_Right_Arithmetic
;
5764 --------------------------
5765 -- Expand_N_Op_Subtract --
5766 --------------------------
5768 procedure Expand_N_Op_Subtract
(N
: Node_Id
) is
5769 Typ
: constant Entity_Id
:= Etype
(N
);
5772 Binary_Op_Validity_Checks
(N
);
5774 -- N - 0 = N for integer types
5776 if Is_Integer_Type
(Typ
)
5777 and then Compile_Time_Known_Value
(Right_Opnd
(N
))
5778 and then Expr_Value
(Right_Opnd
(N
)) = 0
5780 Rewrite
(N
, Left_Opnd
(N
));
5784 -- Arithemtic overflow checks for signed integer/fixed point types
5786 if Is_Signed_Integer_Type
(Typ
)
5787 or else Is_Fixed_Point_Type
(Typ
)
5789 Apply_Arithmetic_Overflow_Check
(N
);
5791 -- Vax floating-point types case
5793 elsif Vax_Float
(Typ
) then
5794 Expand_Vax_Arith
(N
);
5796 end Expand_N_Op_Subtract
;
5798 ---------------------
5799 -- Expand_N_Op_Xor --
5800 ---------------------
5802 procedure Expand_N_Op_Xor
(N
: Node_Id
) is
5803 Typ
: constant Entity_Id
:= Etype
(N
);
5806 Binary_Op_Validity_Checks
(N
);
5808 if Is_Array_Type
(Etype
(N
)) then
5809 Expand_Boolean_Operator
(N
);
5811 elsif Is_Boolean_Type
(Etype
(N
)) then
5812 Adjust_Condition
(Left_Opnd
(N
));
5813 Adjust_Condition
(Right_Opnd
(N
));
5814 Set_Etype
(N
, Standard_Boolean
);
5815 Adjust_Result_Type
(N
, Typ
);
5817 end Expand_N_Op_Xor
;
5819 ----------------------
5820 -- Expand_N_Or_Else --
5821 ----------------------
5823 -- Expand into conditional expression if Actions present, and also
5824 -- deal with optimizing case of arguments being True or False.
5826 procedure Expand_N_Or_Else
(N
: Node_Id
) is
5827 Loc
: constant Source_Ptr
:= Sloc
(N
);
5828 Typ
: constant Entity_Id
:= Etype
(N
);
5829 Left
: constant Node_Id
:= Left_Opnd
(N
);
5830 Right
: constant Node_Id
:= Right_Opnd
(N
);
5834 -- Deal with non-standard booleans
5836 if Is_Boolean_Type
(Typ
) then
5837 Adjust_Condition
(Left
);
5838 Adjust_Condition
(Right
);
5839 Set_Etype
(N
, Standard_Boolean
);
5842 -- Check for cases of left argument is True or False
5844 if Nkind
(Left
) = N_Identifier
then
5846 -- If left argument is False, change (False or else Right) to Right.
5847 -- Any actions associated with Right will be executed unconditionally
5848 -- and can thus be inserted into the tree unconditionally.
5850 if Entity
(Left
) = Standard_False
then
5851 if Present
(Actions
(N
)) then
5852 Insert_Actions
(N
, Actions
(N
));
5856 Adjust_Result_Type
(N
, Typ
);
5859 -- If left argument is True, change (True and then Right) to
5860 -- True. In this case we can forget the actions associated with
5861 -- Right, since they will never be executed.
5863 elsif Entity
(Left
) = Standard_True
then
5864 Kill_Dead_Code
(Right
);
5865 Kill_Dead_Code
(Actions
(N
));
5866 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
5867 Adjust_Result_Type
(N
, Typ
);
5872 -- If Actions are present, we expand
5874 -- left or else right
5878 -- if left then True else right end
5880 -- with the actions becoming the Else_Actions of the conditional
5881 -- expression. This conditional expression is then further expanded
5882 -- (and will eventually disappear)
5884 if Present
(Actions
(N
)) then
5885 Actlist
:= Actions
(N
);
5887 Make_Conditional_Expression
(Loc
,
5888 Expressions
=> New_List
(
5890 New_Occurrence_Of
(Standard_True
, Loc
),
5893 Set_Else_Actions
(N
, Actlist
);
5894 Analyze_And_Resolve
(N
, Standard_Boolean
);
5895 Adjust_Result_Type
(N
, Typ
);
5899 -- No actions present, check for cases of right argument True/False
5901 if Nkind
(Right
) = N_Identifier
then
5903 -- Change (Left or else False) to Left. Note that we know there
5904 -- are no actions associated with the True operand, since we
5905 -- just checked for this case above.
5907 if Entity
(Right
) = Standard_False
then
5910 -- Change (Left or else True) to True, making sure to preserve
5911 -- any side effects associated with the Left operand.
5913 elsif Entity
(Right
) = Standard_True
then
5914 Remove_Side_Effects
(Left
);
5916 (N
, New_Occurrence_Of
(Standard_True
, Loc
));
5920 Adjust_Result_Type
(N
, Typ
);
5921 end Expand_N_Or_Else
;
5923 -----------------------------------
5924 -- Expand_N_Qualified_Expression --
5925 -----------------------------------
5927 procedure Expand_N_Qualified_Expression
(N
: Node_Id
) is
5928 Operand
: constant Node_Id
:= Expression
(N
);
5929 Target_Type
: constant Entity_Id
:= Entity
(Subtype_Mark
(N
));
5932 Apply_Constraint_Check
(Operand
, Target_Type
, No_Sliding
=> True);
5933 end Expand_N_Qualified_Expression
;
5935 ---------------------------------
5936 -- Expand_N_Selected_Component --
5937 ---------------------------------
5939 -- If the selector is a discriminant of a concurrent object, rewrite the
5940 -- prefix to denote the corresponding record type.
5942 procedure Expand_N_Selected_Component
(N
: Node_Id
) is
5943 Loc
: constant Source_Ptr
:= Sloc
(N
);
5944 Par
: constant Node_Id
:= Parent
(N
);
5945 P
: constant Node_Id
:= Prefix
(N
);
5946 Ptyp
: Entity_Id
:= Underlying_Type
(Etype
(P
));
5951 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean;
5952 -- Gigi needs a temporary for prefixes that depend on a discriminant,
5953 -- unless the context of an assignment can provide size information.
5954 -- Don't we have a general routine that does this???
5956 -----------------------
5957 -- In_Left_Hand_Side --
5958 -----------------------
5960 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean is
5962 return (Nkind
(Parent
(Comp
)) = N_Assignment_Statement
5963 and then Comp
= Name
(Parent
(Comp
)))
5964 or else (Present
(Parent
(Comp
))
5965 and then Nkind
(Parent
(Comp
)) in N_Subexpr
5966 and then In_Left_Hand_Side
(Parent
(Comp
)));
5967 end In_Left_Hand_Side
;
5969 -- Start of processing for Expand_N_Selected_Component
5972 -- Insert explicit dereference if required
5974 if Is_Access_Type
(Ptyp
) then
5975 Insert_Explicit_Dereference
(P
);
5976 Analyze_And_Resolve
(P
, Designated_Type
(Ptyp
));
5978 if Ekind
(Etype
(P
)) = E_Private_Subtype
5979 and then Is_For_Access_Subtype
(Etype
(P
))
5981 Set_Etype
(P
, Base_Type
(Etype
(P
)));
5987 -- Deal with discriminant check required
5989 if Do_Discriminant_Check
(N
) then
5991 -- Present the discrminant checking function to the backend,
5992 -- so that it can inline the call to the function.
5995 (Discriminant_Checking_Func
5996 (Original_Record_Component
(Entity
(Selector_Name
(N
)))));
5998 -- Now reset the flag and generate the call
6000 Set_Do_Discriminant_Check
(N
, False);
6001 Generate_Discriminant_Check
(N
);
6004 -- Gigi cannot handle unchecked conversions that are the prefix of a
6005 -- selected component with discriminants. This must be checked during
6006 -- expansion, because during analysis the type of the selector is not
6007 -- known at the point the prefix is analyzed. If the conversion is the
6008 -- target of an assignment, then we cannot force the evaluation.
6010 if Nkind
(Prefix
(N
)) = N_Unchecked_Type_Conversion
6011 and then Has_Discriminants
(Etype
(N
))
6012 and then not In_Left_Hand_Side
(N
)
6014 Force_Evaluation
(Prefix
(N
));
6017 -- Remaining processing applies only if selector is a discriminant
6019 if Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
then
6021 -- If the selector is a discriminant of a constrained record type,
6022 -- we may be able to rewrite the expression with the actual value
6023 -- of the discriminant, a useful optimization in some cases.
6025 if Is_Record_Type
(Ptyp
)
6026 and then Has_Discriminants
(Ptyp
)
6027 and then Is_Constrained
(Ptyp
)
6029 -- Do this optimization for discrete types only, and not for
6030 -- access types (access discriminants get us into trouble!)
6032 if not Is_Discrete_Type
(Etype
(N
)) then
6035 -- Don't do this on the left hand of an assignment statement.
6036 -- Normally one would think that references like this would
6037 -- not occur, but they do in generated code, and mean that
6038 -- we really do want to assign the discriminant!
6040 elsif Nkind
(Par
) = N_Assignment_Statement
6041 and then Name
(Par
) = N
6045 -- Don't do this optimization for the prefix of an attribute
6046 -- or the operand of an object renaming declaration since these
6047 -- are contexts where we do not want the value anyway.
6049 elsif (Nkind
(Par
) = N_Attribute_Reference
6050 and then Prefix
(Par
) = N
)
6051 or else Is_Renamed_Object
(N
)
6055 -- Don't do this optimization if we are within the code for a
6056 -- discriminant check, since the whole point of such a check may
6057 -- be to verify the condition on which the code below depends!
6059 elsif Is_In_Discriminant_Check
(N
) then
6062 -- Green light to see if we can do the optimization. There is
6063 -- still one condition that inhibits the optimization below
6064 -- but now is the time to check the particular discriminant.
6067 -- Loop through discriminants to find the matching
6068 -- discriminant constraint to see if we can copy it.
6070 Disc
:= First_Discriminant
(Ptyp
);
6071 Dcon
:= First_Elmt
(Discriminant_Constraint
(Ptyp
));
6072 Discr_Loop
: while Present
(Dcon
) loop
6074 -- Check if this is the matching discriminant
6076 if Disc
= Entity
(Selector_Name
(N
)) then
6078 -- Here we have the matching discriminant. Check for
6079 -- the case of a discriminant of a component that is
6080 -- constrained by an outer discriminant, which cannot
6081 -- be optimized away.
6084 Denotes_Discriminant
6085 (Node
(Dcon
), Check_Protected
=> True)
6089 -- In the context of a case statement, the expression
6090 -- may have the base type of the discriminant, and we
6091 -- need to preserve the constraint to avoid spurious
6092 -- errors on missing cases.
6094 elsif Nkind
(Parent
(N
)) = N_Case_Statement
6095 and then Etype
(Node
(Dcon
)) /= Etype
(Disc
)
6098 Make_Qualified_Expression
(Loc
,
6100 New_Occurrence_Of
(Etype
(Disc
), Loc
),
6102 New_Copy_Tree
(Node
(Dcon
))));
6103 Analyze_And_Resolve
(N
, Etype
(Disc
));
6105 -- In case that comes out as a static expression,
6106 -- reset it (a selected component is never static).
6108 Set_Is_Static_Expression
(N
, False);
6111 -- Otherwise we can just copy the constraint, but the
6112 -- result is certainly not static! In some cases the
6113 -- discriminant constraint has been analyzed in the
6114 -- context of the original subtype indication, but for
6115 -- itypes the constraint might not have been analyzed
6116 -- yet, and this must be done now.
6119 Rewrite
(N
, New_Copy_Tree
(Node
(Dcon
)));
6120 Analyze_And_Resolve
(N
);
6121 Set_Is_Static_Expression
(N
, False);
6127 Next_Discriminant
(Disc
);
6128 end loop Discr_Loop
;
6130 -- Note: the above loop should always find a matching
6131 -- discriminant, but if it does not, we just missed an
6132 -- optimization due to some glitch (perhaps a previous
6133 -- error), so ignore.
6138 -- The only remaining processing is in the case of a discriminant of
6139 -- a concurrent object, where we rewrite the prefix to denote the
6140 -- corresponding record type. If the type is derived and has renamed
6141 -- discriminants, use corresponding discriminant, which is the one
6142 -- that appears in the corresponding record.
6144 if not Is_Concurrent_Type
(Ptyp
) then
6148 Disc
:= Entity
(Selector_Name
(N
));
6150 if Is_Derived_Type
(Ptyp
)
6151 and then Present
(Corresponding_Discriminant
(Disc
))
6153 Disc
:= Corresponding_Discriminant
(Disc
);
6157 Make_Selected_Component
(Loc
,
6159 Unchecked_Convert_To
(Corresponding_Record_Type
(Ptyp
),
6161 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Disc
)));
6166 end Expand_N_Selected_Component
;
6168 --------------------
6169 -- Expand_N_Slice --
6170 --------------------
6172 procedure Expand_N_Slice
(N
: Node_Id
) is
6173 Loc
: constant Source_Ptr
:= Sloc
(N
);
6174 Typ
: constant Entity_Id
:= Etype
(N
);
6175 Pfx
: constant Node_Id
:= Prefix
(N
);
6176 Ptp
: Entity_Id
:= Etype
(Pfx
);
6178 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean;
6179 -- Check whether the argument is an actual for a procedure call,
6180 -- in which case the expansion of a bit-packed slice is deferred
6181 -- until the call itself is expanded. The reason this is required
6182 -- is that we might have an IN OUT or OUT parameter, and the copy out
6183 -- is essential, and that copy out would be missed if we created a
6184 -- temporary here in Expand_N_Slice. Note that we don't bother
6185 -- to test specifically for an IN OUT or OUT mode parameter, since it
6186 -- is a bit tricky to do, and it is harmless to defer expansion
6187 -- in the IN case, since the call processing will still generate the
6188 -- appropriate copy in operation, which will take care of the slice.
6190 procedure Make_Temporary
;
6191 -- Create a named variable for the value of the slice, in
6192 -- cases where the back-end cannot handle it properly, e.g.
6193 -- when packed types or unaligned slices are involved.
6195 -------------------------
6196 -- Is_Procedure_Actual --
6197 -------------------------
6199 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean is
6200 Par
: Node_Id
:= Parent
(N
);
6204 -- If our parent is a procedure call we can return
6206 if Nkind
(Par
) = N_Procedure_Call_Statement
then
6209 -- If our parent is a type conversion, keep climbing the
6210 -- tree, since a type conversion can be a procedure actual.
6211 -- Also keep climbing if parameter association or a qualified
6212 -- expression, since these are additional cases that do can
6213 -- appear on procedure actuals.
6215 elsif Nkind
(Par
) = N_Type_Conversion
6216 or else Nkind
(Par
) = N_Parameter_Association
6217 or else Nkind
(Par
) = N_Qualified_Expression
6219 Par
:= Parent
(Par
);
6221 -- Any other case is not what we are looking for
6227 end Is_Procedure_Actual
;
6229 --------------------
6230 -- Make_Temporary --
6231 --------------------
6233 procedure Make_Temporary
is
6235 Ent
: constant Entity_Id
:=
6236 Make_Defining_Identifier
(Loc
, New_Internal_Name
('T'));
6239 Make_Object_Declaration
(Loc
,
6240 Defining_Identifier
=> Ent
,
6241 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
6243 Set_No_Initialization
(Decl
);
6245 Insert_Actions
(N
, New_List
(
6247 Make_Assignment_Statement
(Loc
,
6248 Name
=> New_Occurrence_Of
(Ent
, Loc
),
6249 Expression
=> Relocate_Node
(N
))));
6251 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
6252 Analyze_And_Resolve
(N
, Typ
);
6255 -- Start of processing for Expand_N_Slice
6258 -- Special handling for access types
6260 if Is_Access_Type
(Ptp
) then
6262 Ptp
:= Designated_Type
(Ptp
);
6265 Make_Explicit_Dereference
(Sloc
(N
),
6266 Prefix
=> Relocate_Node
(Pfx
)));
6268 Analyze_And_Resolve
(Pfx
, Ptp
);
6271 -- Range checks are potentially also needed for cases involving
6272 -- a slice indexed by a subtype indication, but Do_Range_Check
6273 -- can currently only be set for expressions ???
6275 if not Index_Checks_Suppressed
(Ptp
)
6276 and then (not Is_Entity_Name
(Pfx
)
6277 or else not Index_Checks_Suppressed
(Entity
(Pfx
)))
6278 and then Nkind
(Discrete_Range
(N
)) /= N_Subtype_Indication
6280 Enable_Range_Check
(Discrete_Range
(N
));
6283 -- The remaining case to be handled is packed slices. We can leave
6284 -- packed slices as they are in the following situations:
6286 -- 1. Right or left side of an assignment (we can handle this
6287 -- situation correctly in the assignment statement expansion).
6289 -- 2. Prefix of indexed component (the slide is optimized away
6290 -- in this case, see the start of Expand_N_Slice.
6292 -- 3. Object renaming declaration, since we want the name of
6293 -- the slice, not the value.
6295 -- 4. Argument to procedure call, since copy-in/copy-out handling
6296 -- may be required, and this is handled in the expansion of
6299 -- 5. Prefix of an address attribute (this is an error which
6300 -- is caught elsewhere, and the expansion would intefere
6301 -- with generating the error message).
6303 if not Is_Packed
(Typ
) then
6305 -- Apply transformation for actuals of a function call,
6306 -- where Expand_Actuals is not used.
6308 if Nkind
(Parent
(N
)) = N_Function_Call
6309 and then Is_Possibly_Unaligned_Slice
(N
)
6314 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
6315 or else (Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
6316 and then Parent
(N
) = Name
(Parent
(Parent
(N
))))
6320 elsif Nkind
(Parent
(N
)) = N_Indexed_Component
6321 or else Is_Renamed_Object
(N
)
6322 or else Is_Procedure_Actual
(N
)
6326 elsif Nkind
(Parent
(N
)) = N_Attribute_Reference
6327 and then Attribute_Name
(Parent
(N
)) = Name_Address
6336 ------------------------------
6337 -- Expand_N_Type_Conversion --
6338 ------------------------------
6340 procedure Expand_N_Type_Conversion
(N
: Node_Id
) is
6341 Loc
: constant Source_Ptr
:= Sloc
(N
);
6342 Operand
: constant Node_Id
:= Expression
(N
);
6343 Target_Type
: constant Entity_Id
:= Etype
(N
);
6344 Operand_Type
: Entity_Id
:= Etype
(Operand
);
6346 procedure Handle_Changed_Representation
;
6347 -- This is called in the case of record and array type conversions
6348 -- to see if there is a change of representation to be handled.
6349 -- Change of representation is actually handled at the assignment
6350 -- statement level, and what this procedure does is rewrite node N
6351 -- conversion as an assignment to temporary. If there is no change
6352 -- of representation, then the conversion node is unchanged.
6354 procedure Real_Range_Check
;
6355 -- Handles generation of range check for real target value
6357 -----------------------------------
6358 -- Handle_Changed_Representation --
6359 -----------------------------------
6361 procedure Handle_Changed_Representation
is
6370 -- Nothing to do if no change of representation
6372 if Same_Representation
(Operand_Type
, Target_Type
) then
6375 -- The real change of representation work is done by the assignment
6376 -- statement processing. So if this type conversion is appearing as
6377 -- the expression of an assignment statement, nothing needs to be
6378 -- done to the conversion.
6380 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
then
6383 -- Otherwise we need to generate a temporary variable, and do the
6384 -- change of representation assignment into that temporary variable.
6385 -- The conversion is then replaced by a reference to this variable.
6390 -- If type is unconstrained we have to add a constraint,
6391 -- copied from the actual value of the left hand side.
6393 if not Is_Constrained
(Target_Type
) then
6394 if Has_Discriminants
(Operand_Type
) then
6395 Disc
:= First_Discriminant
(Operand_Type
);
6397 if Disc
/= First_Stored_Discriminant
(Operand_Type
) then
6398 Disc
:= First_Stored_Discriminant
(Operand_Type
);
6402 while Present
(Disc
) loop
6404 Make_Selected_Component
(Loc
,
6405 Prefix
=> Duplicate_Subexpr_Move_Checks
(Operand
),
6407 Make_Identifier
(Loc
, Chars
(Disc
))));
6408 Next_Discriminant
(Disc
);
6411 elsif Is_Array_Type
(Operand_Type
) then
6412 N_Ix
:= First_Index
(Target_Type
);
6415 for J
in 1 .. Number_Dimensions
(Operand_Type
) loop
6417 -- We convert the bounds explicitly. We use an unchecked
6418 -- conversion because bounds checks are done elsewhere.
6423 Unchecked_Convert_To
(Etype
(N_Ix
),
6424 Make_Attribute_Reference
(Loc
,
6426 Duplicate_Subexpr_No_Checks
6427 (Operand
, Name_Req
=> True),
6428 Attribute_Name
=> Name_First
,
6429 Expressions
=> New_List
(
6430 Make_Integer_Literal
(Loc
, J
)))),
6433 Unchecked_Convert_To
(Etype
(N_Ix
),
6434 Make_Attribute_Reference
(Loc
,
6436 Duplicate_Subexpr_No_Checks
6437 (Operand
, Name_Req
=> True),
6438 Attribute_Name
=> Name_Last
,
6439 Expressions
=> New_List
(
6440 Make_Integer_Literal
(Loc
, J
))))));
6447 Odef
:= New_Occurrence_Of
(Target_Type
, Loc
);
6449 if Present
(Cons
) then
6451 Make_Subtype_Indication
(Loc
,
6452 Subtype_Mark
=> Odef
,
6454 Make_Index_Or_Discriminant_Constraint
(Loc
,
6455 Constraints
=> Cons
));
6458 Temp
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('C'));
6460 Make_Object_Declaration
(Loc
,
6461 Defining_Identifier
=> Temp
,
6462 Object_Definition
=> Odef
);
6464 Set_No_Initialization
(Decl
, True);
6466 -- Insert required actions. It is essential to suppress checks
6467 -- since we have suppressed default initialization, which means
6468 -- that the variable we create may have no discriminants.
6473 Make_Assignment_Statement
(Loc
,
6474 Name
=> New_Occurrence_Of
(Temp
, Loc
),
6475 Expression
=> Relocate_Node
(N
))),
6476 Suppress
=> All_Checks
);
6478 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
6481 end Handle_Changed_Representation
;
6483 ----------------------
6484 -- Real_Range_Check --
6485 ----------------------
6487 -- Case of conversions to floating-point or fixed-point. If range
6488 -- checks are enabled and the target type has a range constraint,
6495 -- Tnn : typ'Base := typ'Base (x);
6496 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
6499 -- This is necessary when there is a conversion of integer to float
6500 -- or to fixed-point to ensure that the correct checks are made. It
6501 -- is not necessary for float to float where it is enough to simply
6502 -- set the Do_Range_Check flag.
6504 procedure Real_Range_Check
is
6505 Btyp
: constant Entity_Id
:= Base_Type
(Target_Type
);
6506 Lo
: constant Node_Id
:= Type_Low_Bound
(Target_Type
);
6507 Hi
: constant Node_Id
:= Type_High_Bound
(Target_Type
);
6508 Xtyp
: constant Entity_Id
:= Etype
(Operand
);
6513 -- Nothing to do if conversion was rewritten
6515 if Nkind
(N
) /= N_Type_Conversion
then
6519 -- Nothing to do if range checks suppressed, or target has the
6520 -- same range as the base type (or is the base type).
6522 if Range_Checks_Suppressed
(Target_Type
)
6523 or else (Lo
= Type_Low_Bound
(Btyp
)
6525 Hi
= Type_High_Bound
(Btyp
))
6530 -- Nothing to do if expression is an entity on which checks
6531 -- have been suppressed.
6533 if Is_Entity_Name
(Operand
)
6534 and then Range_Checks_Suppressed
(Entity
(Operand
))
6539 -- Nothing to do if bounds are all static and we can tell that
6540 -- the expression is within the bounds of the target. Note that
6541 -- if the operand is of an unconstrained floating-point type,
6542 -- then we do not trust it to be in range (might be infinite)
6545 S_Lo
: constant Node_Id
:= Type_Low_Bound
(Xtyp
);
6546 S_Hi
: constant Node_Id
:= Type_High_Bound
(Xtyp
);
6549 if (not Is_Floating_Point_Type
(Xtyp
)
6550 or else Is_Constrained
(Xtyp
))
6551 and then Compile_Time_Known_Value
(S_Lo
)
6552 and then Compile_Time_Known_Value
(S_Hi
)
6553 and then Compile_Time_Known_Value
(Hi
)
6554 and then Compile_Time_Known_Value
(Lo
)
6557 D_Lov
: constant Ureal
:= Expr_Value_R
(Lo
);
6558 D_Hiv
: constant Ureal
:= Expr_Value_R
(Hi
);
6563 if Is_Real_Type
(Xtyp
) then
6564 S_Lov
:= Expr_Value_R
(S_Lo
);
6565 S_Hiv
:= Expr_Value_R
(S_Hi
);
6567 S_Lov
:= UR_From_Uint
(Expr_Value
(S_Lo
));
6568 S_Hiv
:= UR_From_Uint
(Expr_Value
(S_Hi
));
6572 and then S_Lov
>= D_Lov
6573 and then S_Hiv
<= D_Hiv
6575 Set_Do_Range_Check
(Operand
, False);
6582 -- For float to float conversions, we are done
6584 if Is_Floating_Point_Type
(Xtyp
)
6586 Is_Floating_Point_Type
(Btyp
)
6591 -- Otherwise rewrite the conversion as described above
6593 Conv
:= Relocate_Node
(N
);
6595 (Subtype_Mark
(Conv
), New_Occurrence_Of
(Btyp
, Loc
));
6596 Set_Etype
(Conv
, Btyp
);
6598 -- Enable overflow except for case of integer to float conversions,
6599 -- where it is never required, since we can never have overflow in
6602 if not Is_Integer_Type
(Etype
(Operand
)) then
6603 Enable_Overflow_Check
(Conv
);
6607 Make_Defining_Identifier
(Loc
,
6608 Chars
=> New_Internal_Name
('T'));
6610 Insert_Actions
(N
, New_List
(
6611 Make_Object_Declaration
(Loc
,
6612 Defining_Identifier
=> Tnn
,
6613 Object_Definition
=> New_Occurrence_Of
(Btyp
, Loc
),
6614 Expression
=> Conv
),
6616 Make_Raise_Constraint_Error
(Loc
,
6621 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
6623 Make_Attribute_Reference
(Loc
,
6624 Attribute_Name
=> Name_First
,
6626 New_Occurrence_Of
(Target_Type
, Loc
))),
6630 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
6632 Make_Attribute_Reference
(Loc
,
6633 Attribute_Name
=> Name_Last
,
6635 New_Occurrence_Of
(Target_Type
, Loc
)))),
6636 Reason
=> CE_Range_Check_Failed
)));
6638 Rewrite
(N
, New_Occurrence_Of
(Tnn
, Loc
));
6639 Analyze_And_Resolve
(N
, Btyp
);
6640 end Real_Range_Check
;
6642 -- Start of processing for Expand_N_Type_Conversion
6645 -- Nothing at all to do if conversion is to the identical type
6646 -- so remove the conversion completely, it is useless.
6648 if Operand_Type
= Target_Type
then
6649 Rewrite
(N
, Relocate_Node
(Operand
));
6653 -- Nothing to do if this is the second argument of read. This
6654 -- is a "backwards" conversion that will be handled by the
6655 -- specialized code in attribute processing.
6657 if Nkind
(Parent
(N
)) = N_Attribute_Reference
6658 and then Attribute_Name
(Parent
(N
)) = Name_Read
6659 and then Next
(First
(Expressions
(Parent
(N
)))) = N
6664 -- Here if we may need to expand conversion
6666 -- Special case of converting from non-standard boolean type
6668 if Is_Boolean_Type
(Operand_Type
)
6669 and then (Nonzero_Is_True
(Operand_Type
))
6671 Adjust_Condition
(Operand
);
6672 Set_Etype
(Operand
, Standard_Boolean
);
6673 Operand_Type
:= Standard_Boolean
;
6676 -- Case of converting to an access type
6678 if Is_Access_Type
(Target_Type
) then
6680 -- Apply an accessibility check if the operand is an
6681 -- access parameter. Note that other checks may still
6682 -- need to be applied below (such as tagged type checks).
6684 if Is_Entity_Name
(Operand
)
6685 and then Ekind
(Entity
(Operand
)) in Formal_Kind
6686 and then Ekind
(Etype
(Operand
)) = E_Anonymous_Access_Type
6688 Apply_Accessibility_Check
(Operand
, Target_Type
);
6690 -- If the level of the operand type is statically deeper
6691 -- then the level of the target type, then force Program_Error.
6692 -- Note that this can only occur for cases where the attribute
6693 -- is within the body of an instantiation (otherwise the
6694 -- conversion will already have been rejected as illegal).
6695 -- Note: warnings are issued by the analyzer for the instance
6698 elsif In_Instance_Body
6699 and then Type_Access_Level
(Operand_Type
) >
6700 Type_Access_Level
(Target_Type
)
6703 Make_Raise_Program_Error
(Sloc
(N
),
6704 Reason
=> PE_Accessibility_Check_Failed
));
6705 Set_Etype
(N
, Target_Type
);
6707 -- When the operand is a selected access discriminant
6708 -- the check needs to be made against the level of the
6709 -- object denoted by the prefix of the selected name.
6710 -- Force Program_Error for this case as well (this
6711 -- accessibility violation can only happen if within
6712 -- the body of an instantiation).
6714 elsif In_Instance_Body
6715 and then Ekind
(Operand_Type
) = E_Anonymous_Access_Type
6716 and then Nkind
(Operand
) = N_Selected_Component
6717 and then Object_Access_Level
(Operand
) >
6718 Type_Access_Level
(Target_Type
)
6721 Make_Raise_Program_Error
(Sloc
(N
),
6722 Reason
=> PE_Accessibility_Check_Failed
));
6723 Set_Etype
(N
, Target_Type
);
6727 -- Case of conversions of tagged types and access to tagged types
6729 -- When needed, that is to say when the expression is class-wide,
6730 -- Add runtime a tag check for (strict) downward conversion by using
6731 -- the membership test, generating:
6733 -- [constraint_error when Operand not in Target_Type'Class]
6735 -- or in the access type case
6737 -- [constraint_error
6738 -- when Operand /= null
6739 -- and then Operand.all not in
6740 -- Designated_Type (Target_Type)'Class]
6742 if (Is_Access_Type
(Target_Type
)
6743 and then Is_Tagged_Type
(Designated_Type
(Target_Type
)))
6744 or else Is_Tagged_Type
(Target_Type
)
6746 -- Do not do any expansion in the access type case if the
6747 -- parent is a renaming, since this is an error situation
6748 -- which will be caught by Sem_Ch8, and the expansion can
6749 -- intefere with this error check.
6751 if Is_Access_Type
(Target_Type
)
6752 and then Is_Renamed_Object
(N
)
6757 -- Oherwise, proceed with processing tagged conversion
6760 Actual_Operand_Type
: Entity_Id
;
6761 Actual_Target_Type
: Entity_Id
;
6766 if Is_Access_Type
(Target_Type
) then
6767 Actual_Operand_Type
:= Designated_Type
(Operand_Type
);
6768 Actual_Target_Type
:= Designated_Type
(Target_Type
);
6771 Actual_Operand_Type
:= Operand_Type
;
6772 Actual_Target_Type
:= Target_Type
;
6775 if Is_Class_Wide_Type
(Actual_Operand_Type
)
6776 and then Root_Type
(Actual_Operand_Type
) /= Actual_Target_Type
6777 and then Is_Ancestor
6778 (Root_Type
(Actual_Operand_Type
),
6780 and then not Tag_Checks_Suppressed
(Actual_Target_Type
)
6782 -- The conversion is valid for any descendant of the
6785 Actual_Target_Type
:= Class_Wide_Type
(Actual_Target_Type
);
6787 if Is_Access_Type
(Target_Type
) then
6792 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
6793 Right_Opnd
=> Make_Null
(Loc
)),
6798 Make_Explicit_Dereference
(Loc
,
6800 Duplicate_Subexpr_No_Checks
(Operand
)),
6802 New_Reference_To
(Actual_Target_Type
, Loc
)));
6807 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
6809 New_Reference_To
(Actual_Target_Type
, Loc
));
6813 Make_Raise_Constraint_Error
(Loc
,
6815 Reason
=> CE_Tag_Check_Failed
));
6821 Make_Unchecked_Type_Conversion
(Loc
,
6822 Subtype_Mark
=> New_Occurrence_Of
(Target_Type
, Loc
),
6823 Expression
=> Relocate_Node
(Expression
(N
)));
6825 Analyze_And_Resolve
(N
, Target_Type
);
6830 -- Case of other access type conversions
6832 elsif Is_Access_Type
(Target_Type
) then
6833 Apply_Constraint_Check
(Operand
, Target_Type
);
6835 -- Case of conversions from a fixed-point type
6837 -- These conversions require special expansion and processing, found
6838 -- in the Exp_Fixd package. We ignore cases where Conversion_OK is
6839 -- set, since from a semantic point of view, these are simple integer
6840 -- conversions, which do not need further processing.
6842 elsif Is_Fixed_Point_Type
(Operand_Type
)
6843 and then not Conversion_OK
(N
)
6845 -- We should never see universal fixed at this case, since the
6846 -- expansion of the constituent divide or multiply should have
6847 -- eliminated the explicit mention of universal fixed.
6849 pragma Assert
(Operand_Type
/= Universal_Fixed
);
6851 -- Check for special case of the conversion to universal real
6852 -- that occurs as a result of the use of a round attribute.
6853 -- In this case, the real type for the conversion is taken
6854 -- from the target type of the Round attribute and the
6855 -- result must be marked as rounded.
6857 if Target_Type
= Universal_Real
6858 and then Nkind
(Parent
(N
)) = N_Attribute_Reference
6859 and then Attribute_Name
(Parent
(N
)) = Name_Round
6861 Set_Rounded_Result
(N
);
6862 Set_Etype
(N
, Etype
(Parent
(N
)));
6865 -- Otherwise do correct fixed-conversion, but skip these if the
6866 -- Conversion_OK flag is set, because from a semantic point of
6867 -- view these are simple integer conversions needing no further
6868 -- processing (the backend will simply treat them as integers)
6870 if not Conversion_OK
(N
) then
6871 if Is_Fixed_Point_Type
(Etype
(N
)) then
6872 Expand_Convert_Fixed_To_Fixed
(N
);
6875 elsif Is_Integer_Type
(Etype
(N
)) then
6876 Expand_Convert_Fixed_To_Integer
(N
);
6879 pragma Assert
(Is_Floating_Point_Type
(Etype
(N
)));
6880 Expand_Convert_Fixed_To_Float
(N
);
6885 -- Case of conversions to a fixed-point type
6887 -- These conversions require special expansion and processing, found
6888 -- in the Exp_Fixd package. Again, ignore cases where Conversion_OK
6889 -- is set, since from a semantic point of view, these are simple
6890 -- integer conversions, which do not need further processing.
6892 elsif Is_Fixed_Point_Type
(Target_Type
)
6893 and then not Conversion_OK
(N
)
6895 if Is_Integer_Type
(Operand_Type
) then
6896 Expand_Convert_Integer_To_Fixed
(N
);
6899 pragma Assert
(Is_Floating_Point_Type
(Operand_Type
));
6900 Expand_Convert_Float_To_Fixed
(N
);
6904 -- Case of float-to-integer conversions
6906 -- We also handle float-to-fixed conversions with Conversion_OK set
6907 -- since semantically the fixed-point target is treated as though it
6908 -- were an integer in such cases.
6910 elsif Is_Floating_Point_Type
(Operand_Type
)
6912 (Is_Integer_Type
(Target_Type
)
6914 (Is_Fixed_Point_Type
(Target_Type
) and then Conversion_OK
(N
)))
6916 -- Special processing required if the conversion is the expression
6917 -- of a Truncation attribute reference. In this case we replace:
6919 -- ityp (ftyp'Truncation (x))
6925 -- with the Float_Truncate flag set. This is clearly more efficient
6927 if Nkind
(Operand
) = N_Attribute_Reference
6928 and then Attribute_Name
(Operand
) = Name_Truncation
6931 Relocate_Node
(First
(Expressions
(Operand
))));
6932 Set_Float_Truncate
(N
, True);
6935 -- One more check here, gcc is still not able to do conversions of
6936 -- this type with proper overflow checking, and so gigi is doing an
6937 -- approximation of what is required by doing floating-point compares
6938 -- with the end-point. But that can lose precision in some cases, and
6939 -- give a wrong result. Converting the operand to Universal_Real is
6940 -- helpful, but still does not catch all cases with 64-bit integers
6941 -- on targets with only 64-bit floats ???
6943 if Do_Range_Check
(Operand
) then
6945 Make_Type_Conversion
(Loc
,
6947 New_Occurrence_Of
(Universal_Real
, Loc
),
6949 Relocate_Node
(Operand
)));
6951 Set_Etype
(Operand
, Universal_Real
);
6952 Enable_Range_Check
(Operand
);
6953 Set_Do_Range_Check
(Expression
(Operand
), False);
6956 -- Case of array conversions
6958 -- Expansion of array conversions, add required length/range checks
6959 -- but only do this if there is no change of representation. For
6960 -- handling of this case, see Handle_Changed_Representation.
6962 elsif Is_Array_Type
(Target_Type
) then
6964 if Is_Constrained
(Target_Type
) then
6965 Apply_Length_Check
(Operand
, Target_Type
);
6967 Apply_Range_Check
(Operand
, Target_Type
);
6970 Handle_Changed_Representation
;
6972 -- Case of conversions of discriminated types
6974 -- Add required discriminant checks if target is constrained. Again
6975 -- this change is skipped if we have a change of representation.
6977 elsif Has_Discriminants
(Target_Type
)
6978 and then Is_Constrained
(Target_Type
)
6980 Apply_Discriminant_Check
(Operand
, Target_Type
);
6981 Handle_Changed_Representation
;
6983 -- Case of all other record conversions. The only processing required
6984 -- is to check for a change of representation requiring the special
6985 -- assignment processing.
6987 elsif Is_Record_Type
(Target_Type
) then
6989 -- Ada 2005 (AI-216): Program_Error is raised when converting from
6990 -- a derived Unchecked_Union type to an unconstrained non-Unchecked_
6991 -- Union type if the operand lacks inferable discriminants.
6993 if Is_Derived_Type
(Operand_Type
)
6994 and then Is_Unchecked_Union
(Base_Type
(Operand_Type
))
6995 and then not Is_Constrained
(Target_Type
)
6996 and then not Is_Unchecked_Union
(Base_Type
(Target_Type
))
6997 and then not Has_Inferable_Discriminants
(Operand
)
6999 -- To prevent Gigi from generating illegal code, we make a
7000 -- Program_Error node, but we give it the target type of the
7004 PE
: constant Node_Id
:= Make_Raise_Program_Error
(Loc
,
7005 Reason
=> PE_Unchecked_Union_Restriction
);
7008 Set_Etype
(PE
, Target_Type
);
7013 Handle_Changed_Representation
;
7016 -- Case of conversions of enumeration types
7018 elsif Is_Enumeration_Type
(Target_Type
) then
7020 -- Special processing is required if there is a change of
7021 -- representation (from enumeration representation clauses)
7023 if not Same_Representation
(Target_Type
, Operand_Type
) then
7025 -- Convert: x(y) to x'val (ytyp'val (y))
7028 Make_Attribute_Reference
(Loc
,
7029 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
7030 Attribute_Name
=> Name_Val
,
7031 Expressions
=> New_List
(
7032 Make_Attribute_Reference
(Loc
,
7033 Prefix
=> New_Occurrence_Of
(Operand_Type
, Loc
),
7034 Attribute_Name
=> Name_Pos
,
7035 Expressions
=> New_List
(Operand
)))));
7037 Analyze_And_Resolve
(N
, Target_Type
);
7040 -- Case of conversions to floating-point
7042 elsif Is_Floating_Point_Type
(Target_Type
) then
7046 -- At this stage, either the conversion node has been transformed
7047 -- into some other equivalent expression, or left as a conversion
7048 -- that can be handled by Gigi. The conversions that Gigi can handle
7049 -- are the following:
7051 -- Conversions with no change of representation or type
7053 -- Numeric conversions involving integer values, floating-point
7054 -- values, and fixed-point values. Fixed-point values are allowed
7055 -- only if Conversion_OK is set, i.e. if the fixed-point values
7056 -- are to be treated as integers.
7058 -- No other conversions should be passed to Gigi
7060 -- Check: are these rules stated in sinfo??? if so, why restate here???
7062 -- The only remaining step is to generate a range check if we still
7063 -- have a type conversion at this stage and Do_Range_Check is set.
7064 -- For now we do this only for conversions of discrete types.
7066 if Nkind
(N
) = N_Type_Conversion
7067 and then Is_Discrete_Type
(Etype
(N
))
7070 Expr
: constant Node_Id
:= Expression
(N
);
7075 if Do_Range_Check
(Expr
)
7076 and then Is_Discrete_Type
(Etype
(Expr
))
7078 Set_Do_Range_Check
(Expr
, False);
7080 -- Before we do a range check, we have to deal with treating
7081 -- a fixed-point operand as an integer. The way we do this
7082 -- is simply to do an unchecked conversion to an appropriate
7083 -- integer type large enough to hold the result.
7085 -- This code is not active yet, because we are only dealing
7086 -- with discrete types so far ???
7088 if Nkind
(Expr
) in N_Has_Treat_Fixed_As_Integer
7089 and then Treat_Fixed_As_Integer
(Expr
)
7091 Ftyp
:= Base_Type
(Etype
(Expr
));
7093 if Esize
(Ftyp
) >= Esize
(Standard_Integer
) then
7094 Ityp
:= Standard_Long_Long_Integer
;
7096 Ityp
:= Standard_Integer
;
7099 Rewrite
(Expr
, Unchecked_Convert_To
(Ityp
, Expr
));
7102 -- Reset overflow flag, since the range check will include
7103 -- dealing with possible overflow, and generate the check
7104 -- If Address is either source or target type, suppress
7105 -- range check to avoid typing anomalies when it is a visible
7108 Set_Do_Overflow_Check
(N
, False);
7109 if not Is_Descendent_Of_Address
(Etype
(Expr
))
7110 and then not Is_Descendent_Of_Address
(Target_Type
)
7112 Generate_Range_Check
7113 (Expr
, Target_Type
, CE_Range_Check_Failed
);
7119 -- Final step, if the result is a type conversion involving Vax_Float
7120 -- types, then it is subject for further special processing.
7122 if Nkind
(N
) = N_Type_Conversion
7123 and then (Vax_Float
(Operand_Type
) or else Vax_Float
(Target_Type
))
7125 Expand_Vax_Conversion
(N
);
7128 end Expand_N_Type_Conversion
;
7130 -----------------------------------
7131 -- Expand_N_Unchecked_Expression --
7132 -----------------------------------
7134 -- Remove the unchecked expression node from the tree. It's job was simply
7135 -- to make sure that its constituent expression was handled with checks
7136 -- off, and now that that is done, we can remove it from the tree, and
7137 -- indeed must, since gigi does not expect to see these nodes.
7139 procedure Expand_N_Unchecked_Expression
(N
: Node_Id
) is
7140 Exp
: constant Node_Id
:= Expression
(N
);
7143 Set_Assignment_OK
(Exp
, Assignment_OK
(N
) or Assignment_OK
(Exp
));
7145 end Expand_N_Unchecked_Expression
;
7147 ----------------------------------------
7148 -- Expand_N_Unchecked_Type_Conversion --
7149 ----------------------------------------
7151 -- If this cannot be handled by Gigi and we haven't already made
7152 -- a temporary for it, do it now.
7154 procedure Expand_N_Unchecked_Type_Conversion
(N
: Node_Id
) is
7155 Target_Type
: constant Entity_Id
:= Etype
(N
);
7156 Operand
: constant Node_Id
:= Expression
(N
);
7157 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
7160 -- If we have a conversion of a compile time known value to a target
7161 -- type and the value is in range of the target type, then we can simply
7162 -- replace the construct by an integer literal of the correct type. We
7163 -- only apply this to integer types being converted. Possibly it may
7164 -- apply in other cases, but it is too much trouble to worry about.
7166 -- Note that we do not do this transformation if the Kill_Range_Check
7167 -- flag is set, since then the value may be outside the expected range.
7168 -- This happens in the Normalize_Scalars case.
7170 if Is_Integer_Type
(Target_Type
)
7171 and then Is_Integer_Type
(Operand_Type
)
7172 and then Compile_Time_Known_Value
(Operand
)
7173 and then not Kill_Range_Check
(N
)
7176 Val
: constant Uint
:= Expr_Value
(Operand
);
7179 if Compile_Time_Known_Value
(Type_Low_Bound
(Target_Type
))
7181 Compile_Time_Known_Value
(Type_High_Bound
(Target_Type
))
7183 Val
>= Expr_Value
(Type_Low_Bound
(Target_Type
))
7185 Val
<= Expr_Value
(Type_High_Bound
(Target_Type
))
7187 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Val
));
7189 -- If Address is the target type, just set the type
7190 -- to avoid a spurious type error on the literal when
7191 -- Address is a visible integer type.
7193 if Is_Descendent_Of_Address
(Target_Type
) then
7194 Set_Etype
(N
, Target_Type
);
7196 Analyze_And_Resolve
(N
, Target_Type
);
7204 -- Nothing to do if conversion is safe
7206 if Safe_Unchecked_Type_Conversion
(N
) then
7210 -- Otherwise force evaluation unless Assignment_OK flag is set (this
7211 -- flag indicates ??? -- more comments needed here)
7213 if Assignment_OK
(N
) then
7216 Force_Evaluation
(N
);
7218 end Expand_N_Unchecked_Type_Conversion
;
7220 ----------------------------
7221 -- Expand_Record_Equality --
7222 ----------------------------
7224 -- For non-variant records, Equality is expanded when needed into:
7226 -- and then Lhs.Discr1 = Rhs.Discr1
7228 -- and then Lhs.Discrn = Rhs.Discrn
7229 -- and then Lhs.Cmp1 = Rhs.Cmp1
7231 -- and then Lhs.Cmpn = Rhs.Cmpn
7233 -- The expression is folded by the back-end for adjacent fields. This
7234 -- function is called for tagged record in only one occasion: for imple-
7235 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
7236 -- otherwise the primitive "=" is used directly.
7238 function Expand_Record_Equality
7243 Bodies
: List_Id
) return Node_Id
7245 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
7250 First_Time
: Boolean := True;
7252 function Suitable_Element
(C
: Entity_Id
) return Entity_Id
;
7253 -- Return the first field to compare beginning with C, skipping the
7254 -- inherited components.
7256 ----------------------
7257 -- Suitable_Element --
7258 ----------------------
7260 function Suitable_Element
(C
: Entity_Id
) return Entity_Id
is
7265 elsif Ekind
(C
) /= E_Discriminant
7266 and then Ekind
(C
) /= E_Component
7268 return Suitable_Element
(Next_Entity
(C
));
7270 elsif Is_Tagged_Type
(Typ
)
7271 and then C
/= Original_Record_Component
(C
)
7273 return Suitable_Element
(Next_Entity
(C
));
7275 elsif Chars
(C
) = Name_uController
7276 or else Chars
(C
) = Name_uTag
7278 return Suitable_Element
(Next_Entity
(C
));
7283 end Suitable_Element
;
7285 -- Start of processing for Expand_Record_Equality
7288 -- Generates the following code: (assuming that Typ has one Discr and
7289 -- component C2 is also a record)
7292 -- and then Lhs.Discr1 = Rhs.Discr1
7293 -- and then Lhs.C1 = Rhs.C1
7294 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
7296 -- and then Lhs.Cmpn = Rhs.Cmpn
7298 Result
:= New_Reference_To
(Standard_True
, Loc
);
7299 C
:= Suitable_Element
(First_Entity
(Typ
));
7301 while Present
(C
) loop
7309 First_Time
:= False;
7313 New_Lhs
:= New_Copy_Tree
(Lhs
);
7314 New_Rhs
:= New_Copy_Tree
(Rhs
);
7318 Expand_Composite_Equality
(Nod
, Etype
(C
),
7320 Make_Selected_Component
(Loc
,
7322 Selector_Name
=> New_Reference_To
(C
, Loc
)),
7324 Make_Selected_Component
(Loc
,
7326 Selector_Name
=> New_Reference_To
(C
, Loc
)),
7329 -- If some (sub)component is an unchecked_union, the whole
7330 -- operation will raise program error.
7332 if Nkind
(Check
) = N_Raise_Program_Error
then
7334 Set_Etype
(Result
, Standard_Boolean
);
7339 Left_Opnd
=> Result
,
7340 Right_Opnd
=> Check
);
7344 C
:= Suitable_Element
(Next_Entity
(C
));
7348 end Expand_Record_Equality
;
7350 -------------------------------------
7351 -- Fixup_Universal_Fixed_Operation --
7352 -------------------------------------
7354 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
) is
7355 Conv
: constant Node_Id
:= Parent
(N
);
7358 -- We must have a type conversion immediately above us
7360 pragma Assert
(Nkind
(Conv
) = N_Type_Conversion
);
7362 -- Normally the type conversion gives our target type. The exception
7363 -- occurs in the case of the Round attribute, where the conversion
7364 -- will be to universal real, and our real type comes from the Round
7365 -- attribute (as well as an indication that we must round the result)
7367 if Nkind
(Parent
(Conv
)) = N_Attribute_Reference
7368 and then Attribute_Name
(Parent
(Conv
)) = Name_Round
7370 Set_Etype
(N
, Etype
(Parent
(Conv
)));
7371 Set_Rounded_Result
(N
);
7373 -- Normal case where type comes from conversion above us
7376 Set_Etype
(N
, Etype
(Conv
));
7378 end Fixup_Universal_Fixed_Operation
;
7380 ------------------------------
7381 -- Get_Allocator_Final_List --
7382 ------------------------------
7384 function Get_Allocator_Final_List
7387 PtrT
: Entity_Id
) return Entity_Id
7389 Loc
: constant Source_Ptr
:= Sloc
(N
);
7391 Owner
: Entity_Id
:= PtrT
;
7392 -- The entity whose finalisation list must be used to attach the
7393 -- allocated object.
7396 if Ekind
(PtrT
) = E_Anonymous_Access_Type
then
7397 if Nkind
(Associated_Node_For_Itype
(PtrT
))
7398 in N_Subprogram_Specification
7400 -- If the context is an access parameter, we need to create
7401 -- a non-anonymous access type in order to have a usable
7402 -- final list, because there is otherwise no pool to which
7403 -- the allocated object can belong. We create both the type
7404 -- and the finalization chain here, because freezing an
7405 -- internal type does not create such a chain. The Final_Chain
7406 -- that is thus created is shared by the access parameter.
7408 Owner
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('J'));
7410 Make_Full_Type_Declaration
(Loc
,
7411 Defining_Identifier
=> Owner
,
7413 Make_Access_To_Object_Definition
(Loc
,
7414 Subtype_Indication
=>
7415 New_Occurrence_Of
(T
, Loc
))));
7417 Build_Final_List
(N
, Owner
);
7418 Set_Associated_Final_Chain
(PtrT
, Associated_Final_Chain
(Owner
));
7421 -- Case of an access discriminant, or (Ada 2005) of
7422 -- an anonymous access component: find the final list
7423 -- associated with the scope of the type.
7425 Owner
:= Scope
(PtrT
);
7429 return Find_Final_List
(Owner
);
7430 end Get_Allocator_Final_List
;
7432 ---------------------------------
7433 -- Has_Inferable_Discriminants --
7434 ---------------------------------
7436 function Has_Inferable_Discriminants
(N
: Node_Id
) return Boolean is
7438 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean;
7439 -- Determines whether the left-most prefix of a selected component is a
7440 -- formal parameter in a subprogram. Assumes N is a selected component.
7442 --------------------------------
7443 -- Prefix_Is_Formal_Parameter --
7444 --------------------------------
7446 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean is
7447 Sel_Comp
: Node_Id
:= N
;
7450 -- Move to the left-most prefix by climbing up the tree
7452 while Present
(Parent
(Sel_Comp
))
7453 and then Nkind
(Parent
(Sel_Comp
)) = N_Selected_Component
7455 Sel_Comp
:= Parent
(Sel_Comp
);
7458 return Ekind
(Entity
(Prefix
(Sel_Comp
))) in Formal_Kind
;
7459 end Prefix_Is_Formal_Parameter
;
7461 -- Start of processing for Has_Inferable_Discriminants
7464 -- For identifiers and indexed components, it is sufficent to have a
7465 -- constrained Unchecked_Union nominal subtype.
7467 if Nkind
(N
) = N_Identifier
7469 Nkind
(N
) = N_Indexed_Component
7471 return Is_Unchecked_Union
(Base_Type
(Etype
(N
)))
7473 Is_Constrained
(Etype
(N
));
7475 -- For selected components, the subtype of the selector must be a
7476 -- constrained Unchecked_Union. If the component is subject to a
7477 -- per-object constraint, then the enclosing object must have inferable
7480 elsif Nkind
(N
) = N_Selected_Component
then
7481 if Has_Per_Object_Constraint
(Entity
(Selector_Name
(N
))) then
7483 -- A small hack. If we have a per-object constrained selected
7484 -- component of a formal parameter, return True since we do not
7485 -- know the actual parameter association yet.
7487 if Prefix_Is_Formal_Parameter
(N
) then
7491 -- Otherwise, check the enclosing object and the selector
7493 return Has_Inferable_Discriminants
(Prefix
(N
))
7495 Has_Inferable_Discriminants
(Selector_Name
(N
));
7498 -- The call to Has_Inferable_Discriminants will determine whether
7499 -- the selector has a constrained Unchecked_Union nominal type.
7501 return Has_Inferable_Discriminants
(Selector_Name
(N
));
7503 -- A qualified expression has inferable discriminants if its subtype
7504 -- mark is a constrained Unchecked_Union subtype.
7506 elsif Nkind
(N
) = N_Qualified_Expression
then
7507 return Is_Unchecked_Union
(Subtype_Mark
(N
))
7509 Is_Constrained
(Subtype_Mark
(N
));
7514 end Has_Inferable_Discriminants
;
7516 -------------------------------
7517 -- Insert_Dereference_Action --
7518 -------------------------------
7520 procedure Insert_Dereference_Action
(N
: Node_Id
) is
7521 Loc
: constant Source_Ptr
:= Sloc
(N
);
7522 Typ
: constant Entity_Id
:= Etype
(N
);
7523 Pool
: constant Entity_Id
:= Associated_Storage_Pool
(Typ
);
7524 Pnod
: constant Node_Id
:= Parent
(N
);
7526 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean;
7527 -- Return true if type of P is derived from Checked_Pool;
7529 -----------------------------
7530 -- Is_Checked_Storage_Pool --
7531 -----------------------------
7533 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean is
7542 while T
/= Etype
(T
) loop
7543 if Is_RTE
(T
, RE_Checked_Pool
) then
7551 end Is_Checked_Storage_Pool
;
7553 -- Start of processing for Insert_Dereference_Action
7556 pragma Assert
(Nkind
(Pnod
) = N_Explicit_Dereference
);
7558 if not (Is_Checked_Storage_Pool
(Pool
)
7559 and then Comes_From_Source
(Original_Node
(Pnod
)))
7565 Make_Procedure_Call_Statement
(Loc
,
7566 Name
=> New_Reference_To
(
7567 Find_Prim_Op
(Etype
(Pool
), Name_Dereference
), Loc
),
7569 Parameter_Associations
=> New_List
(
7573 New_Reference_To
(Pool
, Loc
),
7575 -- Storage_Address. We use the attribute Pool_Address,
7576 -- which uses the pointer itself to find the address of
7577 -- the object, and which handles unconstrained arrays
7578 -- properly by computing the address of the template.
7579 -- i.e. the correct address of the corresponding allocation.
7581 Make_Attribute_Reference
(Loc
,
7582 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
),
7583 Attribute_Name
=> Name_Pool_Address
),
7585 -- Size_In_Storage_Elements
7587 Make_Op_Divide
(Loc
,
7589 Make_Attribute_Reference
(Loc
,
7591 Make_Explicit_Dereference
(Loc
,
7592 Duplicate_Subexpr_Move_Checks
(N
)),
7593 Attribute_Name
=> Name_Size
),
7595 Make_Integer_Literal
(Loc
, System_Storage_Unit
)),
7599 Make_Attribute_Reference
(Loc
,
7601 Make_Explicit_Dereference
(Loc
,
7602 Duplicate_Subexpr_Move_Checks
(N
)),
7603 Attribute_Name
=> Name_Alignment
))));
7606 when RE_Not_Available
=>
7608 end Insert_Dereference_Action
;
7610 ------------------------------
7611 -- Make_Array_Comparison_Op --
7612 ------------------------------
7614 -- This is a hand-coded expansion of the following generic function:
7617 -- type elem is (<>);
7618 -- type index is (<>);
7619 -- type a is array (index range <>) of elem;
7621 -- function Gnnn (X : a; Y: a) return boolean is
7622 -- J : index := Y'first;
7625 -- if X'length = 0 then
7628 -- elsif Y'length = 0 then
7632 -- for I in X'range loop
7633 -- if X (I) = Y (J) then
7634 -- if J = Y'last then
7637 -- J := index'succ (J);
7641 -- return X (I) > Y (J);
7645 -- return X'length > Y'length;
7649 -- Note that since we are essentially doing this expansion by hand, we
7650 -- do not need to generate an actual or formal generic part, just the
7651 -- instantiated function itself.
7653 function Make_Array_Comparison_Op
7655 Nod
: Node_Id
) return Node_Id
7657 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
7659 X
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uX
);
7660 Y
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uY
);
7661 I
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uI
);
7662 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
7664 Index
: constant Entity_Id
:= Base_Type
(Etype
(First_Index
(Typ
)));
7666 Loop_Statement
: Node_Id
;
7667 Loop_Body
: Node_Id
;
7670 Final_Expr
: Node_Id
;
7671 Func_Body
: Node_Id
;
7672 Func_Name
: Entity_Id
;
7678 -- if J = Y'last then
7681 -- J := index'succ (J);
7685 Make_Implicit_If_Statement
(Nod
,
7688 Left_Opnd
=> New_Reference_To
(J
, Loc
),
7690 Make_Attribute_Reference
(Loc
,
7691 Prefix
=> New_Reference_To
(Y
, Loc
),
7692 Attribute_Name
=> Name_Last
)),
7694 Then_Statements
=> New_List
(
7695 Make_Exit_Statement
(Loc
)),
7699 Make_Assignment_Statement
(Loc
,
7700 Name
=> New_Reference_To
(J
, Loc
),
7702 Make_Attribute_Reference
(Loc
,
7703 Prefix
=> New_Reference_To
(Index
, Loc
),
7704 Attribute_Name
=> Name_Succ
,
7705 Expressions
=> New_List
(New_Reference_To
(J
, Loc
))))));
7707 -- if X (I) = Y (J) then
7710 -- return X (I) > Y (J);
7714 Make_Implicit_If_Statement
(Nod
,
7718 Make_Indexed_Component
(Loc
,
7719 Prefix
=> New_Reference_To
(X
, Loc
),
7720 Expressions
=> New_List
(New_Reference_To
(I
, Loc
))),
7723 Make_Indexed_Component
(Loc
,
7724 Prefix
=> New_Reference_To
(Y
, Loc
),
7725 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)))),
7727 Then_Statements
=> New_List
(Inner_If
),
7729 Else_Statements
=> New_List
(
7730 Make_Return_Statement
(Loc
,
7734 Make_Indexed_Component
(Loc
,
7735 Prefix
=> New_Reference_To
(X
, Loc
),
7736 Expressions
=> New_List
(New_Reference_To
(I
, Loc
))),
7739 Make_Indexed_Component
(Loc
,
7740 Prefix
=> New_Reference_To
(Y
, Loc
),
7741 Expressions
=> New_List
(
7742 New_Reference_To
(J
, Loc
)))))));
7744 -- for I in X'range loop
7749 Make_Implicit_Loop_Statement
(Nod
,
7750 Identifier
=> Empty
,
7753 Make_Iteration_Scheme
(Loc
,
7754 Loop_Parameter_Specification
=>
7755 Make_Loop_Parameter_Specification
(Loc
,
7756 Defining_Identifier
=> I
,
7757 Discrete_Subtype_Definition
=>
7758 Make_Attribute_Reference
(Loc
,
7759 Prefix
=> New_Reference_To
(X
, Loc
),
7760 Attribute_Name
=> Name_Range
))),
7762 Statements
=> New_List
(Loop_Body
));
7764 -- if X'length = 0 then
7766 -- elsif Y'length = 0 then
7769 -- for ... loop ... end loop;
7770 -- return X'length > Y'length;
7774 Make_Attribute_Reference
(Loc
,
7775 Prefix
=> New_Reference_To
(X
, Loc
),
7776 Attribute_Name
=> Name_Length
);
7779 Make_Attribute_Reference
(Loc
,
7780 Prefix
=> New_Reference_To
(Y
, Loc
),
7781 Attribute_Name
=> Name_Length
);
7785 Left_Opnd
=> Length1
,
7786 Right_Opnd
=> Length2
);
7789 Make_Implicit_If_Statement
(Nod
,
7793 Make_Attribute_Reference
(Loc
,
7794 Prefix
=> New_Reference_To
(X
, Loc
),
7795 Attribute_Name
=> Name_Length
),
7797 Make_Integer_Literal
(Loc
, 0)),
7801 Make_Return_Statement
(Loc
,
7802 Expression
=> New_Reference_To
(Standard_False
, Loc
))),
7804 Elsif_Parts
=> New_List
(
7805 Make_Elsif_Part
(Loc
,
7809 Make_Attribute_Reference
(Loc
,
7810 Prefix
=> New_Reference_To
(Y
, Loc
),
7811 Attribute_Name
=> Name_Length
),
7813 Make_Integer_Literal
(Loc
, 0)),
7817 Make_Return_Statement
(Loc
,
7818 Expression
=> New_Reference_To
(Standard_True
, Loc
))))),
7820 Else_Statements
=> New_List
(
7822 Make_Return_Statement
(Loc
,
7823 Expression
=> Final_Expr
)));
7827 Formals
:= New_List
(
7828 Make_Parameter_Specification
(Loc
,
7829 Defining_Identifier
=> X
,
7830 Parameter_Type
=> New_Reference_To
(Typ
, Loc
)),
7832 Make_Parameter_Specification
(Loc
,
7833 Defining_Identifier
=> Y
,
7834 Parameter_Type
=> New_Reference_To
(Typ
, Loc
)));
7836 -- function Gnnn (...) return boolean is
7837 -- J : index := Y'first;
7842 Func_Name
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('G'));
7845 Make_Subprogram_Body
(Loc
,
7847 Make_Function_Specification
(Loc
,
7848 Defining_Unit_Name
=> Func_Name
,
7849 Parameter_Specifications
=> Formals
,
7850 Result_Definition
=> New_Reference_To
(Standard_Boolean
, Loc
)),
7852 Declarations
=> New_List
(
7853 Make_Object_Declaration
(Loc
,
7854 Defining_Identifier
=> J
,
7855 Object_Definition
=> New_Reference_To
(Index
, Loc
),
7857 Make_Attribute_Reference
(Loc
,
7858 Prefix
=> New_Reference_To
(Y
, Loc
),
7859 Attribute_Name
=> Name_First
))),
7861 Handled_Statement_Sequence
=>
7862 Make_Handled_Sequence_Of_Statements
(Loc
,
7863 Statements
=> New_List
(If_Stat
)));
7866 end Make_Array_Comparison_Op
;
7868 ---------------------------
7869 -- Make_Boolean_Array_Op --
7870 ---------------------------
7872 -- For logical operations on boolean arrays, expand in line the
7873 -- following, replacing 'and' with 'or' or 'xor' where needed:
7875 -- function Annn (A : typ; B: typ) return typ is
7878 -- for J in A'range loop
7879 -- C (J) := A (J) op B (J);
7884 -- Here typ is the boolean array type
7886 function Make_Boolean_Array_Op
7888 N
: Node_Id
) return Node_Id
7890 Loc
: constant Source_Ptr
:= Sloc
(N
);
7892 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
7893 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
7894 C
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uC
);
7895 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
7903 Func_Name
: Entity_Id
;
7904 Func_Body
: Node_Id
;
7905 Loop_Statement
: Node_Id
;
7909 Make_Indexed_Component
(Loc
,
7910 Prefix
=> New_Reference_To
(A
, Loc
),
7911 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
7914 Make_Indexed_Component
(Loc
,
7915 Prefix
=> New_Reference_To
(B
, Loc
),
7916 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
7919 Make_Indexed_Component
(Loc
,
7920 Prefix
=> New_Reference_To
(C
, Loc
),
7921 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
7923 if Nkind
(N
) = N_Op_And
then
7929 elsif Nkind
(N
) = N_Op_Or
then
7943 Make_Implicit_Loop_Statement
(N
,
7944 Identifier
=> Empty
,
7947 Make_Iteration_Scheme
(Loc
,
7948 Loop_Parameter_Specification
=>
7949 Make_Loop_Parameter_Specification
(Loc
,
7950 Defining_Identifier
=> J
,
7951 Discrete_Subtype_Definition
=>
7952 Make_Attribute_Reference
(Loc
,
7953 Prefix
=> New_Reference_To
(A
, Loc
),
7954 Attribute_Name
=> Name_Range
))),
7956 Statements
=> New_List
(
7957 Make_Assignment_Statement
(Loc
,
7959 Expression
=> Op
)));
7961 Formals
:= New_List
(
7962 Make_Parameter_Specification
(Loc
,
7963 Defining_Identifier
=> A
,
7964 Parameter_Type
=> New_Reference_To
(Typ
, Loc
)),
7966 Make_Parameter_Specification
(Loc
,
7967 Defining_Identifier
=> B
,
7968 Parameter_Type
=> New_Reference_To
(Typ
, Loc
)));
7971 Make_Defining_Identifier
(Loc
, New_Internal_Name
('A'));
7972 Set_Is_Inlined
(Func_Name
);
7975 Make_Subprogram_Body
(Loc
,
7977 Make_Function_Specification
(Loc
,
7978 Defining_Unit_Name
=> Func_Name
,
7979 Parameter_Specifications
=> Formals
,
7980 Result_Definition
=> New_Reference_To
(Typ
, Loc
)),
7982 Declarations
=> New_List
(
7983 Make_Object_Declaration
(Loc
,
7984 Defining_Identifier
=> C
,
7985 Object_Definition
=> New_Reference_To
(Typ
, Loc
))),
7987 Handled_Statement_Sequence
=>
7988 Make_Handled_Sequence_Of_Statements
(Loc
,
7989 Statements
=> New_List
(
7991 Make_Return_Statement
(Loc
,
7992 Expression
=> New_Reference_To
(C
, Loc
)))));
7995 end Make_Boolean_Array_Op
;
7997 ------------------------
7998 -- Rewrite_Comparison --
7999 ------------------------
8001 procedure Rewrite_Comparison
(N
: Node_Id
) is
8003 if Nkind
(N
) = N_Type_Conversion
then
8004 Rewrite_Comparison
(Expression
(N
));
8006 elsif Nkind
(N
) not in N_Op_Compare
then
8011 Typ
: constant Entity_Id
:= Etype
(N
);
8012 Op1
: constant Node_Id
:= Left_Opnd
(N
);
8013 Op2
: constant Node_Id
:= Right_Opnd
(N
);
8015 Res
: constant Compare_Result
:= Compile_Time_Compare
(Op1
, Op2
);
8016 -- Res indicates if compare outcome can be compile time determined
8018 True_Result
: Boolean;
8019 False_Result
: Boolean;
8022 case N_Op_Compare
(Nkind
(N
)) is
8024 True_Result
:= Res
= EQ
;
8025 False_Result
:= Res
= LT
or else Res
= GT
or else Res
= NE
;
8028 True_Result
:= Res
in Compare_GE
;
8029 False_Result
:= Res
= LT
;
8032 and then Constant_Condition_Warnings
8033 and then Comes_From_Source
(Original_Node
(N
))
8034 and then Nkind
(Original_Node
(N
)) = N_Op_Ge
8035 and then not In_Instance
8036 and then not Warnings_Off
(Etype
(Left_Opnd
(N
)))
8037 and then Is_Integer_Type
(Etype
(Left_Opnd
(N
)))
8040 ("can never be greater than, could replace by ""'=""?", N
);
8044 True_Result
:= Res
= GT
;
8045 False_Result
:= Res
in Compare_LE
;
8048 True_Result
:= Res
= LT
;
8049 False_Result
:= Res
in Compare_GE
;
8052 True_Result
:= Res
in Compare_LE
;
8053 False_Result
:= Res
= GT
;
8056 and then Constant_Condition_Warnings
8057 and then Comes_From_Source
(Original_Node
(N
))
8058 and then Nkind
(Original_Node
(N
)) = N_Op_Le
8059 and then not In_Instance
8060 and then not Warnings_Off
(Etype
(Left_Opnd
(N
)))
8061 and then Is_Integer_Type
(Etype
(Left_Opnd
(N
)))
8064 ("can never be less than, could replace by ""'=""?", N
);
8068 True_Result
:= Res
= NE
or else Res
= GT
or else Res
= LT
;
8069 False_Result
:= Res
= EQ
;
8075 New_Occurrence_Of
(Standard_True
, Sloc
(N
))));
8076 Analyze_And_Resolve
(N
, Typ
);
8077 Warn_On_Known_Condition
(N
);
8079 elsif False_Result
then
8082 New_Occurrence_Of
(Standard_False
, Sloc
(N
))));
8083 Analyze_And_Resolve
(N
, Typ
);
8084 Warn_On_Known_Condition
(N
);
8088 end Rewrite_Comparison
;
8090 ----------------------------
8091 -- Safe_In_Place_Array_Op --
8092 ----------------------------
8094 function Safe_In_Place_Array_Op
8097 Op2
: Node_Id
) return Boolean
8101 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean;
8102 -- Operand is safe if it cannot overlap part of the target of the
8103 -- operation. If the operand and the target are identical, the operand
8104 -- is safe. The operand can be empty in the case of negation.
8106 function Is_Unaliased
(N
: Node_Id
) return Boolean;
8107 -- Check that N is a stand-alone entity
8113 function Is_Unaliased
(N
: Node_Id
) return Boolean is
8117 and then No
(Address_Clause
(Entity
(N
)))
8118 and then No
(Renamed_Object
(Entity
(N
)));
8121 ---------------------
8122 -- Is_Safe_Operand --
8123 ---------------------
8125 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean is
8130 elsif Is_Entity_Name
(Op
) then
8131 return Is_Unaliased
(Op
);
8133 elsif Nkind
(Op
) = N_Indexed_Component
8134 or else Nkind
(Op
) = N_Selected_Component
8136 return Is_Unaliased
(Prefix
(Op
));
8138 elsif Nkind
(Op
) = N_Slice
then
8140 Is_Unaliased
(Prefix
(Op
))
8141 and then Entity
(Prefix
(Op
)) /= Target
;
8143 elsif Nkind
(Op
) = N_Op_Not
then
8144 return Is_Safe_Operand
(Right_Opnd
(Op
));
8149 end Is_Safe_Operand
;
8151 -- Start of processing for Is_Safe_In_Place_Array_Op
8154 -- We skip this processing if the component size is not the
8155 -- same as a system storage unit (since at least for NOT
8156 -- this would cause problems).
8158 if Component_Size
(Etype
(Lhs
)) /= System_Storage_Unit
then
8161 -- Cannot do in place stuff on Java_VM since cannot pass addresses
8166 -- Cannot do in place stuff if non-standard Boolean representation
8168 elsif Has_Non_Standard_Rep
(Component_Type
(Etype
(Lhs
))) then
8171 elsif not Is_Unaliased
(Lhs
) then
8174 Target
:= Entity
(Lhs
);
8177 Is_Safe_Operand
(Op1
)
8178 and then Is_Safe_Operand
(Op2
);
8180 end Safe_In_Place_Array_Op
;
8182 -----------------------
8183 -- Tagged_Membership --
8184 -----------------------
8186 -- There are two different cases to consider depending on whether
8187 -- the right operand is a class-wide type or not. If not we just
8188 -- compare the actual tag of the left expr to the target type tag:
8190 -- Left_Expr.Tag = Right_Type'Tag;
8192 -- If it is a class-wide type we use the RT function CW_Membership which
8193 -- is usually implemented by looking in the ancestor tables contained in
8194 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
8196 function Tagged_Membership
(N
: Node_Id
) return Node_Id
is
8197 Left
: constant Node_Id
:= Left_Opnd
(N
);
8198 Right
: constant Node_Id
:= Right_Opnd
(N
);
8199 Loc
: constant Source_Ptr
:= Sloc
(N
);
8201 Left_Type
: Entity_Id
;
8202 Right_Type
: Entity_Id
;
8206 Left_Type
:= Etype
(Left
);
8207 Right_Type
:= Etype
(Right
);
8209 if Is_Class_Wide_Type
(Left_Type
) then
8210 Left_Type
:= Root_Type
(Left_Type
);
8214 Make_Selected_Component
(Loc
,
8215 Prefix
=> Relocate_Node
(Left
),
8217 New_Reference_To
(First_Tag_Component
(Left_Type
), Loc
));
8219 if Is_Class_Wide_Type
(Right_Type
) then
8221 -- Ada 2005 (AI-251): Class-wide applied to interfaces
8223 if Is_Interface
(Etype
(Class_Wide_Type
(Right_Type
)))
8225 -- Give support to: "Iface_CW_Typ in Typ'Class"
8227 or else Is_Interface
(Left_Type
)
8229 -- Issue error if IW_Membership operation not available in a
8230 -- configurable run time setting.
8232 if not RTE_Available
(RE_IW_Membership
) then
8233 Error_Msg_CRT
("abstract interface types", N
);
8238 Make_Function_Call
(Loc
,
8239 Name
=> New_Occurrence_Of
(RTE
(RE_IW_Membership
), Loc
),
8240 Parameter_Associations
=> New_List
(
8241 Make_Attribute_Reference
(Loc
,
8243 Attribute_Name
=> Name_Address
),
8246 (Access_Disp_Table
(Root_Type
(Right_Type
)))),
8249 -- Ada 95: Normal case
8253 Make_Function_Call
(Loc
,
8254 Name
=> New_Occurrence_Of
(RTE
(RE_CW_Membership
), Loc
),
8255 Parameter_Associations
=> New_List
(
8259 (Access_Disp_Table
(Root_Type
(Right_Type
)))),
8266 Left_Opnd
=> Obj_Tag
,
8269 (Node
(First_Elmt
(Access_Disp_Table
(Right_Type
))), Loc
));
8271 end Tagged_Membership
;
8273 ------------------------------
8274 -- Unary_Op_Validity_Checks --
8275 ------------------------------
8277 procedure Unary_Op_Validity_Checks
(N
: Node_Id
) is
8279 if Validity_Checks_On
and Validity_Check_Operands
then
8280 Ensure_Valid
(Right_Opnd
(N
));
8282 end Unary_Op_Validity_Checks
;