2015-05-22 Eric Botcazou <ebotcazou@adacore.com>
[official-gcc.git] / gcc / ada / exp_ch4.adb
blob0ef690b0f9313a6cc05e4ec44858d2487c840960
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 := Corresponding_Spec_Of (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 -- Do not set the base pool and mode of operation on .NET/JVM since
548 -- those targets do not support pools and all VM masters defaulted to
549 -- heterogeneous.
551 if VM_Target = No_VM then
553 -- Generate:
554 -- Set_Base_Pool
555 -- (<FM_Id>, Global_Pool_Object'Unrestricted_Access);
557 Insert_And_Analyze (Decls,
558 Make_Procedure_Call_Statement (Loc,
559 Name =>
560 New_Occurrence_Of (RTE (RE_Set_Base_Pool), Loc),
561 Parameter_Associations => New_List (
562 New_Occurrence_Of (FM_Id, Loc),
563 Make_Attribute_Reference (Loc,
564 Prefix =>
565 New_Occurrence_Of (RTE (RE_Global_Pool_Object), Loc),
566 Attribute_Name => Name_Unrestricted_Access))));
568 -- Generate:
569 -- Set_Is_Heterogeneous (<FM_Id>);
571 Insert_And_Analyze (Decls,
572 Make_Procedure_Call_Statement (Loc,
573 Name =>
574 New_Occurrence_Of (RTE (RE_Set_Is_Heterogeneous), Loc),
575 Parameter_Associations => New_List (
576 New_Occurrence_Of (FM_Id, Loc))));
577 end if;
579 Pop_Scope;
580 return FM_Id;
581 end Create_Anonymous_Master;
583 -- Local declarations
585 Unit_Decl : Node_Id;
586 Unit_Id : Entity_Id;
588 -- Start of processing for Current_Anonymous_Master
590 begin
591 Unit_Decl := Unit (Cunit (Current_Sem_Unit));
592 Unit_Id := Defining_Entity (Unit_Decl);
594 -- The compilation unit is a package instantiation. In this case the
595 -- anonymous master is associated with the package spec as both the
596 -- spec and body appear at the same level.
598 if Nkind (Unit_Decl) = N_Package_Body
599 and then Nkind (Original_Node (Unit_Decl)) = N_Package_Instantiation
600 then
601 Unit_Id := Corresponding_Spec (Unit_Decl);
602 Unit_Decl := Unit_Declaration_Node (Unit_Id);
603 end if;
605 if Present (Anonymous_Master (Unit_Id)) then
606 return Anonymous_Master (Unit_Id);
608 -- Create a new anonymous master when allocating an object of anonymous
609 -- access-to-controlled type for the first time.
611 else
612 return Create_Anonymous_Master (Unit_Id, Unit_Decl);
613 end if;
614 end Current_Anonymous_Master;
616 --------------------------------
617 -- Displace_Allocator_Pointer --
618 --------------------------------
620 procedure Displace_Allocator_Pointer (N : Node_Id) is
621 Loc : constant Source_Ptr := Sloc (N);
622 Orig_Node : constant Node_Id := Original_Node (N);
623 Dtyp : Entity_Id;
624 Etyp : Entity_Id;
625 PtrT : Entity_Id;
627 begin
628 -- Do nothing in case of VM targets: the virtual machine will handle
629 -- interfaces directly.
631 if not Tagged_Type_Expansion then
632 return;
633 end if;
635 pragma Assert (Nkind (N) = N_Identifier
636 and then Nkind (Orig_Node) = N_Allocator);
638 PtrT := Etype (Orig_Node);
639 Dtyp := Available_View (Designated_Type (PtrT));
640 Etyp := Etype (Expression (Orig_Node));
642 if Is_Class_Wide_Type (Dtyp) and then Is_Interface (Dtyp) then
644 -- If the type of the allocator expression is not an interface type
645 -- we can generate code to reference the record component containing
646 -- the pointer to the secondary dispatch table.
648 if not Is_Interface (Etyp) then
649 declare
650 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
652 begin
653 -- 1) Get access to the allocated object
655 Rewrite (N,
656 Make_Explicit_Dereference (Loc, Relocate_Node (N)));
657 Set_Etype (N, Etyp);
658 Set_Analyzed (N);
660 -- 2) Add the conversion to displace the pointer to reference
661 -- the secondary dispatch table.
663 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
664 Analyze_And_Resolve (N, Dtyp);
666 -- 3) The 'access to the secondary dispatch table will be used
667 -- as the value returned by the allocator.
669 Rewrite (N,
670 Make_Attribute_Reference (Loc,
671 Prefix => Relocate_Node (N),
672 Attribute_Name => Name_Access));
673 Set_Etype (N, Saved_Typ);
674 Set_Analyzed (N);
675 end;
677 -- If the type of the allocator expression is an interface type we
678 -- generate a run-time call to displace "this" to reference the
679 -- component containing the pointer to the secondary dispatch table
680 -- or else raise Constraint_Error if the actual object does not
681 -- implement the target interface. This case corresponds to the
682 -- following example:
684 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
685 -- begin
686 -- return new Iface_2'Class'(Obj);
687 -- end Op;
689 else
690 Rewrite (N,
691 Unchecked_Convert_To (PtrT,
692 Make_Function_Call (Loc,
693 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
694 Parameter_Associations => New_List (
695 Unchecked_Convert_To (RTE (RE_Address),
696 Relocate_Node (N)),
698 New_Occurrence_Of
699 (Elists.Node
700 (First_Elmt
701 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
702 Loc)))));
703 Analyze_And_Resolve (N, PtrT);
704 end if;
705 end if;
706 end Displace_Allocator_Pointer;
708 ---------------------------------
709 -- Expand_Allocator_Expression --
710 ---------------------------------
712 procedure Expand_Allocator_Expression (N : Node_Id) is
713 Loc : constant Source_Ptr := Sloc (N);
714 Exp : constant Node_Id := Expression (Expression (N));
715 PtrT : constant Entity_Id := Etype (N);
716 DesigT : constant Entity_Id := Designated_Type (PtrT);
718 procedure Apply_Accessibility_Check
719 (Ref : Node_Id;
720 Built_In_Place : Boolean := False);
721 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
722 -- type, generate an accessibility check to verify that the level of the
723 -- type of the created object is not deeper than the level of the access
724 -- type. If the type of the qualified expression is class-wide, then
725 -- always generate the check (except in the case where it is known to be
726 -- unnecessary, see comment below). Otherwise, only generate the check
727 -- if the level of the qualified expression type is statically deeper
728 -- than the access type.
730 -- Although the static accessibility will generally have been performed
731 -- as a legality check, it won't have been done in cases where the
732 -- allocator appears in generic body, so a run-time check is needed in
733 -- general. One special case is when the access type is declared in the
734 -- same scope as the class-wide allocator, in which case the check can
735 -- never fail, so it need not be generated.
737 -- As an open issue, there seem to be cases where the static level
738 -- associated with the class-wide object's underlying type is not
739 -- sufficient to perform the proper accessibility check, such as for
740 -- allocators in nested subprograms or accept statements initialized by
741 -- class-wide formals when the actual originates outside at a deeper
742 -- static level. The nested subprogram case might require passing
743 -- accessibility levels along with class-wide parameters, and the task
744 -- case seems to be an actual gap in the language rules that needs to
745 -- be fixed by the ARG. ???
747 -------------------------------
748 -- Apply_Accessibility_Check --
749 -------------------------------
751 procedure Apply_Accessibility_Check
752 (Ref : Node_Id;
753 Built_In_Place : Boolean := False)
755 Pool_Id : constant Entity_Id := Associated_Storage_Pool (PtrT);
756 Cond : Node_Id;
757 Fin_Call : Node_Id;
758 Free_Stmt : Node_Id;
759 Obj_Ref : Node_Id;
760 Stmts : List_Id;
762 begin
763 if Ada_Version >= Ada_2005
764 and then Is_Class_Wide_Type (DesigT)
765 and then (Tagged_Type_Expansion or else VM_Target /= No_VM)
766 and then not Scope_Suppress.Suppress (Accessibility_Check)
767 and then
768 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
769 or else
770 (Is_Class_Wide_Type (Etype (Exp))
771 and then Scope (PtrT) /= Current_Scope))
772 then
773 -- If the allocator was built in place, Ref is already a reference
774 -- to the access object initialized to the result of the allocator
775 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
776 -- Remove_Side_Effects for cases where the build-in-place call may
777 -- still be the prefix of the reference (to avoid generating
778 -- duplicate calls). Otherwise, it is the entity associated with
779 -- the object containing the address of the allocated object.
781 if Built_In_Place then
782 Remove_Side_Effects (Ref);
783 Obj_Ref := New_Copy_Tree (Ref);
784 else
785 Obj_Ref := New_Occurrence_Of (Ref, Loc);
786 end if;
788 -- For access to interface types we must generate code to displace
789 -- the pointer to the base of the object since the subsequent code
790 -- references components located in the TSD of the object (which
791 -- is associated with the primary dispatch table --see a-tags.ads)
792 -- and also generates code invoking Free, which requires also a
793 -- reference to the base of the unallocated object.
795 if Is_Interface (DesigT) and then Tagged_Type_Expansion then
796 Obj_Ref :=
797 Unchecked_Convert_To (Etype (Obj_Ref),
798 Make_Function_Call (Loc,
799 Name =>
800 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
801 Parameter_Associations => New_List (
802 Unchecked_Convert_To (RTE (RE_Address),
803 New_Copy_Tree (Obj_Ref)))));
804 end if;
806 -- Step 1: Create the object clean up code
808 Stmts := New_List;
810 -- Deallocate the object if the accessibility check fails. This
811 -- is done only on targets or profiles that support deallocation.
813 -- Free (Obj_Ref);
815 if RTE_Available (RE_Free) then
816 Free_Stmt := Make_Free_Statement (Loc, New_Copy_Tree (Obj_Ref));
817 Set_Storage_Pool (Free_Stmt, Pool_Id);
819 Append_To (Stmts, Free_Stmt);
821 -- The target or profile cannot deallocate objects
823 else
824 Free_Stmt := Empty;
825 end if;
827 -- Finalize the object if applicable. Generate:
829 -- [Deep_]Finalize (Obj_Ref.all);
831 if Needs_Finalization (DesigT) then
832 Fin_Call :=
833 Make_Final_Call
834 (Obj_Ref =>
835 Make_Explicit_Dereference (Loc, New_Copy (Obj_Ref)),
836 Typ => DesigT);
838 -- When the target or profile supports deallocation, wrap the
839 -- finalization call in a block to ensure proper deallocation
840 -- even if finalization fails. Generate:
842 -- begin
843 -- <Fin_Call>
844 -- exception
845 -- when others =>
846 -- <Free_Stmt>
847 -- raise;
848 -- end;
850 if Present (Free_Stmt) then
851 Fin_Call :=
852 Make_Block_Statement (Loc,
853 Handled_Statement_Sequence =>
854 Make_Handled_Sequence_Of_Statements (Loc,
855 Statements => New_List (Fin_Call),
857 Exception_Handlers => New_List (
858 Make_Exception_Handler (Loc,
859 Exception_Choices => New_List (
860 Make_Others_Choice (Loc)),
861 Statements => New_List (
862 New_Copy_Tree (Free_Stmt),
863 Make_Raise_Statement (Loc))))));
864 end if;
866 Prepend_To (Stmts, Fin_Call);
867 end if;
869 -- Signal the accessibility failure through a Program_Error
871 Append_To (Stmts,
872 Make_Raise_Program_Error (Loc,
873 Condition => New_Occurrence_Of (Standard_True, Loc),
874 Reason => PE_Accessibility_Check_Failed));
876 -- Step 2: Create the accessibility comparison
878 -- Generate:
879 -- Ref'Tag
881 Obj_Ref :=
882 Make_Attribute_Reference (Loc,
883 Prefix => Obj_Ref,
884 Attribute_Name => Name_Tag);
886 -- For tagged types, determine the accessibility level by looking
887 -- at the type specific data of the dispatch table. Generate:
889 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
891 if Tagged_Type_Expansion then
892 Cond := Build_Get_Access_Level (Loc, Obj_Ref);
894 -- Use a runtime call to determine the accessibility level when
895 -- compiling on virtual machine targets. Generate:
897 -- Get_Access_Level (Ref'Tag)
899 else
900 Cond :=
901 Make_Function_Call (Loc,
902 Name =>
903 New_Occurrence_Of (RTE (RE_Get_Access_Level), Loc),
904 Parameter_Associations => New_List (Obj_Ref));
905 end if;
907 Cond :=
908 Make_Op_Gt (Loc,
909 Left_Opnd => Cond,
910 Right_Opnd =>
911 Make_Integer_Literal (Loc, Type_Access_Level (PtrT)));
913 -- Due to the complexity and side effects of the check, utilize an
914 -- if statement instead of the regular Program_Error circuitry.
916 Insert_Action (N,
917 Make_Implicit_If_Statement (N,
918 Condition => Cond,
919 Then_Statements => Stmts));
920 end if;
921 end Apply_Accessibility_Check;
923 -- Local variables
925 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
926 Indic : constant Node_Id := Subtype_Mark (Expression (N));
927 T : constant Entity_Id := Entity (Indic);
928 Node : Node_Id;
929 Tag_Assign : Node_Id;
930 Temp : Entity_Id;
931 Temp_Decl : Node_Id;
933 TagT : Entity_Id := Empty;
934 -- Type used as source for tag assignment
936 TagR : Node_Id := Empty;
937 -- Target reference for tag assignment
939 -- Start of processing for Expand_Allocator_Expression
941 begin
942 -- Handle call to C++ constructor
944 if Is_CPP_Constructor_Call (Exp) then
945 Make_CPP_Constructor_Call_In_Allocator
946 (Allocator => N,
947 Function_Call => Exp);
948 return;
949 end if;
951 -- In the case of an Ada 2012 allocator whose initial value comes from a
952 -- function call, pass "the accessibility level determined by the point
953 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
954 -- Expand_Call but it couldn't be done there (because the Etype of the
955 -- allocator wasn't set then) so we generate the parameter here. See
956 -- the Boolean variable Defer in (a block within) Expand_Call.
958 if Ada_Version >= Ada_2012 and then Nkind (Exp) = N_Function_Call then
959 declare
960 Subp : Entity_Id;
962 begin
963 if Nkind (Name (Exp)) = N_Explicit_Dereference then
964 Subp := Designated_Type (Etype (Prefix (Name (Exp))));
965 else
966 Subp := Entity (Name (Exp));
967 end if;
969 Subp := Ultimate_Alias (Subp);
971 if Present (Extra_Accessibility_Of_Result (Subp)) then
972 Add_Extra_Actual_To_Call
973 (Subprogram_Call => Exp,
974 Extra_Formal => Extra_Accessibility_Of_Result (Subp),
975 Extra_Actual => Dynamic_Accessibility_Level (PtrT));
976 end if;
977 end;
978 end if;
980 -- Case of tagged type or type requiring finalization
982 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
984 -- Ada 2005 (AI-318-02): If the initialization expression is a call
985 -- to a build-in-place function, then access to the allocated object
986 -- must be passed to the function. Currently we limit such functions
987 -- to those with constrained limited result subtypes, but eventually
988 -- we plan to expand the allowed forms of functions that are treated
989 -- as build-in-place.
991 if Ada_Version >= Ada_2005
992 and then Is_Build_In_Place_Function_Call (Exp)
993 then
994 Make_Build_In_Place_Call_In_Allocator (N, Exp);
995 Apply_Accessibility_Check (N, Built_In_Place => True);
996 return;
997 end if;
999 -- Actions inserted before:
1000 -- Temp : constant ptr_T := new T'(Expression);
1001 -- Temp._tag = T'tag; -- when not class-wide
1002 -- [Deep_]Adjust (Temp.all);
1004 -- We analyze by hand the new internal allocator to avoid any
1005 -- recursion and inappropriate call to Initialize.
1007 -- We don't want to remove side effects when the expression must be
1008 -- built in place. In the case of a build-in-place function call,
1009 -- that could lead to a duplication of the call, which was already
1010 -- substituted for the allocator.
1012 if not Aggr_In_Place then
1013 Remove_Side_Effects (Exp);
1014 end if;
1016 Temp := Make_Temporary (Loc, 'P', N);
1018 -- For a class wide allocation generate the following code:
1020 -- type Equiv_Record is record ... end record;
1021 -- implicit subtype CW is <Class_Wide_Subytpe>;
1022 -- temp : PtrT := new CW'(CW!(expr));
1024 if Is_Class_Wide_Type (T) then
1025 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
1027 -- Ada 2005 (AI-251): If the expression is a class-wide interface
1028 -- object we generate code to move up "this" to reference the
1029 -- base of the object before allocating the new object.
1031 -- Note that Exp'Address is recursively expanded into a call
1032 -- to Base_Address (Exp.Tag)
1034 if Is_Class_Wide_Type (Etype (Exp))
1035 and then Is_Interface (Etype (Exp))
1036 and then Tagged_Type_Expansion
1037 then
1038 Set_Expression
1039 (Expression (N),
1040 Unchecked_Convert_To (Entity (Indic),
1041 Make_Explicit_Dereference (Loc,
1042 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
1043 Make_Attribute_Reference (Loc,
1044 Prefix => Exp,
1045 Attribute_Name => Name_Address)))));
1046 else
1047 Set_Expression
1048 (Expression (N),
1049 Unchecked_Convert_To (Entity (Indic), Exp));
1050 end if;
1052 Analyze_And_Resolve (Expression (N), Entity (Indic));
1053 end if;
1055 -- Processing for allocators returning non-interface types
1057 if not Is_Interface (Directly_Designated_Type (PtrT)) then
1058 if Aggr_In_Place then
1059 Temp_Decl :=
1060 Make_Object_Declaration (Loc,
1061 Defining_Identifier => Temp,
1062 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1063 Expression =>
1064 Make_Allocator (Loc,
1065 Expression =>
1066 New_Occurrence_Of (Etype (Exp), Loc)));
1068 -- Copy the Comes_From_Source flag for the allocator we just
1069 -- built, since logically this allocator is a replacement of
1070 -- the original allocator node. This is for proper handling of
1071 -- restriction No_Implicit_Heap_Allocations.
1073 Set_Comes_From_Source
1074 (Expression (Temp_Decl), Comes_From_Source (N));
1076 Set_No_Initialization (Expression (Temp_Decl));
1077 Insert_Action (N, Temp_Decl);
1079 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1080 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1082 -- Attach the object to the associated finalization master.
1083 -- This is done manually on .NET/JVM since those compilers do
1084 -- no support pools and can't benefit from internally generated
1085 -- Allocate / Deallocate procedures.
1087 if VM_Target /= No_VM
1088 and then Is_Controlled (DesigT)
1089 and then Present (Finalization_Master (PtrT))
1090 then
1091 Insert_Action (N,
1092 Make_Attach_Call
1093 (Obj_Ref => New_Occurrence_Of (Temp, Loc),
1094 Ptr_Typ => PtrT));
1095 end if;
1097 else
1098 Node := Relocate_Node (N);
1099 Set_Analyzed (Node);
1101 Temp_Decl :=
1102 Make_Object_Declaration (Loc,
1103 Defining_Identifier => Temp,
1104 Constant_Present => True,
1105 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1106 Expression => Node);
1108 Insert_Action (N, Temp_Decl);
1109 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1111 -- Attach the object to the associated finalization master.
1112 -- This is done manually on .NET/JVM since those compilers do
1113 -- no support pools and can't benefit from internally generated
1114 -- Allocate / Deallocate procedures.
1116 if VM_Target /= No_VM
1117 and then Is_Controlled (DesigT)
1118 and then Present (Finalization_Master (PtrT))
1119 then
1120 Insert_Action (N,
1121 Make_Attach_Call
1122 (Obj_Ref => New_Occurrence_Of (Temp, Loc),
1123 Ptr_Typ => PtrT));
1124 end if;
1125 end if;
1127 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
1128 -- interface type. In this case we use the type of the qualified
1129 -- expression to allocate the object.
1131 else
1132 declare
1133 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
1134 New_Decl : Node_Id;
1136 begin
1137 New_Decl :=
1138 Make_Full_Type_Declaration (Loc,
1139 Defining_Identifier => Def_Id,
1140 Type_Definition =>
1141 Make_Access_To_Object_Definition (Loc,
1142 All_Present => True,
1143 Null_Exclusion_Present => False,
1144 Constant_Present =>
1145 Is_Access_Constant (Etype (N)),
1146 Subtype_Indication =>
1147 New_Occurrence_Of (Etype (Exp), Loc)));
1149 Insert_Action (N, New_Decl);
1151 -- Inherit the allocation-related attributes from the original
1152 -- access type.
1154 Set_Finalization_Master
1155 (Def_Id, Finalization_Master (PtrT));
1157 Set_Associated_Storage_Pool
1158 (Def_Id, Associated_Storage_Pool (PtrT));
1160 -- Declare the object using the previous type declaration
1162 if Aggr_In_Place then
1163 Temp_Decl :=
1164 Make_Object_Declaration (Loc,
1165 Defining_Identifier => Temp,
1166 Object_Definition => New_Occurrence_Of (Def_Id, Loc),
1167 Expression =>
1168 Make_Allocator (Loc,
1169 New_Occurrence_Of (Etype (Exp), Loc)));
1171 -- Copy the Comes_From_Source flag for the allocator we just
1172 -- built, since logically this allocator is a replacement of
1173 -- the original allocator node. This is for proper handling
1174 -- of restriction No_Implicit_Heap_Allocations.
1176 Set_Comes_From_Source
1177 (Expression (Temp_Decl), Comes_From_Source (N));
1179 Set_No_Initialization (Expression (Temp_Decl));
1180 Insert_Action (N, Temp_Decl);
1182 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1183 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1185 else
1186 Node := Relocate_Node (N);
1187 Set_Analyzed (Node);
1189 Temp_Decl :=
1190 Make_Object_Declaration (Loc,
1191 Defining_Identifier => Temp,
1192 Constant_Present => True,
1193 Object_Definition => New_Occurrence_Of (Def_Id, Loc),
1194 Expression => Node);
1196 Insert_Action (N, Temp_Decl);
1197 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1198 end if;
1200 -- Generate an additional object containing the address of the
1201 -- returned object. The type of this second object declaration
1202 -- is the correct type required for the common processing that
1203 -- is still performed by this subprogram. The displacement of
1204 -- this pointer to reference the component associated with the
1205 -- interface type will be done at the end of common processing.
1207 New_Decl :=
1208 Make_Object_Declaration (Loc,
1209 Defining_Identifier => Make_Temporary (Loc, 'P'),
1210 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1211 Expression =>
1212 Unchecked_Convert_To (PtrT,
1213 New_Occurrence_Of (Temp, Loc)));
1215 Insert_Action (N, New_Decl);
1217 Temp_Decl := New_Decl;
1218 Temp := Defining_Identifier (New_Decl);
1219 end;
1220 end if;
1222 Apply_Accessibility_Check (Temp);
1224 -- Generate the tag assignment
1226 -- Suppress the tag assignment when VM_Target because VM tags are
1227 -- represented implicitly in objects.
1229 if not Tagged_Type_Expansion then
1230 null;
1232 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1233 -- interface objects because in this case the tag does not change.
1235 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
1236 pragma Assert (Is_Class_Wide_Type
1237 (Directly_Designated_Type (Etype (N))));
1238 null;
1240 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
1241 TagT := T;
1242 TagR := New_Occurrence_Of (Temp, Loc);
1244 elsif Is_Private_Type (T)
1245 and then Is_Tagged_Type (Underlying_Type (T))
1246 then
1247 TagT := Underlying_Type (T);
1248 TagR :=
1249 Unchecked_Convert_To (Underlying_Type (T),
1250 Make_Explicit_Dereference (Loc,
1251 Prefix => New_Occurrence_Of (Temp, Loc)));
1252 end if;
1254 if Present (TagT) then
1255 declare
1256 Full_T : constant Entity_Id := Underlying_Type (TagT);
1258 begin
1259 Tag_Assign :=
1260 Make_Assignment_Statement (Loc,
1261 Name =>
1262 Make_Selected_Component (Loc,
1263 Prefix => TagR,
1264 Selector_Name =>
1265 New_Occurrence_Of
1266 (First_Tag_Component (Full_T), Loc)),
1268 Expression =>
1269 Unchecked_Convert_To (RTE (RE_Tag),
1270 New_Occurrence_Of
1271 (Elists.Node
1272 (First_Elmt (Access_Disp_Table (Full_T))), Loc)));
1273 end;
1275 -- The previous assignment has to be done in any case
1277 Set_Assignment_OK (Name (Tag_Assign));
1278 Insert_Action (N, Tag_Assign);
1279 end if;
1281 if Needs_Finalization (DesigT) and then Needs_Finalization (T) then
1283 -- Generate an Adjust call if the object will be moved. In Ada
1284 -- 2005, the object may be inherently limited, in which case
1285 -- there is no Adjust procedure, and the object is built in
1286 -- place. In Ada 95, the object can be limited but not
1287 -- inherently limited if this allocator came from a return
1288 -- statement (we're allocating the result on the secondary
1289 -- stack). In that case, the object will be moved, so we _do_
1290 -- want to Adjust.
1292 if not Aggr_In_Place
1293 and then not Is_Limited_View (T)
1294 then
1295 Insert_Action (N,
1297 -- An unchecked conversion is needed in the classwide case
1298 -- because the designated type can be an ancestor of the
1299 -- subtype mark of the allocator.
1301 Make_Adjust_Call
1302 (Obj_Ref =>
1303 Unchecked_Convert_To (T,
1304 Make_Explicit_Dereference (Loc,
1305 Prefix => New_Occurrence_Of (Temp, Loc))),
1306 Typ => T));
1307 end if;
1308 end if;
1310 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1311 Analyze_And_Resolve (N, PtrT);
1313 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1314 -- component containing the secondary dispatch table of the interface
1315 -- type.
1317 if Is_Interface (Directly_Designated_Type (PtrT)) then
1318 Displace_Allocator_Pointer (N);
1319 end if;
1321 elsif Aggr_In_Place then
1322 Temp := Make_Temporary (Loc, 'P', N);
1323 Temp_Decl :=
1324 Make_Object_Declaration (Loc,
1325 Defining_Identifier => Temp,
1326 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1327 Expression =>
1328 Make_Allocator (Loc,
1329 Expression => New_Occurrence_Of (Etype (Exp), Loc)));
1331 -- Copy the Comes_From_Source flag for the allocator we just built,
1332 -- since logically this allocator is a replacement of the original
1333 -- allocator node. This is for proper handling of restriction
1334 -- No_Implicit_Heap_Allocations.
1336 Set_Comes_From_Source
1337 (Expression (Temp_Decl), Comes_From_Source (N));
1339 Set_No_Initialization (Expression (Temp_Decl));
1340 Insert_Action (N, Temp_Decl);
1342 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1343 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1345 -- Attach the object to the associated finalization master. Thisis
1346 -- done manually on .NET/JVM since those compilers do no support
1347 -- pools and cannot benefit from internally generated Allocate and
1348 -- Deallocate procedures.
1350 if VM_Target /= No_VM
1351 and then Is_Controlled (DesigT)
1352 and then Present (Finalization_Master (PtrT))
1353 then
1354 Insert_Action (N,
1355 Make_Attach_Call
1356 (Obj_Ref => New_Occurrence_Of (Temp, Loc),
1357 Ptr_Typ => PtrT));
1358 end if;
1360 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1361 Analyze_And_Resolve (N, PtrT);
1363 elsif Is_Access_Type (T) and then Can_Never_Be_Null (T) then
1364 Install_Null_Excluding_Check (Exp);
1366 elsif Is_Access_Type (DesigT)
1367 and then Nkind (Exp) = N_Allocator
1368 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1369 then
1370 -- Apply constraint to designated subtype indication
1372 Apply_Constraint_Check
1373 (Expression (Exp), Designated_Type (DesigT), No_Sliding => True);
1375 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1377 -- Propagate constraint_error to enclosing allocator
1379 Rewrite (Exp, New_Copy (Expression (Exp)));
1380 end if;
1382 else
1383 Build_Allocate_Deallocate_Proc (N, True);
1385 -- If we have:
1386 -- type A is access T1;
1387 -- X : A := new T2'(...);
1388 -- T1 and T2 can be different subtypes, and we might need to check
1389 -- both constraints. First check against the type of the qualified
1390 -- expression.
1392 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1394 if Do_Range_Check (Exp) then
1395 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1396 end if;
1398 -- A check is also needed in cases where the designated subtype is
1399 -- constrained and differs from the subtype given in the qualified
1400 -- expression. Note that the check on the qualified expression does
1401 -- not allow sliding, but this check does (a relaxation from Ada 83).
1403 if Is_Constrained (DesigT)
1404 and then not Subtypes_Statically_Match (T, DesigT)
1405 then
1406 Apply_Constraint_Check
1407 (Exp, DesigT, No_Sliding => False);
1409 if Do_Range_Check (Exp) then
1410 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1411 end if;
1412 end if;
1414 -- For an access to unconstrained packed array, GIGI needs to see an
1415 -- expression with a constrained subtype in order to compute the
1416 -- proper size for the allocator.
1418 if Is_Array_Type (T)
1419 and then not Is_Constrained (T)
1420 and then Is_Packed (T)
1421 then
1422 declare
1423 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1424 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1425 begin
1426 Insert_Action (Exp,
1427 Make_Subtype_Declaration (Loc,
1428 Defining_Identifier => ConstrT,
1429 Subtype_Indication =>
1430 Make_Subtype_From_Expr (Internal_Exp, T)));
1431 Freeze_Itype (ConstrT, Exp);
1432 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1433 end;
1434 end if;
1436 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1437 -- to a build-in-place function, then access to the allocated object
1438 -- must be passed to the function. Currently we limit such functions
1439 -- to those with constrained limited result subtypes, but eventually
1440 -- we plan to expand the allowed forms of functions that are treated
1441 -- as build-in-place.
1443 if Ada_Version >= Ada_2005
1444 and then Is_Build_In_Place_Function_Call (Exp)
1445 then
1446 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1447 end if;
1448 end if;
1450 exception
1451 when RE_Not_Available =>
1452 return;
1453 end Expand_Allocator_Expression;
1455 -----------------------------
1456 -- Expand_Array_Comparison --
1457 -----------------------------
1459 -- Expansion is only required in the case of array types. For the unpacked
1460 -- case, an appropriate runtime routine is called. For packed cases, and
1461 -- also in some other cases where a runtime routine cannot be called, the
1462 -- form of the expansion is:
1464 -- [body for greater_nn; boolean_expression]
1466 -- The body is built by Make_Array_Comparison_Op, and the form of the
1467 -- Boolean expression depends on the operator involved.
1469 procedure Expand_Array_Comparison (N : Node_Id) is
1470 Loc : constant Source_Ptr := Sloc (N);
1471 Op1 : Node_Id := Left_Opnd (N);
1472 Op2 : Node_Id := Right_Opnd (N);
1473 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1474 Ctyp : constant Entity_Id := Component_Type (Typ1);
1476 Expr : Node_Id;
1477 Func_Body : Node_Id;
1478 Func_Name : Entity_Id;
1480 Comp : RE_Id;
1482 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1483 -- True for byte addressable target
1485 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1486 -- Returns True if the length of the given operand is known to be less
1487 -- than 4. Returns False if this length is known to be four or greater
1488 -- or is not known at compile time.
1490 ------------------------
1491 -- Length_Less_Than_4 --
1492 ------------------------
1494 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1495 Otyp : constant Entity_Id := Etype (Opnd);
1497 begin
1498 if Ekind (Otyp) = E_String_Literal_Subtype then
1499 return String_Literal_Length (Otyp) < 4;
1501 else
1502 declare
1503 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1504 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1505 Hi : constant Node_Id := Type_High_Bound (Ityp);
1506 Lov : Uint;
1507 Hiv : Uint;
1509 begin
1510 if Compile_Time_Known_Value (Lo) then
1511 Lov := Expr_Value (Lo);
1512 else
1513 return False;
1514 end if;
1516 if Compile_Time_Known_Value (Hi) then
1517 Hiv := Expr_Value (Hi);
1518 else
1519 return False;
1520 end if;
1522 return Hiv < Lov + 3;
1523 end;
1524 end if;
1525 end Length_Less_Than_4;
1527 -- Start of processing for Expand_Array_Comparison
1529 begin
1530 -- Deal first with unpacked case, where we can call a runtime routine
1531 -- except that we avoid this for targets for which are not addressable
1532 -- by bytes, and for the JVM/CIL, since they do not support direct
1533 -- addressing of array components.
1535 if not Is_Bit_Packed_Array (Typ1)
1536 and then Byte_Addressable
1537 and then VM_Target = No_VM
1538 then
1539 -- The call we generate is:
1541 -- Compare_Array_xn[_Unaligned]
1542 -- (left'address, right'address, left'length, right'length) <op> 0
1544 -- x = U for unsigned, S for signed
1545 -- n = 8,16,32,64 for component size
1546 -- Add _Unaligned if length < 4 and component size is 8.
1547 -- <op> is the standard comparison operator
1549 if Component_Size (Typ1) = 8 then
1550 if Length_Less_Than_4 (Op1)
1551 or else
1552 Length_Less_Than_4 (Op2)
1553 then
1554 if Is_Unsigned_Type (Ctyp) then
1555 Comp := RE_Compare_Array_U8_Unaligned;
1556 else
1557 Comp := RE_Compare_Array_S8_Unaligned;
1558 end if;
1560 else
1561 if Is_Unsigned_Type (Ctyp) then
1562 Comp := RE_Compare_Array_U8;
1563 else
1564 Comp := RE_Compare_Array_S8;
1565 end if;
1566 end if;
1568 elsif Component_Size (Typ1) = 16 then
1569 if Is_Unsigned_Type (Ctyp) then
1570 Comp := RE_Compare_Array_U16;
1571 else
1572 Comp := RE_Compare_Array_S16;
1573 end if;
1575 elsif Component_Size (Typ1) = 32 then
1576 if Is_Unsigned_Type (Ctyp) then
1577 Comp := RE_Compare_Array_U32;
1578 else
1579 Comp := RE_Compare_Array_S32;
1580 end if;
1582 else pragma Assert (Component_Size (Typ1) = 64);
1583 if Is_Unsigned_Type (Ctyp) then
1584 Comp := RE_Compare_Array_U64;
1585 else
1586 Comp := RE_Compare_Array_S64;
1587 end if;
1588 end if;
1590 Remove_Side_Effects (Op1, Name_Req => True);
1591 Remove_Side_Effects (Op2, Name_Req => True);
1593 Rewrite (Op1,
1594 Make_Function_Call (Sloc (Op1),
1595 Name => New_Occurrence_Of (RTE (Comp), Loc),
1597 Parameter_Associations => New_List (
1598 Make_Attribute_Reference (Loc,
1599 Prefix => Relocate_Node (Op1),
1600 Attribute_Name => Name_Address),
1602 Make_Attribute_Reference (Loc,
1603 Prefix => Relocate_Node (Op2),
1604 Attribute_Name => Name_Address),
1606 Make_Attribute_Reference (Loc,
1607 Prefix => Relocate_Node (Op1),
1608 Attribute_Name => Name_Length),
1610 Make_Attribute_Reference (Loc,
1611 Prefix => Relocate_Node (Op2),
1612 Attribute_Name => Name_Length))));
1614 Rewrite (Op2,
1615 Make_Integer_Literal (Sloc (Op2),
1616 Intval => Uint_0));
1618 Analyze_And_Resolve (Op1, Standard_Integer);
1619 Analyze_And_Resolve (Op2, Standard_Integer);
1620 return;
1621 end if;
1623 -- Cases where we cannot make runtime call
1625 -- For (a <= b) we convert to not (a > b)
1627 if Chars (N) = Name_Op_Le then
1628 Rewrite (N,
1629 Make_Op_Not (Loc,
1630 Right_Opnd =>
1631 Make_Op_Gt (Loc,
1632 Left_Opnd => Op1,
1633 Right_Opnd => Op2)));
1634 Analyze_And_Resolve (N, Standard_Boolean);
1635 return;
1637 -- For < the Boolean expression is
1638 -- greater__nn (op2, op1)
1640 elsif Chars (N) = Name_Op_Lt then
1641 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1643 -- Switch operands
1645 Op1 := Right_Opnd (N);
1646 Op2 := Left_Opnd (N);
1648 -- For (a >= b) we convert to not (a < b)
1650 elsif Chars (N) = Name_Op_Ge then
1651 Rewrite (N,
1652 Make_Op_Not (Loc,
1653 Right_Opnd =>
1654 Make_Op_Lt (Loc,
1655 Left_Opnd => Op1,
1656 Right_Opnd => Op2)));
1657 Analyze_And_Resolve (N, Standard_Boolean);
1658 return;
1660 -- For > the Boolean expression is
1661 -- greater__nn (op1, op2)
1663 else
1664 pragma Assert (Chars (N) = Name_Op_Gt);
1665 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1666 end if;
1668 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1669 Expr :=
1670 Make_Function_Call (Loc,
1671 Name => New_Occurrence_Of (Func_Name, Loc),
1672 Parameter_Associations => New_List (Op1, Op2));
1674 Insert_Action (N, Func_Body);
1675 Rewrite (N, Expr);
1676 Analyze_And_Resolve (N, Standard_Boolean);
1678 exception
1679 when RE_Not_Available =>
1680 return;
1681 end Expand_Array_Comparison;
1683 ---------------------------
1684 -- Expand_Array_Equality --
1685 ---------------------------
1687 -- Expand an equality function for multi-dimensional arrays. Here is an
1688 -- example of such a function for Nb_Dimension = 2
1690 -- function Enn (A : atyp; B : btyp) return boolean is
1691 -- begin
1692 -- if (A'length (1) = 0 or else A'length (2) = 0)
1693 -- and then
1694 -- (B'length (1) = 0 or else B'length (2) = 0)
1695 -- then
1696 -- return True; -- RM 4.5.2(22)
1697 -- end if;
1699 -- if A'length (1) /= B'length (1)
1700 -- or else
1701 -- A'length (2) /= B'length (2)
1702 -- then
1703 -- return False; -- RM 4.5.2(23)
1704 -- end if;
1706 -- declare
1707 -- A1 : Index_T1 := A'first (1);
1708 -- B1 : Index_T1 := B'first (1);
1709 -- begin
1710 -- loop
1711 -- declare
1712 -- A2 : Index_T2 := A'first (2);
1713 -- B2 : Index_T2 := B'first (2);
1714 -- begin
1715 -- loop
1716 -- if A (A1, A2) /= B (B1, B2) then
1717 -- return False;
1718 -- end if;
1720 -- exit when A2 = A'last (2);
1721 -- A2 := Index_T2'succ (A2);
1722 -- B2 := Index_T2'succ (B2);
1723 -- end loop;
1724 -- end;
1726 -- exit when A1 = A'last (1);
1727 -- A1 := Index_T1'succ (A1);
1728 -- B1 := Index_T1'succ (B1);
1729 -- end loop;
1730 -- end;
1732 -- return true;
1733 -- end Enn;
1735 -- Note on the formal types used (atyp and btyp). If either of the arrays
1736 -- is of a private type, we use the underlying type, and do an unchecked
1737 -- conversion of the actual. If either of the arrays has a bound depending
1738 -- on a discriminant, then we use the base type since otherwise we have an
1739 -- escaped discriminant in the function.
1741 -- If both arrays are constrained and have the same bounds, we can generate
1742 -- a loop with an explicit iteration scheme using a 'Range attribute over
1743 -- the first array.
1745 function Expand_Array_Equality
1746 (Nod : Node_Id;
1747 Lhs : Node_Id;
1748 Rhs : Node_Id;
1749 Bodies : List_Id;
1750 Typ : Entity_Id) return Node_Id
1752 Loc : constant Source_Ptr := Sloc (Nod);
1753 Decls : constant List_Id := New_List;
1754 Index_List1 : constant List_Id := New_List;
1755 Index_List2 : constant List_Id := New_List;
1757 Actuals : List_Id;
1758 Formals : List_Id;
1759 Func_Name : Entity_Id;
1760 Func_Body : Node_Id;
1762 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1763 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1765 Ltyp : Entity_Id;
1766 Rtyp : Entity_Id;
1767 -- The parameter types to be used for the formals
1769 function Arr_Attr
1770 (Arr : Entity_Id;
1771 Nam : Name_Id;
1772 Num : Int) return Node_Id;
1773 -- This builds the attribute reference Arr'Nam (Expr)
1775 function Component_Equality (Typ : Entity_Id) return Node_Id;
1776 -- Create one statement to compare corresponding components, designated
1777 -- by a full set of indexes.
1779 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1780 -- Given one of the arguments, computes the appropriate type to be used
1781 -- for that argument in the corresponding function formal
1783 function Handle_One_Dimension
1784 (N : Int;
1785 Index : Node_Id) return Node_Id;
1786 -- This procedure returns the following code
1788 -- declare
1789 -- Bn : Index_T := B'First (N);
1790 -- begin
1791 -- loop
1792 -- xxx
1793 -- exit when An = A'Last (N);
1794 -- An := Index_T'Succ (An)
1795 -- Bn := Index_T'Succ (Bn)
1796 -- end loop;
1797 -- end;
1799 -- If both indexes are constrained and identical, the procedure
1800 -- returns a simpler loop:
1802 -- for An in A'Range (N) loop
1803 -- xxx
1804 -- end loop
1806 -- N is the dimension for which we are generating a loop. Index is the
1807 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1808 -- xxx statement is either the loop or declare for the next dimension
1809 -- or if this is the last dimension the comparison of corresponding
1810 -- components of the arrays.
1812 -- The actual way the code works is to return the comparison of
1813 -- corresponding components for the N+1 call. That's neater.
1815 function Test_Empty_Arrays return Node_Id;
1816 -- This function constructs the test for both arrays being empty
1817 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1818 -- and then
1819 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1821 function Test_Lengths_Correspond return Node_Id;
1822 -- This function constructs the test for arrays having different lengths
1823 -- in at least one index position, in which case the resulting code is:
1825 -- A'length (1) /= B'length (1)
1826 -- or else
1827 -- A'length (2) /= B'length (2)
1828 -- or else
1829 -- ...
1831 --------------
1832 -- Arr_Attr --
1833 --------------
1835 function Arr_Attr
1836 (Arr : Entity_Id;
1837 Nam : Name_Id;
1838 Num : Int) return Node_Id
1840 begin
1841 return
1842 Make_Attribute_Reference (Loc,
1843 Attribute_Name => Nam,
1844 Prefix => New_Occurrence_Of (Arr, Loc),
1845 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1846 end Arr_Attr;
1848 ------------------------
1849 -- Component_Equality --
1850 ------------------------
1852 function Component_Equality (Typ : Entity_Id) return Node_Id is
1853 Test : Node_Id;
1854 L, R : Node_Id;
1856 begin
1857 -- if a(i1...) /= b(j1...) then return false; end if;
1859 L :=
1860 Make_Indexed_Component (Loc,
1861 Prefix => Make_Identifier (Loc, Chars (A)),
1862 Expressions => Index_List1);
1864 R :=
1865 Make_Indexed_Component (Loc,
1866 Prefix => Make_Identifier (Loc, Chars (B)),
1867 Expressions => Index_List2);
1869 Test := Expand_Composite_Equality
1870 (Nod, Component_Type (Typ), L, R, Decls);
1872 -- If some (sub)component is an unchecked_union, the whole operation
1873 -- will raise program error.
1875 if Nkind (Test) = N_Raise_Program_Error then
1877 -- This node is going to be inserted at a location where a
1878 -- statement is expected: clear its Etype so analysis will set
1879 -- it to the expected Standard_Void_Type.
1881 Set_Etype (Test, Empty);
1882 return Test;
1884 else
1885 return
1886 Make_Implicit_If_Statement (Nod,
1887 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1888 Then_Statements => New_List (
1889 Make_Simple_Return_Statement (Loc,
1890 Expression => New_Occurrence_Of (Standard_False, Loc))));
1891 end if;
1892 end Component_Equality;
1894 ------------------
1895 -- Get_Arg_Type --
1896 ------------------
1898 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1899 T : Entity_Id;
1900 X : Node_Id;
1902 begin
1903 T := Etype (N);
1905 if No (T) then
1906 return Typ;
1908 else
1909 T := Underlying_Type (T);
1911 X := First_Index (T);
1912 while Present (X) loop
1913 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1914 or else
1915 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1916 then
1917 T := Base_Type (T);
1918 exit;
1919 end if;
1921 Next_Index (X);
1922 end loop;
1924 return T;
1925 end if;
1926 end Get_Arg_Type;
1928 --------------------------
1929 -- Handle_One_Dimension --
1930 ---------------------------
1932 function Handle_One_Dimension
1933 (N : Int;
1934 Index : Node_Id) return Node_Id
1936 Need_Separate_Indexes : constant Boolean :=
1937 Ltyp /= Rtyp or else not Is_Constrained (Ltyp);
1938 -- If the index types are identical, and we are working with
1939 -- constrained types, then we can use the same index for both
1940 -- of the arrays.
1942 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1944 Bn : Entity_Id;
1945 Index_T : Entity_Id;
1946 Stm_List : List_Id;
1947 Loop_Stm : Node_Id;
1949 begin
1950 if N > Number_Dimensions (Ltyp) then
1951 return Component_Equality (Ltyp);
1952 end if;
1954 -- Case where we generate a loop
1956 Index_T := Base_Type (Etype (Index));
1958 if Need_Separate_Indexes then
1959 Bn := Make_Temporary (Loc, 'B');
1960 else
1961 Bn := An;
1962 end if;
1964 Append (New_Occurrence_Of (An, Loc), Index_List1);
1965 Append (New_Occurrence_Of (Bn, Loc), Index_List2);
1967 Stm_List := New_List (
1968 Handle_One_Dimension (N + 1, Next_Index (Index)));
1970 if Need_Separate_Indexes then
1972 -- Generate guard for loop, followed by increments of indexes
1974 Append_To (Stm_List,
1975 Make_Exit_Statement (Loc,
1976 Condition =>
1977 Make_Op_Eq (Loc,
1978 Left_Opnd => New_Occurrence_Of (An, Loc),
1979 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1981 Append_To (Stm_List,
1982 Make_Assignment_Statement (Loc,
1983 Name => New_Occurrence_Of (An, Loc),
1984 Expression =>
1985 Make_Attribute_Reference (Loc,
1986 Prefix => New_Occurrence_Of (Index_T, Loc),
1987 Attribute_Name => Name_Succ,
1988 Expressions => New_List (
1989 New_Occurrence_Of (An, Loc)))));
1991 Append_To (Stm_List,
1992 Make_Assignment_Statement (Loc,
1993 Name => New_Occurrence_Of (Bn, Loc),
1994 Expression =>
1995 Make_Attribute_Reference (Loc,
1996 Prefix => New_Occurrence_Of (Index_T, Loc),
1997 Attribute_Name => Name_Succ,
1998 Expressions => New_List (
1999 New_Occurrence_Of (Bn, Loc)))));
2000 end if;
2002 -- If separate indexes, we need a declare block for An and Bn, and a
2003 -- loop without an iteration scheme.
2005 if Need_Separate_Indexes then
2006 Loop_Stm :=
2007 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
2009 return
2010 Make_Block_Statement (Loc,
2011 Declarations => New_List (
2012 Make_Object_Declaration (Loc,
2013 Defining_Identifier => An,
2014 Object_Definition => New_Occurrence_Of (Index_T, Loc),
2015 Expression => Arr_Attr (A, Name_First, N)),
2017 Make_Object_Declaration (Loc,
2018 Defining_Identifier => Bn,
2019 Object_Definition => New_Occurrence_Of (Index_T, Loc),
2020 Expression => Arr_Attr (B, Name_First, N))),
2022 Handled_Statement_Sequence =>
2023 Make_Handled_Sequence_Of_Statements (Loc,
2024 Statements => New_List (Loop_Stm)));
2026 -- If no separate indexes, return loop statement with explicit
2027 -- iteration scheme on its own
2029 else
2030 Loop_Stm :=
2031 Make_Implicit_Loop_Statement (Nod,
2032 Statements => Stm_List,
2033 Iteration_Scheme =>
2034 Make_Iteration_Scheme (Loc,
2035 Loop_Parameter_Specification =>
2036 Make_Loop_Parameter_Specification (Loc,
2037 Defining_Identifier => An,
2038 Discrete_Subtype_Definition =>
2039 Arr_Attr (A, Name_Range, N))));
2040 return Loop_Stm;
2041 end if;
2042 end Handle_One_Dimension;
2044 -----------------------
2045 -- Test_Empty_Arrays --
2046 -----------------------
2048 function Test_Empty_Arrays return Node_Id is
2049 Alist : Node_Id;
2050 Blist : Node_Id;
2052 Atest : Node_Id;
2053 Btest : Node_Id;
2055 begin
2056 Alist := Empty;
2057 Blist := Empty;
2058 for J in 1 .. Number_Dimensions (Ltyp) loop
2059 Atest :=
2060 Make_Op_Eq (Loc,
2061 Left_Opnd => Arr_Attr (A, Name_Length, J),
2062 Right_Opnd => Make_Integer_Literal (Loc, 0));
2064 Btest :=
2065 Make_Op_Eq (Loc,
2066 Left_Opnd => Arr_Attr (B, Name_Length, J),
2067 Right_Opnd => Make_Integer_Literal (Loc, 0));
2069 if No (Alist) then
2070 Alist := Atest;
2071 Blist := Btest;
2073 else
2074 Alist :=
2075 Make_Or_Else (Loc,
2076 Left_Opnd => Relocate_Node (Alist),
2077 Right_Opnd => Atest);
2079 Blist :=
2080 Make_Or_Else (Loc,
2081 Left_Opnd => Relocate_Node (Blist),
2082 Right_Opnd => Btest);
2083 end if;
2084 end loop;
2086 return
2087 Make_And_Then (Loc,
2088 Left_Opnd => Alist,
2089 Right_Opnd => Blist);
2090 end Test_Empty_Arrays;
2092 -----------------------------
2093 -- Test_Lengths_Correspond --
2094 -----------------------------
2096 function Test_Lengths_Correspond return Node_Id is
2097 Result : Node_Id;
2098 Rtest : Node_Id;
2100 begin
2101 Result := Empty;
2102 for J in 1 .. Number_Dimensions (Ltyp) loop
2103 Rtest :=
2104 Make_Op_Ne (Loc,
2105 Left_Opnd => Arr_Attr (A, Name_Length, J),
2106 Right_Opnd => Arr_Attr (B, Name_Length, J));
2108 if No (Result) then
2109 Result := Rtest;
2110 else
2111 Result :=
2112 Make_Or_Else (Loc,
2113 Left_Opnd => Relocate_Node (Result),
2114 Right_Opnd => Rtest);
2115 end if;
2116 end loop;
2118 return Result;
2119 end Test_Lengths_Correspond;
2121 -- Start of processing for Expand_Array_Equality
2123 begin
2124 Ltyp := Get_Arg_Type (Lhs);
2125 Rtyp := Get_Arg_Type (Rhs);
2127 -- For now, if the argument types are not the same, go to the base type,
2128 -- since the code assumes that the formals have the same type. This is
2129 -- fixable in future ???
2131 if Ltyp /= Rtyp then
2132 Ltyp := Base_Type (Ltyp);
2133 Rtyp := Base_Type (Rtyp);
2134 pragma Assert (Ltyp = Rtyp);
2135 end if;
2137 -- Build list of formals for function
2139 Formals := New_List (
2140 Make_Parameter_Specification (Loc,
2141 Defining_Identifier => A,
2142 Parameter_Type => New_Occurrence_Of (Ltyp, Loc)),
2144 Make_Parameter_Specification (Loc,
2145 Defining_Identifier => B,
2146 Parameter_Type => New_Occurrence_Of (Rtyp, Loc)));
2148 Func_Name := Make_Temporary (Loc, 'E');
2150 -- Build statement sequence for function
2152 Func_Body :=
2153 Make_Subprogram_Body (Loc,
2154 Specification =>
2155 Make_Function_Specification (Loc,
2156 Defining_Unit_Name => Func_Name,
2157 Parameter_Specifications => Formals,
2158 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
2160 Declarations => Decls,
2162 Handled_Statement_Sequence =>
2163 Make_Handled_Sequence_Of_Statements (Loc,
2164 Statements => New_List (
2166 Make_Implicit_If_Statement (Nod,
2167 Condition => Test_Empty_Arrays,
2168 Then_Statements => New_List (
2169 Make_Simple_Return_Statement (Loc,
2170 Expression =>
2171 New_Occurrence_Of (Standard_True, Loc)))),
2173 Make_Implicit_If_Statement (Nod,
2174 Condition => Test_Lengths_Correspond,
2175 Then_Statements => New_List (
2176 Make_Simple_Return_Statement (Loc,
2177 Expression => New_Occurrence_Of (Standard_False, Loc)))),
2179 Handle_One_Dimension (1, First_Index (Ltyp)),
2181 Make_Simple_Return_Statement (Loc,
2182 Expression => New_Occurrence_Of (Standard_True, Loc)))));
2184 Set_Has_Completion (Func_Name, True);
2185 Set_Is_Inlined (Func_Name);
2187 -- If the array type is distinct from the type of the arguments, it
2188 -- is the full view of a private type. Apply an unchecked conversion
2189 -- to insure that analysis of the call succeeds.
2191 declare
2192 L, R : Node_Id;
2194 begin
2195 L := Lhs;
2196 R := Rhs;
2198 if No (Etype (Lhs))
2199 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
2200 then
2201 L := OK_Convert_To (Ltyp, Lhs);
2202 end if;
2204 if No (Etype (Rhs))
2205 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
2206 then
2207 R := OK_Convert_To (Rtyp, Rhs);
2208 end if;
2210 Actuals := New_List (L, R);
2211 end;
2213 Append_To (Bodies, Func_Body);
2215 return
2216 Make_Function_Call (Loc,
2217 Name => New_Occurrence_Of (Func_Name, Loc),
2218 Parameter_Associations => Actuals);
2219 end Expand_Array_Equality;
2221 -----------------------------
2222 -- Expand_Boolean_Operator --
2223 -----------------------------
2225 -- Note that we first get the actual subtypes of the operands, since we
2226 -- always want to deal with types that have bounds.
2228 procedure Expand_Boolean_Operator (N : Node_Id) is
2229 Typ : constant Entity_Id := Etype (N);
2231 begin
2232 -- Special case of bit packed array where both operands are known to be
2233 -- properly aligned. In this case we use an efficient run time routine
2234 -- to carry out the operation (see System.Bit_Ops).
2236 if Is_Bit_Packed_Array (Typ)
2237 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
2238 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
2239 then
2240 Expand_Packed_Boolean_Operator (N);
2241 return;
2242 end if;
2244 -- For the normal non-packed case, the general expansion is to build
2245 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2246 -- and then inserting it into the tree. The original operator node is
2247 -- then rewritten as a call to this function. We also use this in the
2248 -- packed case if either operand is a possibly unaligned object.
2250 declare
2251 Loc : constant Source_Ptr := Sloc (N);
2252 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2253 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2254 Func_Body : Node_Id;
2255 Func_Name : Entity_Id;
2257 begin
2258 Convert_To_Actual_Subtype (L);
2259 Convert_To_Actual_Subtype (R);
2260 Ensure_Defined (Etype (L), N);
2261 Ensure_Defined (Etype (R), N);
2262 Apply_Length_Check (R, Etype (L));
2264 if Nkind (N) = N_Op_Xor then
2265 Silly_Boolean_Array_Xor_Test (N, Etype (L));
2266 end if;
2268 if Nkind (Parent (N)) = N_Assignment_Statement
2269 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
2270 then
2271 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
2273 elsif Nkind (Parent (N)) = N_Op_Not
2274 and then Nkind (N) = N_Op_And
2275 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
2276 and then Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
2277 then
2278 return;
2279 else
2281 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
2282 Func_Name := Defining_Unit_Name (Specification (Func_Body));
2283 Insert_Action (N, Func_Body);
2285 -- Now rewrite the expression with a call
2287 Rewrite (N,
2288 Make_Function_Call (Loc,
2289 Name => New_Occurrence_Of (Func_Name, Loc),
2290 Parameter_Associations =>
2291 New_List (
2293 Make_Type_Conversion
2294 (Loc, New_Occurrence_Of (Etype (L), Loc), R))));
2296 Analyze_And_Resolve (N, Typ);
2297 end if;
2298 end;
2299 end Expand_Boolean_Operator;
2301 ------------------------------------------------
2302 -- Expand_Compare_Minimize_Eliminate_Overflow --
2303 ------------------------------------------------
2305 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is
2306 Loc : constant Source_Ptr := Sloc (N);
2308 Result_Type : constant Entity_Id := Etype (N);
2309 -- Capture result type (could be a derived boolean type)
2311 Llo, Lhi : Uint;
2312 Rlo, Rhi : Uint;
2314 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
2315 -- Entity for Long_Long_Integer'Base
2317 Check : constant Overflow_Mode_Type := Overflow_Check_Mode;
2318 -- Current overflow checking mode
2320 procedure Set_True;
2321 procedure Set_False;
2322 -- These procedures rewrite N with an occurrence of Standard_True or
2323 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2325 ---------------
2326 -- Set_False --
2327 ---------------
2329 procedure Set_False is
2330 begin
2331 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2332 Warn_On_Known_Condition (N);
2333 end Set_False;
2335 --------------
2336 -- Set_True --
2337 --------------
2339 procedure Set_True is
2340 begin
2341 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
2342 Warn_On_Known_Condition (N);
2343 end Set_True;
2345 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2347 begin
2348 -- Nothing to do unless we have a comparison operator with operands
2349 -- that are signed integer types, and we are operating in either
2350 -- MINIMIZED or ELIMINATED overflow checking mode.
2352 if Nkind (N) not in N_Op_Compare
2353 or else Check not in Minimized_Or_Eliminated
2354 or else not Is_Signed_Integer_Type (Etype (Left_Opnd (N)))
2355 then
2356 return;
2357 end if;
2359 -- OK, this is the case we are interested in. First step is to process
2360 -- our operands using the Minimize_Eliminate circuitry which applies
2361 -- this processing to the two operand subtrees.
2363 Minimize_Eliminate_Overflows
2364 (Left_Opnd (N), Llo, Lhi, Top_Level => False);
2365 Minimize_Eliminate_Overflows
2366 (Right_Opnd (N), Rlo, Rhi, Top_Level => False);
2368 -- See if the range information decides the result of the comparison.
2369 -- We can only do this if we in fact have full range information (which
2370 -- won't be the case if either operand is bignum at this stage).
2372 if Llo /= No_Uint and then Rlo /= No_Uint then
2373 case N_Op_Compare (Nkind (N)) is
2374 when N_Op_Eq =>
2375 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2376 Set_True;
2377 elsif Llo > Rhi or else Lhi < Rlo then
2378 Set_False;
2379 end if;
2381 when N_Op_Ge =>
2382 if Llo >= Rhi then
2383 Set_True;
2384 elsif Lhi < Rlo then
2385 Set_False;
2386 end if;
2388 when N_Op_Gt =>
2389 if Llo > Rhi then
2390 Set_True;
2391 elsif Lhi <= Rlo then
2392 Set_False;
2393 end if;
2395 when N_Op_Le =>
2396 if Llo > Rhi then
2397 Set_False;
2398 elsif Lhi <= Rlo then
2399 Set_True;
2400 end if;
2402 when N_Op_Lt =>
2403 if Llo >= Rhi then
2404 Set_False;
2405 elsif Lhi < Rlo then
2406 Set_True;
2407 end if;
2409 when N_Op_Ne =>
2410 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2411 Set_False;
2412 elsif Llo > Rhi or else Lhi < Rlo then
2413 Set_True;
2414 end if;
2415 end case;
2417 -- All done if we did the rewrite
2419 if Nkind (N) not in N_Op_Compare then
2420 return;
2421 end if;
2422 end if;
2424 -- Otherwise, time to do the comparison
2426 declare
2427 Ltype : constant Entity_Id := Etype (Left_Opnd (N));
2428 Rtype : constant Entity_Id := Etype (Right_Opnd (N));
2430 begin
2431 -- If the two operands have the same signed integer type we are
2432 -- all set, nothing more to do. This is the case where either
2433 -- both operands were unchanged, or we rewrote both of them to
2434 -- be Long_Long_Integer.
2436 -- Note: Entity for the comparison may be wrong, but it's not worth
2437 -- the effort to change it, since the back end does not use it.
2439 if Is_Signed_Integer_Type (Ltype)
2440 and then Base_Type (Ltype) = Base_Type (Rtype)
2441 then
2442 return;
2444 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2446 elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then
2447 declare
2448 Left : Node_Id := Left_Opnd (N);
2449 Right : Node_Id := Right_Opnd (N);
2450 -- Bignum references for left and right operands
2452 begin
2453 if not Is_RTE (Ltype, RE_Bignum) then
2454 Left := Convert_To_Bignum (Left);
2455 elsif not Is_RTE (Rtype, RE_Bignum) then
2456 Right := Convert_To_Bignum (Right);
2457 end if;
2459 -- We rewrite our node with:
2461 -- do
2462 -- Bnn : Result_Type;
2463 -- declare
2464 -- M : Mark_Id := SS_Mark;
2465 -- begin
2466 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2467 -- SS_Release (M);
2468 -- end;
2469 -- in
2470 -- Bnn
2471 -- end
2473 declare
2474 Blk : constant Node_Id := Make_Bignum_Block (Loc);
2475 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
2476 Ent : RE_Id;
2478 begin
2479 case N_Op_Compare (Nkind (N)) is
2480 when N_Op_Eq => Ent := RE_Big_EQ;
2481 when N_Op_Ge => Ent := RE_Big_GE;
2482 when N_Op_Gt => Ent := RE_Big_GT;
2483 when N_Op_Le => Ent := RE_Big_LE;
2484 when N_Op_Lt => Ent := RE_Big_LT;
2485 when N_Op_Ne => Ent := RE_Big_NE;
2486 end case;
2488 -- Insert assignment to Bnn into the bignum block
2490 Insert_Before
2491 (First (Statements (Handled_Statement_Sequence (Blk))),
2492 Make_Assignment_Statement (Loc,
2493 Name => New_Occurrence_Of (Bnn, Loc),
2494 Expression =>
2495 Make_Function_Call (Loc,
2496 Name =>
2497 New_Occurrence_Of (RTE (Ent), Loc),
2498 Parameter_Associations => New_List (Left, Right))));
2500 -- Now do the rewrite with expression actions
2502 Rewrite (N,
2503 Make_Expression_With_Actions (Loc,
2504 Actions => New_List (
2505 Make_Object_Declaration (Loc,
2506 Defining_Identifier => Bnn,
2507 Object_Definition =>
2508 New_Occurrence_Of (Result_Type, Loc)),
2509 Blk),
2510 Expression => New_Occurrence_Of (Bnn, Loc)));
2511 Analyze_And_Resolve (N, Result_Type);
2512 end;
2513 end;
2515 -- No bignums involved, but types are different, so we must have
2516 -- rewritten one of the operands as a Long_Long_Integer but not
2517 -- the other one.
2519 -- If left operand is Long_Long_Integer, convert right operand
2520 -- and we are done (with a comparison of two Long_Long_Integers).
2522 elsif Ltype = LLIB then
2523 Convert_To_And_Rewrite (LLIB, Right_Opnd (N));
2524 Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks);
2525 return;
2527 -- If right operand is Long_Long_Integer, convert left operand
2528 -- and we are done (with a comparison of two Long_Long_Integers).
2530 -- This is the only remaining possibility
2532 else pragma Assert (Rtype = LLIB);
2533 Convert_To_And_Rewrite (LLIB, Left_Opnd (N));
2534 Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks);
2535 return;
2536 end if;
2537 end;
2538 end Expand_Compare_Minimize_Eliminate_Overflow;
2540 -------------------------------
2541 -- Expand_Composite_Equality --
2542 -------------------------------
2544 -- This function is only called for comparing internal fields of composite
2545 -- types when these fields are themselves composites. This is a special
2546 -- case because it is not possible to respect normal Ada visibility rules.
2548 function Expand_Composite_Equality
2549 (Nod : Node_Id;
2550 Typ : Entity_Id;
2551 Lhs : Node_Id;
2552 Rhs : Node_Id;
2553 Bodies : List_Id) return Node_Id
2555 Loc : constant Source_Ptr := Sloc (Nod);
2556 Full_Type : Entity_Id;
2557 Prim : Elmt_Id;
2558 Eq_Op : Entity_Id;
2560 function Find_Primitive_Eq return Node_Id;
2561 -- AI05-0123: Locate primitive equality for type if it exists, and
2562 -- build the corresponding call. If operation is abstract, replace
2563 -- call with an explicit raise. Return Empty if there is no primitive.
2565 -----------------------
2566 -- Find_Primitive_Eq --
2567 -----------------------
2569 function Find_Primitive_Eq return Node_Id is
2570 Prim_E : Elmt_Id;
2571 Prim : Node_Id;
2573 begin
2574 Prim_E := First_Elmt (Collect_Primitive_Operations (Typ));
2575 while Present (Prim_E) loop
2576 Prim := Node (Prim_E);
2578 -- Locate primitive equality with the right signature
2580 if Chars (Prim) = Name_Op_Eq
2581 and then Etype (First_Formal (Prim)) =
2582 Etype (Next_Formal (First_Formal (Prim)))
2583 and then Etype (Prim) = Standard_Boolean
2584 then
2585 if Is_Abstract_Subprogram (Prim) then
2586 return
2587 Make_Raise_Program_Error (Loc,
2588 Reason => PE_Explicit_Raise);
2590 else
2591 return
2592 Make_Function_Call (Loc,
2593 Name => New_Occurrence_Of (Prim, Loc),
2594 Parameter_Associations => New_List (Lhs, Rhs));
2595 end if;
2596 end if;
2598 Next_Elmt (Prim_E);
2599 end loop;
2601 -- If not found, predefined operation will be used
2603 return Empty;
2604 end Find_Primitive_Eq;
2606 -- Start of processing for Expand_Composite_Equality
2608 begin
2609 if Is_Private_Type (Typ) then
2610 Full_Type := Underlying_Type (Typ);
2611 else
2612 Full_Type := Typ;
2613 end if;
2615 -- If the private type has no completion the context may be the
2616 -- expansion of a composite equality for a composite type with some
2617 -- still incomplete components. The expression will not be analyzed
2618 -- until the enclosing type is completed, at which point this will be
2619 -- properly expanded, unless there is a bona fide completion error.
2621 if No (Full_Type) then
2622 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2623 end if;
2625 Full_Type := Base_Type (Full_Type);
2627 -- When the base type itself is private, use the full view to expand
2628 -- the composite equality.
2630 if Is_Private_Type (Full_Type) then
2631 Full_Type := Underlying_Type (Full_Type);
2632 end if;
2634 -- Case of array types
2636 if Is_Array_Type (Full_Type) then
2638 -- If the operand is an elementary type other than a floating-point
2639 -- type, then we can simply use the built-in block bitwise equality,
2640 -- since the predefined equality operators always apply and bitwise
2641 -- equality is fine for all these cases.
2643 if Is_Elementary_Type (Component_Type (Full_Type))
2644 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2645 then
2646 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2648 -- For composite component types, and floating-point types, use the
2649 -- expansion. This deals with tagged component types (where we use
2650 -- the applicable equality routine) and floating-point, (where we
2651 -- need to worry about negative zeroes), and also the case of any
2652 -- composite type recursively containing such fields.
2654 else
2655 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2656 end if;
2658 -- Case of tagged record types
2660 elsif Is_Tagged_Type (Full_Type) then
2662 -- Call the primitive operation "=" of this type
2664 if Is_Class_Wide_Type (Full_Type) then
2665 Full_Type := Root_Type (Full_Type);
2666 end if;
2668 -- If this is derived from an untagged private type completed with a
2669 -- tagged type, it does not have a full view, so we use the primitive
2670 -- operations of the private type. This check should no longer be
2671 -- necessary when these types receive their full views ???
2673 if Is_Private_Type (Typ)
2674 and then not Is_Tagged_Type (Typ)
2675 and then not Is_Controlled (Typ)
2676 and then Is_Derived_Type (Typ)
2677 and then No (Full_View (Typ))
2678 then
2679 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2680 else
2681 Prim := First_Elmt (Primitive_Operations (Full_Type));
2682 end if;
2684 loop
2685 Eq_Op := Node (Prim);
2686 exit when Chars (Eq_Op) = Name_Op_Eq
2687 and then Etype (First_Formal (Eq_Op)) =
2688 Etype (Next_Formal (First_Formal (Eq_Op)))
2689 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2690 Next_Elmt (Prim);
2691 pragma Assert (Present (Prim));
2692 end loop;
2694 Eq_Op := Node (Prim);
2696 return
2697 Make_Function_Call (Loc,
2698 Name => New_Occurrence_Of (Eq_Op, Loc),
2699 Parameter_Associations =>
2700 New_List
2701 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2702 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2704 -- Case of untagged record types
2706 elsif Is_Record_Type (Full_Type) then
2707 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2709 if Present (Eq_Op) then
2710 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2712 -- Inherited equality from parent type. Convert the actuals to
2713 -- match signature of operation.
2715 declare
2716 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2718 begin
2719 return
2720 Make_Function_Call (Loc,
2721 Name => New_Occurrence_Of (Eq_Op, Loc),
2722 Parameter_Associations => New_List (
2723 OK_Convert_To (T, Lhs),
2724 OK_Convert_To (T, Rhs)));
2725 end;
2727 else
2728 -- Comparison between Unchecked_Union components
2730 if Is_Unchecked_Union (Full_Type) then
2731 declare
2732 Lhs_Type : Node_Id := Full_Type;
2733 Rhs_Type : Node_Id := Full_Type;
2734 Lhs_Discr_Val : Node_Id;
2735 Rhs_Discr_Val : Node_Id;
2737 begin
2738 -- Lhs subtype
2740 if Nkind (Lhs) = N_Selected_Component then
2741 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2742 end if;
2744 -- Rhs subtype
2746 if Nkind (Rhs) = N_Selected_Component then
2747 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2748 end if;
2750 -- Lhs of the composite equality
2752 if Is_Constrained (Lhs_Type) then
2754 -- Since the enclosing record type can never be an
2755 -- Unchecked_Union (this code is executed for records
2756 -- that do not have variants), we may reference its
2757 -- discriminant(s).
2759 if Nkind (Lhs) = N_Selected_Component
2760 and then Has_Per_Object_Constraint
2761 (Entity (Selector_Name (Lhs)))
2762 then
2763 Lhs_Discr_Val :=
2764 Make_Selected_Component (Loc,
2765 Prefix => Prefix (Lhs),
2766 Selector_Name =>
2767 New_Copy
2768 (Get_Discriminant_Value
2769 (First_Discriminant (Lhs_Type),
2770 Lhs_Type,
2771 Stored_Constraint (Lhs_Type))));
2773 else
2774 Lhs_Discr_Val :=
2775 New_Copy
2776 (Get_Discriminant_Value
2777 (First_Discriminant (Lhs_Type),
2778 Lhs_Type,
2779 Stored_Constraint (Lhs_Type)));
2781 end if;
2782 else
2783 -- It is not possible to infer the discriminant since
2784 -- the subtype is not constrained.
2786 return
2787 Make_Raise_Program_Error (Loc,
2788 Reason => PE_Unchecked_Union_Restriction);
2789 end if;
2791 -- Rhs of the composite equality
2793 if Is_Constrained (Rhs_Type) then
2794 if Nkind (Rhs) = N_Selected_Component
2795 and then Has_Per_Object_Constraint
2796 (Entity (Selector_Name (Rhs)))
2797 then
2798 Rhs_Discr_Val :=
2799 Make_Selected_Component (Loc,
2800 Prefix => Prefix (Rhs),
2801 Selector_Name =>
2802 New_Copy
2803 (Get_Discriminant_Value
2804 (First_Discriminant (Rhs_Type),
2805 Rhs_Type,
2806 Stored_Constraint (Rhs_Type))));
2808 else
2809 Rhs_Discr_Val :=
2810 New_Copy
2811 (Get_Discriminant_Value
2812 (First_Discriminant (Rhs_Type),
2813 Rhs_Type,
2814 Stored_Constraint (Rhs_Type)));
2816 end if;
2817 else
2818 return
2819 Make_Raise_Program_Error (Loc,
2820 Reason => PE_Unchecked_Union_Restriction);
2821 end if;
2823 -- Call the TSS equality function with the inferred
2824 -- discriminant values.
2826 return
2827 Make_Function_Call (Loc,
2828 Name => New_Occurrence_Of (Eq_Op, Loc),
2829 Parameter_Associations => New_List (
2830 Lhs,
2831 Rhs,
2832 Lhs_Discr_Val,
2833 Rhs_Discr_Val));
2834 end;
2836 -- All cases other than comparing Unchecked_Union types
2838 else
2839 declare
2840 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2841 begin
2842 return
2843 Make_Function_Call (Loc,
2844 Name =>
2845 New_Occurrence_Of (Eq_Op, Loc),
2846 Parameter_Associations => New_List (
2847 OK_Convert_To (T, Lhs),
2848 OK_Convert_To (T, Rhs)));
2849 end;
2850 end if;
2851 end if;
2853 -- Equality composes in Ada 2012 for untagged record types. It also
2854 -- composes for bounded strings, because they are part of the
2855 -- predefined environment. We could make it compose for bounded
2856 -- strings by making them tagged, or by making sure all subcomponents
2857 -- are set to the same value, even when not used. Instead, we have
2858 -- this special case in the compiler, because it's more efficient.
2860 elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then
2862 -- If no TSS has been created for the type, check whether there is
2863 -- a primitive equality declared for it.
2865 declare
2866 Op : constant Node_Id := Find_Primitive_Eq;
2868 begin
2869 -- Use user-defined primitive if it exists, otherwise use
2870 -- predefined equality.
2872 if Present (Op) then
2873 return Op;
2874 else
2875 return Make_Op_Eq (Loc, Lhs, Rhs);
2876 end if;
2877 end;
2879 else
2880 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2881 end if;
2883 -- Non-composite types (always use predefined equality)
2885 else
2886 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2887 end if;
2888 end Expand_Composite_Equality;
2890 ------------------------
2891 -- Expand_Concatenate --
2892 ------------------------
2894 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2895 Loc : constant Source_Ptr := Sloc (Cnode);
2897 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2898 -- Result type of concatenation
2900 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2901 -- Component type. Elements of this component type can appear as one
2902 -- of the operands of concatenation as well as arrays.
2904 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2905 -- Index subtype
2907 Ityp : constant Entity_Id := Base_Type (Istyp);
2908 -- Index type. This is the base type of the index subtype, and is used
2909 -- for all computed bounds (which may be out of range of Istyp in the
2910 -- case of null ranges).
2912 Artyp : Entity_Id;
2913 -- This is the type we use to do arithmetic to compute the bounds and
2914 -- lengths of operands. The choice of this type is a little subtle and
2915 -- is discussed in a separate section at the start of the body code.
2917 Concatenation_Error : exception;
2918 -- Raised if concatenation is sure to raise a CE
2920 Result_May_Be_Null : Boolean := True;
2921 -- Reset to False if at least one operand is encountered which is known
2922 -- at compile time to be non-null. Used for handling the special case
2923 -- of setting the high bound to the last operand high bound for a null
2924 -- result, thus ensuring a proper high bound in the super-flat case.
2926 N : constant Nat := List_Length (Opnds);
2927 -- Number of concatenation operands including possibly null operands
2929 NN : Nat := 0;
2930 -- Number of operands excluding any known to be null, except that the
2931 -- last operand is always retained, in case it provides the bounds for
2932 -- a null result.
2934 Opnd : Node_Id;
2935 -- Current operand being processed in the loop through operands. After
2936 -- this loop is complete, always contains the last operand (which is not
2937 -- the same as Operands (NN), since null operands are skipped).
2939 -- Arrays describing the operands, only the first NN entries of each
2940 -- array are set (NN < N when we exclude known null operands).
2942 Is_Fixed_Length : array (1 .. N) of Boolean;
2943 -- True if length of corresponding operand known at compile time
2945 Operands : array (1 .. N) of Node_Id;
2946 -- Set to the corresponding entry in the Opnds list (but note that null
2947 -- operands are excluded, so not all entries in the list are stored).
2949 Fixed_Length : array (1 .. N) of Uint;
2950 -- Set to length of operand. Entries in this array are set only if the
2951 -- corresponding entry in Is_Fixed_Length is True.
2953 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2954 -- Set to lower bound of operand. Either an integer literal in the case
2955 -- where the bound is known at compile time, else actual lower bound.
2956 -- The operand low bound is of type Ityp.
2958 Var_Length : array (1 .. N) of Entity_Id;
2959 -- Set to an entity of type Natural that contains the length of an
2960 -- operand whose length is not known at compile time. Entries in this
2961 -- array are set only if the corresponding entry in Is_Fixed_Length
2962 -- is False. The entity is of type Artyp.
2964 Aggr_Length : array (0 .. N) of Node_Id;
2965 -- The J'th entry in an expression node that represents the total length
2966 -- of operands 1 through J. It is either an integer literal node, or a
2967 -- reference to a constant entity with the right value, so it is fine
2968 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2969 -- entry always is set to zero. The length is of type Artyp.
2971 Low_Bound : Node_Id;
2972 -- A tree node representing the low bound of the result (of type Ityp).
2973 -- This is either an integer literal node, or an identifier reference to
2974 -- a constant entity initialized to the appropriate value.
2976 Last_Opnd_Low_Bound : Node_Id;
2977 -- A tree node representing the low bound of the last operand. This
2978 -- need only be set if the result could be null. It is used for the
2979 -- special case of setting the right low bound for a null result.
2980 -- This is of type Ityp.
2982 Last_Opnd_High_Bound : Node_Id;
2983 -- A tree node representing the high bound of the last operand. This
2984 -- need only be set if the result could be null. It is used for the
2985 -- special case of setting the right high bound for a null result.
2986 -- This is of type Ityp.
2988 High_Bound : Node_Id;
2989 -- A tree node representing the high bound of the result (of type Ityp)
2991 Result : Node_Id;
2992 -- Result of the concatenation (of type Ityp)
2994 Actions : constant List_Id := New_List;
2995 -- Collect actions to be inserted
2997 Known_Non_Null_Operand_Seen : Boolean;
2998 -- Set True during generation of the assignments of operands into
2999 -- result once an operand known to be non-null has been seen.
3001 function Make_Artyp_Literal (Val : Nat) return Node_Id;
3002 -- This function makes an N_Integer_Literal node that is returned in
3003 -- analyzed form with the type set to Artyp. Importantly this literal
3004 -- is not flagged as static, so that if we do computations with it that
3005 -- result in statically detected out of range conditions, we will not
3006 -- generate error messages but instead warning messages.
3008 function To_Artyp (X : Node_Id) return Node_Id;
3009 -- Given a node of type Ityp, returns the corresponding value of type
3010 -- Artyp. For non-enumeration types, this is a plain integer conversion.
3011 -- For enum types, the Pos of the value is returned.
3013 function To_Ityp (X : Node_Id) return Node_Id;
3014 -- The inverse function (uses Val in the case of enumeration types)
3016 ------------------------
3017 -- Make_Artyp_Literal --
3018 ------------------------
3020 function Make_Artyp_Literal (Val : Nat) return Node_Id is
3021 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
3022 begin
3023 Set_Etype (Result, Artyp);
3024 Set_Analyzed (Result, True);
3025 Set_Is_Static_Expression (Result, False);
3026 return Result;
3027 end Make_Artyp_Literal;
3029 --------------
3030 -- To_Artyp --
3031 --------------
3033 function To_Artyp (X : Node_Id) return Node_Id is
3034 begin
3035 if Ityp = Base_Type (Artyp) then
3036 return X;
3038 elsif Is_Enumeration_Type (Ityp) then
3039 return
3040 Make_Attribute_Reference (Loc,
3041 Prefix => New_Occurrence_Of (Ityp, Loc),
3042 Attribute_Name => Name_Pos,
3043 Expressions => New_List (X));
3045 else
3046 return Convert_To (Artyp, X);
3047 end if;
3048 end To_Artyp;
3050 -------------
3051 -- To_Ityp --
3052 -------------
3054 function To_Ityp (X : Node_Id) return Node_Id is
3055 begin
3056 if Is_Enumeration_Type (Ityp) then
3057 return
3058 Make_Attribute_Reference (Loc,
3059 Prefix => New_Occurrence_Of (Ityp, Loc),
3060 Attribute_Name => Name_Val,
3061 Expressions => New_List (X));
3063 -- Case where we will do a type conversion
3065 else
3066 if Ityp = Base_Type (Artyp) then
3067 return X;
3068 else
3069 return Convert_To (Ityp, X);
3070 end if;
3071 end if;
3072 end To_Ityp;
3074 -- Local Declarations
3076 Lib_Level_Target : constant Boolean :=
3077 Nkind (Parent (Cnode)) = N_Object_Declaration
3078 and then
3079 Is_Library_Level_Entity (Defining_Identifier (Parent (Cnode)));
3081 -- If the concatenation declares a library level entity, we call the
3082 -- built-in concatenation routines to prevent code bloat, regardless
3083 -- of optimization level. This is space-efficient, and prevent linking
3084 -- problems when units are compiled with different optimizations.
3086 Opnd_Typ : Entity_Id;
3087 Ent : Entity_Id;
3088 Len : Uint;
3089 J : Nat;
3090 Clen : Node_Id;
3091 Set : Boolean;
3093 -- Start of processing for Expand_Concatenate
3095 begin
3096 -- Choose an appropriate computational type
3098 -- We will be doing calculations of lengths and bounds in this routine
3099 -- and computing one from the other in some cases, e.g. getting the high
3100 -- bound by adding the length-1 to the low bound.
3102 -- We can't just use the index type, or even its base type for this
3103 -- purpose for two reasons. First it might be an enumeration type which
3104 -- is not suitable for computations of any kind, and second it may
3105 -- simply not have enough range. For example if the index type is
3106 -- -128..+127 then lengths can be up to 256, which is out of range of
3107 -- the type.
3109 -- For enumeration types, we can simply use Standard_Integer, this is
3110 -- sufficient since the actual number of enumeration literals cannot
3111 -- possibly exceed the range of integer (remember we will be doing the
3112 -- arithmetic with POS values, not representation values).
3114 if Is_Enumeration_Type (Ityp) then
3115 Artyp := Standard_Integer;
3117 -- If index type is Positive, we use the standard unsigned type, to give
3118 -- more room on the top of the range, obviating the need for an overflow
3119 -- check when creating the upper bound. This is needed to avoid junk
3120 -- overflow checks in the common case of String types.
3122 -- ??? Disabled for now
3124 -- elsif Istyp = Standard_Positive then
3125 -- Artyp := Standard_Unsigned;
3127 -- For modular types, we use a 32-bit modular type for types whose size
3128 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
3129 -- identity type, and for larger unsigned types we use 64-bits.
3131 elsif Is_Modular_Integer_Type (Ityp) then
3132 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
3133 Artyp := Standard_Unsigned;
3134 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
3135 Artyp := Ityp;
3136 else
3137 Artyp := RTE (RE_Long_Long_Unsigned);
3138 end if;
3140 -- Similar treatment for signed types
3142 else
3143 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
3144 Artyp := Standard_Integer;
3145 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
3146 Artyp := Ityp;
3147 else
3148 Artyp := Standard_Long_Long_Integer;
3149 end if;
3150 end if;
3152 -- Supply dummy entry at start of length array
3154 Aggr_Length (0) := Make_Artyp_Literal (0);
3156 -- Go through operands setting up the above arrays
3158 J := 1;
3159 while J <= N loop
3160 Opnd := Remove_Head (Opnds);
3161 Opnd_Typ := Etype (Opnd);
3163 -- The parent got messed up when we put the operands in a list,
3164 -- so now put back the proper parent for the saved operand, that
3165 -- is to say the concatenation node, to make sure that each operand
3166 -- is seen as a subexpression, e.g. if actions must be inserted.
3168 Set_Parent (Opnd, Cnode);
3170 -- Set will be True when we have setup one entry in the array
3172 Set := False;
3174 -- Singleton element (or character literal) case
3176 if Base_Type (Opnd_Typ) = Ctyp then
3177 NN := NN + 1;
3178 Operands (NN) := Opnd;
3179 Is_Fixed_Length (NN) := True;
3180 Fixed_Length (NN) := Uint_1;
3181 Result_May_Be_Null := False;
3183 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
3184 -- since we know that the result cannot be null).
3186 Opnd_Low_Bound (NN) :=
3187 Make_Attribute_Reference (Loc,
3188 Prefix => New_Occurrence_Of (Istyp, Loc),
3189 Attribute_Name => Name_First);
3191 Set := True;
3193 -- String literal case (can only occur for strings of course)
3195 elsif Nkind (Opnd) = N_String_Literal then
3196 Len := String_Literal_Length (Opnd_Typ);
3198 if Len /= 0 then
3199 Result_May_Be_Null := False;
3200 end if;
3202 -- Capture last operand low and high bound if result could be null
3204 if J = N and then Result_May_Be_Null then
3205 Last_Opnd_Low_Bound :=
3206 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3208 Last_Opnd_High_Bound :=
3209 Make_Op_Subtract (Loc,
3210 Left_Opnd =>
3211 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
3212 Right_Opnd => Make_Integer_Literal (Loc, 1));
3213 end if;
3215 -- Skip null string literal
3217 if J < N and then Len = 0 then
3218 goto Continue;
3219 end if;
3221 NN := NN + 1;
3222 Operands (NN) := Opnd;
3223 Is_Fixed_Length (NN) := True;
3225 -- Set length and bounds
3227 Fixed_Length (NN) := Len;
3229 Opnd_Low_Bound (NN) :=
3230 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3232 Set := True;
3234 -- All other cases
3236 else
3237 -- Check constrained case with known bounds
3239 if Is_Constrained (Opnd_Typ) then
3240 declare
3241 Index : constant Node_Id := First_Index (Opnd_Typ);
3242 Indx_Typ : constant Entity_Id := Etype (Index);
3243 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
3244 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
3246 begin
3247 -- Fixed length constrained array type with known at compile
3248 -- time bounds is last case of fixed length operand.
3250 if Compile_Time_Known_Value (Lo)
3251 and then
3252 Compile_Time_Known_Value (Hi)
3253 then
3254 declare
3255 Loval : constant Uint := Expr_Value (Lo);
3256 Hival : constant Uint := Expr_Value (Hi);
3257 Len : constant Uint :=
3258 UI_Max (Hival - Loval + 1, Uint_0);
3260 begin
3261 if Len > 0 then
3262 Result_May_Be_Null := False;
3263 end if;
3265 -- Capture last operand bounds if result could be null
3267 if J = N and then Result_May_Be_Null then
3268 Last_Opnd_Low_Bound :=
3269 Convert_To (Ityp,
3270 Make_Integer_Literal (Loc, Expr_Value (Lo)));
3272 Last_Opnd_High_Bound :=
3273 Convert_To (Ityp,
3274 Make_Integer_Literal (Loc, Expr_Value (Hi)));
3275 end if;
3277 -- Exclude null length case unless last operand
3279 if J < N and then Len = 0 then
3280 goto Continue;
3281 end if;
3283 NN := NN + 1;
3284 Operands (NN) := Opnd;
3285 Is_Fixed_Length (NN) := True;
3286 Fixed_Length (NN) := Len;
3288 Opnd_Low_Bound (NN) :=
3289 To_Ityp
3290 (Make_Integer_Literal (Loc, Expr_Value (Lo)));
3291 Set := True;
3292 end;
3293 end if;
3294 end;
3295 end if;
3297 -- All cases where the length is not known at compile time, or the
3298 -- special case of an operand which is known to be null but has a
3299 -- lower bound other than 1 or is other than a string type.
3301 if not Set then
3302 NN := NN + 1;
3304 -- Capture operand bounds
3306 Opnd_Low_Bound (NN) :=
3307 Make_Attribute_Reference (Loc,
3308 Prefix =>
3309 Duplicate_Subexpr (Opnd, Name_Req => True),
3310 Attribute_Name => Name_First);
3312 -- Capture last operand bounds if result could be null
3314 if J = N and Result_May_Be_Null then
3315 Last_Opnd_Low_Bound :=
3316 Convert_To (Ityp,
3317 Make_Attribute_Reference (Loc,
3318 Prefix =>
3319 Duplicate_Subexpr (Opnd, Name_Req => True),
3320 Attribute_Name => Name_First));
3322 Last_Opnd_High_Bound :=
3323 Convert_To (Ityp,
3324 Make_Attribute_Reference (Loc,
3325 Prefix =>
3326 Duplicate_Subexpr (Opnd, Name_Req => True),
3327 Attribute_Name => Name_Last));
3328 end if;
3330 -- Capture length of operand in entity
3332 Operands (NN) := Opnd;
3333 Is_Fixed_Length (NN) := False;
3335 Var_Length (NN) := Make_Temporary (Loc, 'L');
3337 Append_To (Actions,
3338 Make_Object_Declaration (Loc,
3339 Defining_Identifier => Var_Length (NN),
3340 Constant_Present => True,
3341 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3342 Expression =>
3343 Make_Attribute_Reference (Loc,
3344 Prefix =>
3345 Duplicate_Subexpr (Opnd, Name_Req => True),
3346 Attribute_Name => Name_Length)));
3347 end if;
3348 end if;
3350 -- Set next entry in aggregate length array
3352 -- For first entry, make either integer literal for fixed length
3353 -- or a reference to the saved length for variable length.
3355 if NN = 1 then
3356 if Is_Fixed_Length (1) then
3357 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
3358 else
3359 Aggr_Length (1) := New_Occurrence_Of (Var_Length (1), Loc);
3360 end if;
3362 -- If entry is fixed length and only fixed lengths so far, make
3363 -- appropriate new integer literal adding new length.
3365 elsif Is_Fixed_Length (NN)
3366 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
3367 then
3368 Aggr_Length (NN) :=
3369 Make_Integer_Literal (Loc,
3370 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
3372 -- All other cases, construct an addition node for the length and
3373 -- create an entity initialized to this length.
3375 else
3376 Ent := Make_Temporary (Loc, 'L');
3378 if Is_Fixed_Length (NN) then
3379 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
3380 else
3381 Clen := New_Occurrence_Of (Var_Length (NN), Loc);
3382 end if;
3384 Append_To (Actions,
3385 Make_Object_Declaration (Loc,
3386 Defining_Identifier => Ent,
3387 Constant_Present => True,
3388 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3389 Expression =>
3390 Make_Op_Add (Loc,
3391 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
3392 Right_Opnd => Clen)));
3394 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
3395 end if;
3397 <<Continue>>
3398 J := J + 1;
3399 end loop;
3401 -- If we have only skipped null operands, return the last operand
3403 if NN = 0 then
3404 Result := Opnd;
3405 goto Done;
3406 end if;
3408 -- If we have only one non-null operand, return it and we are done.
3409 -- There is one case in which this cannot be done, and that is when
3410 -- the sole operand is of the element type, in which case it must be
3411 -- converted to an array, and the easiest way of doing that is to go
3412 -- through the normal general circuit.
3414 if NN = 1 and then Base_Type (Etype (Operands (1))) /= Ctyp then
3415 Result := Operands (1);
3416 goto Done;
3417 end if;
3419 -- Cases where we have a real concatenation
3421 -- Next step is to find the low bound for the result array that we
3422 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3424 -- If the ultimate ancestor of the index subtype is a constrained array
3425 -- definition, then the lower bound is that of the index subtype as
3426 -- specified by (RM 4.5.3(6)).
3428 -- The right test here is to go to the root type, and then the ultimate
3429 -- ancestor is the first subtype of this root type.
3431 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
3432 Low_Bound :=
3433 Make_Attribute_Reference (Loc,
3434 Prefix =>
3435 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
3436 Attribute_Name => Name_First);
3438 -- If the first operand in the list has known length we know that
3439 -- the lower bound of the result is the lower bound of this operand.
3441 elsif Is_Fixed_Length (1) then
3442 Low_Bound := Opnd_Low_Bound (1);
3444 -- OK, we don't know the lower bound, we have to build a horrible
3445 -- if expression node of the form
3447 -- if Cond1'Length /= 0 then
3448 -- Opnd1 low bound
3449 -- else
3450 -- if Opnd2'Length /= 0 then
3451 -- Opnd2 low bound
3452 -- else
3453 -- ...
3455 -- The nesting ends either when we hit an operand whose length is known
3456 -- at compile time, or on reaching the last operand, whose low bound we
3457 -- take unconditionally whether or not it is null. It's easiest to do
3458 -- this with a recursive procedure:
3460 else
3461 declare
3462 function Get_Known_Bound (J : Nat) return Node_Id;
3463 -- Returns the lower bound determined by operands J .. NN
3465 ---------------------
3466 -- Get_Known_Bound --
3467 ---------------------
3469 function Get_Known_Bound (J : Nat) return Node_Id is
3470 begin
3471 if Is_Fixed_Length (J) or else J = NN then
3472 return New_Copy (Opnd_Low_Bound (J));
3474 else
3475 return
3476 Make_If_Expression (Loc,
3477 Expressions => New_List (
3479 Make_Op_Ne (Loc,
3480 Left_Opnd =>
3481 New_Occurrence_Of (Var_Length (J), Loc),
3482 Right_Opnd =>
3483 Make_Integer_Literal (Loc, 0)),
3485 New_Copy (Opnd_Low_Bound (J)),
3486 Get_Known_Bound (J + 1)));
3487 end if;
3488 end Get_Known_Bound;
3490 begin
3491 Ent := Make_Temporary (Loc, 'L');
3493 Append_To (Actions,
3494 Make_Object_Declaration (Loc,
3495 Defining_Identifier => Ent,
3496 Constant_Present => True,
3497 Object_Definition => New_Occurrence_Of (Ityp, Loc),
3498 Expression => Get_Known_Bound (1)));
3500 Low_Bound := New_Occurrence_Of (Ent, Loc);
3501 end;
3502 end if;
3504 -- Now we can safely compute the upper bound, normally
3505 -- Low_Bound + Length - 1.
3507 High_Bound :=
3508 To_Ityp
3509 (Make_Op_Add (Loc,
3510 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3511 Right_Opnd =>
3512 Make_Op_Subtract (Loc,
3513 Left_Opnd => New_Copy (Aggr_Length (NN)),
3514 Right_Opnd => Make_Artyp_Literal (1))));
3516 -- Note that calculation of the high bound may cause overflow in some
3517 -- very weird cases, so in the general case we need an overflow check on
3518 -- the high bound. We can avoid this for the common case of string types
3519 -- and other types whose index is Positive, since we chose a wider range
3520 -- for the arithmetic type.
3522 if Istyp /= Standard_Positive then
3523 Activate_Overflow_Check (High_Bound);
3524 end if;
3526 -- Handle the exceptional case where the result is null, in which case
3527 -- case the bounds come from the last operand (so that we get the proper
3528 -- bounds if the last operand is super-flat).
3530 if Result_May_Be_Null then
3531 Low_Bound :=
3532 Make_If_Expression (Loc,
3533 Expressions => New_List (
3534 Make_Op_Eq (Loc,
3535 Left_Opnd => New_Copy (Aggr_Length (NN)),
3536 Right_Opnd => Make_Artyp_Literal (0)),
3537 Last_Opnd_Low_Bound,
3538 Low_Bound));
3540 High_Bound :=
3541 Make_If_Expression (Loc,
3542 Expressions => New_List (
3543 Make_Op_Eq (Loc,
3544 Left_Opnd => New_Copy (Aggr_Length (NN)),
3545 Right_Opnd => Make_Artyp_Literal (0)),
3546 Last_Opnd_High_Bound,
3547 High_Bound));
3548 end if;
3550 -- Here is where we insert the saved up actions
3552 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3554 -- Now we construct an array object with appropriate bounds. We mark
3555 -- the target as internal to prevent useless initialization when
3556 -- Initialize_Scalars is enabled. Also since this is the actual result
3557 -- entity, we make sure we have debug information for the result.
3559 Ent := Make_Temporary (Loc, 'S');
3560 Set_Is_Internal (Ent);
3561 Set_Needs_Debug_Info (Ent);
3563 -- If the bound is statically known to be out of range, we do not want
3564 -- to abort, we want a warning and a runtime constraint error. Note that
3565 -- we have arranged that the result will not be treated as a static
3566 -- constant, so we won't get an illegality during this insertion.
3568 Insert_Action (Cnode,
3569 Make_Object_Declaration (Loc,
3570 Defining_Identifier => Ent,
3571 Object_Definition =>
3572 Make_Subtype_Indication (Loc,
3573 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
3574 Constraint =>
3575 Make_Index_Or_Discriminant_Constraint (Loc,
3576 Constraints => New_List (
3577 Make_Range (Loc,
3578 Low_Bound => Low_Bound,
3579 High_Bound => High_Bound))))),
3580 Suppress => All_Checks);
3582 -- If the result of the concatenation appears as the initializing
3583 -- expression of an object declaration, we can just rename the
3584 -- result, rather than copying it.
3586 Set_OK_To_Rename (Ent);
3588 -- Catch the static out of range case now
3590 if Raises_Constraint_Error (High_Bound) then
3591 raise Concatenation_Error;
3592 end if;
3594 -- Now we will generate the assignments to do the actual concatenation
3596 -- There is one case in which we will not do this, namely when all the
3597 -- following conditions are met:
3599 -- The result type is Standard.String
3601 -- There are nine or fewer retained (non-null) operands
3603 -- The optimization level is -O0
3605 -- The corresponding System.Concat_n.Str_Concat_n routine is
3606 -- available in the run time.
3608 -- The debug flag gnatd.c is not set
3610 -- If all these conditions are met then we generate a call to the
3611 -- relevant concatenation routine. The purpose of this is to avoid
3612 -- undesirable code bloat at -O0.
3614 if Atyp = Standard_String
3615 and then NN in 2 .. 9
3616 and then (Lib_Level_Target
3617 or else ((Optimization_Level = 0 or else Debug_Flag_Dot_CC)
3618 and then not Debug_Flag_Dot_C))
3619 then
3620 declare
3621 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3622 (RE_Str_Concat_2,
3623 RE_Str_Concat_3,
3624 RE_Str_Concat_4,
3625 RE_Str_Concat_5,
3626 RE_Str_Concat_6,
3627 RE_Str_Concat_7,
3628 RE_Str_Concat_8,
3629 RE_Str_Concat_9);
3631 begin
3632 if RTE_Available (RR (NN)) then
3633 declare
3634 Opnds : constant List_Id :=
3635 New_List (New_Occurrence_Of (Ent, Loc));
3637 begin
3638 for J in 1 .. NN loop
3639 if Is_List_Member (Operands (J)) then
3640 Remove (Operands (J));
3641 end if;
3643 if Base_Type (Etype (Operands (J))) = Ctyp then
3644 Append_To (Opnds,
3645 Make_Aggregate (Loc,
3646 Component_Associations => New_List (
3647 Make_Component_Association (Loc,
3648 Choices => New_List (
3649 Make_Integer_Literal (Loc, 1)),
3650 Expression => Operands (J)))));
3652 else
3653 Append_To (Opnds, Operands (J));
3654 end if;
3655 end loop;
3657 Insert_Action (Cnode,
3658 Make_Procedure_Call_Statement (Loc,
3659 Name => New_Occurrence_Of (RTE (RR (NN)), Loc),
3660 Parameter_Associations => Opnds));
3662 Result := New_Occurrence_Of (Ent, Loc);
3663 goto Done;
3664 end;
3665 end if;
3666 end;
3667 end if;
3669 -- Not special case so generate the assignments
3671 Known_Non_Null_Operand_Seen := False;
3673 for J in 1 .. NN loop
3674 declare
3675 Lo : constant Node_Id :=
3676 Make_Op_Add (Loc,
3677 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3678 Right_Opnd => Aggr_Length (J - 1));
3680 Hi : constant Node_Id :=
3681 Make_Op_Add (Loc,
3682 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3683 Right_Opnd =>
3684 Make_Op_Subtract (Loc,
3685 Left_Opnd => Aggr_Length (J),
3686 Right_Opnd => Make_Artyp_Literal (1)));
3688 begin
3689 -- Singleton case, simple assignment
3691 if Base_Type (Etype (Operands (J))) = Ctyp then
3692 Known_Non_Null_Operand_Seen := True;
3693 Insert_Action (Cnode,
3694 Make_Assignment_Statement (Loc,
3695 Name =>
3696 Make_Indexed_Component (Loc,
3697 Prefix => New_Occurrence_Of (Ent, Loc),
3698 Expressions => New_List (To_Ityp (Lo))),
3699 Expression => Operands (J)),
3700 Suppress => All_Checks);
3702 -- Array case, slice assignment, skipped when argument is fixed
3703 -- length and known to be null.
3705 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3706 declare
3707 Assign : Node_Id :=
3708 Make_Assignment_Statement (Loc,
3709 Name =>
3710 Make_Slice (Loc,
3711 Prefix =>
3712 New_Occurrence_Of (Ent, Loc),
3713 Discrete_Range =>
3714 Make_Range (Loc,
3715 Low_Bound => To_Ityp (Lo),
3716 High_Bound => To_Ityp (Hi))),
3717 Expression => Operands (J));
3718 begin
3719 if Is_Fixed_Length (J) then
3720 Known_Non_Null_Operand_Seen := True;
3722 elsif not Known_Non_Null_Operand_Seen then
3724 -- Here if operand length is not statically known and no
3725 -- operand known to be non-null has been processed yet.
3726 -- If operand length is 0, we do not need to perform the
3727 -- assignment, and we must avoid the evaluation of the
3728 -- high bound of the slice, since it may underflow if the
3729 -- low bound is Ityp'First.
3731 Assign :=
3732 Make_Implicit_If_Statement (Cnode,
3733 Condition =>
3734 Make_Op_Ne (Loc,
3735 Left_Opnd =>
3736 New_Occurrence_Of (Var_Length (J), Loc),
3737 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3738 Then_Statements => New_List (Assign));
3739 end if;
3741 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3742 end;
3743 end if;
3744 end;
3745 end loop;
3747 -- Finally we build the result, which is a reference to the array object
3749 Result := New_Occurrence_Of (Ent, Loc);
3751 <<Done>>
3752 Rewrite (Cnode, Result);
3753 Analyze_And_Resolve (Cnode, Atyp);
3755 exception
3756 when Concatenation_Error =>
3758 -- Kill warning generated for the declaration of the static out of
3759 -- range high bound, and instead generate a Constraint_Error with
3760 -- an appropriate specific message.
3762 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3763 Apply_Compile_Time_Constraint_Error
3764 (N => Cnode,
3765 Msg => "concatenation result upper bound out of range??",
3766 Reason => CE_Range_Check_Failed);
3767 end Expand_Concatenate;
3769 ---------------------------------------------------
3770 -- Expand_Membership_Minimize_Eliminate_Overflow --
3771 ---------------------------------------------------
3773 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is
3774 pragma Assert (Nkind (N) = N_In);
3775 -- Despite the name, this routine applies only to N_In, not to
3776 -- N_Not_In. The latter is always rewritten as not (X in Y).
3778 Result_Type : constant Entity_Id := Etype (N);
3779 -- Capture result type, may be a derived boolean type
3781 Loc : constant Source_Ptr := Sloc (N);
3782 Lop : constant Node_Id := Left_Opnd (N);
3783 Rop : constant Node_Id := Right_Opnd (N);
3785 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3786 -- is thus tempting to capture these values, but due to the rewrites
3787 -- that occur as a result of overflow checking, these values change
3788 -- as we go along, and it is safe just to always use Etype explicitly.
3790 Restype : constant Entity_Id := Etype (N);
3791 -- Save result type
3793 Lo, Hi : Uint;
3794 -- Bounds in Minimize calls, not used currently
3796 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
3797 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3799 begin
3800 Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False);
3802 -- If right operand is a subtype name, and the subtype name has no
3803 -- predicate, then we can just replace the right operand with an
3804 -- explicit range T'First .. T'Last, and use the explicit range code.
3806 if Nkind (Rop) /= N_Range
3807 and then No (Predicate_Function (Etype (Rop)))
3808 then
3809 declare
3810 Rtyp : constant Entity_Id := Etype (Rop);
3811 begin
3812 Rewrite (Rop,
3813 Make_Range (Loc,
3814 Low_Bound =>
3815 Make_Attribute_Reference (Loc,
3816 Attribute_Name => Name_First,
3817 Prefix => New_Occurrence_Of (Rtyp, Loc)),
3818 High_Bound =>
3819 Make_Attribute_Reference (Loc,
3820 Attribute_Name => Name_Last,
3821 Prefix => New_Occurrence_Of (Rtyp, Loc))));
3822 Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks);
3823 end;
3824 end if;
3826 -- Here for the explicit range case. Note that the bounds of the range
3827 -- have not been processed for minimized or eliminated checks.
3829 if Nkind (Rop) = N_Range then
3830 Minimize_Eliminate_Overflows
3831 (Low_Bound (Rop), Lo, Hi, Top_Level => False);
3832 Minimize_Eliminate_Overflows
3833 (High_Bound (Rop), Lo, Hi, Top_Level => False);
3835 -- We have A in B .. C, treated as A >= B and then A <= C
3837 -- Bignum case
3839 if Is_RTE (Etype (Lop), RE_Bignum)
3840 or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum)
3841 or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum)
3842 then
3843 declare
3844 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3845 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3846 L : constant Entity_Id :=
3847 Make_Defining_Identifier (Loc, Name_uL);
3848 Lopnd : constant Node_Id := Convert_To_Bignum (Lop);
3849 Lbound : constant Node_Id :=
3850 Convert_To_Bignum (Low_Bound (Rop));
3851 Hbound : constant Node_Id :=
3852 Convert_To_Bignum (High_Bound (Rop));
3854 -- Now we rewrite the membership test node to look like
3856 -- do
3857 -- Bnn : Result_Type;
3858 -- declare
3859 -- M : Mark_Id := SS_Mark;
3860 -- L : Bignum := Lopnd;
3861 -- begin
3862 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3863 -- SS_Release (M);
3864 -- end;
3865 -- in
3866 -- Bnn
3867 -- end
3869 begin
3870 -- Insert declaration of L into declarations of bignum block
3872 Insert_After
3873 (Last (Declarations (Blk)),
3874 Make_Object_Declaration (Loc,
3875 Defining_Identifier => L,
3876 Object_Definition =>
3877 New_Occurrence_Of (RTE (RE_Bignum), Loc),
3878 Expression => Lopnd));
3880 -- Insert assignment to Bnn into expressions of bignum block
3882 Insert_Before
3883 (First (Statements (Handled_Statement_Sequence (Blk))),
3884 Make_Assignment_Statement (Loc,
3885 Name => New_Occurrence_Of (Bnn, Loc),
3886 Expression =>
3887 Make_And_Then (Loc,
3888 Left_Opnd =>
3889 Make_Function_Call (Loc,
3890 Name =>
3891 New_Occurrence_Of (RTE (RE_Big_GE), Loc),
3892 Parameter_Associations => New_List (
3893 New_Occurrence_Of (L, Loc),
3894 Lbound)),
3896 Right_Opnd =>
3897 Make_Function_Call (Loc,
3898 Name =>
3899 New_Occurrence_Of (RTE (RE_Big_LE), Loc),
3900 Parameter_Associations => New_List (
3901 New_Occurrence_Of (L, Loc),
3902 Hbound)))));
3904 -- Now rewrite the node
3906 Rewrite (N,
3907 Make_Expression_With_Actions (Loc,
3908 Actions => New_List (
3909 Make_Object_Declaration (Loc,
3910 Defining_Identifier => Bnn,
3911 Object_Definition =>
3912 New_Occurrence_Of (Result_Type, Loc)),
3913 Blk),
3914 Expression => New_Occurrence_Of (Bnn, Loc)));
3915 Analyze_And_Resolve (N, Result_Type);
3916 return;
3917 end;
3919 -- Here if no bignums around
3921 else
3922 -- Case where types are all the same
3924 if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop)))
3925 and then
3926 Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop)))
3927 then
3928 null;
3930 -- If types are not all the same, it means that we have rewritten
3931 -- at least one of them to be of type Long_Long_Integer, and we
3932 -- will convert the other operands to Long_Long_Integer.
3934 else
3935 Convert_To_And_Rewrite (LLIB, Lop);
3936 Set_Analyzed (Lop, False);
3937 Analyze_And_Resolve (Lop, LLIB);
3939 -- For the right operand, avoid unnecessary recursion into
3940 -- this routine, we know that overflow is not possible.
3942 Convert_To_And_Rewrite (LLIB, Low_Bound (Rop));
3943 Convert_To_And_Rewrite (LLIB, High_Bound (Rop));
3944 Set_Analyzed (Rop, False);
3945 Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check);
3946 end if;
3948 -- Now the three operands are of the same signed integer type,
3949 -- so we can use the normal expansion routine for membership,
3950 -- setting the flag to prevent recursion into this procedure.
3952 Set_No_Minimize_Eliminate (N);
3953 Expand_N_In (N);
3954 end if;
3956 -- Right operand is a subtype name and the subtype has a predicate. We
3957 -- have to make sure the predicate is checked, and for that we need to
3958 -- use the standard N_In circuitry with appropriate types.
3960 else
3961 pragma Assert (Present (Predicate_Function (Etype (Rop))));
3963 -- If types are "right", just call Expand_N_In preventing recursion
3965 if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then
3966 Set_No_Minimize_Eliminate (N);
3967 Expand_N_In (N);
3969 -- Bignum case
3971 elsif Is_RTE (Etype (Lop), RE_Bignum) then
3973 -- For X in T, we want to rewrite our node as
3975 -- do
3976 -- Bnn : Result_Type;
3978 -- declare
3979 -- M : Mark_Id := SS_Mark;
3980 -- Lnn : Long_Long_Integer'Base
3981 -- Nnn : Bignum;
3983 -- begin
3984 -- Nnn := X;
3986 -- if not Bignum_In_LLI_Range (Nnn) then
3987 -- Bnn := False;
3988 -- else
3989 -- Lnn := From_Bignum (Nnn);
3990 -- Bnn :=
3991 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3992 -- and then T'Base (Lnn) in T;
3993 -- end if;
3995 -- SS_Release (M);
3996 -- end
3997 -- in
3998 -- Bnn
3999 -- end
4001 -- A bit gruesome, but there doesn't seem to be a simpler way
4003 declare
4004 Blk : constant Node_Id := Make_Bignum_Block (Loc);
4005 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
4006 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N);
4007 Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N);
4008 T : constant Entity_Id := Etype (Rop);
4009 TB : constant Entity_Id := Base_Type (T);
4010 Nin : Node_Id;
4012 begin
4013 -- Mark the last membership operation to prevent recursion
4015 Nin :=
4016 Make_In (Loc,
4017 Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)),
4018 Right_Opnd => New_Occurrence_Of (T, Loc));
4019 Set_No_Minimize_Eliminate (Nin);
4021 -- Now decorate the block
4023 Insert_After
4024 (Last (Declarations (Blk)),
4025 Make_Object_Declaration (Loc,
4026 Defining_Identifier => Lnn,
4027 Object_Definition => New_Occurrence_Of (LLIB, Loc)));
4029 Insert_After
4030 (Last (Declarations (Blk)),
4031 Make_Object_Declaration (Loc,
4032 Defining_Identifier => Nnn,
4033 Object_Definition =>
4034 New_Occurrence_Of (RTE (RE_Bignum), Loc)));
4036 Insert_List_Before
4037 (First (Statements (Handled_Statement_Sequence (Blk))),
4038 New_List (
4039 Make_Assignment_Statement (Loc,
4040 Name => New_Occurrence_Of (Nnn, Loc),
4041 Expression => Relocate_Node (Lop)),
4043 Make_Implicit_If_Statement (N,
4044 Condition =>
4045 Make_Op_Not (Loc,
4046 Right_Opnd =>
4047 Make_Function_Call (Loc,
4048 Name =>
4049 New_Occurrence_Of
4050 (RTE (RE_Bignum_In_LLI_Range), Loc),
4051 Parameter_Associations => New_List (
4052 New_Occurrence_Of (Nnn, Loc)))),
4054 Then_Statements => New_List (
4055 Make_Assignment_Statement (Loc,
4056 Name => New_Occurrence_Of (Bnn, Loc),
4057 Expression =>
4058 New_Occurrence_Of (Standard_False, Loc))),
4060 Else_Statements => New_List (
4061 Make_Assignment_Statement (Loc,
4062 Name => New_Occurrence_Of (Lnn, Loc),
4063 Expression =>
4064 Make_Function_Call (Loc,
4065 Name =>
4066 New_Occurrence_Of (RTE (RE_From_Bignum), Loc),
4067 Parameter_Associations => New_List (
4068 New_Occurrence_Of (Nnn, Loc)))),
4070 Make_Assignment_Statement (Loc,
4071 Name => New_Occurrence_Of (Bnn, Loc),
4072 Expression =>
4073 Make_And_Then (Loc,
4074 Left_Opnd =>
4075 Make_In (Loc,
4076 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4077 Right_Opnd =>
4078 Make_Range (Loc,
4079 Low_Bound =>
4080 Convert_To (LLIB,
4081 Make_Attribute_Reference (Loc,
4082 Attribute_Name => Name_First,
4083 Prefix =>
4084 New_Occurrence_Of (TB, Loc))),
4086 High_Bound =>
4087 Convert_To (LLIB,
4088 Make_Attribute_Reference (Loc,
4089 Attribute_Name => Name_Last,
4090 Prefix =>
4091 New_Occurrence_Of (TB, Loc))))),
4093 Right_Opnd => Nin))))));
4095 -- Now we can do the rewrite
4097 Rewrite (N,
4098 Make_Expression_With_Actions (Loc,
4099 Actions => New_List (
4100 Make_Object_Declaration (Loc,
4101 Defining_Identifier => Bnn,
4102 Object_Definition =>
4103 New_Occurrence_Of (Result_Type, Loc)),
4104 Blk),
4105 Expression => New_Occurrence_Of (Bnn, Loc)));
4106 Analyze_And_Resolve (N, Result_Type);
4107 return;
4108 end;
4110 -- Not bignum case, but types don't match (this means we rewrote the
4111 -- left operand to be Long_Long_Integer).
4113 else
4114 pragma Assert (Base_Type (Etype (Lop)) = LLIB);
4116 -- We rewrite the membership test as (where T is the type with
4117 -- the predicate, i.e. the type of the right operand)
4119 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
4120 -- and then T'Base (Lop) in T
4122 declare
4123 T : constant Entity_Id := Etype (Rop);
4124 TB : constant Entity_Id := Base_Type (T);
4125 Nin : Node_Id;
4127 begin
4128 -- The last membership test is marked to prevent recursion
4130 Nin :=
4131 Make_In (Loc,
4132 Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)),
4133 Right_Opnd => New_Occurrence_Of (T, Loc));
4134 Set_No_Minimize_Eliminate (Nin);
4136 -- Now do the rewrite
4138 Rewrite (N,
4139 Make_And_Then (Loc,
4140 Left_Opnd =>
4141 Make_In (Loc,
4142 Left_Opnd => Lop,
4143 Right_Opnd =>
4144 Make_Range (Loc,
4145 Low_Bound =>
4146 Convert_To (LLIB,
4147 Make_Attribute_Reference (Loc,
4148 Attribute_Name => Name_First,
4149 Prefix =>
4150 New_Occurrence_Of (TB, Loc))),
4151 High_Bound =>
4152 Convert_To (LLIB,
4153 Make_Attribute_Reference (Loc,
4154 Attribute_Name => Name_Last,
4155 Prefix =>
4156 New_Occurrence_Of (TB, Loc))))),
4157 Right_Opnd => Nin));
4158 Set_Analyzed (N, False);
4159 Analyze_And_Resolve (N, Restype);
4160 end;
4161 end if;
4162 end if;
4163 end Expand_Membership_Minimize_Eliminate_Overflow;
4165 ------------------------
4166 -- Expand_N_Allocator --
4167 ------------------------
4169 procedure Expand_N_Allocator (N : Node_Id) is
4170 Etyp : constant Entity_Id := Etype (Expression (N));
4171 Loc : constant Source_Ptr := Sloc (N);
4172 PtrT : constant Entity_Id := Etype (N);
4174 procedure Rewrite_Coextension (N : Node_Id);
4175 -- Static coextensions have the same lifetime as the entity they
4176 -- constrain. Such occurrences can be rewritten as aliased objects
4177 -- and their unrestricted access used instead of the coextension.
4179 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
4180 -- Given a constrained array type E, returns a node representing the
4181 -- code to compute the size in storage elements for the given type.
4182 -- This is done without using the attribute (which malfunctions for
4183 -- large sizes ???)
4185 -------------------------
4186 -- Rewrite_Coextension --
4187 -------------------------
4189 procedure Rewrite_Coextension (N : Node_Id) is
4190 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
4191 Temp_Decl : Node_Id;
4193 begin
4194 -- Generate:
4195 -- Cnn : aliased Etyp;
4197 Temp_Decl :=
4198 Make_Object_Declaration (Loc,
4199 Defining_Identifier => Temp_Id,
4200 Aliased_Present => True,
4201 Object_Definition => New_Occurrence_Of (Etyp, Loc));
4203 if Nkind (Expression (N)) = N_Qualified_Expression then
4204 Set_Expression (Temp_Decl, Expression (Expression (N)));
4205 end if;
4207 Insert_Action (N, Temp_Decl);
4208 Rewrite (N,
4209 Make_Attribute_Reference (Loc,
4210 Prefix => New_Occurrence_Of (Temp_Id, Loc),
4211 Attribute_Name => Name_Unrestricted_Access));
4213 Analyze_And_Resolve (N, PtrT);
4214 end Rewrite_Coextension;
4216 ------------------------------
4217 -- Size_In_Storage_Elements --
4218 ------------------------------
4220 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
4221 begin
4222 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4223 -- However, the reason for the existence of this function is
4224 -- to construct a test for sizes too large, which means near the
4225 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4226 -- is that we get overflows when sizes are greater than 2**31.
4228 -- So what we end up doing for array types is to use the expression:
4230 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4232 -- which avoids this problem. All this is a bit bogus, but it does
4233 -- mean we catch common cases of trying to allocate arrays that
4234 -- are too large, and which in the absence of a check results in
4235 -- undetected chaos ???
4237 -- Note in particular that this is a pessimistic estimate in the
4238 -- case of packed array types, where an array element might occupy
4239 -- just a fraction of a storage element???
4241 declare
4242 Len : Node_Id;
4243 Res : Node_Id;
4245 begin
4246 for J in 1 .. Number_Dimensions (E) loop
4247 Len :=
4248 Make_Attribute_Reference (Loc,
4249 Prefix => New_Occurrence_Of (E, Loc),
4250 Attribute_Name => Name_Length,
4251 Expressions => New_List (Make_Integer_Literal (Loc, J)));
4253 if J = 1 then
4254 Res := Len;
4256 else
4257 Res :=
4258 Make_Op_Multiply (Loc,
4259 Left_Opnd => Res,
4260 Right_Opnd => Len);
4261 end if;
4262 end loop;
4264 return
4265 Make_Op_Multiply (Loc,
4266 Left_Opnd => Len,
4267 Right_Opnd =>
4268 Make_Attribute_Reference (Loc,
4269 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
4270 Attribute_Name => Name_Max_Size_In_Storage_Elements));
4271 end;
4272 end Size_In_Storage_Elements;
4274 -- Local variables
4276 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
4277 Desig : Entity_Id;
4278 Nod : Node_Id;
4279 Pool : Entity_Id;
4280 Rel_Typ : Entity_Id;
4281 Temp : Entity_Id;
4283 -- Start of processing for Expand_N_Allocator
4285 begin
4286 -- RM E.2.3(22). We enforce that the expected type of an allocator
4287 -- shall not be a remote access-to-class-wide-limited-private type
4289 -- Why is this being done at expansion time, seems clearly wrong ???
4291 Validate_Remote_Access_To_Class_Wide_Type (N);
4293 -- Processing for anonymous access-to-controlled types. These access
4294 -- types receive a special finalization master which appears in the
4295 -- declarations of the enclosing semantic unit. This expansion is done
4296 -- now to ensure that any additional types generated by this routine or
4297 -- Expand_Allocator_Expression inherit the proper type attributes.
4299 if (Ekind (PtrT) = E_Anonymous_Access_Type
4300 or else (Is_Itype (PtrT) and then No (Finalization_Master (PtrT))))
4301 and then Needs_Finalization (Dtyp)
4302 then
4303 -- Detect the allocation of an anonymous controlled object where the
4304 -- type of the context is named. For example:
4306 -- procedure Proc (Ptr : Named_Access_Typ);
4307 -- Proc (new Designated_Typ);
4309 -- Regardless of the anonymous-to-named access type conversion, the
4310 -- lifetime of the object must be associated with the named access
4311 -- type. Use the finalization-related attributes of this type.
4313 if Nkind_In (Parent (N), N_Type_Conversion,
4314 N_Unchecked_Type_Conversion)
4315 and then Ekind_In (Etype (Parent (N)), E_Access_Subtype,
4316 E_Access_Type,
4317 E_General_Access_Type)
4318 then
4319 Rel_Typ := Etype (Parent (N));
4320 else
4321 Rel_Typ := Empty;
4322 end if;
4324 -- Anonymous access-to-controlled types allocate on the global pool.
4325 -- Do not set this attribute on .NET/JVM since those targets do not
4326 -- support pools. Note that this is a "root type only" attribute.
4328 if No (Associated_Storage_Pool (PtrT)) and then VM_Target = No_VM then
4329 if Present (Rel_Typ) then
4330 Set_Associated_Storage_Pool
4331 (Root_Type (PtrT), Associated_Storage_Pool (Rel_Typ));
4332 else
4333 Set_Associated_Storage_Pool
4334 (Root_Type (PtrT), RTE (RE_Global_Pool_Object));
4335 end if;
4336 end if;
4338 -- The finalization master must be inserted and analyzed as part of
4339 -- the current semantic unit. Note that the master is updated when
4340 -- analysis changes current units. Note that this is a "root type
4341 -- only" attribute.
4343 if Present (Rel_Typ) then
4344 Set_Finalization_Master
4345 (Root_Type (PtrT), Finalization_Master (Rel_Typ));
4346 else
4347 Set_Finalization_Master
4348 (Root_Type (PtrT), Current_Anonymous_Master);
4349 end if;
4350 end if;
4352 -- Set the storage pool and find the appropriate version of Allocate to
4353 -- call. Do not overwrite the storage pool if it is already set, which
4354 -- can happen for build-in-place function returns (see
4355 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4357 if No (Storage_Pool (N)) then
4358 Pool := Associated_Storage_Pool (Root_Type (PtrT));
4360 if Present (Pool) then
4361 Set_Storage_Pool (N, Pool);
4363 if Is_RTE (Pool, RE_SS_Pool) then
4364 if VM_Target = No_VM then
4365 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
4366 end if;
4368 -- In the case of an allocator for a simple storage pool, locate
4369 -- and save a reference to the pool type's Allocate routine.
4371 elsif Present (Get_Rep_Pragma
4372 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4373 then
4374 declare
4375 Pool_Type : constant Entity_Id := Base_Type (Etype (Pool));
4376 Alloc_Op : Entity_Id;
4377 begin
4378 Alloc_Op := Get_Name_Entity_Id (Name_Allocate);
4379 while Present (Alloc_Op) loop
4380 if Scope (Alloc_Op) = Scope (Pool_Type)
4381 and then Present (First_Formal (Alloc_Op))
4382 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
4383 then
4384 Set_Procedure_To_Call (N, Alloc_Op);
4385 exit;
4386 else
4387 Alloc_Op := Homonym (Alloc_Op);
4388 end if;
4389 end loop;
4390 end;
4392 elsif Is_Class_Wide_Type (Etype (Pool)) then
4393 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
4395 else
4396 Set_Procedure_To_Call (N,
4397 Find_Prim_Op (Etype (Pool), Name_Allocate));
4398 end if;
4399 end if;
4400 end if;
4402 -- Under certain circumstances we can replace an allocator by an access
4403 -- to statically allocated storage. The conditions, as noted in AARM
4404 -- 3.10 (10c) are as follows:
4406 -- Size and initial value is known at compile time
4407 -- Access type is access-to-constant
4409 -- The allocator is not part of a constraint on a record component,
4410 -- because in that case the inserted actions are delayed until the
4411 -- record declaration is fully analyzed, which is too late for the
4412 -- analysis of the rewritten allocator.
4414 if Is_Access_Constant (PtrT)
4415 and then Nkind (Expression (N)) = N_Qualified_Expression
4416 and then Compile_Time_Known_Value (Expression (Expression (N)))
4417 and then Size_Known_At_Compile_Time
4418 (Etype (Expression (Expression (N))))
4419 and then not Is_Record_Type (Current_Scope)
4420 then
4421 -- Here we can do the optimization. For the allocator
4423 -- new x'(y)
4425 -- We insert an object declaration
4427 -- Tnn : aliased x := y;
4429 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4430 -- marked as requiring static allocation.
4432 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
4433 Desig := Subtype_Mark (Expression (N));
4435 -- If context is constrained, use constrained subtype directly,
4436 -- so that the constant is not labelled as having a nominally
4437 -- unconstrained subtype.
4439 if Entity (Desig) = Base_Type (Dtyp) then
4440 Desig := New_Occurrence_Of (Dtyp, Loc);
4441 end if;
4443 Insert_Action (N,
4444 Make_Object_Declaration (Loc,
4445 Defining_Identifier => Temp,
4446 Aliased_Present => True,
4447 Constant_Present => Is_Access_Constant (PtrT),
4448 Object_Definition => Desig,
4449 Expression => Expression (Expression (N))));
4451 Rewrite (N,
4452 Make_Attribute_Reference (Loc,
4453 Prefix => New_Occurrence_Of (Temp, Loc),
4454 Attribute_Name => Name_Unrestricted_Access));
4456 Analyze_And_Resolve (N, PtrT);
4458 -- We set the variable as statically allocated, since we don't want
4459 -- it going on the stack of the current procedure.
4461 Set_Is_Statically_Allocated (Temp);
4462 return;
4463 end if;
4465 -- Same if the allocator is an access discriminant for a local object:
4466 -- instead of an allocator we create a local value and constrain the
4467 -- enclosing object with the corresponding access attribute.
4469 if Is_Static_Coextension (N) then
4470 Rewrite_Coextension (N);
4471 return;
4472 end if;
4474 -- Check for size too large, we do this because the back end misses
4475 -- proper checks here and can generate rubbish allocation calls when
4476 -- we are near the limit. We only do this for the 32-bit address case
4477 -- since that is from a practical point of view where we see a problem.
4479 if System_Address_Size = 32
4480 and then not Storage_Checks_Suppressed (PtrT)
4481 and then not Storage_Checks_Suppressed (Dtyp)
4482 and then not Storage_Checks_Suppressed (Etyp)
4483 then
4484 -- The check we want to generate should look like
4486 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4487 -- raise Storage_Error;
4488 -- end if;
4490 -- where 3.5 gigabytes is a constant large enough to accommodate any
4491 -- reasonable request for. But we can't do it this way because at
4492 -- least at the moment we don't compute this attribute right, and
4493 -- can silently give wrong results when the result gets large. Since
4494 -- this is all about large results, that's bad, so instead we only
4495 -- apply the check for constrained arrays, and manually compute the
4496 -- value of the attribute ???
4498 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
4499 Insert_Action (N,
4500 Make_Raise_Storage_Error (Loc,
4501 Condition =>
4502 Make_Op_Gt (Loc,
4503 Left_Opnd => Size_In_Storage_Elements (Etyp),
4504 Right_Opnd =>
4505 Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))),
4506 Reason => SE_Object_Too_Large));
4507 end if;
4508 end if;
4510 -- If no storage pool has been specified and we have the restriction
4511 -- No_Standard_Allocators_After_Elaboration is present, then generate
4512 -- a call to Elaboration_Allocators.Check_Standard_Allocator.
4514 if Nkind (N) = N_Allocator
4515 and then No (Storage_Pool (N))
4516 and then Restriction_Active (No_Standard_Allocators_After_Elaboration)
4517 then
4518 Insert_Action (N,
4519 Make_Procedure_Call_Statement (Loc,
4520 Name =>
4521 New_Occurrence_Of (RTE (RE_Check_Standard_Allocator), Loc)));
4522 end if;
4524 -- Handle case of qualified expression (other than optimization above)
4525 -- First apply constraint checks, because the bounds or discriminants
4526 -- in the aggregate might not match the subtype mark in the allocator.
4528 if Nkind (Expression (N)) = N_Qualified_Expression then
4529 Apply_Constraint_Check
4530 (Expression (Expression (N)), Etype (Expression (N)));
4532 Expand_Allocator_Expression (N);
4533 return;
4534 end if;
4536 -- If the allocator is for a type which requires initialization, and
4537 -- there is no initial value (i.e. operand is a subtype indication
4538 -- rather than a qualified expression), then we must generate a call to
4539 -- the initialization routine using an expressions action node:
4541 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4543 -- Here ptr_T is the pointer type for the allocator, and T is the
4544 -- subtype of the allocator. A special case arises if the designated
4545 -- type of the access type is a task or contains tasks. In this case
4546 -- the call to Init (Temp.all ...) is replaced by code that ensures
4547 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4548 -- for details). In addition, if the type T is a task type, then the
4549 -- first argument to Init must be converted to the task record type.
4551 declare
4552 T : constant Entity_Id := Entity (Expression (N));
4553 Args : List_Id;
4554 Decls : List_Id;
4555 Decl : Node_Id;
4556 Discr : Elmt_Id;
4557 Init : Entity_Id;
4558 Init_Arg1 : Node_Id;
4559 Temp_Decl : Node_Id;
4560 Temp_Type : Entity_Id;
4562 begin
4563 if No_Initialization (N) then
4565 -- Even though this might be a simple allocation, create a custom
4566 -- Allocate if the context requires it. Since .NET/JVM compilers
4567 -- do not support pools, this step is skipped.
4569 if VM_Target = No_VM
4570 and then Present (Finalization_Master (PtrT))
4571 then
4572 Build_Allocate_Deallocate_Proc
4573 (N => N,
4574 Is_Allocate => True);
4575 end if;
4577 -- Case of no initialization procedure present
4579 elsif not Has_Non_Null_Base_Init_Proc (T) then
4581 -- Case of simple initialization required
4583 if Needs_Simple_Initialization (T) then
4584 Check_Restriction (No_Default_Initialization, N);
4585 Rewrite (Expression (N),
4586 Make_Qualified_Expression (Loc,
4587 Subtype_Mark => New_Occurrence_Of (T, Loc),
4588 Expression => Get_Simple_Init_Val (T, N)));
4590 Analyze_And_Resolve (Expression (Expression (N)), T);
4591 Analyze_And_Resolve (Expression (N), T);
4592 Set_Paren_Count (Expression (Expression (N)), 1);
4593 Expand_N_Allocator (N);
4595 -- No initialization required
4597 else
4598 null;
4599 end if;
4601 -- Case of initialization procedure present, must be called
4603 else
4604 Check_Restriction (No_Default_Initialization, N);
4606 if not Restriction_Active (No_Default_Initialization) then
4607 Init := Base_Init_Proc (T);
4608 Nod := N;
4609 Temp := Make_Temporary (Loc, 'P');
4611 -- Construct argument list for the initialization routine call
4613 Init_Arg1 :=
4614 Make_Explicit_Dereference (Loc,
4615 Prefix =>
4616 New_Occurrence_Of (Temp, Loc));
4618 Set_Assignment_OK (Init_Arg1);
4619 Temp_Type := PtrT;
4621 -- The initialization procedure expects a specific type. if the
4622 -- context is access to class wide, indicate that the object
4623 -- being allocated has the right specific type.
4625 if Is_Class_Wide_Type (Dtyp) then
4626 Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1);
4627 end if;
4629 -- If designated type is a concurrent type or if it is private
4630 -- type whose definition is a concurrent type, the first
4631 -- argument in the Init routine has to be unchecked conversion
4632 -- to the corresponding record type. If the designated type is
4633 -- a derived type, also convert the argument to its root type.
4635 if Is_Concurrent_Type (T) then
4636 Init_Arg1 :=
4637 Unchecked_Convert_To (
4638 Corresponding_Record_Type (T), Init_Arg1);
4640 elsif Is_Private_Type (T)
4641 and then Present (Full_View (T))
4642 and then Is_Concurrent_Type (Full_View (T))
4643 then
4644 Init_Arg1 :=
4645 Unchecked_Convert_To
4646 (Corresponding_Record_Type (Full_View (T)), Init_Arg1);
4648 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
4649 declare
4650 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
4652 begin
4653 Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1);
4654 Set_Etype (Init_Arg1, Ftyp);
4655 end;
4656 end if;
4658 Args := New_List (Init_Arg1);
4660 -- For the task case, pass the Master_Id of the access type as
4661 -- the value of the _Master parameter, and _Chain as the value
4662 -- of the _Chain parameter (_Chain will be defined as part of
4663 -- the generated code for the allocator).
4665 -- In Ada 2005, the context may be a function that returns an
4666 -- anonymous access type. In that case the Master_Id has been
4667 -- created when expanding the function declaration.
4669 if Has_Task (T) then
4670 if No (Master_Id (Base_Type (PtrT))) then
4672 -- The designated type was an incomplete type, and the
4673 -- access type did not get expanded. Salvage it now.
4675 if not Restriction_Active (No_Task_Hierarchy) then
4676 if Present (Parent (Base_Type (PtrT))) then
4677 Expand_N_Full_Type_Declaration
4678 (Parent (Base_Type (PtrT)));
4680 -- The only other possibility is an itype. For this
4681 -- case, the master must exist in the context. This is
4682 -- the case when the allocator initializes an access
4683 -- component in an init-proc.
4685 else
4686 pragma Assert (Is_Itype (PtrT));
4687 Build_Master_Renaming (PtrT, N);
4688 end if;
4689 end if;
4690 end if;
4692 -- If the context of the allocator is a declaration or an
4693 -- assignment, we can generate a meaningful image for it,
4694 -- even though subsequent assignments might remove the
4695 -- connection between task and entity. We build this image
4696 -- when the left-hand side is a simple variable, a simple
4697 -- indexed assignment or a simple selected component.
4699 if Nkind (Parent (N)) = N_Assignment_Statement then
4700 declare
4701 Nam : constant Node_Id := Name (Parent (N));
4703 begin
4704 if Is_Entity_Name (Nam) then
4705 Decls :=
4706 Build_Task_Image_Decls
4707 (Loc,
4708 New_Occurrence_Of
4709 (Entity (Nam), Sloc (Nam)), T);
4711 elsif Nkind_In (Nam, N_Indexed_Component,
4712 N_Selected_Component)
4713 and then Is_Entity_Name (Prefix (Nam))
4714 then
4715 Decls :=
4716 Build_Task_Image_Decls
4717 (Loc, Nam, Etype (Prefix (Nam)));
4718 else
4719 Decls := Build_Task_Image_Decls (Loc, T, T);
4720 end if;
4721 end;
4723 elsif Nkind (Parent (N)) = N_Object_Declaration then
4724 Decls :=
4725 Build_Task_Image_Decls
4726 (Loc, Defining_Identifier (Parent (N)), T);
4728 else
4729 Decls := Build_Task_Image_Decls (Loc, T, T);
4730 end if;
4732 if Restriction_Active (No_Task_Hierarchy) then
4733 Append_To (Args,
4734 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
4735 else
4736 Append_To (Args,
4737 New_Occurrence_Of
4738 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
4739 end if;
4741 Append_To (Args, Make_Identifier (Loc, Name_uChain));
4743 Decl := Last (Decls);
4744 Append_To (Args,
4745 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
4747 -- Has_Task is false, Decls not used
4749 else
4750 Decls := No_List;
4751 end if;
4753 -- Add discriminants if discriminated type
4755 declare
4756 Dis : Boolean := False;
4757 Typ : Entity_Id;
4759 begin
4760 if Has_Discriminants (T) then
4761 Dis := True;
4762 Typ := T;
4764 elsif Is_Private_Type (T)
4765 and then Present (Full_View (T))
4766 and then Has_Discriminants (Full_View (T))
4767 then
4768 Dis := True;
4769 Typ := Full_View (T);
4770 end if;
4772 if Dis then
4774 -- If the allocated object will be constrained by the
4775 -- default values for discriminants, then build a subtype
4776 -- with those defaults, and change the allocated subtype
4777 -- to that. Note that this happens in fewer cases in Ada
4778 -- 2005 (AI-363).
4780 if not Is_Constrained (Typ)
4781 and then Present (Discriminant_Default_Value
4782 (First_Discriminant (Typ)))
4783 and then (Ada_Version < Ada_2005
4784 or else not
4785 Object_Type_Has_Constrained_Partial_View
4786 (Typ, Current_Scope))
4787 then
4788 Typ := Build_Default_Subtype (Typ, N);
4789 Set_Expression (N, New_Occurrence_Of (Typ, Loc));
4790 end if;
4792 Discr := First_Elmt (Discriminant_Constraint (Typ));
4793 while Present (Discr) loop
4794 Nod := Node (Discr);
4795 Append (New_Copy_Tree (Node (Discr)), Args);
4797 -- AI-416: when the discriminant constraint is an
4798 -- anonymous access type make sure an accessibility
4799 -- check is inserted if necessary (3.10.2(22.q/2))
4801 if Ada_Version >= Ada_2005
4802 and then
4803 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
4804 then
4805 Apply_Accessibility_Check
4806 (Nod, Typ, Insert_Node => Nod);
4807 end if;
4809 Next_Elmt (Discr);
4810 end loop;
4811 end if;
4812 end;
4814 -- We set the allocator as analyzed so that when we analyze
4815 -- the if expression node, we do not get an unwanted recursive
4816 -- expansion of the allocator expression.
4818 Set_Analyzed (N, True);
4819 Nod := Relocate_Node (N);
4821 -- Here is the transformation:
4822 -- input: new Ctrl_Typ
4823 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4824 -- Ctrl_TypIP (Temp.all, ...);
4825 -- [Deep_]Initialize (Temp.all);
4827 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4828 -- is the subtype of the allocator.
4830 Temp_Decl :=
4831 Make_Object_Declaration (Loc,
4832 Defining_Identifier => Temp,
4833 Constant_Present => True,
4834 Object_Definition => New_Occurrence_Of (Temp_Type, Loc),
4835 Expression => Nod);
4837 Set_Assignment_OK (Temp_Decl);
4838 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
4840 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
4842 -- If the designated type is a task type or contains tasks,
4843 -- create block to activate created tasks, and insert
4844 -- declaration for Task_Image variable ahead of call.
4846 if Has_Task (T) then
4847 declare
4848 L : constant List_Id := New_List;
4849 Blk : Node_Id;
4850 begin
4851 Build_Task_Allocate_Block (L, Nod, Args);
4852 Blk := Last (L);
4853 Insert_List_Before (First (Declarations (Blk)), Decls);
4854 Insert_Actions (N, L);
4855 end;
4857 else
4858 Insert_Action (N,
4859 Make_Procedure_Call_Statement (Loc,
4860 Name => New_Occurrence_Of (Init, Loc),
4861 Parameter_Associations => Args));
4862 end if;
4864 if Needs_Finalization (T) then
4866 -- Generate:
4867 -- [Deep_]Initialize (Init_Arg1);
4869 Insert_Action (N,
4870 Make_Init_Call
4871 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4872 Typ => T));
4874 -- Special processing for .NET/JVM, the allocated object is
4875 -- attached to the finalization master. Generate:
4877 -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1));
4879 -- Types derived from [Limited_]Controlled are the only ones
4880 -- considered since they have fields Prev and Next.
4882 if VM_Target /= No_VM
4883 and then Is_Controlled (T)
4884 and then Present (Finalization_Master (PtrT))
4885 then
4886 Insert_Action (N,
4887 Make_Attach_Call
4888 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4889 Ptr_Typ => PtrT));
4890 end if;
4891 end if;
4893 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4894 Analyze_And_Resolve (N, PtrT);
4895 end if;
4896 end if;
4897 end;
4899 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
4900 -- object that has been rewritten as a reference, we displace "this"
4901 -- to reference properly its secondary dispatch table.
4903 if Nkind (N) = N_Identifier and then Is_Interface (Dtyp) then
4904 Displace_Allocator_Pointer (N);
4905 end if;
4907 exception
4908 when RE_Not_Available =>
4909 return;
4910 end Expand_N_Allocator;
4912 -----------------------
4913 -- Expand_N_And_Then --
4914 -----------------------
4916 procedure Expand_N_And_Then (N : Node_Id)
4917 renames Expand_Short_Circuit_Operator;
4919 ------------------------------
4920 -- Expand_N_Case_Expression --
4921 ------------------------------
4923 procedure Expand_N_Case_Expression (N : Node_Id) is
4924 Loc : constant Source_Ptr := Sloc (N);
4925 Typ : constant Entity_Id := Etype (N);
4926 Cstmt : Node_Id;
4927 Decl : Node_Id;
4928 Tnn : Entity_Id;
4929 Pnn : Entity_Id;
4930 Actions : List_Id;
4931 Ttyp : Entity_Id;
4932 Alt : Node_Id;
4933 Fexp : Node_Id;
4935 begin
4936 -- Check for MINIMIZED/ELIMINATED overflow mode
4938 if Minimized_Eliminated_Overflow_Check (N) then
4939 Apply_Arithmetic_Overflow_Check (N);
4940 return;
4941 end if;
4943 -- If the case expression is a predicate specification, do not
4944 -- expand, because it will be converted to the proper predicate
4945 -- form when building the predicate function.
4947 if Ekind_In (Current_Scope, E_Function, E_Procedure)
4948 and then Is_Predicate_Function (Current_Scope)
4949 then
4950 return;
4951 end if;
4953 -- We expand
4955 -- case X is when A => AX, when B => BX ...
4957 -- to
4959 -- do
4960 -- Tnn : typ;
4961 -- case X is
4962 -- when A =>
4963 -- Tnn := AX;
4964 -- when B =>
4965 -- Tnn := BX;
4966 -- ...
4967 -- end case;
4968 -- in Tnn end;
4970 -- However, this expansion is wrong for limited types, and also
4971 -- wrong for unconstrained types (since the bounds may not be the
4972 -- same in all branches). Furthermore it involves an extra copy
4973 -- for large objects. So we take care of this by using the following
4974 -- modified expansion for non-elementary types:
4976 -- do
4977 -- type Pnn is access all typ;
4978 -- Tnn : Pnn;
4979 -- case X is
4980 -- when A =>
4981 -- T := AX'Unrestricted_Access;
4982 -- when B =>
4983 -- T := BX'Unrestricted_Access;
4984 -- ...
4985 -- end case;
4986 -- in Tnn.all end;
4988 Cstmt :=
4989 Make_Case_Statement (Loc,
4990 Expression => Expression (N),
4991 Alternatives => New_List);
4993 -- Preserve the original context for which the case statement is being
4994 -- generated. This is needed by the finalization machinery to prevent
4995 -- the premature finalization of controlled objects found within the
4996 -- case statement.
4998 Set_From_Conditional_Expression (Cstmt);
5000 Actions := New_List;
5002 -- Scalar case
5004 if Is_Elementary_Type (Typ) then
5005 Ttyp := Typ;
5007 else
5008 Pnn := Make_Temporary (Loc, 'P');
5009 Append_To (Actions,
5010 Make_Full_Type_Declaration (Loc,
5011 Defining_Identifier => Pnn,
5012 Type_Definition =>
5013 Make_Access_To_Object_Definition (Loc,
5014 All_Present => True,
5015 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
5016 Ttyp := Pnn;
5017 end if;
5019 Tnn := Make_Temporary (Loc, 'T');
5021 -- Create declaration for target of expression, and indicate that it
5022 -- does not require initialization.
5024 Decl :=
5025 Make_Object_Declaration (Loc,
5026 Defining_Identifier => Tnn,
5027 Object_Definition => New_Occurrence_Of (Ttyp, Loc));
5028 Set_No_Initialization (Decl);
5029 Append_To (Actions, Decl);
5031 -- Now process the alternatives
5033 Alt := First (Alternatives (N));
5034 while Present (Alt) loop
5035 declare
5036 Aexp : Node_Id := Expression (Alt);
5037 Aloc : constant Source_Ptr := Sloc (Aexp);
5038 Stats : List_Id;
5040 begin
5041 -- As described above, take Unrestricted_Access for case of non-
5042 -- scalar types, to avoid big copies, and special cases.
5044 if not Is_Elementary_Type (Typ) then
5045 Aexp :=
5046 Make_Attribute_Reference (Aloc,
5047 Prefix => Relocate_Node (Aexp),
5048 Attribute_Name => Name_Unrestricted_Access);
5049 end if;
5051 Stats := New_List (
5052 Make_Assignment_Statement (Aloc,
5053 Name => New_Occurrence_Of (Tnn, Loc),
5054 Expression => Aexp));
5056 -- Propagate declarations inserted in the node by Insert_Actions
5057 -- (for example, temporaries generated to remove side effects).
5058 -- These actions must remain attached to the alternative, given
5059 -- that they are generated by the corresponding expression.
5061 if Present (Sinfo.Actions (Alt)) then
5062 Prepend_List (Sinfo.Actions (Alt), Stats);
5063 end if;
5065 Append_To
5066 (Alternatives (Cstmt),
5067 Make_Case_Statement_Alternative (Sloc (Alt),
5068 Discrete_Choices => Discrete_Choices (Alt),
5069 Statements => Stats));
5070 end;
5072 Next (Alt);
5073 end loop;
5075 Append_To (Actions, Cstmt);
5077 -- Construct and return final expression with actions
5079 if Is_Elementary_Type (Typ) then
5080 Fexp := New_Occurrence_Of (Tnn, Loc);
5081 else
5082 Fexp :=
5083 Make_Explicit_Dereference (Loc,
5084 Prefix => New_Occurrence_Of (Tnn, Loc));
5085 end if;
5087 Rewrite (N,
5088 Make_Expression_With_Actions (Loc,
5089 Expression => Fexp,
5090 Actions => Actions));
5092 Analyze_And_Resolve (N, Typ);
5093 end Expand_N_Case_Expression;
5095 -----------------------------------
5096 -- Expand_N_Explicit_Dereference --
5097 -----------------------------------
5099 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
5100 begin
5101 -- Insert explicit dereference call for the checked storage pool case
5103 Insert_Dereference_Action (Prefix (N));
5105 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5106 -- we set the atomic sync flag.
5108 if Is_Atomic (Etype (N))
5109 and then not Atomic_Synchronization_Disabled (Etype (N))
5110 then
5111 Activate_Atomic_Synchronization (N);
5112 end if;
5113 end Expand_N_Explicit_Dereference;
5115 --------------------------------------
5116 -- Expand_N_Expression_With_Actions --
5117 --------------------------------------
5119 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
5120 function Process_Action (Act : Node_Id) return Traverse_Result;
5121 -- Inspect and process a single action of an expression_with_actions for
5122 -- transient controlled objects. If such objects are found, the routine
5123 -- generates code to clean them up when the context of the expression is
5124 -- evaluated or elaborated.
5126 --------------------
5127 -- Process_Action --
5128 --------------------
5130 function Process_Action (Act : Node_Id) return Traverse_Result is
5131 begin
5132 if Nkind (Act) = N_Object_Declaration
5133 and then Is_Finalizable_Transient (Act, N)
5134 then
5135 Process_Transient_Object (Act, N);
5136 return Abandon;
5138 -- Avoid processing temporary function results multiple times when
5139 -- dealing with nested expression_with_actions.
5141 elsif Nkind (Act) = N_Expression_With_Actions then
5142 return Abandon;
5144 -- Do not process temporary function results in loops. This is done
5145 -- by Expand_N_Loop_Statement and Build_Finalizer.
5147 elsif Nkind (Act) = N_Loop_Statement then
5148 return Abandon;
5149 end if;
5151 return OK;
5152 end Process_Action;
5154 procedure Process_Single_Action is new Traverse_Proc (Process_Action);
5156 -- Local variables
5158 Acts : constant List_Id := Actions (N);
5159 Expr : constant Node_Id := Expression (N);
5160 Act : Node_Id;
5162 -- Start of processing for Expand_N_Expression_With_Actions
5164 begin
5165 -- Do not evaluate the expression when it denotes an entity because the
5166 -- expression_with_actions node will be replaced by the reference.
5168 if Is_Entity_Name (Expr) then
5169 null;
5171 -- Do not evaluate the expression when there are no actions because the
5172 -- expression_with_actions node will be replaced by the expression.
5174 elsif No (Acts) or else Is_Empty_List (Acts) then
5175 null;
5177 -- Force the evaluation of the expression by capturing its value in a
5178 -- temporary. This ensures that aliases of transient controlled objects
5179 -- do not leak to the expression of the expression_with_actions node:
5181 -- do
5182 -- Trans_Id : Ctrl_Typ : ...;
5183 -- Alias : ... := Trans_Id;
5184 -- in ... Alias ... end;
5186 -- In the example above, Trans_Id cannot be finalized at the end of the
5187 -- actions list because this may affect the alias and the final value of
5188 -- the expression_with_actions. Forcing the evaluation encapsulates the
5189 -- reference to the Alias within the actions list:
5191 -- do
5192 -- Trans_Id : Ctrl_Typ : ...;
5193 -- Alias : ... := Trans_Id;
5194 -- Val : constant Boolean := ... Alias ...;
5195 -- <finalize Trans_Id>
5196 -- in Val end;
5198 -- It is now safe to finalize the transient controlled object at the end
5199 -- of the actions list.
5201 else
5202 Force_Evaluation (Expr);
5203 end if;
5205 -- Process all transient controlled objects found within the actions of
5206 -- the EWA node.
5208 Act := First (Acts);
5209 while Present (Act) loop
5210 Process_Single_Action (Act);
5211 Next (Act);
5212 end loop;
5214 -- Deal with case where there are no actions. In this case we simply
5215 -- rewrite the node with its expression since we don't need the actions
5216 -- and the specification of this node does not allow a null action list.
5218 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5219 -- the expanded tree and relying on being able to retrieve the original
5220 -- tree in cases like this. This raises a whole lot of issues of whether
5221 -- we have problems elsewhere, which will be addressed in the future???
5223 if Is_Empty_List (Acts) then
5224 Rewrite (N, Relocate_Node (Expression (N)));
5225 end if;
5226 end Expand_N_Expression_With_Actions;
5228 ----------------------------
5229 -- Expand_N_If_Expression --
5230 ----------------------------
5232 -- Deal with limited types and condition actions
5234 procedure Expand_N_If_Expression (N : Node_Id) is
5235 procedure Process_Actions (Actions : List_Id);
5236 -- Inspect and process a single action list of an if expression for
5237 -- transient controlled objects. If such objects are found, the routine
5238 -- generates code to clean them up when the context of the expression is
5239 -- evaluated or elaborated.
5241 ---------------------
5242 -- Process_Actions --
5243 ---------------------
5245 procedure Process_Actions (Actions : List_Id) is
5246 Act : Node_Id;
5248 begin
5249 Act := First (Actions);
5250 while Present (Act) loop
5251 if Nkind (Act) = N_Object_Declaration
5252 and then Is_Finalizable_Transient (Act, N)
5253 then
5254 Process_Transient_Object (Act, N);
5255 end if;
5257 Next (Act);
5258 end loop;
5259 end Process_Actions;
5261 -- Local variables
5263 Loc : constant Source_Ptr := Sloc (N);
5264 Cond : constant Node_Id := First (Expressions (N));
5265 Thenx : constant Node_Id := Next (Cond);
5266 Elsex : constant Node_Id := Next (Thenx);
5267 Typ : constant Entity_Id := Etype (N);
5269 Actions : List_Id;
5270 Cnn : Entity_Id;
5271 Decl : Node_Id;
5272 Expr : Node_Id;
5273 New_If : Node_Id;
5274 New_N : Node_Id;
5275 Ptr_Typ : Entity_Id;
5277 -- Start of processing for Expand_N_If_Expression
5279 begin
5280 -- Check for MINIMIZED/ELIMINATED overflow mode
5282 if Minimized_Eliminated_Overflow_Check (N) then
5283 Apply_Arithmetic_Overflow_Check (N);
5284 return;
5285 end if;
5287 -- Fold at compile time if condition known. We have already folded
5288 -- static if expressions, but it is possible to fold any case in which
5289 -- the condition is known at compile time, even though the result is
5290 -- non-static.
5292 -- Note that we don't do the fold of such cases in Sem_Elab because
5293 -- it can cause infinite loops with the expander adding a conditional
5294 -- expression, and Sem_Elab circuitry removing it repeatedly.
5296 if Compile_Time_Known_Value (Cond) then
5297 if Is_True (Expr_Value (Cond)) then
5298 Expr := Thenx;
5299 Actions := Then_Actions (N);
5300 else
5301 Expr := Elsex;
5302 Actions := Else_Actions (N);
5303 end if;
5305 Remove (Expr);
5307 if Present (Actions) then
5308 Rewrite (N,
5309 Make_Expression_With_Actions (Loc,
5310 Expression => Relocate_Node (Expr),
5311 Actions => Actions));
5312 Analyze_And_Resolve (N, Typ);
5313 else
5314 Rewrite (N, Relocate_Node (Expr));
5315 end if;
5317 -- Note that the result is never static (legitimate cases of static
5318 -- if expressions were folded in Sem_Eval).
5320 Set_Is_Static_Expression (N, False);
5321 return;
5322 end if;
5324 -- If the type is limited, and the back end does not handle limited
5325 -- types, then we expand as follows to avoid the possibility of
5326 -- improper copying.
5328 -- type Ptr is access all Typ;
5329 -- Cnn : Ptr;
5330 -- if cond then
5331 -- <<then actions>>
5332 -- Cnn := then-expr'Unrestricted_Access;
5333 -- else
5334 -- <<else actions>>
5335 -- Cnn := else-expr'Unrestricted_Access;
5336 -- end if;
5338 -- and replace the if expression by a reference to Cnn.all.
5340 -- This special case can be skipped if the back end handles limited
5341 -- types properly and ensures that no incorrect copies are made.
5343 if Is_By_Reference_Type (Typ)
5344 and then not Back_End_Handles_Limited_Types
5345 then
5346 -- When the "then" or "else" expressions involve controlled function
5347 -- calls, generated temporaries are chained on the corresponding list
5348 -- of actions. These temporaries need to be finalized after the if
5349 -- expression is evaluated.
5351 Process_Actions (Then_Actions (N));
5352 Process_Actions (Else_Actions (N));
5354 -- Generate:
5355 -- type Ann is access all Typ;
5357 Ptr_Typ := Make_Temporary (Loc, 'A');
5359 Insert_Action (N,
5360 Make_Full_Type_Declaration (Loc,
5361 Defining_Identifier => Ptr_Typ,
5362 Type_Definition =>
5363 Make_Access_To_Object_Definition (Loc,
5364 All_Present => True,
5365 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
5367 -- Generate:
5368 -- Cnn : Ann;
5370 Cnn := Make_Temporary (Loc, 'C', N);
5372 Decl :=
5373 Make_Object_Declaration (Loc,
5374 Defining_Identifier => Cnn,
5375 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc));
5377 -- Generate:
5378 -- if Cond then
5379 -- Cnn := <Thenx>'Unrestricted_Access;
5380 -- else
5381 -- Cnn := <Elsex>'Unrestricted_Access;
5382 -- end if;
5384 New_If :=
5385 Make_Implicit_If_Statement (N,
5386 Condition => Relocate_Node (Cond),
5387 Then_Statements => New_List (
5388 Make_Assignment_Statement (Sloc (Thenx),
5389 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
5390 Expression =>
5391 Make_Attribute_Reference (Loc,
5392 Prefix => Relocate_Node (Thenx),
5393 Attribute_Name => Name_Unrestricted_Access))),
5395 Else_Statements => New_List (
5396 Make_Assignment_Statement (Sloc (Elsex),
5397 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
5398 Expression =>
5399 Make_Attribute_Reference (Loc,
5400 Prefix => Relocate_Node (Elsex),
5401 Attribute_Name => Name_Unrestricted_Access))));
5403 -- Preserve the original context for which the if statement is being
5404 -- generated. This is needed by the finalization machinery to prevent
5405 -- the premature finalization of controlled objects found within the
5406 -- if statement.
5408 Set_From_Conditional_Expression (New_If);
5410 New_N :=
5411 Make_Explicit_Dereference (Loc,
5412 Prefix => New_Occurrence_Of (Cnn, Loc));
5414 -- If the result is an unconstrained array and the if expression is in a
5415 -- context other than the initializing expression of the declaration of
5416 -- an object, then we pull out the if expression as follows:
5418 -- Cnn : constant typ := if-expression
5420 -- and then replace the if expression with an occurrence of Cnn. This
5421 -- avoids the need in the back end to create on-the-fly variable length
5422 -- temporaries (which it cannot do!)
5424 -- Note that the test for being in an object declaration avoids doing an
5425 -- unnecessary expansion, and also avoids infinite recursion.
5427 elsif Is_Array_Type (Typ) and then not Is_Constrained (Typ)
5428 and then (Nkind (Parent (N)) /= N_Object_Declaration
5429 or else Expression (Parent (N)) /= N)
5430 then
5431 declare
5432 Cnn : constant Node_Id := Make_Temporary (Loc, 'C', N);
5433 begin
5434 Insert_Action (N,
5435 Make_Object_Declaration (Loc,
5436 Defining_Identifier => Cnn,
5437 Constant_Present => True,
5438 Object_Definition => New_Occurrence_Of (Typ, Loc),
5439 Expression => Relocate_Node (N),
5440 Has_Init_Expression => True));
5442 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
5443 return;
5444 end;
5446 -- For other types, we only need to expand if there are other actions
5447 -- associated with either branch.
5449 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
5451 -- We now wrap the actions into the appropriate expression
5453 if Present (Then_Actions (N)) then
5454 Rewrite (Thenx,
5455 Make_Expression_With_Actions (Sloc (Thenx),
5456 Actions => Then_Actions (N),
5457 Expression => Relocate_Node (Thenx)));
5459 Set_Then_Actions (N, No_List);
5460 Analyze_And_Resolve (Thenx, Typ);
5461 end if;
5463 if Present (Else_Actions (N)) then
5464 Rewrite (Elsex,
5465 Make_Expression_With_Actions (Sloc (Elsex),
5466 Actions => Else_Actions (N),
5467 Expression => Relocate_Node (Elsex)));
5469 Set_Else_Actions (N, No_List);
5470 Analyze_And_Resolve (Elsex, Typ);
5471 end if;
5473 return;
5475 -- If no actions then no expansion needed, gigi will handle it using the
5476 -- same approach as a C conditional expression.
5478 else
5479 return;
5480 end if;
5482 -- Fall through here for either the limited expansion, or the case of
5483 -- inserting actions for non-limited types. In both these cases, we must
5484 -- move the SLOC of the parent If statement to the newly created one and
5485 -- change it to the SLOC of the expression which, after expansion, will
5486 -- correspond to what is being evaluated.
5488 if Present (Parent (N)) and then Nkind (Parent (N)) = N_If_Statement then
5489 Set_Sloc (New_If, Sloc (Parent (N)));
5490 Set_Sloc (Parent (N), Loc);
5491 end if;
5493 -- Make sure Then_Actions and Else_Actions are appropriately moved
5494 -- to the new if statement.
5496 if Present (Then_Actions (N)) then
5497 Insert_List_Before
5498 (First (Then_Statements (New_If)), Then_Actions (N));
5499 end if;
5501 if Present (Else_Actions (N)) then
5502 Insert_List_Before
5503 (First (Else_Statements (New_If)), Else_Actions (N));
5504 end if;
5506 Insert_Action (N, Decl);
5507 Insert_Action (N, New_If);
5508 Rewrite (N, New_N);
5509 Analyze_And_Resolve (N, Typ);
5510 end Expand_N_If_Expression;
5512 -----------------
5513 -- Expand_N_In --
5514 -----------------
5516 procedure Expand_N_In (N : Node_Id) is
5517 Loc : constant Source_Ptr := Sloc (N);
5518 Restyp : constant Entity_Id := Etype (N);
5519 Lop : constant Node_Id := Left_Opnd (N);
5520 Rop : constant Node_Id := Right_Opnd (N);
5521 Static : constant Boolean := Is_OK_Static_Expression (N);
5523 Ltyp : Entity_Id;
5524 Rtyp : Entity_Id;
5526 procedure Substitute_Valid_Check;
5527 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5528 -- test for the left operand being in range of its subtype.
5530 ----------------------------
5531 -- Substitute_Valid_Check --
5532 ----------------------------
5534 procedure Substitute_Valid_Check is
5535 begin
5536 Rewrite (N,
5537 Make_Attribute_Reference (Loc,
5538 Prefix => Relocate_Node (Lop),
5539 Attribute_Name => Name_Valid));
5541 Analyze_And_Resolve (N, Restyp);
5543 -- Give warning unless overflow checking is MINIMIZED or ELIMINATED,
5544 -- in which case, this usage makes sense, and in any case, we have
5545 -- actually eliminated the danger of optimization above.
5547 if Overflow_Check_Mode not in Minimized_Or_Eliminated then
5548 Error_Msg_N
5549 ("??explicit membership test may be optimized away", N);
5550 Error_Msg_N -- CODEFIX
5551 ("\??use ''Valid attribute instead", N);
5552 end if;
5554 return;
5555 end Substitute_Valid_Check;
5557 -- Start of processing for Expand_N_In
5559 begin
5560 -- If set membership case, expand with separate procedure
5562 if Present (Alternatives (N)) then
5563 Expand_Set_Membership (N);
5564 return;
5565 end if;
5567 -- Not set membership, proceed with expansion
5569 Ltyp := Etype (Left_Opnd (N));
5570 Rtyp := Etype (Right_Opnd (N));
5572 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
5573 -- type, then expand with a separate procedure. Note the use of the
5574 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5576 if Overflow_Check_Mode in Minimized_Or_Eliminated
5577 and then Is_Signed_Integer_Type (Ltyp)
5578 and then not No_Minimize_Eliminate (N)
5579 then
5580 Expand_Membership_Minimize_Eliminate_Overflow (N);
5581 return;
5582 end if;
5584 -- Check case of explicit test for an expression in range of its
5585 -- subtype. This is suspicious usage and we replace it with a 'Valid
5586 -- test and give a warning for scalar types.
5588 if Is_Scalar_Type (Ltyp)
5590 -- Only relevant for source comparisons
5592 and then Comes_From_Source (N)
5594 -- In floating-point this is a standard way to check for finite values
5595 -- and using 'Valid would typically be a pessimization.
5597 and then not Is_Floating_Point_Type (Ltyp)
5599 -- Don't give the message unless right operand is a type entity and
5600 -- the type of the left operand matches this type. Note that this
5601 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
5602 -- checks have changed the type of the left operand.
5604 and then Nkind (Rop) in N_Has_Entity
5605 and then Ltyp = Entity (Rop)
5607 -- Skip in VM mode, where we have no sense of invalid values. The
5608 -- warning still seems relevant, but not important enough to worry.
5610 and then VM_Target = No_VM
5612 -- Skip this for predicated types, where such expressions are a
5613 -- reasonable way of testing if something meets the predicate.
5615 and then not Present (Predicate_Function (Ltyp))
5616 then
5617 Substitute_Valid_Check;
5618 return;
5619 end if;
5621 -- Do validity check on operands
5623 if Validity_Checks_On and Validity_Check_Operands then
5624 Ensure_Valid (Left_Opnd (N));
5625 Validity_Check_Range (Right_Opnd (N));
5626 end if;
5628 -- Case of explicit range
5630 if Nkind (Rop) = N_Range then
5631 declare
5632 Lo : constant Node_Id := Low_Bound (Rop);
5633 Hi : constant Node_Id := High_Bound (Rop);
5635 Lo_Orig : constant Node_Id := Original_Node (Lo);
5636 Hi_Orig : constant Node_Id := Original_Node (Hi);
5638 Lcheck : Compare_Result;
5639 Ucheck : Compare_Result;
5641 Warn1 : constant Boolean :=
5642 Constant_Condition_Warnings
5643 and then Comes_From_Source (N)
5644 and then not In_Instance;
5645 -- This must be true for any of the optimization warnings, we
5646 -- clearly want to give them only for source with the flag on. We
5647 -- also skip these warnings in an instance since it may be the
5648 -- case that different instantiations have different ranges.
5650 Warn2 : constant Boolean :=
5651 Warn1
5652 and then Nkind (Original_Node (Rop)) = N_Range
5653 and then Is_Integer_Type (Etype (Lo));
5654 -- For the case where only one bound warning is elided, we also
5655 -- insist on an explicit range and an integer type. The reason is
5656 -- that the use of enumeration ranges including an end point is
5657 -- common, as is the use of a subtype name, one of whose bounds is
5658 -- the same as the type of the expression.
5660 begin
5661 -- If test is explicit x'First .. x'Last, replace by valid check
5663 -- Could use some individual comments for this complex test ???
5665 if Is_Scalar_Type (Ltyp)
5667 -- And left operand is X'First where X matches left operand
5668 -- type (this eliminates cases of type mismatch, including
5669 -- the cases where ELIMINATED/MINIMIZED mode has changed the
5670 -- type of the left operand.
5672 and then Nkind (Lo_Orig) = N_Attribute_Reference
5673 and then Attribute_Name (Lo_Orig) = Name_First
5674 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
5675 and then Entity (Prefix (Lo_Orig)) = Ltyp
5677 -- Same tests for right operand
5679 and then Nkind (Hi_Orig) = N_Attribute_Reference
5680 and then Attribute_Name (Hi_Orig) = Name_Last
5681 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
5682 and then Entity (Prefix (Hi_Orig)) = Ltyp
5684 -- Relevant only for source cases
5686 and then Comes_From_Source (N)
5688 -- Omit for VM cases, where we don't have invalid values
5690 and then VM_Target = No_VM
5691 then
5692 Substitute_Valid_Check;
5693 goto Leave;
5694 end if;
5696 -- If bounds of type are known at compile time, and the end points
5697 -- are known at compile time and identical, this is another case
5698 -- for substituting a valid test. We only do this for discrete
5699 -- types, since it won't arise in practice for float types.
5701 if Comes_From_Source (N)
5702 and then Is_Discrete_Type (Ltyp)
5703 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
5704 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
5705 and then Compile_Time_Known_Value (Lo)
5706 and then Compile_Time_Known_Value (Hi)
5707 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
5708 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
5710 -- Kill warnings in instances, since they may be cases where we
5711 -- have a test in the generic that makes sense with some types
5712 -- and not with other types.
5714 and then not In_Instance
5715 then
5716 Substitute_Valid_Check;
5717 goto Leave;
5718 end if;
5720 -- If we have an explicit range, do a bit of optimization based on
5721 -- range analysis (we may be able to kill one or both checks).
5723 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
5724 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
5726 -- If either check is known to fail, replace result by False since
5727 -- the other check does not matter. Preserve the static flag for
5728 -- legality checks, because we are constant-folding beyond RM 4.9.
5730 if Lcheck = LT or else Ucheck = GT then
5731 if Warn1 then
5732 Error_Msg_N ("?c?range test optimized away", N);
5733 Error_Msg_N ("\?c?value is known to be out of range", N);
5734 end if;
5736 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5737 Analyze_And_Resolve (N, Restyp);
5738 Set_Is_Static_Expression (N, Static);
5739 goto Leave;
5741 -- If both checks are known to succeed, replace result by True,
5742 -- since we know we are in range.
5744 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
5745 if Warn1 then
5746 Error_Msg_N ("?c?range test optimized away", N);
5747 Error_Msg_N ("\?c?value is known to be in range", N);
5748 end if;
5750 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5751 Analyze_And_Resolve (N, Restyp);
5752 Set_Is_Static_Expression (N, Static);
5753 goto Leave;
5755 -- If lower bound check succeeds and upper bound check is not
5756 -- known to succeed or fail, then replace the range check with
5757 -- a comparison against the upper bound.
5759 elsif Lcheck in Compare_GE then
5760 if Warn2 and then not In_Instance then
5761 Error_Msg_N ("??lower bound test optimized away", Lo);
5762 Error_Msg_N ("\??value is known to be in range", Lo);
5763 end if;
5765 Rewrite (N,
5766 Make_Op_Le (Loc,
5767 Left_Opnd => Lop,
5768 Right_Opnd => High_Bound (Rop)));
5769 Analyze_And_Resolve (N, Restyp);
5770 goto Leave;
5772 -- If upper bound check succeeds and lower bound check is not
5773 -- known to succeed or fail, then replace the range check with
5774 -- a comparison against the lower bound.
5776 elsif Ucheck in Compare_LE then
5777 if Warn2 and then not In_Instance then
5778 Error_Msg_N ("??upper bound test optimized away", Hi);
5779 Error_Msg_N ("\??value is known to be in range", Hi);
5780 end if;
5782 Rewrite (N,
5783 Make_Op_Ge (Loc,
5784 Left_Opnd => Lop,
5785 Right_Opnd => Low_Bound (Rop)));
5786 Analyze_And_Resolve (N, Restyp);
5787 goto Leave;
5788 end if;
5790 -- We couldn't optimize away the range check, but there is one
5791 -- more issue. If we are checking constant conditionals, then we
5792 -- see if we can determine the outcome assuming everything is
5793 -- valid, and if so give an appropriate warning.
5795 if Warn1 and then not Assume_No_Invalid_Values then
5796 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
5797 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
5799 -- Result is out of range for valid value
5801 if Lcheck = LT or else Ucheck = GT then
5802 Error_Msg_N
5803 ("?c?value can only be in range if it is invalid", N);
5805 -- Result is in range for valid value
5807 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
5808 Error_Msg_N
5809 ("?c?value can only be out of range if it is invalid", N);
5811 -- Lower bound check succeeds if value is valid
5813 elsif Warn2 and then Lcheck in Compare_GE then
5814 Error_Msg_N
5815 ("?c?lower bound check only fails if it is invalid", Lo);
5817 -- Upper bound check succeeds if value is valid
5819 elsif Warn2 and then Ucheck in Compare_LE then
5820 Error_Msg_N
5821 ("?c?upper bound check only fails for invalid values", Hi);
5822 end if;
5823 end if;
5824 end;
5826 -- For all other cases of an explicit range, nothing to be done
5828 goto Leave;
5830 -- Here right operand is a subtype mark
5832 else
5833 declare
5834 Typ : Entity_Id := Etype (Rop);
5835 Is_Acc : constant Boolean := Is_Access_Type (Typ);
5836 Cond : Node_Id := Empty;
5837 New_N : Node_Id;
5838 Obj : Node_Id := Lop;
5839 SCIL_Node : Node_Id;
5841 begin
5842 Remove_Side_Effects (Obj);
5844 -- For tagged type, do tagged membership operation
5846 if Is_Tagged_Type (Typ) then
5848 -- No expansion will be performed when VM_Target, as the VM
5849 -- back-ends will handle the membership tests directly (tags
5850 -- are not explicitly represented in Java objects, so the
5851 -- normal tagged membership expansion is not what we want).
5853 if Tagged_Type_Expansion then
5854 Tagged_Membership (N, SCIL_Node, New_N);
5855 Rewrite (N, New_N);
5856 Analyze_And_Resolve (N, Restyp);
5858 -- Update decoration of relocated node referenced by the
5859 -- SCIL node.
5861 if Generate_SCIL and then Present (SCIL_Node) then
5862 Set_SCIL_Node (N, SCIL_Node);
5863 end if;
5864 end if;
5866 goto Leave;
5868 -- If type is scalar type, rewrite as x in t'First .. t'Last.
5869 -- This reason we do this is that the bounds may have the wrong
5870 -- type if they come from the original type definition. Also this
5871 -- way we get all the processing above for an explicit range.
5873 -- Don't do this for predicated types, since in this case we
5874 -- want to check the predicate.
5876 elsif Is_Scalar_Type (Typ) then
5877 if No (Predicate_Function (Typ)) then
5878 Rewrite (Rop,
5879 Make_Range (Loc,
5880 Low_Bound =>
5881 Make_Attribute_Reference (Loc,
5882 Attribute_Name => Name_First,
5883 Prefix => New_Occurrence_Of (Typ, Loc)),
5885 High_Bound =>
5886 Make_Attribute_Reference (Loc,
5887 Attribute_Name => Name_Last,
5888 Prefix => New_Occurrence_Of (Typ, Loc))));
5889 Analyze_And_Resolve (N, Restyp);
5890 end if;
5892 goto Leave;
5894 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5895 -- a membership test if the subtype mark denotes a constrained
5896 -- Unchecked_Union subtype and the expression lacks inferable
5897 -- discriminants.
5899 elsif Is_Unchecked_Union (Base_Type (Typ))
5900 and then Is_Constrained (Typ)
5901 and then not Has_Inferable_Discriminants (Lop)
5902 then
5903 Insert_Action (N,
5904 Make_Raise_Program_Error (Loc,
5905 Reason => PE_Unchecked_Union_Restriction));
5907 -- Prevent Gigi from generating incorrect code by rewriting the
5908 -- test as False. What is this undocumented thing about ???
5910 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5911 goto Leave;
5912 end if;
5914 -- Here we have a non-scalar type
5916 if Is_Acc then
5917 Typ := Designated_Type (Typ);
5918 end if;
5920 if not Is_Constrained (Typ) then
5921 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5922 Analyze_And_Resolve (N, Restyp);
5924 -- For the constrained array case, we have to check the subscripts
5925 -- for an exact match if the lengths are non-zero (the lengths
5926 -- must match in any case).
5928 elsif Is_Array_Type (Typ) then
5929 Check_Subscripts : declare
5930 function Build_Attribute_Reference
5931 (E : Node_Id;
5932 Nam : Name_Id;
5933 Dim : Nat) return Node_Id;
5934 -- Build attribute reference E'Nam (Dim)
5936 -------------------------------
5937 -- Build_Attribute_Reference --
5938 -------------------------------
5940 function Build_Attribute_Reference
5941 (E : Node_Id;
5942 Nam : Name_Id;
5943 Dim : Nat) return Node_Id
5945 begin
5946 return
5947 Make_Attribute_Reference (Loc,
5948 Prefix => E,
5949 Attribute_Name => Nam,
5950 Expressions => New_List (
5951 Make_Integer_Literal (Loc, Dim)));
5952 end Build_Attribute_Reference;
5954 -- Start of processing for Check_Subscripts
5956 begin
5957 for J in 1 .. Number_Dimensions (Typ) loop
5958 Evolve_And_Then (Cond,
5959 Make_Op_Eq (Loc,
5960 Left_Opnd =>
5961 Build_Attribute_Reference
5962 (Duplicate_Subexpr_No_Checks (Obj),
5963 Name_First, J),
5964 Right_Opnd =>
5965 Build_Attribute_Reference
5966 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
5968 Evolve_And_Then (Cond,
5969 Make_Op_Eq (Loc,
5970 Left_Opnd =>
5971 Build_Attribute_Reference
5972 (Duplicate_Subexpr_No_Checks (Obj),
5973 Name_Last, J),
5974 Right_Opnd =>
5975 Build_Attribute_Reference
5976 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
5977 end loop;
5979 if Is_Acc then
5980 Cond :=
5981 Make_Or_Else (Loc,
5982 Left_Opnd =>
5983 Make_Op_Eq (Loc,
5984 Left_Opnd => Obj,
5985 Right_Opnd => Make_Null (Loc)),
5986 Right_Opnd => Cond);
5987 end if;
5989 Rewrite (N, Cond);
5990 Analyze_And_Resolve (N, Restyp);
5991 end Check_Subscripts;
5993 -- These are the cases where constraint checks may be required,
5994 -- e.g. records with possible discriminants
5996 else
5997 -- Expand the test into a series of discriminant comparisons.
5998 -- The expression that is built is the negation of the one that
5999 -- is used for checking discriminant constraints.
6001 Obj := Relocate_Node (Left_Opnd (N));
6003 if Has_Discriminants (Typ) then
6004 Cond := Make_Op_Not (Loc,
6005 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
6007 if Is_Acc then
6008 Cond := Make_Or_Else (Loc,
6009 Left_Opnd =>
6010 Make_Op_Eq (Loc,
6011 Left_Opnd => Obj,
6012 Right_Opnd => Make_Null (Loc)),
6013 Right_Opnd => Cond);
6014 end if;
6016 else
6017 Cond := New_Occurrence_Of (Standard_True, Loc);
6018 end if;
6020 Rewrite (N, Cond);
6021 Analyze_And_Resolve (N, Restyp);
6022 end if;
6024 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
6025 -- expression of an anonymous access type. This can involve an
6026 -- accessibility test and a tagged type membership test in the
6027 -- case of tagged designated types.
6029 if Ada_Version >= Ada_2012
6030 and then Is_Acc
6031 and then Ekind (Ltyp) = E_Anonymous_Access_Type
6032 then
6033 declare
6034 Expr_Entity : Entity_Id := Empty;
6035 New_N : Node_Id;
6036 Param_Level : Node_Id;
6037 Type_Level : Node_Id;
6039 begin
6040 if Is_Entity_Name (Lop) then
6041 Expr_Entity := Param_Entity (Lop);
6043 if not Present (Expr_Entity) then
6044 Expr_Entity := Entity (Lop);
6045 end if;
6046 end if;
6048 -- If a conversion of the anonymous access value to the
6049 -- tested type would be illegal, then the result is False.
6051 if not Valid_Conversion
6052 (Lop, Rtyp, Lop, Report_Errs => False)
6053 then
6054 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
6055 Analyze_And_Resolve (N, Restyp);
6057 -- Apply an accessibility check if the access object has an
6058 -- associated access level and when the level of the type is
6059 -- less deep than the level of the access parameter. This
6060 -- only occur for access parameters and stand-alone objects
6061 -- of an anonymous access type.
6063 else
6064 if Present (Expr_Entity)
6065 and then
6066 Present
6067 (Effective_Extra_Accessibility (Expr_Entity))
6068 and then UI_Gt (Object_Access_Level (Lop),
6069 Type_Access_Level (Rtyp))
6070 then
6071 Param_Level :=
6072 New_Occurrence_Of
6073 (Effective_Extra_Accessibility (Expr_Entity), Loc);
6075 Type_Level :=
6076 Make_Integer_Literal (Loc, Type_Access_Level (Rtyp));
6078 -- Return True only if the accessibility level of the
6079 -- expression entity is not deeper than the level of
6080 -- the tested access type.
6082 Rewrite (N,
6083 Make_And_Then (Loc,
6084 Left_Opnd => Relocate_Node (N),
6085 Right_Opnd => Make_Op_Le (Loc,
6086 Left_Opnd => Param_Level,
6087 Right_Opnd => Type_Level)));
6089 Analyze_And_Resolve (N);
6090 end if;
6092 -- If the designated type is tagged, do tagged membership
6093 -- operation.
6095 -- *** NOTE: we have to check not null before doing the
6096 -- tagged membership test (but maybe that can be done
6097 -- inside Tagged_Membership?).
6099 if Is_Tagged_Type (Typ) then
6100 Rewrite (N,
6101 Make_And_Then (Loc,
6102 Left_Opnd => Relocate_Node (N),
6103 Right_Opnd =>
6104 Make_Op_Ne (Loc,
6105 Left_Opnd => Obj,
6106 Right_Opnd => Make_Null (Loc))));
6108 -- No expansion will be performed when VM_Target, as
6109 -- the VM back-ends will handle the membership tests
6110 -- directly (tags are not explicitly represented in
6111 -- Java objects, so the normal tagged membership
6112 -- expansion is not what we want).
6114 if Tagged_Type_Expansion then
6116 -- Note that we have to pass Original_Node, because
6117 -- the membership test might already have been
6118 -- rewritten by earlier parts of membership test.
6120 Tagged_Membership
6121 (Original_Node (N), SCIL_Node, New_N);
6123 -- Update decoration of relocated node referenced
6124 -- by the SCIL node.
6126 if Generate_SCIL and then Present (SCIL_Node) then
6127 Set_SCIL_Node (New_N, SCIL_Node);
6128 end if;
6130 Rewrite (N,
6131 Make_And_Then (Loc,
6132 Left_Opnd => Relocate_Node (N),
6133 Right_Opnd => New_N));
6135 Analyze_And_Resolve (N, Restyp);
6136 end if;
6137 end if;
6138 end if;
6139 end;
6140 end if;
6141 end;
6142 end if;
6144 -- At this point, we have done the processing required for the basic
6145 -- membership test, but not yet dealt with the predicate.
6147 <<Leave>>
6149 -- If a predicate is present, then we do the predicate test, but we
6150 -- most certainly want to omit this if we are within the predicate
6151 -- function itself, since otherwise we have an infinite recursion.
6152 -- The check should also not be emitted when testing against a range
6153 -- (the check is only done when the right operand is a subtype; see
6154 -- RM12-4.5.2 (28.1/3-30/3)).
6156 declare
6157 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
6159 begin
6160 if Present (PFunc)
6161 and then Current_Scope /= PFunc
6162 and then Nkind (Rop) /= N_Range
6163 then
6164 Rewrite (N,
6165 Make_And_Then (Loc,
6166 Left_Opnd => Relocate_Node (N),
6167 Right_Opnd => Make_Predicate_Call (Rtyp, Lop, Mem => True)));
6169 -- Analyze new expression, mark left operand as analyzed to
6170 -- avoid infinite recursion adding predicate calls. Similarly,
6171 -- suppress further range checks on the call.
6173 Set_Analyzed (Left_Opnd (N));
6174 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6176 -- All done, skip attempt at compile time determination of result
6178 return;
6179 end if;
6180 end;
6181 end Expand_N_In;
6183 --------------------------------
6184 -- Expand_N_Indexed_Component --
6185 --------------------------------
6187 procedure Expand_N_Indexed_Component (N : Node_Id) is
6188 Loc : constant Source_Ptr := Sloc (N);
6189 Typ : constant Entity_Id := Etype (N);
6190 P : constant Node_Id := Prefix (N);
6191 T : constant Entity_Id := Etype (P);
6192 Atp : Entity_Id;
6194 begin
6195 -- A special optimization, if we have an indexed component that is
6196 -- selecting from a slice, then we can eliminate the slice, since, for
6197 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6198 -- the range check required by the slice. The range check for the slice
6199 -- itself has already been generated. The range check for the
6200 -- subscripting operation is ensured by converting the subject to
6201 -- the subtype of the slice.
6203 -- This optimization not only generates better code, avoiding slice
6204 -- messing especially in the packed case, but more importantly bypasses
6205 -- some problems in handling this peculiar case, for example, the issue
6206 -- of dealing specially with object renamings.
6208 if Nkind (P) = N_Slice
6210 -- This optimization is disabled for CodePeer because it can transform
6211 -- an index-check constraint_error into a range-check constraint_error
6212 -- and CodePeer cares about that distinction.
6214 and then not CodePeer_Mode
6215 then
6216 Rewrite (N,
6217 Make_Indexed_Component (Loc,
6218 Prefix => Prefix (P),
6219 Expressions => New_List (
6220 Convert_To
6221 (Etype (First_Index (Etype (P))),
6222 First (Expressions (N))))));
6223 Analyze_And_Resolve (N, Typ);
6224 return;
6225 end if;
6227 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6228 -- function, then additional actuals must be passed.
6230 if Ada_Version >= Ada_2005
6231 and then Is_Build_In_Place_Function_Call (P)
6232 then
6233 Make_Build_In_Place_Call_In_Anonymous_Context (P);
6234 end if;
6236 -- If the prefix is an access type, then we unconditionally rewrite if
6237 -- as an explicit dereference. This simplifies processing for several
6238 -- cases, including packed array cases and certain cases in which checks
6239 -- must be generated. We used to try to do this only when it was
6240 -- necessary, but it cleans up the code to do it all the time.
6242 if Is_Access_Type (T) then
6243 Insert_Explicit_Dereference (P);
6244 Analyze_And_Resolve (P, Designated_Type (T));
6245 Atp := Designated_Type (T);
6246 else
6247 Atp := T;
6248 end if;
6250 -- Generate index and validity checks
6252 Generate_Index_Checks (N);
6254 if Validity_Checks_On and then Validity_Check_Subscripts then
6255 Apply_Subscript_Validity_Checks (N);
6256 end if;
6258 -- If selecting from an array with atomic components, and atomic sync
6259 -- is not suppressed for this array type, set atomic sync flag.
6261 if (Has_Atomic_Components (Atp)
6262 and then not Atomic_Synchronization_Disabled (Atp))
6263 or else (Is_Atomic (Typ)
6264 and then not Atomic_Synchronization_Disabled (Typ))
6265 then
6266 Activate_Atomic_Synchronization (N);
6267 end if;
6269 -- All done for the non-packed case
6271 if not Is_Packed (Etype (Prefix (N))) then
6272 return;
6273 end if;
6275 -- For packed arrays that are not bit-packed (i.e. the case of an array
6276 -- with one or more index types with a non-contiguous enumeration type),
6277 -- we can always use the normal packed element get circuit.
6279 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
6280 Expand_Packed_Element_Reference (N);
6281 return;
6282 end if;
6284 -- For a reference to a component of a bit packed array, we convert it
6285 -- to a reference to the corresponding Packed_Array_Impl_Type. We only
6286 -- want to do this for simple references, and not for:
6288 -- Left side of assignment, or prefix of left side of assignment, or
6289 -- prefix of the prefix, to handle packed arrays of packed arrays,
6290 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6292 -- Renaming objects in renaming associations
6293 -- This case is handled when a use of the renamed variable occurs
6295 -- Actual parameters for a procedure call
6296 -- This case is handled in Exp_Ch6.Expand_Actuals
6298 -- The second expression in a 'Read attribute reference
6300 -- The prefix of an address or bit or size attribute reference
6302 -- The following circuit detects these exceptions
6304 declare
6305 Child : Node_Id := N;
6306 Parnt : Node_Id := Parent (N);
6308 begin
6309 loop
6310 if Nkind (Parnt) = N_Unchecked_Expression then
6311 null;
6313 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
6314 N_Procedure_Call_Statement)
6315 or else (Nkind (Parnt) = N_Parameter_Association
6316 and then
6317 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
6318 then
6319 return;
6321 elsif Nkind (Parnt) = N_Attribute_Reference
6322 and then Nam_In (Attribute_Name (Parnt), Name_Address,
6323 Name_Bit,
6324 Name_Size)
6325 and then Prefix (Parnt) = Child
6326 then
6327 return;
6329 elsif Nkind (Parnt) = N_Assignment_Statement
6330 and then Name (Parnt) = Child
6331 then
6332 return;
6334 -- If the expression is an index of an indexed component, it must
6335 -- be expanded regardless of context.
6337 elsif Nkind (Parnt) = N_Indexed_Component
6338 and then Child /= Prefix (Parnt)
6339 then
6340 Expand_Packed_Element_Reference (N);
6341 return;
6343 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
6344 and then Name (Parent (Parnt)) = Parnt
6345 then
6346 return;
6348 elsif Nkind (Parnt) = N_Attribute_Reference
6349 and then Attribute_Name (Parnt) = Name_Read
6350 and then Next (First (Expressions (Parnt))) = Child
6351 then
6352 return;
6354 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
6355 and then Prefix (Parnt) = Child
6356 then
6357 null;
6359 else
6360 Expand_Packed_Element_Reference (N);
6361 return;
6362 end if;
6364 -- Keep looking up tree for unchecked expression, or if we are the
6365 -- prefix of a possible assignment left side.
6367 Child := Parnt;
6368 Parnt := Parent (Child);
6369 end loop;
6370 end;
6371 end Expand_N_Indexed_Component;
6373 ---------------------
6374 -- Expand_N_Not_In --
6375 ---------------------
6377 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6378 -- can be done. This avoids needing to duplicate this expansion code.
6380 procedure Expand_N_Not_In (N : Node_Id) is
6381 Loc : constant Source_Ptr := Sloc (N);
6382 Typ : constant Entity_Id := Etype (N);
6383 Cfs : constant Boolean := Comes_From_Source (N);
6385 begin
6386 Rewrite (N,
6387 Make_Op_Not (Loc,
6388 Right_Opnd =>
6389 Make_In (Loc,
6390 Left_Opnd => Left_Opnd (N),
6391 Right_Opnd => Right_Opnd (N))));
6393 -- If this is a set membership, preserve list of alternatives
6395 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
6397 -- We want this to appear as coming from source if original does (see
6398 -- transformations in Expand_N_In).
6400 Set_Comes_From_Source (N, Cfs);
6401 Set_Comes_From_Source (Right_Opnd (N), Cfs);
6403 -- Now analyze transformed node
6405 Analyze_And_Resolve (N, Typ);
6406 end Expand_N_Not_In;
6408 -------------------
6409 -- Expand_N_Null --
6410 -------------------
6412 -- The only replacement required is for the case of a null of a type that
6413 -- is an access to protected subprogram, or a subtype thereof. We represent
6414 -- such access values as a record, and so we must replace the occurrence of
6415 -- null by the equivalent record (with a null address and a null pointer in
6416 -- it), so that the backend creates the proper value.
6418 procedure Expand_N_Null (N : Node_Id) is
6419 Loc : constant Source_Ptr := Sloc (N);
6420 Typ : constant Entity_Id := Base_Type (Etype (N));
6421 Agg : Node_Id;
6423 begin
6424 if Is_Access_Protected_Subprogram_Type (Typ) then
6425 Agg :=
6426 Make_Aggregate (Loc,
6427 Expressions => New_List (
6428 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
6429 Make_Null (Loc)));
6431 Rewrite (N, Agg);
6432 Analyze_And_Resolve (N, Equivalent_Type (Typ));
6434 -- For subsequent semantic analysis, the node must retain its type.
6435 -- Gigi in any case replaces this type by the corresponding record
6436 -- type before processing the node.
6438 Set_Etype (N, Typ);
6439 end if;
6441 exception
6442 when RE_Not_Available =>
6443 return;
6444 end Expand_N_Null;
6446 ---------------------
6447 -- Expand_N_Op_Abs --
6448 ---------------------
6450 procedure Expand_N_Op_Abs (N : Node_Id) is
6451 Loc : constant Source_Ptr := Sloc (N);
6452 Expr : constant Node_Id := Right_Opnd (N);
6454 begin
6455 Unary_Op_Validity_Checks (N);
6457 -- Check for MINIMIZED/ELIMINATED overflow mode
6459 if Minimized_Eliminated_Overflow_Check (N) then
6460 Apply_Arithmetic_Overflow_Check (N);
6461 return;
6462 end if;
6464 -- Deal with software overflow checking
6466 if not Backend_Overflow_Checks_On_Target
6467 and then Is_Signed_Integer_Type (Etype (N))
6468 and then Do_Overflow_Check (N)
6469 then
6470 -- The only case to worry about is when the argument is equal to the
6471 -- largest negative number, so what we do is to insert the check:
6473 -- [constraint_error when Expr = typ'Base'First]
6475 -- with the usual Duplicate_Subexpr use coding for expr
6477 Insert_Action (N,
6478 Make_Raise_Constraint_Error (Loc,
6479 Condition =>
6480 Make_Op_Eq (Loc,
6481 Left_Opnd => Duplicate_Subexpr (Expr),
6482 Right_Opnd =>
6483 Make_Attribute_Reference (Loc,
6484 Prefix =>
6485 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
6486 Attribute_Name => Name_First)),
6487 Reason => CE_Overflow_Check_Failed));
6488 end if;
6489 end Expand_N_Op_Abs;
6491 ---------------------
6492 -- Expand_N_Op_Add --
6493 ---------------------
6495 procedure Expand_N_Op_Add (N : Node_Id) is
6496 Typ : constant Entity_Id := Etype (N);
6498 begin
6499 Binary_Op_Validity_Checks (N);
6501 -- Check for MINIMIZED/ELIMINATED overflow mode
6503 if Minimized_Eliminated_Overflow_Check (N) then
6504 Apply_Arithmetic_Overflow_Check (N);
6505 return;
6506 end if;
6508 -- N + 0 = 0 + N = N for integer types
6510 if Is_Integer_Type (Typ) then
6511 if Compile_Time_Known_Value (Right_Opnd (N))
6512 and then Expr_Value (Right_Opnd (N)) = Uint_0
6513 then
6514 Rewrite (N, Left_Opnd (N));
6515 return;
6517 elsif Compile_Time_Known_Value (Left_Opnd (N))
6518 and then Expr_Value (Left_Opnd (N)) = Uint_0
6519 then
6520 Rewrite (N, Right_Opnd (N));
6521 return;
6522 end if;
6523 end if;
6525 -- Arithmetic overflow checks for signed integer/fixed point types
6527 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
6528 Apply_Arithmetic_Overflow_Check (N);
6529 return;
6530 end if;
6532 -- Overflow checks for floating-point if -gnateF mode active
6534 Check_Float_Op_Overflow (N);
6535 end Expand_N_Op_Add;
6537 ---------------------
6538 -- Expand_N_Op_And --
6539 ---------------------
6541 procedure Expand_N_Op_And (N : Node_Id) is
6542 Typ : constant Entity_Id := Etype (N);
6544 begin
6545 Binary_Op_Validity_Checks (N);
6547 if Is_Array_Type (Etype (N)) then
6548 Expand_Boolean_Operator (N);
6550 elsif Is_Boolean_Type (Etype (N)) then
6551 Adjust_Condition (Left_Opnd (N));
6552 Adjust_Condition (Right_Opnd (N));
6553 Set_Etype (N, Standard_Boolean);
6554 Adjust_Result_Type (N, Typ);
6556 elsif Is_Intrinsic_Subprogram (Entity (N)) then
6557 Expand_Intrinsic_Call (N, Entity (N));
6559 end if;
6560 end Expand_N_Op_And;
6562 ------------------------
6563 -- Expand_N_Op_Concat --
6564 ------------------------
6566 procedure Expand_N_Op_Concat (N : Node_Id) is
6567 Opnds : List_Id;
6568 -- List of operands to be concatenated
6570 Cnode : Node_Id;
6571 -- Node which is to be replaced by the result of concatenating the nodes
6572 -- in the list Opnds.
6574 begin
6575 -- Ensure validity of both operands
6577 Binary_Op_Validity_Checks (N);
6579 -- If we are the left operand of a concatenation higher up the tree,
6580 -- then do nothing for now, since we want to deal with a series of
6581 -- concatenations as a unit.
6583 if Nkind (Parent (N)) = N_Op_Concat
6584 and then N = Left_Opnd (Parent (N))
6585 then
6586 return;
6587 end if;
6589 -- We get here with a concatenation whose left operand may be a
6590 -- concatenation itself with a consistent type. We need to process
6591 -- these concatenation operands from left to right, which means
6592 -- from the deepest node in the tree to the highest node.
6594 Cnode := N;
6595 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
6596 Cnode := Left_Opnd (Cnode);
6597 end loop;
6599 -- Now Cnode is the deepest concatenation, and its parents are the
6600 -- concatenation nodes above, so now we process bottom up, doing the
6601 -- operands.
6603 -- The outer loop runs more than once if more than one concatenation
6604 -- type is involved.
6606 Outer : loop
6607 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
6608 Set_Parent (Opnds, N);
6610 -- The inner loop gathers concatenation operands
6612 Inner : while Cnode /= N
6613 and then Base_Type (Etype (Cnode)) =
6614 Base_Type (Etype (Parent (Cnode)))
6615 loop
6616 Cnode := Parent (Cnode);
6617 Append (Right_Opnd (Cnode), Opnds);
6618 end loop Inner;
6620 -- Note: The following code is a temporary workaround for N731-034
6621 -- and N829-028 and will be kept until the general issue of internal
6622 -- symbol serialization is addressed. The workaround is kept under a
6623 -- debug switch to avoid permiating into the general case.
6625 -- Wrap the node to concatenate into an expression actions node to
6626 -- keep it nicely packaged. This is useful in the case of an assert
6627 -- pragma with a concatenation where we want to be able to delete
6628 -- the concatenation and all its expansion stuff.
6630 if Debug_Flag_Dot_H then
6631 declare
6632 Cnod : constant Node_Id := Relocate_Node (Cnode);
6633 Typ : constant Entity_Id := Base_Type (Etype (Cnode));
6635 begin
6636 -- Note: use Rewrite rather than Replace here, so that for
6637 -- example Why_Not_Static can find the original concatenation
6638 -- node OK!
6640 Rewrite (Cnode,
6641 Make_Expression_With_Actions (Sloc (Cnode),
6642 Actions => New_List (Make_Null_Statement (Sloc (Cnode))),
6643 Expression => Cnod));
6645 Expand_Concatenate (Cnod, Opnds);
6646 Analyze_And_Resolve (Cnode, Typ);
6647 end;
6649 -- Default case
6651 else
6652 Expand_Concatenate (Cnode, Opnds);
6653 end if;
6655 exit Outer when Cnode = N;
6656 Cnode := Parent (Cnode);
6657 end loop Outer;
6658 end Expand_N_Op_Concat;
6660 ------------------------
6661 -- Expand_N_Op_Divide --
6662 ------------------------
6664 procedure Expand_N_Op_Divide (N : Node_Id) is
6665 Loc : constant Source_Ptr := Sloc (N);
6666 Lopnd : constant Node_Id := Left_Opnd (N);
6667 Ropnd : constant Node_Id := Right_Opnd (N);
6668 Ltyp : constant Entity_Id := Etype (Lopnd);
6669 Rtyp : constant Entity_Id := Etype (Ropnd);
6670 Typ : Entity_Id := Etype (N);
6671 Rknow : constant Boolean := Is_Integer_Type (Typ)
6672 and then
6673 Compile_Time_Known_Value (Ropnd);
6674 Rval : Uint;
6676 begin
6677 Binary_Op_Validity_Checks (N);
6679 -- Check for MINIMIZED/ELIMINATED overflow mode
6681 if Minimized_Eliminated_Overflow_Check (N) then
6682 Apply_Arithmetic_Overflow_Check (N);
6683 return;
6684 end if;
6686 -- Otherwise proceed with expansion of division
6688 if Rknow then
6689 Rval := Expr_Value (Ropnd);
6690 end if;
6692 -- N / 1 = N for integer types
6694 if Rknow and then Rval = Uint_1 then
6695 Rewrite (N, Lopnd);
6696 return;
6697 end if;
6699 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
6700 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6701 -- operand is an unsigned integer, as required for this to work.
6703 if Nkind (Ropnd) = N_Op_Expon
6704 and then Is_Power_Of_2_For_Shift (Ropnd)
6706 -- We cannot do this transformation in configurable run time mode if we
6707 -- have 64-bit integers and long shifts are not available.
6709 and then (Esize (Ltyp) <= 32 or else Support_Long_Shifts_On_Target)
6710 then
6711 Rewrite (N,
6712 Make_Op_Shift_Right (Loc,
6713 Left_Opnd => Lopnd,
6714 Right_Opnd =>
6715 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
6716 Analyze_And_Resolve (N, Typ);
6717 return;
6718 end if;
6720 -- Do required fixup of universal fixed operation
6722 if Typ = Universal_Fixed then
6723 Fixup_Universal_Fixed_Operation (N);
6724 Typ := Etype (N);
6725 end if;
6727 -- Divisions with fixed-point results
6729 if Is_Fixed_Point_Type (Typ) then
6731 -- Deal with divide-by-zero check if back end cannot handle them
6732 -- and the flag is set indicating that we need such a check. Note
6733 -- that we don't need to bother here with the case of mixed-mode
6734 -- (Right operand an integer type), since these will be rewritten
6735 -- with conversions to a divide with a fixed-point right operand.
6737 if Do_Division_Check (N)
6738 and then not Backend_Divide_Checks_On_Target
6739 and then not Is_Integer_Type (Rtyp)
6740 then
6741 Set_Do_Division_Check (N, False);
6742 Insert_Action (N,
6743 Make_Raise_Constraint_Error (Loc,
6744 Condition =>
6745 Make_Op_Eq (Loc,
6746 Left_Opnd => Duplicate_Subexpr_Move_Checks (Ropnd),
6747 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
6748 Reason => CE_Divide_By_Zero));
6749 end if;
6751 -- No special processing if Treat_Fixed_As_Integer is set, since
6752 -- from a semantic point of view such operations are simply integer
6753 -- operations and will be treated that way.
6755 if not Treat_Fixed_As_Integer (N) then
6756 if Is_Integer_Type (Rtyp) then
6757 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
6758 else
6759 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
6760 end if;
6761 end if;
6763 -- Other cases of division of fixed-point operands. Again we exclude the
6764 -- case where Treat_Fixed_As_Integer is set.
6766 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6767 and then not Treat_Fixed_As_Integer (N)
6768 then
6769 if Is_Integer_Type (Typ) then
6770 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
6771 else
6772 pragma Assert (Is_Floating_Point_Type (Typ));
6773 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
6774 end if;
6776 -- Mixed-mode operations can appear in a non-static universal context,
6777 -- in which case the integer argument must be converted explicitly.
6779 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
6780 Rewrite (Ropnd,
6781 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
6783 Analyze_And_Resolve (Ropnd, Universal_Real);
6785 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
6786 Rewrite (Lopnd,
6787 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
6789 Analyze_And_Resolve (Lopnd, Universal_Real);
6791 -- Non-fixed point cases, do integer zero divide and overflow checks
6793 elsif Is_Integer_Type (Typ) then
6794 Apply_Divide_Checks (N);
6795 end if;
6797 -- Overflow checks for floating-point if -gnateF mode active
6799 Check_Float_Op_Overflow (N);
6800 end Expand_N_Op_Divide;
6802 --------------------
6803 -- Expand_N_Op_Eq --
6804 --------------------
6806 procedure Expand_N_Op_Eq (N : Node_Id) is
6807 Loc : constant Source_Ptr := Sloc (N);
6808 Typ : constant Entity_Id := Etype (N);
6809 Lhs : constant Node_Id := Left_Opnd (N);
6810 Rhs : constant Node_Id := Right_Opnd (N);
6811 Bodies : constant List_Id := New_List;
6812 A_Typ : constant Entity_Id := Etype (Lhs);
6814 Typl : Entity_Id := A_Typ;
6815 Op_Name : Entity_Id;
6816 Prim : Elmt_Id;
6818 procedure Build_Equality_Call (Eq : Entity_Id);
6819 -- If a constructed equality exists for the type or for its parent,
6820 -- build and analyze call, adding conversions if the operation is
6821 -- inherited.
6823 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
6824 -- Determines whether a type has a subcomponent of an unconstrained
6825 -- Unchecked_Union subtype. Typ is a record type.
6827 -------------------------
6828 -- Build_Equality_Call --
6829 -------------------------
6831 procedure Build_Equality_Call (Eq : Entity_Id) is
6832 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
6833 L_Exp : Node_Id := Relocate_Node (Lhs);
6834 R_Exp : Node_Id := Relocate_Node (Rhs);
6836 begin
6837 -- Adjust operands if necessary to comparison type
6839 if Base_Type (Op_Type) /= Base_Type (A_Typ)
6840 and then not Is_Class_Wide_Type (A_Typ)
6841 then
6842 L_Exp := OK_Convert_To (Op_Type, L_Exp);
6843 R_Exp := OK_Convert_To (Op_Type, R_Exp);
6844 end if;
6846 -- If we have an Unchecked_Union, we need to add the inferred
6847 -- discriminant values as actuals in the function call. At this
6848 -- point, the expansion has determined that both operands have
6849 -- inferable discriminants.
6851 if Is_Unchecked_Union (Op_Type) then
6852 declare
6853 Lhs_Type : constant Node_Id := Etype (L_Exp);
6854 Rhs_Type : constant Node_Id := Etype (R_Exp);
6856 Lhs_Discr_Vals : Elist_Id;
6857 -- List of inferred discriminant values for left operand.
6859 Rhs_Discr_Vals : Elist_Id;
6860 -- List of inferred discriminant values for right operand.
6862 Discr : Entity_Id;
6864 begin
6865 Lhs_Discr_Vals := New_Elmt_List;
6866 Rhs_Discr_Vals := New_Elmt_List;
6868 -- Per-object constrained selected components require special
6869 -- attention. If the enclosing scope of the component is an
6870 -- Unchecked_Union, we cannot reference its discriminants
6871 -- directly. This is why we use the extra parameters of the
6872 -- equality function of the enclosing Unchecked_Union.
6874 -- type UU_Type (Discr : Integer := 0) is
6875 -- . . .
6876 -- end record;
6877 -- pragma Unchecked_Union (UU_Type);
6879 -- 1. Unchecked_Union enclosing record:
6881 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
6882 -- . . .
6883 -- Comp : UU_Type (Discr);
6884 -- . . .
6885 -- end Enclosing_UU_Type;
6886 -- pragma Unchecked_Union (Enclosing_UU_Type);
6888 -- Obj1 : Enclosing_UU_Type;
6889 -- Obj2 : Enclosing_UU_Type (1);
6891 -- [. . .] Obj1 = Obj2 [. . .]
6893 -- Generated code:
6895 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
6897 -- A and B are the formal parameters of the equality function
6898 -- of Enclosing_UU_Type. The function always has two extra
6899 -- formals to capture the inferred discriminant values for
6900 -- each discriminant of the type.
6902 -- 2. Non-Unchecked_Union enclosing record:
6904 -- type
6905 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
6906 -- is record
6907 -- . . .
6908 -- Comp : UU_Type (Discr);
6909 -- . . .
6910 -- end Enclosing_Non_UU_Type;
6912 -- Obj1 : Enclosing_Non_UU_Type;
6913 -- Obj2 : Enclosing_Non_UU_Type (1);
6915 -- ... Obj1 = Obj2 ...
6917 -- Generated code:
6919 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
6920 -- obj1.discr, obj2.discr)) then
6922 -- In this case we can directly reference the discriminants of
6923 -- the enclosing record.
6925 -- Process left operand of equality
6927 if Nkind (Lhs) = N_Selected_Component
6928 and then
6929 Has_Per_Object_Constraint (Entity (Selector_Name (Lhs)))
6930 then
6931 -- If enclosing record is an Unchecked_Union, use formals
6932 -- corresponding to each discriminant. The name of the
6933 -- formal is that of the discriminant, with added suffix,
6934 -- see Exp_Ch3.Build_Record_Equality for details.
6936 if Is_Unchecked_Union (Scope (Entity (Selector_Name (Lhs))))
6937 then
6938 Discr :=
6939 First_Discriminant
6940 (Scope (Entity (Selector_Name (Lhs))));
6941 while Present (Discr) loop
6942 Append_Elmt
6943 (Make_Identifier (Loc,
6944 Chars => New_External_Name (Chars (Discr), 'A')),
6945 To => Lhs_Discr_Vals);
6946 Next_Discriminant (Discr);
6947 end loop;
6949 -- If enclosing record is of a non-Unchecked_Union type, it
6950 -- is possible to reference its discriminants directly.
6952 else
6953 Discr := First_Discriminant (Lhs_Type);
6954 while Present (Discr) loop
6955 Append_Elmt
6956 (Make_Selected_Component (Loc,
6957 Prefix => Prefix (Lhs),
6958 Selector_Name =>
6959 New_Copy
6960 (Get_Discriminant_Value (Discr,
6961 Lhs_Type,
6962 Stored_Constraint (Lhs_Type)))),
6963 To => Lhs_Discr_Vals);
6964 Next_Discriminant (Discr);
6965 end loop;
6966 end if;
6968 -- Otherwise operand is on object with a constrained type.
6969 -- Infer the discriminant values from the constraint.
6971 else
6973 Discr := First_Discriminant (Lhs_Type);
6974 while Present (Discr) loop
6975 Append_Elmt
6976 (New_Copy
6977 (Get_Discriminant_Value (Discr,
6978 Lhs_Type,
6979 Stored_Constraint (Lhs_Type))),
6980 To => Lhs_Discr_Vals);
6981 Next_Discriminant (Discr);
6982 end loop;
6983 end if;
6985 -- Similar processing for right operand of equality
6987 if Nkind (Rhs) = N_Selected_Component
6988 and then
6989 Has_Per_Object_Constraint (Entity (Selector_Name (Rhs)))
6990 then
6991 if Is_Unchecked_Union
6992 (Scope (Entity (Selector_Name (Rhs))))
6993 then
6994 Discr :=
6995 First_Discriminant
6996 (Scope (Entity (Selector_Name (Rhs))));
6997 while Present (Discr) loop
6998 Append_Elmt
6999 (Make_Identifier (Loc,
7000 Chars => New_External_Name (Chars (Discr), 'B')),
7001 To => Rhs_Discr_Vals);
7002 Next_Discriminant (Discr);
7003 end loop;
7005 else
7006 Discr := First_Discriminant (Rhs_Type);
7007 while Present (Discr) loop
7008 Append_Elmt
7009 (Make_Selected_Component (Loc,
7010 Prefix => Prefix (Rhs),
7011 Selector_Name =>
7012 New_Copy (Get_Discriminant_Value
7013 (Discr,
7014 Rhs_Type,
7015 Stored_Constraint (Rhs_Type)))),
7016 To => Rhs_Discr_Vals);
7017 Next_Discriminant (Discr);
7018 end loop;
7019 end if;
7021 else
7022 Discr := First_Discriminant (Rhs_Type);
7023 while Present (Discr) loop
7024 Append_Elmt
7025 (New_Copy (Get_Discriminant_Value
7026 (Discr,
7027 Rhs_Type,
7028 Stored_Constraint (Rhs_Type))),
7029 To => Rhs_Discr_Vals);
7030 Next_Discriminant (Discr);
7031 end loop;
7032 end if;
7034 -- Now merge the list of discriminant values so that values
7035 -- of corresponding discriminants are adjacent.
7037 declare
7038 Params : List_Id;
7039 L_Elmt : Elmt_Id;
7040 R_Elmt : Elmt_Id;
7042 begin
7043 Params := New_List (L_Exp, R_Exp);
7044 L_Elmt := First_Elmt (Lhs_Discr_Vals);
7045 R_Elmt := First_Elmt (Rhs_Discr_Vals);
7046 while Present (L_Elmt) loop
7047 Append_To (Params, Node (L_Elmt));
7048 Append_To (Params, Node (R_Elmt));
7049 Next_Elmt (L_Elmt);
7050 Next_Elmt (R_Elmt);
7051 end loop;
7053 Rewrite (N,
7054 Make_Function_Call (Loc,
7055 Name => New_Occurrence_Of (Eq, Loc),
7056 Parameter_Associations => Params));
7057 end;
7058 end;
7060 -- Normal case, not an unchecked union
7062 else
7063 Rewrite (N,
7064 Make_Function_Call (Loc,
7065 Name => New_Occurrence_Of (Eq, Loc),
7066 Parameter_Associations => New_List (L_Exp, R_Exp)));
7067 end if;
7069 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7070 end Build_Equality_Call;
7072 ------------------------------------
7073 -- Has_Unconstrained_UU_Component --
7074 ------------------------------------
7076 function Has_Unconstrained_UU_Component
7077 (Typ : Node_Id) return Boolean
7079 Tdef : constant Node_Id :=
7080 Type_Definition (Declaration_Node (Base_Type (Typ)));
7081 Clist : Node_Id;
7082 Vpart : Node_Id;
7084 function Component_Is_Unconstrained_UU
7085 (Comp : Node_Id) return Boolean;
7086 -- Determines whether the subtype of the component is an
7087 -- unconstrained Unchecked_Union.
7089 function Variant_Is_Unconstrained_UU
7090 (Variant : Node_Id) return Boolean;
7091 -- Determines whether a component of the variant has an unconstrained
7092 -- Unchecked_Union subtype.
7094 -----------------------------------
7095 -- Component_Is_Unconstrained_UU --
7096 -----------------------------------
7098 function Component_Is_Unconstrained_UU
7099 (Comp : Node_Id) return Boolean
7101 begin
7102 if Nkind (Comp) /= N_Component_Declaration then
7103 return False;
7104 end if;
7106 declare
7107 Sindic : constant Node_Id :=
7108 Subtype_Indication (Component_Definition (Comp));
7110 begin
7111 -- Unconstrained nominal type. In the case of a constraint
7112 -- present, the node kind would have been N_Subtype_Indication.
7114 if Nkind (Sindic) = N_Identifier then
7115 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
7116 end if;
7118 return False;
7119 end;
7120 end Component_Is_Unconstrained_UU;
7122 ---------------------------------
7123 -- Variant_Is_Unconstrained_UU --
7124 ---------------------------------
7126 function Variant_Is_Unconstrained_UU
7127 (Variant : Node_Id) return Boolean
7129 Clist : constant Node_Id := Component_List (Variant);
7131 begin
7132 if Is_Empty_List (Component_Items (Clist)) then
7133 return False;
7134 end if;
7136 -- We only need to test one component
7138 declare
7139 Comp : Node_Id := First (Component_Items (Clist));
7141 begin
7142 while Present (Comp) loop
7143 if Component_Is_Unconstrained_UU (Comp) then
7144 return True;
7145 end if;
7147 Next (Comp);
7148 end loop;
7149 end;
7151 -- None of the components withing the variant were of
7152 -- unconstrained Unchecked_Union type.
7154 return False;
7155 end Variant_Is_Unconstrained_UU;
7157 -- Start of processing for Has_Unconstrained_UU_Component
7159 begin
7160 if Null_Present (Tdef) then
7161 return False;
7162 end if;
7164 Clist := Component_List (Tdef);
7165 Vpart := Variant_Part (Clist);
7167 -- Inspect available components
7169 if Present (Component_Items (Clist)) then
7170 declare
7171 Comp : Node_Id := First (Component_Items (Clist));
7173 begin
7174 while Present (Comp) loop
7176 -- One component is sufficient
7178 if Component_Is_Unconstrained_UU (Comp) then
7179 return True;
7180 end if;
7182 Next (Comp);
7183 end loop;
7184 end;
7185 end if;
7187 -- Inspect available components withing variants
7189 if Present (Vpart) then
7190 declare
7191 Variant : Node_Id := First (Variants (Vpart));
7193 begin
7194 while Present (Variant) loop
7196 -- One component within a variant is sufficient
7198 if Variant_Is_Unconstrained_UU (Variant) then
7199 return True;
7200 end if;
7202 Next (Variant);
7203 end loop;
7204 end;
7205 end if;
7207 -- Neither the available components, nor the components inside the
7208 -- variant parts were of an unconstrained Unchecked_Union subtype.
7210 return False;
7211 end Has_Unconstrained_UU_Component;
7213 -- Start of processing for Expand_N_Op_Eq
7215 begin
7216 Binary_Op_Validity_Checks (N);
7218 -- Deal with private types
7220 if Ekind (Typl) = E_Private_Type then
7221 Typl := Underlying_Type (Typl);
7222 elsif Ekind (Typl) = E_Private_Subtype then
7223 Typl := Underlying_Type (Base_Type (Typl));
7224 else
7225 null;
7226 end if;
7228 -- It may happen in error situations that the underlying type is not
7229 -- set. The error will be detected later, here we just defend the
7230 -- expander code.
7232 if No (Typl) then
7233 return;
7234 end if;
7236 -- Now get the implementation base type (note that plain Base_Type here
7237 -- might lead us back to the private type, which is not what we want!)
7239 Typl := Implementation_Base_Type (Typl);
7241 -- Equality between variant records results in a call to a routine
7242 -- that has conditional tests of the discriminant value(s), and hence
7243 -- violates the No_Implicit_Conditionals restriction.
7245 if Has_Variant_Part (Typl) then
7246 declare
7247 Msg : Boolean;
7249 begin
7250 Check_Restriction (Msg, No_Implicit_Conditionals, N);
7252 if Msg then
7253 Error_Msg_N
7254 ("\comparison of variant records tests discriminants", N);
7255 return;
7256 end if;
7257 end;
7258 end if;
7260 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7261 -- means we no longer have a comparison operation, we are all done.
7263 Expand_Compare_Minimize_Eliminate_Overflow (N);
7265 if Nkind (N) /= N_Op_Eq then
7266 return;
7267 end if;
7269 -- Boolean types (requiring handling of non-standard case)
7271 if Is_Boolean_Type (Typl) then
7272 Adjust_Condition (Left_Opnd (N));
7273 Adjust_Condition (Right_Opnd (N));
7274 Set_Etype (N, Standard_Boolean);
7275 Adjust_Result_Type (N, Typ);
7277 -- Array types
7279 elsif Is_Array_Type (Typl) then
7281 -- If we are doing full validity checking, and it is possible for the
7282 -- array elements to be invalid then expand out array comparisons to
7283 -- make sure that we check the array elements.
7285 if Validity_Check_Operands
7286 and then not Is_Known_Valid (Component_Type (Typl))
7287 then
7288 declare
7289 Save_Force_Validity_Checks : constant Boolean :=
7290 Force_Validity_Checks;
7291 begin
7292 Force_Validity_Checks := True;
7293 Rewrite (N,
7294 Expand_Array_Equality
7296 Relocate_Node (Lhs),
7297 Relocate_Node (Rhs),
7298 Bodies,
7299 Typl));
7300 Insert_Actions (N, Bodies);
7301 Analyze_And_Resolve (N, Standard_Boolean);
7302 Force_Validity_Checks := Save_Force_Validity_Checks;
7303 end;
7305 -- Packed case where both operands are known aligned
7307 elsif Is_Bit_Packed_Array (Typl)
7308 and then not Is_Possibly_Unaligned_Object (Lhs)
7309 and then not Is_Possibly_Unaligned_Object (Rhs)
7310 then
7311 Expand_Packed_Eq (N);
7313 -- Where the component type is elementary we can use a block bit
7314 -- comparison (if supported on the target) exception in the case
7315 -- of floating-point (negative zero issues require element by
7316 -- element comparison), and atomic/VFA types (where we must be sure
7317 -- to load elements independently) and possibly unaligned arrays.
7319 elsif Is_Elementary_Type (Component_Type (Typl))
7320 and then not Is_Floating_Point_Type (Component_Type (Typl))
7321 and then not Is_Atomic_Or_VFA (Component_Type (Typl))
7322 and then not Is_Possibly_Unaligned_Object (Lhs)
7323 and then not Is_Possibly_Unaligned_Object (Rhs)
7324 and then Support_Composite_Compare_On_Target
7325 then
7326 null;
7328 -- For composite and floating-point cases, expand equality loop to
7329 -- make sure of using proper comparisons for tagged types, and
7330 -- correctly handling the floating-point case.
7332 else
7333 Rewrite (N,
7334 Expand_Array_Equality
7336 Relocate_Node (Lhs),
7337 Relocate_Node (Rhs),
7338 Bodies,
7339 Typl));
7340 Insert_Actions (N, Bodies, Suppress => All_Checks);
7341 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7342 end if;
7344 -- Record Types
7346 elsif Is_Record_Type (Typl) then
7348 -- For tagged types, use the primitive "="
7350 if Is_Tagged_Type (Typl) then
7352 -- No need to do anything else compiling under restriction
7353 -- No_Dispatching_Calls. During the semantic analysis we
7354 -- already notified such violation.
7356 if Restriction_Active (No_Dispatching_Calls) then
7357 return;
7358 end if;
7360 -- If this is derived from an untagged private type completed with
7361 -- a tagged type, it does not have a full view, so we use the
7362 -- primitive operations of the private type. This check should no
7363 -- longer be necessary when these types get their full views???
7365 if Is_Private_Type (A_Typ)
7366 and then not Is_Tagged_Type (A_Typ)
7367 and then Is_Derived_Type (A_Typ)
7368 and then No (Full_View (A_Typ))
7369 then
7370 -- Search for equality operation, checking that the operands
7371 -- have the same type. Note that we must find a matching entry,
7372 -- or something is very wrong.
7374 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
7376 while Present (Prim) loop
7377 exit when Chars (Node (Prim)) = Name_Op_Eq
7378 and then Etype (First_Formal (Node (Prim))) =
7379 Etype (Next_Formal (First_Formal (Node (Prim))))
7380 and then
7381 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7383 Next_Elmt (Prim);
7384 end loop;
7386 pragma Assert (Present (Prim));
7387 Op_Name := Node (Prim);
7389 -- Find the type's predefined equality or an overriding
7390 -- user-defined equality. The reason for not simply calling
7391 -- Find_Prim_Op here is that there may be a user-defined
7392 -- overloaded equality op that precedes the equality that we
7393 -- want, so we have to explicitly search (e.g., there could be
7394 -- an equality with two different parameter types).
7396 else
7397 if Is_Class_Wide_Type (Typl) then
7398 Typl := Find_Specific_Type (Typl);
7399 end if;
7401 Prim := First_Elmt (Primitive_Operations (Typl));
7402 while Present (Prim) loop
7403 exit when Chars (Node (Prim)) = Name_Op_Eq
7404 and then Etype (First_Formal (Node (Prim))) =
7405 Etype (Next_Formal (First_Formal (Node (Prim))))
7406 and then
7407 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7409 Next_Elmt (Prim);
7410 end loop;
7412 pragma Assert (Present (Prim));
7413 Op_Name := Node (Prim);
7414 end if;
7416 Build_Equality_Call (Op_Name);
7418 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7419 -- predefined equality operator for a type which has a subcomponent
7420 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7422 elsif Has_Unconstrained_UU_Component (Typl) then
7423 Insert_Action (N,
7424 Make_Raise_Program_Error (Loc,
7425 Reason => PE_Unchecked_Union_Restriction));
7427 -- Prevent Gigi from generating incorrect code by rewriting the
7428 -- equality as a standard False. (is this documented somewhere???)
7430 Rewrite (N,
7431 New_Occurrence_Of (Standard_False, Loc));
7433 elsif Is_Unchecked_Union (Typl) then
7435 -- If we can infer the discriminants of the operands, we make a
7436 -- call to the TSS equality function.
7438 if Has_Inferable_Discriminants (Lhs)
7439 and then
7440 Has_Inferable_Discriminants (Rhs)
7441 then
7442 Build_Equality_Call
7443 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7445 else
7446 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7447 -- the predefined equality operator for an Unchecked_Union type
7448 -- if either of the operands lack inferable discriminants.
7450 Insert_Action (N,
7451 Make_Raise_Program_Error (Loc,
7452 Reason => PE_Unchecked_Union_Restriction));
7454 -- Emit a warning on source equalities only, otherwise the
7455 -- message may appear out of place due to internal use. The
7456 -- warning is unconditional because it is required by the
7457 -- language.
7459 if Comes_From_Source (N) then
7460 Error_Msg_N
7461 ("Unchecked_Union discriminants cannot be determined??",
7463 Error_Msg_N
7464 ("\Program_Error will be raised for equality operation??",
7466 end if;
7468 -- Prevent Gigi from generating incorrect code by rewriting
7469 -- the equality as a standard False (documented where???).
7471 Rewrite (N,
7472 New_Occurrence_Of (Standard_False, Loc));
7473 end if;
7475 -- If a type support function is present (for complex cases), use it
7477 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
7478 Build_Equality_Call
7479 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7481 -- When comparing two Bounded_Strings, use the primitive equality of
7482 -- the root Super_String type.
7484 elsif Is_Bounded_String (Typl) then
7485 Prim :=
7486 First_Elmt (Collect_Primitive_Operations (Root_Type (Typl)));
7488 while Present (Prim) loop
7489 exit when Chars (Node (Prim)) = Name_Op_Eq
7490 and then Etype (First_Formal (Node (Prim))) =
7491 Etype (Next_Formal (First_Formal (Node (Prim))))
7492 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7494 Next_Elmt (Prim);
7495 end loop;
7497 -- A Super_String type should always have a primitive equality
7499 pragma Assert (Present (Prim));
7500 Build_Equality_Call (Node (Prim));
7502 -- Otherwise expand the component by component equality. Note that
7503 -- we never use block-bit comparisons for records, because of the
7504 -- problems with gaps. The backend will often be able to recombine
7505 -- the separate comparisons that we generate here.
7507 else
7508 Remove_Side_Effects (Lhs);
7509 Remove_Side_Effects (Rhs);
7510 Rewrite (N,
7511 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
7513 Insert_Actions (N, Bodies, Suppress => All_Checks);
7514 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7515 end if;
7516 end if;
7518 -- Test if result is known at compile time
7520 Rewrite_Comparison (N);
7522 -- Special optimization of length comparison
7524 Optimize_Length_Comparison (N);
7526 -- One more special case, if we have a comparison of X'Result = expr
7527 -- in floating-point, then if not already there, change expr to be
7528 -- f'Machine (expr) to eliminate suprise from extra precision.
7530 if Is_Floating_Point_Type (Typl)
7531 and then Nkind (Original_Node (Lhs)) = N_Attribute_Reference
7532 and then Attribute_Name (Original_Node (Lhs)) = Name_Result
7533 then
7534 -- Stick in the Typ'Machine call if not already there
7536 if Nkind (Rhs) /= N_Attribute_Reference
7537 or else Attribute_Name (Rhs) /= Name_Machine
7538 then
7539 Rewrite (Rhs,
7540 Make_Attribute_Reference (Loc,
7541 Prefix => New_Occurrence_Of (Typl, Loc),
7542 Attribute_Name => Name_Machine,
7543 Expressions => New_List (Relocate_Node (Rhs))));
7544 Analyze_And_Resolve (Rhs, Typl);
7545 end if;
7546 end if;
7547 end Expand_N_Op_Eq;
7549 -----------------------
7550 -- Expand_N_Op_Expon --
7551 -----------------------
7553 procedure Expand_N_Op_Expon (N : Node_Id) is
7554 Loc : constant Source_Ptr := Sloc (N);
7555 Typ : constant Entity_Id := Etype (N);
7556 Rtyp : constant Entity_Id := Root_Type (Typ);
7557 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
7558 Bastyp : constant Node_Id := Etype (Base);
7559 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
7560 Exptyp : constant Entity_Id := Etype (Exp);
7561 Ovflo : constant Boolean := Do_Overflow_Check (N);
7562 Expv : Uint;
7563 Temp : Node_Id;
7564 Rent : RE_Id;
7565 Ent : Entity_Id;
7566 Etyp : Entity_Id;
7567 Xnode : Node_Id;
7569 begin
7570 Binary_Op_Validity_Checks (N);
7572 -- CodePeer wants to see the unexpanded N_Op_Expon node
7574 if CodePeer_Mode then
7575 return;
7576 end if;
7578 -- If either operand is of a private type, then we have the use of an
7579 -- intrinsic operator, and we get rid of the privateness, by using root
7580 -- types of underlying types for the actual operation. Otherwise the
7581 -- private types will cause trouble if we expand multiplications or
7582 -- shifts etc. We also do this transformation if the result type is
7583 -- different from the base type.
7585 if Is_Private_Type (Etype (Base))
7586 or else Is_Private_Type (Typ)
7587 or else Is_Private_Type (Exptyp)
7588 or else Rtyp /= Root_Type (Bastyp)
7589 then
7590 declare
7591 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
7592 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
7593 begin
7594 Rewrite (N,
7595 Unchecked_Convert_To (Typ,
7596 Make_Op_Expon (Loc,
7597 Left_Opnd => Unchecked_Convert_To (Bt, Base),
7598 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
7599 Analyze_And_Resolve (N, Typ);
7600 return;
7601 end;
7602 end if;
7604 -- Check for MINIMIZED/ELIMINATED overflow mode
7606 if Minimized_Eliminated_Overflow_Check (N) then
7607 Apply_Arithmetic_Overflow_Check (N);
7608 return;
7609 end if;
7611 -- Test for case of known right argument where we can replace the
7612 -- exponentiation by an equivalent expression using multiplication.
7614 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
7615 -- configurable run-time mode, we may not have the exponentiation
7616 -- routine available, and we don't want the legality of the program
7617 -- to depend on how clever the compiler is in knowing values.
7619 if CRT_Safe_Compile_Time_Known_Value (Exp) then
7620 Expv := Expr_Value (Exp);
7622 -- We only fold small non-negative exponents. You might think we
7623 -- could fold small negative exponents for the real case, but we
7624 -- can't because we are required to raise Constraint_Error for
7625 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
7626 -- See ACVC test C4A012B.
7628 if Expv >= 0 and then Expv <= 4 then
7630 -- X ** 0 = 1 (or 1.0)
7632 if Expv = 0 then
7634 -- Call Remove_Side_Effects to ensure that any side effects
7635 -- in the ignored left operand (in particular function calls
7636 -- to user defined functions) are properly executed.
7638 Remove_Side_Effects (Base);
7640 if Ekind (Typ) in Integer_Kind then
7641 Xnode := Make_Integer_Literal (Loc, Intval => 1);
7642 else
7643 Xnode := Make_Real_Literal (Loc, Ureal_1);
7644 end if;
7646 -- X ** 1 = X
7648 elsif Expv = 1 then
7649 Xnode := Base;
7651 -- X ** 2 = X * X
7653 elsif Expv = 2 then
7654 Xnode :=
7655 Make_Op_Multiply (Loc,
7656 Left_Opnd => Duplicate_Subexpr (Base),
7657 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
7659 -- X ** 3 = X * X * X
7661 elsif Expv = 3 then
7662 Xnode :=
7663 Make_Op_Multiply (Loc,
7664 Left_Opnd =>
7665 Make_Op_Multiply (Loc,
7666 Left_Opnd => Duplicate_Subexpr (Base),
7667 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
7668 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
7670 -- X ** 4 ->
7672 -- do
7673 -- En : constant base'type := base * base;
7674 -- in
7675 -- En * En
7677 else
7678 pragma Assert (Expv = 4);
7679 Temp := Make_Temporary (Loc, 'E', Base);
7681 Xnode :=
7682 Make_Expression_With_Actions (Loc,
7683 Actions => New_List (
7684 Make_Object_Declaration (Loc,
7685 Defining_Identifier => Temp,
7686 Constant_Present => True,
7687 Object_Definition => New_Occurrence_Of (Typ, Loc),
7688 Expression =>
7689 Make_Op_Multiply (Loc,
7690 Left_Opnd =>
7691 Duplicate_Subexpr (Base),
7692 Right_Opnd =>
7693 Duplicate_Subexpr_No_Checks (Base)))),
7695 Expression =>
7696 Make_Op_Multiply (Loc,
7697 Left_Opnd => New_Occurrence_Of (Temp, Loc),
7698 Right_Opnd => New_Occurrence_Of (Temp, Loc)));
7699 end if;
7701 Rewrite (N, Xnode);
7702 Analyze_And_Resolve (N, Typ);
7703 return;
7704 end if;
7705 end if;
7707 -- Deal with optimizing 2 ** expression to shift where possible
7709 -- Note: we used to check that Exptyp was an unsigned type. But that is
7710 -- an unnecessary check, since if Exp is negative, we have a run-time
7711 -- error that is either caught (so we get the right result) or we have
7712 -- suppressed the check, in which case the code is erroneous anyway.
7714 if Is_Integer_Type (Rtyp)
7716 -- The base value must be "safe compile-time known", and exactly 2
7718 and then Nkind (Base) = N_Integer_Literal
7719 and then CRT_Safe_Compile_Time_Known_Value (Base)
7720 and then Expr_Value (Base) = Uint_2
7722 -- We only handle cases where the right type is a integer
7724 and then Is_Integer_Type (Root_Type (Exptyp))
7725 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
7727 -- This transformation is not applicable for a modular type with a
7728 -- nonbinary modulus because we do not handle modular reduction in
7729 -- a correct manner if we attempt this transformation in this case.
7731 and then not Non_Binary_Modulus (Typ)
7732 then
7733 -- Handle the cases where our parent is a division or multiplication
7734 -- specially. In these cases we can convert to using a shift at the
7735 -- parent level if we are not doing overflow checking, since it is
7736 -- too tricky to combine the overflow check at the parent level.
7738 if not Ovflo
7739 and then Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply)
7740 then
7741 declare
7742 P : constant Node_Id := Parent (N);
7743 L : constant Node_Id := Left_Opnd (P);
7744 R : constant Node_Id := Right_Opnd (P);
7746 begin
7747 if (Nkind (P) = N_Op_Multiply
7748 and then
7749 ((Is_Integer_Type (Etype (L)) and then R = N)
7750 or else
7751 (Is_Integer_Type (Etype (R)) and then L = N))
7752 and then not Do_Overflow_Check (P))
7754 or else
7755 (Nkind (P) = N_Op_Divide
7756 and then Is_Integer_Type (Etype (L))
7757 and then Is_Unsigned_Type (Etype (L))
7758 and then R = N
7759 and then not Do_Overflow_Check (P))
7760 then
7761 Set_Is_Power_Of_2_For_Shift (N);
7762 return;
7763 end if;
7764 end;
7766 -- Here we just have 2 ** N on its own, so we can convert this to a
7767 -- shift node. We are prepared to deal with overflow here, and we
7768 -- also have to handle proper modular reduction for binary modular.
7770 else
7771 declare
7772 OK : Boolean;
7773 Lo : Uint;
7774 Hi : Uint;
7776 MaxS : Uint;
7777 -- Maximum shift count with no overflow
7779 TestS : Boolean;
7780 -- Set True if we must test the shift count
7782 begin
7783 -- Compute maximum shift based on the underlying size. For a
7784 -- modular type this is one less than the size.
7786 if Is_Modular_Integer_Type (Typ) then
7788 -- For modular integer types, this is the size of the value
7789 -- being shifted minus one. Any larger values will cause
7790 -- modular reduction to a result of zero. Note that we do
7791 -- want the RM_Size here (e.g. mod 2 ** 7, we want a result
7792 -- of 6, since 2**7 should be reduced to zero).
7794 MaxS := RM_Size (Rtyp) - 1;
7796 -- For signed integer types, we use the size of the value
7797 -- being shifted minus 2. Larger values cause overflow.
7799 else
7800 MaxS := Esize (Rtyp) - 2;
7801 end if;
7803 -- Determine range to see if it can be larger than MaxS
7805 Determine_Range
7806 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
7807 TestS := (not OK) or else Hi > MaxS;
7809 -- Signed integer case
7811 if Is_Signed_Integer_Type (Typ) then
7813 -- Generate overflow check if overflow is active. Note that
7814 -- we can simply ignore the possibility of overflow if the
7815 -- flag is not set (means that overflow cannot happen or
7816 -- that overflow checks are suppressed).
7818 if Ovflo and TestS then
7819 Insert_Action (N,
7820 Make_Raise_Constraint_Error (Loc,
7821 Condition =>
7822 Make_Op_Gt (Loc,
7823 Left_Opnd => Duplicate_Subexpr (Right_Opnd (N)),
7824 Right_Opnd => Make_Integer_Literal (Loc, MaxS)),
7825 Reason => CE_Overflow_Check_Failed));
7826 end if;
7828 -- Now rewrite node as Shift_Left (1, right-operand)
7830 Rewrite (N,
7831 Make_Op_Shift_Left (Loc,
7832 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
7833 Right_Opnd => Right_Opnd (N)));
7835 -- Modular integer case
7837 else pragma Assert (Is_Modular_Integer_Type (Typ));
7839 -- If shift count can be greater than MaxS, we need to wrap
7840 -- the shift in a test that will reduce the result value to
7841 -- zero if this shift count is exceeded.
7843 if TestS then
7844 Rewrite (N,
7845 Make_If_Expression (Loc,
7846 Expressions => New_List (
7847 Make_Op_Gt (Loc,
7848 Left_Opnd => Duplicate_Subexpr (Right_Opnd (N)),
7849 Right_Opnd => Make_Integer_Literal (Loc, MaxS)),
7851 Make_Integer_Literal (Loc, Uint_0),
7853 Make_Op_Shift_Left (Loc,
7854 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
7855 Right_Opnd => Right_Opnd (N)))));
7857 -- If we know shift count cannot be greater than MaxS, then
7858 -- it is safe to just rewrite as a shift with no test.
7860 else
7861 Rewrite (N,
7862 Make_Op_Shift_Left (Loc,
7863 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
7864 Right_Opnd => Right_Opnd (N)));
7865 end if;
7866 end if;
7868 Analyze_And_Resolve (N, Typ);
7869 return;
7870 end;
7871 end if;
7872 end if;
7874 -- Fall through if exponentiation must be done using a runtime routine
7876 -- First deal with modular case
7878 if Is_Modular_Integer_Type (Rtyp) then
7880 -- Nonbinary case, we call the special exponentiation routine for
7881 -- the nonbinary case, converting the argument to Long_Long_Integer
7882 -- and passing the modulus value. Then the result is converted back
7883 -- to the base type.
7885 if Non_Binary_Modulus (Rtyp) then
7886 Rewrite (N,
7887 Convert_To (Typ,
7888 Make_Function_Call (Loc,
7889 Name =>
7890 New_Occurrence_Of (RTE (RE_Exp_Modular), Loc),
7891 Parameter_Associations => New_List (
7892 Convert_To (RTE (RE_Unsigned), Base),
7893 Make_Integer_Literal (Loc, Modulus (Rtyp)),
7894 Exp))));
7896 -- Binary case, in this case, we call one of two routines, either the
7897 -- unsigned integer case, or the unsigned long long integer case,
7898 -- with a final "and" operation to do the required mod.
7900 else
7901 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
7902 Ent := RTE (RE_Exp_Unsigned);
7903 else
7904 Ent := RTE (RE_Exp_Long_Long_Unsigned);
7905 end if;
7907 Rewrite (N,
7908 Convert_To (Typ,
7909 Make_Op_And (Loc,
7910 Left_Opnd =>
7911 Make_Function_Call (Loc,
7912 Name => New_Occurrence_Of (Ent, Loc),
7913 Parameter_Associations => New_List (
7914 Convert_To (Etype (First_Formal (Ent)), Base),
7915 Exp)),
7916 Right_Opnd =>
7917 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
7919 end if;
7921 -- Common exit point for modular type case
7923 Analyze_And_Resolve (N, Typ);
7924 return;
7926 -- Signed integer cases, done using either Integer or Long_Long_Integer.
7927 -- It is not worth having routines for Short_[Short_]Integer, since for
7928 -- most machines it would not help, and it would generate more code that
7929 -- might need certification when a certified run time is required.
7931 -- In the integer cases, we have two routines, one for when overflow
7932 -- checks are required, and one when they are not required, since there
7933 -- is a real gain in omitting checks on many machines.
7935 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
7936 or else (Rtyp = Base_Type (Standard_Long_Integer)
7937 and then
7938 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
7939 or else Rtyp = Universal_Integer
7940 then
7941 Etyp := Standard_Long_Long_Integer;
7943 -- Overflow checking is the only choice on the AAMP target, where
7944 -- arithmetic instructions check overflow automatically, so only
7945 -- one version of the exponentiation unit is needed.
7947 if Ovflo or AAMP_On_Target then
7948 Rent := RE_Exp_Long_Long_Integer;
7949 else
7950 Rent := RE_Exn_Long_Long_Integer;
7951 end if;
7953 elsif Is_Signed_Integer_Type (Rtyp) then
7954 Etyp := Standard_Integer;
7956 -- Overflow checking is the only choice on the AAMP target, where
7957 -- arithmetic instructions check overflow automatically, so only
7958 -- one version of the exponentiation unit is needed.
7960 if Ovflo or AAMP_On_Target then
7961 Rent := RE_Exp_Integer;
7962 else
7963 Rent := RE_Exn_Integer;
7964 end if;
7966 -- Floating-point cases, always done using Long_Long_Float. We do not
7967 -- need separate routines for the overflow case here, since in the case
7968 -- of floating-point, we generate infinities anyway as a rule (either
7969 -- that or we automatically trap overflow), and if there is an infinity
7970 -- generated and a range check is required, the check will fail anyway.
7972 else
7973 pragma Assert (Is_Floating_Point_Type (Rtyp));
7974 Etyp := Standard_Long_Long_Float;
7975 Rent := RE_Exn_Long_Long_Float;
7976 end if;
7978 -- Common processing for integer cases and floating-point cases.
7979 -- If we are in the right type, we can call runtime routine directly
7981 if Typ = Etyp
7982 and then Rtyp /= Universal_Integer
7983 and then Rtyp /= Universal_Real
7984 then
7985 Rewrite (N,
7986 Make_Function_Call (Loc,
7987 Name => New_Occurrence_Of (RTE (Rent), Loc),
7988 Parameter_Associations => New_List (Base, Exp)));
7990 -- Otherwise we have to introduce conversions (conversions are also
7991 -- required in the universal cases, since the runtime routine is
7992 -- typed using one of the standard types).
7994 else
7995 Rewrite (N,
7996 Convert_To (Typ,
7997 Make_Function_Call (Loc,
7998 Name => New_Occurrence_Of (RTE (Rent), Loc),
7999 Parameter_Associations => New_List (
8000 Convert_To (Etyp, Base),
8001 Exp))));
8002 end if;
8004 Analyze_And_Resolve (N, Typ);
8005 return;
8007 exception
8008 when RE_Not_Available =>
8009 return;
8010 end Expand_N_Op_Expon;
8012 --------------------
8013 -- Expand_N_Op_Ge --
8014 --------------------
8016 procedure Expand_N_Op_Ge (N : Node_Id) is
8017 Typ : constant Entity_Id := Etype (N);
8018 Op1 : constant Node_Id := Left_Opnd (N);
8019 Op2 : constant Node_Id := Right_Opnd (N);
8020 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8022 begin
8023 Binary_Op_Validity_Checks (N);
8025 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8026 -- means we no longer have a comparison operation, we are all done.
8028 Expand_Compare_Minimize_Eliminate_Overflow (N);
8030 if Nkind (N) /= N_Op_Ge then
8031 return;
8032 end if;
8034 -- Array type case
8036 if Is_Array_Type (Typ1) then
8037 Expand_Array_Comparison (N);
8038 return;
8039 end if;
8041 -- Deal with boolean operands
8043 if Is_Boolean_Type (Typ1) then
8044 Adjust_Condition (Op1);
8045 Adjust_Condition (Op2);
8046 Set_Etype (N, Standard_Boolean);
8047 Adjust_Result_Type (N, Typ);
8048 end if;
8050 Rewrite_Comparison (N);
8052 Optimize_Length_Comparison (N);
8053 end Expand_N_Op_Ge;
8055 --------------------
8056 -- Expand_N_Op_Gt --
8057 --------------------
8059 procedure Expand_N_Op_Gt (N : Node_Id) is
8060 Typ : constant Entity_Id := Etype (N);
8061 Op1 : constant Node_Id := Left_Opnd (N);
8062 Op2 : constant Node_Id := Right_Opnd (N);
8063 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8065 begin
8066 Binary_Op_Validity_Checks (N);
8068 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8069 -- means we no longer have a comparison operation, we are all done.
8071 Expand_Compare_Minimize_Eliminate_Overflow (N);
8073 if Nkind (N) /= N_Op_Gt then
8074 return;
8075 end if;
8077 -- Deal with array type operands
8079 if Is_Array_Type (Typ1) then
8080 Expand_Array_Comparison (N);
8081 return;
8082 end if;
8084 -- Deal with boolean type operands
8086 if Is_Boolean_Type (Typ1) then
8087 Adjust_Condition (Op1);
8088 Adjust_Condition (Op2);
8089 Set_Etype (N, Standard_Boolean);
8090 Adjust_Result_Type (N, Typ);
8091 end if;
8093 Rewrite_Comparison (N);
8095 Optimize_Length_Comparison (N);
8096 end Expand_N_Op_Gt;
8098 --------------------
8099 -- Expand_N_Op_Le --
8100 --------------------
8102 procedure Expand_N_Op_Le (N : Node_Id) is
8103 Typ : constant Entity_Id := Etype (N);
8104 Op1 : constant Node_Id := Left_Opnd (N);
8105 Op2 : constant Node_Id := Right_Opnd (N);
8106 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8108 begin
8109 Binary_Op_Validity_Checks (N);
8111 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8112 -- means we no longer have a comparison operation, we are all done.
8114 Expand_Compare_Minimize_Eliminate_Overflow (N);
8116 if Nkind (N) /= N_Op_Le then
8117 return;
8118 end if;
8120 -- Deal with array type operands
8122 if Is_Array_Type (Typ1) then
8123 Expand_Array_Comparison (N);
8124 return;
8125 end if;
8127 -- Deal with Boolean type operands
8129 if Is_Boolean_Type (Typ1) then
8130 Adjust_Condition (Op1);
8131 Adjust_Condition (Op2);
8132 Set_Etype (N, Standard_Boolean);
8133 Adjust_Result_Type (N, Typ);
8134 end if;
8136 Rewrite_Comparison (N);
8138 Optimize_Length_Comparison (N);
8139 end Expand_N_Op_Le;
8141 --------------------
8142 -- Expand_N_Op_Lt --
8143 --------------------
8145 procedure Expand_N_Op_Lt (N : Node_Id) is
8146 Typ : constant Entity_Id := Etype (N);
8147 Op1 : constant Node_Id := Left_Opnd (N);
8148 Op2 : constant Node_Id := Right_Opnd (N);
8149 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
8151 begin
8152 Binary_Op_Validity_Checks (N);
8154 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8155 -- means we no longer have a comparison operation, we are all done.
8157 Expand_Compare_Minimize_Eliminate_Overflow (N);
8159 if Nkind (N) /= N_Op_Lt then
8160 return;
8161 end if;
8163 -- Deal with array type operands
8165 if Is_Array_Type (Typ1) then
8166 Expand_Array_Comparison (N);
8167 return;
8168 end if;
8170 -- Deal with Boolean type operands
8172 if Is_Boolean_Type (Typ1) then
8173 Adjust_Condition (Op1);
8174 Adjust_Condition (Op2);
8175 Set_Etype (N, Standard_Boolean);
8176 Adjust_Result_Type (N, Typ);
8177 end if;
8179 Rewrite_Comparison (N);
8181 Optimize_Length_Comparison (N);
8182 end Expand_N_Op_Lt;
8184 -----------------------
8185 -- Expand_N_Op_Minus --
8186 -----------------------
8188 procedure Expand_N_Op_Minus (N : Node_Id) is
8189 Loc : constant Source_Ptr := Sloc (N);
8190 Typ : constant Entity_Id := Etype (N);
8192 begin
8193 Unary_Op_Validity_Checks (N);
8195 -- Check for MINIMIZED/ELIMINATED overflow mode
8197 if Minimized_Eliminated_Overflow_Check (N) then
8198 Apply_Arithmetic_Overflow_Check (N);
8199 return;
8200 end if;
8202 if not Backend_Overflow_Checks_On_Target
8203 and then Is_Signed_Integer_Type (Etype (N))
8204 and then Do_Overflow_Check (N)
8205 then
8206 -- Software overflow checking expands -expr into (0 - expr)
8208 Rewrite (N,
8209 Make_Op_Subtract (Loc,
8210 Left_Opnd => Make_Integer_Literal (Loc, 0),
8211 Right_Opnd => Right_Opnd (N)));
8213 Analyze_And_Resolve (N, Typ);
8214 end if;
8215 end Expand_N_Op_Minus;
8217 ---------------------
8218 -- Expand_N_Op_Mod --
8219 ---------------------
8221 procedure Expand_N_Op_Mod (N : Node_Id) is
8222 Loc : constant Source_Ptr := Sloc (N);
8223 Typ : constant Entity_Id := Etype (N);
8224 DDC : constant Boolean := Do_Division_Check (N);
8226 Left : Node_Id;
8227 Right : Node_Id;
8229 LLB : Uint;
8230 Llo : Uint;
8231 Lhi : Uint;
8232 LOK : Boolean;
8233 Rlo : Uint;
8234 Rhi : Uint;
8235 ROK : Boolean;
8237 pragma Warnings (Off, Lhi);
8239 begin
8240 Binary_Op_Validity_Checks (N);
8242 -- Check for MINIMIZED/ELIMINATED overflow mode
8244 if Minimized_Eliminated_Overflow_Check (N) then
8245 Apply_Arithmetic_Overflow_Check (N);
8246 return;
8247 end if;
8249 if Is_Integer_Type (Etype (N)) then
8250 Apply_Divide_Checks (N);
8252 -- All done if we don't have a MOD any more, which can happen as a
8253 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8255 if Nkind (N) /= N_Op_Mod then
8256 return;
8257 end if;
8258 end if;
8260 -- Proceed with expansion of mod operator
8262 Left := Left_Opnd (N);
8263 Right := Right_Opnd (N);
8265 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
8266 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
8268 -- Convert mod to rem if operands are both known to be non-negative, or
8269 -- both known to be non-positive (these are the cases in which rem and
8270 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
8271 -- likely that this will improve the quality of code, (the operation now
8272 -- corresponds to the hardware remainder), and it does not seem likely
8273 -- that it could be harmful. It also avoids some cases of the elaborate
8274 -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %).
8276 if (LOK and ROK)
8277 and then ((Llo >= 0 and then Rlo >= 0)
8278 or else
8279 (Lhi <= 0 and then Rhi <= 0))
8280 then
8281 Rewrite (N,
8282 Make_Op_Rem (Sloc (N),
8283 Left_Opnd => Left_Opnd (N),
8284 Right_Opnd => Right_Opnd (N)));
8286 -- Instead of reanalyzing the node we do the analysis manually. This
8287 -- avoids anomalies when the replacement is done in an instance and
8288 -- is epsilon more efficient.
8290 Set_Entity (N, Standard_Entity (S_Op_Rem));
8291 Set_Etype (N, Typ);
8292 Set_Do_Division_Check (N, DDC);
8293 Expand_N_Op_Rem (N);
8294 Set_Analyzed (N);
8295 return;
8297 -- Otherwise, normal mod processing
8299 else
8300 -- Apply optimization x mod 1 = 0. We don't really need that with
8301 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
8302 -- certainly harmless.
8304 if Is_Integer_Type (Etype (N))
8305 and then Compile_Time_Known_Value (Right)
8306 and then Expr_Value (Right) = Uint_1
8307 then
8308 -- Call Remove_Side_Effects to ensure that any side effects in
8309 -- the ignored left operand (in particular function calls to
8310 -- user defined functions) are properly executed.
8312 Remove_Side_Effects (Left);
8314 Rewrite (N, Make_Integer_Literal (Loc, 0));
8315 Analyze_And_Resolve (N, Typ);
8316 return;
8317 end if;
8319 -- If we still have a mod operator and we are in Modify_Tree_For_C
8320 -- mode, and we have a signed integer type, then here is where we do
8321 -- the rewrite in terms of Rem. Note this rewrite bypasses the need
8322 -- for the special handling of the annoying case of largest negative
8323 -- number mod minus one.
8325 if Nkind (N) = N_Op_Mod
8326 and then Is_Signed_Integer_Type (Typ)
8327 and then Modify_Tree_For_C
8328 then
8329 -- In the general case, we expand A mod B as
8331 -- Tnn : constant typ := A rem B;
8332 -- ..
8333 -- (if (A >= 0) = (B >= 0) then Tnn
8334 -- elsif Tnn = 0 then 0
8335 -- else Tnn + B)
8337 -- The comparison can be written simply as A >= 0 if we know that
8338 -- B >= 0 which is a very common case.
8340 -- An important optimization is when B is known at compile time
8341 -- to be 2**K for some constant. In this case we can simply AND
8342 -- the left operand with the bit string 2**K-1 (i.e. K 1-bits)
8343 -- and that works for both the positive and negative cases.
8345 declare
8346 P2 : constant Nat := Power_Of_Two (Right);
8348 begin
8349 if P2 /= 0 then
8350 Rewrite (N,
8351 Unchecked_Convert_To (Typ,
8352 Make_Op_And (Loc,
8353 Left_Opnd =>
8354 Unchecked_Convert_To
8355 (Corresponding_Unsigned_Type (Typ), Left),
8356 Right_Opnd =>
8357 Make_Integer_Literal (Loc, 2 ** P2 - 1))));
8358 Analyze_And_Resolve (N, Typ);
8359 return;
8360 end if;
8361 end;
8363 -- Here for the full rewrite
8365 declare
8366 Tnn : constant Entity_Id := Make_Temporary (Sloc (N), 'T', N);
8367 Cmp : Node_Id;
8369 begin
8370 Cmp :=
8371 Make_Op_Ge (Loc,
8372 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
8373 Right_Opnd => Make_Integer_Literal (Loc, 0));
8375 if not LOK or else Rlo < 0 then
8376 Cmp :=
8377 Make_Op_Eq (Loc,
8378 Left_Opnd => Cmp,
8379 Right_Opnd =>
8380 Make_Op_Ge (Loc,
8381 Left_Opnd => Duplicate_Subexpr_No_Checks (Right),
8382 Right_Opnd => Make_Integer_Literal (Loc, 0)));
8383 end if;
8385 Insert_Action (N,
8386 Make_Object_Declaration (Loc,
8387 Defining_Identifier => Tnn,
8388 Constant_Present => True,
8389 Object_Definition => New_Occurrence_Of (Typ, Loc),
8390 Expression =>
8391 Make_Op_Rem (Loc,
8392 Left_Opnd => Left,
8393 Right_Opnd => Right)));
8395 Rewrite (N,
8396 Make_If_Expression (Loc,
8397 Expressions => New_List (
8398 Cmp,
8399 New_Occurrence_Of (Tnn, Loc),
8400 Make_If_Expression (Loc,
8401 Is_Elsif => True,
8402 Expressions => New_List (
8403 Make_Op_Eq (Loc,
8404 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8405 Right_Opnd => Make_Integer_Literal (Loc, 0)),
8406 Make_Integer_Literal (Loc, 0),
8407 Make_Op_Add (Loc,
8408 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8409 Right_Opnd =>
8410 Duplicate_Subexpr_No_Checks (Right)))))));
8412 Analyze_And_Resolve (N, Typ);
8413 return;
8414 end;
8415 end if;
8417 -- Deal with annoying case of largest negative number mod minus one.
8418 -- Gigi may not handle this case correctly, because on some targets,
8419 -- the mod value is computed using a divide instruction which gives
8420 -- an overflow trap for this case.
8422 -- It would be a bit more efficient to figure out which targets
8423 -- this is really needed for, but in practice it is reasonable
8424 -- to do the following special check in all cases, since it means
8425 -- we get a clearer message, and also the overhead is minimal given
8426 -- that division is expensive in any case.
8428 -- In fact the check is quite easy, if the right operand is -1, then
8429 -- the mod value is always 0, and we can just ignore the left operand
8430 -- completely in this case.
8432 -- This only applies if we still have a mod operator. Skip if we
8433 -- have already rewritten this (e.g. in the case of eliminated
8434 -- overflow checks which have driven us into bignum mode).
8436 if Nkind (N) = N_Op_Mod then
8438 -- The operand type may be private (e.g. in the expansion of an
8439 -- intrinsic operation) so we must use the underlying type to get
8440 -- the bounds, and convert the literals explicitly.
8442 LLB :=
8443 Expr_Value
8444 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
8446 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
8447 and then ((not LOK) or else (Llo = LLB))
8448 then
8449 Rewrite (N,
8450 Make_If_Expression (Loc,
8451 Expressions => New_List (
8452 Make_Op_Eq (Loc,
8453 Left_Opnd => Duplicate_Subexpr (Right),
8454 Right_Opnd =>
8455 Unchecked_Convert_To (Typ,
8456 Make_Integer_Literal (Loc, -1))),
8457 Unchecked_Convert_To (Typ,
8458 Make_Integer_Literal (Loc, Uint_0)),
8459 Relocate_Node (N))));
8461 Set_Analyzed (Next (Next (First (Expressions (N)))));
8462 Analyze_And_Resolve (N, Typ);
8463 end if;
8464 end if;
8465 end if;
8466 end Expand_N_Op_Mod;
8468 --------------------------
8469 -- Expand_N_Op_Multiply --
8470 --------------------------
8472 procedure Expand_N_Op_Multiply (N : Node_Id) is
8473 Loc : constant Source_Ptr := Sloc (N);
8474 Lop : constant Node_Id := Left_Opnd (N);
8475 Rop : constant Node_Id := Right_Opnd (N);
8477 Lp2 : constant Boolean :=
8478 Nkind (Lop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Lop);
8479 Rp2 : constant Boolean :=
8480 Nkind (Rop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Rop);
8482 Ltyp : constant Entity_Id := Etype (Lop);
8483 Rtyp : constant Entity_Id := Etype (Rop);
8484 Typ : Entity_Id := Etype (N);
8486 begin
8487 Binary_Op_Validity_Checks (N);
8489 -- Check for MINIMIZED/ELIMINATED overflow mode
8491 if Minimized_Eliminated_Overflow_Check (N) then
8492 Apply_Arithmetic_Overflow_Check (N);
8493 return;
8494 end if;
8496 -- Special optimizations for integer types
8498 if Is_Integer_Type (Typ) then
8500 -- N * 0 = 0 for integer types
8502 if Compile_Time_Known_Value (Rop)
8503 and then Expr_Value (Rop) = Uint_0
8504 then
8505 -- Call Remove_Side_Effects to ensure that any side effects in
8506 -- the ignored left operand (in particular function calls to
8507 -- user defined functions) are properly executed.
8509 Remove_Side_Effects (Lop);
8511 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8512 Analyze_And_Resolve (N, Typ);
8513 return;
8514 end if;
8516 -- Similar handling for 0 * N = 0
8518 if Compile_Time_Known_Value (Lop)
8519 and then Expr_Value (Lop) = Uint_0
8520 then
8521 Remove_Side_Effects (Rop);
8522 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8523 Analyze_And_Resolve (N, Typ);
8524 return;
8525 end if;
8527 -- N * 1 = 1 * N = N for integer types
8529 -- This optimisation is not done if we are going to
8530 -- rewrite the product 1 * 2 ** N to a shift.
8532 if Compile_Time_Known_Value (Rop)
8533 and then Expr_Value (Rop) = Uint_1
8534 and then not Lp2
8535 then
8536 Rewrite (N, Lop);
8537 return;
8539 elsif Compile_Time_Known_Value (Lop)
8540 and then Expr_Value (Lop) = Uint_1
8541 and then not Rp2
8542 then
8543 Rewrite (N, Rop);
8544 return;
8545 end if;
8546 end if;
8548 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
8549 -- Is_Power_Of_2_For_Shift is set means that we know that our left
8550 -- operand is an integer, as required for this to work.
8552 if Rp2 then
8553 if Lp2 then
8555 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
8557 Rewrite (N,
8558 Make_Op_Expon (Loc,
8559 Left_Opnd => Make_Integer_Literal (Loc, 2),
8560 Right_Opnd =>
8561 Make_Op_Add (Loc,
8562 Left_Opnd => Right_Opnd (Lop),
8563 Right_Opnd => Right_Opnd (Rop))));
8564 Analyze_And_Resolve (N, Typ);
8565 return;
8567 else
8568 -- If the result is modular, perform the reduction of the result
8569 -- appropriately.
8571 if Is_Modular_Integer_Type (Typ)
8572 and then not Non_Binary_Modulus (Typ)
8573 then
8574 Rewrite (N,
8575 Make_Op_And (Loc,
8576 Left_Opnd =>
8577 Make_Op_Shift_Left (Loc,
8578 Left_Opnd => Lop,
8579 Right_Opnd =>
8580 Convert_To (Standard_Natural, Right_Opnd (Rop))),
8581 Right_Opnd =>
8582 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
8584 else
8585 Rewrite (N,
8586 Make_Op_Shift_Left (Loc,
8587 Left_Opnd => Lop,
8588 Right_Opnd =>
8589 Convert_To (Standard_Natural, Right_Opnd (Rop))));
8590 end if;
8592 Analyze_And_Resolve (N, Typ);
8593 return;
8594 end if;
8596 -- Same processing for the operands the other way round
8598 elsif Lp2 then
8599 if Is_Modular_Integer_Type (Typ)
8600 and then not Non_Binary_Modulus (Typ)
8601 then
8602 Rewrite (N,
8603 Make_Op_And (Loc,
8604 Left_Opnd =>
8605 Make_Op_Shift_Left (Loc,
8606 Left_Opnd => Rop,
8607 Right_Opnd =>
8608 Convert_To (Standard_Natural, Right_Opnd (Lop))),
8609 Right_Opnd =>
8610 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
8612 else
8613 Rewrite (N,
8614 Make_Op_Shift_Left (Loc,
8615 Left_Opnd => Rop,
8616 Right_Opnd =>
8617 Convert_To (Standard_Natural, Right_Opnd (Lop))));
8618 end if;
8620 Analyze_And_Resolve (N, Typ);
8621 return;
8622 end if;
8624 -- Do required fixup of universal fixed operation
8626 if Typ = Universal_Fixed then
8627 Fixup_Universal_Fixed_Operation (N);
8628 Typ := Etype (N);
8629 end if;
8631 -- Multiplications with fixed-point results
8633 if Is_Fixed_Point_Type (Typ) then
8635 -- No special processing if Treat_Fixed_As_Integer is set, since from
8636 -- a semantic point of view such operations are simply integer
8637 -- operations and will be treated that way.
8639 if not Treat_Fixed_As_Integer (N) then
8641 -- Case of fixed * integer => fixed
8643 if Is_Integer_Type (Rtyp) then
8644 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
8646 -- Case of integer * fixed => fixed
8648 elsif Is_Integer_Type (Ltyp) then
8649 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
8651 -- Case of fixed * fixed => fixed
8653 else
8654 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
8655 end if;
8656 end if;
8658 -- Other cases of multiplication of fixed-point operands. Again we
8659 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
8661 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
8662 and then not Treat_Fixed_As_Integer (N)
8663 then
8664 if Is_Integer_Type (Typ) then
8665 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
8666 else
8667 pragma Assert (Is_Floating_Point_Type (Typ));
8668 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
8669 end if;
8671 -- Mixed-mode operations can appear in a non-static universal context,
8672 -- in which case the integer argument must be converted explicitly.
8674 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
8675 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
8676 Analyze_And_Resolve (Rop, Universal_Real);
8678 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
8679 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
8680 Analyze_And_Resolve (Lop, Universal_Real);
8682 -- Non-fixed point cases, check software overflow checking required
8684 elsif Is_Signed_Integer_Type (Etype (N)) then
8685 Apply_Arithmetic_Overflow_Check (N);
8686 end if;
8688 -- Overflow checks for floating-point if -gnateF mode active
8690 Check_Float_Op_Overflow (N);
8691 end Expand_N_Op_Multiply;
8693 --------------------
8694 -- Expand_N_Op_Ne --
8695 --------------------
8697 procedure Expand_N_Op_Ne (N : Node_Id) is
8698 Typ : constant Entity_Id := Etype (Left_Opnd (N));
8700 begin
8701 -- Case of elementary type with standard operator
8703 if Is_Elementary_Type (Typ)
8704 and then Sloc (Entity (N)) = Standard_Location
8705 then
8706 Binary_Op_Validity_Checks (N);
8708 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
8709 -- means we no longer have a /= operation, we are all done.
8711 Expand_Compare_Minimize_Eliminate_Overflow (N);
8713 if Nkind (N) /= N_Op_Ne then
8714 return;
8715 end if;
8717 -- Boolean types (requiring handling of non-standard case)
8719 if Is_Boolean_Type (Typ) then
8720 Adjust_Condition (Left_Opnd (N));
8721 Adjust_Condition (Right_Opnd (N));
8722 Set_Etype (N, Standard_Boolean);
8723 Adjust_Result_Type (N, Typ);
8724 end if;
8726 Rewrite_Comparison (N);
8728 -- For all cases other than elementary types, we rewrite node as the
8729 -- negation of an equality operation, and reanalyze. The equality to be
8730 -- used is defined in the same scope and has the same signature. This
8731 -- signature must be set explicitly since in an instance it may not have
8732 -- the same visibility as in the generic unit. This avoids duplicating
8733 -- or factoring the complex code for record/array equality tests etc.
8735 else
8736 declare
8737 Loc : constant Source_Ptr := Sloc (N);
8738 Neg : Node_Id;
8739 Ne : constant Entity_Id := Entity (N);
8741 begin
8742 Binary_Op_Validity_Checks (N);
8744 Neg :=
8745 Make_Op_Not (Loc,
8746 Right_Opnd =>
8747 Make_Op_Eq (Loc,
8748 Left_Opnd => Left_Opnd (N),
8749 Right_Opnd => Right_Opnd (N)));
8750 Set_Paren_Count (Right_Opnd (Neg), 1);
8752 if Scope (Ne) /= Standard_Standard then
8753 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
8754 end if;
8756 -- For navigation purposes, we want to treat the inequality as an
8757 -- implicit reference to the corresponding equality. Preserve the
8758 -- Comes_From_ source flag to generate proper Xref entries.
8760 Preserve_Comes_From_Source (Neg, N);
8761 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
8762 Rewrite (N, Neg);
8763 Analyze_And_Resolve (N, Standard_Boolean);
8764 end;
8765 end if;
8767 Optimize_Length_Comparison (N);
8768 end Expand_N_Op_Ne;
8770 ---------------------
8771 -- Expand_N_Op_Not --
8772 ---------------------
8774 -- If the argument is other than a Boolean array type, there is no special
8775 -- expansion required, except for dealing with validity checks, and non-
8776 -- standard boolean representations.
8778 -- For the packed array case, we call the special routine in Exp_Pakd,
8779 -- except that if the component size is greater than one, we use the
8780 -- standard routine generating a gruesome loop (it is so peculiar to have
8781 -- packed arrays with non-standard Boolean representations anyway, so it
8782 -- does not matter that we do not handle this case efficiently).
8784 -- For the unpacked array case (and for the special packed case where we
8785 -- have non standard Booleans, as discussed above), we generate and insert
8786 -- into the tree the following function definition:
8788 -- function Nnnn (A : arr) is
8789 -- B : arr;
8790 -- begin
8791 -- for J in a'range loop
8792 -- B (J) := not A (J);
8793 -- end loop;
8794 -- return B;
8795 -- end Nnnn;
8797 -- Here arr is the actual subtype of the parameter (and hence always
8798 -- constrained). Then we replace the not with a call to this function.
8800 procedure Expand_N_Op_Not (N : Node_Id) is
8801 Loc : constant Source_Ptr := Sloc (N);
8802 Typ : constant Entity_Id := Etype (N);
8803 Opnd : Node_Id;
8804 Arr : Entity_Id;
8805 A : Entity_Id;
8806 B : Entity_Id;
8807 J : Entity_Id;
8808 A_J : Node_Id;
8809 B_J : Node_Id;
8811 Func_Name : Entity_Id;
8812 Loop_Statement : Node_Id;
8814 begin
8815 Unary_Op_Validity_Checks (N);
8817 -- For boolean operand, deal with non-standard booleans
8819 if Is_Boolean_Type (Typ) then
8820 Adjust_Condition (Right_Opnd (N));
8821 Set_Etype (N, Standard_Boolean);
8822 Adjust_Result_Type (N, Typ);
8823 return;
8824 end if;
8826 -- Only array types need any other processing
8828 if not Is_Array_Type (Typ) then
8829 return;
8830 end if;
8832 -- Case of array operand. If bit packed with a component size of 1,
8833 -- handle it in Exp_Pakd if the operand is known to be aligned.
8835 if Is_Bit_Packed_Array (Typ)
8836 and then Component_Size (Typ) = 1
8837 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
8838 then
8839 Expand_Packed_Not (N);
8840 return;
8841 end if;
8843 -- Case of array operand which is not bit-packed. If the context is
8844 -- a safe assignment, call in-place operation, If context is a larger
8845 -- boolean expression in the context of a safe assignment, expansion is
8846 -- done by enclosing operation.
8848 Opnd := Relocate_Node (Right_Opnd (N));
8849 Convert_To_Actual_Subtype (Opnd);
8850 Arr := Etype (Opnd);
8851 Ensure_Defined (Arr, N);
8852 Silly_Boolean_Array_Not_Test (N, Arr);
8854 if Nkind (Parent (N)) = N_Assignment_Statement then
8855 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
8856 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8857 return;
8859 -- Special case the negation of a binary operation
8861 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
8862 and then Safe_In_Place_Array_Op
8863 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
8864 then
8865 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8866 return;
8867 end if;
8869 elsif Nkind (Parent (N)) in N_Binary_Op
8870 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
8871 then
8872 declare
8873 Op1 : constant Node_Id := Left_Opnd (Parent (N));
8874 Op2 : constant Node_Id := Right_Opnd (Parent (N));
8875 Lhs : constant Node_Id := Name (Parent (Parent (N)));
8877 begin
8878 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
8880 -- (not A) op (not B) can be reduced to a single call
8882 if N = Op1 and then Nkind (Op2) = N_Op_Not then
8883 return;
8885 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
8886 return;
8888 -- A xor (not B) can also be special-cased
8890 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
8891 return;
8892 end if;
8893 end if;
8894 end;
8895 end if;
8897 A := Make_Defining_Identifier (Loc, Name_uA);
8898 B := Make_Defining_Identifier (Loc, Name_uB);
8899 J := Make_Defining_Identifier (Loc, Name_uJ);
8901 A_J :=
8902 Make_Indexed_Component (Loc,
8903 Prefix => New_Occurrence_Of (A, Loc),
8904 Expressions => New_List (New_Occurrence_Of (J, Loc)));
8906 B_J :=
8907 Make_Indexed_Component (Loc,
8908 Prefix => New_Occurrence_Of (B, Loc),
8909 Expressions => New_List (New_Occurrence_Of (J, Loc)));
8911 Loop_Statement :=
8912 Make_Implicit_Loop_Statement (N,
8913 Identifier => Empty,
8915 Iteration_Scheme =>
8916 Make_Iteration_Scheme (Loc,
8917 Loop_Parameter_Specification =>
8918 Make_Loop_Parameter_Specification (Loc,
8919 Defining_Identifier => J,
8920 Discrete_Subtype_Definition =>
8921 Make_Attribute_Reference (Loc,
8922 Prefix => Make_Identifier (Loc, Chars (A)),
8923 Attribute_Name => Name_Range))),
8925 Statements => New_List (
8926 Make_Assignment_Statement (Loc,
8927 Name => B_J,
8928 Expression => Make_Op_Not (Loc, A_J))));
8930 Func_Name := Make_Temporary (Loc, 'N');
8931 Set_Is_Inlined (Func_Name);
8933 Insert_Action (N,
8934 Make_Subprogram_Body (Loc,
8935 Specification =>
8936 Make_Function_Specification (Loc,
8937 Defining_Unit_Name => Func_Name,
8938 Parameter_Specifications => New_List (
8939 Make_Parameter_Specification (Loc,
8940 Defining_Identifier => A,
8941 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8942 Result_Definition => New_Occurrence_Of (Typ, Loc)),
8944 Declarations => New_List (
8945 Make_Object_Declaration (Loc,
8946 Defining_Identifier => B,
8947 Object_Definition => New_Occurrence_Of (Arr, Loc))),
8949 Handled_Statement_Sequence =>
8950 Make_Handled_Sequence_Of_Statements (Loc,
8951 Statements => New_List (
8952 Loop_Statement,
8953 Make_Simple_Return_Statement (Loc,
8954 Expression => Make_Identifier (Loc, Chars (B)))))));
8956 Rewrite (N,
8957 Make_Function_Call (Loc,
8958 Name => New_Occurrence_Of (Func_Name, Loc),
8959 Parameter_Associations => New_List (Opnd)));
8961 Analyze_And_Resolve (N, Typ);
8962 end Expand_N_Op_Not;
8964 --------------------
8965 -- Expand_N_Op_Or --
8966 --------------------
8968 procedure Expand_N_Op_Or (N : Node_Id) is
8969 Typ : constant Entity_Id := Etype (N);
8971 begin
8972 Binary_Op_Validity_Checks (N);
8974 if Is_Array_Type (Etype (N)) then
8975 Expand_Boolean_Operator (N);
8977 elsif Is_Boolean_Type (Etype (N)) then
8978 Adjust_Condition (Left_Opnd (N));
8979 Adjust_Condition (Right_Opnd (N));
8980 Set_Etype (N, Standard_Boolean);
8981 Adjust_Result_Type (N, Typ);
8983 elsif Is_Intrinsic_Subprogram (Entity (N)) then
8984 Expand_Intrinsic_Call (N, Entity (N));
8986 end if;
8987 end Expand_N_Op_Or;
8989 ----------------------
8990 -- Expand_N_Op_Plus --
8991 ----------------------
8993 procedure Expand_N_Op_Plus (N : Node_Id) is
8994 begin
8995 Unary_Op_Validity_Checks (N);
8997 -- Check for MINIMIZED/ELIMINATED overflow mode
8999 if Minimized_Eliminated_Overflow_Check (N) then
9000 Apply_Arithmetic_Overflow_Check (N);
9001 return;
9002 end if;
9003 end Expand_N_Op_Plus;
9005 ---------------------
9006 -- Expand_N_Op_Rem --
9007 ---------------------
9009 procedure Expand_N_Op_Rem (N : Node_Id) is
9010 Loc : constant Source_Ptr := Sloc (N);
9011 Typ : constant Entity_Id := Etype (N);
9013 Left : Node_Id;
9014 Right : Node_Id;
9016 Lo : Uint;
9017 Hi : Uint;
9018 OK : Boolean;
9020 Lneg : Boolean;
9021 Rneg : Boolean;
9022 -- Set if corresponding operand can be negative
9024 pragma Unreferenced (Hi);
9026 begin
9027 Binary_Op_Validity_Checks (N);
9029 -- Check for MINIMIZED/ELIMINATED overflow mode
9031 if Minimized_Eliminated_Overflow_Check (N) then
9032 Apply_Arithmetic_Overflow_Check (N);
9033 return;
9034 end if;
9036 if Is_Integer_Type (Etype (N)) then
9037 Apply_Divide_Checks (N);
9039 -- All done if we don't have a REM any more, which can happen as a
9040 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
9042 if Nkind (N) /= N_Op_Rem then
9043 return;
9044 end if;
9045 end if;
9047 -- Proceed with expansion of REM
9049 Left := Left_Opnd (N);
9050 Right := Right_Opnd (N);
9052 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
9053 -- but it is useful with other back ends (e.g. AAMP), and is certainly
9054 -- harmless.
9056 if Is_Integer_Type (Etype (N))
9057 and then Compile_Time_Known_Value (Right)
9058 and then Expr_Value (Right) = Uint_1
9059 then
9060 -- Call Remove_Side_Effects to ensure that any side effects in the
9061 -- ignored left operand (in particular function calls to user defined
9062 -- functions) are properly executed.
9064 Remove_Side_Effects (Left);
9066 Rewrite (N, Make_Integer_Literal (Loc, 0));
9067 Analyze_And_Resolve (N, Typ);
9068 return;
9069 end if;
9071 -- Deal with annoying case of largest negative number remainder minus
9072 -- one. Gigi may not handle this case correctly, because on some
9073 -- targets, the mod value is computed using a divide instruction
9074 -- which gives an overflow trap for this case.
9076 -- It would be a bit more efficient to figure out which targets this
9077 -- is really needed for, but in practice it is reasonable to do the
9078 -- following special check in all cases, since it means we get a clearer
9079 -- message, and also the overhead is minimal given that division is
9080 -- expensive in any case.
9082 -- In fact the check is quite easy, if the right operand is -1, then
9083 -- the remainder is always 0, and we can just ignore the left operand
9084 -- completely in this case.
9086 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
9087 Lneg := (not OK) or else Lo < 0;
9089 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
9090 Rneg := (not OK) or else Lo < 0;
9092 -- We won't mess with trying to find out if the left operand can really
9093 -- be the largest negative number (that's a pain in the case of private
9094 -- types and this is really marginal). We will just assume that we need
9095 -- the test if the left operand can be negative at all.
9097 if Lneg and Rneg then
9098 Rewrite (N,
9099 Make_If_Expression (Loc,
9100 Expressions => New_List (
9101 Make_Op_Eq (Loc,
9102 Left_Opnd => Duplicate_Subexpr (Right),
9103 Right_Opnd =>
9104 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
9106 Unchecked_Convert_To (Typ,
9107 Make_Integer_Literal (Loc, Uint_0)),
9109 Relocate_Node (N))));
9111 Set_Analyzed (Next (Next (First (Expressions (N)))));
9112 Analyze_And_Resolve (N, Typ);
9113 end if;
9114 end Expand_N_Op_Rem;
9116 -----------------------------
9117 -- Expand_N_Op_Rotate_Left --
9118 -----------------------------
9120 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
9121 begin
9122 Binary_Op_Validity_Checks (N);
9124 -- If we are in Modify_Tree_For_C mode, there is no rotate left in C,
9125 -- so we rewrite in terms of logical shifts
9127 -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits)
9129 -- where Bits is the shift count mod Esize (the mod operation here
9130 -- deals with ludicrous large shift counts, which are apparently OK).
9132 -- What about nonbinary modulus ???
9134 declare
9135 Loc : constant Source_Ptr := Sloc (N);
9136 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
9137 Typ : constant Entity_Id := Etype (N);
9139 begin
9140 if Modify_Tree_For_C then
9141 Rewrite (Right_Opnd (N),
9142 Make_Op_Rem (Loc,
9143 Left_Opnd => Relocate_Node (Right_Opnd (N)),
9144 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
9146 Analyze_And_Resolve (Right_Opnd (N), Rtp);
9148 Rewrite (N,
9149 Make_Op_Or (Loc,
9150 Left_Opnd =>
9151 Make_Op_Shift_Left (Loc,
9152 Left_Opnd => Left_Opnd (N),
9153 Right_Opnd => Right_Opnd (N)),
9155 Right_Opnd =>
9156 Make_Op_Shift_Right (Loc,
9157 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
9158 Right_Opnd =>
9159 Make_Op_Subtract (Loc,
9160 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
9161 Right_Opnd =>
9162 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
9164 Analyze_And_Resolve (N, Typ);
9165 end if;
9166 end;
9167 end Expand_N_Op_Rotate_Left;
9169 ------------------------------
9170 -- Expand_N_Op_Rotate_Right --
9171 ------------------------------
9173 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
9174 begin
9175 Binary_Op_Validity_Checks (N);
9177 -- If we are in Modify_Tree_For_C mode, there is no rotate right in C,
9178 -- so we rewrite in terms of logical shifts
9180 -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits)
9182 -- where Bits is the shift count mod Esize (the mod operation here
9183 -- deals with ludicrous large shift counts, which are apparently OK).
9185 -- What about nonbinary modulus ???
9187 declare
9188 Loc : constant Source_Ptr := Sloc (N);
9189 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
9190 Typ : constant Entity_Id := Etype (N);
9192 begin
9193 Rewrite (Right_Opnd (N),
9194 Make_Op_Rem (Loc,
9195 Left_Opnd => Relocate_Node (Right_Opnd (N)),
9196 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
9198 Analyze_And_Resolve (Right_Opnd (N), Rtp);
9200 if Modify_Tree_For_C then
9201 Rewrite (N,
9202 Make_Op_Or (Loc,
9203 Left_Opnd =>
9204 Make_Op_Shift_Right (Loc,
9205 Left_Opnd => Left_Opnd (N),
9206 Right_Opnd => Right_Opnd (N)),
9208 Right_Opnd =>
9209 Make_Op_Shift_Left (Loc,
9210 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
9211 Right_Opnd =>
9212 Make_Op_Subtract (Loc,
9213 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
9214 Right_Opnd =>
9215 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
9217 Analyze_And_Resolve (N, Typ);
9218 end if;
9219 end;
9220 end Expand_N_Op_Rotate_Right;
9222 ----------------------------
9223 -- Expand_N_Op_Shift_Left --
9224 ----------------------------
9226 -- Note: nothing in this routine depends on left as opposed to right shifts
9227 -- so we share the routine for expanding shift right operations.
9229 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
9230 begin
9231 Binary_Op_Validity_Checks (N);
9233 -- If we are in Modify_Tree_For_C mode, then ensure that the right
9234 -- operand is not greater than the word size (since that would not
9235 -- be defined properly by the corresponding C shift operator).
9237 if Modify_Tree_For_C then
9238 declare
9239 Right : constant Node_Id := Right_Opnd (N);
9240 Loc : constant Source_Ptr := Sloc (Right);
9241 Typ : constant Entity_Id := Etype (N);
9242 Siz : constant Uint := Esize (Typ);
9243 Orig : Node_Id;
9244 OK : Boolean;
9245 Lo : Uint;
9246 Hi : Uint;
9248 begin
9249 if Compile_Time_Known_Value (Right) then
9250 if Expr_Value (Right) >= Siz then
9251 Rewrite (N, Make_Integer_Literal (Loc, 0));
9252 Analyze_And_Resolve (N, Typ);
9253 end if;
9255 -- Not compile time known, find range
9257 else
9258 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
9260 -- Nothing to do if known to be OK range, otherwise expand
9262 if not OK or else Hi >= Siz then
9264 -- Prevent recursion on copy of shift node
9266 Orig := Relocate_Node (N);
9267 Set_Analyzed (Orig);
9269 -- Now do the rewrite
9271 Rewrite (N,
9272 Make_If_Expression (Loc,
9273 Expressions => New_List (
9274 Make_Op_Ge (Loc,
9275 Left_Opnd => Duplicate_Subexpr_Move_Checks (Right),
9276 Right_Opnd => Make_Integer_Literal (Loc, Siz)),
9277 Make_Integer_Literal (Loc, 0),
9278 Orig)));
9279 Analyze_And_Resolve (N, Typ);
9280 end if;
9281 end if;
9282 end;
9283 end if;
9284 end Expand_N_Op_Shift_Left;
9286 -----------------------------
9287 -- Expand_N_Op_Shift_Right --
9288 -----------------------------
9290 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
9291 begin
9292 -- Share shift left circuit
9294 Expand_N_Op_Shift_Left (N);
9295 end Expand_N_Op_Shift_Right;
9297 ----------------------------------------
9298 -- Expand_N_Op_Shift_Right_Arithmetic --
9299 ----------------------------------------
9301 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
9302 begin
9303 Binary_Op_Validity_Checks (N);
9305 -- If we are in Modify_Tree_For_C mode, there is no shift right
9306 -- arithmetic in C, so we rewrite in terms of logical shifts.
9308 -- Shift_Right (Num, Bits) or
9309 -- (if Num >= Sign
9310 -- then not (Shift_Right (Mask, bits))
9311 -- else 0)
9313 -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1)
9315 -- Note: in almost all C compilers it would work to just shift a
9316 -- signed integer right, but it's undefined and we cannot rely on it.
9318 -- Note: the above works fine for shift counts greater than or equal
9319 -- to the word size, since in this case (not (Shift_Right (Mask, bits)))
9320 -- generates all 1'bits.
9322 -- What about nonbinary modulus ???
9324 declare
9325 Loc : constant Source_Ptr := Sloc (N);
9326 Typ : constant Entity_Id := Etype (N);
9327 Sign : constant Uint := 2 ** (Esize (Typ) - 1);
9328 Mask : constant Uint := (2 ** Esize (Typ)) - 1;
9329 Left : constant Node_Id := Left_Opnd (N);
9330 Right : constant Node_Id := Right_Opnd (N);
9331 Maskx : Node_Id;
9333 begin
9334 if Modify_Tree_For_C then
9336 -- Here if not (Shift_Right (Mask, bits)) can be computed at
9337 -- compile time as a single constant.
9339 if Compile_Time_Known_Value (Right) then
9340 declare
9341 Val : constant Uint := Expr_Value (Right);
9343 begin
9344 if Val >= Esize (Typ) then
9345 Maskx := Make_Integer_Literal (Loc, Mask);
9347 else
9348 Maskx :=
9349 Make_Integer_Literal (Loc,
9350 Intval => Mask - (Mask / (2 ** Expr_Value (Right))));
9351 end if;
9352 end;
9354 else
9355 Maskx :=
9356 Make_Op_Not (Loc,
9357 Right_Opnd =>
9358 Make_Op_Shift_Right (Loc,
9359 Left_Opnd => Make_Integer_Literal (Loc, Mask),
9360 Right_Opnd => Duplicate_Subexpr_No_Checks (Right)));
9361 end if;
9363 -- Now do the rewrite
9365 Rewrite (N,
9366 Make_Op_Or (Loc,
9367 Left_Opnd =>
9368 Make_Op_Shift_Right (Loc,
9369 Left_Opnd => Left,
9370 Right_Opnd => Right),
9371 Right_Opnd =>
9372 Make_If_Expression (Loc,
9373 Expressions => New_List (
9374 Make_Op_Ge (Loc,
9375 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
9376 Right_Opnd => Make_Integer_Literal (Loc, Sign)),
9377 Maskx,
9378 Make_Integer_Literal (Loc, 0)))));
9379 Analyze_And_Resolve (N, Typ);
9380 end if;
9381 end;
9382 end Expand_N_Op_Shift_Right_Arithmetic;
9384 --------------------------
9385 -- Expand_N_Op_Subtract --
9386 --------------------------
9388 procedure Expand_N_Op_Subtract (N : Node_Id) is
9389 Typ : constant Entity_Id := Etype (N);
9391 begin
9392 Binary_Op_Validity_Checks (N);
9394 -- Check for MINIMIZED/ELIMINATED overflow mode
9396 if Minimized_Eliminated_Overflow_Check (N) then
9397 Apply_Arithmetic_Overflow_Check (N);
9398 return;
9399 end if;
9401 -- N - 0 = N for integer types
9403 if Is_Integer_Type (Typ)
9404 and then Compile_Time_Known_Value (Right_Opnd (N))
9405 and then Expr_Value (Right_Opnd (N)) = 0
9406 then
9407 Rewrite (N, Left_Opnd (N));
9408 return;
9409 end if;
9411 -- Arithmetic overflow checks for signed integer/fixed point types
9413 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
9414 Apply_Arithmetic_Overflow_Check (N);
9415 end if;
9417 -- Overflow checks for floating-point if -gnateF mode active
9419 Check_Float_Op_Overflow (N);
9420 end Expand_N_Op_Subtract;
9422 ---------------------
9423 -- Expand_N_Op_Xor --
9424 ---------------------
9426 procedure Expand_N_Op_Xor (N : Node_Id) is
9427 Typ : constant Entity_Id := Etype (N);
9429 begin
9430 Binary_Op_Validity_Checks (N);
9432 if Is_Array_Type (Etype (N)) then
9433 Expand_Boolean_Operator (N);
9435 elsif Is_Boolean_Type (Etype (N)) then
9436 Adjust_Condition (Left_Opnd (N));
9437 Adjust_Condition (Right_Opnd (N));
9438 Set_Etype (N, Standard_Boolean);
9439 Adjust_Result_Type (N, Typ);
9441 elsif Is_Intrinsic_Subprogram (Entity (N)) then
9442 Expand_Intrinsic_Call (N, Entity (N));
9444 end if;
9445 end Expand_N_Op_Xor;
9447 ----------------------
9448 -- Expand_N_Or_Else --
9449 ----------------------
9451 procedure Expand_N_Or_Else (N : Node_Id)
9452 renames Expand_Short_Circuit_Operator;
9454 -----------------------------------
9455 -- Expand_N_Qualified_Expression --
9456 -----------------------------------
9458 procedure Expand_N_Qualified_Expression (N : Node_Id) is
9459 Operand : constant Node_Id := Expression (N);
9460 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
9462 begin
9463 -- Do validity check if validity checking operands
9465 if Validity_Checks_On and Validity_Check_Operands then
9466 Ensure_Valid (Operand);
9467 end if;
9469 -- Apply possible constraint check
9471 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
9473 if Do_Range_Check (Operand) then
9474 Set_Do_Range_Check (Operand, False);
9475 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
9476 end if;
9477 end Expand_N_Qualified_Expression;
9479 ------------------------------------
9480 -- Expand_N_Quantified_Expression --
9481 ------------------------------------
9483 -- We expand:
9485 -- for all X in range => Cond
9487 -- into:
9489 -- T := True;
9490 -- for X in range loop
9491 -- if not Cond then
9492 -- T := False;
9493 -- exit;
9494 -- end if;
9495 -- end loop;
9497 -- Similarly, an existentially quantified expression:
9499 -- for some X in range => Cond
9501 -- becomes:
9503 -- T := False;
9504 -- for X in range loop
9505 -- if Cond then
9506 -- T := True;
9507 -- exit;
9508 -- end if;
9509 -- end loop;
9511 -- In both cases, the iteration may be over a container in which case it is
9512 -- given by an iterator specification, not a loop parameter specification.
9514 procedure Expand_N_Quantified_Expression (N : Node_Id) is
9515 Actions : constant List_Id := New_List;
9516 For_All : constant Boolean := All_Present (N);
9517 Iter_Spec : constant Node_Id := Iterator_Specification (N);
9518 Loc : constant Source_Ptr := Sloc (N);
9519 Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N);
9520 Cond : Node_Id;
9521 Flag : Entity_Id;
9522 Scheme : Node_Id;
9523 Stmts : List_Id;
9525 begin
9526 -- Create the declaration of the flag which tracks the status of the
9527 -- quantified expression. Generate:
9529 -- Flag : Boolean := (True | False);
9531 Flag := Make_Temporary (Loc, 'T', N);
9533 Append_To (Actions,
9534 Make_Object_Declaration (Loc,
9535 Defining_Identifier => Flag,
9536 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
9537 Expression =>
9538 New_Occurrence_Of (Boolean_Literals (For_All), Loc)));
9540 -- Construct the circuitry which tracks the status of the quantified
9541 -- expression. Generate:
9543 -- if [not] Cond then
9544 -- Flag := (False | True);
9545 -- exit;
9546 -- end if;
9548 Cond := Relocate_Node (Condition (N));
9550 if For_All then
9551 Cond := Make_Op_Not (Loc, Cond);
9552 end if;
9554 Stmts := New_List (
9555 Make_Implicit_If_Statement (N,
9556 Condition => Cond,
9557 Then_Statements => New_List (
9558 Make_Assignment_Statement (Loc,
9559 Name => New_Occurrence_Of (Flag, Loc),
9560 Expression =>
9561 New_Occurrence_Of (Boolean_Literals (not For_All), Loc)),
9562 Make_Exit_Statement (Loc))));
9564 -- Build the loop equivalent of the quantified expression
9566 if Present (Iter_Spec) then
9567 Scheme :=
9568 Make_Iteration_Scheme (Loc,
9569 Iterator_Specification => Iter_Spec);
9570 else
9571 Scheme :=
9572 Make_Iteration_Scheme (Loc,
9573 Loop_Parameter_Specification => Loop_Spec);
9574 end if;
9576 Append_To (Actions,
9577 Make_Loop_Statement (Loc,
9578 Iteration_Scheme => Scheme,
9579 Statements => Stmts,
9580 End_Label => Empty));
9582 -- Transform the quantified expression
9584 Rewrite (N,
9585 Make_Expression_With_Actions (Loc,
9586 Expression => New_Occurrence_Of (Flag, Loc),
9587 Actions => Actions));
9588 Analyze_And_Resolve (N, Standard_Boolean);
9589 end Expand_N_Quantified_Expression;
9591 ---------------------------------
9592 -- Expand_N_Selected_Component --
9593 ---------------------------------
9595 procedure Expand_N_Selected_Component (N : Node_Id) is
9596 Loc : constant Source_Ptr := Sloc (N);
9597 Par : constant Node_Id := Parent (N);
9598 P : constant Node_Id := Prefix (N);
9599 S : constant Node_Id := Selector_Name (N);
9600 Ptyp : Entity_Id := Underlying_Type (Etype (P));
9601 Disc : Entity_Id;
9602 New_N : Node_Id;
9603 Dcon : Elmt_Id;
9604 Dval : Node_Id;
9606 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
9607 -- Gigi needs a temporary for prefixes that depend on a discriminant,
9608 -- unless the context of an assignment can provide size information.
9609 -- Don't we have a general routine that does this???
9611 function Is_Subtype_Declaration return Boolean;
9612 -- The replacement of a discriminant reference by its value is required
9613 -- if this is part of the initialization of an temporary generated by a
9614 -- change of representation. This shows up as the construction of a
9615 -- discriminant constraint for a subtype declared at the same point as
9616 -- the entity in the prefix of the selected component. We recognize this
9617 -- case when the context of the reference is:
9618 -- subtype ST is T(Obj.D);
9619 -- where the entity for Obj comes from source, and ST has the same sloc.
9621 -----------------------
9622 -- In_Left_Hand_Side --
9623 -----------------------
9625 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
9626 begin
9627 return (Nkind (Parent (Comp)) = N_Assignment_Statement
9628 and then Comp = Name (Parent (Comp)))
9629 or else (Present (Parent (Comp))
9630 and then Nkind (Parent (Comp)) in N_Subexpr
9631 and then In_Left_Hand_Side (Parent (Comp)));
9632 end In_Left_Hand_Side;
9634 -----------------------------
9635 -- Is_Subtype_Declaration --
9636 -----------------------------
9638 function Is_Subtype_Declaration return Boolean is
9639 Par : constant Node_Id := Parent (N);
9640 begin
9641 return
9642 Nkind (Par) = N_Index_Or_Discriminant_Constraint
9643 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
9644 and then Comes_From_Source (Entity (Prefix (N)))
9645 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
9646 end Is_Subtype_Declaration;
9648 -- Start of processing for Expand_N_Selected_Component
9650 begin
9651 -- Insert explicit dereference if required
9653 if Is_Access_Type (Ptyp) then
9655 -- First set prefix type to proper access type, in case it currently
9656 -- has a private (non-access) view of this type.
9658 Set_Etype (P, Ptyp);
9660 Insert_Explicit_Dereference (P);
9661 Analyze_And_Resolve (P, Designated_Type (Ptyp));
9663 if Ekind (Etype (P)) = E_Private_Subtype
9664 and then Is_For_Access_Subtype (Etype (P))
9665 then
9666 Set_Etype (P, Base_Type (Etype (P)));
9667 end if;
9669 Ptyp := Etype (P);
9670 end if;
9672 -- Deal with discriminant check required
9674 if Do_Discriminant_Check (N) then
9675 if Present (Discriminant_Checking_Func
9676 (Original_Record_Component (Entity (S))))
9677 then
9678 -- Present the discriminant checking function to the backend, so
9679 -- that it can inline the call to the function.
9681 Add_Inlined_Body
9682 (Discriminant_Checking_Func
9683 (Original_Record_Component (Entity (S))),
9686 -- Now reset the flag and generate the call
9688 Set_Do_Discriminant_Check (N, False);
9689 Generate_Discriminant_Check (N);
9691 -- In the case of Unchecked_Union, no discriminant checking is
9692 -- actually performed.
9694 else
9695 Set_Do_Discriminant_Check (N, False);
9696 end if;
9697 end if;
9699 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9700 -- function, then additional actuals must be passed.
9702 if Ada_Version >= Ada_2005
9703 and then Is_Build_In_Place_Function_Call (P)
9704 then
9705 Make_Build_In_Place_Call_In_Anonymous_Context (P);
9706 end if;
9708 -- Gigi cannot handle unchecked conversions that are the prefix of a
9709 -- selected component with discriminants. This must be checked during
9710 -- expansion, because during analysis the type of the selector is not
9711 -- known at the point the prefix is analyzed. If the conversion is the
9712 -- target of an assignment, then we cannot force the evaluation.
9714 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
9715 and then Has_Discriminants (Etype (N))
9716 and then not In_Left_Hand_Side (N)
9717 then
9718 Force_Evaluation (Prefix (N));
9719 end if;
9721 -- Remaining processing applies only if selector is a discriminant
9723 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
9725 -- If the selector is a discriminant of a constrained record type,
9726 -- we may be able to rewrite the expression with the actual value
9727 -- of the discriminant, a useful optimization in some cases.
9729 if Is_Record_Type (Ptyp)
9730 and then Has_Discriminants (Ptyp)
9731 and then Is_Constrained (Ptyp)
9732 then
9733 -- Do this optimization for discrete types only, and not for
9734 -- access types (access discriminants get us into trouble).
9736 if not Is_Discrete_Type (Etype (N)) then
9737 null;
9739 -- Don't do this on the left hand of an assignment statement.
9740 -- Normally one would think that references like this would not
9741 -- occur, but they do in generated code, and mean that we really
9742 -- do want to assign the discriminant.
9744 elsif Nkind (Par) = N_Assignment_Statement
9745 and then Name (Par) = N
9746 then
9747 null;
9749 -- Don't do this optimization for the prefix of an attribute or
9750 -- the name of an object renaming declaration since these are
9751 -- contexts where we do not want the value anyway.
9753 elsif (Nkind (Par) = N_Attribute_Reference
9754 and then Prefix (Par) = N)
9755 or else Is_Renamed_Object (N)
9756 then
9757 null;
9759 -- Don't do this optimization if we are within the code for a
9760 -- discriminant check, since the whole point of such a check may
9761 -- be to verify the condition on which the code below depends.
9763 elsif Is_In_Discriminant_Check (N) then
9764 null;
9766 -- Green light to see if we can do the optimization. There is
9767 -- still one condition that inhibits the optimization below but
9768 -- now is the time to check the particular discriminant.
9770 else
9771 -- Loop through discriminants to find the matching discriminant
9772 -- constraint to see if we can copy it.
9774 Disc := First_Discriminant (Ptyp);
9775 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
9776 Discr_Loop : while Present (Dcon) loop
9777 Dval := Node (Dcon);
9779 -- Check if this is the matching discriminant and if the
9780 -- discriminant value is simple enough to make sense to
9781 -- copy. We don't want to copy complex expressions, and
9782 -- indeed to do so can cause trouble (before we put in
9783 -- this guard, a discriminant expression containing an
9784 -- AND THEN was copied, causing problems for coverage
9785 -- analysis tools).
9787 -- However, if the reference is part of the initialization
9788 -- code generated for an object declaration, we must use
9789 -- the discriminant value from the subtype constraint,
9790 -- because the selected component may be a reference to the
9791 -- object being initialized, whose discriminant is not yet
9792 -- set. This only happens in complex cases involving changes
9793 -- or representation.
9795 if Disc = Entity (Selector_Name (N))
9796 and then (Is_Entity_Name (Dval)
9797 or else Compile_Time_Known_Value (Dval)
9798 or else Is_Subtype_Declaration)
9799 then
9800 -- Here we have the matching discriminant. Check for
9801 -- the case of a discriminant of a component that is
9802 -- constrained by an outer discriminant, which cannot
9803 -- be optimized away.
9805 if Denotes_Discriminant
9806 (Dval, Check_Concurrent => True)
9807 then
9808 exit Discr_Loop;
9810 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
9811 and then
9812 Denotes_Discriminant
9813 (Selector_Name (Original_Node (Dval)), True)
9814 then
9815 exit Discr_Loop;
9817 -- Do not retrieve value if constraint is not static. It
9818 -- is generally not useful, and the constraint may be a
9819 -- rewritten outer discriminant in which case it is in
9820 -- fact incorrect.
9822 elsif Is_Entity_Name (Dval)
9823 and then
9824 Nkind (Parent (Entity (Dval))) = N_Object_Declaration
9825 and then Present (Expression (Parent (Entity (Dval))))
9826 and then not
9827 Is_OK_Static_Expression
9828 (Expression (Parent (Entity (Dval))))
9829 then
9830 exit Discr_Loop;
9832 -- In the context of a case statement, the expression may
9833 -- have the base type of the discriminant, and we need to
9834 -- preserve the constraint to avoid spurious errors on
9835 -- missing cases.
9837 elsif Nkind (Parent (N)) = N_Case_Statement
9838 and then Etype (Dval) /= Etype (Disc)
9839 then
9840 Rewrite (N,
9841 Make_Qualified_Expression (Loc,
9842 Subtype_Mark =>
9843 New_Occurrence_Of (Etype (Disc), Loc),
9844 Expression =>
9845 New_Copy_Tree (Dval)));
9846 Analyze_And_Resolve (N, Etype (Disc));
9848 -- In case that comes out as a static expression,
9849 -- reset it (a selected component is never static).
9851 Set_Is_Static_Expression (N, False);
9852 return;
9854 -- Otherwise we can just copy the constraint, but the
9855 -- result is certainly not static. In some cases the
9856 -- discriminant constraint has been analyzed in the
9857 -- context of the original subtype indication, but for
9858 -- itypes the constraint might not have been analyzed
9859 -- yet, and this must be done now.
9861 else
9862 Rewrite (N, New_Copy_Tree (Dval));
9863 Analyze_And_Resolve (N);
9864 Set_Is_Static_Expression (N, False);
9865 return;
9866 end if;
9867 end if;
9869 Next_Elmt (Dcon);
9870 Next_Discriminant (Disc);
9871 end loop Discr_Loop;
9873 -- Note: the above loop should always find a matching
9874 -- discriminant, but if it does not, we just missed an
9875 -- optimization due to some glitch (perhaps a previous
9876 -- error), so ignore.
9878 end if;
9879 end if;
9881 -- The only remaining processing is in the case of a discriminant of
9882 -- a concurrent object, where we rewrite the prefix to denote the
9883 -- corresponding record type. If the type is derived and has renamed
9884 -- discriminants, use corresponding discriminant, which is the one
9885 -- that appears in the corresponding record.
9887 if not Is_Concurrent_Type (Ptyp) then
9888 return;
9889 end if;
9891 Disc := Entity (Selector_Name (N));
9893 if Is_Derived_Type (Ptyp)
9894 and then Present (Corresponding_Discriminant (Disc))
9895 then
9896 Disc := Corresponding_Discriminant (Disc);
9897 end if;
9899 New_N :=
9900 Make_Selected_Component (Loc,
9901 Prefix =>
9902 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
9903 New_Copy_Tree (P)),
9904 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
9906 Rewrite (N, New_N);
9907 Analyze (N);
9908 end if;
9910 -- Set Atomic_Sync_Required if necessary for atomic component
9912 if Nkind (N) = N_Selected_Component then
9913 declare
9914 E : constant Entity_Id := Entity (Selector_Name (N));
9915 Set : Boolean;
9917 begin
9918 -- If component is atomic, but type is not, setting depends on
9919 -- disable/enable state for the component.
9921 if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then
9922 Set := not Atomic_Synchronization_Disabled (E);
9924 -- If component is not atomic, but its type is atomic, setting
9925 -- depends on disable/enable state for the type.
9927 elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9928 Set := not Atomic_Synchronization_Disabled (Etype (E));
9930 -- If both component and type are atomic, we disable if either
9931 -- component or its type have sync disabled.
9933 elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9934 Set := (not Atomic_Synchronization_Disabled (E))
9935 and then
9936 (not Atomic_Synchronization_Disabled (Etype (E)));
9938 else
9939 Set := False;
9940 end if;
9942 -- Set flag if required
9944 if Set then
9945 Activate_Atomic_Synchronization (N);
9946 end if;
9947 end;
9948 end if;
9949 end Expand_N_Selected_Component;
9951 --------------------
9952 -- Expand_N_Slice --
9953 --------------------
9955 procedure Expand_N_Slice (N : Node_Id) is
9956 Loc : constant Source_Ptr := Sloc (N);
9957 Typ : constant Entity_Id := Etype (N);
9959 function Is_Procedure_Actual (N : Node_Id) return Boolean;
9960 -- Check whether the argument is an actual for a procedure call, in
9961 -- which case the expansion of a bit-packed slice is deferred until the
9962 -- call itself is expanded. The reason this is required is that we might
9963 -- have an IN OUT or OUT parameter, and the copy out is essential, and
9964 -- that copy out would be missed if we created a temporary here in
9965 -- Expand_N_Slice. Note that we don't bother to test specifically for an
9966 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
9967 -- is harmless to defer expansion in the IN case, since the call
9968 -- processing will still generate the appropriate copy in operation,
9969 -- which will take care of the slice.
9971 procedure Make_Temporary_For_Slice;
9972 -- Create a named variable for the value of the slice, in cases where
9973 -- the back-end cannot handle it properly, e.g. when packed types or
9974 -- unaligned slices are involved.
9976 -------------------------
9977 -- Is_Procedure_Actual --
9978 -------------------------
9980 function Is_Procedure_Actual (N : Node_Id) return Boolean is
9981 Par : Node_Id := Parent (N);
9983 begin
9984 loop
9985 -- If our parent is a procedure call we can return
9987 if Nkind (Par) = N_Procedure_Call_Statement then
9988 return True;
9990 -- If our parent is a type conversion, keep climbing the tree,
9991 -- since a type conversion can be a procedure actual. Also keep
9992 -- climbing if parameter association or a qualified expression,
9993 -- since these are additional cases that do can appear on
9994 -- procedure actuals.
9996 elsif Nkind_In (Par, N_Type_Conversion,
9997 N_Parameter_Association,
9998 N_Qualified_Expression)
9999 then
10000 Par := Parent (Par);
10002 -- Any other case is not what we are looking for
10004 else
10005 return False;
10006 end if;
10007 end loop;
10008 end Is_Procedure_Actual;
10010 ------------------------------
10011 -- Make_Temporary_For_Slice --
10012 ------------------------------
10014 procedure Make_Temporary_For_Slice is
10015 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
10016 Decl : Node_Id;
10018 begin
10019 Decl :=
10020 Make_Object_Declaration (Loc,
10021 Defining_Identifier => Ent,
10022 Object_Definition => New_Occurrence_Of (Typ, Loc));
10024 Set_No_Initialization (Decl);
10026 Insert_Actions (N, New_List (
10027 Decl,
10028 Make_Assignment_Statement (Loc,
10029 Name => New_Occurrence_Of (Ent, Loc),
10030 Expression => Relocate_Node (N))));
10032 Rewrite (N, New_Occurrence_Of (Ent, Loc));
10033 Analyze_And_Resolve (N, Typ);
10034 end Make_Temporary_For_Slice;
10036 -- Local variables
10038 Pref : constant Node_Id := Prefix (N);
10039 Pref_Typ : Entity_Id := Etype (Pref);
10041 -- Start of processing for Expand_N_Slice
10043 begin
10044 -- Special handling for access types
10046 if Is_Access_Type (Pref_Typ) then
10047 Pref_Typ := Designated_Type (Pref_Typ);
10049 Rewrite (Pref,
10050 Make_Explicit_Dereference (Sloc (N),
10051 Prefix => Relocate_Node (Pref)));
10053 Analyze_And_Resolve (Pref, Pref_Typ);
10054 end if;
10056 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
10057 -- function, then additional actuals must be passed.
10059 if Ada_Version >= Ada_2005
10060 and then Is_Build_In_Place_Function_Call (Pref)
10061 then
10062 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
10063 end if;
10065 -- The remaining case to be handled is packed slices. We can leave
10066 -- packed slices as they are in the following situations:
10068 -- 1. Right or left side of an assignment (we can handle this
10069 -- situation correctly in the assignment statement expansion).
10071 -- 2. Prefix of indexed component (the slide is optimized away in this
10072 -- case, see the start of Expand_N_Slice.)
10074 -- 3. Object renaming declaration, since we want the name of the
10075 -- slice, not the value.
10077 -- 4. Argument to procedure call, since copy-in/copy-out handling may
10078 -- be required, and this is handled in the expansion of call
10079 -- itself.
10081 -- 5. Prefix of an address attribute (this is an error which is caught
10082 -- elsewhere, and the expansion would interfere with generating the
10083 -- error message).
10085 if not Is_Packed (Typ) then
10087 -- Apply transformation for actuals of a function call, where
10088 -- Expand_Actuals is not used.
10090 if Nkind (Parent (N)) = N_Function_Call
10091 and then Is_Possibly_Unaligned_Slice (N)
10092 then
10093 Make_Temporary_For_Slice;
10094 end if;
10096 elsif Nkind (Parent (N)) = N_Assignment_Statement
10097 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
10098 and then Parent (N) = Name (Parent (Parent (N))))
10099 then
10100 return;
10102 elsif Nkind (Parent (N)) = N_Indexed_Component
10103 or else Is_Renamed_Object (N)
10104 or else Is_Procedure_Actual (N)
10105 then
10106 return;
10108 elsif Nkind (Parent (N)) = N_Attribute_Reference
10109 and then Attribute_Name (Parent (N)) = Name_Address
10110 then
10111 return;
10113 else
10114 Make_Temporary_For_Slice;
10115 end if;
10116 end Expand_N_Slice;
10118 ------------------------------
10119 -- Expand_N_Type_Conversion --
10120 ------------------------------
10122 procedure Expand_N_Type_Conversion (N : Node_Id) is
10123 Loc : constant Source_Ptr := Sloc (N);
10124 Operand : constant Node_Id := Expression (N);
10125 Target_Type : constant Entity_Id := Etype (N);
10126 Operand_Type : Entity_Id := Etype (Operand);
10128 procedure Handle_Changed_Representation;
10129 -- This is called in the case of record and array type conversions to
10130 -- see if there is a change of representation to be handled. Change of
10131 -- representation is actually handled at the assignment statement level,
10132 -- and what this procedure does is rewrite node N conversion as an
10133 -- assignment to temporary. If there is no change of representation,
10134 -- then the conversion node is unchanged.
10136 procedure Raise_Accessibility_Error;
10137 -- Called when we know that an accessibility check will fail. Rewrites
10138 -- node N to an appropriate raise statement and outputs warning msgs.
10139 -- The Etype of the raise node is set to Target_Type. Note that in this
10140 -- case the rest of the processing should be skipped (i.e. the call to
10141 -- this procedure will be followed by "goto Done").
10143 procedure Real_Range_Check;
10144 -- Handles generation of range check for real target value
10146 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean;
10147 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
10148 -- evaluates to True.
10150 -----------------------------------
10151 -- Handle_Changed_Representation --
10152 -----------------------------------
10154 procedure Handle_Changed_Representation is
10155 Temp : Entity_Id;
10156 Decl : Node_Id;
10157 Odef : Node_Id;
10158 Disc : Node_Id;
10159 N_Ix : Node_Id;
10160 Cons : List_Id;
10162 begin
10163 -- Nothing else to do if no change of representation
10165 if Same_Representation (Operand_Type, Target_Type) then
10166 return;
10168 -- The real change of representation work is done by the assignment
10169 -- statement processing. So if this type conversion is appearing as
10170 -- the expression of an assignment statement, nothing needs to be
10171 -- done to the conversion.
10173 elsif Nkind (Parent (N)) = N_Assignment_Statement then
10174 return;
10176 -- Otherwise we need to generate a temporary variable, and do the
10177 -- change of representation assignment into that temporary variable.
10178 -- The conversion is then replaced by a reference to this variable.
10180 else
10181 Cons := No_List;
10183 -- If type is unconstrained we have to add a constraint, copied
10184 -- from the actual value of the left hand side.
10186 if not Is_Constrained (Target_Type) then
10187 if Has_Discriminants (Operand_Type) then
10188 Disc := First_Discriminant (Operand_Type);
10190 if Disc /= First_Stored_Discriminant (Operand_Type) then
10191 Disc := First_Stored_Discriminant (Operand_Type);
10192 end if;
10194 Cons := New_List;
10195 while Present (Disc) loop
10196 Append_To (Cons,
10197 Make_Selected_Component (Loc,
10198 Prefix =>
10199 Duplicate_Subexpr_Move_Checks (Operand),
10200 Selector_Name =>
10201 Make_Identifier (Loc, Chars (Disc))));
10202 Next_Discriminant (Disc);
10203 end loop;
10205 elsif Is_Array_Type (Operand_Type) then
10206 N_Ix := First_Index (Target_Type);
10207 Cons := New_List;
10209 for J in 1 .. Number_Dimensions (Operand_Type) loop
10211 -- We convert the bounds explicitly. We use an unchecked
10212 -- conversion because bounds checks are done elsewhere.
10214 Append_To (Cons,
10215 Make_Range (Loc,
10216 Low_Bound =>
10217 Unchecked_Convert_To (Etype (N_Ix),
10218 Make_Attribute_Reference (Loc,
10219 Prefix =>
10220 Duplicate_Subexpr_No_Checks
10221 (Operand, Name_Req => True),
10222 Attribute_Name => Name_First,
10223 Expressions => New_List (
10224 Make_Integer_Literal (Loc, J)))),
10226 High_Bound =>
10227 Unchecked_Convert_To (Etype (N_Ix),
10228 Make_Attribute_Reference (Loc,
10229 Prefix =>
10230 Duplicate_Subexpr_No_Checks
10231 (Operand, Name_Req => True),
10232 Attribute_Name => Name_Last,
10233 Expressions => New_List (
10234 Make_Integer_Literal (Loc, J))))));
10236 Next_Index (N_Ix);
10237 end loop;
10238 end if;
10239 end if;
10241 Odef := New_Occurrence_Of (Target_Type, Loc);
10243 if Present (Cons) then
10244 Odef :=
10245 Make_Subtype_Indication (Loc,
10246 Subtype_Mark => Odef,
10247 Constraint =>
10248 Make_Index_Or_Discriminant_Constraint (Loc,
10249 Constraints => Cons));
10250 end if;
10252 Temp := Make_Temporary (Loc, 'C');
10253 Decl :=
10254 Make_Object_Declaration (Loc,
10255 Defining_Identifier => Temp,
10256 Object_Definition => Odef);
10258 Set_No_Initialization (Decl, True);
10260 -- Insert required actions. It is essential to suppress checks
10261 -- since we have suppressed default initialization, which means
10262 -- that the variable we create may have no discriminants.
10264 Insert_Actions (N,
10265 New_List (
10266 Decl,
10267 Make_Assignment_Statement (Loc,
10268 Name => New_Occurrence_Of (Temp, Loc),
10269 Expression => Relocate_Node (N))),
10270 Suppress => All_Checks);
10272 Rewrite (N, New_Occurrence_Of (Temp, Loc));
10273 return;
10274 end if;
10275 end Handle_Changed_Representation;
10277 -------------------------------
10278 -- Raise_Accessibility_Error --
10279 -------------------------------
10281 procedure Raise_Accessibility_Error is
10282 begin
10283 Error_Msg_Warn := SPARK_Mode /= On;
10284 Rewrite (N,
10285 Make_Raise_Program_Error (Sloc (N),
10286 Reason => PE_Accessibility_Check_Failed));
10287 Set_Etype (N, Target_Type);
10289 Error_Msg_N ("<<accessibility check failure", N);
10290 Error_Msg_NE ("\<<& [", N, Standard_Program_Error);
10291 end Raise_Accessibility_Error;
10293 ----------------------
10294 -- Real_Range_Check --
10295 ----------------------
10297 -- Case of conversions to floating-point or fixed-point. If range checks
10298 -- are enabled and the target type has a range constraint, we convert:
10300 -- typ (x)
10302 -- to
10304 -- Tnn : typ'Base := typ'Base (x);
10305 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
10306 -- Tnn
10308 -- This is necessary when there is a conversion of integer to float or
10309 -- to fixed-point to ensure that the correct checks are made. It is not
10310 -- necessary for float to float where it is enough to simply set the
10311 -- Do_Range_Check flag.
10313 procedure Real_Range_Check is
10314 Btyp : constant Entity_Id := Base_Type (Target_Type);
10315 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
10316 Hi : constant Node_Id := Type_High_Bound (Target_Type);
10317 Xtyp : constant Entity_Id := Etype (Operand);
10318 Conv : Node_Id;
10319 Tnn : Entity_Id;
10321 begin
10322 -- Nothing to do if conversion was rewritten
10324 if Nkind (N) /= N_Type_Conversion then
10325 return;
10326 end if;
10328 -- Nothing to do if range checks suppressed, or target has the same
10329 -- range as the base type (or is the base type).
10331 if Range_Checks_Suppressed (Target_Type)
10332 or else (Lo = Type_Low_Bound (Btyp)
10333 and then
10334 Hi = Type_High_Bound (Btyp))
10335 then
10336 return;
10337 end if;
10339 -- Nothing to do if expression is an entity on which checks have been
10340 -- suppressed.
10342 if Is_Entity_Name (Operand)
10343 and then Range_Checks_Suppressed (Entity (Operand))
10344 then
10345 return;
10346 end if;
10348 -- Nothing to do if bounds are all static and we can tell that the
10349 -- expression is within the bounds of the target. Note that if the
10350 -- operand is of an unconstrained floating-point type, then we do
10351 -- not trust it to be in range (might be infinite)
10353 declare
10354 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
10355 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
10357 begin
10358 if (not Is_Floating_Point_Type (Xtyp)
10359 or else Is_Constrained (Xtyp))
10360 and then Compile_Time_Known_Value (S_Lo)
10361 and then Compile_Time_Known_Value (S_Hi)
10362 and then Compile_Time_Known_Value (Hi)
10363 and then Compile_Time_Known_Value (Lo)
10364 then
10365 declare
10366 D_Lov : constant Ureal := Expr_Value_R (Lo);
10367 D_Hiv : constant Ureal := Expr_Value_R (Hi);
10368 S_Lov : Ureal;
10369 S_Hiv : Ureal;
10371 begin
10372 if Is_Real_Type (Xtyp) then
10373 S_Lov := Expr_Value_R (S_Lo);
10374 S_Hiv := Expr_Value_R (S_Hi);
10375 else
10376 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
10377 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
10378 end if;
10380 if D_Hiv > D_Lov
10381 and then S_Lov >= D_Lov
10382 and then S_Hiv <= D_Hiv
10383 then
10384 -- Unset the range check flag on the current value of
10385 -- Expression (N), since the captured Operand may have
10386 -- been rewritten (such as for the case of a conversion
10387 -- to a fixed-point type).
10389 Set_Do_Range_Check (Expression (N), False);
10391 return;
10392 end if;
10393 end;
10394 end if;
10395 end;
10397 -- For float to float conversions, we are done
10399 if Is_Floating_Point_Type (Xtyp)
10400 and then
10401 Is_Floating_Point_Type (Btyp)
10402 then
10403 return;
10404 end if;
10406 -- Otherwise rewrite the conversion as described above
10408 Conv := Relocate_Node (N);
10409 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
10410 Set_Etype (Conv, Btyp);
10412 -- Enable overflow except for case of integer to float conversions,
10413 -- where it is never required, since we can never have overflow in
10414 -- this case.
10416 if not Is_Integer_Type (Etype (Operand)) then
10417 Enable_Overflow_Check (Conv);
10418 end if;
10420 Tnn := Make_Temporary (Loc, 'T', Conv);
10422 Insert_Actions (N, New_List (
10423 Make_Object_Declaration (Loc,
10424 Defining_Identifier => Tnn,
10425 Object_Definition => New_Occurrence_Of (Btyp, Loc),
10426 Constant_Present => True,
10427 Expression => Conv),
10429 Make_Raise_Constraint_Error (Loc,
10430 Condition =>
10431 Make_Or_Else (Loc,
10432 Left_Opnd =>
10433 Make_Op_Lt (Loc,
10434 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
10435 Right_Opnd =>
10436 Make_Attribute_Reference (Loc,
10437 Attribute_Name => Name_First,
10438 Prefix =>
10439 New_Occurrence_Of (Target_Type, Loc))),
10441 Right_Opnd =>
10442 Make_Op_Gt (Loc,
10443 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
10444 Right_Opnd =>
10445 Make_Attribute_Reference (Loc,
10446 Attribute_Name => Name_Last,
10447 Prefix =>
10448 New_Occurrence_Of (Target_Type, Loc)))),
10449 Reason => CE_Range_Check_Failed)));
10451 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
10452 Analyze_And_Resolve (N, Btyp);
10453 end Real_Range_Check;
10455 -----------------------------
10456 -- Has_Extra_Accessibility --
10457 -----------------------------
10459 -- Returns true for a formal of an anonymous access type or for
10460 -- an Ada 2012-style stand-alone object of an anonymous access type.
10462 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is
10463 begin
10464 if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then
10465 return Present (Effective_Extra_Accessibility (Id));
10466 else
10467 return False;
10468 end if;
10469 end Has_Extra_Accessibility;
10471 -- Start of processing for Expand_N_Type_Conversion
10473 begin
10474 -- First remove check marks put by the semantic analysis on the type
10475 -- conversion between array types. We need these checks, and they will
10476 -- be generated by this expansion routine, but we do not depend on these
10477 -- flags being set, and since we do intend to expand the checks in the
10478 -- front end, we don't want them on the tree passed to the back end.
10480 if Is_Array_Type (Target_Type) then
10481 if Is_Constrained (Target_Type) then
10482 Set_Do_Length_Check (N, False);
10483 else
10484 Set_Do_Range_Check (Operand, False);
10485 end if;
10486 end if;
10488 -- Nothing at all to do if conversion is to the identical type so remove
10489 -- the conversion completely, it is useless, except that it may carry
10490 -- an Assignment_OK attribute, which must be propagated to the operand.
10492 if Operand_Type = Target_Type then
10493 if Assignment_OK (N) then
10494 Set_Assignment_OK (Operand);
10495 end if;
10497 Rewrite (N, Relocate_Node (Operand));
10498 goto Done;
10499 end if;
10501 -- Nothing to do if this is the second argument of read. This is a
10502 -- "backwards" conversion that will be handled by the specialized code
10503 -- in attribute processing.
10505 if Nkind (Parent (N)) = N_Attribute_Reference
10506 and then Attribute_Name (Parent (N)) = Name_Read
10507 and then Next (First (Expressions (Parent (N)))) = N
10508 then
10509 goto Done;
10510 end if;
10512 -- Check for case of converting to a type that has an invariant
10513 -- associated with it. This required an invariant check. We convert
10515 -- typ (expr)
10517 -- into
10519 -- do invariant_check (typ (expr)) in typ (expr);
10521 -- using Duplicate_Subexpr to avoid multiple side effects
10523 -- Note: the Comes_From_Source check, and then the resetting of this
10524 -- flag prevents what would otherwise be an infinite recursion.
10526 if Has_Invariants (Target_Type)
10527 and then Present (Invariant_Procedure (Target_Type))
10528 and then Comes_From_Source (N)
10529 then
10530 Set_Comes_From_Source (N, False);
10531 Rewrite (N,
10532 Make_Expression_With_Actions (Loc,
10533 Actions => New_List (
10534 Make_Invariant_Call (Duplicate_Subexpr (N))),
10535 Expression => Duplicate_Subexpr_No_Checks (N)));
10536 Analyze_And_Resolve (N, Target_Type);
10537 goto Done;
10538 end if;
10540 -- Here if we may need to expand conversion
10542 -- If the operand of the type conversion is an arithmetic operation on
10543 -- signed integers, and the based type of the signed integer type in
10544 -- question is smaller than Standard.Integer, we promote both of the
10545 -- operands to type Integer.
10547 -- For example, if we have
10549 -- target-type (opnd1 + opnd2)
10551 -- and opnd1 and opnd2 are of type short integer, then we rewrite
10552 -- this as:
10554 -- target-type (integer(opnd1) + integer(opnd2))
10556 -- We do this because we are always allowed to compute in a larger type
10557 -- if we do the right thing with the result, and in this case we are
10558 -- going to do a conversion which will do an appropriate check to make
10559 -- sure that things are in range of the target type in any case. This
10560 -- avoids some unnecessary intermediate overflows.
10562 -- We might consider a similar transformation in the case where the
10563 -- target is a real type or a 64-bit integer type, and the operand
10564 -- is an arithmetic operation using a 32-bit integer type. However,
10565 -- we do not bother with this case, because it could cause significant
10566 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
10567 -- much cheaper, but we don't want different behavior on 32-bit and
10568 -- 64-bit machines. Note that the exclusion of the 64-bit case also
10569 -- handles the configurable run-time cases where 64-bit arithmetic
10570 -- may simply be unavailable.
10572 -- Note: this circuit is partially redundant with respect to the circuit
10573 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
10574 -- the processing here. Also we still need the Checks circuit, since we
10575 -- have to be sure not to generate junk overflow checks in the first
10576 -- place, since it would be trick to remove them here.
10578 if Integer_Promotion_Possible (N) then
10580 -- All conditions met, go ahead with transformation
10582 declare
10583 Opnd : Node_Id;
10584 L, R : Node_Id;
10586 begin
10587 R :=
10588 Make_Type_Conversion (Loc,
10589 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
10590 Expression => Relocate_Node (Right_Opnd (Operand)));
10592 Opnd := New_Op_Node (Nkind (Operand), Loc);
10593 Set_Right_Opnd (Opnd, R);
10595 if Nkind (Operand) in N_Binary_Op then
10596 L :=
10597 Make_Type_Conversion (Loc,
10598 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
10599 Expression => Relocate_Node (Left_Opnd (Operand)));
10601 Set_Left_Opnd (Opnd, L);
10602 end if;
10604 Rewrite (N,
10605 Make_Type_Conversion (Loc,
10606 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
10607 Expression => Opnd));
10609 Analyze_And_Resolve (N, Target_Type);
10610 goto Done;
10611 end;
10612 end if;
10614 -- Do validity check if validity checking operands
10616 if Validity_Checks_On and Validity_Check_Operands then
10617 Ensure_Valid (Operand);
10618 end if;
10620 -- Special case of converting from non-standard boolean type
10622 if Is_Boolean_Type (Operand_Type)
10623 and then (Nonzero_Is_True (Operand_Type))
10624 then
10625 Adjust_Condition (Operand);
10626 Set_Etype (Operand, Standard_Boolean);
10627 Operand_Type := Standard_Boolean;
10628 end if;
10630 -- Case of converting to an access type
10632 if Is_Access_Type (Target_Type) then
10634 -- Apply an accessibility check when the conversion operand is an
10635 -- access parameter (or a renaming thereof), unless conversion was
10636 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
10637 -- Note that other checks may still need to be applied below (such
10638 -- as tagged type checks).
10640 if Is_Entity_Name (Operand)
10641 and then Has_Extra_Accessibility (Entity (Operand))
10642 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
10643 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
10644 or else Attribute_Name (Original_Node (N)) = Name_Access)
10645 then
10646 Apply_Accessibility_Check
10647 (Operand, Target_Type, Insert_Node => Operand);
10649 -- If the level of the operand type is statically deeper than the
10650 -- level of the target type, then force Program_Error. Note that this
10651 -- can only occur for cases where the attribute is within the body of
10652 -- an instantiation, otherwise the conversion will already have been
10653 -- rejected as illegal.
10655 -- Note: warnings are issued by the analyzer for the instance cases
10657 elsif In_Instance_Body
10659 -- The case where the target type is an anonymous access type of
10660 -- a discriminant is excluded, because the level of such a type
10661 -- depends on the context and currently the level returned for such
10662 -- types is zero, resulting in warnings about about check failures
10663 -- in certain legal cases involving class-wide interfaces as the
10664 -- designated type (some cases, such as return statements, are
10665 -- checked at run time, but not clear if these are handled right
10666 -- in general, see 3.10.2(12/2-12.5/3) ???).
10668 and then
10669 not (Ekind (Target_Type) = E_Anonymous_Access_Type
10670 and then Present (Associated_Node_For_Itype (Target_Type))
10671 and then Nkind (Associated_Node_For_Itype (Target_Type)) =
10672 N_Discriminant_Specification)
10673 and then
10674 Type_Access_Level (Operand_Type) > Type_Access_Level (Target_Type)
10675 then
10676 Raise_Accessibility_Error;
10677 goto Done;
10679 -- When the operand is a selected access discriminant the check needs
10680 -- to be made against the level of the object denoted by the prefix
10681 -- of the selected name. Force Program_Error for this case as well
10682 -- (this accessibility violation can only happen if within the body
10683 -- of an instantiation).
10685 elsif In_Instance_Body
10686 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
10687 and then Nkind (Operand) = N_Selected_Component
10688 and then Object_Access_Level (Operand) >
10689 Type_Access_Level (Target_Type)
10690 then
10691 Raise_Accessibility_Error;
10692 goto Done;
10693 end if;
10694 end if;
10696 -- Case of conversions of tagged types and access to tagged types
10698 -- When needed, that is to say when the expression is class-wide, Add
10699 -- runtime a tag check for (strict) downward conversion by using the
10700 -- membership test, generating:
10702 -- [constraint_error when Operand not in Target_Type'Class]
10704 -- or in the access type case
10706 -- [constraint_error
10707 -- when Operand /= null
10708 -- and then Operand.all not in
10709 -- Designated_Type (Target_Type)'Class]
10711 if (Is_Access_Type (Target_Type)
10712 and then Is_Tagged_Type (Designated_Type (Target_Type)))
10713 or else Is_Tagged_Type (Target_Type)
10714 then
10715 -- Do not do any expansion in the access type case if the parent is a
10716 -- renaming, since this is an error situation which will be caught by
10717 -- Sem_Ch8, and the expansion can interfere with this error check.
10719 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
10720 goto Done;
10721 end if;
10723 -- Otherwise, proceed with processing tagged conversion
10725 Tagged_Conversion : declare
10726 Actual_Op_Typ : Entity_Id;
10727 Actual_Targ_Typ : Entity_Id;
10728 Make_Conversion : Boolean := False;
10729 Root_Op_Typ : Entity_Id;
10731 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
10732 -- Create a membership check to test whether Operand is a member
10733 -- of Targ_Typ. If the original Target_Type is an access, include
10734 -- a test for null value. The check is inserted at N.
10736 --------------------
10737 -- Make_Tag_Check --
10738 --------------------
10740 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
10741 Cond : Node_Id;
10743 begin
10744 -- Generate:
10745 -- [Constraint_Error
10746 -- when Operand /= null
10747 -- and then Operand.all not in Targ_Typ]
10749 if Is_Access_Type (Target_Type) then
10750 Cond :=
10751 Make_And_Then (Loc,
10752 Left_Opnd =>
10753 Make_Op_Ne (Loc,
10754 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10755 Right_Opnd => Make_Null (Loc)),
10757 Right_Opnd =>
10758 Make_Not_In (Loc,
10759 Left_Opnd =>
10760 Make_Explicit_Dereference (Loc,
10761 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
10762 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc)));
10764 -- Generate:
10765 -- [Constraint_Error when Operand not in Targ_Typ]
10767 else
10768 Cond :=
10769 Make_Not_In (Loc,
10770 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10771 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc));
10772 end if;
10774 Insert_Action (N,
10775 Make_Raise_Constraint_Error (Loc,
10776 Condition => Cond,
10777 Reason => CE_Tag_Check_Failed));
10778 end Make_Tag_Check;
10780 -- Start of processing for Tagged_Conversion
10782 begin
10783 -- Handle entities from the limited view
10785 if Is_Access_Type (Operand_Type) then
10786 Actual_Op_Typ :=
10787 Available_View (Designated_Type (Operand_Type));
10788 else
10789 Actual_Op_Typ := Operand_Type;
10790 end if;
10792 if Is_Access_Type (Target_Type) then
10793 Actual_Targ_Typ :=
10794 Available_View (Designated_Type (Target_Type));
10795 else
10796 Actual_Targ_Typ := Target_Type;
10797 end if;
10799 Root_Op_Typ := Root_Type (Actual_Op_Typ);
10801 -- Ada 2005 (AI-251): Handle interface type conversion
10803 if Is_Interface (Actual_Op_Typ)
10804 or else
10805 Is_Interface (Actual_Targ_Typ)
10806 then
10807 Expand_Interface_Conversion (N);
10808 goto Done;
10809 end if;
10811 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
10813 -- Create a runtime tag check for a downward class-wide type
10814 -- conversion.
10816 if Is_Class_Wide_Type (Actual_Op_Typ)
10817 and then Actual_Op_Typ /= Actual_Targ_Typ
10818 and then Root_Op_Typ /= Actual_Targ_Typ
10819 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ,
10820 Use_Full_View => True)
10821 then
10822 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
10823 Make_Conversion := True;
10824 end if;
10826 -- AI05-0073: If the result subtype of the function is defined
10827 -- by an access_definition designating a specific tagged type
10828 -- T, a check is made that the result value is null or the tag
10829 -- of the object designated by the result value identifies T.
10830 -- Constraint_Error is raised if this check fails.
10832 if Nkind (Parent (N)) = N_Simple_Return_Statement then
10833 declare
10834 Func : Entity_Id;
10835 Func_Typ : Entity_Id;
10837 begin
10838 -- Climb scope stack looking for the enclosing function
10840 Func := Current_Scope;
10841 while Present (Func)
10842 and then Ekind (Func) /= E_Function
10843 loop
10844 Func := Scope (Func);
10845 end loop;
10847 -- The function's return subtype must be defined using
10848 -- an access definition.
10850 if Nkind (Result_Definition (Parent (Func))) =
10851 N_Access_Definition
10852 then
10853 Func_Typ := Directly_Designated_Type (Etype (Func));
10855 -- The return subtype denotes a specific tagged type,
10856 -- in other words, a non class-wide type.
10858 if Is_Tagged_Type (Func_Typ)
10859 and then not Is_Class_Wide_Type (Func_Typ)
10860 then
10861 Make_Tag_Check (Actual_Targ_Typ);
10862 Make_Conversion := True;
10863 end if;
10864 end if;
10865 end;
10866 end if;
10868 -- We have generated a tag check for either a class-wide type
10869 -- conversion or for AI05-0073.
10871 if Make_Conversion then
10872 declare
10873 Conv : Node_Id;
10874 begin
10875 Conv :=
10876 Make_Unchecked_Type_Conversion (Loc,
10877 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
10878 Expression => Relocate_Node (Expression (N)));
10879 Rewrite (N, Conv);
10880 Analyze_And_Resolve (N, Target_Type);
10881 end;
10882 end if;
10883 end if;
10884 end Tagged_Conversion;
10886 -- Case of other access type conversions
10888 elsif Is_Access_Type (Target_Type) then
10889 Apply_Constraint_Check (Operand, Target_Type);
10891 -- Case of conversions from a fixed-point type
10893 -- These conversions require special expansion and processing, found in
10894 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
10895 -- since from a semantic point of view, these are simple integer
10896 -- conversions, which do not need further processing.
10898 elsif Is_Fixed_Point_Type (Operand_Type)
10899 and then not Conversion_OK (N)
10900 then
10901 -- We should never see universal fixed at this case, since the
10902 -- expansion of the constituent divide or multiply should have
10903 -- eliminated the explicit mention of universal fixed.
10905 pragma Assert (Operand_Type /= Universal_Fixed);
10907 -- Check for special case of the conversion to universal real that
10908 -- occurs as a result of the use of a round attribute. In this case,
10909 -- the real type for the conversion is taken from the target type of
10910 -- the Round attribute and the result must be marked as rounded.
10912 if Target_Type = Universal_Real
10913 and then Nkind (Parent (N)) = N_Attribute_Reference
10914 and then Attribute_Name (Parent (N)) = Name_Round
10915 then
10916 Set_Rounded_Result (N);
10917 Set_Etype (N, Etype (Parent (N)));
10918 end if;
10920 -- Otherwise do correct fixed-conversion, but skip these if the
10921 -- Conversion_OK flag is set, because from a semantic point of view
10922 -- these are simple integer conversions needing no further processing
10923 -- (the backend will simply treat them as integers).
10925 if not Conversion_OK (N) then
10926 if Is_Fixed_Point_Type (Etype (N)) then
10927 Expand_Convert_Fixed_To_Fixed (N);
10928 Real_Range_Check;
10930 elsif Is_Integer_Type (Etype (N)) then
10931 Expand_Convert_Fixed_To_Integer (N);
10933 else
10934 pragma Assert (Is_Floating_Point_Type (Etype (N)));
10935 Expand_Convert_Fixed_To_Float (N);
10936 Real_Range_Check;
10937 end if;
10938 end if;
10940 -- Case of conversions to a fixed-point type
10942 -- These conversions require special expansion and processing, found in
10943 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
10944 -- since from a semantic point of view, these are simple integer
10945 -- conversions, which do not need further processing.
10947 elsif Is_Fixed_Point_Type (Target_Type)
10948 and then not Conversion_OK (N)
10949 then
10950 if Is_Integer_Type (Operand_Type) then
10951 Expand_Convert_Integer_To_Fixed (N);
10952 Real_Range_Check;
10953 else
10954 pragma Assert (Is_Floating_Point_Type (Operand_Type));
10955 Expand_Convert_Float_To_Fixed (N);
10956 Real_Range_Check;
10957 end if;
10959 -- Case of float-to-integer conversions
10961 -- We also handle float-to-fixed conversions with Conversion_OK set
10962 -- since semantically the fixed-point target is treated as though it
10963 -- were an integer in such cases.
10965 elsif Is_Floating_Point_Type (Operand_Type)
10966 and then
10967 (Is_Integer_Type (Target_Type)
10968 or else
10969 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
10970 then
10971 -- One more check here, gcc is still not able to do conversions of
10972 -- this type with proper overflow checking, and so gigi is doing an
10973 -- approximation of what is required by doing floating-point compares
10974 -- with the end-point. But that can lose precision in some cases, and
10975 -- give a wrong result. Converting the operand to Universal_Real is
10976 -- helpful, but still does not catch all cases with 64-bit integers
10977 -- on targets with only 64-bit floats.
10979 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
10980 -- Can this code be removed ???
10982 if Do_Range_Check (Operand) then
10983 Rewrite (Operand,
10984 Make_Type_Conversion (Loc,
10985 Subtype_Mark =>
10986 New_Occurrence_Of (Universal_Real, Loc),
10987 Expression =>
10988 Relocate_Node (Operand)));
10990 Set_Etype (Operand, Universal_Real);
10991 Enable_Range_Check (Operand);
10992 Set_Do_Range_Check (Expression (Operand), False);
10993 end if;
10995 -- Case of array conversions
10997 -- Expansion of array conversions, add required length/range checks but
10998 -- only do this if there is no change of representation. For handling of
10999 -- this case, see Handle_Changed_Representation.
11001 elsif Is_Array_Type (Target_Type) then
11002 if Is_Constrained (Target_Type) then
11003 Apply_Length_Check (Operand, Target_Type);
11004 else
11005 Apply_Range_Check (Operand, Target_Type);
11006 end if;
11008 Handle_Changed_Representation;
11010 -- Case of conversions of discriminated types
11012 -- Add required discriminant checks if target is constrained. Again this
11013 -- change is skipped if we have a change of representation.
11015 elsif Has_Discriminants (Target_Type)
11016 and then Is_Constrained (Target_Type)
11017 then
11018 Apply_Discriminant_Check (Operand, Target_Type);
11019 Handle_Changed_Representation;
11021 -- Case of all other record conversions. The only processing required
11022 -- is to check for a change of representation requiring the special
11023 -- assignment processing.
11025 elsif Is_Record_Type (Target_Type) then
11027 -- Ada 2005 (AI-216): Program_Error is raised when converting from
11028 -- a derived Unchecked_Union type to an unconstrained type that is
11029 -- not Unchecked_Union if the operand lacks inferable discriminants.
11031 if Is_Derived_Type (Operand_Type)
11032 and then Is_Unchecked_Union (Base_Type (Operand_Type))
11033 and then not Is_Constrained (Target_Type)
11034 and then not Is_Unchecked_Union (Base_Type (Target_Type))
11035 and then not Has_Inferable_Discriminants (Operand)
11036 then
11037 -- To prevent Gigi from generating illegal code, we generate a
11038 -- Program_Error node, but we give it the target type of the
11039 -- conversion (is this requirement documented somewhere ???)
11041 declare
11042 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
11043 Reason => PE_Unchecked_Union_Restriction);
11045 begin
11046 Set_Etype (PE, Target_Type);
11047 Rewrite (N, PE);
11049 end;
11050 else
11051 Handle_Changed_Representation;
11052 end if;
11054 -- Case of conversions of enumeration types
11056 elsif Is_Enumeration_Type (Target_Type) then
11058 -- Special processing is required if there is a change of
11059 -- representation (from enumeration representation clauses).
11061 if not Same_Representation (Target_Type, Operand_Type) then
11063 -- Convert: x(y) to x'val (ytyp'val (y))
11065 Rewrite (N,
11066 Make_Attribute_Reference (Loc,
11067 Prefix => New_Occurrence_Of (Target_Type, Loc),
11068 Attribute_Name => Name_Val,
11069 Expressions => New_List (
11070 Make_Attribute_Reference (Loc,
11071 Prefix => New_Occurrence_Of (Operand_Type, Loc),
11072 Attribute_Name => Name_Pos,
11073 Expressions => New_List (Operand)))));
11075 Analyze_And_Resolve (N, Target_Type);
11076 end if;
11078 -- Case of conversions to floating-point
11080 elsif Is_Floating_Point_Type (Target_Type) then
11081 Real_Range_Check;
11082 end if;
11084 -- At this stage, either the conversion node has been transformed into
11085 -- some other equivalent expression, or left as a conversion that can be
11086 -- handled by Gigi, in the following cases:
11088 -- Conversions with no change of representation or type
11090 -- Numeric conversions involving integer, floating- and fixed-point
11091 -- values. Fixed-point values are allowed only if Conversion_OK is
11092 -- set, i.e. if the fixed-point values are to be treated as integers.
11094 -- No other conversions should be passed to Gigi
11096 -- Check: are these rules stated in sinfo??? if so, why restate here???
11098 -- The only remaining step is to generate a range check if we still have
11099 -- a type conversion at this stage and Do_Range_Check is set. For now we
11100 -- do this only for conversions of discrete types and for float-to-float
11101 -- conversions.
11103 if Nkind (N) = N_Type_Conversion then
11105 -- For now we only support floating-point cases where both source
11106 -- and target are floating-point types. Conversions where the source
11107 -- and target involve integer or fixed-point types are still TBD,
11108 -- though not clear whether those can even happen at this point, due
11109 -- to transformations above. ???
11111 if Is_Floating_Point_Type (Etype (N))
11112 and then Is_Floating_Point_Type (Etype (Expression (N)))
11113 then
11114 if Do_Range_Check (Expression (N))
11115 and then Is_Floating_Point_Type (Target_Type)
11116 then
11117 Generate_Range_Check
11118 (Expression (N), Target_Type, CE_Range_Check_Failed);
11119 end if;
11121 -- Discrete-to-discrete conversions
11123 elsif Is_Discrete_Type (Etype (N)) then
11124 declare
11125 Expr : constant Node_Id := Expression (N);
11126 Ftyp : Entity_Id;
11127 Ityp : Entity_Id;
11129 begin
11130 if Do_Range_Check (Expr)
11131 and then Is_Discrete_Type (Etype (Expr))
11132 then
11133 Set_Do_Range_Check (Expr, False);
11135 -- Before we do a range check, we have to deal with treating
11136 -- a fixed-point operand as an integer. The way we do this
11137 -- is simply to do an unchecked conversion to an appropriate
11138 -- integer type large enough to hold the result.
11140 -- This code is not active yet, because we are only dealing
11141 -- with discrete types so far ???
11143 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
11144 and then Treat_Fixed_As_Integer (Expr)
11145 then
11146 Ftyp := Base_Type (Etype (Expr));
11148 if Esize (Ftyp) >= Esize (Standard_Integer) then
11149 Ityp := Standard_Long_Long_Integer;
11150 else
11151 Ityp := Standard_Integer;
11152 end if;
11154 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
11155 end if;
11157 -- Reset overflow flag, since the range check will include
11158 -- dealing with possible overflow, and generate the check.
11159 -- If Address is either a source type or target type,
11160 -- suppress range check to avoid typing anomalies when
11161 -- it is a visible integer type.
11163 Set_Do_Overflow_Check (N, False);
11165 if not Is_Descendent_Of_Address (Etype (Expr))
11166 and then not Is_Descendent_Of_Address (Target_Type)
11167 then
11168 Generate_Range_Check
11169 (Expr, Target_Type, CE_Range_Check_Failed);
11170 end if;
11171 end if;
11172 end;
11173 end if;
11174 end if;
11176 -- Here at end of processing
11178 <<Done>>
11179 -- Apply predicate check if required. Note that we can't just call
11180 -- Apply_Predicate_Check here, because the type looks right after
11181 -- the conversion and it would omit the check. The Comes_From_Source
11182 -- guard is necessary to prevent infinite recursions when we generate
11183 -- internal conversions for the purpose of checking predicates.
11185 if Present (Predicate_Function (Target_Type))
11186 and then Target_Type /= Operand_Type
11187 and then Comes_From_Source (N)
11188 then
11189 declare
11190 New_Expr : constant Node_Id := Duplicate_Subexpr (N);
11192 begin
11193 -- Avoid infinite recursion on the subsequent expansion of
11194 -- of the copy of the original type conversion.
11196 Set_Comes_From_Source (New_Expr, False);
11197 Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr));
11198 end;
11199 end if;
11200 end Expand_N_Type_Conversion;
11202 -----------------------------------
11203 -- Expand_N_Unchecked_Expression --
11204 -----------------------------------
11206 -- Remove the unchecked expression node from the tree. Its job was simply
11207 -- to make sure that its constituent expression was handled with checks
11208 -- off, and now that that is done, we can remove it from the tree, and
11209 -- indeed must, since Gigi does not expect to see these nodes.
11211 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
11212 Exp : constant Node_Id := Expression (N);
11213 begin
11214 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
11215 Rewrite (N, Exp);
11216 end Expand_N_Unchecked_Expression;
11218 ----------------------------------------
11219 -- Expand_N_Unchecked_Type_Conversion --
11220 ----------------------------------------
11222 -- If this cannot be handled by Gigi and we haven't already made a
11223 -- temporary for it, do it now.
11225 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
11226 Target_Type : constant Entity_Id := Etype (N);
11227 Operand : constant Node_Id := Expression (N);
11228 Operand_Type : constant Entity_Id := Etype (Operand);
11230 begin
11231 -- Nothing at all to do if conversion is to the identical type so remove
11232 -- the conversion completely, it is useless, except that it may carry
11233 -- an Assignment_OK indication which must be propagated to the operand.
11235 if Operand_Type = Target_Type then
11237 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
11239 if Assignment_OK (N) then
11240 Set_Assignment_OK (Operand);
11241 end if;
11243 Rewrite (N, Relocate_Node (Operand));
11244 return;
11245 end if;
11247 -- If we have a conversion of a compile time known value to a target
11248 -- type and the value is in range of the target type, then we can simply
11249 -- replace the construct by an integer literal of the correct type. We
11250 -- only apply this to integer types being converted. Possibly it may
11251 -- apply in other cases, but it is too much trouble to worry about.
11253 -- Note that we do not do this transformation if the Kill_Range_Check
11254 -- flag is set, since then the value may be outside the expected range.
11255 -- This happens in the Normalize_Scalars case.
11257 -- We also skip this if either the target or operand type is biased
11258 -- because in this case, the unchecked conversion is supposed to
11259 -- preserve the bit pattern, not the integer value.
11261 if Is_Integer_Type (Target_Type)
11262 and then not Has_Biased_Representation (Target_Type)
11263 and then Is_Integer_Type (Operand_Type)
11264 and then not Has_Biased_Representation (Operand_Type)
11265 and then Compile_Time_Known_Value (Operand)
11266 and then not Kill_Range_Check (N)
11267 then
11268 declare
11269 Val : constant Uint := Expr_Value (Operand);
11271 begin
11272 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
11273 and then
11274 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
11275 and then
11276 Val >= Expr_Value (Type_Low_Bound (Target_Type))
11277 and then
11278 Val <= Expr_Value (Type_High_Bound (Target_Type))
11279 then
11280 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
11282 -- If Address is the target type, just set the type to avoid a
11283 -- spurious type error on the literal when Address is a visible
11284 -- integer type.
11286 if Is_Descendent_Of_Address (Target_Type) then
11287 Set_Etype (N, Target_Type);
11288 else
11289 Analyze_And_Resolve (N, Target_Type);
11290 end if;
11292 return;
11293 end if;
11294 end;
11295 end if;
11297 -- Nothing to do if conversion is safe
11299 if Safe_Unchecked_Type_Conversion (N) then
11300 return;
11301 end if;
11303 -- Otherwise force evaluation unless Assignment_OK flag is set (this
11304 -- flag indicates ??? More comments needed here)
11306 if Assignment_OK (N) then
11307 null;
11308 else
11309 Force_Evaluation (N);
11310 end if;
11311 end Expand_N_Unchecked_Type_Conversion;
11313 ----------------------------
11314 -- Expand_Record_Equality --
11315 ----------------------------
11317 -- For non-variant records, Equality is expanded when needed into:
11319 -- and then Lhs.Discr1 = Rhs.Discr1
11320 -- and then ...
11321 -- and then Lhs.Discrn = Rhs.Discrn
11322 -- and then Lhs.Cmp1 = Rhs.Cmp1
11323 -- and then ...
11324 -- and then Lhs.Cmpn = Rhs.Cmpn
11326 -- The expression is folded by the back-end for adjacent fields. This
11327 -- function is called for tagged record in only one occasion: for imple-
11328 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
11329 -- otherwise the primitive "=" is used directly.
11331 function Expand_Record_Equality
11332 (Nod : Node_Id;
11333 Typ : Entity_Id;
11334 Lhs : Node_Id;
11335 Rhs : Node_Id;
11336 Bodies : List_Id) return Node_Id
11338 Loc : constant Source_Ptr := Sloc (Nod);
11340 Result : Node_Id;
11341 C : Entity_Id;
11343 First_Time : Boolean := True;
11345 function Element_To_Compare (C : Entity_Id) return Entity_Id;
11346 -- Return the next discriminant or component to compare, starting with
11347 -- C, skipping inherited components.
11349 ------------------------
11350 -- Element_To_Compare --
11351 ------------------------
11353 function Element_To_Compare (C : Entity_Id) return Entity_Id is
11354 Comp : Entity_Id;
11356 begin
11357 Comp := C;
11358 loop
11359 -- Exit loop when the next element to be compared is found, or
11360 -- there is no more such element.
11362 exit when No (Comp);
11364 exit when Ekind_In (Comp, E_Discriminant, E_Component)
11365 and then not (
11367 -- Skip inherited components
11369 -- Note: for a tagged type, we always generate the "=" primitive
11370 -- for the base type (not on the first subtype), so the test for
11371 -- Comp /= Original_Record_Component (Comp) is True for
11372 -- inherited components only.
11374 (Is_Tagged_Type (Typ)
11375 and then Comp /= Original_Record_Component (Comp))
11377 -- Skip _Tag
11379 or else Chars (Comp) = Name_uTag
11381 -- The .NET/JVM version of type Root_Controlled contains two
11382 -- fields which should not be considered part of the object. To
11383 -- achieve proper equiality between two controlled objects on
11384 -- .NET/JVM, skip _Parent whenever it has type Root_Controlled.
11386 or else (Chars (Comp) = Name_uParent
11387 and then VM_Target /= No_VM
11388 and then Etype (Comp) = RTE (RE_Root_Controlled))
11390 -- Skip interface elements (secondary tags???)
11392 or else Is_Interface (Etype (Comp)));
11394 Next_Entity (Comp);
11395 end loop;
11397 return Comp;
11398 end Element_To_Compare;
11400 -- Start of processing for Expand_Record_Equality
11402 begin
11403 -- Generates the following code: (assuming that Typ has one Discr and
11404 -- component C2 is also a record)
11406 -- True
11407 -- and then Lhs.Discr1 = Rhs.Discr1
11408 -- and then Lhs.C1 = Rhs.C1
11409 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
11410 -- and then ...
11411 -- and then Lhs.Cmpn = Rhs.Cmpn
11413 Result := New_Occurrence_Of (Standard_True, Loc);
11414 C := Element_To_Compare (First_Entity (Typ));
11415 while Present (C) loop
11416 declare
11417 New_Lhs : Node_Id;
11418 New_Rhs : Node_Id;
11419 Check : Node_Id;
11421 begin
11422 if First_Time then
11423 First_Time := False;
11424 New_Lhs := Lhs;
11425 New_Rhs := Rhs;
11426 else
11427 New_Lhs := New_Copy_Tree (Lhs);
11428 New_Rhs := New_Copy_Tree (Rhs);
11429 end if;
11431 Check :=
11432 Expand_Composite_Equality (Nod, Etype (C),
11433 Lhs =>
11434 Make_Selected_Component (Loc,
11435 Prefix => New_Lhs,
11436 Selector_Name => New_Occurrence_Of (C, Loc)),
11437 Rhs =>
11438 Make_Selected_Component (Loc,
11439 Prefix => New_Rhs,
11440 Selector_Name => New_Occurrence_Of (C, Loc)),
11441 Bodies => Bodies);
11443 -- If some (sub)component is an unchecked_union, the whole
11444 -- operation will raise program error.
11446 if Nkind (Check) = N_Raise_Program_Error then
11447 Result := Check;
11448 Set_Etype (Result, Standard_Boolean);
11449 exit;
11450 else
11451 Result :=
11452 Make_And_Then (Loc,
11453 Left_Opnd => Result,
11454 Right_Opnd => Check);
11455 end if;
11456 end;
11458 C := Element_To_Compare (Next_Entity (C));
11459 end loop;
11461 return Result;
11462 end Expand_Record_Equality;
11464 ---------------------------
11465 -- Expand_Set_Membership --
11466 ---------------------------
11468 procedure Expand_Set_Membership (N : Node_Id) is
11469 Lop : constant Node_Id := Left_Opnd (N);
11470 Alt : Node_Id;
11471 Res : Node_Id;
11473 function Make_Cond (Alt : Node_Id) return Node_Id;
11474 -- If the alternative is a subtype mark, create a simple membership
11475 -- test. Otherwise create an equality test for it.
11477 ---------------
11478 -- Make_Cond --
11479 ---------------
11481 function Make_Cond (Alt : Node_Id) return Node_Id is
11482 Cond : Node_Id;
11483 L : constant Node_Id := New_Copy (Lop);
11484 R : constant Node_Id := Relocate_Node (Alt);
11486 begin
11487 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
11488 or else Nkind (Alt) = N_Range
11489 then
11490 Cond :=
11491 Make_In (Sloc (Alt),
11492 Left_Opnd => L,
11493 Right_Opnd => R);
11494 else
11495 Cond :=
11496 Make_Op_Eq (Sloc (Alt),
11497 Left_Opnd => L,
11498 Right_Opnd => R);
11499 end if;
11501 return Cond;
11502 end Make_Cond;
11504 -- Start of processing for Expand_Set_Membership
11506 begin
11507 Remove_Side_Effects (Lop);
11509 Alt := Last (Alternatives (N));
11510 Res := Make_Cond (Alt);
11512 Prev (Alt);
11513 while Present (Alt) loop
11514 Res :=
11515 Make_Or_Else (Sloc (Alt),
11516 Left_Opnd => Make_Cond (Alt),
11517 Right_Opnd => Res);
11518 Prev (Alt);
11519 end loop;
11521 Rewrite (N, Res);
11522 Analyze_And_Resolve (N, Standard_Boolean);
11523 end Expand_Set_Membership;
11525 -----------------------------------
11526 -- Expand_Short_Circuit_Operator --
11527 -----------------------------------
11529 -- Deal with special expansion if actions are present for the right operand
11530 -- and deal with optimizing case of arguments being True or False. We also
11531 -- deal with the special case of non-standard boolean values.
11533 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
11534 Loc : constant Source_Ptr := Sloc (N);
11535 Typ : constant Entity_Id := Etype (N);
11536 Left : constant Node_Id := Left_Opnd (N);
11537 Right : constant Node_Id := Right_Opnd (N);
11538 LocR : constant Source_Ptr := Sloc (Right);
11539 Actlist : List_Id;
11541 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
11542 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
11543 -- If Left = Shortcut_Value then Right need not be evaluated
11545 begin
11546 -- Deal with non-standard booleans
11548 if Is_Boolean_Type (Typ) then
11549 Adjust_Condition (Left);
11550 Adjust_Condition (Right);
11551 Set_Etype (N, Standard_Boolean);
11552 end if;
11554 -- Check for cases where left argument is known to be True or False
11556 if Compile_Time_Known_Value (Left) then
11558 -- Mark SCO for left condition as compile time known
11560 if Generate_SCO and then Comes_From_Source (Left) then
11561 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
11562 end if;
11564 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
11565 -- Any actions associated with Right will be executed unconditionally
11566 -- and can thus be inserted into the tree unconditionally.
11568 if Expr_Value_E (Left) /= Shortcut_Ent then
11569 if Present (Actions (N)) then
11570 Insert_Actions (N, Actions (N));
11571 end if;
11573 Rewrite (N, Right);
11575 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
11576 -- In this case we can forget the actions associated with Right,
11577 -- since they will never be executed.
11579 else
11580 Kill_Dead_Code (Right);
11581 Kill_Dead_Code (Actions (N));
11582 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
11583 end if;
11585 Adjust_Result_Type (N, Typ);
11586 return;
11587 end if;
11589 -- If Actions are present for the right operand, we have to do some
11590 -- special processing. We can't just let these actions filter back into
11591 -- code preceding the short circuit (which is what would have happened
11592 -- if we had not trapped them in the short-circuit form), since they
11593 -- must only be executed if the right operand of the short circuit is
11594 -- executed and not otherwise.
11596 if Present (Actions (N)) then
11597 Actlist := Actions (N);
11599 -- We now use an Expression_With_Actions node for the right operand
11600 -- of the short-circuit form. Note that this solves the traceability
11601 -- problems for coverage analysis.
11603 Rewrite (Right,
11604 Make_Expression_With_Actions (LocR,
11605 Expression => Relocate_Node (Right),
11606 Actions => Actlist));
11608 Set_Actions (N, No_List);
11609 Analyze_And_Resolve (Right, Standard_Boolean);
11611 Adjust_Result_Type (N, Typ);
11612 return;
11613 end if;
11615 -- No actions present, check for cases of right argument True/False
11617 if Compile_Time_Known_Value (Right) then
11619 -- Mark SCO for left condition as compile time known
11621 if Generate_SCO and then Comes_From_Source (Right) then
11622 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
11623 end if;
11625 -- Change (Left and then True), (Left or else False) to Left.
11626 -- Note that we know there are no actions associated with the right
11627 -- operand, since we just checked for this case above.
11629 if Expr_Value_E (Right) /= Shortcut_Ent then
11630 Rewrite (N, Left);
11632 -- Change (Left and then False), (Left or else True) to Right,
11633 -- making sure to preserve any side effects associated with the Left
11634 -- operand.
11636 else
11637 Remove_Side_Effects (Left);
11638 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
11639 end if;
11640 end if;
11642 Adjust_Result_Type (N, Typ);
11643 end Expand_Short_Circuit_Operator;
11645 -------------------------------------
11646 -- Fixup_Universal_Fixed_Operation --
11647 -------------------------------------
11649 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
11650 Conv : constant Node_Id := Parent (N);
11652 begin
11653 -- We must have a type conversion immediately above us
11655 pragma Assert (Nkind (Conv) = N_Type_Conversion);
11657 -- Normally the type conversion gives our target type. The exception
11658 -- occurs in the case of the Round attribute, where the conversion
11659 -- will be to universal real, and our real type comes from the Round
11660 -- attribute (as well as an indication that we must round the result)
11662 if Nkind (Parent (Conv)) = N_Attribute_Reference
11663 and then Attribute_Name (Parent (Conv)) = Name_Round
11664 then
11665 Set_Etype (N, Etype (Parent (Conv)));
11666 Set_Rounded_Result (N);
11668 -- Normal case where type comes from conversion above us
11670 else
11671 Set_Etype (N, Etype (Conv));
11672 end if;
11673 end Fixup_Universal_Fixed_Operation;
11675 ---------------------------------
11676 -- Has_Inferable_Discriminants --
11677 ---------------------------------
11679 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
11681 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
11682 -- Determines whether the left-most prefix of a selected component is a
11683 -- formal parameter in a subprogram. Assumes N is a selected component.
11685 --------------------------------
11686 -- Prefix_Is_Formal_Parameter --
11687 --------------------------------
11689 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
11690 Sel_Comp : Node_Id;
11692 begin
11693 -- Move to the left-most prefix by climbing up the tree
11695 Sel_Comp := N;
11696 while Present (Parent (Sel_Comp))
11697 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
11698 loop
11699 Sel_Comp := Parent (Sel_Comp);
11700 end loop;
11702 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
11703 end Prefix_Is_Formal_Parameter;
11705 -- Start of processing for Has_Inferable_Discriminants
11707 begin
11708 -- For selected components, the subtype of the selector must be a
11709 -- constrained Unchecked_Union. If the component is subject to a
11710 -- per-object constraint, then the enclosing object must have inferable
11711 -- discriminants.
11713 if Nkind (N) = N_Selected_Component then
11714 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
11716 -- A small hack. If we have a per-object constrained selected
11717 -- component of a formal parameter, return True since we do not
11718 -- know the actual parameter association yet.
11720 if Prefix_Is_Formal_Parameter (N) then
11721 return True;
11723 -- Otherwise, check the enclosing object and the selector
11725 else
11726 return Has_Inferable_Discriminants (Prefix (N))
11727 and then Has_Inferable_Discriminants (Selector_Name (N));
11728 end if;
11730 -- The call to Has_Inferable_Discriminants will determine whether
11731 -- the selector has a constrained Unchecked_Union nominal type.
11733 else
11734 return Has_Inferable_Discriminants (Selector_Name (N));
11735 end if;
11737 -- A qualified expression has inferable discriminants if its subtype
11738 -- mark is a constrained Unchecked_Union subtype.
11740 elsif Nkind (N) = N_Qualified_Expression then
11741 return Is_Unchecked_Union (Etype (Subtype_Mark (N)))
11742 and then Is_Constrained (Etype (Subtype_Mark (N)));
11744 -- For all other names, it is sufficient to have a constrained
11745 -- Unchecked_Union nominal subtype.
11747 else
11748 return Is_Unchecked_Union (Base_Type (Etype (N)))
11749 and then Is_Constrained (Etype (N));
11750 end if;
11751 end Has_Inferable_Discriminants;
11753 -------------------------------
11754 -- Insert_Dereference_Action --
11755 -------------------------------
11757 procedure Insert_Dereference_Action (N : Node_Id) is
11759 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
11760 -- Return true if type of P is derived from Checked_Pool;
11762 -----------------------------
11763 -- Is_Checked_Storage_Pool --
11764 -----------------------------
11766 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
11767 T : Entity_Id;
11769 begin
11770 if No (P) then
11771 return False;
11772 end if;
11774 T := Etype (P);
11775 while T /= Etype (T) loop
11776 if Is_RTE (T, RE_Checked_Pool) then
11777 return True;
11778 else
11779 T := Etype (T);
11780 end if;
11781 end loop;
11783 return False;
11784 end Is_Checked_Storage_Pool;
11786 -- Local variables
11788 Typ : constant Entity_Id := Etype (N);
11789 Desig : constant Entity_Id := Available_View (Designated_Type (Typ));
11790 Loc : constant Source_Ptr := Sloc (N);
11791 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
11792 Pnod : constant Node_Id := Parent (N);
11794 Addr : Entity_Id;
11795 Alig : Entity_Id;
11796 Deref : Node_Id;
11797 Size : Entity_Id;
11798 Size_Bits : Node_Id;
11799 Stmt : Node_Id;
11801 -- Start of processing for Insert_Dereference_Action
11803 begin
11804 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
11806 -- Do not re-expand a dereference which has already been processed by
11807 -- this routine.
11809 if Has_Dereference_Action (Pnod) then
11810 return;
11812 -- Do not perform this type of expansion for internally-generated
11813 -- dereferences.
11815 elsif not Comes_From_Source (Original_Node (Pnod)) then
11816 return;
11818 -- A dereference action is only applicable to objects which have been
11819 -- allocated on a checked pool.
11821 elsif not Is_Checked_Storage_Pool (Pool) then
11822 return;
11823 end if;
11825 -- Extract the address of the dereferenced object. Generate:
11827 -- Addr : System.Address := <N>'Pool_Address;
11829 Addr := Make_Temporary (Loc, 'P');
11831 Insert_Action (N,
11832 Make_Object_Declaration (Loc,
11833 Defining_Identifier => Addr,
11834 Object_Definition =>
11835 New_Occurrence_Of (RTE (RE_Address), Loc),
11836 Expression =>
11837 Make_Attribute_Reference (Loc,
11838 Prefix => Duplicate_Subexpr_Move_Checks (N),
11839 Attribute_Name => Name_Pool_Address)));
11841 -- Calculate the size of the dereferenced object. Generate:
11843 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
11845 Deref :=
11846 Make_Explicit_Dereference (Loc,
11847 Prefix => Duplicate_Subexpr_Move_Checks (N));
11848 Set_Has_Dereference_Action (Deref);
11850 Size_Bits :=
11851 Make_Attribute_Reference (Loc,
11852 Prefix => Deref,
11853 Attribute_Name => Name_Size);
11855 -- Special case of an unconstrained array: need to add descriptor size
11857 if Is_Array_Type (Desig)
11858 and then not Is_Constrained (First_Subtype (Desig))
11859 then
11860 Size_Bits :=
11861 Make_Op_Add (Loc,
11862 Left_Opnd =>
11863 Make_Attribute_Reference (Loc,
11864 Prefix =>
11865 New_Occurrence_Of (First_Subtype (Desig), Loc),
11866 Attribute_Name => Name_Descriptor_Size),
11867 Right_Opnd => Size_Bits);
11868 end if;
11870 Size := Make_Temporary (Loc, 'S');
11871 Insert_Action (N,
11872 Make_Object_Declaration (Loc,
11873 Defining_Identifier => Size,
11874 Object_Definition =>
11875 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
11876 Expression =>
11877 Make_Op_Divide (Loc,
11878 Left_Opnd => Size_Bits,
11879 Right_Opnd => Make_Integer_Literal (Loc, System_Storage_Unit))));
11881 -- Calculate the alignment of the dereferenced object. Generate:
11882 -- Alig : constant Storage_Count := <N>.all'Alignment;
11884 Deref :=
11885 Make_Explicit_Dereference (Loc,
11886 Prefix => Duplicate_Subexpr_Move_Checks (N));
11887 Set_Has_Dereference_Action (Deref);
11889 Alig := Make_Temporary (Loc, 'A');
11890 Insert_Action (N,
11891 Make_Object_Declaration (Loc,
11892 Defining_Identifier => Alig,
11893 Object_Definition =>
11894 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
11895 Expression =>
11896 Make_Attribute_Reference (Loc,
11897 Prefix => Deref,
11898 Attribute_Name => Name_Alignment)));
11900 -- A dereference of a controlled object requires special processing. The
11901 -- finalization machinery requests additional space from the underlying
11902 -- pool to allocate and hide two pointers. As a result, a checked pool
11903 -- may mark the wrong memory as valid. Since checked pools do not have
11904 -- knowledge of hidden pointers, we have to bring the two pointers back
11905 -- in view in order to restore the original state of the object.
11907 if Needs_Finalization (Desig) then
11909 -- Adjust the address and size of the dereferenced object. Generate:
11910 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
11912 Stmt :=
11913 Make_Procedure_Call_Statement (Loc,
11914 Name =>
11915 New_Occurrence_Of (RTE (RE_Adjust_Controlled_Dereference), Loc),
11916 Parameter_Associations => New_List (
11917 New_Occurrence_Of (Addr, Loc),
11918 New_Occurrence_Of (Size, Loc),
11919 New_Occurrence_Of (Alig, Loc)));
11921 -- Class-wide types complicate things because we cannot determine
11922 -- statically whether the actual object is truly controlled. We must
11923 -- generate a runtime check to detect this property. Generate:
11925 -- if Needs_Finalization (<N>.all'Tag) then
11926 -- <Stmt>;
11927 -- end if;
11929 if Is_Class_Wide_Type (Desig) then
11930 Deref :=
11931 Make_Explicit_Dereference (Loc,
11932 Prefix => Duplicate_Subexpr_Move_Checks (N));
11933 Set_Has_Dereference_Action (Deref);
11935 Stmt :=
11936 Make_Implicit_If_Statement (N,
11937 Condition =>
11938 Make_Function_Call (Loc,
11939 Name =>
11940 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
11941 Parameter_Associations => New_List (
11942 Make_Attribute_Reference (Loc,
11943 Prefix => Deref,
11944 Attribute_Name => Name_Tag))),
11945 Then_Statements => New_List (Stmt));
11946 end if;
11948 Insert_Action (N, Stmt);
11949 end if;
11951 -- Generate:
11952 -- Dereference (Pool, Addr, Size, Alig);
11954 Insert_Action (N,
11955 Make_Procedure_Call_Statement (Loc,
11956 Name =>
11957 New_Occurrence_Of
11958 (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
11959 Parameter_Associations => New_List (
11960 New_Occurrence_Of (Pool, Loc),
11961 New_Occurrence_Of (Addr, Loc),
11962 New_Occurrence_Of (Size, Loc),
11963 New_Occurrence_Of (Alig, Loc))));
11965 -- Mark the explicit dereference as processed to avoid potential
11966 -- infinite expansion.
11968 Set_Has_Dereference_Action (Pnod);
11970 exception
11971 when RE_Not_Available =>
11972 return;
11973 end Insert_Dereference_Action;
11975 --------------------------------
11976 -- Integer_Promotion_Possible --
11977 --------------------------------
11979 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
11980 Operand : constant Node_Id := Expression (N);
11981 Operand_Type : constant Entity_Id := Etype (Operand);
11982 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
11984 begin
11985 pragma Assert (Nkind (N) = N_Type_Conversion);
11987 return
11989 -- We only do the transformation for source constructs. We assume
11990 -- that the expander knows what it is doing when it generates code.
11992 Comes_From_Source (N)
11994 -- If the operand type is Short_Integer or Short_Short_Integer,
11995 -- then we will promote to Integer, which is available on all
11996 -- targets, and is sufficient to ensure no intermediate overflow.
11997 -- Furthermore it is likely to be as efficient or more efficient
11998 -- than using the smaller type for the computation so we do this
11999 -- unconditionally.
12001 and then
12002 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
12003 or else
12004 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
12006 -- Test for interesting operation, which includes addition,
12007 -- division, exponentiation, multiplication, subtraction, absolute
12008 -- value and unary negation. Unary "+" is omitted since it is a
12009 -- no-op and thus can't overflow.
12011 and then Nkind_In (Operand, N_Op_Abs,
12012 N_Op_Add,
12013 N_Op_Divide,
12014 N_Op_Expon,
12015 N_Op_Minus,
12016 N_Op_Multiply,
12017 N_Op_Subtract);
12018 end Integer_Promotion_Possible;
12020 ------------------------------
12021 -- Make_Array_Comparison_Op --
12022 ------------------------------
12024 -- This is a hand-coded expansion of the following generic function:
12026 -- generic
12027 -- type elem is (<>);
12028 -- type index is (<>);
12029 -- type a is array (index range <>) of elem;
12031 -- function Gnnn (X : a; Y: a) return boolean is
12032 -- J : index := Y'first;
12034 -- begin
12035 -- if X'length = 0 then
12036 -- return false;
12038 -- elsif Y'length = 0 then
12039 -- return true;
12041 -- else
12042 -- for I in X'range loop
12043 -- if X (I) = Y (J) then
12044 -- if J = Y'last then
12045 -- exit;
12046 -- else
12047 -- J := index'succ (J);
12048 -- end if;
12050 -- else
12051 -- return X (I) > Y (J);
12052 -- end if;
12053 -- end loop;
12055 -- return X'length > Y'length;
12056 -- end if;
12057 -- end Gnnn;
12059 -- Note that since we are essentially doing this expansion by hand, we
12060 -- do not need to generate an actual or formal generic part, just the
12061 -- instantiated function itself.
12063 -- Perhaps we could have the actual generic available in the run-time,
12064 -- obtained by rtsfind, and actually expand a real instantiation ???
12066 function Make_Array_Comparison_Op
12067 (Typ : Entity_Id;
12068 Nod : Node_Id) return Node_Id
12070 Loc : constant Source_Ptr := Sloc (Nod);
12072 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
12073 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
12074 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
12075 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
12077 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
12079 Loop_Statement : Node_Id;
12080 Loop_Body : Node_Id;
12081 If_Stat : Node_Id;
12082 Inner_If : Node_Id;
12083 Final_Expr : Node_Id;
12084 Func_Body : Node_Id;
12085 Func_Name : Entity_Id;
12086 Formals : List_Id;
12087 Length1 : Node_Id;
12088 Length2 : Node_Id;
12090 begin
12091 -- if J = Y'last then
12092 -- exit;
12093 -- else
12094 -- J := index'succ (J);
12095 -- end if;
12097 Inner_If :=
12098 Make_Implicit_If_Statement (Nod,
12099 Condition =>
12100 Make_Op_Eq (Loc,
12101 Left_Opnd => New_Occurrence_Of (J, Loc),
12102 Right_Opnd =>
12103 Make_Attribute_Reference (Loc,
12104 Prefix => New_Occurrence_Of (Y, Loc),
12105 Attribute_Name => Name_Last)),
12107 Then_Statements => New_List (
12108 Make_Exit_Statement (Loc)),
12110 Else_Statements =>
12111 New_List (
12112 Make_Assignment_Statement (Loc,
12113 Name => New_Occurrence_Of (J, Loc),
12114 Expression =>
12115 Make_Attribute_Reference (Loc,
12116 Prefix => New_Occurrence_Of (Index, Loc),
12117 Attribute_Name => Name_Succ,
12118 Expressions => New_List (New_Occurrence_Of (J, Loc))))));
12120 -- if X (I) = Y (J) then
12121 -- if ... end if;
12122 -- else
12123 -- return X (I) > Y (J);
12124 -- end if;
12126 Loop_Body :=
12127 Make_Implicit_If_Statement (Nod,
12128 Condition =>
12129 Make_Op_Eq (Loc,
12130 Left_Opnd =>
12131 Make_Indexed_Component (Loc,
12132 Prefix => New_Occurrence_Of (X, Loc),
12133 Expressions => New_List (New_Occurrence_Of (I, Loc))),
12135 Right_Opnd =>
12136 Make_Indexed_Component (Loc,
12137 Prefix => New_Occurrence_Of (Y, Loc),
12138 Expressions => New_List (New_Occurrence_Of (J, Loc)))),
12140 Then_Statements => New_List (Inner_If),
12142 Else_Statements => New_List (
12143 Make_Simple_Return_Statement (Loc,
12144 Expression =>
12145 Make_Op_Gt (Loc,
12146 Left_Opnd =>
12147 Make_Indexed_Component (Loc,
12148 Prefix => New_Occurrence_Of (X, Loc),
12149 Expressions => New_List (New_Occurrence_Of (I, Loc))),
12151 Right_Opnd =>
12152 Make_Indexed_Component (Loc,
12153 Prefix => New_Occurrence_Of (Y, Loc),
12154 Expressions => New_List (
12155 New_Occurrence_Of (J, Loc)))))));
12157 -- for I in X'range loop
12158 -- if ... end if;
12159 -- end loop;
12161 Loop_Statement :=
12162 Make_Implicit_Loop_Statement (Nod,
12163 Identifier => Empty,
12165 Iteration_Scheme =>
12166 Make_Iteration_Scheme (Loc,
12167 Loop_Parameter_Specification =>
12168 Make_Loop_Parameter_Specification (Loc,
12169 Defining_Identifier => I,
12170 Discrete_Subtype_Definition =>
12171 Make_Attribute_Reference (Loc,
12172 Prefix => New_Occurrence_Of (X, Loc),
12173 Attribute_Name => Name_Range))),
12175 Statements => New_List (Loop_Body));
12177 -- if X'length = 0 then
12178 -- return false;
12179 -- elsif Y'length = 0 then
12180 -- return true;
12181 -- else
12182 -- for ... loop ... end loop;
12183 -- return X'length > Y'length;
12184 -- end if;
12186 Length1 :=
12187 Make_Attribute_Reference (Loc,
12188 Prefix => New_Occurrence_Of (X, Loc),
12189 Attribute_Name => Name_Length);
12191 Length2 :=
12192 Make_Attribute_Reference (Loc,
12193 Prefix => New_Occurrence_Of (Y, Loc),
12194 Attribute_Name => Name_Length);
12196 Final_Expr :=
12197 Make_Op_Gt (Loc,
12198 Left_Opnd => Length1,
12199 Right_Opnd => Length2);
12201 If_Stat :=
12202 Make_Implicit_If_Statement (Nod,
12203 Condition =>
12204 Make_Op_Eq (Loc,
12205 Left_Opnd =>
12206 Make_Attribute_Reference (Loc,
12207 Prefix => New_Occurrence_Of (X, Loc),
12208 Attribute_Name => Name_Length),
12209 Right_Opnd =>
12210 Make_Integer_Literal (Loc, 0)),
12212 Then_Statements =>
12213 New_List (
12214 Make_Simple_Return_Statement (Loc,
12215 Expression => New_Occurrence_Of (Standard_False, Loc))),
12217 Elsif_Parts => New_List (
12218 Make_Elsif_Part (Loc,
12219 Condition =>
12220 Make_Op_Eq (Loc,
12221 Left_Opnd =>
12222 Make_Attribute_Reference (Loc,
12223 Prefix => New_Occurrence_Of (Y, Loc),
12224 Attribute_Name => Name_Length),
12225 Right_Opnd =>
12226 Make_Integer_Literal (Loc, 0)),
12228 Then_Statements =>
12229 New_List (
12230 Make_Simple_Return_Statement (Loc,
12231 Expression => New_Occurrence_Of (Standard_True, Loc))))),
12233 Else_Statements => New_List (
12234 Loop_Statement,
12235 Make_Simple_Return_Statement (Loc,
12236 Expression => Final_Expr)));
12238 -- (X : a; Y: a)
12240 Formals := New_List (
12241 Make_Parameter_Specification (Loc,
12242 Defining_Identifier => X,
12243 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
12245 Make_Parameter_Specification (Loc,
12246 Defining_Identifier => Y,
12247 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
12249 -- function Gnnn (...) return boolean is
12250 -- J : index := Y'first;
12251 -- begin
12252 -- if ... end if;
12253 -- end Gnnn;
12255 Func_Name := Make_Temporary (Loc, 'G');
12257 Func_Body :=
12258 Make_Subprogram_Body (Loc,
12259 Specification =>
12260 Make_Function_Specification (Loc,
12261 Defining_Unit_Name => Func_Name,
12262 Parameter_Specifications => Formals,
12263 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
12265 Declarations => New_List (
12266 Make_Object_Declaration (Loc,
12267 Defining_Identifier => J,
12268 Object_Definition => New_Occurrence_Of (Index, Loc),
12269 Expression =>
12270 Make_Attribute_Reference (Loc,
12271 Prefix => New_Occurrence_Of (Y, Loc),
12272 Attribute_Name => Name_First))),
12274 Handled_Statement_Sequence =>
12275 Make_Handled_Sequence_Of_Statements (Loc,
12276 Statements => New_List (If_Stat)));
12278 return Func_Body;
12279 end Make_Array_Comparison_Op;
12281 ---------------------------
12282 -- Make_Boolean_Array_Op --
12283 ---------------------------
12285 -- For logical operations on boolean arrays, expand in line the following,
12286 -- replacing 'and' with 'or' or 'xor' where needed:
12288 -- function Annn (A : typ; B: typ) return typ is
12289 -- C : typ;
12290 -- begin
12291 -- for J in A'range loop
12292 -- C (J) := A (J) op B (J);
12293 -- end loop;
12294 -- return C;
12295 -- end Annn;
12297 -- Here typ is the boolean array type
12299 function Make_Boolean_Array_Op
12300 (Typ : Entity_Id;
12301 N : Node_Id) return Node_Id
12303 Loc : constant Source_Ptr := Sloc (N);
12305 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
12306 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
12307 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
12308 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
12310 A_J : Node_Id;
12311 B_J : Node_Id;
12312 C_J : Node_Id;
12313 Op : Node_Id;
12315 Formals : List_Id;
12316 Func_Name : Entity_Id;
12317 Func_Body : Node_Id;
12318 Loop_Statement : Node_Id;
12320 begin
12321 A_J :=
12322 Make_Indexed_Component (Loc,
12323 Prefix => New_Occurrence_Of (A, Loc),
12324 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12326 B_J :=
12327 Make_Indexed_Component (Loc,
12328 Prefix => New_Occurrence_Of (B, Loc),
12329 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12331 C_J :=
12332 Make_Indexed_Component (Loc,
12333 Prefix => New_Occurrence_Of (C, Loc),
12334 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12336 if Nkind (N) = N_Op_And then
12337 Op :=
12338 Make_Op_And (Loc,
12339 Left_Opnd => A_J,
12340 Right_Opnd => B_J);
12342 elsif Nkind (N) = N_Op_Or then
12343 Op :=
12344 Make_Op_Or (Loc,
12345 Left_Opnd => A_J,
12346 Right_Opnd => B_J);
12348 else
12349 Op :=
12350 Make_Op_Xor (Loc,
12351 Left_Opnd => A_J,
12352 Right_Opnd => B_J);
12353 end if;
12355 Loop_Statement :=
12356 Make_Implicit_Loop_Statement (N,
12357 Identifier => Empty,
12359 Iteration_Scheme =>
12360 Make_Iteration_Scheme (Loc,
12361 Loop_Parameter_Specification =>
12362 Make_Loop_Parameter_Specification (Loc,
12363 Defining_Identifier => J,
12364 Discrete_Subtype_Definition =>
12365 Make_Attribute_Reference (Loc,
12366 Prefix => New_Occurrence_Of (A, Loc),
12367 Attribute_Name => Name_Range))),
12369 Statements => New_List (
12370 Make_Assignment_Statement (Loc,
12371 Name => C_J,
12372 Expression => Op)));
12374 Formals := New_List (
12375 Make_Parameter_Specification (Loc,
12376 Defining_Identifier => A,
12377 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
12379 Make_Parameter_Specification (Loc,
12380 Defining_Identifier => B,
12381 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
12383 Func_Name := Make_Temporary (Loc, 'A');
12384 Set_Is_Inlined (Func_Name);
12386 Func_Body :=
12387 Make_Subprogram_Body (Loc,
12388 Specification =>
12389 Make_Function_Specification (Loc,
12390 Defining_Unit_Name => Func_Name,
12391 Parameter_Specifications => Formals,
12392 Result_Definition => New_Occurrence_Of (Typ, Loc)),
12394 Declarations => New_List (
12395 Make_Object_Declaration (Loc,
12396 Defining_Identifier => C,
12397 Object_Definition => New_Occurrence_Of (Typ, Loc))),
12399 Handled_Statement_Sequence =>
12400 Make_Handled_Sequence_Of_Statements (Loc,
12401 Statements => New_List (
12402 Loop_Statement,
12403 Make_Simple_Return_Statement (Loc,
12404 Expression => New_Occurrence_Of (C, Loc)))));
12406 return Func_Body;
12407 end Make_Boolean_Array_Op;
12409 -----------------------------------------
12410 -- Minimized_Eliminated_Overflow_Check --
12411 -----------------------------------------
12413 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is
12414 begin
12415 return
12416 Is_Signed_Integer_Type (Etype (N))
12417 and then Overflow_Check_Mode in Minimized_Or_Eliminated;
12418 end Minimized_Eliminated_Overflow_Check;
12420 --------------------------------
12421 -- Optimize_Length_Comparison --
12422 --------------------------------
12424 procedure Optimize_Length_Comparison (N : Node_Id) is
12425 Loc : constant Source_Ptr := Sloc (N);
12426 Typ : constant Entity_Id := Etype (N);
12427 Result : Node_Id;
12429 Left : Node_Id;
12430 Right : Node_Id;
12431 -- First and Last attribute reference nodes, which end up as left and
12432 -- right operands of the optimized result.
12434 Is_Zero : Boolean;
12435 -- True for comparison operand of zero
12437 Comp : Node_Id;
12438 -- Comparison operand, set only if Is_Zero is false
12440 Ent : Entity_Id;
12441 -- Entity whose length is being compared
12443 Index : Node_Id;
12444 -- Integer_Literal node for length attribute expression, or Empty
12445 -- if there is no such expression present.
12447 Ityp : Entity_Id;
12448 -- Type of array index to which 'Length is applied
12450 Op : Node_Kind := Nkind (N);
12451 -- Kind of comparison operator, gets flipped if operands backwards
12453 function Is_Optimizable (N : Node_Id) return Boolean;
12454 -- Tests N to see if it is an optimizable comparison value (defined as
12455 -- constant zero or one, or something else where the value is known to
12456 -- be positive and in the range of 32-bits, and where the corresponding
12457 -- Length value is also known to be 32-bits. If result is true, sets
12458 -- Is_Zero, Ityp, and Comp accordingly.
12460 function Is_Entity_Length (N : Node_Id) return Boolean;
12461 -- Tests if N is a length attribute applied to a simple entity. If so,
12462 -- returns True, and sets Ent to the entity, and Index to the integer
12463 -- literal provided as an attribute expression, or to Empty if none.
12464 -- Also returns True if the expression is a generated type conversion
12465 -- whose expression is of the desired form. This latter case arises
12466 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
12467 -- to check for being in range, which is not needed in this context.
12468 -- Returns False if neither condition holds.
12470 function Prepare_64 (N : Node_Id) return Node_Id;
12471 -- Given a discrete expression, returns a Long_Long_Integer typed
12472 -- expression representing the underlying value of the expression.
12473 -- This is done with an unchecked conversion to the result type. We
12474 -- use unchecked conversion to handle the enumeration type case.
12476 ----------------------
12477 -- Is_Entity_Length --
12478 ----------------------
12480 function Is_Entity_Length (N : Node_Id) return Boolean is
12481 begin
12482 if Nkind (N) = N_Attribute_Reference
12483 and then Attribute_Name (N) = Name_Length
12484 and then Is_Entity_Name (Prefix (N))
12485 then
12486 Ent := Entity (Prefix (N));
12488 if Present (Expressions (N)) then
12489 Index := First (Expressions (N));
12490 else
12491 Index := Empty;
12492 end if;
12494 return True;
12496 elsif Nkind (N) = N_Type_Conversion
12497 and then not Comes_From_Source (N)
12498 then
12499 return Is_Entity_Length (Expression (N));
12501 else
12502 return False;
12503 end if;
12504 end Is_Entity_Length;
12506 --------------------
12507 -- Is_Optimizable --
12508 --------------------
12510 function Is_Optimizable (N : Node_Id) return Boolean is
12511 Val : Uint;
12512 OK : Boolean;
12513 Lo : Uint;
12514 Hi : Uint;
12515 Indx : Node_Id;
12517 begin
12518 if Compile_Time_Known_Value (N) then
12519 Val := Expr_Value (N);
12521 if Val = Uint_0 then
12522 Is_Zero := True;
12523 Comp := Empty;
12524 return True;
12526 elsif Val = Uint_1 then
12527 Is_Zero := False;
12528 Comp := Empty;
12529 return True;
12530 end if;
12531 end if;
12533 -- Here we have to make sure of being within 32-bits
12535 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
12537 if not OK
12538 or else Lo < Uint_1
12539 or else Hi > UI_From_Int (Int'Last)
12540 then
12541 return False;
12542 end if;
12544 -- Comparison value was within range, so now we must check the index
12545 -- value to make sure it is also within 32-bits.
12547 Indx := First_Index (Etype (Ent));
12549 if Present (Index) then
12550 for J in 2 .. UI_To_Int (Intval (Index)) loop
12551 Next_Index (Indx);
12552 end loop;
12553 end if;
12555 Ityp := Etype (Indx);
12557 if Esize (Ityp) > 32 then
12558 return False;
12559 end if;
12561 Is_Zero := False;
12562 Comp := N;
12563 return True;
12564 end Is_Optimizable;
12566 ----------------
12567 -- Prepare_64 --
12568 ----------------
12570 function Prepare_64 (N : Node_Id) return Node_Id is
12571 begin
12572 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
12573 end Prepare_64;
12575 -- Start of processing for Optimize_Length_Comparison
12577 begin
12578 -- Nothing to do if not a comparison
12580 if Op not in N_Op_Compare then
12581 return;
12582 end if;
12584 -- Nothing to do if special -gnatd.P debug flag set
12586 if Debug_Flag_Dot_PP then
12587 return;
12588 end if;
12590 -- Ent'Length op 0/1
12592 if Is_Entity_Length (Left_Opnd (N))
12593 and then Is_Optimizable (Right_Opnd (N))
12594 then
12595 null;
12597 -- 0/1 op Ent'Length
12599 elsif Is_Entity_Length (Right_Opnd (N))
12600 and then Is_Optimizable (Left_Opnd (N))
12601 then
12602 -- Flip comparison to opposite sense
12604 case Op is
12605 when N_Op_Lt => Op := N_Op_Gt;
12606 when N_Op_Le => Op := N_Op_Ge;
12607 when N_Op_Gt => Op := N_Op_Lt;
12608 when N_Op_Ge => Op := N_Op_Le;
12609 when others => null;
12610 end case;
12612 -- Else optimization not possible
12614 else
12615 return;
12616 end if;
12618 -- Fall through if we will do the optimization
12620 -- Cases to handle:
12622 -- X'Length = 0 => X'First > X'Last
12623 -- X'Length = 1 => X'First = X'Last
12624 -- X'Length = n => X'First + (n - 1) = X'Last
12626 -- X'Length /= 0 => X'First <= X'Last
12627 -- X'Length /= 1 => X'First /= X'Last
12628 -- X'Length /= n => X'First + (n - 1) /= X'Last
12630 -- X'Length >= 0 => always true, warn
12631 -- X'Length >= 1 => X'First <= X'Last
12632 -- X'Length >= n => X'First + (n - 1) <= X'Last
12634 -- X'Length > 0 => X'First <= X'Last
12635 -- X'Length > 1 => X'First < X'Last
12636 -- X'Length > n => X'First + (n - 1) < X'Last
12638 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
12639 -- X'Length <= 1 => X'First >= X'Last
12640 -- X'Length <= n => X'First + (n - 1) >= X'Last
12642 -- X'Length < 0 => always false (warn)
12643 -- X'Length < 1 => X'First > X'Last
12644 -- X'Length < n => X'First + (n - 1) > X'Last
12646 -- Note: for the cases of n (not constant 0,1), we require that the
12647 -- corresponding index type be integer or shorter (i.e. not 64-bit),
12648 -- and the same for the comparison value. Then we do the comparison
12649 -- using 64-bit arithmetic (actually long long integer), so that we
12650 -- cannot have overflow intefering with the result.
12652 -- First deal with warning cases
12654 if Is_Zero then
12655 case Op is
12657 -- X'Length >= 0
12659 when N_Op_Ge =>
12660 Rewrite (N,
12661 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
12662 Analyze_And_Resolve (N, Typ);
12663 Warn_On_Known_Condition (N);
12664 return;
12666 -- X'Length < 0
12668 when N_Op_Lt =>
12669 Rewrite (N,
12670 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
12671 Analyze_And_Resolve (N, Typ);
12672 Warn_On_Known_Condition (N);
12673 return;
12675 when N_Op_Le =>
12676 if Constant_Condition_Warnings
12677 and then Comes_From_Source (Original_Node (N))
12678 then
12679 Error_Msg_N ("could replace by ""'=""?c?", N);
12680 end if;
12682 Op := N_Op_Eq;
12684 when others =>
12685 null;
12686 end case;
12687 end if;
12689 -- Build the First reference we will use
12691 Left :=
12692 Make_Attribute_Reference (Loc,
12693 Prefix => New_Occurrence_Of (Ent, Loc),
12694 Attribute_Name => Name_First);
12696 if Present (Index) then
12697 Set_Expressions (Left, New_List (New_Copy (Index)));
12698 end if;
12700 -- If general value case, then do the addition of (n - 1), and
12701 -- also add the needed conversions to type Long_Long_Integer.
12703 if Present (Comp) then
12704 Left :=
12705 Make_Op_Add (Loc,
12706 Left_Opnd => Prepare_64 (Left),
12707 Right_Opnd =>
12708 Make_Op_Subtract (Loc,
12709 Left_Opnd => Prepare_64 (Comp),
12710 Right_Opnd => Make_Integer_Literal (Loc, 1)));
12711 end if;
12713 -- Build the Last reference we will use
12715 Right :=
12716 Make_Attribute_Reference (Loc,
12717 Prefix => New_Occurrence_Of (Ent, Loc),
12718 Attribute_Name => Name_Last);
12720 if Present (Index) then
12721 Set_Expressions (Right, New_List (New_Copy (Index)));
12722 end if;
12724 -- If general operand, convert Last reference to Long_Long_Integer
12726 if Present (Comp) then
12727 Right := Prepare_64 (Right);
12728 end if;
12730 -- Check for cases to optimize
12732 -- X'Length = 0 => X'First > X'Last
12733 -- X'Length < 1 => X'First > X'Last
12734 -- X'Length < n => X'First + (n - 1) > X'Last
12736 if (Is_Zero and then Op = N_Op_Eq)
12737 or else (not Is_Zero and then Op = N_Op_Lt)
12738 then
12739 Result :=
12740 Make_Op_Gt (Loc,
12741 Left_Opnd => Left,
12742 Right_Opnd => Right);
12744 -- X'Length = 1 => X'First = X'Last
12745 -- X'Length = n => X'First + (n - 1) = X'Last
12747 elsif not Is_Zero and then Op = N_Op_Eq then
12748 Result :=
12749 Make_Op_Eq (Loc,
12750 Left_Opnd => Left,
12751 Right_Opnd => Right);
12753 -- X'Length /= 0 => X'First <= X'Last
12754 -- X'Length > 0 => X'First <= X'Last
12756 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
12757 Result :=
12758 Make_Op_Le (Loc,
12759 Left_Opnd => Left,
12760 Right_Opnd => Right);
12762 -- X'Length /= 1 => X'First /= X'Last
12763 -- X'Length /= n => X'First + (n - 1) /= X'Last
12765 elsif not Is_Zero and then Op = N_Op_Ne then
12766 Result :=
12767 Make_Op_Ne (Loc,
12768 Left_Opnd => Left,
12769 Right_Opnd => Right);
12771 -- X'Length >= 1 => X'First <= X'Last
12772 -- X'Length >= n => X'First + (n - 1) <= X'Last
12774 elsif not Is_Zero and then Op = N_Op_Ge then
12775 Result :=
12776 Make_Op_Le (Loc,
12777 Left_Opnd => Left,
12778 Right_Opnd => Right);
12780 -- X'Length > 1 => X'First < X'Last
12781 -- X'Length > n => X'First + (n = 1) < X'Last
12783 elsif not Is_Zero and then Op = N_Op_Gt then
12784 Result :=
12785 Make_Op_Lt (Loc,
12786 Left_Opnd => Left,
12787 Right_Opnd => Right);
12789 -- X'Length <= 1 => X'First >= X'Last
12790 -- X'Length <= n => X'First + (n - 1) >= X'Last
12792 elsif not Is_Zero and then Op = N_Op_Le then
12793 Result :=
12794 Make_Op_Ge (Loc,
12795 Left_Opnd => Left,
12796 Right_Opnd => Right);
12798 -- Should not happen at this stage
12800 else
12801 raise Program_Error;
12802 end if;
12804 -- Rewrite and finish up
12806 Rewrite (N, Result);
12807 Analyze_And_Resolve (N, Typ);
12808 return;
12809 end Optimize_Length_Comparison;
12811 ------------------------------
12812 -- Process_Transient_Object --
12813 ------------------------------
12815 procedure Process_Transient_Object
12816 (Decl : Node_Id;
12817 Rel_Node : Node_Id)
12819 Loc : constant Source_Ptr := Sloc (Decl);
12820 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
12821 Obj_Typ : constant Node_Id := Etype (Obj_Id);
12822 Desig_Typ : Entity_Id;
12823 Expr : Node_Id;
12824 Hook_Id : Entity_Id;
12825 Hook_Insert : Node_Id;
12826 Ptr_Id : Entity_Id;
12828 Hook_Context : constant Node_Id := Find_Hook_Context (Rel_Node);
12829 -- The node on which to insert the hook as an action. This is usually
12830 -- the innermost enclosing non-transient construct.
12832 Fin_Context : Node_Id;
12833 -- The node after which to insert the finalization actions of the
12834 -- transient controlled object.
12836 begin
12837 if Is_Boolean_Type (Etype (Rel_Node)) then
12838 Fin_Context := Last (Actions (Rel_Node));
12839 else
12840 Fin_Context := Hook_Context;
12841 end if;
12843 -- Step 1: Create the access type which provides a reference to the
12844 -- transient controlled object.
12846 if Is_Access_Type (Obj_Typ) then
12847 Desig_Typ := Directly_Designated_Type (Obj_Typ);
12848 else
12849 Desig_Typ := Obj_Typ;
12850 end if;
12852 Desig_Typ := Base_Type (Desig_Typ);
12854 -- Generate:
12855 -- Ann : access [all] <Desig_Typ>;
12857 Ptr_Id := Make_Temporary (Loc, 'A');
12859 Insert_Action (Hook_Context,
12860 Make_Full_Type_Declaration (Loc,
12861 Defining_Identifier => Ptr_Id,
12862 Type_Definition =>
12863 Make_Access_To_Object_Definition (Loc,
12864 All_Present => Ekind (Obj_Typ) = E_General_Access_Type,
12865 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc))));
12867 -- Step 2: Create a temporary which acts as a hook to the transient
12868 -- controlled object. Generate:
12870 -- Hook : Ptr_Id := null;
12872 Hook_Id := Make_Temporary (Loc, 'T');
12874 Insert_Action (Hook_Context,
12875 Make_Object_Declaration (Loc,
12876 Defining_Identifier => Hook_Id,
12877 Object_Definition => New_Occurrence_Of (Ptr_Id, Loc)));
12879 -- Mark the hook as created for the purposes of exporting the transient
12880 -- controlled object out of the expression_with_action or if expression.
12881 -- This signals the machinery in Build_Finalizer to treat this case in
12882 -- a special manner.
12884 Set_Status_Flag_Or_Transient_Decl (Hook_Id, Decl);
12886 -- Step 3: Associate the transient object to the hook
12888 -- This must be inserted right after the object declaration, so that
12889 -- the assignment is executed if, and only if, the object is actually
12890 -- created (whereas the declaration of the hook pointer, and the
12891 -- finalization call, may be inserted at an outer level, and may
12892 -- remain unused for some executions, if the actual creation of
12893 -- the object is conditional).
12895 -- The use of unchecked conversion / unrestricted access is needed to
12896 -- avoid an accessibility violation. Note that the finalization code is
12897 -- structured in such a way that the "hook" is processed only when it
12898 -- points to an existing object.
12900 if Is_Access_Type (Obj_Typ) then
12901 Expr :=
12902 Unchecked_Convert_To
12903 (Typ => Ptr_Id,
12904 Expr => New_Occurrence_Of (Obj_Id, Loc));
12905 else
12906 Expr :=
12907 Make_Attribute_Reference (Loc,
12908 Prefix => New_Occurrence_Of (Obj_Id, Loc),
12909 Attribute_Name => Name_Unrestricted_Access);
12910 end if;
12912 -- Generate:
12913 -- Hook := Ptr_Id (Obj_Id);
12914 -- <or>
12915 -- Hook := Obj_Id'Unrestricted_Access;
12917 -- When the transient object is initialized by an aggregate, the hook
12918 -- must capture the object after the last component assignment takes
12919 -- place. Only then is the object fully initialized.
12921 if Ekind (Obj_Id) = E_Variable
12922 and then Present (Last_Aggregate_Assignment (Obj_Id))
12923 then
12924 Hook_Insert := Last_Aggregate_Assignment (Obj_Id);
12926 -- Otherwise the hook seizes the related object immediately
12928 else
12929 Hook_Insert := Decl;
12930 end if;
12932 Insert_After_And_Analyze (Hook_Insert,
12933 Make_Assignment_Statement (Loc,
12934 Name => New_Occurrence_Of (Hook_Id, Loc),
12935 Expression => Expr));
12937 -- Step 4: Finalize the hook after the context has been evaluated or
12938 -- elaborated. Generate:
12940 -- if Hook /= null then
12941 -- [Deep_]Finalize (Hook.all);
12942 -- Hook := null;
12943 -- end if;
12945 -- When the node is part of a return statement, there is no need to
12946 -- insert a finalization call, as the general finalization mechanism
12947 -- (see Build_Finalizer) would take care of the transient controlled
12948 -- object on subprogram exit. Note that it would also be impossible to
12949 -- insert the finalization code after the return statement as this will
12950 -- render it unreachable.
12952 if Nkind (Fin_Context) = N_Simple_Return_Statement then
12953 null;
12955 -- Otherwise finalize the hook
12957 else
12958 Insert_Action_After (Fin_Context,
12959 Make_Implicit_If_Statement (Decl,
12960 Condition =>
12961 Make_Op_Ne (Loc,
12962 Left_Opnd => New_Occurrence_Of (Hook_Id, Loc),
12963 Right_Opnd => Make_Null (Loc)),
12965 Then_Statements => New_List (
12966 Make_Final_Call
12967 (Obj_Ref =>
12968 Make_Explicit_Dereference (Loc,
12969 Prefix => New_Occurrence_Of (Hook_Id, Loc)),
12970 Typ => Desig_Typ),
12972 Make_Assignment_Statement (Loc,
12973 Name => New_Occurrence_Of (Hook_Id, Loc),
12974 Expression => Make_Null (Loc)))));
12975 end if;
12976 end Process_Transient_Object;
12978 ------------------------
12979 -- Rewrite_Comparison --
12980 ------------------------
12982 procedure Rewrite_Comparison (N : Node_Id) is
12983 Warning_Generated : Boolean := False;
12984 -- Set to True if first pass with Assume_Valid generates a warning in
12985 -- which case we skip the second pass to avoid warning overloaded.
12987 Result : Node_Id;
12988 -- Set to Standard_True or Standard_False
12990 begin
12991 if Nkind (N) = N_Type_Conversion then
12992 Rewrite_Comparison (Expression (N));
12993 return;
12995 elsif Nkind (N) not in N_Op_Compare then
12996 return;
12997 end if;
12999 -- Now start looking at the comparison in detail. We potentially go
13000 -- through this loop twice. The first time, Assume_Valid is set False
13001 -- in the call to Compile_Time_Compare. If this call results in a
13002 -- clear result of always True or Always False, that's decisive and
13003 -- we are done. Otherwise we repeat the processing with Assume_Valid
13004 -- set to True to generate additional warnings. We can skip that step
13005 -- if Constant_Condition_Warnings is False.
13007 for AV in False .. True loop
13008 declare
13009 Typ : constant Entity_Id := Etype (N);
13010 Op1 : constant Node_Id := Left_Opnd (N);
13011 Op2 : constant Node_Id := Right_Opnd (N);
13013 Res : constant Compare_Result :=
13014 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
13015 -- Res indicates if compare outcome can be compile time determined
13017 True_Result : Boolean;
13018 False_Result : Boolean;
13020 begin
13021 case N_Op_Compare (Nkind (N)) is
13022 when N_Op_Eq =>
13023 True_Result := Res = EQ;
13024 False_Result := Res = LT or else Res = GT or else Res = NE;
13026 when N_Op_Ge =>
13027 True_Result := Res in Compare_GE;
13028 False_Result := Res = LT;
13030 if Res = LE
13031 and then Constant_Condition_Warnings
13032 and then Comes_From_Source (Original_Node (N))
13033 and then Nkind (Original_Node (N)) = N_Op_Ge
13034 and then not In_Instance
13035 and then Is_Integer_Type (Etype (Left_Opnd (N)))
13036 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
13037 then
13038 Error_Msg_N
13039 ("can never be greater than, could replace by ""'=""?c?",
13041 Warning_Generated := True;
13042 end if;
13044 when N_Op_Gt =>
13045 True_Result := Res = GT;
13046 False_Result := Res in Compare_LE;
13048 when N_Op_Lt =>
13049 True_Result := Res = LT;
13050 False_Result := Res in Compare_GE;
13052 when N_Op_Le =>
13053 True_Result := Res in Compare_LE;
13054 False_Result := Res = GT;
13056 if Res = GE
13057 and then Constant_Condition_Warnings
13058 and then Comes_From_Source (Original_Node (N))
13059 and then Nkind (Original_Node (N)) = N_Op_Le
13060 and then not In_Instance
13061 and then Is_Integer_Type (Etype (Left_Opnd (N)))
13062 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
13063 then
13064 Error_Msg_N
13065 ("can never be less than, could replace by ""'=""?c?", N);
13066 Warning_Generated := True;
13067 end if;
13069 when N_Op_Ne =>
13070 True_Result := Res = NE or else Res = GT or else Res = LT;
13071 False_Result := Res = EQ;
13072 end case;
13074 -- If this is the first iteration, then we actually convert the
13075 -- comparison into True or False, if the result is certain.
13077 if AV = False then
13078 if True_Result or False_Result then
13079 Result := Boolean_Literals (True_Result);
13080 Rewrite (N,
13081 Convert_To (Typ,
13082 New_Occurrence_Of (Result, Sloc (N))));
13083 Analyze_And_Resolve (N, Typ);
13084 Warn_On_Known_Condition (N);
13085 return;
13086 end if;
13088 -- If this is the second iteration (AV = True), and the original
13089 -- node comes from source and we are not in an instance, then give
13090 -- a warning if we know result would be True or False. Note: we
13091 -- know Constant_Condition_Warnings is set if we get here.
13093 elsif Comes_From_Source (Original_Node (N))
13094 and then not In_Instance
13095 then
13096 if True_Result then
13097 Error_Msg_N
13098 ("condition can only be False if invalid values present??",
13100 elsif False_Result then
13101 Error_Msg_N
13102 ("condition can only be True if invalid values present??",
13104 end if;
13105 end if;
13106 end;
13108 -- Skip second iteration if not warning on constant conditions or
13109 -- if the first iteration already generated a warning of some kind or
13110 -- if we are in any case assuming all values are valid (so that the
13111 -- first iteration took care of the valid case).
13113 exit when not Constant_Condition_Warnings;
13114 exit when Warning_Generated;
13115 exit when Assume_No_Invalid_Values;
13116 end loop;
13117 end Rewrite_Comparison;
13119 ----------------------------
13120 -- Safe_In_Place_Array_Op --
13121 ----------------------------
13123 function Safe_In_Place_Array_Op
13124 (Lhs : Node_Id;
13125 Op1 : Node_Id;
13126 Op2 : Node_Id) return Boolean
13128 Target : Entity_Id;
13130 function Is_Safe_Operand (Op : Node_Id) return Boolean;
13131 -- Operand is safe if it cannot overlap part of the target of the
13132 -- operation. If the operand and the target are identical, the operand
13133 -- is safe. The operand can be empty in the case of negation.
13135 function Is_Unaliased (N : Node_Id) return Boolean;
13136 -- Check that N is a stand-alone entity
13138 ------------------
13139 -- Is_Unaliased --
13140 ------------------
13142 function Is_Unaliased (N : Node_Id) return Boolean is
13143 begin
13144 return
13145 Is_Entity_Name (N)
13146 and then No (Address_Clause (Entity (N)))
13147 and then No (Renamed_Object (Entity (N)));
13148 end Is_Unaliased;
13150 ---------------------
13151 -- Is_Safe_Operand --
13152 ---------------------
13154 function Is_Safe_Operand (Op : Node_Id) return Boolean is
13155 begin
13156 if No (Op) then
13157 return True;
13159 elsif Is_Entity_Name (Op) then
13160 return Is_Unaliased (Op);
13162 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
13163 return Is_Unaliased (Prefix (Op));
13165 elsif Nkind (Op) = N_Slice then
13166 return
13167 Is_Unaliased (Prefix (Op))
13168 and then Entity (Prefix (Op)) /= Target;
13170 elsif Nkind (Op) = N_Op_Not then
13171 return Is_Safe_Operand (Right_Opnd (Op));
13173 else
13174 return False;
13175 end if;
13176 end Is_Safe_Operand;
13178 -- Start of processing for Safe_In_Place_Array_Op
13180 begin
13181 -- Skip this processing if the component size is different from system
13182 -- storage unit (since at least for NOT this would cause problems).
13184 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
13185 return False;
13187 -- Cannot do in place stuff on VM_Target since cannot pass addresses
13189 elsif VM_Target /= No_VM then
13190 return False;
13192 -- Cannot do in place stuff if non-standard Boolean representation
13194 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
13195 return False;
13197 elsif not Is_Unaliased (Lhs) then
13198 return False;
13200 else
13201 Target := Entity (Lhs);
13202 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
13203 end if;
13204 end Safe_In_Place_Array_Op;
13206 -----------------------
13207 -- Tagged_Membership --
13208 -----------------------
13210 -- There are two different cases to consider depending on whether the right
13211 -- operand is a class-wide type or not. If not we just compare the actual
13212 -- tag of the left expr to the target type tag:
13214 -- Left_Expr.Tag = Right_Type'Tag;
13216 -- If it is a class-wide type we use the RT function CW_Membership which is
13217 -- usually implemented by looking in the ancestor tables contained in the
13218 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
13220 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
13221 -- function IW_Membership which is usually implemented by looking in the
13222 -- table of abstract interface types plus the ancestor table contained in
13223 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
13225 procedure Tagged_Membership
13226 (N : Node_Id;
13227 SCIL_Node : out Node_Id;
13228 Result : out Node_Id)
13230 Left : constant Node_Id := Left_Opnd (N);
13231 Right : constant Node_Id := Right_Opnd (N);
13232 Loc : constant Source_Ptr := Sloc (N);
13234 Full_R_Typ : Entity_Id;
13235 Left_Type : Entity_Id;
13236 New_Node : Node_Id;
13237 Right_Type : Entity_Id;
13238 Obj_Tag : Node_Id;
13240 begin
13241 SCIL_Node := Empty;
13243 -- Handle entities from the limited view
13245 Left_Type := Available_View (Etype (Left));
13246 Right_Type := Available_View (Etype (Right));
13248 -- In the case where the type is an access type, the test is applied
13249 -- using the designated types (needed in Ada 2012 for implicit anonymous
13250 -- access conversions, for AI05-0149).
13252 if Is_Access_Type (Right_Type) then
13253 Left_Type := Designated_Type (Left_Type);
13254 Right_Type := Designated_Type (Right_Type);
13255 end if;
13257 if Is_Class_Wide_Type (Left_Type) then
13258 Left_Type := Root_Type (Left_Type);
13259 end if;
13261 if Is_Class_Wide_Type (Right_Type) then
13262 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
13263 else
13264 Full_R_Typ := Underlying_Type (Right_Type);
13265 end if;
13267 Obj_Tag :=
13268 Make_Selected_Component (Loc,
13269 Prefix => Relocate_Node (Left),
13270 Selector_Name =>
13271 New_Occurrence_Of (First_Tag_Component (Left_Type), Loc));
13273 if Is_Class_Wide_Type (Right_Type) then
13275 -- No need to issue a run-time check if we statically know that the
13276 -- result of this membership test is always true. For example,
13277 -- considering the following declarations:
13279 -- type Iface is interface;
13280 -- type T is tagged null record;
13281 -- type DT is new T and Iface with null record;
13283 -- Obj1 : T;
13284 -- Obj2 : DT;
13286 -- These membership tests are always true:
13288 -- Obj1 in T'Class
13289 -- Obj2 in T'Class;
13290 -- Obj2 in Iface'Class;
13292 -- We do not need to handle cases where the membership is illegal.
13293 -- For example:
13295 -- Obj1 in DT'Class; -- Compile time error
13296 -- Obj1 in Iface'Class; -- Compile time error
13298 if not Is_Class_Wide_Type (Left_Type)
13299 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
13300 Use_Full_View => True)
13301 or else (Is_Interface (Etype (Right_Type))
13302 and then Interface_Present_In_Ancestor
13303 (Typ => Left_Type,
13304 Iface => Etype (Right_Type))))
13305 then
13306 Result := New_Occurrence_Of (Standard_True, Loc);
13307 return;
13308 end if;
13310 -- Ada 2005 (AI-251): Class-wide applied to interfaces
13312 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
13314 -- Support to: "Iface_CW_Typ in Typ'Class"
13316 or else Is_Interface (Left_Type)
13317 then
13318 -- Issue error if IW_Membership operation not available in a
13319 -- configurable run time setting.
13321 if not RTE_Available (RE_IW_Membership) then
13322 Error_Msg_CRT
13323 ("dynamic membership test on interface types", N);
13324 Result := Empty;
13325 return;
13326 end if;
13328 Result :=
13329 Make_Function_Call (Loc,
13330 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
13331 Parameter_Associations => New_List (
13332 Make_Attribute_Reference (Loc,
13333 Prefix => Obj_Tag,
13334 Attribute_Name => Name_Address),
13335 New_Occurrence_Of (
13336 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
13337 Loc)));
13339 -- Ada 95: Normal case
13341 else
13342 Build_CW_Membership (Loc,
13343 Obj_Tag_Node => Obj_Tag,
13344 Typ_Tag_Node =>
13345 New_Occurrence_Of (
13346 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc),
13347 Related_Nod => N,
13348 New_Node => New_Node);
13350 -- Generate the SCIL node for this class-wide membership test.
13351 -- Done here because the previous call to Build_CW_Membership
13352 -- relocates Obj_Tag.
13354 if Generate_SCIL then
13355 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
13356 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
13357 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
13358 end if;
13360 Result := New_Node;
13361 end if;
13363 -- Right_Type is not a class-wide type
13365 else
13366 -- No need to check the tag of the object if Right_Typ is abstract
13368 if Is_Abstract_Type (Right_Type) then
13369 Result := New_Occurrence_Of (Standard_False, Loc);
13371 else
13372 Result :=
13373 Make_Op_Eq (Loc,
13374 Left_Opnd => Obj_Tag,
13375 Right_Opnd =>
13376 New_Occurrence_Of
13377 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
13378 end if;
13379 end if;
13380 end Tagged_Membership;
13382 ------------------------------
13383 -- Unary_Op_Validity_Checks --
13384 ------------------------------
13386 procedure Unary_Op_Validity_Checks (N : Node_Id) is
13387 begin
13388 if Validity_Checks_On and Validity_Check_Operands then
13389 Ensure_Valid (Right_Opnd (N));
13390 end if;
13391 end Unary_Op_Validity_Checks;
13393 end Exp_Ch4;