]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/exp_ch4.adb
exp_ch4.adb (Expand_N_Attribute_Reference, [...]): Take into account VM_Target
[thirdparty/gcc.git] / gcc / ada / exp_ch4.adb
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-2007, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Aggr; use Exp_Aggr;
32 with Exp_Atag; use Exp_Atag;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Fixd; use Exp_Fixd;
39 with Exp_Pakd; use Exp_Pakd;
40 with Exp_Tss; use Exp_Tss;
41 with Exp_Util; use Exp_Util;
42 with Exp_VFpt; use Exp_VFpt;
43 with Freeze; use Freeze;
44 with Inline; use Inline;
45 with Namet; use Namet;
46 with Nlists; use Nlists;
47 with Nmake; use Nmake;
48 with Opt; use Opt;
49 with Restrict; use Restrict;
50 with Rident; use Rident;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Type; use Sem_Type;
60 with Sem_Util; use Sem_Util;
61 with Sem_Warn; use Sem_Warn;
62 with Sinfo; use Sinfo;
63 with Snames; use Snames;
64 with Stand; use Stand;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Ttypes; use Ttypes;
68 with Uintp; use Uintp;
69 with Urealp; use Urealp;
70 with Validsw; use Validsw;
71
72 package body Exp_Ch4 is
73
74 -----------------------
75 -- Local Subprograms --
76 -----------------------
77
78 procedure Binary_Op_Validity_Checks (N : Node_Id);
79 pragma Inline (Binary_Op_Validity_Checks);
80 -- Performs validity checks for a binary operator
81
82 procedure Build_Boolean_Array_Proc_Call
83 (N : Node_Id;
84 Op1 : Node_Id;
85 Op2 : Node_Id);
86 -- If a boolean array assignment can be done in place, build call to
87 -- corresponding library procedure.
88
89 procedure Displace_Allocator_Pointer (N : Node_Id);
90 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
91 -- Expand_Allocator_Expression. Allocating class-wide interface objects
92 -- this routine displaces the pointer to the allocated object to reference
93 -- the component referencing the corresponding secondary dispatch table.
94
95 procedure Expand_Allocator_Expression (N : Node_Id);
96 -- Subsidiary to Expand_N_Allocator, for the case when the expression
97 -- is a qualified expression or an aggregate.
98
99 procedure Expand_Array_Comparison (N : Node_Id);
100 -- This routine handles expansion of the comparison operators (N_Op_Lt,
101 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
102 -- code for these operators is similar, differing only in the details of
103 -- the actual comparison call that is made. Special processing (call a
104 -- run-time routine)
105
106 function Expand_Array_Equality
107 (Nod : Node_Id;
108 Lhs : Node_Id;
109 Rhs : Node_Id;
110 Bodies : List_Id;
111 Typ : Entity_Id) return Node_Id;
112 -- Expand an array equality into a call to a function implementing this
113 -- equality, and a call to it. Loc is the location for the generated
114 -- nodes. Lhs and Rhs are the array expressions to be compared.
115 -- Bodies is a list on which to attach bodies of local functions that
116 -- are created in the process. It is the responsibility of the
117 -- caller to insert those bodies at the right place. Nod provides
118 -- the Sloc value for the generated code. Normally the types used
119 -- for the generated equality routine are taken from Lhs and Rhs.
120 -- However, in some situations of generated code, the Etype fields
121 -- of Lhs and Rhs are not set yet. In such cases, Typ supplies the
122 -- type to be used for the formal parameters.
123
124 procedure Expand_Boolean_Operator (N : Node_Id);
125 -- Common expansion processing for Boolean operators (And, Or, Xor)
126 -- for the case of array type arguments.
127
128 function Expand_Composite_Equality
129 (Nod : Node_Id;
130 Typ : Entity_Id;
131 Lhs : Node_Id;
132 Rhs : Node_Id;
133 Bodies : List_Id) return Node_Id;
134 -- Local recursive function used to expand equality for nested
135 -- composite types. Used by Expand_Record/Array_Equality, Bodies
136 -- is a list on which to attach bodies of local functions that are
137 -- created in the process. This is the responsability of the caller
138 -- to insert those bodies at the right place. Nod provides the Sloc
139 -- value for generated code. Lhs and Rhs are the left and right sides
140 -- for the comparison, and Typ is the type of the arrays to compare.
141
142 procedure Expand_Concatenate_Other (Cnode : Node_Id; Opnds : List_Id);
143 -- This routine handles expansion of concatenation operations, where
144 -- N is the N_Op_Concat node being expanded and Operands is the list
145 -- of operands (at least two are present). The caller has dealt with
146 -- converting any singleton operands into singleton aggregates.
147
148 procedure Expand_Concatenate_String (Cnode : Node_Id; Opnds : List_Id);
149 -- Routine to expand concatenation of 2-5 operands (in the list Operands)
150 -- and replace node Cnode with the result of the contatenation. If there
151 -- are two operands, they can be string or character. If there are more
152 -- than two operands, then are always of type string (i.e. the caller has
153 -- already converted character operands to strings in this case).
154
155 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
156 -- N is either an N_Op_Divide or N_Op_Multiply node whose result is
157 -- universal fixed. We do not have such a type at runtime, so the
158 -- purpose of this routine is to find the real type by looking up
159 -- the tree. We also determine if the operation must be rounded.
160
161 function Get_Allocator_Final_List
162 (N : Node_Id;
163 T : Entity_Id;
164 PtrT : Entity_Id) return Entity_Id;
165 -- If the designated type is controlled, build final_list expression
166 -- for created object. If context is an access parameter, create a
167 -- local access type to have a usable finalization list.
168
169 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
170 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
171 -- discriminants if it has a constrained nominal type, unless the object
172 -- is a component of an enclosing Unchecked_Union object that is subject
173 -- to a per-object constraint and the enclosing object lacks inferable
174 -- discriminants.
175 --
176 -- An expression of an Unchecked_Union type has inferable discriminants
177 -- if it is either a name of an object with inferable discriminants or a
178 -- qualified expression whose subtype mark denotes a constrained subtype.
179
180 procedure Insert_Dereference_Action (N : Node_Id);
181 -- N is an expression whose type is an access. When the type of the
182 -- associated storage pool is derived from Checked_Pool, generate a
183 -- call to the 'Dereference' primitive operation.
184
185 function Make_Array_Comparison_Op
186 (Typ : Entity_Id;
187 Nod : Node_Id) return Node_Id;
188 -- Comparisons between arrays are expanded in line. This function
189 -- produces the body of the implementation of (a > b), where a and b
190 -- are one-dimensional arrays of some discrete type. The original
191 -- node is then expanded into the appropriate call to this function.
192 -- Nod provides the Sloc value for the generated code.
193
194 function Make_Boolean_Array_Op
195 (Typ : Entity_Id;
196 N : Node_Id) return Node_Id;
197 -- Boolean operations on boolean arrays are expanded in line. This
198 -- function produce the body for the node N, which is (a and b),
199 -- (a or b), or (a xor b). It is used only the normal case and not
200 -- the packed case. The type involved, Typ, is the Boolean array type,
201 -- and the logical operations in the body are simple boolean operations.
202 -- Note that Typ is always a constrained type (the caller has ensured
203 -- this by using Convert_To_Actual_Subtype if necessary).
204
205 procedure Rewrite_Comparison (N : Node_Id);
206 -- If N is the node for a comparison whose outcome can be determined at
207 -- compile time, then the node N can be rewritten with True or False. If
208 -- the outcome cannot be determined at compile time, the call has no
209 -- effect. If N is a type conversion, then this processing is applied to
210 -- its expression. If N is neither comparison nor a type conversion, the
211 -- call has no effect.
212
213 function Tagged_Membership (N : Node_Id) return Node_Id;
214 -- Construct the expression corresponding to the tagged membership test.
215 -- Deals with a second operand being (or not) a class-wide type.
216
217 function Safe_In_Place_Array_Op
218 (Lhs : Node_Id;
219 Op1 : Node_Id;
220 Op2 : Node_Id) return Boolean;
221 -- In the context of an assignment, where the right-hand side is a
222 -- boolean operation on arrays, check whether operation can be performed
223 -- in place.
224
225 procedure Unary_Op_Validity_Checks (N : Node_Id);
226 pragma Inline (Unary_Op_Validity_Checks);
227 -- Performs validity checks for a unary operator
228
229 -------------------------------
230 -- Binary_Op_Validity_Checks --
231 -------------------------------
232
233 procedure Binary_Op_Validity_Checks (N : Node_Id) is
234 begin
235 if Validity_Checks_On and Validity_Check_Operands then
236 Ensure_Valid (Left_Opnd (N));
237 Ensure_Valid (Right_Opnd (N));
238 end if;
239 end Binary_Op_Validity_Checks;
240
241 ------------------------------------
242 -- Build_Boolean_Array_Proc_Call --
243 ------------------------------------
244
245 procedure Build_Boolean_Array_Proc_Call
246 (N : Node_Id;
247 Op1 : Node_Id;
248 Op2 : Node_Id)
249 is
250 Loc : constant Source_Ptr := Sloc (N);
251 Kind : constant Node_Kind := Nkind (Expression (N));
252 Target : constant Node_Id :=
253 Make_Attribute_Reference (Loc,
254 Prefix => Name (N),
255 Attribute_Name => Name_Address);
256
257 Arg1 : constant Node_Id := Op1;
258 Arg2 : Node_Id := Op2;
259 Call_Node : Node_Id;
260 Proc_Name : Entity_Id;
261
262 begin
263 if Kind = N_Op_Not then
264 if Nkind (Op1) in N_Binary_Op then
265
266 -- Use negated version of the binary operators
267
268 if Nkind (Op1) = N_Op_And then
269 Proc_Name := RTE (RE_Vector_Nand);
270
271 elsif Nkind (Op1) = N_Op_Or then
272 Proc_Name := RTE (RE_Vector_Nor);
273
274 else pragma Assert (Nkind (Op1) = N_Op_Xor);
275 Proc_Name := RTE (RE_Vector_Xor);
276 end if;
277
278 Call_Node :=
279 Make_Procedure_Call_Statement (Loc,
280 Name => New_Occurrence_Of (Proc_Name, Loc),
281
282 Parameter_Associations => New_List (
283 Target,
284 Make_Attribute_Reference (Loc,
285 Prefix => Left_Opnd (Op1),
286 Attribute_Name => Name_Address),
287
288 Make_Attribute_Reference (Loc,
289 Prefix => Right_Opnd (Op1),
290 Attribute_Name => Name_Address),
291
292 Make_Attribute_Reference (Loc,
293 Prefix => Left_Opnd (Op1),
294 Attribute_Name => Name_Length)));
295
296 else
297 Proc_Name := RTE (RE_Vector_Not);
298
299 Call_Node :=
300 Make_Procedure_Call_Statement (Loc,
301 Name => New_Occurrence_Of (Proc_Name, Loc),
302 Parameter_Associations => New_List (
303 Target,
304
305 Make_Attribute_Reference (Loc,
306 Prefix => Op1,
307 Attribute_Name => Name_Address),
308
309 Make_Attribute_Reference (Loc,
310 Prefix => Op1,
311 Attribute_Name => Name_Length)));
312 end if;
313
314 else
315 -- We use the following equivalences:
316
317 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
318 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
319 -- (not X) xor (not Y) = X xor Y
320 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
321
322 if Nkind (Op1) = N_Op_Not then
323 if Kind = N_Op_And then
324 Proc_Name := RTE (RE_Vector_Nor);
325
326 elsif Kind = N_Op_Or then
327 Proc_Name := RTE (RE_Vector_Nand);
328
329 else
330 Proc_Name := RTE (RE_Vector_Xor);
331 end if;
332
333 else
334 if Kind = N_Op_And then
335 Proc_Name := RTE (RE_Vector_And);
336
337 elsif Kind = N_Op_Or then
338 Proc_Name := RTE (RE_Vector_Or);
339
340 elsif Nkind (Op2) = N_Op_Not then
341 Proc_Name := RTE (RE_Vector_Nxor);
342 Arg2 := Right_Opnd (Op2);
343
344 else
345 Proc_Name := RTE (RE_Vector_Xor);
346 end if;
347 end if;
348
349 Call_Node :=
350 Make_Procedure_Call_Statement (Loc,
351 Name => New_Occurrence_Of (Proc_Name, Loc),
352 Parameter_Associations => New_List (
353 Target,
354 Make_Attribute_Reference (Loc,
355 Prefix => Arg1,
356 Attribute_Name => Name_Address),
357 Make_Attribute_Reference (Loc,
358 Prefix => Arg2,
359 Attribute_Name => Name_Address),
360 Make_Attribute_Reference (Loc,
361 Prefix => Op1,
362 Attribute_Name => Name_Length)));
363 end if;
364
365 Rewrite (N, Call_Node);
366 Analyze (N);
367
368 exception
369 when RE_Not_Available =>
370 return;
371 end Build_Boolean_Array_Proc_Call;
372
373 --------------------------------
374 -- Displace_Allocator_Pointer --
375 --------------------------------
376
377 procedure Displace_Allocator_Pointer (N : Node_Id) is
378 Loc : constant Source_Ptr := Sloc (N);
379 Orig_Node : constant Node_Id := Original_Node (N);
380 Dtyp : Entity_Id;
381 Etyp : Entity_Id;
382 PtrT : Entity_Id;
383
384 begin
385 -- Do nothing in case of VM targets: the virtual machine will handle
386 -- interfaces directly.
387
388 if VM_Target /= No_VM then
389 return;
390 end if;
391
392 pragma Assert (Nkind (N) = N_Identifier
393 and then Nkind (Orig_Node) = N_Allocator);
394
395 PtrT := Etype (Orig_Node);
396 Dtyp := Designated_Type (PtrT);
397 Etyp := Etype (Expression (Orig_Node));
398
399 if Is_Class_Wide_Type (Dtyp)
400 and then Is_Interface (Dtyp)
401 then
402 -- If the type of the allocator expression is not an interface type
403 -- we can generate code to reference the record component containing
404 -- the pointer to the secondary dispatch table.
405
406 if not Is_Interface (Etyp) then
407 declare
408 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
409
410 begin
411 -- 1) Get access to the allocated object
412
413 Rewrite (N,
414 Make_Explicit_Dereference (Loc,
415 Relocate_Node (N)));
416 Set_Etype (N, Etyp);
417 Set_Analyzed (N);
418
419 -- 2) Add the conversion to displace the pointer to reference
420 -- the secondary dispatch table.
421
422 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
423 Analyze_And_Resolve (N, Dtyp);
424
425 -- 3) The 'access to the secondary dispatch table will be used
426 -- as the value returned by the allocator.
427
428 Rewrite (N,
429 Make_Attribute_Reference (Loc,
430 Prefix => Relocate_Node (N),
431 Attribute_Name => Name_Access));
432 Set_Etype (N, Saved_Typ);
433 Set_Analyzed (N);
434 end;
435
436 -- If the type of the allocator expression is an interface type we
437 -- generate a run-time call to displace "this" to reference the
438 -- component containing the pointer to the secondary dispatch table
439 -- or else raise Constraint_Error if the actual object does not
440 -- implement the target interface. This case corresponds with the
441 -- following example:
442
443 -- function Op (Obj : Iface_1'Class) return access Ifac_2e'Class is
444 -- begin
445 -- return new Iface_2'Class'(Obj);
446 -- end Op;
447
448 else
449 Rewrite (N,
450 Unchecked_Convert_To (PtrT,
451 Make_Function_Call (Loc,
452 Name => New_Reference_To (RTE (RE_Displace), Loc),
453 Parameter_Associations => New_List (
454 Unchecked_Convert_To (RTE (RE_Address),
455 Relocate_Node (N)),
456
457 New_Occurrence_Of
458 (Elists.Node
459 (First_Elmt
460 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
461 Loc)))));
462 Analyze_And_Resolve (N, PtrT);
463 end if;
464 end if;
465 end Displace_Allocator_Pointer;
466
467 ---------------------------------
468 -- Expand_Allocator_Expression --
469 ---------------------------------
470
471 procedure Expand_Allocator_Expression (N : Node_Id) is
472 Loc : constant Source_Ptr := Sloc (N);
473 Exp : constant Node_Id := Expression (Expression (N));
474 PtrT : constant Entity_Id := Etype (N);
475 DesigT : constant Entity_Id := Designated_Type (PtrT);
476
477 procedure Apply_Accessibility_Check
478 (Ref : Node_Id;
479 Built_In_Place : Boolean := False);
480 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
481 -- type, generate an accessibility check to verify that the level of
482 -- the type of the created object is not deeper than the level of the
483 -- access type. If the type of the qualified expression is class-
484 -- wide, then always generate the check (except in the case where it
485 -- is known to be unnecessary, see comment below). Otherwise, only
486 -- generate the check if the level of the qualified expression type
487 -- is statically deeper than the access type. Although the static
488 -- accessibility will generally have been performed as a legality
489 -- check, it won't have been done in cases where the allocator
490 -- appears in generic body, so a run-time check is needed in general.
491 -- One special case is when the access type is declared in the same
492 -- scope as the class-wide allocator, in which case the check can
493 -- never fail, so it need not be generated. As an open issue, there
494 -- seem to be cases where the static level associated with the
495 -- class-wide object's underlying type is not sufficient to perform
496 -- the proper accessibility check, such as for allocators in nested
497 -- subprograms or accept statements initialized by class-wide formals
498 -- when the actual originates outside at a deeper static level. The
499 -- nested subprogram case might require passing accessibility levels
500 -- along with class-wide parameters, and the task case seems to be
501 -- an actual gap in the language rules that needs to be fixed by the
502 -- ARG. ???
503
504 -------------------------------
505 -- Apply_Accessibility_Check --
506 -------------------------------
507
508 procedure Apply_Accessibility_Check
509 (Ref : Node_Id;
510 Built_In_Place : Boolean := False)
511 is
512 Ref_Node : Node_Id;
513
514 begin
515 -- Note: we skip the accessibility check for the VM case, since
516 -- there does not seem to be any practical way of implementing it.
517
518 if Ada_Version >= Ada_05
519 and then VM_Target = No_VM
520 and then Is_Class_Wide_Type (DesigT)
521 and then not Scope_Suppress (Accessibility_Check)
522 and then
523 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
524 or else
525 (Is_Class_Wide_Type (Etype (Exp))
526 and then Scope (PtrT) /= Current_Scope))
527 then
528 -- If the allocator was built in place Ref is already a reference
529 -- to the access object initialized to the result of the allocator
530 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). Otherwise
531 -- it is the entity associated with the object containing the
532 -- address of the allocated object.
533
534 if Built_In_Place then
535 Ref_Node := New_Copy (Ref);
536 else
537 Ref_Node := New_Reference_To (Ref, Loc);
538 end if;
539
540 Insert_Action (N,
541 Make_Raise_Program_Error (Loc,
542 Condition =>
543 Make_Op_Gt (Loc,
544 Left_Opnd =>
545 Build_Get_Access_Level (Loc,
546 Make_Attribute_Reference (Loc,
547 Prefix => Ref_Node,
548 Attribute_Name => Name_Tag)),
549 Right_Opnd =>
550 Make_Integer_Literal (Loc,
551 Type_Access_Level (PtrT))),
552 Reason => PE_Accessibility_Check_Failed));
553 end if;
554 end Apply_Accessibility_Check;
555
556 -- Local variables
557
558 Indic : constant Node_Id := Subtype_Mark (Expression (N));
559 T : constant Entity_Id := Entity (Indic);
560 Flist : Node_Id;
561 Node : Node_Id;
562 Temp : Entity_Id;
563
564 TagT : Entity_Id := Empty;
565 -- Type used as source for tag assignment
566
567 TagR : Node_Id := Empty;
568 -- Target reference for tag assignment
569
570 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
571
572 Tag_Assign : Node_Id;
573 Tmp_Node : Node_Id;
574
575 -- Start of processing for Expand_Allocator_Expression
576
577 begin
578 if Is_Tagged_Type (T) or else Controlled_Type (T) then
579
580 -- Ada 2005 (AI-318-02): If the initialization expression is a
581 -- call to a build-in-place function, then access to the allocated
582 -- object must be passed to the function. Currently we limit such
583 -- functions to those with constrained limited result subtypes,
584 -- but eventually we plan to expand the allowed forms of funtions
585 -- that are treated as build-in-place.
586
587 if Ada_Version >= Ada_05
588 and then Is_Build_In_Place_Function_Call (Exp)
589 then
590 Make_Build_In_Place_Call_In_Allocator (N, Exp);
591 Apply_Accessibility_Check (N, Built_In_Place => True);
592 return;
593 end if;
594
595 -- Actions inserted before:
596 -- Temp : constant ptr_T := new T'(Expression);
597 -- <no CW> Temp._tag := T'tag;
598 -- <CTRL> Adjust (Finalizable (Temp.all));
599 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
600
601 -- We analyze by hand the new internal allocator to avoid
602 -- any recursion and inappropriate call to Initialize
603
604 -- We don't want to remove side effects when the expression must be
605 -- built in place. In the case of a build-in-place function call,
606 -- that could lead to a duplication of the call, which was already
607 -- substituted for the allocator.
608
609 if not Aggr_In_Place then
610 Remove_Side_Effects (Exp);
611 end if;
612
613 Temp :=
614 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
615
616 -- For a class wide allocation generate the following code:
617
618 -- type Equiv_Record is record ... end record;
619 -- implicit subtype CW is <Class_Wide_Subytpe>;
620 -- temp : PtrT := new CW'(CW!(expr));
621
622 if Is_Class_Wide_Type (T) then
623 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
624
625 -- Ada 2005 (AI-251): If the expression is a class-wide interface
626 -- object we generate code to move up "this" to reference the
627 -- base of the object before allocating the new object.
628
629 -- Note that Exp'Address is recursively expanded into a call
630 -- to Base_Address (Exp.Tag)
631
632 if Is_Class_Wide_Type (Etype (Exp))
633 and then Is_Interface (Etype (Exp))
634 and then VM_Target = No_VM
635 then
636 Set_Expression
637 (Expression (N),
638 Unchecked_Convert_To (Entity (Indic),
639 Make_Explicit_Dereference (Loc,
640 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
641 Make_Attribute_Reference (Loc,
642 Prefix => Exp,
643 Attribute_Name => Name_Address)))));
644
645 else
646 Set_Expression
647 (Expression (N),
648 Unchecked_Convert_To (Entity (Indic), Exp));
649 end if;
650
651 Analyze_And_Resolve (Expression (N), Entity (Indic));
652 end if;
653
654 -- Keep separate the management of allocators returning interfaces
655
656 if not Is_Interface (Directly_Designated_Type (PtrT)) then
657 if Aggr_In_Place then
658 Tmp_Node :=
659 Make_Object_Declaration (Loc,
660 Defining_Identifier => Temp,
661 Object_Definition => New_Reference_To (PtrT, Loc),
662 Expression =>
663 Make_Allocator (Loc,
664 New_Reference_To (Etype (Exp), Loc)));
665
666 Set_Comes_From_Source
667 (Expression (Tmp_Node), Comes_From_Source (N));
668
669 Set_No_Initialization (Expression (Tmp_Node));
670 Insert_Action (N, Tmp_Node);
671
672 if Controlled_Type (T)
673 and then Ekind (PtrT) = E_Anonymous_Access_Type
674 then
675 -- Create local finalization list for access parameter
676
677 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
678 end if;
679
680 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
681 else
682 Node := Relocate_Node (N);
683 Set_Analyzed (Node);
684 Insert_Action (N,
685 Make_Object_Declaration (Loc,
686 Defining_Identifier => Temp,
687 Constant_Present => True,
688 Object_Definition => New_Reference_To (PtrT, Loc),
689 Expression => Node));
690 end if;
691
692 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
693 -- interface type. In this case we use the type of the qualified
694 -- expression to allocate the object.
695
696 else
697 declare
698 Def_Id : constant Entity_Id :=
699 Make_Defining_Identifier (Loc,
700 New_Internal_Name ('T'));
701 New_Decl : Node_Id;
702
703 begin
704 New_Decl :=
705 Make_Full_Type_Declaration (Loc,
706 Defining_Identifier => Def_Id,
707 Type_Definition =>
708 Make_Access_To_Object_Definition (Loc,
709 All_Present => True,
710 Null_Exclusion_Present => False,
711 Constant_Present => False,
712 Subtype_Indication =>
713 New_Reference_To (Etype (Exp), Loc)));
714
715 Insert_Action (N, New_Decl);
716
717 -- Inherit the final chain to ensure that the expansion of the
718 -- aggregate is correct in case of controlled types
719
720 if Controlled_Type (Directly_Designated_Type (PtrT)) then
721 Set_Associated_Final_Chain (Def_Id,
722 Associated_Final_Chain (PtrT));
723 end if;
724
725 -- Declare the object using the previous type declaration
726
727 if Aggr_In_Place then
728 Tmp_Node :=
729 Make_Object_Declaration (Loc,
730 Defining_Identifier => Temp,
731 Object_Definition => New_Reference_To (Def_Id, Loc),
732 Expression =>
733 Make_Allocator (Loc,
734 New_Reference_To (Etype (Exp), Loc)));
735
736 Set_Comes_From_Source
737 (Expression (Tmp_Node), Comes_From_Source (N));
738
739 Set_No_Initialization (Expression (Tmp_Node));
740 Insert_Action (N, Tmp_Node);
741
742 if Controlled_Type (T)
743 and then Ekind (PtrT) = E_Anonymous_Access_Type
744 then
745 -- Create local finalization list for access parameter
746
747 Flist :=
748 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
749 end if;
750
751 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
752 else
753 Node := Relocate_Node (N);
754 Set_Analyzed (Node);
755 Insert_Action (N,
756 Make_Object_Declaration (Loc,
757 Defining_Identifier => Temp,
758 Constant_Present => True,
759 Object_Definition => New_Reference_To (Def_Id, Loc),
760 Expression => Node));
761 end if;
762
763 -- Generate an additional object containing the address of the
764 -- returned object. The type of this second object declaration
765 -- is the correct type required for the common proceessing
766 -- that is still performed by this subprogram. The displacement
767 -- of this pointer to reference the component associated with
768 -- the interface type will be done at the end of the common
769 -- processing.
770
771 New_Decl :=
772 Make_Object_Declaration (Loc,
773 Defining_Identifier => Make_Defining_Identifier (Loc,
774 New_Internal_Name ('P')),
775 Object_Definition => New_Reference_To (PtrT, Loc),
776 Expression => Unchecked_Convert_To (PtrT,
777 New_Reference_To (Temp, Loc)));
778
779 Insert_Action (N, New_Decl);
780
781 Tmp_Node := New_Decl;
782 Temp := Defining_Identifier (New_Decl);
783 end;
784 end if;
785
786 Apply_Accessibility_Check (Temp);
787
788 -- Generate the tag assignment
789
790 -- Suppress the tag assignment when VM_Target because VM tags are
791 -- represented implicitly in objects.
792
793 if VM_Target /= No_VM then
794 null;
795
796 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
797 -- interface objects because in this case the tag does not change.
798
799 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
800 pragma Assert (Is_Class_Wide_Type
801 (Directly_Designated_Type (Etype (N))));
802 null;
803
804 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
805 TagT := T;
806 TagR := New_Reference_To (Temp, Loc);
807
808 elsif Is_Private_Type (T)
809 and then Is_Tagged_Type (Underlying_Type (T))
810 then
811 TagT := Underlying_Type (T);
812 TagR :=
813 Unchecked_Convert_To (Underlying_Type (T),
814 Make_Explicit_Dereference (Loc,
815 Prefix => New_Reference_To (Temp, Loc)));
816 end if;
817
818 if Present (TagT) then
819 Tag_Assign :=
820 Make_Assignment_Statement (Loc,
821 Name =>
822 Make_Selected_Component (Loc,
823 Prefix => TagR,
824 Selector_Name =>
825 New_Reference_To (First_Tag_Component (TagT), Loc)),
826
827 Expression =>
828 Unchecked_Convert_To (RTE (RE_Tag),
829 New_Reference_To
830 (Elists.Node (First_Elmt (Access_Disp_Table (TagT))),
831 Loc)));
832
833 -- The previous assignment has to be done in any case
834
835 Set_Assignment_OK (Name (Tag_Assign));
836 Insert_Action (N, Tag_Assign);
837 end if;
838
839 if Controlled_Type (DesigT)
840 and then Controlled_Type (T)
841 then
842 declare
843 Attach : Node_Id;
844 Apool : constant Entity_Id :=
845 Associated_Storage_Pool (PtrT);
846
847 begin
848 -- If it is an allocation on the secondary stack
849 -- (i.e. a value returned from a function), the object
850 -- is attached on the caller side as soon as the call
851 -- is completed (see Expand_Ctrl_Function_Call)
852
853 if Is_RTE (Apool, RE_SS_Pool) then
854 declare
855 F : constant Entity_Id :=
856 Make_Defining_Identifier (Loc,
857 New_Internal_Name ('F'));
858 begin
859 Insert_Action (N,
860 Make_Object_Declaration (Loc,
861 Defining_Identifier => F,
862 Object_Definition => New_Reference_To (RTE
863 (RE_Finalizable_Ptr), Loc)));
864
865 Flist := New_Reference_To (F, Loc);
866 Attach := Make_Integer_Literal (Loc, 1);
867 end;
868
869 -- Normal case, not a secondary stack allocation
870
871 else
872 if Controlled_Type (T)
873 and then Ekind (PtrT) = E_Anonymous_Access_Type
874 then
875 -- Create local finalization list for access parameter
876
877 Flist :=
878 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
879 else
880 Flist := Find_Final_List (PtrT);
881 end if;
882
883 Attach := Make_Integer_Literal (Loc, 2);
884 end if;
885
886 -- Generate an Adjust call if the object will be moved. In Ada
887 -- 2005, the object may be inherently limited, in which case
888 -- there is no Adjust procedure, and the object is built in
889 -- place. In Ada 95, the object can be limited but not
890 -- inherently limited if this allocator came from a return
891 -- statement (we're allocating the result on the secondary
892 -- stack). In that case, the object will be moved, so we _do_
893 -- want to Adjust.
894
895 if not Aggr_In_Place
896 and then not Is_Inherently_Limited_Type (T)
897 then
898 Insert_Actions (N,
899 Make_Adjust_Call (
900 Ref =>
901
902 -- An unchecked conversion is needed in the
903 -- classwide case because the designated type
904 -- can be an ancestor of the subtype mark of
905 -- the allocator.
906
907 Unchecked_Convert_To (T,
908 Make_Explicit_Dereference (Loc,
909 Prefix => New_Reference_To (Temp, Loc))),
910
911 Typ => T,
912 Flist_Ref => Flist,
913 With_Attach => Attach,
914 Allocator => True));
915 end if;
916 end;
917 end if;
918
919 Rewrite (N, New_Reference_To (Temp, Loc));
920 Analyze_And_Resolve (N, PtrT);
921
922 -- Ada 2005 (AI-251): Displace the pointer to reference the
923 -- record component containing the secondary dispatch table
924 -- of the interface type.
925
926 if Is_Interface (Directly_Designated_Type (PtrT)) then
927 Displace_Allocator_Pointer (N);
928 end if;
929
930 elsif Aggr_In_Place then
931 Temp :=
932 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
933 Tmp_Node :=
934 Make_Object_Declaration (Loc,
935 Defining_Identifier => Temp,
936 Object_Definition => New_Reference_To (PtrT, Loc),
937 Expression => Make_Allocator (Loc,
938 New_Reference_To (Etype (Exp), Loc)));
939
940 Set_Comes_From_Source
941 (Expression (Tmp_Node), Comes_From_Source (N));
942
943 Set_No_Initialization (Expression (Tmp_Node));
944 Insert_Action (N, Tmp_Node);
945 Convert_Aggr_In_Allocator (N, Tmp_Node, Exp);
946 Rewrite (N, New_Reference_To (Temp, Loc));
947 Analyze_And_Resolve (N, PtrT);
948
949 elsif Is_Access_Type (DesigT)
950 and then Nkind (Exp) = N_Allocator
951 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
952 then
953 -- Apply constraint to designated subtype indication
954
955 Apply_Constraint_Check (Expression (Exp),
956 Designated_Type (DesigT),
957 No_Sliding => True);
958
959 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
960
961 -- Propagate constraint_error to enclosing allocator
962
963 Rewrite (Exp, New_Copy (Expression (Exp)));
964 end if;
965 else
966 -- First check against the type of the qualified expression
967 --
968 -- NOTE: The commented call should be correct, but for
969 -- some reason causes the compiler to bomb (sigsegv) on
970 -- ACVC test c34007g, so for now we just perform the old
971 -- (incorrect) test against the designated subtype with
972 -- no sliding in the else part of the if statement below.
973 -- ???
974 --
975 -- Apply_Constraint_Check (Exp, T, No_Sliding => True);
976
977 -- A check is also needed in cases where the designated
978 -- subtype is constrained and differs from the subtype
979 -- given in the qualified expression. Note that the check
980 -- on the qualified expression does not allow sliding,
981 -- but this check does (a relaxation from Ada 83).
982
983 if Is_Constrained (DesigT)
984 and then not Subtypes_Statically_Match
985 (T, DesigT)
986 then
987 Apply_Constraint_Check
988 (Exp, DesigT, No_Sliding => False);
989
990 -- The nonsliding check should really be performed
991 -- (unconditionally) against the subtype of the
992 -- qualified expression, but that causes a problem
993 -- with c34007g (see above), so for now we retain this.
994
995 else
996 Apply_Constraint_Check
997 (Exp, DesigT, No_Sliding => True);
998 end if;
999
1000 -- For an access to unconstrained packed array, GIGI needs
1001 -- to see an expression with a constrained subtype in order
1002 -- to compute the proper size for the allocator.
1003
1004 if Is_Array_Type (T)
1005 and then not Is_Constrained (T)
1006 and then Is_Packed (T)
1007 then
1008 declare
1009 ConstrT : constant Entity_Id :=
1010 Make_Defining_Identifier (Loc,
1011 Chars => New_Internal_Name ('A'));
1012 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1013 begin
1014 Insert_Action (Exp,
1015 Make_Subtype_Declaration (Loc,
1016 Defining_Identifier => ConstrT,
1017 Subtype_Indication =>
1018 Make_Subtype_From_Expr (Exp, T)));
1019 Freeze_Itype (ConstrT, Exp);
1020 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1021 end;
1022 end if;
1023
1024 -- Ada 2005 (AI-318-02): If the initialization expression is a
1025 -- call to a build-in-place function, then access to the allocated
1026 -- object must be passed to the function. Currently we limit such
1027 -- functions to those with constrained limited result subtypes,
1028 -- but eventually we plan to expand the allowed forms of funtions
1029 -- that are treated as build-in-place.
1030
1031 if Ada_Version >= Ada_05
1032 and then Is_Build_In_Place_Function_Call (Exp)
1033 then
1034 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1035 end if;
1036 end if;
1037
1038 exception
1039 when RE_Not_Available =>
1040 return;
1041 end Expand_Allocator_Expression;
1042
1043 -----------------------------
1044 -- Expand_Array_Comparison --
1045 -----------------------------
1046
1047 -- Expansion is only required in the case of array types. For the
1048 -- unpacked case, an appropriate runtime routine is called. For
1049 -- packed cases, and also in some other cases where a runtime
1050 -- routine cannot be called, the form of the expansion is:
1051
1052 -- [body for greater_nn; boolean_expression]
1053
1054 -- The body is built by Make_Array_Comparison_Op, and the form of the
1055 -- Boolean expression depends on the operator involved.
1056
1057 procedure Expand_Array_Comparison (N : Node_Id) is
1058 Loc : constant Source_Ptr := Sloc (N);
1059 Op1 : Node_Id := Left_Opnd (N);
1060 Op2 : Node_Id := Right_Opnd (N);
1061 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1062 Ctyp : constant Entity_Id := Component_Type (Typ1);
1063
1064 Expr : Node_Id;
1065 Func_Body : Node_Id;
1066 Func_Name : Entity_Id;
1067
1068 Comp : RE_Id;
1069
1070 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1071 -- True for byte addressable target
1072
1073 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1074 -- Returns True if the length of the given operand is known to be
1075 -- less than 4. Returns False if this length is known to be four
1076 -- or greater or is not known at compile time.
1077
1078 ------------------------
1079 -- Length_Less_Than_4 --
1080 ------------------------
1081
1082 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1083 Otyp : constant Entity_Id := Etype (Opnd);
1084
1085 begin
1086 if Ekind (Otyp) = E_String_Literal_Subtype then
1087 return String_Literal_Length (Otyp) < 4;
1088
1089 else
1090 declare
1091 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1092 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1093 Hi : constant Node_Id := Type_High_Bound (Ityp);
1094 Lov : Uint;
1095 Hiv : Uint;
1096
1097 begin
1098 if Compile_Time_Known_Value (Lo) then
1099 Lov := Expr_Value (Lo);
1100 else
1101 return False;
1102 end if;
1103
1104 if Compile_Time_Known_Value (Hi) then
1105 Hiv := Expr_Value (Hi);
1106 else
1107 return False;
1108 end if;
1109
1110 return Hiv < Lov + 3;
1111 end;
1112 end if;
1113 end Length_Less_Than_4;
1114
1115 -- Start of processing for Expand_Array_Comparison
1116
1117 begin
1118 -- Deal first with unpacked case, where we can call a runtime routine
1119 -- except that we avoid this for targets for which are not addressable
1120 -- by bytes, and for the JVM/CIL, since they do not support direct
1121 -- addressing of array components.
1122
1123 if not Is_Bit_Packed_Array (Typ1)
1124 and then Byte_Addressable
1125 and then VM_Target = No_VM
1126 then
1127 -- The call we generate is:
1128
1129 -- Compare_Array_xn[_Unaligned]
1130 -- (left'address, right'address, left'length, right'length) <op> 0
1131
1132 -- x = U for unsigned, S for signed
1133 -- n = 8,16,32,64 for component size
1134 -- Add _Unaligned if length < 4 and component size is 8.
1135 -- <op> is the standard comparison operator
1136
1137 if Component_Size (Typ1) = 8 then
1138 if Length_Less_Than_4 (Op1)
1139 or else
1140 Length_Less_Than_4 (Op2)
1141 then
1142 if Is_Unsigned_Type (Ctyp) then
1143 Comp := RE_Compare_Array_U8_Unaligned;
1144 else
1145 Comp := RE_Compare_Array_S8_Unaligned;
1146 end if;
1147
1148 else
1149 if Is_Unsigned_Type (Ctyp) then
1150 Comp := RE_Compare_Array_U8;
1151 else
1152 Comp := RE_Compare_Array_S8;
1153 end if;
1154 end if;
1155
1156 elsif Component_Size (Typ1) = 16 then
1157 if Is_Unsigned_Type (Ctyp) then
1158 Comp := RE_Compare_Array_U16;
1159 else
1160 Comp := RE_Compare_Array_S16;
1161 end if;
1162
1163 elsif Component_Size (Typ1) = 32 then
1164 if Is_Unsigned_Type (Ctyp) then
1165 Comp := RE_Compare_Array_U32;
1166 else
1167 Comp := RE_Compare_Array_S32;
1168 end if;
1169
1170 else pragma Assert (Component_Size (Typ1) = 64);
1171 if Is_Unsigned_Type (Ctyp) then
1172 Comp := RE_Compare_Array_U64;
1173 else
1174 Comp := RE_Compare_Array_S64;
1175 end if;
1176 end if;
1177
1178 Remove_Side_Effects (Op1, Name_Req => True);
1179 Remove_Side_Effects (Op2, Name_Req => True);
1180
1181 Rewrite (Op1,
1182 Make_Function_Call (Sloc (Op1),
1183 Name => New_Occurrence_Of (RTE (Comp), Loc),
1184
1185 Parameter_Associations => New_List (
1186 Make_Attribute_Reference (Loc,
1187 Prefix => Relocate_Node (Op1),
1188 Attribute_Name => Name_Address),
1189
1190 Make_Attribute_Reference (Loc,
1191 Prefix => Relocate_Node (Op2),
1192 Attribute_Name => Name_Address),
1193
1194 Make_Attribute_Reference (Loc,
1195 Prefix => Relocate_Node (Op1),
1196 Attribute_Name => Name_Length),
1197
1198 Make_Attribute_Reference (Loc,
1199 Prefix => Relocate_Node (Op2),
1200 Attribute_Name => Name_Length))));
1201
1202 Rewrite (Op2,
1203 Make_Integer_Literal (Sloc (Op2),
1204 Intval => Uint_0));
1205
1206 Analyze_And_Resolve (Op1, Standard_Integer);
1207 Analyze_And_Resolve (Op2, Standard_Integer);
1208 return;
1209 end if;
1210
1211 -- Cases where we cannot make runtime call
1212
1213 -- For (a <= b) we convert to not (a > b)
1214
1215 if Chars (N) = Name_Op_Le then
1216 Rewrite (N,
1217 Make_Op_Not (Loc,
1218 Right_Opnd =>
1219 Make_Op_Gt (Loc,
1220 Left_Opnd => Op1,
1221 Right_Opnd => Op2)));
1222 Analyze_And_Resolve (N, Standard_Boolean);
1223 return;
1224
1225 -- For < the Boolean expression is
1226 -- greater__nn (op2, op1)
1227
1228 elsif Chars (N) = Name_Op_Lt then
1229 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1230
1231 -- Switch operands
1232
1233 Op1 := Right_Opnd (N);
1234 Op2 := Left_Opnd (N);
1235
1236 -- For (a >= b) we convert to not (a < b)
1237
1238 elsif Chars (N) = Name_Op_Ge then
1239 Rewrite (N,
1240 Make_Op_Not (Loc,
1241 Right_Opnd =>
1242 Make_Op_Lt (Loc,
1243 Left_Opnd => Op1,
1244 Right_Opnd => Op2)));
1245 Analyze_And_Resolve (N, Standard_Boolean);
1246 return;
1247
1248 -- For > the Boolean expression is
1249 -- greater__nn (op1, op2)
1250
1251 else
1252 pragma Assert (Chars (N) = Name_Op_Gt);
1253 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1254 end if;
1255
1256 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1257 Expr :=
1258 Make_Function_Call (Loc,
1259 Name => New_Reference_To (Func_Name, Loc),
1260 Parameter_Associations => New_List (Op1, Op2));
1261
1262 Insert_Action (N, Func_Body);
1263 Rewrite (N, Expr);
1264 Analyze_And_Resolve (N, Standard_Boolean);
1265
1266 exception
1267 when RE_Not_Available =>
1268 return;
1269 end Expand_Array_Comparison;
1270
1271 ---------------------------
1272 -- Expand_Array_Equality --
1273 ---------------------------
1274
1275 -- Expand an equality function for multi-dimensional arrays. Here is
1276 -- an example of such a function for Nb_Dimension = 2
1277
1278 -- function Enn (A : atyp; B : btyp) return boolean is
1279 -- begin
1280 -- if (A'length (1) = 0 or else A'length (2) = 0)
1281 -- and then
1282 -- (B'length (1) = 0 or else B'length (2) = 0)
1283 -- then
1284 -- return True; -- RM 4.5.2(22)
1285 -- end if;
1286
1287 -- if A'length (1) /= B'length (1)
1288 -- or else
1289 -- A'length (2) /= B'length (2)
1290 -- then
1291 -- return False; -- RM 4.5.2(23)
1292 -- end if;
1293
1294 -- declare
1295 -- A1 : Index_T1 := A'first (1);
1296 -- B1 : Index_T1 := B'first (1);
1297 -- begin
1298 -- loop
1299 -- declare
1300 -- A2 : Index_T2 := A'first (2);
1301 -- B2 : Index_T2 := B'first (2);
1302 -- begin
1303 -- loop
1304 -- if A (A1, A2) /= B (B1, B2) then
1305 -- return False;
1306 -- end if;
1307
1308 -- exit when A2 = A'last (2);
1309 -- A2 := Index_T2'succ (A2);
1310 -- B2 := Index_T2'succ (B2);
1311 -- end loop;
1312 -- end;
1313
1314 -- exit when A1 = A'last (1);
1315 -- A1 := Index_T1'succ (A1);
1316 -- B1 := Index_T1'succ (B1);
1317 -- end loop;
1318 -- end;
1319
1320 -- return true;
1321 -- end Enn;
1322
1323 -- Note on the formal types used (atyp and btyp). If either of the
1324 -- arrays is of a private type, we use the underlying type, and
1325 -- do an unchecked conversion of the actual. If either of the arrays
1326 -- has a bound depending on a discriminant, then we use the base type
1327 -- since otherwise we have an escaped discriminant in the function.
1328
1329 -- If both arrays are constrained and have the same bounds, we can
1330 -- generate a loop with an explicit iteration scheme using a 'Range
1331 -- attribute over the first array.
1332
1333 function Expand_Array_Equality
1334 (Nod : Node_Id;
1335 Lhs : Node_Id;
1336 Rhs : Node_Id;
1337 Bodies : List_Id;
1338 Typ : Entity_Id) return Node_Id
1339 is
1340 Loc : constant Source_Ptr := Sloc (Nod);
1341 Decls : constant List_Id := New_List;
1342 Index_List1 : constant List_Id := New_List;
1343 Index_List2 : constant List_Id := New_List;
1344
1345 Actuals : List_Id;
1346 Formals : List_Id;
1347 Func_Name : Entity_Id;
1348 Func_Body : Node_Id;
1349
1350 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1351 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1352
1353 Ltyp : Entity_Id;
1354 Rtyp : Entity_Id;
1355 -- The parameter types to be used for the formals
1356
1357 function Arr_Attr
1358 (Arr : Entity_Id;
1359 Nam : Name_Id;
1360 Num : Int) return Node_Id;
1361 -- This builds the attribute reference Arr'Nam (Expr)
1362
1363 function Component_Equality (Typ : Entity_Id) return Node_Id;
1364 -- Create one statement to compare corresponding components,
1365 -- designated by a full set of indices.
1366
1367 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1368 -- Given one of the arguments, computes the appropriate type to
1369 -- be used for that argument in the corresponding function formal
1370
1371 function Handle_One_Dimension
1372 (N : Int;
1373 Index : Node_Id) return Node_Id;
1374 -- This procedure returns the following code
1375 --
1376 -- declare
1377 -- Bn : Index_T := B'First (N);
1378 -- begin
1379 -- loop
1380 -- xxx
1381 -- exit when An = A'Last (N);
1382 -- An := Index_T'Succ (An)
1383 -- Bn := Index_T'Succ (Bn)
1384 -- end loop;
1385 -- end;
1386 --
1387 -- If both indices are constrained and identical, the procedure
1388 -- returns a simpler loop:
1389 --
1390 -- for An in A'Range (N) loop
1391 -- xxx
1392 -- end loop
1393 --
1394 -- N is the dimension for which we are generating a loop. Index is the
1395 -- N'th index node, whose Etype is Index_Type_n in the above code.
1396 -- The xxx statement is either the loop or declare for the next
1397 -- dimension or if this is the last dimension the comparison
1398 -- of corresponding components of the arrays.
1399 --
1400 -- The actual way the code works is to return the comparison
1401 -- of corresponding components for the N+1 call. That's neater!
1402
1403 function Test_Empty_Arrays return Node_Id;
1404 -- This function constructs the test for both arrays being empty
1405 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1406 -- and then
1407 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1408
1409 function Test_Lengths_Correspond return Node_Id;
1410 -- This function constructs the test for arrays having different
1411 -- lengths in at least one index position, in which case resull
1412
1413 -- A'length (1) /= B'length (1)
1414 -- or else
1415 -- A'length (2) /= B'length (2)
1416 -- or else
1417 -- ...
1418
1419 --------------
1420 -- Arr_Attr --
1421 --------------
1422
1423 function Arr_Attr
1424 (Arr : Entity_Id;
1425 Nam : Name_Id;
1426 Num : Int) return Node_Id
1427 is
1428 begin
1429 return
1430 Make_Attribute_Reference (Loc,
1431 Attribute_Name => Nam,
1432 Prefix => New_Reference_To (Arr, Loc),
1433 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1434 end Arr_Attr;
1435
1436 ------------------------
1437 -- Component_Equality --
1438 ------------------------
1439
1440 function Component_Equality (Typ : Entity_Id) return Node_Id is
1441 Test : Node_Id;
1442 L, R : Node_Id;
1443
1444 begin
1445 -- if a(i1...) /= b(j1...) then return false; end if;
1446
1447 L :=
1448 Make_Indexed_Component (Loc,
1449 Prefix => Make_Identifier (Loc, Chars (A)),
1450 Expressions => Index_List1);
1451
1452 R :=
1453 Make_Indexed_Component (Loc,
1454 Prefix => Make_Identifier (Loc, Chars (B)),
1455 Expressions => Index_List2);
1456
1457 Test := Expand_Composite_Equality
1458 (Nod, Component_Type (Typ), L, R, Decls);
1459
1460 -- If some (sub)component is an unchecked_union, the whole operation
1461 -- will raise program error.
1462
1463 if Nkind (Test) = N_Raise_Program_Error then
1464
1465 -- This node is going to be inserted at a location where a
1466 -- statement is expected: clear its Etype so analysis will
1467 -- set it to the expected Standard_Void_Type.
1468
1469 Set_Etype (Test, Empty);
1470 return Test;
1471
1472 else
1473 return
1474 Make_Implicit_If_Statement (Nod,
1475 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1476 Then_Statements => New_List (
1477 Make_Simple_Return_Statement (Loc,
1478 Expression => New_Occurrence_Of (Standard_False, Loc))));
1479 end if;
1480 end Component_Equality;
1481
1482 ------------------
1483 -- Get_Arg_Type --
1484 ------------------
1485
1486 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1487 T : Entity_Id;
1488 X : Node_Id;
1489
1490 begin
1491 T := Etype (N);
1492
1493 if No (T) then
1494 return Typ;
1495
1496 else
1497 T := Underlying_Type (T);
1498
1499 X := First_Index (T);
1500 while Present (X) loop
1501 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1502 or else
1503 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1504 then
1505 T := Base_Type (T);
1506 exit;
1507 end if;
1508
1509 Next_Index (X);
1510 end loop;
1511
1512 return T;
1513 end if;
1514 end Get_Arg_Type;
1515
1516 --------------------------
1517 -- Handle_One_Dimension --
1518 ---------------------------
1519
1520 function Handle_One_Dimension
1521 (N : Int;
1522 Index : Node_Id) return Node_Id
1523 is
1524 Need_Separate_Indexes : constant Boolean :=
1525 Ltyp /= Rtyp
1526 or else not Is_Constrained (Ltyp);
1527 -- If the index types are identical, and we are working with
1528 -- constrained types, then we can use the same index for both of
1529 -- the arrays.
1530
1531 An : constant Entity_Id := Make_Defining_Identifier (Loc,
1532 Chars => New_Internal_Name ('A'));
1533
1534 Bn : Entity_Id;
1535 Index_T : Entity_Id;
1536 Stm_List : List_Id;
1537 Loop_Stm : Node_Id;
1538
1539 begin
1540 if N > Number_Dimensions (Ltyp) then
1541 return Component_Equality (Ltyp);
1542 end if;
1543
1544 -- Case where we generate a loop
1545
1546 Index_T := Base_Type (Etype (Index));
1547
1548 if Need_Separate_Indexes then
1549 Bn :=
1550 Make_Defining_Identifier (Loc,
1551 Chars => New_Internal_Name ('B'));
1552 else
1553 Bn := An;
1554 end if;
1555
1556 Append (New_Reference_To (An, Loc), Index_List1);
1557 Append (New_Reference_To (Bn, Loc), Index_List2);
1558
1559 Stm_List := New_List (
1560 Handle_One_Dimension (N + 1, Next_Index (Index)));
1561
1562 if Need_Separate_Indexes then
1563
1564 -- Generate guard for loop, followed by increments of indices
1565
1566 Append_To (Stm_List,
1567 Make_Exit_Statement (Loc,
1568 Condition =>
1569 Make_Op_Eq (Loc,
1570 Left_Opnd => New_Reference_To (An, Loc),
1571 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1572
1573 Append_To (Stm_List,
1574 Make_Assignment_Statement (Loc,
1575 Name => New_Reference_To (An, Loc),
1576 Expression =>
1577 Make_Attribute_Reference (Loc,
1578 Prefix => New_Reference_To (Index_T, Loc),
1579 Attribute_Name => Name_Succ,
1580 Expressions => New_List (New_Reference_To (An, Loc)))));
1581
1582 Append_To (Stm_List,
1583 Make_Assignment_Statement (Loc,
1584 Name => New_Reference_To (Bn, Loc),
1585 Expression =>
1586 Make_Attribute_Reference (Loc,
1587 Prefix => New_Reference_To (Index_T, Loc),
1588 Attribute_Name => Name_Succ,
1589 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1590 end if;
1591
1592 -- If separate indexes, we need a declare block for An and Bn, and a
1593 -- loop without an iteration scheme.
1594
1595 if Need_Separate_Indexes then
1596 Loop_Stm :=
1597 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1598
1599 return
1600 Make_Block_Statement (Loc,
1601 Declarations => New_List (
1602 Make_Object_Declaration (Loc,
1603 Defining_Identifier => An,
1604 Object_Definition => New_Reference_To (Index_T, Loc),
1605 Expression => Arr_Attr (A, Name_First, N)),
1606
1607 Make_Object_Declaration (Loc,
1608 Defining_Identifier => Bn,
1609 Object_Definition => New_Reference_To (Index_T, Loc),
1610 Expression => Arr_Attr (B, Name_First, N))),
1611
1612 Handled_Statement_Sequence =>
1613 Make_Handled_Sequence_Of_Statements (Loc,
1614 Statements => New_List (Loop_Stm)));
1615
1616 -- If no separate indexes, return loop statement with explicit
1617 -- iteration scheme on its own
1618
1619 else
1620 Loop_Stm :=
1621 Make_Implicit_Loop_Statement (Nod,
1622 Statements => Stm_List,
1623 Iteration_Scheme =>
1624 Make_Iteration_Scheme (Loc,
1625 Loop_Parameter_Specification =>
1626 Make_Loop_Parameter_Specification (Loc,
1627 Defining_Identifier => An,
1628 Discrete_Subtype_Definition =>
1629 Arr_Attr (A, Name_Range, N))));
1630 return Loop_Stm;
1631 end if;
1632 end Handle_One_Dimension;
1633
1634 -----------------------
1635 -- Test_Empty_Arrays --
1636 -----------------------
1637
1638 function Test_Empty_Arrays return Node_Id is
1639 Alist : Node_Id;
1640 Blist : Node_Id;
1641
1642 Atest : Node_Id;
1643 Btest : Node_Id;
1644
1645 begin
1646 Alist := Empty;
1647 Blist := Empty;
1648 for J in 1 .. Number_Dimensions (Ltyp) loop
1649 Atest :=
1650 Make_Op_Eq (Loc,
1651 Left_Opnd => Arr_Attr (A, Name_Length, J),
1652 Right_Opnd => Make_Integer_Literal (Loc, 0));
1653
1654 Btest :=
1655 Make_Op_Eq (Loc,
1656 Left_Opnd => Arr_Attr (B, Name_Length, J),
1657 Right_Opnd => Make_Integer_Literal (Loc, 0));
1658
1659 if No (Alist) then
1660 Alist := Atest;
1661 Blist := Btest;
1662
1663 else
1664 Alist :=
1665 Make_Or_Else (Loc,
1666 Left_Opnd => Relocate_Node (Alist),
1667 Right_Opnd => Atest);
1668
1669 Blist :=
1670 Make_Or_Else (Loc,
1671 Left_Opnd => Relocate_Node (Blist),
1672 Right_Opnd => Btest);
1673 end if;
1674 end loop;
1675
1676 return
1677 Make_And_Then (Loc,
1678 Left_Opnd => Alist,
1679 Right_Opnd => Blist);
1680 end Test_Empty_Arrays;
1681
1682 -----------------------------
1683 -- Test_Lengths_Correspond --
1684 -----------------------------
1685
1686 function Test_Lengths_Correspond return Node_Id is
1687 Result : Node_Id;
1688 Rtest : Node_Id;
1689
1690 begin
1691 Result := Empty;
1692 for J in 1 .. Number_Dimensions (Ltyp) loop
1693 Rtest :=
1694 Make_Op_Ne (Loc,
1695 Left_Opnd => Arr_Attr (A, Name_Length, J),
1696 Right_Opnd => Arr_Attr (B, Name_Length, J));
1697
1698 if No (Result) then
1699 Result := Rtest;
1700 else
1701 Result :=
1702 Make_Or_Else (Loc,
1703 Left_Opnd => Relocate_Node (Result),
1704 Right_Opnd => Rtest);
1705 end if;
1706 end loop;
1707
1708 return Result;
1709 end Test_Lengths_Correspond;
1710
1711 -- Start of processing for Expand_Array_Equality
1712
1713 begin
1714 Ltyp := Get_Arg_Type (Lhs);
1715 Rtyp := Get_Arg_Type (Rhs);
1716
1717 -- For now, if the argument types are not the same, go to the
1718 -- base type, since the code assumes that the formals have the
1719 -- same type. This is fixable in future ???
1720
1721 if Ltyp /= Rtyp then
1722 Ltyp := Base_Type (Ltyp);
1723 Rtyp := Base_Type (Rtyp);
1724 pragma Assert (Ltyp = Rtyp);
1725 end if;
1726
1727 -- Build list of formals for function
1728
1729 Formals := New_List (
1730 Make_Parameter_Specification (Loc,
1731 Defining_Identifier => A,
1732 Parameter_Type => New_Reference_To (Ltyp, Loc)),
1733
1734 Make_Parameter_Specification (Loc,
1735 Defining_Identifier => B,
1736 Parameter_Type => New_Reference_To (Rtyp, Loc)));
1737
1738 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
1739
1740 -- Build statement sequence for function
1741
1742 Func_Body :=
1743 Make_Subprogram_Body (Loc,
1744 Specification =>
1745 Make_Function_Specification (Loc,
1746 Defining_Unit_Name => Func_Name,
1747 Parameter_Specifications => Formals,
1748 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
1749
1750 Declarations => Decls,
1751
1752 Handled_Statement_Sequence =>
1753 Make_Handled_Sequence_Of_Statements (Loc,
1754 Statements => New_List (
1755
1756 Make_Implicit_If_Statement (Nod,
1757 Condition => Test_Empty_Arrays,
1758 Then_Statements => New_List (
1759 Make_Simple_Return_Statement (Loc,
1760 Expression =>
1761 New_Occurrence_Of (Standard_True, Loc)))),
1762
1763 Make_Implicit_If_Statement (Nod,
1764 Condition => Test_Lengths_Correspond,
1765 Then_Statements => New_List (
1766 Make_Simple_Return_Statement (Loc,
1767 Expression =>
1768 New_Occurrence_Of (Standard_False, Loc)))),
1769
1770 Handle_One_Dimension (1, First_Index (Ltyp)),
1771
1772 Make_Simple_Return_Statement (Loc,
1773 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1774
1775 Set_Has_Completion (Func_Name, True);
1776 Set_Is_Inlined (Func_Name);
1777
1778 -- If the array type is distinct from the type of the arguments,
1779 -- it is the full view of a private type. Apply an unchecked
1780 -- conversion to insure that analysis of the call succeeds.
1781
1782 declare
1783 L, R : Node_Id;
1784
1785 begin
1786 L := Lhs;
1787 R := Rhs;
1788
1789 if No (Etype (Lhs))
1790 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1791 then
1792 L := OK_Convert_To (Ltyp, Lhs);
1793 end if;
1794
1795 if No (Etype (Rhs))
1796 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1797 then
1798 R := OK_Convert_To (Rtyp, Rhs);
1799 end if;
1800
1801 Actuals := New_List (L, R);
1802 end;
1803
1804 Append_To (Bodies, Func_Body);
1805
1806 return
1807 Make_Function_Call (Loc,
1808 Name => New_Reference_To (Func_Name, Loc),
1809 Parameter_Associations => Actuals);
1810 end Expand_Array_Equality;
1811
1812 -----------------------------
1813 -- Expand_Boolean_Operator --
1814 -----------------------------
1815
1816 -- Note that we first get the actual subtypes of the operands,
1817 -- since we always want to deal with types that have bounds.
1818
1819 procedure Expand_Boolean_Operator (N : Node_Id) is
1820 Typ : constant Entity_Id := Etype (N);
1821
1822 begin
1823 -- Special case of bit packed array where both operands are known
1824 -- to be properly aligned. In this case we use an efficient run time
1825 -- routine to carry out the operation (see System.Bit_Ops).
1826
1827 if Is_Bit_Packed_Array (Typ)
1828 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
1829 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
1830 then
1831 Expand_Packed_Boolean_Operator (N);
1832 return;
1833 end if;
1834
1835 -- For the normal non-packed case, the general expansion is to build
1836 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
1837 -- and then inserting it into the tree. The original operator node is
1838 -- then rewritten as a call to this function. We also use this in the
1839 -- packed case if either operand is a possibly unaligned object.
1840
1841 declare
1842 Loc : constant Source_Ptr := Sloc (N);
1843 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1844 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1845 Func_Body : Node_Id;
1846 Func_Name : Entity_Id;
1847
1848 begin
1849 Convert_To_Actual_Subtype (L);
1850 Convert_To_Actual_Subtype (R);
1851 Ensure_Defined (Etype (L), N);
1852 Ensure_Defined (Etype (R), N);
1853 Apply_Length_Check (R, Etype (L));
1854
1855 if Nkind (Parent (N)) = N_Assignment_Statement
1856 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
1857 then
1858 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
1859
1860 elsif Nkind (Parent (N)) = N_Op_Not
1861 and then Nkind (N) = N_Op_And
1862 and then
1863 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
1864 then
1865 return;
1866 else
1867
1868 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
1869 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1870 Insert_Action (N, Func_Body);
1871
1872 -- Now rewrite the expression with a call
1873
1874 Rewrite (N,
1875 Make_Function_Call (Loc,
1876 Name => New_Reference_To (Func_Name, Loc),
1877 Parameter_Associations =>
1878 New_List (
1879 L,
1880 Make_Type_Conversion
1881 (Loc, New_Reference_To (Etype (L), Loc), R))));
1882
1883 Analyze_And_Resolve (N, Typ);
1884 end if;
1885 end;
1886 end Expand_Boolean_Operator;
1887
1888 -------------------------------
1889 -- Expand_Composite_Equality --
1890 -------------------------------
1891
1892 -- This function is only called for comparing internal fields of composite
1893 -- types when these fields are themselves composites. This is a special
1894 -- case because it is not possible to respect normal Ada visibility rules.
1895
1896 function Expand_Composite_Equality
1897 (Nod : Node_Id;
1898 Typ : Entity_Id;
1899 Lhs : Node_Id;
1900 Rhs : Node_Id;
1901 Bodies : List_Id) return Node_Id
1902 is
1903 Loc : constant Source_Ptr := Sloc (Nod);
1904 Full_Type : Entity_Id;
1905 Prim : Elmt_Id;
1906 Eq_Op : Entity_Id;
1907
1908 begin
1909 if Is_Private_Type (Typ) then
1910 Full_Type := Underlying_Type (Typ);
1911 else
1912 Full_Type := Typ;
1913 end if;
1914
1915 -- Defense against malformed private types with no completion
1916 -- the error will be diagnosed later by check_completion
1917
1918 if No (Full_Type) then
1919 return New_Reference_To (Standard_False, Loc);
1920 end if;
1921
1922 Full_Type := Base_Type (Full_Type);
1923
1924 if Is_Array_Type (Full_Type) then
1925
1926 -- If the operand is an elementary type other than a floating-point
1927 -- type, then we can simply use the built-in block bitwise equality,
1928 -- since the predefined equality operators always apply and bitwise
1929 -- equality is fine for all these cases.
1930
1931 if Is_Elementary_Type (Component_Type (Full_Type))
1932 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
1933 then
1934 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
1935
1936 -- For composite component types, and floating-point types, use
1937 -- the expansion. This deals with tagged component types (where
1938 -- we use the applicable equality routine) and floating-point,
1939 -- (where we need to worry about negative zeroes), and also the
1940 -- case of any composite type recursively containing such fields.
1941
1942 else
1943 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
1944 end if;
1945
1946 elsif Is_Tagged_Type (Full_Type) then
1947
1948 -- Call the primitive operation "=" of this type
1949
1950 if Is_Class_Wide_Type (Full_Type) then
1951 Full_Type := Root_Type (Full_Type);
1952 end if;
1953
1954 -- If this is derived from an untagged private type completed
1955 -- with a tagged type, it does not have a full view, so we
1956 -- use the primitive operations of the private type.
1957 -- This check should no longer be necessary when these
1958 -- types receive their full views ???
1959
1960 if Is_Private_Type (Typ)
1961 and then not Is_Tagged_Type (Typ)
1962 and then not Is_Controlled (Typ)
1963 and then Is_Derived_Type (Typ)
1964 and then No (Full_View (Typ))
1965 then
1966 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
1967 else
1968 Prim := First_Elmt (Primitive_Operations (Full_Type));
1969 end if;
1970
1971 loop
1972 Eq_Op := Node (Prim);
1973 exit when Chars (Eq_Op) = Name_Op_Eq
1974 and then Etype (First_Formal (Eq_Op)) =
1975 Etype (Next_Formal (First_Formal (Eq_Op)))
1976 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
1977 Next_Elmt (Prim);
1978 pragma Assert (Present (Prim));
1979 end loop;
1980
1981 Eq_Op := Node (Prim);
1982
1983 return
1984 Make_Function_Call (Loc,
1985 Name => New_Reference_To (Eq_Op, Loc),
1986 Parameter_Associations =>
1987 New_List
1988 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
1989 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
1990
1991 elsif Is_Record_Type (Full_Type) then
1992 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
1993
1994 if Present (Eq_Op) then
1995 if Etype (First_Formal (Eq_Op)) /= Full_Type then
1996
1997 -- Inherited equality from parent type. Convert the actuals
1998 -- to match signature of operation.
1999
2000 declare
2001 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2002
2003 begin
2004 return
2005 Make_Function_Call (Loc,
2006 Name => New_Reference_To (Eq_Op, Loc),
2007 Parameter_Associations =>
2008 New_List (OK_Convert_To (T, Lhs),
2009 OK_Convert_To (T, Rhs)));
2010 end;
2011
2012 else
2013 -- Comparison between Unchecked_Union components
2014
2015 if Is_Unchecked_Union (Full_Type) then
2016 declare
2017 Lhs_Type : Node_Id := Full_Type;
2018 Rhs_Type : Node_Id := Full_Type;
2019 Lhs_Discr_Val : Node_Id;
2020 Rhs_Discr_Val : Node_Id;
2021
2022 begin
2023 -- Lhs subtype
2024
2025 if Nkind (Lhs) = N_Selected_Component then
2026 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2027 end if;
2028
2029 -- Rhs subtype
2030
2031 if Nkind (Rhs) = N_Selected_Component then
2032 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2033 end if;
2034
2035 -- Lhs of the composite equality
2036
2037 if Is_Constrained (Lhs_Type) then
2038
2039 -- Since the enclosing record can never be an
2040 -- Unchecked_Union (this code is executed for records
2041 -- that do not have variants), we may reference its
2042 -- discriminant(s).
2043
2044 if Nkind (Lhs) = N_Selected_Component
2045 and then Has_Per_Object_Constraint (
2046 Entity (Selector_Name (Lhs)))
2047 then
2048 Lhs_Discr_Val :=
2049 Make_Selected_Component (Loc,
2050 Prefix => Prefix (Lhs),
2051 Selector_Name =>
2052 New_Copy (
2053 Get_Discriminant_Value (
2054 First_Discriminant (Lhs_Type),
2055 Lhs_Type,
2056 Stored_Constraint (Lhs_Type))));
2057
2058 else
2059 Lhs_Discr_Val := New_Copy (
2060 Get_Discriminant_Value (
2061 First_Discriminant (Lhs_Type),
2062 Lhs_Type,
2063 Stored_Constraint (Lhs_Type)));
2064
2065 end if;
2066 else
2067 -- It is not possible to infer the discriminant since
2068 -- the subtype is not constrained.
2069
2070 return
2071 Make_Raise_Program_Error (Loc,
2072 Reason => PE_Unchecked_Union_Restriction);
2073 end if;
2074
2075 -- Rhs of the composite equality
2076
2077 if Is_Constrained (Rhs_Type) then
2078 if Nkind (Rhs) = N_Selected_Component
2079 and then Has_Per_Object_Constraint (
2080 Entity (Selector_Name (Rhs)))
2081 then
2082 Rhs_Discr_Val :=
2083 Make_Selected_Component (Loc,
2084 Prefix => Prefix (Rhs),
2085 Selector_Name =>
2086 New_Copy (
2087 Get_Discriminant_Value (
2088 First_Discriminant (Rhs_Type),
2089 Rhs_Type,
2090 Stored_Constraint (Rhs_Type))));
2091
2092 else
2093 Rhs_Discr_Val := New_Copy (
2094 Get_Discriminant_Value (
2095 First_Discriminant (Rhs_Type),
2096 Rhs_Type,
2097 Stored_Constraint (Rhs_Type)));
2098
2099 end if;
2100 else
2101 return
2102 Make_Raise_Program_Error (Loc,
2103 Reason => PE_Unchecked_Union_Restriction);
2104 end if;
2105
2106 -- Call the TSS equality function with the inferred
2107 -- discriminant values.
2108
2109 return
2110 Make_Function_Call (Loc,
2111 Name => New_Reference_To (Eq_Op, Loc),
2112 Parameter_Associations => New_List (
2113 Lhs,
2114 Rhs,
2115 Lhs_Discr_Val,
2116 Rhs_Discr_Val));
2117 end;
2118 end if;
2119
2120 -- Shouldn't this be an else, we can't fall through
2121 -- the above IF, right???
2122
2123 return
2124 Make_Function_Call (Loc,
2125 Name => New_Reference_To (Eq_Op, Loc),
2126 Parameter_Associations => New_List (Lhs, Rhs));
2127 end if;
2128
2129 else
2130 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2131 end if;
2132
2133 else
2134 -- It can be a simple record or the full view of a scalar private
2135
2136 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2137 end if;
2138 end Expand_Composite_Equality;
2139
2140 ------------------------------
2141 -- Expand_Concatenate_Other --
2142 ------------------------------
2143
2144 -- Let n be the number of array operands to be concatenated, Base_Typ
2145 -- their base type, Ind_Typ their index type, and Arr_Typ the original
2146 -- array type to which the concatenantion operator applies, then the
2147 -- following subprogram is constructed:
2148
2149 -- [function Cnn (S1 : Base_Typ; ...; Sn : Base_Typ) return Base_Typ is
2150 -- L : Ind_Typ;
2151 -- begin
2152 -- if S1'Length /= 0 then
2153 -- L := XXX; --> XXX = S1'First if Arr_Typ is unconstrained
2154 -- XXX = Arr_Typ'First otherwise
2155 -- elsif S2'Length /= 0 then
2156 -- L := YYY; --> YYY = S2'First if Arr_Typ is unconstrained
2157 -- YYY = Arr_Typ'First otherwise
2158 -- ...
2159 -- elsif Sn-1'Length /= 0 then
2160 -- L := ZZZ; --> ZZZ = Sn-1'First if Arr_Typ is unconstrained
2161 -- ZZZ = Arr_Typ'First otherwise
2162 -- else
2163 -- return Sn;
2164 -- end if;
2165
2166 -- declare
2167 -- P : Ind_Typ;
2168 -- H : Ind_Typ :=
2169 -- Ind_Typ'Val ((((S1'Length - 1) + S2'Length) + ... + Sn'Length)
2170 -- + Ind_Typ'Pos (L));
2171 -- R : Base_Typ (L .. H);
2172 -- begin
2173 -- if S1'Length /= 0 then
2174 -- P := S1'First;
2175 -- loop
2176 -- R (L) := S1 (P);
2177 -- L := Ind_Typ'Succ (L);
2178 -- exit when P = S1'Last;
2179 -- P := Ind_Typ'Succ (P);
2180 -- end loop;
2181 -- end if;
2182 --
2183 -- if S2'Length /= 0 then
2184 -- L := Ind_Typ'Succ (L);
2185 -- loop
2186 -- R (L) := S2 (P);
2187 -- L := Ind_Typ'Succ (L);
2188 -- exit when P = S2'Last;
2189 -- P := Ind_Typ'Succ (P);
2190 -- end loop;
2191 -- end if;
2192
2193 -- ...
2194
2195 -- if Sn'Length /= 0 then
2196 -- P := Sn'First;
2197 -- loop
2198 -- R (L) := Sn (P);
2199 -- L := Ind_Typ'Succ (L);
2200 -- exit when P = Sn'Last;
2201 -- P := Ind_Typ'Succ (P);
2202 -- end loop;
2203 -- end if;
2204
2205 -- return R;
2206 -- end;
2207 -- end Cnn;]
2208
2209 procedure Expand_Concatenate_Other (Cnode : Node_Id; Opnds : List_Id) is
2210 Loc : constant Source_Ptr := Sloc (Cnode);
2211 Nb_Opnds : constant Nat := List_Length (Opnds);
2212
2213 Arr_Typ : constant Entity_Id := Etype (Entity (Cnode));
2214 Base_Typ : constant Entity_Id := Base_Type (Etype (Cnode));
2215 Ind_Typ : constant Entity_Id := Etype (First_Index (Base_Typ));
2216
2217 Func_Id : Node_Id;
2218 Func_Spec : Node_Id;
2219 Param_Specs : List_Id;
2220
2221 Func_Body : Node_Id;
2222 Func_Decls : List_Id;
2223 Func_Stmts : List_Id;
2224
2225 L_Decl : Node_Id;
2226
2227 If_Stmt : Node_Id;
2228 Elsif_List : List_Id;
2229
2230 Declare_Block : Node_Id;
2231 Declare_Decls : List_Id;
2232 Declare_Stmts : List_Id;
2233
2234 H_Decl : Node_Id;
2235 H_Init : Node_Id;
2236 P_Decl : Node_Id;
2237 R_Decl : Node_Id;
2238 R_Constr : Node_Id;
2239 R_Range : Node_Id;
2240
2241 Params : List_Id;
2242 Operand : Node_Id;
2243
2244 function Copy_Into_R_S (I : Nat; Last : Boolean) return List_Id;
2245 -- Builds the sequence of statement:
2246 -- P := Si'First;
2247 -- loop
2248 -- R (L) := Si (P);
2249 -- L := Ind_Typ'Succ (L);
2250 -- exit when P = Si'Last;
2251 -- P := Ind_Typ'Succ (P);
2252 -- end loop;
2253 --
2254 -- where i is the input parameter I given.
2255 -- If the flag Last is true, the exit statement is emitted before
2256 -- incrementing the lower bound, to prevent the creation out of
2257 -- bound values.
2258
2259 function Init_L (I : Nat) return Node_Id;
2260 -- Builds the statement:
2261 -- L := Arr_Typ'First; If Arr_Typ is constrained
2262 -- L := Si'First; otherwise (where I is the input param given)
2263
2264 function H return Node_Id;
2265 -- Builds reference to identifier H
2266
2267 function Ind_Val (E : Node_Id) return Node_Id;
2268 -- Builds expression Ind_Typ'Val (E);
2269
2270 function L return Node_Id;
2271 -- Builds reference to identifier L
2272
2273 function L_Pos return Node_Id;
2274 -- Builds expression Integer_Type'(Ind_Typ'Pos (L)). We qualify the
2275 -- expression to avoid universal_integer computations whenever possible,
2276 -- in the expression for the upper bound H.
2277
2278 function L_Succ return Node_Id;
2279 -- Builds expression Ind_Typ'Succ (L)
2280
2281 function One return Node_Id;
2282 -- Builds integer literal one
2283
2284 function P return Node_Id;
2285 -- Builds reference to identifier P
2286
2287 function P_Succ return Node_Id;
2288 -- Builds expression Ind_Typ'Succ (P)
2289
2290 function R return Node_Id;
2291 -- Builds reference to identifier R
2292
2293 function S (I : Nat) return Node_Id;
2294 -- Builds reference to identifier Si, where I is the value given
2295
2296 function S_First (I : Nat) return Node_Id;
2297 -- Builds expression Si'First, where I is the value given
2298
2299 function S_Last (I : Nat) return Node_Id;
2300 -- Builds expression Si'Last, where I is the value given
2301
2302 function S_Length (I : Nat) return Node_Id;
2303 -- Builds expression Si'Length, where I is the value given
2304
2305 function S_Length_Test (I : Nat) return Node_Id;
2306 -- Builds expression Si'Length /= 0, where I is the value given
2307
2308 -------------------
2309 -- Copy_Into_R_S --
2310 -------------------
2311
2312 function Copy_Into_R_S (I : Nat; Last : Boolean) return List_Id is
2313 Stmts : constant List_Id := New_List;
2314 P_Start : Node_Id;
2315 Loop_Stmt : Node_Id;
2316 R_Copy : Node_Id;
2317 Exit_Stmt : Node_Id;
2318 L_Inc : Node_Id;
2319 P_Inc : Node_Id;
2320
2321 begin
2322 -- First construct the initializations
2323
2324 P_Start := Make_Assignment_Statement (Loc,
2325 Name => P,
2326 Expression => S_First (I));
2327 Append_To (Stmts, P_Start);
2328
2329 -- Then build the loop
2330
2331 R_Copy := Make_Assignment_Statement (Loc,
2332 Name => Make_Indexed_Component (Loc,
2333 Prefix => R,
2334 Expressions => New_List (L)),
2335 Expression => Make_Indexed_Component (Loc,
2336 Prefix => S (I),
2337 Expressions => New_List (P)));
2338
2339 L_Inc := Make_Assignment_Statement (Loc,
2340 Name => L,
2341 Expression => L_Succ);
2342
2343 Exit_Stmt := Make_Exit_Statement (Loc,
2344 Condition => Make_Op_Eq (Loc, P, S_Last (I)));
2345
2346 P_Inc := Make_Assignment_Statement (Loc,
2347 Name => P,
2348 Expression => P_Succ);
2349
2350 if Last then
2351 Loop_Stmt :=
2352 Make_Implicit_Loop_Statement (Cnode,
2353 Statements => New_List (R_Copy, Exit_Stmt, L_Inc, P_Inc));
2354 else
2355 Loop_Stmt :=
2356 Make_Implicit_Loop_Statement (Cnode,
2357 Statements => New_List (R_Copy, L_Inc, Exit_Stmt, P_Inc));
2358 end if;
2359
2360 Append_To (Stmts, Loop_Stmt);
2361
2362 return Stmts;
2363 end Copy_Into_R_S;
2364
2365 -------
2366 -- H --
2367 -------
2368
2369 function H return Node_Id is
2370 begin
2371 return Make_Identifier (Loc, Name_uH);
2372 end H;
2373
2374 -------------
2375 -- Ind_Val --
2376 -------------
2377
2378 function Ind_Val (E : Node_Id) return Node_Id is
2379 begin
2380 return
2381 Make_Attribute_Reference (Loc,
2382 Prefix => New_Reference_To (Ind_Typ, Loc),
2383 Attribute_Name => Name_Val,
2384 Expressions => New_List (E));
2385 end Ind_Val;
2386
2387 ------------
2388 -- Init_L --
2389 ------------
2390
2391 function Init_L (I : Nat) return Node_Id is
2392 E : Node_Id;
2393
2394 begin
2395 if Is_Constrained (Arr_Typ) then
2396 E := Make_Attribute_Reference (Loc,
2397 Prefix => New_Reference_To (Arr_Typ, Loc),
2398 Attribute_Name => Name_First);
2399
2400 else
2401 E := S_First (I);
2402 end if;
2403
2404 return Make_Assignment_Statement (Loc, Name => L, Expression => E);
2405 end Init_L;
2406
2407 -------
2408 -- L --
2409 -------
2410
2411 function L return Node_Id is
2412 begin
2413 return Make_Identifier (Loc, Name_uL);
2414 end L;
2415
2416 -----------
2417 -- L_Pos --
2418 -----------
2419
2420 function L_Pos return Node_Id is
2421 Target_Type : Entity_Id;
2422
2423 begin
2424 -- If the index type is an enumeration type, the computation
2425 -- can be done in standard integer. Otherwise, choose a large
2426 -- enough integer type.
2427
2428 if Is_Enumeration_Type (Ind_Typ)
2429 or else Root_Type (Ind_Typ) = Standard_Integer
2430 or else Root_Type (Ind_Typ) = Standard_Short_Integer
2431 or else Root_Type (Ind_Typ) = Standard_Short_Short_Integer
2432 then
2433 Target_Type := Standard_Integer;
2434 else
2435 Target_Type := Root_Type (Ind_Typ);
2436 end if;
2437
2438 return
2439 Make_Qualified_Expression (Loc,
2440 Subtype_Mark => New_Reference_To (Target_Type, Loc),
2441 Expression =>
2442 Make_Attribute_Reference (Loc,
2443 Prefix => New_Reference_To (Ind_Typ, Loc),
2444 Attribute_Name => Name_Pos,
2445 Expressions => New_List (L)));
2446 end L_Pos;
2447
2448 ------------
2449 -- L_Succ --
2450 ------------
2451
2452 function L_Succ return Node_Id is
2453 begin
2454 return
2455 Make_Attribute_Reference (Loc,
2456 Prefix => New_Reference_To (Ind_Typ, Loc),
2457 Attribute_Name => Name_Succ,
2458 Expressions => New_List (L));
2459 end L_Succ;
2460
2461 ---------
2462 -- One --
2463 ---------
2464
2465 function One return Node_Id is
2466 begin
2467 return Make_Integer_Literal (Loc, 1);
2468 end One;
2469
2470 -------
2471 -- P --
2472 -------
2473
2474 function P return Node_Id is
2475 begin
2476 return Make_Identifier (Loc, Name_uP);
2477 end P;
2478
2479 ------------
2480 -- P_Succ --
2481 ------------
2482
2483 function P_Succ return Node_Id is
2484 begin
2485 return
2486 Make_Attribute_Reference (Loc,
2487 Prefix => New_Reference_To (Ind_Typ, Loc),
2488 Attribute_Name => Name_Succ,
2489 Expressions => New_List (P));
2490 end P_Succ;
2491
2492 -------
2493 -- R --
2494 -------
2495
2496 function R return Node_Id is
2497 begin
2498 return Make_Identifier (Loc, Name_uR);
2499 end R;
2500
2501 -------
2502 -- S --
2503 -------
2504
2505 function S (I : Nat) return Node_Id is
2506 begin
2507 return Make_Identifier (Loc, New_External_Name ('S', I));
2508 end S;
2509
2510 -------------
2511 -- S_First --
2512 -------------
2513
2514 function S_First (I : Nat) return Node_Id is
2515 begin
2516 return Make_Attribute_Reference (Loc,
2517 Prefix => S (I),
2518 Attribute_Name => Name_First);
2519 end S_First;
2520
2521 ------------
2522 -- S_Last --
2523 ------------
2524
2525 function S_Last (I : Nat) return Node_Id is
2526 begin
2527 return Make_Attribute_Reference (Loc,
2528 Prefix => S (I),
2529 Attribute_Name => Name_Last);
2530 end S_Last;
2531
2532 --------------
2533 -- S_Length --
2534 --------------
2535
2536 function S_Length (I : Nat) return Node_Id is
2537 begin
2538 return Make_Attribute_Reference (Loc,
2539 Prefix => S (I),
2540 Attribute_Name => Name_Length);
2541 end S_Length;
2542
2543 -------------------
2544 -- S_Length_Test --
2545 -------------------
2546
2547 function S_Length_Test (I : Nat) return Node_Id is
2548 begin
2549 return
2550 Make_Op_Ne (Loc,
2551 Left_Opnd => S_Length (I),
2552 Right_Opnd => Make_Integer_Literal (Loc, 0));
2553 end S_Length_Test;
2554
2555 -- Start of processing for Expand_Concatenate_Other
2556
2557 begin
2558 -- Construct the parameter specs and the overall function spec
2559
2560 Param_Specs := New_List;
2561 for I in 1 .. Nb_Opnds loop
2562 Append_To
2563 (Param_Specs,
2564 Make_Parameter_Specification (Loc,
2565 Defining_Identifier =>
2566 Make_Defining_Identifier (Loc, New_External_Name ('S', I)),
2567 Parameter_Type => New_Reference_To (Base_Typ, Loc)));
2568 end loop;
2569
2570 Func_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
2571 Func_Spec :=
2572 Make_Function_Specification (Loc,
2573 Defining_Unit_Name => Func_Id,
2574 Parameter_Specifications => Param_Specs,
2575 Result_Definition => New_Reference_To (Base_Typ, Loc));
2576
2577 -- Construct L's object declaration
2578
2579 L_Decl :=
2580 Make_Object_Declaration (Loc,
2581 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uL),
2582 Object_Definition => New_Reference_To (Ind_Typ, Loc));
2583
2584 Func_Decls := New_List (L_Decl);
2585
2586 -- Construct the if-then-elsif statements
2587
2588 Elsif_List := New_List;
2589 for I in 2 .. Nb_Opnds - 1 loop
2590 Append_To (Elsif_List, Make_Elsif_Part (Loc,
2591 Condition => S_Length_Test (I),
2592 Then_Statements => New_List (Init_L (I))));
2593 end loop;
2594
2595 If_Stmt :=
2596 Make_Implicit_If_Statement (Cnode,
2597 Condition => S_Length_Test (1),
2598 Then_Statements => New_List (Init_L (1)),
2599 Elsif_Parts => Elsif_List,
2600 Else_Statements => New_List (Make_Simple_Return_Statement (Loc,
2601 Expression => S (Nb_Opnds))));
2602
2603 -- Construct the declaration for H
2604
2605 P_Decl :=
2606 Make_Object_Declaration (Loc,
2607 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uP),
2608 Object_Definition => New_Reference_To (Ind_Typ, Loc));
2609
2610 H_Init := Make_Op_Subtract (Loc, S_Length (1), One);
2611 for I in 2 .. Nb_Opnds loop
2612 H_Init := Make_Op_Add (Loc, H_Init, S_Length (I));
2613 end loop;
2614 H_Init := Ind_Val (Make_Op_Add (Loc, H_Init, L_Pos));
2615
2616 H_Decl :=
2617 Make_Object_Declaration (Loc,
2618 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uH),
2619 Object_Definition => New_Reference_To (Ind_Typ, Loc),
2620 Expression => H_Init);
2621
2622 -- Construct the declaration for R
2623
2624 R_Range := Make_Range (Loc, Low_Bound => L, High_Bound => H);
2625 R_Constr :=
2626 Make_Index_Or_Discriminant_Constraint (Loc,
2627 Constraints => New_List (R_Range));
2628
2629 R_Decl :=
2630 Make_Object_Declaration (Loc,
2631 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uR),
2632 Object_Definition =>
2633 Make_Subtype_Indication (Loc,
2634 Subtype_Mark => New_Reference_To (Base_Typ, Loc),
2635 Constraint => R_Constr));
2636
2637 -- Construct the declarations for the declare block
2638
2639 Declare_Decls := New_List (P_Decl, H_Decl, R_Decl);
2640
2641 -- Construct list of statements for the declare block
2642
2643 Declare_Stmts := New_List;
2644 for I in 1 .. Nb_Opnds loop
2645 Append_To (Declare_Stmts,
2646 Make_Implicit_If_Statement (Cnode,
2647 Condition => S_Length_Test (I),
2648 Then_Statements => Copy_Into_R_S (I, I = Nb_Opnds)));
2649 end loop;
2650
2651 Append_To
2652 (Declare_Stmts, Make_Simple_Return_Statement (Loc, Expression => R));
2653
2654 -- Construct the declare block
2655
2656 Declare_Block := Make_Block_Statement (Loc,
2657 Declarations => Declare_Decls,
2658 Handled_Statement_Sequence =>
2659 Make_Handled_Sequence_Of_Statements (Loc, Declare_Stmts));
2660
2661 -- Construct the list of function statements
2662
2663 Func_Stmts := New_List (If_Stmt, Declare_Block);
2664
2665 -- Construct the function body
2666
2667 Func_Body :=
2668 Make_Subprogram_Body (Loc,
2669 Specification => Func_Spec,
2670 Declarations => Func_Decls,
2671 Handled_Statement_Sequence =>
2672 Make_Handled_Sequence_Of_Statements (Loc, Func_Stmts));
2673
2674 -- Insert the newly generated function in the code. This is analyzed
2675 -- with all checks off, since we have completed all the checks.
2676
2677 -- Note that this does *not* fix the array concatenation bug when the
2678 -- low bound is Integer'first sibce that bug comes from the pointer
2679 -- dereferencing an unconstrained array. An there we need a constraint
2680 -- check to make sure the length of the concatenated array is ok. ???
2681
2682 Insert_Action (Cnode, Func_Body, Suppress => All_Checks);
2683
2684 -- Construct list of arguments for the function call
2685
2686 Params := New_List;
2687 Operand := First (Opnds);
2688 for I in 1 .. Nb_Opnds loop
2689 Append_To (Params, Relocate_Node (Operand));
2690 Next (Operand);
2691 end loop;
2692
2693 -- Insert the function call
2694
2695 Rewrite
2696 (Cnode,
2697 Make_Function_Call (Loc, New_Reference_To (Func_Id, Loc), Params));
2698
2699 Analyze_And_Resolve (Cnode, Base_Typ);
2700 Set_Is_Inlined (Func_Id);
2701 end Expand_Concatenate_Other;
2702
2703 -------------------------------
2704 -- Expand_Concatenate_String --
2705 -------------------------------
2706
2707 procedure Expand_Concatenate_String (Cnode : Node_Id; Opnds : List_Id) is
2708 Loc : constant Source_Ptr := Sloc (Cnode);
2709 Opnd1 : constant Node_Id := First (Opnds);
2710 Opnd2 : constant Node_Id := Next (Opnd1);
2711 Typ1 : constant Entity_Id := Base_Type (Etype (Opnd1));
2712 Typ2 : constant Entity_Id := Base_Type (Etype (Opnd2));
2713
2714 R : RE_Id;
2715 -- RE_Id value for function to be called
2716
2717 begin
2718 -- In all cases, we build a call to a routine giving the list of
2719 -- arguments as the parameter list to the routine.
2720
2721 case List_Length (Opnds) is
2722 when 2 =>
2723 if Typ1 = Standard_Character then
2724 if Typ2 = Standard_Character then
2725 R := RE_Str_Concat_CC;
2726
2727 else
2728 pragma Assert (Typ2 = Standard_String);
2729 R := RE_Str_Concat_CS;
2730 end if;
2731
2732 elsif Typ1 = Standard_String then
2733 if Typ2 = Standard_Character then
2734 R := RE_Str_Concat_SC;
2735
2736 else
2737 pragma Assert (Typ2 = Standard_String);
2738 R := RE_Str_Concat;
2739 end if;
2740
2741 -- If we have anything other than Standard_Character or
2742 -- Standard_String, then we must have had a serious error
2743 -- earlier, so we just abandon the attempt at expansion.
2744
2745 else
2746 pragma Assert (Serious_Errors_Detected > 0);
2747 return;
2748 end if;
2749
2750 when 3 =>
2751 R := RE_Str_Concat_3;
2752
2753 when 4 =>
2754 R := RE_Str_Concat_4;
2755
2756 when 5 =>
2757 R := RE_Str_Concat_5;
2758
2759 when others =>
2760 R := RE_Null;
2761 raise Program_Error;
2762 end case;
2763
2764 -- Now generate the appropriate call
2765
2766 Rewrite (Cnode,
2767 Make_Function_Call (Sloc (Cnode),
2768 Name => New_Occurrence_Of (RTE (R), Loc),
2769 Parameter_Associations => Opnds));
2770
2771 Analyze_And_Resolve (Cnode, Standard_String);
2772
2773 exception
2774 when RE_Not_Available =>
2775 return;
2776 end Expand_Concatenate_String;
2777
2778 ------------------------
2779 -- Expand_N_Allocator --
2780 ------------------------
2781
2782 procedure Expand_N_Allocator (N : Node_Id) is
2783 PtrT : constant Entity_Id := Etype (N);
2784 Dtyp : constant Entity_Id := Designated_Type (PtrT);
2785 Etyp : constant Entity_Id := Etype (Expression (N));
2786 Loc : constant Source_Ptr := Sloc (N);
2787 Desig : Entity_Id;
2788 Temp : Entity_Id;
2789 Nod : Node_Id;
2790
2791 procedure Complete_Coextension_Finalization;
2792 -- Generate finalization calls for all nested coextensions of N. This
2793 -- routine may allocate list controllers if necessary.
2794
2795 procedure Rewrite_Coextension (N : Node_Id);
2796 -- Static coextensions have the same lifetime as the entity they
2797 -- constrain. Such occurences can be rewritten as aliased objects
2798 -- and their unrestricted access used instead of the coextension.
2799
2800 ---------------------------------------
2801 -- Complete_Coextension_Finalization --
2802 ---------------------------------------
2803
2804 procedure Complete_Coextension_Finalization is
2805 Coext : Node_Id;
2806 Coext_Elmt : Elmt_Id;
2807 Flist : Node_Id;
2808 Ref : Node_Id;
2809
2810 function Inside_A_Return_Statement (N : Node_Id) return Boolean;
2811 -- Determine whether node N is part of a return statement
2812
2813 function Needs_Initialization_Call (N : Node_Id) return Boolean;
2814 -- Determine whether node N is a subtype indicator allocator which
2815 -- asts a coextension. Such coextensions need initialization.
2816
2817 -------------------------------
2818 -- Inside_A_Return_Statement --
2819 -------------------------------
2820
2821 function Inside_A_Return_Statement (N : Node_Id) return Boolean is
2822 P : Node_Id;
2823
2824 begin
2825 P := Parent (N);
2826 while Present (P) loop
2827 if Nkind_In
2828 (P, N_Extended_Return_Statement, N_Simple_Return_Statement)
2829 then
2830 return True;
2831
2832 -- Stop the traversal when we reach a subprogram body
2833
2834 elsif Nkind (P) = N_Subprogram_Body then
2835 return False;
2836 end if;
2837
2838 P := Parent (P);
2839 end loop;
2840
2841 return False;
2842 end Inside_A_Return_Statement;
2843
2844 -------------------------------
2845 -- Needs_Initialization_Call --
2846 -------------------------------
2847
2848 function Needs_Initialization_Call (N : Node_Id) return Boolean is
2849 Obj_Decl : Node_Id;
2850
2851 begin
2852 if Nkind (N) = N_Explicit_Dereference
2853 and then Nkind (Prefix (N)) = N_Identifier
2854 and then Nkind (Parent (Entity (Prefix (N)))) =
2855 N_Object_Declaration
2856 then
2857 Obj_Decl := Parent (Entity (Prefix (N)));
2858
2859 return
2860 Present (Expression (Obj_Decl))
2861 and then Nkind (Expression (Obj_Decl)) = N_Allocator
2862 and then Nkind (Expression (Expression (Obj_Decl))) /=
2863 N_Qualified_Expression;
2864 end if;
2865
2866 return False;
2867 end Needs_Initialization_Call;
2868
2869 -- Start of processing for Complete_Coextension_Finalization
2870
2871 begin
2872 -- When a coextension root is inside a return statement, we need to
2873 -- use the finalization chain of the function's scope. This does not
2874 -- apply for controlled named access types because in those cases we
2875 -- can use the finalization chain of the type itself.
2876
2877 if Inside_A_Return_Statement (N)
2878 and then
2879 (Ekind (PtrT) = E_Anonymous_Access_Type
2880 or else
2881 (Ekind (PtrT) = E_Access_Type
2882 and then No (Associated_Final_Chain (PtrT))))
2883 then
2884 declare
2885 Decl : Node_Id;
2886 Outer_S : Entity_Id;
2887 S : Entity_Id := Current_Scope;
2888
2889 begin
2890 while Present (S) and then S /= Standard_Standard loop
2891 if Ekind (S) = E_Function then
2892 Outer_S := Scope (S);
2893
2894 -- Retrieve the declaration of the body
2895
2896 Decl := Parent (Parent (
2897 Corresponding_Body (Parent (Parent (S)))));
2898 exit;
2899 end if;
2900
2901 S := Scope (S);
2902 end loop;
2903
2904 -- Push the scope of the function body since we are inserting
2905 -- the list before the body, but we are currently in the body
2906 -- itself. Override the finalization list of PtrT since the
2907 -- finalization context is now different.
2908
2909 Push_Scope (Outer_S);
2910 Build_Final_List (Decl, PtrT);
2911 Pop_Scope;
2912 end;
2913
2914 -- The root allocator may not be controlled, but it still needs a
2915 -- finalization list for all nested coextensions.
2916
2917 elsif No (Associated_Final_Chain (PtrT)) then
2918 Build_Final_List (N, PtrT);
2919 end if;
2920
2921 Flist :=
2922 Make_Selected_Component (Loc,
2923 Prefix =>
2924 New_Reference_To (Associated_Final_Chain (PtrT), Loc),
2925 Selector_Name =>
2926 Make_Identifier (Loc, Name_F));
2927
2928 Coext_Elmt := First_Elmt (Coextensions (N));
2929 while Present (Coext_Elmt) loop
2930 Coext := Node (Coext_Elmt);
2931
2932 -- Generate:
2933 -- typ! (coext.all)
2934
2935 if Nkind (Coext) = N_Identifier then
2936 Ref := Make_Unchecked_Type_Conversion (Loc,
2937 Subtype_Mark =>
2938 New_Reference_To (Etype (Coext), Loc),
2939 Expression =>
2940 Make_Explicit_Dereference (Loc,
2941 New_Copy_Tree (Coext)));
2942 else
2943 Ref := New_Copy_Tree (Coext);
2944 end if;
2945
2946 -- Generate:
2947 -- initialize (Ref)
2948 -- attach_to_final_list (Ref, Flist, 2)
2949
2950 if Needs_Initialization_Call (Coext) then
2951 Insert_Actions (N,
2952 Make_Init_Call (
2953 Ref => Ref,
2954 Typ => Etype (Coext),
2955 Flist_Ref => Flist,
2956 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
2957
2958 -- Generate:
2959 -- attach_to_final_list (Ref, Flist, 2)
2960
2961 else
2962 Insert_Action (N,
2963 Make_Attach_Call (
2964 Obj_Ref => Ref,
2965 Flist_Ref => New_Copy_Tree (Flist),
2966 With_Attach => Make_Integer_Literal (Loc, Uint_2)));
2967 end if;
2968
2969 Next_Elmt (Coext_Elmt);
2970 end loop;
2971 end Complete_Coextension_Finalization;
2972
2973 -------------------------
2974 -- Rewrite_Coextension --
2975 -------------------------
2976
2977 procedure Rewrite_Coextension (N : Node_Id) is
2978 Temp : constant Node_Id :=
2979 Make_Defining_Identifier (Loc,
2980 New_Internal_Name ('C'));
2981
2982 -- Generate:
2983 -- Cnn : aliased Etyp;
2984
2985 Decl : constant Node_Id :=
2986 Make_Object_Declaration (Loc,
2987 Defining_Identifier => Temp,
2988 Aliased_Present => True,
2989 Object_Definition =>
2990 New_Occurrence_Of (Etyp, Loc));
2991 Nod : Node_Id;
2992
2993 begin
2994 if Nkind (Expression (N)) = N_Qualified_Expression then
2995 Set_Expression (Decl, Expression (Expression (N)));
2996 end if;
2997
2998 -- Find the proper insertion node for the declaration
2999
3000 Nod := Parent (N);
3001 while Present (Nod) loop
3002 exit when Nkind (Nod) in N_Statement_Other_Than_Procedure_Call
3003 or else Nkind (Nod) = N_Procedure_Call_Statement
3004 or else Nkind (Nod) in N_Declaration;
3005 Nod := Parent (Nod);
3006 end loop;
3007
3008 Insert_Before (Nod, Decl);
3009 Analyze (Decl);
3010
3011 Rewrite (N,
3012 Make_Attribute_Reference (Loc,
3013 Prefix => New_Occurrence_Of (Temp, Loc),
3014 Attribute_Name => Name_Unrestricted_Access));
3015
3016 Analyze_And_Resolve (N, PtrT);
3017 end Rewrite_Coextension;
3018
3019 -- Start of processing for Expand_N_Allocator
3020
3021 begin
3022 -- RM E.2.3(22). We enforce that the expected type of an allocator
3023 -- shall not be a remote access-to-class-wide-limited-private type
3024
3025 -- Why is this being done at expansion time, seems clearly wrong ???
3026
3027 Validate_Remote_Access_To_Class_Wide_Type (N);
3028
3029 -- Set the Storage Pool
3030
3031 Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT)));
3032
3033 if Present (Storage_Pool (N)) then
3034 if Is_RTE (Storage_Pool (N), RE_SS_Pool) then
3035 if VM_Target = No_VM then
3036 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
3037 end if;
3038
3039 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
3040 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
3041
3042 else
3043 Set_Procedure_To_Call (N,
3044 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
3045 end if;
3046 end if;
3047
3048 -- Under certain circumstances we can replace an allocator by an
3049 -- access to statically allocated storage. The conditions, as noted
3050 -- in AARM 3.10 (10c) are as follows:
3051
3052 -- Size and initial value is known at compile time
3053 -- Access type is access-to-constant
3054
3055 -- The allocator is not part of a constraint on a record component,
3056 -- because in that case the inserted actions are delayed until the
3057 -- record declaration is fully analyzed, which is too late for the
3058 -- analysis of the rewritten allocator.
3059
3060 if Is_Access_Constant (PtrT)
3061 and then Nkind (Expression (N)) = N_Qualified_Expression
3062 and then Compile_Time_Known_Value (Expression (Expression (N)))
3063 and then Size_Known_At_Compile_Time (Etype (Expression
3064 (Expression (N))))
3065 and then not Is_Record_Type (Current_Scope)
3066 then
3067 -- Here we can do the optimization. For the allocator
3068
3069 -- new x'(y)
3070
3071 -- We insert an object declaration
3072
3073 -- Tnn : aliased x := y;
3074
3075 -- and replace the allocator by Tnn'Unrestricted_Access.
3076 -- Tnn is marked as requiring static allocation.
3077
3078 Temp :=
3079 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
3080
3081 Desig := Subtype_Mark (Expression (N));
3082
3083 -- If context is constrained, use constrained subtype directly,
3084 -- so that the constant is not labelled as having a nomimally
3085 -- unconstrained subtype.
3086
3087 if Entity (Desig) = Base_Type (Dtyp) then
3088 Desig := New_Occurrence_Of (Dtyp, Loc);
3089 end if;
3090
3091 Insert_Action (N,
3092 Make_Object_Declaration (Loc,
3093 Defining_Identifier => Temp,
3094 Aliased_Present => True,
3095 Constant_Present => Is_Access_Constant (PtrT),
3096 Object_Definition => Desig,
3097 Expression => Expression (Expression (N))));
3098
3099 Rewrite (N,
3100 Make_Attribute_Reference (Loc,
3101 Prefix => New_Occurrence_Of (Temp, Loc),
3102 Attribute_Name => Name_Unrestricted_Access));
3103
3104 Analyze_And_Resolve (N, PtrT);
3105
3106 -- We set the variable as statically allocated, since we don't
3107 -- want it going on the stack of the current procedure!
3108
3109 Set_Is_Statically_Allocated (Temp);
3110 return;
3111 end if;
3112
3113 -- Same if the allocator is an access discriminant for a local object:
3114 -- instead of an allocator we create a local value and constrain the
3115 -- the enclosing object with the corresponding access attribute.
3116
3117 if Is_Static_Coextension (N) then
3118 Rewrite_Coextension (N);
3119 return;
3120 end if;
3121
3122 -- The current allocator creates an object which may contain nested
3123 -- coextensions. Use the current allocator's finalization list to
3124 -- generate finalization call for all nested coextensions.
3125
3126 if Is_Coextension_Root (N) then
3127 Complete_Coextension_Finalization;
3128 end if;
3129
3130 -- Handle case of qualified expression (other than optimization above)
3131
3132 if Nkind (Expression (N)) = N_Qualified_Expression then
3133 Expand_Allocator_Expression (N);
3134 return;
3135 end if;
3136
3137 -- If the allocator is for a type which requires initialization, and
3138 -- there is no initial value (i.e. operand is a subtype indication
3139 -- rather than a qualifed expression), then we must generate a call
3140 -- to the initialization routine. This is done using an expression
3141 -- actions node:
3142
3143 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
3144
3145 -- Here ptr_T is the pointer type for the allocator, and T is the
3146 -- subtype of the allocator. A special case arises if the designated
3147 -- type of the access type is a task or contains tasks. In this case
3148 -- the call to Init (Temp.all ...) is replaced by code that ensures
3149 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
3150 -- for details). In addition, if the type T is a task T, then the
3151 -- first argument to Init must be converted to the task record type.
3152
3153 declare
3154 T : constant Entity_Id := Entity (Expression (N));
3155 Init : Entity_Id;
3156 Arg1 : Node_Id;
3157 Args : List_Id;
3158 Decls : List_Id;
3159 Decl : Node_Id;
3160 Discr : Elmt_Id;
3161 Flist : Node_Id;
3162 Temp_Decl : Node_Id;
3163 Temp_Type : Entity_Id;
3164 Attach_Level : Uint;
3165
3166 begin
3167 if No_Initialization (N) then
3168 null;
3169
3170 -- Case of no initialization procedure present
3171
3172 elsif not Has_Non_Null_Base_Init_Proc (T) then
3173
3174 -- Case of simple initialization required
3175
3176 if Needs_Simple_Initialization (T) then
3177 Rewrite (Expression (N),
3178 Make_Qualified_Expression (Loc,
3179 Subtype_Mark => New_Occurrence_Of (T, Loc),
3180 Expression => Get_Simple_Init_Val (T, Loc)));
3181
3182 Analyze_And_Resolve (Expression (Expression (N)), T);
3183 Analyze_And_Resolve (Expression (N), T);
3184 Set_Paren_Count (Expression (Expression (N)), 1);
3185 Expand_N_Allocator (N);
3186
3187 -- No initialization required
3188
3189 else
3190 null;
3191 end if;
3192
3193 -- Case of initialization procedure present, must be called
3194
3195 else
3196 Init := Base_Init_Proc (T);
3197 Nod := N;
3198 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
3199
3200 -- Construct argument list for the initialization routine call
3201
3202 Arg1 :=
3203 Make_Explicit_Dereference (Loc,
3204 Prefix => New_Reference_To (Temp, Loc));
3205 Set_Assignment_OK (Arg1);
3206 Temp_Type := PtrT;
3207
3208 -- The initialization procedure expects a specific type. if the
3209 -- context is access to class wide, indicate that the object being
3210 -- allocated has the right specific type.
3211
3212 if Is_Class_Wide_Type (Dtyp) then
3213 Arg1 := Unchecked_Convert_To (T, Arg1);
3214 end if;
3215
3216 -- If designated type is a concurrent type or if it is private
3217 -- type whose definition is a concurrent type, the first argument
3218 -- in the Init routine has to be unchecked conversion to the
3219 -- corresponding record type. If the designated type is a derived
3220 -- type, we also convert the argument to its root type.
3221
3222 if Is_Concurrent_Type (T) then
3223 Arg1 :=
3224 Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1);
3225
3226 elsif Is_Private_Type (T)
3227 and then Present (Full_View (T))
3228 and then Is_Concurrent_Type (Full_View (T))
3229 then
3230 Arg1 :=
3231 Unchecked_Convert_To
3232 (Corresponding_Record_Type (Full_View (T)), Arg1);
3233
3234 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
3235 declare
3236 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
3237
3238 begin
3239 Arg1 := OK_Convert_To (Etype (Ftyp), Arg1);
3240 Set_Etype (Arg1, Ftyp);
3241 end;
3242 end if;
3243
3244 Args := New_List (Arg1);
3245
3246 -- For the task case, pass the Master_Id of the access type as
3247 -- the value of the _Master parameter, and _Chain as the value
3248 -- of the _Chain parameter (_Chain will be defined as part of
3249 -- the generated code for the allocator).
3250
3251 -- In Ada 2005, the context may be a function that returns an
3252 -- anonymous access type. In that case the Master_Id has been
3253 -- created when expanding the function declaration.
3254
3255 if Has_Task (T) then
3256 if No (Master_Id (Base_Type (PtrT))) then
3257
3258 -- If we have a non-library level task with the restriction
3259 -- No_Task_Hierarchy set, then no point in expanding.
3260
3261 if not Is_Library_Level_Entity (T)
3262 and then Restriction_Active (No_Task_Hierarchy)
3263 then
3264 return;
3265 end if;
3266
3267 -- The designated type was an incomplete type, and the
3268 -- access type did not get expanded. Salvage it now.
3269
3270 pragma Assert (Present (Parent (Base_Type (PtrT))));
3271 Expand_N_Full_Type_Declaration (Parent (Base_Type (PtrT)));
3272 end if;
3273
3274 -- If the context of the allocator is a declaration or an
3275 -- assignment, we can generate a meaningful image for it,
3276 -- even though subsequent assignments might remove the
3277 -- connection between task and entity. We build this image
3278 -- when the left-hand side is a simple variable, a simple
3279 -- indexed assignment or a simple selected component.
3280
3281 if Nkind (Parent (N)) = N_Assignment_Statement then
3282 declare
3283 Nam : constant Node_Id := Name (Parent (N));
3284
3285 begin
3286 if Is_Entity_Name (Nam) then
3287 Decls :=
3288 Build_Task_Image_Decls (
3289 Loc,
3290 New_Occurrence_Of
3291 (Entity (Nam), Sloc (Nam)), T);
3292
3293 elsif Nkind_In
3294 (Nam, N_Indexed_Component, N_Selected_Component)
3295 and then Is_Entity_Name (Prefix (Nam))
3296 then
3297 Decls :=
3298 Build_Task_Image_Decls
3299 (Loc, Nam, Etype (Prefix (Nam)));
3300 else
3301 Decls := Build_Task_Image_Decls (Loc, T, T);
3302 end if;
3303 end;
3304
3305 elsif Nkind (Parent (N)) = N_Object_Declaration then
3306 Decls :=
3307 Build_Task_Image_Decls (
3308 Loc, Defining_Identifier (Parent (N)), T);
3309
3310 else
3311 Decls := Build_Task_Image_Decls (Loc, T, T);
3312 end if;
3313
3314 Append_To (Args,
3315 New_Reference_To
3316 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
3317 Append_To (Args, Make_Identifier (Loc, Name_uChain));
3318
3319 Decl := Last (Decls);
3320 Append_To (Args,
3321 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
3322
3323 -- Has_Task is false, Decls not used
3324
3325 else
3326 Decls := No_List;
3327 end if;
3328
3329 -- Add discriminants if discriminated type
3330
3331 declare
3332 Dis : Boolean := False;
3333 Typ : Entity_Id;
3334
3335 begin
3336 if Has_Discriminants (T) then
3337 Dis := True;
3338 Typ := T;
3339
3340 elsif Is_Private_Type (T)
3341 and then Present (Full_View (T))
3342 and then Has_Discriminants (Full_View (T))
3343 then
3344 Dis := True;
3345 Typ := Full_View (T);
3346 end if;
3347
3348 if Dis then
3349 -- If the allocated object will be constrained by the
3350 -- default values for discriminants, then build a
3351 -- subtype with those defaults, and change the allocated
3352 -- subtype to that. Note that this happens in fewer
3353 -- cases in Ada 2005 (AI-363).
3354
3355 if not Is_Constrained (Typ)
3356 and then Present (Discriminant_Default_Value
3357 (First_Discriminant (Typ)))
3358 and then (Ada_Version < Ada_05
3359 or else not Has_Constrained_Partial_View (Typ))
3360 then
3361 Typ := Build_Default_Subtype (Typ, N);
3362 Set_Expression (N, New_Reference_To (Typ, Loc));
3363 end if;
3364
3365 Discr := First_Elmt (Discriminant_Constraint (Typ));
3366 while Present (Discr) loop
3367 Nod := Node (Discr);
3368 Append (New_Copy_Tree (Node (Discr)), Args);
3369
3370 -- AI-416: when the discriminant constraint is an
3371 -- anonymous access type make sure an accessibility
3372 -- check is inserted if necessary (3.10.2(22.q/2))
3373
3374 if Ada_Version >= Ada_05
3375 and then Ekind (Etype (Nod)) = E_Anonymous_Access_Type
3376 then
3377 Apply_Accessibility_Check (Nod, Typ);
3378 end if;
3379
3380 Next_Elmt (Discr);
3381 end loop;
3382 end if;
3383 end;
3384
3385 -- We set the allocator as analyzed so that when we analyze the
3386 -- expression actions node, we do not get an unwanted recursive
3387 -- expansion of the allocator expression.
3388
3389 Set_Analyzed (N, True);
3390 Nod := Relocate_Node (N);
3391
3392 -- Here is the transformation:
3393 -- input: new T
3394 -- output: Temp : constant ptr_T := new T;
3395 -- Init (Temp.all, ...);
3396 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
3397 -- <CTRL> Initialize (Finalizable (Temp.all));
3398
3399 -- Here ptr_T is the pointer type for the allocator, and is the
3400 -- subtype of the allocator.
3401
3402 Temp_Decl :=
3403 Make_Object_Declaration (Loc,
3404 Defining_Identifier => Temp,
3405 Constant_Present => True,
3406 Object_Definition => New_Reference_To (Temp_Type, Loc),
3407 Expression => Nod);
3408
3409 Set_Assignment_OK (Temp_Decl);
3410 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
3411
3412 -- If the designated type is a task type or contains tasks,
3413 -- create block to activate created tasks, and insert
3414 -- declaration for Task_Image variable ahead of call.
3415
3416 if Has_Task (T) then
3417 declare
3418 L : constant List_Id := New_List;
3419 Blk : Node_Id;
3420
3421 begin
3422 Build_Task_Allocate_Block (L, Nod, Args);
3423 Blk := Last (L);
3424
3425 Insert_List_Before (First (Declarations (Blk)), Decls);
3426 Insert_Actions (N, L);
3427 end;
3428
3429 else
3430 Insert_Action (N,
3431 Make_Procedure_Call_Statement (Loc,
3432 Name => New_Reference_To (Init, Loc),
3433 Parameter_Associations => Args));
3434 end if;
3435
3436 if Controlled_Type (T) then
3437
3438 -- Postpone the generation of a finalization call for the
3439 -- current allocator if it acts as a coextension.
3440
3441 if Is_Dynamic_Coextension (N) then
3442 if No (Coextensions (N)) then
3443 Set_Coextensions (N, New_Elmt_List);
3444 end if;
3445
3446 Append_Elmt (New_Copy_Tree (Arg1), Coextensions (N));
3447
3448 else
3449 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
3450
3451 -- Anonymous access types created for access parameters
3452 -- are attached to an explicitly constructed controller,
3453 -- which ensures that they can be finalized properly, even
3454 -- if their deallocation might not happen. The list
3455 -- associated with the controller is doubly-linked. For
3456 -- other anonymous access types, the object may end up
3457 -- on the global final list which is singly-linked.
3458 -- Work needed for access discriminants in Ada 2005 ???
3459
3460 if Ekind (PtrT) = E_Anonymous_Access_Type
3461 and then
3462 Nkind (Associated_Node_For_Itype (PtrT))
3463 not in N_Subprogram_Specification
3464 then
3465 Attach_Level := Uint_1;
3466 else
3467 Attach_Level := Uint_2;
3468 end if;
3469
3470 Insert_Actions (N,
3471 Make_Init_Call (
3472 Ref => New_Copy_Tree (Arg1),
3473 Typ => T,
3474 Flist_Ref => Flist,
3475 With_Attach => Make_Integer_Literal
3476 (Loc, Attach_Level)));
3477 end if;
3478 end if;
3479
3480 Rewrite (N, New_Reference_To (Temp, Loc));
3481 Analyze_And_Resolve (N, PtrT);
3482 end if;
3483 end;
3484
3485 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
3486 -- object that has been rewritten as a reference, we displace "this"
3487 -- to reference properly its secondary dispatch table.
3488
3489 if Nkind (N) = N_Identifier
3490 and then Is_Interface (Dtyp)
3491 then
3492 Displace_Allocator_Pointer (N);
3493 end if;
3494
3495 exception
3496 when RE_Not_Available =>
3497 return;
3498 end Expand_N_Allocator;
3499
3500 -----------------------
3501 -- Expand_N_And_Then --
3502 -----------------------
3503
3504 -- Expand into conditional expression if Actions present, and also deal
3505 -- with optimizing case of arguments being True or False.
3506
3507 procedure Expand_N_And_Then (N : Node_Id) is
3508 Loc : constant Source_Ptr := Sloc (N);
3509 Typ : constant Entity_Id := Etype (N);
3510 Left : constant Node_Id := Left_Opnd (N);
3511 Right : constant Node_Id := Right_Opnd (N);
3512 Actlist : List_Id;
3513
3514 begin
3515 -- Deal with non-standard booleans
3516
3517 if Is_Boolean_Type (Typ) then
3518 Adjust_Condition (Left);
3519 Adjust_Condition (Right);
3520 Set_Etype (N, Standard_Boolean);
3521 end if;
3522
3523 -- Check for cases of left argument is True or False
3524
3525 if Nkind (Left) = N_Identifier then
3526
3527 -- If left argument is True, change (True and then Right) to Right.
3528 -- Any actions associated with Right will be executed unconditionally
3529 -- and can thus be inserted into the tree unconditionally.
3530
3531 if Entity (Left) = Standard_True then
3532 if Present (Actions (N)) then
3533 Insert_Actions (N, Actions (N));
3534 end if;
3535
3536 Rewrite (N, Right);
3537 Adjust_Result_Type (N, Typ);
3538 return;
3539
3540 -- If left argument is False, change (False and then Right) to False.
3541 -- In this case we can forget the actions associated with Right,
3542 -- since they will never be executed.
3543
3544 elsif Entity (Left) = Standard_False then
3545 Kill_Dead_Code (Right);
3546 Kill_Dead_Code (Actions (N));
3547 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
3548 Adjust_Result_Type (N, Typ);
3549 return;
3550 end if;
3551 end if;
3552
3553 -- If Actions are present, we expand
3554
3555 -- left and then right
3556
3557 -- into
3558
3559 -- if left then right else false end
3560
3561 -- with the actions becoming the Then_Actions of the conditional
3562 -- expression. This conditional expression is then further expanded
3563 -- (and will eventually disappear)
3564
3565 if Present (Actions (N)) then
3566 Actlist := Actions (N);
3567 Rewrite (N,
3568 Make_Conditional_Expression (Loc,
3569 Expressions => New_List (
3570 Left,
3571 Right,
3572 New_Occurrence_Of (Standard_False, Loc))));
3573
3574 Set_Then_Actions (N, Actlist);
3575 Analyze_And_Resolve (N, Standard_Boolean);
3576 Adjust_Result_Type (N, Typ);
3577 return;
3578 end if;
3579
3580 -- No actions present, check for cases of right argument True/False
3581
3582 if Nkind (Right) = N_Identifier then
3583
3584 -- Change (Left and then True) to Left. Note that we know there
3585 -- are no actions associated with the True operand, since we
3586 -- just checked for this case above.
3587
3588 if Entity (Right) = Standard_True then
3589 Rewrite (N, Left);
3590
3591 -- Change (Left and then False) to False, making sure to preserve
3592 -- any side effects associated with the Left operand.
3593
3594 elsif Entity (Right) = Standard_False then
3595 Remove_Side_Effects (Left);
3596 Rewrite
3597 (N, New_Occurrence_Of (Standard_False, Loc));
3598 end if;
3599 end if;
3600
3601 Adjust_Result_Type (N, Typ);
3602 end Expand_N_And_Then;
3603
3604 -------------------------------------
3605 -- Expand_N_Conditional_Expression --
3606 -------------------------------------
3607
3608 -- Expand into expression actions if then/else actions present
3609
3610 procedure Expand_N_Conditional_Expression (N : Node_Id) is
3611 Loc : constant Source_Ptr := Sloc (N);
3612 Cond : constant Node_Id := First (Expressions (N));
3613 Thenx : constant Node_Id := Next (Cond);
3614 Elsex : constant Node_Id := Next (Thenx);
3615 Typ : constant Entity_Id := Etype (N);
3616 Cnn : Entity_Id;
3617 New_If : Node_Id;
3618
3619 begin
3620 -- If either then or else actions are present, then given:
3621
3622 -- if cond then then-expr else else-expr end
3623
3624 -- we insert the following sequence of actions (using Insert_Actions):
3625
3626 -- Cnn : typ;
3627 -- if cond then
3628 -- <<then actions>>
3629 -- Cnn := then-expr;
3630 -- else
3631 -- <<else actions>>
3632 -- Cnn := else-expr
3633 -- end if;
3634
3635 -- and replace the conditional expression by a reference to Cnn
3636
3637 if Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
3638 Cnn := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
3639
3640 New_If :=
3641 Make_Implicit_If_Statement (N,
3642 Condition => Relocate_Node (Cond),
3643
3644 Then_Statements => New_List (
3645 Make_Assignment_Statement (Sloc (Thenx),
3646 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
3647 Expression => Relocate_Node (Thenx))),
3648
3649 Else_Statements => New_List (
3650 Make_Assignment_Statement (Sloc (Elsex),
3651 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
3652 Expression => Relocate_Node (Elsex))));
3653
3654 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
3655 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
3656
3657 if Present (Then_Actions (N)) then
3658 Insert_List_Before
3659 (First (Then_Statements (New_If)), Then_Actions (N));
3660 end if;
3661
3662 if Present (Else_Actions (N)) then
3663 Insert_List_Before
3664 (First (Else_Statements (New_If)), Else_Actions (N));
3665 end if;
3666
3667 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
3668
3669 Insert_Action (N,
3670 Make_Object_Declaration (Loc,
3671 Defining_Identifier => Cnn,
3672 Object_Definition => New_Occurrence_Of (Typ, Loc)));
3673
3674 Insert_Action (N, New_If);
3675 Analyze_And_Resolve (N, Typ);
3676 end if;
3677 end Expand_N_Conditional_Expression;
3678
3679 -----------------------------------
3680 -- Expand_N_Explicit_Dereference --
3681 -----------------------------------
3682
3683 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
3684 begin
3685 -- Insert explicit dereference call for the checked storage pool case
3686
3687 Insert_Dereference_Action (Prefix (N));
3688 end Expand_N_Explicit_Dereference;
3689
3690 -----------------
3691 -- Expand_N_In --
3692 -----------------
3693
3694 procedure Expand_N_In (N : Node_Id) is
3695 Loc : constant Source_Ptr := Sloc (N);
3696 Rtyp : constant Entity_Id := Etype (N);
3697 Lop : constant Node_Id := Left_Opnd (N);
3698 Rop : constant Node_Id := Right_Opnd (N);
3699 Static : constant Boolean := Is_OK_Static_Expression (N);
3700
3701 procedure Substitute_Valid_Check;
3702 -- Replaces node N by Lop'Valid. This is done when we have an explicit
3703 -- test for the left operand being in range of its subtype.
3704
3705 ----------------------------
3706 -- Substitute_Valid_Check --
3707 ----------------------------
3708
3709 procedure Substitute_Valid_Check is
3710 begin
3711 Rewrite (N,
3712 Make_Attribute_Reference (Loc,
3713 Prefix => Relocate_Node (Lop),
3714 Attribute_Name => Name_Valid));
3715
3716 Analyze_And_Resolve (N, Rtyp);
3717
3718 Error_Msg_N ("?explicit membership test may be optimized away", N);
3719 Error_Msg_N ("\?use ''Valid attribute instead", N);
3720 return;
3721 end Substitute_Valid_Check;
3722
3723 -- Start of processing for Expand_N_In
3724
3725 begin
3726 -- Check case of explicit test for an expression in range of its
3727 -- subtype. This is suspicious usage and we replace it with a 'Valid
3728 -- test and give a warning.
3729
3730 if Is_Scalar_Type (Etype (Lop))
3731 and then Nkind (Rop) in N_Has_Entity
3732 and then Etype (Lop) = Entity (Rop)
3733 and then Comes_From_Source (N)
3734 and then VM_Target = No_VM
3735 then
3736 Substitute_Valid_Check;
3737 return;
3738 end if;
3739
3740 -- Do validity check on operands
3741
3742 if Validity_Checks_On and Validity_Check_Operands then
3743 Ensure_Valid (Left_Opnd (N));
3744 Validity_Check_Range (Right_Opnd (N));
3745 end if;
3746
3747 -- Case of explicit range
3748
3749 if Nkind (Rop) = N_Range then
3750 declare
3751 Lo : constant Node_Id := Low_Bound (Rop);
3752 Hi : constant Node_Id := High_Bound (Rop);
3753
3754 Ltyp : constant Entity_Id := Etype (Lop);
3755
3756 Lo_Orig : constant Node_Id := Original_Node (Lo);
3757 Hi_Orig : constant Node_Id := Original_Node (Hi);
3758
3759 Lcheck : constant Compare_Result := Compile_Time_Compare (Lop, Lo);
3760 Ucheck : constant Compare_Result := Compile_Time_Compare (Lop, Hi);
3761
3762 Warn1 : constant Boolean :=
3763 Constant_Condition_Warnings
3764 and then Comes_From_Source (N);
3765 -- This must be true for any of the optimization warnings, we
3766 -- clearly want to give them only for source with the flag on.
3767
3768 Warn2 : constant Boolean :=
3769 Warn1
3770 and then Nkind (Original_Node (Rop)) = N_Range
3771 and then Is_Integer_Type (Etype (Lo));
3772 -- For the case where only one bound warning is elided, we also
3773 -- insist on an explicit range and an integer type. The reason is
3774 -- that the use of enumeration ranges including an end point is
3775 -- common, as is the use of a subtype name, one of whose bounds
3776 -- is the same as the type of the expression.
3777
3778 begin
3779 -- If test is explicit x'first .. x'last, replace by valid check
3780
3781 if Is_Scalar_Type (Ltyp)
3782 and then Nkind (Lo_Orig) = N_Attribute_Reference
3783 and then Attribute_Name (Lo_Orig) = Name_First
3784 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
3785 and then Entity (Prefix (Lo_Orig)) = Ltyp
3786 and then Nkind (Hi_Orig) = N_Attribute_Reference
3787 and then Attribute_Name (Hi_Orig) = Name_Last
3788 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
3789 and then Entity (Prefix (Hi_Orig)) = Ltyp
3790 and then Comes_From_Source (N)
3791 and then VM_Target = No_VM
3792 then
3793 Substitute_Valid_Check;
3794 return;
3795 end if;
3796
3797 -- If bounds of type are known at compile time, and the end points
3798 -- are known at compile time and identical, this is another case
3799 -- for substituting a valid test. We only do this for discrete
3800 -- types, since it won't arise in practice for float types.
3801
3802 if Comes_From_Source (N)
3803 and then Is_Discrete_Type (Ltyp)
3804 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
3805 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
3806 and then Compile_Time_Known_Value (Lo)
3807 and then Compile_Time_Known_Value (Hi)
3808 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
3809 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
3810 then
3811 Substitute_Valid_Check;
3812 return;
3813 end if;
3814
3815 -- If we have an explicit range, do a bit of optimization based
3816 -- on range analysis (we may be able to kill one or both checks).
3817
3818 -- If either check is known to fail, replace result by False since
3819 -- the other check does not matter. Preserve the static flag for
3820 -- legality checks, because we are constant-folding beyond RM 4.9.
3821
3822 if Lcheck = LT or else Ucheck = GT then
3823 if Warn1 then
3824 Error_Msg_N ("?range test optimized away", N);
3825 Error_Msg_N ("\?value is known to be out of range", N);
3826 end if;
3827
3828 Rewrite (N,
3829 New_Reference_To (Standard_False, Loc));
3830 Analyze_And_Resolve (N, Rtyp);
3831 Set_Is_Static_Expression (N, Static);
3832
3833 return;
3834
3835 -- If both checks are known to succeed, replace result
3836 -- by True, since we know we are in range.
3837
3838 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
3839 if Warn1 then
3840 Error_Msg_N ("?range test optimized away", N);
3841 Error_Msg_N ("\?value is known to be in range", N);
3842 end if;
3843
3844 Rewrite (N,
3845 New_Reference_To (Standard_True, Loc));
3846 Analyze_And_Resolve (N, Rtyp);
3847 Set_Is_Static_Expression (N, Static);
3848
3849 return;
3850
3851 -- If lower bound check succeeds and upper bound check is not
3852 -- known to succeed or fail, then replace the range check with
3853 -- a comparison against the upper bound.
3854
3855 elsif Lcheck in Compare_GE then
3856 if Warn2 then
3857 Error_Msg_N ("?lower bound test optimized away", Lo);
3858 Error_Msg_N ("\?value is known to be in range", Lo);
3859 end if;
3860
3861 Rewrite (N,
3862 Make_Op_Le (Loc,
3863 Left_Opnd => Lop,
3864 Right_Opnd => High_Bound (Rop)));
3865 Analyze_And_Resolve (N, Rtyp);
3866
3867 return;
3868
3869 -- If upper bound check succeeds and lower bound check is not
3870 -- known to succeed or fail, then replace the range check with
3871 -- a comparison against the lower bound.
3872
3873 elsif Ucheck in Compare_LE then
3874 if Warn2 then
3875 Error_Msg_N ("?upper bound test optimized away", Hi);
3876 Error_Msg_N ("\?value is known to be in range", Hi);
3877 end if;
3878
3879 Rewrite (N,
3880 Make_Op_Ge (Loc,
3881 Left_Opnd => Lop,
3882 Right_Opnd => Low_Bound (Rop)));
3883 Analyze_And_Resolve (N, Rtyp);
3884
3885 return;
3886 end if;
3887 end;
3888
3889 -- For all other cases of an explicit range, nothing to be done
3890
3891 return;
3892
3893 -- Here right operand is a subtype mark
3894
3895 else
3896 declare
3897 Typ : Entity_Id := Etype (Rop);
3898 Is_Acc : constant Boolean := Is_Access_Type (Typ);
3899 Obj : Node_Id := Lop;
3900 Cond : Node_Id := Empty;
3901
3902 begin
3903 Remove_Side_Effects (Obj);
3904
3905 -- For tagged type, do tagged membership operation
3906
3907 if Is_Tagged_Type (Typ) then
3908
3909 -- No expansion will be performed when VM_Target, as the VM
3910 -- back-ends will handle the membership tests directly (tags
3911 -- are not explicitly represented in Java objects, so the
3912 -- normal tagged membership expansion is not what we want).
3913
3914 if VM_Target = No_VM then
3915 Rewrite (N, Tagged_Membership (N));
3916 Analyze_And_Resolve (N, Rtyp);
3917 end if;
3918
3919 return;
3920
3921 -- If type is scalar type, rewrite as x in t'first .. t'last.
3922 -- This reason we do this is that the bounds may have the wrong
3923 -- type if they come from the original type definition.
3924
3925 elsif Is_Scalar_Type (Typ) then
3926 Rewrite (Rop,
3927 Make_Range (Loc,
3928 Low_Bound =>
3929 Make_Attribute_Reference (Loc,
3930 Attribute_Name => Name_First,
3931 Prefix => New_Reference_To (Typ, Loc)),
3932
3933 High_Bound =>
3934 Make_Attribute_Reference (Loc,
3935 Attribute_Name => Name_Last,
3936 Prefix => New_Reference_To (Typ, Loc))));
3937 Analyze_And_Resolve (N, Rtyp);
3938 return;
3939
3940 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
3941 -- a membership test if the subtype mark denotes a constrained
3942 -- Unchecked_Union subtype and the expression lacks inferable
3943 -- discriminants.
3944
3945 elsif Is_Unchecked_Union (Base_Type (Typ))
3946 and then Is_Constrained (Typ)
3947 and then not Has_Inferable_Discriminants (Lop)
3948 then
3949 Insert_Action (N,
3950 Make_Raise_Program_Error (Loc,
3951 Reason => PE_Unchecked_Union_Restriction));
3952
3953 -- Prevent Gigi from generating incorrect code by rewriting
3954 -- the test as a standard False.
3955
3956 Rewrite (N,
3957 New_Occurrence_Of (Standard_False, Loc));
3958
3959 return;
3960 end if;
3961
3962 -- Here we have a non-scalar type
3963
3964 if Is_Acc then
3965 Typ := Designated_Type (Typ);
3966 end if;
3967
3968 if not Is_Constrained (Typ) then
3969 Rewrite (N,
3970 New_Reference_To (Standard_True, Loc));
3971 Analyze_And_Resolve (N, Rtyp);
3972
3973 -- For the constrained array case, we have to check the
3974 -- subscripts for an exact match if the lengths are
3975 -- non-zero (the lengths must match in any case).
3976
3977 elsif Is_Array_Type (Typ) then
3978
3979 Check_Subscripts : declare
3980 function Construct_Attribute_Reference
3981 (E : Node_Id;
3982 Nam : Name_Id;
3983 Dim : Nat) return Node_Id;
3984 -- Build attribute reference E'Nam(Dim)
3985
3986 -----------------------------------
3987 -- Construct_Attribute_Reference --
3988 -----------------------------------
3989
3990 function Construct_Attribute_Reference
3991 (E : Node_Id;
3992 Nam : Name_Id;
3993 Dim : Nat) return Node_Id
3994 is
3995 begin
3996 return
3997 Make_Attribute_Reference (Loc,
3998 Prefix => E,
3999 Attribute_Name => Nam,
4000 Expressions => New_List (
4001 Make_Integer_Literal (Loc, Dim)));
4002 end Construct_Attribute_Reference;
4003
4004 -- Start processing for Check_Subscripts
4005
4006 begin
4007 for J in 1 .. Number_Dimensions (Typ) loop
4008 Evolve_And_Then (Cond,
4009 Make_Op_Eq (Loc,
4010 Left_Opnd =>
4011 Construct_Attribute_Reference
4012 (Duplicate_Subexpr_No_Checks (Obj),
4013 Name_First, J),
4014 Right_Opnd =>
4015 Construct_Attribute_Reference
4016 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
4017
4018 Evolve_And_Then (Cond,
4019 Make_Op_Eq (Loc,
4020 Left_Opnd =>
4021 Construct_Attribute_Reference
4022 (Duplicate_Subexpr_No_Checks (Obj),
4023 Name_Last, J),
4024 Right_Opnd =>
4025 Construct_Attribute_Reference
4026 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
4027 end loop;
4028
4029 if Is_Acc then
4030 Cond :=
4031 Make_Or_Else (Loc,
4032 Left_Opnd =>
4033 Make_Op_Eq (Loc,
4034 Left_Opnd => Obj,
4035 Right_Opnd => Make_Null (Loc)),
4036 Right_Opnd => Cond);
4037 end if;
4038
4039 Rewrite (N, Cond);
4040 Analyze_And_Resolve (N, Rtyp);
4041 end Check_Subscripts;
4042
4043 -- These are the cases where constraint checks may be
4044 -- required, e.g. records with possible discriminants
4045
4046 else
4047 -- Expand the test into a series of discriminant comparisons.
4048 -- The expression that is built is the negation of the one
4049 -- that is used for checking discriminant constraints.
4050
4051 Obj := Relocate_Node (Left_Opnd (N));
4052
4053 if Has_Discriminants (Typ) then
4054 Cond := Make_Op_Not (Loc,
4055 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
4056
4057 if Is_Acc then
4058 Cond := Make_Or_Else (Loc,
4059 Left_Opnd =>
4060 Make_Op_Eq (Loc,
4061 Left_Opnd => Obj,
4062 Right_Opnd => Make_Null (Loc)),
4063 Right_Opnd => Cond);
4064 end if;
4065
4066 else
4067 Cond := New_Occurrence_Of (Standard_True, Loc);
4068 end if;
4069
4070 Rewrite (N, Cond);
4071 Analyze_And_Resolve (N, Rtyp);
4072 end if;
4073 end;
4074 end if;
4075 end Expand_N_In;
4076
4077 --------------------------------
4078 -- Expand_N_Indexed_Component --
4079 --------------------------------
4080
4081 procedure Expand_N_Indexed_Component (N : Node_Id) is
4082 Loc : constant Source_Ptr := Sloc (N);
4083 Typ : constant Entity_Id := Etype (N);
4084 P : constant Node_Id := Prefix (N);
4085 T : constant Entity_Id := Etype (P);
4086
4087 begin
4088 -- A special optimization, if we have an indexed component that
4089 -- is selecting from a slice, then we can eliminate the slice,
4090 -- since, for example, x (i .. j)(k) is identical to x(k). The
4091 -- only difference is the range check required by the slice. The
4092 -- range check for the slice itself has already been generated.
4093 -- The range check for the subscripting operation is ensured
4094 -- by converting the subject to the subtype of the slice.
4095
4096 -- This optimization not only generates better code, avoiding
4097 -- slice messing especially in the packed case, but more importantly
4098 -- bypasses some problems in handling this peculiar case, for
4099 -- example, the issue of dealing specially with object renamings.
4100
4101 if Nkind (P) = N_Slice then
4102 Rewrite (N,
4103 Make_Indexed_Component (Loc,
4104 Prefix => Prefix (P),
4105 Expressions => New_List (
4106 Convert_To
4107 (Etype (First_Index (Etype (P))),
4108 First (Expressions (N))))));
4109 Analyze_And_Resolve (N, Typ);
4110 return;
4111 end if;
4112
4113 -- If the prefix is an access type, then we unconditionally rewrite
4114 -- if as an explicit deference. This simplifies processing for several
4115 -- cases, including packed array cases and certain cases in which
4116 -- checks must be generated. We used to try to do this only when it
4117 -- was necessary, but it cleans up the code to do it all the time.
4118
4119 if Is_Access_Type (T) then
4120 Insert_Explicit_Dereference (P);
4121 Analyze_And_Resolve (P, Designated_Type (T));
4122 end if;
4123
4124 -- Generate index and validity checks
4125
4126 Generate_Index_Checks (N);
4127
4128 if Validity_Checks_On and then Validity_Check_Subscripts then
4129 Apply_Subscript_Validity_Checks (N);
4130 end if;
4131
4132 -- All done for the non-packed case
4133
4134 if not Is_Packed (Etype (Prefix (N))) then
4135 return;
4136 end if;
4137
4138 -- For packed arrays that are not bit-packed (i.e. the case of an array
4139 -- with one or more index types with a non-coniguous enumeration type),
4140 -- we can always use the normal packed element get circuit.
4141
4142 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
4143 Expand_Packed_Element_Reference (N);
4144 return;
4145 end if;
4146
4147 -- For a reference to a component of a bit packed array, we have to
4148 -- convert it to a reference to the corresponding Packed_Array_Type.
4149 -- We only want to do this for simple references, and not for:
4150
4151 -- Left side of assignment, or prefix of left side of assignment,
4152 -- or prefix of the prefix, to handle packed arrays of packed arrays,
4153 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
4154
4155 -- Renaming objects in renaming associations
4156 -- This case is handled when a use of the renamed variable occurs
4157
4158 -- Actual parameters for a procedure call
4159 -- This case is handled in Exp_Ch6.Expand_Actuals
4160
4161 -- The second expression in a 'Read attribute reference
4162
4163 -- The prefix of an address or size attribute reference
4164
4165 -- The following circuit detects these exceptions
4166
4167 declare
4168 Child : Node_Id := N;
4169 Parnt : Node_Id := Parent (N);
4170
4171 begin
4172 loop
4173 if Nkind (Parnt) = N_Unchecked_Expression then
4174 null;
4175
4176 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
4177 N_Procedure_Call_Statement)
4178 or else (Nkind (Parnt) = N_Parameter_Association
4179 and then
4180 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
4181 then
4182 return;
4183
4184 elsif Nkind (Parnt) = N_Attribute_Reference
4185 and then (Attribute_Name (Parnt) = Name_Address
4186 or else
4187 Attribute_Name (Parnt) = Name_Size)
4188 and then Prefix (Parnt) = Child
4189 then
4190 return;
4191
4192 elsif Nkind (Parnt) = N_Assignment_Statement
4193 and then Name (Parnt) = Child
4194 then
4195 return;
4196
4197 -- If the expression is an index of an indexed component,
4198 -- it must be expanded regardless of context.
4199
4200 elsif Nkind (Parnt) = N_Indexed_Component
4201 and then Child /= Prefix (Parnt)
4202 then
4203 Expand_Packed_Element_Reference (N);
4204 return;
4205
4206 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
4207 and then Name (Parent (Parnt)) = Parnt
4208 then
4209 return;
4210
4211 elsif Nkind (Parnt) = N_Attribute_Reference
4212 and then Attribute_Name (Parnt) = Name_Read
4213 and then Next (First (Expressions (Parnt))) = Child
4214 then
4215 return;
4216
4217 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
4218 and then Prefix (Parnt) = Child
4219 then
4220 null;
4221
4222 else
4223 Expand_Packed_Element_Reference (N);
4224 return;
4225 end if;
4226
4227 -- Keep looking up tree for unchecked expression, or if we are
4228 -- the prefix of a possible assignment left side.
4229
4230 Child := Parnt;
4231 Parnt := Parent (Child);
4232 end loop;
4233 end;
4234 end Expand_N_Indexed_Component;
4235
4236 ---------------------
4237 -- Expand_N_Not_In --
4238 ---------------------
4239
4240 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
4241 -- can be done. This avoids needing to duplicate this expansion code.
4242
4243 procedure Expand_N_Not_In (N : Node_Id) is
4244 Loc : constant Source_Ptr := Sloc (N);
4245 Typ : constant Entity_Id := Etype (N);
4246 Cfs : constant Boolean := Comes_From_Source (N);
4247
4248 begin
4249 Rewrite (N,
4250 Make_Op_Not (Loc,
4251 Right_Opnd =>
4252 Make_In (Loc,
4253 Left_Opnd => Left_Opnd (N),
4254 Right_Opnd => Right_Opnd (N))));
4255
4256 -- We want this to appear as coming from source if original does (see
4257 -- tranformations in Expand_N_In).
4258
4259 Set_Comes_From_Source (N, Cfs);
4260 Set_Comes_From_Source (Right_Opnd (N), Cfs);
4261
4262 -- Now analyze tranformed node
4263
4264 Analyze_And_Resolve (N, Typ);
4265 end Expand_N_Not_In;
4266
4267 -------------------
4268 -- Expand_N_Null --
4269 -------------------
4270
4271 -- The only replacement required is for the case of a null of type
4272 -- that is an access to protected subprogram. We represent such
4273 -- access values as a record, and so we must replace the occurrence
4274 -- of null by the equivalent record (with a null address and a null
4275 -- pointer in it), so that the backend creates the proper value.
4276
4277 procedure Expand_N_Null (N : Node_Id) is
4278 Loc : constant Source_Ptr := Sloc (N);
4279 Typ : constant Entity_Id := Etype (N);
4280 Agg : Node_Id;
4281
4282 begin
4283 if Is_Access_Protected_Subprogram_Type (Typ) then
4284 Agg :=
4285 Make_Aggregate (Loc,
4286 Expressions => New_List (
4287 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
4288 Make_Null (Loc)));
4289
4290 Rewrite (N, Agg);
4291 Analyze_And_Resolve (N, Equivalent_Type (Typ));
4292
4293 -- For subsequent semantic analysis, the node must retain its
4294 -- type. Gigi in any case replaces this type by the corresponding
4295 -- record type before processing the node.
4296
4297 Set_Etype (N, Typ);
4298 end if;
4299
4300 exception
4301 when RE_Not_Available =>
4302 return;
4303 end Expand_N_Null;
4304
4305 ---------------------
4306 -- Expand_N_Op_Abs --
4307 ---------------------
4308
4309 procedure Expand_N_Op_Abs (N : Node_Id) is
4310 Loc : constant Source_Ptr := Sloc (N);
4311 Expr : constant Node_Id := Right_Opnd (N);
4312
4313 begin
4314 Unary_Op_Validity_Checks (N);
4315
4316 -- Deal with software overflow checking
4317
4318 if not Backend_Overflow_Checks_On_Target
4319 and then Is_Signed_Integer_Type (Etype (N))
4320 and then Do_Overflow_Check (N)
4321 then
4322 -- The only case to worry about is when the argument is
4323 -- equal to the largest negative number, so what we do is
4324 -- to insert the check:
4325
4326 -- [constraint_error when Expr = typ'Base'First]
4327
4328 -- with the usual Duplicate_Subexpr use coding for expr
4329
4330 Insert_Action (N,
4331 Make_Raise_Constraint_Error (Loc,
4332 Condition =>
4333 Make_Op_Eq (Loc,
4334 Left_Opnd => Duplicate_Subexpr (Expr),
4335 Right_Opnd =>
4336 Make_Attribute_Reference (Loc,
4337 Prefix =>
4338 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
4339 Attribute_Name => Name_First)),
4340 Reason => CE_Overflow_Check_Failed));
4341 end if;
4342
4343 -- Vax floating-point types case
4344
4345 if Vax_Float (Etype (N)) then
4346 Expand_Vax_Arith (N);
4347 end if;
4348 end Expand_N_Op_Abs;
4349
4350 ---------------------
4351 -- Expand_N_Op_Add --
4352 ---------------------
4353
4354 procedure Expand_N_Op_Add (N : Node_Id) is
4355 Typ : constant Entity_Id := Etype (N);
4356
4357 begin
4358 Binary_Op_Validity_Checks (N);
4359
4360 -- N + 0 = 0 + N = N for integer types
4361
4362 if Is_Integer_Type (Typ) then
4363 if Compile_Time_Known_Value (Right_Opnd (N))
4364 and then Expr_Value (Right_Opnd (N)) = Uint_0
4365 then
4366 Rewrite (N, Left_Opnd (N));
4367 return;
4368
4369 elsif Compile_Time_Known_Value (Left_Opnd (N))
4370 and then Expr_Value (Left_Opnd (N)) = Uint_0
4371 then
4372 Rewrite (N, Right_Opnd (N));
4373 return;
4374 end if;
4375 end if;
4376
4377 -- Arithmetic overflow checks for signed integer/fixed point types
4378
4379 if Is_Signed_Integer_Type (Typ)
4380 or else Is_Fixed_Point_Type (Typ)
4381 then
4382 Apply_Arithmetic_Overflow_Check (N);
4383 return;
4384
4385 -- Vax floating-point types case
4386
4387 elsif Vax_Float (Typ) then
4388 Expand_Vax_Arith (N);
4389 end if;
4390 end Expand_N_Op_Add;
4391
4392 ---------------------
4393 -- Expand_N_Op_And --
4394 ---------------------
4395
4396 procedure Expand_N_Op_And (N : Node_Id) is
4397 Typ : constant Entity_Id := Etype (N);
4398
4399 begin
4400 Binary_Op_Validity_Checks (N);
4401
4402 if Is_Array_Type (Etype (N)) then
4403 Expand_Boolean_Operator (N);
4404
4405 elsif Is_Boolean_Type (Etype (N)) then
4406 Adjust_Condition (Left_Opnd (N));
4407 Adjust_Condition (Right_Opnd (N));
4408 Set_Etype (N, Standard_Boolean);
4409 Adjust_Result_Type (N, Typ);
4410 end if;
4411 end Expand_N_Op_And;
4412
4413 ------------------------
4414 -- Expand_N_Op_Concat --
4415 ------------------------
4416
4417 Max_Available_String_Operands : Int := -1;
4418 -- This is initialized the first time this routine is called. It records
4419 -- a value of 0,2,3,4,5 depending on what Str_Concat_n procedures are
4420 -- available in the run-time:
4421 --
4422 -- 0 None available
4423 -- 2 RE_Str_Concat available, RE_Str_Concat_3 not available
4424 -- 3 RE_Str_Concat/Concat_2 available, RE_Str_Concat_4 not available
4425 -- 4 RE_Str_Concat/Concat_2/3 available, RE_Str_Concat_5 not available
4426 -- 5 All routines including RE_Str_Concat_5 available
4427
4428 Char_Concat_Available : Boolean;
4429 -- Records if the routines RE_Str_Concat_CC/CS/SC are available. True if
4430 -- all three are available, False if any one of these is unavailable.
4431
4432 procedure Expand_N_Op_Concat (N : Node_Id) is
4433 Opnds : List_Id;
4434 -- List of operands to be concatenated
4435
4436 Opnd : Node_Id;
4437 -- Single operand for concatenation
4438
4439 Cnode : Node_Id;
4440 -- Node which is to be replaced by the result of concatenating
4441 -- the nodes in the list Opnds.
4442
4443 Atyp : Entity_Id;
4444 -- Array type of concatenation result type
4445
4446 Ctyp : Entity_Id;
4447 -- Component type of concatenation represented by Cnode
4448
4449 begin
4450 -- Initialize global variables showing run-time status
4451
4452 if Max_Available_String_Operands < 1 then
4453
4454 -- In No_Run_Time mode, consider that no entities are available
4455
4456 -- This seems wrong, RTE_Available should return False for any entity
4457 -- that is not in the special No_Run_Time list of allowed entities???
4458
4459 if No_Run_Time_Mode then
4460 Max_Available_String_Operands := 0;
4461
4462 -- Otherwise see what routines are available and set max operand
4463 -- count according to the highest count available in the run-time.
4464
4465 elsif not RTE_Available (RE_Str_Concat) then
4466 Max_Available_String_Operands := 0;
4467
4468 elsif not RTE_Available (RE_Str_Concat_3) then
4469 Max_Available_String_Operands := 2;
4470
4471 elsif not RTE_Available (RE_Str_Concat_4) then
4472 Max_Available_String_Operands := 3;
4473
4474 elsif not RTE_Available (RE_Str_Concat_5) then
4475 Max_Available_String_Operands := 4;
4476
4477 else
4478 Max_Available_String_Operands := 5;
4479 end if;
4480
4481 Char_Concat_Available :=
4482 not No_Run_Time_Mode
4483 and then
4484 RTE_Available (RE_Str_Concat_CC)
4485 and then
4486 RTE_Available (RE_Str_Concat_CS)
4487 and then
4488 RTE_Available (RE_Str_Concat_SC);
4489 end if;
4490
4491 -- Ensure validity of both operands
4492
4493 Binary_Op_Validity_Checks (N);
4494
4495 -- If we are the left operand of a concatenation higher up the
4496 -- tree, then do nothing for now, since we want to deal with a
4497 -- series of concatenations as a unit.
4498
4499 if Nkind (Parent (N)) = N_Op_Concat
4500 and then N = Left_Opnd (Parent (N))
4501 then
4502 return;
4503 end if;
4504
4505 -- We get here with a concatenation whose left operand may be a
4506 -- concatenation itself with a consistent type. We need to process
4507 -- these concatenation operands from left to right, which means
4508 -- from the deepest node in the tree to the highest node.
4509
4510 Cnode := N;
4511 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
4512 Cnode := Left_Opnd (Cnode);
4513 end loop;
4514
4515 -- Now Opnd is the deepest Opnd, and its parents are the concatenation
4516 -- nodes above, so now we process bottom up, doing the operations. We
4517 -- gather a string that is as long as possible up to five operands
4518
4519 -- The outer loop runs more than once if there are more than five
4520 -- concatenations of type Standard.String, the most we handle for
4521 -- this case, or if more than one concatenation type is involved.
4522
4523 Outer : loop
4524 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
4525 Set_Parent (Opnds, N);
4526
4527 -- The inner loop gathers concatenation operands. We gather any
4528 -- number of these in the non-string case, or if no concatenation
4529 -- routines are available for string (since in that case we will
4530 -- treat string like any other non-string case). Otherwise we only
4531 -- gather as many operands as can be handled by the available
4532 -- procedures in the run-time library (normally 5, but may be
4533 -- less for the configurable run-time case).
4534
4535 Inner : while Cnode /= N
4536 and then (Base_Type (Etype (Cnode)) /= Standard_String
4537 or else
4538 Max_Available_String_Operands = 0
4539 or else
4540 List_Length (Opnds) <
4541 Max_Available_String_Operands)
4542 and then Base_Type (Etype (Cnode)) =
4543 Base_Type (Etype (Parent (Cnode)))
4544 loop
4545 Cnode := Parent (Cnode);
4546 Append (Right_Opnd (Cnode), Opnds);
4547 end loop Inner;
4548
4549 -- Here we process the collected operands. First we convert
4550 -- singleton operands to singleton aggregates. This is skipped
4551 -- however for the case of two operands of type String, since
4552 -- we have special routines for these cases.
4553
4554 Atyp := Base_Type (Etype (Cnode));
4555 Ctyp := Base_Type (Component_Type (Etype (Cnode)));
4556
4557 if (List_Length (Opnds) > 2 or else Atyp /= Standard_String)
4558 or else not Char_Concat_Available
4559 then
4560 Opnd := First (Opnds);
4561 loop
4562 if Base_Type (Etype (Opnd)) = Ctyp then
4563 Rewrite (Opnd,
4564 Make_Aggregate (Sloc (Cnode),
4565 Expressions => New_List (Relocate_Node (Opnd))));
4566 Analyze_And_Resolve (Opnd, Atyp);
4567 end if;
4568
4569 Next (Opnd);
4570 exit when No (Opnd);
4571 end loop;
4572 end if;
4573
4574 -- Now call appropriate continuation routine
4575
4576 if Atyp = Standard_String
4577 and then Max_Available_String_Operands > 0
4578 then
4579 Expand_Concatenate_String (Cnode, Opnds);
4580 else
4581 Expand_Concatenate_Other (Cnode, Opnds);
4582 end if;
4583
4584 exit Outer when Cnode = N;
4585 Cnode := Parent (Cnode);
4586 end loop Outer;
4587 end Expand_N_Op_Concat;
4588
4589 ------------------------
4590 -- Expand_N_Op_Divide --
4591 ------------------------
4592
4593 procedure Expand_N_Op_Divide (N : Node_Id) is
4594 Loc : constant Source_Ptr := Sloc (N);
4595 Lopnd : constant Node_Id := Left_Opnd (N);
4596 Ropnd : constant Node_Id := Right_Opnd (N);
4597 Ltyp : constant Entity_Id := Etype (Lopnd);
4598 Rtyp : constant Entity_Id := Etype (Ropnd);
4599 Typ : Entity_Id := Etype (N);
4600 Rknow : constant Boolean := Is_Integer_Type (Typ)
4601 and then
4602 Compile_Time_Known_Value (Ropnd);
4603 Rval : Uint;
4604
4605 begin
4606 Binary_Op_Validity_Checks (N);
4607
4608 if Rknow then
4609 Rval := Expr_Value (Ropnd);
4610 end if;
4611
4612 -- N / 1 = N for integer types
4613
4614 if Rknow and then Rval = Uint_1 then
4615 Rewrite (N, Lopnd);
4616 return;
4617 end if;
4618
4619 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
4620 -- Is_Power_Of_2_For_Shift is set means that we know that our left
4621 -- operand is an unsigned integer, as required for this to work.
4622
4623 if Nkind (Ropnd) = N_Op_Expon
4624 and then Is_Power_Of_2_For_Shift (Ropnd)
4625
4626 -- We cannot do this transformation in configurable run time mode if we
4627 -- have 64-bit -- integers and long shifts are not available.
4628
4629 and then
4630 (Esize (Ltyp) <= 32
4631 or else Support_Long_Shifts_On_Target)
4632 then
4633 Rewrite (N,
4634 Make_Op_Shift_Right (Loc,
4635 Left_Opnd => Lopnd,
4636 Right_Opnd =>
4637 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
4638 Analyze_And_Resolve (N, Typ);
4639 return;
4640 end if;
4641
4642 -- Do required fixup of universal fixed operation
4643
4644 if Typ = Universal_Fixed then
4645 Fixup_Universal_Fixed_Operation (N);
4646 Typ := Etype (N);
4647 end if;
4648
4649 -- Divisions with fixed-point results
4650
4651 if Is_Fixed_Point_Type (Typ) then
4652
4653 -- No special processing if Treat_Fixed_As_Integer is set,
4654 -- since from a semantic point of view such operations are
4655 -- simply integer operations and will be treated that way.
4656
4657 if not Treat_Fixed_As_Integer (N) then
4658 if Is_Integer_Type (Rtyp) then
4659 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
4660 else
4661 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
4662 end if;
4663 end if;
4664
4665 -- Other cases of division of fixed-point operands. Again we
4666 -- exclude the case where Treat_Fixed_As_Integer is set.
4667
4668 elsif (Is_Fixed_Point_Type (Ltyp) or else
4669 Is_Fixed_Point_Type (Rtyp))
4670 and then not Treat_Fixed_As_Integer (N)
4671 then
4672 if Is_Integer_Type (Typ) then
4673 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
4674 else
4675 pragma Assert (Is_Floating_Point_Type (Typ));
4676 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
4677 end if;
4678
4679 -- Mixed-mode operations can appear in a non-static universal
4680 -- context, in which case the integer argument must be converted
4681 -- explicitly.
4682
4683 elsif Typ = Universal_Real
4684 and then Is_Integer_Type (Rtyp)
4685 then
4686 Rewrite (Ropnd,
4687 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
4688
4689 Analyze_And_Resolve (Ropnd, Universal_Real);
4690
4691 elsif Typ = Universal_Real
4692 and then Is_Integer_Type (Ltyp)
4693 then
4694 Rewrite (Lopnd,
4695 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
4696
4697 Analyze_And_Resolve (Lopnd, Universal_Real);
4698
4699 -- Non-fixed point cases, do integer zero divide and overflow checks
4700
4701 elsif Is_Integer_Type (Typ) then
4702 Apply_Divide_Check (N);
4703
4704 -- Check for 64-bit division available, or long shifts if the divisor
4705 -- is a small power of 2 (since such divides will be converted into
4706 -- long shifts.
4707
4708 if Esize (Ltyp) > 32
4709 and then not Support_64_Bit_Divides_On_Target
4710 and then
4711 (not Rknow
4712 or else not Support_Long_Shifts_On_Target
4713 or else (Rval /= Uint_2 and then
4714 Rval /= Uint_4 and then
4715 Rval /= Uint_8 and then
4716 Rval /= Uint_16 and then
4717 Rval /= Uint_32 and then
4718 Rval /= Uint_64))
4719 then
4720 Error_Msg_CRT ("64-bit division", N);
4721 end if;
4722
4723 -- Deal with Vax_Float
4724
4725 elsif Vax_Float (Typ) then
4726 Expand_Vax_Arith (N);
4727 return;
4728 end if;
4729 end Expand_N_Op_Divide;
4730
4731 --------------------
4732 -- Expand_N_Op_Eq --
4733 --------------------
4734
4735 procedure Expand_N_Op_Eq (N : Node_Id) is
4736 Loc : constant Source_Ptr := Sloc (N);
4737 Typ : constant Entity_Id := Etype (N);
4738 Lhs : constant Node_Id := Left_Opnd (N);
4739 Rhs : constant Node_Id := Right_Opnd (N);
4740 Bodies : constant List_Id := New_List;
4741 A_Typ : constant Entity_Id := Etype (Lhs);
4742
4743 Typl : Entity_Id := A_Typ;
4744 Op_Name : Entity_Id;
4745 Prim : Elmt_Id;
4746
4747 procedure Build_Equality_Call (Eq : Entity_Id);
4748 -- If a constructed equality exists for the type or for its parent,
4749 -- build and analyze call, adding conversions if the operation is
4750 -- inherited.
4751
4752 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
4753 -- Determines whether a type has a subcompoment of an unconstrained
4754 -- Unchecked_Union subtype. Typ is a record type.
4755
4756 -------------------------
4757 -- Build_Equality_Call --
4758 -------------------------
4759
4760 procedure Build_Equality_Call (Eq : Entity_Id) is
4761 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
4762 L_Exp : Node_Id := Relocate_Node (Lhs);
4763 R_Exp : Node_Id := Relocate_Node (Rhs);
4764
4765 begin
4766 if Base_Type (Op_Type) /= Base_Type (A_Typ)
4767 and then not Is_Class_Wide_Type (A_Typ)
4768 then
4769 L_Exp := OK_Convert_To (Op_Type, L_Exp);
4770 R_Exp := OK_Convert_To (Op_Type, R_Exp);
4771 end if;
4772
4773 -- If we have an Unchecked_Union, we need to add the inferred
4774 -- discriminant values as actuals in the function call. At this
4775 -- point, the expansion has determined that both operands have
4776 -- inferable discriminants.
4777
4778 if Is_Unchecked_Union (Op_Type) then
4779 declare
4780 Lhs_Type : constant Node_Id := Etype (L_Exp);
4781 Rhs_Type : constant Node_Id := Etype (R_Exp);
4782 Lhs_Discr_Val : Node_Id;
4783 Rhs_Discr_Val : Node_Id;
4784
4785 begin
4786 -- Per-object constrained selected components require special
4787 -- attention. If the enclosing scope of the component is an
4788 -- Unchecked_Union, we cannot reference its discriminants
4789 -- directly. This is why we use the two extra parameters of
4790 -- the equality function of the enclosing Unchecked_Union.
4791
4792 -- type UU_Type (Discr : Integer := 0) is
4793 -- . . .
4794 -- end record;
4795 -- pragma Unchecked_Union (UU_Type);
4796
4797 -- 1. Unchecked_Union enclosing record:
4798
4799 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
4800 -- . . .
4801 -- Comp : UU_Type (Discr);
4802 -- . . .
4803 -- end Enclosing_UU_Type;
4804 -- pragma Unchecked_Union (Enclosing_UU_Type);
4805
4806 -- Obj1 : Enclosing_UU_Type;
4807 -- Obj2 : Enclosing_UU_Type (1);
4808
4809 -- [. . .] Obj1 = Obj2 [. . .]
4810
4811 -- Generated code:
4812
4813 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
4814
4815 -- A and B are the formal parameters of the equality function
4816 -- of Enclosing_UU_Type. The function always has two extra
4817 -- formals to capture the inferred discriminant values.
4818
4819 -- 2. Non-Unchecked_Union enclosing record:
4820
4821 -- type
4822 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
4823 -- is record
4824 -- . . .
4825 -- Comp : UU_Type (Discr);
4826 -- . . .
4827 -- end Enclosing_Non_UU_Type;
4828
4829 -- Obj1 : Enclosing_Non_UU_Type;
4830 -- Obj2 : Enclosing_Non_UU_Type (1);
4831
4832 -- ... Obj1 = Obj2 ...
4833
4834 -- Generated code:
4835
4836 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
4837 -- obj1.discr, obj2.discr)) then
4838
4839 -- In this case we can directly reference the discriminants of
4840 -- the enclosing record.
4841
4842 -- Lhs of equality
4843
4844 if Nkind (Lhs) = N_Selected_Component
4845 and then Has_Per_Object_Constraint
4846 (Entity (Selector_Name (Lhs)))
4847 then
4848 -- Enclosing record is an Unchecked_Union, use formal A
4849
4850 if Is_Unchecked_Union (Scope
4851 (Entity (Selector_Name (Lhs))))
4852 then
4853 Lhs_Discr_Val :=
4854 Make_Identifier (Loc,
4855 Chars => Name_A);
4856
4857 -- Enclosing record is of a non-Unchecked_Union type, it is
4858 -- possible to reference the discriminant.
4859
4860 else
4861 Lhs_Discr_Val :=
4862 Make_Selected_Component (Loc,
4863 Prefix => Prefix (Lhs),
4864 Selector_Name =>
4865 New_Copy
4866 (Get_Discriminant_Value
4867 (First_Discriminant (Lhs_Type),
4868 Lhs_Type,
4869 Stored_Constraint (Lhs_Type))));
4870 end if;
4871
4872 -- Comment needed here ???
4873
4874 else
4875 -- Infer the discriminant value
4876
4877 Lhs_Discr_Val :=
4878 New_Copy
4879 (Get_Discriminant_Value
4880 (First_Discriminant (Lhs_Type),
4881 Lhs_Type,
4882 Stored_Constraint (Lhs_Type)));
4883 end if;
4884
4885 -- Rhs of equality
4886
4887 if Nkind (Rhs) = N_Selected_Component
4888 and then Has_Per_Object_Constraint
4889 (Entity (Selector_Name (Rhs)))
4890 then
4891 if Is_Unchecked_Union
4892 (Scope (Entity (Selector_Name (Rhs))))
4893 then
4894 Rhs_Discr_Val :=
4895 Make_Identifier (Loc,
4896 Chars => Name_B);
4897
4898 else
4899 Rhs_Discr_Val :=
4900 Make_Selected_Component (Loc,
4901 Prefix => Prefix (Rhs),
4902 Selector_Name =>
4903 New_Copy (Get_Discriminant_Value (
4904 First_Discriminant (Rhs_Type),
4905 Rhs_Type,
4906 Stored_Constraint (Rhs_Type))));
4907
4908 end if;
4909 else
4910 Rhs_Discr_Val :=
4911 New_Copy (Get_Discriminant_Value (
4912 First_Discriminant (Rhs_Type),
4913 Rhs_Type,
4914 Stored_Constraint (Rhs_Type)));
4915
4916 end if;
4917
4918 Rewrite (N,
4919 Make_Function_Call (Loc,
4920 Name => New_Reference_To (Eq, Loc),
4921 Parameter_Associations => New_List (
4922 L_Exp,
4923 R_Exp,
4924 Lhs_Discr_Val,
4925 Rhs_Discr_Val)));
4926 end;
4927
4928 -- Normal case, not an unchecked union
4929
4930 else
4931 Rewrite (N,
4932 Make_Function_Call (Loc,
4933 Name => New_Reference_To (Eq, Loc),
4934 Parameter_Associations => New_List (L_Exp, R_Exp)));
4935 end if;
4936
4937 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
4938 end Build_Equality_Call;
4939
4940 ------------------------------------
4941 -- Has_Unconstrained_UU_Component --
4942 ------------------------------------
4943
4944 function Has_Unconstrained_UU_Component
4945 (Typ : Node_Id) return Boolean
4946 is
4947 Tdef : constant Node_Id :=
4948 Type_Definition (Declaration_Node (Base_Type (Typ)));
4949 Clist : Node_Id;
4950 Vpart : Node_Id;
4951
4952 function Component_Is_Unconstrained_UU
4953 (Comp : Node_Id) return Boolean;
4954 -- Determines whether the subtype of the component is an
4955 -- unconstrained Unchecked_Union.
4956
4957 function Variant_Is_Unconstrained_UU
4958 (Variant : Node_Id) return Boolean;
4959 -- Determines whether a component of the variant has an unconstrained
4960 -- Unchecked_Union subtype.
4961
4962 -----------------------------------
4963 -- Component_Is_Unconstrained_UU --
4964 -----------------------------------
4965
4966 function Component_Is_Unconstrained_UU
4967 (Comp : Node_Id) return Boolean
4968 is
4969 begin
4970 if Nkind (Comp) /= N_Component_Declaration then
4971 return False;
4972 end if;
4973
4974 declare
4975 Sindic : constant Node_Id :=
4976 Subtype_Indication (Component_Definition (Comp));
4977
4978 begin
4979 -- Unconstrained nominal type. In the case of a constraint
4980 -- present, the node kind would have been N_Subtype_Indication.
4981
4982 if Nkind (Sindic) = N_Identifier then
4983 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
4984 end if;
4985
4986 return False;
4987 end;
4988 end Component_Is_Unconstrained_UU;
4989
4990 ---------------------------------
4991 -- Variant_Is_Unconstrained_UU --
4992 ---------------------------------
4993
4994 function Variant_Is_Unconstrained_UU
4995 (Variant : Node_Id) return Boolean
4996 is
4997 Clist : constant Node_Id := Component_List (Variant);
4998
4999 begin
5000 if Is_Empty_List (Component_Items (Clist)) then
5001 return False;
5002 end if;
5003
5004 -- We only need to test one component
5005
5006 declare
5007 Comp : Node_Id := First (Component_Items (Clist));
5008
5009 begin
5010 while Present (Comp) loop
5011 if Component_Is_Unconstrained_UU (Comp) then
5012 return True;
5013 end if;
5014
5015 Next (Comp);
5016 end loop;
5017 end;
5018
5019 -- None of the components withing the variant were of
5020 -- unconstrained Unchecked_Union type.
5021
5022 return False;
5023 end Variant_Is_Unconstrained_UU;
5024
5025 -- Start of processing for Has_Unconstrained_UU_Component
5026
5027 begin
5028 if Null_Present (Tdef) then
5029 return False;
5030 end if;
5031
5032 Clist := Component_List (Tdef);
5033 Vpart := Variant_Part (Clist);
5034
5035 -- Inspect available components
5036
5037 if Present (Component_Items (Clist)) then
5038 declare
5039 Comp : Node_Id := First (Component_Items (Clist));
5040
5041 begin
5042 while Present (Comp) loop
5043
5044 -- One component is sufficent
5045
5046 if Component_Is_Unconstrained_UU (Comp) then
5047 return True;
5048 end if;
5049
5050 Next (Comp);
5051 end loop;
5052 end;
5053 end if;
5054
5055 -- Inspect available components withing variants
5056
5057 if Present (Vpart) then
5058 declare
5059 Variant : Node_Id := First (Variants (Vpart));
5060
5061 begin
5062 while Present (Variant) loop
5063
5064 -- One component within a variant is sufficent
5065
5066 if Variant_Is_Unconstrained_UU (Variant) then
5067 return True;
5068 end if;
5069
5070 Next (Variant);
5071 end loop;
5072 end;
5073 end if;
5074
5075 -- Neither the available components, nor the components inside the
5076 -- variant parts were of an unconstrained Unchecked_Union subtype.
5077
5078 return False;
5079 end Has_Unconstrained_UU_Component;
5080
5081 -- Start of processing for Expand_N_Op_Eq
5082
5083 begin
5084 Binary_Op_Validity_Checks (N);
5085
5086 if Ekind (Typl) = E_Private_Type then
5087 Typl := Underlying_Type (Typl);
5088 elsif Ekind (Typl) = E_Private_Subtype then
5089 Typl := Underlying_Type (Base_Type (Typl));
5090 else
5091 null;
5092 end if;
5093
5094 -- It may happen in error situations that the underlying type is not
5095 -- set. The error will be detected later, here we just defend the
5096 -- expander code.
5097
5098 if No (Typl) then
5099 return;
5100 end if;
5101
5102 Typl := Base_Type (Typl);
5103
5104 -- Boolean types (requiring handling of non-standard case)
5105
5106 if Is_Boolean_Type (Typl) then
5107 Adjust_Condition (Left_Opnd (N));
5108 Adjust_Condition (Right_Opnd (N));
5109 Set_Etype (N, Standard_Boolean);
5110 Adjust_Result_Type (N, Typ);
5111
5112 -- Array types
5113
5114 elsif Is_Array_Type (Typl) then
5115
5116 -- If we are doing full validity checking, and it is possible for the
5117 -- array elements to be invalid then expand out array comparisons to
5118 -- make sure that we check the array elements.
5119
5120 if Validity_Check_Operands
5121 and then not Is_Known_Valid (Component_Type (Typl))
5122 then
5123 declare
5124 Save_Force_Validity_Checks : constant Boolean :=
5125 Force_Validity_Checks;
5126 begin
5127 Force_Validity_Checks := True;
5128 Rewrite (N,
5129 Expand_Array_Equality
5130 (N,
5131 Relocate_Node (Lhs),
5132 Relocate_Node (Rhs),
5133 Bodies,
5134 Typl));
5135 Insert_Actions (N, Bodies);
5136 Analyze_And_Resolve (N, Standard_Boolean);
5137 Force_Validity_Checks := Save_Force_Validity_Checks;
5138 end;
5139
5140 -- Packed case where both operands are known aligned
5141
5142 elsif Is_Bit_Packed_Array (Typl)
5143 and then not Is_Possibly_Unaligned_Object (Lhs)
5144 and then not Is_Possibly_Unaligned_Object (Rhs)
5145 then
5146 Expand_Packed_Eq (N);
5147
5148 -- Where the component type is elementary we can use a block bit
5149 -- comparison (if supported on the target) exception in the case
5150 -- of floating-point (negative zero issues require element by
5151 -- element comparison), and atomic types (where we must be sure
5152 -- to load elements independently) and possibly unaligned arrays.
5153
5154 elsif Is_Elementary_Type (Component_Type (Typl))
5155 and then not Is_Floating_Point_Type (Component_Type (Typl))
5156 and then not Is_Atomic (Component_Type (Typl))
5157 and then not Is_Possibly_Unaligned_Object (Lhs)
5158 and then not Is_Possibly_Unaligned_Object (Rhs)
5159 and then Support_Composite_Compare_On_Target
5160 then
5161 null;
5162
5163 -- For composite and floating-point cases, expand equality loop
5164 -- to make sure of using proper comparisons for tagged types,
5165 -- and correctly handling the floating-point case.
5166
5167 else
5168 Rewrite (N,
5169 Expand_Array_Equality
5170 (N,
5171 Relocate_Node (Lhs),
5172 Relocate_Node (Rhs),
5173 Bodies,
5174 Typl));
5175 Insert_Actions (N, Bodies, Suppress => All_Checks);
5176 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5177 end if;
5178
5179 -- Record Types
5180
5181 elsif Is_Record_Type (Typl) then
5182
5183 -- For tagged types, use the primitive "="
5184
5185 if Is_Tagged_Type (Typl) then
5186
5187 -- No need to do anything else compiling under restriction
5188 -- No_Dispatching_Calls. During the semantic analysis we
5189 -- already notified such violation.
5190
5191 if Restriction_Active (No_Dispatching_Calls) then
5192 return;
5193 end if;
5194
5195 -- If this is derived from an untagged private type completed
5196 -- with a tagged type, it does not have a full view, so we
5197 -- use the primitive operations of the private type.
5198 -- This check should no longer be necessary when these
5199 -- types receive their full views ???
5200
5201 if Is_Private_Type (A_Typ)
5202 and then not Is_Tagged_Type (A_Typ)
5203 and then Is_Derived_Type (A_Typ)
5204 and then No (Full_View (A_Typ))
5205 then
5206 -- Search for equality operation, checking that the
5207 -- operands have the same type. Note that we must find
5208 -- a matching entry, or something is very wrong!
5209
5210 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
5211
5212 while Present (Prim) loop
5213 exit when Chars (Node (Prim)) = Name_Op_Eq
5214 and then Etype (First_Formal (Node (Prim))) =
5215 Etype (Next_Formal (First_Formal (Node (Prim))))
5216 and then
5217 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5218
5219 Next_Elmt (Prim);
5220 end loop;
5221
5222 pragma Assert (Present (Prim));
5223 Op_Name := Node (Prim);
5224
5225 -- Find the type's predefined equality or an overriding
5226 -- user-defined equality. The reason for not simply calling
5227 -- Find_Prim_Op here is that there may be a user-defined
5228 -- overloaded equality op that precedes the equality that
5229 -- we want, so we have to explicitly search (e.g., there
5230 -- could be an equality with two different parameter types).
5231
5232 else
5233 if Is_Class_Wide_Type (Typl) then
5234 Typl := Root_Type (Typl);
5235 end if;
5236
5237 Prim := First_Elmt (Primitive_Operations (Typl));
5238 while Present (Prim) loop
5239 exit when Chars (Node (Prim)) = Name_Op_Eq
5240 and then Etype (First_Formal (Node (Prim))) =
5241 Etype (Next_Formal (First_Formal (Node (Prim))))
5242 and then
5243 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
5244
5245 Next_Elmt (Prim);
5246 end loop;
5247
5248 pragma Assert (Present (Prim));
5249 Op_Name := Node (Prim);
5250 end if;
5251
5252 Build_Equality_Call (Op_Name);
5253
5254 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
5255 -- predefined equality operator for a type which has a subcomponent
5256 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
5257
5258 elsif Has_Unconstrained_UU_Component (Typl) then
5259 Insert_Action (N,
5260 Make_Raise_Program_Error (Loc,
5261 Reason => PE_Unchecked_Union_Restriction));
5262
5263 -- Prevent Gigi from generating incorrect code by rewriting the
5264 -- equality as a standard False.
5265
5266 Rewrite (N,
5267 New_Occurrence_Of (Standard_False, Loc));
5268
5269 elsif Is_Unchecked_Union (Typl) then
5270
5271 -- If we can infer the discriminants of the operands, we make a
5272 -- call to the TSS equality function.
5273
5274 if Has_Inferable_Discriminants (Lhs)
5275 and then
5276 Has_Inferable_Discriminants (Rhs)
5277 then
5278 Build_Equality_Call
5279 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5280
5281 else
5282 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5283 -- the predefined equality operator for an Unchecked_Union type
5284 -- if either of the operands lack inferable discriminants.
5285
5286 Insert_Action (N,
5287 Make_Raise_Program_Error (Loc,
5288 Reason => PE_Unchecked_Union_Restriction));
5289
5290 -- Prevent Gigi from generating incorrect code by rewriting
5291 -- the equality as a standard False.
5292
5293 Rewrite (N,
5294 New_Occurrence_Of (Standard_False, Loc));
5295
5296 end if;
5297
5298 -- If a type support function is present (for complex cases), use it
5299
5300 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
5301 Build_Equality_Call
5302 (TSS (Root_Type (Typl), TSS_Composite_Equality));
5303
5304 -- Otherwise expand the component by component equality. Note that
5305 -- we never use block-bit coparisons for records, because of the
5306 -- problems with gaps. The backend will often be able to recombine
5307 -- the separate comparisons that we generate here.
5308
5309 else
5310 Remove_Side_Effects (Lhs);
5311 Remove_Side_Effects (Rhs);
5312 Rewrite (N,
5313 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
5314
5315 Insert_Actions (N, Bodies, Suppress => All_Checks);
5316 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
5317 end if;
5318 end if;
5319
5320 -- Test if result is known at compile time
5321
5322 Rewrite_Comparison (N);
5323
5324 -- If we still have comparison for Vax_Float, process it
5325
5326 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
5327 Expand_Vax_Comparison (N);
5328 return;
5329 end if;
5330 end Expand_N_Op_Eq;
5331
5332 -----------------------
5333 -- Expand_N_Op_Expon --
5334 -----------------------
5335
5336 procedure Expand_N_Op_Expon (N : Node_Id) is
5337 Loc : constant Source_Ptr := Sloc (N);
5338 Typ : constant Entity_Id := Etype (N);
5339 Rtyp : constant Entity_Id := Root_Type (Typ);
5340 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
5341 Bastyp : constant Node_Id := Etype (Base);
5342 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
5343 Exptyp : constant Entity_Id := Etype (Exp);
5344 Ovflo : constant Boolean := Do_Overflow_Check (N);
5345 Expv : Uint;
5346 Xnode : Node_Id;
5347 Temp : Node_Id;
5348 Rent : RE_Id;
5349 Ent : Entity_Id;
5350 Etyp : Entity_Id;
5351
5352 begin
5353 Binary_Op_Validity_Checks (N);
5354
5355 -- If either operand is of a private type, then we have the use of
5356 -- an intrinsic operator, and we get rid of the privateness, by using
5357 -- root types of underlying types for the actual operation. Otherwise
5358 -- the private types will cause trouble if we expand multiplications
5359 -- or shifts etc. We also do this transformation if the result type
5360 -- is different from the base type.
5361
5362 if Is_Private_Type (Etype (Base))
5363 or else
5364 Is_Private_Type (Typ)
5365 or else
5366 Is_Private_Type (Exptyp)
5367 or else
5368 Rtyp /= Root_Type (Bastyp)
5369 then
5370 declare
5371 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
5372 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
5373
5374 begin
5375 Rewrite (N,
5376 Unchecked_Convert_To (Typ,
5377 Make_Op_Expon (Loc,
5378 Left_Opnd => Unchecked_Convert_To (Bt, Base),
5379 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
5380 Analyze_And_Resolve (N, Typ);
5381 return;
5382 end;
5383 end if;
5384
5385 -- Test for case of known right argument
5386
5387 if Compile_Time_Known_Value (Exp) then
5388 Expv := Expr_Value (Exp);
5389
5390 -- We only fold small non-negative exponents. You might think we
5391 -- could fold small negative exponents for the real case, but we
5392 -- can't because we are required to raise Constraint_Error for
5393 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
5394 -- See ACVC test C4A012B.
5395
5396 if Expv >= 0 and then Expv <= 4 then
5397
5398 -- X ** 0 = 1 (or 1.0)
5399
5400 if Expv = 0 then
5401 if Ekind (Typ) in Integer_Kind then
5402 Xnode := Make_Integer_Literal (Loc, Intval => 1);
5403 else
5404 Xnode := Make_Real_Literal (Loc, Ureal_1);
5405 end if;
5406
5407 -- X ** 1 = X
5408
5409 elsif Expv = 1 then
5410 Xnode := Base;
5411
5412 -- X ** 2 = X * X
5413
5414 elsif Expv = 2 then
5415 Xnode :=
5416 Make_Op_Multiply (Loc,
5417 Left_Opnd => Duplicate_Subexpr (Base),
5418 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
5419
5420 -- X ** 3 = X * X * X
5421
5422 elsif Expv = 3 then
5423 Xnode :=
5424 Make_Op_Multiply (Loc,
5425 Left_Opnd =>
5426 Make_Op_Multiply (Loc,
5427 Left_Opnd => Duplicate_Subexpr (Base),
5428 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
5429 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
5430
5431 -- X ** 4 ->
5432 -- En : constant base'type := base * base;
5433 -- ...
5434 -- En * En
5435
5436 else -- Expv = 4
5437 Temp :=
5438 Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
5439
5440 Insert_Actions (N, New_List (
5441 Make_Object_Declaration (Loc,
5442 Defining_Identifier => Temp,
5443 Constant_Present => True,
5444 Object_Definition => New_Reference_To (Typ, Loc),
5445 Expression =>
5446 Make_Op_Multiply (Loc,
5447 Left_Opnd => Duplicate_Subexpr (Base),
5448 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
5449
5450 Xnode :=
5451 Make_Op_Multiply (Loc,
5452 Left_Opnd => New_Reference_To (Temp, Loc),
5453 Right_Opnd => New_Reference_To (Temp, Loc));
5454 end if;
5455
5456 Rewrite (N, Xnode);
5457 Analyze_And_Resolve (N, Typ);
5458 return;
5459 end if;
5460 end if;
5461
5462 -- Case of (2 ** expression) appearing as an argument of an integer
5463 -- multiplication, or as the right argument of a division of a non-
5464 -- negative integer. In such cases we leave the node untouched, setting
5465 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
5466 -- of the higher level node converts it into a shift.
5467
5468 if Nkind (Base) = N_Integer_Literal
5469 and then Intval (Base) = 2
5470 and then Is_Integer_Type (Root_Type (Exptyp))
5471 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
5472 and then Is_Unsigned_Type (Exptyp)
5473 and then not Ovflo
5474 and then Nkind (Parent (N)) in N_Binary_Op
5475 then
5476 declare
5477 P : constant Node_Id := Parent (N);
5478 L : constant Node_Id := Left_Opnd (P);
5479 R : constant Node_Id := Right_Opnd (P);
5480
5481 begin
5482 if (Nkind (P) = N_Op_Multiply
5483 and then
5484 ((Is_Integer_Type (Etype (L)) and then R = N)
5485 or else
5486 (Is_Integer_Type (Etype (R)) and then L = N))
5487 and then not Do_Overflow_Check (P))
5488
5489 or else
5490 (Nkind (P) = N_Op_Divide
5491 and then Is_Integer_Type (Etype (L))
5492 and then Is_Unsigned_Type (Etype (L))
5493 and then R = N
5494 and then not Do_Overflow_Check (P))
5495 then
5496 Set_Is_Power_Of_2_For_Shift (N);
5497 return;
5498 end if;
5499 end;
5500 end if;
5501
5502 -- Fall through if exponentiation must be done using a runtime routine
5503
5504 -- First deal with modular case
5505
5506 if Is_Modular_Integer_Type (Rtyp) then
5507
5508 -- Non-binary case, we call the special exponentiation routine for
5509 -- the non-binary case, converting the argument to Long_Long_Integer
5510 -- and passing the modulus value. Then the result is converted back
5511 -- to the base type.
5512
5513 if Non_Binary_Modulus (Rtyp) then
5514 Rewrite (N,
5515 Convert_To (Typ,
5516 Make_Function_Call (Loc,
5517 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
5518 Parameter_Associations => New_List (
5519 Convert_To (Standard_Integer, Base),
5520 Make_Integer_Literal (Loc, Modulus (Rtyp)),
5521 Exp))));
5522
5523 -- Binary case, in this case, we call one of two routines, either
5524 -- the unsigned integer case, or the unsigned long long integer
5525 -- case, with a final "and" operation to do the required mod.
5526
5527 else
5528 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
5529 Ent := RTE (RE_Exp_Unsigned);
5530 else
5531 Ent := RTE (RE_Exp_Long_Long_Unsigned);
5532 end if;
5533
5534 Rewrite (N,
5535 Convert_To (Typ,
5536 Make_Op_And (Loc,
5537 Left_Opnd =>
5538 Make_Function_Call (Loc,
5539 Name => New_Reference_To (Ent, Loc),
5540 Parameter_Associations => New_List (
5541 Convert_To (Etype (First_Formal (Ent)), Base),
5542 Exp)),
5543 Right_Opnd =>
5544 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
5545
5546 end if;
5547
5548 -- Common exit point for modular type case
5549
5550 Analyze_And_Resolve (N, Typ);
5551 return;
5552
5553 -- Signed integer cases, done using either Integer or Long_Long_Integer.
5554 -- It is not worth having routines for Short_[Short_]Integer, since for
5555 -- most machines it would not help, and it would generate more code that
5556 -- might need certification when a certified run time is required.
5557
5558 -- In the integer cases, we have two routines, one for when overflow
5559 -- checks are required, and one when they are not required, since there
5560 -- is a real gain in omitting checks on many machines.
5561
5562 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
5563 or else (Rtyp = Base_Type (Standard_Long_Integer)
5564 and then
5565 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
5566 or else (Rtyp = Universal_Integer)
5567 then
5568 Etyp := Standard_Long_Long_Integer;
5569
5570 if Ovflo then
5571 Rent := RE_Exp_Long_Long_Integer;
5572 else
5573 Rent := RE_Exn_Long_Long_Integer;
5574 end if;
5575
5576 elsif Is_Signed_Integer_Type (Rtyp) then
5577 Etyp := Standard_Integer;
5578
5579 if Ovflo then
5580 Rent := RE_Exp_Integer;
5581 else
5582 Rent := RE_Exn_Integer;
5583 end if;
5584
5585 -- Floating-point cases, always done using Long_Long_Float. We do not
5586 -- need separate routines for the overflow case here, since in the case
5587 -- of floating-point, we generate infinities anyway as a rule (either
5588 -- that or we automatically trap overflow), and if there is an infinity
5589 -- generated and a range check is required, the check will fail anyway.
5590
5591 else
5592 pragma Assert (Is_Floating_Point_Type (Rtyp));
5593 Etyp := Standard_Long_Long_Float;
5594 Rent := RE_Exn_Long_Long_Float;
5595 end if;
5596
5597 -- Common processing for integer cases and floating-point cases.
5598 -- If we are in the right type, we can call runtime routine directly
5599
5600 if Typ = Etyp
5601 and then Rtyp /= Universal_Integer
5602 and then Rtyp /= Universal_Real
5603 then
5604 Rewrite (N,
5605 Make_Function_Call (Loc,
5606 Name => New_Reference_To (RTE (Rent), Loc),
5607 Parameter_Associations => New_List (Base, Exp)));
5608
5609 -- Otherwise we have to introduce conversions (conversions are also
5610 -- required in the universal cases, since the runtime routine is
5611 -- typed using one of the standard types.
5612
5613 else
5614 Rewrite (N,
5615 Convert_To (Typ,
5616 Make_Function_Call (Loc,
5617 Name => New_Reference_To (RTE (Rent), Loc),
5618 Parameter_Associations => New_List (
5619 Convert_To (Etyp, Base),
5620 Exp))));
5621 end if;
5622
5623 Analyze_And_Resolve (N, Typ);
5624 return;
5625
5626 exception
5627 when RE_Not_Available =>
5628 return;
5629 end Expand_N_Op_Expon;
5630
5631 --------------------
5632 -- Expand_N_Op_Ge --
5633 --------------------
5634
5635 procedure Expand_N_Op_Ge (N : Node_Id) is
5636 Typ : constant Entity_Id := Etype (N);
5637 Op1 : constant Node_Id := Left_Opnd (N);
5638 Op2 : constant Node_Id := Right_Opnd (N);
5639 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
5640
5641 begin
5642 Binary_Op_Validity_Checks (N);
5643
5644 if Is_Array_Type (Typ1) then
5645 Expand_Array_Comparison (N);
5646 return;
5647 end if;
5648
5649 if Is_Boolean_Type (Typ1) then
5650 Adjust_Condition (Op1);
5651 Adjust_Condition (Op2);
5652 Set_Etype (N, Standard_Boolean);
5653 Adjust_Result_Type (N, Typ);
5654 end if;
5655
5656 Rewrite_Comparison (N);
5657
5658 -- If we still have comparison, and Vax_Float type, process it
5659
5660 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
5661 Expand_Vax_Comparison (N);
5662 return;
5663 end if;
5664 end Expand_N_Op_Ge;
5665
5666 --------------------
5667 -- Expand_N_Op_Gt --
5668 --------------------
5669
5670 procedure Expand_N_Op_Gt (N : Node_Id) is
5671 Typ : constant Entity_Id := Etype (N);
5672 Op1 : constant Node_Id := Left_Opnd (N);
5673 Op2 : constant Node_Id := Right_Opnd (N);
5674 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
5675
5676 begin
5677 Binary_Op_Validity_Checks (N);
5678
5679 if Is_Array_Type (Typ1) then
5680 Expand_Array_Comparison (N);
5681 return;
5682 end if;
5683
5684 if Is_Boolean_Type (Typ1) then
5685 Adjust_Condition (Op1);
5686 Adjust_Condition (Op2);
5687 Set_Etype (N, Standard_Boolean);
5688 Adjust_Result_Type (N, Typ);
5689 end if;
5690
5691 Rewrite_Comparison (N);
5692
5693 -- If we still have comparison, and Vax_Float type, process it
5694
5695 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
5696 Expand_Vax_Comparison (N);
5697 return;
5698 end if;
5699 end Expand_N_Op_Gt;
5700
5701 --------------------
5702 -- Expand_N_Op_Le --
5703 --------------------
5704
5705 procedure Expand_N_Op_Le (N : Node_Id) is
5706 Typ : constant Entity_Id := Etype (N);
5707 Op1 : constant Node_Id := Left_Opnd (N);
5708 Op2 : constant Node_Id := Right_Opnd (N);
5709 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
5710
5711 begin
5712 Binary_Op_Validity_Checks (N);
5713
5714 if Is_Array_Type (Typ1) then
5715 Expand_Array_Comparison (N);
5716 return;
5717 end if;
5718
5719 if Is_Boolean_Type (Typ1) then
5720 Adjust_Condition (Op1);
5721 Adjust_Condition (Op2);
5722 Set_Etype (N, Standard_Boolean);
5723 Adjust_Result_Type (N, Typ);
5724 end if;
5725
5726 Rewrite_Comparison (N);
5727
5728 -- If we still have comparison, and Vax_Float type, process it
5729
5730 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
5731 Expand_Vax_Comparison (N);
5732 return;
5733 end if;
5734 end Expand_N_Op_Le;
5735
5736 --------------------
5737 -- Expand_N_Op_Lt --
5738 --------------------
5739
5740 procedure Expand_N_Op_Lt (N : Node_Id) is
5741 Typ : constant Entity_Id := Etype (N);
5742 Op1 : constant Node_Id := Left_Opnd (N);
5743 Op2 : constant Node_Id := Right_Opnd (N);
5744 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
5745
5746 begin
5747 Binary_Op_Validity_Checks (N);
5748
5749 if Is_Array_Type (Typ1) then
5750 Expand_Array_Comparison (N);
5751 return;
5752 end if;
5753
5754 if Is_Boolean_Type (Typ1) then
5755 Adjust_Condition (Op1);
5756 Adjust_Condition (Op2);
5757 Set_Etype (N, Standard_Boolean);
5758 Adjust_Result_Type (N, Typ);
5759 end if;
5760
5761 Rewrite_Comparison (N);
5762
5763 -- If we still have comparison, and Vax_Float type, process it
5764
5765 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
5766 Expand_Vax_Comparison (N);
5767 return;
5768 end if;
5769 end Expand_N_Op_Lt;
5770
5771 -----------------------
5772 -- Expand_N_Op_Minus --
5773 -----------------------
5774
5775 procedure Expand_N_Op_Minus (N : Node_Id) is
5776 Loc : constant Source_Ptr := Sloc (N);
5777 Typ : constant Entity_Id := Etype (N);
5778
5779 begin
5780 Unary_Op_Validity_Checks (N);
5781
5782 if not Backend_Overflow_Checks_On_Target
5783 and then Is_Signed_Integer_Type (Etype (N))
5784 and then Do_Overflow_Check (N)
5785 then
5786 -- Software overflow checking expands -expr into (0 - expr)
5787
5788 Rewrite (N,
5789 Make_Op_Subtract (Loc,
5790 Left_Opnd => Make_Integer_Literal (Loc, 0),
5791 Right_Opnd => Right_Opnd (N)));
5792
5793 Analyze_And_Resolve (N, Typ);
5794
5795 -- Vax floating-point types case
5796
5797 elsif Vax_Float (Etype (N)) then
5798 Expand_Vax_Arith (N);
5799 end if;
5800 end Expand_N_Op_Minus;
5801
5802 ---------------------
5803 -- Expand_N_Op_Mod --
5804 ---------------------
5805
5806 procedure Expand_N_Op_Mod (N : Node_Id) is
5807 Loc : constant Source_Ptr := Sloc (N);
5808 Typ : constant Entity_Id := Etype (N);
5809 Left : constant Node_Id := Left_Opnd (N);
5810 Right : constant Node_Id := Right_Opnd (N);
5811 DOC : constant Boolean := Do_Overflow_Check (N);
5812 DDC : constant Boolean := Do_Division_Check (N);
5813
5814 LLB : Uint;
5815 Llo : Uint;
5816 Lhi : Uint;
5817 LOK : Boolean;
5818 Rlo : Uint;
5819 Rhi : Uint;
5820 ROK : Boolean;
5821
5822 pragma Warnings (Off, Lhi);
5823
5824 begin
5825 Binary_Op_Validity_Checks (N);
5826
5827 Determine_Range (Right, ROK, Rlo, Rhi);
5828 Determine_Range (Left, LOK, Llo, Lhi);
5829
5830 -- Convert mod to rem if operands are known non-negative. We do this
5831 -- since it is quite likely that this will improve the quality of code,
5832 -- (the operation now corresponds to the hardware remainder), and it
5833 -- does not seem likely that it could be harmful.
5834
5835 if LOK and then Llo >= 0
5836 and then
5837 ROK and then Rlo >= 0
5838 then
5839 Rewrite (N,
5840 Make_Op_Rem (Sloc (N),
5841 Left_Opnd => Left_Opnd (N),
5842 Right_Opnd => Right_Opnd (N)));
5843
5844 -- Instead of reanalyzing the node we do the analysis manually.
5845 -- This avoids anomalies when the replacement is done in an
5846 -- instance and is epsilon more efficient.
5847
5848 Set_Entity (N, Standard_Entity (S_Op_Rem));
5849 Set_Etype (N, Typ);
5850 Set_Do_Overflow_Check (N, DOC);
5851 Set_Do_Division_Check (N, DDC);
5852 Expand_N_Op_Rem (N);
5853 Set_Analyzed (N);
5854
5855 -- Otherwise, normal mod processing
5856
5857 else
5858 if Is_Integer_Type (Etype (N)) then
5859 Apply_Divide_Check (N);
5860 end if;
5861
5862 -- Apply optimization x mod 1 = 0. We don't really need that with
5863 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
5864 -- certainly harmless.
5865
5866 if Is_Integer_Type (Etype (N))
5867 and then Compile_Time_Known_Value (Right)
5868 and then Expr_Value (Right) = Uint_1
5869 then
5870 Rewrite (N, Make_Integer_Literal (Loc, 0));
5871 Analyze_And_Resolve (N, Typ);
5872 return;
5873 end if;
5874
5875 -- Deal with annoying case of largest negative number remainder
5876 -- minus one. Gigi does not handle this case correctly, because
5877 -- it generates a divide instruction which may trap in this case.
5878
5879 -- In fact the check is quite easy, if the right operand is -1,
5880 -- then the mod value is always 0, and we can just ignore the
5881 -- left operand completely in this case.
5882
5883 -- The operand type may be private (e.g. in the expansion of an
5884 -- an intrinsic operation) so we must use the underlying type to
5885 -- get the bounds, and convert the literals explicitly.
5886
5887 LLB :=
5888 Expr_Value
5889 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
5890
5891 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
5892 and then
5893 ((not LOK) or else (Llo = LLB))
5894 then
5895 Rewrite (N,
5896 Make_Conditional_Expression (Loc,
5897 Expressions => New_List (
5898 Make_Op_Eq (Loc,
5899 Left_Opnd => Duplicate_Subexpr (Right),
5900 Right_Opnd =>
5901 Unchecked_Convert_To (Typ,
5902 Make_Integer_Literal (Loc, -1))),
5903 Unchecked_Convert_To (Typ,
5904 Make_Integer_Literal (Loc, Uint_0)),
5905 Relocate_Node (N))));
5906
5907 Set_Analyzed (Next (Next (First (Expressions (N)))));
5908 Analyze_And_Resolve (N, Typ);
5909 end if;
5910 end if;
5911 end Expand_N_Op_Mod;
5912
5913 --------------------------
5914 -- Expand_N_Op_Multiply --
5915 --------------------------
5916
5917 procedure Expand_N_Op_Multiply (N : Node_Id) is
5918 Loc : constant Source_Ptr := Sloc (N);
5919 Lop : constant Node_Id := Left_Opnd (N);
5920 Rop : constant Node_Id := Right_Opnd (N);
5921
5922 Lp2 : constant Boolean :=
5923 Nkind (Lop) = N_Op_Expon
5924 and then Is_Power_Of_2_For_Shift (Lop);
5925
5926 Rp2 : constant Boolean :=
5927 Nkind (Rop) = N_Op_Expon
5928 and then Is_Power_Of_2_For_Shift (Rop);
5929
5930 Ltyp : constant Entity_Id := Etype (Lop);
5931 Rtyp : constant Entity_Id := Etype (Rop);
5932 Typ : Entity_Id := Etype (N);
5933
5934 begin
5935 Binary_Op_Validity_Checks (N);
5936
5937 -- Special optimizations for integer types
5938
5939 if Is_Integer_Type (Typ) then
5940
5941 -- N * 0 = 0 * N = 0 for integer types
5942
5943 if (Compile_Time_Known_Value (Rop)
5944 and then Expr_Value (Rop) = Uint_0)
5945 or else
5946 (Compile_Time_Known_Value (Lop)
5947 and then Expr_Value (Lop) = Uint_0)
5948 then
5949 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
5950 Analyze_And_Resolve (N, Typ);
5951 return;
5952 end if;
5953
5954 -- N * 1 = 1 * N = N for integer types
5955
5956 -- This optimisation is not done if we are going to
5957 -- rewrite the product 1 * 2 ** N to a shift.
5958
5959 if Compile_Time_Known_Value (Rop)
5960 and then Expr_Value (Rop) = Uint_1
5961 and then not Lp2
5962 then
5963 Rewrite (N, Lop);
5964 return;
5965
5966 elsif Compile_Time_Known_Value (Lop)
5967 and then Expr_Value (Lop) = Uint_1
5968 and then not Rp2
5969 then
5970 Rewrite (N, Rop);
5971 return;
5972 end if;
5973 end if;
5974
5975 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
5976 -- Is_Power_Of_2_For_Shift is set means that we know that our left
5977 -- operand is an integer, as required for this to work.
5978
5979 if Rp2 then
5980 if Lp2 then
5981
5982 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
5983
5984 Rewrite (N,
5985 Make_Op_Expon (Loc,
5986 Left_Opnd => Make_Integer_Literal (Loc, 2),
5987 Right_Opnd =>
5988 Make_Op_Add (Loc,
5989 Left_Opnd => Right_Opnd (Lop),
5990 Right_Opnd => Right_Opnd (Rop))));
5991 Analyze_And_Resolve (N, Typ);
5992 return;
5993
5994 else
5995 Rewrite (N,
5996 Make_Op_Shift_Left (Loc,
5997 Left_Opnd => Lop,
5998 Right_Opnd =>
5999 Convert_To (Standard_Natural, Right_Opnd (Rop))));
6000 Analyze_And_Resolve (N, Typ);
6001 return;
6002 end if;
6003
6004 -- Same processing for the operands the other way round
6005
6006 elsif Lp2 then
6007 Rewrite (N,
6008 Make_Op_Shift_Left (Loc,
6009 Left_Opnd => Rop,
6010 Right_Opnd =>
6011 Convert_To (Standard_Natural, Right_Opnd (Lop))));
6012 Analyze_And_Resolve (N, Typ);
6013 return;
6014 end if;
6015
6016 -- Do required fixup of universal fixed operation
6017
6018 if Typ = Universal_Fixed then
6019 Fixup_Universal_Fixed_Operation (N);
6020 Typ := Etype (N);
6021 end if;
6022
6023 -- Multiplications with fixed-point results
6024
6025 if Is_Fixed_Point_Type (Typ) then
6026
6027 -- No special processing if Treat_Fixed_As_Integer is set,
6028 -- since from a semantic point of view such operations are
6029 -- simply integer operations and will be treated that way.
6030
6031 if not Treat_Fixed_As_Integer (N) then
6032
6033 -- Case of fixed * integer => fixed
6034
6035 if Is_Integer_Type (Rtyp) then
6036 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
6037
6038 -- Case of integer * fixed => fixed
6039
6040 elsif Is_Integer_Type (Ltyp) then
6041 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
6042
6043 -- Case of fixed * fixed => fixed
6044
6045 else
6046 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
6047 end if;
6048 end if;
6049
6050 -- Other cases of multiplication of fixed-point operands. Again
6051 -- we exclude the cases where Treat_Fixed_As_Integer flag is set.
6052
6053 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6054 and then not Treat_Fixed_As_Integer (N)
6055 then
6056 if Is_Integer_Type (Typ) then
6057 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
6058 else
6059 pragma Assert (Is_Floating_Point_Type (Typ));
6060 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
6061 end if;
6062
6063 -- Mixed-mode operations can appear in a non-static universal
6064 -- context, in which case the integer argument must be converted
6065 -- explicitly.
6066
6067 elsif Typ = Universal_Real
6068 and then Is_Integer_Type (Rtyp)
6069 then
6070 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
6071
6072 Analyze_And_Resolve (Rop, Universal_Real);
6073
6074 elsif Typ = Universal_Real
6075 and then Is_Integer_Type (Ltyp)
6076 then
6077 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
6078
6079 Analyze_And_Resolve (Lop, Universal_Real);
6080
6081 -- Non-fixed point cases, check software overflow checking required
6082
6083 elsif Is_Signed_Integer_Type (Etype (N)) then
6084 Apply_Arithmetic_Overflow_Check (N);
6085
6086 -- Deal with VAX float case
6087
6088 elsif Vax_Float (Typ) then
6089 Expand_Vax_Arith (N);
6090 return;
6091 end if;
6092 end Expand_N_Op_Multiply;
6093
6094 --------------------
6095 -- Expand_N_Op_Ne --
6096 --------------------
6097
6098 procedure Expand_N_Op_Ne (N : Node_Id) is
6099 Typ : constant Entity_Id := Etype (Left_Opnd (N));
6100
6101 begin
6102 -- Case of elementary type with standard operator
6103
6104 if Is_Elementary_Type (Typ)
6105 and then Sloc (Entity (N)) = Standard_Location
6106 then
6107 Binary_Op_Validity_Checks (N);
6108
6109 -- Boolean types (requiring handling of non-standard case)
6110
6111 if Is_Boolean_Type (Typ) then
6112 Adjust_Condition (Left_Opnd (N));
6113 Adjust_Condition (Right_Opnd (N));
6114 Set_Etype (N, Standard_Boolean);
6115 Adjust_Result_Type (N, Typ);
6116 end if;
6117
6118 Rewrite_Comparison (N);
6119
6120 -- If we still have comparison for Vax_Float, process it
6121
6122 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
6123 Expand_Vax_Comparison (N);
6124 return;
6125 end if;
6126
6127 -- For all cases other than elementary types, we rewrite node as the
6128 -- negation of an equality operation, and reanalyze. The equality to be
6129 -- used is defined in the same scope and has the same signature. This
6130 -- signature must be set explicitly since in an instance it may not have
6131 -- the same visibility as in the generic unit. This avoids duplicating
6132 -- or factoring the complex code for record/array equality tests etc.
6133
6134 else
6135 declare
6136 Loc : constant Source_Ptr := Sloc (N);
6137 Neg : Node_Id;
6138 Ne : constant Entity_Id := Entity (N);
6139
6140 begin
6141 Binary_Op_Validity_Checks (N);
6142
6143 Neg :=
6144 Make_Op_Not (Loc,
6145 Right_Opnd =>
6146 Make_Op_Eq (Loc,
6147 Left_Opnd => Left_Opnd (N),
6148 Right_Opnd => Right_Opnd (N)));
6149 Set_Paren_Count (Right_Opnd (Neg), 1);
6150
6151 if Scope (Ne) /= Standard_Standard then
6152 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
6153 end if;
6154
6155 -- For navigation purposes, the inequality is treated as an
6156 -- implicit reference to the corresponding equality. Preserve the
6157 -- Comes_From_ source flag so that the proper Xref entry is
6158 -- generated.
6159
6160 Preserve_Comes_From_Source (Neg, N);
6161 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
6162 Rewrite (N, Neg);
6163 Analyze_And_Resolve (N, Standard_Boolean);
6164 end;
6165 end if;
6166 end Expand_N_Op_Ne;
6167
6168 ---------------------
6169 -- Expand_N_Op_Not --
6170 ---------------------
6171
6172 -- If the argument is other than a Boolean array type, there is no
6173 -- special expansion required.
6174
6175 -- For the packed case, we call the special routine in Exp_Pakd, except
6176 -- that if the component size is greater than one, we use the standard
6177 -- routine generating a gruesome loop (it is so peculiar to have packed
6178 -- arrays with non-standard Boolean representations anyway, so it does
6179 -- not matter that we do not handle this case efficiently).
6180
6181 -- For the unpacked case (and for the special packed case where we have
6182 -- non standard Booleans, as discussed above), we generate and insert
6183 -- into the tree the following function definition:
6184
6185 -- function Nnnn (A : arr) is
6186 -- B : arr;
6187 -- begin
6188 -- for J in a'range loop
6189 -- B (J) := not A (J);
6190 -- end loop;
6191 -- return B;
6192 -- end Nnnn;
6193
6194 -- Here arr is the actual subtype of the parameter (and hence always
6195 -- constrained). Then we replace the not with a call to this function.
6196
6197 procedure Expand_N_Op_Not (N : Node_Id) is
6198 Loc : constant Source_Ptr := Sloc (N);
6199 Typ : constant Entity_Id := Etype (N);
6200 Opnd : Node_Id;
6201 Arr : Entity_Id;
6202 A : Entity_Id;
6203 B : Entity_Id;
6204 J : Entity_Id;
6205 A_J : Node_Id;
6206 B_J : Node_Id;
6207
6208 Func_Name : Entity_Id;
6209 Loop_Statement : Node_Id;
6210
6211 begin
6212 Unary_Op_Validity_Checks (N);
6213
6214 -- For boolean operand, deal with non-standard booleans
6215
6216 if Is_Boolean_Type (Typ) then
6217 Adjust_Condition (Right_Opnd (N));
6218 Set_Etype (N, Standard_Boolean);
6219 Adjust_Result_Type (N, Typ);
6220 return;
6221 end if;
6222
6223 -- Only array types need any other processing
6224
6225 if not Is_Array_Type (Typ) then
6226 return;
6227 end if;
6228
6229 -- Case of array operand. If bit packed with a component size of 1,
6230 -- handle it in Exp_Pakd if the operand is known to be aligned.
6231
6232 if Is_Bit_Packed_Array (Typ)
6233 and then Component_Size (Typ) = 1
6234 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
6235 then
6236 Expand_Packed_Not (N);
6237 return;
6238 end if;
6239
6240 -- Case of array operand which is not bit-packed. If the context is
6241 -- a safe assignment, call in-place operation, If context is a larger
6242 -- boolean expression in the context of a safe assignment, expansion is
6243 -- done by enclosing operation.
6244
6245 Opnd := Relocate_Node (Right_Opnd (N));
6246 Convert_To_Actual_Subtype (Opnd);
6247 Arr := Etype (Opnd);
6248 Ensure_Defined (Arr, N);
6249
6250 if Nkind (Parent (N)) = N_Assignment_Statement then
6251 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
6252 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
6253 return;
6254
6255 -- Special case the negation of a binary operation
6256
6257 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
6258 and then Safe_In_Place_Array_Op
6259 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
6260 then
6261 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
6262 return;
6263 end if;
6264
6265 elsif Nkind (Parent (N)) in N_Binary_Op
6266 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
6267 then
6268 declare
6269 Op1 : constant Node_Id := Left_Opnd (Parent (N));
6270 Op2 : constant Node_Id := Right_Opnd (Parent (N));
6271 Lhs : constant Node_Id := Name (Parent (Parent (N)));
6272
6273 begin
6274 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
6275 if N = Op1
6276 and then Nkind (Op2) = N_Op_Not
6277 then
6278 -- (not A) op (not B) can be reduced to a single call
6279
6280 return;
6281
6282 elsif N = Op2
6283 and then Nkind (Parent (N)) = N_Op_Xor
6284 then
6285 -- A xor (not B) can also be special-cased
6286
6287 return;
6288 end if;
6289 end if;
6290 end;
6291 end if;
6292
6293 A := Make_Defining_Identifier (Loc, Name_uA);
6294 B := Make_Defining_Identifier (Loc, Name_uB);
6295 J := Make_Defining_Identifier (Loc, Name_uJ);
6296
6297 A_J :=
6298 Make_Indexed_Component (Loc,
6299 Prefix => New_Reference_To (A, Loc),
6300 Expressions => New_List (New_Reference_To (J, Loc)));
6301
6302 B_J :=
6303 Make_Indexed_Component (Loc,
6304 Prefix => New_Reference_To (B, Loc),
6305 Expressions => New_List (New_Reference_To (J, Loc)));
6306
6307 Loop_Statement :=
6308 Make_Implicit_Loop_Statement (N,
6309 Identifier => Empty,
6310
6311 Iteration_Scheme =>
6312 Make_Iteration_Scheme (Loc,
6313 Loop_Parameter_Specification =>
6314 Make_Loop_Parameter_Specification (Loc,
6315 Defining_Identifier => J,
6316 Discrete_Subtype_Definition =>
6317 Make_Attribute_Reference (Loc,
6318 Prefix => Make_Identifier (Loc, Chars (A)),
6319 Attribute_Name => Name_Range))),
6320
6321 Statements => New_List (
6322 Make_Assignment_Statement (Loc,
6323 Name => B_J,
6324 Expression => Make_Op_Not (Loc, A_J))));
6325
6326 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('N'));
6327 Set_Is_Inlined (Func_Name);
6328
6329 Insert_Action (N,
6330 Make_Subprogram_Body (Loc,
6331 Specification =>
6332 Make_Function_Specification (Loc,
6333 Defining_Unit_Name => Func_Name,
6334 Parameter_Specifications => New_List (
6335 Make_Parameter_Specification (Loc,
6336 Defining_Identifier => A,
6337 Parameter_Type => New_Reference_To (Typ, Loc))),
6338 Result_Definition => New_Reference_To (Typ, Loc)),
6339
6340 Declarations => New_List (
6341 Make_Object_Declaration (Loc,
6342 Defining_Identifier => B,
6343 Object_Definition => New_Reference_To (Arr, Loc))),
6344
6345 Handled_Statement_Sequence =>
6346 Make_Handled_Sequence_Of_Statements (Loc,
6347 Statements => New_List (
6348 Loop_Statement,
6349 Make_Simple_Return_Statement (Loc,
6350 Expression =>
6351 Make_Identifier (Loc, Chars (B)))))));
6352
6353 Rewrite (N,
6354 Make_Function_Call (Loc,
6355 Name => New_Reference_To (Func_Name, Loc),
6356 Parameter_Associations => New_List (Opnd)));
6357
6358 Analyze_And_Resolve (N, Typ);
6359 end Expand_N_Op_Not;
6360
6361 --------------------
6362 -- Expand_N_Op_Or --
6363 --------------------
6364
6365 procedure Expand_N_Op_Or (N : Node_Id) is
6366 Typ : constant Entity_Id := Etype (N);
6367
6368 begin
6369 Binary_Op_Validity_Checks (N);
6370
6371 if Is_Array_Type (Etype (N)) then
6372 Expand_Boolean_Operator (N);
6373
6374 elsif Is_Boolean_Type (Etype (N)) then
6375 Adjust_Condition (Left_Opnd (N));
6376 Adjust_Condition (Right_Opnd (N));
6377 Set_Etype (N, Standard_Boolean);
6378 Adjust_Result_Type (N, Typ);
6379 end if;
6380 end Expand_N_Op_Or;
6381
6382 ----------------------
6383 -- Expand_N_Op_Plus --
6384 ----------------------
6385
6386 procedure Expand_N_Op_Plus (N : Node_Id) is
6387 begin
6388 Unary_Op_Validity_Checks (N);
6389 end Expand_N_Op_Plus;
6390
6391 ---------------------
6392 -- Expand_N_Op_Rem --
6393 ---------------------
6394
6395 procedure Expand_N_Op_Rem (N : Node_Id) is
6396 Loc : constant Source_Ptr := Sloc (N);
6397 Typ : constant Entity_Id := Etype (N);
6398
6399 Left : constant Node_Id := Left_Opnd (N);
6400 Right : constant Node_Id := Right_Opnd (N);
6401
6402 LLB : Uint;
6403 Llo : Uint;
6404 Lhi : Uint;
6405 LOK : Boolean;
6406 Rlo : Uint;
6407 Rhi : Uint;
6408 ROK : Boolean;
6409
6410 pragma Warnings (Off, Lhi);
6411
6412 begin
6413 Binary_Op_Validity_Checks (N);
6414
6415 if Is_Integer_Type (Etype (N)) then
6416 Apply_Divide_Check (N);
6417 end if;
6418
6419 -- Apply optimization x rem 1 = 0. We don't really need that with
6420 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
6421 -- certainly harmless.
6422
6423 if Is_Integer_Type (Etype (N))
6424 and then Compile_Time_Known_Value (Right)
6425 and then Expr_Value (Right) = Uint_1
6426 then
6427 Rewrite (N, Make_Integer_Literal (Loc, 0));
6428 Analyze_And_Resolve (N, Typ);
6429 return;
6430 end if;
6431
6432 -- Deal with annoying case of largest negative number remainder
6433 -- minus one. Gigi does not handle this case correctly, because
6434 -- it generates a divide instruction which may trap in this case.
6435
6436 -- In fact the check is quite easy, if the right operand is -1,
6437 -- then the remainder is always 0, and we can just ignore the
6438 -- left operand completely in this case.
6439
6440 Determine_Range (Right, ROK, Rlo, Rhi);
6441 Determine_Range (Left, LOK, Llo, Lhi);
6442
6443 -- The operand type may be private (e.g. in the expansion of an
6444 -- an intrinsic operation) so we must use the underlying type to
6445 -- get the bounds, and convert the literals explicitly.
6446
6447 LLB :=
6448 Expr_Value
6449 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
6450
6451 -- Now perform the test, generating code only if needed
6452
6453 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
6454 and then
6455 ((not LOK) or else (Llo = LLB))
6456 then
6457 Rewrite (N,
6458 Make_Conditional_Expression (Loc,
6459 Expressions => New_List (
6460 Make_Op_Eq (Loc,
6461 Left_Opnd => Duplicate_Subexpr (Right),
6462 Right_Opnd =>
6463 Unchecked_Convert_To (Typ,
6464 Make_Integer_Literal (Loc, -1))),
6465
6466 Unchecked_Convert_To (Typ,
6467 Make_Integer_Literal (Loc, Uint_0)),
6468
6469 Relocate_Node (N))));
6470
6471 Set_Analyzed (Next (Next (First (Expressions (N)))));
6472 Analyze_And_Resolve (N, Typ);
6473 end if;
6474 end Expand_N_Op_Rem;
6475
6476 -----------------------------
6477 -- Expand_N_Op_Rotate_Left --
6478 -----------------------------
6479
6480 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
6481 begin
6482 Binary_Op_Validity_Checks (N);
6483 end Expand_N_Op_Rotate_Left;
6484
6485 ------------------------------
6486 -- Expand_N_Op_Rotate_Right --
6487 ------------------------------
6488
6489 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
6490 begin
6491 Binary_Op_Validity_Checks (N);
6492 end Expand_N_Op_Rotate_Right;
6493
6494 ----------------------------
6495 -- Expand_N_Op_Shift_Left --
6496 ----------------------------
6497
6498 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
6499 begin
6500 Binary_Op_Validity_Checks (N);
6501 end Expand_N_Op_Shift_Left;
6502
6503 -----------------------------
6504 -- Expand_N_Op_Shift_Right --
6505 -----------------------------
6506
6507 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
6508 begin
6509 Binary_Op_Validity_Checks (N);
6510 end Expand_N_Op_Shift_Right;
6511
6512 ----------------------------------------
6513 -- Expand_N_Op_Shift_Right_Arithmetic --
6514 ----------------------------------------
6515
6516 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
6517 begin
6518 Binary_Op_Validity_Checks (N);
6519 end Expand_N_Op_Shift_Right_Arithmetic;
6520
6521 --------------------------
6522 -- Expand_N_Op_Subtract --
6523 --------------------------
6524
6525 procedure Expand_N_Op_Subtract (N : Node_Id) is
6526 Typ : constant Entity_Id := Etype (N);
6527
6528 begin
6529 Binary_Op_Validity_Checks (N);
6530
6531 -- N - 0 = N for integer types
6532
6533 if Is_Integer_Type (Typ)
6534 and then Compile_Time_Known_Value (Right_Opnd (N))
6535 and then Expr_Value (Right_Opnd (N)) = 0
6536 then
6537 Rewrite (N, Left_Opnd (N));
6538 return;
6539 end if;
6540
6541 -- Arithemtic overflow checks for signed integer/fixed point types
6542
6543 if Is_Signed_Integer_Type (Typ)
6544 or else Is_Fixed_Point_Type (Typ)
6545 then
6546 Apply_Arithmetic_Overflow_Check (N);
6547
6548 -- Vax floating-point types case
6549
6550 elsif Vax_Float (Typ) then
6551 Expand_Vax_Arith (N);
6552 end if;
6553 end Expand_N_Op_Subtract;
6554
6555 ---------------------
6556 -- Expand_N_Op_Xor --
6557 ---------------------
6558
6559 procedure Expand_N_Op_Xor (N : Node_Id) is
6560 Typ : constant Entity_Id := Etype (N);
6561
6562 begin
6563 Binary_Op_Validity_Checks (N);
6564
6565 if Is_Array_Type (Etype (N)) then
6566 Expand_Boolean_Operator (N);
6567
6568 elsif Is_Boolean_Type (Etype (N)) then
6569 Adjust_Condition (Left_Opnd (N));
6570 Adjust_Condition (Right_Opnd (N));
6571 Set_Etype (N, Standard_Boolean);
6572 Adjust_Result_Type (N, Typ);
6573 end if;
6574 end Expand_N_Op_Xor;
6575
6576 ----------------------
6577 -- Expand_N_Or_Else --
6578 ----------------------
6579
6580 -- Expand into conditional expression if Actions present, and also
6581 -- deal with optimizing case of arguments being True or False.
6582
6583 procedure Expand_N_Or_Else (N : Node_Id) is
6584 Loc : constant Source_Ptr := Sloc (N);
6585 Typ : constant Entity_Id := Etype (N);
6586 Left : constant Node_Id := Left_Opnd (N);
6587 Right : constant Node_Id := Right_Opnd (N);
6588 Actlist : List_Id;
6589
6590 begin
6591 -- Deal with non-standard booleans
6592
6593 if Is_Boolean_Type (Typ) then
6594 Adjust_Condition (Left);
6595 Adjust_Condition (Right);
6596 Set_Etype (N, Standard_Boolean);
6597 end if;
6598
6599 -- Check for cases of left argument is True or False
6600
6601 if Nkind (Left) = N_Identifier then
6602
6603 -- If left argument is False, change (False or else Right) to Right.
6604 -- Any actions associated with Right will be executed unconditionally
6605 -- and can thus be inserted into the tree unconditionally.
6606
6607 if Entity (Left) = Standard_False then
6608 if Present (Actions (N)) then
6609 Insert_Actions (N, Actions (N));
6610 end if;
6611
6612 Rewrite (N, Right);
6613 Adjust_Result_Type (N, Typ);
6614 return;
6615
6616 -- If left argument is True, change (True and then Right) to
6617 -- True. In this case we can forget the actions associated with
6618 -- Right, since they will never be executed.
6619
6620 elsif Entity (Left) = Standard_True then
6621 Kill_Dead_Code (Right);
6622 Kill_Dead_Code (Actions (N));
6623 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
6624 Adjust_Result_Type (N, Typ);
6625 return;
6626 end if;
6627 end if;
6628
6629 -- If Actions are present, we expand
6630
6631 -- left or else right
6632
6633 -- into
6634
6635 -- if left then True else right end
6636
6637 -- with the actions becoming the Else_Actions of the conditional
6638 -- expression. This conditional expression is then further expanded
6639 -- (and will eventually disappear)
6640
6641 if Present (Actions (N)) then
6642 Actlist := Actions (N);
6643 Rewrite (N,
6644 Make_Conditional_Expression (Loc,
6645 Expressions => New_List (
6646 Left,
6647 New_Occurrence_Of (Standard_True, Loc),
6648 Right)));
6649
6650 Set_Else_Actions (N, Actlist);
6651 Analyze_And_Resolve (N, Standard_Boolean);
6652 Adjust_Result_Type (N, Typ);
6653 return;
6654 end if;
6655
6656 -- No actions present, check for cases of right argument True/False
6657
6658 if Nkind (Right) = N_Identifier then
6659
6660 -- Change (Left or else False) to Left. Note that we know there
6661 -- are no actions associated with the True operand, since we
6662 -- just checked for this case above.
6663
6664 if Entity (Right) = Standard_False then
6665 Rewrite (N, Left);
6666
6667 -- Change (Left or else True) to True, making sure to preserve
6668 -- any side effects associated with the Left operand.
6669
6670 elsif Entity (Right) = Standard_True then
6671 Remove_Side_Effects (Left);
6672 Rewrite
6673 (N, New_Occurrence_Of (Standard_True, Loc));
6674 end if;
6675 end if;
6676
6677 Adjust_Result_Type (N, Typ);
6678 end Expand_N_Or_Else;
6679
6680 -----------------------------------
6681 -- Expand_N_Qualified_Expression --
6682 -----------------------------------
6683
6684 procedure Expand_N_Qualified_Expression (N : Node_Id) is
6685 Operand : constant Node_Id := Expression (N);
6686 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
6687
6688 begin
6689 -- Do validity check if validity checking operands
6690
6691 if Validity_Checks_On
6692 and then Validity_Check_Operands
6693 then
6694 Ensure_Valid (Operand);
6695 end if;
6696
6697 -- Apply possible constraint check
6698
6699 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
6700 end Expand_N_Qualified_Expression;
6701
6702 ---------------------------------
6703 -- Expand_N_Selected_Component --
6704 ---------------------------------
6705
6706 -- If the selector is a discriminant of a concurrent object, rewrite the
6707 -- prefix to denote the corresponding record type.
6708
6709 procedure Expand_N_Selected_Component (N : Node_Id) is
6710 Loc : constant Source_Ptr := Sloc (N);
6711 Par : constant Node_Id := Parent (N);
6712 P : constant Node_Id := Prefix (N);
6713 Ptyp : Entity_Id := Underlying_Type (Etype (P));
6714 Disc : Entity_Id;
6715 New_N : Node_Id;
6716 Dcon : Elmt_Id;
6717
6718 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
6719 -- Gigi needs a temporary for prefixes that depend on a discriminant,
6720 -- unless the context of an assignment can provide size information.
6721 -- Don't we have a general routine that does this???
6722
6723 -----------------------
6724 -- In_Left_Hand_Side --
6725 -----------------------
6726
6727 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
6728 begin
6729 return (Nkind (Parent (Comp)) = N_Assignment_Statement
6730 and then Comp = Name (Parent (Comp)))
6731 or else (Present (Parent (Comp))
6732 and then Nkind (Parent (Comp)) in N_Subexpr
6733 and then In_Left_Hand_Side (Parent (Comp)));
6734 end In_Left_Hand_Side;
6735
6736 -- Start of processing for Expand_N_Selected_Component
6737
6738 begin
6739 -- Insert explicit dereference if required
6740
6741 if Is_Access_Type (Ptyp) then
6742 Insert_Explicit_Dereference (P);
6743 Analyze_And_Resolve (P, Designated_Type (Ptyp));
6744
6745 if Ekind (Etype (P)) = E_Private_Subtype
6746 and then Is_For_Access_Subtype (Etype (P))
6747 then
6748 Set_Etype (P, Base_Type (Etype (P)));
6749 end if;
6750
6751 Ptyp := Etype (P);
6752 end if;
6753
6754 -- Deal with discriminant check required
6755
6756 if Do_Discriminant_Check (N) then
6757
6758 -- Present the discrminant checking function to the backend,
6759 -- so that it can inline the call to the function.
6760
6761 Add_Inlined_Body
6762 (Discriminant_Checking_Func
6763 (Original_Record_Component (Entity (Selector_Name (N)))));
6764
6765 -- Now reset the flag and generate the call
6766
6767 Set_Do_Discriminant_Check (N, False);
6768 Generate_Discriminant_Check (N);
6769 end if;
6770
6771 -- Gigi cannot handle unchecked conversions that are the prefix of a
6772 -- selected component with discriminants. This must be checked during
6773 -- expansion, because during analysis the type of the selector is not
6774 -- known at the point the prefix is analyzed. If the conversion is the
6775 -- target of an assignment, then we cannot force the evaluation.
6776
6777 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
6778 and then Has_Discriminants (Etype (N))
6779 and then not In_Left_Hand_Side (N)
6780 then
6781 Force_Evaluation (Prefix (N));
6782 end if;
6783
6784 -- Remaining processing applies only if selector is a discriminant
6785
6786 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
6787
6788 -- If the selector is a discriminant of a constrained record type,
6789 -- we may be able to rewrite the expression with the actual value
6790 -- of the discriminant, a useful optimization in some cases.
6791
6792 if Is_Record_Type (Ptyp)
6793 and then Has_Discriminants (Ptyp)
6794 and then Is_Constrained (Ptyp)
6795 then
6796 -- Do this optimization for discrete types only, and not for
6797 -- access types (access discriminants get us into trouble!)
6798
6799 if not Is_Discrete_Type (Etype (N)) then
6800 null;
6801
6802 -- Don't do this on the left hand of an assignment statement.
6803 -- Normally one would think that references like this would
6804 -- not occur, but they do in generated code, and mean that
6805 -- we really do want to assign the discriminant!
6806
6807 elsif Nkind (Par) = N_Assignment_Statement
6808 and then Name (Par) = N
6809 then
6810 null;
6811
6812 -- Don't do this optimization for the prefix of an attribute
6813 -- or the operand of an object renaming declaration since these
6814 -- are contexts where we do not want the value anyway.
6815
6816 elsif (Nkind (Par) = N_Attribute_Reference
6817 and then Prefix (Par) = N)
6818 or else Is_Renamed_Object (N)
6819 then
6820 null;
6821
6822 -- Don't do this optimization if we are within the code for a
6823 -- discriminant check, since the whole point of such a check may
6824 -- be to verify the condition on which the code below depends!
6825
6826 elsif Is_In_Discriminant_Check (N) then
6827 null;
6828
6829 -- Green light to see if we can do the optimization. There is
6830 -- still one condition that inhibits the optimization below
6831 -- but now is the time to check the particular discriminant.
6832
6833 else
6834 -- Loop through discriminants to find the matching
6835 -- discriminant constraint to see if we can copy it.
6836
6837 Disc := First_Discriminant (Ptyp);
6838 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
6839 Discr_Loop : while Present (Dcon) loop
6840
6841 -- Check if this is the matching discriminant
6842
6843 if Disc = Entity (Selector_Name (N)) then
6844
6845 -- Here we have the matching discriminant. Check for
6846 -- the case of a discriminant of a component that is
6847 -- constrained by an outer discriminant, which cannot
6848 -- be optimized away.
6849
6850 if
6851 Denotes_Discriminant
6852 (Node (Dcon), Check_Concurrent => True)
6853 then
6854 exit Discr_Loop;
6855
6856 -- In the context of a case statement, the expression
6857 -- may have the base type of the discriminant, and we
6858 -- need to preserve the constraint to avoid spurious
6859 -- errors on missing cases.
6860
6861 elsif Nkind (Parent (N)) = N_Case_Statement
6862 and then Etype (Node (Dcon)) /= Etype (Disc)
6863 then
6864 Rewrite (N,
6865 Make_Qualified_Expression (Loc,
6866 Subtype_Mark =>
6867 New_Occurrence_Of (Etype (Disc), Loc),
6868 Expression =>
6869 New_Copy_Tree (Node (Dcon))));
6870 Analyze_And_Resolve (N, Etype (Disc));
6871
6872 -- In case that comes out as a static expression,
6873 -- reset it (a selected component is never static).
6874
6875 Set_Is_Static_Expression (N, False);
6876 return;
6877
6878 -- Otherwise we can just copy the constraint, but the
6879 -- result is certainly not static! In some cases the
6880 -- discriminant constraint has been analyzed in the
6881 -- context of the original subtype indication, but for
6882 -- itypes the constraint might not have been analyzed
6883 -- yet, and this must be done now.
6884
6885 else
6886 Rewrite (N, New_Copy_Tree (Node (Dcon)));
6887 Analyze_And_Resolve (N);
6888 Set_Is_Static_Expression (N, False);
6889 return;
6890 end if;
6891 end if;
6892
6893 Next_Elmt (Dcon);
6894 Next_Discriminant (Disc);
6895 end loop Discr_Loop;
6896
6897 -- Note: the above loop should always find a matching
6898 -- discriminant, but if it does not, we just missed an
6899 -- optimization due to some glitch (perhaps a previous
6900 -- error), so ignore.
6901
6902 end if;
6903 end if;
6904
6905 -- The only remaining processing is in the case of a discriminant of
6906 -- a concurrent object, where we rewrite the prefix to denote the
6907 -- corresponding record type. If the type is derived and has renamed
6908 -- discriminants, use corresponding discriminant, which is the one
6909 -- that appears in the corresponding record.
6910
6911 if not Is_Concurrent_Type (Ptyp) then
6912 return;
6913 end if;
6914
6915 Disc := Entity (Selector_Name (N));
6916
6917 if Is_Derived_Type (Ptyp)
6918 and then Present (Corresponding_Discriminant (Disc))
6919 then
6920 Disc := Corresponding_Discriminant (Disc);
6921 end if;
6922
6923 New_N :=
6924 Make_Selected_Component (Loc,
6925 Prefix =>
6926 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
6927 New_Copy_Tree (P)),
6928 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
6929
6930 Rewrite (N, New_N);
6931 Analyze (N);
6932 end if;
6933 end Expand_N_Selected_Component;
6934
6935 --------------------
6936 -- Expand_N_Slice --
6937 --------------------
6938
6939 procedure Expand_N_Slice (N : Node_Id) is
6940 Loc : constant Source_Ptr := Sloc (N);
6941 Typ : constant Entity_Id := Etype (N);
6942 Pfx : constant Node_Id := Prefix (N);
6943 Ptp : Entity_Id := Etype (Pfx);
6944
6945 function Is_Procedure_Actual (N : Node_Id) return Boolean;
6946 -- Check whether the argument is an actual for a procedure call,
6947 -- in which case the expansion of a bit-packed slice is deferred
6948 -- until the call itself is expanded. The reason this is required
6949 -- is that we might have an IN OUT or OUT parameter, and the copy out
6950 -- is essential, and that copy out would be missed if we created a
6951 -- temporary here in Expand_N_Slice. Note that we don't bother
6952 -- to test specifically for an IN OUT or OUT mode parameter, since it
6953 -- is a bit tricky to do, and it is harmless to defer expansion
6954 -- in the IN case, since the call processing will still generate the
6955 -- appropriate copy in operation, which will take care of the slice.
6956
6957 procedure Make_Temporary;
6958 -- Create a named variable for the value of the slice, in
6959 -- cases where the back-end cannot handle it properly, e.g.
6960 -- when packed types or unaligned slices are involved.
6961
6962 -------------------------
6963 -- Is_Procedure_Actual --
6964 -------------------------
6965
6966 function Is_Procedure_Actual (N : Node_Id) return Boolean is
6967 Par : Node_Id := Parent (N);
6968
6969 begin
6970 loop
6971 -- If our parent is a procedure call we can return
6972
6973 if Nkind (Par) = N_Procedure_Call_Statement then
6974 return True;
6975
6976 -- If our parent is a type conversion, keep climbing the
6977 -- tree, since a type conversion can be a procedure actual.
6978 -- Also keep climbing if parameter association or a qualified
6979 -- expression, since these are additional cases that do can
6980 -- appear on procedure actuals.
6981
6982 elsif Nkind_In (Par, N_Type_Conversion,
6983 N_Parameter_Association,
6984 N_Qualified_Expression)
6985 then
6986 Par := Parent (Par);
6987
6988 -- Any other case is not what we are looking for
6989
6990 else
6991 return False;
6992 end if;
6993 end loop;
6994 end Is_Procedure_Actual;
6995
6996 --------------------
6997 -- Make_Temporary --
6998 --------------------
6999
7000 procedure Make_Temporary is
7001 Decl : Node_Id;
7002 Ent : constant Entity_Id :=
7003 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
7004 begin
7005 Decl :=
7006 Make_Object_Declaration (Loc,
7007 Defining_Identifier => Ent,
7008 Object_Definition => New_Occurrence_Of (Typ, Loc));
7009
7010 Set_No_Initialization (Decl);
7011
7012 Insert_Actions (N, New_List (
7013 Decl,
7014 Make_Assignment_Statement (Loc,
7015 Name => New_Occurrence_Of (Ent, Loc),
7016 Expression => Relocate_Node (N))));
7017
7018 Rewrite (N, New_Occurrence_Of (Ent, Loc));
7019 Analyze_And_Resolve (N, Typ);
7020 end Make_Temporary;
7021
7022 -- Start of processing for Expand_N_Slice
7023
7024 begin
7025 -- Special handling for access types
7026
7027 if Is_Access_Type (Ptp) then
7028
7029 Ptp := Designated_Type (Ptp);
7030
7031 Rewrite (Pfx,
7032 Make_Explicit_Dereference (Sloc (N),
7033 Prefix => Relocate_Node (Pfx)));
7034
7035 Analyze_And_Resolve (Pfx, Ptp);
7036 end if;
7037
7038 -- Range checks are potentially also needed for cases involving
7039 -- a slice indexed by a subtype indication, but Do_Range_Check
7040 -- can currently only be set for expressions ???
7041
7042 if not Index_Checks_Suppressed (Ptp)
7043 and then (not Is_Entity_Name (Pfx)
7044 or else not Index_Checks_Suppressed (Entity (Pfx)))
7045 and then Nkind (Discrete_Range (N)) /= N_Subtype_Indication
7046
7047 -- Do not enable range check to nodes associated with the frontend
7048 -- expansion of the dispatch table. We first check if Ada.Tags is
7049 -- already loaded to avoid the addition of an undesired dependence
7050 -- on such run-time unit.
7051
7052 and then
7053 (VM_Target /= No_VM
7054 or else not
7055 (RTU_Loaded (Ada_Tags)
7056 and then Nkind (Prefix (N)) = N_Selected_Component
7057 and then Present (Entity (Selector_Name (Prefix (N))))
7058 and then Entity (Selector_Name (Prefix (N))) =
7059 RTE_Record_Component (RE_Prims_Ptr)))
7060 then
7061 Enable_Range_Check (Discrete_Range (N));
7062 end if;
7063
7064 -- The remaining case to be handled is packed slices. We can leave
7065 -- packed slices as they are in the following situations:
7066
7067 -- 1. Right or left side of an assignment (we can handle this
7068 -- situation correctly in the assignment statement expansion).
7069
7070 -- 2. Prefix of indexed component (the slide is optimized away
7071 -- in this case, see the start of Expand_N_Slice.)
7072
7073 -- 3. Object renaming declaration, since we want the name of
7074 -- the slice, not the value.
7075
7076 -- 4. Argument to procedure call, since copy-in/copy-out handling
7077 -- may be required, and this is handled in the expansion of
7078 -- call itself.
7079
7080 -- 5. Prefix of an address attribute (this is an error which
7081 -- is caught elsewhere, and the expansion would intefere
7082 -- with generating the error message).
7083
7084 if not Is_Packed (Typ) then
7085
7086 -- Apply transformation for actuals of a function call,
7087 -- where Expand_Actuals is not used.
7088
7089 if Nkind (Parent (N)) = N_Function_Call
7090 and then Is_Possibly_Unaligned_Slice (N)
7091 then
7092 Make_Temporary;
7093 end if;
7094
7095 elsif Nkind (Parent (N)) = N_Assignment_Statement
7096 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
7097 and then Parent (N) = Name (Parent (Parent (N))))
7098 then
7099 return;
7100
7101 elsif Nkind (Parent (N)) = N_Indexed_Component
7102 or else Is_Renamed_Object (N)
7103 or else Is_Procedure_Actual (N)
7104 then
7105 return;
7106
7107 elsif Nkind (Parent (N)) = N_Attribute_Reference
7108 and then Attribute_Name (Parent (N)) = Name_Address
7109 then
7110 return;
7111
7112 else
7113 Make_Temporary;
7114 end if;
7115 end Expand_N_Slice;
7116
7117 ------------------------------
7118 -- Expand_N_Type_Conversion --
7119 ------------------------------
7120
7121 procedure Expand_N_Type_Conversion (N : Node_Id) is
7122 Loc : constant Source_Ptr := Sloc (N);
7123 Operand : constant Node_Id := Expression (N);
7124 Target_Type : constant Entity_Id := Etype (N);
7125 Operand_Type : Entity_Id := Etype (Operand);
7126
7127 procedure Handle_Changed_Representation;
7128 -- This is called in the case of record and array type conversions
7129 -- to see if there is a change of representation to be handled.
7130 -- Change of representation is actually handled at the assignment
7131 -- statement level, and what this procedure does is rewrite node N
7132 -- conversion as an assignment to temporary. If there is no change
7133 -- of representation, then the conversion node is unchanged.
7134
7135 procedure Real_Range_Check;
7136 -- Handles generation of range check for real target value
7137
7138 -----------------------------------
7139 -- Handle_Changed_Representation --
7140 -----------------------------------
7141
7142 procedure Handle_Changed_Representation is
7143 Temp : Entity_Id;
7144 Decl : Node_Id;
7145 Odef : Node_Id;
7146 Disc : Node_Id;
7147 N_Ix : Node_Id;
7148 Cons : List_Id;
7149
7150 begin
7151 -- Nothing else to do if no change of representation
7152
7153 if Same_Representation (Operand_Type, Target_Type) then
7154 return;
7155
7156 -- The real change of representation work is done by the assignment
7157 -- statement processing. So if this type conversion is appearing as
7158 -- the expression of an assignment statement, nothing needs to be
7159 -- done to the conversion.
7160
7161 elsif Nkind (Parent (N)) = N_Assignment_Statement then
7162 return;
7163
7164 -- Otherwise we need to generate a temporary variable, and do the
7165 -- change of representation assignment into that temporary variable.
7166 -- The conversion is then replaced by a reference to this variable.
7167
7168 else
7169 Cons := No_List;
7170
7171 -- If type is unconstrained we have to add a constraint,
7172 -- copied from the actual value of the left hand side.
7173
7174 if not Is_Constrained (Target_Type) then
7175 if Has_Discriminants (Operand_Type) then
7176 Disc := First_Discriminant (Operand_Type);
7177
7178 if Disc /= First_Stored_Discriminant (Operand_Type) then
7179 Disc := First_Stored_Discriminant (Operand_Type);
7180 end if;
7181
7182 Cons := New_List;
7183 while Present (Disc) loop
7184 Append_To (Cons,
7185 Make_Selected_Component (Loc,
7186 Prefix => Duplicate_Subexpr_Move_Checks (Operand),
7187 Selector_Name =>
7188 Make_Identifier (Loc, Chars (Disc))));
7189 Next_Discriminant (Disc);
7190 end loop;
7191
7192 elsif Is_Array_Type (Operand_Type) then
7193 N_Ix := First_Index (Target_Type);
7194 Cons := New_List;
7195
7196 for J in 1 .. Number_Dimensions (Operand_Type) loop
7197
7198 -- We convert the bounds explicitly. We use an unchecked
7199 -- conversion because bounds checks are done elsewhere.
7200
7201 Append_To (Cons,
7202 Make_Range (Loc,
7203 Low_Bound =>
7204 Unchecked_Convert_To (Etype (N_Ix),
7205 Make_Attribute_Reference (Loc,
7206 Prefix =>
7207 Duplicate_Subexpr_No_Checks
7208 (Operand, Name_Req => True),
7209 Attribute_Name => Name_First,
7210 Expressions => New_List (
7211 Make_Integer_Literal (Loc, J)))),
7212
7213 High_Bound =>
7214 Unchecked_Convert_To (Etype (N_Ix),
7215 Make_Attribute_Reference (Loc,
7216 Prefix =>
7217 Duplicate_Subexpr_No_Checks
7218 (Operand, Name_Req => True),
7219 Attribute_Name => Name_Last,
7220 Expressions => New_List (
7221 Make_Integer_Literal (Loc, J))))));
7222
7223 Next_Index (N_Ix);
7224 end loop;
7225 end if;
7226 end if;
7227
7228 Odef := New_Occurrence_Of (Target_Type, Loc);
7229
7230 if Present (Cons) then
7231 Odef :=
7232 Make_Subtype_Indication (Loc,
7233 Subtype_Mark => Odef,
7234 Constraint =>
7235 Make_Index_Or_Discriminant_Constraint (Loc,
7236 Constraints => Cons));
7237 end if;
7238
7239 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
7240 Decl :=
7241 Make_Object_Declaration (Loc,
7242 Defining_Identifier => Temp,
7243 Object_Definition => Odef);
7244
7245 Set_No_Initialization (Decl, True);
7246
7247 -- Insert required actions. It is essential to suppress checks
7248 -- since we have suppressed default initialization, which means
7249 -- that the variable we create may have no discriminants.
7250
7251 Insert_Actions (N,
7252 New_List (
7253 Decl,
7254 Make_Assignment_Statement (Loc,
7255 Name => New_Occurrence_Of (Temp, Loc),
7256 Expression => Relocate_Node (N))),
7257 Suppress => All_Checks);
7258
7259 Rewrite (N, New_Occurrence_Of (Temp, Loc));
7260 return;
7261 end if;
7262 end Handle_Changed_Representation;
7263
7264 ----------------------
7265 -- Real_Range_Check --
7266 ----------------------
7267
7268 -- Case of conversions to floating-point or fixed-point. If range
7269 -- checks are enabled and the target type has a range constraint,
7270 -- we convert:
7271
7272 -- typ (x)
7273
7274 -- to
7275
7276 -- Tnn : typ'Base := typ'Base (x);
7277 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
7278 -- Tnn
7279
7280 -- This is necessary when there is a conversion of integer to float
7281 -- or to fixed-point to ensure that the correct checks are made. It
7282 -- is not necessary for float to float where it is enough to simply
7283 -- set the Do_Range_Check flag.
7284
7285 procedure Real_Range_Check is
7286 Btyp : constant Entity_Id := Base_Type (Target_Type);
7287 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
7288 Hi : constant Node_Id := Type_High_Bound (Target_Type);
7289 Xtyp : constant Entity_Id := Etype (Operand);
7290 Conv : Node_Id;
7291 Tnn : Entity_Id;
7292
7293 begin
7294 -- Nothing to do if conversion was rewritten
7295
7296 if Nkind (N) /= N_Type_Conversion then
7297 return;
7298 end if;
7299
7300 -- Nothing to do if range checks suppressed, or target has the
7301 -- same range as the base type (or is the base type).
7302
7303 if Range_Checks_Suppressed (Target_Type)
7304 or else (Lo = Type_Low_Bound (Btyp)
7305 and then
7306 Hi = Type_High_Bound (Btyp))
7307 then
7308 return;
7309 end if;
7310
7311 -- Nothing to do if expression is an entity on which checks
7312 -- have been suppressed.
7313
7314 if Is_Entity_Name (Operand)
7315 and then Range_Checks_Suppressed (Entity (Operand))
7316 then
7317 return;
7318 end if;
7319
7320 -- Nothing to do if bounds are all static and we can tell that
7321 -- the expression is within the bounds of the target. Note that
7322 -- if the operand is of an unconstrained floating-point type,
7323 -- then we do not trust it to be in range (might be infinite)
7324
7325 declare
7326 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
7327 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
7328
7329 begin
7330 if (not Is_Floating_Point_Type (Xtyp)
7331 or else Is_Constrained (Xtyp))
7332 and then Compile_Time_Known_Value (S_Lo)
7333 and then Compile_Time_Known_Value (S_Hi)
7334 and then Compile_Time_Known_Value (Hi)
7335 and then Compile_Time_Known_Value (Lo)
7336 then
7337 declare
7338 D_Lov : constant Ureal := Expr_Value_R (Lo);
7339 D_Hiv : constant Ureal := Expr_Value_R (Hi);
7340 S_Lov : Ureal;
7341 S_Hiv : Ureal;
7342
7343 begin
7344 if Is_Real_Type (Xtyp) then
7345 S_Lov := Expr_Value_R (S_Lo);
7346 S_Hiv := Expr_Value_R (S_Hi);
7347 else
7348 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
7349 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
7350 end if;
7351
7352 if D_Hiv > D_Lov
7353 and then S_Lov >= D_Lov
7354 and then S_Hiv <= D_Hiv
7355 then
7356 Set_Do_Range_Check (Operand, False);
7357 return;
7358 end if;
7359 end;
7360 end if;
7361 end;
7362
7363 -- For float to float conversions, we are done
7364
7365 if Is_Floating_Point_Type (Xtyp)
7366 and then
7367 Is_Floating_Point_Type (Btyp)
7368 then
7369 return;
7370 end if;
7371
7372 -- Otherwise rewrite the conversion as described above
7373
7374 Conv := Relocate_Node (N);
7375 Rewrite
7376 (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
7377 Set_Etype (Conv, Btyp);
7378
7379 -- Enable overflow except for case of integer to float conversions,
7380 -- where it is never required, since we can never have overflow in
7381 -- this case.
7382
7383 if not Is_Integer_Type (Etype (Operand)) then
7384 Enable_Overflow_Check (Conv);
7385 end if;
7386
7387 Tnn :=
7388 Make_Defining_Identifier (Loc,
7389 Chars => New_Internal_Name ('T'));
7390
7391 Insert_Actions (N, New_List (
7392 Make_Object_Declaration (Loc,
7393 Defining_Identifier => Tnn,
7394 Object_Definition => New_Occurrence_Of (Btyp, Loc),
7395 Expression => Conv),
7396
7397 Make_Raise_Constraint_Error (Loc,
7398 Condition =>
7399 Make_Or_Else (Loc,
7400 Left_Opnd =>
7401 Make_Op_Lt (Loc,
7402 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
7403 Right_Opnd =>
7404 Make_Attribute_Reference (Loc,
7405 Attribute_Name => Name_First,
7406 Prefix =>
7407 New_Occurrence_Of (Target_Type, Loc))),
7408
7409 Right_Opnd =>
7410 Make_Op_Gt (Loc,
7411 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
7412 Right_Opnd =>
7413 Make_Attribute_Reference (Loc,
7414 Attribute_Name => Name_Last,
7415 Prefix =>
7416 New_Occurrence_Of (Target_Type, Loc)))),
7417 Reason => CE_Range_Check_Failed)));
7418
7419 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
7420 Analyze_And_Resolve (N, Btyp);
7421 end Real_Range_Check;
7422
7423 -- Start of processing for Expand_N_Type_Conversion
7424
7425 begin
7426 -- Nothing at all to do if conversion is to the identical type
7427 -- so remove the conversion completely, it is useless.
7428
7429 if Operand_Type = Target_Type then
7430 Rewrite (N, Relocate_Node (Operand));
7431 return;
7432 end if;
7433
7434 -- Nothing to do if this is the second argument of read. This
7435 -- is a "backwards" conversion that will be handled by the
7436 -- specialized code in attribute processing.
7437
7438 if Nkind (Parent (N)) = N_Attribute_Reference
7439 and then Attribute_Name (Parent (N)) = Name_Read
7440 and then Next (First (Expressions (Parent (N)))) = N
7441 then
7442 return;
7443 end if;
7444
7445 -- Here if we may need to expand conversion
7446
7447 -- Do validity check if validity checking operands
7448
7449 if Validity_Checks_On
7450 and then Validity_Check_Operands
7451 then
7452 Ensure_Valid (Operand);
7453 end if;
7454
7455 -- Special case of converting from non-standard boolean type
7456
7457 if Is_Boolean_Type (Operand_Type)
7458 and then (Nonzero_Is_True (Operand_Type))
7459 then
7460 Adjust_Condition (Operand);
7461 Set_Etype (Operand, Standard_Boolean);
7462 Operand_Type := Standard_Boolean;
7463 end if;
7464
7465 -- Case of converting to an access type
7466
7467 if Is_Access_Type (Target_Type) then
7468
7469 -- Apply an accessibility check when the conversion operand is an
7470 -- access parameter (or a renaming thereof), unless conversion was
7471 -- expanded from an unchecked or unrestricted access attribute. Note
7472 -- that other checks may still need to be applied below (such as
7473 -- tagged type checks).
7474
7475 if Is_Entity_Name (Operand)
7476 and then
7477 (Is_Formal (Entity (Operand))
7478 or else
7479 (Present (Renamed_Object (Entity (Operand)))
7480 and then Is_Entity_Name (Renamed_Object (Entity (Operand)))
7481 and then Is_Formal
7482 (Entity (Renamed_Object (Entity (Operand))))))
7483 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
7484 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
7485 or else Attribute_Name (Original_Node (N)) = Name_Access)
7486 then
7487 Apply_Accessibility_Check (Operand, Target_Type);
7488
7489 -- If the level of the operand type is statically deeper
7490 -- then the level of the target type, then force Program_Error.
7491 -- Note that this can only occur for cases where the attribute
7492 -- is within the body of an instantiation (otherwise the
7493 -- conversion will already have been rejected as illegal).
7494 -- Note: warnings are issued by the analyzer for the instance
7495 -- cases.
7496
7497 elsif In_Instance_Body
7498 and then Type_Access_Level (Operand_Type) >
7499 Type_Access_Level (Target_Type)
7500 then
7501 Rewrite (N,
7502 Make_Raise_Program_Error (Sloc (N),
7503 Reason => PE_Accessibility_Check_Failed));
7504 Set_Etype (N, Target_Type);
7505
7506 -- When the operand is a selected access discriminant
7507 -- the check needs to be made against the level of the
7508 -- object denoted by the prefix of the selected name.
7509 -- Force Program_Error for this case as well (this
7510 -- accessibility violation can only happen if within
7511 -- the body of an instantiation).
7512
7513 elsif In_Instance_Body
7514 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
7515 and then Nkind (Operand) = N_Selected_Component
7516 and then Object_Access_Level (Operand) >
7517 Type_Access_Level (Target_Type)
7518 then
7519 Rewrite (N,
7520 Make_Raise_Program_Error (Sloc (N),
7521 Reason => PE_Accessibility_Check_Failed));
7522 Set_Etype (N, Target_Type);
7523 end if;
7524 end if;
7525
7526 -- Case of conversions of tagged types and access to tagged types
7527
7528 -- When needed, that is to say when the expression is class-wide,
7529 -- Add runtime a tag check for (strict) downward conversion by using
7530 -- the membership test, generating:
7531
7532 -- [constraint_error when Operand not in Target_Type'Class]
7533
7534 -- or in the access type case
7535
7536 -- [constraint_error
7537 -- when Operand /= null
7538 -- and then Operand.all not in
7539 -- Designated_Type (Target_Type)'Class]
7540
7541 if (Is_Access_Type (Target_Type)
7542 and then Is_Tagged_Type (Designated_Type (Target_Type)))
7543 or else Is_Tagged_Type (Target_Type)
7544 then
7545 -- Do not do any expansion in the access type case if the
7546 -- parent is a renaming, since this is an error situation
7547 -- which will be caught by Sem_Ch8, and the expansion can
7548 -- intefere with this error check.
7549
7550 if Is_Access_Type (Target_Type)
7551 and then Is_Renamed_Object (N)
7552 then
7553 return;
7554 end if;
7555
7556 -- Otherwise, proceed with processing tagged conversion
7557
7558 declare
7559 Actual_Operand_Type : Entity_Id;
7560 Actual_Target_Type : Entity_Id;
7561
7562 Cond : Node_Id;
7563
7564 begin
7565 if Is_Access_Type (Target_Type) then
7566 Actual_Operand_Type := Designated_Type (Operand_Type);
7567 Actual_Target_Type := Designated_Type (Target_Type);
7568
7569 else
7570 Actual_Operand_Type := Operand_Type;
7571 Actual_Target_Type := Target_Type;
7572 end if;
7573
7574 -- Ada 2005 (AI-251): Handle interface type conversion
7575
7576 if Is_Interface (Actual_Operand_Type) then
7577 Expand_Interface_Conversion (N, Is_Static => False);
7578 return;
7579 end if;
7580
7581 if Is_Class_Wide_Type (Actual_Operand_Type)
7582 and then Root_Type (Actual_Operand_Type) /= Actual_Target_Type
7583 and then Is_Ancestor
7584 (Root_Type (Actual_Operand_Type),
7585 Actual_Target_Type)
7586 and then not Tag_Checks_Suppressed (Actual_Target_Type)
7587 then
7588 -- The conversion is valid for any descendant of the
7589 -- target type
7590
7591 Actual_Target_Type := Class_Wide_Type (Actual_Target_Type);
7592
7593 if Is_Access_Type (Target_Type) then
7594 Cond :=
7595 Make_And_Then (Loc,
7596 Left_Opnd =>
7597 Make_Op_Ne (Loc,
7598 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
7599 Right_Opnd => Make_Null (Loc)),
7600
7601 Right_Opnd =>
7602 Make_Not_In (Loc,
7603 Left_Opnd =>
7604 Make_Explicit_Dereference (Loc,
7605 Prefix =>
7606 Duplicate_Subexpr_No_Checks (Operand)),
7607 Right_Opnd =>
7608 New_Reference_To (Actual_Target_Type, Loc)));
7609
7610 else
7611 Cond :=
7612 Make_Not_In (Loc,
7613 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
7614 Right_Opnd =>
7615 New_Reference_To (Actual_Target_Type, Loc));
7616 end if;
7617
7618 Insert_Action (N,
7619 Make_Raise_Constraint_Error (Loc,
7620 Condition => Cond,
7621 Reason => CE_Tag_Check_Failed));
7622
7623 declare
7624 Conv : Node_Id;
7625 begin
7626 Conv :=
7627 Make_Unchecked_Type_Conversion (Loc,
7628 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
7629 Expression => Relocate_Node (Expression (N)));
7630 Rewrite (N, Conv);
7631 Analyze_And_Resolve (N, Target_Type);
7632 end;
7633 end if;
7634 end;
7635
7636 -- Case of other access type conversions
7637
7638 elsif Is_Access_Type (Target_Type) then
7639 Apply_Constraint_Check (Operand, Target_Type);
7640
7641 -- Case of conversions from a fixed-point type
7642
7643 -- These conversions require special expansion and processing, found
7644 -- in the Exp_Fixd package. We ignore cases where Conversion_OK is
7645 -- set, since from a semantic point of view, these are simple integer
7646 -- conversions, which do not need further processing.
7647
7648 elsif Is_Fixed_Point_Type (Operand_Type)
7649 and then not Conversion_OK (N)
7650 then
7651 -- We should never see universal fixed at this case, since the
7652 -- expansion of the constituent divide or multiply should have
7653 -- eliminated the explicit mention of universal fixed.
7654
7655 pragma Assert (Operand_Type /= Universal_Fixed);
7656
7657 -- Check for special case of the conversion to universal real
7658 -- that occurs as a result of the use of a round attribute.
7659 -- In this case, the real type for the conversion is taken
7660 -- from the target type of the Round attribute and the
7661 -- result must be marked as rounded.
7662
7663 if Target_Type = Universal_Real
7664 and then Nkind (Parent (N)) = N_Attribute_Reference
7665 and then Attribute_Name (Parent (N)) = Name_Round
7666 then
7667 Set_Rounded_Result (N);
7668 Set_Etype (N, Etype (Parent (N)));
7669 end if;
7670
7671 -- Otherwise do correct fixed-conversion, but skip these if the
7672 -- Conversion_OK flag is set, because from a semantic point of
7673 -- view these are simple integer conversions needing no further
7674 -- processing (the backend will simply treat them as integers)
7675
7676 if not Conversion_OK (N) then
7677 if Is_Fixed_Point_Type (Etype (N)) then
7678 Expand_Convert_Fixed_To_Fixed (N);
7679 Real_Range_Check;
7680
7681 elsif Is_Integer_Type (Etype (N)) then
7682 Expand_Convert_Fixed_To_Integer (N);
7683
7684 else
7685 pragma Assert (Is_Floating_Point_Type (Etype (N)));
7686 Expand_Convert_Fixed_To_Float (N);
7687 Real_Range_Check;
7688 end if;
7689 end if;
7690
7691 -- Case of conversions to a fixed-point type
7692
7693 -- These conversions require special expansion and processing, found
7694 -- in the Exp_Fixd package. Again, ignore cases where Conversion_OK
7695 -- is set, since from a semantic point of view, these are simple
7696 -- integer conversions, which do not need further processing.
7697
7698 elsif Is_Fixed_Point_Type (Target_Type)
7699 and then not Conversion_OK (N)
7700 then
7701 if Is_Integer_Type (Operand_Type) then
7702 Expand_Convert_Integer_To_Fixed (N);
7703 Real_Range_Check;
7704 else
7705 pragma Assert (Is_Floating_Point_Type (Operand_Type));
7706 Expand_Convert_Float_To_Fixed (N);
7707 Real_Range_Check;
7708 end if;
7709
7710 -- Case of float-to-integer conversions
7711
7712 -- We also handle float-to-fixed conversions with Conversion_OK set
7713 -- since semantically the fixed-point target is treated as though it
7714 -- were an integer in such cases.
7715
7716 elsif Is_Floating_Point_Type (Operand_Type)
7717 and then
7718 (Is_Integer_Type (Target_Type)
7719 or else
7720 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
7721 then
7722 -- One more check here, gcc is still not able to do conversions of
7723 -- this type with proper overflow checking, and so gigi is doing an
7724 -- approximation of what is required by doing floating-point compares
7725 -- with the end-point. But that can lose precision in some cases, and
7726 -- give a wrong result. Converting the operand to Universal_Real is
7727 -- helpful, but still does not catch all cases with 64-bit integers
7728 -- on targets with only 64-bit floats
7729
7730 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
7731 -- Can this code be removed ???
7732
7733 if Do_Range_Check (Operand) then
7734 Rewrite (Operand,
7735 Make_Type_Conversion (Loc,
7736 Subtype_Mark =>
7737 New_Occurrence_Of (Universal_Real, Loc),
7738 Expression =>
7739 Relocate_Node (Operand)));
7740
7741 Set_Etype (Operand, Universal_Real);
7742 Enable_Range_Check (Operand);
7743 Set_Do_Range_Check (Expression (Operand), False);
7744 end if;
7745
7746 -- Case of array conversions
7747
7748 -- Expansion of array conversions, add required length/range checks
7749 -- but only do this if there is no change of representation. For
7750 -- handling of this case, see Handle_Changed_Representation.
7751
7752 elsif Is_Array_Type (Target_Type) then
7753
7754 if Is_Constrained (Target_Type) then
7755 Apply_Length_Check (Operand, Target_Type);
7756 else
7757 Apply_Range_Check (Operand, Target_Type);
7758 end if;
7759
7760 Handle_Changed_Representation;
7761
7762 -- Case of conversions of discriminated types
7763
7764 -- Add required discriminant checks if target is constrained. Again
7765 -- this change is skipped if we have a change of representation.
7766
7767 elsif Has_Discriminants (Target_Type)
7768 and then Is_Constrained (Target_Type)
7769 then
7770 Apply_Discriminant_Check (Operand, Target_Type);
7771 Handle_Changed_Representation;
7772
7773 -- Case of all other record conversions. The only processing required
7774 -- is to check for a change of representation requiring the special
7775 -- assignment processing.
7776
7777 elsif Is_Record_Type (Target_Type) then
7778
7779 -- Ada 2005 (AI-216): Program_Error is raised when converting from
7780 -- a derived Unchecked_Union type to an unconstrained non-Unchecked_
7781 -- Union type if the operand lacks inferable discriminants.
7782
7783 if Is_Derived_Type (Operand_Type)
7784 and then Is_Unchecked_Union (Base_Type (Operand_Type))
7785 and then not Is_Constrained (Target_Type)
7786 and then not Is_Unchecked_Union (Base_Type (Target_Type))
7787 and then not Has_Inferable_Discriminants (Operand)
7788 then
7789 -- To prevent Gigi from generating illegal code, we make a
7790 -- Program_Error node, but we give it the target type of the
7791 -- conversion.
7792
7793 declare
7794 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
7795 Reason => PE_Unchecked_Union_Restriction);
7796
7797 begin
7798 Set_Etype (PE, Target_Type);
7799 Rewrite (N, PE);
7800
7801 end;
7802 else
7803 Handle_Changed_Representation;
7804 end if;
7805
7806 -- Case of conversions of enumeration types
7807
7808 elsif Is_Enumeration_Type (Target_Type) then
7809
7810 -- Special processing is required if there is a change of
7811 -- representation (from enumeration representation clauses)
7812
7813 if not Same_Representation (Target_Type, Operand_Type) then
7814
7815 -- Convert: x(y) to x'val (ytyp'val (y))
7816
7817 Rewrite (N,
7818 Make_Attribute_Reference (Loc,
7819 Prefix => New_Occurrence_Of (Target_Type, Loc),
7820 Attribute_Name => Name_Val,
7821 Expressions => New_List (
7822 Make_Attribute_Reference (Loc,
7823 Prefix => New_Occurrence_Of (Operand_Type, Loc),
7824 Attribute_Name => Name_Pos,
7825 Expressions => New_List (Operand)))));
7826
7827 Analyze_And_Resolve (N, Target_Type);
7828 end if;
7829
7830 -- Case of conversions to floating-point
7831
7832 elsif Is_Floating_Point_Type (Target_Type) then
7833 Real_Range_Check;
7834 end if;
7835
7836 -- At this stage, either the conversion node has been transformed
7837 -- into some other equivalent expression, or left as a conversion
7838 -- that can be handled by Gigi. The conversions that Gigi can handle
7839 -- are the following:
7840
7841 -- Conversions with no change of representation or type
7842
7843 -- Numeric conversions involving integer values, floating-point
7844 -- values, and fixed-point values. Fixed-point values are allowed
7845 -- only if Conversion_OK is set, i.e. if the fixed-point values
7846 -- are to be treated as integers.
7847
7848 -- No other conversions should be passed to Gigi
7849
7850 -- Check: are these rules stated in sinfo??? if so, why restate here???
7851
7852 -- The only remaining step is to generate a range check if we still
7853 -- have a type conversion at this stage and Do_Range_Check is set.
7854 -- For now we do this only for conversions of discrete types.
7855
7856 if Nkind (N) = N_Type_Conversion
7857 and then Is_Discrete_Type (Etype (N))
7858 then
7859 declare
7860 Expr : constant Node_Id := Expression (N);
7861 Ftyp : Entity_Id;
7862 Ityp : Entity_Id;
7863
7864 begin
7865 if Do_Range_Check (Expr)
7866 and then Is_Discrete_Type (Etype (Expr))
7867 then
7868 Set_Do_Range_Check (Expr, False);
7869
7870 -- Before we do a range check, we have to deal with treating
7871 -- a fixed-point operand as an integer. The way we do this
7872 -- is simply to do an unchecked conversion to an appropriate
7873 -- integer type large enough to hold the result.
7874
7875 -- This code is not active yet, because we are only dealing
7876 -- with discrete types so far ???
7877
7878 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
7879 and then Treat_Fixed_As_Integer (Expr)
7880 then
7881 Ftyp := Base_Type (Etype (Expr));
7882
7883 if Esize (Ftyp) >= Esize (Standard_Integer) then
7884 Ityp := Standard_Long_Long_Integer;
7885 else
7886 Ityp := Standard_Integer;
7887 end if;
7888
7889 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
7890 end if;
7891
7892 -- Reset overflow flag, since the range check will include
7893 -- dealing with possible overflow, and generate the check
7894 -- If Address is either source or target type, suppress
7895 -- range check to avoid typing anomalies when it is a visible
7896 -- integer type.
7897
7898 Set_Do_Overflow_Check (N, False);
7899 if not Is_Descendent_Of_Address (Etype (Expr))
7900 and then not Is_Descendent_Of_Address (Target_Type)
7901 then
7902 Generate_Range_Check
7903 (Expr, Target_Type, CE_Range_Check_Failed);
7904 end if;
7905 end if;
7906 end;
7907 end if;
7908
7909 -- Final step, if the result is a type conversion involving Vax_Float
7910 -- types, then it is subject for further special processing.
7911
7912 if Nkind (N) = N_Type_Conversion
7913 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
7914 then
7915 Expand_Vax_Conversion (N);
7916 return;
7917 end if;
7918 end Expand_N_Type_Conversion;
7919
7920 -----------------------------------
7921 -- Expand_N_Unchecked_Expression --
7922 -----------------------------------
7923
7924 -- Remove the unchecked expression node from the tree. It's job was simply
7925 -- to make sure that its constituent expression was handled with checks
7926 -- off, and now that that is done, we can remove it from the tree, and
7927 -- indeed must, since gigi does not expect to see these nodes.
7928
7929 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
7930 Exp : constant Node_Id := Expression (N);
7931
7932 begin
7933 Set_Assignment_OK (Exp, Assignment_OK (N) or Assignment_OK (Exp));
7934 Rewrite (N, Exp);
7935 end Expand_N_Unchecked_Expression;
7936
7937 ----------------------------------------
7938 -- Expand_N_Unchecked_Type_Conversion --
7939 ----------------------------------------
7940
7941 -- If this cannot be handled by Gigi and we haven't already made
7942 -- a temporary for it, do it now.
7943
7944 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
7945 Target_Type : constant Entity_Id := Etype (N);
7946 Operand : constant Node_Id := Expression (N);
7947 Operand_Type : constant Entity_Id := Etype (Operand);
7948
7949 begin
7950 -- If we have a conversion of a compile time known value to a target
7951 -- type and the value is in range of the target type, then we can simply
7952 -- replace the construct by an integer literal of the correct type. We
7953 -- only apply this to integer types being converted. Possibly it may
7954 -- apply in other cases, but it is too much trouble to worry about.
7955
7956 -- Note that we do not do this transformation if the Kill_Range_Check
7957 -- flag is set, since then the value may be outside the expected range.
7958 -- This happens in the Normalize_Scalars case.
7959
7960 -- We also skip this if either the target or operand type is biased
7961 -- because in this case, the unchecked conversion is supposed to
7962 -- preserve the bit pattern, not the integer value.
7963
7964 if Is_Integer_Type (Target_Type)
7965 and then not Has_Biased_Representation (Target_Type)
7966 and then Is_Integer_Type (Operand_Type)
7967 and then not Has_Biased_Representation (Operand_Type)
7968 and then Compile_Time_Known_Value (Operand)
7969 and then not Kill_Range_Check (N)
7970 then
7971 declare
7972 Val : constant Uint := Expr_Value (Operand);
7973
7974 begin
7975 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
7976 and then
7977 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
7978 and then
7979 Val >= Expr_Value (Type_Low_Bound (Target_Type))
7980 and then
7981 Val <= Expr_Value (Type_High_Bound (Target_Type))
7982 then
7983 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
7984
7985 -- If Address is the target type, just set the type
7986 -- to avoid a spurious type error on the literal when
7987 -- Address is a visible integer type.
7988
7989 if Is_Descendent_Of_Address (Target_Type) then
7990 Set_Etype (N, Target_Type);
7991 else
7992 Analyze_And_Resolve (N, Target_Type);
7993 end if;
7994
7995 return;
7996 end if;
7997 end;
7998 end if;
7999
8000 -- Nothing to do if conversion is safe
8001
8002 if Safe_Unchecked_Type_Conversion (N) then
8003 return;
8004 end if;
8005
8006 -- Otherwise force evaluation unless Assignment_OK flag is set (this
8007 -- flag indicates ??? -- more comments needed here)
8008
8009 if Assignment_OK (N) then
8010 null;
8011 else
8012 Force_Evaluation (N);
8013 end if;
8014 end Expand_N_Unchecked_Type_Conversion;
8015
8016 ----------------------------
8017 -- Expand_Record_Equality --
8018 ----------------------------
8019
8020 -- For non-variant records, Equality is expanded when needed into:
8021
8022 -- and then Lhs.Discr1 = Rhs.Discr1
8023 -- and then ...
8024 -- and then Lhs.Discrn = Rhs.Discrn
8025 -- and then Lhs.Cmp1 = Rhs.Cmp1
8026 -- and then ...
8027 -- and then Lhs.Cmpn = Rhs.Cmpn
8028
8029 -- The expression is folded by the back-end for adjacent fields. This
8030 -- function is called for tagged record in only one occasion: for imple-
8031 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
8032 -- otherwise the primitive "=" is used directly.
8033
8034 function Expand_Record_Equality
8035 (Nod : Node_Id;
8036 Typ : Entity_Id;
8037 Lhs : Node_Id;
8038 Rhs : Node_Id;
8039 Bodies : List_Id) return Node_Id
8040 is
8041 Loc : constant Source_Ptr := Sloc (Nod);
8042
8043 Result : Node_Id;
8044 C : Entity_Id;
8045
8046 First_Time : Boolean := True;
8047
8048 function Suitable_Element (C : Entity_Id) return Entity_Id;
8049 -- Return the first field to compare beginning with C, skipping the
8050 -- inherited components.
8051
8052 ----------------------
8053 -- Suitable_Element --
8054 ----------------------
8055
8056 function Suitable_Element (C : Entity_Id) return Entity_Id is
8057 begin
8058 if No (C) then
8059 return Empty;
8060
8061 elsif Ekind (C) /= E_Discriminant
8062 and then Ekind (C) /= E_Component
8063 then
8064 return Suitable_Element (Next_Entity (C));
8065
8066 elsif Is_Tagged_Type (Typ)
8067 and then C /= Original_Record_Component (C)
8068 then
8069 return Suitable_Element (Next_Entity (C));
8070
8071 elsif Chars (C) = Name_uController
8072 or else Chars (C) = Name_uTag
8073 then
8074 return Suitable_Element (Next_Entity (C));
8075
8076 elsif Is_Interface (Etype (C)) then
8077 return Suitable_Element (Next_Entity (C));
8078
8079 else
8080 return C;
8081 end if;
8082 end Suitable_Element;
8083
8084 -- Start of processing for Expand_Record_Equality
8085
8086 begin
8087 -- Generates the following code: (assuming that Typ has one Discr and
8088 -- component C2 is also a record)
8089
8090 -- True
8091 -- and then Lhs.Discr1 = Rhs.Discr1
8092 -- and then Lhs.C1 = Rhs.C1
8093 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
8094 -- and then ...
8095 -- and then Lhs.Cmpn = Rhs.Cmpn
8096
8097 Result := New_Reference_To (Standard_True, Loc);
8098 C := Suitable_Element (First_Entity (Typ));
8099
8100 while Present (C) loop
8101 declare
8102 New_Lhs : Node_Id;
8103 New_Rhs : Node_Id;
8104 Check : Node_Id;
8105
8106 begin
8107 if First_Time then
8108 First_Time := False;
8109 New_Lhs := Lhs;
8110 New_Rhs := Rhs;
8111 else
8112 New_Lhs := New_Copy_Tree (Lhs);
8113 New_Rhs := New_Copy_Tree (Rhs);
8114 end if;
8115
8116 Check :=
8117 Expand_Composite_Equality (Nod, Etype (C),
8118 Lhs =>
8119 Make_Selected_Component (Loc,
8120 Prefix => New_Lhs,
8121 Selector_Name => New_Reference_To (C, Loc)),
8122 Rhs =>
8123 Make_Selected_Component (Loc,
8124 Prefix => New_Rhs,
8125 Selector_Name => New_Reference_To (C, Loc)),
8126 Bodies => Bodies);
8127
8128 -- If some (sub)component is an unchecked_union, the whole
8129 -- operation will raise program error.
8130
8131 if Nkind (Check) = N_Raise_Program_Error then
8132 Result := Check;
8133 Set_Etype (Result, Standard_Boolean);
8134 exit;
8135 else
8136 Result :=
8137 Make_And_Then (Loc,
8138 Left_Opnd => Result,
8139 Right_Opnd => Check);
8140 end if;
8141 end;
8142
8143 C := Suitable_Element (Next_Entity (C));
8144 end loop;
8145
8146 return Result;
8147 end Expand_Record_Equality;
8148
8149 -------------------------------------
8150 -- Fixup_Universal_Fixed_Operation --
8151 -------------------------------------
8152
8153 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
8154 Conv : constant Node_Id := Parent (N);
8155
8156 begin
8157 -- We must have a type conversion immediately above us
8158
8159 pragma Assert (Nkind (Conv) = N_Type_Conversion);
8160
8161 -- Normally the type conversion gives our target type. The exception
8162 -- occurs in the case of the Round attribute, where the conversion
8163 -- will be to universal real, and our real type comes from the Round
8164 -- attribute (as well as an indication that we must round the result)
8165
8166 if Nkind (Parent (Conv)) = N_Attribute_Reference
8167 and then Attribute_Name (Parent (Conv)) = Name_Round
8168 then
8169 Set_Etype (N, Etype (Parent (Conv)));
8170 Set_Rounded_Result (N);
8171
8172 -- Normal case where type comes from conversion above us
8173
8174 else
8175 Set_Etype (N, Etype (Conv));
8176 end if;
8177 end Fixup_Universal_Fixed_Operation;
8178
8179 ------------------------------
8180 -- Get_Allocator_Final_List --
8181 ------------------------------
8182
8183 function Get_Allocator_Final_List
8184 (N : Node_Id;
8185 T : Entity_Id;
8186 PtrT : Entity_Id) return Entity_Id
8187 is
8188 Loc : constant Source_Ptr := Sloc (N);
8189
8190 Owner : Entity_Id := PtrT;
8191 -- The entity whose finalization list must be used to attach the
8192 -- allocated object.
8193
8194 begin
8195 if Ekind (PtrT) = E_Anonymous_Access_Type then
8196
8197 -- If the context is an access parameter, we need to create a
8198 -- non-anonymous access type in order to have a usable final list,
8199 -- because there is otherwise no pool to which the allocated object
8200 -- can belong. We create both the type and the finalization chain
8201 -- here, because freezing an internal type does not create such a
8202 -- chain. The Final_Chain that is thus created is shared by the
8203 -- access parameter. The access type is tested against the result
8204 -- type of the function to exclude allocators whose type is an
8205 -- anonymous access result type.
8206
8207 if Nkind (Associated_Node_For_Itype (PtrT))
8208 in N_Subprogram_Specification
8209 and then
8210 PtrT /=
8211 Etype (Defining_Unit_Name (Associated_Node_For_Itype (PtrT)))
8212 then
8213 Owner := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
8214 Insert_Action (N,
8215 Make_Full_Type_Declaration (Loc,
8216 Defining_Identifier => Owner,
8217 Type_Definition =>
8218 Make_Access_To_Object_Definition (Loc,
8219 Subtype_Indication =>
8220 New_Occurrence_Of (T, Loc))));
8221
8222 Build_Final_List (N, Owner);
8223 Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner));
8224
8225 -- Ada 2005 (AI-318-02): If the context is a return object
8226 -- declaration, then the anonymous return subtype is defined to have
8227 -- the same accessibility level as that of the function's result
8228 -- subtype, which means that we want the scope where the function is
8229 -- declared.
8230
8231 elsif Nkind (Associated_Node_For_Itype (PtrT)) = N_Object_Declaration
8232 and then Ekind (Scope (PtrT)) = E_Return_Statement
8233 then
8234 Owner := Scope (Return_Applies_To (Scope (PtrT)));
8235
8236 -- Case of an access discriminant, or (Ada 2005), of an anonymous
8237 -- access component or anonymous access function result: find the
8238 -- final list associated with the scope of the type. (In the
8239 -- anonymous access component kind, a list controller will have
8240 -- been allocated when freezing the record type, and PtrT has an
8241 -- Associated_Final_Chain attribute designating it.)
8242
8243 elsif No (Associated_Final_Chain (PtrT)) then
8244 Owner := Scope (PtrT);
8245 end if;
8246 end if;
8247
8248 return Find_Final_List (Owner);
8249 end Get_Allocator_Final_List;
8250
8251 ---------------------------------
8252 -- Has_Inferable_Discriminants --
8253 ---------------------------------
8254
8255 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
8256
8257 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
8258 -- Determines whether the left-most prefix of a selected component is a
8259 -- formal parameter in a subprogram. Assumes N is a selected component.
8260
8261 --------------------------------
8262 -- Prefix_Is_Formal_Parameter --
8263 --------------------------------
8264
8265 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
8266 Sel_Comp : Node_Id := N;
8267
8268 begin
8269 -- Move to the left-most prefix by climbing up the tree
8270
8271 while Present (Parent (Sel_Comp))
8272 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
8273 loop
8274 Sel_Comp := Parent (Sel_Comp);
8275 end loop;
8276
8277 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
8278 end Prefix_Is_Formal_Parameter;
8279
8280 -- Start of processing for Has_Inferable_Discriminants
8281
8282 begin
8283 -- For identifiers and indexed components, it is sufficent to have a
8284 -- constrained Unchecked_Union nominal subtype.
8285
8286 if Nkind_In (N, N_Identifier, N_Indexed_Component) then
8287 return Is_Unchecked_Union (Base_Type (Etype (N)))
8288 and then
8289 Is_Constrained (Etype (N));
8290
8291 -- For selected components, the subtype of the selector must be a
8292 -- constrained Unchecked_Union. If the component is subject to a
8293 -- per-object constraint, then the enclosing object must have inferable
8294 -- discriminants.
8295
8296 elsif Nkind (N) = N_Selected_Component then
8297 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
8298
8299 -- A small hack. If we have a per-object constrained selected
8300 -- component of a formal parameter, return True since we do not
8301 -- know the actual parameter association yet.
8302
8303 if Prefix_Is_Formal_Parameter (N) then
8304 return True;
8305 end if;
8306
8307 -- Otherwise, check the enclosing object and the selector
8308
8309 return Has_Inferable_Discriminants (Prefix (N))
8310 and then
8311 Has_Inferable_Discriminants (Selector_Name (N));
8312 end if;
8313
8314 -- The call to Has_Inferable_Discriminants will determine whether
8315 -- the selector has a constrained Unchecked_Union nominal type.
8316
8317 return Has_Inferable_Discriminants (Selector_Name (N));
8318
8319 -- A qualified expression has inferable discriminants if its subtype
8320 -- mark is a constrained Unchecked_Union subtype.
8321
8322 elsif Nkind (N) = N_Qualified_Expression then
8323 return Is_Unchecked_Union (Subtype_Mark (N))
8324 and then
8325 Is_Constrained (Subtype_Mark (N));
8326
8327 end if;
8328
8329 return False;
8330 end Has_Inferable_Discriminants;
8331
8332 -------------------------------
8333 -- Insert_Dereference_Action --
8334 -------------------------------
8335
8336 procedure Insert_Dereference_Action (N : Node_Id) is
8337 Loc : constant Source_Ptr := Sloc (N);
8338 Typ : constant Entity_Id := Etype (N);
8339 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
8340 Pnod : constant Node_Id := Parent (N);
8341
8342 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
8343 -- Return true if type of P is derived from Checked_Pool;
8344
8345 -----------------------------
8346 -- Is_Checked_Storage_Pool --
8347 -----------------------------
8348
8349 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
8350 T : Entity_Id;
8351
8352 begin
8353 if No (P) then
8354 return False;
8355 end if;
8356
8357 T := Etype (P);
8358 while T /= Etype (T) loop
8359 if Is_RTE (T, RE_Checked_Pool) then
8360 return True;
8361 else
8362 T := Etype (T);
8363 end if;
8364 end loop;
8365
8366 return False;
8367 end Is_Checked_Storage_Pool;
8368
8369 -- Start of processing for Insert_Dereference_Action
8370
8371 begin
8372 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
8373
8374 if not (Is_Checked_Storage_Pool (Pool)
8375 and then Comes_From_Source (Original_Node (Pnod)))
8376 then
8377 return;
8378 end if;
8379
8380 Insert_Action (N,
8381 Make_Procedure_Call_Statement (Loc,
8382 Name => New_Reference_To (
8383 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
8384
8385 Parameter_Associations => New_List (
8386
8387 -- Pool
8388
8389 New_Reference_To (Pool, Loc),
8390
8391 -- Storage_Address. We use the attribute Pool_Address,
8392 -- which uses the pointer itself to find the address of
8393 -- the object, and which handles unconstrained arrays
8394 -- properly by computing the address of the template.
8395 -- i.e. the correct address of the corresponding allocation.
8396
8397 Make_Attribute_Reference (Loc,
8398 Prefix => Duplicate_Subexpr_Move_Checks (N),
8399 Attribute_Name => Name_Pool_Address),
8400
8401 -- Size_In_Storage_Elements
8402
8403 Make_Op_Divide (Loc,
8404 Left_Opnd =>
8405 Make_Attribute_Reference (Loc,
8406 Prefix =>
8407 Make_Explicit_Dereference (Loc,
8408 Duplicate_Subexpr_Move_Checks (N)),
8409 Attribute_Name => Name_Size),
8410 Right_Opnd =>
8411 Make_Integer_Literal (Loc, System_Storage_Unit)),
8412
8413 -- Alignment
8414
8415 Make_Attribute_Reference (Loc,
8416 Prefix =>
8417 Make_Explicit_Dereference (Loc,
8418 Duplicate_Subexpr_Move_Checks (N)),
8419 Attribute_Name => Name_Alignment))));
8420
8421 exception
8422 when RE_Not_Available =>
8423 return;
8424 end Insert_Dereference_Action;
8425
8426 ------------------------------
8427 -- Make_Array_Comparison_Op --
8428 ------------------------------
8429
8430 -- This is a hand-coded expansion of the following generic function:
8431
8432 -- generic
8433 -- type elem is (<>);
8434 -- type index is (<>);
8435 -- type a is array (index range <>) of elem;
8436
8437 -- function Gnnn (X : a; Y: a) return boolean is
8438 -- J : index := Y'first;
8439
8440 -- begin
8441 -- if X'length = 0 then
8442 -- return false;
8443
8444 -- elsif Y'length = 0 then
8445 -- return true;
8446
8447 -- else
8448 -- for I in X'range loop
8449 -- if X (I) = Y (J) then
8450 -- if J = Y'last then
8451 -- exit;
8452 -- else
8453 -- J := index'succ (J);
8454 -- end if;
8455
8456 -- else
8457 -- return X (I) > Y (J);
8458 -- end if;
8459 -- end loop;
8460
8461 -- return X'length > Y'length;
8462 -- end if;
8463 -- end Gnnn;
8464
8465 -- Note that since we are essentially doing this expansion by hand, we
8466 -- do not need to generate an actual or formal generic part, just the
8467 -- instantiated function itself.
8468
8469 function Make_Array_Comparison_Op
8470 (Typ : Entity_Id;
8471 Nod : Node_Id) return Node_Id
8472 is
8473 Loc : constant Source_Ptr := Sloc (Nod);
8474
8475 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
8476 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
8477 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
8478 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
8479
8480 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
8481
8482 Loop_Statement : Node_Id;
8483 Loop_Body : Node_Id;
8484 If_Stat : Node_Id;
8485 Inner_If : Node_Id;
8486 Final_Expr : Node_Id;
8487 Func_Body : Node_Id;
8488 Func_Name : Entity_Id;
8489 Formals : List_Id;
8490 Length1 : Node_Id;
8491 Length2 : Node_Id;
8492
8493 begin
8494 -- if J = Y'last then
8495 -- exit;
8496 -- else
8497 -- J := index'succ (J);
8498 -- end if;
8499
8500 Inner_If :=
8501 Make_Implicit_If_Statement (Nod,
8502 Condition =>
8503 Make_Op_Eq (Loc,
8504 Left_Opnd => New_Reference_To (J, Loc),
8505 Right_Opnd =>
8506 Make_Attribute_Reference (Loc,
8507 Prefix => New_Reference_To (Y, Loc),
8508 Attribute_Name => Name_Last)),
8509
8510 Then_Statements => New_List (
8511 Make_Exit_Statement (Loc)),
8512
8513 Else_Statements =>
8514 New_List (
8515 Make_Assignment_Statement (Loc,
8516 Name => New_Reference_To (J, Loc),
8517 Expression =>
8518 Make_Attribute_Reference (Loc,
8519 Prefix => New_Reference_To (Index, Loc),
8520 Attribute_Name => Name_Succ,
8521 Expressions => New_List (New_Reference_To (J, Loc))))));
8522
8523 -- if X (I) = Y (J) then
8524 -- if ... end if;
8525 -- else
8526 -- return X (I) > Y (J);
8527 -- end if;
8528
8529 Loop_Body :=
8530 Make_Implicit_If_Statement (Nod,
8531 Condition =>
8532 Make_Op_Eq (Loc,
8533 Left_Opnd =>
8534 Make_Indexed_Component (Loc,
8535 Prefix => New_Reference_To (X, Loc),
8536 Expressions => New_List (New_Reference_To (I, Loc))),
8537
8538 Right_Opnd =>
8539 Make_Indexed_Component (Loc,
8540 Prefix => New_Reference_To (Y, Loc),
8541 Expressions => New_List (New_Reference_To (J, Loc)))),
8542
8543 Then_Statements => New_List (Inner_If),
8544
8545 Else_Statements => New_List (
8546 Make_Simple_Return_Statement (Loc,
8547 Expression =>
8548 Make_Op_Gt (Loc,
8549 Left_Opnd =>
8550 Make_Indexed_Component (Loc,
8551 Prefix => New_Reference_To (X, Loc),
8552 Expressions => New_List (New_Reference_To (I, Loc))),
8553
8554 Right_Opnd =>
8555 Make_Indexed_Component (Loc,
8556 Prefix => New_Reference_To (Y, Loc),
8557 Expressions => New_List (
8558 New_Reference_To (J, Loc)))))));
8559
8560 -- for I in X'range loop
8561 -- if ... end if;
8562 -- end loop;
8563
8564 Loop_Statement :=
8565 Make_Implicit_Loop_Statement (Nod,
8566 Identifier => Empty,
8567
8568 Iteration_Scheme =>
8569 Make_Iteration_Scheme (Loc,
8570 Loop_Parameter_Specification =>
8571 Make_Loop_Parameter_Specification (Loc,
8572 Defining_Identifier => I,
8573 Discrete_Subtype_Definition =>
8574 Make_Attribute_Reference (Loc,
8575 Prefix => New_Reference_To (X, Loc),
8576 Attribute_Name => Name_Range))),
8577
8578 Statements => New_List (Loop_Body));
8579
8580 -- if X'length = 0 then
8581 -- return false;
8582 -- elsif Y'length = 0 then
8583 -- return true;
8584 -- else
8585 -- for ... loop ... end loop;
8586 -- return X'length > Y'length;
8587 -- end if;
8588
8589 Length1 :=
8590 Make_Attribute_Reference (Loc,
8591 Prefix => New_Reference_To (X, Loc),
8592 Attribute_Name => Name_Length);
8593
8594 Length2 :=
8595 Make_Attribute_Reference (Loc,
8596 Prefix => New_Reference_To (Y, Loc),
8597 Attribute_Name => Name_Length);
8598
8599 Final_Expr :=
8600 Make_Op_Gt (Loc,
8601 Left_Opnd => Length1,
8602 Right_Opnd => Length2);
8603
8604 If_Stat :=
8605 Make_Implicit_If_Statement (Nod,
8606 Condition =>
8607 Make_Op_Eq (Loc,
8608 Left_Opnd =>
8609 Make_Attribute_Reference (Loc,
8610 Prefix => New_Reference_To (X, Loc),
8611 Attribute_Name => Name_Length),
8612 Right_Opnd =>
8613 Make_Integer_Literal (Loc, 0)),
8614
8615 Then_Statements =>
8616 New_List (
8617 Make_Simple_Return_Statement (Loc,
8618 Expression => New_Reference_To (Standard_False, Loc))),
8619
8620 Elsif_Parts => New_List (
8621 Make_Elsif_Part (Loc,
8622 Condition =>
8623 Make_Op_Eq (Loc,
8624 Left_Opnd =>
8625 Make_Attribute_Reference (Loc,
8626 Prefix => New_Reference_To (Y, Loc),
8627 Attribute_Name => Name_Length),
8628 Right_Opnd =>
8629 Make_Integer_Literal (Loc, 0)),
8630
8631 Then_Statements =>
8632 New_List (
8633 Make_Simple_Return_Statement (Loc,
8634 Expression => New_Reference_To (Standard_True, Loc))))),
8635
8636 Else_Statements => New_List (
8637 Loop_Statement,
8638 Make_Simple_Return_Statement (Loc,
8639 Expression => Final_Expr)));
8640
8641 -- (X : a; Y: a)
8642
8643 Formals := New_List (
8644 Make_Parameter_Specification (Loc,
8645 Defining_Identifier => X,
8646 Parameter_Type => New_Reference_To (Typ, Loc)),
8647
8648 Make_Parameter_Specification (Loc,
8649 Defining_Identifier => Y,
8650 Parameter_Type => New_Reference_To (Typ, Loc)));
8651
8652 -- function Gnnn (...) return boolean is
8653 -- J : index := Y'first;
8654 -- begin
8655 -- if ... end if;
8656 -- end Gnnn;
8657
8658 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('G'));
8659
8660 Func_Body :=
8661 Make_Subprogram_Body (Loc,
8662 Specification =>
8663 Make_Function_Specification (Loc,
8664 Defining_Unit_Name => Func_Name,
8665 Parameter_Specifications => Formals,
8666 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
8667
8668 Declarations => New_List (
8669 Make_Object_Declaration (Loc,
8670 Defining_Identifier => J,
8671 Object_Definition => New_Reference_To (Index, Loc),
8672 Expression =>
8673 Make_Attribute_Reference (Loc,
8674 Prefix => New_Reference_To (Y, Loc),
8675 Attribute_Name => Name_First))),
8676
8677 Handled_Statement_Sequence =>
8678 Make_Handled_Sequence_Of_Statements (Loc,
8679 Statements => New_List (If_Stat)));
8680
8681 return Func_Body;
8682 end Make_Array_Comparison_Op;
8683
8684 ---------------------------
8685 -- Make_Boolean_Array_Op --
8686 ---------------------------
8687
8688 -- For logical operations on boolean arrays, expand in line the
8689 -- following, replacing 'and' with 'or' or 'xor' where needed:
8690
8691 -- function Annn (A : typ; B: typ) return typ is
8692 -- C : typ;
8693 -- begin
8694 -- for J in A'range loop
8695 -- C (J) := A (J) op B (J);
8696 -- end loop;
8697 -- return C;
8698 -- end Annn;
8699
8700 -- Here typ is the boolean array type
8701
8702 function Make_Boolean_Array_Op
8703 (Typ : Entity_Id;
8704 N : Node_Id) return Node_Id
8705 is
8706 Loc : constant Source_Ptr := Sloc (N);
8707
8708 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
8709 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
8710 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
8711 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
8712
8713 A_J : Node_Id;
8714 B_J : Node_Id;
8715 C_J : Node_Id;
8716 Op : Node_Id;
8717
8718 Formals : List_Id;
8719 Func_Name : Entity_Id;
8720 Func_Body : Node_Id;
8721 Loop_Statement : Node_Id;
8722
8723 begin
8724 A_J :=
8725 Make_Indexed_Component (Loc,
8726 Prefix => New_Reference_To (A, Loc),
8727 Expressions => New_List (New_Reference_To (J, Loc)));
8728
8729 B_J :=
8730 Make_Indexed_Component (Loc,
8731 Prefix => New_Reference_To (B, Loc),
8732 Expressions => New_List (New_Reference_To (J, Loc)));
8733
8734 C_J :=
8735 Make_Indexed_Component (Loc,
8736 Prefix => New_Reference_To (C, Loc),
8737 Expressions => New_List (New_Reference_To (J, Loc)));
8738
8739 if Nkind (N) = N_Op_And then
8740 Op :=
8741 Make_Op_And (Loc,
8742 Left_Opnd => A_J,
8743 Right_Opnd => B_J);
8744
8745 elsif Nkind (N) = N_Op_Or then
8746 Op :=
8747 Make_Op_Or (Loc,
8748 Left_Opnd => A_J,
8749 Right_Opnd => B_J);
8750
8751 else
8752 Op :=
8753 Make_Op_Xor (Loc,
8754 Left_Opnd => A_J,
8755 Right_Opnd => B_J);
8756 end if;
8757
8758 Loop_Statement :=
8759 Make_Implicit_Loop_Statement (N,
8760 Identifier => Empty,
8761
8762 Iteration_Scheme =>
8763 Make_Iteration_Scheme (Loc,
8764 Loop_Parameter_Specification =>
8765 Make_Loop_Parameter_Specification (Loc,
8766 Defining_Identifier => J,
8767 Discrete_Subtype_Definition =>
8768 Make_Attribute_Reference (Loc,
8769 Prefix => New_Reference_To (A, Loc),
8770 Attribute_Name => Name_Range))),
8771
8772 Statements => New_List (
8773 Make_Assignment_Statement (Loc,
8774 Name => C_J,
8775 Expression => Op)));
8776
8777 Formals := New_List (
8778 Make_Parameter_Specification (Loc,
8779 Defining_Identifier => A,
8780 Parameter_Type => New_Reference_To (Typ, Loc)),
8781
8782 Make_Parameter_Specification (Loc,
8783 Defining_Identifier => B,
8784 Parameter_Type => New_Reference_To (Typ, Loc)));
8785
8786 Func_Name :=
8787 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
8788 Set_Is_Inlined (Func_Name);
8789
8790 Func_Body :=
8791 Make_Subprogram_Body (Loc,
8792 Specification =>
8793 Make_Function_Specification (Loc,
8794 Defining_Unit_Name => Func_Name,
8795 Parameter_Specifications => Formals,
8796 Result_Definition => New_Reference_To (Typ, Loc)),
8797
8798 Declarations => New_List (
8799 Make_Object_Declaration (Loc,
8800 Defining_Identifier => C,
8801 Object_Definition => New_Reference_To (Typ, Loc))),
8802
8803 Handled_Statement_Sequence =>
8804 Make_Handled_Sequence_Of_Statements (Loc,
8805 Statements => New_List (
8806 Loop_Statement,
8807 Make_Simple_Return_Statement (Loc,
8808 Expression => New_Reference_To (C, Loc)))));
8809
8810 return Func_Body;
8811 end Make_Boolean_Array_Op;
8812
8813 ------------------------
8814 -- Rewrite_Comparison --
8815 ------------------------
8816
8817 procedure Rewrite_Comparison (N : Node_Id) is
8818 begin
8819 if Nkind (N) = N_Type_Conversion then
8820 Rewrite_Comparison (Expression (N));
8821 return;
8822
8823 elsif Nkind (N) not in N_Op_Compare then
8824 return;
8825 end if;
8826
8827 declare
8828 Typ : constant Entity_Id := Etype (N);
8829 Op1 : constant Node_Id := Left_Opnd (N);
8830 Op2 : constant Node_Id := Right_Opnd (N);
8831
8832 Res : constant Compare_Result := Compile_Time_Compare (Op1, Op2);
8833 -- Res indicates if compare outcome can be compile time determined
8834
8835 True_Result : Boolean;
8836 False_Result : Boolean;
8837
8838 begin
8839 case N_Op_Compare (Nkind (N)) is
8840 when N_Op_Eq =>
8841 True_Result := Res = EQ;
8842 False_Result := Res = LT or else Res = GT or else Res = NE;
8843
8844 when N_Op_Ge =>
8845 True_Result := Res in Compare_GE;
8846 False_Result := Res = LT;
8847
8848 if Res = LE
8849 and then Constant_Condition_Warnings
8850 and then Comes_From_Source (Original_Node (N))
8851 and then Nkind (Original_Node (N)) = N_Op_Ge
8852 and then not In_Instance
8853 and then not Warnings_Off (Etype (Left_Opnd (N)))
8854 and then Is_Integer_Type (Etype (Left_Opnd (N)))
8855 then
8856 Error_Msg_N
8857 ("can never be greater than, could replace by ""'=""?", N);
8858 end if;
8859
8860 when N_Op_Gt =>
8861 True_Result := Res = GT;
8862 False_Result := Res in Compare_LE;
8863
8864 when N_Op_Lt =>
8865 True_Result := Res = LT;
8866 False_Result := Res in Compare_GE;
8867
8868 when N_Op_Le =>
8869 True_Result := Res in Compare_LE;
8870 False_Result := Res = GT;
8871
8872 if Res = GE
8873 and then Constant_Condition_Warnings
8874 and then Comes_From_Source (Original_Node (N))
8875 and then Nkind (Original_Node (N)) = N_Op_Le
8876 and then not In_Instance
8877 and then not Warnings_Off (Etype (Left_Opnd (N)))
8878 and then Is_Integer_Type (Etype (Left_Opnd (N)))
8879 then
8880 Error_Msg_N
8881 ("can never be less than, could replace by ""'=""?", N);
8882 end if;
8883
8884 when N_Op_Ne =>
8885 True_Result := Res = NE or else Res = GT or else Res = LT;
8886 False_Result := Res = EQ;
8887 end case;
8888
8889 if True_Result then
8890 Rewrite (N,
8891 Convert_To (Typ,
8892 New_Occurrence_Of (Standard_True, Sloc (N))));
8893 Analyze_And_Resolve (N, Typ);
8894 Warn_On_Known_Condition (N);
8895
8896 elsif False_Result then
8897 Rewrite (N,
8898 Convert_To (Typ,
8899 New_Occurrence_Of (Standard_False, Sloc (N))));
8900 Analyze_And_Resolve (N, Typ);
8901 Warn_On_Known_Condition (N);
8902 end if;
8903 end;
8904 end Rewrite_Comparison;
8905
8906 ----------------------------
8907 -- Safe_In_Place_Array_Op --
8908 ----------------------------
8909
8910 function Safe_In_Place_Array_Op
8911 (Lhs : Node_Id;
8912 Op1 : Node_Id;
8913 Op2 : Node_Id) return Boolean
8914 is
8915 Target : Entity_Id;
8916
8917 function Is_Safe_Operand (Op : Node_Id) return Boolean;
8918 -- Operand is safe if it cannot overlap part of the target of the
8919 -- operation. If the operand and the target are identical, the operand
8920 -- is safe. The operand can be empty in the case of negation.
8921
8922 function Is_Unaliased (N : Node_Id) return Boolean;
8923 -- Check that N is a stand-alone entity
8924
8925 ------------------
8926 -- Is_Unaliased --
8927 ------------------
8928
8929 function Is_Unaliased (N : Node_Id) return Boolean is
8930 begin
8931 return
8932 Is_Entity_Name (N)
8933 and then No (Address_Clause (Entity (N)))
8934 and then No (Renamed_Object (Entity (N)));
8935 end Is_Unaliased;
8936
8937 ---------------------
8938 -- Is_Safe_Operand --
8939 ---------------------
8940
8941 function Is_Safe_Operand (Op : Node_Id) return Boolean is
8942 begin
8943 if No (Op) then
8944 return True;
8945
8946 elsif Is_Entity_Name (Op) then
8947 return Is_Unaliased (Op);
8948
8949 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
8950 return Is_Unaliased (Prefix (Op));
8951
8952 elsif Nkind (Op) = N_Slice then
8953 return
8954 Is_Unaliased (Prefix (Op))
8955 and then Entity (Prefix (Op)) /= Target;
8956
8957 elsif Nkind (Op) = N_Op_Not then
8958 return Is_Safe_Operand (Right_Opnd (Op));
8959
8960 else
8961 return False;
8962 end if;
8963 end Is_Safe_Operand;
8964
8965 -- Start of processing for Is_Safe_In_Place_Array_Op
8966
8967 begin
8968 -- We skip this processing if the component size is not the
8969 -- same as a system storage unit (since at least for NOT
8970 -- this would cause problems).
8971
8972 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
8973 return False;
8974
8975 -- Cannot do in place stuff on VM_Target since cannot pass addresses
8976
8977 elsif VM_Target /= No_VM then
8978 return False;
8979
8980 -- Cannot do in place stuff if non-standard Boolean representation
8981
8982 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
8983 return False;
8984
8985 elsif not Is_Unaliased (Lhs) then
8986 return False;
8987 else
8988 Target := Entity (Lhs);
8989
8990 return
8991 Is_Safe_Operand (Op1)
8992 and then Is_Safe_Operand (Op2);
8993 end if;
8994 end Safe_In_Place_Array_Op;
8995
8996 -----------------------
8997 -- Tagged_Membership --
8998 -----------------------
8999
9000 -- There are two different cases to consider depending on whether
9001 -- the right operand is a class-wide type or not. If not we just
9002 -- compare the actual tag of the left expr to the target type tag:
9003 --
9004 -- Left_Expr.Tag = Right_Type'Tag;
9005 --
9006 -- If it is a class-wide type we use the RT function CW_Membership which
9007 -- is usually implemented by looking in the ancestor tables contained in
9008 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
9009
9010 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
9011 -- function IW_Membership which is usually implemented by looking in the
9012 -- table of abstract interface types plus the ancestor table contained in
9013 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
9014
9015 function Tagged_Membership (N : Node_Id) return Node_Id is
9016 Left : constant Node_Id := Left_Opnd (N);
9017 Right : constant Node_Id := Right_Opnd (N);
9018 Loc : constant Source_Ptr := Sloc (N);
9019
9020 Left_Type : Entity_Id;
9021 Right_Type : Entity_Id;
9022 Obj_Tag : Node_Id;
9023
9024 begin
9025 Left_Type := Etype (Left);
9026 Right_Type := Etype (Right);
9027
9028 if Is_Class_Wide_Type (Left_Type) then
9029 Left_Type := Root_Type (Left_Type);
9030 end if;
9031
9032 Obj_Tag :=
9033 Make_Selected_Component (Loc,
9034 Prefix => Relocate_Node (Left),
9035 Selector_Name =>
9036 New_Reference_To (First_Tag_Component (Left_Type), Loc));
9037
9038 if Is_Class_Wide_Type (Right_Type) then
9039
9040 -- No need to issue a run-time check if we statically know that the
9041 -- result of this membership test is always true. For example,
9042 -- considering the following declarations:
9043
9044 -- type Iface is interface;
9045 -- type T is tagged null record;
9046 -- type DT is new T and Iface with null record;
9047
9048 -- Obj1 : T;
9049 -- Obj2 : DT;
9050
9051 -- These membership tests are always true:
9052
9053 -- Obj1 in T'Class
9054 -- Obj2 in T'Class;
9055 -- Obj2 in Iface'Class;
9056
9057 -- We do not need to handle cases where the membership is illegal.
9058 -- For example:
9059
9060 -- Obj1 in DT'Class; -- Compile time error
9061 -- Obj1 in Iface'Class; -- Compile time error
9062
9063 if not Is_Class_Wide_Type (Left_Type)
9064 and then (Is_Parent (Etype (Right_Type), Left_Type)
9065 or else (Is_Interface (Etype (Right_Type))
9066 and then Interface_Present_In_Ancestor
9067 (Typ => Left_Type,
9068 Iface => Etype (Right_Type))))
9069 then
9070 return New_Reference_To (Standard_True, Loc);
9071 end if;
9072
9073 -- Ada 2005 (AI-251): Class-wide applied to interfaces
9074
9075 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
9076
9077 -- Support to: "Iface_CW_Typ in Typ'Class"
9078
9079 or else Is_Interface (Left_Type)
9080 then
9081 -- Issue error if IW_Membership operation not available in a
9082 -- configurable run time setting.
9083
9084 if not RTE_Available (RE_IW_Membership) then
9085 Error_Msg_CRT ("abstract interface types", N);
9086 return Empty;
9087 end if;
9088
9089 return
9090 Make_Function_Call (Loc,
9091 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
9092 Parameter_Associations => New_List (
9093 Make_Attribute_Reference (Loc,
9094 Prefix => Obj_Tag,
9095 Attribute_Name => Name_Address),
9096 New_Reference_To (
9097 Node (First_Elmt
9098 (Access_Disp_Table (Root_Type (Right_Type)))),
9099 Loc)));
9100
9101 -- Ada 95: Normal case
9102
9103 else
9104 return
9105 Build_CW_Membership (Loc,
9106 Obj_Tag_Node => Obj_Tag,
9107 Typ_Tag_Node =>
9108 New_Reference_To (
9109 Node (First_Elmt
9110 (Access_Disp_Table (Root_Type (Right_Type)))),
9111 Loc));
9112 end if;
9113
9114 -- Right_Type is not a class-wide type
9115
9116 else
9117 -- No need to check the tag of the object if Right_Typ is abstract
9118
9119 if Is_Abstract_Type (Right_Type) then
9120 return New_Reference_To (Standard_False, Loc);
9121
9122 else
9123 return
9124 Make_Op_Eq (Loc,
9125 Left_Opnd => Obj_Tag,
9126 Right_Opnd =>
9127 New_Reference_To
9128 (Node (First_Elmt (Access_Disp_Table (Right_Type))), Loc));
9129 end if;
9130 end if;
9131 end Tagged_Membership;
9132
9133 ------------------------------
9134 -- Unary_Op_Validity_Checks --
9135 ------------------------------
9136
9137 procedure Unary_Op_Validity_Checks (N : Node_Id) is
9138 begin
9139 if Validity_Checks_On and Validity_Check_Operands then
9140 Ensure_Valid (Right_Opnd (N));
9141 end if;
9142 end Unary_Op_Validity_Checks;
9143
9144 end Exp_Ch4;