9a86fb48360f6850e63aada5fd32f7cf0964d881
[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-2014, 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 Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Aggr; use Exp_Aggr;
33 with Exp_Atag; use Exp_Atag;
34 with Exp_Ch2; use Exp_Ch2;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch6; use Exp_Ch6;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Ch9; use Exp_Ch9;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Fixd; use Exp_Fixd;
41 with Exp_Intr; use Exp_Intr;
42 with Exp_Pakd; use Exp_Pakd;
43 with Exp_Tss; use Exp_Tss;
44 with Exp_Util; use Exp_Util;
45 with Exp_VFpt; use Exp_VFpt;
46 with Freeze; use Freeze;
47 with Inline; use Inline;
48 with Lib; use Lib;
49 with Namet; use Namet;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
52 with Opt; use Opt;
53 with Par_SCO; use Par_SCO;
54 with Restrict; use Restrict;
55 with Rident; use Rident;
56 with Rtsfind; use Rtsfind;
57 with Sem; use Sem;
58 with Sem_Aux; use Sem_Aux;
59 with Sem_Cat; use Sem_Cat;
60 with Sem_Ch3; use Sem_Ch3;
61 with Sem_Ch8; use Sem_Ch8;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Res; use Sem_Res;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sem_Warn; use Sem_Warn;
68 with Sinfo; use Sinfo;
69 with Snames; use Snames;
70 with Stand; use Stand;
71 with SCIL_LL; use SCIL_LL;
72 with Targparm; use Targparm;
73 with Tbuild; use Tbuild;
74 with Ttypes; use Ttypes;
75 with Uintp; use Uintp;
76 with Urealp; use Urealp;
77 with Validsw; use Validsw;
78
79 package body Exp_Ch4 is
80
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
84
85 procedure Binary_Op_Validity_Checks (N : Node_Id);
86 pragma Inline (Binary_Op_Validity_Checks);
87 -- Performs validity checks for a binary operator
88
89 procedure Build_Boolean_Array_Proc_Call
90 (N : Node_Id;
91 Op1 : Node_Id;
92 Op2 : Node_Id);
93 -- If a boolean array assignment can be done in place, build call to
94 -- corresponding library procedure.
95
96 function Current_Anonymous_Master return Entity_Id;
97 -- Return the entity of the heterogeneous finalization master belonging to
98 -- the current unit (either function, package or procedure). This master
99 -- services all anonymous access-to-controlled types. If the current unit
100 -- does not have such master, create one.
101
102 procedure Displace_Allocator_Pointer (N : Node_Id);
103 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
104 -- Expand_Allocator_Expression. Allocating class-wide interface objects
105 -- this routine displaces the pointer to the allocated object to reference
106 -- the component referencing the corresponding secondary dispatch table.
107
108 procedure Expand_Allocator_Expression (N : Node_Id);
109 -- Subsidiary to Expand_N_Allocator, for the case when the expression
110 -- is a qualified expression or an aggregate.
111
112 procedure Expand_Array_Comparison (N : Node_Id);
113 -- This routine handles expansion of the comparison operators (N_Op_Lt,
114 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
115 -- code for these operators is similar, differing only in the details of
116 -- the actual comparison call that is made. Special processing (call a
117 -- run-time routine)
118
119 function Expand_Array_Equality
120 (Nod : Node_Id;
121 Lhs : Node_Id;
122 Rhs : Node_Id;
123 Bodies : List_Id;
124 Typ : Entity_Id) return Node_Id;
125 -- Expand an array equality into a call to a function implementing this
126 -- equality, and a call to it. Loc is the location for the generated nodes.
127 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
128 -- on which to attach bodies of local functions that are created in the
129 -- process. It is the responsibility of the caller to insert those bodies
130 -- at the right place. Nod provides the Sloc value for the generated code.
131 -- Normally the types used for the generated equality routine are taken
132 -- from Lhs and Rhs. However, in some situations of generated code, the
133 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
134 -- the type to be used for the formal parameters.
135
136 procedure Expand_Boolean_Operator (N : Node_Id);
137 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
138 -- case of array type arguments.
139
140 procedure Expand_Short_Circuit_Operator (N : Node_Id);
141 -- Common expansion processing for short-circuit boolean operators
142
143 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id);
144 -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is
145 -- where we allow comparison of "out of range" values.
146
147 function Expand_Composite_Equality
148 (Nod : Node_Id;
149 Typ : Entity_Id;
150 Lhs : Node_Id;
151 Rhs : Node_Id;
152 Bodies : List_Id) return Node_Id;
153 -- Local recursive function used to expand equality for nested composite
154 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
155 -- to attach bodies of local functions that are created in the process.
156 -- It is the responsibility of the caller to insert those bodies at the
157 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
158 -- are the left and right sides for the comparison, and Typ is the type of
159 -- the objects to compare.
160
161 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
162 -- Routine to expand concatenation of a sequence of two or more operands
163 -- (in the list Operands) and replace node Cnode with the result of the
164 -- concatenation. The operands can be of any appropriate type, and can
165 -- include both arrays and singleton elements.
166
167 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id);
168 -- N is an N_In membership test mode, with the overflow check mode set to
169 -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed
170 -- integer type. This is a case where top level processing is required to
171 -- handle overflow checks in subtrees.
172
173 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
174 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
175 -- fixed. We do not have such a type at runtime, so the purpose of this
176 -- routine is to find the real type by looking up the tree. We also
177 -- determine if the operation must be rounded.
178
179 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
180 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
181 -- discriminants if it has a constrained nominal type, unless the object
182 -- is a component of an enclosing Unchecked_Union object that is subject
183 -- to a per-object constraint and the enclosing object lacks inferable
184 -- discriminants.
185 --
186 -- An expression of an Unchecked_Union type has inferable discriminants
187 -- if it is either a name of an object with inferable discriminants or a
188 -- qualified expression whose subtype mark denotes a constrained subtype.
189
190 procedure Insert_Dereference_Action (N : Node_Id);
191 -- N is an expression whose type is an access. When the type of the
192 -- associated storage pool is derived from Checked_Pool, generate a
193 -- call to the 'Dereference' primitive operation.
194
195 function Make_Array_Comparison_Op
196 (Typ : Entity_Id;
197 Nod : Node_Id) return Node_Id;
198 -- Comparisons between arrays are expanded in line. This function produces
199 -- the body of the implementation of (a > b), where a and b are one-
200 -- dimensional arrays of some discrete type. The original node is then
201 -- expanded into the appropriate call to this function. Nod provides the
202 -- Sloc value for the generated code.
203
204 function Make_Boolean_Array_Op
205 (Typ : Entity_Id;
206 N : Node_Id) return Node_Id;
207 -- Boolean operations on boolean arrays are expanded in line. This function
208 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
209 -- b). It is used only the normal case and not the packed case. The type
210 -- involved, Typ, is the Boolean array type, and the logical operations in
211 -- the body are simple boolean operations. Note that Typ is always a
212 -- constrained type (the caller has ensured this by using
213 -- Convert_To_Actual_Subtype if necessary).
214
215 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean;
216 -- For signed arithmetic operations when the current overflow mode is
217 -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks
218 -- as the first thing we do. We then return. We count on the recursive
219 -- apparatus for overflow checks to call us back with an equivalent
220 -- operation that is in CHECKED mode, avoiding a recursive entry into this
221 -- routine, and that is when we will proceed with the expansion of the
222 -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do
223 -- these optimizations without first making this check, since there may be
224 -- operands further down the tree that are relying on the recursive calls
225 -- triggered by the top level nodes to properly process overflow checking
226 -- and remaining expansion on these nodes. Note that this call back may be
227 -- skipped if the operation is done in Bignum mode but that's fine, since
228 -- the Bignum call takes care of everything.
229
230 procedure Optimize_Length_Comparison (N : Node_Id);
231 -- Given an expression, if it is of the form X'Length op N (or the other
232 -- way round), where N is known at compile time to be 0 or 1, and X is a
233 -- simple entity, and op is a comparison operator, optimizes it into a
234 -- comparison of First and Last.
235
236 procedure Process_Transient_Object
237 (Decl : Node_Id;
238 Rel_Node : Node_Id);
239 -- Subsidiary routine to the expansion of expression_with_actions and if
240 -- expressions. Generate all the necessary code to finalize a transient
241 -- controlled object when the enclosing context is elaborated or evaluated.
242 -- Decl denotes the declaration of the transient controlled object which is
243 -- usually the result of a controlled function call. Rel_Node denotes the
244 -- context, either an expression_with_actions or an if expression.
245
246 procedure Rewrite_Comparison (N : Node_Id);
247 -- If N is the node for a comparison whose outcome can be determined at
248 -- compile time, then the node N can be rewritten with True or False. If
249 -- the outcome cannot be determined at compile time, the call has no
250 -- effect. If N is a type conversion, then this processing is applied to
251 -- its expression. If N is neither comparison nor a type conversion, the
252 -- call has no effect.
253
254 procedure Tagged_Membership
255 (N : Node_Id;
256 SCIL_Node : out Node_Id;
257 Result : out Node_Id);
258 -- Construct the expression corresponding to the tagged membership test.
259 -- Deals with a second operand being (or not) a class-wide type.
260
261 function Safe_In_Place_Array_Op
262 (Lhs : Node_Id;
263 Op1 : Node_Id;
264 Op2 : Node_Id) return Boolean;
265 -- In the context of an assignment, where the right-hand side is a boolean
266 -- operation on arrays, check whether operation can be performed in place.
267
268 procedure Unary_Op_Validity_Checks (N : Node_Id);
269 pragma Inline (Unary_Op_Validity_Checks);
270 -- Performs validity checks for a unary operator
271
272 -------------------------------
273 -- Binary_Op_Validity_Checks --
274 -------------------------------
275
276 procedure Binary_Op_Validity_Checks (N : Node_Id) is
277 begin
278 if Validity_Checks_On and Validity_Check_Operands then
279 Ensure_Valid (Left_Opnd (N));
280 Ensure_Valid (Right_Opnd (N));
281 end if;
282 end Binary_Op_Validity_Checks;
283
284 ------------------------------------
285 -- Build_Boolean_Array_Proc_Call --
286 ------------------------------------
287
288 procedure Build_Boolean_Array_Proc_Call
289 (N : Node_Id;
290 Op1 : Node_Id;
291 Op2 : Node_Id)
292 is
293 Loc : constant Source_Ptr := Sloc (N);
294 Kind : constant Node_Kind := Nkind (Expression (N));
295 Target : constant Node_Id :=
296 Make_Attribute_Reference (Loc,
297 Prefix => Name (N),
298 Attribute_Name => Name_Address);
299
300 Arg1 : Node_Id := Op1;
301 Arg2 : Node_Id := Op2;
302 Call_Node : Node_Id;
303 Proc_Name : Entity_Id;
304
305 begin
306 if Kind = N_Op_Not then
307 if Nkind (Op1) in N_Binary_Op then
308
309 -- Use negated version of the binary operators
310
311 if Nkind (Op1) = N_Op_And then
312 Proc_Name := RTE (RE_Vector_Nand);
313
314 elsif Nkind (Op1) = N_Op_Or then
315 Proc_Name := RTE (RE_Vector_Nor);
316
317 else pragma Assert (Nkind (Op1) = N_Op_Xor);
318 Proc_Name := RTE (RE_Vector_Xor);
319 end if;
320
321 Call_Node :=
322 Make_Procedure_Call_Statement (Loc,
323 Name => New_Occurrence_Of (Proc_Name, Loc),
324
325 Parameter_Associations => New_List (
326 Target,
327 Make_Attribute_Reference (Loc,
328 Prefix => Left_Opnd (Op1),
329 Attribute_Name => Name_Address),
330
331 Make_Attribute_Reference (Loc,
332 Prefix => Right_Opnd (Op1),
333 Attribute_Name => Name_Address),
334
335 Make_Attribute_Reference (Loc,
336 Prefix => Left_Opnd (Op1),
337 Attribute_Name => Name_Length)));
338
339 else
340 Proc_Name := RTE (RE_Vector_Not);
341
342 Call_Node :=
343 Make_Procedure_Call_Statement (Loc,
344 Name => New_Occurrence_Of (Proc_Name, Loc),
345 Parameter_Associations => New_List (
346 Target,
347
348 Make_Attribute_Reference (Loc,
349 Prefix => Op1,
350 Attribute_Name => Name_Address),
351
352 Make_Attribute_Reference (Loc,
353 Prefix => Op1,
354 Attribute_Name => Name_Length)));
355 end if;
356
357 else
358 -- We use the following equivalences:
359
360 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
361 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
362 -- (not X) xor (not Y) = X xor Y
363 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
364
365 if Nkind (Op1) = N_Op_Not then
366 Arg1 := Right_Opnd (Op1);
367 Arg2 := Right_Opnd (Op2);
368
369 if Kind = N_Op_And then
370 Proc_Name := RTE (RE_Vector_Nor);
371 elsif Kind = N_Op_Or then
372 Proc_Name := RTE (RE_Vector_Nand);
373 else
374 Proc_Name := RTE (RE_Vector_Xor);
375 end if;
376
377 else
378 if Kind = N_Op_And then
379 Proc_Name := RTE (RE_Vector_And);
380 elsif Kind = N_Op_Or then
381 Proc_Name := RTE (RE_Vector_Or);
382 elsif Nkind (Op2) = N_Op_Not then
383 Proc_Name := RTE (RE_Vector_Nxor);
384 Arg2 := Right_Opnd (Op2);
385 else
386 Proc_Name := RTE (RE_Vector_Xor);
387 end if;
388 end if;
389
390 Call_Node :=
391 Make_Procedure_Call_Statement (Loc,
392 Name => New_Occurrence_Of (Proc_Name, Loc),
393 Parameter_Associations => New_List (
394 Target,
395 Make_Attribute_Reference (Loc,
396 Prefix => Arg1,
397 Attribute_Name => Name_Address),
398 Make_Attribute_Reference (Loc,
399 Prefix => Arg2,
400 Attribute_Name => Name_Address),
401 Make_Attribute_Reference (Loc,
402 Prefix => Arg1,
403 Attribute_Name => Name_Length)));
404 end if;
405
406 Rewrite (N, Call_Node);
407 Analyze (N);
408
409 exception
410 when RE_Not_Available =>
411 return;
412 end Build_Boolean_Array_Proc_Call;
413
414 ------------------------------
415 -- Current_Anonymous_Master --
416 ------------------------------
417
418 function Current_Anonymous_Master return Entity_Id is
419 Decls : List_Id;
420 Loc : Source_Ptr;
421 Subp_Body : Node_Id;
422 Unit_Decl : Node_Id;
423 Unit_Id : Entity_Id;
424
425 begin
426 Unit_Id := Cunit_Entity (Current_Sem_Unit);
427
428 -- Find the entity of the current unit
429
430 if Ekind (Unit_Id) = E_Subprogram_Body then
431
432 -- When processing subprogram bodies, the proper scope is always that
433 -- of the spec.
434
435 Subp_Body := Unit_Id;
436 while Present (Subp_Body)
437 and then Nkind (Subp_Body) /= N_Subprogram_Body
438 loop
439 Subp_Body := Parent (Subp_Body);
440 end loop;
441
442 Unit_Id := Corresponding_Spec (Subp_Body);
443 end if;
444
445 Loc := Sloc (Unit_Id);
446 Unit_Decl := Unit (Cunit (Current_Sem_Unit));
447
448 -- Find the declarations list of the current unit
449
450 if Nkind (Unit_Decl) = N_Package_Declaration then
451 Unit_Decl := Specification (Unit_Decl);
452 Decls := Visible_Declarations (Unit_Decl);
453
454 if No (Decls) then
455 Decls := New_List (Make_Null_Statement (Loc));
456 Set_Visible_Declarations (Unit_Decl, Decls);
457
458 elsif Is_Empty_List (Decls) then
459 Append_To (Decls, Make_Null_Statement (Loc));
460 end if;
461
462 else
463 Decls := Declarations (Unit_Decl);
464
465 if No (Decls) then
466 Decls := New_List (Make_Null_Statement (Loc));
467 Set_Declarations (Unit_Decl, Decls);
468
469 elsif Is_Empty_List (Decls) then
470 Append_To (Decls, Make_Null_Statement (Loc));
471 end if;
472 end if;
473
474 -- The current unit has an existing anonymous master, traverse its
475 -- declarations and locate the entity.
476
477 if Has_Anonymous_Master (Unit_Id) then
478 declare
479 Decl : Node_Id;
480 Fin_Mas_Id : Entity_Id;
481
482 begin
483 Decl := First (Decls);
484 while Present (Decl) loop
485
486 -- Look for the first variable in the declarations whole type
487 -- is Finalization_Master.
488
489 if Nkind (Decl) = N_Object_Declaration then
490 Fin_Mas_Id := Defining_Identifier (Decl);
491
492 if Ekind (Fin_Mas_Id) = E_Variable
493 and then Etype (Fin_Mas_Id) = RTE (RE_Finalization_Master)
494 then
495 return Fin_Mas_Id;
496 end if;
497 end if;
498
499 Next (Decl);
500 end loop;
501
502 -- The master was not found even though the unit was labeled as
503 -- having one.
504
505 raise Program_Error;
506 end;
507
508 -- Create a new anonymous master
509
510 else
511 declare
512 First_Decl : constant Node_Id := First (Decls);
513 Action : Node_Id;
514 Fin_Mas_Id : Entity_Id;
515
516 begin
517 -- Since the master and its associated initialization is inserted
518 -- at top level, use the scope of the unit when analyzing.
519
520 Push_Scope (Unit_Id);
521
522 -- Create the finalization master
523
524 Fin_Mas_Id :=
525 Make_Defining_Identifier (Loc,
526 Chars => New_External_Name (Chars (Unit_Id), "AM"));
527
528 -- Generate:
529 -- <Fin_Mas_Id> : Finalization_Master;
530
531 Action :=
532 Make_Object_Declaration (Loc,
533 Defining_Identifier => Fin_Mas_Id,
534 Object_Definition =>
535 New_Occurrence_Of (RTE (RE_Finalization_Master), Loc));
536
537 Insert_Before_And_Analyze (First_Decl, Action);
538
539 -- Mark the unit to prevent the generation of multiple masters
540
541 Set_Has_Anonymous_Master (Unit_Id);
542
543 -- Do not set the base pool and mode of operation on .NET/JVM
544 -- since those targets do not support pools and all VM masters
545 -- are heterogeneous by default.
546
547 if VM_Target = No_VM then
548
549 -- Generate:
550 -- Set_Base_Pool
551 -- (<Fin_Mas_Id>, Global_Pool_Object'Unrestricted_Access);
552
553 Action :=
554 Make_Procedure_Call_Statement (Loc,
555 Name =>
556 New_Occurrence_Of (RTE (RE_Set_Base_Pool), Loc),
557
558 Parameter_Associations => New_List (
559 New_Occurrence_Of (Fin_Mas_Id, Loc),
560 Make_Attribute_Reference (Loc,
561 Prefix =>
562 New_Occurrence_Of (RTE (RE_Global_Pool_Object), Loc),
563 Attribute_Name => Name_Unrestricted_Access)));
564
565 Insert_Before_And_Analyze (First_Decl, Action);
566
567 -- Generate:
568 -- Set_Is_Heterogeneous (<Fin_Mas_Id>);
569
570 Action :=
571 Make_Procedure_Call_Statement (Loc,
572 Name =>
573 New_Occurrence_Of (RTE (RE_Set_Is_Heterogeneous), Loc),
574 Parameter_Associations => New_List (
575 New_Occurrence_Of (Fin_Mas_Id, Loc)));
576
577 Insert_Before_And_Analyze (First_Decl, Action);
578 end if;
579
580 -- Restore the original state of the scope stack
581
582 Pop_Scope;
583
584 return Fin_Mas_Id;
585 end;
586 end if;
587 end Current_Anonymous_Master;
588
589 --------------------------------
590 -- Displace_Allocator_Pointer --
591 --------------------------------
592
593 procedure Displace_Allocator_Pointer (N : Node_Id) is
594 Loc : constant Source_Ptr := Sloc (N);
595 Orig_Node : constant Node_Id := Original_Node (N);
596 Dtyp : Entity_Id;
597 Etyp : Entity_Id;
598 PtrT : Entity_Id;
599
600 begin
601 -- Do nothing in case of VM targets: the virtual machine will handle
602 -- interfaces directly.
603
604 if not Tagged_Type_Expansion then
605 return;
606 end if;
607
608 pragma Assert (Nkind (N) = N_Identifier
609 and then Nkind (Orig_Node) = N_Allocator);
610
611 PtrT := Etype (Orig_Node);
612 Dtyp := Available_View (Designated_Type (PtrT));
613 Etyp := Etype (Expression (Orig_Node));
614
615 if Is_Class_Wide_Type (Dtyp) and then Is_Interface (Dtyp) then
616
617 -- If the type of the allocator expression is not an interface type
618 -- we can generate code to reference the record component containing
619 -- the pointer to the secondary dispatch table.
620
621 if not Is_Interface (Etyp) then
622 declare
623 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
624
625 begin
626 -- 1) Get access to the allocated object
627
628 Rewrite (N,
629 Make_Explicit_Dereference (Loc, Relocate_Node (N)));
630 Set_Etype (N, Etyp);
631 Set_Analyzed (N);
632
633 -- 2) Add the conversion to displace the pointer to reference
634 -- the secondary dispatch table.
635
636 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
637 Analyze_And_Resolve (N, Dtyp);
638
639 -- 3) The 'access to the secondary dispatch table will be used
640 -- as the value returned by the allocator.
641
642 Rewrite (N,
643 Make_Attribute_Reference (Loc,
644 Prefix => Relocate_Node (N),
645 Attribute_Name => Name_Access));
646 Set_Etype (N, Saved_Typ);
647 Set_Analyzed (N);
648 end;
649
650 -- If the type of the allocator expression is an interface type we
651 -- generate a run-time call to displace "this" to reference the
652 -- component containing the pointer to the secondary dispatch table
653 -- or else raise Constraint_Error if the actual object does not
654 -- implement the target interface. This case corresponds to the
655 -- following example:
656
657 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
658 -- begin
659 -- return new Iface_2'Class'(Obj);
660 -- end Op;
661
662 else
663 Rewrite (N,
664 Unchecked_Convert_To (PtrT,
665 Make_Function_Call (Loc,
666 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
667 Parameter_Associations => New_List (
668 Unchecked_Convert_To (RTE (RE_Address),
669 Relocate_Node (N)),
670
671 New_Occurrence_Of
672 (Elists.Node
673 (First_Elmt
674 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
675 Loc)))));
676 Analyze_And_Resolve (N, PtrT);
677 end if;
678 end if;
679 end Displace_Allocator_Pointer;
680
681 ---------------------------------
682 -- Expand_Allocator_Expression --
683 ---------------------------------
684
685 procedure Expand_Allocator_Expression (N : Node_Id) is
686 Loc : constant Source_Ptr := Sloc (N);
687 Exp : constant Node_Id := Expression (Expression (N));
688 PtrT : constant Entity_Id := Etype (N);
689 DesigT : constant Entity_Id := Designated_Type (PtrT);
690
691 procedure Apply_Accessibility_Check
692 (Ref : Node_Id;
693 Built_In_Place : Boolean := False);
694 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
695 -- type, generate an accessibility check to verify that the level of the
696 -- type of the created object is not deeper than the level of the access
697 -- type. If the type of the qualified expression is class-wide, then
698 -- always generate the check (except in the case where it is known to be
699 -- unnecessary, see comment below). Otherwise, only generate the check
700 -- if the level of the qualified expression type is statically deeper
701 -- than the access type.
702 --
703 -- Although the static accessibility will generally have been performed
704 -- as a legality check, it won't have been done in cases where the
705 -- allocator appears in generic body, so a run-time check is needed in
706 -- general. One special case is when the access type is declared in the
707 -- same scope as the class-wide allocator, in which case the check can
708 -- never fail, so it need not be generated.
709 --
710 -- As an open issue, there seem to be cases where the static level
711 -- associated with the class-wide object's underlying type is not
712 -- sufficient to perform the proper accessibility check, such as for
713 -- allocators in nested subprograms or accept statements initialized by
714 -- class-wide formals when the actual originates outside at a deeper
715 -- static level. The nested subprogram case might require passing
716 -- accessibility levels along with class-wide parameters, and the task
717 -- case seems to be an actual gap in the language rules that needs to
718 -- be fixed by the ARG. ???
719
720 -------------------------------
721 -- Apply_Accessibility_Check --
722 -------------------------------
723
724 procedure Apply_Accessibility_Check
725 (Ref : Node_Id;
726 Built_In_Place : Boolean := False)
727 is
728 Pool_Id : constant Entity_Id := Associated_Storage_Pool (PtrT);
729 Cond : Node_Id;
730 Fin_Call : Node_Id;
731 Free_Stmt : Node_Id;
732 Obj_Ref : Node_Id;
733 Stmts : List_Id;
734
735 begin
736 if Ada_Version >= Ada_2005
737 and then Is_Class_Wide_Type (DesigT)
738 and then (Tagged_Type_Expansion or else VM_Target /= No_VM)
739 and then not Scope_Suppress.Suppress (Accessibility_Check)
740 and then
741 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
742 or else
743 (Is_Class_Wide_Type (Etype (Exp))
744 and then Scope (PtrT) /= Current_Scope))
745 then
746 -- If the allocator was built in place, Ref is already a reference
747 -- to the access object initialized to the result of the allocator
748 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
749 -- Remove_Side_Effects for cases where the build-in-place call may
750 -- still be the prefix of the reference (to avoid generating
751 -- duplicate calls). Otherwise, it is the entity associated with
752 -- the object containing the address of the allocated object.
753
754 if Built_In_Place then
755 Remove_Side_Effects (Ref);
756 Obj_Ref := New_Copy_Tree (Ref);
757 else
758 Obj_Ref := New_Occurrence_Of (Ref, Loc);
759 end if;
760
761 -- Step 1: Create the object clean up code
762
763 Stmts := New_List;
764
765 -- Deallocate the object if the accessibility check fails. This
766 -- is done only on targets or profiles that support deallocation.
767
768 -- Free (Obj_Ref);
769
770 if RTE_Available (RE_Free) then
771 Free_Stmt := Make_Free_Statement (Loc, New_Copy_Tree (Obj_Ref));
772 Set_Storage_Pool (Free_Stmt, Pool_Id);
773
774 Append_To (Stmts, Free_Stmt);
775
776 -- The target or profile cannot deallocate objects
777
778 else
779 Free_Stmt := Empty;
780 end if;
781
782 -- Finalize the object if applicable. Generate:
783
784 -- [Deep_]Finalize (Obj_Ref.all);
785
786 if Needs_Finalization (DesigT) then
787 Fin_Call :=
788 Make_Final_Call (
789 Obj_Ref =>
790 Make_Explicit_Dereference (Loc, New_Copy (Obj_Ref)),
791 Typ => DesigT);
792
793 -- When the target or profile supports deallocation, wrap the
794 -- finalization call in a block to ensure proper deallocation
795 -- even if finalization fails. Generate:
796
797 -- begin
798 -- <Fin_Call>
799 -- exception
800 -- when others =>
801 -- <Free_Stmt>
802 -- raise;
803 -- end;
804
805 if Present (Free_Stmt) then
806 Fin_Call :=
807 Make_Block_Statement (Loc,
808 Handled_Statement_Sequence =>
809 Make_Handled_Sequence_Of_Statements (Loc,
810 Statements => New_List (Fin_Call),
811
812 Exception_Handlers => New_List (
813 Make_Exception_Handler (Loc,
814 Exception_Choices => New_List (
815 Make_Others_Choice (Loc)),
816
817 Statements => New_List (
818 New_Copy_Tree (Free_Stmt),
819 Make_Raise_Statement (Loc))))));
820 end if;
821
822 Prepend_To (Stmts, Fin_Call);
823 end if;
824
825 -- Signal the accessibility failure through a Program_Error
826
827 Append_To (Stmts,
828 Make_Raise_Program_Error (Loc,
829 Condition => New_Occurrence_Of (Standard_True, Loc),
830 Reason => PE_Accessibility_Check_Failed));
831
832 -- Step 2: Create the accessibility comparison
833
834 -- Reference the tag: for a renaming of an access to an interface
835 -- object Obj_Ref already references the tag of the secondary
836 -- dispatch table.
837
838 if Nkind (Obj_Ref) in N_Has_Entity
839 and then Present (Entity (Obj_Ref))
840 and then Present (Renamed_Object (Entity (Obj_Ref)))
841 and then Is_Interface (DesigT)
842 then
843 null;
844
845 -- Generate:
846 -- Ref'Tag
847
848 else
849 Obj_Ref :=
850 Make_Attribute_Reference (Loc,
851 Prefix => Obj_Ref,
852 Attribute_Name => Name_Tag);
853 end if;
854
855 -- For tagged types, determine the accessibility level by looking
856 -- at the type specific data of the dispatch table. Generate:
857
858 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
859
860 if Tagged_Type_Expansion then
861 Cond := Build_Get_Access_Level (Loc, Obj_Ref);
862
863 -- Use a runtime call to determine the accessibility level when
864 -- compiling on virtual machine targets. Generate:
865
866 -- Get_Access_Level (Ref'Tag)
867
868 else
869 Cond :=
870 Make_Function_Call (Loc,
871 Name =>
872 New_Occurrence_Of (RTE (RE_Get_Access_Level), Loc),
873 Parameter_Associations => New_List (Obj_Ref));
874 end if;
875
876 Cond :=
877 Make_Op_Gt (Loc,
878 Left_Opnd => Cond,
879 Right_Opnd =>
880 Make_Integer_Literal (Loc, Type_Access_Level (PtrT)));
881
882 -- Due to the complexity and side effects of the check, utilize an
883 -- if statement instead of the regular Program_Error circuitry.
884
885 Insert_Action (N,
886 Make_Implicit_If_Statement (N,
887 Condition => Cond,
888 Then_Statements => Stmts));
889 end if;
890 end Apply_Accessibility_Check;
891
892 -- Local variables
893
894 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
895 Indic : constant Node_Id := Subtype_Mark (Expression (N));
896 T : constant Entity_Id := Entity (Indic);
897 Node : Node_Id;
898 Tag_Assign : Node_Id;
899 Temp : Entity_Id;
900 Temp_Decl : Node_Id;
901
902 TagT : Entity_Id := Empty;
903 -- Type used as source for tag assignment
904
905 TagR : Node_Id := Empty;
906 -- Target reference for tag assignment
907
908 -- Start of processing for Expand_Allocator_Expression
909
910 begin
911 -- Handle call to C++ constructor
912
913 if Is_CPP_Constructor_Call (Exp) then
914 Make_CPP_Constructor_Call_In_Allocator
915 (Allocator => N,
916 Function_Call => Exp);
917 return;
918 end if;
919
920 -- In the case of an Ada 2012 allocator whose initial value comes from a
921 -- function call, pass "the accessibility level determined by the point
922 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
923 -- Expand_Call but it couldn't be done there (because the Etype of the
924 -- allocator wasn't set then) so we generate the parameter here. See
925 -- the Boolean variable Defer in (a block within) Expand_Call.
926
927 if Ada_Version >= Ada_2012 and then Nkind (Exp) = N_Function_Call then
928 declare
929 Subp : Entity_Id;
930
931 begin
932 if Nkind (Name (Exp)) = N_Explicit_Dereference then
933 Subp := Designated_Type (Etype (Prefix (Name (Exp))));
934 else
935 Subp := Entity (Name (Exp));
936 end if;
937
938 Subp := Ultimate_Alias (Subp);
939
940 if Present (Extra_Accessibility_Of_Result (Subp)) then
941 Add_Extra_Actual_To_Call
942 (Subprogram_Call => Exp,
943 Extra_Formal => Extra_Accessibility_Of_Result (Subp),
944 Extra_Actual => Dynamic_Accessibility_Level (PtrT));
945 end if;
946 end;
947 end if;
948
949 -- Case of tagged type or type requiring finalization
950
951 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
952
953 -- Ada 2005 (AI-318-02): If the initialization expression is a call
954 -- to a build-in-place function, then access to the allocated object
955 -- must be passed to the function. Currently we limit such functions
956 -- to those with constrained limited result subtypes, but eventually
957 -- we plan to expand the allowed forms of functions that are treated
958 -- as build-in-place.
959
960 if Ada_Version >= Ada_2005
961 and then Is_Build_In_Place_Function_Call (Exp)
962 then
963 Make_Build_In_Place_Call_In_Allocator (N, Exp);
964 Apply_Accessibility_Check (N, Built_In_Place => True);
965 return;
966 end if;
967
968 -- Actions inserted before:
969 -- Temp : constant ptr_T := new T'(Expression);
970 -- Temp._tag = T'tag; -- when not class-wide
971 -- [Deep_]Adjust (Temp.all);
972
973 -- We analyze by hand the new internal allocator to avoid any
974 -- recursion and inappropriate call to Initialize.
975
976 -- We don't want to remove side effects when the expression must be
977 -- built in place. In the case of a build-in-place function call,
978 -- that could lead to a duplication of the call, which was already
979 -- substituted for the allocator.
980
981 if not Aggr_In_Place then
982 Remove_Side_Effects (Exp);
983 end if;
984
985 Temp := Make_Temporary (Loc, 'P', N);
986
987 -- For a class wide allocation generate the following code:
988
989 -- type Equiv_Record is record ... end record;
990 -- implicit subtype CW is <Class_Wide_Subytpe>;
991 -- temp : PtrT := new CW'(CW!(expr));
992
993 if Is_Class_Wide_Type (T) then
994 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
995
996 -- Ada 2005 (AI-251): If the expression is a class-wide interface
997 -- object we generate code to move up "this" to reference the
998 -- base of the object before allocating the new object.
999
1000 -- Note that Exp'Address is recursively expanded into a call
1001 -- to Base_Address (Exp.Tag)
1002
1003 if Is_Class_Wide_Type (Etype (Exp))
1004 and then Is_Interface (Etype (Exp))
1005 and then Tagged_Type_Expansion
1006 then
1007 Set_Expression
1008 (Expression (N),
1009 Unchecked_Convert_To (Entity (Indic),
1010 Make_Explicit_Dereference (Loc,
1011 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
1012 Make_Attribute_Reference (Loc,
1013 Prefix => Exp,
1014 Attribute_Name => Name_Address)))));
1015 else
1016 Set_Expression
1017 (Expression (N),
1018 Unchecked_Convert_To (Entity (Indic), Exp));
1019 end if;
1020
1021 Analyze_And_Resolve (Expression (N), Entity (Indic));
1022 end if;
1023
1024 -- Processing for allocators returning non-interface types
1025
1026 if not Is_Interface (Directly_Designated_Type (PtrT)) then
1027 if Aggr_In_Place then
1028 Temp_Decl :=
1029 Make_Object_Declaration (Loc,
1030 Defining_Identifier => Temp,
1031 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1032 Expression =>
1033 Make_Allocator (Loc,
1034 Expression =>
1035 New_Occurrence_Of (Etype (Exp), Loc)));
1036
1037 -- Copy the Comes_From_Source flag for the allocator we just
1038 -- built, since logically this allocator is a replacement of
1039 -- the original allocator node. This is for proper handling of
1040 -- restriction No_Implicit_Heap_Allocations.
1041
1042 Set_Comes_From_Source
1043 (Expression (Temp_Decl), Comes_From_Source (N));
1044
1045 Set_No_Initialization (Expression (Temp_Decl));
1046 Insert_Action (N, Temp_Decl);
1047
1048 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1049 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1050
1051 -- Attach the object to the associated finalization master.
1052 -- This is done manually on .NET/JVM since those compilers do
1053 -- no support pools and can't benefit from internally generated
1054 -- Allocate / Deallocate procedures.
1055
1056 if VM_Target /= No_VM
1057 and then Is_Controlled (DesigT)
1058 and then Present (Finalization_Master (PtrT))
1059 then
1060 Insert_Action (N,
1061 Make_Attach_Call (
1062 Obj_Ref => New_Occurrence_Of (Temp, Loc),
1063 Ptr_Typ => PtrT));
1064 end if;
1065
1066 else
1067 Node := Relocate_Node (N);
1068 Set_Analyzed (Node);
1069
1070 Temp_Decl :=
1071 Make_Object_Declaration (Loc,
1072 Defining_Identifier => Temp,
1073 Constant_Present => True,
1074 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1075 Expression => Node);
1076
1077 Insert_Action (N, Temp_Decl);
1078 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1079
1080 -- Attach the object to the associated finalization master.
1081 -- This is done manually on .NET/JVM since those compilers do
1082 -- no support pools and can't benefit from internally generated
1083 -- Allocate / Deallocate procedures.
1084
1085 if VM_Target /= No_VM
1086 and then Is_Controlled (DesigT)
1087 and then Present (Finalization_Master (PtrT))
1088 then
1089 Insert_Action (N,
1090 Make_Attach_Call (
1091 Obj_Ref =>
1092 New_Occurrence_Of (Temp, Loc),
1093 Ptr_Typ => PtrT));
1094 end if;
1095 end if;
1096
1097 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
1098 -- interface type. In this case we use the type of the qualified
1099 -- expression to allocate the object.
1100
1101 else
1102 declare
1103 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
1104 New_Decl : Node_Id;
1105
1106 begin
1107 New_Decl :=
1108 Make_Full_Type_Declaration (Loc,
1109 Defining_Identifier => Def_Id,
1110 Type_Definition =>
1111 Make_Access_To_Object_Definition (Loc,
1112 All_Present => True,
1113 Null_Exclusion_Present => False,
1114 Constant_Present =>
1115 Is_Access_Constant (Etype (N)),
1116 Subtype_Indication =>
1117 New_Occurrence_Of (Etype (Exp), Loc)));
1118
1119 Insert_Action (N, New_Decl);
1120
1121 -- Inherit the allocation-related attributes from the original
1122 -- access type.
1123
1124 Set_Finalization_Master (Def_Id, Finalization_Master (PtrT));
1125
1126 Set_Associated_Storage_Pool (Def_Id,
1127 Associated_Storage_Pool (PtrT));
1128
1129 -- Declare the object using the previous type declaration
1130
1131 if Aggr_In_Place then
1132 Temp_Decl :=
1133 Make_Object_Declaration (Loc,
1134 Defining_Identifier => Temp,
1135 Object_Definition => New_Occurrence_Of (Def_Id, Loc),
1136 Expression =>
1137 Make_Allocator (Loc,
1138 New_Occurrence_Of (Etype (Exp), Loc)));
1139
1140 -- Copy the Comes_From_Source flag for the allocator we just
1141 -- built, since logically this allocator is a replacement of
1142 -- the original allocator node. This is for proper handling
1143 -- of restriction No_Implicit_Heap_Allocations.
1144
1145 Set_Comes_From_Source
1146 (Expression (Temp_Decl), Comes_From_Source (N));
1147
1148 Set_No_Initialization (Expression (Temp_Decl));
1149 Insert_Action (N, Temp_Decl);
1150
1151 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1152 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1153
1154 else
1155 Node := Relocate_Node (N);
1156 Set_Analyzed (Node);
1157
1158 Temp_Decl :=
1159 Make_Object_Declaration (Loc,
1160 Defining_Identifier => Temp,
1161 Constant_Present => True,
1162 Object_Definition => New_Occurrence_Of (Def_Id, Loc),
1163 Expression => Node);
1164
1165 Insert_Action (N, Temp_Decl);
1166 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1167 end if;
1168
1169 -- Generate an additional object containing the address of the
1170 -- returned object. The type of this second object declaration
1171 -- is the correct type required for the common processing that
1172 -- is still performed by this subprogram. The displacement of
1173 -- this pointer to reference the component associated with the
1174 -- interface type will be done at the end of common processing.
1175
1176 New_Decl :=
1177 Make_Object_Declaration (Loc,
1178 Defining_Identifier => Make_Temporary (Loc, 'P'),
1179 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1180 Expression =>
1181 Unchecked_Convert_To (PtrT,
1182 New_Occurrence_Of (Temp, Loc)));
1183
1184 Insert_Action (N, New_Decl);
1185
1186 Temp_Decl := New_Decl;
1187 Temp := Defining_Identifier (New_Decl);
1188 end;
1189 end if;
1190
1191 Apply_Accessibility_Check (Temp);
1192
1193 -- Generate the tag assignment
1194
1195 -- Suppress the tag assignment when VM_Target because VM tags are
1196 -- represented implicitly in objects.
1197
1198 if not Tagged_Type_Expansion then
1199 null;
1200
1201 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1202 -- interface objects because in this case the tag does not change.
1203
1204 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
1205 pragma Assert (Is_Class_Wide_Type
1206 (Directly_Designated_Type (Etype (N))));
1207 null;
1208
1209 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
1210 TagT := T;
1211 TagR := New_Occurrence_Of (Temp, Loc);
1212
1213 elsif Is_Private_Type (T)
1214 and then Is_Tagged_Type (Underlying_Type (T))
1215 then
1216 TagT := Underlying_Type (T);
1217 TagR :=
1218 Unchecked_Convert_To (Underlying_Type (T),
1219 Make_Explicit_Dereference (Loc,
1220 Prefix => New_Occurrence_Of (Temp, Loc)));
1221 end if;
1222
1223 if Present (TagT) then
1224 declare
1225 Full_T : constant Entity_Id := Underlying_Type (TagT);
1226
1227 begin
1228 Tag_Assign :=
1229 Make_Assignment_Statement (Loc,
1230 Name =>
1231 Make_Selected_Component (Loc,
1232 Prefix => TagR,
1233 Selector_Name =>
1234 New_Occurrence_Of
1235 (First_Tag_Component (Full_T), Loc)),
1236
1237 Expression =>
1238 Unchecked_Convert_To (RTE (RE_Tag),
1239 New_Occurrence_Of
1240 (Elists.Node
1241 (First_Elmt (Access_Disp_Table (Full_T))), Loc)));
1242 end;
1243
1244 -- The previous assignment has to be done in any case
1245
1246 Set_Assignment_OK (Name (Tag_Assign));
1247 Insert_Action (N, Tag_Assign);
1248 end if;
1249
1250 if Needs_Finalization (DesigT) and then Needs_Finalization (T) then
1251
1252 -- Generate an Adjust call if the object will be moved. In Ada
1253 -- 2005, the object may be inherently limited, in which case
1254 -- there is no Adjust procedure, and the object is built in
1255 -- place. In Ada 95, the object can be limited but not
1256 -- inherently limited if this allocator came from a return
1257 -- statement (we're allocating the result on the secondary
1258 -- stack). In that case, the object will be moved, so we _do_
1259 -- want to Adjust.
1260
1261 if not Aggr_In_Place
1262 and then not Is_Limited_View (T)
1263 then
1264 Insert_Action (N,
1265
1266 -- An unchecked conversion is needed in the classwide case
1267 -- because the designated type can be an ancestor of the
1268 -- subtype mark of the allocator.
1269
1270 Make_Adjust_Call
1271 (Obj_Ref =>
1272 Unchecked_Convert_To (T,
1273 Make_Explicit_Dereference (Loc,
1274 Prefix => New_Occurrence_Of (Temp, Loc))),
1275 Typ => T));
1276 end if;
1277
1278 -- Generate:
1279 -- Set_Finalize_Address (<PtrT>FM, <T>FD'Unrestricted_Access);
1280
1281 -- Do not generate this call in the following cases:
1282
1283 -- * .NET/JVM - these targets do not support address arithmetic
1284 -- and unchecked conversion, key elements of Finalize_Address.
1285
1286 -- * CodePeer mode - TSS primitive Finalize_Address is not
1287 -- created in this mode.
1288
1289 if VM_Target = No_VM
1290 and then not CodePeer_Mode
1291 and then Present (Finalization_Master (PtrT))
1292 and then Present (Temp_Decl)
1293 and then Nkind (Expression (Temp_Decl)) = N_Allocator
1294 then
1295 Insert_Action (N,
1296 Make_Set_Finalize_Address_Call
1297 (Loc => Loc,
1298 Typ => T,
1299 Ptr_Typ => PtrT));
1300 end if;
1301 end if;
1302
1303 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1304 Analyze_And_Resolve (N, PtrT);
1305
1306 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1307 -- component containing the secondary dispatch table of the interface
1308 -- type.
1309
1310 if Is_Interface (Directly_Designated_Type (PtrT)) then
1311 Displace_Allocator_Pointer (N);
1312 end if;
1313
1314 elsif Aggr_In_Place then
1315 Temp := Make_Temporary (Loc, 'P', N);
1316 Temp_Decl :=
1317 Make_Object_Declaration (Loc,
1318 Defining_Identifier => Temp,
1319 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1320 Expression =>
1321 Make_Allocator (Loc,
1322 Expression => New_Occurrence_Of (Etype (Exp), Loc)));
1323
1324 -- Copy the Comes_From_Source flag for the allocator we just built,
1325 -- since logically this allocator is a replacement of the original
1326 -- allocator node. This is for proper handling of restriction
1327 -- No_Implicit_Heap_Allocations.
1328
1329 Set_Comes_From_Source
1330 (Expression (Temp_Decl), Comes_From_Source (N));
1331
1332 Set_No_Initialization (Expression (Temp_Decl));
1333 Insert_Action (N, Temp_Decl);
1334
1335 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1336 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1337
1338 -- Attach the object to the associated finalization master. Thisis
1339 -- done manually on .NET/JVM since those compilers do no support
1340 -- pools and cannot benefit from internally generated Allocate and
1341 -- Deallocate procedures.
1342
1343 if VM_Target /= No_VM
1344 and then Is_Controlled (DesigT)
1345 and then Present (Finalization_Master (PtrT))
1346 then
1347 Insert_Action (N,
1348 Make_Attach_Call
1349 (Obj_Ref => New_Occurrence_Of (Temp, Loc),
1350 Ptr_Typ => PtrT));
1351 end if;
1352
1353 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1354 Analyze_And_Resolve (N, PtrT);
1355
1356 elsif Is_Access_Type (T) and then Can_Never_Be_Null (T) then
1357 Install_Null_Excluding_Check (Exp);
1358
1359 elsif Is_Access_Type (DesigT)
1360 and then Nkind (Exp) = N_Allocator
1361 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1362 then
1363 -- Apply constraint to designated subtype indication
1364
1365 Apply_Constraint_Check (Expression (Exp),
1366 Designated_Type (DesigT),
1367 No_Sliding => True);
1368
1369 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1370
1371 -- Propagate constraint_error to enclosing allocator
1372
1373 Rewrite (Exp, New_Copy (Expression (Exp)));
1374 end if;
1375
1376 else
1377 Build_Allocate_Deallocate_Proc (N, True);
1378
1379 -- If we have:
1380 -- type A is access T1;
1381 -- X : A := new T2'(...);
1382 -- T1 and T2 can be different subtypes, and we might need to check
1383 -- both constraints. First check against the type of the qualified
1384 -- expression.
1385
1386 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1387
1388 if Do_Range_Check (Exp) then
1389 Set_Do_Range_Check (Exp, False);
1390 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1391 end if;
1392
1393 -- A check is also needed in cases where the designated subtype is
1394 -- constrained and differs from the subtype given in the qualified
1395 -- expression. Note that the check on the qualified expression does
1396 -- not allow sliding, but this check does (a relaxation from Ada 83).
1397
1398 if Is_Constrained (DesigT)
1399 and then not Subtypes_Statically_Match (T, DesigT)
1400 then
1401 Apply_Constraint_Check
1402 (Exp, DesigT, No_Sliding => False);
1403
1404 if Do_Range_Check (Exp) then
1405 Set_Do_Range_Check (Exp, False);
1406 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1407 end if;
1408 end if;
1409
1410 -- For an access to unconstrained packed array, GIGI needs to see an
1411 -- expression with a constrained subtype in order to compute the
1412 -- proper size for the allocator.
1413
1414 if Is_Array_Type (T)
1415 and then not Is_Constrained (T)
1416 and then Is_Packed (T)
1417 then
1418 declare
1419 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1420 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1421 begin
1422 Insert_Action (Exp,
1423 Make_Subtype_Declaration (Loc,
1424 Defining_Identifier => ConstrT,
1425 Subtype_Indication =>
1426 Make_Subtype_From_Expr (Internal_Exp, T)));
1427 Freeze_Itype (ConstrT, Exp);
1428 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1429 end;
1430 end if;
1431
1432 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1433 -- to a build-in-place function, then access to the allocated object
1434 -- must be passed to the function. Currently we limit such functions
1435 -- to those with constrained limited result subtypes, but eventually
1436 -- we plan to expand the allowed forms of functions that are treated
1437 -- as build-in-place.
1438
1439 if Ada_Version >= Ada_2005
1440 and then Is_Build_In_Place_Function_Call (Exp)
1441 then
1442 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1443 end if;
1444 end if;
1445
1446 exception
1447 when RE_Not_Available =>
1448 return;
1449 end Expand_Allocator_Expression;
1450
1451 -----------------------------
1452 -- Expand_Array_Comparison --
1453 -----------------------------
1454
1455 -- Expansion is only required in the case of array types. For the unpacked
1456 -- case, an appropriate runtime routine is called. For packed cases, and
1457 -- also in some other cases where a runtime routine cannot be called, the
1458 -- form of the expansion is:
1459
1460 -- [body for greater_nn; boolean_expression]
1461
1462 -- The body is built by Make_Array_Comparison_Op, and the form of the
1463 -- Boolean expression depends on the operator involved.
1464
1465 procedure Expand_Array_Comparison (N : Node_Id) is
1466 Loc : constant Source_Ptr := Sloc (N);
1467 Op1 : Node_Id := Left_Opnd (N);
1468 Op2 : Node_Id := Right_Opnd (N);
1469 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1470 Ctyp : constant Entity_Id := Component_Type (Typ1);
1471
1472 Expr : Node_Id;
1473 Func_Body : Node_Id;
1474 Func_Name : Entity_Id;
1475
1476 Comp : RE_Id;
1477
1478 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1479 -- True for byte addressable target
1480
1481 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1482 -- Returns True if the length of the given operand is known to be less
1483 -- than 4. Returns False if this length is known to be four or greater
1484 -- or is not known at compile time.
1485
1486 ------------------------
1487 -- Length_Less_Than_4 --
1488 ------------------------
1489
1490 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1491 Otyp : constant Entity_Id := Etype (Opnd);
1492
1493 begin
1494 if Ekind (Otyp) = E_String_Literal_Subtype then
1495 return String_Literal_Length (Otyp) < 4;
1496
1497 else
1498 declare
1499 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1500 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1501 Hi : constant Node_Id := Type_High_Bound (Ityp);
1502 Lov : Uint;
1503 Hiv : Uint;
1504
1505 begin
1506 if Compile_Time_Known_Value (Lo) then
1507 Lov := Expr_Value (Lo);
1508 else
1509 return False;
1510 end if;
1511
1512 if Compile_Time_Known_Value (Hi) then
1513 Hiv := Expr_Value (Hi);
1514 else
1515 return False;
1516 end if;
1517
1518 return Hiv < Lov + 3;
1519 end;
1520 end if;
1521 end Length_Less_Than_4;
1522
1523 -- Start of processing for Expand_Array_Comparison
1524
1525 begin
1526 -- Deal first with unpacked case, where we can call a runtime routine
1527 -- except that we avoid this for targets for which are not addressable
1528 -- by bytes, and for the JVM/CIL, since they do not support direct
1529 -- addressing of array components.
1530
1531 if not Is_Bit_Packed_Array (Typ1)
1532 and then Byte_Addressable
1533 and then VM_Target = No_VM
1534 then
1535 -- The call we generate is:
1536
1537 -- Compare_Array_xn[_Unaligned]
1538 -- (left'address, right'address, left'length, right'length) <op> 0
1539
1540 -- x = U for unsigned, S for signed
1541 -- n = 8,16,32,64 for component size
1542 -- Add _Unaligned if length < 4 and component size is 8.
1543 -- <op> is the standard comparison operator
1544
1545 if Component_Size (Typ1) = 8 then
1546 if Length_Less_Than_4 (Op1)
1547 or else
1548 Length_Less_Than_4 (Op2)
1549 then
1550 if Is_Unsigned_Type (Ctyp) then
1551 Comp := RE_Compare_Array_U8_Unaligned;
1552 else
1553 Comp := RE_Compare_Array_S8_Unaligned;
1554 end if;
1555
1556 else
1557 if Is_Unsigned_Type (Ctyp) then
1558 Comp := RE_Compare_Array_U8;
1559 else
1560 Comp := RE_Compare_Array_S8;
1561 end if;
1562 end if;
1563
1564 elsif Component_Size (Typ1) = 16 then
1565 if Is_Unsigned_Type (Ctyp) then
1566 Comp := RE_Compare_Array_U16;
1567 else
1568 Comp := RE_Compare_Array_S16;
1569 end if;
1570
1571 elsif Component_Size (Typ1) = 32 then
1572 if Is_Unsigned_Type (Ctyp) then
1573 Comp := RE_Compare_Array_U32;
1574 else
1575 Comp := RE_Compare_Array_S32;
1576 end if;
1577
1578 else pragma Assert (Component_Size (Typ1) = 64);
1579 if Is_Unsigned_Type (Ctyp) then
1580 Comp := RE_Compare_Array_U64;
1581 else
1582 Comp := RE_Compare_Array_S64;
1583 end if;
1584 end if;
1585
1586 Remove_Side_Effects (Op1, Name_Req => True);
1587 Remove_Side_Effects (Op2, Name_Req => True);
1588
1589 Rewrite (Op1,
1590 Make_Function_Call (Sloc (Op1),
1591 Name => New_Occurrence_Of (RTE (Comp), Loc),
1592
1593 Parameter_Associations => New_List (
1594 Make_Attribute_Reference (Loc,
1595 Prefix => Relocate_Node (Op1),
1596 Attribute_Name => Name_Address),
1597
1598 Make_Attribute_Reference (Loc,
1599 Prefix => Relocate_Node (Op2),
1600 Attribute_Name => Name_Address),
1601
1602 Make_Attribute_Reference (Loc,
1603 Prefix => Relocate_Node (Op1),
1604 Attribute_Name => Name_Length),
1605
1606 Make_Attribute_Reference (Loc,
1607 Prefix => Relocate_Node (Op2),
1608 Attribute_Name => Name_Length))));
1609
1610 Rewrite (Op2,
1611 Make_Integer_Literal (Sloc (Op2),
1612 Intval => Uint_0));
1613
1614 Analyze_And_Resolve (Op1, Standard_Integer);
1615 Analyze_And_Resolve (Op2, Standard_Integer);
1616 return;
1617 end if;
1618
1619 -- Cases where we cannot make runtime call
1620
1621 -- For (a <= b) we convert to not (a > b)
1622
1623 if Chars (N) = Name_Op_Le then
1624 Rewrite (N,
1625 Make_Op_Not (Loc,
1626 Right_Opnd =>
1627 Make_Op_Gt (Loc,
1628 Left_Opnd => Op1,
1629 Right_Opnd => Op2)));
1630 Analyze_And_Resolve (N, Standard_Boolean);
1631 return;
1632
1633 -- For < the Boolean expression is
1634 -- greater__nn (op2, op1)
1635
1636 elsif Chars (N) = Name_Op_Lt then
1637 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1638
1639 -- Switch operands
1640
1641 Op1 := Right_Opnd (N);
1642 Op2 := Left_Opnd (N);
1643
1644 -- For (a >= b) we convert to not (a < b)
1645
1646 elsif Chars (N) = Name_Op_Ge then
1647 Rewrite (N,
1648 Make_Op_Not (Loc,
1649 Right_Opnd =>
1650 Make_Op_Lt (Loc,
1651 Left_Opnd => Op1,
1652 Right_Opnd => Op2)));
1653 Analyze_And_Resolve (N, Standard_Boolean);
1654 return;
1655
1656 -- For > the Boolean expression is
1657 -- greater__nn (op1, op2)
1658
1659 else
1660 pragma Assert (Chars (N) = Name_Op_Gt);
1661 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1662 end if;
1663
1664 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1665 Expr :=
1666 Make_Function_Call (Loc,
1667 Name => New_Occurrence_Of (Func_Name, Loc),
1668 Parameter_Associations => New_List (Op1, Op2));
1669
1670 Insert_Action (N, Func_Body);
1671 Rewrite (N, Expr);
1672 Analyze_And_Resolve (N, Standard_Boolean);
1673
1674 exception
1675 when RE_Not_Available =>
1676 return;
1677 end Expand_Array_Comparison;
1678
1679 ---------------------------
1680 -- Expand_Array_Equality --
1681 ---------------------------
1682
1683 -- Expand an equality function for multi-dimensional arrays. Here is an
1684 -- example of such a function for Nb_Dimension = 2
1685
1686 -- function Enn (A : atyp; B : btyp) return boolean is
1687 -- begin
1688 -- if (A'length (1) = 0 or else A'length (2) = 0)
1689 -- and then
1690 -- (B'length (1) = 0 or else B'length (2) = 0)
1691 -- then
1692 -- return True; -- RM 4.5.2(22)
1693 -- end if;
1694
1695 -- if A'length (1) /= B'length (1)
1696 -- or else
1697 -- A'length (2) /= B'length (2)
1698 -- then
1699 -- return False; -- RM 4.5.2(23)
1700 -- end if;
1701
1702 -- declare
1703 -- A1 : Index_T1 := A'first (1);
1704 -- B1 : Index_T1 := B'first (1);
1705 -- begin
1706 -- loop
1707 -- declare
1708 -- A2 : Index_T2 := A'first (2);
1709 -- B2 : Index_T2 := B'first (2);
1710 -- begin
1711 -- loop
1712 -- if A (A1, A2) /= B (B1, B2) then
1713 -- return False;
1714 -- end if;
1715
1716 -- exit when A2 = A'last (2);
1717 -- A2 := Index_T2'succ (A2);
1718 -- B2 := Index_T2'succ (B2);
1719 -- end loop;
1720 -- end;
1721
1722 -- exit when A1 = A'last (1);
1723 -- A1 := Index_T1'succ (A1);
1724 -- B1 := Index_T1'succ (B1);
1725 -- end loop;
1726 -- end;
1727
1728 -- return true;
1729 -- end Enn;
1730
1731 -- Note on the formal types used (atyp and btyp). If either of the arrays
1732 -- is of a private type, we use the underlying type, and do an unchecked
1733 -- conversion of the actual. If either of the arrays has a bound depending
1734 -- on a discriminant, then we use the base type since otherwise we have an
1735 -- escaped discriminant in the function.
1736
1737 -- If both arrays are constrained and have the same bounds, we can generate
1738 -- a loop with an explicit iteration scheme using a 'Range attribute over
1739 -- the first array.
1740
1741 function Expand_Array_Equality
1742 (Nod : Node_Id;
1743 Lhs : Node_Id;
1744 Rhs : Node_Id;
1745 Bodies : List_Id;
1746 Typ : Entity_Id) return Node_Id
1747 is
1748 Loc : constant Source_Ptr := Sloc (Nod);
1749 Decls : constant List_Id := New_List;
1750 Index_List1 : constant List_Id := New_List;
1751 Index_List2 : constant List_Id := New_List;
1752
1753 Actuals : List_Id;
1754 Formals : List_Id;
1755 Func_Name : Entity_Id;
1756 Func_Body : Node_Id;
1757
1758 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1759 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1760
1761 Ltyp : Entity_Id;
1762 Rtyp : Entity_Id;
1763 -- The parameter types to be used for the formals
1764
1765 function Arr_Attr
1766 (Arr : Entity_Id;
1767 Nam : Name_Id;
1768 Num : Int) return Node_Id;
1769 -- This builds the attribute reference Arr'Nam (Expr)
1770
1771 function Component_Equality (Typ : Entity_Id) return Node_Id;
1772 -- Create one statement to compare corresponding components, designated
1773 -- by a full set of indexes.
1774
1775 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1776 -- Given one of the arguments, computes the appropriate type to be used
1777 -- for that argument in the corresponding function formal
1778
1779 function Handle_One_Dimension
1780 (N : Int;
1781 Index : Node_Id) return Node_Id;
1782 -- This procedure returns the following code
1783 --
1784 -- declare
1785 -- Bn : Index_T := B'First (N);
1786 -- begin
1787 -- loop
1788 -- xxx
1789 -- exit when An = A'Last (N);
1790 -- An := Index_T'Succ (An)
1791 -- Bn := Index_T'Succ (Bn)
1792 -- end loop;
1793 -- end;
1794 --
1795 -- If both indexes are constrained and identical, the procedure
1796 -- returns a simpler loop:
1797 --
1798 -- for An in A'Range (N) loop
1799 -- xxx
1800 -- end loop
1801 --
1802 -- N is the dimension for which we are generating a loop. Index is the
1803 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1804 -- xxx statement is either the loop or declare for the next dimension
1805 -- or if this is the last dimension the comparison of corresponding
1806 -- components of the arrays.
1807 --
1808 -- The actual way the code works is to return the comparison of
1809 -- corresponding components for the N+1 call. That's neater.
1810
1811 function Test_Empty_Arrays return Node_Id;
1812 -- This function constructs the test for both arrays being empty
1813 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1814 -- and then
1815 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1816
1817 function Test_Lengths_Correspond return Node_Id;
1818 -- This function constructs the test for arrays having different lengths
1819 -- in at least one index position, in which case the resulting code is:
1820
1821 -- A'length (1) /= B'length (1)
1822 -- or else
1823 -- A'length (2) /= B'length (2)
1824 -- or else
1825 -- ...
1826
1827 --------------
1828 -- Arr_Attr --
1829 --------------
1830
1831 function Arr_Attr
1832 (Arr : Entity_Id;
1833 Nam : Name_Id;
1834 Num : Int) return Node_Id
1835 is
1836 begin
1837 return
1838 Make_Attribute_Reference (Loc,
1839 Attribute_Name => Nam,
1840 Prefix => New_Occurrence_Of (Arr, Loc),
1841 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1842 end Arr_Attr;
1843
1844 ------------------------
1845 -- Component_Equality --
1846 ------------------------
1847
1848 function Component_Equality (Typ : Entity_Id) return Node_Id is
1849 Test : Node_Id;
1850 L, R : Node_Id;
1851
1852 begin
1853 -- if a(i1...) /= b(j1...) then return false; end if;
1854
1855 L :=
1856 Make_Indexed_Component (Loc,
1857 Prefix => Make_Identifier (Loc, Chars (A)),
1858 Expressions => Index_List1);
1859
1860 R :=
1861 Make_Indexed_Component (Loc,
1862 Prefix => Make_Identifier (Loc, Chars (B)),
1863 Expressions => Index_List2);
1864
1865 Test := Expand_Composite_Equality
1866 (Nod, Component_Type (Typ), L, R, Decls);
1867
1868 -- If some (sub)component is an unchecked_union, the whole operation
1869 -- will raise program error.
1870
1871 if Nkind (Test) = N_Raise_Program_Error then
1872
1873 -- This node is going to be inserted at a location where a
1874 -- statement is expected: clear its Etype so analysis will set
1875 -- it to the expected Standard_Void_Type.
1876
1877 Set_Etype (Test, Empty);
1878 return Test;
1879
1880 else
1881 return
1882 Make_Implicit_If_Statement (Nod,
1883 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1884 Then_Statements => New_List (
1885 Make_Simple_Return_Statement (Loc,
1886 Expression => New_Occurrence_Of (Standard_False, Loc))));
1887 end if;
1888 end Component_Equality;
1889
1890 ------------------
1891 -- Get_Arg_Type --
1892 ------------------
1893
1894 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1895 T : Entity_Id;
1896 X : Node_Id;
1897
1898 begin
1899 T := Etype (N);
1900
1901 if No (T) then
1902 return Typ;
1903
1904 else
1905 T := Underlying_Type (T);
1906
1907 X := First_Index (T);
1908 while Present (X) loop
1909 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1910 or else
1911 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1912 then
1913 T := Base_Type (T);
1914 exit;
1915 end if;
1916
1917 Next_Index (X);
1918 end loop;
1919
1920 return T;
1921 end if;
1922 end Get_Arg_Type;
1923
1924 --------------------------
1925 -- Handle_One_Dimension --
1926 ---------------------------
1927
1928 function Handle_One_Dimension
1929 (N : Int;
1930 Index : Node_Id) return Node_Id
1931 is
1932 Need_Separate_Indexes : constant Boolean :=
1933 Ltyp /= Rtyp or else not Is_Constrained (Ltyp);
1934 -- If the index types are identical, and we are working with
1935 -- constrained types, then we can use the same index for both
1936 -- of the arrays.
1937
1938 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1939
1940 Bn : Entity_Id;
1941 Index_T : Entity_Id;
1942 Stm_List : List_Id;
1943 Loop_Stm : Node_Id;
1944
1945 begin
1946 if N > Number_Dimensions (Ltyp) then
1947 return Component_Equality (Ltyp);
1948 end if;
1949
1950 -- Case where we generate a loop
1951
1952 Index_T := Base_Type (Etype (Index));
1953
1954 if Need_Separate_Indexes then
1955 Bn := Make_Temporary (Loc, 'B');
1956 else
1957 Bn := An;
1958 end if;
1959
1960 Append (New_Occurrence_Of (An, Loc), Index_List1);
1961 Append (New_Occurrence_Of (Bn, Loc), Index_List2);
1962
1963 Stm_List := New_List (
1964 Handle_One_Dimension (N + 1, Next_Index (Index)));
1965
1966 if Need_Separate_Indexes then
1967
1968 -- Generate guard for loop, followed by increments of indexes
1969
1970 Append_To (Stm_List,
1971 Make_Exit_Statement (Loc,
1972 Condition =>
1973 Make_Op_Eq (Loc,
1974 Left_Opnd => New_Occurrence_Of (An, Loc),
1975 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1976
1977 Append_To (Stm_List,
1978 Make_Assignment_Statement (Loc,
1979 Name => New_Occurrence_Of (An, Loc),
1980 Expression =>
1981 Make_Attribute_Reference (Loc,
1982 Prefix => New_Occurrence_Of (Index_T, Loc),
1983 Attribute_Name => Name_Succ,
1984 Expressions => New_List (
1985 New_Occurrence_Of (An, Loc)))));
1986
1987 Append_To (Stm_List,
1988 Make_Assignment_Statement (Loc,
1989 Name => New_Occurrence_Of (Bn, Loc),
1990 Expression =>
1991 Make_Attribute_Reference (Loc,
1992 Prefix => New_Occurrence_Of (Index_T, Loc),
1993 Attribute_Name => Name_Succ,
1994 Expressions => New_List (
1995 New_Occurrence_Of (Bn, Loc)))));
1996 end if;
1997
1998 -- If separate indexes, we need a declare block for An and Bn, and a
1999 -- loop without an iteration scheme.
2000
2001 if Need_Separate_Indexes then
2002 Loop_Stm :=
2003 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
2004
2005 return
2006 Make_Block_Statement (Loc,
2007 Declarations => New_List (
2008 Make_Object_Declaration (Loc,
2009 Defining_Identifier => An,
2010 Object_Definition => New_Occurrence_Of (Index_T, Loc),
2011 Expression => Arr_Attr (A, Name_First, N)),
2012
2013 Make_Object_Declaration (Loc,
2014 Defining_Identifier => Bn,
2015 Object_Definition => New_Occurrence_Of (Index_T, Loc),
2016 Expression => Arr_Attr (B, Name_First, N))),
2017
2018 Handled_Statement_Sequence =>
2019 Make_Handled_Sequence_Of_Statements (Loc,
2020 Statements => New_List (Loop_Stm)));
2021
2022 -- If no separate indexes, return loop statement with explicit
2023 -- iteration scheme on its own
2024
2025 else
2026 Loop_Stm :=
2027 Make_Implicit_Loop_Statement (Nod,
2028 Statements => Stm_List,
2029 Iteration_Scheme =>
2030 Make_Iteration_Scheme (Loc,
2031 Loop_Parameter_Specification =>
2032 Make_Loop_Parameter_Specification (Loc,
2033 Defining_Identifier => An,
2034 Discrete_Subtype_Definition =>
2035 Arr_Attr (A, Name_Range, N))));
2036 return Loop_Stm;
2037 end if;
2038 end Handle_One_Dimension;
2039
2040 -----------------------
2041 -- Test_Empty_Arrays --
2042 -----------------------
2043
2044 function Test_Empty_Arrays return Node_Id is
2045 Alist : Node_Id;
2046 Blist : Node_Id;
2047
2048 Atest : Node_Id;
2049 Btest : Node_Id;
2050
2051 begin
2052 Alist := Empty;
2053 Blist := Empty;
2054 for J in 1 .. Number_Dimensions (Ltyp) loop
2055 Atest :=
2056 Make_Op_Eq (Loc,
2057 Left_Opnd => Arr_Attr (A, Name_Length, J),
2058 Right_Opnd => Make_Integer_Literal (Loc, 0));
2059
2060 Btest :=
2061 Make_Op_Eq (Loc,
2062 Left_Opnd => Arr_Attr (B, Name_Length, J),
2063 Right_Opnd => Make_Integer_Literal (Loc, 0));
2064
2065 if No (Alist) then
2066 Alist := Atest;
2067 Blist := Btest;
2068
2069 else
2070 Alist :=
2071 Make_Or_Else (Loc,
2072 Left_Opnd => Relocate_Node (Alist),
2073 Right_Opnd => Atest);
2074
2075 Blist :=
2076 Make_Or_Else (Loc,
2077 Left_Opnd => Relocate_Node (Blist),
2078 Right_Opnd => Btest);
2079 end if;
2080 end loop;
2081
2082 return
2083 Make_And_Then (Loc,
2084 Left_Opnd => Alist,
2085 Right_Opnd => Blist);
2086 end Test_Empty_Arrays;
2087
2088 -----------------------------
2089 -- Test_Lengths_Correspond --
2090 -----------------------------
2091
2092 function Test_Lengths_Correspond return Node_Id is
2093 Result : Node_Id;
2094 Rtest : Node_Id;
2095
2096 begin
2097 Result := Empty;
2098 for J in 1 .. Number_Dimensions (Ltyp) loop
2099 Rtest :=
2100 Make_Op_Ne (Loc,
2101 Left_Opnd => Arr_Attr (A, Name_Length, J),
2102 Right_Opnd => Arr_Attr (B, Name_Length, J));
2103
2104 if No (Result) then
2105 Result := Rtest;
2106 else
2107 Result :=
2108 Make_Or_Else (Loc,
2109 Left_Opnd => Relocate_Node (Result),
2110 Right_Opnd => Rtest);
2111 end if;
2112 end loop;
2113
2114 return Result;
2115 end Test_Lengths_Correspond;
2116
2117 -- Start of processing for Expand_Array_Equality
2118
2119 begin
2120 Ltyp := Get_Arg_Type (Lhs);
2121 Rtyp := Get_Arg_Type (Rhs);
2122
2123 -- For now, if the argument types are not the same, go to the base type,
2124 -- since the code assumes that the formals have the same type. This is
2125 -- fixable in future ???
2126
2127 if Ltyp /= Rtyp then
2128 Ltyp := Base_Type (Ltyp);
2129 Rtyp := Base_Type (Rtyp);
2130 pragma Assert (Ltyp = Rtyp);
2131 end if;
2132
2133 -- Build list of formals for function
2134
2135 Formals := New_List (
2136 Make_Parameter_Specification (Loc,
2137 Defining_Identifier => A,
2138 Parameter_Type => New_Occurrence_Of (Ltyp, Loc)),
2139
2140 Make_Parameter_Specification (Loc,
2141 Defining_Identifier => B,
2142 Parameter_Type => New_Occurrence_Of (Rtyp, Loc)));
2143
2144 Func_Name := Make_Temporary (Loc, 'E');
2145
2146 -- Build statement sequence for function
2147
2148 Func_Body :=
2149 Make_Subprogram_Body (Loc,
2150 Specification =>
2151 Make_Function_Specification (Loc,
2152 Defining_Unit_Name => Func_Name,
2153 Parameter_Specifications => Formals,
2154 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
2155
2156 Declarations => Decls,
2157
2158 Handled_Statement_Sequence =>
2159 Make_Handled_Sequence_Of_Statements (Loc,
2160 Statements => New_List (
2161
2162 Make_Implicit_If_Statement (Nod,
2163 Condition => Test_Empty_Arrays,
2164 Then_Statements => New_List (
2165 Make_Simple_Return_Statement (Loc,
2166 Expression =>
2167 New_Occurrence_Of (Standard_True, Loc)))),
2168
2169 Make_Implicit_If_Statement (Nod,
2170 Condition => Test_Lengths_Correspond,
2171 Then_Statements => New_List (
2172 Make_Simple_Return_Statement (Loc,
2173 Expression =>
2174 New_Occurrence_Of (Standard_False, Loc)))),
2175
2176 Handle_One_Dimension (1, First_Index (Ltyp)),
2177
2178 Make_Simple_Return_Statement (Loc,
2179 Expression => New_Occurrence_Of (Standard_True, Loc)))));
2180
2181 Set_Has_Completion (Func_Name, True);
2182 Set_Is_Inlined (Func_Name);
2183
2184 -- If the array type is distinct from the type of the arguments, it
2185 -- is the full view of a private type. Apply an unchecked conversion
2186 -- to insure that analysis of the call succeeds.
2187
2188 declare
2189 L, R : Node_Id;
2190
2191 begin
2192 L := Lhs;
2193 R := Rhs;
2194
2195 if No (Etype (Lhs))
2196 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
2197 then
2198 L := OK_Convert_To (Ltyp, Lhs);
2199 end if;
2200
2201 if No (Etype (Rhs))
2202 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
2203 then
2204 R := OK_Convert_To (Rtyp, Rhs);
2205 end if;
2206
2207 Actuals := New_List (L, R);
2208 end;
2209
2210 Append_To (Bodies, Func_Body);
2211
2212 return
2213 Make_Function_Call (Loc,
2214 Name => New_Occurrence_Of (Func_Name, Loc),
2215 Parameter_Associations => Actuals);
2216 end Expand_Array_Equality;
2217
2218 -----------------------------
2219 -- Expand_Boolean_Operator --
2220 -----------------------------
2221
2222 -- Note that we first get the actual subtypes of the operands, since we
2223 -- always want to deal with types that have bounds.
2224
2225 procedure Expand_Boolean_Operator (N : Node_Id) is
2226 Typ : constant Entity_Id := Etype (N);
2227
2228 begin
2229 -- Special case of bit packed array where both operands are known to be
2230 -- properly aligned. In this case we use an efficient run time routine
2231 -- to carry out the operation (see System.Bit_Ops).
2232
2233 if Is_Bit_Packed_Array (Typ)
2234 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
2235 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
2236 then
2237 Expand_Packed_Boolean_Operator (N);
2238 return;
2239 end if;
2240
2241 -- For the normal non-packed case, the general expansion is to build
2242 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2243 -- and then inserting it into the tree. The original operator node is
2244 -- then rewritten as a call to this function. We also use this in the
2245 -- packed case if either operand is a possibly unaligned object.
2246
2247 declare
2248 Loc : constant Source_Ptr := Sloc (N);
2249 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2250 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2251 Func_Body : Node_Id;
2252 Func_Name : Entity_Id;
2253
2254 begin
2255 Convert_To_Actual_Subtype (L);
2256 Convert_To_Actual_Subtype (R);
2257 Ensure_Defined (Etype (L), N);
2258 Ensure_Defined (Etype (R), N);
2259 Apply_Length_Check (R, Etype (L));
2260
2261 if Nkind (N) = N_Op_Xor then
2262 Silly_Boolean_Array_Xor_Test (N, Etype (L));
2263 end if;
2264
2265 if Nkind (Parent (N)) = N_Assignment_Statement
2266 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
2267 then
2268 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
2269
2270 elsif Nkind (Parent (N)) = N_Op_Not
2271 and then Nkind (N) = N_Op_And
2272 and then
2273 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
2274 then
2275 return;
2276 else
2277
2278 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
2279 Func_Name := Defining_Unit_Name (Specification (Func_Body));
2280 Insert_Action (N, Func_Body);
2281
2282 -- Now rewrite the expression with a call
2283
2284 Rewrite (N,
2285 Make_Function_Call (Loc,
2286 Name => New_Occurrence_Of (Func_Name, Loc),
2287 Parameter_Associations =>
2288 New_List (
2289 L,
2290 Make_Type_Conversion
2291 (Loc, New_Occurrence_Of (Etype (L), Loc), R))));
2292
2293 Analyze_And_Resolve (N, Typ);
2294 end if;
2295 end;
2296 end Expand_Boolean_Operator;
2297
2298 ------------------------------------------------
2299 -- Expand_Compare_Minimize_Eliminate_Overflow --
2300 ------------------------------------------------
2301
2302 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is
2303 Loc : constant Source_Ptr := Sloc (N);
2304
2305 Result_Type : constant Entity_Id := Etype (N);
2306 -- Capture result type (could be a derived boolean type)
2307
2308 Llo, Lhi : Uint;
2309 Rlo, Rhi : Uint;
2310
2311 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
2312 -- Entity for Long_Long_Integer'Base
2313
2314 Check : constant Overflow_Mode_Type := Overflow_Check_Mode;
2315 -- Current overflow checking mode
2316
2317 procedure Set_True;
2318 procedure Set_False;
2319 -- These procedures rewrite N with an occurrence of Standard_True or
2320 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2321
2322 ---------------
2323 -- Set_False --
2324 ---------------
2325
2326 procedure Set_False is
2327 begin
2328 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2329 Warn_On_Known_Condition (N);
2330 end Set_False;
2331
2332 --------------
2333 -- Set_True --
2334 --------------
2335
2336 procedure Set_True is
2337 begin
2338 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
2339 Warn_On_Known_Condition (N);
2340 end Set_True;
2341
2342 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2343
2344 begin
2345 -- Nothing to do unless we have a comparison operator with operands
2346 -- that are signed integer types, and we are operating in either
2347 -- MINIMIZED or ELIMINATED overflow checking mode.
2348
2349 if Nkind (N) not in N_Op_Compare
2350 or else Check not in Minimized_Or_Eliminated
2351 or else not Is_Signed_Integer_Type (Etype (Left_Opnd (N)))
2352 then
2353 return;
2354 end if;
2355
2356 -- OK, this is the case we are interested in. First step is to process
2357 -- our operands using the Minimize_Eliminate circuitry which applies
2358 -- this processing to the two operand subtrees.
2359
2360 Minimize_Eliminate_Overflows
2361 (Left_Opnd (N), Llo, Lhi, Top_Level => False);
2362 Minimize_Eliminate_Overflows
2363 (Right_Opnd (N), Rlo, Rhi, Top_Level => False);
2364
2365 -- See if the range information decides the result of the comparison.
2366 -- We can only do this if we in fact have full range information (which
2367 -- won't be the case if either operand is bignum at this stage).
2368
2369 if Llo /= No_Uint and then Rlo /= No_Uint then
2370 case N_Op_Compare (Nkind (N)) is
2371 when N_Op_Eq =>
2372 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2373 Set_True;
2374 elsif Llo > Rhi or else Lhi < Rlo then
2375 Set_False;
2376 end if;
2377
2378 when N_Op_Ge =>
2379 if Llo >= Rhi then
2380 Set_True;
2381 elsif Lhi < Rlo then
2382 Set_False;
2383 end if;
2384
2385 when N_Op_Gt =>
2386 if Llo > Rhi then
2387 Set_True;
2388 elsif Lhi <= Rlo then
2389 Set_False;
2390 end if;
2391
2392 when N_Op_Le =>
2393 if Llo > Rhi then
2394 Set_False;
2395 elsif Lhi <= Rlo then
2396 Set_True;
2397 end if;
2398
2399 when N_Op_Lt =>
2400 if Llo >= Rhi then
2401 Set_False;
2402 elsif Lhi < Rlo then
2403 Set_True;
2404 end if;
2405
2406 when N_Op_Ne =>
2407 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2408 Set_False;
2409 elsif Llo > Rhi or else Lhi < Rlo then
2410 Set_True;
2411 end if;
2412 end case;
2413
2414 -- All done if we did the rewrite
2415
2416 if Nkind (N) not in N_Op_Compare then
2417 return;
2418 end if;
2419 end if;
2420
2421 -- Otherwise, time to do the comparison
2422
2423 declare
2424 Ltype : constant Entity_Id := Etype (Left_Opnd (N));
2425 Rtype : constant Entity_Id := Etype (Right_Opnd (N));
2426
2427 begin
2428 -- If the two operands have the same signed integer type we are
2429 -- all set, nothing more to do. This is the case where either
2430 -- both operands were unchanged, or we rewrote both of them to
2431 -- be Long_Long_Integer.
2432
2433 -- Note: Entity for the comparison may be wrong, but it's not worth
2434 -- the effort to change it, since the back end does not use it.
2435
2436 if Is_Signed_Integer_Type (Ltype)
2437 and then Base_Type (Ltype) = Base_Type (Rtype)
2438 then
2439 return;
2440
2441 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2442
2443 elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then
2444 declare
2445 Left : Node_Id := Left_Opnd (N);
2446 Right : Node_Id := Right_Opnd (N);
2447 -- Bignum references for left and right operands
2448
2449 begin
2450 if not Is_RTE (Ltype, RE_Bignum) then
2451 Left := Convert_To_Bignum (Left);
2452 elsif not Is_RTE (Rtype, RE_Bignum) then
2453 Right := Convert_To_Bignum (Right);
2454 end if;
2455
2456 -- We rewrite our node with:
2457
2458 -- do
2459 -- Bnn : Result_Type;
2460 -- declare
2461 -- M : Mark_Id := SS_Mark;
2462 -- begin
2463 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2464 -- SS_Release (M);
2465 -- end;
2466 -- in
2467 -- Bnn
2468 -- end
2469
2470 declare
2471 Blk : constant Node_Id := Make_Bignum_Block (Loc);
2472 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
2473 Ent : RE_Id;
2474
2475 begin
2476 case N_Op_Compare (Nkind (N)) is
2477 when N_Op_Eq => Ent := RE_Big_EQ;
2478 when N_Op_Ge => Ent := RE_Big_GE;
2479 when N_Op_Gt => Ent := RE_Big_GT;
2480 when N_Op_Le => Ent := RE_Big_LE;
2481 when N_Op_Lt => Ent := RE_Big_LT;
2482 when N_Op_Ne => Ent := RE_Big_NE;
2483 end case;
2484
2485 -- Insert assignment to Bnn into the bignum block
2486
2487 Insert_Before
2488 (First (Statements (Handled_Statement_Sequence (Blk))),
2489 Make_Assignment_Statement (Loc,
2490 Name => New_Occurrence_Of (Bnn, Loc),
2491 Expression =>
2492 Make_Function_Call (Loc,
2493 Name =>
2494 New_Occurrence_Of (RTE (Ent), Loc),
2495 Parameter_Associations => New_List (Left, Right))));
2496
2497 -- Now do the rewrite with expression actions
2498
2499 Rewrite (N,
2500 Make_Expression_With_Actions (Loc,
2501 Actions => New_List (
2502 Make_Object_Declaration (Loc,
2503 Defining_Identifier => Bnn,
2504 Object_Definition =>
2505 New_Occurrence_Of (Result_Type, Loc)),
2506 Blk),
2507 Expression => New_Occurrence_Of (Bnn, Loc)));
2508 Analyze_And_Resolve (N, Result_Type);
2509 end;
2510 end;
2511
2512 -- No bignums involved, but types are different, so we must have
2513 -- rewritten one of the operands as a Long_Long_Integer but not
2514 -- the other one.
2515
2516 -- If left operand is Long_Long_Integer, convert right operand
2517 -- and we are done (with a comparison of two Long_Long_Integers).
2518
2519 elsif Ltype = LLIB then
2520 Convert_To_And_Rewrite (LLIB, Right_Opnd (N));
2521 Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks);
2522 return;
2523
2524 -- If right operand is Long_Long_Integer, convert left operand
2525 -- and we are done (with a comparison of two Long_Long_Integers).
2526
2527 -- This is the only remaining possibility
2528
2529 else pragma Assert (Rtype = LLIB);
2530 Convert_To_And_Rewrite (LLIB, Left_Opnd (N));
2531 Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks);
2532 return;
2533 end if;
2534 end;
2535 end Expand_Compare_Minimize_Eliminate_Overflow;
2536
2537 -------------------------------
2538 -- Expand_Composite_Equality --
2539 -------------------------------
2540
2541 -- This function is only called for comparing internal fields of composite
2542 -- types when these fields are themselves composites. This is a special
2543 -- case because it is not possible to respect normal Ada visibility rules.
2544
2545 function Expand_Composite_Equality
2546 (Nod : Node_Id;
2547 Typ : Entity_Id;
2548 Lhs : Node_Id;
2549 Rhs : Node_Id;
2550 Bodies : List_Id) return Node_Id
2551 is
2552 Loc : constant Source_Ptr := Sloc (Nod);
2553 Full_Type : Entity_Id;
2554 Prim : Elmt_Id;
2555 Eq_Op : Entity_Id;
2556
2557 function Find_Primitive_Eq return Node_Id;
2558 -- AI05-0123: Locate primitive equality for type if it exists, and
2559 -- build the corresponding call. If operation is abstract, replace
2560 -- call with an explicit raise. Return Empty if there is no primitive.
2561
2562 -----------------------
2563 -- Find_Primitive_Eq --
2564 -----------------------
2565
2566 function Find_Primitive_Eq return Node_Id is
2567 Prim_E : Elmt_Id;
2568 Prim : Node_Id;
2569
2570 begin
2571 Prim_E := First_Elmt (Collect_Primitive_Operations (Typ));
2572 while Present (Prim_E) loop
2573 Prim := Node (Prim_E);
2574
2575 -- Locate primitive equality with the right signature
2576
2577 if Chars (Prim) = Name_Op_Eq
2578 and then Etype (First_Formal (Prim)) =
2579 Etype (Next_Formal (First_Formal (Prim)))
2580 and then Etype (Prim) = Standard_Boolean
2581 then
2582 if Is_Abstract_Subprogram (Prim) then
2583 return
2584 Make_Raise_Program_Error (Loc,
2585 Reason => PE_Explicit_Raise);
2586
2587 else
2588 return
2589 Make_Function_Call (Loc,
2590 Name => New_Occurrence_Of (Prim, Loc),
2591 Parameter_Associations => New_List (Lhs, Rhs));
2592 end if;
2593 end if;
2594
2595 Next_Elmt (Prim_E);
2596 end loop;
2597
2598 -- If not found, predefined operation will be used
2599
2600 return Empty;
2601 end Find_Primitive_Eq;
2602
2603 -- Start of processing for Expand_Composite_Equality
2604
2605 begin
2606 if Is_Private_Type (Typ) then
2607 Full_Type := Underlying_Type (Typ);
2608 else
2609 Full_Type := Typ;
2610 end if;
2611
2612 -- If the private type has no completion the context may be the
2613 -- expansion of a composite equality for a composite type with some
2614 -- still incomplete components. The expression will not be analyzed
2615 -- until the enclosing type is completed, at which point this will be
2616 -- properly expanded, unless there is a bona fide completion error.
2617
2618 if No (Full_Type) then
2619 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2620 end if;
2621
2622 Full_Type := Base_Type (Full_Type);
2623
2624 -- When the base type itself is private, use the full view to expand
2625 -- the composite equality.
2626
2627 if Is_Private_Type (Full_Type) then
2628 Full_Type := Underlying_Type (Full_Type);
2629 end if;
2630
2631 -- Case of array types
2632
2633 if Is_Array_Type (Full_Type) then
2634
2635 -- If the operand is an elementary type other than a floating-point
2636 -- type, then we can simply use the built-in block bitwise equality,
2637 -- since the predefined equality operators always apply and bitwise
2638 -- equality is fine for all these cases.
2639
2640 if Is_Elementary_Type (Component_Type (Full_Type))
2641 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2642 then
2643 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2644
2645 -- For composite component types, and floating-point types, use the
2646 -- expansion. This deals with tagged component types (where we use
2647 -- the applicable equality routine) and floating-point, (where we
2648 -- need to worry about negative zeroes), and also the case of any
2649 -- composite type recursively containing such fields.
2650
2651 else
2652 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2653 end if;
2654
2655 -- Case of tagged record types
2656
2657 elsif Is_Tagged_Type (Full_Type) then
2658
2659 -- Call the primitive operation "=" of this type
2660
2661 if Is_Class_Wide_Type (Full_Type) then
2662 Full_Type := Root_Type (Full_Type);
2663 end if;
2664
2665 -- If this is derived from an untagged private type completed with a
2666 -- tagged type, it does not have a full view, so we use the primitive
2667 -- operations of the private type. This check should no longer be
2668 -- necessary when these types receive their full views ???
2669
2670 if Is_Private_Type (Typ)
2671 and then not Is_Tagged_Type (Typ)
2672 and then not Is_Controlled (Typ)
2673 and then Is_Derived_Type (Typ)
2674 and then No (Full_View (Typ))
2675 then
2676 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2677 else
2678 Prim := First_Elmt (Primitive_Operations (Full_Type));
2679 end if;
2680
2681 loop
2682 Eq_Op := Node (Prim);
2683 exit when Chars (Eq_Op) = Name_Op_Eq
2684 and then Etype (First_Formal (Eq_Op)) =
2685 Etype (Next_Formal (First_Formal (Eq_Op)))
2686 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2687 Next_Elmt (Prim);
2688 pragma Assert (Present (Prim));
2689 end loop;
2690
2691 Eq_Op := Node (Prim);
2692
2693 return
2694 Make_Function_Call (Loc,
2695 Name => New_Occurrence_Of (Eq_Op, Loc),
2696 Parameter_Associations =>
2697 New_List
2698 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2699 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2700
2701 -- Case of untagged record types
2702
2703 elsif Is_Record_Type (Full_Type) then
2704 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2705
2706 if Present (Eq_Op) then
2707 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2708
2709 -- Inherited equality from parent type. Convert the actuals to
2710 -- match signature of operation.
2711
2712 declare
2713 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2714
2715 begin
2716 return
2717 Make_Function_Call (Loc,
2718 Name => New_Occurrence_Of (Eq_Op, Loc),
2719 Parameter_Associations => New_List (
2720 OK_Convert_To (T, Lhs),
2721 OK_Convert_To (T, Rhs)));
2722 end;
2723
2724 else
2725 -- Comparison between Unchecked_Union components
2726
2727 if Is_Unchecked_Union (Full_Type) then
2728 declare
2729 Lhs_Type : Node_Id := Full_Type;
2730 Rhs_Type : Node_Id := Full_Type;
2731 Lhs_Discr_Val : Node_Id;
2732 Rhs_Discr_Val : Node_Id;
2733
2734 begin
2735 -- Lhs subtype
2736
2737 if Nkind (Lhs) = N_Selected_Component then
2738 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2739 end if;
2740
2741 -- Rhs subtype
2742
2743 if Nkind (Rhs) = N_Selected_Component then
2744 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2745 end if;
2746
2747 -- Lhs of the composite equality
2748
2749 if Is_Constrained (Lhs_Type) then
2750
2751 -- Since the enclosing record type can never be an
2752 -- Unchecked_Union (this code is executed for records
2753 -- that do not have variants), we may reference its
2754 -- discriminant(s).
2755
2756 if Nkind (Lhs) = N_Selected_Component
2757 and then Has_Per_Object_Constraint
2758 (Entity (Selector_Name (Lhs)))
2759 then
2760 Lhs_Discr_Val :=
2761 Make_Selected_Component (Loc,
2762 Prefix => Prefix (Lhs),
2763 Selector_Name =>
2764 New_Copy
2765 (Get_Discriminant_Value
2766 (First_Discriminant (Lhs_Type),
2767 Lhs_Type,
2768 Stored_Constraint (Lhs_Type))));
2769
2770 else
2771 Lhs_Discr_Val :=
2772 New_Copy
2773 (Get_Discriminant_Value
2774 (First_Discriminant (Lhs_Type),
2775 Lhs_Type,
2776 Stored_Constraint (Lhs_Type)));
2777
2778 end if;
2779 else
2780 -- It is not possible to infer the discriminant since
2781 -- the subtype is not constrained.
2782
2783 return
2784 Make_Raise_Program_Error (Loc,
2785 Reason => PE_Unchecked_Union_Restriction);
2786 end if;
2787
2788 -- Rhs of the composite equality
2789
2790 if Is_Constrained (Rhs_Type) then
2791 if Nkind (Rhs) = N_Selected_Component
2792 and then Has_Per_Object_Constraint
2793 (Entity (Selector_Name (Rhs)))
2794 then
2795 Rhs_Discr_Val :=
2796 Make_Selected_Component (Loc,
2797 Prefix => Prefix (Rhs),
2798 Selector_Name =>
2799 New_Copy
2800 (Get_Discriminant_Value
2801 (First_Discriminant (Rhs_Type),
2802 Rhs_Type,
2803 Stored_Constraint (Rhs_Type))));
2804
2805 else
2806 Rhs_Discr_Val :=
2807 New_Copy
2808 (Get_Discriminant_Value
2809 (First_Discriminant (Rhs_Type),
2810 Rhs_Type,
2811 Stored_Constraint (Rhs_Type)));
2812
2813 end if;
2814 else
2815 return
2816 Make_Raise_Program_Error (Loc,
2817 Reason => PE_Unchecked_Union_Restriction);
2818 end if;
2819
2820 -- Call the TSS equality function with the inferred
2821 -- discriminant values.
2822
2823 return
2824 Make_Function_Call (Loc,
2825 Name => New_Occurrence_Of (Eq_Op, Loc),
2826 Parameter_Associations => New_List (
2827 Lhs,
2828 Rhs,
2829 Lhs_Discr_Val,
2830 Rhs_Discr_Val));
2831 end;
2832
2833 else
2834 return
2835 Make_Function_Call (Loc,
2836 Name => New_Occurrence_Of (Eq_Op, Loc),
2837 Parameter_Associations => New_List (Lhs, Rhs));
2838 end if;
2839 end if;
2840
2841 -- Equality composes in Ada 2012 for untagged record types. It also
2842 -- composes for bounded strings, because they are part of the
2843 -- predefined environment. We could make it compose for bounded
2844 -- strings by making them tagged, or by making sure all subcomponents
2845 -- are set to the same value, even when not used. Instead, we have
2846 -- this special case in the compiler, because it's more efficient.
2847
2848 elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then
2849
2850 -- If no TSS has been created for the type, check whether there is
2851 -- a primitive equality declared for it.
2852
2853 declare
2854 Op : constant Node_Id := Find_Primitive_Eq;
2855
2856 begin
2857 -- Use user-defined primitive if it exists, otherwise use
2858 -- predefined equality.
2859
2860 if Present (Op) then
2861 return Op;
2862 else
2863 return Make_Op_Eq (Loc, Lhs, Rhs);
2864 end if;
2865 end;
2866
2867 else
2868 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2869 end if;
2870
2871 -- Non-composite types (always use predefined equality)
2872
2873 else
2874 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2875 end if;
2876 end Expand_Composite_Equality;
2877
2878 ------------------------
2879 -- Expand_Concatenate --
2880 ------------------------
2881
2882 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2883 Loc : constant Source_Ptr := Sloc (Cnode);
2884
2885 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2886 -- Result type of concatenation
2887
2888 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2889 -- Component type. Elements of this component type can appear as one
2890 -- of the operands of concatenation as well as arrays.
2891
2892 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2893 -- Index subtype
2894
2895 Ityp : constant Entity_Id := Base_Type (Istyp);
2896 -- Index type. This is the base type of the index subtype, and is used
2897 -- for all computed bounds (which may be out of range of Istyp in the
2898 -- case of null ranges).
2899
2900 Artyp : Entity_Id;
2901 -- This is the type we use to do arithmetic to compute the bounds and
2902 -- lengths of operands. The choice of this type is a little subtle and
2903 -- is discussed in a separate section at the start of the body code.
2904
2905 Concatenation_Error : exception;
2906 -- Raised if concatenation is sure to raise a CE
2907
2908 Result_May_Be_Null : Boolean := True;
2909 -- Reset to False if at least one operand is encountered which is known
2910 -- at compile time to be non-null. Used for handling the special case
2911 -- of setting the high bound to the last operand high bound for a null
2912 -- result, thus ensuring a proper high bound in the super-flat case.
2913
2914 N : constant Nat := List_Length (Opnds);
2915 -- Number of concatenation operands including possibly null operands
2916
2917 NN : Nat := 0;
2918 -- Number of operands excluding any known to be null, except that the
2919 -- last operand is always retained, in case it provides the bounds for
2920 -- a null result.
2921
2922 Opnd : Node_Id;
2923 -- Current operand being processed in the loop through operands. After
2924 -- this loop is complete, always contains the last operand (which is not
2925 -- the same as Operands (NN), since null operands are skipped).
2926
2927 -- Arrays describing the operands, only the first NN entries of each
2928 -- array are set (NN < N when we exclude known null operands).
2929
2930 Is_Fixed_Length : array (1 .. N) of Boolean;
2931 -- True if length of corresponding operand known at compile time
2932
2933 Operands : array (1 .. N) of Node_Id;
2934 -- Set to the corresponding entry in the Opnds list (but note that null
2935 -- operands are excluded, so not all entries in the list are stored).
2936
2937 Fixed_Length : array (1 .. N) of Uint;
2938 -- Set to length of operand. Entries in this array are set only if the
2939 -- corresponding entry in Is_Fixed_Length is True.
2940
2941 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2942 -- Set to lower bound of operand. Either an integer literal in the case
2943 -- where the bound is known at compile time, else actual lower bound.
2944 -- The operand low bound is of type Ityp.
2945
2946 Var_Length : array (1 .. N) of Entity_Id;
2947 -- Set to an entity of type Natural that contains the length of an
2948 -- operand whose length is not known at compile time. Entries in this
2949 -- array are set only if the corresponding entry in Is_Fixed_Length
2950 -- is False. The entity is of type Artyp.
2951
2952 Aggr_Length : array (0 .. N) of Node_Id;
2953 -- The J'th entry in an expression node that represents the total length
2954 -- of operands 1 through J. It is either an integer literal node, or a
2955 -- reference to a constant entity with the right value, so it is fine
2956 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2957 -- entry always is set to zero. The length is of type Artyp.
2958
2959 Low_Bound : Node_Id;
2960 -- A tree node representing the low bound of the result (of type Ityp).
2961 -- This is either an integer literal node, or an identifier reference to
2962 -- a constant entity initialized to the appropriate value.
2963
2964 Last_Opnd_Low_Bound : Node_Id;
2965 -- A tree node representing the low bound of the last operand. This
2966 -- need only be set if the result could be null. It is used for the
2967 -- special case of setting the right low bound for a null result.
2968 -- This is of type Ityp.
2969
2970 Last_Opnd_High_Bound : Node_Id;
2971 -- A tree node representing the high bound of the last operand. This
2972 -- need only be set if the result could be null. It is used for the
2973 -- special case of setting the right high bound for a null result.
2974 -- This is of type Ityp.
2975
2976 High_Bound : Node_Id;
2977 -- A tree node representing the high bound of the result (of type Ityp)
2978
2979 Result : Node_Id;
2980 -- Result of the concatenation (of type Ityp)
2981
2982 Actions : constant List_Id := New_List;
2983 -- Collect actions to be inserted
2984
2985 Known_Non_Null_Operand_Seen : Boolean;
2986 -- Set True during generation of the assignments of operands into
2987 -- result once an operand known to be non-null has been seen.
2988
2989 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2990 -- This function makes an N_Integer_Literal node that is returned in
2991 -- analyzed form with the type set to Artyp. Importantly this literal
2992 -- is not flagged as static, so that if we do computations with it that
2993 -- result in statically detected out of range conditions, we will not
2994 -- generate error messages but instead warning messages.
2995
2996 function To_Artyp (X : Node_Id) return Node_Id;
2997 -- Given a node of type Ityp, returns the corresponding value of type
2998 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2999 -- For enum types, the Pos of the value is returned.
3000
3001 function To_Ityp (X : Node_Id) return Node_Id;
3002 -- The inverse function (uses Val in the case of enumeration types)
3003
3004 ------------------------
3005 -- Make_Artyp_Literal --
3006 ------------------------
3007
3008 function Make_Artyp_Literal (Val : Nat) return Node_Id is
3009 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
3010 begin
3011 Set_Etype (Result, Artyp);
3012 Set_Analyzed (Result, True);
3013 Set_Is_Static_Expression (Result, False);
3014 return Result;
3015 end Make_Artyp_Literal;
3016
3017 --------------
3018 -- To_Artyp --
3019 --------------
3020
3021 function To_Artyp (X : Node_Id) return Node_Id is
3022 begin
3023 if Ityp = Base_Type (Artyp) then
3024 return X;
3025
3026 elsif Is_Enumeration_Type (Ityp) then
3027 return
3028 Make_Attribute_Reference (Loc,
3029 Prefix => New_Occurrence_Of (Ityp, Loc),
3030 Attribute_Name => Name_Pos,
3031 Expressions => New_List (X));
3032
3033 else
3034 return Convert_To (Artyp, X);
3035 end if;
3036 end To_Artyp;
3037
3038 -------------
3039 -- To_Ityp --
3040 -------------
3041
3042 function To_Ityp (X : Node_Id) return Node_Id is
3043 begin
3044 if Is_Enumeration_Type (Ityp) then
3045 return
3046 Make_Attribute_Reference (Loc,
3047 Prefix => New_Occurrence_Of (Ityp, Loc),
3048 Attribute_Name => Name_Val,
3049 Expressions => New_List (X));
3050
3051 -- Case where we will do a type conversion
3052
3053 else
3054 if Ityp = Base_Type (Artyp) then
3055 return X;
3056 else
3057 return Convert_To (Ityp, X);
3058 end if;
3059 end if;
3060 end To_Ityp;
3061
3062 -- Local Declarations
3063
3064 Lib_Level_Target : constant Boolean :=
3065 Nkind (Parent (Cnode)) = N_Object_Declaration
3066 and then
3067 Is_Library_Level_Entity (Defining_Identifier (Parent (Cnode)));
3068
3069 -- If the concatenation declares a library level entity, we call the
3070 -- built-in concatenation routines to prevent code bloat, regardless
3071 -- of optimization level. This is space-efficient, and prevent linking
3072 -- problems when units are compiled with different optimizations.
3073
3074 Opnd_Typ : Entity_Id;
3075 Ent : Entity_Id;
3076 Len : Uint;
3077 J : Nat;
3078 Clen : Node_Id;
3079 Set : Boolean;
3080
3081 -- Start of processing for Expand_Concatenate
3082
3083 begin
3084 -- Choose an appropriate computational type
3085
3086 -- We will be doing calculations of lengths and bounds in this routine
3087 -- and computing one from the other in some cases, e.g. getting the high
3088 -- bound by adding the length-1 to the low bound.
3089
3090 -- We can't just use the index type, or even its base type for this
3091 -- purpose for two reasons. First it might be an enumeration type which
3092 -- is not suitable for computations of any kind, and second it may
3093 -- simply not have enough range. For example if the index type is
3094 -- -128..+127 then lengths can be up to 256, which is out of range of
3095 -- the type.
3096
3097 -- For enumeration types, we can simply use Standard_Integer, this is
3098 -- sufficient since the actual number of enumeration literals cannot
3099 -- possibly exceed the range of integer (remember we will be doing the
3100 -- arithmetic with POS values, not representation values).
3101
3102 if Is_Enumeration_Type (Ityp) then
3103 Artyp := Standard_Integer;
3104
3105 -- If index type is Positive, we use the standard unsigned type, to give
3106 -- more room on the top of the range, obviating the need for an overflow
3107 -- check when creating the upper bound. This is needed to avoid junk
3108 -- overflow checks in the common case of String types.
3109
3110 -- ??? Disabled for now
3111
3112 -- elsif Istyp = Standard_Positive then
3113 -- Artyp := Standard_Unsigned;
3114
3115 -- For modular types, we use a 32-bit modular type for types whose size
3116 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
3117 -- identity type, and for larger unsigned types we use 64-bits.
3118
3119 elsif Is_Modular_Integer_Type (Ityp) then
3120 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
3121 Artyp := Standard_Unsigned;
3122 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
3123 Artyp := Ityp;
3124 else
3125 Artyp := RTE (RE_Long_Long_Unsigned);
3126 end if;
3127
3128 -- Similar treatment for signed types
3129
3130 else
3131 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
3132 Artyp := Standard_Integer;
3133 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
3134 Artyp := Ityp;
3135 else
3136 Artyp := Standard_Long_Long_Integer;
3137 end if;
3138 end if;
3139
3140 -- Supply dummy entry at start of length array
3141
3142 Aggr_Length (0) := Make_Artyp_Literal (0);
3143
3144 -- Go through operands setting up the above arrays
3145
3146 J := 1;
3147 while J <= N loop
3148 Opnd := Remove_Head (Opnds);
3149 Opnd_Typ := Etype (Opnd);
3150
3151 -- The parent got messed up when we put the operands in a list,
3152 -- so now put back the proper parent for the saved operand, that
3153 -- is to say the concatenation node, to make sure that each operand
3154 -- is seen as a subexpression, e.g. if actions must be inserted.
3155
3156 Set_Parent (Opnd, Cnode);
3157
3158 -- Set will be True when we have setup one entry in the array
3159
3160 Set := False;
3161
3162 -- Singleton element (or character literal) case
3163
3164 if Base_Type (Opnd_Typ) = Ctyp then
3165 NN := NN + 1;
3166 Operands (NN) := Opnd;
3167 Is_Fixed_Length (NN) := True;
3168 Fixed_Length (NN) := Uint_1;
3169 Result_May_Be_Null := False;
3170
3171 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
3172 -- since we know that the result cannot be null).
3173
3174 Opnd_Low_Bound (NN) :=
3175 Make_Attribute_Reference (Loc,
3176 Prefix => New_Occurrence_Of (Istyp, Loc),
3177 Attribute_Name => Name_First);
3178
3179 Set := True;
3180
3181 -- String literal case (can only occur for strings of course)
3182
3183 elsif Nkind (Opnd) = N_String_Literal then
3184 Len := String_Literal_Length (Opnd_Typ);
3185
3186 if Len /= 0 then
3187 Result_May_Be_Null := False;
3188 end if;
3189
3190 -- Capture last operand low and high bound if result could be null
3191
3192 if J = N and then Result_May_Be_Null then
3193 Last_Opnd_Low_Bound :=
3194 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3195
3196 Last_Opnd_High_Bound :=
3197 Make_Op_Subtract (Loc,
3198 Left_Opnd =>
3199 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
3200 Right_Opnd => Make_Integer_Literal (Loc, 1));
3201 end if;
3202
3203 -- Skip null string literal
3204
3205 if J < N and then Len = 0 then
3206 goto Continue;
3207 end if;
3208
3209 NN := NN + 1;
3210 Operands (NN) := Opnd;
3211 Is_Fixed_Length (NN) := True;
3212
3213 -- Set length and bounds
3214
3215 Fixed_Length (NN) := Len;
3216
3217 Opnd_Low_Bound (NN) :=
3218 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3219
3220 Set := True;
3221
3222 -- All other cases
3223
3224 else
3225 -- Check constrained case with known bounds
3226
3227 if Is_Constrained (Opnd_Typ) then
3228 declare
3229 Index : constant Node_Id := First_Index (Opnd_Typ);
3230 Indx_Typ : constant Entity_Id := Etype (Index);
3231 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
3232 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
3233
3234 begin
3235 -- Fixed length constrained array type with known at compile
3236 -- time bounds is last case of fixed length operand.
3237
3238 if Compile_Time_Known_Value (Lo)
3239 and then
3240 Compile_Time_Known_Value (Hi)
3241 then
3242 declare
3243 Loval : constant Uint := Expr_Value (Lo);
3244 Hival : constant Uint := Expr_Value (Hi);
3245 Len : constant Uint :=
3246 UI_Max (Hival - Loval + 1, Uint_0);
3247
3248 begin
3249 if Len > 0 then
3250 Result_May_Be_Null := False;
3251 end if;
3252
3253 -- Capture last operand bounds if result could be null
3254
3255 if J = N and then Result_May_Be_Null then
3256 Last_Opnd_Low_Bound :=
3257 Convert_To (Ityp,
3258 Make_Integer_Literal (Loc, Expr_Value (Lo)));
3259
3260 Last_Opnd_High_Bound :=
3261 Convert_To (Ityp,
3262 Make_Integer_Literal (Loc, Expr_Value (Hi)));
3263 end if;
3264
3265 -- Exclude null length case unless last operand
3266
3267 if J < N and then Len = 0 then
3268 goto Continue;
3269 end if;
3270
3271 NN := NN + 1;
3272 Operands (NN) := Opnd;
3273 Is_Fixed_Length (NN) := True;
3274 Fixed_Length (NN) := Len;
3275
3276 Opnd_Low_Bound (NN) :=
3277 To_Ityp
3278 (Make_Integer_Literal (Loc, Expr_Value (Lo)));
3279 Set := True;
3280 end;
3281 end if;
3282 end;
3283 end if;
3284
3285 -- All cases where the length is not known at compile time, or the
3286 -- special case of an operand which is known to be null but has a
3287 -- lower bound other than 1 or is other than a string type.
3288
3289 if not Set then
3290 NN := NN + 1;
3291
3292 -- Capture operand bounds
3293
3294 Opnd_Low_Bound (NN) :=
3295 Make_Attribute_Reference (Loc,
3296 Prefix =>
3297 Duplicate_Subexpr (Opnd, Name_Req => True),
3298 Attribute_Name => Name_First);
3299
3300 -- Capture last operand bounds if result could be null
3301
3302 if J = N and Result_May_Be_Null then
3303 Last_Opnd_Low_Bound :=
3304 Convert_To (Ityp,
3305 Make_Attribute_Reference (Loc,
3306 Prefix =>
3307 Duplicate_Subexpr (Opnd, Name_Req => True),
3308 Attribute_Name => Name_First));
3309
3310 Last_Opnd_High_Bound :=
3311 Convert_To (Ityp,
3312 Make_Attribute_Reference (Loc,
3313 Prefix =>
3314 Duplicate_Subexpr (Opnd, Name_Req => True),
3315 Attribute_Name => Name_Last));
3316 end if;
3317
3318 -- Capture length of operand in entity
3319
3320 Operands (NN) := Opnd;
3321 Is_Fixed_Length (NN) := False;
3322
3323 Var_Length (NN) := Make_Temporary (Loc, 'L');
3324
3325 Append_To (Actions,
3326 Make_Object_Declaration (Loc,
3327 Defining_Identifier => Var_Length (NN),
3328 Constant_Present => True,
3329 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3330 Expression =>
3331 Make_Attribute_Reference (Loc,
3332 Prefix =>
3333 Duplicate_Subexpr (Opnd, Name_Req => True),
3334 Attribute_Name => Name_Length)));
3335 end if;
3336 end if;
3337
3338 -- Set next entry in aggregate length array
3339
3340 -- For first entry, make either integer literal for fixed length
3341 -- or a reference to the saved length for variable length.
3342
3343 if NN = 1 then
3344 if Is_Fixed_Length (1) then
3345 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
3346 else
3347 Aggr_Length (1) := New_Occurrence_Of (Var_Length (1), Loc);
3348 end if;
3349
3350 -- If entry is fixed length and only fixed lengths so far, make
3351 -- appropriate new integer literal adding new length.
3352
3353 elsif Is_Fixed_Length (NN)
3354 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
3355 then
3356 Aggr_Length (NN) :=
3357 Make_Integer_Literal (Loc,
3358 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
3359
3360 -- All other cases, construct an addition node for the length and
3361 -- create an entity initialized to this length.
3362
3363 else
3364 Ent := Make_Temporary (Loc, 'L');
3365
3366 if Is_Fixed_Length (NN) then
3367 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
3368 else
3369 Clen := New_Occurrence_Of (Var_Length (NN), Loc);
3370 end if;
3371
3372 Append_To (Actions,
3373 Make_Object_Declaration (Loc,
3374 Defining_Identifier => Ent,
3375 Constant_Present => True,
3376 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3377 Expression =>
3378 Make_Op_Add (Loc,
3379 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
3380 Right_Opnd => Clen)));
3381
3382 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
3383 end if;
3384
3385 <<Continue>>
3386 J := J + 1;
3387 end loop;
3388
3389 -- If we have only skipped null operands, return the last operand
3390
3391 if NN = 0 then
3392 Result := Opnd;
3393 goto Done;
3394 end if;
3395
3396 -- If we have only one non-null operand, return it and we are done.
3397 -- There is one case in which this cannot be done, and that is when
3398 -- the sole operand is of the element type, in which case it must be
3399 -- converted to an array, and the easiest way of doing that is to go
3400 -- through the normal general circuit.
3401
3402 if NN = 1 and then Base_Type (Etype (Operands (1))) /= Ctyp then
3403 Result := Operands (1);
3404 goto Done;
3405 end if;
3406
3407 -- Cases where we have a real concatenation
3408
3409 -- Next step is to find the low bound for the result array that we
3410 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3411
3412 -- If the ultimate ancestor of the index subtype is a constrained array
3413 -- definition, then the lower bound is that of the index subtype as
3414 -- specified by (RM 4.5.3(6)).
3415
3416 -- The right test here is to go to the root type, and then the ultimate
3417 -- ancestor is the first subtype of this root type.
3418
3419 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
3420 Low_Bound :=
3421 Make_Attribute_Reference (Loc,
3422 Prefix =>
3423 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
3424 Attribute_Name => Name_First);
3425
3426 -- If the first operand in the list has known length we know that
3427 -- the lower bound of the result is the lower bound of this operand.
3428
3429 elsif Is_Fixed_Length (1) then
3430 Low_Bound := Opnd_Low_Bound (1);
3431
3432 -- OK, we don't know the lower bound, we have to build a horrible
3433 -- if expression node of the form
3434
3435 -- if Cond1'Length /= 0 then
3436 -- Opnd1 low bound
3437 -- else
3438 -- if Opnd2'Length /= 0 then
3439 -- Opnd2 low bound
3440 -- else
3441 -- ...
3442
3443 -- The nesting ends either when we hit an operand whose length is known
3444 -- at compile time, or on reaching the last operand, whose low bound we
3445 -- take unconditionally whether or not it is null. It's easiest to do
3446 -- this with a recursive procedure:
3447
3448 else
3449 declare
3450 function Get_Known_Bound (J : Nat) return Node_Id;
3451 -- Returns the lower bound determined by operands J .. NN
3452
3453 ---------------------
3454 -- Get_Known_Bound --
3455 ---------------------
3456
3457 function Get_Known_Bound (J : Nat) return Node_Id is
3458 begin
3459 if Is_Fixed_Length (J) or else J = NN then
3460 return New_Copy (Opnd_Low_Bound (J));
3461
3462 else
3463 return
3464 Make_If_Expression (Loc,
3465 Expressions => New_List (
3466
3467 Make_Op_Ne (Loc,
3468 Left_Opnd =>
3469 New_Occurrence_Of (Var_Length (J), Loc),
3470 Right_Opnd =>
3471 Make_Integer_Literal (Loc, 0)),
3472
3473 New_Copy (Opnd_Low_Bound (J)),
3474 Get_Known_Bound (J + 1)));
3475 end if;
3476 end Get_Known_Bound;
3477
3478 begin
3479 Ent := Make_Temporary (Loc, 'L');
3480
3481 Append_To (Actions,
3482 Make_Object_Declaration (Loc,
3483 Defining_Identifier => Ent,
3484 Constant_Present => True,
3485 Object_Definition => New_Occurrence_Of (Ityp, Loc),
3486 Expression => Get_Known_Bound (1)));
3487
3488 Low_Bound := New_Occurrence_Of (Ent, Loc);
3489 end;
3490 end if;
3491
3492 -- Now we can safely compute the upper bound, normally
3493 -- Low_Bound + Length - 1.
3494
3495 High_Bound :=
3496 To_Ityp (
3497 Make_Op_Add (Loc,
3498 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3499 Right_Opnd =>
3500 Make_Op_Subtract (Loc,
3501 Left_Opnd => New_Copy (Aggr_Length (NN)),
3502 Right_Opnd => Make_Artyp_Literal (1))));
3503
3504 -- Note that calculation of the high bound may cause overflow in some
3505 -- very weird cases, so in the general case we need an overflow check on
3506 -- the high bound. We can avoid this for the common case of string types
3507 -- and other types whose index is Positive, since we chose a wider range
3508 -- for the arithmetic type.
3509
3510 if Istyp /= Standard_Positive then
3511 Activate_Overflow_Check (High_Bound);
3512 end if;
3513
3514 -- Handle the exceptional case where the result is null, in which case
3515 -- case the bounds come from the last operand (so that we get the proper
3516 -- bounds if the last operand is super-flat).
3517
3518 if Result_May_Be_Null then
3519 Low_Bound :=
3520 Make_If_Expression (Loc,
3521 Expressions => New_List (
3522 Make_Op_Eq (Loc,
3523 Left_Opnd => New_Copy (Aggr_Length (NN)),
3524 Right_Opnd => Make_Artyp_Literal (0)),
3525 Last_Opnd_Low_Bound,
3526 Low_Bound));
3527
3528 High_Bound :=
3529 Make_If_Expression (Loc,
3530 Expressions => New_List (
3531 Make_Op_Eq (Loc,
3532 Left_Opnd => New_Copy (Aggr_Length (NN)),
3533 Right_Opnd => Make_Artyp_Literal (0)),
3534 Last_Opnd_High_Bound,
3535 High_Bound));
3536 end if;
3537
3538 -- Here is where we insert the saved up actions
3539
3540 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3541
3542 -- Now we construct an array object with appropriate bounds. We mark
3543 -- the target as internal to prevent useless initialization when
3544 -- Initialize_Scalars is enabled. Also since this is the actual result
3545 -- entity, we make sure we have debug information for the result.
3546
3547 Ent := Make_Temporary (Loc, 'S');
3548 Set_Is_Internal (Ent);
3549 Set_Needs_Debug_Info (Ent);
3550
3551 -- If the bound is statically known to be out of range, we do not want
3552 -- to abort, we want a warning and a runtime constraint error. Note that
3553 -- we have arranged that the result will not be treated as a static
3554 -- constant, so we won't get an illegality during this insertion.
3555
3556 Insert_Action (Cnode,
3557 Make_Object_Declaration (Loc,
3558 Defining_Identifier => Ent,
3559 Object_Definition =>
3560 Make_Subtype_Indication (Loc,
3561 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
3562 Constraint =>
3563 Make_Index_Or_Discriminant_Constraint (Loc,
3564 Constraints => New_List (
3565 Make_Range (Loc,
3566 Low_Bound => Low_Bound,
3567 High_Bound => High_Bound))))),
3568 Suppress => All_Checks);
3569
3570 -- If the result of the concatenation appears as the initializing
3571 -- expression of an object declaration, we can just rename the
3572 -- result, rather than copying it.
3573
3574 Set_OK_To_Rename (Ent);
3575
3576 -- Catch the static out of range case now
3577
3578 if Raises_Constraint_Error (High_Bound) then
3579 raise Concatenation_Error;
3580 end if;
3581
3582 -- Now we will generate the assignments to do the actual concatenation
3583
3584 -- There is one case in which we will not do this, namely when all the
3585 -- following conditions are met:
3586
3587 -- The result type is Standard.String
3588
3589 -- There are nine or fewer retained (non-null) operands
3590
3591 -- The optimization level is -O0
3592
3593 -- The corresponding System.Concat_n.Str_Concat_n routine is
3594 -- available in the run time.
3595
3596 -- The debug flag gnatd.c is not set
3597
3598 -- If all these conditions are met then we generate a call to the
3599 -- relevant concatenation routine. The purpose of this is to avoid
3600 -- undesirable code bloat at -O0.
3601
3602 if Atyp = Standard_String
3603 and then NN in 2 .. 9
3604 and then (Lib_Level_Target
3605 or else
3606 ((Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
3607 and then not Debug_Flag_Dot_C))
3608 then
3609 declare
3610 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3611 (RE_Str_Concat_2,
3612 RE_Str_Concat_3,
3613 RE_Str_Concat_4,
3614 RE_Str_Concat_5,
3615 RE_Str_Concat_6,
3616 RE_Str_Concat_7,
3617 RE_Str_Concat_8,
3618 RE_Str_Concat_9);
3619
3620 begin
3621 if RTE_Available (RR (NN)) then
3622 declare
3623 Opnds : constant List_Id :=
3624 New_List (New_Occurrence_Of (Ent, Loc));
3625
3626 begin
3627 for J in 1 .. NN loop
3628 if Is_List_Member (Operands (J)) then
3629 Remove (Operands (J));
3630 end if;
3631
3632 if Base_Type (Etype (Operands (J))) = Ctyp then
3633 Append_To (Opnds,
3634 Make_Aggregate (Loc,
3635 Component_Associations => New_List (
3636 Make_Component_Association (Loc,
3637 Choices => New_List (
3638 Make_Integer_Literal (Loc, 1)),
3639 Expression => Operands (J)))));
3640
3641 else
3642 Append_To (Opnds, Operands (J));
3643 end if;
3644 end loop;
3645
3646 Insert_Action (Cnode,
3647 Make_Procedure_Call_Statement (Loc,
3648 Name => New_Occurrence_Of (RTE (RR (NN)), Loc),
3649 Parameter_Associations => Opnds));
3650
3651 Result := New_Occurrence_Of (Ent, Loc);
3652 goto Done;
3653 end;
3654 end if;
3655 end;
3656 end if;
3657
3658 -- Not special case so generate the assignments
3659
3660 Known_Non_Null_Operand_Seen := False;
3661
3662 for J in 1 .. NN loop
3663 declare
3664 Lo : constant Node_Id :=
3665 Make_Op_Add (Loc,
3666 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3667 Right_Opnd => Aggr_Length (J - 1));
3668
3669 Hi : constant Node_Id :=
3670 Make_Op_Add (Loc,
3671 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3672 Right_Opnd =>
3673 Make_Op_Subtract (Loc,
3674 Left_Opnd => Aggr_Length (J),
3675 Right_Opnd => Make_Artyp_Literal (1)));
3676
3677 begin
3678 -- Singleton case, simple assignment
3679
3680 if Base_Type (Etype (Operands (J))) = Ctyp then
3681 Known_Non_Null_Operand_Seen := True;
3682 Insert_Action (Cnode,
3683 Make_Assignment_Statement (Loc,
3684 Name =>
3685 Make_Indexed_Component (Loc,
3686 Prefix => New_Occurrence_Of (Ent, Loc),
3687 Expressions => New_List (To_Ityp (Lo))),
3688 Expression => Operands (J)),
3689 Suppress => All_Checks);
3690
3691 -- Array case, slice assignment, skipped when argument is fixed
3692 -- length and known to be null.
3693
3694 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3695 declare
3696 Assign : Node_Id :=
3697 Make_Assignment_Statement (Loc,
3698 Name =>
3699 Make_Slice (Loc,
3700 Prefix =>
3701 New_Occurrence_Of (Ent, Loc),
3702 Discrete_Range =>
3703 Make_Range (Loc,
3704 Low_Bound => To_Ityp (Lo),
3705 High_Bound => To_Ityp (Hi))),
3706 Expression => Operands (J));
3707 begin
3708 if Is_Fixed_Length (J) then
3709 Known_Non_Null_Operand_Seen := True;
3710
3711 elsif not Known_Non_Null_Operand_Seen then
3712
3713 -- Here if operand length is not statically known and no
3714 -- operand known to be non-null has been processed yet.
3715 -- If operand length is 0, we do not need to perform the
3716 -- assignment, and we must avoid the evaluation of the
3717 -- high bound of the slice, since it may underflow if the
3718 -- low bound is Ityp'First.
3719
3720 Assign :=
3721 Make_Implicit_If_Statement (Cnode,
3722 Condition =>
3723 Make_Op_Ne (Loc,
3724 Left_Opnd =>
3725 New_Occurrence_Of (Var_Length (J), Loc),
3726 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3727 Then_Statements => New_List (Assign));
3728 end if;
3729
3730 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3731 end;
3732 end if;
3733 end;
3734 end loop;
3735
3736 -- Finally we build the result, which is a reference to the array object
3737
3738 Result := New_Occurrence_Of (Ent, Loc);
3739
3740 <<Done>>
3741 Rewrite (Cnode, Result);
3742 Analyze_And_Resolve (Cnode, Atyp);
3743
3744 exception
3745 when Concatenation_Error =>
3746
3747 -- Kill warning generated for the declaration of the static out of
3748 -- range high bound, and instead generate a Constraint_Error with
3749 -- an appropriate specific message.
3750
3751 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3752 Apply_Compile_Time_Constraint_Error
3753 (N => Cnode,
3754 Msg => "concatenation result upper bound out of range??",
3755 Reason => CE_Range_Check_Failed);
3756 end Expand_Concatenate;
3757
3758 ---------------------------------------------------
3759 -- Expand_Membership_Minimize_Eliminate_Overflow --
3760 ---------------------------------------------------
3761
3762 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is
3763 pragma Assert (Nkind (N) = N_In);
3764 -- Despite the name, this routine applies only to N_In, not to
3765 -- N_Not_In. The latter is always rewritten as not (X in Y).
3766
3767 Result_Type : constant Entity_Id := Etype (N);
3768 -- Capture result type, may be a derived boolean type
3769
3770 Loc : constant Source_Ptr := Sloc (N);
3771 Lop : constant Node_Id := Left_Opnd (N);
3772 Rop : constant Node_Id := Right_Opnd (N);
3773
3774 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3775 -- is thus tempting to capture these values, but due to the rewrites
3776 -- that occur as a result of overflow checking, these values change
3777 -- as we go along, and it is safe just to always use Etype explicitly.
3778
3779 Restype : constant Entity_Id := Etype (N);
3780 -- Save result type
3781
3782 Lo, Hi : Uint;
3783 -- Bounds in Minimize calls, not used currently
3784
3785 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
3786 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3787
3788 begin
3789 Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False);
3790
3791 -- If right operand is a subtype name, and the subtype name has no
3792 -- predicate, then we can just replace the right operand with an
3793 -- explicit range T'First .. T'Last, and use the explicit range code.
3794
3795 if Nkind (Rop) /= N_Range
3796 and then No (Predicate_Function (Etype (Rop)))
3797 then
3798 declare
3799 Rtyp : constant Entity_Id := Etype (Rop);
3800 begin
3801 Rewrite (Rop,
3802 Make_Range (Loc,
3803 Low_Bound =>
3804 Make_Attribute_Reference (Loc,
3805 Attribute_Name => Name_First,
3806 Prefix => New_Occurrence_Of (Rtyp, Loc)),
3807 High_Bound =>
3808 Make_Attribute_Reference (Loc,
3809 Attribute_Name => Name_Last,
3810 Prefix => New_Occurrence_Of (Rtyp, Loc))));
3811 Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks);
3812 end;
3813 end if;
3814
3815 -- Here for the explicit range case. Note that the bounds of the range
3816 -- have not been processed for minimized or eliminated checks.
3817
3818 if Nkind (Rop) = N_Range then
3819 Minimize_Eliminate_Overflows
3820 (Low_Bound (Rop), Lo, Hi, Top_Level => False);
3821 Minimize_Eliminate_Overflows
3822 (High_Bound (Rop), Lo, Hi, Top_Level => False);
3823
3824 -- We have A in B .. C, treated as A >= B and then A <= C
3825
3826 -- Bignum case
3827
3828 if Is_RTE (Etype (Lop), RE_Bignum)
3829 or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum)
3830 or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum)
3831 then
3832 declare
3833 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3834 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3835 L : constant Entity_Id :=
3836 Make_Defining_Identifier (Loc, Name_uL);
3837 Lopnd : constant Node_Id := Convert_To_Bignum (Lop);
3838 Lbound : constant Node_Id :=
3839 Convert_To_Bignum (Low_Bound (Rop));
3840 Hbound : constant Node_Id :=
3841 Convert_To_Bignum (High_Bound (Rop));
3842
3843 -- Now we rewrite the membership test node to look like
3844
3845 -- do
3846 -- Bnn : Result_Type;
3847 -- declare
3848 -- M : Mark_Id := SS_Mark;
3849 -- L : Bignum := Lopnd;
3850 -- begin
3851 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3852 -- SS_Release (M);
3853 -- end;
3854 -- in
3855 -- Bnn
3856 -- end
3857
3858 begin
3859 -- Insert declaration of L into declarations of bignum block
3860
3861 Insert_After
3862 (Last (Declarations (Blk)),
3863 Make_Object_Declaration (Loc,
3864 Defining_Identifier => L,
3865 Object_Definition =>
3866 New_Occurrence_Of (RTE (RE_Bignum), Loc),
3867 Expression => Lopnd));
3868
3869 -- Insert assignment to Bnn into expressions of bignum block
3870
3871 Insert_Before
3872 (First (Statements (Handled_Statement_Sequence (Blk))),
3873 Make_Assignment_Statement (Loc,
3874 Name => New_Occurrence_Of (Bnn, Loc),
3875 Expression =>
3876 Make_And_Then (Loc,
3877 Left_Opnd =>
3878 Make_Function_Call (Loc,
3879 Name =>
3880 New_Occurrence_Of (RTE (RE_Big_GE), Loc),
3881 Parameter_Associations => New_List (
3882 New_Occurrence_Of (L, Loc),
3883 Lbound)),
3884 Right_Opnd =>
3885 Make_Function_Call (Loc,
3886 Name =>
3887 New_Occurrence_Of (RTE (RE_Big_LE), Loc),
3888 Parameter_Associations => New_List (
3889 New_Occurrence_Of (L, Loc),
3890 Hbound)))));
3891
3892 -- Now rewrite the node
3893
3894 Rewrite (N,
3895 Make_Expression_With_Actions (Loc,
3896 Actions => New_List (
3897 Make_Object_Declaration (Loc,
3898 Defining_Identifier => Bnn,
3899 Object_Definition =>
3900 New_Occurrence_Of (Result_Type, Loc)),
3901 Blk),
3902 Expression => New_Occurrence_Of (Bnn, Loc)));
3903 Analyze_And_Resolve (N, Result_Type);
3904 return;
3905 end;
3906
3907 -- Here if no bignums around
3908
3909 else
3910 -- Case where types are all the same
3911
3912 if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop)))
3913 and then
3914 Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop)))
3915 then
3916 null;
3917
3918 -- If types are not all the same, it means that we have rewritten
3919 -- at least one of them to be of type Long_Long_Integer, and we
3920 -- will convert the other operands to Long_Long_Integer.
3921
3922 else
3923 Convert_To_And_Rewrite (LLIB, Lop);
3924 Set_Analyzed (Lop, False);
3925 Analyze_And_Resolve (Lop, LLIB);
3926
3927 -- For the right operand, avoid unnecessary recursion into
3928 -- this routine, we know that overflow is not possible.
3929
3930 Convert_To_And_Rewrite (LLIB, Low_Bound (Rop));
3931 Convert_To_And_Rewrite (LLIB, High_Bound (Rop));
3932 Set_Analyzed (Rop, False);
3933 Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check);
3934 end if;
3935
3936 -- Now the three operands are of the same signed integer type,
3937 -- so we can use the normal expansion routine for membership,
3938 -- setting the flag to prevent recursion into this procedure.
3939
3940 Set_No_Minimize_Eliminate (N);
3941 Expand_N_In (N);
3942 end if;
3943
3944 -- Right operand is a subtype name and the subtype has a predicate. We
3945 -- have to make sure the predicate is checked, and for that we need to
3946 -- use the standard N_In circuitry with appropriate types.
3947
3948 else
3949 pragma Assert (Present (Predicate_Function (Etype (Rop))));
3950
3951 -- If types are "right", just call Expand_N_In preventing recursion
3952
3953 if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then
3954 Set_No_Minimize_Eliminate (N);
3955 Expand_N_In (N);
3956
3957 -- Bignum case
3958
3959 elsif Is_RTE (Etype (Lop), RE_Bignum) then
3960
3961 -- For X in T, we want to rewrite our node as
3962
3963 -- do
3964 -- Bnn : Result_Type;
3965
3966 -- declare
3967 -- M : Mark_Id := SS_Mark;
3968 -- Lnn : Long_Long_Integer'Base
3969 -- Nnn : Bignum;
3970
3971 -- begin
3972 -- Nnn := X;
3973
3974 -- if not Bignum_In_LLI_Range (Nnn) then
3975 -- Bnn := False;
3976 -- else
3977 -- Lnn := From_Bignum (Nnn);
3978 -- Bnn :=
3979 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3980 -- and then T'Base (Lnn) in T;
3981 -- end if;
3982 --
3983 -- SS_Release (M);
3984 -- end
3985 -- in
3986 -- Bnn
3987 -- end
3988
3989 -- A bit gruesome, but there doesn't seem to be a simpler way
3990
3991 declare
3992 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3993 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3994 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N);
3995 Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N);
3996 T : constant Entity_Id := Etype (Rop);
3997 TB : constant Entity_Id := Base_Type (T);
3998 Nin : Node_Id;
3999
4000 begin
4001 -- Mark the last membership operation to prevent recursion
4002
4003 Nin :=
4004 Make_In (Loc,
4005 Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)),
4006 Right_Opnd => New_Occurrence_Of (T, Loc));
4007 Set_No_Minimize_Eliminate (Nin);
4008
4009 -- Now decorate the block
4010
4011 Insert_After
4012 (Last (Declarations (Blk)),
4013 Make_Object_Declaration (Loc,
4014 Defining_Identifier => Lnn,
4015 Object_Definition => New_Occurrence_Of (LLIB, Loc)));
4016
4017 Insert_After
4018 (Last (Declarations (Blk)),
4019 Make_Object_Declaration (Loc,
4020 Defining_Identifier => Nnn,
4021 Object_Definition =>
4022 New_Occurrence_Of (RTE (RE_Bignum), Loc)));
4023
4024 Insert_List_Before
4025 (First (Statements (Handled_Statement_Sequence (Blk))),
4026 New_List (
4027 Make_Assignment_Statement (Loc,
4028 Name => New_Occurrence_Of (Nnn, Loc),
4029 Expression => Relocate_Node (Lop)),
4030
4031 Make_Implicit_If_Statement (N,
4032 Condition =>
4033 Make_Op_Not (Loc,
4034 Right_Opnd =>
4035 Make_Function_Call (Loc,
4036 Name =>
4037 New_Occurrence_Of
4038 (RTE (RE_Bignum_In_LLI_Range), Loc),
4039 Parameter_Associations => New_List (
4040 New_Occurrence_Of (Nnn, Loc)))),
4041
4042 Then_Statements => New_List (
4043 Make_Assignment_Statement (Loc,
4044 Name => New_Occurrence_Of (Bnn, Loc),
4045 Expression =>
4046 New_Occurrence_Of (Standard_False, Loc))),
4047
4048 Else_Statements => New_List (
4049 Make_Assignment_Statement (Loc,
4050 Name => New_Occurrence_Of (Lnn, Loc),
4051 Expression =>
4052 Make_Function_Call (Loc,
4053 Name =>
4054 New_Occurrence_Of (RTE (RE_From_Bignum), Loc),
4055 Parameter_Associations => New_List (
4056 New_Occurrence_Of (Nnn, Loc)))),
4057
4058 Make_Assignment_Statement (Loc,
4059 Name => New_Occurrence_Of (Bnn, Loc),
4060 Expression =>
4061 Make_And_Then (Loc,
4062 Left_Opnd =>
4063 Make_In (Loc,
4064 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4065 Right_Opnd =>
4066 Make_Range (Loc,
4067 Low_Bound =>
4068 Convert_To (LLIB,
4069 Make_Attribute_Reference (Loc,
4070 Attribute_Name => Name_First,
4071 Prefix =>
4072 New_Occurrence_Of (TB, Loc))),
4073
4074 High_Bound =>
4075 Convert_To (LLIB,
4076 Make_Attribute_Reference (Loc,
4077 Attribute_Name => Name_Last,
4078 Prefix =>
4079 New_Occurrence_Of (TB, Loc))))),
4080
4081 Right_Opnd => Nin))))));
4082
4083 -- Now we can do the rewrite
4084
4085 Rewrite (N,
4086 Make_Expression_With_Actions (Loc,
4087 Actions => New_List (
4088 Make_Object_Declaration (Loc,
4089 Defining_Identifier => Bnn,
4090 Object_Definition =>
4091 New_Occurrence_Of (Result_Type, Loc)),
4092 Blk),
4093 Expression => New_Occurrence_Of (Bnn, Loc)));
4094 Analyze_And_Resolve (N, Result_Type);
4095 return;
4096 end;
4097
4098 -- Not bignum case, but types don't match (this means we rewrote the
4099 -- left operand to be Long_Long_Integer).
4100
4101 else
4102 pragma Assert (Base_Type (Etype (Lop)) = LLIB);
4103
4104 -- We rewrite the membership test as (where T is the type with
4105 -- the predicate, i.e. the type of the right operand)
4106
4107 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
4108 -- and then T'Base (Lop) in T
4109
4110 declare
4111 T : constant Entity_Id := Etype (Rop);
4112 TB : constant Entity_Id := Base_Type (T);
4113 Nin : Node_Id;
4114
4115 begin
4116 -- The last membership test is marked to prevent recursion
4117
4118 Nin :=
4119 Make_In (Loc,
4120 Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)),
4121 Right_Opnd => New_Occurrence_Of (T, Loc));
4122 Set_No_Minimize_Eliminate (Nin);
4123
4124 -- Now do the rewrite
4125
4126 Rewrite (N,
4127 Make_And_Then (Loc,
4128 Left_Opnd =>
4129 Make_In (Loc,
4130 Left_Opnd => Lop,
4131 Right_Opnd =>
4132 Make_Range (Loc,
4133 Low_Bound =>
4134 Convert_To (LLIB,
4135 Make_Attribute_Reference (Loc,
4136 Attribute_Name => Name_First,
4137 Prefix => New_Occurrence_Of (TB, Loc))),
4138 High_Bound =>
4139 Convert_To (LLIB,
4140 Make_Attribute_Reference (Loc,
4141 Attribute_Name => Name_Last,
4142 Prefix => New_Occurrence_Of (TB, Loc))))),
4143 Right_Opnd => Nin));
4144 Set_Analyzed (N, False);
4145 Analyze_And_Resolve (N, Restype);
4146 end;
4147 end if;
4148 end if;
4149 end Expand_Membership_Minimize_Eliminate_Overflow;
4150
4151 ------------------------
4152 -- Expand_N_Allocator --
4153 ------------------------
4154
4155 procedure Expand_N_Allocator (N : Node_Id) is
4156 Etyp : constant Entity_Id := Etype (Expression (N));
4157 Loc : constant Source_Ptr := Sloc (N);
4158 PtrT : constant Entity_Id := Etype (N);
4159
4160 procedure Rewrite_Coextension (N : Node_Id);
4161 -- Static coextensions have the same lifetime as the entity they
4162 -- constrain. Such occurrences can be rewritten as aliased objects
4163 -- and their unrestricted access used instead of the coextension.
4164
4165 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
4166 -- Given a constrained array type E, returns a node representing the
4167 -- code to compute the size in storage elements for the given type.
4168 -- This is done without using the attribute (which malfunctions for
4169 -- large sizes ???)
4170
4171 -------------------------
4172 -- Rewrite_Coextension --
4173 -------------------------
4174
4175 procedure Rewrite_Coextension (N : Node_Id) is
4176 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
4177 Temp_Decl : Node_Id;
4178
4179 begin
4180 -- Generate:
4181 -- Cnn : aliased Etyp;
4182
4183 Temp_Decl :=
4184 Make_Object_Declaration (Loc,
4185 Defining_Identifier => Temp_Id,
4186 Aliased_Present => True,
4187 Object_Definition => New_Occurrence_Of (Etyp, Loc));
4188
4189 if Nkind (Expression (N)) = N_Qualified_Expression then
4190 Set_Expression (Temp_Decl, Expression (Expression (N)));
4191 end if;
4192
4193 Insert_Action (N, Temp_Decl);
4194 Rewrite (N,
4195 Make_Attribute_Reference (Loc,
4196 Prefix => New_Occurrence_Of (Temp_Id, Loc),
4197 Attribute_Name => Name_Unrestricted_Access));
4198
4199 Analyze_And_Resolve (N, PtrT);
4200 end Rewrite_Coextension;
4201
4202 ------------------------------
4203 -- Size_In_Storage_Elements --
4204 ------------------------------
4205
4206 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
4207 begin
4208 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4209 -- However, the reason for the existence of this function is
4210 -- to construct a test for sizes too large, which means near the
4211 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4212 -- is that we get overflows when sizes are greater than 2**31.
4213
4214 -- So what we end up doing for array types is to use the expression:
4215
4216 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4217
4218 -- which avoids this problem. All this is a bit bogus, but it does
4219 -- mean we catch common cases of trying to allocate arrays that
4220 -- are too large, and which in the absence of a check results in
4221 -- undetected chaos ???
4222
4223 -- Note in particular that this is a pessimistic estimate in the
4224 -- case of packed array types, where an array element might occupy
4225 -- just a fraction of a storage element???
4226
4227 declare
4228 Len : Node_Id;
4229 Res : Node_Id;
4230
4231 begin
4232 for J in 1 .. Number_Dimensions (E) loop
4233 Len :=
4234 Make_Attribute_Reference (Loc,
4235 Prefix => New_Occurrence_Of (E, Loc),
4236 Attribute_Name => Name_Length,
4237 Expressions => New_List (Make_Integer_Literal (Loc, J)));
4238
4239 if J = 1 then
4240 Res := Len;
4241
4242 else
4243 Res :=
4244 Make_Op_Multiply (Loc,
4245 Left_Opnd => Res,
4246 Right_Opnd => Len);
4247 end if;
4248 end loop;
4249
4250 return
4251 Make_Op_Multiply (Loc,
4252 Left_Opnd => Len,
4253 Right_Opnd =>
4254 Make_Attribute_Reference (Loc,
4255 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
4256 Attribute_Name => Name_Max_Size_In_Storage_Elements));
4257 end;
4258 end Size_In_Storage_Elements;
4259
4260 -- Local variables
4261
4262 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
4263 Desig : Entity_Id;
4264 Nod : Node_Id;
4265 Pool : Entity_Id;
4266 Rel_Typ : Entity_Id;
4267 Temp : Entity_Id;
4268
4269 -- Start of processing for Expand_N_Allocator
4270
4271 begin
4272 -- RM E.2.3(22). We enforce that the expected type of an allocator
4273 -- shall not be a remote access-to-class-wide-limited-private type
4274
4275 -- Why is this being done at expansion time, seems clearly wrong ???
4276
4277 Validate_Remote_Access_To_Class_Wide_Type (N);
4278
4279 -- Processing for anonymous access-to-controlled types. These access
4280 -- types receive a special finalization master which appears in the
4281 -- declarations of the enclosing semantic unit. This expansion is done
4282 -- now to ensure that any additional types generated by this routine or
4283 -- Expand_Allocator_Expression inherit the proper type attributes.
4284
4285 if (Ekind (PtrT) = E_Anonymous_Access_Type
4286 or else (Is_Itype (PtrT) and then No (Finalization_Master (PtrT))))
4287 and then Needs_Finalization (Dtyp)
4288 then
4289 -- Detect the allocation of an anonymous controlled object where the
4290 -- type of the context is named. For example:
4291
4292 -- procedure Proc (Ptr : Named_Access_Typ);
4293 -- Proc (new Designated_Typ);
4294
4295 -- Regardless of the anonymous-to-named access type conversion, the
4296 -- lifetime of the object must be associated with the named access
4297 -- type. Use the finalization-related attributes of this type.
4298
4299 if Nkind_In (Parent (N), N_Type_Conversion,
4300 N_Unchecked_Type_Conversion)
4301 and then Ekind_In (Etype (Parent (N)), E_Access_Subtype,
4302 E_Access_Type,
4303 E_General_Access_Type)
4304 then
4305 Rel_Typ := Etype (Parent (N));
4306 else
4307 Rel_Typ := Empty;
4308 end if;
4309
4310 -- Anonymous access-to-controlled types allocate on the global pool.
4311 -- Do not set this attribute on .NET/JVM since those targets do not
4312 -- support pools.
4313
4314 if No (Associated_Storage_Pool (PtrT)) and then VM_Target = No_VM then
4315 if Present (Rel_Typ) then
4316 Set_Associated_Storage_Pool (PtrT,
4317 Associated_Storage_Pool (Rel_Typ));
4318 else
4319 Set_Associated_Storage_Pool (PtrT,
4320 Get_Global_Pool_For_Access_Type (PtrT));
4321 end if;
4322 end if;
4323
4324 -- The finalization master must be inserted and analyzed as part of
4325 -- the current semantic unit. Note that the master is updated when
4326 -- analysis changes current units.
4327
4328 if Present (Rel_Typ) then
4329 Set_Finalization_Master (PtrT, Finalization_Master (Rel_Typ));
4330 else
4331 Set_Finalization_Master (PtrT, Current_Anonymous_Master);
4332 end if;
4333 end if;
4334
4335 -- Set the storage pool and find the appropriate version of Allocate to
4336 -- call. Do not overwrite the storage pool if it is already set, which
4337 -- can happen for build-in-place function returns (see
4338 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4339
4340 if No (Storage_Pool (N)) then
4341 Pool := Associated_Storage_Pool (Root_Type (PtrT));
4342
4343 if Present (Pool) then
4344 Set_Storage_Pool (N, Pool);
4345
4346 if Is_RTE (Pool, RE_SS_Pool) then
4347 if VM_Target = No_VM then
4348 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
4349 end if;
4350
4351 -- In the case of an allocator for a simple storage pool, locate
4352 -- and save a reference to the pool type's Allocate routine.
4353
4354 elsif Present (Get_Rep_Pragma
4355 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4356 then
4357 declare
4358 Pool_Type : constant Entity_Id := Base_Type (Etype (Pool));
4359 Alloc_Op : Entity_Id;
4360 begin
4361 Alloc_Op := Get_Name_Entity_Id (Name_Allocate);
4362 while Present (Alloc_Op) loop
4363 if Scope (Alloc_Op) = Scope (Pool_Type)
4364 and then Present (First_Formal (Alloc_Op))
4365 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
4366 then
4367 Set_Procedure_To_Call (N, Alloc_Op);
4368 exit;
4369 else
4370 Alloc_Op := Homonym (Alloc_Op);
4371 end if;
4372 end loop;
4373 end;
4374
4375 elsif Is_Class_Wide_Type (Etype (Pool)) then
4376 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
4377
4378 else
4379 Set_Procedure_To_Call (N,
4380 Find_Prim_Op (Etype (Pool), Name_Allocate));
4381 end if;
4382 end if;
4383 end if;
4384
4385 -- Under certain circumstances we can replace an allocator by an access
4386 -- to statically allocated storage. The conditions, as noted in AARM
4387 -- 3.10 (10c) are as follows:
4388
4389 -- Size and initial value is known at compile time
4390 -- Access type is access-to-constant
4391
4392 -- The allocator is not part of a constraint on a record component,
4393 -- because in that case the inserted actions are delayed until the
4394 -- record declaration is fully analyzed, which is too late for the
4395 -- analysis of the rewritten allocator.
4396
4397 if Is_Access_Constant (PtrT)
4398 and then Nkind (Expression (N)) = N_Qualified_Expression
4399 and then Compile_Time_Known_Value (Expression (Expression (N)))
4400 and then Size_Known_At_Compile_Time
4401 (Etype (Expression (Expression (N))))
4402 and then not Is_Record_Type (Current_Scope)
4403 then
4404 -- Here we can do the optimization. For the allocator
4405
4406 -- new x'(y)
4407
4408 -- We insert an object declaration
4409
4410 -- Tnn : aliased x := y;
4411
4412 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4413 -- marked as requiring static allocation.
4414
4415 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
4416 Desig := Subtype_Mark (Expression (N));
4417
4418 -- If context is constrained, use constrained subtype directly,
4419 -- so that the constant is not labelled as having a nominally
4420 -- unconstrained subtype.
4421
4422 if Entity (Desig) = Base_Type (Dtyp) then
4423 Desig := New_Occurrence_Of (Dtyp, Loc);
4424 end if;
4425
4426 Insert_Action (N,
4427 Make_Object_Declaration (Loc,
4428 Defining_Identifier => Temp,
4429 Aliased_Present => True,
4430 Constant_Present => Is_Access_Constant (PtrT),
4431 Object_Definition => Desig,
4432 Expression => Expression (Expression (N))));
4433
4434 Rewrite (N,
4435 Make_Attribute_Reference (Loc,
4436 Prefix => New_Occurrence_Of (Temp, Loc),
4437 Attribute_Name => Name_Unrestricted_Access));
4438
4439 Analyze_And_Resolve (N, PtrT);
4440
4441 -- We set the variable as statically allocated, since we don't want
4442 -- it going on the stack of the current procedure.
4443
4444 Set_Is_Statically_Allocated (Temp);
4445 return;
4446 end if;
4447
4448 -- Same if the allocator is an access discriminant for a local object:
4449 -- instead of an allocator we create a local value and constrain the
4450 -- enclosing object with the corresponding access attribute.
4451
4452 if Is_Static_Coextension (N) then
4453 Rewrite_Coextension (N);
4454 return;
4455 end if;
4456
4457 -- Check for size too large, we do this because the back end misses
4458 -- proper checks here and can generate rubbish allocation calls when
4459 -- we are near the limit. We only do this for the 32-bit address case
4460 -- since that is from a practical point of view where we see a problem.
4461
4462 if System_Address_Size = 32
4463 and then not Storage_Checks_Suppressed (PtrT)
4464 and then not Storage_Checks_Suppressed (Dtyp)
4465 and then not Storage_Checks_Suppressed (Etyp)
4466 then
4467 -- The check we want to generate should look like
4468
4469 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4470 -- raise Storage_Error;
4471 -- end if;
4472
4473 -- where 3.5 gigabytes is a constant large enough to accommodate any
4474 -- reasonable request for. But we can't do it this way because at
4475 -- least at the moment we don't compute this attribute right, and
4476 -- can silently give wrong results when the result gets large. Since
4477 -- this is all about large results, that's bad, so instead we only
4478 -- apply the check for constrained arrays, and manually compute the
4479 -- value of the attribute ???
4480
4481 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
4482 Insert_Action (N,
4483 Make_Raise_Storage_Error (Loc,
4484 Condition =>
4485 Make_Op_Gt (Loc,
4486 Left_Opnd => Size_In_Storage_Elements (Etyp),
4487 Right_Opnd =>
4488 Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))),
4489 Reason => SE_Object_Too_Large));
4490 end if;
4491 end if;
4492
4493 -- Handle case of qualified expression (other than optimization above)
4494 -- First apply constraint checks, because the bounds or discriminants
4495 -- in the aggregate might not match the subtype mark in the allocator.
4496
4497 if Nkind (Expression (N)) = N_Qualified_Expression then
4498 Apply_Constraint_Check
4499 (Expression (Expression (N)), Etype (Expression (N)));
4500
4501 Expand_Allocator_Expression (N);
4502 return;
4503 end if;
4504
4505 -- If the allocator is for a type which requires initialization, and
4506 -- there is no initial value (i.e. operand is a subtype indication
4507 -- rather than a qualified expression), then we must generate a call to
4508 -- the initialization routine using an expressions action node:
4509
4510 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4511
4512 -- Here ptr_T is the pointer type for the allocator, and T is the
4513 -- subtype of the allocator. A special case arises if the designated
4514 -- type of the access type is a task or contains tasks. In this case
4515 -- the call to Init (Temp.all ...) is replaced by code that ensures
4516 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4517 -- for details). In addition, if the type T is a task T, then the
4518 -- first argument to Init must be converted to the task record type.
4519
4520 declare
4521 T : constant Entity_Id := Entity (Expression (N));
4522 Args : List_Id;
4523 Decls : List_Id;
4524 Decl : Node_Id;
4525 Discr : Elmt_Id;
4526 Init : Entity_Id;
4527 Init_Arg1 : Node_Id;
4528 Temp_Decl : Node_Id;
4529 Temp_Type : Entity_Id;
4530
4531 begin
4532 if No_Initialization (N) then
4533
4534 -- Even though this might be a simple allocation, create a custom
4535 -- Allocate if the context requires it. Since .NET/JVM compilers
4536 -- do not support pools, this step is skipped.
4537
4538 if VM_Target = No_VM
4539 and then Present (Finalization_Master (PtrT))
4540 then
4541 Build_Allocate_Deallocate_Proc
4542 (N => N,
4543 Is_Allocate => True);
4544 end if;
4545
4546 -- Case of no initialization procedure present
4547
4548 elsif not Has_Non_Null_Base_Init_Proc (T) then
4549
4550 -- Case of simple initialization required
4551
4552 if Needs_Simple_Initialization (T) then
4553 Check_Restriction (No_Default_Initialization, N);
4554 Rewrite (Expression (N),
4555 Make_Qualified_Expression (Loc,
4556 Subtype_Mark => New_Occurrence_Of (T, Loc),
4557 Expression => Get_Simple_Init_Val (T, N)));
4558
4559 Analyze_And_Resolve (Expression (Expression (N)), T);
4560 Analyze_And_Resolve (Expression (N), T);
4561 Set_Paren_Count (Expression (Expression (N)), 1);
4562 Expand_N_Allocator (N);
4563
4564 -- No initialization required
4565
4566 else
4567 null;
4568 end if;
4569
4570 -- Case of initialization procedure present, must be called
4571
4572 else
4573 Check_Restriction (No_Default_Initialization, N);
4574
4575 if not Restriction_Active (No_Default_Initialization) then
4576 Init := Base_Init_Proc (T);
4577 Nod := N;
4578 Temp := Make_Temporary (Loc, 'P');
4579
4580 -- Construct argument list for the initialization routine call
4581
4582 Init_Arg1 :=
4583 Make_Explicit_Dereference (Loc,
4584 Prefix =>
4585 New_Occurrence_Of (Temp, Loc));
4586
4587 Set_Assignment_OK (Init_Arg1);
4588 Temp_Type := PtrT;
4589
4590 -- The initialization procedure expects a specific type. if the
4591 -- context is access to class wide, indicate that the object
4592 -- being allocated has the right specific type.
4593
4594 if Is_Class_Wide_Type (Dtyp) then
4595 Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1);
4596 end if;
4597
4598 -- If designated type is a concurrent type or if it is private
4599 -- type whose definition is a concurrent type, the first
4600 -- argument in the Init routine has to be unchecked conversion
4601 -- to the corresponding record type. If the designated type is
4602 -- a derived type, also convert the argument to its root type.
4603
4604 if Is_Concurrent_Type (T) then
4605 Init_Arg1 :=
4606 Unchecked_Convert_To (
4607 Corresponding_Record_Type (T), Init_Arg1);
4608
4609 elsif Is_Private_Type (T)
4610 and then Present (Full_View (T))
4611 and then Is_Concurrent_Type (Full_View (T))
4612 then
4613 Init_Arg1 :=
4614 Unchecked_Convert_To
4615 (Corresponding_Record_Type (Full_View (T)), Init_Arg1);
4616
4617 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
4618 declare
4619 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
4620
4621 begin
4622 Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1);
4623 Set_Etype (Init_Arg1, Ftyp);
4624 end;
4625 end if;
4626
4627 Args := New_List (Init_Arg1);
4628
4629 -- For the task case, pass the Master_Id of the access type as
4630 -- the value of the _Master parameter, and _Chain as the value
4631 -- of the _Chain parameter (_Chain will be defined as part of
4632 -- the generated code for the allocator).
4633
4634 -- In Ada 2005, the context may be a function that returns an
4635 -- anonymous access type. In that case the Master_Id has been
4636 -- created when expanding the function declaration.
4637
4638 if Has_Task (T) then
4639 if No (Master_Id (Base_Type (PtrT))) then
4640
4641 -- The designated type was an incomplete type, and the
4642 -- access type did not get expanded. Salvage it now.
4643
4644 if not Restriction_Active (No_Task_Hierarchy) then
4645 if Present (Parent (Base_Type (PtrT))) then
4646 Expand_N_Full_Type_Declaration
4647 (Parent (Base_Type (PtrT)));
4648
4649 -- The only other possibility is an itype. For this
4650 -- case, the master must exist in the context. This is
4651 -- the case when the allocator initializes an access
4652 -- component in an init-proc.
4653
4654 else
4655 pragma Assert (Is_Itype (PtrT));
4656 Build_Master_Renaming (PtrT, N);
4657 end if;
4658 end if;
4659 end if;
4660
4661 -- If the context of the allocator is a declaration or an
4662 -- assignment, we can generate a meaningful image for it,
4663 -- even though subsequent assignments might remove the
4664 -- connection between task and entity. We build this image
4665 -- when the left-hand side is a simple variable, a simple
4666 -- indexed assignment or a simple selected component.
4667
4668 if Nkind (Parent (N)) = N_Assignment_Statement then
4669 declare
4670 Nam : constant Node_Id := Name (Parent (N));
4671
4672 begin
4673 if Is_Entity_Name (Nam) then
4674 Decls :=
4675 Build_Task_Image_Decls
4676 (Loc,
4677 New_Occurrence_Of
4678 (Entity (Nam), Sloc (Nam)), T);
4679
4680 elsif Nkind_In (Nam, N_Indexed_Component,
4681 N_Selected_Component)
4682 and then Is_Entity_Name (Prefix (Nam))
4683 then
4684 Decls :=
4685 Build_Task_Image_Decls
4686 (Loc, Nam, Etype (Prefix (Nam)));
4687 else
4688 Decls := Build_Task_Image_Decls (Loc, T, T);
4689 end if;
4690 end;
4691
4692 elsif Nkind (Parent (N)) = N_Object_Declaration then
4693 Decls :=
4694 Build_Task_Image_Decls
4695 (Loc, Defining_Identifier (Parent (N)), T);
4696
4697 else
4698 Decls := Build_Task_Image_Decls (Loc, T, T);
4699 end if;
4700
4701 if Restriction_Active (No_Task_Hierarchy) then
4702 Append_To (Args,
4703 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
4704 else
4705 Append_To (Args,
4706 New_Occurrence_Of
4707 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
4708 end if;
4709
4710 Append_To (Args, Make_Identifier (Loc, Name_uChain));
4711
4712 Decl := Last (Decls);
4713 Append_To (Args,
4714 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
4715
4716 -- Has_Task is false, Decls not used
4717
4718 else
4719 Decls := No_List;
4720 end if;
4721
4722 -- Add discriminants if discriminated type
4723
4724 declare
4725 Dis : Boolean := False;
4726 Typ : Entity_Id;
4727
4728 begin
4729 if Has_Discriminants (T) then
4730 Dis := True;
4731 Typ := T;
4732
4733 elsif Is_Private_Type (T)
4734 and then Present (Full_View (T))
4735 and then Has_Discriminants (Full_View (T))
4736 then
4737 Dis := True;
4738 Typ := Full_View (T);
4739 end if;
4740
4741 if Dis then
4742
4743 -- If the allocated object will be constrained by the
4744 -- default values for discriminants, then build a subtype
4745 -- with those defaults, and change the allocated subtype
4746 -- to that. Note that this happens in fewer cases in Ada
4747 -- 2005 (AI-363).
4748
4749 if not Is_Constrained (Typ)
4750 and then Present (Discriminant_Default_Value
4751 (First_Discriminant (Typ)))
4752 and then (Ada_Version < Ada_2005
4753 or else not
4754 Object_Type_Has_Constrained_Partial_View
4755 (Typ, Current_Scope))
4756 then
4757 Typ := Build_Default_Subtype (Typ, N);
4758 Set_Expression (N, New_Occurrence_Of (Typ, Loc));
4759 end if;
4760
4761 Discr := First_Elmt (Discriminant_Constraint (Typ));
4762 while Present (Discr) loop
4763 Nod := Node (Discr);
4764 Append (New_Copy_Tree (Node (Discr)), Args);
4765
4766 -- AI-416: when the discriminant constraint is an
4767 -- anonymous access type make sure an accessibility
4768 -- check is inserted if necessary (3.10.2(22.q/2))
4769
4770 if Ada_Version >= Ada_2005
4771 and then
4772 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
4773 then
4774 Apply_Accessibility_Check
4775 (Nod, Typ, Insert_Node => Nod);
4776 end if;
4777
4778 Next_Elmt (Discr);
4779 end loop;
4780 end if;
4781 end;
4782
4783 -- We set the allocator as analyzed so that when we analyze
4784 -- the if expression node, we do not get an unwanted recursive
4785 -- expansion of the allocator expression.
4786
4787 Set_Analyzed (N, True);
4788 Nod := Relocate_Node (N);
4789
4790 -- Here is the transformation:
4791 -- input: new Ctrl_Typ
4792 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4793 -- Ctrl_TypIP (Temp.all, ...);
4794 -- [Deep_]Initialize (Temp.all);
4795
4796 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4797 -- is the subtype of the allocator.
4798
4799 Temp_Decl :=
4800 Make_Object_Declaration (Loc,
4801 Defining_Identifier => Temp,
4802 Constant_Present => True,
4803 Object_Definition => New_Occurrence_Of (Temp_Type, Loc),
4804 Expression => Nod);
4805
4806 Set_Assignment_OK (Temp_Decl);
4807 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
4808
4809 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
4810
4811 -- If the designated type is a task type or contains tasks,
4812 -- create block to activate created tasks, and insert
4813 -- declaration for Task_Image variable ahead of call.
4814
4815 if Has_Task (T) then
4816 declare
4817 L : constant List_Id := New_List;
4818 Blk : Node_Id;
4819 begin
4820 Build_Task_Allocate_Block (L, Nod, Args);
4821 Blk := Last (L);
4822 Insert_List_Before (First (Declarations (Blk)), Decls);
4823 Insert_Actions (N, L);
4824 end;
4825
4826 else
4827 Insert_Action (N,
4828 Make_Procedure_Call_Statement (Loc,
4829 Name => New_Occurrence_Of (Init, Loc),
4830 Parameter_Associations => Args));
4831 end if;
4832
4833 if Needs_Finalization (T) then
4834
4835 -- Generate:
4836 -- [Deep_]Initialize (Init_Arg1);
4837
4838 Insert_Action (N,
4839 Make_Init_Call
4840 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4841 Typ => T));
4842
4843 if Present (Finalization_Master (PtrT)) then
4844
4845 -- Special processing for .NET/JVM, the allocated object
4846 -- is attached to the finalization master. Generate:
4847
4848 -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1));
4849
4850 -- Types derived from [Limited_]Controlled are the only
4851 -- ones considered since they have fields Prev and Next.
4852
4853 if VM_Target /= No_VM then
4854 if Is_Controlled (T) then
4855 Insert_Action (N,
4856 Make_Attach_Call
4857 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4858 Ptr_Typ => PtrT));
4859 end if;
4860
4861 -- Default case, generate:
4862
4863 -- Set_Finalize_Address
4864 -- (<PtrT>FM, <T>FD'Unrestricted_Access);
4865
4866 -- Do not generate this call in CodePeer mode, as TSS
4867 -- primitive Finalize_Address is not created in this
4868 -- mode.
4869
4870 elsif not CodePeer_Mode then
4871 Insert_Action (N,
4872 Make_Set_Finalize_Address_Call
4873 (Loc => Loc,
4874 Typ => T,
4875 Ptr_Typ => PtrT));
4876 end if;
4877 end if;
4878 end if;
4879
4880 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4881 Analyze_And_Resolve (N, PtrT);
4882 end if;
4883 end if;
4884 end;
4885
4886 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
4887 -- object that has been rewritten as a reference, we displace "this"
4888 -- to reference properly its secondary dispatch table.
4889
4890 if Nkind (N) = N_Identifier and then Is_Interface (Dtyp) then
4891 Displace_Allocator_Pointer (N);
4892 end if;
4893
4894 exception
4895 when RE_Not_Available =>
4896 return;
4897 end Expand_N_Allocator;
4898
4899 -----------------------
4900 -- Expand_N_And_Then --
4901 -----------------------
4902
4903 procedure Expand_N_And_Then (N : Node_Id)
4904 renames Expand_Short_Circuit_Operator;
4905
4906 ------------------------------
4907 -- Expand_N_Case_Expression --
4908 ------------------------------
4909
4910 procedure Expand_N_Case_Expression (N : Node_Id) is
4911 Loc : constant Source_Ptr := Sloc (N);
4912 Typ : constant Entity_Id := Etype (N);
4913 Cstmt : Node_Id;
4914 Decl : Node_Id;
4915 Tnn : Entity_Id;
4916 Pnn : Entity_Id;
4917 Actions : List_Id;
4918 Ttyp : Entity_Id;
4919 Alt : Node_Id;
4920 Fexp : Node_Id;
4921
4922 begin
4923 -- Check for MINIMIZED/ELIMINATED overflow mode
4924
4925 if Minimized_Eliminated_Overflow_Check (N) then
4926 Apply_Arithmetic_Overflow_Check (N);
4927 return;
4928 end if;
4929
4930 -- If the case expression is a predicate specification, do not
4931 -- expand, because it will be converted to the proper predicate
4932 -- form when building the predicate function.
4933
4934 if Ekind_In (Current_Scope, E_Function, E_Procedure)
4935 and then Is_Predicate_Function (Current_Scope)
4936 then
4937 return;
4938 end if;
4939
4940 -- We expand
4941
4942 -- case X is when A => AX, when B => BX ...
4943
4944 -- to
4945
4946 -- do
4947 -- Tnn : typ;
4948 -- case X is
4949 -- when A =>
4950 -- Tnn := AX;
4951 -- when B =>
4952 -- Tnn := BX;
4953 -- ...
4954 -- end case;
4955 -- in Tnn end;
4956
4957 -- However, this expansion is wrong for limited types, and also
4958 -- wrong for unconstrained types (since the bounds may not be the
4959 -- same in all branches). Furthermore it involves an extra copy
4960 -- for large objects. So we take care of this by using the following
4961 -- modified expansion for non-elementary types:
4962
4963 -- do
4964 -- type Pnn is access all typ;
4965 -- Tnn : Pnn;
4966 -- case X is
4967 -- when A =>
4968 -- T := AX'Unrestricted_Access;
4969 -- when B =>
4970 -- T := BX'Unrestricted_Access;
4971 -- ...
4972 -- end case;
4973 -- in Tnn.all end;
4974
4975 Cstmt :=
4976 Make_Case_Statement (Loc,
4977 Expression => Expression (N),
4978 Alternatives => New_List);
4979
4980 Actions := New_List;
4981
4982 -- Scalar case
4983
4984 if Is_Elementary_Type (Typ) then
4985 Ttyp := Typ;
4986
4987 else
4988 Pnn := Make_Temporary (Loc, 'P');
4989 Append_To (Actions,
4990 Make_Full_Type_Declaration (Loc,
4991 Defining_Identifier => Pnn,
4992 Type_Definition =>
4993 Make_Access_To_Object_Definition (Loc,
4994 All_Present => True,
4995 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
4996 Ttyp := Pnn;
4997 end if;
4998
4999 Tnn := Make_Temporary (Loc, 'T');
5000
5001 -- Create declaration for target of expression, and indicate that it
5002 -- does not require initialization.
5003
5004 Decl :=
5005 Make_Object_Declaration (Loc,
5006 Defining_Identifier => Tnn,
5007 Object_Definition => New_Occurrence_Of (Ttyp, Loc));
5008 Set_No_Initialization (Decl);
5009 Append_To (Actions, Decl);
5010
5011 -- Now process the alternatives
5012
5013 Alt := First (Alternatives (N));
5014 while Present (Alt) loop
5015 declare
5016 Aexp : Node_Id := Expression (Alt);
5017 Aloc : constant Source_Ptr := Sloc (Aexp);
5018 Stats : List_Id;
5019
5020 begin
5021 -- As described above, take Unrestricted_Access for case of non-
5022 -- scalar types, to avoid big copies, and special cases.
5023
5024 if not Is_Elementary_Type (Typ) then
5025 Aexp :=
5026 Make_Attribute_Reference (Aloc,
5027 Prefix => Relocate_Node (Aexp),
5028 Attribute_Name => Name_Unrestricted_Access);
5029 end if;
5030
5031 Stats := New_List (
5032 Make_Assignment_Statement (Aloc,
5033 Name => New_Occurrence_Of (Tnn, Loc),
5034 Expression => Aexp));
5035
5036 -- Propagate declarations inserted in the node by Insert_Actions
5037 -- (for example, temporaries generated to remove side effects).
5038 -- These actions must remain attached to the alternative, given
5039 -- that they are generated by the corresponding expression.
5040
5041 if Present (Sinfo.Actions (Alt)) then
5042 Prepend_List (Sinfo.Actions (Alt), Stats);
5043 end if;
5044
5045 Append_To
5046 (Alternatives (Cstmt),
5047 Make_Case_Statement_Alternative (Sloc (Alt),
5048 Discrete_Choices => Discrete_Choices (Alt),
5049 Statements => Stats));
5050 end;
5051
5052 Next (Alt);
5053 end loop;
5054
5055 Append_To (Actions, Cstmt);
5056
5057 -- Construct and return final expression with actions
5058
5059 if Is_Elementary_Type (Typ) then
5060 Fexp := New_Occurrence_Of (Tnn, Loc);
5061 else
5062 Fexp :=
5063 Make_Explicit_Dereference (Loc,
5064 Prefix => New_Occurrence_Of (Tnn, Loc));
5065 end if;
5066
5067 Rewrite (N,
5068 Make_Expression_With_Actions (Loc,
5069 Expression => Fexp,
5070 Actions => Actions));
5071
5072 Analyze_And_Resolve (N, Typ);
5073 end Expand_N_Case_Expression;
5074
5075 -----------------------------------
5076 -- Expand_N_Explicit_Dereference --
5077 -----------------------------------
5078
5079 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
5080 begin
5081 -- Insert explicit dereference call for the checked storage pool case
5082
5083 Insert_Dereference_Action (Prefix (N));
5084
5085 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5086 -- we set the atomic sync flag.
5087
5088 if Is_Atomic (Etype (N))
5089 and then not Atomic_Synchronization_Disabled (Etype (N))
5090 then
5091 Activate_Atomic_Synchronization (N);
5092 end if;
5093 end Expand_N_Explicit_Dereference;
5094
5095 --------------------------------------
5096 -- Expand_N_Expression_With_Actions --
5097 --------------------------------------
5098
5099 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
5100
5101 function Process_Action (Act : Node_Id) return Traverse_Result;
5102 -- Inspect and process a single action of an expression_with_actions for
5103 -- transient controlled objects. If such objects are found, the routine
5104 -- generates code to clean them up when the context of the expression is
5105 -- evaluated or elaborated.
5106
5107 --------------------
5108 -- Process_Action --
5109 --------------------
5110
5111 function Process_Action (Act : Node_Id) return Traverse_Result is
5112 begin
5113 if Nkind (Act) = N_Object_Declaration
5114 and then Is_Finalizable_Transient (Act, N)
5115 then
5116 Process_Transient_Object (Act, N);
5117 return Abandon;
5118
5119 -- Avoid processing temporary function results multiple times when
5120 -- dealing with nested expression_with_actions.
5121
5122 elsif Nkind (Act) = N_Expression_With_Actions then
5123 return Abandon;
5124
5125 -- Do not process temporary function results in loops. This is done
5126 -- by Expand_N_Loop_Statement and Build_Finalizer.
5127
5128 elsif Nkind (Act) = N_Loop_Statement then
5129 return Abandon;
5130 end if;
5131
5132 return OK;
5133 end Process_Action;
5134
5135 procedure Process_Single_Action is new Traverse_Proc (Process_Action);
5136
5137 -- Local variables
5138
5139 Act : Node_Id;
5140
5141 -- Start of processing for Expand_N_Expression_With_Actions
5142
5143 begin
5144 -- Process the actions as described above
5145
5146 Act := First (Actions (N));
5147 while Present (Act) loop
5148 Process_Single_Action (Act);
5149 Next (Act);
5150 end loop;
5151
5152 -- Deal with case where there are no actions. In this case we simply
5153 -- rewrite the node with its expression since we don't need the actions
5154 -- and the specification of this node does not allow a null action list.
5155
5156 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5157 -- the expanded tree and relying on being able to retrieve the original
5158 -- tree in cases like this. This raises a whole lot of issues of whether
5159 -- we have problems elsewhere, which will be addressed in the future???
5160
5161 if Is_Empty_List (Actions (N)) then
5162 Rewrite (N, Relocate_Node (Expression (N)));
5163 end if;
5164 end Expand_N_Expression_With_Actions;
5165
5166 ----------------------------
5167 -- Expand_N_If_Expression --
5168 ----------------------------
5169
5170 -- Deal with limited types and condition actions
5171
5172 procedure Expand_N_If_Expression (N : Node_Id) is
5173 procedure Process_Actions (Actions : List_Id);
5174 -- Inspect and process a single action list of an if expression for
5175 -- transient controlled objects. If such objects are found, the routine
5176 -- generates code to clean them up when the context of the expression is
5177 -- evaluated or elaborated.
5178
5179 ---------------------
5180 -- Process_Actions --
5181 ---------------------
5182
5183 procedure Process_Actions (Actions : List_Id) is
5184 Act : Node_Id;
5185
5186 begin
5187 Act := First (Actions);
5188 while Present (Act) loop
5189 if Nkind (Act) = N_Object_Declaration
5190 and then Is_Finalizable_Transient (Act, N)
5191 then
5192 Process_Transient_Object (Act, N);
5193 end if;
5194
5195 Next (Act);
5196 end loop;
5197 end Process_Actions;
5198
5199 -- Local variables
5200
5201 Loc : constant Source_Ptr := Sloc (N);
5202 Cond : constant Node_Id := First (Expressions (N));
5203 Thenx : constant Node_Id := Next (Cond);
5204 Elsex : constant Node_Id := Next (Thenx);
5205 Typ : constant Entity_Id := Etype (N);
5206
5207 Actions : List_Id;
5208 Cnn : Entity_Id;
5209 Decl : Node_Id;
5210 Expr : Node_Id;
5211 New_If : Node_Id;
5212 New_N : Node_Id;
5213 Ptr_Typ : Entity_Id;
5214
5215 -- Start of processing for Expand_N_If_Expression
5216
5217 begin
5218 -- Check for MINIMIZED/ELIMINATED overflow mode
5219
5220 if Minimized_Eliminated_Overflow_Check (N) then
5221 Apply_Arithmetic_Overflow_Check (N);
5222 return;
5223 end if;
5224
5225 -- Fold at compile time if condition known. We have already folded
5226 -- static if expressions, but it is possible to fold any case in which
5227 -- the condition is known at compile time, even though the result is
5228 -- non-static.
5229
5230 -- Note that we don't do the fold of such cases in Sem_Elab because
5231 -- it can cause infinite loops with the expander adding a conditional
5232 -- expression, and Sem_Elab circuitry removing it repeatedly.
5233
5234 if Compile_Time_Known_Value (Cond) then
5235 if Is_True (Expr_Value (Cond)) then
5236 Expr := Thenx;
5237 Actions := Then_Actions (N);
5238 else
5239 Expr := Elsex;
5240 Actions := Else_Actions (N);
5241 end if;
5242
5243 Remove (Expr);
5244
5245 if Present (Actions) then
5246 Rewrite (N,
5247 Make_Expression_With_Actions (Loc,
5248 Expression => Relocate_Node (Expr),
5249 Actions => Actions));
5250 Analyze_And_Resolve (N, Typ);
5251 else
5252 Rewrite (N, Relocate_Node (Expr));
5253 end if;
5254
5255 -- Note that the result is never static (legitimate cases of static
5256 -- if expressions were folded in Sem_Eval).
5257
5258 Set_Is_Static_Expression (N, False);
5259 return;
5260 end if;
5261
5262 -- If the type is limited or unconstrained, we expand as follows to
5263 -- avoid any possibility of improper copies.
5264
5265 -- Note: it may be possible to avoid this special processing if the
5266 -- back end uses its own mechanisms for handling by-reference types ???
5267
5268 -- type Ptr is access all Typ;
5269 -- Cnn : Ptr;
5270 -- if cond then
5271 -- <<then actions>>
5272 -- Cnn := then-expr'Unrestricted_Access;
5273 -- else
5274 -- <<else actions>>
5275 -- Cnn := else-expr'Unrestricted_Access;
5276 -- end if;
5277
5278 -- and replace the if expression by a reference to Cnn.all.
5279
5280 -- This special case can be skipped if the back end handles limited
5281 -- types properly and ensures that no incorrect copies are made.
5282
5283 if Is_By_Reference_Type (Typ)
5284 and then not Back_End_Handles_Limited_Types
5285 then
5286 -- When the "then" or "else" expressions involve controlled function
5287 -- calls, generated temporaries are chained on the corresponding list
5288 -- of actions. These temporaries need to be finalized after the if
5289 -- expression is evaluated.
5290
5291 Process_Actions (Then_Actions (N));
5292 Process_Actions (Else_Actions (N));
5293
5294 -- Generate:
5295 -- type Ann is access all Typ;
5296
5297 Ptr_Typ := Make_Temporary (Loc, 'A');
5298
5299 Insert_Action (N,
5300 Make_Full_Type_Declaration (Loc,
5301 Defining_Identifier => Ptr_Typ,
5302 Type_Definition =>
5303 Make_Access_To_Object_Definition (Loc,
5304 All_Present => True,
5305 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
5306
5307 -- Generate:
5308 -- Cnn : Ann;
5309
5310 Cnn := Make_Temporary (Loc, 'C', N);
5311
5312 Decl :=
5313 Make_Object_Declaration (Loc,
5314 Defining_Identifier => Cnn,
5315 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc));
5316
5317 -- Generate:
5318 -- if Cond then
5319 -- Cnn := <Thenx>'Unrestricted_Access;
5320 -- else
5321 -- Cnn := <Elsex>'Unrestricted_Access;
5322 -- end if;
5323
5324 New_If :=
5325 Make_Implicit_If_Statement (N,
5326 Condition => Relocate_Node (Cond),
5327 Then_Statements => New_List (
5328 Make_Assignment_Statement (Sloc (Thenx),
5329 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
5330 Expression =>
5331 Make_Attribute_Reference (Loc,
5332 Prefix => Relocate_Node (Thenx),
5333 Attribute_Name => Name_Unrestricted_Access))),
5334
5335 Else_Statements => New_List (
5336 Make_Assignment_Statement (Sloc (Elsex),
5337 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
5338 Expression =>
5339 Make_Attribute_Reference (Loc,
5340 Prefix => Relocate_Node (Elsex),
5341 Attribute_Name => Name_Unrestricted_Access))));
5342
5343 New_N :=
5344 Make_Explicit_Dereference (Loc,
5345 Prefix => New_Occurrence_Of (Cnn, Loc));
5346
5347 -- For other types, we only need to expand if there are other actions
5348 -- associated with either branch.
5349
5350 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
5351
5352 -- We now wrap the actions into the appropriate expression
5353
5354 if Present (Then_Actions (N)) then
5355 Rewrite (Thenx,
5356 Make_Expression_With_Actions (Sloc (Thenx),
5357 Actions => Then_Actions (N),
5358 Expression => Relocate_Node (Thenx)));
5359
5360 Set_Then_Actions (N, No_List);
5361 Analyze_And_Resolve (Thenx, Typ);
5362 end if;
5363
5364 if Present (Else_Actions (N)) then
5365 Rewrite (Elsex,
5366 Make_Expression_With_Actions (Sloc (Elsex),
5367 Actions => Else_Actions (N),
5368 Expression => Relocate_Node (Elsex)));
5369
5370 Set_Else_Actions (N, No_List);
5371 Analyze_And_Resolve (Elsex, Typ);
5372 end if;
5373
5374 return;
5375
5376 -- If no actions then no expansion needed, gigi will handle it using the
5377 -- same approach as a C conditional expression.
5378
5379 else
5380 return;
5381 end if;
5382
5383 -- Fall through here for either the limited expansion, or the case of
5384 -- inserting actions for non-limited types. In both these cases, we must
5385 -- move the SLOC of the parent If statement to the newly created one and
5386 -- change it to the SLOC of the expression which, after expansion, will
5387 -- correspond to what is being evaluated.
5388
5389 if Present (Parent (N)) and then Nkind (Parent (N)) = N_If_Statement then
5390 Set_Sloc (New_If, Sloc (Parent (N)));
5391 Set_Sloc (Parent (N), Loc);
5392 end if;
5393
5394 -- Make sure Then_Actions and Else_Actions are appropriately moved
5395 -- to the new if statement.
5396
5397 if Present (Then_Actions (N)) then
5398 Insert_List_Before
5399 (First (Then_Statements (New_If)), Then_Actions (N));
5400 end if;
5401
5402 if Present (Else_Actions (N)) then
5403 Insert_List_Before
5404 (First (Else_Statements (New_If)), Else_Actions (N));
5405 end if;
5406
5407 Insert_Action (N, Decl);
5408 Insert_Action (N, New_If);
5409 Rewrite (N, New_N);
5410 Analyze_And_Resolve (N, Typ);
5411 end Expand_N_If_Expression;
5412
5413 -----------------
5414 -- Expand_N_In --
5415 -----------------
5416
5417 procedure Expand_N_In (N : Node_Id) is
5418 Loc : constant Source_Ptr := Sloc (N);
5419 Restyp : constant Entity_Id := Etype (N);
5420 Lop : constant Node_Id := Left_Opnd (N);
5421 Rop : constant Node_Id := Right_Opnd (N);
5422 Static : constant Boolean := Is_OK_Static_Expression (N);
5423
5424 Ltyp : Entity_Id;
5425 Rtyp : Entity_Id;
5426
5427 procedure Substitute_Valid_Check;
5428 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5429 -- test for the left operand being in range of its subtype.
5430
5431 ----------------------------
5432 -- Substitute_Valid_Check --
5433 ----------------------------
5434
5435 procedure Substitute_Valid_Check is
5436 begin
5437 Rewrite (N,
5438 Make_Attribute_Reference (Loc,
5439 Prefix => Relocate_Node (Lop),
5440 Attribute_Name => Name_Valid));
5441
5442 Analyze_And_Resolve (N, Restyp);
5443
5444 -- Give warning unless overflow checking is MINIMIZED or ELIMINATED,
5445 -- in which case, this usage makes sense, and in any case, we have
5446 -- actually eliminated the danger of optimization above.
5447
5448 if Overflow_Check_Mode not in Minimized_Or_Eliminated then
5449 Error_Msg_N
5450 ("??explicit membership test may be optimized away", N);
5451 Error_Msg_N -- CODEFIX
5452 ("\??use ''Valid attribute instead", N);
5453 end if;
5454
5455 return;
5456 end Substitute_Valid_Check;
5457
5458 -- Start of processing for Expand_N_In
5459
5460 begin
5461 -- If set membership case, expand with separate procedure
5462
5463 if Present (Alternatives (N)) then
5464 Expand_Set_Membership (N);
5465 return;
5466 end if;
5467
5468 -- Not set membership, proceed with expansion
5469
5470 Ltyp := Etype (Left_Opnd (N));
5471 Rtyp := Etype (Right_Opnd (N));
5472
5473 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
5474 -- type, then expand with a separate procedure. Note the use of the
5475 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5476
5477 if Overflow_Check_Mode in Minimized_Or_Eliminated
5478 and then Is_Signed_Integer_Type (Ltyp)
5479 and then not No_Minimize_Eliminate (N)
5480 then
5481 Expand_Membership_Minimize_Eliminate_Overflow (N);
5482 return;
5483 end if;
5484
5485 -- Check case of explicit test for an expression in range of its
5486 -- subtype. This is suspicious usage and we replace it with a 'Valid
5487 -- test and give a warning for scalar types.
5488
5489 if Is_Scalar_Type (Ltyp)
5490
5491 -- Only relevant for source comparisons
5492
5493 and then Comes_From_Source (N)
5494
5495 -- In floating-point this is a standard way to check for finite values
5496 -- and using 'Valid would typically be a pessimization.
5497
5498 and then not Is_Floating_Point_Type (Ltyp)
5499
5500 -- Don't give the message unless right operand is a type entity and
5501 -- the type of the left operand matches this type. Note that this
5502 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
5503 -- checks have changed the type of the left operand.
5504
5505 and then Nkind (Rop) in N_Has_Entity
5506 and then Ltyp = Entity (Rop)
5507
5508 -- Skip in VM mode, where we have no sense of invalid values. The
5509 -- warning still seems relevant, but not important enough to worry.
5510
5511 and then VM_Target = No_VM
5512
5513 -- Skip this for predicated types, where such expressions are a
5514 -- reasonable way of testing if something meets the predicate.
5515
5516 and then not Present (Predicate_Function (Ltyp))
5517 then
5518 Substitute_Valid_Check;
5519 return;
5520 end if;
5521
5522 -- Do validity check on operands
5523
5524 if Validity_Checks_On and Validity_Check_Operands then
5525 Ensure_Valid (Left_Opnd (N));
5526 Validity_Check_Range (Right_Opnd (N));
5527 end if;
5528
5529 -- Case of explicit range
5530
5531 if Nkind (Rop) = N_Range then
5532 declare
5533 Lo : constant Node_Id := Low_Bound (Rop);
5534 Hi : constant Node_Id := High_Bound (Rop);
5535
5536 Lo_Orig : constant Node_Id := Original_Node (Lo);
5537 Hi_Orig : constant Node_Id := Original_Node (Hi);
5538
5539 Lcheck : Compare_Result;
5540 Ucheck : Compare_Result;
5541
5542 Warn1 : constant Boolean :=
5543 Constant_Condition_Warnings
5544 and then Comes_From_Source (N)
5545 and then not In_Instance;
5546 -- This must be true for any of the optimization warnings, we
5547 -- clearly want to give them only for source with the flag on. We
5548 -- also skip these warnings in an instance since it may be the
5549 -- case that different instantiations have different ranges.
5550
5551 Warn2 : constant Boolean :=
5552 Warn1
5553 and then Nkind (Original_Node (Rop)) = N_Range
5554 and then Is_Integer_Type (Etype (Lo));
5555 -- For the case where only one bound warning is elided, we also
5556 -- insist on an explicit range and an integer type. The reason is
5557 -- that the use of enumeration ranges including an end point is
5558 -- common, as is the use of a subtype name, one of whose bounds is
5559 -- the same as the type of the expression.
5560
5561 begin
5562 -- If test is explicit x'First .. x'Last, replace by valid check
5563
5564 -- Could use some individual comments for this complex test ???
5565
5566 if Is_Scalar_Type (Ltyp)
5567
5568 -- And left operand is X'First where X matches left operand
5569 -- type (this eliminates cases of type mismatch, including
5570 -- the cases where ELIMINATED/MINIMIZED mode has changed the
5571 -- type of the left operand.
5572
5573 and then Nkind (Lo_Orig) = N_Attribute_Reference
5574 and then Attribute_Name (Lo_Orig) = Name_First
5575 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
5576 and then Entity (Prefix (Lo_Orig)) = Ltyp
5577
5578 -- Same tests for right operand
5579
5580 and then Nkind (Hi_Orig) = N_Attribute_Reference
5581 and then Attribute_Name (Hi_Orig) = Name_Last
5582 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
5583 and then Entity (Prefix (Hi_Orig)) = Ltyp
5584
5585 -- Relevant only for source cases
5586
5587 and then Comes_From_Source (N)
5588
5589 -- Omit for VM cases, where we don't have invalid values
5590
5591 and then VM_Target = No_VM
5592 then
5593 Substitute_Valid_Check;
5594 goto Leave;
5595 end if;
5596
5597 -- If bounds of type are known at compile time, and the end points
5598 -- are known at compile time and identical, this is another case
5599 -- for substituting a valid test. We only do this for discrete
5600 -- types, since it won't arise in practice for float types.
5601
5602 if Comes_From_Source (N)
5603 and then Is_Discrete_Type (Ltyp)
5604 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
5605 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
5606 and then Compile_Time_Known_Value (Lo)
5607 and then Compile_Time_Known_Value (Hi)
5608 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
5609 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
5610
5611 -- Kill warnings in instances, since they may be cases where we
5612 -- have a test in the generic that makes sense with some types
5613 -- and not with other types.
5614
5615 and then not In_Instance
5616 then
5617 Substitute_Valid_Check;
5618 goto Leave;
5619 end if;
5620
5621 -- If we have an explicit range, do a bit of optimization based on
5622 -- range analysis (we may be able to kill one or both checks).
5623
5624 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
5625 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
5626
5627 -- If either check is known to fail, replace result by False since
5628 -- the other check does not matter. Preserve the static flag for
5629 -- legality checks, because we are constant-folding beyond RM 4.9.
5630
5631 if Lcheck = LT or else Ucheck = GT then
5632 if Warn1 then
5633 Error_Msg_N ("?c?range test optimized away", N);
5634 Error_Msg_N ("\?c?value is known to be out of range", N);
5635 end if;
5636
5637 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5638 Analyze_And_Resolve (N, Restyp);
5639 Set_Is_Static_Expression (N, Static);
5640 goto Leave;
5641
5642 -- If both checks are known to succeed, replace result by True,
5643 -- since we know we are in range.
5644
5645 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
5646 if Warn1 then
5647 Error_Msg_N ("?c?range test optimized away", N);
5648 Error_Msg_N ("\?c?value is known to be in range", N);
5649 end if;
5650
5651 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5652 Analyze_And_Resolve (N, Restyp);
5653 Set_Is_Static_Expression (N, Static);
5654 goto Leave;
5655
5656 -- If lower bound check succeeds and upper bound check is not
5657 -- known to succeed or fail, then replace the range check with
5658 -- a comparison against the upper bound.
5659
5660 elsif Lcheck in Compare_GE then
5661 if Warn2 and then not In_Instance then
5662 Error_Msg_N ("??lower bound test optimized away", Lo);
5663 Error_Msg_N ("\??value is known to be in range", Lo);
5664 end if;
5665
5666 Rewrite (N,
5667 Make_Op_Le (Loc,
5668 Left_Opnd => Lop,
5669 Right_Opnd => High_Bound (Rop)));
5670 Analyze_And_Resolve (N, Restyp);
5671 goto Leave;
5672
5673 -- If upper bound check succeeds and lower bound check is not
5674 -- known to succeed or fail, then replace the range check with
5675 -- a comparison against the lower bound.
5676
5677 elsif Ucheck in Compare_LE then
5678 if Warn2 and then not In_Instance then
5679 Error_Msg_N ("??upper bound test optimized away", Hi);
5680 Error_Msg_N ("\??value is known to be in range", Hi);
5681 end if;
5682
5683 Rewrite (N,
5684 Make_Op_Ge (Loc,
5685 Left_Opnd => Lop,
5686 Right_Opnd => Low_Bound (Rop)));
5687 Analyze_And_Resolve (N, Restyp);
5688 goto Leave;
5689 end if;
5690
5691 -- We couldn't optimize away the range check, but there is one
5692 -- more issue. If we are checking constant conditionals, then we
5693 -- see if we can determine the outcome assuming everything is
5694 -- valid, and if so give an appropriate warning.
5695
5696 if Warn1 and then not Assume_No_Invalid_Values then
5697 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
5698 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
5699
5700 -- Result is out of range for valid value
5701
5702 if Lcheck = LT or else Ucheck = GT then
5703 Error_Msg_N
5704 ("?c?value can only be in range if it is invalid", N);
5705
5706 -- Result is in range for valid value
5707
5708 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
5709 Error_Msg_N
5710 ("?c?value can only be out of range if it is invalid", N);
5711
5712 -- Lower bound check succeeds if value is valid
5713
5714 elsif Warn2 and then Lcheck in Compare_GE then
5715 Error_Msg_N
5716 ("?c?lower bound check only fails if it is invalid", Lo);
5717
5718 -- Upper bound check succeeds if value is valid
5719
5720 elsif Warn2 and then Ucheck in Compare_LE then
5721 Error_Msg_N
5722 ("?c?upper bound check only fails for invalid values", Hi);
5723 end if;
5724 end if;
5725 end;
5726
5727 -- For all other cases of an explicit range, nothing to be done
5728
5729 goto Leave;
5730
5731 -- Here right operand is a subtype mark
5732
5733 else
5734 declare
5735 Typ : Entity_Id := Etype (Rop);
5736 Is_Acc : constant Boolean := Is_Access_Type (Typ);
5737 Cond : Node_Id := Empty;
5738 New_N : Node_Id;
5739 Obj : Node_Id := Lop;
5740 SCIL_Node : Node_Id;
5741
5742 begin
5743 Remove_Side_Effects (Obj);
5744
5745 -- For tagged type, do tagged membership operation
5746
5747 if Is_Tagged_Type (Typ) then
5748
5749 -- No expansion will be performed when VM_Target, as the VM
5750 -- back-ends will handle the membership tests directly (tags
5751 -- are not explicitly represented in Java objects, so the
5752 -- normal tagged membership expansion is not what we want).
5753
5754 if Tagged_Type_Expansion then
5755 Tagged_Membership (N, SCIL_Node, New_N);
5756 Rewrite (N, New_N);
5757 Analyze_And_Resolve (N, Restyp);
5758
5759 -- Update decoration of relocated node referenced by the
5760 -- SCIL node.
5761
5762 if Generate_SCIL and then Present (SCIL_Node) then
5763 Set_SCIL_Node (N, SCIL_Node);
5764 end if;
5765 end if;
5766
5767 goto Leave;
5768
5769 -- If type is scalar type, rewrite as x in t'First .. t'Last.
5770 -- This reason we do this is that the bounds may have the wrong
5771 -- type if they come from the original type definition. Also this
5772 -- way we get all the processing above for an explicit range.
5773
5774 -- Don't do this for predicated types, since in this case we
5775 -- want to check the predicate.
5776
5777 elsif Is_Scalar_Type (Typ) then
5778 if No (Predicate_Function (Typ)) then
5779 Rewrite (Rop,
5780 Make_Range (Loc,
5781 Low_Bound =>
5782 Make_Attribute_Reference (Loc,
5783 Attribute_Name => Name_First,
5784 Prefix => New_Occurrence_Of (Typ, Loc)),
5785
5786 High_Bound =>
5787 Make_Attribute_Reference (Loc,
5788 Attribute_Name => Name_Last,
5789 Prefix => New_Occurrence_Of (Typ, Loc))));
5790 Analyze_And_Resolve (N, Restyp);
5791 end if;
5792
5793 goto Leave;
5794
5795 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5796 -- a membership test if the subtype mark denotes a constrained
5797 -- Unchecked_Union subtype and the expression lacks inferable
5798 -- discriminants.
5799
5800 elsif Is_Unchecked_Union (Base_Type (Typ))
5801 and then Is_Constrained (Typ)
5802 and then not Has_Inferable_Discriminants (Lop)
5803 then
5804 Insert_Action (N,
5805 Make_Raise_Program_Error (Loc,
5806 Reason => PE_Unchecked_Union_Restriction));
5807
5808 -- Prevent Gigi from generating incorrect code by rewriting the
5809 -- test as False. What is this undocumented thing about ???
5810
5811 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5812 goto Leave;
5813 end if;
5814
5815 -- Here we have a non-scalar type
5816
5817 if Is_Acc then
5818 Typ := Designated_Type (Typ);
5819 end if;
5820
5821 if not Is_Constrained (Typ) then
5822 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5823 Analyze_And_Resolve (N, Restyp);
5824
5825 -- For the constrained array case, we have to check the subscripts
5826 -- for an exact match if the lengths are non-zero (the lengths
5827 -- must match in any case).
5828
5829 elsif Is_Array_Type (Typ) then
5830 Check_Subscripts : declare
5831 function Build_Attribute_Reference
5832 (E : Node_Id;
5833 Nam : Name_Id;
5834 Dim : Nat) return Node_Id;
5835 -- Build attribute reference E'Nam (Dim)
5836
5837 -------------------------------
5838 -- Build_Attribute_Reference --
5839 -------------------------------
5840
5841 function Build_Attribute_Reference
5842 (E : Node_Id;
5843 Nam : Name_Id;
5844 Dim : Nat) return Node_Id
5845 is
5846 begin
5847 return
5848 Make_Attribute_Reference (Loc,
5849 Prefix => E,
5850 Attribute_Name => Nam,
5851 Expressions => New_List (
5852 Make_Integer_Literal (Loc, Dim)));
5853 end Build_Attribute_Reference;
5854
5855 -- Start of processing for Check_Subscripts
5856
5857 begin
5858 for J in 1 .. Number_Dimensions (Typ) loop
5859 Evolve_And_Then (Cond,
5860 Make_Op_Eq (Loc,
5861 Left_Opnd =>
5862 Build_Attribute_Reference
5863 (Duplicate_Subexpr_No_Checks (Obj),
5864 Name_First, J),
5865 Right_Opnd =>
5866 Build_Attribute_Reference
5867 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
5868
5869 Evolve_And_Then (Cond,
5870 Make_Op_Eq (Loc,
5871 Left_Opnd =>
5872 Build_Attribute_Reference
5873 (Duplicate_Subexpr_No_Checks (Obj),
5874 Name_Last, J),
5875 Right_Opnd =>
5876 Build_Attribute_Reference
5877 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
5878 end loop;
5879
5880 if Is_Acc then
5881 Cond :=
5882 Make_Or_Else (Loc,
5883 Left_Opnd =>
5884 Make_Op_Eq (Loc,
5885 Left_Opnd => Obj,
5886 Right_Opnd => Make_Null (Loc)),
5887 Right_Opnd => Cond);
5888 end if;
5889
5890 Rewrite (N, Cond);
5891 Analyze_And_Resolve (N, Restyp);
5892 end Check_Subscripts;
5893
5894 -- These are the cases where constraint checks may be required,
5895 -- e.g. records with possible discriminants
5896
5897 else
5898 -- Expand the test into a series of discriminant comparisons.
5899 -- The expression that is built is the negation of the one that
5900 -- is used for checking discriminant constraints.
5901
5902 Obj := Relocate_Node (Left_Opnd (N));
5903
5904 if Has_Discriminants (Typ) then
5905 Cond := Make_Op_Not (Loc,
5906 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
5907
5908 if Is_Acc then
5909 Cond := Make_Or_Else (Loc,
5910 Left_Opnd =>
5911 Make_Op_Eq (Loc,
5912 Left_Opnd => Obj,
5913 Right_Opnd => Make_Null (Loc)),
5914 Right_Opnd => Cond);
5915 end if;
5916
5917 else
5918 Cond := New_Occurrence_Of (Standard_True, Loc);
5919 end if;
5920
5921 Rewrite (N, Cond);
5922 Analyze_And_Resolve (N, Restyp);
5923 end if;
5924
5925 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
5926 -- expression of an anonymous access type. This can involve an
5927 -- accessibility test and a tagged type membership test in the
5928 -- case of tagged designated types.
5929
5930 if Ada_Version >= Ada_2012
5931 and then Is_Acc
5932 and then Ekind (Ltyp) = E_Anonymous_Access_Type
5933 then
5934 declare
5935 Expr_Entity : Entity_Id := Empty;
5936 New_N : Node_Id;
5937 Param_Level : Node_Id;
5938 Type_Level : Node_Id;
5939
5940 begin
5941 if Is_Entity_Name (Lop) then
5942 Expr_Entity := Param_Entity (Lop);
5943
5944 if not Present (Expr_Entity) then
5945 Expr_Entity := Entity (Lop);
5946 end if;
5947 end if;
5948
5949 -- If a conversion of the anonymous access value to the
5950 -- tested type would be illegal, then the result is False.
5951
5952 if not Valid_Conversion
5953 (Lop, Rtyp, Lop, Report_Errs => False)
5954 then
5955 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5956 Analyze_And_Resolve (N, Restyp);
5957
5958 -- Apply an accessibility check if the access object has an
5959 -- associated access level and when the level of the type is
5960 -- less deep than the level of the access parameter. This
5961 -- only occur for access parameters and stand-alone objects
5962 -- of an anonymous access type.
5963
5964 else
5965 if Present (Expr_Entity)
5966 and then
5967 Present
5968 (Effective_Extra_Accessibility (Expr_Entity))
5969 and then UI_Gt (Object_Access_Level (Lop),
5970 Type_Access_Level (Rtyp))
5971 then
5972 Param_Level :=
5973 New_Occurrence_Of
5974 (Effective_Extra_Accessibility (Expr_Entity), Loc);
5975
5976 Type_Level :=
5977 Make_Integer_Literal (Loc, Type_Access_Level (Rtyp));
5978
5979 -- Return True only if the accessibility level of the
5980 -- expression entity is not deeper than the level of
5981 -- the tested access type.
5982
5983 Rewrite (N,
5984 Make_And_Then (Loc,
5985 Left_Opnd => Relocate_Node (N),
5986 Right_Opnd => Make_Op_Le (Loc,
5987 Left_Opnd => Param_Level,
5988 Right_Opnd => Type_Level)));
5989
5990 Analyze_And_Resolve (N);
5991 end if;
5992
5993 -- If the designated type is tagged, do tagged membership
5994 -- operation.
5995
5996 -- *** NOTE: we have to check not null before doing the
5997 -- tagged membership test (but maybe that can be done
5998 -- inside Tagged_Membership?).
5999
6000 if Is_Tagged_Type (Typ) then
6001 Rewrite (N,
6002 Make_And_Then (Loc,
6003 Left_Opnd => Relocate_Node (N),
6004 Right_Opnd =>
6005 Make_Op_Ne (Loc,
6006 Left_Opnd => Obj,
6007 Right_Opnd => Make_Null (Loc))));
6008
6009 -- No expansion will be performed when VM_Target, as
6010 -- the VM back-ends will handle the membership tests
6011 -- directly (tags are not explicitly represented in
6012 -- Java objects, so the normal tagged membership
6013 -- expansion is not what we want).
6014
6015 if Tagged_Type_Expansion then
6016
6017 -- Note that we have to pass Original_Node, because
6018 -- the membership test might already have been
6019 -- rewritten by earlier parts of membership test.
6020
6021 Tagged_Membership
6022 (Original_Node (N), SCIL_Node, New_N);
6023
6024 -- Update decoration of relocated node referenced
6025 -- by the SCIL node.
6026
6027 if Generate_SCIL and then Present (SCIL_Node) then
6028 Set_SCIL_Node (New_N, SCIL_Node);
6029 end if;
6030
6031 Rewrite (N,
6032 Make_And_Then (Loc,
6033 Left_Opnd => Relocate_Node (N),
6034 Right_Opnd => New_N));
6035
6036 Analyze_And_Resolve (N, Restyp);
6037 end if;
6038 end if;
6039 end if;
6040 end;
6041 end if;
6042 end;
6043 end if;
6044
6045 -- At this point, we have done the processing required for the basic
6046 -- membership test, but not yet dealt with the predicate.
6047
6048 <<Leave>>
6049
6050 -- If a predicate is present, then we do the predicate test, but we
6051 -- most certainly want to omit this if we are within the predicate
6052 -- function itself, since otherwise we have an infinite recursion.
6053 -- The check should also not be emitted when testing against a range
6054 -- (the check is only done when the right operand is a subtype; see
6055 -- RM12-4.5.2 (28.1/3-30/3)).
6056
6057 declare
6058 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
6059
6060 begin
6061 if Present (PFunc)
6062 and then Current_Scope /= PFunc
6063 and then Nkind (Rop) /= N_Range
6064 then
6065 Rewrite (N,
6066 Make_And_Then (Loc,
6067 Left_Opnd => Relocate_Node (N),
6068 Right_Opnd => Make_Predicate_Call (Rtyp, Lop, Mem => True)));
6069
6070 -- Analyze new expression, mark left operand as analyzed to
6071 -- avoid infinite recursion adding predicate calls. Similarly,
6072 -- suppress further range checks on the call.
6073
6074 Set_Analyzed (Left_Opnd (N));
6075 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6076
6077 -- All done, skip attempt at compile time determination of result
6078
6079 return;
6080 end if;
6081 end;
6082 end Expand_N_In;
6083
6084 --------------------------------
6085 -- Expand_N_Indexed_Component --
6086 --------------------------------
6087
6088 procedure Expand_N_Indexed_Component (N : Node_Id) is
6089 Loc : constant Source_Ptr := Sloc (N);
6090 Typ : constant Entity_Id := Etype (N);
6091 P : constant Node_Id := Prefix (N);
6092 T : constant Entity_Id := Etype (P);
6093 Atp : Entity_Id;
6094
6095 begin
6096 -- A special optimization, if we have an indexed component that is
6097 -- selecting from a slice, then we can eliminate the slice, since, for
6098 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6099 -- the range check required by the slice. The range check for the slice
6100 -- itself has already been generated. The range check for the
6101 -- subscripting operation is ensured by converting the subject to
6102 -- the subtype of the slice.
6103
6104 -- This optimization not only generates better code, avoiding slice
6105 -- messing especially in the packed case, but more importantly bypasses
6106 -- some problems in handling this peculiar case, for example, the issue
6107 -- of dealing specially with object renamings.
6108
6109 if Nkind (P) = N_Slice then
6110 Rewrite (N,
6111 Make_Indexed_Component (Loc,
6112 Prefix => Prefix (P),
6113 Expressions => New_List (
6114 Convert_To
6115 (Etype (First_Index (Etype (P))),
6116 First (Expressions (N))))));
6117 Analyze_And_Resolve (N, Typ);
6118 return;
6119 end if;
6120
6121 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6122 -- function, then additional actuals must be passed.
6123
6124 if Ada_Version >= Ada_2005
6125 and then Is_Build_In_Place_Function_Call (P)
6126 then
6127 Make_Build_In_Place_Call_In_Anonymous_Context (P);
6128 end if;
6129
6130 -- If the prefix is an access type, then we unconditionally rewrite if
6131 -- as an explicit dereference. This simplifies processing for several
6132 -- cases, including packed array cases and certain cases in which checks
6133 -- must be generated. We used to try to do this only when it was
6134 -- necessary, but it cleans up the code to do it all the time.
6135
6136 if Is_Access_Type (T) then
6137 Insert_Explicit_Dereference (P);
6138 Analyze_And_Resolve (P, Designated_Type (T));
6139 Atp := Designated_Type (T);
6140 else
6141 Atp := T;
6142 end if;
6143
6144 -- Generate index and validity checks
6145
6146 Generate_Index_Checks (N);
6147
6148 if Validity_Checks_On and then Validity_Check_Subscripts then
6149 Apply_Subscript_Validity_Checks (N);
6150 end if;
6151
6152 -- If selecting from an array with atomic components, and atomic sync
6153 -- is not suppressed for this array type, set atomic sync flag.
6154
6155 if (Has_Atomic_Components (Atp)
6156 and then not Atomic_Synchronization_Disabled (Atp))
6157 or else (Is_Atomic (Typ)
6158 and then not Atomic_Synchronization_Disabled (Typ))
6159 then
6160 Activate_Atomic_Synchronization (N);
6161 end if;
6162
6163 -- All done for the non-packed case
6164
6165 if not Is_Packed (Etype (Prefix (N))) then
6166 return;
6167 end if;
6168
6169 -- For packed arrays that are not bit-packed (i.e. the case of an array
6170 -- with one or more index types with a non-contiguous enumeration type),
6171 -- we can always use the normal packed element get circuit.
6172
6173 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
6174 Expand_Packed_Element_Reference (N);
6175 return;
6176 end if;
6177
6178 -- For a reference to a component of a bit packed array, we have to
6179 -- convert it to a reference to the corresponding Packed_Array_Type.
6180 -- We only want to do this for simple references, and not for:
6181
6182 -- Left side of assignment, or prefix of left side of assignment, or
6183 -- prefix of the prefix, to handle packed arrays of packed arrays,
6184 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6185
6186 -- Renaming objects in renaming associations
6187 -- This case is handled when a use of the renamed variable occurs
6188
6189 -- Actual parameters for a procedure call
6190 -- This case is handled in Exp_Ch6.Expand_Actuals
6191
6192 -- The second expression in a 'Read attribute reference
6193
6194 -- The prefix of an address or bit or size attribute reference
6195
6196 -- The following circuit detects these exceptions
6197
6198 declare
6199 Child : Node_Id := N;
6200 Parnt : Node_Id := Parent (N);
6201
6202 begin
6203 loop
6204 if Nkind (Parnt) = N_Unchecked_Expression then
6205 null;
6206
6207 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
6208 N_Procedure_Call_Statement)
6209 or else (Nkind (Parnt) = N_Parameter_Association
6210 and then
6211 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
6212 then
6213 return;
6214
6215 elsif Nkind (Parnt) = N_Attribute_Reference
6216 and then Nam_In (Attribute_Name (Parnt), Name_Address,
6217 Name_Bit,
6218 Name_Size)
6219 and then Prefix (Parnt) = Child
6220 then
6221 return;
6222
6223 elsif Nkind (Parnt) = N_Assignment_Statement
6224 and then Name (Parnt) = Child
6225 then
6226 return;
6227
6228 -- If the expression is an index of an indexed component, it must
6229 -- be expanded regardless of context.
6230
6231 elsif Nkind (Parnt) = N_Indexed_Component
6232 and then Child /= Prefix (Parnt)
6233 then
6234 Expand_Packed_Element_Reference (N);
6235 return;
6236
6237 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
6238 and then Name (Parent (Parnt)) = Parnt
6239 then
6240 return;
6241
6242 elsif Nkind (Parnt) = N_Attribute_Reference
6243 and then Attribute_Name (Parnt) = Name_Read
6244 and then Next (First (Expressions (Parnt))) = Child
6245 then
6246 return;
6247
6248 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
6249 and then Prefix (Parnt) = Child
6250 then
6251 null;
6252
6253 else
6254 Expand_Packed_Element_Reference (N);
6255 return;
6256 end if;
6257
6258 -- Keep looking up tree for unchecked expression, or if we are the
6259 -- prefix of a possible assignment left side.
6260
6261 Child := Parnt;
6262 Parnt := Parent (Child);
6263 end loop;
6264 end;
6265 end Expand_N_Indexed_Component;
6266
6267 ---------------------
6268 -- Expand_N_Not_In --
6269 ---------------------
6270
6271 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6272 -- can be done. This avoids needing to duplicate this expansion code.
6273
6274 procedure Expand_N_Not_In (N : Node_Id) is
6275 Loc : constant Source_Ptr := Sloc (N);
6276 Typ : constant Entity_Id := Etype (N);
6277 Cfs : constant Boolean := Comes_From_Source (N);
6278
6279 begin
6280 Rewrite (N,
6281 Make_Op_Not (Loc,
6282 Right_Opnd =>
6283 Make_In (Loc,
6284 Left_Opnd => Left_Opnd (N),
6285 Right_Opnd => Right_Opnd (N))));
6286
6287 -- If this is a set membership, preserve list of alternatives
6288
6289 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
6290
6291 -- We want this to appear as coming from source if original does (see
6292 -- transformations in Expand_N_In).
6293
6294 Set_Comes_From_Source (N, Cfs);
6295 Set_Comes_From_Source (Right_Opnd (N), Cfs);
6296
6297 -- Now analyze transformed node
6298
6299 Analyze_And_Resolve (N, Typ);
6300 end Expand_N_Not_In;
6301
6302 -------------------
6303 -- Expand_N_Null --
6304 -------------------
6305
6306 -- The only replacement required is for the case of a null of a type that
6307 -- is an access to protected subprogram, or a subtype thereof. We represent
6308 -- such access values as a record, and so we must replace the occurrence of
6309 -- null by the equivalent record (with a null address and a null pointer in
6310 -- it), so that the backend creates the proper value.
6311
6312 procedure Expand_N_Null (N : Node_Id) is
6313 Loc : constant Source_Ptr := Sloc (N);
6314 Typ : constant Entity_Id := Base_Type (Etype (N));
6315 Agg : Node_Id;
6316
6317 begin
6318 if Is_Access_Protected_Subprogram_Type (Typ) then
6319 Agg :=
6320 Make_Aggregate (Loc,
6321 Expressions => New_List (
6322 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
6323 Make_Null (Loc)));
6324
6325 Rewrite (N, Agg);
6326 Analyze_And_Resolve (N, Equivalent_Type (Typ));
6327
6328 -- For subsequent semantic analysis, the node must retain its type.
6329 -- Gigi in any case replaces this type by the corresponding record
6330 -- type before processing the node.
6331
6332 Set_Etype (N, Typ);
6333 end if;
6334
6335 exception
6336 when RE_Not_Available =>
6337 return;
6338 end Expand_N_Null;
6339
6340 ---------------------
6341 -- Expand_N_Op_Abs --
6342 ---------------------
6343
6344 procedure Expand_N_Op_Abs (N : Node_Id) is
6345 Loc : constant Source_Ptr := Sloc (N);
6346 Expr : constant Node_Id := Right_Opnd (N);
6347
6348 begin
6349 Unary_Op_Validity_Checks (N);
6350
6351 -- Check for MINIMIZED/ELIMINATED overflow mode
6352
6353 if Minimized_Eliminated_Overflow_Check (N) then
6354 Apply_Arithmetic_Overflow_Check (N);
6355 return;
6356 end if;
6357
6358 -- Deal with software overflow checking
6359
6360 if not Backend_Overflow_Checks_On_Target
6361 and then Is_Signed_Integer_Type (Etype (N))
6362 and then Do_Overflow_Check (N)
6363 then
6364 -- The only case to worry about is when the argument is equal to the
6365 -- largest negative number, so what we do is to insert the check:
6366
6367 -- [constraint_error when Expr = typ'Base'First]
6368
6369 -- with the usual Duplicate_Subexpr use coding for expr
6370
6371 Insert_Action (N,
6372 Make_Raise_Constraint_Error (Loc,
6373 Condition =>
6374 Make_Op_Eq (Loc,
6375 Left_Opnd => Duplicate_Subexpr (Expr),
6376 Right_Opnd =>
6377 Make_Attribute_Reference (Loc,
6378 Prefix =>
6379 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
6380 Attribute_Name => Name_First)),
6381 Reason => CE_Overflow_Check_Failed));
6382 end if;
6383
6384 -- Vax floating-point types case
6385
6386 if Vax_Float (Etype (N)) then
6387 Expand_Vax_Arith (N);
6388 end if;
6389 end Expand_N_Op_Abs;
6390
6391 ---------------------
6392 -- Expand_N_Op_Add --
6393 ---------------------
6394
6395 procedure Expand_N_Op_Add (N : Node_Id) is
6396 Typ : constant Entity_Id := Etype (N);
6397
6398 begin
6399 Binary_Op_Validity_Checks (N);
6400
6401 -- Check for MINIMIZED/ELIMINATED overflow mode
6402
6403 if Minimized_Eliminated_Overflow_Check (N) then
6404 Apply_Arithmetic_Overflow_Check (N);
6405 return;
6406 end if;
6407
6408 -- N + 0 = 0 + N = N for integer types
6409
6410 if Is_Integer_Type (Typ) then
6411 if Compile_Time_Known_Value (Right_Opnd (N))
6412 and then Expr_Value (Right_Opnd (N)) = Uint_0
6413 then
6414 Rewrite (N, Left_Opnd (N));
6415 return;
6416
6417 elsif Compile_Time_Known_Value (Left_Opnd (N))
6418 and then Expr_Value (Left_Opnd (N)) = Uint_0
6419 then
6420 Rewrite (N, Right_Opnd (N));
6421 return;
6422 end if;
6423 end if;
6424
6425 -- Arithmetic overflow checks for signed integer/fixed point types
6426
6427 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
6428 Apply_Arithmetic_Overflow_Check (N);
6429 return;
6430
6431 -- Vax floating-point types case
6432
6433 elsif Vax_Float (Typ) then
6434 Expand_Vax_Arith (N);
6435 end if;
6436 end Expand_N_Op_Add;
6437
6438 ---------------------
6439 -- Expand_N_Op_And --
6440 ---------------------
6441
6442 procedure Expand_N_Op_And (N : Node_Id) is
6443 Typ : constant Entity_Id := Etype (N);
6444
6445 begin
6446 Binary_Op_Validity_Checks (N);
6447
6448 if Is_Array_Type (Etype (N)) then
6449 Expand_Boolean_Operator (N);
6450
6451 elsif Is_Boolean_Type (Etype (N)) then
6452 Adjust_Condition (Left_Opnd (N));
6453 Adjust_Condition (Right_Opnd (N));
6454 Set_Etype (N, Standard_Boolean);
6455 Adjust_Result_Type (N, Typ);
6456
6457 elsif Is_Intrinsic_Subprogram (Entity (N)) then
6458 Expand_Intrinsic_Call (N, Entity (N));
6459
6460 end if;
6461 end Expand_N_Op_And;
6462
6463 ------------------------
6464 -- Expand_N_Op_Concat --
6465 ------------------------
6466
6467 procedure Expand_N_Op_Concat (N : Node_Id) is
6468 Opnds : List_Id;
6469 -- List of operands to be concatenated
6470
6471 Cnode : Node_Id;
6472 -- Node which is to be replaced by the result of concatenating the nodes
6473 -- in the list Opnds.
6474
6475 begin
6476 -- Ensure validity of both operands
6477
6478 Binary_Op_Validity_Checks (N);
6479
6480 -- If we are the left operand of a concatenation higher up the tree,
6481 -- then do nothing for now, since we want to deal with a series of
6482 -- concatenations as a unit.
6483
6484 if Nkind (Parent (N)) = N_Op_Concat
6485 and then N = Left_Opnd (Parent (N))
6486 then
6487 return;
6488 end if;
6489
6490 -- We get here with a concatenation whose left operand may be a
6491 -- concatenation itself with a consistent type. We need to process
6492 -- these concatenation operands from left to right, which means
6493 -- from the deepest node in the tree to the highest node.
6494
6495 Cnode := N;
6496 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
6497 Cnode := Left_Opnd (Cnode);
6498 end loop;
6499
6500 -- Now Cnode is the deepest concatenation, and its parents are the
6501 -- concatenation nodes above, so now we process bottom up, doing the
6502 -- operands.
6503
6504 -- The outer loop runs more than once if more than one concatenation
6505 -- type is involved.
6506
6507 Outer : loop
6508 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
6509 Set_Parent (Opnds, N);
6510
6511 -- The inner loop gathers concatenation operands
6512
6513 Inner : while Cnode /= N
6514 and then Base_Type (Etype (Cnode)) =
6515 Base_Type (Etype (Parent (Cnode)))
6516 loop
6517 Cnode := Parent (Cnode);
6518 Append (Right_Opnd (Cnode), Opnds);
6519 end loop Inner;
6520
6521 Expand_Concatenate (Cnode, Opnds);
6522
6523 exit Outer when Cnode = N;
6524 Cnode := Parent (Cnode);
6525 end loop Outer;
6526 end Expand_N_Op_Concat;
6527
6528 ------------------------
6529 -- Expand_N_Op_Divide --
6530 ------------------------
6531
6532 procedure Expand_N_Op_Divide (N : Node_Id) is
6533 Loc : constant Source_Ptr := Sloc (N);
6534 Lopnd : constant Node_Id := Left_Opnd (N);
6535 Ropnd : constant Node_Id := Right_Opnd (N);
6536 Ltyp : constant Entity_Id := Etype (Lopnd);
6537 Rtyp : constant Entity_Id := Etype (Ropnd);
6538 Typ : Entity_Id := Etype (N);
6539 Rknow : constant Boolean := Is_Integer_Type (Typ)
6540 and then
6541 Compile_Time_Known_Value (Ropnd);
6542 Rval : Uint;
6543
6544 begin
6545 Binary_Op_Validity_Checks (N);
6546
6547 -- Check for MINIMIZED/ELIMINATED overflow mode
6548
6549 if Minimized_Eliminated_Overflow_Check (N) then
6550 Apply_Arithmetic_Overflow_Check (N);
6551 return;
6552 end if;
6553
6554 -- Otherwise proceed with expansion of division
6555
6556 if Rknow then
6557 Rval := Expr_Value (Ropnd);
6558 end if;
6559
6560 -- N / 1 = N for integer types
6561
6562 if Rknow and then Rval = Uint_1 then
6563 Rewrite (N, Lopnd);
6564 return;
6565 end if;
6566
6567 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
6568 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6569 -- operand is an unsigned integer, as required for this to work.
6570
6571 if Nkind (Ropnd) = N_Op_Expon
6572 and then Is_Power_Of_2_For_Shift (Ropnd)
6573
6574 -- We cannot do this transformation in configurable run time mode if we
6575 -- have 64-bit integers and long shifts are not available.
6576
6577 and then (Esize (Ltyp) <= 32 or else Support_Long_Shifts_On_Target)
6578 then
6579 Rewrite (N,
6580 Make_Op_Shift_Right (Loc,
6581 Left_Opnd => Lopnd,
6582 Right_Opnd =>
6583 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
6584 Analyze_And_Resolve (N, Typ);
6585 return;
6586 end if;
6587
6588 -- Do required fixup of universal fixed operation
6589
6590 if Typ = Universal_Fixed then
6591 Fixup_Universal_Fixed_Operation (N);
6592 Typ := Etype (N);
6593 end if;
6594
6595 -- Divisions with fixed-point results
6596
6597 if Is_Fixed_Point_Type (Typ) then
6598
6599 -- No special processing if Treat_Fixed_As_Integer is set, since
6600 -- from a semantic point of view such operations are simply integer
6601 -- operations and will be treated that way.
6602
6603 if not Treat_Fixed_As_Integer (N) then
6604 if Is_Integer_Type (Rtyp) then
6605 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
6606 else
6607 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
6608 end if;
6609 end if;
6610
6611 -- Other cases of division of fixed-point operands. Again we exclude the
6612 -- case where Treat_Fixed_As_Integer is set.
6613
6614 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6615 and then not Treat_Fixed_As_Integer (N)
6616 then
6617 if Is_Integer_Type (Typ) then
6618 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
6619 else
6620 pragma Assert (Is_Floating_Point_Type (Typ));
6621 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
6622 end if;
6623
6624 -- Mixed-mode operations can appear in a non-static universal context,
6625 -- in which case the integer argument must be converted explicitly.
6626
6627 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
6628 Rewrite (Ropnd,
6629 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
6630
6631 Analyze_And_Resolve (Ropnd, Universal_Real);
6632
6633 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
6634 Rewrite (Lopnd,
6635 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
6636
6637 Analyze_And_Resolve (Lopnd, Universal_Real);
6638
6639 -- Non-fixed point cases, do integer zero divide and overflow checks
6640
6641 elsif Is_Integer_Type (Typ) then
6642 Apply_Divide_Checks (N);
6643
6644 -- Deal with Vax_Float
6645
6646 elsif Vax_Float (Typ) then
6647 Expand_Vax_Arith (N);
6648 return;
6649 end if;
6650 end Expand_N_Op_Divide;
6651
6652 --------------------
6653 -- Expand_N_Op_Eq --
6654 --------------------
6655
6656 procedure Expand_N_Op_Eq (N : Node_Id) is
6657 Loc : constant Source_Ptr := Sloc (N);
6658 Typ : constant Entity_Id := Etype (N);
6659 Lhs : constant Node_Id := Left_Opnd (N);
6660 Rhs : constant Node_Id := Right_Opnd (N);
6661 Bodies : constant List_Id := New_List;
6662 A_Typ : constant Entity_Id := Etype (Lhs);
6663
6664 Typl : Entity_Id := A_Typ;
6665 Op_Name : Entity_Id;
6666 Prim : Elmt_Id;
6667
6668 procedure Build_Equality_Call (Eq : Entity_Id);
6669 -- If a constructed equality exists for the type or for its parent,
6670 -- build and analyze call, adding conversions if the operation is
6671 -- inherited.
6672
6673 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
6674 -- Determines whether a type has a subcomponent of an unconstrained
6675 -- Unchecked_Union subtype. Typ is a record type.
6676
6677 -------------------------
6678 -- Build_Equality_Call --
6679 -------------------------
6680
6681 procedure Build_Equality_Call (Eq : Entity_Id) is
6682 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
6683 L_Exp : Node_Id := Relocate_Node (Lhs);
6684 R_Exp : Node_Id := Relocate_Node (Rhs);
6685
6686 begin
6687 -- Adjust operands if necessary to comparison type
6688
6689 if Base_Type (Op_Type) /= Base_Type (A_Typ)
6690 and then not Is_Class_Wide_Type (A_Typ)
6691 then
6692 L_Exp := OK_Convert_To (Op_Type, L_Exp);
6693 R_Exp := OK_Convert_To (Op_Type, R_Exp);
6694 end if;
6695
6696 -- If we have an Unchecked_Union, we need to add the inferred
6697 -- discriminant values as actuals in the function call. At this
6698 -- point, the expansion has determined that both operands have
6699 -- inferable discriminants.
6700
6701 if Is_Unchecked_Union (Op_Type) then
6702 declare
6703 Lhs_Type : constant Node_Id := Etype (L_Exp);
6704 Rhs_Type : constant Node_Id := Etype (R_Exp);
6705
6706 Lhs_Discr_Vals : Elist_Id;
6707 -- List of inferred discriminant values for left operand.
6708
6709 Rhs_Discr_Vals : Elist_Id;
6710 -- List of inferred discriminant values for right operand.
6711
6712 Discr : Entity_Id;
6713
6714 begin
6715 Lhs_Discr_Vals := New_Elmt_List;
6716 Rhs_Discr_Vals := New_Elmt_List;
6717
6718 -- Per-object constrained selected components require special
6719 -- attention. If the enclosing scope of the component is an
6720 -- Unchecked_Union, we cannot reference its discriminants
6721 -- directly. This is why we use the extra parameters of the
6722 -- equality function of the enclosing Unchecked_Union.
6723
6724 -- type UU_Type (Discr : Integer := 0) is
6725 -- . . .
6726 -- end record;
6727 -- pragma Unchecked_Union (UU_Type);
6728
6729 -- 1. Unchecked_Union enclosing record:
6730
6731 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
6732 -- . . .
6733 -- Comp : UU_Type (Discr);
6734 -- . . .
6735 -- end Enclosing_UU_Type;
6736 -- pragma Unchecked_Union (Enclosing_UU_Type);
6737
6738 -- Obj1 : Enclosing_UU_Type;
6739 -- Obj2 : Enclosing_UU_Type (1);
6740
6741 -- [. . .] Obj1 = Obj2 [. . .]
6742
6743 -- Generated code:
6744
6745 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
6746
6747 -- A and B are the formal parameters of the equality function
6748 -- of Enclosing_UU_Type. The function always has two extra
6749 -- formals to capture the inferred discriminant values for
6750 -- each discriminant of the type.
6751
6752 -- 2. Non-Unchecked_Union enclosing record:
6753
6754 -- type
6755 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
6756 -- is record
6757 -- . . .
6758 -- Comp : UU_Type (Discr);
6759 -- . . .
6760 -- end Enclosing_Non_UU_Type;
6761
6762 -- Obj1 : Enclosing_Non_UU_Type;
6763 -- Obj2 : Enclosing_Non_UU_Type (1);
6764
6765 -- ... Obj1 = Obj2 ...
6766
6767 -- Generated code:
6768
6769 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
6770 -- obj1.discr, obj2.discr)) then
6771
6772 -- In this case we can directly reference the discriminants of
6773 -- the enclosing record.
6774
6775 -- Process left operand of equality
6776
6777 if Nkind (Lhs) = N_Selected_Component
6778 and then
6779 Has_Per_Object_Constraint (Entity (Selector_Name (Lhs)))
6780 then
6781 -- If enclosing record is an Unchecked_Union, use formals
6782 -- corresponding to each discriminant. The name of the
6783 -- formal is that of the discriminant, with added suffix,
6784 -- see Exp_Ch3.Build_Record_Equality for details.
6785
6786 if Is_Unchecked_Union (Scope (Entity (Selector_Name (Lhs))))
6787 then
6788 Discr :=
6789 First_Discriminant
6790 (Scope (Entity (Selector_Name (Lhs))));
6791 while Present (Discr) loop
6792 Append_Elmt (
6793 Make_Identifier (Loc,
6794 Chars => New_External_Name (Chars (Discr), 'A')),
6795 To => Lhs_Discr_Vals);
6796 Next_Discriminant (Discr);
6797 end loop;
6798
6799 -- If enclosing record is of a non-Unchecked_Union type, it
6800 -- is possible to reference its discriminants directly.
6801
6802 else
6803 Discr := First_Discriminant (Lhs_Type);
6804 while Present (Discr) loop
6805 Append_Elmt (
6806 Make_Selected_Component (Loc,
6807 Prefix => Prefix (Lhs),
6808 Selector_Name =>
6809 New_Copy
6810 (Get_Discriminant_Value (Discr,
6811 Lhs_Type,
6812 Stored_Constraint (Lhs_Type)))),
6813 To => Lhs_Discr_Vals);
6814 Next_Discriminant (Discr);
6815 end loop;
6816 end if;
6817
6818 -- Otherwise operand is on object with a constrained type.
6819 -- Infer the discriminant values from the constraint.
6820
6821 else
6822
6823 Discr := First_Discriminant (Lhs_Type);
6824 while Present (Discr) loop
6825 Append_Elmt (
6826 New_Copy
6827 (Get_Discriminant_Value (Discr,
6828 Lhs_Type,
6829 Stored_Constraint (Lhs_Type))),
6830 To => Lhs_Discr_Vals);
6831 Next_Discriminant (Discr);
6832 end loop;
6833 end if;
6834
6835 -- Similar processing for right operand of equality
6836
6837 if Nkind (Rhs) = N_Selected_Component
6838 and then
6839 Has_Per_Object_Constraint (Entity (Selector_Name (Rhs)))
6840 then
6841 if Is_Unchecked_Union
6842 (Scope (Entity (Selector_Name (Rhs))))
6843 then
6844 Discr :=
6845 First_Discriminant
6846 (Scope (Entity (Selector_Name (Rhs))));
6847 while Present (Discr) loop
6848 Append_Elmt (
6849 Make_Identifier (Loc,
6850 Chars => New_External_Name (Chars (Discr), 'B')),
6851 To => Rhs_Discr_Vals);
6852 Next_Discriminant (Discr);
6853 end loop;
6854
6855 else
6856 Discr := First_Discriminant (Rhs_Type);
6857 while Present (Discr) loop
6858 Append_Elmt (
6859 Make_Selected_Component (Loc,
6860 Prefix => Prefix (Rhs),
6861 Selector_Name =>
6862 New_Copy (Get_Discriminant_Value
6863 (Discr,
6864 Rhs_Type,
6865 Stored_Constraint (Rhs_Type)))),
6866 To => Rhs_Discr_Vals);
6867 Next_Discriminant (Discr);
6868 end loop;
6869 end if;
6870
6871 else
6872 Discr := First_Discriminant (Rhs_Type);
6873 while Present (Discr) loop
6874 Append_Elmt (
6875 New_Copy (Get_Discriminant_Value
6876 (Discr,
6877 Rhs_Type,
6878 Stored_Constraint (Rhs_Type))),
6879 To => Rhs_Discr_Vals);
6880 Next_Discriminant (Discr);
6881 end loop;
6882 end if;
6883
6884 -- Now merge the list of discriminant values so that values
6885 -- of corresponding discriminants are adjacent.
6886
6887 declare
6888 Params : List_Id;
6889 L_Elmt : Elmt_Id;
6890 R_Elmt : Elmt_Id;
6891
6892 begin
6893 Params := New_List (L_Exp, R_Exp);
6894 L_Elmt := First_Elmt (Lhs_Discr_Vals);
6895 R_Elmt := First_Elmt (Rhs_Discr_Vals);
6896 while Present (L_Elmt) loop
6897 Append_To (Params, Node (L_Elmt));
6898 Append_To (Params, Node (R_Elmt));
6899 Next_Elmt (L_Elmt);
6900 Next_Elmt (R_Elmt);
6901 end loop;
6902
6903 Rewrite (N,
6904 Make_Function_Call (Loc,
6905 Name => New_Occurrence_Of (Eq, Loc),
6906 Parameter_Associations => Params));
6907 end;
6908 end;
6909
6910 -- Normal case, not an unchecked union
6911
6912 else
6913 Rewrite (N,
6914 Make_Function_Call (Loc,
6915 Name => New_Occurrence_Of (Eq, Loc),
6916 Parameter_Associations => New_List (L_Exp, R_Exp)));
6917 end if;
6918
6919 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6920 end Build_Equality_Call;
6921
6922 ------------------------------------
6923 -- Has_Unconstrained_UU_Component --
6924 ------------------------------------
6925
6926 function Has_Unconstrained_UU_Component
6927 (Typ : Node_Id) return Boolean
6928 is
6929 Tdef : constant Node_Id :=
6930 Type_Definition (Declaration_Node (Base_Type (Typ)));
6931 Clist : Node_Id;
6932 Vpart : Node_Id;
6933
6934 function Component_Is_Unconstrained_UU
6935 (Comp : Node_Id) return Boolean;
6936 -- Determines whether the subtype of the component is an
6937 -- unconstrained Unchecked_Union.
6938
6939 function Variant_Is_Unconstrained_UU
6940 (Variant : Node_Id) return Boolean;
6941 -- Determines whether a component of the variant has an unconstrained
6942 -- Unchecked_Union subtype.
6943
6944 -----------------------------------
6945 -- Component_Is_Unconstrained_UU --
6946 -----------------------------------
6947
6948 function Component_Is_Unconstrained_UU
6949 (Comp : Node_Id) return Boolean
6950 is
6951 begin
6952 if Nkind (Comp) /= N_Component_Declaration then
6953 return False;
6954 end if;
6955
6956 declare
6957 Sindic : constant Node_Id :=
6958 Subtype_Indication (Component_Definition (Comp));
6959
6960 begin
6961 -- Unconstrained nominal type. In the case of a constraint
6962 -- present, the node kind would have been N_Subtype_Indication.
6963
6964 if Nkind (Sindic) = N_Identifier then
6965 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
6966 end if;
6967
6968 return False;
6969 end;
6970 end Component_Is_Unconstrained_UU;
6971
6972 ---------------------------------
6973 -- Variant_Is_Unconstrained_UU --
6974 ---------------------------------
6975
6976 function Variant_Is_Unconstrained_UU
6977 (Variant : Node_Id) return Boolean
6978 is
6979 Clist : constant Node_Id := Component_List (Variant);
6980
6981 begin
6982 if Is_Empty_List (Component_Items (Clist)) then
6983 return False;
6984 end if;
6985
6986 -- We only need to test one component
6987
6988 declare
6989 Comp : Node_Id := First (Component_Items (Clist));
6990
6991 begin
6992 while Present (Comp) loop
6993 if Component_Is_Unconstrained_UU (Comp) then
6994 return True;
6995 end if;
6996
6997 Next (Comp);
6998 end loop;
6999 end;
7000
7001 -- None of the components withing the variant were of
7002 -- unconstrained Unchecked_Union type.
7003
7004 return False;
7005 end Variant_Is_Unconstrained_UU;
7006
7007 -- Start of processing for Has_Unconstrained_UU_Component
7008
7009 begin
7010 if Null_Present (Tdef) then
7011 return False;
7012 end if;
7013
7014 Clist := Component_List (Tdef);
7015 Vpart := Variant_Part (Clist);
7016
7017 -- Inspect available components
7018
7019 if Present (Component_Items (Clist)) then
7020 declare
7021 Comp : Node_Id := First (Component_Items (Clist));
7022
7023 begin
7024 while Present (Comp) loop
7025
7026 -- One component is sufficient
7027
7028 if Component_Is_Unconstrained_UU (Comp) then
7029 return True;
7030 end if;
7031
7032 Next (Comp);
7033 end loop;
7034 end;
7035 end if;
7036
7037 -- Inspect available components withing variants
7038
7039 if Present (Vpart) then
7040 declare
7041 Variant : Node_Id := First (Variants (Vpart));
7042
7043 begin
7044 while Present (Variant) loop
7045
7046 -- One component within a variant is sufficient
7047
7048 if Variant_Is_Unconstrained_UU (Variant) then
7049 return True;
7050 end if;
7051
7052 Next (Variant);
7053 end loop;
7054 end;
7055 end if;
7056
7057 -- Neither the available components, nor the components inside the
7058 -- variant parts were of an unconstrained Unchecked_Union subtype.
7059
7060 return False;
7061 end Has_Unconstrained_UU_Component;
7062
7063 -- Start of processing for Expand_N_Op_Eq
7064
7065 begin
7066 Binary_Op_Validity_Checks (N);
7067
7068 -- Deal with private types
7069
7070 if Ekind (Typl) = E_Private_Type then
7071 Typl := Underlying_Type (Typl);
7072 elsif Ekind (Typl) = E_Private_Subtype then
7073 Typl := Underlying_Type (Base_Type (Typl));
7074 else
7075 null;
7076 end if;
7077
7078 -- It may happen in error situations that the underlying type is not
7079 -- set. The error will be detected later, here we just defend the
7080 -- expander code.
7081
7082 if No (Typl) then
7083 return;
7084 end if;
7085
7086 Typl := Base_Type (Typl);
7087
7088 -- Equality between variant records results in a call to a routine
7089 -- that has conditional tests of the discriminant value(s), and hence
7090 -- violates the No_Implicit_Conditionals restriction.
7091
7092 if Has_Variant_Part (Typl) then
7093 declare
7094 Msg : Boolean;
7095
7096 begin
7097 Check_Restriction (Msg, No_Implicit_Conditionals, N);
7098
7099 if Msg then
7100 Error_Msg_N
7101 ("\comparison of variant records tests discriminants", N);
7102 return;
7103 end if;
7104 end;
7105 end if;
7106
7107 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7108 -- means we no longer have a comparison operation, we are all done.
7109
7110 Expand_Compare_Minimize_Eliminate_Overflow (N);
7111
7112 if Nkind (N) /= N_Op_Eq then
7113 return;
7114 end if;
7115
7116 -- Boolean types (requiring handling of non-standard case)
7117
7118 if Is_Boolean_Type (Typl) then
7119 Adjust_Condition (Left_Opnd (N));
7120 Adjust_Condition (Right_Opnd (N));
7121 Set_Etype (N, Standard_Boolean);
7122 Adjust_Result_Type (N, Typ);
7123
7124 -- Array types
7125
7126 elsif Is_Array_Type (Typl) then
7127
7128 -- If we are doing full validity checking, and it is possible for the
7129 -- array elements to be invalid then expand out array comparisons to
7130 -- make sure that we check the array elements.
7131
7132 if Validity_Check_Operands
7133 and then not Is_Known_Valid (Component_Type (Typl))
7134 then
7135 declare
7136 Save_Force_Validity_Checks : constant Boolean :=
7137 Force_Validity_Checks;
7138 begin
7139 Force_Validity_Checks := True;
7140 Rewrite (N,
7141 Expand_Array_Equality
7142 (N,
7143 Relocate_Node (Lhs),
7144 Relocate_Node (Rhs),
7145 Bodies,
7146 Typl));
7147 Insert_Actions (N, Bodies);
7148 Analyze_And_Resolve (N, Standard_Boolean);
7149 Force_Validity_Checks := Save_Force_Validity_Checks;
7150 end;
7151
7152 -- Packed case where both operands are known aligned
7153
7154 elsif Is_Bit_Packed_Array (Typl)
7155 and then not Is_Possibly_Unaligned_Object (Lhs)
7156 and then not Is_Possibly_Unaligned_Object (Rhs)
7157 then
7158 Expand_Packed_Eq (N);
7159
7160 -- Where the component type is elementary we can use a block bit
7161 -- comparison (if supported on the target) exception in the case
7162 -- of floating-point (negative zero issues require element by
7163 -- element comparison), and atomic types (where we must be sure
7164 -- to load elements independently) and possibly unaligned arrays.
7165
7166 elsif Is_Elementary_Type (Component_Type (Typl))
7167 and then not Is_Floating_Point_Type (Component_Type (Typl))
7168 and then not Is_Atomic (Component_Type (Typl))
7169 and then not Is_Possibly_Unaligned_Object (Lhs)
7170 and then not Is_Possibly_Unaligned_Object (Rhs)
7171 and then Support_Composite_Compare_On_Target
7172 then
7173 null;
7174
7175 -- For composite and floating-point cases, expand equality loop to
7176 -- make sure of using proper comparisons for tagged types, and
7177 -- correctly handling the floating-point case.
7178
7179 else
7180 Rewrite (N,
7181 Expand_Array_Equality
7182 (N,
7183 Relocate_Node (Lhs),
7184 Relocate_Node (Rhs),
7185 Bodies,
7186 Typl));
7187 Insert_Actions (N, Bodies, Suppress => All_Checks);
7188 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7189 end if;
7190
7191 -- Record Types
7192
7193 elsif Is_Record_Type (Typl) then
7194
7195 -- For tagged types, use the primitive "="
7196
7197 if Is_Tagged_Type (Typl) then
7198
7199 -- No need to do anything else compiling under restriction
7200 -- No_Dispatching_Calls. During the semantic analysis we
7201 -- already notified such violation.
7202
7203 if Restriction_Active (No_Dispatching_Calls) then
7204 return;
7205 end if;
7206
7207 -- If this is derived from an untagged private type completed with
7208 -- a tagged type, it does not have a full view, so we use the
7209 -- primitive operations of the private type. This check should no
7210 -- longer be necessary when these types get their full views???
7211
7212 if Is_Private_Type (A_Typ)
7213 and then not Is_Tagged_Type (A_Typ)
7214 and then Is_Derived_Type (A_Typ)
7215 and then No (Full_View (A_Typ))
7216 then
7217 -- Search for equality operation, checking that the operands
7218 -- have the same type. Note that we must find a matching entry,
7219 -- or something is very wrong.
7220
7221 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
7222
7223 while Present (Prim) loop
7224 exit when Chars (Node (Prim)) = Name_Op_Eq
7225 and then Etype (First_Formal (Node (Prim))) =
7226 Etype (Next_Formal (First_Formal (Node (Prim))))
7227 and then
7228 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7229
7230 Next_Elmt (Prim);
7231 end loop;
7232
7233 pragma Assert (Present (Prim));
7234 Op_Name := Node (Prim);
7235
7236 -- Find the type's predefined equality or an overriding
7237 -- user- defined equality. The reason for not simply calling
7238 -- Find_Prim_Op here is that there may be a user-defined
7239 -- overloaded equality op that precedes the equality that we want,
7240 -- so we have to explicitly search (e.g., there could be an
7241 -- equality with two different parameter types).
7242
7243 else
7244 if Is_Class_Wide_Type (Typl) then
7245 Typl := Root_Type (Typl);
7246 end if;
7247
7248 Prim := First_Elmt (Primitive_Operations (Typl));
7249 while Present (Prim) loop
7250 exit when Chars (Node (Prim)) = Name_Op_Eq
7251 and then Etype (First_Formal (Node (Prim))) =
7252 Etype (Next_Formal (First_Formal (Node (Prim))))
7253 and then
7254 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7255
7256 Next_Elmt (Prim);
7257 end loop;
7258
7259 pragma Assert (Present (Prim));
7260 Op_Name := Node (Prim);
7261 end if;
7262
7263 Build_Equality_Call (Op_Name);
7264
7265 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7266 -- predefined equality operator for a type which has a subcomponent
7267 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7268
7269 elsif Has_Unconstrained_UU_Component (Typl) then
7270 Insert_Action (N,
7271 Make_Raise_Program_Error (Loc,
7272 Reason => PE_Unchecked_Union_Restriction));
7273
7274 -- Prevent Gigi from generating incorrect code by rewriting the
7275 -- equality as a standard False. (is this documented somewhere???)
7276
7277 Rewrite (N,
7278 New_Occurrence_Of (Standard_False, Loc));
7279
7280 elsif Is_Unchecked_Union (Typl) then
7281
7282 -- If we can infer the discriminants of the operands, we make a
7283 -- call to the TSS equality function.
7284
7285 if Has_Inferable_Discriminants (Lhs)
7286 and then
7287 Has_Inferable_Discriminants (Rhs)
7288 then
7289 Build_Equality_Call
7290 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7291
7292 else
7293 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7294 -- the predefined equality operator for an Unchecked_Union type
7295 -- if either of the operands lack inferable discriminants.
7296
7297 Insert_Action (N,
7298 Make_Raise_Program_Error (Loc,
7299 Reason => PE_Unchecked_Union_Restriction));
7300
7301 -- Prevent Gigi from generating incorrect code by rewriting
7302 -- the equality as a standard False (documented where???).
7303
7304 Rewrite (N,
7305 New_Occurrence_Of (Standard_False, Loc));
7306
7307 end if;
7308
7309 -- If a type support function is present (for complex cases), use it
7310
7311 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
7312 Build_Equality_Call
7313 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7314
7315 -- When comparing two Bounded_Strings, use the primitive equality of
7316 -- the root Super_String type.
7317
7318 elsif Is_Bounded_String (Typl) then
7319 Prim :=
7320 First_Elmt (Collect_Primitive_Operations (Root_Type (Typl)));
7321
7322 while Present (Prim) loop
7323 exit when Chars (Node (Prim)) = Name_Op_Eq
7324 and then Etype (First_Formal (Node (Prim))) =
7325 Etype (Next_Formal (First_Formal (Node (Prim))))
7326 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7327
7328 Next_Elmt (Prim);
7329 end loop;
7330
7331 -- A Super_String type should always have a primitive equality
7332
7333 pragma Assert (Present (Prim));
7334 Build_Equality_Call (Node (Prim));
7335
7336 -- Otherwise expand the component by component equality. Note that
7337 -- we never use block-bit comparisons for records, because of the
7338 -- problems with gaps. The backend will often be able to recombine
7339 -- the separate comparisons that we generate here.
7340
7341 else
7342 Remove_Side_Effects (Lhs);
7343 Remove_Side_Effects (Rhs);
7344 Rewrite (N,
7345 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
7346
7347 Insert_Actions (N, Bodies, Suppress => All_Checks);
7348 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7349 end if;
7350 end if;
7351
7352 -- Test if result is known at compile time
7353
7354 Rewrite_Comparison (N);
7355
7356 -- If we still have comparison for Vax_Float, process it
7357
7358 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
7359 Expand_Vax_Comparison (N);
7360 return;
7361 end if;
7362
7363 Optimize_Length_Comparison (N);
7364 end Expand_N_Op_Eq;
7365
7366 -----------------------
7367 -- Expand_N_Op_Expon --
7368 -----------------------
7369
7370 procedure Expand_N_Op_Expon (N : Node_Id) is
7371 Loc : constant Source_Ptr := Sloc (N);
7372 Typ : constant Entity_Id := Etype (N);
7373 Rtyp : constant Entity_Id := Root_Type (Typ);
7374 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
7375 Bastyp : constant Node_Id := Etype (Base);
7376 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
7377 Exptyp : constant Entity_Id := Etype (Exp);
7378 Ovflo : constant Boolean := Do_Overflow_Check (N);
7379 Expv : Uint;
7380 Temp : Node_Id;
7381 Rent : RE_Id;
7382 Ent : Entity_Id;
7383 Etyp : Entity_Id;
7384 Xnode : Node_Id;
7385
7386 begin
7387 Binary_Op_Validity_Checks (N);
7388
7389 -- CodePeer wants to see the unexpanded N_Op_Expon node
7390
7391 if CodePeer_Mode then
7392 return;
7393 end if;
7394
7395 -- If either operand is of a private type, then we have the use of an
7396 -- intrinsic operator, and we get rid of the privateness, by using root
7397 -- types of underlying types for the actual operation. Otherwise the
7398 -- private types will cause trouble if we expand multiplications or
7399 -- shifts etc. We also do this transformation if the result type is
7400 -- different from the base type.
7401
7402 if Is_Private_Type (Etype (Base))
7403 or else Is_Private_Type (Typ)
7404 or else Is_Private_Type (Exptyp)
7405 or else Rtyp /= Root_Type (Bastyp)
7406 then
7407 declare
7408 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
7409 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
7410 begin
7411 Rewrite (N,
7412 Unchecked_Convert_To (Typ,
7413 Make_Op_Expon (Loc,
7414 Left_Opnd => Unchecked_Convert_To (Bt, Base),
7415 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
7416 Analyze_And_Resolve (N, Typ);
7417 return;
7418 end;
7419 end if;
7420
7421 -- Check for MINIMIZED/ELIMINATED overflow mode
7422
7423 if Minimized_Eliminated_Overflow_Check (N) then
7424 Apply_Arithmetic_Overflow_Check (N);
7425 return;
7426 end if;
7427
7428 -- Test for case of known right argument where we can replace the
7429 -- exponentiation by an equivalent expression using multiplication.
7430
7431 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
7432 -- configurable run-time mode, we may not have the exponentiation
7433 -- routine available, and we don't want the legality of the program
7434 -- to depend on how clever the compiler is in knowing values.
7435
7436 if CRT_Safe_Compile_Time_Known_Value (Exp) then
7437 Expv := Expr_Value (Exp);
7438
7439 -- We only fold small non-negative exponents. You might think we
7440 -- could fold small negative exponents for the real case, but we
7441 -- can't because we are required to raise Constraint_Error for
7442 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
7443 -- See ACVC test C4A012B.
7444
7445 if Expv >= 0 and then Expv <= 4 then
7446
7447 -- X ** 0 = 1 (or 1.0)
7448
7449 if Expv = 0 then
7450
7451 -- Call Remove_Side_Effects to ensure that any side effects
7452 -- in the ignored left operand (in particular function calls
7453 -- to user defined functions) are properly executed.
7454
7455 Remove_Side_Effects (Base);
7456
7457 if Ekind (Typ) in Integer_Kind then
7458 Xnode := Make_Integer_Literal (Loc, Intval => 1);
7459 else
7460 Xnode := Make_Real_Literal (Loc, Ureal_1);
7461 end if;
7462
7463 -- X ** 1 = X
7464
7465 elsif Expv = 1 then
7466 Xnode := Base;
7467
7468 -- X ** 2 = X * X
7469
7470 elsif Expv = 2 then
7471 Xnode :=
7472 Make_Op_Multiply (Loc,
7473 Left_Opnd => Duplicate_Subexpr (Base),
7474 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
7475
7476 -- X ** 3 = X * X * X
7477
7478 elsif Expv = 3 then
7479 Xnode :=
7480 Make_Op_Multiply (Loc,
7481 Left_Opnd =>
7482 Make_Op_Multiply (Loc,
7483 Left_Opnd => Duplicate_Subexpr (Base),
7484 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
7485 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
7486
7487 -- X ** 4 ->
7488
7489 -- do
7490 -- En : constant base'type := base * base;
7491 -- in
7492 -- En * En
7493
7494 else
7495 pragma Assert (Expv = 4);
7496 Temp := Make_Temporary (Loc, 'E', Base);
7497
7498 Xnode :=
7499 Make_Expression_With_Actions (Loc,
7500 Actions => New_List (
7501 Make_Object_Declaration (Loc,
7502 Defining_Identifier => Temp,
7503 Constant_Present => True,
7504 Object_Definition => New_Occurrence_Of (Typ, Loc),
7505 Expression =>
7506 Make_Op_Multiply (Loc,
7507 Left_Opnd =>
7508 Duplicate_Subexpr (Base),
7509 Right_Opnd =>
7510 Duplicate_Subexpr_No_Checks (Base)))),
7511
7512 Expression =>
7513 Make_Op_Multiply (Loc,
7514 Left_Opnd => New_Occurrence_Of (Temp, Loc),
7515 Right_Opnd => New_Occurrence_Of (Temp, Loc)));
7516 end if;
7517
7518 Rewrite (N, Xnode);
7519 Analyze_And_Resolve (N, Typ);
7520 return;
7521 end if;
7522 end if;
7523
7524 -- Case of (2 ** expression) appearing as an argument of an integer
7525 -- multiplication, or as the right argument of a division of a non-
7526 -- negative integer. In such cases we leave the node untouched, setting
7527 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
7528 -- of the higher level node converts it into a shift.
7529
7530 -- Another case is 2 ** N in any other context. We simply convert
7531 -- this to 1 * 2 ** N, and then the above transformation applies.
7532
7533 -- Note: this transformation is not applicable for a modular type with
7534 -- a non-binary modulus in the multiplication case, since we get a wrong
7535 -- result if the shift causes an overflow before the modular reduction.
7536
7537 -- Note: we used to check that Exptyp was an unsigned type. But that is
7538 -- an unnecessary check, since if Exp is negative, we have a run-time
7539 -- error that is either caught (so we get the right result) or we have
7540 -- suppressed the check, in which case the code is erroneous anyway.
7541
7542 if Nkind (Base) = N_Integer_Literal
7543 and then CRT_Safe_Compile_Time_Known_Value (Base)
7544 and then Expr_Value (Base) = Uint_2
7545 and then Is_Integer_Type (Root_Type (Exptyp))
7546 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
7547 and then not Ovflo
7548 then
7549 -- First the multiply and divide cases
7550
7551 if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then
7552 declare
7553 P : constant Node_Id := Parent (N);
7554 L : constant Node_Id := Left_Opnd (P);
7555 R : constant Node_Id := Right_Opnd (P);
7556
7557 begin
7558 if (Nkind (P) = N_Op_Multiply
7559 and then not Non_Binary_Modulus (Typ)
7560 and then
7561 ((Is_Integer_Type (Etype (L)) and then R = N)
7562 or else
7563 (Is_Integer_Type (Etype (R)) and then L = N))
7564 and then not Do_Overflow_Check (P))
7565 or else
7566 (Nkind (P) = N_Op_Divide
7567 and then Is_Integer_Type (Etype (L))
7568 and then Is_Unsigned_Type (Etype (L))
7569 and then R = N
7570 and then not Do_Overflow_Check (P))
7571 then
7572 Set_Is_Power_Of_2_For_Shift (N);
7573 return;
7574 end if;
7575 end;
7576
7577 -- Now the other cases
7578
7579 elsif not Non_Binary_Modulus (Typ) then
7580 Rewrite (N,
7581 Make_Op_Multiply (Loc,
7582 Left_Opnd => Make_Integer_Literal (Loc, 1),
7583 Right_Opnd => Relocate_Node (N)));
7584 Analyze_And_Resolve (N, Typ);
7585 return;
7586 end if;
7587 end if;
7588
7589 -- Fall through if exponentiation must be done using a runtime routine
7590
7591 -- First deal with modular case
7592
7593 if Is_Modular_Integer_Type (Rtyp) then
7594
7595 -- Non-binary case, we call the special exponentiation routine for
7596 -- the non-binary case, converting the argument to Long_Long_Integer
7597 -- and passing the modulus value. Then the result is converted back
7598 -- to the base type.
7599
7600 if Non_Binary_Modulus (Rtyp) then
7601 Rewrite (N,
7602 Convert_To (Typ,
7603 Make_Function_Call (Loc,
7604 Name => New_Occurrence_Of (RTE (RE_Exp_Modular), Loc),
7605 Parameter_Associations => New_List (
7606 Convert_To (RTE (RE_Unsigned), Base),
7607 Make_Integer_Literal (Loc, Modulus (Rtyp)),
7608 Exp))));
7609
7610 -- Binary case, in this case, we call one of two routines, either the
7611 -- unsigned integer case, or the unsigned long long integer case,
7612 -- with a final "and" operation to do the required mod.
7613
7614 else
7615 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
7616 Ent := RTE (RE_Exp_Unsigned);
7617 else
7618 Ent := RTE (RE_Exp_Long_Long_Unsigned);
7619 end if;
7620
7621 Rewrite (N,
7622 Convert_To (Typ,
7623 Make_Op_And (Loc,
7624 Left_Opnd =>
7625 Make_Function_Call (Loc,
7626 Name => New_Occurrence_Of (Ent, Loc),
7627 Parameter_Associations => New_List (
7628 Convert_To (Etype (First_Formal (Ent)), Base),
7629 Exp)),
7630 Right_Opnd =>
7631 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
7632
7633 end if;
7634
7635 -- Common exit point for modular type case
7636
7637 Analyze_And_Resolve (N, Typ);
7638 return;
7639
7640 -- Signed integer cases, done using either Integer or Long_Long_Integer.
7641 -- It is not worth having routines for Short_[Short_]Integer, since for
7642 -- most machines it would not help, and it would generate more code that
7643 -- might need certification when a certified run time is required.
7644
7645 -- In the integer cases, we have two routines, one for when overflow
7646 -- checks are required, and one when they are not required, since there
7647 -- is a real gain in omitting checks on many machines.
7648
7649 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
7650 or else (Rtyp = Base_Type (Standard_Long_Integer)
7651 and then
7652 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
7653 or else Rtyp = Universal_Integer
7654 then
7655 Etyp := Standard_Long_Long_Integer;
7656
7657 -- Overflow checking is the only choice on the AAMP target, where
7658 -- arithmetic instructions check overflow automatically, so only
7659 -- one version of the exponentiation unit is needed.
7660
7661 if Ovflo or AAMP_On_Target then
7662 Rent := RE_Exp_Long_Long_Integer;
7663 else
7664 Rent := RE_Exn_Long_Long_Integer;
7665 end if;
7666
7667 elsif Is_Signed_Integer_Type (Rtyp) then
7668 Etyp := Standard_Integer;
7669
7670 -- Overflow checking is the only choice on the AAMP target, where
7671 -- arithmetic instructions check overflow automatically, so only
7672 -- one version of the exponentiation unit is needed.
7673
7674 if Ovflo or AAMP_On_Target then
7675 Rent := RE_Exp_Integer;
7676 else
7677 Rent := RE_Exn_Integer;
7678 end if;
7679
7680 -- Floating-point cases, always done using Long_Long_Float. We do not
7681 -- need separate routines for the overflow case here, since in the case
7682 -- of floating-point, we generate infinities anyway as a rule (either
7683 -- that or we automatically trap overflow), and if there is an infinity
7684 -- generated and a range check is required, the check will fail anyway.
7685
7686 else
7687 pragma Assert (Is_Floating_Point_Type (Rtyp));
7688 Etyp := Standard_Long_Long_Float;
7689 Rent := RE_Exn_Long_Long_Float;
7690 end if;
7691
7692 -- Common processing for integer cases and floating-point cases.
7693 -- If we are in the right type, we can call runtime routine directly
7694
7695 if Typ = Etyp
7696 and then Rtyp /= Universal_Integer
7697 and then Rtyp /= Universal_Real
7698 then
7699 Rewrite (N,
7700 Make_Function_Call (Loc,
7701 Name => New_Occurrence_Of (RTE (Rent), Loc),
7702 Parameter_Associations => New_List (Base, Exp)));
7703
7704 -- Otherwise we have to introduce conversions (conversions are also
7705 -- required in the universal cases, since the runtime routine is
7706 -- typed using one of the standard types).
7707
7708 else
7709 Rewrite (N,
7710 Convert_To (Typ,
7711 Make_Function_Call (Loc,
7712 Name => New_Occurrence_Of (RTE (Rent), Loc),
7713 Parameter_Associations => New_List (
7714 Convert_To (Etyp, Base),
7715 Exp))));
7716 end if;
7717
7718 Analyze_And_Resolve (N, Typ);
7719 return;
7720
7721 exception
7722 when RE_Not_Available =>
7723 return;
7724 end Expand_N_Op_Expon;
7725
7726 --------------------
7727 -- Expand_N_Op_Ge --
7728 --------------------
7729
7730 procedure Expand_N_Op_Ge (N : Node_Id) is
7731 Typ : constant Entity_Id := Etype (N);
7732 Op1 : constant Node_Id := Left_Opnd (N);
7733 Op2 : constant Node_Id := Right_Opnd (N);
7734 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7735
7736 begin
7737 Binary_Op_Validity_Checks (N);
7738
7739 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7740 -- means we no longer have a comparison operation, we are all done.
7741
7742 Expand_Compare_Minimize_Eliminate_Overflow (N);
7743
7744 if Nkind (N) /= N_Op_Ge then
7745 return;
7746 end if;
7747
7748 -- Array type case
7749
7750 if Is_Array_Type (Typ1) then
7751 Expand_Array_Comparison (N);
7752 return;
7753 end if;
7754
7755 -- Deal with boolean operands
7756
7757 if Is_Boolean_Type (Typ1) then
7758 Adjust_Condition (Op1);
7759 Adjust_Condition (Op2);
7760 Set_Etype (N, Standard_Boolean);
7761 Adjust_Result_Type (N, Typ);
7762 end if;
7763
7764 Rewrite_Comparison (N);
7765
7766 -- If we still have comparison, and Vax_Float type, process it
7767
7768 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7769 Expand_Vax_Comparison (N);
7770 return;
7771 end if;
7772
7773 Optimize_Length_Comparison (N);
7774 end Expand_N_Op_Ge;
7775
7776 --------------------
7777 -- Expand_N_Op_Gt --
7778 --------------------
7779
7780 procedure Expand_N_Op_Gt (N : Node_Id) is
7781 Typ : constant Entity_Id := Etype (N);
7782 Op1 : constant Node_Id := Left_Opnd (N);
7783 Op2 : constant Node_Id := Right_Opnd (N);
7784 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7785
7786 begin
7787 Binary_Op_Validity_Checks (N);
7788
7789 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7790 -- means we no longer have a comparison operation, we are all done.
7791
7792 Expand_Compare_Minimize_Eliminate_Overflow (N);
7793
7794 if Nkind (N) /= N_Op_Gt then
7795 return;
7796 end if;
7797
7798 -- Deal with array type operands
7799
7800 if Is_Array_Type (Typ1) then
7801 Expand_Array_Comparison (N);
7802 return;
7803 end if;
7804
7805 -- Deal with boolean type operands
7806
7807 if Is_Boolean_Type (Typ1) then
7808 Adjust_Condition (Op1);
7809 Adjust_Condition (Op2);
7810 Set_Etype (N, Standard_Boolean);
7811 Adjust_Result_Type (N, Typ);
7812 end if;
7813
7814 Rewrite_Comparison (N);
7815
7816 -- If we still have comparison, and Vax_Float type, process it
7817
7818 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7819 Expand_Vax_Comparison (N);
7820 return;
7821 end if;
7822
7823 Optimize_Length_Comparison (N);
7824 end Expand_N_Op_Gt;
7825
7826 --------------------
7827 -- Expand_N_Op_Le --
7828 --------------------
7829
7830 procedure Expand_N_Op_Le (N : Node_Id) is
7831 Typ : constant Entity_Id := Etype (N);
7832 Op1 : constant Node_Id := Left_Opnd (N);
7833 Op2 : constant Node_Id := Right_Opnd (N);
7834 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7835
7836 begin
7837 Binary_Op_Validity_Checks (N);
7838
7839 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7840 -- means we no longer have a comparison operation, we are all done.
7841
7842 Expand_Compare_Minimize_Eliminate_Overflow (N);
7843
7844 if Nkind (N) /= N_Op_Le then
7845 return;
7846 end if;
7847
7848 -- Deal with array type operands
7849
7850 if Is_Array_Type (Typ1) then
7851 Expand_Array_Comparison (N);
7852 return;
7853 end if;
7854
7855 -- Deal with Boolean type operands
7856
7857 if Is_Boolean_Type (Typ1) then
7858 Adjust_Condition (Op1);
7859 Adjust_Condition (Op2);
7860 Set_Etype (N, Standard_Boolean);
7861 Adjust_Result_Type (N, Typ);
7862 end if;
7863
7864 Rewrite_Comparison (N);
7865
7866 -- If we still have comparison, and Vax_Float type, process it
7867
7868 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7869 Expand_Vax_Comparison (N);
7870 return;
7871 end if;
7872
7873 Optimize_Length_Comparison (N);
7874 end Expand_N_Op_Le;
7875
7876 --------------------
7877 -- Expand_N_Op_Lt --
7878 --------------------
7879
7880 procedure Expand_N_Op_Lt (N : Node_Id) is
7881 Typ : constant Entity_Id := Etype (N);
7882 Op1 : constant Node_Id := Left_Opnd (N);
7883 Op2 : constant Node_Id := Right_Opnd (N);
7884 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7885
7886 begin
7887 Binary_Op_Validity_Checks (N);
7888
7889 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7890 -- means we no longer have a comparison operation, we are all done.
7891
7892 Expand_Compare_Minimize_Eliminate_Overflow (N);
7893
7894 if Nkind (N) /= N_Op_Lt then
7895 return;
7896 end if;
7897
7898 -- Deal with array type operands
7899
7900 if Is_Array_Type (Typ1) then
7901 Expand_Array_Comparison (N);
7902 return;
7903 end if;
7904
7905 -- Deal with Boolean type operands
7906
7907 if Is_Boolean_Type (Typ1) then
7908 Adjust_Condition (Op1);
7909 Adjust_Condition (Op2);
7910 Set_Etype (N, Standard_Boolean);
7911 Adjust_Result_Type (N, Typ);
7912 end if;
7913
7914 Rewrite_Comparison (N);
7915
7916 -- If we still have comparison, and Vax_Float type, process it
7917
7918 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7919 Expand_Vax_Comparison (N);
7920 return;
7921 end if;
7922
7923 Optimize_Length_Comparison (N);
7924 end Expand_N_Op_Lt;
7925
7926 -----------------------
7927 -- Expand_N_Op_Minus --
7928 -----------------------
7929
7930 procedure Expand_N_Op_Minus (N : Node_Id) is
7931 Loc : constant Source_Ptr := Sloc (N);
7932 Typ : constant Entity_Id := Etype (N);
7933
7934 begin
7935 Unary_Op_Validity_Checks (N);
7936
7937 -- Check for MINIMIZED/ELIMINATED overflow mode
7938
7939 if Minimized_Eliminated_Overflow_Check (N) then
7940 Apply_Arithmetic_Overflow_Check (N);
7941 return;
7942 end if;
7943
7944 if not Backend_Overflow_Checks_On_Target
7945 and then Is_Signed_Integer_Type (Etype (N))
7946 and then Do_Overflow_Check (N)
7947 then
7948 -- Software overflow checking expands -expr into (0 - expr)
7949
7950 Rewrite (N,
7951 Make_Op_Subtract (Loc,
7952 Left_Opnd => Make_Integer_Literal (Loc, 0),
7953 Right_Opnd => Right_Opnd (N)));
7954
7955 Analyze_And_Resolve (N, Typ);
7956
7957 -- Vax floating-point types case
7958
7959 elsif Vax_Float (Etype (N)) then
7960 Expand_Vax_Arith (N);
7961 end if;
7962 end Expand_N_Op_Minus;
7963
7964 ---------------------
7965 -- Expand_N_Op_Mod --
7966 ---------------------
7967
7968 procedure Expand_N_Op_Mod (N : Node_Id) is
7969 Loc : constant Source_Ptr := Sloc (N);
7970 Typ : constant Entity_Id := Etype (N);
7971 DDC : constant Boolean := Do_Division_Check (N);
7972
7973 Left : Node_Id;
7974 Right : Node_Id;
7975
7976 LLB : Uint;
7977 Llo : Uint;
7978 Lhi : Uint;
7979 LOK : Boolean;
7980 Rlo : Uint;
7981 Rhi : Uint;
7982 ROK : Boolean;
7983
7984 pragma Warnings (Off, Lhi);
7985
7986 begin
7987 Binary_Op_Validity_Checks (N);
7988
7989 -- Check for MINIMIZED/ELIMINATED overflow mode
7990
7991 if Minimized_Eliminated_Overflow_Check (N) then
7992 Apply_Arithmetic_Overflow_Check (N);
7993 return;
7994 end if;
7995
7996 if Is_Integer_Type (Etype (N)) then
7997 Apply_Divide_Checks (N);
7998
7999 -- All done if we don't have a MOD any more, which can happen as a
8000 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8001
8002 if Nkind (N) /= N_Op_Mod then
8003 return;
8004 end if;
8005 end if;
8006
8007 -- Proceed with expansion of mod operator
8008
8009 Left := Left_Opnd (N);
8010 Right := Right_Opnd (N);
8011
8012 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
8013 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
8014
8015 -- Convert mod to rem if operands are both known to be non-negative, or
8016 -- both known to be non-positive (these are the cases in which rem and
8017 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
8018 -- likely that this will improve the quality of code, (the operation now
8019 -- corresponds to the hardware remainder), and it does not seem likely
8020 -- that it could be harmful. It also avoids some cases of the elaborate
8021 -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %).
8022
8023 if (LOK and ROK)
8024 and then ((Llo >= 0 and then Rlo >= 0)
8025 or else
8026 (Lhi <= 0 and then Rhi <= 0))
8027 then
8028 Rewrite (N,
8029 Make_Op_Rem (Sloc (N),
8030 Left_Opnd => Left_Opnd (N),
8031 Right_Opnd => Right_Opnd (N)));
8032
8033 -- Instead of reanalyzing the node we do the analysis manually. This
8034 -- avoids anomalies when the replacement is done in an instance and
8035 -- is epsilon more efficient.
8036
8037 Set_Entity (N, Standard_Entity (S_Op_Rem));
8038 Set_Etype (N, Typ);
8039 Set_Do_Division_Check (N, DDC);
8040 Expand_N_Op_Rem (N);
8041 Set_Analyzed (N);
8042 return;
8043
8044 -- Otherwise, normal mod processing
8045
8046 else
8047 -- Apply optimization x mod 1 = 0. We don't really need that with
8048 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
8049 -- certainly harmless.
8050
8051 if Is_Integer_Type (Etype (N))
8052 and then Compile_Time_Known_Value (Right)
8053 and then Expr_Value (Right) = Uint_1
8054 then
8055 -- Call Remove_Side_Effects to ensure that any side effects in
8056 -- the ignored left operand (in particular function calls to
8057 -- user defined functions) are properly executed.
8058
8059 Remove_Side_Effects (Left);
8060
8061 Rewrite (N, Make_Integer_Literal (Loc, 0));
8062 Analyze_And_Resolve (N, Typ);
8063 return;
8064 end if;
8065
8066 -- If we still have a mod operator and we are in Modify_Tree_For_C
8067 -- mode, and we have a signed integer type, then here is where we do
8068 -- the rewrite in terms of Rem. Note this rewrite bypasses the need
8069 -- for the special handling of the annoying case of largest negative
8070 -- number mod minus one.
8071
8072 if Nkind (N) = N_Op_Mod
8073 and then Is_Signed_Integer_Type (Typ)
8074 and then Modify_Tree_For_C
8075 then
8076 -- In the general case, we expand A mod B as
8077
8078 -- Tnn : constant typ := A rem B;
8079 -- ..
8080 -- (if (A >= 0) = (B >= 0) then Tnn
8081 -- elsif Tnn = 0 then 0
8082 -- else Tnn + B)
8083
8084 -- The comparison can be written simply as A >= 0 if we know that
8085 -- B >= 0 which is a very common case.
8086
8087 -- An important optimization is when B is known at compile time
8088 -- to be 2**K for some constant. In this case we can simply AND
8089 -- the left operand with the bit string 2**K-1 (i.e. K 1-bits)
8090 -- and that works for both the positive and negative cases.
8091
8092 declare
8093 P2 : constant Nat := Power_Of_Two (Right);
8094
8095 begin
8096 if P2 /= 0 then
8097 Rewrite (N,
8098 Unchecked_Convert_To (Typ,
8099 Make_Op_And (Loc,
8100 Left_Opnd =>
8101 Unchecked_Convert_To
8102 (Corresponding_Unsigned_Type (Typ), Left),
8103 Right_Opnd =>
8104 Make_Integer_Literal (Loc, 2 ** P2 - 1))));
8105 Analyze_And_Resolve (N, Typ);
8106 return;
8107 end if;
8108 end;
8109
8110 -- Here for the full rewrite
8111
8112 declare
8113 Tnn : constant Entity_Id := Make_Temporary (Sloc (N), 'T', N);
8114 Cmp : Node_Id;
8115
8116 begin
8117 Cmp :=
8118 Make_Op_Ge (Loc,
8119 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
8120 Right_Opnd => Make_Integer_Literal (Loc, 0));
8121
8122 if not LOK or else Rlo < 0 then
8123 Cmp :=
8124 Make_Op_Eq (Loc,
8125 Left_Opnd => Cmp,
8126 Right_Opnd =>
8127 Make_Op_Ge (Loc,
8128 Left_Opnd => Duplicate_Subexpr_No_Checks (Right),
8129 Right_Opnd => Make_Integer_Literal (Loc, 0)));
8130 end if;
8131
8132 Insert_Action (N,
8133 Make_Object_Declaration (Loc,
8134 Defining_Identifier => Tnn,
8135 Constant_Present => True,
8136 Object_Definition => New_Occurrence_Of (Typ, Loc),
8137 Expression =>
8138 Make_Op_Rem (Loc,
8139 Left_Opnd => Left,
8140 Right_Opnd => Right)));
8141
8142 Rewrite (N,
8143 Make_If_Expression (Loc,
8144 Expressions => New_List (
8145 Cmp,
8146 New_Occurrence_Of (Tnn, Loc),
8147 Make_If_Expression (Loc,
8148 Is_Elsif => True,
8149 Expressions => New_List (
8150 Make_Op_Eq (Loc,
8151 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8152 Right_Opnd => Make_Integer_Literal (Loc, 0)),
8153 Make_Integer_Literal (Loc, 0),
8154 Make_Op_Add (Loc,
8155 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8156 Right_Opnd =>
8157 Duplicate_Subexpr_No_Checks (Right)))))));
8158
8159 Analyze_And_Resolve (N, Typ);
8160 return;
8161 end;
8162 end if;
8163
8164 -- Deal with annoying case of largest negative number mod minus one.
8165 -- Gigi may not handle this case correctly, because on some targets,
8166 -- the mod value is computed using a divide instruction which gives
8167 -- an overflow trap for this case.
8168
8169 -- It would be a bit more efficient to figure out which targets
8170 -- this is really needed for, but in practice it is reasonable
8171 -- to do the following special check in all cases, since it means
8172 -- we get a clearer message, and also the overhead is minimal given
8173 -- that division is expensive in any case.
8174
8175 -- In fact the check is quite easy, if the right operand is -1, then
8176 -- the mod value is always 0, and we can just ignore the left operand
8177 -- completely in this case.
8178
8179 -- This only applies if we still have a mod operator. Skip if we
8180 -- have already rewritten this (e.g. in the case of eliminated
8181 -- overflow checks which have driven us into bignum mode).
8182
8183 if Nkind (N) = N_Op_Mod then
8184
8185 -- The operand type may be private (e.g. in the expansion of an
8186 -- intrinsic operation) so we must use the underlying type to get
8187 -- the bounds, and convert the literals explicitly.
8188
8189 LLB :=
8190 Expr_Value
8191 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
8192
8193 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
8194 and then ((not LOK) or else (Llo = LLB))
8195 then
8196 Rewrite (N,
8197 Make_If_Expression (Loc,
8198 Expressions => New_List (
8199 Make_Op_Eq (Loc,
8200 Left_Opnd => Duplicate_Subexpr (Right),
8201 Right_Opnd =>
8202 Unchecked_Convert_To (Typ,
8203 Make_Integer_Literal (Loc, -1))),
8204 Unchecked_Convert_To (Typ,
8205 Make_Integer_Literal (Loc, Uint_0)),
8206 Relocate_Node (N))));
8207
8208 Set_Analyzed (Next (Next (First (Expressions (N)))));
8209 Analyze_And_Resolve (N, Typ);
8210 end if;
8211 end if;
8212 end if;
8213 end Expand_N_Op_Mod;
8214
8215 --------------------------
8216 -- Expand_N_Op_Multiply --
8217 --------------------------
8218
8219 procedure Expand_N_Op_Multiply (N : Node_Id) is
8220 Loc : constant Source_Ptr := Sloc (N);
8221 Lop : constant Node_Id := Left_Opnd (N);
8222 Rop : constant Node_Id := Right_Opnd (N);
8223
8224 Lp2 : constant Boolean :=
8225 Nkind (Lop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Lop);
8226 Rp2 : constant Boolean :=
8227 Nkind (Rop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Rop);
8228
8229 Ltyp : constant Entity_Id := Etype (Lop);
8230 Rtyp : constant Entity_Id := Etype (Rop);
8231 Typ : Entity_Id := Etype (N);
8232
8233 begin
8234 Binary_Op_Validity_Checks (N);
8235
8236 -- Check for MINIMIZED/ELIMINATED overflow mode
8237
8238 if Minimized_Eliminated_Overflow_Check (N) then
8239 Apply_Arithmetic_Overflow_Check (N);
8240 return;
8241 end if;
8242
8243 -- Special optimizations for integer types
8244
8245 if Is_Integer_Type (Typ) then
8246
8247 -- N * 0 = 0 for integer types
8248
8249 if Compile_Time_Known_Value (Rop)
8250 and then Expr_Value (Rop) = Uint_0
8251 then
8252 -- Call Remove_Side_Effects to ensure that any side effects in
8253 -- the ignored left operand (in particular function calls to
8254 -- user defined functions) are properly executed.
8255
8256 Remove_Side_Effects (Lop);
8257
8258 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8259 Analyze_And_Resolve (N, Typ);
8260 return;
8261 end if;
8262
8263 -- Similar handling for 0 * N = 0
8264
8265 if Compile_Time_Known_Value (Lop)
8266 and then Expr_Value (Lop) = Uint_0
8267 then
8268 Remove_Side_Effects (Rop);
8269 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8270 Analyze_And_Resolve (N, Typ);
8271 return;
8272 end if;
8273
8274 -- N * 1 = 1 * N = N for integer types
8275
8276 -- This optimisation is not done if we are going to
8277 -- rewrite the product 1 * 2 ** N to a shift.
8278
8279 if Compile_Time_Known_Value (Rop)
8280 and then Expr_Value (Rop) = Uint_1
8281 and then not Lp2
8282 then
8283 Rewrite (N, Lop);
8284 return;
8285
8286 elsif Compile_Time_Known_Value (Lop)
8287 and then Expr_Value (Lop) = Uint_1
8288 and then not Rp2
8289 then
8290 Rewrite (N, Rop);
8291 return;
8292 end if;
8293 end if;
8294
8295 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
8296 -- Is_Power_Of_2_For_Shift is set means that we know that our left
8297 -- operand is an integer, as required for this to work.
8298
8299 if Rp2 then
8300 if Lp2 then
8301
8302 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
8303
8304 Rewrite (N,
8305 Make_Op_Expon (Loc,
8306 Left_Opnd => Make_Integer_Literal (Loc, 2),
8307 Right_Opnd =>
8308 Make_Op_Add (Loc,
8309 Left_Opnd => Right_Opnd (Lop),
8310 Right_Opnd => Right_Opnd (Rop))));
8311 Analyze_And_Resolve (N, Typ);
8312 return;
8313
8314 else
8315 -- If the result is modular, perform the reduction of the result
8316 -- appropriately.
8317
8318 if Is_Modular_Integer_Type (Typ)
8319 and then not Non_Binary_Modulus (Typ)
8320 then
8321 Rewrite (N,
8322 Make_Op_And (Loc,
8323 Left_Opnd =>
8324 Make_Op_Shift_Left (Loc,
8325 Left_Opnd => Lop,
8326 Right_Opnd =>
8327 Convert_To (Standard_Natural, Right_Opnd (Rop))),
8328 Right_Opnd =>
8329 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
8330
8331 else
8332 Rewrite (N,
8333 Make_Op_Shift_Left (Loc,
8334 Left_Opnd => Lop,
8335 Right_Opnd =>
8336 Convert_To (Standard_Natural, Right_Opnd (Rop))));
8337 end if;
8338
8339 Analyze_And_Resolve (N, Typ);
8340 return;
8341 end if;
8342
8343 -- Same processing for the operands the other way round
8344
8345 elsif Lp2 then
8346 if Is_Modular_Integer_Type (Typ)
8347 and then not Non_Binary_Modulus (Typ)
8348 then
8349 Rewrite (N,
8350 Make_Op_And (Loc,
8351 Left_Opnd =>
8352 Make_Op_Shift_Left (Loc,
8353 Left_Opnd => Rop,
8354 Right_Opnd =>
8355 Convert_To (Standard_Natural, Right_Opnd (Lop))),
8356 Right_Opnd =>
8357 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
8358
8359 else
8360 Rewrite (N,
8361 Make_Op_Shift_Left (Loc,
8362 Left_Opnd => Rop,
8363 Right_Opnd =>
8364 Convert_To (Standard_Natural, Right_Opnd (Lop))));
8365 end if;
8366
8367 Analyze_And_Resolve (N, Typ);
8368 return;
8369 end if;
8370
8371 -- Do required fixup of universal fixed operation
8372
8373 if Typ = Universal_Fixed then
8374 Fixup_Universal_Fixed_Operation (N);
8375 Typ := Etype (N);
8376 end if;
8377
8378 -- Multiplications with fixed-point results
8379
8380 if Is_Fixed_Point_Type (Typ) then
8381
8382 -- No special processing if Treat_Fixed_As_Integer is set, since from
8383 -- a semantic point of view such operations are simply integer
8384 -- operations and will be treated that way.
8385
8386 if not Treat_Fixed_As_Integer (N) then
8387
8388 -- Case of fixed * integer => fixed
8389
8390 if Is_Integer_Type (Rtyp) then
8391 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
8392
8393 -- Case of integer * fixed => fixed
8394
8395 elsif Is_Integer_Type (Ltyp) then
8396 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
8397
8398 -- Case of fixed * fixed => fixed
8399
8400 else
8401 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
8402 end if;
8403 end if;
8404
8405 -- Other cases of multiplication of fixed-point operands. Again we
8406 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
8407
8408 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
8409 and then not Treat_Fixed_As_Integer (N)
8410 then
8411 if Is_Integer_Type (Typ) then
8412 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
8413 else
8414 pragma Assert (Is_Floating_Point_Type (Typ));
8415 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
8416 end if;
8417
8418 -- Mixed-mode operations can appear in a non-static universal context,
8419 -- in which case the integer argument must be converted explicitly.
8420
8421 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
8422 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
8423 Analyze_And_Resolve (Rop, Universal_Real);
8424
8425 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
8426 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
8427 Analyze_And_Resolve (Lop, Universal_Real);
8428
8429 -- Non-fixed point cases, check software overflow checking required
8430
8431 elsif Is_Signed_Integer_Type (Etype (N)) then
8432 Apply_Arithmetic_Overflow_Check (N);
8433
8434 -- Deal with VAX float case
8435
8436 elsif Vax_Float (Typ) then
8437 Expand_Vax_Arith (N);
8438 return;
8439 end if;
8440 end Expand_N_Op_Multiply;
8441
8442 --------------------
8443 -- Expand_N_Op_Ne --
8444 --------------------
8445
8446 procedure Expand_N_Op_Ne (N : Node_Id) is
8447 Typ : constant Entity_Id := Etype (Left_Opnd (N));
8448
8449 begin
8450 -- Case of elementary type with standard operator
8451
8452 if Is_Elementary_Type (Typ)
8453 and then Sloc (Entity (N)) = Standard_Location
8454 then
8455 Binary_Op_Validity_Checks (N);
8456
8457 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
8458 -- means we no longer have a /= operation, we are all done.
8459
8460 Expand_Compare_Minimize_Eliminate_Overflow (N);
8461
8462 if Nkind (N) /= N_Op_Ne then
8463 return;
8464 end if;
8465
8466 -- Boolean types (requiring handling of non-standard case)
8467
8468 if Is_Boolean_Type (Typ) then
8469 Adjust_Condition (Left_Opnd (N));
8470 Adjust_Condition (Right_Opnd (N));
8471 Set_Etype (N, Standard_Boolean);
8472 Adjust_Result_Type (N, Typ);
8473 end if;
8474
8475 Rewrite_Comparison (N);
8476
8477 -- If we still have comparison for Vax_Float, process it
8478
8479 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
8480 Expand_Vax_Comparison (N);
8481 return;
8482 end if;
8483
8484 -- For all cases other than elementary types, we rewrite node as the
8485 -- negation of an equality operation, and reanalyze. The equality to be
8486 -- used is defined in the same scope and has the same signature. This
8487 -- signature must be set explicitly since in an instance it may not have
8488 -- the same visibility as in the generic unit. This avoids duplicating
8489 -- or factoring the complex code for record/array equality tests etc.
8490
8491 else
8492 declare
8493 Loc : constant Source_Ptr := Sloc (N);
8494 Neg : Node_Id;
8495 Ne : constant Entity_Id := Entity (N);
8496
8497 begin
8498 Binary_Op_Validity_Checks (N);
8499
8500 Neg :=
8501 Make_Op_Not (Loc,
8502 Right_Opnd =>
8503 Make_Op_Eq (Loc,
8504 Left_Opnd => Left_Opnd (N),
8505 Right_Opnd => Right_Opnd (N)));
8506 Set_Paren_Count (Right_Opnd (Neg), 1);
8507
8508 if Scope (Ne) /= Standard_Standard then
8509 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
8510 end if;
8511
8512 -- For navigation purposes, we want to treat the inequality as an
8513 -- implicit reference to the corresponding equality. Preserve the
8514 -- Comes_From_ source flag to generate proper Xref entries.
8515
8516 Preserve_Comes_From_Source (Neg, N);
8517 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
8518 Rewrite (N, Neg);
8519 Analyze_And_Resolve (N, Standard_Boolean);
8520 end;
8521 end if;
8522
8523 Optimize_Length_Comparison (N);
8524 end Expand_N_Op_Ne;
8525
8526 ---------------------
8527 -- Expand_N_Op_Not --
8528 ---------------------
8529
8530 -- If the argument is other than a Boolean array type, there is no special
8531 -- expansion required, except for VMS operations on signed integers.
8532
8533 -- For the packed case, we call the special routine in Exp_Pakd, except
8534 -- that if the component size is greater than one, we use the standard
8535 -- routine generating a gruesome loop (it is so peculiar to have packed
8536 -- arrays with non-standard Boolean representations anyway, so it does not
8537 -- matter that we do not handle this case efficiently).
8538
8539 -- For the unpacked case (and for the special packed case where we have non
8540 -- standard Booleans, as discussed above), we generate and insert into the
8541 -- tree the following function definition:
8542
8543 -- function Nnnn (A : arr) is
8544 -- B : arr;
8545 -- begin
8546 -- for J in a'range loop
8547 -- B (J) := not A (J);
8548 -- end loop;
8549 -- return B;
8550 -- end Nnnn;
8551
8552 -- Here arr is the actual subtype of the parameter (and hence always
8553 -- constrained). Then we replace the not with a call to this function.
8554
8555 procedure Expand_N_Op_Not (N : Node_Id) is
8556 Loc : constant Source_Ptr := Sloc (N);
8557 Typ : constant Entity_Id := Etype (N);
8558 Opnd : Node_Id;
8559 Arr : Entity_Id;
8560 A : Entity_Id;
8561 B : Entity_Id;
8562 J : Entity_Id;
8563 A_J : Node_Id;
8564 B_J : Node_Id;
8565
8566 Func_Name : Entity_Id;
8567 Loop_Statement : Node_Id;
8568
8569 begin
8570 Unary_Op_Validity_Checks (N);
8571
8572 -- For boolean operand, deal with non-standard booleans
8573
8574 if Is_Boolean_Type (Typ) then
8575 Adjust_Condition (Right_Opnd (N));
8576 Set_Etype (N, Standard_Boolean);
8577 Adjust_Result_Type (N, Typ);
8578 return;
8579 end if;
8580
8581 -- For the VMS "not" on signed integer types, use conversion to and from
8582 -- a predefined modular type.
8583
8584 if Is_VMS_Operator (Entity (N)) then
8585 declare
8586 Rtyp : Entity_Id;
8587 Utyp : Entity_Id;
8588
8589 begin
8590 -- If this is a derived type, retrieve original VMS type so that
8591 -- the proper sized type is used for intermediate values.
8592
8593 if Is_Derived_Type (Typ) then
8594 Rtyp := First_Subtype (Etype (Typ));
8595 else
8596 Rtyp := Typ;
8597 end if;
8598
8599 -- The proper unsigned type must have a size compatible with the
8600 -- operand, to prevent misalignment.
8601
8602 if RM_Size (Rtyp) <= 8 then
8603 Utyp := RTE (RE_Unsigned_8);
8604
8605 elsif RM_Size (Rtyp) <= 16 then
8606 Utyp := RTE (RE_Unsigned_16);
8607
8608 elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then
8609 Utyp := RTE (RE_Unsigned_32);
8610
8611 else
8612 Utyp := RTE (RE_Long_Long_Unsigned);
8613 end if;
8614
8615 Rewrite (N,
8616 Unchecked_Convert_To (Typ,
8617 Make_Op_Not (Loc,
8618 Unchecked_Convert_To (Utyp, Right_Opnd (N)))));
8619 Analyze_And_Resolve (N, Typ);
8620 return;
8621 end;
8622 end if;
8623
8624 -- Only array types need any other processing
8625
8626 if not Is_Array_Type (Typ) then
8627 return;
8628 end if;
8629
8630 -- Case of array operand. If bit packed with a component size of 1,
8631 -- handle it in Exp_Pakd if the operand is known to be aligned.
8632
8633 if Is_Bit_Packed_Array (Typ)
8634 and then Component_Size (Typ) = 1
8635 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
8636 then
8637 Expand_Packed_Not (N);
8638 return;
8639 end if;
8640
8641 -- Case of array operand which is not bit-packed. If the context is
8642 -- a safe assignment, call in-place operation, If context is a larger
8643 -- boolean expression in the context of a safe assignment, expansion is
8644 -- done by enclosing operation.
8645
8646 Opnd := Relocate_Node (Right_Opnd (N));
8647 Convert_To_Actual_Subtype (Opnd);
8648 Arr := Etype (Opnd);
8649 Ensure_Defined (Arr, N);
8650 Silly_Boolean_Array_Not_Test (N, Arr);
8651
8652 if Nkind (Parent (N)) = N_Assignment_Statement then
8653 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
8654 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8655 return;
8656
8657 -- Special case the negation of a binary operation
8658
8659 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
8660 and then Safe_In_Place_Array_Op
8661 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
8662 then
8663 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8664 return;
8665 end if;
8666
8667 elsif Nkind (Parent (N)) in N_Binary_Op
8668 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
8669 then
8670 declare
8671 Op1 : constant Node_Id := Left_Opnd (Parent (N));
8672 Op2 : constant Node_Id := Right_Opnd (Parent (N));
8673 Lhs : constant Node_Id := Name (Parent (Parent (N)));
8674
8675 begin
8676 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
8677
8678 -- (not A) op (not B) can be reduced to a single call
8679
8680 if N = Op1 and then Nkind (Op2) = N_Op_Not then
8681 return;
8682
8683 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
8684 return;
8685
8686 -- A xor (not B) can also be special-cased
8687
8688 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
8689 return;
8690 end if;
8691 end if;
8692 end;
8693 end if;
8694
8695 A := Make_Defining_Identifier (Loc, Name_uA);
8696 B := Make_Defining_Identifier (Loc, Name_uB);
8697 J := Make_Defining_Identifier (Loc, Name_uJ);
8698
8699 A_J :=
8700 Make_Indexed_Component (Loc,
8701 Prefix => New_Occurrence_Of (A, Loc),
8702 Expressions => New_List (New_Occurrence_Of (J, Loc)));
8703
8704 B_J :=
8705 Make_Indexed_Component (Loc,
8706 Prefix => New_Occurrence_Of (B, Loc),
8707 Expressions => New_List (New_Occurrence_Of (J, Loc)));
8708
8709 Loop_Statement :=
8710 Make_Implicit_Loop_Statement (N,
8711 Identifier => Empty,
8712
8713 Iteration_Scheme =>
8714 Make_Iteration_Scheme (Loc,
8715 Loop_Parameter_Specification =>
8716 Make_Loop_Parameter_Specification (Loc,
8717 Defining_Identifier => J,
8718 Discrete_Subtype_Definition =>
8719 Make_Attribute_Reference (Loc,
8720 Prefix => Make_Identifier (Loc, Chars (A)),
8721 Attribute_Name => Name_Range))),
8722
8723 Statements => New_List (
8724 Make_Assignment_Statement (Loc,
8725 Name => B_J,
8726 Expression => Make_Op_Not (Loc, A_J))));
8727
8728 Func_Name := Make_Temporary (Loc, 'N');
8729 Set_Is_Inlined (Func_Name);
8730
8731 Insert_Action (N,
8732 Make_Subprogram_Body (Loc,
8733 Specification =>
8734 Make_Function_Specification (Loc,
8735 Defining_Unit_Name => Func_Name,
8736 Parameter_Specifications => New_List (
8737 Make_Parameter_Specification (Loc,
8738 Defining_Identifier => A,
8739 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8740 Result_Definition => New_Occurrence_Of (Typ, Loc)),
8741
8742 Declarations => New_List (
8743 Make_Object_Declaration (Loc,
8744 Defining_Identifier => B,
8745 Object_Definition => New_Occurrence_Of (Arr, Loc))),
8746
8747 Handled_Statement_Sequence =>
8748 Make_Handled_Sequence_Of_Statements (Loc,
8749 Statements => New_List (
8750 Loop_Statement,
8751 Make_Simple_Return_Statement (Loc,
8752 Expression => Make_Identifier (Loc, Chars (B)))))));
8753
8754 Rewrite (N,
8755 Make_Function_Call (Loc,
8756 Name => New_Occurrence_Of (Func_Name, Loc),
8757 Parameter_Associations => New_List (Opnd)));
8758
8759 Analyze_And_Resolve (N, Typ);
8760 end Expand_N_Op_Not;
8761
8762 --------------------
8763 -- Expand_N_Op_Or --
8764 --------------------
8765
8766 procedure Expand_N_Op_Or (N : Node_Id) is
8767 Typ : constant Entity_Id := Etype (N);
8768
8769 begin
8770 Binary_Op_Validity_Checks (N);
8771
8772 if Is_Array_Type (Etype (N)) then
8773 Expand_Boolean_Operator (N);
8774
8775 elsif Is_Boolean_Type (Etype (N)) then
8776 Adjust_Condition (Left_Opnd (N));
8777 Adjust_Condition (Right_Opnd (N));
8778 Set_Etype (N, Standard_Boolean);
8779 Adjust_Result_Type (N, Typ);
8780
8781 elsif Is_Intrinsic_Subprogram (Entity (N)) then
8782 Expand_Intrinsic_Call (N, Entity (N));
8783
8784 end if;
8785 end Expand_N_Op_Or;
8786
8787 ----------------------
8788 -- Expand_N_Op_Plus --
8789 ----------------------
8790
8791 procedure Expand_N_Op_Plus (N : Node_Id) is
8792 begin
8793 Unary_Op_Validity_Checks (N);
8794
8795 -- Check for MINIMIZED/ELIMINATED overflow mode
8796
8797 if Minimized_Eliminated_Overflow_Check (N) then
8798 Apply_Arithmetic_Overflow_Check (N);
8799 return;
8800 end if;
8801 end Expand_N_Op_Plus;
8802
8803 ---------------------
8804 -- Expand_N_Op_Rem --
8805 ---------------------
8806
8807 procedure Expand_N_Op_Rem (N : Node_Id) is
8808 Loc : constant Source_Ptr := Sloc (N);
8809 Typ : constant Entity_Id := Etype (N);
8810
8811 Left : Node_Id;
8812 Right : Node_Id;
8813
8814 Lo : Uint;
8815 Hi : Uint;
8816 OK : Boolean;
8817
8818 Lneg : Boolean;
8819 Rneg : Boolean;
8820 -- Set if corresponding operand can be negative
8821
8822 pragma Unreferenced (Hi);
8823
8824 begin
8825 Binary_Op_Validity_Checks (N);
8826
8827 -- Check for MINIMIZED/ELIMINATED overflow mode
8828
8829 if Minimized_Eliminated_Overflow_Check (N) then
8830 Apply_Arithmetic_Overflow_Check (N);
8831 return;
8832 end if;
8833
8834 if Is_Integer_Type (Etype (N)) then
8835 Apply_Divide_Checks (N);
8836
8837 -- All done if we don't have a REM any more, which can happen as a
8838 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8839
8840 if Nkind (N) /= N_Op_Rem then
8841 return;
8842 end if;
8843 end if;
8844
8845 -- Proceed with expansion of REM
8846
8847 Left := Left_Opnd (N);
8848 Right := Right_Opnd (N);
8849
8850 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
8851 -- but it is useful with other back ends (e.g. AAMP), and is certainly
8852 -- harmless.
8853
8854 if Is_Integer_Type (Etype (N))
8855 and then Compile_Time_Known_Value (Right)
8856 and then Expr_Value (Right) = Uint_1
8857 then
8858 -- Call Remove_Side_Effects to ensure that any side effects in the
8859 -- ignored left operand (in particular function calls to user defined
8860 -- functions) are properly executed.
8861
8862 Remove_Side_Effects (Left);
8863
8864 Rewrite (N, Make_Integer_Literal (Loc, 0));
8865 Analyze_And_Resolve (N, Typ);
8866 return;
8867 end if;
8868
8869 -- Deal with annoying case of largest negative number remainder minus
8870 -- one. Gigi may not handle this case correctly, because on some
8871 -- targets, the mod value is computed using a divide instruction
8872 -- which gives an overflow trap for this case.
8873
8874 -- It would be a bit more efficient to figure out which targets this
8875 -- is really needed for, but in practice it is reasonable to do the
8876 -- following special check in all cases, since it means we get a clearer
8877 -- message, and also the overhead is minimal given that division is
8878 -- expensive in any case.
8879
8880 -- In fact the check is quite easy, if the right operand is -1, then
8881 -- the remainder is always 0, and we can just ignore the left operand
8882 -- completely in this case.
8883
8884 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
8885 Lneg := (not OK) or else Lo < 0;
8886
8887 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
8888 Rneg := (not OK) or else Lo < 0;
8889
8890 -- We won't mess with trying to find out if the left operand can really
8891 -- be the largest negative number (that's a pain in the case of private
8892 -- types and this is really marginal). We will just assume that we need
8893 -- the test if the left operand can be negative at all.
8894
8895 if Lneg and Rneg then
8896 Rewrite (N,
8897 Make_If_Expression (Loc,
8898 Expressions => New_List (
8899 Make_Op_Eq (Loc,
8900 Left_Opnd => Duplicate_Subexpr (Right),
8901 Right_Opnd =>
8902 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
8903
8904 Unchecked_Convert_To (Typ,
8905 Make_Integer_Literal (Loc, Uint_0)),
8906
8907 Relocate_Node (N))));
8908
8909 Set_Analyzed (Next (Next (First (Expressions (N)))));
8910 Analyze_And_Resolve (N, Typ);
8911 end if;
8912 end Expand_N_Op_Rem;
8913
8914 -----------------------------
8915 -- Expand_N_Op_Rotate_Left --
8916 -----------------------------
8917
8918 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
8919 begin
8920 Binary_Op_Validity_Checks (N);
8921
8922 -- If we are in Modify_Tree_For_C mode, there is no rotate left in C,
8923 -- so we rewrite in terms of logical shifts
8924
8925 -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits)
8926
8927 -- where Bits is the shift count mod Esize (the mod operation here
8928 -- deals with ludicrous large shift counts, which are apparently OK).
8929
8930 -- What about non-binary modulus ???
8931
8932 declare
8933 Loc : constant Source_Ptr := Sloc (N);
8934 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
8935 Typ : constant Entity_Id := Etype (N);
8936
8937 begin
8938 if Modify_Tree_For_C then
8939 Rewrite (Right_Opnd (N),
8940 Make_Op_Rem (Loc,
8941 Left_Opnd => Relocate_Node (Right_Opnd (N)),
8942 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
8943
8944 Analyze_And_Resolve (Right_Opnd (N), Rtp);
8945
8946 Rewrite (N,
8947 Make_Op_Or (Loc,
8948 Left_Opnd =>
8949 Make_Op_Shift_Left (Loc,
8950 Left_Opnd => Left_Opnd (N),
8951 Right_Opnd => Right_Opnd (N)),
8952
8953 Right_Opnd =>
8954 Make_Op_Shift_Right (Loc,
8955 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
8956 Right_Opnd =>
8957 Make_Op_Subtract (Loc,
8958 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
8959 Right_Opnd =>
8960 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
8961
8962 Analyze_And_Resolve (N, Typ);
8963 end if;
8964 end;
8965 end Expand_N_Op_Rotate_Left;
8966
8967 ------------------------------
8968 -- Expand_N_Op_Rotate_Right --
8969 ------------------------------
8970
8971 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
8972 begin
8973 Binary_Op_Validity_Checks (N);
8974
8975 -- If we are in Modify_Tree_For_C mode, there is no rotate right in C,
8976 -- so we rewrite in terms of logical shifts
8977
8978 -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits)
8979
8980 -- where Bits is the shift count mod Esize (the mod operation here
8981 -- deals with ludicrous large shift counts, which are apparently OK).
8982
8983 -- What about non-binary modulus ???
8984
8985 declare
8986 Loc : constant Source_Ptr := Sloc (N);
8987 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
8988 Typ : constant Entity_Id := Etype (N);
8989
8990 begin
8991 Rewrite (Right_Opnd (N),
8992 Make_Op_Rem (Loc,
8993 Left_Opnd => Relocate_Node (Right_Opnd (N)),
8994 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
8995
8996 Analyze_And_Resolve (Right_Opnd (N), Rtp);
8997
8998 if Modify_Tree_For_C then
8999 Rewrite (N,
9000 Make_Op_Or (Loc,
9001 Left_Opnd =>
9002 Make_Op_Shift_Right (Loc,
9003 Left_Opnd => Left_Opnd (N),
9004 Right_Opnd => Right_Opnd (N)),
9005
9006 Right_Opnd =>
9007 Make_Op_Shift_Left (Loc,
9008 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
9009 Right_Opnd =>
9010 Make_Op_Subtract (Loc,
9011 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
9012 Right_Opnd =>
9013 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
9014
9015 Analyze_And_Resolve (N, Typ);
9016 end if;
9017 end;
9018 end Expand_N_Op_Rotate_Right;
9019
9020 ----------------------------
9021 -- Expand_N_Op_Shift_Left --
9022 ----------------------------
9023
9024 -- Note: nothing in this routine depends on left as opposed to right shifts
9025 -- so we share the routine for expanding shift right operations.
9026
9027 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
9028 begin
9029 Binary_Op_Validity_Checks (N);
9030
9031 -- If we are in Modify_Tree_For_C mode, then ensure that the right
9032 -- operand is not greater than the word size (since that would not
9033 -- be defined properly by the corresponding C shift operator).
9034
9035 if Modify_Tree_For_C then
9036 declare
9037 Right : constant Node_Id := Right_Opnd (N);
9038 Loc : constant Source_Ptr := Sloc (Right);
9039 Typ : constant Entity_Id := Etype (N);
9040 Siz : constant Uint := Esize (Typ);
9041 Orig : Node_Id;
9042 OK : Boolean;
9043 Lo : Uint;
9044 Hi : Uint;
9045
9046 begin
9047 if Compile_Time_Known_Value (Right) then
9048 if Expr_Value (Right) >= Siz then
9049 Rewrite (N, Make_Integer_Literal (Loc, 0));
9050 Analyze_And_Resolve (N, Typ);
9051 end if;
9052
9053 -- Not compile time known, find range
9054
9055 else
9056 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
9057
9058 -- Nothing to do if known to be OK range, otherwise expand
9059
9060 if not OK or else Hi >= Siz then
9061
9062 -- Prevent recursion on copy of shift node
9063
9064 Orig := Relocate_Node (N);
9065 Set_Analyzed (Orig);
9066
9067 -- Now do the rewrite
9068
9069 Rewrite (N,
9070 Make_If_Expression (Loc,
9071 Expressions => New_List (
9072 Make_Op_Ge (Loc,
9073 Left_Opnd => Duplicate_Subexpr_Move_Checks (Right),
9074 Right_Opnd => Make_Integer_Literal (Loc, Siz)),
9075 Make_Integer_Literal (Loc, 0),
9076 Orig)));
9077 Analyze_And_Resolve (N, Typ);
9078 end if;
9079 end if;
9080 end;
9081 end if;
9082 end Expand_N_Op_Shift_Left;
9083
9084 -----------------------------
9085 -- Expand_N_Op_Shift_Right --
9086 -----------------------------
9087
9088 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
9089 begin
9090 -- Share shift left circuit
9091
9092 Expand_N_Op_Shift_Left (N);
9093 end Expand_N_Op_Shift_Right;
9094
9095 ----------------------------------------
9096 -- Expand_N_Op_Shift_Right_Arithmetic --
9097 ----------------------------------------
9098
9099 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
9100 begin
9101 Binary_Op_Validity_Checks (N);
9102
9103 -- If we are in Modify_Tree_For_C mode, there is no shift right
9104 -- arithmetic in C, so we rewrite in terms of logical shifts.
9105
9106 -- Shift_Right (Num, Bits) or
9107 -- (if Num >= Sign
9108 -- then not (Shift_Right (Mask, bits))
9109 -- else 0)
9110
9111 -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1)
9112
9113 -- Note: in almost all C compilers it would work to just shift a
9114 -- signed integer right, but it's undefined and we cannot rely on it.
9115
9116 -- Note: the above works fine for shift counts greater than or equal
9117 -- to the word size, since in this case (not (Shift_Right (Mask, bits)))
9118 -- generates all 1'bits.
9119
9120 -- What about non-binary modulus ???
9121
9122 declare
9123 Loc : constant Source_Ptr := Sloc (N);
9124 Typ : constant Entity_Id := Etype (N);
9125 Sign : constant Uint := 2 ** (Esize (Typ) - 1);
9126 Mask : constant Uint := (2 ** Esize (Typ)) - 1;
9127 Left : constant Node_Id := Left_Opnd (N);
9128 Right : constant Node_Id := Right_Opnd (N);
9129 Maskx : Node_Id;
9130
9131 begin
9132 if Modify_Tree_For_C then
9133
9134 -- Here if not (Shift_Right (Mask, bits)) can be computed at
9135 -- compile time as a single constant.
9136
9137 if Compile_Time_Known_Value (Right) then
9138 declare
9139 Val : constant Uint := Expr_Value (Right);
9140
9141 begin
9142 if Val >= Esize (Typ) then
9143 Maskx := Make_Integer_Literal (Loc, Mask);
9144
9145 else
9146 Maskx :=
9147 Make_Integer_Literal (Loc,
9148 Intval => Mask - (Mask / (2 ** Expr_Value (Right))));
9149 end if;
9150 end;
9151
9152 else
9153 Maskx :=
9154 Make_Op_Not (Loc,
9155 Right_Opnd =>
9156 Make_Op_Shift_Right (Loc,
9157 Left_Opnd => Make_Integer_Literal (Loc, Mask),
9158 Right_Opnd => Duplicate_Subexpr_No_Checks (Right)));
9159 end if;
9160
9161 -- Now do the rewrite
9162
9163 Rewrite (N,
9164 Make_Op_Or (Loc,
9165 Left_Opnd =>
9166 Make_Op_Shift_Right (Loc,
9167 Left_Opnd => Left,
9168 Right_Opnd => Right),
9169 Right_Opnd =>
9170 Make_If_Expression (Loc,
9171 Expressions => New_List (
9172 Make_Op_Ge (Loc,
9173 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
9174 Right_Opnd => Make_Integer_Literal (Loc, Sign)),
9175 Maskx,
9176 Make_Integer_Literal (Loc, 0)))));
9177 Analyze_And_Resolve (N, Typ);
9178 end if;
9179 end;
9180 end Expand_N_Op_Shift_Right_Arithmetic;
9181
9182 --------------------------
9183 -- Expand_N_Op_Subtract --
9184 --------------------------
9185
9186 procedure Expand_N_Op_Subtract (N : Node_Id) is
9187 Typ : constant Entity_Id := Etype (N);
9188
9189 begin
9190 Binary_Op_Validity_Checks (N);
9191
9192 -- Check for MINIMIZED/ELIMINATED overflow mode
9193
9194 if Minimized_Eliminated_Overflow_Check (N) then
9195 Apply_Arithmetic_Overflow_Check (N);
9196 return;
9197 end if;
9198
9199 -- N - 0 = N for integer types
9200
9201 if Is_Integer_Type (Typ)
9202 and then Compile_Time_Known_Value (Right_Opnd (N))
9203 and then Expr_Value (Right_Opnd (N)) = 0
9204 then
9205 Rewrite (N, Left_Opnd (N));
9206 return;
9207 end if;
9208
9209 -- Arithmetic overflow checks for signed integer/fixed point types
9210
9211 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
9212 Apply_Arithmetic_Overflow_Check (N);
9213
9214 -- VAX floating-point types case
9215
9216 elsif Vax_Float (Typ) then
9217 Expand_Vax_Arith (N);
9218 end if;
9219 end Expand_N_Op_Subtract;
9220
9221 ---------------------
9222 -- Expand_N_Op_Xor --
9223 ---------------------
9224
9225 procedure Expand_N_Op_Xor (N : Node_Id) is
9226 Typ : constant Entity_Id := Etype (N);
9227
9228 begin
9229 Binary_Op_Validity_Checks (N);
9230
9231 if Is_Array_Type (Etype (N)) then
9232 Expand_Boolean_Operator (N);
9233
9234 elsif Is_Boolean_Type (Etype (N)) then
9235 Adjust_Condition (Left_Opnd (N));
9236 Adjust_Condition (Right_Opnd (N));
9237 Set_Etype (N, Standard_Boolean);
9238 Adjust_Result_Type (N, Typ);
9239
9240 elsif Is_Intrinsic_Subprogram (Entity (N)) then
9241 Expand_Intrinsic_Call (N, Entity (N));
9242
9243 end if;
9244 end Expand_N_Op_Xor;
9245
9246 ----------------------
9247 -- Expand_N_Or_Else --
9248 ----------------------
9249
9250 procedure Expand_N_Or_Else (N : Node_Id)
9251 renames Expand_Short_Circuit_Operator;
9252
9253 -----------------------------------
9254 -- Expand_N_Qualified_Expression --
9255 -----------------------------------
9256
9257 procedure Expand_N_Qualified_Expression (N : Node_Id) is
9258 Operand : constant Node_Id := Expression (N);
9259 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
9260
9261 begin
9262 -- Do validity check if validity checking operands
9263
9264 if Validity_Checks_On and Validity_Check_Operands then
9265 Ensure_Valid (Operand);
9266 end if;
9267
9268 -- Apply possible constraint check
9269
9270 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
9271
9272 if Do_Range_Check (Operand) then
9273 Set_Do_Range_Check (Operand, False);
9274 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
9275 end if;
9276 end Expand_N_Qualified_Expression;
9277
9278 ------------------------------------
9279 -- Expand_N_Quantified_Expression --
9280 ------------------------------------
9281
9282 -- We expand:
9283
9284 -- for all X in range => Cond
9285
9286 -- into:
9287
9288 -- T := True;
9289 -- for X in range loop
9290 -- if not Cond then
9291 -- T := False;
9292 -- exit;
9293 -- end if;
9294 -- end loop;
9295
9296 -- Similarly, an existentially quantified expression:
9297
9298 -- for some X in range => Cond
9299
9300 -- becomes:
9301
9302 -- T := False;
9303 -- for X in range loop
9304 -- if Cond then
9305 -- T := True;
9306 -- exit;
9307 -- end if;
9308 -- end loop;
9309
9310 -- In both cases, the iteration may be over a container in which case it is
9311 -- given by an iterator specification, not a loop parameter specification.
9312
9313 procedure Expand_N_Quantified_Expression (N : Node_Id) is
9314 Actions : constant List_Id := New_List;
9315 For_All : constant Boolean := All_Present (N);
9316 Iter_Spec : constant Node_Id := Iterator_Specification (N);
9317 Loc : constant Source_Ptr := Sloc (N);
9318 Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N);
9319 Cond : Node_Id;
9320 Flag : Entity_Id;
9321 Scheme : Node_Id;
9322 Stmts : List_Id;
9323
9324 begin
9325 -- Create the declaration of the flag which tracks the status of the
9326 -- quantified expression. Generate:
9327
9328 -- Flag : Boolean := (True | False);
9329
9330 Flag := Make_Temporary (Loc, 'T', N);
9331
9332 Append_To (Actions,
9333 Make_Object_Declaration (Loc,
9334 Defining_Identifier => Flag,
9335 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
9336 Expression =>
9337 New_Occurrence_Of (Boolean_Literals (For_All), Loc)));
9338
9339 -- Construct the circuitry which tracks the status of the quantified
9340 -- expression. Generate:
9341
9342 -- if [not] Cond then
9343 -- Flag := (False | True);
9344 -- exit;
9345 -- end if;
9346
9347 Cond := Relocate_Node (Condition (N));
9348
9349 if For_All then
9350 Cond := Make_Op_Not (Loc, Cond);
9351 end if;
9352
9353 Stmts := New_List (
9354 Make_Implicit_If_Statement (N,
9355 Condition => Cond,
9356 Then_Statements => New_List (
9357 Make_Assignment_Statement (Loc,
9358 Name => New_Occurrence_Of (Flag, Loc),
9359 Expression =>
9360 New_Occurrence_Of (Boolean_Literals (not For_All), Loc)),
9361 Make_Exit_Statement (Loc))));
9362
9363 -- Build the loop equivalent of the quantified expression
9364
9365 if Present (Iter_Spec) then
9366 Scheme :=
9367 Make_Iteration_Scheme (Loc,
9368 Iterator_Specification => Iter_Spec);
9369 else
9370 Scheme :=
9371 Make_Iteration_Scheme (Loc,
9372 Loop_Parameter_Specification => Loop_Spec);
9373 end if;
9374
9375 Append_To (Actions,
9376 Make_Loop_Statement (Loc,
9377 Iteration_Scheme => Scheme,
9378 Statements => Stmts,
9379 End_Label => Empty));
9380
9381 -- Transform the quantified expression
9382
9383 Rewrite (N,
9384 Make_Expression_With_Actions (Loc,
9385 Expression => New_Occurrence_Of (Flag, Loc),
9386 Actions => Actions));
9387 Analyze_And_Resolve (N, Standard_Boolean);
9388 end Expand_N_Quantified_Expression;
9389
9390 ---------------------------------
9391 -- Expand_N_Selected_Component --
9392 ---------------------------------
9393
9394 procedure Expand_N_Selected_Component (N : Node_Id) is
9395 Loc : constant Source_Ptr := Sloc (N);
9396 Par : constant Node_Id := Parent (N);
9397 P : constant Node_Id := Prefix (N);
9398 S : constant Node_Id := Selector_Name (N);
9399 Ptyp : Entity_Id := Underlying_Type (Etype (P));
9400 Disc : Entity_Id;
9401 New_N : Node_Id;
9402 Dcon : Elmt_Id;
9403 Dval : Node_Id;
9404
9405 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
9406 -- Gigi needs a temporary for prefixes that depend on a discriminant,
9407 -- unless the context of an assignment can provide size information.
9408 -- Don't we have a general routine that does this???
9409
9410 function Is_Subtype_Declaration return Boolean;
9411 -- The replacement of a discriminant reference by its value is required
9412 -- if this is part of the initialization of an temporary generated by a
9413 -- change of representation. This shows up as the construction of a
9414 -- discriminant constraint for a subtype declared at the same point as
9415 -- the entity in the prefix of the selected component. We recognize this
9416 -- case when the context of the reference is:
9417 -- subtype ST is T(Obj.D);
9418 -- where the entity for Obj comes from source, and ST has the same sloc.
9419
9420 -----------------------
9421 -- In_Left_Hand_Side --
9422 -----------------------
9423
9424 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
9425 begin
9426 return (Nkind (Parent (Comp)) = N_Assignment_Statement
9427 and then Comp = Name (Parent (Comp)))
9428 or else (Present (Parent (Comp))
9429 and then Nkind (Parent (Comp)) in N_Subexpr
9430 and then In_Left_Hand_Side (Parent (Comp)));
9431 end In_Left_Hand_Side;
9432
9433 -----------------------------
9434 -- Is_Subtype_Declaration --
9435 -----------------------------
9436
9437 function Is_Subtype_Declaration return Boolean is
9438 Par : constant Node_Id := Parent (N);
9439 begin
9440 return
9441 Nkind (Par) = N_Index_Or_Discriminant_Constraint
9442 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
9443 and then Comes_From_Source (Entity (Prefix (N)))
9444 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
9445 end Is_Subtype_Declaration;
9446
9447 -- Start of processing for Expand_N_Selected_Component
9448
9449 begin
9450 -- Insert explicit dereference if required
9451
9452 if Is_Access_Type (Ptyp) then
9453
9454 -- First set prefix type to proper access type, in case it currently
9455 -- has a private (non-access) view of this type.
9456
9457 Set_Etype (P, Ptyp);
9458
9459 Insert_Explicit_Dereference (P);
9460 Analyze_And_Resolve (P, Designated_Type (Ptyp));
9461
9462 if Ekind (Etype (P)) = E_Private_Subtype
9463 and then Is_For_Access_Subtype (Etype (P))
9464 then
9465 Set_Etype (P, Base_Type (Etype (P)));
9466 end if;
9467
9468 Ptyp := Etype (P);
9469 end if;
9470
9471 -- Deal with discriminant check required
9472
9473 if Do_Discriminant_Check (N) then
9474 if Present (Discriminant_Checking_Func
9475 (Original_Record_Component (Entity (S))))
9476 then
9477 -- Present the discriminant checking function to the backend, so
9478 -- that it can inline the call to the function.
9479
9480 Add_Inlined_Body
9481 (Discriminant_Checking_Func
9482 (Original_Record_Component (Entity (S))));
9483
9484 -- Now reset the flag and generate the call
9485
9486 Set_Do_Discriminant_Check (N, False);
9487 Generate_Discriminant_Check (N);
9488
9489 -- In the case of Unchecked_Union, no discriminant checking is
9490 -- actually performed.
9491
9492 else
9493 Set_Do_Discriminant_Check (N, False);
9494 end if;
9495 end if;
9496
9497 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9498 -- function, then additional actuals must be passed.
9499
9500 if Ada_Version >= Ada_2005
9501 and then Is_Build_In_Place_Function_Call (P)
9502 then
9503 Make_Build_In_Place_Call_In_Anonymous_Context (P);
9504 end if;
9505
9506 -- Gigi cannot handle unchecked conversions that are the prefix of a
9507 -- selected component with discriminants. This must be checked during
9508 -- expansion, because during analysis the type of the selector is not
9509 -- known at the point the prefix is analyzed. If the conversion is the
9510 -- target of an assignment, then we cannot force the evaluation.
9511
9512 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
9513 and then Has_Discriminants (Etype (N))
9514 and then not In_Left_Hand_Side (N)
9515 then
9516 Force_Evaluation (Prefix (N));
9517 end if;
9518
9519 -- Remaining processing applies only if selector is a discriminant
9520
9521 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
9522
9523 -- If the selector is a discriminant of a constrained record type,
9524 -- we may be able to rewrite the expression with the actual value
9525 -- of the discriminant, a useful optimization in some cases.
9526
9527 if Is_Record_Type (Ptyp)
9528 and then Has_Discriminants (Ptyp)
9529 and then Is_Constrained (Ptyp)
9530 then
9531 -- Do this optimization for discrete types only, and not for
9532 -- access types (access discriminants get us into trouble).
9533
9534 if not Is_Discrete_Type (Etype (N)) then
9535 null;
9536
9537 -- Don't do this on the left hand of an assignment statement.
9538 -- Normally one would think that references like this would not
9539 -- occur, but they do in generated code, and mean that we really
9540 -- do want to assign the discriminant.
9541
9542 elsif Nkind (Par) = N_Assignment_Statement
9543 and then Name (Par) = N
9544 then
9545 null;
9546
9547 -- Don't do this optimization for the prefix of an attribute or
9548 -- the name of an object renaming declaration since these are
9549 -- contexts where we do not want the value anyway.
9550
9551 elsif (Nkind (Par) = N_Attribute_Reference
9552 and then Prefix (Par) = N)
9553 or else Is_Renamed_Object (N)
9554 then
9555 null;
9556
9557 -- Don't do this optimization if we are within the code for a
9558 -- discriminant check, since the whole point of such a check may
9559 -- be to verify the condition on which the code below depends.
9560
9561 elsif Is_In_Discriminant_Check (N) then
9562 null;
9563
9564 -- Green light to see if we can do the optimization. There is
9565 -- still one condition that inhibits the optimization below but
9566 -- now is the time to check the particular discriminant.
9567
9568 else
9569 -- Loop through discriminants to find the matching discriminant
9570 -- constraint to see if we can copy it.
9571
9572 Disc := First_Discriminant (Ptyp);
9573 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
9574 Discr_Loop : while Present (Dcon) loop
9575 Dval := Node (Dcon);
9576
9577 -- Check if this is the matching discriminant and if the
9578 -- discriminant value is simple enough to make sense to
9579 -- copy. We don't want to copy complex expressions, and
9580 -- indeed to do so can cause trouble (before we put in
9581 -- this guard, a discriminant expression containing an
9582 -- AND THEN was copied, causing problems for coverage
9583 -- analysis tools).
9584
9585 -- However, if the reference is part of the initialization
9586 -- code generated for an object declaration, we must use
9587 -- the discriminant value from the subtype constraint,
9588 -- because the selected component may be a reference to the
9589 -- object being initialized, whose discriminant is not yet
9590 -- set. This only happens in complex cases involving changes
9591 -- or representation.
9592
9593 if Disc = Entity (Selector_Name (N))
9594 and then (Is_Entity_Name (Dval)
9595 or else Compile_Time_Known_Value (Dval)
9596 or else Is_Subtype_Declaration)
9597 then
9598 -- Here we have the matching discriminant. Check for
9599 -- the case of a discriminant of a component that is
9600 -- constrained by an outer discriminant, which cannot
9601 -- be optimized away.
9602
9603 if Denotes_Discriminant
9604 (Dval, Check_Concurrent => True)
9605 then
9606 exit Discr_Loop;
9607
9608 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
9609 and then
9610 Denotes_Discriminant
9611 (Selector_Name (Original_Node (Dval)), True)
9612 then
9613 exit Discr_Loop;
9614
9615 -- Do not retrieve value if constraint is not static. It
9616 -- is generally not useful, and the constraint may be a
9617 -- rewritten outer discriminant in which case it is in
9618 -- fact incorrect.
9619
9620 elsif Is_Entity_Name (Dval)
9621 and then
9622 Nkind (Parent (Entity (Dval))) = N_Object_Declaration
9623 and then Present (Expression (Parent (Entity (Dval))))
9624 and then not
9625 Is_Static_Expression
9626 (Expression (Parent (Entity (Dval))))
9627 then
9628 exit Discr_Loop;
9629
9630 -- In the context of a case statement, the expression may
9631 -- have the base type of the discriminant, and we need to
9632 -- preserve the constraint to avoid spurious errors on
9633 -- missing cases.
9634
9635 elsif Nkind (Parent (N)) = N_Case_Statement
9636 and then Etype (Dval) /= Etype (Disc)
9637 then
9638 Rewrite (N,
9639 Make_Qualified_Expression (Loc,
9640 Subtype_Mark =>
9641 New_Occurrence_Of (Etype (Disc), Loc),
9642 Expression =>
9643 New_Copy_Tree (Dval)));
9644 Analyze_And_Resolve (N, Etype (Disc));
9645
9646 -- In case that comes out as a static expression,
9647 -- reset it (a selected component is never static).
9648
9649 Set_Is_Static_Expression (N, False);
9650 return;
9651
9652 -- Otherwise we can just copy the constraint, but the
9653 -- result is certainly not static. In some cases the
9654 -- discriminant constraint has been analyzed in the
9655 -- context of the original subtype indication, but for
9656 -- itypes the constraint might not have been analyzed
9657 -- yet, and this must be done now.
9658
9659 else
9660 Rewrite (N, New_Copy_Tree (Dval));
9661 Analyze_And_Resolve (N);
9662 Set_Is_Static_Expression (N, False);
9663 return;
9664 end if;
9665 end if;
9666
9667 Next_Elmt (Dcon);
9668 Next_Discriminant (Disc);
9669 end loop Discr_Loop;
9670
9671 -- Note: the above loop should always find a matching
9672 -- discriminant, but if it does not, we just missed an
9673 -- optimization due to some glitch (perhaps a previous
9674 -- error), so ignore.
9675
9676 end if;
9677 end if;
9678
9679 -- The only remaining processing is in the case of a discriminant of
9680 -- a concurrent object, where we rewrite the prefix to denote the
9681 -- corresponding record type. If the type is derived and has renamed
9682 -- discriminants, use corresponding discriminant, which is the one
9683 -- that appears in the corresponding record.
9684
9685 if not Is_Concurrent_Type (Ptyp) then
9686 return;
9687 end if;
9688
9689 Disc := Entity (Selector_Name (N));
9690
9691 if Is_Derived_Type (Ptyp)
9692 and then Present (Corresponding_Discriminant (Disc))
9693 then
9694 Disc := Corresponding_Discriminant (Disc);
9695 end if;
9696
9697 New_N :=
9698 Make_Selected_Component (Loc,
9699 Prefix =>
9700 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
9701 New_Copy_Tree (P)),
9702 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
9703
9704 Rewrite (N, New_N);
9705 Analyze (N);
9706 end if;
9707
9708 -- Set Atomic_Sync_Required if necessary for atomic component
9709
9710 if Nkind (N) = N_Selected_Component then
9711 declare
9712 E : constant Entity_Id := Entity (Selector_Name (N));
9713 Set : Boolean;
9714
9715 begin
9716 -- If component is atomic, but type is not, setting depends on
9717 -- disable/enable state for the component.
9718
9719 if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then
9720 Set := not Atomic_Synchronization_Disabled (E);
9721
9722 -- If component is not atomic, but its type is atomic, setting
9723 -- depends on disable/enable state for the type.
9724
9725 elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9726 Set := not Atomic_Synchronization_Disabled (Etype (E));
9727
9728 -- If both component and type are atomic, we disable if either
9729 -- component or its type have sync disabled.
9730
9731 elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9732 Set := (not Atomic_Synchronization_Disabled (E))
9733 and then
9734 (not Atomic_Synchronization_Disabled (Etype (E)));
9735
9736 else
9737 Set := False;
9738 end if;
9739
9740 -- Set flag if required
9741
9742 if Set then
9743 Activate_Atomic_Synchronization (N);
9744 end if;
9745 end;
9746 end if;
9747 end Expand_N_Selected_Component;
9748
9749 --------------------
9750 -- Expand_N_Slice --
9751 --------------------
9752
9753 procedure Expand_N_Slice (N : Node_Id) is
9754 Loc : constant Source_Ptr := Sloc (N);
9755 Typ : constant Entity_Id := Etype (N);
9756
9757 function Is_Procedure_Actual (N : Node_Id) return Boolean;
9758 -- Check whether the argument is an actual for a procedure call, in
9759 -- which case the expansion of a bit-packed slice is deferred until the
9760 -- call itself is expanded. The reason this is required is that we might
9761 -- have an IN OUT or OUT parameter, and the copy out is essential, and
9762 -- that copy out would be missed if we created a temporary here in
9763 -- Expand_N_Slice. Note that we don't bother to test specifically for an
9764 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
9765 -- is harmless to defer expansion in the IN case, since the call
9766 -- processing will still generate the appropriate copy in operation,
9767 -- which will take care of the slice.
9768
9769 procedure Make_Temporary_For_Slice;
9770 -- Create a named variable for the value of the slice, in cases where
9771 -- the back-end cannot handle it properly, e.g. when packed types or
9772 -- unaligned slices are involved.
9773
9774 -------------------------
9775 -- Is_Procedure_Actual --
9776 -------------------------
9777
9778 function Is_Procedure_Actual (N : Node_Id) return Boolean is
9779 Par : Node_Id := Parent (N);
9780
9781 begin
9782 loop
9783 -- If our parent is a procedure call we can return
9784
9785 if Nkind (Par) = N_Procedure_Call_Statement then
9786 return True;
9787
9788 -- If our parent is a type conversion, keep climbing the tree,
9789 -- since a type conversion can be a procedure actual. Also keep
9790 -- climbing if parameter association or a qualified expression,
9791 -- since these are additional cases that do can appear on
9792 -- procedure actuals.
9793
9794 elsif Nkind_In (Par, N_Type_Conversion,
9795 N_Parameter_Association,
9796 N_Qualified_Expression)
9797 then
9798 Par := Parent (Par);
9799
9800 -- Any other case is not what we are looking for
9801
9802 else
9803 return False;
9804 end if;
9805 end loop;
9806 end Is_Procedure_Actual;
9807
9808 ------------------------------
9809 -- Make_Temporary_For_Slice --
9810 ------------------------------
9811
9812 procedure Make_Temporary_For_Slice is
9813 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
9814 Decl : Node_Id;
9815
9816 begin
9817 Decl :=
9818 Make_Object_Declaration (Loc,
9819 Defining_Identifier => Ent,
9820 Object_Definition => New_Occurrence_Of (Typ, Loc));
9821
9822 Set_No_Initialization (Decl);
9823
9824 Insert_Actions (N, New_List (
9825 Decl,
9826 Make_Assignment_Statement (Loc,
9827 Name => New_Occurrence_Of (Ent, Loc),
9828 Expression => Relocate_Node (N))));
9829
9830 Rewrite (N, New_Occurrence_Of (Ent, Loc));
9831 Analyze_And_Resolve (N, Typ);
9832 end Make_Temporary_For_Slice;
9833
9834 -- Local variables
9835
9836 Pref : constant Node_Id := Prefix (N);
9837 Pref_Typ : Entity_Id := Etype (Pref);
9838
9839 -- Start of processing for Expand_N_Slice
9840
9841 begin
9842 -- Special handling for access types
9843
9844 if Is_Access_Type (Pref_Typ) then
9845 Pref_Typ := Designated_Type (Pref_Typ);
9846
9847 Rewrite (Pref,
9848 Make_Explicit_Dereference (Sloc (N),
9849 Prefix => Relocate_Node (Pref)));
9850
9851 Analyze_And_Resolve (Pref, Pref_Typ);
9852 end if;
9853
9854 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9855 -- function, then additional actuals must be passed.
9856
9857 if Ada_Version >= Ada_2005
9858 and then Is_Build_In_Place_Function_Call (Pref)
9859 then
9860 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
9861 end if;
9862
9863 -- The remaining case to be handled is packed slices. We can leave
9864 -- packed slices as they are in the following situations:
9865
9866 -- 1. Right or left side of an assignment (we can handle this
9867 -- situation correctly in the assignment statement expansion).
9868
9869 -- 2. Prefix of indexed component (the slide is optimized away in this
9870 -- case, see the start of Expand_N_Slice.)
9871
9872 -- 3. Object renaming declaration, since we want the name of the
9873 -- slice, not the value.
9874
9875 -- 4. Argument to procedure call, since copy-in/copy-out handling may
9876 -- be required, and this is handled in the expansion of call
9877 -- itself.
9878
9879 -- 5. Prefix of an address attribute (this is an error which is caught
9880 -- elsewhere, and the expansion would interfere with generating the
9881 -- error message).
9882
9883 if not Is_Packed (Typ) then
9884
9885 -- Apply transformation for actuals of a function call, where
9886 -- Expand_Actuals is not used.
9887
9888 if Nkind (Parent (N)) = N_Function_Call
9889 and then Is_Possibly_Unaligned_Slice (N)
9890 then
9891 Make_Temporary_For_Slice;
9892 end if;
9893
9894 elsif Nkind (Parent (N)) = N_Assignment_Statement
9895 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
9896 and then Parent (N) = Name (Parent (Parent (N))))
9897 then
9898 return;
9899
9900 elsif Nkind (Parent (N)) = N_Indexed_Component
9901 or else Is_Renamed_Object (N)
9902 or else Is_Procedure_Actual (N)
9903 then
9904 return;
9905
9906 elsif Nkind (Parent (N)) = N_Attribute_Reference
9907 and then Attribute_Name (Parent (N)) = Name_Address
9908 then
9909 return;
9910
9911 else
9912 Make_Temporary_For_Slice;
9913 end if;
9914 end Expand_N_Slice;
9915
9916 ------------------------------
9917 -- Expand_N_Type_Conversion --
9918 ------------------------------
9919
9920 procedure Expand_N_Type_Conversion (N : Node_Id) is
9921 Loc : constant Source_Ptr := Sloc (N);
9922 Operand : constant Node_Id := Expression (N);
9923 Target_Type : constant Entity_Id := Etype (N);
9924 Operand_Type : Entity_Id := Etype (Operand);
9925
9926 procedure Handle_Changed_Representation;
9927 -- This is called in the case of record and array type conversions to
9928 -- see if there is a change of representation to be handled. Change of
9929 -- representation is actually handled at the assignment statement level,
9930 -- and what this procedure does is rewrite node N conversion as an
9931 -- assignment to temporary. If there is no change of representation,
9932 -- then the conversion node is unchanged.
9933
9934 procedure Raise_Accessibility_Error;
9935 -- Called when we know that an accessibility check will fail. Rewrites
9936 -- node N to an appropriate raise statement and outputs warning msgs.
9937 -- The Etype of the raise node is set to Target_Type.
9938
9939 procedure Real_Range_Check;
9940 -- Handles generation of range check for real target value
9941
9942 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean;
9943 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
9944 -- evaluates to True.
9945
9946 -----------------------------------
9947 -- Handle_Changed_Representation --
9948 -----------------------------------
9949
9950 procedure Handle_Changed_Representation is
9951 Temp : Entity_Id;
9952 Decl : Node_Id;
9953 Odef : Node_Id;
9954 Disc : Node_Id;
9955 N_Ix : Node_Id;
9956 Cons : List_Id;
9957
9958 begin
9959 -- Nothing else to do if no change of representation
9960
9961 if Same_Representation (Operand_Type, Target_Type) then
9962 return;
9963
9964 -- The real change of representation work is done by the assignment
9965 -- statement processing. So if this type conversion is appearing as
9966 -- the expression of an assignment statement, nothing needs to be
9967 -- done to the conversion.
9968
9969 elsif Nkind (Parent (N)) = N_Assignment_Statement then
9970 return;
9971
9972 -- Otherwise we need to generate a temporary variable, and do the
9973 -- change of representation assignment into that temporary variable.
9974 -- The conversion is then replaced by a reference to this variable.
9975
9976 else
9977 Cons := No_List;
9978
9979 -- If type is unconstrained we have to add a constraint, copied
9980 -- from the actual value of the left hand side.
9981
9982 if not Is_Constrained (Target_Type) then
9983 if Has_Discriminants (Operand_Type) then
9984 Disc := First_Discriminant (Operand_Type);
9985
9986 if Disc /= First_Stored_Discriminant (Operand_Type) then
9987 Disc := First_Stored_Discriminant (Operand_Type);
9988 end if;
9989
9990 Cons := New_List;
9991 while Present (Disc) loop
9992 Append_To (Cons,
9993 Make_Selected_Component (Loc,
9994 Prefix =>
9995 Duplicate_Subexpr_Move_Checks (Operand),
9996 Selector_Name =>
9997 Make_Identifier (Loc, Chars (Disc))));
9998 Next_Discriminant (Disc);
9999 end loop;
10000
10001 elsif Is_Array_Type (Operand_Type) then
10002 N_Ix := First_Index (Target_Type);
10003 Cons := New_List;
10004
10005 for J in 1 .. Number_Dimensions (Operand_Type) loop
10006
10007 -- We convert the bounds explicitly. We use an unchecked
10008 -- conversion because bounds checks are done elsewhere.
10009
10010 Append_To (Cons,
10011 Make_Range (Loc,
10012 Low_Bound =>
10013 Unchecked_Convert_To (Etype (N_Ix),
10014 Make_Attribute_Reference (Loc,
10015 Prefix =>
10016 Duplicate_Subexpr_No_Checks
10017 (Operand, Name_Req => True),
10018 Attribute_Name => Name_First,
10019 Expressions => New_List (
10020 Make_Integer_Literal (Loc, J)))),
10021
10022 High_Bound =>
10023 Unchecked_Convert_To (Etype (N_Ix),
10024 Make_Attribute_Reference (Loc,
10025 Prefix =>
10026 Duplicate_Subexpr_No_Checks
10027 (Operand, Name_Req => True),
10028 Attribute_Name => Name_Last,
10029 Expressions => New_List (
10030 Make_Integer_Literal (Loc, J))))));
10031
10032 Next_Index (N_Ix);
10033 end loop;
10034 end if;
10035 end if;
10036
10037 Odef := New_Occurrence_Of (Target_Type, Loc);
10038
10039 if Present (Cons) then
10040 Odef :=
10041 Make_Subtype_Indication (Loc,
10042 Subtype_Mark => Odef,
10043 Constraint =>
10044 Make_Index_Or_Discriminant_Constraint (Loc,
10045 Constraints => Cons));
10046 end if;
10047
10048 Temp := Make_Temporary (Loc, 'C');
10049 Decl :=
10050 Make_Object_Declaration (Loc,
10051 Defining_Identifier => Temp,
10052 Object_Definition => Odef);
10053
10054 Set_No_Initialization (Decl, True);
10055
10056 -- Insert required actions. It is essential to suppress checks
10057 -- since we have suppressed default initialization, which means
10058 -- that the variable we create may have no discriminants.
10059
10060 Insert_Actions (N,
10061 New_List (
10062 Decl,
10063 Make_Assignment_Statement (Loc,
10064 Name => New_Occurrence_Of (Temp, Loc),
10065 Expression => Relocate_Node (N))),
10066 Suppress => All_Checks);
10067
10068 Rewrite (N, New_Occurrence_Of (Temp, Loc));
10069 return;
10070 end if;
10071 end Handle_Changed_Representation;
10072
10073 -------------------------------
10074 -- Raise_Accessibility_Error --
10075 -------------------------------
10076
10077 procedure Raise_Accessibility_Error is
10078 begin
10079 Error_Msg_Warn := SPARK_Mode /= On;
10080 Rewrite (N,
10081 Make_Raise_Program_Error (Sloc (N),
10082 Reason => PE_Accessibility_Check_Failed));
10083 Set_Etype (N, Target_Type);
10084
10085 Error_Msg_N ("<<accessibility check failure", N);
10086 Error_Msg_NE ("\<<& [", N, Standard_Program_Error);
10087 end Raise_Accessibility_Error;
10088
10089 ----------------------
10090 -- Real_Range_Check --
10091 ----------------------
10092
10093 -- Case of conversions to floating-point or fixed-point. If range checks
10094 -- are enabled and the target type has a range constraint, we convert:
10095
10096 -- typ (x)
10097
10098 -- to
10099
10100 -- Tnn : typ'Base := typ'Base (x);
10101 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
10102 -- Tnn
10103
10104 -- This is necessary when there is a conversion of integer to float or
10105 -- to fixed-point to ensure that the correct checks are made. It is not
10106 -- necessary for float to float where it is enough to simply set the
10107 -- Do_Range_Check flag.
10108
10109 procedure Real_Range_Check is
10110 Btyp : constant Entity_Id := Base_Type (Target_Type);
10111 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
10112 Hi : constant Node_Id := Type_High_Bound (Target_Type);
10113 Xtyp : constant Entity_Id := Etype (Operand);
10114 Conv : Node_Id;
10115 Tnn : Entity_Id;
10116
10117 begin
10118 -- Nothing to do if conversion was rewritten
10119
10120 if Nkind (N) /= N_Type_Conversion then
10121 return;
10122 end if;
10123
10124 -- Nothing to do if range checks suppressed, or target has the same
10125 -- range as the base type (or is the base type).
10126
10127 if Range_Checks_Suppressed (Target_Type)
10128 or else (Lo = Type_Low_Bound (Btyp)
10129 and then
10130 Hi = Type_High_Bound (Btyp))
10131 then
10132 return;
10133 end if;
10134
10135 -- Nothing to do if expression is an entity on which checks have been
10136 -- suppressed.
10137
10138 if Is_Entity_Name (Operand)
10139 and then Range_Checks_Suppressed (Entity (Operand))
10140 then
10141 return;
10142 end if;
10143
10144 -- Nothing to do if bounds are all static and we can tell that the
10145 -- expression is within the bounds of the target. Note that if the
10146 -- operand is of an unconstrained floating-point type, then we do
10147 -- not trust it to be in range (might be infinite)
10148
10149 declare
10150 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
10151 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
10152
10153 begin
10154 if (not Is_Floating_Point_Type (Xtyp)
10155 or else Is_Constrained (Xtyp))
10156 and then Compile_Time_Known_Value (S_Lo)
10157 and then Compile_Time_Known_Value (S_Hi)
10158 and then Compile_Time_Known_Value (Hi)
10159 and then Compile_Time_Known_Value (Lo)
10160 then
10161 declare
10162 D_Lov : constant Ureal := Expr_Value_R (Lo);
10163 D_Hiv : constant Ureal := Expr_Value_R (Hi);
10164 S_Lov : Ureal;
10165 S_Hiv : Ureal;
10166
10167 begin
10168 if Is_Real_Type (Xtyp) then
10169 S_Lov := Expr_Value_R (S_Lo);
10170 S_Hiv := Expr_Value_R (S_Hi);
10171 else
10172 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
10173 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
10174 end if;
10175
10176 if D_Hiv > D_Lov
10177 and then S_Lov >= D_Lov
10178 and then S_Hiv <= D_Hiv
10179 then
10180 Set_Do_Range_Check (Operand, False);
10181 return;
10182 end if;
10183 end;
10184 end if;
10185 end;
10186
10187 -- For float to float conversions, we are done
10188
10189 if Is_Floating_Point_Type (Xtyp)
10190 and then
10191 Is_Floating_Point_Type (Btyp)
10192 then
10193 return;
10194 end if;
10195
10196 -- Otherwise rewrite the conversion as described above
10197
10198 Conv := Relocate_Node (N);
10199 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
10200 Set_Etype (Conv, Btyp);
10201
10202 -- Enable overflow except for case of integer to float conversions,
10203 -- where it is never required, since we can never have overflow in
10204 -- this case.
10205
10206 if not Is_Integer_Type (Etype (Operand)) then
10207 Enable_Overflow_Check (Conv);
10208 end if;
10209
10210 Tnn := Make_Temporary (Loc, 'T', Conv);
10211
10212 Insert_Actions (N, New_List (
10213 Make_Object_Declaration (Loc,
10214 Defining_Identifier => Tnn,
10215 Object_Definition => New_Occurrence_Of (Btyp, Loc),
10216 Constant_Present => True,
10217 Expression => Conv),
10218
10219 Make_Raise_Constraint_Error (Loc,
10220 Condition =>
10221 Make_Or_Else (Loc,
10222 Left_Opnd =>
10223 Make_Op_Lt (Loc,
10224 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
10225 Right_Opnd =>
10226 Make_Attribute_Reference (Loc,
10227 Attribute_Name => Name_First,
10228 Prefix =>
10229 New_Occurrence_Of (Target_Type, Loc))),
10230
10231 Right_Opnd =>
10232 Make_Op_Gt (Loc,
10233 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
10234 Right_Opnd =>
10235 Make_Attribute_Reference (Loc,
10236 Attribute_Name => Name_Last,
10237 Prefix =>
10238 New_Occurrence_Of (Target_Type, Loc)))),
10239 Reason => CE_Range_Check_Failed)));
10240
10241 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
10242 Analyze_And_Resolve (N, Btyp);
10243 end Real_Range_Check;
10244
10245 -----------------------------
10246 -- Has_Extra_Accessibility --
10247 -----------------------------
10248
10249 -- Returns true for a formal of an anonymous access type or for
10250 -- an Ada 2012-style stand-alone object of an anonymous access type.
10251
10252 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is
10253 begin
10254 if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then
10255 return Present (Effective_Extra_Accessibility (Id));
10256 else
10257 return False;
10258 end if;
10259 end Has_Extra_Accessibility;
10260
10261 -- Start of processing for Expand_N_Type_Conversion
10262
10263 begin
10264 -- First remove check marks put by the semantic analysis on the type
10265 -- conversion between array types. We need these checks, and they will
10266 -- be generated by this expansion routine, but we do not depend on these
10267 -- flags being set, and since we do intend to expand the checks in the
10268 -- front end, we don't want them on the tree passed to the back end.
10269
10270 if Is_Array_Type (Target_Type) then
10271 if Is_Constrained (Target_Type) then
10272 Set_Do_Length_Check (N, False);
10273 else
10274 Set_Do_Range_Check (Operand, False);
10275 end if;
10276 end if;
10277
10278 -- Nothing at all to do if conversion is to the identical type so remove
10279 -- the conversion completely, it is useless, except that it may carry
10280 -- an Assignment_OK attribute, which must be propagated to the operand.
10281
10282 if Operand_Type = Target_Type then
10283 if Assignment_OK (N) then
10284 Set_Assignment_OK (Operand);
10285 end if;
10286
10287 Rewrite (N, Relocate_Node (Operand));
10288 goto Done;
10289 end if;
10290
10291 -- Nothing to do if this is the second argument of read. This is a
10292 -- "backwards" conversion that will be handled by the specialized code
10293 -- in attribute processing.
10294
10295 if Nkind (Parent (N)) = N_Attribute_Reference
10296 and then Attribute_Name (Parent (N)) = Name_Read
10297 and then Next (First (Expressions (Parent (N)))) = N
10298 then
10299 goto Done;
10300 end if;
10301
10302 -- Check for case of converting to a type that has an invariant
10303 -- associated with it. This required an invariant check. We convert
10304
10305 -- typ (expr)
10306
10307 -- into
10308
10309 -- do invariant_check (typ (expr)) in typ (expr);
10310
10311 -- using Duplicate_Subexpr to avoid multiple side effects
10312
10313 -- Note: the Comes_From_Source check, and then the resetting of this
10314 -- flag prevents what would otherwise be an infinite recursion.
10315
10316 if Has_Invariants (Target_Type)
10317 and then Present (Invariant_Procedure (Target_Type))
10318 and then Comes_From_Source (N)
10319 then
10320 Set_Comes_From_Source (N, False);
10321 Rewrite (N,
10322 Make_Expression_With_Actions (Loc,
10323 Actions => New_List (
10324 Make_Invariant_Call (Duplicate_Subexpr (N))),
10325 Expression => Duplicate_Subexpr_No_Checks (N)));
10326 Analyze_And_Resolve (N, Target_Type);
10327 goto Done;
10328 end if;
10329
10330 -- Here if we may need to expand conversion
10331
10332 -- If the operand of the type conversion is an arithmetic operation on
10333 -- signed integers, and the based type of the signed integer type in
10334 -- question is smaller than Standard.Integer, we promote both of the
10335 -- operands to type Integer.
10336
10337 -- For example, if we have
10338
10339 -- target-type (opnd1 + opnd2)
10340
10341 -- and opnd1 and opnd2 are of type short integer, then we rewrite
10342 -- this as:
10343
10344 -- target-type (integer(opnd1) + integer(opnd2))
10345
10346 -- We do this because we are always allowed to compute in a larger type
10347 -- if we do the right thing with the result, and in this case we are
10348 -- going to do a conversion which will do an appropriate check to make
10349 -- sure that things are in range of the target type in any case. This
10350 -- avoids some unnecessary intermediate overflows.
10351
10352 -- We might consider a similar transformation in the case where the
10353 -- target is a real type or a 64-bit integer type, and the operand
10354 -- is an arithmetic operation using a 32-bit integer type. However,
10355 -- we do not bother with this case, because it could cause significant
10356 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
10357 -- much cheaper, but we don't want different behavior on 32-bit and
10358 -- 64-bit machines. Note that the exclusion of the 64-bit case also
10359 -- handles the configurable run-time cases where 64-bit arithmetic
10360 -- may simply be unavailable.
10361
10362 -- Note: this circuit is partially redundant with respect to the circuit
10363 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
10364 -- the processing here. Also we still need the Checks circuit, since we
10365 -- have to be sure not to generate junk overflow checks in the first
10366 -- place, since it would be trick to remove them here.
10367
10368 if Integer_Promotion_Possible (N) then
10369
10370 -- All conditions met, go ahead with transformation
10371
10372 declare
10373 Opnd : Node_Id;
10374 L, R : Node_Id;
10375
10376 begin
10377 R :=
10378 Make_Type_Conversion (Loc,
10379 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
10380 Expression => Relocate_Node (Right_Opnd (Operand)));
10381
10382 Opnd := New_Op_Node (Nkind (Operand), Loc);
10383 Set_Right_Opnd (Opnd, R);
10384
10385 if Nkind (Operand) in N_Binary_Op then
10386 L :=
10387 Make_Type_Conversion (Loc,
10388 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
10389 Expression => Relocate_Node (Left_Opnd (Operand)));
10390
10391 Set_Left_Opnd (Opnd, L);
10392 end if;
10393
10394 Rewrite (N,
10395 Make_Type_Conversion (Loc,
10396 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
10397 Expression => Opnd));
10398
10399 Analyze_And_Resolve (N, Target_Type);
10400 goto Done;
10401 end;
10402 end if;
10403
10404 -- Do validity check if validity checking operands
10405
10406 if Validity_Checks_On and Validity_Check_Operands then
10407 Ensure_Valid (Operand);
10408 end if;
10409
10410 -- Special case of converting from non-standard boolean type
10411
10412 if Is_Boolean_Type (Operand_Type)
10413 and then (Nonzero_Is_True (Operand_Type))
10414 then
10415 Adjust_Condition (Operand);
10416 Set_Etype (Operand, Standard_Boolean);
10417 Operand_Type := Standard_Boolean;
10418 end if;
10419
10420 -- Case of converting to an access type
10421
10422 if Is_Access_Type (Target_Type) then
10423
10424 -- Apply an accessibility check when the conversion operand is an
10425 -- access parameter (or a renaming thereof), unless conversion was
10426 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
10427 -- Note that other checks may still need to be applied below (such
10428 -- as tagged type checks).
10429
10430 if Is_Entity_Name (Operand)
10431 and then Has_Extra_Accessibility (Entity (Operand))
10432 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
10433 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
10434 or else Attribute_Name (Original_Node (N)) = Name_Access)
10435 then
10436 Apply_Accessibility_Check
10437 (Operand, Target_Type, Insert_Node => Operand);
10438
10439 -- If the level of the operand type is statically deeper than the
10440 -- level of the target type, then force Program_Error. Note that this
10441 -- can only occur for cases where the attribute is within the body of
10442 -- an instantiation (otherwise the conversion will already have been
10443 -- rejected as illegal). Note: warnings are issued by the analyzer
10444 -- for the instance cases.
10445
10446 elsif In_Instance_Body
10447 and then Type_Access_Level (Operand_Type) >
10448 Type_Access_Level (Target_Type)
10449 then
10450 Raise_Accessibility_Error;
10451
10452 -- When the operand is a selected access discriminant the check needs
10453 -- to be made against the level of the object denoted by the prefix
10454 -- of the selected name. Force Program_Error for this case as well
10455 -- (this accessibility violation can only happen if within the body
10456 -- of an instantiation).
10457
10458 elsif In_Instance_Body
10459 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
10460 and then Nkind (Operand) = N_Selected_Component
10461 and then Object_Access_Level (Operand) >
10462 Type_Access_Level (Target_Type)
10463 then
10464 Raise_Accessibility_Error;
10465 goto Done;
10466 end if;
10467 end if;
10468
10469 -- Case of conversions of tagged types and access to tagged types
10470
10471 -- When needed, that is to say when the expression is class-wide, Add
10472 -- runtime a tag check for (strict) downward conversion by using the
10473 -- membership test, generating:
10474
10475 -- [constraint_error when Operand not in Target_Type'Class]
10476
10477 -- or in the access type case
10478
10479 -- [constraint_error
10480 -- when Operand /= null
10481 -- and then Operand.all not in
10482 -- Designated_Type (Target_Type)'Class]
10483
10484 if (Is_Access_Type (Target_Type)
10485 and then Is_Tagged_Type (Designated_Type (Target_Type)))
10486 or else Is_Tagged_Type (Target_Type)
10487 then
10488 -- Do not do any expansion in the access type case if the parent is a
10489 -- renaming, since this is an error situation which will be caught by
10490 -- Sem_Ch8, and the expansion can interfere with this error check.
10491
10492 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
10493 goto Done;
10494 end if;
10495
10496 -- Otherwise, proceed with processing tagged conversion
10497
10498 Tagged_Conversion : declare
10499 Actual_Op_Typ : Entity_Id;
10500 Actual_Targ_Typ : Entity_Id;
10501 Make_Conversion : Boolean := False;
10502 Root_Op_Typ : Entity_Id;
10503
10504 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
10505 -- Create a membership check to test whether Operand is a member
10506 -- of Targ_Typ. If the original Target_Type is an access, include
10507 -- a test for null value. The check is inserted at N.
10508
10509 --------------------
10510 -- Make_Tag_Check --
10511 --------------------
10512
10513 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
10514 Cond : Node_Id;
10515
10516 begin
10517 -- Generate:
10518 -- [Constraint_Error
10519 -- when Operand /= null
10520 -- and then Operand.all not in Targ_Typ]
10521
10522 if Is_Access_Type (Target_Type) then
10523 Cond :=
10524 Make_And_Then (Loc,
10525 Left_Opnd =>
10526 Make_Op_Ne (Loc,
10527 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10528 Right_Opnd => Make_Null (Loc)),
10529
10530 Right_Opnd =>
10531 Make_Not_In (Loc,
10532 Left_Opnd =>
10533 Make_Explicit_Dereference (Loc,
10534 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
10535 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc)));
10536
10537 -- Generate:
10538 -- [Constraint_Error when Operand not in Targ_Typ]
10539
10540 else
10541 Cond :=
10542 Make_Not_In (Loc,
10543 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10544 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc));
10545 end if;
10546
10547 Insert_Action (N,
10548 Make_Raise_Constraint_Error (Loc,
10549 Condition => Cond,
10550 Reason => CE_Tag_Check_Failed));
10551 end Make_Tag_Check;
10552
10553 -- Start of processing for Tagged_Conversion
10554
10555 begin
10556 -- Handle entities from the limited view
10557
10558 if Is_Access_Type (Operand_Type) then
10559 Actual_Op_Typ :=
10560 Available_View (Designated_Type (Operand_Type));
10561 else
10562 Actual_Op_Typ := Operand_Type;
10563 end if;
10564
10565 if Is_Access_Type (Target_Type) then
10566 Actual_Targ_Typ :=
10567 Available_View (Designated_Type (Target_Type));
10568 else
10569 Actual_Targ_Typ := Target_Type;
10570 end if;
10571
10572 Root_Op_Typ := Root_Type (Actual_Op_Typ);
10573
10574 -- Ada 2005 (AI-251): Handle interface type conversion
10575
10576 if Is_Interface (Actual_Op_Typ) then
10577 Expand_Interface_Conversion (N);
10578 goto Done;
10579 end if;
10580
10581 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
10582
10583 -- Create a runtime tag check for a downward class-wide type
10584 -- conversion.
10585
10586 if Is_Class_Wide_Type (Actual_Op_Typ)
10587 and then Actual_Op_Typ /= Actual_Targ_Typ
10588 and then Root_Op_Typ /= Actual_Targ_Typ
10589 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ,
10590 Use_Full_View => True)
10591 then
10592 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
10593 Make_Conversion := True;
10594 end if;
10595
10596 -- AI05-0073: If the result subtype of the function is defined
10597 -- by an access_definition designating a specific tagged type
10598 -- T, a check is made that the result value is null or the tag
10599 -- of the object designated by the result value identifies T.
10600 -- Constraint_Error is raised if this check fails.
10601
10602 if Nkind (Parent (N)) = N_Simple_Return_Statement then
10603 declare
10604 Func : Entity_Id;
10605 Func_Typ : Entity_Id;
10606
10607 begin
10608 -- Climb scope stack looking for the enclosing function
10609
10610 Func := Current_Scope;
10611 while Present (Func)
10612 and then Ekind (Func) /= E_Function
10613 loop
10614 Func := Scope (Func);
10615 end loop;
10616
10617 -- The function's return subtype must be defined using
10618 -- an access definition.
10619
10620 if Nkind (Result_Definition (Parent (Func))) =
10621 N_Access_Definition
10622 then
10623 Func_Typ := Directly_Designated_Type (Etype (Func));
10624
10625 -- The return subtype denotes a specific tagged type,
10626 -- in other words, a non class-wide type.
10627
10628 if Is_Tagged_Type (Func_Typ)
10629 and then not Is_Class_Wide_Type (Func_Typ)
10630 then
10631 Make_Tag_Check (Actual_Targ_Typ);
10632 Make_Conversion := True;
10633 end if;
10634 end if;
10635 end;
10636 end if;
10637
10638 -- We have generated a tag check for either a class-wide type
10639 -- conversion or for AI05-0073.
10640
10641 if Make_Conversion then
10642 declare
10643 Conv : Node_Id;
10644 begin
10645 Conv :=
10646 Make_Unchecked_Type_Conversion (Loc,
10647 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
10648 Expression => Relocate_Node (Expression (N)));
10649 Rewrite (N, Conv);
10650 Analyze_And_Resolve (N, Target_Type);
10651 end;
10652 end if;
10653 end if;
10654 end Tagged_Conversion;
10655
10656 -- Case of other access type conversions
10657
10658 elsif Is_Access_Type (Target_Type) then
10659 Apply_Constraint_Check (Operand, Target_Type);
10660
10661 -- Case of conversions from a fixed-point type
10662
10663 -- These conversions require special expansion and processing, found in
10664 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
10665 -- since from a semantic point of view, these are simple integer
10666 -- conversions, which do not need further processing.
10667
10668 elsif Is_Fixed_Point_Type (Operand_Type)
10669 and then not Conversion_OK (N)
10670 then
10671 -- We should never see universal fixed at this case, since the
10672 -- expansion of the constituent divide or multiply should have
10673 -- eliminated the explicit mention of universal fixed.
10674
10675 pragma Assert (Operand_Type /= Universal_Fixed);
10676
10677 -- Check for special case of the conversion to universal real that
10678 -- occurs as a result of the use of a round attribute. In this case,
10679 -- the real type for the conversion is taken from the target type of
10680 -- the Round attribute and the result must be marked as rounded.
10681
10682 if Target_Type = Universal_Real
10683 and then Nkind (Parent (N)) = N_Attribute_Reference
10684 and then Attribute_Name (Parent (N)) = Name_Round
10685 then
10686 Set_Rounded_Result (N);
10687 Set_Etype (N, Etype (Parent (N)));
10688 end if;
10689
10690 -- Otherwise do correct fixed-conversion, but skip these if the
10691 -- Conversion_OK flag is set, because from a semantic point of view
10692 -- these are simple integer conversions needing no further processing
10693 -- (the backend will simply treat them as integers).
10694
10695 if not Conversion_OK (N) then
10696 if Is_Fixed_Point_Type (Etype (N)) then
10697 Expand_Convert_Fixed_To_Fixed (N);
10698 Real_Range_Check;
10699
10700 elsif Is_Integer_Type (Etype (N)) then
10701 Expand_Convert_Fixed_To_Integer (N);
10702
10703 else
10704 pragma Assert (Is_Floating_Point_Type (Etype (N)));
10705 Expand_Convert_Fixed_To_Float (N);
10706 Real_Range_Check;
10707 end if;
10708 end if;
10709
10710 -- Case of conversions to a fixed-point type
10711
10712 -- These conversions require special expansion and processing, found in
10713 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
10714 -- since from a semantic point of view, these are simple integer
10715 -- conversions, which do not need further processing.
10716
10717 elsif Is_Fixed_Point_Type (Target_Type)
10718 and then not Conversion_OK (N)
10719 then
10720 if Is_Integer_Type (Operand_Type) then
10721 Expand_Convert_Integer_To_Fixed (N);
10722 Real_Range_Check;
10723 else
10724 pragma Assert (Is_Floating_Point_Type (Operand_Type));
10725 Expand_Convert_Float_To_Fixed (N);
10726 Real_Range_Check;
10727 end if;
10728
10729 -- Case of float-to-integer conversions
10730
10731 -- We also handle float-to-fixed conversions with Conversion_OK set
10732 -- since semantically the fixed-point target is treated as though it
10733 -- were an integer in such cases.
10734
10735 elsif Is_Floating_Point_Type (Operand_Type)
10736 and then
10737 (Is_Integer_Type (Target_Type)
10738 or else
10739 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
10740 then
10741 -- One more check here, gcc is still not able to do conversions of
10742 -- this type with proper overflow checking, and so gigi is doing an
10743 -- approximation of what is required by doing floating-point compares
10744 -- with the end-point. But that can lose precision in some cases, and
10745 -- give a wrong result. Converting the operand to Universal_Real is
10746 -- helpful, but still does not catch all cases with 64-bit integers
10747 -- on targets with only 64-bit floats.
10748
10749 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
10750 -- Can this code be removed ???
10751
10752 if Do_Range_Check (Operand) then
10753 Rewrite (Operand,
10754 Make_Type_Conversion (Loc,
10755 Subtype_Mark =>
10756 New_Occurrence_Of (Universal_Real, Loc),
10757 Expression =>
10758 Relocate_Node (Operand)));
10759
10760 Set_Etype (Operand, Universal_Real);
10761 Enable_Range_Check (Operand);
10762 Set_Do_Range_Check (Expression (Operand), False);
10763 end if;
10764
10765 -- Case of array conversions
10766
10767 -- Expansion of array conversions, add required length/range checks but
10768 -- only do this if there is no change of representation. For handling of
10769 -- this case, see Handle_Changed_Representation.
10770
10771 elsif Is_Array_Type (Target_Type) then
10772 if Is_Constrained (Target_Type) then
10773 Apply_Length_Check (Operand, Target_Type);
10774 else
10775 Apply_Range_Check (Operand, Target_Type);
10776 end if;
10777
10778 Handle_Changed_Representation;
10779
10780 -- Case of conversions of discriminated types
10781
10782 -- Add required discriminant checks if target is constrained. Again this
10783 -- change is skipped if we have a change of representation.
10784
10785 elsif Has_Discriminants (Target_Type)
10786 and then Is_Constrained (Target_Type)
10787 then
10788 Apply_Discriminant_Check (Operand, Target_Type);
10789 Handle_Changed_Representation;
10790
10791 -- Case of all other record conversions. The only processing required
10792 -- is to check for a change of representation requiring the special
10793 -- assignment processing.
10794
10795 elsif Is_Record_Type (Target_Type) then
10796
10797 -- Ada 2005 (AI-216): Program_Error is raised when converting from
10798 -- a derived Unchecked_Union type to an unconstrained type that is
10799 -- not Unchecked_Union if the operand lacks inferable discriminants.
10800
10801 if Is_Derived_Type (Operand_Type)
10802 and then Is_Unchecked_Union (Base_Type (Operand_Type))
10803 and then not Is_Constrained (Target_Type)
10804 and then not Is_Unchecked_Union (Base_Type (Target_Type))
10805 and then not Has_Inferable_Discriminants (Operand)
10806 then
10807 -- To prevent Gigi from generating illegal code, we generate a
10808 -- Program_Error node, but we give it the target type of the
10809 -- conversion (is this requirement documented somewhere ???)
10810
10811 declare
10812 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
10813 Reason => PE_Unchecked_Union_Restriction);
10814
10815 begin
10816 Set_Etype (PE, Target_Type);
10817 Rewrite (N, PE);
10818
10819 end;
10820 else
10821 Handle_Changed_Representation;
10822 end if;
10823
10824 -- Case of conversions of enumeration types
10825
10826 elsif Is_Enumeration_Type (Target_Type) then
10827
10828 -- Special processing is required if there is a change of
10829 -- representation (from enumeration representation clauses).
10830
10831 if not Same_Representation (Target_Type, Operand_Type) then
10832
10833 -- Convert: x(y) to x'val (ytyp'val (y))
10834
10835 Rewrite (N,
10836 Make_Attribute_Reference (Loc,
10837 Prefix => New_Occurrence_Of (Target_Type, Loc),
10838 Attribute_Name => Name_Val,
10839 Expressions => New_List (
10840 Make_Attribute_Reference (Loc,
10841 Prefix => New_Occurrence_Of (Operand_Type, Loc),
10842 Attribute_Name => Name_Pos,
10843 Expressions => New_List (Operand)))));
10844
10845 Analyze_And_Resolve (N, Target_Type);
10846 end if;
10847
10848 -- Case of conversions to floating-point
10849
10850 elsif Is_Floating_Point_Type (Target_Type) then
10851 Real_Range_Check;
10852 end if;
10853
10854 -- At this stage, either the conversion node has been transformed into
10855 -- some other equivalent expression, or left as a conversion that can be
10856 -- handled by Gigi, in the following cases:
10857
10858 -- Conversions with no change of representation or type
10859
10860 -- Numeric conversions involving integer, floating- and fixed-point
10861 -- values. Fixed-point values are allowed only if Conversion_OK is
10862 -- set, i.e. if the fixed-point values are to be treated as integers.
10863
10864 -- No other conversions should be passed to Gigi
10865
10866 -- Check: are these rules stated in sinfo??? if so, why restate here???
10867
10868 -- The only remaining step is to generate a range check if we still have
10869 -- a type conversion at this stage and Do_Range_Check is set. For now we
10870 -- do this only for conversions of discrete types.
10871
10872 if Nkind (N) = N_Type_Conversion
10873 and then Is_Discrete_Type (Etype (N))
10874 then
10875 declare
10876 Expr : constant Node_Id := Expression (N);
10877 Ftyp : Entity_Id;
10878 Ityp : Entity_Id;
10879
10880 begin
10881 if Do_Range_Check (Expr)
10882 and then Is_Discrete_Type (Etype (Expr))
10883 then
10884 Set_Do_Range_Check (Expr, False);
10885
10886 -- Before we do a range check, we have to deal with treating a
10887 -- fixed-point operand as an integer. The way we do this is
10888 -- simply to do an unchecked conversion to an appropriate
10889 -- integer type large enough to hold the result.
10890
10891 -- This code is not active yet, because we are only dealing
10892 -- with discrete types so far ???
10893
10894 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
10895 and then Treat_Fixed_As_Integer (Expr)
10896 then
10897 Ftyp := Base_Type (Etype (Expr));
10898
10899 if Esize (Ftyp) >= Esize (Standard_Integer) then
10900 Ityp := Standard_Long_Long_Integer;
10901 else
10902 Ityp := Standard_Integer;
10903 end if;
10904
10905 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
10906 end if;
10907
10908 -- Reset overflow flag, since the range check will include
10909 -- dealing with possible overflow, and generate the check. If
10910 -- Address is either a source type or target type, suppress
10911 -- range check to avoid typing anomalies when it is a visible
10912 -- integer type.
10913
10914 Set_Do_Overflow_Check (N, False);
10915 if not Is_Descendent_Of_Address (Etype (Expr))
10916 and then not Is_Descendent_Of_Address (Target_Type)
10917 then
10918 Generate_Range_Check
10919 (Expr, Target_Type, CE_Range_Check_Failed);
10920 end if;
10921 end if;
10922 end;
10923 end if;
10924
10925 -- Final step, if the result is a type conversion involving Vax_Float
10926 -- types, then it is subject for further special processing.
10927
10928 if Nkind (N) = N_Type_Conversion
10929 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
10930 then
10931 Expand_Vax_Conversion (N);
10932 goto Done;
10933 end if;
10934
10935 -- Here at end of processing
10936
10937 <<Done>>
10938 -- Apply predicate check if required. Note that we can't just call
10939 -- Apply_Predicate_Check here, because the type looks right after
10940 -- the conversion and it would omit the check. The Comes_From_Source
10941 -- guard is necessary to prevent infinite recursions when we generate
10942 -- internal conversions for the purpose of checking predicates.
10943
10944 if Present (Predicate_Function (Target_Type))
10945 and then Target_Type /= Operand_Type
10946 and then Comes_From_Source (N)
10947 then
10948 declare
10949 New_Expr : constant Node_Id := Duplicate_Subexpr (N);
10950
10951 begin
10952 -- Avoid infinite recursion on the subsequent expansion of
10953 -- of the copy of the original type conversion.
10954
10955 Set_Comes_From_Source (New_Expr, False);
10956 Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr));
10957 end;
10958 end if;
10959 end Expand_N_Type_Conversion;
10960
10961 -----------------------------------
10962 -- Expand_N_Unchecked_Expression --
10963 -----------------------------------
10964
10965 -- Remove the unchecked expression node from the tree. Its job was simply
10966 -- to make sure that its constituent expression was handled with checks
10967 -- off, and now that that is done, we can remove it from the tree, and
10968 -- indeed must, since Gigi does not expect to see these nodes.
10969
10970 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
10971 Exp : constant Node_Id := Expression (N);
10972 begin
10973 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
10974 Rewrite (N, Exp);
10975 end Expand_N_Unchecked_Expression;
10976
10977 ----------------------------------------
10978 -- Expand_N_Unchecked_Type_Conversion --
10979 ----------------------------------------
10980
10981 -- If this cannot be handled by Gigi and we haven't already made a
10982 -- temporary for it, do it now.
10983
10984 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
10985 Target_Type : constant Entity_Id := Etype (N);
10986 Operand : constant Node_Id := Expression (N);
10987 Operand_Type : constant Entity_Id := Etype (Operand);
10988
10989 begin
10990 -- Nothing at all to do if conversion is to the identical type so remove
10991 -- the conversion completely, it is useless, except that it may carry
10992 -- an Assignment_OK indication which must be propagated to the operand.
10993
10994 if Operand_Type = Target_Type then
10995
10996 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
10997
10998 if Assignment_OK (N) then
10999 Set_Assignment_OK (Operand);
11000 end if;
11001
11002 Rewrite (N, Relocate_Node (Operand));
11003 return;
11004 end if;
11005
11006 -- If we have a conversion of a compile time known value to a target
11007 -- type and the value is in range of the target type, then we can simply
11008 -- replace the construct by an integer literal of the correct type. We
11009 -- only apply this to integer types being converted. Possibly it may
11010 -- apply in other cases, but it is too much trouble to worry about.
11011
11012 -- Note that we do not do this transformation if the Kill_Range_Check
11013 -- flag is set, since then the value may be outside the expected range.
11014 -- This happens in the Normalize_Scalars case.
11015
11016 -- We also skip this if either the target or operand type is biased
11017 -- because in this case, the unchecked conversion is supposed to
11018 -- preserve the bit pattern, not the integer value.
11019
11020 if Is_Integer_Type (Target_Type)
11021 and then not Has_Biased_Representation (Target_Type)
11022 and then Is_Integer_Type (Operand_Type)
11023 and then not Has_Biased_Representation (Operand_Type)
11024 and then Compile_Time_Known_Value (Operand)
11025 and then not Kill_Range_Check (N)
11026 then
11027 declare
11028 Val : constant Uint := Expr_Value (Operand);
11029
11030 begin
11031 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
11032 and then
11033 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
11034 and then
11035 Val >= Expr_Value (Type_Low_Bound (Target_Type))
11036 and then
11037 Val <= Expr_Value (Type_High_Bound (Target_Type))
11038 then
11039 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
11040
11041 -- If Address is the target type, just set the type to avoid a
11042 -- spurious type error on the literal when Address is a visible
11043 -- integer type.
11044
11045 if Is_Descendent_Of_Address (Target_Type) then
11046 Set_Etype (N, Target_Type);
11047 else
11048 Analyze_And_Resolve (N, Target_Type);
11049 end if;
11050
11051 return;
11052 end if;
11053 end;
11054 end if;
11055
11056 -- Nothing to do if conversion is safe
11057
11058 if Safe_Unchecked_Type_Conversion (N) then
11059 return;
11060 end if;
11061
11062 -- Otherwise force evaluation unless Assignment_OK flag is set (this
11063 -- flag indicates ??? More comments needed here)
11064
11065 if Assignment_OK (N) then
11066 null;
11067 else
11068 Force_Evaluation (N);
11069 end if;
11070 end Expand_N_Unchecked_Type_Conversion;
11071
11072 ----------------------------
11073 -- Expand_Record_Equality --
11074 ----------------------------
11075
11076 -- For non-variant records, Equality is expanded when needed into:
11077
11078 -- and then Lhs.Discr1 = Rhs.Discr1
11079 -- and then ...
11080 -- and then Lhs.Discrn = Rhs.Discrn
11081 -- and then Lhs.Cmp1 = Rhs.Cmp1
11082 -- and then ...
11083 -- and then Lhs.Cmpn = Rhs.Cmpn
11084
11085 -- The expression is folded by the back-end for adjacent fields. This
11086 -- function is called for tagged record in only one occasion: for imple-
11087 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
11088 -- otherwise the primitive "=" is used directly.
11089
11090 function Expand_Record_Equality
11091 (Nod : Node_Id;
11092 Typ : Entity_Id;
11093 Lhs : Node_Id;
11094 Rhs : Node_Id;
11095 Bodies : List_Id) return Node_Id
11096 is
11097 Loc : constant Source_Ptr := Sloc (Nod);
11098
11099 Result : Node_Id;
11100 C : Entity_Id;
11101
11102 First_Time : Boolean := True;
11103
11104 function Element_To_Compare (C : Entity_Id) return Entity_Id;
11105 -- Return the next discriminant or component to compare, starting with
11106 -- C, skipping inherited components.
11107
11108 ------------------------
11109 -- Element_To_Compare --
11110 ------------------------
11111
11112 function Element_To_Compare (C : Entity_Id) return Entity_Id is
11113 Comp : Entity_Id;
11114
11115 begin
11116 Comp := C;
11117 loop
11118 -- Exit loop when the next element to be compared is found, or
11119 -- there is no more such element.
11120
11121 exit when No (Comp);
11122
11123 exit when Ekind_In (Comp, E_Discriminant, E_Component)
11124 and then not (
11125
11126 -- Skip inherited components
11127
11128 -- Note: for a tagged type, we always generate the "=" primitive
11129 -- for the base type (not on the first subtype), so the test for
11130 -- Comp /= Original_Record_Component (Comp) is True for
11131 -- inherited components only.
11132
11133 (Is_Tagged_Type (Typ)
11134 and then Comp /= Original_Record_Component (Comp))
11135
11136 -- Skip _Tag
11137
11138 or else Chars (Comp) = Name_uTag
11139
11140 -- The .NET/JVM version of type Root_Controlled contains two
11141 -- fields which should not be considered part of the object. To
11142 -- achieve proper equiality between two controlled objects on
11143 -- .NET/JVM, skip _Parent whenever it has type Root_Controlled.
11144
11145 or else (Chars (Comp) = Name_uParent
11146 and then VM_Target /= No_VM
11147 and then Etype (Comp) = RTE (RE_Root_Controlled))
11148
11149 -- Skip interface elements (secondary tags???)
11150
11151 or else Is_Interface (Etype (Comp)));
11152
11153 Next_Entity (Comp);
11154 end loop;
11155
11156 return Comp;
11157 end Element_To_Compare;
11158
11159 -- Start of processing for Expand_Record_Equality
11160
11161 begin
11162 -- Generates the following code: (assuming that Typ has one Discr and
11163 -- component C2 is also a record)
11164
11165 -- True
11166 -- and then Lhs.Discr1 = Rhs.Discr1
11167 -- and then Lhs.C1 = Rhs.C1
11168 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
11169 -- and then ...
11170 -- and then Lhs.Cmpn = Rhs.Cmpn
11171
11172 Result := New_Occurrence_Of (Standard_True, Loc);
11173 C := Element_To_Compare (First_Entity (Typ));
11174 while Present (C) loop
11175 declare
11176 New_Lhs : Node_Id;
11177 New_Rhs : Node_Id;
11178 Check : Node_Id;
11179
11180 begin
11181 if First_Time then
11182 First_Time := False;
11183 New_Lhs := Lhs;
11184 New_Rhs := Rhs;
11185 else
11186 New_Lhs := New_Copy_Tree (Lhs);
11187 New_Rhs := New_Copy_Tree (Rhs);
11188 end if;
11189
11190 Check :=
11191 Expand_Composite_Equality (Nod, Etype (C),
11192 Lhs =>
11193 Make_Selected_Component (Loc,
11194 Prefix => New_Lhs,
11195 Selector_Name => New_Occurrence_Of (C, Loc)),
11196 Rhs =>
11197 Make_Selected_Component (Loc,
11198 Prefix => New_Rhs,
11199 Selector_Name => New_Occurrence_Of (C, Loc)),
11200 Bodies => Bodies);
11201
11202 -- If some (sub)component is an unchecked_union, the whole
11203 -- operation will raise program error.
11204
11205 if Nkind (Check) = N_Raise_Program_Error then
11206 Result := Check;
11207 Set_Etype (Result, Standard_Boolean);
11208 exit;
11209 else
11210 Result :=
11211 Make_And_Then (Loc,
11212 Left_Opnd => Result,
11213 Right_Opnd => Check);
11214 end if;
11215 end;
11216
11217 C := Element_To_Compare (Next_Entity (C));
11218 end loop;
11219
11220 return Result;
11221 end Expand_Record_Equality;
11222
11223 ---------------------------
11224 -- Expand_Set_Membership --
11225 ---------------------------
11226
11227 procedure Expand_Set_Membership (N : Node_Id) is
11228 Lop : constant Node_Id := Left_Opnd (N);
11229 Alt : Node_Id;
11230 Res : Node_Id;
11231
11232 function Make_Cond (Alt : Node_Id) return Node_Id;
11233 -- If the alternative is a subtype mark, create a simple membership
11234 -- test. Otherwise create an equality test for it.
11235
11236 ---------------
11237 -- Make_Cond --
11238 ---------------
11239
11240 function Make_Cond (Alt : Node_Id) return Node_Id is
11241 Cond : Node_Id;
11242 L : constant Node_Id := New_Copy (Lop);
11243 R : constant Node_Id := Relocate_Node (Alt);
11244
11245 begin
11246 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
11247 or else Nkind (Alt) = N_Range
11248 then
11249 Cond :=
11250 Make_In (Sloc (Alt),
11251 Left_Opnd => L,
11252 Right_Opnd => R);
11253 else
11254 Cond :=
11255 Make_Op_Eq (Sloc (Alt),
11256 Left_Opnd => L,
11257 Right_Opnd => R);
11258 end if;
11259
11260 return Cond;
11261 end Make_Cond;
11262
11263 -- Start of processing for Expand_Set_Membership
11264
11265 begin
11266 Remove_Side_Effects (Lop);
11267
11268 Alt := Last (Alternatives (N));
11269 Res := Make_Cond (Alt);
11270
11271 Prev (Alt);
11272 while Present (Alt) loop
11273 Res :=
11274 Make_Or_Else (Sloc (Alt),
11275 Left_Opnd => Make_Cond (Alt),
11276 Right_Opnd => Res);
11277 Prev (Alt);
11278 end loop;
11279
11280 Rewrite (N, Res);
11281 Analyze_And_Resolve (N, Standard_Boolean);
11282 end Expand_Set_Membership;
11283
11284 -----------------------------------
11285 -- Expand_Short_Circuit_Operator --
11286 -----------------------------------
11287
11288 -- Deal with special expansion if actions are present for the right operand
11289 -- and deal with optimizing case of arguments being True or False. We also
11290 -- deal with the special case of non-standard boolean values.
11291
11292 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
11293 Loc : constant Source_Ptr := Sloc (N);
11294 Typ : constant Entity_Id := Etype (N);
11295 Left : constant Node_Id := Left_Opnd (N);
11296 Right : constant Node_Id := Right_Opnd (N);
11297 LocR : constant Source_Ptr := Sloc (Right);
11298 Actlist : List_Id;
11299
11300 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
11301 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
11302 -- If Left = Shortcut_Value then Right need not be evaluated
11303
11304 begin
11305 -- Deal with non-standard booleans
11306
11307 if Is_Boolean_Type (Typ) then
11308 Adjust_Condition (Left);
11309 Adjust_Condition (Right);
11310 Set_Etype (N, Standard_Boolean);
11311 end if;
11312
11313 -- Check for cases where left argument is known to be True or False
11314
11315 if Compile_Time_Known_Value (Left) then
11316
11317 -- Mark SCO for left condition as compile time known
11318
11319 if Generate_SCO and then Comes_From_Source (Left) then
11320 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
11321 end if;
11322
11323 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
11324 -- Any actions associated with Right will be executed unconditionally
11325 -- and can thus be inserted into the tree unconditionally.
11326
11327 if Expr_Value_E (Left) /= Shortcut_Ent then
11328 if Present (Actions (N)) then
11329 Insert_Actions (N, Actions (N));
11330 end if;
11331
11332 Rewrite (N, Right);
11333
11334 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
11335 -- In this case we can forget the actions associated with Right,
11336 -- since they will never be executed.
11337
11338 else
11339 Kill_Dead_Code (Right);
11340 Kill_Dead_Code (Actions (N));
11341 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
11342 end if;
11343
11344 Adjust_Result_Type (N, Typ);
11345 return;
11346 end if;
11347
11348 -- If Actions are present for the right operand, we have to do some
11349 -- special processing. We can't just let these actions filter back into
11350 -- code preceding the short circuit (which is what would have happened
11351 -- if we had not trapped them in the short-circuit form), since they
11352 -- must only be executed if the right operand of the short circuit is
11353 -- executed and not otherwise.
11354
11355 if Present (Actions (N)) then
11356 Actlist := Actions (N);
11357
11358 -- We now use an Expression_With_Actions node for the right operand
11359 -- of the short-circuit form. Note that this solves the traceability
11360 -- problems for coverage analysis.
11361
11362 Rewrite (Right,
11363 Make_Expression_With_Actions (LocR,
11364 Expression => Relocate_Node (Right),
11365 Actions => Actlist));
11366 Set_Actions (N, No_List);
11367 Analyze_And_Resolve (Right, Standard_Boolean);
11368
11369 Adjust_Result_Type (N, Typ);
11370 return;
11371 end if;
11372
11373 -- No actions present, check for cases of right argument True/False
11374
11375 if Compile_Time_Known_Value (Right) then
11376
11377 -- Mark SCO for left condition as compile time known
11378
11379 if Generate_SCO and then Comes_From_Source (Right) then
11380 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
11381 end if;
11382
11383 -- Change (Left and then True), (Left or else False) to Left.
11384 -- Note that we know there are no actions associated with the right
11385 -- operand, since we just checked for this case above.
11386
11387 if Expr_Value_E (Right) /= Shortcut_Ent then
11388 Rewrite (N, Left);
11389
11390 -- Change (Left and then False), (Left or else True) to Right,
11391 -- making sure to preserve any side effects associated with the Left
11392 -- operand.
11393
11394 else
11395 Remove_Side_Effects (Left);
11396 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
11397 end if;
11398 end if;
11399
11400 Adjust_Result_Type (N, Typ);
11401 end Expand_Short_Circuit_Operator;
11402
11403 -------------------------------------
11404 -- Fixup_Universal_Fixed_Operation --
11405 -------------------------------------
11406
11407 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
11408 Conv : constant Node_Id := Parent (N);
11409
11410 begin
11411 -- We must have a type conversion immediately above us
11412
11413 pragma Assert (Nkind (Conv) = N_Type_Conversion);
11414
11415 -- Normally the type conversion gives our target type. The exception
11416 -- occurs in the case of the Round attribute, where the conversion
11417 -- will be to universal real, and our real type comes from the Round
11418 -- attribute (as well as an indication that we must round the result)
11419
11420 if Nkind (Parent (Conv)) = N_Attribute_Reference
11421 and then Attribute_Name (Parent (Conv)) = Name_Round
11422 then
11423 Set_Etype (N, Etype (Parent (Conv)));
11424 Set_Rounded_Result (N);
11425
11426 -- Normal case where type comes from conversion above us
11427
11428 else
11429 Set_Etype (N, Etype (Conv));
11430 end if;
11431 end Fixup_Universal_Fixed_Operation;
11432
11433 ---------------------------------
11434 -- Has_Inferable_Discriminants --
11435 ---------------------------------
11436
11437 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
11438
11439 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
11440 -- Determines whether the left-most prefix of a selected component is a
11441 -- formal parameter in a subprogram. Assumes N is a selected component.
11442
11443 --------------------------------
11444 -- Prefix_Is_Formal_Parameter --
11445 --------------------------------
11446
11447 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
11448 Sel_Comp : Node_Id;
11449
11450 begin
11451 -- Move to the left-most prefix by climbing up the tree
11452
11453 Sel_Comp := N;
11454 while Present (Parent (Sel_Comp))
11455 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
11456 loop
11457 Sel_Comp := Parent (Sel_Comp);
11458 end loop;
11459
11460 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
11461 end Prefix_Is_Formal_Parameter;
11462
11463 -- Start of processing for Has_Inferable_Discriminants
11464
11465 begin
11466 -- For selected components, the subtype of the selector must be a
11467 -- constrained Unchecked_Union. If the component is subject to a
11468 -- per-object constraint, then the enclosing object must have inferable
11469 -- discriminants.
11470
11471 if Nkind (N) = N_Selected_Component then
11472 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
11473
11474 -- A small hack. If we have a per-object constrained selected
11475 -- component of a formal parameter, return True since we do not
11476 -- know the actual parameter association yet.
11477
11478 if Prefix_Is_Formal_Parameter (N) then
11479 return True;
11480
11481 -- Otherwise, check the enclosing object and the selector
11482
11483 else
11484 return Has_Inferable_Discriminants (Prefix (N))
11485 and then Has_Inferable_Discriminants (Selector_Name (N));
11486 end if;
11487
11488 -- The call to Has_Inferable_Discriminants will determine whether
11489 -- the selector has a constrained Unchecked_Union nominal type.
11490
11491 else
11492 return Has_Inferable_Discriminants (Selector_Name (N));
11493 end if;
11494
11495 -- A qualified expression has inferable discriminants if its subtype
11496 -- mark is a constrained Unchecked_Union subtype.
11497
11498 elsif Nkind (N) = N_Qualified_Expression then
11499 return Is_Unchecked_Union (Etype (Subtype_Mark (N)))
11500 and then Is_Constrained (Etype (Subtype_Mark (N)));
11501
11502 -- For all other names, it is sufficient to have a constrained
11503 -- Unchecked_Union nominal subtype.
11504
11505 else
11506 return Is_Unchecked_Union (Base_Type (Etype (N)))
11507 and then Is_Constrained (Etype (N));
11508 end if;
11509 end Has_Inferable_Discriminants;
11510
11511 -------------------------------
11512 -- Insert_Dereference_Action --
11513 -------------------------------
11514
11515 procedure Insert_Dereference_Action (N : Node_Id) is
11516
11517 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
11518 -- Return true if type of P is derived from Checked_Pool;
11519
11520 -----------------------------
11521 -- Is_Checked_Storage_Pool --
11522 -----------------------------
11523
11524 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
11525 T : Entity_Id;
11526
11527 begin
11528 if No (P) then
11529 return False;
11530 end if;
11531
11532 T := Etype (P);
11533 while T /= Etype (T) loop
11534 if Is_RTE (T, RE_Checked_Pool) then
11535 return True;
11536 else
11537 T := Etype (T);
11538 end if;
11539 end loop;
11540
11541 return False;
11542 end Is_Checked_Storage_Pool;
11543
11544 -- Local variables
11545
11546 Typ : constant Entity_Id := Etype (N);
11547 Desig : constant Entity_Id := Available_View (Designated_Type (Typ));
11548 Loc : constant Source_Ptr := Sloc (N);
11549 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
11550 Pnod : constant Node_Id := Parent (N);
11551
11552 Addr : Entity_Id;
11553 Alig : Entity_Id;
11554 Deref : Node_Id;
11555 Size : Entity_Id;
11556 Stmt : Node_Id;
11557
11558 -- Start of processing for Insert_Dereference_Action
11559
11560 begin
11561 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
11562
11563 -- Do not re-expand a dereference which has already been processed by
11564 -- this routine.
11565
11566 if Has_Dereference_Action (Pnod) then
11567 return;
11568
11569 -- Do not perform this type of expansion for internally-generated
11570 -- dereferences.
11571
11572 elsif not Comes_From_Source (Original_Node (Pnod)) then
11573 return;
11574
11575 -- A dereference action is only applicable to objects which have been
11576 -- allocated on a checked pool.
11577
11578 elsif not Is_Checked_Storage_Pool (Pool) then
11579 return;
11580 end if;
11581
11582 -- Extract the address of the dereferenced object. Generate:
11583
11584 -- Addr : System.Address := <N>'Pool_Address;
11585
11586 Addr := Make_Temporary (Loc, 'P');
11587
11588 Insert_Action (N,
11589 Make_Object_Declaration (Loc,
11590 Defining_Identifier => Addr,
11591 Object_Definition =>
11592 New_Occurrence_Of (RTE (RE_Address), Loc),
11593 Expression =>
11594 Make_Attribute_Reference (Loc,
11595 Prefix => Duplicate_Subexpr_Move_Checks (N),
11596 Attribute_Name => Name_Pool_Address)));
11597
11598 -- Calculate the size of the dereferenced object. Generate:
11599
11600 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
11601
11602 Deref :=
11603 Make_Explicit_Dereference (Loc,
11604 Prefix => Duplicate_Subexpr_Move_Checks (N));
11605 Set_Has_Dereference_Action (Deref);
11606
11607 Size := Make_Temporary (Loc, 'S');
11608
11609 Insert_Action (N,
11610 Make_Object_Declaration (Loc,
11611 Defining_Identifier => Size,
11612
11613 Object_Definition =>
11614 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
11615
11616 Expression =>
11617 Make_Op_Divide (Loc,
11618 Left_Opnd =>
11619 Make_Attribute_Reference (Loc,
11620 Prefix => Deref,
11621 Attribute_Name => Name_Size),
11622 Right_Opnd =>
11623 Make_Integer_Literal (Loc, System_Storage_Unit))));
11624
11625 -- Calculate the alignment of the dereferenced object. Generate:
11626 -- Alig : constant Storage_Count := <N>.all'Alignment;
11627
11628 Deref :=
11629 Make_Explicit_Dereference (Loc,
11630 Prefix => Duplicate_Subexpr_Move_Checks (N));
11631 Set_Has_Dereference_Action (Deref);
11632
11633 Alig := Make_Temporary (Loc, 'A');
11634
11635 Insert_Action (N,
11636 Make_Object_Declaration (Loc,
11637 Defining_Identifier => Alig,
11638 Object_Definition =>
11639 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
11640 Expression =>
11641 Make_Attribute_Reference (Loc,
11642 Prefix => Deref,
11643 Attribute_Name => Name_Alignment)));
11644
11645 -- A dereference of a controlled object requires special processing. The
11646 -- finalization machinery requests additional space from the underlying
11647 -- pool to allocate and hide two pointers. As a result, a checked pool
11648 -- may mark the wrong memory as valid. Since checked pools do not have
11649 -- knowledge of hidden pointers, we have to bring the two pointers back
11650 -- in view in order to restore the original state of the object.
11651
11652 if Needs_Finalization (Desig) then
11653
11654 -- Adjust the address and size of the dereferenced object. Generate:
11655 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
11656
11657 Stmt :=
11658 Make_Procedure_Call_Statement (Loc,
11659 Name =>
11660 New_Occurrence_Of (RTE (RE_Adjust_Controlled_Dereference), Loc),
11661 Parameter_Associations => New_List (
11662 New_Occurrence_Of (Addr, Loc),
11663 New_Occurrence_Of (Size, Loc),
11664 New_Occurrence_Of (Alig, Loc)));
11665
11666 -- Class-wide types complicate things because we cannot determine
11667 -- statically whether the actual object is truly controlled. We must
11668 -- generate a runtime check to detect this property. Generate:
11669 --
11670 -- if Needs_Finalization (<N>.all'Tag) then
11671 -- <Stmt>;
11672 -- end if;
11673
11674 if Is_Class_Wide_Type (Desig) then
11675 Deref :=
11676 Make_Explicit_Dereference (Loc,
11677 Prefix => Duplicate_Subexpr_Move_Checks (N));
11678 Set_Has_Dereference_Action (Deref);
11679
11680 Stmt :=
11681 Make_Implicit_If_Statement (N,
11682 Condition =>
11683 Make_Function_Call (Loc,
11684 Name =>
11685 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
11686 Parameter_Associations => New_List (
11687 Make_Attribute_Reference (Loc,
11688 Prefix => Deref,
11689 Attribute_Name => Name_Tag))),
11690 Then_Statements => New_List (Stmt));
11691 end if;
11692
11693 Insert_Action (N, Stmt);
11694 end if;
11695
11696 -- Generate:
11697 -- Dereference (Pool, Addr, Size, Alig);
11698
11699 Insert_Action (N,
11700 Make_Procedure_Call_Statement (Loc,
11701 Name =>
11702 New_Occurrence_Of
11703 (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
11704 Parameter_Associations => New_List (
11705 New_Occurrence_Of (Pool, Loc),
11706 New_Occurrence_Of (Addr, Loc),
11707 New_Occurrence_Of (Size, Loc),
11708 New_Occurrence_Of (Alig, Loc))));
11709
11710 -- Mark the explicit dereference as processed to avoid potential
11711 -- infinite expansion.
11712
11713 Set_Has_Dereference_Action (Pnod);
11714
11715 exception
11716 when RE_Not_Available =>
11717 return;
11718 end Insert_Dereference_Action;
11719
11720 --------------------------------
11721 -- Integer_Promotion_Possible --
11722 --------------------------------
11723
11724 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
11725 Operand : constant Node_Id := Expression (N);
11726 Operand_Type : constant Entity_Id := Etype (Operand);
11727 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
11728
11729 begin
11730 pragma Assert (Nkind (N) = N_Type_Conversion);
11731
11732 return
11733
11734 -- We only do the transformation for source constructs. We assume
11735 -- that the expander knows what it is doing when it generates code.
11736
11737 Comes_From_Source (N)
11738
11739 -- If the operand type is Short_Integer or Short_Short_Integer,
11740 -- then we will promote to Integer, which is available on all
11741 -- targets, and is sufficient to ensure no intermediate overflow.
11742 -- Furthermore it is likely to be as efficient or more efficient
11743 -- than using the smaller type for the computation so we do this
11744 -- unconditionally.
11745
11746 and then
11747 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
11748 or else
11749 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
11750
11751 -- Test for interesting operation, which includes addition,
11752 -- division, exponentiation, multiplication, subtraction, absolute
11753 -- value and unary negation. Unary "+" is omitted since it is a
11754 -- no-op and thus can't overflow.
11755
11756 and then Nkind_In (Operand, N_Op_Abs,
11757 N_Op_Add,
11758 N_Op_Divide,
11759 N_Op_Expon,
11760 N_Op_Minus,
11761 N_Op_Multiply,
11762 N_Op_Subtract);
11763 end Integer_Promotion_Possible;
11764
11765 ------------------------------
11766 -- Make_Array_Comparison_Op --
11767 ------------------------------
11768
11769 -- This is a hand-coded expansion of the following generic function:
11770
11771 -- generic
11772 -- type elem is (<>);
11773 -- type index is (<>);
11774 -- type a is array (index range <>) of elem;
11775
11776 -- function Gnnn (X : a; Y: a) return boolean is
11777 -- J : index := Y'first;
11778
11779 -- begin
11780 -- if X'length = 0 then
11781 -- return false;
11782
11783 -- elsif Y'length = 0 then
11784 -- return true;
11785
11786 -- else
11787 -- for I in X'range loop
11788 -- if X (I) = Y (J) then
11789 -- if J = Y'last then
11790 -- exit;
11791 -- else
11792 -- J := index'succ (J);
11793 -- end if;
11794
11795 -- else
11796 -- return X (I) > Y (J);
11797 -- end if;
11798 -- end loop;
11799
11800 -- return X'length > Y'length;
11801 -- end if;
11802 -- end Gnnn;
11803
11804 -- Note that since we are essentially doing this expansion by hand, we
11805 -- do not need to generate an actual or formal generic part, just the
11806 -- instantiated function itself.
11807
11808 function Make_Array_Comparison_Op
11809 (Typ : Entity_Id;
11810 Nod : Node_Id) return Node_Id
11811 is
11812 Loc : constant Source_Ptr := Sloc (Nod);
11813
11814 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
11815 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
11816 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
11817 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
11818
11819 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
11820
11821 Loop_Statement : Node_Id;
11822 Loop_Body : Node_Id;
11823 If_Stat : Node_Id;
11824 Inner_If : Node_Id;
11825 Final_Expr : Node_Id;
11826 Func_Body : Node_Id;
11827 Func_Name : Entity_Id;
11828 Formals : List_Id;
11829 Length1 : Node_Id;
11830 Length2 : Node_Id;
11831
11832 begin
11833 -- if J = Y'last then
11834 -- exit;
11835 -- else
11836 -- J := index'succ (J);
11837 -- end if;
11838
11839 Inner_If :=
11840 Make_Implicit_If_Statement (Nod,
11841 Condition =>
11842 Make_Op_Eq (Loc,
11843 Left_Opnd => New_Occurrence_Of (J, Loc),
11844 Right_Opnd =>
11845 Make_Attribute_Reference (Loc,
11846 Prefix => New_Occurrence_Of (Y, Loc),
11847 Attribute_Name => Name_Last)),
11848
11849 Then_Statements => New_List (
11850 Make_Exit_Statement (Loc)),
11851
11852 Else_Statements =>
11853 New_List (
11854 Make_Assignment_Statement (Loc,
11855 Name => New_Occurrence_Of (J, Loc),
11856 Expression =>
11857 Make_Attribute_Reference (Loc,
11858 Prefix => New_Occurrence_Of (Index, Loc),
11859 Attribute_Name => Name_Succ,
11860 Expressions => New_List (New_Occurrence_Of (J, Loc))))));
11861
11862 -- if X (I) = Y (J) then
11863 -- if ... end if;
11864 -- else
11865 -- return X (I) > Y (J);
11866 -- end if;
11867
11868 Loop_Body :=
11869 Make_Implicit_If_Statement (Nod,
11870 Condition =>
11871 Make_Op_Eq (Loc,
11872 Left_Opnd =>
11873 Make_Indexed_Component (Loc,
11874 Prefix => New_Occurrence_Of (X, Loc),
11875 Expressions => New_List (New_Occurrence_Of (I, Loc))),
11876
11877 Right_Opnd =>
11878 Make_Indexed_Component (Loc,
11879 Prefix => New_Occurrence_Of (Y, Loc),
11880 Expressions => New_List (New_Occurrence_Of (J, Loc)))),
11881
11882 Then_Statements => New_List (Inner_If),
11883
11884 Else_Statements => New_List (
11885 Make_Simple_Return_Statement (Loc,
11886 Expression =>
11887 Make_Op_Gt (Loc,
11888 Left_Opnd =>
11889 Make_Indexed_Component (Loc,
11890 Prefix => New_Occurrence_Of (X, Loc),
11891 Expressions => New_List (New_Occurrence_Of (I, Loc))),
11892
11893 Right_Opnd =>
11894 Make_Indexed_Component (Loc,
11895 Prefix => New_Occurrence_Of (Y, Loc),
11896 Expressions => New_List (
11897 New_Occurrence_Of (J, Loc)))))));
11898
11899 -- for I in X'range loop
11900 -- if ... end if;
11901 -- end loop;
11902
11903 Loop_Statement :=
11904 Make_Implicit_Loop_Statement (Nod,
11905 Identifier => Empty,
11906
11907 Iteration_Scheme =>
11908 Make_Iteration_Scheme (Loc,
11909 Loop_Parameter_Specification =>
11910 Make_Loop_Parameter_Specification (Loc,
11911 Defining_Identifier => I,
11912 Discrete_Subtype_Definition =>
11913 Make_Attribute_Reference (Loc,
11914 Prefix => New_Occurrence_Of (X, Loc),
11915 Attribute_Name => Name_Range))),
11916
11917 Statements => New_List (Loop_Body));
11918
11919 -- if X'length = 0 then
11920 -- return false;
11921 -- elsif Y'length = 0 then
11922 -- return true;
11923 -- else
11924 -- for ... loop ... end loop;
11925 -- return X'length > Y'length;
11926 -- end if;
11927
11928 Length1 :=
11929 Make_Attribute_Reference (Loc,
11930 Prefix => New_Occurrence_Of (X, Loc),
11931 Attribute_Name => Name_Length);
11932
11933 Length2 :=
11934 Make_Attribute_Reference (Loc,
11935 Prefix => New_Occurrence_Of (Y, Loc),
11936 Attribute_Name => Name_Length);
11937
11938 Final_Expr :=
11939 Make_Op_Gt (Loc,
11940 Left_Opnd => Length1,
11941 Right_Opnd => Length2);
11942
11943 If_Stat :=
11944 Make_Implicit_If_Statement (Nod,
11945 Condition =>
11946 Make_Op_Eq (Loc,
11947 Left_Opnd =>
11948 Make_Attribute_Reference (Loc,
11949 Prefix => New_Occurrence_Of (X, Loc),
11950 Attribute_Name => Name_Length),
11951 Right_Opnd =>
11952 Make_Integer_Literal (Loc, 0)),
11953
11954 Then_Statements =>
11955 New_List (
11956 Make_Simple_Return_Statement (Loc,
11957 Expression => New_Occurrence_Of (Standard_False, Loc))),
11958
11959 Elsif_Parts => New_List (
11960 Make_Elsif_Part (Loc,
11961 Condition =>
11962 Make_Op_Eq (Loc,
11963 Left_Opnd =>
11964 Make_Attribute_Reference (Loc,
11965 Prefix => New_Occurrence_Of (Y, Loc),
11966 Attribute_Name => Name_Length),
11967 Right_Opnd =>
11968 Make_Integer_Literal (Loc, 0)),
11969
11970 Then_Statements =>
11971 New_List (
11972 Make_Simple_Return_Statement (Loc,
11973 Expression => New_Occurrence_Of (Standard_True, Loc))))),
11974
11975 Else_Statements => New_List (
11976 Loop_Statement,
11977 Make_Simple_Return_Statement (Loc,
11978 Expression => Final_Expr)));
11979
11980 -- (X : a; Y: a)
11981
11982 Formals := New_List (
11983 Make_Parameter_Specification (Loc,
11984 Defining_Identifier => X,
11985 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
11986
11987 Make_Parameter_Specification (Loc,
11988 Defining_Identifier => Y,
11989 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
11990
11991 -- function Gnnn (...) return boolean is
11992 -- J : index := Y'first;
11993 -- begin
11994 -- if ... end if;
11995 -- end Gnnn;
11996
11997 Func_Name := Make_Temporary (Loc, 'G');
11998
11999 Func_Body :=
12000 Make_Subprogram_Body (Loc,
12001 Specification =>
12002 Make_Function_Specification (Loc,
12003 Defining_Unit_Name => Func_Name,
12004 Parameter_Specifications => Formals,
12005 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
12006
12007 Declarations => New_List (
12008 Make_Object_Declaration (Loc,
12009 Defining_Identifier => J,
12010 Object_Definition => New_Occurrence_Of (Index, Loc),
12011 Expression =>
12012 Make_Attribute_Reference (Loc,
12013 Prefix => New_Occurrence_Of (Y, Loc),
12014 Attribute_Name => Name_First))),
12015
12016 Handled_Statement_Sequence =>
12017 Make_Handled_Sequence_Of_Statements (Loc,
12018 Statements => New_List (If_Stat)));
12019
12020 return Func_Body;
12021 end Make_Array_Comparison_Op;
12022
12023 ---------------------------
12024 -- Make_Boolean_Array_Op --
12025 ---------------------------
12026
12027 -- For logical operations on boolean arrays, expand in line the following,
12028 -- replacing 'and' with 'or' or 'xor' where needed:
12029
12030 -- function Annn (A : typ; B: typ) return typ is
12031 -- C : typ;
12032 -- begin
12033 -- for J in A'range loop
12034 -- C (J) := A (J) op B (J);
12035 -- end loop;
12036 -- return C;
12037 -- end Annn;
12038
12039 -- Here typ is the boolean array type
12040
12041 function Make_Boolean_Array_Op
12042 (Typ : Entity_Id;
12043 N : Node_Id) return Node_Id
12044 is
12045 Loc : constant Source_Ptr := Sloc (N);
12046
12047 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
12048 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
12049 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
12050 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
12051
12052 A_J : Node_Id;
12053 B_J : Node_Id;
12054 C_J : Node_Id;
12055 Op : Node_Id;
12056
12057 Formals : List_Id;
12058 Func_Name : Entity_Id;
12059 Func_Body : Node_Id;
12060 Loop_Statement : Node_Id;
12061
12062 begin
12063 A_J :=
12064 Make_Indexed_Component (Loc,
12065 Prefix => New_Occurrence_Of (A, Loc),
12066 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12067
12068 B_J :=
12069 Make_Indexed_Component (Loc,
12070 Prefix => New_Occurrence_Of (B, Loc),
12071 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12072
12073 C_J :=
12074 Make_Indexed_Component (Loc,
12075 Prefix => New_Occurrence_Of (C, Loc),
12076 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12077
12078 if Nkind (N) = N_Op_And then
12079 Op :=
12080 Make_Op_And (Loc,
12081 Left_Opnd => A_J,
12082 Right_Opnd => B_J);
12083
12084 elsif Nkind (N) = N_Op_Or then
12085 Op :=
12086 Make_Op_Or (Loc,
12087 Left_Opnd => A_J,
12088 Right_Opnd => B_J);
12089
12090 else
12091 Op :=
12092 Make_Op_Xor (Loc,
12093 Left_Opnd => A_J,
12094 Right_Opnd => B_J);
12095 end if;
12096
12097 Loop_Statement :=
12098 Make_Implicit_Loop_Statement (N,
12099 Identifier => Empty,
12100
12101 Iteration_Scheme =>
12102 Make_Iteration_Scheme (Loc,
12103 Loop_Parameter_Specification =>
12104 Make_Loop_Parameter_Specification (Loc,
12105 Defining_Identifier => J,
12106 Discrete_Subtype_Definition =>
12107 Make_Attribute_Reference (Loc,
12108 Prefix => New_Occurrence_Of (A, Loc),
12109 Attribute_Name => Name_Range))),
12110
12111 Statements => New_List (
12112 Make_Assignment_Statement (Loc,
12113 Name => C_J,
12114 Expression => Op)));
12115
12116 Formals := New_List (
12117 Make_Parameter_Specification (Loc,
12118 Defining_Identifier => A,
12119 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
12120
12121 Make_Parameter_Specification (Loc,
12122 Defining_Identifier => B,
12123 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
12124
12125 Func_Name := Make_Temporary (Loc, 'A');
12126 Set_Is_Inlined (Func_Name);
12127
12128 Func_Body :=
12129 Make_Subprogram_Body (Loc,
12130 Specification =>
12131 Make_Function_Specification (Loc,
12132 Defining_Unit_Name => Func_Name,
12133 Parameter_Specifications => Formals,
12134 Result_Definition => New_Occurrence_Of (Typ, Loc)),
12135
12136 Declarations => New_List (
12137 Make_Object_Declaration (Loc,
12138 Defining_Identifier => C,
12139 Object_Definition => New_Occurrence_Of (Typ, Loc))),
12140
12141 Handled_Statement_Sequence =>
12142 Make_Handled_Sequence_Of_Statements (Loc,
12143 Statements => New_List (
12144 Loop_Statement,
12145 Make_Simple_Return_Statement (Loc,
12146 Expression => New_Occurrence_Of (C, Loc)))));
12147
12148 return Func_Body;
12149 end Make_Boolean_Array_Op;
12150
12151 -----------------------------------------
12152 -- Minimized_Eliminated_Overflow_Check --
12153 -----------------------------------------
12154
12155 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is
12156 begin
12157 return
12158 Is_Signed_Integer_Type (Etype (N))
12159 and then Overflow_Check_Mode in Minimized_Or_Eliminated;
12160 end Minimized_Eliminated_Overflow_Check;
12161
12162 --------------------------------
12163 -- Optimize_Length_Comparison --
12164 --------------------------------
12165
12166 procedure Optimize_Length_Comparison (N : Node_Id) is
12167 Loc : constant Source_Ptr := Sloc (N);
12168 Typ : constant Entity_Id := Etype (N);
12169 Result : Node_Id;
12170
12171 Left : Node_Id;
12172 Right : Node_Id;
12173 -- First and Last attribute reference nodes, which end up as left and
12174 -- right operands of the optimized result.
12175
12176 Is_Zero : Boolean;
12177 -- True for comparison operand of zero
12178
12179 Comp : Node_Id;
12180 -- Comparison operand, set only if Is_Zero is false
12181
12182 Ent : Entity_Id;
12183 -- Entity whose length is being compared
12184
12185 Index : Node_Id;
12186 -- Integer_Literal node for length attribute expression, or Empty
12187 -- if there is no such expression present.
12188
12189 Ityp : Entity_Id;
12190 -- Type of array index to which 'Length is applied
12191
12192 Op : Node_Kind := Nkind (N);
12193 -- Kind of comparison operator, gets flipped if operands backwards
12194
12195 function Is_Optimizable (N : Node_Id) return Boolean;
12196 -- Tests N to see if it is an optimizable comparison value (defined as
12197 -- constant zero or one, or something else where the value is known to
12198 -- be positive and in the range of 32-bits, and where the corresponding
12199 -- Length value is also known to be 32-bits. If result is true, sets
12200 -- Is_Zero, Ityp, and Comp accordingly.
12201
12202 function Is_Entity_Length (N : Node_Id) return Boolean;
12203 -- Tests if N is a length attribute applied to a simple entity. If so,
12204 -- returns True, and sets Ent to the entity, and Index to the integer
12205 -- literal provided as an attribute expression, or to Empty if none.
12206 -- Also returns True if the expression is a generated type conversion
12207 -- whose expression is of the desired form. This latter case arises
12208 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
12209 -- to check for being in range, which is not needed in this context.
12210 -- Returns False if neither condition holds.
12211
12212 function Prepare_64 (N : Node_Id) return Node_Id;
12213 -- Given a discrete expression, returns a Long_Long_Integer typed
12214 -- expression representing the underlying value of the expression.
12215 -- This is done with an unchecked conversion to the result type. We
12216 -- use unchecked conversion to handle the enumeration type case.
12217
12218 ----------------------
12219 -- Is_Entity_Length --
12220 ----------------------
12221
12222 function Is_Entity_Length (N : Node_Id) return Boolean is
12223 begin
12224 if Nkind (N) = N_Attribute_Reference
12225 and then Attribute_Name (N) = Name_Length
12226 and then Is_Entity_Name (Prefix (N))
12227 then
12228 Ent := Entity (Prefix (N));
12229
12230 if Present (Expressions (N)) then
12231 Index := First (Expressions (N));
12232 else
12233 Index := Empty;
12234 end if;
12235
12236 return True;
12237
12238 elsif Nkind (N) = N_Type_Conversion
12239 and then not Comes_From_Source (N)
12240 then
12241 return Is_Entity_Length (Expression (N));
12242
12243 else
12244 return False;
12245 end if;
12246 end Is_Entity_Length;
12247
12248 --------------------
12249 -- Is_Optimizable --
12250 --------------------
12251
12252 function Is_Optimizable (N : Node_Id) return Boolean is
12253 Val : Uint;
12254 OK : Boolean;
12255 Lo : Uint;
12256 Hi : Uint;
12257 Indx : Node_Id;
12258
12259 begin
12260 if Compile_Time_Known_Value (N) then
12261 Val := Expr_Value (N);
12262
12263 if Val = Uint_0 then
12264 Is_Zero := True;
12265 Comp := Empty;
12266 return True;
12267
12268 elsif Val = Uint_1 then
12269 Is_Zero := False;
12270 Comp := Empty;
12271 return True;
12272 end if;
12273 end if;
12274
12275 -- Here we have to make sure of being within 32-bits
12276
12277 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
12278
12279 if not OK
12280 or else Lo < Uint_1
12281 or else Hi > UI_From_Int (Int'Last)
12282 then
12283 return False;
12284 end if;
12285
12286 -- Comparison value was within range, so now we must check the index
12287 -- value to make sure it is also within 32-bits.
12288
12289 Indx := First_Index (Etype (Ent));
12290
12291 if Present (Index) then
12292 for J in 2 .. UI_To_Int (Intval (Index)) loop
12293 Next_Index (Indx);
12294 end loop;
12295 end if;
12296
12297 Ityp := Etype (Indx);
12298
12299 if Esize (Ityp) > 32 then
12300 return False;
12301 end if;
12302
12303 Is_Zero := False;
12304 Comp := N;
12305 return True;
12306 end Is_Optimizable;
12307
12308 ----------------
12309 -- Prepare_64 --
12310 ----------------
12311
12312 function Prepare_64 (N : Node_Id) return Node_Id is
12313 begin
12314 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
12315 end Prepare_64;
12316
12317 -- Start of processing for Optimize_Length_Comparison
12318
12319 begin
12320 -- Nothing to do if not a comparison
12321
12322 if Op not in N_Op_Compare then
12323 return;
12324 end if;
12325
12326 -- Nothing to do if special -gnatd.P debug flag set
12327
12328 if Debug_Flag_Dot_PP then
12329 return;
12330 end if;
12331
12332 -- Ent'Length op 0/1
12333
12334 if Is_Entity_Length (Left_Opnd (N))
12335 and then Is_Optimizable (Right_Opnd (N))
12336 then
12337 null;
12338
12339 -- 0/1 op Ent'Length
12340
12341 elsif Is_Entity_Length (Right_Opnd (N))
12342 and then Is_Optimizable (Left_Opnd (N))
12343 then
12344 -- Flip comparison to opposite sense
12345
12346 case Op is
12347 when N_Op_Lt => Op := N_Op_Gt;
12348 when N_Op_Le => Op := N_Op_Ge;
12349 when N_Op_Gt => Op := N_Op_Lt;
12350 when N_Op_Ge => Op := N_Op_Le;
12351 when others => null;
12352 end case;
12353
12354 -- Else optimization not possible
12355
12356 else
12357 return;
12358 end if;
12359
12360 -- Fall through if we will do the optimization
12361
12362 -- Cases to handle:
12363
12364 -- X'Length = 0 => X'First > X'Last
12365 -- X'Length = 1 => X'First = X'Last
12366 -- X'Length = n => X'First + (n - 1) = X'Last
12367
12368 -- X'Length /= 0 => X'First <= X'Last
12369 -- X'Length /= 1 => X'First /= X'Last
12370 -- X'Length /= n => X'First + (n - 1) /= X'Last
12371
12372 -- X'Length >= 0 => always true, warn
12373 -- X'Length >= 1 => X'First <= X'Last
12374 -- X'Length >= n => X'First + (n - 1) <= X'Last
12375
12376 -- X'Length > 0 => X'First <= X'Last
12377 -- X'Length > 1 => X'First < X'Last
12378 -- X'Length > n => X'First + (n - 1) < X'Last
12379
12380 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
12381 -- X'Length <= 1 => X'First >= X'Last
12382 -- X'Length <= n => X'First + (n - 1) >= X'Last
12383
12384 -- X'Length < 0 => always false (warn)
12385 -- X'Length < 1 => X'First > X'Last
12386 -- X'Length < n => X'First + (n - 1) > X'Last
12387
12388 -- Note: for the cases of n (not constant 0,1), we require that the
12389 -- corresponding index type be integer or shorter (i.e. not 64-bit),
12390 -- and the same for the comparison value. Then we do the comparison
12391 -- using 64-bit arithmetic (actually long long integer), so that we
12392 -- cannot have overflow intefering with the result.
12393
12394 -- First deal with warning cases
12395
12396 if Is_Zero then
12397 case Op is
12398
12399 -- X'Length >= 0
12400
12401 when N_Op_Ge =>
12402 Rewrite (N,
12403 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
12404 Analyze_And_Resolve (N, Typ);
12405 Warn_On_Known_Condition (N);
12406 return;
12407
12408 -- X'Length < 0
12409
12410 when N_Op_Lt =>
12411 Rewrite (N,
12412 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
12413 Analyze_And_Resolve (N, Typ);
12414 Warn_On_Known_Condition (N);
12415 return;
12416
12417 when N_Op_Le =>
12418 if Constant_Condition_Warnings
12419 and then Comes_From_Source (Original_Node (N))
12420 then
12421 Error_Msg_N ("could replace by ""'=""?c?", N);
12422 end if;
12423
12424 Op := N_Op_Eq;
12425
12426 when others =>
12427 null;
12428 end case;
12429 end if;
12430
12431 -- Build the First reference we will use
12432
12433 Left :=
12434 Make_Attribute_Reference (Loc,
12435 Prefix => New_Occurrence_Of (Ent, Loc),
12436 Attribute_Name => Name_First);
12437
12438 if Present (Index) then
12439 Set_Expressions (Left, New_List (New_Copy (Index)));
12440 end if;
12441
12442 -- If general value case, then do the addition of (n - 1), and
12443 -- also add the needed conversions to type Long_Long_Integer.
12444
12445 if Present (Comp) then
12446 Left :=
12447 Make_Op_Add (Loc,
12448 Left_Opnd => Prepare_64 (Left),
12449 Right_Opnd =>
12450 Make_Op_Subtract (Loc,
12451 Left_Opnd => Prepare_64 (Comp),
12452 Right_Opnd => Make_Integer_Literal (Loc, 1)));
12453 end if;
12454
12455 -- Build the Last reference we will use
12456
12457 Right :=
12458 Make_Attribute_Reference (Loc,
12459 Prefix => New_Occurrence_Of (Ent, Loc),
12460 Attribute_Name => Name_Last);
12461
12462 if Present (Index) then
12463 Set_Expressions (Right, New_List (New_Copy (Index)));
12464 end if;
12465
12466 -- If general operand, convert Last reference to Long_Long_Integer
12467
12468 if Present (Comp) then
12469 Right := Prepare_64 (Right);
12470 end if;
12471
12472 -- Check for cases to optimize
12473
12474 -- X'Length = 0 => X'First > X'Last
12475 -- X'Length < 1 => X'First > X'Last
12476 -- X'Length < n => X'First + (n - 1) > X'Last
12477
12478 if (Is_Zero and then Op = N_Op_Eq)
12479 or else (not Is_Zero and then Op = N_Op_Lt)
12480 then
12481 Result :=
12482 Make_Op_Gt (Loc,
12483 Left_Opnd => Left,
12484 Right_Opnd => Right);
12485
12486 -- X'Length = 1 => X'First = X'Last
12487 -- X'Length = n => X'First + (n - 1) = X'Last
12488
12489 elsif not Is_Zero and then Op = N_Op_Eq then
12490 Result :=
12491 Make_Op_Eq (Loc,
12492 Left_Opnd => Left,
12493 Right_Opnd => Right);
12494
12495 -- X'Length /= 0 => X'First <= X'Last
12496 -- X'Length > 0 => X'First <= X'Last
12497
12498 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
12499 Result :=
12500 Make_Op_Le (Loc,
12501 Left_Opnd => Left,
12502 Right_Opnd => Right);
12503
12504 -- X'Length /= 1 => X'First /= X'Last
12505 -- X'Length /= n => X'First + (n - 1) /= X'Last
12506
12507 elsif not Is_Zero and then Op = N_Op_Ne then
12508 Result :=
12509 Make_Op_Ne (Loc,
12510 Left_Opnd => Left,
12511 Right_Opnd => Right);
12512
12513 -- X'Length >= 1 => X'First <= X'Last
12514 -- X'Length >= n => X'First + (n - 1) <= X'Last
12515
12516 elsif not Is_Zero and then Op = N_Op_Ge then
12517 Result :=
12518 Make_Op_Le (Loc,
12519 Left_Opnd => Left,
12520 Right_Opnd => Right);
12521
12522 -- X'Length > 1 => X'First < X'Last
12523 -- X'Length > n => X'First + (n = 1) < X'Last
12524
12525 elsif not Is_Zero and then Op = N_Op_Gt then
12526 Result :=
12527 Make_Op_Lt (Loc,
12528 Left_Opnd => Left,
12529 Right_Opnd => Right);
12530
12531 -- X'Length <= 1 => X'First >= X'Last
12532 -- X'Length <= n => X'First + (n - 1) >= X'Last
12533
12534 elsif not Is_Zero and then Op = N_Op_Le then
12535 Result :=
12536 Make_Op_Ge (Loc,
12537 Left_Opnd => Left,
12538 Right_Opnd => Right);
12539
12540 -- Should not happen at this stage
12541
12542 else
12543 raise Program_Error;
12544 end if;
12545
12546 -- Rewrite and finish up
12547
12548 Rewrite (N, Result);
12549 Analyze_And_Resolve (N, Typ);
12550 return;
12551 end Optimize_Length_Comparison;
12552
12553 ------------------------------
12554 -- Process_Transient_Object --
12555 ------------------------------
12556
12557 procedure Process_Transient_Object
12558 (Decl : Node_Id;
12559 Rel_Node : Node_Id)
12560 is
12561 Loc : constant Source_Ptr := Sloc (Decl);
12562 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
12563 Obj_Typ : constant Node_Id := Etype (Obj_Id);
12564 Desig_Typ : Entity_Id;
12565 Expr : Node_Id;
12566 Fin_Stmts : List_Id;
12567 Ptr_Id : Entity_Id;
12568 Temp_Id : Entity_Id;
12569 Temp_Ins : Node_Id;
12570
12571 Hook_Context : constant Node_Id := Find_Hook_Context (Rel_Node);
12572 -- Node on which to insert the hook pointer (as an action): the
12573 -- innermost enclosing non-transient scope.
12574
12575 Finalization_Context : Node_Id;
12576 -- Node after which to insert finalization actions
12577
12578 Finalize_Always : Boolean;
12579 -- If False, call to finalizer includes a test of whether the hook
12580 -- pointer is null.
12581
12582 In_Cond_Expr : constant Boolean :=
12583 Within_Case_Or_If_Expression (Rel_Node);
12584
12585 begin
12586 -- Step 0: determine where to attach finalization actions in the tree
12587
12588 -- Special case for Boolean EWAs: capture expression in a temporary,
12589 -- whose declaration will serve as the context around which to insert
12590 -- finalization code. The finalization thus remains local to the
12591 -- specific condition being evaluated.
12592
12593 if Is_Boolean_Type (Etype (Rel_Node)) then
12594
12595 -- In this case, the finalization context is chosen so that we know
12596 -- at finalization point that the hook pointer is never null, so no
12597 -- need for a test, we can call the finalizer unconditionally, except
12598 -- in the case where the object is created in a specific branch of a
12599 -- conditional expression.
12600
12601 Finalize_Always :=
12602 not (In_Cond_Expr
12603 or else
12604 Nkind_In (Original_Node (Rel_Node), N_Case_Expression,
12605 N_If_Expression));
12606
12607 declare
12608 Loc : constant Source_Ptr := Sloc (Rel_Node);
12609 Temp : constant Entity_Id := Make_Temporary (Loc, 'E', Rel_Node);
12610
12611 begin
12612 Append_To (Actions (Rel_Node),
12613 Make_Object_Declaration (Loc,
12614 Defining_Identifier => Temp,
12615 Constant_Present => True,
12616 Object_Definition =>
12617 New_Occurrence_Of (Etype (Rel_Node), Loc),
12618 Expression => Expression (Rel_Node)));
12619 Finalization_Context := Last (Actions (Rel_Node));
12620
12621 Analyze (Last (Actions (Rel_Node)));
12622
12623 Set_Expression (Rel_Node, New_Occurrence_Of (Temp, Loc));
12624 Analyze (Expression (Rel_Node));
12625 end;
12626
12627 else
12628 Finalize_Always := False;
12629 Finalization_Context := Hook_Context;
12630 end if;
12631
12632 -- Step 1: Create the access type which provides a reference to the
12633 -- transient controlled object.
12634
12635 if Is_Access_Type (Obj_Typ) then
12636 Desig_Typ := Directly_Designated_Type (Obj_Typ);
12637 else
12638 Desig_Typ := Obj_Typ;
12639 end if;
12640
12641 Desig_Typ := Base_Type (Desig_Typ);
12642
12643 -- Generate:
12644 -- Ann : access [all] <Desig_Typ>;
12645
12646 Ptr_Id := Make_Temporary (Loc, 'A');
12647
12648 Insert_Action (Hook_Context,
12649 Make_Full_Type_Declaration (Loc,
12650 Defining_Identifier => Ptr_Id,
12651 Type_Definition =>
12652 Make_Access_To_Object_Definition (Loc,
12653 All_Present => Ekind (Obj_Typ) = E_General_Access_Type,
12654 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc))));
12655
12656 -- Step 2: Create a temporary which acts as a hook to the transient
12657 -- controlled object. Generate:
12658
12659 -- Temp : Ptr_Id := null;
12660
12661 Temp_Id := Make_Temporary (Loc, 'T');
12662
12663 Insert_Action (Hook_Context,
12664 Make_Object_Declaration (Loc,
12665 Defining_Identifier => Temp_Id,
12666 Object_Definition => New_Occurrence_Of (Ptr_Id, Loc)));
12667
12668 -- Mark the temporary as created for the purposes of exporting the
12669 -- transient controlled object out of the expression_with_action or if
12670 -- expression. This signals the machinery in Build_Finalizer to treat
12671 -- this case specially.
12672
12673 Set_Status_Flag_Or_Transient_Decl (Temp_Id, Decl);
12674
12675 -- Step 3: Hook the transient object to the temporary
12676
12677 -- This must be inserted right after the object declaration, so that
12678 -- the assignment is executed if, and only if, the object is actually
12679 -- created (whereas the declaration of the hook pointer, and the
12680 -- finalization call, may be inserted at an outer level, and may
12681 -- remain unused for some executions, if the actual creation of
12682 -- the object is conditional).
12683
12684 -- The use of unchecked conversion / unrestricted access is needed to
12685 -- avoid an accessibility violation. Note that the finalization code is
12686 -- structured in such a way that the "hook" is processed only when it
12687 -- points to an existing object.
12688
12689 if Is_Access_Type (Obj_Typ) then
12690 Expr :=
12691 Unchecked_Convert_To (Ptr_Id, New_Occurrence_Of (Obj_Id, Loc));
12692 else
12693 Expr :=
12694 Make_Attribute_Reference (Loc,
12695 Prefix => New_Occurrence_Of (Obj_Id, Loc),
12696 Attribute_Name => Name_Unrestricted_Access);
12697 end if;
12698
12699 -- Generate:
12700 -- Temp := Ptr_Id (Obj_Id);
12701 -- <or>
12702 -- Temp := Obj_Id'Unrestricted_Access;
12703
12704 -- When the transient object is initialized by an aggregate, the hook
12705 -- must capture the object after the last component assignment takes
12706 -- place. Only then is the object fully initialized.
12707
12708 if Ekind (Obj_Id) = E_Variable
12709 and then Present (Last_Aggregate_Assignment (Obj_Id))
12710 then
12711 Temp_Ins := Last_Aggregate_Assignment (Obj_Id);
12712
12713 -- Otherwise the hook seizes the related object immediately
12714
12715 else
12716 Temp_Ins := Decl;
12717 end if;
12718
12719 Insert_After_And_Analyze (Temp_Ins,
12720 Make_Assignment_Statement (Loc,
12721 Name => New_Occurrence_Of (Temp_Id, Loc),
12722 Expression => Expr));
12723
12724 -- Step 4: Finalize the transient controlled object after the context
12725 -- has been evaluated/elaborated. Generate:
12726
12727 -- if Temp /= null then
12728 -- [Deep_]Finalize (Temp.all);
12729 -- Temp := null;
12730 -- end if;
12731
12732 -- When the node is part of a return statement, there is no need to
12733 -- insert a finalization call, as the general finalization mechanism
12734 -- (see Build_Finalizer) would take care of the transient controlled
12735 -- object on subprogram exit. Note that it would also be impossible to
12736 -- insert the finalization code after the return statement as this will
12737 -- render it unreachable.
12738
12739 if Nkind (Finalization_Context) /= N_Simple_Return_Statement then
12740 Fin_Stmts := New_List (
12741 Make_Final_Call
12742 (Obj_Ref =>
12743 Make_Explicit_Dereference (Loc,
12744 Prefix => New_Occurrence_Of (Temp_Id, Loc)),
12745 Typ => Desig_Typ),
12746
12747 Make_Assignment_Statement (Loc,
12748 Name => New_Occurrence_Of (Temp_Id, Loc),
12749 Expression => Make_Null (Loc)));
12750
12751 if not Finalize_Always then
12752 Fin_Stmts := New_List (
12753 Make_Implicit_If_Statement (Decl,
12754 Condition =>
12755 Make_Op_Ne (Loc,
12756 Left_Opnd => New_Occurrence_Of (Temp_Id, Loc),
12757 Right_Opnd => Make_Null (Loc)),
12758 Then_Statements => Fin_Stmts));
12759 end if;
12760
12761 Insert_Actions_After (Finalization_Context, Fin_Stmts);
12762 end if;
12763 end Process_Transient_Object;
12764
12765 ------------------------
12766 -- Rewrite_Comparison --
12767 ------------------------
12768
12769 procedure Rewrite_Comparison (N : Node_Id) is
12770 Warning_Generated : Boolean := False;
12771 -- Set to True if first pass with Assume_Valid generates a warning in
12772 -- which case we skip the second pass to avoid warning overloaded.
12773
12774 Result : Node_Id;
12775 -- Set to Standard_True or Standard_False
12776
12777 begin
12778 if Nkind (N) = N_Type_Conversion then
12779 Rewrite_Comparison (Expression (N));
12780 return;
12781
12782 elsif Nkind (N) not in N_Op_Compare then
12783 return;
12784 end if;
12785
12786 -- Now start looking at the comparison in detail. We potentially go
12787 -- through this loop twice. The first time, Assume_Valid is set False
12788 -- in the call to Compile_Time_Compare. If this call results in a
12789 -- clear result of always True or Always False, that's decisive and
12790 -- we are done. Otherwise we repeat the processing with Assume_Valid
12791 -- set to True to generate additional warnings. We can skip that step
12792 -- if Constant_Condition_Warnings is False.
12793
12794 for AV in False .. True loop
12795 declare
12796 Typ : constant Entity_Id := Etype (N);
12797 Op1 : constant Node_Id := Left_Opnd (N);
12798 Op2 : constant Node_Id := Right_Opnd (N);
12799
12800 Res : constant Compare_Result :=
12801 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
12802 -- Res indicates if compare outcome can be compile time determined
12803
12804 True_Result : Boolean;
12805 False_Result : Boolean;
12806
12807 begin
12808 case N_Op_Compare (Nkind (N)) is
12809 when N_Op_Eq =>
12810 True_Result := Res = EQ;
12811 False_Result := Res = LT or else Res = GT or else Res = NE;
12812
12813 when N_Op_Ge =>
12814 True_Result := Res in Compare_GE;
12815 False_Result := Res = LT;
12816
12817 if Res = LE
12818 and then Constant_Condition_Warnings
12819 and then Comes_From_Source (Original_Node (N))
12820 and then Nkind (Original_Node (N)) = N_Op_Ge
12821 and then not In_Instance
12822 and then Is_Integer_Type (Etype (Left_Opnd (N)))
12823 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
12824 then
12825 Error_Msg_N
12826 ("can never be greater than, could replace by ""'=""?c?",
12827 N);
12828 Warning_Generated := True;
12829 end if;
12830
12831 when N_Op_Gt =>
12832 True_Result := Res = GT;
12833 False_Result := Res in Compare_LE;
12834
12835 when N_Op_Lt =>
12836 True_Result := Res = LT;
12837 False_Result := Res in Compare_GE;
12838
12839 when N_Op_Le =>
12840 True_Result := Res in Compare_LE;
12841 False_Result := Res = GT;
12842
12843 if Res = GE
12844 and then Constant_Condition_Warnings
12845 and then Comes_From_Source (Original_Node (N))
12846 and then Nkind (Original_Node (N)) = N_Op_Le
12847 and then not In_Instance
12848 and then Is_Integer_Type (Etype (Left_Opnd (N)))
12849 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
12850 then
12851 Error_Msg_N
12852 ("can never be less than, could replace by ""'=""?c?", N);
12853 Warning_Generated := True;
12854 end if;
12855
12856 when N_Op_Ne =>
12857 True_Result := Res = NE or else Res = GT or else Res = LT;
12858 False_Result := Res = EQ;
12859 end case;
12860
12861 -- If this is the first iteration, then we actually convert the
12862 -- comparison into True or False, if the result is certain.
12863
12864 if AV = False then
12865 if True_Result or False_Result then
12866 Result := Boolean_Literals (True_Result);
12867 Rewrite (N,
12868 Convert_To (Typ,
12869 New_Occurrence_Of (Result, Sloc (N))));
12870 Analyze_And_Resolve (N, Typ);
12871 Warn_On_Known_Condition (N);
12872 return;
12873 end if;
12874
12875 -- If this is the second iteration (AV = True), and the original
12876 -- node comes from source and we are not in an instance, then give
12877 -- a warning if we know result would be True or False. Note: we
12878 -- know Constant_Condition_Warnings is set if we get here.
12879
12880 elsif Comes_From_Source (Original_Node (N))
12881 and then not In_Instance
12882 then
12883 if True_Result then
12884 Error_Msg_N
12885 ("condition can only be False if invalid values present??",
12886 N);
12887 elsif False_Result then
12888 Error_Msg_N
12889 ("condition can only be True if invalid values present??",
12890 N);
12891 end if;
12892 end if;
12893 end;
12894
12895 -- Skip second iteration if not warning on constant conditions or
12896 -- if the first iteration already generated a warning of some kind or
12897 -- if we are in any case assuming all values are valid (so that the
12898 -- first iteration took care of the valid case).
12899
12900 exit when not Constant_Condition_Warnings;
12901 exit when Warning_Generated;
12902 exit when Assume_No_Invalid_Values;
12903 end loop;
12904 end Rewrite_Comparison;
12905
12906 ----------------------------
12907 -- Safe_In_Place_Array_Op --
12908 ----------------------------
12909
12910 function Safe_In_Place_Array_Op
12911 (Lhs : Node_Id;
12912 Op1 : Node_Id;
12913 Op2 : Node_Id) return Boolean
12914 is
12915 Target : Entity_Id;
12916
12917 function Is_Safe_Operand (Op : Node_Id) return Boolean;
12918 -- Operand is safe if it cannot overlap part of the target of the
12919 -- operation. If the operand and the target are identical, the operand
12920 -- is safe. The operand can be empty in the case of negation.
12921
12922 function Is_Unaliased (N : Node_Id) return Boolean;
12923 -- Check that N is a stand-alone entity
12924
12925 ------------------
12926 -- Is_Unaliased --
12927 ------------------
12928
12929 function Is_Unaliased (N : Node_Id) return Boolean is
12930 begin
12931 return
12932 Is_Entity_Name (N)
12933 and then No (Address_Clause (Entity (N)))
12934 and then No (Renamed_Object (Entity (N)));
12935 end Is_Unaliased;
12936
12937 ---------------------
12938 -- Is_Safe_Operand --
12939 ---------------------
12940
12941 function Is_Safe_Operand (Op : Node_Id) return Boolean is
12942 begin
12943 if No (Op) then
12944 return True;
12945
12946 elsif Is_Entity_Name (Op) then
12947 return Is_Unaliased (Op);
12948
12949 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
12950 return Is_Unaliased (Prefix (Op));
12951
12952 elsif Nkind (Op) = N_Slice then
12953 return
12954 Is_Unaliased (Prefix (Op))
12955 and then Entity (Prefix (Op)) /= Target;
12956
12957 elsif Nkind (Op) = N_Op_Not then
12958 return Is_Safe_Operand (Right_Opnd (Op));
12959
12960 else
12961 return False;
12962 end if;
12963 end Is_Safe_Operand;
12964
12965 -- Start of processing for Safe_In_Place_Array_Op
12966
12967 begin
12968 -- Skip this processing if the component size is different from system
12969 -- storage unit (since at least for NOT this would cause problems).
12970
12971 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
12972 return False;
12973
12974 -- Cannot do in place stuff on VM_Target since cannot pass addresses
12975
12976 elsif VM_Target /= No_VM then
12977 return False;
12978
12979 -- Cannot do in place stuff if non-standard Boolean representation
12980
12981 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
12982 return False;
12983
12984 elsif not Is_Unaliased (Lhs) then
12985 return False;
12986
12987 else
12988 Target := Entity (Lhs);
12989 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
12990 end if;
12991 end Safe_In_Place_Array_Op;
12992
12993 -----------------------
12994 -- Tagged_Membership --
12995 -----------------------
12996
12997 -- There are two different cases to consider depending on whether the right
12998 -- operand is a class-wide type or not. If not we just compare the actual
12999 -- tag of the left expr to the target type tag:
13000 --
13001 -- Left_Expr.Tag = Right_Type'Tag;
13002 --
13003 -- If it is a class-wide type we use the RT function CW_Membership which is
13004 -- usually implemented by looking in the ancestor tables contained in the
13005 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
13006
13007 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
13008 -- function IW_Membership which is usually implemented by looking in the
13009 -- table of abstract interface types plus the ancestor table contained in
13010 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
13011
13012 procedure Tagged_Membership
13013 (N : Node_Id;
13014 SCIL_Node : out Node_Id;
13015 Result : out Node_Id)
13016 is
13017 Left : constant Node_Id := Left_Opnd (N);
13018 Right : constant Node_Id := Right_Opnd (N);
13019 Loc : constant Source_Ptr := Sloc (N);
13020
13021 Full_R_Typ : Entity_Id;
13022 Left_Type : Entity_Id;
13023 New_Node : Node_Id;
13024 Right_Type : Entity_Id;
13025 Obj_Tag : Node_Id;
13026
13027 begin
13028 SCIL_Node := Empty;
13029
13030 -- Handle entities from the limited view
13031
13032 Left_Type := Available_View (Etype (Left));
13033 Right_Type := Available_View (Etype (Right));
13034
13035 -- In the case where the type is an access type, the test is applied
13036 -- using the designated types (needed in Ada 2012 for implicit anonymous
13037 -- access conversions, for AI05-0149).
13038
13039 if Is_Access_Type (Right_Type) then
13040 Left_Type := Designated_Type (Left_Type);
13041 Right_Type := Designated_Type (Right_Type);
13042 end if;
13043
13044 if Is_Class_Wide_Type (Left_Type) then
13045 Left_Type := Root_Type (Left_Type);
13046 end if;
13047
13048 if Is_Class_Wide_Type (Right_Type) then
13049 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
13050 else
13051 Full_R_Typ := Underlying_Type (Right_Type);
13052 end if;
13053
13054 Obj_Tag :=
13055 Make_Selected_Component (Loc,
13056 Prefix => Relocate_Node (Left),
13057 Selector_Name =>
13058 New_Occurrence_Of (First_Tag_Component (Left_Type), Loc));
13059
13060 if Is_Class_Wide_Type (Right_Type) then
13061
13062 -- No need to issue a run-time check if we statically know that the
13063 -- result of this membership test is always true. For example,
13064 -- considering the following declarations:
13065
13066 -- type Iface is interface;
13067 -- type T is tagged null record;
13068 -- type DT is new T and Iface with null record;
13069
13070 -- Obj1 : T;
13071 -- Obj2 : DT;
13072
13073 -- These membership tests are always true:
13074
13075 -- Obj1 in T'Class
13076 -- Obj2 in T'Class;
13077 -- Obj2 in Iface'Class;
13078
13079 -- We do not need to handle cases where the membership is illegal.
13080 -- For example:
13081
13082 -- Obj1 in DT'Class; -- Compile time error
13083 -- Obj1 in Iface'Class; -- Compile time error
13084
13085 if not Is_Class_Wide_Type (Left_Type)
13086 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
13087 Use_Full_View => True)
13088 or else (Is_Interface (Etype (Right_Type))
13089 and then Interface_Present_In_Ancestor
13090 (Typ => Left_Type,
13091 Iface => Etype (Right_Type))))
13092 then
13093 Result := New_Occurrence_Of (Standard_True, Loc);
13094 return;
13095 end if;
13096
13097 -- Ada 2005 (AI-251): Class-wide applied to interfaces
13098
13099 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
13100
13101 -- Support to: "Iface_CW_Typ in Typ'Class"
13102
13103 or else Is_Interface (Left_Type)
13104 then
13105 -- Issue error if IW_Membership operation not available in a
13106 -- configurable run time setting.
13107
13108 if not RTE_Available (RE_IW_Membership) then
13109 Error_Msg_CRT
13110 ("dynamic membership test on interface types", N);
13111 Result := Empty;
13112 return;
13113 end if;
13114
13115 Result :=
13116 Make_Function_Call (Loc,
13117 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
13118 Parameter_Associations => New_List (
13119 Make_Attribute_Reference (Loc,
13120 Prefix => Obj_Tag,
13121 Attribute_Name => Name_Address),
13122 New_Occurrence_Of (
13123 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
13124 Loc)));
13125
13126 -- Ada 95: Normal case
13127
13128 else
13129 Build_CW_Membership (Loc,
13130 Obj_Tag_Node => Obj_Tag,
13131 Typ_Tag_Node =>
13132 New_Occurrence_Of (
13133 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc),
13134 Related_Nod => N,
13135 New_Node => New_Node);
13136
13137 -- Generate the SCIL node for this class-wide membership test.
13138 -- Done here because the previous call to Build_CW_Membership
13139 -- relocates Obj_Tag.
13140
13141 if Generate_SCIL then
13142 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
13143 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
13144 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
13145 end if;
13146
13147 Result := New_Node;
13148 end if;
13149
13150 -- Right_Type is not a class-wide type
13151
13152 else
13153 -- No need to check the tag of the object if Right_Typ is abstract
13154
13155 if Is_Abstract_Type (Right_Type) then
13156 Result := New_Occurrence_Of (Standard_False, Loc);
13157
13158 else
13159 Result :=
13160 Make_Op_Eq (Loc,
13161 Left_Opnd => Obj_Tag,
13162 Right_Opnd =>
13163 New_Occurrence_Of
13164 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
13165 end if;
13166 end if;
13167 end Tagged_Membership;
13168
13169 ------------------------------
13170 -- Unary_Op_Validity_Checks --
13171 ------------------------------
13172
13173 procedure Unary_Op_Validity_Checks (N : Node_Id) is
13174 begin
13175 if Validity_Checks_On and Validity_Check_Operands then
13176 Ensure_Valid (Right_Opnd (N));
13177 end if;
13178 end Unary_Op_Validity_Checks;
13179
13180 end Exp_Ch4;