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