2014-08-04 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / exp_ch4.adb
blob0f4261fb7b99d30163f3523dcbf962821ed300df
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 (Def_Id, Finalization_Master (PtrT));
1129 Set_Associated_Storage_Pool (Def_Id,
1130 Associated_Storage_Pool (PtrT));
1132 -- Declare the object using the previous type declaration
1134 if Aggr_In_Place then
1135 Temp_Decl :=
1136 Make_Object_Declaration (Loc,
1137 Defining_Identifier => Temp,
1138 Object_Definition => New_Occurrence_Of (Def_Id, Loc),
1139 Expression =>
1140 Make_Allocator (Loc,
1141 New_Occurrence_Of (Etype (Exp), Loc)));
1143 -- Copy the Comes_From_Source flag for the allocator we just
1144 -- built, since logically this allocator is a replacement of
1145 -- the original allocator node. This is for proper handling
1146 -- of restriction No_Implicit_Heap_Allocations.
1148 Set_Comes_From_Source
1149 (Expression (Temp_Decl), Comes_From_Source (N));
1151 Set_No_Initialization (Expression (Temp_Decl));
1152 Insert_Action (N, Temp_Decl);
1154 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1155 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1157 else
1158 Node := Relocate_Node (N);
1159 Set_Analyzed (Node);
1161 Temp_Decl :=
1162 Make_Object_Declaration (Loc,
1163 Defining_Identifier => Temp,
1164 Constant_Present => True,
1165 Object_Definition => New_Occurrence_Of (Def_Id, Loc),
1166 Expression => Node);
1168 Insert_Action (N, Temp_Decl);
1169 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1170 end if;
1172 -- Generate an additional object containing the address of the
1173 -- returned object. The type of this second object declaration
1174 -- is the correct type required for the common processing that
1175 -- is still performed by this subprogram. The displacement of
1176 -- this pointer to reference the component associated with the
1177 -- interface type will be done at the end of common processing.
1179 New_Decl :=
1180 Make_Object_Declaration (Loc,
1181 Defining_Identifier => Make_Temporary (Loc, 'P'),
1182 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1183 Expression =>
1184 Unchecked_Convert_To (PtrT,
1185 New_Occurrence_Of (Temp, Loc)));
1187 Insert_Action (N, New_Decl);
1189 Temp_Decl := New_Decl;
1190 Temp := Defining_Identifier (New_Decl);
1191 end;
1192 end if;
1194 Apply_Accessibility_Check (Temp);
1196 -- Generate the tag assignment
1198 -- Suppress the tag assignment when VM_Target because VM tags are
1199 -- represented implicitly in objects.
1201 if not Tagged_Type_Expansion then
1202 null;
1204 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1205 -- interface objects because in this case the tag does not change.
1207 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
1208 pragma Assert (Is_Class_Wide_Type
1209 (Directly_Designated_Type (Etype (N))));
1210 null;
1212 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
1213 TagT := T;
1214 TagR := New_Occurrence_Of (Temp, Loc);
1216 elsif Is_Private_Type (T)
1217 and then Is_Tagged_Type (Underlying_Type (T))
1218 then
1219 TagT := Underlying_Type (T);
1220 TagR :=
1221 Unchecked_Convert_To (Underlying_Type (T),
1222 Make_Explicit_Dereference (Loc,
1223 Prefix => New_Occurrence_Of (Temp, Loc)));
1224 end if;
1226 if Present (TagT) then
1227 declare
1228 Full_T : constant Entity_Id := Underlying_Type (TagT);
1230 begin
1231 Tag_Assign :=
1232 Make_Assignment_Statement (Loc,
1233 Name =>
1234 Make_Selected_Component (Loc,
1235 Prefix => TagR,
1236 Selector_Name =>
1237 New_Occurrence_Of
1238 (First_Tag_Component (Full_T), Loc)),
1240 Expression =>
1241 Unchecked_Convert_To (RTE (RE_Tag),
1242 New_Occurrence_Of
1243 (Elists.Node
1244 (First_Elmt (Access_Disp_Table (Full_T))), Loc)));
1245 end;
1247 -- The previous assignment has to be done in any case
1249 Set_Assignment_OK (Name (Tag_Assign));
1250 Insert_Action (N, Tag_Assign);
1251 end if;
1253 if Needs_Finalization (DesigT) and then Needs_Finalization (T) then
1255 -- Generate an Adjust call if the object will be moved. In Ada
1256 -- 2005, the object may be inherently limited, in which case
1257 -- there is no Adjust procedure, and the object is built in
1258 -- place. In Ada 95, the object can be limited but not
1259 -- inherently limited if this allocator came from a return
1260 -- statement (we're allocating the result on the secondary
1261 -- stack). In that case, the object will be moved, so we _do_
1262 -- want to Adjust.
1264 if not Aggr_In_Place
1265 and then not Is_Limited_View (T)
1266 then
1267 Insert_Action (N,
1269 -- An unchecked conversion is needed in the classwide case
1270 -- because the designated type can be an ancestor of the
1271 -- subtype mark of the allocator.
1273 Make_Adjust_Call
1274 (Obj_Ref =>
1275 Unchecked_Convert_To (T,
1276 Make_Explicit_Dereference (Loc,
1277 Prefix => New_Occurrence_Of (Temp, Loc))),
1278 Typ => T));
1279 end if;
1281 -- Generate:
1282 -- Set_Finalize_Address (<PtrT>FM, <T>FD'Unrestricted_Access);
1284 -- Do not generate this call in the following cases:
1286 -- * .NET/JVM - these targets do not support address arithmetic
1287 -- and unchecked conversion, key elements of Finalize_Address.
1289 -- * CodePeer mode - TSS primitive Finalize_Address is not
1290 -- created in this mode.
1292 if VM_Target = No_VM
1293 and then not CodePeer_Mode
1294 and then Present (Finalization_Master (PtrT))
1295 and then Present (Temp_Decl)
1296 and then Nkind (Expression (Temp_Decl)) = N_Allocator
1297 then
1298 Insert_Action (N,
1299 Make_Set_Finalize_Address_Call
1300 (Loc => Loc,
1301 Typ => T,
1302 Ptr_Typ => PtrT));
1303 end if;
1304 end if;
1306 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1307 Analyze_And_Resolve (N, PtrT);
1309 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1310 -- component containing the secondary dispatch table of the interface
1311 -- type.
1313 if Is_Interface (Directly_Designated_Type (PtrT)) then
1314 Displace_Allocator_Pointer (N);
1315 end if;
1317 elsif Aggr_In_Place then
1318 Temp := Make_Temporary (Loc, 'P', N);
1319 Temp_Decl :=
1320 Make_Object_Declaration (Loc,
1321 Defining_Identifier => Temp,
1322 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1323 Expression =>
1324 Make_Allocator (Loc,
1325 Expression => New_Occurrence_Of (Etype (Exp), Loc)));
1327 -- Copy the Comes_From_Source flag for the allocator we just built,
1328 -- since logically this allocator is a replacement of the original
1329 -- allocator node. This is for proper handling of restriction
1330 -- No_Implicit_Heap_Allocations.
1332 Set_Comes_From_Source
1333 (Expression (Temp_Decl), Comes_From_Source (N));
1335 Set_No_Initialization (Expression (Temp_Decl));
1336 Insert_Action (N, Temp_Decl);
1338 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1339 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1341 -- Attach the object to the associated finalization master. Thisis
1342 -- done manually on .NET/JVM since those compilers do no support
1343 -- pools and cannot benefit from internally generated Allocate and
1344 -- Deallocate procedures.
1346 if VM_Target /= No_VM
1347 and then Is_Controlled (DesigT)
1348 and then Present (Finalization_Master (PtrT))
1349 then
1350 Insert_Action (N,
1351 Make_Attach_Call
1352 (Obj_Ref => New_Occurrence_Of (Temp, Loc),
1353 Ptr_Typ => PtrT));
1354 end if;
1356 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1357 Analyze_And_Resolve (N, PtrT);
1359 elsif Is_Access_Type (T) and then Can_Never_Be_Null (T) then
1360 Install_Null_Excluding_Check (Exp);
1362 elsif Is_Access_Type (DesigT)
1363 and then Nkind (Exp) = N_Allocator
1364 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1365 then
1366 -- Apply constraint to designated subtype indication
1368 Apply_Constraint_Check
1369 (Expression (Exp), Designated_Type (DesigT), No_Sliding => True);
1371 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1373 -- Propagate constraint_error to enclosing allocator
1375 Rewrite (Exp, New_Copy (Expression (Exp)));
1376 end if;
1378 else
1379 Build_Allocate_Deallocate_Proc (N, True);
1381 -- If we have:
1382 -- type A is access T1;
1383 -- X : A := new T2'(...);
1384 -- T1 and T2 can be different subtypes, and we might need to check
1385 -- both constraints. First check against the type of the qualified
1386 -- expression.
1388 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1390 if Do_Range_Check (Exp) then
1391 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1392 end if;
1394 -- A check is also needed in cases where the designated subtype is
1395 -- constrained and differs from the subtype given in the qualified
1396 -- expression. Note that the check on the qualified expression does
1397 -- not allow sliding, but this check does (a relaxation from Ada 83).
1399 if Is_Constrained (DesigT)
1400 and then not Subtypes_Statically_Match (T, DesigT)
1401 then
1402 Apply_Constraint_Check
1403 (Exp, DesigT, No_Sliding => False);
1405 if Do_Range_Check (Exp) then
1406 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1407 end if;
1408 end if;
1410 -- For an access to unconstrained packed array, GIGI needs to see an
1411 -- expression with a constrained subtype in order to compute the
1412 -- proper size for the allocator.
1414 if Is_Array_Type (T)
1415 and then not Is_Constrained (T)
1416 and then Is_Packed (T)
1417 then
1418 declare
1419 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1420 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1421 begin
1422 Insert_Action (Exp,
1423 Make_Subtype_Declaration (Loc,
1424 Defining_Identifier => ConstrT,
1425 Subtype_Indication =>
1426 Make_Subtype_From_Expr (Internal_Exp, T)));
1427 Freeze_Itype (ConstrT, Exp);
1428 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1429 end;
1430 end if;
1432 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1433 -- to a build-in-place function, then access to the allocated object
1434 -- must be passed to the function. Currently we limit such functions
1435 -- to those with constrained limited result subtypes, but eventually
1436 -- we plan to expand the allowed forms of functions that are treated
1437 -- as build-in-place.
1439 if Ada_Version >= Ada_2005
1440 and then Is_Build_In_Place_Function_Call (Exp)
1441 then
1442 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1443 end if;
1444 end if;
1446 exception
1447 when RE_Not_Available =>
1448 return;
1449 end Expand_Allocator_Expression;
1451 -----------------------------
1452 -- Expand_Array_Comparison --
1453 -----------------------------
1455 -- Expansion is only required in the case of array types. For the unpacked
1456 -- case, an appropriate runtime routine is called. For packed cases, and
1457 -- also in some other cases where a runtime routine cannot be called, the
1458 -- form of the expansion is:
1460 -- [body for greater_nn; boolean_expression]
1462 -- The body is built by Make_Array_Comparison_Op, and the form of the
1463 -- Boolean expression depends on the operator involved.
1465 procedure Expand_Array_Comparison (N : Node_Id) is
1466 Loc : constant Source_Ptr := Sloc (N);
1467 Op1 : Node_Id := Left_Opnd (N);
1468 Op2 : Node_Id := Right_Opnd (N);
1469 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1470 Ctyp : constant Entity_Id := Component_Type (Typ1);
1472 Expr : Node_Id;
1473 Func_Body : Node_Id;
1474 Func_Name : Entity_Id;
1476 Comp : RE_Id;
1478 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1479 -- True for byte addressable target
1481 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1482 -- Returns True if the length of the given operand is known to be less
1483 -- than 4. Returns False if this length is known to be four or greater
1484 -- or is not known at compile time.
1486 ------------------------
1487 -- Length_Less_Than_4 --
1488 ------------------------
1490 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1491 Otyp : constant Entity_Id := Etype (Opnd);
1493 begin
1494 if Ekind (Otyp) = E_String_Literal_Subtype then
1495 return String_Literal_Length (Otyp) < 4;
1497 else
1498 declare
1499 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1500 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1501 Hi : constant Node_Id := Type_High_Bound (Ityp);
1502 Lov : Uint;
1503 Hiv : Uint;
1505 begin
1506 if Compile_Time_Known_Value (Lo) then
1507 Lov := Expr_Value (Lo);
1508 else
1509 return False;
1510 end if;
1512 if Compile_Time_Known_Value (Hi) then
1513 Hiv := Expr_Value (Hi);
1514 else
1515 return False;
1516 end if;
1518 return Hiv < Lov + 3;
1519 end;
1520 end if;
1521 end Length_Less_Than_4;
1523 -- Start of processing for Expand_Array_Comparison
1525 begin
1526 -- Deal first with unpacked case, where we can call a runtime routine
1527 -- except that we avoid this for targets for which are not addressable
1528 -- by bytes, and for the JVM/CIL, since they do not support direct
1529 -- addressing of array components.
1531 if not Is_Bit_Packed_Array (Typ1)
1532 and then Byte_Addressable
1533 and then VM_Target = No_VM
1534 then
1535 -- The call we generate is:
1537 -- Compare_Array_xn[_Unaligned]
1538 -- (left'address, right'address, left'length, right'length) <op> 0
1540 -- x = U for unsigned, S for signed
1541 -- n = 8,16,32,64 for component size
1542 -- Add _Unaligned if length < 4 and component size is 8.
1543 -- <op> is the standard comparison operator
1545 if Component_Size (Typ1) = 8 then
1546 if Length_Less_Than_4 (Op1)
1547 or else
1548 Length_Less_Than_4 (Op2)
1549 then
1550 if Is_Unsigned_Type (Ctyp) then
1551 Comp := RE_Compare_Array_U8_Unaligned;
1552 else
1553 Comp := RE_Compare_Array_S8_Unaligned;
1554 end if;
1556 else
1557 if Is_Unsigned_Type (Ctyp) then
1558 Comp := RE_Compare_Array_U8;
1559 else
1560 Comp := RE_Compare_Array_S8;
1561 end if;
1562 end if;
1564 elsif Component_Size (Typ1) = 16 then
1565 if Is_Unsigned_Type (Ctyp) then
1566 Comp := RE_Compare_Array_U16;
1567 else
1568 Comp := RE_Compare_Array_S16;
1569 end if;
1571 elsif Component_Size (Typ1) = 32 then
1572 if Is_Unsigned_Type (Ctyp) then
1573 Comp := RE_Compare_Array_U32;
1574 else
1575 Comp := RE_Compare_Array_S32;
1576 end if;
1578 else pragma Assert (Component_Size (Typ1) = 64);
1579 if Is_Unsigned_Type (Ctyp) then
1580 Comp := RE_Compare_Array_U64;
1581 else
1582 Comp := RE_Compare_Array_S64;
1583 end if;
1584 end if;
1586 Remove_Side_Effects (Op1, Name_Req => True);
1587 Remove_Side_Effects (Op2, Name_Req => True);
1589 Rewrite (Op1,
1590 Make_Function_Call (Sloc (Op1),
1591 Name => New_Occurrence_Of (RTE (Comp), Loc),
1593 Parameter_Associations => New_List (
1594 Make_Attribute_Reference (Loc,
1595 Prefix => Relocate_Node (Op1),
1596 Attribute_Name => Name_Address),
1598 Make_Attribute_Reference (Loc,
1599 Prefix => Relocate_Node (Op2),
1600 Attribute_Name => Name_Address),
1602 Make_Attribute_Reference (Loc,
1603 Prefix => Relocate_Node (Op1),
1604 Attribute_Name => Name_Length),
1606 Make_Attribute_Reference (Loc,
1607 Prefix => Relocate_Node (Op2),
1608 Attribute_Name => Name_Length))));
1610 Rewrite (Op2,
1611 Make_Integer_Literal (Sloc (Op2),
1612 Intval => Uint_0));
1614 Analyze_And_Resolve (Op1, Standard_Integer);
1615 Analyze_And_Resolve (Op2, Standard_Integer);
1616 return;
1617 end if;
1619 -- Cases where we cannot make runtime call
1621 -- For (a <= b) we convert to not (a > b)
1623 if Chars (N) = Name_Op_Le then
1624 Rewrite (N,
1625 Make_Op_Not (Loc,
1626 Right_Opnd =>
1627 Make_Op_Gt (Loc,
1628 Left_Opnd => Op1,
1629 Right_Opnd => Op2)));
1630 Analyze_And_Resolve (N, Standard_Boolean);
1631 return;
1633 -- For < the Boolean expression is
1634 -- greater__nn (op2, op1)
1636 elsif Chars (N) = Name_Op_Lt then
1637 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1639 -- Switch operands
1641 Op1 := Right_Opnd (N);
1642 Op2 := Left_Opnd (N);
1644 -- For (a >= b) we convert to not (a < b)
1646 elsif Chars (N) = Name_Op_Ge then
1647 Rewrite (N,
1648 Make_Op_Not (Loc,
1649 Right_Opnd =>
1650 Make_Op_Lt (Loc,
1651 Left_Opnd => Op1,
1652 Right_Opnd => Op2)));
1653 Analyze_And_Resolve (N, Standard_Boolean);
1654 return;
1656 -- For > the Boolean expression is
1657 -- greater__nn (op1, op2)
1659 else
1660 pragma Assert (Chars (N) = Name_Op_Gt);
1661 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1662 end if;
1664 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1665 Expr :=
1666 Make_Function_Call (Loc,
1667 Name => New_Occurrence_Of (Func_Name, Loc),
1668 Parameter_Associations => New_List (Op1, Op2));
1670 Insert_Action (N, Func_Body);
1671 Rewrite (N, Expr);
1672 Analyze_And_Resolve (N, Standard_Boolean);
1674 exception
1675 when RE_Not_Available =>
1676 return;
1677 end Expand_Array_Comparison;
1679 ---------------------------
1680 -- Expand_Array_Equality --
1681 ---------------------------
1683 -- Expand an equality function for multi-dimensional arrays. Here is an
1684 -- example of such a function for Nb_Dimension = 2
1686 -- function Enn (A : atyp; B : btyp) return boolean is
1687 -- begin
1688 -- if (A'length (1) = 0 or else A'length (2) = 0)
1689 -- and then
1690 -- (B'length (1) = 0 or else B'length (2) = 0)
1691 -- then
1692 -- return True; -- RM 4.5.2(22)
1693 -- end if;
1695 -- if A'length (1) /= B'length (1)
1696 -- or else
1697 -- A'length (2) /= B'length (2)
1698 -- then
1699 -- return False; -- RM 4.5.2(23)
1700 -- end if;
1702 -- declare
1703 -- A1 : Index_T1 := A'first (1);
1704 -- B1 : Index_T1 := B'first (1);
1705 -- begin
1706 -- loop
1707 -- declare
1708 -- A2 : Index_T2 := A'first (2);
1709 -- B2 : Index_T2 := B'first (2);
1710 -- begin
1711 -- loop
1712 -- if A (A1, A2) /= B (B1, B2) then
1713 -- return False;
1714 -- end if;
1716 -- exit when A2 = A'last (2);
1717 -- A2 := Index_T2'succ (A2);
1718 -- B2 := Index_T2'succ (B2);
1719 -- end loop;
1720 -- end;
1722 -- exit when A1 = A'last (1);
1723 -- A1 := Index_T1'succ (A1);
1724 -- B1 := Index_T1'succ (B1);
1725 -- end loop;
1726 -- end;
1728 -- return true;
1729 -- end Enn;
1731 -- Note on the formal types used (atyp and btyp). If either of the arrays
1732 -- is of a private type, we use the underlying type, and do an unchecked
1733 -- conversion of the actual. If either of the arrays has a bound depending
1734 -- on a discriminant, then we use the base type since otherwise we have an
1735 -- escaped discriminant in the function.
1737 -- If both arrays are constrained and have the same bounds, we can generate
1738 -- a loop with an explicit iteration scheme using a 'Range attribute over
1739 -- the first array.
1741 function Expand_Array_Equality
1742 (Nod : Node_Id;
1743 Lhs : Node_Id;
1744 Rhs : Node_Id;
1745 Bodies : List_Id;
1746 Typ : Entity_Id) return Node_Id
1748 Loc : constant Source_Ptr := Sloc (Nod);
1749 Decls : constant List_Id := New_List;
1750 Index_List1 : constant List_Id := New_List;
1751 Index_List2 : constant List_Id := New_List;
1753 Actuals : List_Id;
1754 Formals : List_Id;
1755 Func_Name : Entity_Id;
1756 Func_Body : Node_Id;
1758 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1759 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1761 Ltyp : Entity_Id;
1762 Rtyp : Entity_Id;
1763 -- The parameter types to be used for the formals
1765 function Arr_Attr
1766 (Arr : Entity_Id;
1767 Nam : Name_Id;
1768 Num : Int) return Node_Id;
1769 -- This builds the attribute reference Arr'Nam (Expr)
1771 function Component_Equality (Typ : Entity_Id) return Node_Id;
1772 -- Create one statement to compare corresponding components, designated
1773 -- by a full set of indexes.
1775 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1776 -- Given one of the arguments, computes the appropriate type to be used
1777 -- for that argument in the corresponding function formal
1779 function Handle_One_Dimension
1780 (N : Int;
1781 Index : Node_Id) return Node_Id;
1782 -- This procedure returns the following code
1784 -- declare
1785 -- Bn : Index_T := B'First (N);
1786 -- begin
1787 -- loop
1788 -- xxx
1789 -- exit when An = A'Last (N);
1790 -- An := Index_T'Succ (An)
1791 -- Bn := Index_T'Succ (Bn)
1792 -- end loop;
1793 -- end;
1795 -- If both indexes are constrained and identical, the procedure
1796 -- returns a simpler loop:
1798 -- for An in A'Range (N) loop
1799 -- xxx
1800 -- end loop
1802 -- N is the dimension for which we are generating a loop. Index is the
1803 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1804 -- xxx statement is either the loop or declare for the next dimension
1805 -- or if this is the last dimension the comparison of corresponding
1806 -- components of the arrays.
1808 -- The actual way the code works is to return the comparison of
1809 -- corresponding components for the N+1 call. That's neater.
1811 function Test_Empty_Arrays return Node_Id;
1812 -- This function constructs the test for both arrays being empty
1813 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1814 -- and then
1815 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1817 function Test_Lengths_Correspond return Node_Id;
1818 -- This function constructs the test for arrays having different lengths
1819 -- in at least one index position, in which case the resulting code is:
1821 -- A'length (1) /= B'length (1)
1822 -- or else
1823 -- A'length (2) /= B'length (2)
1824 -- or else
1825 -- ...
1827 --------------
1828 -- Arr_Attr --
1829 --------------
1831 function Arr_Attr
1832 (Arr : Entity_Id;
1833 Nam : Name_Id;
1834 Num : Int) return Node_Id
1836 begin
1837 return
1838 Make_Attribute_Reference (Loc,
1839 Attribute_Name => Nam,
1840 Prefix => New_Occurrence_Of (Arr, Loc),
1841 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1842 end Arr_Attr;
1844 ------------------------
1845 -- Component_Equality --
1846 ------------------------
1848 function Component_Equality (Typ : Entity_Id) return Node_Id is
1849 Test : Node_Id;
1850 L, R : Node_Id;
1852 begin
1853 -- if a(i1...) /= b(j1...) then return false; end if;
1855 L :=
1856 Make_Indexed_Component (Loc,
1857 Prefix => Make_Identifier (Loc, Chars (A)),
1858 Expressions => Index_List1);
1860 R :=
1861 Make_Indexed_Component (Loc,
1862 Prefix => Make_Identifier (Loc, Chars (B)),
1863 Expressions => Index_List2);
1865 Test := Expand_Composite_Equality
1866 (Nod, Component_Type (Typ), L, R, Decls);
1868 -- If some (sub)component is an unchecked_union, the whole operation
1869 -- will raise program error.
1871 if Nkind (Test) = N_Raise_Program_Error then
1873 -- This node is going to be inserted at a location where a
1874 -- statement is expected: clear its Etype so analysis will set
1875 -- it to the expected Standard_Void_Type.
1877 Set_Etype (Test, Empty);
1878 return Test;
1880 else
1881 return
1882 Make_Implicit_If_Statement (Nod,
1883 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1884 Then_Statements => New_List (
1885 Make_Simple_Return_Statement (Loc,
1886 Expression => New_Occurrence_Of (Standard_False, Loc))));
1887 end if;
1888 end Component_Equality;
1890 ------------------
1891 -- Get_Arg_Type --
1892 ------------------
1894 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1895 T : Entity_Id;
1896 X : Node_Id;
1898 begin
1899 T := Etype (N);
1901 if No (T) then
1902 return Typ;
1904 else
1905 T := Underlying_Type (T);
1907 X := First_Index (T);
1908 while Present (X) loop
1909 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1910 or else
1911 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1912 then
1913 T := Base_Type (T);
1914 exit;
1915 end if;
1917 Next_Index (X);
1918 end loop;
1920 return T;
1921 end if;
1922 end Get_Arg_Type;
1924 --------------------------
1925 -- Handle_One_Dimension --
1926 ---------------------------
1928 function Handle_One_Dimension
1929 (N : Int;
1930 Index : Node_Id) return Node_Id
1932 Need_Separate_Indexes : constant Boolean :=
1933 Ltyp /= Rtyp or else not Is_Constrained (Ltyp);
1934 -- If the index types are identical, and we are working with
1935 -- constrained types, then we can use the same index for both
1936 -- of the arrays.
1938 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1940 Bn : Entity_Id;
1941 Index_T : Entity_Id;
1942 Stm_List : List_Id;
1943 Loop_Stm : Node_Id;
1945 begin
1946 if N > Number_Dimensions (Ltyp) then
1947 return Component_Equality (Ltyp);
1948 end if;
1950 -- Case where we generate a loop
1952 Index_T := Base_Type (Etype (Index));
1954 if Need_Separate_Indexes then
1955 Bn := Make_Temporary (Loc, 'B');
1956 else
1957 Bn := An;
1958 end if;
1960 Append (New_Occurrence_Of (An, Loc), Index_List1);
1961 Append (New_Occurrence_Of (Bn, Loc), Index_List2);
1963 Stm_List := New_List (
1964 Handle_One_Dimension (N + 1, Next_Index (Index)));
1966 if Need_Separate_Indexes then
1968 -- Generate guard for loop, followed by increments of indexes
1970 Append_To (Stm_List,
1971 Make_Exit_Statement (Loc,
1972 Condition =>
1973 Make_Op_Eq (Loc,
1974 Left_Opnd => New_Occurrence_Of (An, Loc),
1975 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1977 Append_To (Stm_List,
1978 Make_Assignment_Statement (Loc,
1979 Name => New_Occurrence_Of (An, Loc),
1980 Expression =>
1981 Make_Attribute_Reference (Loc,
1982 Prefix => New_Occurrence_Of (Index_T, Loc),
1983 Attribute_Name => Name_Succ,
1984 Expressions => New_List (
1985 New_Occurrence_Of (An, Loc)))));
1987 Append_To (Stm_List,
1988 Make_Assignment_Statement (Loc,
1989 Name => New_Occurrence_Of (Bn, Loc),
1990 Expression =>
1991 Make_Attribute_Reference (Loc,
1992 Prefix => New_Occurrence_Of (Index_T, Loc),
1993 Attribute_Name => Name_Succ,
1994 Expressions => New_List (
1995 New_Occurrence_Of (Bn, Loc)))));
1996 end if;
1998 -- If separate indexes, we need a declare block for An and Bn, and a
1999 -- loop without an iteration scheme.
2001 if Need_Separate_Indexes then
2002 Loop_Stm :=
2003 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
2005 return
2006 Make_Block_Statement (Loc,
2007 Declarations => New_List (
2008 Make_Object_Declaration (Loc,
2009 Defining_Identifier => An,
2010 Object_Definition => New_Occurrence_Of (Index_T, Loc),
2011 Expression => Arr_Attr (A, Name_First, N)),
2013 Make_Object_Declaration (Loc,
2014 Defining_Identifier => Bn,
2015 Object_Definition => New_Occurrence_Of (Index_T, Loc),
2016 Expression => Arr_Attr (B, Name_First, N))),
2018 Handled_Statement_Sequence =>
2019 Make_Handled_Sequence_Of_Statements (Loc,
2020 Statements => New_List (Loop_Stm)));
2022 -- If no separate indexes, return loop statement with explicit
2023 -- iteration scheme on its own
2025 else
2026 Loop_Stm :=
2027 Make_Implicit_Loop_Statement (Nod,
2028 Statements => Stm_List,
2029 Iteration_Scheme =>
2030 Make_Iteration_Scheme (Loc,
2031 Loop_Parameter_Specification =>
2032 Make_Loop_Parameter_Specification (Loc,
2033 Defining_Identifier => An,
2034 Discrete_Subtype_Definition =>
2035 Arr_Attr (A, Name_Range, N))));
2036 return Loop_Stm;
2037 end if;
2038 end Handle_One_Dimension;
2040 -----------------------
2041 -- Test_Empty_Arrays --
2042 -----------------------
2044 function Test_Empty_Arrays return Node_Id is
2045 Alist : Node_Id;
2046 Blist : Node_Id;
2048 Atest : Node_Id;
2049 Btest : Node_Id;
2051 begin
2052 Alist := Empty;
2053 Blist := Empty;
2054 for J in 1 .. Number_Dimensions (Ltyp) loop
2055 Atest :=
2056 Make_Op_Eq (Loc,
2057 Left_Opnd => Arr_Attr (A, Name_Length, J),
2058 Right_Opnd => Make_Integer_Literal (Loc, 0));
2060 Btest :=
2061 Make_Op_Eq (Loc,
2062 Left_Opnd => Arr_Attr (B, Name_Length, J),
2063 Right_Opnd => Make_Integer_Literal (Loc, 0));
2065 if No (Alist) then
2066 Alist := Atest;
2067 Blist := Btest;
2069 else
2070 Alist :=
2071 Make_Or_Else (Loc,
2072 Left_Opnd => Relocate_Node (Alist),
2073 Right_Opnd => Atest);
2075 Blist :=
2076 Make_Or_Else (Loc,
2077 Left_Opnd => Relocate_Node (Blist),
2078 Right_Opnd => Btest);
2079 end if;
2080 end loop;
2082 return
2083 Make_And_Then (Loc,
2084 Left_Opnd => Alist,
2085 Right_Opnd => Blist);
2086 end Test_Empty_Arrays;
2088 -----------------------------
2089 -- Test_Lengths_Correspond --
2090 -----------------------------
2092 function Test_Lengths_Correspond return Node_Id is
2093 Result : Node_Id;
2094 Rtest : Node_Id;
2096 begin
2097 Result := Empty;
2098 for J in 1 .. Number_Dimensions (Ltyp) loop
2099 Rtest :=
2100 Make_Op_Ne (Loc,
2101 Left_Opnd => Arr_Attr (A, Name_Length, J),
2102 Right_Opnd => Arr_Attr (B, Name_Length, J));
2104 if No (Result) then
2105 Result := Rtest;
2106 else
2107 Result :=
2108 Make_Or_Else (Loc,
2109 Left_Opnd => Relocate_Node (Result),
2110 Right_Opnd => Rtest);
2111 end if;
2112 end loop;
2114 return Result;
2115 end Test_Lengths_Correspond;
2117 -- Start of processing for Expand_Array_Equality
2119 begin
2120 Ltyp := Get_Arg_Type (Lhs);
2121 Rtyp := Get_Arg_Type (Rhs);
2123 -- For now, if the argument types are not the same, go to the base type,
2124 -- since the code assumes that the formals have the same type. This is
2125 -- fixable in future ???
2127 if Ltyp /= Rtyp then
2128 Ltyp := Base_Type (Ltyp);
2129 Rtyp := Base_Type (Rtyp);
2130 pragma Assert (Ltyp = Rtyp);
2131 end if;
2133 -- Build list of formals for function
2135 Formals := New_List (
2136 Make_Parameter_Specification (Loc,
2137 Defining_Identifier => A,
2138 Parameter_Type => New_Occurrence_Of (Ltyp, Loc)),
2140 Make_Parameter_Specification (Loc,
2141 Defining_Identifier => B,
2142 Parameter_Type => New_Occurrence_Of (Rtyp, Loc)));
2144 Func_Name := Make_Temporary (Loc, 'E');
2146 -- Build statement sequence for function
2148 Func_Body :=
2149 Make_Subprogram_Body (Loc,
2150 Specification =>
2151 Make_Function_Specification (Loc,
2152 Defining_Unit_Name => Func_Name,
2153 Parameter_Specifications => Formals,
2154 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
2156 Declarations => Decls,
2158 Handled_Statement_Sequence =>
2159 Make_Handled_Sequence_Of_Statements (Loc,
2160 Statements => New_List (
2162 Make_Implicit_If_Statement (Nod,
2163 Condition => Test_Empty_Arrays,
2164 Then_Statements => New_List (
2165 Make_Simple_Return_Statement (Loc,
2166 Expression =>
2167 New_Occurrence_Of (Standard_True, Loc)))),
2169 Make_Implicit_If_Statement (Nod,
2170 Condition => Test_Lengths_Correspond,
2171 Then_Statements => New_List (
2172 Make_Simple_Return_Statement (Loc,
2173 Expression => New_Occurrence_Of (Standard_False, Loc)))),
2175 Handle_One_Dimension (1, First_Index (Ltyp)),
2177 Make_Simple_Return_Statement (Loc,
2178 Expression => New_Occurrence_Of (Standard_True, Loc)))));
2180 Set_Has_Completion (Func_Name, True);
2181 Set_Is_Inlined (Func_Name);
2183 -- If the array type is distinct from the type of the arguments, it
2184 -- is the full view of a private type. Apply an unchecked conversion
2185 -- to insure that analysis of the call succeeds.
2187 declare
2188 L, R : Node_Id;
2190 begin
2191 L := Lhs;
2192 R := Rhs;
2194 if No (Etype (Lhs))
2195 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
2196 then
2197 L := OK_Convert_To (Ltyp, Lhs);
2198 end if;
2200 if No (Etype (Rhs))
2201 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
2202 then
2203 R := OK_Convert_To (Rtyp, Rhs);
2204 end if;
2206 Actuals := New_List (L, R);
2207 end;
2209 Append_To (Bodies, Func_Body);
2211 return
2212 Make_Function_Call (Loc,
2213 Name => New_Occurrence_Of (Func_Name, Loc),
2214 Parameter_Associations => Actuals);
2215 end Expand_Array_Equality;
2217 -----------------------------
2218 -- Expand_Boolean_Operator --
2219 -----------------------------
2221 -- Note that we first get the actual subtypes of the operands, since we
2222 -- always want to deal with types that have bounds.
2224 procedure Expand_Boolean_Operator (N : Node_Id) is
2225 Typ : constant Entity_Id := Etype (N);
2227 begin
2228 -- Special case of bit packed array where both operands are known to be
2229 -- properly aligned. In this case we use an efficient run time routine
2230 -- to carry out the operation (see System.Bit_Ops).
2232 if Is_Bit_Packed_Array (Typ)
2233 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
2234 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
2235 then
2236 Expand_Packed_Boolean_Operator (N);
2237 return;
2238 end if;
2240 -- For the normal non-packed case, the general expansion is to build
2241 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2242 -- and then inserting it into the tree. The original operator node is
2243 -- then rewritten as a call to this function. We also use this in the
2244 -- packed case if either operand is a possibly unaligned object.
2246 declare
2247 Loc : constant Source_Ptr := Sloc (N);
2248 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2249 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2250 Func_Body : Node_Id;
2251 Func_Name : Entity_Id;
2253 begin
2254 Convert_To_Actual_Subtype (L);
2255 Convert_To_Actual_Subtype (R);
2256 Ensure_Defined (Etype (L), N);
2257 Ensure_Defined (Etype (R), N);
2258 Apply_Length_Check (R, Etype (L));
2260 if Nkind (N) = N_Op_Xor then
2261 Silly_Boolean_Array_Xor_Test (N, Etype (L));
2262 end if;
2264 if Nkind (Parent (N)) = N_Assignment_Statement
2265 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
2266 then
2267 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
2269 elsif Nkind (Parent (N)) = N_Op_Not
2270 and then Nkind (N) = N_Op_And
2271 and then Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
2272 then
2273 return;
2274 else
2276 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
2277 Func_Name := Defining_Unit_Name (Specification (Func_Body));
2278 Insert_Action (N, Func_Body);
2280 -- Now rewrite the expression with a call
2282 Rewrite (N,
2283 Make_Function_Call (Loc,
2284 Name => New_Occurrence_Of (Func_Name, Loc),
2285 Parameter_Associations =>
2286 New_List (
2288 Make_Type_Conversion
2289 (Loc, New_Occurrence_Of (Etype (L), Loc), R))));
2291 Analyze_And_Resolve (N, Typ);
2292 end if;
2293 end;
2294 end Expand_Boolean_Operator;
2296 ------------------------------------------------
2297 -- Expand_Compare_Minimize_Eliminate_Overflow --
2298 ------------------------------------------------
2300 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is
2301 Loc : constant Source_Ptr := Sloc (N);
2303 Result_Type : constant Entity_Id := Etype (N);
2304 -- Capture result type (could be a derived boolean type)
2306 Llo, Lhi : Uint;
2307 Rlo, Rhi : Uint;
2309 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
2310 -- Entity for Long_Long_Integer'Base
2312 Check : constant Overflow_Mode_Type := Overflow_Check_Mode;
2313 -- Current overflow checking mode
2315 procedure Set_True;
2316 procedure Set_False;
2317 -- These procedures rewrite N with an occurrence of Standard_True or
2318 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2320 ---------------
2321 -- Set_False --
2322 ---------------
2324 procedure Set_False is
2325 begin
2326 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2327 Warn_On_Known_Condition (N);
2328 end Set_False;
2330 --------------
2331 -- Set_True --
2332 --------------
2334 procedure Set_True is
2335 begin
2336 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
2337 Warn_On_Known_Condition (N);
2338 end Set_True;
2340 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2342 begin
2343 -- Nothing to do unless we have a comparison operator with operands
2344 -- that are signed integer types, and we are operating in either
2345 -- MINIMIZED or ELIMINATED overflow checking mode.
2347 if Nkind (N) not in N_Op_Compare
2348 or else Check not in Minimized_Or_Eliminated
2349 or else not Is_Signed_Integer_Type (Etype (Left_Opnd (N)))
2350 then
2351 return;
2352 end if;
2354 -- OK, this is the case we are interested in. First step is to process
2355 -- our operands using the Minimize_Eliminate circuitry which applies
2356 -- this processing to the two operand subtrees.
2358 Minimize_Eliminate_Overflows
2359 (Left_Opnd (N), Llo, Lhi, Top_Level => False);
2360 Minimize_Eliminate_Overflows
2361 (Right_Opnd (N), Rlo, Rhi, Top_Level => False);
2363 -- See if the range information decides the result of the comparison.
2364 -- We can only do this if we in fact have full range information (which
2365 -- won't be the case if either operand is bignum at this stage).
2367 if Llo /= No_Uint and then Rlo /= No_Uint then
2368 case N_Op_Compare (Nkind (N)) is
2369 when N_Op_Eq =>
2370 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2371 Set_True;
2372 elsif Llo > Rhi or else Lhi < Rlo then
2373 Set_False;
2374 end if;
2376 when N_Op_Ge =>
2377 if Llo >= Rhi then
2378 Set_True;
2379 elsif Lhi < Rlo then
2380 Set_False;
2381 end if;
2383 when N_Op_Gt =>
2384 if Llo > Rhi then
2385 Set_True;
2386 elsif Lhi <= Rlo then
2387 Set_False;
2388 end if;
2390 when N_Op_Le =>
2391 if Llo > Rhi then
2392 Set_False;
2393 elsif Lhi <= Rlo then
2394 Set_True;
2395 end if;
2397 when N_Op_Lt =>
2398 if Llo >= Rhi then
2399 Set_False;
2400 elsif Lhi < Rlo then
2401 Set_True;
2402 end if;
2404 when N_Op_Ne =>
2405 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2406 Set_False;
2407 elsif Llo > Rhi or else Lhi < Rlo then
2408 Set_True;
2409 end if;
2410 end case;
2412 -- All done if we did the rewrite
2414 if Nkind (N) not in N_Op_Compare then
2415 return;
2416 end if;
2417 end if;
2419 -- Otherwise, time to do the comparison
2421 declare
2422 Ltype : constant Entity_Id := Etype (Left_Opnd (N));
2423 Rtype : constant Entity_Id := Etype (Right_Opnd (N));
2425 begin
2426 -- If the two operands have the same signed integer type we are
2427 -- all set, nothing more to do. This is the case where either
2428 -- both operands were unchanged, or we rewrote both of them to
2429 -- be Long_Long_Integer.
2431 -- Note: Entity for the comparison may be wrong, but it's not worth
2432 -- the effort to change it, since the back end does not use it.
2434 if Is_Signed_Integer_Type (Ltype)
2435 and then Base_Type (Ltype) = Base_Type (Rtype)
2436 then
2437 return;
2439 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2441 elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then
2442 declare
2443 Left : Node_Id := Left_Opnd (N);
2444 Right : Node_Id := Right_Opnd (N);
2445 -- Bignum references for left and right operands
2447 begin
2448 if not Is_RTE (Ltype, RE_Bignum) then
2449 Left := Convert_To_Bignum (Left);
2450 elsif not Is_RTE (Rtype, RE_Bignum) then
2451 Right := Convert_To_Bignum (Right);
2452 end if;
2454 -- We rewrite our node with:
2456 -- do
2457 -- Bnn : Result_Type;
2458 -- declare
2459 -- M : Mark_Id := SS_Mark;
2460 -- begin
2461 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2462 -- SS_Release (M);
2463 -- end;
2464 -- in
2465 -- Bnn
2466 -- end
2468 declare
2469 Blk : constant Node_Id := Make_Bignum_Block (Loc);
2470 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
2471 Ent : RE_Id;
2473 begin
2474 case N_Op_Compare (Nkind (N)) is
2475 when N_Op_Eq => Ent := RE_Big_EQ;
2476 when N_Op_Ge => Ent := RE_Big_GE;
2477 when N_Op_Gt => Ent := RE_Big_GT;
2478 when N_Op_Le => Ent := RE_Big_LE;
2479 when N_Op_Lt => Ent := RE_Big_LT;
2480 when N_Op_Ne => Ent := RE_Big_NE;
2481 end case;
2483 -- Insert assignment to Bnn into the bignum block
2485 Insert_Before
2486 (First (Statements (Handled_Statement_Sequence (Blk))),
2487 Make_Assignment_Statement (Loc,
2488 Name => New_Occurrence_Of (Bnn, Loc),
2489 Expression =>
2490 Make_Function_Call (Loc,
2491 Name =>
2492 New_Occurrence_Of (RTE (Ent), Loc),
2493 Parameter_Associations => New_List (Left, Right))));
2495 -- Now do the rewrite with expression actions
2497 Rewrite (N,
2498 Make_Expression_With_Actions (Loc,
2499 Actions => New_List (
2500 Make_Object_Declaration (Loc,
2501 Defining_Identifier => Bnn,
2502 Object_Definition =>
2503 New_Occurrence_Of (Result_Type, Loc)),
2504 Blk),
2505 Expression => New_Occurrence_Of (Bnn, Loc)));
2506 Analyze_And_Resolve (N, Result_Type);
2507 end;
2508 end;
2510 -- No bignums involved, but types are different, so we must have
2511 -- rewritten one of the operands as a Long_Long_Integer but not
2512 -- the other one.
2514 -- If left operand is Long_Long_Integer, convert right operand
2515 -- and we are done (with a comparison of two Long_Long_Integers).
2517 elsif Ltype = LLIB then
2518 Convert_To_And_Rewrite (LLIB, Right_Opnd (N));
2519 Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks);
2520 return;
2522 -- If right operand is Long_Long_Integer, convert left operand
2523 -- and we are done (with a comparison of two Long_Long_Integers).
2525 -- This is the only remaining possibility
2527 else pragma Assert (Rtype = LLIB);
2528 Convert_To_And_Rewrite (LLIB, Left_Opnd (N));
2529 Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks);
2530 return;
2531 end if;
2532 end;
2533 end Expand_Compare_Minimize_Eliminate_Overflow;
2535 -------------------------------
2536 -- Expand_Composite_Equality --
2537 -------------------------------
2539 -- This function is only called for comparing internal fields of composite
2540 -- types when these fields are themselves composites. This is a special
2541 -- case because it is not possible to respect normal Ada visibility rules.
2543 function Expand_Composite_Equality
2544 (Nod : Node_Id;
2545 Typ : Entity_Id;
2546 Lhs : Node_Id;
2547 Rhs : Node_Id;
2548 Bodies : List_Id) return Node_Id
2550 Loc : constant Source_Ptr := Sloc (Nod);
2551 Full_Type : Entity_Id;
2552 Prim : Elmt_Id;
2553 Eq_Op : Entity_Id;
2555 function Find_Primitive_Eq return Node_Id;
2556 -- AI05-0123: Locate primitive equality for type if it exists, and
2557 -- build the corresponding call. If operation is abstract, replace
2558 -- call with an explicit raise. Return Empty if there is no primitive.
2560 -----------------------
2561 -- Find_Primitive_Eq --
2562 -----------------------
2564 function Find_Primitive_Eq return Node_Id is
2565 Prim_E : Elmt_Id;
2566 Prim : Node_Id;
2568 begin
2569 Prim_E := First_Elmt (Collect_Primitive_Operations (Typ));
2570 while Present (Prim_E) loop
2571 Prim := Node (Prim_E);
2573 -- Locate primitive equality with the right signature
2575 if Chars (Prim) = Name_Op_Eq
2576 and then Etype (First_Formal (Prim)) =
2577 Etype (Next_Formal (First_Formal (Prim)))
2578 and then Etype (Prim) = Standard_Boolean
2579 then
2580 if Is_Abstract_Subprogram (Prim) then
2581 return
2582 Make_Raise_Program_Error (Loc,
2583 Reason => PE_Explicit_Raise);
2585 else
2586 return
2587 Make_Function_Call (Loc,
2588 Name => New_Occurrence_Of (Prim, Loc),
2589 Parameter_Associations => New_List (Lhs, Rhs));
2590 end if;
2591 end if;
2593 Next_Elmt (Prim_E);
2594 end loop;
2596 -- If not found, predefined operation will be used
2598 return Empty;
2599 end Find_Primitive_Eq;
2601 -- Start of processing for Expand_Composite_Equality
2603 begin
2604 if Is_Private_Type (Typ) then
2605 Full_Type := Underlying_Type (Typ);
2606 else
2607 Full_Type := Typ;
2608 end if;
2610 -- If the private type has no completion the context may be the
2611 -- expansion of a composite equality for a composite type with some
2612 -- still incomplete components. The expression will not be analyzed
2613 -- until the enclosing type is completed, at which point this will be
2614 -- properly expanded, unless there is a bona fide completion error.
2616 if No (Full_Type) then
2617 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2618 end if;
2620 Full_Type := Base_Type (Full_Type);
2622 -- When the base type itself is private, use the full view to expand
2623 -- the composite equality.
2625 if Is_Private_Type (Full_Type) then
2626 Full_Type := Underlying_Type (Full_Type);
2627 end if;
2629 -- Case of array types
2631 if Is_Array_Type (Full_Type) then
2633 -- If the operand is an elementary type other than a floating-point
2634 -- type, then we can simply use the built-in block bitwise equality,
2635 -- since the predefined equality operators always apply and bitwise
2636 -- equality is fine for all these cases.
2638 if Is_Elementary_Type (Component_Type (Full_Type))
2639 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2640 then
2641 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2643 -- For composite component types, and floating-point types, use the
2644 -- expansion. This deals with tagged component types (where we use
2645 -- the applicable equality routine) and floating-point, (where we
2646 -- need to worry about negative zeroes), and also the case of any
2647 -- composite type recursively containing such fields.
2649 else
2650 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2651 end if;
2653 -- Case of tagged record types
2655 elsif Is_Tagged_Type (Full_Type) then
2657 -- Call the primitive operation "=" of this type
2659 if Is_Class_Wide_Type (Full_Type) then
2660 Full_Type := Root_Type (Full_Type);
2661 end if;
2663 -- If this is derived from an untagged private type completed with a
2664 -- tagged type, it does not have a full view, so we use the primitive
2665 -- operations of the private type. This check should no longer be
2666 -- necessary when these types receive their full views ???
2668 if Is_Private_Type (Typ)
2669 and then not Is_Tagged_Type (Typ)
2670 and then not Is_Controlled (Typ)
2671 and then Is_Derived_Type (Typ)
2672 and then No (Full_View (Typ))
2673 then
2674 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2675 else
2676 Prim := First_Elmt (Primitive_Operations (Full_Type));
2677 end if;
2679 loop
2680 Eq_Op := Node (Prim);
2681 exit when Chars (Eq_Op) = Name_Op_Eq
2682 and then Etype (First_Formal (Eq_Op)) =
2683 Etype (Next_Formal (First_Formal (Eq_Op)))
2684 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2685 Next_Elmt (Prim);
2686 pragma Assert (Present (Prim));
2687 end loop;
2689 Eq_Op := Node (Prim);
2691 return
2692 Make_Function_Call (Loc,
2693 Name => New_Occurrence_Of (Eq_Op, Loc),
2694 Parameter_Associations =>
2695 New_List
2696 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2697 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2699 -- Case of untagged record types
2701 elsif Is_Record_Type (Full_Type) then
2702 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2704 if Present (Eq_Op) then
2705 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2707 -- Inherited equality from parent type. Convert the actuals to
2708 -- match signature of operation.
2710 declare
2711 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2713 begin
2714 return
2715 Make_Function_Call (Loc,
2716 Name => New_Occurrence_Of (Eq_Op, Loc),
2717 Parameter_Associations => New_List (
2718 OK_Convert_To (T, Lhs),
2719 OK_Convert_To (T, Rhs)));
2720 end;
2722 else
2723 -- Comparison between Unchecked_Union components
2725 if Is_Unchecked_Union (Full_Type) then
2726 declare
2727 Lhs_Type : Node_Id := Full_Type;
2728 Rhs_Type : Node_Id := Full_Type;
2729 Lhs_Discr_Val : Node_Id;
2730 Rhs_Discr_Val : Node_Id;
2732 begin
2733 -- Lhs subtype
2735 if Nkind (Lhs) = N_Selected_Component then
2736 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2737 end if;
2739 -- Rhs subtype
2741 if Nkind (Rhs) = N_Selected_Component then
2742 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2743 end if;
2745 -- Lhs of the composite equality
2747 if Is_Constrained (Lhs_Type) then
2749 -- Since the enclosing record type can never be an
2750 -- Unchecked_Union (this code is executed for records
2751 -- that do not have variants), we may reference its
2752 -- discriminant(s).
2754 if Nkind (Lhs) = N_Selected_Component
2755 and then Has_Per_Object_Constraint
2756 (Entity (Selector_Name (Lhs)))
2757 then
2758 Lhs_Discr_Val :=
2759 Make_Selected_Component (Loc,
2760 Prefix => Prefix (Lhs),
2761 Selector_Name =>
2762 New_Copy
2763 (Get_Discriminant_Value
2764 (First_Discriminant (Lhs_Type),
2765 Lhs_Type,
2766 Stored_Constraint (Lhs_Type))));
2768 else
2769 Lhs_Discr_Val :=
2770 New_Copy
2771 (Get_Discriminant_Value
2772 (First_Discriminant (Lhs_Type),
2773 Lhs_Type,
2774 Stored_Constraint (Lhs_Type)));
2776 end if;
2777 else
2778 -- It is not possible to infer the discriminant since
2779 -- the subtype is not constrained.
2781 return
2782 Make_Raise_Program_Error (Loc,
2783 Reason => PE_Unchecked_Union_Restriction);
2784 end if;
2786 -- Rhs of the composite equality
2788 if Is_Constrained (Rhs_Type) then
2789 if Nkind (Rhs) = N_Selected_Component
2790 and then Has_Per_Object_Constraint
2791 (Entity (Selector_Name (Rhs)))
2792 then
2793 Rhs_Discr_Val :=
2794 Make_Selected_Component (Loc,
2795 Prefix => Prefix (Rhs),
2796 Selector_Name =>
2797 New_Copy
2798 (Get_Discriminant_Value
2799 (First_Discriminant (Rhs_Type),
2800 Rhs_Type,
2801 Stored_Constraint (Rhs_Type))));
2803 else
2804 Rhs_Discr_Val :=
2805 New_Copy
2806 (Get_Discriminant_Value
2807 (First_Discriminant (Rhs_Type),
2808 Rhs_Type,
2809 Stored_Constraint (Rhs_Type)));
2811 end if;
2812 else
2813 return
2814 Make_Raise_Program_Error (Loc,
2815 Reason => PE_Unchecked_Union_Restriction);
2816 end if;
2818 -- Call the TSS equality function with the inferred
2819 -- discriminant values.
2821 return
2822 Make_Function_Call (Loc,
2823 Name => New_Occurrence_Of (Eq_Op, Loc),
2824 Parameter_Associations => New_List (
2825 Lhs,
2826 Rhs,
2827 Lhs_Discr_Val,
2828 Rhs_Discr_Val));
2829 end;
2831 -- All cases other than comparing Unchecked_Union types
2833 else
2834 declare
2835 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2836 begin
2837 return
2838 Make_Function_Call (Loc,
2839 Name =>
2840 New_Occurrence_Of (Eq_Op, Loc),
2841 Parameter_Associations => New_List (
2842 OK_Convert_To (T, Lhs),
2843 OK_Convert_To (T, Rhs)));
2844 end;
2845 end if;
2846 end if;
2848 -- Equality composes in Ada 2012 for untagged record types. It also
2849 -- composes for bounded strings, because they are part of the
2850 -- predefined environment. We could make it compose for bounded
2851 -- strings by making them tagged, or by making sure all subcomponents
2852 -- are set to the same value, even when not used. Instead, we have
2853 -- this special case in the compiler, because it's more efficient.
2855 elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then
2857 -- If no TSS has been created for the type, check whether there is
2858 -- a primitive equality declared for it.
2860 declare
2861 Op : constant Node_Id := Find_Primitive_Eq;
2863 begin
2864 -- Use user-defined primitive if it exists, otherwise use
2865 -- predefined equality.
2867 if Present (Op) then
2868 return Op;
2869 else
2870 return Make_Op_Eq (Loc, Lhs, Rhs);
2871 end if;
2872 end;
2874 else
2875 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2876 end if;
2878 -- Non-composite types (always use predefined equality)
2880 else
2881 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2882 end if;
2883 end Expand_Composite_Equality;
2885 ------------------------
2886 -- Expand_Concatenate --
2887 ------------------------
2889 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2890 Loc : constant Source_Ptr := Sloc (Cnode);
2892 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2893 -- Result type of concatenation
2895 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2896 -- Component type. Elements of this component type can appear as one
2897 -- of the operands of concatenation as well as arrays.
2899 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2900 -- Index subtype
2902 Ityp : constant Entity_Id := Base_Type (Istyp);
2903 -- Index type. This is the base type of the index subtype, and is used
2904 -- for all computed bounds (which may be out of range of Istyp in the
2905 -- case of null ranges).
2907 Artyp : Entity_Id;
2908 -- This is the type we use to do arithmetic to compute the bounds and
2909 -- lengths of operands. The choice of this type is a little subtle and
2910 -- is discussed in a separate section at the start of the body code.
2912 Concatenation_Error : exception;
2913 -- Raised if concatenation is sure to raise a CE
2915 Result_May_Be_Null : Boolean := True;
2916 -- Reset to False if at least one operand is encountered which is known
2917 -- at compile time to be non-null. Used for handling the special case
2918 -- of setting the high bound to the last operand high bound for a null
2919 -- result, thus ensuring a proper high bound in the super-flat case.
2921 N : constant Nat := List_Length (Opnds);
2922 -- Number of concatenation operands including possibly null operands
2924 NN : Nat := 0;
2925 -- Number of operands excluding any known to be null, except that the
2926 -- last operand is always retained, in case it provides the bounds for
2927 -- a null result.
2929 Opnd : Node_Id;
2930 -- Current operand being processed in the loop through operands. After
2931 -- this loop is complete, always contains the last operand (which is not
2932 -- the same as Operands (NN), since null operands are skipped).
2934 -- Arrays describing the operands, only the first NN entries of each
2935 -- array are set (NN < N when we exclude known null operands).
2937 Is_Fixed_Length : array (1 .. N) of Boolean;
2938 -- True if length of corresponding operand known at compile time
2940 Operands : array (1 .. N) of Node_Id;
2941 -- Set to the corresponding entry in the Opnds list (but note that null
2942 -- operands are excluded, so not all entries in the list are stored).
2944 Fixed_Length : array (1 .. N) of Uint;
2945 -- Set to length of operand. Entries in this array are set only if the
2946 -- corresponding entry in Is_Fixed_Length is True.
2948 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2949 -- Set to lower bound of operand. Either an integer literal in the case
2950 -- where the bound is known at compile time, else actual lower bound.
2951 -- The operand low bound is of type Ityp.
2953 Var_Length : array (1 .. N) of Entity_Id;
2954 -- Set to an entity of type Natural that contains the length of an
2955 -- operand whose length is not known at compile time. Entries in this
2956 -- array are set only if the corresponding entry in Is_Fixed_Length
2957 -- is False. The entity is of type Artyp.
2959 Aggr_Length : array (0 .. N) of Node_Id;
2960 -- The J'th entry in an expression node that represents the total length
2961 -- of operands 1 through J. It is either an integer literal node, or a
2962 -- reference to a constant entity with the right value, so it is fine
2963 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2964 -- entry always is set to zero. The length is of type Artyp.
2966 Low_Bound : Node_Id;
2967 -- A tree node representing the low bound of the result (of type Ityp).
2968 -- This is either an integer literal node, or an identifier reference to
2969 -- a constant entity initialized to the appropriate value.
2971 Last_Opnd_Low_Bound : Node_Id;
2972 -- A tree node representing the low bound of the last operand. This
2973 -- need only be set if the result could be null. It is used for the
2974 -- special case of setting the right low bound for a null result.
2975 -- This is of type Ityp.
2977 Last_Opnd_High_Bound : Node_Id;
2978 -- A tree node representing the high bound of the last operand. This
2979 -- need only be set if the result could be null. It is used for the
2980 -- special case of setting the right high bound for a null result.
2981 -- This is of type Ityp.
2983 High_Bound : Node_Id;
2984 -- A tree node representing the high bound of the result (of type Ityp)
2986 Result : Node_Id;
2987 -- Result of the concatenation (of type Ityp)
2989 Actions : constant List_Id := New_List;
2990 -- Collect actions to be inserted
2992 Known_Non_Null_Operand_Seen : Boolean;
2993 -- Set True during generation of the assignments of operands into
2994 -- result once an operand known to be non-null has been seen.
2996 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2997 -- This function makes an N_Integer_Literal node that is returned in
2998 -- analyzed form with the type set to Artyp. Importantly this literal
2999 -- is not flagged as static, so that if we do computations with it that
3000 -- result in statically detected out of range conditions, we will not
3001 -- generate error messages but instead warning messages.
3003 function To_Artyp (X : Node_Id) return Node_Id;
3004 -- Given a node of type Ityp, returns the corresponding value of type
3005 -- Artyp. For non-enumeration types, this is a plain integer conversion.
3006 -- For enum types, the Pos of the value is returned.
3008 function To_Ityp (X : Node_Id) return Node_Id;
3009 -- The inverse function (uses Val in the case of enumeration types)
3011 ------------------------
3012 -- Make_Artyp_Literal --
3013 ------------------------
3015 function Make_Artyp_Literal (Val : Nat) return Node_Id is
3016 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
3017 begin
3018 Set_Etype (Result, Artyp);
3019 Set_Analyzed (Result, True);
3020 Set_Is_Static_Expression (Result, False);
3021 return Result;
3022 end Make_Artyp_Literal;
3024 --------------
3025 -- To_Artyp --
3026 --------------
3028 function To_Artyp (X : Node_Id) return Node_Id is
3029 begin
3030 if Ityp = Base_Type (Artyp) then
3031 return X;
3033 elsif Is_Enumeration_Type (Ityp) then
3034 return
3035 Make_Attribute_Reference (Loc,
3036 Prefix => New_Occurrence_Of (Ityp, Loc),
3037 Attribute_Name => Name_Pos,
3038 Expressions => New_List (X));
3040 else
3041 return Convert_To (Artyp, X);
3042 end if;
3043 end To_Artyp;
3045 -------------
3046 -- To_Ityp --
3047 -------------
3049 function To_Ityp (X : Node_Id) return Node_Id is
3050 begin
3051 if Is_Enumeration_Type (Ityp) then
3052 return
3053 Make_Attribute_Reference (Loc,
3054 Prefix => New_Occurrence_Of (Ityp, Loc),
3055 Attribute_Name => Name_Val,
3056 Expressions => New_List (X));
3058 -- Case where we will do a type conversion
3060 else
3061 if Ityp = Base_Type (Artyp) then
3062 return X;
3063 else
3064 return Convert_To (Ityp, X);
3065 end if;
3066 end if;
3067 end To_Ityp;
3069 -- Local Declarations
3071 Lib_Level_Target : constant Boolean :=
3072 Nkind (Parent (Cnode)) = N_Object_Declaration
3073 and then
3074 Is_Library_Level_Entity (Defining_Identifier (Parent (Cnode)));
3076 -- If the concatenation declares a library level entity, we call the
3077 -- built-in concatenation routines to prevent code bloat, regardless
3078 -- of optimization level. This is space-efficient, and prevent linking
3079 -- problems when units are compiled with different optimizations.
3081 Opnd_Typ : Entity_Id;
3082 Ent : Entity_Id;
3083 Len : Uint;
3084 J : Nat;
3085 Clen : Node_Id;
3086 Set : Boolean;
3088 -- Start of processing for Expand_Concatenate
3090 begin
3091 -- Choose an appropriate computational type
3093 -- We will be doing calculations of lengths and bounds in this routine
3094 -- and computing one from the other in some cases, e.g. getting the high
3095 -- bound by adding the length-1 to the low bound.
3097 -- We can't just use the index type, or even its base type for this
3098 -- purpose for two reasons. First it might be an enumeration type which
3099 -- is not suitable for computations of any kind, and second it may
3100 -- simply not have enough range. For example if the index type is
3101 -- -128..+127 then lengths can be up to 256, which is out of range of
3102 -- the type.
3104 -- For enumeration types, we can simply use Standard_Integer, this is
3105 -- sufficient since the actual number of enumeration literals cannot
3106 -- possibly exceed the range of integer (remember we will be doing the
3107 -- arithmetic with POS values, not representation values).
3109 if Is_Enumeration_Type (Ityp) then
3110 Artyp := Standard_Integer;
3112 -- If index type is Positive, we use the standard unsigned type, to give
3113 -- more room on the top of the range, obviating the need for an overflow
3114 -- check when creating the upper bound. This is needed to avoid junk
3115 -- overflow checks in the common case of String types.
3117 -- ??? Disabled for now
3119 -- elsif Istyp = Standard_Positive then
3120 -- Artyp := Standard_Unsigned;
3122 -- For modular types, we use a 32-bit modular type for types whose size
3123 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
3124 -- identity type, and for larger unsigned types we use 64-bits.
3126 elsif Is_Modular_Integer_Type (Ityp) then
3127 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
3128 Artyp := Standard_Unsigned;
3129 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
3130 Artyp := Ityp;
3131 else
3132 Artyp := RTE (RE_Long_Long_Unsigned);
3133 end if;
3135 -- Similar treatment for signed types
3137 else
3138 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
3139 Artyp := Standard_Integer;
3140 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
3141 Artyp := Ityp;
3142 else
3143 Artyp := Standard_Long_Long_Integer;
3144 end if;
3145 end if;
3147 -- Supply dummy entry at start of length array
3149 Aggr_Length (0) := Make_Artyp_Literal (0);
3151 -- Go through operands setting up the above arrays
3153 J := 1;
3154 while J <= N loop
3155 Opnd := Remove_Head (Opnds);
3156 Opnd_Typ := Etype (Opnd);
3158 -- The parent got messed up when we put the operands in a list,
3159 -- so now put back the proper parent for the saved operand, that
3160 -- is to say the concatenation node, to make sure that each operand
3161 -- is seen as a subexpression, e.g. if actions must be inserted.
3163 Set_Parent (Opnd, Cnode);
3165 -- Set will be True when we have setup one entry in the array
3167 Set := False;
3169 -- Singleton element (or character literal) case
3171 if Base_Type (Opnd_Typ) = Ctyp then
3172 NN := NN + 1;
3173 Operands (NN) := Opnd;
3174 Is_Fixed_Length (NN) := True;
3175 Fixed_Length (NN) := Uint_1;
3176 Result_May_Be_Null := False;
3178 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
3179 -- since we know that the result cannot be null).
3181 Opnd_Low_Bound (NN) :=
3182 Make_Attribute_Reference (Loc,
3183 Prefix => New_Occurrence_Of (Istyp, Loc),
3184 Attribute_Name => Name_First);
3186 Set := True;
3188 -- String literal case (can only occur for strings of course)
3190 elsif Nkind (Opnd) = N_String_Literal then
3191 Len := String_Literal_Length (Opnd_Typ);
3193 if Len /= 0 then
3194 Result_May_Be_Null := False;
3195 end if;
3197 -- Capture last operand low and high bound if result could be null
3199 if J = N and then Result_May_Be_Null then
3200 Last_Opnd_Low_Bound :=
3201 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3203 Last_Opnd_High_Bound :=
3204 Make_Op_Subtract (Loc,
3205 Left_Opnd =>
3206 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
3207 Right_Opnd => Make_Integer_Literal (Loc, 1));
3208 end if;
3210 -- Skip null string literal
3212 if J < N and then Len = 0 then
3213 goto Continue;
3214 end if;
3216 NN := NN + 1;
3217 Operands (NN) := Opnd;
3218 Is_Fixed_Length (NN) := True;
3220 -- Set length and bounds
3222 Fixed_Length (NN) := Len;
3224 Opnd_Low_Bound (NN) :=
3225 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3227 Set := True;
3229 -- All other cases
3231 else
3232 -- Check constrained case with known bounds
3234 if Is_Constrained (Opnd_Typ) then
3235 declare
3236 Index : constant Node_Id := First_Index (Opnd_Typ);
3237 Indx_Typ : constant Entity_Id := Etype (Index);
3238 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
3239 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
3241 begin
3242 -- Fixed length constrained array type with known at compile
3243 -- time bounds is last case of fixed length operand.
3245 if Compile_Time_Known_Value (Lo)
3246 and then
3247 Compile_Time_Known_Value (Hi)
3248 then
3249 declare
3250 Loval : constant Uint := Expr_Value (Lo);
3251 Hival : constant Uint := Expr_Value (Hi);
3252 Len : constant Uint :=
3253 UI_Max (Hival - Loval + 1, Uint_0);
3255 begin
3256 if Len > 0 then
3257 Result_May_Be_Null := False;
3258 end if;
3260 -- Capture last operand bounds if result could be null
3262 if J = N and then Result_May_Be_Null then
3263 Last_Opnd_Low_Bound :=
3264 Convert_To (Ityp,
3265 Make_Integer_Literal (Loc, Expr_Value (Lo)));
3267 Last_Opnd_High_Bound :=
3268 Convert_To (Ityp,
3269 Make_Integer_Literal (Loc, Expr_Value (Hi)));
3270 end if;
3272 -- Exclude null length case unless last operand
3274 if J < N and then Len = 0 then
3275 goto Continue;
3276 end if;
3278 NN := NN + 1;
3279 Operands (NN) := Opnd;
3280 Is_Fixed_Length (NN) := True;
3281 Fixed_Length (NN) := Len;
3283 Opnd_Low_Bound (NN) :=
3284 To_Ityp
3285 (Make_Integer_Literal (Loc, Expr_Value (Lo)));
3286 Set := True;
3287 end;
3288 end if;
3289 end;
3290 end if;
3292 -- All cases where the length is not known at compile time, or the
3293 -- special case of an operand which is known to be null but has a
3294 -- lower bound other than 1 or is other than a string type.
3296 if not Set then
3297 NN := NN + 1;
3299 -- Capture operand bounds
3301 Opnd_Low_Bound (NN) :=
3302 Make_Attribute_Reference (Loc,
3303 Prefix =>
3304 Duplicate_Subexpr (Opnd, Name_Req => True),
3305 Attribute_Name => Name_First);
3307 -- Capture last operand bounds if result could be null
3309 if J = N and Result_May_Be_Null then
3310 Last_Opnd_Low_Bound :=
3311 Convert_To (Ityp,
3312 Make_Attribute_Reference (Loc,
3313 Prefix =>
3314 Duplicate_Subexpr (Opnd, Name_Req => True),
3315 Attribute_Name => Name_First));
3317 Last_Opnd_High_Bound :=
3318 Convert_To (Ityp,
3319 Make_Attribute_Reference (Loc,
3320 Prefix =>
3321 Duplicate_Subexpr (Opnd, Name_Req => True),
3322 Attribute_Name => Name_Last));
3323 end if;
3325 -- Capture length of operand in entity
3327 Operands (NN) := Opnd;
3328 Is_Fixed_Length (NN) := False;
3330 Var_Length (NN) := Make_Temporary (Loc, 'L');
3332 Append_To (Actions,
3333 Make_Object_Declaration (Loc,
3334 Defining_Identifier => Var_Length (NN),
3335 Constant_Present => True,
3336 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3337 Expression =>
3338 Make_Attribute_Reference (Loc,
3339 Prefix =>
3340 Duplicate_Subexpr (Opnd, Name_Req => True),
3341 Attribute_Name => Name_Length)));
3342 end if;
3343 end if;
3345 -- Set next entry in aggregate length array
3347 -- For first entry, make either integer literal for fixed length
3348 -- or a reference to the saved length for variable length.
3350 if NN = 1 then
3351 if Is_Fixed_Length (1) then
3352 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
3353 else
3354 Aggr_Length (1) := New_Occurrence_Of (Var_Length (1), Loc);
3355 end if;
3357 -- If entry is fixed length and only fixed lengths so far, make
3358 -- appropriate new integer literal adding new length.
3360 elsif Is_Fixed_Length (NN)
3361 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
3362 then
3363 Aggr_Length (NN) :=
3364 Make_Integer_Literal (Loc,
3365 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
3367 -- All other cases, construct an addition node for the length and
3368 -- create an entity initialized to this length.
3370 else
3371 Ent := Make_Temporary (Loc, 'L');
3373 if Is_Fixed_Length (NN) then
3374 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
3375 else
3376 Clen := New_Occurrence_Of (Var_Length (NN), Loc);
3377 end if;
3379 Append_To (Actions,
3380 Make_Object_Declaration (Loc,
3381 Defining_Identifier => Ent,
3382 Constant_Present => True,
3383 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3384 Expression =>
3385 Make_Op_Add (Loc,
3386 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
3387 Right_Opnd => Clen)));
3389 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
3390 end if;
3392 <<Continue>>
3393 J := J + 1;
3394 end loop;
3396 -- If we have only skipped null operands, return the last operand
3398 if NN = 0 then
3399 Result := Opnd;
3400 goto Done;
3401 end if;
3403 -- If we have only one non-null operand, return it and we are done.
3404 -- There is one case in which this cannot be done, and that is when
3405 -- the sole operand is of the element type, in which case it must be
3406 -- converted to an array, and the easiest way of doing that is to go
3407 -- through the normal general circuit.
3409 if NN = 1 and then Base_Type (Etype (Operands (1))) /= Ctyp then
3410 Result := Operands (1);
3411 goto Done;
3412 end if;
3414 -- Cases where we have a real concatenation
3416 -- Next step is to find the low bound for the result array that we
3417 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3419 -- If the ultimate ancestor of the index subtype is a constrained array
3420 -- definition, then the lower bound is that of the index subtype as
3421 -- specified by (RM 4.5.3(6)).
3423 -- The right test here is to go to the root type, and then the ultimate
3424 -- ancestor is the first subtype of this root type.
3426 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
3427 Low_Bound :=
3428 Make_Attribute_Reference (Loc,
3429 Prefix =>
3430 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
3431 Attribute_Name => Name_First);
3433 -- If the first operand in the list has known length we know that
3434 -- the lower bound of the result is the lower bound of this operand.
3436 elsif Is_Fixed_Length (1) then
3437 Low_Bound := Opnd_Low_Bound (1);
3439 -- OK, we don't know the lower bound, we have to build a horrible
3440 -- if expression node of the form
3442 -- if Cond1'Length /= 0 then
3443 -- Opnd1 low bound
3444 -- else
3445 -- if Opnd2'Length /= 0 then
3446 -- Opnd2 low bound
3447 -- else
3448 -- ...
3450 -- The nesting ends either when we hit an operand whose length is known
3451 -- at compile time, or on reaching the last operand, whose low bound we
3452 -- take unconditionally whether or not it is null. It's easiest to do
3453 -- this with a recursive procedure:
3455 else
3456 declare
3457 function Get_Known_Bound (J : Nat) return Node_Id;
3458 -- Returns the lower bound determined by operands J .. NN
3460 ---------------------
3461 -- Get_Known_Bound --
3462 ---------------------
3464 function Get_Known_Bound (J : Nat) return Node_Id is
3465 begin
3466 if Is_Fixed_Length (J) or else J = NN then
3467 return New_Copy (Opnd_Low_Bound (J));
3469 else
3470 return
3471 Make_If_Expression (Loc,
3472 Expressions => New_List (
3474 Make_Op_Ne (Loc,
3475 Left_Opnd =>
3476 New_Occurrence_Of (Var_Length (J), Loc),
3477 Right_Opnd =>
3478 Make_Integer_Literal (Loc, 0)),
3480 New_Copy (Opnd_Low_Bound (J)),
3481 Get_Known_Bound (J + 1)));
3482 end if;
3483 end Get_Known_Bound;
3485 begin
3486 Ent := Make_Temporary (Loc, 'L');
3488 Append_To (Actions,
3489 Make_Object_Declaration (Loc,
3490 Defining_Identifier => Ent,
3491 Constant_Present => True,
3492 Object_Definition => New_Occurrence_Of (Ityp, Loc),
3493 Expression => Get_Known_Bound (1)));
3495 Low_Bound := New_Occurrence_Of (Ent, Loc);
3496 end;
3497 end if;
3499 -- Now we can safely compute the upper bound, normally
3500 -- Low_Bound + Length - 1.
3502 High_Bound :=
3503 To_Ityp
3504 (Make_Op_Add (Loc,
3505 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3506 Right_Opnd =>
3507 Make_Op_Subtract (Loc,
3508 Left_Opnd => New_Copy (Aggr_Length (NN)),
3509 Right_Opnd => Make_Artyp_Literal (1))));
3511 -- Note that calculation of the high bound may cause overflow in some
3512 -- very weird cases, so in the general case we need an overflow check on
3513 -- the high bound. We can avoid this for the common case of string types
3514 -- and other types whose index is Positive, since we chose a wider range
3515 -- for the arithmetic type.
3517 if Istyp /= Standard_Positive then
3518 Activate_Overflow_Check (High_Bound);
3519 end if;
3521 -- Handle the exceptional case where the result is null, in which case
3522 -- case the bounds come from the last operand (so that we get the proper
3523 -- bounds if the last operand is super-flat).
3525 if Result_May_Be_Null then
3526 Low_Bound :=
3527 Make_If_Expression (Loc,
3528 Expressions => New_List (
3529 Make_Op_Eq (Loc,
3530 Left_Opnd => New_Copy (Aggr_Length (NN)),
3531 Right_Opnd => Make_Artyp_Literal (0)),
3532 Last_Opnd_Low_Bound,
3533 Low_Bound));
3535 High_Bound :=
3536 Make_If_Expression (Loc,
3537 Expressions => New_List (
3538 Make_Op_Eq (Loc,
3539 Left_Opnd => New_Copy (Aggr_Length (NN)),
3540 Right_Opnd => Make_Artyp_Literal (0)),
3541 Last_Opnd_High_Bound,
3542 High_Bound));
3543 end if;
3545 -- Here is where we insert the saved up actions
3547 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3549 -- Now we construct an array object with appropriate bounds. We mark
3550 -- the target as internal to prevent useless initialization when
3551 -- Initialize_Scalars is enabled. Also since this is the actual result
3552 -- entity, we make sure we have debug information for the result.
3554 Ent := Make_Temporary (Loc, 'S');
3555 Set_Is_Internal (Ent);
3556 Set_Needs_Debug_Info (Ent);
3558 -- If the bound is statically known to be out of range, we do not want
3559 -- to abort, we want a warning and a runtime constraint error. Note that
3560 -- we have arranged that the result will not be treated as a static
3561 -- constant, so we won't get an illegality during this insertion.
3563 Insert_Action (Cnode,
3564 Make_Object_Declaration (Loc,
3565 Defining_Identifier => Ent,
3566 Object_Definition =>
3567 Make_Subtype_Indication (Loc,
3568 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
3569 Constraint =>
3570 Make_Index_Or_Discriminant_Constraint (Loc,
3571 Constraints => New_List (
3572 Make_Range (Loc,
3573 Low_Bound => Low_Bound,
3574 High_Bound => High_Bound))))),
3575 Suppress => All_Checks);
3577 -- If the result of the concatenation appears as the initializing
3578 -- expression of an object declaration, we can just rename the
3579 -- result, rather than copying it.
3581 Set_OK_To_Rename (Ent);
3583 -- Catch the static out of range case now
3585 if Raises_Constraint_Error (High_Bound) then
3586 raise Concatenation_Error;
3587 end if;
3589 -- Now we will generate the assignments to do the actual concatenation
3591 -- There is one case in which we will not do this, namely when all the
3592 -- following conditions are met:
3594 -- The result type is Standard.String
3596 -- There are nine or fewer retained (non-null) operands
3598 -- The optimization level is -O0
3600 -- The corresponding System.Concat_n.Str_Concat_n routine is
3601 -- available in the run time.
3603 -- The debug flag gnatd.c is not set
3605 -- If all these conditions are met then we generate a call to the
3606 -- relevant concatenation routine. The purpose of this is to avoid
3607 -- undesirable code bloat at -O0.
3609 if Atyp = Standard_String
3610 and then NN in 2 .. 9
3611 and then (Lib_Level_Target
3612 or else ((Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
3613 and then not Debug_Flag_Dot_C))
3614 then
3615 declare
3616 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3617 (RE_Str_Concat_2,
3618 RE_Str_Concat_3,
3619 RE_Str_Concat_4,
3620 RE_Str_Concat_5,
3621 RE_Str_Concat_6,
3622 RE_Str_Concat_7,
3623 RE_Str_Concat_8,
3624 RE_Str_Concat_9);
3626 begin
3627 if RTE_Available (RR (NN)) then
3628 declare
3629 Opnds : constant List_Id :=
3630 New_List (New_Occurrence_Of (Ent, Loc));
3632 begin
3633 for J in 1 .. NN loop
3634 if Is_List_Member (Operands (J)) then
3635 Remove (Operands (J));
3636 end if;
3638 if Base_Type (Etype (Operands (J))) = Ctyp then
3639 Append_To (Opnds,
3640 Make_Aggregate (Loc,
3641 Component_Associations => New_List (
3642 Make_Component_Association (Loc,
3643 Choices => New_List (
3644 Make_Integer_Literal (Loc, 1)),
3645 Expression => Operands (J)))));
3647 else
3648 Append_To (Opnds, Operands (J));
3649 end if;
3650 end loop;
3652 Insert_Action (Cnode,
3653 Make_Procedure_Call_Statement (Loc,
3654 Name => New_Occurrence_Of (RTE (RR (NN)), Loc),
3655 Parameter_Associations => Opnds));
3657 Result := New_Occurrence_Of (Ent, Loc);
3658 goto Done;
3659 end;
3660 end if;
3661 end;
3662 end if;
3664 -- Not special case so generate the assignments
3666 Known_Non_Null_Operand_Seen := False;
3668 for J in 1 .. NN loop
3669 declare
3670 Lo : constant Node_Id :=
3671 Make_Op_Add (Loc,
3672 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3673 Right_Opnd => Aggr_Length (J - 1));
3675 Hi : constant Node_Id :=
3676 Make_Op_Add (Loc,
3677 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3678 Right_Opnd =>
3679 Make_Op_Subtract (Loc,
3680 Left_Opnd => Aggr_Length (J),
3681 Right_Opnd => Make_Artyp_Literal (1)));
3683 begin
3684 -- Singleton case, simple assignment
3686 if Base_Type (Etype (Operands (J))) = Ctyp then
3687 Known_Non_Null_Operand_Seen := True;
3688 Insert_Action (Cnode,
3689 Make_Assignment_Statement (Loc,
3690 Name =>
3691 Make_Indexed_Component (Loc,
3692 Prefix => New_Occurrence_Of (Ent, Loc),
3693 Expressions => New_List (To_Ityp (Lo))),
3694 Expression => Operands (J)),
3695 Suppress => All_Checks);
3697 -- Array case, slice assignment, skipped when argument is fixed
3698 -- length and known to be null.
3700 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3701 declare
3702 Assign : Node_Id :=
3703 Make_Assignment_Statement (Loc,
3704 Name =>
3705 Make_Slice (Loc,
3706 Prefix =>
3707 New_Occurrence_Of (Ent, Loc),
3708 Discrete_Range =>
3709 Make_Range (Loc,
3710 Low_Bound => To_Ityp (Lo),
3711 High_Bound => To_Ityp (Hi))),
3712 Expression => Operands (J));
3713 begin
3714 if Is_Fixed_Length (J) then
3715 Known_Non_Null_Operand_Seen := True;
3717 elsif not Known_Non_Null_Operand_Seen then
3719 -- Here if operand length is not statically known and no
3720 -- operand known to be non-null has been processed yet.
3721 -- If operand length is 0, we do not need to perform the
3722 -- assignment, and we must avoid the evaluation of the
3723 -- high bound of the slice, since it may underflow if the
3724 -- low bound is Ityp'First.
3726 Assign :=
3727 Make_Implicit_If_Statement (Cnode,
3728 Condition =>
3729 Make_Op_Ne (Loc,
3730 Left_Opnd =>
3731 New_Occurrence_Of (Var_Length (J), Loc),
3732 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3733 Then_Statements => New_List (Assign));
3734 end if;
3736 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3737 end;
3738 end if;
3739 end;
3740 end loop;
3742 -- Finally we build the result, which is a reference to the array object
3744 Result := New_Occurrence_Of (Ent, Loc);
3746 <<Done>>
3747 Rewrite (Cnode, Result);
3748 Analyze_And_Resolve (Cnode, Atyp);
3750 exception
3751 when Concatenation_Error =>
3753 -- Kill warning generated for the declaration of the static out of
3754 -- range high bound, and instead generate a Constraint_Error with
3755 -- an appropriate specific message.
3757 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3758 Apply_Compile_Time_Constraint_Error
3759 (N => Cnode,
3760 Msg => "concatenation result upper bound out of range??",
3761 Reason => CE_Range_Check_Failed);
3762 end Expand_Concatenate;
3764 ---------------------------------------------------
3765 -- Expand_Membership_Minimize_Eliminate_Overflow --
3766 ---------------------------------------------------
3768 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is
3769 pragma Assert (Nkind (N) = N_In);
3770 -- Despite the name, this routine applies only to N_In, not to
3771 -- N_Not_In. The latter is always rewritten as not (X in Y).
3773 Result_Type : constant Entity_Id := Etype (N);
3774 -- Capture result type, may be a derived boolean type
3776 Loc : constant Source_Ptr := Sloc (N);
3777 Lop : constant Node_Id := Left_Opnd (N);
3778 Rop : constant Node_Id := Right_Opnd (N);
3780 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3781 -- is thus tempting to capture these values, but due to the rewrites
3782 -- that occur as a result of overflow checking, these values change
3783 -- as we go along, and it is safe just to always use Etype explicitly.
3785 Restype : constant Entity_Id := Etype (N);
3786 -- Save result type
3788 Lo, Hi : Uint;
3789 -- Bounds in Minimize calls, not used currently
3791 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
3792 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3794 begin
3795 Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False);
3797 -- If right operand is a subtype name, and the subtype name has no
3798 -- predicate, then we can just replace the right operand with an
3799 -- explicit range T'First .. T'Last, and use the explicit range code.
3801 if Nkind (Rop) /= N_Range
3802 and then No (Predicate_Function (Etype (Rop)))
3803 then
3804 declare
3805 Rtyp : constant Entity_Id := Etype (Rop);
3806 begin
3807 Rewrite (Rop,
3808 Make_Range (Loc,
3809 Low_Bound =>
3810 Make_Attribute_Reference (Loc,
3811 Attribute_Name => Name_First,
3812 Prefix => New_Occurrence_Of (Rtyp, Loc)),
3813 High_Bound =>
3814 Make_Attribute_Reference (Loc,
3815 Attribute_Name => Name_Last,
3816 Prefix => New_Occurrence_Of (Rtyp, Loc))));
3817 Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks);
3818 end;
3819 end if;
3821 -- Here for the explicit range case. Note that the bounds of the range
3822 -- have not been processed for minimized or eliminated checks.
3824 if Nkind (Rop) = N_Range then
3825 Minimize_Eliminate_Overflows
3826 (Low_Bound (Rop), Lo, Hi, Top_Level => False);
3827 Minimize_Eliminate_Overflows
3828 (High_Bound (Rop), Lo, Hi, Top_Level => False);
3830 -- We have A in B .. C, treated as A >= B and then A <= C
3832 -- Bignum case
3834 if Is_RTE (Etype (Lop), RE_Bignum)
3835 or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum)
3836 or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum)
3837 then
3838 declare
3839 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3840 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3841 L : constant Entity_Id :=
3842 Make_Defining_Identifier (Loc, Name_uL);
3843 Lopnd : constant Node_Id := Convert_To_Bignum (Lop);
3844 Lbound : constant Node_Id :=
3845 Convert_To_Bignum (Low_Bound (Rop));
3846 Hbound : constant Node_Id :=
3847 Convert_To_Bignum (High_Bound (Rop));
3849 -- Now we rewrite the membership test node to look like
3851 -- do
3852 -- Bnn : Result_Type;
3853 -- declare
3854 -- M : Mark_Id := SS_Mark;
3855 -- L : Bignum := Lopnd;
3856 -- begin
3857 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3858 -- SS_Release (M);
3859 -- end;
3860 -- in
3861 -- Bnn
3862 -- end
3864 begin
3865 -- Insert declaration of L into declarations of bignum block
3867 Insert_After
3868 (Last (Declarations (Blk)),
3869 Make_Object_Declaration (Loc,
3870 Defining_Identifier => L,
3871 Object_Definition =>
3872 New_Occurrence_Of (RTE (RE_Bignum), Loc),
3873 Expression => Lopnd));
3875 -- Insert assignment to Bnn into expressions of bignum block
3877 Insert_Before
3878 (First (Statements (Handled_Statement_Sequence (Blk))),
3879 Make_Assignment_Statement (Loc,
3880 Name => New_Occurrence_Of (Bnn, Loc),
3881 Expression =>
3882 Make_And_Then (Loc,
3883 Left_Opnd =>
3884 Make_Function_Call (Loc,
3885 Name =>
3886 New_Occurrence_Of (RTE (RE_Big_GE), Loc),
3887 Parameter_Associations => New_List (
3888 New_Occurrence_Of (L, Loc),
3889 Lbound)),
3891 Right_Opnd =>
3892 Make_Function_Call (Loc,
3893 Name =>
3894 New_Occurrence_Of (RTE (RE_Big_LE), Loc),
3895 Parameter_Associations => New_List (
3896 New_Occurrence_Of (L, Loc),
3897 Hbound)))));
3899 -- Now rewrite the node
3901 Rewrite (N,
3902 Make_Expression_With_Actions (Loc,
3903 Actions => New_List (
3904 Make_Object_Declaration (Loc,
3905 Defining_Identifier => Bnn,
3906 Object_Definition =>
3907 New_Occurrence_Of (Result_Type, Loc)),
3908 Blk),
3909 Expression => New_Occurrence_Of (Bnn, Loc)));
3910 Analyze_And_Resolve (N, Result_Type);
3911 return;
3912 end;
3914 -- Here if no bignums around
3916 else
3917 -- Case where types are all the same
3919 if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop)))
3920 and then
3921 Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop)))
3922 then
3923 null;
3925 -- If types are not all the same, it means that we have rewritten
3926 -- at least one of them to be of type Long_Long_Integer, and we
3927 -- will convert the other operands to Long_Long_Integer.
3929 else
3930 Convert_To_And_Rewrite (LLIB, Lop);
3931 Set_Analyzed (Lop, False);
3932 Analyze_And_Resolve (Lop, LLIB);
3934 -- For the right operand, avoid unnecessary recursion into
3935 -- this routine, we know that overflow is not possible.
3937 Convert_To_And_Rewrite (LLIB, Low_Bound (Rop));
3938 Convert_To_And_Rewrite (LLIB, High_Bound (Rop));
3939 Set_Analyzed (Rop, False);
3940 Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check);
3941 end if;
3943 -- Now the three operands are of the same signed integer type,
3944 -- so we can use the normal expansion routine for membership,
3945 -- setting the flag to prevent recursion into this procedure.
3947 Set_No_Minimize_Eliminate (N);
3948 Expand_N_In (N);
3949 end if;
3951 -- Right operand is a subtype name and the subtype has a predicate. We
3952 -- have to make sure the predicate is checked, and for that we need to
3953 -- use the standard N_In circuitry with appropriate types.
3955 else
3956 pragma Assert (Present (Predicate_Function (Etype (Rop))));
3958 -- If types are "right", just call Expand_N_In preventing recursion
3960 if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then
3961 Set_No_Minimize_Eliminate (N);
3962 Expand_N_In (N);
3964 -- Bignum case
3966 elsif Is_RTE (Etype (Lop), RE_Bignum) then
3968 -- For X in T, we want to rewrite our node as
3970 -- do
3971 -- Bnn : Result_Type;
3973 -- declare
3974 -- M : Mark_Id := SS_Mark;
3975 -- Lnn : Long_Long_Integer'Base
3976 -- Nnn : Bignum;
3978 -- begin
3979 -- Nnn := X;
3981 -- if not Bignum_In_LLI_Range (Nnn) then
3982 -- Bnn := False;
3983 -- else
3984 -- Lnn := From_Bignum (Nnn);
3985 -- Bnn :=
3986 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3987 -- and then T'Base (Lnn) in T;
3988 -- end if;
3990 -- SS_Release (M);
3991 -- end
3992 -- in
3993 -- Bnn
3994 -- end
3996 -- A bit gruesome, but there doesn't seem to be a simpler way
3998 declare
3999 Blk : constant Node_Id := Make_Bignum_Block (Loc);
4000 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
4001 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N);
4002 Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N);
4003 T : constant Entity_Id := Etype (Rop);
4004 TB : constant Entity_Id := Base_Type (T);
4005 Nin : Node_Id;
4007 begin
4008 -- Mark the last membership operation to prevent recursion
4010 Nin :=
4011 Make_In (Loc,
4012 Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)),
4013 Right_Opnd => New_Occurrence_Of (T, Loc));
4014 Set_No_Minimize_Eliminate (Nin);
4016 -- Now decorate the block
4018 Insert_After
4019 (Last (Declarations (Blk)),
4020 Make_Object_Declaration (Loc,
4021 Defining_Identifier => Lnn,
4022 Object_Definition => New_Occurrence_Of (LLIB, Loc)));
4024 Insert_After
4025 (Last (Declarations (Blk)),
4026 Make_Object_Declaration (Loc,
4027 Defining_Identifier => Nnn,
4028 Object_Definition =>
4029 New_Occurrence_Of (RTE (RE_Bignum), Loc)));
4031 Insert_List_Before
4032 (First (Statements (Handled_Statement_Sequence (Blk))),
4033 New_List (
4034 Make_Assignment_Statement (Loc,
4035 Name => New_Occurrence_Of (Nnn, Loc),
4036 Expression => Relocate_Node (Lop)),
4038 Make_Implicit_If_Statement (N,
4039 Condition =>
4040 Make_Op_Not (Loc,
4041 Right_Opnd =>
4042 Make_Function_Call (Loc,
4043 Name =>
4044 New_Occurrence_Of
4045 (RTE (RE_Bignum_In_LLI_Range), Loc),
4046 Parameter_Associations => New_List (
4047 New_Occurrence_Of (Nnn, Loc)))),
4049 Then_Statements => New_List (
4050 Make_Assignment_Statement (Loc,
4051 Name => New_Occurrence_Of (Bnn, Loc),
4052 Expression =>
4053 New_Occurrence_Of (Standard_False, Loc))),
4055 Else_Statements => New_List (
4056 Make_Assignment_Statement (Loc,
4057 Name => New_Occurrence_Of (Lnn, Loc),
4058 Expression =>
4059 Make_Function_Call (Loc,
4060 Name =>
4061 New_Occurrence_Of (RTE (RE_From_Bignum), Loc),
4062 Parameter_Associations => New_List (
4063 New_Occurrence_Of (Nnn, Loc)))),
4065 Make_Assignment_Statement (Loc,
4066 Name => New_Occurrence_Of (Bnn, Loc),
4067 Expression =>
4068 Make_And_Then (Loc,
4069 Left_Opnd =>
4070 Make_In (Loc,
4071 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4072 Right_Opnd =>
4073 Make_Range (Loc,
4074 Low_Bound =>
4075 Convert_To (LLIB,
4076 Make_Attribute_Reference (Loc,
4077 Attribute_Name => Name_First,
4078 Prefix =>
4079 New_Occurrence_Of (TB, Loc))),
4081 High_Bound =>
4082 Convert_To (LLIB,
4083 Make_Attribute_Reference (Loc,
4084 Attribute_Name => Name_Last,
4085 Prefix =>
4086 New_Occurrence_Of (TB, Loc))))),
4088 Right_Opnd => Nin))))));
4090 -- Now we can do the rewrite
4092 Rewrite (N,
4093 Make_Expression_With_Actions (Loc,
4094 Actions => New_List (
4095 Make_Object_Declaration (Loc,
4096 Defining_Identifier => Bnn,
4097 Object_Definition =>
4098 New_Occurrence_Of (Result_Type, Loc)),
4099 Blk),
4100 Expression => New_Occurrence_Of (Bnn, Loc)));
4101 Analyze_And_Resolve (N, Result_Type);
4102 return;
4103 end;
4105 -- Not bignum case, but types don't match (this means we rewrote the
4106 -- left operand to be Long_Long_Integer).
4108 else
4109 pragma Assert (Base_Type (Etype (Lop)) = LLIB);
4111 -- We rewrite the membership test as (where T is the type with
4112 -- the predicate, i.e. the type of the right operand)
4114 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
4115 -- and then T'Base (Lop) in T
4117 declare
4118 T : constant Entity_Id := Etype (Rop);
4119 TB : constant Entity_Id := Base_Type (T);
4120 Nin : Node_Id;
4122 begin
4123 -- The last membership test is marked to prevent recursion
4125 Nin :=
4126 Make_In (Loc,
4127 Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)),
4128 Right_Opnd => New_Occurrence_Of (T, Loc));
4129 Set_No_Minimize_Eliminate (Nin);
4131 -- Now do the rewrite
4133 Rewrite (N,
4134 Make_And_Then (Loc,
4135 Left_Opnd =>
4136 Make_In (Loc,
4137 Left_Opnd => Lop,
4138 Right_Opnd =>
4139 Make_Range (Loc,
4140 Low_Bound =>
4141 Convert_To (LLIB,
4142 Make_Attribute_Reference (Loc,
4143 Attribute_Name => Name_First,
4144 Prefix =>
4145 New_Occurrence_Of (TB, Loc))),
4146 High_Bound =>
4147 Convert_To (LLIB,
4148 Make_Attribute_Reference (Loc,
4149 Attribute_Name => Name_Last,
4150 Prefix =>
4151 New_Occurrence_Of (TB, Loc))))),
4152 Right_Opnd => Nin));
4153 Set_Analyzed (N, False);
4154 Analyze_And_Resolve (N, Restype);
4155 end;
4156 end if;
4157 end if;
4158 end Expand_Membership_Minimize_Eliminate_Overflow;
4160 ------------------------
4161 -- Expand_N_Allocator --
4162 ------------------------
4164 procedure Expand_N_Allocator (N : Node_Id) is
4165 Etyp : constant Entity_Id := Etype (Expression (N));
4166 Loc : constant Source_Ptr := Sloc (N);
4167 PtrT : constant Entity_Id := Etype (N);
4169 procedure Rewrite_Coextension (N : Node_Id);
4170 -- Static coextensions have the same lifetime as the entity they
4171 -- constrain. Such occurrences can be rewritten as aliased objects
4172 -- and their unrestricted access used instead of the coextension.
4174 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
4175 -- Given a constrained array type E, returns a node representing the
4176 -- code to compute the size in storage elements for the given type.
4177 -- This is done without using the attribute (which malfunctions for
4178 -- large sizes ???)
4180 -------------------------
4181 -- Rewrite_Coextension --
4182 -------------------------
4184 procedure Rewrite_Coextension (N : Node_Id) is
4185 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
4186 Temp_Decl : Node_Id;
4188 begin
4189 -- Generate:
4190 -- Cnn : aliased Etyp;
4192 Temp_Decl :=
4193 Make_Object_Declaration (Loc,
4194 Defining_Identifier => Temp_Id,
4195 Aliased_Present => True,
4196 Object_Definition => New_Occurrence_Of (Etyp, Loc));
4198 if Nkind (Expression (N)) = N_Qualified_Expression then
4199 Set_Expression (Temp_Decl, Expression (Expression (N)));
4200 end if;
4202 Insert_Action (N, Temp_Decl);
4203 Rewrite (N,
4204 Make_Attribute_Reference (Loc,
4205 Prefix => New_Occurrence_Of (Temp_Id, Loc),
4206 Attribute_Name => Name_Unrestricted_Access));
4208 Analyze_And_Resolve (N, PtrT);
4209 end Rewrite_Coextension;
4211 ------------------------------
4212 -- Size_In_Storage_Elements --
4213 ------------------------------
4215 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
4216 begin
4217 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4218 -- However, the reason for the existence of this function is
4219 -- to construct a test for sizes too large, which means near the
4220 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4221 -- is that we get overflows when sizes are greater than 2**31.
4223 -- So what we end up doing for array types is to use the expression:
4225 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4227 -- which avoids this problem. All this is a bit bogus, but it does
4228 -- mean we catch common cases of trying to allocate arrays that
4229 -- are too large, and which in the absence of a check results in
4230 -- undetected chaos ???
4232 -- Note in particular that this is a pessimistic estimate in the
4233 -- case of packed array types, where an array element might occupy
4234 -- just a fraction of a storage element???
4236 declare
4237 Len : Node_Id;
4238 Res : Node_Id;
4240 begin
4241 for J in 1 .. Number_Dimensions (E) loop
4242 Len :=
4243 Make_Attribute_Reference (Loc,
4244 Prefix => New_Occurrence_Of (E, Loc),
4245 Attribute_Name => Name_Length,
4246 Expressions => New_List (Make_Integer_Literal (Loc, J)));
4248 if J = 1 then
4249 Res := Len;
4251 else
4252 Res :=
4253 Make_Op_Multiply (Loc,
4254 Left_Opnd => Res,
4255 Right_Opnd => Len);
4256 end if;
4257 end loop;
4259 return
4260 Make_Op_Multiply (Loc,
4261 Left_Opnd => Len,
4262 Right_Opnd =>
4263 Make_Attribute_Reference (Loc,
4264 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
4265 Attribute_Name => Name_Max_Size_In_Storage_Elements));
4266 end;
4267 end Size_In_Storage_Elements;
4269 -- Local variables
4271 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
4272 Desig : Entity_Id;
4273 Nod : Node_Id;
4274 Pool : Entity_Id;
4275 Rel_Typ : Entity_Id;
4276 Temp : Entity_Id;
4278 -- Start of processing for Expand_N_Allocator
4280 begin
4281 -- RM E.2.3(22). We enforce that the expected type of an allocator
4282 -- shall not be a remote access-to-class-wide-limited-private type
4284 -- Why is this being done at expansion time, seems clearly wrong ???
4286 Validate_Remote_Access_To_Class_Wide_Type (N);
4288 -- Processing for anonymous access-to-controlled types. These access
4289 -- types receive a special finalization master which appears in the
4290 -- declarations of the enclosing semantic unit. This expansion is done
4291 -- now to ensure that any additional types generated by this routine or
4292 -- Expand_Allocator_Expression inherit the proper type attributes.
4294 if (Ekind (PtrT) = E_Anonymous_Access_Type
4295 or else (Is_Itype (PtrT) and then No (Finalization_Master (PtrT))))
4296 and then Needs_Finalization (Dtyp)
4297 then
4298 -- Detect the allocation of an anonymous controlled object where the
4299 -- type of the context is named. For example:
4301 -- procedure Proc (Ptr : Named_Access_Typ);
4302 -- Proc (new Designated_Typ);
4304 -- Regardless of the anonymous-to-named access type conversion, the
4305 -- lifetime of the object must be associated with the named access
4306 -- type. Use the finalization-related attributes of this type.
4308 if Nkind_In (Parent (N), N_Type_Conversion,
4309 N_Unchecked_Type_Conversion)
4310 and then Ekind_In (Etype (Parent (N)), E_Access_Subtype,
4311 E_Access_Type,
4312 E_General_Access_Type)
4313 then
4314 Rel_Typ := Etype (Parent (N));
4315 else
4316 Rel_Typ := Empty;
4317 end if;
4319 -- Anonymous access-to-controlled types allocate on the global pool.
4320 -- Do not set this attribute on .NET/JVM since those targets do not
4321 -- support pools.
4323 if No (Associated_Storage_Pool (PtrT)) and then VM_Target = No_VM then
4324 if Present (Rel_Typ) then
4325 Set_Associated_Storage_Pool
4326 (PtrT, Associated_Storage_Pool (Rel_Typ));
4327 else
4328 Set_Associated_Storage_Pool
4329 (PtrT, RTE (RE_Global_Pool_Object));
4330 end if;
4331 end if;
4333 -- The finalization master must be inserted and analyzed as part of
4334 -- the current semantic unit. Note that the master is updated when
4335 -- analysis changes current units.
4337 if Present (Rel_Typ) then
4338 Set_Finalization_Master (PtrT, Finalization_Master (Rel_Typ));
4339 else
4340 Set_Finalization_Master (PtrT, Current_Anonymous_Master);
4341 end if;
4342 end if;
4344 -- Set the storage pool and find the appropriate version of Allocate to
4345 -- call. Do not overwrite the storage pool if it is already set, which
4346 -- can happen for build-in-place function returns (see
4347 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4349 if No (Storage_Pool (N)) then
4350 Pool := Associated_Storage_Pool (Root_Type (PtrT));
4352 if Present (Pool) then
4353 Set_Storage_Pool (N, Pool);
4355 if Is_RTE (Pool, RE_SS_Pool) then
4356 if VM_Target = No_VM then
4357 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
4358 end if;
4360 -- In the case of an allocator for a simple storage pool, locate
4361 -- and save a reference to the pool type's Allocate routine.
4363 elsif Present (Get_Rep_Pragma
4364 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4365 then
4366 declare
4367 Pool_Type : constant Entity_Id := Base_Type (Etype (Pool));
4368 Alloc_Op : Entity_Id;
4369 begin
4370 Alloc_Op := Get_Name_Entity_Id (Name_Allocate);
4371 while Present (Alloc_Op) loop
4372 if Scope (Alloc_Op) = Scope (Pool_Type)
4373 and then Present (First_Formal (Alloc_Op))
4374 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
4375 then
4376 Set_Procedure_To_Call (N, Alloc_Op);
4377 exit;
4378 else
4379 Alloc_Op := Homonym (Alloc_Op);
4380 end if;
4381 end loop;
4382 end;
4384 elsif Is_Class_Wide_Type (Etype (Pool)) then
4385 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
4387 else
4388 Set_Procedure_To_Call (N,
4389 Find_Prim_Op (Etype (Pool), Name_Allocate));
4390 end if;
4391 end if;
4392 end if;
4394 -- Under certain circumstances we can replace an allocator by an access
4395 -- to statically allocated storage. The conditions, as noted in AARM
4396 -- 3.10 (10c) are as follows:
4398 -- Size and initial value is known at compile time
4399 -- Access type is access-to-constant
4401 -- The allocator is not part of a constraint on a record component,
4402 -- because in that case the inserted actions are delayed until the
4403 -- record declaration is fully analyzed, which is too late for the
4404 -- analysis of the rewritten allocator.
4406 if Is_Access_Constant (PtrT)
4407 and then Nkind (Expression (N)) = N_Qualified_Expression
4408 and then Compile_Time_Known_Value (Expression (Expression (N)))
4409 and then Size_Known_At_Compile_Time
4410 (Etype (Expression (Expression (N))))
4411 and then not Is_Record_Type (Current_Scope)
4412 then
4413 -- Here we can do the optimization. For the allocator
4415 -- new x'(y)
4417 -- We insert an object declaration
4419 -- Tnn : aliased x := y;
4421 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4422 -- marked as requiring static allocation.
4424 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
4425 Desig := Subtype_Mark (Expression (N));
4427 -- If context is constrained, use constrained subtype directly,
4428 -- so that the constant is not labelled as having a nominally
4429 -- unconstrained subtype.
4431 if Entity (Desig) = Base_Type (Dtyp) then
4432 Desig := New_Occurrence_Of (Dtyp, Loc);
4433 end if;
4435 Insert_Action (N,
4436 Make_Object_Declaration (Loc,
4437 Defining_Identifier => Temp,
4438 Aliased_Present => True,
4439 Constant_Present => Is_Access_Constant (PtrT),
4440 Object_Definition => Desig,
4441 Expression => Expression (Expression (N))));
4443 Rewrite (N,
4444 Make_Attribute_Reference (Loc,
4445 Prefix => New_Occurrence_Of (Temp, Loc),
4446 Attribute_Name => Name_Unrestricted_Access));
4448 Analyze_And_Resolve (N, PtrT);
4450 -- We set the variable as statically allocated, since we don't want
4451 -- it going on the stack of the current procedure.
4453 Set_Is_Statically_Allocated (Temp);
4454 return;
4455 end if;
4457 -- Same if the allocator is an access discriminant for a local object:
4458 -- instead of an allocator we create a local value and constrain the
4459 -- enclosing object with the corresponding access attribute.
4461 if Is_Static_Coextension (N) then
4462 Rewrite_Coextension (N);
4463 return;
4464 end if;
4466 -- Check for size too large, we do this because the back end misses
4467 -- proper checks here and can generate rubbish allocation calls when
4468 -- we are near the limit. We only do this for the 32-bit address case
4469 -- since that is from a practical point of view where we see a problem.
4471 if System_Address_Size = 32
4472 and then not Storage_Checks_Suppressed (PtrT)
4473 and then not Storage_Checks_Suppressed (Dtyp)
4474 and then not Storage_Checks_Suppressed (Etyp)
4475 then
4476 -- The check we want to generate should look like
4478 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4479 -- raise Storage_Error;
4480 -- end if;
4482 -- where 3.5 gigabytes is a constant large enough to accommodate any
4483 -- reasonable request for. But we can't do it this way because at
4484 -- least at the moment we don't compute this attribute right, and
4485 -- can silently give wrong results when the result gets large. Since
4486 -- this is all about large results, that's bad, so instead we only
4487 -- apply the check for constrained arrays, and manually compute the
4488 -- value of the attribute ???
4490 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
4491 Insert_Action (N,
4492 Make_Raise_Storage_Error (Loc,
4493 Condition =>
4494 Make_Op_Gt (Loc,
4495 Left_Opnd => Size_In_Storage_Elements (Etyp),
4496 Right_Opnd =>
4497 Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))),
4498 Reason => SE_Object_Too_Large));
4499 end if;
4500 end if;
4502 -- If no storage pool has been specified and we have the restriction
4503 -- No_Standard_Allocators_After_Elaboration is present, then generate
4504 -- a call to Elaboration_Allocators.Check_Standard_Allocator.
4506 if Nkind (N) = N_Allocator
4507 and then No (Storage_Pool (N))
4508 and then Restriction_Active (No_Standard_Allocators_After_Elaboration)
4509 then
4510 Insert_Action (N,
4511 Make_Procedure_Call_Statement (Loc,
4512 Name =>
4513 New_Occurrence_Of (RTE (RE_Check_Standard_Allocator), Loc)));
4514 end if;
4516 -- Handle case of qualified expression (other than optimization above)
4517 -- First apply constraint checks, because the bounds or discriminants
4518 -- in the aggregate might not match the subtype mark in the allocator.
4520 if Nkind (Expression (N)) = N_Qualified_Expression then
4521 Apply_Constraint_Check
4522 (Expression (Expression (N)), Etype (Expression (N)));
4524 Expand_Allocator_Expression (N);
4525 return;
4526 end if;
4528 -- If the allocator is for a type which requires initialization, and
4529 -- there is no initial value (i.e. operand is a subtype indication
4530 -- rather than a qualified expression), then we must generate a call to
4531 -- the initialization routine using an expressions action node:
4533 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4535 -- Here ptr_T is the pointer type for the allocator, and T is the
4536 -- subtype of the allocator. A special case arises if the designated
4537 -- type of the access type is a task or contains tasks. In this case
4538 -- the call to Init (Temp.all ...) is replaced by code that ensures
4539 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4540 -- for details). In addition, if the type T is a task type, then the
4541 -- first argument to Init must be converted to the task record type.
4543 declare
4544 T : constant Entity_Id := Entity (Expression (N));
4545 Args : List_Id;
4546 Decls : List_Id;
4547 Decl : Node_Id;
4548 Discr : Elmt_Id;
4549 Init : Entity_Id;
4550 Init_Arg1 : Node_Id;
4551 Temp_Decl : Node_Id;
4552 Temp_Type : Entity_Id;
4554 begin
4555 if No_Initialization (N) then
4557 -- Even though this might be a simple allocation, create a custom
4558 -- Allocate if the context requires it. Since .NET/JVM compilers
4559 -- do not support pools, this step is skipped.
4561 if VM_Target = No_VM
4562 and then Present (Finalization_Master (PtrT))
4563 then
4564 Build_Allocate_Deallocate_Proc
4565 (N => N,
4566 Is_Allocate => True);
4567 end if;
4569 -- Case of no initialization procedure present
4571 elsif not Has_Non_Null_Base_Init_Proc (T) then
4573 -- Case of simple initialization required
4575 if Needs_Simple_Initialization (T) then
4576 Check_Restriction (No_Default_Initialization, N);
4577 Rewrite (Expression (N),
4578 Make_Qualified_Expression (Loc,
4579 Subtype_Mark => New_Occurrence_Of (T, Loc),
4580 Expression => Get_Simple_Init_Val (T, N)));
4582 Analyze_And_Resolve (Expression (Expression (N)), T);
4583 Analyze_And_Resolve (Expression (N), T);
4584 Set_Paren_Count (Expression (Expression (N)), 1);
4585 Expand_N_Allocator (N);
4587 -- No initialization required
4589 else
4590 null;
4591 end if;
4593 -- Case of initialization procedure present, must be called
4595 else
4596 Check_Restriction (No_Default_Initialization, N);
4598 if not Restriction_Active (No_Default_Initialization) then
4599 Init := Base_Init_Proc (T);
4600 Nod := N;
4601 Temp := Make_Temporary (Loc, 'P');
4603 -- Construct argument list for the initialization routine call
4605 Init_Arg1 :=
4606 Make_Explicit_Dereference (Loc,
4607 Prefix =>
4608 New_Occurrence_Of (Temp, Loc));
4610 Set_Assignment_OK (Init_Arg1);
4611 Temp_Type := PtrT;
4613 -- The initialization procedure expects a specific type. if the
4614 -- context is access to class wide, indicate that the object
4615 -- being allocated has the right specific type.
4617 if Is_Class_Wide_Type (Dtyp) then
4618 Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1);
4619 end if;
4621 -- If designated type is a concurrent type or if it is private
4622 -- type whose definition is a concurrent type, the first
4623 -- argument in the Init routine has to be unchecked conversion
4624 -- to the corresponding record type. If the designated type is
4625 -- a derived type, also convert the argument to its root type.
4627 if Is_Concurrent_Type (T) then
4628 Init_Arg1 :=
4629 Unchecked_Convert_To (
4630 Corresponding_Record_Type (T), Init_Arg1);
4632 elsif Is_Private_Type (T)
4633 and then Present (Full_View (T))
4634 and then Is_Concurrent_Type (Full_View (T))
4635 then
4636 Init_Arg1 :=
4637 Unchecked_Convert_To
4638 (Corresponding_Record_Type (Full_View (T)), Init_Arg1);
4640 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
4641 declare
4642 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
4644 begin
4645 Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1);
4646 Set_Etype (Init_Arg1, Ftyp);
4647 end;
4648 end if;
4650 Args := New_List (Init_Arg1);
4652 -- For the task case, pass the Master_Id of the access type as
4653 -- the value of the _Master parameter, and _Chain as the value
4654 -- of the _Chain parameter (_Chain will be defined as part of
4655 -- the generated code for the allocator).
4657 -- In Ada 2005, the context may be a function that returns an
4658 -- anonymous access type. In that case the Master_Id has been
4659 -- created when expanding the function declaration.
4661 if Has_Task (T) then
4662 if No (Master_Id (Base_Type (PtrT))) then
4664 -- The designated type was an incomplete type, and the
4665 -- access type did not get expanded. Salvage it now.
4667 if not Restriction_Active (No_Task_Hierarchy) then
4668 if Present (Parent (Base_Type (PtrT))) then
4669 Expand_N_Full_Type_Declaration
4670 (Parent (Base_Type (PtrT)));
4672 -- The only other possibility is an itype. For this
4673 -- case, the master must exist in the context. This is
4674 -- the case when the allocator initializes an access
4675 -- component in an init-proc.
4677 else
4678 pragma Assert (Is_Itype (PtrT));
4679 Build_Master_Renaming (PtrT, N);
4680 end if;
4681 end if;
4682 end if;
4684 -- If the context of the allocator is a declaration or an
4685 -- assignment, we can generate a meaningful image for it,
4686 -- even though subsequent assignments might remove the
4687 -- connection between task and entity. We build this image
4688 -- when the left-hand side is a simple variable, a simple
4689 -- indexed assignment or a simple selected component.
4691 if Nkind (Parent (N)) = N_Assignment_Statement then
4692 declare
4693 Nam : constant Node_Id := Name (Parent (N));
4695 begin
4696 if Is_Entity_Name (Nam) then
4697 Decls :=
4698 Build_Task_Image_Decls
4699 (Loc,
4700 New_Occurrence_Of
4701 (Entity (Nam), Sloc (Nam)), T);
4703 elsif Nkind_In (Nam, N_Indexed_Component,
4704 N_Selected_Component)
4705 and then Is_Entity_Name (Prefix (Nam))
4706 then
4707 Decls :=
4708 Build_Task_Image_Decls
4709 (Loc, Nam, Etype (Prefix (Nam)));
4710 else
4711 Decls := Build_Task_Image_Decls (Loc, T, T);
4712 end if;
4713 end;
4715 elsif Nkind (Parent (N)) = N_Object_Declaration then
4716 Decls :=
4717 Build_Task_Image_Decls
4718 (Loc, Defining_Identifier (Parent (N)), T);
4720 else
4721 Decls := Build_Task_Image_Decls (Loc, T, T);
4722 end if;
4724 if Restriction_Active (No_Task_Hierarchy) then
4725 Append_To (Args,
4726 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
4727 else
4728 Append_To (Args,
4729 New_Occurrence_Of
4730 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
4731 end if;
4733 Append_To (Args, Make_Identifier (Loc, Name_uChain));
4735 Decl := Last (Decls);
4736 Append_To (Args,
4737 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
4739 -- Has_Task is false, Decls not used
4741 else
4742 Decls := No_List;
4743 end if;
4745 -- Add discriminants if discriminated type
4747 declare
4748 Dis : Boolean := False;
4749 Typ : Entity_Id;
4751 begin
4752 if Has_Discriminants (T) then
4753 Dis := True;
4754 Typ := T;
4756 elsif Is_Private_Type (T)
4757 and then Present (Full_View (T))
4758 and then Has_Discriminants (Full_View (T))
4759 then
4760 Dis := True;
4761 Typ := Full_View (T);
4762 end if;
4764 if Dis then
4766 -- If the allocated object will be constrained by the
4767 -- default values for discriminants, then build a subtype
4768 -- with those defaults, and change the allocated subtype
4769 -- to that. Note that this happens in fewer cases in Ada
4770 -- 2005 (AI-363).
4772 if not Is_Constrained (Typ)
4773 and then Present (Discriminant_Default_Value
4774 (First_Discriminant (Typ)))
4775 and then (Ada_Version < Ada_2005
4776 or else not
4777 Object_Type_Has_Constrained_Partial_View
4778 (Typ, Current_Scope))
4779 then
4780 Typ := Build_Default_Subtype (Typ, N);
4781 Set_Expression (N, New_Occurrence_Of (Typ, Loc));
4782 end if;
4784 Discr := First_Elmt (Discriminant_Constraint (Typ));
4785 while Present (Discr) loop
4786 Nod := Node (Discr);
4787 Append (New_Copy_Tree (Node (Discr)), Args);
4789 -- AI-416: when the discriminant constraint is an
4790 -- anonymous access type make sure an accessibility
4791 -- check is inserted if necessary (3.10.2(22.q/2))
4793 if Ada_Version >= Ada_2005
4794 and then
4795 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
4796 then
4797 Apply_Accessibility_Check
4798 (Nod, Typ, Insert_Node => Nod);
4799 end if;
4801 Next_Elmt (Discr);
4802 end loop;
4803 end if;
4804 end;
4806 -- We set the allocator as analyzed so that when we analyze
4807 -- the if expression node, we do not get an unwanted recursive
4808 -- expansion of the allocator expression.
4810 Set_Analyzed (N, True);
4811 Nod := Relocate_Node (N);
4813 -- Here is the transformation:
4814 -- input: new Ctrl_Typ
4815 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4816 -- Ctrl_TypIP (Temp.all, ...);
4817 -- [Deep_]Initialize (Temp.all);
4819 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4820 -- is the subtype of the allocator.
4822 Temp_Decl :=
4823 Make_Object_Declaration (Loc,
4824 Defining_Identifier => Temp,
4825 Constant_Present => True,
4826 Object_Definition => New_Occurrence_Of (Temp_Type, Loc),
4827 Expression => Nod);
4829 Set_Assignment_OK (Temp_Decl);
4830 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
4832 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
4834 -- If the designated type is a task type or contains tasks,
4835 -- create block to activate created tasks, and insert
4836 -- declaration for Task_Image variable ahead of call.
4838 if Has_Task (T) then
4839 declare
4840 L : constant List_Id := New_List;
4841 Blk : Node_Id;
4842 begin
4843 Build_Task_Allocate_Block (L, Nod, Args);
4844 Blk := Last (L);
4845 Insert_List_Before (First (Declarations (Blk)), Decls);
4846 Insert_Actions (N, L);
4847 end;
4849 else
4850 Insert_Action (N,
4851 Make_Procedure_Call_Statement (Loc,
4852 Name => New_Occurrence_Of (Init, Loc),
4853 Parameter_Associations => Args));
4854 end if;
4856 if Needs_Finalization (T) then
4858 -- Generate:
4859 -- [Deep_]Initialize (Init_Arg1);
4861 Insert_Action (N,
4862 Make_Init_Call
4863 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4864 Typ => T));
4866 if Present (Finalization_Master (PtrT)) then
4868 -- Special processing for .NET/JVM, the allocated object
4869 -- is attached to the finalization master. Generate:
4871 -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1));
4873 -- Types derived from [Limited_]Controlled are the only
4874 -- ones considered since they have fields Prev and Next.
4876 if VM_Target /= No_VM then
4877 if Is_Controlled (T) then
4878 Insert_Action (N,
4879 Make_Attach_Call
4880 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4881 Ptr_Typ => PtrT));
4882 end if;
4884 -- Default case, generate:
4886 -- Set_Finalize_Address
4887 -- (<PtrT>FM, <T>FD'Unrestricted_Access);
4889 -- Do not generate this call in CodePeer mode, as TSS
4890 -- primitive Finalize_Address is not created in this
4891 -- mode.
4893 elsif not CodePeer_Mode then
4894 Insert_Action (N,
4895 Make_Set_Finalize_Address_Call
4896 (Loc => Loc,
4897 Typ => T,
4898 Ptr_Typ => PtrT));
4899 end if;
4900 end if;
4901 end if;
4903 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4904 Analyze_And_Resolve (N, PtrT);
4905 end if;
4906 end if;
4907 end;
4909 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
4910 -- object that has been rewritten as a reference, we displace "this"
4911 -- to reference properly its secondary dispatch table.
4913 if Nkind (N) = N_Identifier and then Is_Interface (Dtyp) then
4914 Displace_Allocator_Pointer (N);
4915 end if;
4917 exception
4918 when RE_Not_Available =>
4919 return;
4920 end Expand_N_Allocator;
4922 -----------------------
4923 -- Expand_N_And_Then --
4924 -----------------------
4926 procedure Expand_N_And_Then (N : Node_Id)
4927 renames Expand_Short_Circuit_Operator;
4929 ------------------------------
4930 -- Expand_N_Case_Expression --
4931 ------------------------------
4933 procedure Expand_N_Case_Expression (N : Node_Id) is
4934 Loc : constant Source_Ptr := Sloc (N);
4935 Typ : constant Entity_Id := Etype (N);
4936 Cstmt : Node_Id;
4937 Decl : Node_Id;
4938 Tnn : Entity_Id;
4939 Pnn : Entity_Id;
4940 Actions : List_Id;
4941 Ttyp : Entity_Id;
4942 Alt : Node_Id;
4943 Fexp : Node_Id;
4945 begin
4946 -- Check for MINIMIZED/ELIMINATED overflow mode
4948 if Minimized_Eliminated_Overflow_Check (N) then
4949 Apply_Arithmetic_Overflow_Check (N);
4950 return;
4951 end if;
4953 -- If the case expression is a predicate specification, do not
4954 -- expand, because it will be converted to the proper predicate
4955 -- form when building the predicate function.
4957 if Ekind_In (Current_Scope, E_Function, E_Procedure)
4958 and then Is_Predicate_Function (Current_Scope)
4959 then
4960 return;
4961 end if;
4963 -- We expand
4965 -- case X is when A => AX, when B => BX ...
4967 -- to
4969 -- do
4970 -- Tnn : typ;
4971 -- case X is
4972 -- when A =>
4973 -- Tnn := AX;
4974 -- when B =>
4975 -- Tnn := BX;
4976 -- ...
4977 -- end case;
4978 -- in Tnn end;
4980 -- However, this expansion is wrong for limited types, and also
4981 -- wrong for unconstrained types (since the bounds may not be the
4982 -- same in all branches). Furthermore it involves an extra copy
4983 -- for large objects. So we take care of this by using the following
4984 -- modified expansion for non-elementary types:
4986 -- do
4987 -- type Pnn is access all typ;
4988 -- Tnn : Pnn;
4989 -- case X is
4990 -- when A =>
4991 -- T := AX'Unrestricted_Access;
4992 -- when B =>
4993 -- T := BX'Unrestricted_Access;
4994 -- ...
4995 -- end case;
4996 -- in Tnn.all end;
4998 Cstmt :=
4999 Make_Case_Statement (Loc,
5000 Expression => Expression (N),
5001 Alternatives => New_List);
5003 -- Preserve the original context for which the case statement is being
5004 -- generated. This is needed by the finalization machinery to prevent
5005 -- the premature finalization of controlled objects found within the
5006 -- case statement.
5008 Set_From_Conditional_Expression (Cstmt);
5010 Actions := New_List;
5012 -- Scalar case
5014 if Is_Elementary_Type (Typ) then
5015 Ttyp := Typ;
5017 else
5018 Pnn := Make_Temporary (Loc, 'P');
5019 Append_To (Actions,
5020 Make_Full_Type_Declaration (Loc,
5021 Defining_Identifier => Pnn,
5022 Type_Definition =>
5023 Make_Access_To_Object_Definition (Loc,
5024 All_Present => True,
5025 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
5026 Ttyp := Pnn;
5027 end if;
5029 Tnn := Make_Temporary (Loc, 'T');
5031 -- Create declaration for target of expression, and indicate that it
5032 -- does not require initialization.
5034 Decl :=
5035 Make_Object_Declaration (Loc,
5036 Defining_Identifier => Tnn,
5037 Object_Definition => New_Occurrence_Of (Ttyp, Loc));
5038 Set_No_Initialization (Decl);
5039 Append_To (Actions, Decl);
5041 -- Now process the alternatives
5043 Alt := First (Alternatives (N));
5044 while Present (Alt) loop
5045 declare
5046 Aexp : Node_Id := Expression (Alt);
5047 Aloc : constant Source_Ptr := Sloc (Aexp);
5048 Stats : List_Id;
5050 begin
5051 -- As described above, take Unrestricted_Access for case of non-
5052 -- scalar types, to avoid big copies, and special cases.
5054 if not Is_Elementary_Type (Typ) then
5055 Aexp :=
5056 Make_Attribute_Reference (Aloc,
5057 Prefix => Relocate_Node (Aexp),
5058 Attribute_Name => Name_Unrestricted_Access);
5059 end if;
5061 Stats := New_List (
5062 Make_Assignment_Statement (Aloc,
5063 Name => New_Occurrence_Of (Tnn, Loc),
5064 Expression => Aexp));
5066 -- Propagate declarations inserted in the node by Insert_Actions
5067 -- (for example, temporaries generated to remove side effects).
5068 -- These actions must remain attached to the alternative, given
5069 -- that they are generated by the corresponding expression.
5071 if Present (Sinfo.Actions (Alt)) then
5072 Prepend_List (Sinfo.Actions (Alt), Stats);
5073 end if;
5075 Append_To
5076 (Alternatives (Cstmt),
5077 Make_Case_Statement_Alternative (Sloc (Alt),
5078 Discrete_Choices => Discrete_Choices (Alt),
5079 Statements => Stats));
5080 end;
5082 Next (Alt);
5083 end loop;
5085 Append_To (Actions, Cstmt);
5087 -- Construct and return final expression with actions
5089 if Is_Elementary_Type (Typ) then
5090 Fexp := New_Occurrence_Of (Tnn, Loc);
5091 else
5092 Fexp :=
5093 Make_Explicit_Dereference (Loc,
5094 Prefix => New_Occurrence_Of (Tnn, Loc));
5095 end if;
5097 Rewrite (N,
5098 Make_Expression_With_Actions (Loc,
5099 Expression => Fexp,
5100 Actions => Actions));
5102 Analyze_And_Resolve (N, Typ);
5103 end Expand_N_Case_Expression;
5105 -----------------------------------
5106 -- Expand_N_Explicit_Dereference --
5107 -----------------------------------
5109 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
5110 begin
5111 -- Insert explicit dereference call for the checked storage pool case
5113 Insert_Dereference_Action (Prefix (N));
5115 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5116 -- we set the atomic sync flag.
5118 if Is_Atomic (Etype (N))
5119 and then not Atomic_Synchronization_Disabled (Etype (N))
5120 then
5121 Activate_Atomic_Synchronization (N);
5122 end if;
5123 end Expand_N_Explicit_Dereference;
5125 --------------------------------------
5126 -- Expand_N_Expression_With_Actions --
5127 --------------------------------------
5129 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
5131 function Process_Action (Act : Node_Id) return Traverse_Result;
5132 -- Inspect and process a single action of an expression_with_actions for
5133 -- transient controlled objects. If such objects are found, the routine
5134 -- generates code to clean them up when the context of the expression is
5135 -- evaluated or elaborated.
5137 --------------------
5138 -- Process_Action --
5139 --------------------
5141 function Process_Action (Act : Node_Id) return Traverse_Result is
5142 begin
5143 if Nkind (Act) = N_Object_Declaration
5144 and then Is_Finalizable_Transient (Act, N)
5145 then
5146 Process_Transient_Object (Act, N);
5147 return Abandon;
5149 -- Avoid processing temporary function results multiple times when
5150 -- dealing with nested expression_with_actions.
5152 elsif Nkind (Act) = N_Expression_With_Actions then
5153 return Abandon;
5155 -- Do not process temporary function results in loops. This is done
5156 -- by Expand_N_Loop_Statement and Build_Finalizer.
5158 elsif Nkind (Act) = N_Loop_Statement then
5159 return Abandon;
5160 end if;
5162 return OK;
5163 end Process_Action;
5165 procedure Process_Single_Action is new Traverse_Proc (Process_Action);
5167 -- Local variables
5169 Act : Node_Id;
5171 -- Start of processing for Expand_N_Expression_With_Actions
5173 begin
5174 -- Process the actions as described above
5176 Act := First (Actions (N));
5177 while Present (Act) loop
5178 Process_Single_Action (Act);
5179 Next (Act);
5180 end loop;
5182 -- Deal with case where there are no actions. In this case we simply
5183 -- rewrite the node with its expression since we don't need the actions
5184 -- and the specification of this node does not allow a null action list.
5186 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5187 -- the expanded tree and relying on being able to retrieve the original
5188 -- tree in cases like this. This raises a whole lot of issues of whether
5189 -- we have problems elsewhere, which will be addressed in the future???
5191 if Is_Empty_List (Actions (N)) then
5192 Rewrite (N, Relocate_Node (Expression (N)));
5193 end if;
5194 end Expand_N_Expression_With_Actions;
5196 ----------------------------
5197 -- Expand_N_If_Expression --
5198 ----------------------------
5200 -- Deal with limited types and condition actions
5202 procedure Expand_N_If_Expression (N : Node_Id) is
5203 procedure Process_Actions (Actions : List_Id);
5204 -- Inspect and process a single action list of an if expression for
5205 -- transient controlled objects. If such objects are found, the routine
5206 -- generates code to clean them up when the context of the expression is
5207 -- evaluated or elaborated.
5209 ---------------------
5210 -- Process_Actions --
5211 ---------------------
5213 procedure Process_Actions (Actions : List_Id) is
5214 Act : Node_Id;
5216 begin
5217 Act := First (Actions);
5218 while Present (Act) loop
5219 if Nkind (Act) = N_Object_Declaration
5220 and then Is_Finalizable_Transient (Act, N)
5221 then
5222 Process_Transient_Object (Act, N);
5223 end if;
5225 Next (Act);
5226 end loop;
5227 end Process_Actions;
5229 -- Local variables
5231 Loc : constant Source_Ptr := Sloc (N);
5232 Cond : constant Node_Id := First (Expressions (N));
5233 Thenx : constant Node_Id := Next (Cond);
5234 Elsex : constant Node_Id := Next (Thenx);
5235 Typ : constant Entity_Id := Etype (N);
5237 Actions : List_Id;
5238 Cnn : Entity_Id;
5239 Decl : Node_Id;
5240 Expr : Node_Id;
5241 New_If : Node_Id;
5242 New_N : Node_Id;
5243 Ptr_Typ : Entity_Id;
5245 -- Start of processing for Expand_N_If_Expression
5247 begin
5248 -- Check for MINIMIZED/ELIMINATED overflow mode
5250 if Minimized_Eliminated_Overflow_Check (N) then
5251 Apply_Arithmetic_Overflow_Check (N);
5252 return;
5253 end if;
5255 -- Fold at compile time if condition known. We have already folded
5256 -- static if expressions, but it is possible to fold any case in which
5257 -- the condition is known at compile time, even though the result is
5258 -- non-static.
5260 -- Note that we don't do the fold of such cases in Sem_Elab because
5261 -- it can cause infinite loops with the expander adding a conditional
5262 -- expression, and Sem_Elab circuitry removing it repeatedly.
5264 if Compile_Time_Known_Value (Cond) then
5265 if Is_True (Expr_Value (Cond)) then
5266 Expr := Thenx;
5267 Actions := Then_Actions (N);
5268 else
5269 Expr := Elsex;
5270 Actions := Else_Actions (N);
5271 end if;
5273 Remove (Expr);
5275 if Present (Actions) then
5276 Rewrite (N,
5277 Make_Expression_With_Actions (Loc,
5278 Expression => Relocate_Node (Expr),
5279 Actions => Actions));
5280 Analyze_And_Resolve (N, Typ);
5281 else
5282 Rewrite (N, Relocate_Node (Expr));
5283 end if;
5285 -- Note that the result is never static (legitimate cases of static
5286 -- if expressions were folded in Sem_Eval).
5288 Set_Is_Static_Expression (N, False);
5289 return;
5290 end if;
5292 -- If the type is limited, and the back end does not handle limited
5293 -- types, then we expand as follows to avoid the possibility of
5294 -- improper copying.
5296 -- type Ptr is access all Typ;
5297 -- Cnn : Ptr;
5298 -- if cond then
5299 -- <<then actions>>
5300 -- Cnn := then-expr'Unrestricted_Access;
5301 -- else
5302 -- <<else actions>>
5303 -- Cnn := else-expr'Unrestricted_Access;
5304 -- end if;
5306 -- and replace the if expression by a reference to Cnn.all.
5308 -- This special case can be skipped if the back end handles limited
5309 -- types properly and ensures that no incorrect copies are made.
5311 if Is_By_Reference_Type (Typ)
5312 and then not Back_End_Handles_Limited_Types
5313 then
5314 -- When the "then" or "else" expressions involve controlled function
5315 -- calls, generated temporaries are chained on the corresponding list
5316 -- of actions. These temporaries need to be finalized after the if
5317 -- expression is evaluated.
5319 Process_Actions (Then_Actions (N));
5320 Process_Actions (Else_Actions (N));
5322 -- Generate:
5323 -- type Ann is access all Typ;
5325 Ptr_Typ := Make_Temporary (Loc, 'A');
5327 Insert_Action (N,
5328 Make_Full_Type_Declaration (Loc,
5329 Defining_Identifier => Ptr_Typ,
5330 Type_Definition =>
5331 Make_Access_To_Object_Definition (Loc,
5332 All_Present => True,
5333 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
5335 -- Generate:
5336 -- Cnn : Ann;
5338 Cnn := Make_Temporary (Loc, 'C', N);
5340 Decl :=
5341 Make_Object_Declaration (Loc,
5342 Defining_Identifier => Cnn,
5343 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc));
5345 -- Generate:
5346 -- if Cond then
5347 -- Cnn := <Thenx>'Unrestricted_Access;
5348 -- else
5349 -- Cnn := <Elsex>'Unrestricted_Access;
5350 -- end if;
5352 New_If :=
5353 Make_Implicit_If_Statement (N,
5354 Condition => Relocate_Node (Cond),
5355 Then_Statements => New_List (
5356 Make_Assignment_Statement (Sloc (Thenx),
5357 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
5358 Expression =>
5359 Make_Attribute_Reference (Loc,
5360 Prefix => Relocate_Node (Thenx),
5361 Attribute_Name => Name_Unrestricted_Access))),
5363 Else_Statements => New_List (
5364 Make_Assignment_Statement (Sloc (Elsex),
5365 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
5366 Expression =>
5367 Make_Attribute_Reference (Loc,
5368 Prefix => Relocate_Node (Elsex),
5369 Attribute_Name => Name_Unrestricted_Access))));
5371 -- Preserve the original context for which the if statement is being
5372 -- generated. This is needed by the finalization machinery to prevent
5373 -- the premature finalization of controlled objects found within the
5374 -- if statement.
5376 Set_From_Conditional_Expression (New_If);
5378 New_N :=
5379 Make_Explicit_Dereference (Loc,
5380 Prefix => New_Occurrence_Of (Cnn, Loc));
5382 -- If the result is an unconstrained array and the if expression is in a
5383 -- context other than the initializing expression of the declaration of
5384 -- an object, then we pull out the if expression as follows:
5386 -- Cnn : constant typ := if-expression
5388 -- and then replace the if expression with an occurrence of Cnn. This
5389 -- avoids the need in the back end to create on-the-fly variable length
5390 -- temporaries (which it cannot do!)
5392 -- Note that the test for being in an object declaration avoids doing an
5393 -- unnecessary expansion, and also avoids infinite recursion.
5395 elsif Is_Array_Type (Typ) and then not Is_Constrained (Typ)
5396 and then (Nkind (Parent (N)) /= N_Object_Declaration
5397 or else Expression (Parent (N)) /= N)
5398 then
5399 declare
5400 Cnn : constant Node_Id := Make_Temporary (Loc, 'C', N);
5401 begin
5402 Insert_Action (N,
5403 Make_Object_Declaration (Loc,
5404 Defining_Identifier => Cnn,
5405 Constant_Present => True,
5406 Object_Definition => New_Occurrence_Of (Typ, Loc),
5407 Expression => Relocate_Node (N),
5408 Has_Init_Expression => True));
5410 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
5411 return;
5412 end;
5414 -- For other types, we only need to expand if there are other actions
5415 -- associated with either branch.
5417 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
5419 -- We now wrap the actions into the appropriate expression
5421 if Present (Then_Actions (N)) then
5422 Rewrite (Thenx,
5423 Make_Expression_With_Actions (Sloc (Thenx),
5424 Actions => Then_Actions (N),
5425 Expression => Relocate_Node (Thenx)));
5427 Set_Then_Actions (N, No_List);
5428 Analyze_And_Resolve (Thenx, Typ);
5429 end if;
5431 if Present (Else_Actions (N)) then
5432 Rewrite (Elsex,
5433 Make_Expression_With_Actions (Sloc (Elsex),
5434 Actions => Else_Actions (N),
5435 Expression => Relocate_Node (Elsex)));
5437 Set_Else_Actions (N, No_List);
5438 Analyze_And_Resolve (Elsex, Typ);
5439 end if;
5441 return;
5443 -- If no actions then no expansion needed, gigi will handle it using the
5444 -- same approach as a C conditional expression.
5446 else
5447 return;
5448 end if;
5450 -- Fall through here for either the limited expansion, or the case of
5451 -- inserting actions for non-limited types. In both these cases, we must
5452 -- move the SLOC of the parent If statement to the newly created one and
5453 -- change it to the SLOC of the expression which, after expansion, will
5454 -- correspond to what is being evaluated.
5456 if Present (Parent (N)) and then Nkind (Parent (N)) = N_If_Statement then
5457 Set_Sloc (New_If, Sloc (Parent (N)));
5458 Set_Sloc (Parent (N), Loc);
5459 end if;
5461 -- Make sure Then_Actions and Else_Actions are appropriately moved
5462 -- to the new if statement.
5464 if Present (Then_Actions (N)) then
5465 Insert_List_Before
5466 (First (Then_Statements (New_If)), Then_Actions (N));
5467 end if;
5469 if Present (Else_Actions (N)) then
5470 Insert_List_Before
5471 (First (Else_Statements (New_If)), Else_Actions (N));
5472 end if;
5474 Insert_Action (N, Decl);
5475 Insert_Action (N, New_If);
5476 Rewrite (N, New_N);
5477 Analyze_And_Resolve (N, Typ);
5478 end Expand_N_If_Expression;
5480 -----------------
5481 -- Expand_N_In --
5482 -----------------
5484 procedure Expand_N_In (N : Node_Id) is
5485 Loc : constant Source_Ptr := Sloc (N);
5486 Restyp : constant Entity_Id := Etype (N);
5487 Lop : constant Node_Id := Left_Opnd (N);
5488 Rop : constant Node_Id := Right_Opnd (N);
5489 Static : constant Boolean := Is_OK_Static_Expression (N);
5491 Ltyp : Entity_Id;
5492 Rtyp : Entity_Id;
5494 procedure Substitute_Valid_Check;
5495 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5496 -- test for the left operand being in range of its subtype.
5498 ----------------------------
5499 -- Substitute_Valid_Check --
5500 ----------------------------
5502 procedure Substitute_Valid_Check is
5503 begin
5504 Rewrite (N,
5505 Make_Attribute_Reference (Loc,
5506 Prefix => Relocate_Node (Lop),
5507 Attribute_Name => Name_Valid));
5509 Analyze_And_Resolve (N, Restyp);
5511 -- Give warning unless overflow checking is MINIMIZED or ELIMINATED,
5512 -- in which case, this usage makes sense, and in any case, we have
5513 -- actually eliminated the danger of optimization above.
5515 if Overflow_Check_Mode not in Minimized_Or_Eliminated then
5516 Error_Msg_N
5517 ("??explicit membership test may be optimized away", N);
5518 Error_Msg_N -- CODEFIX
5519 ("\??use ''Valid attribute instead", N);
5520 end if;
5522 return;
5523 end Substitute_Valid_Check;
5525 -- Start of processing for Expand_N_In
5527 begin
5528 -- If set membership case, expand with separate procedure
5530 if Present (Alternatives (N)) then
5531 Expand_Set_Membership (N);
5532 return;
5533 end if;
5535 -- Not set membership, proceed with expansion
5537 Ltyp := Etype (Left_Opnd (N));
5538 Rtyp := Etype (Right_Opnd (N));
5540 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
5541 -- type, then expand with a separate procedure. Note the use of the
5542 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5544 if Overflow_Check_Mode in Minimized_Or_Eliminated
5545 and then Is_Signed_Integer_Type (Ltyp)
5546 and then not No_Minimize_Eliminate (N)
5547 then
5548 Expand_Membership_Minimize_Eliminate_Overflow (N);
5549 return;
5550 end if;
5552 -- Check case of explicit test for an expression in range of its
5553 -- subtype. This is suspicious usage and we replace it with a 'Valid
5554 -- test and give a warning for scalar types.
5556 if Is_Scalar_Type (Ltyp)
5558 -- Only relevant for source comparisons
5560 and then Comes_From_Source (N)
5562 -- In floating-point this is a standard way to check for finite values
5563 -- and using 'Valid would typically be a pessimization.
5565 and then not Is_Floating_Point_Type (Ltyp)
5567 -- Don't give the message unless right operand is a type entity and
5568 -- the type of the left operand matches this type. Note that this
5569 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
5570 -- checks have changed the type of the left operand.
5572 and then Nkind (Rop) in N_Has_Entity
5573 and then Ltyp = Entity (Rop)
5575 -- Skip in VM mode, where we have no sense of invalid values. The
5576 -- warning still seems relevant, but not important enough to worry.
5578 and then VM_Target = No_VM
5580 -- Skip this for predicated types, where such expressions are a
5581 -- reasonable way of testing if something meets the predicate.
5583 and then not Present (Predicate_Function (Ltyp))
5584 then
5585 Substitute_Valid_Check;
5586 return;
5587 end if;
5589 -- Do validity check on operands
5591 if Validity_Checks_On and Validity_Check_Operands then
5592 Ensure_Valid (Left_Opnd (N));
5593 Validity_Check_Range (Right_Opnd (N));
5594 end if;
5596 -- Case of explicit range
5598 if Nkind (Rop) = N_Range then
5599 declare
5600 Lo : constant Node_Id := Low_Bound (Rop);
5601 Hi : constant Node_Id := High_Bound (Rop);
5603 Lo_Orig : constant Node_Id := Original_Node (Lo);
5604 Hi_Orig : constant Node_Id := Original_Node (Hi);
5606 Lcheck : Compare_Result;
5607 Ucheck : Compare_Result;
5609 Warn1 : constant Boolean :=
5610 Constant_Condition_Warnings
5611 and then Comes_From_Source (N)
5612 and then not In_Instance;
5613 -- This must be true for any of the optimization warnings, we
5614 -- clearly want to give them only for source with the flag on. We
5615 -- also skip these warnings in an instance since it may be the
5616 -- case that different instantiations have different ranges.
5618 Warn2 : constant Boolean :=
5619 Warn1
5620 and then Nkind (Original_Node (Rop)) = N_Range
5621 and then Is_Integer_Type (Etype (Lo));
5622 -- For the case where only one bound warning is elided, we also
5623 -- insist on an explicit range and an integer type. The reason is
5624 -- that the use of enumeration ranges including an end point is
5625 -- common, as is the use of a subtype name, one of whose bounds is
5626 -- the same as the type of the expression.
5628 begin
5629 -- If test is explicit x'First .. x'Last, replace by valid check
5631 -- Could use some individual comments for this complex test ???
5633 if Is_Scalar_Type (Ltyp)
5635 -- And left operand is X'First where X matches left operand
5636 -- type (this eliminates cases of type mismatch, including
5637 -- the cases where ELIMINATED/MINIMIZED mode has changed the
5638 -- type of the left operand.
5640 and then Nkind (Lo_Orig) = N_Attribute_Reference
5641 and then Attribute_Name (Lo_Orig) = Name_First
5642 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
5643 and then Entity (Prefix (Lo_Orig)) = Ltyp
5645 -- Same tests for right operand
5647 and then Nkind (Hi_Orig) = N_Attribute_Reference
5648 and then Attribute_Name (Hi_Orig) = Name_Last
5649 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
5650 and then Entity (Prefix (Hi_Orig)) = Ltyp
5652 -- Relevant only for source cases
5654 and then Comes_From_Source (N)
5656 -- Omit for VM cases, where we don't have invalid values
5658 and then VM_Target = No_VM
5659 then
5660 Substitute_Valid_Check;
5661 goto Leave;
5662 end if;
5664 -- If bounds of type are known at compile time, and the end points
5665 -- are known at compile time and identical, this is another case
5666 -- for substituting a valid test. We only do this for discrete
5667 -- types, since it won't arise in practice for float types.
5669 if Comes_From_Source (N)
5670 and then Is_Discrete_Type (Ltyp)
5671 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
5672 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
5673 and then Compile_Time_Known_Value (Lo)
5674 and then Compile_Time_Known_Value (Hi)
5675 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
5676 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
5678 -- Kill warnings in instances, since they may be cases where we
5679 -- have a test in the generic that makes sense with some types
5680 -- and not with other types.
5682 and then not In_Instance
5683 then
5684 Substitute_Valid_Check;
5685 goto Leave;
5686 end if;
5688 -- If we have an explicit range, do a bit of optimization based on
5689 -- range analysis (we may be able to kill one or both checks).
5691 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
5692 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
5694 -- If either check is known to fail, replace result by False since
5695 -- the other check does not matter. Preserve the static flag for
5696 -- legality checks, because we are constant-folding beyond RM 4.9.
5698 if Lcheck = LT or else Ucheck = GT then
5699 if Warn1 then
5700 Error_Msg_N ("?c?range test optimized away", N);
5701 Error_Msg_N ("\?c?value is known to be out of range", N);
5702 end if;
5704 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5705 Analyze_And_Resolve (N, Restyp);
5706 Set_Is_Static_Expression (N, Static);
5707 goto Leave;
5709 -- If both checks are known to succeed, replace result by True,
5710 -- since we know we are in range.
5712 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
5713 if Warn1 then
5714 Error_Msg_N ("?c?range test optimized away", N);
5715 Error_Msg_N ("\?c?value is known to be in range", N);
5716 end if;
5718 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5719 Analyze_And_Resolve (N, Restyp);
5720 Set_Is_Static_Expression (N, Static);
5721 goto Leave;
5723 -- If lower bound check succeeds and upper bound check is not
5724 -- known to succeed or fail, then replace the range check with
5725 -- a comparison against the upper bound.
5727 elsif Lcheck in Compare_GE then
5728 if Warn2 and then not In_Instance then
5729 Error_Msg_N ("??lower bound test optimized away", Lo);
5730 Error_Msg_N ("\??value is known to be in range", Lo);
5731 end if;
5733 Rewrite (N,
5734 Make_Op_Le (Loc,
5735 Left_Opnd => Lop,
5736 Right_Opnd => High_Bound (Rop)));
5737 Analyze_And_Resolve (N, Restyp);
5738 goto Leave;
5740 -- If upper bound check succeeds and lower bound check is not
5741 -- known to succeed or fail, then replace the range check with
5742 -- a comparison against the lower bound.
5744 elsif Ucheck in Compare_LE then
5745 if Warn2 and then not In_Instance then
5746 Error_Msg_N ("??upper bound test optimized away", Hi);
5747 Error_Msg_N ("\??value is known to be in range", Hi);
5748 end if;
5750 Rewrite (N,
5751 Make_Op_Ge (Loc,
5752 Left_Opnd => Lop,
5753 Right_Opnd => Low_Bound (Rop)));
5754 Analyze_And_Resolve (N, Restyp);
5755 goto Leave;
5756 end if;
5758 -- We couldn't optimize away the range check, but there is one
5759 -- more issue. If we are checking constant conditionals, then we
5760 -- see if we can determine the outcome assuming everything is
5761 -- valid, and if so give an appropriate warning.
5763 if Warn1 and then not Assume_No_Invalid_Values then
5764 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
5765 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
5767 -- Result is out of range for valid value
5769 if Lcheck = LT or else Ucheck = GT then
5770 Error_Msg_N
5771 ("?c?value can only be in range if it is invalid", N);
5773 -- Result is in range for valid value
5775 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
5776 Error_Msg_N
5777 ("?c?value can only be out of range if it is invalid", N);
5779 -- Lower bound check succeeds if value is valid
5781 elsif Warn2 and then Lcheck in Compare_GE then
5782 Error_Msg_N
5783 ("?c?lower bound check only fails if it is invalid", Lo);
5785 -- Upper bound check succeeds if value is valid
5787 elsif Warn2 and then Ucheck in Compare_LE then
5788 Error_Msg_N
5789 ("?c?upper bound check only fails for invalid values", Hi);
5790 end if;
5791 end if;
5792 end;
5794 -- For all other cases of an explicit range, nothing to be done
5796 goto Leave;
5798 -- Here right operand is a subtype mark
5800 else
5801 declare
5802 Typ : Entity_Id := Etype (Rop);
5803 Is_Acc : constant Boolean := Is_Access_Type (Typ);
5804 Cond : Node_Id := Empty;
5805 New_N : Node_Id;
5806 Obj : Node_Id := Lop;
5807 SCIL_Node : Node_Id;
5809 begin
5810 Remove_Side_Effects (Obj);
5812 -- For tagged type, do tagged membership operation
5814 if Is_Tagged_Type (Typ) then
5816 -- No expansion will be performed when VM_Target, as the VM
5817 -- back-ends will handle the membership tests directly (tags
5818 -- are not explicitly represented in Java objects, so the
5819 -- normal tagged membership expansion is not what we want).
5821 if Tagged_Type_Expansion then
5822 Tagged_Membership (N, SCIL_Node, New_N);
5823 Rewrite (N, New_N);
5824 Analyze_And_Resolve (N, Restyp);
5826 -- Update decoration of relocated node referenced by the
5827 -- SCIL node.
5829 if Generate_SCIL and then Present (SCIL_Node) then
5830 Set_SCIL_Node (N, SCIL_Node);
5831 end if;
5832 end if;
5834 goto Leave;
5836 -- If type is scalar type, rewrite as x in t'First .. t'Last.
5837 -- This reason we do this is that the bounds may have the wrong
5838 -- type if they come from the original type definition. Also this
5839 -- way we get all the processing above for an explicit range.
5841 -- Don't do this for predicated types, since in this case we
5842 -- want to check the predicate.
5844 elsif Is_Scalar_Type (Typ) then
5845 if No (Predicate_Function (Typ)) then
5846 Rewrite (Rop,
5847 Make_Range (Loc,
5848 Low_Bound =>
5849 Make_Attribute_Reference (Loc,
5850 Attribute_Name => Name_First,
5851 Prefix => New_Occurrence_Of (Typ, Loc)),
5853 High_Bound =>
5854 Make_Attribute_Reference (Loc,
5855 Attribute_Name => Name_Last,
5856 Prefix => New_Occurrence_Of (Typ, Loc))));
5857 Analyze_And_Resolve (N, Restyp);
5858 end if;
5860 goto Leave;
5862 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5863 -- a membership test if the subtype mark denotes a constrained
5864 -- Unchecked_Union subtype and the expression lacks inferable
5865 -- discriminants.
5867 elsif Is_Unchecked_Union (Base_Type (Typ))
5868 and then Is_Constrained (Typ)
5869 and then not Has_Inferable_Discriminants (Lop)
5870 then
5871 Insert_Action (N,
5872 Make_Raise_Program_Error (Loc,
5873 Reason => PE_Unchecked_Union_Restriction));
5875 -- Prevent Gigi from generating incorrect code by rewriting the
5876 -- test as False. What is this undocumented thing about ???
5878 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5879 goto Leave;
5880 end if;
5882 -- Here we have a non-scalar type
5884 if Is_Acc then
5885 Typ := Designated_Type (Typ);
5886 end if;
5888 if not Is_Constrained (Typ) then
5889 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5890 Analyze_And_Resolve (N, Restyp);
5892 -- For the constrained array case, we have to check the subscripts
5893 -- for an exact match if the lengths are non-zero (the lengths
5894 -- must match in any case).
5896 elsif Is_Array_Type (Typ) then
5897 Check_Subscripts : declare
5898 function Build_Attribute_Reference
5899 (E : Node_Id;
5900 Nam : Name_Id;
5901 Dim : Nat) return Node_Id;
5902 -- Build attribute reference E'Nam (Dim)
5904 -------------------------------
5905 -- Build_Attribute_Reference --
5906 -------------------------------
5908 function Build_Attribute_Reference
5909 (E : Node_Id;
5910 Nam : Name_Id;
5911 Dim : Nat) return Node_Id
5913 begin
5914 return
5915 Make_Attribute_Reference (Loc,
5916 Prefix => E,
5917 Attribute_Name => Nam,
5918 Expressions => New_List (
5919 Make_Integer_Literal (Loc, Dim)));
5920 end Build_Attribute_Reference;
5922 -- Start of processing for Check_Subscripts
5924 begin
5925 for J in 1 .. Number_Dimensions (Typ) loop
5926 Evolve_And_Then (Cond,
5927 Make_Op_Eq (Loc,
5928 Left_Opnd =>
5929 Build_Attribute_Reference
5930 (Duplicate_Subexpr_No_Checks (Obj),
5931 Name_First, J),
5932 Right_Opnd =>
5933 Build_Attribute_Reference
5934 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
5936 Evolve_And_Then (Cond,
5937 Make_Op_Eq (Loc,
5938 Left_Opnd =>
5939 Build_Attribute_Reference
5940 (Duplicate_Subexpr_No_Checks (Obj),
5941 Name_Last, J),
5942 Right_Opnd =>
5943 Build_Attribute_Reference
5944 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
5945 end loop;
5947 if Is_Acc then
5948 Cond :=
5949 Make_Or_Else (Loc,
5950 Left_Opnd =>
5951 Make_Op_Eq (Loc,
5952 Left_Opnd => Obj,
5953 Right_Opnd => Make_Null (Loc)),
5954 Right_Opnd => Cond);
5955 end if;
5957 Rewrite (N, Cond);
5958 Analyze_And_Resolve (N, Restyp);
5959 end Check_Subscripts;
5961 -- These are the cases where constraint checks may be required,
5962 -- e.g. records with possible discriminants
5964 else
5965 -- Expand the test into a series of discriminant comparisons.
5966 -- The expression that is built is the negation of the one that
5967 -- is used for checking discriminant constraints.
5969 Obj := Relocate_Node (Left_Opnd (N));
5971 if Has_Discriminants (Typ) then
5972 Cond := Make_Op_Not (Loc,
5973 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
5975 if Is_Acc then
5976 Cond := Make_Or_Else (Loc,
5977 Left_Opnd =>
5978 Make_Op_Eq (Loc,
5979 Left_Opnd => Obj,
5980 Right_Opnd => Make_Null (Loc)),
5981 Right_Opnd => Cond);
5982 end if;
5984 else
5985 Cond := New_Occurrence_Of (Standard_True, Loc);
5986 end if;
5988 Rewrite (N, Cond);
5989 Analyze_And_Resolve (N, Restyp);
5990 end if;
5992 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
5993 -- expression of an anonymous access type. This can involve an
5994 -- accessibility test and a tagged type membership test in the
5995 -- case of tagged designated types.
5997 if Ada_Version >= Ada_2012
5998 and then Is_Acc
5999 and then Ekind (Ltyp) = E_Anonymous_Access_Type
6000 then
6001 declare
6002 Expr_Entity : Entity_Id := Empty;
6003 New_N : Node_Id;
6004 Param_Level : Node_Id;
6005 Type_Level : Node_Id;
6007 begin
6008 if Is_Entity_Name (Lop) then
6009 Expr_Entity := Param_Entity (Lop);
6011 if not Present (Expr_Entity) then
6012 Expr_Entity := Entity (Lop);
6013 end if;
6014 end if;
6016 -- If a conversion of the anonymous access value to the
6017 -- tested type would be illegal, then the result is False.
6019 if not Valid_Conversion
6020 (Lop, Rtyp, Lop, Report_Errs => False)
6021 then
6022 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
6023 Analyze_And_Resolve (N, Restyp);
6025 -- Apply an accessibility check if the access object has an
6026 -- associated access level and when the level of the type is
6027 -- less deep than the level of the access parameter. This
6028 -- only occur for access parameters and stand-alone objects
6029 -- of an anonymous access type.
6031 else
6032 if Present (Expr_Entity)
6033 and then
6034 Present
6035 (Effective_Extra_Accessibility (Expr_Entity))
6036 and then UI_Gt (Object_Access_Level (Lop),
6037 Type_Access_Level (Rtyp))
6038 then
6039 Param_Level :=
6040 New_Occurrence_Of
6041 (Effective_Extra_Accessibility (Expr_Entity), Loc);
6043 Type_Level :=
6044 Make_Integer_Literal (Loc, Type_Access_Level (Rtyp));
6046 -- Return True only if the accessibility level of the
6047 -- expression entity is not deeper than the level of
6048 -- the tested access type.
6050 Rewrite (N,
6051 Make_And_Then (Loc,
6052 Left_Opnd => Relocate_Node (N),
6053 Right_Opnd => Make_Op_Le (Loc,
6054 Left_Opnd => Param_Level,
6055 Right_Opnd => Type_Level)));
6057 Analyze_And_Resolve (N);
6058 end if;
6060 -- If the designated type is tagged, do tagged membership
6061 -- operation.
6063 -- *** NOTE: we have to check not null before doing the
6064 -- tagged membership test (but maybe that can be done
6065 -- inside Tagged_Membership?).
6067 if Is_Tagged_Type (Typ) then
6068 Rewrite (N,
6069 Make_And_Then (Loc,
6070 Left_Opnd => Relocate_Node (N),
6071 Right_Opnd =>
6072 Make_Op_Ne (Loc,
6073 Left_Opnd => Obj,
6074 Right_Opnd => Make_Null (Loc))));
6076 -- No expansion will be performed when VM_Target, as
6077 -- the VM back-ends will handle the membership tests
6078 -- directly (tags are not explicitly represented in
6079 -- Java objects, so the normal tagged membership
6080 -- expansion is not what we want).
6082 if Tagged_Type_Expansion then
6084 -- Note that we have to pass Original_Node, because
6085 -- the membership test might already have been
6086 -- rewritten by earlier parts of membership test.
6088 Tagged_Membership
6089 (Original_Node (N), SCIL_Node, New_N);
6091 -- Update decoration of relocated node referenced
6092 -- by the SCIL node.
6094 if Generate_SCIL and then Present (SCIL_Node) then
6095 Set_SCIL_Node (New_N, SCIL_Node);
6096 end if;
6098 Rewrite (N,
6099 Make_And_Then (Loc,
6100 Left_Opnd => Relocate_Node (N),
6101 Right_Opnd => New_N));
6103 Analyze_And_Resolve (N, Restyp);
6104 end if;
6105 end if;
6106 end if;
6107 end;
6108 end if;
6109 end;
6110 end if;
6112 -- At this point, we have done the processing required for the basic
6113 -- membership test, but not yet dealt with the predicate.
6115 <<Leave>>
6117 -- If a predicate is present, then we do the predicate test, but we
6118 -- most certainly want to omit this if we are within the predicate
6119 -- function itself, since otherwise we have an infinite recursion.
6120 -- The check should also not be emitted when testing against a range
6121 -- (the check is only done when the right operand is a subtype; see
6122 -- RM12-4.5.2 (28.1/3-30/3)).
6124 declare
6125 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
6127 begin
6128 if Present (PFunc)
6129 and then Current_Scope /= PFunc
6130 and then Nkind (Rop) /= N_Range
6131 then
6132 Rewrite (N,
6133 Make_And_Then (Loc,
6134 Left_Opnd => Relocate_Node (N),
6135 Right_Opnd => Make_Predicate_Call (Rtyp, Lop, Mem => True)));
6137 -- Analyze new expression, mark left operand as analyzed to
6138 -- avoid infinite recursion adding predicate calls. Similarly,
6139 -- suppress further range checks on the call.
6141 Set_Analyzed (Left_Opnd (N));
6142 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6144 -- All done, skip attempt at compile time determination of result
6146 return;
6147 end if;
6148 end;
6149 end Expand_N_In;
6151 --------------------------------
6152 -- Expand_N_Indexed_Component --
6153 --------------------------------
6155 procedure Expand_N_Indexed_Component (N : Node_Id) is
6156 Loc : constant Source_Ptr := Sloc (N);
6157 Typ : constant Entity_Id := Etype (N);
6158 P : constant Node_Id := Prefix (N);
6159 T : constant Entity_Id := Etype (P);
6160 Atp : Entity_Id;
6162 begin
6163 -- A special optimization, if we have an indexed component that is
6164 -- selecting from a slice, then we can eliminate the slice, since, for
6165 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6166 -- the range check required by the slice. The range check for the slice
6167 -- itself has already been generated. The range check for the
6168 -- subscripting operation is ensured by converting the subject to
6169 -- the subtype of the slice.
6171 -- This optimization not only generates better code, avoiding slice
6172 -- messing especially in the packed case, but more importantly bypasses
6173 -- some problems in handling this peculiar case, for example, the issue
6174 -- of dealing specially with object renamings.
6176 if Nkind (P) = N_Slice
6178 -- This optimization is disabled for CodePeer because it can transform
6179 -- an index-check constraint_error into a range-check constraint_error
6180 -- and CodePeer cares about that distinction.
6182 and then not CodePeer_Mode
6183 then
6184 Rewrite (N,
6185 Make_Indexed_Component (Loc,
6186 Prefix => Prefix (P),
6187 Expressions => New_List (
6188 Convert_To
6189 (Etype (First_Index (Etype (P))),
6190 First (Expressions (N))))));
6191 Analyze_And_Resolve (N, Typ);
6192 return;
6193 end if;
6195 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6196 -- function, then additional actuals must be passed.
6198 if Ada_Version >= Ada_2005
6199 and then Is_Build_In_Place_Function_Call (P)
6200 then
6201 Make_Build_In_Place_Call_In_Anonymous_Context (P);
6202 end if;
6204 -- If the prefix is an access type, then we unconditionally rewrite if
6205 -- as an explicit dereference. This simplifies processing for several
6206 -- cases, including packed array cases and certain cases in which checks
6207 -- must be generated. We used to try to do this only when it was
6208 -- necessary, but it cleans up the code to do it all the time.
6210 if Is_Access_Type (T) then
6211 Insert_Explicit_Dereference (P);
6212 Analyze_And_Resolve (P, Designated_Type (T));
6213 Atp := Designated_Type (T);
6214 else
6215 Atp := T;
6216 end if;
6218 -- Generate index and validity checks
6220 Generate_Index_Checks (N);
6222 if Validity_Checks_On and then Validity_Check_Subscripts then
6223 Apply_Subscript_Validity_Checks (N);
6224 end if;
6226 -- If selecting from an array with atomic components, and atomic sync
6227 -- is not suppressed for this array type, set atomic sync flag.
6229 if (Has_Atomic_Components (Atp)
6230 and then not Atomic_Synchronization_Disabled (Atp))
6231 or else (Is_Atomic (Typ)
6232 and then not Atomic_Synchronization_Disabled (Typ))
6233 then
6234 Activate_Atomic_Synchronization (N);
6235 end if;
6237 -- All done for the non-packed case
6239 if not Is_Packed (Etype (Prefix (N))) then
6240 return;
6241 end if;
6243 -- For packed arrays that are not bit-packed (i.e. the case of an array
6244 -- with one or more index types with a non-contiguous enumeration type),
6245 -- we can always use the normal packed element get circuit.
6247 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
6248 Expand_Packed_Element_Reference (N);
6249 return;
6250 end if;
6252 -- For a reference to a component of a bit packed array, we convert it
6253 -- to a reference to the corresponding Packed_Array_Impl_Type. We only
6254 -- want to do this for simple references, and not for:
6256 -- Left side of assignment, or prefix of left side of assignment, or
6257 -- prefix of the prefix, to handle packed arrays of packed arrays,
6258 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6260 -- Renaming objects in renaming associations
6261 -- This case is handled when a use of the renamed variable occurs
6263 -- Actual parameters for a procedure call
6264 -- This case is handled in Exp_Ch6.Expand_Actuals
6266 -- The second expression in a 'Read attribute reference
6268 -- The prefix of an address or bit or size attribute reference
6270 -- The following circuit detects these exceptions
6272 declare
6273 Child : Node_Id := N;
6274 Parnt : Node_Id := Parent (N);
6276 begin
6277 loop
6278 if Nkind (Parnt) = N_Unchecked_Expression then
6279 null;
6281 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
6282 N_Procedure_Call_Statement)
6283 or else (Nkind (Parnt) = N_Parameter_Association
6284 and then
6285 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
6286 then
6287 return;
6289 elsif Nkind (Parnt) = N_Attribute_Reference
6290 and then Nam_In (Attribute_Name (Parnt), Name_Address,
6291 Name_Bit,
6292 Name_Size)
6293 and then Prefix (Parnt) = Child
6294 then
6295 return;
6297 elsif Nkind (Parnt) = N_Assignment_Statement
6298 and then Name (Parnt) = Child
6299 then
6300 return;
6302 -- If the expression is an index of an indexed component, it must
6303 -- be expanded regardless of context.
6305 elsif Nkind (Parnt) = N_Indexed_Component
6306 and then Child /= Prefix (Parnt)
6307 then
6308 Expand_Packed_Element_Reference (N);
6309 return;
6311 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
6312 and then Name (Parent (Parnt)) = Parnt
6313 then
6314 return;
6316 elsif Nkind (Parnt) = N_Attribute_Reference
6317 and then Attribute_Name (Parnt) = Name_Read
6318 and then Next (First (Expressions (Parnt))) = Child
6319 then
6320 return;
6322 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
6323 and then Prefix (Parnt) = Child
6324 then
6325 null;
6327 else
6328 Expand_Packed_Element_Reference (N);
6329 return;
6330 end if;
6332 -- Keep looking up tree for unchecked expression, or if we are the
6333 -- prefix of a possible assignment left side.
6335 Child := Parnt;
6336 Parnt := Parent (Child);
6337 end loop;
6338 end;
6339 end Expand_N_Indexed_Component;
6341 ---------------------
6342 -- Expand_N_Not_In --
6343 ---------------------
6345 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6346 -- can be done. This avoids needing to duplicate this expansion code.
6348 procedure Expand_N_Not_In (N : Node_Id) is
6349 Loc : constant Source_Ptr := Sloc (N);
6350 Typ : constant Entity_Id := Etype (N);
6351 Cfs : constant Boolean := Comes_From_Source (N);
6353 begin
6354 Rewrite (N,
6355 Make_Op_Not (Loc,
6356 Right_Opnd =>
6357 Make_In (Loc,
6358 Left_Opnd => Left_Opnd (N),
6359 Right_Opnd => Right_Opnd (N))));
6361 -- If this is a set membership, preserve list of alternatives
6363 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
6365 -- We want this to appear as coming from source if original does (see
6366 -- transformations in Expand_N_In).
6368 Set_Comes_From_Source (N, Cfs);
6369 Set_Comes_From_Source (Right_Opnd (N), Cfs);
6371 -- Now analyze transformed node
6373 Analyze_And_Resolve (N, Typ);
6374 end Expand_N_Not_In;
6376 -------------------
6377 -- Expand_N_Null --
6378 -------------------
6380 -- The only replacement required is for the case of a null of a type that
6381 -- is an access to protected subprogram, or a subtype thereof. We represent
6382 -- such access values as a record, and so we must replace the occurrence of
6383 -- null by the equivalent record (with a null address and a null pointer in
6384 -- it), so that the backend creates the proper value.
6386 procedure Expand_N_Null (N : Node_Id) is
6387 Loc : constant Source_Ptr := Sloc (N);
6388 Typ : constant Entity_Id := Base_Type (Etype (N));
6389 Agg : Node_Id;
6391 begin
6392 if Is_Access_Protected_Subprogram_Type (Typ) then
6393 Agg :=
6394 Make_Aggregate (Loc,
6395 Expressions => New_List (
6396 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
6397 Make_Null (Loc)));
6399 Rewrite (N, Agg);
6400 Analyze_And_Resolve (N, Equivalent_Type (Typ));
6402 -- For subsequent semantic analysis, the node must retain its type.
6403 -- Gigi in any case replaces this type by the corresponding record
6404 -- type before processing the node.
6406 Set_Etype (N, Typ);
6407 end if;
6409 exception
6410 when RE_Not_Available =>
6411 return;
6412 end Expand_N_Null;
6414 ---------------------
6415 -- Expand_N_Op_Abs --
6416 ---------------------
6418 procedure Expand_N_Op_Abs (N : Node_Id) is
6419 Loc : constant Source_Ptr := Sloc (N);
6420 Expr : constant Node_Id := Right_Opnd (N);
6422 begin
6423 Unary_Op_Validity_Checks (N);
6425 -- Check for MINIMIZED/ELIMINATED overflow mode
6427 if Minimized_Eliminated_Overflow_Check (N) then
6428 Apply_Arithmetic_Overflow_Check (N);
6429 return;
6430 end if;
6432 -- Deal with software overflow checking
6434 if not Backend_Overflow_Checks_On_Target
6435 and then Is_Signed_Integer_Type (Etype (N))
6436 and then Do_Overflow_Check (N)
6437 then
6438 -- The only case to worry about is when the argument is equal to the
6439 -- largest negative number, so what we do is to insert the check:
6441 -- [constraint_error when Expr = typ'Base'First]
6443 -- with the usual Duplicate_Subexpr use coding for expr
6445 Insert_Action (N,
6446 Make_Raise_Constraint_Error (Loc,
6447 Condition =>
6448 Make_Op_Eq (Loc,
6449 Left_Opnd => Duplicate_Subexpr (Expr),
6450 Right_Opnd =>
6451 Make_Attribute_Reference (Loc,
6452 Prefix =>
6453 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
6454 Attribute_Name => Name_First)),
6455 Reason => CE_Overflow_Check_Failed));
6456 end if;
6457 end Expand_N_Op_Abs;
6459 ---------------------
6460 -- Expand_N_Op_Add --
6461 ---------------------
6463 procedure Expand_N_Op_Add (N : Node_Id) is
6464 Typ : constant Entity_Id := Etype (N);
6466 begin
6467 Binary_Op_Validity_Checks (N);
6469 -- Check for MINIMIZED/ELIMINATED overflow mode
6471 if Minimized_Eliminated_Overflow_Check (N) then
6472 Apply_Arithmetic_Overflow_Check (N);
6473 return;
6474 end if;
6476 -- N + 0 = 0 + N = N for integer types
6478 if Is_Integer_Type (Typ) then
6479 if Compile_Time_Known_Value (Right_Opnd (N))
6480 and then Expr_Value (Right_Opnd (N)) = Uint_0
6481 then
6482 Rewrite (N, Left_Opnd (N));
6483 return;
6485 elsif Compile_Time_Known_Value (Left_Opnd (N))
6486 and then Expr_Value (Left_Opnd (N)) = Uint_0
6487 then
6488 Rewrite (N, Right_Opnd (N));
6489 return;
6490 end if;
6491 end if;
6493 -- Arithmetic overflow checks for signed integer/fixed point types
6495 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
6496 Apply_Arithmetic_Overflow_Check (N);
6497 return;
6498 end if;
6500 -- Overflow checks for floating-point if -gnateF mode active
6502 Check_Float_Op_Overflow (N);
6503 end Expand_N_Op_Add;
6505 ---------------------
6506 -- Expand_N_Op_And --
6507 ---------------------
6509 procedure Expand_N_Op_And (N : Node_Id) is
6510 Typ : constant Entity_Id := Etype (N);
6512 begin
6513 Binary_Op_Validity_Checks (N);
6515 if Is_Array_Type (Etype (N)) then
6516 Expand_Boolean_Operator (N);
6518 elsif Is_Boolean_Type (Etype (N)) then
6519 Adjust_Condition (Left_Opnd (N));
6520 Adjust_Condition (Right_Opnd (N));
6521 Set_Etype (N, Standard_Boolean);
6522 Adjust_Result_Type (N, Typ);
6524 elsif Is_Intrinsic_Subprogram (Entity (N)) then
6525 Expand_Intrinsic_Call (N, Entity (N));
6527 end if;
6528 end Expand_N_Op_And;
6530 ------------------------
6531 -- Expand_N_Op_Concat --
6532 ------------------------
6534 procedure Expand_N_Op_Concat (N : Node_Id) is
6535 Opnds : List_Id;
6536 -- List of operands to be concatenated
6538 Cnode : Node_Id;
6539 -- Node which is to be replaced by the result of concatenating the nodes
6540 -- in the list Opnds.
6542 begin
6543 -- Ensure validity of both operands
6545 Binary_Op_Validity_Checks (N);
6547 -- If we are the left operand of a concatenation higher up the tree,
6548 -- then do nothing for now, since we want to deal with a series of
6549 -- concatenations as a unit.
6551 if Nkind (Parent (N)) = N_Op_Concat
6552 and then N = Left_Opnd (Parent (N))
6553 then
6554 return;
6555 end if;
6557 -- We get here with a concatenation whose left operand may be a
6558 -- concatenation itself with a consistent type. We need to process
6559 -- these concatenation operands from left to right, which means
6560 -- from the deepest node in the tree to the highest node.
6562 Cnode := N;
6563 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
6564 Cnode := Left_Opnd (Cnode);
6565 end loop;
6567 -- Now Cnode is the deepest concatenation, and its parents are the
6568 -- concatenation nodes above, so now we process bottom up, doing the
6569 -- operands.
6571 -- The outer loop runs more than once if more than one concatenation
6572 -- type is involved.
6574 Outer : loop
6575 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
6576 Set_Parent (Opnds, N);
6578 -- The inner loop gathers concatenation operands
6580 Inner : while Cnode /= N
6581 and then Base_Type (Etype (Cnode)) =
6582 Base_Type (Etype (Parent (Cnode)))
6583 loop
6584 Cnode := Parent (Cnode);
6585 Append (Right_Opnd (Cnode), Opnds);
6586 end loop Inner;
6588 Expand_Concatenate (Cnode, Opnds);
6590 exit Outer when Cnode = N;
6591 Cnode := Parent (Cnode);
6592 end loop Outer;
6593 end Expand_N_Op_Concat;
6595 ------------------------
6596 -- Expand_N_Op_Divide --
6597 ------------------------
6599 procedure Expand_N_Op_Divide (N : Node_Id) is
6600 Loc : constant Source_Ptr := Sloc (N);
6601 Lopnd : constant Node_Id := Left_Opnd (N);
6602 Ropnd : constant Node_Id := Right_Opnd (N);
6603 Ltyp : constant Entity_Id := Etype (Lopnd);
6604 Rtyp : constant Entity_Id := Etype (Ropnd);
6605 Typ : Entity_Id := Etype (N);
6606 Rknow : constant Boolean := Is_Integer_Type (Typ)
6607 and then
6608 Compile_Time_Known_Value (Ropnd);
6609 Rval : Uint;
6611 begin
6612 Binary_Op_Validity_Checks (N);
6614 -- Check for MINIMIZED/ELIMINATED overflow mode
6616 if Minimized_Eliminated_Overflow_Check (N) then
6617 Apply_Arithmetic_Overflow_Check (N);
6618 return;
6619 end if;
6621 -- Otherwise proceed with expansion of division
6623 if Rknow then
6624 Rval := Expr_Value (Ropnd);
6625 end if;
6627 -- N / 1 = N for integer types
6629 if Rknow and then Rval = Uint_1 then
6630 Rewrite (N, Lopnd);
6631 return;
6632 end if;
6634 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
6635 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6636 -- operand is an unsigned integer, as required for this to work.
6638 if Nkind (Ropnd) = N_Op_Expon
6639 and then Is_Power_Of_2_For_Shift (Ropnd)
6641 -- We cannot do this transformation in configurable run time mode if we
6642 -- have 64-bit integers and long shifts are not available.
6644 and then (Esize (Ltyp) <= 32 or else Support_Long_Shifts_On_Target)
6645 then
6646 Rewrite (N,
6647 Make_Op_Shift_Right (Loc,
6648 Left_Opnd => Lopnd,
6649 Right_Opnd =>
6650 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
6651 Analyze_And_Resolve (N, Typ);
6652 return;
6653 end if;
6655 -- Do required fixup of universal fixed operation
6657 if Typ = Universal_Fixed then
6658 Fixup_Universal_Fixed_Operation (N);
6659 Typ := Etype (N);
6660 end if;
6662 -- Divisions with fixed-point results
6664 if Is_Fixed_Point_Type (Typ) then
6666 -- No special processing if Treat_Fixed_As_Integer is set, since
6667 -- from a semantic point of view such operations are simply integer
6668 -- operations and will be treated that way.
6670 if not Treat_Fixed_As_Integer (N) then
6671 if Is_Integer_Type (Rtyp) then
6672 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
6673 else
6674 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
6675 end if;
6676 end if;
6678 -- Other cases of division of fixed-point operands. Again we exclude the
6679 -- case where Treat_Fixed_As_Integer is set.
6681 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6682 and then not Treat_Fixed_As_Integer (N)
6683 then
6684 if Is_Integer_Type (Typ) then
6685 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
6686 else
6687 pragma Assert (Is_Floating_Point_Type (Typ));
6688 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
6689 end if;
6691 -- Mixed-mode operations can appear in a non-static universal context,
6692 -- in which case the integer argument must be converted explicitly.
6694 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
6695 Rewrite (Ropnd,
6696 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
6698 Analyze_And_Resolve (Ropnd, Universal_Real);
6700 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
6701 Rewrite (Lopnd,
6702 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
6704 Analyze_And_Resolve (Lopnd, Universal_Real);
6706 -- Non-fixed point cases, do integer zero divide and overflow checks
6708 elsif Is_Integer_Type (Typ) then
6709 Apply_Divide_Checks (N);
6710 end if;
6712 -- Overflow checks for floating-point if -gnateF mode active
6714 Check_Float_Op_Overflow (N);
6715 end Expand_N_Op_Divide;
6717 --------------------
6718 -- Expand_N_Op_Eq --
6719 --------------------
6721 procedure Expand_N_Op_Eq (N : Node_Id) is
6722 Loc : constant Source_Ptr := Sloc (N);
6723 Typ : constant Entity_Id := Etype (N);
6724 Lhs : constant Node_Id := Left_Opnd (N);
6725 Rhs : constant Node_Id := Right_Opnd (N);
6726 Bodies : constant List_Id := New_List;
6727 A_Typ : constant Entity_Id := Etype (Lhs);
6729 Typl : Entity_Id := A_Typ;
6730 Op_Name : Entity_Id;
6731 Prim : Elmt_Id;
6733 procedure Build_Equality_Call (Eq : Entity_Id);
6734 -- If a constructed equality exists for the type or for its parent,
6735 -- build and analyze call, adding conversions if the operation is
6736 -- inherited.
6738 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
6739 -- Determines whether a type has a subcomponent of an unconstrained
6740 -- Unchecked_Union subtype. Typ is a record type.
6742 -------------------------
6743 -- Build_Equality_Call --
6744 -------------------------
6746 procedure Build_Equality_Call (Eq : Entity_Id) is
6747 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
6748 L_Exp : Node_Id := Relocate_Node (Lhs);
6749 R_Exp : Node_Id := Relocate_Node (Rhs);
6751 begin
6752 -- Adjust operands if necessary to comparison type
6754 if Base_Type (Op_Type) /= Base_Type (A_Typ)
6755 and then not Is_Class_Wide_Type (A_Typ)
6756 then
6757 L_Exp := OK_Convert_To (Op_Type, L_Exp);
6758 R_Exp := OK_Convert_To (Op_Type, R_Exp);
6759 end if;
6761 -- If we have an Unchecked_Union, we need to add the inferred
6762 -- discriminant values as actuals in the function call. At this
6763 -- point, the expansion has determined that both operands have
6764 -- inferable discriminants.
6766 if Is_Unchecked_Union (Op_Type) then
6767 declare
6768 Lhs_Type : constant Node_Id := Etype (L_Exp);
6769 Rhs_Type : constant Node_Id := Etype (R_Exp);
6771 Lhs_Discr_Vals : Elist_Id;
6772 -- List of inferred discriminant values for left operand.
6774 Rhs_Discr_Vals : Elist_Id;
6775 -- List of inferred discriminant values for right operand.
6777 Discr : Entity_Id;
6779 begin
6780 Lhs_Discr_Vals := New_Elmt_List;
6781 Rhs_Discr_Vals := New_Elmt_List;
6783 -- Per-object constrained selected components require special
6784 -- attention. If the enclosing scope of the component is an
6785 -- Unchecked_Union, we cannot reference its discriminants
6786 -- directly. This is why we use the extra parameters of the
6787 -- equality function of the enclosing Unchecked_Union.
6789 -- type UU_Type (Discr : Integer := 0) is
6790 -- . . .
6791 -- end record;
6792 -- pragma Unchecked_Union (UU_Type);
6794 -- 1. Unchecked_Union enclosing record:
6796 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
6797 -- . . .
6798 -- Comp : UU_Type (Discr);
6799 -- . . .
6800 -- end Enclosing_UU_Type;
6801 -- pragma Unchecked_Union (Enclosing_UU_Type);
6803 -- Obj1 : Enclosing_UU_Type;
6804 -- Obj2 : Enclosing_UU_Type (1);
6806 -- [. . .] Obj1 = Obj2 [. . .]
6808 -- Generated code:
6810 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
6812 -- A and B are the formal parameters of the equality function
6813 -- of Enclosing_UU_Type. The function always has two extra
6814 -- formals to capture the inferred discriminant values for
6815 -- each discriminant of the type.
6817 -- 2. Non-Unchecked_Union enclosing record:
6819 -- type
6820 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
6821 -- is record
6822 -- . . .
6823 -- Comp : UU_Type (Discr);
6824 -- . . .
6825 -- end Enclosing_Non_UU_Type;
6827 -- Obj1 : Enclosing_Non_UU_Type;
6828 -- Obj2 : Enclosing_Non_UU_Type (1);
6830 -- ... Obj1 = Obj2 ...
6832 -- Generated code:
6834 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
6835 -- obj1.discr, obj2.discr)) then
6837 -- In this case we can directly reference the discriminants of
6838 -- the enclosing record.
6840 -- Process left operand of equality
6842 if Nkind (Lhs) = N_Selected_Component
6843 and then
6844 Has_Per_Object_Constraint (Entity (Selector_Name (Lhs)))
6845 then
6846 -- If enclosing record is an Unchecked_Union, use formals
6847 -- corresponding to each discriminant. The name of the
6848 -- formal is that of the discriminant, with added suffix,
6849 -- see Exp_Ch3.Build_Record_Equality for details.
6851 if Is_Unchecked_Union (Scope (Entity (Selector_Name (Lhs))))
6852 then
6853 Discr :=
6854 First_Discriminant
6855 (Scope (Entity (Selector_Name (Lhs))));
6856 while Present (Discr) loop
6857 Append_Elmt
6858 (Make_Identifier (Loc,
6859 Chars => New_External_Name (Chars (Discr), 'A')),
6860 To => Lhs_Discr_Vals);
6861 Next_Discriminant (Discr);
6862 end loop;
6864 -- If enclosing record is of a non-Unchecked_Union type, it
6865 -- is possible to reference its discriminants directly.
6867 else
6868 Discr := First_Discriminant (Lhs_Type);
6869 while Present (Discr) loop
6870 Append_Elmt
6871 (Make_Selected_Component (Loc,
6872 Prefix => Prefix (Lhs),
6873 Selector_Name =>
6874 New_Copy
6875 (Get_Discriminant_Value (Discr,
6876 Lhs_Type,
6877 Stored_Constraint (Lhs_Type)))),
6878 To => Lhs_Discr_Vals);
6879 Next_Discriminant (Discr);
6880 end loop;
6881 end if;
6883 -- Otherwise operand is on object with a constrained type.
6884 -- Infer the discriminant values from the constraint.
6886 else
6888 Discr := First_Discriminant (Lhs_Type);
6889 while Present (Discr) loop
6890 Append_Elmt
6891 (New_Copy
6892 (Get_Discriminant_Value (Discr,
6893 Lhs_Type,
6894 Stored_Constraint (Lhs_Type))),
6895 To => Lhs_Discr_Vals);
6896 Next_Discriminant (Discr);
6897 end loop;
6898 end if;
6900 -- Similar processing for right operand of equality
6902 if Nkind (Rhs) = N_Selected_Component
6903 and then
6904 Has_Per_Object_Constraint (Entity (Selector_Name (Rhs)))
6905 then
6906 if Is_Unchecked_Union
6907 (Scope (Entity (Selector_Name (Rhs))))
6908 then
6909 Discr :=
6910 First_Discriminant
6911 (Scope (Entity (Selector_Name (Rhs))));
6912 while Present (Discr) loop
6913 Append_Elmt
6914 (Make_Identifier (Loc,
6915 Chars => New_External_Name (Chars (Discr), 'B')),
6916 To => Rhs_Discr_Vals);
6917 Next_Discriminant (Discr);
6918 end loop;
6920 else
6921 Discr := First_Discriminant (Rhs_Type);
6922 while Present (Discr) loop
6923 Append_Elmt
6924 (Make_Selected_Component (Loc,
6925 Prefix => Prefix (Rhs),
6926 Selector_Name =>
6927 New_Copy (Get_Discriminant_Value
6928 (Discr,
6929 Rhs_Type,
6930 Stored_Constraint (Rhs_Type)))),
6931 To => Rhs_Discr_Vals);
6932 Next_Discriminant (Discr);
6933 end loop;
6934 end if;
6936 else
6937 Discr := First_Discriminant (Rhs_Type);
6938 while Present (Discr) loop
6939 Append_Elmt
6940 (New_Copy (Get_Discriminant_Value
6941 (Discr,
6942 Rhs_Type,
6943 Stored_Constraint (Rhs_Type))),
6944 To => Rhs_Discr_Vals);
6945 Next_Discriminant (Discr);
6946 end loop;
6947 end if;
6949 -- Now merge the list of discriminant values so that values
6950 -- of corresponding discriminants are adjacent.
6952 declare
6953 Params : List_Id;
6954 L_Elmt : Elmt_Id;
6955 R_Elmt : Elmt_Id;
6957 begin
6958 Params := New_List (L_Exp, R_Exp);
6959 L_Elmt := First_Elmt (Lhs_Discr_Vals);
6960 R_Elmt := First_Elmt (Rhs_Discr_Vals);
6961 while Present (L_Elmt) loop
6962 Append_To (Params, Node (L_Elmt));
6963 Append_To (Params, Node (R_Elmt));
6964 Next_Elmt (L_Elmt);
6965 Next_Elmt (R_Elmt);
6966 end loop;
6968 Rewrite (N,
6969 Make_Function_Call (Loc,
6970 Name => New_Occurrence_Of (Eq, Loc),
6971 Parameter_Associations => Params));
6972 end;
6973 end;
6975 -- Normal case, not an unchecked union
6977 else
6978 Rewrite (N,
6979 Make_Function_Call (Loc,
6980 Name => New_Occurrence_Of (Eq, Loc),
6981 Parameter_Associations => New_List (L_Exp, R_Exp)));
6982 end if;
6984 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6985 end Build_Equality_Call;
6987 ------------------------------------
6988 -- Has_Unconstrained_UU_Component --
6989 ------------------------------------
6991 function Has_Unconstrained_UU_Component
6992 (Typ : Node_Id) return Boolean
6994 Tdef : constant Node_Id :=
6995 Type_Definition (Declaration_Node (Base_Type (Typ)));
6996 Clist : Node_Id;
6997 Vpart : Node_Id;
6999 function Component_Is_Unconstrained_UU
7000 (Comp : Node_Id) return Boolean;
7001 -- Determines whether the subtype of the component is an
7002 -- unconstrained Unchecked_Union.
7004 function Variant_Is_Unconstrained_UU
7005 (Variant : Node_Id) return Boolean;
7006 -- Determines whether a component of the variant has an unconstrained
7007 -- Unchecked_Union subtype.
7009 -----------------------------------
7010 -- Component_Is_Unconstrained_UU --
7011 -----------------------------------
7013 function Component_Is_Unconstrained_UU
7014 (Comp : Node_Id) return Boolean
7016 begin
7017 if Nkind (Comp) /= N_Component_Declaration then
7018 return False;
7019 end if;
7021 declare
7022 Sindic : constant Node_Id :=
7023 Subtype_Indication (Component_Definition (Comp));
7025 begin
7026 -- Unconstrained nominal type. In the case of a constraint
7027 -- present, the node kind would have been N_Subtype_Indication.
7029 if Nkind (Sindic) = N_Identifier then
7030 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
7031 end if;
7033 return False;
7034 end;
7035 end Component_Is_Unconstrained_UU;
7037 ---------------------------------
7038 -- Variant_Is_Unconstrained_UU --
7039 ---------------------------------
7041 function Variant_Is_Unconstrained_UU
7042 (Variant : Node_Id) return Boolean
7044 Clist : constant Node_Id := Component_List (Variant);
7046 begin
7047 if Is_Empty_List (Component_Items (Clist)) then
7048 return False;
7049 end if;
7051 -- We only need to test one component
7053 declare
7054 Comp : Node_Id := First (Component_Items (Clist));
7056 begin
7057 while Present (Comp) loop
7058 if Component_Is_Unconstrained_UU (Comp) then
7059 return True;
7060 end if;
7062 Next (Comp);
7063 end loop;
7064 end;
7066 -- None of the components withing the variant were of
7067 -- unconstrained Unchecked_Union type.
7069 return False;
7070 end Variant_Is_Unconstrained_UU;
7072 -- Start of processing for Has_Unconstrained_UU_Component
7074 begin
7075 if Null_Present (Tdef) then
7076 return False;
7077 end if;
7079 Clist := Component_List (Tdef);
7080 Vpart := Variant_Part (Clist);
7082 -- Inspect available components
7084 if Present (Component_Items (Clist)) then
7085 declare
7086 Comp : Node_Id := First (Component_Items (Clist));
7088 begin
7089 while Present (Comp) loop
7091 -- One component is sufficient
7093 if Component_Is_Unconstrained_UU (Comp) then
7094 return True;
7095 end if;
7097 Next (Comp);
7098 end loop;
7099 end;
7100 end if;
7102 -- Inspect available components withing variants
7104 if Present (Vpart) then
7105 declare
7106 Variant : Node_Id := First (Variants (Vpart));
7108 begin
7109 while Present (Variant) loop
7111 -- One component within a variant is sufficient
7113 if Variant_Is_Unconstrained_UU (Variant) then
7114 return True;
7115 end if;
7117 Next (Variant);
7118 end loop;
7119 end;
7120 end if;
7122 -- Neither the available components, nor the components inside the
7123 -- variant parts were of an unconstrained Unchecked_Union subtype.
7125 return False;
7126 end Has_Unconstrained_UU_Component;
7128 -- Start of processing for Expand_N_Op_Eq
7130 begin
7131 Binary_Op_Validity_Checks (N);
7133 -- Deal with private types
7135 if Ekind (Typl) = E_Private_Type then
7136 Typl := Underlying_Type (Typl);
7137 elsif Ekind (Typl) = E_Private_Subtype then
7138 Typl := Underlying_Type (Base_Type (Typl));
7139 else
7140 null;
7141 end if;
7143 -- It may happen in error situations that the underlying type is not
7144 -- set. The error will be detected later, here we just defend the
7145 -- expander code.
7147 if No (Typl) then
7148 return;
7149 end if;
7151 Typl := Base_Type (Typl);
7153 -- Equality between variant records results in a call to a routine
7154 -- that has conditional tests of the discriminant value(s), and hence
7155 -- violates the No_Implicit_Conditionals restriction.
7157 if Has_Variant_Part (Typl) then
7158 declare
7159 Msg : Boolean;
7161 begin
7162 Check_Restriction (Msg, No_Implicit_Conditionals, N);
7164 if Msg then
7165 Error_Msg_N
7166 ("\comparison of variant records tests discriminants", N);
7167 return;
7168 end if;
7169 end;
7170 end if;
7172 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7173 -- means we no longer have a comparison operation, we are all done.
7175 Expand_Compare_Minimize_Eliminate_Overflow (N);
7177 if Nkind (N) /= N_Op_Eq then
7178 return;
7179 end if;
7181 -- Boolean types (requiring handling of non-standard case)
7183 if Is_Boolean_Type (Typl) then
7184 Adjust_Condition (Left_Opnd (N));
7185 Adjust_Condition (Right_Opnd (N));
7186 Set_Etype (N, Standard_Boolean);
7187 Adjust_Result_Type (N, Typ);
7189 -- Array types
7191 elsif Is_Array_Type (Typl) then
7193 -- If we are doing full validity checking, and it is possible for the
7194 -- array elements to be invalid then expand out array comparisons to
7195 -- make sure that we check the array elements.
7197 if Validity_Check_Operands
7198 and then not Is_Known_Valid (Component_Type (Typl))
7199 then
7200 declare
7201 Save_Force_Validity_Checks : constant Boolean :=
7202 Force_Validity_Checks;
7203 begin
7204 Force_Validity_Checks := True;
7205 Rewrite (N,
7206 Expand_Array_Equality
7208 Relocate_Node (Lhs),
7209 Relocate_Node (Rhs),
7210 Bodies,
7211 Typl));
7212 Insert_Actions (N, Bodies);
7213 Analyze_And_Resolve (N, Standard_Boolean);
7214 Force_Validity_Checks := Save_Force_Validity_Checks;
7215 end;
7217 -- Packed case where both operands are known aligned
7219 elsif Is_Bit_Packed_Array (Typl)
7220 and then not Is_Possibly_Unaligned_Object (Lhs)
7221 and then not Is_Possibly_Unaligned_Object (Rhs)
7222 then
7223 Expand_Packed_Eq (N);
7225 -- Where the component type is elementary we can use a block bit
7226 -- comparison (if supported on the target) exception in the case
7227 -- of floating-point (negative zero issues require element by
7228 -- element comparison), and atomic types (where we must be sure
7229 -- to load elements independently) and possibly unaligned arrays.
7231 elsif Is_Elementary_Type (Component_Type (Typl))
7232 and then not Is_Floating_Point_Type (Component_Type (Typl))
7233 and then not Is_Atomic (Component_Type (Typl))
7234 and then not Is_Possibly_Unaligned_Object (Lhs)
7235 and then not Is_Possibly_Unaligned_Object (Rhs)
7236 and then Support_Composite_Compare_On_Target
7237 then
7238 null;
7240 -- For composite and floating-point cases, expand equality loop to
7241 -- make sure of using proper comparisons for tagged types, and
7242 -- correctly handling the floating-point case.
7244 else
7245 Rewrite (N,
7246 Expand_Array_Equality
7248 Relocate_Node (Lhs),
7249 Relocate_Node (Rhs),
7250 Bodies,
7251 Typl));
7252 Insert_Actions (N, Bodies, Suppress => All_Checks);
7253 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7254 end if;
7256 -- Record Types
7258 elsif Is_Record_Type (Typl) then
7260 -- For tagged types, use the primitive "="
7262 if Is_Tagged_Type (Typl) then
7264 -- No need to do anything else compiling under restriction
7265 -- No_Dispatching_Calls. During the semantic analysis we
7266 -- already notified such violation.
7268 if Restriction_Active (No_Dispatching_Calls) then
7269 return;
7270 end if;
7272 -- If this is derived from an untagged private type completed with
7273 -- a tagged type, it does not have a full view, so we use the
7274 -- primitive operations of the private type. This check should no
7275 -- longer be necessary when these types get their full views???
7277 if Is_Private_Type (A_Typ)
7278 and then not Is_Tagged_Type (A_Typ)
7279 and then Is_Derived_Type (A_Typ)
7280 and then No (Full_View (A_Typ))
7281 then
7282 -- Search for equality operation, checking that the operands
7283 -- have the same type. Note that we must find a matching entry,
7284 -- or something is very wrong.
7286 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
7288 while Present (Prim) loop
7289 exit when Chars (Node (Prim)) = Name_Op_Eq
7290 and then Etype (First_Formal (Node (Prim))) =
7291 Etype (Next_Formal (First_Formal (Node (Prim))))
7292 and then
7293 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7295 Next_Elmt (Prim);
7296 end loop;
7298 pragma Assert (Present (Prim));
7299 Op_Name := Node (Prim);
7301 -- Find the type's predefined equality or an overriding
7302 -- user-defined equality. The reason for not simply calling
7303 -- Find_Prim_Op here is that there may be a user-defined
7304 -- overloaded equality op that precedes the equality that we
7305 -- want, so we have to explicitly search (e.g., there could be
7306 -- an equality with two different parameter types).
7308 else
7309 if Is_Class_Wide_Type (Typl) then
7310 Typl := Find_Specific_Type (Typl);
7311 end if;
7313 Prim := First_Elmt (Primitive_Operations (Typl));
7314 while Present (Prim) loop
7315 exit when Chars (Node (Prim)) = Name_Op_Eq
7316 and then Etype (First_Formal (Node (Prim))) =
7317 Etype (Next_Formal (First_Formal (Node (Prim))))
7318 and then
7319 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7321 Next_Elmt (Prim);
7322 end loop;
7324 pragma Assert (Present (Prim));
7325 Op_Name := Node (Prim);
7326 end if;
7328 Build_Equality_Call (Op_Name);
7330 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7331 -- predefined equality operator for a type which has a subcomponent
7332 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7334 elsif Has_Unconstrained_UU_Component (Typl) then
7335 Insert_Action (N,
7336 Make_Raise_Program_Error (Loc,
7337 Reason => PE_Unchecked_Union_Restriction));
7339 -- Prevent Gigi from generating incorrect code by rewriting the
7340 -- equality as a standard False. (is this documented somewhere???)
7342 Rewrite (N,
7343 New_Occurrence_Of (Standard_False, Loc));
7345 elsif Is_Unchecked_Union (Typl) then
7347 -- If we can infer the discriminants of the operands, we make a
7348 -- call to the TSS equality function.
7350 if Has_Inferable_Discriminants (Lhs)
7351 and then
7352 Has_Inferable_Discriminants (Rhs)
7353 then
7354 Build_Equality_Call
7355 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7357 else
7358 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7359 -- the predefined equality operator for an Unchecked_Union type
7360 -- if either of the operands lack inferable discriminants.
7362 Insert_Action (N,
7363 Make_Raise_Program_Error (Loc,
7364 Reason => PE_Unchecked_Union_Restriction));
7366 -- Emit a warning on source equalities only, otherwise the
7367 -- message may appear out of place due to internal use. The
7368 -- warning is unconditional because it is required by the
7369 -- language.
7371 if Comes_From_Source (N) then
7372 Error_Msg_N
7373 ("Unchecked_Union discriminants cannot be determined??",
7375 Error_Msg_N
7376 ("\Program_Error will be raised for equality operation??",
7378 end if;
7380 -- Prevent Gigi from generating incorrect code by rewriting
7381 -- the equality as a standard False (documented where???).
7383 Rewrite (N,
7384 New_Occurrence_Of (Standard_False, Loc));
7385 end if;
7387 -- If a type support function is present (for complex cases), use it
7389 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
7390 Build_Equality_Call
7391 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7393 -- When comparing two Bounded_Strings, use the primitive equality of
7394 -- the root Super_String type.
7396 elsif Is_Bounded_String (Typl) then
7397 Prim :=
7398 First_Elmt (Collect_Primitive_Operations (Root_Type (Typl)));
7400 while Present (Prim) loop
7401 exit when Chars (Node (Prim)) = Name_Op_Eq
7402 and then Etype (First_Formal (Node (Prim))) =
7403 Etype (Next_Formal (First_Formal (Node (Prim))))
7404 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7406 Next_Elmt (Prim);
7407 end loop;
7409 -- A Super_String type should always have a primitive equality
7411 pragma Assert (Present (Prim));
7412 Build_Equality_Call (Node (Prim));
7414 -- Otherwise expand the component by component equality. Note that
7415 -- we never use block-bit comparisons for records, because of the
7416 -- problems with gaps. The backend will often be able to recombine
7417 -- the separate comparisons that we generate here.
7419 else
7420 Remove_Side_Effects (Lhs);
7421 Remove_Side_Effects (Rhs);
7422 Rewrite (N,
7423 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
7425 Insert_Actions (N, Bodies, Suppress => All_Checks);
7426 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7427 end if;
7428 end if;
7430 -- Test if result is known at compile time
7432 Rewrite_Comparison (N);
7434 Optimize_Length_Comparison (N);
7435 end Expand_N_Op_Eq;
7437 -----------------------
7438 -- Expand_N_Op_Expon --
7439 -----------------------
7441 procedure Expand_N_Op_Expon (N : Node_Id) is
7442 Loc : constant Source_Ptr := Sloc (N);
7443 Typ : constant Entity_Id := Etype (N);
7444 Rtyp : constant Entity_Id := Root_Type (Typ);
7445 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
7446 Bastyp : constant Node_Id := Etype (Base);
7447 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
7448 Exptyp : constant Entity_Id := Etype (Exp);
7449 Ovflo : constant Boolean := Do_Overflow_Check (N);
7450 Expv : Uint;
7451 Temp : Node_Id;
7452 Rent : RE_Id;
7453 Ent : Entity_Id;
7454 Etyp : Entity_Id;
7455 Xnode : Node_Id;
7457 begin
7458 Binary_Op_Validity_Checks (N);
7460 -- CodePeer wants to see the unexpanded N_Op_Expon node
7462 if CodePeer_Mode then
7463 return;
7464 end if;
7466 -- If either operand is of a private type, then we have the use of an
7467 -- intrinsic operator, and we get rid of the privateness, by using root
7468 -- types of underlying types for the actual operation. Otherwise the
7469 -- private types will cause trouble if we expand multiplications or
7470 -- shifts etc. We also do this transformation if the result type is
7471 -- different from the base type.
7473 if Is_Private_Type (Etype (Base))
7474 or else Is_Private_Type (Typ)
7475 or else Is_Private_Type (Exptyp)
7476 or else Rtyp /= Root_Type (Bastyp)
7477 then
7478 declare
7479 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
7480 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
7481 begin
7482 Rewrite (N,
7483 Unchecked_Convert_To (Typ,
7484 Make_Op_Expon (Loc,
7485 Left_Opnd => Unchecked_Convert_To (Bt, Base),
7486 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
7487 Analyze_And_Resolve (N, Typ);
7488 return;
7489 end;
7490 end if;
7492 -- Check for MINIMIZED/ELIMINATED overflow mode
7494 if Minimized_Eliminated_Overflow_Check (N) then
7495 Apply_Arithmetic_Overflow_Check (N);
7496 return;
7497 end if;
7499 -- Test for case of known right argument where we can replace the
7500 -- exponentiation by an equivalent expression using multiplication.
7502 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
7503 -- configurable run-time mode, we may not have the exponentiation
7504 -- routine available, and we don't want the legality of the program
7505 -- to depend on how clever the compiler is in knowing values.
7507 if CRT_Safe_Compile_Time_Known_Value (Exp) then
7508 Expv := Expr_Value (Exp);
7510 -- We only fold small non-negative exponents. You might think we
7511 -- could fold small negative exponents for the real case, but we
7512 -- can't because we are required to raise Constraint_Error for
7513 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
7514 -- See ACVC test C4A012B.
7516 if Expv >= 0 and then Expv <= 4 then
7518 -- X ** 0 = 1 (or 1.0)
7520 if Expv = 0 then
7522 -- Call Remove_Side_Effects to ensure that any side effects
7523 -- in the ignored left operand (in particular function calls
7524 -- to user defined functions) are properly executed.
7526 Remove_Side_Effects (Base);
7528 if Ekind (Typ) in Integer_Kind then
7529 Xnode := Make_Integer_Literal (Loc, Intval => 1);
7530 else
7531 Xnode := Make_Real_Literal (Loc, Ureal_1);
7532 end if;
7534 -- X ** 1 = X
7536 elsif Expv = 1 then
7537 Xnode := Base;
7539 -- X ** 2 = X * X
7541 elsif Expv = 2 then
7542 Xnode :=
7543 Make_Op_Multiply (Loc,
7544 Left_Opnd => Duplicate_Subexpr (Base),
7545 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
7547 -- X ** 3 = X * X * X
7549 elsif Expv = 3 then
7550 Xnode :=
7551 Make_Op_Multiply (Loc,
7552 Left_Opnd =>
7553 Make_Op_Multiply (Loc,
7554 Left_Opnd => Duplicate_Subexpr (Base),
7555 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
7556 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
7558 -- X ** 4 ->
7560 -- do
7561 -- En : constant base'type := base * base;
7562 -- in
7563 -- En * En
7565 else
7566 pragma Assert (Expv = 4);
7567 Temp := Make_Temporary (Loc, 'E', Base);
7569 Xnode :=
7570 Make_Expression_With_Actions (Loc,
7571 Actions => New_List (
7572 Make_Object_Declaration (Loc,
7573 Defining_Identifier => Temp,
7574 Constant_Present => True,
7575 Object_Definition => New_Occurrence_Of (Typ, Loc),
7576 Expression =>
7577 Make_Op_Multiply (Loc,
7578 Left_Opnd =>
7579 Duplicate_Subexpr (Base),
7580 Right_Opnd =>
7581 Duplicate_Subexpr_No_Checks (Base)))),
7583 Expression =>
7584 Make_Op_Multiply (Loc,
7585 Left_Opnd => New_Occurrence_Of (Temp, Loc),
7586 Right_Opnd => New_Occurrence_Of (Temp, Loc)));
7587 end if;
7589 Rewrite (N, Xnode);
7590 Analyze_And_Resolve (N, Typ);
7591 return;
7592 end if;
7593 end if;
7595 -- Case of (2 ** expression) appearing as an argument of an integer
7596 -- multiplication, or as the right argument of a division of a non-
7597 -- negative integer. In such cases we leave the node untouched, setting
7598 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
7599 -- of the higher level node converts it into a shift.
7601 -- Another case is 2 ** N in any other context. We simply convert
7602 -- this to 1 * 2 ** N, and then the above transformation applies.
7604 -- Note: this transformation is not applicable for a modular type with
7605 -- a non-binary modulus in the multiplication case, since we get a wrong
7606 -- result if the shift causes an overflow before the modular reduction.
7608 -- Note: we used to check that Exptyp was an unsigned type. But that is
7609 -- an unnecessary check, since if Exp is negative, we have a run-time
7610 -- error that is either caught (so we get the right result) or we have
7611 -- suppressed the check, in which case the code is erroneous anyway.
7613 if Nkind (Base) = N_Integer_Literal
7614 and then CRT_Safe_Compile_Time_Known_Value (Base)
7615 and then Expr_Value (Base) = Uint_2
7616 and then Is_Integer_Type (Root_Type (Exptyp))
7617 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
7618 and then not Ovflo
7619 then
7620 -- First the multiply and divide cases
7622 if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then
7623 declare
7624 P : constant Node_Id := Parent (N);
7625 L : constant Node_Id := Left_Opnd (P);
7626 R : constant Node_Id := Right_Opnd (P);
7628 begin
7629 if (Nkind (P) = N_Op_Multiply
7630 and then not Non_Binary_Modulus (Typ)
7631 and then
7632 ((Is_Integer_Type (Etype (L)) and then R = N)
7633 or else
7634 (Is_Integer_Type (Etype (R)) and then L = N))
7635 and then not Do_Overflow_Check (P))
7636 or else
7637 (Nkind (P) = N_Op_Divide
7638 and then Is_Integer_Type (Etype (L))
7639 and then Is_Unsigned_Type (Etype (L))
7640 and then R = N
7641 and then not Do_Overflow_Check (P))
7642 then
7643 Set_Is_Power_Of_2_For_Shift (N);
7644 return;
7645 end if;
7646 end;
7648 -- Now the other cases
7650 elsif not Non_Binary_Modulus (Typ) then
7651 Rewrite (N,
7652 Make_Op_Multiply (Loc,
7653 Left_Opnd => Make_Integer_Literal (Loc, 1),
7654 Right_Opnd => Relocate_Node (N)));
7655 Analyze_And_Resolve (N, Typ);
7656 return;
7657 end if;
7658 end if;
7660 -- Fall through if exponentiation must be done using a runtime routine
7662 -- First deal with modular case
7664 if Is_Modular_Integer_Type (Rtyp) then
7666 -- Non-binary case, we call the special exponentiation routine for
7667 -- the non-binary case, converting the argument to Long_Long_Integer
7668 -- and passing the modulus value. Then the result is converted back
7669 -- to the base type.
7671 if Non_Binary_Modulus (Rtyp) then
7672 Rewrite (N,
7673 Convert_To (Typ,
7674 Make_Function_Call (Loc,
7675 Name =>
7676 New_Occurrence_Of (RTE (RE_Exp_Modular), Loc),
7677 Parameter_Associations => New_List (
7678 Convert_To (RTE (RE_Unsigned), Base),
7679 Make_Integer_Literal (Loc, Modulus (Rtyp)),
7680 Exp))));
7682 -- Binary case, in this case, we call one of two routines, either the
7683 -- unsigned integer case, or the unsigned long long integer case,
7684 -- with a final "and" operation to do the required mod.
7686 else
7687 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
7688 Ent := RTE (RE_Exp_Unsigned);
7689 else
7690 Ent := RTE (RE_Exp_Long_Long_Unsigned);
7691 end if;
7693 Rewrite (N,
7694 Convert_To (Typ,
7695 Make_Op_And (Loc,
7696 Left_Opnd =>
7697 Make_Function_Call (Loc,
7698 Name => New_Occurrence_Of (Ent, Loc),
7699 Parameter_Associations => New_List (
7700 Convert_To (Etype (First_Formal (Ent)), Base),
7701 Exp)),
7702 Right_Opnd =>
7703 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
7705 end if;
7707 -- Common exit point for modular type case
7709 Analyze_And_Resolve (N, Typ);
7710 return;
7712 -- Signed integer cases, done using either Integer or Long_Long_Integer.
7713 -- It is not worth having routines for Short_[Short_]Integer, since for
7714 -- most machines it would not help, and it would generate more code that
7715 -- might need certification when a certified run time is required.
7717 -- In the integer cases, we have two routines, one for when overflow
7718 -- checks are required, and one when they are not required, since there
7719 -- is a real gain in omitting checks on many machines.
7721 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
7722 or else (Rtyp = Base_Type (Standard_Long_Integer)
7723 and then
7724 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
7725 or else Rtyp = Universal_Integer
7726 then
7727 Etyp := Standard_Long_Long_Integer;
7729 -- Overflow checking is the only choice on the AAMP target, where
7730 -- arithmetic instructions check overflow automatically, so only
7731 -- one version of the exponentiation unit is needed.
7733 if Ovflo or AAMP_On_Target then
7734 Rent := RE_Exp_Long_Long_Integer;
7735 else
7736 Rent := RE_Exn_Long_Long_Integer;
7737 end if;
7739 elsif Is_Signed_Integer_Type (Rtyp) then
7740 Etyp := Standard_Integer;
7742 -- Overflow checking is the only choice on the AAMP target, where
7743 -- arithmetic instructions check overflow automatically, so only
7744 -- one version of the exponentiation unit is needed.
7746 if Ovflo or AAMP_On_Target then
7747 Rent := RE_Exp_Integer;
7748 else
7749 Rent := RE_Exn_Integer;
7750 end if;
7752 -- Floating-point cases, always done using Long_Long_Float. We do not
7753 -- need separate routines for the overflow case here, since in the case
7754 -- of floating-point, we generate infinities anyway as a rule (either
7755 -- that or we automatically trap overflow), and if there is an infinity
7756 -- generated and a range check is required, the check will fail anyway.
7758 else
7759 pragma Assert (Is_Floating_Point_Type (Rtyp));
7760 Etyp := Standard_Long_Long_Float;
7761 Rent := RE_Exn_Long_Long_Float;
7762 end if;
7764 -- Common processing for integer cases and floating-point cases.
7765 -- If we are in the right type, we can call runtime routine directly
7767 if Typ = Etyp
7768 and then Rtyp /= Universal_Integer
7769 and then Rtyp /= Universal_Real
7770 then
7771 Rewrite (N,
7772 Make_Function_Call (Loc,
7773 Name => New_Occurrence_Of (RTE (Rent), Loc),
7774 Parameter_Associations => New_List (Base, Exp)));
7776 -- Otherwise we have to introduce conversions (conversions are also
7777 -- required in the universal cases, since the runtime routine is
7778 -- typed using one of the standard types).
7780 else
7781 Rewrite (N,
7782 Convert_To (Typ,
7783 Make_Function_Call (Loc,
7784 Name => New_Occurrence_Of (RTE (Rent), Loc),
7785 Parameter_Associations => New_List (
7786 Convert_To (Etyp, Base),
7787 Exp))));
7788 end if;
7790 Analyze_And_Resolve (N, Typ);
7791 return;
7793 exception
7794 when RE_Not_Available =>
7795 return;
7796 end Expand_N_Op_Expon;
7798 --------------------
7799 -- Expand_N_Op_Ge --
7800 --------------------
7802 procedure Expand_N_Op_Ge (N : Node_Id) is
7803 Typ : constant Entity_Id := Etype (N);
7804 Op1 : constant Node_Id := Left_Opnd (N);
7805 Op2 : constant Node_Id := Right_Opnd (N);
7806 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7808 begin
7809 Binary_Op_Validity_Checks (N);
7811 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7812 -- means we no longer have a comparison operation, we are all done.
7814 Expand_Compare_Minimize_Eliminate_Overflow (N);
7816 if Nkind (N) /= N_Op_Ge then
7817 return;
7818 end if;
7820 -- Array type case
7822 if Is_Array_Type (Typ1) then
7823 Expand_Array_Comparison (N);
7824 return;
7825 end if;
7827 -- Deal with boolean operands
7829 if Is_Boolean_Type (Typ1) then
7830 Adjust_Condition (Op1);
7831 Adjust_Condition (Op2);
7832 Set_Etype (N, Standard_Boolean);
7833 Adjust_Result_Type (N, Typ);
7834 end if;
7836 Rewrite_Comparison (N);
7838 Optimize_Length_Comparison (N);
7839 end Expand_N_Op_Ge;
7841 --------------------
7842 -- Expand_N_Op_Gt --
7843 --------------------
7845 procedure Expand_N_Op_Gt (N : Node_Id) is
7846 Typ : constant Entity_Id := Etype (N);
7847 Op1 : constant Node_Id := Left_Opnd (N);
7848 Op2 : constant Node_Id := Right_Opnd (N);
7849 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7851 begin
7852 Binary_Op_Validity_Checks (N);
7854 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7855 -- means we no longer have a comparison operation, we are all done.
7857 Expand_Compare_Minimize_Eliminate_Overflow (N);
7859 if Nkind (N) /= N_Op_Gt then
7860 return;
7861 end if;
7863 -- Deal with array type operands
7865 if Is_Array_Type (Typ1) then
7866 Expand_Array_Comparison (N);
7867 return;
7868 end if;
7870 -- Deal with boolean type operands
7872 if Is_Boolean_Type (Typ1) then
7873 Adjust_Condition (Op1);
7874 Adjust_Condition (Op2);
7875 Set_Etype (N, Standard_Boolean);
7876 Adjust_Result_Type (N, Typ);
7877 end if;
7879 Rewrite_Comparison (N);
7881 Optimize_Length_Comparison (N);
7882 end Expand_N_Op_Gt;
7884 --------------------
7885 -- Expand_N_Op_Le --
7886 --------------------
7888 procedure Expand_N_Op_Le (N : Node_Id) is
7889 Typ : constant Entity_Id := Etype (N);
7890 Op1 : constant Node_Id := Left_Opnd (N);
7891 Op2 : constant Node_Id := Right_Opnd (N);
7892 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7894 begin
7895 Binary_Op_Validity_Checks (N);
7897 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7898 -- means we no longer have a comparison operation, we are all done.
7900 Expand_Compare_Minimize_Eliminate_Overflow (N);
7902 if Nkind (N) /= N_Op_Le then
7903 return;
7904 end if;
7906 -- Deal with array type operands
7908 if Is_Array_Type (Typ1) then
7909 Expand_Array_Comparison (N);
7910 return;
7911 end if;
7913 -- Deal with Boolean type operands
7915 if Is_Boolean_Type (Typ1) then
7916 Adjust_Condition (Op1);
7917 Adjust_Condition (Op2);
7918 Set_Etype (N, Standard_Boolean);
7919 Adjust_Result_Type (N, Typ);
7920 end if;
7922 Rewrite_Comparison (N);
7924 Optimize_Length_Comparison (N);
7925 end Expand_N_Op_Le;
7927 --------------------
7928 -- Expand_N_Op_Lt --
7929 --------------------
7931 procedure Expand_N_Op_Lt (N : Node_Id) is
7932 Typ : constant Entity_Id := Etype (N);
7933 Op1 : constant Node_Id := Left_Opnd (N);
7934 Op2 : constant Node_Id := Right_Opnd (N);
7935 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7937 begin
7938 Binary_Op_Validity_Checks (N);
7940 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7941 -- means we no longer have a comparison operation, we are all done.
7943 Expand_Compare_Minimize_Eliminate_Overflow (N);
7945 if Nkind (N) /= N_Op_Lt then
7946 return;
7947 end if;
7949 -- Deal with array type operands
7951 if Is_Array_Type (Typ1) then
7952 Expand_Array_Comparison (N);
7953 return;
7954 end if;
7956 -- Deal with Boolean type operands
7958 if Is_Boolean_Type (Typ1) then
7959 Adjust_Condition (Op1);
7960 Adjust_Condition (Op2);
7961 Set_Etype (N, Standard_Boolean);
7962 Adjust_Result_Type (N, Typ);
7963 end if;
7965 Rewrite_Comparison (N);
7967 Optimize_Length_Comparison (N);
7968 end Expand_N_Op_Lt;
7970 -----------------------
7971 -- Expand_N_Op_Minus --
7972 -----------------------
7974 procedure Expand_N_Op_Minus (N : Node_Id) is
7975 Loc : constant Source_Ptr := Sloc (N);
7976 Typ : constant Entity_Id := Etype (N);
7978 begin
7979 Unary_Op_Validity_Checks (N);
7981 -- Check for MINIMIZED/ELIMINATED overflow mode
7983 if Minimized_Eliminated_Overflow_Check (N) then
7984 Apply_Arithmetic_Overflow_Check (N);
7985 return;
7986 end if;
7988 if not Backend_Overflow_Checks_On_Target
7989 and then Is_Signed_Integer_Type (Etype (N))
7990 and then Do_Overflow_Check (N)
7991 then
7992 -- Software overflow checking expands -expr into (0 - expr)
7994 Rewrite (N,
7995 Make_Op_Subtract (Loc,
7996 Left_Opnd => Make_Integer_Literal (Loc, 0),
7997 Right_Opnd => Right_Opnd (N)));
7999 Analyze_And_Resolve (N, Typ);
8000 end if;
8001 end Expand_N_Op_Minus;
8003 ---------------------
8004 -- Expand_N_Op_Mod --
8005 ---------------------
8007 procedure Expand_N_Op_Mod (N : Node_Id) is
8008 Loc : constant Source_Ptr := Sloc (N);
8009 Typ : constant Entity_Id := Etype (N);
8010 DDC : constant Boolean := Do_Division_Check (N);
8012 Left : Node_Id;
8013 Right : Node_Id;
8015 LLB : Uint;
8016 Llo : Uint;
8017 Lhi : Uint;
8018 LOK : Boolean;
8019 Rlo : Uint;
8020 Rhi : Uint;
8021 ROK : Boolean;
8023 pragma Warnings (Off, Lhi);
8025 begin
8026 Binary_Op_Validity_Checks (N);
8028 -- Check for MINIMIZED/ELIMINATED overflow mode
8030 if Minimized_Eliminated_Overflow_Check (N) then
8031 Apply_Arithmetic_Overflow_Check (N);
8032 return;
8033 end if;
8035 if Is_Integer_Type (Etype (N)) then
8036 Apply_Divide_Checks (N);
8038 -- All done if we don't have a MOD any more, which can happen as a
8039 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8041 if Nkind (N) /= N_Op_Mod then
8042 return;
8043 end if;
8044 end if;
8046 -- Proceed with expansion of mod operator
8048 Left := Left_Opnd (N);
8049 Right := Right_Opnd (N);
8051 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
8052 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
8054 -- Convert mod to rem if operands are both known to be non-negative, or
8055 -- both known to be non-positive (these are the cases in which rem and
8056 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
8057 -- likely that this will improve the quality of code, (the operation now
8058 -- corresponds to the hardware remainder), and it does not seem likely
8059 -- that it could be harmful. It also avoids some cases of the elaborate
8060 -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %).
8062 if (LOK and ROK)
8063 and then ((Llo >= 0 and then Rlo >= 0)
8064 or else
8065 (Lhi <= 0 and then Rhi <= 0))
8066 then
8067 Rewrite (N,
8068 Make_Op_Rem (Sloc (N),
8069 Left_Opnd => Left_Opnd (N),
8070 Right_Opnd => Right_Opnd (N)));
8072 -- Instead of reanalyzing the node we do the analysis manually. This
8073 -- avoids anomalies when the replacement is done in an instance and
8074 -- is epsilon more efficient.
8076 Set_Entity (N, Standard_Entity (S_Op_Rem));
8077 Set_Etype (N, Typ);
8078 Set_Do_Division_Check (N, DDC);
8079 Expand_N_Op_Rem (N);
8080 Set_Analyzed (N);
8081 return;
8083 -- Otherwise, normal mod processing
8085 else
8086 -- Apply optimization x mod 1 = 0. We don't really need that with
8087 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
8088 -- certainly harmless.
8090 if Is_Integer_Type (Etype (N))
8091 and then Compile_Time_Known_Value (Right)
8092 and then Expr_Value (Right) = Uint_1
8093 then
8094 -- Call Remove_Side_Effects to ensure that any side effects in
8095 -- the ignored left operand (in particular function calls to
8096 -- user defined functions) are properly executed.
8098 Remove_Side_Effects (Left);
8100 Rewrite (N, Make_Integer_Literal (Loc, 0));
8101 Analyze_And_Resolve (N, Typ);
8102 return;
8103 end if;
8105 -- If we still have a mod operator and we are in Modify_Tree_For_C
8106 -- mode, and we have a signed integer type, then here is where we do
8107 -- the rewrite in terms of Rem. Note this rewrite bypasses the need
8108 -- for the special handling of the annoying case of largest negative
8109 -- number mod minus one.
8111 if Nkind (N) = N_Op_Mod
8112 and then Is_Signed_Integer_Type (Typ)
8113 and then Modify_Tree_For_C
8114 then
8115 -- In the general case, we expand A mod B as
8117 -- Tnn : constant typ := A rem B;
8118 -- ..
8119 -- (if (A >= 0) = (B >= 0) then Tnn
8120 -- elsif Tnn = 0 then 0
8121 -- else Tnn + B)
8123 -- The comparison can be written simply as A >= 0 if we know that
8124 -- B >= 0 which is a very common case.
8126 -- An important optimization is when B is known at compile time
8127 -- to be 2**K for some constant. In this case we can simply AND
8128 -- the left operand with the bit string 2**K-1 (i.e. K 1-bits)
8129 -- and that works for both the positive and negative cases.
8131 declare
8132 P2 : constant Nat := Power_Of_Two (Right);
8134 begin
8135 if P2 /= 0 then
8136 Rewrite (N,
8137 Unchecked_Convert_To (Typ,
8138 Make_Op_And (Loc,
8139 Left_Opnd =>
8140 Unchecked_Convert_To
8141 (Corresponding_Unsigned_Type (Typ), Left),
8142 Right_Opnd =>
8143 Make_Integer_Literal (Loc, 2 ** P2 - 1))));
8144 Analyze_And_Resolve (N, Typ);
8145 return;
8146 end if;
8147 end;
8149 -- Here for the full rewrite
8151 declare
8152 Tnn : constant Entity_Id := Make_Temporary (Sloc (N), 'T', N);
8153 Cmp : Node_Id;
8155 begin
8156 Cmp :=
8157 Make_Op_Ge (Loc,
8158 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
8159 Right_Opnd => Make_Integer_Literal (Loc, 0));
8161 if not LOK or else Rlo < 0 then
8162 Cmp :=
8163 Make_Op_Eq (Loc,
8164 Left_Opnd => Cmp,
8165 Right_Opnd =>
8166 Make_Op_Ge (Loc,
8167 Left_Opnd => Duplicate_Subexpr_No_Checks (Right),
8168 Right_Opnd => Make_Integer_Literal (Loc, 0)));
8169 end if;
8171 Insert_Action (N,
8172 Make_Object_Declaration (Loc,
8173 Defining_Identifier => Tnn,
8174 Constant_Present => True,
8175 Object_Definition => New_Occurrence_Of (Typ, Loc),
8176 Expression =>
8177 Make_Op_Rem (Loc,
8178 Left_Opnd => Left,
8179 Right_Opnd => Right)));
8181 Rewrite (N,
8182 Make_If_Expression (Loc,
8183 Expressions => New_List (
8184 Cmp,
8185 New_Occurrence_Of (Tnn, Loc),
8186 Make_If_Expression (Loc,
8187 Is_Elsif => True,
8188 Expressions => New_List (
8189 Make_Op_Eq (Loc,
8190 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8191 Right_Opnd => Make_Integer_Literal (Loc, 0)),
8192 Make_Integer_Literal (Loc, 0),
8193 Make_Op_Add (Loc,
8194 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8195 Right_Opnd =>
8196 Duplicate_Subexpr_No_Checks (Right)))))));
8198 Analyze_And_Resolve (N, Typ);
8199 return;
8200 end;
8201 end if;
8203 -- Deal with annoying case of largest negative number mod minus one.
8204 -- Gigi may not handle this case correctly, because on some targets,
8205 -- the mod value is computed using a divide instruction which gives
8206 -- an overflow trap for this case.
8208 -- It would be a bit more efficient to figure out which targets
8209 -- this is really needed for, but in practice it is reasonable
8210 -- to do the following special check in all cases, since it means
8211 -- we get a clearer message, and also the overhead is minimal given
8212 -- that division is expensive in any case.
8214 -- In fact the check is quite easy, if the right operand is -1, then
8215 -- the mod value is always 0, and we can just ignore the left operand
8216 -- completely in this case.
8218 -- This only applies if we still have a mod operator. Skip if we
8219 -- have already rewritten this (e.g. in the case of eliminated
8220 -- overflow checks which have driven us into bignum mode).
8222 if Nkind (N) = N_Op_Mod then
8224 -- The operand type may be private (e.g. in the expansion of an
8225 -- intrinsic operation) so we must use the underlying type to get
8226 -- the bounds, and convert the literals explicitly.
8228 LLB :=
8229 Expr_Value
8230 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
8232 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
8233 and then ((not LOK) or else (Llo = LLB))
8234 then
8235 Rewrite (N,
8236 Make_If_Expression (Loc,
8237 Expressions => New_List (
8238 Make_Op_Eq (Loc,
8239 Left_Opnd => Duplicate_Subexpr (Right),
8240 Right_Opnd =>
8241 Unchecked_Convert_To (Typ,
8242 Make_Integer_Literal (Loc, -1))),
8243 Unchecked_Convert_To (Typ,
8244 Make_Integer_Literal (Loc, Uint_0)),
8245 Relocate_Node (N))));
8247 Set_Analyzed (Next (Next (First (Expressions (N)))));
8248 Analyze_And_Resolve (N, Typ);
8249 end if;
8250 end if;
8251 end if;
8252 end Expand_N_Op_Mod;
8254 --------------------------
8255 -- Expand_N_Op_Multiply --
8256 --------------------------
8258 procedure Expand_N_Op_Multiply (N : Node_Id) is
8259 Loc : constant Source_Ptr := Sloc (N);
8260 Lop : constant Node_Id := Left_Opnd (N);
8261 Rop : constant Node_Id := Right_Opnd (N);
8263 Lp2 : constant Boolean :=
8264 Nkind (Lop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Lop);
8265 Rp2 : constant Boolean :=
8266 Nkind (Rop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Rop);
8268 Ltyp : constant Entity_Id := Etype (Lop);
8269 Rtyp : constant Entity_Id := Etype (Rop);
8270 Typ : Entity_Id := Etype (N);
8272 begin
8273 Binary_Op_Validity_Checks (N);
8275 -- Check for MINIMIZED/ELIMINATED overflow mode
8277 if Minimized_Eliminated_Overflow_Check (N) then
8278 Apply_Arithmetic_Overflow_Check (N);
8279 return;
8280 end if;
8282 -- Special optimizations for integer types
8284 if Is_Integer_Type (Typ) then
8286 -- N * 0 = 0 for integer types
8288 if Compile_Time_Known_Value (Rop)
8289 and then Expr_Value (Rop) = Uint_0
8290 then
8291 -- Call Remove_Side_Effects to ensure that any side effects in
8292 -- the ignored left operand (in particular function calls to
8293 -- user defined functions) are properly executed.
8295 Remove_Side_Effects (Lop);
8297 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8298 Analyze_And_Resolve (N, Typ);
8299 return;
8300 end if;
8302 -- Similar handling for 0 * N = 0
8304 if Compile_Time_Known_Value (Lop)
8305 and then Expr_Value (Lop) = Uint_0
8306 then
8307 Remove_Side_Effects (Rop);
8308 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8309 Analyze_And_Resolve (N, Typ);
8310 return;
8311 end if;
8313 -- N * 1 = 1 * N = N for integer types
8315 -- This optimisation is not done if we are going to
8316 -- rewrite the product 1 * 2 ** N to a shift.
8318 if Compile_Time_Known_Value (Rop)
8319 and then Expr_Value (Rop) = Uint_1
8320 and then not Lp2
8321 then
8322 Rewrite (N, Lop);
8323 return;
8325 elsif Compile_Time_Known_Value (Lop)
8326 and then Expr_Value (Lop) = Uint_1
8327 and then not Rp2
8328 then
8329 Rewrite (N, Rop);
8330 return;
8331 end if;
8332 end if;
8334 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
8335 -- Is_Power_Of_2_For_Shift is set means that we know that our left
8336 -- operand is an integer, as required for this to work.
8338 if Rp2 then
8339 if Lp2 then
8341 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
8343 Rewrite (N,
8344 Make_Op_Expon (Loc,
8345 Left_Opnd => Make_Integer_Literal (Loc, 2),
8346 Right_Opnd =>
8347 Make_Op_Add (Loc,
8348 Left_Opnd => Right_Opnd (Lop),
8349 Right_Opnd => Right_Opnd (Rop))));
8350 Analyze_And_Resolve (N, Typ);
8351 return;
8353 else
8354 -- If the result is modular, perform the reduction of the result
8355 -- appropriately.
8357 if Is_Modular_Integer_Type (Typ)
8358 and then not Non_Binary_Modulus (Typ)
8359 then
8360 Rewrite (N,
8361 Make_Op_And (Loc,
8362 Left_Opnd =>
8363 Make_Op_Shift_Left (Loc,
8364 Left_Opnd => Lop,
8365 Right_Opnd =>
8366 Convert_To (Standard_Natural, Right_Opnd (Rop))),
8367 Right_Opnd =>
8368 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
8370 else
8371 Rewrite (N,
8372 Make_Op_Shift_Left (Loc,
8373 Left_Opnd => Lop,
8374 Right_Opnd =>
8375 Convert_To (Standard_Natural, Right_Opnd (Rop))));
8376 end if;
8378 Analyze_And_Resolve (N, Typ);
8379 return;
8380 end if;
8382 -- Same processing for the operands the other way round
8384 elsif Lp2 then
8385 if Is_Modular_Integer_Type (Typ)
8386 and then not Non_Binary_Modulus (Typ)
8387 then
8388 Rewrite (N,
8389 Make_Op_And (Loc,
8390 Left_Opnd =>
8391 Make_Op_Shift_Left (Loc,
8392 Left_Opnd => Rop,
8393 Right_Opnd =>
8394 Convert_To (Standard_Natural, Right_Opnd (Lop))),
8395 Right_Opnd =>
8396 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
8398 else
8399 Rewrite (N,
8400 Make_Op_Shift_Left (Loc,
8401 Left_Opnd => Rop,
8402 Right_Opnd =>
8403 Convert_To (Standard_Natural, Right_Opnd (Lop))));
8404 end if;
8406 Analyze_And_Resolve (N, Typ);
8407 return;
8408 end if;
8410 -- Do required fixup of universal fixed operation
8412 if Typ = Universal_Fixed then
8413 Fixup_Universal_Fixed_Operation (N);
8414 Typ := Etype (N);
8415 end if;
8417 -- Multiplications with fixed-point results
8419 if Is_Fixed_Point_Type (Typ) then
8421 -- No special processing if Treat_Fixed_As_Integer is set, since from
8422 -- a semantic point of view such operations are simply integer
8423 -- operations and will be treated that way.
8425 if not Treat_Fixed_As_Integer (N) then
8427 -- Case of fixed * integer => fixed
8429 if Is_Integer_Type (Rtyp) then
8430 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
8432 -- Case of integer * fixed => fixed
8434 elsif Is_Integer_Type (Ltyp) then
8435 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
8437 -- Case of fixed * fixed => fixed
8439 else
8440 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
8441 end if;
8442 end if;
8444 -- Other cases of multiplication of fixed-point operands. Again we
8445 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
8447 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
8448 and then not Treat_Fixed_As_Integer (N)
8449 then
8450 if Is_Integer_Type (Typ) then
8451 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
8452 else
8453 pragma Assert (Is_Floating_Point_Type (Typ));
8454 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
8455 end if;
8457 -- Mixed-mode operations can appear in a non-static universal context,
8458 -- in which case the integer argument must be converted explicitly.
8460 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
8461 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
8462 Analyze_And_Resolve (Rop, Universal_Real);
8464 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
8465 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
8466 Analyze_And_Resolve (Lop, Universal_Real);
8468 -- Non-fixed point cases, check software overflow checking required
8470 elsif Is_Signed_Integer_Type (Etype (N)) then
8471 Apply_Arithmetic_Overflow_Check (N);
8472 end if;
8474 -- Overflow checks for floating-point if -gnateF mode active
8476 Check_Float_Op_Overflow (N);
8477 end Expand_N_Op_Multiply;
8479 --------------------
8480 -- Expand_N_Op_Ne --
8481 --------------------
8483 procedure Expand_N_Op_Ne (N : Node_Id) is
8484 Typ : constant Entity_Id := Etype (Left_Opnd (N));
8486 begin
8487 -- Case of elementary type with standard operator
8489 if Is_Elementary_Type (Typ)
8490 and then Sloc (Entity (N)) = Standard_Location
8491 then
8492 Binary_Op_Validity_Checks (N);
8494 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
8495 -- means we no longer have a /= operation, we are all done.
8497 Expand_Compare_Minimize_Eliminate_Overflow (N);
8499 if Nkind (N) /= N_Op_Ne then
8500 return;
8501 end if;
8503 -- Boolean types (requiring handling of non-standard case)
8505 if Is_Boolean_Type (Typ) then
8506 Adjust_Condition (Left_Opnd (N));
8507 Adjust_Condition (Right_Opnd (N));
8508 Set_Etype (N, Standard_Boolean);
8509 Adjust_Result_Type (N, Typ);
8510 end if;
8512 Rewrite_Comparison (N);
8514 -- For all cases other than elementary types, we rewrite node as the
8515 -- negation of an equality operation, and reanalyze. The equality to be
8516 -- used is defined in the same scope and has the same signature. This
8517 -- signature must be set explicitly since in an instance it may not have
8518 -- the same visibility as in the generic unit. This avoids duplicating
8519 -- or factoring the complex code for record/array equality tests etc.
8521 else
8522 declare
8523 Loc : constant Source_Ptr := Sloc (N);
8524 Neg : Node_Id;
8525 Ne : constant Entity_Id := Entity (N);
8527 begin
8528 Binary_Op_Validity_Checks (N);
8530 Neg :=
8531 Make_Op_Not (Loc,
8532 Right_Opnd =>
8533 Make_Op_Eq (Loc,
8534 Left_Opnd => Left_Opnd (N),
8535 Right_Opnd => Right_Opnd (N)));
8536 Set_Paren_Count (Right_Opnd (Neg), 1);
8538 if Scope (Ne) /= Standard_Standard then
8539 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
8540 end if;
8542 -- For navigation purposes, we want to treat the inequality as an
8543 -- implicit reference to the corresponding equality. Preserve the
8544 -- Comes_From_ source flag to generate proper Xref entries.
8546 Preserve_Comes_From_Source (Neg, N);
8547 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
8548 Rewrite (N, Neg);
8549 Analyze_And_Resolve (N, Standard_Boolean);
8550 end;
8551 end if;
8553 Optimize_Length_Comparison (N);
8554 end Expand_N_Op_Ne;
8556 ---------------------
8557 -- Expand_N_Op_Not --
8558 ---------------------
8560 -- If the argument is other than a Boolean array type, there is no special
8561 -- expansion required, except for dealing with validity checks, and non-
8562 -- standard boolean representations.
8564 -- For the packed array case, we call the special routine in Exp_Pakd,
8565 -- except that if the component size is greater than one, we use the
8566 -- standard routine generating a gruesome loop (it is so peculiar to have
8567 -- packed arrays with non-standard Boolean representations anyway, so it
8568 -- does not matter that we do not handle this case efficiently).
8570 -- For the unpacked array case (and for the special packed case where we
8571 -- have non standard Booleans, as discussed above), we generate and insert
8572 -- into the tree the following function definition:
8574 -- function Nnnn (A : arr) is
8575 -- B : arr;
8576 -- begin
8577 -- for J in a'range loop
8578 -- B (J) := not A (J);
8579 -- end loop;
8580 -- return B;
8581 -- end Nnnn;
8583 -- Here arr is the actual subtype of the parameter (and hence always
8584 -- constrained). Then we replace the not with a call to this function.
8586 procedure Expand_N_Op_Not (N : Node_Id) is
8587 Loc : constant Source_Ptr := Sloc (N);
8588 Typ : constant Entity_Id := Etype (N);
8589 Opnd : Node_Id;
8590 Arr : Entity_Id;
8591 A : Entity_Id;
8592 B : Entity_Id;
8593 J : Entity_Id;
8594 A_J : Node_Id;
8595 B_J : Node_Id;
8597 Func_Name : Entity_Id;
8598 Loop_Statement : Node_Id;
8600 begin
8601 Unary_Op_Validity_Checks (N);
8603 -- For boolean operand, deal with non-standard booleans
8605 if Is_Boolean_Type (Typ) then
8606 Adjust_Condition (Right_Opnd (N));
8607 Set_Etype (N, Standard_Boolean);
8608 Adjust_Result_Type (N, Typ);
8609 return;
8610 end if;
8612 -- Only array types need any other processing
8614 if not Is_Array_Type (Typ) then
8615 return;
8616 end if;
8618 -- Case of array operand. If bit packed with a component size of 1,
8619 -- handle it in Exp_Pakd if the operand is known to be aligned.
8621 if Is_Bit_Packed_Array (Typ)
8622 and then Component_Size (Typ) = 1
8623 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
8624 then
8625 Expand_Packed_Not (N);
8626 return;
8627 end if;
8629 -- Case of array operand which is not bit-packed. If the context is
8630 -- a safe assignment, call in-place operation, If context is a larger
8631 -- boolean expression in the context of a safe assignment, expansion is
8632 -- done by enclosing operation.
8634 Opnd := Relocate_Node (Right_Opnd (N));
8635 Convert_To_Actual_Subtype (Opnd);
8636 Arr := Etype (Opnd);
8637 Ensure_Defined (Arr, N);
8638 Silly_Boolean_Array_Not_Test (N, Arr);
8640 if Nkind (Parent (N)) = N_Assignment_Statement then
8641 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
8642 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8643 return;
8645 -- Special case the negation of a binary operation
8647 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
8648 and then Safe_In_Place_Array_Op
8649 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
8650 then
8651 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8652 return;
8653 end if;
8655 elsif Nkind (Parent (N)) in N_Binary_Op
8656 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
8657 then
8658 declare
8659 Op1 : constant Node_Id := Left_Opnd (Parent (N));
8660 Op2 : constant Node_Id := Right_Opnd (Parent (N));
8661 Lhs : constant Node_Id := Name (Parent (Parent (N)));
8663 begin
8664 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
8666 -- (not A) op (not B) can be reduced to a single call
8668 if N = Op1 and then Nkind (Op2) = N_Op_Not then
8669 return;
8671 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
8672 return;
8674 -- A xor (not B) can also be special-cased
8676 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
8677 return;
8678 end if;
8679 end if;
8680 end;
8681 end if;
8683 A := Make_Defining_Identifier (Loc, Name_uA);
8684 B := Make_Defining_Identifier (Loc, Name_uB);
8685 J := Make_Defining_Identifier (Loc, Name_uJ);
8687 A_J :=
8688 Make_Indexed_Component (Loc,
8689 Prefix => New_Occurrence_Of (A, Loc),
8690 Expressions => New_List (New_Occurrence_Of (J, Loc)));
8692 B_J :=
8693 Make_Indexed_Component (Loc,
8694 Prefix => New_Occurrence_Of (B, Loc),
8695 Expressions => New_List (New_Occurrence_Of (J, Loc)));
8697 Loop_Statement :=
8698 Make_Implicit_Loop_Statement (N,
8699 Identifier => Empty,
8701 Iteration_Scheme =>
8702 Make_Iteration_Scheme (Loc,
8703 Loop_Parameter_Specification =>
8704 Make_Loop_Parameter_Specification (Loc,
8705 Defining_Identifier => J,
8706 Discrete_Subtype_Definition =>
8707 Make_Attribute_Reference (Loc,
8708 Prefix => Make_Identifier (Loc, Chars (A)),
8709 Attribute_Name => Name_Range))),
8711 Statements => New_List (
8712 Make_Assignment_Statement (Loc,
8713 Name => B_J,
8714 Expression => Make_Op_Not (Loc, A_J))));
8716 Func_Name := Make_Temporary (Loc, 'N');
8717 Set_Is_Inlined (Func_Name);
8719 Insert_Action (N,
8720 Make_Subprogram_Body (Loc,
8721 Specification =>
8722 Make_Function_Specification (Loc,
8723 Defining_Unit_Name => Func_Name,
8724 Parameter_Specifications => New_List (
8725 Make_Parameter_Specification (Loc,
8726 Defining_Identifier => A,
8727 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8728 Result_Definition => New_Occurrence_Of (Typ, Loc)),
8730 Declarations => New_List (
8731 Make_Object_Declaration (Loc,
8732 Defining_Identifier => B,
8733 Object_Definition => New_Occurrence_Of (Arr, Loc))),
8735 Handled_Statement_Sequence =>
8736 Make_Handled_Sequence_Of_Statements (Loc,
8737 Statements => New_List (
8738 Loop_Statement,
8739 Make_Simple_Return_Statement (Loc,
8740 Expression => Make_Identifier (Loc, Chars (B)))))));
8742 Rewrite (N,
8743 Make_Function_Call (Loc,
8744 Name => New_Occurrence_Of (Func_Name, Loc),
8745 Parameter_Associations => New_List (Opnd)));
8747 Analyze_And_Resolve (N, Typ);
8748 end Expand_N_Op_Not;
8750 --------------------
8751 -- Expand_N_Op_Or --
8752 --------------------
8754 procedure Expand_N_Op_Or (N : Node_Id) is
8755 Typ : constant Entity_Id := Etype (N);
8757 begin
8758 Binary_Op_Validity_Checks (N);
8760 if Is_Array_Type (Etype (N)) then
8761 Expand_Boolean_Operator (N);
8763 elsif Is_Boolean_Type (Etype (N)) then
8764 Adjust_Condition (Left_Opnd (N));
8765 Adjust_Condition (Right_Opnd (N));
8766 Set_Etype (N, Standard_Boolean);
8767 Adjust_Result_Type (N, Typ);
8769 elsif Is_Intrinsic_Subprogram (Entity (N)) then
8770 Expand_Intrinsic_Call (N, Entity (N));
8772 end if;
8773 end Expand_N_Op_Or;
8775 ----------------------
8776 -- Expand_N_Op_Plus --
8777 ----------------------
8779 procedure Expand_N_Op_Plus (N : Node_Id) is
8780 begin
8781 Unary_Op_Validity_Checks (N);
8783 -- Check for MINIMIZED/ELIMINATED overflow mode
8785 if Minimized_Eliminated_Overflow_Check (N) then
8786 Apply_Arithmetic_Overflow_Check (N);
8787 return;
8788 end if;
8789 end Expand_N_Op_Plus;
8791 ---------------------
8792 -- Expand_N_Op_Rem --
8793 ---------------------
8795 procedure Expand_N_Op_Rem (N : Node_Id) is
8796 Loc : constant Source_Ptr := Sloc (N);
8797 Typ : constant Entity_Id := Etype (N);
8799 Left : Node_Id;
8800 Right : Node_Id;
8802 Lo : Uint;
8803 Hi : Uint;
8804 OK : Boolean;
8806 Lneg : Boolean;
8807 Rneg : Boolean;
8808 -- Set if corresponding operand can be negative
8810 pragma Unreferenced (Hi);
8812 begin
8813 Binary_Op_Validity_Checks (N);
8815 -- Check for MINIMIZED/ELIMINATED overflow mode
8817 if Minimized_Eliminated_Overflow_Check (N) then
8818 Apply_Arithmetic_Overflow_Check (N);
8819 return;
8820 end if;
8822 if Is_Integer_Type (Etype (N)) then
8823 Apply_Divide_Checks (N);
8825 -- All done if we don't have a REM any more, which can happen as a
8826 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8828 if Nkind (N) /= N_Op_Rem then
8829 return;
8830 end if;
8831 end if;
8833 -- Proceed with expansion of REM
8835 Left := Left_Opnd (N);
8836 Right := Right_Opnd (N);
8838 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
8839 -- but it is useful with other back ends (e.g. AAMP), and is certainly
8840 -- harmless.
8842 if Is_Integer_Type (Etype (N))
8843 and then Compile_Time_Known_Value (Right)
8844 and then Expr_Value (Right) = Uint_1
8845 then
8846 -- Call Remove_Side_Effects to ensure that any side effects in the
8847 -- ignored left operand (in particular function calls to user defined
8848 -- functions) are properly executed.
8850 Remove_Side_Effects (Left);
8852 Rewrite (N, Make_Integer_Literal (Loc, 0));
8853 Analyze_And_Resolve (N, Typ);
8854 return;
8855 end if;
8857 -- Deal with annoying case of largest negative number remainder minus
8858 -- one. Gigi may not handle this case correctly, because on some
8859 -- targets, the mod value is computed using a divide instruction
8860 -- which gives an overflow trap for this case.
8862 -- It would be a bit more efficient to figure out which targets this
8863 -- is really needed for, but in practice it is reasonable to do the
8864 -- following special check in all cases, since it means we get a clearer
8865 -- message, and also the overhead is minimal given that division is
8866 -- expensive in any case.
8868 -- In fact the check is quite easy, if the right operand is -1, then
8869 -- the remainder is always 0, and we can just ignore the left operand
8870 -- completely in this case.
8872 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
8873 Lneg := (not OK) or else Lo < 0;
8875 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
8876 Rneg := (not OK) or else Lo < 0;
8878 -- We won't mess with trying to find out if the left operand can really
8879 -- be the largest negative number (that's a pain in the case of private
8880 -- types and this is really marginal). We will just assume that we need
8881 -- the test if the left operand can be negative at all.
8883 if Lneg and Rneg then
8884 Rewrite (N,
8885 Make_If_Expression (Loc,
8886 Expressions => New_List (
8887 Make_Op_Eq (Loc,
8888 Left_Opnd => Duplicate_Subexpr (Right),
8889 Right_Opnd =>
8890 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
8892 Unchecked_Convert_To (Typ,
8893 Make_Integer_Literal (Loc, Uint_0)),
8895 Relocate_Node (N))));
8897 Set_Analyzed (Next (Next (First (Expressions (N)))));
8898 Analyze_And_Resolve (N, Typ);
8899 end if;
8900 end Expand_N_Op_Rem;
8902 -----------------------------
8903 -- Expand_N_Op_Rotate_Left --
8904 -----------------------------
8906 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
8907 begin
8908 Binary_Op_Validity_Checks (N);
8910 -- If we are in Modify_Tree_For_C mode, there is no rotate left in C,
8911 -- so we rewrite in terms of logical shifts
8913 -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits)
8915 -- where Bits is the shift count mod Esize (the mod operation here
8916 -- deals with ludicrous large shift counts, which are apparently OK).
8918 -- What about non-binary modulus ???
8920 declare
8921 Loc : constant Source_Ptr := Sloc (N);
8922 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
8923 Typ : constant Entity_Id := Etype (N);
8925 begin
8926 if Modify_Tree_For_C then
8927 Rewrite (Right_Opnd (N),
8928 Make_Op_Rem (Loc,
8929 Left_Opnd => Relocate_Node (Right_Opnd (N)),
8930 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
8932 Analyze_And_Resolve (Right_Opnd (N), Rtp);
8934 Rewrite (N,
8935 Make_Op_Or (Loc,
8936 Left_Opnd =>
8937 Make_Op_Shift_Left (Loc,
8938 Left_Opnd => Left_Opnd (N),
8939 Right_Opnd => Right_Opnd (N)),
8941 Right_Opnd =>
8942 Make_Op_Shift_Right (Loc,
8943 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
8944 Right_Opnd =>
8945 Make_Op_Subtract (Loc,
8946 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
8947 Right_Opnd =>
8948 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
8950 Analyze_And_Resolve (N, Typ);
8951 end if;
8952 end;
8953 end Expand_N_Op_Rotate_Left;
8955 ------------------------------
8956 -- Expand_N_Op_Rotate_Right --
8957 ------------------------------
8959 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
8960 begin
8961 Binary_Op_Validity_Checks (N);
8963 -- If we are in Modify_Tree_For_C mode, there is no rotate right in C,
8964 -- so we rewrite in terms of logical shifts
8966 -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits)
8968 -- where Bits is the shift count mod Esize (the mod operation here
8969 -- deals with ludicrous large shift counts, which are apparently OK).
8971 -- What about non-binary modulus ???
8973 declare
8974 Loc : constant Source_Ptr := Sloc (N);
8975 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
8976 Typ : constant Entity_Id := Etype (N);
8978 begin
8979 Rewrite (Right_Opnd (N),
8980 Make_Op_Rem (Loc,
8981 Left_Opnd => Relocate_Node (Right_Opnd (N)),
8982 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
8984 Analyze_And_Resolve (Right_Opnd (N), Rtp);
8986 if Modify_Tree_For_C then
8987 Rewrite (N,
8988 Make_Op_Or (Loc,
8989 Left_Opnd =>
8990 Make_Op_Shift_Right (Loc,
8991 Left_Opnd => Left_Opnd (N),
8992 Right_Opnd => Right_Opnd (N)),
8994 Right_Opnd =>
8995 Make_Op_Shift_Left (Loc,
8996 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
8997 Right_Opnd =>
8998 Make_Op_Subtract (Loc,
8999 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
9000 Right_Opnd =>
9001 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
9003 Analyze_And_Resolve (N, Typ);
9004 end if;
9005 end;
9006 end Expand_N_Op_Rotate_Right;
9008 ----------------------------
9009 -- Expand_N_Op_Shift_Left --
9010 ----------------------------
9012 -- Note: nothing in this routine depends on left as opposed to right shifts
9013 -- so we share the routine for expanding shift right operations.
9015 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
9016 begin
9017 Binary_Op_Validity_Checks (N);
9019 -- If we are in Modify_Tree_For_C mode, then ensure that the right
9020 -- operand is not greater than the word size (since that would not
9021 -- be defined properly by the corresponding C shift operator).
9023 if Modify_Tree_For_C then
9024 declare
9025 Right : constant Node_Id := Right_Opnd (N);
9026 Loc : constant Source_Ptr := Sloc (Right);
9027 Typ : constant Entity_Id := Etype (N);
9028 Siz : constant Uint := Esize (Typ);
9029 Orig : Node_Id;
9030 OK : Boolean;
9031 Lo : Uint;
9032 Hi : Uint;
9034 begin
9035 if Compile_Time_Known_Value (Right) then
9036 if Expr_Value (Right) >= Siz then
9037 Rewrite (N, Make_Integer_Literal (Loc, 0));
9038 Analyze_And_Resolve (N, Typ);
9039 end if;
9041 -- Not compile time known, find range
9043 else
9044 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
9046 -- Nothing to do if known to be OK range, otherwise expand
9048 if not OK or else Hi >= Siz then
9050 -- Prevent recursion on copy of shift node
9052 Orig := Relocate_Node (N);
9053 Set_Analyzed (Orig);
9055 -- Now do the rewrite
9057 Rewrite (N,
9058 Make_If_Expression (Loc,
9059 Expressions => New_List (
9060 Make_Op_Ge (Loc,
9061 Left_Opnd => Duplicate_Subexpr_Move_Checks (Right),
9062 Right_Opnd => Make_Integer_Literal (Loc, Siz)),
9063 Make_Integer_Literal (Loc, 0),
9064 Orig)));
9065 Analyze_And_Resolve (N, Typ);
9066 end if;
9067 end if;
9068 end;
9069 end if;
9070 end Expand_N_Op_Shift_Left;
9072 -----------------------------
9073 -- Expand_N_Op_Shift_Right --
9074 -----------------------------
9076 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
9077 begin
9078 -- Share shift left circuit
9080 Expand_N_Op_Shift_Left (N);
9081 end Expand_N_Op_Shift_Right;
9083 ----------------------------------------
9084 -- Expand_N_Op_Shift_Right_Arithmetic --
9085 ----------------------------------------
9087 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
9088 begin
9089 Binary_Op_Validity_Checks (N);
9091 -- If we are in Modify_Tree_For_C mode, there is no shift right
9092 -- arithmetic in C, so we rewrite in terms of logical shifts.
9094 -- Shift_Right (Num, Bits) or
9095 -- (if Num >= Sign
9096 -- then not (Shift_Right (Mask, bits))
9097 -- else 0)
9099 -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1)
9101 -- Note: in almost all C compilers it would work to just shift a
9102 -- signed integer right, but it's undefined and we cannot rely on it.
9104 -- Note: the above works fine for shift counts greater than or equal
9105 -- to the word size, since in this case (not (Shift_Right (Mask, bits)))
9106 -- generates all 1'bits.
9108 -- What about non-binary modulus ???
9110 declare
9111 Loc : constant Source_Ptr := Sloc (N);
9112 Typ : constant Entity_Id := Etype (N);
9113 Sign : constant Uint := 2 ** (Esize (Typ) - 1);
9114 Mask : constant Uint := (2 ** Esize (Typ)) - 1;
9115 Left : constant Node_Id := Left_Opnd (N);
9116 Right : constant Node_Id := Right_Opnd (N);
9117 Maskx : Node_Id;
9119 begin
9120 if Modify_Tree_For_C then
9122 -- Here if not (Shift_Right (Mask, bits)) can be computed at
9123 -- compile time as a single constant.
9125 if Compile_Time_Known_Value (Right) then
9126 declare
9127 Val : constant Uint := Expr_Value (Right);
9129 begin
9130 if Val >= Esize (Typ) then
9131 Maskx := Make_Integer_Literal (Loc, Mask);
9133 else
9134 Maskx :=
9135 Make_Integer_Literal (Loc,
9136 Intval => Mask - (Mask / (2 ** Expr_Value (Right))));
9137 end if;
9138 end;
9140 else
9141 Maskx :=
9142 Make_Op_Not (Loc,
9143 Right_Opnd =>
9144 Make_Op_Shift_Right (Loc,
9145 Left_Opnd => Make_Integer_Literal (Loc, Mask),
9146 Right_Opnd => Duplicate_Subexpr_No_Checks (Right)));
9147 end if;
9149 -- Now do the rewrite
9151 Rewrite (N,
9152 Make_Op_Or (Loc,
9153 Left_Opnd =>
9154 Make_Op_Shift_Right (Loc,
9155 Left_Opnd => Left,
9156 Right_Opnd => Right),
9157 Right_Opnd =>
9158 Make_If_Expression (Loc,
9159 Expressions => New_List (
9160 Make_Op_Ge (Loc,
9161 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
9162 Right_Opnd => Make_Integer_Literal (Loc, Sign)),
9163 Maskx,
9164 Make_Integer_Literal (Loc, 0)))));
9165 Analyze_And_Resolve (N, Typ);
9166 end if;
9167 end;
9168 end Expand_N_Op_Shift_Right_Arithmetic;
9170 --------------------------
9171 -- Expand_N_Op_Subtract --
9172 --------------------------
9174 procedure Expand_N_Op_Subtract (N : Node_Id) is
9175 Typ : constant Entity_Id := Etype (N);
9177 begin
9178 Binary_Op_Validity_Checks (N);
9180 -- Check for MINIMIZED/ELIMINATED overflow mode
9182 if Minimized_Eliminated_Overflow_Check (N) then
9183 Apply_Arithmetic_Overflow_Check (N);
9184 return;
9185 end if;
9187 -- N - 0 = N for integer types
9189 if Is_Integer_Type (Typ)
9190 and then Compile_Time_Known_Value (Right_Opnd (N))
9191 and then Expr_Value (Right_Opnd (N)) = 0
9192 then
9193 Rewrite (N, Left_Opnd (N));
9194 return;
9195 end if;
9197 -- Arithmetic overflow checks for signed integer/fixed point types
9199 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
9200 Apply_Arithmetic_Overflow_Check (N);
9201 end if;
9203 -- Overflow checks for floating-point if -gnateF mode active
9205 Check_Float_Op_Overflow (N);
9206 end Expand_N_Op_Subtract;
9208 ---------------------
9209 -- Expand_N_Op_Xor --
9210 ---------------------
9212 procedure Expand_N_Op_Xor (N : Node_Id) is
9213 Typ : constant Entity_Id := Etype (N);
9215 begin
9216 Binary_Op_Validity_Checks (N);
9218 if Is_Array_Type (Etype (N)) then
9219 Expand_Boolean_Operator (N);
9221 elsif Is_Boolean_Type (Etype (N)) then
9222 Adjust_Condition (Left_Opnd (N));
9223 Adjust_Condition (Right_Opnd (N));
9224 Set_Etype (N, Standard_Boolean);
9225 Adjust_Result_Type (N, Typ);
9227 elsif Is_Intrinsic_Subprogram (Entity (N)) then
9228 Expand_Intrinsic_Call (N, Entity (N));
9230 end if;
9231 end Expand_N_Op_Xor;
9233 ----------------------
9234 -- Expand_N_Or_Else --
9235 ----------------------
9237 procedure Expand_N_Or_Else (N : Node_Id)
9238 renames Expand_Short_Circuit_Operator;
9240 -----------------------------------
9241 -- Expand_N_Qualified_Expression --
9242 -----------------------------------
9244 procedure Expand_N_Qualified_Expression (N : Node_Id) is
9245 Operand : constant Node_Id := Expression (N);
9246 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
9248 begin
9249 -- Do validity check if validity checking operands
9251 if Validity_Checks_On and Validity_Check_Operands then
9252 Ensure_Valid (Operand);
9253 end if;
9255 -- Apply possible constraint check
9257 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
9259 if Do_Range_Check (Operand) then
9260 Set_Do_Range_Check (Operand, False);
9261 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
9262 end if;
9263 end Expand_N_Qualified_Expression;
9265 ------------------------------------
9266 -- Expand_N_Quantified_Expression --
9267 ------------------------------------
9269 -- We expand:
9271 -- for all X in range => Cond
9273 -- into:
9275 -- T := True;
9276 -- for X in range loop
9277 -- if not Cond then
9278 -- T := False;
9279 -- exit;
9280 -- end if;
9281 -- end loop;
9283 -- Similarly, an existentially quantified expression:
9285 -- for some X in range => Cond
9287 -- becomes:
9289 -- T := False;
9290 -- for X in range loop
9291 -- if Cond then
9292 -- T := True;
9293 -- exit;
9294 -- end if;
9295 -- end loop;
9297 -- In both cases, the iteration may be over a container in which case it is
9298 -- given by an iterator specification, not a loop parameter specification.
9300 procedure Expand_N_Quantified_Expression (N : Node_Id) is
9301 Actions : constant List_Id := New_List;
9302 For_All : constant Boolean := All_Present (N);
9303 Iter_Spec : constant Node_Id := Iterator_Specification (N);
9304 Loc : constant Source_Ptr := Sloc (N);
9305 Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N);
9306 Cond : Node_Id;
9307 Flag : Entity_Id;
9308 Scheme : Node_Id;
9309 Stmts : List_Id;
9311 begin
9312 -- Create the declaration of the flag which tracks the status of the
9313 -- quantified expression. Generate:
9315 -- Flag : Boolean := (True | False);
9317 Flag := Make_Temporary (Loc, 'T', N);
9319 Append_To (Actions,
9320 Make_Object_Declaration (Loc,
9321 Defining_Identifier => Flag,
9322 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
9323 Expression =>
9324 New_Occurrence_Of (Boolean_Literals (For_All), Loc)));
9326 -- Construct the circuitry which tracks the status of the quantified
9327 -- expression. Generate:
9329 -- if [not] Cond then
9330 -- Flag := (False | True);
9331 -- exit;
9332 -- end if;
9334 Cond := Relocate_Node (Condition (N));
9336 if For_All then
9337 Cond := Make_Op_Not (Loc, Cond);
9338 end if;
9340 Stmts := New_List (
9341 Make_Implicit_If_Statement (N,
9342 Condition => Cond,
9343 Then_Statements => New_List (
9344 Make_Assignment_Statement (Loc,
9345 Name => New_Occurrence_Of (Flag, Loc),
9346 Expression =>
9347 New_Occurrence_Of (Boolean_Literals (not For_All), Loc)),
9348 Make_Exit_Statement (Loc))));
9350 -- Build the loop equivalent of the quantified expression
9352 if Present (Iter_Spec) then
9353 Scheme :=
9354 Make_Iteration_Scheme (Loc,
9355 Iterator_Specification => Iter_Spec);
9356 else
9357 Scheme :=
9358 Make_Iteration_Scheme (Loc,
9359 Loop_Parameter_Specification => Loop_Spec);
9360 end if;
9362 Append_To (Actions,
9363 Make_Loop_Statement (Loc,
9364 Iteration_Scheme => Scheme,
9365 Statements => Stmts,
9366 End_Label => Empty));
9368 -- Transform the quantified expression
9370 Rewrite (N,
9371 Make_Expression_With_Actions (Loc,
9372 Expression => New_Occurrence_Of (Flag, Loc),
9373 Actions => Actions));
9374 Analyze_And_Resolve (N, Standard_Boolean);
9375 end Expand_N_Quantified_Expression;
9377 ---------------------------------
9378 -- Expand_N_Selected_Component --
9379 ---------------------------------
9381 procedure Expand_N_Selected_Component (N : Node_Id) is
9382 Loc : constant Source_Ptr := Sloc (N);
9383 Par : constant Node_Id := Parent (N);
9384 P : constant Node_Id := Prefix (N);
9385 S : constant Node_Id := Selector_Name (N);
9386 Ptyp : Entity_Id := Underlying_Type (Etype (P));
9387 Disc : Entity_Id;
9388 New_N : Node_Id;
9389 Dcon : Elmt_Id;
9390 Dval : Node_Id;
9392 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
9393 -- Gigi needs a temporary for prefixes that depend on a discriminant,
9394 -- unless the context of an assignment can provide size information.
9395 -- Don't we have a general routine that does this???
9397 function Is_Subtype_Declaration return Boolean;
9398 -- The replacement of a discriminant reference by its value is required
9399 -- if this is part of the initialization of an temporary generated by a
9400 -- change of representation. This shows up as the construction of a
9401 -- discriminant constraint for a subtype declared at the same point as
9402 -- the entity in the prefix of the selected component. We recognize this
9403 -- case when the context of the reference is:
9404 -- subtype ST is T(Obj.D);
9405 -- where the entity for Obj comes from source, and ST has the same sloc.
9407 -----------------------
9408 -- In_Left_Hand_Side --
9409 -----------------------
9411 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
9412 begin
9413 return (Nkind (Parent (Comp)) = N_Assignment_Statement
9414 and then Comp = Name (Parent (Comp)))
9415 or else (Present (Parent (Comp))
9416 and then Nkind (Parent (Comp)) in N_Subexpr
9417 and then In_Left_Hand_Side (Parent (Comp)));
9418 end In_Left_Hand_Side;
9420 -----------------------------
9421 -- Is_Subtype_Declaration --
9422 -----------------------------
9424 function Is_Subtype_Declaration return Boolean is
9425 Par : constant Node_Id := Parent (N);
9426 begin
9427 return
9428 Nkind (Par) = N_Index_Or_Discriminant_Constraint
9429 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
9430 and then Comes_From_Source (Entity (Prefix (N)))
9431 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
9432 end Is_Subtype_Declaration;
9434 -- Start of processing for Expand_N_Selected_Component
9436 begin
9437 -- Insert explicit dereference if required
9439 if Is_Access_Type (Ptyp) then
9441 -- First set prefix type to proper access type, in case it currently
9442 -- has a private (non-access) view of this type.
9444 Set_Etype (P, Ptyp);
9446 Insert_Explicit_Dereference (P);
9447 Analyze_And_Resolve (P, Designated_Type (Ptyp));
9449 if Ekind (Etype (P)) = E_Private_Subtype
9450 and then Is_For_Access_Subtype (Etype (P))
9451 then
9452 Set_Etype (P, Base_Type (Etype (P)));
9453 end if;
9455 Ptyp := Etype (P);
9456 end if;
9458 -- Deal with discriminant check required
9460 if Do_Discriminant_Check (N) then
9461 if Present (Discriminant_Checking_Func
9462 (Original_Record_Component (Entity (S))))
9463 then
9464 -- Present the discriminant checking function to the backend, so
9465 -- that it can inline the call to the function.
9467 Add_Inlined_Body
9468 (Discriminant_Checking_Func
9469 (Original_Record_Component (Entity (S))));
9471 -- Now reset the flag and generate the call
9473 Set_Do_Discriminant_Check (N, False);
9474 Generate_Discriminant_Check (N);
9476 -- In the case of Unchecked_Union, no discriminant checking is
9477 -- actually performed.
9479 else
9480 Set_Do_Discriminant_Check (N, False);
9481 end if;
9482 end if;
9484 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9485 -- function, then additional actuals must be passed.
9487 if Ada_Version >= Ada_2005
9488 and then Is_Build_In_Place_Function_Call (P)
9489 then
9490 Make_Build_In_Place_Call_In_Anonymous_Context (P);
9491 end if;
9493 -- Gigi cannot handle unchecked conversions that are the prefix of a
9494 -- selected component with discriminants. This must be checked during
9495 -- expansion, because during analysis the type of the selector is not
9496 -- known at the point the prefix is analyzed. If the conversion is the
9497 -- target of an assignment, then we cannot force the evaluation.
9499 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
9500 and then Has_Discriminants (Etype (N))
9501 and then not In_Left_Hand_Side (N)
9502 then
9503 Force_Evaluation (Prefix (N));
9504 end if;
9506 -- Remaining processing applies only if selector is a discriminant
9508 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
9510 -- If the selector is a discriminant of a constrained record type,
9511 -- we may be able to rewrite the expression with the actual value
9512 -- of the discriminant, a useful optimization in some cases.
9514 if Is_Record_Type (Ptyp)
9515 and then Has_Discriminants (Ptyp)
9516 and then Is_Constrained (Ptyp)
9517 then
9518 -- Do this optimization for discrete types only, and not for
9519 -- access types (access discriminants get us into trouble).
9521 if not Is_Discrete_Type (Etype (N)) then
9522 null;
9524 -- Don't do this on the left hand of an assignment statement.
9525 -- Normally one would think that references like this would not
9526 -- occur, but they do in generated code, and mean that we really
9527 -- do want to assign the discriminant.
9529 elsif Nkind (Par) = N_Assignment_Statement
9530 and then Name (Par) = N
9531 then
9532 null;
9534 -- Don't do this optimization for the prefix of an attribute or
9535 -- the name of an object renaming declaration since these are
9536 -- contexts where we do not want the value anyway.
9538 elsif (Nkind (Par) = N_Attribute_Reference
9539 and then Prefix (Par) = N)
9540 or else Is_Renamed_Object (N)
9541 then
9542 null;
9544 -- Don't do this optimization if we are within the code for a
9545 -- discriminant check, since the whole point of such a check may
9546 -- be to verify the condition on which the code below depends.
9548 elsif Is_In_Discriminant_Check (N) then
9549 null;
9551 -- Green light to see if we can do the optimization. There is
9552 -- still one condition that inhibits the optimization below but
9553 -- now is the time to check the particular discriminant.
9555 else
9556 -- Loop through discriminants to find the matching discriminant
9557 -- constraint to see if we can copy it.
9559 Disc := First_Discriminant (Ptyp);
9560 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
9561 Discr_Loop : while Present (Dcon) loop
9562 Dval := Node (Dcon);
9564 -- Check if this is the matching discriminant and if the
9565 -- discriminant value is simple enough to make sense to
9566 -- copy. We don't want to copy complex expressions, and
9567 -- indeed to do so can cause trouble (before we put in
9568 -- this guard, a discriminant expression containing an
9569 -- AND THEN was copied, causing problems for coverage
9570 -- analysis tools).
9572 -- However, if the reference is part of the initialization
9573 -- code generated for an object declaration, we must use
9574 -- the discriminant value from the subtype constraint,
9575 -- because the selected component may be a reference to the
9576 -- object being initialized, whose discriminant is not yet
9577 -- set. This only happens in complex cases involving changes
9578 -- or representation.
9580 if Disc = Entity (Selector_Name (N))
9581 and then (Is_Entity_Name (Dval)
9582 or else Compile_Time_Known_Value (Dval)
9583 or else Is_Subtype_Declaration)
9584 then
9585 -- Here we have the matching discriminant. Check for
9586 -- the case of a discriminant of a component that is
9587 -- constrained by an outer discriminant, which cannot
9588 -- be optimized away.
9590 if Denotes_Discriminant
9591 (Dval, Check_Concurrent => True)
9592 then
9593 exit Discr_Loop;
9595 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
9596 and then
9597 Denotes_Discriminant
9598 (Selector_Name (Original_Node (Dval)), True)
9599 then
9600 exit Discr_Loop;
9602 -- Do not retrieve value if constraint is not static. It
9603 -- is generally not useful, and the constraint may be a
9604 -- rewritten outer discriminant in which case it is in
9605 -- fact incorrect.
9607 elsif Is_Entity_Name (Dval)
9608 and then
9609 Nkind (Parent (Entity (Dval))) = N_Object_Declaration
9610 and then Present (Expression (Parent (Entity (Dval))))
9611 and then not
9612 Is_OK_Static_Expression
9613 (Expression (Parent (Entity (Dval))))
9614 then
9615 exit Discr_Loop;
9617 -- In the context of a case statement, the expression may
9618 -- have the base type of the discriminant, and we need to
9619 -- preserve the constraint to avoid spurious errors on
9620 -- missing cases.
9622 elsif Nkind (Parent (N)) = N_Case_Statement
9623 and then Etype (Dval) /= Etype (Disc)
9624 then
9625 Rewrite (N,
9626 Make_Qualified_Expression (Loc,
9627 Subtype_Mark =>
9628 New_Occurrence_Of (Etype (Disc), Loc),
9629 Expression =>
9630 New_Copy_Tree (Dval)));
9631 Analyze_And_Resolve (N, Etype (Disc));
9633 -- In case that comes out as a static expression,
9634 -- reset it (a selected component is never static).
9636 Set_Is_Static_Expression (N, False);
9637 return;
9639 -- Otherwise we can just copy the constraint, but the
9640 -- result is certainly not static. In some cases the
9641 -- discriminant constraint has been analyzed in the
9642 -- context of the original subtype indication, but for
9643 -- itypes the constraint might not have been analyzed
9644 -- yet, and this must be done now.
9646 else
9647 Rewrite (N, New_Copy_Tree (Dval));
9648 Analyze_And_Resolve (N);
9649 Set_Is_Static_Expression (N, False);
9650 return;
9651 end if;
9652 end if;
9654 Next_Elmt (Dcon);
9655 Next_Discriminant (Disc);
9656 end loop Discr_Loop;
9658 -- Note: the above loop should always find a matching
9659 -- discriminant, but if it does not, we just missed an
9660 -- optimization due to some glitch (perhaps a previous
9661 -- error), so ignore.
9663 end if;
9664 end if;
9666 -- The only remaining processing is in the case of a discriminant of
9667 -- a concurrent object, where we rewrite the prefix to denote the
9668 -- corresponding record type. If the type is derived and has renamed
9669 -- discriminants, use corresponding discriminant, which is the one
9670 -- that appears in the corresponding record.
9672 if not Is_Concurrent_Type (Ptyp) then
9673 return;
9674 end if;
9676 Disc := Entity (Selector_Name (N));
9678 if Is_Derived_Type (Ptyp)
9679 and then Present (Corresponding_Discriminant (Disc))
9680 then
9681 Disc := Corresponding_Discriminant (Disc);
9682 end if;
9684 New_N :=
9685 Make_Selected_Component (Loc,
9686 Prefix =>
9687 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
9688 New_Copy_Tree (P)),
9689 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
9691 Rewrite (N, New_N);
9692 Analyze (N);
9693 end if;
9695 -- Set Atomic_Sync_Required if necessary for atomic component
9697 if Nkind (N) = N_Selected_Component then
9698 declare
9699 E : constant Entity_Id := Entity (Selector_Name (N));
9700 Set : Boolean;
9702 begin
9703 -- If component is atomic, but type is not, setting depends on
9704 -- disable/enable state for the component.
9706 if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then
9707 Set := not Atomic_Synchronization_Disabled (E);
9709 -- If component is not atomic, but its type is atomic, setting
9710 -- depends on disable/enable state for the type.
9712 elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9713 Set := not Atomic_Synchronization_Disabled (Etype (E));
9715 -- If both component and type are atomic, we disable if either
9716 -- component or its type have sync disabled.
9718 elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9719 Set := (not Atomic_Synchronization_Disabled (E))
9720 and then
9721 (not Atomic_Synchronization_Disabled (Etype (E)));
9723 else
9724 Set := False;
9725 end if;
9727 -- Set flag if required
9729 if Set then
9730 Activate_Atomic_Synchronization (N);
9731 end if;
9732 end;
9733 end if;
9734 end Expand_N_Selected_Component;
9736 --------------------
9737 -- Expand_N_Slice --
9738 --------------------
9740 procedure Expand_N_Slice (N : Node_Id) is
9741 Loc : constant Source_Ptr := Sloc (N);
9742 Typ : constant Entity_Id := Etype (N);
9744 function Is_Procedure_Actual (N : Node_Id) return Boolean;
9745 -- Check whether the argument is an actual for a procedure call, in
9746 -- which case the expansion of a bit-packed slice is deferred until the
9747 -- call itself is expanded. The reason this is required is that we might
9748 -- have an IN OUT or OUT parameter, and the copy out is essential, and
9749 -- that copy out would be missed if we created a temporary here in
9750 -- Expand_N_Slice. Note that we don't bother to test specifically for an
9751 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
9752 -- is harmless to defer expansion in the IN case, since the call
9753 -- processing will still generate the appropriate copy in operation,
9754 -- which will take care of the slice.
9756 procedure Make_Temporary_For_Slice;
9757 -- Create a named variable for the value of the slice, in cases where
9758 -- the back-end cannot handle it properly, e.g. when packed types or
9759 -- unaligned slices are involved.
9761 -------------------------
9762 -- Is_Procedure_Actual --
9763 -------------------------
9765 function Is_Procedure_Actual (N : Node_Id) return Boolean is
9766 Par : Node_Id := Parent (N);
9768 begin
9769 loop
9770 -- If our parent is a procedure call we can return
9772 if Nkind (Par) = N_Procedure_Call_Statement then
9773 return True;
9775 -- If our parent is a type conversion, keep climbing the tree,
9776 -- since a type conversion can be a procedure actual. Also keep
9777 -- climbing if parameter association or a qualified expression,
9778 -- since these are additional cases that do can appear on
9779 -- procedure actuals.
9781 elsif Nkind_In (Par, N_Type_Conversion,
9782 N_Parameter_Association,
9783 N_Qualified_Expression)
9784 then
9785 Par := Parent (Par);
9787 -- Any other case is not what we are looking for
9789 else
9790 return False;
9791 end if;
9792 end loop;
9793 end Is_Procedure_Actual;
9795 ------------------------------
9796 -- Make_Temporary_For_Slice --
9797 ------------------------------
9799 procedure Make_Temporary_For_Slice is
9800 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
9801 Decl : Node_Id;
9803 begin
9804 Decl :=
9805 Make_Object_Declaration (Loc,
9806 Defining_Identifier => Ent,
9807 Object_Definition => New_Occurrence_Of (Typ, Loc));
9809 Set_No_Initialization (Decl);
9811 Insert_Actions (N, New_List (
9812 Decl,
9813 Make_Assignment_Statement (Loc,
9814 Name => New_Occurrence_Of (Ent, Loc),
9815 Expression => Relocate_Node (N))));
9817 Rewrite (N, New_Occurrence_Of (Ent, Loc));
9818 Analyze_And_Resolve (N, Typ);
9819 end Make_Temporary_For_Slice;
9821 -- Local variables
9823 Pref : constant Node_Id := Prefix (N);
9824 Pref_Typ : Entity_Id := Etype (Pref);
9826 -- Start of processing for Expand_N_Slice
9828 begin
9829 -- Special handling for access types
9831 if Is_Access_Type (Pref_Typ) then
9832 Pref_Typ := Designated_Type (Pref_Typ);
9834 Rewrite (Pref,
9835 Make_Explicit_Dereference (Sloc (N),
9836 Prefix => Relocate_Node (Pref)));
9838 Analyze_And_Resolve (Pref, Pref_Typ);
9839 end if;
9841 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9842 -- function, then additional actuals must be passed.
9844 if Ada_Version >= Ada_2005
9845 and then Is_Build_In_Place_Function_Call (Pref)
9846 then
9847 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
9848 end if;
9850 -- The remaining case to be handled is packed slices. We can leave
9851 -- packed slices as they are in the following situations:
9853 -- 1. Right or left side of an assignment (we can handle this
9854 -- situation correctly in the assignment statement expansion).
9856 -- 2. Prefix of indexed component (the slide is optimized away in this
9857 -- case, see the start of Expand_N_Slice.)
9859 -- 3. Object renaming declaration, since we want the name of the
9860 -- slice, not the value.
9862 -- 4. Argument to procedure call, since copy-in/copy-out handling may
9863 -- be required, and this is handled in the expansion of call
9864 -- itself.
9866 -- 5. Prefix of an address attribute (this is an error which is caught
9867 -- elsewhere, and the expansion would interfere with generating the
9868 -- error message).
9870 if not Is_Packed (Typ) then
9872 -- Apply transformation for actuals of a function call, where
9873 -- Expand_Actuals is not used.
9875 if Nkind (Parent (N)) = N_Function_Call
9876 and then Is_Possibly_Unaligned_Slice (N)
9877 then
9878 Make_Temporary_For_Slice;
9879 end if;
9881 elsif Nkind (Parent (N)) = N_Assignment_Statement
9882 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
9883 and then Parent (N) = Name (Parent (Parent (N))))
9884 then
9885 return;
9887 elsif Nkind (Parent (N)) = N_Indexed_Component
9888 or else Is_Renamed_Object (N)
9889 or else Is_Procedure_Actual (N)
9890 then
9891 return;
9893 elsif Nkind (Parent (N)) = N_Attribute_Reference
9894 and then Attribute_Name (Parent (N)) = Name_Address
9895 then
9896 return;
9898 else
9899 Make_Temporary_For_Slice;
9900 end if;
9901 end Expand_N_Slice;
9903 ------------------------------
9904 -- Expand_N_Type_Conversion --
9905 ------------------------------
9907 procedure Expand_N_Type_Conversion (N : Node_Id) is
9908 Loc : constant Source_Ptr := Sloc (N);
9909 Operand : constant Node_Id := Expression (N);
9910 Target_Type : constant Entity_Id := Etype (N);
9911 Operand_Type : Entity_Id := Etype (Operand);
9913 procedure Handle_Changed_Representation;
9914 -- This is called in the case of record and array type conversions to
9915 -- see if there is a change of representation to be handled. Change of
9916 -- representation is actually handled at the assignment statement level,
9917 -- and what this procedure does is rewrite node N conversion as an
9918 -- assignment to temporary. If there is no change of representation,
9919 -- then the conversion node is unchanged.
9921 procedure Raise_Accessibility_Error;
9922 -- Called when we know that an accessibility check will fail. Rewrites
9923 -- node N to an appropriate raise statement and outputs warning msgs.
9924 -- The Etype of the raise node is set to Target_Type.
9926 procedure Real_Range_Check;
9927 -- Handles generation of range check for real target value
9929 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean;
9930 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
9931 -- evaluates to True.
9933 -----------------------------------
9934 -- Handle_Changed_Representation --
9935 -----------------------------------
9937 procedure Handle_Changed_Representation is
9938 Temp : Entity_Id;
9939 Decl : Node_Id;
9940 Odef : Node_Id;
9941 Disc : Node_Id;
9942 N_Ix : Node_Id;
9943 Cons : List_Id;
9945 begin
9946 -- Nothing else to do if no change of representation
9948 if Same_Representation (Operand_Type, Target_Type) then
9949 return;
9951 -- The real change of representation work is done by the assignment
9952 -- statement processing. So if this type conversion is appearing as
9953 -- the expression of an assignment statement, nothing needs to be
9954 -- done to the conversion.
9956 elsif Nkind (Parent (N)) = N_Assignment_Statement then
9957 return;
9959 -- Otherwise we need to generate a temporary variable, and do the
9960 -- change of representation assignment into that temporary variable.
9961 -- The conversion is then replaced by a reference to this variable.
9963 else
9964 Cons := No_List;
9966 -- If type is unconstrained we have to add a constraint, copied
9967 -- from the actual value of the left hand side.
9969 if not Is_Constrained (Target_Type) then
9970 if Has_Discriminants (Operand_Type) then
9971 Disc := First_Discriminant (Operand_Type);
9973 if Disc /= First_Stored_Discriminant (Operand_Type) then
9974 Disc := First_Stored_Discriminant (Operand_Type);
9975 end if;
9977 Cons := New_List;
9978 while Present (Disc) loop
9979 Append_To (Cons,
9980 Make_Selected_Component (Loc,
9981 Prefix =>
9982 Duplicate_Subexpr_Move_Checks (Operand),
9983 Selector_Name =>
9984 Make_Identifier (Loc, Chars (Disc))));
9985 Next_Discriminant (Disc);
9986 end loop;
9988 elsif Is_Array_Type (Operand_Type) then
9989 N_Ix := First_Index (Target_Type);
9990 Cons := New_List;
9992 for J in 1 .. Number_Dimensions (Operand_Type) loop
9994 -- We convert the bounds explicitly. We use an unchecked
9995 -- conversion because bounds checks are done elsewhere.
9997 Append_To (Cons,
9998 Make_Range (Loc,
9999 Low_Bound =>
10000 Unchecked_Convert_To (Etype (N_Ix),
10001 Make_Attribute_Reference (Loc,
10002 Prefix =>
10003 Duplicate_Subexpr_No_Checks
10004 (Operand, Name_Req => True),
10005 Attribute_Name => Name_First,
10006 Expressions => New_List (
10007 Make_Integer_Literal (Loc, J)))),
10009 High_Bound =>
10010 Unchecked_Convert_To (Etype (N_Ix),
10011 Make_Attribute_Reference (Loc,
10012 Prefix =>
10013 Duplicate_Subexpr_No_Checks
10014 (Operand, Name_Req => True),
10015 Attribute_Name => Name_Last,
10016 Expressions => New_List (
10017 Make_Integer_Literal (Loc, J))))));
10019 Next_Index (N_Ix);
10020 end loop;
10021 end if;
10022 end if;
10024 Odef := New_Occurrence_Of (Target_Type, Loc);
10026 if Present (Cons) then
10027 Odef :=
10028 Make_Subtype_Indication (Loc,
10029 Subtype_Mark => Odef,
10030 Constraint =>
10031 Make_Index_Or_Discriminant_Constraint (Loc,
10032 Constraints => Cons));
10033 end if;
10035 Temp := Make_Temporary (Loc, 'C');
10036 Decl :=
10037 Make_Object_Declaration (Loc,
10038 Defining_Identifier => Temp,
10039 Object_Definition => Odef);
10041 Set_No_Initialization (Decl, True);
10043 -- Insert required actions. It is essential to suppress checks
10044 -- since we have suppressed default initialization, which means
10045 -- that the variable we create may have no discriminants.
10047 Insert_Actions (N,
10048 New_List (
10049 Decl,
10050 Make_Assignment_Statement (Loc,
10051 Name => New_Occurrence_Of (Temp, Loc),
10052 Expression => Relocate_Node (N))),
10053 Suppress => All_Checks);
10055 Rewrite (N, New_Occurrence_Of (Temp, Loc));
10056 return;
10057 end if;
10058 end Handle_Changed_Representation;
10060 -------------------------------
10061 -- Raise_Accessibility_Error --
10062 -------------------------------
10064 procedure Raise_Accessibility_Error is
10065 begin
10066 Error_Msg_Warn := SPARK_Mode /= On;
10067 Rewrite (N,
10068 Make_Raise_Program_Error (Sloc (N),
10069 Reason => PE_Accessibility_Check_Failed));
10070 Set_Etype (N, Target_Type);
10072 Error_Msg_N ("<<accessibility check failure", N);
10073 Error_Msg_NE ("\<<& [", N, Standard_Program_Error);
10074 end Raise_Accessibility_Error;
10076 ----------------------
10077 -- Real_Range_Check --
10078 ----------------------
10080 -- Case of conversions to floating-point or fixed-point. If range checks
10081 -- are enabled and the target type has a range constraint, we convert:
10083 -- typ (x)
10085 -- to
10087 -- Tnn : typ'Base := typ'Base (x);
10088 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
10089 -- Tnn
10091 -- This is necessary when there is a conversion of integer to float or
10092 -- to fixed-point to ensure that the correct checks are made. It is not
10093 -- necessary for float to float where it is enough to simply set the
10094 -- Do_Range_Check flag.
10096 procedure Real_Range_Check is
10097 Btyp : constant Entity_Id := Base_Type (Target_Type);
10098 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
10099 Hi : constant Node_Id := Type_High_Bound (Target_Type);
10100 Xtyp : constant Entity_Id := Etype (Operand);
10101 Conv : Node_Id;
10102 Tnn : Entity_Id;
10104 begin
10105 -- Nothing to do if conversion was rewritten
10107 if Nkind (N) /= N_Type_Conversion then
10108 return;
10109 end if;
10111 -- Nothing to do if range checks suppressed, or target has the same
10112 -- range as the base type (or is the base type).
10114 if Range_Checks_Suppressed (Target_Type)
10115 or else (Lo = Type_Low_Bound (Btyp)
10116 and then
10117 Hi = Type_High_Bound (Btyp))
10118 then
10119 return;
10120 end if;
10122 -- Nothing to do if expression is an entity on which checks have been
10123 -- suppressed.
10125 if Is_Entity_Name (Operand)
10126 and then Range_Checks_Suppressed (Entity (Operand))
10127 then
10128 return;
10129 end if;
10131 -- Nothing to do if bounds are all static and we can tell that the
10132 -- expression is within the bounds of the target. Note that if the
10133 -- operand is of an unconstrained floating-point type, then we do
10134 -- not trust it to be in range (might be infinite)
10136 declare
10137 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
10138 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
10140 begin
10141 if (not Is_Floating_Point_Type (Xtyp)
10142 or else Is_Constrained (Xtyp))
10143 and then Compile_Time_Known_Value (S_Lo)
10144 and then Compile_Time_Known_Value (S_Hi)
10145 and then Compile_Time_Known_Value (Hi)
10146 and then Compile_Time_Known_Value (Lo)
10147 then
10148 declare
10149 D_Lov : constant Ureal := Expr_Value_R (Lo);
10150 D_Hiv : constant Ureal := Expr_Value_R (Hi);
10151 S_Lov : Ureal;
10152 S_Hiv : Ureal;
10154 begin
10155 if Is_Real_Type (Xtyp) then
10156 S_Lov := Expr_Value_R (S_Lo);
10157 S_Hiv := Expr_Value_R (S_Hi);
10158 else
10159 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
10160 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
10161 end if;
10163 if D_Hiv > D_Lov
10164 and then S_Lov >= D_Lov
10165 and then S_Hiv <= D_Hiv
10166 then
10167 -- Unset the range check flag on the current value of
10168 -- Expression (N), since the captured Operand may have
10169 -- been rewritten (such as for the case of a conversion
10170 -- to a fixed-point type).
10172 Set_Do_Range_Check (Expression (N), False);
10174 return;
10175 end if;
10176 end;
10177 end if;
10178 end;
10180 -- For float to float conversions, we are done
10182 if Is_Floating_Point_Type (Xtyp)
10183 and then
10184 Is_Floating_Point_Type (Btyp)
10185 then
10186 return;
10187 end if;
10189 -- Otherwise rewrite the conversion as described above
10191 Conv := Relocate_Node (N);
10192 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
10193 Set_Etype (Conv, Btyp);
10195 -- Enable overflow except for case of integer to float conversions,
10196 -- where it is never required, since we can never have overflow in
10197 -- this case.
10199 if not Is_Integer_Type (Etype (Operand)) then
10200 Enable_Overflow_Check (Conv);
10201 end if;
10203 Tnn := Make_Temporary (Loc, 'T', Conv);
10205 Insert_Actions (N, New_List (
10206 Make_Object_Declaration (Loc,
10207 Defining_Identifier => Tnn,
10208 Object_Definition => New_Occurrence_Of (Btyp, Loc),
10209 Constant_Present => True,
10210 Expression => Conv),
10212 Make_Raise_Constraint_Error (Loc,
10213 Condition =>
10214 Make_Or_Else (Loc,
10215 Left_Opnd =>
10216 Make_Op_Lt (Loc,
10217 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
10218 Right_Opnd =>
10219 Make_Attribute_Reference (Loc,
10220 Attribute_Name => Name_First,
10221 Prefix =>
10222 New_Occurrence_Of (Target_Type, Loc))),
10224 Right_Opnd =>
10225 Make_Op_Gt (Loc,
10226 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
10227 Right_Opnd =>
10228 Make_Attribute_Reference (Loc,
10229 Attribute_Name => Name_Last,
10230 Prefix =>
10231 New_Occurrence_Of (Target_Type, Loc)))),
10232 Reason => CE_Range_Check_Failed)));
10234 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
10235 Analyze_And_Resolve (N, Btyp);
10236 end Real_Range_Check;
10238 -----------------------------
10239 -- Has_Extra_Accessibility --
10240 -----------------------------
10242 -- Returns true for a formal of an anonymous access type or for
10243 -- an Ada 2012-style stand-alone object of an anonymous access type.
10245 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is
10246 begin
10247 if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then
10248 return Present (Effective_Extra_Accessibility (Id));
10249 else
10250 return False;
10251 end if;
10252 end Has_Extra_Accessibility;
10254 -- Start of processing for Expand_N_Type_Conversion
10256 begin
10257 -- First remove check marks put by the semantic analysis on the type
10258 -- conversion between array types. We need these checks, and they will
10259 -- be generated by this expansion routine, but we do not depend on these
10260 -- flags being set, and since we do intend to expand the checks in the
10261 -- front end, we don't want them on the tree passed to the back end.
10263 if Is_Array_Type (Target_Type) then
10264 if Is_Constrained (Target_Type) then
10265 Set_Do_Length_Check (N, False);
10266 else
10267 Set_Do_Range_Check (Operand, False);
10268 end if;
10269 end if;
10271 -- Nothing at all to do if conversion is to the identical type so remove
10272 -- the conversion completely, it is useless, except that it may carry
10273 -- an Assignment_OK attribute, which must be propagated to the operand.
10275 if Operand_Type = Target_Type then
10276 if Assignment_OK (N) then
10277 Set_Assignment_OK (Operand);
10278 end if;
10280 Rewrite (N, Relocate_Node (Operand));
10281 goto Done;
10282 end if;
10284 -- Nothing to do if this is the second argument of read. This is a
10285 -- "backwards" conversion that will be handled by the specialized code
10286 -- in attribute processing.
10288 if Nkind (Parent (N)) = N_Attribute_Reference
10289 and then Attribute_Name (Parent (N)) = Name_Read
10290 and then Next (First (Expressions (Parent (N)))) = N
10291 then
10292 goto Done;
10293 end if;
10295 -- Check for case of converting to a type that has an invariant
10296 -- associated with it. This required an invariant check. We convert
10298 -- typ (expr)
10300 -- into
10302 -- do invariant_check (typ (expr)) in typ (expr);
10304 -- using Duplicate_Subexpr to avoid multiple side effects
10306 -- Note: the Comes_From_Source check, and then the resetting of this
10307 -- flag prevents what would otherwise be an infinite recursion.
10309 if Has_Invariants (Target_Type)
10310 and then Present (Invariant_Procedure (Target_Type))
10311 and then Comes_From_Source (N)
10312 then
10313 Set_Comes_From_Source (N, False);
10314 Rewrite (N,
10315 Make_Expression_With_Actions (Loc,
10316 Actions => New_List (
10317 Make_Invariant_Call (Duplicate_Subexpr (N))),
10318 Expression => Duplicate_Subexpr_No_Checks (N)));
10319 Analyze_And_Resolve (N, Target_Type);
10320 goto Done;
10321 end if;
10323 -- Here if we may need to expand conversion
10325 -- If the operand of the type conversion is an arithmetic operation on
10326 -- signed integers, and the based type of the signed integer type in
10327 -- question is smaller than Standard.Integer, we promote both of the
10328 -- operands to type Integer.
10330 -- For example, if we have
10332 -- target-type (opnd1 + opnd2)
10334 -- and opnd1 and opnd2 are of type short integer, then we rewrite
10335 -- this as:
10337 -- target-type (integer(opnd1) + integer(opnd2))
10339 -- We do this because we are always allowed to compute in a larger type
10340 -- if we do the right thing with the result, and in this case we are
10341 -- going to do a conversion which will do an appropriate check to make
10342 -- sure that things are in range of the target type in any case. This
10343 -- avoids some unnecessary intermediate overflows.
10345 -- We might consider a similar transformation in the case where the
10346 -- target is a real type or a 64-bit integer type, and the operand
10347 -- is an arithmetic operation using a 32-bit integer type. However,
10348 -- we do not bother with this case, because it could cause significant
10349 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
10350 -- much cheaper, but we don't want different behavior on 32-bit and
10351 -- 64-bit machines. Note that the exclusion of the 64-bit case also
10352 -- handles the configurable run-time cases where 64-bit arithmetic
10353 -- may simply be unavailable.
10355 -- Note: this circuit is partially redundant with respect to the circuit
10356 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
10357 -- the processing here. Also we still need the Checks circuit, since we
10358 -- have to be sure not to generate junk overflow checks in the first
10359 -- place, since it would be trick to remove them here.
10361 if Integer_Promotion_Possible (N) then
10363 -- All conditions met, go ahead with transformation
10365 declare
10366 Opnd : Node_Id;
10367 L, R : Node_Id;
10369 begin
10370 R :=
10371 Make_Type_Conversion (Loc,
10372 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
10373 Expression => Relocate_Node (Right_Opnd (Operand)));
10375 Opnd := New_Op_Node (Nkind (Operand), Loc);
10376 Set_Right_Opnd (Opnd, R);
10378 if Nkind (Operand) in N_Binary_Op then
10379 L :=
10380 Make_Type_Conversion (Loc,
10381 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
10382 Expression => Relocate_Node (Left_Opnd (Operand)));
10384 Set_Left_Opnd (Opnd, L);
10385 end if;
10387 Rewrite (N,
10388 Make_Type_Conversion (Loc,
10389 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
10390 Expression => Opnd));
10392 Analyze_And_Resolve (N, Target_Type);
10393 goto Done;
10394 end;
10395 end if;
10397 -- Do validity check if validity checking operands
10399 if Validity_Checks_On and Validity_Check_Operands then
10400 Ensure_Valid (Operand);
10401 end if;
10403 -- Special case of converting from non-standard boolean type
10405 if Is_Boolean_Type (Operand_Type)
10406 and then (Nonzero_Is_True (Operand_Type))
10407 then
10408 Adjust_Condition (Operand);
10409 Set_Etype (Operand, Standard_Boolean);
10410 Operand_Type := Standard_Boolean;
10411 end if;
10413 -- Case of converting to an access type
10415 if Is_Access_Type (Target_Type) then
10417 -- Apply an accessibility check when the conversion operand is an
10418 -- access parameter (or a renaming thereof), unless conversion was
10419 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
10420 -- Note that other checks may still need to be applied below (such
10421 -- as tagged type checks).
10423 if Is_Entity_Name (Operand)
10424 and then Has_Extra_Accessibility (Entity (Operand))
10425 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
10426 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
10427 or else Attribute_Name (Original_Node (N)) = Name_Access)
10428 then
10429 Apply_Accessibility_Check
10430 (Operand, Target_Type, Insert_Node => Operand);
10432 -- If the level of the operand type is statically deeper than the
10433 -- level of the target type, then force Program_Error. Note that this
10434 -- can only occur for cases where the attribute is within the body of
10435 -- an instantiation, otherwise the conversion will already have been
10436 -- rejected as illegal.
10438 -- Note: warnings are issued by the analyzer for the instance cases
10440 elsif In_Instance_Body
10442 -- The case where the target type is an anonymous access type of
10443 -- a discriminant is excluded, because the level of such a type
10444 -- depends on the context and currently the level returned for such
10445 -- types is zero, resulting in warnings about about check failures
10446 -- in certain legal cases involving class-wide interfaces as the
10447 -- designated type (some cases, such as return statements, are
10448 -- checked at run time, but not clear if these are handled right
10449 -- in general, see 3.10.2(12/2-12.5/3) ???).
10451 and then
10452 not (Ekind (Target_Type) = E_Anonymous_Access_Type
10453 and then Present (Associated_Node_For_Itype (Target_Type))
10454 and then Nkind (Associated_Node_For_Itype (Target_Type)) =
10455 N_Discriminant_Specification)
10456 and then
10457 Type_Access_Level (Operand_Type) > Type_Access_Level (Target_Type)
10458 then
10459 Raise_Accessibility_Error;
10461 -- When the operand is a selected access discriminant the check needs
10462 -- to be made against the level of the object denoted by the prefix
10463 -- of the selected name. Force Program_Error for this case as well
10464 -- (this accessibility violation can only happen if within the body
10465 -- of an instantiation).
10467 elsif In_Instance_Body
10468 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
10469 and then Nkind (Operand) = N_Selected_Component
10470 and then Object_Access_Level (Operand) >
10471 Type_Access_Level (Target_Type)
10472 then
10473 Raise_Accessibility_Error;
10474 goto Done;
10475 end if;
10476 end if;
10478 -- Case of conversions of tagged types and access to tagged types
10480 -- When needed, that is to say when the expression is class-wide, Add
10481 -- runtime a tag check for (strict) downward conversion by using the
10482 -- membership test, generating:
10484 -- [constraint_error when Operand not in Target_Type'Class]
10486 -- or in the access type case
10488 -- [constraint_error
10489 -- when Operand /= null
10490 -- and then Operand.all not in
10491 -- Designated_Type (Target_Type)'Class]
10493 if (Is_Access_Type (Target_Type)
10494 and then Is_Tagged_Type (Designated_Type (Target_Type)))
10495 or else Is_Tagged_Type (Target_Type)
10496 then
10497 -- Do not do any expansion in the access type case if the parent is a
10498 -- renaming, since this is an error situation which will be caught by
10499 -- Sem_Ch8, and the expansion can interfere with this error check.
10501 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
10502 goto Done;
10503 end if;
10505 -- Otherwise, proceed with processing tagged conversion
10507 Tagged_Conversion : declare
10508 Actual_Op_Typ : Entity_Id;
10509 Actual_Targ_Typ : Entity_Id;
10510 Make_Conversion : Boolean := False;
10511 Root_Op_Typ : Entity_Id;
10513 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
10514 -- Create a membership check to test whether Operand is a member
10515 -- of Targ_Typ. If the original Target_Type is an access, include
10516 -- a test for null value. The check is inserted at N.
10518 --------------------
10519 -- Make_Tag_Check --
10520 --------------------
10522 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
10523 Cond : Node_Id;
10525 begin
10526 -- Generate:
10527 -- [Constraint_Error
10528 -- when Operand /= null
10529 -- and then Operand.all not in Targ_Typ]
10531 if Is_Access_Type (Target_Type) then
10532 Cond :=
10533 Make_And_Then (Loc,
10534 Left_Opnd =>
10535 Make_Op_Ne (Loc,
10536 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10537 Right_Opnd => Make_Null (Loc)),
10539 Right_Opnd =>
10540 Make_Not_In (Loc,
10541 Left_Opnd =>
10542 Make_Explicit_Dereference (Loc,
10543 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
10544 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc)));
10546 -- Generate:
10547 -- [Constraint_Error when Operand not in Targ_Typ]
10549 else
10550 Cond :=
10551 Make_Not_In (Loc,
10552 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10553 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc));
10554 end if;
10556 Insert_Action (N,
10557 Make_Raise_Constraint_Error (Loc,
10558 Condition => Cond,
10559 Reason => CE_Tag_Check_Failed));
10560 end Make_Tag_Check;
10562 -- Start of processing for Tagged_Conversion
10564 begin
10565 -- Handle entities from the limited view
10567 if Is_Access_Type (Operand_Type) then
10568 Actual_Op_Typ :=
10569 Available_View (Designated_Type (Operand_Type));
10570 else
10571 Actual_Op_Typ := Operand_Type;
10572 end if;
10574 if Is_Access_Type (Target_Type) then
10575 Actual_Targ_Typ :=
10576 Available_View (Designated_Type (Target_Type));
10577 else
10578 Actual_Targ_Typ := Target_Type;
10579 end if;
10581 Root_Op_Typ := Root_Type (Actual_Op_Typ);
10583 -- Ada 2005 (AI-251): Handle interface type conversion
10585 if Is_Interface (Actual_Op_Typ) then
10586 Expand_Interface_Conversion (N);
10587 goto Done;
10588 end if;
10590 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
10592 -- Create a runtime tag check for a downward class-wide type
10593 -- conversion.
10595 if Is_Class_Wide_Type (Actual_Op_Typ)
10596 and then Actual_Op_Typ /= Actual_Targ_Typ
10597 and then Root_Op_Typ /= Actual_Targ_Typ
10598 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ,
10599 Use_Full_View => True)
10600 then
10601 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
10602 Make_Conversion := True;
10603 end if;
10605 -- AI05-0073: If the result subtype of the function is defined
10606 -- by an access_definition designating a specific tagged type
10607 -- T, a check is made that the result value is null or the tag
10608 -- of the object designated by the result value identifies T.
10609 -- Constraint_Error is raised if this check fails.
10611 if Nkind (Parent (N)) = N_Simple_Return_Statement then
10612 declare
10613 Func : Entity_Id;
10614 Func_Typ : Entity_Id;
10616 begin
10617 -- Climb scope stack looking for the enclosing function
10619 Func := Current_Scope;
10620 while Present (Func)
10621 and then Ekind (Func) /= E_Function
10622 loop
10623 Func := Scope (Func);
10624 end loop;
10626 -- The function's return subtype must be defined using
10627 -- an access definition.
10629 if Nkind (Result_Definition (Parent (Func))) =
10630 N_Access_Definition
10631 then
10632 Func_Typ := Directly_Designated_Type (Etype (Func));
10634 -- The return subtype denotes a specific tagged type,
10635 -- in other words, a non class-wide type.
10637 if Is_Tagged_Type (Func_Typ)
10638 and then not Is_Class_Wide_Type (Func_Typ)
10639 then
10640 Make_Tag_Check (Actual_Targ_Typ);
10641 Make_Conversion := True;
10642 end if;
10643 end if;
10644 end;
10645 end if;
10647 -- We have generated a tag check for either a class-wide type
10648 -- conversion or for AI05-0073.
10650 if Make_Conversion then
10651 declare
10652 Conv : Node_Id;
10653 begin
10654 Conv :=
10655 Make_Unchecked_Type_Conversion (Loc,
10656 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
10657 Expression => Relocate_Node (Expression (N)));
10658 Rewrite (N, Conv);
10659 Analyze_And_Resolve (N, Target_Type);
10660 end;
10661 end if;
10662 end if;
10663 end Tagged_Conversion;
10665 -- Case of other access type conversions
10667 elsif Is_Access_Type (Target_Type) then
10668 Apply_Constraint_Check (Operand, Target_Type);
10670 -- Case of conversions from a fixed-point type
10672 -- These conversions require special expansion and processing, found in
10673 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
10674 -- since from a semantic point of view, these are simple integer
10675 -- conversions, which do not need further processing.
10677 elsif Is_Fixed_Point_Type (Operand_Type)
10678 and then not Conversion_OK (N)
10679 then
10680 -- We should never see universal fixed at this case, since the
10681 -- expansion of the constituent divide or multiply should have
10682 -- eliminated the explicit mention of universal fixed.
10684 pragma Assert (Operand_Type /= Universal_Fixed);
10686 -- Check for special case of the conversion to universal real that
10687 -- occurs as a result of the use of a round attribute. In this case,
10688 -- the real type for the conversion is taken from the target type of
10689 -- the Round attribute and the result must be marked as rounded.
10691 if Target_Type = Universal_Real
10692 and then Nkind (Parent (N)) = N_Attribute_Reference
10693 and then Attribute_Name (Parent (N)) = Name_Round
10694 then
10695 Set_Rounded_Result (N);
10696 Set_Etype (N, Etype (Parent (N)));
10697 end if;
10699 -- Otherwise do correct fixed-conversion, but skip these if the
10700 -- Conversion_OK flag is set, because from a semantic point of view
10701 -- these are simple integer conversions needing no further processing
10702 -- (the backend will simply treat them as integers).
10704 if not Conversion_OK (N) then
10705 if Is_Fixed_Point_Type (Etype (N)) then
10706 Expand_Convert_Fixed_To_Fixed (N);
10707 Real_Range_Check;
10709 elsif Is_Integer_Type (Etype (N)) then
10710 Expand_Convert_Fixed_To_Integer (N);
10712 else
10713 pragma Assert (Is_Floating_Point_Type (Etype (N)));
10714 Expand_Convert_Fixed_To_Float (N);
10715 Real_Range_Check;
10716 end if;
10717 end if;
10719 -- Case of conversions to a fixed-point type
10721 -- These conversions require special expansion and processing, found in
10722 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
10723 -- since from a semantic point of view, these are simple integer
10724 -- conversions, which do not need further processing.
10726 elsif Is_Fixed_Point_Type (Target_Type)
10727 and then not Conversion_OK (N)
10728 then
10729 if Is_Integer_Type (Operand_Type) then
10730 Expand_Convert_Integer_To_Fixed (N);
10731 Real_Range_Check;
10732 else
10733 pragma Assert (Is_Floating_Point_Type (Operand_Type));
10734 Expand_Convert_Float_To_Fixed (N);
10735 Real_Range_Check;
10736 end if;
10738 -- Case of float-to-integer conversions
10740 -- We also handle float-to-fixed conversions with Conversion_OK set
10741 -- since semantically the fixed-point target is treated as though it
10742 -- were an integer in such cases.
10744 elsif Is_Floating_Point_Type (Operand_Type)
10745 and then
10746 (Is_Integer_Type (Target_Type)
10747 or else
10748 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
10749 then
10750 -- One more check here, gcc is still not able to do conversions of
10751 -- this type with proper overflow checking, and so gigi is doing an
10752 -- approximation of what is required by doing floating-point compares
10753 -- with the end-point. But that can lose precision in some cases, and
10754 -- give a wrong result. Converting the operand to Universal_Real is
10755 -- helpful, but still does not catch all cases with 64-bit integers
10756 -- on targets with only 64-bit floats.
10758 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
10759 -- Can this code be removed ???
10761 if Do_Range_Check (Operand) then
10762 Rewrite (Operand,
10763 Make_Type_Conversion (Loc,
10764 Subtype_Mark =>
10765 New_Occurrence_Of (Universal_Real, Loc),
10766 Expression =>
10767 Relocate_Node (Operand)));
10769 Set_Etype (Operand, Universal_Real);
10770 Enable_Range_Check (Operand);
10771 Set_Do_Range_Check (Expression (Operand), False);
10772 end if;
10774 -- Case of array conversions
10776 -- Expansion of array conversions, add required length/range checks but
10777 -- only do this if there is no change of representation. For handling of
10778 -- this case, see Handle_Changed_Representation.
10780 elsif Is_Array_Type (Target_Type) then
10781 if Is_Constrained (Target_Type) then
10782 Apply_Length_Check (Operand, Target_Type);
10783 else
10784 Apply_Range_Check (Operand, Target_Type);
10785 end if;
10787 Handle_Changed_Representation;
10789 -- Case of conversions of discriminated types
10791 -- Add required discriminant checks if target is constrained. Again this
10792 -- change is skipped if we have a change of representation.
10794 elsif Has_Discriminants (Target_Type)
10795 and then Is_Constrained (Target_Type)
10796 then
10797 Apply_Discriminant_Check (Operand, Target_Type);
10798 Handle_Changed_Representation;
10800 -- Case of all other record conversions. The only processing required
10801 -- is to check for a change of representation requiring the special
10802 -- assignment processing.
10804 elsif Is_Record_Type (Target_Type) then
10806 -- Ada 2005 (AI-216): Program_Error is raised when converting from
10807 -- a derived Unchecked_Union type to an unconstrained type that is
10808 -- not Unchecked_Union if the operand lacks inferable discriminants.
10810 if Is_Derived_Type (Operand_Type)
10811 and then Is_Unchecked_Union (Base_Type (Operand_Type))
10812 and then not Is_Constrained (Target_Type)
10813 and then not Is_Unchecked_Union (Base_Type (Target_Type))
10814 and then not Has_Inferable_Discriminants (Operand)
10815 then
10816 -- To prevent Gigi from generating illegal code, we generate a
10817 -- Program_Error node, but we give it the target type of the
10818 -- conversion (is this requirement documented somewhere ???)
10820 declare
10821 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
10822 Reason => PE_Unchecked_Union_Restriction);
10824 begin
10825 Set_Etype (PE, Target_Type);
10826 Rewrite (N, PE);
10828 end;
10829 else
10830 Handle_Changed_Representation;
10831 end if;
10833 -- Case of conversions of enumeration types
10835 elsif Is_Enumeration_Type (Target_Type) then
10837 -- Special processing is required if there is a change of
10838 -- representation (from enumeration representation clauses).
10840 if not Same_Representation (Target_Type, Operand_Type) then
10842 -- Convert: x(y) to x'val (ytyp'val (y))
10844 Rewrite (N,
10845 Make_Attribute_Reference (Loc,
10846 Prefix => New_Occurrence_Of (Target_Type, Loc),
10847 Attribute_Name => Name_Val,
10848 Expressions => New_List (
10849 Make_Attribute_Reference (Loc,
10850 Prefix => New_Occurrence_Of (Operand_Type, Loc),
10851 Attribute_Name => Name_Pos,
10852 Expressions => New_List (Operand)))));
10854 Analyze_And_Resolve (N, Target_Type);
10855 end if;
10857 -- Case of conversions to floating-point
10859 elsif Is_Floating_Point_Type (Target_Type) then
10860 Real_Range_Check;
10861 end if;
10863 -- At this stage, either the conversion node has been transformed into
10864 -- some other equivalent expression, or left as a conversion that can be
10865 -- handled by Gigi, in the following cases:
10867 -- Conversions with no change of representation or type
10869 -- Numeric conversions involving integer, floating- and fixed-point
10870 -- values. Fixed-point values are allowed only if Conversion_OK is
10871 -- set, i.e. if the fixed-point values are to be treated as integers.
10873 -- No other conversions should be passed to Gigi
10875 -- Check: are these rules stated in sinfo??? if so, why restate here???
10877 -- The only remaining step is to generate a range check if we still have
10878 -- a type conversion at this stage and Do_Range_Check is set. For now we
10879 -- do this only for conversions of discrete types and for float-to-float
10880 -- conversions.
10882 if Nkind (N) = N_Type_Conversion then
10884 -- For now we only support floating-point cases where both source
10885 -- and target are floating-point types. Conversions where the source
10886 -- and target involve integer or fixed-point types are still TBD,
10887 -- though not clear whether those can even happen at this point, due
10888 -- to transformations above. ???
10890 if Is_Floating_Point_Type (Etype (N))
10891 and then Is_Floating_Point_Type (Etype (Expression (N)))
10892 then
10893 if Do_Range_Check (Expression (N))
10894 and then Is_Floating_Point_Type (Target_Type)
10895 then
10896 Generate_Range_Check
10897 (Expression (N), Target_Type, CE_Range_Check_Failed);
10898 end if;
10900 -- Discrete-to-discrete conversions
10902 elsif Is_Discrete_Type (Etype (N)) then
10903 declare
10904 Expr : constant Node_Id := Expression (N);
10905 Ftyp : Entity_Id;
10906 Ityp : Entity_Id;
10908 begin
10909 if Do_Range_Check (Expr)
10910 and then Is_Discrete_Type (Etype (Expr))
10911 then
10912 Set_Do_Range_Check (Expr, False);
10914 -- Before we do a range check, we have to deal with treating
10915 -- a fixed-point operand as an integer. The way we do this
10916 -- is simply to do an unchecked conversion to an appropriate
10917 -- integer type large enough to hold the result.
10919 -- This code is not active yet, because we are only dealing
10920 -- with discrete types so far ???
10922 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
10923 and then Treat_Fixed_As_Integer (Expr)
10924 then
10925 Ftyp := Base_Type (Etype (Expr));
10927 if Esize (Ftyp) >= Esize (Standard_Integer) then
10928 Ityp := Standard_Long_Long_Integer;
10929 else
10930 Ityp := Standard_Integer;
10931 end if;
10933 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
10934 end if;
10936 -- Reset overflow flag, since the range check will include
10937 -- dealing with possible overflow, and generate the check.
10938 -- If Address is either a source type or target type,
10939 -- suppress range check to avoid typing anomalies when
10940 -- it is a visible integer type.
10942 Set_Do_Overflow_Check (N, False);
10944 if not Is_Descendent_Of_Address (Etype (Expr))
10945 and then not Is_Descendent_Of_Address (Target_Type)
10946 then
10947 Generate_Range_Check
10948 (Expr, Target_Type, CE_Range_Check_Failed);
10949 end if;
10950 end if;
10951 end;
10952 end if;
10953 end if;
10955 -- Here at end of processing
10957 <<Done>>
10958 -- Apply predicate check if required. Note that we can't just call
10959 -- Apply_Predicate_Check here, because the type looks right after
10960 -- the conversion and it would omit the check. The Comes_From_Source
10961 -- guard is necessary to prevent infinite recursions when we generate
10962 -- internal conversions for the purpose of checking predicates.
10964 if Present (Predicate_Function (Target_Type))
10965 and then Target_Type /= Operand_Type
10966 and then Comes_From_Source (N)
10967 then
10968 declare
10969 New_Expr : constant Node_Id := Duplicate_Subexpr (N);
10971 begin
10972 -- Avoid infinite recursion on the subsequent expansion of
10973 -- of the copy of the original type conversion.
10975 Set_Comes_From_Source (New_Expr, False);
10976 Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr));
10977 end;
10978 end if;
10979 end Expand_N_Type_Conversion;
10981 -----------------------------------
10982 -- Expand_N_Unchecked_Expression --
10983 -----------------------------------
10985 -- Remove the unchecked expression node from the tree. Its job was simply
10986 -- to make sure that its constituent expression was handled with checks
10987 -- off, and now that that is done, we can remove it from the tree, and
10988 -- indeed must, since Gigi does not expect to see these nodes.
10990 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
10991 Exp : constant Node_Id := Expression (N);
10992 begin
10993 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
10994 Rewrite (N, Exp);
10995 end Expand_N_Unchecked_Expression;
10997 ----------------------------------------
10998 -- Expand_N_Unchecked_Type_Conversion --
10999 ----------------------------------------
11001 -- If this cannot be handled by Gigi and we haven't already made a
11002 -- temporary for it, do it now.
11004 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
11005 Target_Type : constant Entity_Id := Etype (N);
11006 Operand : constant Node_Id := Expression (N);
11007 Operand_Type : constant Entity_Id := Etype (Operand);
11009 begin
11010 -- Nothing at all to do if conversion is to the identical type so remove
11011 -- the conversion completely, it is useless, except that it may carry
11012 -- an Assignment_OK indication which must be propagated to the operand.
11014 if Operand_Type = Target_Type then
11016 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
11018 if Assignment_OK (N) then
11019 Set_Assignment_OK (Operand);
11020 end if;
11022 Rewrite (N, Relocate_Node (Operand));
11023 return;
11024 end if;
11026 -- If we have a conversion of a compile time known value to a target
11027 -- type and the value is in range of the target type, then we can simply
11028 -- replace the construct by an integer literal of the correct type. We
11029 -- only apply this to integer types being converted. Possibly it may
11030 -- apply in other cases, but it is too much trouble to worry about.
11032 -- Note that we do not do this transformation if the Kill_Range_Check
11033 -- flag is set, since then the value may be outside the expected range.
11034 -- This happens in the Normalize_Scalars case.
11036 -- We also skip this if either the target or operand type is biased
11037 -- because in this case, the unchecked conversion is supposed to
11038 -- preserve the bit pattern, not the integer value.
11040 if Is_Integer_Type (Target_Type)
11041 and then not Has_Biased_Representation (Target_Type)
11042 and then Is_Integer_Type (Operand_Type)
11043 and then not Has_Biased_Representation (Operand_Type)
11044 and then Compile_Time_Known_Value (Operand)
11045 and then not Kill_Range_Check (N)
11046 then
11047 declare
11048 Val : constant Uint := Expr_Value (Operand);
11050 begin
11051 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
11052 and then
11053 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
11054 and then
11055 Val >= Expr_Value (Type_Low_Bound (Target_Type))
11056 and then
11057 Val <= Expr_Value (Type_High_Bound (Target_Type))
11058 then
11059 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
11061 -- If Address is the target type, just set the type to avoid a
11062 -- spurious type error on the literal when Address is a visible
11063 -- integer type.
11065 if Is_Descendent_Of_Address (Target_Type) then
11066 Set_Etype (N, Target_Type);
11067 else
11068 Analyze_And_Resolve (N, Target_Type);
11069 end if;
11071 return;
11072 end if;
11073 end;
11074 end if;
11076 -- Nothing to do if conversion is safe
11078 if Safe_Unchecked_Type_Conversion (N) then
11079 return;
11080 end if;
11082 -- Otherwise force evaluation unless Assignment_OK flag is set (this
11083 -- flag indicates ??? More comments needed here)
11085 if Assignment_OK (N) then
11086 null;
11087 else
11088 Force_Evaluation (N);
11089 end if;
11090 end Expand_N_Unchecked_Type_Conversion;
11092 ----------------------------
11093 -- Expand_Record_Equality --
11094 ----------------------------
11096 -- For non-variant records, Equality is expanded when needed into:
11098 -- and then Lhs.Discr1 = Rhs.Discr1
11099 -- and then ...
11100 -- and then Lhs.Discrn = Rhs.Discrn
11101 -- and then Lhs.Cmp1 = Rhs.Cmp1
11102 -- and then ...
11103 -- and then Lhs.Cmpn = Rhs.Cmpn
11105 -- The expression is folded by the back-end for adjacent fields. This
11106 -- function is called for tagged record in only one occasion: for imple-
11107 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
11108 -- otherwise the primitive "=" is used directly.
11110 function Expand_Record_Equality
11111 (Nod : Node_Id;
11112 Typ : Entity_Id;
11113 Lhs : Node_Id;
11114 Rhs : Node_Id;
11115 Bodies : List_Id) return Node_Id
11117 Loc : constant Source_Ptr := Sloc (Nod);
11119 Result : Node_Id;
11120 C : Entity_Id;
11122 First_Time : Boolean := True;
11124 function Element_To_Compare (C : Entity_Id) return Entity_Id;
11125 -- Return the next discriminant or component to compare, starting with
11126 -- C, skipping inherited components.
11128 ------------------------
11129 -- Element_To_Compare --
11130 ------------------------
11132 function Element_To_Compare (C : Entity_Id) return Entity_Id is
11133 Comp : Entity_Id;
11135 begin
11136 Comp := C;
11137 loop
11138 -- Exit loop when the next element to be compared is found, or
11139 -- there is no more such element.
11141 exit when No (Comp);
11143 exit when Ekind_In (Comp, E_Discriminant, E_Component)
11144 and then not (
11146 -- Skip inherited components
11148 -- Note: for a tagged type, we always generate the "=" primitive
11149 -- for the base type (not on the first subtype), so the test for
11150 -- Comp /= Original_Record_Component (Comp) is True for
11151 -- inherited components only.
11153 (Is_Tagged_Type (Typ)
11154 and then Comp /= Original_Record_Component (Comp))
11156 -- Skip _Tag
11158 or else Chars (Comp) = Name_uTag
11160 -- The .NET/JVM version of type Root_Controlled contains two
11161 -- fields which should not be considered part of the object. To
11162 -- achieve proper equiality between two controlled objects on
11163 -- .NET/JVM, skip _Parent whenever it has type Root_Controlled.
11165 or else (Chars (Comp) = Name_uParent
11166 and then VM_Target /= No_VM
11167 and then Etype (Comp) = RTE (RE_Root_Controlled))
11169 -- Skip interface elements (secondary tags???)
11171 or else Is_Interface (Etype (Comp)));
11173 Next_Entity (Comp);
11174 end loop;
11176 return Comp;
11177 end Element_To_Compare;
11179 -- Start of processing for Expand_Record_Equality
11181 begin
11182 -- Generates the following code: (assuming that Typ has one Discr and
11183 -- component C2 is also a record)
11185 -- True
11186 -- and then Lhs.Discr1 = Rhs.Discr1
11187 -- and then Lhs.C1 = Rhs.C1
11188 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
11189 -- and then ...
11190 -- and then Lhs.Cmpn = Rhs.Cmpn
11192 Result := New_Occurrence_Of (Standard_True, Loc);
11193 C := Element_To_Compare (First_Entity (Typ));
11194 while Present (C) loop
11195 declare
11196 New_Lhs : Node_Id;
11197 New_Rhs : Node_Id;
11198 Check : Node_Id;
11200 begin
11201 if First_Time then
11202 First_Time := False;
11203 New_Lhs := Lhs;
11204 New_Rhs := Rhs;
11205 else
11206 New_Lhs := New_Copy_Tree (Lhs);
11207 New_Rhs := New_Copy_Tree (Rhs);
11208 end if;
11210 Check :=
11211 Expand_Composite_Equality (Nod, Etype (C),
11212 Lhs =>
11213 Make_Selected_Component (Loc,
11214 Prefix => New_Lhs,
11215 Selector_Name => New_Occurrence_Of (C, Loc)),
11216 Rhs =>
11217 Make_Selected_Component (Loc,
11218 Prefix => New_Rhs,
11219 Selector_Name => New_Occurrence_Of (C, Loc)),
11220 Bodies => Bodies);
11222 -- If some (sub)component is an unchecked_union, the whole
11223 -- operation will raise program error.
11225 if Nkind (Check) = N_Raise_Program_Error then
11226 Result := Check;
11227 Set_Etype (Result, Standard_Boolean);
11228 exit;
11229 else
11230 Result :=
11231 Make_And_Then (Loc,
11232 Left_Opnd => Result,
11233 Right_Opnd => Check);
11234 end if;
11235 end;
11237 C := Element_To_Compare (Next_Entity (C));
11238 end loop;
11240 return Result;
11241 end Expand_Record_Equality;
11243 ---------------------------
11244 -- Expand_Set_Membership --
11245 ---------------------------
11247 procedure Expand_Set_Membership (N : Node_Id) is
11248 Lop : constant Node_Id := Left_Opnd (N);
11249 Alt : Node_Id;
11250 Res : Node_Id;
11252 function Make_Cond (Alt : Node_Id) return Node_Id;
11253 -- If the alternative is a subtype mark, create a simple membership
11254 -- test. Otherwise create an equality test for it.
11256 ---------------
11257 -- Make_Cond --
11258 ---------------
11260 function Make_Cond (Alt : Node_Id) return Node_Id is
11261 Cond : Node_Id;
11262 L : constant Node_Id := New_Copy (Lop);
11263 R : constant Node_Id := Relocate_Node (Alt);
11265 begin
11266 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
11267 or else Nkind (Alt) = N_Range
11268 then
11269 Cond :=
11270 Make_In (Sloc (Alt),
11271 Left_Opnd => L,
11272 Right_Opnd => R);
11273 else
11274 Cond :=
11275 Make_Op_Eq (Sloc (Alt),
11276 Left_Opnd => L,
11277 Right_Opnd => R);
11278 end if;
11280 return Cond;
11281 end Make_Cond;
11283 -- Start of processing for Expand_Set_Membership
11285 begin
11286 Remove_Side_Effects (Lop);
11288 Alt := Last (Alternatives (N));
11289 Res := Make_Cond (Alt);
11291 Prev (Alt);
11292 while Present (Alt) loop
11293 Res :=
11294 Make_Or_Else (Sloc (Alt),
11295 Left_Opnd => Make_Cond (Alt),
11296 Right_Opnd => Res);
11297 Prev (Alt);
11298 end loop;
11300 Rewrite (N, Res);
11301 Analyze_And_Resolve (N, Standard_Boolean);
11302 end Expand_Set_Membership;
11304 -----------------------------------
11305 -- Expand_Short_Circuit_Operator --
11306 -----------------------------------
11308 -- Deal with special expansion if actions are present for the right operand
11309 -- and deal with optimizing case of arguments being True or False. We also
11310 -- deal with the special case of non-standard boolean values.
11312 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
11313 Loc : constant Source_Ptr := Sloc (N);
11314 Typ : constant Entity_Id := Etype (N);
11315 Left : constant Node_Id := Left_Opnd (N);
11316 Right : constant Node_Id := Right_Opnd (N);
11317 LocR : constant Source_Ptr := Sloc (Right);
11318 Actlist : List_Id;
11320 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
11321 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
11322 -- If Left = Shortcut_Value then Right need not be evaluated
11324 begin
11325 -- Deal with non-standard booleans
11327 if Is_Boolean_Type (Typ) then
11328 Adjust_Condition (Left);
11329 Adjust_Condition (Right);
11330 Set_Etype (N, Standard_Boolean);
11331 end if;
11333 -- Check for cases where left argument is known to be True or False
11335 if Compile_Time_Known_Value (Left) then
11337 -- Mark SCO for left condition as compile time known
11339 if Generate_SCO and then Comes_From_Source (Left) then
11340 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
11341 end if;
11343 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
11344 -- Any actions associated with Right will be executed unconditionally
11345 -- and can thus be inserted into the tree unconditionally.
11347 if Expr_Value_E (Left) /= Shortcut_Ent then
11348 if Present (Actions (N)) then
11349 Insert_Actions (N, Actions (N));
11350 end if;
11352 Rewrite (N, Right);
11354 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
11355 -- In this case we can forget the actions associated with Right,
11356 -- since they will never be executed.
11358 else
11359 Kill_Dead_Code (Right);
11360 Kill_Dead_Code (Actions (N));
11361 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
11362 end if;
11364 Adjust_Result_Type (N, Typ);
11365 return;
11366 end if;
11368 -- If Actions are present for the right operand, we have to do some
11369 -- special processing. We can't just let these actions filter back into
11370 -- code preceding the short circuit (which is what would have happened
11371 -- if we had not trapped them in the short-circuit form), since they
11372 -- must only be executed if the right operand of the short circuit is
11373 -- executed and not otherwise.
11375 if Present (Actions (N)) then
11376 Actlist := Actions (N);
11378 -- We now use an Expression_With_Actions node for the right operand
11379 -- of the short-circuit form. Note that this solves the traceability
11380 -- problems for coverage analysis.
11382 Rewrite (Right,
11383 Make_Expression_With_Actions (LocR,
11384 Expression => Relocate_Node (Right),
11385 Actions => Actlist));
11386 Set_Actions (N, No_List);
11387 Analyze_And_Resolve (Right, Standard_Boolean);
11389 Adjust_Result_Type (N, Typ);
11390 return;
11391 end if;
11393 -- No actions present, check for cases of right argument True/False
11395 if Compile_Time_Known_Value (Right) then
11397 -- Mark SCO for left condition as compile time known
11399 if Generate_SCO and then Comes_From_Source (Right) then
11400 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
11401 end if;
11403 -- Change (Left and then True), (Left or else False) to Left.
11404 -- Note that we know there are no actions associated with the right
11405 -- operand, since we just checked for this case above.
11407 if Expr_Value_E (Right) /= Shortcut_Ent then
11408 Rewrite (N, Left);
11410 -- Change (Left and then False), (Left or else True) to Right,
11411 -- making sure to preserve any side effects associated with the Left
11412 -- operand.
11414 else
11415 Remove_Side_Effects (Left);
11416 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
11417 end if;
11418 end if;
11420 Adjust_Result_Type (N, Typ);
11421 end Expand_Short_Circuit_Operator;
11423 -------------------------------------
11424 -- Fixup_Universal_Fixed_Operation --
11425 -------------------------------------
11427 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
11428 Conv : constant Node_Id := Parent (N);
11430 begin
11431 -- We must have a type conversion immediately above us
11433 pragma Assert (Nkind (Conv) = N_Type_Conversion);
11435 -- Normally the type conversion gives our target type. The exception
11436 -- occurs in the case of the Round attribute, where the conversion
11437 -- will be to universal real, and our real type comes from the Round
11438 -- attribute (as well as an indication that we must round the result)
11440 if Nkind (Parent (Conv)) = N_Attribute_Reference
11441 and then Attribute_Name (Parent (Conv)) = Name_Round
11442 then
11443 Set_Etype (N, Etype (Parent (Conv)));
11444 Set_Rounded_Result (N);
11446 -- Normal case where type comes from conversion above us
11448 else
11449 Set_Etype (N, Etype (Conv));
11450 end if;
11451 end Fixup_Universal_Fixed_Operation;
11453 ---------------------------------
11454 -- Has_Inferable_Discriminants --
11455 ---------------------------------
11457 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
11459 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
11460 -- Determines whether the left-most prefix of a selected component is a
11461 -- formal parameter in a subprogram. Assumes N is a selected component.
11463 --------------------------------
11464 -- Prefix_Is_Formal_Parameter --
11465 --------------------------------
11467 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
11468 Sel_Comp : Node_Id;
11470 begin
11471 -- Move to the left-most prefix by climbing up the tree
11473 Sel_Comp := N;
11474 while Present (Parent (Sel_Comp))
11475 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
11476 loop
11477 Sel_Comp := Parent (Sel_Comp);
11478 end loop;
11480 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
11481 end Prefix_Is_Formal_Parameter;
11483 -- Start of processing for Has_Inferable_Discriminants
11485 begin
11486 -- For selected components, the subtype of the selector must be a
11487 -- constrained Unchecked_Union. If the component is subject to a
11488 -- per-object constraint, then the enclosing object must have inferable
11489 -- discriminants.
11491 if Nkind (N) = N_Selected_Component then
11492 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
11494 -- A small hack. If we have a per-object constrained selected
11495 -- component of a formal parameter, return True since we do not
11496 -- know the actual parameter association yet.
11498 if Prefix_Is_Formal_Parameter (N) then
11499 return True;
11501 -- Otherwise, check the enclosing object and the selector
11503 else
11504 return Has_Inferable_Discriminants (Prefix (N))
11505 and then Has_Inferable_Discriminants (Selector_Name (N));
11506 end if;
11508 -- The call to Has_Inferable_Discriminants will determine whether
11509 -- the selector has a constrained Unchecked_Union nominal type.
11511 else
11512 return Has_Inferable_Discriminants (Selector_Name (N));
11513 end if;
11515 -- A qualified expression has inferable discriminants if its subtype
11516 -- mark is a constrained Unchecked_Union subtype.
11518 elsif Nkind (N) = N_Qualified_Expression then
11519 return Is_Unchecked_Union (Etype (Subtype_Mark (N)))
11520 and then Is_Constrained (Etype (Subtype_Mark (N)));
11522 -- For all other names, it is sufficient to have a constrained
11523 -- Unchecked_Union nominal subtype.
11525 else
11526 return Is_Unchecked_Union (Base_Type (Etype (N)))
11527 and then Is_Constrained (Etype (N));
11528 end if;
11529 end Has_Inferable_Discriminants;
11531 -------------------------------
11532 -- Insert_Dereference_Action --
11533 -------------------------------
11535 procedure Insert_Dereference_Action (N : Node_Id) is
11537 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
11538 -- Return true if type of P is derived from Checked_Pool;
11540 -----------------------------
11541 -- Is_Checked_Storage_Pool --
11542 -----------------------------
11544 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
11545 T : Entity_Id;
11547 begin
11548 if No (P) then
11549 return False;
11550 end if;
11552 T := Etype (P);
11553 while T /= Etype (T) loop
11554 if Is_RTE (T, RE_Checked_Pool) then
11555 return True;
11556 else
11557 T := Etype (T);
11558 end if;
11559 end loop;
11561 return False;
11562 end Is_Checked_Storage_Pool;
11564 -- Local variables
11566 Typ : constant Entity_Id := Etype (N);
11567 Desig : constant Entity_Id := Available_View (Designated_Type (Typ));
11568 Loc : constant Source_Ptr := Sloc (N);
11569 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
11570 Pnod : constant Node_Id := Parent (N);
11572 Addr : Entity_Id;
11573 Alig : Entity_Id;
11574 Deref : Node_Id;
11575 Size : Entity_Id;
11576 Stmt : Node_Id;
11578 -- Start of processing for Insert_Dereference_Action
11580 begin
11581 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
11583 -- Do not re-expand a dereference which has already been processed by
11584 -- this routine.
11586 if Has_Dereference_Action (Pnod) then
11587 return;
11589 -- Do not perform this type of expansion for internally-generated
11590 -- dereferences.
11592 elsif not Comes_From_Source (Original_Node (Pnod)) then
11593 return;
11595 -- A dereference action is only applicable to objects which have been
11596 -- allocated on a checked pool.
11598 elsif not Is_Checked_Storage_Pool (Pool) then
11599 return;
11600 end if;
11602 -- Extract the address of the dereferenced object. Generate:
11604 -- Addr : System.Address := <N>'Pool_Address;
11606 Addr := Make_Temporary (Loc, 'P');
11608 Insert_Action (N,
11609 Make_Object_Declaration (Loc,
11610 Defining_Identifier => Addr,
11611 Object_Definition =>
11612 New_Occurrence_Of (RTE (RE_Address), Loc),
11613 Expression =>
11614 Make_Attribute_Reference (Loc,
11615 Prefix => Duplicate_Subexpr_Move_Checks (N),
11616 Attribute_Name => Name_Pool_Address)));
11618 -- Calculate the size of the dereferenced object. Generate:
11620 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
11622 Deref :=
11623 Make_Explicit_Dereference (Loc,
11624 Prefix => Duplicate_Subexpr_Move_Checks (N));
11625 Set_Has_Dereference_Action (Deref);
11627 Size := Make_Temporary (Loc, 'S');
11629 Insert_Action (N,
11630 Make_Object_Declaration (Loc,
11631 Defining_Identifier => Size,
11633 Object_Definition =>
11634 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
11636 Expression =>
11637 Make_Op_Divide (Loc,
11638 Left_Opnd =>
11639 Make_Attribute_Reference (Loc,
11640 Prefix => Deref,
11641 Attribute_Name => Name_Size),
11642 Right_Opnd =>
11643 Make_Integer_Literal (Loc, System_Storage_Unit))));
11645 -- Calculate the alignment of the dereferenced object. Generate:
11646 -- Alig : constant Storage_Count := <N>.all'Alignment;
11648 Deref :=
11649 Make_Explicit_Dereference (Loc,
11650 Prefix => Duplicate_Subexpr_Move_Checks (N));
11651 Set_Has_Dereference_Action (Deref);
11653 Alig := Make_Temporary (Loc, 'A');
11655 Insert_Action (N,
11656 Make_Object_Declaration (Loc,
11657 Defining_Identifier => Alig,
11658 Object_Definition =>
11659 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
11660 Expression =>
11661 Make_Attribute_Reference (Loc,
11662 Prefix => Deref,
11663 Attribute_Name => Name_Alignment)));
11665 -- A dereference of a controlled object requires special processing. The
11666 -- finalization machinery requests additional space from the underlying
11667 -- pool to allocate and hide two pointers. As a result, a checked pool
11668 -- may mark the wrong memory as valid. Since checked pools do not have
11669 -- knowledge of hidden pointers, we have to bring the two pointers back
11670 -- in view in order to restore the original state of the object.
11672 if Needs_Finalization (Desig) then
11674 -- Adjust the address and size of the dereferenced object. Generate:
11675 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
11677 Stmt :=
11678 Make_Procedure_Call_Statement (Loc,
11679 Name =>
11680 New_Occurrence_Of (RTE (RE_Adjust_Controlled_Dereference), Loc),
11681 Parameter_Associations => New_List (
11682 New_Occurrence_Of (Addr, Loc),
11683 New_Occurrence_Of (Size, Loc),
11684 New_Occurrence_Of (Alig, Loc)));
11686 -- Class-wide types complicate things because we cannot determine
11687 -- statically whether the actual object is truly controlled. We must
11688 -- generate a runtime check to detect this property. Generate:
11690 -- if Needs_Finalization (<N>.all'Tag) then
11691 -- <Stmt>;
11692 -- end if;
11694 if Is_Class_Wide_Type (Desig) then
11695 Deref :=
11696 Make_Explicit_Dereference (Loc,
11697 Prefix => Duplicate_Subexpr_Move_Checks (N));
11698 Set_Has_Dereference_Action (Deref);
11700 Stmt :=
11701 Make_Implicit_If_Statement (N,
11702 Condition =>
11703 Make_Function_Call (Loc,
11704 Name =>
11705 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
11706 Parameter_Associations => New_List (
11707 Make_Attribute_Reference (Loc,
11708 Prefix => Deref,
11709 Attribute_Name => Name_Tag))),
11710 Then_Statements => New_List (Stmt));
11711 end if;
11713 Insert_Action (N, Stmt);
11714 end if;
11716 -- Generate:
11717 -- Dereference (Pool, Addr, Size, Alig);
11719 Insert_Action (N,
11720 Make_Procedure_Call_Statement (Loc,
11721 Name =>
11722 New_Occurrence_Of
11723 (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
11724 Parameter_Associations => New_List (
11725 New_Occurrence_Of (Pool, Loc),
11726 New_Occurrence_Of (Addr, Loc),
11727 New_Occurrence_Of (Size, Loc),
11728 New_Occurrence_Of (Alig, Loc))));
11730 -- Mark the explicit dereference as processed to avoid potential
11731 -- infinite expansion.
11733 Set_Has_Dereference_Action (Pnod);
11735 exception
11736 when RE_Not_Available =>
11737 return;
11738 end Insert_Dereference_Action;
11740 --------------------------------
11741 -- Integer_Promotion_Possible --
11742 --------------------------------
11744 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
11745 Operand : constant Node_Id := Expression (N);
11746 Operand_Type : constant Entity_Id := Etype (Operand);
11747 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
11749 begin
11750 pragma Assert (Nkind (N) = N_Type_Conversion);
11752 return
11754 -- We only do the transformation for source constructs. We assume
11755 -- that the expander knows what it is doing when it generates code.
11757 Comes_From_Source (N)
11759 -- If the operand type is Short_Integer or Short_Short_Integer,
11760 -- then we will promote to Integer, which is available on all
11761 -- targets, and is sufficient to ensure no intermediate overflow.
11762 -- Furthermore it is likely to be as efficient or more efficient
11763 -- than using the smaller type for the computation so we do this
11764 -- unconditionally.
11766 and then
11767 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
11768 or else
11769 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
11771 -- Test for interesting operation, which includes addition,
11772 -- division, exponentiation, multiplication, subtraction, absolute
11773 -- value and unary negation. Unary "+" is omitted since it is a
11774 -- no-op and thus can't overflow.
11776 and then Nkind_In (Operand, N_Op_Abs,
11777 N_Op_Add,
11778 N_Op_Divide,
11779 N_Op_Expon,
11780 N_Op_Minus,
11781 N_Op_Multiply,
11782 N_Op_Subtract);
11783 end Integer_Promotion_Possible;
11785 ------------------------------
11786 -- Make_Array_Comparison_Op --
11787 ------------------------------
11789 -- This is a hand-coded expansion of the following generic function:
11791 -- generic
11792 -- type elem is (<>);
11793 -- type index is (<>);
11794 -- type a is array (index range <>) of elem;
11796 -- function Gnnn (X : a; Y: a) return boolean is
11797 -- J : index := Y'first;
11799 -- begin
11800 -- if X'length = 0 then
11801 -- return false;
11803 -- elsif Y'length = 0 then
11804 -- return true;
11806 -- else
11807 -- for I in X'range loop
11808 -- if X (I) = Y (J) then
11809 -- if J = Y'last then
11810 -- exit;
11811 -- else
11812 -- J := index'succ (J);
11813 -- end if;
11815 -- else
11816 -- return X (I) > Y (J);
11817 -- end if;
11818 -- end loop;
11820 -- return X'length > Y'length;
11821 -- end if;
11822 -- end Gnnn;
11824 -- Note that since we are essentially doing this expansion by hand, we
11825 -- do not need to generate an actual or formal generic part, just the
11826 -- instantiated function itself.
11828 -- Perhaps we could have the actual generic available in the run-time,
11829 -- obtained by rtsfind, and actually expand a real instantiation ???
11831 function Make_Array_Comparison_Op
11832 (Typ : Entity_Id;
11833 Nod : Node_Id) return Node_Id
11835 Loc : constant Source_Ptr := Sloc (Nod);
11837 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
11838 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
11839 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
11840 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
11842 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
11844 Loop_Statement : Node_Id;
11845 Loop_Body : Node_Id;
11846 If_Stat : Node_Id;
11847 Inner_If : Node_Id;
11848 Final_Expr : Node_Id;
11849 Func_Body : Node_Id;
11850 Func_Name : Entity_Id;
11851 Formals : List_Id;
11852 Length1 : Node_Id;
11853 Length2 : Node_Id;
11855 begin
11856 -- if J = Y'last then
11857 -- exit;
11858 -- else
11859 -- J := index'succ (J);
11860 -- end if;
11862 Inner_If :=
11863 Make_Implicit_If_Statement (Nod,
11864 Condition =>
11865 Make_Op_Eq (Loc,
11866 Left_Opnd => New_Occurrence_Of (J, Loc),
11867 Right_Opnd =>
11868 Make_Attribute_Reference (Loc,
11869 Prefix => New_Occurrence_Of (Y, Loc),
11870 Attribute_Name => Name_Last)),
11872 Then_Statements => New_List (
11873 Make_Exit_Statement (Loc)),
11875 Else_Statements =>
11876 New_List (
11877 Make_Assignment_Statement (Loc,
11878 Name => New_Occurrence_Of (J, Loc),
11879 Expression =>
11880 Make_Attribute_Reference (Loc,
11881 Prefix => New_Occurrence_Of (Index, Loc),
11882 Attribute_Name => Name_Succ,
11883 Expressions => New_List (New_Occurrence_Of (J, Loc))))));
11885 -- if X (I) = Y (J) then
11886 -- if ... end if;
11887 -- else
11888 -- return X (I) > Y (J);
11889 -- end if;
11891 Loop_Body :=
11892 Make_Implicit_If_Statement (Nod,
11893 Condition =>
11894 Make_Op_Eq (Loc,
11895 Left_Opnd =>
11896 Make_Indexed_Component (Loc,
11897 Prefix => New_Occurrence_Of (X, Loc),
11898 Expressions => New_List (New_Occurrence_Of (I, Loc))),
11900 Right_Opnd =>
11901 Make_Indexed_Component (Loc,
11902 Prefix => New_Occurrence_Of (Y, Loc),
11903 Expressions => New_List (New_Occurrence_Of (J, Loc)))),
11905 Then_Statements => New_List (Inner_If),
11907 Else_Statements => New_List (
11908 Make_Simple_Return_Statement (Loc,
11909 Expression =>
11910 Make_Op_Gt (Loc,
11911 Left_Opnd =>
11912 Make_Indexed_Component (Loc,
11913 Prefix => New_Occurrence_Of (X, Loc),
11914 Expressions => New_List (New_Occurrence_Of (I, Loc))),
11916 Right_Opnd =>
11917 Make_Indexed_Component (Loc,
11918 Prefix => New_Occurrence_Of (Y, Loc),
11919 Expressions => New_List (
11920 New_Occurrence_Of (J, Loc)))))));
11922 -- for I in X'range loop
11923 -- if ... end if;
11924 -- end loop;
11926 Loop_Statement :=
11927 Make_Implicit_Loop_Statement (Nod,
11928 Identifier => Empty,
11930 Iteration_Scheme =>
11931 Make_Iteration_Scheme (Loc,
11932 Loop_Parameter_Specification =>
11933 Make_Loop_Parameter_Specification (Loc,
11934 Defining_Identifier => I,
11935 Discrete_Subtype_Definition =>
11936 Make_Attribute_Reference (Loc,
11937 Prefix => New_Occurrence_Of (X, Loc),
11938 Attribute_Name => Name_Range))),
11940 Statements => New_List (Loop_Body));
11942 -- if X'length = 0 then
11943 -- return false;
11944 -- elsif Y'length = 0 then
11945 -- return true;
11946 -- else
11947 -- for ... loop ... end loop;
11948 -- return X'length > Y'length;
11949 -- end if;
11951 Length1 :=
11952 Make_Attribute_Reference (Loc,
11953 Prefix => New_Occurrence_Of (X, Loc),
11954 Attribute_Name => Name_Length);
11956 Length2 :=
11957 Make_Attribute_Reference (Loc,
11958 Prefix => New_Occurrence_Of (Y, Loc),
11959 Attribute_Name => Name_Length);
11961 Final_Expr :=
11962 Make_Op_Gt (Loc,
11963 Left_Opnd => Length1,
11964 Right_Opnd => Length2);
11966 If_Stat :=
11967 Make_Implicit_If_Statement (Nod,
11968 Condition =>
11969 Make_Op_Eq (Loc,
11970 Left_Opnd =>
11971 Make_Attribute_Reference (Loc,
11972 Prefix => New_Occurrence_Of (X, Loc),
11973 Attribute_Name => Name_Length),
11974 Right_Opnd =>
11975 Make_Integer_Literal (Loc, 0)),
11977 Then_Statements =>
11978 New_List (
11979 Make_Simple_Return_Statement (Loc,
11980 Expression => New_Occurrence_Of (Standard_False, Loc))),
11982 Elsif_Parts => New_List (
11983 Make_Elsif_Part (Loc,
11984 Condition =>
11985 Make_Op_Eq (Loc,
11986 Left_Opnd =>
11987 Make_Attribute_Reference (Loc,
11988 Prefix => New_Occurrence_Of (Y, Loc),
11989 Attribute_Name => Name_Length),
11990 Right_Opnd =>
11991 Make_Integer_Literal (Loc, 0)),
11993 Then_Statements =>
11994 New_List (
11995 Make_Simple_Return_Statement (Loc,
11996 Expression => New_Occurrence_Of (Standard_True, Loc))))),
11998 Else_Statements => New_List (
11999 Loop_Statement,
12000 Make_Simple_Return_Statement (Loc,
12001 Expression => Final_Expr)));
12003 -- (X : a; Y: a)
12005 Formals := New_List (
12006 Make_Parameter_Specification (Loc,
12007 Defining_Identifier => X,
12008 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
12010 Make_Parameter_Specification (Loc,
12011 Defining_Identifier => Y,
12012 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
12014 -- function Gnnn (...) return boolean is
12015 -- J : index := Y'first;
12016 -- begin
12017 -- if ... end if;
12018 -- end Gnnn;
12020 Func_Name := Make_Temporary (Loc, 'G');
12022 Func_Body :=
12023 Make_Subprogram_Body (Loc,
12024 Specification =>
12025 Make_Function_Specification (Loc,
12026 Defining_Unit_Name => Func_Name,
12027 Parameter_Specifications => Formals,
12028 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
12030 Declarations => New_List (
12031 Make_Object_Declaration (Loc,
12032 Defining_Identifier => J,
12033 Object_Definition => New_Occurrence_Of (Index, Loc),
12034 Expression =>
12035 Make_Attribute_Reference (Loc,
12036 Prefix => New_Occurrence_Of (Y, Loc),
12037 Attribute_Name => Name_First))),
12039 Handled_Statement_Sequence =>
12040 Make_Handled_Sequence_Of_Statements (Loc,
12041 Statements => New_List (If_Stat)));
12043 return Func_Body;
12044 end Make_Array_Comparison_Op;
12046 ---------------------------
12047 -- Make_Boolean_Array_Op --
12048 ---------------------------
12050 -- For logical operations on boolean arrays, expand in line the following,
12051 -- replacing 'and' with 'or' or 'xor' where needed:
12053 -- function Annn (A : typ; B: typ) return typ is
12054 -- C : typ;
12055 -- begin
12056 -- for J in A'range loop
12057 -- C (J) := A (J) op B (J);
12058 -- end loop;
12059 -- return C;
12060 -- end Annn;
12062 -- Here typ is the boolean array type
12064 function Make_Boolean_Array_Op
12065 (Typ : Entity_Id;
12066 N : Node_Id) return Node_Id
12068 Loc : constant Source_Ptr := Sloc (N);
12070 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
12071 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
12072 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
12073 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
12075 A_J : Node_Id;
12076 B_J : Node_Id;
12077 C_J : Node_Id;
12078 Op : Node_Id;
12080 Formals : List_Id;
12081 Func_Name : Entity_Id;
12082 Func_Body : Node_Id;
12083 Loop_Statement : Node_Id;
12085 begin
12086 A_J :=
12087 Make_Indexed_Component (Loc,
12088 Prefix => New_Occurrence_Of (A, Loc),
12089 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12091 B_J :=
12092 Make_Indexed_Component (Loc,
12093 Prefix => New_Occurrence_Of (B, Loc),
12094 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12096 C_J :=
12097 Make_Indexed_Component (Loc,
12098 Prefix => New_Occurrence_Of (C, Loc),
12099 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12101 if Nkind (N) = N_Op_And then
12102 Op :=
12103 Make_Op_And (Loc,
12104 Left_Opnd => A_J,
12105 Right_Opnd => B_J);
12107 elsif Nkind (N) = N_Op_Or then
12108 Op :=
12109 Make_Op_Or (Loc,
12110 Left_Opnd => A_J,
12111 Right_Opnd => B_J);
12113 else
12114 Op :=
12115 Make_Op_Xor (Loc,
12116 Left_Opnd => A_J,
12117 Right_Opnd => B_J);
12118 end if;
12120 Loop_Statement :=
12121 Make_Implicit_Loop_Statement (N,
12122 Identifier => Empty,
12124 Iteration_Scheme =>
12125 Make_Iteration_Scheme (Loc,
12126 Loop_Parameter_Specification =>
12127 Make_Loop_Parameter_Specification (Loc,
12128 Defining_Identifier => J,
12129 Discrete_Subtype_Definition =>
12130 Make_Attribute_Reference (Loc,
12131 Prefix => New_Occurrence_Of (A, Loc),
12132 Attribute_Name => Name_Range))),
12134 Statements => New_List (
12135 Make_Assignment_Statement (Loc,
12136 Name => C_J,
12137 Expression => Op)));
12139 Formals := New_List (
12140 Make_Parameter_Specification (Loc,
12141 Defining_Identifier => A,
12142 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
12144 Make_Parameter_Specification (Loc,
12145 Defining_Identifier => B,
12146 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
12148 Func_Name := Make_Temporary (Loc, 'A');
12149 Set_Is_Inlined (Func_Name);
12151 Func_Body :=
12152 Make_Subprogram_Body (Loc,
12153 Specification =>
12154 Make_Function_Specification (Loc,
12155 Defining_Unit_Name => Func_Name,
12156 Parameter_Specifications => Formals,
12157 Result_Definition => New_Occurrence_Of (Typ, Loc)),
12159 Declarations => New_List (
12160 Make_Object_Declaration (Loc,
12161 Defining_Identifier => C,
12162 Object_Definition => New_Occurrence_Of (Typ, Loc))),
12164 Handled_Statement_Sequence =>
12165 Make_Handled_Sequence_Of_Statements (Loc,
12166 Statements => New_List (
12167 Loop_Statement,
12168 Make_Simple_Return_Statement (Loc,
12169 Expression => New_Occurrence_Of (C, Loc)))));
12171 return Func_Body;
12172 end Make_Boolean_Array_Op;
12174 -----------------------------------------
12175 -- Minimized_Eliminated_Overflow_Check --
12176 -----------------------------------------
12178 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is
12179 begin
12180 return
12181 Is_Signed_Integer_Type (Etype (N))
12182 and then Overflow_Check_Mode in Minimized_Or_Eliminated;
12183 end Minimized_Eliminated_Overflow_Check;
12185 --------------------------------
12186 -- Optimize_Length_Comparison --
12187 --------------------------------
12189 procedure Optimize_Length_Comparison (N : Node_Id) is
12190 Loc : constant Source_Ptr := Sloc (N);
12191 Typ : constant Entity_Id := Etype (N);
12192 Result : Node_Id;
12194 Left : Node_Id;
12195 Right : Node_Id;
12196 -- First and Last attribute reference nodes, which end up as left and
12197 -- right operands of the optimized result.
12199 Is_Zero : Boolean;
12200 -- True for comparison operand of zero
12202 Comp : Node_Id;
12203 -- Comparison operand, set only if Is_Zero is false
12205 Ent : Entity_Id;
12206 -- Entity whose length is being compared
12208 Index : Node_Id;
12209 -- Integer_Literal node for length attribute expression, or Empty
12210 -- if there is no such expression present.
12212 Ityp : Entity_Id;
12213 -- Type of array index to which 'Length is applied
12215 Op : Node_Kind := Nkind (N);
12216 -- Kind of comparison operator, gets flipped if operands backwards
12218 function Is_Optimizable (N : Node_Id) return Boolean;
12219 -- Tests N to see if it is an optimizable comparison value (defined as
12220 -- constant zero or one, or something else where the value is known to
12221 -- be positive and in the range of 32-bits, and where the corresponding
12222 -- Length value is also known to be 32-bits. If result is true, sets
12223 -- Is_Zero, Ityp, and Comp accordingly.
12225 function Is_Entity_Length (N : Node_Id) return Boolean;
12226 -- Tests if N is a length attribute applied to a simple entity. If so,
12227 -- returns True, and sets Ent to the entity, and Index to the integer
12228 -- literal provided as an attribute expression, or to Empty if none.
12229 -- Also returns True if the expression is a generated type conversion
12230 -- whose expression is of the desired form. This latter case arises
12231 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
12232 -- to check for being in range, which is not needed in this context.
12233 -- Returns False if neither condition holds.
12235 function Prepare_64 (N : Node_Id) return Node_Id;
12236 -- Given a discrete expression, returns a Long_Long_Integer typed
12237 -- expression representing the underlying value of the expression.
12238 -- This is done with an unchecked conversion to the result type. We
12239 -- use unchecked conversion to handle the enumeration type case.
12241 ----------------------
12242 -- Is_Entity_Length --
12243 ----------------------
12245 function Is_Entity_Length (N : Node_Id) return Boolean is
12246 begin
12247 if Nkind (N) = N_Attribute_Reference
12248 and then Attribute_Name (N) = Name_Length
12249 and then Is_Entity_Name (Prefix (N))
12250 then
12251 Ent := Entity (Prefix (N));
12253 if Present (Expressions (N)) then
12254 Index := First (Expressions (N));
12255 else
12256 Index := Empty;
12257 end if;
12259 return True;
12261 elsif Nkind (N) = N_Type_Conversion
12262 and then not Comes_From_Source (N)
12263 then
12264 return Is_Entity_Length (Expression (N));
12266 else
12267 return False;
12268 end if;
12269 end Is_Entity_Length;
12271 --------------------
12272 -- Is_Optimizable --
12273 --------------------
12275 function Is_Optimizable (N : Node_Id) return Boolean is
12276 Val : Uint;
12277 OK : Boolean;
12278 Lo : Uint;
12279 Hi : Uint;
12280 Indx : Node_Id;
12282 begin
12283 if Compile_Time_Known_Value (N) then
12284 Val := Expr_Value (N);
12286 if Val = Uint_0 then
12287 Is_Zero := True;
12288 Comp := Empty;
12289 return True;
12291 elsif Val = Uint_1 then
12292 Is_Zero := False;
12293 Comp := Empty;
12294 return True;
12295 end if;
12296 end if;
12298 -- Here we have to make sure of being within 32-bits
12300 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
12302 if not OK
12303 or else Lo < Uint_1
12304 or else Hi > UI_From_Int (Int'Last)
12305 then
12306 return False;
12307 end if;
12309 -- Comparison value was within range, so now we must check the index
12310 -- value to make sure it is also within 32-bits.
12312 Indx := First_Index (Etype (Ent));
12314 if Present (Index) then
12315 for J in 2 .. UI_To_Int (Intval (Index)) loop
12316 Next_Index (Indx);
12317 end loop;
12318 end if;
12320 Ityp := Etype (Indx);
12322 if Esize (Ityp) > 32 then
12323 return False;
12324 end if;
12326 Is_Zero := False;
12327 Comp := N;
12328 return True;
12329 end Is_Optimizable;
12331 ----------------
12332 -- Prepare_64 --
12333 ----------------
12335 function Prepare_64 (N : Node_Id) return Node_Id is
12336 begin
12337 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
12338 end Prepare_64;
12340 -- Start of processing for Optimize_Length_Comparison
12342 begin
12343 -- Nothing to do if not a comparison
12345 if Op not in N_Op_Compare then
12346 return;
12347 end if;
12349 -- Nothing to do if special -gnatd.P debug flag set
12351 if Debug_Flag_Dot_PP then
12352 return;
12353 end if;
12355 -- Ent'Length op 0/1
12357 if Is_Entity_Length (Left_Opnd (N))
12358 and then Is_Optimizable (Right_Opnd (N))
12359 then
12360 null;
12362 -- 0/1 op Ent'Length
12364 elsif Is_Entity_Length (Right_Opnd (N))
12365 and then Is_Optimizable (Left_Opnd (N))
12366 then
12367 -- Flip comparison to opposite sense
12369 case Op is
12370 when N_Op_Lt => Op := N_Op_Gt;
12371 when N_Op_Le => Op := N_Op_Ge;
12372 when N_Op_Gt => Op := N_Op_Lt;
12373 when N_Op_Ge => Op := N_Op_Le;
12374 when others => null;
12375 end case;
12377 -- Else optimization not possible
12379 else
12380 return;
12381 end if;
12383 -- Fall through if we will do the optimization
12385 -- Cases to handle:
12387 -- X'Length = 0 => X'First > X'Last
12388 -- X'Length = 1 => X'First = X'Last
12389 -- X'Length = n => X'First + (n - 1) = X'Last
12391 -- X'Length /= 0 => X'First <= X'Last
12392 -- X'Length /= 1 => X'First /= X'Last
12393 -- X'Length /= n => X'First + (n - 1) /= X'Last
12395 -- X'Length >= 0 => always true, warn
12396 -- X'Length >= 1 => X'First <= X'Last
12397 -- X'Length >= n => X'First + (n - 1) <= X'Last
12399 -- X'Length > 0 => X'First <= X'Last
12400 -- X'Length > 1 => X'First < X'Last
12401 -- X'Length > n => X'First + (n - 1) < X'Last
12403 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
12404 -- X'Length <= 1 => X'First >= X'Last
12405 -- X'Length <= n => X'First + (n - 1) >= X'Last
12407 -- X'Length < 0 => always false (warn)
12408 -- X'Length < 1 => X'First > X'Last
12409 -- X'Length < n => X'First + (n - 1) > X'Last
12411 -- Note: for the cases of n (not constant 0,1), we require that the
12412 -- corresponding index type be integer or shorter (i.e. not 64-bit),
12413 -- and the same for the comparison value. Then we do the comparison
12414 -- using 64-bit arithmetic (actually long long integer), so that we
12415 -- cannot have overflow intefering with the result.
12417 -- First deal with warning cases
12419 if Is_Zero then
12420 case Op is
12422 -- X'Length >= 0
12424 when N_Op_Ge =>
12425 Rewrite (N,
12426 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
12427 Analyze_And_Resolve (N, Typ);
12428 Warn_On_Known_Condition (N);
12429 return;
12431 -- X'Length < 0
12433 when N_Op_Lt =>
12434 Rewrite (N,
12435 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
12436 Analyze_And_Resolve (N, Typ);
12437 Warn_On_Known_Condition (N);
12438 return;
12440 when N_Op_Le =>
12441 if Constant_Condition_Warnings
12442 and then Comes_From_Source (Original_Node (N))
12443 then
12444 Error_Msg_N ("could replace by ""'=""?c?", N);
12445 end if;
12447 Op := N_Op_Eq;
12449 when others =>
12450 null;
12451 end case;
12452 end if;
12454 -- Build the First reference we will use
12456 Left :=
12457 Make_Attribute_Reference (Loc,
12458 Prefix => New_Occurrence_Of (Ent, Loc),
12459 Attribute_Name => Name_First);
12461 if Present (Index) then
12462 Set_Expressions (Left, New_List (New_Copy (Index)));
12463 end if;
12465 -- If general value case, then do the addition of (n - 1), and
12466 -- also add the needed conversions to type Long_Long_Integer.
12468 if Present (Comp) then
12469 Left :=
12470 Make_Op_Add (Loc,
12471 Left_Opnd => Prepare_64 (Left),
12472 Right_Opnd =>
12473 Make_Op_Subtract (Loc,
12474 Left_Opnd => Prepare_64 (Comp),
12475 Right_Opnd => Make_Integer_Literal (Loc, 1)));
12476 end if;
12478 -- Build the Last reference we will use
12480 Right :=
12481 Make_Attribute_Reference (Loc,
12482 Prefix => New_Occurrence_Of (Ent, Loc),
12483 Attribute_Name => Name_Last);
12485 if Present (Index) then
12486 Set_Expressions (Right, New_List (New_Copy (Index)));
12487 end if;
12489 -- If general operand, convert Last reference to Long_Long_Integer
12491 if Present (Comp) then
12492 Right := Prepare_64 (Right);
12493 end if;
12495 -- Check for cases to optimize
12497 -- X'Length = 0 => X'First > X'Last
12498 -- X'Length < 1 => X'First > X'Last
12499 -- X'Length < n => X'First + (n - 1) > X'Last
12501 if (Is_Zero and then Op = N_Op_Eq)
12502 or else (not Is_Zero and then Op = N_Op_Lt)
12503 then
12504 Result :=
12505 Make_Op_Gt (Loc,
12506 Left_Opnd => Left,
12507 Right_Opnd => Right);
12509 -- X'Length = 1 => X'First = X'Last
12510 -- X'Length = n => X'First + (n - 1) = X'Last
12512 elsif not Is_Zero and then Op = N_Op_Eq then
12513 Result :=
12514 Make_Op_Eq (Loc,
12515 Left_Opnd => Left,
12516 Right_Opnd => Right);
12518 -- X'Length /= 0 => X'First <= X'Last
12519 -- X'Length > 0 => X'First <= X'Last
12521 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
12522 Result :=
12523 Make_Op_Le (Loc,
12524 Left_Opnd => Left,
12525 Right_Opnd => Right);
12527 -- X'Length /= 1 => X'First /= X'Last
12528 -- X'Length /= n => X'First + (n - 1) /= X'Last
12530 elsif not Is_Zero and then Op = N_Op_Ne then
12531 Result :=
12532 Make_Op_Ne (Loc,
12533 Left_Opnd => Left,
12534 Right_Opnd => Right);
12536 -- X'Length >= 1 => X'First <= X'Last
12537 -- X'Length >= n => X'First + (n - 1) <= X'Last
12539 elsif not Is_Zero and then Op = N_Op_Ge then
12540 Result :=
12541 Make_Op_Le (Loc,
12542 Left_Opnd => Left,
12543 Right_Opnd => Right);
12545 -- X'Length > 1 => X'First < X'Last
12546 -- X'Length > n => X'First + (n = 1) < X'Last
12548 elsif not Is_Zero and then Op = N_Op_Gt then
12549 Result :=
12550 Make_Op_Lt (Loc,
12551 Left_Opnd => Left,
12552 Right_Opnd => Right);
12554 -- X'Length <= 1 => X'First >= X'Last
12555 -- X'Length <= n => X'First + (n - 1) >= X'Last
12557 elsif not Is_Zero and then Op = N_Op_Le then
12558 Result :=
12559 Make_Op_Ge (Loc,
12560 Left_Opnd => Left,
12561 Right_Opnd => Right);
12563 -- Should not happen at this stage
12565 else
12566 raise Program_Error;
12567 end if;
12569 -- Rewrite and finish up
12571 Rewrite (N, Result);
12572 Analyze_And_Resolve (N, Typ);
12573 return;
12574 end Optimize_Length_Comparison;
12576 ------------------------------
12577 -- Process_Transient_Object --
12578 ------------------------------
12580 procedure Process_Transient_Object
12581 (Decl : Node_Id;
12582 Rel_Node : Node_Id)
12584 Loc : constant Source_Ptr := Sloc (Decl);
12585 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
12586 Obj_Typ : constant Node_Id := Etype (Obj_Id);
12587 Desig_Typ : Entity_Id;
12588 Expr : Node_Id;
12589 Fin_Stmts : List_Id;
12590 Ptr_Id : Entity_Id;
12591 Temp_Id : Entity_Id;
12592 Temp_Ins : Node_Id;
12594 Hook_Context : constant Node_Id := Find_Hook_Context (Rel_Node);
12595 -- Node on which to insert the hook pointer (as an action): the
12596 -- innermost enclosing non-transient scope.
12598 Finalization_Context : Node_Id;
12599 -- Node after which to insert finalization actions
12601 Finalize_Always : Boolean;
12602 -- If False, call to finalizer includes a test of whether the hook
12603 -- pointer is null.
12605 begin
12606 -- Step 0: determine where to attach finalization actions in the tree
12608 -- Special case for Boolean EWAs: capture expression in a temporary,
12609 -- whose declaration will serve as the context around which to insert
12610 -- finalization code. The finalization thus remains local to the
12611 -- specific condition being evaluated.
12613 if Is_Boolean_Type (Etype (Rel_Node)) then
12615 -- In this case, the finalization context is chosen so that we know
12616 -- at finalization point that the hook pointer is never null, so no
12617 -- need for a test, we can call the finalizer unconditionally, except
12618 -- in the case where the object is created in a specific branch of a
12619 -- conditional expression.
12621 Finalize_Always :=
12622 not Within_Case_Or_If_Expression (Rel_Node)
12623 and then not Nkind_In
12624 (Original_Node (Rel_Node), N_Case_Expression,
12625 N_If_Expression);
12627 declare
12628 Loc : constant Source_Ptr := Sloc (Rel_Node);
12629 Temp : constant Entity_Id := Make_Temporary (Loc, 'E', Rel_Node);
12631 begin
12632 Append_To (Actions (Rel_Node),
12633 Make_Object_Declaration (Loc,
12634 Defining_Identifier => Temp,
12635 Constant_Present => True,
12636 Object_Definition =>
12637 New_Occurrence_Of (Etype (Rel_Node), Loc),
12638 Expression => Expression (Rel_Node)));
12639 Finalization_Context := Last (Actions (Rel_Node));
12641 Analyze (Last (Actions (Rel_Node)));
12643 Set_Expression (Rel_Node, New_Occurrence_Of (Temp, Loc));
12644 Analyze (Expression (Rel_Node));
12645 end;
12647 else
12648 Finalize_Always := False;
12649 Finalization_Context := Hook_Context;
12650 end if;
12652 -- Step 1: Create the access type which provides a reference to the
12653 -- transient controlled object.
12655 if Is_Access_Type (Obj_Typ) then
12656 Desig_Typ := Directly_Designated_Type (Obj_Typ);
12657 else
12658 Desig_Typ := Obj_Typ;
12659 end if;
12661 Desig_Typ := Base_Type (Desig_Typ);
12663 -- Generate:
12664 -- Ann : access [all] <Desig_Typ>;
12666 Ptr_Id := Make_Temporary (Loc, 'A');
12668 Insert_Action (Hook_Context,
12669 Make_Full_Type_Declaration (Loc,
12670 Defining_Identifier => Ptr_Id,
12671 Type_Definition =>
12672 Make_Access_To_Object_Definition (Loc,
12673 All_Present => Ekind (Obj_Typ) = E_General_Access_Type,
12674 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc))));
12676 -- Step 2: Create a temporary which acts as a hook to the transient
12677 -- controlled object. Generate:
12679 -- Temp : Ptr_Id := null;
12681 Temp_Id := Make_Temporary (Loc, 'T');
12683 Insert_Action (Hook_Context,
12684 Make_Object_Declaration (Loc,
12685 Defining_Identifier => Temp_Id,
12686 Object_Definition => New_Occurrence_Of (Ptr_Id, Loc)));
12688 -- Mark the temporary as created for the purposes of exporting the
12689 -- transient controlled object out of the expression_with_action or if
12690 -- expression. This signals the machinery in Build_Finalizer to treat
12691 -- this case specially.
12693 Set_Status_Flag_Or_Transient_Decl (Temp_Id, Decl);
12695 -- Step 3: Hook the transient object to the temporary
12697 -- This must be inserted right after the object declaration, so that
12698 -- the assignment is executed if, and only if, the object is actually
12699 -- created (whereas the declaration of the hook pointer, and the
12700 -- finalization call, may be inserted at an outer level, and may
12701 -- remain unused for some executions, if the actual creation of
12702 -- the object is conditional).
12704 -- The use of unchecked conversion / unrestricted access is needed to
12705 -- avoid an accessibility violation. Note that the finalization code is
12706 -- structured in such a way that the "hook" is processed only when it
12707 -- points to an existing object.
12709 if Is_Access_Type (Obj_Typ) then
12710 Expr :=
12711 Unchecked_Convert_To (Ptr_Id, New_Occurrence_Of (Obj_Id, Loc));
12712 else
12713 Expr :=
12714 Make_Attribute_Reference (Loc,
12715 Prefix => New_Occurrence_Of (Obj_Id, Loc),
12716 Attribute_Name => Name_Unrestricted_Access);
12717 end if;
12719 -- Generate:
12720 -- Temp := Ptr_Id (Obj_Id);
12721 -- <or>
12722 -- Temp := Obj_Id'Unrestricted_Access;
12724 -- When the transient object is initialized by an aggregate, the hook
12725 -- must capture the object after the last component assignment takes
12726 -- place. Only then is the object fully initialized.
12728 if Ekind (Obj_Id) = E_Variable
12729 and then Present (Last_Aggregate_Assignment (Obj_Id))
12730 then
12731 Temp_Ins := Last_Aggregate_Assignment (Obj_Id);
12733 -- Otherwise the hook seizes the related object immediately
12735 else
12736 Temp_Ins := Decl;
12737 end if;
12739 Insert_After_And_Analyze (Temp_Ins,
12740 Make_Assignment_Statement (Loc,
12741 Name => New_Occurrence_Of (Temp_Id, Loc),
12742 Expression => Expr));
12744 -- Step 4: Finalize the transient controlled object after the context
12745 -- has been evaluated/elaborated. Generate:
12747 -- if Temp /= null then
12748 -- [Deep_]Finalize (Temp.all);
12749 -- Temp := null;
12750 -- end if;
12752 -- When the node is part of a return statement, there is no need to
12753 -- insert a finalization call, as the general finalization mechanism
12754 -- (see Build_Finalizer) would take care of the transient controlled
12755 -- object on subprogram exit. Note that it would also be impossible to
12756 -- insert the finalization code after the return statement as this will
12757 -- render it unreachable.
12759 if Nkind (Finalization_Context) /= N_Simple_Return_Statement then
12760 Fin_Stmts := New_List (
12761 Make_Final_Call
12762 (Obj_Ref =>
12763 Make_Explicit_Dereference (Loc,
12764 Prefix => New_Occurrence_Of (Temp_Id, Loc)),
12765 Typ => Desig_Typ),
12767 Make_Assignment_Statement (Loc,
12768 Name => New_Occurrence_Of (Temp_Id, Loc),
12769 Expression => Make_Null (Loc)));
12771 if not Finalize_Always then
12772 Fin_Stmts := New_List (
12773 Make_Implicit_If_Statement (Decl,
12774 Condition =>
12775 Make_Op_Ne (Loc,
12776 Left_Opnd => New_Occurrence_Of (Temp_Id, Loc),
12777 Right_Opnd => Make_Null (Loc)),
12778 Then_Statements => Fin_Stmts));
12779 end if;
12781 Insert_Actions_After (Finalization_Context, Fin_Stmts);
12782 end if;
12783 end Process_Transient_Object;
12785 ------------------------
12786 -- Rewrite_Comparison --
12787 ------------------------
12789 procedure Rewrite_Comparison (N : Node_Id) is
12790 Warning_Generated : Boolean := False;
12791 -- Set to True if first pass with Assume_Valid generates a warning in
12792 -- which case we skip the second pass to avoid warning overloaded.
12794 Result : Node_Id;
12795 -- Set to Standard_True or Standard_False
12797 begin
12798 if Nkind (N) = N_Type_Conversion then
12799 Rewrite_Comparison (Expression (N));
12800 return;
12802 elsif Nkind (N) not in N_Op_Compare then
12803 return;
12804 end if;
12806 -- Now start looking at the comparison in detail. We potentially go
12807 -- through this loop twice. The first time, Assume_Valid is set False
12808 -- in the call to Compile_Time_Compare. If this call results in a
12809 -- clear result of always True or Always False, that's decisive and
12810 -- we are done. Otherwise we repeat the processing with Assume_Valid
12811 -- set to True to generate additional warnings. We can skip that step
12812 -- if Constant_Condition_Warnings is False.
12814 for AV in False .. True loop
12815 declare
12816 Typ : constant Entity_Id := Etype (N);
12817 Op1 : constant Node_Id := Left_Opnd (N);
12818 Op2 : constant Node_Id := Right_Opnd (N);
12820 Res : constant Compare_Result :=
12821 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
12822 -- Res indicates if compare outcome can be compile time determined
12824 True_Result : Boolean;
12825 False_Result : Boolean;
12827 begin
12828 case N_Op_Compare (Nkind (N)) is
12829 when N_Op_Eq =>
12830 True_Result := Res = EQ;
12831 False_Result := Res = LT or else Res = GT or else Res = NE;
12833 when N_Op_Ge =>
12834 True_Result := Res in Compare_GE;
12835 False_Result := Res = LT;
12837 if Res = LE
12838 and then Constant_Condition_Warnings
12839 and then Comes_From_Source (Original_Node (N))
12840 and then Nkind (Original_Node (N)) = N_Op_Ge
12841 and then not In_Instance
12842 and then Is_Integer_Type (Etype (Left_Opnd (N)))
12843 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
12844 then
12845 Error_Msg_N
12846 ("can never be greater than, could replace by ""'=""?c?",
12848 Warning_Generated := True;
12849 end if;
12851 when N_Op_Gt =>
12852 True_Result := Res = GT;
12853 False_Result := Res in Compare_LE;
12855 when N_Op_Lt =>
12856 True_Result := Res = LT;
12857 False_Result := Res in Compare_GE;
12859 when N_Op_Le =>
12860 True_Result := Res in Compare_LE;
12861 False_Result := Res = GT;
12863 if Res = GE
12864 and then Constant_Condition_Warnings
12865 and then Comes_From_Source (Original_Node (N))
12866 and then Nkind (Original_Node (N)) = N_Op_Le
12867 and then not In_Instance
12868 and then Is_Integer_Type (Etype (Left_Opnd (N)))
12869 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
12870 then
12871 Error_Msg_N
12872 ("can never be less than, could replace by ""'=""?c?", N);
12873 Warning_Generated := True;
12874 end if;
12876 when N_Op_Ne =>
12877 True_Result := Res = NE or else Res = GT or else Res = LT;
12878 False_Result := Res = EQ;
12879 end case;
12881 -- If this is the first iteration, then we actually convert the
12882 -- comparison into True or False, if the result is certain.
12884 if AV = False then
12885 if True_Result or False_Result then
12886 Result := Boolean_Literals (True_Result);
12887 Rewrite (N,
12888 Convert_To (Typ,
12889 New_Occurrence_Of (Result, Sloc (N))));
12890 Analyze_And_Resolve (N, Typ);
12891 Warn_On_Known_Condition (N);
12892 return;
12893 end if;
12895 -- If this is the second iteration (AV = True), and the original
12896 -- node comes from source and we are not in an instance, then give
12897 -- a warning if we know result would be True or False. Note: we
12898 -- know Constant_Condition_Warnings is set if we get here.
12900 elsif Comes_From_Source (Original_Node (N))
12901 and then not In_Instance
12902 then
12903 if True_Result then
12904 Error_Msg_N
12905 ("condition can only be False if invalid values present??",
12907 elsif False_Result then
12908 Error_Msg_N
12909 ("condition can only be True if invalid values present??",
12911 end if;
12912 end if;
12913 end;
12915 -- Skip second iteration if not warning on constant conditions or
12916 -- if the first iteration already generated a warning of some kind or
12917 -- if we are in any case assuming all values are valid (so that the
12918 -- first iteration took care of the valid case).
12920 exit when not Constant_Condition_Warnings;
12921 exit when Warning_Generated;
12922 exit when Assume_No_Invalid_Values;
12923 end loop;
12924 end Rewrite_Comparison;
12926 ----------------------------
12927 -- Safe_In_Place_Array_Op --
12928 ----------------------------
12930 function Safe_In_Place_Array_Op
12931 (Lhs : Node_Id;
12932 Op1 : Node_Id;
12933 Op2 : Node_Id) return Boolean
12935 Target : Entity_Id;
12937 function Is_Safe_Operand (Op : Node_Id) return Boolean;
12938 -- Operand is safe if it cannot overlap part of the target of the
12939 -- operation. If the operand and the target are identical, the operand
12940 -- is safe. The operand can be empty in the case of negation.
12942 function Is_Unaliased (N : Node_Id) return Boolean;
12943 -- Check that N is a stand-alone entity
12945 ------------------
12946 -- Is_Unaliased --
12947 ------------------
12949 function Is_Unaliased (N : Node_Id) return Boolean is
12950 begin
12951 return
12952 Is_Entity_Name (N)
12953 and then No (Address_Clause (Entity (N)))
12954 and then No (Renamed_Object (Entity (N)));
12955 end Is_Unaliased;
12957 ---------------------
12958 -- Is_Safe_Operand --
12959 ---------------------
12961 function Is_Safe_Operand (Op : Node_Id) return Boolean is
12962 begin
12963 if No (Op) then
12964 return True;
12966 elsif Is_Entity_Name (Op) then
12967 return Is_Unaliased (Op);
12969 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
12970 return Is_Unaliased (Prefix (Op));
12972 elsif Nkind (Op) = N_Slice then
12973 return
12974 Is_Unaliased (Prefix (Op))
12975 and then Entity (Prefix (Op)) /= Target;
12977 elsif Nkind (Op) = N_Op_Not then
12978 return Is_Safe_Operand (Right_Opnd (Op));
12980 else
12981 return False;
12982 end if;
12983 end Is_Safe_Operand;
12985 -- Start of processing for Safe_In_Place_Array_Op
12987 begin
12988 -- Skip this processing if the component size is different from system
12989 -- storage unit (since at least for NOT this would cause problems).
12991 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
12992 return False;
12994 -- Cannot do in place stuff on VM_Target since cannot pass addresses
12996 elsif VM_Target /= No_VM then
12997 return False;
12999 -- Cannot do in place stuff if non-standard Boolean representation
13001 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
13002 return False;
13004 elsif not Is_Unaliased (Lhs) then
13005 return False;
13007 else
13008 Target := Entity (Lhs);
13009 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
13010 end if;
13011 end Safe_In_Place_Array_Op;
13013 -----------------------
13014 -- Tagged_Membership --
13015 -----------------------
13017 -- There are two different cases to consider depending on whether the right
13018 -- operand is a class-wide type or not. If not we just compare the actual
13019 -- tag of the left expr to the target type tag:
13021 -- Left_Expr.Tag = Right_Type'Tag;
13023 -- If it is a class-wide type we use the RT function CW_Membership which is
13024 -- usually implemented by looking in the ancestor tables contained in the
13025 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
13027 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
13028 -- function IW_Membership which is usually implemented by looking in the
13029 -- table of abstract interface types plus the ancestor table contained in
13030 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
13032 procedure Tagged_Membership
13033 (N : Node_Id;
13034 SCIL_Node : out Node_Id;
13035 Result : out Node_Id)
13037 Left : constant Node_Id := Left_Opnd (N);
13038 Right : constant Node_Id := Right_Opnd (N);
13039 Loc : constant Source_Ptr := Sloc (N);
13041 Full_R_Typ : Entity_Id;
13042 Left_Type : Entity_Id;
13043 New_Node : Node_Id;
13044 Right_Type : Entity_Id;
13045 Obj_Tag : Node_Id;
13047 begin
13048 SCIL_Node := Empty;
13050 -- Handle entities from the limited view
13052 Left_Type := Available_View (Etype (Left));
13053 Right_Type := Available_View (Etype (Right));
13055 -- In the case where the type is an access type, the test is applied
13056 -- using the designated types (needed in Ada 2012 for implicit anonymous
13057 -- access conversions, for AI05-0149).
13059 if Is_Access_Type (Right_Type) then
13060 Left_Type := Designated_Type (Left_Type);
13061 Right_Type := Designated_Type (Right_Type);
13062 end if;
13064 if Is_Class_Wide_Type (Left_Type) then
13065 Left_Type := Root_Type (Left_Type);
13066 end if;
13068 if Is_Class_Wide_Type (Right_Type) then
13069 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
13070 else
13071 Full_R_Typ := Underlying_Type (Right_Type);
13072 end if;
13074 Obj_Tag :=
13075 Make_Selected_Component (Loc,
13076 Prefix => Relocate_Node (Left),
13077 Selector_Name =>
13078 New_Occurrence_Of (First_Tag_Component (Left_Type), Loc));
13080 if Is_Class_Wide_Type (Right_Type) then
13082 -- No need to issue a run-time check if we statically know that the
13083 -- result of this membership test is always true. For example,
13084 -- considering the following declarations:
13086 -- type Iface is interface;
13087 -- type T is tagged null record;
13088 -- type DT is new T and Iface with null record;
13090 -- Obj1 : T;
13091 -- Obj2 : DT;
13093 -- These membership tests are always true:
13095 -- Obj1 in T'Class
13096 -- Obj2 in T'Class;
13097 -- Obj2 in Iface'Class;
13099 -- We do not need to handle cases where the membership is illegal.
13100 -- For example:
13102 -- Obj1 in DT'Class; -- Compile time error
13103 -- Obj1 in Iface'Class; -- Compile time error
13105 if not Is_Class_Wide_Type (Left_Type)
13106 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
13107 Use_Full_View => True)
13108 or else (Is_Interface (Etype (Right_Type))
13109 and then Interface_Present_In_Ancestor
13110 (Typ => Left_Type,
13111 Iface => Etype (Right_Type))))
13112 then
13113 Result := New_Occurrence_Of (Standard_True, Loc);
13114 return;
13115 end if;
13117 -- Ada 2005 (AI-251): Class-wide applied to interfaces
13119 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
13121 -- Support to: "Iface_CW_Typ in Typ'Class"
13123 or else Is_Interface (Left_Type)
13124 then
13125 -- Issue error if IW_Membership operation not available in a
13126 -- configurable run time setting.
13128 if not RTE_Available (RE_IW_Membership) then
13129 Error_Msg_CRT
13130 ("dynamic membership test on interface types", N);
13131 Result := Empty;
13132 return;
13133 end if;
13135 Result :=
13136 Make_Function_Call (Loc,
13137 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
13138 Parameter_Associations => New_List (
13139 Make_Attribute_Reference (Loc,
13140 Prefix => Obj_Tag,
13141 Attribute_Name => Name_Address),
13142 New_Occurrence_Of (
13143 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
13144 Loc)));
13146 -- Ada 95: Normal case
13148 else
13149 Build_CW_Membership (Loc,
13150 Obj_Tag_Node => Obj_Tag,
13151 Typ_Tag_Node =>
13152 New_Occurrence_Of (
13153 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc),
13154 Related_Nod => N,
13155 New_Node => New_Node);
13157 -- Generate the SCIL node for this class-wide membership test.
13158 -- Done here because the previous call to Build_CW_Membership
13159 -- relocates Obj_Tag.
13161 if Generate_SCIL then
13162 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
13163 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
13164 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
13165 end if;
13167 Result := New_Node;
13168 end if;
13170 -- Right_Type is not a class-wide type
13172 else
13173 -- No need to check the tag of the object if Right_Typ is abstract
13175 if Is_Abstract_Type (Right_Type) then
13176 Result := New_Occurrence_Of (Standard_False, Loc);
13178 else
13179 Result :=
13180 Make_Op_Eq (Loc,
13181 Left_Opnd => Obj_Tag,
13182 Right_Opnd =>
13183 New_Occurrence_Of
13184 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
13185 end if;
13186 end if;
13187 end Tagged_Membership;
13189 ------------------------------
13190 -- Unary_Op_Validity_Checks --
13191 ------------------------------
13193 procedure Unary_Op_Validity_Checks (N : Node_Id) is
13194 begin
13195 if Validity_Checks_On and Validity_Check_Operands then
13196 Ensure_Valid (Right_Opnd (N));
13197 end if;
13198 end Unary_Op_Validity_Checks;
13200 end Exp_Ch4;