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