* xcoffout.c (xcoff_tls_data_section_name): Define.
[official-gcc.git] / gcc / ada / exp_ch4.adb
blobbf3a6479761f4fa4f60580f3a2a7d70f4f341e1a
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-2012, 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 Rewrite_Comparison (N : Node_Id);
237 -- If N is the node for a comparison whose outcome can be determined at
238 -- compile time, then the node N can be rewritten with True or False. If
239 -- the outcome cannot be determined at compile time, the call has no
240 -- effect. If N is a type conversion, then this processing is applied to
241 -- its expression. If N is neither comparison nor a type conversion, the
242 -- call has no effect.
244 procedure Tagged_Membership
245 (N : Node_Id;
246 SCIL_Node : out Node_Id;
247 Result : out Node_Id);
248 -- Construct the expression corresponding to the tagged membership test.
249 -- Deals with a second operand being (or not) a class-wide type.
251 function Safe_In_Place_Array_Op
252 (Lhs : Node_Id;
253 Op1 : Node_Id;
254 Op2 : Node_Id) return Boolean;
255 -- In the context of an assignment, where the right-hand side is a boolean
256 -- operation on arrays, check whether operation can be performed in place.
258 procedure Unary_Op_Validity_Checks (N : Node_Id);
259 pragma Inline (Unary_Op_Validity_Checks);
260 -- Performs validity checks for a unary operator
262 -------------------------------
263 -- Binary_Op_Validity_Checks --
264 -------------------------------
266 procedure Binary_Op_Validity_Checks (N : Node_Id) is
267 begin
268 if Validity_Checks_On and Validity_Check_Operands then
269 Ensure_Valid (Left_Opnd (N));
270 Ensure_Valid (Right_Opnd (N));
271 end if;
272 end Binary_Op_Validity_Checks;
274 ------------------------------------
275 -- Build_Boolean_Array_Proc_Call --
276 ------------------------------------
278 procedure Build_Boolean_Array_Proc_Call
279 (N : Node_Id;
280 Op1 : Node_Id;
281 Op2 : Node_Id)
283 Loc : constant Source_Ptr := Sloc (N);
284 Kind : constant Node_Kind := Nkind (Expression (N));
285 Target : constant Node_Id :=
286 Make_Attribute_Reference (Loc,
287 Prefix => Name (N),
288 Attribute_Name => Name_Address);
290 Arg1 : Node_Id := Op1;
291 Arg2 : Node_Id := Op2;
292 Call_Node : Node_Id;
293 Proc_Name : Entity_Id;
295 begin
296 if Kind = N_Op_Not then
297 if Nkind (Op1) in N_Binary_Op then
299 -- Use negated version of the binary operators
301 if Nkind (Op1) = N_Op_And then
302 Proc_Name := RTE (RE_Vector_Nand);
304 elsif Nkind (Op1) = N_Op_Or then
305 Proc_Name := RTE (RE_Vector_Nor);
307 else pragma Assert (Nkind (Op1) = N_Op_Xor);
308 Proc_Name := RTE (RE_Vector_Xor);
309 end if;
311 Call_Node :=
312 Make_Procedure_Call_Statement (Loc,
313 Name => New_Occurrence_Of (Proc_Name, Loc),
315 Parameter_Associations => New_List (
316 Target,
317 Make_Attribute_Reference (Loc,
318 Prefix => Left_Opnd (Op1),
319 Attribute_Name => Name_Address),
321 Make_Attribute_Reference (Loc,
322 Prefix => Right_Opnd (Op1),
323 Attribute_Name => Name_Address),
325 Make_Attribute_Reference (Loc,
326 Prefix => Left_Opnd (Op1),
327 Attribute_Name => Name_Length)));
329 else
330 Proc_Name := RTE (RE_Vector_Not);
332 Call_Node :=
333 Make_Procedure_Call_Statement (Loc,
334 Name => New_Occurrence_Of (Proc_Name, Loc),
335 Parameter_Associations => New_List (
336 Target,
338 Make_Attribute_Reference (Loc,
339 Prefix => Op1,
340 Attribute_Name => Name_Address),
342 Make_Attribute_Reference (Loc,
343 Prefix => Op1,
344 Attribute_Name => Name_Length)));
345 end if;
347 else
348 -- We use the following equivalences:
350 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
351 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
352 -- (not X) xor (not Y) = X xor Y
353 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
355 if Nkind (Op1) = N_Op_Not then
356 Arg1 := Right_Opnd (Op1);
357 Arg2 := Right_Opnd (Op2);
358 if Kind = N_Op_And then
359 Proc_Name := RTE (RE_Vector_Nor);
360 elsif Kind = N_Op_Or then
361 Proc_Name := RTE (RE_Vector_Nand);
362 else
363 Proc_Name := RTE (RE_Vector_Xor);
364 end if;
366 else
367 if Kind = N_Op_And then
368 Proc_Name := RTE (RE_Vector_And);
369 elsif Kind = N_Op_Or then
370 Proc_Name := RTE (RE_Vector_Or);
371 elsif Nkind (Op2) = N_Op_Not then
372 Proc_Name := RTE (RE_Vector_Nxor);
373 Arg2 := Right_Opnd (Op2);
374 else
375 Proc_Name := RTE (RE_Vector_Xor);
376 end if;
377 end if;
379 Call_Node :=
380 Make_Procedure_Call_Statement (Loc,
381 Name => New_Occurrence_Of (Proc_Name, Loc),
382 Parameter_Associations => New_List (
383 Target,
384 Make_Attribute_Reference (Loc,
385 Prefix => Arg1,
386 Attribute_Name => Name_Address),
387 Make_Attribute_Reference (Loc,
388 Prefix => Arg2,
389 Attribute_Name => Name_Address),
390 Make_Attribute_Reference (Loc,
391 Prefix => Arg1,
392 Attribute_Name => Name_Length)));
393 end if;
395 Rewrite (N, Call_Node);
396 Analyze (N);
398 exception
399 when RE_Not_Available =>
400 return;
401 end Build_Boolean_Array_Proc_Call;
403 ------------------------------
404 -- Current_Anonymous_Master --
405 ------------------------------
407 function Current_Anonymous_Master return Entity_Id is
408 Decls : List_Id;
409 Loc : Source_Ptr;
410 Subp_Body : Node_Id;
411 Unit_Decl : Node_Id;
412 Unit_Id : Entity_Id;
414 begin
415 Unit_Id := Cunit_Entity (Current_Sem_Unit);
417 -- Find the entity of the current unit
419 if Ekind (Unit_Id) = E_Subprogram_Body then
421 -- When processing subprogram bodies, the proper scope is always that
422 -- of the spec.
424 Subp_Body := Unit_Id;
425 while Present (Subp_Body)
426 and then Nkind (Subp_Body) /= N_Subprogram_Body
427 loop
428 Subp_Body := Parent (Subp_Body);
429 end loop;
431 Unit_Id := Corresponding_Spec (Subp_Body);
432 end if;
434 Loc := Sloc (Unit_Id);
435 Unit_Decl := Unit (Cunit (Current_Sem_Unit));
437 -- Find the declarations list of the current unit
439 if Nkind (Unit_Decl) = N_Package_Declaration then
440 Unit_Decl := Specification (Unit_Decl);
441 Decls := Visible_Declarations (Unit_Decl);
443 if No (Decls) then
444 Decls := New_List (Make_Null_Statement (Loc));
445 Set_Visible_Declarations (Unit_Decl, Decls);
447 elsif Is_Empty_List (Decls) then
448 Append_To (Decls, Make_Null_Statement (Loc));
449 end if;
451 else
452 Decls := Declarations (Unit_Decl);
454 if No (Decls) then
455 Decls := New_List (Make_Null_Statement (Loc));
456 Set_Declarations (Unit_Decl, Decls);
458 elsif Is_Empty_List (Decls) then
459 Append_To (Decls, Make_Null_Statement (Loc));
460 end if;
461 end if;
463 -- The current unit has an existing anonymous master, traverse its
464 -- declarations and locate the entity.
466 if Has_Anonymous_Master (Unit_Id) then
467 declare
468 Decl : Node_Id;
469 Fin_Mas_Id : Entity_Id;
471 begin
472 Decl := First (Decls);
473 while Present (Decl) loop
475 -- Look for the first variable in the declarations whole type
476 -- is Finalization_Master.
478 if Nkind (Decl) = N_Object_Declaration then
479 Fin_Mas_Id := Defining_Identifier (Decl);
481 if Ekind (Fin_Mas_Id) = E_Variable
482 and then Etype (Fin_Mas_Id) = RTE (RE_Finalization_Master)
483 then
484 return Fin_Mas_Id;
485 end if;
486 end if;
488 Next (Decl);
489 end loop;
491 -- The master was not found even though the unit was labeled as
492 -- having one.
494 raise Program_Error;
495 end;
497 -- Create a new anonymous master
499 else
500 declare
501 First_Decl : constant Node_Id := First (Decls);
502 Action : Node_Id;
503 Fin_Mas_Id : Entity_Id;
505 begin
506 -- Since the master and its associated initialization is inserted
507 -- at top level, use the scope of the unit when analyzing.
509 Push_Scope (Unit_Id);
511 -- Create the finalization master
513 Fin_Mas_Id :=
514 Make_Defining_Identifier (Loc,
515 Chars => New_External_Name (Chars (Unit_Id), "AM"));
517 -- Generate:
518 -- <Fin_Mas_Id> : Finalization_Master;
520 Action :=
521 Make_Object_Declaration (Loc,
522 Defining_Identifier => Fin_Mas_Id,
523 Object_Definition =>
524 New_Reference_To (RTE (RE_Finalization_Master), Loc));
526 Insert_Before_And_Analyze (First_Decl, Action);
528 -- Mark the unit to prevent the generation of multiple masters
530 Set_Has_Anonymous_Master (Unit_Id);
532 -- Do not set the base pool and mode of operation on .NET/JVM
533 -- since those targets do not support pools and all VM masters
534 -- are heterogeneous by default.
536 if VM_Target = No_VM then
538 -- Generate:
539 -- Set_Base_Pool
540 -- (<Fin_Mas_Id>, Global_Pool_Object'Unrestricted_Access);
542 Action :=
543 Make_Procedure_Call_Statement (Loc,
544 Name =>
545 New_Reference_To (RTE (RE_Set_Base_Pool), Loc),
547 Parameter_Associations => New_List (
548 New_Reference_To (Fin_Mas_Id, Loc),
549 Make_Attribute_Reference (Loc,
550 Prefix =>
551 New_Reference_To (RTE (RE_Global_Pool_Object), Loc),
552 Attribute_Name => Name_Unrestricted_Access)));
554 Insert_Before_And_Analyze (First_Decl, Action);
556 -- Generate:
557 -- Set_Is_Heterogeneous (<Fin_Mas_Id>);
559 Action :=
560 Make_Procedure_Call_Statement (Loc,
561 Name =>
562 New_Reference_To (RTE (RE_Set_Is_Heterogeneous), Loc),
563 Parameter_Associations => New_List (
564 New_Reference_To (Fin_Mas_Id, Loc)));
566 Insert_Before_And_Analyze (First_Decl, Action);
567 end if;
569 -- Restore the original state of the scope stack
571 Pop_Scope;
573 return Fin_Mas_Id;
574 end;
575 end if;
576 end Current_Anonymous_Master;
578 --------------------------------
579 -- Displace_Allocator_Pointer --
580 --------------------------------
582 procedure Displace_Allocator_Pointer (N : Node_Id) is
583 Loc : constant Source_Ptr := Sloc (N);
584 Orig_Node : constant Node_Id := Original_Node (N);
585 Dtyp : Entity_Id;
586 Etyp : Entity_Id;
587 PtrT : Entity_Id;
589 begin
590 -- Do nothing in case of VM targets: the virtual machine will handle
591 -- interfaces directly.
593 if not Tagged_Type_Expansion then
594 return;
595 end if;
597 pragma Assert (Nkind (N) = N_Identifier
598 and then Nkind (Orig_Node) = N_Allocator);
600 PtrT := Etype (Orig_Node);
601 Dtyp := Available_View (Designated_Type (PtrT));
602 Etyp := Etype (Expression (Orig_Node));
604 if Is_Class_Wide_Type (Dtyp)
605 and then Is_Interface (Dtyp)
606 then
607 -- If the type of the allocator expression is not an interface type
608 -- we can generate code to reference the record component containing
609 -- the pointer to the secondary dispatch table.
611 if not Is_Interface (Etyp) then
612 declare
613 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
615 begin
616 -- 1) Get access to the allocated object
618 Rewrite (N,
619 Make_Explicit_Dereference (Loc, Relocate_Node (N)));
620 Set_Etype (N, Etyp);
621 Set_Analyzed (N);
623 -- 2) Add the conversion to displace the pointer to reference
624 -- the secondary dispatch table.
626 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
627 Analyze_And_Resolve (N, Dtyp);
629 -- 3) The 'access to the secondary dispatch table will be used
630 -- as the value returned by the allocator.
632 Rewrite (N,
633 Make_Attribute_Reference (Loc,
634 Prefix => Relocate_Node (N),
635 Attribute_Name => Name_Access));
636 Set_Etype (N, Saved_Typ);
637 Set_Analyzed (N);
638 end;
640 -- If the type of the allocator expression is an interface type we
641 -- generate a run-time call to displace "this" to reference the
642 -- component containing the pointer to the secondary dispatch table
643 -- or else raise Constraint_Error if the actual object does not
644 -- implement the target interface. This case corresponds with the
645 -- following example:
647 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
648 -- begin
649 -- return new Iface_2'Class'(Obj);
650 -- end Op;
652 else
653 Rewrite (N,
654 Unchecked_Convert_To (PtrT,
655 Make_Function_Call (Loc,
656 Name => New_Reference_To (RTE (RE_Displace), Loc),
657 Parameter_Associations => New_List (
658 Unchecked_Convert_To (RTE (RE_Address),
659 Relocate_Node (N)),
661 New_Occurrence_Of
662 (Elists.Node
663 (First_Elmt
664 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
665 Loc)))));
666 Analyze_And_Resolve (N, PtrT);
667 end if;
668 end if;
669 end Displace_Allocator_Pointer;
671 ---------------------------------
672 -- Expand_Allocator_Expression --
673 ---------------------------------
675 procedure Expand_Allocator_Expression (N : Node_Id) is
676 Loc : constant Source_Ptr := Sloc (N);
677 Exp : constant Node_Id := Expression (Expression (N));
678 PtrT : constant Entity_Id := Etype (N);
679 DesigT : constant Entity_Id := Designated_Type (PtrT);
681 procedure Apply_Accessibility_Check
682 (Ref : Node_Id;
683 Built_In_Place : Boolean := False);
684 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
685 -- type, generate an accessibility check to verify that the level of the
686 -- type of the created object is not deeper than the level of the access
687 -- type. If the type of the qualified expression is class-wide, then
688 -- always generate the check (except in the case where it is known to be
689 -- unnecessary, see comment below). Otherwise, only generate the check
690 -- if the level of the qualified expression type is statically deeper
691 -- than the access type.
693 -- Although the static accessibility will generally have been performed
694 -- as a legality check, it won't have been done in cases where the
695 -- allocator appears in generic body, so a run-time check is needed in
696 -- general. One special case is when the access type is declared in the
697 -- same scope as the class-wide allocator, in which case the check can
698 -- never fail, so it need not be generated.
700 -- As an open issue, there seem to be cases where the static level
701 -- associated with the class-wide object's underlying type is not
702 -- sufficient to perform the proper accessibility check, such as for
703 -- allocators in nested subprograms or accept statements initialized by
704 -- class-wide formals when the actual originates outside at a deeper
705 -- static level. The nested subprogram case might require passing
706 -- accessibility levels along with class-wide parameters, and the task
707 -- case seems to be an actual gap in the language rules that needs to
708 -- be fixed by the ARG. ???
710 -------------------------------
711 -- Apply_Accessibility_Check --
712 -------------------------------
714 procedure Apply_Accessibility_Check
715 (Ref : Node_Id;
716 Built_In_Place : Boolean := False)
718 Pool_Id : constant Entity_Id := Associated_Storage_Pool (PtrT);
719 Cond : Node_Id;
720 Free_Stmt : Node_Id;
721 Obj_Ref : Node_Id;
722 Stmts : List_Id;
724 begin
725 if Ada_Version >= Ada_2005
726 and then Is_Class_Wide_Type (DesigT)
727 and then not Scope_Suppress.Suppress (Accessibility_Check)
728 and then
729 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
730 or else
731 (Is_Class_Wide_Type (Etype (Exp))
732 and then Scope (PtrT) /= Current_Scope))
733 and then (Tagged_Type_Expansion or else VM_Target /= No_VM)
734 then
735 -- If the allocator was built in place, Ref is already a reference
736 -- to the access object initialized to the result of the allocator
737 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
738 -- Remove_Side_Effects for cases where the build-in-place call may
739 -- still be the prefix of the reference (to avoid generating
740 -- duplicate calls). Otherwise, it is the entity associated with
741 -- the object containing the address of the allocated object.
743 if Built_In_Place then
744 Remove_Side_Effects (Ref);
745 Obj_Ref := New_Copy (Ref);
746 else
747 Obj_Ref := New_Reference_To (Ref, Loc);
748 end if;
750 -- Step 1: Create the object clean up code
752 Stmts := New_List;
754 -- Create an explicit free statement to clean up the allocated
755 -- object in case the accessibility check fails. Generate:
757 -- Free (Obj_Ref);
759 Free_Stmt := Make_Free_Statement (Loc, New_Copy (Obj_Ref));
760 Set_Storage_Pool (Free_Stmt, Pool_Id);
762 Append_To (Stmts, Free_Stmt);
764 -- Finalize the object (if applicable), but wrap the call inside
765 -- a block to ensure that the object would still be deallocated in
766 -- case the finalization fails. Generate:
768 -- begin
769 -- [Deep_]Finalize (Obj_Ref.all);
770 -- exception
771 -- when others =>
772 -- Free (Obj_Ref);
773 -- raise;
774 -- end;
776 if Needs_Finalization (DesigT) then
777 Prepend_To (Stmts,
778 Make_Block_Statement (Loc,
779 Handled_Statement_Sequence =>
780 Make_Handled_Sequence_Of_Statements (Loc,
781 Statements => New_List (
782 Make_Final_Call (
783 Obj_Ref =>
784 Make_Explicit_Dereference (Loc,
785 Prefix => New_Copy (Obj_Ref)),
786 Typ => DesigT)),
788 Exception_Handlers => New_List (
789 Make_Exception_Handler (Loc,
790 Exception_Choices => New_List (
791 Make_Others_Choice (Loc)),
792 Statements => New_List (
793 New_Copy_Tree (Free_Stmt),
794 Make_Raise_Statement (Loc)))))));
795 end if;
797 -- Signal the accessibility failure through a Program_Error
799 Append_To (Stmts,
800 Make_Raise_Program_Error (Loc,
801 Condition => New_Reference_To (Standard_True, Loc),
802 Reason => PE_Accessibility_Check_Failed));
804 -- Step 2: Create the accessibility comparison
806 -- Generate:
807 -- Ref'Tag
809 Obj_Ref :=
810 Make_Attribute_Reference (Loc,
811 Prefix => Obj_Ref,
812 Attribute_Name => Name_Tag);
814 -- For tagged types, determine the accessibility level by looking
815 -- at the type specific data of the dispatch table. Generate:
817 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
819 if Tagged_Type_Expansion then
820 Cond := Build_Get_Access_Level (Loc, Obj_Ref);
822 -- Use a runtime call to determine the accessibility level when
823 -- compiling on virtual machine targets. Generate:
825 -- Get_Access_Level (Ref'Tag)
827 else
828 Cond :=
829 Make_Function_Call (Loc,
830 Name =>
831 New_Reference_To (RTE (RE_Get_Access_Level), Loc),
832 Parameter_Associations => New_List (Obj_Ref));
833 end if;
835 Cond :=
836 Make_Op_Gt (Loc,
837 Left_Opnd => Cond,
838 Right_Opnd =>
839 Make_Integer_Literal (Loc, Type_Access_Level (PtrT)));
841 -- Due to the complexity and side effects of the check, utilize an
842 -- if statement instead of the regular Program_Error circuitry.
844 Insert_Action (N,
845 Make_If_Statement (Loc,
846 Condition => Cond,
847 Then_Statements => Stmts));
848 end if;
849 end Apply_Accessibility_Check;
851 -- Local variables
853 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
854 Indic : constant Node_Id := Subtype_Mark (Expression (N));
855 T : constant Entity_Id := Entity (Indic);
856 Node : Node_Id;
857 Tag_Assign : Node_Id;
858 Temp : Entity_Id;
859 Temp_Decl : Node_Id;
861 TagT : Entity_Id := Empty;
862 -- Type used as source for tag assignment
864 TagR : Node_Id := Empty;
865 -- Target reference for tag assignment
867 -- Start of processing for Expand_Allocator_Expression
869 begin
870 -- Handle call to C++ constructor
872 if Is_CPP_Constructor_Call (Exp) then
873 Make_CPP_Constructor_Call_In_Allocator
874 (Allocator => N,
875 Function_Call => Exp);
876 return;
877 end if;
879 -- In the case of an Ada 2012 allocator whose initial value comes from a
880 -- function call, pass "the accessibility level determined by the point
881 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
882 -- Expand_Call but it couldn't be done there (because the Etype of the
883 -- allocator wasn't set then) so we generate the parameter here. See
884 -- the Boolean variable Defer in (a block within) Expand_Call.
886 if Ada_Version >= Ada_2012 and then Nkind (Exp) = N_Function_Call then
887 declare
888 Subp : Entity_Id;
890 begin
891 if Nkind (Name (Exp)) = N_Explicit_Dereference then
892 Subp := Designated_Type (Etype (Prefix (Name (Exp))));
893 else
894 Subp := Entity (Name (Exp));
895 end if;
897 Subp := Ultimate_Alias (Subp);
899 if Present (Extra_Accessibility_Of_Result (Subp)) then
900 Add_Extra_Actual_To_Call
901 (Subprogram_Call => Exp,
902 Extra_Formal => Extra_Accessibility_Of_Result (Subp),
903 Extra_Actual => Dynamic_Accessibility_Level (PtrT));
904 end if;
905 end;
906 end if;
908 -- Case of tagged type or type requiring finalization
910 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
912 -- Ada 2005 (AI-318-02): If the initialization expression is a call
913 -- to a build-in-place function, then access to the allocated object
914 -- must be passed to the function. Currently we limit such functions
915 -- to those with constrained limited result subtypes, but eventually
916 -- we plan to expand the allowed forms of functions that are treated
917 -- as build-in-place.
919 if Ada_Version >= Ada_2005
920 and then Is_Build_In_Place_Function_Call (Exp)
921 then
922 Make_Build_In_Place_Call_In_Allocator (N, Exp);
923 Apply_Accessibility_Check (N, Built_In_Place => True);
924 return;
925 end if;
927 -- Actions inserted before:
928 -- Temp : constant ptr_T := new T'(Expression);
929 -- Temp._tag = T'tag; -- when not class-wide
930 -- [Deep_]Adjust (Temp.all);
932 -- We analyze by hand the new internal allocator to avoid any
933 -- recursion and inappropriate call to Initialize
935 -- We don't want to remove side effects when the expression must be
936 -- built in place. In the case of a build-in-place function call,
937 -- that could lead to a duplication of the call, which was already
938 -- substituted for the allocator.
940 if not Aggr_In_Place then
941 Remove_Side_Effects (Exp);
942 end if;
944 Temp := Make_Temporary (Loc, 'P', N);
946 -- For a class wide allocation generate the following code:
948 -- type Equiv_Record is record ... end record;
949 -- implicit subtype CW is <Class_Wide_Subytpe>;
950 -- temp : PtrT := new CW'(CW!(expr));
952 if Is_Class_Wide_Type (T) then
953 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
955 -- Ada 2005 (AI-251): If the expression is a class-wide interface
956 -- object we generate code to move up "this" to reference the
957 -- base of the object before allocating the new object.
959 -- Note that Exp'Address is recursively expanded into a call
960 -- to Base_Address (Exp.Tag)
962 if Is_Class_Wide_Type (Etype (Exp))
963 and then Is_Interface (Etype (Exp))
964 and then Tagged_Type_Expansion
965 then
966 Set_Expression
967 (Expression (N),
968 Unchecked_Convert_To (Entity (Indic),
969 Make_Explicit_Dereference (Loc,
970 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
971 Make_Attribute_Reference (Loc,
972 Prefix => Exp,
973 Attribute_Name => Name_Address)))));
974 else
975 Set_Expression
976 (Expression (N),
977 Unchecked_Convert_To (Entity (Indic), Exp));
978 end if;
980 Analyze_And_Resolve (Expression (N), Entity (Indic));
981 end if;
983 -- Processing for allocators returning non-interface types
985 if not Is_Interface (Directly_Designated_Type (PtrT)) then
986 if Aggr_In_Place then
987 Temp_Decl :=
988 Make_Object_Declaration (Loc,
989 Defining_Identifier => Temp,
990 Object_Definition => New_Reference_To (PtrT, Loc),
991 Expression =>
992 Make_Allocator (Loc,
993 Expression =>
994 New_Reference_To (Etype (Exp), Loc)));
996 -- Copy the Comes_From_Source flag for the allocator we just
997 -- built, since logically this allocator is a replacement of
998 -- the original allocator node. This is for proper handling of
999 -- restriction No_Implicit_Heap_Allocations.
1001 Set_Comes_From_Source
1002 (Expression (Temp_Decl), Comes_From_Source (N));
1004 Set_No_Initialization (Expression (Temp_Decl));
1005 Insert_Action (N, Temp_Decl);
1007 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1008 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1010 -- Attach the object to the associated finalization master.
1011 -- This is done manually on .NET/JVM since those compilers do
1012 -- no support pools and can't benefit from internally generated
1013 -- Allocate / Deallocate procedures.
1015 if VM_Target /= No_VM
1016 and then Is_Controlled (DesigT)
1017 and then Present (Finalization_Master (PtrT))
1018 then
1019 Insert_Action (N,
1020 Make_Attach_Call (
1021 Obj_Ref =>
1022 New_Reference_To (Temp, Loc),
1023 Ptr_Typ => PtrT));
1024 end if;
1026 else
1027 Node := Relocate_Node (N);
1028 Set_Analyzed (Node);
1030 Temp_Decl :=
1031 Make_Object_Declaration (Loc,
1032 Defining_Identifier => Temp,
1033 Constant_Present => True,
1034 Object_Definition => New_Reference_To (PtrT, Loc),
1035 Expression => Node);
1037 Insert_Action (N, Temp_Decl);
1038 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1040 -- Attach the object to the associated finalization master.
1041 -- This is done manually on .NET/JVM since those compilers do
1042 -- no support pools and can't benefit from internally generated
1043 -- Allocate / Deallocate procedures.
1045 if VM_Target /= No_VM
1046 and then Is_Controlled (DesigT)
1047 and then Present (Finalization_Master (PtrT))
1048 then
1049 Insert_Action (N,
1050 Make_Attach_Call (
1051 Obj_Ref =>
1052 New_Reference_To (Temp, Loc),
1053 Ptr_Typ => PtrT));
1054 end if;
1055 end if;
1057 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
1058 -- interface type. In this case we use the type of the qualified
1059 -- expression to allocate the object.
1061 else
1062 declare
1063 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
1064 New_Decl : Node_Id;
1066 begin
1067 New_Decl :=
1068 Make_Full_Type_Declaration (Loc,
1069 Defining_Identifier => Def_Id,
1070 Type_Definition =>
1071 Make_Access_To_Object_Definition (Loc,
1072 All_Present => True,
1073 Null_Exclusion_Present => False,
1074 Constant_Present =>
1075 Is_Access_Constant (Etype (N)),
1076 Subtype_Indication =>
1077 New_Reference_To (Etype (Exp), Loc)));
1079 Insert_Action (N, New_Decl);
1081 -- Inherit the allocation-related attributes from the original
1082 -- access type.
1084 Set_Finalization_Master (Def_Id, Finalization_Master (PtrT));
1086 Set_Associated_Storage_Pool (Def_Id,
1087 Associated_Storage_Pool (PtrT));
1089 -- Declare the object using the previous type declaration
1091 if Aggr_In_Place then
1092 Temp_Decl :=
1093 Make_Object_Declaration (Loc,
1094 Defining_Identifier => Temp,
1095 Object_Definition => New_Reference_To (Def_Id, Loc),
1096 Expression =>
1097 Make_Allocator (Loc,
1098 New_Reference_To (Etype (Exp), Loc)));
1100 -- Copy the Comes_From_Source flag for the allocator we just
1101 -- built, since logically this allocator is a replacement of
1102 -- the original allocator node. This is for proper handling
1103 -- of restriction No_Implicit_Heap_Allocations.
1105 Set_Comes_From_Source
1106 (Expression (Temp_Decl), Comes_From_Source (N));
1108 Set_No_Initialization (Expression (Temp_Decl));
1109 Insert_Action (N, Temp_Decl);
1111 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1112 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1114 else
1115 Node := Relocate_Node (N);
1116 Set_Analyzed (Node);
1118 Temp_Decl :=
1119 Make_Object_Declaration (Loc,
1120 Defining_Identifier => Temp,
1121 Constant_Present => True,
1122 Object_Definition => New_Reference_To (Def_Id, Loc),
1123 Expression => Node);
1125 Insert_Action (N, Temp_Decl);
1126 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1127 end if;
1129 -- Generate an additional object containing the address of the
1130 -- returned object. The type of this second object declaration
1131 -- is the correct type required for the common processing that
1132 -- is still performed by this subprogram. The displacement of
1133 -- this pointer to reference the component associated with the
1134 -- interface type will be done at the end of common processing.
1136 New_Decl :=
1137 Make_Object_Declaration (Loc,
1138 Defining_Identifier => Make_Temporary (Loc, 'P'),
1139 Object_Definition => New_Reference_To (PtrT, Loc),
1140 Expression =>
1141 Unchecked_Convert_To (PtrT,
1142 New_Reference_To (Temp, Loc)));
1144 Insert_Action (N, New_Decl);
1146 Temp_Decl := New_Decl;
1147 Temp := Defining_Identifier (New_Decl);
1148 end;
1149 end if;
1151 Apply_Accessibility_Check (Temp);
1153 -- Generate the tag assignment
1155 -- Suppress the tag assignment when VM_Target because VM tags are
1156 -- represented implicitly in objects.
1158 if not Tagged_Type_Expansion then
1159 null;
1161 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1162 -- interface objects because in this case the tag does not change.
1164 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
1165 pragma Assert (Is_Class_Wide_Type
1166 (Directly_Designated_Type (Etype (N))));
1167 null;
1169 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
1170 TagT := T;
1171 TagR := New_Reference_To (Temp, Loc);
1173 elsif Is_Private_Type (T)
1174 and then Is_Tagged_Type (Underlying_Type (T))
1175 then
1176 TagT := Underlying_Type (T);
1177 TagR :=
1178 Unchecked_Convert_To (Underlying_Type (T),
1179 Make_Explicit_Dereference (Loc,
1180 Prefix => New_Reference_To (Temp, Loc)));
1181 end if;
1183 if Present (TagT) then
1184 declare
1185 Full_T : constant Entity_Id := Underlying_Type (TagT);
1186 begin
1187 Tag_Assign :=
1188 Make_Assignment_Statement (Loc,
1189 Name =>
1190 Make_Selected_Component (Loc,
1191 Prefix => TagR,
1192 Selector_Name =>
1193 New_Reference_To (First_Tag_Component (Full_T), Loc)),
1194 Expression =>
1195 Unchecked_Convert_To (RTE (RE_Tag),
1196 New_Reference_To
1197 (Elists.Node
1198 (First_Elmt (Access_Disp_Table (Full_T))), Loc)));
1199 end;
1201 -- The previous assignment has to be done in any case
1203 Set_Assignment_OK (Name (Tag_Assign));
1204 Insert_Action (N, Tag_Assign);
1205 end if;
1207 if Needs_Finalization (DesigT)
1208 and then Needs_Finalization (T)
1209 then
1210 -- Generate an Adjust call if the object will be moved. In Ada
1211 -- 2005, the object may be inherently limited, in which case
1212 -- there is no Adjust procedure, and the object is built in
1213 -- place. In Ada 95, the object can be limited but not
1214 -- inherently limited if this allocator came from a return
1215 -- statement (we're allocating the result on the secondary
1216 -- stack). In that case, the object will be moved, so we _do_
1217 -- want to Adjust.
1219 if not Aggr_In_Place
1220 and then not Is_Immutably_Limited_Type (T)
1221 then
1222 Insert_Action (N,
1223 Make_Adjust_Call (
1224 Obj_Ref =>
1226 -- An unchecked conversion is needed in the classwide
1227 -- case because the designated type can be an ancestor
1228 -- of the subtype mark of the allocator.
1230 Unchecked_Convert_To (T,
1231 Make_Explicit_Dereference (Loc,
1232 Prefix => New_Reference_To (Temp, Loc))),
1233 Typ => T));
1234 end if;
1236 -- Generate:
1237 -- Set_Finalize_Address (<PtrT>FM, <T>FD'Unrestricted_Access);
1239 -- Do not generate this call in the following cases:
1241 -- * .NET/JVM - these targets do not support address arithmetic
1242 -- and unchecked conversion, key elements of Finalize_Address.
1244 -- * Alfa mode - the call is useless and results in unwanted
1245 -- expansion.
1247 -- * CodePeer mode - TSS primitive Finalize_Address is not
1248 -- created in this mode.
1250 if VM_Target = No_VM
1251 and then not Alfa_Mode
1252 and then not CodePeer_Mode
1253 and then Present (Finalization_Master (PtrT))
1254 and then Present (Temp_Decl)
1255 and then Nkind (Expression (Temp_Decl)) = N_Allocator
1256 then
1257 Insert_Action (N,
1258 Make_Set_Finalize_Address_Call
1259 (Loc => Loc,
1260 Typ => T,
1261 Ptr_Typ => PtrT));
1262 end if;
1263 end if;
1265 Rewrite (N, New_Reference_To (Temp, Loc));
1266 Analyze_And_Resolve (N, PtrT);
1268 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1269 -- component containing the secondary dispatch table of the interface
1270 -- type.
1272 if Is_Interface (Directly_Designated_Type (PtrT)) then
1273 Displace_Allocator_Pointer (N);
1274 end if;
1276 elsif Aggr_In_Place then
1277 Temp := Make_Temporary (Loc, 'P', N);
1278 Temp_Decl :=
1279 Make_Object_Declaration (Loc,
1280 Defining_Identifier => Temp,
1281 Object_Definition => New_Reference_To (PtrT, Loc),
1282 Expression =>
1283 Make_Allocator (Loc,
1284 Expression => New_Reference_To (Etype (Exp), Loc)));
1286 -- Copy the Comes_From_Source flag for the allocator we just built,
1287 -- since logically this allocator is a replacement of the original
1288 -- allocator node. This is for proper handling of restriction
1289 -- No_Implicit_Heap_Allocations.
1291 Set_Comes_From_Source
1292 (Expression (Temp_Decl), Comes_From_Source (N));
1294 Set_No_Initialization (Expression (Temp_Decl));
1295 Insert_Action (N, Temp_Decl);
1297 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1298 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1300 -- Attach the object to the associated finalization master. Thisis
1301 -- done manually on .NET/JVM since those compilers do no support
1302 -- pools and cannot benefit from internally generated Allocate and
1303 -- Deallocate procedures.
1305 if VM_Target /= No_VM
1306 and then Is_Controlled (DesigT)
1307 and then Present (Finalization_Master (PtrT))
1308 then
1309 Insert_Action (N,
1310 Make_Attach_Call
1311 (Obj_Ref => New_Reference_To (Temp, Loc),
1312 Ptr_Typ => PtrT));
1313 end if;
1315 Rewrite (N, New_Reference_To (Temp, Loc));
1316 Analyze_And_Resolve (N, PtrT);
1318 elsif Is_Access_Type (T)
1319 and then Can_Never_Be_Null (T)
1320 then
1321 Install_Null_Excluding_Check (Exp);
1323 elsif Is_Access_Type (DesigT)
1324 and then Nkind (Exp) = N_Allocator
1325 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1326 then
1327 -- Apply constraint to designated subtype indication
1329 Apply_Constraint_Check (Expression (Exp),
1330 Designated_Type (DesigT),
1331 No_Sliding => True);
1333 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1335 -- Propagate constraint_error to enclosing allocator
1337 Rewrite (Exp, New_Copy (Expression (Exp)));
1338 end if;
1340 else
1341 Build_Allocate_Deallocate_Proc (N, True);
1343 -- If we have:
1344 -- type A is access T1;
1345 -- X : A := new T2'(...);
1346 -- T1 and T2 can be different subtypes, and we might need to check
1347 -- both constraints. First check against the type of the qualified
1348 -- expression.
1350 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1352 if Do_Range_Check (Exp) then
1353 Set_Do_Range_Check (Exp, False);
1354 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1355 end if;
1357 -- A check is also needed in cases where the designated subtype is
1358 -- constrained and differs from the subtype given in the qualified
1359 -- expression. Note that the check on the qualified expression does
1360 -- not allow sliding, but this check does (a relaxation from Ada 83).
1362 if Is_Constrained (DesigT)
1363 and then not Subtypes_Statically_Match (T, DesigT)
1364 then
1365 Apply_Constraint_Check
1366 (Exp, DesigT, No_Sliding => False);
1368 if Do_Range_Check (Exp) then
1369 Set_Do_Range_Check (Exp, False);
1370 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1371 end if;
1372 end if;
1374 -- For an access to unconstrained packed array, GIGI needs to see an
1375 -- expression with a constrained subtype in order to compute the
1376 -- proper size for the allocator.
1378 if Is_Array_Type (T)
1379 and then not Is_Constrained (T)
1380 and then Is_Packed (T)
1381 then
1382 declare
1383 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1384 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1385 begin
1386 Insert_Action (Exp,
1387 Make_Subtype_Declaration (Loc,
1388 Defining_Identifier => ConstrT,
1389 Subtype_Indication =>
1390 Make_Subtype_From_Expr (Internal_Exp, T)));
1391 Freeze_Itype (ConstrT, Exp);
1392 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1393 end;
1394 end if;
1396 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1397 -- to a build-in-place function, then access to the allocated object
1398 -- must be passed to the function. Currently we limit such functions
1399 -- to those with constrained limited result subtypes, but eventually
1400 -- we plan to expand the allowed forms of functions that are treated
1401 -- as build-in-place.
1403 if Ada_Version >= Ada_2005
1404 and then Is_Build_In_Place_Function_Call (Exp)
1405 then
1406 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1407 end if;
1408 end if;
1410 exception
1411 when RE_Not_Available =>
1412 return;
1413 end Expand_Allocator_Expression;
1415 -----------------------------
1416 -- Expand_Array_Comparison --
1417 -----------------------------
1419 -- Expansion is only required in the case of array types. For the unpacked
1420 -- case, an appropriate runtime routine is called. For packed cases, and
1421 -- also in some other cases where a runtime routine cannot be called, the
1422 -- form of the expansion is:
1424 -- [body for greater_nn; boolean_expression]
1426 -- The body is built by Make_Array_Comparison_Op, and the form of the
1427 -- Boolean expression depends on the operator involved.
1429 procedure Expand_Array_Comparison (N : Node_Id) is
1430 Loc : constant Source_Ptr := Sloc (N);
1431 Op1 : Node_Id := Left_Opnd (N);
1432 Op2 : Node_Id := Right_Opnd (N);
1433 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1434 Ctyp : constant Entity_Id := Component_Type (Typ1);
1436 Expr : Node_Id;
1437 Func_Body : Node_Id;
1438 Func_Name : Entity_Id;
1440 Comp : RE_Id;
1442 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1443 -- True for byte addressable target
1445 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1446 -- Returns True if the length of the given operand is known to be less
1447 -- than 4. Returns False if this length is known to be four or greater
1448 -- or is not known at compile time.
1450 ------------------------
1451 -- Length_Less_Than_4 --
1452 ------------------------
1454 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1455 Otyp : constant Entity_Id := Etype (Opnd);
1457 begin
1458 if Ekind (Otyp) = E_String_Literal_Subtype then
1459 return String_Literal_Length (Otyp) < 4;
1461 else
1462 declare
1463 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1464 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1465 Hi : constant Node_Id := Type_High_Bound (Ityp);
1466 Lov : Uint;
1467 Hiv : Uint;
1469 begin
1470 if Compile_Time_Known_Value (Lo) then
1471 Lov := Expr_Value (Lo);
1472 else
1473 return False;
1474 end if;
1476 if Compile_Time_Known_Value (Hi) then
1477 Hiv := Expr_Value (Hi);
1478 else
1479 return False;
1480 end if;
1482 return Hiv < Lov + 3;
1483 end;
1484 end if;
1485 end Length_Less_Than_4;
1487 -- Start of processing for Expand_Array_Comparison
1489 begin
1490 -- Deal first with unpacked case, where we can call a runtime routine
1491 -- except that we avoid this for targets for which are not addressable
1492 -- by bytes, and for the JVM/CIL, since they do not support direct
1493 -- addressing of array components.
1495 if not Is_Bit_Packed_Array (Typ1)
1496 and then Byte_Addressable
1497 and then VM_Target = No_VM
1498 then
1499 -- The call we generate is:
1501 -- Compare_Array_xn[_Unaligned]
1502 -- (left'address, right'address, left'length, right'length) <op> 0
1504 -- x = U for unsigned, S for signed
1505 -- n = 8,16,32,64 for component size
1506 -- Add _Unaligned if length < 4 and component size is 8.
1507 -- <op> is the standard comparison operator
1509 if Component_Size (Typ1) = 8 then
1510 if Length_Less_Than_4 (Op1)
1511 or else
1512 Length_Less_Than_4 (Op2)
1513 then
1514 if Is_Unsigned_Type (Ctyp) then
1515 Comp := RE_Compare_Array_U8_Unaligned;
1516 else
1517 Comp := RE_Compare_Array_S8_Unaligned;
1518 end if;
1520 else
1521 if Is_Unsigned_Type (Ctyp) then
1522 Comp := RE_Compare_Array_U8;
1523 else
1524 Comp := RE_Compare_Array_S8;
1525 end if;
1526 end if;
1528 elsif Component_Size (Typ1) = 16 then
1529 if Is_Unsigned_Type (Ctyp) then
1530 Comp := RE_Compare_Array_U16;
1531 else
1532 Comp := RE_Compare_Array_S16;
1533 end if;
1535 elsif Component_Size (Typ1) = 32 then
1536 if Is_Unsigned_Type (Ctyp) then
1537 Comp := RE_Compare_Array_U32;
1538 else
1539 Comp := RE_Compare_Array_S32;
1540 end if;
1542 else pragma Assert (Component_Size (Typ1) = 64);
1543 if Is_Unsigned_Type (Ctyp) then
1544 Comp := RE_Compare_Array_U64;
1545 else
1546 Comp := RE_Compare_Array_S64;
1547 end if;
1548 end if;
1550 Remove_Side_Effects (Op1, Name_Req => True);
1551 Remove_Side_Effects (Op2, Name_Req => True);
1553 Rewrite (Op1,
1554 Make_Function_Call (Sloc (Op1),
1555 Name => New_Occurrence_Of (RTE (Comp), Loc),
1557 Parameter_Associations => New_List (
1558 Make_Attribute_Reference (Loc,
1559 Prefix => Relocate_Node (Op1),
1560 Attribute_Name => Name_Address),
1562 Make_Attribute_Reference (Loc,
1563 Prefix => Relocate_Node (Op2),
1564 Attribute_Name => Name_Address),
1566 Make_Attribute_Reference (Loc,
1567 Prefix => Relocate_Node (Op1),
1568 Attribute_Name => Name_Length),
1570 Make_Attribute_Reference (Loc,
1571 Prefix => Relocate_Node (Op2),
1572 Attribute_Name => Name_Length))));
1574 Rewrite (Op2,
1575 Make_Integer_Literal (Sloc (Op2),
1576 Intval => Uint_0));
1578 Analyze_And_Resolve (Op1, Standard_Integer);
1579 Analyze_And_Resolve (Op2, Standard_Integer);
1580 return;
1581 end if;
1583 -- Cases where we cannot make runtime call
1585 -- For (a <= b) we convert to not (a > b)
1587 if Chars (N) = Name_Op_Le then
1588 Rewrite (N,
1589 Make_Op_Not (Loc,
1590 Right_Opnd =>
1591 Make_Op_Gt (Loc,
1592 Left_Opnd => Op1,
1593 Right_Opnd => Op2)));
1594 Analyze_And_Resolve (N, Standard_Boolean);
1595 return;
1597 -- For < the Boolean expression is
1598 -- greater__nn (op2, op1)
1600 elsif Chars (N) = Name_Op_Lt then
1601 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1603 -- Switch operands
1605 Op1 := Right_Opnd (N);
1606 Op2 := Left_Opnd (N);
1608 -- For (a >= b) we convert to not (a < b)
1610 elsif Chars (N) = Name_Op_Ge then
1611 Rewrite (N,
1612 Make_Op_Not (Loc,
1613 Right_Opnd =>
1614 Make_Op_Lt (Loc,
1615 Left_Opnd => Op1,
1616 Right_Opnd => Op2)));
1617 Analyze_And_Resolve (N, Standard_Boolean);
1618 return;
1620 -- For > the Boolean expression is
1621 -- greater__nn (op1, op2)
1623 else
1624 pragma Assert (Chars (N) = Name_Op_Gt);
1625 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1626 end if;
1628 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1629 Expr :=
1630 Make_Function_Call (Loc,
1631 Name => New_Reference_To (Func_Name, Loc),
1632 Parameter_Associations => New_List (Op1, Op2));
1634 Insert_Action (N, Func_Body);
1635 Rewrite (N, Expr);
1636 Analyze_And_Resolve (N, Standard_Boolean);
1638 exception
1639 when RE_Not_Available =>
1640 return;
1641 end Expand_Array_Comparison;
1643 ---------------------------
1644 -- Expand_Array_Equality --
1645 ---------------------------
1647 -- Expand an equality function for multi-dimensional arrays. Here is an
1648 -- example of such a function for Nb_Dimension = 2
1650 -- function Enn (A : atyp; B : btyp) return boolean is
1651 -- begin
1652 -- if (A'length (1) = 0 or else A'length (2) = 0)
1653 -- and then
1654 -- (B'length (1) = 0 or else B'length (2) = 0)
1655 -- then
1656 -- return True; -- RM 4.5.2(22)
1657 -- end if;
1659 -- if A'length (1) /= B'length (1)
1660 -- or else
1661 -- A'length (2) /= B'length (2)
1662 -- then
1663 -- return False; -- RM 4.5.2(23)
1664 -- end if;
1666 -- declare
1667 -- A1 : Index_T1 := A'first (1);
1668 -- B1 : Index_T1 := B'first (1);
1669 -- begin
1670 -- loop
1671 -- declare
1672 -- A2 : Index_T2 := A'first (2);
1673 -- B2 : Index_T2 := B'first (2);
1674 -- begin
1675 -- loop
1676 -- if A (A1, A2) /= B (B1, B2) then
1677 -- return False;
1678 -- end if;
1680 -- exit when A2 = A'last (2);
1681 -- A2 := Index_T2'succ (A2);
1682 -- B2 := Index_T2'succ (B2);
1683 -- end loop;
1684 -- end;
1686 -- exit when A1 = A'last (1);
1687 -- A1 := Index_T1'succ (A1);
1688 -- B1 := Index_T1'succ (B1);
1689 -- end loop;
1690 -- end;
1692 -- return true;
1693 -- end Enn;
1695 -- Note on the formal types used (atyp and btyp). If either of the arrays
1696 -- is of a private type, we use the underlying type, and do an unchecked
1697 -- conversion of the actual. If either of the arrays has a bound depending
1698 -- on a discriminant, then we use the base type since otherwise we have an
1699 -- escaped discriminant in the function.
1701 -- If both arrays are constrained and have the same bounds, we can generate
1702 -- a loop with an explicit iteration scheme using a 'Range attribute over
1703 -- the first array.
1705 function Expand_Array_Equality
1706 (Nod : Node_Id;
1707 Lhs : Node_Id;
1708 Rhs : Node_Id;
1709 Bodies : List_Id;
1710 Typ : Entity_Id) return Node_Id
1712 Loc : constant Source_Ptr := Sloc (Nod);
1713 Decls : constant List_Id := New_List;
1714 Index_List1 : constant List_Id := New_List;
1715 Index_List2 : constant List_Id := New_List;
1717 Actuals : List_Id;
1718 Formals : List_Id;
1719 Func_Name : Entity_Id;
1720 Func_Body : Node_Id;
1722 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1723 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1725 Ltyp : Entity_Id;
1726 Rtyp : Entity_Id;
1727 -- The parameter types to be used for the formals
1729 function Arr_Attr
1730 (Arr : Entity_Id;
1731 Nam : Name_Id;
1732 Num : Int) return Node_Id;
1733 -- This builds the attribute reference Arr'Nam (Expr)
1735 function Component_Equality (Typ : Entity_Id) return Node_Id;
1736 -- Create one statement to compare corresponding components, designated
1737 -- by a full set of indexes.
1739 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1740 -- Given one of the arguments, computes the appropriate type to be used
1741 -- for that argument in the corresponding function formal
1743 function Handle_One_Dimension
1744 (N : Int;
1745 Index : Node_Id) return Node_Id;
1746 -- This procedure returns the following code
1748 -- declare
1749 -- Bn : Index_T := B'First (N);
1750 -- begin
1751 -- loop
1752 -- xxx
1753 -- exit when An = A'Last (N);
1754 -- An := Index_T'Succ (An)
1755 -- Bn := Index_T'Succ (Bn)
1756 -- end loop;
1757 -- end;
1759 -- If both indexes are constrained and identical, the procedure
1760 -- returns a simpler loop:
1762 -- for An in A'Range (N) loop
1763 -- xxx
1764 -- end loop
1766 -- N is the dimension for which we are generating a loop. Index is the
1767 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1768 -- xxx statement is either the loop or declare for the next dimension
1769 -- or if this is the last dimension the comparison of corresponding
1770 -- components of the arrays.
1772 -- The actual way the code works is to return the comparison of
1773 -- corresponding components for the N+1 call. That's neater!
1775 function Test_Empty_Arrays return Node_Id;
1776 -- This function constructs the test for both arrays being empty
1777 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1778 -- and then
1779 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1781 function Test_Lengths_Correspond return Node_Id;
1782 -- This function constructs the test for arrays having different lengths
1783 -- in at least one index position, in which case the resulting code is:
1785 -- A'length (1) /= B'length (1)
1786 -- or else
1787 -- A'length (2) /= B'length (2)
1788 -- or else
1789 -- ...
1791 --------------
1792 -- Arr_Attr --
1793 --------------
1795 function Arr_Attr
1796 (Arr : Entity_Id;
1797 Nam : Name_Id;
1798 Num : Int) return Node_Id
1800 begin
1801 return
1802 Make_Attribute_Reference (Loc,
1803 Attribute_Name => Nam,
1804 Prefix => New_Reference_To (Arr, Loc),
1805 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1806 end Arr_Attr;
1808 ------------------------
1809 -- Component_Equality --
1810 ------------------------
1812 function Component_Equality (Typ : Entity_Id) return Node_Id is
1813 Test : Node_Id;
1814 L, R : Node_Id;
1816 begin
1817 -- if a(i1...) /= b(j1...) then return false; end if;
1819 L :=
1820 Make_Indexed_Component (Loc,
1821 Prefix => Make_Identifier (Loc, Chars (A)),
1822 Expressions => Index_List1);
1824 R :=
1825 Make_Indexed_Component (Loc,
1826 Prefix => Make_Identifier (Loc, Chars (B)),
1827 Expressions => Index_List2);
1829 Test := Expand_Composite_Equality
1830 (Nod, Component_Type (Typ), L, R, Decls);
1832 -- If some (sub)component is an unchecked_union, the whole operation
1833 -- will raise program error.
1835 if Nkind (Test) = N_Raise_Program_Error then
1837 -- This node is going to be inserted at a location where a
1838 -- statement is expected: clear its Etype so analysis will set
1839 -- it to the expected Standard_Void_Type.
1841 Set_Etype (Test, Empty);
1842 return Test;
1844 else
1845 return
1846 Make_Implicit_If_Statement (Nod,
1847 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1848 Then_Statements => New_List (
1849 Make_Simple_Return_Statement (Loc,
1850 Expression => New_Occurrence_Of (Standard_False, Loc))));
1851 end if;
1852 end Component_Equality;
1854 ------------------
1855 -- Get_Arg_Type --
1856 ------------------
1858 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1859 T : Entity_Id;
1860 X : Node_Id;
1862 begin
1863 T := Etype (N);
1865 if No (T) then
1866 return Typ;
1868 else
1869 T := Underlying_Type (T);
1871 X := First_Index (T);
1872 while Present (X) loop
1873 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1874 or else
1875 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1876 then
1877 T := Base_Type (T);
1878 exit;
1879 end if;
1881 Next_Index (X);
1882 end loop;
1884 return T;
1885 end if;
1886 end Get_Arg_Type;
1888 --------------------------
1889 -- Handle_One_Dimension --
1890 ---------------------------
1892 function Handle_One_Dimension
1893 (N : Int;
1894 Index : Node_Id) return Node_Id
1896 Need_Separate_Indexes : constant Boolean :=
1897 Ltyp /= Rtyp
1898 or else not Is_Constrained (Ltyp);
1899 -- If the index types are identical, and we are working with
1900 -- constrained types, then we can use the same index for both
1901 -- of the arrays.
1903 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1905 Bn : Entity_Id;
1906 Index_T : Entity_Id;
1907 Stm_List : List_Id;
1908 Loop_Stm : Node_Id;
1910 begin
1911 if N > Number_Dimensions (Ltyp) then
1912 return Component_Equality (Ltyp);
1913 end if;
1915 -- Case where we generate a loop
1917 Index_T := Base_Type (Etype (Index));
1919 if Need_Separate_Indexes then
1920 Bn := Make_Temporary (Loc, 'B');
1921 else
1922 Bn := An;
1923 end if;
1925 Append (New_Reference_To (An, Loc), Index_List1);
1926 Append (New_Reference_To (Bn, Loc), Index_List2);
1928 Stm_List := New_List (
1929 Handle_One_Dimension (N + 1, Next_Index (Index)));
1931 if Need_Separate_Indexes then
1933 -- Generate guard for loop, followed by increments of indexes
1935 Append_To (Stm_List,
1936 Make_Exit_Statement (Loc,
1937 Condition =>
1938 Make_Op_Eq (Loc,
1939 Left_Opnd => New_Reference_To (An, Loc),
1940 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1942 Append_To (Stm_List,
1943 Make_Assignment_Statement (Loc,
1944 Name => New_Reference_To (An, Loc),
1945 Expression =>
1946 Make_Attribute_Reference (Loc,
1947 Prefix => New_Reference_To (Index_T, Loc),
1948 Attribute_Name => Name_Succ,
1949 Expressions => New_List (New_Reference_To (An, Loc)))));
1951 Append_To (Stm_List,
1952 Make_Assignment_Statement (Loc,
1953 Name => New_Reference_To (Bn, Loc),
1954 Expression =>
1955 Make_Attribute_Reference (Loc,
1956 Prefix => New_Reference_To (Index_T, Loc),
1957 Attribute_Name => Name_Succ,
1958 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1959 end if;
1961 -- If separate indexes, we need a declare block for An and Bn, and a
1962 -- loop without an iteration scheme.
1964 if Need_Separate_Indexes then
1965 Loop_Stm :=
1966 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1968 return
1969 Make_Block_Statement (Loc,
1970 Declarations => New_List (
1971 Make_Object_Declaration (Loc,
1972 Defining_Identifier => An,
1973 Object_Definition => New_Reference_To (Index_T, Loc),
1974 Expression => Arr_Attr (A, Name_First, N)),
1976 Make_Object_Declaration (Loc,
1977 Defining_Identifier => Bn,
1978 Object_Definition => New_Reference_To (Index_T, Loc),
1979 Expression => Arr_Attr (B, Name_First, N))),
1981 Handled_Statement_Sequence =>
1982 Make_Handled_Sequence_Of_Statements (Loc,
1983 Statements => New_List (Loop_Stm)));
1985 -- If no separate indexes, return loop statement with explicit
1986 -- iteration scheme on its own
1988 else
1989 Loop_Stm :=
1990 Make_Implicit_Loop_Statement (Nod,
1991 Statements => Stm_List,
1992 Iteration_Scheme =>
1993 Make_Iteration_Scheme (Loc,
1994 Loop_Parameter_Specification =>
1995 Make_Loop_Parameter_Specification (Loc,
1996 Defining_Identifier => An,
1997 Discrete_Subtype_Definition =>
1998 Arr_Attr (A, Name_Range, N))));
1999 return Loop_Stm;
2000 end if;
2001 end Handle_One_Dimension;
2003 -----------------------
2004 -- Test_Empty_Arrays --
2005 -----------------------
2007 function Test_Empty_Arrays return Node_Id is
2008 Alist : Node_Id;
2009 Blist : Node_Id;
2011 Atest : Node_Id;
2012 Btest : Node_Id;
2014 begin
2015 Alist := Empty;
2016 Blist := Empty;
2017 for J in 1 .. Number_Dimensions (Ltyp) loop
2018 Atest :=
2019 Make_Op_Eq (Loc,
2020 Left_Opnd => Arr_Attr (A, Name_Length, J),
2021 Right_Opnd => Make_Integer_Literal (Loc, 0));
2023 Btest :=
2024 Make_Op_Eq (Loc,
2025 Left_Opnd => Arr_Attr (B, Name_Length, J),
2026 Right_Opnd => Make_Integer_Literal (Loc, 0));
2028 if No (Alist) then
2029 Alist := Atest;
2030 Blist := Btest;
2032 else
2033 Alist :=
2034 Make_Or_Else (Loc,
2035 Left_Opnd => Relocate_Node (Alist),
2036 Right_Opnd => Atest);
2038 Blist :=
2039 Make_Or_Else (Loc,
2040 Left_Opnd => Relocate_Node (Blist),
2041 Right_Opnd => Btest);
2042 end if;
2043 end loop;
2045 return
2046 Make_And_Then (Loc,
2047 Left_Opnd => Alist,
2048 Right_Opnd => Blist);
2049 end Test_Empty_Arrays;
2051 -----------------------------
2052 -- Test_Lengths_Correspond --
2053 -----------------------------
2055 function Test_Lengths_Correspond return Node_Id is
2056 Result : Node_Id;
2057 Rtest : Node_Id;
2059 begin
2060 Result := Empty;
2061 for J in 1 .. Number_Dimensions (Ltyp) loop
2062 Rtest :=
2063 Make_Op_Ne (Loc,
2064 Left_Opnd => Arr_Attr (A, Name_Length, J),
2065 Right_Opnd => Arr_Attr (B, Name_Length, J));
2067 if No (Result) then
2068 Result := Rtest;
2069 else
2070 Result :=
2071 Make_Or_Else (Loc,
2072 Left_Opnd => Relocate_Node (Result),
2073 Right_Opnd => Rtest);
2074 end if;
2075 end loop;
2077 return Result;
2078 end Test_Lengths_Correspond;
2080 -- Start of processing for Expand_Array_Equality
2082 begin
2083 Ltyp := Get_Arg_Type (Lhs);
2084 Rtyp := Get_Arg_Type (Rhs);
2086 -- For now, if the argument types are not the same, go to the base type,
2087 -- since the code assumes that the formals have the same type. This is
2088 -- fixable in future ???
2090 if Ltyp /= Rtyp then
2091 Ltyp := Base_Type (Ltyp);
2092 Rtyp := Base_Type (Rtyp);
2093 pragma Assert (Ltyp = Rtyp);
2094 end if;
2096 -- Build list of formals for function
2098 Formals := New_List (
2099 Make_Parameter_Specification (Loc,
2100 Defining_Identifier => A,
2101 Parameter_Type => New_Reference_To (Ltyp, Loc)),
2103 Make_Parameter_Specification (Loc,
2104 Defining_Identifier => B,
2105 Parameter_Type => New_Reference_To (Rtyp, Loc)));
2107 Func_Name := Make_Temporary (Loc, 'E');
2109 -- Build statement sequence for function
2111 Func_Body :=
2112 Make_Subprogram_Body (Loc,
2113 Specification =>
2114 Make_Function_Specification (Loc,
2115 Defining_Unit_Name => Func_Name,
2116 Parameter_Specifications => Formals,
2117 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
2119 Declarations => Decls,
2121 Handled_Statement_Sequence =>
2122 Make_Handled_Sequence_Of_Statements (Loc,
2123 Statements => New_List (
2125 Make_Implicit_If_Statement (Nod,
2126 Condition => Test_Empty_Arrays,
2127 Then_Statements => New_List (
2128 Make_Simple_Return_Statement (Loc,
2129 Expression =>
2130 New_Occurrence_Of (Standard_True, Loc)))),
2132 Make_Implicit_If_Statement (Nod,
2133 Condition => Test_Lengths_Correspond,
2134 Then_Statements => New_List (
2135 Make_Simple_Return_Statement (Loc,
2136 Expression =>
2137 New_Occurrence_Of (Standard_False, Loc)))),
2139 Handle_One_Dimension (1, First_Index (Ltyp)),
2141 Make_Simple_Return_Statement (Loc,
2142 Expression => New_Occurrence_Of (Standard_True, Loc)))));
2144 Set_Has_Completion (Func_Name, True);
2145 Set_Is_Inlined (Func_Name);
2147 -- If the array type is distinct from the type of the arguments, it
2148 -- is the full view of a private type. Apply an unchecked conversion
2149 -- to insure that analysis of the call succeeds.
2151 declare
2152 L, R : Node_Id;
2154 begin
2155 L := Lhs;
2156 R := Rhs;
2158 if No (Etype (Lhs))
2159 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
2160 then
2161 L := OK_Convert_To (Ltyp, Lhs);
2162 end if;
2164 if No (Etype (Rhs))
2165 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
2166 then
2167 R := OK_Convert_To (Rtyp, Rhs);
2168 end if;
2170 Actuals := New_List (L, R);
2171 end;
2173 Append_To (Bodies, Func_Body);
2175 return
2176 Make_Function_Call (Loc,
2177 Name => New_Reference_To (Func_Name, Loc),
2178 Parameter_Associations => Actuals);
2179 end Expand_Array_Equality;
2181 -----------------------------
2182 -- Expand_Boolean_Operator --
2183 -----------------------------
2185 -- Note that we first get the actual subtypes of the operands, since we
2186 -- always want to deal with types that have bounds.
2188 procedure Expand_Boolean_Operator (N : Node_Id) is
2189 Typ : constant Entity_Id := Etype (N);
2191 begin
2192 -- Special case of bit packed array where both operands are known to be
2193 -- properly aligned. In this case we use an efficient run time routine
2194 -- to carry out the operation (see System.Bit_Ops).
2196 if Is_Bit_Packed_Array (Typ)
2197 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
2198 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
2199 then
2200 Expand_Packed_Boolean_Operator (N);
2201 return;
2202 end if;
2204 -- For the normal non-packed case, the general expansion is to build
2205 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2206 -- and then inserting it into the tree. The original operator node is
2207 -- then rewritten as a call to this function. We also use this in the
2208 -- packed case if either operand is a possibly unaligned object.
2210 declare
2211 Loc : constant Source_Ptr := Sloc (N);
2212 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2213 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2214 Func_Body : Node_Id;
2215 Func_Name : Entity_Id;
2217 begin
2218 Convert_To_Actual_Subtype (L);
2219 Convert_To_Actual_Subtype (R);
2220 Ensure_Defined (Etype (L), N);
2221 Ensure_Defined (Etype (R), N);
2222 Apply_Length_Check (R, Etype (L));
2224 if Nkind (N) = N_Op_Xor then
2225 Silly_Boolean_Array_Xor_Test (N, Etype (L));
2226 end if;
2228 if Nkind (Parent (N)) = N_Assignment_Statement
2229 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
2230 then
2231 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
2233 elsif Nkind (Parent (N)) = N_Op_Not
2234 and then Nkind (N) = N_Op_And
2235 and then
2236 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
2237 then
2238 return;
2239 else
2241 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
2242 Func_Name := Defining_Unit_Name (Specification (Func_Body));
2243 Insert_Action (N, Func_Body);
2245 -- Now rewrite the expression with a call
2247 Rewrite (N,
2248 Make_Function_Call (Loc,
2249 Name => New_Reference_To (Func_Name, Loc),
2250 Parameter_Associations =>
2251 New_List (
2253 Make_Type_Conversion
2254 (Loc, New_Reference_To (Etype (L), Loc), R))));
2256 Analyze_And_Resolve (N, Typ);
2257 end if;
2258 end;
2259 end Expand_Boolean_Operator;
2261 ------------------------------------------------
2262 -- Expand_Compare_Minimize_Eliminate_Overflow --
2263 ------------------------------------------------
2265 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is
2266 Loc : constant Source_Ptr := Sloc (N);
2268 Result_Type : constant Entity_Id := Etype (N);
2269 -- Capture result type (could be a derived boolean type)
2271 Llo, Lhi : Uint;
2272 Rlo, Rhi : Uint;
2274 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
2275 -- Entity for Long_Long_Integer'Base
2277 Check : constant Overflow_Check_Type := Overflow_Check_Mode;
2278 -- Current overflow checking mode
2280 procedure Set_True;
2281 procedure Set_False;
2282 -- These procedures rewrite N with an occurrence of Standard_True or
2283 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2285 ---------------
2286 -- Set_False --
2287 ---------------
2289 procedure Set_False is
2290 begin
2291 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2292 Warn_On_Known_Condition (N);
2293 end Set_False;
2295 --------------
2296 -- Set_True --
2297 --------------
2299 procedure Set_True is
2300 begin
2301 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
2302 Warn_On_Known_Condition (N);
2303 end Set_True;
2305 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2307 begin
2308 -- Nothing to do unless we have a comparison operator with operands
2309 -- that are signed integer types, and we are operating in either
2310 -- MINIMIZED or ELIMINATED overflow checking mode.
2312 if Nkind (N) not in N_Op_Compare
2313 or else Check not in Minimized_Or_Eliminated
2314 or else not Is_Signed_Integer_Type (Etype (Left_Opnd (N)))
2315 then
2316 return;
2317 end if;
2319 -- OK, this is the case we are interested in. First step is to process
2320 -- our operands using the Minimize_Eliminate circuitry which applies
2321 -- this processing to the two operand subtrees.
2323 Minimize_Eliminate_Overflows
2324 (Left_Opnd (N), Llo, Lhi, Top_Level => False);
2325 Minimize_Eliminate_Overflows
2326 (Right_Opnd (N), Rlo, Rhi, Top_Level => False);
2328 -- See if the range information decides the result of the comparison.
2329 -- We can only do this if we in fact have full range information (which
2330 -- won't be the case if either operand is bignum at this stage).
2332 if Llo /= No_Uint and then Rlo /= No_Uint then
2333 case N_Op_Compare (Nkind (N)) is
2334 when N_Op_Eq =>
2335 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2336 Set_True;
2337 elsif Llo > Rhi or else Lhi < Rlo then
2338 Set_False;
2339 end if;
2341 when N_Op_Ge =>
2342 if Llo >= Rhi then
2343 Set_True;
2344 elsif Lhi < Rlo then
2345 Set_False;
2346 end if;
2348 when N_Op_Gt =>
2349 if Llo > Rhi then
2350 Set_True;
2351 elsif Lhi <= Rlo then
2352 Set_False;
2353 end if;
2355 when N_Op_Le =>
2356 if Llo > Rhi then
2357 Set_False;
2358 elsif Lhi <= Rlo then
2359 Set_True;
2360 end if;
2362 when N_Op_Lt =>
2363 if Llo >= Rhi then
2364 Set_False;
2365 elsif Lhi < Rlo then
2366 Set_True;
2367 end if;
2369 when N_Op_Ne =>
2370 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2371 Set_False;
2372 elsif Llo > Rhi or else Lhi < Rlo then
2373 Set_True;
2374 end if;
2375 end case;
2377 -- All done if we did the rewrite
2379 if Nkind (N) not in N_Op_Compare then
2380 return;
2381 end if;
2382 end if;
2384 -- Otherwise, time to do the comparison
2386 declare
2387 Ltype : constant Entity_Id := Etype (Left_Opnd (N));
2388 Rtype : constant Entity_Id := Etype (Right_Opnd (N));
2390 begin
2391 -- If the two operands have the same signed integer type we are
2392 -- all set, nothing more to do. This is the case where either
2393 -- both operands were unchanged, or we rewrote both of them to
2394 -- be Long_Long_Integer.
2396 -- Note: Entity for the comparison may be wrong, but it's not worth
2397 -- the effort to change it, since the back end does not use it.
2399 if Is_Signed_Integer_Type (Ltype)
2400 and then Base_Type (Ltype) = Base_Type (Rtype)
2401 then
2402 return;
2404 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2406 elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then
2407 declare
2408 Left : Node_Id := Left_Opnd (N);
2409 Right : Node_Id := Right_Opnd (N);
2410 -- Bignum references for left and right operands
2412 begin
2413 if not Is_RTE (Ltype, RE_Bignum) then
2414 Left := Convert_To_Bignum (Left);
2415 elsif not Is_RTE (Rtype, RE_Bignum) then
2416 Right := Convert_To_Bignum (Right);
2417 end if;
2419 -- We rewrite our node with:
2421 -- do
2422 -- Bnn : Result_Type;
2423 -- declare
2424 -- M : Mark_Id := SS_Mark;
2425 -- begin
2426 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2427 -- SS_Release (M);
2428 -- end;
2429 -- in
2430 -- Bnn
2431 -- end
2433 declare
2434 Blk : constant Node_Id := Make_Bignum_Block (Loc);
2435 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
2436 Ent : RE_Id;
2438 begin
2439 case N_Op_Compare (Nkind (N)) is
2440 when N_Op_Eq => Ent := RE_Big_EQ;
2441 when N_Op_Ge => Ent := RE_Big_GE;
2442 when N_Op_Gt => Ent := RE_Big_GT;
2443 when N_Op_Le => Ent := RE_Big_LE;
2444 when N_Op_Lt => Ent := RE_Big_LT;
2445 when N_Op_Ne => Ent := RE_Big_NE;
2446 end case;
2448 -- Insert assignment to Bnn into the bignum block
2450 Insert_Before
2451 (First (Statements (Handled_Statement_Sequence (Blk))),
2452 Make_Assignment_Statement (Loc,
2453 Name => New_Occurrence_Of (Bnn, Loc),
2454 Expression =>
2455 Make_Function_Call (Loc,
2456 Name =>
2457 New_Occurrence_Of (RTE (Ent), Loc),
2458 Parameter_Associations => New_List (Left, Right))));
2460 -- Now do the rewrite with expression actions
2462 Rewrite (N,
2463 Make_Expression_With_Actions (Loc,
2464 Actions => New_List (
2465 Make_Object_Declaration (Loc,
2466 Defining_Identifier => Bnn,
2467 Object_Definition =>
2468 New_Occurrence_Of (Result_Type, Loc)),
2469 Blk),
2470 Expression => New_Occurrence_Of (Bnn, Loc)));
2471 Analyze_And_Resolve (N, Result_Type);
2472 end;
2473 end;
2475 -- No bignums involved, but types are different, so we must have
2476 -- rewritten one of the operands as a Long_Long_Integer but not
2477 -- the other one.
2479 -- If left operand is Long_Long_Integer, convert right operand
2480 -- and we are done (with a comparison of two Long_Long_Integers).
2482 elsif Ltype = LLIB then
2483 Convert_To_And_Rewrite (LLIB, Right_Opnd (N));
2484 Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks);
2485 return;
2487 -- If right operand is Long_Long_Integer, convert left operand
2488 -- and we are done (with a comparison of two Long_Long_Integers).
2490 -- This is the only remaining possibility
2492 else pragma Assert (Rtype = LLIB);
2493 Convert_To_And_Rewrite (LLIB, Left_Opnd (N));
2494 Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks);
2495 return;
2496 end if;
2497 end;
2498 end Expand_Compare_Minimize_Eliminate_Overflow;
2500 -------------------------------
2501 -- Expand_Composite_Equality --
2502 -------------------------------
2504 -- This function is only called for comparing internal fields of composite
2505 -- types when these fields are themselves composites. This is a special
2506 -- case because it is not possible to respect normal Ada visibility rules.
2508 function Expand_Composite_Equality
2509 (Nod : Node_Id;
2510 Typ : Entity_Id;
2511 Lhs : Node_Id;
2512 Rhs : Node_Id;
2513 Bodies : List_Id) return Node_Id
2515 Loc : constant Source_Ptr := Sloc (Nod);
2516 Full_Type : Entity_Id;
2517 Prim : Elmt_Id;
2518 Eq_Op : Entity_Id;
2520 function Find_Primitive_Eq return Node_Id;
2521 -- AI05-0123: Locate primitive equality for type if it exists, and
2522 -- build the corresponding call. If operation is abstract, replace
2523 -- call with an explicit raise. Return Empty if there is no primitive.
2525 -----------------------
2526 -- Find_Primitive_Eq --
2527 -----------------------
2529 function Find_Primitive_Eq return Node_Id is
2530 Prim_E : Elmt_Id;
2531 Prim : Node_Id;
2533 begin
2534 Prim_E := First_Elmt (Collect_Primitive_Operations (Typ));
2535 while Present (Prim_E) loop
2536 Prim := Node (Prim_E);
2538 -- Locate primitive equality with the right signature
2540 if Chars (Prim) = Name_Op_Eq
2541 and then Etype (First_Formal (Prim)) =
2542 Etype (Next_Formal (First_Formal (Prim)))
2543 and then Etype (Prim) = Standard_Boolean
2544 then
2545 if Is_Abstract_Subprogram (Prim) then
2546 return
2547 Make_Raise_Program_Error (Loc,
2548 Reason => PE_Explicit_Raise);
2550 else
2551 return
2552 Make_Function_Call (Loc,
2553 Name => New_Reference_To (Prim, Loc),
2554 Parameter_Associations => New_List (Lhs, Rhs));
2555 end if;
2556 end if;
2558 Next_Elmt (Prim_E);
2559 end loop;
2561 -- If not found, predefined operation will be used
2563 return Empty;
2564 end Find_Primitive_Eq;
2566 -- Start of processing for Expand_Composite_Equality
2568 begin
2569 if Is_Private_Type (Typ) then
2570 Full_Type := Underlying_Type (Typ);
2571 else
2572 Full_Type := Typ;
2573 end if;
2575 -- Defense against malformed private types with no completion the error
2576 -- will be diagnosed later by check_completion
2578 if No (Full_Type) then
2579 return New_Reference_To (Standard_False, Loc);
2580 end if;
2582 Full_Type := Base_Type (Full_Type);
2584 if Is_Array_Type (Full_Type) then
2586 -- If the operand is an elementary type other than a floating-point
2587 -- type, then we can simply use the built-in block bitwise equality,
2588 -- since the predefined equality operators always apply and bitwise
2589 -- equality is fine for all these cases.
2591 if Is_Elementary_Type (Component_Type (Full_Type))
2592 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2593 then
2594 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2596 -- For composite component types, and floating-point types, use the
2597 -- expansion. This deals with tagged component types (where we use
2598 -- the applicable equality routine) and floating-point, (where we
2599 -- need to worry about negative zeroes), and also the case of any
2600 -- composite type recursively containing such fields.
2602 else
2603 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2604 end if;
2606 elsif Is_Tagged_Type (Full_Type) then
2608 -- Call the primitive operation "=" of this type
2610 if Is_Class_Wide_Type (Full_Type) then
2611 Full_Type := Root_Type (Full_Type);
2612 end if;
2614 -- If this is derived from an untagged private type completed with a
2615 -- tagged type, it does not have a full view, so we use the primitive
2616 -- operations of the private type. This check should no longer be
2617 -- necessary when these types receive their full views ???
2619 if Is_Private_Type (Typ)
2620 and then not Is_Tagged_Type (Typ)
2621 and then not Is_Controlled (Typ)
2622 and then Is_Derived_Type (Typ)
2623 and then No (Full_View (Typ))
2624 then
2625 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2626 else
2627 Prim := First_Elmt (Primitive_Operations (Full_Type));
2628 end if;
2630 loop
2631 Eq_Op := Node (Prim);
2632 exit when Chars (Eq_Op) = Name_Op_Eq
2633 and then Etype (First_Formal (Eq_Op)) =
2634 Etype (Next_Formal (First_Formal (Eq_Op)))
2635 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2636 Next_Elmt (Prim);
2637 pragma Assert (Present (Prim));
2638 end loop;
2640 Eq_Op := Node (Prim);
2642 return
2643 Make_Function_Call (Loc,
2644 Name => New_Reference_To (Eq_Op, Loc),
2645 Parameter_Associations =>
2646 New_List
2647 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2648 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2650 elsif Is_Record_Type (Full_Type) then
2651 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2653 if Present (Eq_Op) then
2654 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2656 -- Inherited equality from parent type. Convert the actuals to
2657 -- match signature of operation.
2659 declare
2660 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2662 begin
2663 return
2664 Make_Function_Call (Loc,
2665 Name => New_Reference_To (Eq_Op, Loc),
2666 Parameter_Associations => New_List (
2667 OK_Convert_To (T, Lhs),
2668 OK_Convert_To (T, Rhs)));
2669 end;
2671 else
2672 -- Comparison between Unchecked_Union components
2674 if Is_Unchecked_Union (Full_Type) then
2675 declare
2676 Lhs_Type : Node_Id := Full_Type;
2677 Rhs_Type : Node_Id := Full_Type;
2678 Lhs_Discr_Val : Node_Id;
2679 Rhs_Discr_Val : Node_Id;
2681 begin
2682 -- Lhs subtype
2684 if Nkind (Lhs) = N_Selected_Component then
2685 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2686 end if;
2688 -- Rhs subtype
2690 if Nkind (Rhs) = N_Selected_Component then
2691 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2692 end if;
2694 -- Lhs of the composite equality
2696 if Is_Constrained (Lhs_Type) then
2698 -- Since the enclosing record type can never be an
2699 -- Unchecked_Union (this code is executed for records
2700 -- that do not have variants), we may reference its
2701 -- discriminant(s).
2703 if Nkind (Lhs) = N_Selected_Component
2704 and then Has_Per_Object_Constraint (
2705 Entity (Selector_Name (Lhs)))
2706 then
2707 Lhs_Discr_Val :=
2708 Make_Selected_Component (Loc,
2709 Prefix => Prefix (Lhs),
2710 Selector_Name =>
2711 New_Copy
2712 (Get_Discriminant_Value
2713 (First_Discriminant (Lhs_Type),
2714 Lhs_Type,
2715 Stored_Constraint (Lhs_Type))));
2717 else
2718 Lhs_Discr_Val :=
2719 New_Copy
2720 (Get_Discriminant_Value
2721 (First_Discriminant (Lhs_Type),
2722 Lhs_Type,
2723 Stored_Constraint (Lhs_Type)));
2725 end if;
2726 else
2727 -- It is not possible to infer the discriminant since
2728 -- the subtype is not constrained.
2730 return
2731 Make_Raise_Program_Error (Loc,
2732 Reason => PE_Unchecked_Union_Restriction);
2733 end if;
2735 -- Rhs of the composite equality
2737 if Is_Constrained (Rhs_Type) then
2738 if Nkind (Rhs) = N_Selected_Component
2739 and then Has_Per_Object_Constraint
2740 (Entity (Selector_Name (Rhs)))
2741 then
2742 Rhs_Discr_Val :=
2743 Make_Selected_Component (Loc,
2744 Prefix => Prefix (Rhs),
2745 Selector_Name =>
2746 New_Copy
2747 (Get_Discriminant_Value
2748 (First_Discriminant (Rhs_Type),
2749 Rhs_Type,
2750 Stored_Constraint (Rhs_Type))));
2752 else
2753 Rhs_Discr_Val :=
2754 New_Copy
2755 (Get_Discriminant_Value
2756 (First_Discriminant (Rhs_Type),
2757 Rhs_Type,
2758 Stored_Constraint (Rhs_Type)));
2760 end if;
2761 else
2762 return
2763 Make_Raise_Program_Error (Loc,
2764 Reason => PE_Unchecked_Union_Restriction);
2765 end if;
2767 -- Call the TSS equality function with the inferred
2768 -- discriminant values.
2770 return
2771 Make_Function_Call (Loc,
2772 Name => New_Reference_To (Eq_Op, Loc),
2773 Parameter_Associations => New_List (
2774 Lhs,
2775 Rhs,
2776 Lhs_Discr_Val,
2777 Rhs_Discr_Val));
2778 end;
2780 else
2781 return
2782 Make_Function_Call (Loc,
2783 Name => New_Reference_To (Eq_Op, Loc),
2784 Parameter_Associations => New_List (Lhs, Rhs));
2785 end if;
2786 end if;
2788 -- Equality composes in Ada 2012 for untagged record types. It also
2789 -- composes for bounded strings, because they are part of the
2790 -- predefined environment. We could make it compose for bounded
2791 -- strings by making them tagged, or by making sure all subcomponents
2792 -- are set to the same value, even when not used. Instead, we have
2793 -- this special case in the compiler, because it's more efficient.
2795 elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then
2797 -- if no TSS has been created for the type, check whether there is
2798 -- a primitive equality declared for it.
2800 declare
2801 Op : constant Node_Id := Find_Primitive_Eq;
2803 begin
2804 -- Use user-defined primitive if it exists, otherwise use
2805 -- predefined equality.
2807 if Present (Op) then
2808 return Op;
2809 else
2810 return Make_Op_Eq (Loc, Lhs, Rhs);
2811 end if;
2812 end;
2814 else
2815 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2816 end if;
2818 else
2819 -- If not array or record type, it is predefined equality.
2821 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2822 end if;
2823 end Expand_Composite_Equality;
2825 ------------------------
2826 -- Expand_Concatenate --
2827 ------------------------
2829 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2830 Loc : constant Source_Ptr := Sloc (Cnode);
2832 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2833 -- Result type of concatenation
2835 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2836 -- Component type. Elements of this component type can appear as one
2837 -- of the operands of concatenation as well as arrays.
2839 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2840 -- Index subtype
2842 Ityp : constant Entity_Id := Base_Type (Istyp);
2843 -- Index type. This is the base type of the index subtype, and is used
2844 -- for all computed bounds (which may be out of range of Istyp in the
2845 -- case of null ranges).
2847 Artyp : Entity_Id;
2848 -- This is the type we use to do arithmetic to compute the bounds and
2849 -- lengths of operands. The choice of this type is a little subtle and
2850 -- is discussed in a separate section at the start of the body code.
2852 Concatenation_Error : exception;
2853 -- Raised if concatenation is sure to raise a CE
2855 Result_May_Be_Null : Boolean := True;
2856 -- Reset to False if at least one operand is encountered which is known
2857 -- at compile time to be non-null. Used for handling the special case
2858 -- of setting the high bound to the last operand high bound for a null
2859 -- result, thus ensuring a proper high bound in the super-flat case.
2861 N : constant Nat := List_Length (Opnds);
2862 -- Number of concatenation operands including possibly null operands
2864 NN : Nat := 0;
2865 -- Number of operands excluding any known to be null, except that the
2866 -- last operand is always retained, in case it provides the bounds for
2867 -- a null result.
2869 Opnd : Node_Id;
2870 -- Current operand being processed in the loop through operands. After
2871 -- this loop is complete, always contains the last operand (which is not
2872 -- the same as Operands (NN), since null operands are skipped).
2874 -- Arrays describing the operands, only the first NN entries of each
2875 -- array are set (NN < N when we exclude known null operands).
2877 Is_Fixed_Length : array (1 .. N) of Boolean;
2878 -- True if length of corresponding operand known at compile time
2880 Operands : array (1 .. N) of Node_Id;
2881 -- Set to the corresponding entry in the Opnds list (but note that null
2882 -- operands are excluded, so not all entries in the list are stored).
2884 Fixed_Length : array (1 .. N) of Uint;
2885 -- Set to length of operand. Entries in this array are set only if the
2886 -- corresponding entry in Is_Fixed_Length is True.
2888 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2889 -- Set to lower bound of operand. Either an integer literal in the case
2890 -- where the bound is known at compile time, else actual lower bound.
2891 -- The operand low bound is of type Ityp.
2893 Var_Length : array (1 .. N) of Entity_Id;
2894 -- Set to an entity of type Natural that contains the length of an
2895 -- operand whose length is not known at compile time. Entries in this
2896 -- array are set only if the corresponding entry in Is_Fixed_Length
2897 -- is False. The entity is of type Artyp.
2899 Aggr_Length : array (0 .. N) of Node_Id;
2900 -- The J'th entry in an expression node that represents the total length
2901 -- of operands 1 through J. It is either an integer literal node, or a
2902 -- reference to a constant entity with the right value, so it is fine
2903 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2904 -- entry always is set to zero. The length is of type Artyp.
2906 Low_Bound : Node_Id;
2907 -- A tree node representing the low bound of the result (of type Ityp).
2908 -- This is either an integer literal node, or an identifier reference to
2909 -- a constant entity initialized to the appropriate value.
2911 Last_Opnd_Low_Bound : Node_Id;
2912 -- A tree node representing the low bound of the last operand. This
2913 -- need only be set if the result could be null. It is used for the
2914 -- special case of setting the right low bound for a null result.
2915 -- This is of type Ityp.
2917 Last_Opnd_High_Bound : Node_Id;
2918 -- A tree node representing the high bound of the last operand. This
2919 -- need only be set if the result could be null. It is used for the
2920 -- special case of setting the right high bound for a null result.
2921 -- This is of type Ityp.
2923 High_Bound : Node_Id;
2924 -- A tree node representing the high bound of the result (of type Ityp)
2926 Result : Node_Id;
2927 -- Result of the concatenation (of type Ityp)
2929 Actions : constant List_Id := New_List;
2930 -- Collect actions to be inserted
2932 Known_Non_Null_Operand_Seen : Boolean;
2933 -- Set True during generation of the assignments of operands into
2934 -- result once an operand known to be non-null has been seen.
2936 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2937 -- This function makes an N_Integer_Literal node that is returned in
2938 -- analyzed form with the type set to Artyp. Importantly this literal
2939 -- is not flagged as static, so that if we do computations with it that
2940 -- result in statically detected out of range conditions, we will not
2941 -- generate error messages but instead warning messages.
2943 function To_Artyp (X : Node_Id) return Node_Id;
2944 -- Given a node of type Ityp, returns the corresponding value of type
2945 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2946 -- For enum types, the Pos of the value is returned.
2948 function To_Ityp (X : Node_Id) return Node_Id;
2949 -- The inverse function (uses Val in the case of enumeration types)
2951 ------------------------
2952 -- Make_Artyp_Literal --
2953 ------------------------
2955 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2956 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2957 begin
2958 Set_Etype (Result, Artyp);
2959 Set_Analyzed (Result, True);
2960 Set_Is_Static_Expression (Result, False);
2961 return Result;
2962 end Make_Artyp_Literal;
2964 --------------
2965 -- To_Artyp --
2966 --------------
2968 function To_Artyp (X : Node_Id) return Node_Id is
2969 begin
2970 if Ityp = Base_Type (Artyp) then
2971 return X;
2973 elsif Is_Enumeration_Type (Ityp) then
2974 return
2975 Make_Attribute_Reference (Loc,
2976 Prefix => New_Occurrence_Of (Ityp, Loc),
2977 Attribute_Name => Name_Pos,
2978 Expressions => New_List (X));
2980 else
2981 return Convert_To (Artyp, X);
2982 end if;
2983 end To_Artyp;
2985 -------------
2986 -- To_Ityp --
2987 -------------
2989 function To_Ityp (X : Node_Id) return Node_Id is
2990 begin
2991 if Is_Enumeration_Type (Ityp) then
2992 return
2993 Make_Attribute_Reference (Loc,
2994 Prefix => New_Occurrence_Of (Ityp, Loc),
2995 Attribute_Name => Name_Val,
2996 Expressions => New_List (X));
2998 -- Case where we will do a type conversion
3000 else
3001 if Ityp = Base_Type (Artyp) then
3002 return X;
3003 else
3004 return Convert_To (Ityp, X);
3005 end if;
3006 end if;
3007 end To_Ityp;
3009 -- Local Declarations
3011 Opnd_Typ : Entity_Id;
3012 Ent : Entity_Id;
3013 Len : Uint;
3014 J : Nat;
3015 Clen : Node_Id;
3016 Set : Boolean;
3018 -- Start of processing for Expand_Concatenate
3020 begin
3021 -- Choose an appropriate computational type
3023 -- We will be doing calculations of lengths and bounds in this routine
3024 -- and computing one from the other in some cases, e.g. getting the high
3025 -- bound by adding the length-1 to the low bound.
3027 -- We can't just use the index type, or even its base type for this
3028 -- purpose for two reasons. First it might be an enumeration type which
3029 -- is not suitable for computations of any kind, and second it may
3030 -- simply not have enough range. For example if the index type is
3031 -- -128..+127 then lengths can be up to 256, which is out of range of
3032 -- the type.
3034 -- For enumeration types, we can simply use Standard_Integer, this is
3035 -- sufficient since the actual number of enumeration literals cannot
3036 -- possibly exceed the range of integer (remember we will be doing the
3037 -- arithmetic with POS values, not representation values).
3039 if Is_Enumeration_Type (Ityp) then
3040 Artyp := Standard_Integer;
3042 -- If index type is Positive, we use the standard unsigned type, to give
3043 -- more room on the top of the range, obviating the need for an overflow
3044 -- check when creating the upper bound. This is needed to avoid junk
3045 -- overflow checks in the common case of String types.
3047 -- ??? Disabled for now
3049 -- elsif Istyp = Standard_Positive then
3050 -- Artyp := Standard_Unsigned;
3052 -- For modular types, we use a 32-bit modular type for types whose size
3053 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
3054 -- identity type, and for larger unsigned types we use 64-bits.
3056 elsif Is_Modular_Integer_Type (Ityp) then
3057 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
3058 Artyp := Standard_Unsigned;
3059 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
3060 Artyp := Ityp;
3061 else
3062 Artyp := RTE (RE_Long_Long_Unsigned);
3063 end if;
3065 -- Similar treatment for signed types
3067 else
3068 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
3069 Artyp := Standard_Integer;
3070 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
3071 Artyp := Ityp;
3072 else
3073 Artyp := Standard_Long_Long_Integer;
3074 end if;
3075 end if;
3077 -- Supply dummy entry at start of length array
3079 Aggr_Length (0) := Make_Artyp_Literal (0);
3081 -- Go through operands setting up the above arrays
3083 J := 1;
3084 while J <= N loop
3085 Opnd := Remove_Head (Opnds);
3086 Opnd_Typ := Etype (Opnd);
3088 -- The parent got messed up when we put the operands in a list,
3089 -- so now put back the proper parent for the saved operand, that
3090 -- is to say the concatenation node, to make sure that each operand
3091 -- is seen as a subexpression, e.g. if actions must be inserted.
3093 Set_Parent (Opnd, Cnode);
3095 -- Set will be True when we have setup one entry in the array
3097 Set := False;
3099 -- Singleton element (or character literal) case
3101 if Base_Type (Opnd_Typ) = Ctyp then
3102 NN := NN + 1;
3103 Operands (NN) := Opnd;
3104 Is_Fixed_Length (NN) := True;
3105 Fixed_Length (NN) := Uint_1;
3106 Result_May_Be_Null := False;
3108 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
3109 -- since we know that the result cannot be null).
3111 Opnd_Low_Bound (NN) :=
3112 Make_Attribute_Reference (Loc,
3113 Prefix => New_Reference_To (Istyp, Loc),
3114 Attribute_Name => Name_First);
3116 Set := True;
3118 -- String literal case (can only occur for strings of course)
3120 elsif Nkind (Opnd) = N_String_Literal then
3121 Len := String_Literal_Length (Opnd_Typ);
3123 if Len /= 0 then
3124 Result_May_Be_Null := False;
3125 end if;
3127 -- Capture last operand low and high bound if result could be null
3129 if J = N and then Result_May_Be_Null then
3130 Last_Opnd_Low_Bound :=
3131 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3133 Last_Opnd_High_Bound :=
3134 Make_Op_Subtract (Loc,
3135 Left_Opnd =>
3136 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
3137 Right_Opnd => Make_Integer_Literal (Loc, 1));
3138 end if;
3140 -- Skip null string literal
3142 if J < N and then Len = 0 then
3143 goto Continue;
3144 end if;
3146 NN := NN + 1;
3147 Operands (NN) := Opnd;
3148 Is_Fixed_Length (NN) := True;
3150 -- Set length and bounds
3152 Fixed_Length (NN) := Len;
3154 Opnd_Low_Bound (NN) :=
3155 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3157 Set := True;
3159 -- All other cases
3161 else
3162 -- Check constrained case with known bounds
3164 if Is_Constrained (Opnd_Typ) then
3165 declare
3166 Index : constant Node_Id := First_Index (Opnd_Typ);
3167 Indx_Typ : constant Entity_Id := Etype (Index);
3168 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
3169 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
3171 begin
3172 -- Fixed length constrained array type with known at compile
3173 -- time bounds is last case of fixed length operand.
3175 if Compile_Time_Known_Value (Lo)
3176 and then
3177 Compile_Time_Known_Value (Hi)
3178 then
3179 declare
3180 Loval : constant Uint := Expr_Value (Lo);
3181 Hival : constant Uint := Expr_Value (Hi);
3182 Len : constant Uint :=
3183 UI_Max (Hival - Loval + 1, Uint_0);
3185 begin
3186 if Len > 0 then
3187 Result_May_Be_Null := False;
3188 end if;
3190 -- Capture last operand bounds if result could be null
3192 if J = N and then Result_May_Be_Null then
3193 Last_Opnd_Low_Bound :=
3194 Convert_To (Ityp,
3195 Make_Integer_Literal (Loc, Expr_Value (Lo)));
3197 Last_Opnd_High_Bound :=
3198 Convert_To (Ityp,
3199 Make_Integer_Literal (Loc, Expr_Value (Hi)));
3200 end if;
3202 -- Exclude null length case unless last operand
3204 if J < N and then Len = 0 then
3205 goto Continue;
3206 end if;
3208 NN := NN + 1;
3209 Operands (NN) := Opnd;
3210 Is_Fixed_Length (NN) := True;
3211 Fixed_Length (NN) := Len;
3213 Opnd_Low_Bound (NN) :=
3214 To_Ityp
3215 (Make_Integer_Literal (Loc, Expr_Value (Lo)));
3216 Set := True;
3217 end;
3218 end if;
3219 end;
3220 end if;
3222 -- All cases where the length is not known at compile time, or the
3223 -- special case of an operand which is known to be null but has a
3224 -- lower bound other than 1 or is other than a string type.
3226 if not Set then
3227 NN := NN + 1;
3229 -- Capture operand bounds
3231 Opnd_Low_Bound (NN) :=
3232 Make_Attribute_Reference (Loc,
3233 Prefix =>
3234 Duplicate_Subexpr (Opnd, Name_Req => True),
3235 Attribute_Name => Name_First);
3237 -- Capture last operand bounds if result could be null
3239 if J = N and Result_May_Be_Null then
3240 Last_Opnd_Low_Bound :=
3241 Convert_To (Ityp,
3242 Make_Attribute_Reference (Loc,
3243 Prefix =>
3244 Duplicate_Subexpr (Opnd, Name_Req => True),
3245 Attribute_Name => Name_First));
3247 Last_Opnd_High_Bound :=
3248 Convert_To (Ityp,
3249 Make_Attribute_Reference (Loc,
3250 Prefix =>
3251 Duplicate_Subexpr (Opnd, Name_Req => True),
3252 Attribute_Name => Name_Last));
3253 end if;
3255 -- Capture length of operand in entity
3257 Operands (NN) := Opnd;
3258 Is_Fixed_Length (NN) := False;
3260 Var_Length (NN) := Make_Temporary (Loc, 'L');
3262 Append_To (Actions,
3263 Make_Object_Declaration (Loc,
3264 Defining_Identifier => Var_Length (NN),
3265 Constant_Present => True,
3266 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3267 Expression =>
3268 Make_Attribute_Reference (Loc,
3269 Prefix =>
3270 Duplicate_Subexpr (Opnd, Name_Req => True),
3271 Attribute_Name => Name_Length)));
3272 end if;
3273 end if;
3275 -- Set next entry in aggregate length array
3277 -- For first entry, make either integer literal for fixed length
3278 -- or a reference to the saved length for variable length.
3280 if NN = 1 then
3281 if Is_Fixed_Length (1) then
3282 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
3283 else
3284 Aggr_Length (1) := New_Reference_To (Var_Length (1), Loc);
3285 end if;
3287 -- If entry is fixed length and only fixed lengths so far, make
3288 -- appropriate new integer literal adding new length.
3290 elsif Is_Fixed_Length (NN)
3291 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
3292 then
3293 Aggr_Length (NN) :=
3294 Make_Integer_Literal (Loc,
3295 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
3297 -- All other cases, construct an addition node for the length and
3298 -- create an entity initialized to this length.
3300 else
3301 Ent := Make_Temporary (Loc, 'L');
3303 if Is_Fixed_Length (NN) then
3304 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
3305 else
3306 Clen := New_Reference_To (Var_Length (NN), Loc);
3307 end if;
3309 Append_To (Actions,
3310 Make_Object_Declaration (Loc,
3311 Defining_Identifier => Ent,
3312 Constant_Present => True,
3313 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3314 Expression =>
3315 Make_Op_Add (Loc,
3316 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
3317 Right_Opnd => Clen)));
3319 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
3320 end if;
3322 <<Continue>>
3323 J := J + 1;
3324 end loop;
3326 -- If we have only skipped null operands, return the last operand
3328 if NN = 0 then
3329 Result := Opnd;
3330 goto Done;
3331 end if;
3333 -- If we have only one non-null operand, return it and we are done.
3334 -- There is one case in which this cannot be done, and that is when
3335 -- the sole operand is of the element type, in which case it must be
3336 -- converted to an array, and the easiest way of doing that is to go
3337 -- through the normal general circuit.
3339 if NN = 1
3340 and then Base_Type (Etype (Operands (1))) /= Ctyp
3341 then
3342 Result := Operands (1);
3343 goto Done;
3344 end if;
3346 -- Cases where we have a real concatenation
3348 -- Next step is to find the low bound for the result array that we
3349 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3351 -- If the ultimate ancestor of the index subtype is a constrained array
3352 -- definition, then the lower bound is that of the index subtype as
3353 -- specified by (RM 4.5.3(6)).
3355 -- The right test here is to go to the root type, and then the ultimate
3356 -- ancestor is the first subtype of this root type.
3358 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
3359 Low_Bound :=
3360 Make_Attribute_Reference (Loc,
3361 Prefix =>
3362 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
3363 Attribute_Name => Name_First);
3365 -- If the first operand in the list has known length we know that
3366 -- the lower bound of the result is the lower bound of this operand.
3368 elsif Is_Fixed_Length (1) then
3369 Low_Bound := Opnd_Low_Bound (1);
3371 -- OK, we don't know the lower bound, we have to build a horrible
3372 -- if expression node of the form
3374 -- if Cond1'Length /= 0 then
3375 -- Opnd1 low bound
3376 -- else
3377 -- if Opnd2'Length /= 0 then
3378 -- Opnd2 low bound
3379 -- else
3380 -- ...
3382 -- The nesting ends either when we hit an operand whose length is known
3383 -- at compile time, or on reaching the last operand, whose low bound we
3384 -- take unconditionally whether or not it is null. It's easiest to do
3385 -- this with a recursive procedure:
3387 else
3388 declare
3389 function Get_Known_Bound (J : Nat) return Node_Id;
3390 -- Returns the lower bound determined by operands J .. NN
3392 ---------------------
3393 -- Get_Known_Bound --
3394 ---------------------
3396 function Get_Known_Bound (J : Nat) return Node_Id is
3397 begin
3398 if Is_Fixed_Length (J) or else J = NN then
3399 return New_Copy (Opnd_Low_Bound (J));
3401 else
3402 return
3403 Make_If_Expression (Loc,
3404 Expressions => New_List (
3406 Make_Op_Ne (Loc,
3407 Left_Opnd => New_Reference_To (Var_Length (J), Loc),
3408 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3410 New_Copy (Opnd_Low_Bound (J)),
3411 Get_Known_Bound (J + 1)));
3412 end if;
3413 end Get_Known_Bound;
3415 begin
3416 Ent := Make_Temporary (Loc, 'L');
3418 Append_To (Actions,
3419 Make_Object_Declaration (Loc,
3420 Defining_Identifier => Ent,
3421 Constant_Present => True,
3422 Object_Definition => New_Occurrence_Of (Ityp, Loc),
3423 Expression => Get_Known_Bound (1)));
3425 Low_Bound := New_Reference_To (Ent, Loc);
3426 end;
3427 end if;
3429 -- Now we can safely compute the upper bound, normally
3430 -- Low_Bound + Length - 1.
3432 High_Bound :=
3433 To_Ityp (
3434 Make_Op_Add (Loc,
3435 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3436 Right_Opnd =>
3437 Make_Op_Subtract (Loc,
3438 Left_Opnd => New_Copy (Aggr_Length (NN)),
3439 Right_Opnd => Make_Artyp_Literal (1))));
3441 -- Note that calculation of the high bound may cause overflow in some
3442 -- very weird cases, so in the general case we need an overflow check on
3443 -- the high bound. We can avoid this for the common case of string types
3444 -- and other types whose index is Positive, since we chose a wider range
3445 -- for the arithmetic type.
3447 if Istyp /= Standard_Positive then
3448 Activate_Overflow_Check (High_Bound);
3449 end if;
3451 -- Handle the exceptional case where the result is null, in which case
3452 -- case the bounds come from the last operand (so that we get the proper
3453 -- bounds if the last operand is super-flat).
3455 if Result_May_Be_Null then
3456 Low_Bound :=
3457 Make_If_Expression (Loc,
3458 Expressions => New_List (
3459 Make_Op_Eq (Loc,
3460 Left_Opnd => New_Copy (Aggr_Length (NN)),
3461 Right_Opnd => Make_Artyp_Literal (0)),
3462 Last_Opnd_Low_Bound,
3463 Low_Bound));
3465 High_Bound :=
3466 Make_If_Expression (Loc,
3467 Expressions => New_List (
3468 Make_Op_Eq (Loc,
3469 Left_Opnd => New_Copy (Aggr_Length (NN)),
3470 Right_Opnd => Make_Artyp_Literal (0)),
3471 Last_Opnd_High_Bound,
3472 High_Bound));
3473 end if;
3475 -- Here is where we insert the saved up actions
3477 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3479 -- Now we construct an array object with appropriate bounds. We mark
3480 -- the target as internal to prevent useless initialization when
3481 -- Initialize_Scalars is enabled. Also since this is the actual result
3482 -- entity, we make sure we have debug information for the result.
3484 Ent := Make_Temporary (Loc, 'S');
3485 Set_Is_Internal (Ent);
3486 Set_Needs_Debug_Info (Ent);
3488 -- If the bound is statically known to be out of range, we do not want
3489 -- to abort, we want a warning and a runtime constraint error. Note that
3490 -- we have arranged that the result will not be treated as a static
3491 -- constant, so we won't get an illegality during this insertion.
3493 Insert_Action (Cnode,
3494 Make_Object_Declaration (Loc,
3495 Defining_Identifier => Ent,
3496 Object_Definition =>
3497 Make_Subtype_Indication (Loc,
3498 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
3499 Constraint =>
3500 Make_Index_Or_Discriminant_Constraint (Loc,
3501 Constraints => New_List (
3502 Make_Range (Loc,
3503 Low_Bound => Low_Bound,
3504 High_Bound => High_Bound))))),
3505 Suppress => All_Checks);
3507 -- If the result of the concatenation appears as the initializing
3508 -- expression of an object declaration, we can just rename the
3509 -- result, rather than copying it.
3511 Set_OK_To_Rename (Ent);
3513 -- Catch the static out of range case now
3515 if Raises_Constraint_Error (High_Bound) then
3516 raise Concatenation_Error;
3517 end if;
3519 -- Now we will generate the assignments to do the actual concatenation
3521 -- There is one case in which we will not do this, namely when all the
3522 -- following conditions are met:
3524 -- The result type is Standard.String
3526 -- There are nine or fewer retained (non-null) operands
3528 -- The optimization level is -O0
3530 -- The corresponding System.Concat_n.Str_Concat_n routine is
3531 -- available in the run time.
3533 -- The debug flag gnatd.c is not set
3535 -- If all these conditions are met then we generate a call to the
3536 -- relevant concatenation routine. The purpose of this is to avoid
3537 -- undesirable code bloat at -O0.
3539 if Atyp = Standard_String
3540 and then NN in 2 .. 9
3541 and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
3542 and then not Debug_Flag_Dot_C
3543 then
3544 declare
3545 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3546 (RE_Str_Concat_2,
3547 RE_Str_Concat_3,
3548 RE_Str_Concat_4,
3549 RE_Str_Concat_5,
3550 RE_Str_Concat_6,
3551 RE_Str_Concat_7,
3552 RE_Str_Concat_8,
3553 RE_Str_Concat_9);
3555 begin
3556 if RTE_Available (RR (NN)) then
3557 declare
3558 Opnds : constant List_Id :=
3559 New_List (New_Occurrence_Of (Ent, Loc));
3561 begin
3562 for J in 1 .. NN loop
3563 if Is_List_Member (Operands (J)) then
3564 Remove (Operands (J));
3565 end if;
3567 if Base_Type (Etype (Operands (J))) = Ctyp then
3568 Append_To (Opnds,
3569 Make_Aggregate (Loc,
3570 Component_Associations => New_List (
3571 Make_Component_Association (Loc,
3572 Choices => New_List (
3573 Make_Integer_Literal (Loc, 1)),
3574 Expression => Operands (J)))));
3576 else
3577 Append_To (Opnds, Operands (J));
3578 end if;
3579 end loop;
3581 Insert_Action (Cnode,
3582 Make_Procedure_Call_Statement (Loc,
3583 Name => New_Reference_To (RTE (RR (NN)), Loc),
3584 Parameter_Associations => Opnds));
3586 Result := New_Reference_To (Ent, Loc);
3587 goto Done;
3588 end;
3589 end if;
3590 end;
3591 end if;
3593 -- Not special case so generate the assignments
3595 Known_Non_Null_Operand_Seen := False;
3597 for J in 1 .. NN loop
3598 declare
3599 Lo : constant Node_Id :=
3600 Make_Op_Add (Loc,
3601 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3602 Right_Opnd => Aggr_Length (J - 1));
3604 Hi : constant Node_Id :=
3605 Make_Op_Add (Loc,
3606 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3607 Right_Opnd =>
3608 Make_Op_Subtract (Loc,
3609 Left_Opnd => Aggr_Length (J),
3610 Right_Opnd => Make_Artyp_Literal (1)));
3612 begin
3613 -- Singleton case, simple assignment
3615 if Base_Type (Etype (Operands (J))) = Ctyp then
3616 Known_Non_Null_Operand_Seen := True;
3617 Insert_Action (Cnode,
3618 Make_Assignment_Statement (Loc,
3619 Name =>
3620 Make_Indexed_Component (Loc,
3621 Prefix => New_Occurrence_Of (Ent, Loc),
3622 Expressions => New_List (To_Ityp (Lo))),
3623 Expression => Operands (J)),
3624 Suppress => All_Checks);
3626 -- Array case, slice assignment, skipped when argument is fixed
3627 -- length and known to be null.
3629 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3630 declare
3631 Assign : Node_Id :=
3632 Make_Assignment_Statement (Loc,
3633 Name =>
3634 Make_Slice (Loc,
3635 Prefix =>
3636 New_Occurrence_Of (Ent, Loc),
3637 Discrete_Range =>
3638 Make_Range (Loc,
3639 Low_Bound => To_Ityp (Lo),
3640 High_Bound => To_Ityp (Hi))),
3641 Expression => Operands (J));
3642 begin
3643 if Is_Fixed_Length (J) then
3644 Known_Non_Null_Operand_Seen := True;
3646 elsif not Known_Non_Null_Operand_Seen then
3648 -- Here if operand length is not statically known and no
3649 -- operand known to be non-null has been processed yet.
3650 -- If operand length is 0, we do not need to perform the
3651 -- assignment, and we must avoid the evaluation of the
3652 -- high bound of the slice, since it may underflow if the
3653 -- low bound is Ityp'First.
3655 Assign :=
3656 Make_Implicit_If_Statement (Cnode,
3657 Condition =>
3658 Make_Op_Ne (Loc,
3659 Left_Opnd =>
3660 New_Occurrence_Of (Var_Length (J), Loc),
3661 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3662 Then_Statements => New_List (Assign));
3663 end if;
3665 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3666 end;
3667 end if;
3668 end;
3669 end loop;
3671 -- Finally we build the result, which is a reference to the array object
3673 Result := New_Reference_To (Ent, Loc);
3675 <<Done>>
3676 Rewrite (Cnode, Result);
3677 Analyze_And_Resolve (Cnode, Atyp);
3679 exception
3680 when Concatenation_Error =>
3682 -- Kill warning generated for the declaration of the static out of
3683 -- range high bound, and instead generate a Constraint_Error with
3684 -- an appropriate specific message.
3686 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3687 Apply_Compile_Time_Constraint_Error
3688 (N => Cnode,
3689 Msg => "concatenation result upper bound out of range?",
3690 Reason => CE_Range_Check_Failed);
3691 end Expand_Concatenate;
3693 ---------------------------------------------------
3694 -- Expand_Membership_Minimize_Eliminate_Overflow --
3695 ---------------------------------------------------
3697 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is
3698 pragma Assert (Nkind (N) = N_In);
3699 -- Despite the name, this routine applies only to N_In, not to
3700 -- N_Not_In. The latter is always rewritten as not (X in Y).
3702 Result_Type : constant Entity_Id := Etype (N);
3703 -- Capture result type, may be a derived boolean type
3705 Loc : constant Source_Ptr := Sloc (N);
3706 Lop : constant Node_Id := Left_Opnd (N);
3707 Rop : constant Node_Id := Right_Opnd (N);
3709 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3710 -- is thus tempting to capture these values, but due to the rewrites
3711 -- that occur as a result of overflow checking, these values change
3712 -- as we go along, and it is safe just to always use Etype explicitly.
3714 Restype : constant Entity_Id := Etype (N);
3715 -- Save result type
3717 Lo, Hi : Uint;
3718 -- Bounds in Minimize calls, not used currently
3720 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
3721 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3723 begin
3724 Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False);
3726 -- If right operand is a subtype name, and the subtype name has no
3727 -- predicate, then we can just replace the right operand with an
3728 -- explicit range T'First .. T'Last, and use the explicit range code.
3730 if Nkind (Rop) /= N_Range
3731 and then No (Predicate_Function (Etype (Rop)))
3732 then
3733 declare
3734 Rtyp : constant Entity_Id := Etype (Rop);
3735 begin
3736 Rewrite (Rop,
3737 Make_Range (Loc,
3738 Low_Bound =>
3739 Make_Attribute_Reference (Loc,
3740 Attribute_Name => Name_First,
3741 Prefix => New_Reference_To (Rtyp, Loc)),
3742 High_Bound =>
3743 Make_Attribute_Reference (Loc,
3744 Attribute_Name => Name_Last,
3745 Prefix => New_Reference_To (Rtyp, Loc))));
3746 Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks);
3747 end;
3748 end if;
3750 -- Here for the explicit range case. Note that the bounds of the range
3751 -- have not been processed for minimized or eliminated checks.
3753 if Nkind (Rop) = N_Range then
3754 Minimize_Eliminate_Overflows
3755 (Low_Bound (Rop), Lo, Hi, Top_Level => False);
3756 Minimize_Eliminate_Overflows
3757 (High_Bound (Rop), Lo, Hi, Top_Level => False);
3759 -- We have A in B .. C, treated as A >= B and then A <= C
3761 -- Bignum case
3763 if Is_RTE (Etype (Lop), RE_Bignum)
3764 or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum)
3765 or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum)
3766 then
3767 declare
3768 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3769 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3770 L : constant Entity_Id :=
3771 Make_Defining_Identifier (Loc, Name_uL);
3772 Lopnd : constant Node_Id := Convert_To_Bignum (Lop);
3773 Lbound : constant Node_Id :=
3774 Convert_To_Bignum (Low_Bound (Rop));
3775 Hbound : constant Node_Id :=
3776 Convert_To_Bignum (High_Bound (Rop));
3778 -- Now we rewrite the membership test node to look like
3780 -- do
3781 -- Bnn : Result_Type;
3782 -- declare
3783 -- M : Mark_Id := SS_Mark;
3784 -- L : Bignum := Lopnd;
3785 -- begin
3786 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3787 -- SS_Release (M);
3788 -- end;
3789 -- in
3790 -- Bnn
3791 -- end
3793 begin
3794 -- Insert declaration of L into declarations of bignum block
3796 Insert_After
3797 (Last (Declarations (Blk)),
3798 Make_Object_Declaration (Loc,
3799 Defining_Identifier => L,
3800 Object_Definition =>
3801 New_Occurrence_Of (RTE (RE_Bignum), Loc),
3802 Expression => Lopnd));
3804 -- Insert assignment to Bnn into expressions of bignum block
3806 Insert_Before
3807 (First (Statements (Handled_Statement_Sequence (Blk))),
3808 Make_Assignment_Statement (Loc,
3809 Name => New_Occurrence_Of (Bnn, Loc),
3810 Expression =>
3811 Make_And_Then (Loc,
3812 Left_Opnd =>
3813 Make_Function_Call (Loc,
3814 Name =>
3815 New_Occurrence_Of (RTE (RE_Big_GE), Loc),
3816 Parameter_Associations => New_List (
3817 New_Occurrence_Of (L, Loc),
3818 Lbound)),
3819 Right_Opnd =>
3820 Make_Function_Call (Loc,
3821 Name =>
3822 New_Occurrence_Of (RTE (RE_Big_LE), Loc),
3823 Parameter_Associations => New_List (
3824 New_Occurrence_Of (L, Loc),
3825 Hbound)))));
3827 -- Now rewrite the node
3829 Rewrite (N,
3830 Make_Expression_With_Actions (Loc,
3831 Actions => New_List (
3832 Make_Object_Declaration (Loc,
3833 Defining_Identifier => Bnn,
3834 Object_Definition =>
3835 New_Occurrence_Of (Result_Type, Loc)),
3836 Blk),
3837 Expression => New_Occurrence_Of (Bnn, Loc)));
3838 Analyze_And_Resolve (N, Result_Type);
3839 return;
3840 end;
3842 -- Here if no bignums around
3844 else
3845 -- Case where types are all the same
3847 if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop)))
3848 and then
3849 Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop)))
3850 then
3851 null;
3853 -- If types are not all the same, it means that we have rewritten
3854 -- at least one of them to be of type Long_Long_Integer, and we
3855 -- will convert the other operands to Long_Long_Integer.
3857 else
3858 Convert_To_And_Rewrite (LLIB, Lop);
3859 Set_Analyzed (Lop, False);
3860 Analyze_And_Resolve (Lop, LLIB);
3862 -- For the right operand, avoid unnecessary recursion into
3863 -- this routine, we know that overflow is not possible.
3865 Convert_To_And_Rewrite (LLIB, Low_Bound (Rop));
3866 Convert_To_And_Rewrite (LLIB, High_Bound (Rop));
3867 Set_Analyzed (Rop, False);
3868 Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check);
3869 end if;
3871 -- Now the three operands are of the same signed integer type,
3872 -- so we can use the normal expansion routine for membership,
3873 -- setting the flag to prevent recursion into this procedure.
3875 Set_No_Minimize_Eliminate (N);
3876 Expand_N_In (N);
3877 end if;
3879 -- Right operand is a subtype name and the subtype has a predicate. We
3880 -- have to make sure the predicate is checked, and for that we need to
3881 -- use the standard N_In circuitry with appropriate types.
3883 else
3884 pragma Assert (Present (Predicate_Function (Etype (Rop))));
3886 -- If types are "right", just call Expand_N_In preventing recursion
3888 if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then
3889 Set_No_Minimize_Eliminate (N);
3890 Expand_N_In (N);
3892 -- Bignum case
3894 elsif Is_RTE (Etype (Lop), RE_Bignum) then
3896 -- For X in T, we want to rewrite our node as
3898 -- do
3899 -- Bnn : Result_Type;
3901 -- declare
3902 -- M : Mark_Id := SS_Mark;
3903 -- Lnn : Long_Long_Integer'Base
3904 -- Nnn : Bignum;
3906 -- begin
3907 -- Nnn := X;
3909 -- if not Bignum_In_LLI_Range (Nnn) then
3910 -- Bnn := False;
3911 -- else
3912 -- Lnn := From_Bignum (Nnn);
3913 -- Bnn :=
3914 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3915 -- and then T'Base (Lnn) in T;
3916 -- end if;
3918 -- SS_Release (M);
3919 -- end
3920 -- in
3921 -- Bnn
3922 -- end
3924 -- A bit gruesome, but there doesn't seem to be a simpler way
3926 declare
3927 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3928 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3929 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N);
3930 Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N);
3931 T : constant Entity_Id := Etype (Rop);
3932 TB : constant Entity_Id := Base_Type (T);
3933 Nin : Node_Id;
3935 begin
3936 -- Mark the last membership operation to prevent recursion
3938 Nin :=
3939 Make_In (Loc,
3940 Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)),
3941 Right_Opnd => New_Occurrence_Of (T, Loc));
3942 Set_No_Minimize_Eliminate (Nin);
3944 -- Now decorate the block
3946 Insert_After
3947 (Last (Declarations (Blk)),
3948 Make_Object_Declaration (Loc,
3949 Defining_Identifier => Lnn,
3950 Object_Definition => New_Occurrence_Of (LLIB, Loc)));
3952 Insert_After
3953 (Last (Declarations (Blk)),
3954 Make_Object_Declaration (Loc,
3955 Defining_Identifier => Nnn,
3956 Object_Definition =>
3957 New_Occurrence_Of (RTE (RE_Bignum), Loc)));
3959 Insert_List_Before
3960 (First (Statements (Handled_Statement_Sequence (Blk))),
3961 New_List (
3962 Make_Assignment_Statement (Loc,
3963 Name => New_Occurrence_Of (Nnn, Loc),
3964 Expression => Relocate_Node (Lop)),
3966 Make_If_Statement (Loc,
3967 Condition =>
3968 Make_Op_Not (Loc,
3969 Right_Opnd =>
3970 Make_Function_Call (Loc,
3971 Name =>
3972 New_Occurrence_Of
3973 (RTE (RE_Bignum_In_LLI_Range), Loc),
3974 Parameter_Associations => New_List (
3975 New_Occurrence_Of (Nnn, Loc)))),
3977 Then_Statements => New_List (
3978 Make_Assignment_Statement (Loc,
3979 Name => New_Occurrence_Of (Bnn, Loc),
3980 Expression =>
3981 New_Occurrence_Of (Standard_False, Loc))),
3983 Else_Statements => New_List (
3984 Make_Assignment_Statement (Loc,
3985 Name => New_Occurrence_Of (Lnn, Loc),
3986 Expression =>
3987 Make_Function_Call (Loc,
3988 Name =>
3989 New_Occurrence_Of (RTE (RE_From_Bignum), Loc),
3990 Parameter_Associations => New_List (
3991 New_Occurrence_Of (Nnn, Loc)))),
3993 Make_Assignment_Statement (Loc,
3994 Name => New_Occurrence_Of (Bnn, Loc),
3995 Expression =>
3996 Make_And_Then (Loc,
3997 Left_Opnd =>
3998 Make_In (Loc,
3999 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4000 Right_Opnd =>
4001 Make_Range (Loc,
4002 Low_Bound =>
4003 Convert_To (LLIB,
4004 Make_Attribute_Reference (Loc,
4005 Attribute_Name => Name_First,
4006 Prefix =>
4007 New_Occurrence_Of (TB, Loc))),
4009 High_Bound =>
4010 Convert_To (LLIB,
4011 Make_Attribute_Reference (Loc,
4012 Attribute_Name => Name_Last,
4013 Prefix =>
4014 New_Occurrence_Of (TB, Loc))))),
4016 Right_Opnd => Nin))))));
4018 -- Now we can do the rewrite
4020 Rewrite (N,
4021 Make_Expression_With_Actions (Loc,
4022 Actions => New_List (
4023 Make_Object_Declaration (Loc,
4024 Defining_Identifier => Bnn,
4025 Object_Definition =>
4026 New_Occurrence_Of (Result_Type, Loc)),
4027 Blk),
4028 Expression => New_Occurrence_Of (Bnn, Loc)));
4029 Analyze_And_Resolve (N, Result_Type);
4030 return;
4031 end;
4033 -- Not bignum case, but types don't match (this means we rewrote the
4034 -- left operand to be Long_Long_Integer).
4036 else
4037 pragma Assert (Base_Type (Etype (Lop)) = LLIB);
4039 -- We rewrite the membership test as (where T is the type with
4040 -- the predicate, i.e. the type of the right operand)
4042 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
4043 -- and then T'Base (Lop) in T
4045 declare
4046 T : constant Entity_Id := Etype (Rop);
4047 TB : constant Entity_Id := Base_Type (T);
4048 Nin : Node_Id;
4050 begin
4051 -- The last membership test is marked to prevent recursion
4053 Nin :=
4054 Make_In (Loc,
4055 Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)),
4056 Right_Opnd => New_Occurrence_Of (T, Loc));
4057 Set_No_Minimize_Eliminate (Nin);
4059 -- Now do the rewrite
4061 Rewrite (N,
4062 Make_And_Then (Loc,
4063 Left_Opnd =>
4064 Make_In (Loc,
4065 Left_Opnd => Lop,
4066 Right_Opnd =>
4067 Make_Range (Loc,
4068 Low_Bound =>
4069 Convert_To (LLIB,
4070 Make_Attribute_Reference (Loc,
4071 Attribute_Name => Name_First,
4072 Prefix => New_Occurrence_Of (TB, Loc))),
4073 High_Bound =>
4074 Convert_To (LLIB,
4075 Make_Attribute_Reference (Loc,
4076 Attribute_Name => Name_Last,
4077 Prefix => New_Occurrence_Of (TB, Loc))))),
4078 Right_Opnd => Nin));
4079 Set_Analyzed (N, False);
4080 Analyze_And_Resolve (N, Restype);
4081 end;
4082 end if;
4083 end if;
4084 end Expand_Membership_Minimize_Eliminate_Overflow;
4086 ------------------------
4087 -- Expand_N_Allocator --
4088 ------------------------
4090 procedure Expand_N_Allocator (N : Node_Id) is
4091 PtrT : constant Entity_Id := Etype (N);
4092 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
4093 Etyp : constant Entity_Id := Etype (Expression (N));
4094 Loc : constant Source_Ptr := Sloc (N);
4095 Desig : Entity_Id;
4096 Nod : Node_Id;
4097 Pool : Entity_Id;
4098 Temp : Entity_Id;
4100 procedure Rewrite_Coextension (N : Node_Id);
4101 -- Static coextensions have the same lifetime as the entity they
4102 -- constrain. Such occurrences can be rewritten as aliased objects
4103 -- and their unrestricted access used instead of the coextension.
4105 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
4106 -- Given a constrained array type E, returns a node representing the
4107 -- code to compute the size in storage elements for the given type.
4108 -- This is done without using the attribute (which malfunctions for
4109 -- large sizes ???)
4111 -------------------------
4112 -- Rewrite_Coextension --
4113 -------------------------
4115 procedure Rewrite_Coextension (N : Node_Id) is
4116 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
4117 Temp_Decl : Node_Id;
4119 begin
4120 -- Generate:
4121 -- Cnn : aliased Etyp;
4123 Temp_Decl :=
4124 Make_Object_Declaration (Loc,
4125 Defining_Identifier => Temp_Id,
4126 Aliased_Present => True,
4127 Object_Definition => New_Occurrence_Of (Etyp, Loc));
4129 if Nkind (Expression (N)) = N_Qualified_Expression then
4130 Set_Expression (Temp_Decl, Expression (Expression (N)));
4131 end if;
4133 Insert_Action (N, Temp_Decl);
4134 Rewrite (N,
4135 Make_Attribute_Reference (Loc,
4136 Prefix => New_Occurrence_Of (Temp_Id, Loc),
4137 Attribute_Name => Name_Unrestricted_Access));
4139 Analyze_And_Resolve (N, PtrT);
4140 end Rewrite_Coextension;
4142 ------------------------------
4143 -- Size_In_Storage_Elements --
4144 ------------------------------
4146 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
4147 begin
4148 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4149 -- However, the reason for the existence of this function is
4150 -- to construct a test for sizes too large, which means near the
4151 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4152 -- is that we get overflows when sizes are greater than 2**31.
4154 -- So what we end up doing for array types is to use the expression:
4156 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4158 -- which avoids this problem. All this is a bit bogus, but it does
4159 -- mean we catch common cases of trying to allocate arrays that
4160 -- are too large, and which in the absence of a check results in
4161 -- undetected chaos ???
4163 declare
4164 Len : Node_Id;
4165 Res : Node_Id;
4167 begin
4168 for J in 1 .. Number_Dimensions (E) loop
4169 Len :=
4170 Make_Attribute_Reference (Loc,
4171 Prefix => New_Occurrence_Of (E, Loc),
4172 Attribute_Name => Name_Length,
4173 Expressions => New_List (Make_Integer_Literal (Loc, J)));
4175 if J = 1 then
4176 Res := Len;
4178 else
4179 Res :=
4180 Make_Op_Multiply (Loc,
4181 Left_Opnd => Res,
4182 Right_Opnd => Len);
4183 end if;
4184 end loop;
4186 return
4187 Make_Op_Multiply (Loc,
4188 Left_Opnd => Len,
4189 Right_Opnd =>
4190 Make_Attribute_Reference (Loc,
4191 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
4192 Attribute_Name => Name_Max_Size_In_Storage_Elements));
4193 end;
4194 end Size_In_Storage_Elements;
4196 -- Start of processing for Expand_N_Allocator
4198 begin
4199 -- RM E.2.3(22). We enforce that the expected type of an allocator
4200 -- shall not be a remote access-to-class-wide-limited-private type
4202 -- Why is this being done at expansion time, seems clearly wrong ???
4204 Validate_Remote_Access_To_Class_Wide_Type (N);
4206 -- Processing for anonymous access-to-controlled types. These access
4207 -- types receive a special finalization master which appears in the
4208 -- declarations of the enclosing semantic unit. This expansion is done
4209 -- now to ensure that any additional types generated by this routine or
4210 -- Expand_Allocator_Expression inherit the proper type attributes.
4212 if (Ekind (PtrT) = E_Anonymous_Access_Type
4213 or else
4214 (Is_Itype (PtrT) and then No (Finalization_Master (PtrT))))
4215 and then Needs_Finalization (Dtyp)
4216 then
4217 -- Anonymous access-to-controlled types allocate on the global pool.
4218 -- Do not set this attribute on .NET/JVM since those targets do not
4219 -- support pools.
4221 if No (Associated_Storage_Pool (PtrT)) and then VM_Target = No_VM then
4222 Set_Associated_Storage_Pool
4223 (PtrT, Get_Global_Pool_For_Access_Type (PtrT));
4224 end if;
4226 -- The finalization master must be inserted and analyzed as part of
4227 -- the current semantic unit. This form of expansion is not carried
4228 -- out in Alfa mode because it is useless. Note that the master is
4229 -- updated when analysis changes current units.
4231 if not Alfa_Mode then
4232 Set_Finalization_Master (PtrT, Current_Anonymous_Master);
4233 end if;
4234 end if;
4236 -- Set the storage pool and find the appropriate version of Allocate to
4237 -- call. Do not overwrite the storage pool if it is already set, which
4238 -- can happen for build-in-place function returns (see
4239 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4241 if No (Storage_Pool (N)) then
4242 Pool := Associated_Storage_Pool (Root_Type (PtrT));
4244 if Present (Pool) then
4245 Set_Storage_Pool (N, Pool);
4247 if Is_RTE (Pool, RE_SS_Pool) then
4248 if VM_Target = No_VM then
4249 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
4250 end if;
4252 -- In the case of an allocator for a simple storage pool, locate
4253 -- and save a reference to the pool type's Allocate routine.
4255 elsif Present (Get_Rep_Pragma
4256 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4257 then
4258 declare
4259 Pool_Type : constant Entity_Id := Base_Type (Etype (Pool));
4260 Alloc_Op : Entity_Id;
4261 begin
4262 Alloc_Op := Get_Name_Entity_Id (Name_Allocate);
4263 while Present (Alloc_Op) loop
4264 if Scope (Alloc_Op) = Scope (Pool_Type)
4265 and then Present (First_Formal (Alloc_Op))
4266 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
4267 then
4268 Set_Procedure_To_Call (N, Alloc_Op);
4269 exit;
4270 else
4271 Alloc_Op := Homonym (Alloc_Op);
4272 end if;
4273 end loop;
4274 end;
4276 elsif Is_Class_Wide_Type (Etype (Pool)) then
4277 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
4279 else
4280 Set_Procedure_To_Call (N,
4281 Find_Prim_Op (Etype (Pool), Name_Allocate));
4282 end if;
4283 end if;
4284 end if;
4286 -- Under certain circumstances we can replace an allocator by an access
4287 -- to statically allocated storage. The conditions, as noted in AARM
4288 -- 3.10 (10c) are as follows:
4290 -- Size and initial value is known at compile time
4291 -- Access type is access-to-constant
4293 -- The allocator is not part of a constraint on a record component,
4294 -- because in that case the inserted actions are delayed until the
4295 -- record declaration is fully analyzed, which is too late for the
4296 -- analysis of the rewritten allocator.
4298 if Is_Access_Constant (PtrT)
4299 and then Nkind (Expression (N)) = N_Qualified_Expression
4300 and then Compile_Time_Known_Value (Expression (Expression (N)))
4301 and then Size_Known_At_Compile_Time
4302 (Etype (Expression (Expression (N))))
4303 and then not Is_Record_Type (Current_Scope)
4304 then
4305 -- Here we can do the optimization. For the allocator
4307 -- new x'(y)
4309 -- We insert an object declaration
4311 -- Tnn : aliased x := y;
4313 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4314 -- marked as requiring static allocation.
4316 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
4317 Desig := Subtype_Mark (Expression (N));
4319 -- If context is constrained, use constrained subtype directly,
4320 -- so that the constant is not labelled as having a nominally
4321 -- unconstrained subtype.
4323 if Entity (Desig) = Base_Type (Dtyp) then
4324 Desig := New_Occurrence_Of (Dtyp, Loc);
4325 end if;
4327 Insert_Action (N,
4328 Make_Object_Declaration (Loc,
4329 Defining_Identifier => Temp,
4330 Aliased_Present => True,
4331 Constant_Present => Is_Access_Constant (PtrT),
4332 Object_Definition => Desig,
4333 Expression => Expression (Expression (N))));
4335 Rewrite (N,
4336 Make_Attribute_Reference (Loc,
4337 Prefix => New_Occurrence_Of (Temp, Loc),
4338 Attribute_Name => Name_Unrestricted_Access));
4340 Analyze_And_Resolve (N, PtrT);
4342 -- We set the variable as statically allocated, since we don't want
4343 -- it going on the stack of the current procedure!
4345 Set_Is_Statically_Allocated (Temp);
4346 return;
4347 end if;
4349 -- Same if the allocator is an access discriminant for a local object:
4350 -- instead of an allocator we create a local value and constrain the
4351 -- enclosing object with the corresponding access attribute.
4353 if Is_Static_Coextension (N) then
4354 Rewrite_Coextension (N);
4355 return;
4356 end if;
4358 -- Check for size too large, we do this because the back end misses
4359 -- proper checks here and can generate rubbish allocation calls when
4360 -- we are near the limit. We only do this for the 32-bit address case
4361 -- since that is from a practical point of view where we see a problem.
4363 if System_Address_Size = 32
4364 and then not Storage_Checks_Suppressed (PtrT)
4365 and then not Storage_Checks_Suppressed (Dtyp)
4366 and then not Storage_Checks_Suppressed (Etyp)
4367 then
4368 -- The check we want to generate should look like
4370 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4371 -- raise Storage_Error;
4372 -- end if;
4374 -- where 3.5 gigabytes is a constant large enough to accommodate any
4375 -- reasonable request for. But we can't do it this way because at
4376 -- least at the moment we don't compute this attribute right, and
4377 -- can silently give wrong results when the result gets large. Since
4378 -- this is all about large results, that's bad, so instead we only
4379 -- apply the check for constrained arrays, and manually compute the
4380 -- value of the attribute ???
4382 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
4383 Insert_Action (N,
4384 Make_Raise_Storage_Error (Loc,
4385 Condition =>
4386 Make_Op_Gt (Loc,
4387 Left_Opnd => Size_In_Storage_Elements (Etyp),
4388 Right_Opnd =>
4389 Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))),
4390 Reason => SE_Object_Too_Large));
4391 end if;
4392 end if;
4394 -- Handle case of qualified expression (other than optimization above)
4395 -- First apply constraint checks, because the bounds or discriminants
4396 -- in the aggregate might not match the subtype mark in the allocator.
4398 if Nkind (Expression (N)) = N_Qualified_Expression then
4399 Apply_Constraint_Check
4400 (Expression (Expression (N)), Etype (Expression (N)));
4402 Expand_Allocator_Expression (N);
4403 return;
4404 end if;
4406 -- If the allocator is for a type which requires initialization, and
4407 -- there is no initial value (i.e. operand is a subtype indication
4408 -- rather than a qualified expression), then we must generate a call to
4409 -- the initialization routine using an expressions action node:
4411 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4413 -- Here ptr_T is the pointer type for the allocator, and T is the
4414 -- subtype of the allocator. A special case arises if the designated
4415 -- type of the access type is a task or contains tasks. In this case
4416 -- the call to Init (Temp.all ...) is replaced by code that ensures
4417 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4418 -- for details). In addition, if the type T is a task T, then the
4419 -- first argument to Init must be converted to the task record type.
4421 declare
4422 T : constant Entity_Id := Entity (Expression (N));
4423 Args : List_Id;
4424 Decls : List_Id;
4425 Decl : Node_Id;
4426 Discr : Elmt_Id;
4427 Init : Entity_Id;
4428 Init_Arg1 : Node_Id;
4429 Temp_Decl : Node_Id;
4430 Temp_Type : Entity_Id;
4432 begin
4433 if No_Initialization (N) then
4435 -- Even though this might be a simple allocation, create a custom
4436 -- Allocate if the context requires it. Since .NET/JVM compilers
4437 -- do not support pools, this step is skipped.
4439 if VM_Target = No_VM
4440 and then Present (Finalization_Master (PtrT))
4441 then
4442 Build_Allocate_Deallocate_Proc
4443 (N => N,
4444 Is_Allocate => True);
4445 end if;
4447 -- Case of no initialization procedure present
4449 elsif not Has_Non_Null_Base_Init_Proc (T) then
4451 -- Case of simple initialization required
4453 if Needs_Simple_Initialization (T) then
4454 Check_Restriction (No_Default_Initialization, N);
4455 Rewrite (Expression (N),
4456 Make_Qualified_Expression (Loc,
4457 Subtype_Mark => New_Occurrence_Of (T, Loc),
4458 Expression => Get_Simple_Init_Val (T, N)));
4460 Analyze_And_Resolve (Expression (Expression (N)), T);
4461 Analyze_And_Resolve (Expression (N), T);
4462 Set_Paren_Count (Expression (Expression (N)), 1);
4463 Expand_N_Allocator (N);
4465 -- No initialization required
4467 else
4468 null;
4469 end if;
4471 -- Case of initialization procedure present, must be called
4473 else
4474 Check_Restriction (No_Default_Initialization, N);
4476 if not Restriction_Active (No_Default_Initialization) then
4477 Init := Base_Init_Proc (T);
4478 Nod := N;
4479 Temp := Make_Temporary (Loc, 'P');
4481 -- Construct argument list for the initialization routine call
4483 Init_Arg1 :=
4484 Make_Explicit_Dereference (Loc,
4485 Prefix =>
4486 New_Reference_To (Temp, Loc));
4488 Set_Assignment_OK (Init_Arg1);
4489 Temp_Type := PtrT;
4491 -- The initialization procedure expects a specific type. if the
4492 -- context is access to class wide, indicate that the object
4493 -- being allocated has the right specific type.
4495 if Is_Class_Wide_Type (Dtyp) then
4496 Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1);
4497 end if;
4499 -- If designated type is a concurrent type or if it is private
4500 -- type whose definition is a concurrent type, the first
4501 -- argument in the Init routine has to be unchecked conversion
4502 -- to the corresponding record type. If the designated type is
4503 -- a derived type, also convert the argument to its root type.
4505 if Is_Concurrent_Type (T) then
4506 Init_Arg1 :=
4507 Unchecked_Convert_To (
4508 Corresponding_Record_Type (T), Init_Arg1);
4510 elsif Is_Private_Type (T)
4511 and then Present (Full_View (T))
4512 and then Is_Concurrent_Type (Full_View (T))
4513 then
4514 Init_Arg1 :=
4515 Unchecked_Convert_To
4516 (Corresponding_Record_Type (Full_View (T)), Init_Arg1);
4518 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
4519 declare
4520 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
4522 begin
4523 Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1);
4524 Set_Etype (Init_Arg1, Ftyp);
4525 end;
4526 end if;
4528 Args := New_List (Init_Arg1);
4530 -- For the task case, pass the Master_Id of the access type as
4531 -- the value of the _Master parameter, and _Chain as the value
4532 -- of the _Chain parameter (_Chain will be defined as part of
4533 -- the generated code for the allocator).
4535 -- In Ada 2005, the context may be a function that returns an
4536 -- anonymous access type. In that case the Master_Id has been
4537 -- created when expanding the function declaration.
4539 if Has_Task (T) then
4540 if No (Master_Id (Base_Type (PtrT))) then
4542 -- The designated type was an incomplete type, and the
4543 -- access type did not get expanded. Salvage it now.
4545 if not Restriction_Active (No_Task_Hierarchy) then
4546 pragma Assert (Present (Parent (Base_Type (PtrT))));
4547 Expand_N_Full_Type_Declaration
4548 (Parent (Base_Type (PtrT)));
4549 end if;
4550 end if;
4552 -- If the context of the allocator is a declaration or an
4553 -- assignment, we can generate a meaningful image for it,
4554 -- even though subsequent assignments might remove the
4555 -- connection between task and entity. We build this image
4556 -- when the left-hand side is a simple variable, a simple
4557 -- indexed assignment or a simple selected component.
4559 if Nkind (Parent (N)) = N_Assignment_Statement then
4560 declare
4561 Nam : constant Node_Id := Name (Parent (N));
4563 begin
4564 if Is_Entity_Name (Nam) then
4565 Decls :=
4566 Build_Task_Image_Decls
4567 (Loc,
4568 New_Occurrence_Of
4569 (Entity (Nam), Sloc (Nam)), T);
4571 elsif Nkind_In (Nam, N_Indexed_Component,
4572 N_Selected_Component)
4573 and then Is_Entity_Name (Prefix (Nam))
4574 then
4575 Decls :=
4576 Build_Task_Image_Decls
4577 (Loc, Nam, Etype (Prefix (Nam)));
4578 else
4579 Decls := Build_Task_Image_Decls (Loc, T, T);
4580 end if;
4581 end;
4583 elsif Nkind (Parent (N)) = N_Object_Declaration then
4584 Decls :=
4585 Build_Task_Image_Decls
4586 (Loc, Defining_Identifier (Parent (N)), T);
4588 else
4589 Decls := Build_Task_Image_Decls (Loc, T, T);
4590 end if;
4592 if Restriction_Active (No_Task_Hierarchy) then
4593 Append_To (Args,
4594 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
4595 else
4596 Append_To (Args,
4597 New_Reference_To
4598 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
4599 end if;
4601 Append_To (Args, Make_Identifier (Loc, Name_uChain));
4603 Decl := Last (Decls);
4604 Append_To (Args,
4605 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
4607 -- Has_Task is false, Decls not used
4609 else
4610 Decls := No_List;
4611 end if;
4613 -- Add discriminants if discriminated type
4615 declare
4616 Dis : Boolean := False;
4617 Typ : Entity_Id;
4619 begin
4620 if Has_Discriminants (T) then
4621 Dis := True;
4622 Typ := T;
4624 elsif Is_Private_Type (T)
4625 and then Present (Full_View (T))
4626 and then Has_Discriminants (Full_View (T))
4627 then
4628 Dis := True;
4629 Typ := Full_View (T);
4630 end if;
4632 if Dis then
4634 -- If the allocated object will be constrained by the
4635 -- default values for discriminants, then build a subtype
4636 -- with those defaults, and change the allocated subtype
4637 -- to that. Note that this happens in fewer cases in Ada
4638 -- 2005 (AI-363).
4640 if not Is_Constrained (Typ)
4641 and then Present (Discriminant_Default_Value
4642 (First_Discriminant (Typ)))
4643 and then (Ada_Version < Ada_2005
4644 or else not
4645 Effectively_Has_Constrained_Partial_View
4646 (Typ => Typ,
4647 Scop => Current_Scope))
4648 then
4649 Typ := Build_Default_Subtype (Typ, N);
4650 Set_Expression (N, New_Reference_To (Typ, Loc));
4651 end if;
4653 Discr := First_Elmt (Discriminant_Constraint (Typ));
4654 while Present (Discr) loop
4655 Nod := Node (Discr);
4656 Append (New_Copy_Tree (Node (Discr)), Args);
4658 -- AI-416: when the discriminant constraint is an
4659 -- anonymous access type make sure an accessibility
4660 -- check is inserted if necessary (3.10.2(22.q/2))
4662 if Ada_Version >= Ada_2005
4663 and then
4664 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
4665 then
4666 Apply_Accessibility_Check
4667 (Nod, Typ, Insert_Node => Nod);
4668 end if;
4670 Next_Elmt (Discr);
4671 end loop;
4672 end if;
4673 end;
4675 -- We set the allocator as analyzed so that when we analyze
4676 -- the if expression node, we do not get an unwanted recursive
4677 -- expansion of the allocator expression.
4679 Set_Analyzed (N, True);
4680 Nod := Relocate_Node (N);
4682 -- Here is the transformation:
4683 -- input: new Ctrl_Typ
4684 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4685 -- Ctrl_TypIP (Temp.all, ...);
4686 -- [Deep_]Initialize (Temp.all);
4688 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4689 -- is the subtype of the allocator.
4691 Temp_Decl :=
4692 Make_Object_Declaration (Loc,
4693 Defining_Identifier => Temp,
4694 Constant_Present => True,
4695 Object_Definition => New_Reference_To (Temp_Type, Loc),
4696 Expression => Nod);
4698 Set_Assignment_OK (Temp_Decl);
4699 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
4701 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
4703 -- If the designated type is a task type or contains tasks,
4704 -- create block to activate created tasks, and insert
4705 -- declaration for Task_Image variable ahead of call.
4707 if Has_Task (T) then
4708 declare
4709 L : constant List_Id := New_List;
4710 Blk : Node_Id;
4711 begin
4712 Build_Task_Allocate_Block (L, Nod, Args);
4713 Blk := Last (L);
4714 Insert_List_Before (First (Declarations (Blk)), Decls);
4715 Insert_Actions (N, L);
4716 end;
4718 else
4719 Insert_Action (N,
4720 Make_Procedure_Call_Statement (Loc,
4721 Name => New_Reference_To (Init, Loc),
4722 Parameter_Associations => Args));
4723 end if;
4725 if Needs_Finalization (T) then
4727 -- Generate:
4728 -- [Deep_]Initialize (Init_Arg1);
4730 Insert_Action (N,
4731 Make_Init_Call
4732 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4733 Typ => T));
4735 if Present (Finalization_Master (PtrT)) then
4737 -- Special processing for .NET/JVM, the allocated object
4738 -- is attached to the finalization master. Generate:
4740 -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1));
4742 -- Types derived from [Limited_]Controlled are the only
4743 -- ones considered since they have fields Prev and Next.
4745 if VM_Target /= No_VM then
4746 if Is_Controlled (T) then
4747 Insert_Action (N,
4748 Make_Attach_Call
4749 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4750 Ptr_Typ => PtrT));
4751 end if;
4753 -- Default case, generate:
4755 -- Set_Finalize_Address
4756 -- (<PtrT>FM, <T>FD'Unrestricted_Access);
4758 -- Do not generate this call in the following cases:
4760 -- * Alfa mode - the call is useless and results in
4761 -- unwanted expansion.
4763 -- * CodePeer mode - TSS primitive Finalize_Address is
4764 -- not created in this mode.
4766 elsif not Alfa_Mode
4767 and then not CodePeer_Mode
4768 then
4769 Insert_Action (N,
4770 Make_Set_Finalize_Address_Call
4771 (Loc => Loc,
4772 Typ => T,
4773 Ptr_Typ => PtrT));
4774 end if;
4775 end if;
4776 end if;
4778 Rewrite (N, New_Reference_To (Temp, Loc));
4779 Analyze_And_Resolve (N, PtrT);
4780 end if;
4781 end if;
4782 end;
4784 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
4785 -- object that has been rewritten as a reference, we displace "this"
4786 -- to reference properly its secondary dispatch table.
4788 if Nkind (N) = N_Identifier
4789 and then Is_Interface (Dtyp)
4790 then
4791 Displace_Allocator_Pointer (N);
4792 end if;
4794 exception
4795 when RE_Not_Available =>
4796 return;
4797 end Expand_N_Allocator;
4799 -----------------------
4800 -- Expand_N_And_Then --
4801 -----------------------
4803 procedure Expand_N_And_Then (N : Node_Id)
4804 renames Expand_Short_Circuit_Operator;
4806 ------------------------------
4807 -- Expand_N_Case_Expression --
4808 ------------------------------
4810 procedure Expand_N_Case_Expression (N : Node_Id) is
4811 Loc : constant Source_Ptr := Sloc (N);
4812 Typ : constant Entity_Id := Etype (N);
4813 Cstmt : Node_Id;
4814 Tnn : Entity_Id;
4815 Pnn : Entity_Id;
4816 Actions : List_Id;
4817 Ttyp : Entity_Id;
4818 Alt : Node_Id;
4819 Fexp : Node_Id;
4821 begin
4822 -- Check for MINIMIZED/ELIMINATED overflow mode
4824 if Minimized_Eliminated_Overflow_Check (N) then
4825 Apply_Arithmetic_Overflow_Check (N);
4826 return;
4827 end if;
4829 -- We expand
4831 -- case X is when A => AX, when B => BX ...
4833 -- to
4835 -- do
4836 -- Tnn : typ;
4837 -- case X is
4838 -- when A =>
4839 -- Tnn := AX;
4840 -- when B =>
4841 -- Tnn := BX;
4842 -- ...
4843 -- end case;
4844 -- in Tnn end;
4846 -- However, this expansion is wrong for limited types, and also
4847 -- wrong for unconstrained types (since the bounds may not be the
4848 -- same in all branches). Furthermore it involves an extra copy
4849 -- for large objects. So we take care of this by using the following
4850 -- modified expansion for non-elementary types:
4852 -- do
4853 -- type Pnn is access all typ;
4854 -- Tnn : Pnn;
4855 -- case X is
4856 -- when A =>
4857 -- T := AX'Unrestricted_Access;
4858 -- when B =>
4859 -- T := BX'Unrestricted_Access;
4860 -- ...
4861 -- end case;
4862 -- in Tnn.all end;
4864 Cstmt :=
4865 Make_Case_Statement (Loc,
4866 Expression => Expression (N),
4867 Alternatives => New_List);
4869 Actions := New_List;
4871 -- Scalar case
4873 if Is_Elementary_Type (Typ) then
4874 Ttyp := Typ;
4876 else
4877 Pnn := Make_Temporary (Loc, 'P');
4878 Append_To (Actions,
4879 Make_Full_Type_Declaration (Loc,
4880 Defining_Identifier => Pnn,
4881 Type_Definition =>
4882 Make_Access_To_Object_Definition (Loc,
4883 All_Present => True,
4884 Subtype_Indication =>
4885 New_Reference_To (Typ, Loc))));
4886 Ttyp := Pnn;
4887 end if;
4889 Tnn := Make_Temporary (Loc, 'T');
4890 Append_To (Actions,
4891 Make_Object_Declaration (Loc,
4892 Defining_Identifier => Tnn,
4893 Object_Definition => New_Occurrence_Of (Ttyp, Loc)));
4895 -- Now process the alternatives
4897 Alt := First (Alternatives (N));
4898 while Present (Alt) loop
4899 declare
4900 Aexp : Node_Id := Expression (Alt);
4901 Aloc : constant Source_Ptr := Sloc (Aexp);
4902 Stats : List_Id;
4904 begin
4905 -- As described above, take Unrestricted_Access for case of non-
4906 -- scalar types, to avoid big copies, and special cases.
4908 if not Is_Elementary_Type (Typ) then
4909 Aexp :=
4910 Make_Attribute_Reference (Aloc,
4911 Prefix => Relocate_Node (Aexp),
4912 Attribute_Name => Name_Unrestricted_Access);
4913 end if;
4915 Stats := New_List (
4916 Make_Assignment_Statement (Aloc,
4917 Name => New_Occurrence_Of (Tnn, Loc),
4918 Expression => Aexp));
4920 -- Propagate declarations inserted in the node by Insert_Actions
4921 -- (for example, temporaries generated to remove side effects).
4922 -- These actions must remain attached to the alternative, given
4923 -- that they are generated by the corresponding expression.
4925 if Present (Sinfo.Actions (Alt)) then
4926 Prepend_List (Sinfo.Actions (Alt), Stats);
4927 end if;
4929 Append_To
4930 (Alternatives (Cstmt),
4931 Make_Case_Statement_Alternative (Sloc (Alt),
4932 Discrete_Choices => Discrete_Choices (Alt),
4933 Statements => Stats));
4934 end;
4936 Next (Alt);
4937 end loop;
4939 Append_To (Actions, Cstmt);
4941 -- Construct and return final expression with actions
4943 if Is_Elementary_Type (Typ) then
4944 Fexp := New_Occurrence_Of (Tnn, Loc);
4945 else
4946 Fexp :=
4947 Make_Explicit_Dereference (Loc,
4948 Prefix => New_Occurrence_Of (Tnn, Loc));
4949 end if;
4951 Rewrite (N,
4952 Make_Expression_With_Actions (Loc,
4953 Expression => Fexp,
4954 Actions => Actions));
4956 Analyze_And_Resolve (N, Typ);
4957 end Expand_N_Case_Expression;
4959 -----------------------------------
4960 -- Expand_N_Explicit_Dereference --
4961 -----------------------------------
4963 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
4964 begin
4965 -- Insert explicit dereference call for the checked storage pool case
4967 Insert_Dereference_Action (Prefix (N));
4969 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
4970 -- we set the atomic sync flag.
4972 if Is_Atomic (Etype (N))
4973 and then not Atomic_Synchronization_Disabled (Etype (N))
4974 then
4975 Activate_Atomic_Synchronization (N);
4976 end if;
4977 end Expand_N_Explicit_Dereference;
4979 --------------------------------------
4980 -- Expand_N_Expression_With_Actions --
4981 --------------------------------------
4983 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
4985 procedure Process_Transient_Object (Decl : Node_Id);
4986 -- Given the declaration of a controlled transient declared inside the
4987 -- Actions list of an Expression_With_Actions, generate all necessary
4988 -- types and hooks in order to properly finalize the transient. This
4989 -- mechanism works in conjunction with Build_Finalizer.
4991 ------------------------------
4992 -- Process_Transient_Object --
4993 ------------------------------
4995 procedure Process_Transient_Object (Decl : Node_Id) is
4997 function Find_Insertion_Node return Node_Id;
4998 -- Complex conditions in if statements may be converted into nested
4999 -- EWAs. In this case, any generated code must be inserted before the
5000 -- if statement to ensure proper visibility of the hook objects. This
5001 -- routine returns the top most short circuit operator or the parent
5002 -- of the EWA if no nesting was detected.
5004 -------------------------
5005 -- Find_Insertion_Node --
5006 -------------------------
5008 function Find_Insertion_Node return Node_Id is
5009 Par : Node_Id;
5011 begin
5012 -- Climb up the branches of a complex condition
5014 Par := N;
5015 while Nkind_In (Parent (Par), N_And_Then, N_Op_Not, N_Or_Else) loop
5016 Par := Parent (Par);
5017 end loop;
5019 return Par;
5020 end Find_Insertion_Node;
5022 -- Local variables
5024 Ins_Node : constant Node_Id := Find_Insertion_Node;
5025 Loc : constant Source_Ptr := Sloc (Decl);
5026 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
5027 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
5028 Desig_Typ : Entity_Id;
5029 Expr : Node_Id;
5030 Ptr_Decl : Node_Id;
5031 Ptr_Id : Entity_Id;
5032 Temp_Decl : Node_Id;
5033 Temp_Id : Node_Id;
5035 -- Start of processing for Process_Transient_Object
5037 begin
5038 -- Step 1: Create the access type which provides a reference to the
5039 -- transient object.
5041 if Is_Access_Type (Obj_Typ) then
5042 Desig_Typ := Directly_Designated_Type (Obj_Typ);
5043 else
5044 Desig_Typ := Obj_Typ;
5045 end if;
5047 -- Generate:
5048 -- Ann : access [all] <Desig_Typ>;
5050 Ptr_Id := Make_Temporary (Loc, 'A');
5052 Ptr_Decl :=
5053 Make_Full_Type_Declaration (Loc,
5054 Defining_Identifier => Ptr_Id,
5055 Type_Definition =>
5056 Make_Access_To_Object_Definition (Loc,
5057 All_Present =>
5058 Ekind (Obj_Typ) = E_General_Access_Type,
5059 Subtype_Indication => New_Reference_To (Desig_Typ, Loc)));
5061 Insert_Action (Ins_Node, Ptr_Decl);
5062 Analyze (Ptr_Decl);
5064 -- Step 2: Create a temporary which acts as a hook to the transient
5065 -- object. Generate:
5067 -- Temp : Ptr_Id := null;
5069 Temp_Id := Make_Temporary (Loc, 'T');
5071 Temp_Decl :=
5072 Make_Object_Declaration (Loc,
5073 Defining_Identifier => Temp_Id,
5074 Object_Definition => New_Reference_To (Ptr_Id, Loc));
5076 Insert_Action (Ins_Node, Temp_Decl);
5077 Analyze (Temp_Decl);
5079 -- Mark this temporary as created for the purposes of exporting the
5080 -- transient declaration out of the Actions list. This signals the
5081 -- machinery in Build_Finalizer to recognize this special case.
5083 Set_Status_Flag_Or_Transient_Decl (Temp_Id, Decl);
5085 -- Step 3: Hook the transient object to the temporary
5087 if Is_Access_Type (Obj_Typ) then
5088 Expr := Convert_To (Ptr_Id, New_Reference_To (Obj_Id, Loc));
5089 else
5090 Expr :=
5091 Make_Attribute_Reference (Loc,
5092 Prefix => New_Reference_To (Obj_Id, Loc),
5093 Attribute_Name => Name_Unrestricted_Access);
5094 end if;
5096 -- Generate:
5097 -- Temp := Ptr_Id (Obj_Id);
5098 -- <or>
5099 -- Temp := Obj_Id'Unrestricted_Access;
5101 Insert_After_And_Analyze (Decl,
5102 Make_Assignment_Statement (Loc,
5103 Name => New_Reference_To (Temp_Id, Loc),
5104 Expression => Expr));
5105 end Process_Transient_Object;
5107 -- Local variables
5109 Decl : Node_Id;
5111 -- Start of processing for Expand_N_Expression_With_Actions
5113 begin
5114 Decl := First (Actions (N));
5115 while Present (Decl) loop
5116 if Nkind (Decl) = N_Object_Declaration
5117 and then Is_Finalizable_Transient (Decl, N)
5118 then
5119 Process_Transient_Object (Decl);
5120 end if;
5122 Next (Decl);
5123 end loop;
5124 end Expand_N_Expression_With_Actions;
5126 ----------------------------
5127 -- Expand_N_If_Expression --
5128 ----------------------------
5130 -- Deal with limited types and condition actions
5132 procedure Expand_N_If_Expression (N : Node_Id) is
5133 function Create_Alternative
5134 (Loc : Source_Ptr;
5135 Temp_Id : Entity_Id;
5136 Flag_Id : Entity_Id;
5137 Expr : Node_Id) return List_Id;
5138 -- Build the statements of a "then" or "else" dependent expression
5139 -- alternative. Temp_Id is the if expression result, Flag_Id is a
5140 -- finalization flag created to service expression Expr.
5142 function Is_Controlled_Function_Call (Expr : Node_Id) return Boolean;
5143 -- Determine if expression Expr is a rewritten controlled function call
5145 ------------------------
5146 -- Create_Alternative --
5147 ------------------------
5149 function Create_Alternative
5150 (Loc : Source_Ptr;
5151 Temp_Id : Entity_Id;
5152 Flag_Id : Entity_Id;
5153 Expr : Node_Id) return List_Id
5155 Result : constant List_Id := New_List;
5157 begin
5158 -- Generate:
5159 -- Fnn := True;
5161 if Present (Flag_Id)
5162 and then not Is_Controlled_Function_Call (Expr)
5163 then
5164 Append_To (Result,
5165 Make_Assignment_Statement (Loc,
5166 Name => New_Reference_To (Flag_Id, Loc),
5167 Expression => New_Reference_To (Standard_True, Loc)));
5168 end if;
5170 -- Generate:
5171 -- Cnn := <expr>'Unrestricted_Access;
5173 Append_To (Result,
5174 Make_Assignment_Statement (Loc,
5175 Name => New_Reference_To (Temp_Id, Loc),
5176 Expression =>
5177 Make_Attribute_Reference (Loc,
5178 Prefix => Relocate_Node (Expr),
5179 Attribute_Name => Name_Unrestricted_Access)));
5181 return Result;
5182 end Create_Alternative;
5184 ---------------------------------
5185 -- Is_Controlled_Function_Call --
5186 ---------------------------------
5188 function Is_Controlled_Function_Call (Expr : Node_Id) return Boolean is
5189 begin
5190 return
5191 Nkind (Original_Node (Expr)) = N_Function_Call
5192 and then Needs_Finalization (Etype (Expr));
5193 end Is_Controlled_Function_Call;
5195 -- Local variables
5197 Loc : constant Source_Ptr := Sloc (N);
5198 Cond : constant Node_Id := First (Expressions (N));
5199 Thenx : constant Node_Id := Next (Cond);
5200 Elsex : constant Node_Id := Next (Thenx);
5201 Typ : constant Entity_Id := Etype (N);
5203 Actions : List_Id;
5204 Cnn : Entity_Id;
5205 Decl : Node_Id;
5206 Expr : Node_Id;
5207 New_If : Node_Id;
5208 New_N : Node_Id;
5210 begin
5211 -- Check for MINIMIZED/ELIMINATED overflow mode
5213 if Minimized_Eliminated_Overflow_Check (N) then
5214 Apply_Arithmetic_Overflow_Check (N);
5215 return;
5216 end if;
5218 -- Fold at compile time if condition known. We have already folded
5219 -- static if expressions, but it is possible to fold any case in which
5220 -- the condition is known at compile time, even though the result is
5221 -- non-static.
5223 -- Note that we don't do the fold of such cases in Sem_Elab because
5224 -- it can cause infinite loops with the expander adding a conditional
5225 -- expression, and Sem_Elab circuitry removing it repeatedly.
5227 if Compile_Time_Known_Value (Cond) then
5228 if Is_True (Expr_Value (Cond)) then
5229 Expr := Thenx;
5230 Actions := Then_Actions (N);
5231 else
5232 Expr := Elsex;
5233 Actions := Else_Actions (N);
5234 end if;
5236 Remove (Expr);
5238 if Present (Actions) then
5240 -- If we are not allowed to use Expression_With_Actions, just skip
5241 -- the optimization, it is not critical for correctness.
5243 if not Use_Expression_With_Actions then
5244 goto Skip_Optimization;
5245 end if;
5247 Rewrite (N,
5248 Make_Expression_With_Actions (Loc,
5249 Expression => Relocate_Node (Expr),
5250 Actions => Actions));
5251 Analyze_And_Resolve (N, Typ);
5253 else
5254 Rewrite (N, Relocate_Node (Expr));
5255 end if;
5257 -- Note that the result is never static (legitimate cases of static
5258 -- if expressions were folded in Sem_Eval).
5260 Set_Is_Static_Expression (N, False);
5261 return;
5262 end if;
5264 <<Skip_Optimization>>
5266 -- If the type is limited or unconstrained, we expand as follows to
5267 -- avoid any possibility of improper copies.
5269 -- Note: it may be possible to avoid this special processing if the
5270 -- back end uses its own mechanisms for handling by-reference types ???
5272 -- type Ptr is access all Typ;
5273 -- Cnn : Ptr;
5274 -- if cond then
5275 -- <<then actions>>
5276 -- Cnn := then-expr'Unrestricted_Access;
5277 -- else
5278 -- <<else actions>>
5279 -- Cnn := else-expr'Unrestricted_Access;
5280 -- end if;
5282 -- and replace the if expression by a reference to Cnn.all.
5284 -- This special case can be skipped if the back end handles limited
5285 -- types properly and ensures that no incorrect copies are made.
5287 if Is_By_Reference_Type (Typ)
5288 and then not Back_End_Handles_Limited_Types
5289 then
5290 declare
5291 Flag_Id : Entity_Id;
5292 Ptr_Typ : Entity_Id;
5294 begin
5295 Flag_Id := Empty;
5297 -- At least one of the if expression dependent expressions uses a
5298 -- controlled function to provide the result. Create a status flag
5299 -- to signal the finalization machinery that Cnn needs special
5300 -- handling.
5302 if Is_Controlled_Function_Call (Thenx)
5303 or else
5304 Is_Controlled_Function_Call (Elsex)
5305 then
5306 Flag_Id := Make_Temporary (Loc, 'F');
5308 Insert_Action (N,
5309 Make_Object_Declaration (Loc,
5310 Defining_Identifier => Flag_Id,
5311 Object_Definition =>
5312 New_Reference_To (Standard_Boolean, Loc),
5313 Expression =>
5314 New_Reference_To (Standard_False, Loc)));
5315 end if;
5317 -- Generate:
5318 -- type Ann is access all Typ;
5320 Ptr_Typ := Make_Temporary (Loc, 'A');
5322 Insert_Action (N,
5323 Make_Full_Type_Declaration (Loc,
5324 Defining_Identifier => Ptr_Typ,
5325 Type_Definition =>
5326 Make_Access_To_Object_Definition (Loc,
5327 All_Present => True,
5328 Subtype_Indication => New_Reference_To (Typ, Loc))));
5330 -- Generate:
5331 -- Cnn : Ann;
5333 Cnn := Make_Temporary (Loc, 'C', N);
5334 Set_Ekind (Cnn, E_Variable);
5335 Set_Status_Flag_Or_Transient_Decl (Cnn, Flag_Id);
5337 Decl :=
5338 Make_Object_Declaration (Loc,
5339 Defining_Identifier => Cnn,
5340 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc));
5342 New_If :=
5343 Make_Implicit_If_Statement (N,
5344 Condition => Relocate_Node (Cond),
5345 Then_Statements =>
5346 Create_Alternative (Sloc (Thenx), Cnn, Flag_Id, Thenx),
5347 Else_Statements =>
5348 Create_Alternative (Sloc (Elsex), Cnn, Flag_Id, Elsex));
5350 New_N :=
5351 Make_Explicit_Dereference (Loc,
5352 Prefix => New_Occurrence_Of (Cnn, Loc));
5353 end;
5355 -- For other types, we only need to expand if there are other actions
5356 -- associated with either branch.
5358 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
5360 -- We have two approaches to handling this. If we are allowed to use
5361 -- N_Expression_With_Actions, then we can just wrap the actions into
5362 -- the appropriate expression.
5364 if Use_Expression_With_Actions then
5365 if Present (Then_Actions (N)) then
5366 Rewrite (Thenx,
5367 Make_Expression_With_Actions (Sloc (Thenx),
5368 Actions => Then_Actions (N),
5369 Expression => Relocate_Node (Thenx)));
5370 Set_Then_Actions (N, No_List);
5371 Analyze_And_Resolve (Thenx, Typ);
5372 end if;
5374 if Present (Else_Actions (N)) then
5375 Rewrite (Elsex,
5376 Make_Expression_With_Actions (Sloc (Elsex),
5377 Actions => Else_Actions (N),
5378 Expression => Relocate_Node (Elsex)));
5379 Set_Else_Actions (N, No_List);
5380 Analyze_And_Resolve (Elsex, Typ);
5381 end if;
5383 return;
5385 -- if we can't use N_Expression_With_Actions nodes, then we insert
5386 -- the following sequence of actions (using Insert_Actions):
5388 -- Cnn : typ;
5389 -- if cond then
5390 -- <<then actions>>
5391 -- Cnn := then-expr;
5392 -- else
5393 -- <<else actions>>
5394 -- Cnn := else-expr
5395 -- end if;
5397 -- and replace the if expression by a reference to Cnn
5399 else
5400 Cnn := Make_Temporary (Loc, 'C', N);
5402 Decl :=
5403 Make_Object_Declaration (Loc,
5404 Defining_Identifier => Cnn,
5405 Object_Definition => New_Occurrence_Of (Typ, Loc));
5407 New_If :=
5408 Make_Implicit_If_Statement (N,
5409 Condition => Relocate_Node (Cond),
5411 Then_Statements => New_List (
5412 Make_Assignment_Statement (Sloc (Thenx),
5413 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
5414 Expression => Relocate_Node (Thenx))),
5416 Else_Statements => New_List (
5417 Make_Assignment_Statement (Sloc (Elsex),
5418 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
5419 Expression => Relocate_Node (Elsex))));
5421 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
5422 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
5424 New_N := New_Occurrence_Of (Cnn, Loc);
5425 end if;
5427 -- If no actions then no expansion needed, gigi will handle it using
5428 -- the same approach as a C conditional expression.
5430 else
5431 return;
5432 end if;
5434 -- Fall through here for either the limited expansion, or the case of
5435 -- inserting actions for non-limited types. In both these cases, we must
5436 -- move the SLOC of the parent If statement to the newly created one and
5437 -- change it to the SLOC of the expression which, after expansion, will
5438 -- correspond to what is being evaluated.
5440 if Present (Parent (N))
5441 and then Nkind (Parent (N)) = N_If_Statement
5442 then
5443 Set_Sloc (New_If, Sloc (Parent (N)));
5444 Set_Sloc (Parent (N), Loc);
5445 end if;
5447 -- Make sure Then_Actions and Else_Actions are appropriately moved
5448 -- to the new if statement.
5450 if Present (Then_Actions (N)) then
5451 Insert_List_Before
5452 (First (Then_Statements (New_If)), Then_Actions (N));
5453 end if;
5455 if Present (Else_Actions (N)) then
5456 Insert_List_Before
5457 (First (Else_Statements (New_If)), Else_Actions (N));
5458 end if;
5460 Insert_Action (N, Decl);
5461 Insert_Action (N, New_If);
5462 Rewrite (N, New_N);
5463 Analyze_And_Resolve (N, Typ);
5464 end Expand_N_If_Expression;
5466 -----------------
5467 -- Expand_N_In --
5468 -----------------
5470 procedure Expand_N_In (N : Node_Id) is
5471 Loc : constant Source_Ptr := Sloc (N);
5472 Restyp : constant Entity_Id := Etype (N);
5473 Lop : constant Node_Id := Left_Opnd (N);
5474 Rop : constant Node_Id := Right_Opnd (N);
5475 Static : constant Boolean := Is_OK_Static_Expression (N);
5477 Ltyp : Entity_Id;
5478 Rtyp : Entity_Id;
5480 procedure Substitute_Valid_Check;
5481 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5482 -- test for the left operand being in range of its subtype.
5484 ----------------------------
5485 -- Substitute_Valid_Check --
5486 ----------------------------
5488 procedure Substitute_Valid_Check is
5489 begin
5490 Rewrite (N,
5491 Make_Attribute_Reference (Loc,
5492 Prefix => Relocate_Node (Lop),
5493 Attribute_Name => Name_Valid));
5495 Analyze_And_Resolve (N, Restyp);
5497 -- Give warning unless overflow checking is MINIMIZED or ELIMINATED,
5498 -- in which case, this usage makes sense, and in any case, we have
5499 -- actually eliminated the danger of optimization above.
5501 if Overflow_Check_Mode not in Minimized_Or_Eliminated then
5502 Error_Msg_N ("?explicit membership test may be optimized away", N);
5503 Error_Msg_N -- CODEFIX
5504 ("\?use ''Valid attribute instead", N);
5505 end if;
5507 return;
5508 end Substitute_Valid_Check;
5510 -- Start of processing for Expand_N_In
5512 begin
5513 -- If set membership case, expand with separate procedure
5515 if Present (Alternatives (N)) then
5516 Expand_Set_Membership (N);
5517 return;
5518 end if;
5520 -- Not set membership, proceed with expansion
5522 Ltyp := Etype (Left_Opnd (N));
5523 Rtyp := Etype (Right_Opnd (N));
5525 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
5526 -- type, then expand with a separate procedure. Note the use of the
5527 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5529 if Overflow_Check_Mode in Minimized_Or_Eliminated
5530 and then Is_Signed_Integer_Type (Ltyp)
5531 and then not No_Minimize_Eliminate (N)
5532 then
5533 Expand_Membership_Minimize_Eliminate_Overflow (N);
5534 return;
5535 end if;
5537 -- Check case of explicit test for an expression in range of its
5538 -- subtype. This is suspicious usage and we replace it with a 'Valid
5539 -- test and give a warning for scalar types.
5541 if Is_Scalar_Type (Ltyp)
5543 -- Only relevant for source comparisons
5545 and then Comes_From_Source (N)
5547 -- In floating-point this is a standard way to check for finite values
5548 -- and using 'Valid would typically be a pessimization.
5550 and then not Is_Floating_Point_Type (Ltyp)
5552 -- Don't give the message unless right operand is a type entity and
5553 -- the type of the left operand matches this type. Note that this
5554 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
5555 -- checks have changed the type of the left operand.
5557 and then Nkind (Rop) in N_Has_Entity
5558 and then Ltyp = Entity (Rop)
5560 -- Skip in VM mode, where we have no sense of invalid values. The
5561 -- warning still seems relevant, but not important enough to worry.
5563 and then VM_Target = No_VM
5565 -- Skip this for predicated types, where such expressions are a
5566 -- reasonable way of testing if something meets the predicate.
5568 and then not Present (Predicate_Function (Ltyp))
5569 then
5570 Substitute_Valid_Check;
5571 return;
5572 end if;
5574 -- Do validity check on operands
5576 if Validity_Checks_On and Validity_Check_Operands then
5577 Ensure_Valid (Left_Opnd (N));
5578 Validity_Check_Range (Right_Opnd (N));
5579 end if;
5581 -- Case of explicit range
5583 if Nkind (Rop) = N_Range then
5584 declare
5585 Lo : constant Node_Id := Low_Bound (Rop);
5586 Hi : constant Node_Id := High_Bound (Rop);
5588 Lo_Orig : constant Node_Id := Original_Node (Lo);
5589 Hi_Orig : constant Node_Id := Original_Node (Hi);
5591 Lcheck : Compare_Result;
5592 Ucheck : Compare_Result;
5594 Warn1 : constant Boolean :=
5595 Constant_Condition_Warnings
5596 and then Comes_From_Source (N)
5597 and then not In_Instance;
5598 -- This must be true for any of the optimization warnings, we
5599 -- clearly want to give them only for source with the flag on. We
5600 -- also skip these warnings in an instance since it may be the
5601 -- case that different instantiations have different ranges.
5603 Warn2 : constant Boolean :=
5604 Warn1
5605 and then Nkind (Original_Node (Rop)) = N_Range
5606 and then Is_Integer_Type (Etype (Lo));
5607 -- For the case where only one bound warning is elided, we also
5608 -- insist on an explicit range and an integer type. The reason is
5609 -- that the use of enumeration ranges including an end point is
5610 -- common, as is the use of a subtype name, one of whose bounds is
5611 -- the same as the type of the expression.
5613 begin
5614 -- If test is explicit x'First .. x'Last, replace by valid check
5616 -- Could use some individual comments for this complex test ???
5618 if Is_Scalar_Type (Ltyp)
5620 -- And left operand is X'First where X matches left operand
5621 -- type (this eliminates cases of type mismatch, including
5622 -- the cases where ELIMINATED/MINIMIZED mode has changed the
5623 -- type of the left operand.
5625 and then Nkind (Lo_Orig) = N_Attribute_Reference
5626 and then Attribute_Name (Lo_Orig) = Name_First
5627 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
5628 and then Entity (Prefix (Lo_Orig)) = Ltyp
5630 -- Same tests for right operand
5632 and then Nkind (Hi_Orig) = N_Attribute_Reference
5633 and then Attribute_Name (Hi_Orig) = Name_Last
5634 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
5635 and then Entity (Prefix (Hi_Orig)) = Ltyp
5637 -- Relevant only for source cases
5639 and then Comes_From_Source (N)
5641 -- Omit for VM cases, where we don't have invalid values
5643 and then VM_Target = No_VM
5644 then
5645 Substitute_Valid_Check;
5646 goto Leave;
5647 end if;
5649 -- If bounds of type are known at compile time, and the end points
5650 -- are known at compile time and identical, this is another case
5651 -- for substituting a valid test. We only do this for discrete
5652 -- types, since it won't arise in practice for float types.
5654 if Comes_From_Source (N)
5655 and then Is_Discrete_Type (Ltyp)
5656 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
5657 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
5658 and then Compile_Time_Known_Value (Lo)
5659 and then Compile_Time_Known_Value (Hi)
5660 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
5661 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
5663 -- Kill warnings in instances, since they may be cases where we
5664 -- have a test in the generic that makes sense with some types
5665 -- and not with other types.
5667 and then not In_Instance
5668 then
5669 Substitute_Valid_Check;
5670 goto Leave;
5671 end if;
5673 -- If we have an explicit range, do a bit of optimization based on
5674 -- range analysis (we may be able to kill one or both checks).
5676 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
5677 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
5679 -- If either check is known to fail, replace result by False since
5680 -- the other check does not matter. Preserve the static flag for
5681 -- legality checks, because we are constant-folding beyond RM 4.9.
5683 if Lcheck = LT or else Ucheck = GT then
5684 if Warn1 then
5685 Error_Msg_N ("?range test optimized away", N);
5686 Error_Msg_N ("\?value is known to be out of range", N);
5687 end if;
5689 Rewrite (N, New_Reference_To (Standard_False, Loc));
5690 Analyze_And_Resolve (N, Restyp);
5691 Set_Is_Static_Expression (N, Static);
5692 goto Leave;
5694 -- If both checks are known to succeed, replace result by True,
5695 -- since we know we are in range.
5697 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
5698 if Warn1 then
5699 Error_Msg_N ("?range test optimized away", N);
5700 Error_Msg_N ("\?value is known to be in range", N);
5701 end if;
5703 Rewrite (N, New_Reference_To (Standard_True, Loc));
5704 Analyze_And_Resolve (N, Restyp);
5705 Set_Is_Static_Expression (N, Static);
5706 goto Leave;
5708 -- If lower bound check succeeds and upper bound check is not
5709 -- known to succeed or fail, then replace the range check with
5710 -- a comparison against the upper bound.
5712 elsif Lcheck in Compare_GE then
5713 if Warn2 and then not In_Instance then
5714 Error_Msg_N ("?lower bound test optimized away", Lo);
5715 Error_Msg_N ("\?value is known to be in range", Lo);
5716 end if;
5718 Rewrite (N,
5719 Make_Op_Le (Loc,
5720 Left_Opnd => Lop,
5721 Right_Opnd => High_Bound (Rop)));
5722 Analyze_And_Resolve (N, Restyp);
5723 goto Leave;
5725 -- If upper bound check succeeds and lower bound check is not
5726 -- known to succeed or fail, then replace the range check with
5727 -- a comparison against the lower bound.
5729 elsif Ucheck in Compare_LE then
5730 if Warn2 and then not In_Instance then
5731 Error_Msg_N ("?upper bound test optimized away", Hi);
5732 Error_Msg_N ("\?value is known to be in range", Hi);
5733 end if;
5735 Rewrite (N,
5736 Make_Op_Ge (Loc,
5737 Left_Opnd => Lop,
5738 Right_Opnd => Low_Bound (Rop)));
5739 Analyze_And_Resolve (N, Restyp);
5740 goto Leave;
5741 end if;
5743 -- We couldn't optimize away the range check, but there is one
5744 -- more issue. If we are checking constant conditionals, then we
5745 -- see if we can determine the outcome assuming everything is
5746 -- valid, and if so give an appropriate warning.
5748 if Warn1 and then not Assume_No_Invalid_Values then
5749 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
5750 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
5752 -- Result is out of range for valid value
5754 if Lcheck = LT or else Ucheck = GT then
5755 Error_Msg_N
5756 ("?value can only be in range if it is invalid", N);
5758 -- Result is in range for valid value
5760 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
5761 Error_Msg_N
5762 ("?value can only be out of range if it is invalid", N);
5764 -- Lower bound check succeeds if value is valid
5766 elsif Warn2 and then Lcheck in Compare_GE then
5767 Error_Msg_N
5768 ("?lower bound check only fails if it is invalid", Lo);
5770 -- Upper bound check succeeds if value is valid
5772 elsif Warn2 and then Ucheck in Compare_LE then
5773 Error_Msg_N
5774 ("?upper bound check only fails for invalid values", Hi);
5775 end if;
5776 end if;
5777 end;
5779 -- For all other cases of an explicit range, nothing to be done
5781 goto Leave;
5783 -- Here right operand is a subtype mark
5785 else
5786 declare
5787 Typ : Entity_Id := Etype (Rop);
5788 Is_Acc : constant Boolean := Is_Access_Type (Typ);
5789 Cond : Node_Id := Empty;
5790 New_N : Node_Id;
5791 Obj : Node_Id := Lop;
5792 SCIL_Node : Node_Id;
5794 begin
5795 Remove_Side_Effects (Obj);
5797 -- For tagged type, do tagged membership operation
5799 if Is_Tagged_Type (Typ) then
5801 -- No expansion will be performed when VM_Target, as the VM
5802 -- back-ends will handle the membership tests directly (tags
5803 -- are not explicitly represented in Java objects, so the
5804 -- normal tagged membership expansion is not what we want).
5806 if Tagged_Type_Expansion then
5807 Tagged_Membership (N, SCIL_Node, New_N);
5808 Rewrite (N, New_N);
5809 Analyze_And_Resolve (N, Restyp);
5811 -- Update decoration of relocated node referenced by the
5812 -- SCIL node.
5814 if Generate_SCIL and then Present (SCIL_Node) then
5815 Set_SCIL_Node (N, SCIL_Node);
5816 end if;
5817 end if;
5819 goto Leave;
5821 -- If type is scalar type, rewrite as x in t'First .. t'Last.
5822 -- This reason we do this is that the bounds may have the wrong
5823 -- type if they come from the original type definition. Also this
5824 -- way we get all the processing above for an explicit range.
5826 -- Don't do this for predicated types, since in this case we
5827 -- want to check the predicate!
5829 elsif Is_Scalar_Type (Typ) then
5830 if No (Predicate_Function (Typ)) then
5831 Rewrite (Rop,
5832 Make_Range (Loc,
5833 Low_Bound =>
5834 Make_Attribute_Reference (Loc,
5835 Attribute_Name => Name_First,
5836 Prefix => New_Reference_To (Typ, Loc)),
5838 High_Bound =>
5839 Make_Attribute_Reference (Loc,
5840 Attribute_Name => Name_Last,
5841 Prefix => New_Reference_To (Typ, Loc))));
5842 Analyze_And_Resolve (N, Restyp);
5843 end if;
5845 goto Leave;
5847 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5848 -- a membership test if the subtype mark denotes a constrained
5849 -- Unchecked_Union subtype and the expression lacks inferable
5850 -- discriminants.
5852 elsif Is_Unchecked_Union (Base_Type (Typ))
5853 and then Is_Constrained (Typ)
5854 and then not Has_Inferable_Discriminants (Lop)
5855 then
5856 Insert_Action (N,
5857 Make_Raise_Program_Error (Loc,
5858 Reason => PE_Unchecked_Union_Restriction));
5860 -- Prevent Gigi from generating incorrect code by rewriting the
5861 -- test as False. What is this undocumented thing about ???
5863 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5864 goto Leave;
5865 end if;
5867 -- Here we have a non-scalar type
5869 if Is_Acc then
5870 Typ := Designated_Type (Typ);
5871 end if;
5873 if not Is_Constrained (Typ) then
5874 Rewrite (N, New_Reference_To (Standard_True, Loc));
5875 Analyze_And_Resolve (N, Restyp);
5877 -- For the constrained array case, we have to check the subscripts
5878 -- for an exact match if the lengths are non-zero (the lengths
5879 -- must match in any case).
5881 elsif Is_Array_Type (Typ) then
5882 Check_Subscripts : declare
5883 function Build_Attribute_Reference
5884 (E : Node_Id;
5885 Nam : Name_Id;
5886 Dim : Nat) return Node_Id;
5887 -- Build attribute reference E'Nam (Dim)
5889 -------------------------------
5890 -- Build_Attribute_Reference --
5891 -------------------------------
5893 function Build_Attribute_Reference
5894 (E : Node_Id;
5895 Nam : Name_Id;
5896 Dim : Nat) return Node_Id
5898 begin
5899 return
5900 Make_Attribute_Reference (Loc,
5901 Prefix => E,
5902 Attribute_Name => Nam,
5903 Expressions => New_List (
5904 Make_Integer_Literal (Loc, Dim)));
5905 end Build_Attribute_Reference;
5907 -- Start of processing for Check_Subscripts
5909 begin
5910 for J in 1 .. Number_Dimensions (Typ) loop
5911 Evolve_And_Then (Cond,
5912 Make_Op_Eq (Loc,
5913 Left_Opnd =>
5914 Build_Attribute_Reference
5915 (Duplicate_Subexpr_No_Checks (Obj),
5916 Name_First, J),
5917 Right_Opnd =>
5918 Build_Attribute_Reference
5919 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
5921 Evolve_And_Then (Cond,
5922 Make_Op_Eq (Loc,
5923 Left_Opnd =>
5924 Build_Attribute_Reference
5925 (Duplicate_Subexpr_No_Checks (Obj),
5926 Name_Last, J),
5927 Right_Opnd =>
5928 Build_Attribute_Reference
5929 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
5930 end loop;
5932 if Is_Acc then
5933 Cond :=
5934 Make_Or_Else (Loc,
5935 Left_Opnd =>
5936 Make_Op_Eq (Loc,
5937 Left_Opnd => Obj,
5938 Right_Opnd => Make_Null (Loc)),
5939 Right_Opnd => Cond);
5940 end if;
5942 Rewrite (N, Cond);
5943 Analyze_And_Resolve (N, Restyp);
5944 end Check_Subscripts;
5946 -- These are the cases where constraint checks may be required,
5947 -- e.g. records with possible discriminants
5949 else
5950 -- Expand the test into a series of discriminant comparisons.
5951 -- The expression that is built is the negation of the one that
5952 -- is used for checking discriminant constraints.
5954 Obj := Relocate_Node (Left_Opnd (N));
5956 if Has_Discriminants (Typ) then
5957 Cond := Make_Op_Not (Loc,
5958 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
5960 if Is_Acc then
5961 Cond := Make_Or_Else (Loc,
5962 Left_Opnd =>
5963 Make_Op_Eq (Loc,
5964 Left_Opnd => Obj,
5965 Right_Opnd => Make_Null (Loc)),
5966 Right_Opnd => Cond);
5967 end if;
5969 else
5970 Cond := New_Occurrence_Of (Standard_True, Loc);
5971 end if;
5973 Rewrite (N, Cond);
5974 Analyze_And_Resolve (N, Restyp);
5975 end if;
5977 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
5978 -- expression of an anonymous access type. This can involve an
5979 -- accessibility test and a tagged type membership test in the
5980 -- case of tagged designated types.
5982 if Ada_Version >= Ada_2012
5983 and then Is_Acc
5984 and then Ekind (Ltyp) = E_Anonymous_Access_Type
5985 then
5986 declare
5987 Expr_Entity : Entity_Id := Empty;
5988 New_N : Node_Id;
5989 Param_Level : Node_Id;
5990 Type_Level : Node_Id;
5992 begin
5993 if Is_Entity_Name (Lop) then
5994 Expr_Entity := Param_Entity (Lop);
5996 if not Present (Expr_Entity) then
5997 Expr_Entity := Entity (Lop);
5998 end if;
5999 end if;
6001 -- If a conversion of the anonymous access value to the
6002 -- tested type would be illegal, then the result is False.
6004 if not Valid_Conversion
6005 (Lop, Rtyp, Lop, Report_Errs => False)
6006 then
6007 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
6008 Analyze_And_Resolve (N, Restyp);
6010 -- Apply an accessibility check if the access object has an
6011 -- associated access level and when the level of the type is
6012 -- less deep than the level of the access parameter. This
6013 -- only occur for access parameters and stand-alone objects
6014 -- of an anonymous access type.
6016 else
6017 if Present (Expr_Entity)
6018 and then
6019 Present
6020 (Effective_Extra_Accessibility (Expr_Entity))
6021 and then UI_Gt (Object_Access_Level (Lop),
6022 Type_Access_Level (Rtyp))
6023 then
6024 Param_Level :=
6025 New_Occurrence_Of
6026 (Effective_Extra_Accessibility (Expr_Entity), Loc);
6028 Type_Level :=
6029 Make_Integer_Literal (Loc, Type_Access_Level (Rtyp));
6031 -- Return True only if the accessibility level of the
6032 -- expression entity is not deeper than the level of
6033 -- the tested access type.
6035 Rewrite (N,
6036 Make_And_Then (Loc,
6037 Left_Opnd => Relocate_Node (N),
6038 Right_Opnd => Make_Op_Le (Loc,
6039 Left_Opnd => Param_Level,
6040 Right_Opnd => Type_Level)));
6042 Analyze_And_Resolve (N);
6043 end if;
6045 -- If the designated type is tagged, do tagged membership
6046 -- operation.
6048 -- *** NOTE: we have to check not null before doing the
6049 -- tagged membership test (but maybe that can be done
6050 -- inside Tagged_Membership?).
6052 if Is_Tagged_Type (Typ) then
6053 Rewrite (N,
6054 Make_And_Then (Loc,
6055 Left_Opnd => Relocate_Node (N),
6056 Right_Opnd =>
6057 Make_Op_Ne (Loc,
6058 Left_Opnd => Obj,
6059 Right_Opnd => Make_Null (Loc))));
6061 -- No expansion will be performed when VM_Target, as
6062 -- the VM back-ends will handle the membership tests
6063 -- directly (tags are not explicitly represented in
6064 -- Java objects, so the normal tagged membership
6065 -- expansion is not what we want).
6067 if Tagged_Type_Expansion then
6069 -- Note that we have to pass Original_Node, because
6070 -- the membership test might already have been
6071 -- rewritten by earlier parts of membership test.
6073 Tagged_Membership
6074 (Original_Node (N), SCIL_Node, New_N);
6076 -- Update decoration of relocated node referenced
6077 -- by the SCIL node.
6079 if Generate_SCIL and then Present (SCIL_Node) then
6080 Set_SCIL_Node (New_N, SCIL_Node);
6081 end if;
6083 Rewrite (N,
6084 Make_And_Then (Loc,
6085 Left_Opnd => Relocate_Node (N),
6086 Right_Opnd => New_N));
6088 Analyze_And_Resolve (N, Restyp);
6089 end if;
6090 end if;
6091 end if;
6092 end;
6093 end if;
6094 end;
6095 end if;
6097 -- At this point, we have done the processing required for the basic
6098 -- membership test, but not yet dealt with the predicate.
6100 <<Leave>>
6102 -- If a predicate is present, then we do the predicate test, but we
6103 -- most certainly want to omit this if we are within the predicate
6104 -- function itself, since otherwise we have an infinite recursion!
6105 -- The check should also not be emitted when testing against a range
6106 -- (the check is only done when the right operand is a subtype; see
6107 -- RM12-4.5.2 (28.1/3-30/3)).
6109 declare
6110 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
6112 begin
6113 if Present (PFunc)
6114 and then Current_Scope /= PFunc
6115 and then Nkind (Rop) /= N_Range
6116 then
6117 Rewrite (N,
6118 Make_And_Then (Loc,
6119 Left_Opnd => Relocate_Node (N),
6120 Right_Opnd => Make_Predicate_Call (Rtyp, Lop)));
6122 -- Analyze new expression, mark left operand as analyzed to
6123 -- avoid infinite recursion adding predicate calls. Similarly,
6124 -- suppress further range checks on the call.
6126 Set_Analyzed (Left_Opnd (N));
6127 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6129 -- All done, skip attempt at compile time determination of result
6131 return;
6132 end if;
6133 end;
6134 end Expand_N_In;
6136 --------------------------------
6137 -- Expand_N_Indexed_Component --
6138 --------------------------------
6140 procedure Expand_N_Indexed_Component (N : Node_Id) is
6141 Loc : constant Source_Ptr := Sloc (N);
6142 Typ : constant Entity_Id := Etype (N);
6143 P : constant Node_Id := Prefix (N);
6144 T : constant Entity_Id := Etype (P);
6145 Atp : Entity_Id;
6147 begin
6148 -- A special optimization, if we have an indexed component that is
6149 -- selecting from a slice, then we can eliminate the slice, since, for
6150 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6151 -- the range check required by the slice. The range check for the slice
6152 -- itself has already been generated. The range check for the
6153 -- subscripting operation is ensured by converting the subject to
6154 -- the subtype of the slice.
6156 -- This optimization not only generates better code, avoiding slice
6157 -- messing especially in the packed case, but more importantly bypasses
6158 -- some problems in handling this peculiar case, for example, the issue
6159 -- of dealing specially with object renamings.
6161 if Nkind (P) = N_Slice then
6162 Rewrite (N,
6163 Make_Indexed_Component (Loc,
6164 Prefix => Prefix (P),
6165 Expressions => New_List (
6166 Convert_To
6167 (Etype (First_Index (Etype (P))),
6168 First (Expressions (N))))));
6169 Analyze_And_Resolve (N, Typ);
6170 return;
6171 end if;
6173 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6174 -- function, then additional actuals must be passed.
6176 if Ada_Version >= Ada_2005
6177 and then Is_Build_In_Place_Function_Call (P)
6178 then
6179 Make_Build_In_Place_Call_In_Anonymous_Context (P);
6180 end if;
6182 -- If the prefix is an access type, then we unconditionally rewrite if
6183 -- as an explicit dereference. This simplifies processing for several
6184 -- cases, including packed array cases and certain cases in which checks
6185 -- must be generated. We used to try to do this only when it was
6186 -- necessary, but it cleans up the code to do it all the time.
6188 if Is_Access_Type (T) then
6189 Insert_Explicit_Dereference (P);
6190 Analyze_And_Resolve (P, Designated_Type (T));
6191 Atp := Designated_Type (T);
6192 else
6193 Atp := T;
6194 end if;
6196 -- Generate index and validity checks
6198 Generate_Index_Checks (N);
6200 if Validity_Checks_On and then Validity_Check_Subscripts then
6201 Apply_Subscript_Validity_Checks (N);
6202 end if;
6204 -- If selecting from an array with atomic components, and atomic sync
6205 -- is not suppressed for this array type, set atomic sync flag.
6207 if (Has_Atomic_Components (Atp)
6208 and then not Atomic_Synchronization_Disabled (Atp))
6209 or else (Is_Atomic (Typ)
6210 and then not Atomic_Synchronization_Disabled (Typ))
6211 then
6212 Activate_Atomic_Synchronization (N);
6213 end if;
6215 -- All done for the non-packed case
6217 if not Is_Packed (Etype (Prefix (N))) then
6218 return;
6219 end if;
6221 -- For packed arrays that are not bit-packed (i.e. the case of an array
6222 -- with one or more index types with a non-contiguous enumeration type),
6223 -- we can always use the normal packed element get circuit.
6225 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
6226 Expand_Packed_Element_Reference (N);
6227 return;
6228 end if;
6230 -- For a reference to a component of a bit packed array, we have to
6231 -- convert it to a reference to the corresponding Packed_Array_Type.
6232 -- We only want to do this for simple references, and not for:
6234 -- Left side of assignment, or prefix of left side of assignment, or
6235 -- prefix of the prefix, to handle packed arrays of packed arrays,
6236 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6238 -- Renaming objects in renaming associations
6239 -- This case is handled when a use of the renamed variable occurs
6241 -- Actual parameters for a procedure call
6242 -- This case is handled in Exp_Ch6.Expand_Actuals
6244 -- The second expression in a 'Read attribute reference
6246 -- The prefix of an address or bit or size attribute reference
6248 -- The following circuit detects these exceptions
6250 declare
6251 Child : Node_Id := N;
6252 Parnt : Node_Id := Parent (N);
6254 begin
6255 loop
6256 if Nkind (Parnt) = N_Unchecked_Expression then
6257 null;
6259 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
6260 N_Procedure_Call_Statement)
6261 or else (Nkind (Parnt) = N_Parameter_Association
6262 and then
6263 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
6264 then
6265 return;
6267 elsif Nkind (Parnt) = N_Attribute_Reference
6268 and then (Attribute_Name (Parnt) = Name_Address
6269 or else
6270 Attribute_Name (Parnt) = Name_Bit
6271 or else
6272 Attribute_Name (Parnt) = Name_Size)
6273 and then Prefix (Parnt) = Child
6274 then
6275 return;
6277 elsif Nkind (Parnt) = N_Assignment_Statement
6278 and then Name (Parnt) = Child
6279 then
6280 return;
6282 -- If the expression is an index of an indexed component, it must
6283 -- be expanded regardless of context.
6285 elsif Nkind (Parnt) = N_Indexed_Component
6286 and then Child /= Prefix (Parnt)
6287 then
6288 Expand_Packed_Element_Reference (N);
6289 return;
6291 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
6292 and then Name (Parent (Parnt)) = Parnt
6293 then
6294 return;
6296 elsif Nkind (Parnt) = N_Attribute_Reference
6297 and then Attribute_Name (Parnt) = Name_Read
6298 and then Next (First (Expressions (Parnt))) = Child
6299 then
6300 return;
6302 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
6303 and then Prefix (Parnt) = Child
6304 then
6305 null;
6307 else
6308 Expand_Packed_Element_Reference (N);
6309 return;
6310 end if;
6312 -- Keep looking up tree for unchecked expression, or if we are the
6313 -- prefix of a possible assignment left side.
6315 Child := Parnt;
6316 Parnt := Parent (Child);
6317 end loop;
6318 end;
6319 end Expand_N_Indexed_Component;
6321 ---------------------
6322 -- Expand_N_Not_In --
6323 ---------------------
6325 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6326 -- can be done. This avoids needing to duplicate this expansion code.
6328 procedure Expand_N_Not_In (N : Node_Id) is
6329 Loc : constant Source_Ptr := Sloc (N);
6330 Typ : constant Entity_Id := Etype (N);
6331 Cfs : constant Boolean := Comes_From_Source (N);
6333 begin
6334 Rewrite (N,
6335 Make_Op_Not (Loc,
6336 Right_Opnd =>
6337 Make_In (Loc,
6338 Left_Opnd => Left_Opnd (N),
6339 Right_Opnd => Right_Opnd (N))));
6341 -- If this is a set membership, preserve list of alternatives
6343 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
6345 -- We want this to appear as coming from source if original does (see
6346 -- transformations in Expand_N_In).
6348 Set_Comes_From_Source (N, Cfs);
6349 Set_Comes_From_Source (Right_Opnd (N), Cfs);
6351 -- Now analyze transformed node
6353 Analyze_And_Resolve (N, Typ);
6354 end Expand_N_Not_In;
6356 -------------------
6357 -- Expand_N_Null --
6358 -------------------
6360 -- The only replacement required is for the case of a null of a type that
6361 -- is an access to protected subprogram, or a subtype thereof. We represent
6362 -- such access values as a record, and so we must replace the occurrence of
6363 -- null by the equivalent record (with a null address and a null pointer in
6364 -- it), so that the backend creates the proper value.
6366 procedure Expand_N_Null (N : Node_Id) is
6367 Loc : constant Source_Ptr := Sloc (N);
6368 Typ : constant Entity_Id := Base_Type (Etype (N));
6369 Agg : Node_Id;
6371 begin
6372 if Is_Access_Protected_Subprogram_Type (Typ) then
6373 Agg :=
6374 Make_Aggregate (Loc,
6375 Expressions => New_List (
6376 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
6377 Make_Null (Loc)));
6379 Rewrite (N, Agg);
6380 Analyze_And_Resolve (N, Equivalent_Type (Typ));
6382 -- For subsequent semantic analysis, the node must retain its type.
6383 -- Gigi in any case replaces this type by the corresponding record
6384 -- type before processing the node.
6386 Set_Etype (N, Typ);
6387 end if;
6389 exception
6390 when RE_Not_Available =>
6391 return;
6392 end Expand_N_Null;
6394 ---------------------
6395 -- Expand_N_Op_Abs --
6396 ---------------------
6398 procedure Expand_N_Op_Abs (N : Node_Id) is
6399 Loc : constant Source_Ptr := Sloc (N);
6400 Expr : constant Node_Id := Right_Opnd (N);
6402 begin
6403 Unary_Op_Validity_Checks (N);
6405 -- Check for MINIMIZED/ELIMINATED overflow mode
6407 if Minimized_Eliminated_Overflow_Check (N) then
6408 Apply_Arithmetic_Overflow_Check (N);
6409 return;
6410 end if;
6412 -- Deal with software overflow checking
6414 if not Backend_Overflow_Checks_On_Target
6415 and then Is_Signed_Integer_Type (Etype (N))
6416 and then Do_Overflow_Check (N)
6417 then
6418 -- The only case to worry about is when the argument is equal to the
6419 -- largest negative number, so what we do is to insert the check:
6421 -- [constraint_error when Expr = typ'Base'First]
6423 -- with the usual Duplicate_Subexpr use coding for expr
6425 Insert_Action (N,
6426 Make_Raise_Constraint_Error (Loc,
6427 Condition =>
6428 Make_Op_Eq (Loc,
6429 Left_Opnd => Duplicate_Subexpr (Expr),
6430 Right_Opnd =>
6431 Make_Attribute_Reference (Loc,
6432 Prefix =>
6433 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
6434 Attribute_Name => Name_First)),
6435 Reason => CE_Overflow_Check_Failed));
6436 end if;
6438 -- Vax floating-point types case
6440 if Vax_Float (Etype (N)) then
6441 Expand_Vax_Arith (N);
6442 end if;
6443 end Expand_N_Op_Abs;
6445 ---------------------
6446 -- Expand_N_Op_Add --
6447 ---------------------
6449 procedure Expand_N_Op_Add (N : Node_Id) is
6450 Typ : constant Entity_Id := Etype (N);
6452 begin
6453 Binary_Op_Validity_Checks (N);
6455 -- Check for MINIMIZED/ELIMINATED overflow mode
6457 if Minimized_Eliminated_Overflow_Check (N) then
6458 Apply_Arithmetic_Overflow_Check (N);
6459 return;
6460 end if;
6462 -- N + 0 = 0 + N = N for integer types
6464 if Is_Integer_Type (Typ) then
6465 if Compile_Time_Known_Value (Right_Opnd (N))
6466 and then Expr_Value (Right_Opnd (N)) = Uint_0
6467 then
6468 Rewrite (N, Left_Opnd (N));
6469 return;
6471 elsif Compile_Time_Known_Value (Left_Opnd (N))
6472 and then Expr_Value (Left_Opnd (N)) = Uint_0
6473 then
6474 Rewrite (N, Right_Opnd (N));
6475 return;
6476 end if;
6477 end if;
6479 -- Arithmetic overflow checks for signed integer/fixed point types
6481 if Is_Signed_Integer_Type (Typ)
6482 or else Is_Fixed_Point_Type (Typ)
6483 then
6484 Apply_Arithmetic_Overflow_Check (N);
6485 return;
6487 -- Vax floating-point types case
6489 elsif Vax_Float (Typ) then
6490 Expand_Vax_Arith (N);
6491 end if;
6492 end Expand_N_Op_Add;
6494 ---------------------
6495 -- Expand_N_Op_And --
6496 ---------------------
6498 procedure Expand_N_Op_And (N : Node_Id) is
6499 Typ : constant Entity_Id := Etype (N);
6501 begin
6502 Binary_Op_Validity_Checks (N);
6504 if Is_Array_Type (Etype (N)) then
6505 Expand_Boolean_Operator (N);
6507 elsif Is_Boolean_Type (Etype (N)) then
6508 Adjust_Condition (Left_Opnd (N));
6509 Adjust_Condition (Right_Opnd (N));
6510 Set_Etype (N, Standard_Boolean);
6511 Adjust_Result_Type (N, Typ);
6513 elsif Is_Intrinsic_Subprogram (Entity (N)) then
6514 Expand_Intrinsic_Call (N, Entity (N));
6516 end if;
6517 end Expand_N_Op_And;
6519 ------------------------
6520 -- Expand_N_Op_Concat --
6521 ------------------------
6523 procedure Expand_N_Op_Concat (N : Node_Id) is
6524 Opnds : List_Id;
6525 -- List of operands to be concatenated
6527 Cnode : Node_Id;
6528 -- Node which is to be replaced by the result of concatenating the nodes
6529 -- in the list Opnds.
6531 begin
6532 -- Ensure validity of both operands
6534 Binary_Op_Validity_Checks (N);
6536 -- If we are the left operand of a concatenation higher up the tree,
6537 -- then do nothing for now, since we want to deal with a series of
6538 -- concatenations as a unit.
6540 if Nkind (Parent (N)) = N_Op_Concat
6541 and then N = Left_Opnd (Parent (N))
6542 then
6543 return;
6544 end if;
6546 -- We get here with a concatenation whose left operand may be a
6547 -- concatenation itself with a consistent type. We need to process
6548 -- these concatenation operands from left to right, which means
6549 -- from the deepest node in the tree to the highest node.
6551 Cnode := N;
6552 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
6553 Cnode := Left_Opnd (Cnode);
6554 end loop;
6556 -- Now Cnode is the deepest concatenation, and its parents are the
6557 -- concatenation nodes above, so now we process bottom up, doing the
6558 -- operations. We gather a string that is as long as possible up to five
6559 -- operands.
6561 -- The outer loop runs more than once if more than one concatenation
6562 -- type is involved.
6564 Outer : loop
6565 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
6566 Set_Parent (Opnds, N);
6568 -- The inner loop gathers concatenation operands
6570 Inner : while Cnode /= N
6571 and then Base_Type (Etype (Cnode)) =
6572 Base_Type (Etype (Parent (Cnode)))
6573 loop
6574 Cnode := Parent (Cnode);
6575 Append (Right_Opnd (Cnode), Opnds);
6576 end loop Inner;
6578 Expand_Concatenate (Cnode, Opnds);
6580 exit Outer when Cnode = N;
6581 Cnode := Parent (Cnode);
6582 end loop Outer;
6583 end Expand_N_Op_Concat;
6585 ------------------------
6586 -- Expand_N_Op_Divide --
6587 ------------------------
6589 procedure Expand_N_Op_Divide (N : Node_Id) is
6590 Loc : constant Source_Ptr := Sloc (N);
6591 Lopnd : constant Node_Id := Left_Opnd (N);
6592 Ropnd : constant Node_Id := Right_Opnd (N);
6593 Ltyp : constant Entity_Id := Etype (Lopnd);
6594 Rtyp : constant Entity_Id := Etype (Ropnd);
6595 Typ : Entity_Id := Etype (N);
6596 Rknow : constant Boolean := Is_Integer_Type (Typ)
6597 and then
6598 Compile_Time_Known_Value (Ropnd);
6599 Rval : Uint;
6601 begin
6602 Binary_Op_Validity_Checks (N);
6604 -- Check for MINIMIZED/ELIMINATED overflow mode
6606 if Minimized_Eliminated_Overflow_Check (N) then
6607 Apply_Arithmetic_Overflow_Check (N);
6608 return;
6609 end if;
6611 -- Otherwise proceed with expansion of division
6613 if Rknow then
6614 Rval := Expr_Value (Ropnd);
6615 end if;
6617 -- N / 1 = N for integer types
6619 if Rknow and then Rval = Uint_1 then
6620 Rewrite (N, Lopnd);
6621 return;
6622 end if;
6624 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
6625 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6626 -- operand is an unsigned integer, as required for this to work.
6628 if Nkind (Ropnd) = N_Op_Expon
6629 and then Is_Power_Of_2_For_Shift (Ropnd)
6631 -- We cannot do this transformation in configurable run time mode if we
6632 -- have 64-bit integers and long shifts are not available.
6634 and then
6635 (Esize (Ltyp) <= 32
6636 or else Support_Long_Shifts_On_Target)
6637 then
6638 Rewrite (N,
6639 Make_Op_Shift_Right (Loc,
6640 Left_Opnd => Lopnd,
6641 Right_Opnd =>
6642 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
6643 Analyze_And_Resolve (N, Typ);
6644 return;
6645 end if;
6647 -- Do required fixup of universal fixed operation
6649 if Typ = Universal_Fixed then
6650 Fixup_Universal_Fixed_Operation (N);
6651 Typ := Etype (N);
6652 end if;
6654 -- Divisions with fixed-point results
6656 if Is_Fixed_Point_Type (Typ) then
6658 -- No special processing if Treat_Fixed_As_Integer is set, since
6659 -- from a semantic point of view such operations are simply integer
6660 -- operations and will be treated that way.
6662 if not Treat_Fixed_As_Integer (N) then
6663 if Is_Integer_Type (Rtyp) then
6664 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
6665 else
6666 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
6667 end if;
6668 end if;
6670 -- Other cases of division of fixed-point operands. Again we exclude the
6671 -- case where Treat_Fixed_As_Integer is set.
6673 elsif (Is_Fixed_Point_Type (Ltyp) or else
6674 Is_Fixed_Point_Type (Rtyp))
6675 and then not Treat_Fixed_As_Integer (N)
6676 then
6677 if Is_Integer_Type (Typ) then
6678 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
6679 else
6680 pragma Assert (Is_Floating_Point_Type (Typ));
6681 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
6682 end if;
6684 -- Mixed-mode operations can appear in a non-static universal context,
6685 -- in which case the integer argument must be converted explicitly.
6687 elsif Typ = Universal_Real
6688 and then Is_Integer_Type (Rtyp)
6689 then
6690 Rewrite (Ropnd,
6691 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
6693 Analyze_And_Resolve (Ropnd, Universal_Real);
6695 elsif Typ = Universal_Real
6696 and then Is_Integer_Type (Ltyp)
6697 then
6698 Rewrite (Lopnd,
6699 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
6701 Analyze_And_Resolve (Lopnd, Universal_Real);
6703 -- Non-fixed point cases, do integer zero divide and overflow checks
6705 elsif Is_Integer_Type (Typ) then
6706 Apply_Divide_Checks (N);
6708 -- Deal with Vax_Float
6710 elsif Vax_Float (Typ) then
6711 Expand_Vax_Arith (N);
6712 return;
6713 end if;
6714 end Expand_N_Op_Divide;
6716 --------------------
6717 -- Expand_N_Op_Eq --
6718 --------------------
6720 procedure Expand_N_Op_Eq (N : Node_Id) is
6721 Loc : constant Source_Ptr := Sloc (N);
6722 Typ : constant Entity_Id := Etype (N);
6723 Lhs : constant Node_Id := Left_Opnd (N);
6724 Rhs : constant Node_Id := Right_Opnd (N);
6725 Bodies : constant List_Id := New_List;
6726 A_Typ : constant Entity_Id := Etype (Lhs);
6728 Typl : Entity_Id := A_Typ;
6729 Op_Name : Entity_Id;
6730 Prim : Elmt_Id;
6732 procedure Build_Equality_Call (Eq : Entity_Id);
6733 -- If a constructed equality exists for the type or for its parent,
6734 -- build and analyze call, adding conversions if the operation is
6735 -- inherited.
6737 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
6738 -- Determines whether a type has a subcomponent of an unconstrained
6739 -- Unchecked_Union subtype. Typ is a record type.
6741 -------------------------
6742 -- Build_Equality_Call --
6743 -------------------------
6745 procedure Build_Equality_Call (Eq : Entity_Id) is
6746 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
6747 L_Exp : Node_Id := Relocate_Node (Lhs);
6748 R_Exp : Node_Id := Relocate_Node (Rhs);
6750 begin
6751 if Base_Type (Op_Type) /= Base_Type (A_Typ)
6752 and then not Is_Class_Wide_Type (A_Typ)
6753 then
6754 L_Exp := OK_Convert_To (Op_Type, L_Exp);
6755 R_Exp := OK_Convert_To (Op_Type, R_Exp);
6756 end if;
6758 -- If we have an Unchecked_Union, we need to add the inferred
6759 -- discriminant values as actuals in the function call. At this
6760 -- point, the expansion has determined that both operands have
6761 -- inferable discriminants.
6763 if Is_Unchecked_Union (Op_Type) then
6764 declare
6765 Lhs_Type : constant Node_Id := Etype (L_Exp);
6766 Rhs_Type : constant Node_Id := Etype (R_Exp);
6767 Lhs_Discr_Val : Node_Id;
6768 Rhs_Discr_Val : Node_Id;
6770 begin
6771 -- Per-object constrained selected components require special
6772 -- attention. If the enclosing scope of the component is an
6773 -- Unchecked_Union, we cannot reference its discriminants
6774 -- directly. This is why we use the two extra parameters of
6775 -- the equality function of the enclosing Unchecked_Union.
6777 -- type UU_Type (Discr : Integer := 0) is
6778 -- . . .
6779 -- end record;
6780 -- pragma Unchecked_Union (UU_Type);
6782 -- 1. Unchecked_Union enclosing record:
6784 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
6785 -- . . .
6786 -- Comp : UU_Type (Discr);
6787 -- . . .
6788 -- end Enclosing_UU_Type;
6789 -- pragma Unchecked_Union (Enclosing_UU_Type);
6791 -- Obj1 : Enclosing_UU_Type;
6792 -- Obj2 : Enclosing_UU_Type (1);
6794 -- [. . .] Obj1 = Obj2 [. . .]
6796 -- Generated code:
6798 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
6800 -- A and B are the formal parameters of the equality function
6801 -- of Enclosing_UU_Type. The function always has two extra
6802 -- formals to capture the inferred discriminant values.
6804 -- 2. Non-Unchecked_Union enclosing record:
6806 -- type
6807 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
6808 -- is record
6809 -- . . .
6810 -- Comp : UU_Type (Discr);
6811 -- . . .
6812 -- end Enclosing_Non_UU_Type;
6814 -- Obj1 : Enclosing_Non_UU_Type;
6815 -- Obj2 : Enclosing_Non_UU_Type (1);
6817 -- ... Obj1 = Obj2 ...
6819 -- Generated code:
6821 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
6822 -- obj1.discr, obj2.discr)) then
6824 -- In this case we can directly reference the discriminants of
6825 -- the enclosing record.
6827 -- Lhs of equality
6829 if Nkind (Lhs) = N_Selected_Component
6830 and then Has_Per_Object_Constraint
6831 (Entity (Selector_Name (Lhs)))
6832 then
6833 -- Enclosing record is an Unchecked_Union, use formal A
6835 if Is_Unchecked_Union
6836 (Scope (Entity (Selector_Name (Lhs))))
6837 then
6838 Lhs_Discr_Val := Make_Identifier (Loc, Name_A);
6840 -- Enclosing record is of a non-Unchecked_Union type, it is
6841 -- possible to reference the discriminant.
6843 else
6844 Lhs_Discr_Val :=
6845 Make_Selected_Component (Loc,
6846 Prefix => Prefix (Lhs),
6847 Selector_Name =>
6848 New_Copy
6849 (Get_Discriminant_Value
6850 (First_Discriminant (Lhs_Type),
6851 Lhs_Type,
6852 Stored_Constraint (Lhs_Type))));
6853 end if;
6855 -- Comment needed here ???
6857 else
6858 -- Infer the discriminant value
6860 Lhs_Discr_Val :=
6861 New_Copy
6862 (Get_Discriminant_Value
6863 (First_Discriminant (Lhs_Type),
6864 Lhs_Type,
6865 Stored_Constraint (Lhs_Type)));
6866 end if;
6868 -- Rhs of equality
6870 if Nkind (Rhs) = N_Selected_Component
6871 and then Has_Per_Object_Constraint
6872 (Entity (Selector_Name (Rhs)))
6873 then
6874 if Is_Unchecked_Union
6875 (Scope (Entity (Selector_Name (Rhs))))
6876 then
6877 Rhs_Discr_Val := Make_Identifier (Loc, Name_B);
6879 else
6880 Rhs_Discr_Val :=
6881 Make_Selected_Component (Loc,
6882 Prefix => Prefix (Rhs),
6883 Selector_Name =>
6884 New_Copy (Get_Discriminant_Value (
6885 First_Discriminant (Rhs_Type),
6886 Rhs_Type,
6887 Stored_Constraint (Rhs_Type))));
6889 end if;
6890 else
6891 Rhs_Discr_Val :=
6892 New_Copy (Get_Discriminant_Value (
6893 First_Discriminant (Rhs_Type),
6894 Rhs_Type,
6895 Stored_Constraint (Rhs_Type)));
6897 end if;
6899 Rewrite (N,
6900 Make_Function_Call (Loc,
6901 Name => New_Reference_To (Eq, Loc),
6902 Parameter_Associations => New_List (
6903 L_Exp,
6904 R_Exp,
6905 Lhs_Discr_Val,
6906 Rhs_Discr_Val)));
6907 end;
6909 -- Normal case, not an unchecked union
6911 else
6912 Rewrite (N,
6913 Make_Function_Call (Loc,
6914 Name => New_Reference_To (Eq, Loc),
6915 Parameter_Associations => New_List (L_Exp, R_Exp)));
6916 end if;
6918 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6919 end Build_Equality_Call;
6921 ------------------------------------
6922 -- Has_Unconstrained_UU_Component --
6923 ------------------------------------
6925 function Has_Unconstrained_UU_Component
6926 (Typ : Node_Id) return Boolean
6928 Tdef : constant Node_Id :=
6929 Type_Definition (Declaration_Node (Base_Type (Typ)));
6930 Clist : Node_Id;
6931 Vpart : Node_Id;
6933 function Component_Is_Unconstrained_UU
6934 (Comp : Node_Id) return Boolean;
6935 -- Determines whether the subtype of the component is an
6936 -- unconstrained Unchecked_Union.
6938 function Variant_Is_Unconstrained_UU
6939 (Variant : Node_Id) return Boolean;
6940 -- Determines whether a component of the variant has an unconstrained
6941 -- Unchecked_Union subtype.
6943 -----------------------------------
6944 -- Component_Is_Unconstrained_UU --
6945 -----------------------------------
6947 function Component_Is_Unconstrained_UU
6948 (Comp : Node_Id) return Boolean
6950 begin
6951 if Nkind (Comp) /= N_Component_Declaration then
6952 return False;
6953 end if;
6955 declare
6956 Sindic : constant Node_Id :=
6957 Subtype_Indication (Component_Definition (Comp));
6959 begin
6960 -- Unconstrained nominal type. In the case of a constraint
6961 -- present, the node kind would have been N_Subtype_Indication.
6963 if Nkind (Sindic) = N_Identifier then
6964 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
6965 end if;
6967 return False;
6968 end;
6969 end Component_Is_Unconstrained_UU;
6971 ---------------------------------
6972 -- Variant_Is_Unconstrained_UU --
6973 ---------------------------------
6975 function Variant_Is_Unconstrained_UU
6976 (Variant : Node_Id) return Boolean
6978 Clist : constant Node_Id := Component_List (Variant);
6980 begin
6981 if Is_Empty_List (Component_Items (Clist)) then
6982 return False;
6983 end if;
6985 -- We only need to test one component
6987 declare
6988 Comp : Node_Id := First (Component_Items (Clist));
6990 begin
6991 while Present (Comp) loop
6992 if Component_Is_Unconstrained_UU (Comp) then
6993 return True;
6994 end if;
6996 Next (Comp);
6997 end loop;
6998 end;
7000 -- None of the components withing the variant were of
7001 -- unconstrained Unchecked_Union type.
7003 return False;
7004 end Variant_Is_Unconstrained_UU;
7006 -- Start of processing for Has_Unconstrained_UU_Component
7008 begin
7009 if Null_Present (Tdef) then
7010 return False;
7011 end if;
7013 Clist := Component_List (Tdef);
7014 Vpart := Variant_Part (Clist);
7016 -- Inspect available components
7018 if Present (Component_Items (Clist)) then
7019 declare
7020 Comp : Node_Id := First (Component_Items (Clist));
7022 begin
7023 while Present (Comp) loop
7025 -- One component is sufficient
7027 if Component_Is_Unconstrained_UU (Comp) then
7028 return True;
7029 end if;
7031 Next (Comp);
7032 end loop;
7033 end;
7034 end if;
7036 -- Inspect available components withing variants
7038 if Present (Vpart) then
7039 declare
7040 Variant : Node_Id := First (Variants (Vpart));
7042 begin
7043 while Present (Variant) loop
7045 -- One component within a variant is sufficient
7047 if Variant_Is_Unconstrained_UU (Variant) then
7048 return True;
7049 end if;
7051 Next (Variant);
7052 end loop;
7053 end;
7054 end if;
7056 -- Neither the available components, nor the components inside the
7057 -- variant parts were of an unconstrained Unchecked_Union subtype.
7059 return False;
7060 end Has_Unconstrained_UU_Component;
7062 -- Start of processing for Expand_N_Op_Eq
7064 begin
7065 Binary_Op_Validity_Checks (N);
7067 -- Deal with private types
7069 if Ekind (Typl) = E_Private_Type then
7070 Typl := Underlying_Type (Typl);
7071 elsif Ekind (Typl) = E_Private_Subtype then
7072 Typl := Underlying_Type (Base_Type (Typl));
7073 else
7074 null;
7075 end if;
7077 -- It may happen in error situations that the underlying type is not
7078 -- set. The error will be detected later, here we just defend the
7079 -- expander code.
7081 if No (Typl) then
7082 return;
7083 end if;
7085 Typl := Base_Type (Typl);
7087 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7088 -- means we no longer have a comparison operation, we are all done.
7090 Expand_Compare_Minimize_Eliminate_Overflow (N);
7092 if Nkind (N) /= N_Op_Eq then
7093 return;
7094 end if;
7096 -- Boolean types (requiring handling of non-standard case)
7098 if Is_Boolean_Type (Typl) then
7099 Adjust_Condition (Left_Opnd (N));
7100 Adjust_Condition (Right_Opnd (N));
7101 Set_Etype (N, Standard_Boolean);
7102 Adjust_Result_Type (N, Typ);
7104 -- Array types
7106 elsif Is_Array_Type (Typl) then
7108 -- If we are doing full validity checking, and it is possible for the
7109 -- array elements to be invalid then expand out array comparisons to
7110 -- make sure that we check the array elements.
7112 if Validity_Check_Operands
7113 and then not Is_Known_Valid (Component_Type (Typl))
7114 then
7115 declare
7116 Save_Force_Validity_Checks : constant Boolean :=
7117 Force_Validity_Checks;
7118 begin
7119 Force_Validity_Checks := True;
7120 Rewrite (N,
7121 Expand_Array_Equality
7123 Relocate_Node (Lhs),
7124 Relocate_Node (Rhs),
7125 Bodies,
7126 Typl));
7127 Insert_Actions (N, Bodies);
7128 Analyze_And_Resolve (N, Standard_Boolean);
7129 Force_Validity_Checks := Save_Force_Validity_Checks;
7130 end;
7132 -- Packed case where both operands are known aligned
7134 elsif Is_Bit_Packed_Array (Typl)
7135 and then not Is_Possibly_Unaligned_Object (Lhs)
7136 and then not Is_Possibly_Unaligned_Object (Rhs)
7137 then
7138 Expand_Packed_Eq (N);
7140 -- Where the component type is elementary we can use a block bit
7141 -- comparison (if supported on the target) exception in the case
7142 -- of floating-point (negative zero issues require element by
7143 -- element comparison), and atomic types (where we must be sure
7144 -- to load elements independently) and possibly unaligned arrays.
7146 elsif Is_Elementary_Type (Component_Type (Typl))
7147 and then not Is_Floating_Point_Type (Component_Type (Typl))
7148 and then not Is_Atomic (Component_Type (Typl))
7149 and then not Is_Possibly_Unaligned_Object (Lhs)
7150 and then not Is_Possibly_Unaligned_Object (Rhs)
7151 and then Support_Composite_Compare_On_Target
7152 then
7153 null;
7155 -- For composite and floating-point cases, expand equality loop to
7156 -- make sure of using proper comparisons for tagged types, and
7157 -- correctly handling the floating-point case.
7159 else
7160 Rewrite (N,
7161 Expand_Array_Equality
7163 Relocate_Node (Lhs),
7164 Relocate_Node (Rhs),
7165 Bodies,
7166 Typl));
7167 Insert_Actions (N, Bodies, Suppress => All_Checks);
7168 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7169 end if;
7171 -- Record Types
7173 elsif Is_Record_Type (Typl) then
7175 -- For tagged types, use the primitive "="
7177 if Is_Tagged_Type (Typl) then
7179 -- No need to do anything else compiling under restriction
7180 -- No_Dispatching_Calls. During the semantic analysis we
7181 -- already notified such violation.
7183 if Restriction_Active (No_Dispatching_Calls) then
7184 return;
7185 end if;
7187 -- If this is derived from an untagged private type completed with
7188 -- a tagged type, it does not have a full view, so we use the
7189 -- primitive operations of the private type. This check should no
7190 -- longer be necessary when these types get their full views???
7192 if Is_Private_Type (A_Typ)
7193 and then not Is_Tagged_Type (A_Typ)
7194 and then Is_Derived_Type (A_Typ)
7195 and then No (Full_View (A_Typ))
7196 then
7197 -- Search for equality operation, checking that the operands
7198 -- have the same type. Note that we must find a matching entry,
7199 -- or something is very wrong!
7201 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
7203 while Present (Prim) loop
7204 exit when Chars (Node (Prim)) = Name_Op_Eq
7205 and then Etype (First_Formal (Node (Prim))) =
7206 Etype (Next_Formal (First_Formal (Node (Prim))))
7207 and then
7208 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7210 Next_Elmt (Prim);
7211 end loop;
7213 pragma Assert (Present (Prim));
7214 Op_Name := Node (Prim);
7216 -- Find the type's predefined equality or an overriding
7217 -- user- defined equality. The reason for not simply calling
7218 -- Find_Prim_Op here is that there may be a user-defined
7219 -- overloaded equality op that precedes the equality that we want,
7220 -- so we have to explicitly search (e.g., there could be an
7221 -- equality with two different parameter types).
7223 else
7224 if Is_Class_Wide_Type (Typl) then
7225 Typl := Root_Type (Typl);
7226 end if;
7228 Prim := First_Elmt (Primitive_Operations (Typl));
7229 while Present (Prim) loop
7230 exit when Chars (Node (Prim)) = Name_Op_Eq
7231 and then Etype (First_Formal (Node (Prim))) =
7232 Etype (Next_Formal (First_Formal (Node (Prim))))
7233 and then
7234 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7236 Next_Elmt (Prim);
7237 end loop;
7239 pragma Assert (Present (Prim));
7240 Op_Name := Node (Prim);
7241 end if;
7243 Build_Equality_Call (Op_Name);
7245 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7246 -- predefined equality operator for a type which has a subcomponent
7247 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7249 elsif Has_Unconstrained_UU_Component (Typl) then
7250 Insert_Action (N,
7251 Make_Raise_Program_Error (Loc,
7252 Reason => PE_Unchecked_Union_Restriction));
7254 -- Prevent Gigi from generating incorrect code by rewriting the
7255 -- equality as a standard False. (is this documented somewhere???)
7257 Rewrite (N,
7258 New_Occurrence_Of (Standard_False, Loc));
7260 elsif Is_Unchecked_Union (Typl) then
7262 -- If we can infer the discriminants of the operands, we make a
7263 -- call to the TSS equality function.
7265 if Has_Inferable_Discriminants (Lhs)
7266 and then
7267 Has_Inferable_Discriminants (Rhs)
7268 then
7269 Build_Equality_Call
7270 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7272 else
7273 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7274 -- the predefined equality operator for an Unchecked_Union type
7275 -- if either of the operands lack inferable discriminants.
7277 Insert_Action (N,
7278 Make_Raise_Program_Error (Loc,
7279 Reason => PE_Unchecked_Union_Restriction));
7281 -- Prevent Gigi from generating incorrect code by rewriting
7282 -- the equality as a standard False (documented where???).
7284 Rewrite (N,
7285 New_Occurrence_Of (Standard_False, Loc));
7287 end if;
7289 -- If a type support function is present (for complex cases), use it
7291 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
7292 Build_Equality_Call
7293 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7295 -- Otherwise expand the component by component equality. Note that
7296 -- we never use block-bit comparisons for records, because of the
7297 -- problems with gaps. The backend will often be able to recombine
7298 -- the separate comparisons that we generate here.
7300 else
7301 Remove_Side_Effects (Lhs);
7302 Remove_Side_Effects (Rhs);
7303 Rewrite (N,
7304 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
7306 Insert_Actions (N, Bodies, Suppress => All_Checks);
7307 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7308 end if;
7309 end if;
7311 -- Test if result is known at compile time
7313 Rewrite_Comparison (N);
7315 -- If we still have comparison for Vax_Float, process it
7317 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
7318 Expand_Vax_Comparison (N);
7319 return;
7320 end if;
7322 Optimize_Length_Comparison (N);
7323 end Expand_N_Op_Eq;
7325 -----------------------
7326 -- Expand_N_Op_Expon --
7327 -----------------------
7329 procedure Expand_N_Op_Expon (N : Node_Id) is
7330 Loc : constant Source_Ptr := Sloc (N);
7331 Typ : constant Entity_Id := Etype (N);
7332 Rtyp : constant Entity_Id := Root_Type (Typ);
7333 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
7334 Bastyp : constant Node_Id := Etype (Base);
7335 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
7336 Exptyp : constant Entity_Id := Etype (Exp);
7337 Ovflo : constant Boolean := Do_Overflow_Check (N);
7338 Expv : Uint;
7339 Temp : Node_Id;
7340 Rent : RE_Id;
7341 Ent : Entity_Id;
7342 Etyp : Entity_Id;
7343 Xnode : Node_Id;
7345 begin
7346 Binary_Op_Validity_Checks (N);
7348 -- CodePeer and GNATprove want to see the unexpanded N_Op_Expon node
7350 if CodePeer_Mode or Alfa_Mode then
7351 return;
7352 end if;
7354 -- If either operand is of a private type, then we have the use of an
7355 -- intrinsic operator, and we get rid of the privateness, by using root
7356 -- types of underlying types for the actual operation. Otherwise the
7357 -- private types will cause trouble if we expand multiplications or
7358 -- shifts etc. We also do this transformation if the result type is
7359 -- different from the base type.
7361 if Is_Private_Type (Etype (Base))
7362 or else Is_Private_Type (Typ)
7363 or else Is_Private_Type (Exptyp)
7364 or else Rtyp /= Root_Type (Bastyp)
7365 then
7366 declare
7367 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
7368 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
7370 begin
7371 Rewrite (N,
7372 Unchecked_Convert_To (Typ,
7373 Make_Op_Expon (Loc,
7374 Left_Opnd => Unchecked_Convert_To (Bt, Base),
7375 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
7376 Analyze_And_Resolve (N, Typ);
7377 return;
7378 end;
7379 end if;
7381 -- Check for MINIMIZED/ELIMINATED overflow mode
7383 if Minimized_Eliminated_Overflow_Check (N) then
7384 Apply_Arithmetic_Overflow_Check (N);
7385 return;
7386 end if;
7388 -- Test for case of known right argument where we can replace the
7389 -- exponentiation by an equivalent expression using multiplication.
7391 if Compile_Time_Known_Value (Exp) then
7392 Expv := Expr_Value (Exp);
7394 -- We only fold small non-negative exponents. You might think we
7395 -- could fold small negative exponents for the real case, but we
7396 -- can't because we are required to raise Constraint_Error for
7397 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
7398 -- See ACVC test C4A012B.
7400 if Expv >= 0 and then Expv <= 4 then
7402 -- X ** 0 = 1 (or 1.0)
7404 if Expv = 0 then
7406 -- Call Remove_Side_Effects to ensure that any side effects
7407 -- in the ignored left operand (in particular function calls
7408 -- to user defined functions) are properly executed.
7410 Remove_Side_Effects (Base);
7412 if Ekind (Typ) in Integer_Kind then
7413 Xnode := Make_Integer_Literal (Loc, Intval => 1);
7414 else
7415 Xnode := Make_Real_Literal (Loc, Ureal_1);
7416 end if;
7418 -- X ** 1 = X
7420 elsif Expv = 1 then
7421 Xnode := Base;
7423 -- X ** 2 = X * X
7425 elsif Expv = 2 then
7426 Xnode :=
7427 Make_Op_Multiply (Loc,
7428 Left_Opnd => Duplicate_Subexpr (Base),
7429 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
7431 -- X ** 3 = X * X * X
7433 elsif Expv = 3 then
7434 Xnode :=
7435 Make_Op_Multiply (Loc,
7436 Left_Opnd =>
7437 Make_Op_Multiply (Loc,
7438 Left_Opnd => Duplicate_Subexpr (Base),
7439 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
7440 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
7442 -- X ** 4 ->
7444 -- do
7445 -- En : constant base'type := base * base;
7446 -- in
7447 -- En * En
7449 else
7450 pragma Assert (Expv = 4);
7451 Temp := Make_Temporary (Loc, 'E', Base);
7453 Xnode :=
7454 Make_Expression_With_Actions (Loc,
7455 Actions => New_List (
7456 Make_Object_Declaration (Loc,
7457 Defining_Identifier => Temp,
7458 Constant_Present => True,
7459 Object_Definition => New_Reference_To (Typ, Loc),
7460 Expression =>
7461 Make_Op_Multiply (Loc,
7462 Left_Opnd =>
7463 Duplicate_Subexpr (Base),
7464 Right_Opnd =>
7465 Duplicate_Subexpr_No_Checks (Base)))),
7467 Expression =>
7468 Make_Op_Multiply (Loc,
7469 Left_Opnd => New_Reference_To (Temp, Loc),
7470 Right_Opnd => New_Reference_To (Temp, Loc)));
7471 end if;
7473 Rewrite (N, Xnode);
7474 Analyze_And_Resolve (N, Typ);
7475 return;
7476 end if;
7477 end if;
7479 -- Case of (2 ** expression) appearing as an argument of an integer
7480 -- multiplication, or as the right argument of a division of a non-
7481 -- negative integer. In such cases we leave the node untouched, setting
7482 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
7483 -- of the higher level node converts it into a shift.
7485 -- Another case is 2 ** N in any other context. We simply convert
7486 -- this to 1 * 2 ** N, and then the above transformation applies.
7488 -- Note: this transformation is not applicable for a modular type with
7489 -- a non-binary modulus in the multiplication case, since we get a wrong
7490 -- result if the shift causes an overflow before the modular reduction.
7492 if Nkind (Base) = N_Integer_Literal
7493 and then Intval (Base) = 2
7494 and then Is_Integer_Type (Root_Type (Exptyp))
7495 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
7496 and then Is_Unsigned_Type (Exptyp)
7497 and then not Ovflo
7498 then
7499 -- First the multiply and divide cases
7501 if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then
7502 declare
7503 P : constant Node_Id := Parent (N);
7504 L : constant Node_Id := Left_Opnd (P);
7505 R : constant Node_Id := Right_Opnd (P);
7507 begin
7508 if (Nkind (P) = N_Op_Multiply
7509 and then not Non_Binary_Modulus (Typ)
7510 and then
7511 ((Is_Integer_Type (Etype (L)) and then R = N)
7512 or else
7513 (Is_Integer_Type (Etype (R)) and then L = N))
7514 and then not Do_Overflow_Check (P))
7515 or else
7516 (Nkind (P) = N_Op_Divide
7517 and then Is_Integer_Type (Etype (L))
7518 and then Is_Unsigned_Type (Etype (L))
7519 and then R = N
7520 and then not Do_Overflow_Check (P))
7521 then
7522 Set_Is_Power_Of_2_For_Shift (N);
7523 return;
7524 end if;
7525 end;
7527 -- Now the other cases
7529 elsif not Non_Binary_Modulus (Typ) then
7530 Rewrite (N,
7531 Make_Op_Multiply (Loc,
7532 Left_Opnd => Make_Integer_Literal (Loc, 1),
7533 Right_Opnd => Relocate_Node (N)));
7534 Analyze_And_Resolve (N, Typ);
7535 return;
7536 end if;
7537 end if;
7539 -- Fall through if exponentiation must be done using a runtime routine
7541 -- First deal with modular case
7543 if Is_Modular_Integer_Type (Rtyp) then
7545 -- Non-binary case, we call the special exponentiation routine for
7546 -- the non-binary case, converting the argument to Long_Long_Integer
7547 -- and passing the modulus value. Then the result is converted back
7548 -- to the base type.
7550 if Non_Binary_Modulus (Rtyp) then
7551 Rewrite (N,
7552 Convert_To (Typ,
7553 Make_Function_Call (Loc,
7554 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
7555 Parameter_Associations => New_List (
7556 Convert_To (Standard_Integer, Base),
7557 Make_Integer_Literal (Loc, Modulus (Rtyp)),
7558 Exp))));
7560 -- Binary case, in this case, we call one of two routines, either the
7561 -- unsigned integer case, or the unsigned long long integer case,
7562 -- with a final "and" operation to do the required mod.
7564 else
7565 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
7566 Ent := RTE (RE_Exp_Unsigned);
7567 else
7568 Ent := RTE (RE_Exp_Long_Long_Unsigned);
7569 end if;
7571 Rewrite (N,
7572 Convert_To (Typ,
7573 Make_Op_And (Loc,
7574 Left_Opnd =>
7575 Make_Function_Call (Loc,
7576 Name => New_Reference_To (Ent, Loc),
7577 Parameter_Associations => New_List (
7578 Convert_To (Etype (First_Formal (Ent)), Base),
7579 Exp)),
7580 Right_Opnd =>
7581 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
7583 end if;
7585 -- Common exit point for modular type case
7587 Analyze_And_Resolve (N, Typ);
7588 return;
7590 -- Signed integer cases, done using either Integer or Long_Long_Integer.
7591 -- It is not worth having routines for Short_[Short_]Integer, since for
7592 -- most machines it would not help, and it would generate more code that
7593 -- might need certification when a certified run time is required.
7595 -- In the integer cases, we have two routines, one for when overflow
7596 -- checks are required, and one when they are not required, since there
7597 -- is a real gain in omitting checks on many machines.
7599 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
7600 or else (Rtyp = Base_Type (Standard_Long_Integer)
7601 and then
7602 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
7603 or else (Rtyp = Universal_Integer)
7604 then
7605 Etyp := Standard_Long_Long_Integer;
7607 if Ovflo then
7608 Rent := RE_Exp_Long_Long_Integer;
7609 else
7610 Rent := RE_Exn_Long_Long_Integer;
7611 end if;
7613 elsif Is_Signed_Integer_Type (Rtyp) then
7614 Etyp := Standard_Integer;
7616 if Ovflo then
7617 Rent := RE_Exp_Integer;
7618 else
7619 Rent := RE_Exn_Integer;
7620 end if;
7622 -- Floating-point cases, always done using Long_Long_Float. We do not
7623 -- need separate routines for the overflow case here, since in the case
7624 -- of floating-point, we generate infinities anyway as a rule (either
7625 -- that or we automatically trap overflow), and if there is an infinity
7626 -- generated and a range check is required, the check will fail anyway.
7628 else
7629 pragma Assert (Is_Floating_Point_Type (Rtyp));
7630 Etyp := Standard_Long_Long_Float;
7631 Rent := RE_Exn_Long_Long_Float;
7632 end if;
7634 -- Common processing for integer cases and floating-point cases.
7635 -- If we are in the right type, we can call runtime routine directly
7637 if Typ = Etyp
7638 and then Rtyp /= Universal_Integer
7639 and then Rtyp /= Universal_Real
7640 then
7641 Rewrite (N,
7642 Make_Function_Call (Loc,
7643 Name => New_Reference_To (RTE (Rent), Loc),
7644 Parameter_Associations => New_List (Base, Exp)));
7646 -- Otherwise we have to introduce conversions (conversions are also
7647 -- required in the universal cases, since the runtime routine is
7648 -- typed using one of the standard types).
7650 else
7651 Rewrite (N,
7652 Convert_To (Typ,
7653 Make_Function_Call (Loc,
7654 Name => New_Reference_To (RTE (Rent), Loc),
7655 Parameter_Associations => New_List (
7656 Convert_To (Etyp, Base),
7657 Exp))));
7658 end if;
7660 Analyze_And_Resolve (N, Typ);
7661 return;
7663 exception
7664 when RE_Not_Available =>
7665 return;
7666 end Expand_N_Op_Expon;
7668 --------------------
7669 -- Expand_N_Op_Ge --
7670 --------------------
7672 procedure Expand_N_Op_Ge (N : Node_Id) is
7673 Typ : constant Entity_Id := Etype (N);
7674 Op1 : constant Node_Id := Left_Opnd (N);
7675 Op2 : constant Node_Id := Right_Opnd (N);
7676 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7678 begin
7679 Binary_Op_Validity_Checks (N);
7681 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7682 -- means we no longer have a comparison operation, we are all done.
7684 Expand_Compare_Minimize_Eliminate_Overflow (N);
7686 if Nkind (N) /= N_Op_Ge then
7687 return;
7688 end if;
7690 -- Array type case
7692 if Is_Array_Type (Typ1) then
7693 Expand_Array_Comparison (N);
7694 return;
7695 end if;
7697 -- Deal with boolean operands
7699 if Is_Boolean_Type (Typ1) then
7700 Adjust_Condition (Op1);
7701 Adjust_Condition (Op2);
7702 Set_Etype (N, Standard_Boolean);
7703 Adjust_Result_Type (N, Typ);
7704 end if;
7706 Rewrite_Comparison (N);
7708 -- If we still have comparison, and Vax_Float type, process it
7710 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7711 Expand_Vax_Comparison (N);
7712 return;
7713 end if;
7715 Optimize_Length_Comparison (N);
7716 end Expand_N_Op_Ge;
7718 --------------------
7719 -- Expand_N_Op_Gt --
7720 --------------------
7722 procedure Expand_N_Op_Gt (N : Node_Id) is
7723 Typ : constant Entity_Id := Etype (N);
7724 Op1 : constant Node_Id := Left_Opnd (N);
7725 Op2 : constant Node_Id := Right_Opnd (N);
7726 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7728 begin
7729 Binary_Op_Validity_Checks (N);
7731 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7732 -- means we no longer have a comparison operation, we are all done.
7734 Expand_Compare_Minimize_Eliminate_Overflow (N);
7736 if Nkind (N) /= N_Op_Gt then
7737 return;
7738 end if;
7740 -- Deal with array type operands
7742 if Is_Array_Type (Typ1) then
7743 Expand_Array_Comparison (N);
7744 return;
7745 end if;
7747 -- Deal with boolean type operands
7749 if Is_Boolean_Type (Typ1) then
7750 Adjust_Condition (Op1);
7751 Adjust_Condition (Op2);
7752 Set_Etype (N, Standard_Boolean);
7753 Adjust_Result_Type (N, Typ);
7754 end if;
7756 Rewrite_Comparison (N);
7758 -- If we still have comparison, and Vax_Float type, process it
7760 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7761 Expand_Vax_Comparison (N);
7762 return;
7763 end if;
7765 Optimize_Length_Comparison (N);
7766 end Expand_N_Op_Gt;
7768 --------------------
7769 -- Expand_N_Op_Le --
7770 --------------------
7772 procedure Expand_N_Op_Le (N : Node_Id) is
7773 Typ : constant Entity_Id := Etype (N);
7774 Op1 : constant Node_Id := Left_Opnd (N);
7775 Op2 : constant Node_Id := Right_Opnd (N);
7776 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7778 begin
7779 Binary_Op_Validity_Checks (N);
7781 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7782 -- means we no longer have a comparison operation, we are all done.
7784 Expand_Compare_Minimize_Eliminate_Overflow (N);
7786 if Nkind (N) /= N_Op_Le then
7787 return;
7788 end if;
7790 -- Deal with array type operands
7792 if Is_Array_Type (Typ1) then
7793 Expand_Array_Comparison (N);
7794 return;
7795 end if;
7797 -- Deal with Boolean type operands
7799 if Is_Boolean_Type (Typ1) then
7800 Adjust_Condition (Op1);
7801 Adjust_Condition (Op2);
7802 Set_Etype (N, Standard_Boolean);
7803 Adjust_Result_Type (N, Typ);
7804 end if;
7806 Rewrite_Comparison (N);
7808 -- If we still have comparison, and Vax_Float type, process it
7810 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7811 Expand_Vax_Comparison (N);
7812 return;
7813 end if;
7815 Optimize_Length_Comparison (N);
7816 end Expand_N_Op_Le;
7818 --------------------
7819 -- Expand_N_Op_Lt --
7820 --------------------
7822 procedure Expand_N_Op_Lt (N : Node_Id) is
7823 Typ : constant Entity_Id := Etype (N);
7824 Op1 : constant Node_Id := Left_Opnd (N);
7825 Op2 : constant Node_Id := Right_Opnd (N);
7826 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7828 begin
7829 Binary_Op_Validity_Checks (N);
7831 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7832 -- means we no longer have a comparison operation, we are all done.
7834 Expand_Compare_Minimize_Eliminate_Overflow (N);
7836 if Nkind (N) /= N_Op_Lt then
7837 return;
7838 end if;
7840 -- Deal with array type operands
7842 if Is_Array_Type (Typ1) then
7843 Expand_Array_Comparison (N);
7844 return;
7845 end if;
7847 -- Deal with Boolean type operands
7849 if Is_Boolean_Type (Typ1) then
7850 Adjust_Condition (Op1);
7851 Adjust_Condition (Op2);
7852 Set_Etype (N, Standard_Boolean);
7853 Adjust_Result_Type (N, Typ);
7854 end if;
7856 Rewrite_Comparison (N);
7858 -- If we still have comparison, and Vax_Float type, process it
7860 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7861 Expand_Vax_Comparison (N);
7862 return;
7863 end if;
7865 Optimize_Length_Comparison (N);
7866 end Expand_N_Op_Lt;
7868 -----------------------
7869 -- Expand_N_Op_Minus --
7870 -----------------------
7872 procedure Expand_N_Op_Minus (N : Node_Id) is
7873 Loc : constant Source_Ptr := Sloc (N);
7874 Typ : constant Entity_Id := Etype (N);
7876 begin
7877 Unary_Op_Validity_Checks (N);
7879 -- Check for MINIMIZED/ELIMINATED overflow mode
7881 if Minimized_Eliminated_Overflow_Check (N) then
7882 Apply_Arithmetic_Overflow_Check (N);
7883 return;
7884 end if;
7886 if not Backend_Overflow_Checks_On_Target
7887 and then Is_Signed_Integer_Type (Etype (N))
7888 and then Do_Overflow_Check (N)
7889 then
7890 -- Software overflow checking expands -expr into (0 - expr)
7892 Rewrite (N,
7893 Make_Op_Subtract (Loc,
7894 Left_Opnd => Make_Integer_Literal (Loc, 0),
7895 Right_Opnd => Right_Opnd (N)));
7897 Analyze_And_Resolve (N, Typ);
7899 -- Vax floating-point types case
7901 elsif Vax_Float (Etype (N)) then
7902 Expand_Vax_Arith (N);
7903 end if;
7904 end Expand_N_Op_Minus;
7906 ---------------------
7907 -- Expand_N_Op_Mod --
7908 ---------------------
7910 procedure Expand_N_Op_Mod (N : Node_Id) is
7911 Loc : constant Source_Ptr := Sloc (N);
7912 Typ : constant Entity_Id := Etype (N);
7913 DOC : constant Boolean := Do_Overflow_Check (N);
7914 DDC : constant Boolean := Do_Division_Check (N);
7916 Left : Node_Id;
7917 Right : Node_Id;
7919 LLB : Uint;
7920 Llo : Uint;
7921 Lhi : Uint;
7922 LOK : Boolean;
7923 Rlo : Uint;
7924 Rhi : Uint;
7925 ROK : Boolean;
7927 pragma Warnings (Off, Lhi);
7929 begin
7930 Binary_Op_Validity_Checks (N);
7932 -- Check for MINIMIZED/ELIMINATED overflow mode
7934 if Minimized_Eliminated_Overflow_Check (N) then
7935 Apply_Arithmetic_Overflow_Check (N);
7936 return;
7937 end if;
7939 if Is_Integer_Type (Etype (N)) then
7940 Apply_Divide_Checks (N);
7942 -- All done if we don't have a MOD any more, which can happen as a
7943 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
7945 if Nkind (N) /= N_Op_Mod then
7946 return;
7947 end if;
7948 end if;
7950 -- Proceed with expansion of mod operator
7952 Left := Left_Opnd (N);
7953 Right := Right_Opnd (N);
7955 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
7956 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
7958 -- Convert mod to rem if operands are known non-negative. We do this
7959 -- since it is quite likely that this will improve the quality of code,
7960 -- (the operation now corresponds to the hardware remainder), and it
7961 -- does not seem likely that it could be harmful.
7963 if LOK and then Llo >= 0
7964 and then
7965 ROK and then Rlo >= 0
7966 then
7967 Rewrite (N,
7968 Make_Op_Rem (Sloc (N),
7969 Left_Opnd => Left_Opnd (N),
7970 Right_Opnd => Right_Opnd (N)));
7972 -- Instead of reanalyzing the node we do the analysis manually. This
7973 -- avoids anomalies when the replacement is done in an instance and
7974 -- is epsilon more efficient.
7976 Set_Entity (N, Standard_Entity (S_Op_Rem));
7977 Set_Etype (N, Typ);
7978 Set_Do_Overflow_Check (N, DOC);
7979 Set_Do_Division_Check (N, DDC);
7980 Expand_N_Op_Rem (N);
7981 Set_Analyzed (N);
7983 -- Otherwise, normal mod processing
7985 else
7986 -- Apply optimization x mod 1 = 0. We don't really need that with
7987 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
7988 -- certainly harmless.
7990 if Is_Integer_Type (Etype (N))
7991 and then Compile_Time_Known_Value (Right)
7992 and then Expr_Value (Right) = Uint_1
7993 then
7994 -- Call Remove_Side_Effects to ensure that any side effects in
7995 -- the ignored left operand (in particular function calls to
7996 -- user defined functions) are properly executed.
7998 Remove_Side_Effects (Left);
8000 Rewrite (N, Make_Integer_Literal (Loc, 0));
8001 Analyze_And_Resolve (N, Typ);
8002 return;
8003 end if;
8005 -- Deal with annoying case of largest negative number remainder
8006 -- minus one. Gigi does not handle this case correctly, because
8007 -- it generates a divide instruction which may trap in this case.
8009 -- In fact the check is quite easy, if the right operand is -1, then
8010 -- the mod value is always 0, and we can just ignore the left operand
8011 -- completely in this case.
8013 -- This only applies if we still have a mod operator. Skip if we
8014 -- have already rewritten this (e.g. in the case of eliminated
8015 -- overflow checks which have driven us into bignum mode).
8017 if Nkind (N) = N_Op_Mod then
8019 -- The operand type may be private (e.g. in the expansion of an
8020 -- intrinsic operation) so we must use the underlying type to get
8021 -- the bounds, and convert the literals explicitly.
8023 LLB :=
8024 Expr_Value
8025 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
8027 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
8028 and then
8029 ((not LOK) or else (Llo = LLB))
8030 then
8031 Rewrite (N,
8032 Make_If_Expression (Loc,
8033 Expressions => New_List (
8034 Make_Op_Eq (Loc,
8035 Left_Opnd => Duplicate_Subexpr (Right),
8036 Right_Opnd =>
8037 Unchecked_Convert_To (Typ,
8038 Make_Integer_Literal (Loc, -1))),
8039 Unchecked_Convert_To (Typ,
8040 Make_Integer_Literal (Loc, Uint_0)),
8041 Relocate_Node (N))));
8043 Set_Analyzed (Next (Next (First (Expressions (N)))));
8044 Analyze_And_Resolve (N, Typ);
8045 end if;
8046 end if;
8047 end if;
8048 end Expand_N_Op_Mod;
8050 --------------------------
8051 -- Expand_N_Op_Multiply --
8052 --------------------------
8054 procedure Expand_N_Op_Multiply (N : Node_Id) is
8055 Loc : constant Source_Ptr := Sloc (N);
8056 Lop : constant Node_Id := Left_Opnd (N);
8057 Rop : constant Node_Id := Right_Opnd (N);
8059 Lp2 : constant Boolean :=
8060 Nkind (Lop) = N_Op_Expon
8061 and then Is_Power_Of_2_For_Shift (Lop);
8063 Rp2 : constant Boolean :=
8064 Nkind (Rop) = N_Op_Expon
8065 and then Is_Power_Of_2_For_Shift (Rop);
8067 Ltyp : constant Entity_Id := Etype (Lop);
8068 Rtyp : constant Entity_Id := Etype (Rop);
8069 Typ : Entity_Id := Etype (N);
8071 begin
8072 Binary_Op_Validity_Checks (N);
8074 -- Check for MINIMIZED/ELIMINATED overflow mode
8076 if Minimized_Eliminated_Overflow_Check (N) then
8077 Apply_Arithmetic_Overflow_Check (N);
8078 return;
8079 end if;
8081 -- Special optimizations for integer types
8083 if Is_Integer_Type (Typ) then
8085 -- N * 0 = 0 for integer types
8087 if Compile_Time_Known_Value (Rop)
8088 and then Expr_Value (Rop) = Uint_0
8089 then
8090 -- Call Remove_Side_Effects to ensure that any side effects in
8091 -- the ignored left operand (in particular function calls to
8092 -- user defined functions) are properly executed.
8094 Remove_Side_Effects (Lop);
8096 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8097 Analyze_And_Resolve (N, Typ);
8098 return;
8099 end if;
8101 -- Similar handling for 0 * N = 0
8103 if Compile_Time_Known_Value (Lop)
8104 and then Expr_Value (Lop) = Uint_0
8105 then
8106 Remove_Side_Effects (Rop);
8107 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8108 Analyze_And_Resolve (N, Typ);
8109 return;
8110 end if;
8112 -- N * 1 = 1 * N = N for integer types
8114 -- This optimisation is not done if we are going to
8115 -- rewrite the product 1 * 2 ** N to a shift.
8117 if Compile_Time_Known_Value (Rop)
8118 and then Expr_Value (Rop) = Uint_1
8119 and then not Lp2
8120 then
8121 Rewrite (N, Lop);
8122 return;
8124 elsif Compile_Time_Known_Value (Lop)
8125 and then Expr_Value (Lop) = Uint_1
8126 and then not Rp2
8127 then
8128 Rewrite (N, Rop);
8129 return;
8130 end if;
8131 end if;
8133 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
8134 -- Is_Power_Of_2_For_Shift is set means that we know that our left
8135 -- operand is an integer, as required for this to work.
8137 if Rp2 then
8138 if Lp2 then
8140 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
8142 Rewrite (N,
8143 Make_Op_Expon (Loc,
8144 Left_Opnd => Make_Integer_Literal (Loc, 2),
8145 Right_Opnd =>
8146 Make_Op_Add (Loc,
8147 Left_Opnd => Right_Opnd (Lop),
8148 Right_Opnd => Right_Opnd (Rop))));
8149 Analyze_And_Resolve (N, Typ);
8150 return;
8152 else
8153 Rewrite (N,
8154 Make_Op_Shift_Left (Loc,
8155 Left_Opnd => Lop,
8156 Right_Opnd =>
8157 Convert_To (Standard_Natural, Right_Opnd (Rop))));
8158 Analyze_And_Resolve (N, Typ);
8159 return;
8160 end if;
8162 -- Same processing for the operands the other way round
8164 elsif Lp2 then
8165 Rewrite (N,
8166 Make_Op_Shift_Left (Loc,
8167 Left_Opnd => Rop,
8168 Right_Opnd =>
8169 Convert_To (Standard_Natural, Right_Opnd (Lop))));
8170 Analyze_And_Resolve (N, Typ);
8171 return;
8172 end if;
8174 -- Do required fixup of universal fixed operation
8176 if Typ = Universal_Fixed then
8177 Fixup_Universal_Fixed_Operation (N);
8178 Typ := Etype (N);
8179 end if;
8181 -- Multiplications with fixed-point results
8183 if Is_Fixed_Point_Type (Typ) then
8185 -- No special processing if Treat_Fixed_As_Integer is set, since from
8186 -- a semantic point of view such operations are simply integer
8187 -- operations and will be treated that way.
8189 if not Treat_Fixed_As_Integer (N) then
8191 -- Case of fixed * integer => fixed
8193 if Is_Integer_Type (Rtyp) then
8194 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
8196 -- Case of integer * fixed => fixed
8198 elsif Is_Integer_Type (Ltyp) then
8199 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
8201 -- Case of fixed * fixed => fixed
8203 else
8204 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
8205 end if;
8206 end if;
8208 -- Other cases of multiplication of fixed-point operands. Again we
8209 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
8211 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
8212 and then not Treat_Fixed_As_Integer (N)
8213 then
8214 if Is_Integer_Type (Typ) then
8215 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
8216 else
8217 pragma Assert (Is_Floating_Point_Type (Typ));
8218 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
8219 end if;
8221 -- Mixed-mode operations can appear in a non-static universal context,
8222 -- in which case the integer argument must be converted explicitly.
8224 elsif Typ = Universal_Real
8225 and then Is_Integer_Type (Rtyp)
8226 then
8227 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
8229 Analyze_And_Resolve (Rop, Universal_Real);
8231 elsif Typ = Universal_Real
8232 and then Is_Integer_Type (Ltyp)
8233 then
8234 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
8236 Analyze_And_Resolve (Lop, Universal_Real);
8238 -- Non-fixed point cases, check software overflow checking required
8240 elsif Is_Signed_Integer_Type (Etype (N)) then
8241 Apply_Arithmetic_Overflow_Check (N);
8243 -- Deal with VAX float case
8245 elsif Vax_Float (Typ) then
8246 Expand_Vax_Arith (N);
8247 return;
8248 end if;
8249 end Expand_N_Op_Multiply;
8251 --------------------
8252 -- Expand_N_Op_Ne --
8253 --------------------
8255 procedure Expand_N_Op_Ne (N : Node_Id) is
8256 Typ : constant Entity_Id := Etype (Left_Opnd (N));
8258 begin
8259 -- Case of elementary type with standard operator
8261 if Is_Elementary_Type (Typ)
8262 and then Sloc (Entity (N)) = Standard_Location
8263 then
8264 Binary_Op_Validity_Checks (N);
8266 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
8267 -- means we no longer have a /= operation, we are all done.
8269 Expand_Compare_Minimize_Eliminate_Overflow (N);
8271 if Nkind (N) /= N_Op_Ne then
8272 return;
8273 end if;
8275 -- Boolean types (requiring handling of non-standard case)
8277 if Is_Boolean_Type (Typ) then
8278 Adjust_Condition (Left_Opnd (N));
8279 Adjust_Condition (Right_Opnd (N));
8280 Set_Etype (N, Standard_Boolean);
8281 Adjust_Result_Type (N, Typ);
8282 end if;
8284 Rewrite_Comparison (N);
8286 -- If we still have comparison for Vax_Float, process it
8288 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
8289 Expand_Vax_Comparison (N);
8290 return;
8291 end if;
8293 -- For all cases other than elementary types, we rewrite node as the
8294 -- negation of an equality operation, and reanalyze. The equality to be
8295 -- used is defined in the same scope and has the same signature. This
8296 -- signature must be set explicitly since in an instance it may not have
8297 -- the same visibility as in the generic unit. This avoids duplicating
8298 -- or factoring the complex code for record/array equality tests etc.
8300 else
8301 declare
8302 Loc : constant Source_Ptr := Sloc (N);
8303 Neg : Node_Id;
8304 Ne : constant Entity_Id := Entity (N);
8306 begin
8307 Binary_Op_Validity_Checks (N);
8309 Neg :=
8310 Make_Op_Not (Loc,
8311 Right_Opnd =>
8312 Make_Op_Eq (Loc,
8313 Left_Opnd => Left_Opnd (N),
8314 Right_Opnd => Right_Opnd (N)));
8315 Set_Paren_Count (Right_Opnd (Neg), 1);
8317 if Scope (Ne) /= Standard_Standard then
8318 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
8319 end if;
8321 -- For navigation purposes, we want to treat the inequality as an
8322 -- implicit reference to the corresponding equality. Preserve the
8323 -- Comes_From_ source flag to generate proper Xref entries.
8325 Preserve_Comes_From_Source (Neg, N);
8326 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
8327 Rewrite (N, Neg);
8328 Analyze_And_Resolve (N, Standard_Boolean);
8329 end;
8330 end if;
8332 Optimize_Length_Comparison (N);
8333 end Expand_N_Op_Ne;
8335 ---------------------
8336 -- Expand_N_Op_Not --
8337 ---------------------
8339 -- If the argument is other than a Boolean array type, there is no special
8340 -- expansion required, except for VMS operations on signed integers.
8342 -- For the packed case, we call the special routine in Exp_Pakd, except
8343 -- that if the component size is greater than one, we use the standard
8344 -- routine generating a gruesome loop (it is so peculiar to have packed
8345 -- arrays with non-standard Boolean representations anyway, so it does not
8346 -- matter that we do not handle this case efficiently).
8348 -- For the unpacked case (and for the special packed case where we have non
8349 -- standard Booleans, as discussed above), we generate and insert into the
8350 -- tree the following function definition:
8352 -- function Nnnn (A : arr) is
8353 -- B : arr;
8354 -- begin
8355 -- for J in a'range loop
8356 -- B (J) := not A (J);
8357 -- end loop;
8358 -- return B;
8359 -- end Nnnn;
8361 -- Here arr is the actual subtype of the parameter (and hence always
8362 -- constrained). Then we replace the not with a call to this function.
8364 procedure Expand_N_Op_Not (N : Node_Id) is
8365 Loc : constant Source_Ptr := Sloc (N);
8366 Typ : constant Entity_Id := Etype (N);
8367 Opnd : Node_Id;
8368 Arr : Entity_Id;
8369 A : Entity_Id;
8370 B : Entity_Id;
8371 J : Entity_Id;
8372 A_J : Node_Id;
8373 B_J : Node_Id;
8375 Func_Name : Entity_Id;
8376 Loop_Statement : Node_Id;
8378 begin
8379 Unary_Op_Validity_Checks (N);
8381 -- For boolean operand, deal with non-standard booleans
8383 if Is_Boolean_Type (Typ) then
8384 Adjust_Condition (Right_Opnd (N));
8385 Set_Etype (N, Standard_Boolean);
8386 Adjust_Result_Type (N, Typ);
8387 return;
8388 end if;
8390 -- For the VMS "not" on signed integer types, use conversion to and from
8391 -- a predefined modular type.
8393 if Is_VMS_Operator (Entity (N)) then
8394 declare
8395 Rtyp : Entity_Id;
8396 Utyp : Entity_Id;
8398 begin
8399 -- If this is a derived type, retrieve original VMS type so that
8400 -- the proper sized type is used for intermediate values.
8402 if Is_Derived_Type (Typ) then
8403 Rtyp := First_Subtype (Etype (Typ));
8404 else
8405 Rtyp := Typ;
8406 end if;
8408 -- The proper unsigned type must have a size compatible with the
8409 -- operand, to prevent misalignment.
8411 if RM_Size (Rtyp) <= 8 then
8412 Utyp := RTE (RE_Unsigned_8);
8414 elsif RM_Size (Rtyp) <= 16 then
8415 Utyp := RTE (RE_Unsigned_16);
8417 elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then
8418 Utyp := RTE (RE_Unsigned_32);
8420 else
8421 Utyp := RTE (RE_Long_Long_Unsigned);
8422 end if;
8424 Rewrite (N,
8425 Unchecked_Convert_To (Typ,
8426 Make_Op_Not (Loc,
8427 Unchecked_Convert_To (Utyp, Right_Opnd (N)))));
8428 Analyze_And_Resolve (N, Typ);
8429 return;
8430 end;
8431 end if;
8433 -- Only array types need any other processing
8435 if not Is_Array_Type (Typ) then
8436 return;
8437 end if;
8439 -- Case of array operand. If bit packed with a component size of 1,
8440 -- handle it in Exp_Pakd if the operand is known to be aligned.
8442 if Is_Bit_Packed_Array (Typ)
8443 and then Component_Size (Typ) = 1
8444 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
8445 then
8446 Expand_Packed_Not (N);
8447 return;
8448 end if;
8450 -- Case of array operand which is not bit-packed. If the context is
8451 -- a safe assignment, call in-place operation, If context is a larger
8452 -- boolean expression in the context of a safe assignment, expansion is
8453 -- done by enclosing operation.
8455 Opnd := Relocate_Node (Right_Opnd (N));
8456 Convert_To_Actual_Subtype (Opnd);
8457 Arr := Etype (Opnd);
8458 Ensure_Defined (Arr, N);
8459 Silly_Boolean_Array_Not_Test (N, Arr);
8461 if Nkind (Parent (N)) = N_Assignment_Statement then
8462 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
8463 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8464 return;
8466 -- Special case the negation of a binary operation
8468 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
8469 and then Safe_In_Place_Array_Op
8470 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
8471 then
8472 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8473 return;
8474 end if;
8476 elsif Nkind (Parent (N)) in N_Binary_Op
8477 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
8478 then
8479 declare
8480 Op1 : constant Node_Id := Left_Opnd (Parent (N));
8481 Op2 : constant Node_Id := Right_Opnd (Parent (N));
8482 Lhs : constant Node_Id := Name (Parent (Parent (N)));
8484 begin
8485 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
8487 -- (not A) op (not B) can be reduced to a single call
8489 if N = Op1 and then Nkind (Op2) = N_Op_Not then
8490 return;
8492 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
8493 return;
8495 -- A xor (not B) can also be special-cased
8497 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
8498 return;
8499 end if;
8500 end if;
8501 end;
8502 end if;
8504 A := Make_Defining_Identifier (Loc, Name_uA);
8505 B := Make_Defining_Identifier (Loc, Name_uB);
8506 J := Make_Defining_Identifier (Loc, Name_uJ);
8508 A_J :=
8509 Make_Indexed_Component (Loc,
8510 Prefix => New_Reference_To (A, Loc),
8511 Expressions => New_List (New_Reference_To (J, Loc)));
8513 B_J :=
8514 Make_Indexed_Component (Loc,
8515 Prefix => New_Reference_To (B, Loc),
8516 Expressions => New_List (New_Reference_To (J, Loc)));
8518 Loop_Statement :=
8519 Make_Implicit_Loop_Statement (N,
8520 Identifier => Empty,
8522 Iteration_Scheme =>
8523 Make_Iteration_Scheme (Loc,
8524 Loop_Parameter_Specification =>
8525 Make_Loop_Parameter_Specification (Loc,
8526 Defining_Identifier => J,
8527 Discrete_Subtype_Definition =>
8528 Make_Attribute_Reference (Loc,
8529 Prefix => Make_Identifier (Loc, Chars (A)),
8530 Attribute_Name => Name_Range))),
8532 Statements => New_List (
8533 Make_Assignment_Statement (Loc,
8534 Name => B_J,
8535 Expression => Make_Op_Not (Loc, A_J))));
8537 Func_Name := Make_Temporary (Loc, 'N');
8538 Set_Is_Inlined (Func_Name);
8540 Insert_Action (N,
8541 Make_Subprogram_Body (Loc,
8542 Specification =>
8543 Make_Function_Specification (Loc,
8544 Defining_Unit_Name => Func_Name,
8545 Parameter_Specifications => New_List (
8546 Make_Parameter_Specification (Loc,
8547 Defining_Identifier => A,
8548 Parameter_Type => New_Reference_To (Typ, Loc))),
8549 Result_Definition => New_Reference_To (Typ, Loc)),
8551 Declarations => New_List (
8552 Make_Object_Declaration (Loc,
8553 Defining_Identifier => B,
8554 Object_Definition => New_Reference_To (Arr, Loc))),
8556 Handled_Statement_Sequence =>
8557 Make_Handled_Sequence_Of_Statements (Loc,
8558 Statements => New_List (
8559 Loop_Statement,
8560 Make_Simple_Return_Statement (Loc,
8561 Expression => Make_Identifier (Loc, Chars (B)))))));
8563 Rewrite (N,
8564 Make_Function_Call (Loc,
8565 Name => New_Reference_To (Func_Name, Loc),
8566 Parameter_Associations => New_List (Opnd)));
8568 Analyze_And_Resolve (N, Typ);
8569 end Expand_N_Op_Not;
8571 --------------------
8572 -- Expand_N_Op_Or --
8573 --------------------
8575 procedure Expand_N_Op_Or (N : Node_Id) is
8576 Typ : constant Entity_Id := Etype (N);
8578 begin
8579 Binary_Op_Validity_Checks (N);
8581 if Is_Array_Type (Etype (N)) then
8582 Expand_Boolean_Operator (N);
8584 elsif Is_Boolean_Type (Etype (N)) then
8585 Adjust_Condition (Left_Opnd (N));
8586 Adjust_Condition (Right_Opnd (N));
8587 Set_Etype (N, Standard_Boolean);
8588 Adjust_Result_Type (N, Typ);
8590 elsif Is_Intrinsic_Subprogram (Entity (N)) then
8591 Expand_Intrinsic_Call (N, Entity (N));
8593 end if;
8594 end Expand_N_Op_Or;
8596 ----------------------
8597 -- Expand_N_Op_Plus --
8598 ----------------------
8600 procedure Expand_N_Op_Plus (N : Node_Id) is
8601 begin
8602 Unary_Op_Validity_Checks (N);
8604 -- Check for MINIMIZED/ELIMINATED overflow mode
8606 if Minimized_Eliminated_Overflow_Check (N) then
8607 Apply_Arithmetic_Overflow_Check (N);
8608 return;
8609 end if;
8610 end Expand_N_Op_Plus;
8612 ---------------------
8613 -- Expand_N_Op_Rem --
8614 ---------------------
8616 procedure Expand_N_Op_Rem (N : Node_Id) is
8617 Loc : constant Source_Ptr := Sloc (N);
8618 Typ : constant Entity_Id := Etype (N);
8620 Left : Node_Id;
8621 Right : Node_Id;
8623 Lo : Uint;
8624 Hi : Uint;
8625 OK : Boolean;
8627 Lneg : Boolean;
8628 Rneg : Boolean;
8629 -- Set if corresponding operand can be negative
8631 pragma Unreferenced (Hi);
8633 begin
8634 Binary_Op_Validity_Checks (N);
8636 -- Check for MINIMIZED/ELIMINATED overflow mode
8638 if Minimized_Eliminated_Overflow_Check (N) then
8639 Apply_Arithmetic_Overflow_Check (N);
8640 return;
8641 end if;
8643 if Is_Integer_Type (Etype (N)) then
8644 Apply_Divide_Checks (N);
8646 -- All done if we don't have a REM any more, which can happen as a
8647 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8649 if Nkind (N) /= N_Op_Rem then
8650 return;
8651 end if;
8652 end if;
8654 -- Proceed with expansion of REM
8656 Left := Left_Opnd (N);
8657 Right := Right_Opnd (N);
8659 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
8660 -- but it is useful with other back ends (e.g. AAMP), and is certainly
8661 -- harmless.
8663 if Is_Integer_Type (Etype (N))
8664 and then Compile_Time_Known_Value (Right)
8665 and then Expr_Value (Right) = Uint_1
8666 then
8667 -- Call Remove_Side_Effects to ensure that any side effects in the
8668 -- ignored left operand (in particular function calls to user defined
8669 -- functions) are properly executed.
8671 Remove_Side_Effects (Left);
8673 Rewrite (N, Make_Integer_Literal (Loc, 0));
8674 Analyze_And_Resolve (N, Typ);
8675 return;
8676 end if;
8678 -- Deal with annoying case of largest negative number remainder minus
8679 -- one. Gigi does not handle this case correctly, because it generates
8680 -- a divide instruction which may trap in this case.
8682 -- In fact the check is quite easy, if the right operand is -1, then
8683 -- the remainder is always 0, and we can just ignore the left operand
8684 -- completely in this case.
8686 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
8687 Lneg := (not OK) or else Lo < 0;
8689 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
8690 Rneg := (not OK) or else Lo < 0;
8692 -- We won't mess with trying to find out if the left operand can really
8693 -- be the largest negative number (that's a pain in the case of private
8694 -- types and this is really marginal). We will just assume that we need
8695 -- the test if the left operand can be negative at all.
8697 if Lneg and Rneg then
8698 Rewrite (N,
8699 Make_If_Expression (Loc,
8700 Expressions => New_List (
8701 Make_Op_Eq (Loc,
8702 Left_Opnd => Duplicate_Subexpr (Right),
8703 Right_Opnd =>
8704 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
8706 Unchecked_Convert_To (Typ,
8707 Make_Integer_Literal (Loc, Uint_0)),
8709 Relocate_Node (N))));
8711 Set_Analyzed (Next (Next (First (Expressions (N)))));
8712 Analyze_And_Resolve (N, Typ);
8713 end if;
8714 end Expand_N_Op_Rem;
8716 -----------------------------
8717 -- Expand_N_Op_Rotate_Left --
8718 -----------------------------
8720 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
8721 begin
8722 Binary_Op_Validity_Checks (N);
8723 end Expand_N_Op_Rotate_Left;
8725 ------------------------------
8726 -- Expand_N_Op_Rotate_Right --
8727 ------------------------------
8729 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
8730 begin
8731 Binary_Op_Validity_Checks (N);
8732 end Expand_N_Op_Rotate_Right;
8734 ----------------------------
8735 -- Expand_N_Op_Shift_Left --
8736 ----------------------------
8738 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
8739 begin
8740 Binary_Op_Validity_Checks (N);
8741 end Expand_N_Op_Shift_Left;
8743 -----------------------------
8744 -- Expand_N_Op_Shift_Right --
8745 -----------------------------
8747 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
8748 begin
8749 Binary_Op_Validity_Checks (N);
8750 end Expand_N_Op_Shift_Right;
8752 ----------------------------------------
8753 -- Expand_N_Op_Shift_Right_Arithmetic --
8754 ----------------------------------------
8756 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
8757 begin
8758 Binary_Op_Validity_Checks (N);
8759 end Expand_N_Op_Shift_Right_Arithmetic;
8761 --------------------------
8762 -- Expand_N_Op_Subtract --
8763 --------------------------
8765 procedure Expand_N_Op_Subtract (N : Node_Id) is
8766 Typ : constant Entity_Id := Etype (N);
8768 begin
8769 Binary_Op_Validity_Checks (N);
8771 -- Check for MINIMIZED/ELIMINATED overflow mode
8773 if Minimized_Eliminated_Overflow_Check (N) then
8774 Apply_Arithmetic_Overflow_Check (N);
8775 return;
8776 end if;
8778 -- N - 0 = N for integer types
8780 if Is_Integer_Type (Typ)
8781 and then Compile_Time_Known_Value (Right_Opnd (N))
8782 and then Expr_Value (Right_Opnd (N)) = 0
8783 then
8784 Rewrite (N, Left_Opnd (N));
8785 return;
8786 end if;
8788 -- Arithmetic overflow checks for signed integer/fixed point types
8790 if Is_Signed_Integer_Type (Typ)
8791 or else
8792 Is_Fixed_Point_Type (Typ)
8793 then
8794 Apply_Arithmetic_Overflow_Check (N);
8796 -- VAX floating-point types case
8798 elsif Vax_Float (Typ) then
8799 Expand_Vax_Arith (N);
8800 end if;
8801 end Expand_N_Op_Subtract;
8803 ---------------------
8804 -- Expand_N_Op_Xor --
8805 ---------------------
8807 procedure Expand_N_Op_Xor (N : Node_Id) is
8808 Typ : constant Entity_Id := Etype (N);
8810 begin
8811 Binary_Op_Validity_Checks (N);
8813 if Is_Array_Type (Etype (N)) then
8814 Expand_Boolean_Operator (N);
8816 elsif Is_Boolean_Type (Etype (N)) then
8817 Adjust_Condition (Left_Opnd (N));
8818 Adjust_Condition (Right_Opnd (N));
8819 Set_Etype (N, Standard_Boolean);
8820 Adjust_Result_Type (N, Typ);
8822 elsif Is_Intrinsic_Subprogram (Entity (N)) then
8823 Expand_Intrinsic_Call (N, Entity (N));
8825 end if;
8826 end Expand_N_Op_Xor;
8828 ----------------------
8829 -- Expand_N_Or_Else --
8830 ----------------------
8832 procedure Expand_N_Or_Else (N : Node_Id)
8833 renames Expand_Short_Circuit_Operator;
8835 -----------------------------------
8836 -- Expand_N_Qualified_Expression --
8837 -----------------------------------
8839 procedure Expand_N_Qualified_Expression (N : Node_Id) is
8840 Operand : constant Node_Id := Expression (N);
8841 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
8843 begin
8844 -- Do validity check if validity checking operands
8846 if Validity_Checks_On and then Validity_Check_Operands then
8847 Ensure_Valid (Operand);
8848 end if;
8850 -- Apply possible constraint check
8852 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
8854 if Do_Range_Check (Operand) then
8855 Set_Do_Range_Check (Operand, False);
8856 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
8857 end if;
8858 end Expand_N_Qualified_Expression;
8860 ------------------------------------
8861 -- Expand_N_Quantified_Expression --
8862 ------------------------------------
8864 -- We expand:
8866 -- for all X in range => Cond
8868 -- into:
8870 -- T := True;
8871 -- for X in range loop
8872 -- if not Cond then
8873 -- T := False;
8874 -- exit;
8875 -- end if;
8876 -- end loop;
8878 -- Similarly, an existentially quantified expression:
8880 -- for some X in range => Cond
8882 -- becomes:
8884 -- T := False;
8885 -- for X in range loop
8886 -- if Cond then
8887 -- T := True;
8888 -- exit;
8889 -- end if;
8890 -- end loop;
8892 -- In both cases, the iteration may be over a container in which case it is
8893 -- given by an iterator specification, not a loop parameter specification.
8895 procedure Expand_N_Quantified_Expression (N : Node_Id) is
8896 Actions : constant List_Id := New_List;
8897 For_All : constant Boolean := All_Present (N);
8898 Iter_Spec : constant Node_Id := Iterator_Specification (N);
8899 Loc : constant Source_Ptr := Sloc (N);
8900 Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N);
8901 Cond : Node_Id;
8902 Flag : Entity_Id;
8903 Scheme : Node_Id;
8904 Stmts : List_Id;
8906 begin
8907 -- Create the declaration of the flag which tracks the status of the
8908 -- quantified expression. Generate:
8910 -- Flag : Boolean := (True | False);
8912 Flag := Make_Temporary (Loc, 'T', N);
8914 Append_To (Actions,
8915 Make_Object_Declaration (Loc,
8916 Defining_Identifier => Flag,
8917 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
8918 Expression =>
8919 New_Occurrence_Of (Boolean_Literals (For_All), Loc)));
8921 -- Construct the circuitry which tracks the status of the quantified
8922 -- expression. Generate:
8924 -- if [not] Cond then
8925 -- Flag := (False | True);
8926 -- exit;
8927 -- end if;
8929 Cond := Relocate_Node (Condition (N));
8931 if For_All then
8932 Cond := Make_Op_Not (Loc, Cond);
8933 end if;
8935 Stmts := New_List (
8936 Make_Implicit_If_Statement (N,
8937 Condition => Cond,
8938 Then_Statements => New_List (
8939 Make_Assignment_Statement (Loc,
8940 Name => New_Occurrence_Of (Flag, Loc),
8941 Expression =>
8942 New_Occurrence_Of (Boolean_Literals (not For_All), Loc)),
8943 Make_Exit_Statement (Loc))));
8945 -- Build the loop equivalent of the quantified expression
8947 if Present (Iter_Spec) then
8948 Scheme :=
8949 Make_Iteration_Scheme (Loc,
8950 Iterator_Specification => Iter_Spec);
8951 else
8952 Scheme :=
8953 Make_Iteration_Scheme (Loc,
8954 Loop_Parameter_Specification => Loop_Spec);
8955 end if;
8957 Append_To (Actions,
8958 Make_Loop_Statement (Loc,
8959 Iteration_Scheme => Scheme,
8960 Statements => Stmts,
8961 End_Label => Empty));
8963 -- Transform the quantified expression
8965 Rewrite (N,
8966 Make_Expression_With_Actions (Loc,
8967 Expression => New_Occurrence_Of (Flag, Loc),
8968 Actions => Actions));
8969 Analyze_And_Resolve (N, Standard_Boolean);
8970 end Expand_N_Quantified_Expression;
8972 ---------------------------------
8973 -- Expand_N_Selected_Component --
8974 ---------------------------------
8976 procedure Expand_N_Selected_Component (N : Node_Id) is
8977 Loc : constant Source_Ptr := Sloc (N);
8978 Par : constant Node_Id := Parent (N);
8979 P : constant Node_Id := Prefix (N);
8980 Ptyp : Entity_Id := Underlying_Type (Etype (P));
8981 Disc : Entity_Id;
8982 New_N : Node_Id;
8983 Dcon : Elmt_Id;
8984 Dval : Node_Id;
8986 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
8987 -- Gigi needs a temporary for prefixes that depend on a discriminant,
8988 -- unless the context of an assignment can provide size information.
8989 -- Don't we have a general routine that does this???
8991 function Is_Subtype_Declaration return Boolean;
8992 -- The replacement of a discriminant reference by its value is required
8993 -- if this is part of the initialization of an temporary generated by a
8994 -- change of representation. This shows up as the construction of a
8995 -- discriminant constraint for a subtype declared at the same point as
8996 -- the entity in the prefix of the selected component. We recognize this
8997 -- case when the context of the reference is:
8998 -- subtype ST is T(Obj.D);
8999 -- where the entity for Obj comes from source, and ST has the same sloc.
9001 -----------------------
9002 -- In_Left_Hand_Side --
9003 -----------------------
9005 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
9006 begin
9007 return (Nkind (Parent (Comp)) = N_Assignment_Statement
9008 and then Comp = Name (Parent (Comp)))
9009 or else (Present (Parent (Comp))
9010 and then Nkind (Parent (Comp)) in N_Subexpr
9011 and then In_Left_Hand_Side (Parent (Comp)));
9012 end In_Left_Hand_Side;
9014 -----------------------------
9015 -- Is_Subtype_Declaration --
9016 -----------------------------
9018 function Is_Subtype_Declaration return Boolean is
9019 Par : constant Node_Id := Parent (N);
9020 begin
9021 return
9022 Nkind (Par) = N_Index_Or_Discriminant_Constraint
9023 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
9024 and then Comes_From_Source (Entity (Prefix (N)))
9025 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
9026 end Is_Subtype_Declaration;
9028 -- Start of processing for Expand_N_Selected_Component
9030 begin
9031 -- Insert explicit dereference if required
9033 if Is_Access_Type (Ptyp) then
9035 -- First set prefix type to proper access type, in case it currently
9036 -- has a private (non-access) view of this type.
9038 Set_Etype (P, Ptyp);
9040 Insert_Explicit_Dereference (P);
9041 Analyze_And_Resolve (P, Designated_Type (Ptyp));
9043 if Ekind (Etype (P)) = E_Private_Subtype
9044 and then Is_For_Access_Subtype (Etype (P))
9045 then
9046 Set_Etype (P, Base_Type (Etype (P)));
9047 end if;
9049 Ptyp := Etype (P);
9050 end if;
9052 -- Deal with discriminant check required
9054 if Do_Discriminant_Check (N) then
9056 -- Present the discriminant checking function to the backend, so that
9057 -- it can inline the call to the function.
9059 Add_Inlined_Body
9060 (Discriminant_Checking_Func
9061 (Original_Record_Component (Entity (Selector_Name (N)))));
9063 -- Now reset the flag and generate the call
9065 Set_Do_Discriminant_Check (N, False);
9066 Generate_Discriminant_Check (N);
9067 end if;
9069 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9070 -- function, then additional actuals must be passed.
9072 if Ada_Version >= Ada_2005
9073 and then Is_Build_In_Place_Function_Call (P)
9074 then
9075 Make_Build_In_Place_Call_In_Anonymous_Context (P);
9076 end if;
9078 -- Gigi cannot handle unchecked conversions that are the prefix of a
9079 -- selected component with discriminants. This must be checked during
9080 -- expansion, because during analysis the type of the selector is not
9081 -- known at the point the prefix is analyzed. If the conversion is the
9082 -- target of an assignment, then we cannot force the evaluation.
9084 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
9085 and then Has_Discriminants (Etype (N))
9086 and then not In_Left_Hand_Side (N)
9087 then
9088 Force_Evaluation (Prefix (N));
9089 end if;
9091 -- Remaining processing applies only if selector is a discriminant
9093 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
9095 -- If the selector is a discriminant of a constrained record type,
9096 -- we may be able to rewrite the expression with the actual value
9097 -- of the discriminant, a useful optimization in some cases.
9099 if Is_Record_Type (Ptyp)
9100 and then Has_Discriminants (Ptyp)
9101 and then Is_Constrained (Ptyp)
9102 then
9103 -- Do this optimization for discrete types only, and not for
9104 -- access types (access discriminants get us into trouble!)
9106 if not Is_Discrete_Type (Etype (N)) then
9107 null;
9109 -- Don't do this on the left hand of an assignment statement.
9110 -- Normally one would think that references like this would not
9111 -- occur, but they do in generated code, and mean that we really
9112 -- do want to assign the discriminant!
9114 elsif Nkind (Par) = N_Assignment_Statement
9115 and then Name (Par) = N
9116 then
9117 null;
9119 -- Don't do this optimization for the prefix of an attribute or
9120 -- the name of an object renaming declaration since these are
9121 -- contexts where we do not want the value anyway.
9123 elsif (Nkind (Par) = N_Attribute_Reference
9124 and then Prefix (Par) = N)
9125 or else Is_Renamed_Object (N)
9126 then
9127 null;
9129 -- Don't do this optimization if we are within the code for a
9130 -- discriminant check, since the whole point of such a check may
9131 -- be to verify the condition on which the code below depends!
9133 elsif Is_In_Discriminant_Check (N) then
9134 null;
9136 -- Green light to see if we can do the optimization. There is
9137 -- still one condition that inhibits the optimization below but
9138 -- now is the time to check the particular discriminant.
9140 else
9141 -- Loop through discriminants to find the matching discriminant
9142 -- constraint to see if we can copy it.
9144 Disc := First_Discriminant (Ptyp);
9145 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
9146 Discr_Loop : while Present (Dcon) loop
9147 Dval := Node (Dcon);
9149 -- Check if this is the matching discriminant and if the
9150 -- discriminant value is simple enough to make sense to
9151 -- copy. We don't want to copy complex expressions, and
9152 -- indeed to do so can cause trouble (before we put in
9153 -- this guard, a discriminant expression containing an
9154 -- AND THEN was copied, causing problems for coverage
9155 -- analysis tools).
9157 -- However, if the reference is part of the initialization
9158 -- code generated for an object declaration, we must use
9159 -- the discriminant value from the subtype constraint,
9160 -- because the selected component may be a reference to the
9161 -- object being initialized, whose discriminant is not yet
9162 -- set. This only happens in complex cases involving changes
9163 -- or representation.
9165 if Disc = Entity (Selector_Name (N))
9166 and then (Is_Entity_Name (Dval)
9167 or else Compile_Time_Known_Value (Dval)
9168 or else Is_Subtype_Declaration)
9169 then
9170 -- Here we have the matching discriminant. Check for
9171 -- the case of a discriminant of a component that is
9172 -- constrained by an outer discriminant, which cannot
9173 -- be optimized away.
9175 if Denotes_Discriminant
9176 (Dval, Check_Concurrent => True)
9177 then
9178 exit Discr_Loop;
9180 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
9181 and then
9182 Denotes_Discriminant
9183 (Selector_Name (Original_Node (Dval)), True)
9184 then
9185 exit Discr_Loop;
9187 -- Do not retrieve value if constraint is not static. It
9188 -- is generally not useful, and the constraint may be a
9189 -- rewritten outer discriminant in which case it is in
9190 -- fact incorrect.
9192 elsif Is_Entity_Name (Dval)
9193 and then Nkind (Parent (Entity (Dval))) =
9194 N_Object_Declaration
9195 and then Present (Expression (Parent (Entity (Dval))))
9196 and then
9197 not Is_Static_Expression
9198 (Expression (Parent (Entity (Dval))))
9199 then
9200 exit Discr_Loop;
9202 -- In the context of a case statement, the expression may
9203 -- have the base type of the discriminant, and we need to
9204 -- preserve the constraint to avoid spurious errors on
9205 -- missing cases.
9207 elsif Nkind (Parent (N)) = N_Case_Statement
9208 and then Etype (Dval) /= Etype (Disc)
9209 then
9210 Rewrite (N,
9211 Make_Qualified_Expression (Loc,
9212 Subtype_Mark =>
9213 New_Occurrence_Of (Etype (Disc), Loc),
9214 Expression =>
9215 New_Copy_Tree (Dval)));
9216 Analyze_And_Resolve (N, Etype (Disc));
9218 -- In case that comes out as a static expression,
9219 -- reset it (a selected component is never static).
9221 Set_Is_Static_Expression (N, False);
9222 return;
9224 -- Otherwise we can just copy the constraint, but the
9225 -- result is certainly not static! In some cases the
9226 -- discriminant constraint has been analyzed in the
9227 -- context of the original subtype indication, but for
9228 -- itypes the constraint might not have been analyzed
9229 -- yet, and this must be done now.
9231 else
9232 Rewrite (N, New_Copy_Tree (Dval));
9233 Analyze_And_Resolve (N);
9234 Set_Is_Static_Expression (N, False);
9235 return;
9236 end if;
9237 end if;
9239 Next_Elmt (Dcon);
9240 Next_Discriminant (Disc);
9241 end loop Discr_Loop;
9243 -- Note: the above loop should always find a matching
9244 -- discriminant, but if it does not, we just missed an
9245 -- optimization due to some glitch (perhaps a previous
9246 -- error), so ignore.
9248 end if;
9249 end if;
9251 -- The only remaining processing is in the case of a discriminant of
9252 -- a concurrent object, where we rewrite the prefix to denote the
9253 -- corresponding record type. If the type is derived and has renamed
9254 -- discriminants, use corresponding discriminant, which is the one
9255 -- that appears in the corresponding record.
9257 if not Is_Concurrent_Type (Ptyp) then
9258 return;
9259 end if;
9261 Disc := Entity (Selector_Name (N));
9263 if Is_Derived_Type (Ptyp)
9264 and then Present (Corresponding_Discriminant (Disc))
9265 then
9266 Disc := Corresponding_Discriminant (Disc);
9267 end if;
9269 New_N :=
9270 Make_Selected_Component (Loc,
9271 Prefix =>
9272 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
9273 New_Copy_Tree (P)),
9274 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
9276 Rewrite (N, New_N);
9277 Analyze (N);
9278 end if;
9280 -- Set Atomic_Sync_Required if necessary for atomic component
9282 if Nkind (N) = N_Selected_Component then
9283 declare
9284 E : constant Entity_Id := Entity (Selector_Name (N));
9285 Set : Boolean;
9287 begin
9288 -- If component is atomic, but type is not, setting depends on
9289 -- disable/enable state for the component.
9291 if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then
9292 Set := not Atomic_Synchronization_Disabled (E);
9294 -- If component is not atomic, but its type is atomic, setting
9295 -- depends on disable/enable state for the type.
9297 elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9298 Set := not Atomic_Synchronization_Disabled (Etype (E));
9300 -- If both component and type are atomic, we disable if either
9301 -- component or its type have sync disabled.
9303 elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9304 Set := (not Atomic_Synchronization_Disabled (E))
9305 and then
9306 (not Atomic_Synchronization_Disabled (Etype (E)));
9308 else
9309 Set := False;
9310 end if;
9312 -- Set flag if required
9314 if Set then
9315 Activate_Atomic_Synchronization (N);
9316 end if;
9317 end;
9318 end if;
9319 end Expand_N_Selected_Component;
9321 --------------------
9322 -- Expand_N_Slice --
9323 --------------------
9325 procedure Expand_N_Slice (N : Node_Id) is
9326 Loc : constant Source_Ptr := Sloc (N);
9327 Typ : constant Entity_Id := Etype (N);
9328 Pfx : constant Node_Id := Prefix (N);
9329 Ptp : Entity_Id := Etype (Pfx);
9331 function Is_Procedure_Actual (N : Node_Id) return Boolean;
9332 -- Check whether the argument is an actual for a procedure call, in
9333 -- which case the expansion of a bit-packed slice is deferred until the
9334 -- call itself is expanded. The reason this is required is that we might
9335 -- have an IN OUT or OUT parameter, and the copy out is essential, and
9336 -- that copy out would be missed if we created a temporary here in
9337 -- Expand_N_Slice. Note that we don't bother to test specifically for an
9338 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
9339 -- is harmless to defer expansion in the IN case, since the call
9340 -- processing will still generate the appropriate copy in operation,
9341 -- which will take care of the slice.
9343 procedure Make_Temporary_For_Slice;
9344 -- Create a named variable for the value of the slice, in cases where
9345 -- the back-end cannot handle it properly, e.g. when packed types or
9346 -- unaligned slices are involved.
9348 -------------------------
9349 -- Is_Procedure_Actual --
9350 -------------------------
9352 function Is_Procedure_Actual (N : Node_Id) return Boolean is
9353 Par : Node_Id := Parent (N);
9355 begin
9356 loop
9357 -- If our parent is a procedure call we can return
9359 if Nkind (Par) = N_Procedure_Call_Statement then
9360 return True;
9362 -- If our parent is a type conversion, keep climbing the tree,
9363 -- since a type conversion can be a procedure actual. Also keep
9364 -- climbing if parameter association or a qualified expression,
9365 -- since these are additional cases that do can appear on
9366 -- procedure actuals.
9368 elsif Nkind_In (Par, N_Type_Conversion,
9369 N_Parameter_Association,
9370 N_Qualified_Expression)
9371 then
9372 Par := Parent (Par);
9374 -- Any other case is not what we are looking for
9376 else
9377 return False;
9378 end if;
9379 end loop;
9380 end Is_Procedure_Actual;
9382 ------------------------------
9383 -- Make_Temporary_For_Slice --
9384 ------------------------------
9386 procedure Make_Temporary_For_Slice is
9387 Decl : Node_Id;
9388 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
9390 begin
9391 Decl :=
9392 Make_Object_Declaration (Loc,
9393 Defining_Identifier => Ent,
9394 Object_Definition => New_Occurrence_Of (Typ, Loc));
9396 Set_No_Initialization (Decl);
9398 Insert_Actions (N, New_List (
9399 Decl,
9400 Make_Assignment_Statement (Loc,
9401 Name => New_Occurrence_Of (Ent, Loc),
9402 Expression => Relocate_Node (N))));
9404 Rewrite (N, New_Occurrence_Of (Ent, Loc));
9405 Analyze_And_Resolve (N, Typ);
9406 end Make_Temporary_For_Slice;
9408 -- Start of processing for Expand_N_Slice
9410 begin
9411 -- Special handling for access types
9413 if Is_Access_Type (Ptp) then
9415 Ptp := Designated_Type (Ptp);
9417 Rewrite (Pfx,
9418 Make_Explicit_Dereference (Sloc (N),
9419 Prefix => Relocate_Node (Pfx)));
9421 Analyze_And_Resolve (Pfx, Ptp);
9422 end if;
9424 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9425 -- function, then additional actuals must be passed.
9427 if Ada_Version >= Ada_2005
9428 and then Is_Build_In_Place_Function_Call (Pfx)
9429 then
9430 Make_Build_In_Place_Call_In_Anonymous_Context (Pfx);
9431 end if;
9433 -- The remaining case to be handled is packed slices. We can leave
9434 -- packed slices as they are in the following situations:
9436 -- 1. Right or left side of an assignment (we can handle this
9437 -- situation correctly in the assignment statement expansion).
9439 -- 2. Prefix of indexed component (the slide is optimized away in this
9440 -- case, see the start of Expand_N_Slice.)
9442 -- 3. Object renaming declaration, since we want the name of the
9443 -- slice, not the value.
9445 -- 4. Argument to procedure call, since copy-in/copy-out handling may
9446 -- be required, and this is handled in the expansion of call
9447 -- itself.
9449 -- 5. Prefix of an address attribute (this is an error which is caught
9450 -- elsewhere, and the expansion would interfere with generating the
9451 -- error message).
9453 if not Is_Packed (Typ) then
9455 -- Apply transformation for actuals of a function call, where
9456 -- Expand_Actuals is not used.
9458 if Nkind (Parent (N)) = N_Function_Call
9459 and then Is_Possibly_Unaligned_Slice (N)
9460 then
9461 Make_Temporary_For_Slice;
9462 end if;
9464 elsif Nkind (Parent (N)) = N_Assignment_Statement
9465 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
9466 and then Parent (N) = Name (Parent (Parent (N))))
9467 then
9468 return;
9470 elsif Nkind (Parent (N)) = N_Indexed_Component
9471 or else Is_Renamed_Object (N)
9472 or else Is_Procedure_Actual (N)
9473 then
9474 return;
9476 elsif Nkind (Parent (N)) = N_Attribute_Reference
9477 and then Attribute_Name (Parent (N)) = Name_Address
9478 then
9479 return;
9481 else
9482 Make_Temporary_For_Slice;
9483 end if;
9484 end Expand_N_Slice;
9486 ------------------------------
9487 -- Expand_N_Type_Conversion --
9488 ------------------------------
9490 procedure Expand_N_Type_Conversion (N : Node_Id) is
9491 Loc : constant Source_Ptr := Sloc (N);
9492 Operand : constant Node_Id := Expression (N);
9493 Target_Type : constant Entity_Id := Etype (N);
9494 Operand_Type : Entity_Id := Etype (Operand);
9496 procedure Handle_Changed_Representation;
9497 -- This is called in the case of record and array type conversions to
9498 -- see if there is a change of representation to be handled. Change of
9499 -- representation is actually handled at the assignment statement level,
9500 -- and what this procedure does is rewrite node N conversion as an
9501 -- assignment to temporary. If there is no change of representation,
9502 -- then the conversion node is unchanged.
9504 procedure Raise_Accessibility_Error;
9505 -- Called when we know that an accessibility check will fail. Rewrites
9506 -- node N to an appropriate raise statement and outputs warning msgs.
9507 -- The Etype of the raise node is set to Target_Type.
9509 procedure Real_Range_Check;
9510 -- Handles generation of range check for real target value
9512 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean;
9513 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
9514 -- evaluates to True.
9516 -----------------------------------
9517 -- Handle_Changed_Representation --
9518 -----------------------------------
9520 procedure Handle_Changed_Representation is
9521 Temp : Entity_Id;
9522 Decl : Node_Id;
9523 Odef : Node_Id;
9524 Disc : Node_Id;
9525 N_Ix : Node_Id;
9526 Cons : List_Id;
9528 begin
9529 -- Nothing else to do if no change of representation
9531 if Same_Representation (Operand_Type, Target_Type) then
9532 return;
9534 -- The real change of representation work is done by the assignment
9535 -- statement processing. So if this type conversion is appearing as
9536 -- the expression of an assignment statement, nothing needs to be
9537 -- done to the conversion.
9539 elsif Nkind (Parent (N)) = N_Assignment_Statement then
9540 return;
9542 -- Otherwise we need to generate a temporary variable, and do the
9543 -- change of representation assignment into that temporary variable.
9544 -- The conversion is then replaced by a reference to this variable.
9546 else
9547 Cons := No_List;
9549 -- If type is unconstrained we have to add a constraint, copied
9550 -- from the actual value of the left hand side.
9552 if not Is_Constrained (Target_Type) then
9553 if Has_Discriminants (Operand_Type) then
9554 Disc := First_Discriminant (Operand_Type);
9556 if Disc /= First_Stored_Discriminant (Operand_Type) then
9557 Disc := First_Stored_Discriminant (Operand_Type);
9558 end if;
9560 Cons := New_List;
9561 while Present (Disc) loop
9562 Append_To (Cons,
9563 Make_Selected_Component (Loc,
9564 Prefix =>
9565 Duplicate_Subexpr_Move_Checks (Operand),
9566 Selector_Name =>
9567 Make_Identifier (Loc, Chars (Disc))));
9568 Next_Discriminant (Disc);
9569 end loop;
9571 elsif Is_Array_Type (Operand_Type) then
9572 N_Ix := First_Index (Target_Type);
9573 Cons := New_List;
9575 for J in 1 .. Number_Dimensions (Operand_Type) loop
9577 -- We convert the bounds explicitly. We use an unchecked
9578 -- conversion because bounds checks are done elsewhere.
9580 Append_To (Cons,
9581 Make_Range (Loc,
9582 Low_Bound =>
9583 Unchecked_Convert_To (Etype (N_Ix),
9584 Make_Attribute_Reference (Loc,
9585 Prefix =>
9586 Duplicate_Subexpr_No_Checks
9587 (Operand, Name_Req => True),
9588 Attribute_Name => Name_First,
9589 Expressions => New_List (
9590 Make_Integer_Literal (Loc, J)))),
9592 High_Bound =>
9593 Unchecked_Convert_To (Etype (N_Ix),
9594 Make_Attribute_Reference (Loc,
9595 Prefix =>
9596 Duplicate_Subexpr_No_Checks
9597 (Operand, Name_Req => True),
9598 Attribute_Name => Name_Last,
9599 Expressions => New_List (
9600 Make_Integer_Literal (Loc, J))))));
9602 Next_Index (N_Ix);
9603 end loop;
9604 end if;
9605 end if;
9607 Odef := New_Occurrence_Of (Target_Type, Loc);
9609 if Present (Cons) then
9610 Odef :=
9611 Make_Subtype_Indication (Loc,
9612 Subtype_Mark => Odef,
9613 Constraint =>
9614 Make_Index_Or_Discriminant_Constraint (Loc,
9615 Constraints => Cons));
9616 end if;
9618 Temp := Make_Temporary (Loc, 'C');
9619 Decl :=
9620 Make_Object_Declaration (Loc,
9621 Defining_Identifier => Temp,
9622 Object_Definition => Odef);
9624 Set_No_Initialization (Decl, True);
9626 -- Insert required actions. It is essential to suppress checks
9627 -- since we have suppressed default initialization, which means
9628 -- that the variable we create may have no discriminants.
9630 Insert_Actions (N,
9631 New_List (
9632 Decl,
9633 Make_Assignment_Statement (Loc,
9634 Name => New_Occurrence_Of (Temp, Loc),
9635 Expression => Relocate_Node (N))),
9636 Suppress => All_Checks);
9638 Rewrite (N, New_Occurrence_Of (Temp, Loc));
9639 return;
9640 end if;
9641 end Handle_Changed_Representation;
9643 -------------------------------
9644 -- Raise_Accessibility_Error --
9645 -------------------------------
9647 procedure Raise_Accessibility_Error is
9648 begin
9649 Rewrite (N,
9650 Make_Raise_Program_Error (Sloc (N),
9651 Reason => PE_Accessibility_Check_Failed));
9652 Set_Etype (N, Target_Type);
9654 Error_Msg_N ("?accessibility check failure", N);
9655 Error_Msg_NE
9656 ("\?& will be raised at run time", N, Standard_Program_Error);
9657 end Raise_Accessibility_Error;
9659 ----------------------
9660 -- Real_Range_Check --
9661 ----------------------
9663 -- Case of conversions to floating-point or fixed-point. If range checks
9664 -- are enabled and the target type has a range constraint, we convert:
9666 -- typ (x)
9668 -- to
9670 -- Tnn : typ'Base := typ'Base (x);
9671 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
9672 -- Tnn
9674 -- This is necessary when there is a conversion of integer to float or
9675 -- to fixed-point to ensure that the correct checks are made. It is not
9676 -- necessary for float to float where it is enough to simply set the
9677 -- Do_Range_Check flag.
9679 procedure Real_Range_Check is
9680 Btyp : constant Entity_Id := Base_Type (Target_Type);
9681 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
9682 Hi : constant Node_Id := Type_High_Bound (Target_Type);
9683 Xtyp : constant Entity_Id := Etype (Operand);
9684 Conv : Node_Id;
9685 Tnn : Entity_Id;
9687 begin
9688 -- Nothing to do if conversion was rewritten
9690 if Nkind (N) /= N_Type_Conversion then
9691 return;
9692 end if;
9694 -- Nothing to do if range checks suppressed, or target has the same
9695 -- range as the base type (or is the base type).
9697 if Range_Checks_Suppressed (Target_Type)
9698 or else (Lo = Type_Low_Bound (Btyp)
9699 and then
9700 Hi = Type_High_Bound (Btyp))
9701 then
9702 return;
9703 end if;
9705 -- Nothing to do if expression is an entity on which checks have been
9706 -- suppressed.
9708 if Is_Entity_Name (Operand)
9709 and then Range_Checks_Suppressed (Entity (Operand))
9710 then
9711 return;
9712 end if;
9714 -- Nothing to do if bounds are all static and we can tell that the
9715 -- expression is within the bounds of the target. Note that if the
9716 -- operand is of an unconstrained floating-point type, then we do
9717 -- not trust it to be in range (might be infinite)
9719 declare
9720 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
9721 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
9723 begin
9724 if (not Is_Floating_Point_Type (Xtyp)
9725 or else Is_Constrained (Xtyp))
9726 and then Compile_Time_Known_Value (S_Lo)
9727 and then Compile_Time_Known_Value (S_Hi)
9728 and then Compile_Time_Known_Value (Hi)
9729 and then Compile_Time_Known_Value (Lo)
9730 then
9731 declare
9732 D_Lov : constant Ureal := Expr_Value_R (Lo);
9733 D_Hiv : constant Ureal := Expr_Value_R (Hi);
9734 S_Lov : Ureal;
9735 S_Hiv : Ureal;
9737 begin
9738 if Is_Real_Type (Xtyp) then
9739 S_Lov := Expr_Value_R (S_Lo);
9740 S_Hiv := Expr_Value_R (S_Hi);
9741 else
9742 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
9743 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
9744 end if;
9746 if D_Hiv > D_Lov
9747 and then S_Lov >= D_Lov
9748 and then S_Hiv <= D_Hiv
9749 then
9750 Set_Do_Range_Check (Operand, False);
9751 return;
9752 end if;
9753 end;
9754 end if;
9755 end;
9757 -- For float to float conversions, we are done
9759 if Is_Floating_Point_Type (Xtyp)
9760 and then
9761 Is_Floating_Point_Type (Btyp)
9762 then
9763 return;
9764 end if;
9766 -- Otherwise rewrite the conversion as described above
9768 Conv := Relocate_Node (N);
9769 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
9770 Set_Etype (Conv, Btyp);
9772 -- Enable overflow except for case of integer to float conversions,
9773 -- where it is never required, since we can never have overflow in
9774 -- this case.
9776 if not Is_Integer_Type (Etype (Operand)) then
9777 Enable_Overflow_Check (Conv);
9778 end if;
9780 Tnn := Make_Temporary (Loc, 'T', Conv);
9782 Insert_Actions (N, New_List (
9783 Make_Object_Declaration (Loc,
9784 Defining_Identifier => Tnn,
9785 Object_Definition => New_Occurrence_Of (Btyp, Loc),
9786 Constant_Present => True,
9787 Expression => Conv),
9789 Make_Raise_Constraint_Error (Loc,
9790 Condition =>
9791 Make_Or_Else (Loc,
9792 Left_Opnd =>
9793 Make_Op_Lt (Loc,
9794 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
9795 Right_Opnd =>
9796 Make_Attribute_Reference (Loc,
9797 Attribute_Name => Name_First,
9798 Prefix =>
9799 New_Occurrence_Of (Target_Type, Loc))),
9801 Right_Opnd =>
9802 Make_Op_Gt (Loc,
9803 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
9804 Right_Opnd =>
9805 Make_Attribute_Reference (Loc,
9806 Attribute_Name => Name_Last,
9807 Prefix =>
9808 New_Occurrence_Of (Target_Type, Loc)))),
9809 Reason => CE_Range_Check_Failed)));
9811 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
9812 Analyze_And_Resolve (N, Btyp);
9813 end Real_Range_Check;
9815 -----------------------------
9816 -- Has_Extra_Accessibility --
9817 -----------------------------
9819 -- Returns true for a formal of an anonymous access type or for
9820 -- an Ada 2012-style stand-alone object of an anonymous access type.
9822 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is
9823 begin
9824 if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then
9825 return Present (Effective_Extra_Accessibility (Id));
9826 else
9827 return False;
9828 end if;
9829 end Has_Extra_Accessibility;
9831 -- Start of processing for Expand_N_Type_Conversion
9833 begin
9834 -- Nothing at all to do if conversion is to the identical type so remove
9835 -- the conversion completely, it is useless, except that it may carry
9836 -- an Assignment_OK attribute, which must be propagated to the operand.
9838 if Operand_Type = Target_Type then
9839 if Assignment_OK (N) then
9840 Set_Assignment_OK (Operand);
9841 end if;
9843 Rewrite (N, Relocate_Node (Operand));
9844 goto Done;
9845 end if;
9847 -- Nothing to do if this is the second argument of read. This is a
9848 -- "backwards" conversion that will be handled by the specialized code
9849 -- in attribute processing.
9851 if Nkind (Parent (N)) = N_Attribute_Reference
9852 and then Attribute_Name (Parent (N)) = Name_Read
9853 and then Next (First (Expressions (Parent (N)))) = N
9854 then
9855 goto Done;
9856 end if;
9858 -- Check for case of converting to a type that has an invariant
9859 -- associated with it. This required an invariant check. We convert
9861 -- typ (expr)
9863 -- into
9865 -- do invariant_check (typ (expr)) in typ (expr);
9867 -- using Duplicate_Subexpr to avoid multiple side effects
9869 -- Note: the Comes_From_Source check, and then the resetting of this
9870 -- flag prevents what would otherwise be an infinite recursion.
9872 if Has_Invariants (Target_Type)
9873 and then Present (Invariant_Procedure (Target_Type))
9874 and then Comes_From_Source (N)
9875 then
9876 Set_Comes_From_Source (N, False);
9877 Rewrite (N,
9878 Make_Expression_With_Actions (Loc,
9879 Actions => New_List (
9880 Make_Invariant_Call (Duplicate_Subexpr (N))),
9881 Expression => Duplicate_Subexpr_No_Checks (N)));
9882 Analyze_And_Resolve (N, Target_Type);
9883 goto Done;
9884 end if;
9886 -- Here if we may need to expand conversion
9888 -- If the operand of the type conversion is an arithmetic operation on
9889 -- signed integers, and the based type of the signed integer type in
9890 -- question is smaller than Standard.Integer, we promote both of the
9891 -- operands to type Integer.
9893 -- For example, if we have
9895 -- target-type (opnd1 + opnd2)
9897 -- and opnd1 and opnd2 are of type short integer, then we rewrite
9898 -- this as:
9900 -- target-type (integer(opnd1) + integer(opnd2))
9902 -- We do this because we are always allowed to compute in a larger type
9903 -- if we do the right thing with the result, and in this case we are
9904 -- going to do a conversion which will do an appropriate check to make
9905 -- sure that things are in range of the target type in any case. This
9906 -- avoids some unnecessary intermediate overflows.
9908 -- We might consider a similar transformation in the case where the
9909 -- target is a real type or a 64-bit integer type, and the operand
9910 -- is an arithmetic operation using a 32-bit integer type. However,
9911 -- we do not bother with this case, because it could cause significant
9912 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
9913 -- much cheaper, but we don't want different behavior on 32-bit and
9914 -- 64-bit machines. Note that the exclusion of the 64-bit case also
9915 -- handles the configurable run-time cases where 64-bit arithmetic
9916 -- may simply be unavailable.
9918 -- Note: this circuit is partially redundant with respect to the circuit
9919 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
9920 -- the processing here. Also we still need the Checks circuit, since we
9921 -- have to be sure not to generate junk overflow checks in the first
9922 -- place, since it would be trick to remove them here!
9924 if Integer_Promotion_Possible (N) then
9926 -- All conditions met, go ahead with transformation
9928 declare
9929 Opnd : Node_Id;
9930 L, R : Node_Id;
9932 begin
9933 R :=
9934 Make_Type_Conversion (Loc,
9935 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
9936 Expression => Relocate_Node (Right_Opnd (Operand)));
9938 Opnd := New_Op_Node (Nkind (Operand), Loc);
9939 Set_Right_Opnd (Opnd, R);
9941 if Nkind (Operand) in N_Binary_Op then
9942 L :=
9943 Make_Type_Conversion (Loc,
9944 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
9945 Expression => Relocate_Node (Left_Opnd (Operand)));
9947 Set_Left_Opnd (Opnd, L);
9948 end if;
9950 Rewrite (N,
9951 Make_Type_Conversion (Loc,
9952 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
9953 Expression => Opnd));
9955 Analyze_And_Resolve (N, Target_Type);
9956 goto Done;
9957 end;
9958 end if;
9960 -- Do validity check if validity checking operands
9962 if Validity_Checks_On
9963 and then Validity_Check_Operands
9964 then
9965 Ensure_Valid (Operand);
9966 end if;
9968 -- Special case of converting from non-standard boolean type
9970 if Is_Boolean_Type (Operand_Type)
9971 and then (Nonzero_Is_True (Operand_Type))
9972 then
9973 Adjust_Condition (Operand);
9974 Set_Etype (Operand, Standard_Boolean);
9975 Operand_Type := Standard_Boolean;
9976 end if;
9978 -- Case of converting to an access type
9980 if Is_Access_Type (Target_Type) then
9982 -- Apply an accessibility check when the conversion operand is an
9983 -- access parameter (or a renaming thereof), unless conversion was
9984 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
9985 -- Note that other checks may still need to be applied below (such
9986 -- as tagged type checks).
9988 if Is_Entity_Name (Operand)
9989 and then Has_Extra_Accessibility (Entity (Operand))
9990 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
9991 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
9992 or else Attribute_Name (Original_Node (N)) = Name_Access)
9993 then
9994 Apply_Accessibility_Check
9995 (Operand, Target_Type, Insert_Node => Operand);
9997 -- If the level of the operand type is statically deeper than the
9998 -- level of the target type, then force Program_Error. Note that this
9999 -- can only occur for cases where the attribute is within the body of
10000 -- an instantiation (otherwise the conversion will already have been
10001 -- rejected as illegal). Note: warnings are issued by the analyzer
10002 -- for the instance cases.
10004 elsif In_Instance_Body
10005 and then Type_Access_Level (Operand_Type) >
10006 Type_Access_Level (Target_Type)
10007 then
10008 Raise_Accessibility_Error;
10010 -- When the operand is a selected access discriminant the check needs
10011 -- to be made against the level of the object denoted by the prefix
10012 -- of the selected name. Force Program_Error for this case as well
10013 -- (this accessibility violation can only happen if within the body
10014 -- of an instantiation).
10016 elsif In_Instance_Body
10017 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
10018 and then Nkind (Operand) = N_Selected_Component
10019 and then Object_Access_Level (Operand) >
10020 Type_Access_Level (Target_Type)
10021 then
10022 Raise_Accessibility_Error;
10023 goto Done;
10024 end if;
10025 end if;
10027 -- Case of conversions of tagged types and access to tagged types
10029 -- When needed, that is to say when the expression is class-wide, Add
10030 -- runtime a tag check for (strict) downward conversion by using the
10031 -- membership test, generating:
10033 -- [constraint_error when Operand not in Target_Type'Class]
10035 -- or in the access type case
10037 -- [constraint_error
10038 -- when Operand /= null
10039 -- and then Operand.all not in
10040 -- Designated_Type (Target_Type)'Class]
10042 if (Is_Access_Type (Target_Type)
10043 and then Is_Tagged_Type (Designated_Type (Target_Type)))
10044 or else Is_Tagged_Type (Target_Type)
10045 then
10046 -- Do not do any expansion in the access type case if the parent is a
10047 -- renaming, since this is an error situation which will be caught by
10048 -- Sem_Ch8, and the expansion can interfere with this error check.
10050 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
10051 goto Done;
10052 end if;
10054 -- Otherwise, proceed with processing tagged conversion
10056 Tagged_Conversion : declare
10057 Actual_Op_Typ : Entity_Id;
10058 Actual_Targ_Typ : Entity_Id;
10059 Make_Conversion : Boolean := False;
10060 Root_Op_Typ : Entity_Id;
10062 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
10063 -- Create a membership check to test whether Operand is a member
10064 -- of Targ_Typ. If the original Target_Type is an access, include
10065 -- a test for null value. The check is inserted at N.
10067 --------------------
10068 -- Make_Tag_Check --
10069 --------------------
10071 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
10072 Cond : Node_Id;
10074 begin
10075 -- Generate:
10076 -- [Constraint_Error
10077 -- when Operand /= null
10078 -- and then Operand.all not in Targ_Typ]
10080 if Is_Access_Type (Target_Type) then
10081 Cond :=
10082 Make_And_Then (Loc,
10083 Left_Opnd =>
10084 Make_Op_Ne (Loc,
10085 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10086 Right_Opnd => Make_Null (Loc)),
10088 Right_Opnd =>
10089 Make_Not_In (Loc,
10090 Left_Opnd =>
10091 Make_Explicit_Dereference (Loc,
10092 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
10093 Right_Opnd => New_Reference_To (Targ_Typ, Loc)));
10095 -- Generate:
10096 -- [Constraint_Error when Operand not in Targ_Typ]
10098 else
10099 Cond :=
10100 Make_Not_In (Loc,
10101 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10102 Right_Opnd => New_Reference_To (Targ_Typ, Loc));
10103 end if;
10105 Insert_Action (N,
10106 Make_Raise_Constraint_Error (Loc,
10107 Condition => Cond,
10108 Reason => CE_Tag_Check_Failed));
10109 end Make_Tag_Check;
10111 -- Start of processing for Tagged_Conversion
10113 begin
10114 -- Handle entities from the limited view
10116 if Is_Access_Type (Operand_Type) then
10117 Actual_Op_Typ :=
10118 Available_View (Designated_Type (Operand_Type));
10119 else
10120 Actual_Op_Typ := Operand_Type;
10121 end if;
10123 if Is_Access_Type (Target_Type) then
10124 Actual_Targ_Typ :=
10125 Available_View (Designated_Type (Target_Type));
10126 else
10127 Actual_Targ_Typ := Target_Type;
10128 end if;
10130 Root_Op_Typ := Root_Type (Actual_Op_Typ);
10132 -- Ada 2005 (AI-251): Handle interface type conversion
10134 if Is_Interface (Actual_Op_Typ) then
10135 Expand_Interface_Conversion (N, Is_Static => False);
10136 goto Done;
10137 end if;
10139 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
10141 -- Create a runtime tag check for a downward class-wide type
10142 -- conversion.
10144 if Is_Class_Wide_Type (Actual_Op_Typ)
10145 and then Actual_Op_Typ /= Actual_Targ_Typ
10146 and then Root_Op_Typ /= Actual_Targ_Typ
10147 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ,
10148 Use_Full_View => True)
10149 then
10150 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
10151 Make_Conversion := True;
10152 end if;
10154 -- AI05-0073: If the result subtype of the function is defined
10155 -- by an access_definition designating a specific tagged type
10156 -- T, a check is made that the result value is null or the tag
10157 -- of the object designated by the result value identifies T.
10158 -- Constraint_Error is raised if this check fails.
10160 if Nkind (Parent (N)) = N_Simple_Return_Statement then
10161 declare
10162 Func : Entity_Id;
10163 Func_Typ : Entity_Id;
10165 begin
10166 -- Climb scope stack looking for the enclosing function
10168 Func := Current_Scope;
10169 while Present (Func)
10170 and then Ekind (Func) /= E_Function
10171 loop
10172 Func := Scope (Func);
10173 end loop;
10175 -- The function's return subtype must be defined using
10176 -- an access definition.
10178 if Nkind (Result_Definition (Parent (Func))) =
10179 N_Access_Definition
10180 then
10181 Func_Typ := Directly_Designated_Type (Etype (Func));
10183 -- The return subtype denotes a specific tagged type,
10184 -- in other words, a non class-wide type.
10186 if Is_Tagged_Type (Func_Typ)
10187 and then not Is_Class_Wide_Type (Func_Typ)
10188 then
10189 Make_Tag_Check (Actual_Targ_Typ);
10190 Make_Conversion := True;
10191 end if;
10192 end if;
10193 end;
10194 end if;
10196 -- We have generated a tag check for either a class-wide type
10197 -- conversion or for AI05-0073.
10199 if Make_Conversion then
10200 declare
10201 Conv : Node_Id;
10202 begin
10203 Conv :=
10204 Make_Unchecked_Type_Conversion (Loc,
10205 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
10206 Expression => Relocate_Node (Expression (N)));
10207 Rewrite (N, Conv);
10208 Analyze_And_Resolve (N, Target_Type);
10209 end;
10210 end if;
10211 end if;
10212 end Tagged_Conversion;
10214 -- Case of other access type conversions
10216 elsif Is_Access_Type (Target_Type) then
10217 Apply_Constraint_Check (Operand, Target_Type);
10219 -- Case of conversions from a fixed-point type
10221 -- These conversions require special expansion and processing, found in
10222 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
10223 -- since from a semantic point of view, these are simple integer
10224 -- conversions, which do not need further processing.
10226 elsif Is_Fixed_Point_Type (Operand_Type)
10227 and then not Conversion_OK (N)
10228 then
10229 -- We should never see universal fixed at this case, since the
10230 -- expansion of the constituent divide or multiply should have
10231 -- eliminated the explicit mention of universal fixed.
10233 pragma Assert (Operand_Type /= Universal_Fixed);
10235 -- Check for special case of the conversion to universal real that
10236 -- occurs as a result of the use of a round attribute. In this case,
10237 -- the real type for the conversion is taken from the target type of
10238 -- the Round attribute and the result must be marked as rounded.
10240 if Target_Type = Universal_Real
10241 and then Nkind (Parent (N)) = N_Attribute_Reference
10242 and then Attribute_Name (Parent (N)) = Name_Round
10243 then
10244 Set_Rounded_Result (N);
10245 Set_Etype (N, Etype (Parent (N)));
10246 end if;
10248 -- Otherwise do correct fixed-conversion, but skip these if the
10249 -- Conversion_OK flag is set, because from a semantic point of view
10250 -- these are simple integer conversions needing no further processing
10251 -- (the backend will simply treat them as integers).
10253 if not Conversion_OK (N) then
10254 if Is_Fixed_Point_Type (Etype (N)) then
10255 Expand_Convert_Fixed_To_Fixed (N);
10256 Real_Range_Check;
10258 elsif Is_Integer_Type (Etype (N)) then
10259 Expand_Convert_Fixed_To_Integer (N);
10261 else
10262 pragma Assert (Is_Floating_Point_Type (Etype (N)));
10263 Expand_Convert_Fixed_To_Float (N);
10264 Real_Range_Check;
10265 end if;
10266 end if;
10268 -- Case of conversions to a fixed-point type
10270 -- These conversions require special expansion and processing, found in
10271 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
10272 -- since from a semantic point of view, these are simple integer
10273 -- conversions, which do not need further processing.
10275 elsif Is_Fixed_Point_Type (Target_Type)
10276 and then not Conversion_OK (N)
10277 then
10278 if Is_Integer_Type (Operand_Type) then
10279 Expand_Convert_Integer_To_Fixed (N);
10280 Real_Range_Check;
10281 else
10282 pragma Assert (Is_Floating_Point_Type (Operand_Type));
10283 Expand_Convert_Float_To_Fixed (N);
10284 Real_Range_Check;
10285 end if;
10287 -- Case of float-to-integer conversions
10289 -- We also handle float-to-fixed conversions with Conversion_OK set
10290 -- since semantically the fixed-point target is treated as though it
10291 -- were an integer in such cases.
10293 elsif Is_Floating_Point_Type (Operand_Type)
10294 and then
10295 (Is_Integer_Type (Target_Type)
10296 or else
10297 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
10298 then
10299 -- One more check here, gcc is still not able to do conversions of
10300 -- this type with proper overflow checking, and so gigi is doing an
10301 -- approximation of what is required by doing floating-point compares
10302 -- with the end-point. But that can lose precision in some cases, and
10303 -- give a wrong result. Converting the operand to Universal_Real is
10304 -- helpful, but still does not catch all cases with 64-bit integers
10305 -- on targets with only 64-bit floats.
10307 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
10308 -- Can this code be removed ???
10310 if Do_Range_Check (Operand) then
10311 Rewrite (Operand,
10312 Make_Type_Conversion (Loc,
10313 Subtype_Mark =>
10314 New_Occurrence_Of (Universal_Real, Loc),
10315 Expression =>
10316 Relocate_Node (Operand)));
10318 Set_Etype (Operand, Universal_Real);
10319 Enable_Range_Check (Operand);
10320 Set_Do_Range_Check (Expression (Operand), False);
10321 end if;
10323 -- Case of array conversions
10325 -- Expansion of array conversions, add required length/range checks but
10326 -- only do this if there is no change of representation. For handling of
10327 -- this case, see Handle_Changed_Representation.
10329 elsif Is_Array_Type (Target_Type) then
10330 if Is_Constrained (Target_Type) then
10331 Apply_Length_Check (Operand, Target_Type);
10332 else
10333 Apply_Range_Check (Operand, Target_Type);
10334 end if;
10336 Handle_Changed_Representation;
10338 -- Case of conversions of discriminated types
10340 -- Add required discriminant checks if target is constrained. Again this
10341 -- change is skipped if we have a change of representation.
10343 elsif Has_Discriminants (Target_Type)
10344 and then Is_Constrained (Target_Type)
10345 then
10346 Apply_Discriminant_Check (Operand, Target_Type);
10347 Handle_Changed_Representation;
10349 -- Case of all other record conversions. The only processing required
10350 -- is to check for a change of representation requiring the special
10351 -- assignment processing.
10353 elsif Is_Record_Type (Target_Type) then
10355 -- Ada 2005 (AI-216): Program_Error is raised when converting from
10356 -- a derived Unchecked_Union type to an unconstrained type that is
10357 -- not Unchecked_Union if the operand lacks inferable discriminants.
10359 if Is_Derived_Type (Operand_Type)
10360 and then Is_Unchecked_Union (Base_Type (Operand_Type))
10361 and then not Is_Constrained (Target_Type)
10362 and then not Is_Unchecked_Union (Base_Type (Target_Type))
10363 and then not Has_Inferable_Discriminants (Operand)
10364 then
10365 -- To prevent Gigi from generating illegal code, we generate a
10366 -- Program_Error node, but we give it the target type of the
10367 -- conversion (is this requirement documented somewhere ???)
10369 declare
10370 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
10371 Reason => PE_Unchecked_Union_Restriction);
10373 begin
10374 Set_Etype (PE, Target_Type);
10375 Rewrite (N, PE);
10377 end;
10378 else
10379 Handle_Changed_Representation;
10380 end if;
10382 -- Case of conversions of enumeration types
10384 elsif Is_Enumeration_Type (Target_Type) then
10386 -- Special processing is required if there is a change of
10387 -- representation (from enumeration representation clauses).
10389 if not Same_Representation (Target_Type, Operand_Type) then
10391 -- Convert: x(y) to x'val (ytyp'val (y))
10393 Rewrite (N,
10394 Make_Attribute_Reference (Loc,
10395 Prefix => New_Occurrence_Of (Target_Type, Loc),
10396 Attribute_Name => Name_Val,
10397 Expressions => New_List (
10398 Make_Attribute_Reference (Loc,
10399 Prefix => New_Occurrence_Of (Operand_Type, Loc),
10400 Attribute_Name => Name_Pos,
10401 Expressions => New_List (Operand)))));
10403 Analyze_And_Resolve (N, Target_Type);
10404 end if;
10406 -- Case of conversions to floating-point
10408 elsif Is_Floating_Point_Type (Target_Type) then
10409 Real_Range_Check;
10410 end if;
10412 -- At this stage, either the conversion node has been transformed into
10413 -- some other equivalent expression, or left as a conversion that can be
10414 -- handled by Gigi, in the following cases:
10416 -- Conversions with no change of representation or type
10418 -- Numeric conversions involving integer, floating- and fixed-point
10419 -- values. Fixed-point values are allowed only if Conversion_OK is
10420 -- set, i.e. if the fixed-point values are to be treated as integers.
10422 -- No other conversions should be passed to Gigi
10424 -- Check: are these rules stated in sinfo??? if so, why restate here???
10426 -- The only remaining step is to generate a range check if we still have
10427 -- a type conversion at this stage and Do_Range_Check is set. For now we
10428 -- do this only for conversions of discrete types.
10430 if Nkind (N) = N_Type_Conversion
10431 and then Is_Discrete_Type (Etype (N))
10432 then
10433 declare
10434 Expr : constant Node_Id := Expression (N);
10435 Ftyp : Entity_Id;
10436 Ityp : Entity_Id;
10438 begin
10439 if Do_Range_Check (Expr)
10440 and then Is_Discrete_Type (Etype (Expr))
10441 then
10442 Set_Do_Range_Check (Expr, False);
10444 -- Before we do a range check, we have to deal with treating a
10445 -- fixed-point operand as an integer. The way we do this is
10446 -- simply to do an unchecked conversion to an appropriate
10447 -- integer type large enough to hold the result.
10449 -- This code is not active yet, because we are only dealing
10450 -- with discrete types so far ???
10452 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
10453 and then Treat_Fixed_As_Integer (Expr)
10454 then
10455 Ftyp := Base_Type (Etype (Expr));
10457 if Esize (Ftyp) >= Esize (Standard_Integer) then
10458 Ityp := Standard_Long_Long_Integer;
10459 else
10460 Ityp := Standard_Integer;
10461 end if;
10463 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
10464 end if;
10466 -- Reset overflow flag, since the range check will include
10467 -- dealing with possible overflow, and generate the check. If
10468 -- Address is either a source type or target type, suppress
10469 -- range check to avoid typing anomalies when it is a visible
10470 -- integer type.
10472 Set_Do_Overflow_Check (N, False);
10473 if not Is_Descendent_Of_Address (Etype (Expr))
10474 and then not Is_Descendent_Of_Address (Target_Type)
10475 then
10476 Generate_Range_Check
10477 (Expr, Target_Type, CE_Range_Check_Failed);
10478 end if;
10479 end if;
10480 end;
10481 end if;
10483 -- Final step, if the result is a type conversion involving Vax_Float
10484 -- types, then it is subject for further special processing.
10486 if Nkind (N) = N_Type_Conversion
10487 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
10488 then
10489 Expand_Vax_Conversion (N);
10490 goto Done;
10491 end if;
10493 -- Here at end of processing
10495 <<Done>>
10496 -- Apply predicate check if required. Note that we can't just call
10497 -- Apply_Predicate_Check here, because the type looks right after
10498 -- the conversion and it would omit the check. The Comes_From_Source
10499 -- guard is necessary to prevent infinite recursions when we generate
10500 -- internal conversions for the purpose of checking predicates.
10502 if Present (Predicate_Function (Target_Type))
10503 and then Target_Type /= Operand_Type
10504 and then Comes_From_Source (N)
10505 then
10506 declare
10507 New_Expr : constant Node_Id := Duplicate_Subexpr (N);
10509 begin
10510 -- Avoid infinite recursion on the subsequent expansion of
10511 -- of the copy of the original type conversion.
10513 Set_Comes_From_Source (New_Expr, False);
10514 Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr));
10515 end;
10516 end if;
10517 end Expand_N_Type_Conversion;
10519 -----------------------------------
10520 -- Expand_N_Unchecked_Expression --
10521 -----------------------------------
10523 -- Remove the unchecked expression node from the tree. Its job was simply
10524 -- to make sure that its constituent expression was handled with checks
10525 -- off, and now that that is done, we can remove it from the tree, and
10526 -- indeed must, since Gigi does not expect to see these nodes.
10528 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
10529 Exp : constant Node_Id := Expression (N);
10530 begin
10531 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
10532 Rewrite (N, Exp);
10533 end Expand_N_Unchecked_Expression;
10535 ----------------------------------------
10536 -- Expand_N_Unchecked_Type_Conversion --
10537 ----------------------------------------
10539 -- If this cannot be handled by Gigi and we haven't already made a
10540 -- temporary for it, do it now.
10542 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
10543 Target_Type : constant Entity_Id := Etype (N);
10544 Operand : constant Node_Id := Expression (N);
10545 Operand_Type : constant Entity_Id := Etype (Operand);
10547 begin
10548 -- Nothing at all to do if conversion is to the identical type so remove
10549 -- the conversion completely, it is useless, except that it may carry
10550 -- an Assignment_OK indication which must be propagated to the operand.
10552 if Operand_Type = Target_Type then
10554 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
10556 if Assignment_OK (N) then
10557 Set_Assignment_OK (Operand);
10558 end if;
10560 Rewrite (N, Relocate_Node (Operand));
10561 return;
10562 end if;
10564 -- If we have a conversion of a compile time known value to a target
10565 -- type and the value is in range of the target type, then we can simply
10566 -- replace the construct by an integer literal of the correct type. We
10567 -- only apply this to integer types being converted. Possibly it may
10568 -- apply in other cases, but it is too much trouble to worry about.
10570 -- Note that we do not do this transformation if the Kill_Range_Check
10571 -- flag is set, since then the value may be outside the expected range.
10572 -- This happens in the Normalize_Scalars case.
10574 -- We also skip this if either the target or operand type is biased
10575 -- because in this case, the unchecked conversion is supposed to
10576 -- preserve the bit pattern, not the integer value.
10578 if Is_Integer_Type (Target_Type)
10579 and then not Has_Biased_Representation (Target_Type)
10580 and then Is_Integer_Type (Operand_Type)
10581 and then not Has_Biased_Representation (Operand_Type)
10582 and then Compile_Time_Known_Value (Operand)
10583 and then not Kill_Range_Check (N)
10584 then
10585 declare
10586 Val : constant Uint := Expr_Value (Operand);
10588 begin
10589 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
10590 and then
10591 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
10592 and then
10593 Val >= Expr_Value (Type_Low_Bound (Target_Type))
10594 and then
10595 Val <= Expr_Value (Type_High_Bound (Target_Type))
10596 then
10597 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
10599 -- If Address is the target type, just set the type to avoid a
10600 -- spurious type error on the literal when Address is a visible
10601 -- integer type.
10603 if Is_Descendent_Of_Address (Target_Type) then
10604 Set_Etype (N, Target_Type);
10605 else
10606 Analyze_And_Resolve (N, Target_Type);
10607 end if;
10609 return;
10610 end if;
10611 end;
10612 end if;
10614 -- Nothing to do if conversion is safe
10616 if Safe_Unchecked_Type_Conversion (N) then
10617 return;
10618 end if;
10620 -- Otherwise force evaluation unless Assignment_OK flag is set (this
10621 -- flag indicates ??? -- more comments needed here)
10623 if Assignment_OK (N) then
10624 null;
10625 else
10626 Force_Evaluation (N);
10627 end if;
10628 end Expand_N_Unchecked_Type_Conversion;
10630 ----------------------------
10631 -- Expand_Record_Equality --
10632 ----------------------------
10634 -- For non-variant records, Equality is expanded when needed into:
10636 -- and then Lhs.Discr1 = Rhs.Discr1
10637 -- and then ...
10638 -- and then Lhs.Discrn = Rhs.Discrn
10639 -- and then Lhs.Cmp1 = Rhs.Cmp1
10640 -- and then ...
10641 -- and then Lhs.Cmpn = Rhs.Cmpn
10643 -- The expression is folded by the back-end for adjacent fields. This
10644 -- function is called for tagged record in only one occasion: for imple-
10645 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
10646 -- otherwise the primitive "=" is used directly.
10648 function Expand_Record_Equality
10649 (Nod : Node_Id;
10650 Typ : Entity_Id;
10651 Lhs : Node_Id;
10652 Rhs : Node_Id;
10653 Bodies : List_Id) return Node_Id
10655 Loc : constant Source_Ptr := Sloc (Nod);
10657 Result : Node_Id;
10658 C : Entity_Id;
10660 First_Time : Boolean := True;
10662 function Suitable_Element (C : Entity_Id) return Entity_Id;
10663 -- Return the first field to compare beginning with C, skipping the
10664 -- inherited components.
10666 ----------------------
10667 -- Suitable_Element --
10668 ----------------------
10670 function Suitable_Element (C : Entity_Id) return Entity_Id is
10671 begin
10672 if No (C) then
10673 return Empty;
10675 elsif Ekind (C) /= E_Discriminant
10676 and then Ekind (C) /= E_Component
10677 then
10678 return Suitable_Element (Next_Entity (C));
10680 elsif Is_Tagged_Type (Typ)
10681 and then C /= Original_Record_Component (C)
10682 then
10683 return Suitable_Element (Next_Entity (C));
10685 elsif Chars (C) = Name_uTag then
10686 return Suitable_Element (Next_Entity (C));
10688 -- The .NET/JVM version of type Root_Controlled contains two fields
10689 -- which should not be considered part of the object. To achieve
10690 -- proper equiality between two controlled objects on .NET/JVM, skip
10691 -- field _parent whenever it is of type Root_Controlled.
10693 elsif Chars (C) = Name_uParent
10694 and then VM_Target /= No_VM
10695 and then Etype (C) = RTE (RE_Root_Controlled)
10696 then
10697 return Suitable_Element (Next_Entity (C));
10699 elsif Is_Interface (Etype (C)) then
10700 return Suitable_Element (Next_Entity (C));
10702 else
10703 return C;
10704 end if;
10705 end Suitable_Element;
10707 -- Start of processing for Expand_Record_Equality
10709 begin
10710 -- Generates the following code: (assuming that Typ has one Discr and
10711 -- component C2 is also a record)
10713 -- True
10714 -- and then Lhs.Discr1 = Rhs.Discr1
10715 -- and then Lhs.C1 = Rhs.C1
10716 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
10717 -- and then ...
10718 -- and then Lhs.Cmpn = Rhs.Cmpn
10720 Result := New_Reference_To (Standard_True, Loc);
10721 C := Suitable_Element (First_Entity (Typ));
10722 while Present (C) loop
10723 declare
10724 New_Lhs : Node_Id;
10725 New_Rhs : Node_Id;
10726 Check : Node_Id;
10728 begin
10729 if First_Time then
10730 First_Time := False;
10731 New_Lhs := Lhs;
10732 New_Rhs := Rhs;
10733 else
10734 New_Lhs := New_Copy_Tree (Lhs);
10735 New_Rhs := New_Copy_Tree (Rhs);
10736 end if;
10738 Check :=
10739 Expand_Composite_Equality (Nod, Etype (C),
10740 Lhs =>
10741 Make_Selected_Component (Loc,
10742 Prefix => New_Lhs,
10743 Selector_Name => New_Reference_To (C, Loc)),
10744 Rhs =>
10745 Make_Selected_Component (Loc,
10746 Prefix => New_Rhs,
10747 Selector_Name => New_Reference_To (C, Loc)),
10748 Bodies => Bodies);
10750 -- If some (sub)component is an unchecked_union, the whole
10751 -- operation will raise program error.
10753 if Nkind (Check) = N_Raise_Program_Error then
10754 Result := Check;
10755 Set_Etype (Result, Standard_Boolean);
10756 exit;
10757 else
10758 Result :=
10759 Make_And_Then (Loc,
10760 Left_Opnd => Result,
10761 Right_Opnd => Check);
10762 end if;
10763 end;
10765 C := Suitable_Element (Next_Entity (C));
10766 end loop;
10768 return Result;
10769 end Expand_Record_Equality;
10771 ---------------------------
10772 -- Expand_Set_Membership --
10773 ---------------------------
10775 procedure Expand_Set_Membership (N : Node_Id) is
10776 Lop : constant Node_Id := Left_Opnd (N);
10777 Alt : Node_Id;
10778 Res : Node_Id;
10780 function Make_Cond (Alt : Node_Id) return Node_Id;
10781 -- If the alternative is a subtype mark, create a simple membership
10782 -- test. Otherwise create an equality test for it.
10784 ---------------
10785 -- Make_Cond --
10786 ---------------
10788 function Make_Cond (Alt : Node_Id) return Node_Id is
10789 Cond : Node_Id;
10790 L : constant Node_Id := New_Copy (Lop);
10791 R : constant Node_Id := Relocate_Node (Alt);
10793 begin
10794 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
10795 or else Nkind (Alt) = N_Range
10796 then
10797 Cond :=
10798 Make_In (Sloc (Alt),
10799 Left_Opnd => L,
10800 Right_Opnd => R);
10801 else
10802 Cond :=
10803 Make_Op_Eq (Sloc (Alt),
10804 Left_Opnd => L,
10805 Right_Opnd => R);
10806 end if;
10808 return Cond;
10809 end Make_Cond;
10811 -- Start of processing for Expand_Set_Membership
10813 begin
10814 Remove_Side_Effects (Lop);
10816 Alt := Last (Alternatives (N));
10817 Res := Make_Cond (Alt);
10819 Prev (Alt);
10820 while Present (Alt) loop
10821 Res :=
10822 Make_Or_Else (Sloc (Alt),
10823 Left_Opnd => Make_Cond (Alt),
10824 Right_Opnd => Res);
10825 Prev (Alt);
10826 end loop;
10828 Rewrite (N, Res);
10829 Analyze_And_Resolve (N, Standard_Boolean);
10830 end Expand_Set_Membership;
10832 -----------------------------------
10833 -- Expand_Short_Circuit_Operator --
10834 -----------------------------------
10836 -- Deal with special expansion if actions are present for the right operand
10837 -- and deal with optimizing case of arguments being True or False. We also
10838 -- deal with the special case of non-standard boolean values.
10840 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
10841 Loc : constant Source_Ptr := Sloc (N);
10842 Typ : constant Entity_Id := Etype (N);
10843 Left : constant Node_Id := Left_Opnd (N);
10844 Right : constant Node_Id := Right_Opnd (N);
10845 LocR : constant Source_Ptr := Sloc (Right);
10846 Actlist : List_Id;
10848 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
10849 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
10850 -- If Left = Shortcut_Value then Right need not be evaluated
10852 function Make_Test_Expr (Opnd : Node_Id) return Node_Id;
10853 -- For Opnd a boolean expression, return a Boolean expression equivalent
10854 -- to Opnd /= Shortcut_Value.
10856 --------------------
10857 -- Make_Test_Expr --
10858 --------------------
10860 function Make_Test_Expr (Opnd : Node_Id) return Node_Id is
10861 begin
10862 if Shortcut_Value then
10863 return Make_Op_Not (Sloc (Opnd), Opnd);
10864 else
10865 return Opnd;
10866 end if;
10867 end Make_Test_Expr;
10869 Op_Var : Entity_Id;
10870 -- Entity for a temporary variable holding the value of the operator,
10871 -- used for expansion in the case where actions are present.
10873 -- Start of processing for Expand_Short_Circuit_Operator
10875 begin
10876 -- Deal with non-standard booleans
10878 if Is_Boolean_Type (Typ) then
10879 Adjust_Condition (Left);
10880 Adjust_Condition (Right);
10881 Set_Etype (N, Standard_Boolean);
10882 end if;
10884 -- Check for cases where left argument is known to be True or False
10886 if Compile_Time_Known_Value (Left) then
10888 -- Mark SCO for left condition as compile time known
10890 if Generate_SCO and then Comes_From_Source (Left) then
10891 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
10892 end if;
10894 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
10895 -- Any actions associated with Right will be executed unconditionally
10896 -- and can thus be inserted into the tree unconditionally.
10898 if Expr_Value_E (Left) /= Shortcut_Ent then
10899 if Present (Actions (N)) then
10900 Insert_Actions (N, Actions (N));
10901 end if;
10903 Rewrite (N, Right);
10905 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
10906 -- In this case we can forget the actions associated with Right,
10907 -- since they will never be executed.
10909 else
10910 Kill_Dead_Code (Right);
10911 Kill_Dead_Code (Actions (N));
10912 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
10913 end if;
10915 Adjust_Result_Type (N, Typ);
10916 return;
10917 end if;
10919 -- If Actions are present for the right operand, we have to do some
10920 -- special processing. We can't just let these actions filter back into
10921 -- code preceding the short circuit (which is what would have happened
10922 -- if we had not trapped them in the short-circuit form), since they
10923 -- must only be executed if the right operand of the short circuit is
10924 -- executed and not otherwise.
10926 -- the temporary variable C.
10928 if Present (Actions (N)) then
10929 Actlist := Actions (N);
10931 -- The old approach is to expand:
10933 -- left AND THEN right
10935 -- into
10937 -- C : Boolean := False;
10938 -- IF left THEN
10939 -- Actions;
10940 -- IF right THEN
10941 -- C := True;
10942 -- END IF;
10943 -- END IF;
10945 -- and finally rewrite the operator into a reference to C. Similarly
10946 -- for left OR ELSE right, with negated values. Note that this
10947 -- rewrite causes some difficulties for coverage analysis because
10948 -- of the introduction of the new variable C, which obscures the
10949 -- structure of the test.
10951 -- We use this "old approach" if use of N_Expression_With_Actions
10952 -- is False (see description in Opt of when this is or is not set).
10954 if not Use_Expression_With_Actions then
10955 Op_Var := Make_Temporary (Loc, 'C', Related_Node => N);
10957 Insert_Action (N,
10958 Make_Object_Declaration (Loc,
10959 Defining_Identifier =>
10960 Op_Var,
10961 Object_Definition =>
10962 New_Occurrence_Of (Standard_Boolean, Loc),
10963 Expression =>
10964 New_Occurrence_Of (Shortcut_Ent, Loc)));
10966 Append_To (Actlist,
10967 Make_Implicit_If_Statement (Right,
10968 Condition => Make_Test_Expr (Right),
10969 Then_Statements => New_List (
10970 Make_Assignment_Statement (LocR,
10971 Name => New_Occurrence_Of (Op_Var, LocR),
10972 Expression =>
10973 New_Occurrence_Of
10974 (Boolean_Literals (not Shortcut_Value), LocR)))));
10976 Insert_Action (N,
10977 Make_Implicit_If_Statement (Left,
10978 Condition => Make_Test_Expr (Left),
10979 Then_Statements => Actlist));
10981 Rewrite (N, New_Occurrence_Of (Op_Var, Loc));
10982 Analyze_And_Resolve (N, Standard_Boolean);
10984 -- The new approach, activated for now by the use of debug flag
10985 -- -gnatd.X is to use the new Expression_With_Actions node for the
10986 -- right operand of the short-circuit form. This should solve the
10987 -- traceability problems for coverage analysis.
10989 else
10990 Rewrite (Right,
10991 Make_Expression_With_Actions (LocR,
10992 Expression => Relocate_Node (Right),
10993 Actions => Actlist));
10994 Set_Actions (N, No_List);
10995 Analyze_And_Resolve (Right, Standard_Boolean);
10996 end if;
10998 Adjust_Result_Type (N, Typ);
10999 return;
11000 end if;
11002 -- No actions present, check for cases of right argument True/False
11004 if Compile_Time_Known_Value (Right) then
11006 -- Mark SCO for left condition as compile time known
11008 if Generate_SCO and then Comes_From_Source (Right) then
11009 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
11010 end if;
11012 -- Change (Left and then True), (Left or else False) to Left.
11013 -- Note that we know there are no actions associated with the right
11014 -- operand, since we just checked for this case above.
11016 if Expr_Value_E (Right) /= Shortcut_Ent then
11017 Rewrite (N, Left);
11019 -- Change (Left and then False), (Left or else True) to Right,
11020 -- making sure to preserve any side effects associated with the Left
11021 -- operand.
11023 else
11024 Remove_Side_Effects (Left);
11025 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
11026 end if;
11027 end if;
11029 Adjust_Result_Type (N, Typ);
11030 end Expand_Short_Circuit_Operator;
11032 -------------------------------------
11033 -- Fixup_Universal_Fixed_Operation --
11034 -------------------------------------
11036 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
11037 Conv : constant Node_Id := Parent (N);
11039 begin
11040 -- We must have a type conversion immediately above us
11042 pragma Assert (Nkind (Conv) = N_Type_Conversion);
11044 -- Normally the type conversion gives our target type. The exception
11045 -- occurs in the case of the Round attribute, where the conversion
11046 -- will be to universal real, and our real type comes from the Round
11047 -- attribute (as well as an indication that we must round the result)
11049 if Nkind (Parent (Conv)) = N_Attribute_Reference
11050 and then Attribute_Name (Parent (Conv)) = Name_Round
11051 then
11052 Set_Etype (N, Etype (Parent (Conv)));
11053 Set_Rounded_Result (N);
11055 -- Normal case where type comes from conversion above us
11057 else
11058 Set_Etype (N, Etype (Conv));
11059 end if;
11060 end Fixup_Universal_Fixed_Operation;
11062 ---------------------------------
11063 -- Has_Inferable_Discriminants --
11064 ---------------------------------
11066 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
11068 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
11069 -- Determines whether the left-most prefix of a selected component is a
11070 -- formal parameter in a subprogram. Assumes N is a selected component.
11072 --------------------------------
11073 -- Prefix_Is_Formal_Parameter --
11074 --------------------------------
11076 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
11077 Sel_Comp : Node_Id;
11079 begin
11080 -- Move to the left-most prefix by climbing up the tree
11082 Sel_Comp := N;
11083 while Present (Parent (Sel_Comp))
11084 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
11085 loop
11086 Sel_Comp := Parent (Sel_Comp);
11087 end loop;
11089 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
11090 end Prefix_Is_Formal_Parameter;
11092 -- Start of processing for Has_Inferable_Discriminants
11094 begin
11095 -- For selected components, the subtype of the selector must be a
11096 -- constrained Unchecked_Union. If the component is subject to a
11097 -- per-object constraint, then the enclosing object must have inferable
11098 -- discriminants.
11100 if Nkind (N) = N_Selected_Component then
11101 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
11103 -- A small hack. If we have a per-object constrained selected
11104 -- component of a formal parameter, return True since we do not
11105 -- know the actual parameter association yet.
11107 if Prefix_Is_Formal_Parameter (N) then
11108 return True;
11110 -- Otherwise, check the enclosing object and the selector
11112 else
11113 return Has_Inferable_Discriminants (Prefix (N))
11114 and then Has_Inferable_Discriminants (Selector_Name (N));
11115 end if;
11117 -- The call to Has_Inferable_Discriminants will determine whether
11118 -- the selector has a constrained Unchecked_Union nominal type.
11120 else
11121 return Has_Inferable_Discriminants (Selector_Name (N));
11122 end if;
11124 -- A qualified expression has inferable discriminants if its subtype
11125 -- mark is a constrained Unchecked_Union subtype.
11127 elsif Nkind (N) = N_Qualified_Expression then
11128 return Is_Unchecked_Union (Etype (Subtype_Mark (N)))
11129 and then Is_Constrained (Etype (Subtype_Mark (N)));
11131 -- For all other names, it is sufficient to have a constrained
11132 -- Unchecked_Union nominal subtype.
11134 else
11135 return Is_Unchecked_Union (Base_Type (Etype (N)))
11136 and then Is_Constrained (Etype (N));
11137 end if;
11138 end Has_Inferable_Discriminants;
11140 -------------------------------
11141 -- Insert_Dereference_Action --
11142 -------------------------------
11144 procedure Insert_Dereference_Action (N : Node_Id) is
11146 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
11147 -- Return true if type of P is derived from Checked_Pool;
11149 -----------------------------
11150 -- Is_Checked_Storage_Pool --
11151 -----------------------------
11153 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
11154 T : Entity_Id;
11156 begin
11157 if No (P) then
11158 return False;
11159 end if;
11161 T := Etype (P);
11162 while T /= Etype (T) loop
11163 if Is_RTE (T, RE_Checked_Pool) then
11164 return True;
11165 else
11166 T := Etype (T);
11167 end if;
11168 end loop;
11170 return False;
11171 end Is_Checked_Storage_Pool;
11173 -- Local variables
11175 Typ : constant Entity_Id := Etype (N);
11176 Desig : constant Entity_Id := Available_View (Designated_Type (Typ));
11177 Loc : constant Source_Ptr := Sloc (N);
11178 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
11179 Pnod : constant Node_Id := Parent (N);
11181 Addr : Entity_Id;
11182 Alig : Entity_Id;
11183 Deref : Node_Id;
11184 Size : Entity_Id;
11185 Stmt : Node_Id;
11187 -- Start of processing for Insert_Dereference_Action
11189 begin
11190 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
11192 -- Do not re-expand a dereference which has already been processed by
11193 -- this routine.
11195 if Has_Dereference_Action (Pnod) then
11196 return;
11198 -- Do not perform this type of expansion for internally-generated
11199 -- dereferences.
11201 elsif not Comes_From_Source (Original_Node (Pnod)) then
11202 return;
11204 -- A dereference action is only applicable to objects which have been
11205 -- allocated on a checked pool.
11207 elsif not Is_Checked_Storage_Pool (Pool) then
11208 return;
11209 end if;
11211 -- Extract the address of the dereferenced object. Generate:
11213 -- Addr : System.Address := <N>'Pool_Address;
11215 Addr := Make_Temporary (Loc, 'P');
11217 Insert_Action (N,
11218 Make_Object_Declaration (Loc,
11219 Defining_Identifier => Addr,
11220 Object_Definition =>
11221 New_Reference_To (RTE (RE_Address), Loc),
11222 Expression =>
11223 Make_Attribute_Reference (Loc,
11224 Prefix => Duplicate_Subexpr_Move_Checks (N),
11225 Attribute_Name => Name_Pool_Address)));
11227 -- Calculate the size of the dereferenced object. Generate:
11229 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
11231 Deref :=
11232 Make_Explicit_Dereference (Loc,
11233 Prefix => Duplicate_Subexpr_Move_Checks (N));
11234 Set_Has_Dereference_Action (Deref);
11236 Size := Make_Temporary (Loc, 'S');
11238 Insert_Action (N,
11239 Make_Object_Declaration (Loc,
11240 Defining_Identifier => Size,
11242 Object_Definition =>
11243 New_Reference_To (RTE (RE_Storage_Count), Loc),
11245 Expression =>
11246 Make_Op_Divide (Loc,
11247 Left_Opnd =>
11248 Make_Attribute_Reference (Loc,
11249 Prefix => Deref,
11250 Attribute_Name => Name_Size),
11251 Right_Opnd =>
11252 Make_Integer_Literal (Loc, System_Storage_Unit))));
11254 -- Calculate the alignment of the dereferenced object. Generate:
11255 -- Alig : constant Storage_Count := <N>.all'Alignment;
11257 Deref :=
11258 Make_Explicit_Dereference (Loc,
11259 Prefix => Duplicate_Subexpr_Move_Checks (N));
11260 Set_Has_Dereference_Action (Deref);
11262 Alig := Make_Temporary (Loc, 'A');
11264 Insert_Action (N,
11265 Make_Object_Declaration (Loc,
11266 Defining_Identifier => Alig,
11267 Object_Definition =>
11268 New_Reference_To (RTE (RE_Storage_Count), Loc),
11269 Expression =>
11270 Make_Attribute_Reference (Loc,
11271 Prefix => Deref,
11272 Attribute_Name => Name_Alignment)));
11274 -- A dereference of a controlled object requires special processing. The
11275 -- finalization machinery requests additional space from the underlying
11276 -- pool to allocate and hide two pointers. As a result, a checked pool
11277 -- may mark the wrong memory as valid. Since checked pools do not have
11278 -- knowledge of hidden pointers, we have to bring the two pointers back
11279 -- in view in order to restore the original state of the object.
11281 if Needs_Finalization (Desig) then
11283 -- Adjust the address and size of the dereferenced object. Generate:
11284 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
11286 Stmt :=
11287 Make_Procedure_Call_Statement (Loc,
11288 Name =>
11289 New_Reference_To (RTE (RE_Adjust_Controlled_Dereference), Loc),
11290 Parameter_Associations => New_List (
11291 New_Reference_To (Addr, Loc),
11292 New_Reference_To (Size, Loc),
11293 New_Reference_To (Alig, Loc)));
11295 -- Class-wide types complicate things because we cannot determine
11296 -- statically whether the actual object is truly controlled. We must
11297 -- generate a runtime check to detect this property. Generate:
11299 -- if Needs_Finalization (<N>.all'Tag) then
11300 -- <Stmt>;
11301 -- end if;
11303 if Is_Class_Wide_Type (Desig) then
11304 Deref :=
11305 Make_Explicit_Dereference (Loc,
11306 Prefix => Duplicate_Subexpr_Move_Checks (N));
11307 Set_Has_Dereference_Action (Deref);
11309 Stmt :=
11310 Make_If_Statement (Loc,
11311 Condition =>
11312 Make_Function_Call (Loc,
11313 Name =>
11314 New_Reference_To (RTE (RE_Needs_Finalization), Loc),
11315 Parameter_Associations => New_List (
11316 Make_Attribute_Reference (Loc,
11317 Prefix => Deref,
11318 Attribute_Name => Name_Tag))),
11319 Then_Statements => New_List (Stmt));
11320 end if;
11322 Insert_Action (N, Stmt);
11323 end if;
11325 -- Generate:
11326 -- Dereference (Pool, Addr, Size, Alig);
11328 Insert_Action (N,
11329 Make_Procedure_Call_Statement (Loc,
11330 Name =>
11331 New_Reference_To
11332 (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
11333 Parameter_Associations => New_List (
11334 New_Reference_To (Pool, Loc),
11335 New_Reference_To (Addr, Loc),
11336 New_Reference_To (Size, Loc),
11337 New_Reference_To (Alig, Loc))));
11339 -- Mark the explicit dereference as processed to avoid potential
11340 -- infinite expansion.
11342 Set_Has_Dereference_Action (Pnod);
11344 exception
11345 when RE_Not_Available =>
11346 return;
11347 end Insert_Dereference_Action;
11349 --------------------------------
11350 -- Integer_Promotion_Possible --
11351 --------------------------------
11353 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
11354 Operand : constant Node_Id := Expression (N);
11355 Operand_Type : constant Entity_Id := Etype (Operand);
11356 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
11358 begin
11359 pragma Assert (Nkind (N) = N_Type_Conversion);
11361 return
11363 -- We only do the transformation for source constructs. We assume
11364 -- that the expander knows what it is doing when it generates code.
11366 Comes_From_Source (N)
11368 -- If the operand type is Short_Integer or Short_Short_Integer,
11369 -- then we will promote to Integer, which is available on all
11370 -- targets, and is sufficient to ensure no intermediate overflow.
11371 -- Furthermore it is likely to be as efficient or more efficient
11372 -- than using the smaller type for the computation so we do this
11373 -- unconditionally.
11375 and then
11376 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
11377 or else
11378 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
11380 -- Test for interesting operation, which includes addition,
11381 -- division, exponentiation, multiplication, subtraction, absolute
11382 -- value and unary negation. Unary "+" is omitted since it is a
11383 -- no-op and thus can't overflow.
11385 and then Nkind_In (Operand, N_Op_Abs,
11386 N_Op_Add,
11387 N_Op_Divide,
11388 N_Op_Expon,
11389 N_Op_Minus,
11390 N_Op_Multiply,
11391 N_Op_Subtract);
11392 end Integer_Promotion_Possible;
11394 ------------------------------
11395 -- Make_Array_Comparison_Op --
11396 ------------------------------
11398 -- This is a hand-coded expansion of the following generic function:
11400 -- generic
11401 -- type elem is (<>);
11402 -- type index is (<>);
11403 -- type a is array (index range <>) of elem;
11405 -- function Gnnn (X : a; Y: a) return boolean is
11406 -- J : index := Y'first;
11408 -- begin
11409 -- if X'length = 0 then
11410 -- return false;
11412 -- elsif Y'length = 0 then
11413 -- return true;
11415 -- else
11416 -- for I in X'range loop
11417 -- if X (I) = Y (J) then
11418 -- if J = Y'last then
11419 -- exit;
11420 -- else
11421 -- J := index'succ (J);
11422 -- end if;
11424 -- else
11425 -- return X (I) > Y (J);
11426 -- end if;
11427 -- end loop;
11429 -- return X'length > Y'length;
11430 -- end if;
11431 -- end Gnnn;
11433 -- Note that since we are essentially doing this expansion by hand, we
11434 -- do not need to generate an actual or formal generic part, just the
11435 -- instantiated function itself.
11437 function Make_Array_Comparison_Op
11438 (Typ : Entity_Id;
11439 Nod : Node_Id) return Node_Id
11441 Loc : constant Source_Ptr := Sloc (Nod);
11443 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
11444 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
11445 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
11446 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
11448 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
11450 Loop_Statement : Node_Id;
11451 Loop_Body : Node_Id;
11452 If_Stat : Node_Id;
11453 Inner_If : Node_Id;
11454 Final_Expr : Node_Id;
11455 Func_Body : Node_Id;
11456 Func_Name : Entity_Id;
11457 Formals : List_Id;
11458 Length1 : Node_Id;
11459 Length2 : Node_Id;
11461 begin
11462 -- if J = Y'last then
11463 -- exit;
11464 -- else
11465 -- J := index'succ (J);
11466 -- end if;
11468 Inner_If :=
11469 Make_Implicit_If_Statement (Nod,
11470 Condition =>
11471 Make_Op_Eq (Loc,
11472 Left_Opnd => New_Reference_To (J, Loc),
11473 Right_Opnd =>
11474 Make_Attribute_Reference (Loc,
11475 Prefix => New_Reference_To (Y, Loc),
11476 Attribute_Name => Name_Last)),
11478 Then_Statements => New_List (
11479 Make_Exit_Statement (Loc)),
11481 Else_Statements =>
11482 New_List (
11483 Make_Assignment_Statement (Loc,
11484 Name => New_Reference_To (J, Loc),
11485 Expression =>
11486 Make_Attribute_Reference (Loc,
11487 Prefix => New_Reference_To (Index, Loc),
11488 Attribute_Name => Name_Succ,
11489 Expressions => New_List (New_Reference_To (J, Loc))))));
11491 -- if X (I) = Y (J) then
11492 -- if ... end if;
11493 -- else
11494 -- return X (I) > Y (J);
11495 -- end if;
11497 Loop_Body :=
11498 Make_Implicit_If_Statement (Nod,
11499 Condition =>
11500 Make_Op_Eq (Loc,
11501 Left_Opnd =>
11502 Make_Indexed_Component (Loc,
11503 Prefix => New_Reference_To (X, Loc),
11504 Expressions => New_List (New_Reference_To (I, Loc))),
11506 Right_Opnd =>
11507 Make_Indexed_Component (Loc,
11508 Prefix => New_Reference_To (Y, Loc),
11509 Expressions => New_List (New_Reference_To (J, Loc)))),
11511 Then_Statements => New_List (Inner_If),
11513 Else_Statements => New_List (
11514 Make_Simple_Return_Statement (Loc,
11515 Expression =>
11516 Make_Op_Gt (Loc,
11517 Left_Opnd =>
11518 Make_Indexed_Component (Loc,
11519 Prefix => New_Reference_To (X, Loc),
11520 Expressions => New_List (New_Reference_To (I, Loc))),
11522 Right_Opnd =>
11523 Make_Indexed_Component (Loc,
11524 Prefix => New_Reference_To (Y, Loc),
11525 Expressions => New_List (
11526 New_Reference_To (J, Loc)))))));
11528 -- for I in X'range loop
11529 -- if ... end if;
11530 -- end loop;
11532 Loop_Statement :=
11533 Make_Implicit_Loop_Statement (Nod,
11534 Identifier => Empty,
11536 Iteration_Scheme =>
11537 Make_Iteration_Scheme (Loc,
11538 Loop_Parameter_Specification =>
11539 Make_Loop_Parameter_Specification (Loc,
11540 Defining_Identifier => I,
11541 Discrete_Subtype_Definition =>
11542 Make_Attribute_Reference (Loc,
11543 Prefix => New_Reference_To (X, Loc),
11544 Attribute_Name => Name_Range))),
11546 Statements => New_List (Loop_Body));
11548 -- if X'length = 0 then
11549 -- return false;
11550 -- elsif Y'length = 0 then
11551 -- return true;
11552 -- else
11553 -- for ... loop ... end loop;
11554 -- return X'length > Y'length;
11555 -- end if;
11557 Length1 :=
11558 Make_Attribute_Reference (Loc,
11559 Prefix => New_Reference_To (X, Loc),
11560 Attribute_Name => Name_Length);
11562 Length2 :=
11563 Make_Attribute_Reference (Loc,
11564 Prefix => New_Reference_To (Y, Loc),
11565 Attribute_Name => Name_Length);
11567 Final_Expr :=
11568 Make_Op_Gt (Loc,
11569 Left_Opnd => Length1,
11570 Right_Opnd => Length2);
11572 If_Stat :=
11573 Make_Implicit_If_Statement (Nod,
11574 Condition =>
11575 Make_Op_Eq (Loc,
11576 Left_Opnd =>
11577 Make_Attribute_Reference (Loc,
11578 Prefix => New_Reference_To (X, Loc),
11579 Attribute_Name => Name_Length),
11580 Right_Opnd =>
11581 Make_Integer_Literal (Loc, 0)),
11583 Then_Statements =>
11584 New_List (
11585 Make_Simple_Return_Statement (Loc,
11586 Expression => New_Reference_To (Standard_False, Loc))),
11588 Elsif_Parts => New_List (
11589 Make_Elsif_Part (Loc,
11590 Condition =>
11591 Make_Op_Eq (Loc,
11592 Left_Opnd =>
11593 Make_Attribute_Reference (Loc,
11594 Prefix => New_Reference_To (Y, Loc),
11595 Attribute_Name => Name_Length),
11596 Right_Opnd =>
11597 Make_Integer_Literal (Loc, 0)),
11599 Then_Statements =>
11600 New_List (
11601 Make_Simple_Return_Statement (Loc,
11602 Expression => New_Reference_To (Standard_True, Loc))))),
11604 Else_Statements => New_List (
11605 Loop_Statement,
11606 Make_Simple_Return_Statement (Loc,
11607 Expression => Final_Expr)));
11609 -- (X : a; Y: a)
11611 Formals := New_List (
11612 Make_Parameter_Specification (Loc,
11613 Defining_Identifier => X,
11614 Parameter_Type => New_Reference_To (Typ, Loc)),
11616 Make_Parameter_Specification (Loc,
11617 Defining_Identifier => Y,
11618 Parameter_Type => New_Reference_To (Typ, Loc)));
11620 -- function Gnnn (...) return boolean is
11621 -- J : index := Y'first;
11622 -- begin
11623 -- if ... end if;
11624 -- end Gnnn;
11626 Func_Name := Make_Temporary (Loc, 'G');
11628 Func_Body :=
11629 Make_Subprogram_Body (Loc,
11630 Specification =>
11631 Make_Function_Specification (Loc,
11632 Defining_Unit_Name => Func_Name,
11633 Parameter_Specifications => Formals,
11634 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
11636 Declarations => New_List (
11637 Make_Object_Declaration (Loc,
11638 Defining_Identifier => J,
11639 Object_Definition => New_Reference_To (Index, Loc),
11640 Expression =>
11641 Make_Attribute_Reference (Loc,
11642 Prefix => New_Reference_To (Y, Loc),
11643 Attribute_Name => Name_First))),
11645 Handled_Statement_Sequence =>
11646 Make_Handled_Sequence_Of_Statements (Loc,
11647 Statements => New_List (If_Stat)));
11649 return Func_Body;
11650 end Make_Array_Comparison_Op;
11652 ---------------------------
11653 -- Make_Boolean_Array_Op --
11654 ---------------------------
11656 -- For logical operations on boolean arrays, expand in line the following,
11657 -- replacing 'and' with 'or' or 'xor' where needed:
11659 -- function Annn (A : typ; B: typ) return typ is
11660 -- C : typ;
11661 -- begin
11662 -- for J in A'range loop
11663 -- C (J) := A (J) op B (J);
11664 -- end loop;
11665 -- return C;
11666 -- end Annn;
11668 -- Here typ is the boolean array type
11670 function Make_Boolean_Array_Op
11671 (Typ : Entity_Id;
11672 N : Node_Id) return Node_Id
11674 Loc : constant Source_Ptr := Sloc (N);
11676 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
11677 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
11678 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
11679 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
11681 A_J : Node_Id;
11682 B_J : Node_Id;
11683 C_J : Node_Id;
11684 Op : Node_Id;
11686 Formals : List_Id;
11687 Func_Name : Entity_Id;
11688 Func_Body : Node_Id;
11689 Loop_Statement : Node_Id;
11691 begin
11692 A_J :=
11693 Make_Indexed_Component (Loc,
11694 Prefix => New_Reference_To (A, Loc),
11695 Expressions => New_List (New_Reference_To (J, Loc)));
11697 B_J :=
11698 Make_Indexed_Component (Loc,
11699 Prefix => New_Reference_To (B, Loc),
11700 Expressions => New_List (New_Reference_To (J, Loc)));
11702 C_J :=
11703 Make_Indexed_Component (Loc,
11704 Prefix => New_Reference_To (C, Loc),
11705 Expressions => New_List (New_Reference_To (J, Loc)));
11707 if Nkind (N) = N_Op_And then
11708 Op :=
11709 Make_Op_And (Loc,
11710 Left_Opnd => A_J,
11711 Right_Opnd => B_J);
11713 elsif Nkind (N) = N_Op_Or then
11714 Op :=
11715 Make_Op_Or (Loc,
11716 Left_Opnd => A_J,
11717 Right_Opnd => B_J);
11719 else
11720 Op :=
11721 Make_Op_Xor (Loc,
11722 Left_Opnd => A_J,
11723 Right_Opnd => B_J);
11724 end if;
11726 Loop_Statement :=
11727 Make_Implicit_Loop_Statement (N,
11728 Identifier => Empty,
11730 Iteration_Scheme =>
11731 Make_Iteration_Scheme (Loc,
11732 Loop_Parameter_Specification =>
11733 Make_Loop_Parameter_Specification (Loc,
11734 Defining_Identifier => J,
11735 Discrete_Subtype_Definition =>
11736 Make_Attribute_Reference (Loc,
11737 Prefix => New_Reference_To (A, Loc),
11738 Attribute_Name => Name_Range))),
11740 Statements => New_List (
11741 Make_Assignment_Statement (Loc,
11742 Name => C_J,
11743 Expression => Op)));
11745 Formals := New_List (
11746 Make_Parameter_Specification (Loc,
11747 Defining_Identifier => A,
11748 Parameter_Type => New_Reference_To (Typ, Loc)),
11750 Make_Parameter_Specification (Loc,
11751 Defining_Identifier => B,
11752 Parameter_Type => New_Reference_To (Typ, Loc)));
11754 Func_Name := Make_Temporary (Loc, 'A');
11755 Set_Is_Inlined (Func_Name);
11757 Func_Body :=
11758 Make_Subprogram_Body (Loc,
11759 Specification =>
11760 Make_Function_Specification (Loc,
11761 Defining_Unit_Name => Func_Name,
11762 Parameter_Specifications => Formals,
11763 Result_Definition => New_Reference_To (Typ, Loc)),
11765 Declarations => New_List (
11766 Make_Object_Declaration (Loc,
11767 Defining_Identifier => C,
11768 Object_Definition => New_Reference_To (Typ, Loc))),
11770 Handled_Statement_Sequence =>
11771 Make_Handled_Sequence_Of_Statements (Loc,
11772 Statements => New_List (
11773 Loop_Statement,
11774 Make_Simple_Return_Statement (Loc,
11775 Expression => New_Reference_To (C, Loc)))));
11777 return Func_Body;
11778 end Make_Boolean_Array_Op;
11780 -----------------------------------------
11781 -- Minimized_Eliminated_Overflow_Check --
11782 -----------------------------------------
11784 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is
11785 begin
11786 return
11787 Is_Signed_Integer_Type (Etype (N))
11788 and then Overflow_Check_Mode in Minimized_Or_Eliminated;
11789 end Minimized_Eliminated_Overflow_Check;
11791 --------------------------------
11792 -- Optimize_Length_Comparison --
11793 --------------------------------
11795 procedure Optimize_Length_Comparison (N : Node_Id) is
11796 Loc : constant Source_Ptr := Sloc (N);
11797 Typ : constant Entity_Id := Etype (N);
11798 Result : Node_Id;
11800 Left : Node_Id;
11801 Right : Node_Id;
11802 -- First and Last attribute reference nodes, which end up as left and
11803 -- right operands of the optimized result.
11805 Is_Zero : Boolean;
11806 -- True for comparison operand of zero
11808 Comp : Node_Id;
11809 -- Comparison operand, set only if Is_Zero is false
11811 Ent : Entity_Id;
11812 -- Entity whose length is being compared
11814 Index : Node_Id;
11815 -- Integer_Literal node for length attribute expression, or Empty
11816 -- if there is no such expression present.
11818 Ityp : Entity_Id;
11819 -- Type of array index to which 'Length is applied
11821 Op : Node_Kind := Nkind (N);
11822 -- Kind of comparison operator, gets flipped if operands backwards
11824 function Is_Optimizable (N : Node_Id) return Boolean;
11825 -- Tests N to see if it is an optimizable comparison value (defined as
11826 -- constant zero or one, or something else where the value is known to
11827 -- be positive and in the range of 32-bits, and where the corresponding
11828 -- Length value is also known to be 32-bits. If result is true, sets
11829 -- Is_Zero, Ityp, and Comp accordingly.
11831 function Is_Entity_Length (N : Node_Id) return Boolean;
11832 -- Tests if N is a length attribute applied to a simple entity. If so,
11833 -- returns True, and sets Ent to the entity, and Index to the integer
11834 -- literal provided as an attribute expression, or to Empty if none.
11835 -- Also returns True if the expression is a generated type conversion
11836 -- whose expression is of the desired form. This latter case arises
11837 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
11838 -- to check for being in range, which is not needed in this context.
11839 -- Returns False if neither condition holds.
11841 function Prepare_64 (N : Node_Id) return Node_Id;
11842 -- Given a discrete expression, returns a Long_Long_Integer typed
11843 -- expression representing the underlying value of the expression.
11844 -- This is done with an unchecked conversion to the result type. We
11845 -- use unchecked conversion to handle the enumeration type case.
11847 ----------------------
11848 -- Is_Entity_Length --
11849 ----------------------
11851 function Is_Entity_Length (N : Node_Id) return Boolean is
11852 begin
11853 if Nkind (N) = N_Attribute_Reference
11854 and then Attribute_Name (N) = Name_Length
11855 and then Is_Entity_Name (Prefix (N))
11856 then
11857 Ent := Entity (Prefix (N));
11859 if Present (Expressions (N)) then
11860 Index := First (Expressions (N));
11861 else
11862 Index := Empty;
11863 end if;
11865 return True;
11867 elsif Nkind (N) = N_Type_Conversion
11868 and then not Comes_From_Source (N)
11869 then
11870 return Is_Entity_Length (Expression (N));
11872 else
11873 return False;
11874 end if;
11875 end Is_Entity_Length;
11877 --------------------
11878 -- Is_Optimizable --
11879 --------------------
11881 function Is_Optimizable (N : Node_Id) return Boolean is
11882 Val : Uint;
11883 OK : Boolean;
11884 Lo : Uint;
11885 Hi : Uint;
11886 Indx : Node_Id;
11888 begin
11889 if Compile_Time_Known_Value (N) then
11890 Val := Expr_Value (N);
11892 if Val = Uint_0 then
11893 Is_Zero := True;
11894 Comp := Empty;
11895 return True;
11897 elsif Val = Uint_1 then
11898 Is_Zero := False;
11899 Comp := Empty;
11900 return True;
11901 end if;
11902 end if;
11904 -- Here we have to make sure of being within 32-bits
11906 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
11908 if not OK
11909 or else Lo < Uint_1
11910 or else Hi > UI_From_Int (Int'Last)
11911 then
11912 return False;
11913 end if;
11915 -- Comparison value was within range, so now we must check the index
11916 -- value to make sure it is also within 32-bits.
11918 Indx := First_Index (Etype (Ent));
11920 if Present (Index) then
11921 for J in 2 .. UI_To_Int (Intval (Index)) loop
11922 Next_Index (Indx);
11923 end loop;
11924 end if;
11926 Ityp := Etype (Indx);
11928 if Esize (Ityp) > 32 then
11929 return False;
11930 end if;
11932 Is_Zero := False;
11933 Comp := N;
11934 return True;
11935 end Is_Optimizable;
11937 ----------------
11938 -- Prepare_64 --
11939 ----------------
11941 function Prepare_64 (N : Node_Id) return Node_Id is
11942 begin
11943 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
11944 end Prepare_64;
11946 -- Start of processing for Optimize_Length_Comparison
11948 begin
11949 -- Nothing to do if not a comparison
11951 if Op not in N_Op_Compare then
11952 return;
11953 end if;
11955 -- Nothing to do if special -gnatd.P debug flag set
11957 if Debug_Flag_Dot_PP then
11958 return;
11959 end if;
11961 -- Ent'Length op 0/1
11963 if Is_Entity_Length (Left_Opnd (N))
11964 and then Is_Optimizable (Right_Opnd (N))
11965 then
11966 null;
11968 -- 0/1 op Ent'Length
11970 elsif Is_Entity_Length (Right_Opnd (N))
11971 and then Is_Optimizable (Left_Opnd (N))
11972 then
11973 -- Flip comparison to opposite sense
11975 case Op is
11976 when N_Op_Lt => Op := N_Op_Gt;
11977 when N_Op_Le => Op := N_Op_Ge;
11978 when N_Op_Gt => Op := N_Op_Lt;
11979 when N_Op_Ge => Op := N_Op_Le;
11980 when others => null;
11981 end case;
11983 -- Else optimization not possible
11985 else
11986 return;
11987 end if;
11989 -- Fall through if we will do the optimization
11991 -- Cases to handle:
11993 -- X'Length = 0 => X'First > X'Last
11994 -- X'Length = 1 => X'First = X'Last
11995 -- X'Length = n => X'First + (n - 1) = X'Last
11997 -- X'Length /= 0 => X'First <= X'Last
11998 -- X'Length /= 1 => X'First /= X'Last
11999 -- X'Length /= n => X'First + (n - 1) /= X'Last
12001 -- X'Length >= 0 => always true, warn
12002 -- X'Length >= 1 => X'First <= X'Last
12003 -- X'Length >= n => X'First + (n - 1) <= X'Last
12005 -- X'Length > 0 => X'First <= X'Last
12006 -- X'Length > 1 => X'First < X'Last
12007 -- X'Length > n => X'First + (n - 1) < X'Last
12009 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
12010 -- X'Length <= 1 => X'First >= X'Last
12011 -- X'Length <= n => X'First + (n - 1) >= X'Last
12013 -- X'Length < 0 => always false (warn)
12014 -- X'Length < 1 => X'First > X'Last
12015 -- X'Length < n => X'First + (n - 1) > X'Last
12017 -- Note: for the cases of n (not constant 0,1), we require that the
12018 -- corresponding index type be integer or shorter (i.e. not 64-bit),
12019 -- and the same for the comparison value. Then we do the comparison
12020 -- using 64-bit arithmetic (actually long long integer), so that we
12021 -- cannot have overflow intefering with the result.
12023 -- First deal with warning cases
12025 if Is_Zero then
12026 case Op is
12028 -- X'Length >= 0
12030 when N_Op_Ge =>
12031 Rewrite (N,
12032 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
12033 Analyze_And_Resolve (N, Typ);
12034 Warn_On_Known_Condition (N);
12035 return;
12037 -- X'Length < 0
12039 when N_Op_Lt =>
12040 Rewrite (N,
12041 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
12042 Analyze_And_Resolve (N, Typ);
12043 Warn_On_Known_Condition (N);
12044 return;
12046 when N_Op_Le =>
12047 if Constant_Condition_Warnings
12048 and then Comes_From_Source (Original_Node (N))
12049 then
12050 Error_Msg_N ("could replace by ""'=""?", N);
12051 end if;
12053 Op := N_Op_Eq;
12055 when others =>
12056 null;
12057 end case;
12058 end if;
12060 -- Build the First reference we will use
12062 Left :=
12063 Make_Attribute_Reference (Loc,
12064 Prefix => New_Occurrence_Of (Ent, Loc),
12065 Attribute_Name => Name_First);
12067 if Present (Index) then
12068 Set_Expressions (Left, New_List (New_Copy (Index)));
12069 end if;
12071 -- If general value case, then do the addition of (n - 1), and
12072 -- also add the needed conversions to type Long_Long_Integer.
12074 if Present (Comp) then
12075 Left :=
12076 Make_Op_Add (Loc,
12077 Left_Opnd => Prepare_64 (Left),
12078 Right_Opnd =>
12079 Make_Op_Subtract (Loc,
12080 Left_Opnd => Prepare_64 (Comp),
12081 Right_Opnd => Make_Integer_Literal (Loc, 1)));
12082 end if;
12084 -- Build the Last reference we will use
12086 Right :=
12087 Make_Attribute_Reference (Loc,
12088 Prefix => New_Occurrence_Of (Ent, Loc),
12089 Attribute_Name => Name_Last);
12091 if Present (Index) then
12092 Set_Expressions (Right, New_List (New_Copy (Index)));
12093 end if;
12095 -- If general operand, convert Last reference to Long_Long_Integer
12097 if Present (Comp) then
12098 Right := Prepare_64 (Right);
12099 end if;
12101 -- Check for cases to optimize
12103 -- X'Length = 0 => X'First > X'Last
12104 -- X'Length < 1 => X'First > X'Last
12105 -- X'Length < n => X'First + (n - 1) > X'Last
12107 if (Is_Zero and then Op = N_Op_Eq)
12108 or else (not Is_Zero and then Op = N_Op_Lt)
12109 then
12110 Result :=
12111 Make_Op_Gt (Loc,
12112 Left_Opnd => Left,
12113 Right_Opnd => Right);
12115 -- X'Length = 1 => X'First = X'Last
12116 -- X'Length = n => X'First + (n - 1) = X'Last
12118 elsif not Is_Zero and then Op = N_Op_Eq then
12119 Result :=
12120 Make_Op_Eq (Loc,
12121 Left_Opnd => Left,
12122 Right_Opnd => Right);
12124 -- X'Length /= 0 => X'First <= X'Last
12125 -- X'Length > 0 => X'First <= X'Last
12127 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
12128 Result :=
12129 Make_Op_Le (Loc,
12130 Left_Opnd => Left,
12131 Right_Opnd => Right);
12133 -- X'Length /= 1 => X'First /= X'Last
12134 -- X'Length /= n => X'First + (n - 1) /= X'Last
12136 elsif not Is_Zero and then Op = N_Op_Ne then
12137 Result :=
12138 Make_Op_Ne (Loc,
12139 Left_Opnd => Left,
12140 Right_Opnd => Right);
12142 -- X'Length >= 1 => X'First <= X'Last
12143 -- X'Length >= n => X'First + (n - 1) <= X'Last
12145 elsif not Is_Zero and then Op = N_Op_Ge then
12146 Result :=
12147 Make_Op_Le (Loc,
12148 Left_Opnd => Left,
12149 Right_Opnd => Right);
12151 -- X'Length > 1 => X'First < X'Last
12152 -- X'Length > n => X'First + (n = 1) < X'Last
12154 elsif not Is_Zero and then Op = N_Op_Gt then
12155 Result :=
12156 Make_Op_Lt (Loc,
12157 Left_Opnd => Left,
12158 Right_Opnd => Right);
12160 -- X'Length <= 1 => X'First >= X'Last
12161 -- X'Length <= n => X'First + (n - 1) >= X'Last
12163 elsif not Is_Zero and then Op = N_Op_Le then
12164 Result :=
12165 Make_Op_Ge (Loc,
12166 Left_Opnd => Left,
12167 Right_Opnd => Right);
12169 -- Should not happen at this stage
12171 else
12172 raise Program_Error;
12173 end if;
12175 -- Rewrite and finish up
12177 Rewrite (N, Result);
12178 Analyze_And_Resolve (N, Typ);
12179 return;
12180 end Optimize_Length_Comparison;
12182 ------------------------
12183 -- Rewrite_Comparison --
12184 ------------------------
12186 procedure Rewrite_Comparison (N : Node_Id) is
12187 Warning_Generated : Boolean := False;
12188 -- Set to True if first pass with Assume_Valid generates a warning in
12189 -- which case we skip the second pass to avoid warning overloaded.
12191 Result : Node_Id;
12192 -- Set to Standard_True or Standard_False
12194 begin
12195 if Nkind (N) = N_Type_Conversion then
12196 Rewrite_Comparison (Expression (N));
12197 return;
12199 elsif Nkind (N) not in N_Op_Compare then
12200 return;
12201 end if;
12203 -- Now start looking at the comparison in detail. We potentially go
12204 -- through this loop twice. The first time, Assume_Valid is set False
12205 -- in the call to Compile_Time_Compare. If this call results in a
12206 -- clear result of always True or Always False, that's decisive and
12207 -- we are done. Otherwise we repeat the processing with Assume_Valid
12208 -- set to True to generate additional warnings. We can skip that step
12209 -- if Constant_Condition_Warnings is False.
12211 for AV in False .. True loop
12212 declare
12213 Typ : constant Entity_Id := Etype (N);
12214 Op1 : constant Node_Id := Left_Opnd (N);
12215 Op2 : constant Node_Id := Right_Opnd (N);
12217 Res : constant Compare_Result :=
12218 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
12219 -- Res indicates if compare outcome can be compile time determined
12221 True_Result : Boolean;
12222 False_Result : Boolean;
12224 begin
12225 case N_Op_Compare (Nkind (N)) is
12226 when N_Op_Eq =>
12227 True_Result := Res = EQ;
12228 False_Result := Res = LT or else Res = GT or else Res = NE;
12230 when N_Op_Ge =>
12231 True_Result := Res in Compare_GE;
12232 False_Result := Res = LT;
12234 if Res = LE
12235 and then Constant_Condition_Warnings
12236 and then Comes_From_Source (Original_Node (N))
12237 and then Nkind (Original_Node (N)) = N_Op_Ge
12238 and then not In_Instance
12239 and then Is_Integer_Type (Etype (Left_Opnd (N)))
12240 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
12241 then
12242 Error_Msg_N
12243 ("can never be greater than, could replace by ""'=""?", N);
12244 Warning_Generated := True;
12245 end if;
12247 when N_Op_Gt =>
12248 True_Result := Res = GT;
12249 False_Result := Res in Compare_LE;
12251 when N_Op_Lt =>
12252 True_Result := Res = LT;
12253 False_Result := Res in Compare_GE;
12255 when N_Op_Le =>
12256 True_Result := Res in Compare_LE;
12257 False_Result := Res = GT;
12259 if Res = GE
12260 and then Constant_Condition_Warnings
12261 and then Comes_From_Source (Original_Node (N))
12262 and then Nkind (Original_Node (N)) = N_Op_Le
12263 and then not In_Instance
12264 and then Is_Integer_Type (Etype (Left_Opnd (N)))
12265 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
12266 then
12267 Error_Msg_N
12268 ("can never be less than, could replace by ""'=""?", N);
12269 Warning_Generated := True;
12270 end if;
12272 when N_Op_Ne =>
12273 True_Result := Res = NE or else Res = GT or else Res = LT;
12274 False_Result := Res = EQ;
12275 end case;
12277 -- If this is the first iteration, then we actually convert the
12278 -- comparison into True or False, if the result is certain.
12280 if AV = False then
12281 if True_Result or False_Result then
12282 Result := Boolean_Literals (True_Result);
12283 Rewrite (N,
12284 Convert_To (Typ,
12285 New_Occurrence_Of (Result, Sloc (N))));
12286 Analyze_And_Resolve (N, Typ);
12287 Warn_On_Known_Condition (N);
12288 return;
12289 end if;
12291 -- If this is the second iteration (AV = True), and the original
12292 -- node comes from source and we are not in an instance, then give
12293 -- a warning if we know result would be True or False. Note: we
12294 -- know Constant_Condition_Warnings is set if we get here.
12296 elsif Comes_From_Source (Original_Node (N))
12297 and then not In_Instance
12298 then
12299 if True_Result then
12300 Error_Msg_N
12301 ("condition can only be False if invalid values present?",
12303 elsif False_Result then
12304 Error_Msg_N
12305 ("condition can only be True if invalid values present?",
12307 end if;
12308 end if;
12309 end;
12311 -- Skip second iteration if not warning on constant conditions or
12312 -- if the first iteration already generated a warning of some kind or
12313 -- if we are in any case assuming all values are valid (so that the
12314 -- first iteration took care of the valid case).
12316 exit when not Constant_Condition_Warnings;
12317 exit when Warning_Generated;
12318 exit when Assume_No_Invalid_Values;
12319 end loop;
12320 end Rewrite_Comparison;
12322 ----------------------------
12323 -- Safe_In_Place_Array_Op --
12324 ----------------------------
12326 function Safe_In_Place_Array_Op
12327 (Lhs : Node_Id;
12328 Op1 : Node_Id;
12329 Op2 : Node_Id) return Boolean
12331 Target : Entity_Id;
12333 function Is_Safe_Operand (Op : Node_Id) return Boolean;
12334 -- Operand is safe if it cannot overlap part of the target of the
12335 -- operation. If the operand and the target are identical, the operand
12336 -- is safe. The operand can be empty in the case of negation.
12338 function Is_Unaliased (N : Node_Id) return Boolean;
12339 -- Check that N is a stand-alone entity
12341 ------------------
12342 -- Is_Unaliased --
12343 ------------------
12345 function Is_Unaliased (N : Node_Id) return Boolean is
12346 begin
12347 return
12348 Is_Entity_Name (N)
12349 and then No (Address_Clause (Entity (N)))
12350 and then No (Renamed_Object (Entity (N)));
12351 end Is_Unaliased;
12353 ---------------------
12354 -- Is_Safe_Operand --
12355 ---------------------
12357 function Is_Safe_Operand (Op : Node_Id) return Boolean is
12358 begin
12359 if No (Op) then
12360 return True;
12362 elsif Is_Entity_Name (Op) then
12363 return Is_Unaliased (Op);
12365 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
12366 return Is_Unaliased (Prefix (Op));
12368 elsif Nkind (Op) = N_Slice then
12369 return
12370 Is_Unaliased (Prefix (Op))
12371 and then Entity (Prefix (Op)) /= Target;
12373 elsif Nkind (Op) = N_Op_Not then
12374 return Is_Safe_Operand (Right_Opnd (Op));
12376 else
12377 return False;
12378 end if;
12379 end Is_Safe_Operand;
12381 -- Start of processing for Safe_In_Place_Array_Op
12383 begin
12384 -- Skip this processing if the component size is different from system
12385 -- storage unit (since at least for NOT this would cause problems).
12387 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
12388 return False;
12390 -- Cannot do in place stuff on VM_Target since cannot pass addresses
12392 elsif VM_Target /= No_VM then
12393 return False;
12395 -- Cannot do in place stuff if non-standard Boolean representation
12397 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
12398 return False;
12400 elsif not Is_Unaliased (Lhs) then
12401 return False;
12403 else
12404 Target := Entity (Lhs);
12405 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
12406 end if;
12407 end Safe_In_Place_Array_Op;
12409 -----------------------
12410 -- Tagged_Membership --
12411 -----------------------
12413 -- There are two different cases to consider depending on whether the right
12414 -- operand is a class-wide type or not. If not we just compare the actual
12415 -- tag of the left expr to the target type tag:
12417 -- Left_Expr.Tag = Right_Type'Tag;
12419 -- If it is a class-wide type we use the RT function CW_Membership which is
12420 -- usually implemented by looking in the ancestor tables contained in the
12421 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
12423 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
12424 -- function IW_Membership which is usually implemented by looking in the
12425 -- table of abstract interface types plus the ancestor table contained in
12426 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
12428 procedure Tagged_Membership
12429 (N : Node_Id;
12430 SCIL_Node : out Node_Id;
12431 Result : out Node_Id)
12433 Left : constant Node_Id := Left_Opnd (N);
12434 Right : constant Node_Id := Right_Opnd (N);
12435 Loc : constant Source_Ptr := Sloc (N);
12437 Full_R_Typ : Entity_Id;
12438 Left_Type : Entity_Id;
12439 New_Node : Node_Id;
12440 Right_Type : Entity_Id;
12441 Obj_Tag : Node_Id;
12443 begin
12444 SCIL_Node := Empty;
12446 -- Handle entities from the limited view
12448 Left_Type := Available_View (Etype (Left));
12449 Right_Type := Available_View (Etype (Right));
12451 -- In the case where the type is an access type, the test is applied
12452 -- using the designated types (needed in Ada 2012 for implicit anonymous
12453 -- access conversions, for AI05-0149).
12455 if Is_Access_Type (Right_Type) then
12456 Left_Type := Designated_Type (Left_Type);
12457 Right_Type := Designated_Type (Right_Type);
12458 end if;
12460 if Is_Class_Wide_Type (Left_Type) then
12461 Left_Type := Root_Type (Left_Type);
12462 end if;
12464 if Is_Class_Wide_Type (Right_Type) then
12465 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
12466 else
12467 Full_R_Typ := Underlying_Type (Right_Type);
12468 end if;
12470 Obj_Tag :=
12471 Make_Selected_Component (Loc,
12472 Prefix => Relocate_Node (Left),
12473 Selector_Name =>
12474 New_Reference_To (First_Tag_Component (Left_Type), Loc));
12476 if Is_Class_Wide_Type (Right_Type) then
12478 -- No need to issue a run-time check if we statically know that the
12479 -- result of this membership test is always true. For example,
12480 -- considering the following declarations:
12482 -- type Iface is interface;
12483 -- type T is tagged null record;
12484 -- type DT is new T and Iface with null record;
12486 -- Obj1 : T;
12487 -- Obj2 : DT;
12489 -- These membership tests are always true:
12491 -- Obj1 in T'Class
12492 -- Obj2 in T'Class;
12493 -- Obj2 in Iface'Class;
12495 -- We do not need to handle cases where the membership is illegal.
12496 -- For example:
12498 -- Obj1 in DT'Class; -- Compile time error
12499 -- Obj1 in Iface'Class; -- Compile time error
12501 if not Is_Class_Wide_Type (Left_Type)
12502 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
12503 Use_Full_View => True)
12504 or else (Is_Interface (Etype (Right_Type))
12505 and then Interface_Present_In_Ancestor
12506 (Typ => Left_Type,
12507 Iface => Etype (Right_Type))))
12508 then
12509 Result := New_Reference_To (Standard_True, Loc);
12510 return;
12511 end if;
12513 -- Ada 2005 (AI-251): Class-wide applied to interfaces
12515 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
12517 -- Support to: "Iface_CW_Typ in Typ'Class"
12519 or else Is_Interface (Left_Type)
12520 then
12521 -- Issue error if IW_Membership operation not available in a
12522 -- configurable run time setting.
12524 if not RTE_Available (RE_IW_Membership) then
12525 Error_Msg_CRT
12526 ("dynamic membership test on interface types", N);
12527 Result := Empty;
12528 return;
12529 end if;
12531 Result :=
12532 Make_Function_Call (Loc,
12533 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
12534 Parameter_Associations => New_List (
12535 Make_Attribute_Reference (Loc,
12536 Prefix => Obj_Tag,
12537 Attribute_Name => Name_Address),
12538 New_Reference_To (
12539 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
12540 Loc)));
12542 -- Ada 95: Normal case
12544 else
12545 Build_CW_Membership (Loc,
12546 Obj_Tag_Node => Obj_Tag,
12547 Typ_Tag_Node =>
12548 New_Reference_To (
12549 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc),
12550 Related_Nod => N,
12551 New_Node => New_Node);
12553 -- Generate the SCIL node for this class-wide membership test.
12554 -- Done here because the previous call to Build_CW_Membership
12555 -- relocates Obj_Tag.
12557 if Generate_SCIL then
12558 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
12559 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
12560 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
12561 end if;
12563 Result := New_Node;
12564 end if;
12566 -- Right_Type is not a class-wide type
12568 else
12569 -- No need to check the tag of the object if Right_Typ is abstract
12571 if Is_Abstract_Type (Right_Type) then
12572 Result := New_Reference_To (Standard_False, Loc);
12574 else
12575 Result :=
12576 Make_Op_Eq (Loc,
12577 Left_Opnd => Obj_Tag,
12578 Right_Opnd =>
12579 New_Reference_To
12580 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
12581 end if;
12582 end if;
12583 end Tagged_Membership;
12585 ------------------------------
12586 -- Unary_Op_Validity_Checks --
12587 ------------------------------
12589 procedure Unary_Op_Validity_Checks (N : Node_Id) is
12590 begin
12591 if Validity_Checks_On and Validity_Check_Operands then
12592 Ensure_Valid (Right_Opnd (N));
12593 end if;
12594 end Unary_Op_Validity_Checks;
12596 end Exp_Ch4;