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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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_Disp
; use Exp_Disp
;
37 with Exp_Fixd
; use Exp_Fixd
;
38 with Exp_Pakd
; use Exp_Pakd
;
39 with Exp_Tss
; use Exp_Tss
;
40 with Exp_Util
; use Exp_Util
;
41 with Exp_VFpt
; use Exp_VFpt
;
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 -- N is the node for a compile time comparison. If this outcome of this
196 -- comparison can be determined at compile time, then the node N can be
197 -- rewritten with True or False. If the outcome cannot be determined at
198 -- compile time, the call has no effect.
200 function Tagged_Membership
(N
: Node_Id
) return Node_Id
;
201 -- Construct the expression corresponding to the tagged membership test.
202 -- Deals with a second operand being (or not) a class-wide type.
204 function Safe_In_Place_Array_Op
207 Op2
: Node_Id
) return Boolean;
208 -- In the context of an assignment, where the right-hand side is a
209 -- boolean operation on arrays, check whether operation can be performed
212 procedure Unary_Op_Validity_Checks
(N
: Node_Id
);
213 pragma Inline
(Unary_Op_Validity_Checks
);
214 -- Performs validity checks for a unary operator
216 -------------------------------
217 -- Binary_Op_Validity_Checks --
218 -------------------------------
220 procedure Binary_Op_Validity_Checks
(N
: Node_Id
) is
222 if Validity_Checks_On
and Validity_Check_Operands
then
223 Ensure_Valid
(Left_Opnd
(N
));
224 Ensure_Valid
(Right_Opnd
(N
));
226 end Binary_Op_Validity_Checks
;
228 ------------------------------------
229 -- Build_Boolean_Array_Proc_Call --
230 ------------------------------------
232 procedure Build_Boolean_Array_Proc_Call
237 Loc
: constant Source_Ptr
:= Sloc
(N
);
238 Kind
: constant Node_Kind
:= Nkind
(Expression
(N
));
239 Target
: constant Node_Id
:=
240 Make_Attribute_Reference
(Loc
,
242 Attribute_Name
=> Name_Address
);
244 Arg1
: constant Node_Id
:= Op1
;
245 Arg2
: Node_Id
:= Op2
;
247 Proc_Name
: Entity_Id
;
250 if Kind
= N_Op_Not
then
251 if Nkind
(Op1
) in N_Binary_Op
then
253 -- Use negated version of the binary operators
255 if Nkind
(Op1
) = N_Op_And
then
256 Proc_Name
:= RTE
(RE_Vector_Nand
);
258 elsif Nkind
(Op1
) = N_Op_Or
then
259 Proc_Name
:= RTE
(RE_Vector_Nor
);
261 else pragma Assert
(Nkind
(Op1
) = N_Op_Xor
);
262 Proc_Name
:= RTE
(RE_Vector_Xor
);
266 Make_Procedure_Call_Statement
(Loc
,
267 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
269 Parameter_Associations
=> New_List
(
271 Make_Attribute_Reference
(Loc
,
272 Prefix
=> Left_Opnd
(Op1
),
273 Attribute_Name
=> Name_Address
),
275 Make_Attribute_Reference
(Loc
,
276 Prefix
=> Right_Opnd
(Op1
),
277 Attribute_Name
=> Name_Address
),
279 Make_Attribute_Reference
(Loc
,
280 Prefix
=> Left_Opnd
(Op1
),
281 Attribute_Name
=> Name_Length
)));
284 Proc_Name
:= RTE
(RE_Vector_Not
);
287 Make_Procedure_Call_Statement
(Loc
,
288 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
289 Parameter_Associations
=> New_List
(
292 Make_Attribute_Reference
(Loc
,
294 Attribute_Name
=> Name_Address
),
296 Make_Attribute_Reference
(Loc
,
298 Attribute_Name
=> Name_Length
)));
302 -- We use the following equivalences:
304 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
305 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
306 -- (not X) xor (not Y) = X xor Y
307 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
309 if Nkind
(Op1
) = N_Op_Not
then
310 if Kind
= N_Op_And
then
311 Proc_Name
:= RTE
(RE_Vector_Nor
);
313 elsif Kind
= N_Op_Or
then
314 Proc_Name
:= RTE
(RE_Vector_Nand
);
317 Proc_Name
:= RTE
(RE_Vector_Xor
);
321 if Kind
= N_Op_And
then
322 Proc_Name
:= RTE
(RE_Vector_And
);
324 elsif Kind
= N_Op_Or
then
325 Proc_Name
:= RTE
(RE_Vector_Or
);
327 elsif Nkind
(Op2
) = N_Op_Not
then
328 Proc_Name
:= RTE
(RE_Vector_Nxor
);
329 Arg2
:= Right_Opnd
(Op2
);
332 Proc_Name
:= RTE
(RE_Vector_Xor
);
337 Make_Procedure_Call_Statement
(Loc
,
338 Name
=> New_Occurrence_Of
(Proc_Name
, Loc
),
339 Parameter_Associations
=> New_List
(
341 Make_Attribute_Reference
(Loc
,
343 Attribute_Name
=> Name_Address
),
344 Make_Attribute_Reference
(Loc
,
346 Attribute_Name
=> Name_Address
),
347 Make_Attribute_Reference
(Loc
,
349 Attribute_Name
=> Name_Length
)));
352 Rewrite
(N
, Call_Node
);
356 when RE_Not_Available
=>
358 end Build_Boolean_Array_Proc_Call
;
360 ---------------------------------
361 -- Expand_Allocator_Expression --
362 ---------------------------------
364 procedure Expand_Allocator_Expression
(N
: Node_Id
) is
365 Loc
: constant Source_Ptr
:= Sloc
(N
);
366 Exp
: constant Node_Id
:= Expression
(Expression
(N
));
367 Indic
: constant Node_Id
:= Subtype_Mark
(Expression
(N
));
368 PtrT
: constant Entity_Id
:= Etype
(N
);
369 T
: constant Entity_Id
:= Entity
(Indic
);
374 Aggr_In_Place
: constant Boolean := Is_Delayed_Aggregate
(Exp
);
376 Tag_Assign
: Node_Id
;
380 if Is_Tagged_Type
(T
) or else Controlled_Type
(T
) then
382 -- Actions inserted before:
383 -- Temp : constant ptr_T := new T'(Expression);
384 -- <no CW> Temp._tag := T'tag;
385 -- <CTRL> Adjust (Finalizable (Temp.all));
386 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
388 -- We analyze by hand the new internal allocator to avoid
389 -- any recursion and inappropriate call to Initialize
391 if not Aggr_In_Place
then
392 Remove_Side_Effects
(Exp
);
396 Make_Defining_Identifier
(Loc
, New_Internal_Name
('P'));
398 -- For a class wide allocation generate the following code:
400 -- type Equiv_Record is record ... end record;
401 -- implicit subtype CW is <Class_Wide_Subytpe>;
402 -- temp : PtrT := new CW'(CW!(expr));
404 if Is_Class_Wide_Type
(T
) then
405 Expand_Subtype_From_Expr
(Empty
, T
, Indic
, Exp
);
407 Set_Expression
(Expression
(N
),
408 Unchecked_Convert_To
(Entity
(Indic
), Exp
));
410 Analyze_And_Resolve
(Expression
(N
), Entity
(Indic
));
413 if Aggr_In_Place
then
415 Make_Object_Declaration
(Loc
,
416 Defining_Identifier
=> Temp
,
417 Object_Definition
=> New_Reference_To
(PtrT
, Loc
),
420 New_Reference_To
(Etype
(Exp
), Loc
)));
422 Set_Comes_From_Source
423 (Expression
(Tmp_Node
), Comes_From_Source
(N
));
425 Set_No_Initialization
(Expression
(Tmp_Node
));
426 Insert_Action
(N
, Tmp_Node
);
428 if Controlled_Type
(T
)
429 and then Ekind
(PtrT
) = E_Anonymous_Access_Type
431 -- Create local finalization list for access parameter
433 Flist
:= Get_Allocator_Final_List
(N
, Base_Type
(T
), PtrT
);
436 Convert_Aggr_In_Allocator
(Tmp_Node
, Exp
);
438 Node
:= Relocate_Node
(N
);
441 Make_Object_Declaration
(Loc
,
442 Defining_Identifier
=> Temp
,
443 Constant_Present
=> True,
444 Object_Definition
=> New_Reference_To
(PtrT
, Loc
),
445 Expression
=> Node
));
448 -- Suppress the tag assignment when Java_VM because JVM tags
449 -- are represented implicitly in objects.
451 if Is_Tagged_Type
(T
)
452 and then not Is_Class_Wide_Type
(T
)
456 Make_Assignment_Statement
(Loc
,
458 Make_Selected_Component
(Loc
,
459 Prefix
=> New_Reference_To
(Temp
, Loc
),
461 New_Reference_To
(Tag_Component
(T
), Loc
)),
464 Unchecked_Convert_To
(RTE
(RE_Tag
),
465 New_Reference_To
(Access_Disp_Table
(T
), Loc
)));
467 -- The previous assignment has to be done in any case
469 Set_Assignment_OK
(Name
(Tag_Assign
));
470 Insert_Action
(N
, Tag_Assign
);
472 elsif Is_Private_Type
(T
)
473 and then Is_Tagged_Type
(Underlying_Type
(T
))
477 Utyp
: constant Entity_Id
:= Underlying_Type
(T
);
478 Ref
: constant Node_Id
:=
479 Unchecked_Convert_To
(Utyp
,
480 Make_Explicit_Dereference
(Loc
,
481 New_Reference_To
(Temp
, Loc
)));
485 Make_Assignment_Statement
(Loc
,
487 Make_Selected_Component
(Loc
,
490 New_Reference_To
(Tag_Component
(Utyp
), Loc
)),
493 Unchecked_Convert_To
(RTE
(RE_Tag
),
495 Access_Disp_Table
(Utyp
), Loc
)));
497 Set_Assignment_OK
(Name
(Tag_Assign
));
498 Insert_Action
(N
, Tag_Assign
);
502 if Controlled_Type
(Designated_Type
(PtrT
))
503 and then Controlled_Type
(T
)
507 Apool
: constant Entity_Id
:=
508 Associated_Storage_Pool
(PtrT
);
511 -- If it is an allocation on the secondary stack
512 -- (i.e. a value returned from a function), the object
513 -- is attached on the caller side as soon as the call
514 -- is completed (see Expand_Ctrl_Function_Call)
516 if Is_RTE
(Apool
, RE_SS_Pool
) then
518 F
: constant Entity_Id
:=
519 Make_Defining_Identifier
(Loc
,
520 New_Internal_Name
('F'));
523 Make_Object_Declaration
(Loc
,
524 Defining_Identifier
=> F
,
525 Object_Definition
=> New_Reference_To
(RTE
526 (RE_Finalizable_Ptr
), Loc
)));
528 Flist
:= New_Reference_To
(F
, Loc
);
529 Attach
:= Make_Integer_Literal
(Loc
, 1);
532 -- Normal case, not a secondary stack allocation
535 if Controlled_Type
(T
)
536 and then Ekind
(PtrT
) = E_Anonymous_Access_Type
538 -- Create local finalization list for access parameter
541 Get_Allocator_Final_List
(N
, Base_Type
(T
), PtrT
);
543 Flist
:= Find_Final_List
(PtrT
);
546 Attach
:= Make_Integer_Literal
(Loc
, 2);
549 if not Aggr_In_Place
then
554 -- An unchecked conversion is needed in the
555 -- classwide case because the designated type
556 -- can be an ancestor of the subtype mark of
559 Unchecked_Convert_To
(T
,
560 Make_Explicit_Dereference
(Loc
,
561 New_Reference_To
(Temp
, Loc
))),
565 With_Attach
=> Attach
));
570 Rewrite
(N
, New_Reference_To
(Temp
, Loc
));
571 Analyze_And_Resolve
(N
, PtrT
);
573 elsif Aggr_In_Place
then
575 Make_Defining_Identifier
(Loc
, New_Internal_Name
('P'));
577 Make_Object_Declaration
(Loc
,
578 Defining_Identifier
=> Temp
,
579 Object_Definition
=> New_Reference_To
(PtrT
, Loc
),
580 Expression
=> Make_Allocator
(Loc
,
581 New_Reference_To
(Etype
(Exp
), Loc
)));
583 Set_Comes_From_Source
584 (Expression
(Tmp_Node
), Comes_From_Source
(N
));
586 Set_No_Initialization
(Expression
(Tmp_Node
));
587 Insert_Action
(N
, Tmp_Node
);
588 Convert_Aggr_In_Allocator
(Tmp_Node
, Exp
);
589 Rewrite
(N
, New_Reference_To
(Temp
, Loc
));
590 Analyze_And_Resolve
(N
, PtrT
);
592 elsif Is_Access_Type
(Designated_Type
(PtrT
))
593 and then Nkind
(Exp
) = N_Allocator
594 and then Nkind
(Expression
(Exp
)) /= N_Qualified_Expression
596 -- Apply constraint to designated subtype indication
598 Apply_Constraint_Check
(Expression
(Exp
),
599 Designated_Type
(Designated_Type
(PtrT
)),
602 if Nkind
(Expression
(Exp
)) = N_Raise_Constraint_Error
then
604 -- Propagate constraint_error to enclosing allocator
606 Rewrite
(Exp
, New_Copy
(Expression
(Exp
)));
609 -- First check against the type of the qualified expression
611 -- NOTE: The commented call should be correct, but for
612 -- some reason causes the compiler to bomb (sigsegv) on
613 -- ACVC test c34007g, so for now we just perform the old
614 -- (incorrect) test against the designated subtype with
615 -- no sliding in the else part of the if statement below.
618 -- Apply_Constraint_Check (Exp, T, No_Sliding => True);
620 -- A check is also needed in cases where the designated
621 -- subtype is constrained and differs from the subtype
622 -- given in the qualified expression. Note that the check
623 -- on the qualified expression does not allow sliding,
624 -- but this check does (a relaxation from Ada 83).
626 if Is_Constrained
(Designated_Type
(PtrT
))
627 and then not Subtypes_Statically_Match
628 (T
, Designated_Type
(PtrT
))
630 Apply_Constraint_Check
631 (Exp
, Designated_Type
(PtrT
), No_Sliding
=> False);
633 -- The nonsliding check should really be performed
634 -- (unconditionally) against the subtype of the
635 -- qualified expression, but that causes a problem
636 -- with c34007g (see above), so for now we retain this.
639 Apply_Constraint_Check
640 (Exp
, Designated_Type
(PtrT
), No_Sliding
=> True);
645 when RE_Not_Available
=>
647 end Expand_Allocator_Expression
;
649 -----------------------------
650 -- Expand_Array_Comparison --
651 -----------------------------
653 -- Expansion is only required in the case of array types. For the
654 -- unpacked case, an appropriate runtime routine is called. For
655 -- packed cases, and also in some other cases where a runtime
656 -- routine cannot be called, the form of the expansion is:
658 -- [body for greater_nn; boolean_expression]
660 -- The body is built by Make_Array_Comparison_Op, and the form of the
661 -- Boolean expression depends on the operator involved.
663 procedure Expand_Array_Comparison
(N
: Node_Id
) is
664 Loc
: constant Source_Ptr
:= Sloc
(N
);
665 Op1
: Node_Id
:= Left_Opnd
(N
);
666 Op2
: Node_Id
:= Right_Opnd
(N
);
667 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
668 Ctyp
: constant Entity_Id
:= Component_Type
(Typ1
);
672 Func_Name
: Entity_Id
;
676 Byte_Addressable
: constant Boolean := System_Storage_Unit
= Byte
'Size;
677 -- True for byte addressable target
679 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean;
680 -- Returns True if the length of the given operand is known to be
681 -- less than 4. Returns False if this length is known to be four
682 -- or greater or is not known at compile time.
684 ------------------------
685 -- Length_Less_Than_4 --
686 ------------------------
688 function Length_Less_Than_4
(Opnd
: Node_Id
) return Boolean is
689 Otyp
: constant Entity_Id
:= Etype
(Opnd
);
692 if Ekind
(Otyp
) = E_String_Literal_Subtype
then
693 return String_Literal_Length
(Otyp
) < 4;
697 Ityp
: constant Entity_Id
:= Etype
(First_Index
(Otyp
));
698 Lo
: constant Node_Id
:= Type_Low_Bound
(Ityp
);
699 Hi
: constant Node_Id
:= Type_High_Bound
(Ityp
);
704 if Compile_Time_Known_Value
(Lo
) then
705 Lov
:= Expr_Value
(Lo
);
710 if Compile_Time_Known_Value
(Hi
) then
711 Hiv
:= Expr_Value
(Hi
);
716 return Hiv
< Lov
+ 3;
719 end Length_Less_Than_4
;
721 -- Start of processing for Expand_Array_Comparison
724 -- Deal first with unpacked case, where we can call a runtime routine
725 -- except that we avoid this for targets for which are not addressable
726 -- by bytes, and for the JVM, since the JVM does not support direct
727 -- addressing of array components.
729 if not Is_Bit_Packed_Array
(Typ1
)
730 and then Byte_Addressable
733 -- The call we generate is:
735 -- Compare_Array_xn[_Unaligned]
736 -- (left'address, right'address, left'length, right'length) <op> 0
738 -- x = U for unsigned, S for signed
739 -- n = 8,16,32,64 for component size
740 -- Add _Unaligned if length < 4 and component size is 8.
741 -- <op> is the standard comparison operator
743 if Component_Size
(Typ1
) = 8 then
744 if Length_Less_Than_4
(Op1
)
746 Length_Less_Than_4
(Op2
)
748 if Is_Unsigned_Type
(Ctyp
) then
749 Comp
:= RE_Compare_Array_U8_Unaligned
;
751 Comp
:= RE_Compare_Array_S8_Unaligned
;
755 if Is_Unsigned_Type
(Ctyp
) then
756 Comp
:= RE_Compare_Array_U8
;
758 Comp
:= RE_Compare_Array_S8
;
762 elsif Component_Size
(Typ1
) = 16 then
763 if Is_Unsigned_Type
(Ctyp
) then
764 Comp
:= RE_Compare_Array_U16
;
766 Comp
:= RE_Compare_Array_S16
;
769 elsif Component_Size
(Typ1
) = 32 then
770 if Is_Unsigned_Type
(Ctyp
) then
771 Comp
:= RE_Compare_Array_U32
;
773 Comp
:= RE_Compare_Array_S32
;
776 else pragma Assert
(Component_Size
(Typ1
) = 64);
777 if Is_Unsigned_Type
(Ctyp
) then
778 Comp
:= RE_Compare_Array_U64
;
780 Comp
:= RE_Compare_Array_S64
;
784 Remove_Side_Effects
(Op1
, Name_Req
=> True);
785 Remove_Side_Effects
(Op2
, Name_Req
=> True);
788 Make_Function_Call
(Sloc
(Op1
),
789 Name
=> New_Occurrence_Of
(RTE
(Comp
), Loc
),
791 Parameter_Associations
=> New_List
(
792 Make_Attribute_Reference
(Loc
,
793 Prefix
=> Relocate_Node
(Op1
),
794 Attribute_Name
=> Name_Address
),
796 Make_Attribute_Reference
(Loc
,
797 Prefix
=> Relocate_Node
(Op2
),
798 Attribute_Name
=> Name_Address
),
800 Make_Attribute_Reference
(Loc
,
801 Prefix
=> Relocate_Node
(Op1
),
802 Attribute_Name
=> Name_Length
),
804 Make_Attribute_Reference
(Loc
,
805 Prefix
=> Relocate_Node
(Op2
),
806 Attribute_Name
=> Name_Length
))));
809 Make_Integer_Literal
(Sloc
(Op2
),
812 Analyze_And_Resolve
(Op1
, Standard_Integer
);
813 Analyze_And_Resolve
(Op2
, Standard_Integer
);
817 -- Cases where we cannot make runtime call
819 -- For (a <= b) we convert to not (a > b)
821 if Chars
(N
) = Name_Op_Le
then
827 Right_Opnd
=> Op2
)));
828 Analyze_And_Resolve
(N
, Standard_Boolean
);
831 -- For < the Boolean expression is
832 -- greater__nn (op2, op1)
834 elsif Chars
(N
) = Name_Op_Lt
then
835 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
839 Op1
:= Right_Opnd
(N
);
840 Op2
:= Left_Opnd
(N
);
842 -- For (a >= b) we convert to not (a < b)
844 elsif Chars
(N
) = Name_Op_Ge
then
850 Right_Opnd
=> Op2
)));
851 Analyze_And_Resolve
(N
, Standard_Boolean
);
854 -- For > the Boolean expression is
855 -- greater__nn (op1, op2)
858 pragma Assert
(Chars
(N
) = Name_Op_Gt
);
859 Func_Body
:= Make_Array_Comparison_Op
(Typ1
, N
);
862 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
864 Make_Function_Call
(Loc
,
865 Name
=> New_Reference_To
(Func_Name
, Loc
),
866 Parameter_Associations
=> New_List
(Op1
, Op2
));
868 Insert_Action
(N
, Func_Body
);
870 Analyze_And_Resolve
(N
, Standard_Boolean
);
873 when RE_Not_Available
=>
875 end Expand_Array_Comparison
;
877 ---------------------------
878 -- Expand_Array_Equality --
879 ---------------------------
881 -- Expand an equality function for multi-dimensional arrays. Here is
882 -- an example of such a function for Nb_Dimension = 2
884 -- function Enn (A : atyp; B : btyp) return boolean is
886 -- if (A'length (1) = 0 or else A'length (2) = 0)
888 -- (B'length (1) = 0 or else B'length (2) = 0)
890 -- return True; -- RM 4.5.2(22)
893 -- if A'length (1) /= B'length (1)
895 -- A'length (2) /= B'length (2)
897 -- return False; -- RM 4.5.2(23)
901 -- A1 : Index_T1 := A'first (1);
902 -- B1 : Index_T1 := B'first (1);
906 -- A2 : Index_T2 := A'first (2);
907 -- B2 : Index_T2 := B'first (2);
910 -- if A (A1, A2) /= B (B1, B2) then
914 -- exit when A2 = A'last (2);
915 -- A2 := Index_T2'succ (A2);
916 -- B2 := Index_T2'succ (B2);
920 -- exit when A1 = A'last (1);
921 -- A1 := Index_T1'succ (A1);
922 -- B1 := Index_T1'succ (B1);
929 -- Note on the formal types used (atyp and btyp). If either of the
930 -- arrays is of a private type, we use the underlying type, and
931 -- do an unchecked conversion of the actual. If either of the arrays
932 -- has a bound depending on a discriminant, then we use the base type
933 -- since otherwise we have an escaped discriminant in the function.
935 -- If both arrays are constrained and have the same bounds, we can
936 -- generate a loop with an explicit iteration scheme using a 'Range
937 -- attribute over the first array.
939 function Expand_Array_Equality
944 Typ
: Entity_Id
) return Node_Id
946 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
947 Decls
: constant List_Id
:= New_List
;
948 Index_List1
: constant List_Id
:= New_List
;
949 Index_List2
: constant List_Id
:= New_List
;
953 Func_Name
: Entity_Id
;
956 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
957 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
961 -- The parameter types to be used for the formals
966 Num
: Int
) return Node_Id
;
967 -- This builds the attribute reference Arr'Nam (Expr)
969 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
;
970 -- Create one statement to compare corresponding components,
971 -- designated by a full set of indices.
973 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
;
974 -- Given one of the arguments, computes the appropriate type to
975 -- be used for that argument in the corresponding function formal
977 function Handle_One_Dimension
979 Index
: Node_Id
) return Node_Id
;
980 -- This procedure returns the following code
983 -- Bn : Index_T := B'First (N);
987 -- exit when An = A'Last (N);
988 -- An := Index_T'Succ (An)
989 -- Bn := Index_T'Succ (Bn)
993 -- If both indices are constrained and identical, the procedure
994 -- returns a simpler loop:
996 -- for An in A'Range (N) loop
1000 -- N is the dimension for which we are generating a loop. Index is the
1001 -- N'th index node, whose Etype is Index_Type_n in the above code.
1002 -- The xxx statement is either the loop or declare for the next
1003 -- dimension or if this is the last dimension the comparison
1004 -- of corresponding components of the arrays.
1006 -- The actual way the code works is to return the comparison
1007 -- of corresponding components for the N+1 call. That's neater!
1009 function Test_Empty_Arrays
return Node_Id
;
1010 -- This function constructs the test for both arrays being empty
1011 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1013 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1015 function Test_Lengths_Correspond
return Node_Id
;
1016 -- This function constructs the test for arrays having different
1017 -- lengths in at least one index position, in which case resull
1019 -- A'length (1) /= B'length (1)
1021 -- A'length (2) /= B'length (2)
1032 Num
: Int
) return Node_Id
1036 Make_Attribute_Reference
(Loc
,
1037 Attribute_Name
=> Nam
,
1038 Prefix
=> New_Reference_To
(Arr
, Loc
),
1039 Expressions
=> New_List
(Make_Integer_Literal
(Loc
, Num
)));
1042 ------------------------
1043 -- Component_Equality --
1044 ------------------------
1046 function Component_Equality
(Typ
: Entity_Id
) return Node_Id
is
1051 -- if a(i1...) /= b(j1...) then return false; end if;
1054 Make_Indexed_Component
(Loc
,
1055 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
1056 Expressions
=> Index_List1
);
1059 Make_Indexed_Component
(Loc
,
1060 Prefix
=> Make_Identifier
(Loc
, Chars
(B
)),
1061 Expressions
=> Index_List2
);
1063 Test
:= Expand_Composite_Equality
1064 (Nod
, Component_Type
(Typ
), L
, R
, Decls
);
1066 -- If some (sub)component is an unchecked_union, the whole
1067 -- operation will raise program error.
1069 if Nkind
(Test
) = N_Raise_Program_Error
then
1074 Make_Implicit_If_Statement
(Nod
,
1075 Condition
=> Make_Op_Not
(Loc
, Right_Opnd
=> Test
),
1076 Then_Statements
=> New_List
(
1077 Make_Return_Statement
(Loc
,
1078 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))));
1080 end Component_Equality
;
1086 function Get_Arg_Type
(N
: Node_Id
) return Entity_Id
is
1097 T
:= Underlying_Type
(T
);
1099 X
:= First_Index
(T
);
1100 while Present
(X
) loop
1101 if Denotes_Discriminant
(Type_Low_Bound
(Etype
(X
)))
1103 Denotes_Discriminant
(Type_High_Bound
(Etype
(X
)))
1116 --------------------------
1117 -- Handle_One_Dimension --
1118 ---------------------------
1120 function Handle_One_Dimension
1122 Index
: Node_Id
) return Node_Id
1124 Need_Separate_Indexes
: constant Boolean :=
1126 or else not Is_Constrained
(Ltyp
);
1127 -- If the index types are identical, and we are working with
1128 -- constrained types, then we can use the same index for both of
1131 An
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
,
1132 Chars
=> New_Internal_Name
('A'));
1135 Index_T
: Entity_Id
;
1140 if N
> Number_Dimensions
(Ltyp
) then
1141 return Component_Equality
(Ltyp
);
1144 -- Case where we generate a loop
1146 Index_T
:= Base_Type
(Etype
(Index
));
1148 if Need_Separate_Indexes
then
1150 Make_Defining_Identifier
(Loc
,
1151 Chars
=> New_Internal_Name
('B'));
1156 Append
(New_Reference_To
(An
, Loc
), Index_List1
);
1157 Append
(New_Reference_To
(Bn
, Loc
), Index_List2
);
1159 Stm_List
:= New_List
(
1160 Handle_One_Dimension
(N
+ 1, Next_Index
(Index
)));
1162 if Need_Separate_Indexes
then
1163 -- Generate guard for loop, followed by increments of indices
1165 Append_To
(Stm_List
,
1166 Make_Exit_Statement
(Loc
,
1169 Left_Opnd
=> New_Reference_To
(An
, Loc
),
1170 Right_Opnd
=> Arr_Attr
(A
, Name_Last
, N
))));
1172 Append_To
(Stm_List
,
1173 Make_Assignment_Statement
(Loc
,
1174 Name
=> New_Reference_To
(An
, Loc
),
1176 Make_Attribute_Reference
(Loc
,
1177 Prefix
=> New_Reference_To
(Index_T
, Loc
),
1178 Attribute_Name
=> Name_Succ
,
1179 Expressions
=> New_List
(New_Reference_To
(An
, Loc
)))));
1181 Append_To
(Stm_List
,
1182 Make_Assignment_Statement
(Loc
,
1183 Name
=> New_Reference_To
(Bn
, Loc
),
1185 Make_Attribute_Reference
(Loc
,
1186 Prefix
=> New_Reference_To
(Index_T
, Loc
),
1187 Attribute_Name
=> Name_Succ
,
1188 Expressions
=> New_List
(New_Reference_To
(Bn
, Loc
)))));
1191 -- If separate indexes, we need a declare block for An and Bn,
1192 -- and a loop without an iteration scheme.
1194 if Need_Separate_Indexes
then
1196 Make_Implicit_Loop_Statement
(Nod
, Statements
=> Stm_List
);
1199 Make_Block_Statement
(Loc
,
1200 Declarations
=> New_List
(
1201 Make_Object_Declaration
(Loc
,
1202 Defining_Identifier
=> An
,
1203 Object_Definition
=> New_Reference_To
(Index_T
, Loc
),
1204 Expression
=> Arr_Attr
(A
, Name_First
, N
)),
1206 Make_Object_Declaration
(Loc
,
1207 Defining_Identifier
=> Bn
,
1208 Object_Definition
=> New_Reference_To
(Index_T
, Loc
),
1209 Expression
=> Arr_Attr
(B
, Name_First
, N
))),
1211 Handled_Statement_Sequence
=>
1212 Make_Handled_Sequence_Of_Statements
(Loc
,
1213 Statements
=> New_List
(Loop_Stm
)));
1215 -- If no separate indexes, return loop statement with explicit
1216 -- iteration scheme on its own
1220 Make_Implicit_Loop_Statement
(Nod
,
1221 Statements
=> Stm_List
,
1223 Make_Iteration_Scheme
(Loc
,
1224 Loop_Parameter_Specification
=>
1225 Make_Loop_Parameter_Specification
(Loc
,
1226 Defining_Identifier
=> An
,
1227 Discrete_Subtype_Definition
=>
1228 Arr_Attr
(A
, Name_Range
, N
))));
1231 end Handle_One_Dimension
;
1233 -----------------------
1234 -- Test_Empty_Arrays --
1235 -----------------------
1237 function Test_Empty_Arrays
return Node_Id
is
1247 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
1250 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
1251 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
1255 Left_Opnd
=> Arr_Attr
(B
, Name_Length
, J
),
1256 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
1265 Left_Opnd
=> Relocate_Node
(Alist
),
1266 Right_Opnd
=> Atest
);
1270 Left_Opnd
=> Relocate_Node
(Blist
),
1271 Right_Opnd
=> Btest
);
1278 Right_Opnd
=> Blist
);
1279 end Test_Empty_Arrays
;
1281 -----------------------------
1282 -- Test_Lengths_Correspond --
1283 -----------------------------
1285 function Test_Lengths_Correspond
return Node_Id
is
1291 for J
in 1 .. Number_Dimensions
(Ltyp
) loop
1294 Left_Opnd
=> Arr_Attr
(A
, Name_Length
, J
),
1295 Right_Opnd
=> Arr_Attr
(B
, Name_Length
, J
));
1302 Left_Opnd
=> Relocate_Node
(Result
),
1303 Right_Opnd
=> Rtest
);
1308 end Test_Lengths_Correspond
;
1310 -- Start of processing for Expand_Array_Equality
1313 Ltyp
:= Get_Arg_Type
(Lhs
);
1314 Rtyp
:= Get_Arg_Type
(Rhs
);
1316 -- For now, if the argument types are not the same, go to the
1317 -- base type, since the code assumes that the formals have the
1318 -- same type. This is fixable in future ???
1320 if Ltyp
/= Rtyp
then
1321 Ltyp
:= Base_Type
(Ltyp
);
1322 Rtyp
:= Base_Type
(Rtyp
);
1323 pragma Assert
(Ltyp
= Rtyp
);
1326 -- Build list of formals for function
1328 Formals
:= New_List
(
1329 Make_Parameter_Specification
(Loc
,
1330 Defining_Identifier
=> A
,
1331 Parameter_Type
=> New_Reference_To
(Ltyp
, Loc
)),
1333 Make_Parameter_Specification
(Loc
,
1334 Defining_Identifier
=> B
,
1335 Parameter_Type
=> New_Reference_To
(Rtyp
, Loc
)));
1337 Func_Name
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('E'));
1339 -- Build statement sequence for function
1342 Make_Subprogram_Body
(Loc
,
1344 Make_Function_Specification
(Loc
,
1345 Defining_Unit_Name
=> Func_Name
,
1346 Parameter_Specifications
=> Formals
,
1347 Subtype_Mark
=> New_Reference_To
(Standard_Boolean
, Loc
)),
1349 Declarations
=> Decls
,
1351 Handled_Statement_Sequence
=>
1352 Make_Handled_Sequence_Of_Statements
(Loc
,
1353 Statements
=> New_List
(
1355 Make_Implicit_If_Statement
(Nod
,
1356 Condition
=> Test_Empty_Arrays
,
1357 Then_Statements
=> New_List
(
1358 Make_Return_Statement
(Loc
,
1360 New_Occurrence_Of
(Standard_True
, Loc
)))),
1362 Make_Implicit_If_Statement
(Nod
,
1363 Condition
=> Test_Lengths_Correspond
,
1364 Then_Statements
=> New_List
(
1365 Make_Return_Statement
(Loc
,
1367 New_Occurrence_Of
(Standard_False
, Loc
)))),
1369 Handle_One_Dimension
(1, First_Index
(Ltyp
)),
1371 Make_Return_Statement
(Loc
,
1372 Expression
=> New_Occurrence_Of
(Standard_True
, Loc
)))));
1374 Set_Has_Completion
(Func_Name
, True);
1375 Set_Is_Inlined
(Func_Name
);
1377 -- If the array type is distinct from the type of the arguments,
1378 -- it is the full view of a private type. Apply an unchecked
1379 -- conversion to insure that analysis of the call succeeds.
1389 or else Base_Type
(Etype
(Lhs
)) /= Base_Type
(Ltyp
)
1391 L
:= OK_Convert_To
(Ltyp
, Lhs
);
1395 or else Base_Type
(Etype
(Rhs
)) /= Base_Type
(Rtyp
)
1397 R
:= OK_Convert_To
(Rtyp
, Rhs
);
1400 Actuals
:= New_List
(L
, R
);
1403 Append_To
(Bodies
, Func_Body
);
1406 Make_Function_Call
(Loc
,
1407 Name
=> New_Reference_To
(Func_Name
, Loc
),
1408 Parameter_Associations
=> Actuals
);
1409 end Expand_Array_Equality
;
1411 -----------------------------
1412 -- Expand_Boolean_Operator --
1413 -----------------------------
1415 -- Note that we first get the actual subtypes of the operands,
1416 -- since we always want to deal with types that have bounds.
1418 procedure Expand_Boolean_Operator
(N
: Node_Id
) is
1419 Typ
: constant Entity_Id
:= Etype
(N
);
1422 if Is_Bit_Packed_Array
(Typ
) then
1423 Expand_Packed_Boolean_Operator
(N
);
1426 -- For the normal non-packed case, the general expansion is
1427 -- to build a function for carrying out the comparison (using
1428 -- Make_Boolean_Array_Op) and then inserting it into the tree.
1429 -- The original operator node is then rewritten as a call to
1433 Loc
: constant Source_Ptr
:= Sloc
(N
);
1434 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
1435 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
1436 Func_Body
: Node_Id
;
1437 Func_Name
: Entity_Id
;
1440 Convert_To_Actual_Subtype
(L
);
1441 Convert_To_Actual_Subtype
(R
);
1442 Ensure_Defined
(Etype
(L
), N
);
1443 Ensure_Defined
(Etype
(R
), N
);
1444 Apply_Length_Check
(R
, Etype
(L
));
1446 if Nkind
(Parent
(N
)) = N_Assignment_Statement
1447 and then Safe_In_Place_Array_Op
(Name
(Parent
(N
)), L
, R
)
1449 Build_Boolean_Array_Proc_Call
(Parent
(N
), L
, R
);
1451 elsif Nkind
(Parent
(N
)) = N_Op_Not
1452 and then Nkind
(N
) = N_Op_And
1454 Safe_In_Place_Array_Op
(Name
(Parent
(Parent
(N
))), L
, R
)
1459 Func_Body
:= Make_Boolean_Array_Op
(Etype
(L
), N
);
1460 Func_Name
:= Defining_Unit_Name
(Specification
(Func_Body
));
1461 Insert_Action
(N
, Func_Body
);
1463 -- Now rewrite the expression with a call
1466 Make_Function_Call
(Loc
,
1467 Name
=> New_Reference_To
(Func_Name
, Loc
),
1468 Parameter_Associations
=>
1470 (L
, Make_Type_Conversion
1471 (Loc
, New_Reference_To
(Etype
(L
), Loc
), R
))));
1473 Analyze_And_Resolve
(N
, Typ
);
1477 end Expand_Boolean_Operator
;
1479 -------------------------------
1480 -- Expand_Composite_Equality --
1481 -------------------------------
1483 -- This function is only called for comparing internal fields of composite
1484 -- types when these fields are themselves composites. This is a special
1485 -- case because it is not possible to respect normal Ada visibility rules.
1487 function Expand_Composite_Equality
1492 Bodies
: List_Id
) return Node_Id
1494 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
1495 Full_Type
: Entity_Id
;
1500 if Is_Private_Type
(Typ
) then
1501 Full_Type
:= Underlying_Type
(Typ
);
1506 -- Defense against malformed private types with no completion
1507 -- the error will be diagnosed later by check_completion
1509 if No
(Full_Type
) then
1510 return New_Reference_To
(Standard_False
, Loc
);
1513 Full_Type
:= Base_Type
(Full_Type
);
1515 if Is_Array_Type
(Full_Type
) then
1517 -- If the operand is an elementary type other than a floating-point
1518 -- type, then we can simply use the built-in block bitwise equality,
1519 -- since the predefined equality operators always apply and bitwise
1520 -- equality is fine for all these cases.
1522 if Is_Elementary_Type
(Component_Type
(Full_Type
))
1523 and then not Is_Floating_Point_Type
(Component_Type
(Full_Type
))
1525 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
1527 -- For composite component types, and floating-point types, use
1528 -- the expansion. This deals with tagged component types (where
1529 -- we use the applicable equality routine) and floating-point,
1530 -- (where we need to worry about negative zeroes), and also the
1531 -- case of any composite type recursively containing such fields.
1534 return Expand_Array_Equality
(Nod
, Lhs
, Rhs
, Bodies
, Full_Type
);
1537 elsif Is_Tagged_Type
(Full_Type
) then
1539 -- Call the primitive operation "=" of this type
1541 if Is_Class_Wide_Type
(Full_Type
) then
1542 Full_Type
:= Root_Type
(Full_Type
);
1545 -- If this is derived from an untagged private type completed
1546 -- with a tagged type, it does not have a full view, so we
1547 -- use the primitive operations of the private type.
1548 -- This check should no longer be necessary when these
1549 -- types receive their full views ???
1551 if Is_Private_Type
(Typ
)
1552 and then not Is_Tagged_Type
(Typ
)
1553 and then not Is_Controlled
(Typ
)
1554 and then Is_Derived_Type
(Typ
)
1555 and then No
(Full_View
(Typ
))
1557 Prim
:= First_Elmt
(Collect_Primitive_Operations
(Typ
));
1559 Prim
:= First_Elmt
(Primitive_Operations
(Full_Type
));
1563 Eq_Op
:= Node
(Prim
);
1564 exit when Chars
(Eq_Op
) = Name_Op_Eq
1565 and then Etype
(First_Formal
(Eq_Op
)) =
1566 Etype
(Next_Formal
(First_Formal
(Eq_Op
)))
1567 and then Base_Type
(Etype
(Eq_Op
)) = Standard_Boolean
;
1569 pragma Assert
(Present
(Prim
));
1572 Eq_Op
:= Node
(Prim
);
1575 Make_Function_Call
(Loc
,
1576 Name
=> New_Reference_To
(Eq_Op
, Loc
),
1577 Parameter_Associations
=>
1579 (Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Lhs
),
1580 Unchecked_Convert_To
(Etype
(First_Formal
(Eq_Op
)), Rhs
)));
1582 elsif Is_Record_Type
(Full_Type
) then
1583 Eq_Op
:= TSS
(Full_Type
, TSS_Composite_Equality
);
1585 if Present
(Eq_Op
) then
1586 if Etype
(First_Formal
(Eq_Op
)) /= Full_Type
then
1588 -- Inherited equality from parent type. Convert the actuals
1589 -- to match signature of operation.
1592 T
: constant Entity_Id
:= Etype
(First_Formal
(Eq_Op
));
1596 Make_Function_Call
(Loc
,
1597 Name
=> New_Reference_To
(Eq_Op
, Loc
),
1598 Parameter_Associations
=>
1599 New_List
(OK_Convert_To
(T
, Lhs
),
1600 OK_Convert_To
(T
, Rhs
)));
1604 -- Comparison between Unchecked_Union components
1606 if Is_Unchecked_Union
(Full_Type
) then
1608 Lhs_Type
: Node_Id
:= Full_Type
;
1609 Rhs_Type
: Node_Id
:= Full_Type
;
1610 Lhs_Discr_Val
: Node_Id
;
1611 Rhs_Discr_Val
: Node_Id
;
1616 if Nkind
(Lhs
) = N_Selected_Component
then
1617 Lhs_Type
:= Etype
(Entity
(Selector_Name
(Lhs
)));
1622 if Nkind
(Rhs
) = N_Selected_Component
then
1623 Rhs_Type
:= Etype
(Entity
(Selector_Name
(Rhs
)));
1626 -- Lhs of the composite equality
1628 if Is_Constrained
(Lhs_Type
) then
1630 -- Since the enclosing record can never be an
1631 -- Unchecked_Union (this code is executed for records
1632 -- that do not have variants), we may reference its
1635 if Nkind
(Lhs
) = N_Selected_Component
1636 and then Has_Per_Object_Constraint
(
1637 Entity
(Selector_Name
(Lhs
)))
1640 Make_Selected_Component
(Loc
,
1641 Prefix
=> Prefix
(Lhs
),
1644 Get_Discriminant_Value
(
1645 First_Discriminant
(Lhs_Type
),
1647 Stored_Constraint
(Lhs_Type
))));
1650 Lhs_Discr_Val
:= New_Copy
(
1651 Get_Discriminant_Value
(
1652 First_Discriminant
(Lhs_Type
),
1654 Stored_Constraint
(Lhs_Type
)));
1658 -- It is not possible to infer the discriminant since
1659 -- the subtype is not constrained.
1662 Make_Raise_Program_Error
(Loc
,
1663 Reason
=> PE_Unchecked_Union_Restriction
);
1666 -- Rhs of the composite equality
1668 if Is_Constrained
(Rhs_Type
) then
1669 if Nkind
(Rhs
) = N_Selected_Component
1670 and then Has_Per_Object_Constraint
(
1671 Entity
(Selector_Name
(Rhs
)))
1674 Make_Selected_Component
(Loc
,
1675 Prefix
=> Prefix
(Rhs
),
1678 Get_Discriminant_Value
(
1679 First_Discriminant
(Rhs_Type
),
1681 Stored_Constraint
(Rhs_Type
))));
1684 Rhs_Discr_Val
:= New_Copy
(
1685 Get_Discriminant_Value
(
1686 First_Discriminant
(Rhs_Type
),
1688 Stored_Constraint
(Rhs_Type
)));
1693 Make_Raise_Program_Error
(Loc
,
1694 Reason
=> PE_Unchecked_Union_Restriction
);
1697 -- Call the TSS equality function with the inferred
1698 -- discriminant values.
1701 Make_Function_Call
(Loc
,
1702 Name
=> New_Reference_To
(Eq_Op
, Loc
),
1703 Parameter_Associations
=> New_List
(
1711 -- Shouldn't this be an else, we can't fall through
1712 -- the above IF, right???
1715 Make_Function_Call
(Loc
,
1716 Name
=> New_Reference_To
(Eq_Op
, Loc
),
1717 Parameter_Associations
=> New_List
(Lhs
, Rhs
));
1721 return Expand_Record_Equality
(Nod
, Full_Type
, Lhs
, Rhs
, Bodies
);
1725 -- It can be a simple record or the full view of a scalar private
1727 return Make_Op_Eq
(Loc
, Left_Opnd
=> Lhs
, Right_Opnd
=> Rhs
);
1729 end Expand_Composite_Equality
;
1731 ------------------------------
1732 -- Expand_Concatenate_Other --
1733 ------------------------------
1735 -- Let n be the number of array operands to be concatenated, Base_Typ
1736 -- their base type, Ind_Typ their index type, and Arr_Typ the original
1737 -- array type to which the concatenantion operator applies, then the
1738 -- following subprogram is constructed:
1740 -- [function Cnn (S1 : Base_Typ; ...; Sn : Base_Typ) return Base_Typ is
1743 -- if S1'Length /= 0 then
1744 -- L := XXX; --> XXX = S1'First if Arr_Typ is unconstrained
1745 -- XXX = Arr_Typ'First otherwise
1746 -- elsif S2'Length /= 0 then
1747 -- L := YYY; --> YYY = S2'First if Arr_Typ is unconstrained
1748 -- YYY = Arr_Typ'First otherwise
1750 -- elsif Sn-1'Length /= 0 then
1751 -- L := ZZZ; --> ZZZ = Sn-1'First if Arr_Typ is unconstrained
1752 -- ZZZ = Arr_Typ'First otherwise
1760 -- Ind_Typ'Val ((((S1'Length - 1) + S2'Length) + ... + Sn'Length)
1761 -- + Ind_Typ'Pos (L));
1762 -- R : Base_Typ (L .. H);
1764 -- if S1'Length /= 0 then
1768 -- L := Ind_Typ'Succ (L);
1769 -- exit when P = S1'Last;
1770 -- P := Ind_Typ'Succ (P);
1774 -- if S2'Length /= 0 then
1775 -- L := Ind_Typ'Succ (L);
1778 -- L := Ind_Typ'Succ (L);
1779 -- exit when P = S2'Last;
1780 -- P := Ind_Typ'Succ (P);
1786 -- if Sn'Length /= 0 then
1790 -- L := Ind_Typ'Succ (L);
1791 -- exit when P = Sn'Last;
1792 -- P := Ind_Typ'Succ (P);
1800 procedure Expand_Concatenate_Other
(Cnode
: Node_Id
; Opnds
: List_Id
) is
1801 Loc
: constant Source_Ptr
:= Sloc
(Cnode
);
1802 Nb_Opnds
: constant Nat
:= List_Length
(Opnds
);
1804 Arr_Typ
: constant Entity_Id
:= Etype
(Entity
(Cnode
));
1805 Base_Typ
: constant Entity_Id
:= Base_Type
(Etype
(Cnode
));
1806 Ind_Typ
: constant Entity_Id
:= Etype
(First_Index
(Base_Typ
));
1809 Func_Spec
: Node_Id
;
1810 Param_Specs
: List_Id
;
1812 Func_Body
: Node_Id
;
1813 Func_Decls
: List_Id
;
1814 Func_Stmts
: List_Id
;
1819 Elsif_List
: List_Id
;
1821 Declare_Block
: Node_Id
;
1822 Declare_Decls
: List_Id
;
1823 Declare_Stmts
: List_Id
;
1835 function Copy_Into_R_S
(I
: Nat
; Last
: Boolean) return List_Id
;
1836 -- Builds the sequence of statement:
1840 -- L := Ind_Typ'Succ (L);
1841 -- exit when P = Si'Last;
1842 -- P := Ind_Typ'Succ (P);
1845 -- where i is the input parameter I given.
1846 -- If the flag Last is true, the exit statement is emitted before
1847 -- incrementing the lower bound, to prevent the creation out of
1850 function Init_L
(I
: Nat
) return Node_Id
;
1851 -- Builds the statement:
1852 -- L := Arr_Typ'First; If Arr_Typ is constrained
1853 -- L := Si'First; otherwise (where I is the input param given)
1855 function H
return Node_Id
;
1856 -- Builds reference to identifier H
1858 function Ind_Val
(E
: Node_Id
) return Node_Id
;
1859 -- Builds expression Ind_Typ'Val (E);
1861 function L
return Node_Id
;
1862 -- Builds reference to identifier L
1864 function L_Pos
return Node_Id
;
1865 -- Builds expression Integer_Type'(Ind_Typ'Pos (L)). We qualify the
1866 -- expression to avoid universal_integer computations whenever possible,
1867 -- in the expression for the upper bound H.
1869 function L_Succ
return Node_Id
;
1870 -- Builds expression Ind_Typ'Succ (L)
1872 function One
return Node_Id
;
1873 -- Builds integer literal one
1875 function P
return Node_Id
;
1876 -- Builds reference to identifier P
1878 function P_Succ
return Node_Id
;
1879 -- Builds expression Ind_Typ'Succ (P)
1881 function R
return Node_Id
;
1882 -- Builds reference to identifier R
1884 function S
(I
: Nat
) return Node_Id
;
1885 -- Builds reference to identifier Si, where I is the value given
1887 function S_First
(I
: Nat
) return Node_Id
;
1888 -- Builds expression Si'First, where I is the value given
1890 function S_Last
(I
: Nat
) return Node_Id
;
1891 -- Builds expression Si'Last, where I is the value given
1893 function S_Length
(I
: Nat
) return Node_Id
;
1894 -- Builds expression Si'Length, where I is the value given
1896 function S_Length_Test
(I
: Nat
) return Node_Id
;
1897 -- Builds expression Si'Length /= 0, where I is the value given
1903 function Copy_Into_R_S
(I
: Nat
; Last
: Boolean) return List_Id
is
1904 Stmts
: constant List_Id
:= New_List
;
1906 Loop_Stmt
: Node_Id
;
1908 Exit_Stmt
: Node_Id
;
1913 -- First construct the initializations
1915 P_Start
:= Make_Assignment_Statement
(Loc
,
1917 Expression
=> S_First
(I
));
1918 Append_To
(Stmts
, P_Start
);
1920 -- Then build the loop
1922 R_Copy
:= Make_Assignment_Statement
(Loc
,
1923 Name
=> Make_Indexed_Component
(Loc
,
1925 Expressions
=> New_List
(L
)),
1926 Expression
=> Make_Indexed_Component
(Loc
,
1928 Expressions
=> New_List
(P
)));
1930 L_Inc
:= Make_Assignment_Statement
(Loc
,
1932 Expression
=> L_Succ
);
1934 Exit_Stmt
:= Make_Exit_Statement
(Loc
,
1935 Condition
=> Make_Op_Eq
(Loc
, P
, S_Last
(I
)));
1937 P_Inc
:= Make_Assignment_Statement
(Loc
,
1939 Expression
=> P_Succ
);
1943 Make_Implicit_Loop_Statement
(Cnode
,
1944 Statements
=> New_List
(R_Copy
, Exit_Stmt
, L_Inc
, P_Inc
));
1947 Make_Implicit_Loop_Statement
(Cnode
,
1948 Statements
=> New_List
(R_Copy
, L_Inc
, Exit_Stmt
, P_Inc
));
1951 Append_To
(Stmts
, Loop_Stmt
);
1960 function H
return Node_Id
is
1962 return Make_Identifier
(Loc
, Name_uH
);
1969 function Ind_Val
(E
: Node_Id
) return Node_Id
is
1972 Make_Attribute_Reference
(Loc
,
1973 Prefix
=> New_Reference_To
(Ind_Typ
, Loc
),
1974 Attribute_Name
=> Name_Val
,
1975 Expressions
=> New_List
(E
));
1982 function Init_L
(I
: Nat
) return Node_Id
is
1986 if Is_Constrained
(Arr_Typ
) then
1987 E
:= Make_Attribute_Reference
(Loc
,
1988 Prefix
=> New_Reference_To
(Arr_Typ
, Loc
),
1989 Attribute_Name
=> Name_First
);
1995 return Make_Assignment_Statement
(Loc
, Name
=> L
, Expression
=> E
);
2002 function L
return Node_Id
is
2004 return Make_Identifier
(Loc
, Name_uL
);
2011 function L_Pos
return Node_Id
is
2012 Target_Type
: Entity_Id
;
2015 -- If the index type is an enumeration type, the computation
2016 -- can be done in standard integer. Otherwise, choose a large
2017 -- enough integer type.
2019 if Is_Enumeration_Type
(Ind_Typ
)
2020 or else Root_Type
(Ind_Typ
) = Standard_Integer
2021 or else Root_Type
(Ind_Typ
) = Standard_Short_Integer
2022 or else Root_Type
(Ind_Typ
) = Standard_Short_Short_Integer
2024 Target_Type
:= Standard_Integer
;
2026 Target_Type
:= Root_Type
(Ind_Typ
);
2030 Make_Qualified_Expression
(Loc
,
2031 Subtype_Mark
=> New_Reference_To
(Target_Type
, Loc
),
2033 Make_Attribute_Reference
(Loc
,
2034 Prefix
=> New_Reference_To
(Ind_Typ
, Loc
),
2035 Attribute_Name
=> Name_Pos
,
2036 Expressions
=> New_List
(L
)));
2043 function L_Succ
return Node_Id
is
2046 Make_Attribute_Reference
(Loc
,
2047 Prefix
=> New_Reference_To
(Ind_Typ
, Loc
),
2048 Attribute_Name
=> Name_Succ
,
2049 Expressions
=> New_List
(L
));
2056 function One
return Node_Id
is
2058 return Make_Integer_Literal
(Loc
, 1);
2065 function P
return Node_Id
is
2067 return Make_Identifier
(Loc
, Name_uP
);
2074 function P_Succ
return Node_Id
is
2077 Make_Attribute_Reference
(Loc
,
2078 Prefix
=> New_Reference_To
(Ind_Typ
, Loc
),
2079 Attribute_Name
=> Name_Succ
,
2080 Expressions
=> New_List
(P
));
2087 function R
return Node_Id
is
2089 return Make_Identifier
(Loc
, Name_uR
);
2096 function S
(I
: Nat
) return Node_Id
is
2098 return Make_Identifier
(Loc
, New_External_Name
('S', I
));
2105 function S_First
(I
: Nat
) return Node_Id
is
2107 return Make_Attribute_Reference
(Loc
,
2109 Attribute_Name
=> Name_First
);
2116 function S_Last
(I
: Nat
) return Node_Id
is
2118 return Make_Attribute_Reference
(Loc
,
2120 Attribute_Name
=> Name_Last
);
2127 function S_Length
(I
: Nat
) return Node_Id
is
2129 return Make_Attribute_Reference
(Loc
,
2131 Attribute_Name
=> Name_Length
);
2138 function S_Length_Test
(I
: Nat
) return Node_Id
is
2142 Left_Opnd
=> S_Length
(I
),
2143 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0));
2146 -- Start of processing for Expand_Concatenate_Other
2149 -- Construct the parameter specs and the overall function spec
2151 Param_Specs
:= New_List
;
2152 for I
in 1 .. Nb_Opnds
loop
2155 Make_Parameter_Specification
(Loc
,
2156 Defining_Identifier
=>
2157 Make_Defining_Identifier
(Loc
, New_External_Name
('S', I
)),
2158 Parameter_Type
=> New_Reference_To
(Base_Typ
, Loc
)));
2161 Func_Id
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('C'));
2163 Make_Function_Specification
(Loc
,
2164 Defining_Unit_Name
=> Func_Id
,
2165 Parameter_Specifications
=> Param_Specs
,
2166 Subtype_Mark
=> New_Reference_To
(Base_Typ
, Loc
));
2168 -- Construct L's object declaration
2171 Make_Object_Declaration
(Loc
,
2172 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_uL
),
2173 Object_Definition
=> New_Reference_To
(Ind_Typ
, Loc
));
2175 Func_Decls
:= New_List
(L_Decl
);
2177 -- Construct the if-then-elsif statements
2179 Elsif_List
:= New_List
;
2180 for I
in 2 .. Nb_Opnds
- 1 loop
2181 Append_To
(Elsif_List
, Make_Elsif_Part
(Loc
,
2182 Condition
=> S_Length_Test
(I
),
2183 Then_Statements
=> New_List
(Init_L
(I
))));
2187 Make_Implicit_If_Statement
(Cnode
,
2188 Condition
=> S_Length_Test
(1),
2189 Then_Statements
=> New_List
(Init_L
(1)),
2190 Elsif_Parts
=> Elsif_List
,
2191 Else_Statements
=> New_List
(Make_Return_Statement
(Loc
,
2192 Expression
=> S
(Nb_Opnds
))));
2194 -- Construct the declaration for H
2197 Make_Object_Declaration
(Loc
,
2198 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_uP
),
2199 Object_Definition
=> New_Reference_To
(Ind_Typ
, Loc
));
2201 H_Init
:= Make_Op_Subtract
(Loc
, S_Length
(1), One
);
2202 for I
in 2 .. Nb_Opnds
loop
2203 H_Init
:= Make_Op_Add
(Loc
, H_Init
, S_Length
(I
));
2205 H_Init
:= Ind_Val
(Make_Op_Add
(Loc
, H_Init
, L_Pos
));
2208 Make_Object_Declaration
(Loc
,
2209 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_uH
),
2210 Object_Definition
=> New_Reference_To
(Ind_Typ
, Loc
),
2211 Expression
=> H_Init
);
2213 -- Construct the declaration for R
2215 R_Range
:= Make_Range
(Loc
, Low_Bound
=> L
, High_Bound
=> H
);
2217 Make_Index_Or_Discriminant_Constraint
(Loc
,
2218 Constraints
=> New_List
(R_Range
));
2221 Make_Object_Declaration
(Loc
,
2222 Defining_Identifier
=> Make_Defining_Identifier
(Loc
, Name_uR
),
2223 Object_Definition
=>
2224 Make_Subtype_Indication
(Loc
,
2225 Subtype_Mark
=> New_Reference_To
(Base_Typ
, Loc
),
2226 Constraint
=> R_Constr
));
2228 -- Construct the declarations for the declare block
2230 Declare_Decls
:= New_List
(P_Decl
, H_Decl
, R_Decl
);
2232 -- Construct list of statements for the declare block
2234 Declare_Stmts
:= New_List
;
2235 for I
in 1 .. Nb_Opnds
loop
2236 Append_To
(Declare_Stmts
,
2237 Make_Implicit_If_Statement
(Cnode
,
2238 Condition
=> S_Length_Test
(I
),
2239 Then_Statements
=> Copy_Into_R_S
(I
, I
= Nb_Opnds
)));
2242 Append_To
(Declare_Stmts
, Make_Return_Statement
(Loc
, Expression
=> R
));
2244 -- Construct the declare block
2246 Declare_Block
:= Make_Block_Statement
(Loc
,
2247 Declarations
=> Declare_Decls
,
2248 Handled_Statement_Sequence
=>
2249 Make_Handled_Sequence_Of_Statements
(Loc
, Declare_Stmts
));
2251 -- Construct the list of function statements
2253 Func_Stmts
:= New_List
(If_Stmt
, Declare_Block
);
2255 -- Construct the function body
2258 Make_Subprogram_Body
(Loc
,
2259 Specification
=> Func_Spec
,
2260 Declarations
=> Func_Decls
,
2261 Handled_Statement_Sequence
=>
2262 Make_Handled_Sequence_Of_Statements
(Loc
, Func_Stmts
));
2264 -- Insert the newly generated function in the code. This is analyzed
2265 -- with all checks off, since we have completed all the checks.
2267 -- Note that this does *not* fix the array concatenation bug when the
2268 -- low bound is Integer'first sibce that bug comes from the pointer
2269 -- dereferencing an unconstrained array. An there we need a constraint
2270 -- check to make sure the length of the concatenated array is ok. ???
2272 Insert_Action
(Cnode
, Func_Body
, Suppress
=> All_Checks
);
2274 -- Construct list of arguments for the function call
2277 Operand
:= First
(Opnds
);
2278 for I
in 1 .. Nb_Opnds
loop
2279 Append_To
(Params
, Relocate_Node
(Operand
));
2283 -- Insert the function call
2287 Make_Function_Call
(Loc
, New_Reference_To
(Func_Id
, Loc
), Params
));
2289 Analyze_And_Resolve
(Cnode
, Base_Typ
);
2290 Set_Is_Inlined
(Func_Id
);
2291 end Expand_Concatenate_Other
;
2293 -------------------------------
2294 -- Expand_Concatenate_String --
2295 -------------------------------
2297 procedure Expand_Concatenate_String
(Cnode
: Node_Id
; Opnds
: List_Id
) is
2298 Loc
: constant Source_Ptr
:= Sloc
(Cnode
);
2299 Opnd1
: constant Node_Id
:= First
(Opnds
);
2300 Opnd2
: constant Node_Id
:= Next
(Opnd1
);
2301 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Opnd1
));
2302 Typ2
: constant Entity_Id
:= Base_Type
(Etype
(Opnd2
));
2305 -- RE_Id value for function to be called
2308 -- In all cases, we build a call to a routine giving the list of
2309 -- arguments as the parameter list to the routine.
2311 case List_Length
(Opnds
) is
2313 if Typ1
= Standard_Character
then
2314 if Typ2
= Standard_Character
then
2315 R
:= RE_Str_Concat_CC
;
2318 pragma Assert
(Typ2
= Standard_String
);
2319 R
:= RE_Str_Concat_CS
;
2322 elsif Typ1
= Standard_String
then
2323 if Typ2
= Standard_Character
then
2324 R
:= RE_Str_Concat_SC
;
2327 pragma Assert
(Typ2
= Standard_String
);
2331 -- If we have anything other than Standard_Character or
2332 -- Standard_String, then we must have had a serious error
2333 -- earlier, so we just abandon the attempt at expansion.
2336 pragma Assert
(Serious_Errors_Detected
> 0);
2341 R
:= RE_Str_Concat_3
;
2344 R
:= RE_Str_Concat_4
;
2347 R
:= RE_Str_Concat_5
;
2351 raise Program_Error
;
2354 -- Now generate the appropriate call
2357 Make_Function_Call
(Sloc
(Cnode
),
2358 Name
=> New_Occurrence_Of
(RTE
(R
), Loc
),
2359 Parameter_Associations
=> Opnds
));
2361 Analyze_And_Resolve
(Cnode
, Standard_String
);
2364 when RE_Not_Available
=>
2366 end Expand_Concatenate_String
;
2368 ------------------------
2369 -- Expand_N_Allocator --
2370 ------------------------
2372 procedure Expand_N_Allocator
(N
: Node_Id
) is
2373 PtrT
: constant Entity_Id
:= Etype
(N
);
2374 Dtyp
: constant Entity_Id
:= Designated_Type
(PtrT
);
2376 Loc
: constant Source_Ptr
:= Sloc
(N
);
2381 -- RM E.2.3(22). We enforce that the expected type of an allocator
2382 -- shall not be a remote access-to-class-wide-limited-private type
2384 -- Why is this being done at expansion time, seems clearly wrong ???
2386 Validate_Remote_Access_To_Class_Wide_Type
(N
);
2388 -- Set the Storage Pool
2390 Set_Storage_Pool
(N
, Associated_Storage_Pool
(Root_Type
(PtrT
)));
2392 if Present
(Storage_Pool
(N
)) then
2393 if Is_RTE
(Storage_Pool
(N
), RE_SS_Pool
) then
2395 Set_Procedure_To_Call
(N
, RTE
(RE_SS_Allocate
));
2398 elsif Is_Class_Wide_Type
(Etype
(Storage_Pool
(N
))) then
2399 Set_Procedure_To_Call
(N
, RTE
(RE_Allocate_Any
));
2402 Set_Procedure_To_Call
(N
,
2403 Find_Prim_Op
(Etype
(Storage_Pool
(N
)), Name_Allocate
));
2407 -- Under certain circumstances we can replace an allocator by an
2408 -- access to statically allocated storage. The conditions, as noted
2409 -- in AARM 3.10 (10c) are as follows:
2411 -- Size and initial value is known at compile time
2412 -- Access type is access-to-constant
2414 -- The allocator is not part of a constraint on a record component,
2415 -- because in that case the inserted actions are delayed until the
2416 -- record declaration is fully analyzed, which is too late for the
2417 -- analysis of the rewritten allocator.
2419 if Is_Access_Constant
(PtrT
)
2420 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
2421 and then Compile_Time_Known_Value
(Expression
(Expression
(N
)))
2422 and then Size_Known_At_Compile_Time
(Etype
(Expression
2424 and then not Is_Record_Type
(Current_Scope
)
2426 -- Here we can do the optimization. For the allocator
2430 -- We insert an object declaration
2432 -- Tnn : aliased x := y;
2434 -- and replace the allocator by Tnn'Unrestricted_Access.
2435 -- Tnn is marked as requiring static allocation.
2438 Make_Defining_Identifier
(Loc
, New_Internal_Name
('T'));
2440 Desig
:= Subtype_Mark
(Expression
(N
));
2442 -- If context is constrained, use constrained subtype directly,
2443 -- so that the constant is not labelled as having a nomimally
2444 -- unconstrained subtype.
2446 if Entity
(Desig
) = Base_Type
(Dtyp
) then
2447 Desig
:= New_Occurrence_Of
(Dtyp
, Loc
);
2451 Make_Object_Declaration
(Loc
,
2452 Defining_Identifier
=> Temp
,
2453 Aliased_Present
=> True,
2454 Constant_Present
=> Is_Access_Constant
(PtrT
),
2455 Object_Definition
=> Desig
,
2456 Expression
=> Expression
(Expression
(N
))));
2459 Make_Attribute_Reference
(Loc
,
2460 Prefix
=> New_Occurrence_Of
(Temp
, Loc
),
2461 Attribute_Name
=> Name_Unrestricted_Access
));
2463 Analyze_And_Resolve
(N
, PtrT
);
2465 -- We set the variable as statically allocated, since we don't
2466 -- want it going on the stack of the current procedure!
2468 Set_Is_Statically_Allocated
(Temp
);
2472 -- Handle case of qualified expression (other than optimization above)
2474 if Nkind
(Expression
(N
)) = N_Qualified_Expression
then
2475 Expand_Allocator_Expression
(N
);
2477 -- If the allocator is for a type which requires initialization, and
2478 -- there is no initial value (i.e. operand is a subtype indication
2479 -- rather than a qualifed expression), then we must generate a call
2480 -- to the initialization routine. This is done using an expression
2483 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
2485 -- Here ptr_T is the pointer type for the allocator, and T is the
2486 -- subtype of the allocator. A special case arises if the designated
2487 -- type of the access type is a task or contains tasks. In this case
2488 -- the call to Init (Temp.all ...) is replaced by code that ensures
2489 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
2490 -- for details). In addition, if the type T is a task T, then the
2491 -- first argument to Init must be converted to the task record type.
2495 T
: constant Entity_Id
:= Entity
(Expression
(N
));
2503 Temp_Decl
: Node_Id
;
2504 Temp_Type
: Entity_Id
;
2505 Attach_Level
: Uint
;
2508 if No_Initialization
(N
) then
2511 -- Case of no initialization procedure present
2513 elsif not Has_Non_Null_Base_Init_Proc
(T
) then
2515 -- Case of simple initialization required
2517 if Needs_Simple_Initialization
(T
) then
2518 Rewrite
(Expression
(N
),
2519 Make_Qualified_Expression
(Loc
,
2520 Subtype_Mark
=> New_Occurrence_Of
(T
, Loc
),
2521 Expression
=> Get_Simple_Init_Val
(T
, Loc
)));
2523 Analyze_And_Resolve
(Expression
(Expression
(N
)), T
);
2524 Analyze_And_Resolve
(Expression
(N
), T
);
2525 Set_Paren_Count
(Expression
(Expression
(N
)), 1);
2526 Expand_N_Allocator
(N
);
2528 -- No initialization required
2534 -- Case of initialization procedure present, must be called
2537 Init
:= Base_Init_Proc
(T
);
2540 Make_Defining_Identifier
(Loc
, New_Internal_Name
('P'));
2542 -- Construct argument list for the initialization routine call
2543 -- The CPP constructor needs the address directly
2545 if Is_CPP_Class
(T
) then
2546 Arg1
:= New_Reference_To
(Temp
, Loc
);
2551 Make_Explicit_Dereference
(Loc
,
2552 Prefix
=> New_Reference_To
(Temp
, Loc
));
2553 Set_Assignment_OK
(Arg1
);
2556 -- The initialization procedure expects a specific type.
2557 -- if the context is access to class wide, indicate that
2558 -- the object being allocated has the right specific type.
2560 if Is_Class_Wide_Type
(Dtyp
) then
2561 Arg1
:= Unchecked_Convert_To
(T
, Arg1
);
2565 -- If designated type is a concurrent type or if it is a
2566 -- private type whose definition is a concurrent type,
2567 -- the first argument in the Init routine has to be
2568 -- unchecked conversion to the corresponding record type.
2569 -- If the designated type is a derived type, we also
2570 -- convert the argument to its root type.
2572 if Is_Concurrent_Type
(T
) then
2574 Unchecked_Convert_To
(Corresponding_Record_Type
(T
), Arg1
);
2576 elsif Is_Private_Type
(T
)
2577 and then Present
(Full_View
(T
))
2578 and then Is_Concurrent_Type
(Full_View
(T
))
2581 Unchecked_Convert_To
2582 (Corresponding_Record_Type
(Full_View
(T
)), Arg1
);
2584 elsif Etype
(First_Formal
(Init
)) /= Base_Type
(T
) then
2587 Ftyp
: constant Entity_Id
:= Etype
(First_Formal
(Init
));
2590 Arg1
:= OK_Convert_To
(Etype
(Ftyp
), Arg1
);
2591 Set_Etype
(Arg1
, Ftyp
);
2595 Args
:= New_List
(Arg1
);
2597 -- For the task case, pass the Master_Id of the access type
2598 -- as the value of the _Master parameter, and _Chain as the
2599 -- value of the _Chain parameter (_Chain will be defined as
2600 -- part of the generated code for the allocator).
2602 if Has_Task
(T
) then
2603 if No
(Master_Id
(Base_Type
(PtrT
))) then
2605 -- The designated type was an incomplete type, and
2606 -- the access type did not get expanded. Salvage
2609 Expand_N_Full_Type_Declaration
2610 (Parent
(Base_Type
(PtrT
)));
2613 -- If the context of the allocator is a declaration or
2614 -- an assignment, we can generate a meaningful image for
2615 -- it, even though subsequent assignments might remove
2616 -- the connection between task and entity. We build this
2617 -- image when the left-hand side is a simple variable,
2618 -- a simple indexed assignment or a simple selected
2621 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
2623 Nam
: constant Node_Id
:= Name
(Parent
(N
));
2626 if Is_Entity_Name
(Nam
) then
2628 Build_Task_Image_Decls
(
2631 (Entity
(Nam
), Sloc
(Nam
)), T
);
2633 elsif (Nkind
(Nam
) = N_Indexed_Component
2634 or else Nkind
(Nam
) = N_Selected_Component
)
2635 and then Is_Entity_Name
(Prefix
(Nam
))
2638 Build_Task_Image_Decls
2639 (Loc
, Nam
, Etype
(Prefix
(Nam
)));
2641 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
2645 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
2647 Build_Task_Image_Decls
(
2648 Loc
, Defining_Identifier
(Parent
(N
)), T
);
2651 Decls
:= Build_Task_Image_Decls
(Loc
, T
, T
);
2656 (Master_Id
(Base_Type
(Root_Type
(PtrT
))), Loc
));
2657 Append_To
(Args
, Make_Identifier
(Loc
, Name_uChain
));
2659 Decl
:= Last
(Decls
);
2661 New_Occurrence_Of
(Defining_Identifier
(Decl
), Loc
));
2663 -- Has_Task is false, Decls not used
2669 -- Add discriminants if discriminated type
2671 if Has_Discriminants
(T
) then
2672 Discr
:= First_Elmt
(Discriminant_Constraint
(T
));
2674 while Present
(Discr
) loop
2675 Append
(New_Copy_Tree
(Elists
.Node
(Discr
)), Args
);
2679 elsif Is_Private_Type
(T
)
2680 and then Present
(Full_View
(T
))
2681 and then Has_Discriminants
(Full_View
(T
))
2684 First_Elmt
(Discriminant_Constraint
(Full_View
(T
)));
2686 while Present
(Discr
) loop
2687 Append
(New_Copy_Tree
(Elists
.Node
(Discr
)), Args
);
2692 -- We set the allocator as analyzed so that when we analyze the
2693 -- expression actions node, we do not get an unwanted recursive
2694 -- expansion of the allocator expression.
2696 Set_Analyzed
(N
, True);
2697 Node
:= Relocate_Node
(N
);
2699 -- Here is the transformation:
2701 -- output: Temp : constant ptr_T := new T;
2702 -- Init (Temp.all, ...);
2703 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
2704 -- <CTRL> Initialize (Finalizable (Temp.all));
2706 -- Here ptr_T is the pointer type for the allocator, and T
2707 -- is the subtype of the allocator.
2710 Make_Object_Declaration
(Loc
,
2711 Defining_Identifier
=> Temp
,
2712 Constant_Present
=> True,
2713 Object_Definition
=> New_Reference_To
(Temp_Type
, Loc
),
2714 Expression
=> Node
);
2716 Set_Assignment_OK
(Temp_Decl
);
2718 if Is_CPP_Class
(T
) then
2719 Set_Aliased_Present
(Temp_Decl
);
2722 Insert_Action
(N
, Temp_Decl
, Suppress
=> All_Checks
);
2724 -- If the designated type is task type or contains tasks,
2725 -- Create block to activate created tasks, and insert
2726 -- declaration for Task_Image variable ahead of call.
2728 if Has_Task
(T
) then
2730 L
: constant List_Id
:= New_List
;
2734 Build_Task_Allocate_Block
(L
, Node
, Args
);
2737 Insert_List_Before
(First
(Declarations
(Blk
)), Decls
);
2738 Insert_Actions
(N
, L
);
2743 Make_Procedure_Call_Statement
(Loc
,
2744 Name
=> New_Reference_To
(Init
, Loc
),
2745 Parameter_Associations
=> Args
));
2748 if Controlled_Type
(T
) then
2749 Flist
:= Get_Allocator_Final_List
(N
, Base_Type
(T
), PtrT
);
2750 if Ekind
(PtrT
) = E_Anonymous_Access_Type
then
2751 Attach_Level
:= Uint_1
;
2753 Attach_Level
:= Uint_2
;
2757 Ref
=> New_Copy_Tree
(Arg1
),
2760 With_Attach
=> Make_Integer_Literal
(Loc
,
2764 if Is_CPP_Class
(T
) then
2766 Make_Attribute_Reference
(Loc
,
2767 Prefix
=> New_Reference_To
(Temp
, Loc
),
2768 Attribute_Name
=> Name_Unchecked_Access
));
2770 Rewrite
(N
, New_Reference_To
(Temp
, Loc
));
2773 Analyze_And_Resolve
(N
, PtrT
);
2779 when RE_Not_Available
=>
2781 end Expand_N_Allocator
;
2783 -----------------------
2784 -- Expand_N_And_Then --
2785 -----------------------
2787 -- Expand into conditional expression if Actions present, and also
2788 -- deal with optimizing case of arguments being True or False.
2790 procedure Expand_N_And_Then
(N
: Node_Id
) is
2791 Loc
: constant Source_Ptr
:= Sloc
(N
);
2792 Typ
: constant Entity_Id
:= Etype
(N
);
2793 Left
: constant Node_Id
:= Left_Opnd
(N
);
2794 Right
: constant Node_Id
:= Right_Opnd
(N
);
2798 -- Deal with non-standard booleans
2800 if Is_Boolean_Type
(Typ
) then
2801 Adjust_Condition
(Left
);
2802 Adjust_Condition
(Right
);
2803 Set_Etype
(N
, Standard_Boolean
);
2806 -- Check for cases of left argument is True or False
2808 if Nkind
(Left
) = N_Identifier
then
2810 -- If left argument is True, change (True and then Right) to Right.
2811 -- Any actions associated with Right will be executed unconditionally
2812 -- and can thus be inserted into the tree unconditionally.
2814 if Entity
(Left
) = Standard_True
then
2815 if Present
(Actions
(N
)) then
2816 Insert_Actions
(N
, Actions
(N
));
2820 Adjust_Result_Type
(N
, Typ
);
2823 -- If left argument is False, change (False and then Right) to
2824 -- False. In this case we can forget the actions associated with
2825 -- Right, since they will never be executed.
2827 elsif Entity
(Left
) = Standard_False
then
2828 Kill_Dead_Code
(Right
);
2829 Kill_Dead_Code
(Actions
(N
));
2830 Rewrite
(N
, New_Occurrence_Of
(Standard_False
, Loc
));
2831 Adjust_Result_Type
(N
, Typ
);
2836 -- If Actions are present, we expand
2838 -- left and then right
2842 -- if left then right else false end
2844 -- with the actions becoming the Then_Actions of the conditional
2845 -- expression. This conditional expression is then further expanded
2846 -- (and will eventually disappear)
2848 if Present
(Actions
(N
)) then
2849 Actlist
:= Actions
(N
);
2851 Make_Conditional_Expression
(Loc
,
2852 Expressions
=> New_List
(
2855 New_Occurrence_Of
(Standard_False
, Loc
))));
2857 Set_Then_Actions
(N
, Actlist
);
2858 Analyze_And_Resolve
(N
, Standard_Boolean
);
2859 Adjust_Result_Type
(N
, Typ
);
2863 -- No actions present, check for cases of right argument True/False
2865 if Nkind
(Right
) = N_Identifier
then
2867 -- Change (Left and then True) to Left. Note that we know there
2868 -- are no actions associated with the True operand, since we
2869 -- just checked for this case above.
2871 if Entity
(Right
) = Standard_True
then
2874 -- Change (Left and then False) to False, making sure to preserve
2875 -- any side effects associated with the Left operand.
2877 elsif Entity
(Right
) = Standard_False
then
2878 Remove_Side_Effects
(Left
);
2880 (N
, New_Occurrence_Of
(Standard_False
, Loc
));
2884 Adjust_Result_Type
(N
, Typ
);
2885 end Expand_N_And_Then
;
2887 -------------------------------------
2888 -- Expand_N_Conditional_Expression --
2889 -------------------------------------
2891 -- Expand into expression actions if then/else actions present
2893 procedure Expand_N_Conditional_Expression
(N
: Node_Id
) is
2894 Loc
: constant Source_Ptr
:= Sloc
(N
);
2895 Cond
: constant Node_Id
:= First
(Expressions
(N
));
2896 Thenx
: constant Node_Id
:= Next
(Cond
);
2897 Elsex
: constant Node_Id
:= Next
(Thenx
);
2898 Typ
: constant Entity_Id
:= Etype
(N
);
2903 -- If either then or else actions are present, then given:
2905 -- if cond then then-expr else else-expr end
2907 -- we insert the following sequence of actions (using Insert_Actions):
2912 -- Cnn := then-expr;
2918 -- and replace the conditional expression by a reference to Cnn
2920 if Present
(Then_Actions
(N
)) or else Present
(Else_Actions
(N
)) then
2921 Cnn
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('C'));
2924 Make_Implicit_If_Statement
(N
,
2925 Condition
=> Relocate_Node
(Cond
),
2927 Then_Statements
=> New_List
(
2928 Make_Assignment_Statement
(Sloc
(Thenx
),
2929 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Thenx
)),
2930 Expression
=> Relocate_Node
(Thenx
))),
2932 Else_Statements
=> New_List
(
2933 Make_Assignment_Statement
(Sloc
(Elsex
),
2934 Name
=> New_Occurrence_Of
(Cnn
, Sloc
(Elsex
)),
2935 Expression
=> Relocate_Node
(Elsex
))));
2937 Set_Assignment_OK
(Name
(First
(Then_Statements
(New_If
))));
2938 Set_Assignment_OK
(Name
(First
(Else_Statements
(New_If
))));
2940 if Present
(Then_Actions
(N
)) then
2942 (First
(Then_Statements
(New_If
)), Then_Actions
(N
));
2945 if Present
(Else_Actions
(N
)) then
2947 (First
(Else_Statements
(New_If
)), Else_Actions
(N
));
2950 Rewrite
(N
, New_Occurrence_Of
(Cnn
, Loc
));
2953 Make_Object_Declaration
(Loc
,
2954 Defining_Identifier
=> Cnn
,
2955 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
)));
2957 Insert_Action
(N
, New_If
);
2958 Analyze_And_Resolve
(N
, Typ
);
2960 end Expand_N_Conditional_Expression
;
2962 -----------------------------------
2963 -- Expand_N_Explicit_Dereference --
2964 -----------------------------------
2966 procedure Expand_N_Explicit_Dereference
(N
: Node_Id
) is
2968 -- The only processing required is an insertion of an explicit
2969 -- dereference call for the checked storage pool case.
2971 Insert_Dereference_Action
(Prefix
(N
));
2972 end Expand_N_Explicit_Dereference
;
2978 procedure Expand_N_In
(N
: Node_Id
) is
2979 Loc
: constant Source_Ptr
:= Sloc
(N
);
2980 Rtyp
: constant Entity_Id
:= Etype
(N
);
2981 Lop
: constant Node_Id
:= Left_Opnd
(N
);
2982 Rop
: constant Node_Id
:= Right_Opnd
(N
);
2983 Static
: constant Boolean := Is_OK_Static_Expression
(N
);
2986 -- If we have an explicit range, do a bit of optimization based
2987 -- on range analysis (we may be able to kill one or both checks).
2989 if Nkind
(Rop
) = N_Range
then
2991 Lcheck
: constant Compare_Result
:=
2992 Compile_Time_Compare
(Lop
, Low_Bound
(Rop
));
2993 Ucheck
: constant Compare_Result
:=
2994 Compile_Time_Compare
(Lop
, High_Bound
(Rop
));
2997 -- If either check is known to fail, replace result
2998 -- by False, since the other check does not matter.
2999 -- Preserve the static flag for legality checks, because
3000 -- we are constant-folding beyond RM 4.9.
3002 if Lcheck
= LT
or else Ucheck
= GT
then
3004 New_Reference_To
(Standard_False
, Loc
));
3005 Analyze_And_Resolve
(N
, Rtyp
);
3006 Set_Is_Static_Expression
(N
, Static
);
3009 -- If both checks are known to succeed, replace result
3010 -- by True, since we know we are in range.
3012 elsif Lcheck
in Compare_GE
and then Ucheck
in Compare_LE
then
3014 New_Reference_To
(Standard_True
, Loc
));
3015 Analyze_And_Resolve
(N
, Rtyp
);
3016 Set_Is_Static_Expression
(N
, Static
);
3019 -- If lower bound check succeeds and upper bound check is
3020 -- not known to succeed or fail, then replace the range check
3021 -- with a comparison against the upper bound.
3023 elsif Lcheck
in Compare_GE
then
3027 Right_Opnd
=> High_Bound
(Rop
)));
3028 Analyze_And_Resolve
(N
, Rtyp
);
3031 -- If upper bound check succeeds and lower bound check is
3032 -- not known to succeed or fail, then replace the range check
3033 -- with a comparison against the lower bound.
3035 elsif Ucheck
in Compare_LE
then
3039 Right_Opnd
=> Low_Bound
(Rop
)));
3040 Analyze_And_Resolve
(N
, Rtyp
);
3045 -- For all other cases of an explicit range, nothing to be done
3049 -- Here right operand is a subtype mark
3053 Typ
: Entity_Id
:= Etype
(Rop
);
3054 Is_Acc
: constant Boolean := Is_Access_Type
(Typ
);
3055 Obj
: Node_Id
:= Lop
;
3056 Cond
: Node_Id
:= Empty
;
3059 Remove_Side_Effects
(Obj
);
3061 -- For tagged type, do tagged membership operation
3063 if Is_Tagged_Type
(Typ
) then
3065 -- No expansion will be performed when Java_VM, as the
3066 -- JVM back end will handle the membership tests directly
3067 -- (tags are not explicitly represented in Java objects,
3068 -- so the normal tagged membership expansion is not what
3072 Rewrite
(N
, Tagged_Membership
(N
));
3073 Analyze_And_Resolve
(N
, Rtyp
);
3078 -- If type is scalar type, rewrite as x in t'first .. t'last
3079 -- This reason we do this is that the bounds may have the wrong
3080 -- type if they come from the original type definition.
3082 elsif Is_Scalar_Type
(Typ
) then
3086 Make_Attribute_Reference
(Loc
,
3087 Attribute_Name
=> Name_First
,
3088 Prefix
=> New_Reference_To
(Typ
, Loc
)),
3091 Make_Attribute_Reference
(Loc
,
3092 Attribute_Name
=> Name_Last
,
3093 Prefix
=> New_Reference_To
(Typ
, Loc
))));
3094 Analyze_And_Resolve
(N
, Rtyp
);
3097 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
3098 -- a membership test if the subtype mark denotes a constrained
3099 -- Unchecked_Union subtype and the expression lacks inferable
3102 elsif Is_Unchecked_Union
(Base_Type
(Typ
))
3103 and then Is_Constrained
(Typ
)
3104 and then not Has_Inferable_Discriminants
(Lop
)
3107 Make_Raise_Program_Error
(Loc
,
3108 Reason
=> PE_Unchecked_Union_Restriction
));
3110 -- Prevent Gigi from generating incorrect code by rewriting
3111 -- the test as a standard False.
3114 New_Occurrence_Of
(Standard_False
, Loc
));
3119 -- Here we have a non-scalar type
3122 Typ
:= Designated_Type
(Typ
);
3125 if not Is_Constrained
(Typ
) then
3127 New_Reference_To
(Standard_True
, Loc
));
3128 Analyze_And_Resolve
(N
, Rtyp
);
3130 -- For the constrained array case, we have to check the
3131 -- subscripts for an exact match if the lengths are
3132 -- non-zero (the lengths must match in any case).
3134 elsif Is_Array_Type
(Typ
) then
3136 Check_Subscripts
: declare
3137 function Construct_Attribute_Reference
3140 Dim
: Nat
) return Node_Id
;
3141 -- Build attribute reference E'Nam(Dim)
3143 -----------------------------------
3144 -- Construct_Attribute_Reference --
3145 -----------------------------------
3147 function Construct_Attribute_Reference
3150 Dim
: Nat
) return Node_Id
3154 Make_Attribute_Reference
(Loc
,
3156 Attribute_Name
=> Nam
,
3157 Expressions
=> New_List
(
3158 Make_Integer_Literal
(Loc
, Dim
)));
3159 end Construct_Attribute_Reference
;
3161 -- Start processing for Check_Subscripts
3164 for J
in 1 .. Number_Dimensions
(Typ
) loop
3165 Evolve_And_Then
(Cond
,
3168 Construct_Attribute_Reference
3169 (Duplicate_Subexpr_No_Checks
(Obj
),
3172 Construct_Attribute_Reference
3173 (New_Occurrence_Of
(Typ
, Loc
), Name_First
, J
)));
3175 Evolve_And_Then
(Cond
,
3178 Construct_Attribute_Reference
3179 (Duplicate_Subexpr_No_Checks
(Obj
),
3182 Construct_Attribute_Reference
3183 (New_Occurrence_Of
(Typ
, Loc
), Name_Last
, J
)));
3192 Right_Opnd
=> Make_Null
(Loc
)),
3193 Right_Opnd
=> Cond
);
3197 Analyze_And_Resolve
(N
, Rtyp
);
3198 end Check_Subscripts
;
3200 -- These are the cases where constraint checks may be
3201 -- required, e.g. records with possible discriminants
3204 -- Expand the test into a series of discriminant comparisons.
3205 -- The expression that is built is the negation of the one
3206 -- that is used for checking discriminant constraints.
3208 Obj
:= Relocate_Node
(Left_Opnd
(N
));
3210 if Has_Discriminants
(Typ
) then
3211 Cond
:= Make_Op_Not
(Loc
,
3212 Right_Opnd
=> Build_Discriminant_Checks
(Obj
, Typ
));
3215 Cond
:= Make_Or_Else
(Loc
,
3219 Right_Opnd
=> Make_Null
(Loc
)),
3220 Right_Opnd
=> Cond
);
3224 Cond
:= New_Occurrence_Of
(Standard_True
, Loc
);
3228 Analyze_And_Resolve
(N
, Rtyp
);
3234 --------------------------------
3235 -- Expand_N_Indexed_Component --
3236 --------------------------------
3238 procedure Expand_N_Indexed_Component
(N
: Node_Id
) is
3239 Loc
: constant Source_Ptr
:= Sloc
(N
);
3240 Typ
: constant Entity_Id
:= Etype
(N
);
3241 P
: constant Node_Id
:= Prefix
(N
);
3242 T
: constant Entity_Id
:= Etype
(P
);
3245 -- A special optimization, if we have an indexed component that
3246 -- is selecting from a slice, then we can eliminate the slice,
3247 -- since, for example, x (i .. j)(k) is identical to x(k). The
3248 -- only difference is the range check required by the slice. The
3249 -- range check for the slice itself has already been generated.
3250 -- The range check for the subscripting operation is ensured
3251 -- by converting the subject to the subtype of the slice.
3253 -- This optimization not only generates better code, avoiding
3254 -- slice messing especially in the packed case, but more importantly
3255 -- bypasses some problems in handling this peculiar case, for
3256 -- example, the issue of dealing specially with object renamings.
3258 if Nkind
(P
) = N_Slice
then
3260 Make_Indexed_Component
(Loc
,
3261 Prefix
=> Prefix
(P
),
3262 Expressions
=> New_List
(
3264 (Etype
(First_Index
(Etype
(P
))),
3265 First
(Expressions
(N
))))));
3266 Analyze_And_Resolve
(N
, Typ
);
3270 -- If the prefix is an access type, then we unconditionally rewrite
3271 -- if as an explicit deference. This simplifies processing for several
3272 -- cases, including packed array cases and certain cases in which
3273 -- checks must be generated. We used to try to do this only when it
3274 -- was necessary, but it cleans up the code to do it all the time.
3276 if Is_Access_Type
(T
) then
3277 Insert_Explicit_Dereference
(P
);
3278 Analyze_And_Resolve
(P
, Designated_Type
(T
));
3281 -- Generate index and validity checks
3283 Generate_Index_Checks
(N
);
3285 if Validity_Checks_On
and then Validity_Check_Subscripts
then
3286 Apply_Subscript_Validity_Checks
(N
);
3289 -- All done for the non-packed case
3291 if not Is_Packed
(Etype
(Prefix
(N
))) then
3295 -- For packed arrays that are not bit-packed (i.e. the case of an array
3296 -- with one or more index types with a non-coniguous enumeration type),
3297 -- we can always use the normal packed element get circuit.
3299 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
3300 Expand_Packed_Element_Reference
(N
);
3304 -- For a reference to a component of a bit packed array, we have to
3305 -- convert it to a reference to the corresponding Packed_Array_Type.
3306 -- We only want to do this for simple references, and not for:
3308 -- Left side of assignment, or prefix of left side of assignment,
3309 -- or prefix of the prefix, to handle packed arrays of packed arrays,
3310 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
3312 -- Renaming objects in renaming associations
3313 -- This case is handled when a use of the renamed variable occurs
3315 -- Actual parameters for a procedure call
3316 -- This case is handled in Exp_Ch6.Expand_Actuals
3318 -- The second expression in a 'Read attribute reference
3320 -- The prefix of an address or size attribute reference
3322 -- The following circuit detects these exceptions
3325 Child
: Node_Id
:= N
;
3326 Parnt
: Node_Id
:= Parent
(N
);
3330 if Nkind
(Parnt
) = N_Unchecked_Expression
then
3333 elsif Nkind
(Parnt
) = N_Object_Renaming_Declaration
3334 or else Nkind
(Parnt
) = N_Procedure_Call_Statement
3335 or else (Nkind
(Parnt
) = N_Parameter_Association
3337 Nkind
(Parent
(Parnt
)) = N_Procedure_Call_Statement
)
3341 elsif Nkind
(Parnt
) = N_Attribute_Reference
3342 and then (Attribute_Name
(Parnt
) = Name_Address
3344 Attribute_Name
(Parnt
) = Name_Size
)
3345 and then Prefix
(Parnt
) = Child
3349 elsif Nkind
(Parnt
) = N_Assignment_Statement
3350 and then Name
(Parnt
) = Child
3354 -- If the expression is an index of an indexed component,
3355 -- it must be expanded regardless of context.
3357 elsif Nkind
(Parnt
) = N_Indexed_Component
3358 and then Child
/= Prefix
(Parnt
)
3360 Expand_Packed_Element_Reference
(N
);
3363 elsif Nkind
(Parent
(Parnt
)) = N_Assignment_Statement
3364 and then Name
(Parent
(Parnt
)) = Parnt
3368 elsif Nkind
(Parnt
) = N_Attribute_Reference
3369 and then Attribute_Name
(Parnt
) = Name_Read
3370 and then Next
(First
(Expressions
(Parnt
))) = Child
3374 elsif (Nkind
(Parnt
) = N_Indexed_Component
3375 or else Nkind
(Parnt
) = N_Selected_Component
)
3376 and then Prefix
(Parnt
) = Child
3381 Expand_Packed_Element_Reference
(N
);
3385 -- Keep looking up tree for unchecked expression, or if we are
3386 -- the prefix of a possible assignment left side.
3389 Parnt
:= Parent
(Child
);
3393 end Expand_N_Indexed_Component
;
3395 ---------------------
3396 -- Expand_N_Not_In --
3397 ---------------------
3399 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
3400 -- can be done. This avoids needing to duplicate this expansion code.
3402 procedure Expand_N_Not_In
(N
: Node_Id
) is
3403 Loc
: constant Source_Ptr
:= Sloc
(N
);
3404 Typ
: constant Entity_Id
:= Etype
(N
);
3411 Left_Opnd
=> Left_Opnd
(N
),
3412 Right_Opnd
=> Right_Opnd
(N
))));
3413 Analyze_And_Resolve
(N
, Typ
);
3414 end Expand_N_Not_In
;
3420 -- The only replacement required is for the case of a null of type
3421 -- that is an access to protected subprogram. We represent such
3422 -- access values as a record, and so we must replace the occurrence
3423 -- of null by the equivalent record (with a null address and a null
3424 -- pointer in it), so that the backend creates the proper value.
3426 procedure Expand_N_Null
(N
: Node_Id
) is
3427 Loc
: constant Source_Ptr
:= Sloc
(N
);
3428 Typ
: constant Entity_Id
:= Etype
(N
);
3432 if Ekind
(Typ
) = E_Access_Protected_Subprogram_Type
then
3434 Make_Aggregate
(Loc
,
3435 Expressions
=> New_List
(
3436 New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
),
3440 Analyze_And_Resolve
(N
, Equivalent_Type
(Typ
));
3442 -- For subsequent semantic analysis, the node must retain its
3443 -- type. Gigi in any case replaces this type by the corresponding
3444 -- record type before processing the node.
3450 when RE_Not_Available
=>
3454 ---------------------
3455 -- Expand_N_Op_Abs --
3456 ---------------------
3458 procedure Expand_N_Op_Abs
(N
: Node_Id
) is
3459 Loc
: constant Source_Ptr
:= Sloc
(N
);
3460 Expr
: constant Node_Id
:= Right_Opnd
(N
);
3463 Unary_Op_Validity_Checks
(N
);
3465 -- Deal with software overflow checking
3467 if not Backend_Overflow_Checks_On_Target
3468 and then Is_Signed_Integer_Type
(Etype
(N
))
3469 and then Do_Overflow_Check
(N
)
3471 -- The only case to worry about is when the argument is
3472 -- equal to the largest negative number, so what we do is
3473 -- to insert the check:
3475 -- [constraint_error when Expr = typ'Base'First]
3477 -- with the usual Duplicate_Subexpr use coding for expr
3480 Make_Raise_Constraint_Error
(Loc
,
3483 Left_Opnd
=> Duplicate_Subexpr
(Expr
),
3485 Make_Attribute_Reference
(Loc
,
3487 New_Occurrence_Of
(Base_Type
(Etype
(Expr
)), Loc
),
3488 Attribute_Name
=> Name_First
)),
3489 Reason
=> CE_Overflow_Check_Failed
));
3492 -- Vax floating-point types case
3494 if Vax_Float
(Etype
(N
)) then
3495 Expand_Vax_Arith
(N
);
3497 end Expand_N_Op_Abs
;
3499 ---------------------
3500 -- Expand_N_Op_Add --
3501 ---------------------
3503 procedure Expand_N_Op_Add
(N
: Node_Id
) is
3504 Typ
: constant Entity_Id
:= Etype
(N
);
3507 Binary_Op_Validity_Checks
(N
);
3509 -- N + 0 = 0 + N = N for integer types
3511 if Is_Integer_Type
(Typ
) then
3512 if Compile_Time_Known_Value
(Right_Opnd
(N
))
3513 and then Expr_Value
(Right_Opnd
(N
)) = Uint_0
3515 Rewrite
(N
, Left_Opnd
(N
));
3518 elsif Compile_Time_Known_Value
(Left_Opnd
(N
))
3519 and then Expr_Value
(Left_Opnd
(N
)) = Uint_0
3521 Rewrite
(N
, Right_Opnd
(N
));
3526 -- Arithmetic overflow checks for signed integer/fixed point types
3528 if Is_Signed_Integer_Type
(Typ
)
3529 or else Is_Fixed_Point_Type
(Typ
)
3531 Apply_Arithmetic_Overflow_Check
(N
);
3534 -- Vax floating-point types case
3536 elsif Vax_Float
(Typ
) then
3537 Expand_Vax_Arith
(N
);
3539 end Expand_N_Op_Add
;
3541 ---------------------
3542 -- Expand_N_Op_And --
3543 ---------------------
3545 procedure Expand_N_Op_And
(N
: Node_Id
) is
3546 Typ
: constant Entity_Id
:= Etype
(N
);
3549 Binary_Op_Validity_Checks
(N
);
3551 if Is_Array_Type
(Etype
(N
)) then
3552 Expand_Boolean_Operator
(N
);
3554 elsif Is_Boolean_Type
(Etype
(N
)) then
3555 Adjust_Condition
(Left_Opnd
(N
));
3556 Adjust_Condition
(Right_Opnd
(N
));
3557 Set_Etype
(N
, Standard_Boolean
);
3558 Adjust_Result_Type
(N
, Typ
);
3560 end Expand_N_Op_And
;
3562 ------------------------
3563 -- Expand_N_Op_Concat --
3564 ------------------------
3566 Max_Available_String_Operands
: Int
:= -1;
3567 -- This is initialized the first time this routine is called. It records
3568 -- a value of 0,2,3,4,5 depending on what Str_Concat_n procedures are
3569 -- available in the run-time:
3572 -- 2 RE_Str_Concat available, RE_Str_Concat_3 not available
3573 -- 3 RE_Str_Concat/Concat_2 available, RE_Str_Concat_4 not available
3574 -- 4 RE_Str_Concat/Concat_2/3 available, RE_Str_Concat_5 not available
3575 -- 5 All routines including RE_Str_Concat_5 available
3577 Char_Concat_Available
: Boolean;
3578 -- Records if the routines RE_Str_Concat_CC/CS/SC are available. True if
3579 -- all three are available, False if any one of these is unavailable.
3581 procedure Expand_N_Op_Concat
(N
: Node_Id
) is
3583 -- List of operands to be concatenated
3586 -- Single operand for concatenation
3589 -- Node which is to be replaced by the result of concatenating
3590 -- the nodes in the list Opnds.
3593 -- Array type of concatenation result type
3596 -- Component type of concatenation represented by Cnode
3599 -- Initialize global variables showing run-time status
3601 if Max_Available_String_Operands
< 1 then
3602 if not RTE_Available
(RE_Str_Concat
) then
3603 Max_Available_String_Operands
:= 0;
3604 elsif not RTE_Available
(RE_Str_Concat_3
) then
3605 Max_Available_String_Operands
:= 2;
3606 elsif not RTE_Available
(RE_Str_Concat_4
) then
3607 Max_Available_String_Operands
:= 3;
3608 elsif not RTE_Available
(RE_Str_Concat_5
) then
3609 Max_Available_String_Operands
:= 4;
3611 Max_Available_String_Operands
:= 5;
3614 Char_Concat_Available
:=
3615 RTE_Available
(RE_Str_Concat_CC
)
3617 RTE_Available
(RE_Str_Concat_CS
)
3619 RTE_Available
(RE_Str_Concat_SC
);
3622 -- Ensure validity of both operands
3624 Binary_Op_Validity_Checks
(N
);
3626 -- If we are the left operand of a concatenation higher up the
3627 -- tree, then do nothing for now, since we want to deal with a
3628 -- series of concatenations as a unit.
3630 if Nkind
(Parent
(N
)) = N_Op_Concat
3631 and then N
= Left_Opnd
(Parent
(N
))
3636 -- We get here with a concatenation whose left operand may be a
3637 -- concatenation itself with a consistent type. We need to process
3638 -- these concatenation operands from left to right, which means
3639 -- from the deepest node in the tree to the highest node.
3642 while Nkind
(Left_Opnd
(Cnode
)) = N_Op_Concat
loop
3643 Cnode
:= Left_Opnd
(Cnode
);
3646 -- Now Opnd is the deepest Opnd, and its parents are the concatenation
3647 -- nodes above, so now we process bottom up, doing the operations. We
3648 -- gather a string that is as long as possible up to five operands
3650 -- The outer loop runs more than once if there are more than five
3651 -- concatenations of type Standard.String, the most we handle for
3652 -- this case, or if more than one concatenation type is involved.
3655 Opnds
:= New_List
(Left_Opnd
(Cnode
), Right_Opnd
(Cnode
));
3656 Set_Parent
(Opnds
, N
);
3658 -- The inner loop gathers concatenation operands. We gather any
3659 -- number of these in the non-string case, or if no concatenation
3660 -- routines are available for string (since in that case we will
3661 -- treat string like any other non-string case). Otherwise we only
3662 -- gather as many operands as can be handled by the available
3663 -- procedures in the run-time library (normally 5, but may be
3664 -- less for the configurable run-time case).
3666 Inner
: while Cnode
/= N
3667 and then (Base_Type
(Etype
(Cnode
)) /= Standard_String
3669 Max_Available_String_Operands
= 0
3671 List_Length
(Opnds
) <
3672 Max_Available_String_Operands
)
3673 and then Base_Type
(Etype
(Cnode
)) =
3674 Base_Type
(Etype
(Parent
(Cnode
)))
3676 Cnode
:= Parent
(Cnode
);
3677 Append
(Right_Opnd
(Cnode
), Opnds
);
3680 -- Here we process the collected operands. First we convert
3681 -- singleton operands to singleton aggregates. This is skipped
3682 -- however for the case of two operands of type String, since
3683 -- we have special routines for these cases.
3685 Atyp
:= Base_Type
(Etype
(Cnode
));
3686 Ctyp
:= Base_Type
(Component_Type
(Etype
(Cnode
)));
3688 if (List_Length
(Opnds
) > 2 or else Atyp
/= Standard_String
)
3689 or else not Char_Concat_Available
3691 Opnd
:= First
(Opnds
);
3693 if Base_Type
(Etype
(Opnd
)) = Ctyp
then
3695 Make_Aggregate
(Sloc
(Cnode
),
3696 Expressions
=> New_List
(Relocate_Node
(Opnd
))));
3697 Analyze_And_Resolve
(Opnd
, Atyp
);
3701 exit when No
(Opnd
);
3705 -- Now call appropriate continuation routine
3707 if Atyp
= Standard_String
3708 and then Max_Available_String_Operands
> 0
3710 Expand_Concatenate_String
(Cnode
, Opnds
);
3712 Expand_Concatenate_Other
(Cnode
, Opnds
);
3715 exit Outer
when Cnode
= N
;
3716 Cnode
:= Parent
(Cnode
);
3718 end Expand_N_Op_Concat
;
3720 ------------------------
3721 -- Expand_N_Op_Divide --
3722 ------------------------
3724 procedure Expand_N_Op_Divide
(N
: Node_Id
) is
3725 Loc
: constant Source_Ptr
:= Sloc
(N
);
3726 Ltyp
: constant Entity_Id
:= Etype
(Left_Opnd
(N
));
3727 Rtyp
: constant Entity_Id
:= Etype
(Right_Opnd
(N
));
3728 Typ
: Entity_Id
:= Etype
(N
);
3731 Binary_Op_Validity_Checks
(N
);
3733 -- Vax_Float is a special case
3735 if Vax_Float
(Typ
) then
3736 Expand_Vax_Arith
(N
);
3740 -- N / 1 = N for integer types
3742 if Is_Integer_Type
(Typ
)
3743 and then Compile_Time_Known_Value
(Right_Opnd
(N
))
3744 and then Expr_Value
(Right_Opnd
(N
)) = Uint_1
3746 Rewrite
(N
, Left_Opnd
(N
));
3750 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
3751 -- Is_Power_Of_2_For_Shift is set means that we know that our left
3752 -- operand is an unsigned integer, as required for this to work.
3754 if Nkind
(Right_Opnd
(N
)) = N_Op_Expon
3755 and then Is_Power_Of_2_For_Shift
(Right_Opnd
(N
))
3757 -- We cannot do this transformation in configurable run time mode if we
3758 -- have 64-bit -- integers and long shifts are not available.
3762 or else Support_Long_Shifts_On_Target
)
3765 Make_Op_Shift_Right
(Loc
,
3766 Left_Opnd
=> Left_Opnd
(N
),
3768 Convert_To
(Standard_Natural
, Right_Opnd
(Right_Opnd
(N
)))));
3769 Analyze_And_Resolve
(N
, Typ
);
3773 -- Do required fixup of universal fixed operation
3775 if Typ
= Universal_Fixed
then
3776 Fixup_Universal_Fixed_Operation
(N
);
3780 -- Divisions with fixed-point results
3782 if Is_Fixed_Point_Type
(Typ
) then
3784 -- No special processing if Treat_Fixed_As_Integer is set,
3785 -- since from a semantic point of view such operations are
3786 -- simply integer operations and will be treated that way.
3788 if not Treat_Fixed_As_Integer
(N
) then
3789 if Is_Integer_Type
(Rtyp
) then
3790 Expand_Divide_Fixed_By_Integer_Giving_Fixed
(N
);
3792 Expand_Divide_Fixed_By_Fixed_Giving_Fixed
(N
);
3796 -- Other cases of division of fixed-point operands. Again we
3797 -- exclude the case where Treat_Fixed_As_Integer is set.
3799 elsif (Is_Fixed_Point_Type
(Ltyp
) or else
3800 Is_Fixed_Point_Type
(Rtyp
))
3801 and then not Treat_Fixed_As_Integer
(N
)
3803 if Is_Integer_Type
(Typ
) then
3804 Expand_Divide_Fixed_By_Fixed_Giving_Integer
(N
);
3806 pragma Assert
(Is_Floating_Point_Type
(Typ
));
3807 Expand_Divide_Fixed_By_Fixed_Giving_Float
(N
);
3810 -- Mixed-mode operations can appear in a non-static universal
3811 -- context, in which case the integer argument must be converted
3814 elsif Typ
= Universal_Real
3815 and then Is_Integer_Type
(Rtyp
)
3817 Rewrite
(Right_Opnd
(N
),
3818 Convert_To
(Universal_Real
, Relocate_Node
(Right_Opnd
(N
))));
3820 Analyze_And_Resolve
(Right_Opnd
(N
), Universal_Real
);
3822 elsif Typ
= Universal_Real
3823 and then Is_Integer_Type
(Ltyp
)
3825 Rewrite
(Left_Opnd
(N
),
3826 Convert_To
(Universal_Real
, Relocate_Node
(Left_Opnd
(N
))));
3828 Analyze_And_Resolve
(Left_Opnd
(N
), Universal_Real
);
3830 -- Non-fixed point cases, do zero divide and overflow checks
3832 elsif Is_Integer_Type
(Typ
) then
3833 Apply_Divide_Check
(N
);
3835 -- Check for 64-bit division available
3837 if Esize
(Ltyp
) > 32
3838 and then not Support_64_Bit_Divides_On_Target
3840 Error_Msg_CRT
("64-bit division", N
);
3843 end Expand_N_Op_Divide
;
3845 --------------------
3846 -- Expand_N_Op_Eq --
3847 --------------------
3849 procedure Expand_N_Op_Eq
(N
: Node_Id
) is
3850 Loc
: constant Source_Ptr
:= Sloc
(N
);
3851 Typ
: constant Entity_Id
:= Etype
(N
);
3852 Lhs
: constant Node_Id
:= Left_Opnd
(N
);
3853 Rhs
: constant Node_Id
:= Right_Opnd
(N
);
3854 Bodies
: constant List_Id
:= New_List
;
3855 A_Typ
: constant Entity_Id
:= Etype
(Lhs
);
3857 Typl
: Entity_Id
:= A_Typ
;
3858 Op_Name
: Entity_Id
;
3861 procedure Build_Equality_Call
(Eq
: Entity_Id
);
3862 -- If a constructed equality exists for the type or for its parent,
3863 -- build and analyze call, adding conversions if the operation is
3866 function Has_Unconstrained_UU_Component
(Typ
: Node_Id
) return Boolean;
3867 -- Determines whether a type has a subcompoment of an unconstrained
3868 -- Unchecked_Union subtype. Typ is a record type.
3870 -------------------------
3871 -- Build_Equality_Call --
3872 -------------------------
3874 procedure Build_Equality_Call
(Eq
: Entity_Id
) is
3875 Op_Type
: constant Entity_Id
:= Etype
(First_Formal
(Eq
));
3876 L_Exp
: Node_Id
:= Relocate_Node
(Lhs
);
3877 R_Exp
: Node_Id
:= Relocate_Node
(Rhs
);
3880 if Base_Type
(Op_Type
) /= Base_Type
(A_Typ
)
3881 and then not Is_Class_Wide_Type
(A_Typ
)
3883 L_Exp
:= OK_Convert_To
(Op_Type
, L_Exp
);
3884 R_Exp
:= OK_Convert_To
(Op_Type
, R_Exp
);
3887 -- If we have an Unchecked_Union, we need to add the inferred
3888 -- discriminant values as actuals in the function call. At this
3889 -- point, the expansion has determined that both operands have
3890 -- inferable discriminants.
3892 if Is_Unchecked_Union
(Op_Type
) then
3894 Lhs_Type
: constant Node_Id
:= Etype
(L_Exp
);
3895 Rhs_Type
: constant Node_Id
:= Etype
(R_Exp
);
3896 Lhs_Discr_Val
: Node_Id
;
3897 Rhs_Discr_Val
: Node_Id
;
3900 -- Per-object constrained selected components require special
3901 -- attention. If the enclosing scope of the component is an
3902 -- Unchecked_Union, we can not reference its discriminants
3903 -- directly. This is why we use the two extra parameters of
3904 -- the equality function of the enclosing Unchecked_Union.
3906 -- type UU_Type (Discr : Integer := 0) is
3909 -- pragma Unchecked_Union (UU_Type);
3911 -- 1. Unchecked_Union enclosing record:
3913 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
3915 -- Comp : UU_Type (Discr);
3917 -- end Enclosing_UU_Type;
3918 -- pragma Unchecked_Union (Enclosing_UU_Type);
3920 -- Obj1 : Enclosing_UU_Type;
3921 -- Obj2 : Enclosing_UU_Type (1);
3923 -- [. . .] Obj1 = Obj2 [. . .]
3927 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
3929 -- A and B are the formal parameters of the equality function
3930 -- of Enclosing_UU_Type. The function always has two extra
3931 -- formals to capture the inferred discriminant values.
3933 -- 2. Non-Unchecked_Union enclosing record:
3936 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
3939 -- Comp : UU_Type (Discr);
3941 -- end Enclosing_Non_UU_Type;
3943 -- Obj1 : Enclosing_Non_UU_Type;
3944 -- Obj2 : Enclosing_Non_UU_Type (1);
3946 -- . . . Obj1 = Obj2 . . .
3950 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
3951 -- obj1.discr, obj2.discr)) then
3953 -- In this case we can directly reference the discriminants of
3954 -- the enclosing record.
3958 if Nkind
(Lhs
) = N_Selected_Component
3959 and then Has_Per_Object_Constraint
3960 (Entity
(Selector_Name
(Lhs
)))
3962 -- Enclosing record is an Unchecked_Union, use formal A
3964 if Is_Unchecked_Union
(Scope
3965 (Entity
(Selector_Name
(Lhs
))))
3968 Make_Identifier
(Loc
,
3971 -- Enclosing record is of a non-Unchecked_Union type, it is
3972 -- possible to reference the discriminant.
3976 Make_Selected_Component
(Loc
,
3977 Prefix
=> Prefix
(Lhs
),
3980 (Get_Discriminant_Value
3981 (First_Discriminant
(Lhs_Type
),
3983 Stored_Constraint
(Lhs_Type
))));
3986 -- Comment needed here ???
3989 -- Infer the discriminant value
3993 (Get_Discriminant_Value
3994 (First_Discriminant
(Lhs_Type
),
3996 Stored_Constraint
(Lhs_Type
)));
4001 if Nkind
(Rhs
) = N_Selected_Component
4002 and then Has_Per_Object_Constraint
4003 (Entity
(Selector_Name
(Rhs
)))
4005 if Is_Unchecked_Union
4006 (Scope
(Entity
(Selector_Name
(Rhs
))))
4009 Make_Identifier
(Loc
,
4014 Make_Selected_Component
(Loc
,
4015 Prefix
=> Prefix
(Rhs
),
4017 New_Copy
(Get_Discriminant_Value
(
4018 First_Discriminant
(Rhs_Type
),
4020 Stored_Constraint
(Rhs_Type
))));
4025 New_Copy
(Get_Discriminant_Value
(
4026 First_Discriminant
(Rhs_Type
),
4028 Stored_Constraint
(Rhs_Type
)));
4033 Make_Function_Call
(Loc
,
4034 Name
=> New_Reference_To
(Eq
, Loc
),
4035 Parameter_Associations
=> New_List
(
4042 -- Normal case, not an unchecked union
4046 Make_Function_Call
(Loc
,
4047 Name
=> New_Reference_To
(Eq
, Loc
),
4048 Parameter_Associations
=> New_List
(L_Exp
, R_Exp
)));
4051 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
4052 end Build_Equality_Call
;
4054 ------------------------------------
4055 -- Has_Unconstrained_UU_Component --
4056 ------------------------------------
4058 function Has_Unconstrained_UU_Component
4059 (Typ
: Node_Id
) return Boolean
4061 Tdef
: constant Node_Id
:=
4062 Type_Definition
(Declaration_Node
(Typ
));
4066 function Component_Is_Unconstrained_UU
4067 (Comp
: Node_Id
) return Boolean;
4068 -- Determines whether the subtype of the component is an
4069 -- unconstrained Unchecked_Union.
4071 function Variant_Is_Unconstrained_UU
4072 (Variant
: Node_Id
) return Boolean;
4073 -- Determines whether a component of the variant has an unconstrained
4074 -- Unchecked_Union subtype.
4076 -----------------------------------
4077 -- Component_Is_Unconstrained_UU --
4078 -----------------------------------
4080 function Component_Is_Unconstrained_UU
4081 (Comp
: Node_Id
) return Boolean
4084 if Nkind
(Comp
) /= N_Component_Declaration
then
4089 Sindic
: constant Node_Id
:=
4090 Subtype_Indication
(Component_Definition
(Comp
));
4093 -- Unconstrained nominal type. In the case of a constraint
4094 -- present, the node kind would have been N_Subtype_Indication.
4096 if Nkind
(Sindic
) = N_Identifier
then
4097 return Is_Unchecked_Union
(Base_Type
(Etype
(Sindic
)));
4102 end Component_Is_Unconstrained_UU
;
4104 ---------------------------------
4105 -- Variant_Is_Unconstrained_UU --
4106 ---------------------------------
4108 function Variant_Is_Unconstrained_UU
4109 (Variant
: Node_Id
) return Boolean
4111 Clist
: constant Node_Id
:= Component_List
(Variant
);
4114 if Is_Empty_List
(Component_Items
(Clist
)) then
4119 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
4122 while Present
(Comp
) loop
4124 -- One component is sufficent
4126 if Component_Is_Unconstrained_UU
(Comp
) then
4134 -- None of the components withing the variant were of
4135 -- unconstrained Unchecked_Union type.
4138 end Variant_Is_Unconstrained_UU
;
4140 -- Start of processing for Has_Unconstrained_UU_Component
4143 if Null_Present
(Tdef
) then
4147 Clist
:= Component_List
(Tdef
);
4148 Vpart
:= Variant_Part
(Clist
);
4150 -- Inspect available components
4152 if Present
(Component_Items
(Clist
)) then
4154 Comp
: Node_Id
:= First
(Component_Items
(Clist
));
4157 while Present
(Comp
) loop
4159 -- One component is sufficent
4161 if Component_Is_Unconstrained_UU
(Comp
) then
4170 -- Inspect available components withing variants
4172 if Present
(Vpart
) then
4174 Variant
: Node_Id
:= First
(Variants
(Vpart
));
4177 while Present
(Variant
) loop
4179 -- One component within a variant is sufficent
4181 if Variant_Is_Unconstrained_UU
(Variant
) then
4190 -- Neither the available components, nor the components inside the
4191 -- variant parts were of an unconstrained Unchecked_Union subtype.
4194 end Has_Unconstrained_UU_Component
;
4196 -- Start of processing for Expand_N_Op_Eq
4199 Binary_Op_Validity_Checks
(N
);
4201 if Ekind
(Typl
) = E_Private_Type
then
4202 Typl
:= Underlying_Type
(Typl
);
4204 elsif Ekind
(Typl
) = E_Private_Subtype
then
4205 Typl
:= Underlying_Type
(Base_Type
(Typl
));
4208 -- It may happen in error situations that the underlying type is not
4209 -- set. The error will be detected later, here we just defend the
4216 Typl
:= Base_Type
(Typl
);
4220 if Vax_Float
(Typl
) then
4221 Expand_Vax_Comparison
(N
);
4224 -- Boolean types (requiring handling of non-standard case)
4226 elsif Is_Boolean_Type
(Typl
) then
4227 Adjust_Condition
(Left_Opnd
(N
));
4228 Adjust_Condition
(Right_Opnd
(N
));
4229 Set_Etype
(N
, Standard_Boolean
);
4230 Adjust_Result_Type
(N
, Typ
);
4234 elsif Is_Array_Type
(Typl
) then
4236 -- If we are doing full validity checking, then expand out array
4237 -- comparisons to make sure that we check the array elements.
4239 if Validity_Check_Operands
then
4241 Save_Force_Validity_Checks
: constant Boolean :=
4242 Force_Validity_Checks
;
4244 Force_Validity_Checks
:= True;
4246 Expand_Array_Equality
4248 Relocate_Node
(Lhs
),
4249 Relocate_Node
(Rhs
),
4252 Insert_Actions
(N
, Bodies
);
4253 Analyze_And_Resolve
(N
, Standard_Boolean
);
4254 Force_Validity_Checks
:= Save_Force_Validity_Checks
;
4259 elsif Is_Bit_Packed_Array
(Typl
) then
4260 Expand_Packed_Eq
(N
);
4262 -- Where the component type is elementary we can use a block bit
4263 -- comparison (if supported on the target) exception in the case
4264 -- of floating-point (negative zero issues require element by
4265 -- element comparison), and atomic types (where we must be sure
4266 -- to load elements independently).
4268 elsif Is_Elementary_Type
(Component_Type
(Typl
))
4269 and then not Is_Floating_Point_Type
(Component_Type
(Typl
))
4270 and then not Is_Atomic
(Component_Type
(Typl
))
4271 and then Support_Composite_Compare_On_Target
4275 -- For composite and floating-point cases, expand equality loop
4276 -- to make sure of using proper comparisons for tagged types,
4277 -- and correctly handling the floating-point case.
4281 Expand_Array_Equality
4283 Relocate_Node
(Lhs
),
4284 Relocate_Node
(Rhs
),
4287 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
4288 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
4293 elsif Is_Record_Type
(Typl
) then
4295 -- For tagged types, use the primitive "="
4297 if Is_Tagged_Type
(Typl
) then
4299 -- If this is derived from an untagged private type completed
4300 -- with a tagged type, it does not have a full view, so we
4301 -- use the primitive operations of the private type.
4302 -- This check should no longer be necessary when these
4303 -- types receive their full views ???
4305 if Is_Private_Type
(A_Typ
)
4306 and then not Is_Tagged_Type
(A_Typ
)
4307 and then Is_Derived_Type
(A_Typ
)
4308 and then No
(Full_View
(A_Typ
))
4310 -- Search for equality operation, checking that the
4311 -- operands have the same type. Note that we must find
4312 -- a matching entry, or something is very wrong!
4314 Prim
:= First_Elmt
(Collect_Primitive_Operations
(A_Typ
));
4316 while Present
(Prim
) loop
4317 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
4318 and then Etype
(First_Formal
(Node
(Prim
))) =
4319 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
4321 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
4326 pragma Assert
(Present
(Prim
));
4327 Op_Name
:= Node
(Prim
);
4329 -- Find the type's predefined equality or an overriding
4330 -- user-defined equality. The reason for not simply calling
4331 -- Find_Prim_Op here is that there may be a user-defined
4332 -- overloaded equality op that precedes the equality that
4333 -- we want, so we have to explicitly search (e.g., there
4334 -- could be an equality with two different parameter types).
4337 if Is_Class_Wide_Type
(Typl
) then
4338 Typl
:= Root_Type
(Typl
);
4341 Prim
:= First_Elmt
(Primitive_Operations
(Typl
));
4342 while Present
(Prim
) loop
4343 exit when Chars
(Node
(Prim
)) = Name_Op_Eq
4344 and then Etype
(First_Formal
(Node
(Prim
))) =
4345 Etype
(Next_Formal
(First_Formal
(Node
(Prim
))))
4347 Base_Type
(Etype
(Node
(Prim
))) = Standard_Boolean
;
4352 pragma Assert
(Present
(Prim
));
4353 Op_Name
:= Node
(Prim
);
4356 Build_Equality_Call
(Op_Name
);
4358 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
4359 -- predefined equality operator for a type which has a subcomponent
4360 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
4362 elsif Has_Unconstrained_UU_Component
(Typl
) then
4364 Make_Raise_Program_Error
(Loc
,
4365 Reason
=> PE_Unchecked_Union_Restriction
));
4367 -- Prevent Gigi from generating incorrect code by rewriting the
4368 -- equality as a standard False.
4371 New_Occurrence_Of
(Standard_False
, Loc
));
4373 elsif Is_Unchecked_Union
(Typl
) then
4375 -- If we can infer the discriminants of the operands, we make a
4376 -- call to the TSS equality function.
4378 if Has_Inferable_Discriminants
(Lhs
)
4380 Has_Inferable_Discriminants
(Rhs
)
4383 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
4386 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4387 -- the predefined equality operator for an Unchecked_Union type
4388 -- if either of the operands lack inferable discriminants.
4391 Make_Raise_Program_Error
(Loc
,
4392 Reason
=> PE_Unchecked_Union_Restriction
));
4394 -- Prevent Gigi from generating incorrect code by rewriting
4395 -- the equality as a standard False.
4398 New_Occurrence_Of
(Standard_False
, Loc
));
4402 -- If a type support function is present (for complex cases), use it
4404 elsif Present
(TSS
(Root_Type
(Typl
), TSS_Composite_Equality
)) then
4406 (TSS
(Root_Type
(Typl
), TSS_Composite_Equality
));
4408 -- Otherwise expand the component by component equality. Note that
4409 -- we never use block-bit coparisons for records, because of the
4410 -- problems with gaps. The backend will often be able to recombine
4411 -- the separate comparisons that we generate here.
4414 Remove_Side_Effects
(Lhs
);
4415 Remove_Side_Effects
(Rhs
);
4417 Expand_Record_Equality
(N
, Typl
, Lhs
, Rhs
, Bodies
));
4419 Insert_Actions
(N
, Bodies
, Suppress
=> All_Checks
);
4420 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
4424 -- If we still have an equality comparison (i.e. it was not rewritten
4425 -- in some way), then we can test if result is needed at compile time).
4427 if Nkind
(N
) = N_Op_Eq
then
4428 Rewrite_Comparison
(N
);
4432 -----------------------
4433 -- Expand_N_Op_Expon --
4434 -----------------------
4436 procedure Expand_N_Op_Expon
(N
: Node_Id
) is
4437 Loc
: constant Source_Ptr
:= Sloc
(N
);
4438 Typ
: constant Entity_Id
:= Etype
(N
);
4439 Rtyp
: constant Entity_Id
:= Root_Type
(Typ
);
4440 Base
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
4441 Bastyp
: constant Node_Id
:= Etype
(Base
);
4442 Exp
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
4443 Exptyp
: constant Entity_Id
:= Etype
(Exp
);
4444 Ovflo
: constant Boolean := Do_Overflow_Check
(N
);
4453 Binary_Op_Validity_Checks
(N
);
4455 -- If either operand is of a private type, then we have the use of
4456 -- an intrinsic operator, and we get rid of the privateness, by using
4457 -- root types of underlying types for the actual operation. Otherwise
4458 -- the private types will cause trouble if we expand multiplications
4459 -- or shifts etc. We also do this transformation if the result type
4460 -- is different from the base type.
4462 if Is_Private_Type
(Etype
(Base
))
4464 Is_Private_Type
(Typ
)
4466 Is_Private_Type
(Exptyp
)
4468 Rtyp
/= Root_Type
(Bastyp
)
4471 Bt
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Bastyp
));
4472 Et
: constant Entity_Id
:= Root_Type
(Underlying_Type
(Exptyp
));
4476 Unchecked_Convert_To
(Typ
,
4478 Left_Opnd
=> Unchecked_Convert_To
(Bt
, Base
),
4479 Right_Opnd
=> Unchecked_Convert_To
(Et
, Exp
))));
4480 Analyze_And_Resolve
(N
, Typ
);
4485 -- Test for case of known right argument
4487 if Compile_Time_Known_Value
(Exp
) then
4488 Expv
:= Expr_Value
(Exp
);
4490 -- We only fold small non-negative exponents. You might think we
4491 -- could fold small negative exponents for the real case, but we
4492 -- can't because we are required to raise Constraint_Error for
4493 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
4494 -- See ACVC test C4A012B.
4496 if Expv
>= 0 and then Expv
<= 4 then
4498 -- X ** 0 = 1 (or 1.0)
4501 if Ekind
(Typ
) in Integer_Kind
then
4502 Xnode
:= Make_Integer_Literal
(Loc
, Intval
=> 1);
4504 Xnode
:= Make_Real_Literal
(Loc
, Ureal_1
);
4516 Make_Op_Multiply
(Loc
,
4517 Left_Opnd
=> Duplicate_Subexpr
(Base
),
4518 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
));
4520 -- X ** 3 = X * X * X
4524 Make_Op_Multiply
(Loc
,
4526 Make_Op_Multiply
(Loc
,
4527 Left_Opnd
=> Duplicate_Subexpr
(Base
),
4528 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)),
4529 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
));
4532 -- En : constant base'type := base * base;
4538 Make_Defining_Identifier
(Loc
, New_Internal_Name
('E'));
4540 Insert_Actions
(N
, New_List
(
4541 Make_Object_Declaration
(Loc
,
4542 Defining_Identifier
=> Temp
,
4543 Constant_Present
=> True,
4544 Object_Definition
=> New_Reference_To
(Typ
, Loc
),
4546 Make_Op_Multiply
(Loc
,
4547 Left_Opnd
=> Duplicate_Subexpr
(Base
),
4548 Right_Opnd
=> Duplicate_Subexpr_No_Checks
(Base
)))));
4551 Make_Op_Multiply
(Loc
,
4552 Left_Opnd
=> New_Reference_To
(Temp
, Loc
),
4553 Right_Opnd
=> New_Reference_To
(Temp
, Loc
));
4557 Analyze_And_Resolve
(N
, Typ
);
4562 -- Case of (2 ** expression) appearing as an argument of an integer
4563 -- multiplication, or as the right argument of a division of a non-
4564 -- negative integer. In such cases we leave the node untouched, setting
4565 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
4566 -- of the higher level node converts it into a shift.
4568 if Nkind
(Base
) = N_Integer_Literal
4569 and then Intval
(Base
) = 2
4570 and then Is_Integer_Type
(Root_Type
(Exptyp
))
4571 and then Esize
(Root_Type
(Exptyp
)) <= Esize
(Standard_Integer
)
4572 and then Is_Unsigned_Type
(Exptyp
)
4574 and then Nkind
(Parent
(N
)) in N_Binary_Op
4577 P
: constant Node_Id
:= Parent
(N
);
4578 L
: constant Node_Id
:= Left_Opnd
(P
);
4579 R
: constant Node_Id
:= Right_Opnd
(P
);
4582 if (Nkind
(P
) = N_Op_Multiply
4584 ((Is_Integer_Type
(Etype
(L
)) and then R
= N
)
4586 (Is_Integer_Type
(Etype
(R
)) and then L
= N
))
4587 and then not Do_Overflow_Check
(P
))
4590 (Nkind
(P
) = N_Op_Divide
4591 and then Is_Integer_Type
(Etype
(L
))
4592 and then Is_Unsigned_Type
(Etype
(L
))
4594 and then not Do_Overflow_Check
(P
))
4596 Set_Is_Power_Of_2_For_Shift
(N
);
4602 -- Fall through if exponentiation must be done using a runtime routine
4604 -- First deal with modular case
4606 if Is_Modular_Integer_Type
(Rtyp
) then
4608 -- Non-binary case, we call the special exponentiation routine for
4609 -- the non-binary case, converting the argument to Long_Long_Integer
4610 -- and passing the modulus value. Then the result is converted back
4611 -- to the base type.
4613 if Non_Binary_Modulus
(Rtyp
) then
4616 Make_Function_Call
(Loc
,
4617 Name
=> New_Reference_To
(RTE
(RE_Exp_Modular
), Loc
),
4618 Parameter_Associations
=> New_List
(
4619 Convert_To
(Standard_Integer
, Base
),
4620 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
)),
4623 -- Binary case, in this case, we call one of two routines, either
4624 -- the unsigned integer case, or the unsigned long long integer
4625 -- case, with a final "and" operation to do the required mod.
4628 if UI_To_Int
(Esize
(Rtyp
)) <= Standard_Integer_Size
then
4629 Ent
:= RTE
(RE_Exp_Unsigned
);
4631 Ent
:= RTE
(RE_Exp_Long_Long_Unsigned
);
4638 Make_Function_Call
(Loc
,
4639 Name
=> New_Reference_To
(Ent
, Loc
),
4640 Parameter_Associations
=> New_List
(
4641 Convert_To
(Etype
(First_Formal
(Ent
)), Base
),
4644 Make_Integer_Literal
(Loc
, Modulus
(Rtyp
) - 1))));
4648 -- Common exit point for modular type case
4650 Analyze_And_Resolve
(N
, Typ
);
4653 -- Signed integer cases, done using either Integer or Long_Long_Integer.
4654 -- It is not worth having routines for Short_[Short_]Integer, since for
4655 -- most machines it would not help, and it would generate more code that
4656 -- might need certification in the HI-E case.
4658 -- In the integer cases, we have two routines, one for when overflow
4659 -- checks are required, and one when they are not required, since
4660 -- there is a real gain in ommitting checks on many machines.
4662 elsif Rtyp
= Base_Type
(Standard_Long_Long_Integer
)
4663 or else (Rtyp
= Base_Type
(Standard_Long_Integer
)
4665 Esize
(Standard_Long_Integer
) > Esize
(Standard_Integer
))
4666 or else (Rtyp
= Universal_Integer
)
4668 Etyp
:= Standard_Long_Long_Integer
;
4671 Rent
:= RE_Exp_Long_Long_Integer
;
4673 Rent
:= RE_Exn_Long_Long_Integer
;
4676 elsif Is_Signed_Integer_Type
(Rtyp
) then
4677 Etyp
:= Standard_Integer
;
4680 Rent
:= RE_Exp_Integer
;
4682 Rent
:= RE_Exn_Integer
;
4685 -- Floating-point cases, always done using Long_Long_Float. We do not
4686 -- need separate routines for the overflow case here, since in the case
4687 -- of floating-point, we generate infinities anyway as a rule (either
4688 -- that or we automatically trap overflow), and if there is an infinity
4689 -- generated and a range check is required, the check will fail anyway.
4692 pragma Assert
(Is_Floating_Point_Type
(Rtyp
));
4693 Etyp
:= Standard_Long_Long_Float
;
4694 Rent
:= RE_Exn_Long_Long_Float
;
4697 -- Common processing for integer cases and floating-point cases.
4698 -- If we are in the right type, we can call runtime routine directly
4701 and then Rtyp
/= Universal_Integer
4702 and then Rtyp
/= Universal_Real
4705 Make_Function_Call
(Loc
,
4706 Name
=> New_Reference_To
(RTE
(Rent
), Loc
),
4707 Parameter_Associations
=> New_List
(Base
, Exp
)));
4709 -- Otherwise we have to introduce conversions (conversions are also
4710 -- required in the universal cases, since the runtime routine is
4711 -- typed using one of the standard types.
4716 Make_Function_Call
(Loc
,
4717 Name
=> New_Reference_To
(RTE
(Rent
), Loc
),
4718 Parameter_Associations
=> New_List
(
4719 Convert_To
(Etyp
, Base
),
4723 Analyze_And_Resolve
(N
, Typ
);
4727 when RE_Not_Available
=>
4729 end Expand_N_Op_Expon
;
4731 --------------------
4732 -- Expand_N_Op_Ge --
4733 --------------------
4735 procedure Expand_N_Op_Ge
(N
: Node_Id
) is
4736 Typ
: constant Entity_Id
:= Etype
(N
);
4737 Op1
: constant Node_Id
:= Left_Opnd
(N
);
4738 Op2
: constant Node_Id
:= Right_Opnd
(N
);
4739 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
4742 Binary_Op_Validity_Checks
(N
);
4744 if Vax_Float
(Typ1
) then
4745 Expand_Vax_Comparison
(N
);
4748 elsif Is_Array_Type
(Typ1
) then
4749 Expand_Array_Comparison
(N
);
4753 if Is_Boolean_Type
(Typ1
) then
4754 Adjust_Condition
(Op1
);
4755 Adjust_Condition
(Op2
);
4756 Set_Etype
(N
, Standard_Boolean
);
4757 Adjust_Result_Type
(N
, Typ
);
4760 Rewrite_Comparison
(N
);
4763 --------------------
4764 -- Expand_N_Op_Gt --
4765 --------------------
4767 procedure Expand_N_Op_Gt
(N
: Node_Id
) is
4768 Typ
: constant Entity_Id
:= Etype
(N
);
4769 Op1
: constant Node_Id
:= Left_Opnd
(N
);
4770 Op2
: constant Node_Id
:= Right_Opnd
(N
);
4771 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
4774 Binary_Op_Validity_Checks
(N
);
4776 if Vax_Float
(Typ1
) then
4777 Expand_Vax_Comparison
(N
);
4780 elsif Is_Array_Type
(Typ1
) then
4781 Expand_Array_Comparison
(N
);
4785 if Is_Boolean_Type
(Typ1
) then
4786 Adjust_Condition
(Op1
);
4787 Adjust_Condition
(Op2
);
4788 Set_Etype
(N
, Standard_Boolean
);
4789 Adjust_Result_Type
(N
, Typ
);
4792 Rewrite_Comparison
(N
);
4795 --------------------
4796 -- Expand_N_Op_Le --
4797 --------------------
4799 procedure Expand_N_Op_Le
(N
: Node_Id
) is
4800 Typ
: constant Entity_Id
:= Etype
(N
);
4801 Op1
: constant Node_Id
:= Left_Opnd
(N
);
4802 Op2
: constant Node_Id
:= Right_Opnd
(N
);
4803 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
4806 Binary_Op_Validity_Checks
(N
);
4808 if Vax_Float
(Typ1
) then
4809 Expand_Vax_Comparison
(N
);
4812 elsif Is_Array_Type
(Typ1
) then
4813 Expand_Array_Comparison
(N
);
4817 if Is_Boolean_Type
(Typ1
) then
4818 Adjust_Condition
(Op1
);
4819 Adjust_Condition
(Op2
);
4820 Set_Etype
(N
, Standard_Boolean
);
4821 Adjust_Result_Type
(N
, Typ
);
4824 Rewrite_Comparison
(N
);
4827 --------------------
4828 -- Expand_N_Op_Lt --
4829 --------------------
4831 procedure Expand_N_Op_Lt
(N
: Node_Id
) is
4832 Typ
: constant Entity_Id
:= Etype
(N
);
4833 Op1
: constant Node_Id
:= Left_Opnd
(N
);
4834 Op2
: constant Node_Id
:= Right_Opnd
(N
);
4835 Typ1
: constant Entity_Id
:= Base_Type
(Etype
(Op1
));
4838 Binary_Op_Validity_Checks
(N
);
4840 if Vax_Float
(Typ1
) then
4841 Expand_Vax_Comparison
(N
);
4844 elsif Is_Array_Type
(Typ1
) then
4845 Expand_Array_Comparison
(N
);
4849 if Is_Boolean_Type
(Typ1
) then
4850 Adjust_Condition
(Op1
);
4851 Adjust_Condition
(Op2
);
4852 Set_Etype
(N
, Standard_Boolean
);
4853 Adjust_Result_Type
(N
, Typ
);
4856 Rewrite_Comparison
(N
);
4859 -----------------------
4860 -- Expand_N_Op_Minus --
4861 -----------------------
4863 procedure Expand_N_Op_Minus
(N
: Node_Id
) is
4864 Loc
: constant Source_Ptr
:= Sloc
(N
);
4865 Typ
: constant Entity_Id
:= Etype
(N
);
4868 Unary_Op_Validity_Checks
(N
);
4870 if not Backend_Overflow_Checks_On_Target
4871 and then Is_Signed_Integer_Type
(Etype
(N
))
4872 and then Do_Overflow_Check
(N
)
4874 -- Software overflow checking expands -expr into (0 - expr)
4877 Make_Op_Subtract
(Loc
,
4878 Left_Opnd
=> Make_Integer_Literal
(Loc
, 0),
4879 Right_Opnd
=> Right_Opnd
(N
)));
4881 Analyze_And_Resolve
(N
, Typ
);
4883 -- Vax floating-point types case
4885 elsif Vax_Float
(Etype
(N
)) then
4886 Expand_Vax_Arith
(N
);
4888 end Expand_N_Op_Minus
;
4890 ---------------------
4891 -- Expand_N_Op_Mod --
4892 ---------------------
4894 procedure Expand_N_Op_Mod
(N
: Node_Id
) is
4895 Loc
: constant Source_Ptr
:= Sloc
(N
);
4896 Typ
: constant Entity_Id
:= Etype
(N
);
4897 Left
: constant Node_Id
:= Left_Opnd
(N
);
4898 Right
: constant Node_Id
:= Right_Opnd
(N
);
4899 DOC
: constant Boolean := Do_Overflow_Check
(N
);
4900 DDC
: constant Boolean := Do_Division_Check
(N
);
4911 Binary_Op_Validity_Checks
(N
);
4913 Determine_Range
(Right
, ROK
, Rlo
, Rhi
);
4914 Determine_Range
(Left
, LOK
, Llo
, Lhi
);
4916 -- Convert mod to rem if operands are known non-negative. We do this
4917 -- since it is quite likely that this will improve the quality of code,
4918 -- (the operation now corresponds to the hardware remainder), and it
4919 -- does not seem likely that it could be harmful.
4921 if LOK
and then Llo
>= 0
4923 ROK
and then Rlo
>= 0
4926 Make_Op_Rem
(Sloc
(N
),
4927 Left_Opnd
=> Left_Opnd
(N
),
4928 Right_Opnd
=> Right_Opnd
(N
)));
4930 -- Instead of reanalyzing the node we do the analysis manually.
4931 -- This avoids anomalies when the replacement is done in an
4932 -- instance and is epsilon more efficient.
4934 Set_Entity
(N
, Standard_Entity
(S_Op_Rem
));
4936 Set_Do_Overflow_Check
(N
, DOC
);
4937 Set_Do_Division_Check
(N
, DDC
);
4938 Expand_N_Op_Rem
(N
);
4941 -- Otherwise, normal mod processing
4944 if Is_Integer_Type
(Etype
(N
)) then
4945 Apply_Divide_Check
(N
);
4948 -- Apply optimization x mod 1 = 0. We don't really need that with
4949 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
4950 -- certainly harmless.
4952 if Is_Integer_Type
(Etype
(N
))
4953 and then Compile_Time_Known_Value
(Right
)
4954 and then Expr_Value
(Right
) = Uint_1
4956 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
4957 Analyze_And_Resolve
(N
, Typ
);
4961 -- Deal with annoying case of largest negative number remainder
4962 -- minus one. Gigi does not handle this case correctly, because
4963 -- it generates a divide instruction which may trap in this case.
4965 -- In fact the check is quite easy, if the right operand is -1,
4966 -- then the mod value is always 0, and we can just ignore the
4967 -- left operand completely in this case.
4969 -- The operand type may be private (e.g. in the expansion of an
4970 -- an intrinsic operation) so we must use the underlying type to
4971 -- get the bounds, and convert the literals explicitly.
4975 (Type_Low_Bound
(Base_Type
(Underlying_Type
(Etype
(Left
)))));
4977 if ((not ROK
) or else (Rlo
<= (-1) and then (-1) <= Rhi
))
4979 ((not LOK
) or else (Llo
= LLB
))
4982 Make_Conditional_Expression
(Loc
,
4983 Expressions
=> New_List
(
4985 Left_Opnd
=> Duplicate_Subexpr
(Right
),
4987 Unchecked_Convert_To
(Typ
,
4988 Make_Integer_Literal
(Loc
, -1))),
4989 Unchecked_Convert_To
(Typ
,
4990 Make_Integer_Literal
(Loc
, Uint_0
)),
4991 Relocate_Node
(N
))));
4993 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
4994 Analyze_And_Resolve
(N
, Typ
);
4997 end Expand_N_Op_Mod
;
4999 --------------------------
5000 -- Expand_N_Op_Multiply --
5001 --------------------------
5003 procedure Expand_N_Op_Multiply
(N
: Node_Id
) is
5004 Loc
: constant Source_Ptr
:= Sloc
(N
);
5005 Lop
: constant Node_Id
:= Left_Opnd
(N
);
5006 Rop
: constant Node_Id
:= Right_Opnd
(N
);
5008 Lp2
: constant Boolean :=
5009 Nkind
(Lop
) = N_Op_Expon
5010 and then Is_Power_Of_2_For_Shift
(Lop
);
5012 Rp2
: constant Boolean :=
5013 Nkind
(Rop
) = N_Op_Expon
5014 and then Is_Power_Of_2_For_Shift
(Rop
);
5016 Ltyp
: constant Entity_Id
:= Etype
(Lop
);
5017 Rtyp
: constant Entity_Id
:= Etype
(Rop
);
5018 Typ
: Entity_Id
:= Etype
(N
);
5021 Binary_Op_Validity_Checks
(N
);
5023 -- Special optimizations for integer types
5025 if Is_Integer_Type
(Typ
) then
5027 -- N * 0 = 0 * N = 0 for integer types
5029 if (Compile_Time_Known_Value
(Rop
)
5030 and then Expr_Value
(Rop
) = Uint_0
)
5032 (Compile_Time_Known_Value
(Lop
)
5033 and then Expr_Value
(Lop
) = Uint_0
)
5035 Rewrite
(N
, Make_Integer_Literal
(Loc
, Uint_0
));
5036 Analyze_And_Resolve
(N
, Typ
);
5040 -- N * 1 = 1 * N = N for integer types
5042 -- This optimisation is not done if we are going to
5043 -- rewrite the product 1 * 2 ** N to a shift.
5045 if Compile_Time_Known_Value
(Rop
)
5046 and then Expr_Value
(Rop
) = Uint_1
5052 elsif Compile_Time_Known_Value
(Lop
)
5053 and then Expr_Value
(Lop
) = Uint_1
5061 -- Deal with VAX float case
5063 if Vax_Float
(Typ
) then
5064 Expand_Vax_Arith
(N
);
5068 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
5069 -- Is_Power_Of_2_For_Shift is set means that we know that our left
5070 -- operand is an integer, as required for this to work.
5075 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
5079 Left_Opnd
=> Make_Integer_Literal
(Loc
, 2),
5082 Left_Opnd
=> Right_Opnd
(Lop
),
5083 Right_Opnd
=> Right_Opnd
(Rop
))));
5084 Analyze_And_Resolve
(N
, Typ
);
5089 Make_Op_Shift_Left
(Loc
,
5092 Convert_To
(Standard_Natural
, Right_Opnd
(Rop
))));
5093 Analyze_And_Resolve
(N
, Typ
);
5097 -- Same processing for the operands the other way round
5101 Make_Op_Shift_Left
(Loc
,
5104 Convert_To
(Standard_Natural
, Right_Opnd
(Lop
))));
5105 Analyze_And_Resolve
(N
, Typ
);
5109 -- Do required fixup of universal fixed operation
5111 if Typ
= Universal_Fixed
then
5112 Fixup_Universal_Fixed_Operation
(N
);
5116 -- Multiplications with fixed-point results
5118 if Is_Fixed_Point_Type
(Typ
) then
5120 -- No special processing if Treat_Fixed_As_Integer is set,
5121 -- since from a semantic point of view such operations are
5122 -- simply integer operations and will be treated that way.
5124 if not Treat_Fixed_As_Integer
(N
) then
5126 -- Case of fixed * integer => fixed
5128 if Is_Integer_Type
(Rtyp
) then
5129 Expand_Multiply_Fixed_By_Integer_Giving_Fixed
(N
);
5131 -- Case of integer * fixed => fixed
5133 elsif Is_Integer_Type
(Ltyp
) then
5134 Expand_Multiply_Integer_By_Fixed_Giving_Fixed
(N
);
5136 -- Case of fixed * fixed => fixed
5139 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed
(N
);
5143 -- Other cases of multiplication of fixed-point operands. Again
5144 -- we exclude the cases where Treat_Fixed_As_Integer flag is set.
5146 elsif (Is_Fixed_Point_Type
(Ltyp
) or else Is_Fixed_Point_Type
(Rtyp
))
5147 and then not Treat_Fixed_As_Integer
(N
)
5149 if Is_Integer_Type
(Typ
) then
5150 Expand_Multiply_Fixed_By_Fixed_Giving_Integer
(N
);
5152 pragma Assert
(Is_Floating_Point_Type
(Typ
));
5153 Expand_Multiply_Fixed_By_Fixed_Giving_Float
(N
);
5156 -- Mixed-mode operations can appear in a non-static universal
5157 -- context, in which case the integer argument must be converted
5160 elsif Typ
= Universal_Real
5161 and then Is_Integer_Type
(Rtyp
)
5163 Rewrite
(Rop
, Convert_To
(Universal_Real
, Relocate_Node
(Rop
)));
5165 Analyze_And_Resolve
(Rop
, Universal_Real
);
5167 elsif Typ
= Universal_Real
5168 and then Is_Integer_Type
(Ltyp
)
5170 Rewrite
(Lop
, Convert_To
(Universal_Real
, Relocate_Node
(Lop
)));
5172 Analyze_And_Resolve
(Lop
, Universal_Real
);
5174 -- Non-fixed point cases, check software overflow checking required
5176 elsif Is_Signed_Integer_Type
(Etype
(N
)) then
5177 Apply_Arithmetic_Overflow_Check
(N
);
5179 end Expand_N_Op_Multiply
;
5181 --------------------
5182 -- Expand_N_Op_Ne --
5183 --------------------
5185 -- Rewrite node as the negation of an equality operation, and reanalyze.
5186 -- The equality to be used is defined in the same scope and has the same
5187 -- signature. It must be set explicitly because in an instance it may not
5188 -- have the same visibility as in the generic unit.
5190 procedure Expand_N_Op_Ne
(N
: Node_Id
) is
5191 Loc
: constant Source_Ptr
:= Sloc
(N
);
5193 Ne
: constant Entity_Id
:= Entity
(N
);
5196 Binary_Op_Validity_Checks
(N
);
5202 Left_Opnd
=> Left_Opnd
(N
),
5203 Right_Opnd
=> Right_Opnd
(N
)));
5204 Set_Paren_Count
(Right_Opnd
(Neg
), 1);
5206 if Scope
(Ne
) /= Standard_Standard
then
5207 Set_Entity
(Right_Opnd
(Neg
), Corresponding_Equality
(Ne
));
5210 -- For navigation purposes, the inequality is treated as an implicit
5211 -- reference to the corresponding equality. Preserve the Comes_From_
5212 -- source flag so that the proper Xref entry is generated.
5214 Preserve_Comes_From_Source
(Neg
, N
);
5215 Preserve_Comes_From_Source
(Right_Opnd
(Neg
), N
);
5217 Analyze_And_Resolve
(N
, Standard_Boolean
);
5220 ---------------------
5221 -- Expand_N_Op_Not --
5222 ---------------------
5224 -- If the argument is other than a Boolean array type, there is no
5225 -- special expansion required.
5227 -- For the packed case, we call the special routine in Exp_Pakd, except
5228 -- that if the component size is greater than one, we use the standard
5229 -- routine generating a gruesome loop (it is so peculiar to have packed
5230 -- arrays with non-standard Boolean representations anyway, so it does
5231 -- not matter that we do not handle this case efficiently).
5233 -- For the unpacked case (and for the special packed case where we have
5234 -- non standard Booleans, as discussed above), we generate and insert
5235 -- into the tree the following function definition:
5237 -- function Nnnn (A : arr) is
5240 -- for J in a'range loop
5241 -- B (J) := not A (J);
5246 -- Here arr is the actual subtype of the parameter (and hence always
5247 -- constrained). Then we replace the not with a call to this function.
5249 procedure Expand_N_Op_Not
(N
: Node_Id
) is
5250 Loc
: constant Source_Ptr
:= Sloc
(N
);
5251 Typ
: constant Entity_Id
:= Etype
(N
);
5260 Func_Name
: Entity_Id
;
5261 Loop_Statement
: Node_Id
;
5264 Unary_Op_Validity_Checks
(N
);
5266 -- For boolean operand, deal with non-standard booleans
5268 if Is_Boolean_Type
(Typ
) then
5269 Adjust_Condition
(Right_Opnd
(N
));
5270 Set_Etype
(N
, Standard_Boolean
);
5271 Adjust_Result_Type
(N
, Typ
);
5275 -- Only array types need any other processing
5277 if not Is_Array_Type
(Typ
) then
5281 -- Case of array operand. If bit packed, handle it in Exp_Pakd
5283 if Is_Bit_Packed_Array
(Typ
) and then Component_Size
(Typ
) = 1 then
5284 Expand_Packed_Not
(N
);
5288 -- Case of array operand which is not bit-packed. If the context is
5289 -- a safe assignment, call in-place operation, If context is a larger
5290 -- boolean expression in the context of a safe assignment, expansion is
5291 -- done by enclosing operation.
5293 Opnd
:= Relocate_Node
(Right_Opnd
(N
));
5294 Convert_To_Actual_Subtype
(Opnd
);
5295 Arr
:= Etype
(Opnd
);
5296 Ensure_Defined
(Arr
, N
);
5298 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
5299 if Safe_In_Place_Array_Op
(Name
(Parent
(N
)), N
, Empty
) then
5300 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
5303 -- Special case the negation of a binary operation
5305 elsif (Nkind
(Opnd
) = N_Op_And
5306 or else Nkind
(Opnd
) = N_Op_Or
5307 or else Nkind
(Opnd
) = N_Op_Xor
)
5308 and then Safe_In_Place_Array_Op
5309 (Name
(Parent
(N
)), Left_Opnd
(Opnd
), Right_Opnd
(Opnd
))
5311 Build_Boolean_Array_Proc_Call
(Parent
(N
), Opnd
, Empty
);
5315 elsif Nkind
(Parent
(N
)) in N_Binary_Op
5316 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
5319 Op1
: constant Node_Id
:= Left_Opnd
(Parent
(N
));
5320 Op2
: constant Node_Id
:= Right_Opnd
(Parent
(N
));
5321 Lhs
: constant Node_Id
:= Name
(Parent
(Parent
(N
)));
5324 if Safe_In_Place_Array_Op
(Lhs
, Op1
, Op2
) then
5326 and then Nkind
(Op2
) = N_Op_Not
5328 -- (not A) op (not B) can be reduced to a single call
5333 and then Nkind
(Parent
(N
)) = N_Op_Xor
5335 -- A xor (not B) can also be special-cased
5343 A
:= Make_Defining_Identifier
(Loc
, Name_uA
);
5344 B
:= Make_Defining_Identifier
(Loc
, Name_uB
);
5345 J
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
5348 Make_Indexed_Component
(Loc
,
5349 Prefix
=> New_Reference_To
(A
, Loc
),
5350 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
5353 Make_Indexed_Component
(Loc
,
5354 Prefix
=> New_Reference_To
(B
, Loc
),
5355 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
5358 Make_Implicit_Loop_Statement
(N
,
5359 Identifier
=> Empty
,
5362 Make_Iteration_Scheme
(Loc
,
5363 Loop_Parameter_Specification
=>
5364 Make_Loop_Parameter_Specification
(Loc
,
5365 Defining_Identifier
=> J
,
5366 Discrete_Subtype_Definition
=>
5367 Make_Attribute_Reference
(Loc
,
5368 Prefix
=> Make_Identifier
(Loc
, Chars
(A
)),
5369 Attribute_Name
=> Name_Range
))),
5371 Statements
=> New_List
(
5372 Make_Assignment_Statement
(Loc
,
5374 Expression
=> Make_Op_Not
(Loc
, A_J
))));
5376 Func_Name
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('N'));
5377 Set_Is_Inlined
(Func_Name
);
5380 Make_Subprogram_Body
(Loc
,
5382 Make_Function_Specification
(Loc
,
5383 Defining_Unit_Name
=> Func_Name
,
5384 Parameter_Specifications
=> New_List
(
5385 Make_Parameter_Specification
(Loc
,
5386 Defining_Identifier
=> A
,
5387 Parameter_Type
=> New_Reference_To
(Typ
, Loc
))),
5388 Subtype_Mark
=> New_Reference_To
(Typ
, Loc
)),
5390 Declarations
=> New_List
(
5391 Make_Object_Declaration
(Loc
,
5392 Defining_Identifier
=> B
,
5393 Object_Definition
=> New_Reference_To
(Arr
, Loc
))),
5395 Handled_Statement_Sequence
=>
5396 Make_Handled_Sequence_Of_Statements
(Loc
,
5397 Statements
=> New_List
(
5399 Make_Return_Statement
(Loc
,
5401 Make_Identifier
(Loc
, Chars
(B
)))))));
5404 Make_Function_Call
(Loc
,
5405 Name
=> New_Reference_To
(Func_Name
, Loc
),
5406 Parameter_Associations
=> New_List
(Opnd
)));
5408 Analyze_And_Resolve
(N
, Typ
);
5409 end Expand_N_Op_Not
;
5411 --------------------
5412 -- Expand_N_Op_Or --
5413 --------------------
5415 procedure Expand_N_Op_Or
(N
: Node_Id
) is
5416 Typ
: constant Entity_Id
:= Etype
(N
);
5419 Binary_Op_Validity_Checks
(N
);
5421 if Is_Array_Type
(Etype
(N
)) then
5422 Expand_Boolean_Operator
(N
);
5424 elsif Is_Boolean_Type
(Etype
(N
)) then
5425 Adjust_Condition
(Left_Opnd
(N
));
5426 Adjust_Condition
(Right_Opnd
(N
));
5427 Set_Etype
(N
, Standard_Boolean
);
5428 Adjust_Result_Type
(N
, Typ
);
5432 ----------------------
5433 -- Expand_N_Op_Plus --
5434 ----------------------
5436 procedure Expand_N_Op_Plus
(N
: Node_Id
) is
5438 Unary_Op_Validity_Checks
(N
);
5439 end Expand_N_Op_Plus
;
5441 ---------------------
5442 -- Expand_N_Op_Rem --
5443 ---------------------
5445 procedure Expand_N_Op_Rem
(N
: Node_Id
) is
5446 Loc
: constant Source_Ptr
:= Sloc
(N
);
5447 Typ
: constant Entity_Id
:= Etype
(N
);
5449 Left
: constant Node_Id
:= Left_Opnd
(N
);
5450 Right
: constant Node_Id
:= Right_Opnd
(N
);
5461 Binary_Op_Validity_Checks
(N
);
5463 if Is_Integer_Type
(Etype
(N
)) then
5464 Apply_Divide_Check
(N
);
5467 -- Apply optimization x rem 1 = 0. We don't really need that with
5468 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
5469 -- certainly harmless.
5471 if Is_Integer_Type
(Etype
(N
))
5472 and then Compile_Time_Known_Value
(Right
)
5473 and then Expr_Value
(Right
) = Uint_1
5475 Rewrite
(N
, Make_Integer_Literal
(Loc
, 0));
5476 Analyze_And_Resolve
(N
, Typ
);
5480 -- Deal with annoying case of largest negative number remainder
5481 -- minus one. Gigi does not handle this case correctly, because
5482 -- it generates a divide instruction which may trap in this case.
5484 -- In fact the check is quite easy, if the right operand is -1,
5485 -- then the remainder is always 0, and we can just ignore the
5486 -- left operand completely in this case.
5488 Determine_Range
(Right
, ROK
, Rlo
, Rhi
);
5489 Determine_Range
(Left
, LOK
, Llo
, Lhi
);
5491 -- The operand type may be private (e.g. in the expansion of an
5492 -- an intrinsic operation) so we must use the underlying type to
5493 -- get the bounds, and convert the literals explicitly.
5497 (Type_Low_Bound
(Base_Type
(Underlying_Type
(Etype
(Left
)))));
5499 -- Now perform the test, generating code only if needed
5501 if ((not ROK
) or else (Rlo
<= (-1) and then (-1) <= Rhi
))
5503 ((not LOK
) or else (Llo
= LLB
))
5506 Make_Conditional_Expression
(Loc
,
5507 Expressions
=> New_List
(
5509 Left_Opnd
=> Duplicate_Subexpr
(Right
),
5511 Unchecked_Convert_To
(Typ
,
5512 Make_Integer_Literal
(Loc
, -1))),
5514 Unchecked_Convert_To
(Typ
,
5515 Make_Integer_Literal
(Loc
, Uint_0
)),
5517 Relocate_Node
(N
))));
5519 Set_Analyzed
(Next
(Next
(First
(Expressions
(N
)))));
5520 Analyze_And_Resolve
(N
, Typ
);
5522 end Expand_N_Op_Rem
;
5524 -----------------------------
5525 -- Expand_N_Op_Rotate_Left --
5526 -----------------------------
5528 procedure Expand_N_Op_Rotate_Left
(N
: Node_Id
) is
5530 Binary_Op_Validity_Checks
(N
);
5531 end Expand_N_Op_Rotate_Left
;
5533 ------------------------------
5534 -- Expand_N_Op_Rotate_Right --
5535 ------------------------------
5537 procedure Expand_N_Op_Rotate_Right
(N
: Node_Id
) is
5539 Binary_Op_Validity_Checks
(N
);
5540 end Expand_N_Op_Rotate_Right
;
5542 ----------------------------
5543 -- Expand_N_Op_Shift_Left --
5544 ----------------------------
5546 procedure Expand_N_Op_Shift_Left
(N
: Node_Id
) is
5548 Binary_Op_Validity_Checks
(N
);
5549 end Expand_N_Op_Shift_Left
;
5551 -----------------------------
5552 -- Expand_N_Op_Shift_Right --
5553 -----------------------------
5555 procedure Expand_N_Op_Shift_Right
(N
: Node_Id
) is
5557 Binary_Op_Validity_Checks
(N
);
5558 end Expand_N_Op_Shift_Right
;
5560 ----------------------------------------
5561 -- Expand_N_Op_Shift_Right_Arithmetic --
5562 ----------------------------------------
5564 procedure Expand_N_Op_Shift_Right_Arithmetic
(N
: Node_Id
) is
5566 Binary_Op_Validity_Checks
(N
);
5567 end Expand_N_Op_Shift_Right_Arithmetic
;
5569 --------------------------
5570 -- Expand_N_Op_Subtract --
5571 --------------------------
5573 procedure Expand_N_Op_Subtract
(N
: Node_Id
) is
5574 Typ
: constant Entity_Id
:= Etype
(N
);
5577 Binary_Op_Validity_Checks
(N
);
5579 -- N - 0 = N for integer types
5581 if Is_Integer_Type
(Typ
)
5582 and then Compile_Time_Known_Value
(Right_Opnd
(N
))
5583 and then Expr_Value
(Right_Opnd
(N
)) = 0
5585 Rewrite
(N
, Left_Opnd
(N
));
5589 -- Arithemtic overflow checks for signed integer/fixed point types
5591 if Is_Signed_Integer_Type
(Typ
)
5592 or else Is_Fixed_Point_Type
(Typ
)
5594 Apply_Arithmetic_Overflow_Check
(N
);
5596 -- Vax floating-point types case
5598 elsif Vax_Float
(Typ
) then
5599 Expand_Vax_Arith
(N
);
5601 end Expand_N_Op_Subtract
;
5603 ---------------------
5604 -- Expand_N_Op_Xor --
5605 ---------------------
5607 procedure Expand_N_Op_Xor
(N
: Node_Id
) is
5608 Typ
: constant Entity_Id
:= Etype
(N
);
5611 Binary_Op_Validity_Checks
(N
);
5613 if Is_Array_Type
(Etype
(N
)) then
5614 Expand_Boolean_Operator
(N
);
5616 elsif Is_Boolean_Type
(Etype
(N
)) then
5617 Adjust_Condition
(Left_Opnd
(N
));
5618 Adjust_Condition
(Right_Opnd
(N
));
5619 Set_Etype
(N
, Standard_Boolean
);
5620 Adjust_Result_Type
(N
, Typ
);
5622 end Expand_N_Op_Xor
;
5624 ----------------------
5625 -- Expand_N_Or_Else --
5626 ----------------------
5628 -- Expand into conditional expression if Actions present, and also
5629 -- deal with optimizing case of arguments being True or False.
5631 procedure Expand_N_Or_Else
(N
: Node_Id
) is
5632 Loc
: constant Source_Ptr
:= Sloc
(N
);
5633 Typ
: constant Entity_Id
:= Etype
(N
);
5634 Left
: constant Node_Id
:= Left_Opnd
(N
);
5635 Right
: constant Node_Id
:= Right_Opnd
(N
);
5639 -- Deal with non-standard booleans
5641 if Is_Boolean_Type
(Typ
) then
5642 Adjust_Condition
(Left
);
5643 Adjust_Condition
(Right
);
5644 Set_Etype
(N
, Standard_Boolean
);
5647 -- Check for cases of left argument is True or False
5649 if Nkind
(Left
) = N_Identifier
then
5651 -- If left argument is False, change (False or else Right) to Right.
5652 -- Any actions associated with Right will be executed unconditionally
5653 -- and can thus be inserted into the tree unconditionally.
5655 if Entity
(Left
) = Standard_False
then
5656 if Present
(Actions
(N
)) then
5657 Insert_Actions
(N
, Actions
(N
));
5661 Adjust_Result_Type
(N
, Typ
);
5664 -- If left argument is True, change (True and then Right) to
5665 -- True. In this case we can forget the actions associated with
5666 -- Right, since they will never be executed.
5668 elsif Entity
(Left
) = Standard_True
then
5669 Kill_Dead_Code
(Right
);
5670 Kill_Dead_Code
(Actions
(N
));
5671 Rewrite
(N
, New_Occurrence_Of
(Standard_True
, Loc
));
5672 Adjust_Result_Type
(N
, Typ
);
5677 -- If Actions are present, we expand
5679 -- left or else right
5683 -- if left then True else right end
5685 -- with the actions becoming the Else_Actions of the conditional
5686 -- expression. This conditional expression is then further expanded
5687 -- (and will eventually disappear)
5689 if Present
(Actions
(N
)) then
5690 Actlist
:= Actions
(N
);
5692 Make_Conditional_Expression
(Loc
,
5693 Expressions
=> New_List
(
5695 New_Occurrence_Of
(Standard_True
, Loc
),
5698 Set_Else_Actions
(N
, Actlist
);
5699 Analyze_And_Resolve
(N
, Standard_Boolean
);
5700 Adjust_Result_Type
(N
, Typ
);
5704 -- No actions present, check for cases of right argument True/False
5706 if Nkind
(Right
) = N_Identifier
then
5708 -- Change (Left or else False) to Left. Note that we know there
5709 -- are no actions associated with the True operand, since we
5710 -- just checked for this case above.
5712 if Entity
(Right
) = Standard_False
then
5715 -- Change (Left or else True) to True, making sure to preserve
5716 -- any side effects associated with the Left operand.
5718 elsif Entity
(Right
) = Standard_True
then
5719 Remove_Side_Effects
(Left
);
5721 (N
, New_Occurrence_Of
(Standard_True
, Loc
));
5725 Adjust_Result_Type
(N
, Typ
);
5726 end Expand_N_Or_Else
;
5728 -----------------------------------
5729 -- Expand_N_Qualified_Expression --
5730 -----------------------------------
5732 procedure Expand_N_Qualified_Expression
(N
: Node_Id
) is
5733 Operand
: constant Node_Id
:= Expression
(N
);
5734 Target_Type
: constant Entity_Id
:= Entity
(Subtype_Mark
(N
));
5737 Apply_Constraint_Check
(Operand
, Target_Type
, No_Sliding
=> True);
5738 end Expand_N_Qualified_Expression
;
5740 ---------------------------------
5741 -- Expand_N_Selected_Component --
5742 ---------------------------------
5744 -- If the selector is a discriminant of a concurrent object, rewrite the
5745 -- prefix to denote the corresponding record type.
5747 procedure Expand_N_Selected_Component
(N
: Node_Id
) is
5748 Loc
: constant Source_Ptr
:= Sloc
(N
);
5749 Par
: constant Node_Id
:= Parent
(N
);
5750 P
: constant Node_Id
:= Prefix
(N
);
5751 Ptyp
: Entity_Id
:= Underlying_Type
(Etype
(P
));
5756 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean;
5757 -- Gigi needs a temporary for prefixes that depend on a discriminant,
5758 -- unless the context of an assignment can provide size information.
5759 -- Don't we have a general routine that does this???
5761 -----------------------
5762 -- In_Left_Hand_Side --
5763 -----------------------
5765 function In_Left_Hand_Side
(Comp
: Node_Id
) return Boolean is
5767 return (Nkind
(Parent
(Comp
)) = N_Assignment_Statement
5768 and then Comp
= Name
(Parent
(Comp
)))
5769 or else (Present
(Parent
(Comp
))
5770 and then Nkind
(Parent
(Comp
)) in N_Subexpr
5771 and then In_Left_Hand_Side
(Parent
(Comp
)));
5772 end In_Left_Hand_Side
;
5774 -- Start of processing for Expand_N_Selected_Component
5777 -- Insert explicit dereference if required
5779 if Is_Access_Type
(Ptyp
) then
5780 Insert_Explicit_Dereference
(P
);
5781 Analyze_And_Resolve
(P
, Designated_Type
(Ptyp
));
5783 if Ekind
(Etype
(P
)) = E_Private_Subtype
5784 and then Is_For_Access_Subtype
(Etype
(P
))
5786 Set_Etype
(P
, Base_Type
(Etype
(P
)));
5792 -- Deal with discriminant check required
5794 if Do_Discriminant_Check
(N
) then
5796 -- Present the discrminant checking function to the backend,
5797 -- so that it can inline the call to the function.
5800 (Discriminant_Checking_Func
5801 (Original_Record_Component
(Entity
(Selector_Name
(N
)))));
5803 -- Now reset the flag and generate the call
5805 Set_Do_Discriminant_Check
(N
, False);
5806 Generate_Discriminant_Check
(N
);
5809 -- Gigi cannot handle unchecked conversions that are the prefix of a
5810 -- selected component with discriminants. This must be checked during
5811 -- expansion, because during analysis the type of the selector is not
5812 -- known at the point the prefix is analyzed. If the conversion is the
5813 -- target of an assignment, then we cannot force the evaluation.
5815 if Nkind
(Prefix
(N
)) = N_Unchecked_Type_Conversion
5816 and then Has_Discriminants
(Etype
(N
))
5817 and then not In_Left_Hand_Side
(N
)
5819 Force_Evaluation
(Prefix
(N
));
5822 -- Remaining processing applies only if selector is a discriminant
5824 if Ekind
(Entity
(Selector_Name
(N
))) = E_Discriminant
then
5826 -- If the selector is a discriminant of a constrained record type,
5827 -- we may be able to rewrite the expression with the actual value
5828 -- of the discriminant, a useful optimization in some cases.
5830 if Is_Record_Type
(Ptyp
)
5831 and then Has_Discriminants
(Ptyp
)
5832 and then Is_Constrained
(Ptyp
)
5834 -- Do this optimization for discrete types only, and not for
5835 -- access types (access discriminants get us into trouble!)
5837 if not Is_Discrete_Type
(Etype
(N
)) then
5840 -- Don't do this on the left hand of an assignment statement.
5841 -- Normally one would think that references like this would
5842 -- not occur, but they do in generated code, and mean that
5843 -- we really do want to assign the discriminant!
5845 elsif Nkind
(Par
) = N_Assignment_Statement
5846 and then Name
(Par
) = N
5850 -- Don't do this optimization for the prefix of an attribute
5851 -- or the operand of an object renaming declaration since these
5852 -- are contexts where we do not want the value anyway.
5854 elsif (Nkind
(Par
) = N_Attribute_Reference
5855 and then Prefix
(Par
) = N
)
5856 or else Is_Renamed_Object
(N
)
5860 -- Don't do this optimization if we are within the code for a
5861 -- discriminant check, since the whole point of such a check may
5862 -- be to verify the condition on which the code below depends!
5864 elsif Is_In_Discriminant_Check
(N
) then
5867 -- Green light to see if we can do the optimization. There is
5868 -- still one condition that inhibits the optimization below
5869 -- but now is the time to check the particular discriminant.
5872 -- Loop through discriminants to find the matching
5873 -- discriminant constraint to see if we can copy it.
5875 Disc
:= First_Discriminant
(Ptyp
);
5876 Dcon
:= First_Elmt
(Discriminant_Constraint
(Ptyp
));
5877 Discr_Loop
: while Present
(Dcon
) loop
5879 -- Check if this is the matching discriminant
5881 if Disc
= Entity
(Selector_Name
(N
)) then
5883 -- Here we have the matching discriminant. Check for
5884 -- the case of a discriminant of a component that is
5885 -- constrained by an outer discriminant, which cannot
5886 -- be optimized away.
5889 Denotes_Discriminant
5890 (Node
(Dcon
), Check_Protected
=> True)
5894 -- In the context of a case statement, the expression
5895 -- may have the base type of the discriminant, and we
5896 -- need to preserve the constraint to avoid spurious
5897 -- errors on missing cases.
5899 elsif Nkind
(Parent
(N
)) = N_Case_Statement
5900 and then Etype
(Node
(Dcon
)) /= Etype
(Disc
)
5903 Make_Qualified_Expression
(Loc
,
5905 New_Occurrence_Of
(Etype
(Disc
), Loc
),
5907 New_Copy_Tree
(Node
(Dcon
))));
5908 Analyze_And_Resolve
(N
, Etype
(Disc
));
5910 -- In case that comes out as a static expression,
5911 -- reset it (a selected component is never static).
5913 Set_Is_Static_Expression
(N
, False);
5916 -- Otherwise we can just copy the constraint, but the
5917 -- result is certainly not static! In some cases the
5918 -- discriminant constraint has been analyzed in the
5919 -- context of the original subtype indication, but for
5920 -- itypes the constraint might not have been analyzed
5921 -- yet, and this must be done now.
5924 Rewrite
(N
, New_Copy_Tree
(Node
(Dcon
)));
5925 Analyze_And_Resolve
(N
);
5926 Set_Is_Static_Expression
(N
, False);
5932 Next_Discriminant
(Disc
);
5933 end loop Discr_Loop
;
5935 -- Note: the above loop should always find a matching
5936 -- discriminant, but if it does not, we just missed an
5937 -- optimization due to some glitch (perhaps a previous
5938 -- error), so ignore.
5943 -- The only remaining processing is in the case of a discriminant of
5944 -- a concurrent object, where we rewrite the prefix to denote the
5945 -- corresponding record type. If the type is derived and has renamed
5946 -- discriminants, use corresponding discriminant, which is the one
5947 -- that appears in the corresponding record.
5949 if not Is_Concurrent_Type
(Ptyp
) then
5953 Disc
:= Entity
(Selector_Name
(N
));
5955 if Is_Derived_Type
(Ptyp
)
5956 and then Present
(Corresponding_Discriminant
(Disc
))
5958 Disc
:= Corresponding_Discriminant
(Disc
);
5962 Make_Selected_Component
(Loc
,
5964 Unchecked_Convert_To
(Corresponding_Record_Type
(Ptyp
),
5966 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Disc
)));
5971 end Expand_N_Selected_Component
;
5973 --------------------
5974 -- Expand_N_Slice --
5975 --------------------
5977 procedure Expand_N_Slice
(N
: Node_Id
) is
5978 Loc
: constant Source_Ptr
:= Sloc
(N
);
5979 Typ
: constant Entity_Id
:= Etype
(N
);
5980 Pfx
: constant Node_Id
:= Prefix
(N
);
5981 Ptp
: Entity_Id
:= Etype
(Pfx
);
5983 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean;
5984 -- Check whether the argument is an actual for a procedure call,
5985 -- in which case the expansion of a bit-packed slice is deferred
5986 -- until the call itself is expanded. The reason this is required
5987 -- is that we might have an IN OUT or OUT parameter, and the copy out
5988 -- is essential, and that copy out would be missed if we created a
5989 -- temporary here in Expand_N_Slice. Note that we don't bother
5990 -- to test specifically for an IN OUT or OUT mode parameter, since it
5991 -- is a bit tricky to do, and it is harmless to defer expansion
5992 -- in the IN case, since the call processing will still generate the
5993 -- appropriate copy in operation, which will take care of the slice.
5995 procedure Make_Temporary
;
5996 -- Create a named variable for the value of the slice, in
5997 -- cases where the back-end cannot handle it properly, e.g.
5998 -- when packed types or unaligned slices are involved.
6000 -------------------------
6001 -- Is_Procedure_Actual --
6002 -------------------------
6004 function Is_Procedure_Actual
(N
: Node_Id
) return Boolean is
6005 Par
: Node_Id
:= Parent
(N
);
6009 -- If our parent is a procedure call we can return
6011 if Nkind
(Par
) = N_Procedure_Call_Statement
then
6014 -- If our parent is a type conversion, keep climbing the
6015 -- tree, since a type conversion can be a procedure actual.
6016 -- Also keep climbing if parameter association or a qualified
6017 -- expression, since these are additional cases that do can
6018 -- appear on procedure actuals.
6020 elsif Nkind
(Par
) = N_Type_Conversion
6021 or else Nkind
(Par
) = N_Parameter_Association
6022 or else Nkind
(Par
) = N_Qualified_Expression
6024 Par
:= Parent
(Par
);
6026 -- Any other case is not what we are looking for
6032 end Is_Procedure_Actual
;
6034 --------------------
6035 -- Make_Temporary --
6036 --------------------
6038 procedure Make_Temporary
is
6040 Ent
: constant Entity_Id
:=
6041 Make_Defining_Identifier
(Loc
, New_Internal_Name
('T'));
6044 Make_Object_Declaration
(Loc
,
6045 Defining_Identifier
=> Ent
,
6046 Object_Definition
=> New_Occurrence_Of
(Typ
, Loc
));
6048 Set_No_Initialization
(Decl
);
6050 Insert_Actions
(N
, New_List
(
6052 Make_Assignment_Statement
(Loc
,
6053 Name
=> New_Occurrence_Of
(Ent
, Loc
),
6054 Expression
=> Relocate_Node
(N
))));
6056 Rewrite
(N
, New_Occurrence_Of
(Ent
, Loc
));
6057 Analyze_And_Resolve
(N
, Typ
);
6060 -- Start of processing for Expand_N_Slice
6063 -- Special handling for access types
6065 if Is_Access_Type
(Ptp
) then
6067 Ptp
:= Designated_Type
(Ptp
);
6070 Make_Explicit_Dereference
(Sloc
(N
),
6071 Prefix
=> Relocate_Node
(Pfx
)));
6073 Analyze_And_Resolve
(Pfx
, Ptp
);
6076 -- Range checks are potentially also needed for cases involving
6077 -- a slice indexed by a subtype indication, but Do_Range_Check
6078 -- can currently only be set for expressions ???
6080 if not Index_Checks_Suppressed
(Ptp
)
6081 and then (not Is_Entity_Name
(Pfx
)
6082 or else not Index_Checks_Suppressed
(Entity
(Pfx
)))
6083 and then Nkind
(Discrete_Range
(N
)) /= N_Subtype_Indication
6085 Enable_Range_Check
(Discrete_Range
(N
));
6088 -- The remaining case to be handled is packed slices. We can leave
6089 -- packed slices as they are in the following situations:
6091 -- 1. Right or left side of an assignment (we can handle this
6092 -- situation correctly in the assignment statement expansion).
6094 -- 2. Prefix of indexed component (the slide is optimized away
6095 -- in this case, see the start of Expand_N_Slice.
6097 -- 3. Object renaming declaration, since we want the name of
6098 -- the slice, not the value.
6100 -- 4. Argument to procedure call, since copy-in/copy-out handling
6101 -- may be required, and this is handled in the expansion of
6104 -- 5. Prefix of an address attribute (this is an error which
6105 -- is caught elsewhere, and the expansion would intefere
6106 -- with generating the error message).
6108 if not Is_Packed
(Typ
) then
6110 -- Apply transformation for actuals of a function call,
6111 -- where Expand_Actuals is not used.
6113 if Nkind
(Parent
(N
)) = N_Function_Call
6114 and then Is_Possibly_Unaligned_Slice
(N
)
6119 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
6120 or else (Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
6121 and then Parent
(N
) = Name
(Parent
(Parent
(N
))))
6125 elsif Nkind
(Parent
(N
)) = N_Indexed_Component
6126 or else Is_Renamed_Object
(N
)
6127 or else Is_Procedure_Actual
(N
)
6131 elsif Nkind
(Parent
(N
)) = N_Attribute_Reference
6132 and then Attribute_Name
(Parent
(N
)) = Name_Address
6141 ------------------------------
6142 -- Expand_N_Type_Conversion --
6143 ------------------------------
6145 procedure Expand_N_Type_Conversion
(N
: Node_Id
) is
6146 Loc
: constant Source_Ptr
:= Sloc
(N
);
6147 Operand
: constant Node_Id
:= Expression
(N
);
6148 Target_Type
: constant Entity_Id
:= Etype
(N
);
6149 Operand_Type
: Entity_Id
:= Etype
(Operand
);
6151 procedure Handle_Changed_Representation
;
6152 -- This is called in the case of record and array type conversions
6153 -- to see if there is a change of representation to be handled.
6154 -- Change of representation is actually handled at the assignment
6155 -- statement level, and what this procedure does is rewrite node N
6156 -- conversion as an assignment to temporary. If there is no change
6157 -- of representation, then the conversion node is unchanged.
6159 procedure Real_Range_Check
;
6160 -- Handles generation of range check for real target value
6162 -----------------------------------
6163 -- Handle_Changed_Representation --
6164 -----------------------------------
6166 procedure Handle_Changed_Representation
is
6175 -- Nothing to do if no change of representation
6177 if Same_Representation
(Operand_Type
, Target_Type
) then
6180 -- The real change of representation work is done by the assignment
6181 -- statement processing. So if this type conversion is appearing as
6182 -- the expression of an assignment statement, nothing needs to be
6183 -- done to the conversion.
6185 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
then
6188 -- Otherwise we need to generate a temporary variable, and do the
6189 -- change of representation assignment into that temporary variable.
6190 -- The conversion is then replaced by a reference to this variable.
6195 -- If type is unconstrained we have to add a constraint,
6196 -- copied from the actual value of the left hand side.
6198 if not Is_Constrained
(Target_Type
) then
6199 if Has_Discriminants
(Operand_Type
) then
6200 Disc
:= First_Discriminant
(Operand_Type
);
6202 if Disc
/= First_Stored_Discriminant
(Operand_Type
) then
6203 Disc
:= First_Stored_Discriminant
(Operand_Type
);
6207 while Present
(Disc
) loop
6209 Make_Selected_Component
(Loc
,
6210 Prefix
=> Duplicate_Subexpr_Move_Checks
(Operand
),
6212 Make_Identifier
(Loc
, Chars
(Disc
))));
6213 Next_Discriminant
(Disc
);
6216 elsif Is_Array_Type
(Operand_Type
) then
6217 N_Ix
:= First_Index
(Target_Type
);
6220 for J
in 1 .. Number_Dimensions
(Operand_Type
) loop
6222 -- We convert the bounds explicitly. We use an unchecked
6223 -- conversion because bounds checks are done elsewhere.
6228 Unchecked_Convert_To
(Etype
(N_Ix
),
6229 Make_Attribute_Reference
(Loc
,
6231 Duplicate_Subexpr_No_Checks
6232 (Operand
, Name_Req
=> True),
6233 Attribute_Name
=> Name_First
,
6234 Expressions
=> New_List
(
6235 Make_Integer_Literal
(Loc
, J
)))),
6238 Unchecked_Convert_To
(Etype
(N_Ix
),
6239 Make_Attribute_Reference
(Loc
,
6241 Duplicate_Subexpr_No_Checks
6242 (Operand
, Name_Req
=> True),
6243 Attribute_Name
=> Name_Last
,
6244 Expressions
=> New_List
(
6245 Make_Integer_Literal
(Loc
, J
))))));
6252 Odef
:= New_Occurrence_Of
(Target_Type
, Loc
);
6254 if Present
(Cons
) then
6256 Make_Subtype_Indication
(Loc
,
6257 Subtype_Mark
=> Odef
,
6259 Make_Index_Or_Discriminant_Constraint
(Loc
,
6260 Constraints
=> Cons
));
6263 Temp
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('C'));
6265 Make_Object_Declaration
(Loc
,
6266 Defining_Identifier
=> Temp
,
6267 Object_Definition
=> Odef
);
6269 Set_No_Initialization
(Decl
, True);
6271 -- Insert required actions. It is essential to suppress checks
6272 -- since we have suppressed default initialization, which means
6273 -- that the variable we create may have no discriminants.
6278 Make_Assignment_Statement
(Loc
,
6279 Name
=> New_Occurrence_Of
(Temp
, Loc
),
6280 Expression
=> Relocate_Node
(N
))),
6281 Suppress
=> All_Checks
);
6283 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
6286 end Handle_Changed_Representation
;
6288 ----------------------
6289 -- Real_Range_Check --
6290 ----------------------
6292 -- Case of conversions to floating-point or fixed-point. If range
6293 -- checks are enabled and the target type has a range constraint,
6300 -- Tnn : typ'Base := typ'Base (x);
6301 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
6304 -- This is necessary when there is a conversion of integer to float
6305 -- or to fixed-point to ensure that the correct checks are made. It
6306 -- is not necessary for float to float where it is enough to simply
6307 -- set the Do_Range_Check flag.
6309 procedure Real_Range_Check
is
6310 Btyp
: constant Entity_Id
:= Base_Type
(Target_Type
);
6311 Lo
: constant Node_Id
:= Type_Low_Bound
(Target_Type
);
6312 Hi
: constant Node_Id
:= Type_High_Bound
(Target_Type
);
6313 Xtyp
: constant Entity_Id
:= Etype
(Operand
);
6318 -- Nothing to do if conversion was rewritten
6320 if Nkind
(N
) /= N_Type_Conversion
then
6324 -- Nothing to do if range checks suppressed, or target has the
6325 -- same range as the base type (or is the base type).
6327 if Range_Checks_Suppressed
(Target_Type
)
6328 or else (Lo
= Type_Low_Bound
(Btyp
)
6330 Hi
= Type_High_Bound
(Btyp
))
6335 -- Nothing to do if expression is an entity on which checks
6336 -- have been suppressed.
6338 if Is_Entity_Name
(Operand
)
6339 and then Range_Checks_Suppressed
(Entity
(Operand
))
6344 -- Nothing to do if bounds are all static and we can tell that
6345 -- the expression is within the bounds of the target. Note that
6346 -- if the operand is of an unconstrained floating-point type,
6347 -- then we do not trust it to be in range (might be infinite)
6350 S_Lo
: constant Node_Id
:= Type_Low_Bound
(Xtyp
);
6351 S_Hi
: constant Node_Id
:= Type_High_Bound
(Xtyp
);
6354 if (not Is_Floating_Point_Type
(Xtyp
)
6355 or else Is_Constrained
(Xtyp
))
6356 and then Compile_Time_Known_Value
(S_Lo
)
6357 and then Compile_Time_Known_Value
(S_Hi
)
6358 and then Compile_Time_Known_Value
(Hi
)
6359 and then Compile_Time_Known_Value
(Lo
)
6362 D_Lov
: constant Ureal
:= Expr_Value_R
(Lo
);
6363 D_Hiv
: constant Ureal
:= Expr_Value_R
(Hi
);
6368 if Is_Real_Type
(Xtyp
) then
6369 S_Lov
:= Expr_Value_R
(S_Lo
);
6370 S_Hiv
:= Expr_Value_R
(S_Hi
);
6372 S_Lov
:= UR_From_Uint
(Expr_Value
(S_Lo
));
6373 S_Hiv
:= UR_From_Uint
(Expr_Value
(S_Hi
));
6377 and then S_Lov
>= D_Lov
6378 and then S_Hiv
<= D_Hiv
6380 Set_Do_Range_Check
(Operand
, False);
6387 -- For float to float conversions, we are done
6389 if Is_Floating_Point_Type
(Xtyp
)
6391 Is_Floating_Point_Type
(Btyp
)
6396 -- Otherwise rewrite the conversion as described above
6398 Conv
:= Relocate_Node
(N
);
6400 (Subtype_Mark
(Conv
), New_Occurrence_Of
(Btyp
, Loc
));
6401 Set_Etype
(Conv
, Btyp
);
6403 -- Enable overflow except in the case of integer to float
6404 -- conversions, where it is never required, since we can
6405 -- never have overflow in this case.
6407 if not Is_Integer_Type
(Etype
(Operand
)) then
6408 Enable_Overflow_Check
(Conv
);
6412 Make_Defining_Identifier
(Loc
,
6413 Chars
=> New_Internal_Name
('T'));
6415 Insert_Actions
(N
, New_List
(
6416 Make_Object_Declaration
(Loc
,
6417 Defining_Identifier
=> Tnn
,
6418 Object_Definition
=> New_Occurrence_Of
(Btyp
, Loc
),
6419 Expression
=> Conv
),
6421 Make_Raise_Constraint_Error
(Loc
,
6426 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
6428 Make_Attribute_Reference
(Loc
,
6429 Attribute_Name
=> Name_First
,
6431 New_Occurrence_Of
(Target_Type
, Loc
))),
6435 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
6437 Make_Attribute_Reference
(Loc
,
6438 Attribute_Name
=> Name_Last
,
6440 New_Occurrence_Of
(Target_Type
, Loc
)))),
6441 Reason
=> CE_Range_Check_Failed
)));
6443 Rewrite
(N
, New_Occurrence_Of
(Tnn
, Loc
));
6444 Analyze_And_Resolve
(N
, Btyp
);
6445 end Real_Range_Check
;
6447 -- Start of processing for Expand_N_Type_Conversion
6450 -- Nothing at all to do if conversion is to the identical type
6451 -- so remove the conversion completely, it is useless.
6453 if Operand_Type
= Target_Type
then
6454 Rewrite
(N
, Relocate_Node
(Operand
));
6458 -- Deal with Vax floating-point cases
6460 if Vax_Float
(Operand_Type
) or else Vax_Float
(Target_Type
) then
6461 Expand_Vax_Conversion
(N
);
6465 -- Nothing to do if this is the second argument of read. This
6466 -- is a "backwards" conversion that will be handled by the
6467 -- specialized code in attribute processing.
6469 if Nkind
(Parent
(N
)) = N_Attribute_Reference
6470 and then Attribute_Name
(Parent
(N
)) = Name_Read
6471 and then Next
(First
(Expressions
(Parent
(N
)))) = N
6476 -- Here if we may need to expand conversion
6478 -- Special case of converting from non-standard boolean type
6480 if Is_Boolean_Type
(Operand_Type
)
6481 and then (Nonzero_Is_True
(Operand_Type
))
6483 Adjust_Condition
(Operand
);
6484 Set_Etype
(Operand
, Standard_Boolean
);
6485 Operand_Type
:= Standard_Boolean
;
6488 -- Case of converting to an access type
6490 if Is_Access_Type
(Target_Type
) then
6492 -- Apply an accessibility check if the operand is an
6493 -- access parameter. Note that other checks may still
6494 -- need to be applied below (such as tagged type checks).
6496 if Is_Entity_Name
(Operand
)
6497 and then Ekind
(Entity
(Operand
)) in Formal_Kind
6498 and then Ekind
(Etype
(Operand
)) = E_Anonymous_Access_Type
6500 Apply_Accessibility_Check
(Operand
, Target_Type
);
6502 -- If the level of the operand type is statically deeper
6503 -- then the level of the target type, then force Program_Error.
6504 -- Note that this can only occur for cases where the attribute
6505 -- is within the body of an instantiation (otherwise the
6506 -- conversion will already have been rejected as illegal).
6507 -- Note: warnings are issued by the analyzer for the instance
6510 elsif In_Instance_Body
6511 and then Type_Access_Level
(Operand_Type
) >
6512 Type_Access_Level
(Target_Type
)
6515 Make_Raise_Program_Error
(Sloc
(N
),
6516 Reason
=> PE_Accessibility_Check_Failed
));
6517 Set_Etype
(N
, Target_Type
);
6519 -- When the operand is a selected access discriminant
6520 -- the check needs to be made against the level of the
6521 -- object denoted by the prefix of the selected name.
6522 -- Force Program_Error for this case as well (this
6523 -- accessibility violation can only happen if within
6524 -- the body of an instantiation).
6526 elsif In_Instance_Body
6527 and then Ekind
(Operand_Type
) = E_Anonymous_Access_Type
6528 and then Nkind
(Operand
) = N_Selected_Component
6529 and then Object_Access_Level
(Operand
) >
6530 Type_Access_Level
(Target_Type
)
6533 Make_Raise_Program_Error
(Sloc
(N
),
6534 Reason
=> PE_Accessibility_Check_Failed
));
6535 Set_Etype
(N
, Target_Type
);
6539 -- Case of conversions of tagged types and access to tagged types
6541 -- When needed, that is to say when the expression is class-wide,
6542 -- Add runtime a tag check for (strict) downward conversion by using
6543 -- the membership test, generating:
6545 -- [constraint_error when Operand not in Target_Type'Class]
6547 -- or in the access type case
6549 -- [constraint_error
6550 -- when Operand /= null
6551 -- and then Operand.all not in
6552 -- Designated_Type (Target_Type)'Class]
6554 if (Is_Access_Type
(Target_Type
)
6555 and then Is_Tagged_Type
(Designated_Type
(Target_Type
)))
6556 or else Is_Tagged_Type
(Target_Type
)
6558 -- Do not do any expansion in the access type case if the
6559 -- parent is a renaming, since this is an error situation
6560 -- which will be caught by Sem_Ch8, and the expansion can
6561 -- intefere with this error check.
6563 if Is_Access_Type
(Target_Type
)
6564 and then Is_Renamed_Object
(N
)
6569 -- Oherwise, proceed with processing tagged conversion
6572 Actual_Operand_Type
: Entity_Id
;
6573 Actual_Target_Type
: Entity_Id
;
6578 if Is_Access_Type
(Target_Type
) then
6579 Actual_Operand_Type
:= Designated_Type
(Operand_Type
);
6580 Actual_Target_Type
:= Designated_Type
(Target_Type
);
6583 Actual_Operand_Type
:= Operand_Type
;
6584 Actual_Target_Type
:= Target_Type
;
6587 if Is_Class_Wide_Type
(Actual_Operand_Type
)
6588 and then Root_Type
(Actual_Operand_Type
) /= Actual_Target_Type
6589 and then Is_Ancestor
6590 (Root_Type
(Actual_Operand_Type
),
6592 and then not Tag_Checks_Suppressed
(Actual_Target_Type
)
6594 -- The conversion is valid for any descendant of the
6597 Actual_Target_Type
:= Class_Wide_Type
(Actual_Target_Type
);
6599 if Is_Access_Type
(Target_Type
) then
6604 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
6605 Right_Opnd
=> Make_Null
(Loc
)),
6610 Make_Explicit_Dereference
(Loc
,
6612 Duplicate_Subexpr_No_Checks
(Operand
)),
6614 New_Reference_To
(Actual_Target_Type
, Loc
)));
6619 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Operand
),
6621 New_Reference_To
(Actual_Target_Type
, Loc
));
6625 Make_Raise_Constraint_Error
(Loc
,
6627 Reason
=> CE_Tag_Check_Failed
));
6633 Make_Unchecked_Type_Conversion
(Loc
,
6634 Subtype_Mark
=> New_Occurrence_Of
(Target_Type
, Loc
),
6635 Expression
=> Relocate_Node
(Expression
(N
)));
6637 Analyze_And_Resolve
(N
, Target_Type
);
6642 -- Case of other access type conversions
6644 elsif Is_Access_Type
(Target_Type
) then
6645 Apply_Constraint_Check
(Operand
, Target_Type
);
6647 -- Case of conversions from a fixed-point type
6649 -- These conversions require special expansion and processing, found
6650 -- in the Exp_Fixd package. We ignore cases where Conversion_OK is
6651 -- set, since from a semantic point of view, these are simple integer
6652 -- conversions, which do not need further processing.
6654 elsif Is_Fixed_Point_Type
(Operand_Type
)
6655 and then not Conversion_OK
(N
)
6657 -- We should never see universal fixed at this case, since the
6658 -- expansion of the constituent divide or multiply should have
6659 -- eliminated the explicit mention of universal fixed.
6661 pragma Assert
(Operand_Type
/= Universal_Fixed
);
6663 -- Check for special case of the conversion to universal real
6664 -- that occurs as a result of the use of a round attribute.
6665 -- In this case, the real type for the conversion is taken
6666 -- from the target type of the Round attribute and the
6667 -- result must be marked as rounded.
6669 if Target_Type
= Universal_Real
6670 and then Nkind
(Parent
(N
)) = N_Attribute_Reference
6671 and then Attribute_Name
(Parent
(N
)) = Name_Round
6673 Set_Rounded_Result
(N
);
6674 Set_Etype
(N
, Etype
(Parent
(N
)));
6677 -- Otherwise do correct fixed-conversion, but skip these if the
6678 -- Conversion_OK flag is set, because from a semantic point of
6679 -- view these are simple integer conversions needing no further
6680 -- processing (the backend will simply treat them as integers)
6682 if not Conversion_OK
(N
) then
6683 if Is_Fixed_Point_Type
(Etype
(N
)) then
6684 Expand_Convert_Fixed_To_Fixed
(N
);
6687 elsif Is_Integer_Type
(Etype
(N
)) then
6688 Expand_Convert_Fixed_To_Integer
(N
);
6691 pragma Assert
(Is_Floating_Point_Type
(Etype
(N
)));
6692 Expand_Convert_Fixed_To_Float
(N
);
6697 -- Case of conversions to a fixed-point type
6699 -- These conversions require special expansion and processing, found
6700 -- in the Exp_Fixd package. Again, ignore cases where Conversion_OK
6701 -- is set, since from a semantic point of view, these are simple
6702 -- integer conversions, which do not need further processing.
6704 elsif Is_Fixed_Point_Type
(Target_Type
)
6705 and then not Conversion_OK
(N
)
6707 if Is_Integer_Type
(Operand_Type
) then
6708 Expand_Convert_Integer_To_Fixed
(N
);
6711 pragma Assert
(Is_Floating_Point_Type
(Operand_Type
));
6712 Expand_Convert_Float_To_Fixed
(N
);
6716 -- Case of float-to-integer conversions
6718 -- We also handle float-to-fixed conversions with Conversion_OK set
6719 -- since semantically the fixed-point target is treated as though it
6720 -- were an integer in such cases.
6722 elsif Is_Floating_Point_Type
(Operand_Type
)
6724 (Is_Integer_Type
(Target_Type
)
6726 (Is_Fixed_Point_Type
(Target_Type
) and then Conversion_OK
(N
)))
6728 -- Special processing required if the conversion is the expression
6729 -- of a Truncation attribute reference. In this case we replace:
6731 -- ityp (ftyp'Truncation (x))
6737 -- with the Float_Truncate flag set. This is clearly more efficient
6739 if Nkind
(Operand
) = N_Attribute_Reference
6740 and then Attribute_Name
(Operand
) = Name_Truncation
6743 Relocate_Node
(First
(Expressions
(Operand
))));
6744 Set_Float_Truncate
(N
, True);
6747 -- One more check here, gcc is still not able to do conversions of
6748 -- this type with proper overflow checking, and so gigi is doing an
6749 -- approximation of what is required by doing floating-point compares
6750 -- with the end-point. But that can lose precision in some cases, and
6751 -- give a wrong result. Converting the operand to Long_Long_Float is
6752 -- helpful, but still does not catch all cases with 64-bit integers
6753 -- on targets with only 64-bit floats ???
6755 if Do_Range_Check
(Operand
) then
6757 Make_Type_Conversion
(Loc
,
6759 New_Occurrence_Of
(Standard_Long_Long_Float
, Loc
),
6761 Relocate_Node
(Operand
)));
6763 Set_Etype
(Operand
, Standard_Long_Long_Float
);
6764 Enable_Range_Check
(Operand
);
6765 Set_Do_Range_Check
(Expression
(Operand
), False);
6768 -- Case of array conversions
6770 -- Expansion of array conversions, add required length/range checks
6771 -- but only do this if there is no change of representation. For
6772 -- handling of this case, see Handle_Changed_Representation.
6774 elsif Is_Array_Type
(Target_Type
) then
6776 if Is_Constrained
(Target_Type
) then
6777 Apply_Length_Check
(Operand
, Target_Type
);
6779 Apply_Range_Check
(Operand
, Target_Type
);
6782 Handle_Changed_Representation
;
6784 -- Case of conversions of discriminated types
6786 -- Add required discriminant checks if target is constrained. Again
6787 -- this change is skipped if we have a change of representation.
6789 elsif Has_Discriminants
(Target_Type
)
6790 and then Is_Constrained
(Target_Type
)
6792 Apply_Discriminant_Check
(Operand
, Target_Type
);
6793 Handle_Changed_Representation
;
6795 -- Case of all other record conversions. The only processing required
6796 -- is to check for a change of representation requiring the special
6797 -- assignment processing.
6799 elsif Is_Record_Type
(Target_Type
) then
6801 -- Ada 2005 (AI-216): Program_Error is raised when converting from
6802 -- a derived Unchecked_Union type to an unconstrained non-Unchecked_
6803 -- Union type if the operand lacks inferable discriminants.
6805 if Is_Derived_Type
(Operand_Type
)
6806 and then Is_Unchecked_Union
(Base_Type
(Operand_Type
))
6807 and then not Is_Constrained
(Target_Type
)
6808 and then not Is_Unchecked_Union
(Base_Type
(Target_Type
))
6809 and then not Has_Inferable_Discriminants
(Operand
)
6811 -- To prevent Gigi from generating illegal code, we make a
6812 -- Program_Error node, but we give it the target type of the
6816 PE
: constant Node_Id
:= Make_Raise_Program_Error
(Loc
,
6817 Reason
=> PE_Unchecked_Union_Restriction
);
6820 Set_Etype
(PE
, Target_Type
);
6825 Handle_Changed_Representation
;
6828 -- Case of conversions of enumeration types
6830 elsif Is_Enumeration_Type
(Target_Type
) then
6832 -- Special processing is required if there is a change of
6833 -- representation (from enumeration representation clauses)
6835 if not Same_Representation
(Target_Type
, Operand_Type
) then
6837 -- Convert: x(y) to x'val (ytyp'val (y))
6840 Make_Attribute_Reference
(Loc
,
6841 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
6842 Attribute_Name
=> Name_Val
,
6843 Expressions
=> New_List
(
6844 Make_Attribute_Reference
(Loc
,
6845 Prefix
=> New_Occurrence_Of
(Operand_Type
, Loc
),
6846 Attribute_Name
=> Name_Pos
,
6847 Expressions
=> New_List
(Operand
)))));
6849 Analyze_And_Resolve
(N
, Target_Type
);
6852 -- Case of conversions to floating-point
6854 elsif Is_Floating_Point_Type
(Target_Type
) then
6857 -- The remaining cases require no front end processing
6863 -- At this stage, either the conversion node has been transformed
6864 -- into some other equivalent expression, or left as a conversion
6865 -- that can be handled by Gigi. The conversions that Gigi can handle
6866 -- are the following:
6868 -- Conversions with no change of representation or type
6870 -- Numeric conversions involving integer values, floating-point
6871 -- values, and fixed-point values. Fixed-point values are allowed
6872 -- only if Conversion_OK is set, i.e. if the fixed-point values
6873 -- are to be treated as integers.
6875 -- No other conversions should be passed to Gigi
6877 -- Check: are these rules stated in sinfo??? if so, why restate here???
6879 -- The only remaining step is to generate a range check if we still
6880 -- have a type conversion at this stage and Do_Range_Check is set.
6881 -- For now we do this only for conversions of discrete types.
6883 if Nkind
(N
) = N_Type_Conversion
6884 and then Is_Discrete_Type
(Etype
(N
))
6887 Expr
: constant Node_Id
:= Expression
(N
);
6892 if Do_Range_Check
(Expr
)
6893 and then Is_Discrete_Type
(Etype
(Expr
))
6895 Set_Do_Range_Check
(Expr
, False);
6897 -- Before we do a range check, we have to deal with treating
6898 -- a fixed-point operand as an integer. The way we do this
6899 -- is simply to do an unchecked conversion to an appropriate
6900 -- integer type large enough to hold the result.
6902 -- This code is not active yet, because we are only dealing
6903 -- with discrete types so far ???
6905 if Nkind
(Expr
) in N_Has_Treat_Fixed_As_Integer
6906 and then Treat_Fixed_As_Integer
(Expr
)
6908 Ftyp
:= Base_Type
(Etype
(Expr
));
6910 if Esize
(Ftyp
) >= Esize
(Standard_Integer
) then
6911 Ityp
:= Standard_Long_Long_Integer
;
6913 Ityp
:= Standard_Integer
;
6916 Rewrite
(Expr
, Unchecked_Convert_To
(Ityp
, Expr
));
6919 -- Reset overflow flag, since the range check will include
6920 -- dealing with possible overflow, and generate the check
6921 -- If Address is either source or target type, suppress
6922 -- range check to avoid typing anomalies when it is a visible
6925 Set_Do_Overflow_Check
(N
, False);
6926 if not Is_Descendent_Of_Address
(Etype
(Expr
))
6927 and then not Is_Descendent_Of_Address
(Target_Type
)
6929 Generate_Range_Check
6930 (Expr
, Target_Type
, CE_Range_Check_Failed
);
6935 end Expand_N_Type_Conversion
;
6937 -----------------------------------
6938 -- Expand_N_Unchecked_Expression --
6939 -----------------------------------
6941 -- Remove the unchecked expression node from the tree. It's job was simply
6942 -- to make sure that its constituent expression was handled with checks
6943 -- off, and now that that is done, we can remove it from the tree, and
6944 -- indeed must, since gigi does not expect to see these nodes.
6946 procedure Expand_N_Unchecked_Expression
(N
: Node_Id
) is
6947 Exp
: constant Node_Id
:= Expression
(N
);
6950 Set_Assignment_OK
(Exp
, Assignment_OK
(N
) or Assignment_OK
(Exp
));
6952 end Expand_N_Unchecked_Expression
;
6954 ----------------------------------------
6955 -- Expand_N_Unchecked_Type_Conversion --
6956 ----------------------------------------
6958 -- If this cannot be handled by Gigi and we haven't already made
6959 -- a temporary for it, do it now.
6961 procedure Expand_N_Unchecked_Type_Conversion
(N
: Node_Id
) is
6962 Target_Type
: constant Entity_Id
:= Etype
(N
);
6963 Operand
: constant Node_Id
:= Expression
(N
);
6964 Operand_Type
: constant Entity_Id
:= Etype
(Operand
);
6967 -- If we have a conversion of a compile time known value to a target
6968 -- type and the value is in range of the target type, then we can simply
6969 -- replace the construct by an integer literal of the correct type. We
6970 -- only apply this to integer types being converted. Possibly it may
6971 -- apply in other cases, but it is too much trouble to worry about.
6973 -- Note that we do not do this transformation if the Kill_Range_Check
6974 -- flag is set, since then the value may be outside the expected range.
6975 -- This happens in the Normalize_Scalars case.
6977 if Is_Integer_Type
(Target_Type
)
6978 and then Is_Integer_Type
(Operand_Type
)
6979 and then Compile_Time_Known_Value
(Operand
)
6980 and then not Kill_Range_Check
(N
)
6983 Val
: constant Uint
:= Expr_Value
(Operand
);
6986 if Compile_Time_Known_Value
(Type_Low_Bound
(Target_Type
))
6988 Compile_Time_Known_Value
(Type_High_Bound
(Target_Type
))
6990 Val
>= Expr_Value
(Type_Low_Bound
(Target_Type
))
6992 Val
<= Expr_Value
(Type_High_Bound
(Target_Type
))
6994 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Val
));
6996 -- If Address is the target type, just set the type
6997 -- to avoid a spurious type error on the literal when
6998 -- Address is a visible integer type.
7000 if Is_Descendent_Of_Address
(Target_Type
) then
7001 Set_Etype
(N
, Target_Type
);
7003 Analyze_And_Resolve
(N
, Target_Type
);
7011 -- Nothing to do if conversion is safe
7013 if Safe_Unchecked_Type_Conversion
(N
) then
7017 -- Otherwise force evaluation unless Assignment_OK flag is set (this
7018 -- flag indicates ??? -- more comments needed here)
7020 if Assignment_OK
(N
) then
7023 Force_Evaluation
(N
);
7025 end Expand_N_Unchecked_Type_Conversion
;
7027 ----------------------------
7028 -- Expand_Record_Equality --
7029 ----------------------------
7031 -- For non-variant records, Equality is expanded when needed into:
7033 -- and then Lhs.Discr1 = Rhs.Discr1
7035 -- and then Lhs.Discrn = Rhs.Discrn
7036 -- and then Lhs.Cmp1 = Rhs.Cmp1
7038 -- and then Lhs.Cmpn = Rhs.Cmpn
7040 -- The expression is folded by the back-end for adjacent fields. This
7041 -- function is called for tagged record in only one occasion: for imple-
7042 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
7043 -- otherwise the primitive "=" is used directly.
7045 function Expand_Record_Equality
7050 Bodies
: List_Id
) return Node_Id
7052 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
7057 First_Time
: Boolean := True;
7059 function Suitable_Element
(C
: Entity_Id
) return Entity_Id
;
7060 -- Return the first field to compare beginning with C, skipping the
7061 -- inherited components.
7063 ----------------------
7064 -- Suitable_Element --
7065 ----------------------
7067 function Suitable_Element
(C
: Entity_Id
) return Entity_Id
is
7072 elsif Ekind
(C
) /= E_Discriminant
7073 and then Ekind
(C
) /= E_Component
7075 return Suitable_Element
(Next_Entity
(C
));
7077 elsif Is_Tagged_Type
(Typ
)
7078 and then C
/= Original_Record_Component
(C
)
7080 return Suitable_Element
(Next_Entity
(C
));
7082 elsif Chars
(C
) = Name_uController
7083 or else Chars
(C
) = Name_uTag
7085 return Suitable_Element
(Next_Entity
(C
));
7090 end Suitable_Element
;
7092 -- Start of processing for Expand_Record_Equality
7095 -- Generates the following code: (assuming that Typ has one Discr and
7096 -- component C2 is also a record)
7099 -- and then Lhs.Discr1 = Rhs.Discr1
7100 -- and then Lhs.C1 = Rhs.C1
7101 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
7103 -- and then Lhs.Cmpn = Rhs.Cmpn
7105 Result
:= New_Reference_To
(Standard_True
, Loc
);
7106 C
:= Suitable_Element
(First_Entity
(Typ
));
7108 while Present
(C
) loop
7116 First_Time
:= False;
7120 New_Lhs
:= New_Copy_Tree
(Lhs
);
7121 New_Rhs
:= New_Copy_Tree
(Rhs
);
7125 Expand_Composite_Equality
(Nod
, Etype
(C
),
7127 Make_Selected_Component
(Loc
,
7129 Selector_Name
=> New_Reference_To
(C
, Loc
)),
7131 Make_Selected_Component
(Loc
,
7133 Selector_Name
=> New_Reference_To
(C
, Loc
)),
7136 -- If some (sub)component is an unchecked_union, the whole
7137 -- operation will raise program error.
7139 if Nkind
(Check
) = N_Raise_Program_Error
then
7141 Set_Etype
(Result
, Standard_Boolean
);
7146 Left_Opnd
=> Result
,
7147 Right_Opnd
=> Check
);
7151 C
:= Suitable_Element
(Next_Entity
(C
));
7155 end Expand_Record_Equality
;
7157 -------------------------------------
7158 -- Fixup_Universal_Fixed_Operation --
7159 -------------------------------------
7161 procedure Fixup_Universal_Fixed_Operation
(N
: Node_Id
) is
7162 Conv
: constant Node_Id
:= Parent
(N
);
7165 -- We must have a type conversion immediately above us
7167 pragma Assert
(Nkind
(Conv
) = N_Type_Conversion
);
7169 -- Normally the type conversion gives our target type. The exception
7170 -- occurs in the case of the Round attribute, where the conversion
7171 -- will be to universal real, and our real type comes from the Round
7172 -- attribute (as well as an indication that we must round the result)
7174 if Nkind
(Parent
(Conv
)) = N_Attribute_Reference
7175 and then Attribute_Name
(Parent
(Conv
)) = Name_Round
7177 Set_Etype
(N
, Etype
(Parent
(Conv
)));
7178 Set_Rounded_Result
(N
);
7180 -- Normal case where type comes from conversion above us
7183 Set_Etype
(N
, Etype
(Conv
));
7185 end Fixup_Universal_Fixed_Operation
;
7187 ------------------------------
7188 -- Get_Allocator_Final_List --
7189 ------------------------------
7191 function Get_Allocator_Final_List
7194 PtrT
: Entity_Id
) return Entity_Id
7196 Loc
: constant Source_Ptr
:= Sloc
(N
);
7198 Owner
: Entity_Id
:= PtrT
;
7199 -- The entity whose finalisation list must be used to attach the
7200 -- allocated object.
7203 if Ekind
(PtrT
) = E_Anonymous_Access_Type
then
7204 if Nkind
(Associated_Node_For_Itype
(PtrT
))
7205 in N_Subprogram_Specification
7207 -- If the context is an access parameter, we need to create
7208 -- a non-anonymous access type in order to have a usable
7209 -- final list, because there is otherwise no pool to which
7210 -- the allocated object can belong. We create both the type
7211 -- and the finalization chain here, because freezing an
7212 -- internal type does not create such a chain. The Final_Chain
7213 -- that is thus created is shared by the access parameter.
7215 Owner
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('J'));
7217 Make_Full_Type_Declaration
(Loc
,
7218 Defining_Identifier
=> Owner
,
7220 Make_Access_To_Object_Definition
(Loc
,
7221 Subtype_Indication
=>
7222 New_Occurrence_Of
(T
, Loc
))));
7224 Build_Final_List
(N
, Owner
);
7225 Set_Associated_Final_Chain
(PtrT
, Associated_Final_Chain
(Owner
));
7228 -- Case of an access discriminant, or (Ada 2005) of
7229 -- an anonymous access component: find the final list
7230 -- associated with the scope of the type.
7232 Owner
:= Scope
(PtrT
);
7236 return Find_Final_List
(Owner
);
7237 end Get_Allocator_Final_List
;
7239 ---------------------------------
7240 -- Has_Inferable_Discriminants --
7241 ---------------------------------
7243 function Has_Inferable_Discriminants
(N
: Node_Id
) return Boolean is
7245 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean;
7246 -- Determines whether the left-most prefix of a selected component is a
7247 -- formal parameter in a subprogram. Assumes N is a selected component.
7249 --------------------------------
7250 -- Prefix_Is_Formal_Parameter --
7251 --------------------------------
7253 function Prefix_Is_Formal_Parameter
(N
: Node_Id
) return Boolean is
7254 Sel_Comp
: Node_Id
:= N
;
7257 -- Move to the left-most prefix by climbing up the tree
7259 while Present
(Parent
(Sel_Comp
))
7260 and then Nkind
(Parent
(Sel_Comp
)) = N_Selected_Component
7262 Sel_Comp
:= Parent
(Sel_Comp
);
7265 return Ekind
(Entity
(Prefix
(Sel_Comp
))) in Formal_Kind
;
7266 end Prefix_Is_Formal_Parameter
;
7268 -- Start of processing for Has_Inferable_Discriminants
7271 -- For identifiers and indexed components, it is sufficent to have a
7272 -- constrained Unchecked_Union nominal subtype.
7274 if Nkind
(N
) = N_Identifier
7276 Nkind
(N
) = N_Indexed_Component
7278 return Is_Unchecked_Union
(Base_Type
(Etype
(N
)))
7280 Is_Constrained
(Etype
(N
));
7282 -- For selected components, the subtype of the selector must be a
7283 -- constrained Unchecked_Union. If the component is subject to a
7284 -- per-object constraint, then the enclosing object must have inferable
7287 elsif Nkind
(N
) = N_Selected_Component
then
7288 if Has_Per_Object_Constraint
(Entity
(Selector_Name
(N
))) then
7290 -- A small hack. If we have a per-object constrained selected
7291 -- component of a formal parameter, return True since we do not
7292 -- know the actual parameter association yet.
7294 if Prefix_Is_Formal_Parameter
(N
) then
7298 -- Otherwise, check the enclosing object and the selector
7300 return Has_Inferable_Discriminants
(Prefix
(N
))
7302 Has_Inferable_Discriminants
(Selector_Name
(N
));
7305 -- The call to Has_Inferable_Discriminants will determine whether
7306 -- the selector has a constrained Unchecked_Union nominal type.
7308 return Has_Inferable_Discriminants
(Selector_Name
(N
));
7310 -- A qualified expression has inferable discriminants if its subtype
7311 -- mark is a constrained Unchecked_Union subtype.
7313 elsif Nkind
(N
) = N_Qualified_Expression
then
7314 return Is_Unchecked_Union
(Subtype_Mark
(N
))
7316 Is_Constrained
(Subtype_Mark
(N
));
7321 end Has_Inferable_Discriminants
;
7323 -------------------------------
7324 -- Insert_Dereference_Action --
7325 -------------------------------
7327 procedure Insert_Dereference_Action
(N
: Node_Id
) is
7328 Loc
: constant Source_Ptr
:= Sloc
(N
);
7329 Typ
: constant Entity_Id
:= Etype
(N
);
7330 Pool
: constant Entity_Id
:= Associated_Storage_Pool
(Typ
);
7331 Pnod
: constant Node_Id
:= Parent
(N
);
7333 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean;
7334 -- Return true if type of P is derived from Checked_Pool;
7336 -----------------------------
7337 -- Is_Checked_Storage_Pool --
7338 -----------------------------
7340 function Is_Checked_Storage_Pool
(P
: Entity_Id
) return Boolean is
7349 while T
/= Etype
(T
) loop
7350 if Is_RTE
(T
, RE_Checked_Pool
) then
7358 end Is_Checked_Storage_Pool
;
7360 -- Start of processing for Insert_Dereference_Action
7363 pragma Assert
(Nkind
(Pnod
) = N_Explicit_Dereference
);
7365 if not (Is_Checked_Storage_Pool
(Pool
)
7366 and then Comes_From_Source
(Original_Node
(Pnod
)))
7372 Make_Procedure_Call_Statement
(Loc
,
7373 Name
=> New_Reference_To
(
7374 Find_Prim_Op
(Etype
(Pool
), Name_Dereference
), Loc
),
7376 Parameter_Associations
=> New_List
(
7380 New_Reference_To
(Pool
, Loc
),
7382 -- Storage_Address. We use the attribute Pool_Address,
7383 -- which uses the pointer itself to find the address of
7384 -- the object, and which handles unconstrained arrays
7385 -- properly by computing the address of the template.
7386 -- i.e. the correct address of the corresponding allocation.
7388 Make_Attribute_Reference
(Loc
,
7389 Prefix
=> Duplicate_Subexpr_Move_Checks
(N
),
7390 Attribute_Name
=> Name_Pool_Address
),
7392 -- Size_In_Storage_Elements
7394 Make_Op_Divide
(Loc
,
7396 Make_Attribute_Reference
(Loc
,
7398 Make_Explicit_Dereference
(Loc
,
7399 Duplicate_Subexpr_Move_Checks
(N
)),
7400 Attribute_Name
=> Name_Size
),
7402 Make_Integer_Literal
(Loc
, System_Storage_Unit
)),
7406 Make_Attribute_Reference
(Loc
,
7408 Make_Explicit_Dereference
(Loc
,
7409 Duplicate_Subexpr_Move_Checks
(N
)),
7410 Attribute_Name
=> Name_Alignment
))));
7413 when RE_Not_Available
=>
7415 end Insert_Dereference_Action
;
7417 ------------------------------
7418 -- Make_Array_Comparison_Op --
7419 ------------------------------
7421 -- This is a hand-coded expansion of the following generic function:
7424 -- type elem is (<>);
7425 -- type index is (<>);
7426 -- type a is array (index range <>) of elem;
7428 -- function Gnnn (X : a; Y: a) return boolean is
7429 -- J : index := Y'first;
7432 -- if X'length = 0 then
7435 -- elsif Y'length = 0 then
7439 -- for I in X'range loop
7440 -- if X (I) = Y (J) then
7441 -- if J = Y'last then
7444 -- J := index'succ (J);
7448 -- return X (I) > Y (J);
7452 -- return X'length > Y'length;
7456 -- Note that since we are essentially doing this expansion by hand, we
7457 -- do not need to generate an actual or formal generic part, just the
7458 -- instantiated function itself.
7460 function Make_Array_Comparison_Op
7462 Nod
: Node_Id
) return Node_Id
7464 Loc
: constant Source_Ptr
:= Sloc
(Nod
);
7466 X
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uX
);
7467 Y
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uY
);
7468 I
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uI
);
7469 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
7471 Index
: constant Entity_Id
:= Base_Type
(Etype
(First_Index
(Typ
)));
7473 Loop_Statement
: Node_Id
;
7474 Loop_Body
: Node_Id
;
7477 Final_Expr
: Node_Id
;
7478 Func_Body
: Node_Id
;
7479 Func_Name
: Entity_Id
;
7485 -- if J = Y'last then
7488 -- J := index'succ (J);
7492 Make_Implicit_If_Statement
(Nod
,
7495 Left_Opnd
=> New_Reference_To
(J
, Loc
),
7497 Make_Attribute_Reference
(Loc
,
7498 Prefix
=> New_Reference_To
(Y
, Loc
),
7499 Attribute_Name
=> Name_Last
)),
7501 Then_Statements
=> New_List
(
7502 Make_Exit_Statement
(Loc
)),
7506 Make_Assignment_Statement
(Loc
,
7507 Name
=> New_Reference_To
(J
, Loc
),
7509 Make_Attribute_Reference
(Loc
,
7510 Prefix
=> New_Reference_To
(Index
, Loc
),
7511 Attribute_Name
=> Name_Succ
,
7512 Expressions
=> New_List
(New_Reference_To
(J
, Loc
))))));
7514 -- if X (I) = Y (J) then
7517 -- return X (I) > Y (J);
7521 Make_Implicit_If_Statement
(Nod
,
7525 Make_Indexed_Component
(Loc
,
7526 Prefix
=> New_Reference_To
(X
, Loc
),
7527 Expressions
=> New_List
(New_Reference_To
(I
, Loc
))),
7530 Make_Indexed_Component
(Loc
,
7531 Prefix
=> New_Reference_To
(Y
, Loc
),
7532 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)))),
7534 Then_Statements
=> New_List
(Inner_If
),
7536 Else_Statements
=> New_List
(
7537 Make_Return_Statement
(Loc
,
7541 Make_Indexed_Component
(Loc
,
7542 Prefix
=> New_Reference_To
(X
, Loc
),
7543 Expressions
=> New_List
(New_Reference_To
(I
, Loc
))),
7546 Make_Indexed_Component
(Loc
,
7547 Prefix
=> New_Reference_To
(Y
, Loc
),
7548 Expressions
=> New_List
(
7549 New_Reference_To
(J
, Loc
)))))));
7551 -- for I in X'range loop
7556 Make_Implicit_Loop_Statement
(Nod
,
7557 Identifier
=> Empty
,
7560 Make_Iteration_Scheme
(Loc
,
7561 Loop_Parameter_Specification
=>
7562 Make_Loop_Parameter_Specification
(Loc
,
7563 Defining_Identifier
=> I
,
7564 Discrete_Subtype_Definition
=>
7565 Make_Attribute_Reference
(Loc
,
7566 Prefix
=> New_Reference_To
(X
, Loc
),
7567 Attribute_Name
=> Name_Range
))),
7569 Statements
=> New_List
(Loop_Body
));
7571 -- if X'length = 0 then
7573 -- elsif Y'length = 0 then
7576 -- for ... loop ... end loop;
7577 -- return X'length > Y'length;
7581 Make_Attribute_Reference
(Loc
,
7582 Prefix
=> New_Reference_To
(X
, Loc
),
7583 Attribute_Name
=> Name_Length
);
7586 Make_Attribute_Reference
(Loc
,
7587 Prefix
=> New_Reference_To
(Y
, Loc
),
7588 Attribute_Name
=> Name_Length
);
7592 Left_Opnd
=> Length1
,
7593 Right_Opnd
=> Length2
);
7596 Make_Implicit_If_Statement
(Nod
,
7600 Make_Attribute_Reference
(Loc
,
7601 Prefix
=> New_Reference_To
(X
, Loc
),
7602 Attribute_Name
=> Name_Length
),
7604 Make_Integer_Literal
(Loc
, 0)),
7608 Make_Return_Statement
(Loc
,
7609 Expression
=> New_Reference_To
(Standard_False
, Loc
))),
7611 Elsif_Parts
=> New_List
(
7612 Make_Elsif_Part
(Loc
,
7616 Make_Attribute_Reference
(Loc
,
7617 Prefix
=> New_Reference_To
(Y
, Loc
),
7618 Attribute_Name
=> Name_Length
),
7620 Make_Integer_Literal
(Loc
, 0)),
7624 Make_Return_Statement
(Loc
,
7625 Expression
=> New_Reference_To
(Standard_True
, Loc
))))),
7627 Else_Statements
=> New_List
(
7629 Make_Return_Statement
(Loc
,
7630 Expression
=> Final_Expr
)));
7634 Formals
:= New_List
(
7635 Make_Parameter_Specification
(Loc
,
7636 Defining_Identifier
=> X
,
7637 Parameter_Type
=> New_Reference_To
(Typ
, Loc
)),
7639 Make_Parameter_Specification
(Loc
,
7640 Defining_Identifier
=> Y
,
7641 Parameter_Type
=> New_Reference_To
(Typ
, Loc
)));
7643 -- function Gnnn (...) return boolean is
7644 -- J : index := Y'first;
7649 Func_Name
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('G'));
7652 Make_Subprogram_Body
(Loc
,
7654 Make_Function_Specification
(Loc
,
7655 Defining_Unit_Name
=> Func_Name
,
7656 Parameter_Specifications
=> Formals
,
7657 Subtype_Mark
=> New_Reference_To
(Standard_Boolean
, Loc
)),
7659 Declarations
=> New_List
(
7660 Make_Object_Declaration
(Loc
,
7661 Defining_Identifier
=> J
,
7662 Object_Definition
=> New_Reference_To
(Index
, Loc
),
7664 Make_Attribute_Reference
(Loc
,
7665 Prefix
=> New_Reference_To
(Y
, Loc
),
7666 Attribute_Name
=> Name_First
))),
7668 Handled_Statement_Sequence
=>
7669 Make_Handled_Sequence_Of_Statements
(Loc
,
7670 Statements
=> New_List
(If_Stat
)));
7674 end Make_Array_Comparison_Op
;
7676 ---------------------------
7677 -- Make_Boolean_Array_Op --
7678 ---------------------------
7680 -- For logical operations on boolean arrays, expand in line the
7681 -- following, replacing 'and' with 'or' or 'xor' where needed:
7683 -- function Annn (A : typ; B: typ) return typ is
7686 -- for J in A'range loop
7687 -- C (J) := A (J) op B (J);
7692 -- Here typ is the boolean array type
7694 function Make_Boolean_Array_Op
7696 N
: Node_Id
) return Node_Id
7698 Loc
: constant Source_Ptr
:= Sloc
(N
);
7700 A
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uA
);
7701 B
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uB
);
7702 C
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uC
);
7703 J
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uJ
);
7711 Func_Name
: Entity_Id
;
7712 Func_Body
: Node_Id
;
7713 Loop_Statement
: Node_Id
;
7717 Make_Indexed_Component
(Loc
,
7718 Prefix
=> New_Reference_To
(A
, Loc
),
7719 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
7722 Make_Indexed_Component
(Loc
,
7723 Prefix
=> New_Reference_To
(B
, Loc
),
7724 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
7727 Make_Indexed_Component
(Loc
,
7728 Prefix
=> New_Reference_To
(C
, Loc
),
7729 Expressions
=> New_List
(New_Reference_To
(J
, Loc
)));
7731 if Nkind
(N
) = N_Op_And
then
7737 elsif Nkind
(N
) = N_Op_Or
then
7751 Make_Implicit_Loop_Statement
(N
,
7752 Identifier
=> Empty
,
7755 Make_Iteration_Scheme
(Loc
,
7756 Loop_Parameter_Specification
=>
7757 Make_Loop_Parameter_Specification
(Loc
,
7758 Defining_Identifier
=> J
,
7759 Discrete_Subtype_Definition
=>
7760 Make_Attribute_Reference
(Loc
,
7761 Prefix
=> New_Reference_To
(A
, Loc
),
7762 Attribute_Name
=> Name_Range
))),
7764 Statements
=> New_List
(
7765 Make_Assignment_Statement
(Loc
,
7767 Expression
=> Op
)));
7769 Formals
:= New_List
(
7770 Make_Parameter_Specification
(Loc
,
7771 Defining_Identifier
=> A
,
7772 Parameter_Type
=> New_Reference_To
(Typ
, Loc
)),
7774 Make_Parameter_Specification
(Loc
,
7775 Defining_Identifier
=> B
,
7776 Parameter_Type
=> New_Reference_To
(Typ
, Loc
)));
7779 Make_Defining_Identifier
(Loc
, New_Internal_Name
('A'));
7780 Set_Is_Inlined
(Func_Name
);
7783 Make_Subprogram_Body
(Loc
,
7785 Make_Function_Specification
(Loc
,
7786 Defining_Unit_Name
=> Func_Name
,
7787 Parameter_Specifications
=> Formals
,
7788 Subtype_Mark
=> New_Reference_To
(Typ
, Loc
)),
7790 Declarations
=> New_List
(
7791 Make_Object_Declaration
(Loc
,
7792 Defining_Identifier
=> C
,
7793 Object_Definition
=> New_Reference_To
(Typ
, Loc
))),
7795 Handled_Statement_Sequence
=>
7796 Make_Handled_Sequence_Of_Statements
(Loc
,
7797 Statements
=> New_List
(
7799 Make_Return_Statement
(Loc
,
7800 Expression
=> New_Reference_To
(C
, Loc
)))));
7803 end Make_Boolean_Array_Op
;
7805 ------------------------
7806 -- Rewrite_Comparison --
7807 ------------------------
7809 procedure Rewrite_Comparison
(N
: Node_Id
) is
7810 Typ
: constant Entity_Id
:= Etype
(N
);
7811 Op1
: constant Node_Id
:= Left_Opnd
(N
);
7812 Op2
: constant Node_Id
:= Right_Opnd
(N
);
7814 Res
: constant Compare_Result
:= Compile_Time_Compare
(Op1
, Op2
);
7815 -- Res indicates if compare outcome can be determined at compile time
7817 True_Result
: Boolean;
7818 False_Result
: Boolean;
7821 case N_Op_Compare
(Nkind
(N
)) is
7823 True_Result
:= Res
= EQ
;
7824 False_Result
:= Res
= LT
or else Res
= GT
or else Res
= NE
;
7827 True_Result
:= Res
in Compare_GE
;
7828 False_Result
:= Res
= LT
;
7831 True_Result
:= Res
= GT
;
7832 False_Result
:= Res
in Compare_LE
;
7835 True_Result
:= Res
= LT
;
7836 False_Result
:= Res
in Compare_GE
;
7839 True_Result
:= Res
in Compare_LE
;
7840 False_Result
:= Res
= GT
;
7843 True_Result
:= Res
= NE
;
7844 False_Result
:= Res
= LT
or else Res
= GT
or else Res
= EQ
;
7849 Convert_To
(Typ
, New_Occurrence_Of
(Standard_True
, Sloc
(N
))));
7850 Analyze_And_Resolve
(N
, Typ
);
7851 Warn_On_Known_Condition
(N
);
7853 elsif False_Result
then
7855 Convert_To
(Typ
, New_Occurrence_Of
(Standard_False
, Sloc
(N
))));
7856 Analyze_And_Resolve
(N
, Typ
);
7857 Warn_On_Known_Condition
(N
);
7859 end Rewrite_Comparison
;
7861 ----------------------------
7862 -- Safe_In_Place_Array_Op --
7863 ----------------------------
7865 function Safe_In_Place_Array_Op
7868 Op2
: Node_Id
) return Boolean
7872 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean;
7873 -- Operand is safe if it cannot overlap part of the target of the
7874 -- operation. If the operand and the target are identical, the operand
7875 -- is safe. The operand can be empty in the case of negation.
7877 function Is_Unaliased
(N
: Node_Id
) return Boolean;
7878 -- Check that N is a stand-alone entity
7884 function Is_Unaliased
(N
: Node_Id
) return Boolean is
7888 and then No
(Address_Clause
(Entity
(N
)))
7889 and then No
(Renamed_Object
(Entity
(N
)));
7892 ---------------------
7893 -- Is_Safe_Operand --
7894 ---------------------
7896 function Is_Safe_Operand
(Op
: Node_Id
) return Boolean is
7901 elsif Is_Entity_Name
(Op
) then
7902 return Is_Unaliased
(Op
);
7904 elsif Nkind
(Op
) = N_Indexed_Component
7905 or else Nkind
(Op
) = N_Selected_Component
7907 return Is_Unaliased
(Prefix
(Op
));
7909 elsif Nkind
(Op
) = N_Slice
then
7911 Is_Unaliased
(Prefix
(Op
))
7912 and then Entity
(Prefix
(Op
)) /= Target
;
7914 elsif Nkind
(Op
) = N_Op_Not
then
7915 return Is_Safe_Operand
(Right_Opnd
(Op
));
7920 end Is_Safe_Operand
;
7922 -- Start of processing for Is_Safe_In_Place_Array_Op
7925 -- We skip this processing if the component size is not the
7926 -- same as a system storage unit (since at least for NOT
7927 -- this would cause problems).
7929 if Component_Size
(Etype
(Lhs
)) /= System_Storage_Unit
then
7932 -- Cannot do in place stuff on Java_VM since cannot pass addresses
7937 -- Cannot do in place stuff if non-standard Boolean representation
7939 elsif Has_Non_Standard_Rep
(Component_Type
(Etype
(Lhs
))) then
7942 elsif not Is_Unaliased
(Lhs
) then
7945 Target
:= Entity
(Lhs
);
7948 Is_Safe_Operand
(Op1
)
7949 and then Is_Safe_Operand
(Op2
);
7951 end Safe_In_Place_Array_Op
;
7953 -----------------------
7954 -- Tagged_Membership --
7955 -----------------------
7957 -- There are two different cases to consider depending on whether
7958 -- the right operand is a class-wide type or not. If not we just
7959 -- compare the actual tag of the left expr to the target type tag:
7961 -- Left_Expr.Tag = Right_Type'Tag;
7963 -- If it is a class-wide type we use the RT function CW_Membership which
7964 -- is usually implemented by looking in the ancestor tables contained in
7965 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
7967 function Tagged_Membership
(N
: Node_Id
) return Node_Id
is
7968 Left
: constant Node_Id
:= Left_Opnd
(N
);
7969 Right
: constant Node_Id
:= Right_Opnd
(N
);
7970 Loc
: constant Source_Ptr
:= Sloc
(N
);
7972 Left_Type
: Entity_Id
;
7973 Right_Type
: Entity_Id
;
7977 Left_Type
:= Etype
(Left
);
7978 Right_Type
:= Etype
(Right
);
7980 if Is_Class_Wide_Type
(Left_Type
) then
7981 Left_Type
:= Root_Type
(Left_Type
);
7985 Make_Selected_Component
(Loc
,
7986 Prefix
=> Relocate_Node
(Left
),
7987 Selector_Name
=> New_Reference_To
(Tag_Component
(Left_Type
), Loc
));
7989 if Is_Class_Wide_Type
(Right_Type
) then
7991 Make_DT_Access_Action
(Left_Type
,
7992 Action
=> CW_Membership
,
7996 Access_Disp_Table
(Root_Type
(Right_Type
)), Loc
)));
8000 Left_Opnd
=> Obj_Tag
,
8002 New_Reference_To
(Access_Disp_Table
(Right_Type
), Loc
));
8005 end Tagged_Membership
;
8007 ------------------------------
8008 -- Unary_Op_Validity_Checks --
8009 ------------------------------
8011 procedure Unary_Op_Validity_Checks
(N
: Node_Id
) is
8013 if Validity_Checks_On
and Validity_Check_Operands
then
8014 Ensure_Valid
(Right_Opnd
(N
));
8016 end Unary_Op_Validity_Checks
;