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