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