Mark ChangeLog
[official-gcc.git] / gcc / ada / exp_ch4.adb
blobfd03a08b41129434cc3cd8de0511d6460c02a1e4
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 4 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2005, Free Software Foundation, Inc. --
10 -- --
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. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
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;
46 with Opt; use Opt;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
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
78 (N : Node_Id;
79 Op1 : Node_Id;
80 Op2 : Node_Id);
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
93 -- run-time routine)
95 function Expand_Array_Equality
96 (Nod : Node_Id;
97 Lhs : Node_Id;
98 Rhs : Node_Id;
99 Bodies : List_Id;
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
118 (Nod : Node_Id;
119 Typ : Entity_Id;
120 Lhs : Node_Id;
121 Rhs : Node_Id;
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
151 (N : Node_Id;
152 T : Entity_Id;
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
163 -- discriminants.
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
175 (Typ : Entity_Id;
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
184 (Typ : Entity_Id;
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
205 (Lhs : Node_Id;
206 Op1 : Node_Id;
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
210 -- in place.
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
221 begin
222 if Validity_Checks_On and Validity_Check_Operands then
223 Ensure_Valid (Left_Opnd (N));
224 Ensure_Valid (Right_Opnd (N));
225 end if;
226 end Binary_Op_Validity_Checks;
228 ------------------------------------
229 -- Build_Boolean_Array_Proc_Call --
230 ------------------------------------
232 procedure Build_Boolean_Array_Proc_Call
233 (N : Node_Id;
234 Op1 : Node_Id;
235 Op2 : Node_Id)
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,
241 Prefix => Name (N),
242 Attribute_Name => Name_Address);
244 Arg1 : constant Node_Id := Op1;
245 Arg2 : Node_Id := Op2;
246 Call_Node : Node_Id;
247 Proc_Name : Entity_Id;
249 begin
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);
263 end if;
265 Call_Node :=
266 Make_Procedure_Call_Statement (Loc,
267 Name => New_Occurrence_Of (Proc_Name, Loc),
269 Parameter_Associations => New_List (
270 Target,
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)));
283 else
284 Proc_Name := RTE (RE_Vector_Not);
286 Call_Node :=
287 Make_Procedure_Call_Statement (Loc,
288 Name => New_Occurrence_Of (Proc_Name, Loc),
289 Parameter_Associations => New_List (
290 Target,
292 Make_Attribute_Reference (Loc,
293 Prefix => Op1,
294 Attribute_Name => Name_Address),
296 Make_Attribute_Reference (Loc,
297 Prefix => Op1,
298 Attribute_Name => Name_Length)));
299 end if;
301 else
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);
316 else
317 Proc_Name := RTE (RE_Vector_Xor);
318 end if;
320 else
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);
331 else
332 Proc_Name := RTE (RE_Vector_Xor);
333 end if;
334 end if;
336 Call_Node :=
337 Make_Procedure_Call_Statement (Loc,
338 Name => New_Occurrence_Of (Proc_Name, Loc),
339 Parameter_Associations => New_List (
340 Target,
341 Make_Attribute_Reference (Loc,
342 Prefix => Arg1,
343 Attribute_Name => Name_Address),
344 Make_Attribute_Reference (Loc,
345 Prefix => Arg2,
346 Attribute_Name => Name_Address),
347 Make_Attribute_Reference (Loc,
348 Prefix => Op1,
349 Attribute_Name => Name_Length)));
350 end if;
352 Rewrite (N, Call_Node);
353 Analyze (N);
355 exception
356 when RE_Not_Available =>
357 return;
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);
370 Flist : Node_Id;
371 Node : Node_Id;
372 Temp : Entity_Id;
374 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
376 Tag_Assign : Node_Id;
377 Tmp_Node : Node_Id;
379 begin
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);
393 end if;
395 Temp :=
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));
411 end if;
413 if Aggr_In_Place then
414 Tmp_Node :=
415 Make_Object_Declaration (Loc,
416 Defining_Identifier => Temp,
417 Object_Definition => New_Reference_To (PtrT, Loc),
418 Expression =>
419 Make_Allocator (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
430 then
431 -- Create local finalization list for access parameter
433 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
434 end if;
436 Convert_Aggr_In_Allocator (Tmp_Node, Exp);
437 else
438 Node := Relocate_Node (N);
439 Set_Analyzed (Node);
440 Insert_Action (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));
446 end if;
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)
453 and then not Java_VM
454 then
455 Tag_Assign :=
456 Make_Assignment_Statement (Loc,
457 Name =>
458 Make_Selected_Component (Loc,
459 Prefix => New_Reference_To (Temp, Loc),
460 Selector_Name =>
461 New_Reference_To (Tag_Component (T), Loc)),
463 Expression =>
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))
474 and then not Java_VM
475 then
476 declare
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)));
483 begin
484 Tag_Assign :=
485 Make_Assignment_Statement (Loc,
486 Name =>
487 Make_Selected_Component (Loc,
488 Prefix => Ref,
489 Selector_Name =>
490 New_Reference_To (Tag_Component (Utyp), Loc)),
492 Expression =>
493 Unchecked_Convert_To (RTE (RE_Tag),
494 New_Reference_To (
495 Access_Disp_Table (Utyp), Loc)));
497 Set_Assignment_OK (Name (Tag_Assign));
498 Insert_Action (N, Tag_Assign);
499 end;
500 end if;
502 if Controlled_Type (Designated_Type (PtrT))
503 and then Controlled_Type (T)
504 then
505 declare
506 Attach : Node_Id;
507 Apool : constant Entity_Id :=
508 Associated_Storage_Pool (PtrT);
510 begin
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
517 declare
518 F : constant Entity_Id :=
519 Make_Defining_Identifier (Loc,
520 New_Internal_Name ('F'));
521 begin
522 Insert_Action (N,
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);
530 end;
532 -- Normal case, not a secondary stack allocation
534 else
535 if Controlled_Type (T)
536 and then Ekind (PtrT) = E_Anonymous_Access_Type
537 then
538 -- Create local finalization list for access parameter
540 Flist :=
541 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
542 else
543 Flist := Find_Final_List (PtrT);
544 end if;
546 Attach := Make_Integer_Literal (Loc, 2);
547 end if;
549 if not Aggr_In_Place then
550 Insert_Actions (N,
551 Make_Adjust_Call (
552 Ref =>
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
557 -- the allocator.
559 Unchecked_Convert_To (T,
560 Make_Explicit_Dereference (Loc,
561 New_Reference_To (Temp, Loc))),
563 Typ => T,
564 Flist_Ref => Flist,
565 With_Attach => Attach));
566 end if;
567 end;
568 end if;
570 Rewrite (N, New_Reference_To (Temp, Loc));
571 Analyze_And_Resolve (N, PtrT);
573 elsif Aggr_In_Place then
574 Temp :=
575 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
576 Tmp_Node :=
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
595 then
596 -- Apply constraint to designated subtype indication
598 Apply_Constraint_Check (Expression (Exp),
599 Designated_Type (Designated_Type (PtrT)),
600 No_Sliding => True);
602 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
604 -- Propagate constraint_error to enclosing allocator
606 Rewrite (Exp, New_Copy (Expression (Exp)));
607 end if;
608 else
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.
616 -- ???
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))
629 then
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.
638 else
639 Apply_Constraint_Check
640 (Exp, Designated_Type (PtrT), No_Sliding => True);
641 end if;
642 end if;
644 exception
645 when RE_Not_Available =>
646 return;
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);
670 Expr : Node_Id;
671 Func_Body : Node_Id;
672 Func_Name : Entity_Id;
674 Comp : RE_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);
691 begin
692 if Ekind (Otyp) = E_String_Literal_Subtype then
693 return String_Literal_Length (Otyp) < 4;
695 else
696 declare
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);
700 Lov : Uint;
701 Hiv : Uint;
703 begin
704 if Compile_Time_Known_Value (Lo) then
705 Lov := Expr_Value (Lo);
706 else
707 return False;
708 end if;
710 if Compile_Time_Known_Value (Hi) then
711 Hiv := Expr_Value (Hi);
712 else
713 return False;
714 end if;
716 return Hiv < Lov + 3;
717 end;
718 end if;
719 end Length_Less_Than_4;
721 -- Start of processing for Expand_Array_Comparison
723 begin
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
731 and then not Java_VM
732 then
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)
745 or else
746 Length_Less_Than_4 (Op2)
747 then
748 if Is_Unsigned_Type (Ctyp) then
749 Comp := RE_Compare_Array_U8_Unaligned;
750 else
751 Comp := RE_Compare_Array_S8_Unaligned;
752 end if;
754 else
755 if Is_Unsigned_Type (Ctyp) then
756 Comp := RE_Compare_Array_U8;
757 else
758 Comp := RE_Compare_Array_S8;
759 end if;
760 end if;
762 elsif Component_Size (Typ1) = 16 then
763 if Is_Unsigned_Type (Ctyp) then
764 Comp := RE_Compare_Array_U16;
765 else
766 Comp := RE_Compare_Array_S16;
767 end if;
769 elsif Component_Size (Typ1) = 32 then
770 if Is_Unsigned_Type (Ctyp) then
771 Comp := RE_Compare_Array_U32;
772 else
773 Comp := RE_Compare_Array_S32;
774 end if;
776 else pragma Assert (Component_Size (Typ1) = 64);
777 if Is_Unsigned_Type (Ctyp) then
778 Comp := RE_Compare_Array_U64;
779 else
780 Comp := RE_Compare_Array_S64;
781 end if;
782 end if;
784 Remove_Side_Effects (Op1, Name_Req => True);
785 Remove_Side_Effects (Op2, Name_Req => True);
787 Rewrite (Op1,
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))));
808 Rewrite (Op2,
809 Make_Integer_Literal (Sloc (Op2),
810 Intval => Uint_0));
812 Analyze_And_Resolve (Op1, Standard_Integer);
813 Analyze_And_Resolve (Op2, Standard_Integer);
814 return;
815 end if;
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
822 Rewrite (N,
823 Make_Op_Not (Loc,
824 Right_Opnd =>
825 Make_Op_Gt (Loc,
826 Left_Opnd => Op1,
827 Right_Opnd => Op2)));
828 Analyze_And_Resolve (N, Standard_Boolean);
829 return;
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);
837 -- Switch operands
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
845 Rewrite (N,
846 Make_Op_Not (Loc,
847 Right_Opnd =>
848 Make_Op_Lt (Loc,
849 Left_Opnd => Op1,
850 Right_Opnd => Op2)));
851 Analyze_And_Resolve (N, Standard_Boolean);
852 return;
854 -- For > the Boolean expression is
855 -- greater__nn (op1, op2)
857 else
858 pragma Assert (Chars (N) = Name_Op_Gt);
859 Func_Body := Make_Array_Comparison_Op (Typ1, N);
860 end if;
862 Func_Name := Defining_Unit_Name (Specification (Func_Body));
863 Expr :=
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);
869 Rewrite (N, Expr);
870 Analyze_And_Resolve (N, Standard_Boolean);
872 exception
873 when RE_Not_Available =>
874 return;
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
885 -- begin
886 -- if (A'length (1) = 0 or else A'length (2) = 0)
887 -- and then
888 -- (B'length (1) = 0 or else B'length (2) = 0)
889 -- then
890 -- return True; -- RM 4.5.2(22)
891 -- end if;
893 -- if A'length (1) /= B'length (1)
894 -- or else
895 -- A'length (2) /= B'length (2)
896 -- then
897 -- return False; -- RM 4.5.2(23)
898 -- end if;
900 -- declare
901 -- A1 : Index_T1 := A'first (1);
902 -- B1 : Index_T1 := B'first (1);
903 -- begin
904 -- loop
905 -- declare
906 -- A2 : Index_T2 := A'first (2);
907 -- B2 : Index_T2 := B'first (2);
908 -- begin
909 -- loop
910 -- if A (A1, A2) /= B (B1, B2) then
911 -- return False;
912 -- end if;
914 -- exit when A2 = A'last (2);
915 -- A2 := Index_T2'succ (A2);
916 -- B2 := Index_T2'succ (B2);
917 -- end loop;
918 -- end;
920 -- exit when A1 = A'last (1);
921 -- A1 := Index_T1'succ (A1);
922 -- B1 := Index_T1'succ (B1);
923 -- end loop;
924 -- end;
926 -- return true;
927 -- end Enn;
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
940 (Nod : Node_Id;
941 Lhs : Node_Id;
942 Rhs : Node_Id;
943 Bodies : List_Id;
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;
951 Actuals : List_Id;
952 Formals : List_Id;
953 Func_Name : Entity_Id;
954 Func_Body : Node_Id;
956 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
957 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
959 Ltyp : Entity_Id;
960 Rtyp : Entity_Id;
961 -- The parameter types to be used for the formals
963 function Arr_Attr
964 (Arr : Entity_Id;
965 Nam : Name_Id;
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
978 (N : Int;
979 Index : Node_Id) return Node_Id;
980 -- This procedure returns the following code
982 -- declare
983 -- Bn : Index_T := B'First (N);
984 -- begin
985 -- loop
986 -- xxx
987 -- exit when An = A'Last (N);
988 -- An := Index_T'Succ (An)
989 -- Bn := Index_T'Succ (Bn)
990 -- end loop;
991 -- end;
993 -- If both indices are constrained and identical, the procedure
994 -- returns a simpler loop:
996 -- for An in A'Range (N) loop
997 -- xxx
998 -- end 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 ...)
1012 -- and then
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)
1020 -- or else
1021 -- A'length (2) /= B'length (2)
1022 -- or else
1023 -- ...
1025 --------------
1026 -- Arr_Attr --
1027 --------------
1029 function Arr_Attr
1030 (Arr : Entity_Id;
1031 Nam : Name_Id;
1032 Num : Int) return Node_Id
1034 begin
1035 return
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)));
1040 end Arr_Attr;
1042 ------------------------
1043 -- Component_Equality --
1044 ------------------------
1046 function Component_Equality (Typ : Entity_Id) return Node_Id is
1047 Test : Node_Id;
1048 L, R : Node_Id;
1050 begin
1051 -- if a(i1...) /= b(j1...) then return false; end if;
1053 L :=
1054 Make_Indexed_Component (Loc,
1055 Prefix => Make_Identifier (Loc, Chars (A)),
1056 Expressions => Index_List1);
1058 R :=
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
1070 return Test;
1072 else
1073 return
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))));
1079 end if;
1080 end Component_Equality;
1082 ------------------
1083 -- Get_Arg_Type --
1084 ------------------
1086 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1087 T : Entity_Id;
1088 X : Node_Id;
1090 begin
1091 T := Etype (N);
1093 if No (T) then
1094 return Typ;
1096 else
1097 T := Underlying_Type (T);
1099 X := First_Index (T);
1100 while Present (X) loop
1101 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1102 or else
1103 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1104 then
1105 T := Base_Type (T);
1106 exit;
1107 end if;
1109 Next_Index (X);
1110 end loop;
1112 return T;
1113 end if;
1114 end Get_Arg_Type;
1116 --------------------------
1117 -- Handle_One_Dimension --
1118 ---------------------------
1120 function Handle_One_Dimension
1121 (N : Int;
1122 Index : Node_Id) return Node_Id
1124 Need_Separate_Indexes : constant Boolean :=
1125 Ltyp /= Rtyp
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
1129 -- the arrays.
1131 An : constant Entity_Id := Make_Defining_Identifier (Loc,
1132 Chars => New_Internal_Name ('A'));
1134 Bn : Entity_Id;
1135 Index_T : Entity_Id;
1136 Stm_List : List_Id;
1137 Loop_Stm : Node_Id;
1139 begin
1140 if N > Number_Dimensions (Ltyp) then
1141 return Component_Equality (Ltyp);
1142 end if;
1144 -- Case where we generate a loop
1146 Index_T := Base_Type (Etype (Index));
1148 if Need_Separate_Indexes then
1149 Bn :=
1150 Make_Defining_Identifier (Loc,
1151 Chars => New_Internal_Name ('B'));
1152 else
1153 Bn := An;
1154 end if;
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,
1167 Condition =>
1168 Make_Op_Eq (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),
1175 Expression =>
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),
1184 Expression =>
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)))));
1189 end if;
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
1195 Loop_Stm :=
1196 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1198 return
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
1218 else
1219 Loop_Stm :=
1220 Make_Implicit_Loop_Statement (Nod,
1221 Statements => Stm_List,
1222 Iteration_Scheme =>
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))));
1229 return Loop_Stm;
1230 end if;
1231 end Handle_One_Dimension;
1233 -----------------------
1234 -- Test_Empty_Arrays --
1235 -----------------------
1237 function Test_Empty_Arrays return Node_Id is
1238 Alist : Node_Id;
1239 Blist : Node_Id;
1241 Atest : Node_Id;
1242 Btest : Node_Id;
1244 begin
1245 Alist := Empty;
1246 Blist := Empty;
1247 for J in 1 .. Number_Dimensions (Ltyp) loop
1248 Atest :=
1249 Make_Op_Eq (Loc,
1250 Left_Opnd => Arr_Attr (A, Name_Length, J),
1251 Right_Opnd => Make_Integer_Literal (Loc, 0));
1253 Btest :=
1254 Make_Op_Eq (Loc,
1255 Left_Opnd => Arr_Attr (B, Name_Length, J),
1256 Right_Opnd => Make_Integer_Literal (Loc, 0));
1258 if No (Alist) then
1259 Alist := Atest;
1260 Blist := Btest;
1262 else
1263 Alist :=
1264 Make_Or_Else (Loc,
1265 Left_Opnd => Relocate_Node (Alist),
1266 Right_Opnd => Atest);
1268 Blist :=
1269 Make_Or_Else (Loc,
1270 Left_Opnd => Relocate_Node (Blist),
1271 Right_Opnd => Btest);
1272 end if;
1273 end loop;
1275 return
1276 Make_And_Then (Loc,
1277 Left_Opnd => Alist,
1278 Right_Opnd => Blist);
1279 end Test_Empty_Arrays;
1281 -----------------------------
1282 -- Test_Lengths_Correspond --
1283 -----------------------------
1285 function Test_Lengths_Correspond return Node_Id is
1286 Result : Node_Id;
1287 Rtest : Node_Id;
1289 begin
1290 Result := Empty;
1291 for J in 1 .. Number_Dimensions (Ltyp) loop
1292 Rtest :=
1293 Make_Op_Ne (Loc,
1294 Left_Opnd => Arr_Attr (A, Name_Length, J),
1295 Right_Opnd => Arr_Attr (B, Name_Length, J));
1297 if No (Result) then
1298 Result := Rtest;
1299 else
1300 Result :=
1301 Make_Or_Else (Loc,
1302 Left_Opnd => Relocate_Node (Result),
1303 Right_Opnd => Rtest);
1304 end if;
1305 end loop;
1307 return Result;
1308 end Test_Lengths_Correspond;
1310 -- Start of processing for Expand_Array_Equality
1312 begin
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);
1324 end if;
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
1341 Func_Body :=
1342 Make_Subprogram_Body (Loc,
1343 Specification =>
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,
1359 Expression =>
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,
1366 Expression =>
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.
1381 declare
1382 L, R : Node_Id;
1384 begin
1385 L := Lhs;
1386 R := Rhs;
1388 if No (Etype (Lhs))
1389 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1390 then
1391 L := OK_Convert_To (Ltyp, Lhs);
1392 end if;
1394 if No (Etype (Rhs))
1395 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1396 then
1397 R := OK_Convert_To (Rtyp, Rhs);
1398 end if;
1400 Actuals := New_List (L, R);
1401 end;
1403 Append_To (Bodies, Func_Body);
1405 return
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);
1421 begin
1422 if Is_Bit_Packed_Array (Typ) then
1423 Expand_Packed_Boolean_Operator (N);
1425 else
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
1430 -- this function.
1432 declare
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;
1439 begin
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)
1448 then
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
1453 and then
1454 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
1455 then
1456 return;
1457 else
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
1465 Rewrite (N,
1466 Make_Function_Call (Loc,
1467 Name => New_Reference_To (Func_Name, Loc),
1468 Parameter_Associations =>
1469 New_List
1470 (L, Make_Type_Conversion
1471 (Loc, New_Reference_To (Etype (L), Loc), R))));
1473 Analyze_And_Resolve (N, Typ);
1474 end if;
1475 end;
1476 end if;
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
1488 (Nod : Node_Id;
1489 Typ : Entity_Id;
1490 Lhs : Node_Id;
1491 Rhs : Node_Id;
1492 Bodies : List_Id) return Node_Id
1494 Loc : constant Source_Ptr := Sloc (Nod);
1495 Full_Type : Entity_Id;
1496 Prim : Elmt_Id;
1497 Eq_Op : Entity_Id;
1499 begin
1500 if Is_Private_Type (Typ) then
1501 Full_Type := Underlying_Type (Typ);
1502 else
1503 Full_Type := Typ;
1504 end if;
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);
1511 end if;
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))
1524 then
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.
1533 else
1534 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
1535 end if;
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);
1543 end if;
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))
1556 then
1557 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
1558 else
1559 Prim := First_Elmt (Primitive_Operations (Full_Type));
1560 end if;
1562 loop
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;
1568 Next_Elmt (Prim);
1569 pragma Assert (Present (Prim));
1570 end loop;
1572 Eq_Op := Node (Prim);
1574 return
1575 Make_Function_Call (Loc,
1576 Name => New_Reference_To (Eq_Op, Loc),
1577 Parameter_Associations =>
1578 New_List
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.
1591 declare
1592 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
1594 begin
1595 return
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)));
1601 end;
1603 else
1604 -- Comparison between Unchecked_Union components
1606 if Is_Unchecked_Union (Full_Type) then
1607 declare
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;
1613 begin
1614 -- Lhs subtype
1616 if Nkind (Lhs) = N_Selected_Component then
1617 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
1618 end if;
1620 -- Rhs subtype
1622 if Nkind (Rhs) = N_Selected_Component then
1623 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
1624 end if;
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
1633 -- discriminant(s).
1635 if Nkind (Lhs) = N_Selected_Component
1636 and then Has_Per_Object_Constraint (
1637 Entity (Selector_Name (Lhs)))
1638 then
1639 Lhs_Discr_Val :=
1640 Make_Selected_Component (Loc,
1641 Prefix => Prefix (Lhs),
1642 Selector_Name =>
1643 New_Copy (
1644 Get_Discriminant_Value (
1645 First_Discriminant (Lhs_Type),
1646 Lhs_Type,
1647 Stored_Constraint (Lhs_Type))));
1649 else
1650 Lhs_Discr_Val := New_Copy (
1651 Get_Discriminant_Value (
1652 First_Discriminant (Lhs_Type),
1653 Lhs_Type,
1654 Stored_Constraint (Lhs_Type)));
1656 end if;
1657 else
1658 -- It is not possible to infer the discriminant since
1659 -- the subtype is not constrained.
1661 return
1662 Make_Raise_Program_Error (Loc,
1663 Reason => PE_Unchecked_Union_Restriction);
1664 end if;
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)))
1672 then
1673 Rhs_Discr_Val :=
1674 Make_Selected_Component (Loc,
1675 Prefix => Prefix (Rhs),
1676 Selector_Name =>
1677 New_Copy (
1678 Get_Discriminant_Value (
1679 First_Discriminant (Rhs_Type),
1680 Rhs_Type,
1681 Stored_Constraint (Rhs_Type))));
1683 else
1684 Rhs_Discr_Val := New_Copy (
1685 Get_Discriminant_Value (
1686 First_Discriminant (Rhs_Type),
1687 Rhs_Type,
1688 Stored_Constraint (Rhs_Type)));
1690 end if;
1691 else
1692 return
1693 Make_Raise_Program_Error (Loc,
1694 Reason => PE_Unchecked_Union_Restriction);
1695 end if;
1697 -- Call the TSS equality function with the inferred
1698 -- discriminant values.
1700 return
1701 Make_Function_Call (Loc,
1702 Name => New_Reference_To (Eq_Op, Loc),
1703 Parameter_Associations => New_List (
1704 Lhs,
1705 Rhs,
1706 Lhs_Discr_Val,
1707 Rhs_Discr_Val));
1708 end;
1709 end if;
1711 -- Shouldn't this be an else, we can't fall through
1712 -- the above IF, right???
1714 return
1715 Make_Function_Call (Loc,
1716 Name => New_Reference_To (Eq_Op, Loc),
1717 Parameter_Associations => New_List (Lhs, Rhs));
1718 end if;
1720 else
1721 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
1722 end if;
1724 else
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);
1728 end if;
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
1741 -- L : Ind_Typ;
1742 -- begin
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
1749 -- ...
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
1753 -- else
1754 -- return Sn;
1755 -- end if;
1757 -- declare
1758 -- P : Ind_Typ;
1759 -- H : Ind_Typ :=
1760 -- Ind_Typ'Val ((((S1'Length - 1) + S2'Length) + ... + Sn'Length)
1761 -- + Ind_Typ'Pos (L));
1762 -- R : Base_Typ (L .. H);
1763 -- begin
1764 -- if S1'Length /= 0 then
1765 -- P := S1'First;
1766 -- loop
1767 -- R (L) := S1 (P);
1768 -- L := Ind_Typ'Succ (L);
1769 -- exit when P = S1'Last;
1770 -- P := Ind_Typ'Succ (P);
1771 -- end loop;
1772 -- end if;
1774 -- if S2'Length /= 0 then
1775 -- L := Ind_Typ'Succ (L);
1776 -- loop
1777 -- R (L) := S2 (P);
1778 -- L := Ind_Typ'Succ (L);
1779 -- exit when P = S2'Last;
1780 -- P := Ind_Typ'Succ (P);
1781 -- end loop;
1782 -- end if;
1784 -- . . .
1786 -- if Sn'Length /= 0 then
1787 -- P := Sn'First;
1788 -- loop
1789 -- R (L) := Sn (P);
1790 -- L := Ind_Typ'Succ (L);
1791 -- exit when P = Sn'Last;
1792 -- P := Ind_Typ'Succ (P);
1793 -- end loop;
1794 -- end if;
1796 -- return R;
1797 -- end;
1798 -- end Cnn;]
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));
1808 Func_Id : Node_Id;
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;
1816 L_Decl : Node_Id;
1818 If_Stmt : Node_Id;
1819 Elsif_List : List_Id;
1821 Declare_Block : Node_Id;
1822 Declare_Decls : List_Id;
1823 Declare_Stmts : List_Id;
1825 H_Decl : Node_Id;
1826 H_Init : Node_Id;
1827 P_Decl : Node_Id;
1828 R_Decl : Node_Id;
1829 R_Constr : Node_Id;
1830 R_Range : Node_Id;
1832 Params : List_Id;
1833 Operand : Node_Id;
1835 function Copy_Into_R_S (I : Nat; Last : Boolean) return List_Id;
1836 -- Builds the sequence of statement:
1837 -- P := Si'First;
1838 -- loop
1839 -- R (L) := Si (P);
1840 -- L := Ind_Typ'Succ (L);
1841 -- exit when P = Si'Last;
1842 -- P := Ind_Typ'Succ (P);
1843 -- end loop;
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
1848 -- bound values.
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
1899 -------------------
1900 -- Copy_Into_R_S --
1901 -------------------
1903 function Copy_Into_R_S (I : Nat; Last : Boolean) return List_Id is
1904 Stmts : constant List_Id := New_List;
1905 P_Start : Node_Id;
1906 Loop_Stmt : Node_Id;
1907 R_Copy : Node_Id;
1908 Exit_Stmt : Node_Id;
1909 L_Inc : Node_Id;
1910 P_Inc : Node_Id;
1912 begin
1913 -- First construct the initializations
1915 P_Start := Make_Assignment_Statement (Loc,
1916 Name => P,
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,
1924 Prefix => R,
1925 Expressions => New_List (L)),
1926 Expression => Make_Indexed_Component (Loc,
1927 Prefix => S (I),
1928 Expressions => New_List (P)));
1930 L_Inc := Make_Assignment_Statement (Loc,
1931 Name => L,
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,
1938 Name => P,
1939 Expression => P_Succ);
1941 if Last then
1942 Loop_Stmt :=
1943 Make_Implicit_Loop_Statement (Cnode,
1944 Statements => New_List (R_Copy, Exit_Stmt, L_Inc, P_Inc));
1945 else
1946 Loop_Stmt :=
1947 Make_Implicit_Loop_Statement (Cnode,
1948 Statements => New_List (R_Copy, L_Inc, Exit_Stmt, P_Inc));
1949 end if;
1951 Append_To (Stmts, Loop_Stmt);
1953 return Stmts;
1954 end Copy_Into_R_S;
1956 -------
1957 -- H --
1958 -------
1960 function H return Node_Id is
1961 begin
1962 return Make_Identifier (Loc, Name_uH);
1963 end H;
1965 -------------
1966 -- Ind_Val --
1967 -------------
1969 function Ind_Val (E : Node_Id) return Node_Id is
1970 begin
1971 return
1972 Make_Attribute_Reference (Loc,
1973 Prefix => New_Reference_To (Ind_Typ, Loc),
1974 Attribute_Name => Name_Val,
1975 Expressions => New_List (E));
1976 end Ind_Val;
1978 ------------
1979 -- Init_L --
1980 ------------
1982 function Init_L (I : Nat) return Node_Id is
1983 E : Node_Id;
1985 begin
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);
1991 else
1992 E := S_First (I);
1993 end if;
1995 return Make_Assignment_Statement (Loc, Name => L, Expression => E);
1996 end Init_L;
1998 -------
1999 -- L --
2000 -------
2002 function L return Node_Id is
2003 begin
2004 return Make_Identifier (Loc, Name_uL);
2005 end L;
2007 -----------
2008 -- L_Pos --
2009 -----------
2011 function L_Pos return Node_Id is
2012 Target_Type : Entity_Id;
2014 begin
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
2023 then
2024 Target_Type := Standard_Integer;
2025 else
2026 Target_Type := Root_Type (Ind_Typ);
2027 end if;
2029 return
2030 Make_Qualified_Expression (Loc,
2031 Subtype_Mark => New_Reference_To (Target_Type, Loc),
2032 Expression =>
2033 Make_Attribute_Reference (Loc,
2034 Prefix => New_Reference_To (Ind_Typ, Loc),
2035 Attribute_Name => Name_Pos,
2036 Expressions => New_List (L)));
2037 end L_Pos;
2039 ------------
2040 -- L_Succ --
2041 ------------
2043 function L_Succ return Node_Id is
2044 begin
2045 return
2046 Make_Attribute_Reference (Loc,
2047 Prefix => New_Reference_To (Ind_Typ, Loc),
2048 Attribute_Name => Name_Succ,
2049 Expressions => New_List (L));
2050 end L_Succ;
2052 ---------
2053 -- One --
2054 ---------
2056 function One return Node_Id is
2057 begin
2058 return Make_Integer_Literal (Loc, 1);
2059 end One;
2061 -------
2062 -- P --
2063 -------
2065 function P return Node_Id is
2066 begin
2067 return Make_Identifier (Loc, Name_uP);
2068 end P;
2070 ------------
2071 -- P_Succ --
2072 ------------
2074 function P_Succ return Node_Id is
2075 begin
2076 return
2077 Make_Attribute_Reference (Loc,
2078 Prefix => New_Reference_To (Ind_Typ, Loc),
2079 Attribute_Name => Name_Succ,
2080 Expressions => New_List (P));
2081 end P_Succ;
2083 -------
2084 -- R --
2085 -------
2087 function R return Node_Id is
2088 begin
2089 return Make_Identifier (Loc, Name_uR);
2090 end R;
2092 -------
2093 -- S --
2094 -------
2096 function S (I : Nat) return Node_Id is
2097 begin
2098 return Make_Identifier (Loc, New_External_Name ('S', I));
2099 end S;
2101 -------------
2102 -- S_First --
2103 -------------
2105 function S_First (I : Nat) return Node_Id is
2106 begin
2107 return Make_Attribute_Reference (Loc,
2108 Prefix => S (I),
2109 Attribute_Name => Name_First);
2110 end S_First;
2112 ------------
2113 -- S_Last --
2114 ------------
2116 function S_Last (I : Nat) return Node_Id is
2117 begin
2118 return Make_Attribute_Reference (Loc,
2119 Prefix => S (I),
2120 Attribute_Name => Name_Last);
2121 end S_Last;
2123 --------------
2124 -- S_Length --
2125 --------------
2127 function S_Length (I : Nat) return Node_Id is
2128 begin
2129 return Make_Attribute_Reference (Loc,
2130 Prefix => S (I),
2131 Attribute_Name => Name_Length);
2132 end S_Length;
2134 -------------------
2135 -- S_Length_Test --
2136 -------------------
2138 function S_Length_Test (I : Nat) return Node_Id is
2139 begin
2140 return
2141 Make_Op_Ne (Loc,
2142 Left_Opnd => S_Length (I),
2143 Right_Opnd => Make_Integer_Literal (Loc, 0));
2144 end S_Length_Test;
2146 -- Start of processing for Expand_Concatenate_Other
2148 begin
2149 -- Construct the parameter specs and the overall function spec
2151 Param_Specs := New_List;
2152 for I in 1 .. Nb_Opnds loop
2153 Append_To
2154 (Param_Specs,
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)));
2159 end loop;
2161 Func_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
2162 Func_Spec :=
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
2170 L_Decl :=
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))));
2184 end loop;
2186 If_Stmt :=
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
2196 P_Decl :=
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));
2204 end loop;
2205 H_Init := Ind_Val (Make_Op_Add (Loc, H_Init, L_Pos));
2207 H_Decl :=
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);
2216 R_Constr :=
2217 Make_Index_Or_Discriminant_Constraint (Loc,
2218 Constraints => New_List (R_Range));
2220 R_Decl :=
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)));
2240 end loop;
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
2257 Func_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
2276 Params := New_List;
2277 Operand := First (Opnds);
2278 for I in 1 .. Nb_Opnds loop
2279 Append_To (Params, Relocate_Node (Operand));
2280 Next (Operand);
2281 end loop;
2283 -- Insert the function call
2285 Rewrite
2286 (Cnode,
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));
2304 R : RE_Id;
2305 -- RE_Id value for function to be called
2307 begin
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
2312 when 2 =>
2313 if Typ1 = Standard_Character then
2314 if Typ2 = Standard_Character then
2315 R := RE_Str_Concat_CC;
2317 else
2318 pragma Assert (Typ2 = Standard_String);
2319 R := RE_Str_Concat_CS;
2320 end if;
2322 elsif Typ1 = Standard_String then
2323 if Typ2 = Standard_Character then
2324 R := RE_Str_Concat_SC;
2326 else
2327 pragma Assert (Typ2 = Standard_String);
2328 R := RE_Str_Concat;
2329 end if;
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.
2335 else
2336 pragma Assert (Serious_Errors_Detected > 0);
2337 return;
2338 end if;
2340 when 3 =>
2341 R := RE_Str_Concat_3;
2343 when 4 =>
2344 R := RE_Str_Concat_4;
2346 when 5 =>
2347 R := RE_Str_Concat_5;
2349 when others =>
2350 R := RE_Null;
2351 raise Program_Error;
2352 end case;
2354 -- Now generate the appropriate call
2356 Rewrite (Cnode,
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);
2363 exception
2364 when RE_Not_Available =>
2365 return;
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);
2375 Desig : Entity_Id;
2376 Loc : constant Source_Ptr := Sloc (N);
2377 Temp : Entity_Id;
2378 Node : Node_Id;
2380 begin
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
2394 if not Java_VM then
2395 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
2396 end if;
2398 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
2399 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
2401 else
2402 Set_Procedure_To_Call (N,
2403 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
2404 end if;
2405 end if;
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
2423 (Expression (N))))
2424 and then not Is_Record_Type (Current_Scope)
2425 then
2426 -- Here we can do the optimization. For the allocator
2428 -- new x'(y)
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.
2437 Temp :=
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);
2448 end if;
2450 Insert_Action (N,
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))));
2458 Rewrite (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);
2469 return;
2470 end if;
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
2481 -- actions node:
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.
2493 else
2494 declare
2495 T : constant Entity_Id := Entity (Expression (N));
2496 Init : Entity_Id;
2497 Arg1 : Node_Id;
2498 Args : List_Id;
2499 Decls : List_Id;
2500 Decl : Node_Id;
2501 Discr : Elmt_Id;
2502 Flist : Node_Id;
2503 Temp_Decl : Node_Id;
2504 Temp_Type : Entity_Id;
2505 Attach_Level : Uint;
2507 begin
2508 if No_Initialization (N) then
2509 null;
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
2530 else
2531 null;
2532 end if;
2534 -- Case of initialization procedure present, must be called
2536 else
2537 Init := Base_Init_Proc (T);
2538 Node := N;
2539 Temp :=
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);
2547 Temp_Type := T;
2549 else
2550 Arg1 :=
2551 Make_Explicit_Dereference (Loc,
2552 Prefix => New_Reference_To (Temp, Loc));
2553 Set_Assignment_OK (Arg1);
2554 Temp_Type := PtrT;
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);
2562 end if;
2563 end if;
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
2573 Arg1 :=
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))
2579 then
2580 Arg1 :=
2581 Unchecked_Convert_To
2582 (Corresponding_Record_Type (Full_View (T)), Arg1);
2584 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
2586 declare
2587 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
2589 begin
2590 Arg1 := OK_Convert_To (Etype (Ftyp), Arg1);
2591 Set_Etype (Arg1, Ftyp);
2592 end;
2593 end if;
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
2607 -- it now.
2609 Expand_N_Full_Type_Declaration
2610 (Parent (Base_Type (PtrT)));
2611 end if;
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
2619 -- component.
2621 if Nkind (Parent (N)) = N_Assignment_Statement then
2622 declare
2623 Nam : constant Node_Id := Name (Parent (N));
2625 begin
2626 if Is_Entity_Name (Nam) then
2627 Decls :=
2628 Build_Task_Image_Decls (
2629 Loc,
2630 New_Occurrence_Of
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))
2636 then
2637 Decls :=
2638 Build_Task_Image_Decls
2639 (Loc, Nam, Etype (Prefix (Nam)));
2640 else
2641 Decls := Build_Task_Image_Decls (Loc, T, T);
2642 end if;
2643 end;
2645 elsif Nkind (Parent (N)) = N_Object_Declaration then
2646 Decls :=
2647 Build_Task_Image_Decls (
2648 Loc, Defining_Identifier (Parent (N)), T);
2650 else
2651 Decls := Build_Task_Image_Decls (Loc, T, T);
2652 end if;
2654 Append_To (Args,
2655 New_Reference_To
2656 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
2657 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2659 Decl := Last (Decls);
2660 Append_To (Args,
2661 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
2663 -- Has_Task is false, Decls not used
2665 else
2666 Decls := No_List;
2667 end if;
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);
2676 Next_Elmt (Discr);
2677 end loop;
2679 elsif Is_Private_Type (T)
2680 and then Present (Full_View (T))
2681 and then Has_Discriminants (Full_View (T))
2682 then
2683 Discr :=
2684 First_Elmt (Discriminant_Constraint (Full_View (T)));
2686 while Present (Discr) loop
2687 Append (New_Copy_Tree (Elists.Node (Discr)), Args);
2688 Next_Elmt (Discr);
2689 end loop;
2690 end if;
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:
2700 -- input: new T
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.
2709 Temp_Decl :=
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);
2720 end if;
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
2729 declare
2730 L : constant List_Id := New_List;
2731 Blk : Node_Id;
2733 begin
2734 Build_Task_Allocate_Block (L, Node, Args);
2735 Blk := Last (L);
2737 Insert_List_Before (First (Declarations (Blk)), Decls);
2738 Insert_Actions (N, L);
2739 end;
2741 else
2742 Insert_Action (N,
2743 Make_Procedure_Call_Statement (Loc,
2744 Name => New_Reference_To (Init, Loc),
2745 Parameter_Associations => Args));
2746 end if;
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;
2752 else
2753 Attach_Level := Uint_2;
2754 end if;
2755 Insert_Actions (N,
2756 Make_Init_Call (
2757 Ref => New_Copy_Tree (Arg1),
2758 Typ => T,
2759 Flist_Ref => Flist,
2760 With_Attach => Make_Integer_Literal (Loc,
2761 Attach_Level)));
2762 end if;
2764 if Is_CPP_Class (T) then
2765 Rewrite (N,
2766 Make_Attribute_Reference (Loc,
2767 Prefix => New_Reference_To (Temp, Loc),
2768 Attribute_Name => Name_Unchecked_Access));
2769 else
2770 Rewrite (N, New_Reference_To (Temp, Loc));
2771 end if;
2773 Analyze_And_Resolve (N, PtrT);
2774 end if;
2775 end;
2776 end if;
2778 exception
2779 when RE_Not_Available =>
2780 return;
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);
2795 Actlist : List_Id;
2797 begin
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);
2804 end if;
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));
2817 end if;
2819 Rewrite (N, Right);
2820 Adjust_Result_Type (N, Typ);
2821 return;
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);
2832 return;
2833 end if;
2834 end if;
2836 -- If Actions are present, we expand
2838 -- left and then right
2840 -- into
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);
2850 Rewrite (N,
2851 Make_Conditional_Expression (Loc,
2852 Expressions => New_List (
2853 Left,
2854 Right,
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);
2860 return;
2861 end if;
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
2872 Rewrite (N, Left);
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);
2879 Rewrite
2880 (N, New_Occurrence_Of (Standard_False, Loc));
2881 end if;
2882 end if;
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);
2899 Cnn : Entity_Id;
2900 New_If : Node_Id;
2902 begin
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):
2909 -- Cnn : typ;
2910 -- if cond then
2911 -- <<then actions>>
2912 -- Cnn := then-expr;
2913 -- else
2914 -- <<else actions>>
2915 -- Cnn := else-expr
2916 -- end if;
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'));
2923 New_If :=
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
2941 Insert_List_Before
2942 (First (Then_Statements (New_If)), Then_Actions (N));
2943 end if;
2945 if Present (Else_Actions (N)) then
2946 Insert_List_Before
2947 (First (Else_Statements (New_If)), Else_Actions (N));
2948 end if;
2950 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
2952 Insert_Action (N,
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);
2959 end if;
2960 end Expand_N_Conditional_Expression;
2962 -----------------------------------
2963 -- Expand_N_Explicit_Dereference --
2964 -----------------------------------
2966 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
2967 begin
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;
2974 -----------------
2975 -- Expand_N_In --
2976 -----------------
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);
2985 begin
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
2990 declare
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));
2996 begin
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
3003 Rewrite (N,
3004 New_Reference_To (Standard_False, Loc));
3005 Analyze_And_Resolve (N, Rtyp);
3006 Set_Is_Static_Expression (N, Static);
3007 return;
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
3013 Rewrite (N,
3014 New_Reference_To (Standard_True, Loc));
3015 Analyze_And_Resolve (N, Rtyp);
3016 Set_Is_Static_Expression (N, Static);
3017 return;
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
3024 Rewrite (N,
3025 Make_Op_Le (Loc,
3026 Left_Opnd => Lop,
3027 Right_Opnd => High_Bound (Rop)));
3028 Analyze_And_Resolve (N, Rtyp);
3029 return;
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
3036 Rewrite (N,
3037 Make_Op_Ge (Loc,
3038 Left_Opnd => Lop,
3039 Right_Opnd => Low_Bound (Rop)));
3040 Analyze_And_Resolve (N, Rtyp);
3041 return;
3042 end if;
3043 end;
3045 -- For all other cases of an explicit range, nothing to be done
3047 return;
3049 -- Here right operand is a subtype mark
3051 else
3052 declare
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;
3058 begin
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
3069 -- we want).
3071 if not Java_VM then
3072 Rewrite (N, Tagged_Membership (N));
3073 Analyze_And_Resolve (N, Rtyp);
3074 end if;
3076 return;
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
3083 Rewrite (Rop,
3084 Make_Range (Loc,
3085 Low_Bound =>
3086 Make_Attribute_Reference (Loc,
3087 Attribute_Name => Name_First,
3088 Prefix => New_Reference_To (Typ, Loc)),
3090 High_Bound =>
3091 Make_Attribute_Reference (Loc,
3092 Attribute_Name => Name_Last,
3093 Prefix => New_Reference_To (Typ, Loc))));
3094 Analyze_And_Resolve (N, Rtyp);
3095 return;
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
3100 -- discriminants.
3102 elsif Is_Unchecked_Union (Base_Type (Typ))
3103 and then Is_Constrained (Typ)
3104 and then not Has_Inferable_Discriminants (Lop)
3105 then
3106 Insert_Action (N,
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.
3113 Rewrite (N,
3114 New_Occurrence_Of (Standard_False, Loc));
3116 return;
3117 end if;
3119 -- Here we have a non-scalar type
3121 if Is_Acc then
3122 Typ := Designated_Type (Typ);
3123 end if;
3125 if not Is_Constrained (Typ) then
3126 Rewrite (N,
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
3138 (E : Node_Id;
3139 Nam : Name_Id;
3140 Dim : Nat) return Node_Id;
3141 -- Build attribute reference E'Nam(Dim)
3143 -----------------------------------
3144 -- Construct_Attribute_Reference --
3145 -----------------------------------
3147 function Construct_Attribute_Reference
3148 (E : Node_Id;
3149 Nam : Name_Id;
3150 Dim : Nat) return Node_Id
3152 begin
3153 return
3154 Make_Attribute_Reference (Loc,
3155 Prefix => E,
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
3163 begin
3164 for J in 1 .. Number_Dimensions (Typ) loop
3165 Evolve_And_Then (Cond,
3166 Make_Op_Eq (Loc,
3167 Left_Opnd =>
3168 Construct_Attribute_Reference
3169 (Duplicate_Subexpr_No_Checks (Obj),
3170 Name_First, J),
3171 Right_Opnd =>
3172 Construct_Attribute_Reference
3173 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
3175 Evolve_And_Then (Cond,
3176 Make_Op_Eq (Loc,
3177 Left_Opnd =>
3178 Construct_Attribute_Reference
3179 (Duplicate_Subexpr_No_Checks (Obj),
3180 Name_Last, J),
3181 Right_Opnd =>
3182 Construct_Attribute_Reference
3183 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
3184 end loop;
3186 if Is_Acc then
3187 Cond :=
3188 Make_Or_Else (Loc,
3189 Left_Opnd =>
3190 Make_Op_Eq (Loc,
3191 Left_Opnd => Obj,
3192 Right_Opnd => Make_Null (Loc)),
3193 Right_Opnd => Cond);
3194 end if;
3196 Rewrite (N, 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
3203 else
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));
3214 if Is_Acc then
3215 Cond := Make_Or_Else (Loc,
3216 Left_Opnd =>
3217 Make_Op_Eq (Loc,
3218 Left_Opnd => Obj,
3219 Right_Opnd => Make_Null (Loc)),
3220 Right_Opnd => Cond);
3221 end if;
3223 else
3224 Cond := New_Occurrence_Of (Standard_True, Loc);
3225 end if;
3227 Rewrite (N, Cond);
3228 Analyze_And_Resolve (N, Rtyp);
3229 end if;
3230 end;
3231 end if;
3232 end Expand_N_In;
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);
3244 begin
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
3259 Rewrite (N,
3260 Make_Indexed_Component (Loc,
3261 Prefix => Prefix (P),
3262 Expressions => New_List (
3263 Convert_To
3264 (Etype (First_Index (Etype (P))),
3265 First (Expressions (N))))));
3266 Analyze_And_Resolve (N, Typ);
3267 return;
3268 end if;
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));
3279 end if;
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);
3287 end if;
3289 -- All done for the non-packed case
3291 if not Is_Packed (Etype (Prefix (N))) then
3292 return;
3293 end if;
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);
3301 return;
3302 end if;
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
3324 declare
3325 Child : Node_Id := N;
3326 Parnt : Node_Id := Parent (N);
3328 begin
3329 loop
3330 if Nkind (Parnt) = N_Unchecked_Expression then
3331 null;
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
3336 and then
3337 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
3338 then
3339 return;
3341 elsif Nkind (Parnt) = N_Attribute_Reference
3342 and then (Attribute_Name (Parnt) = Name_Address
3343 or else
3344 Attribute_Name (Parnt) = Name_Size)
3345 and then Prefix (Parnt) = Child
3346 then
3347 return;
3349 elsif Nkind (Parnt) = N_Assignment_Statement
3350 and then Name (Parnt) = Child
3351 then
3352 return;
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)
3359 then
3360 Expand_Packed_Element_Reference (N);
3361 return;
3363 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
3364 and then Name (Parent (Parnt)) = Parnt
3365 then
3366 return;
3368 elsif Nkind (Parnt) = N_Attribute_Reference
3369 and then Attribute_Name (Parnt) = Name_Read
3370 and then Next (First (Expressions (Parnt))) = Child
3371 then
3372 return;
3374 elsif (Nkind (Parnt) = N_Indexed_Component
3375 or else Nkind (Parnt) = N_Selected_Component)
3376 and then Prefix (Parnt) = Child
3377 then
3378 null;
3380 else
3381 Expand_Packed_Element_Reference (N);
3382 return;
3383 end if;
3385 -- Keep looking up tree for unchecked expression, or if we are
3386 -- the prefix of a possible assignment left side.
3388 Child := Parnt;
3389 Parnt := Parent (Child);
3390 end loop;
3391 end;
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);
3406 begin
3407 Rewrite (N,
3408 Make_Op_Not (Loc,
3409 Right_Opnd =>
3410 Make_In (Loc,
3411 Left_Opnd => Left_Opnd (N),
3412 Right_Opnd => Right_Opnd (N))));
3413 Analyze_And_Resolve (N, Typ);
3414 end Expand_N_Not_In;
3416 -------------------
3417 -- Expand_N_Null --
3418 -------------------
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);
3429 Agg : Node_Id;
3431 begin
3432 if Ekind (Typ) = E_Access_Protected_Subprogram_Type then
3433 Agg :=
3434 Make_Aggregate (Loc,
3435 Expressions => New_List (
3436 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
3437 Make_Null (Loc)));
3439 Rewrite (N, Agg);
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.
3446 Set_Etype (N, Typ);
3447 end if;
3449 exception
3450 when RE_Not_Available =>
3451 return;
3452 end Expand_N_Null;
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);
3462 begin
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)
3470 then
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
3479 Insert_Action (N,
3480 Make_Raise_Constraint_Error (Loc,
3481 Condition =>
3482 Make_Op_Eq (Loc,
3483 Left_Opnd => Duplicate_Subexpr (Expr),
3484 Right_Opnd =>
3485 Make_Attribute_Reference (Loc,
3486 Prefix =>
3487 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
3488 Attribute_Name => Name_First)),
3489 Reason => CE_Overflow_Check_Failed));
3490 end if;
3492 -- Vax floating-point types case
3494 if Vax_Float (Etype (N)) then
3495 Expand_Vax_Arith (N);
3496 end if;
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);
3506 begin
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
3514 then
3515 Rewrite (N, Left_Opnd (N));
3516 return;
3518 elsif Compile_Time_Known_Value (Left_Opnd (N))
3519 and then Expr_Value (Left_Opnd (N)) = Uint_0
3520 then
3521 Rewrite (N, Right_Opnd (N));
3522 return;
3523 end if;
3524 end if;
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)
3530 then
3531 Apply_Arithmetic_Overflow_Check (N);
3532 return;
3534 -- Vax floating-point types case
3536 elsif Vax_Float (Typ) then
3537 Expand_Vax_Arith (N);
3538 end if;
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);
3548 begin
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);
3559 end if;
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:
3571 -- 0 None available
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
3582 Opnds : List_Id;
3583 -- List of operands to be concatenated
3585 Opnd : Node_Id;
3586 -- Single operand for concatenation
3588 Cnode : Node_Id;
3589 -- Node which is to be replaced by the result of concatenating
3590 -- the nodes in the list Opnds.
3592 Atyp : Entity_Id;
3593 -- Array type of concatenation result type
3595 Ctyp : Entity_Id;
3596 -- Component type of concatenation represented by Cnode
3598 begin
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;
3610 else
3611 Max_Available_String_Operands := 5;
3612 end if;
3614 Char_Concat_Available :=
3615 RTE_Available (RE_Str_Concat_CC)
3616 and then
3617 RTE_Available (RE_Str_Concat_CS)
3618 and then
3619 RTE_Available (RE_Str_Concat_SC);
3620 end if;
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))
3632 then
3633 return;
3634 end if;
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.
3641 Cnode := N;
3642 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
3643 Cnode := Left_Opnd (Cnode);
3644 end loop;
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.
3654 Outer : loop
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
3668 or else
3669 Max_Available_String_Operands = 0
3670 or else
3671 List_Length (Opnds) <
3672 Max_Available_String_Operands)
3673 and then Base_Type (Etype (Cnode)) =
3674 Base_Type (Etype (Parent (Cnode)))
3675 loop
3676 Cnode := Parent (Cnode);
3677 Append (Right_Opnd (Cnode), Opnds);
3678 end loop Inner;
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
3690 then
3691 Opnd := First (Opnds);
3692 loop
3693 if Base_Type (Etype (Opnd)) = Ctyp then
3694 Rewrite (Opnd,
3695 Make_Aggregate (Sloc (Cnode),
3696 Expressions => New_List (Relocate_Node (Opnd))));
3697 Analyze_And_Resolve (Opnd, Atyp);
3698 end if;
3700 Next (Opnd);
3701 exit when No (Opnd);
3702 end loop;
3703 end if;
3705 -- Now call appropriate continuation routine
3707 if Atyp = Standard_String
3708 and then Max_Available_String_Operands > 0
3709 then
3710 Expand_Concatenate_String (Cnode, Opnds);
3711 else
3712 Expand_Concatenate_Other (Cnode, Opnds);
3713 end if;
3715 exit Outer when Cnode = N;
3716 Cnode := Parent (Cnode);
3717 end loop Outer;
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);
3730 begin
3731 Binary_Op_Validity_Checks (N);
3733 -- Vax_Float is a special case
3735 if Vax_Float (Typ) then
3736 Expand_Vax_Arith (N);
3737 return;
3738 end if;
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
3745 then
3746 Rewrite (N, Left_Opnd (N));
3747 return;
3748 end if;
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.
3760 and then
3761 (Esize (Ltyp) <= 32
3762 or else Support_Long_Shifts_On_Target)
3763 then
3764 Rewrite (N,
3765 Make_Op_Shift_Right (Loc,
3766 Left_Opnd => Left_Opnd (N),
3767 Right_Opnd =>
3768 Convert_To (Standard_Natural, Right_Opnd (Right_Opnd (N)))));
3769 Analyze_And_Resolve (N, Typ);
3770 return;
3771 end if;
3773 -- Do required fixup of universal fixed operation
3775 if Typ = Universal_Fixed then
3776 Fixup_Universal_Fixed_Operation (N);
3777 Typ := Etype (N);
3778 end if;
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);
3791 else
3792 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
3793 end if;
3794 end if;
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)
3802 then
3803 if Is_Integer_Type (Typ) then
3804 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
3805 else
3806 pragma Assert (Is_Floating_Point_Type (Typ));
3807 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
3808 end if;
3810 -- Mixed-mode operations can appear in a non-static universal
3811 -- context, in which case the integer argument must be converted
3812 -- explicitly.
3814 elsif Typ = Universal_Real
3815 and then Is_Integer_Type (Rtyp)
3816 then
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)
3824 then
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
3839 then
3840 Error_Msg_CRT ("64-bit division", N);
3841 end if;
3842 end if;
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;
3859 Prim : Elmt_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
3864 -- inherited.
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);
3879 begin
3880 if Base_Type (Op_Type) /= Base_Type (A_Typ)
3881 and then not Is_Class_Wide_Type (A_Typ)
3882 then
3883 L_Exp := OK_Convert_To (Op_Type, L_Exp);
3884 R_Exp := OK_Convert_To (Op_Type, R_Exp);
3885 end if;
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
3893 declare
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;
3899 begin
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
3907 -- . . .
3908 -- end record;
3909 -- pragma Unchecked_Union (UU_Type);
3911 -- 1. Unchecked_Union enclosing record:
3913 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
3914 -- . . .
3915 -- Comp : UU_Type (Discr);
3916 -- . . .
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 [. . .]
3925 -- Generated code:
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:
3935 -- type
3936 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
3937 -- is record
3938 -- . . .
3939 -- Comp : UU_Type (Discr);
3940 -- . . .
3941 -- end Enclosing_Non_UU_Type;
3943 -- Obj1 : Enclosing_Non_UU_Type;
3944 -- Obj2 : Enclosing_Non_UU_Type (1);
3946 -- . . . Obj1 = Obj2 . . .
3948 -- Generated code:
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.
3956 -- Lhs of equality
3958 if Nkind (Lhs) = N_Selected_Component
3959 and then Has_Per_Object_Constraint
3960 (Entity (Selector_Name (Lhs)))
3961 then
3962 -- Enclosing record is an Unchecked_Union, use formal A
3964 if Is_Unchecked_Union (Scope
3965 (Entity (Selector_Name (Lhs))))
3966 then
3967 Lhs_Discr_Val :=
3968 Make_Identifier (Loc,
3969 Chars => Name_A);
3971 -- Enclosing record is of a non-Unchecked_Union type, it is
3972 -- possible to reference the discriminant.
3974 else
3975 Lhs_Discr_Val :=
3976 Make_Selected_Component (Loc,
3977 Prefix => Prefix (Lhs),
3978 Selector_Name =>
3979 New_Copy
3980 (Get_Discriminant_Value
3981 (First_Discriminant (Lhs_Type),
3982 Lhs_Type,
3983 Stored_Constraint (Lhs_Type))));
3984 end if;
3986 -- Comment needed here ???
3988 else
3989 -- Infer the discriminant value
3991 Lhs_Discr_Val :=
3992 New_Copy
3993 (Get_Discriminant_Value
3994 (First_Discriminant (Lhs_Type),
3995 Lhs_Type,
3996 Stored_Constraint (Lhs_Type)));
3997 end if;
3999 -- Rhs of equality
4001 if Nkind (Rhs) = N_Selected_Component
4002 and then Has_Per_Object_Constraint
4003 (Entity (Selector_Name (Rhs)))
4004 then
4005 if Is_Unchecked_Union
4006 (Scope (Entity (Selector_Name (Rhs))))
4007 then
4008 Rhs_Discr_Val :=
4009 Make_Identifier (Loc,
4010 Chars => Name_B);
4012 else
4013 Rhs_Discr_Val :=
4014 Make_Selected_Component (Loc,
4015 Prefix => Prefix (Rhs),
4016 Selector_Name =>
4017 New_Copy (Get_Discriminant_Value (
4018 First_Discriminant (Rhs_Type),
4019 Rhs_Type,
4020 Stored_Constraint (Rhs_Type))));
4022 end if;
4023 else
4024 Rhs_Discr_Val :=
4025 New_Copy (Get_Discriminant_Value (
4026 First_Discriminant (Rhs_Type),
4027 Rhs_Type,
4028 Stored_Constraint (Rhs_Type)));
4030 end if;
4032 Rewrite (N,
4033 Make_Function_Call (Loc,
4034 Name => New_Reference_To (Eq, Loc),
4035 Parameter_Associations => New_List (
4036 L_Exp,
4037 R_Exp,
4038 Lhs_Discr_Val,
4039 Rhs_Discr_Val)));
4040 end;
4042 -- Normal case, not an unchecked union
4044 else
4045 Rewrite (N,
4046 Make_Function_Call (Loc,
4047 Name => New_Reference_To (Eq, Loc),
4048 Parameter_Associations => New_List (L_Exp, R_Exp)));
4049 end if;
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));
4063 Clist : Node_Id;
4064 Vpart : Node_Id;
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
4083 begin
4084 if Nkind (Comp) /= N_Component_Declaration then
4085 return False;
4086 end if;
4088 declare
4089 Sindic : constant Node_Id :=
4090 Subtype_Indication (Component_Definition (Comp));
4092 begin
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)));
4098 end if;
4100 return False;
4101 end;
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);
4113 begin
4114 if Is_Empty_List (Component_Items (Clist)) then
4115 return False;
4116 end if;
4118 declare
4119 Comp : Node_Id := First (Component_Items (Clist));
4121 begin
4122 while Present (Comp) loop
4124 -- One component is sufficent
4126 if Component_Is_Unconstrained_UU (Comp) then
4127 return True;
4128 end if;
4130 Next (Comp);
4131 end loop;
4132 end;
4134 -- None of the components withing the variant were of
4135 -- unconstrained Unchecked_Union type.
4137 return False;
4138 end Variant_Is_Unconstrained_UU;
4140 -- Start of processing for Has_Unconstrained_UU_Component
4142 begin
4143 if Null_Present (Tdef) then
4144 return False;
4145 end if;
4147 Clist := Component_List (Tdef);
4148 Vpart := Variant_Part (Clist);
4150 -- Inspect available components
4152 if Present (Component_Items (Clist)) then
4153 declare
4154 Comp : Node_Id := First (Component_Items (Clist));
4156 begin
4157 while Present (Comp) loop
4159 -- One component is sufficent
4161 if Component_Is_Unconstrained_UU (Comp) then
4162 return True;
4163 end if;
4165 Next (Comp);
4166 end loop;
4167 end;
4168 end if;
4170 -- Inspect available components withing variants
4172 if Present (Vpart) then
4173 declare
4174 Variant : Node_Id := First (Variants (Vpart));
4176 begin
4177 while Present (Variant) loop
4179 -- One component within a variant is sufficent
4181 if Variant_Is_Unconstrained_UU (Variant) then
4182 return True;
4183 end if;
4185 Next (Variant);
4186 end loop;
4187 end;
4188 end if;
4190 -- Neither the available components, nor the components inside the
4191 -- variant parts were of an unconstrained Unchecked_Union subtype.
4193 return False;
4194 end Has_Unconstrained_UU_Component;
4196 -- Start of processing for Expand_N_Op_Eq
4198 begin
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));
4206 end if;
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
4210 -- expander code.
4212 if No (Typl) then
4213 return;
4214 end if;
4216 Typl := Base_Type (Typl);
4218 -- Vax float types
4220 if Vax_Float (Typl) then
4221 Expand_Vax_Comparison (N);
4222 return;
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);
4232 -- Array types
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
4240 declare
4241 Save_Force_Validity_Checks : constant Boolean :=
4242 Force_Validity_Checks;
4243 begin
4244 Force_Validity_Checks := True;
4245 Rewrite (N,
4246 Expand_Array_Equality
4248 Relocate_Node (Lhs),
4249 Relocate_Node (Rhs),
4250 Bodies,
4251 Typl));
4252 Insert_Actions (N, Bodies);
4253 Analyze_And_Resolve (N, Standard_Boolean);
4254 Force_Validity_Checks := Save_Force_Validity_Checks;
4255 end;
4257 -- Packed case
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
4272 then
4273 null;
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.
4279 else
4280 Rewrite (N,
4281 Expand_Array_Equality
4283 Relocate_Node (Lhs),
4284 Relocate_Node (Rhs),
4285 Bodies,
4286 Typl));
4287 Insert_Actions (N, Bodies, Suppress => All_Checks);
4288 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
4289 end if;
4291 -- Record Types
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))
4309 then
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))))
4320 and then
4321 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
4323 Next_Elmt (Prim);
4324 end loop;
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).
4336 else
4337 if Is_Class_Wide_Type (Typl) then
4338 Typl := Root_Type (Typl);
4339 end if;
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))))
4346 and then
4347 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
4349 Next_Elmt (Prim);
4350 end loop;
4352 pragma Assert (Present (Prim));
4353 Op_Name := Node (Prim);
4354 end if;
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
4363 Insert_Action (N,
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.
4370 Rewrite (N,
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)
4379 and then
4380 Has_Inferable_Discriminants (Rhs)
4381 then
4382 Build_Equality_Call
4383 (TSS (Root_Type (Typl), TSS_Composite_Equality));
4385 else
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.
4390 Insert_Action (N,
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.
4397 Rewrite (N,
4398 New_Occurrence_Of (Standard_False, Loc));
4400 end if;
4402 -- If a type support function is present (for complex cases), use it
4404 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
4405 Build_Equality_Call
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.
4413 else
4414 Remove_Side_Effects (Lhs);
4415 Remove_Side_Effects (Rhs);
4416 Rewrite (N,
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);
4421 end if;
4422 end if;
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);
4429 end if;
4430 end Expand_N_Op_Eq;
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);
4445 Expv : Uint;
4446 Xnode : Node_Id;
4447 Temp : Node_Id;
4448 Rent : RE_Id;
4449 Ent : Entity_Id;
4450 Etyp : Entity_Id;
4452 begin
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))
4463 or else
4464 Is_Private_Type (Typ)
4465 or else
4466 Is_Private_Type (Exptyp)
4467 or else
4468 Rtyp /= Root_Type (Bastyp)
4469 then
4470 declare
4471 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
4472 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
4474 begin
4475 Rewrite (N,
4476 Unchecked_Convert_To (Typ,
4477 Make_Op_Expon (Loc,
4478 Left_Opnd => Unchecked_Convert_To (Bt, Base),
4479 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
4480 Analyze_And_Resolve (N, Typ);
4481 return;
4482 end;
4483 end if;
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)
4500 if Expv = 0 then
4501 if Ekind (Typ) in Integer_Kind then
4502 Xnode := Make_Integer_Literal (Loc, Intval => 1);
4503 else
4504 Xnode := Make_Real_Literal (Loc, Ureal_1);
4505 end if;
4507 -- X ** 1 = X
4509 elsif Expv = 1 then
4510 Xnode := Base;
4512 -- X ** 2 = X * X
4514 elsif Expv = 2 then
4515 Xnode :=
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
4522 elsif Expv = 3 then
4523 Xnode :=
4524 Make_Op_Multiply (Loc,
4525 Left_Opnd =>
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));
4531 -- X ** 4 ->
4532 -- En : constant base'type := base * base;
4533 -- ...
4534 -- En * En
4536 else -- Expv = 4
4537 Temp :=
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),
4545 Expression =>
4546 Make_Op_Multiply (Loc,
4547 Left_Opnd => Duplicate_Subexpr (Base),
4548 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
4550 Xnode :=
4551 Make_Op_Multiply (Loc,
4552 Left_Opnd => New_Reference_To (Temp, Loc),
4553 Right_Opnd => New_Reference_To (Temp, Loc));
4554 end if;
4556 Rewrite (N, Xnode);
4557 Analyze_And_Resolve (N, Typ);
4558 return;
4559 end if;
4560 end if;
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)
4573 and then not Ovflo
4574 and then Nkind (Parent (N)) in N_Binary_Op
4575 then
4576 declare
4577 P : constant Node_Id := Parent (N);
4578 L : constant Node_Id := Left_Opnd (P);
4579 R : constant Node_Id := Right_Opnd (P);
4581 begin
4582 if (Nkind (P) = N_Op_Multiply
4583 and then
4584 ((Is_Integer_Type (Etype (L)) and then R = N)
4585 or else
4586 (Is_Integer_Type (Etype (R)) and then L = N))
4587 and then not Do_Overflow_Check (P))
4589 or else
4590 (Nkind (P) = N_Op_Divide
4591 and then Is_Integer_Type (Etype (L))
4592 and then Is_Unsigned_Type (Etype (L))
4593 and then R = N
4594 and then not Do_Overflow_Check (P))
4595 then
4596 Set_Is_Power_Of_2_For_Shift (N);
4597 return;
4598 end if;
4599 end;
4600 end if;
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
4614 Rewrite (N,
4615 Convert_To (Typ,
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)),
4621 Exp))));
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.
4627 else
4628 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
4629 Ent := RTE (RE_Exp_Unsigned);
4630 else
4631 Ent := RTE (RE_Exp_Long_Long_Unsigned);
4632 end if;
4634 Rewrite (N,
4635 Convert_To (Typ,
4636 Make_Op_And (Loc,
4637 Left_Opnd =>
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),
4642 Exp)),
4643 Right_Opnd =>
4644 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
4646 end if;
4648 -- Common exit point for modular type case
4650 Analyze_And_Resolve (N, Typ);
4651 return;
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)
4664 and then
4665 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
4666 or else (Rtyp = Universal_Integer)
4667 then
4668 Etyp := Standard_Long_Long_Integer;
4670 if Ovflo then
4671 Rent := RE_Exp_Long_Long_Integer;
4672 else
4673 Rent := RE_Exn_Long_Long_Integer;
4674 end if;
4676 elsif Is_Signed_Integer_Type (Rtyp) then
4677 Etyp := Standard_Integer;
4679 if Ovflo then
4680 Rent := RE_Exp_Integer;
4681 else
4682 Rent := RE_Exn_Integer;
4683 end if;
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.
4691 else
4692 pragma Assert (Is_Floating_Point_Type (Rtyp));
4693 Etyp := Standard_Long_Long_Float;
4694 Rent := RE_Exn_Long_Long_Float;
4695 end if;
4697 -- Common processing for integer cases and floating-point cases.
4698 -- If we are in the right type, we can call runtime routine directly
4700 if Typ = Etyp
4701 and then Rtyp /= Universal_Integer
4702 and then Rtyp /= Universal_Real
4703 then
4704 Rewrite (N,
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.
4713 else
4714 Rewrite (N,
4715 Convert_To (Typ,
4716 Make_Function_Call (Loc,
4717 Name => New_Reference_To (RTE (Rent), Loc),
4718 Parameter_Associations => New_List (
4719 Convert_To (Etyp, Base),
4720 Exp))));
4721 end if;
4723 Analyze_And_Resolve (N, Typ);
4724 return;
4726 exception
4727 when RE_Not_Available =>
4728 return;
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));
4741 begin
4742 Binary_Op_Validity_Checks (N);
4744 if Vax_Float (Typ1) then
4745 Expand_Vax_Comparison (N);
4746 return;
4748 elsif Is_Array_Type (Typ1) then
4749 Expand_Array_Comparison (N);
4750 return;
4751 end if;
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);
4758 end if;
4760 Rewrite_Comparison (N);
4761 end Expand_N_Op_Ge;
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));
4773 begin
4774 Binary_Op_Validity_Checks (N);
4776 if Vax_Float (Typ1) then
4777 Expand_Vax_Comparison (N);
4778 return;
4780 elsif Is_Array_Type (Typ1) then
4781 Expand_Array_Comparison (N);
4782 return;
4783 end if;
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);
4790 end if;
4792 Rewrite_Comparison (N);
4793 end Expand_N_Op_Gt;
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));
4805 begin
4806 Binary_Op_Validity_Checks (N);
4808 if Vax_Float (Typ1) then
4809 Expand_Vax_Comparison (N);
4810 return;
4812 elsif Is_Array_Type (Typ1) then
4813 Expand_Array_Comparison (N);
4814 return;
4815 end if;
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);
4822 end if;
4824 Rewrite_Comparison (N);
4825 end Expand_N_Op_Le;
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));
4837 begin
4838 Binary_Op_Validity_Checks (N);
4840 if Vax_Float (Typ1) then
4841 Expand_Vax_Comparison (N);
4842 return;
4844 elsif Is_Array_Type (Typ1) then
4845 Expand_Array_Comparison (N);
4846 return;
4847 end if;
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);
4854 end if;
4856 Rewrite_Comparison (N);
4857 end Expand_N_Op_Lt;
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);
4867 begin
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)
4873 then
4874 -- Software overflow checking expands -expr into (0 - expr)
4876 Rewrite (N,
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);
4887 end if;
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);
4902 LLB : Uint;
4903 Llo : Uint;
4904 Lhi : Uint;
4905 LOK : Boolean;
4906 Rlo : Uint;
4907 Rhi : Uint;
4908 ROK : Boolean;
4910 begin
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
4922 and then
4923 ROK and then Rlo >= 0
4924 then
4925 Rewrite (N,
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));
4935 Set_Etype (N, Typ);
4936 Set_Do_Overflow_Check (N, DOC);
4937 Set_Do_Division_Check (N, DDC);
4938 Expand_N_Op_Rem (N);
4939 Set_Analyzed (N);
4941 -- Otherwise, normal mod processing
4943 else
4944 if Is_Integer_Type (Etype (N)) then
4945 Apply_Divide_Check (N);
4946 end if;
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
4955 then
4956 Rewrite (N, Make_Integer_Literal (Loc, 0));
4957 Analyze_And_Resolve (N, Typ);
4958 return;
4959 end if;
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.
4973 LLB :=
4974 Expr_Value
4975 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
4977 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
4978 and then
4979 ((not LOK) or else (Llo = LLB))
4980 then
4981 Rewrite (N,
4982 Make_Conditional_Expression (Loc,
4983 Expressions => New_List (
4984 Make_Op_Eq (Loc,
4985 Left_Opnd => Duplicate_Subexpr (Right),
4986 Right_Opnd =>
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);
4995 end if;
4996 end if;
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);
5020 begin
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)
5031 or else
5032 (Compile_Time_Known_Value (Lop)
5033 and then Expr_Value (Lop) = Uint_0)
5034 then
5035 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
5036 Analyze_And_Resolve (N, Typ);
5037 return;
5038 end if;
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
5047 and then not Lp2
5048 then
5049 Rewrite (N, Lop);
5050 return;
5052 elsif Compile_Time_Known_Value (Lop)
5053 and then Expr_Value (Lop) = Uint_1
5054 and then not Rp2
5055 then
5056 Rewrite (N, Rop);
5057 return;
5058 end if;
5059 end if;
5061 -- Deal with VAX float case
5063 if Vax_Float (Typ) then
5064 Expand_Vax_Arith (N);
5065 return;
5066 end if;
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.
5072 if Rp2 then
5073 if Lp2 then
5075 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
5077 Rewrite (N,
5078 Make_Op_Expon (Loc,
5079 Left_Opnd => Make_Integer_Literal (Loc, 2),
5080 Right_Opnd =>
5081 Make_Op_Add (Loc,
5082 Left_Opnd => Right_Opnd (Lop),
5083 Right_Opnd => Right_Opnd (Rop))));
5084 Analyze_And_Resolve (N, Typ);
5085 return;
5087 else
5088 Rewrite (N,
5089 Make_Op_Shift_Left (Loc,
5090 Left_Opnd => Lop,
5091 Right_Opnd =>
5092 Convert_To (Standard_Natural, Right_Opnd (Rop))));
5093 Analyze_And_Resolve (N, Typ);
5094 return;
5095 end if;
5097 -- Same processing for the operands the other way round
5099 elsif Lp2 then
5100 Rewrite (N,
5101 Make_Op_Shift_Left (Loc,
5102 Left_Opnd => Rop,
5103 Right_Opnd =>
5104 Convert_To (Standard_Natural, Right_Opnd (Lop))));
5105 Analyze_And_Resolve (N, Typ);
5106 return;
5107 end if;
5109 -- Do required fixup of universal fixed operation
5111 if Typ = Universal_Fixed then
5112 Fixup_Universal_Fixed_Operation (N);
5113 Typ := Etype (N);
5114 end if;
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
5138 else
5139 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
5140 end if;
5141 end if;
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)
5148 then
5149 if Is_Integer_Type (Typ) then
5150 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
5151 else
5152 pragma Assert (Is_Floating_Point_Type (Typ));
5153 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
5154 end if;
5156 -- Mixed-mode operations can appear in a non-static universal
5157 -- context, in which case the integer argument must be converted
5158 -- explicitly.
5160 elsif Typ = Universal_Real
5161 and then Is_Integer_Type (Rtyp)
5162 then
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)
5169 then
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);
5178 end if;
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);
5192 Neg : Node_Id;
5193 Ne : constant Entity_Id := Entity (N);
5195 begin
5196 Binary_Op_Validity_Checks (N);
5198 Neg :=
5199 Make_Op_Not (Loc,
5200 Right_Opnd =>
5201 Make_Op_Eq (Loc,
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));
5208 end if;
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);
5216 Rewrite (N, Neg);
5217 Analyze_And_Resolve (N, Standard_Boolean);
5218 end Expand_N_Op_Ne;
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
5238 -- B : arr;
5239 -- begin
5240 -- for J in a'range loop
5241 -- B (J) := not A (J);
5242 -- end loop;
5243 -- return B;
5244 -- end Nnnn;
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);
5252 Opnd : Node_Id;
5253 Arr : Entity_Id;
5254 A : Entity_Id;
5255 B : Entity_Id;
5256 J : Entity_Id;
5257 A_J : Node_Id;
5258 B_J : Node_Id;
5260 Func_Name : Entity_Id;
5261 Loop_Statement : Node_Id;
5263 begin
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);
5272 return;
5273 end if;
5275 -- Only array types need any other processing
5277 if not Is_Array_Type (Typ) then
5278 return;
5279 end if;
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);
5285 return;
5286 end if;
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);
5301 return;
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))
5310 then
5311 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
5312 return;
5313 end if;
5315 elsif Nkind (Parent (N)) in N_Binary_Op
5316 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
5317 then
5318 declare
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)));
5323 begin
5324 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
5325 if N = Op1
5326 and then Nkind (Op2) = N_Op_Not
5327 then
5328 -- (not A) op (not B) can be reduced to a single call
5330 return;
5332 elsif N = Op2
5333 and then Nkind (Parent (N)) = N_Op_Xor
5334 then
5335 -- A xor (not B) can also be special-cased
5337 return;
5338 end if;
5339 end if;
5340 end;
5341 end if;
5343 A := Make_Defining_Identifier (Loc, Name_uA);
5344 B := Make_Defining_Identifier (Loc, Name_uB);
5345 J := Make_Defining_Identifier (Loc, Name_uJ);
5347 A_J :=
5348 Make_Indexed_Component (Loc,
5349 Prefix => New_Reference_To (A, Loc),
5350 Expressions => New_List (New_Reference_To (J, Loc)));
5352 B_J :=
5353 Make_Indexed_Component (Loc,
5354 Prefix => New_Reference_To (B, Loc),
5355 Expressions => New_List (New_Reference_To (J, Loc)));
5357 Loop_Statement :=
5358 Make_Implicit_Loop_Statement (N,
5359 Identifier => Empty,
5361 Iteration_Scheme =>
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,
5373 Name => B_J,
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);
5379 Insert_Action (N,
5380 Make_Subprogram_Body (Loc,
5381 Specification =>
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 (
5398 Loop_Statement,
5399 Make_Return_Statement (Loc,
5400 Expression =>
5401 Make_Identifier (Loc, Chars (B)))))));
5403 Rewrite (N,
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);
5418 begin
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);
5429 end if;
5430 end Expand_N_Op_Or;
5432 ----------------------
5433 -- Expand_N_Op_Plus --
5434 ----------------------
5436 procedure Expand_N_Op_Plus (N : Node_Id) is
5437 begin
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);
5452 LLB : Uint;
5453 Llo : Uint;
5454 Lhi : Uint;
5455 LOK : Boolean;
5456 Rlo : Uint;
5457 Rhi : Uint;
5458 ROK : Boolean;
5460 begin
5461 Binary_Op_Validity_Checks (N);
5463 if Is_Integer_Type (Etype (N)) then
5464 Apply_Divide_Check (N);
5465 end if;
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
5474 then
5475 Rewrite (N, Make_Integer_Literal (Loc, 0));
5476 Analyze_And_Resolve (N, Typ);
5477 return;
5478 end if;
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.
5495 LLB :=
5496 Expr_Value
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))
5502 and then
5503 ((not LOK) or else (Llo = LLB))
5504 then
5505 Rewrite (N,
5506 Make_Conditional_Expression (Loc,
5507 Expressions => New_List (
5508 Make_Op_Eq (Loc,
5509 Left_Opnd => Duplicate_Subexpr (Right),
5510 Right_Opnd =>
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);
5521 end if;
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
5529 begin
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
5538 begin
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
5547 begin
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
5556 begin
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
5565 begin
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);
5576 begin
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
5584 then
5585 Rewrite (N, Left_Opnd (N));
5586 return;
5587 end if;
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)
5593 then
5594 Apply_Arithmetic_Overflow_Check (N);
5596 -- Vax floating-point types case
5598 elsif Vax_Float (Typ) then
5599 Expand_Vax_Arith (N);
5600 end if;
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);
5610 begin
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);
5621 end if;
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);
5636 Actlist : List_Id;
5638 begin
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);
5645 end if;
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));
5658 end if;
5660 Rewrite (N, Right);
5661 Adjust_Result_Type (N, Typ);
5662 return;
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);
5673 return;
5674 end if;
5675 end if;
5677 -- If Actions are present, we expand
5679 -- left or else right
5681 -- into
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);
5691 Rewrite (N,
5692 Make_Conditional_Expression (Loc,
5693 Expressions => New_List (
5694 Left,
5695 New_Occurrence_Of (Standard_True, Loc),
5696 Right)));
5698 Set_Else_Actions (N, Actlist);
5699 Analyze_And_Resolve (N, Standard_Boolean);
5700 Adjust_Result_Type (N, Typ);
5701 return;
5702 end if;
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
5713 Rewrite (N, Left);
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);
5720 Rewrite
5721 (N, New_Occurrence_Of (Standard_True, Loc));
5722 end if;
5723 end if;
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));
5736 begin
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));
5752 Disc : Entity_Id;
5753 New_N : Node_Id;
5754 Dcon : Elmt_Id;
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
5766 begin
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
5776 begin
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))
5785 then
5786 Set_Etype (P, Base_Type (Etype (P)));
5787 end if;
5789 Ptyp := Etype (P);
5790 end if;
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.
5799 Add_Inlined_Body
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);
5807 end if;
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)
5818 then
5819 Force_Evaluation (Prefix (N));
5820 end if;
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)
5833 then
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
5838 null;
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
5847 then
5848 null;
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)
5857 then
5858 null;
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
5865 null;
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.
5871 else
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)
5891 then
5892 exit Discr_Loop;
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)
5901 then
5902 Rewrite (N,
5903 Make_Qualified_Expression (Loc,
5904 Subtype_Mark =>
5905 New_Occurrence_Of (Etype (Disc), Loc),
5906 Expression =>
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);
5914 return;
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.
5923 else
5924 Rewrite (N, New_Copy_Tree (Node (Dcon)));
5925 Analyze_And_Resolve (N);
5926 Set_Is_Static_Expression (N, False);
5927 return;
5928 end if;
5929 end if;
5931 Next_Elmt (Dcon);
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.
5940 end if;
5941 end if;
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
5950 return;
5951 end if;
5953 Disc := Entity (Selector_Name (N));
5955 if Is_Derived_Type (Ptyp)
5956 and then Present (Corresponding_Discriminant (Disc))
5957 then
5958 Disc := Corresponding_Discriminant (Disc);
5959 end if;
5961 New_N :=
5962 Make_Selected_Component (Loc,
5963 Prefix =>
5964 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
5965 New_Copy_Tree (P)),
5966 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
5968 Rewrite (N, New_N);
5969 Analyze (N);
5970 end if;
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);
6007 begin
6008 loop
6009 -- If our parent is a procedure call we can return
6011 if Nkind (Par) = N_Procedure_Call_Statement then
6012 return True;
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
6023 then
6024 Par := Parent (Par);
6026 -- Any other case is not what we are looking for
6028 else
6029 return False;
6030 end if;
6031 end loop;
6032 end Is_Procedure_Actual;
6034 --------------------
6035 -- Make_Temporary --
6036 --------------------
6038 procedure Make_Temporary is
6039 Decl : Node_Id;
6040 Ent : constant Entity_Id :=
6041 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
6042 begin
6043 Decl :=
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 (
6051 Decl,
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);
6058 end Make_Temporary;
6060 -- Start of processing for Expand_N_Slice
6062 begin
6063 -- Special handling for access types
6065 if Is_Access_Type (Ptp) then
6067 Ptp := Designated_Type (Ptp);
6069 Rewrite (Pfx,
6070 Make_Explicit_Dereference (Sloc (N),
6071 Prefix => Relocate_Node (Pfx)));
6073 Analyze_And_Resolve (Pfx, Ptp);
6074 end if;
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
6084 then
6085 Enable_Range_Check (Discrete_Range (N));
6086 end if;
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
6102 -- call itself.
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)
6115 then
6116 Make_Temporary;
6117 end if;
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))))
6122 then
6123 return;
6125 elsif Nkind (Parent (N)) = N_Indexed_Component
6126 or else Is_Renamed_Object (N)
6127 or else Is_Procedure_Actual (N)
6128 then
6129 return;
6131 elsif Nkind (Parent (N)) = N_Attribute_Reference
6132 and then Attribute_Name (Parent (N)) = Name_Address
6133 then
6134 return;
6136 else
6137 Make_Temporary;
6138 end if;
6139 end Expand_N_Slice;
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
6167 Temp : Entity_Id;
6168 Decl : Node_Id;
6169 Odef : Node_Id;
6170 Disc : Node_Id;
6171 N_Ix : Node_Id;
6172 Cons : List_Id;
6174 begin
6175 -- Nothing to do if no change of representation
6177 if Same_Representation (Operand_Type, Target_Type) then
6178 return;
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
6186 return;
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.
6192 else
6193 Cons := No_List;
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);
6204 end if;
6206 Cons := New_List;
6207 while Present (Disc) loop
6208 Append_To (Cons,
6209 Make_Selected_Component (Loc,
6210 Prefix => Duplicate_Subexpr_Move_Checks (Operand),
6211 Selector_Name =>
6212 Make_Identifier (Loc, Chars (Disc))));
6213 Next_Discriminant (Disc);
6214 end loop;
6216 elsif Is_Array_Type (Operand_Type) then
6217 N_Ix := First_Index (Target_Type);
6218 Cons := New_List;
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.
6225 Append_To (Cons,
6226 Make_Range (Loc,
6227 Low_Bound =>
6228 Unchecked_Convert_To (Etype (N_Ix),
6229 Make_Attribute_Reference (Loc,
6230 Prefix =>
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)))),
6237 High_Bound =>
6238 Unchecked_Convert_To (Etype (N_Ix),
6239 Make_Attribute_Reference (Loc,
6240 Prefix =>
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))))));
6247 Next_Index (N_Ix);
6248 end loop;
6249 end if;
6250 end if;
6252 Odef := New_Occurrence_Of (Target_Type, Loc);
6254 if Present (Cons) then
6255 Odef :=
6256 Make_Subtype_Indication (Loc,
6257 Subtype_Mark => Odef,
6258 Constraint =>
6259 Make_Index_Or_Discriminant_Constraint (Loc,
6260 Constraints => Cons));
6261 end if;
6263 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
6264 Decl :=
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.
6275 Insert_Actions (N,
6276 New_List (
6277 Decl,
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));
6284 return;
6285 end if;
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,
6294 -- we convert:
6296 -- typ (x)
6298 -- to
6300 -- Tnn : typ'Base := typ'Base (x);
6301 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
6302 -- Tnn
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);
6314 Conv : Node_Id;
6315 Tnn : Entity_Id;
6317 begin
6318 -- Nothing to do if conversion was rewritten
6320 if Nkind (N) /= N_Type_Conversion then
6321 return;
6322 end if;
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)
6329 and then
6330 Hi = Type_High_Bound (Btyp))
6331 then
6332 return;
6333 end if;
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))
6340 then
6341 return;
6342 end if;
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)
6349 declare
6350 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
6351 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
6353 begin
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)
6360 then
6361 declare
6362 D_Lov : constant Ureal := Expr_Value_R (Lo);
6363 D_Hiv : constant Ureal := Expr_Value_R (Hi);
6364 S_Lov : Ureal;
6365 S_Hiv : Ureal;
6367 begin
6368 if Is_Real_Type (Xtyp) then
6369 S_Lov := Expr_Value_R (S_Lo);
6370 S_Hiv := Expr_Value_R (S_Hi);
6371 else
6372 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
6373 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
6374 end if;
6376 if D_Hiv > D_Lov
6377 and then S_Lov >= D_Lov
6378 and then S_Hiv <= D_Hiv
6379 then
6380 Set_Do_Range_Check (Operand, False);
6381 return;
6382 end if;
6383 end;
6384 end if;
6385 end;
6387 -- For float to float conversions, we are done
6389 if Is_Floating_Point_Type (Xtyp)
6390 and then
6391 Is_Floating_Point_Type (Btyp)
6392 then
6393 return;
6394 end if;
6396 -- Otherwise rewrite the conversion as described above
6398 Conv := Relocate_Node (N);
6399 Rewrite
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);
6409 end if;
6411 Tnn :=
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,
6422 Condition =>
6423 Make_Or_Else (Loc,
6424 Left_Opnd =>
6425 Make_Op_Lt (Loc,
6426 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
6427 Right_Opnd =>
6428 Make_Attribute_Reference (Loc,
6429 Attribute_Name => Name_First,
6430 Prefix =>
6431 New_Occurrence_Of (Target_Type, Loc))),
6433 Right_Opnd =>
6434 Make_Op_Gt (Loc,
6435 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
6436 Right_Opnd =>
6437 Make_Attribute_Reference (Loc,
6438 Attribute_Name => Name_Last,
6439 Prefix =>
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
6449 begin
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));
6455 return;
6456 end if;
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);
6462 return;
6463 end if;
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
6472 then
6473 return;
6474 end if;
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))
6482 then
6483 Adjust_Condition (Operand);
6484 Set_Etype (Operand, Standard_Boolean);
6485 Operand_Type := Standard_Boolean;
6486 end if;
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
6499 then
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
6508 -- cases.
6510 elsif In_Instance_Body
6511 and then Type_Access_Level (Operand_Type) >
6512 Type_Access_Level (Target_Type)
6513 then
6514 Rewrite (N,
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)
6531 then
6532 Rewrite (N,
6533 Make_Raise_Program_Error (Sloc (N),
6534 Reason => PE_Accessibility_Check_Failed));
6535 Set_Etype (N, Target_Type);
6536 end if;
6537 end if;
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)
6557 then
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)
6565 then
6566 return;
6567 end if;
6569 -- Oherwise, proceed with processing tagged conversion
6571 declare
6572 Actual_Operand_Type : Entity_Id;
6573 Actual_Target_Type : Entity_Id;
6575 Cond : Node_Id;
6577 begin
6578 if Is_Access_Type (Target_Type) then
6579 Actual_Operand_Type := Designated_Type (Operand_Type);
6580 Actual_Target_Type := Designated_Type (Target_Type);
6582 else
6583 Actual_Operand_Type := Operand_Type;
6584 Actual_Target_Type := Target_Type;
6585 end if;
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),
6591 Actual_Target_Type)
6592 and then not Tag_Checks_Suppressed (Actual_Target_Type)
6593 then
6594 -- The conversion is valid for any descendant of the
6595 -- target type
6597 Actual_Target_Type := Class_Wide_Type (Actual_Target_Type);
6599 if Is_Access_Type (Target_Type) then
6600 Cond :=
6601 Make_And_Then (Loc,
6602 Left_Opnd =>
6603 Make_Op_Ne (Loc,
6604 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
6605 Right_Opnd => Make_Null (Loc)),
6607 Right_Opnd =>
6608 Make_Not_In (Loc,
6609 Left_Opnd =>
6610 Make_Explicit_Dereference (Loc,
6611 Prefix =>
6612 Duplicate_Subexpr_No_Checks (Operand)),
6613 Right_Opnd =>
6614 New_Reference_To (Actual_Target_Type, Loc)));
6616 else
6617 Cond :=
6618 Make_Not_In (Loc,
6619 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
6620 Right_Opnd =>
6621 New_Reference_To (Actual_Target_Type, Loc));
6622 end if;
6624 Insert_Action (N,
6625 Make_Raise_Constraint_Error (Loc,
6626 Condition => Cond,
6627 Reason => CE_Tag_Check_Failed));
6629 declare
6630 Conv : Node_Id;
6631 begin
6632 Conv :=
6633 Make_Unchecked_Type_Conversion (Loc,
6634 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
6635 Expression => Relocate_Node (Expression (N)));
6636 Rewrite (N, Conv);
6637 Analyze_And_Resolve (N, Target_Type);
6638 end;
6639 end if;
6640 end;
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)
6656 then
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
6672 then
6673 Set_Rounded_Result (N);
6674 Set_Etype (N, Etype (Parent (N)));
6675 end if;
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);
6685 Real_Range_Check;
6687 elsif Is_Integer_Type (Etype (N)) then
6688 Expand_Convert_Fixed_To_Integer (N);
6690 else
6691 pragma Assert (Is_Floating_Point_Type (Etype (N)));
6692 Expand_Convert_Fixed_To_Float (N);
6693 Real_Range_Check;
6694 end if;
6695 end if;
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)
6706 then
6707 if Is_Integer_Type (Operand_Type) then
6708 Expand_Convert_Integer_To_Fixed (N);
6709 Real_Range_Check;
6710 else
6711 pragma Assert (Is_Floating_Point_Type (Operand_Type));
6712 Expand_Convert_Float_To_Fixed (N);
6713 Real_Range_Check;
6714 end if;
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)
6723 and then
6724 (Is_Integer_Type (Target_Type)
6725 or else
6726 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
6727 then
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))
6733 -- by
6735 -- ityp (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
6741 then
6742 Rewrite (Operand,
6743 Relocate_Node (First (Expressions (Operand))));
6744 Set_Float_Truncate (N, True);
6745 end if;
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
6756 Rewrite (Operand,
6757 Make_Type_Conversion (Loc,
6758 Subtype_Mark =>
6759 New_Occurrence_Of (Standard_Long_Long_Float, Loc),
6760 Expression =>
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);
6766 end if;
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);
6778 else
6779 Apply_Range_Check (Operand, Target_Type);
6780 end if;
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)
6791 then
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)
6810 then
6811 -- To prevent Gigi from generating illegal code, we make a
6812 -- Program_Error node, but we give it the target type of the
6813 -- conversion.
6815 declare
6816 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
6817 Reason => PE_Unchecked_Union_Restriction);
6819 begin
6820 Set_Etype (PE, Target_Type);
6821 Rewrite (N, PE);
6823 end;
6824 else
6825 Handle_Changed_Representation;
6826 end if;
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))
6839 Rewrite (N,
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);
6850 end if;
6852 -- Case of conversions to floating-point
6854 elsif Is_Floating_Point_Type (Target_Type) then
6855 Real_Range_Check;
6857 -- The remaining cases require no front end processing
6859 else
6860 null;
6861 end if;
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))
6885 then
6886 declare
6887 Expr : constant Node_Id := Expression (N);
6888 Ftyp : Entity_Id;
6889 Ityp : Entity_Id;
6891 begin
6892 if Do_Range_Check (Expr)
6893 and then Is_Discrete_Type (Etype (Expr))
6894 then
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)
6907 then
6908 Ftyp := Base_Type (Etype (Expr));
6910 if Esize (Ftyp) >= Esize (Standard_Integer) then
6911 Ityp := Standard_Long_Long_Integer;
6912 else
6913 Ityp := Standard_Integer;
6914 end if;
6916 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
6917 end if;
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
6923 -- integer type.
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)
6928 then
6929 Generate_Range_Check
6930 (Expr, Target_Type, CE_Range_Check_Failed);
6931 end if;
6932 end if;
6933 end;
6934 end if;
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);
6949 begin
6950 Set_Assignment_OK (Exp, Assignment_OK (N) or Assignment_OK (Exp));
6951 Rewrite (N, 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);
6966 begin
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)
6981 then
6982 declare
6983 Val : constant Uint := Expr_Value (Operand);
6985 begin
6986 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
6987 and then
6988 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
6989 and then
6990 Val >= Expr_Value (Type_Low_Bound (Target_Type))
6991 and then
6992 Val <= Expr_Value (Type_High_Bound (Target_Type))
6993 then
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);
7002 else
7003 Analyze_And_Resolve (N, Target_Type);
7004 end if;
7006 return;
7007 end if;
7008 end;
7009 end if;
7011 -- Nothing to do if conversion is safe
7013 if Safe_Unchecked_Type_Conversion (N) then
7014 return;
7015 end if;
7017 -- Otherwise force evaluation unless Assignment_OK flag is set (this
7018 -- flag indicates ??? -- more comments needed here)
7020 if Assignment_OK (N) then
7021 null;
7022 else
7023 Force_Evaluation (N);
7024 end if;
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
7034 -- and then ...
7035 -- and then Lhs.Discrn = Rhs.Discrn
7036 -- and then Lhs.Cmp1 = Rhs.Cmp1
7037 -- and then ...
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
7046 (Nod : Node_Id;
7047 Typ : Entity_Id;
7048 Lhs : Node_Id;
7049 Rhs : Node_Id;
7050 Bodies : List_Id) return Node_Id
7052 Loc : constant Source_Ptr := Sloc (Nod);
7054 Result : Node_Id;
7055 C : Entity_Id;
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
7068 begin
7069 if No (C) then
7070 return Empty;
7072 elsif Ekind (C) /= E_Discriminant
7073 and then Ekind (C) /= E_Component
7074 then
7075 return Suitable_Element (Next_Entity (C));
7077 elsif Is_Tagged_Type (Typ)
7078 and then C /= Original_Record_Component (C)
7079 then
7080 return Suitable_Element (Next_Entity (C));
7082 elsif Chars (C) = Name_uController
7083 or else Chars (C) = Name_uTag
7084 then
7085 return Suitable_Element (Next_Entity (C));
7087 else
7088 return C;
7089 end if;
7090 end Suitable_Element;
7092 -- Start of processing for Expand_Record_Equality
7094 begin
7095 -- Generates the following code: (assuming that Typ has one Discr and
7096 -- component C2 is also a record)
7098 -- True
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
7102 -- and then ...
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
7109 declare
7110 New_Lhs : Node_Id;
7111 New_Rhs : Node_Id;
7112 Check : Node_Id;
7114 begin
7115 if First_Time then
7116 First_Time := False;
7117 New_Lhs := Lhs;
7118 New_Rhs := Rhs;
7119 else
7120 New_Lhs := New_Copy_Tree (Lhs);
7121 New_Rhs := New_Copy_Tree (Rhs);
7122 end if;
7124 Check :=
7125 Expand_Composite_Equality (Nod, Etype (C),
7126 Lhs =>
7127 Make_Selected_Component (Loc,
7128 Prefix => New_Lhs,
7129 Selector_Name => New_Reference_To (C, Loc)),
7130 Rhs =>
7131 Make_Selected_Component (Loc,
7132 Prefix => New_Rhs,
7133 Selector_Name => New_Reference_To (C, Loc)),
7134 Bodies => Bodies);
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
7140 Result := Check;
7141 Set_Etype (Result, Standard_Boolean);
7142 exit;
7143 else
7144 Result :=
7145 Make_And_Then (Loc,
7146 Left_Opnd => Result,
7147 Right_Opnd => Check);
7148 end if;
7149 end;
7151 C := Suitable_Element (Next_Entity (C));
7152 end loop;
7154 return Result;
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);
7164 begin
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
7176 then
7177 Set_Etype (N, Etype (Parent (Conv)));
7178 Set_Rounded_Result (N);
7180 -- Normal case where type comes from conversion above us
7182 else
7183 Set_Etype (N, Etype (Conv));
7184 end if;
7185 end Fixup_Universal_Fixed_Operation;
7187 ------------------------------
7188 -- Get_Allocator_Final_List --
7189 ------------------------------
7191 function Get_Allocator_Final_List
7192 (N : Node_Id;
7193 T : Entity_Id;
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.
7202 begin
7203 if Ekind (PtrT) = E_Anonymous_Access_Type then
7204 if Nkind (Associated_Node_For_Itype (PtrT))
7205 in N_Subprogram_Specification
7206 then
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'));
7216 Insert_Action (N,
7217 Make_Full_Type_Declaration (Loc,
7218 Defining_Identifier => Owner,
7219 Type_Definition =>
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));
7227 else
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);
7233 end if;
7234 end if;
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;
7256 begin
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
7261 loop
7262 Sel_Comp := Parent (Sel_Comp);
7263 end loop;
7265 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
7266 end Prefix_Is_Formal_Parameter;
7268 -- Start of processing for Has_Inferable_Discriminants
7270 begin
7271 -- For identifiers and indexed components, it is sufficent to have a
7272 -- constrained Unchecked_Union nominal subtype.
7274 if Nkind (N) = N_Identifier
7275 or else
7276 Nkind (N) = N_Indexed_Component
7277 then
7278 return Is_Unchecked_Union (Base_Type (Etype (N)))
7279 and then
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
7285 -- discriminants.
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
7295 return True;
7296 end if;
7298 -- Otherwise, check the enclosing object and the selector
7300 return Has_Inferable_Discriminants (Prefix (N))
7301 and then
7302 Has_Inferable_Discriminants (Selector_Name (N));
7303 end if;
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))
7315 and then
7316 Is_Constrained (Subtype_Mark (N));
7318 end if;
7320 return False;
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
7341 T : Entity_Id;
7343 begin
7344 if No (P) then
7345 return False;
7346 end if;
7348 T := Etype (P);
7349 while T /= Etype (T) loop
7350 if Is_RTE (T, RE_Checked_Pool) then
7351 return True;
7352 else
7353 T := Etype (T);
7354 end if;
7355 end loop;
7357 return False;
7358 end Is_Checked_Storage_Pool;
7360 -- Start of processing for Insert_Dereference_Action
7362 begin
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)))
7367 then
7368 return;
7369 end if;
7371 Insert_Action (N,
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 (
7378 -- Pool
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,
7395 Left_Opnd =>
7396 Make_Attribute_Reference (Loc,
7397 Prefix =>
7398 Make_Explicit_Dereference (Loc,
7399 Duplicate_Subexpr_Move_Checks (N)),
7400 Attribute_Name => Name_Size),
7401 Right_Opnd =>
7402 Make_Integer_Literal (Loc, System_Storage_Unit)),
7404 -- Alignment
7406 Make_Attribute_Reference (Loc,
7407 Prefix =>
7408 Make_Explicit_Dereference (Loc,
7409 Duplicate_Subexpr_Move_Checks (N)),
7410 Attribute_Name => Name_Alignment))));
7412 exception
7413 when RE_Not_Available =>
7414 return;
7415 end Insert_Dereference_Action;
7417 ------------------------------
7418 -- Make_Array_Comparison_Op --
7419 ------------------------------
7421 -- This is a hand-coded expansion of the following generic function:
7423 -- generic
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;
7431 -- begin
7432 -- if X'length = 0 then
7433 -- return false;
7435 -- elsif Y'length = 0 then
7436 -- return true;
7438 -- else
7439 -- for I in X'range loop
7440 -- if X (I) = Y (J) then
7441 -- if J = Y'last then
7442 -- exit;
7443 -- else
7444 -- J := index'succ (J);
7445 -- end if;
7447 -- else
7448 -- return X (I) > Y (J);
7449 -- end if;
7450 -- end loop;
7452 -- return X'length > Y'length;
7453 -- end if;
7454 -- end Gnnn;
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
7461 (Typ : Entity_Id;
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;
7475 If_Stat : Node_Id;
7476 Inner_If : Node_Id;
7477 Final_Expr : Node_Id;
7478 Func_Body : Node_Id;
7479 Func_Name : Entity_Id;
7480 Formals : List_Id;
7481 Length1 : Node_Id;
7482 Length2 : Node_Id;
7484 begin
7485 -- if J = Y'last then
7486 -- exit;
7487 -- else
7488 -- J := index'succ (J);
7489 -- end if;
7491 Inner_If :=
7492 Make_Implicit_If_Statement (Nod,
7493 Condition =>
7494 Make_Op_Eq (Loc,
7495 Left_Opnd => New_Reference_To (J, Loc),
7496 Right_Opnd =>
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)),
7504 Else_Statements =>
7505 New_List (
7506 Make_Assignment_Statement (Loc,
7507 Name => New_Reference_To (J, Loc),
7508 Expression =>
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
7515 -- if ... end if;
7516 -- else
7517 -- return X (I) > Y (J);
7518 -- end if;
7520 Loop_Body :=
7521 Make_Implicit_If_Statement (Nod,
7522 Condition =>
7523 Make_Op_Eq (Loc,
7524 Left_Opnd =>
7525 Make_Indexed_Component (Loc,
7526 Prefix => New_Reference_To (X, Loc),
7527 Expressions => New_List (New_Reference_To (I, Loc))),
7529 Right_Opnd =>
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,
7538 Expression =>
7539 Make_Op_Gt (Loc,
7540 Left_Opnd =>
7541 Make_Indexed_Component (Loc,
7542 Prefix => New_Reference_To (X, Loc),
7543 Expressions => New_List (New_Reference_To (I, Loc))),
7545 Right_Opnd =>
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
7552 -- if ... end if;
7553 -- end loop;
7555 Loop_Statement :=
7556 Make_Implicit_Loop_Statement (Nod,
7557 Identifier => Empty,
7559 Iteration_Scheme =>
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
7572 -- return false;
7573 -- elsif Y'length = 0 then
7574 -- return true;
7575 -- else
7576 -- for ... loop ... end loop;
7577 -- return X'length > Y'length;
7578 -- end if;
7580 Length1 :=
7581 Make_Attribute_Reference (Loc,
7582 Prefix => New_Reference_To (X, Loc),
7583 Attribute_Name => Name_Length);
7585 Length2 :=
7586 Make_Attribute_Reference (Loc,
7587 Prefix => New_Reference_To (Y, Loc),
7588 Attribute_Name => Name_Length);
7590 Final_Expr :=
7591 Make_Op_Gt (Loc,
7592 Left_Opnd => Length1,
7593 Right_Opnd => Length2);
7595 If_Stat :=
7596 Make_Implicit_If_Statement (Nod,
7597 Condition =>
7598 Make_Op_Eq (Loc,
7599 Left_Opnd =>
7600 Make_Attribute_Reference (Loc,
7601 Prefix => New_Reference_To (X, Loc),
7602 Attribute_Name => Name_Length),
7603 Right_Opnd =>
7604 Make_Integer_Literal (Loc, 0)),
7606 Then_Statements =>
7607 New_List (
7608 Make_Return_Statement (Loc,
7609 Expression => New_Reference_To (Standard_False, Loc))),
7611 Elsif_Parts => New_List (
7612 Make_Elsif_Part (Loc,
7613 Condition =>
7614 Make_Op_Eq (Loc,
7615 Left_Opnd =>
7616 Make_Attribute_Reference (Loc,
7617 Prefix => New_Reference_To (Y, Loc),
7618 Attribute_Name => Name_Length),
7619 Right_Opnd =>
7620 Make_Integer_Literal (Loc, 0)),
7622 Then_Statements =>
7623 New_List (
7624 Make_Return_Statement (Loc,
7625 Expression => New_Reference_To (Standard_True, Loc))))),
7627 Else_Statements => New_List (
7628 Loop_Statement,
7629 Make_Return_Statement (Loc,
7630 Expression => Final_Expr)));
7632 -- (X : a; Y: a)
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;
7645 -- begin
7646 -- if ... end if;
7647 -- end Gnnn;
7649 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('G'));
7651 Func_Body :=
7652 Make_Subprogram_Body (Loc,
7653 Specification =>
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),
7663 Expression =>
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)));
7672 return Func_Body;
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
7684 -- C : typ;
7685 -- begin
7686 -- for J in A'range loop
7687 -- C (J) := A (J) op B (J);
7688 -- end loop;
7689 -- return C;
7690 -- end Annn;
7692 -- Here typ is the boolean array type
7694 function Make_Boolean_Array_Op
7695 (Typ : Entity_Id;
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);
7705 A_J : Node_Id;
7706 B_J : Node_Id;
7707 C_J : Node_Id;
7708 Op : Node_Id;
7710 Formals : List_Id;
7711 Func_Name : Entity_Id;
7712 Func_Body : Node_Id;
7713 Loop_Statement : Node_Id;
7715 begin
7716 A_J :=
7717 Make_Indexed_Component (Loc,
7718 Prefix => New_Reference_To (A, Loc),
7719 Expressions => New_List (New_Reference_To (J, Loc)));
7721 B_J :=
7722 Make_Indexed_Component (Loc,
7723 Prefix => New_Reference_To (B, Loc),
7724 Expressions => New_List (New_Reference_To (J, Loc)));
7726 C_J :=
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
7732 Op :=
7733 Make_Op_And (Loc,
7734 Left_Opnd => A_J,
7735 Right_Opnd => B_J);
7737 elsif Nkind (N) = N_Op_Or then
7738 Op :=
7739 Make_Op_Or (Loc,
7740 Left_Opnd => A_J,
7741 Right_Opnd => B_J);
7743 else
7744 Op :=
7745 Make_Op_Xor (Loc,
7746 Left_Opnd => A_J,
7747 Right_Opnd => B_J);
7748 end if;
7750 Loop_Statement :=
7751 Make_Implicit_Loop_Statement (N,
7752 Identifier => Empty,
7754 Iteration_Scheme =>
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,
7766 Name => C_J,
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)));
7778 Func_Name :=
7779 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
7780 Set_Is_Inlined (Func_Name);
7782 Func_Body :=
7783 Make_Subprogram_Body (Loc,
7784 Specification =>
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 (
7798 Loop_Statement,
7799 Make_Return_Statement (Loc,
7800 Expression => New_Reference_To (C, Loc)))));
7802 return Func_Body;
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;
7820 begin
7821 case N_Op_Compare (Nkind (N)) is
7822 when N_Op_Eq =>
7823 True_Result := Res = EQ;
7824 False_Result := Res = LT or else Res = GT or else Res = NE;
7826 when N_Op_Ge =>
7827 True_Result := Res in Compare_GE;
7828 False_Result := Res = LT;
7830 when N_Op_Gt =>
7831 True_Result := Res = GT;
7832 False_Result := Res in Compare_LE;
7834 when N_Op_Lt =>
7835 True_Result := Res = LT;
7836 False_Result := Res in Compare_GE;
7838 when N_Op_Le =>
7839 True_Result := Res in Compare_LE;
7840 False_Result := Res = GT;
7842 when N_Op_Ne =>
7843 True_Result := Res = NE;
7844 False_Result := Res = LT or else Res = GT or else Res = EQ;
7845 end case;
7847 if True_Result then
7848 Rewrite (N,
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
7854 Rewrite (N,
7855 Convert_To (Typ, New_Occurrence_Of (Standard_False, Sloc (N))));
7856 Analyze_And_Resolve (N, Typ);
7857 Warn_On_Known_Condition (N);
7858 end if;
7859 end Rewrite_Comparison;
7861 ----------------------------
7862 -- Safe_In_Place_Array_Op --
7863 ----------------------------
7865 function Safe_In_Place_Array_Op
7866 (Lhs : Node_Id;
7867 Op1 : Node_Id;
7868 Op2 : Node_Id) return Boolean
7870 Target : Entity_Id;
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
7880 ------------------
7881 -- Is_Unaliased --
7882 ------------------
7884 function Is_Unaliased (N : Node_Id) return Boolean is
7885 begin
7886 return
7887 Is_Entity_Name (N)
7888 and then No (Address_Clause (Entity (N)))
7889 and then No (Renamed_Object (Entity (N)));
7890 end Is_Unaliased;
7892 ---------------------
7893 -- Is_Safe_Operand --
7894 ---------------------
7896 function Is_Safe_Operand (Op : Node_Id) return Boolean is
7897 begin
7898 if No (Op) then
7899 return True;
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
7906 then
7907 return Is_Unaliased (Prefix (Op));
7909 elsif Nkind (Op) = N_Slice then
7910 return
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));
7917 else
7918 return False;
7919 end if;
7920 end Is_Safe_Operand;
7922 -- Start of processing for Is_Safe_In_Place_Array_Op
7924 begin
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
7930 return False;
7932 -- Cannot do in place stuff on Java_VM since cannot pass addresses
7934 elsif Java_VM then
7935 return False;
7937 -- Cannot do in place stuff if non-standard Boolean representation
7939 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
7940 return False;
7942 elsif not Is_Unaliased (Lhs) then
7943 return False;
7944 else
7945 Target := Entity (Lhs);
7947 return
7948 Is_Safe_Operand (Op1)
7949 and then Is_Safe_Operand (Op2);
7950 end if;
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;
7974 Obj_Tag : Node_Id;
7976 begin
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);
7982 end if;
7984 Obj_Tag :=
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
7990 return
7991 Make_DT_Access_Action (Left_Type,
7992 Action => CW_Membership,
7993 Args => New_List (
7994 Obj_Tag,
7995 New_Reference_To (
7996 Access_Disp_Table (Root_Type (Right_Type)), Loc)));
7997 else
7998 return
7999 Make_Op_Eq (Loc,
8000 Left_Opnd => Obj_Tag,
8001 Right_Opnd =>
8002 New_Reference_To (Access_Disp_Table (Right_Type), Loc));
8003 end if;
8005 end Tagged_Membership;
8007 ------------------------------
8008 -- Unary_Op_Validity_Checks --
8009 ------------------------------
8011 procedure Unary_Op_Validity_Checks (N : Node_Id) is
8012 begin
8013 if Validity_Checks_On and Validity_Check_Operands then
8014 Ensure_Valid (Right_Opnd (N));
8015 end if;
8016 end Unary_Op_Validity_Checks;
8018 end Exp_Ch4;